diff --git a/.changeset/33689-ai-action-confirmation-elicited.md b/.changeset/33689-ai-action-confirmation-elicited.md new file mode 100644 index 00000000000..b4c235240a3 --- /dev/null +++ b/.changeset/33689-ai-action-confirmation-elicited.md @@ -0,0 +1,30 @@ +--- +'@objectstack/spec': minor +--- + +feat(spec)!: a declared-gated action on an AI door is confirmed by a person, elicited in the MCP client or handed off to the app, never by the caller's own `confirm: true` + +Clause-②: yes (narrowing) — once the doors implement the contract, a caller's `confirm: true` stops counting on an action declaring `ai.requiresConfirmation: true`; `@objectstack/spec/contracts` gains `AI_ACTION_CONFIRMATION_CAPABILITY`, `ACTION_CONFIRMATION_REFUSAL_REASONS`, `ActionConfirmationRefusalReason`, `ActionConfirmationHandOff`, `ActionConfirmationPending`, `IActionConfirmationService`, `ACTION_CONFIRMATION_SERVICE` and the optional `ActionConfirmationRequiredDetails.reason`. +Metadata-protocol: semantic +Skills-docs: follow-up — `skills/objectstack-ai/SKILL.md` (the MCP tool table: `run_action` / `resume_run` "refused with the same 428 unless the call sends `confirm: true`") teaches the member contract, which is still the door's behaviour in this release; it moves with the change that implements the doors, together with the MCP-served skill text (`packages/mcp/src/skill-md.ts`, `ui/apps/console/src/pages/developer/objectstack-skill.ts`) and the `ToolSchema` prescription in `packages/spec/src/ai/tool.zod.ts`. + + + +**BREAKING, when the implementing change ships**, for an MCP client, an agent or a script that runs an action declaring `ai.requiresConfirmation: true` through `run_action` or `resume_run` by sending `confirm: true` itself. Ruled by the account owner: 「3826 照建议(征询版)」, amended 「我开始重新接受你说的主流方案,就是推送到客户的手机端」 and 「比如最简单的我可以先推送到邮件」. + +⚠ In the release that carries this changeset nothing changes at any door: `run_action` and `resume_run` still run a declared-gated action on `confirm: true` and refuse it without, over stdio and Streamable HTTP alike. Keep sending `confirm: true` until a release's changelog says the doors confirm by elicitation. What ships now is the contract the doors move to, in `@objectstack/spec/contracts` (`AI_ACTION_CONFIRMATION_CAPABILITY`, `contracts/ai-service.ts`): + +- After every admission gate (the subject load included) and before anything runs, the person is asked, in three steps: **elicit** when the client advertised `elicitation` at `initialize` and the door can carry it (one `elicitation/create`, form mode); otherwise **hand off** to the host's pending-confirmation service (`IActionConfirmationService`, registered under `ACTION_CONFIRMATION_SERVICE` = `'ai.actionConfirmation'`), answering `status: 'pending_confirmation'` with a pending id and a plain message, the person the call ran as, and only they, then approving or rejecting in the app, where the action runs as them with every gate and permission re-checked; a handed-off `resume_run` carries its run id and values (`ActionConfirmationHandOff.run`), and approving it resumes that parked run, never a new one; **refuse** only when the host registers no such service (`428 ACTION_CONFIRMATION_REQUIRED`, `details.reason: 'no_confirmation_service'`). +- An elicited `accept` runs the call once; `decline`, `cancel` (an error answer or a dropped connection included) and no answer within the door's bound are refused with reason `declined`, `cancelled` or `timed_out`. A call refused at confirmation may have read its subject and writes nothing. +- The approve and reject doors of the pending-confirmation service take a person's signed-in session to the app and nothing else: they refuse an AI identity, and an API key, token or other service credential, whoever it resolves to. When a pending item lapses, and how the caller learns its outcome, are the host's to define. +- The caller's `confirm` never counts on such a call. +- An AI service account acting for itself is refused whatever it sends, unchanged (ADR-0151 D3 rule 3), with reason `service_account` on every door; it is neither elicited nor handed off. +- An elicited `accept` proves the client answered, not that a person did; an approved hand-off proves a signed-in session to the app answered, not that a person did. +- Outside the amendment: the MCP `delete_record` and `delete_records` gates still read `confirm: true`, and REST `/actions` stays outside the gate for a person. + +The semantic entry `action-requires-confirmation-elicited` carries the same notice, with the same timing, into the upgrade guide and `os migrate meta`. + +What changes for a caller, from the release whose doors implement it: + +- FROM confirm with the person yourself, then send `confirm: true` on `run_action` / `resume_run` TO advertise `elicitation` (form mode) at `initialize` and let the server ask the person, or, from a client that cannot, relay the pending answer's message and let the person decide in the app; `confirm` then confers nothing. +- FROM a script or agent that runs a gated action unattended with `confirm: true` TO a pending answer the person decides in the app (or a refusal on a host with no pending-confirmation service); where the action is safe unattended, its author declares `ai.requiresConfirmation: false`. diff --git a/packages/spec/api-surface/contracts.json b/packages/spec/api-surface/contracts.json index d337bbb0eb7..062f768ad51 100644 --- a/packages/spec/api-surface/contracts.json +++ b/packages/spec/api-surface/contracts.json @@ -2,6 +2,8 @@ "description": "Every exported `name (kind)` of one published entry point of @objectstack/spec — the breadth half of the ADR-0059 backward-compatibility gate. Sharded by entry point (#5837) so two PRs touching different entry points never share a file. Reads the BUILT dist/*.d.ts: regenerate with `pnpm --filter @objectstack/spec gen:api-surface` after a real build.", "entry": "./contracts", "exports": [ + "ACTION_CONFIRMATION_REFUSAL_REASONS (const)", + "ACTION_CONFIRMATION_SERVICE (const)", "AIActionConfirmation (interface)", "AIConversation (interface)", "AIMessage (type)", @@ -14,6 +16,7 @@ "AIToolCall (type)", "AIToolDefinition (interface)", "AIToolResult (type)", + "AI_ACTION_CONFIRMATION_CAPABILITY (const)", "AI_ACTION_CONFIRMATION_MEMBER (const)", "APPROVAL_ACTION_KINDS (const)", "APPROVAL_ACTION_KIND_LABELS (const)", @@ -23,6 +26,9 @@ "APPROVAL_STATUS_LABELS (const)", "AUDIT_CHECKPOINT_SIGNER_SERVICE (const)", "AUDIT_CHECKPOINT_SINK_SERVICE (const)", + "ActionConfirmationHandOff (interface)", + "ActionConfirmationPending (interface)", + "ActionConfirmationRefusalReason (type)", "ActionConfirmationRequiredDetails (interface)", "AdapterContext (interface)", "AdapterSearchOptions (interface)", @@ -135,6 +141,7 @@ "HttpResponseObserver (type)", "IAIConversationService (interface)", "IAIService (interface)", + "IActionConfirmationService (interface)", "IAdvancedServiceRegistry (interface)", "IAnalyticsService (interface)", "IAppLifecycleService (interface)", diff --git a/packages/spec/export-origins/contracts.json b/packages/spec/export-origins/contracts.json index 41d842b1fb3..136cdb67531 100644 --- a/packages/spec/export-origins/contracts.json +++ b/packages/spec/export-origins/contracts.json @@ -2,6 +2,8 @@ "description": "Which SOURCE DECLARATION each name exported by one public entry point of @objectstack/spec resolves to, after its alias chain is unwound: `# ()`. Two exports share an origin string iff they are the same declaration — so equal origins across two entries are a harmless re-export, and different origins under one name are the #4411 dual-source trap. Generated from src/ (no build needed) and read by the export-surface pin tests, which compare against it instead of each building their own ts.createProgram — that was ~55s of compilation per CI lap and a non-deterministic timeout that ejected unrelated PRs from the merge queue (#4796). Sharded by entry point (#5837) so two retirement PRs never share a file. Carries NO line numbers: the pins asserted the line as `\\d+`, and recording it would rewrite this artifact on every edit that shifts a line in any .zod.ts. Regenerate with `pnpm --filter @objectstack/spec gen:export-origins` and read the diff.", "entry": "./contracts", "exports": { + "ACTION_CONFIRMATION_REFUSAL_REASONS": "src/contracts/ai-service.ts#ACTION_CONFIRMATION_REFUSAL_REASONS (const)", + "ACTION_CONFIRMATION_SERVICE": "src/contracts/ai-service.ts#ACTION_CONFIRMATION_SERVICE (const)", "AIActionConfirmation": "src/contracts/ai-service.ts#AIActionConfirmation (interface)", "AIConversation": "src/contracts/ai-service.ts#AIConversation (interface)", "AIMessage": "src/contracts/ai-service.ts#AIMessage (type)", @@ -14,6 +16,7 @@ "AIToolCall": "src/contracts/ai-service.ts#AIToolCall (type)", "AIToolDefinition": "src/contracts/ai-service.ts#AIToolDefinition (interface)", "AIToolResult": "src/contracts/ai-service.ts#AIToolResult (type)", + "AI_ACTION_CONFIRMATION_CAPABILITY": "src/contracts/ai-service.ts#AI_ACTION_CONFIRMATION_CAPABILITY (const)", "AI_ACTION_CONFIRMATION_MEMBER": "src/contracts/ai-service.ts#AI_ACTION_CONFIRMATION_MEMBER (const)", "APPROVAL_ACTION_KINDS": "src/contracts/approval-service.ts#APPROVAL_ACTION_KINDS (const)", "APPROVAL_ACTION_KIND_LABELS": "src/contracts/approval-service.ts#APPROVAL_ACTION_KIND_LABELS (const)", @@ -23,6 +26,9 @@ "APPROVAL_STATUS_LABELS": "src/contracts/approval-service.ts#APPROVAL_STATUS_LABELS (const)", "AUDIT_CHECKPOINT_SIGNER_SERVICE": "src/contracts/audit-checkpoint.ts#AUDIT_CHECKPOINT_SIGNER_SERVICE (const)", "AUDIT_CHECKPOINT_SINK_SERVICE": "src/contracts/audit-checkpoint.ts#AUDIT_CHECKPOINT_SINK_SERVICE (const)", + "ActionConfirmationHandOff": "src/contracts/ai-service.ts#ActionConfirmationHandOff (interface)", + "ActionConfirmationPending": "src/contracts/ai-service.ts#ActionConfirmationPending (interface)", + "ActionConfirmationRefusalReason": "src/contracts/ai-service.ts#ActionConfirmationRefusalReason (type)", "ActionConfirmationRequiredDetails": "src/contracts/ai-service.ts#ActionConfirmationRequiredDetails (interface)", "AdapterContext": "src/contracts/knowledge-adapter.ts#AdapterContext (interface)", "AdapterSearchOptions": "src/contracts/knowledge-adapter.ts#AdapterSearchOptions (interface)", @@ -135,6 +141,7 @@ "HttpResponseObserver": "src/contracts/http-server.ts#HttpResponseObserver (type)", "IAIConversationService": "src/contracts/ai-service.ts#IAIConversationService (interface)", "IAIService": "src/contracts/ai-service.ts#IAIService (interface)", + "IActionConfirmationService": "src/contracts/ai-service.ts#IActionConfirmationService (interface)", "IAdvancedServiceRegistry": "src/contracts/service-registry.ts#IAdvancedServiceRegistry (interface)", "IAnalyticsService": "src/contracts/analytics-service.ts#IAnalyticsService (interface)", "IAppLifecycleService": "src/contracts/app-lifecycle-service.ts#IAppLifecycleService (interface)", diff --git a/packages/spec/src/contracts/action-confirmation-contract.pin.test.ts b/packages/spec/src/contracts/action-confirmation-contract.pin.test.ts index 8af09e5d5fb..b2e0bdce8f0 100644 --- a/packages/spec/src/contracts/action-confirmation-contract.pin.test.ts +++ b/packages/spec/src/contracts/action-confirmation-contract.pin.test.ts @@ -46,10 +46,21 @@ import { ErrorCode, standardSynonymOf, } from '../api/error-code-ledger.zod'; +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + import { + ACTION_CONFIRMATION_REFUSAL_REASONS, + ACTION_CONFIRMATION_SERVICE, + AI_ACTION_CONFIRMATION_CAPABILITY, AI_ACTION_CONFIRMATION_MEMBER, type AIActionConfirmation, + type ActionConfirmationHandOff, + type ActionConfirmationPending, + type ActionConfirmationRefusalReason, type ActionConfirmationRequiredDetails, + type IActionConfirmationService, } from './ai-service'; /** The refusal code the contract names. */ @@ -137,3 +148,182 @@ describe('action-confirmation contract — an unconfirmed gated action is refuse expect(String(stringy.confirm)).toBe('true'); }); }); + +/** + * The amended contract: a person confirms a declared-gated action, through + * MCP elicitation when the client can be asked and through a hand-off to the + * host's pending-confirmation service otherwise, refused only when the host + * has none; the caller's own `confirm` never counts (ruling + * 「3826 照建议(征询版)」, objectstack-ai/cloud#3826 item 6, as amended on + * the card). + * + * Pinned here: the capability's spelling, the service slot the hand-off seam + * is registered under, the CLOSED refusal-reason set (a + * hand-off is not in it), that `details.reason` is typed to it and stays + * optional while the member gates still answer without it, the pending + * answer's discriminant and the hand-off seam's shape (a `resume_run` call + * carries its run), that the amendment adds + * no second refusal code, and that the contract's own text names every reason, + * every answer and the three steps, so the types and the prose cannot drift + * apart. NOT pinned: any door behaviour, which the implementing change lands + * and pins. + */ +describe('action-confirmation contract, as amended: a person confirms, elicited or handed off', () => { + const HERE = dirname(fileURLToPath(import.meta.url)); + const CONTRACT_SOURCE = join(HERE, 'ai-service.ts'); + + /** The capability constant's docblock, comment gutters folded into spaces. */ + function capabilityProse(): string { + const source = readFileSync(CONTRACT_SOURCE, 'utf8'); + const at = source.indexOf('export const AI_ACTION_CONFIRMATION_CAPABILITY'); + expect(at, '`AI_ACTION_CONFIRMATION_CAPABILITY` moved — re-anchor this pin').toBeGreaterThan(-1); + const open = source.lastIndexOf('/**', at); + return source.slice(open, at).replace(/\n\s*\*\s?/g, ' ').replace(/\s+/g, ' '); + } + + it('the capability is MCP\'s own spelling, one exported constant', () => { + expect(AI_ACTION_CONFIRMATION_CAPABILITY).toBe('elicitation'); + // Control: it is not the member; the two travel on different channels. + expect(AI_ACTION_CONFIRMATION_CAPABILITY).not.toBe(AI_ACTION_CONFIRMATION_MEMBER); + }); + + it('the hand-off seam has ONE service slot a door and a host agree on', () => { + expect(ACTION_CONFIRMATION_SERVICE).toBe('ai.actionConfirmation'); + // Step 3 turns on that slot, by name, and on nothing else. + expect(capabilityProse()).toContain( + '**Refuse**, only when the host registers no service under {@link ACTION_CONFIRMATION_SERVICE}', + ); + }); + + it('the refusal reasons are a CLOSED set of exactly five', () => { + expect([...ACTION_CONFIRMATION_REFUSAL_REASONS]).toEqual([ + 'declined', + 'cancelled', + 'timed_out', + 'no_confirmation_service', + 'service_account', + ]); + // Controls: an accepted elicitation and a hand-off are not refusals, and a + // client that cannot elicit is handed off rather than refused for that. + const reasons = ACTION_CONFIRMATION_REFUSAL_REASONS as readonly string[]; + for (const notARefusal of ['accepted', 'pending_confirmation', 'elicitation_unsupported']) { + expect(reasons.includes(notARefusal), `${notARefusal} must not be a refusal reason`).toBe(false); + } + }); + + it('`details.reason` is typed to the closed set, and optional', () => { + const declined = asDetails({ + actionName: 'archive_account', + objectName: 'account', + confirmationMember: AI_ACTION_CONFIRMATION_MEMBER, + reason: 'declined', + }); + expect(declined.reason).toBe('declined'); + // Optional: a member gate (the MCP delete tools, a door before the + // implementing change) still answers without it. + const memberGate = asDetails({ actionName: 'delete_record', confirmationMember: AI_ACTION_CONFIRMATION_MEMBER }); + expect(memberGate.reason).toBeUndefined(); + + const nearMiss = asDetails({ + actionName: 'archive_account', + confirmationMember: AI_ACTION_CONFIRMATION_MEMBER, + // @ts-expect-error — not a reason the contract names; the set is closed. + reason: 'denied', + }); + expect(String(nearMiss.reason)).toBe('denied'); + const reason: ActionConfirmationRefusalReason = 'timed_out'; + expect(ACTION_CONFIRMATION_REFUSAL_REASONS).toContain(reason); + }); + + it('a hand-off answers with the pending discriminant, through an optional host seam', async () => { + const pending: ActionConfirmationPending = { + status: 'pending_confirmation', + pendingId: 'pc_1', + message: 'A confirmation was sent to you.', + actionName: 'archive_account', + objectName: 'account', + }; + expect(pending.status).toBe('pending_confirmation'); + const wrongStatus: ActionConfirmationPending = { + // @ts-expect-error — the discriminant is one literal; the in-app chat's + // older `pending_approval` envelope is not this answer. + status: 'pending_approval', + pendingId: 'pc_1', + message: 'm', + actionName: 'archive_account', + }; + expect(String(wrongStatus.status)).toBe('pending_approval'); + + const handOff: ActionConfirmationHandOff = { + actionName: 'archive_account', objectName: 'account', recordId: 'a1', params: {}, door: 'mcp', + }; + // A `resume_run` hand-off carries the parked run and the screen's values, + // so approving it resumes THAT run rather than starting a new one. + const resumeHandOff: ActionConfirmationHandOff = { + actionName: 'approve_discount', params: {}, door: 'mcp', + run: { runId: 'run_7', values: { discount: 12 } }, + }; + const runWithoutValues: ActionConfirmationHandOff = { + actionName: 'approve_discount', params: {}, door: 'mcp', + // @ts-expect-error — the run leg carries the values it resumes with. + run: { runId: 'run_7' }, + }; + expect(runWithoutValues.run?.runId).toBe('run_7'); + const service: IActionConfirmationService = { + submit: async (h) => ({ pendingId: h.run ? `pc_resume_${h.run.runId}` : `pc_${h.actionName}` }), + }; + await expect(service.submit(handOff, {} as never)).resolves.toEqual({ pendingId: 'pc_archive_account' }); + await expect(service.submit(resumeHandOff, {} as never)).resolves.toEqual({ pendingId: 'pc_resume_run_7' }); + }); + + it('the amendment keeps ONE refusal code: no second code is registered for it', () => { + const all = Object.values(ERROR_CODE_LEDGER).flat() as string[]; + expect(all).toContain(CODE); + // Every reason rides `details.reason` under the one registered code. + expect(all.filter((code) => /ELICIT|CONFIRMATION_(DECLINED|CANCELLED|TIMED_OUT|UNSUPPORTED)/.test(code))).toEqual([]); + }); + + it('the contract text names every reason and every elicitation answer', () => { + const prose = capabilityProse(); + expect(prose.length, 'the capability docblock read empty').toBeGreaterThan(3000); + for (const reason of ACTION_CONFIRMATION_REFUSAL_REASONS) { + expect(prose, `the contract no longer states the \`${reason}\` case`).toContain(`\`${reason}\``); + } + for (const answer of ['`accept`', '`decline`', '`cancel`', 'timed out', 'then approved in the app', 'then rejected in the app']) { + expect(prose, `the contract no longer says what ${answer} does`).toContain(answer); + } + for (const step of ['**Elicit**', '**Hand off**', '**Refuse**, only when the host registers no service']) { + expect(prose, `the contract dropped the step ${step}`).toContain(step); + } + expect(prose, 'the approve and reject doors no longer take only a person\'s signed-in session').toMatch( + /approve and reject doors of the pending-confirmation service take a person's signed-in session to the app and nothing else/, + ); + expect(prose, 'the contract dropped the AI-identity refusal on the approve and reject doors').toMatch(/They refuse an AI identity/); + expect(prose, 'the approve and reject doors no longer refuse a credential that resolves to the person').toMatch( + /also refuse an API key, a delegated or OAuth token and any other service credential, whoever it resolves to/, + ); + expect(prose, 'the contract no longer says who decides a pending item').toMatch( + /The person who approves or rejects is the person the call ran as .*and nobody else/, + ); + expect(prose, 'the contract no longer leaves lapse and outcome delivery to the host').toMatch( + /When a pending item lapses, and how the caller learns its outcome, are the host's to define and outside this contract/, + ); + expect(prose, 'approving a handed-off resume_run no longer resumes the parked run').toMatch( + /approving it resumes THAT parked run with the values the call carried, as the approver, who must be the run's owner as today; it never starts a new run/, + ); + expect(prose, 'the contract dropped the re-check at approval').toMatch(/every gate and permission re-checked/); + expect(prose, 'the contract dropped the trust bound of an elicited answer').toMatch(/proves that the CLIENT answered, not that a person did/); + expect(prose, 'the contract dropped the trust bound of an approved hand-off').toMatch( + /An approval of a hand-off proves that a signed-in session to the app answered, not that a person did/, + ); + expect(prose, 'the contract claims a hand-off proves a person again').not.toMatch(/is the stronger of the two/); + expect(prose, 'the contract claims a refused call read nothing again').not.toMatch(/no record was read/); + expect(prose, 'the contract dropped "the caller\'s `confirm` never counts"').toMatch(/The caller's `confirm` never counts/); + expect(prose, 'the contract dropped the unchanged AI service account rule').toMatch( + /An AI service account: unchanged\*\* \(ADR-0151 D3 rule 3\)/, + ); + expect(prose, 'the refusal for a host without the pending-confirmation service no longer names the remedy').toMatch( + /use a client that supports confirmation, or to perform the action in the app/, + ); + }); +}); diff --git a/packages/spec/src/contracts/ai-service.ts b/packages/spec/src/contracts/ai-service.ts index f4bff0446d5..c73c190e56b 100644 --- a/packages/spec/src/contracts/ai-service.ts +++ b/packages/spec/src/contracts/ai-service.ts @@ -238,6 +238,16 @@ export interface AIToolDefinition { * — there is no header there for it to ride on. A free-form header would * also be an OPEN channel, which is the one thing a safety member must not * be. + * + * ## Amended: on a declared-gated action the member stops confirming + * + * The amended contract ({@link AI_ACTION_CONFIRMATION_CAPABILITY}) has a + * person confirm an action declaring `ai.requiresConfirmation: true`, through + * an elicitation or a hand-off to the app, and the member never counts on such + * a call. That is what the doors enforce from the change that implements the + * amendment; until it ships they still gate on the member as described above + * and in {@link AIActionConfirmation}. The MCP `delete_record` and + * `delete_records` gates are outside the amendment and keep reading it. */ export const AI_ACTION_CONFIRMATION_MEMBER = 'confirm'; @@ -246,6 +256,19 @@ export const AI_ACTION_CONFIRMATION_MEMBER = 'confirm'; * member, declared once here and mixed into each door's own request shape * rather than restated by it. * + * ## Amended: the member contract is the FROM half + * + * For an action declaring `ai.requiresConfirmation: true`, the contract is + * now a person's confirmation ({@link AI_ACTION_CONFIRMATION_CAPABILITY}): + * the server elicits it through the client, or hands the call to the host's + * pending-confirmation service for the person to decide in the app, and the + * caller's own `confirm` never counts. What follows describes the member contract the + * doors enforce until the change that implements the amendment; the + * predicate section (the DECLARED flag, never the listing heuristic) and the + * AI service account rule carry over unchanged. The MCP `delete_record` and + * `delete_records` gates are outside the amendment and keep reading the + * member as described below. + * * ## What the member means * * `confirm: true` asserts that the human in the loop has approved THIS call. @@ -374,8 +397,306 @@ export interface ActionConfirmationRequiredDetails { * the refusal instead of restating it. An AI service account's refusal * carries it too, but no retry of that account's succeeds: a person runs * the action (ADR-0151 D3 rule 3). + * + * Under the amended contract ({@link AI_ACTION_CONFIRMATION_CAPABILITY}), + * once the doors implement it, no retry with the member runs a + * declared-gated action: the member confers nothing there, and `reason` + * says what the caller can do. */ confirmationMember: typeof AI_ACTION_CONFIRMATION_MEMBER; + /** + * Which case of the amended contract refused the call; see + * {@link ActionConfirmationRefusalReason}. Every door implementing that + * contract sets it on a declared-gated refusal, the AI service account's + * included on every door. A refusal without it comes from a member gate: + * a door before the implementing change, or the MCP `delete_record` / + * `delete_records` gate. + */ + reason?: ActionConfirmationRefusalReason; +} + +// --------------------------------------------------------------------------- +// Action Confirmation by Elicitation or Hand-off — the amended contract +// --------------------------------------------------------------------------- + +/** + * The MCP client capability a person's in-client confirmation travels + * through: `elicitation`, which an MCP client advertises in the + * `capabilities` of its `initialize` request. + * + * ONE exported constant for the same reason {@link AI_ACTION_CONFIRMATION_MEMBER} + * is one: the door that reads the capability and the client that advertises + * it have to agree on its spelling. + * + * ## Why the contract was amended + * + * Under the member contract ({@link AIActionConfirmation}) the confirmation of + * a declared-gated action is the caller's own `confirm: true`, an + * unverifiable claim: the gate made FORGETTING loud and did not prove a + * person. Measured on published 18.4.0, an agent sent `confirm: true` itself + * on an irreversible action in 2 of 6 runs, reading the person's instruction + * as consent. The account owner ruled the fix on objectstack-ai/cloud#3826 + * (「3826 照建议(征询版)」, phase 1, card 1 of its item-6 chain) and amended + * it on the card that carries this contract (「我开始重新接受你说的主流方案,就是推送到客户的手机端」, + * 「比如最简单的我可以先推送到邮件」): the server asks the person, in the + * client when the client can be asked and in the app otherwise, and the + * caller's own answer never counts. + * + * ## The contract + * + * 1. **Which calls.** A call on an action whose author declared + * `ai.requiresConfirmation: true` (the DECLARED flag, never the listing + * heuristic; {@link AIActionConfirmation} keeps that predicate), through an + * AI door: the MCP `run_action` tool, and the MCP `resume_run` tool for the + * candidate action it would run. Every caller acting for a person is + * covered, OAuth token or API key, a person or an agent acting for one + * (`onBehalfOf`). + * 2. **When the person is asked.** After EVERY admission gate has passed, + * the subject load and the caller-scope subject verdict included, so + * nobody is asked to confirm a call that is refused anyway, and before + * anything dispatches. A call refused at confirmation, or never confirmed, + * may therefore have READ its subject under the caller's permissions; it + * writes nothing. + * 3. **How the person is asked — three steps, in order.** + * 1. **Elicit**, when the client advertised `elicitation` at `initialize` + * for the session the call rides on and the door can carry a request to + * it while the call is open. The server sends ONE MCP + * `elicitation/create` request in form mode, naming the action, the + * object and the record it runs on (its id, and its title when the + * subject load read one). An empty `elicitation: {}` reads as form mode + * (MCP 2025-11-25). The answer is the result's `action` alone; any + * `content` is not read. + * 2. **Hand off**, otherwise: the door hands the call to the host's + * pending-confirmation service ({@link IActionConfirmationService}, the + * service registered under {@link ACTION_CONFIRMATION_SERVICE}) and + * answers the caller with {@link ActionConfirmationPending}: a pending + * id and a plain message that a confirmation was sent to the person. + * Nothing runs then. The host shows the pending item to the person in + * the app and notifies them (an emailed link, for example); the person + * signs in and approves or rejects it there. The person who approves or + * rejects is the person the call ran as (its `userId`, the person an + * agent acts for), and nobody else: another user who can see the item + * cannot decide it. When a pending item lapses, + * and how the caller learns its outcome, are the host's to define and + * outside this contract. + * 3. **Refuse**, only when the host registers no service under + * {@link ACTION_CONFIRMATION_SERVICE}: reason + * `no_confirmation_service`. The message tells the person to use a + * client that supports confirmation, or to perform the action in the + * app. + * A door or a client that cannot elicit is never refused for that alone: + * it takes step 2, or step 3 when the host has no service. No step ever + * falls back to the member. + * 4. **What each answer does.** + * - Elicited `accept`: the call dispatches as any admitted call does, + * exactly once, under the caller's permissions. The confirmation covers + * that one call: nothing is stored, and the next call is asked again. + * - Elicited `decline`: the person said no. Refused, reason `declined`; + * nothing runs. The caller does not retry unless the person asks again. + * - Elicited `cancel`: the person dismissed the question without choosing. + * Refused, reason `cancelled`; nothing runs. An error answer to the + * elicitation request, or a connection that drops before an answer, is + * read the same way. + * - Elicitation timed out: no answer within the door's bound, which the + * implementing door names. Refused, reason `timed_out`; nothing runs, + * and an answer that arrives later runs nothing. + * - Handed off, then approved in the app: the action runs as the person + * who approved it, through the same admitted sequence, every gate and + * permission re-checked at that moment; a refusal there runs nothing and + * is recorded on the pending item. A handed-off `resume_run` call + * carries its run ({@link ActionConfirmationHandOff.run}): approving it + * resumes THAT parked run with the values the call carried, as the + * approver, who must be the run's owner as today; it never starts a new + * run of the flow. + * - Handed off, then rejected in the app: nothing runs. + * The approve and reject doors of the pending-confirmation service take a + * person's signed-in session to the app and nothing else. They refuse an + * AI identity: an agent principal (`principalKind: 'agent'`, with or + * without `onBehalfOf`), and any call that arrives through an AI door. They + * also refuse an API key, a delegated or OAuth token and any other service + * credential, whoever it resolves to: a person's own API key resolves as + * that person, and is refused there all the same. A person decides; an + * agent, a script or a credential never decides for one. + * 5. **The caller's `confirm` never counts.** On a declared-gated call the + * member ({@link AI_ACTION_CONFIRMATION_MEMBER}) confers nothing: not with + * an elicitation, not in place of one, not in place of a hand-off, and not + * when the host has no service. + * 6. **An AI service account: unchanged** (ADR-0151 D3 rule 3). An agent + * principal acting for itself, with no `onBehalfOf`, is refused whatever it + * sends, reason `service_account`. It is neither elicited (no person stands + * behind the account, and its own client could answer) nor handed off. The + * one admitted sequence asks this rule at every door that runs an action, + * so REST `/actions` and a host door carry the same `reason` on that + * refusal. + * + * Every refusal keeps the registered envelope: `428`, + * `ACTION_CONFIRMATION_REQUIRED`, and `error.details` shaped as + * {@link ActionConfirmationRequiredDetails}, whose `reason` says which case + * above it is. A hand-off is not a refusal: it is the successful answer + * {@link ActionConfirmationPending}. + * + * ## What an answer proves + * + * An elicited `accept` proves that the CLIENT answered, not that a person + * did. The amendment closes the measured hole, an agent model answering for + * the person inside a client that would have asked its user; a caller that + * writes its own client can advertise `elicitation` and answer `accept` + * itself, bounded by its credentials as before. + * + * An approval of a hand-off proves that a signed-in session to the app + * answered, not that a person did: the approve and reject doors refuse every + * credential but a person's session, so a caller holding only the person's + * API key or token cannot approve its own hand-off, and one that holds the + * person's session (their password, or a browser they are signed in to) can, + * bounded by that session as any action in the app is. Each answer is as + * strong as what it takes to give it; neither proves a human. + * + * ## Its bounds + * + * - The MCP `delete_record` and `delete_records` gates are not declared-gated + * actions: they read the member, as {@link AIActionConfirmation} describes, + * until a decision moves them. + * - REST `/actions` stays outside the gate for a person, as before. + * + * ## When it binds + * + * This text is the contract from the release that carries it. The doors + * implement it in the change that enforces it, which also moves the shipped + * prescriptions that describe the member. Until that change ships, every door + * still gates on the member as {@link AIActionConfirmation} describes: the + * MCP `run_action` and `resume_run` tools run a declared-gated action on + * `confirm: true`, over stdio and over Streamable HTTP alike, and that reading + * is the FROM half of the breaking note. + * + * Measured at the amendment: the long-lived stdio server keeps the client's + * `initialize` capabilities and can send a request to the client while a call + * is open. The Streamable HTTP door (`handleHttpRequest` in + * `packages/mcp/src/mcp-server-runtime.ts`) builds a fresh server per request, + * issues no session id and answers in JSON, so as built it can do neither: + * under step 2 its callers are handed off. Whether, and how, that door comes + * to elicit is the implementing change's decision. + */ +export const AI_ACTION_CONFIRMATION_CAPABILITY = 'elicitation'; + +/** + * Why a declared-gated call was refused under the amended contract + * ({@link AI_ACTION_CONFIRMATION_CAPABILITY}), carried as + * {@link ActionConfirmationRequiredDetails.reason}. A CLOSED set; a hand-off is + * not among them, because it is not a refusal: + * + * - `declined`: the person answered an elicitation with no. + * - `cancelled`: the person dismissed the elicitation, the client answered it + * with an error, or the connection dropped first. + * - `timed_out`: no answer to the elicitation within the door's bound. + * - `no_confirmation_service`: the client could not be elicited and the host + * registers no pending-confirmation service ({@link IActionConfirmationService}) + * under {@link ACTION_CONFIRMATION_SERVICE}. + * The person uses a client that supports confirmation, or performs the + * action in the app. + * - `service_account`: an AI service account, which never confirms an action + * for itself (ADR-0151 D3 rule 3). A person runs the action. + */ +export const ACTION_CONFIRMATION_REFUSAL_REASONS = [ + 'declined', + 'cancelled', + 'timed_out', + 'no_confirmation_service', + 'service_account', +] as const; + +/** One member of {@link ACTION_CONFIRMATION_REFUSAL_REASONS}. */ +export type ActionConfirmationRefusalReason = (typeof ACTION_CONFIRMATION_REFUSAL_REASONS)[number]; + +/** + * The call an AI door hands to the host's pending-confirmation service (step 2 + * of {@link AI_ACTION_CONFIRMATION_CAPABILITY}): enough to show the person what + * they are asked to approve, and to run it again, as them, once they do. + */ +export interface ActionConfirmationHandOff { + /** The declarative action name the call addressed. */ + actionName: string; + /** The object the action operates on; omitted for object-less actions. */ + objectName?: string; + /** The package namespace the call qualified the action with, if any. */ + namespace?: string; + /** The record the action runs on, when it takes one. */ + recordId?: string; + /** The action params the call carried, after the door's own checks. */ + params: Record; + /** The AI door the call arrived through, e.g. `mcp`. */ + door: string; + /** + * The parked run a `resume_run` call continues: present exactly when the + * call is a `resume_run`, absent for `run_action`. Approval resumes this + * run with `values`, as the approver, who must be the run's owner; it + * never starts a new run, and `params` is then empty. + */ + run?: { + /** The paused run the call resumes. */ + runId: string; + /** The screen's field values the call carried. */ + values: Record; + }; +} + +/** + * The answer an AI door gives the caller when it hands a declared-gated call + * off (step 2 of {@link AI_ACTION_CONFIRMATION_CAPABILITY}). A success, not a + * refusal: the call was accepted for a person's decision, and nothing has run. + */ +export interface ActionConfirmationPending { + /** The discriminant: the call waits on a person's confirmation in the app. */ + status: 'pending_confirmation'; + /** The pending item's id in the host's pending-confirmation service. */ + pendingId: string; + /** A plain message for the person: a confirmation was sent to them, and where to answer it. */ + message: string; + /** The declarative action name the call addressed. */ + actionName: string; + /** The object the action operates on; omitted for object-less actions. */ + objectName?: string; +} + +/** + * The service slot a {@link IActionConfirmationService} is registered under + * (step 2 of {@link AI_ACTION_CONFIRMATION_CAPABILITY}). The open build ships + * none; with none registered, a door that cannot elicit refuses, reason + * `no_confirmation_service` (step 3). + */ +export const ACTION_CONFIRMATION_SERVICE = 'ai.actionConfirmation'; + +/** + * The host's pending-confirmation service: the hand-off seam of the amended + * contract ({@link AI_ACTION_CONFIRMATION_CAPABILITY}, step 2), registered + * under {@link ACTION_CONFIRMATION_SERVICE}. OPTIONAL — a host without one + * refuses, reason `no_confirmation_service` (step 3), which is the case of a + * bare open-source deployment. + * + * The service owns what happens after the hand-off: it stores the pending + * item, shows it to the person the call ran as in the app and notifies them, + * and serves the approve and reject doors. Only that person decides the item. + * Those doors take a person's signed-in session and nothing else: they refuse + * an AI identity (an agent principal, with or without `onBehalfOf`, and any + * call through an AI door) and an API key, token or other service credential, + * whoever it resolves to. On approval the action runs as the approving person + * through the same admitted sequence, every gate and permission re-checked; a + * hand-off that carries a run ({@link ActionConfirmationHandOff.run}) resumes + * that run instead of starting one. On rejection nothing runs. When an item + * lapses, and how the caller learns its outcome, are the host's to define. A + * host may build it over its own pending-action store; the + * `held` outcome of {@link IAIService.approvePendingAction} is the existing + * precedent for a write that waits on a person outside the conversation. + */ +export interface IActionConfirmationService { + /** + * Record `handOff` for the person the caller acts for, notify them, and + * return the pending item's id. Runs nothing. + * + * @param handOff - The call to confirm + * @param context - The caller's execution context: who is asked, and as + * whose call the item is recorded + */ + submit(handOff: ActionConfirmationHandOff, context: ExecutionContext): Promise<{ pendingId: string }>; } // --------------------------------------------------------------------------- diff --git a/packages/spec/src/migrations/entries/semantic/18.action-requires-confirmation-elicited.ts b/packages/spec/src/migrations/entries/semantic/18.action-requires-confirmation-elicited.ts new file mode 100644 index 00000000000..d8061a43dd9 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/18.action-requires-confirmation-elicited.ts @@ -0,0 +1,45 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// The confirmation contract of `ai.requiresConfirmation: true` is amended +// (`AI_ACTION_CONFIRMATION_CAPABILITY`, `contracts/ai-service.ts`): a person +// confirms, elicited in the MCP client or handed off to the app, and the +// caller's own `confirm: true` stops counting once the doors implement it. A +// structured TODO, not a D2 conversion: nothing an author wrote is rewritten +// or refused, and whether a gated action still needs a person is the author's +// judgement. Written conditionally on purpose: in the release that first +// carries this entry the doors still gate on `confirm: true`. +export const entry: SemanticMigration = { + id: 'action-requires-confirmation-elicited', + surface: + 'objects[].actions[].ai.requiresConfirmation and actions[].ai.requiresConfirmation set to `true` — ' + + 'what the declaration asks of a caller on the MCP `run_action` and `resume_run` tools', + replacement: + 'Read this against your runtime first. Until the release whose changelog says the AI doors confirm by ' + + 'elicitation, `run_action` and `resume_run` still run a declared-gated action on the caller\'s ' + + '`confirm: true`, and a call without it is refused with `ACTION_CONFIRMATION_REQUIRED` (428): keep sending ' + + 'it there. From that release, keep the declaration where a person must approve the call; what changes is ' + + 'who confirms. The server asks the person in the MCP client with an elicitation (`elicitation/create`, ' + + 'form mode) when the client advertised the `elicitation` capability at `initialize`; otherwise it hands ' + + 'the call to the host\'s pending-confirmation service, answers `status: \'pending_confirmation\'` with a ' + + 'pending id, and the person approves or rejects it in the app, where it runs as them with every ' + + 'permission re-checked. Only a host with no such service refuses, with `details.reason` ' + + '`no_confirmation_service`. From then on the caller\'s own `confirm: true` confirms nothing.', + reason: + '`confirm: true` was the caller\'s own claim that a person approved, and nothing could check it: an agent ' + + 'could send it for an irreversible action on reading the person\'s instruction as consent, and measured ' + + 'runs did exactly that. The gate made forgetting loud and did not prove a person. The server asking the ' + + 'person, in the client or in the app, with the caller unable to answer for them, is what makes the ' + + 'declaration mean what an author who wrote it meant. No mechanical move applies: the metadata is ' + + 'unchanged, and the work is on the client side and in the author\'s decision about each gated action. ' + + 'ADR-0011 / ADR-0151 / ADR-0087.', + acceptanceCriteria: + 'Now: for every action declaring `ai.requiresConfirmation: true`, decide again that a person must approve ' + + 'each AI call; if not, declare `false` knowingly. Once your runtime confirms by elicitation (its changelog ' + + 'says so; a refusal then carries `details.reason`): an MCP client that supports elicitation asks the ' + + 'person in place; any other caller gets a pending answer and the person decides in the app, provided the ' + + 'host runs a pending-confirmation service; a script or agent that ran such an action unattended on its ' + + 'own `confirm: true` no longer runs it, and a person does. Done when no caller relies on its own ' + + '`confirm: true` for a declared-gated action on a runtime that has switched.', +};