Skip to content

fix(cli): guard embedded agent state ownership for --local runs - #120896

Merged
steipete merged 1 commit into
mainfrom
steipete/agent-local-gateway-guard
Aug 9, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/agent-local-gateway-guard

Conversation

@steipete

@steipete steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

openclaw agent --local ran an embedded turn against the same agent SQLite state a live Gateway owns, with no state-directory coordination — the only guard was the session-write lease, which is scheduled for removal. Concurrent local+gateway writers could interleave on one agent DB.

Why This Change Was Made

This closes the cross-process hole at the correct ownership layer (the Gateway state-directory lock) before the session-write lease is deleted. It adds a new agent-embedded lock role: --local refuses to run when a live Gateway owns the state directory, with an actionable pid/port message; otherwise it holds exclusive state ownership for the run and releases it on completion or SIGINT/SIGTERM unwind. Gateway-identity probes ignore the new role, and Gateway startup errors name the actual embedded holder instead of claiming a Gateway is running.

User Impact

--local against a running Gateway now fails fast with remediation instead of silently double-writing. There is no change when no Gateway is running.

Evidence

  • Gateway-lock suites: 40 passed.
  • Agent/Gateway command suites: 100 passed, 1 skipped.
  • Broader agent suite: 43 passed.
  • Full pnpm build: passed.
  • Codex autoreview: clean (no P0).
  • Source-blind built-CLI check: exit 1, stdout 0 bytes, actionable refusal on stderr.

Named follow-up surfaces (out of scope, coordinator-tracked):

  • agent exec --state-dir (src/commands/agent-exec.ts:562)
  • TUI embedded backend (src/tui/embedded-backend.ts:1428)
  • models status --probe (src/commands/models/list.probe.ts:769)
  • attempt-execution session-lease path (src/agents/command/attempt-execution.ts:523)

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation cli CLI command changes commands Command implementations size: M maintainer Maintainer-authored PR labels Aug 9, 2026
@steipete
steipete marked this pull request as ready for review August 9, 2026 04:52
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Aug 9, 2026
@clawsweeper

clawsweeper Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 9, 2026, 2:42 AM ET / 06:42 UTC.

ClawSweeper review

What this changes

This PR makes openclaw agent --local exclusively own its state directory for the embedded turn, refusing to run beside a live Gateway or another local run.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep this focused maintainer-authored repair open for normal review; current main still permits the concurrent state-owner path, and the branch adds the shared lock at the correct boundary without a source-proven regression.

Priority: P1
Reviewed head: a6b027269744dafcdac49143d6a674b9f94e0e9b

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with coherent ownership placement and targeted regression coverage, subject to normal maintainer acceptance of the intentional fail-fast change.
Proof confidence 🌊 off-meta tidepool Not applicable: This is a maintainer-authored PR, so the external-contributor real-behavior-proof gate does not apply; the PR body also records built-CLI and test evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a maintainer-authored PR, so the external-contributor real-behavior-proof gate does not apply; the PR body also records built-CLI and test evidence.
Evidence reviewed 5 items Current-main gap: Current main routes --local directly into embedded execution and does not acquire the Gateway state-directory lock first.
Owner-boundary repair: The branch checks active Gateway ownership, acquires the shared lock with the agent-embedded role, rechecks a Gateway race, and releases the lock around the complete embedded run.
Lock-role contract: The new role is verified as a real openclaw agent --local process and is deliberately excluded from Gateway identity probes, preserving Gateway-specific lifecycle behavior.
Findings None None.
Security None None.

How this fits together

The agent CLI can either send a turn to the Gateway or run an embedded agent locally. Both paths use agent state under the configured state directory, so the shared Gateway lock determines whether a local turn may safely start.

flowchart TD
A[Operator starts local agent] --> B[State directory lock]
B --> C{Live Gateway owns state?}
C -->|Yes| D[Actionable refusal]
C -->|No| E[Embedded agent turn]
E --> F[Release state lock]
Loading

Before merge

  • Resolve merge risk (P1) - Existing workflows that intentionally run --local beside a live Gateway will now fail fast and require using the Gateway-backed command or stopping the Gateway.
Agent review details

Security

None.

PR surface

Source +66, Tests +187, Docs +1. Total +254 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 2 84 18 +66
Tests 3 192 5 +187
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 277 23 +254

Review metrics

Metric Value Why it matters
Production versus tests production +84/-18; docs +1; tests +192/-5 The production growth implements one shared ownership boundary, while the tests cover contention, cleanup, and supervisor behavior.

Stored data model

Persistent data-model change detected: serialized state: src/infra/gateway-lock.roles.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Keep the fail-fast ownership boundary (recommended)
    Accept the intentional compatibility change so concurrent local and Gateway writers cannot continue silently.

Technical review

Best possible solution:

Retain the exclusive state-directory ownership boundary and its documented Gateway-backed alternative before merging.

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

Yes—current main's --local branch enters embedded execution without the shared state lock, so starting it beside a Gateway that owns the same state directory is source-reproducible.

Is this the best way to solve the issue?

Yes—the shared Gateway state-directory lock is the owner boundary for this conflict; the branch also distinguishes embedded owners from actual Gateways and handles the acquisition race.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against a3d2ac7dc887.

Labels

Label changes:

  • add P1: The PR prevents concurrent agent and Gateway processes from interleaving writes to the same agent state.
  • add merge-risk: 🚨 compatibility: Merging intentionally changes an existing concurrent --local workflow into an actionable refusal.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored PR, so the external-contributor real-behavior-proof gate does not apply; the PR body also records built-CLI and test evidence.

Label justifications:

  • P1: The PR prevents concurrent agent and Gateway processes from interleaving writes to the same agent state.
  • merge-risk: 🚨 compatibility: Merging intentionally changes an existing concurrent --local workflow into an actionable refusal.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored PR, so the external-contributor real-behavior-proof gate does not apply; the PR body also records built-CLI and test evidence.

Evidence

What I checked:

  • Current-main gap: Current main routes --local directly into embedded execution and does not acquire the Gateway state-directory lock first. (src/commands/agent-via-gateway.ts:991, a3d2ac7dc887)
  • Owner-boundary repair: The branch checks active Gateway ownership, acquires the shared lock with the agent-embedded role, rechecks a Gateway race, and releases the lock around the complete embedded run. (src/commands/agent-via-gateway.ts:221, a6b027269744)
  • Lock-role contract: The new role is verified as a real openclaw agent --local process and is deliberately excluded from Gateway identity probes, preserving Gateway-specific lifecycle behavior. (src/infra/gateway-lock.ts:203, a6b027269744)
  • Regression coverage: Focused tests cover live-Gateway refusal before agent startup, concurrent local-run exclusion, signal cleanup, role distinction, and avoiding supervised Gateway retries for an embedded holder. (src/commands/agent-via-gateway.test.ts:485, a6b027269744)
  • History and ownership: The current lock identity and local-agent dispatch paths blame to the recent shared state-lock work, with Peter Steinberger the dominant contributor in both histories. (src/infra/gateway-lock.ts:319, c2e7c819f570)

Likely related people:

  • steipete: Authored the proposed repair and the current-main lock and agent-dispatch paths trace to recent work by this contributor. (role: recent area contributor; confidence: high; commits: a6b027269744, c2e7c819f570; files: src/infra/gateway-lock.ts, src/commands/agent-via-gateway.ts)
  • Vincent Koc: Relevant history includes prior SQLite maintenance serialization using the same Gateway-lock owner boundary. (role: lock serialization contributor; confidence: medium; commits: e474ba38e805; files: src/infra/gateway-lock.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

ClawSweeper disposition:

  • The review contains no Rank-up moves: list and no actionable code findings.
  • Its sole before-merge item is the intentional compatibility change: a concurrent openclaw agent --local run now fails fast while a live Gateway owns the same state directory.
  • This risk is accepted as the purpose of the repair. Continuing the concurrent workflow would preserve the unsafe cross-process writer hole; the supported remediation is to use the Gateway-backed path or stop the Gateway. The coordinator reviewed the branch and explicitly directed landing, so no code change is needed for this item.

@steipete
steipete merged commit 8ede404 into main Aug 9, 2026
192 checks passed
@steipete
steipete deleted the steipete/agent-local-gateway-guard branch August 9, 2026 06:59
@steipete

steipete commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

cli CLI command changes commands Command implementations docs Improvements or additions to documentation maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant