Overhaul shell commands for POSIX compliance and AI-agent ergonomics

Each built-in shell command has been audited and rewritten to support the
options AI agents (and humans) typically reach for, close critical behavior
gaps, and align with POSIX/GNU conventions:

- Shared argument parser (utils.ts) handles combined short flags, long
  options with '=VALUE' or space-separated values, the '--' sentinel,
  numeric shortcuts like 'head -5', and '-' for stdin.
- cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed
  the legacy 'absolute paths are not supported' and 'reading from stdin
  is not supported' barriers where they made agent use awkward.
- mv now overwrites destinations by default (POSIX) and supports -n.
- touch updates mtime on existing files (by rewriting content) and
  accepts -c/-a/-m/-h/-r/-d/-t options.
- find: new expression engine with -iname, -path, -regex, -type,
  -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0,
  -delete, -quit.
- grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C
  context, --include/--exclude, multiple -e, -f scriptfile.
- head/tail: -c bytes, -NUM shorthand, negative/positive line counts,
  proper multi-file headers.
- sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes.
- uniq: -i, -f, -s, -w, -D, and the [input [output]] form.
- wc: -m characters and -L longest line; -c now counts bytes (UTF-8).
- cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter.
- tr: -c/-s/-t, [:class:] character classes, escape sequences.
- sed: real multi-command parser supporting ';', addresses (N, $,
  /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX].
- echo: -n, -e, -E with standard backslash escapes.
- pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking.
- diff: proper LCS algorithm producing correct normal and unified
  output; adds -q, -s, -N, -i, -w, -b, -B.
- env: -i, -u, NAME=VAL assignments; reads the live shell environment.
- date: -u, -d, -R, -I, and a full GNU-subset format specifier set.

Tests for each command were updated to reflect the corrected behavior
(e.g. absolute paths and stdin are now accepted, POSIX error messages).
All 1334 tests pass; typecheck and production build succeed.
This commit is contained in:
Alex Gleason committed 2026-04-26 23:58:13 -05:00
1 parent 258dce35d2
commit 376a982449
52 files changed
+4948 -2803

No files matched your search

+1 -1
View File
@@ -134,7 +134,7 @@ describe('ShellTool (end-to-end)', () => {
});
it('runs pwd', async () => {
expect(await run('pwd')).toBe('/work');
expect(await run('pwd')).toBe('/work\n');
});
it('cd changes cwd', async () => {
+7 -4
View File
@@ -126,7 +126,7 @@ describe('ShellTool', () => {
expect(catCommand).toEqual({
name: 'cat',
description: 'Concatenate and display file contents',
usage: 'cat [file...]',
usage: 'cat [-nbsETAvu] [--] [file...]',
});
// Check that other commands are included
@@ -150,9 +150,12 @@ describe('ShellTool', () => {
});
it('should show stderr output', async () => {
const result = await shellTool.execute({ command: 'cat' });
// With no args, cat reads empty stdin and outputs nothing.
// Use cat on a missing file to trigger an error instead.
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file or directory'));
const result = await shellTool.execute({ command: 'cat missing.txt' });
expect(result.content).toContain('missing file operand');
expect(result.content).toContain('No such file or directory');
expect(result.content).toContain('Exit code: 1');
});
@@ -165,7 +168,7 @@ describe('ShellTool', () => {
it('should execute pwd command', async () => {
const result = await shellTool.execute({ command: 'pwd' });
expect(result.content).toBe(testCwd);
expect(result.content).toBe(testCwd + '\n');
});
it('should handle cd command and update working directory', async () => {
+8 -3
View File
@@ -89,7 +89,8 @@ export class ShellTool implements Tool<ShellToolParams> {
// Register all built-in commands.
this.registerCommand(new CatCommand(fs));
this.registerCommand(new CdCommand(fs));
const cdCommand = new CdCommand(fs);
this.registerCommand(cdCommand);
this.registerCommand(new ClearCommand());
this.registerCommand(new CpCommand(fs));
this.registerCommand(new CurlCommand(fs, this.corsProxy));
@@ -97,7 +98,8 @@ export class ShellTool implements Tool<ShellToolParams> {
this.registerCommand(new DateCommand());
this.registerCommand(new DiffCommand(fs));
this.registerCommand(new EchoCommand());
this.registerCommand(new EnvCommand());
const envCommand = new EnvCommand();
this.registerCommand(envCommand);
this.registerCommand(new FindCommand(fs));
this.registerCommand(new GitCommand({ git: this.git, fs, cwd, signer: this.signer }));
this.registerCommand(new GrepCommand(fs));
@@ -125,8 +127,11 @@ export class ShellTool implements Tool<ShellToolParams> {
fs,
commands: this.commands,
initialCwd: cwd,
initialEnv: { PWD: cwd, SHELL: '/bin/sh' },
initialEnv: { PWD: cwd, SHELL: '/bin/sh', HOME: cwd },
home: cwd,
});
cdCommand.setHomeDir(cwd);
envCommand.envSource = () => this.executor.getVars();
}
private registerCommand(command: ShellCommand): void {