Skip to content

Commit 6739719

Browse files
fix(runtime)!: an AI service account does not confirm an action for itself (#32926)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DQAjJRsJBbEW4k4W1YDZ3rq)_ Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: follow-up #32936 ## 维护者速读 - **改了什么**:AI 服务账号(自己行事、不代表任何人的 agent 主体)不能替自己确认动作。声明了 `ai.requiresConfirmation: true` 的动作,对这类账号在所有动作入口(MCP `run_action`、`resume_run`、REST `/actions`、宿主 `dispatchAction` 的两种姿态)一律拒绝,无论请求里带什么;错误码、状态码与 details 不变(`428 ACTION_CONFIRMATION_REQUIRED`),提示语只陈述规则:由人来执行此动作。 - **为什么改**:ADR-0151 D3 第 3 条已裁定:需要人确认的动作,在交给人处理的挂起入口建成之前,对服务账号直接拒绝,且绝不由账号自己确认。 - **风险与回滚**:属破坏性变更(BREAKING,收窄)。受影响的只有:用 AI 服务账号的 Key 运行“需确认”动作的部署,升级后该调用被拒;处置是改由人执行,或由动作作者确认可无人值守后声明 `ai.requiresConfirmation: false`。人的调用、代人执行的 agent(带 `onBehalfOf`)、以及账号运行未声明确认的动作,全部不变。回滚即还原本 PR。 - **先看**:① `packages/runtime/src/action-execution.ts`(`actionConfirmationRefusal`:读取调用者、账号分支与提示语);② `packages/runtime/src/action-dispatch.ts`(准入序列:账号在两种姿态下都过确认闸);③ `packages/runtime/src/domains/mcp.ts`(`resume_run` 把调用者传给确认闸)。 - **两分钟验证**:`pnpm --filter @objectstack/runtime exec vitest run src/action-dispatch.test.ts -t "ADR-0151 D3 rule 3"`,预期 6 条全过:账号在 REST、MCP、宿主两种姿态下都得到 428 且什么都没执行、没读记录、没写调用记录;人和代人 agent 照旧。 - **需要你做的**:无,席位落地。 Supersedes #32691 (same change, carried forward onto current `main`; the merge conflict in a shared test harness is resolved keeping both sides). ## What changes The rule, from ADR-0151 D3 rule 3: **an AI service account does not confirm an action for itself.** An AI service account is an agent principal acting for itself (`principalKind: 'agent'` with no `onBehalfOf`). - **The gate reads the caller.** `actionConfirmationRefusal` (`packages/runtime/src/action-execution.ts`) takes the caller's envelope. For an account, an action declaring `ai.requiresConfirmation: true` is refused whatever the request carries; the confirmation member is not read for one. `isAiServiceAccountCaller` spells the account test as the flow-action door already does (`principalKind === 'agent'` and no `onBehalfOf`). A missing envelope and the system context are not an account. - **Every door.** The one admitted sequence (`action-dispatch.ts`) asks the gate for an account under both postures, so REST `/actions` and a host's `dispatchAction` on `rest` refuse it too; MCP `run_action` already runs the sequence under `ai`, and `resume_run` (`domains/mcp.ts`) passes the caller to the gate for every candidate action. The refusal stays the last pre-dispatch check: nothing runs, no record is read and no invocation row is written. - **Same envelope, a rule sentence.** The registered code, status and `details` are unchanged (`428` `ACTION_CONFIRMATION_REQUIRED`; no new error code). For an account the message states the rule — an AI service account does not confirm an action for itself: a person runs this action — and prescribes no retry. - **Unchanged for everyone else.** A person's call is gated exactly as before (at MCP `confirm: true` runs a gated action; REST `/actions` stays outside the gate for a person). A delegated agent (`onBehalfOf` present) is gated as before. An account runs every action that declares no confirmation, or `false`. - **Spec prose.** The `tool.requiresConfirmation` retirement prescription (`packages/spec/src/ai/tool.zod.ts`) states the account rule beside the REST `/actions` bound, and its pin (`tool-confirmation-prescription-tense.pin.test.ts`) is re-anchored: the gate reads the caller, the sequence asks it for an account under both postures, the REST carve-out holds for a person, and the prose carries the account rule. `check:generated` reports every spec artifact current; no artifact changed. No schema accepts or refuses anything new. - **The published contract.** `AIActionConfirmation` and `ActionConfirmationRequiredDetails` (`packages/spec/src/contracts/ai-service.ts`) state the account rule: the confirmation member is no longer described as the only predicate, and the retry is described as a caller acting for a person's; an AI service account is refused a declared-gated action whatever it sends. The gate docblock (`action-execution.ts`), the `ACTION_CONFIRMATION_REQUIRED` ledger comment (`error-code-ledger.zod.ts`) and the dispatcher's listing-predicate comment (`http-dispatcher.ts`) qualify their retry sentences the same way; the `tool.zod.ts` comment says the REST carve-out holds for every caller but an account. Comments and JSDoc only; no schema, export or runtime behaviour moves. - **Docs.** One rule line each in `content/docs/ai/actions-as-tools.mdx` and `content/docs/ai/connect-mcp.mdx`. - **Changeset.** `.changeset/agent-account-never-confirms-alone.md` (`@objectstack/runtime`, `@objectstack/spec`, patch): BREAKING, "Who moves", and the ADR-0087 `not-required` marker. ## Tests - `packages/runtime/src/action-confirmation-gate.test.ts`: the producer. An account is refused with the confirmation member and without it, with the registered code, status and `details`; its message states the rule and prescribes no retry (a person's still does, as a control); a person, a delegated agent, a missing envelope and the system context confirm as before; an account passes an action that declares no confirmation, or `false`. - `packages/runtime/src/action-dispatch.test.ts` (one new top-level `describe` in the shared harness): an account at REST `/actions`, MCP `run_action` and a host door under `rest`, `ai` and no posture, with the member and without it — `428` `ACTION_CONFIRMATION_REQUIRED`, nothing run, no record read, no invocation row. Controls: a person runs with the member at MCP and with or without it at REST (and the same call from a person does read the record, so the read counter is live); a delegated agent as before at both doors; an account runs an undeclared and a `false` action at REST, MCP and a host door. - `packages/runtime/src/mcp-resume-run.test.ts`: an account's resume of a gated action is refused `428` with the member and without it — nothing resumed, run still parked, no record read, no invocation row. Controls: an account resumes an ungated action; a delegated agent is refused without the member and completes with it. - `packages/qa/dogfood/test/service-account-register-key.dogfood.test.ts`: on the real showcase stack, the registered account's real key is refused `428` at REST `/actions` on a confirmation-gated platform action, with the member and without it; its owner, a person, runs the same action (`200`). - `packages/spec/src/ai/tool-confirmation-prescription-tense.pin.test.ts`: re-anchored as above, and it now reads a second carrier, the `AIActionConfirmation` / `ActionConfirmationRequiredDetails` docblocks, which must carry the account rule and may not say "when, and only when" or promise an always-confirming client success. Reverse-verified once: restoring "when, and only when" turned that case red (1 failed / 8 passed), restored from `HEAD`, `git diff HEAD` empty. - `packages/mcp/src/__tests__/stdio-agent-account.test.ts`: the case title now says what it asserts (the data bridge stdio registers carries no action seam). <details><summary>Ablation (one, on the principal check)</summary> `isAiServiceAccountCaller` mutated to answer `false`, through `scripts/ablation-replace.mjs` (anchor 1 → 0, blob changed); the three runtime suites ran 7 failed / 82 passed — every account refusal case red, every control green. Restored: blob equal to `HEAD`, `git diff HEAD` empty. </details> ## Acceptance notes - **stdio.** The stdio transport registers no action tools in this tree (its data bridge carries no action seam; `skills/objectstack-ai/SKILL.md` says the same), so the stdio case pins that absence rather than a refusal. - **Dogfood scope.** The showcase stack registers no `audit` service, so the dogfood case asserts the refusal and the person control at REST; "nothing run, no record read, no invocation row" is pinned in the runtime harnesses above. The showcase boot here carries no MCP plugin, so MCP and `resume_run` are pinned in the runtime harnesses. - **Skills docs follow-up.** `skills/objectstack-ai/SKILL.md` (the `run_action` / `resume_run` row) and `packages/mcp/src/skill-md.ts` (the `run_action` / `resume_run` entries) teach `confirm: true` as the retry for a gated action; they should say an AI service account is refused whatever it sends. Both are outside this PR's surface (`skills/**` is Tier H), filed as #32936 (blocked by this PR), which also carries `packages/mcp/src/mcp-http-tools.ts` (the HTTP MCP `run_action` description and the account's `list_actions` row). - `packages/runtime/src/http-dispatcher.ts`: its listing-predicate comment now qualifies the retry the same way (see What changes). ## Verification Gate union head: `8e2c6ee48a` (`6729ea847c` plus one comment/JSDoc-and-pin commit for the contract review's should-fix and nits; `main` not re-merged, no conflict reported). <details><summary>Derived gate families (dispatch-gates --commands, reconciled with --ran)</summary> - `node scripts/pm/dispatch-gates.mjs --repo objectstack-ai/objectstack --ran` on `8e2c6ee48a`: 116 derived (the five new families are read by the newly touched files: `check:authz-resolver`, `check:error-code-casing`, `check:init-service-contract`, `check:startup-registry-verdict`, `check:wildcard-fallthrough`), 116 run, every one recorded with its exit code, all exit 0; 0 NOT MEASURED, 0 unrun. - `pnpm check:dual-build-cjs-loads` and `pnpm check:type-check-debt` exit 0 on a full build of this head. - `pnpm --filter @objectstack/spec check:generated`: every artifact current; no reference page changed. </details> <details><summary>Tests and typecheck (on 8e2c6ee and 6729ea8)</summary> - On `8e2c6ee48a` (full `pnpm build` green): spec pin 9 of 9; `stdio-agent-account.test.ts` passed; `pnpm --filter` typecheck exit 0 for `@objectstack/spec`, `@objectstack/runtime`, `@objectstack/mcp`. - On `6729ea847c` (the delta since moves comments, JSDoc and one test title only): `@objectstack/runtime` test 588 files, 10265 passed, 19 skipped; `@objectstack/mcp` test 63 files, 1058 passed; `service-account-register-key.dogfood.test.ts` 10 of 10; full dogfood battery 456 files passed, 3 skipped, 3833 tests passed, 42 skipped, no failures. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4d1MskSgQZSjfWLUqNr)_ <!-- signed: security#1 · os-elon-musk · session_016WV4cx5nHfga3Hm9BzikGw --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent e728d7a commit 6739719

17 files changed

Lines changed: 516 additions & 60 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@objectstack/runtime': patch
3+
'@objectstack/spec': patch
4+
---
5+
6+
fix(runtime)!: an AI service account does not confirm an action for itself
7+
8+
Clause-②: no (narrowing)
9+
Metadata-protocol: none
10+
11+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no stored row changes shape. The rule is enforced at request time by the action doors' confirmation gate, so there is nothing for a metadata migration to rewrite and no tombstone. -->
12+
13+
**BREAKING** — an AI service account (an agent principal acting for itself: `principalKind: 'agent'` with no `onBehalfOf`) does not confirm an action for itself (ADR-0151 D3 rule 3). An action that declares `ai.requiresConfirmation: true` is refused for an account at every action door — MCP `run_action`, MCP `resume_run`, REST `/actions`, and a host's `dispatchAction` under either posture — whatever the request carries, with the registered `428` `ACTION_CONFIRMATION_REQUIRED` and its `details` unchanged. The refusal's message states the rule (a person runs this action) and prescribes no retry.
14+
15+
**Who moves.** A deployment that runs a confirmation-gated action from an AI service account's key. That call is now refused; have a person run the action with their own credentials, or, if the action is safe to run unattended, have its author declare `ai.requiresConfirmation: false` (or remove the key). Nothing else moves:
16+
17+
- A person's call is unchanged at every door: at MCP the confirmation member `confirm: true` still runs a gated action, and REST `/actions` still sits outside the confirmation gate for a person.
18+
- A delegated agent (an OAuth agent acting for a person, `onBehalfOf` present) is gated exactly as before.
19+
- An account still runs every action that declares no confirmation, or declares `false`.
20+
21+
`@objectstack/spec`: the `tool.requiresConfirmation` retirement prescription states the account rule beside the REST `/actions` bound. No schema accepts or refuses anything new.

‎content/docs/ai/actions-as-tools.mdx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ through the API. Destructive actions (`confirmText`, `mode: 'delete'`,
126126
`requiresConfirmation: true` so the client can ask the human before calling. To
127127
assert that a destructive-looking action is safe for autonomous execution, set
128128
`ai.requiresConfirmation: false`.
129+
An AI service account does not confirm an action for itself: an action declaring
130+
`ai.requiresConfirmation: true` refuses it at every door, REST `/actions` included,
131+
whatever the request carries, and a person runs that action.
129132

130133
## Wiring it up
131134

‎content/docs/ai/connect-mcp.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ drive this MCP server. Same word, different layer.
488488
an action as a code-review-worthy act — that's the real security boundary.
489489
- An action can also declare `ai.requiresConfirmation`; destructive-looking
490490
actions (`confirmText`, danger variants) default to requiring it.
491+
- **An AI service account does not confirm an action for itself.** An action
492+
declaring `ai.requiresConfirmation: true` refuses an AI service account's key
493+
(`428` `ACTION_CONFIRMATION_REQUIRED`) at `run_action`, `resume_run` and REST
494+
`/actions`, whatever the call carries; a person runs that action.
491495
- **`delete_record` needs `confirm: true` on every call.** A record delete over
492496
MCP is permanent, so the tool refuses a call without it with `run_action`'s
493497
refusal (`428` `ACTION_CONFIRMATION_REQUIRED`, its `details.actionName`

‎packages/mcp/src/__tests__/stdio-agent-account.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ describe('[ADR-0151 D3] stdio: an AI service account\'s key admits the account i
142142
expect(context.isSystem).toBe(false);
143143
});
144144

145+
it('[ADR-0151 D3 rule 3] the data bridge stdio registers carries no action seam (listActions, runAction, resumeRun)', async () => {
146+
// The account rule lives at the action doors (`run_action`, `resume_run`,
147+
// REST `/actions`, a host door), pinned in `@objectstack/runtime`. Over
148+
// stdio the account's tools are the bridge registered here; this holds
149+
// that it carries no action seam, so an action door that ever joins stdio
150+
// fails here first and is asked to carry the rule.
151+
process.env.OS_MCP_STDIO_API_KEY = BOT_KEY;
152+
const { ctx } = createHarness();
153+
let bridge: Record<string, unknown> | undefined;
154+
const bridgeDataTools = vi
155+
.spyOn(MCPServerRuntime.prototype, 'bridgeDataTools')
156+
.mockImplementation((b: unknown) => { bridge = b as Record<string, unknown>; return []; });
157+
try {
158+
await startStdio(ctx);
159+
} finally {
160+
bridgeDataTools.mockRestore();
161+
}
162+
expect(bridge, 'stdio registered no data bridge').toBeDefined();
163+
expect(typeof bridge!.listObjects).toBe('function');
164+
for (const seam of ['listActions', 'runAction', 'resumeRun']) {
165+
expect(typeof bridge![seam], `stdio now carries \`${seam}\``).toBe('undefined');
166+
}
167+
});
168+
145169
it('CONTROL: a person\'s key through the same face is a human principal, with the everyone anchor', async () => {
146170
const context = await contextFor(MIA_KEY);
147171
expect(context.userId).toBe('u_mia');

‎packages/qa/dogfood/test/approval-decide-over-mcp.dogfood.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ describe('an approver\'s agent decides a pending approval request over MCP (#324
243243
const requestId = await openRequest('account');
244244
const answer = await approve({ key: accountKey }, requestId, true);
245245
expect(answer.isError, answer.text).toBe(true);
246-
expect(answer.payload?.error?.code, answer.text).toBe('REQUEST_NOT_FOUND');
246+
// The account confirmation rule answers first (ADR-0151 D3 rule 3): an AI
247+
// service account does not confirm an action for itself, so the decision
248+
// action, which declares `ai.requiresConfirmation`, is refused at admission
249+
// whatever the call carries, before the request is looked up.
250+
expect(answer.payload?.error?.code, answer.text).toBe('ACTION_CONFIRMATION_REQUIRED');
251+
expect(answer.payload?.error?.status, answer.text).toBe(428);
247252
await expectUndecided(requestId, 'an AI service account');
248253
});
249254

‎packages/qa/dogfood/test/service-account-register-key.dogfood.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
* its next call exactly as an unknown key is refused, and lifting the ban
1919
* lets it through again. The access matrix and the one-act write are pinned
2020
* on a real engine in plugin-auth `service-account-doors.test.ts`.
21+
*
22+
* [ADR-0151 D3 rule 3] The key, at the REST action door: an AI service account
23+
* does not confirm an action for itself, so an action declaring
24+
* `ai.requiresConfirmation: true` refuses it whatever the request carries,
25+
* while its owner, a person, runs the same action. The other doors, and that a
26+
* refusal runs nothing, reads no record and writes no invocation row, are
27+
* pinned in `@objectstack/runtime` (`action-dispatch.test.ts`,
28+
* `mcp-resume-run.test.ts`).
2129
*/
2230

2331
import { randomBytes } from 'node:crypto';
@@ -173,3 +181,26 @@ describe('[ADR-0151 D4] minting the account\'s key', () => {
173181
expect((await answerOf(await readAs(accountKey))).status).not.toBe(401);
174182
});
175183
});
184+
185+
describe('[ADR-0151 D3 rule 3] the account does not confirm an action for itself', () => {
186+
/** A platform identity action its author gated on a person's confirmation. */
187+
const GATED = '/actions/sys_session/revoke_my_other_sessions';
188+
189+
it('the account\'s key is refused 428 at REST /actions with the confirmation member and without it', async () => {
190+
expect(accountKey).toBeTruthy();
191+
for (const body of [{}, { confirm: true }]) {
192+
const res = await postWithKey(GATED, accountKey, body);
193+
const text = await res.text();
194+
let error: any;
195+
try { error = JSON.parse(text)?.error; } catch { error = undefined; }
196+
expect({ status: res.status, code: error?.code }, text).toEqual({ status: 428, code: 'ACTION_CONFIRMATION_REQUIRED' });
197+
expect(String(error?.message)).toContain('An AI service account does not confirm an action for itself');
198+
expect(String(error?.message)).not.toMatch(/retry/i);
199+
}
200+
});
201+
202+
it('CONTROL: the account\'s owner, a person, runs the same action at REST', async () => {
203+
const res = await stack.apiAs(ownerSession, 'POST', GATED, {});
204+
expect(res.status, await res.clone().text()).toBe(200);
205+
});
206+
});

‎packages/runtime/src/action-confirmation-gate.test.ts‎

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
// cause is refusing calls no author ever asked to gate — which is what the
2929
// heuristic cases below exist to catch.
3030

31+
// Paid at collection, not inside a case's clock (check:test-source-alias).
32+
import '@objectstack/metadata';
3133
import { describe, it, expect, vi } from 'vitest';
3234

3335
import { AI_ACTION_CONFIRMATION_MEMBER } from '@objectstack/spec/contracts';
@@ -37,12 +39,20 @@ import {
3739
invokeBusinessAction,
3840
actionConfirmationRefusal,
3941
actionLooksDestructive,
42+
isAiServiceAccountCaller,
4043
ACTION_CONFIRMATION_REQUIRED_CODE,
4144
ACTION_CONFIRMATION_REQUIRED_STATUS,
4245
} from './action-execution.js';
4346

4447
const OBJECT = 'crm_lead';
4548

49+
/** A person's envelope. */
50+
const PERSON_EC = { userId: 'u_person' };
51+
/** [ADR-0151 D3] An AI service account: an agent principal acting for itself. */
52+
const ACCOUNT_EC = { userId: 'u_bot', principalKind: 'agent' };
53+
/** An OAuth-delegated agent: it acts for the person it names. */
54+
const DELEGATED_EC = { userId: 'u_person', principalKind: 'agent', onBehalfOf: { userId: 'u_person', principalKind: 'human' } };
55+
4656
/** Author-gated: `ai.requiresConfirmation: true`, declared on purpose. */
4757
const GATED = {
4858
name: 'archive_lead', label: 'Archive Lead', objectName: OBJECT, type: 'script',
@@ -207,17 +217,17 @@ describe('the shared refusal producer itself (#15942)', () => {
207217
const deps: any = {};
208218

209219
it('is silent unless the author declared the flag as `true`', () => {
210-
expect(actionConfirmationRefusal(deps, LOOKS_DESTRUCTIVE, {}, OBJECT)).toBeUndefined();
211-
expect(actionConfirmationRefusal(deps, DECLARED_SAFE, {}, OBJECT)).toBeUndefined();
212-
expect(actionConfirmationRefusal(deps, {}, {}, OBJECT)).toBeUndefined();
220+
expect(actionConfirmationRefusal(deps, LOOKS_DESTRUCTIVE, {}, OBJECT, PERSON_EC)).toBeUndefined();
221+
expect(actionConfirmationRefusal(deps, DECLARED_SAFE, {}, OBJECT, PERSON_EC)).toBeUndefined();
222+
expect(actionConfirmationRefusal(deps, {}, {}, OBJECT, PERSON_EC)).toBeUndefined();
213223
// Lit control on the same call: the one shape that DOES refuse.
214-
expect(actionConfirmationRefusal(deps, GATED, {}, OBJECT)?.code).toBe(
224+
expect(actionConfirmationRefusal(deps, GATED, {}, OBJECT, PERSON_EC)?.code).toBe(
215225
ACTION_CONFIRMATION_REQUIRED_CODE,
216226
);
217227
});
218228

219229
it('echoes the member off the CONTRACT constant, not a hand-spelled string', () => {
220-
const refusal = actionConfirmationRefusal(deps, GATED, undefined, OBJECT);
230+
const refusal = actionConfirmationRefusal(deps, GATED, undefined, OBJECT, PERSON_EC);
221231

222232
expect(refusal).toMatchObject({
223233
code: ACTION_CONFIRMATION_REQUIRED_CODE,
@@ -234,7 +244,7 @@ describe('the shared refusal producer itself (#15942)', () => {
234244
});
235245

236246
it('omits `objectName` for an object-less action rather than emitting an empty one', () => {
237-
const refusal = actionConfirmationRefusal(deps, GATED, {}, undefined);
247+
const refusal = actionConfirmationRefusal(deps, GATED, {}, undefined, PERSON_EC);
238248

239249
expect(refusal?.details).toEqual({
240250
actionName: GATED.name,
@@ -243,3 +253,60 @@ describe('the shared refusal producer itself (#15942)', () => {
243253
expect('objectName' in (refusal?.details ?? {})).toBe(false);
244254
});
245255
});
256+
257+
describe('[ADR-0151 D3 rule 3] an AI service account does not confirm an action for itself', () => {
258+
const deps: any = {};
259+
const CONFIRMED = { [AI_ACTION_CONFIRMATION_MEMBER]: true };
260+
261+
it('refuses an account on a gated action with the confirmation member and without it, with the registered code, status and details', () => {
262+
for (const request of [CONFIRMED, {}, undefined]) {
263+
const refusal = actionConfirmationRefusal(deps, GATED, request, OBJECT, ACCOUNT_EC);
264+
expect(refusal, `request ${JSON.stringify(request)}`).toMatchObject({
265+
code: ACTION_CONFIRMATION_REQUIRED_CODE,
266+
status: ACTION_CONFIRMATION_REQUIRED_STATUS,
267+
});
268+
expect(refusal?.code).toBe('ACTION_CONFIRMATION_REQUIRED');
269+
expect(refusal?.status).toBe(428);
270+
expect(refusal?.details).toEqual({
271+
actionName: GATED.name,
272+
objectName: OBJECT,
273+
confirmationMember: AI_ACTION_CONFIRMATION_MEMBER,
274+
});
275+
}
276+
});
277+
278+
it('the account refusal states the rule and prescribes no retry', () => {
279+
const refusal = actionConfirmationRefusal(deps, GATED, CONFIRMED, OBJECT, ACCOUNT_EC);
280+
const message = String(refusal?.message);
281+
expect(message).toContain(GATED.name);
282+
expect(message).toContain('An AI service account does not confirm an action for itself');
283+
expect(message).not.toMatch(/retry/i);
284+
expect(message).not.toContain(`'${AI_ACTION_CONFIRMATION_MEMBER}': true`);
285+
// CONTROL: a person's refusal still prescribes the retry, so the
286+
// assertions above read the account's sentence and not a reworded one.
287+
const person = String(actionConfirmationRefusal(deps, GATED, {}, OBJECT, PERSON_EC)?.message);
288+
expect(person).toMatch(/retry/i);
289+
expect(person).toContain(`'${AI_ACTION_CONFIRMATION_MEMBER}': true`);
290+
});
291+
292+
it('CONTROL: a person and a delegated agent confirm as before, and a missing or system envelope is not an account', () => {
293+
for (const caller of [PERSON_EC, DELEGATED_EC, { userId: 'u_x', principalKind: 'human' }, undefined, null, { isSystem: true }]) {
294+
const label = JSON.stringify(caller);
295+
expect(actionConfirmationRefusal(deps, GATED, CONFIRMED, OBJECT, caller), label).toBeUndefined();
296+
expect(actionConfirmationRefusal(deps, GATED, {}, OBJECT, caller)?.message, label).toMatch(/retry/i);
297+
}
298+
});
299+
300+
it('CONTROL: an account runs an action that declares no confirmation, or `false`', () => {
301+
for (const action of [LOOKS_DESTRUCTIVE, DECLARED_SAFE, {}]) {
302+
expect(actionConfirmationRefusal(deps, action, {}, OBJECT, ACCOUNT_EC)).toBeUndefined();
303+
}
304+
});
305+
306+
it('reads an account as `principalKind: agent` with no `onBehalfOf`, and nothing else as one', () => {
307+
expect(isAiServiceAccountCaller(ACCOUNT_EC)).toBe(true);
308+
for (const caller of [PERSON_EC, DELEGATED_EC, { principalKind: 'service' }, undefined, null, { isSystem: true }]) {
309+
expect(isAiServiceAccountCaller(caller), JSON.stringify(caller)).toBe(false);
310+
}
311+
});
312+
});

0 commit comments

Comments
 (0)