Skip to content

feat(tools): add Monitor for polling background tasks#184

Merged
emal-avala merged 1 commit intomainfrom
feat/tool-monitor
Apr 23, 2026
Merged

feat(tools): add Monitor for polling background tasks#184
emal-avala merged 1 commit intomainfrom
feat/tool-monitor

Conversation

@emal-avala
Copy link
Copy Markdown
Member

Summary

Adds Monitor tool — one call returns status, elapsed time, and the tail of output for a background task.

Why not just TaskGet + TaskOutput? Separate tool calls cost two turns of latency and double the tool-result JSON. Monitor is the combined read, shaped for the polling loop: agent calls it every N seconds while waiting on a build / deploy / long-running command.

Output cap: 8 KiB tail (configurable per-call via tail_bytes, capped). Prevents multi-MB tails from bloating the context mid-poll.

UTF-8 safety: the tail walks past continuation bytes so a multibyte character never gets split at the slice boundary. Prevents the from_utf8_lossy output from showing mojibake when the tail cutoff lands mid-char.

Failure mode: on I/O error (file mid-rotation, transient), returns [output unreadable: \<err\>] instead of erroring — polling should keep working even if a single read fails.

Implementation

  • New crates/lib/src/tools/monitor.rs (~200 LOC incl. tests)
  • TaskStatus variants mapped to human-readable labels (running / completed / failed: <msg> / killed)
  • Elapsed time computed from started_at to finished_at.unwrap_or_else(now)
  • Registered in ToolRegistry::with_defaults between TaskOutput and TodoWrite

Test plan

  • cargo fmt --all clean
  • cargo check passes
  • cargo clippy --all-targets -- -D warnings clean
  • cargo test -p agent-code-lib --lib tools::monitor — 6 tests (ASCII align, UTF-8 continuation walk, ASCII tail, smaller-than-cap, missing file, multibyte no-split)
  • Manual: !cargo build --release &Monitor tool call with returned task id shows status=running, then status=completed with tail of build output

Returns status (running/completed/failed/killed), elapsed seconds, and
the tail of stdout+stderr (last ~8 KB) in one call — cheaper than
separate TaskGet + TaskOutput and lets the agent loop on progress
('check the build every 30s until it completes').

UTF-8-safe tail: walks past continuation bytes so a multibyte char
never gets split at the slice boundary. Best-effort on I/O errors —
returns '[output unreadable: ...]' rather than erroring the tool,
since output files can be mid-rotation when the agent polls.

Tail capped at 8 KiB so polling doesn't balloon context.
@emal-avala emal-avala merged commit 1869ae5 into main Apr 23, 2026
13 of 14 checks passed
@emal-avala emal-avala deleted the feat/tool-monitor branch April 23, 2026 02:23
@emal-avala emal-avala mentioned this pull request Apr 23, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant