Skip to content

A worker source's copy turn sends its worker requests with bounded concurrency - #2209

Open
zlareb1 wants to merge 6 commits into
mainfrom
feat/worker-copy-concurrency
Open

zlareb1 wants to merge 6 commits into
mainfrom
feat/worker-copy-concurrency

Conversation

@zlareb1

@zlareb1 zlareb1 commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Related: #2165
Related: #2196

Changelog category (leave one):

  • Performance Improvement

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 worker source's copy turn can keep up to knowledge_source_worker_copy_concurrency worker requests in flight (default 1, as before); raising it lets a turn wait on several worker requests at once instead of one, once each worker has the slots for it. Its effect on how many copies one process serves is not measured yet.

Why

A hosted run of the copy capacity workflow (24 sources × 10 resources) measured, in Loom, about 234 ms per copy and 302 ms per access check at the median, while the worker answered in about 93 ms. A process sent those requests one at a time within the mirrored phase of each sync pass. At the default settings that keeps roughly a thousand copies served per process. Most of each request's time is Loom's own authority reads before and after it, which stay as they are. So the request count in flight is the lever.

Scope and risk

  • Setting: knowledge_source_worker_copy_concurrency, default 1 (one request at a time, as before), range 1 to 16, read at boot and at each turn, not live. Raising it is the operator's step: each worker's read_limits.concurrent_per_org needs at least the setting plus room for the org's live reads and setup, and twice the setting where a manual loom knowledge sync runs beside the background pass (or a lease handover overlaps two passes); otherwise live reads and the turn's own requests answer overloaded. Only the turn's own request answering overloaded ends the turn.
  • Turn (server/knowledge/app/source_sync_worker.py):
    • Requests start in the existing visit order: resources without a copy first, then stored copies by oldest confirmation. The time budget, pass_context.check_stop() and the document count are checked before each start.
    • Document bound: a copy takes its place in run.left before it is sent and gives it back if it brings no document, so run.left never goes below 0. A turn may leave a place unused that a later pass picks up.
    • Refusals: a refusal that ends the turn stops new requests. Requests already in flight finish and their answers apply, but the refusal that ended the turn stands: a request refused after it, including an upstream denial whose authority read outlasts it, keeps its copy and leaves the status alone. A resource without a copy whose own answer (an upstream denial or unsupported) arrives after the turn ended waits its refresh interval; any other refusal waits half the stale window. An upstream denial answered after a refusal ended the turn retires nothing; one answered before it retires, as it does one request at a time.
    • The lapsing retirement and the listing run only after every request has finished, with their existing gates.
    • An error in one request is raised after the others finish. A cancelled turn cancels its in-flight requests and waits for them.
    • Each request still resolves its authority before and after the reply; nothing about standing is cached.
  • Channel (server/knowledge/app/mcp_worker_read.py, mcp_worker_routes.py): copies and access checks share one sync share sized from the setting through a copy_slots argument, replacing the fixed stored and copy shares, and the connection pool counts it once.
  • Capacity workflow: a copy_concurrency input (default 2) reaches Loom only through the job's environment. The driver records the setting and refuses, as a harness error, a value past the worker's concurrent_per_org.
  • Docs: the turn and Capacity paragraphs of docs/architecture/mcp-source-worker-boundary.md, and the source worker README's read_limits note (concurrent_per_org needs at least the setting plus room for live reads).

Compatibility, rollout and rollback

  • Wire: none.
  • Settings: one new setting; the settings reference, schema and example are regenerated.
  • Rolling upgrade: with the default of 1 nothing changes; each process bounds only its own requests.
  • Operator steps: before raising the setting, give each worker's read_limits.concurrent_per_org at least that many slots plus room for live reads.
  • Rollback: reverting brings back one request at a time.

Tests

  • tests/unit/test_worker_source_copies.py (91 passed): the existing tests run one request at a time. Fourteen new ones, thirteen at 2 or 3 in flight and one at 1, cover:
    • requests overlapping up to the limit and never more, in visit order, with the listing recorded only after all finish;
    • the document bound never overspent, and a copy with no document giving its place back;
    • a turn-ending refusal starting no new request while the answer in flight still applies, and standing against a refusal answered after it;
    • an upstream denial answered after a refusal retiring nothing, and one answered before it retiring;
    • a denial whose authority read outlasts a peer's turn-ending refusal keeping that refusal and, when the authority still holds, its stored copy; an uncopied resource's own answer (unsupported or an upstream denial) after the turn ended waiting its refresh interval;
    • lapsing retired when a peer check confirms and kept when a check beside it ends the turn;
    • an error raised after the others finish, and cancellation reaching in-flight requests.
  • tests/unit/test_mcp_worker_read.py (28) and tests/unit/test_mcp_worker_routes.py (6) cover the shared sync share and the setting reaching the channel; tests/unit/test_bench_source_worker_copies.py (59) covers the workflow input and the driver's room check.
  • Each new guard was broken once and a test failed.
  • Through the focused gate also: test_tunable_constants.py, test_doc_links.py, test_public_copy.py, test_settings_reference.py, test_settings_every_field_read.py, test_live_settings.py. ruff, the function-length gate, the settings and docs generator --checks, the prose checker and Prettier pass; mypy shows no errors in the changed modules.
  • The next capacity run measures it at the workflow's copy_concurrency of 2.

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 wrote the concurrent turn, the setting and the tests. I set the scope from the measured run (concurrency as the lever, authority reads unchanged) and checked the bound and refusal handling.

  • 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. 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: the settings reference, schema and example.
  • No migration.
  • No plugin-shipped file changed.
  • No frontend change.
  • Docs under docs/ describe the system as it now is.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off.

🤖 Generated with Claude Code


Note

Medium Risk
Changes mirrored sync turn scheduling, refusal/retirement timing with overlapping requests, and worker channel capacity; default 1 preserves prior behavior but mis-tuned concurrency can cause overloaded on workers or alter copy-serving bounds.

Overview
Adds knowledge_source_worker_copy_concurrency (default 1, range 1–16) so each process’s worker copy turn can keep multiple copy and access-check requests in flight instead of strictly serializing them.

The sync turn schedules due resources in the same order as before but starts up to the limit in parallel via _Visits (asyncio tasks). A turn-ending refusal stops new starts while in-flight work still settles; document-bound slots are reserved before a copy is sent and returned when no document is written. On the worker channel, separate stored/copy slot pools are replaced by one sync share sized from the setting.

The source-worker copy capacity workflow gains a copy_concurrency input (default 2); the bench driver records the value and fails fast if it exceeds the worker’s concurrent_per_org.

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

…ncurrency

A process sent the copies and access checks of a worker source's turn one
at a time, so the copies one process keeps served were bounded by the
mirrored phase time in half the stale window divided by one request's
time in Loom (authority reads and the worker's reply). A turn now keeps up
to knowledge_source_worker_copy_concurrency requests in flight, which
raises that bound by the same factor.

The concurrency is a setting, not a constant: each worker's
read_limits.concurrent_per_org must hold it beside the org's live reads,
and only the operator knows both. The default is 2, the floor a worker
that serves setup already meets (a selection proof runs two checks), so
an existing deployment keeps working; 1 restores one request at a time.
It is read at boot for the channel shares and at each turn, and is not
live.

What holds under concurrency:
- Requests start in the visit order (resources without a copy, then the
  oldest confirmation) and the pass's time and stop checks run before
  each start.
- A copy reserves its place in the document bound before it is sent and
  gives it back when no document arrives, so the copies in flight never
  spend more than the bound has left.
- A request that ends the turn stops new ones; those in flight finish and
  their answers apply. An upstream denial answered beside such a refusal
  keeps its copy, so a turn that ended on a refusal retires nothing.
  Lapsing retirement and the listing still run only after every request
  finished, gated as before.
- An error raised by one request is raised once the others finish; a
  cancelled turn cancels its requests in flight.
- Every request still resolves the owner's authority before and after.

The channel's stored-check and copy shares each hold the setting's number
of slots, counted in the connection pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.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:20am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 6:20am 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 2 potential issues.

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 c8b9e6a. Configure here.

finally:
await visits.finish()
if visits.error is not None:
raise visits.error

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Finished peer copies discarded on error

Medium Severity

If one request raises, the others are drained and can still write a copy or confirm access. The stored error is then raised out of the turn before _keep, and the sync caller records a fresh failed result. Those finished updates never reach sync state, so a written copy can disagree with the recorded digest and confirmation.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c8b9e6a. Configure here.

zlareb1 and others added 5 commits December 5, 2026 05:48
…share

A default of 2 would raise every process's sync load on each worker at
upgrade, and a worker at the setup minimum (concurrent_per_org 2) would
lose its room for live reads: live reads and turns would answer
overloaded, turns would end and listings would lapse. The setting now
defaults to 1, today's behaviour; raising it is the operator's step, with
each worker's concurrent_per_org at least the setting plus room for the
org's live reads and setup.

The channel kept a stored-check share and a copy share of copy_slots each,
reserving twice what a turn can use. Copies and checks now take one sync
share of copy_slots, counted once in the connection pool.

The capacity workflow takes a copy_concurrency input (default 2, inside
the worker's default concurrent_per_org of 4), passed only through the
environment. The driver records the setting in environment.loom and
refuses a value above the worker's concurrent_per_org, which would read
as a figure rather than a misconfigured run.

The docs say that an upstream denial answered after a refusal ended the
turn retires nothing, while one answered before it retires, as one
request at a time; a test covers the denial-first order. Only the turn's
own request answering overloaded ends it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.com>
…pass

A manual loom knowledge sync, or a lease handover, can run a second copy turn
for an org against the same worker, so the sizing advice now allows for twice
the setting there. The copy turn drains its requests in one place, and a test
that read the HTTP client's private pool is dropped; the share tests cover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.com>
With requests in flight, a refusal answered after another had ended the turn
replaced its status, so the source showed whichever came last. A request
answered after the turn ended now keeps its copy and leaves the status alone. A
request that raised just before a cancel has its error read, and the routes
test says it checks only the wiring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.com>
A copy's upstream denial whose authority read outlasted a peer's turn-ending
refusal replaced that refusal with its own. It now fails the turn only while the
turn is still ok. An uncopied resource whose own answer arrives after the turn
ended waits its refresh interval, as the module says, and the boundary page says
copies and confirmations share one set of slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.com>
…'s read

The guard keeping a stored copy whose upstream denial was read again while a
peer ended the turn had no test. One now covers it, the own-answer wait covers
an upstream denial as well as unsupported, and the provisioning paragraph says
copies and their checks share the copy concurrency slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: zlareb1 <lareb.zafar@clickhouse.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