第 3 步,共 8 步 - 约 35 分钟
Lab 02 - The memory store
Time: about 35 minutes. You will: create a memory entry with the CLI, see how the index is
generated and budgeted, and learn which mistakes check refuses.
Why
An agent forgets everything between sessions. Anything it should know next time has to be written down and loaded again. Free-form notes do not survive that: they grow, go stale, contradict each other, and nobody can tell which ones still matter. The memory store makes each lesson a small, validated file with its lifecycle in fields, and keeps only a one-line summary in the context that is loaded on every request.
Concepts
One file per lesson
---
name: sandbox-timeout-needs-idempotency-key
description: "Retry payment sandbox timeouts only with an idempotency key, or charges can be duplicated."
type: observation # user | feedback | project | reference | observation | decision
scope: local # local | shared
status: active # active | promoted | superseded | stale
sources: [add-checkout-retry] # OpenSpec changes where the lesson was seen
occurrences: 1 # must equal the number of distinct sources
last_verified: 2026-11-22
provenance: {by: agent, date: 2026-11-22}
links: []
---
## What happened
## Why
## Resolution
## Wheredescriptionis the recall line. It says when the lesson matters, not the topic.typedecides what can be promoted later: onlyobservation,feedback, anddecision.occurrencesis derived.checkrejects any value other than the number of distinct sources, so one change can never count twice toward promotion.- Status lives in fields, never only in prose ("Lifecycle in fields" in
constitution.md).
The index is generated, grouped, and budgeted
MEMORY.md has one line per active entry, grouped by type:
- [name](name.md) - descriptionPromoted, superseded, and stale entries stay on disk for provenance but cost no tokens. The budget
(index_budget.max_lines 150 and max_bytes 20000 in .flywheel/config.json) is a hard limit:
check fails when the index is over it.
Writes are all-or-nothing
new, add-source, set-status, and verify --apply read every entry before they write
anything. A malformed entry stops them with exit code 2 and no file changed. Values the CLI writes
into frontmatter are quoted when they would otherwise be read differently (the description above has
a comma, so it is quoted), with quotes and backslashes escaped.
Build
.flywheel/templates/memory-entry.md- the entry template..flywheel/bin/flywheel.py:parse_frontmatter()andformat_scalar()- a deliberately small YAML subset, so the CLI needs no third-party library (the decision is recorded in.flywheel/memory/flywheel-config-is-json.md);check_entry()- every rule an entry must pass;render_index()andrun_check()- the index and its budget;cmd_new(),preflight()- creating an entry without breaking the store.
openspec/changes/add-memory-store/specs/memory-store/spec.md- the requirements this code meets.
Run
python3 .flywheel/bin/flywheel.py new sandbox-timeout-needs-idempotency-key \
--type observation \
--description "Retry payment sandbox timeouts only with an idempotency key, or charges can be duplicated." \
--source add-checkout-retry
python3 .flywheel/bin/flywheel.py checkcreated .flywheel/memory/sandbox-timeout-needs-idempotency-key.md (fill in the TODO sections, in your own words)
WARN .flywheel/memory/sandbox-timeout-needs-idempotency-key.md: body still has TODO placeholders from `new`; record the lesson in your own words
check: 0 error(s), 1 warning(s)Open the new file and replace the four TODO lines with the lesson in your own words, for example:
"A checkout call to the payments sandbox timed out. A plain retry created a second charge." (What
happened), "The sandbox had processed the first request; only the response timed out." (Why), "Send
an idempotency key with every payment request and reuse it on retry." (Resolution). Leave paths out
of ## Where for now and name the place in words instead (for example "the checkout retry
code"); lab 06 shows what the verify pass does with backticked paths. Run check again: 0 errors,
0 warnings. Then look at the new line in .flywheel/memory/MEMORY.md.
Keep this entry: labs 03, 05, and 06 build on it.
Break it
Count a change twice. Edit the entry so
sources: [add-checkout-retry, add-checkout-retry]andoccurrences: 2, then runcheck:ERROR .flywheel/memory/sandbox-timeout-needs-idempotency-key.md: occurrences is 2 but sources has 1 distinct changes check: 1 error(s), 0 warning(s)Put it back to one source and
occurrences: 1.Shrink the budget. Set
"max_lines": 10underindex_budgetin.flywheel/config.json:ERROR .flywheel/memory/MEMORY.md: index is over budget (15 lines / 1182 bytes; max 10 / 20000); run the compact-memory skillstatusshows the same numbers asindex: 15/10 lines. Set it back to 150.A malformed entry. Create a broken file and try to add another entry:
printf -- '---\nname broken\n---\nx\n' > .flywheel/memory/broken.md python3 .flywheel/bin/flywheel.py new another-lesson --type user --description "A fact."error: .../.flywheel/memory/broken.md: unsupported frontmatter line: 'name broken'(Path shortened.) The exit code is 2 and
another-lesson.mdwas not created. Deletebroken.md(rm .flywheel/memory/broken.md).
Check
python3 -m unittest tests.test_flywheel.TestFrontmatter tests.test_flywheel.TestMemoryStore -v
python3 -m unittest tests.test_flywheel.TestCommands.test_broken_entry_stops_writes_before_anything_changes tests.test_flywheel.TestCommands.test_new_round_trips_hash_and_quotes_in_description -v
python3 .flywheel/bin/flywheel.py checkTestMemoryStore covers the entry rules (test_occurrences_must_match_distinct_sources,
test_index_over_budget, test_index_lists_only_active_grouped_by_type, and more). The two
TestCommands tests prove the all-or-nothing writes and the quoting round trip.
Exercises
- Create an entry whose description starts with
#and contains a double quote. Read the file: how did the CLI quote it? Runcheck. - Write a
decisionentry for a choice you made in a real project: the question, the decision, the reason, and aRejected:line. Which--sourcedid you use when there was no OpenSpec change? (Thecapture-observationskill says:session-YYYY-MM-DD.) - Why does the index group by type instead of by date? Write one sentence for each answer you consider.
Exam connection
CCA-F - Context Management & Reliability (what goes into the always-loaded context, a hard budget for it, structured memory with lifecycle fields) and Tool Design & MCP Integration (a CLI the agent calls with clear errors and exit codes: 0 ok, 1 check failed, 2 refused). Practice both on CertLabPro. Structured agent memory is also part of the Cognition, Planning, and Memory domain of NCP-AAI.
Previous: Lab 01 - Plan with OpenSpec - Next: Lab 03 - Capture