Skip to content

Return use guidance with retrieved knowledge - #1510

Open
krishnadobhal wants to merge 144 commits into
ClickHouse:mainfrom
krishnadobhal:feat/retrieved-use-guidance
Open

krishnadobhal wants to merge 144 commits into
ClickHouse:mainfrom
krishnadobhal:feat/retrieved-use-guidance

Conversation

@krishnadobhal

@krishnadobhal krishnadobhal commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #1230

Changelog category

  • New Feature

Changelog entry

Loom suggests how to use retrieved memories, documents, procedures, and code lessons. Dashboard search shows the suggested step and the reasons behind it.

Why

A relevant result can still be out of date, disputed, or waiting for review. Loom now uses the lifecycle, review, freshness, and dispute evidence available to the caller to recommend what to do with the result.

Scope and risk

  • Search checks the current revision and caller access before serving parked hits. A replacement with the same version and different content cannot inherit the parked hit’s guidance. Older cursors without a content hash cannot inherit review approval.

  • If search replaces a hit’s text with a source passage, it drops the original guidance. An expanded memory body keeps guidance only when its version and content hash match.

  • memory.verify now mirrors the exact attestation version to embeddings that still match the reviewed content. The mirror runs asynchronously, and search still requires matching revisions before attaching review evidence.

  • Postgres-backed stacks check the authoritative head before serving pinned or recent entries from ClickHouse. File recall, code lessons, and stacks account for procedure status, including quarantine.

  • Source freshness respects caller access. A readable citation without a recorded source revision stays unknown. Context packs attach guidance to the citations they render.

  • Guidance on a code lesson is positive only when its memory-code link was checked at the repository's checked head, or covered by a recheck pass. A link checked at an older commit counts only when that commit is an ancestor of the checked head, so a branch rewind or force push reads unknown. Each request checks at most 8 distinct commits, 4 at a time, within 0.1 seconds. Search keeps its own 0.2 second limit. A commit over the cap or past the deadline reads unknown, and only memories that could still earn positive guidance spend a check.

  • After the checked head moves, guidance on links the pass has not yet covered reads unknown until the next recheck pass covers them.

  • The dashboard treats each memory as a writer-scoped revision. Search, list and Recall Lab resolve the agent, user, session and namespace before opening a memory, and the inspector rejects a namesake returned by memory.get. memory.list reports rows_read for paging, and the dashboard list requests 14 fields, with version_text replacing the numeric version.

Compatibility, rollout and rollback

  • memory.search, memory.for_file, knowledge.search, procedure.match, and memory.stack add optional use_guidance. search.query types its memory and knowledge sections and preserves guidance in compact MCP responses. Reader blocks can include the suggested step.
  • context.pack returns guidance keyed by section, citation, and item index. Code lessons add optional guidance in code.callees, code.callers, code.context_pack, code.enclosing, code.grep, code.impact, code.outline, code.read, code.search, and code.symbol.
  • The code operations listed above now return typed CodeLesson rows in the Python and TypeScript SDKs. In Python, pass dict(row) to helpers that require dict[str, Any], or change read-only helpers to accept Mapping[str, Any]. In TypeScript, use row[key] with a key typed as keyof CodeLesson. For an arbitrary string key, copy the row into const fields: Record<string, unknown> = { ...row }, then narrow fields[key] before using it. Rust lesson rows remain maps.
  • Generated clients include the new types and accept responses from older servers that omit guidance.
  • Migration 118_memory_code_link_covered.sql adds memory_code_links.covered (UInt8, default 0). It needs no operator step and adds no setting. Existing rows start uncovered, and the next recheck pass covers them. During a rolling upgrade, a previous-build writer inserts links without the column and without the repository's recheck lease. Guidance on those links reads unknown until a pass covers them (rolling upgrades).
  • Reverting removes the guidance fields and dashboard labels. The covered column can stay; older builds ignore it.

Tests

  • On the current head, 366 focused unit tests pass for code-plane reads, memory-code links, the list projection and the analyzer corpus. The 287 public-copy and doc-link tests and 159 dashboard tests for Recall Lab, the memory inspector and recent memories also pass, along with Ruff, mypy --strict on the Python SDK and every codegen --check script. The Python tests ran in WSL.
  • Unit tests cover the ancestry cap, the deadline under search's outer limit, cancellation of slow checks, and a memory owed a recheck spending no check.
  • The Recall Lab and Back-navigation Playwright specs were updated but not run locally.
  • Locally, test_no_new_tunable_constant fails on a constant in server/code/domain/index/test_gate.py, a file this PR does not touch. The sender-census test fails only because of CRLF line endings in the Windows checkout. CI covers both.
  • Earlier live probes passed for parked-page revocation and erasure, withholding freshness for unreadable sources, compact MCP guidance, and review becoming stale after a content edit. Those probes and the live lifecycle test have not been rerun on the current head. Live Postgres and dashboard checks are pending.

AI assistance in the code

  • AI-generated. I designed and reviewed the change. Codex assisted with implementation and testing.

Checklist

  • Local format, lint, type checks and focused tests pass.
  • Applicable generated files were regenerated and checked.
  • Frontend build and tests pass on the submitted commit.
  • Docs describe use guidance, and this description names the operator steps and wire changes.
  • Commits are signed off.

Note

Medium Risk
Changes how memories are opened and trusted in the UI (exact writer/revision matching); incorrect resolution could block opens or show wrong rows, while the covered link flag affects guidance correctness during rolling upgrades.

Overview
This PR documents and surfaces use_guidance on maintained search and context results: docs define the next-step rules (inspect, recheck, review, validate, follow), and the dashboard shows suggested use in header search, Recall Lab, and the “Why this result” popover via GuidanceLabel.

Writer-exact memory opening is the other major theme: search hits that lack a full agent / user / session / version_text identity are resolved with memory.list prefix paging (rows_read, namespace version stability) or stay preview-only; MemoryInspector with verifyWriter rejects memory.get namesakes. Navigation history keeps inspectedScope (namespace + exact flag) for reload/Back.

Ops/docs add memory–code link covered semantics for recheck passes and rolling upgrades (migration 120_memory_code_link_covered.sql). API reference types are tightened (CodeLesson, SearchSection, ContextUseGuidance, rows_read). The frontend splits lazy API helpers (memoryReads.ts, admin grants) and adds deleteMemoryVersion; e2e mocks gain version_text and writer fields.

Reviewed by Cursor Bugbot for commit 788cece. Bugbot is set up for automated code reviews on this repo. Configure here.

Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>
Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>
Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>
@cla-assistant

cla-assistant Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 3, 2026 9:34am UTC
loom-telemetry Ready Ready Preview Dec 3, 2026 9:34am UTC

Request Review

Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>
@krishnadobhal
krishnadobhal force-pushed the feat/retrieved-use-guidance branch from cd4010a to e78e001 Compare November 26, 2026 05:52
@krishnadobhal
krishnadobhal marked this pull request as ready for review November 26, 2026 05:52
Copilot AI balanced review requested due to automatic review settings November 26, 2026 05:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/memory/app/use_guidance.py Resolved
Comment thread server/memory/app/search/output.py Resolved
Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/use_guidance.py Resolved
Comment thread server/memory/app/stack.py Resolved
Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head drops eligible historical results from continuation pages and can give search and file recall different review states for the same attested content. The inline findings cover these regressions and other read and client paths. The existing source-freshness thread also still applies. Source freshness discussion

Verified against the exact head by tracing the read paths and checking the existing regression assertions. Full integration results are pending.

Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/code_link_reads.py Resolved
Comment thread server/memory/app/use_guidance.py Resolved
Comment thread server/memory/app/path_lessons.py Resolved
Comment thread server/memory/app/for_file.py Resolved
Comment thread frontend/src/components/SearchWhy.tsx Resolved
Comment thread server/code/api/models_common.py Resolved
Comment thread sdk/python/loom/memory.py Resolved
… content

Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/handlers.py Resolved
Comment thread server/memory/store/pg_memory.py Resolved
@zlareb1

zlareb1 commented Nov 26, 2026

Copy link
Copy Markdown
Member

plz fix merge conflicts

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head returns expired facts on as_of continuation pages. The history lookup failure and Postgres history metadata also need changes before this can merge.

Comment thread server/memory/app/search/guidance.py Resolved
Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread server/memory/app/search/output.py Resolved
Comment thread frontend/src/views/RecallLab.tsx Resolved
Comment thread server/memory/app/context_pack.py Resolved
Signed-off-by: Krishna Dobhal <107910364+krishnadobhal@users.noreply.github.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The head fixes the live-row expiry check. An edited historical revision can still pass after its own expiry, and memory.touch skips the expiry mirror for rows written before content hashes. Both cases were reproduced against this head.

The format job also fails on docs/features/memory/history-and-provenance.md. Format job

Comment thread server/memory/app/search/guidance.py Resolved
Comment thread server/memory/app/handlers.py Resolved
Comment thread server/transport/mcp_tools.py Resolved
@zlareb1

zlareb1 commented Nov 26, 2026

Copy link
Copy Markdown
Member

The tunable-constant check reports the same five constants on this PR and on clean main. The check passes with the tracked repair. Failed job · Repair #1503

Comment thread server/memory/store/history.py Resolved
Comment thread sdk/python/loom/memory.py Resolved
Comment thread server/memory/app/use_guidance.py Resolved
zlareb1
zlareb1 previously approved these changes Dec 2, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both stack queries now exclude ended validity intervals, and foreign ranked previews use their own conflict status. All 18 focused stack and analyzer tests passed. The dashboard adds a regression for a local conflict and a foreign result with the same key.

@krishnadobhal

Copy link
Copy Markdown
Contributor Author

@zlareb1 thanks for the approval. Could you merge this at b741d21 while it is still mergeable? Main moves fast and each upstream merge has meant renumbering the migration and regenerating the SDKs, so a later merge would likely conflict again.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub reports conflicts with main in sdk/go/wire/models.gen.go and tests/analyzer/shapes.txt. Reconcile the combined wire schema and regenerate the model and analyzer corpus.

Regenerate sdk/go/wire/models.gen.go with sdk_go_gen.py and
tests/analyzer/shapes.txt with tests.analyzer.corpus --write, taking
upstream's copies of both first. The shapes regeneration ran without a
ClickHouse server; --check was not run.

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
@krishnadobhal

Copy link
Copy Markdown
Contributor Author

Merged upstream/main (b84b355) into this branch in a44e835. Two generated files conflicted: sdk/go/wire/models.gen.go and tests/analyzer/shapes.txt. I took upstream's copy of each and regenerated it. The Go wire file now matches sdk_go_gen.py --check. The analyzer shapes file came from tests.analyzer.corpus --write, which ran without a ClickHouse server, so the EXPLAIN check was not run.

zlareb1
zlareb1 previously approved these changes Dec 2, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The combined Go models and analyzer corpus match their generators. All 66 focused guidance, stack, and analyzer tests passed. GitHub reports a new conflict in the generated Go and Rust wire models. Reconcile the schema with main and regenerate both models.

Take upstream's generated Go and Rust wire models and regenerate them with
sdk_go_gen.py and sdk_rust_gen.py so they match the merged operation registry.

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
@krishnadobhal

Copy link
Copy Markdown
Contributor Author

Merged upstream/main (3251ba1) into this branch in 7c39cdd. The only conflicts were the generated wire models, sdk/go/wire/models.gen.go and sdk/rust/src/wire_models.rs. Both took upstream's copy and were regenerated with scripts/codegen/sdk_go_gen.py and scripts/codegen/sdk_rust_gen.py, so they match the merged operation registry. The generated Go client and Rust operation files changed with them. The schema source had no conflict. Every codegen --check passes.

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same-key shortcut leaves a ranked hit in preview even when its revision identifies one keyword row. GitHub reports conflicts with main in the context pack, the for-file handler, and the evidence pack.

Comment thread frontend/src/views/RecallLab.tsx Resolved
@krishnadobhal

Copy link
Copy Markdown
Contributor Author

Merged upstream/main twice: 3b0af47 (upstream #2026, which conflicted) and 959dc4d (up to #2030, no conflicts).

#2026 conflicted in server/memory/app/context_pack.py, server/memory/app/for_file.py and server/memory/domain/evidence_pack.py. I took its label changes (_line, _writer_label, _shown_state, _block_text) and kept this PR's use_guidance field on the evidence item.

In _status_notes the branch's rule stays: " (source changed)" prints only when use_guidance.freshness.source == "changed", so a decision whose source is unreadable is not marked. The SOURCE_CHANGED_TAG check from #2026 is not used there.

The focused unit tests, ruff, mypy and all codegen --check scripts pass.

…guidance

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
@krishnadobhal
krishnadobhal force-pushed the feat/retrieved-use-guidance branch from 959dc4d to 33b581b Compare December 2, 2026 16:45
@krishnadobhal

Copy link
Copy Markdown
Contributor Author

The second merge commit was missing its sign-off, so I amended it: 959dc4d is now 33b581b, with the same tree.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

# does not attest the shared copy and may not be readable here.
evidence[i] = {"status": "active", "source_class": hits[i].source_class}
return denied, evidence

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared parked hits skip validity checks

Medium Severity

_shared_evidence treats a shared parked hit as authorized when the live version number still matches. It does not apply the expiry, valid_to, or content-hash checks that _memory_evidence uses before serving a parked fact. An expired, ended, or same-version replacement of a shared row can therefore stay on a parked page and keep its body, instead of being dropped or stripped of inherited guidance.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 33b581b. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a5b30e1. _shared_evidence now uses the same expiry check (_current) and the same served-body check (_digest_diverges) as _memory_evidence. The shared head reads the body and hashes it as the promotion source does. Tests cover an expired row, an ended valid_to and a same-version body change, on both a fresh and a parked page.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9e9d84f. The shared lookup now records the served digest on the live pass, so a parked shared hit comes back with it and a same-version replacement is dropped. The test now gets its digest from a real live pass and a page round trip.

I dropped the expiry and valid_to cases: loom.shared has neither column, so they only passed because the test put them in the head.

…ld at two revisions merges into the row at the hit's revision

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
… and holds the body it served

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
…a same-version replacement is caught

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

There are 23 total unresolved issues (including 21 from previous reviews).

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 9e9d84f. Configure here.

hash_text(row.get("content_sha256")), _served_digest(hits[i], parked)
)
):
denied.add(i)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared digest mismatch drops live hits

Medium Severity

_shared_evidence treats any body-hash mismatch as an immediate deny, before it checks whether the head is still that hit’s revision. _memory_evidence only uses _digest_diverges after the version or body already matched, so a newer head with different content still serves the live hit without that head’s review. A re-promoted shared row can therefore vanish from live search while a private memory in the same situation stays visible.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9e9d84f. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b482833. _shared_evidence now shares _head_holds_hit with _memory_evidence: the digest check applies only when the head is the served revision, so a newer head with other content keeps a live hit served without that head's guidance. A parked page still drops it, as it does for a private row.

"content_sha256": sha256_value(served_row_value(r)),
}
for r in rows
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falsy shared values fail digest check

Medium Severity

guidance_heads hashes shared rows with served_row_value, which returns None for any falsy value ({}, [], "", 0, false) because those rows have no value_json. Embeddings store sha256_value of the real body, so the new _digest_diverges check treats those hits as replaced and omits them from search even when the promoted row is unchanged.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9e9d84f. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change needed. shared.value is a String column that always holds json.dumps of the promoted value, and served_row_value parses that text, so {}, [], "", 0 and false hash to the same digest the embedding stores. A new test in b482833 covers all five and passes on the existing code.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recall Lab now matches a ranked hit to the keyword row at its exact writer and revision. The Memories list still gives a same-key preview another writer’s conflict badge.

Comment thread frontend/src/views/Recent.tsx Resolved
…head holds other content

A shared hit follows the private-row order: the digest check applies only when the head is the served revision.

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
…espace

The badge now matches the Conflicted filter, so another writer's conflict on the same key does not mark it.

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>
zlareb1
zlareb1 previously approved these changes Dec 2, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ranked previews use their own conflict status, and parked shared hits keep evidence only for the body served. On the merge tree, 57 dashboard and 254 Python tests pass.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub reports a merge conflict with main in the generated Go and Rust wire models and tests/analyzer/shapes.txt. Reconcile the wire changes and regenerate the models and statement shapes so CI can test the combined tree.

…guidance

Signed-off-by: Krishna Dobhal <krishnadobhal234@gmail.com>

# Conflicts:
#	sdk/go/wire/models.gen.go
#	sdk/rust/src/wire_models.rs
#	tests/analyzer/shapes.txt
@krishnadobhal

Copy link
Copy Markdown
Contributor Author

Merged upstream ClickHouse/loom main at eb98b9b62 (Release 1.4.0) into this branch in merge commit 788cece18, and pushed it. The three generated files that conflicted (sdk/go/wire/models.gen.go, sdk/rust/src/wire_models.rs, tests/analyzer/shapes.txt) were taken from upstream and regenerated with their generators. registry_contracts.rs was regenerated too, since the Rust generator writes it.

The generators' --check modes all pass, and tests/unit/test_schema_baseline.py and tests/analyzer pass (20 passed, 2 skipped). The merge is clean against the current upstream tip.

@krishnadobhal
krishnadobhal requested a review from zlareb1 December 3, 2026 09:40

This branch has not been deployed

No deployments
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.

Tell agents how retrieved knowledge may be used

3 participants