Lab 03 — AgentCore Memory: Short-Term Events + Long-Term Strategies

Phase 20 · Lab 03 · Phase README · Warmup

The problem

An agent with no memory greets you as a stranger every turn. AgentCore Memory gives an agent two tiers, and the split is the whole design:

  1. Short-term memory is the current session's event log — every turn (user said X, agent did Y), appended in order, scoped to one session_id. It is what multi-turn context is built from. It is ephemeral: when the session ends, the raw turns are gone.
  2. Long-term memory is what persists across sessions. You don't keep raw turns forever; you run strategies that extract durable records from the event log into namespaces. AgentCore ships three built-in strategy types: summary, semantic (facts), and user_preference. Because records are keyed by actor (the user), memory is shareable across agents; because they live in long-term namespaces, they persist across sessions.

The extraction — turning turns into records — is model-driven in production. You inject it as a pure function, exactly the seam that makes a real memory pipeline testable: the store's mechanics are deterministic and independent of how clever the extractor is.

What you build

PieceWhat it doesThe lesson
create_event(session, actor, payload)append a turn to the session's short-term logshort-term = per-session event log
Strategy(name, namespace, extract)an injected extractor filed under a namespace templatestrategies turn turns into durable records
consolidate(session)run strategies over events, file records into namespaces (idempotent)"learn from experience," safely repeatable
retrieve(namespace, query, top_k)rank records by bag-of-words cosine relevancenamespace + relevance retrieval
Agent over a shared MemoryStoretwo agents share long-term namespacesmemory is shareable across agents
tokenize / cosinethe stdlib similarity signalthe retriever's shape, made visible

File map

FileRole
lab.pyyour implementation (fill the # TODOs)
solution.pyreference implementation + worked example (python solution.py)
test_lab.py25 tests: short-term isolation, strategy extraction, retrieval, cross-session, cross-agent
requirements.txtpytest only

Run it

pytest test_lab.py -v                       # your lab.py (red until you implement)
LAB_MODULE=solution pytest test_lab.py -v   # the reference (green)
python solution.py                          # worked example

Success criteria

  • create_event appends to a per-session log, and short-term memory is isolated per session.
  • consolidate runs each strategy and files extracted records into the right namespace, grouping by actor when the namespace template contains {actor}; re-running is idempotent.
  • retrieve ranks by relevance (cosine), tie-breaks deterministically, and excludes zero-similarity records.
  • Records persist across sessions and are shareable across agents (same store).
  • All 25 tests pass under both lab and solution.

How this maps to the real stack

  • Short-term vs long-term is AgentCore Memory's core split: short-term is the session's conversation/event history (retrieved for multi-turn context); long-term is durable, namespaced memory that survives session end. Your short_term (per-session event lists) and long_term (namespaced records) are those two tiers.
  • Strategies are the real feature name. AgentCore's built-in strategy types are summary, semantic (facts), and user_preference — exactly the three in default_strategies(). In production the extraction is performed by a model over the event log; you inject a pure function in its place (the Phase-00 "inject the model" seam), which is how you'd unit-test a real memory consolidation pipeline.
  • Namespaces organize and scope memory. Real AgentCore namespaces template on {actorId}, {sessionId}, and {strategyId} — your {actor}/{session_id} templates model exactly that, and the actor keying is what makes memory shareable across agents and persistent across sessions (Phase 04 context/memory).
  • Retrieval in production is vector similarity over learned embeddings; your bag-of-words cosine is the same shape (a normalized dot product, ranked) with a transparent signal, the same reduction used in Phase 05 retrieval. You test the ranking, not the embedding model.
  • Idempotent consolidation (dedup by namespace + content) is a real production property: memory extraction runs repeatedly as a session grows, and it must not duplicate what it already learned.

Limits of the miniature. Real Memory uses learned embeddings and a vector index (not bag-of-words), model-based extraction (not regex/count stand-ins), and managed durability and access control; ours is in-memory and deterministic. The architecture — two tiers, strategy-extracted namespaced records, relevance retrieval, cross-session/cross-agent sharing — is faithful; the storage and similarity engines are stand-ins.

Extensions (your own machine)

  • Swap the bag-of-words cosine for a hashing embedder or real embeddings (Phase 05) and confirm the retrieval interface is unchanged — proving similarity is a pluggable signal.
  • Add a recency/decay term to the retrieval score so newer records rank higher on ties.
  • Add a branch/merge of long-term memory across two agents and reconcile conflicting preferences (last-write-wins vs a merge strategy).
  • Model short-term summarization into long-term on a token-budget trigger (Phase 04): when the event log grows past a budget, consolidate and truncate.

Interview / resume signal

"Built a miniature Bedrock AgentCore Memory: a per-session short-term event log plus long-term strategy extraction (summary / semantic facts / user_preference) into actor-keyed namespaces, with idempotent consolidation and relevance retrieval — proving memory that persists across sessions and is shareable across agents, with the model-driven extraction injected so the pipeline is deterministic and testable."