feat(session): archive compaction-dropped messages to disk (M1.4)#5
Merged
Conversation
Compaction used to replace the dropped middle slice with an inline "[context compacted: N messages omitted]" notice and discard the originals. That made the operation lossy: there was no way to recover what got cut, and no way to audit a long-running session's compaction history. Surface introduced: - `CompactOptions.archiveSink?: (omitted: readonly Message[]) => void` on `compactMessages`. Synchronously hands the caller the *original* (unsnipped) dropped slice. Default is no-op, so headless query-loop retries on prompt_too_long stay unchanged. - `compactSessionRecord` becomes async and accepts an optional `archiver: (omitted, at) => Promise<string|undefined>`. The returned path is stamped onto the resulting `compact` SessionEvent as `archivePath`. Omitting the archiver, or returning undefined, keeps `archivePath` unset for back-compat with older sessions. - `myagent compact <id>` and the TUI `/compact` wire a default archiver that writes JSON to `.myagent/artifacts/<sessionId>/compactions/<at>.json`. The CLI prints the archive path on success. Filename uses `at` with `:` and `.` replaced by `-` so it round-trips on Windows. - `myagent resume <id> --show-compactions` lists every compact event on the session with its archive path (or `<not archived>` for pre-M1.4 sessions / query-loop retries with no archiver). Tests added in `packages/core/test/security/compaction-archive.test.ts` pin the new invariants (sink fires only on omission, archiver path round-trips, undefined archiver/return keeps archivePath unset). A CLI-level e2e test exercises the whole path: build a fixture session big enough to actually trigger omission, run `myagent compact`, read back the JSON archive, verify content is unsnipped, then run `myagent resume --show-compactions` and assert the archive path is listed. Catalog row added to `packages/core/test/security/README.md`; CLAUDE.md updated. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Compaction used to replace the dropped middle slice with an inline "[context compacted: N messages omitted]" notice and discard the originals. That made the operation lossy: there was no way to recover what got cut, and no way to audit a long-running session's compaction history.
Surface introduced:
CompactOptions.archiveSink?: (omitted: readonly Message[]) => voidoncompactMessages. Synchronously hands the caller the original (unsnipped) dropped slice. Default is no-op, so headless query-loop retries on prompt_too_long stay unchanged.compactSessionRecordbecomes async and accepts an optionalarchiver: (omitted, at) => Promise<string|undefined>. The returned path is stamped onto the resultingcompactSessionEvent asarchivePath. Omitting the archiver, or returning undefined, keepsarchivePathunset for back-compat with older sessions.myagent compact <id>and the TUI/compactwire a default archiver that writes JSON to.myagent/artifacts/<sessionId>/compactions/<at>.json. The CLI prints the archive path on success. Filename usesatwith:and.replaced by-so it round-trips on Windows.myagent resume <id> --show-compactionslists every compact event on the session with its archive path (or<not archived>for pre-M1.4 sessions / query-loop retries with no archiver).Tests added in
packages/core/test/security/compaction-archive.test.tspin the new invariants (sink fires only on omission, archiver path round-trips, undefined archiver/return keeps archivePath unset). A CLI-level e2e test exercises the whole path: build a fixture session big enough to actually trigger omission, runmyagent compact, read back the JSON archive, verify content is unsnipped, then runmyagent resume --show-compactionsand assert the archive path is listed. Catalog row added topackages/core/test/security/README.md; CLAUDE.md updated.