Skip to content

Commit e330e4a

Browse files
authored
refactor(agents): privatize system prompt helpers (#106965)
1 parent b9c4815 commit e330e4a

3 files changed

Lines changed: 80 additions & 100 deletions

File tree

‎scripts/deadcode-exports.baseline.mjs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,6 @@ export const KNIP_UNUSED_EXPORT_BASELINE = [
770770
"src/agents/subagent-registry.ts: testing",
771771
"src/agents/subagent-spawn.ts: testing",
772772
"src/agents/system-prompt-config.ts: resolveAgentSystemPromptConfig",
773-
"src/agents/system-prompt.ts: buildAgentBootstrapSystemContext",
774-
"src/agents/system-prompt.ts: buildAgentBootstrapSystemPromptSections",
775-
"src/agents/system-prompt.ts: buildRuntimeLine",
776773
"src/agents/tool-call-id.ts: isValidCloudCodeAssistToolId",
777774
"src/agents/tool-call-id.ts: sanitizeToolCallId",
778775
"src/agents/tool-loop-detection.ts: CRITICAL_THRESHOLD",

‎src/agents/system-prompt.test.ts‎

Lines changed: 77 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import { listDeliverableMessageChannels } from "../utils/message-channel.js";
88
import { resolveAgentPromptSurfaceForSessionKey } from "./prompt-surface.js";
99
import { buildSkillWorkshopPromptSection } from "./skill-workshop-prompt.js";
1010
import { buildSubagentSystemPrompt } from "./subagent-system-prompt.js";
11-
import {
12-
buildAgentBootstrapSystemContext,
13-
buildAgentBootstrapSystemPromptSections,
14-
buildAgentSystemPrompt,
15-
buildRuntimeLine,
16-
} from "./system-prompt.js";
11+
import { buildAgentSystemPrompt } from "./system-prompt.js";
1712

1813
describe("buildAgentSystemPrompt", () => {
1914
it("resolves helper session keys to scoped prompt surfaces", () => {
@@ -603,9 +598,39 @@ describe("buildAgentSystemPrompt", () => {
603598

604599
expect(prompt).toContain("## Bootstrap Pending");
605600
expect(prompt).toContain("BOOTSTRAP.md below; follow before normal reply.");
601+
expect(prompt).toContain("Can finish BOOTSTRAP.md here: do it.");
602+
expect(prompt).toContain("brief blocker");
603+
expect(prompt).toContain("simplest next step");
604+
expect(prompt).toContain("Never claim completion early");
606605
expect(prompt).toContain("First visible reply must follow BOOTSTRAP.md");
607606
expect(prompt).toContain("## /tmp/openclaw/BOOTSTRAP.md");
608607
expect(prompt).toContain("Ask who I am.");
608+
expect(prompt.match(/## \/tmp\/openclaw\/BOOTSTRAP\.md/g)).toHaveLength(1);
609+
expect(prompt.match(/Ask who I am\./g)).toHaveLength(1);
610+
});
611+
612+
it("uses limited bootstrap wording for constrained user-facing runs", () => {
613+
const prompt = buildAgentSystemPrompt({
614+
workspaceDir: "/tmp/openclaw",
615+
bootstrapMode: "limited",
616+
});
617+
618+
expect(prompt).toContain("## Bootstrap Pending");
619+
expect(prompt).toContain("cannot safely finish full BOOTSTRAP.md");
620+
expect(prompt).toContain("Never claim complete");
621+
expect(prompt).toContain("no generic first greeting");
622+
expect(prompt).toContain("primary interactive run with normal workspace access");
623+
});
624+
625+
it("omits bootstrap instructions when bootstrap is not pending", () => {
626+
for (const bootstrapMode of ["none", undefined] as const) {
627+
const prompt = buildAgentSystemPrompt({
628+
workspaceDir: "/tmp/openclaw",
629+
...(bootstrapMode ? { bootstrapMode } : {}),
630+
});
631+
632+
expect(prompt).not.toContain("## Bootstrap Pending");
633+
}
609634
});
610635

611636
it("includes bootstrap truncation notice in system prompt without raw diagnostics", () => {
@@ -1316,8 +1341,9 @@ describe("buildAgentSystemPrompt", () => {
13161341
});
13171342

13181343
it("builds runtime line with agent and channel details", () => {
1319-
const line = buildRuntimeLine(
1320-
{
1344+
const prompt = buildAgentSystemPrompt({
1345+
workspaceDir: "/tmp/openclaw",
1346+
runtimeInfo: {
13211347
agentId: "work",
13221348
sessionKey: "agent:main:subagent:runtime-check",
13231349
sessionId: "23ae7fce-3c27-4a51-b58e-d800d8ca091f",
@@ -1329,59 +1355,65 @@ describe("buildAgentSystemPrompt", () => {
13291355
model: "anthropic/claude",
13301356
defaultModel: "anthropic/claude-opus-4-5",
13311357
activeNode: "mac-123",
1358+
channel: "telegram",
1359+
capabilities: ["inlineButtons"],
13321360
},
1333-
"telegram",
1334-
["inlineButtons"],
1335-
"low",
1336-
);
1361+
defaultThinkLevel: "low",
1362+
});
13371363

1338-
expect(line).toContain("agent=work");
1339-
expect(line).toContain("session=agent:main:subagent:runtime-check");
1340-
expect(line).toContain("sessionId=23ae7fce-3c27-4a51-b58e-d800d8ca091f");
1341-
expect(line).toContain("host=host");
1342-
expect(line).toContain("repo=/repo");
1343-
expect(line).toContain("os=macOS (arm64)");
1344-
expect(line).toContain("node=v20");
1345-
expect(line).toContain("model=anthropic/claude");
1346-
expect(line).toContain("default_model=anthropic/claude-opus-4-5");
1347-
expect(line).toContain("active_node=mac-123");
1348-
expect(line).toContain("channel=telegram");
1349-
expect(line).toContain("capabilities=inlinebuttons");
1350-
expect(line).toContain("thinking=low");
1364+
expect(prompt).toContain("agent=work");
1365+
expect(prompt).toContain("session=agent:main:subagent:runtime-check");
1366+
expect(prompt).toContain("sessionId=23ae7fce-3c27-4a51-b58e-d800d8ca091f");
1367+
expect(prompt).toContain("host=host");
1368+
expect(prompt).toContain("repo=/repo");
1369+
expect(prompt).toContain("os=macOS (arm64)");
1370+
expect(prompt).toContain("node=v20");
1371+
expect(prompt).toContain("model=anthropic/claude");
1372+
expect(prompt).toContain("default_model=anthropic/claude-opus-4-5");
1373+
expect(prompt).toContain("active_node=mac-123");
1374+
expect(prompt).toContain("channel=telegram");
1375+
expect(prompt).toContain("capabilities=inlinebuttons");
1376+
expect(prompt).toContain("thinking=low");
13511377
});
13521378

13531379
it("keeps the runtime line cache-stable across isolated cron runs", () => {
13541380
// Isolated cron run-scoped keys carry a fresh per-run id every run (forceNew). Rendering it
13551381
// verbatim re-busts byte-exact prefix caching for the tool catalog after it (#96677 / #43148).
13561382
const buildForRun = (runId: string) =>
1357-
buildRuntimeLine({
1358-
agentId: "work",
1359-
sessionKey: `agent:work:cron:nightly-job:run:${runId}`,
1360-
sessionId: runId,
1361-
host: "host",
1362-
os: "linux",
1383+
buildAgentSystemPrompt({
1384+
workspaceDir: "/tmp/openclaw",
1385+
runtimeInfo: {
1386+
agentId: "work",
1387+
sessionKey: `agent:work:cron:nightly-job:run:${runId}`,
1388+
sessionId: runId,
1389+
host: "host",
1390+
os: "linux",
1391+
},
13631392
});
1364-
const lineA = buildForRun("11111111-1111-1111-1111-111111111111");
1365-
const lineB = buildForRun("22222222-2222-2222-2222-222222222222");
1393+
const promptA = buildForRun("11111111-1111-1111-1111-111111111111");
1394+
const promptB = buildForRun("22222222-2222-2222-2222-222222222222");
13661395

1367-
expect(lineA).toContain("session=agent:work:cron:nightly-job");
1368-
expect(lineA).not.toContain(":run:");
1369-
expect(lineA).not.toContain("sessionId=");
1396+
expect(promptA).toContain("session=agent:work:cron:nightly-job");
1397+
expect(promptA).not.toContain(":run:");
1398+
expect(promptA).not.toContain("sessionId=");
13701399
// Two runs of the same job render identical bytes, so the cached prefix is reused.
1371-
expect(lineA).toBe(lineB);
1400+
expect(promptA).toBe(promptB);
13721401
});
13731402

13741403
it("preserves a stable session id that is not the run-scope id", () => {
1375-
const line = buildRuntimeLine({
1376-
agentId: "work",
1377-
sessionKey: "agent:work:cron:nightly-job:run:run-id",
1378-
sessionId: "stable-session-id",
1379-
host: "host",
1380-
os: "linux",
1404+
const prompt = buildAgentSystemPrompt({
1405+
workspaceDir: "/tmp/openclaw",
1406+
runtimeInfo: {
1407+
agentId: "work",
1408+
sessionKey: "agent:work:cron:nightly-job:run:run-id",
1409+
sessionId: "stable-session-id",
1410+
host: "host",
1411+
os: "linux",
1412+
},
13811413
});
13821414

1383-
expect(line).toContain("session=agent:work:cron:nightly-job");
1384-
expect(line).toContain("sessionId=stable-session-id");
1415+
expect(prompt).toContain("session=agent:work:cron:nightly-job");
1416+
expect(prompt).toContain("sessionId=stable-session-id");
13851417
});
13861418

13871419
it("renders extra system prompt exactly once", () => {
@@ -1523,55 +1555,6 @@ describe("buildAgentSystemPrompt", () => {
15231555
});
15241556
});
15251557

1526-
describe("buildAgentBootstrapSystemContext", () => {
1527-
it("uses friendly full bootstrap wording that is truthful about completion blockers", () => {
1528-
const prompt = buildAgentBootstrapSystemContext({
1529-
bootstrapMode: "full",
1530-
hasBootstrapFileInProjectContext: true,
1531-
}).join("\n");
1532-
1533-
expect(prompt).toContain("## Bootstrap Pending");
1534-
expect(prompt).toContain("BOOTSTRAP.md below; follow before normal reply.");
1535-
expect(prompt).toContain("Can finish BOOTSTRAP.md here: do it.");
1536-
expect(prompt).toContain("brief blocker");
1537-
expect(prompt).toContain("simplest next step");
1538-
expect(prompt).toContain("Never claim completion early");
1539-
expect(prompt).toContain("First visible reply must follow BOOTSTRAP.md");
1540-
});
1541-
1542-
it("uses limited bootstrap wording for constrained user-facing runs", () => {
1543-
const prompt = buildAgentBootstrapSystemContext({ bootstrapMode: "limited" }).join("\n");
1544-
1545-
expect(prompt).toContain("## Bootstrap Pending");
1546-
expect(prompt).toContain("cannot safely finish full BOOTSTRAP.md");
1547-
expect(prompt).toContain("Never claim complete");
1548-
expect(prompt).toContain("no generic first greeting");
1549-
expect(prompt).toContain("primary interactive run with normal workspace access");
1550-
});
1551-
1552-
it("returns nothing when bootstrap is not pending", () => {
1553-
expect(buildAgentBootstrapSystemContext({ bootstrapMode: "none" })).toStrictEqual([]);
1554-
expect(buildAgentBootstrapSystemContext({})).toStrictEqual([]);
1555-
});
1556-
});
1557-
1558-
describe("buildAgentBootstrapSystemPromptSections", () => {
1559-
it("can render bootstrap guidance without duplicating Project Context", () => {
1560-
const sections = buildAgentBootstrapSystemPromptSections({
1561-
bootstrapMode: "full",
1562-
bootstrapTruncationNotice: "Bootstrap context was truncated.",
1563-
contextFiles: [{ path: "/tmp/openclaw/BOOTSTRAP.md", content: "Ask who I am." }],
1564-
}).join("\n");
1565-
1566-
expect(sections).toContain("## Bootstrap Pending");
1567-
expect(sections).toContain("BOOTSTRAP.md below; follow before normal reply.");
1568-
expect(sections).toContain("## Bootstrap Context Notice");
1569-
expect(sections).toContain("Bootstrap context was truncated.");
1570-
expect(sections).not.toContain("## /tmp/openclaw/BOOTSTRAP.md");
1571-
expect(sections).not.toContain("Ask who I am.");
1572-
});
1573-
});
1574-
15751558
describe("buildSubagentSystemPrompt", () => {
15761559
it("renders depth-1 orchestrator guidance, labels, and recovery notes", () => {
15771560
const prompt = buildSubagentSystemPrompt({

‎src/agents/system-prompt.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ function buildMemorySection(params: {
319319
});
320320
}
321321

322-
export function buildAgentBootstrapSystemContext(params: {
322+
function buildAgentBootstrapSystemContext(params: {
323323
bootstrapMode?: BootstrapMode;
324324
hasBootstrapFileInProjectContext?: boolean;
325325
}): string[] {
@@ -349,7 +349,7 @@ export function buildAgentBootstrapSystemContext(params: {
349349
];
350350
}
351351

352-
export function buildAgentBootstrapSystemPromptSections(params: {
352+
function buildAgentBootstrapSystemPromptSections(params: {
353353
bootstrapMode?: BootstrapMode;
354354
bootstrapTruncationNotice?: string;
355355
contextFiles?: EmbeddedContextFile[];
@@ -1384,7 +1384,7 @@ function buildActiveProcessSessionReferenceLines(
13841384
];
13851385
}
13861386

1387-
export function buildRuntimeLine(
1387+
function buildRuntimeLine(
13881388
runtimeInfo?: {
13891389
agentId?: string;
13901390
sessionKey?: string;

0 commit comments

Comments
 (0)