Module ztcl::code
ztcl::code has tools to read, search, edit, and test a source tree. The output of each tool is short, so a coding agent that calls the tools from a shell reads few tokens. ztcl code runs the tools from the command line.
ztcl code 'grep evalScript src -glob *.zig -n 5'
ztcl code 'def runScript
show src/modules.zig runScript'
ztcl code replace src/modules.zig 'try runScript(it, m.script);' 'try runScript(it, m.script); // the module'
ztcl code 'run -ask "Which tests failed?" {zig build test}'
The program
ztcl code 'SCRIPT' # runs the script and prints the result of each command
ztcl code CMD ARG ... # runs one command with the words as the shell gives them
ztcl code < script.tcl # runs the script of stdin
ztcl code claude ?OPT ...? ?-- ARG ...? # runs Claude Code in a harness (see below)
ztcl code turns on file, channels, exec, clock, socket, and env, with the files of the host. Each program of exec gets the environment of the runner. The network policy of socket allows the loopback addresses and the host and the port of ZTCL_CODE_LLM. ztcl code claude also turns on thread, event, and vm, and its network policy also allows the hosts of Anthropic and Claude on port 443, for the guest of -isolation vm.
ztcl code has exec, so a script has the full power of the user. It is a tool for a trusted script on your own tree. It is not a sandbox.
A script runs one command at a time. The program prints the result of each command at once, unless the result is empty or the command sets a variable, defines a proc, or is a loop. An error prints error: MESSAGE after the output of the commands before it and ends the program with the exit code 1. A script is Tcl, so put a shell command line or a text with $ or [ in braces.
In a script of package require ztcl::code, call the tools as ztcl::code grep .... The module needs the capabilities file and channels. run, the file list of git, and the backends claude and codex also need exec. The backend http needs socket and the module ztcl::json.
The output
- A line of a file is
N<tab>TEXT. - A change is
FILE:A-B +ADDED -REMOVED. A-B is the range of the new lines. A change that only removes lines isFILE:A -REMOVED. - A long list stops at a limit. Its last line gives the count of the rest and the option that shows them.
- An error gives the lines for the next try: the places of a text that is not unique, the lines that have the text with other white space, or the first line that differs.
Commands
| Command | What it does |
|---|---|
grep PATTERN ?PATH ...? ?-re? ?-w? ?-i? ?-glob G? ?-c N? ?-n MAX? ?-files? |
Searches the files. PATTERN is literal unless -re. -w matches whole words, -i ignores case, -c N shows N lines of context, -n MAX shows at most MAX hits (60), and -files gives the count of each file. |
def NAME ?PATH ...? ?-glob G? ?-show? |
Gives each definition of NAME in the tree, with its line range. A.b is a definition b inside A. -show gives the lines. |
outline FILE ?FILE ...? ?-full? |
Gives the definitions of each file with their line ranges, indented by nesting. |
show FILE ?SPEC ...? ?-n MAX? |
Gives lines with numbers. A SPEC is A-B, A+N, A (40 lines), -N (the last N lines), A- (to the end), or the name of a definition. At most MAX lines (300) show. |
files ?PATH ...? ?-glob G? ?-n MAX? |
Gives the files of the tree. |
tree ?PATH? ?-depth N? |
Gives the folders to the depth N (2), with the count of files of each one. |
replace FILE OLD NEW ?-all? |
Changes the one place of the text OLD to NEW. -all changes each place. |
edit FILE {OLD NEW ?OLD NEW ...?} |
Makes several changes in one file. Each OLD must be at one place of the file before the changes. One bad OLD stops all changes. |
lines FILE A B TEXT ?-expect T? |
Replaces lines A to B with TEXT. An empty TEXT removes the lines. With -expect, the lines must have the text T. |
insert FILE LINE TEXT |
Puts TEXT before line LINE. LINE end adds TEXT at the end. |
write FILE TEXT |
Makes or replaces a file, and its folders. |
sub OLD NEW ?PATH ...? ?-re? ?-w? ?-i? ?-glob G? ?-dry? |
Changes OLD to NEW in each file. -dry gives the changes and writes no file. |
run ?-keep RE? ?-c N? ?-tail N? ?-all? ?-ask Q? ?-cd DIR? COMMAND ?ARG ...? |
Runs a program and gives short output. |
digest ?-q QUESTION? ?-words N? TEXT |
Gives the answer of a model to QUESTION about TEXT. |
help |
Gives a list of the commands, in 15 lines. |
script TEXT |
Runs TEXT as ztcl code runs it. |
Search
A search reads the files of git ls-files -co --exclude-standard: the tracked files and the new files that git does not ignore. Outside a git tree, or with no exec, it walks the folders and skips each folder whose name starts with a dot, and zig-out, zig-cache, node_modules, target, build, dist, and __pycache__. A file with a NUL byte in its first 8,000 bytes is binary, and a search skips it. A search also skips a file larger than 4 MB unless a PATH names it.
A pattern of -glob with no / matches the name of the file, and a pattern with a / matches the full path. -glob can come more than once, and a value can have several patterns with commas: -glob *.zig,*.tcl.
A line of a result loses the white space at its ends. A line longer than 160 characters shows as a part around the match.
Outline
The outline has rules for Zig, Tcl, Python, JavaScript and TypeScript, C, Go, Rust, shell, and Markdown, by the extension of the file. The end of a definition comes from its braces, from the indent in Python, and from the next heading in Markdown. A brace in a string or a comment does not count. The rules are regular expressions, so a definition in an unusual form can be missing.
Edits
A file is read and written as UTF-8 with no change of its line ends, so a CRLF file keeps its CRs. A binary file gives an error. A text of lines, insert, and write is whole lines: lines and insert add a line end to a TEXT that has none.
run
run runs COMMAND with the error output in the output. One word with a space is a shell command line for sh -c. The first line is exit STATUS, N lines, T s. The output loses the escapes of the terminal, and a line with a CR keeps only its text after the last CR, so a progress line shows once. A run of equal lines is one line with a count, such as ok (x40).
The other lines are:
- after a status of 0: the last 5 lines;
- after another status: the lines that match
-keep RE, with 1 line before and-c Nlines (2) after, and the last 20 lines. With no-keep, the lines with words such aserror,failed,panic, andexpectedmatch.
-tail N sets the count of last lines, and -all gives each line. At most 120 lines show. A run of more than 25 lines writes the full output to a log and gives its path on the first line. The log folder is ztcl-code in the git folder, which git does not track, or /tmp/ztcl-code. It keeps the 20 newest logs. Search a log with grep PATTERN .git/ztcl-code/run-3.log.
The model
digest and run -ask QUESTION send a text to a model and give its answer. run -ask sends the output of a run of more than 25 lines. It gives the first line of the run, with the log, the backend, and the time of the model, and then the answer. A shorter output shows as it is.
The backend is the CLI of the agent that runs the tool, with a cheaper model than the model of the agent:
| Backend | When | Call | Model |
|---|---|---|---|
claude |
CLAUDECODE is set, or claude is on the PATH |
claude -p with its own system text, no tools, no MCP servers, and no saved session |
claude-sonnet-5-5 |
codex |
codex is on the PATH and claude is not |
codex exec in a read-only sandbox, with no saved session |
the default of codex |
http |
ZTCL_CODE_LLM is set, or no CLI is on the PATH |
a POST to a chat endpoint of the OpenAI API, such as llama-server | the default of the server |
| Variable | Value |
|---|---|
ZTCL_CODE_AGENT |
claude, codex, or http: the backend. |
ZTCL_CODE_MODEL |
The model of the backend. |
ZTCL_CODE_LLM |
The URL of the chat endpoint of http, http:// only. The default is http://127.0.0.1:8080/v1/chat/completions. |
A CLI runs in /tmp, so it reads no CLAUDE.md or AGENTS.md of the tree. The backend http turns the thinking of a Qwen model off, and the module removes <think> blocks from each answer.
The system text tells the model to answer with facts of the text only, to copy paths, line numbers, and messages exactly, and to answer "not in the text" when the text has no answer. A model can still make an error, so check a fact in the log before you act on it. A text of more than 240,000 characters goes as its start and its end, with a mark between them. -words N sets the length of the answer (60 words).
Claude Code in a harness
ztcl code claude runs Claude Code in an isolated thread and shows what it does. The screen of Claude Code is on the left, and a panel on the right shows the model, the tokens, the tasks, the files that it changed, and each tool call with its result.
ztcl code claude # a new session
ztcl code claude -- --continue # each word after -- goes to Claude Code
ztcl code claude -isolation interp -- -r # no sandbox, and the picker of sessions
ztcl code claude -write ~/notes -- # one more path that Claude Code can change
ztcl code claude -isolation vm -- # Claude Code in a guest of ztcl::vm
| Option | Value | Default |
|---|---|---|
-isolation process|interp|vm |
process runs the thread in a child process in the sandbox of the system (Isolated threads). vm runs Claude Code in a guest of ztcl::vm with its own Linux kernel (see The guest). interp runs it in this process, with no sandbox. |
process |
-write LIST |
More paths that Claude Code and its programs can change. | none |
-program PATH |
The program, in place of claude on the PATH. |
claude |
-mcp 0|1 |
The MCP server ztcl (ztcl debug --mcp of this runner) for Claude Code. |
1 |
-skills 0|1 |
The skills ztcl-dev and ztcl-apps, and a short text about the harness at the end of the system prompt. |
1 |
-kernel F, -initrd F |
The guest of vm. The defaults are ZTCL_CODE_VM_KERNEL and ZTCL_CODE_VM_INITRD, or the image ztcl-guest. |
|
-memory MB |
The memory of the guest of vm. |
2048 |
-share LIST |
More folders of the host in the guest of vm, at the same paths: PATH or {PATH rw}. |
none |
Ctrl-] shows or hides the panel. Each other key goes to Claude Code. A window narrower than 100 columns has no panel. At the end, the program prints the exit status, the id of the session, and a command that resumes it.
The sandbox
At the level process, Claude Code and each program that it starts can read each file of the user, and can change only these paths:
- the project: the top of the git work tree, or the current folder;
- the state of Claude Code:
~/.claude(orCLAUDE_CONFIG_DIR),~/.claude.json, and~/.local/share/claude; ~/.cache,~/.local/state,/tmp,/var/tmp,TMPDIR, andXDG_RUNTIME_DIR;- each path of
-write.
A write to another path gets "Permission denied", and Claude Code sees the error as the result of its tool. On Linux the thread also has its own PID namespace and a cgroup. On Windows the thread has a Job Object, and no write list yet. The harness sets DISABLE_AUTOUPDATER, because the folder of the program is read-only.
The sandbox limits the files. The network, the credentials of Claude Code, and the programs on the PATH stay in reach, so a tool call can still send data out. A program can also ask a service of the host, such as the session bus or an ssh agent, to change files outside the list (Isolated threads). Use -isolation vm to keep the services of the host out of reach. Use -isolation interp only for a project that you trust.
The kit
Claude Code in the harness gets these parts, unless -mcp 0 or -skills 0:
- the plugin
ztclin~/.cache/ztcl/claude-plugin(--plugin-dir), with the skillsztcl-dev(work on ztcl and ztk: the commands, the memory cap, andztcl code) andztcl-apps(scripts on ztcl and apps on ztk, and how to check them); - the MCP server
ztcl, which isztcl debug --mcpof the same runner (--mcp-config), with the toolsztcl_run,ztcl_explain,ztcl_records,ztcl_eval, andztcl_disasm; - a short text at the end of the system prompt (
--append-system-prompt) that names the harness, the sandbox, the skills, and the MCP server.
The harness writes the kit again at each start.
The guest
-isolation vm runs Claude Code on a PTY of a new guest of ztcl::vm, with ztcl::pty spawn -isolation vm, on x86-64 Linux with KVM. The guest has its own kernel, and it gets only these folders of the host, each at its path of the host:
| Folder | Mode |
|---|---|
/usr |
read-only |
| the project | read-write |
~/.claude (or CLAUDE_CONFIG_DIR) |
read-write |
the folder of the claude program |
read-only |
each folder of -share {PATH ...}: PATH is read-only, and {PATH rw} is read-write |
as given |
| the folder of this runner, for the MCP server, when no other share has it | read-only |
HOME and the state folder keep their paths, so a hook or an MCP server of your settings that names a shared path works, and the transcripts go into the same folder as a session on the host. A folder inside another shared folder needs no share of its own. A guest with the network has room for 5 shares (4 with the image ztcl-guest, whose root is a disk). When no share is free for the runner, the guest has no MCP server, and the panel says so.
The network of the guest is the NAT of ztcl::vm, and it reaches only the hosts of Anthropic and Claude on port 443. Claude Code in the guest keeps its own copy of ~/.claude.json as ~/.claude/.claude.json, which the harness copies from the host file the first time. A hook or an MCP server of your settings that names a path outside these folders fails in the guest: give its folder with -share. The signed image ztcl-guest has no PTY in its agent yet, so the level needs -kernel and -initrd of tools/vm-guest/pack.sh until the next image release. Claude Code in a guest has the design and the measurements.
How it works
- The thread starts
claudeon aztcl::ptyand keeps its output bytes. It has nothreadcapability, so it cannot send a script to the UI. - It reads the transcript of the session,
~/.claude/projects/FOLDER/SESSION.jsonl, and turns each line into an event: a prompt, a tool call, the result of a call, a text, the tasks, or the usage of a reply. The id of the session comes from~/.claude/sessions/PID.jsonof its own child, so--continue,--resume, and/clearfollow the right file. - The UI calls
takeon the thread withthread::send -asyncand gets the bytes and the events as data. It keeps the screen in aztcl::vtand draws the rows that changed.
The module ztcl::code::claude has the parts for another UI: start ?OPTION ...? ?-- ARG ...? gives a handle, take H CMD calls CMD DICT with the dict out, events, and status, input H BYTES and resize H COLS ROWS go to Claude Code, and stop H ends it. panelNew, note, and panelLines keep the state of the panel. ztk uses them with a ztk::terminal widget.
Errors
Each error has a code that starts with ZTCL CODE: USAGE, NOFILE, NOTFILE, BINARY, NOMATCH, AMBIGUOUS, OVERLAP, RANGE, EXPECT, NONAME, LANG, PATTERN, CAPABILITY, and LLM.