Skip to content

An SDK call that carries a run labels its ledger row with it - #2215

Open
zlareb1 wants to merge 2 commits into
mainfrom
fix/sdk-run-header
Open

zlareb1 wants to merge 2 commits into
mainfrom
fix/sdk-run-header

Conversation

@zlareb1

@zlareb1 zlareb1 commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Related: #1760

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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

A call the Python SDK makes inside a run_scope or under auto_run, and a TypeScript SDK call given a runId, is recorded under its run in the op ledger also when the op's input has no run_id field, such as memory.promote. Both SDKs send the run in X-Loom-Run-Id on every such call, and the TypeScript SDK gains the per-call option runId, which labels a read without filtering it.

Why

The server names a call's run on its ledger row from the X-Loom-Run-Id header (set_ledger_run in server/transport/http.py) and reads a body run_id only where the op's input declares one (run_id_of in server/observability/ledger.py). An SDK that puts the run only in the body is recorded under no run on every op that has no such field, and a report built from the ledger misses those calls.

What the SDKs did, read on main:

SDK Sent the header on Gap
Python, run_scope memory.search, memory.search_trace, memory.ask, memory.list every other op; memory.promote carried the run in a body field the server drops
Python, auto_run every memory.*, working_memory.*, observe.* and context.* call except the ten writes that carry the run in the body calls on every other plane (documents, procedures, grants)
Python, ReferenceClient with an open run the same four recall reads every other op
TypeScript no call every call given a runId to an op with no run_id field
Rust every call of a client bound to a run none
Go, Ruby, Java, .NET no call; these clients hold no run and take none per call outside the op's own input not in this change

The fix is the header on every call that carries a run, not a list of ops, so a new op needs no SDK change.

  • Python: scope_label (was scope_read_label) returns the header for every op inside a run_scope, and still takes the scope's run_id off a recall read's body, where it would filter to the run's own rows. AmbientRun.before_post opens the run on a memory-plane call as before, and from then on labels every call on any plane, the stamped writes included. ReferenceClient labels every call while its run is open. A header the caller passes in extra_headers is kept over the ambient run's, as it is over a run scope's, so a call that names its own run in X-Loom-Run-Id is recorded under that run.
  • TypeScript: CallOptions.runId sends the header and leaves the body as it is. Agent.call passes its scope's runId as that option, so the verbs, Session and AgentTools label their calls; the two calls AgentTools sends through the client itself (loom_update, and loom_code_lookup, whose code ops declare no run_id) pass the option directly. A read given runId only as the call option is labelled and not filtered, which the SDK could not do before.
  • A run id is the caller's own string. One with a character outside printable ASCII is not a header value: httpx and fetch refuse it before the request is sent, which would fail a write that works today. Such an id is not sent as a header and the call goes out as before.

Scope and risk

  • SDK only: sdk/python/loom/run.py, transport.py, reference_client.py, agent.py (a docstring); sdk/typescript/src/transport.ts, agent.ts, tools.ts. No server change, no op, field, table or setting. The agent guide in skills/loom/, which mentor.guide serves, says what a Python run scope now labels.
  • The body run_id is sent exactly where it was. A write keeps it (the row's provenance); a recall read inside a scope still carries none.
  • The header is a label. The server reads it into the request's ledger run and nothing on a retrieval or authorization path reads that value. Where a request also names a run in a declared body field, the body's run names the row, as before.
  • More rows name the run than before: the op ledger row, the read receipt and the usage label of every call inside the run. run.get and run.reliance list reads they did not list (a memory.get_by_id or a document read inside a run_scope), and a memory.outcome made inside a run_scope is credited under the run, so run.retract_credit with that run reaches it. Under auto_run this was already so for the memory planes.
  • Under auto_run, the run's calls count in run.complete's metadata is unchanged: it counts the memory-plane calls.
  • Under auto_run, a call that passes X-Loom-Run-Id in extra_headers keeps that label on every op. On main the ambient run's label replaced it on the memory-plane calls the ambient run labelled.
  • The sender census (tests/unit/session_content_senders.json) pins sdk/python/loom/run.py as it is now. No sender call in that file changed: the five reviewed run.* sites keep their keys.
  • Not in this change: a Python call that passes run_id= outside a scope to an op with no such field (Agent.call(op, run_id=...)) still names no run; the Go, Ruby, Java and .NET clients send no run header.

Compatibility, rollout and rollback

  • Wire: no op or field is added, changed or removed. The request header X-Loom-Run-Id, which the server already reads on every op, is sent on more calls.
  • SDK surface: TypeScript CallOptions.runId is new and optional. In Python, loom.run.scope_read_label is renamed scope_label with no alias, and loom.run.run_header is new; neither is exported from loom.
  • Operator steps: none.
  • Rollback: reverting leaves ledger rows, read receipts and usage labels that name a run. They are correct and need no cleanup.

Tests

Failing first, on the tree before the change:

  • tests/unit/test_sdk_run_scope_read_label.py: test_a_call_to_an_op_with_no_run_id_field_carries_the_header and test_a_write_in_the_scope_carries_the_header_and_its_body_run_id failed with assert None == 'run-1' (2 failed, 4 passed).
  • tests/unit/test_reference_client.py::test_an_open_run_stamps_writes_and_leaves_reads_unfiltered failed on the missing header of memory.set.
  • sdk/typescript/src/client.test.ts, run label: 2 failed with expected undefined to be 'r1', 1 passed (the call outside a run).
  • test_a_run_id_a_header_cannot_carry_labels_nothing failed for both ids once every call carried the header, before run_header checked the id.
  • tests/unit/test_sdk_ambient_run.py::test_a_label_the_caller_passes_wins_over_the_ambient_run failed with assert 'amb-1' == 'mine'.
  • run label in sdk/typescript/src/client.test.ts, with a loom_code_lookup call through tools bound to a runId: failed with expected undefined to be 'r2'.
  • tests/unit/test_session_content_senders.py::test_shipped_automatic_senders_have_an_admission_class_or_reviewed_exception failed on the pin of sdk/python/loom/run.py, and on nothing else.

On the final tree:

  • Python unit, the changed tests and the tests of the changed modules: pytest -n 2 tests/unit/test_sdk_ambient_run.py tests/unit/test_session_content_senders.py tests/unit/test_sdk_run_scope_read_label.py tests/unit/test_sdk_run_scope.py tests/unit/test_reference_client.py tests/unit/test_sdk_agent_call.py tests/unit/test_sdk_consumer_identity.py tests/unit/test_sdk_session_protection.py tests/unit/test_sdk_hardening.py tests/unit/test_sdk_retry_logging.py tests/unit/test_sdk_transport_pacing.py tests/unit/test_sdk_sync_run.py tests/unit/test_run_scope_lint.py tests/unit/test_mentor_guide.py tests/unit/test_standalone_skills.py: 293 passed. A call inside a scope to memory.promote and to grants.list carries the header; a call before and after the scope carries none, and neither does the scope's own run.begin or run.complete; a recall read carries the header and no body run_id; under auto_run a label passed in extra_headers is the one sent.
  • TypeScript: biome check ., tsc --noEmit, and vitest run on the eight test files that reach the client and the tools (client, code-tools, tools, memory-tool-receipts, model-memory-metadata, and the ai-sdk, mastra and openai-agents integrations): 153 passed. The four run label cases cover the op with no run_id field, a write, a session, the tools (a memory update and a code lookup), a call outside a run, a read labelled through the call option, and an id that is not a header value.
  • ruff format --check and ruff check on the changed Python files: clean. make typecheck: the mypy gate at 0 errors, mypy --strict sdk/python/loom clean. The function-length gate is at its baseline. Prettier --check on the three changed pages: clean.
  • Repository scans: pytest -n 2 tests/unit/test_package_contracts.py tests/unit/test_op_transport_contract.py tests/unit/test_tunable_constants.py tests/unit/test_store_names_refused.py tests/unit/test_degraded_unnoted.py tests/unit/test_public_copy.py tests/unit/test_doc_links.py tests/unit/test_agent_instructions.py tests/analyzer: 601 passed, 12 skipped (the analyzer's live cases, which need a ClickHouse; no statement changed).

On the first commit's tree, and not run again after the review fixes:

  • Python unit, a wider list of eighteen SDK test files: 279 passed.
  • npm --prefix sdk/typescript run check (Biome, tsc, the version and README checks, every unit test): 40 files, 798 tests passed.
  • Integration, against a server from this branch on a ClickHouse started for it: pytest tests/integration/test_sdk_run_header.py: 1 passed. The loom.agent_events row of a memory.promote made inside a run_scope names the run; the row of one made after the scope closed names none. The review fixes change no path this test takes.

Left to CI: the full unit and integration suites, npm --prefix sdk/typescript run check:dist, the Python SDK wheel smoke and make test-mixed-version. No generator applies: no op, field, setting or CLI command changed, and skills/loom/ is written by hand.

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 under Why against the server and SDK code on main, each new test failing before its fix, and the runs listed under Tests.

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

Checklist

  • Local format, lint, type checks and focused tests pass, as listed under Tests. make format and make lint were not run as a whole: ruff, the type checks, Biome and Prettier were run on what changed. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: none is affected. No op, field, setting or table changes, and no generator reads skills/loom/.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. None added.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. Neither plugin ships a changed file; skills/loom/ ships with the root package, whose version follows the server's.
  • The frontend builds and its tests pass, if frontend/ changed. Not changed.
  • Docs under docs/ describe the system as it now is (docs/features/runs.md, docs/reference/python-sdk.md, docs/reference/typescript-sdk.md, and the agent guide in skills/loom/), and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers (tests/unit/test_public_copy.py passes).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature: none was requested when this was written.

🤖 Generated with Claude Code

The server names a call's run on its ledger row from `X-Loom-Run-Id`, and
reads a body `run_id` only where the op's input declares one. The Python
SDK sent the header on the four recall reads inside a `run_scope`, and under
`auto_run` on the memory, working-memory, observe and context planes except
their writes. The TypeScript SDK sent it on no call. A call inside a run to
an op with no `run_id` field, such as `memory.promote`, was recorded under
no run, so a report built from the ledger missed it.

Python: every call inside a `run_scope` carries the header. Under
`auto_run` a memory-plane call still opens the run, and from then on every
call on any plane carries the header. `ReferenceClient` labels every call
while its run is open. The body `run_id` is sent where it was: on writes,
and never on a recall read, where it filters to the run's own rows.

TypeScript: `CallOptions.runId` sends the header and leaves the body as it
is. `Agent.call` passes the scope's `runId` as that option, so the verbs,
`Session` and `AgentTools` label their calls; the body is unchanged.

A run id is the caller's own string. One with a character outside
printable ASCII is not a header value and would fail the request, so it is
not sent and the call goes out as before.

No server change.

Related: #1760

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vercel

vercel Bot commented Dec 5, 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 5, 2026 6:39am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 6:39am UTC

Request Review

@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 default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f0fa5b9. Configure here.

Comment thread sdk/typescript/src/tools.ts Resolved
…caller passes wins under auto_run

TypeScript: `AgentTools` sends `loom_code_lookup` through the client, not
through `Agent.call`, so the call carried no `X-Loom-Run-Id`. The code ops
it reaches (`code.symbol`, `code.search`, `code.callers`, `code.impact`)
declare no `run_id`, so the server dropped the body's run and recorded the
lookup under no run. The lookup passes the scope's `runId` as the call
option.

Python: `Transport.post_with_meta` applied the `before_post` hook's headers
over the ones the caller passed in `extra_headers`. With the ambient run
labelling every call, a call that named its own run in `X-Loom-Run-Id` was
recorded under the ambient run. The hook's headers are applied the way a
run scope's are: a header the caller passed is kept.

The sender census pins `sdk/python/loom/run.py` as it is; no sender call
in it changed.

The agent guide in `skills/loom/` says every call in a Python run scope is
labelled with the header and that the body `run_id` goes only on the
listed writes. The Python SDK page says a run id outside printable ASCII
is not sent as the header. The TypeScript pages say the tools take `runId`
in their scope.

Related: #1760

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

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.

1 participant