Skip to content

Commit 0407fc0

Browse files
jalehmanclaude
andcommitted
fix(agents): drift first-only CLI sessions when truncation state changes
Fold bootstrap truncation-notice presence into the CLI session reuse fingerprint, mirroring the existing bootstrapMode hashing. Without this, a systemPromptWhen:"first" backend resuming an existing session never receives the updated system prompt when an AGENTS.md edit crosses the truncation threshold mid-session — a path the removed per-turn body append used to cover. Two-turn regression proves the drift resend. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 49bf3d7 commit 0407fc0

2 files changed

Lines changed: 82 additions & 7 deletions

File tree

‎src/agents/cli-runner/prepare.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,75 @@ describe("prepareCliRunContext", () => {
18111811
});
18121812
});
18131813

1814+
it("drifts a resumed first-only session when truncation starts mid-session", async () => {
1815+
const { dir } = fixture.session;
1816+
const agentsPath = path.join(dir, "AGENTS.md");
1817+
const bootstrapContextFor = (injected: string) => ({
1818+
bootstrapFiles: [
1819+
{
1820+
name: "AGENTS.md" as const,
1821+
path: agentsPath,
1822+
content: "policy ".repeat(100),
1823+
missing: false,
1824+
},
1825+
],
1826+
contextFiles: [{ path: agentsPath, content: injected }],
1827+
});
1828+
setRawCliBackendForPrepareTest({
1829+
id: "test-cli",
1830+
pluginId: "test",
1831+
bundleMcp: false,
1832+
nativeToolMode: "always-on",
1833+
config: {
1834+
command: "test-cli",
1835+
args: ["--print"],
1836+
systemPromptArg: "--system-prompt",
1837+
systemPromptWhen: "first",
1838+
sessionMode: "existing",
1839+
output: "text",
1840+
input: "arg",
1841+
},
1842+
});
1843+
setCliRunnerPrepareTestDeps({
1844+
resolveBootstrapContextForRun: vi.fn(async () => bootstrapContextFor("policy ".repeat(100))),
1845+
});
1846+
const firstTurn = await fixture.prepare({
1847+
sessionKey: "agent:main:main",
1848+
config: createCliBackendConfig(),
1849+
prompt: "turn one",
1850+
runId: "run-truncation-drift-1",
1851+
trigger: "user",
1852+
extraSystemPrompt: "stable prompt",
1853+
});
1854+
expect(firstTurn.systemPrompt).not.toContain("[Bootstrap truncation warning]");
1855+
const untruncatedBinding = {
1856+
sessionId: "cli-session",
1857+
extraSystemPromptHash: firstTurn.extraSystemPromptHash,
1858+
cwdHash: hashCliSessionText(dir),
1859+
};
1860+
1861+
// AGENTS.md grows past the bootstrap cap between turns of the same session.
1862+
setCliRunnerPrepareTestDeps({
1863+
resolveBootstrapContextForRun: vi.fn(async () => bootstrapContextFor("policy ".repeat(10))),
1864+
});
1865+
const secondTurn = await fixture.prepare({
1866+
sessionKey: "agent:main:main",
1867+
config: createCliBackendConfig(),
1868+
prompt: "turn two",
1869+
runId: "run-truncation-drift-2",
1870+
trigger: "user",
1871+
extraSystemPrompt: "stable prompt",
1872+
cliSessionBinding: untruncatedBinding,
1873+
});
1874+
1875+
expect(secondTurn.systemPrompt).toContain("[Bootstrap truncation warning]");
1876+
expect(secondTurn.reusableCliSession).toEqual({
1877+
mode: "reuse-with-drift",
1878+
sessionId: "cli-session",
1879+
drift: { reasons: ["system-prompt"] },
1880+
});
1881+
});
1882+
18141883
it("applies prompt-build hook context to Claude-style CLI preparation", async () => {
18151884
const { dir } = fixture.session;
18161885
fixture.appendTranscript({

‎src/agents/cli-runner/prepare.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@ export async function prepareCliRunContext(
966966
seenSignatures: params.bootstrapPromptWarningSignaturesSeen,
967967
previousSignature: params.bootstrapPromptWarningSignature,
968968
});
969+
const bootstrapTruncationNotice = buildBootstrapPromptWarningNotice(bootstrapPromptWarning.lines);
969970
// Ring-zero OpenClaw runs replace the bundle MCP surface entirely: no
970971
// loopback server, no plugin/user servers. A selectable backend also removes
971972
// its native tools, leaving only this openclaw stdio server.
@@ -1157,12 +1158,19 @@ export async function prepareCliRunContext(
11571158
]),
11581159
)
11591160
: baseExtraSystemPromptHash;
1160-
// Bootstrap guidance changes resumable system context. Hash the pending mode
1161-
// so entering or leaving bootstrap refreshes first-only CLI system prompts.
1161+
// Bootstrap guidance and truncation notices change resumable system context.
1162+
// Hash both so entering or leaving either state refreshes first-only CLI
1163+
// system prompts.
11621164
const extraSystemPromptHash =
1163-
bootstrapMode === "none"
1165+
bootstrapMode === "none" && bootstrapTruncationNotice === undefined
11641166
? toolBoundExtraSystemPromptHash
1165-
: hashCliSessionText(JSON.stringify([toolBoundExtraSystemPromptHash ?? null, bootstrapMode]));
1167+
: hashCliSessionText(
1168+
JSON.stringify([
1169+
toolBoundExtraSystemPromptHash ?? null,
1170+
bootstrapMode,
1171+
bootstrapTruncationNotice !== undefined,
1172+
]),
1173+
);
11661174
let cleanupPreparedResources: (() => Promise<void>) | undefined;
11671175
let preparedExecution: PrivateCliBackendPreparedExecution | undefined;
11681176
try {
@@ -1612,9 +1620,7 @@ export async function prepareCliRunContext(
16121620
tools: promptTools,
16131621
contextFiles,
16141622
bootstrapMode,
1615-
bootstrapTruncationNotice: buildBootstrapPromptWarningNotice(
1616-
bootstrapPromptWarning.lines,
1617-
),
1623+
bootstrapTruncationNotice,
16181624
modelDisplay,
16191625
agentId: sessionAgentId,
16201626
sessionKey: params.sessionKey,

0 commit comments

Comments
 (0)