Skip to content

Commit 0f655ef

Browse files
committed
fix(plugin-audit): write conversation_id / confirmation_id only beside the in-product agent, and state how a reader tells the two performed_by kinds apart
Claude-Session: https://claude.ai/code/session_01ADHkmSQnLEs6vGofJXcguE Co-authored-by: Claude <noreply@anthropic.com>
1 parent 768136c commit 0f655ef

4 files changed

Lines changed: 66 additions & 21 deletions

File tree

‎packages/plugins/plugin-audit/src/agent-dual-attribution.integration.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ describe('[#33694] an in-product AI write names the agent, the conversation and
449449
const CONFIRMED_RECORD = 'c_ai_confirmed';
450450
const UNCONFIRMED_RECORD = 'c_ai_unconfirmed';
451451
const PERSON_RECORD = 'c_ai_person_control';
452+
const CLIENT_MARKS_RECORD = 'c_ai_client_with_marks';
453+
const BOTH_PERFORMERS_RECORD = 'c_ai_both_performers';
452454

453455
let kernel: ObjectKernel;
454456
let driver: SqliteWasmDriver;
@@ -497,6 +499,19 @@ describe('[#33694] an in-product AI write names the agent, the conversation and
497499
{ id: PERSON_RECORD, full_name: 'Typed by the person' },
498500
{ context: personCtx as any },
499501
);
502+
// No producer in the tree builds these two envelopes (each door sets only
503+
// its own performer key, and the OAuth door no conversation or
504+
// confirmation); they pin what the spec tells a reader of `performed_by`.
505+
await engine.insert(
506+
AUDITED_OBJECT,
507+
{ id: CLIENT_MARKS_RECORD, full_name: 'Client beside stray marks' },
508+
{ context: aiCtx({ clientId: CLIENT, conversationId: CONVERSATION, confirmationId: CONFIRMATION }) as any },
509+
);
510+
await engine.insert(
511+
AUDITED_OBJECT,
512+
{ id: BOTH_PERFORMERS_RECORD, full_name: 'Both performers' },
513+
{ context: aiCtx({ clientId: CLIENT, agentId: AGENT, conversationId: CONVERSATION, confirmationId: CONFIRMATION }) as any },
514+
);
500515
await engine.update(
501516
AUDITED_OBJECT,
502517
{ full_name: 'Renamed by the AI' },
@@ -551,6 +566,16 @@ describe('[#33694] an in-product AI write names the agent, the conversation and
551566
});
552567
});
553568

569+
it('conversation_id / confirmation_id never ride beside an OAuth client id, so a client row carries only the pair', async () => {
570+
const row = await ledgerRow(CLIENT_MARKS_RECORD, 'create');
571+
expect(JSON.parse(String(row.metadata))).toEqual({ performed_by: CLIENT, on_behalf_of: HUMAN });
572+
});
573+
574+
it('an envelope naming both performers is recorded as the OAuth client, without the agent\'s marks', async () => {
575+
const row = await ledgerRow(BOTH_PERFORMERS_RECORD, 'create');
576+
expect(JSON.parse(String(row.metadata))).toEqual({ performed_by: CLIENT, on_behalf_of: HUMAN });
577+
});
578+
554579
it('an AI update of a record the person created is marked on the update row, and the person\'s own create row stays unmarked', async () => {
555580
const created = await ledgerRow(PERSON_RECORD, 'create');
556581
expect(created.metadata ?? null).toBeNull();

‎packages/plugins/plugin-audit/src/audit-writers.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,17 +2210,24 @@ export function installAuditWriters(
22102210
// names the agent it ran, the conversation it ran in and, when the person
22112211
// confirmed the call, the confirmation. The ruling reuses the existing
22122212
// `performed_by` / `on_behalf_of` pair and adds no actor vocabulary, so the
2213-
// agent lands in `performed_by` exactly where an OAuth client does; the two
2214-
// ids are stamped together with the client. The /mcp OAuth door and the
2215-
// in-product path each set only their own key (`clientId` vs `agentId`),
2216-
// so one envelope never carries both; if one ever did, the OAuth client
2217-
// keeps the column it has always held.
2213+
// agent lands in `performed_by` exactly where an OAuth client does. The
2214+
// /mcp OAuth door and the in-product path each set only their own key
2215+
// (`clientId` vs `agentId`); if one envelope ever carried both, the OAuth
2216+
// client keeps the column it has always held.
2217+
//
2218+
// The conversation and the confirmation are the in-product path's own
2219+
// marks, so they are written ONLY when the in-product agent is the
2220+
// performer this row records — never beside an OAuth client id. That is
2221+
// what the spec promises a reader of `performed_by`, which holds either
2222+
// kind: `conversation_id` / `confirmation_id` come from the in-product
2223+
// path alone.
22182224
const performedByAgentId: string | undefined = provenanceMark(ctx, 'performedByAgentId');
22192225
const performedBy: string | undefined = performedByClientId ?? performedByAgentId;
2220-
const performedInConversationId: string | undefined = performedBy
2226+
const agentIsRecordedPerformer: boolean = !performedByClientId && performedByAgentId !== undefined;
2227+
const performedInConversationId: string | undefined = agentIsRecordedPerformer
22212228
? provenanceMark(ctx, 'performedByConversationId')
22222229
: undefined;
2223-
const performedUnderConfirmationId: string | undefined = performedBy
2230+
const performedUnderConfirmationId: string | undefined = agentIsRecordedPerformer
22242231
? provenanceMark(ctx, 'performedByConfirmationId')
22252232
: undefined;
22262233
// The ADMINISTRATOR who made this write from an impersonation session. A
@@ -2373,10 +2380,10 @@ export function installAuditWriters(
23732380
// connection acted.
23742381
//
23752382
// `conversation_id` / `confirmation_id` (#33694) ride ONLY beside a
2376-
// `performed_by`: the conversation the in-product AI performed the write
2377-
// in, and the pending item the person's approval decided for it. Each is
2378-
// present only when the producer stamped it — no confirmation key ⇒ the
2379-
// write needed none.
2383+
// `performed_by` naming the in-product agent: the conversation it
2384+
// performed the write in, and the pending item the person's approval
2385+
// decided for it. Each is present only when the producer stamped it — no
2386+
// confirmation key ⇒ the write needed none.
23802387
if (performedBy || impersonatedBy || signInStamp || scimConnectionId) {
23812388
auditRow.metadata = safeStringify({
23822389
// A sign-in stamp names its event the way a `config_change` row does

‎packages/spec/src/data/hook.zod.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,9 +1682,12 @@ export const HookContextSchema = lazySchema(() => z.object({
16821682
* (征询版)」, carried by #33694 and #33786); each key is copied from its
16831683
* `ExecutionContext.performedBy` sibling, as `performedByClientId` is from
16841684
* `clientId`. The record-write audit writer records the agent as
1685-
* `performed_by` beside `on_behalf_of`, exactly where it records
1686-
* `performedByClientId`, with the conversation as `conversation_id` and
1687-
* the confirmation as `confirmation_id` (#33694);
1685+
* `performed_by` beside `on_behalf_of`, the same key it records
1686+
* `performedByClientId` in, so a reader tells the two by resolving the
1687+
* value (a registered `sys_oauth_application` `client_id` is the OAuth
1688+
* client, any other value the in-product agent); only beside the agent
1689+
* does it also record the conversation as `conversation_id` and the
1690+
* confirmation as `confirmation_id` (#33694);
16881691
* - `impersonatedBy` — WHICH ADMINISTRATOR made a write from an
16891692
* impersonation session, authorized as the impersonated user in
16901693
* `session.userId`.
@@ -1702,9 +1705,9 @@ export const HookContextSchema = lazySchema(() => z.object({
17021705
flowRunId: z.string().optional().describe('Id of the automation flow run performing this write, when it originates from a flow data node. Lets a hook recognize the run that OWNS state that run itself opened — the approvals record lock exempts the run holding the pending request.'),
17031706
attributedUserId: z.string().optional().describe('The real human credited for a write whose authorization subject was the SYSTEM — e.g. the admin whose better-auth `update-member-role` call the identity adapter executes as `isSystem`. ATTRIBUTION ONLY: the audit writer records it as `sys_audit_log.user_id`; no security middleware reads it, and it never becomes the subject the write is authorized as.'),
17041707
performedByClientId: z.string().optional().describe('[ADR-0090 D10 rule 4] The AGENT that performed this write — the OAuth client (`azp`, a registered sys_oauth_application id) the access token was issued to, when an AI client acted on behalf of the human in `session.userId`. Copied from `ExecutionContext.performedBy.clientId`, which only the /mcp OAuth door sets. ATTRIBUTION ONLY, exactly like `attributedUserId`: the audit writer records it in `sys_audit_log.metadata` as `performed_by` beside `on_behalf_of`; no security middleware reads it and it never becomes the subject the write is authorized as. Absent on every non-delegated write, so a hook tells the two apart by the key being there rather than by guesswork.'),
1705-
performedByAgentId: z.string().optional().describe('The in-product AI agent that performed this write for the signed-in person in `session.userId`, whose write it runs as: the agent the chat runs as. Copied from `ExecutionContext.performedBy.agentId`, which only the server-constructed in-product AI path sets, after resolving it as the agent it actually ran. ATTRIBUTION ONLY, exactly like `performedByClientId`: the record-write audit writer records it in `sys_audit_log.metadata` as `performed_by` beside `on_behalf_of`; no security middleware reads it and it never becomes the subject the write is authorized as. Absent on every write the person made themselves.'),
1706-
performedByConversationId: z.string().optional().describe('The AI conversation an in-product AI write was performed in. Copied from `ExecutionContext.performedBy.conversationId`, set beside `performedByAgentId`. ATTRIBUTION ONLY: the record-write audit writer records it as `sys_audit_log.metadata.conversation_id` beside `performed_by`; no security middleware reads it.'),
1707-
performedByConfirmationId: z.string().optional().describe('The confirmation the person gave before an AI-performed write ran: the id of the pending item their approval decided. Copied from `ExecutionContext.performedBy.confirmationId`; absent when the write needed no confirmation. ATTRIBUTION ONLY: the record-write audit writer records it as `sys_audit_log.metadata.confirmation_id` beside `performed_by`; no security middleware reads it, and it is never the authorization for the write, which runs as the person with every gate re-checked.'),
1708+
performedByAgentId: z.string().optional().describe('The in-product AI agent that performed this write for the signed-in person in `session.userId`, whose write it runs as: the agent the chat runs as. Copied from `ExecutionContext.performedBy.agentId`, which only the server-constructed in-product AI path sets, after resolving it as the agent it actually ran. ATTRIBUTION ONLY, exactly like `performedByClientId`: the record-write audit writer records it in `sys_audit_log.metadata` as `performed_by` beside `on_behalf_of`, the key an OAuth client id is also recorded in: a value matching the `client_id` of a registered `sys_oauth_application` row is the OAuth client, any other value is an in-product agent name (when `performedByClientId` is also present the row records the client instead); no security middleware reads it and it never becomes the subject the write is authorized as. Absent on every write the person made themselves.'),
1709+
performedByConversationId: z.string().optional().describe('The AI conversation an in-product AI write was performed in. Copied from `ExecutionContext.performedBy.conversationId`, set beside `performedByAgentId`. ATTRIBUTION ONLY: the record-write audit writer records it as `sys_audit_log.metadata.conversation_id`, only when `performed_by` names the in-product agent; no security middleware reads it.'),
1710+
performedByConfirmationId: z.string().optional().describe('The confirmation the person gave before an AI-performed write ran: the id of the pending item their approval decided. Copied from `ExecutionContext.performedBy.confirmationId`; absent when the write needed no confirmation. ATTRIBUTION ONLY: the record-write audit writer records it as `sys_audit_log.metadata.confirmation_id`, only when `performed_by` names the in-product agent; no security middleware reads it, and it is never the authorization for the write, which runs as the person with every gate re-checked.'),
17081711
impersonatedBy: z.string().optional().describe('The ADMINISTRATOR (a sys_user id) who made this write from an impersonation session, while `session.userId` is the impersonated user the write is authorized as. Copied from `ExecutionContext.impersonatedBy`, which the shared request-identity resolution sets from the session read. ATTRIBUTION ONLY, exactly like `performedByClientId`: the audit writer records it as `sys_audit_log.metadata.impersonated_by` and on the sys_activity row; no security middleware reads it and it never becomes the subject the write is authorized as. Absent on every write the user made themselves.'),
17091712
}).optional().describe('Server-stamped write provenance (never client-supplied, never an authorization input)'),
17101713

‎packages/spec/src/kernel/execution-context.zod.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ export const ExecutionContextSchema = lazySchema(() => z.object({
305305
* when the performer is not the principal the write is authorized as.
306306
*
307307
* Two server-side producers set it, each naming its performer in its own
308-
* key so one key never holds two id namespaces:
308+
* key so no key of this envelope holds two id namespaces (the audit row's
309+
* `performed_by` does hold both, by ruling; how its reader tells them apart
310+
* is stated below):
309311
*
310312
* - **The `/mcp` OAuth door** — the ONE door that resolves an agent, whose
311313
* access token names an authorized client (`azp`) — sets {@link clientId}
@@ -365,8 +367,16 @@ export const ExecutionContextSchema = lazySchema(() => z.object({
365367
* performer in `sys_audit_log.metadata` as `performed_by` beside
366368
* `on_behalf_of` — {@link clientId}, or {@link agentId} for the in-product AI
367369
* (owner ruling on objectstack-ai/cloud#3826: the existing pair, no new actor
368-
* vocabulary) — with {@link conversationId} as `conversation_id` and
369-
* {@link confirmationId} as `confirmation_id` beside them when set. The
370+
* vocabulary). So that one key holds two id kinds, and its reader tells them
371+
* apart by resolving the value: one that matches the `client_id` of a
372+
* registered `sys_oauth_application` row is the OAuth client; any other
373+
* value is an in-product agent name. Only when the in-product agent is the
374+
* recorded performer does the row also carry {@link conversationId} as
375+
* `conversation_id` and {@link confirmationId} as `confirmation_id` (each
376+
* when set); a row naming an OAuth client never carries either, but their
377+
* absence alone does not mark a row as the client's, since both are
378+
* optional. If one envelope ever carried both performers, the row records
379+
* {@link clientId} and drops the agent with its two marks. The
370380
* action-invocation, data-export and dataset-query rows record
371381
* {@link clientId} only. ⛔ It
372382
* does NOT move `actor`: ADR-0118 D1/D5 keeps that column two-valued (a user
@@ -413,7 +423,7 @@ export const ExecutionContextSchema = lazySchema(() => z.object({
413423
confirmationId: z.string().optional().describe('The confirmation the person gave before the operation ran: the id of the pending item their approval decided (the in-app pending action id, or the pending-confirmation service item id). Stamped only after the server resolved it as a pending item the person in userId decided for this operation; absent when the operation needed no confirmation.'),
414424
}).refine(requiredOneOf(['clientId', 'agentId']), {
415425
message: 'performedBy names its performer: clientId (the /mcp OAuth door) or agentId (the in-product AI path)',
416-
}).optional().describe('ADR-0090 D10 rule 4 dual attribution: what PERFORMED this operation, when the performer is not the principal the write is authorized as. Two server-side producers set it: the /mcp OAuth door names the agent client in clientId, on the same branch that decides principalKind: agent and onBehalfOf; the server-constructed in-product AI path, acting for the signed-in person and running as them, names its agent in agentId with conversationId and, when the person confirmed the call, confirmationId. Absent everywhere else, and the absence is the record that the principal acted for itself; when present it names its performer, at least one of clientId and agentId, each set only by its own producer. ATTRIBUTION ONLY — no security middleware reads it, it never becomes the authorization subject, and userId stays the human so owner-stamping and current_user.* RLS still resolve to them. Server-constructed only, never client-supplied: an id a request named (an agent name, a conversation, a pending item) is stamped only after the server resolved it in its own store for the person in userId, and an unresolved id is not stamped. Surfaced to hooks as HookContext.provenance.performedByClientId / performedByAgentId / performedByConversationId / performedByConfirmationId. The record-write audit writer records the performer (clientId, or agentId for the in-product AI) as sys_audit_log.metadata.performed_by beside on_behalf_of, with conversationId as metadata.conversation_id and confirmationId as metadata.confirmation_id when set; the action-invocation, data-export and dataset-query rows record clientId only. It does not move `actor`, which ADR-0118 D1/D5 keeps two-valued.'),
426+
}).optional().describe('ADR-0090 D10 rule 4 dual attribution: what PERFORMED this operation, when the performer is not the principal the write is authorized as. Two server-side producers set it: the /mcp OAuth door names the agent client in clientId, on the same branch that decides principalKind: agent and onBehalfOf; the server-constructed in-product AI path, acting for the signed-in person and running as them, names its agent in agentId with conversationId and, when the person confirmed the call, confirmationId. Absent everywhere else, and the absence is the record that the principal acted for itself; when present it names its performer, at least one of clientId and agentId, each set only by its own producer. ATTRIBUTION ONLY — no security middleware reads it, it never becomes the authorization subject, and userId stays the human so owner-stamping and current_user.* RLS still resolve to them. Server-constructed only, never client-supplied: an id a request named (an agent name, a conversation, a pending item) is stamped only after the server resolved it in its own store for the person in userId, and an unresolved id is not stamped. Surfaced to hooks as HookContext.provenance.performedByClientId / performedByAgentId / performedByConversationId / performedByConfirmationId. The record-write audit writer records the performer (clientId, or agentId for the in-product AI) as sys_audit_log.metadata.performed_by beside on_behalf_of, so that key holds two id kinds: a value matching the client_id of a registered sys_oauth_application row is the OAuth client, any other value is an in-product agent name. Only when the agent is the recorded performer does the row also carry conversationId as metadata.conversation_id and confirmationId as metadata.confirmation_id (each when set); a client row never carries them, but both are optional, so their absence does not mark a client row. An envelope carrying both performers is recorded as the client. The action-invocation, data-export and dataset-query rows record clientId only. It does not move `actor`, which ADR-0118 D1/D5 keeps two-valued.'),
417427

418428
/**
419429
* [#27908 — ruling 6513869839 on #27681, parameter 3] WHICH DOOR an action

0 commit comments

Comments
 (0)