Repository navigation
Description
Part of #754.
Scope
A gateway keeps its drained workers in its state.db and refuses to start while a workers.json sits in its folder. simlock worker drain, undrain and remove work as today, and a drain survives a gateway restart.
In short
The gateway's drain store reads and writes the drained_workers table, one row per drained worker id, instead of workers.json. A write that fails is still logged and swallowed, since the drain already holds in memory.
flowchart LR
D[worker drain / undrain / remove] --> R[WorkerRegistry]
R --> S["StateDrainStore: drained_workers (changed)"]
G[gateway start] --> O{"workers.json present? (new)"}
O -->|yes| X[refuse]
O -->|no| L[load drained ids]
If this goes wrong, a user would notice a drained worker taking fleet requests again after a gateway restart.
Technical spec
Facts
Commit 8db8112b.
| # | Fact | Source |
|---|---|---|
| F1 | FileDrainStore keeps {"drained": [...]} in workers.json; a read error logs Could not read the gateway's worker registry; drains are forgotten and returns []; a write error logs Could not persist the gateway's worker registry and is swallowed |
src/gateway/drain-store.ts:25-80 |
| F2 | DrainStore is load(): Promise<readonly string[]> and save(ids): Promise<void>; MemoryDrainStore implements it for tests |
src/gateway/drain-store.ts:20-23, :83-93 |
| F3 | WorkerRegistry.load adds each loaded id; setDrained changes the set, then saves the whole set; also at :504, :591 |
src/gateway/worker-registry.ts:140-142, :465-472 |
| F4 | GatewayService.start loads drains before the listener opens |
src/gateway/AGENTS.md "registry.load() (drained ids) before the listener opens"; src/daemon/main.ts:901 |
| F5 | startGatewayDaemon builds FileDrainStore at join(dataDirectory, "workers.json") |
src/daemon/main.ts:755-765 |
| F6 | src/gateway/index.ts:15, :17 export FileDrainStore and DrainStore |
as cited |
| F7 | Tests using the drain stores: src/gateway/drain-store.test.ts, service.test.ts, dispatcher.test.ts, component-relay.test.ts, worker-registry.test.ts |
rg -l "DrainStore" src -g '*.test.ts' |
| F8 | Task 1 gives the gateway a state.db with an empty owner list and old-file list; task 1 moves the transaction helper into src/ports/sqlite.ts |
#754 Shared values |
| F9 | The gateway may import ports | src/gateway/AGENTS.md "Uses: … ports" |
| F10 | A gateway restart test exists: it drains a worker, restarts the gateway on the same directory and asserts drained: true through worker.list once the worker reconnects; its noWait NO_CAPACITY check runs only before the restart |
src/daemon/gateway-fleet.e2e.test.ts:410, :485-491, :530-553 |
| F11 | Other files naming workers.json: src/gateway/worker-registry.ts:495, :569, src/gateway/worker-registry.test.ts:886, :1028, src/ports/uplink-websocket.ts:376, src/daemon/AGENTS.md:39, src/daemon/gateway-fleet.e2e.test.ts:164, :406, :530, :539 (all comments) |
rg -n "workers\.json" src e2e docs --glob '!docs/internal/adr/**' |
Modules touched
Searches: DrainStore, workers.json, drainStore over src/, e2e/, docs/.
src/gateway/drain-store.ts(StateDrainStorereplacesFileDrainStore; exportsdrainedWorkersOwner),src/gateway/index.ts.src/daemon/main.ts:drainedWorkersOwnerin the gateway's owner list,workers.jsonin its old-file list,new StateDrainStore({ database, logger }).src/gateway/drain-store.test.ts;src/daemon/gateway-fleet.e2e.test.ts(the F10 test).- Comments naming
workers.json(F11):src/gateway/worker-registry.ts,src/gateway/worker-registry.test.ts,src/ports/uplink-websocket.ts,src/daemon/gateway-fleet.e2e.test.ts, andsrc/daemon/AGENTS.md:39. - Docs:
docs/CONFIGURATION.md:200-201,docs/internal/ARCHITECTURE.md:30-31,docs/internal/flows/startup.md:98,src/gateway/AGENTS.md(file table,Persisted:line),docs/CLI.mdupgrade section (drains are set again).
flowchart LR
main["daemon/main.ts *"] --> ds["gateway StateDrainStore *"]
ds --> ports[ports/sqlite.ts]
wr[WorkerRegistry] --> ds
Surface and extension points
DrainStore is unchanged. Owner drained-workers, version 1: table drained_workers with worker_id (key). save(ids) replaces the set in one transaction (delete all, insert each). load() answers the ids in insertion order (ORDER BY rowid).
Contract and event changes
None (searched src/contract for drain: the worker.drain operations are unchanged).
Other code on the same state
Searched drainStore, save(, workers.json: WorkerRegistry is the only caller (F3). Task 3's tokens table shares the gateway's state.db. Its transactions are separate and synchronous, so neither waits on the other beyond SQLite's lock.
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
save |
ok | one transaction | — |
| SQLite throws | rollback, logged Could not persist the gateway's worker registry, swallowed (F1) |
busy wait 5 s, from the statement | |
load |
ok | ids | — |
| SQLite throws | logged Could not read the gateway's worker registry; drains are forgotten, answers [] (F1) |
— | |
| start | workers.json present |
refuse with the old-file message | — |
Rules in play
- ADR 0036 §1, the drained workers in the gateway's
state.db; §5, refuse on an old file. src/gateway/drain-store.ts:35-41"refusing to start would take a whole fleet's routing offline to protect a flag": kept for read and write errors after a good open. A damaged file refuses at open (task 1), as ADR 0036 §5 decides.
Tests
Seam: the drain store over openMemoryStateDatabase([drainedWorkersOwner]) (model: src/gateway/drain-store.test.ts), and startDaemon as a gateway (model: src/daemon/gateway-fleet.e2e.test.ts:410).
savethenloadreturns the same ids in the order saved, with ids not in sorted order (wrk_b,wrk_a), a latersavereplaces the set (including with none), and a store on a fresh database loads[].- A
savewhose statement throws (the table dropped in the test) resolves, and aloadthat throws answers[]. - A gateway restarted on the same
state.dbkeeps a worker drained: extend the F10 test so it runs onstate.db, and repeat itsnoWaitNO_CAPACITYcheck once the drained worker has reconnected after the restart (between:545-550and:552), so the check fails if the drain was lost. startDaemonas a gateway withworkers.jsonin its folder throws the old-file message naming it (the log line and exit code are state.db opens and checks at start, and the worker's registry lives in it #761's shared refusal path).
Done when
pnpm test:changedpasses.rg -n "workers\.json" src docs --glob '!docs/internal/adr/**'finds only the gateway's old-file list, its test and thedocs/CLI.mdupgrade section.
Out of scope
- The gateway's in-memory lease requests and lease index.
- Importing
workers.json.
Depends on
- task 3 (Tokens live in state.db on both daemon kinds #758): both change the gateway's owner and old-file lists in
src/daemon/main.ts.
Written by an agent.
Activity
[-]Tokens live in state.db on both daemon kinds[/-][+]The gateway's drained workers live in state.db[/+]docs: the gateway's drained workers live in state.db (#759)
docs: the drain store's comments name the table, not a file (#759)
test: fold the workers.json refusal into the table, drop log-string a…
fix(e2e): startDaemon stops waiting for admin.token when the daemon a…
fix(e2e): a console daemon start waits for its HTTP listener, so a po…
feat(gateway): the gateway's drained workers live in state.db (#786)