Fix KeyError in cmd_compress: stats dict key mismatch - #588
Conversation
…ression_stats() Dialect.compression_stats() was refactored to use semantic key names (summary_chars, original_tokens_est, summary_tokens_est, size_ratio) but cmd_compress still referenced the old names (compressed_chars, original_tokens, compressed_tokens, ratio), causing a KeyError crash on every invocation. Updated cli.py and test mocks to use the correct key names. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
web3guru888
left a comment
There was a problem hiding this comment.
Fix is correct. I verified against dialect.py — compression_stats() has returned summary_chars, original_tokens_est, summary_tokens_est, and size_ratio since the 39c14be refactor. The old key names in cli.py were never updated, so this was a guaranteed crash on any mempalace compress invocation.
One non-blocking observation: line 374 in the patch stores to comp_meta["original_tokens"] (the ChromaDB metadata field name) while reading from stats["original_tokens_est"] (the dialect key). The asymmetry is intentional — the stored metadata key predates the _est suffix — but it's a potential source of confusion for future contributors. A brief comment (# stored key name predates _est suffix) would help.
The test mock updates are exactly right. No new tests needed since this is a key-name-only fix with coverage already in place.
LGTM — ship it.
web3guru888
left a comment
There was a problem hiding this comment.
Clean fix — the key mismatch is mechanical (cli.py references stale keys that no longer match what compression_stats() returns) and the PR addresses all 4 mismatches consistently across both cli.py and the test mocks.
The key mapping table in the PR description is exactly what this kind of rename fix needs — makes the diff reviewable in 30 seconds.
One note: #562 includes the same compress key fix (along with 14 other items). If #562 lands first, this PR would conflict. Either way one of them fixes it — just flagging for maintainer coordination.
LGTM.
web3guru888
left a comment
There was a problem hiding this comment.
Correct fix. The key mismatch is easy to miss because compression_stats() returns a dict that looks structurally similar to what cmd_compress expects — same rough shape, slightly different key names, no type error until the wrong key is accessed.
The test mock updates are exactly right: mock dicts should mirror what the real method returns, not what the caller expects.
One small observation: ratio → size_ratio is the only key rename that changed semantics slightly (ratio implies compression ratio; size_ratio is more precise). The others are just naming convention (_chars, _est suffix). Worth a brief comment in the mock explaining what size_ratio means (chars-before / chars-after) so future test authors don't confuse it with token ratio or compression speed.
Otherwise clean — the fix is minimal and targeted.
|
Clean fix. The key mapping table in the issue description is exactly what a reviewer needs — makes it unambiguous that the rename is one-to-one and the semantics are preserved. Two things worth noting for context:
LGTM otherwise. The [MemPalace-AGI integration — 540+ discoveries, 5,251 KG triples] |
|
Already fixed in #562 (commit |
|
Closing in favor of #569, which has the identical cli.py fix plus stronger test coverage (adds the "note" key to mocks and a new integration test verifying key parity with Dialect.compression_stats()). Thanks for the contribution! |
Summary
mempalace compresscrashes withKeyError: 'compressed_chars'becausecmd_compressincli.pyreferences stale dictionary keys that no longer match whatDialect.compression_stats()returnscli.pyand 2 test mock dictionaries intest_cli.pyto use the correct keys:summary_chars,original_tokens_est,summary_tokens_est,size_ratioKey mapping
compressed_charssummary_charsoriginal_tokensoriginal_tokens_estcompressed_tokenssummary_tokens_estratiosize_ratioRepro
Test plan
pytest tests/test_cli.py -k compress)mempalace compress --dry-runworks against a real palace with 21K+ drawers🤖 Generated with Claude Code