Skip to content

feat(worker): stage-class workers with concurrent slots (D146) - #533

Merged
fazpu merged 7 commits into
mainfrom
feat/stage-class-worker
Nov 6, 2026
Merged

fazpu merged 7 commits into
mainfrom
feat/stage-class-worker

Conversation

@fazpu

@fazpu fazpu commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Relaunch plan phase 2 item (b): stage-class worker (UMC design/plan/relaunch-shared-runtime.md phase 2b; reasoning and the class table in the UMC relaunch analysis §3 items 1, 2, 4, 5). Builds on #529 (claim leases, merged).

What changes

  • CLI. worker --class convert|llm [--concurrency N] replaces worker --stage STAGE. convert = the convert stage, default concurrency 1; llm = every other stage, default 20 (WORKER_CLASSES, DEFAULT_WORKER_CONCURRENCY in profiles/selfhost.py).
  • Loop. SelfHostWorkerLoop now serves a class: N slot threads each run one task at a time through the unchanged Worker.run_one. The main thread holds the LISTEN connection, runs the D145 reaper, and on each notification or fallback poll wakes one idle slot; a slot that finds due work wakes one more before running it, so idle slots look for work one at a time and a burst ramps up quickly. TokenBucket is now thread-safe.
  • Fair order. Routes are every stage's steady lane, then every stage's backfill lane, each downstream first. WorkLedger.due_routes() finds the routes with claimable work in one probe query that reuses the claim's own due predicate (_CLAIMABLE), so an idle class worker does not open one claim transaction per empty route.
  • Reaper per database. WorkLedger.reap_expired_leases(deployment_id, stages=None) sweeps every stage in one pass, so a sweeper serving many deployment databases without their handlers can call it per database (it then announces requeued rows and runs dead-lettered rows' finalizers itself).
  • Execution seam. Worker.run_one delegates to a public Worker.run_claimed(claimed, started_ns); run_one(on_claimed=...) lets the loop wake another slot only after a real claim. The profile's _worker(stages=...) (from the D147 library seams) now builds a worker for several stages and is shared by worker_loop and run_task.
  • Pool. SelfHostProfile.from_settings(settings, engine_options=...) (the shared seam agreed with the library-seams track) lets the worker command size the worker's general pool to N + 2 plus N overflow (one code path nests a read inside a transaction, so a task can briefly hold two connections; renewals and the reaper need a few more). API/setup keep the old pool.
  • Compose. compose.yaml runs worker-convert and worker-llm (concurrency via REMEMBERSTACK_SELFHOST_CONVERT_CONCURRENCY / REMEMBERSTACK_SELFHOST_LLM_CONCURRENCY interpolated into the command) instead of fourteen stage workers. CI's quickstart and upgrade-gate service lists follow. There is no other Compose file in this repo that runs workers (.github/compose/upgrade-gate.yaml only overrides setup).
  • LoCoMo shard runner. The five LOCOMO_*_WORKERS replica knobs (28 replicas) become LOCOMO_LLM_CONCURRENCY (default 40), passed to Compose as REMEMBERSTACK_SELFHOST_LLM_CONCURRENCY.
  • Corpus. D146; packaging §3 "Worker classes", fair order, slots and wake-ups; orchestration §3 backfill bullet; chunk-level extract and claim-normalize fan-out designs no longer name per-stage workers.
  • Docs. Upgrades (docker compose down --remove-orphans, so retired per-stage workers stop), install (services table), CLI reference, scaling (concurrency, containers, connection ceilings), requirements (ceilings, container count), troubleshooting, observability, versions-in-force, choose page, .env.example.

Concurrency safety

An audit of the handler object graph found no shared mutable state mutated during handle() (handlers, catalogs, P1 index, refreshers, adjudicators; one unlocked idempotent CascadeResolver._registered flag whose race only re-runs an ON CONFLICT DO NOTHING seed). The shared httpx.Client and boto3 client are thread-safe. Known non-defects left as they are: boto3's default max_pool_connections=10 logs "pool is full" above 10 concurrent object-store calls (performance only); the structure stage's own 8-thread summary pool multiplies with concurrency.

Tests

  • src/tests/adapters/test_selfhost_worker_classes.py (real Postgres):
    • due_routes lists only routes with claimable work, in the caller's order;
    • one slot drains steady-downstream, steady-upstream, then backfill (fair order);
    • four slots run four tasks at once, with both stages in flight together;
    • two-account interleaving: deployments A and B in two databases on one server, each with its own class loop (3 slots), run side by side in one process; their tasks overlap in time, each loop completes exactly its own six tasks, and neither database holds the other's rows.
  • test_selfhost_profile.py: class split and order, CLI refuses --stage, unknown classes and concurrency 0; Compose wires exactly the two classes; connection-ceiling arithmetic.
  • The D145 kill-mid-task test from feat(ledger): claim leases and a reaper for dead workers (D145) #529 still covers a worker dying with work in flight.

Rule 4 (D56)

No ingestion keyed input, chunking, extraction or reuse identity changes; the same handlers run, only scheduling changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm

@fazpu
fazpu force-pushed the feat/claim-leases branch from a567879 to edf945f Compare November 6, 2026 17:01
fazpu added a commit that referenced this pull request Nov 6, 2026
Address Codex round 1 on #533: a slot probes for due routes before taking
a claim token, so idle wake-ups never drain the bucket; the upgrade guide
stops with `docker compose down --remove-orphans` so retired per-stage
workers do not keep running. Rebased onto the merged library seams (D144):
the class worker builds on the profile's `_worker`, now over several
stages; this decision is D146 and the claim-lease decision is D145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/stage-class-worker branch from 90610c4 to 3c2d4bd Compare November 6, 2026 17:08
@fazpu fazpu changed the title feat(worker): stage-class workers with concurrent slots (D145) feat(worker): stage-class workers with concurrent slots (D146) Nov 6, 2026
@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 1 (gpt-6.1-sol, max)

VERDICT: REQUEST_CHANGES

Reviewed through 90610c4e5.

  1. Major — Idle slots exhaust the shared claim budget. queue.py:216 takes a token before checking for due work. The default 20 slots can spend all 20 burst tokens on empty startup probes. A subsequent work notification can then be consumed while tokens are unavailable; slots remain asleep until the fallback poll, even after refill. Reproduced: 20 idle probes, zero claims after notification, refilled tokens available. Simplest fix: probe for due routes before spending a token, and add this case using the stock concurrency and bucket values.

  2. Major — Documented upgrades leave retired workers running. The service replacement at compose.yaml:251 makes services such as worker-structure orphans. The upgrade guide checks out the new configuration before running plain docker compose down, which leaves those containers running. I confirmed this with disposable Compose services. Old workers retain their interpreters and pools and can reconnect during migration. Simplest fix: change the documented shutdown to docker compose down --remove-orphans in this PR.

Validation: 83 focused checks, 21 reuse/worker checks, and the new reaper check passed; one documented D122 expected failure. Two-account interleaving passed. The due-route probe used indexed route lookups with 100,000 pending rows. No Rule 4 reuse boundary changed.

I made no repository edits.

@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Dispositions for Codex round 1 (fixed in 3c2d4bd)

  1. Major, idle slots exhaust the claim budget — fixed. _run_next probes due_routes first (free) and takes a token only per claim attempt. New test test_idle_wake_ups_spend_no_claim_tokens: a one-token bucket survives several idle wake-ups and still claims the work enqueued afterwards.
  2. Major, upgrades leave retired workers running — fixed. The upgrade guide stops with docker compose down --remove-orphans and says why.

Also in this push, found while waiting for the review: only a real claim wakes another slot (run_one(on_claimed=...)), so a route the probe lists but nobody can claim (a held window-review route) cannot make slots wake each other in a loop (test test_work_that_is_listed_but_cannot_be_claimed_does_not_spin_the_slots); reap_expired_leases(stages=None) sweeps a whole database for a multi-database sweeper; rebased onto the merged library seams (D144), so this decision is now D146 and the class worker uses the profile's shared _worker(stages=...).

@fazpu
fazpu force-pushed the feat/claim-leases branch from edf945f to 4efd3ff Compare November 6, 2026 17:24
fazpu added a commit that referenced this pull request Nov 6, 2026
Address Codex round 1 on #533: a slot probes for due routes before taking
a claim token, so idle wake-ups never drain the bucket; the upgrade guide
stops with `docker compose down --remove-orphans` so retired per-stage
workers do not keep running. Rebased onto the merged library seams (D144):
the class worker builds on the profile's `_worker`, now over several
stages; this decision is D146 and the claim-lease decision is D145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/stage-class-worker branch from fdde26c to 0936e16 Compare November 6, 2026 17:27
@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Codex review round 2 (gpt-6.1-sol, max)

VERDICT: APPROVE

No blocker or major findings. Both round-1 fixes and the rebase resolutions check out.

Minor — e3_entity_obs_flush_fanout_design.md:283 still recommends scaling the retired worker-adjudicate-observations service. Simplest fix: reference worker-llm concurrency or replicas under D146.

Validation: 125 targeted tests passed, one documented expected failure; the worker-class suite passed three additional runs. Shutdown/join, exception propagation, empty-bucket behavior, and index use checked successfully. Ruff and Pyright passed. Rule 4 reuse regressions passed.

No files modified; disposable Postgres removed.

Disposition

Minor fixed in 0936e16: the obs-flush fan-out design now points at the llm worker class. Also since round 2: the worker pool is sized through the shared from_settings(..., engine_options=) seam agreed with the library-seams track (fdde26c), and after #534 moved library seams to D147 the decisions are renumbered: claim leases D144, worker classes D145. No other changes.

@fazpu fazpu changed the title feat(worker): stage-class workers with concurrent slots (D146) feat(worker): stage-class workers with concurrent slots (D145) Nov 6, 2026
@fazpu
fazpu force-pushed the feat/claim-leases branch from 4efd3ff to 68d50ef Compare November 6, 2026 18:18
fazpu added a commit that referenced this pull request Nov 6, 2026
Address Codex round 1 on #533: a slot probes for due routes before taking
a claim token, so idle wake-ups never drain the bucket; the upgrade guide
stops with `docker compose down --remove-orphans` so retired per-stage
workers do not keep running. Rebased onto the merged library seams (D144):
the class worker builds on the profile's `_worker`, now over several
stages; this decision is D146 and the claim-lease decision is D145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/stage-class-worker branch from 0936e16 to 80b1f30 Compare November 6, 2026 18:23
fazpu added a commit that referenced this pull request Nov 6, 2026
Address Codex round 1 on #533: a slot probes for due routes before taking
a claim token, so idle wake-ups never drain the bucket; the upgrade guide
stops with `docker compose down --remove-orphans` so retired per-stage
workers do not keep running. Rebased onto the merged library seams (D144):
the class worker builds on the profile's `_worker`, now over several
stages; this decision is D146 and the claim-lease decision is D145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/stage-class-worker branch from 80b1f30 to f3f1a32 Compare November 6, 2026 18:26
@fazpu fazpu changed the title feat(worker): stage-class workers with concurrent slots (D145) feat(worker): stage-class workers with concurrent slots (D146) Nov 6, 2026
@fazpu
fazpu force-pushed the feat/claim-leases branch from 05d46a2 to 72b49f7 Compare November 6, 2026 20:47
fazpu and others added 7 commits November 6, 2026 22:05
`worker --class convert|llm [--concurrency N]` serves every stage of its
class from one process with N tasks in flight (convert 1, llm 20 by
default). Slots claim the first route with due work: steady lanes before
backfill, downstream stages first, found by one probe query. LISTEN/NOTIFY
and SKIP LOCKED stay; a wake-up rouses one idle slot and a slot that finds
work rouses one more. The main thread runs the lease reaper. The worker's
database pool is sized from its concurrency.

Compose runs worker-convert and worker-llm instead of fourteen stage
workers; docs, the LoCoMo shard runner, CI service lists and the design
corpus follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
A route the probe lists but no slot can claim (a held window-review
route) made slots wake each other in a loop. Worker.run_one now takes an
on_claimed callback, called between the claim and the handler, and the
claimed-attempt execution is its own small method, run_claimed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
WorkLedger.reap_expired_leases takes stages=None to sweep all stages, so a
sweeper serving many deployment databases can run one pass per database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Address Codex round 1 on #533: a slot probes for due routes before taking
a claim token, so idle wake-ups never drain the bucket; the upgrade guide
stops with `docker compose down --remove-orphans` so retired per-stage
workers do not keep running. Rebased onto the merged library seams (D144):
the class worker builds on the profile's `_worker`, now over several
stages; this decision is D146 and the claim-lease decision is D145.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
…ptions=)

Use the engine_options seam agreed with the library-seams track instead
of a worker-specific keyword.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
…gn at the llm class

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
@fazpu
fazpu force-pushed the feat/stage-class-worker branch from f3f1a32 to 7ff63bb Compare November 6, 2026 21:08
@fazpu
fazpu changed the base branch from feat/claim-leases to main November 6, 2026 21:08
@fazpu
fazpu marked this pull request as ready for review November 6, 2026 21:08
@fazpu

fazpu commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

#529 merged; rebased onto main 860c757 (incl. #537, #538) with no conflicts, base retargeted to main. Local quality gates and targeted tests pass (110).

@fazpu
fazpu merged commit 122518d into main Nov 6, 2026
20 checks passed
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