Plan: jobs - processes pi starts, tracks across sessions, and wakes on

Closes: #577, by U4’s MR (U1-U3 Relates to #577; U5 is pi-win’s, on its own issue). Relates to: #561 (the pid and start-time check this moves), #553 (process groups on Windows), #546 (the status line’s monitor count), #545 (the hub). Branches: feat/jobs-registry, feat/jobs-across-sessions, feat/jobs-hub, feat/jobs-detach-hint, in order; then pi-win’s. Status: Done (2026-11-01) — U1-U4 in 0.143.0, U5 (Windows: the stuck rule, the suites on Windows) by pi-win; Windows job objects deferred to #643

Erratum (2026-10-31): the job’s script redirects its own output, exec >> <log> 2>&1 as its first line, and spawnInShell is exported with its stdio widened to take "ignore", not given the log’s descriptor: a descriptor handed to the child loses writes under MSYS on Windows (#555), and a pipe into pi would close when pi exits.

Erratum (2026-10-31): the command runs in a subshell, ( <command> ); echo $? > <exit file>, so an exit, an exec or a set -e abort ends only the subshell and the exit file is still written. Only a kill of the whole group writes none, and that is recorded as stopped (by job_stop) or lost.

Erratum (2026-10-31): the registry is one record file per job, <id>.json, its id claimed by an exclusive create, not one jobs.json with a counter: two pi processes writing one file at once lose an entry without a lock. pgid is not stored (the pid off Windows; the script’s MSYS group file on Windows), and no separate startedAt is read: the wall clock once the shell runs is the since the check judges against, as the link lock’s is, since the Windows start query blocks for 1.5 to 2 s (#570). The rule itself moves too, as pi-workspace sameProcess, which holderAlive and the registry share.

Erratum (2026-10-31): the log tail and the match, wake and max_line_chars parameters arrive with U2, which uses them; U1 starts, lists, settles and stops.

Erratum (2026-10-31): a stop waits on the whole group, not the shell’s pid: the command runs in a subshell, and a shell that dies on SIGTERM leaves a subshell that ignores it running.

Erratum (2026-10-31): U2’s watch (job-watch.ts) reads disk only, each second: the log, the exit file and the record, the same way for a job this pi started and one found at a session start; it uses no child-process event. It splits and matches the log’s lines itself rather than through the monitor’s LineBatcher, whose event cut comes before the match and drops a matched line behind 3 000 characters of others (#624).

Erratum (2026-10-31): in U3 a job keeps its own row and detail (jobRow, jobDetail in hub-views.ts), not a MonitorSummary from jobSummary: a monitor’s event, restart and action counts and its actions file are not a job’s, and lost is not a monitor status. The tab’s items are either kind, and s on a job picks stop-job. The status count is the jobs the session’s watch follows.

Erratum (2026-10-31): two defects of U1 and U2 found in U3 and fixed there: a stop records stopped before it signals, since a look while the group died recorded the job lost; and a stopped job’s end is told without a turn, as a stopped monitor’s is.

Design

What the operator decided (2026-10-30, 2026-10-31)

A jobs layer, planned first: a job tool whose process pi starts and records in a registry under the agent dir (pid, process group, command, log, start time); it wakes the agent on exit and optionally on matched lines; it survives /reload and a new session and is re-found by pid plus start time (#561’s check); the hub’s Monitors tab becomes Monitors & Jobs with state, elapsed time, latest line, log tail and stop; the status line counts jobs; a hint, never a block, when a bash command detaches. On 2026-10-31: a job outlives its session and pi itself by default, since that is what separates it from a monitor; it is named on quit and again at the next session start. Monitors stay the session-bound runner.

What a job is, beside a monitor

monitor

job

Purpose

watch a command’s output for events

run a command pi owns to its end

Lifetime

the session’s; ended at session end (#553)

its own; outlives the session and pi

Found again

never; tellLost (monitor-records.ts) says so

at every session start, by pid and start time

Wakes on

matched lines, silence, exit

exit; matched lines optionally

Restart, on_match, silence

yes

no: a job is one run

Hub

Monitors tab

the same tab, Monitors & Jobs

Files

pi-workspace: process-start.ts (new, moved from pi-remote), index.ts or package.json exports, test/process-start.test.ts (new). pi-remote: process-start.ts (removed), identity.ts, test/process-start.test.ts (trimmed to the lock tests), package.json (already depends on pi-workspace). pi-modes: job-registry.ts, job-tools.ts, job-records.ts (new), monitor-process.ts, monitor-tools.ts, hub-pane.ts, hub-views.ts, hub-wiring.ts, status.ts, modes.json, index.ts, repeat-guard.ts (the hook pattern; the hint gets its own detach-hint.ts), tests beside each. pi-bash: line.ts, test/line.test.ts. Docs: docs/modules/ROOT/pages/modes.adoc (over the review size cap, not supplied: the tools' section, the hub’s tab, the hint), CHANGELOG.adoc (not supplied).

U1: the pid check moves, the registry, the tool

The pid check moves. processStartMs, startTicks, windowsAnswer, startMemo, rememberedStart, REMEMBER_START_MS and ProcessStart move verbatim from packages/pi-remote/process-start.ts to packages/pi-workspace/process-start.ts, exported as @gadhs/pi-workspace/process-start (a new entry in pi-workspace’s exports, beside ./contract). pidAlive (pi-remote identity.ts:324) moves with them. pi-remote’s identity.ts imports both from there; pi-modes may then import them (pi-modes depends on pi-workspace; pi-remote on both; no cycle). Of pi-remote’s test/process-start.test.ts, the cases on processStartMs, startTicks, windowsAnswer and startMemo move to pi-workspace’s new test file with dead-pid.ts’s helper; the cases on `holderAlive and acquireLinkLock stay in pi-remote, importing the moved names from pi-workspace. The liveness rule for a job is holderAlive’s (identity.ts:312-322), restated for jobs: pid alive, and `processStartMs(pid) either unavailable or at most START_SLACK_MS (1 s) after the recorded start; an "unreadable" start (#571) is not ours.

Starting a process. spawnInShell in monitor-process.ts is module-private today; U1 exports it unchanged (it already takes the shell, command, cwd, env, script path and stdio) and JobRegistry calls it with stdout and stderr both set to the log file’s descriptor. The command runs wrapped: <command> then echo $? > <exit file> on the next line of the same script, so the exit code reaches disk whether or not a pi is alive to read the child’s exit event. On Windows the file transport and the MSYS pgid line (#553) come with spawnInShell.

The registry (job-registry.ts): <agentDir>/gadhs-pi-jobs/jobs.json, written whole to <path>.<pid>.tmp then renamed (as pi-remote’s spaces.ts saveState does), 0600, holding { next: number, jobs: JobEntry[] }:

id

j<next>, from the file’s counter.

pid, pgid

the shell’s pid, and its group: the pid on POSIX (detached), the MSYS pgid on Windows.

startedAt, startedAtMs

processStartMs(pid) as recorded at start (undefined where the host cannot say), and the wall clock.

command, cwd, description, shell

as given; the shell as resolved.

log, exitFile, script

<id>.log, <id>.exit, <id>.sh under gadhs-pi-jobs/.

project

workspaceFor(cwd).root (pi-workspace slot.ts), so the right session is told.

match, wake

the optional line pattern and whether exit wakes (default true).

ended

absent while running; { at, exitCode: number | null, cause: "exited" | "stopped" | "lost", reported: boolean } once known.

JobRegistry.list() reads the file and settles every running entry: the exit file present, ended with its code; else the pid alive by the rule above, running; else lost (exitCode: null). The file is rewritten with what was found, so it never asserts a running job it did not check. start(spec) writes the script, spawns through spawnInShell, records, and tails the log with the monitor’s LineBatcher while this process lives. stop(id) ends the group as killTree (monitor-process.ts, exported) does, and records cause: "stopped". Ids and the file are read and written whole in one start, so two pi processes starting jobs at once on one agent dir each read the other’s entry on their next write.

The tool (job-tools.ts): job { command, description, cwd?, match?, wake?, max_line_chars? } answers job j3 started (pid N); log <path>; its command is judged as a monitor’s source is (judge.judgeCommand, the same rules), in the mode’s local shell. A remote workspace (workspaceFor(cwd).isRemote) is refused with "a job runs on this box; on a remote use a monitor": its pid is the remote’s, not re-findable here. job_stop { id }; job_list lists every job with state, elapsed, exit code and log, running first. modes.json: the three allowed beside `monitor’s three in auto, plan and manual (yolo allows all); the Explore, Verify and Research profiles get none.

Tests. Unit (test/job-registry.test.ts): the state machine with a fake start-time source and a fake exit file (running, exited with code, stopped, lost; a reused pid with a later start reads as lost; an "unreadable" start reads as lost); contract: the file’s shape round-trips and a hand-edited file missing fields drops those entries. Concurrency: two JobRegistry instances on one dir start a job each, and both are listed with distinct ids. Integration (test/job-tools.test.ts, the lifecycle harness): job runs a real sh -c 'echo hi; exit 3', the exit file reads 3, job_list shows exited 3; a remote cwd is refused by name; modes.json’s allow entries, as the monitor tests check theirs. Unit (pi-workspace `test/process-start.test.ts): the moved cases pass where they are.

U2: across sessions, the wake, the quit notice

Waking. While the starting process lives, the child’s exit event and the exit file both end the job; the agent gets one custom message (gadhs-job, deliverAs: "steer", triggerTurn: true when wake), framed like a monitor event (deliverEvent, monitor-tools.ts:199): job j3 ended: exit 0 after 4m12s · <description>, then the log’s last 20 lines. A match line wakes the same way with the line, at most once per RateLimiter window (monitor-stream.ts:138).

At session start (session_start, every reason): registry.list(); every job with ended and reported: false whose project is this session’s is reported in one message (2 jobs ended while no session watched: j3 exit 0 (release land), j4 lost), then marked reported. Running jobs are tailed again from the log’s current end, not replayed, and the exit file is polled each second with the tail. A session in another project sees the jobs in /hub and job_list and is told nothing.

At quit (session_shutdown, not a helper’s, as isHelperSession tells): running jobs are left running, and the session is told once: 3 jobs still running: j3 (release land), …; they continue, and the next session is told how they ended. Nothing is killed: killAllOnExit(live) (monitor-process.ts) kills what its live() returns, and that stays the monitor registry’s processes; JobRegistry registers no exit hook, so a job’s group outlives pi.

A reload is a quit and a start in one process: the registry is rebuilt from the file, the old instance’s children are re-found by pid like any other job, and their exit reaches the new instance through the exit file.

Tests. Integration (lifecycle harness): a job’s exit wakes with the code and the tail; a session ending with a job running leaves the child alive (pid checked) and listed, and says so once; a new session on the same agent dir and project reports the ended job once and never again; a session in another project is not told; a match line wakes once within the window. Contract: the gadhs-job message’s details shape, pinned.

U3: the hub and the status line

The tab’s label becomes Monitors & Jobs (HUB_TABS keeps the key monitors; TAB_NAMES, hub-pane.ts:60, changes). Its items are the monitors, then the jobs, each as a MonitorSummary built by jobSummary(entry) in job-registry.ts: status (running, exited, stopped, lost), elapsed since startedAtMs, the latest log line, exit code, the log as files.stdout; enter opens the detail with the log tail; s stops a running job through JobRegistry.stop, routed in hub-wiring.ts beside the monitor stop. HubSources.monitors becomes the combined list, with jobs separate for the stop route. The status line (spendSegments, status.ts:240) adds N jobs beside N monitors, from stats.jobs, counted as countRunning (monitor-tools.ts:351) counts monitors.

Tests. Unit (test/hub-pane.test.ts): a job row and detail, the renamed label, s on a job yields a job stop pick; (test/status.test.ts) the segment for 0, 1 and 2 jobs. Integration (test/hub-wiring.test.ts): the stop pick reaches JobRegistry.stop.

U4: the detach hint

Detection, in pi-bash. BashLine (line.ts:61) gains detaches: boolean, set in collect() (line.ts:400) when the parse holds any of: a statement terminated by the & token (in tree-sitter-bash’s grammar a background & is a _terminator, an anonymous & token that follows the statement as its next sibling under program, _statements or a compound body; list nodes are &&/|| only, and && is one token, so the walk tests the sibling token’s text & after each statement-level node); a command whose head after wrappers (headAfterWrappers, line.ts:285) is disown; or nohup or setsid as a wrapper at a command’s head (WRAPPER_VALUE_FLAGS, wrappers.ts:24, already names both). A line whose status is unavailable or that holds a syntax error has detaches: false: no hint from a guess.

The hint, in pi-modes (detach-hint.ts): on tool_result for the bash tool (the repeat-guard.ts:107 pattern), analyzeLine(command).detaches sends one steer: that command detached a process pi cannot see or stop; job runs it tracked, shows it in /hub and wakes you when it exits. Once per session per distinct command text, and not after the agent has called job in the session. Never a block: it reads results and never touches authorizeAsk.

Tests. Unit (pi-bash test/line.test.ts): detaches true for nohup x &, setsid x, x & disown, x > log 2>&1 &; false for a && b, x 2>&1, grep '&' f, a heredoc containing &, and an unparseable line. Unit (pi-modes test/detach-hint.test.ts): one steer per distinct command, none after a job call, none for a non-bash tool; property (fast-check): no input to the hint ever yields a deny or a changed tool input, which pins "never a block".

U5: Windows (pi-win)

U1 starts a Windows job through the file transport with the MSYS pgid (#553), and stop uses windowsStopCommands. pi-win’s unit, from this plan, on its own issue: re-finding a job’s whole tree after a reload by Win32_Process parent ids (a shell’s children leave its MSYS group once the launcher is gone, #603), the exit file’s path and script quoting under Git Bash, and the Windows runs of U1-U4’s tests. Its shape is decided with pi-win once U1 is merged.

Risks

  • A reused pid read as a live job (fail-open: a stranger’s process shown as ours, or stopped by s). The liveness rule above; an unreadable start is not ours. Tested in U1’s unit cases.

  • A job killed at pi’s exit after all (silent: the operator chose the opposite). No exit hook and no stopAll for jobs; U2’s integration test ends a session with a job running and asserts the child lives.

  • A stale registry after a crash (fail-open: running entries that are not). list() settles every entry before it answers and rewrites the file; U1’s unit cases.

  • The exit file never written (silent: a job running for good). echo $? runs after the command whatever it did; a shell killed by SIGKILL writes none, and then a dead pid with no exit file reads as lost; U1’s unit cases.

  • Two pi processes starting jobs at once (silent: a lost entry). Read and written whole per start; U1’s concurrency test.

  • The hint nags (silent: the agent ignores everything after it). Once per session per command, and never after a job call; U4’s unit test.

  • The hint blocks (fail-closed: a detaching command refused). The hint reads tool_result only; U4’s property test.

  • Log growth (silent: a long job fills the disk). The log is the job’s own; job_list shows its size and the doc says a job that logs much should redirect.

Scope

In: U1-U4 above, with their tests, mutation checks, CHANGELOG.adoc entries and modes.adoc on each branch; the plan committed first.

Out: remote jobs (an issue when wanted); restart, on_match and silence for jobs (use a monitor); a job’s output on the phone; attaching an already-running process as a job; surviving a host reboot; U5’s Windows tree work, which is pi-win’s issue.

Delivery, per unit: a branch from origin/main; code and the tests named; a mutation check; the changelog entry and modes.adoc on the same branch; the pi-modes, pi-workspace, pi-remote and pi-bash suites, tsc and biome check green; land by MR, Relates to #577 on U1-U3 and Closes #577 on U4. One release after U4 (pi-bash, pi-workspace, pi-modes, pi-remote). After the close, one line on #577 with the merge SHA and what was deferred (U5, remote jobs).

Edit this page · latest