Skip to content

test: cover chatd compaction usage decision (AIGOV-585) - #28053

Draft
johnstcn wants to merge 1 commit into
mainfrom
cian/aigov-585-chat-compaction-tests
Draft

test: cover chatd compaction usage decision (AIGOV-585)#28053
johnstcn wants to merge 1 commit into
mainfrom
cian/aigov-585-chat-compaction-tests

Conversation

@johnstcn

Copy link
Copy Markdown
Member

Summary

Adds test coverage for the three compaction-decision functions in chatd that had zero tests: latestPromptUsage, shouldCompactPromptUsage, and contextTokensFromUsage.

AIGOV-585 hypothesized that chatd's token counting logic was incorrect — that it compared a cumulative sum of prompt tokens across all agentic-loop steps against the context window. The tests disprove this: latestPromptUsage returns the last persisted assistant message's usage, not a sum. The actual bug was in the aibridge streaming interceptor, which summed usage across SSE chunks and persisted inflated values (fixed in ad100452d4).

What's tested

  • TestLatestPromptUsage — pins that the compaction path reads the last step's usage (5,400), not a cumulative sum across steps (15,600). If someone wires TotalUsage into the compaction path as the issue suggested, this fails.
  • TestShouldCompactPromptUsage — covers the threshold decision with the inflated value from the issue (417,012 → compacts), the correct value (6,000 → doesn't compact), cache token counting, and both disable guards (threshold=100, contextLimit=0).
Plan / investigation notes
  • Traced the full flow: chatloop.go:993 sets result.usage = part.Usage from the per-step StreamPartTypeFinish event, not the accumulated TotalUsage from agent.go:544. chatd never calls fantasy's Agent interface.
  • The TotalUsage accumulation in agent.go:544 is only used for cost attribution, not context occupancy.
  • Commit ad100452d4 fixed the real bug in aibridge/intercept/chatcompletions/streaming.go (cross-chunk usage summation for vLLM-style backends).
  • Tests reuse existing dbMessage and withUsage helpers from message_conversion_test.go (same package).

Generated by Coder Agents

@linear-code

linear-code Bot commented Aug 12, 2026

Copy link
Copy Markdown

AIGOV-585

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant