Skip to content

feat: scope, private memory per agent and per task (#119 H2) - #156

Merged
gosha70 merged 2 commits into
mainfrom
feat/119-h2-scope
Nov 24, 2026
Merged

gosha70 merged 2 commits into
mainfrom
feat/119-h2-scope

Conversation

@gosha70

@gosha70 gosha70 commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

The second slice of issue #119: H2, scope and abstention (FR-001, FR-005, SC-1, SC-4, SC-6), built as the approved amendment H2 (#155: B1, B2, B3, B5 confirmed, B4 revised by the owner). It leaves #119 open for H3 (the context diff).

What's in it

  • One rule (B5, B6). Caller(agent_id, task_id) in core/models.py sees project records and its own agent's and task's; a record without the scope key is project (SC-4). EVERYONE is store maintenance's and the operator's view. Applied in Python, after the store returns candidates.
  • Who the caller is (B7). MCP: recall, retain, get, ask, forget, promote, consolidate take agent_id/task_id (default MEMKERNEL_AGENT_ID/MEMKERNEL_TASK_ID); the hook blocks use the process's; MemoryService methods take caller= (default: the settings' ids).
  • Every read path (B8, SC-1). recall (both orders), get, ask, list_memories, consolidate's candidates, the session-start and compaction-recovery blocks (checkpoint, context, needs-review list, candidates), wiki_export (project only, B3). Another agent's record is not found (B2).
  • Writes (B1, B9). retain(scope="project"|"agent"|"task"), refused without the id. Dedup per scope (a pre-H2 record and a new project one are one record). promote keeps the source's scope; scope="project" publishes. A private record supersedes only its own scope (refused with an error otherwise); code's automatic supersede follows it (one live snapshot per source and scope). A supersede target the caller cannot see is ignored and its link dropped; a missing target keeps its link as before (health reports it dangling). Document parts carry no scope and resolve only to a summary the caller can see.
  • Checkpoints (B4, as the owner revised it). Automatic and implicit (retain(checkpoint=true) without scope) checkpoints go to the caller's task, else agent, else project; latest-checkpoint recovery (session start, compaction recovery, recall(order="newest")) reads only that scope, never falling back to project.
  • Store-wide (B8). The purge, revalidation, health counts, the unsaved-session report (a checkpoint in any scope counts), the repeated-mistake count, and the admin API.
  • Receipts (B10, B11). excluded adds out_of_scope and capped, each candidate counted once in that order; an empty recall records empty_reason (no_match, out_of_scope, excluded); an entry names its scope when it is not project. The MCP response is still a JSON list.
  • Docs. README: a "Scope: agents and tasks" section (including "a convention, not access control"), the ids guidance in "One store across tools" (same task id to continue work, same agent id to act as the same agent, ids in the environment of the server and the hooks, a new id starts empty, scope="project" hands off), the tool rows, config and recall-log rows. The Codex and Cursor templates say which ids carry a checkpoint. CHANGELOG under [Unreleased], with the behaviour change for anyone setting the ids.

Tests

tests/unit/test_scope.py (27) and tests/e2e/test_mcp_scope.py; each SC-1 read path has a test where agent a's private record is invisible to agent b.

Path or rule Test
recall relevance test_recall_returns_project_records_and_the_callers_own_only
get, ask, list_memories test_get_ask_and_list_do_not_show_another_agents_record
promotion candidates test_promotion_candidates_are_the_callers, test_another_agents_private_promotion_leaves_the_shared_source_a_candidate
session-start block, compaction recovery test_the_session_start_block_shows_only_what_the_process_may_see, test_compaction_recovery_reads_only_the_processes_effective_scope
wiki_export test_wiki_export_drafts_project_records_only_whoever_calls
every MCP tool, receipts tests/e2e/test_mcp_scope.py
B4 writes, recovery (no fallback), hooks test_a_checkpoint_goes_to_the_task_then_the_agent_then_the_project, test_latest_checkpoint_recovery_reads_only_the_effective_scope, test_the_hooks_recover_and_save_in_the_processes_effective_scope
B8 store-wide test_an_unsaved_session_counts_a_checkpoint_in_any_scope, test_the_purge_removes_expired_private_records_whoever_runs_it, test_stats_count_every_record
B1, B9 writes test_retain_names_only_the_callers_own_scope, test_dedup_is_per_scope_and_an_older_record_counts_as_project, test_promote_keeps_the_scope_and_publishes_only_when_asked, test_a_private_record_supersedes_only_its_own_scope, test_an_unseen_supersede_target_is_ignored_and_a_missing_one_keeps_its_link, test_forget_cannot_delete_another_agents_record, test_code_keeps_one_live_snapshot_per_source_and_scope, test_a_document_chunk_resolves_only_to_a_summary_the_caller_can_see, test_a_shared_body_is_found_through_each_owners_own_summary
B5, B10, B11 test_recall_widens_past_out_of_scope_candidates, test_each_exclusion_is_counted_once_in_order_and_capped_is_counted, test_an_empty_recall_says_why

The scope tests fail against two mutations: every caller seeing every record (12 fail) and retain never storing a scope (17 fail).

Checks

  • Fast suite: 1312 passed, coverage 95.05 % (gate 80 %). Slow suite (real stdio and HTTP transports): 13 passed on the final commit 00909d5.
  • ruff check, ruff format --check, mypy src/ strict, mypy --strict on the eval tools: clean.
  • Origin alignment: the 2026-11-19 record was stale after the two amendments (exit 4); re-reviewed against Feature: AI Harness Foundations — Scoped Knowledge, Reproducible Context, and Retrieval Observability #119 Part 1, no mismatch, new record origin-alignment-2026-11-23-2114-build-h2.md; gate passes.
  • DeepSeek, three rounds: PASS, 0 blocking. Acted on: the port docstring's scope filter, _dedup_hit's scope made required, and promoted sources counted only through copies the caller can see (a real gap, with a test). Its claim that document chunks take the first owner's scope was wrong (parts carry no scope); a test now shows each owner's summary finds a shared body.

For the reviewer

  • I fixed one regression of my own before review: a supersedes= naming a missing record had stopped keeping its link, which the health report counts (test_status_resource_carries_health_summary caught it).
  • Two H1 tests compared the exact excluded dict; they now include out_of_scope and capped (B10).
  • An empty order="newest" recall scans the checkpoints a second time, unscoped, to tell no_match from out_of_scope; only on an empty result.

Review round 1 (00909d5 → 8397301)

  • The leak the review found: consolidate(revalidate=True) returned every item the store-wide revalidation flagged, and the session-start block listed them, so agent b saw the source, type, reason and ref_id of agent a's private decision whose file changed. The listed items are now filtered by the caller (_visible_ref); the counts stay store-wide; the admin API's revalidate keeps the full list.
  • Test: test_flags_found_by_this_pass_are_shown_only_to_who_can_see_them (a private decision citing a file that changes: listed for agent a, absent from agent b's consolidate and session start; changed still counts it). It fails with the fix removed.
  • Fast suite 1313 passed, 95.06 %; lint, both mypy runs clean; DeepSeek PASS (an item without a ref_id is now skipped, not looked up).

A memory is project (shared, the default) or private to the caller's agent or
task: retain(scope="agent"|"task") with agent_id/task_id as tool arguments or
MEMKERNEL_AGENT_ID/MEMKERNEL_TASK_ID. One predicate decides visibility on every
read path: recall (both orders), get, ask, list_memories, consolidate's
candidates, the hook blocks; wiki_export drafts project records only. Older
records are project. Another agent's record is not found by get, ask, forget
and promote.

Writes: dedup per scope; promote keeps the source's scope, scope="project"
publishes; a private record supersedes only its own scope (explicit and code's
automatic supersede: one live snapshot per source and scope); a supersede
target the caller cannot see is ignored; document parts stay shared and
resolve only to a summary the caller can see.

Checkpoints without a scope go to the caller's task, else agent, else
project; latest-checkpoint recovery reads only that scope, with no fallback.
Store maintenance (purge, revalidation, health counts, the unsaved-session
report), the eval count and the admin API stay store-wide.

Receipts: excluded adds out_of_scope and capped; an empty recall records
empty_reason; an entry names a non-project scope. README: a Scope section and
how tools sharing a store share ids; the Codex and Cursor templates say so.

Spec: specs/memkernel-0-15-harness-foundations, amendment H2 (B1-B11).
…hem (#119 H2)

consolidate(revalidate=True) returned every item the store-wide revalidation
flagged, and the session-start block listed them, so agent b saw the source,
type, reason and ref_id of agent a's private decision whose file changed. The
listed items are now filtered by the caller; the counts stay store-wide and the
admin API's revalidate keeps the full list. Test: a private decision citing a
file that changes is listed for agent a and not for agent b.
@gosha70
gosha70 merged commit 4a5eba5 into main Nov 24, 2026
11 checks passed
@gosha70
gosha70 deleted the feat/119-h2-scope branch November 24, 2026 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant