Skip to content

Commit 27b3b15

Browse files
philoserfclaude
andcommitted
fix: handle slice(-0) in truncateHeadTail when tail budget is zero
Use floor instead of round and compute right as remainder to guarantee left + right === limit. Guard slice with right > 0 to avoid slice(-0) returning the full array. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4fa72f6 commit 27b3b15

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/utils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ describe("truncateHeadTail", () => {
136136
test("handles limit of 1", () => {
137137
const tokens = ["a", "b", "c", "d", "e"];
138138
const result = truncateHeadTail(tokens, 1);
139-
// 80% of 1 = 1, 20% of 1 = 0 — head portion only
139+
// All budget goes to head, tail is empty
140140
expect(result).toContain("a");
141141
expect(result).toContain("\n...\n");
142+
expect(result).not.toContain("e");
142143
});
143144
});
144145

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ export function truncateHeadOnly(tokens: string[], limit: number): string {
8888
}
8989

9090
export function truncateHeadTail(tokens: string[], limit: number): string {
91-
const left = Math.round(limit * 0.8);
92-
const right = Math.round(limit * 0.2);
91+
const left = Math.max(1, Math.floor(limit * 0.8));
92+
const right = Math.max(0, limit - left);
9393
const leftTokens = tokens.slice(0, left);
94-
const rightTokens = tokens.slice(-right);
94+
const rightTokens = right > 0 ? tokens.slice(-right) : [];
9595
return `${joinTokens(leftTokens)}\n...\n${joinTokens(rightTokens)}`;
9696
}
9797

0 commit comments

Comments
 (0)