Skip to content

memory.ask and memory.search read a run's working-memory notes only inside the request's read scope - #196

Merged
zlareb1 merged 7 commits into
mainfrom
fix/ask-run-notes-read-scope
Nov 3, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
fix/ask-run-notes-read-scope

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (data loss, cross-tenant read, auth bypass)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

memory.ask and memory.search with a run_id no longer return another user's working-memory notes when the token is bounded to one user or session.

Why

Failing case: a token with read_scope=own_user (or a read_user_id pin) for u1 names the run_id of a run u2 wrote working-memory notes under.

  • memory.ask: render_run_blocks read loom.working_memory by (org, namespace, run_id) with no user or session predicate, so u2's notes went into the synthesis prompt.
  • memory.search (and context.pack through it): scratchpad.load read the run's blocks the same way and returned them in scratchpad.

Pattern: working_memory.get and working_memory.render check the request's read scope; the two run-notes readers did not. The read-scope section of the identities concept page already lists working memory among the reads that carry the scope.

Fix: every working-memory read in server/memory/app/scratchpad.py goes through _select, which appends the request's read-scope predicates (scope_and(request_scope(), params)). run_rows is the run read and _session_rows the session read. render_run_blocks drops its own statement and reads through run_rows, so memory.ask and memory.search share one scoped run read. Under namespace nothing is added, so unbounded tokens see the same notes as before. A scope that does not resolve raises before any statement runs: memory.ask refuses the call with that error (it resolves the scope before its fail-open read, so a refusal is not logged as a failed read), and memory.search drops the notes, as it does on any scratchpad read error.

Scope and risk

Touches the working-memory reads in server/memory/app/scratchpad.py and server/memory/app/working_memory.py, and the Limits section of docs/features/memory/working-memory-and-profiles.md, which said blocks never appear in memory.search or memory.ask. Tenant isolation tightens; no write path, schema or wire change. Under own_session, a run's notes written with session_id='-' (as the plugin writes them) are no longer served, even to the run's own user, which matches working_memory.get under that scope. memory.ask's run notes are now ordered updated_at DESC, block_key (ties broken by key), as memory.search's already were.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: reverting restores the unscoped reads; nothing is stored.

Tests

tests/unit/test_ask_run_scratchpad.py:

  • test_run_notes_stay_inside_a_narrowed_read_scope seeds one run with notes from u1 and u2, binds an own_session scope for u1, and checks that render_run_blocks and scratchpad.load return only u1's note, while the whole-namespace read still returns both.
  • test_a_refused_read_scope_reads_no_run_notes binds a refused scope and checks that render_run_blocks and scratchpad.load both raise it and that no statement runs.

Both fail with the scope line removed. Locally: tests/unit/test_ask_run_scratchpad.py, test_scratchpad_inject.py, test_pack_agent_working_memory.py, test_working_memory_ops.py, test_package_contracts.py and the doc and copy checks pass; make format lint typecheck clean. CI runs the full suite.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The failing case was checked against the code and the new test fails before the fix.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 2 commits November 3, 2026 09:54
…'s read scope

render_run_blocks read loom.working_memory by (org, namespace, run_id) alone,
so a token narrowed to one user that named another user's run_id got that
user's notes in the synthesis prompt. The statement now carries the request's
read-scope predicates, as working_memory.get and working_memory.render do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…est's read scope

scratchpad.load's run-scope read had the same gap as memory.ask's: no user
or session predicate. The read-scope predicates now go on the one statement
behind scratchpad.load, and render_run_blocks reads through it
(scratchpad.run_rows) instead of keeping its own statement. The tests seed
one run with two users' notes and check what each reader returns, and that
a refused scope reads nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1 zlareb1 changed the title memory.ask reads a run's working-memory notes only inside the request's read scope memory.ask and memory.search read a run's working-memory notes only inside the request's read scope Nov 3, 2026
zlareb1 and others added 5 commits November 3, 2026 11:18
One function owns the run-scope read, so no call site passes identity
arguments the run statement ignores. The refused-scope test covers
memory.search's reader too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…oped select

run_rows names only the predicates the run read has; the session read keeps
its identity axes. Both go through _select, which adds the request's read
scope. The working-memory page says which reads carry blocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…un-notes read

render_run_blocks resolves the request's read scope before its fail-open
read, so the refusal reaches the caller as the scope error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 7071bf3 into main Nov 3, 2026
22 checks passed
@zlareb1
zlareb1 deleted the fix/ask-run-notes-read-scope branch November 3, 2026 17:57
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
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