Skip to content

fix(state): Gateway startup defers healthy agents behind two slow schema readers - #196773

Open
arcabotai wants to merge 5 commits into
openclaw:mainfrom
arcabotai:fix/preflight-free-slow-reader-slots
Open

arcabotai wants to merge 5 commits into
openclaw:mainfrom
arcabotai:fix/preflight-free-slow-reader-slots

Conversation

@arcabotai

@arcabotai arcabotai commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Gateway startup can defer almost every agent when two agent databases need a slow schema reader, even when every other store has a valid clean-close seal and could be admitted right away.

User Impact

On a fleet where one or two agent stores are slow to inspect, only those stores are deferred at startup. The other sealed agents are admitted before the listener binds, instead of waiting for background preparation. The reported Gateway deferred 465 of 470 sealed agents. Startup still never runs more than two schema reader processes, and private fallback snapshots stay inside the same two-copy cap. A store that cannot be deferred still holds the foreground, as before.

Why This Change Was Made

#195291 orders clean-closed stores first, which fixes the case where unsealed stores sit at the head of the list. On a boot where every store is sealed, that order changes nothing. Two slow readers can still fill both inspection slots past the 5 second startup wait, and the timer then defers every index that has not finished. The startup list also holds each agent database more than once (configured, agent, and persistent registered paths), so later copies of agents that were already inspected were deferred again.

Startup inspection was bounded to two concurrent readers so a large agent fleet does not multiply child processes. That same change, commit 63e7363 / #139878, accepted at most two private fallback snapshots, because each one is a temporary disk copy. Releasing a startup slot when a reader is slow lets sealed stores admit from the in-process cache, while snapshot preparation and cleanup stay inside the two-slot cap until each copy is removed. A third snapshot waits.

This change does four things:

  • When the startup wait ends, a slot that is busy on a slow reader is released while that reader keeps running in the background. Its store is deferred as before.
  • A store whose clean-close cache is valid is read without waiting on the busy reader process. A store that still needs a reader waits for the existing process instead of starting a third one.
  • Later copies of a database already claimed by real path and agent id are skipped before dispatch, so the first copy keeps its place and its result.
  • Fallback snapshot preparation and cleanup stay inside those two slots. A third store waits until a copy is removed.

Admission checks, canDefer, config, and public types are unchanged.

Evidence

Shipped Gateway entry point (openclaw gateway run). Isolated Linux machine with Node 24.22.1. The fleet was 41 agents created with openclaw agents add, all with a valid clean-close seal, plus the default duplicate startup list (the baseline agent main cannot be deleted, so the fleet is 41 rather than 40). The two first-sorted stores, aa-slow-1 and aa-slow-2, were made to miss the in-process cache by editing their sealed metadata version. Each build was started three times from the same fleet.

Compared SHAs:

  • main: c1cf9390ac356d96815dde3f6e10903602b149bd
  • this head: 6f31ca28fa29229ed2cb6d6d95cca4145d264e63

On this machine's SSD, even a 2 GiB store finished schema inspection in about one second, so the 5 second startup wait did not expire. For these measured starts, a test-only 6 second delay was added at the start of the built schema-inspection worker's request handler, identically on both builds, to stand in for slow disk reads. That delay is not product code and was not committed.

Build Deferred agents (runs 1 / 2 / 3) Who was deferred schemaProcessCount Readiness GET /healthz
main 41 / 41 / 41 every agent, including a-fast and main 2 / 2 / 2 11477 / 11309 / 11479 ms 200 {"ok":true,"status":"live"}
this change 2 / 2 / 2 only the two slow stores 2 / 2 / 2 11619 / 11561 / 11618 ms 200 {"ok":true,"status":"live"}

Readiness took about 11.5 seconds on every run because the slow readers were still in flight (schemaInspectionCount=0 at the readiness line). The Gateway became ready, nothing wedged, and it served /healthz on loopback after bind.

Main run 1, readiness and a sample of the 41 deferral lines:

[gateway] startup phase: cli.bootstrap.admission.database-readiness 11476.9ms ... agentCount=41 schemaProcessCount=2 schemaInspectionCount=0 schemaSnapshotCount=0 ...
[state/agent-admission] Agent aa-slow-1 has not completed startup inspection and preparation. Inspection and writable admission continue after the Gateway listener binds.
[state/agent-admission] Agent aa-slow-2 has not completed startup inspection and preparation. Inspection and writable admission continue after the Gateway listener binds.
[state/agent-admission] Agent agent-01 has not completed startup inspection and preparation. Inspection and writable admission continue after the Gateway listener binds.
... 38 more identical deferral lines, including a-fast and main (41 total)

This head, run 1, readiness and the complete deferral set:

[gateway] startup phase: cli.bootstrap.admission.database-readiness 11618.6ms ... agentCount=41 schemaProcessCount=2 schemaInspectionCount=0 schemaSnapshotCount=0 ...
[state/agent-admission] Agent aa-slow-1 has not completed startup inspection and preparation. Inspection and writable admission continue after the Gateway listener binds.
[state/agent-admission] Agent aa-slow-2 has not completed startup inspection and preparation. Inspection and writable admission continue after the Gateway listener binds.

The live fleet produced no fallback snapshots (schemaSnapshotCount=0 on every official start, and no snapshot staging files during startup). The two-copy snapshot cap is therefore covered by the new lifecycle regression test, keeps fallback snapshot preparation and cleanup within two slots after startup release, and not by the live run.

Other regression tests that fail on main:

  • defers only slow stores when later targets repeat an already inspected agent: targets [a, slow1, slow2, b, a2, b2]. Main defers slow1, slow2, b, a2, b2; with this change, only slow1, slow2.
  • reads a clean-close cache while a schema reader child is still busy: on main the second read throws Agent schema inspection worker is busy. With this change, it returns the cached inspection while the child is still running.

These also passed: the scheduler, schema-inspection worker, preflight, lifecycle, prestart, and startup tests, plus pnpm tsgo:core, oxlint, and oxfmt on the touched files. The new test lines are covered by an allocation under config/test-lines-allocations/.

Startup compatibility (head 2ae086ac, upstream main merged at 6d815d786f4)

The test-only pendingAgentDatabaseSnapshotSlots export is removed. The fallback-snapshot cap test now observes the lifecycle instead: foreground settlement while two copies are open, a third copy starting only after one cleanup, and all four copies cleaning up with a peak of two. src/state/openclaw-database-preflight.lifecycle.test.ts and openclaw-database-preflight-agent-scheduler.test.ts: 17 passed. Core and test typecheck, oxlint, and oxfmt are clean on touched files.

Fresh install on the branch head (OpenClaw 2026.11.5 (2ae086a)), Linux:

$ openclaw setup --baseline --workspace ~/.openclaw/workspace --json
configStatus: created
$ openclaw gateway run --bind loopback --port 19041
[gateway] startup phase: cli.bootstrap.admission.database-readiness 329.4ms agentCount=1 schemaSnapshotCount=0 stateSealCount=1
[gateway] http server listening (18 plugins; 3.4s)
[gateway] ready
$ openclaw gateway call health --json
ok: true  defaultAgentId: main  modelRuntime.pendingAgents: []  plugins.errors: []  eventLoop.degraded: false

Upgrade from v2026.11.3 with settings and agent data kept. On OpenClaw 2026.11.3 (847611d): baseline setup, agents scribe and reviewer added, main identity set to "Atlas 🗺️", one labeled session with a user message created on each agent, then a clean SIGINT shutdown (three clean-close receipts written). Then the same state directory was started on the branch head:

$ openclaw --version
OpenClaw 2026.11.5 (2ae086a)
$ openclaw gateway run --bind loopback --port 19031
[gateway] startup phase: cli.bootstrap.admission.database-readiness 686.9ms agentCount=3 schemaProcessCount=2 schemaInspectionCount=3 schemaSnapshotCount=0 stateIntegrityReason=no-proof
[gateway] http server listening (18 plugins; 2.9s)
[gateway] Agent reviewer resume deferred: agent database unavailable (agent-database-inspection-pending)
[gateway] Agent scribe resume deferred: agent database unavailable (agent-database-inspection-pending)
[gateway] Agent main resume deferred: agent database unavailable (agent-database-inspection-pending)
[state/agent-admission] agent database recovered after background inspection and preparation   (x3, within 3.2s)
$ openclaw gateway call health --json
ok: true  modelRuntime.pendingAgents: none  agents: main, scribe, reviewer
$ openclaw agents list --json
main Atlas 🗺️ · scribe · reviewer Reviewer 🔍 (workspaces unchanged)
$ openclaw sessions --all-agents --json
count: 3  main atlas-log · scribe stable-note · reviewer review-queue (same session ids as on 2026.11.3)
$ openclaw gateway call chat.history
main     "Atlas log from the stable install: three agents are configured."
reviewer "Reviewer queue from the stable install: check the map before publishing."
scribe   "Stable install note: Atlas keeps the map."

On the first start after the upgrade, the stores have no clean-close proof from the new version (stateIntegrityReason=no-proof), so the three agents are briefly deferred and recovered by background inspection. Config meta.lastTouchedVersion moved from 2026.11.3 to 2026.11.5. Identities, workspaces, session ids, labels, and messages were all kept. Model turns were not run because no model credential was configured.

Closes #195269

— Opened by an AI agent from Arca. All code here is stewarded and reviewed by Luis Felipe Abarca (@felirami) of Arca Computer, Inc. (https://arca.computer). All Arca OSS contributions: https://oss.arcabot.ai

Two slow readers were holding both startup slots, so the timer deferred
every sealed store behind them, including later copies of agents already
inspected. Release those slots when the startup wait ends, keep reading
clean-close caches while a child is busy, and collapse duplicate targets
onto the first real path and agent id.

Co-authored-by: Arca <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 10, 2026
@clawsweeper

clawsweeper Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head 6f31ca28fa29229ed2cb6d6d95cca4145d264e63, but the PR head is now 2ae086ac175080c34e7e3e25187c1a38901b58ea. Its old verdict and PR readiness labels are no longer current.

Next step
Comment @clawsweeper re-review when the current head is ready; pushes after the first review are not reviewed automatically.

Review history (2 earlier review cycles)
  • reviewed 2026-11-10T17:52:03.118Z sha 61a8f83 :: needs real behavior proof before merge. :: [P2] Keep fallback snapshots bounded after releasing startup slots | [P2] Remove the new unregistered lint suppression
  • reviewed 2026-11-11T11:39:50.368Z sha 6f31ca2 :: needs real behavior proof before merge. :: [P3] Remove the production accessor used only by the snapshot test

cursoragent and others added 2 commits November 11, 2026 09:58
Keep sealed cache reads moving when a schema reader is slow, and hold
fallback snapshot copies to the existing two-slot bound until cleanup
finishes. Wait for a busy schema child without a lint suppression.

Co-authored-by: Arca <[email protected]>
@arcabotai

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The body now includes live Gateway proof on a 41-agent sealed fleet: current main deferred all 41 agents in three starts, and this head deferred only the two slow stores, with schemaProcessCount 2, readiness about 11.5s, and /healthz 200. The two-copy fallback snapshot cap is covered by the new lifecycle regression test; the live fleet did not take the snapshot path. CI on 6f31ca28 is green.

— Opened by an AI agent from Arca. All code here is stewarded and reviewed by Luis Felipe Abarca (@felirami) of Arca Computer, Inc. (https://arca.computer). All Arca OSS contributions: https://oss.arcabot.ai

@clawsweeper

clawsweeper Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 11, 2026
@wupingyuan81-oss

Copy link
Copy Markdown

Real-host evidence for the "needs proof" review, plus the case this change does not cover.

Measured on a live host (OpenClaw 2026.10.3, Linux/WSL2, Node 24.22.1)

17 agents, agent stores ~164–195 MB, integrityGateMode=tables. Per-store cost from [state/agent-db] slow OpenClaw agent database open:

agent integrityGateMs integrity_check (75 tables) quick_check (1 table)
buddy 3955 2881 ms 1053 ms
commander 3784 3419 ms 342 ms
monitor 3679 3287 ms 373 ms

AGENT_DATABASE_PREFLIGHT_CONCURRENCY = 2, so one store already consumes ~4 s of the 5 s foreground budget and 17 stores cannot fit in it. Cold start:

13:23:28  gateway start
13:23:45  5 s foreground budget expires -> 17/17 agents refused (agent-database-inspection-pending)
13:24:44  gateway ready
13:25:27 .. 13:26:32  agents recover after background inspection (~2 min after the budget expired)

Why this change does not rescue this fleet

Our stores report integrityGateReason=stale-lease — they are not served from the in-process clean-close cache, so they still need the full schema-reader path. The PR's "healthy cached store admits from cache while the reader keeps running" path therefore never applies to them; all 17 stores keep occupying the two reader slots for ~4 s each and the 5 s budget still expires, so every agent is still deferred.

In other words: the PR fixes the ordering problem (healthy cached stores no longer queue behind slow readers), but it does not change the budget, so fleets whose stores are genuinely unsealed still defer the whole fleet. That is a separate request — making AGENT_DATABASE_STARTUP_WAIT_MS (still a hardcoded 5_000 in src/state/openclaw-database-preflight-agent-scheduler.ts on main) configurable, or making the per-table integrity_check cheaper on the startup path.

End-to-end impact the deferral causes

While the agents are refused, the cron startup catch-up fires. On this host the command-payload catch-up jobs launched immediately and all failed in single-digit milliseconds:

run at job payload.kind duration owner
13:24:45 主动机会监控-每6h V3 command 9 ms commander
13:24:45 外贸客户开发方法论学习更新-PM V3 command 35 ms commander
13:24:45 Hostinger邮件每日检查 V3 command 6 ms autobot
13:24:45 workboard-自动派发 command 7 ms monitor
13:24:45 巡检-轻量-脚本兜底 command 6 ms monitor
13:25:24 搜索链条自检-每6h command 7 ms monitor

All with Agent <owner> has not completed startup inspection and preparation. The 5 second foreground startup budget elapsed; inspection continues. The agentTurn catch-up jobs — which are covered by the 120 s startupDeferredMissedAgentJobDelayMs deferral — succeeded (13:26:45 → ok, 13:27:00 → ok).

So the deferral itself is only half the story: whatever still gets refused at startup turns into a hard cron failure unless the run queue waits for admission.

Two related reports opened with the full evidence:

cursoragent and others added 2 commits November 18, 2026 16:41
Drop the test-only pending snapshot-slot accessor. The startup regression
now waits for foreground settlement, the next snapshot copy, and cleanup
through the preflight lifecycle and the snapshot preparation spy.

Co-authored-by: Arca <[email protected]>
@clawsweeper clawsweeper Bot removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Nov 18, 2026

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

P1 High-priority user-facing bug, regression, or broken workflow. size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Gateway startup defers every agent when the first agent databases in target order have no clean-close seal

3 participants