Skip to content

feat(coder): preserve lifecycle metadata and diagnostics for detached jobs - #103

Merged
aixle-com[bot] merged 12 commits into
developfrom
feature/detached-job-lifecycle-metadata
Aug 15, 2026
Merged

aixle-com[bot] merged 12 commits into
developfrom
feature/detached-job-lifecycle-metadata

Conversation

@aixle-com

@aixle-com aixle-com Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

What changed

A detached job (coder_ssh_exec(detach: true)) used to write its exit code only after the command returned normally. If the wrapper or its process group was killed, coder_job_status could report no more than died: no exit code, no signal, no timing, nothing to tell a failing test from a workspace that rebooted. Recovering any of that meant reading file mtimes or Docker events by hand.

Launcher (Coder::SshRunner#detach_script) now persists <job_id>.meta before it starts anything: job id, workspace, job dir, log path, started_at (+ epoch). A job killed a second later still has its identity on disk.

Wrapper (<job_id>.run):

  • traps TERM/INT/HUP/QUIT, forwards the signal to the command, and then records finished_at, reason=signaled, the signal name and the 128+n exit code — so an interrupted job still writes its exit file instead of looking like it evaporated;
  • runs the command in the background and waits on it, because a POSIX shell only runs a trap once the foreground command returns;
  • writes a heartbeat (<job_id>.hb) every 10s, which is what dates the death of a job whose wrapper was SIGKILLed and could therefore run no trap;
  • records its own pid/pgid, and classifies its own outcome (completed, command_failed, timeout for exit 124, signaled).

coder_job_status returns that metadata alongside the existing state/exit_code/tail: reason, signal, command, workspace, pid, pgid, started_at, finished_at (+ finished_at_estimated when it comes from the last heartbeat), elapsed_seconds, last_heartbeat_at, heartbeat_age_seconds, log_path, log_bytes — plus a one-line diagnosis that names the four outcomes apart: the command's own failure, a timeout, a cancellation/outside kill, and an infrastructure failure.

The workspace name is sanitised before it is recorded, because it lands inside a double-quoted shell string.

Why the shape

  • Metadata is one append-only key=value file, last line wins: the wrapper appends as it learns, and a reader never needs a second source.
  • The status script emits the log tail last, so the metadata survives response truncation.
  • started_at and meta_path are also returned from the launch call, so a caller knows them without polling.

Compatibility

A job started by the previous wrapper has no .meta file. It still reports state, exit_code and tail; reason is then derived from the exit code alone, and every metadata-derived field is simply absent rather than null. Covered by a test.

How to test

docker compose exec -T web bin/rails test test/services/coder/ssh_runner_test.rb test/services/internal_tools/coder_tools_test.rb test/services/coder/repo_bootstrap_test.rb
docker compose exec -T web bundle exec rubocop
docker compose exec -T web bundle exec brakeman -q --no-pager

New coverage in test/services/coder/ssh_runner_test.rb:

  • Parsing/classification — successful exit, non-zero exit, trapped signal, a job that died before the exit file (heartbeat-dated, marked as an estimate), and a job that predates lifecycle metadata.
  • The wrapper for real — three tests launch and poll actual jobs through a local shell (AIXLE_JOB_DIR in a tmpdir, coder ssh swapped for sh -c): success, exit 3, and a SIGTERM mid-run that must record reason=signaled/signal=TERM/143 instead of vanishing.
  • Launch metadata is written before the wrapper starts; the recorded workspace name cannot carry shell syntax.

Board task: #581.

🤖 Generated with Claude Code

Alex (Aixle agent) added 4 commits August 14, 2026 11:03
A detached job that was not allowed to finish normally used to lose
everything: the wrapper wrote its exit code only after the command
returned, so a killed wrapper left `coder_job_status` able to say no more
than `died` — no exit code, no signal, no timing, no way to tell a failing
test from a workspace that rebooted.

The launcher now persists `<job_id>.meta` before it starts anything (job
id, command, workspace, pid/pgid, `started_at`), the wrapper traps
TERM/INT/HUP/QUIT so an interrupted job still records its signal,
`finished_at` and exit code, and a heartbeat dates the death of a job
whose wrapper was SIGKILLed and could therefore run no trap.
`coder_job_status` returns that metadata plus a `reason`
(completed | command_failed | timeout | signaled | runner_vanished) and a
one-line `diagnosis`, which is what separates a command failure from a
cancellation, a timeout and an infrastructure failure.

A job started by the previous wrapper has no metadata file and still
reports state, exit code and log tail; its `reason` is derived from the
exit code alone.
…njection assertion

Also prefer the heartbeat's own timestamp over re-deriving it from its
epoch when dating a job that died.
…ob-lifecycle-metadata

# Conflicts:
#	app/services/coder/ssh_runner.rb
Comment thread app/services/coder/ssh_runner.rb Resolved
Comment thread app/services/coder/ssh_runner.rb Resolved
Alex (Aixle agent) added 2 commits August 14, 2026 20:15
… cause

Two blocking review findings on PR #103.

The detach wrapper's trap recorded the signal it received but always sent
TERM to the command, so a HUP was reported as something the command never
got and signal-specific cleanup ran for the wrong signal. It now forwards
the signal it was sent. INT and QUIT are the exception POSIX forces: a
non-interactive shell sets them to ignore in a command it starts in the
background, so they cannot reach the command at all — TERM follows them so
the job is not orphaned, and `child_signal` records the substitution
instead of letting the metadata imply otherwise. (The same rule applies to
the wrapper itself, which is why its INT/QUIT traps cannot fire in the
detached launch path; TERM and HUP, the ones a teardown sends, can.)

The diagnosis for a vanished runner asserted an infrastructure failure,
but a hard `kill -9` on the job's process group leaves byte-identical
evidence. Nothing in the app hard-kills a detached job, so there is no
cancellation intent to persist; the poll now reports the state as
indeterminate, names both candidate causes in the diagnosis and carries
them as `possible_causes` rather than sending an operator after a phantom
infrastructure failure.
Comment thread app/services/coder/ssh_runner.rb Resolved
aixle-com added 3 commits August 14, 2026 21:48
A successful `kill` confirms delivery, not termination: a command that traps
the signal and keeps working stayed alive while the job already reported
`exited`, so the persisted lifecycle state could be false while the log was
still changing.

The wrapper's trap now forwards the signal as before, then waits for the
command and reaps it before writing anything terminal. The wait is bounded by
`job_kill_grace_seconds` and ended by a SIGKILL escalation, and what really
happened is recorded: `signaled_at` when the cancellation started,
`child_exit_code` from the reap, `escalated_to` when the first signal was not
what ended it. A command that trapped the signal and exited 0 keeps its own
status in `child_exit_code` while the job still reports the conventional
128+signo code — cancelled, not successful. The heartbeat keeps running through
the grace window, so a job whose command is still alive keeps reading `running`.
Comment thread app/services/coder/ssh_runner.rb Resolved
aixle-com and others added 3 commits August 15, 2026 11:46
`wait` in the job wrapper reaps the `sh <job>.cmd` shell and nothing under
it, and both the forwarded signal and the SIGKILL escalation targeted only
that pid. A command script blocked on a foreground descendant (`sleep 60;
echo done`) therefore returned 143 the moment the shell was signalled while
the descendant kept running, and the wrapper published a terminal `.exit`
over work that was still consuming the workspace.

The command is now started under `setsid`, so it leads a process group of
its own and the cancellation reaches the whole tree it built. The group is
only ever targeted when it is led by the command itself and differs from the
wrapper's group — signalling the wrapper's own group would kill the wrapper
and lose the metadata this path exists to write — and a workspace without
`setsid` falls back to the previous pid targeting. After the reap the wrapper
drains what is left of the group before publishing anything terminal, bounded
by the grace window's SIGKILL and a settle cap for what even that cannot
reap (`group_survivors`).

Signals go out as `kill -NAME <target>`: dash parses the argument after
`-s NAME` as another option bundle, so `kill -s TERM -1234` dies with
"Illegal option -1".

The pgid is read from `/proc/<pid>/stat` with `ps` as the fallback, because
the busybox `ps` of a minimal workspace image has no `-p`. `command_pid` and
`command_pgid` are recorded, and since a hard kill of the wrapper's group no
longer stops the command, the poll reports `command_group_alive` for a
vanished runner whose command is still running — with the group to kill.
setsid makes the command the leader of a group of its own, so its group
id is its pid. Reading it back out of /proc after the launch instead
lost the race against a command that had already exited, and the
wrapper then wrote no command_pgid at all — the field a caller cleans up
survivors by, and the one command_group_alive is derived from.

@aixle-com aixle-com Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approved on technical merit. The lifecycle implementation meets the task requirements and follows established Coder service patterns; prior blocking findings are resolved. GitHub does not permit this app identity to formally approve its own PR. One non-blocking framing robustness recommendation is attached inline.

Comment thread app/services/coder/ssh_runner.rb Resolved
@aixle-com
aixle-com Bot merged commit 2e53396 into develop Aug 15, 2026
7 checks passed
artempartos added a commit that referenced this pull request Aug 28, 2026
)

The detached-job signal tests kill the wrapper themselves and assert the
job publishes the signal instead of disappearing. Both outcomes read as
`died`, because that state means only "the wrapper is gone and wrote no
exit file" — which is the #103 regression, but equally what is left when
something outside the test destroys the wrapper mid-run. No trap survives
SIGKILL, so a wrapper reclaimed by the environment cannot write the exit
file however correct it is.

`signaled_at` separates the two: the trap records it before it touches
anything else. Present, the wrapper did receive and register the signal
the test sent and was ended afterwards by something that is not the
subject here, so the case skips with the status attached. Absent, the job
really did vanish unrecorded and the assertions fail as before.

The terminal assertions also carry the full status now. A failure used to
print `Expected "exited", Actual "died"` and nothing else, which cannot
distinguish a wrapper that published the wrong thing from one that never
got to publish at all.

Signed-off-by: Artem Petrov <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

1 participant