feat(agent): keep every user turn verbatim across compaction + structured facts digest#4052
Merged
Merged
Conversation
…digest Builds on the first-turn pin (#4048): the deterministic floor now covers a fact the user states at ANY point, not just the opening turn. Compaction keeps every small user turn verbatim and folds only the assistant/tool work, so a mid-session "always deploy to eu-west-3" survives regardless of how the summarizer behaves. On top of that floor, the digest now leads with a structured "Standing facts & constraints" section consolidating what the user stated into one tidy view — redundant with the verbatim turns by design, so a weak summarizer dropping a fact there loses nothing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #4048. That PR pinned the first user turn; this adds the deterministic floor for facts stated at any point, plus a structured consolidation layer.
Base layer — the guarantee
Compaction now keeps every small user turn verbatim and folds only assistant/tool work (and oversized user pastes). A fact the user states mid-session — "always deploy to eu-west-3, never us-east-1" at turn 4 — is preserved in its own words regardless of how the summarizer behaves. This removes the model from the loop for user-stated facts: the only way to guarantee a fact stays in context is to never let a model re-generate it.
partitionFoldsplits the compaction region into kept (small user turns) and folded (the rest); the kept turns are spliced back verbatim before the digest.Upper layer — the tidy view
The digest leads with a structured
## Standing facts & constraintssection that consolidates what the user stated into one clean list. It's redundant with the verbatim turns by design — best-effort consolidation on top of a deterministic floor, so a weak summarizer dropping a fact there loses nothing.Real-provider A/B
5-turn session, 14k window, 18 folds. Fact A stated turn 1, fact B ("eu-west-3") stated mid-session at turn 2; final turn asks for both.
Both recalled the fact this run (the old summary happened to keep it), but the mechanism differs: the floor makes presence structural, not stochastic.
Tests
New
TestCompactKeepsMidSessionUserTurns(a mid-session user fact survives a fold verbatim while the work folds). Existing compaction/prune tests unchanged and green;go test ./internal/agent/..., control, boot pass.