Skip to content

fix(cli): exit after local OpenAI agent replies - #170391

Merged
obviyus merged 1 commit into
mainfrom
fix/agent-local-exit
Oct 12, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/agent-local-exit

Conversation

@obviyus

@obviyus obviyus commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes openclaw agent --local --json printing a completed OpenAI reply but keeping the process alive on its cached WebSocket connection.

Found during live-provider verification of #167736.

User Impact

Local one-shot agent commands release cached provider connections after the reply and exit normally. JSON/text output and exit status are unchanged; the running Gateway retains its own session resources.

Why This Change Was Made

The native OpenAI Responses transport registers its cached WebSocket with the existing AI session-resource cleanup registry. Gateway shutdown already drains that registry, but CLI cleanup omitted it. Call the same cleanup during one-shot CLI teardown, after agent harness disposal, rather than forcing process exit or disabling connection reuse between turns.

No overlap with Pash/Sarah changes in the touched files.

Evidence

Node 24.21.0, isolated state, real openai-responses / gpt-5-mini, minimal tools, bootstrap skipped:

  • Main 6315f8adc18: agent --local --agent main --session-id … --message … --thinking minimal --timeout 90 --json printed LIVE_OK, then was still alive at the 120-second harness deadline. The same happened with diagnostics enabled. Active-handle tracing identified TLSSocket → ws/lib/websocket.js; the cached connection has a five-minute idle TTL. A loopback SSE provider exited normally, isolating the native WebSocket transport.
  • Candidate 95ec4e8210bb: real-provider JSON reply LIVE_OK at 10,275 ms; exit 0 at 10,554 ms (279 ms after output).
  • Identical-command confirmation on the candidate (pnpm openclaw, --timeout 90, same provider/config/flags as the baseline): LIVE_OK, natural exit 0 in 14,301 ms; no harness kill.
  • Text mode: LIVE_OK, exit 0, 272 ms after reply. Plugins enabled: LIVE_OK, exit 0, 244 ms after reply.
  • Provider failure control (loopback HTTP 401): exit 1 at 9,632 ms, without a harness kill.
  • Regression: a registered provider resource survives CLI teardown before the fix (test fails); teardown releases it after the fix. Focused cleanup/dispatch tests: 22 passed, command wall time 28.79 s including one-time worker preparation. Changed TypeScript formatting and lint passed.
  • Gateway control (no --local): real-provider LIVE_OK, exit 0, 33 ms after output; Gateway shutdown also exited 0 in 328 ms.

Two-way door: reverting restores only the previous CLI cleanup omission; no config or persisted-data changes. Blast radius: one-shot CLI provider-resource teardown. Gateway-owned connections are not retired by another CLI process.

@clawsweeper

clawsweeper Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@obviyus

obviyus commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation cli CLI command changes size: XS labels Oct 12, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 12, 2026
@clawsweeper

clawsweeper Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR releases cached provider connections during CLI teardown so local agent commands exit after replying.

Example: Run openclaw agent --local --json using openai-responses with gpt-5-mini.

  • Before: The command prints LIVE_OK but remains alive on its cached WebSocket.
  • After: The command prints LIVE_OK, closes the cached connection, and exits naturally with status 0.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused ownership-preserving repair with supplied production-entry-point proof and a useful regression; broad CI is still running.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied live results exercise the changed CLI cleanup through pnpm openclaw with real OpenAI Responses on isolated state: the baseline remains alive after its reply, while the pinned candidate exits naturally with status 0; text, plugins-enabled, failure-status, and Gateway controls support the claimed scope.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A completed local OpenAI agent reply leaves the CLI process running and blocks scripts waiting for completion.
Reason: The documented one-shot contract requires cleanup after completion, and this maintainer-authored repair uses the established resource owner without adding configuration or changing output.

Merge readiness

✅ Ready for maintainer review

Keep this PR: it fixes a confirmed CLI cleanup omission still present on main, follows existing lifecycle ownership, and introduces no actionable correctness or security findings.

Priority: P2
Reviewed head: 95ec4e8210bb2dff8a6475723a6852548547cd69

Before merge

None.

Findings

None.

Agent review details

How this fits together

CLI dispatch runs the agent and writes its result, then invokes process-local cleanup; the existing AI resource registry closes retained provider transports after harness disposal.

flowchart TD
  A[Local agent command] --> B[Agent harness]
  B --> C[OpenAI Responses transport]
  C --> D[Reply output]
  D --> E[CLI teardown]
  E --> F[Session resource registry]
  F --> G[Close cached sockets]
Loading

Technical review

Best possible solution:

Use the existing provider-resource registry during terminal CLI cleanup while preserving Gateway custody and natural process exit.

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

Source confirms the retained WebSocket cleanup hook and the missing CLI invocation; the author supplies matching before-and-after real-provider observations, which were not rerun during this read-only review.

Is this the best way to solve the issue?

The patch repairs the missing lifecycle connection through the existing owner, preserves transport reuse during execution, and avoids forced exit or a parallel cleanup mechanism.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 58fda0da7614.

Provenance checked

  • CLI lifecycle cleanup keeps the original intent (518fe6a: Transient harnesses must close at process completion; subsequent shutdown work preserves natural exit and settles owned work.)
  • Documented local versus Gateway lifecycle keeps the original intent (c5254f1: Explicit local execution remains distinct from Gateway execution, avoiding ambiguous transport failures silently rerunning side effects.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 518fe6a adds src/cli/runtime-cleanup.ts:3 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 518fe6a25536; files: src/cli/runtime-cleanup.ts)

PR surface

Source +5, Tests +15, Docs 0. Total +20 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 5 0 +5
Tests 1 15 0 +15
Docs 1 1 1 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 21 1 +20

Labels

Label changes:

  • add P2: Completed local agent commands can keep scripted invocations alive on a retained provider socket; the repair has a focused process-local scope.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Completed local agent commands can keep scripted invocations alive on a retained provider socket; the repair has a focused process-local scope.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 12, 2026, 7:43 PM ET / 23:43 UTC.

@obviyus
obviyus merged commit f4d984d into main Oct 12, 2026
136 checks passed
@obviyus
obviyus deleted the fix/agent-local-exit branch October 12, 2026 23:50
@obviyus

obviyus commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

Landed in f4d984d. openclaw agent --local exits after the turn again.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
openclaw agent --local printed its result but did not exit when the turn used the native OpenAI Responses WebSocket transport. The transport's session cache keeps the ws TLS socket open for connection reuse (packages/ai/src/transports/openai-responses-websocket.ts), and the CLI's closeCliResources never ran the AI session-resource cleanup registry that the Gateway already drains on shutdown. CLI cleanup now runs that registry after harness disposal. There is no forced exit, and Gateway mode is unchanged. Proof: on main feb893c a real-provider turn printed its answer and was still alive at the 120 s kill, with async-hooks identifying the retained TLS socket. With the PR the same command exited 0 about 0.27 s after printing, in JSON mode, text mode and with plugins enabled. A provider 401 exited 1 promptly, and Gateway mode with its shutdown was unchanged.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS 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