Skip to content

fix(config): serialize concurrent config mutations - #76601

Merged
steipete merged 14 commits into
openclaw:mainfrom
dlebee-agent:fix/agents-add-concurrent-config-race
May 13, 2026
Merged

steipete merged 14 commits into
openclaw:mainfrom
dlebee-agent:fix/agents-add-concurrent-config-race

Conversation

@dlebee-agent

@dlebee-agent dlebee-agent commented May 3, 2026 •

Copy link
Copy Markdown

Summary

Centralizes config-file mutation behind reusable transform/write helpers so concurrent writers re-read, rebase, and commit against the latest persisted config instead of clobbering each other or surfacing avoidable ConfigMutationConflictErrors.

This is intentionally generic: agents add is one caller of the shared config mutation API, and gateway/runtime/plugin writers now use the same semantic mutation path where they previously rewrote a prepared full config object.

Related: #43367

Problem

Config updates were spread across read/modify/write call sites. A caller could read a snapshot, prepare a new config, then lose the race when another command or gateway request wrote first. Cross-process locking alone was not enough because same-process callers could overlap, and command-specific retry loops would leave the same stale-snapshot bug available to the next config writer.

The visible case was concurrent openclaw agents add --non-interactive calls: one process could crash on a conflict or overwrite from a stale snapshot, leaving only part of the requested agent set in the config.

What Changed

  • Added central config mutation helpers in src/config/mutate.ts:
    • transformConfigFile / mutateConfigFile
    • retrying variants for conflict-safe semantic updates
    • same-process per-config-path serialization layered with the existing file lock for cross-process writers
    • nested mutation handling so internal commit paths do not deadlock while independent callers still serialize
  • Preserved the optimistic baseHash contract for stale prepared snapshots and made prepared writes carry their original baseSnapshot through the config writer.
  • Moved pending plugin-install config writes onto the same generic mutation pipeline, including rollback behavior for install-record persistence.
  • Reworked agents add to express the desired addition as a config transform, so retries reapply intent to the latest config snapshot.
  • Broadened the same model to gateway and runtime writers that mutate config from long-lived processes:
    • gateway agents.create, agents.update, agents.delete, and skills.config.set
    • chat /config, /plugins enable|disable, and /allowlist config edits
    • browser control auth/profile writes
    • bundled plugin toggles and profile/migration writes in memory, active-memory, phone-control, talk-voice, Nostr, Slack, and Telegram
  • Mapped rebased gateway agent races back to protocol errors instead of uncaught transport failures: duplicate create returns invalid request; update/delete after concurrent removal returns agent-not-found.
  • Fixed browser profile rebases so create allocates CDP ports from the latest draft profile set, and delete clears a concurrently changed default profile that points at the removed profile.
  • Updated browser and Telegram mocks/tests to use mutateConfigFile where production code now does.
  • Kept full-replacement writes only where they are process-boundary setup/doctor/import flows or already tied to a prepared snapshot.
  • Cleaned current-base dependency/lint blockers: removed an unused WhatsApp direct dependency, typed the plugin inventory generator map, and deleted a stale provider retry re-export barrel.
  • Added/updated regression coverage for retry, same-process serialization, plugin-install commit integration, gateway agent mutation behavior, install-token snapshot writes, and concurrent agents add behavior.
  • Added a changelog entry for the user-visible concurrent config write fix.

Why This Shape

The important invariant is now owned by the config layer: when code reads config in order to write config, it should do that through one mutation path that owns locking, base-hash checks, retry semantics, include-file writes, runtime snapshot refresh, and after-write follow-up behavior.

That avoids duplicating conflict loops at each command boundary and keeps future config writers from accidentally reintroducing stale-snapshot writes.

Real behavior proof

  • Behavior or issue addressed: Concurrent openclaw agents add --non-interactive calls should preserve all requested agents when they mutate the same config file at the same time.
  • Real environment tested: Local OpenClaw checkout at PR head f5ab8e8d78, macOS, Node via pnpm openclaw, isolated temp OPENCLAW_HOME, OPENCLAW_STATE_DIR, and OPENCLAW_CONFIG_PATH.
  • Exact steps or command run after this patch: Ran three pnpm openclaw agents add commands concurrently against /tmp/openclaw-pr76601-proof.lG9g45/openclaw.json, waited for all three, then ran pnpm openclaw agents list and inspected the final config.
  • Evidence after fix (screenshot, recording, terminal capture, console output, redacted runtime log, linked artifact, or copied live output): Copied live terminal output from the latest PR head:
$ export OPENCLAW_CONFIG_PATH=/tmp/openclaw-pr76601-proof.lG9g45/openclaw.json
$ pnpm openclaw agents add race-one --non-interactive --workspace /tmp/openclaw-pr76601-proof.lG9g45/workspace-one --model anthropic/claude-sonnet-4-20250514 &
$ pnpm openclaw agents add race-two --non-interactive --workspace /tmp/openclaw-pr76601-proof.lG9g45/workspace-two --model anthropic/claude-sonnet-4-20250514 &
$ pnpm openclaw agents add race-three --non-interactive --workspace /tmp/openclaw-pr76601-proof.lG9g45/workspace-three --model anthropic/claude-sonnet-4-20250514 &
$ wait
$ pnpm openclaw agents list
Agents:
- main (default)
- race-two
  Workspace: /tmp/openclaw-pr76601-proof.lG9g45/workspace-two
  Model: anthropic/claude-sonnet-4-20250514
- race-one
  Workspace: /tmp/openclaw-pr76601-proof.lG9g45/workspace-one
  Model: anthropic/claude-sonnet-4-20250514
- race-three
  Workspace: /tmp/openclaw-pr76601-proof.lG9g45/workspace-three
  Model: anthropic/claude-sonnet-4-20250514
  • Observed result after fix: Final config contained main, race-one, race-two, and race-three. No ConfigMutationConflictError escaped to the user, and no agent entry was lost.
  • What was not tested: Interactive wizard TTY conflict display and stress beyond three concurrent CLI processes.

Verification

Rebased on current origin/main and pushed latest head 494f7531eb.

Commands run on latest head:

pnpm test extensions/browser/src/browser/profiles-service.test.ts
pnpm test extensions/browser/src/browser/control-auth.auto-token.test.ts extensions/browser/src/browser/profiles-service.test.ts extensions/browser/src/browser/server.agent-contract-core.test.ts extensions/browser/src/browser/server.agent-contract-form-layout-act-commands.test.ts src/gateway/server-methods/agents-mutate.test.ts
pnpm test extensions/telegram/src/bot.create-telegram-bot.test.ts -- -t "retries group migration"
pnpm test src/plugins/contracts/extension-runtime-dependencies.contract.test.ts
pnpm lint --threads=8
pnpm check:test-types
pnpm tsgo:prod
pnpm deps:pins:check
pnpm deps:root-ownership:check
pnpm deadcode:dependencies
pnpm deadcode:unused-files
pnpm deadcode:report:ci:ts-unused
git diff --check

Results:

  • Browser profile service focused tests: 14 passed.
  • Gateway/browser focused tests: 122 passed across 5 files.
  • Telegram migration-focused test: 1 passed, 76 skipped.
  • Plugin runtime dependency contract: 364 passed.
  • Lint, production typecheck, test typecheck, dependency pin/root ownership checks, deadcode dependency checks, unused-file check, ts-unused report, and diff whitespace check: passed.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: S labels May 3, 2026
@clawsweeper

clawsweeper Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The branch adds config-layer serialized transform/retry helpers, forwards write metadata, rewires non-interactive agents add and pending plugin-install commits onto that path, and adds regression coverage plus a changelog entry.

Reproducibility: yes. Current main prepares non-interactive agents add writes from an initial config snapshot and commits with the initial hash, which is enough to source-reproduce the stale writer/conflict path; I did not run the live race in this read-only review.

Real behavior proof
Sufficient (terminal): The PR body includes after-fix terminal output from a real PR-head run showing concurrent agents add commands and the final persisted agent set with all requested agents present.

Next step before merge
No repair lane is needed because I did not find a discrete automated fix; the PR is recent, has proof, and is ready for normal maintainer review or landing gates.

Security
Cleared: The diff changes config mutation/CLI code, tests, and changelog only, with no new dependencies, workflows, downloaded artifacts, secret handling, or permission broadening.

Review details

Best possible solution:

Land this centralized config mutation fix, then keep any remaining multi-agent orchestration failures tracked in the linked broader issues as separate work.

Do we have a high-confidence way to reproduce the issue?

Yes. Current main prepares non-interactive agents add writes from an initial config snapshot and commits with the initial hash, which is enough to source-reproduce the stale writer/conflict path; I did not run the live race in this read-only review.

Is this the best way to solve the issue?

Yes. Moving read/transform/write into a central config-layer lock and retry helper is the narrow maintainable fix for this race, and it avoids a command-local retry loop while preserving broader follow-up scope separately.

What I checked:

  • current-main race source: Current main computes the non-interactive agent addition from the initially loaded config snapshot before the commit, so overlapping writers can prepare stale nextConfig values. (src/commands/agents.commands.add.ts:154, 1ae9d8227ae0)
  • current-main optimistic commit: Current main commits the prepared agent config with the initial baseHash, which can surface ConfigMutationConflictError instead of rebasing the semantic add on the latest file contents. (src/commands/agents.commands.add.ts:177, 1ae9d8227ae0)
  • write wrapper metadata gap: On current main, the exported writeConfigFile wrapper does not pass baseSnapshot or afterWrite to the lower-level writer; the PR explicitly threads both through. (src/config/io.ts:2430, 1ae9d8227ae0)
  • proposed serialized mutation path: The PR head adds config mutation queueing, re-entrant same-process tracking, cross-process file locking, and retrying transform helpers in the config layer. (src/config/mutate.ts:139, 48467ee6fc41)
  • proposed agents add rebase: The PR head moves non-interactive agents add into transformConfigWithPendingPluginInstalls, rechecking duplicate agents, resolving agent dirs, and parsing bindings against the latest config inside the mutation transform. (src/commands/agents.commands.add.ts:178, 48467ee6fc41)
  • regression coverage: The PR adds focused tests for retrying stale transform conflicts, same-process serialization before snapshot reads, and non-interactive agents add rebasing on a later config snapshot. (src/config/mutate.test.ts:130, 48467ee6fc41)

Likely related people:

  • steipete: PR head commits are authored by steipete, and recent GitHub path history shows repeated work on config mutation/io and agents add surfaces touched by this PR. (role: recent area contributor; confidence: high; commits: d5f204dd7d35, 48467ee6fc41, 538605ff44d2; files: src/config/mutate.ts, src/config/io.ts, src/commands/agents.commands.add.ts)
  • shakkernerd: Recent merged history introduced and refined pending plugin install record commits and rollback behavior, which this PR routes through the new transform commit path. (role: adjacent owner; confidence: medium; commits: 921ffad7c7a6, 29463b9c47c7, e7c131d6de92; files: src/cli/plugins-install-record-commit.ts, src/config/mutate.ts, src/commands/agents.commands.add.ts)
  • joshp123: Recent config write-guard work touched the same mutation/write path and tests that this PR extends with serialized mutation behavior. (role: recent config contributor; confidence: medium; commits: d4b46600269c; files: src/config/mutate.ts, src/config/io.ts, src/config/mutate.test.ts)

Remaining risk / open question:

  • This PR fixes the config mutation race; the broader linked multi-agent orchestration issue still has separate session-lock, auth-refresh, and detached-work symptoms outside this patch.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 1ae9d8227ae0.

@dlebee-agent
dlebee-agent force-pushed the fix/agents-add-concurrent-config-race branch from f086923 to 842c8c2 Compare May 6, 2026 04:18
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 6, 2026
@dlebee-agent
dlebee-agent force-pushed the fix/agents-add-concurrent-config-race branch from 4385ae9 to 8a60c43 Compare May 6, 2026 04:50
@dlebee

dlebee commented May 6, 2026

Copy link
Copy Markdown

bump ^?

@steipete
steipete force-pushed the fix/agents-add-concurrent-config-race branch 2 times, most recently from a1442fd to 1cd93cf Compare May 13, 2026 05:45
@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: L and removed size: M labels May 13, 2026
@steipete
steipete force-pushed the fix/agents-add-concurrent-config-race branch from 1cd93cf to d5f204d Compare May 13, 2026 06:42
@steipete steipete changed the title fix(agents): add retry logic for concurrent config modifications fix(config): serialize concurrent config mutations May 13, 2026
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. proof: supplied External PR includes structured after-fix real behavior proof. and removed proof: supplied External PR includes structured after-fix real behavior proof. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 13, 2026
@openclaw-barnacle openclaw-barnacle Bot added channel: nostr Channel integration: nostr channel: slack Channel integration: slack channel: telegram Channel integration: telegram gateway Gateway runtime labels May 13, 2026
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. proof: supplied External PR includes structured after-fix real behavior proof. and removed proof: supplied External PR includes structured after-fix real behavior proof. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@steipete
steipete force-pushed the fix/agents-add-concurrent-config-race branch from 0bfdf24 to 2404582 Compare May 13, 2026 13:48
@steipete

Copy link
Copy Markdown
Contributor

Landing proof for PR #76601.

  • Final head: 04cc540
  • Local/Testbox gate: pnpm check:changed on Testbox tbx_01krgsb5j3tvftvqp8yanht5kb, exit 0
  • Focused local proof run while addressing review feedback:
    • pnpm test extensions/browser/src/browser/profiles-service.test.ts src/auto-reply/reply/commands-gating.test.ts
    • pnpm check:test-types
    • pnpm lint
    • pnpm deadcode:unused-files
  • GitHub checks on final head: Real behavior proof, Critical Quality, Security High, lint/type/build/test shards all passed or were intentionally skipped by selectors.
  • Known proof gaps: none.

@steipete
steipete merged commit 2181564 into openclaw:main May 13, 2026
111 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed PR #76601 via rebase merge.

  • Merge commit: 2181564
  • Final PR head before merge: 04cc540
  • Proof: pnpm check:changed on Testbox tbx_01krgsb5j3tvftvqp8yanht5kb, exit 0; GitHub CI green on final head.
  • Known proof gaps: none.

@dlebee-agent

Copy link
Copy Markdown
Author

thanks for merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: nostr Channel integration: nostr channel: slack Channel integration: slack channel: telegram Channel integration: telegram channel: whatsapp-web Channel integration: whatsapp-web cli CLI command changes commands Command implementations dependencies-changed PR changes dependency-related files extensions: memory-core Extension: memory-core extensions: phone-control extensions: talk-voice gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. scripts Repository scripts size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants