Skip to main content

Goal and prerequisites

The notes Agent needs project instructions and remembered facts. Bind concrete implementations in extensions, then select memory.sources: [project_memory] and compaction.provider: closed_window. MemorySourceAdapter works with both MemdirMemory and MarkdownFileMemory; users do not construct contributions, provider messages or compaction receipts. This fixture makes four scripted tool calls (two passes over the two notes), then finishes. ClosedExchangeWindowCompactor omits the oldest eligible closed exchanges without a summary only when the request exceeds its configured budget. ContextBudget.protected_recent_exchanges=2 protects the recent window. Without an explicitly selected compactor, history overflow fails instead of silently dropping exchanges. The public YAML loader accepts context.budget_policy: budget and context.allow_codec_loss: true. Bind budget explicitly through extensions to a context budget policy factory. Loss must be a real boolean; unknown fields and unbound factories fail before the first model request. The complete tutorial below also demonstrates Python composition of other context services; the installed B consumer uses these options directly in YAML. Basic Python is required. The package declares Python ≥3.10; local qualification uses Python 3.12.7. Each chapter runs independently; reuse the environment and matching files when continuing your project. Commands below use a macOS/Linux shell.

Prepare the project

This lesson uses the integrated R1 implementation pinned below; a source install does not imply a PyPI release. Save the complete files below in this directory. No repository clone, editable install, or copied tests are needed.

Run and verify

Expected output fragments (generated IDs vary). Each run verifies tool results or persistence assertions and exits 0 on success.

Behavior and support boundaries

Memory is request context data at user placement, not a system instruction, permission or checkpoint store. The namespace is a logical label bound to a memory resource by the application factory; it is not permission to read arbitrary directories. No global memory is read by default. Memdir text records retain an independent record_id across processes. Equal text in two independent records remains two records; content edits change the contribution revision/digest. Recall reads disk afresh rather than merging stale append-time records. External edits/deletions are reflected in retrieval and summary. Host paths are not added to contribution IDs, content or metadata. Use create=True only for explicit initialization; normal construction restores an existing root and raises MemoryResourceError when it is missing. This changes the old implicit-create default, including memdir_memory(...). The adapter borrows the memory. Adapter reset/close do not reset, close or delete it. Memdir reset/evict affect only its in-memory cache, not persistent files. Text records are the durable reference; arbitrary Python/JSON records or historical metadata are not a lossless storage promise. MarkdownFileMemory works within its current instance, but a fresh instance does not reload its append-only log. No metadata evaluation or guessed reconstruction occurs. Selection never mutates ExchangeLog bytes or tool completion order. Tool declarations/results are retained or omitted together. Incomplete exchanges, pending steering and required context/artifacts stay protected; opaque continuation state conservatively protects the complete history because no smaller dependency closure is declared. If protected content cannot fit, selection raises a typed failure. Recalling an unchanged revision after an earlier omission or restore injects it again. The installed two-process consumer seeds remembered-value=17, exits, restores the configured memory and verifies actual encoded requests and repeated budget compaction. It uses a deterministic provider, not a live model. Legacy CompactHistory remains compatible. Markdown durable reading, model summaries, complete legacy-history retirement, cross-Agent memory policy and original-Agent migration experiments remain out of scope.

Exercise and answer

Edit the persisted text without changing its record ID, then verify its revision changes. Restore the same resource in a second process; a different factory-bound namespace should stay empty.

Common errors and cleanup

ModuleNotFoundError: activate the environment with the specified installation and save every file on this page. Existing run root: choose a new --root instead of overwriting evidence. Assertion failure: inspect the first failed tool or typed error, not only the final text. After all processes and the board stop, remove only this lesson’s newly created run directories if no longer needed; retain SQLite, journals and reports you want to debug.

Complete files: save in the project root

notes.py
agent.yaml
context.py

Next step and API

API Reference · Configuration · Learning path · Next Source file (optional; all required code is already on this page).