Skip to content

fix: show a yielded agent turn's acknowledgment and its subagents' reply in the CLI - #207491

Merged
obviyus merged 5 commits into
mainfrom
fix/cli-yield-ack
Nov 28, 2026
Merged

obviyus merged 5 commits into
mainfrom
fix/cli-yield-ack

Conversation

@obviyus

@obviyus obviyus commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

When you run openclaw agent -m "…" through the Gateway and the agent hands work to a subagent and yields (sessions_yield), the command exits after a few seconds and prints nothing. It even drops the short acknowledgment the model wrote for the user. The subagent's result arrives later in the session, but the terminal never shows it. To the user, the command did nothing. Control UI and Telegram already show a wait state for the same turn, so only the CLI looks dead.

Log signature from a production-shaped replay with gpt-5.4-mini: payloads: [], meta.yielded: true, meta.yieldAcknowledgment: "Waiting for the delegated arithmetic check to finish.", CLI stdout empty, exit 0 after about 3 s (3 of 5 subagent turns).

User Impact

  • After a yield with pending subagents, openclaw agent prints the model's acknowledgment, shows "Waiting on N subagent(s)…" while they work, then prints the reply the session gives when they finish, and exits 0.
  • The wait uses the run's existing --timeout, so --timeout 0 means no deadline. If the wait ends early (timeout, Gateway restart or drop, Ctrl-C) or the follow-up fails, the CLI prints one stderr line naming the session where the result will appear and exits 1. It never exits silently. Ctrl-C during the wait does not abort anything: the turn already ended and the subagents keep running.
  • --json output and timing are unchanged.
  • openclaw agent --local is unchanged. Subagent spawning there is a separate open gap.

Why This Change Was Made

The CLI already holds an authenticated connection for the agent call. That connection is an unscoped session-event audience: it has no session-scoped-events capability, so the Gateway already sends it the agent lifecycle and chat events of the session and its child sessions (server-broadcast.transport.test.ts, "visits only the session audience"). Control UI renders its wait state from these same events. The CLI now keeps the connection open after a yielded response (onResponse) and reads those events (onEvent, now passed through callGateway). No new RPC, subscription or protocol field.

The follow-up is the run that starts in the session after one of the turn's own completion-expecting children (acceptedSessionSpawns) ends; its reply is the next terminal chat event in the session. A session runs one turn at a time, so a run already in progress when the child ends, or queued behind the follow-up, is not taken. A follow-up that yields again hands over to the next run.

Accepted edge: the CLI matches the follow-up by session order, not by run identity. If a different turn starts in the same session in the idle gap after a child ends (or after a follow-up that yields again), that turn's reply is printed instead. With the default session.dmScope: main, a channel DM shares agent:main:main with a plain openclaw agent -m, so a DM that arrives during that gap is the realistic case. The fault is visible and recoverable: the real result still lands in the session. Exact matching needs either the requester-settle run identity in the agent response (a protocol change) or a sessions.messages.subscribe read of the transcript's subagent-completion marker; both are left for a follow-up. The follow-up's text is printed. Media attached to the follow-up is not (it stays in the session).

Reply printing moved from agent-via-gateway.ts into agent-gateway-reply.ts, because the line-cap ratchet allows no growth in that file. It is unchanged except that an ok reply with no payloads now prints meta.yieldAcknowledgment.

Evidence

  • Red on main, green here: src/commands/agent-via-gateway.yield.test.ts. The acknowledgment + follow-up test and the connection-drop test fail on the merge base (nothing printed, exit 0) and pass here. The other two tests cover --timeout 0 and --json. The existing agent-via-gateway*.test.ts suites pass: 4 files, 69 passed + 1 skipped.
  • Real Gateway, scripted model (isolated state, dist build): the model spawns one child and yields; the child answers after 20 s.
    • Main exits after 1.2 s, and only the thinking-level notice is printed.
    • This branch prints the acknowledgment and "Waiting on 1 subagent…", then "The helper finished: 17 × 19 = 323." It exits 0 after 21 s, with and without a model-written acknowledgment.
    • A Gateway restart during the wait printed "Stopped waiting for the subagents. Their result will still be added to session agent:main:…" and exited 1.
  • Real model, gpt-5.4-mini, CLI Gateway route: 15 trials on this branch, 0 with no visible output. gpt-5.4-mini yielded in 3 of them, and all 3 printed the acknowledgment and then the subagent's result (323) in the same command. Main: 5 trials, no yield in this sample. The main-side signature above is from the earlier replay.
  • Event fan-out probe: on a real Gateway, the agent connection received, without subscribing: the turn's chat final with yielded: true, the child's lifecycle end, and the announce:requester-settle:…:yield-1 lifecycle start and chat final on the parent session.

Not covered: a mocked or real test of more than one child; Discord and other channels are unaffected by this change.

CLI before and after, model acknowledgment
CLI before and after, no acknowledgment
CLI after: Gateway restarted during the wait

@clawsweeper

clawsweeper Bot commented Nov 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 ended

ClawSweeper stopped reviewing this revision because the pull request closed.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime commands Command implementations size: L labels Nov 28, 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. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 28, 2026
@obviyus
obviyus merged commit 890c189 into main Nov 28, 2026
128 checks passed
@obviyus
obviyus deleted the fix/cli-yield-ack branch November 28, 2026 11:55
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 29, 2026
…ply in the CLI (openclaw#207491)

## What Problem This Solves

When you run `openclaw agent -m "…"` through the Gateway and the agent hands work to a subagent and yields (`sessions_yield`), the command exits after a few seconds and prints nothing. It even drops the short acknowledgment the model wrote for the user. The subagent's result arrives later in the session, but the terminal never shows it. To the user, the command did nothing. Control UI and Telegram already show a wait state for the same turn, so only the CLI looks dead.

Log signature from a production-shaped replay with `gpt-5.4-mini`: `payloads: []`, `meta.yielded: true`, `meta.yieldAcknowledgment: "Waiting for the delegated arithmetic check to finish."`, CLI stdout empty, exit 0 after about 3 s (3 of 5 subagent turns).

## User Impact

- After a yield with pending subagents, `openclaw agent` prints the model's acknowledgment, shows "Waiting on N subagent(s)…" while they work, then prints the reply the session gives when they finish, and exits 0.
- The wait uses the run's existing `--timeout`, so `--timeout 0` means no deadline. If the wait ends early (timeout, Gateway restart or drop, Ctrl-C) or the follow-up fails, the CLI prints one stderr line naming the session where the result will appear and exits 1. It never exits silently. Ctrl-C during the wait does not abort anything: the turn already ended and the subagents keep running.
- `--json` output and timing are unchanged.
- `openclaw agent --local` is unchanged. Subagent spawning there is a separate open gap.

## Why This Change Was Made

The CLI already holds an authenticated connection for the `agent` call. That connection is an unscoped session-event audience: it has no `session-scoped-events` capability, so the Gateway already sends it the `agent` lifecycle and `chat` events of the session and its child sessions (`server-broadcast.transport.test.ts`, "visits only the session audience"). Control UI renders its wait state from these same events. The CLI now keeps the connection open after a yielded response (`onResponse`) and reads those events (`onEvent`, now passed through `callGateway`). No new RPC, subscription or protocol field.

The follow-up is the run that starts in the session after one of the turn's own completion-expecting children (`acceptedSessionSpawns`) ends; its reply is the next terminal `chat` event in the session. A session runs one turn at a time, so a run already in progress when the child ends, or queued behind the follow-up, is not taken. A follow-up that yields again hands over to the next run.

Accepted edge: the CLI matches the follow-up by session order, not by run identity. If a different turn starts in the same session in the idle gap after a child ends (or after a follow-up that yields again), that turn's reply is printed instead. With the default `session.dmScope: main`, a channel DM shares `agent:main:main` with a plain `openclaw agent -m`, so a DM that arrives during that gap is the realistic case. The fault is visible and recoverable: the real result still lands in the session. Exact matching needs either the requester-settle run identity in the `agent` response (a protocol change) or a `sessions.messages.subscribe` read of the transcript's subagent-completion marker; both are left for a follow-up. The follow-up's text is printed. Media attached to the follow-up is not (it stays in the session).

Reply printing moved from `agent-via-gateway.ts` into `agent-gateway-reply.ts`, because the line-cap ratchet allows no growth in that file. It is unchanged except that an ok reply with no payloads now prints `meta.yieldAcknowledgment`.

## Evidence

- **Red on main, green here:** `src/commands/agent-via-gateway.yield.test.ts`. The acknowledgment + follow-up test and the connection-drop test fail on the merge base (nothing printed, exit 0) and pass here. The other two tests cover `--timeout 0` and `--json`. The existing `agent-via-gateway*.test.ts` suites pass: 4 files, 69 passed + 1 skipped.
- **Real Gateway, scripted model (isolated state, dist build):** the model spawns one child and yields; the child answers after 20 s.
  - Main exits after 1.2 s, and only the thinking-level notice is printed.
  - This branch prints the acknowledgment and "Waiting on 1 subagent…", then "The helper finished: 17 × 19 = 323." It exits 0 after 21 s, with and without a model-written acknowledgment.
  - A Gateway restart during the wait printed "Stopped waiting for the subagents. Their result will still be added to session agent:main:…" and exited 1.
- **Real model, `gpt-5.4-mini`, CLI Gateway route:** 15 trials on this branch, 0 with no visible output. gpt-5.4-mini yielded in 3 of them, and all 3 printed the acknowledgment and then the subagent's result (323) in the same command. Main: 5 trials, no yield in this sample. The main-side signature above is from the earlier replay.
- **Event fan-out probe:** on a real Gateway, the `agent` connection received, without subscribing: the turn's `chat` final with `yielded: true`, the child's lifecycle `end`, and the `announce:requester-settle:…:yield-1` lifecycle `start` and `chat` final on the parent session.


Not covered: a mocked or real test of more than one child; Discord and other channels are unaffected by this change.

![CLI before and after, model acknowledgment](https://github.com/user-attachments/assets/9a3bf26d-8ed6-470a-8102-8f53c79db020)
![CLI before and after, no acknowledgment](https://github.com/user-attachments/assets/df8aa461-bdf8-4b44-b703-301ece14ccc8)
![CLI after: Gateway restarted during the wait](https://github.com/user-attachments/assets/25ce00c1-8f81-457c-9492-ee3e59e01289)

Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
#205320)

Root cause: src/cli/logs-cli.ts:114 `fetchGatewayLogs` opens a new Gateway client through `callGatewayFromCli("logs.tail")` on every `--follow` poll, so each poll pays a full WebSocket handshake and the Gateway logs one `client connected ... name=gateway:logs.tail` line per second into the log being followed.

Fixes #205269

## What Problem This Solves

`openclaw logs --follow` reconnects and re-authenticates to the Gateway on every poll (once a second by default). The Gateway logs each of those handshakes at info, so following the log writes about one line per second into that same log: about 86,400 lines a day for each follower left running. Thanks @anyech for the clear report and the source pointers.

## User Impact

`openclaw logs --follow` now connects once and keeps polling `logs.tail` on that connection. The followed log shows one `client connected` line for the follower instead of one per second. Output lines, `--interval`, cursors, the journal and local-file fallbacks, the reconnect notices and Ctrl-C behave as before. One-shot `openclaw logs` is unchanged.

## Why This Change Was Made

This is option 1 from the issue. Polling `logs.tail` stays (no new subscription method, per the earlier #56475 decision). The existing Gateway call path already supports more requests on the same authenticated connection (`onResponse`); the follow loop now keeps that connection open and sends each later `logs.tail` on it. `onResponse` now also receives the first response, so the first poll's result is not requested twice. Every follow poll keeps the `--timeout` deadline, including connecting for the first poll on a connection. A poll that runs out of time closes the connection and reports the same "gateway timeout after Nms" error as before, so the existing follow retry reconnects.

When the connection drops (for example a Gateway restart), the next poll opens a new connection through the same setup, so auth is resolved again, and failures go through the existing follow retry and backoff notices. On exit (Ctrl-C, closed stdout, fatal error) the connection is closed before the command returns.

## Evidence

Built Gateway and CLI for base `aa2e4e6a9f9e` and this head inside a secretless `node:24.22.1` container (token auth, loopback, task-owned state), two passes each. Proof lines: #205320 (comment) (first run: #205320 (comment))

| check | base (2 passes) | this PR (2 passes) |
|---|---|---|
| `logs --follow` for 60 s: Gateway `client connected ... name=gateway:logs.tail` lines | 58 / 58 | 1 / 1 |
| one-shot `openclaw logs` (control): connections | 1 / 1 | 1 / 1 |
| Gateway stopped and restarted during follow | pass 1 recovered; pass 2 exited with HTTP 503 "admission closed" (a per-poll connect hit the shutdown window) | recovered both passes: disconnect notices, then "gateway reconnected", 95 more lines printed |
| Ctrl-C: exit code, exit time, sockets left on the Gateway port | 130, 104 ms, 0 / 130, 104 ms, 0 | 130, 103 ms, 0 / 130, 112 ms, 0 |
| base CLI and this CLI following the same Gateway for 30 s | | same lines in both outputs (212 and 211 lines, 0 differences) |

Regression test `reuses one Gateway connection for every --follow poll` fails on base (the follow loop never uses the open connection) and passes here. The logs CLI suites (6 files) pass.

Merge danger: two-way door. CLI-only connection lifetime; no stored state, config, protocol or Gateway change. A revert restores per-poll connections.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit 4076a73)

Companion audit: #207491 is selected later only for the non-JSON acknowledgment; #205898 adds update-follow behavior independently. Preserve existing call result, reconnect and cleanup.

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
…ply in the CLI (#207491)

## What Problem This Solves

When you run `openclaw agent -m "…"` through the Gateway and the agent hands work to a subagent and yields (`sessions_yield`), the command exits after a few seconds and prints nothing. It even drops the short acknowledgment the model wrote for the user. The subagent's result arrives later in the session, but the terminal never shows it. To the user, the command did nothing. Control UI and Telegram already show a wait state for the same turn, so only the CLI looks dead.

Log signature from a production-shaped replay with `gpt-5.4-mini`: `payloads: []`, `meta.yielded: true`, `meta.yieldAcknowledgment: "Waiting for the delegated arithmetic check to finish."`, CLI stdout empty, exit 0 after about 3 s (3 of 5 subagent turns).

## User Impact

- After a yield with pending subagents, `openclaw agent` prints the model's acknowledgment, shows "Waiting on N subagent(s)…" while they work, then prints the reply the session gives when they finish, and exits 0.
- The wait uses the run's existing `--timeout`, so `--timeout 0` means no deadline. If the wait ends early (timeout, Gateway restart or drop, Ctrl-C) or the follow-up fails, the CLI prints one stderr line naming the session where the result will appear and exits 1. It never exits silently. Ctrl-C during the wait does not abort anything: the turn already ended and the subagents keep running.
- `--json` output and timing are unchanged.
- `openclaw agent --local` is unchanged. Subagent spawning there is a separate open gap.

## Why This Change Was Made

The CLI already holds an authenticated connection for the `agent` call. That connection is an unscoped session-event audience: it has no `session-scoped-events` capability, so the Gateway already sends it the `agent` lifecycle and `chat` events of the session and its child sessions (`server-broadcast.transport.test.ts`, "visits only the session audience"). Control UI renders its wait state from these same events. The CLI now keeps the connection open after a yielded response (`onResponse`) and reads those events (`onEvent`, now passed through `callGateway`). No new RPC, subscription or protocol field.

The follow-up is the run that starts in the session after one of the turn's own completion-expecting children (`acceptedSessionSpawns`) ends; its reply is the next terminal `chat` event in the session. A session runs one turn at a time, so a run already in progress when the child ends, or queued behind the follow-up, is not taken. A follow-up that yields again hands over to the next run.

Accepted edge: the CLI matches the follow-up by session order, not by run identity. If a different turn starts in the same session in the idle gap after a child ends (or after a follow-up that yields again), that turn's reply is printed instead. With the default `session.dmScope: main`, a channel DM shares `agent:main:main` with a plain `openclaw agent -m`, so a DM that arrives during that gap is the realistic case. The fault is visible and recoverable: the real result still lands in the session. Exact matching needs either the requester-settle run identity in the `agent` response (a protocol change) or a `sessions.messages.subscribe` read of the transcript's subagent-completion marker; both are left for a follow-up. The follow-up's text is printed. Media attached to the follow-up is not (it stays in the session).

Reply printing moved from `agent-via-gateway.ts` into `agent-gateway-reply.ts`, because the line-cap ratchet allows no growth in that file. It is unchanged except that an ok reply with no payloads now prints `meta.yieldAcknowledgment`.

## Evidence

- **Red on main, green here:** `src/commands/agent-via-gateway.yield.test.ts`. The acknowledgment + follow-up test and the connection-drop test fail on the merge base (nothing printed, exit 0) and pass here. The other two tests cover `--timeout 0` and `--json`. The existing `agent-via-gateway*.test.ts` suites pass: 4 files, 69 passed + 1 skipped.
- **Real Gateway, scripted model (isolated state, dist build):** the model spawns one child and yields; the child answers after 20 s.
  - Main exits after 1.2 s, and only the thinking-level notice is printed.
  - This branch prints the acknowledgment and "Waiting on 1 subagent…", then "The helper finished: 17 × 19 = 323." It exits 0 after 21 s, with and without a model-written acknowledgment.
  - A Gateway restart during the wait printed "Stopped waiting for the subagents. Their result will still be added to session agent:main:…" and exited 1.
- **Real model, `gpt-5.4-mini`, CLI Gateway route:** 15 trials on this branch, 0 with no visible output. gpt-5.4-mini yielded in 3 of them, and all 3 printed the acknowledgment and then the subagent's result (323) in the same command. Main: 5 trials, no yield in this sample. The main-side signature above is from the earlier replay.
- **Event fan-out probe:** on a real Gateway, the `agent` connection received, without subscribing: the turn's `chat` final with `yielded: true`, the child's lifecycle `end`, and the `announce:requester-settle:…:yield-1` lifecycle `start` and `chat` final on the parent session.

Not covered: a mocked or real test of more than one child; Discord and other channels are unaffected by this change.

![CLI before and after, model acknowledgment](https://github.com/user-attachments/assets/9a3bf26d-8ed6-470a-8102-8f53c79db020)
![CLI before and after, no acknowledgment](https://github.com/user-attachments/assets/df8aa461-bdf8-4b44-b703-301ece14ccc8)
![CLI after: Gateway restarted during the wait](https://github.com/user-attachments/assets/25ce00c1-8f81-457c-9492-ee3e59e01289)

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 890c189)

Companion audit: #207510 one-shot resource settlement and #208522 global deadline are unrelated to non-JSON empty-payload acknowledgment; skip event follower and Gateway call changes.

Release adaptation: Port only the stable non-JSON, empty-payload acknowledgment. Keep payload logging in a small sibling and isolate the new test to respect both line-cap checks. Omit the event follower and all Gateway call changes.

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot added a commit that referenced this pull request Nov 30, 2026
The #207491 CLI yield test introduced closed Gateway mocks and the protected Codex continuation backport changed an existing closed mock. Keep the same overrides but preserve all other exports. The Codex and Matrix QA backports leave two stale fingerprints; remove only those entries from the shrinking ratchet.

Co-authored-by: RomneyDa <[email protected]>
obviyus added a commit that referenced this pull request Nov 30, 2026
…he Gateway (#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 (#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 #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 #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]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
…ply in the CLI (#207491)

## What Problem This Solves

When you run `openclaw agent -m "…"` through the Gateway and the agent hands work to a subagent and yields (`sessions_yield`), the command exits after a few seconds and prints nothing. It even drops the short acknowledgment the model wrote for the user. The subagent's result arrives later in the session, but the terminal never shows it. To the user, the command did nothing. Control UI and Telegram already show a wait state for the same turn, so only the CLI looks dead.

Log signature from a production-shaped replay with `gpt-5.4-mini`: `payloads: []`, `meta.yielded: true`, `meta.yieldAcknowledgment: "Waiting for the delegated arithmetic check to finish."`, CLI stdout empty, exit 0 after about 3 s (3 of 5 subagent turns).

## User Impact

- After a yield with pending subagents, `openclaw agent` prints the model's acknowledgment, shows "Waiting on N subagent(s)…" while they work, then prints the reply the session gives when they finish, and exits 0.
- The wait uses the run's existing `--timeout`, so `--timeout 0` means no deadline. If the wait ends early (timeout, Gateway restart or drop, Ctrl-C) or the follow-up fails, the CLI prints one stderr line naming the session where the result will appear and exits 1. It never exits silently. Ctrl-C during the wait does not abort anything: the turn already ended and the subagents keep running.
- `--json` output and timing are unchanged.
- `openclaw agent --local` is unchanged. Subagent spawning there is a separate open gap.

## Why This Change Was Made

The CLI already holds an authenticated connection for the `agent` call. That connection is an unscoped session-event audience: it has no `session-scoped-events` capability, so the Gateway already sends it the `agent` lifecycle and `chat` events of the session and its child sessions (`server-broadcast.transport.test.ts`, "visits only the session audience"). Control UI renders its wait state from these same events. The CLI now keeps the connection open after a yielded response (`onResponse`) and reads those events (`onEvent`, now passed through `callGateway`). No new RPC, subscription or protocol field.

The follow-up is the run that starts in the session after one of the turn's own completion-expecting children (`acceptedSessionSpawns`) ends; its reply is the next terminal `chat` event in the session. A session runs one turn at a time, so a run already in progress when the child ends, or queued behind the follow-up, is not taken. A follow-up that yields again hands over to the next run.

Accepted edge: the CLI matches the follow-up by session order, not by run identity. If a different turn starts in the same session in the idle gap after a child ends (or after a follow-up that yields again), that turn's reply is printed instead. With the default `session.dmScope: main`, a channel DM shares `agent:main:main` with a plain `openclaw agent -m`, so a DM that arrives during that gap is the realistic case. The fault is visible and recoverable: the real result still lands in the session. Exact matching needs either the requester-settle run identity in the `agent` response (a protocol change) or a `sessions.messages.subscribe` read of the transcript's subagent-completion marker; both are left for a follow-up. The follow-up's text is printed. Media attached to the follow-up is not (it stays in the session).

Reply printing moved from `agent-via-gateway.ts` into `agent-gateway-reply.ts`, because the line-cap ratchet allows no growth in that file. It is unchanged except that an ok reply with no payloads now prints `meta.yieldAcknowledgment`.

## Evidence

- **Red on main, green here:** `src/commands/agent-via-gateway.yield.test.ts`. The acknowledgment + follow-up test and the connection-drop test fail on the merge base (nothing printed, exit 0) and pass here. The other two tests cover `--timeout 0` and `--json`. The existing `agent-via-gateway*.test.ts` suites pass: 4 files, 69 passed + 1 skipped.
- **Real Gateway, scripted model (isolated state, dist build):** the model spawns one child and yields; the child answers after 20 s.
  - Main exits after 1.2 s, and only the thinking-level notice is printed.
  - This branch prints the acknowledgment and "Waiting on 1 subagent…", then "The helper finished: 17 × 19 = 323." It exits 0 after 21 s, with and without a model-written acknowledgment.
  - A Gateway restart during the wait printed "Stopped waiting for the subagents. Their result will still be added to session agent:main:…" and exited 1.
- **Real model, `gpt-5.4-mini`, CLI Gateway route:** 15 trials on this branch, 0 with no visible output. gpt-5.4-mini yielded in 3 of them, and all 3 printed the acknowledgment and then the subagent's result (323) in the same command. Main: 5 trials, no yield in this sample. The main-side signature above is from the earlier replay.
- **Event fan-out probe:** on a real Gateway, the `agent` connection received, without subscribing: the turn's `chat` final with `yielded: true`, the child's lifecycle `end`, and the `announce:requester-settle:…:yield-1` lifecycle `start` and `chat` final on the parent session.

Not covered: a mocked or real test of more than one child; Discord and other channels are unaffected by this change.

![CLI before and after, model acknowledgment](https://github.com/user-attachments/assets/9a3bf26d-8ed6-470a-8102-8f53c79db020)
![CLI before and after, no acknowledgment](https://github.com/user-attachments/assets/df8aa461-bdf8-4b44-b703-301ece14ccc8)
![CLI after: Gateway restarted during the wait](https://github.com/user-attachments/assets/25ce00c1-8f81-457c-9492-ee3e59e01289)

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 890c189)

Release adaptation: Mock only the admission boundary in the synthetic mocked-Gateway event fixture, not runtime, so the inherited agent execution marker is not mistaken for an operator admission test. Gateway attribution checks are unchanged.

Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 30, 2026
## What Problem This Solves

One command, two deadlines. Without `--timeout`:

- `openclaw agent` through the Gateway sent a CLI fallback of **600 s** (`--timeout ?? agents.defaults.timeoutSeconds ?? 600`) as the run's per-attempt deadline. At 600 s the Gateway aborted the attempt and the run ended with `timeoutPhase: "provider"` and "Request timed out before a response was generated".
- `openclaw agent --local` resolved `agents.defaults.timeoutSeconds`, which defaults to **48 hours**, as Gateway turns and cron do.
- `openclaw agent exec` hardcoded **600 s**.

So long coding tasks finished with `--local` and were cut at 10 minutes through the Gateway. In a long-task coding evaluation (DeepSWE), 3 of 5 Gateway-route trials ended at 607–608 s, while `--local` trials of the same tasks ran 1,084–2,279 s without a cut.

The 600 s dates from when the CLI waited on a bounded client timer. That wait has been unbounded for a long time (the CLI waits for the Gateway's terminal result), but the 600 kept travelling as the run's own deadline.

Three related bugs come from the same CLI-side value:

1. **Remote Gateway config was overridden.** The CLI sent the CLI host's `agents.defaults.timeoutSeconds`, so a remote Gateway's own setting never applied to CLI turns.
2. **Subagent timeouts were shadowed.** The Gateway gives a turn into a live registered subagent session that subagent's `runTimeoutSeconds` only when the request carries no timeout. The CLI always sent one.
3. **The docs matched only one route.** They said "default 600" for `openclaw agent`, which was true on the Gateway route and false with `--local`.

## Fix

`resolveAgentTimeoutMs` (`agents.defaults.timeoutSeconds`, default 48 h) is now the only owner of how long `openclaw agent` and `openclaw agent exec` run, on both routes.

- The Gateway route sends `timeout` only when `--timeout` is given; the Gateway resolves the default exactly as for every other turn.
- `agent exec` no longer hardcodes 600; without `--timeout` its embedded run resolves the same default.
- The CLI's wait for a yielded turn's subagents has no deadline unless `--timeout` is given. It still ends on the follow-up reply, Ctrl-C or disconnect.
- Removed the stale transport hint "use --timeout <seconds> to extend the CLI wait".
- Help text and docs now state one default for every route: `docs/cli/agent.md`, `docs/tools/agent-send.md`, `docs/gateway/cli-backends.md`, `docs/tools/autoreview.md`, `docs/plugins/codex-harness-reference/timeouts.md`.

`--timeout N` behaves exactly as before on both routes, including `0` (unlimited).

## Behavior change

- **Old:** without `--timeout`, `openclaw agent` through the Gateway and `openclaw agent exec` stopped each model attempt at **600 s (10 minutes)**.
- **New:** they use `agents.defaults.timeoutSeconds`, which is **48 hours** unless configured, the same deadline as `openclaw agent --local`, Control UI and channel turns, and cron.
- **To keep the old cap:** pass `--timeout 600`, or set `agents.defaults.timeoutSeconds: 600` in the config (the Gateway's config on the Gateway route).
- **Accepted cost:** a script or CI job that relied on the implicit 600 s cap and passes no `--timeout` loses it. A run that neither stalls nor repeats can then run until 48 hours or the job's own limit. Stalled and looping runs are still stopped by the model idle timeout (120 s without output) and the existing loop guards.
- **The timeout message** now names the limit that fired and how to raise it: "…the run reached its 10m limit. Please try again, or raise the limit with `--timeout <seconds>` for `openclaw agent`, or `agents.defaults.timeoutSeconds` in your config."

## User Impact

- Long tasks through the Gateway are no longer stopped at 10 minutes; `--local` is unchanged.
- **Default change:** Gateway-route `openclaw agent` and `agent exec` without `--timeout` now get `agents.defaults.timeoutSeconds` (48 h unless configured) instead of 600 s. Scripts that relied on the implicit 10-minute stop should pass `--timeout 600`.
- Autoreview passes launched without a helper timeout can now run up to `agents.defaults.timeoutSeconds` (48 h unless configured) instead of 600 s; helpers that set a timeout are unchanged.
- A scripted `openclaw agent` whose yielded turn waits on subagents now waits until they settle (or Ctrl-C/disconnect) instead of exiting 1 at 600 s; pass `--timeout` to bound that wait.
- A dead provider is still caught by the model idle timeout (120 s without output). A looping agent is bounded by the configured run deadline and the existing runaway guards.
- If the CLI process is killed with SIGKILL or its connection drops, an accepted Gateway run continues and its result is added to the session (accepted execution is bound to the Gateway lifetime, not the caller's connection). Ctrl-C/SIGTERM still sends `chat.abort`, as before.

## Evidence

- Red on the merge base, green with the fix:
  - `src/commands/agent-via-gateway.deadline.test.ts`: without `--timeout`, the Gateway request carries no `timeout` for a local and a remote Gateway (main sent 600 from the CLI host's config).
  - `src/commands/agent-exec.test.ts`: `agent exec` without `--timeout` passes no timeout to the run (main passed "600").
  - `src/cli/program/register.agent.test.ts`: `agent exec fix` without `--timeout` reaches the command with no timeout (main's Commander default injected "600").
- The Gateway already gives a timeout-less request into a registered subagent session that subagent's budget (`agent.session-followup` suite, unchanged).
- Affected consumers rerun green: `agent-via-gateway.test.ts`, `agent-via-gateway.yield.test.ts`, `register.agent.test.ts`.
- Real-product evidence: the long-task evaluation above (Gateway route 3 of 5 trials cut at 600 s; `--local` none cut).
- `src/agents/embedded-agent-runner/run/terminal-timeout.test.ts`: the provider timeout message names the limit that fired (from the run's deadline), `--timeout <seconds>` and `agents.defaults.timeoutSeconds`.

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit b170f7d)

Release adaptation: Beta.3 does not have the yielded-reply relay introduced by #207491. Keep its existing reply path and port the run-owner deadline, command defaults, timeout diagnostic and request-boundary regression without introducing the unrelated relay.

Co-authored-by: RomneyDa <[email protected]>
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

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. commands Command implementations gateway Gateway runtime 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: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant