Skip to content

knowledge.inspect reports a namespace's documents, the chunks search serves and the decisions cited from them - #251

Merged
zlareb1 merged 4 commits into
mainfrom
feat/i160-doc-inspect
Nov 4, 2026
Merged

zlareb1 merged 4 commits into
mainfrom
feat/i160-doc-inspect

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Related: #160
Related: #249 (the same change; its branch was reset by mistake and the closed PR cannot be reopened, so its review history is there)

Changelog category (leave one):

  • New Feature

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):

knowledge.inspect (viewer, assistant toolset) reports a namespace's documents: counts by format, source scheme and revision, which live chunks search can serve, the newest documents with their chunks and the decisions cited from them (decided apart from proposed), findings on chunks that search cannot serve, and namespace.audit's findings on memories shaped like documents.

Why

Someone investigating a namespace, or an assistant acting for them, has no single read that answers "which documents are here, which versions are searchable, and what was extracted from them". knowledge.list pages documents, knowledge.get reads one, and the audit's document-shaped memory findings sit in another report. This op composes what is already stored into one bounded, read-only report. It never parses a document.

What it reads:

  • loom.documents FINAL on the (org, namespace) prefix: live and deleted documents grouped by format (the row's format, which knowledge.ingest sets and knowledge.get serves, else the ingested_format a source sync records in metadata (transcript, markdown, slides, or the extractor's pdf or pptx), else the stored content type, else the extension of the original filename or the source_uri path, host excluded) and by source_uri scheme, plus the newest live ingest. The same read sums the row's revision into earlier_revisions (the published revisions before each current one) and counts unnumbered rows (revision 0).
  • loom.chunk_supersessions: retired chunk versions, counted once per (source_uri, doc_version). These include every knowledge.ingest attempt that inserted chunks and never published a document row (failed, cancelled, or beaten by a later write, retired by the job's failure path), which no revision numbers. That is why revisions come from the row.
  • loom.chunks under the shared live-chunk predicate: live chunks per embedding model, how many carry a vector (embedding_dim > 0), and searchable, meaning under the server's model with a vector. knowledge.search reads exactly these.
  • One join of live documents with their per-document chunk aggregate drives four findings. Each carries the findings contract (kind, rule_version, source_op, population, sample_limit, samples_complete, exceptions, uncertainty). A clean check is reported at info with count 0:
    • documents_without_chunks, unembedded_chunks and chunks_under_other_model are system_state: the server's own state. chunks_under_other_model counts documents with live chunks and none under the server's model, so a document whose chunks are under the server's model without a vector is unembedded_chunks only, and agrees with the listing's other_model. Each finding counts and samples the documents knowledge.ingest published apart (ingested, ingested_samples). The remedy names knowledge.upsert or loom rechunk --source-uri <uri> for a text document. For an ingested one it names knowledge.ingest of its original (GET /v1/knowledge.download serves it): a text write re-cuts the rendered text and clears format, parser, coverage and ingestion_id, which knowledge.update_metadata(reindex=true) refuses for the same reason. For mixed_live_versions it first points to the source's open knowledge.ingest job, which settles that state on its next run, and to a new ingest only when none is open. loom rechunk itself does not skip ingested documents, so its remedies name one source_uri instead of the whole namespace.
    • mixed_live_versions is a usage_heuristic: live chunks of two versions of one document. Its uncertainty names an ingest's publish window: the new version's chunks land before the row, and the old ones are retired after it. An insert the job could not confirm leaves both live until its next run.
  • The newest sample_limit live documents, each with its version, retired versions, live chunks (searchable, unembedded, other model, distinct versions, min/median/max characters, models) and decisions.
  • Decision rows: live memory rows typed decision (or tagged so) whose source_resource_id cites knowledge:<namespace>/<uri>#<section>, as loom decisions extract and decision.record write it. They are reported by stage, per listed document and for the namespace: decided, and proposed (recorded with stage="proposed", tagged decision:proposed; the extractor records a proposal's decisions this way). Each stage counts confirmed (pinned, or attested in its current content), unconfirmed, source-changed and outcome known. A pin on a proposal confirms the proposal's wording, not a choice, so it is never counted with the decided rows. A citation goes to the longest document URI it starts with, so doc#v2#Intro counts for doc#v2, never doc, whether or not doc#v2 is listed. One bounded read of loom.documents (sort-key column, at most 1,000 URIs, flagged in skipped past that) finds the documents, live or deleted, that extend a listed one with #. They go to the decision read with the listed URIs. A row under a longer unlisted document counts only in the namespace total.
  • namespace.audit, called in process with sample_limit=0 as namespace.tune calls it. Its oversized_values and episodic_blobs findings are cited by code, kind, severity, rule version and count. The audit's own samples and evidence stay in the audit.

namespace.audit's own reason for a rule it skipped is carried into skipped as memory_findings.<code>: <reason>.

Each section is guarded. A failed read lands in skipped as <section>: <ExcName> and its fields stay null or empty. A store outage (BACKEND_NOT_NOW) fails the call, as namespace.inventory does.

Design calls:

  • Read scope. The op is in SCOPED_OPS. Documents and chunks are the namespace's under any scope, as every knowledge.* read serves them. The decision rows are memory rows, so they are read with scope_and. The audit reads every user's rows and is not itself served to a narrowed token, so under a narrowed scope memory_findings is skipped and says why. Listing the op explicitly in SCOPED_OPS, rather than leaving it under the knowledge. narrow-safe group, records that it reads memory rows.
  • Where the decision statement lives. It reads loom.memory, so it lives in the memory plane's store (server/memory/store/document_decisions.py, through memory.app.query.document_decision_tally), not in server/knowledge/store/. The table's owner keeps its statements, as namespace.inventory reads memory through memory_tally. It reuses attested_sql and served_status_sql.
  • Decision stage. The stage is a tag on the memory row (the record carries no stage column), so the read groups by has(tags, 'decision:proposed'). Counting per stage keeps every count meaningful for a proposal, rather than adding a proposed count beside totals that mix the two.
  • The mentor is not called. mentor.brief's documents_as_memories is per agent. A namespace report has no agent to ask about, and calling the mentor once per writing agent would cost one ledger scan each. The audit's oversized_values is the namespace-wide count of the same shape, and the output names the mentor hint as its per-agent counterpart.
  • _REPORT_OPS. knowledge.inspect joins the audit's report-op list, as memory.inspect and namespace.tune did, so its own ledger rows do not close a telemetry gap. telemetry_unavailable goes to rule version 3.
  • No canCall entry and no frontend change. No dashboard page calls the op yet.

Findings about what main stores (stated here, not worked around):

  • loom.documents is ReplacingMergeTree(version) keyed on (org, namespace, source_uri), so it holds only a document's current version. superseded_at on that row means deleted, not revised. The row's revision numbers it, so the count of earlier revisions comes from the row. An earlier revision's text survives only as retired chunk ingestions, and its title or metadata is not recoverable. retired_versions from loom.chunk_supersessions also counts the version a delete retired and ingest attempts that never published.
  • knowledge.add on an existing source_uri writes a new version without retiring the earlier one's chunks, so search serves both texts. knowledge.upsert retires them. This was verified on a live server, and mixed_live_versions reports it.
  • No wire op writes a knowledge chunk without a vector or under another model: chunks are embedded inline, and a model change leaves the old chunks under the old model. The integration test writes both states through ClickHouse. loom reembed covers memory only; loom rechunk is the remedy for documents.
  • Per-document ingestion jobs (loom.knowledge_ingestions), extraction coverage and parser state exist on main. This report reads only the row's format, revision, ingestion_id and the ingested_format in its metadata. Job state, failure type, coverage.lost and skipped pages are deferred to the next pull request.
  • memory.pin writes the row it is given. Pinning a decision without passing its memory_type, tags and source_resource_id drops its citation and its decision type. The integration test passes them.

Scope and risk

New files: server/knowledge/api/models_inspect.py, server/knowledge/app/inspect.py, server/knowledge/store/inspect.py, server/memory/store/document_decisions.py.

Small edits: the op declaration (ops_knowledge.py), MCP_ASSISTANT_OPS, the MCP description, OP_BUDGETS_P99_MS (4000 ms, with a comment naming the reads), SCOPED_OPS, the audit's _REPORT_OPS and its rule version, and the feature and MCP reference docs.

Tenant isolation: every statement filters on (org, namespace) with bound parameters. The decision read takes the request scope. The cross-principal walk includes the op in _CORE.

Nothing is written: no ledger exposure row, since the op lists document URIs and not memory rows. The integration test checks that loom.memory is unchanged.

Statement shapes: the per-document chunk read measures chunk text (lengthUTF8(content)) for the listed documents only. No statement reads a document's text.

tests/analyzer/shapes.txt is regenerated. Besides the op's ten new shapes, it picks up one memory.for_file shape that main's file had not caught up with.

Compatibility, rollout and rollback

  • Wire: one new op, knowledge.inspect (experimental, viewer, read-only, not audited). Input: org, namespace, sample_limit (0–100, default 20). No field of any other op changes. A build one release older answers it as an unknown op.
  • Operator steps: none.
  • Rollback: revert. The op stores nothing, and there is no migration.

Tests

  • tests/unit/test_knowledge_inspect.py (30 tests on fakes) covers:

    • totals folding and ordering, and searchable vs unembedded chunks;
    • each finding's kind, severity, share and sample cut;
    • the listed documents' chunks, retired versions and decisions, with the longest-first citation order;
    • listed_complete, sample_limit=0, and the audit citation;
    • an audit check that did not run, and a narrowed scope running no audit;
    • each store read failing into skipped with null fields, the decision and audit reads failing, an unbuildable embedder, and the group cap;
    • a store outage raising, and the decision read taking the scope;
    • longer documents passed to the decision read, the read sorting them longest first, its cap flagged, and its failure skipped;
    • the audit's reason for each skipped rule reaching skipped.
  • tests/unit/test_knowledge_inspect_sql.py (12 tests, clickhouse-local, real schema.sql tables) covers: a synced document (a transcript at a URI with no extension, a markdown export, a synced PDF upload) counting under the format its source converted, and

    • FINAL collapsing a document's two rows, and format and scheme derivation (a query string, a host that is not an extension, a deleted document);
    • retired versions de-duplicated;
    • chunk health under both join_use_nulls values;
    • newest-first listing with each row's revision and ingestion_id, and per-document chunk sizes;
    • a document as knowledge.ingest publishes it: revision 2, format, parser, artifact_sha256, ingestion_id, and chunks with keys. Its format comes from the row, it counts one earlier revision, and its chunks are two live versions (an unconfirmed insert) plus a retired attempt that never published. That attempt counts in retired_versions, not in the revisions, and the document is named among the ingested ones of mixed_live_versions;
    • a document whose only chunk is under the server's model with no vector, which counts as unembedded and not as another model, in both the health read and the listing;
    • the longer-document read finding a deleted doc#v2 for doc;
    • decision attribution to the longest URI, by stage (two proposals, one pinned and one source-changed, on their own row), with tombstoned rows, other namespaces, untyped rows and a narrowed scope excluded or bound.
  • tests/integration/test_knowledge_inspect.py passes against a private server with auth off and with auth on. It writes:

    • a document revised once (knowledge.upsert twice) and a page written twice with knowledge.add;
    • a web page, a deleted document, and a document whose chunks are under another model;
    • an unembedded chunk;
    • a document as knowledge.ingest publishes it, written through ClickHouse since the extractor service and the ingest worker do not run here: revision 2, format, ingestion_id, a chunk with keys, and a later attempt's chunks retired without a document row;
    • three decided rows citing the revised document (one pinned, one tagged source-changed) and one proposed row (stage="proposed"), each stage checked on every count.

    It then checks every section, then that a narrowed token reads documents with memory_findings skipped, and that a foreign token gets 403.

  • Cross-principal walk with auth on: test_cross_principal_leakage.py, test_restricted_namespace_leakage.py and test_read_scope_isolation.py give 67 passed, 17 skipped; rerun after the stage change with this op's integration test, the leakage and restricted suites: 44 passed, 17 skipped. The skips are the end-user issuer and code-repo cases, unrelated to this op.

  • Affected unit suites (2,579 tests) pass: knowledge, audit, read scope, op budgets and reach, MCP listing budget and assistant toolset, the LibreChat integration, package contracts, public copy, doc links, SDK surface, agent instructions.

  • After the ingest review fixes, the affected unit suites (1,835 tests) pass. The integration test, the leakage suite and the restricted-namespace suite with auth on give 44 passed, 17 skipped. python -m tests.analyzer.corpus --check gives 2,044 shapes, 0 rejected.

  • After the review fixes, the affected unit suites (2,251 tests) pass. The integration test, the leakage suite and the restricted-namespace suite with auth on give 44 passed, 17 skipped. python -m tests.analyzer.corpus --check gives 2,017 shapes, 0 rejected.

  • After the stage change, the affected unit suites (2,318 tests: knowledge, decisions, context pack decisions, audit, read scope, op budgets and reach, MCP listing and assistant toolset, LibreChat, package contracts, public copy, doc links, SDK surface, agent instructions) pass.

  • make format-check, make lint (every generator --check, reach census) and make typecheck are clean. python -m tests.analyzer.corpus --check against the migrated ClickHouse: 2,017 shapes, 0 rejected. tests/analyzer/test_corpus.py passes.

Cost on a namespace of 2,000 documents, on a private single-node ClickHouse with the fake embedder. The namespace holds 1,900 live and 100 deleted documents, 4,781 live chunks, 313 retired versions, 60 decision rows and four URI schemes. Figures come from system.query_log, filtered on the call's log_comment, as the median of three runs:

sample_limit statements rows read bytes read ClickHouse ms (sum) slowest statement peak memory wall ms
20 22 39,380 1.50 MB 244 25 ms 10.1 MB 311
100 22 39,480 1.96 MB 242 25 ms 10.0 MB 311

The cost was measured before the stage grouping. That grouping adds one key to a GROUP BY over the same rows, and there were 60 decision rows.

The longer-document read added since then is one more statement over the namespace's document URIs (the sort-key column), so a run is 23 statements.

Of the 22 statements, 8 are the report's own (29,127 rows, 98 ms) and 14 are namespace.audit's. The audit's share grows with the namespace's memory rows, not its documents. The largest single read is the chunk-health join (9,319 rows). The listed-document reads grow with sample_limit in bytes, not rows.

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. Checked: each statement on clickhouse-local and the live server, the scope handling under a narrowed token, and the cost numbers in system.query_log.

  • 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. (format, lint, typecheck and the affected unit and integration suites pass; the full make test-unit is still to run.)
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • 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; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/i160-doc-inspect branch from d8269ad to 533d50e Compare November 4, 2026 04:32
…serves and the decisions cited from them

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/i160-doc-inspect branch from be559f2 to 64d6f69 Compare November 4, 2026 04:46
zlareb1 and others added 3 commits November 4, 2026 04:49
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…n on one line

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 5795e28 into main Nov 4, 2026
21 of 22 checks passed
@zlareb1
zlareb1 deleted the feat/i160-doc-inspect branch November 4, 2026 05:10
@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