Repository navigation
feat(mocactl): show a turn's tool calls in a headless run (--show-tools) - #673
Conversation
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]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
Problem
A headless
mocactl runprints only the assistant's text — the frame handlerwrote
textandworkspace_resetand dropped everything else. So to show a roomthe commands an agent actually runs in its sandbox, the VM demo's research act had
to ask for
--jsonand pipe it through ajq -jfilter that reassembled the textdeltas by hand.
Fix
--show-toolsprints each tool call between the turn's text, on stdout, in frameorder:
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.
toolSummary(render/tools.ts), the formatter the TUI and thetranscript 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 atruncated
name(args)for a tool with no dedicated renderer.it goes through the same
sanitizeRemoteas the text path — the tool name asmuch as its arguments.
sanitizeRemotedeliberately keeps\n(the text pathneeds it), so a multi-line argument would otherwise read as several calls.
tool_resultis reported, and without its preview. A successfulcommand's output is the agent's business, and the preview is remote multi-line
text that would bury the answer.
--json(exit 2) rather than ignored.--jsonalready streamsthe
tool_useandtool_resultframes, so the flag asks for nothing it does nothave; a script whose flag is silently dropped learns nothing. It also keeps
--jsonat one JSON document per line — a stray$ ls /tmpwould break everyreader of it.
The demo's act 2a now runs
mocactl run "$P" --show-toolsand thejqfilter isgone, as the issue proposed. No
jqdependency left in that act.Tests
11 new tests (8 headless, 3 CLI): exact interleaved output in frame order, plain
rununchanged without the flag, nothing printed for a successful call beyond thecommand, escape sequences stripped from name and args, a multi-line argument kept
on one line, parity with
toolSummaryfor a non-bash tool, every--jsonlinestill
JSON.parse-able, flag forwarded (and defaulting tofalse, notundefined),the
--jsonconflict refused with no turn run, and--show-toolspresent 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
oneLinecollapsedropped,
sanitizeRemotedropped, everytool_resultreported instead of only thefailed 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--jsonconflict not refused, and the flag forwarded as
undefinedrather thanfalse.Also green:
tsc --noEmit,prettier --checkon all 6 files,test/layering.test.ts(
headless.tsis outside theapi/+core/import restriction and already importsrender/blocks.js), andscripts/check-doc-links.py.Closes #419.
Assisted-By: Claude