Шаг 7 из 8 - Около 30 мин
Lab 06 - Compaction
Time: about 30 minutes. You will: mark entries stale when the files they name are gone, find near-duplicates, and see why the cleanup pass never lets a model rewrite an entry.
Why
A memory store that only grows becomes the problem it was meant to solve. Files get renamed and the entries that point at them keep giving confident, wrong advice. Duplicates split one lesson's evidence. The index creeps toward its budget. The fix is a regular pass that is cheap, repeatable, and reviewable as a plain diff.
Concepts
Verify: paths are the cheapest freshness signal
flywheel.py verify reads each active entry, collects the repo-relative paths in backticks
(they must contain a slash and end in a known extension or /), and checks that they exist.
| Line | Meaning | With --apply |
|---|---|---|
STALE |
A named path is missing | status: stale |
OK |
All named paths exist | last_verified set to today |
REVIEW |
No checkable paths and last_verified older than 90 days |
nothing; a human decides |
Bare file names like SKILL.md are ignored: they are ambiguous and gave false stale results while
the command was built (.flywheel/memory/verify-ignores-bare-file-names.md).
Dupes: cheap similarity, human judgment
flywheel.py dupes scores pairs of active entries by token Jaccard similarity (shared words over
all words, stopwords removed) and lists pairs at or above dedup.jaccard_threshold (0.5). Token
overlap is a hint, not a verdict: the flywheel-curator agent reads both entries and decides
whether they record the same cause and the same resolution.
Merge by fixed rules, never by rewriting
For a confirmed pair the representative is chosen by rules, in order: more occurrences, then the
more recent last_verified, then the longer body, then the name that sorts first. The other entry's
sources move to it with add-source, and the other entry is superseded. No text is summarized by
a model, so the same input gives the same result and the diff shows exactly what happened.
Contradictions and skill retirement (status: deprecated plus deprecated_reason) need a human.
Build
.claude/skills/compact-memory/SKILL.md- the pass: curator analysis, verify, duplicates, contradictions, retirement, regenerate, review the diff..claude/agents/flywheel-curator.md- read-only analysis (tools: Read, Grep, Glob, Bash); it proposes commands and changes nothing..flywheel/bin/flywheel.py-referenced_paths(),verify_entries(),find_duplicates().openspec/changes/add-compaction/specs/compaction/spec.md.
Run
python3 .flywheel/bin/flywheel.py verify
python3 .flywheel/bin/flywheel.py dupes
python3 .flywheel/bin/flywheel.py statusOK flywheel-config-is-json: 2 path(s) exist
OK openspec-config-rules-quote-colons: 2 path(s) exist
OK openspec-init-installs-skills-machine-wide: 4 path(s) exist
OK verify-ignores-bare-file-names: 2 path(s) exist
no near-duplicates at threshold 0.5Your lab 02 entry is not listed by verify if it names no paths (or if lab 05 promoted it: only
active entries are verified).
Needs Claude Code and a model: ask Claude Code to "run the compact-memory skill". It delegates the
analysis to the flywheel-curator agent, shows you a report (duplicates, contradictions, stale and
review, skills), and applies only what you approve. It ends with git diff --stat, which is why
lab 00 made a baseline commit.
Break it
Delete the file an entry depends on. Create an entry that names a path, then verify:
python3 .flywheel/bin/flywheel.py new checkout-client-retries --type observation \ --description "The checkout client retries twice; check its retry settings before changing timeouts." \ --source add-checkout-retryFill in the body and put
`src/checkout/client.py`under## Where(the file does not exist in this repository, which plays the part of a deleted file). Then:python3 .flywheel/bin/flywheel.py verify --applySTALE checkout-client-retries: missing src/checkout/client.py OK flywheel-config-is-json: 2 path(s) exist OK openspec-config-rules-quote-colons: 2 path(s) exist OK openspec-init-installs-skills-machine-wide: 4 path(s) exist OK verify-ignores-bare-file-names: 2 path(s) exist verify: updated 5 entriesThe count is the stale entry plus the four entries whose
last_verifiedmoved to today (an entry already verified today is not counted again). The entry is nowstatus: staleand gone from the index;checkstill passes.Lower the threshold until it lies. Run
python3 .flywheel/bin/flywheel.py dupes --threshold 0.1:0.129 flywheel-config-is-json openspec-init-installs-skills-machine-wide 0.109 flywheel-config-is-json verify-ignores-bare-file-names 0.103 flywheel-config-is-json openspec-config-rules-quote-colons(That run had the lab 02 entry promoted; if yours is active, it may add pairs.) None of these is a duplicate: a JSON config decision and an OpenSpec install pitfall share a few words, not a lesson. That is why a human or the curator confirms every pair, and why merging is never automatic.
Undo the break-its but keep your entries from labs 02 to 05, which the exercises use:
rm .flywheel/memory/checkout-client-retries.md
python3 .flywheel/bin/flywheel.py checkWhen you finish the exercises, git checkout -- . && git clean -fdq resets the lab to the
baseline commit (it removes everything you added since lab 00).
Check
python3 -m unittest tests.test_flywheel.TestCompaction -vtest_verify_marks_stale, test_bare_names_not_stale, test_referenced_paths, and test_dupes.
Exercises
- Make an entry show up as
REVIEW: an entry with no backticked paths andlast_verified: 2026-01-01.verifyprintedREVIEW old-note: no checkable paths, last verified 325 days agofor such an entry on 2026-11-22. - Apply the representative rules by hand to the pair from lab 03
(
sandbox-timeout-needs-idempotency-keyandrefund-retry-duplicated-refund). Which rule decided it? - Retire a skill: add
status: deprecatedanddeprecated_reason: <why>to the frontmatter of theidempotent-retriesskill from lab 05 and runcheck. Then trystatus: retired. What doeschecksay, and why does the constitution say to deprecate instead of delete?
Exam connection
CCA-F - Context Management & Reliability (keeping the always-loaded context small and current, stale-context detection, deterministic and auditable maintenance). Practice it on CertLabPro. Knowledge freshness, deduplication, and retirement in agent memory are also part of the Knowledge Integration and Data Handling and the Cognition, Planning, and Memory domains of NCP-AAI.
Previous: Lab 05 - Promotion - Next: Lab 07 - Install into another repository