Skip to content

Commit 2deda04

Browse files
authored
fix: preserve policy owners across reviews and compaction (#133912)
* fix: preserve policy owners across reviews and compaction * fix: retain policy owners through native tool construction * fix(agents): preserve policy keys for plugin delivery * test(codex): use prepared runtime plan in hook fixture
1 parent 9596987 commit 2deda04

66 files changed

Lines changed: 1270 additions & 1242 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.

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ src/agents/bash-tools.exec-approval-followup.ts 5
16821682
src/agents/bash-tools.exec-approval-request.ts 2
16831683
src/agents/bash-tools.exec-host-node-phases.ts 3
16841684
src/agents/bash-tools.exec-host-node.ts 2
1685-
src/agents/bash-tools.exec-request-preparation.ts 7
1685+
src/agents/bash-tools.exec-request-preparation.ts 6
16861686
src/agents/bash-tools.exec-run.ts 1
16871687
src/agents/bash-tools.exec-runtime.ts 2
16881688
src/agents/bash-tools.exec-script-preflight.ts 2

‎docs/concepts/memory.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ OpenClaw runs a silent turn that reminds the agent to save important context
219219
to memory files. This is on by default; set
220220
`agents.defaults.compaction.memoryFlush.enabled: false` to turn it off.
221221

222+
Memory flushing requires writable workspace access. Sessions whose sandbox
223+
requires read-only or no workspace access skip the flush, including sessions
224+
with a persisted sandbox requirement that overrides the agent's configuration.
225+
222226
To keep that housekeeping turn on a local model, set an exact override that
223227
applies only to the memory-flush turn (it does not inherit the active
224228
session's model fallback chain):

‎docs/tools/self-learning.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ Experience review starts only when all of these conditions hold:
6868
- no agent or reply run is still active.
6969

7070
A later foreground completion in the same session restarts the quiet period.
71-
Only one experience review runs at a time. The foreground answer is never delayed.
71+
Pending reviews belong to an agent and session together, so agents using `global`
72+
retain separate candidates. Only one experience review runs at a time. The
73+
foreground answer is never delayed.
7274

7375
The reviewer continues the finished turn from the same transcript prefix, but
7476
runs under a private detached session identity. This keeps the foreground session
7577
available while the provider reuses its prompt cache. The review message and tool
76-
results never enter the foreground transcript or session record.
78+
results never enter the foreground transcript or session record. Reviews retain
79+
the foreground session's sandbox policy, including during compaction.
7780

7881
The reviewer is detached and biased toward small, well-evidenced captures. It
7982
receives an authoritative receipt of the skills the foreground run actually

‎extensions/codex/src/app-server/compact.test.ts‎

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,38 +1170,46 @@ describe("maybeCompactCodexAppServerSession", () => {
11701170
expect(fake.request).not.toHaveBeenCalled();
11711171
});
11721172

1173-
it("uses the retained agent for unscoped explicit-roster compaction", async () => {
1174-
const fake = createFakeCodexClient();
1175-
setCodexAppServerClientFactoryForTest(async () => fake.client);
1176-
const sessionFile = await writeTestBinding();
1177-
1178-
const result = requireCompactResult(
1179-
await maybeCompactCodexAppServerSession({
1180-
sessionId: "session-1",
1181-
sessionKey: "node-session",
1182-
sessionFile,
1183-
workspaceDir: tempDir,
1184-
trigger: "manual",
1185-
agentId: "alpha",
1186-
config: {
1187-
tools: { exec: { host: "gateway" } },
1188-
agents: {
1189-
entries: {
1190-
alpha: { tools: { exec: { host: "node", node: "worker-1" } } },
1191-
beta: {},
1173+
it.each([
1174+
["node-session", "alpha", undefined, undefined],
1175+
["agent:beta:session-1", "beta", "global", "alpha"],
1176+
] as const)(
1177+
"uses the retained policy owner for explicit-roster compaction (%s)",
1178+
async (sessionKey, agentId, sandboxSessionKey, sandboxAgentId) => {
1179+
const fake = createFakeCodexClient();
1180+
setCodexAppServerClientFactoryForTest(async () => fake.client);
1181+
const sessionFile = await writeTestBinding();
1182+
1183+
const result = requireCompactResult(
1184+
await maybeCompactCodexAppServerSession({
1185+
sessionId: "session-1",
1186+
sessionKey,
1187+
sandboxSessionKey,
1188+
sandboxAgentId,
1189+
sessionFile,
1190+
workspaceDir: tempDir,
1191+
trigger: "manual",
1192+
agentId,
1193+
config: {
1194+
tools: { exec: { host: "gateway" } },
1195+
agents: {
1196+
entries: {
1197+
alpha: { tools: { exec: { host: "node", node: "worker-1" } } },
1198+
beta: {},
1199+
},
11921200
},
11931201
},
1194-
},
1195-
}),
1196-
);
1202+
}),
1203+
);
11971204

1198-
expect(result.ok).toBe(false);
1199-
expect(result.compacted).toBe(false);
1200-
expect(result.reason).toContain(
1201-
"Codex-native native compaction is unavailable because OpenClaw exec host=node is active for this session.",
1202-
);
1203-
expect(fake.request).not.toHaveBeenCalled();
1204-
});
1205+
expect(result.ok).toBe(false);
1206+
expect(result.compacted).toBe(false);
1207+
expect(result.reason).toContain(
1208+
"Codex-native native compaction is unavailable because OpenClaw exec host=node is active for this session.",
1209+
);
1210+
expect(fake.request).not.toHaveBeenCalled();
1211+
},
1212+
);
12051213

12061214
it("does not finish until the matching native compaction turn completes", async () => {
12071215
const fake = createFakeCodexClient({ autoCompleteCompaction: false });

‎extensions/codex/src/app-server/compact.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ async function compactCodexNativeThread(
445445
config: params.config,
446446
sessionKey: params.sandboxSessionKey ?? params.sessionKey,
447447
sessionId: params.sessionId,
448-
agentId: params.agentId,
448+
agentId: params.sandboxAgentId ?? params.agentId,
449449
sandbox,
450450
surface: "native compaction",
451451
});

‎extensions/codex/src/app-server/dynamic-tool-build.test.ts‎

Lines changed: 115 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import {
1111
wrapToolWithBeforeToolCallHook,
1212
} from "openclaw/plugin-sdk/agent-harness-runtime";
1313
import { readMemoryArtifactProvenance } from "openclaw/plugin-sdk/memory-core-host-runtime-core";
14-
import { createAgentHarnessHostCapabilitiesForTest } from "openclaw/plugin-sdk/plugin-test-runtime";
14+
import {
15+
createAgentHarnessHostCapabilitiesForTest,
16+
createMockPluginRegistry,
17+
initializeGlobalHookRunner,
18+
resetGlobalHookRunner,
19+
} from "openclaw/plugin-sdk/plugin-test-runtime";
1520
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1621
import { dynamicToolBuildState } from "./dynamic-tool-build-state.js";
1722
import {
@@ -193,6 +198,7 @@ async function buildDynamicToolsForTest(
193198
nativeToolSurfaceEnabled: true,
194199
runAbortController: new AbortController(),
195200
sessionAgentId: "main",
201+
policyAgentId: params.sandboxAgentId ?? options.sessionAgentId ?? "main",
196202
pluginConfig: {},
197203
onYieldDetected: () => undefined,
198204
...options,
@@ -383,62 +389,112 @@ describe("Codex app-server dynamic tool build", () => {
383389
]);
384390
});
385391

386-
it("filters disabled-native replacements with the canonical conversation profile", async () => {
387-
setOpenClawCodingToolsFactoryForTests((options) =>
388-
createOpenClawCodingTools(options).filter((tool) =>
389-
["read", "write", "edit", "apply_patch", "exec", "process"].includes(tool.name),
390-
),
391-
);
392-
const workspaceDir = path.join(tempDir, "workspace");
393-
const params = createParams(path.join(tempDir, "policy-session.jsonl"), workspaceDir);
394-
params.disableTools = false;
395-
params.runtimePlan = createCodexRuntimePlanFixture();
396-
params.conversationToolPolicy = { deny: ["exec", "process", "write", "edit"] };
397-
const tools = await buildDynamicToolsForTest(params, workspaceDir, {
398-
nativeToolSurfaceEnabled: false,
399-
});
400-
const names = tools.map((tool) => tool.name);
401-
402-
expect(names.toSorted()).toEqual(["apply_patch", "read"]);
403-
const conversationNativeTools = await buildDynamicToolsForTest(params, workspaceDir, {
404-
nativeToolSurfaceEnabled: true,
405-
});
406-
expect(conversationNativeTools.map((tool) => tool.name)).not.toContain("gateway_exec");
407-
expect(conversationNativeTools.map((tool) => tool.name)).not.toContain("gateway_process");
408-
409-
params.config = { tools: { deny: ["apply_patch"] } };
410-
const intersected = await buildDynamicToolsForTest(params, workspaceDir, {
411-
nativeToolSurfaceEnabled: false,
412-
});
413-
expect(intersected.map((tool) => tool.name)).not.toContain("apply_patch");
414-
415-
params.conversationToolPolicy = {};
416-
params.config = { tools: { deny: ["exec", "process", "write", "edit"] } };
417-
const globalPolicyTools = await buildDynamicToolsForTest(params, workspaceDir, {
418-
nativeToolSurfaceEnabled: false,
419-
});
420-
expect(globalPolicyTools.map((tool) => tool.name).toSorted()).toEqual(["apply_patch", "read"]);
421-
const globalNativeTools = await buildDynamicToolsForTest(params, workspaceDir, {
422-
nativeToolSurfaceEnabled: true,
423-
});
424-
expect(globalNativeTools.map((tool) => tool.name)).not.toContain("gateway_exec");
425-
expect(globalNativeTools.map((tool) => tool.name)).not.toContain("gateway_process");
426-
427-
params.config = {
428-
agents: {
429-
list: [{ id: "main", tools: { deny: ["exec", "process", "write", "edit"] } }],
430-
},
431-
};
432-
const agentPolicyTools = await buildDynamicToolsForTest(params, workspaceDir, {
433-
nativeToolSurfaceEnabled: false,
434-
});
435-
expect(agentPolicyTools.map((tool) => tool.name).toSorted()).toEqual(["apply_patch", "read"]);
436-
const agentNativeTools = await buildDynamicToolsForTest(params, workspaceDir, {
437-
nativeToolSurfaceEnabled: true,
438-
});
439-
expect(agentNativeTools.map((tool) => tool.name)).not.toContain("gateway_exec");
440-
expect(agentNativeTools.map((tool) => tool.name)).not.toContain("gateway_process");
441-
});
392+
const policyDeny = ["exec", "process", "write", "edit"];
393+
it.each<{
394+
source: string;
395+
config?: EmbeddedRunAttemptParams["config"];
396+
conversationToolPolicy?: EmbeddedRunAttemptParams["conversationToolPolicy"];
397+
sandboxAgentId?: string;
398+
denied: boolean;
399+
}>([
400+
{ source: "allowed", sandboxAgentId: "policy", denied: false },
401+
{ source: "conversation", conversationToolPolicy: { deny: policyDeny }, denied: true },
402+
{
403+
source: "intersection",
404+
conversationToolPolicy: { deny: policyDeny },
405+
config: { tools: { deny: ["apply_patch"] } },
406+
denied: true,
407+
},
408+
{ source: "global", config: { tools: { deny: policyDeny } }, denied: true },
409+
{
410+
source: "execution owner",
411+
config: { agents: { entries: { main: { tools: { deny: policyDeny } } } } },
412+
denied: true,
413+
},
414+
{
415+
source: "retained owner",
416+
config: { agents: { entries: { main: {}, policy: { tools: { deny: policyDeny } } } } },
417+
sandboxAgentId: "policy",
418+
denied: true,
419+
},
420+
])(
421+
"enforces $source policy when a dynamic write reaches the bridge",
422+
async ({ source, config, conversationToolPolicy, sandboxAgentId, denied }) => {
423+
const workspaceDir = path.join(tempDir, "workspace");
424+
await fs.mkdir(workspaceDir, { recursive: true });
425+
const targetPath = path.join(workspaceDir, "policy-write.txt");
426+
const params = createParams(path.join(tempDir, "policy-session.jsonl"), workspaceDir);
427+
params.agentId = "main";
428+
params.sandboxSessionKey = sandboxAgentId ? "global" : undefined;
429+
params.sandboxAgentId = sandboxAgentId;
430+
params.conversationToolPolicy = conversationToolPolicy;
431+
params.disableTools = false;
432+
params.runtimePlan = createCodexRuntimePlanFixture();
433+
params.config = {
434+
plugins: { enabled: false },
435+
agents: config?.agents ?? { entries: { main: {}, policy: {} } },
436+
tools: { ...config?.tools, fs: { workspaceOnly: true } },
437+
};
438+
const beforeToolCall = vi.fn(() => ({}));
439+
initializeGlobalHookRunner(
440+
createMockPluginRegistry([{ hookName: "before_tool_call", handler: beforeToolCall }]),
441+
);
442+
try {
443+
await bindProductionCodexHostCapabilities(params);
444+
setOpenClawCodingToolsFactoryForTests((options) =>
445+
createOpenClawCodingTools(options).filter((tool) =>
446+
["read", "write", "edit", "apply_patch", "exec", "process"].includes(tool.name),
447+
),
448+
);
449+
const tools = await buildDynamicToolsForTest(params, workspaceDir, {
450+
sandboxSessionKey: params.sandboxSessionKey ?? params.sessionKey,
451+
nativeToolSurfaceEnabled: false,
452+
sandbox: null,
453+
});
454+
const bridge = createCodexDynamicToolBridge({
455+
tools,
456+
signal: new AbortController().signal,
457+
loading: "direct",
458+
hookContext: {
459+
agentId: "main",
460+
sessionKey: params.sessionKey,
461+
sessionId: params.sessionId,
462+
runId: params.runId,
463+
workspaceDir,
464+
},
465+
});
466+
const result = await bridge.handleToolCall({
467+
threadId: "thread-policy",
468+
turnId: "turn-policy",
469+
callId: "write-policy",
470+
tool: "write",
471+
arguments: { path: targetPath, content: "allowed owner write" },
472+
});
473+
if (denied) {
474+
await expect(fs.readFile(targetPath, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
475+
expect(result.success).toBe(false);
476+
expect(beforeToolCall).not.toHaveBeenCalled();
477+
expect(tools.map((tool) => tool.name).toSorted()).toEqual(
478+
source === "intersection" ? ["read"] : ["apply_patch", "read"],
479+
);
480+
const nativeTools = await buildDynamicToolsForTest(params, workspaceDir, {
481+
sandboxSessionKey: params.sandboxSessionKey ?? params.sessionKey,
482+
});
483+
expect(nativeTools.map((tool) => tool.name)).not.toContain("gateway_exec");
484+
expect(nativeTools.map((tool) => tool.name)).not.toContain("gateway_process");
485+
} else {
486+
expect(result.success).toBe(true);
487+
await expect(fs.readFile(targetPath, "utf8")).resolves.toBe("allowed owner write");
488+
expect(beforeToolCall).toHaveBeenCalledWith(
489+
expect.objectContaining({ toolName: "write" }),
490+
expect.objectContaining({ agentId: "main", sessionKey: params.sessionKey }),
491+
);
492+
}
493+
} finally {
494+
resetGlobalHookRunner();
495+
}
496+
},
497+
);
442498

443499
it("removes account-wide app access when native tools are restricted", () => {
444500
expect(
@@ -1697,6 +1753,7 @@ describe("Codex app-server dynamic tool build", () => {
16971753
runtimePolicyParams.runtimePlan = createCodexRuntimePlanFixture();
16981754
runtimePolicyParams.sessionKey = "agent:main:session-1";
16991755
runtimePolicyParams.sandboxSessionKey = "agent:policy:session-1";
1756+
runtimePolicyParams.sandboxAgentId = "policy";
17001757
runtimePolicyParams.config = {
17011758
agents: {
17021759
entries: {
@@ -1713,7 +1770,7 @@ describe("Codex app-server dynamic tool build", () => {
17131770
const runtimePolicyTools = await buildDynamicToolsForTest(runtimePolicyParams, workspaceDir, {
17141771
sandboxSessionKey: "agent:policy:session-1",
17151772
nativeToolSurfaceEnabled: runtimePolicyNativeToolSurfaceEnabled,
1716-
sessionAgentId: "policy",
1773+
sessionAgentId: "main",
17171774
});
17181775

17191776
expect(runtimePolicyNativeToolSurfaceEnabled).toBe(false);

0 commit comments

Comments
 (0)