File size: 9,302 Bytes
34810d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | import path from "node:path";
import { DatabaseSync } from "node:sqlite";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
import { createAuditEventWriter } from "./audit-event-writer.js";
import { pageExecutionDecisionFactsForContext } from "./execution-decision-facts.js";
import type { ExecutionDecisionWork } from "./execution-decision-work.js";
import {
configureExecutionIdentityAdmissionSink,
createExecutionIdentityAdmissionToken,
enqueueExecutionIdentityContextAtAdmission,
} from "./execution-identity-admission.js";
function decisionWork(params: {
token: ReturnType<typeof createExecutionIdentityAdmissionToken>;
receiptId: string;
rawResource: string;
rawTarget: string;
targetNamespace?: "model-route" | "session";
}): ExecutionDecisionWork {
return {
workVersion: 1,
token: params.token,
receipt: {
schemaVersion: 1,
receiptId: params.receiptId,
occurredAt: params.token.createdAt + 1,
action: {
family: "model-routing",
operation: "select",
summary: "Selected one admitted model route.",
},
decision: { outcome: "allowed", reasonCode: "model_route_selected" },
enforcement: {
coverageState: "attribution-only",
policyRefs: [],
grantRefs: [],
contextFieldsUsed: ["contextId", "executionId", "runId"],
},
source: {
owner: "model-routing",
recordRef: params.receiptId,
decisionBoundary: "agent-runtime.post-admission",
},
missingEvidence: [],
remediation: [],
},
refs: {
resource: { namespace: "credential-profile", value: params.rawResource },
target: { namespace: params.targetNamespace ?? "model-route", value: params.rawTarget },
},
};
}
afterEach(() => {
closeOpenClawStateDatabaseForTest();
});
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
describe("private execution decision work", () => {
it.each(["raw ref", "complete envelope"] as const)(
"rejects an oversized %s before the FIFO clone boundary",
async (oversizedPart) => {
const stateDir = tempDirs.make("openclaw-audit-private-decision-bounds-");
const errors: string[] = [];
const writer = createAuditEventWriter({ stateDir, onError: (error) => errors.push(error) });
const token = createExecutionIdentityAdmissionToken("bounded-private-decision-run", {
contextId: "bounded-private-decision-context",
executionId: "bounded-private-decision-execution",
now: 1_000,
});
const work = decisionWork({
token,
receiptId: "bounded-private-decision",
rawResource: oversizedPart === "raw ref" ? "r".repeat(4_097) : "resource",
rawTarget: "target",
});
if (oversizedPart === "complete envelope") {
const refs = Array.from({ length: 16 }, (_, index) => `${index}:`.padEnd(256, "p"));
work.receipt.enforcement.policyRefs = refs;
work.receipt.enforcement.grantRefs = refs;
work.receipt.enforcement.contextFieldsUsed = refs;
work.receipt.missingEvidence = refs;
work.receipt.remediation = Array.from({ length: 8 }, (_, index) => ({
code: `repair-${index}`,
text: "r".repeat(512),
}));
}
await writer.ready;
const clone = vi.spyOn(globalThis, "structuredClone");
expect(writer.recordExecutionDecisionWork(work)).toBe(false);
expect(clone).not.toHaveBeenCalled();
clone.mockRestore();
await writer.stop();
expect(errors).toEqual(["audit execution decision receipt could not be queued"]);
},
);
it("projects private refs inside the admission FIFO without retaining raw owners", async () => {
const stateDir = tempDirs.make("openclaw-audit-private-decision-");
const database = { env: { OPENCLAW_STATE_DIR: stateDir } };
const errors: string[] = [];
const writer = createAuditEventWriter({ stateDir, onError: (error) => errors.push(error) });
const admittedAt = Date.now();
const token = createExecutionIdentityAdmissionToken("private-decision-run", {
contextId: "private-decision-context",
executionId: "private-decision-execution",
now: admittedAt,
});
const rawCredentialProfile = "openai:user@example.test";
const rawModelTarget = "openai\0gpt-5.6-sol";
const clearAdmissionSink = configureExecutionIdentityAdmissionSink(
writer.recordExecutionIdentity,
);
await writer.ready;
expect(
enqueueExecutionIdentityContextAtAdmission(
{
runId: token.runId,
agentId: "main",
ingress: { kind: "local-cli", boundary: "agent-command.local", state: "present" },
runtime: { kind: "embedded" },
},
{ enabled: true, token, runtimeInstanceId: "private-decision-runtime" },
)?.accepted,
).toBe(true);
expect(
writer.recordExecutionDecisionWork(
decisionWork({
token,
receiptId: "private-model-route",
rawResource: rawCredentialProfile,
rawTarget: rawModelTarget,
}),
),
).toBe(true);
clearAdmissionSink();
await writer.stop();
expect(errors).toEqual([]);
const [receipt] = pageExecutionDecisionFactsForContext({
context: token,
limit: 10,
now: admittedAt + 1,
database,
}).receipts;
expect(receipt).toMatchObject({
contextId: token.contextId,
executionId: token.executionId,
runId: token.runId,
action: {
family: "model-routing",
resourceRef: expect.stringMatching(/^hmac-sha256:v1:/u),
targetRef: expect.stringMatching(/^hmac-sha256:v1:/u),
},
});
expect(receipt?.action.resourceRef).not.toBe(receipt?.action.targetRef);
const sqlite = new DatabaseSync(path.join(stateDir, "state", "openclaw.sqlite"), {
readOnly: true,
});
try {
const persisted = JSON.stringify(
sqlite
.prepare("SELECT * FROM execution_decision_facts WHERE receipt_id = ?")
.get("private-model-route"),
);
expect(persisted).not.toContain(rawCredentialProfile);
expect(persisted).not.toContain(rawModelTarget);
} finally {
sqlite.close();
}
});
it("keeps refs restart-stable, namespace-separated, and installation-local", async () => {
const rawRef = "same-private-owner-ref";
const firstStateDir = tempDirs.make("openclaw-audit-private-stability-");
const write = async (params: {
stateDir: string;
suffix: string;
targetNamespace?: "model-route" | "session";
}) => {
const now = Date.now();
const token = createExecutionIdentityAdmissionToken(`run-${params.suffix}`, {
contextId: `context-${params.suffix}`,
executionId: `execution-${params.suffix}`,
now,
});
const database = { env: { OPENCLAW_STATE_DIR: params.stateDir } };
const writer = createAuditEventWriter({ stateDir: params.stateDir });
const clearAdmissionSink = configureExecutionIdentityAdmissionSink(
writer.recordExecutionIdentity,
);
await writer.ready;
expect(
enqueueExecutionIdentityContextAtAdmission(
{
runId: token.runId,
agentId: "main",
ingress: { kind: "local-cli", boundary: "agent-command.local" },
runtime: { kind: "embedded" },
},
{ enabled: true, token, runtimeInstanceId: `runtime-${params.suffix}` },
)?.accepted,
).toBe(true);
expect(
writer.recordExecutionDecisionWork(
decisionWork({
token,
receiptId: `receipt-${params.suffix}`,
rawResource: rawRef,
rawTarget: rawRef,
...(params.targetNamespace ? { targetNamespace: params.targetNamespace } : {}),
}),
),
).toBe(true);
clearAdmissionSink();
await writer.stop();
return pageExecutionDecisionFactsForContext({
context: token,
limit: 1,
now: now + 1,
database,
}).receipts[0]!;
};
const first = await write({ stateDir: firstStateDir, suffix: "first" });
closeOpenClawStateDatabaseForTest();
const restarted = await write({ stateDir: firstStateDir, suffix: "restart" });
const sessionScoped = await write({
stateDir: firstStateDir,
suffix: "session",
targetNamespace: "session",
});
closeOpenClawStateDatabaseForTest();
const otherInstallation = await write({
stateDir: tempDirs.make("openclaw-audit-private-other-installation-"),
suffix: "other",
});
expect(restarted.action.resourceRef).toBe(first.action.resourceRef);
expect(restarted.action.targetRef).toBe(first.action.targetRef);
expect(first.action.resourceRef).not.toBe(first.action.targetRef);
expect(sessionScoped.action.targetRef).not.toBe(first.action.targetRef);
expect(otherInstallation.action.resourceRef).not.toBe(first.action.resourceRef);
expect(otherInstallation.action.targetRef).not.toBe(first.action.targetRef);
});
});
|