Skip to content

Commit 3b01ea7

Browse files
authored
fix(audit): show authenticated users for Gateway runs (#122484)
* fix(audit): show authenticated users for Gateway runs * fix(audit): keep profile labels out of sessions * test(qa): verify session label retention at storage * fix(sessions): preserve canonical profile ownership * test(qa): require full identity inspection proof * docs(agents): preserve execution identity ownership boundary
1 parent 6d973d1 commit 3b01ea7

15 files changed

Lines changed: 1080 additions & 40 deletions

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Skills own workflows; root owns hard policy and routing. Product direction and m
170170
## Execution Identity Audit
171171

172172
- Execution identity is opt-in diagnostic provenance, never authorization or enforcement. Unknown facts stay unknown; record ingress or invoker facts only at their authoritative producer. Never infer identity from session keys, `runId`, or routing metadata.
173+
- Frozen ingress identity facts are diagnostic audit input, not session-ownership state. Session provenance must use the current canonical authenticated profile ID and never retain a profile display label; only explicitly enabled execution-identity audit storage may retain its bounded, redacted form.
173174
- Invoker evidence is tri-state: tagged principal-bearing input is `present`, tagged principal-less input is `unknown`, and omission alone is `absent`. Validate the closed raw variant before projection or field dropping; reject malformed, mixed, untagged, or extra-field input instead of normalizing it to `unknown` or absence.
174175
- Each outer admitted turn owns one immutable `executionId` and `contextId`; `runId` is non-unique correlation. Retries, fallbacks, and recovery reuse the original admission identity. Only byte-identical canonical replay is idempotent.
175176
- Admission may only validate, bound, freeze, and enqueue through the shared audit writer. Admission validates only a recursively owned, enumerable, accessor-free data snapshot constructed from descriptors before schema checks or ordinary property reads; inherited properties are absent and accessors never run. No synchronous SQLite, schema, filesystem, HMAC-key, or readiness work. Audit failure never delays or aborts execution.

‎docs/cli/audit.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ ingress, an absent invoker, and
124124
`unattributed` coverage. Its admission receipt says `not-applicable` because no
125125
identity-aware policy or grant evaluation was proven.
126126

127+
For Gateway runs, a resolved authenticated profile can make the invoker
128+
`present` and coverage `attribution-only`. Paired devices and shared credentials
129+
do not establish a person: without a durable profile the invoker stays absent,
130+
or `unknown` when authenticated user evidence promised a profile that could not
131+
be resolved. Session creation retains the live canonical durable profile id so
132+
profile linking does not orphan ownership, while run inspection consumes the
133+
immutable connection-time audit fact. Ordinary session provenance stores no
134+
display label. An optional bounded, secret-redacted label can be retained only
135+
in execution identity after that audit storage is explicitly enabled.
136+
127137
JSON output is the Gateway result without lossy reformatting. An exact result contains one
128138
bounded V1 context (maximum 16 KiB), up to 100 decision receipts, coverage and
129139
missing-evidence codes, and an optional `nextDecisionCursor`. An ambiguous run

‎docs/gateway/audit.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ this boundary. A run becomes
9191
`attribution-only` only when an authoritative ingress supplies an invoker fact.
9292
Neither state means that identity affected an allow or deny decision.
9393

94+
Authenticated Gateway attach records immutable audit facts once. Session
95+
creation separately reads the live canonical durable profile id so a profile
96+
link performed after attach cannot orphan session ownership. Ordinary session
97+
provenance retains that id only; it does not retain a profile display label.
98+
When execution identity recording is explicitly enabled, its audit context may
99+
also retain the prepared display label after secret redaction and the
100+
128-character bound. A resolved durable profile, including one established by
101+
verified trusted-proxy or Tailscale identity, supplies a pseudonymized person
102+
invoker. A paired device adds device assurance but never becomes a person.
103+
Shared tokens, passwords, auth-none connections, and other profileless clients
104+
remain unattributed. If authenticated user evidence promises a durable profile
105+
but profile resolution fails, the invoker is `unknown` rather than guessed from
106+
headers, device ids, connection ids, or credentials.
107+
94108
Each present context currently projects one run-admission receipt. Its outcome
95109
is `not-applicable`, its policy and grant references are empty, and its reason
96110
states that no identity-aware policy or grant evaluation was proven. This is
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ExecutionIdentityAdmissionFacts } from "../audit/execution-identity-admission.js";
2+
3+
type AgentCommandAdmissionFacts = Readonly<
4+
Pick<ExecutionIdentityAdmissionFacts, "assurance" | "ingress" | "invoker">
5+
>;
6+
7+
const factsByIngress = new WeakMap<object, AgentCommandAdmissionFacts>();
8+
9+
export function attachAgentCommandAdmissionFacts(
10+
ingress: object,
11+
facts: AgentCommandAdmissionFacts,
12+
): void {
13+
factsByIngress.set(ingress, facts);
14+
}
15+
16+
export function getAgentCommandAdmissionFacts(
17+
ingress: object,
18+
): AgentCommandAdmissionFacts | undefined {
19+
return factsByIngress.get(ingress);
20+
}

‎src/agents/agent-command-execution-identity.test.ts‎

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
import { describe, expect, it } from "vitest";
2-
import { sanitizePublicAgentCommandIngressOpts } from "./agent-command-execution-identity.js";
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import {
3+
configureExecutionIdentityAdmissionSink,
4+
type ExecutionIdentityAdmissionWork,
5+
} from "../audit/execution-identity-admission.js";
6+
import { attachAgentCommandAdmissionFacts } from "./agent-command-admission-facts.js";
7+
import {
8+
prepareAgentCommandExecutionIdentity,
9+
sanitizePublicAgentCommandIngressOpts,
10+
} from "./agent-command-execution-identity.js";
311
import type { AgentCommandIngressOpts } from "./command/types.js";
412

13+
let cleanupSink: (() => void) | undefined;
14+
15+
afterEach(() => {
16+
cleanupSink?.();
17+
cleanupSink = undefined;
18+
});
19+
520
describe("sanitizePublicAgentCommandIngressOpts", () => {
621
it("removes a forged cron creator authority capability from plain-JavaScript ingress", () => {
722
const forgedCapability = {
@@ -22,3 +37,123 @@ describe("sanitizePublicAgentCommandIngressOpts", () => {
2237
});
2338
});
2439
});
40+
41+
describe("Gateway agent command execution identity", () => {
42+
it("carries only the prepared bounded, redacted label into opt-in run admission", async () => {
43+
let work: ExecutionIdentityAdmissionWork | undefined;
44+
const displayLabel = "Operator OPENAI_API_KEY=***".padEnd(128, "x");
45+
cleanupSink = configureExecutionIdentityAdmissionSink((candidate) => {
46+
work = candidate;
47+
return true;
48+
});
49+
50+
const opts: AgentCommandIngressOpts = {
51+
message: "attribute this run",
52+
allowModelOverride: false,
53+
};
54+
attachAgentCommandAdmissionFacts(opts, {
55+
ingress: {
56+
kind: "gateway-client",
57+
boundary: "gateway.ws.authenticated-connect",
58+
state: "present",
59+
rawSourceRef: "profile-ada",
60+
},
61+
invoker: {
62+
state: "present",
63+
kind: "person",
64+
rawPrincipalRef: "profile-ada",
65+
displayLabel,
66+
},
67+
assurance: [
68+
{
69+
kind: "durable-profile",
70+
rawEvidenceRef: "profile-ada",
71+
strength: "boundary-verified",
72+
},
73+
],
74+
});
75+
const prepared = prepareAgentCommandExecutionIdentity({
76+
opts,
77+
prepared: {
78+
cfg: { logging: { audit: { enabled: true, executionIdentity: true } } },
79+
runId: "run-profiled",
80+
sessionAgentId: "main",
81+
sessionId: "session-profiled",
82+
},
83+
ingress: { kind: "api", boundary: "agent-command.from-ingress", state: "unknown" },
84+
lifecycleGeneration: "generation-1",
85+
});
86+
87+
await prepared.admit("embedded");
88+
89+
expect(work).toMatchObject({
90+
kind: "capture",
91+
envelope: {
92+
ingress: {
93+
kind: "gateway-client",
94+
boundary: "gateway.ws.authenticated-connect",
95+
state: "present",
96+
},
97+
invoker: {
98+
state: "present",
99+
kind: "person",
100+
rawPrincipalRef: "profile-ada",
101+
displayLabel: "Operator OPENAI_API_KEY=***",
102+
},
103+
assurance: [
104+
{
105+
kind: "durable-profile",
106+
rawEvidenceRef: "profile-ada",
107+
strength: "boundary-verified",
108+
},
109+
],
110+
},
111+
});
112+
if (work?.kind !== "capture" || work.envelope.invoker?.state !== "present") {
113+
throw new Error("expected captured present invoker");
114+
}
115+
expect(work.envelope.invoker.displayLabel).toBe("Operator OPENAI_API_KEY=***");
116+
expect(work.envelope.invoker.displayLabel?.length).toBeLessThanOrEqual(128);
117+
});
118+
119+
it("does not offer the prepared profile label to storage without execution audit opt-in", async () => {
120+
let work: ExecutionIdentityAdmissionWork | undefined;
121+
cleanupSink = configureExecutionIdentityAdmissionSink((candidate) => {
122+
work = candidate;
123+
return true;
124+
});
125+
126+
const opts: AgentCommandIngressOpts = {
127+
message: "do not retain this label",
128+
allowModelOverride: false,
129+
};
130+
attachAgentCommandAdmissionFacts(opts, {
131+
ingress: {
132+
kind: "gateway-client",
133+
boundary: "gateway.ws.authenticated-connect",
134+
state: "present",
135+
},
136+
invoker: {
137+
state: "present",
138+
kind: "person",
139+
rawPrincipalRef: "profile-ada",
140+
displayLabel: "Ada",
141+
},
142+
});
143+
const prepared = prepareAgentCommandExecutionIdentity({
144+
opts,
145+
prepared: {
146+
cfg: { logging: { audit: { enabled: true, executionIdentity: false } } },
147+
runId: "run-profiled-disabled",
148+
sessionAgentId: "main",
149+
sessionId: "session-profiled-disabled",
150+
},
151+
ingress: { kind: "api", boundary: "agent-command.from-ingress", state: "unknown" },
152+
lifecycleGeneration: "generation-1",
153+
});
154+
155+
await prepared.admit("embedded");
156+
157+
expect(work).toBeUndefined();
158+
});
159+
});

‎src/agents/agent-command-execution-identity.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
prepareAgentRunAdmission,
99
type OperationalRunInstanceRef,
1010
} from "./admitted-run-context.js";
11+
import {
12+
attachAgentCommandAdmissionFacts,
13+
getAgentCommandAdmissionFacts,
14+
} from "./agent-command-admission-facts.js";
1115
import type {
1216
AgentCommandGatewayIngressOpts,
1317
AgentCommandIngressOpts,
@@ -38,13 +42,16 @@ function prepareAgentCommandRunAdmission(params: {
3842
runId: string;
3943
onAdmitted?: Parameters<typeof prepareAgentRunAdmission>[0]["onAdmitted"];
4044
}) {
45+
const admissionFacts = getAgentCommandAdmissionFacts(params.operationalRunInstance) ?? {
46+
ingress: params.ingress,
47+
};
4148
return prepareAgentRunAdmission({
4249
cfg: params.cfg,
4350
operationalRunInstance: params.operationalRunInstance,
4451
facts: {
4552
runId: params.runId,
4653
agentId: params.agentId,
47-
ingress: params.ingress,
54+
...admissionFacts,
4855
},
4956
...(params.admission ? { recovery: params.admission } : {}),
5057
...(params.onAdmitted ? { onAdmitted: params.onAdmitted } : {}),
@@ -96,13 +103,18 @@ export function prepareAgentCommandExecutionIdentity(params: {
96103
lifecycleGeneration: string;
97104
}) {
98105
const { opts, prepared } = params;
106+
const operationalRunInstance =
107+
opts.operationalRunInstance ?? createOperationalRunInstanceRef(prepared.runId);
108+
const admissionFacts = getAgentCommandAdmissionFacts(params.opts.runContext ?? params.opts);
109+
if (admissionFacts) {
110+
attachAgentCommandAdmissionFacts(operationalRunInstance, admissionFacts);
111+
}
99112
return executionIdentity.prepare({
100113
admission: opts.executionIdentityAdmission,
101114
agentId: prepared.sessionAgentId,
102115
cfg: prepared.cfg,
103116
ingress: params.ingress,
104-
operationalRunInstance:
105-
opts.operationalRunInstance ?? createOperationalRunInstanceRef(prepared.runId),
117+
operationalRunInstance,
106118
runId: prepared.runId,
107119
onAdmitted: async (admittedRunContext) => {
108120
await opts.onAdmittedRunContext?.(admittedRunContext);

‎src/gateway/agent-turn/agent-run-execution-phase.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { randomUUID } from "node:crypto";
22
import { ErrorCodes, errorShape } from "../../../packages/gateway-protocol/src/index.js";
33
import { getAdmittedRunDelegatedAuthority } from "../../agents/admitted-run-context.js";
4+
import { attachAgentCommandAdmissionFacts } from "../../agents/agent-command-admission-facts.js";
45
import type { AgentRunTerminalOutcome } from "../../agents/agent-run-terminal-outcome.js";
56
import {
67
claimExecApprovalFollowupRuntimeHandoff,
@@ -41,6 +42,7 @@ import {
4142
buildRunUserTurnIdempotencyKey,
4243
createUserTurnTranscriptRecorder,
4344
} from "../../sessions/user-turn-transcript.js";
45+
import { getGatewayLocalUserIngress } from "../local-user-ingress.js";
4446
import type { AgentRunRequest } from "../server-methods/agent-request-types.js";
4547
import { createAgentRunModelSelectionHandler } from "../server-methods/agent-run-model-selection.js";
4648
import { resolveSessionRuntimeCwd } from "../server-methods/agent-session-reset.js";
@@ -366,6 +368,10 @@ export function startAgentRunExecution(params: {
366368
restartRecoveryChannelContext?.sameChannelThreadRequired,
367369
);
368370

371+
const localUserIngress = getGatewayLocalUserIngress(params.client);
372+
if (localUserIngress) {
373+
attachAgentCommandAdmissionFacts(runContext, localUserIngress.facts);
374+
}
369375
dispatchAgentRunFromGateway({
370376
cronCreatorAuthority: prepared.cronCreatorAuthority,
371377
ingressOpts: {

‎src/gateway/agent-turn/principal.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
GATEWAY_CLIENT_CAPS,
33
hasGatewayClientCap,
44
} from "../../../packages/gateway-protocol/src/client-info.js";
5+
import { transferGatewayLocalUserIngress } from "../local-user-ingress.js";
56
import type { GatewayClient, GatewayRequestContext } from "../server-methods/shared-types.js";
67
import type { AgentTurnPrincipal } from "./types.js";
78

@@ -10,14 +11,16 @@ export function captureAgentTurnPrincipal(client: GatewayClient | null): AgentTu
1011
if (!client) {
1112
return null;
1213
}
13-
return {
14+
const principal: AgentTurnPrincipal = {
1415
authenticatedUserId: client.authenticatedUserId,
1516
authenticatedUserProfile: client.authenticatedUserProfile,
1617
connId: client.connId,
1718
connect: client.connect,
1819
internal: client.internal,
1920
isDeviceTokenAuth: client.isDeviceTokenAuth,
2021
};
22+
transferGatewayLocalUserIngress(client, principal);
23+
return principal;
2124
}
2225

2326
/** Preserve capability-gated tool-event observation across agent turn entry paths. */

0 commit comments

Comments
 (0)