Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(system-prompt): add bootstrap budget control for workspace and skills

Jiayuan Zhangc3433871a668
diff --git a/packages/core/src/agent/system-prompt/budget-integration.test.ts b/packages/core/src/agent/system-prompt/budget-integration.test.ts
new file mode 100644
index 0000000000..52d1a1bc2e
--- /dev/null
+++ b/packages/core/src/agent/system-prompt/budget-integration.test.ts
@@ -0,0 +1,211 @@
+/**
+ * E2E Integration Test: Phase 3 + Phase 4 — Budget Control + Report Enhancement
+ *
+ * Tests the full system prompt build flow with budget-controlled sections
+ * and enhanced report diagnostics.
+ */
+import { describe, it, expect } from "vitest";
+import { buildSystemPrompt, buildSystemPromptWithReport } from "./builder.js";
+import { formatPromptReport } from "./report.js";
+import {
+ DEFAULT_BOOTSTRAP_MAX_CHARS,
+ DEFAULT_SKILLS_MAX_CHARS,
+ truncateWithBudget,
+} from "./sections.js";
+
+describe("Phase 3 E2E: Budget Control in Full Prompt Build", () => {
+ it("truncates oversized workspace.md in full prompt", () => {
+ const oversizedWorkspace = "HEADER\n" + "X".repeat(30_000) + "\nFOOTER";
+ const prompt = buildSystemPrompt({
+ mode: "full",
+ profile: { workspace: oversizedWorkspace },
+ });
+
+ // Prompt should be shorter than raw inclusion
+ expect(prompt.length).toBeLessThan(oversizedWorkspace.length + 5000); // allow for other sections
+ // Truncation marker should be present
+ expect(prompt).toContain("characters omitted");
+ // Head content preserved
+ expect(prompt).toContain("HEADER");
+ // Tail content preserved
+ expect(prompt).toContain("FOOTER");
+ });
+
+ it("truncates oversized skills prompt in full prompt", () => {
+ const oversizedSkills = "## myskill\n" + "Z".repeat(20_000) + "\nSKILL_END";
+ const prompt = buildSystemPrompt({
+ mode: "full",
+ skillsPrompt: oversizedSkills,
+ });
+
+ expect(prompt).toContain("## Skills (mandatory)");
+ expect(prompt).toContain("characters omitted");
+ expect(prompt).toContain("## myskill");
+ expect(prompt).toContain("SKILL_END");
+ });
+
+ it("truncates both workspace AND skills independently", () => {
+ const bigWorkspace = "WS_HEAD\n" + "A".repeat(25_000) + "\nWS_TAIL";
+ const bigSkills = "## sk\n" + "B".repeat(15_000) + "\nSK_TAIL";
+
+ const prompt = buildSystemPrompt({
+ mode: "full",
+ profile: { workspace: bigWorkspace },
+ skillsPrompt: bigSkills,
+ });
+
+ // Both sections should be truncated
+ const markers = (prompt.match(/characters omitted/g) ?? []).length;
+ expect(markers).toBe(2);
+
+ // Both heads and tails preserved
+ expect(prompt).toContain("WS_HEAD");
+ expect(prompt).toContain("WS_TAIL");
+ expect(prompt).toContain("## sk");
+ expect(prompt).toContain("SK_TAIL");
+ });
+
+ it("does NOT truncate small workspace and skills", () => {
+ const smallWorkspace = "Small workspace rules.";
+ const smallSkills = "## commit\nDo conventional commits.";
+
+ const prompt = buildSystemPrompt({
+ mode: "full",
+ profile: { workspace: smallWorkspace },
+ skillsPrompt: smallSkills,
+ });
+
+ expect(prompt).not.toContain("characters omitted");
+ expect(prompt).toContain("Small workspace rules.");
+ expect(prompt).toContain("Do conventional commits.");
+ });
+
+ it("does not affect minimal or none modes", () => {
+ const bigWorkspace = "W".repeat(30_000);
+ const bigSkills = "## s\n" + "S".repeat(20_000);
+
+ // Minimal mode excludes workspace and skills
+ const minimal = buildSystemPrompt({
+ mode: "minimal",
+ profile: { workspace: bigWorkspace },
+ skillsPrompt: bigSkills,
+ });
+ expect(minimal).not.toContain("characters omitted");
+
+ // None mode excludes workspace and skills
+ const none = buildSystemPrompt({
+ mode: "none",
+ profile: { workspace: bigWorkspace },
+ skillsPrompt: bigSkills,
+ });
+ expect(none).not.toContain("characters omitted");
+ });
+});
+
+describe("Phase 4 E2E: Report Enhancement", () => {
+ it("report includes truncation metadata for oversized workspace", () => {
+ const oversized = "X".repeat(25_000);
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: oversized },
+ });
+
+ const ws = report.sections.find((s) => s.name === "workspace")!;
+ expect(ws.included).toBe(true);
+ expect(ws.truncated).toBe(true);
+ expect(ws.originalChars).toBe(25_000);
+ expect(ws.chars).toBeLessThan(25_000);
+ });
+
+ it("report includes truncation metadata for oversized skills", () => {
+ const oversized = "## sk\n" + "Y".repeat(15_000);
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ skillsPrompt: oversized,
+ });
+
+ const sk = report.sections.find((s) => s.name === "skills")!;
+ expect(sk.included).toBe(true);
+ expect(sk.truncated).toBe(true);
+ expect(sk.originalChars).toBe(oversized.trim().length);
+ });
+
+ it("report does NOT flag truncation for small content", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "Small" },
+ skillsPrompt: "## commit\nOK",
+ });
+
+ const ws = report.sections.find((s) => s.name === "workspace")!;
+ expect(ws.truncated).toBeUndefined();
+ expect(ws.originalChars).toBeUndefined();
+
+ const sk = report.sections.find((s) => s.name === "skills")!;
+ expect(sk.truncated).toBeUndefined();
+ });
+
+ it("formatPromptReport includes token estimate", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ tools: ["read", "write", "exec"],
+ });
+
+ const formatted = formatPromptReport(report);
+ const expectedTokens = Math.ceil(report.totalChars / 4);
+ expect(formatted).toContain(`~${expectedTokens} tokens`);
+ });
+
+ it("formatPromptReport shows truncation detail", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "X".repeat(25_000) },
+ });
+
+ const formatted = formatPromptReport(report);
+ expect(formatted).toContain("truncated from 25000 chars");
+ });
+
+ it("formatPromptReport does NOT show truncation for small sections", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "Small" },
+ });
+
+ const formatted = formatPromptReport(report);
+ expect(formatted).not.toContain("truncated from");
+ });
+});
+
+describe("truncateWithBudget edge cases", () => {
+ it("empty string", () => {
+ const result = truncateWithBudget("", 100);
+ expect(result).toEqual({ text: "", truncated: false });
+ });
+
+ it("budget of 0", () => {
+ const result = truncateWithBudget("hello", 0);
+ expect(result.truncated).toBe(true);
+ });
+
+ it("very large content with very small budget", () => {
+ const text = "A".repeat(100_000);
+ const result = truncateWithBudget(text, 100);
+ expect(result.truncated).toBe(true);
+ expect(result.text.length).toBeLessThan(text.length);
+ expect(result.text).toContain("characters omitted");
+ });
+
+ it("content at exact budget boundary", () => {
+ const text = "A".repeat(DEFAULT_BOOTSTRAP_MAX_CHARS);
+ const result = truncateWithBudget(text, DEFAULT_BOOTSTRAP_MAX_CHARS);
+ expect(result.truncated).toBe(false);
+ expect(result.text).toBe(text);
+ });
+
+ it("content at budget + 1", () => {
+ const text = "A".repeat(DEFAULT_BOOTSTRAP_MAX_CHARS + 1);
+ const result = truncateWithBudget(text, DEFAULT_BOOTSTRAP_MAX_CHARS);
+ expect(result.truncated).toBe(true);
+ });
+});
diff --git a/packages/core/src/agent/system-prompt/index.ts b/packages/core/src/agent/system-prompt/index.ts
index 5944eb399f..ce16215d09 100644
--- a/packages/core/src/agent/system-prompt/index.ts
+++ b/packages/core/src/agent/system-prompt/index.ts
@@ -6,6 +6,11 @@ export { buildSystemPrompt, buildSystemPromptWithReport } from "./builder.js";
export { collectRuntimeInfo, formatRuntimeLine } from "./runtime-info.js";
export { formatPromptReport } from "./report.js";
export { SAFETY_CONSTITUTION } from "./constitution.js";
+export {
+ DEFAULT_BOOTSTRAP_MAX_CHARS,
+ DEFAULT_SKILLS_MAX_CHARS,
+ truncateWithBudget,
+} from "./sections.js";
export type {
ChannelInfo,
diff --git a/packages/core/src/agent/system-prompt/sections.test.ts b/packages/core/src/agent/system-prompt/sections.test.ts
index 5ac3bfe031..71de087105 100644
--- a/packages/core/src/agent/system-prompt/sections.test.ts
+++ b/packages/core/src/agent/system-prompt/sections.test.ts
@@ -13,6 +13,9 @@ import {
buildToolingSummary,
buildUserSection,
buildWorkspaceSection,
+ DEFAULT_BOOTSTRAP_MAX_CHARS,
+ DEFAULT_SKILLS_MAX_CHARS,
+ truncateWithBudget,
} from "./sections.js";
describe("buildIdentitySection", () => {
@@ -274,6 +277,96 @@ describe("buildProfileDirSection", () => {
});
});
+describe("truncateWithBudget", () => {
+ it("returns text unchanged when within budget", () => {
+ const result = truncateWithBudget("short text", 100);
+ expect(result).toEqual({ text: "short text", truncated: false });
+ });
+
+ it("truncates text exceeding budget with head + marker + tail", () => {
+ const text = "A".repeat(1000);
+ const result = truncateWithBudget(text, 200);
+ expect(result.truncated).toBe(true);
+ expect(result.text).toContain("characters omitted");
+ expect(result.text).toContain("content truncated to fit system prompt budget");
+ // Head should be ~70% of budget = 140 chars of "A"
+ expect(result.text.startsWith("A".repeat(100))).toBe(true);
+ // Tail should be ~20% of budget = 40 chars of "A"
+ expect(result.text.endsWith("A".repeat(40))).toBe(true);
+ });
+
+ it("snaps head boundary to newline when possible", () => {
+ // Build text with a newline at ~85% of head budget (within the 80-100% snap range)
+ const budget = 200;
+ // Place a newline at position 120 (> 140*0.8=112, so within snap range)
+ const text = "A".repeat(120) + "\n" + "B".repeat(900);
+ const result = truncateWithBudget(text, budget);
+ expect(result.truncated).toBe(true);
+ // Head should snap to the newline at position 120 (not continue to 140)
+ const headPortion = result.text.split("characters omitted")[0]!;
+ // The head portion should end at the newline, so no "B" chars before the marker
+ expect(headPortion).not.toContain("B");
+ expect(result.text.startsWith("A".repeat(120))).toBe(true);
+ });
+
+ it("reports correct omitted character count", () => {
+ const text = "X".repeat(500);
+ const result = truncateWithBudget(text, 100);
+ expect(result.truncated).toBe(true);
+ // head = floor(100*0.7) = 70, tail = floor(100*0.2) = 20, omitted = 500 - 70 - 20 = 410
+ expect(result.text).toContain("410 characters omitted");
+ });
+
+ it("handles exact budget boundary", () => {
+ const text = "A".repeat(100);
+ const result = truncateWithBudget(text, 100);
+ expect(result).toEqual({ text: text, truncated: false });
+ });
+});
+
+describe("buildWorkspaceSection budget control", () => {
+ it("truncates oversized workspace.md content", () => {
+ const oversized = "Line " + "X".repeat(DEFAULT_BOOTSTRAP_MAX_CHARS + 5000);
+ const result = buildWorkspaceSection({ workspace: oversized }, "full");
+ const text = result.join("\n");
+ expect(text).toContain("characters omitted");
+ expect(text.length).toBeLessThan(oversized.length);
+ });
+
+ it("preserves small workspace.md without truncation", () => {
+ const small = "Small workspace rules.";
+ const result = buildWorkspaceSection({ workspace: small }, "full");
+ const text = result.join("\n");
+ expect(text).toContain("Small workspace rules.");
+ expect(text).not.toContain("characters omitted");
+ });
+});
+
+describe("buildSkillsSection budget control", () => {
+ it("truncates oversized skills prompt", () => {
+ const oversized = "## skill1\n" + "Z".repeat(DEFAULT_SKILLS_MAX_CHARS + 3000);
+ const result = buildSkillsSection(oversized, "full");
+ const text = result.join("\n");
+ expect(text).toContain("characters omitted");
+ expect(text).toContain("## Skills (mandatory)");
+ });
+
+ it("preserves small skills prompt without truncation", () => {
+ const small = "## commit\nDo commits.";
+ const result = buildSkillsSection(small, "full");
+ const text = result.join("\n");
+ expect(text).toContain("## commit");
+ expect(text).not.toContain("characters omitted");
+ });
+});
+
+describe("budget constants", () => {
+ it("exports expected default values", () => {
+ expect(DEFAULT_BOOTSTRAP_MAX_CHARS).toBe(20_000);
+ expect(DEFAULT_SKILLS_MAX_CHARS).toBe(12_000);
+ });
+});
+
describe("buildSubagentSection", () => {
const ctx = { requesterSessionId: "parent", childSessionId: "child", task: "Find bugs" };
diff --git a/packages/core/src/agent/system-prompt/sections.ts b/packages/core/src/agent/system-prompt/sections.ts
index 05e53a8952..8c6c74b4bf 100644
--- a/packages/core/src/agent/system-prompt/sections.ts
+++ b/packages/core/src/agent/system-prompt/sections.ts
@@ -47,6 +47,48 @@ const TOOL_ORDER = [
"data",
];
+// ─── Bootstrap budget constants ──────────────────────────────────────────────
+
+/** Max characters per injected file (workspace.md, etc.) */
+export const DEFAULT_BOOTSTRAP_MAX_CHARS = 20_000;
+
+/** Max characters for the skills prompt total */
+export const DEFAULT_SKILLS_MAX_CHARS = 12_000;
+
+/**
+ * Truncate text to fit within a character budget.
+ *
+ * When text exceeds budget, keeps head (70%) + truncation marker + tail (20%).
+ * Returns { text, truncated } so callers can log/report the truncation.
+ */
+export function truncateWithBudget(
+ text: string,
+ budget: number,
+): { text: string; truncated: boolean } {
+ if (text.length <= budget) return { text, truncated: false };
+
+ const headChars = Math.floor(budget * 0.7);
+ const tailChars = Math.floor(budget * 0.2);
+
+ // Try to break head at a newline boundary (within last 20% of head budget)
+ let headEnd = headChars;
+ const lastNewline = text.lastIndexOf("\n", headChars);
+ if (lastNewline > headChars * 0.8) {
+ headEnd = lastNewline;
+ }
+
+ const head = text.slice(0, headEnd);
+ const tail = text.slice(text.length - tailChars);
+ const omitted = text.length - headEnd - tailChars;
+
+ const marker = `\n\n... [${omitted} characters omitted — content truncated to fit system prompt budget] ...\n\n`;
+
+ return {
+ text: head + marker + tail,
+ truncated: true,
+ };
+}
+
// ─── Section builders ───────────────────────────────────────────────────────
/**
@@ -125,9 +167,10 @@ export function buildWorkspaceSection(
);
}
- // Add workspace.md content
+ // Add workspace.md content (with budget control)
if (profile?.workspace) {
- lines.push(profile.workspace);
+ const { text } = truncateWithBudget(profile.workspace, DEFAULT_BOOTSTRAP_MAX_CHARS);
+ lines.push(text);
}
return lines;
@@ -366,6 +409,8 @@ export function buildSkillsSection(
const trimmed = skillsPrompt?.trim();
if (!trimmed) return [];
+ const { text: budgeted } = truncateWithBudget(trimmed, DEFAULT_SKILLS_MAX_CHARS);
+
return [
"## Skills (mandatory)",
"Before replying: scan the available skills below.",
@@ -374,7 +419,7 @@ export function buildSkillsSection(
"- If none clearly apply but an **inactive skill** matches the user's intent: suggest activating it.",
"- If no skill matches at all: skip skill invocation.",
"",
- trimmed,
+ budgeted,
"",
];
}