Skip to content

fix(cli): agent --json waits for a yielded turn's follow-up through the Gateway - #208755

Merged
obviyus merged 1 commit into
mainfrom
fix/agent-json-waits-for-yield
Nov 30, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/agent-json-waits-for-yield

Conversation

@obviyus

@obviyus obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

openclaw agent --json through the Gateway returns too early when the agent hands work to a subagent and yields (sessions_yield). The CLI prints the yielding turn's envelope (status: "ok", result.payloads: [], result.meta.yielded: true, result.requesterContinuationSettled: true) and exits 0 before the subagent reports and the session resumes. A script, CI job or orchestrator then tears down or moves on while the real work is still pending. Text mode already waits (#207491), and --local --json waits too.

Reproduced on main (9389d99) through a real isolated Gateway and a scripted model: the turn spawns one child and yields, the child answers after 8 s.

route exit time resumed turn
Gateway agent --json 0 8 s reaches the model after the CLI exits
Gateway agent (text) 0 24 s before exit
agent --local --json 0 23 s before exit; prints the yielding turn's envelope

Solution

--json now uses the same wait as text mode: the existing createYieldFollower in src/commands/agent-gateway-reply.ts. The two json || early returns are gone.

  • --json writes one document after the follow-up settles: the yielding turn's response, with today's fields. That is the same timing as text mode and --local --json.
  • If the follow-up fails or is aborted, or the wait ends early (explicit --timeout or a dropped connection), --json writes the documented ok: false failure envelope (with runId and origin: "gateway") instead of an empty success, prints the same stderr line as text mode, and exits 1.
  • SIGINT/SIGTERM during the wait keep the documented signal exit status (130/143) and write nothing to stdout, like any interrupted run: the CLI's signal exit owner exits before the command returns. Text mode behaves the same on main. Not a success, so no empty-success case remains.
  • No new field, flag, timer, RPC or second envelope. No change to sessions_yield.

The follow-up's reply text is not added to the --json document. The resumed turn is a separate Gateway run, and its full envelope (result.meta, deliveryStatus) reaches only the in-process announce caller; nothing on the CLI connection supplies it. Putting it on stdout needs a Gateway protocol decision and is out of scope here. The reply is in the session, as with --local --json today.

Bounds

  • The CLI adds no deadline. The wait ends on the follow-up's terminal chat event, an explicit --timeout (text-mode rule from fix: show a yielded agent turn's acknowledgment and its subagents' reply in the CLI #207491), a signal or connection loss.
  • Without --timeout, the follow-up run gets the Gateway's resolved run deadline (resolveAgentTimeoutMs, agent-turn-source-admission.ts:250).
  • Accepted edge, inherited from text mode and not changed here: a native subagent spawned without runTimeoutSeconds has no run deadline by default (agents.defaults.subagents.runTimeoutSeconds, 0 = no timeout, documented in docs/tools/subagents/tool-reference.md). Such a child, or a settle wake the Gateway loses while it stays up, keeps a --json wait without --timeout open until a signal or disconnect. Bounding that from the CLI would need either a CLI-side deadline (a second owner, which fix(cli): one run deadline for openclaw agent on both routes #208522 removed) or a Gateway-supplied deadline (protocol). A child with a deadline is bounded: in the repro, a child with runTimeoutSeconds: 10 timed out, the session resumed, and the CLI exited.

User Impact

  • openclaw agent --json against a Gateway returns after the resumed turn settles, with exit 0, or exits nonzero with the standard failure envelope when the follow-up fails or the wait ends early. It never returns an empty success at the yield.
  • Text mode and --local are unchanged.

Evidence

  • Red on main, green here: src/commands/agent-via-gateway.yield.test.ts. The three --json cases (reply, failed follow-up, aborted follow-up) fail on main (the CLI stopped waiting before the follow-up) and pass here. The other agent-via-gateway* and agent CLI suites pass.
  • Real CLI and Gateway: new case in test/e2e/agent-route-parity.built-cli.e2e.test.ts. A scripted model spawns one child and yields; the child answers once the CLI exits or after 3 s. Main dist: the CLI exits before the resumed turn (red). This branch's dist: the resumed turn runs while the CLI waits, stdout is one JSON document with result.meta.yielded: true, exit 0 (green).
  • Real CLI and Gateway, scripted model, isolated state, this branch's dist:
    • spawn, yield, child answers in 8 s: agent --json exits 0 at 16 s, after the resumed turn's model call (main: exit 0 at 8 s, before it);
    • Gateway stopped during the wait: exit 1, stdout is {"ok": false, "runId": "…", "origin": "gateway", "error": {"type": "cli_error", "message": "Stopped waiting for the subagents. Their result will still be added to session agent:main:main."}}, the same line on stderr;
    • SIGTERM during the wait: exit 143, empty stdout (no truncated document; nothing is written on that path).
  • Docs: docs/cli/agent.md notes, docs/tools/agent-send.md.

…he Gateway

The Gateway route returned at sessions_yield with an empty success while the subagents still worked. --json now shares text mode's wait, writes the turn's response once the session resumes, and writes the standard failure envelope when the follow-up fails or the wait ends early.
@clawsweeper

clawsweeper Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Pull request received.

ClawSweeper review ended

ClawSweeper stopped reviewing this revision because the pull request closed.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation commands Command implementations size: M labels Nov 30, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:ux-friction User-facing flow adds avoidable confusion or support burden without fully blocking progress. labels Nov 30, 2026
@obviyus
obviyus merged commit 531c797 into main Nov 30, 2026
116 checks passed
@obviyus
obviyus deleted the fix/agent-json-waits-for-yield branch November 30, 2026 03:34
@obviyus

obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Landed as 531c797. Bug: Gateway openclaw agent --json returned an empty success at sessions_yield while subagents still worked. Fix: --json shares text mode's wait and writes one document after the session resumes, or the standard ok: false envelope when the follow-up fails or the wait ends early. Proof: unit cases red on main and green here, built-CLI e2e red on main dist and green on this dist, real Gateway stop during the wait gives the failure envelope and exit 1.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 30, 2026
…he Gateway (openclaw#208755)

## What Problem This Solves

`openclaw agent --json` through the Gateway returns too early when the agent hands work to a subagent and yields (`sessions_yield`). The CLI prints the yielding turn's envelope (`status: "ok"`, `result.payloads: []`, `result.meta.yielded: true`, `result.requesterContinuationSettled: true`) and exits 0 before the subagent reports and the session resumes. A script, CI job or orchestrator then tears down or moves on while the real work is still pending. Text mode already waits (openclaw#207491), and `--local --json` waits too.

Reproduced on main (be5d7c6) through a real isolated Gateway and a scripted model: the turn spawns one child and yields, the child answers after 8 s.

| route | exit | time | resumed turn |
|---|---|---|---|
| Gateway `agent --json` | 0 | 8 s | reaches the model after the CLI exits |
| Gateway `agent` (text) | 0 | 24 s | before exit |
| `agent --local --json` | 0 | 23 s | before exit; prints the yielding turn's envelope |

## Solution

`--json` now uses the same wait as text mode: the existing `createYieldFollower` in `src/commands/agent-gateway-reply.ts`. The two `json ||` early returns are gone.

- `--json` writes one document after the follow-up settles: the yielding turn's response, with today's fields. That is the same timing as text mode and `--local --json`.
- If the follow-up fails or is aborted, or the wait ends early (explicit `--timeout` or a dropped connection), `--json` writes the documented `ok: false` failure envelope (with `runId` and `origin: "gateway"`) instead of an empty success, prints the same stderr line as text mode, and exits 1.
- `SIGINT`/`SIGTERM` during the wait keep the documented signal exit status (130/143) and write nothing to stdout, like any interrupted run: the CLI's signal exit owner exits before the command returns. Text mode behaves the same on main. Not a success, so no empty-success case remains.
- No new field, flag, timer, RPC or second envelope. No change to `sessions_yield`.

The follow-up's reply text is not added to the `--json` document. The resumed turn is a separate Gateway run, and its full envelope (`result.meta`, `deliveryStatus`) reaches only the in-process announce caller; nothing on the CLI connection supplies it. Putting it on stdout needs a Gateway protocol decision and is out of scope here. The reply is in the session, as with `--local --json` today.

## Bounds

- The CLI adds no deadline. The wait ends on the follow-up's terminal `chat` event, an explicit `--timeout` (text-mode rule from openclaw#207491), a signal or connection loss.
- Without `--timeout`, the follow-up run gets the Gateway's resolved run deadline (`resolveAgentTimeoutMs`, `agent-turn-source-admission.ts:250`).
- Accepted edge, inherited from text mode and not changed here: a native subagent spawned without `runTimeoutSeconds` has no run deadline by default (`agents.defaults.subagents.runTimeoutSeconds`, 0 = no timeout, documented in `docs/tools/subagents/tool-reference.md`). Such a child, or a settle wake the Gateway loses while it stays up, keeps a `--json` wait without `--timeout` open until a signal or disconnect. Bounding that from the CLI would need either a CLI-side deadline (a second owner, which openclaw#208522 removed) or a Gateway-supplied deadline (protocol). A child with a deadline is bounded: in the repro, a child with `runTimeoutSeconds: 10` timed out, the session resumed, and the CLI exited.

## User Impact

- `openclaw agent --json` against a Gateway returns after the resumed turn settles, with exit 0, or exits nonzero with the standard failure envelope when the follow-up fails or the wait ends early. It never returns an empty success at the yield.
- Text mode and `--local` are unchanged.

## Evidence

- Red on main, green here: `src/commands/agent-via-gateway.yield.test.ts`. The three `--json` cases (reply, failed follow-up, aborted follow-up) fail on main (the CLI stopped waiting before the follow-up) and pass here. The other `agent-via-gateway*` and agent CLI suites pass.
- Real CLI and Gateway: new case in `test/e2e/agent-route-parity.built-cli.e2e.test.ts`. A scripted model spawns one child and yields; the child answers once the CLI exits or after 3 s. Main dist: the CLI exits before the resumed turn (red). This branch's dist: the resumed turn runs while the CLI waits, stdout is one JSON document with `result.meta.yielded: true`, exit 0 (green).
- Real CLI and Gateway, scripted model, isolated state, this branch's dist:
  - spawn, yield, child answers in 8 s: `agent --json` exits 0 at 16 s, after the resumed turn's model call (main: exit 0 at 8 s, before it);
  - Gateway stopped during the wait: exit 1, stdout is `{"ok": false, "runId": "…", "origin": "gateway", "error": {"type": "cli_error", "message": "Stopped waiting for the subagents. Their result will still be added to session agent:main:main."}}`, the same line on stderr;
  - `SIGTERM` during the wait: exit 143, empty stdout (no truncated document; nothing is written on that path).
- Docs: `docs/cli/agent.md` notes, `docs/tools/agent-send.md`.

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

commands Command implementations docs Improvements or additions to documentation impact:ux-friction User-facing flow adds avoidable confusion or support burden without fully blocking progress. P2 Normal backlog priority with limited blast radius. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant