Skip to content

feat(cli): continue, wait on, answer and stop maka run Sessions - #7210

Merged
M4n5ter merged 3 commits into
apache:mainfrom
YongGoose:feat/cli-run-follow-up
Nov 25, 2026
Merged

M4n5ter merged 3 commits into
apache:mainfrom
YongGoose:feat/cli-run-follow-up

Conversation

@YongGoose

@YongGoose YongGoose commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Supersedes #7149, which GitHub closed when rust-preview was deleted after the switch to main. Rebased onto main (73f2333); the run:: tests, clippy and fmt below were rerun there.

Summary

#7094 added maka run for a single prompt in a new Session. A script or another agent still can't follow up on that Session, and a run that stops on a question or an approval is cancelled with run failed, so there's nothing left to answer. This is the follow-up I offered in #6366 (reply in thread).

New options:

Call Does stdout
maka run --no-wait "<prompt>" Starts a Session and returns once the prompt is queued {"status":"submitted","sessionId":…}
maka run --session <id> "<prompt>" Queues a prompt to an existing Session and waits for its Run answer text, or waiting JSON
maka run --session <id> --wait Waits for the Session's current work without sending anything; returns the latest result when idle answer text, or waiting JSON
maka run --session <id> --answer '["Zeta"]' Answers the single pending question and waits for the same Run (--no-wait returns the receipt) answer text, or waiting JSON
maka run --session <id> --stop Stops the active Run and waits until it settles {"status":"stopped","sessionId":…}

A Session follow-up (--session with a prompt, --wait or --answer) that reaches a pending interaction leaves the Run waiting, prints {"status":"waiting","sessionId":…,"interactions":[…]} and exits 3, so a script can tell it from an answer without parsing stdout (clap uses 2 for usage errors). Questions include their text and options; other kinds only show kind. A single-shot maka run "<prompt>" keeps #7094's behavior: it cancels the Run and exits 1 with execution blocked by pending interaction(s): …, since nothing can answer it later. Multi-turn callers start with --no-wait.

How it works, using only existing Host operations (observe, queue, watch, work, pendingInteractions, interaction, respond, stop, workReadiness):

  • Every wait goes through one loop over agent.watch. Only the target differs: a message this invocation queued, a message or Run started elsewhere, or the Run that received an answer. Ctrl+C withdraws only a message this invocation queued; observing someone else's work just detaches.
  • Ended is now matched to the target Run. The old loop took the first Ended it saw, which is fine in a new Session but picks up an earlier Run's end when following up on an existing one.
  • A Run that is already terminal when the target resolves is read immediately instead of waiting for an Ended that may predate the subscription. Its failure row is found in the history tail by run_id, so the diagnostic no longer depends on the Ended sequence.
  • --wait picks the last queue entry that isn't held, then the active Run, then the latest Run. It reads the queue before observe, so a message admitted between the two reads shows up as the active Run instead of being skipped.
  • --stop was added because, without automatic cancellation, nothing in the CLI could abandon a waiting Run. It reuses cancel_work's settle loop.

Refs #6366

Review focus

  • A completed ordinary run still prints only the answer, so continuing it needs --no-wait up front. I left --json out of this PR.
  • Approvals (--approve / --deny) aren't included: permission decisions need a scope and a granted set, so I'd rather handle them separately. Until then they're reported as {"kind":"permissions"} and handled in the workbench.
  • While a follow-up's Run waits for input, an ephemeral Host stays up until someone answers or stops it.

Verification

  • cargo nextest run -p maka-cli --test integration -E 'test(/^run::/)': 10 passed, three runs in a row.
    • run_continues_a_session_started_without_waiting (new): --no-wait receipt, --wait returns the first answer, a second prompt on the same Session sees the first, --wait on the idle Session returns the latest answer, and the ephemeral Host releases the Root.
    • run_reports_a_pending_question_and_continues_with_the_answer (new): after a --no-wait start, --wait reports AskUserQuestion with its question and options and exits 3, --answer '["Zeta"]' finishes the same Run, and answering again fails with session has no pending question.
    • run_reuses_service_host_and_keeps_a_pending_permission_until_stopped (updated): a single-shot run that hits a sandbox permission request exits 1 with execution blocked by pending interaction(s): permissions(…) and leaves the service Host running. A Session started with --no-wait reports the same request from --wait with exit 3, twice (the Run wasn't cancelled); --stop stops it, a second --stop fails with session has no active run, and the service Host retires cleanly.
    • run_wait_reports_the_failure_of_an_ended_run (new): --wait reports a model 400 as run failed: … both while following the Run and after it ended.
    • run_sigterm_detaches_a_wait_without_cancelling_the_run (new): SIGTERM during --wait exits 1, and releasing the model afterwards still completes the Run.
    • The other five run:: tests pass unchanged, including the failure diagnostics after a model 400 and after a 429, which now come from the run_id lookup.
  • Without the exit-code change, the question and permission tests fail: --wait exits 0 instead of 3, and the single-shot run exits 0 instead of 1.
  • cargo fmt and cargo clippy -p maka-cli --all-targets: no warnings in the changed files.
  • Not run: the full just check and the other maka-cli test binaries.

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code drafted the implementation, tests and this description under my direction; I reviewed the design and the results. The commit carries Generated-by: Claude Code.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

`maka run` could only run one prompt in a new Session, and a run that
stopped on a question or an approval was cancelled. A script or another
agent had no way to answer it or follow up.

Add `--session` to queue a prompt to an existing Session, `--no-wait`
to return a JSON receipt once the prompt or answer is accepted, `--wait`
to wait on the Session's current work, `--answer` to answer its pending
question and `--stop` to stop its active Run. A wait that reaches a
pending interaction now leaves the Run waiting and prints it as JSON.
Ctrl+C withdraws only a message this invocation queued.

Generated-by: Claude Code
@github-actions github-actions Bot added the effort/L Under 1000 readable lines label Nov 23, 2026

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on! Reusing the existing Host operations and unifying the wait loop around Action / Target / Followed / Outcome keeps run.rs clean and readable, and the new black-box CLI integration tests are solid.

Before merging, there is one CLI contract decision we should align on (cc @Colafornia @me2seeks @Astro-Han), plus a couple of concrete bugs and small cleanups:

1. Single-shot maka run contract on pending interactions (design alignment)

Changing default maka run "<prompt>" from cancelling + exit 1 on pending interactions to returning {"status":"waiting",...} with exit 0 creates a few practical tensions while --json and --approve/--deny are still missing:

  • Mixed stdout + exit 0: scripts, CI jobs, and eval harnesses (the main use case from #7094 / #6667) doing out=$(maka run "...") || exit 1 will treat a blocked permissions or question turn as success and pass the waiting JSON downstream as the model's answer. (0.2 had --json so scripted callers had a uniform structured envelope.)
  • Dangling ephemeral Host with no CLI resolution for permissions: as noted under Review focus, a waiting Run keeps an ephemeral Host alive until answered or stopped. Because --approve/--deny isn't supported yet, hitting a permissions prompt on an isolated --root (where no workbench is attached) leaves a resident ephemeral Host that maka run can only --stop.

cc @Colafornia @me2seeks @Astro-Han — how would you like to shape the CLI contract here (e.g. distinct exit code for waiting, pairing with --json, or keeping single-shot calls without --session / --json fail-fast)?

2. Concrete bugs in products/maka/cli/src/run.rs

  • report_waiting can emit {"status":"waiting","interactions":[]} with exit 0: when a Question is answered or closed between DeliveryPage::Interactions and querying agent.interaction, if record.outcome.is_some() { continue; } skips it, but report_waiting still returns Outcome::Report(Report::Waiting { ..., interactions: [] }) instead of continuing to watch. Resolving question details inside the follow loop (and continuing if interactions ends up empty) fixes this and also lets stop.cancelled() cancel the agent.interaction query (currently report_waiting isn't guarded by stop.cancelled(), unlike read_result).
  • Action::Stop ignores Stopped.requested and Ctrl+C during settle: let _: Stopped = mutate(..., "stop", ...).await?; discards requested: false (which prepare_stop_run returns when the Run already finished between observe and stop, or when !may_control). settle() then immediately sees WorkReadiness::Ready, so --stop prints {"status":"stopped"} with exit 0 even though it didn't stop anything. Checking if !stopped.requested avoids a false receipt. Also settle_until is reused from cancel_work (post-Ctrl+C cleanup), so --stop ignores Ctrl+C for up to 15s while waiting to settle.

3. Small cleanups & test gaps

  • Skip redundant Host roundtrips:
    • Action::Send calls observe(host, &session_id) even with --no-wait, where through is discarded.
    • Action::Wait on an already-idle Session (no runnable queue entry, observation.run is None, latest_run_id is Some) still opens and closes an agent.watch subscription and queries agent.work twice (once in follow discarding answer, then again in read_result; the fast-completion path for Message/Submitted does the same double agent.work). Handling idle latest_run_id directly and passing the already-fetched Work::Delivered into pagination avoids both.
  • Tests (products/maka/cli/tests/integration/run.rs):
    • run_reuses_service_host_and_stops_on_pending_interaction still has its old name now that the Run stays waiting.
    • Worth adding coverage for (a) --wait on a failed idle Run (exercising read_failure_diagnostic with through: None + HistoryPosition::Tail) and (b) SIGTERM/Ctrl+C on --session <id> --wait confirming it detaches without cancelling the active Run.

`maka run` reported `{"status":"waiting","interactions":[]}` when every
pending question was answered or closed between the Interactions page and
the `agent.interaction` read. The questions are now described inside the
follow loop, where Ctrl+C can interrupt the read, and an empty result keeps
following instead of reporting.

`--stop` ignored `Stopped.requested`, so a Run that ended between `observe`
and `stop` printed a stopped receipt. It now fails with `session has no
active run`, and Ctrl+C interrupts the wait for the Run to settle.

`--wait` on an idle Session reads the latest Run's result without opening a
watch, a result already read to check for completion is reused instead of
read again, and `--no-wait` skips the `observe` it never used.

Generated-by: Claude Code
@YongGoose

Copy link
Copy Markdown
Member Author

Thanks for the thorough review. 8daa5ff addresses sections 2 and 3. For section 1, I'll wait for the CLI contract you settle on and implement it in this PR.

Bugs

  • Pending questions are now described inside the follow loop, under stop.cancelled(). If every question was answered or closed since the Interactions page, it keeps following instead of reporting an empty interactions list.
  • --stop fails with session has no active run when Stopped.requested is false, and Ctrl+C now interrupts the wait for the Run to settle. The settle loop stays shared with cancel_work; only the --stop caller selects on the signal.

Roundtrips

  • --no-wait no longer calls observe: submit takes the history cut as Option<u64>, and None means return the receipt.
  • --wait on an idle Session (no runnable queue entry, no active Run) reads the latest Run's result directly, without opening agent.watch.
  • When follow has already read a terminal Work::Delivered (the Run target check, or the message fast path), it hands that page to read_result, so agent.work isn't queried twice.

Tests

  • Renamed the permission test to run_reuses_service_host_and_keeps_a_pending_permission_until_stopped.
  • run_wait_reports_the_failure_of_an_ended_run: a Run fails on a model 400 after --no-wait; --wait reports the diagnostic, and a second --wait finds the Run already ended and reports it through the history tail lookup.
  • run_sigterm_detaches_a_wait_without_cancelling_the_run: SIGTERM ends --session <id> --wait with interrupted by signal. Releasing the held model turn then lets a new --wait return the answer.

cargo nextest run -p maka-cli --test integration -E 'test(/^run::/)': 10 passed. The three new or renamed tests passed three more times. cargo clippy -p maka-cli --all-targets and cargo fmt are clean for these files.

@M4n5ter

M4n5ter commented Nov 25, 2026

Copy link
Copy Markdown
Member

8daa5ffb3f looks very clean — all the fixes, roundtrip cleanups, and new behavioral tests look great.

Let's go ahead and settle section 1 so you aren't blocked waiting:

  1. Single-shot blocking maka run "<prompt>" (no --session, no --no-wait): keep feat(cli): add headless single-shot maka run command #7094's fail-fast behavior — when it hits a pending interaction, cancel the Run and exit 1 with the stderr diagnostic. That preserves existing script / CI / eval expectations and avoids leaving an orphaned ephemeral Host resident on an isolated --root (especially while --approve/--deny isn't available yet).
  2. Session follow-up waits (--session <id> with --wait, "<prompt>", or --answer): keep the Run waiting, print {"status":"waiting","sessionId":...,"interactions":[...]} to stdout, and exit with status 3 (distinct from 0 so scripts can tell waiting from a plain-text answer without parsing stdout before --json lands, and distinct from 2 which clap reserves for CLI usage errors). Multi-turn callers already start with maka run --no-wait "<prompt>" to get sessionId, so every follow-up flow goes through --session.

args::Cli::run already returns Result<ExitCode, HostError>, so run::Args::run can return Result<ExitCode, HostError> directly. Once this is in and CI is green, this looks ready to merge!

@Colafornia

Copy link
Copy Markdown
Member

+1 on @M4n5ter's split. Keeping single-shot maka run "<prompt>" fail-fast while routing interactive waiting through --session cleanly separates unattended eval/CI runs from multi-turn workflows.

…waits

A `maka run "<prompt>"` without `--session` or `--no-wait` cancels its Run
and exits 1 with `execution blocked by pending interaction(s): ...` when the
Run stops on a question or approval, as apache#7094 did. Nothing can answer it
later, and scripts, CI and evals treat a non-zero exit as failure.

Session follow-ups (`--session` with a prompt, `--wait` or `--answer`) leave
the Run waiting, print the `waiting` report and exit 3, so a script can tell
a pending decision from an answer without parsing stdout. clap uses 2 for
usage errors.

Generated-by: Claude Code
@YongGoose

Copy link
Copy Markdown
Member Author

Done in d03d375: a single-shot maka run "<prompt>" cancels its Run and exits 1 with execution blocked by pending interaction(s): … again, and --session follow-ups print the waiting report and exit 3. Args::run returns ExitCode directly. The question and permission tests now start with --no-wait and check both exit codes; all 10 run:: tests pass, and the two updated ones fail without the change. I've updated the description to match.

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d03d3752b looks great — OnPending::Fail / OnPending::Report keeps single-shot maka run "<prompt>" fail-fast while --session follow-ups report waiting and exit 3, and all 10 run:: integration tests pass cleanly on top of latest main. Merging!

@M4n5ter
M4n5ter merged commit 5e912ca into apache:main Nov 25, 2026
1 check passed
@Astro-Han

Copy link
Copy Markdown
Contributor

[P2] Synchronize the new SIGTERM test with the waiter attaching

The first post-merge full CI on 5e912ca2c3067a302520537c563c39ce67bc9ba2 fails in the new run_sigterm_detaches_a_wait_without_cancelling_the_run: Linux CLI job. At products/maka/cli/tests/integration/run.rs:1076, stderr is run cancelled before connecting to Host, rather than the asserted interrupted by signal.

The test sends SIGTERM after a fixed two-second sleep (lines 1063–1068), which does not establish that this new waiter has connected. products/maka/cli/src/run.rs:243–247 explicitly handles a signal during connection with that different diagnostic. Please synchronize with the waiter reaching the follow phase before signalling it, so the test actually exercises detachment and reaches the subsequent assertion that the original Run completes. The observed failure is in the newly added test; it does not establish that an attached wait cancels the original Run.

Scope: I checked this test, its connection/signal path, and the raw CI failure log. This is a focused post-merge CI finding, not complete validation of the CLI feature.


Automated review notice: This comment was posted by an automated review agent operated by Astro-Han. It is not an independent human review and does not replace one.

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

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants