Skip to content

Tools: per-command shell output filters with tee-on-failure (RTK-inspired) - #189

Merged
FMXExpress merged 1 commit into
mainfrom
claude/shell-output-filters
Jun 9, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/shell-output-filters

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

shell_exec is the loudest tool in the registry. A typical session burns kilobytes on the success path of git status, cargo test, pytest, npm test, recursive greps, and recursive listings — mostly noise the model rarely reads in full. New PasClaw.Tools.Shell.Filters condenses the success path while leaving failures untouched. Concept borrowed from rtk-ai/rtk.

A — per-command filters

Filters fire only on the success path. Implemented in this PR:

Command Behaviour
git status branch + grouped counts (staged/unstaged/untracked) + first 8 files per group
git diff passthrough when ≤ 60 lines; otherwise one-line-per-file summary with +X -Y counts
git log newest 20 commits as <hash> <subject>
npm/yarn/pnpm/cargo test, pytest pass/skip count + every failure with the next ~10 lines of context
grep / findstr / Select-String aggregate by file, first 3 matches per file, top 20 files
ls -R / find / dir /s / Get-ChildItem -Recurse cap at 40 entries + (+N elided — narrow the path)

Unknown commands pass through unchanged — a bespoke incantation the model invented never comes back mangled.

B — tee-on-failure

ApplyShellFilter short-circuits when ExitCode <> 0 and returns the raw output verbatim. A failing test or compile needs the full stack trace to debug; a condensed failure would be worse than no filter at all. The same contract rtk-ai/rtk's tee pattern is built around.

Cross-shell normalisation (Windows!)

The dispatcher canonicalises Windows aliases so the same filter fires regardless of host shell. The model doesn't have to remember whether it's talking to bash, PowerShell, or cmd.exe:

// PowerShell -> Unix
Get-ChildItem -Recurse src   -> ls
gci -r src                    -> ls
Select-String -Path *.pas foo -> grep
sls foo *.pas                 -> grep
Get-Content README.md         -> cat
gc README.md                  -> cat

// cmd.exe builtins
dir /s        -> ls
findstr /R "foo" *.pas -> grep
type README.md -> cat

// Shell-wrapper peeling
powershell -Command "git status"   -> git status
pwsh -c "git status"                -> git status
cmd /c dir /s                       -> ls
bash -c "git log"                   -> git log

// Two-word commands (git/npm/yarn/pnpm/cargo/mvn carry the subcommand)
git status, git diff, git log
npm test, yarn test, pnpm test, cargo test

Counters in /stats

Process-lifetime ShellFilterCalls and ShellFilterBytesSaved accumulate across the session and surface in the TUI /stats overlay alongside the existing OutputCache numbers — operators can watch the savings climb. Reset on TUI startup.

Tests

shell_filters_tests (10 cases):

  • Canonicalisation: git subcommands, PowerShell aliases, cmd builtins, shell-wrapper peeling, edge cases
  • Tee-on-failure: ExitCode = 1 returns raw, counters don't bump
  • Unknown command: round-trip unchanged, counters don't bump
  • Each filter: git status (clean + with changes), git diff (passthrough + summary), pytest (failures surfaced with context), grep (alias-dispatched via Select-String)
  • Counter accumulation across calls

Test plan

  • make rebuilds cleanly
  • make test — all 14 suites green (smoke + 12 existing + new shell_filters_tests)
  • Manual on Linux: agent runs pasclaw agent, asks it to "run the tests" — confirm pytest / cargo test output is condensed, failures still visible. Check /stats shows non-zero shell line.
  • Manual on Windows: same exercise via powershell -Command "git status" invocation — confirm the wrapper peeling lands on the git status filter, the alias-dispatched Select-String filter matches a grep -r filter.
  • Manual: ask the agent to run a command that fails (cargo test against a known-broken test) — confirm the full failure context comes through, no truncation.

Generated by Claude Code

shell_exec is the loudest tool in the registry. A typical
session burns kilobytes on the success path of git status,
cargo test, pytest, npm test, recursive greps, and recursive
listings -- mostly noise the model rarely reads in full. New
PasClaw.Tools.Shell.Filters condenses the success path while
leaving failures untouched.

Filters (always success-path; failures pass through verbatim):
  git status     branch + grouped counts + first 8 files per group
  git diff       file-level summary with +/- counts when diff
                 exceeds 60 lines (passthrough below)
  git log        newest 20 commit hash + subject lines
  npm/yarn/pnpm/cargo test, pytest
                 pass count + every failure with context
  grep / findstr / Select-String
                 aggregate by file, first 3 matches per file
  ls -R / find / dir /s / Get-ChildItem -Recurse
                 cap at 40 entries + "(+N elided)"

Tee-on-failure: ApplyShellFilter short-circuits when ExitCode
<> 0 and returns RawOut unchanged. A failing test or compile
needs the full stack to debug -- a condensed failure would be
worse than no filter at all. Inspired by rtk-ai/rtk's same
contract.

Cross-shell normalisation so the same filter fires regardless
of host shell:
  PowerShell aliases  gci/Get-ChildItem -> ls
                      sls/Select-String -> grep
                      gc/Get-Content    -> cat
  cmd.exe builtins    dir / findstr / type
  Shell wrappers      powershell -Command "..."
                      pwsh -c "..."
                      cmd /c ...
                      bash -c "..."  (peeled before dispatch)
  Two-word commands   git/npm/yarn/pnpm/cargo/mvn carry the
                      subcommand as part of the dispatch key.

Unknown commands always pass through -- a bespoke incantation
the model invented never comes back mangled.

Process-lifetime counters (ShellFilterCalls, ShellFilterBytesSaved)
surface in the TUI /stats overlay next to the OutputCache numbers,
so operators can see the impact accumulating session-by-session.

Tests: shell_filters_tests covers canonicalisation (PowerShell +
cmd + shell wrappers), tee-on-failure, unknown-command passthrough,
each filter, and counter accumulation.
@FMXExpress
FMXExpress merged commit fe9cb67 into main Jun 9, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
PR FMXExpress#189 introduced PasClaw.Tools.Shell.Filters using
TStringArray as the SplitTokens return type. FPC's SysUtils
declares TStringArray; Delphi's RTL doesn't, so dcc64 errors
out on the function signature and every downstream
LowerCase(Tokens[i]) call cascades into E2008 / E2250 noise:

  PasClaw.Tools.Shell.Filters.pas(89):  E2003 Undeclared identifier: 'TStringArray'
  PasClaw.Tools.Shell.Filters.pas(98):  E2008 Incompatible types
  PasClaw.Tools.Shell.Filters.pas(133): E2008 Incompatible types
  PasClaw.Tools.Shell.Filters.pas(156): E2007 Constant or type identifier expected
  ... (15 more downstream cascades)

Same fix shape PasClaw uses in PasClaw.Tools.Registry: declare
TStringArray locally under {\$IFNDEF FPC} so the Delphi build
has a definition without conflicting with FPC's SysUtils
declaration. One-block change at the top of the implementation
section, no functional changes.
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.

2 participants