Tools: images, counting, monitors and jobs

Part of the Workflow Modes reference.

Images: generate_image

generate_image makes an image with an image model and saves it in the workspace (#481):

generate_image { prompt: "A red fox in the snow, watercolor",
                 path: "assets/fox.png",
                 images: ["assets/sketch.png"],        // optional references
                 model: "vertex-gemini/gemini-3-pro-image" }  // optional

The result carries the saved path(s), the cost, any text the model said, and the image itself, so the agent can look at what it made and iterate by passing the saved file back as a reference. The usage goes on the result, so pi counts it in the session’s spend and the spend cap sees it.

  • The model. Any registered image model (vertex.adoc#image-models). The default is vertex-gemini/gemini-nano-banana-2.1; set imageModel in your modes overlay to change it. An unknown model is refused, naming the ones there are.

  • Where it writes. Inside the workspace by real location, through .. or a symlink as a write’s location is decided. Never a credential-shaped name, never under .git, and never over an existing file: the path as given is checked before the paid call. A path with no extension takes the image’s own; several images are numbered -1, -2.

  • Its failures are errors the agent reads (Google’s safety refusal included), and nothing is saved.

  • Where it is offered. It has an allow rule in auto, plan and manual, but is not in plan mode’s tool list, since plan changes nothing. No packaged helper is given it.

Codemode’s models.generateImages() reaches the same models from a script, where codemode is enabled.

Counting: count

count gives exact regex match totals over a file or a tree, with a per-file breakdown 20 files at a time. It exists because a model tallying grep output by eye is reliably wrong. It is read-only, with caps on files visited and bytes read, so read-only helpers carry it too.

  • Per match, not per line: deny deny on one line counts 2.

  • ^ and $ anchor at every line, as grep’s do, for LF and CRLF line endings alike (#463). They once anchored only at the file’s start and end, so ^== ` in a changelog counted 0. JS regex has no inline `(?m), so no pattern could ask for the line form.

  • A pattern that matches the empty string is refused when it could match between characters (a*, ^|a*): the number would mean nothing. One held to a line’s edges is a line count, as grep -c gives it (#528): a bare ^ or $ counts lines, blank ones included, and ^$ counts blank lines. A last line without a newline counts, and nothing past a final newline does. literal: true searches for the text as written.

  • Its path reads as pi’s tools read theirs (#537): ~ is the home (the remote’s in a session over SSH), and on Windows /d/x is D:\x. The name glob ignores case where the walked filesystem does, on a local Windows or macOS disk, so *.TXT finds a.txt there. A remote is POSIX and keeps case.

The monitor: any source wakes the agent or acts for it

The agent can watch any process, bus or data source in the background, for as long as the operator allows (#446). It does this with the monitor tool. A monitor runs a command; each line on its stdout is an event. A line that matches, or a silence that lasts, either wakes the agent with the instructions it left itself, or runs a pre-approved action unattended.

The listener is deterministic. The command is whatever was given: tail -F, a curl stream, a while … sleep 30 polling loop. No model watches it. The tool returns as soon as the command is running. The agent goes on with its turn, or sits idle, until an event arrives.

Nothing here knows about any particular transport. ntfy, sockets, message buses, CI and other sessions are all recipes: anything that can print a line is a source.

monitor, monitor_stop and monitor_list are offered in auto, manual and yolo; monitor_restore is declared only when the session’s branch holds monitors a restart ended that have not yet been restored (#1043), and is re-evaluated on /tree branch switches (#1044). Plan mode does not offer them, since plan designs and the acting mode monitors. No helper is given them either: helpers yield, and the parent monitors. Every monitor stops when the session ends, its whole process group with it.

A resume says which monitors it ended (#460). The registry lives in memory, but the agent’s context still holds the messages that started its monitors after a restart, a resume or a session switch. An agent that set a watcher as a precondition would go on acting on it. So each start and each delivered end (stopped, timed out, exited, flooded, failed) is a session entry, and pi replays those on resume. A session that starts with monitors started but never ended gets one message, never a user turn. It lists each lost monitor’s id, description and command, and says nothing watches for them now. It is told once; a fresh session hears nothing. Nothing is restored silently. monitor_restore (#629) starts the monitors the notice named again in one step, all of them or the ids given, from the parameters their starts recorded. Each is judged afresh as it starts, as every monitor start is, and the answer says, per monitor, whether it started again (and under which new id) or why not: the gate refused it, the notice did not name it, or an older pi started it and recorded no parameters. A restored monitor is not offered again. A loop that must survive the session itself, such as a merge train, belongs in a job (Jobs: a command pi runs to its end, across sessions), which a restart does not end.

The tools

monitor parameter Type Meaning

command

string

The source, run in the bash tool’s shell. Each stdout line is an event; stderr goes to a file; exit ends the monitor.

description

string

Names the monitor in every message and in monitor_list.

instructions

string?

The agent’s standing instructions to itself (≤ 2 000 characters), so the woken turn knows its job hours later and after compaction. They go with the monitor’s first event and again with its first event after a compaction; any other event says standing instructions: as sent with an earlier event of m3 (#889).

match

string?

A JavaScript regular expression. Only matching lines are events; the rest are written to the stdout file and counted as ignored.

silence_ms

number?

Wake with no event for N s when nothing has arrived for this long (at least 1 000). It fires again after each silence, until the next event or a stop.

on_match

string?

A command run unattended on each event (Actions).

wake

boolean?

Whether an event wakes the agent. Default true, or false when on_match is given.

max_line_chars

integer?

Keep lines up to this long: 500 by default, at most 4 096, which a JSON event stream such as ntfy’s wants (Limits, below).

timeout_ms

number?

How long it runs; default monitor.defaultTimeoutMs (5 minutes), at most monitor.maxTimeoutMs. For longer, persistent: true (with restart: "never" for a source that should run once), where the operator allows it. The tool’s description states the session’s own numbers, and so does the refusal of a longer value (#473).

persistent

boolean?

Run until the session ends. Refused when the operator sets allowPersistent: false under a cap.

restart

"never" | "on-exit"?

Start the same source again when it exits, with backoff ([monitor-restart]). Default on-exit for a persistent monitor, otherwise never.

monitor_stop takes an id and kills the source’s group, and any running action. An event reaches pi as a steer, which pi queues during a turn and injects after the current tool call. So an event sent just before the stop can arrive after the stop’s reply, and the reply says so (#529). monitor_list shows each running monitor’s status, its counts (events, ignored, suppressed, actions run, failed and dropped), its exit code and its three files. Ended monitors get a line each (status, end time, exit), and only the last five are listed, with a count of the rest. A monitor pi ended (stopped, timed out, flooded) shows no exit code, exit -, on every host. The code its source closed with then answers pi’s own stop, and on Windows that would be Cygwin’s 3840 (#560). all: true lists every monitor in full (#530). It also shows the latest line the source printed, matched or not, clipped to 200 characters, with when it was read (#474). Counts prove a monitor is reading, while that line shows where its job has got to. The files live under <agentDir>/gadhs-pi-monitor/ (0700), each 0600 and named for the session’s registry and the monitor (<token>-<id>, the token 6 hex characters): <token>-<id>.stdout, <token>-<id>.stderr and <token>-<id>.actions.log. Nothing is replayed from them; the agent reads them with read when it wants history.

Both commands are bash

The source and the action are each decided exactly as a bash call would be, once, before anything runs. They meet the same rules, the safety valve, the high-consequence list, the denial threshold, the judge, the pending queue and the phone. The judge is told that an action runs unattended on every event, with the event text in a variable. A deferral that would have reached the permission system’s own dialog is asked through ours, with the command shown. A refusal of either command refuses the whole monitor, and nothing is spawned. In manual mode you approve both. In auto mode an undecided command waits in /pending, and the agent calls monitor again once you approve it.

Both commands run in the shell pi’s bash tool runs, so a command runs where it was judged to run (#451). pi chooses that shell (getShellConfig) in this order:

  1. the shellPath setting;

  2. on Windows, Git Bash in its install locations;

  3. bash on PATH.

The monitor also uses the shellCommandPrefix setting, pi’s command transport, and pi’s bin directory on PATH.

A monitor runs Git Bash, as the bash tool does: its stop reads Git Bash’s process group, below.

There is one deliberate difference from the bash tool. On Windows a command is never passed as an argument, because Git Bash halves the backslashes in one (#450). Instead the monitor writes it to a 0600 script file next to its logs (<token>-<id>.source.sh, <token>-<id>.action.sh) and runs that. The WSL launcher keeps pi’s stdin transport. A project’s settings count only when the project is trusted, so an untrusted repository cannot choose the binary.

On Windows a monitor’s or a job’s shell starts with PYTHON_BASIC_REPL=1 unless you set it, as the bash tool’s commands do (#713, #711). Its stdin is the NUL device, which Windows calls a terminal, so a python - with nothing to read started Python 3.13’s new REPL, which loops on NUL printing tracebacks, into a job’s log until the disk filled. The old REPL reads end of file and exits.

Stopping on Windows signals the command’s MSYS process group (#553). Under Git Bash a command’s children are fork-and-exec processes whose Windows parent is a fork stub, not the shell. So taskkill /T on the shell left them running: a stopped loop’s sleep, and every monitor’s curl stream after a /reload. Now the script’s first line records the shell’s group beside it (<token>-<id>.source.sh.pgid, through MSYS’s /proc/<pid>/pgid). A stop sends that group TERM with Git’s own usr/bin/kill.exe, then KILL after the grace, waits up to 250 ms for the launcher wrapper to exit on its own, and falls back to an identity-verified PowerShell tree stop only if the launcher is still running (#928). A shell that is not Git’s uses the verified tree stop alone. While the group is not yet written, the TERM sends nothing, and the KILL after the grace kills the group, written by then in the usual case, and stops the launcher only if it remains alive (#918, #928). Once the launcher has gone, the recorded group is killed again, and once more 200 ms later: a group KILL that lands while bash is forking misses the child it was making, which joins the group a moment later. The fallback never kills a raw PID with taskkill: it opens the launcher’s OS process handle (locking the PID against reuse while held), checks both the shell image name and StartTime ⇐ startedAtMs + 100 ms, walks descendants only when each child’s CreationDate is not earlier than its parent’s (closing Windows' stale ParentProcessId reuse hole), verifies each descendant’s live StartTime, and terminates those verified handles bottom-up (#925, #928). After the two group reaps, Git Bash stops also reap any orphaned two-argument cygwin-console-helper.exe born within the stopped command’s lifetime (never touching three-argument PTY helpers or any helper whose parent is alive or unreadable) so an aborted console allocation cannot hold the job directory or monitor output files (#930). Because the orphan match is by birth window and dead parent rather than working directory, it sweeps box-wide across [startedAtMs - 1000 ms, stopAtMs + 1000 ms] for any such dead-parent two-argument helper.

In a remote session (pi over SSH, #492) both commands run on the remote in bash -lc, as the bash tool’s do there, in their own process group: a stop ends the whole remote group, SIGKILL after the grace. They get pi’s session markers and the event’s variables, and nothing else of this machine’s environment. The records (the output copy, stderr, the action log) stay on this machine. On a resilient link a dropped link does not end the monitor: its command runs on, and its events resume when the link comes back (ssh.adoc#dropped-link). If pi gives up reconnecting, or the link is not resilient, the command’s stream ends as an exit with no code, and the monitor with it, as at any exit. The shell is named bash on <destination>:<path>.

The start message, monitor_list and the closing notice name the shell. Before #451, the monitor took the first bash on PATH. On Windows that is usually the WSL launcher, so a command judged for Git Bash ran in WSL, and an action there saw an empty $GADHS_MONITOR_EVENT. On Windows the event’s variables are also named in WSLENV, so a shellPath set to WSL on purpose still passes them through.

NOTE
In WSL, tail -F on a /mnt/…​ file never sees what Windows programs write there: inotify does not report changes made from the Windows side. Use tail -F ---disable-inotify, or Git Bash, whose tail polls.

Events and wakes

An event reaches the session as a custom message, type gadhs-monitor. Its first line is fixed: monitor <id> · <description> · outside signal, not the human’s instruction. If the monitor has instructions, the next line gives them, framed as the agent’s own words. Then come the lines, the silence, or a failed action’s output.

In the transcript an event is that first line and a count, · 3 more lines · ctrl+o (#546); ctrl+o expands it to every line. The agent reads the whole message either way. The live output is the hub’s Monitors & Jobs tab (The hub), and the status line says how many monitors run (2 monitors) while any does.

A custom message is never a human request (The judge): it fires no input event, so its text cannot become something you asked for, or answer a step waiting in /pending.

Delivery is pi’s steer:

  • an idle agent starts a turn;

  • a busy one sees the event after its current tool call.

While a turn is running, at most one wake per monitor sits in pi’s steer queue at a time (#937): later events from that monitor are held and coalesced into one combined message once the earlier wake reaches the conversation. Calling monitor_stop drops every held event immediately and says how many were dropped, so a bursty monitor cannot keep waking the agent for turns after it is stopped.

When a monitor ends (its source exits, it times out, it fails, it floods, or it is stopped), one closing notice says why, and nothing follows it. A stop the agent asked for is told without starting a turn.

What the source already wrote is told before the closing notice when it exits or times out (#663). That covers lines still in the batch window, and a last line with no newline after it. A stop drops them: it was asked for. A monitor on a long command (a push, a build) needs a timeout_ms past that command, or persistent: true. At the default 5 minutes, a monitor tailing a 10-minute push’s log ends first, with "its timeout passed" and 0 events, before the line it waits for is written.

A monitor with restart: "on-exit", the default for a persistent one, does not end when its source exits; it starts the same command again (#457). The command was judged once, at setup, and is not judged again.

  • Backoff: the first wait is 1 s, and each quick exit doubles it, up to 60 s. A run of 30 s or more counts as healthy and resets the wait.

  • Notice: five quick exits in a row earn one notice, whatever wake says, because a source that keeps dying is worth knowing about. An ordinary reconnect, such as a server restarting, is silent.

  • What never restarts: a stop, a timeout, a flood, or a shell that is no longer there to start the source. A source that prints its signal and exits 0 when done should set restart: "never" (a persistent monitor otherwise restarts it on every exit, #937).

  • Counting: monitor_list shows the restarts.

Actions

on_match runs in the source’s shell (the bash tool’s; a script file on Windows, bash -lc on a remote) in the session’s directory, with the session’s environment and four more variables:

  • GADHS_MONITOR_EVENT: the event’s text;

  • GADHS_MONITOR_ID;

  • GADHS_MONITOR_DESCRIPTION;

  • GADHS_MONITOR_CAUSE: output or silence.

The event is never written into the command line, so text from the source cannot run as shell.

Actions run one at a time per monitor:

  • While one runs, up to 100 events queue. Beyond that they are dropped and counted.

  • Each run is bounded by monitor.actionTimeoutMs (60 s). Past that, its process group is killed.

  • Output goes to the actions log.

  • A failed run always wakes the agent, whatever wake says: a non-zero exit, a timeout, or one that could not start. The wake carries how it failed and the run’s last lines.

  • When the source exits, the actions it is still owed run before the closing notice. A source that prints passed and exits still gets its action.

Limits

A chatty source cannot buy a turn, or an action, per line. These numbers are pi-cc-monitor’s (MIT; the stream and process code is ported from it, with attribution):

What Value Effect

Batch window

200 ms

Lines within it are one event.

Line / event

500 / 3 000 chars

Longer is cut, with …(truncated). max_line_chars keeps lines up to 4 096, ntfy’s inline limit, so one JSON event is read in one wake; the event’s cap rises with it (#527). The event is cut after match has chosen its lines, so a matching line is never lost behind other output in the same window (#624).

Burst, then steady

10, then 1 per 2 s

Events over that are suppressed; the next delivered one says how many.

Flood

30 s

Suppressed this long: the monitor stops, saying so.

Unended line

1 MiB

A line that never ends keeps its last megabyte.

Monitors

8 (maxMonitors)

Running at once per session; the next is refused.

Configuration

The monitor block in the modes overlay (the cookbook):

Key Default Meaning

maxTimeoutMs

3 600 000

The most a timeout_ms may ask; 0 is no bound.

defaultTimeoutMs

300 000

When timeout_ms is omitted; refused at load above the cap.

maxMonitors

8

Running at once per session.

actionTimeoutMs

60 000

One action run’s bound.

allowPersistent

true

false refuses persistent unless the cap is 0.

Recipes

Watch How

A log

command: "tail -n0 -F deploy.log", match: "ERROR|FATAL"

CI

command: "glab ci status --live"

A polling check

command: "while true; do curl -s https://svc/health; sleep 30; done", match: "down"

A watchdog

a heartbeat source with silence_ms: the wake is that it stopped

ntfy

command: "curl -sN https://ntfy.example/topic/json?since=30s", match: '"event":"message"', persistent: true: it reconnects when the server restarts; to send, a bash curl -d "text" https://ntfy.example/topic

A message bus

mosquitto_sub -t alerts/#, nats sub alerts, redis-cli subscribe alerts, kafka-console-consumer --topic alerts

Kubernetes, containers, the system

kubectl get pods -w, docker events, journalctl -f -u myservice

A socket

nc -lk 127.0.0.1 9000, websocat ws://host/feed

Another session on this box

one monitors tail -n0 -F ~/.local/state/gadhs/inbox-alice; the other appends a line with bash

Forward alerts somewhere

on_match: 'curl -sd "$GADHS_MONITOR_EVENT" https://ntfy.example/me', judged once

WARNING
A source anyone can write to is a way to put text in front of the model: a public ntfy topic, an inbox file another account can append to, a socket on 0.0.0.0. Treat a topic name as a credential. Use a self-hosted server with tokens, and keep inbox files in your own state directory. The event is still an outside signal, never your instruction, and never shell. But the model reads it.

Jobs: a command pi runs to its end, across sessions

A job is a command pi starts in the background and tracks until it ends (#577). A release, a long build or a battery run are examples. A monitor watches a command’s output and ends with its session. A job is different: it outlives its session and pi itself. The agent is woken when it ends, and any later session on the box can list it, see how it ended and stop it. Before jobs, an agent detached such a command with nohup, setsid or a trailing &, and pi lost sight of it.

job, job_stop and job_list are offered in auto, manual and yolo. Plan mode does not offer them, and neither do helpers, as for the monitor.

The hub’s Monitors & Jobs tab lists every job on the box beside the session’s monitors, and s there stops one. While a session watches running jobs, the status line counts them: 2 jobs.

The tools

Parameter Type Meaning

command

string

The command, run to its end in the bash tool’s shell.

description

string

Names the job in every listing.

cwd

string, optional

Where it runs. Default: the session’s directory. A relative path is taken against it. In a remote session a path that is absolute on the remote (/srv/app/sub) is the remote’s, from a Windows client too (#998); one outside the remote workspace is read on this machine.

match

string, optional

A regular expression. Each line of the job’s log that matches is told as it arrives.

wake

boolean, optional

Whether the job’s end and its matched lines wake the agent. Default: true. With false, they are told without starting a turn.

max_line_chars

integer, optional

Keep matched lines up to this long, as for a monitor (default 500, at most 4 096).

The command is judged as bash once, before anything runs, exactly as a monitor’s source is. When cwd differs from the session’s directory, the gate evaluates the command in that target directory (cd <cwd> && <command>) so out-of-workspace targets never take session-scoped rule or plain-line allows and the judge sees where the command will run; locally the job always uses the session’s own shell settings rather than reading .pi/settings.json from cwd (#1022). A command the gate refuses never starts. job answers at once with the job’s id (j1, j2, …​), its pid and its log.

job_list lists every job on the session’s host (this box, or in a remote session the remote), running ones first. Each line gives the state (running, exit N, stopped or lost), how long it has run or ran, its pid, its directory, and its log with the log’s size. A job that writes a lot should send that output to its own file, since its log grows for as long as it runs.

On Windows a job can also read as stuck: never started its script (120 s) (#626). A Git Bash process can hang as it starts, before its script’s first line, while its pid stays alive. That first line writes the job’s MSYS group file, and it is the job’s start marker: a running job whose marker is still missing 120 seconds after it started reads as stuck. The grace was 30 seconds until #687. On a box at 100% CPU, one Git Bash start took longer than that, and a job that finished normally was told stuck (#685). A false stuck misleads the agent, while a stuck start told 90 seconds later costs little. The seconds count from when the spawn returned, so they start late, which is the safe direction. Stuck is read afresh each time and never recorded, and it is not an end. If the script starts late, the marker appears and the job is simply running. job_stop ends a stuck job as it ends any running one. Only a job whose script writes the marker can read as stuck: Windows' file transport with Git Bash.

job_stop ends a running job’s whole process group: SIGTERM, then SIGKILL two seconds later if anything in the group is still there. The job is recorded as stopped before the signal, so no look in between takes it for lost. Its end is told without starting a turn, as a stopped monitor’s is.

On Windows the group is the job’s MSYS group. job_stop sends it SIGTERM with Git’s own kill. If the job’s launcher is still running two seconds later, it sends the group SIGKILL and ends the launcher’s tree with taskkill /T /F. A stop before the script has recorded its group sends no SIGTERM and waits out the two seconds, as above (#918), so job_stop answers about two seconds later there. Once the launcher is gone, the stop kills what is left of the group the script recorded, and again 200 ms later (#920, #918): a stop in the job’s first moments, before the script’s first line had recorded its group, could only reach the launcher, and a fork the script started a moment later ran on, as a monitor’s did before #603. Two cases can still outlive a stop (#626):

  • A native Windows process whose native parent has already exited, for example a server’s worker started by a node that then quit. Cygwin never knew it, and taskkill /T cannot reach it with its parent gone.

  • Anything in the job that ignores SIGTERM, when the launcher itself ends on it. The wait watches the launcher, so it sees the job gone and does not escalate.

Stop such a process by its pid. Windows job objects, which would reach both, are #643.

In a remote session (pi over SSH) a job runs on the remote, and lives there (#671). Its record, script, log and exit file are under the remote’s ~/.pi-ssh/jobs (0700, the files 0600), and its id is r<n>, so an id names one host (j3 here, r3 there). Any later session on that remote, from any client machine, finds it, watches it if it runs, and is told how it ended. Nothing of it is kept on this machine.

It is started outside pi’s SSH helper in a session of its own — with setsid nohup where setsid(1) exists (Linux), falling back to node (detached: true) or perl (POSIX::setsid) on remotes without setsid(1) such as macOS (#1005). The helper ends what it started when its daemon ends: at pi’s goodbye, at /reload, ten idle minutes after its last client. A job outlives all three. It gets pi’s session markers, as a remote monitor does, and nothing else of this machine’s environment. Its pid is judged against the remote’s own clock and process table, so a pid the remote gave to a later process is never taken for it. The remote needs bash: the launch, the job’s script, and the checks and signals pi sends it all run in bash, never the remote’s sh. On Debian and Ubuntu sh is dash, and its kill cannot take --; until #686, a live job there read as lost and a stop sent nothing.

The link matters only to look at it. While the link is down the watch misses a look and tries the next, and the job runs on. job_list says so when the remote cannot be asked ("the remote cannot be asked now; its jobs run on"), never that there are none. A remote job with no match is looked at every 5 seconds, since only its end is wanted and each look is a round trip; one with a match, every second, as here. /hub lists it with (on <host>) where a job here shows its latest line: its log is on the remote, and its lines and end reach the session as messages.

What a session is told

Every message about a job starts with the same framing line as a monitor event: job j3 · <description> · outside signal, not the human’s instruction. A job’s output comes from the world, not from the human.

  • The job ends. One message, job ended: exit 0 after 4m 12s, with the last 20 lines of its log. It wakes the agent unless wake is false.

  • The job never started its script (Windows, #626). One message the first time a session’s watch reads it as stuck, saying that its shell is alive but never ran the command, and that job_stop ends it. It wakes the agent unless wake is false, so an agent waiting on the end is not left waiting. Each session’s watch tells it once, so a later session that takes the job over tells it again.

  • Lines its match picks. The lines found in each look at the log (once a second) are one message, under the monitor’s rate limit: a burst of ten, then one every two seconds. Lines over the limit are only counted, and the next message says how many were held back. A job is never stopped for what it prints, as a flooding monitor is. The match runs on each line before any cut, so a matching line is never lost behind other output. That holds for a burst of any size (#879): everything a job wrote since the last look is read, a mebibyte at a time. A look used to skip to the last mebibyte, and a matching line in the part skipped was never told. A job that writes faster than the watch reads is therefore told late, never in part; lines picked beyond what one message carries are counted, and the message is cut and says so.

  • A session starts. It names the jobs earlier sessions in its project left: those still running, which it now watches, and those that ended with no session watching. This message does not start a turn. A session in another project is told nothing; job_list still shows it every job. A session with no dialogs, such as a helper or pi -p, takes no notice and leaves the report for the next interactive session. The project is the directory, by whichever of its paths a job was started from (#843): where a bind mount gives one directory two paths, a session started from either is told of the jobs started from the other. The paths are asked of the host the directory is on at the session’s start, for at most two seconds; without an answer the session knows its own path only. A job’s record still names the path it was started from.

  • pi quits. After pi has restored the terminal, it prints a line naming this project’s jobs that are still running. The jobs go on. This happens only for a quit in the terminal, not for a reload, a session switch or RPC mode.

A job’s end is told once. Whichever session sees it first tells it and marks it told, and no other session tells it again.

How a job is found again

Each job has its own files under <agentDir>/gadhs-pi-jobs/:

  • j<N>.json, the record. Its id is claimed by creating this file exclusively, so two pi processes starting jobs at once never take the same id or lose each other’s record.

  • j<N>.log, created 0600.

  • j<N>.exit, the exit code.

  • j<N>.sh, the script, on Windows.

The job’s own script writes the log and the exit code. It runs the command in a subshell, then writes the code, so an exit, an exec or a set -e abort in the command still leaves its code. How a job ended therefore reaches disk whether or not any pi is running to see it.

Every listing settles each job from disk first:

  • An exit file means the job exited with that code.

  • Otherwise, if the shell’s pid is alive and is still the process that was recorded, the job is running.

  • Otherwise the job is lost: it was killed, or the host restarted.

"Still the process that was recorded" is the link lock’s check (#561): the pid’s process must have started no later than a second after the job’s recorded start. A pid the host gave to a later process, which Windows does within minutes, is not mistaken for the job. Neither is a process whose start this user may not read.

How long a job is kept

A job stays listed after it ends until a session in its project has been told how it ended, and then for a week (#623). After that, the next listing removes it: its record, log, exit file and script all go. A job no session was told of yet is kept for the next one. Ids are never reused: the highest id claimed is recorded beside the records, so a new job after a pruning still gets the next number.

The detach hint

When a bash command the agent ran puts a process out of pi’s reach, the agent is told once that job would track it. The hint comes after the command has run. It never blocks or changes anything; it only reads the result. A command puts a process out of reach when its parse holds any of:

  • a statement ended with &;

  • a disown;

  • nohup or setsid among a command’s wrappers, including behind another wrapper (timeout 600 nohup ./run.sh).

An && list, 2>&1, &>, |&, arithmetic &, and an & in quotes or a heredoc do not count. Neither does a line pi could not parse, or one with a syntax error: the hint is never given from a guess.

The hint is given once per session for each distinct command. It stops for the rest of the session once the agent has used job.

The poll-loop hint

When a bash command the agent ran polls with sleep inside a for, while, or until loop (loop-sleep) or sits on a bare sleep longer than 30 s (long-sleep), the agent is told once per shape per session that monitor wakes on a matching line and job tracks a command to its end (#741). Like the detach hint, it rides on tool_result, never blocks or alters the result, ignores quoted text, heredocs, and unparseable lines, and logs poll_hint.shown in diagnostics so its effect on poll-loop counts can be measured.

A default timeout for shell calls

pi’s bash and powershell take a timeout in seconds, with no default. A call that omits it has no bound, and a headless session has nobody to end it: eight workers each sat 13-17 hours on one call (#711). Set GADHS_PI_SHELL_TIMEOUT_S to a whole number of seconds, and a shell call that passes no timeout gets that one (#712). It covers pi’s own powershell and every bash the distribution builds, a helper’s included, since helpers share the session’s environment. A call that names its own timeout keeps it, and the human’s ! is untouched. When a defaulted call times out, its result adds that the session’s default applied and that long work belongs in a job. A value that is not a whole number of seconds is reported at session start, and no default applies. Unset, nothing changes, so an interactive seat leaves it alone; a dispatcher of headless workers sets it (gesso’s uses 900).

Edit this page · latest