Repository navigation
fix: gateway startup waits for concurrent first-run migrations - #120959
Conversation
…iling gateway startup
|
Codex review: needs maintainer review before merge. Reviewed August 9, 2026, 2:44 AM ET / 06:44 UTC. ClawSweeper reviewWhat this changesThe PR makes same-host processes wait for a concurrent first-run migration lease and refreshes migration checkpoint facts after acquiring that lease. Regression provenancePossible regression — probable (reproduction; reviewed change). No predecessor PR is attributed. Merge readinessKeep open for normal maintainer handling. The patch is a coherent owner-boundary repair for the current first-run race; no blocking correctness or security defect was found, and the maintainer/protected label prevents automated cleanup. Priority: P1 Review scores
Verification
How this fits togetherGateway and worker startup enter doctor preflight against one state directory. Preflight uses a shared migration lease and recorded checkpoints to decide whether migrations must run before startup continues. flowchart LR
A[Gateway and worker startup] --> B[Doctor preflight]
B --> C[Shared migration lease]
C --> D{Same-host owner active?}
D -->|yes| E[Bounded wait and retry]
D -->|no| F[Refresh checkpoint facts]
E --> F
F --> G[Run or skip migrations]
G --> H[Startup continues]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +108, Tests +70. Total +178 across 5 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep lease policy centralized in the checkpoint owner, preserving fail-fast behavior for non-local or unprovable owners while serializing legitimate local first-run contenders. Do we have a high-confidence way to reproduce the issue? Yes, from source: a gateway startup path reaches synchronous lease acquisition, and a live owner currently throws immediately; the branch directly covers that transition with focused regression tests. Is this the best way to solve the issue? Yes: placing serialization and the post-acquisition reread at the shared lease/checkpoint owner fixes every caller without duplicating retry policy in the Mac app or gateway spawner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against a3d2ac7dc887. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
…iling gateway startup (openclaw#120959)
…iling gateway startup (openclaw#120959) (cherry picked from commit 7dc4dc8)
Rebase-onto-main fixups (main moved ~2544 commits since this branch was last synced, largely via openclaw#120959's startup-migration-checkpoint rework): - withTempDir -> withTestDir (helper renamed in src/test-helpers/temp-dir.ts). - makeTempRoot -> makeDoctorStateDir (the actual tracked-tempdir helper this test file uses elsewhere; makeTempRoot never existed here). - Mock ../config/io.js's readConfigFileSnapshotWithPluginMetadata and ../infra/startup-migration-checkpoint.js's needsStateMigrationCheckpoint / recordSuccessfulStateMigrations / acquireStartupMigrationLeaseWithWait, all added/renamed by openclaw#120959's checkpoint-identity rework. - needsStateMigrationCheckpoint now mirrors needsStartupMigrationCheckpoint by default (openclaw#120959 decoupled the two), and the plugin migration fingerprint is pinned non-null so the pre/post-convergence identity check doesn't spuriously refuse readiness in this suite. ClawSweeper P3 fix (confidence 0.98): the two "no-copy" fixtures in doctor-config-preflight.legacy-config.test.ts never created a bare .openclaw directory, so config/paths.ts's resolveStateDir fell through to the legacy dir as the resolved state dir instead of ~/.openclaw. That made targetsDefaultStateRoot false, so the canonical-root guard this PR added was never actually exercised — the assertions passed vacuously because the copy target itself became ~/.clawdbot. Creating a bare ~/.openclaw in both fixtures makes it the resolved target and forces the guard to run.
What Problem This Solves
Fixes an issue where first-run macOS onboarding could dead-end with “The Gateway didn’t start” when the Mac app launched the gateway and mac-node worker nearly simultaneously. Both processes ran startup migrations against the fresh state directory, and the process that lost the migration lease exited immediately instead of waiting for its sibling to finish.
Observed repeatedly on a fresh state directory:
Why This Change Was Made
The startup-migration lease owner now serializes expected same-host contenders with a bounded, asynchronous 250 ms poll. The wait is capped by the existing five-minute lease TTL, preserves dead-owner and PID-reuse reclamation, and retains the existing contention error on timeout. Cross-host or unprovable owners still fail immediately.
After every successful acquisition, doctor preflight refreshes the authoritative config, plugin-index, and checkpoint facts under the lease. This closes both contention races: if the prior owner completed before or during the wait—or just before the first acquisition attempt—the next process skips migrations already recorded by that owner.
No retry was added to the Mac app or gateway spawner, and no config or environment surface was added.
User Impact
Fresh macOS onboarding can start the gateway and mac-node worker concurrently without the lease loser crashing the CLI. Normal contention resolves within seconds when the first process finishes; genuinely stuck live owners still produce the existing actionable error after the bounded wait.
Evidence
node scripts/run-vitest.mjs src/infra/startup-migration-checkpoint.test.tsTest Files 1 passed (1)Tests 16 passed (16)node scripts/run-vitest.mjs src/commands/doctor-config-preflight.state-migration.test.tsTest Files 1 passed (1)Tests 30 passed (30)Test Files 1 passed (1)/Tests 69 passed (69)Test Files 2 passed (2)/Tests 20 passed (20)node scripts/check-changed.mjs -- <changed files>on Blacksmith Testbox0; production and test typechecks, lint, formatting, import cycles, SQLite/database guards, and changed-file guards passed.Production LOC: +155/-47 (net +108). Tests: +107/-37 (net +70). The production growth establishes the bounded async serialization owner boundary and authoritative post-acquisition refresh; the synchronous acquisition API remains available for existing synchronous callers.