Skip to content

Commit 3df53b5

Browse files
anandgupta42claude
andcommitted
fix: address multi-model code review findings on PR #35
- Fix text-end overwriting time.start (processor.ts) — use spread to preserve original start timestamp, matching reasoning-end handler - Guard negative usable in isOverflow (compaction.ts) — return false when headroom exceeds base instead of producing negative usable that would trigger compaction on every turn - Remove dead "manual" trigger type from telemetry union - Add tests for negative usable edge cases Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5aab4bc commit 3df53b5

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

packages/altimate-code/src/session/compaction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ export namespace SessionCompaction {
8585
const maxOutput = ProviderTransform.maxOutputTokens(input.model)
8686
const reserved = config.compaction?.reserved ?? COMPACTION_BUFFER
8787
const headroom = Math.max(reserved, maxOutput)
88-
const usable = (input.model.limit.input ?? context) - headroom
89-
return count >= usable
88+
const base = input.model.limit.input ?? context
89+
if (base <= headroom) return false
90+
return count >= base - headroom
9091
}
9192

9293
export const PRUNE_MINIMUM = 20_000

packages/altimate-code/src/session/processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export namespace SessionProcessor {
374374
)
375375
currentText.text = textOutput.text
376376
currentText.time = {
377-
start: Date.now(),
377+
...currentText.time,
378378
end: Date.now(),
379379
}
380380
if (value.providerMetadata) currentText.metadata = value.providerMetadata

packages/altimate-code/src/telemetry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export namespace Telemetry {
9797
type: "compaction_triggered"
9898
timestamp: number
9999
session_id: string
100-
trigger: "overflow_detection" | "error_recovery" | "manual"
100+
trigger: "overflow_detection" | "error_recovery"
101101
attempt: number
102102
}
103103
| {

packages/altimate-code/test/session/compaction-loop.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,33 @@ describe("session.compaction.isOverflow boundary conditions", () => {
534534
})
535535
})
536536

537+
test("returns false when headroom exceeds base (negative usable guard)", async () => {
538+
await using tmp = await tmpdir()
539+
await Instance.provide({
540+
directory: tmp.path,
541+
fn: async () => {
542+
// Tiny model: input=32K, output=128K → headroom=128K > base=32K
543+
// Without guard this would produce negative usable and always trigger
544+
const model = createModel({ context: 200_000, input: 32_000, output: 128_000 })
545+
const tokens = { input: 1_000, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
546+
expect(await SessionCompaction.isOverflow({ tokens, model })).toBe(false)
547+
},
548+
})
549+
})
550+
551+
test("returns false when headroom equals base exactly", async () => {
552+
await using tmp = await tmpdir()
553+
await Instance.provide({
554+
directory: tmp.path,
555+
fn: async () => {
556+
// headroom = max(20K, 32K) = 32K, base = 32K → usable = 0
557+
const model = createModel({ context: 32_000, output: 32_000 })
558+
const tokens = { input: 1_000, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
559+
expect(await SessionCompaction.isOverflow({ tokens, model })).toBe(false)
560+
},
561+
})
562+
})
563+
537564
test("compaction disabled via prune config still allows isOverflow", async () => {
538565
await using tmp = await tmpdir({
539566
init: async (dir) => {

0 commit comments

Comments
 (0)