Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .changeset/34515-resume-approval-bound-to-screen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/spec': patch
'@objectstack/runtime': patch
---

fix(runtime)!: a resume approval is bound to the screen it was given on, so a wizard that moved on is never handed the earlier screen's values

Clause-②: yes (narrowing) — narrowed: an MCP `resume_run` of a declared-gated flow action on a client that is asked in place (form-mode `elicitation`), where the person accepts after the wizard moved on to another screen in the meantime, is now refused `409 RESOURCE_CONFLICT` and resumes nothing, where it resumed the screen the run had moved on to. Widened beside it: `ActionConfirmationHandOff.run.screenNodeId` and `ActionResumeRequest.screenNodeId` are new optional keys, and a host call that omits `screenNodeId` behaves as before.
Metadata-protocol: none
Skills-docs: none — no skill under `skills/**` teaches what a second resume of one run does (`skills/objectstack-ai/SKILL.md` lines 211–217 and `skills/objectstack-platform/SKILL.md` line 344 teach the hand-off, the `confirm` refusal and the elicitation answers only); `content/docs/ui/actions.mdx` gains the rule in this change.

<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is removed, renamed or re-shaped: the two new keys are optional TypeScript members of runtime contracts, and the moved answer is a runtime refusal at the MCP `resume_run` door, so there is nothing stored for a migration to rewrite and no tombstone. -->

**BREAKING** for an MCP client that is asked in place: when the person accepts a `resume_run` of a declared-gated action after the wizard has moved on to another screen (advanced in the app while they were being asked), the call is refused `409 RESOURCE_CONFLICT`, nothing resumes, and the run stays on the screen it is on. Before this change the same call resumed that other screen with the values the agent gave for the first one, as the person. The agent asks again on the screen the run is on now.

A multi-screen wizard stays paused under the same run id as it moves from screen to screen. So when the MCP `resume_run` tool handed a resume to the person, a second resume of the same pending item was still admitted after the first approval had moved the wizard on: the values given for the first screen were submitted to the next screen, as the approver. The same happened when the agent called `resume_run` twice (two pending items), and when the person advanced the wizard in the app before the item was approved.

- The hand-off now names the screen its values were given for: `ActionConfirmationHandOff.run.screenNodeId` (the `ScreenSpec.nodeId` the run was parked on). It is optional and absent only when the engine named no node.
- `dispatchActionResume` takes the same optional `screenNodeId`. A host that stores it with the pending item and passes it back on approval binds the approval to that screen: a run no longer paused on it is refused `409 RESOURCE_CONFLICT`, nothing resumes, and the run stays where it is. A `screenNodeId` that is not a non-empty string is refused `400`.
- A host call that passes no `screenNodeId` resumes the run's current screen, as before. A host should pass it to get the binding.

Unchanged: a single-screen flow, an elicited approval whose run is still on the screen the person was asked about, and an MCP `resume_run` of an action that declares no confirmation.

Limit: a wizard that loops back to the same screen node cannot be told apart by node id; no flow doing that has been found in use.
18 changes: 12 additions & 6 deletions content/docs/ui/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ whose package is disabled is **404** `OBJECT_PACKAGE_DISABLED`.

When the MCP `resume_run` tool hands a resume to the person (an action that
declares `ai.requiresConfirmation: true`, on a host with a pending-confirmation
service), the hand-off carries the paused run: `run: { runId, values }`.
service), the hand-off carries the paused run: `run: { runId, values, screenNodeId }`,
where `screenNodeId` names the screen the values were given for.
Your approve door resumes that run with `dispatchActionResume`, as the person
who approved it, instead of re-implementing the `resume_run` checks:

Expand All @@ -780,6 +781,7 @@ const answer = await dispatchActionResume(kernel, {
environmentId, // optional, placed as for dispatchAction
runId: handOff.run.runId,
values: handOff.run.values,
screenNodeId: handOff.run.screenNodeId, // binds the approval to that screen
confirm: true, // the person's confirmation
});
if (answer.status !== 200) throw answer.body.error;
Expand All @@ -791,11 +793,15 @@ paused and must have been started by them (otherwise **404**
that admits the run must pass every `run_action` check, the record is read
again as them, and the run must be paused on a screen (otherwise **409**). The
run then resumes; it never starts a new run and is never handed off again.
Only a run that is no longer paused is refused: a wizard your approval moved
on to its next screen is still paused under the same run id, so calling again
with the same hand-off resumes that next screen with the same values. Decide
each pending item once in your approve door, and void an item whose run has
moved on. Without `confirm: true`, an action that declares
A wizard stays paused under the same run id as it moves from screen to
screen, so pass `screenNodeId` back: when the run is no longer on that screen
(an earlier approval of the same item moved it on, or the person advanced it
in the app) the call is refused **409** `RESOURCE_CONFLICT`, nothing resumes,
and the run stays where it is. Without `screenNodeId`, the run's current
screen is resumed with the values given. A `screenNodeId` that is `null`, empty
or not a string is refused **400**, so omit the key for an item stored without
one. A wizard that loops back to the same screen is not told apart by its
screen id. Decide each pending item once in your approve door. Without `confirm: true`, an action that declares
`ai.requiresConfirmation: true` is refused **428** `ACTION_CONFIRMATION_REQUIRED`,
and an AI service account is refused whatever it sends. The answer is the REST
envelope, with `resume_run`'s `{ ok, action, objectName, recordId, result }` as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@
// is written and nobody is asked;
// ⭐ CONTROL: the same update through an action that declares no
// confirmation runs at once, and nobody is asked.
// ⭐ [#34515] a `resume_run` hand-off on a two-screen wizard names the
// screen its values were given for; the host approves it through
// `dispatchActionResume` with that screen, and a second approval of
// the same item (or one approved after the person advanced the wizard
// in the app) is refused 409 `RESOURCE_CONFLICT` with the run left on
// the next screen. CONTROL: a single-screen run approved with its
// screen completes.

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { type VerifyStack } from '@objectstack/verify';
import { bootStack } from '@objectstack/verify';
import { MCPServerPlugin } from '@objectstack/mcp';
import { dispatchActionResume } from '@objectstack/runtime';
import { defineStack } from '@objectstack/spec';
import { ObjectSchema, Field } from '@objectstack/spec/data';
import { PermissionSetSchema, type PermissionSet } from '@objectstack/spec/security';
Expand All @@ -38,6 +46,11 @@ import { catalogPermissionSet } from './catalog-permission-set.js';
const OBJECT = 'achoff_ticket';
const GATED = 'achoff_close_ticket';
const PLAIN = 'achoff_flag_ticket';
/** [#34515] A gated flow action whose screen flow is a two-screen wizard, and one whose flow has one screen. */
const WIZARD_ACTION = 'achoff_close_wizard';
const WIZARD_FLOW = 'achoff_close_wizard';
const ONE_SCREEN_ACTION = 'achoff_close_one_screen';
const ONE_SCREEN_FLOW = 'achoff_close_one_screen';
const PERSON = 'achoff-person@verify.test';
const SYS = { context: { isSystem: true } } as const;
const N = 3;
Expand All @@ -60,9 +73,31 @@ const ticket = ObjectSchema.create({
name: PLAIN, label: 'Flag Ticket', objectName: OBJECT, operation: 'update', patch: { status: 'flagged' },
ai: { exposed: true, description: 'Flag the ticket so a person looks at it during the next review.' },
},
{
name: WIZARD_ACTION, label: 'Close Ticket (wizard)', objectName: OBJECT, type: 'flow', target: WIZARD_FLOW,
ai: { exposed: true, description: 'Walk the two-step close wizard for this ticket: a reason, then a final sign-off.', requiresConfirmation: true },
},
{
name: ONE_SCREEN_ACTION, label: 'Close Ticket (one screen)', objectName: OBJECT, type: 'flow', target: ONE_SCREEN_FLOW,
ai: { exposed: true, description: 'Close this ticket after asking for a comment on one screen.', requiresConfirmation: true },
},
],
} as never);

/** A screen node collecting an optional `comment`: both wizard screens accept the same bag. */
const commentScreen = (id: string, title: string) => ({
id, type: 'screen', label: title, config: { title, fields: [{ name: 'comment', label: 'Comment', type: 'text' }] },
});
/** A screen flow walking `screens` in order, start to end. */
const screenFlow = (name: string, screens: Array<ReturnType<typeof commentScreen>>) => {
const path = ['start', ...screens.map((n) => n.id), 'end'];
return {
name, label: name, type: 'screen', status: 'active', runAs: 'user',
nodes: [{ id: 'start', type: 'start', label: 'Start' }, ...screens, { id: 'end', type: 'end', label: 'End' }],
edges: path.slice(1).map((target, i) => ({ id: `e${i}`, source: path[i], target })),
};
};

const stackConfig = defineStack({
manifest: {
id: 'com.dogfood.action-confirmation-handoff',
Expand All @@ -72,8 +107,13 @@ const stackConfig = defineStack({
name: 'Action Confirmation Hand-off Fixture',
description: 'One object with a confirmation-gated declarative update, run over MCP.',
},
requires: ['automation'],
objects: [ticket],
});
flows: [
screenFlow(WIZARD_FLOW, [commentScreen('step_1', 'Step 1: reason'), commentScreen('step_2', 'Step 2: final sign-off')]),
screenFlow(ONE_SCREEN_FLOW, [commentScreen('step_1', 'Comment')]),
],
} as never);

const baselineSet: PermissionSet = PermissionSetSchema.parse({
name: 'achoff_baseline',
Expand Down Expand Up @@ -192,4 +232,65 @@ describe('[#33690] a confirmation-gated action over MCP is handed to the person,
expect(await statusOf(id)).toBe('flagged');
expect(handOffs.length).toBe(before);
}, 120_000);

// ── [#34515] a resume approval is bound to the screen it was given on ──────
const automation = () => (stack.kernel as any).getService('automation');
const screenOf = async (runId: string) => (await automation().getSuspendedScreen(runId))?.nodeId ?? null;
/** The person starts the gated flow action in the app (REST, their confirm): the run pauses on its first screen. */
const startPaused = async (action: string, id: string): Promise<string> => {
const res = await stack.apiAs(person, 'POST', `/actions/${OBJECT}/${action}/${id}`, { [AI_ACTION_CONFIRMATION_MEMBER]: true });
const body: any = await res.json();
expect(res.status, JSON.stringify(body)).toBe(200);
const result = body?.data?.result ?? body?.data;
expect(result?.status, JSON.stringify(body)).toBe('paused');
return String(result.runId);
};
/** The agent's `resume_run`, handed to the person: the hand-off it recorded. */
const handOffOf = async (runId: string, comment: string) => {
const held = await tool('resume_run', { runId, values: { comment } });
expect(held.payload, held.text).toMatchObject({ status: 'pending_confirmation' });
return handOffs.at(-1)!.handOff.run as { runId: string; values: Record<string, unknown>; screenNodeId?: string };
};
/** The host's approve door: the approving person, through `dispatchActionResume`, with the item's screen. */
const approve = (run: { runId: string; values: Record<string, unknown>; screenNodeId?: string }) =>
dispatchActionResume(stack.kernel as never, {
executionContext: { userId: personId } as never, runId: run.runId, values: run.values, screenNodeId: run.screenNodeId, confirm: true,
});

it('⭐ [#34515] the same item approved twice on a two-screen wizard: the first resumes step 1, the second is refused 409', async () => {
const runId = await startPaused(WIZARD_ACTION, await openTicket('wizard-twice'));
const run = await handOffOf(runId, 'meant for step 1');
expect(run.screenNodeId).toBe('step_1');

const first = await approve(run);
expect(first.status, JSON.stringify(first.body)).toBe(200);
expect(await screenOf(runId)).toBe('step_2');

const second = await approve(run);
expect({ status: second.status, code: second.body.error?.code }, JSON.stringify(second.body)).toEqual({ status: 409, code: 'RESOURCE_CONFLICT' });
expect((await automation().getRun(runId))?.status).toBe('paused');
expect(await screenOf(runId), 'the refused approval moved the run').toBe('step_2');
}, 120_000);

it('⭐ [#34515] an item approved after the person advanced the wizard in the app is refused 409', async () => {
const runId = await startPaused(WIZARD_ACTION, await openTicket('wizard-advanced'));
const run = await handOffOf(runId, 'agent, for step 1');
const inApp = await stack.apiAs(person, 'POST', `/automation/${WIZARD_FLOW}/runs/${runId}/resume`, { inputs: { comment: 'typed in the app' } });
expect(inApp.status, await inApp.clone().text()).toBe(200);
expect(await screenOf(runId)).toBe('step_2');

const refused = await approve(run);
expect({ status: refused.status, code: refused.body.error?.code }, JSON.stringify(refused.body)).toEqual({ status: 409, code: 'RESOURCE_CONFLICT' });
expect(await screenOf(runId)).toBe('step_2');
}, 120_000);

it('CONTROL [#34515]: a single-screen run approved with its screen completes', async () => {
const runId = await startPaused(ONE_SCREEN_ACTION, await openTicket('one-screen'));
const run = await handOffOf(runId, 'only screen');
expect(run.screenNodeId).toBe('step_1');

const answer = await approve(run);
expect(answer.status, JSON.stringify(answer.body)).toBe(200);
expect((await automation().getRun(runId))?.status).toBe('completed');
}, 120_000);
});
Loading
Loading