Skip to content

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

Go

feat(system-prompt): enhance report with truncation tracking and token estimates

Jiayuan Zhanga1ac250e2b26
diff --git a/packages/core/src/agent/system-prompt/builder.test.ts b/packages/core/src/agent/system-prompt/builder.test.ts
index 426e857a33..21dce243aa 100644
--- a/packages/core/src/agent/system-prompt/builder.test.ts
+++ b/packages/core/src/agent/system-prompt/builder.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { buildSystemPrompt, buildSystemPromptWithReport } from "./builder.js";
+import { formatPromptReport } from "./report.js";
import type { SystemPromptOptions } from "./types.js";
const PROFILE = {
@@ -252,4 +253,72 @@ describe("buildSystemPromptWithReport", () => {
const safety = report.sections.find((s) => s.name === "safety");
expect(safety?.included).toBe(true);
});
+
+ it("report includes truncation info for oversized workspace", () => {
+ const oversizedWorkspace = "X".repeat(25_000);
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: oversizedWorkspace },
+ });
+ 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 info for oversized skills", () => {
+ const oversizedSkills = "## skill\n" + "Y".repeat(15_000);
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ skillsPrompt: oversizedSkills,
+ });
+ const sk = report.sections.find((s) => s.name === "skills");
+ expect(sk?.included).toBe(true);
+ expect(sk?.truncated).toBe(true);
+ expect(sk?.originalChars).toBe(oversizedSkills.trim().length);
+ });
+
+ it("report does not flag truncation for small content", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "Small rules" },
+ skillsPrompt: "## commit\nDo commits.",
+ });
+ 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();
+ });
+});
+
+describe("formatPromptReport", () => {
+ it("includes estimated token count", () => {
+ const { report } = buildSystemPromptWithReport({ mode: "full", tools: TOOLS });
+ const formatted = formatPromptReport(report);
+ expect(formatted).toContain("tokens)");
+ // Estimated tokens = chars / 4
+ const expectedTokens = Math.ceil(report.totalChars / 4);
+ expect(formatted).toContain(`~${expectedTokens} tokens`);
+ });
+
+ it("shows truncation info in formatted output", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "X".repeat(25_000) },
+ });
+ const formatted = formatPromptReport(report);
+ expect(formatted).toContain("truncated from 25000 chars");
+ });
+
+ it("does not show truncation for non-truncated sections", () => {
+ const { report } = buildSystemPromptWithReport({
+ mode: "full",
+ profile: { workspace: "Small" },
+ });
+ const formatted = formatPromptReport(report);
+ expect(formatted).not.toContain("truncated from");
+ });
});
diff --git a/packages/core/src/agent/system-prompt/builder.ts b/packages/core/src/agent/system-prompt/builder.ts
index f7120d7243..f49269b6c6 100644
--- a/packages/core/src/agent/system-prompt/builder.ts
+++ b/packages/core/src/agent/system-prompt/builder.ts
@@ -26,6 +26,9 @@ import {
buildToolingSummary,
buildUserSection,
buildWorkspaceSection,
+ truncateWithBudget,
+ DEFAULT_BOOTSTRAP_MAX_CHARS,
+ DEFAULT_SKILLS_MAX_CHARS,
} from "./sections.js";
/**
@@ -57,18 +60,29 @@ export function buildSystemPromptWithReport(options: SystemPromptOptions): {
includeSafety = true,
} = options;
+ // Pre-compute truncation info for budget-controlled sections
+ const workspaceOriginalChars = profile?.workspace?.length ?? 0;
+ const workspaceTruncated = workspaceOriginalChars > DEFAULT_BOOTSTRAP_MAX_CHARS;
+ const skillsTrimmed = skillsPrompt?.trim() ?? "";
+ const skillsOriginalChars = skillsTrimmed.length;
+ const skillsTruncated = skillsOriginalChars > DEFAULT_SKILLS_MAX_CHARS;
+
// Collect all candidate sections in order
- const candidates: Array<{ name: string; lines: string[] }> = [
+ const candidates: Array<{ name: string; lines: string[]; truncated?: boolean; originalChars?: number }> = [
{ name: "identity", lines: buildIdentitySection(profile, mode) },
{ name: "user", lines: buildUserSection(profile, mode) },
- { name: "workspace", lines: buildWorkspaceSection(profile, mode, profileDir, workspaceDir) },
+ { name: "workspace", lines: buildWorkspaceSection(profile, mode, profileDir, workspaceDir),
+ ...(workspaceTruncated ? { truncated: true, originalChars: workspaceOriginalChars } : {}),
+ },
{ name: "memory", lines: buildMemoryFileSection(profile, mode) },
{ name: "heartbeat", lines: buildHeartbeatSection(profile, mode) },
{ name: "safety", lines: buildSafetySection(includeSafety) },
{ name: "tooling", lines: buildToolingSummary(tools, mode) },
{ name: "tool-call-style", lines: buildToolCallStyleSection(mode) },
{ name: "conditional-tools", lines: buildConditionalToolSections(tools, mode) },
- { name: "skills", lines: buildSkillsSection(skillsPrompt, mode) },
+ { name: "skills", lines: buildSkillsSection(skillsPrompt, mode),
+ ...(skillsTruncated ? { truncated: true, originalChars: skillsOriginalChars } : {}),
+ },
{ name: "runtime", lines: buildRuntimeSection(runtime, mode) },
{ name: "time-awareness", lines: buildTimeAwarenessSection(tools, mode) },
{ name: "profile-dir", lines: buildProfileDirSection(profileDir, mode) },
@@ -81,7 +95,7 @@ export function buildSystemPromptWithReport(options: SystemPromptOptions): {
const sections: PromptSection[] = [];
const reportSections: SystemPromptReport["sections"] = [];
- for (const { name, lines } of candidates) {
+ for (const { name, lines, truncated, originalChars } of candidates) {
const included = lines.length > 0;
const content = lines.join("\n");
reportSections.push({
@@ -89,6 +103,7 @@ export function buildSystemPromptWithReport(options: SystemPromptOptions): {
chars: content.length,
lines: included ? content.split("\n").length : 0,
included,
+ ...(truncated && included ? { truncated: true, originalChars } : {}),
});
if (included) {
sections.push({ name, content });
diff --git a/packages/core/src/agent/system-prompt/report.ts b/packages/core/src/agent/system-prompt/report.ts
index 3bcaea148d..89a8344c17 100644
--- a/packages/core/src/agent/system-prompt/report.ts
+++ b/packages/core/src/agent/system-prompt/report.ts
@@ -8,9 +8,12 @@ import type { SystemPromptReport } from "./types.js";
* Format a prompt report as a human-readable summary for debugging.
*/
export function formatPromptReport(report: SystemPromptReport): string {
+ // Estimate tokens as chars/4 (same heuristic used by token estimation)
+ const estimatedTokens = Math.ceil(report.totalChars / 4);
+
const lines: string[] = [
`System Prompt Report (mode: ${report.mode})`,
- ` Total: ${report.totalChars} chars, ${report.totalLines} lines`,
+ ` Total: ${report.totalChars} chars, ${report.totalLines} lines (~${estimatedTokens} tokens)`,
` Tools: ${report.toolCount}`,
` Skills: ${report.skillsIncluded ? "yes" : "no"}`,
` Safety: ${report.safetyIncluded ? "yes" : "no"}`,
@@ -20,9 +23,11 @@ export function formatPromptReport(report: SystemPromptReport): string {
for (const section of report.sections) {
const status = section.included ? "✓" : "—";
- lines.push(
- ` ${status} ${section.name}: ${section.chars} chars, ${section.lines} lines`,
- );
+ let detail = `${section.chars} chars, ${section.lines} lines`;
+ if (section.truncated && section.originalChars !== undefined) {
+ detail += ` (truncated from ${section.originalChars} chars)`;
+ }
+ lines.push(` ${status} ${section.name}: ${detail}`);
}
return lines.join("\n");
diff --git a/packages/core/src/agent/system-prompt/types.ts b/packages/core/src/agent/system-prompt/types.ts
index 0741a80e0c..5420733b38 100644
--- a/packages/core/src/agent/system-prompt/types.ts
+++ b/packages/core/src/agent/system-prompt/types.ts
@@ -107,6 +107,10 @@ export interface SectionReport {
chars: number;
lines: number;
included: boolean;
+ /** Whether this section's content was truncated by budget control */
+ truncated?: boolean | undefined;
+ /** Original character count before truncation (only present when truncated) */
+ originalChars?: number | undefined;
}
/** Telemetry report about a generated system prompt */