|
| 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 | +}); |
0 commit comments