Skip to content

Commit 79b8775

Browse files
committed
feat(audit): preserve exact agent execution provenance
1 parent 1574723 commit 79b8775

49 files changed

Lines changed: 6417 additions & 572 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/gateway-protocol/src/schema/nodes.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe("node protocol schemas", () => {
1919
expect(Value.Check(NodeInvokeRequestEventSchema, { ...request, sessionKey: null })).toBe(true);
2020
expect(Value.Check(NodeInvokeRequestEventSchema, { ...request, sessionKey: "" })).toBe(false);
2121
expect(Value.Check(NodeInvokeRequestEventSchema, { ...request, extra: true })).toBe(false);
22+
for (const forbidden of ["attribution", "passportId", "principalId", "instanceId"]) {
23+
expect(Value.Check(NodeInvokeRequestEventSchema, { ...request, [forbidden]: "forged" })).toBe(
24+
false,
25+
);
26+
}
2227
});
2328

2429
it("accepts bounded progress chunks and rejects extra fields", () => {
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
export const STATE_SCHEMA_INLINE_PLUGIN_NAME: string;
22

3-
export function createStateSchemaInlinePlugin(rootDir?: string): {
3+
export type StateSchemaInlinePluginOptions = {
4+
vitestFsModuleCache?: boolean;
5+
};
6+
7+
export type VitestCacheKeyGenerator = (context: {
8+
id: string;
9+
sourceCode: string;
10+
environment: unknown;
11+
}) => string | undefined | null | false;
12+
13+
export function createStateSchemaInlinePlugin(
14+
rootDir?: string,
15+
options?: StateSchemaInlinePluginOptions,
16+
): {
417
name: string;
518
load(
619
this: { addWatchFile(id: string): void },
720
id: string,
821
): { code: string; moduleType: "js" } | null;
22+
configureVitest?: (context: {
23+
experimental_defineCacheKeyGenerator(callback: VitestCacheKeyGenerator): void;
24+
}) => void;
925
};

‎scripts/lib/state-schema-inline-plugin.mjs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ const STATE_SCHEMA_MODULES = [
1717
];
1818

1919
/** Inline canonical schema bytes so bundled consumers need no SQL asset. */
20-
export function createStateSchemaInlinePlugin(rootDir = process.cwd()) {
20+
export function createStateSchemaInlinePlugin(rootDir = process.cwd(), options = {}) {
2121
const schemasByModulePath = new Map(
2222
STATE_SCHEMA_MODULES.map((schema) => [path.resolve(rootDir, schema.modulePath), schema]),
2323
);
2424

25-
return {
25+
const plugin = {
2626
name: STATE_SCHEMA_INLINE_PLUGIN_NAME,
2727
load(id) {
2828
const schema = schemasByModulePath.get(path.resolve(id));
@@ -37,4 +37,15 @@ export function createStateSchemaInlinePlugin(rootDir = process.cwd()) {
3737
};
3838
},
3939
};
40+
if (options.vitestFsModuleCache) {
41+
plugin.configureVitest = ({ experimental_defineCacheKeyGenerator }) => {
42+
experimental_defineCacheKeyGenerator(({ id }) => {
43+
const schema = schemasByModulePath.get(path.resolve(id));
44+
return schema
45+
? fs.readFileSync(path.resolve(rootDir, schema.schemaPath), "utf8")
46+
: undefined;
47+
});
48+
};
49+
}
50+
return plugin;
4051
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/** Guardrails keeping execution attribution private to host-owned runtime wiring. */
2+
import { readFileSync, readdirSync } from "node:fs";
3+
import { dirname, extname, relative, resolve } from "node:path";
4+
import { fileURLToPath } from "node:url";
5+
import { describe, expect, it } from "vitest";
6+
7+
const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
8+
const SOURCE_EXTENSIONS = new Set([".cjs", ".cts", ".js", ".mjs", ".mts", ".ts", ".tsx"]);
9+
const SKIPPED_DIRECTORIES = new Set([".git", "dist", "node_modules", "vendor"]);
10+
11+
const ALLOWED_PRODUCTION_IMPORTERS = [
12+
"src/agents/agent-command-execution-identity.ts",
13+
"src/agents/agent-tools.before-tool-call.attribution.ts",
14+
"src/agents/agent-tools.ts",
15+
"src/agents/btw.ts",
16+
"src/agents/cli-runner/types.ts",
17+
"src/agents/command/acp-execution.ts",
18+
"src/agents/command/attempt-execution.ts",
19+
"src/agents/command/run-embedded-attempt.ts",
20+
"src/agents/command/types.ts",
21+
"src/agents/embedded-agent-runner/run/attempt-execution-attribution.ts",
22+
"src/agents/embedded-agent-runner/run/internal-params.ts",
23+
"src/agents/embedded-agent-runner/run/lane-controller.ts",
24+
"src/agents/harness/side-question-execution-attribution.ts",
25+
"src/auto-reply/reply/agent-runner-cli-candidate.ts",
26+
"src/auto-reply/reply/agent-runner-execution-identity.ts",
27+
"src/auto-reply/reply/agent-runner-execution.types.ts",
28+
"src/gateway/server-methods/agent-run-admission-phase.ts",
29+
"src/infra/agent-run-registry.ts",
30+
] as const;
31+
32+
const FORBIDDEN_PUBLIC_ROOTS = [
33+
"apps",
34+
"extensions",
35+
"packages/gateway-protocol",
36+
"src/plugin-sdk",
37+
"ui",
38+
] as const;
39+
40+
function listSourceFiles(root: string): string[] {
41+
return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
42+
if (SKIPPED_DIRECTORIES.has(entry.name)) {
43+
return [];
44+
}
45+
const path = resolve(root, entry.name);
46+
if (entry.isDirectory()) {
47+
return listSourceFiles(path);
48+
}
49+
return SOURCE_EXTENSIONS.has(extname(entry.name)) ? [path] : [];
50+
});
51+
}
52+
53+
function repoPath(path: string): string {
54+
return relative(REPO_ROOT, path).replaceAll("\\", "/");
55+
}
56+
57+
function isProductionSource(path: string): boolean {
58+
return !/\.test(?:-support)?\.[cm]?[jt]sx?$|\.spec\.[cm]?[jt]sx?$/.test(path);
59+
}
60+
61+
describe("agent execution attribution architecture guardrails", () => {
62+
it("limits the private primitive to the reviewed host-owned wiring", () => {
63+
const actualImporters = listSourceFiles(resolve(REPO_ROOT, "src"))
64+
.filter(isProductionSource)
65+
.filter((path) => readFileSync(path, "utf8").includes("agent-execution-attribution.js"))
66+
.map(repoPath)
67+
.toSorted();
68+
69+
expect(actualImporters).toEqual(ALLOWED_PRODUCTION_IMPORTERS.toSorted());
70+
});
71+
72+
it("keeps the private primitive out of public SDK, protocol, plugin, app, and UI roots", () => {
73+
const leaks = FORBIDDEN_PUBLIC_ROOTS.flatMap((root) =>
74+
listSourceFiles(resolve(REPO_ROOT, root))
75+
.filter((path) => {
76+
const text = readFileSync(path, "utf8");
77+
return (
78+
text.includes("AgentExecutionAttribution") ||
79+
text.includes("agent-execution-attribution")
80+
);
81+
})
82+
.map(repoPath),
83+
);
84+
85+
expect(leaks).toEqual([]);
86+
});
87+
88+
it("keeps undecided identity fields out of the node protocol and public tool options", () => {
89+
const nodeSchema = readFileSync(
90+
resolve(REPO_ROOT, "packages/gateway-protocol/src/schema/nodes.ts"),
91+
"utf8",
92+
);
93+
for (const field of ["attribution", "passportId", "principalId", "instanceId"]) {
94+
expect(nodeSchema).not.toMatch(new RegExp(`\\b${field}\\s*:`));
95+
}
96+
97+
const agentTools = readFileSync(resolve(REPO_ROOT, "src/agents/agent-tools.ts"), "utf8");
98+
const publicOptions = agentTools.slice(
99+
agentTools.indexOf("type OpenClawCodingToolsOptions ="),
100+
agentTools.indexOf("type OpenClawCodingToolsInternalOptions ="),
101+
);
102+
expect(publicOptions).not.toMatch(/\battribution\s*\??:/);
103+
});
104+
});

‎src/agents/embedded-agent-runner/run/attempt.llm-boundary.cache-stability.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,33 @@ describe("prompt-cache byte-identity (issue #3658)", () => {
266266
);
267267
});
268268

269+
it("does not copy host-owned identity metadata into provider payloads", async () => {
270+
const message = currentUserMsg("Provider payload identity sentinel", TS_TURN1) as AgentMsg & {
271+
attribution?: unknown;
272+
passportId?: string;
273+
principalId?: string;
274+
};
275+
message.attribution = {
276+
runId: "private-run-sentinel",
277+
sessionKey: "private-session-key-sentinel",
278+
};
279+
message.passportId = "private-passport-sentinel";
280+
message.principalId = "private-principal-sentinel";
281+
282+
const payloads = await Promise.all([
283+
captureOpenAICompletionsPayload([message]),
284+
captureOpenAIResponsesPayload([message]),
285+
]);
286+
287+
for (const payload of payloads) {
288+
const wireBytes = JSON.stringify(payload);
289+
expect(wireBytes).not.toContain("private-run-sentinel");
290+
expect(wireBytes).not.toContain("private-session-key-sentinel");
291+
expect(wireBytes).not.toContain("private-passport-sentinel");
292+
expect(wireBytes).not.toContain("private-principal-sentinel");
293+
}
294+
});
295+
269296
it("stamp derives from message timestamp, not wall-clock — repeated calls are byte-stable", () => {
270297
// Same message object (fixed timestamp) → identical serialization regardless
271298
// of when normalize is called. Guards against any "now"-based drift.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type {
2+
AgentAuditPendingTerminalWithOwnership,
3+
AgentAuditSettledRun,
4+
} from "./agent-event-audit-terminal.js";
5+
6+
function moveMapValue<T>(map: Map<string, T>, from: string, to: string): void {
7+
const value = map.get(from);
8+
if (value === undefined || map.has(to)) {
9+
return;
10+
}
11+
map.delete(from);
12+
map.set(to, value);
13+
}
14+
15+
function moveSetValue(set: Set<string>, from: string, to: string): void {
16+
if (set.delete(from)) {
17+
set.add(to);
18+
}
19+
}
20+
21+
function movePendingTerminal(
22+
pendingTerminals: Map<string, AgentAuditPendingTerminalWithOwnership>,
23+
from: string,
24+
to: string,
25+
): void {
26+
const pending = pendingTerminals.get(from);
27+
if (!pending || pendingTerminals.has(to)) {
28+
return;
29+
}
30+
pendingTerminals.delete(from);
31+
pending.flushTarget.attemptStateKey = to;
32+
pendingTerminals.set(to, pending);
33+
}
34+
35+
export function adoptAgentAuditAttemptState(params: {
36+
from: string;
37+
to: string;
38+
openAttempts: Set<string>;
39+
unownedAttempts: Set<string>;
40+
rejectedStarts: Set<string>;
41+
pendingTerminals: Map<string, AgentAuditPendingTerminalWithOwnership>;
42+
settledAttempts: Map<string, AgentAuditSettledRun>;
43+
attemptEpochs: Map<string, number>;
44+
attemptStartSequences: Map<string, number>;
45+
}): boolean {
46+
if (params.from === params.to || !params.openAttempts.has(params.from)) {
47+
return false;
48+
}
49+
moveSetValue(params.openAttempts, params.from, params.to);
50+
params.unownedAttempts.delete(params.from);
51+
moveSetValue(params.rejectedStarts, params.from, params.to);
52+
movePendingTerminal(params.pendingTerminals, params.from, params.to);
53+
moveMapValue(params.settledAttempts, params.from, params.to);
54+
moveMapValue(params.attemptEpochs, params.from, params.to);
55+
moveMapValue(params.attemptStartSequences, params.from, params.to);
56+
return true;
57+
}

0 commit comments

Comments
 (0)