Run a correction through review and publication
Needs a disposable org on a Loom server and three tokens minted for it. sdk/python/examples/knowledge_factory.py runs one correction over HTTP: a weekly report…
Needs a disposable org on a Loom server and three tokens minted for it.
sdk/python/examples/knowledge_factory.py
runs one correction over HTTP: a weekly report fails on an outdated definition, a
writer prepares a correction from the source document, an administrator approves the
exact text, and a fresh run is served the published definition and passes on other
numbers. This guide gives the commands, what a pass proves, the reads that show it
without the script, and the limits.
The reporter, the curator, the reviewer and the grader are scripted, and no call is answered by a model. A pass shows that Loom keeps its contract. It does not measure an agent.
The run does not exercise the following, and its result lists each under coverage:
- a problem report that Loom holds, and a second report of the same problem
- a curation pass, its retry after an interruption, a source that changed before the approval, and a guard against publishing twice
- the read receipt of the revision each run was served
- a run by a real agent, and the MCP transport
Until the first three are exercised the result reads qualified: false, also when
every check passes.
The knowledge factory walkthrough is a shell session that publishes a document and saves a lesson citing it. This guide starts later: a published definition turns out wrong.
Run it
Install the Python SDK (pip install clickhouse-loom) and set the inputs. Each has a
flag of the same name, such as --team-namespace.
| Variable | Meaning |
|---|---|
LOOM_BASE_URL, LOOM_ORG |
The deployment and a disposable org |
LOOM_TEAM_NAMESPACE, LOOM_REPORTER_NAMESPACE |
Two namespace names that do not exist yet |
LOOM_REVIEWER_API_KEY, LOOM_CURATOR_API_KEY, LOOM_REPORTER_API_KEY |
The three tokens, each with its own subject |
From an operator shell that holds the signing key (mint a token):
suffix=$(date +%s)
export LOOM_TEAM_NAMESPACE=kf-team-$suffix LOOM_REPORTER_NAMESPACE=kf-reporter-$suffix
export LOOM_REVIEWER_API_KEY=$(loom mint-token --user-token --subject kf-reviewer \
--org "$LOOM_ORG" --namespaces "$LOOM_TEAM_NAMESPACE" --roles admin --ttl-days 1)
export LOOM_CURATOR_API_KEY=$(loom mint-token --subject sa:kf-curator \
--org "$LOOM_ORG" --namespaces "$LOOM_TEAM_NAMESPACE" --roles writer --ttl-days 1)
export LOOM_REPORTER_API_KEY=$(loom mint-token --subject sa:kf-reporter \
--org "$LOOM_ORG" --namespaces "$LOOM_TEAM_NAMESPACE" "$LOOM_REPORTER_NAMESPACE" \
--roles writer --ttl-days 1)
python sdk/python/examples/knowledge_factory.py run --result kf-result.json
The reviewer is a person with admin on the team namespace. The curator and the
reporter are service accounts with writer; the reporter’s token also names a
namespace of its own. Nothing is turned on at the server, and the example sends no
model credential: Loom embeds on the server.
The run prints one line per stage, then the result document, which it also writes to
--result. Each stage of the result lists the request ids of its calls, except the
identity and capability reads of preflight.
| Exit | status |
Meaning |
|---|---|---|
| 0 | passed |
Every check passed; read qualified for the gate |
| 1 | failed |
A check failed, or the server refused a call during the run |
| 2 | A usage error | |
| 3 | not_ready |
The setup is wrong: a token, a grant, a fixture file, a namespace that is in use |
| 4 | unavailable |
The deployment does not serve an op or a request field the run needs |
A usage error names the options the command does not take and counts every other word without printing it: such a word may be a token.
Before the first write, preflight names everything the run lacks, each as one entry of
stages[0].misses. It checks the inputs, among them a base URL that is not an http or
https URL with a host and a --result path whose directory is missing or cannot be
written, reads each token’s identity (GET /v1/whoami) and claims, asks the deployment
for every op and request field of the run, and checks that the team namespace is absent
and holds no grant. A run that cannot write its result file still prints the result.
Three things to know before running:
- Names are spent after a run. A namespace’s registry row and its grants outlive
the cleanup. For another run, choose a new suffix, mint the tokens again and name a
new
--resultpath: a run does not replace a result file that holds a run. - Send no request that names the reporter’s namespace before the run. Loom lets a
writerregister a name only while nothing is recorded under it, and a read that runs over its latency budget is recorded under the namespace it names. The reporter’s registration is the first request of the run that names its namespace. When the server refuses it the run endsnot_readybefore any write. - The server provisions a personal namespace for the reviewer. On the reviewer’s
first op call, when its subject holds no grant in the org, Loom registers
personal-kf-reviewerwith an admin grant. The example neither writes to it nor erases it. One reviewer subject for every run in an org provisions it once.
The stages
| Stage | Token | What happens |
|---|---|---|
preflight |
all three | Identity, claims, ops and fields, the absent team namespace; then the two writers’ memory.definitions.list must answer 403 |
setup |
all three | namespace.register for both names with the run’s marker, and the curator’s refused read of the reporter’s namespace; the definition (memory.set, memory.promote), the data dictionary (knowledge.upsert), the procedure (procedure.add), a personal request |
run_1 |
reporter, reviewer | run.begin, a labelled memory.search and memory.get, the report on week 1’s numbers, run.complete with a reflection, run.grade: fail |
report |
Not exercised | |
investigation |
curator | knowledge.get of the source document, then the candidate: memory.set with if_match_sha256 and source_resource_id; the same write sent a second time answers 409 |
review |
all three | What a reader is served meanwhile, two refused promotions, an amended candidate, a stale approval refused with 409, a personal request proposed for the team and rejected, a team note proposed and accepted, a note the reviewer proposed and cannot accept |
publication |
reviewer | memory.get of the amended text, memory.promote of that read’s hash, memory.lineage, audit.list |
run_2 |
reporter, reviewer | A new client, session and run on week 2’s numbers: the first search result is the governed definition, the report passes |
evidence |
reporter | run.get for both runs, the reflection, memory.provenance |
The numbers come from the fixture’s numbers source on every run and are never saved as knowledge: no request of the example holds one of them.
What Loom enforces and what the example arranges
| Property | Loom | The example |
|---|---|---|
Publication needs admin on the namespace |
Enforced: memory.promote answers 403 to a writer |
|
| An approval names the exact text | Enforced: approved_sha256, else 409 PRECONDITION_FAILED with current_sha256 |
Reads the text again after a 409 |
| A write does not replace a revision its writer did not read | Enforced for a write that sends if_match_sha256: 409 when the row holds another hash |
Both of the curator’s writes send it |
| The approver of a definition is a person | Not enforced: a service account with admin can promote |
Preflight requires a person’s token for the reviewer |
| The approver is not the author | Not enforced for a promotion | By tokens: the curator holds writer; the result shows author and approver |
Deciding a proposal needs admin on the namespace |
Enforced: memory.proposal_decide answers 403 to a writer |
|
| A person other than the proposer accepts a proposal | Enforced by memory.proposal_decide (proposals) |
The reviewer cannot accept a note it proposed: 200 with nothing decided |
| A proposal is not served | Enforced: a proposed row is in no search result |
The same search returns the row once it is accepted |
A writer cannot change an active team row without review |
Not enforced | A limit, below |
| The approved definition stays served during review | No: an edit of its source row ends governance until the next approval | The reporter uses a definition only from a governed hit |
| The link from the problem to the correction | Not held | Text in the promotion’s rationale that names run 1 |
| The revision a run was served | Kept as a read receipt where one is written; no op serves it (runs) | Prints not_read: it calls ops only |
| An independent grader | Not enforced: run.grade takes any writer |
The reviewer’s token grades; the audit row shows who called |
| No duplicate publication | Not enforced: memory.promote is not idempotent |
One call; one that gets no usable answer is settled by reads |
Governed definitions in search explains when a definition is governed and how a query matches its term.
What the example checks
- Only an administrator publishes, and a
writercannot decide a proposal. The curator’s and the reporter’smemory.promoteand the curator’smemory.proposal_decideanswer 403, and the definitions list is unchanged after them. The curator’s decision is a rejection, which the role alone refuses: an acceptance also needs a person. A refusal counts only as Loom’s 403, which carries the codeAUTH_INSUFFICIENT. After publicationmemory.lineagenames the reviewer aspromoted_byandmemory.getnames the curator asactor. - An approval names the text it approved. The reviewer’s approval of the hash it read before the curator amended the candidate answers 409 with the current hash. The reviewer reads the row again, compares it with the text it accepts and approves the hash of that read.
- A candidate write names the revision it read. Both of the curator’s writes send
if_match_sha256, and the candidate write sent a second time answers 409 with the current hash, because the revision it names is gone. - Nothing unreviewed is served as governed. While the candidate awaits review the
definition is
lapsedwith reasonchanged, no search hit is governed, and the reporter, given that search, produces no report. - A proposal is served only once it is accepted. The curator cannot read the
reporter’s namespace. The personal request it writes as a team proposal is listed,
stays
proposed, is in no result of a search for its text and is rejected by the reviewer. A second proposal, a team note, is the control: the same search for its text does not return it while it is proposed and returns it once the reviewer accepted the hash of the entry it read in the list. A proposal’s own writer cannot accept it: the reviewer proposes a note and accepts it with the hash its write answered. The call answers 200 withdecided: 0, and the key’s result readsdecided: falseandstatus: "proposed". The reviewer then rejects the note, which takes no second person. The check reads these fields and not the reason text. - The fresh run is served the published text. Its first search result is governed,
term_legiscomplete, the served value equals the approved one, and the report equals the expected report for week 2. - The correction leaves a question it does not concern unaffected. The report’s title and account count rest on the procedure row: both runs read the same revision of it and both get that part right.
- Evidence is three objects per run and none fills in for another: what
run.getreports Loom returned (returned), what the client reported it relied on (client_reported), and the grade with the caller Loom recorded (graded). Whererun.getreports the keys the labelled search returned, the check fails unlessreturnedholds the definition’s key. - Every link between the stages is typed and says who holds it:
loom, orclientfor a link that is only text the example wrote.
tests/unit/test_sdk_example_knowledge_factory.py runs the example against a scripted
server, and tests/integration/test_sdk_example_knowledge_factory.py against a live
one, where it also compares the result with the fixture’s expected.json.
Check it yourself
With the names and ids of the result file, each read as the token named:
run.geton run 1 (any of the three):gradeisfail, andmemories_usedholds the definition’s and the procedure’s keys.memory.getonreflection:<run 1>with agentkf-reporter(any): the problem as the reporter recorded it.memory.provenanceondefinition/active-accountwith agent-(any):source_resource_id. Thenknowledge.geton the source URI: the samedoc_id, and itsrevision.memory.lineageon the definition with agent-andscope: "shared"(any):promoted_by,rationale,promotion_event.audit.listwithevent_typememory.promote, thenrun.grade(reviewer): who called each, by request id.memory.searchfor “What is an active account?” (any): the first result hasgoverned: true.memory.definitions.list(reviewer):verdictisgoverned.run.geton run 2 (any):gradeispass.
The result document
| Field | Holds |
|---|---|
mode |
deterministic: every role is scripted |
status |
passed, failed, not_ready or unavailable; running in a file whose run did not reach its end |
qualified |
True when the run passed and every gating coverage entry is exercised |
run |
The run’s id and marker, the base URL without any user:password@, the org, the two namespaces and the three subjects |
stages |
Each stage’s status and request ids; a revision where the stage read one; served and revision: "not_read" for the two runs |
runs |
Per run: the report, the control question, returned, client_reported, graded |
links |
Problem, correction, source, review, published revision, later run and outcome: a typed ref, held_by and the read that shows it |
observations |
What a reader is served while a candidate awaits review; values of this path, not required behaviour |
coverage |
What this version exercises, and the capability each entry that is not exercised needs |
checks |
Every check, with its stage and a detail; a refusal, a grade, the search during review and the audited promotion name their request id |
ids |
The source document’s doc_id and the two promotion events |
cleanup |
not_run, completed or failed; after a cleanup also erasures and remains |
A revision is the source row’s version_text and content_sha256 from one
memory.get, with read saying which. The version is a decimal string: as a number it
is above 2^53, which a reader holding doubles rounds.
A governed hit carries no version and no hash, so the two run stages print
revision: "not_read" beside what the read did carry. not_read says that the example
did not read the revision, not that Loom holds none. Where receipts are written, Loom
keeps the revision each labelled read held as a read receipt under the run’s run_id
(what gets none). No op serves a
receipt: it is read with SQL
(reading the table).
returned holds what run.get reports and nothing else. It reads not_recorded unless
the server keeps the search trace log (LOOM_SEARCH_TRACE_LOG_ENABLED); it then lists
the keys the run’s labelled search returned, and the run’s evidence check fails when the
definition’s key is not among them once the wait is spent. recalled: -1 on run.get
means that it holds no count, not zero
(runs).
Coverage
| Entry | Exercised | Needs |
|---|---|---|
run_1, investigation, review, publication, run_2, evidence, control_question |
yes | |
report, repeated_report |
no | Problem reports that Loom holds |
curation_pass, interrupted_pass_retry, stale_source_refusal, duplicate_publication_guard |
no | A curation pass |
revision_receipts |
no | An op that serves a read’s receipt |
real_agent_run |
no | An agent runner; does not gate |
mcp_path |
no | An MCP client; does not gate |
The table is a constant of the example’s version. An entry reads exercised only in the version that makes its calls and checks them, never at run time.
Clean up, and what remains
python sdk/python/examples/knowledge_factory.py cleanup --result kf-result.json \
--accept-org-cache-loss
cleanup needs the result file, the run’s base URL and the reviewer’s and the
reporter’s tokens; it has no flag for the curator’s. It erases the two namespaces of
the run with erasure.request, each with the token that holds admin there, and
waits for erasure.status to read completed with no residue
(erasure).
It refuses, with no erasure request sent:
- a
--resultpath that holds no file, or a file that is not a result file - a call without
--accept-org-cache-loss: an erasure also deletes the org’s replay cache and its cached query vectors, which is why the org must be disposable - a base URL or one of the two tokens that is not set, a base URL other than the one
the result file records, each compared without
user:password@, and one that is not an http or https URL with a host - a namespace whose tags lack the marker in the result file
cleanup asks for the erasure of each namespace once, however many times it runs.
The erasure runs inside its request, so cleanup writes an entry for the namespace to
the result file before it sends the request, with status requested and no id, and
the erasure’s id as soon as the request answers. A request that gets no usable answer
may have erased: no response, a 5xx, or a 2xx whose body is not the server’s JSON,
such as a proxy’s page. cleanup then reads the erasure’s id from erasure.list and
sends no second request. When the ledger shows no erasure of the namespace, the pass
ends failed and a later pass reads the ledger again. To have the erasure asked for
again, remove that namespace’s entry from erasures in the result file. A request
the server refused with a 4xx erased nothing and leaves no entry.
A later cleanup reads the status of the erasures the file records and sends no
request for them, also after a pass that failed. An erasure.status answer that does
not find the erasure is read again, for as long as one that reads in_progress, and
replaces nothing the file records. cleanup exits 0 when every erasure completed
with no residue, 1 when one did not or a namespace the run registered is absent, and
3 when it refused.
What remains, as the result’s remains lists it: the registry rows of both
namespaces, the admin grants their registration wrote, the reviewer’s provisioned
namespace with its grant, the audit rows, and the erasure ledger rows.
What the agent host runs
Loom holds the knowledge, its review state and each run’s evidence. The loop around them runs in the agent host. In the example a script plays every part.
| Part | The host | Loom |
|---|---|---|
| The reporting agent, on a schedule | Starts each run, labels its reads with it, reports what it relied on | run.begin, run.complete with memories_used (runs) |
| The sweep that finds work | Lists failed and ungraded runs, and questions no source answered | run.list with grade; knowledge.gaps.list where it is on |
| The curator | Reads the failed run and the source document, writes the candidate | memory.set with if_match_sha256 and source_resource_id |
| The evaluator | Decides each grade and names itself in source |
run.grade records the grade and its caller, and judges nothing |
| The display of what an answer used | Shows the keys a run reported and, where listed, those its search returned | run.get: memories_used, recalled_keys |
Limits
- A
writercan end governance. Anywriterof the namespace can edit a definition’s source row, which stops it being served as governed, and can write an active team row without a proposal. - A service account can publish. Loom requires
adminformemory.promote, not a person, and not someone other than the author. Proposal acceptance requires both. - Review has a gap. While a candidate awaits review the approved definition is not
served as governed, and the candidate is an ordinary row every reader of the
namespace can read. The result records both under
observations. - Another form of the term is a silent miss. A term matches at word edges, and a
word in capitals only as written. A query that names another form is answered with
term_legcompleteand no governed hit.governedis on a hit only when true. - After a 409 on a promotion, read the text again. Never send
current_sha256back unseen: it names text the approver has not read. - A
memory.promotethat gets no usable answer is settled by reads, never by sending it again: no response, a 5xx, or a 2xx whose body is not the server’s JSON. The example reads the definitions list, the source row and the lineage until they show the promotion. Only the lineage names it, by its rationale: the row held the approved hash before. Any other write that ends this way fails the run and is not sent again. grade_sourceis text the caller sent. The audit row says who graded.- Run 1’s fail grade stays with the key. Grades are counted against each key a run relied on, so the corrected revision carries the earlier failure (graded runs).
- HTTP only. A read over MCP is filed under the connection’s implicit run, so its run evidence differs.
- Minted, flat tokens only. The tokens come from
loom mint-tokenwith--namespacesand--roles. Preflight refuses a token minted with--namespace-role. A reviewer signed in through an identity provider, and a token pinned to one user, are not covered by the example’s tests. - Preview ops. The example calls these ops that the status page lists as preview:
- marked
experimental:memory.definitions.list,memory.promote,memory.lineage,memory.proposals,memory.proposal_decide,knowledge.upsert,run.grade,audit.list,erasure.status,erasure.list
- marked
Related
- Build a knowledge factory: documents, lessons and the walkthrough.
- Identities and namespaces: the shared layer and governed definitions.
- Runs: reliance, grades and what
run.getreturns. - Tokens and grants: minting tokens and roles.