Skip to content

feat(mocactl): show a turn's tool calls in a headless run (--show-tools) - #673

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/headless-show-tools-419
Nov 2, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/headless-show-tools-419

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Problem

A headless mocactl run prints only the assistant's text — the frame handler
wrote text and workspace_reset and dropped everything else. So to show a room
the commands an agent actually runs in its sandbox, the VM demo's research act had
to ask for --json and pipe it through a jq -j filter that reassembled the text
deltas by hand.

Fix

--show-tools prints each tool call between the turn's text, on stdout, in frame
order:

$ mocactl run "what python version is in here?" --show-tools
let me look
$ python3 --version

Python 3.11.9 is installed.
  • Interleaved, not set aside on stderr — the point of the flag is to show, in
    order, what the agent did before it answered. Each call is fenced in newlines of
    its own, because the text around it arrives as deltas that may end mid-line.
  • Formatted by toolSummary (render/tools.ts), the formatter the TUI and the
    transcript export already use, so the three can never disagree about what a given
    call looked like: $ ls /tmp, read src/a.ts:10-29, edit a.ts · +2 −1, and a
    truncated name(args) for a tool with no dedicated renderer.
  • Sanitized and collapsed to one line. The whole line is server-originated, so
    it goes through the same sanitizeRemote as the text path — the tool name as
    much as its arguments. sanitizeRemote deliberately keeps \n (the text path
    needs it), so a multi-line argument would otherwise read as several calls.
  • Only a FAILED tool_result is reported, and without its preview. A successful
    command's output is the agent's business, and the preview is remote multi-line
    text that would bury the answer.
  • Refused with --json (exit 2) rather than ignored. --json already streams
    the tool_use and tool_result frames, so the flag asks for nothing it does not
    have; a script whose flag is silently dropped learns nothing. It also keeps
    --json at one JSON document per line — a stray $ ls /tmp would break every
    reader of it.

The demo's act 2a now runs mocactl run "$P" --show-tools and the jq filter is
gone, as the issue proposed. No jq dependency left in that act.

Tests

11 new tests (8 headless, 3 CLI): exact interleaved output in frame order, plain
run unchanged without the flag, nothing printed for a successful call beyond the
command, escape sequences stripped from name and args, a multi-line argument kept
on one line, parity with toolSummary for a non-bash tool, every --json line
still JSON.parse-able, flag forwarded (and defaulting to false, not undefined),
the --json conflict refused with no turn run, and --show-tools present in USAGE.

Proven to bite: with the source changes stashed, 5/8 headless and 3/3 CLI tests go
red (the 3 that stay green are deliberate regression guards).

Mutation testing — 12 mutants, all caught, 0 survivors: the oneLine collapse
dropped, sanitizeRemote dropped, every tool_result reported instead of only the
failed one, the preview included, the flag ignored (at each of the two guards), tool
lines sent to stderr, the fencing newlines dropped, a hand-rolled formatter
substituted for toolSummary, the CLI not forwarding the flag, the --json
conflict not refused, and the flag forwarded as undefined rather than false.

Also green: tsc --noEmit, prettier --check on all 6 files, test/layering.test.ts
(headless.ts is outside the api/+core/ import restriction and already imports
render/blocks.js), and scripts/check-doc-links.py.

Closes #419.


Assisted-By: Claude

A headless `mocactl run` printed only the assistant's text: the frame
handler wrote `text` and `workspace_reset` and dropped the rest. To show a
room the commands an agent runs in its sandbox, the VM demo's research act
had to ask for `--json` and pipe it through a `jq -j` filter that
reassembled the text deltas by hand.

`--show-tools` prints each tool call between the turn's text, on stdout and
in frame order -- interleaved rather than set aside on stderr, because the
whole point is to show what the agent did before it answered. Each call is
fenced in newlines of its own, since the text around it arrives as deltas
that may end mid-line.

Formatted by `toolSummary` (render/tools.ts), the formatter the TUI and the
transcript export already use, so the three never disagree about what a
given call looked like: `$ ls /tmp` for bash, `read src/a.ts:10-29`,
`edit a.ts · +2 −1`, and a truncated `name(args)` for a tool with no
dedicated renderer. The whole line is server-originated, so it goes through
the same sanitizeRemote as the text path -- the tool NAME as much as its
arguments -- and is then collapsed to one line, because sanitizeRemote keeps
`\n` (the text path needs it) and a multi-line argument would otherwise read
as several calls.

Only a FAILED tool_result is reported, and without its preview: a successful
command's output is the agent's business, and the preview is remote
multi-line text that would bury the answer.

Refused with `--json` rather than ignored. `--json` already streams the
tool_use and tool_result frames, so the flag asks for nothing it does not
have, and a script whose flag is silently dropped learns nothing. `--json`
stays one JSON document per line: a stray `$ ls /tmp` would break every
reader of it.

The demo's research act now runs `mocactl run "$P" --show-tools` and the jq
filter is gone, as rossoctl#419 proposed.

Twelve mutants, all caught: the oneLine collapse dropped, sanitizeRemote
dropped, every tool_result reported instead of only the failed one, the
preview included, the flag ignored (at each of the two guards), tool lines
sent to stderr, a hand-rolled formatter instead of toolSummary, the fencing
newlines dropped, the CLI not forwarding the flag, the --json conflict not
refused, and the flag forwarded as undefined rather than false when absent.

Closes rossoctl#419.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 2, 2026 17:49
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 2, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Adds mocactl run --show-tools, which prints each tool call (formatted with the TUI's toolSummary, sanitized and collapsed to one line) between the turn's text, plus a marker for a failed tool_result. It's refused with --json. The VM demo's jq filter is replaced. The wiring, sanitization and --json contract look correct and are well tested.

One suggestion: Pi runs a turn's tool calls in parallel by default, and moca's harness doesn't override that. In that mode every tool_use frame of a batch arrives before any tool_result, and the results arrive in completion order. So the anonymous (that command failed) marker can sit under the wrong call. See the inline comment.

Author: cwiklik (MEMBER — maintainer)
Areas reviewed: TypeScript (mocactl CLI/headless), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (all 17 checks)

// Only a FAILED result is reported, and without its preview: a successful command's output is
// the agent's business, and a preview is remote multi-line text that would bury the answer.
else if (opts.showTools && e.frame.type === 'tool_result' && e.frame.isError)
io.out(' (that command failed)\n');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: The failure marker doesn't say which call failed, and with parallel tool execution it can be wrong. moca's harness doesn't set toolExecution, so Pi's Agent defaults to "parallel" (pi-fork packages/agent/src/agent.ts:218). In executeToolCallsParallel (agent-loop.ts), tool_execution_start is emitted for every call in the batch first, and then each tool_execution_end fires as its tool completes. A batch where the second of two commands fails therefore prints:

$ ls /tmp
$ ls /nope
  (that command failed)

That's fine by luck here. If the first call is the one that fails, the output is identical and points at the wrong command. The test fixture only covers the sequential interleave (use, result, use, result).

One cheap fix: remember id → summary from the tool_use frames and print the failed call's own summary, e.g. (failed: $ ls /nope). Or print the marker only when the result's id matches the most recent tool_use, and name the call otherwise. A test with two tool_use frames followed by out-of-order results would pin it down.

io.out(`\n${oneLine(sanitizeRemote(toolSummary(e.frame.name, e.frame.args)))}\n`);
// Only a FAILED result is reported, and without its preview: a successful command's output is
// the agent's business, and a preview is remote multi-line text that would bury the answer.
else if (opts.showTools && e.frame.type === 'tool_result' && e.frame.isError)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: The marker says "command" for every tool, so a failed read src/a.ts or edit a.ts also prints (that command failed). The README says "marks a tool call that failed". Consider (that call failed), or naming the call as suggested on the next line.

@cwiklik
cwiklik merged commit 32c117e into rossoctl:main Nov 2, 2026
29 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mocactl: show a turn's tool calls in a headless run (--show-tools)

2 participants