Skip to content

Commit 22c04b5

Browse files
committed
fix(codex): preserve ring-zero context isolation
1 parent b4be559 commit 22c04b5

6 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎extensions/codex/src/app-server/attempt-context.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe("Codex app-server attempt context", () => {
141141
sessionKey: "agent:main:session-1",
142142
sessionAgentId: "main",
143143
memoryToolNames: ["memory_search", "memory_get"],
144+
ringZeroActive: false,
144145
});
145146

146147
expect(context.memoryReferenceFiles).toEqual([]);
@@ -181,6 +182,7 @@ describe("Codex app-server attempt context", () => {
181182
sessionKey: "agent:marketing-agent:session-1",
182183
sessionAgentId: "marketing-agent",
183184
memoryToolNames: ["memory_search", "memory_get"],
185+
ringZeroActive: false,
184186
sandboxed: true,
185187
});
186188

@@ -219,6 +221,7 @@ describe("Codex app-server attempt context", () => {
219221
sessionKey: "agent:main:session-1",
220222
sessionAgentId: "main",
221223
memoryToolNames: ["memory_search", "memory_get"],
224+
ringZeroActive: false,
222225
});
223226

224227
expect(context.threadDeveloperInstructions).toContain("Canonical agent instructions");
@@ -241,6 +244,37 @@ describe("Codex app-server attempt context", () => {
241244
}
242245
});
243246

247+
it("keeps ambient workspace instructions out of overlapping ring-zero restrictions", async () => {
248+
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-ring-zero-workspace-"));
249+
const executionDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-ring-zero-execution-"));
250+
await fs.writeFile(path.join(workspaceDir, "AGENTS.md"), "Ambient workspace instructions");
251+
252+
try {
253+
const context = await buildCodexWorkspaceBootstrapContext({
254+
params: {
255+
sessionId: "session-1",
256+
sessionKey: "agent:openclaw:session-1",
257+
toolsAllow: ["openclaw"],
258+
pluginHarnessToolPolicyRestricted: true,
259+
config: { agents: { defaults: { workspace: workspaceDir } } },
260+
} as EmbeddedRunAttemptParams,
261+
resolvedWorkspace: workspaceDir,
262+
executionWorkspace: executionDir,
263+
effectiveWorkspace: executionDir,
264+
sessionKey: "agent:openclaw:session-1",
265+
sessionAgentId: "openclaw",
266+
memoryToolNames: [],
267+
ringZeroActive: true,
268+
});
269+
270+
expect(context.threadDeveloperInstructions).toBeUndefined();
271+
expect(context.threadDeveloperInstructionFiles).toEqual([]);
272+
} finally {
273+
await fs.rm(workspaceDir, { recursive: true, force: true });
274+
await fs.rm(executionDir, { recursive: true, force: true });
275+
}
276+
});
277+
244278
it("reads and compares thread-bootstrap context-engine projections", () => {
245279
const projection = readContextEngineThreadBootstrapProjection({
246280
mode: "thread_bootstrap",

‎extensions/codex/src/app-server/attempt-context.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export async function buildCodexWorkspaceBootstrapContext(params: {
177177
sessionKey: string;
178178
sessionAgentId: string;
179179
memoryToolNames: readonly string[];
180+
ringZeroActive: boolean;
180181
sandboxed?: boolean;
181182
}): Promise<CodexWorkspaceBootstrapContext> {
182183
try {
@@ -251,7 +252,9 @@ export async function buildCodexWorkspaceBootstrapContext(params: {
251252
!isMessageOnlyCodexSourceReply(params.params) &&
252253
params.params.bootstrapContextMode !== "lightweight";
253254
const threadDeveloperInstructionFiles =
254-
injectOpenClawContext && (inheritsAgentWorkspace || restrictedProjectDocNeedsOpenClawCarrier)
255+
injectOpenClawContext &&
256+
!params.ringZeroActive &&
257+
(inheritsAgentWorkspace || restrictedProjectDocNeedsOpenClawCarrier)
255258
? selectCodexWorkspaceAgentProjectInstructionFiles(contextFiles, params.resolvedWorkspace)
256259
: [];
257260
const turnScopedDeveloperInstructionFiles = injectOpenClawContext

‎extensions/codex/src/app-server/run-attempt-context.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CODEX_APP_SERVER_CONTEXT_ENGINE_HOST,
55
embeddedAgentLog,
66
getAgentHarnessHookRunner,
7+
isHostScopedAgentToolActive,
78
resolveContextEngineOwnerPluginId,
89
runHarnessContextEngineMaintenance,
910
} from "openclaw/plugin-sdk/agent-harness-runtime";
@@ -21,6 +22,7 @@ import {
2122
resolveCodexContinuityProjectionMaxChars,
2223
type CodexProjectedContextRange,
2324
} from "./context-engine-projection.js";
25+
import { isSystemAgentOnlyCodexDynamicToolAllowlist } from "./dynamic-tool-profile.js";
2426
import type { CodexAttemptRuntime } from "./run-attempt-runtime.js";
2527
import { joinPresentSections } from "./run-attempt-state.js";
2628
import type { CodexAttemptTools } from "./run-attempt-tool-setup.js";
@@ -148,6 +150,9 @@ export async function prepareCodexAttemptContext(
148150
sessionKey: contextSessionKey,
149151
sessionAgentId,
150152
memoryToolNames,
153+
ringZeroActive:
154+
isHostScopedAgentToolActive("openclaw") &&
155+
isSystemAgentOnlyCodexDynamicToolAllowlist(runtimeParams.toolsAllow),
151156
sandboxed: sandbox?.enabled === true,
152157
});
153158
// A thread keeps the bounded agent-workspace snapshot captured at creation.

‎extensions/codex/src/app-server/run-attempt.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ async function buildCodexTurnContextForTest(
429429
sessionKey: params.sessionKey ?? params.sessionId,
430430
sessionAgentId,
431431
memoryToolNames,
432+
ringZeroActive: false,
432433
});
433434
const threadDeveloperInstructions = testing.buildDeveloperInstructions(params, { dynamicTools });
434435
const openClawPromptContext = buildCodexOpenClawPromptContext({

‎extensions/codex/src/app-server/thread-lifecycle.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,22 @@ describe("Codex ring-zero thread config", () => {
353353
it("applies the restriction to both thread start and resume", () => {
354354
const params = createAttemptParams({ provider: "openai" });
355355
params.toolsAllow = ["openclaw"];
356+
params.pluginHarnessToolPolicyRestricted = true;
356357
const appServer = createAppServerOptions() as never;
358+
const developerInstructions = "Host-authored ring-zero instructions.";
357359
const start = buildThreadStartParams(params, {
358360
appServer,
359361
cwd: "/repo",
360362
dynamicTools: [],
363+
developerInstructions,
361364
hostSystemAgentActive: true,
362365
nativeCodeModeEnabled: false,
363366
config: { project_doc_max_bytes: 64_000 },
364367
});
365368
const resume = buildThreadResumeParams(params, {
366369
appServer,
367370
dynamicTools: [],
371+
developerInstructions,
368372
hostSystemAgentActive: true,
369373
nativeCodeModeEnabled: false,
370374
threadId: "thread-1",
@@ -373,6 +377,8 @@ describe("Codex ring-zero thread config", () => {
373377

374378
expect(start.environments).toEqual([]);
375379
expect(start.baseInstructions).toBe("");
380+
expect(start.developerInstructions).toBe(developerInstructions);
381+
expect(resume.developerInstructions).toBe(developerInstructions);
376382
for (const config of [start.config, resume.config]) {
377383
expect(config?.["agents.enabled"]).toBe(false);
378384
expect(config?.["tools.experimental_request_user_input.enabled"]).toBe(false);
@@ -3528,6 +3534,7 @@ describe("Codex app-server supervised branch lifecycle", () => {
35283534
| { config?: Record<string, unknown> }
35293535
| undefined;
35303536
expect(threadRequest?.config).toMatchObject({
3537+
project_doc_max_bytes: 0,
35313538
mcp_servers: {
35323539
inherited: { enabled: false },
35333540
"request-only": { enabled: false },

‎extensions/codex/src/app-server/thread-requests.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ export function buildCodexRuntimeThreadConfigForRun(
430430
const restrictedToolSurface =
431431
ringZeroActive || messageOnlySourceReply || params.pluginHarnessToolPolicyRestricted === true;
432432
const restrictedTurnDisablesProjectDocs =
433-
messageOnlySourceReply || (params.pluginHarnessToolPolicyRestricted && params.disableTools);
433+
ringZeroActive ||
434+
messageOnlySourceReply ||
435+
(params.pluginHarnessToolPolicyRestricted && params.disableTools);
434436
const configMcpServers = config?.mcp_servers;
435437
if (restrictedToolSurface && configMcpServers !== undefined && !isJsonObject(configMcpServers)) {
436438
throw new Error("Codex restricted tool surface received invalid thread mcp_servers config");

0 commit comments

Comments
 (0)