Source read mapping
A read mapping is a JSON document that tells the source worker's mapped reader which tools of a protocol server list a source's documents, fetch one and show…
A read mapping is a JSON document that tells the source worker’s mapped reader which tools of a protocol server list a source’s documents, fetch one and show that one was removed. This page states the format, the three rules that set it apart from the live read’s tool mapping, what the reader answers in each case, the turn in which the sync engine runs a reader, and how a server’s results are recorded as test fixtures.
The reader and the engine are libraries of the worker package. No route of the source worker calls them: a live read uses the tool mapping of the outbound MCP source client, which this format does not replace.
Source entry points
source_worker/loom_source_worker/mapping.py: theMappingmodel,load,digest(the mapping’s identity),definition_digest(the digest a tool is pinned by) andfill(an argument template with its values).source_worker/loom_source_worker/readers/mcp_mapped.py(MappedReader): lists, fetches and checks removals through aToolCaller.source_worker/loom_source_worker/sync_types.py:SourceReader,ToolCaller, the values they exchange andReaderErrorwith its codes.source_worker/loom_source_worker/sync.py(run_turn,SyncLimits,TurnReport): the sync engine, one turn of one source a call.source_worker/loom_source_worker/sync_binding.py:SyncBinding, the source a claim answered, andSyncBackend, which supplies its reader.source_worker/loom_source_worker/loom_client.py:LoomOps, the four turn ops, andToolCallerLoom, which sends them through the server’s generic op tool.source_worker/loom_source_worker/host_backend.py(HostBackend) andhost.py(run_once,HostLimits,RunSummary): the backend and the run of a worker beside an account host (the host backend).source_worker/qualification/record_mapped.py: the recorder, andReplay, which answers a reader from a recording.source_worker/qualification/stand_in_channel.py(Channel,Stdio): a stand-in for an account host’s tool channel over a server started as a local process.source_worker/mappings/wiki.json: the reference mapping, for an open-source wiki’s protocol server.
Invariants
- The reader holds no credential and no organization. The
ToolCallerit is given is its whole reach. - A mapped tool that is missing from the server’s tool list, lacks
readOnlyHint: trueor has another digest than the pinned one refuses withmapping_changedbefore any call. Nothing is listed or fetched. - A document is never returned short. A result over a size bound, or one that holds the
mapping’s truncation marker, refuses with
too_large. - A result the mapping cannot read refuses with
protocol. No value is guessed. - Only values the mapping names reach a call: the document id, the cursor, the time and the scope, each at its placeholder.
- The reader reports no absence. A page’s
gonelist is empty, andcompleteis true only on the last page of an unfiltered listing that the mapping declares complete and that one call read from its start. removedanswersgoneonly for the mapped not-found answer, and only when the scope answered the same reader before that answer and answers it again after.- The engine is the only caller of a reader and of the Loom client, and a reader imports neither. The engine acts only on the source its claim answered.
- The engine sends one op at a time, holds one document in memory, writes no file, keeps no sync state between turns and logs no claim, title or text.
- The engine reports no removal without evidence, and sends no op after a lost claim.
The format
load parses version 1 and refuses a key the format does not name. digest is the
SHA-256 of the mapping’s canonical JSON, so key order, whitespace and a key written as
null do not change it.
| Key | Value |
|---|---|
version |
1 |
link_origins |
The https://host[:port] origins a returned link may have |
limits |
max_result_bytes, max_text_bytes (at most 4,000,000), max_pages_per_turn |
fetch |
The tool that reads one document |
list |
The tool that lists the scope; without it the source is its selected items |
removed |
mode (listing, fetch_not_found or none) and, for fetch_not_found, not_found |
denied |
The answer that means the connection may not read the document |
fetch and list both carry tool, definition_sha256, arguments and result.
fetch key |
Meaning |
|---|---|
id_path, text_path |
Required for a JSON result; the id must equal the requested one |
title_path, link_path, revision_path |
Optional paths into a JSON result |
text_frame (start, end) |
In a text result, the document is the text between the two markers |
title_regex |
In a text result, a pattern with one group for the title |
link_template |
A link with {id} once, used when no other link is known |
truncated_marker |
Text that marks a result the server cut short |
truncated_marker is searched in the result’s text block as the server wrote it. In a
structured result it is searched in the canonical JSON of the structured content:
sorted keys, no space after : or ,, and each string JSON-escaped. A flag is written
"truncated":true, and a quote inside a string value \".
list key |
Meaning |
|---|---|
items_path |
The list of items in the result; empty when the result is the list |
id_path |
The id inside one item |
revision_path, modified_path, title_path, link_path |
Optional paths inside one item |
next_cursor_path |
The cursor of the next page; no page follows without one |
complete |
Whether a listing read to its end names every document |
changed_since |
Whether the tool can list only what changed since a time |
A result that is itself the list has no place for a cursor, so load refuses
next_cursor_path beside an empty items_path.
An optional path of an item (revision_path, modified_path, title_path,
link_path) reads as empty when its last key is absent or null. A member above the
last key that is absent, null or not an object refuses with protocol, and so does a
value that is not a string.
next_cursor_path is optional along its whole length. A member that is absent or
null at any depth means that no page follows, and so does an empty cursor: a server
may leave out the whole member that holds its cursor on a listing’s last page. A member
that is present and is not an object where the path goes on, and a cursor that is not a
string, refuse with protocol.
A path is a list of object keys. not_found and denied are matchers: is_error and
text_prefix. A result matches when its error flag equals is_error and its one text
block starts with text_prefix. load refuses a mapping whose two matchers could match
the same result, so a denial never reads as a removal.
The three rules
A tool is pinned by digest. definition_sha256 is the SHA-256 of the canonical JSON
of {name, inputSchema, outputSchema, readOnlyHint} as the server’s tool list advertises
them. The reader never interprets the schema, so a tool may use any JSON Schema keyword.
The description is left out: it changes without changing the call.
Arguments are a template. arguments is a JSON object in which a member
{"$value": "document_id"}, "cursor", "since" or "scope" is replaced by that
value, at any depth. A placeholder with no value drops its key. A fetch names
document_id and may name scope; a listing may name cursor, since and scope.
Nothing else in a call comes from the upstream or from the registration.
A result may be plain text. result is structured (the result’s structured
content), json_text (exactly one text block that parses as JSON) or text (exactly
one text block, which is the document). A listing is never text.
Listing
list_since(checkpoint, full) yields pages until the cursor runs out or
max_pages_per_turn pages were read. A page’s more is true while a cursor is pending,
so the last page of a call that stopped at that bound says that more is left. The scope
is the registration’s one item, sent through the scope placeholder, so one mapping
serves every container of a server. Without list, the reader yields the registration’s
items as one page that is not complete.
Each page carries a checkpoint: JSON of at most 4,096 bytes that holds the pending
cursor and the listing’s times, and no document text. A later call with that checkpoint
resumes the listing at its cursor. Its pages name only the rest of the listing, so its
last page is not complete. A cursor too long for the checkpoint refuses with
protocol.
When a listing ends and the mapping sets changed_since, the checkpoint’s since is
the newest modified time the listing saw, less 60 seconds: an upstream may list a change
a little after the time it stamps on it. since is never later than the time the
listing started, less 60 seconds. The reader reads the wall clock when a listing starts
without a cursor and carries that time in the checkpoint while a cursor is pending. The
cap covers two cases:
- A document that is edited after its page was listed, while the listing still runs or waits for its next turn. Its new time is after the listing’s start, so the next listing by time names it.
- An item stamped in the future. It would otherwise move
sincepast every later edit.
A newest modified time with no 60 seconds before it, such as a zero time sent for a time
that is not set, refuses with protocol.
A call with full false sends since through the since placeholder. A listing
filtered by time is never complete. A call with full true lists without the filter
and restarts a filtered listing that was pending.
Fetch
In a JSON result the id is read at id_path and must equal the requested id. In a
text result the id is the requested one. The other values fall back in order:
| Value | Order |
|---|---|
| Title | title_path, or title_regex on the first 4,096 bytes; the listing; the id |
| Link | link_path; the listing; link_template |
| Revision | revision_path; the listing; the SHA-256 of the text |
| Modified | The listing |
A link that is not https, or whose origin is not in link_origins, is dropped: the
document has an empty link.
Removal
removed answers gone, present or unknown and raises nothing. With
fetch_not_found it fetches the id: the not_found answer is gone, a readable
document is present. A denial, a timeout, any other error and an unreadable result are
unknown, and so is every id under the modes listing and none, which make no call.
gone also needs evidence that the scope still reads, taken after the not-found answer.
A server may answer for what a connection can no longer see as for what is not there,
and a connection may lose the scope in the middle of a turn. The reader reads again what
the scope already gave it:
- When its listing named an item, it calls the listing again, without a cursor or a time, and the first page must name an item.
- Otherwise, or when that fails, it fetches the document it read last, through
fetchor apresentanswer, and that document must still read. When that is the document asked about, the answer isunknown: the reader tries no earlier document.
A reader that has neither listed an item nor read a document answers unknown, and so
does one whose evidence no longer reads. A gone answer takes at least one call after
the fetch.
Failure codes
ReaderError carries one code and no upstream text.
| Code | Raised by the reader when |
|---|---|
mapping_changed |
A mapped tool is missing, not read-only or differs from its pinned digest |
too_large |
A result or text is over its bound, or holds the truncation marker |
denied |
A result matches denied |
protocol |
Any other error result, a not-found answer to fetch, or an unreadable result |
timeout |
A call did not answer by its deadline |
A ToolCaller raises the same error for its own failures, with one of the remaining
codes: reconnect_required, access_revoked, tool_not_allowed, server_off,
rate_limited or upstream_unavailable. A caller over an account host’s channel also
raises timeout for a call the channel timed out and too_large for a result the
channel refused for its size
(the host backend).
The engine’s turn
run_turn(loom, backend, readers=..., digests=..., limits=..., now=...) runs one turn of
one source and answers a TurnReport. It uses the four turn ops:
knowledge.source.worker.claim,
.stored,
.put and
.commit.
loomis aLoomOps.ToolCallerLoomsends each op as{"op": ..., "arguments": ...}through the server’s generic op tool, so a worker beside an account host reaches Loom through the host’s tool channel alone. That tool leaves an empty string or list out of an answer, so the engine reads a missing one as empty.ToolCallerLoomis givencall_tool, which answers the tool’s result. The server answers a refusal as an error result, and a failure it names, such asMODEL_CALL_DEFERRED, as a JSON-RPC error whosedataholds thecodeandretry_after_s.call_toolanswers that JSON-RPC error as an error result with thedataas its structured content, so both reach the client in one shape. It raisesReaderErrorfor any other failure of the channel.backendis aSyncBackend: it holds the connection and gives the engine a reader.nowreads the clock the readers’ deadlines are measured on.
A turn, in order:
- Claim.
claimnames the readers and the profile digests the worker holds. An answer ofnoneends the call with the wait and the sources that wait for a person. A server that does not serve the ops ends it withunavailable: no other op is sent, and the engine logs one line a day in a process. - Bind.
backend.bindis given aSyncBindingbuilt from the claimed source. A refusal is committed as the turn’serrorwithdone, and nothing is read. - Stored. The engine reads every page of
stored: per document id, the copy’s revision and whether it is searchable. - List.
list_sinceis given the claim’s checkpoint andfull. A listed document whose copy is searchable and has the listed revision is skipped. Any other is fetched, and uploaded withputunless its copy is searchable and has the fetched revision. A text over 64 KiB is sent as one gzip member, and everyputdeclarestext/plain: a reader’s document carries no format. Before eachputthe engine asksbackend.still_bound;falseends the turn withreconnect_required. After each page, onecommitcarries the nextseq, the page’s checkpoint and the counts so far. - Bounds. The turn stops reading after
SyncLimits.documents_per_turnuploads (40), or 30 seconds before the claim ends. The page that was cut is not committed, so the next turn lists it again, and the turn ends withlisting: partial. A reader that stops at its own bound leavesmoretrue on its last page. That page is committed, and the turn ends withlisting: partialtoo, so the source is due again at once and the next turn goes on from the cursor. - Removals. A page’s observations ride its commits, at most 40 a commit, and the
page’s checkpoint is committed with the last of them. A commit that fails moves no
cursor: the last commit carries the checkpoint of the last commit that landed, so the
next turn lists that page, and reports its removals, again.
absentobservations follow the last page. - Done. The last commit is
done, withlisting: completewhen the listing reached its end and, when the turn failed, itserror.
The engine reports a removal only for a copy the source holds, and only on evidence:
| Observation | Evidence |
|---|---|
gone |
A page’s gone list names the id, or its fetch failed with protocol and removed answered so |
denied |
Its fetch failed with denied |
absent |
A listing that this turn read whole, of a listed source in a full turn, did not name the id |
A listing is read whole when one call of list_since read it from its start to its
end, its last page is complete and it named at least one document. A time-out, a
listing that was cut, a listing that is not complete and an empty listing report
nothing. A turn that goes on from a pending cursor reports no absence either: its pages
name only the rest of the listing, and the engine keeps nothing of the turn before it.
A full turn that starts from the checkpoint a finished listing left starts the listing
over, so each such turn can read it whole.
A listed source whose full listing does not fit one turn therefore reports no
absent: a document that left its listing keeps its copy until a full turn reads the
listing in one call.
What is counted and what ends the turn:
| Event | Result |
|---|---|
A fetch fails with protocol or denied, or put is refused with SOURCE_ADDRESS_TAKEN or as invalid input |
failed counts the document and the turn goes on |
A fetch fails with too_large, put answers too_large, or the put is over SyncLimits.max_put_bytes |
too_large counts the document and the turn goes on; a put over the bound is not sent |
put answers over_cap |
over_cap counts it and each later document with no copy, which is not fetched |
put answers over_budget |
over_budget counts it and each later document that needs an upload; no put is sent |
bind, the listing or a fetch fails with any other code that a commit’s error can name |
The turn ends with that error and listing: partial |
bind or the listing fails with too_large or denied |
The turn ends with protocol |
| An op answers what the engine does not know, or is refused with another code | The turn ends with internal |
An op is answered RATE_LIMITED, or a put is answered MODEL_CALL_DEFERRED |
The client waits retry_after_s once; a second answer, or a wait over 60 seconds, ends the turn |
An op is answered CLAIM_LOST |
No further op is sent |
An op after the claim is answered UNKNOWN_OP |
No further op is sent, the turn’s last commit included; the claim ends by its age |
A fetched text that is not UTF-8 also counts its document failed. After over_cap and
over_budget the turn still updates what it can, reports removals and ends without an
error. When an answer that asks for a wait ends the turn, the last commit has
listing: partial and no error, so the next claim goes on from the checkpoint. While
builds are replaced, one build may serve the claim and an older one answer a later op
as unknown. That says nothing about the source, so the turn stops without its last
commit and the source keeps its status.
SyncLimits.max_put_bytes is the longest put the backend’s channel carries, measured
as the JSON of the op’s arguments; a backend with no cap leaves it unset.
The engine keeps nothing between turns. A turn that stops anywhere, a killed process
included, is resumed by the next claim from the last committed checkpoint. A document
uploaded before the stop is skipped from stored, or its put answers unchanged.
source_worker/tests/test_sync_engine.py runs the engine against a scripted reader and a
scripted server, which checks every request against
source_worker/tests/fixtures/worker_ops.json: the JSON Schemas of the four ops.
tests/unit/test_source_worker_wire_fixture.py fails when that file differs from the op
models, and rewrites it when run with LOOM_UPDATE_WORKER_OPS_FIXTURE=1.
Recording fixtures
source_worker/qualification/record_mapped.py records a server through a ToolCaller,
so a person runs it where the connection lives and no credential leaves that place. CI
never runs it.
python source_worker/qualification/record_mapped.py \
--caller package.module:factory --mapping mapping.json \
--out source_worker/tests/fixtures/mapped/<server id> \
--scope <id> --empty-scope <id> --document <id> --missing <id> --truncated <id> \
--denied <id> --name "<a workspace or person name>"
factory() returns the ToolCaller. The scope must list in exactly two pages, and the
listing of what changed since must fit one page: the recorder stops otherwise, because
a recording holds one call of that listing. The recorder writes the mapped tools’
definitions (tools.json), one file a call (list_first, list_second,
list_changed, list_empty, fetch, fetch_missing, fetch_truncated,
fetch_denied), each with the call’s name, arguments and result, and expected.json:
per document its id, title, link, revision, modified time and, for the fetched one, the
SHA-256 of its text.
Before anything is written, every upstream id, every cursor and every name given with
--name is replaced by a neutral value such as doc-0001, the same wherever it recurs.
A UUID is found in either spelling. Any other value is replaced as written and as a
link spells it: with _ for a space, percent-encoded, or both. A number is never
replaced, so a server that names its rows by number is recorded from a workspace made
for the recording. The recorder stops when a call still holds one of the values in one
of those spellings. Tool
definitions are written as advertised, parameter descriptions included and without the
tool’s own description, because their digest is what a mapping pins. Run make format
on the written files before committing them.
source_worker/tests/test_mapping_proof.py replays two recordings through Replay,
which refuses a call the recording does not hold:
source_worker/tests/fixtures/mapped/example/is built by hand for a made-up server.source_worker/tests/fixtures/mapped/wiki/holds what an open-source wiki’s protocol server answered for a wiki of made-up pages.source_worker/mappings/wiki.json, the reference mapping, reads it. The directory’sREADME.mdsays how to repeat the recording.
source_worker/qualification/stand_in_channel.py is the ToolCaller of the second
recording. Channel stands between the reader and a server as an account host’s
channel does: it lists the server’s tools as advertised, and it raises too_large for
a result over its cap, reconnect_required when the account needs a sign-in,
server_off when the server is not enabled and tool_not_allowed for a tool the
host’s policy refuses. Stdio starts the server as a local process. The proof test
puts Channel in each of those states over the recording, and the reader raises the
channel’s code unchanged. The package never imports the module.
source_worker/tests/test_reader_contract.py holds the rules every reader keeps.