Skip to content

Commit 070b8b8

Browse files
fix(agents): report accurate omitted char count in capDirectTextContent marker
1 parent f765506 commit 070b8b8

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/agents/subagent-announce-delivery.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4644,7 +4644,8 @@ describe("capDirectTextContent", () => {
46444644
const capped = testing.capDirectTextContent(longText);
46454645

46464646
expect(capped.length).toBeLessThanOrEqual(4000);
4647-
expect(capped).toContain("... [truncated 1000 chars] ...");
4647+
// 5000 chars - 2600 head - 1000 tail = 1400 omitted (not 1000)
4648+
expect(capped).toContain("... [truncated 1400 chars] ...");
46484649
expect(capped.startsWith("x".repeat(2600))).toBe(true);
46494650
expect(capped.endsWith("x".repeat(1000))).toBe(true);
46504651
});
@@ -4659,7 +4660,8 @@ describe("capDirectTextContent", () => {
46594660
const text = "z".repeat(3000);
46604661
const capped = testing.capDirectTextContent(text, 2000);
46614662
expect(capped.length).toBeLessThanOrEqual(2000);
4662-
expect(capped).toContain("... [truncated 1000 chars] ...");
4663+
// 3000 chars - 1300 head (0.65*2000) - 500 tail (0.25*2000) = 1200 omitted
4664+
expect(capped).toContain("... [truncated 1200 chars] ...");
46634665
});
46644666

46654667
it("handles edge case of exactly maxChars", () => {
@@ -4668,6 +4670,16 @@ describe("capDirectTextContent", () => {
46684670
expect(capped).toBe(text);
46694671
expect(capped.length).toBe(4000);
46704672
});
4673+
4674+
it("reports accurate omitted count for head+tail cap (regression for misleading marker)", () => {
4675+
// 5000 chars, cap 4000 → head 2600 + tail 1000 = 3600 kept, 1400 omitted.
4676+
const text = "z".repeat(5000);
4677+
const capped = testing.capDirectTextContent(text);
4678+
const match = capped.match(/\[truncated (\d+) chars\]/);
4679+
expect(match).not.toBeNull();
4680+
const reported = Number(match?.[1]);
4681+
expect(reported).toBe(1400);
4682+
});
46714683
});
46724684

46734685
/**
@@ -4860,7 +4872,7 @@ describe("active wake failure fallback", () => {
48604872

48614873
expect(sendMessage).toHaveBeenCalledWith(
48624874
expect.objectContaining({
4863-
content: expect.stringContaining("... [truncated 2000 chars] ..."),
4875+
content: expect.stringContaining("... [truncated 2400 chars] ..."),
48644876
}),
48654877
);
48664878
});

src/agents/subagent-announce-delivery.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,13 @@ function capDirectTextContent(text: string, maxChars = 4000): string {
922922

923923
const headChars = Math.floor(maxChars * 0.65); // 2600
924924
const tailChars = Math.floor(maxChars * 0.25); // 1000
925-
const marker = `\n\n... [truncated ${text.length - maxChars} chars] ...\n\n`;
925+
// The omitted count is `text.length - headChars - tailChars`, NOT
926+
// `text.length - maxChars`. The marker used to over-state how many
927+
// characters were dropped relative to the original `maxChars` cap
928+
// (e.g. for 5000 chars with the default 4000 cap, head=2600 and
929+
// tail=1000 so we keep 3600 and drop 1400, not the reported 1000).
930+
const omitted = text.length - headChars - tailChars;
931+
const marker = `\n\n... [truncated ${omitted} chars] ...\n\n`;
926932

927933
return text.slice(0, headChars) + marker + text.slice(-tailChars);
928934
}

0 commit comments

Comments
 (0)