Skip to content

Commit e9bbdec

Browse files
committed
fix(sessions): inherit agent bootstrap outside workspace
1 parent ebac731 commit e9bbdec

16 files changed

Lines changed: 156 additions & 17 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Docs: https://docs.openclaw.ai
6969

7070
### Fixes
7171

72+
- **Session workspace inheritance:** keep OpenClaw bootstrap files, agent instructions, memory guidance, and skills anchored to the configured agent workspace when a session runs from another folder or managed worktree, while preserving that folder as the execution workspace.
7273
- **Plugin setup diagnostics:** stop treating metadata-only provider setup descriptors as missing runtime registrations while retaining undeclared runtime and CLI drift warnings. Fixes #125506. Thanks @shakkernerd.
7374
- **Onboarding model browsing:** keep preferred-provider model discovery scoped to the selected provider, preserve route variants, and avoid loading unrelated provider setup surfaces. Fixes #125363. Thanks @shakkernerd.
7475
- **Codex subagent fan-out:** settle successful terminal yields immediately and preserve requester ownership so completed children reliably resume their parent.

‎docs/concepts/system-prompt.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Regenerate with `pnpm prompt:snapshots:gen`; verify drift with `pnpm prompt:snap
9191

9292
## Workspace bootstrap injection
9393

94-
Bootstrap files are resolved from the active workspace and routed to the prompt surface matching their lifetime:
94+
Bootstrap files are resolved from the configured agent workspace and routed to the prompt surface matching their lifetime. When a session runs from another folder or managed worktree, that folder remains the execution workspace while the agent's bootstrap identity and memory continue to come from its configured workspace.
9595

9696
- `AGENTS.md`
9797
- `SOUL.md`
@@ -100,7 +100,7 @@ Bootstrap files are resolved from the active workspace and routed to the prompt
100100
- `BOOTSTRAP.md` (only on brand-new workspaces)
101101
- `MEMORY.md` when present
102102

103-
On the native Codex harness, OpenClaw avoids repeating stable workspace files in every user turn. Codex loads `AGENTS.md`, including its `## Tools` section, through native project-doc discovery. `SOUL.md`, `IDENTITY.md`, and `USER.md` are forwarded as turn-scoped collaboration developer instructions so native Codex sub-agents do not inherit them. `MEMORY.md` content is not pasted into every native Codex turn either: when memory tools are available for the workspace, Codex turns get a small workspace-memory note directing the model to `memory_search` or `memory_get`. If tools are disabled, memory search is unavailable, or the active workspace differs from the agent memory workspace, `MEMORY.md` falls back to the normal bounded turn-context path. `BOOTSTRAP.md` keeps the normal turn-context role.
103+
On the native Codex harness, OpenClaw avoids repeating stable workspace files in every user turn. Codex loads project-local `AGENTS.md`, including its `## Tools` section, through native project-doc discovery. When execution uses another folder, OpenClaw also forwards the configured agent workspace's `AGENTS.md` with `SOUL.md`, `IDENTITY.md`, and `USER.md` as turn-scoped collaboration developer instructions so native Codex sub-agents do not inherit them. `MEMORY.md` content is not pasted into every native Codex turn either: when memory tools are available for the agent workspace, Codex turns get a small workspace-memory note directing the model to `memory_search` or `memory_get`. If tools are disabled or memory search is unavailable, `MEMORY.md` falls back to the normal bounded turn-context path. `BOOTSTRAP.md` keeps the normal turn-context role.
104104

105105
Heartbeat monitor scratch is not a bootstrap file. The heartbeat runner appends it only to heartbeat turns; normal turns do not receive it. The default agent's system prompt automatically includes heartbeat guidance while its cadence is enabled, with no independent heartbeat setting to hide that section.
106106

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,41 @@ describe("Codex app-server attempt context", () => {
197197
}
198198
});
199199

200+
it("inherits agent workspace instructions when Codex executes in another folder", async () => {
201+
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-agent-workspace-"));
202+
const executionDir = await fs.mkdtemp(path.join(os.tmpdir(), "codex-execution-workspace-"));
203+
await fs.writeFile(path.join(workspaceDir, "AGENTS.md"), "Canonical agent instructions");
204+
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "Canonical agent soul");
205+
await fs.writeFile(path.join(workspaceDir, "MEMORY.md"), "Canonical agent memory");
206+
207+
try {
208+
const context = await buildCodexWorkspaceBootstrapContext({
209+
params: {
210+
sessionId: "session-1",
211+
sessionKey: "agent:main:session-1",
212+
config: { agents: { defaults: { workspace: workspaceDir } } },
213+
} as EmbeddedRunAttemptParams,
214+
resolvedWorkspace: workspaceDir,
215+
executionWorkspace: executionDir,
216+
effectiveWorkspace: executionDir,
217+
sessionKey: "agent:main:session-1",
218+
sessionAgentId: "main",
219+
memoryToolNames: ["memory_search", "memory_get"],
220+
});
221+
222+
expect(context.turnScopedDeveloperInstructions).toContain("Canonical agent instructions");
223+
expect(context.turnScopedDeveloperInstructions).toContain("Canonical agent soul");
224+
expect(context.turnScopedDeveloperInstructions).toContain(
225+
path.join(workspaceDir, "AGENTS.md"),
226+
);
227+
expect(context.memoryToolRouted).toBe(true);
228+
expect(context.promptContext).not.toContain("Canonical agent memory");
229+
} finally {
230+
await fs.rm(workspaceDir, { recursive: true, force: true });
231+
await fs.rm(executionDir, { recursive: true, force: true });
232+
}
233+
});
234+
200235
it("reads and compares thread-bootstrap context-engine projections", () => {
201236
const projection = readContextEngineThreadBootstrapProjection({
202237
mode: "thread_bootstrap",

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,25 @@ export function resolveContextEngineBootstrapProjectionDecision(params: {
168168
export async function buildCodexWorkspaceBootstrapContext(params: {
169169
params: EmbeddedRunAttemptParams;
170170
resolvedWorkspace: string;
171+
executionWorkspace?: string;
171172
effectiveWorkspace: string;
172173
sessionKey: string;
173174
sessionAgentId: string;
174175
memoryToolNames: readonly string[];
175176
sandboxed?: boolean;
176177
}): Promise<CodexWorkspaceBootstrapContext> {
177178
try {
179+
const executionWorkspace = params.executionWorkspace ?? params.resolvedWorkspace;
180+
const inheritsAgentWorkspace = executionWorkspace !== params.resolvedWorkspace;
181+
const promptWorkspace = inheritsAgentWorkspace
182+
? params.resolvedWorkspace
183+
: params.effectiveWorkspace;
178184
const memoryToolsAvailable =
179185
params.memoryToolNames.length > 0 &&
180186
canRouteCodexWorkspaceMemoryThroughTools({
181187
config: params.params.config,
182188
agentId: params.params.agentId ?? params.sessionAgentId,
183-
workspaceDir: params.effectiveWorkspace,
189+
workspaceDir: inheritsAgentWorkspace ? params.resolvedWorkspace : params.effectiveWorkspace,
184190
});
185191
// Native Codex turns should read workspace MEMORY.md through tools when
186192
// possible; pasting it into every prompt turns durable memory into policy.
@@ -205,7 +211,7 @@ export async function buildCodexWorkspaceBootstrapContext(params: {
205211
remapCodexContextFilePath({
206212
file: toCodexEmbeddedContextFile(file),
207213
sourceWorkspaceDir: params.resolvedWorkspace,
208-
targetWorkspaceDir: params.effectiveWorkspace,
214+
targetWorkspaceDir: promptWorkspace,
209215
}),
210216
);
211217
const contextFiles = buildBootstrapContextForFiles(
@@ -227,7 +233,7 @@ export async function buildCodexWorkspaceBootstrapContext(params: {
227233
remapCodexContextFilePath({
228234
file,
229235
sourceWorkspaceDir: params.resolvedWorkspace,
230-
targetWorkspaceDir: params.effectiveWorkspace,
236+
targetWorkspaceDir: promptWorkspace,
231237
}),
232238
);
233239
const promptContextFiles = selectCodexWorkspacePromptContextFiles(contextFiles, {
@@ -237,7 +243,9 @@ export async function buildCodexWorkspaceBootstrapContext(params: {
237243
const turnScopedDeveloperInstructionFiles = shouldInjectCodexOpenClawPromptContext(
238244
params.params,
239245
)
240-
? selectCodexWorkspaceTurnScopedDeveloperInstructionFiles(contextFiles)
246+
? selectCodexWorkspaceTurnScopedDeveloperInstructionFiles(contextFiles, {
247+
includeAgentProjectDocs: inheritsAgentWorkspace,
248+
})
241249
: [];
242250
return {
243251
bootstrapFiles,
@@ -675,7 +683,7 @@ function renderCodexWorkspaceBootstrapPromptContext(
675683
return undefined;
676684
}
677685
const lines = [
678-
"OpenClaw loaded these user-editable workspace files for the current turn. Codex loads AGENTS.md natively. SOUL.md, IDENTITY.md, and USER.md are provided as turn-scoped collaboration instructions so native Codex subagents do not inherit them. Those files are not repeated here.",
686+
"OpenClaw loaded these user-editable workspace files for the current turn. Codex loads project-local AGENTS.md natively. When execution uses another folder, the agent workspace AGENTS.md is inherited with SOUL.md, IDENTITY.md, and USER.md as turn-scoped collaboration instructions. Those files are not repeated here.",
679687
"",
680688
"# Project Context",
681689
"",
@@ -713,10 +721,16 @@ function selectCodexWorkspacePromptContextFiles(
713721

714722
function selectCodexWorkspaceTurnScopedDeveloperInstructionFiles(
715723
contextFiles: EmbeddedContextFile[],
724+
options: { includeAgentProjectDocs?: boolean } = {},
716725
): EmbeddedContextFile[] {
717726
return selectCodexWorkspaceDeveloperInstructionFiles(
718727
contextFiles,
719-
CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES,
728+
options.includeAgentProjectDocs
729+
? new Set([
730+
...CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES,
731+
...CODEX_NATIVE_PROJECT_DOC_BASENAMES,
732+
])
733+
: CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES,
720734
);
721735
}
722736

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export async function prepareCodexAttemptContext(
141141
const memoryToolNames = getCodexWorkspaceMemoryToolNames(toolBridge.availableSpecs);
142142
const workspaceBootstrapContext = await buildCodexWorkspaceBootstrapContext({
143143
params: runtimeParams,
144-
resolvedWorkspace,
144+
resolvedWorkspace: runtimeParams.bootstrapWorkspaceDir ?? resolvedWorkspace,
145+
executionWorkspace: resolvedWorkspace,
145146
effectiveWorkspace,
146147
sessionKey: contextSessionKey,
147148
sessionAgentId,

‎src/agents/embedded-agent-runner/run-orchestrator.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ async function runEmbeddedAgentInternal(
421421
agentDir,
422422
workspaceResolution,
423423
workspaceDir: resolvedWorkspace,
424+
bootstrapWorkspaceDir: canonicalWorkspace,
424425
isCanonicalWorkspace,
425426
globalLane,
426427
hookRunner,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import fs from "node:fs/promises";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { prepareEmbeddedAttemptBootstrap } from "./attempt-bootstrap-prepare.js";
6+
import type { EmbeddedRunAttemptParams } from "./types.js";
7+
8+
const tempDirs: string[] = [];
9+
10+
afterEach(async () => {
11+
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
12+
});
13+
14+
describe("prepareEmbeddedAttemptBootstrap", () => {
15+
it("inherits bootstrap files from the canonical agent workspace for external sessions", async () => {
16+
const agentWorkspace = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-workspace-"));
17+
const sessionWorkspace = await fs.mkdtemp(
18+
path.join(os.tmpdir(), "openclaw-session-workspace-"),
19+
);
20+
tempDirs.push(agentWorkspace, sessionWorkspace);
21+
await fs.writeFile(path.join(agentWorkspace, "SOUL.md"), "Canonical agent soul");
22+
23+
const result = await prepareEmbeddedAttemptBootstrap({
24+
attempt: {
25+
sessionId: "session-1",
26+
sessionKey: "agent:main:session-1",
27+
trigger: "user",
28+
isCanonicalWorkspace: false,
29+
config: { agents: { defaults: { workspace: agentWorkspace } } },
30+
} as EmbeddedRunAttemptParams,
31+
bootstrapWorkspaceDir: agentWorkspace,
32+
effectiveWorkspace: sessionWorkspace,
33+
hasReadTool: true,
34+
isRawModelRun: false,
35+
markStage: () => undefined,
36+
resolvedWorkspace: sessionWorkspace,
37+
sessionAgentId: "main",
38+
sessionLabel: "agent:main:session-1",
39+
});
40+
41+
expect(result.contextFiles).toEqual(
42+
expect.arrayContaining([
43+
expect.objectContaining({
44+
path: path.join(agentWorkspace, "SOUL.md"),
45+
content: "Canonical agent soul",
46+
}),
47+
]),
48+
);
49+
});
50+
});

‎src/agents/embedded-agent-runner/run/attempt-bootstrap-prepare.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { EmbeddedRunAttemptParams } from "./types.js";
2424

2525
export async function prepareEmbeddedAttemptBootstrap(params: {
2626
attempt: EmbeddedRunAttemptParams;
27+
bootstrapWorkspaceDir?: string;
2728
effectiveWorkspace: string;
2829
hasReadTool: boolean;
2930
isRawModelRun: boolean;
@@ -33,12 +34,19 @@ export async function prepareEmbeddedAttemptBootstrap(params: {
3334
sessionLabel: string;
3435
}) {
3536
const { attempt } = params;
37+
const bootstrapWorkspaceDir = params.bootstrapWorkspaceDir ?? params.resolvedWorkspace;
38+
// The selected session workspace owns execution. Bootstrap files remain owned
39+
// by the configured agent workspace and keep honest paths when those differ.
40+
const bootstrapPromptWorkspaceDir =
41+
bootstrapWorkspaceDir === params.resolvedWorkspace
42+
? params.effectiveWorkspace
43+
: bootstrapWorkspaceDir;
3644
const suppressAmbientContext =
3745
params.isRawModelRun || attempt.operation === "settled-tool-finalization";
3846
const contextInjectionMode = resolveContextInjectionMode(attempt.config, params.sessionAgentId);
3947
const bootstrapWarn = makeBootstrapWarn({
4048
sessionLabel: params.sessionLabel,
41-
workspaceDir: params.resolvedWorkspace,
49+
workspaceDir: bootstrapWorkspaceDir,
4250
warn: (message) => log.warn(message),
4351
});
4452
let completedBootstrapTurn: boolean | undefined;
@@ -56,7 +64,7 @@ export async function prepareEmbeddedAttemptBootstrap(params: {
5664
isPrimaryRun: isPrimaryBootstrapRun(attempt.sessionKey),
5765
isCanonicalWorkspace: attempt.isCanonicalWorkspace,
5866
effectiveWorkspace: params.effectiveWorkspace,
59-
resolvedWorkspace: params.resolvedWorkspace,
67+
resolvedWorkspace: bootstrapWorkspaceDir,
6068
hasBootstrapFileAccess: params.hasReadTool,
6169
});
6270
const shouldProbeContinuationSkip =
@@ -75,7 +83,7 @@ export async function prepareEmbeddedAttemptBootstrap(params: {
7583
(bootstrapRouting === undefined || bootstrapRouting.bootstrapMode === "full")
7684
) {
7785
preloadedBootstrapFiles = await resolveBootstrapFilesForRun({
78-
workspaceDir: params.resolvedWorkspace,
86+
workspaceDir: bootstrapWorkspaceDir,
7987
config: attempt.config,
8088
sessionKey: attempt.sessionKey,
8189
sessionId: attempt.sessionId,
@@ -105,7 +113,7 @@ export async function prepareEmbeddedAttemptBootstrap(params: {
105113
const bootstrapFiles =
106114
preloadedBootstrapFiles ??
107115
(await resolveBootstrapFilesForRun({
108-
workspaceDir: params.resolvedWorkspace,
116+
workspaceDir: bootstrapWorkspaceDir,
109117
config: attempt.config,
110118
sessionKey: attempt.sessionKey,
111119
sessionId: attempt.sessionId,
@@ -128,8 +136,8 @@ export async function prepareEmbeddedAttemptBootstrap(params: {
128136
params.markStage("bootstrap-context");
129137
const remappedContextFiles = remapInjectedContextFilesToWorkspace({
130138
files: resolvedContextFiles,
131-
sourceWorkspaceDir: params.resolvedWorkspace,
132-
targetWorkspaceDir: params.effectiveWorkspace,
139+
sourceWorkspaceDir: bootstrapWorkspaceDir,
140+
targetWorkspaceDir: bootstrapPromptWorkspaceDir,
133141
});
134142
const contextFiles = bootstrapRouting.includeBootstrapInSystemContext
135143
? remappedContextFiles

‎src/agents/embedded-agent-runner/run/attempt-dispatch-preparation.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function prepareAndDispatchEmbeddedRunAttempt(input: {
5858
const {
5959
workspaceResolution,
6060
workspaceDir,
61+
bootstrapWorkspaceDir,
6162
isCanonicalWorkspace,
6263
agentDir,
6364
resolvedSessionKey,
@@ -212,6 +213,7 @@ export async function prepareAndDispatchEmbeddedRunAttempt(input: {
212213
sessionKey: resolvedSessionKey,
213214
trajectoryRecorder: trajectoryRecorder ?? undefined,
214215
workspaceDir,
216+
bootstrapWorkspaceDir,
215217
isCanonicalWorkspace,
216218
agentDir,
217219
preparedModelRuntime: runInput.preparedModelRuntime,

‎src/agents/embedded-agent-runner/run/attempt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export async function runEmbeddedAttempt(
246246
() =>
247247
prepareEmbeddedAttemptBootstrap({
248248
attempt: params,
249+
bootstrapWorkspaceDir: params.bootstrapWorkspaceDir,
249250
effectiveWorkspace,
250251
hasReadTool: toolsEnabled && toolsRaw.some((tool) => tool.name === "read"),
251252
isRawModelRun,

0 commit comments

Comments
 (0)