Skip to content

fix: cover the trim-to-compaction amnesia window with a continuity note#1439

Merged
njbrake merged 2 commits into
mainfrom
fix/post-trim-continuity-note
Jun 10, 2026
Merged

fix: cover the trim-to-compaction amnesia window with a continuity note#1439
njbrake merged 2 commits into
mainfrom
fix/post-trim-continuity-note

Conversation

@njbrake

@njbrake njbrake commented Jun 10, 2026

Copy link
Copy Markdown
Member

Note: this PR was drafted by Claude via back-and-forth with @njbrake. The reasoning and decisions are his; the prose is Claude's.

Description

Fixes #1432.

trigger_compaction_for_dropped advances the trim watermark synchronously, so dropped rows vanish from LLM context on the very next message, but the compaction LLM call that extracts their facts into MEMORY.md is async and may still be in flight (or may have failed). In that window the agent has amnesia for the dropped range, at the worst possible moment: immediately after the trim, when the dropped content is most likely still topical. The deterministic trim summary the trim turn itself saw is never persisted, so it exists for one turn only.

Mitigation, preserving the watermark semantics untouched:

  • New build_pending_compaction_note(user_id) rebuilds a terse deterministic summary (the existing summarize_dropped_messages shape) of the rows covered by the user's recent (under 60 minutes) 'pending' compaction events, straight from the durable message rows.
  • The note is injected as a dynamic system-prompt section ("Recently Archived Conversation (memory update in progress)"). Once the event flips to 'completed', the note disappears and MEMORY.md carries the facts. The acceptance criterion from the issue holds: on the message immediately following a trim, the context contains either the compacted facts or the note, never neither (the pending row is inserted synchronously in phase 1).

Deliberate deviation from the issue: the issue proposed persisting the summary on the CompactionEvent row. Recomputing per turn instead avoids a schema migration (also avoiding an Alembic head collision with #1438's migration 039): the inputs are durable (seq range + message rows are never deleted), the summarizer is deterministic and cheap (no LLM, output capped at 500 chars), and the recompute only runs while a pending event exists. The common case costs one indexed SELECT per turn.

Bounds:

Type

  • Feature
  • Bug fix
  • Refactor
  • Test
  • CI/CD
  • Documentation

Checklist

  • Tests pass (uv run pytest -v) (2891 passed, 2 skipped)
  • Lint passes (ruff check backend/ && ruff format --check backend/)
  • New tests added for new functionality (note presence, determinism, completion clears it, stale-event cutoff, multi-event coverage)
  • Bug fixes include regression tests

AI Usage

  • AI-assisted (describe how): Claude analyzed the amnesia window, implemented the continuity note, and wrote the tests, with direction and review by @njbrake.
  • No AI used

trigger_compaction_for_dropped advances the trim watermark
synchronously, so dropped rows vanish from LLM context on the very next
message, but the compaction LLM call that extracts their facts into
MEMORY.md is async and may still be in flight (or may have failed). In
that window the agent has amnesia for the dropped range, immediately
after the trim, when the dropped content is most likely still topical.
The deterministic trim summary the trim turn saw was never persisted,
so it existed for one turn only.

While the user has a recent (under 60 minutes) 'pending' compaction
event, a terse deterministic summary of the covered rows is rebuilt
from the durable message rows (same summarize_dropped_messages shape)
and injected as a dynamic system-prompt section. Once the event flips
to 'completed', the note disappears and MEMORY.md carries the facts:
the context now contains either the compacted facts or the note, never
neither.

Design choices: the summary is recomputed per turn instead of persisted
(inputs are durable, the summarizer is cheap and deterministic, no
migration needed); the note rides the dynamic half of the prompt so it
never busts the history cache; the 60-minute window keeps a permanently
stuck event from pinning a stale note forever (that case belongs to the
retry sweep, issue #1431). Watermark semantics are untouched.

Fixes #1432

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@njbrake, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 10 minutes and 45 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 67d9b696-2de7-4199-ba77-01d425ac920f

📥 Commits

Reviewing files that changed from the base of the PR and between 449f85b and be048c8.

📒 Files selected for processing (3)
  • backend/app/agent/compaction_note.py
  • backend/app/agent/system_prompt.py
  • tests/test_compaction_note.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/post-trim-continuity-note
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/post-trim-continuity-note

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@njbrake njbrake merged commit f30e8ce into main Jun 10, 2026
10 checks passed
@njbrake njbrake deleted the fix/post-trim-continuity-note branch June 10, 2026 11:02
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.

Post-trim amnesia window: watermark advances before async compaction lands

1 participant