Skip to content

fix(gateway): failed visible tasks leave a stranded child session for write-only operators - #160046

Open
DonnieFi wants to merge 8 commits into
openclaw:mainfrom
DonnieFi:fix/159862-visible-spawn-rollback
Open

DonnieFi wants to merge 8 commits into
openclaw:mainfrom
DonnieFi:fix/159862-visible-spawn-rollback

Conversation

@DonnieFi

@DonnieFi DonnieFi commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Related: #159862
Stacked on #159940. Review only commit e77c394fd72; the earlier commits belong to #159940 and drop out once it lands, and the later ones are a main merge and an empty CI retrigger.

What Problem This Solves

Fixes: when a visible sessions_spawn task fails to start for an operator with operator.write but no operator.admin, the failed child session is left behind and the model is told Session cleanup unconfirmed.

User Impact

User impact: a failed visible task leaves no child behind for any caller that could create it, and the model gets the start error directly (for example send blocked by session policy). Callers with operator.admin see the same outcome as before, reported as a tool error instead of a structured error result.

Why This Change Was Made

The tool's cleanup runs sessions.delete, which needs operator.admin. In-process tool calls are intersected with the caller's operator authority, so a write-only caller could create the child but never remove it.

Now sessions.create owns rollback of the child it created. When a spawn-owned visible child (via: "spawn" with a completion owner) has an initial task that never started, the same request deletes it through the shared deleteGatewaySession owner and returns the start error. That delete:

  • runs under the create request's own session authority, revalidated just before the delete, so no scope is widened and a caller whose authority closed keeps the child for recovery;
  • is fenced to the exact session id and lifecycle revision this request committed, so a reset or replacement in between keeps the newer child;
  • keeps emitLifecycleHooks: false, matching the old tool cleanup, so hook behavior does not change.

If the rollback declines, sessions.create returns the old successful result with runStarted: false, and the tool's existing cleanup and message apply. Other sessions.create callers, including worker spawns without a completion owner, Control UI placement, and deferred worktree setup, keep the documented "child survives a rejected first send" contract. The protocol doc now describes the spawn exception.

Evidence

Real Gateway, write-only client. The new src/gateway/server.visible-spawn-start-failure.product-proof.test.ts starts a real Gateway server and a real WebSocket client declaring operator.approvals, operator.questions, operator.read, operator.write, with a fake OpenAI Responses provider whose parent turn calls sessions_spawn { task, visible: true }. A session.sendPolicy deny rule on the dashboard prefix makes the child's first send fail. The client sends chat.send, waits with agent.wait, then reads sessions.list. Redacted output:

Before (this change reverted, #159940 head):

{
  "spawnToolOutput": "{ \"status\": \"error\", \"error\": \"send blocked by session policy. Session cleanup unconfirmed. Inspect the child session before retrying.\", \"childSessionKey\": \"agent:main:dashboard:<uuid>\" }",
  "dashboardChildren": ["agent:main:dashboard:<uuid>"]
}

After:

{
  "spawnToolOutput": "{ \"status\": \"error\", \"tool\": \"sessions_spawn\", \"error\": \"send blocked by session policy\" }",
  "dashboardChildren": []
}

Real handler chain. src/gateway/server.sessions.visible-spawn-failure.test.ts runs the registered tool through in-process dispatch as an admin Control UI client and as a paired write-only operator profile (the incident's shape). Both now report the start error and leave only the parent session. A third case resets a child and then asks the rollback to delete the original incarnation: the session id is unchanged, the lifecycle revision differs, and the child is kept. These tests fail without the sessions-create.ts change, and the fence case fails if the lifecycle-revision fence is dropped.

Checks. The visible-spawn, product-proof, child-custody, worktree-spawn, and visible-spawn-fallback Gateway suites pass (80 tests), as does sessions-spawn-tool.test.ts. pnpm tsgo:core and pnpm tsgo:core:test pass, and oxlint and oxfmt are clean. An independent review found no blocking issues. Its should-fix findings (protocol doc, and revalidating only session authority rather than creation-time selections) are addressed.

Test cost. Measured locally with node scripts/run-vitest.mjs run <file> --maxWorkers=1, and from the test times in CI run 36420025695, where all three files passed.

Test file Tests Local one-worker wall CI test time
src/agents/tools/sessions-spawn-tool.test.ts (changed in #159940, unchanged here) 123 33.7s 21.3s (shard agentic-agents-tools)
src/gateway/server.sessions.visible-spawn-failure.test.ts (two cases added) 3 32.8s 23.1s (shard agentic-control-plane-agent-chat)
src/gateway/server.visible-spawn-start-failure.product-proof.test.ts (new) 1 30.6s 12.0s (shard agentic-control-plane-runtime-state-hosted-2)

CI note. The first run failed check-test-types on test/scripts/ci-static-check-evidence.test.ts, which main broke at 02:27 UTC and fixed at 03:08 UTC. After merging main, run 36420025695 had five timeouts in areas this diff does not reach: a Control UI sidebar e2e test, a Telegram history test, a chat.send history test, Gateway node shutdown, and a subagent requester-wake e2e test that mocks the Gateway. main was green at the same base, and GitHub refuses job reruns here, so an empty commit retriggers CI.

Not covered. Only the send-policy rejection is exercised; other pre-acknowledgement send failures are expected to leave the lifecycle revision unchanged and follow the same path. Live Android proof was not run.

Visible spawn cleanup used an admin-only sessions.delete, so a caller with operator.write could create the child but never remove it after a failed start. Use the operator.write archive-then-delete contract instead.
Keep this change to reporting the child start error and logging cleanup failures. The write-only rollback moves to a Gateway-owned follow-up.
@clawsweeper

clawsweeper Bot commented Sep 28, 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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: L labels Sep 28, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. labels Sep 28, 2026
@clawsweeper

clawsweeper Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 28, 2026, 11:17 AM ET / 15:17 UTC (Revision 4).

ClawSweeper review

What this changes

The branch reports specific visible-task start errors and makes the Gateway remove a newly created child session when its first task is rejected, with protocol documentation and regression tests.

Merge readiness

✅ Ready for maintainer review

This remains a useful, distinct fix. Current main still leaves failed visible-task cleanup to an admin-scoped delete, while the branch has convincing Gateway-level evidence that a write-only operator receives the start error and does not retain the failed child. The prior review’s measured test-cost request is addressed.

Priority: P1
Reviewed head: 172c42ff4e828bfe01c8182710e25a190ec0d6f6

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) The patch is focused at the lifecycle owner and has strong production-path proof, with the measured test cost now documented.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The changed Gateway creation and deletion path is exercised through a real Gateway and WebSocket client with write-only scopes, an injected first-send policy rejection, and observed after-fix error output with no child in sessions.list. A synthetic local provider supplies the tool call; no stored-data schema contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The changed Gateway creation and deletion path is exercised through a real Gateway and WebSocket client with write-only scopes, an injected first-send policy rejection, and observed after-fix error output with no child in sessions.list. A synthetic local provider supplies the tool call; no stored-data schema contract changes.
Evidence reviewed 6 items Current main still has the reported boundary: At the fetched main revision, sessions.create returns a successful result with runStarted and runError after a rejected first send; it has no Gateway rollback branch.
Gateway-owned rollback: The introduced branch limits rollback to spawn-owned children with a completion owner, a rejected initial turn, and a recorded committed incarnation.
Authority and lifecycle fence: Rollback passes the create request’s current-authority guard and committed session identity to the shared deletion owner, which rechecks identity before mutation.
Findings None None.
Security None None.

How this fits together

The agent’s visible-task tool asks the Gateway to create a child session and start its first turn. The Gateway records the child, sends the task, and returns either a started run or a failure that the tool reports to the agent.

flowchart LR
  A[Visible task request] --> B[Gateway session creation]
  B --> C[Committed child]
  C --> D[First task send]
  D --> E{Task started?}
  E -->|Yes| F[Active child run]
  E -->|No| G[Guarded child cleanup]
  G --> H[Start error or retained child]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +108, Tests +402, Docs 0. Total +510 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 5 133 25 +108
Tests 4 509 107 +402
Docs 1 1 1 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 643 133 +510

Review metrics

Metric Value Why it matters
Production and test growth production +108 net lines; tests and test support +402 net lines The production growth implements Gateway-owned rollback; the larger test growth covers role, lifecycle, and WebSocket behavior.
Gateway test cost 2 new files: 32.8s and 30.6s local wall time The PR body also records CI test times of 23.1s and 12.0s for these recurring checks.

Technical review

Best possible solution:

Keep rollback in the Gateway’s session lifecycle owner, fenced to the child created by that request, while tracking the broader Android Talk approval and task-continuity symptoms separately.

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

Yes, with high confidence from the source path and the contributor’s before-and-after Gateway trace: a write-only client triggers a visible spawn whose first send is denied by session policy. I did not execute the checkout.

Is this the best way to solve the issue?

Yes. The creating Gateway request is the narrow owner that can remove only its own unstarted child while preserving the existing result when cleanup declines.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 1a94ee9ed7e2.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: A failed visible agent task can strand a child session for a real write-only operator workflow.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The changed Gateway creation and deletion path is exercised through a real Gateway and WebSocket client with write-only scopes, an injected first-send policy rejection, and observed after-fix error output with no child in sessions.list. A synthetic local provider supplies the tool call; no stored-data schema contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The changed Gateway creation and deletion path is exercised through a real Gateway and WebSocket client with write-only scopes, an injected first-send policy rejection, and observed after-fix error output with no child in sessions.list. A synthetic local provider supplies the tool call; no stored-data schema contract changes.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • RoboClaw: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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.

History

Review history (3 earlier review cycles)
  • reviewed 2026-09-28T02:46:48.234Z sha e77c394 :: blocked before merge. :: none
  • reviewed 2026-09-28T12:17:18.989Z sha 0b8ea4a :: needs changes before merge. :: none
  • reviewed 2026-09-28T13:23:58.205Z sha 3cd8f9f :: blocked before merge. :: none

@DonnieFi

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Added measured test cost per changed test file and a CI note: the remaining failures are timeouts in unrelated suites. The PR body has the evidence (shard order, code path, local reruns).

@clawsweeper

clawsweeper Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@VACInc

VACInc commented Sep 28, 2026

Copy link
Copy Markdown
Member

Related update: #160296 is merged on main with eligible write-operator archive/restore/Stop controls, while retaining deletion restrictions. It does not perform generation-fenced rollback when a visible sessions_spawn initial task fails to start. This PR's spawn-cleanup scope is not superseded and should remain open.

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Oct 13, 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

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L stale Marked as stale due to inactivity 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.

2 participants