From 791b1cf1a4c32d8b13863d6790456346d9a80b9a Mon Sep 17 00:00:00 2001 From: omartelo Date: Mon, 23 Nov 2026 14:34:19 -0300 Subject: [PATCH] feat(hooks): report background work on the done report A turn that ends with a background command or subagent still running sends `background: true` on `done`, so lich holds a worker's answer until the turn the work's end starts. Claude Code's Stop names the work in background_tasks (measured on 2.1.330), Antigravity's in fullyIdle false (measured on 1.5.15). session-state.jsonl is read at lich bb6c4a07, in no release yet. --- CHANGELOG.md | 11 +++++++ docs/session-state.md | 23 ++++++++++++++ hooks/report-state.sh | 17 +++++++++++ tests/fixtures/session-state.jsonl | 2 ++ tests/hooks.test.mjs | 49 ++++++++++++++++++++++++++++++ tests/refresh-fixtures.sh | 11 ++++++- 6 files changed, 112 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a90bca..19c3787 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +Needs the lich that understands background work. + +### Added + +- **A worker's answer waits for its background work on Claude Code and + Antigravity.** When a turn ends with a background command or subagent still + running, the `done` report now says `background: true`. lich holds the + errand's answer and sends it at the next turn end with no work left. Claude + Code's `Stop` names the work in `background_tasks`; Antigravity's `Stop` + says `fullyIdle: false`. The other harnesses send nothing new. + ## [0.22.0] - 2026-11-23 Needs the lich release after v0.92.0, the first that answers `/subagent`, takes diff --git a/docs/session-state.md b/docs/session-state.md index e40a951..dbab21c 100644 --- a/docs/session-state.md +++ b/docs/session-state.md @@ -129,6 +129,29 @@ the turn without another tool call stays `waiting` until `Stop`. Codex recovers the same way: an approved request runs the tool it was asking about, and that tool's `PostToolUse` re-arms the spinner. +## Background work + +A turn can end with work the agent started still running: a background +command, or a background subagent. That turn's words are not the answer; the +work's end starts a turn of its own, and that turn's `done` is. So the `done` +report carries `background: true` while any such work runs, and lich answers no +errand until a `done` arrives without it. + +| harness | what says the work still runs | +|-------------|------------------------------------------------------------------------| +| Claude Code | a `background_tasks` entry with `status` `running`, on `Stop` and `StopFailure` | +| Antigravity | `fullyIdle: false` on `Stop` | +| the rest | nothing: no field is sent | + +Both are measured. Claude Code 2.1.330 lists each piece of work as +`{type: shell|subagent, status: running}`, and sends `[]` on the turn the +work's end starts. Antigravity 1.5.15 sent `fullyIdle: false` on the `Stop` of a +turn that left a `sleep` running, then `fullyIdle: true` on the `Stop` of the +turn that started when it finished. oh-my-pi needs neither: it holds its +`session_stop` until its async work is done. Without `jq` the script matches +`"status":"running"` or `"fullyIdle":false` anywhere in the payload instead of +in those fields. + ## Compacting No client reports `compacting`. The Claude Code mod that bracketed diff --git a/hooks/report-state.sh b/hooks/report-state.sh index 81626a2..514fc76 100755 --- a/hooks/report-state.sh +++ b/hooks/report-state.sh @@ -77,6 +77,23 @@ if [ "$state" = waiting ]; then fi fi +# A turn that ends with a background command or subagent still running is not +# the answer: the work's end starts a turn of its own, and that turn's `done` is +# (docs/session-state.md, Background work). Claude Code lists the work in +# `background_tasks`, Antigravity says `fullyIdle: false`; both measured, no +# other harness names it. Without jq the match is not anchored to those fields. +if [ "$state" = done ]; then + if command -v jq >/dev/null 2>&1; then + printf '%s' "$payload" | jq -e \ + 'any(.background_tasks | arrays | .[]; .status? == "running") or .fullyIdle == false' \ + >/dev/null 2>&1 && body="${body%\}},\"background\":true}" + else + printf '%s' "$payload" | \ + grep -Eq '"status"[[:space:]]*:[[:space:]]*"running"|"fullyIdle"[[:space:]]*:[[:space:]]*false' && \ + body="${body%\}},\"background\":true}" + fi +fi + # The conversation that fired the hook, so lich can drop a report from an agent # CLI nested in the session (docs/session-state.md, Nested agent CLIs). An id # needs no escaping, and none is left out rather than sent empty. diff --git a/tests/fixtures/session-state.jsonl b/tests/fixtures/session-state.jsonl index 404d594..2f8855a 100644 --- a/tests/fixtures/session-state.jsonl +++ b/tests/fixtures/session-state.jsonl @@ -1,5 +1,7 @@ {"name":"busy","body":{"session_id":"s1","state":"busy"},"accept":{"session_id":"s1","state":"busy","tool":"","detail":""}} {"name":"done","body":{"session_id":"s1","state":"done"},"accept":{"session_id":"s1","state":"done"}} +{"name":"done with background work running","body":{"session_id":"s1","state":"done","background":true},"accept":{"session_id":"s1","state":"done","background":true}} +{"name":"background outside done says nothing","body":{"session_id":"s1","state":"busy","background":true},"accept":{"session_id":"s1","state":"busy","background":false}} {"name":"waiting","body":{"session_id":"s1","state":"waiting"},"accept":{"session_id":"s1","state":"waiting"}} {"name":"idle","body":{"session_id":"s1","state":"idle"},"accept":{"session_id":"s1","state":"idle"}} {"name":"compacting","body":{"session_id":"s1","state":"compacting"},"accept":{"session_id":"s1","state":"compacting"}} diff --git a/tests/hooks.test.mjs b/tests/hooks.test.mjs index 03091ea..82c0025 100644 --- a/tests/hooks.test.mjs +++ b/tests/hooks.test.mjs @@ -762,6 +762,55 @@ for (const state of ['busy', 'done', 'idle']) { }) } +// A turn that ends with background work still running is not the worker's +// answer, so its `done` says so and the next one without it answers. Both +// payloads measured: Claude Code 2.1.330's `background_tasks` (`[]` once the +// work ends), Antigravity 1.5.15's `fullyIdle`. +const BACKGROUND = [ + ['claude', 'Stop', { background_tasks: [{ type: 'shell', status: 'running' }] }, true], + ['claude', 'Stop', { background_tasks: [{ type: 'subagent', status: 'running' }] }, true], + ['claude', 'Stop', { background_tasks: [] }, false], + ['claude', 'StopFailure', { background_tasks: [{ type: 'shell', status: 'running' }] }, true], + ['antigravity', 'Stop', { fullyIdle: false }, true], + ['antigravity', 'Stop', { fullyIdle: true }, false], +] + +for (const [provider, event, fields, background] of BACKGROUND) { + const registration = REGISTRATIONS.find( + (r) => r.provider === provider && r.event === event && r.script === 'report-state.sh', + ) + for (const [how, env] of [['', {}], [' without jq', { PATH: binWithout('jq') }]]) { + test(`${provider} ${event} ${JSON.stringify(fields)} reports background ${background}${how}`, async () => { + await withStub(async (stub) => { + const result = await runHook(registration.command, { + env: { ...lichEnv(stub.port), ...env }, + stdin: JSON.stringify({ ...JSON.parse(stdinFor(registration)), ...fields }), + pluginRoot: registration.pluginRoot, + }) + assertHookSucceeded(result, registration.stdout ?? '') + const { body } = assertHookHonoured('/hook', stub.requests[0]) + assert.equal(body.state, 'done') + if (background) assert.equal(body.background, true) + else assert.ok(!('background' in body), `sent background: ${stub.requests[0].raw}`) + assert.equal(body.provider_session_id, PROVIDER_SESSION_ID[provider]) + }) + }) + } +} + +// lich drops `background` on any state but `done`, so no other one sends it. +test('busy says nothing of the background work still running', async () => { + await withStub(async (stub) => { + const result = await runHook(`"${ROOT}/hooks/report-state.sh" busy`, { + env: lichEnv(stub.port), + stdin: JSON.stringify({ session_id: PROVIDER_SESSION_ID.claude, background_tasks: [{ type: 'shell', status: 'running' }] }), + }) + assertHookSucceeded(result) + const { body } = assertHookHonoured('/hook', stub.requests[0]) + assert.ok(!('background' in body), `busy sent background: ${stub.requests[0].raw}`) + }) +}) + for (const [script, argument] of [['report-state.sh', 'busy'], ['report-tool.sh', '']]) { const command = `"${ROOT}/hooks/${script}" ${argument}` test(`${script} names the conversation without jq`, async () => { diff --git a/tests/refresh-fixtures.sh b/tests/refresh-fixtures.sh index 3b124f5..88718f7 100755 --- a/tests/refresh-fixtures.sh +++ b/tests/refresh-fixtures.sh @@ -19,11 +19,20 @@ ref=$(tr -d '[:space:]' <"$here/lich-ref") raw=https://raw.githubusercontent.com/lichdotdev/lich/$ref base=$raw/docs/hooks/fixtures -for file in session-start.jsonl session-state.jsonl session-title.jsonl; do +for file in session-start.jsonl session-title.jsonl; do curl -fsSL "$base/$file" -o "$here/fixtures/$file" echo "refreshed $file at $ref" done +# session-state gained `background` on the done report in lich's commit +# bb6c4a07 (fix/turn-end-background-work), in no release yet, so it is read at +# that commit. Once a release ships it, move tests/lich-ref to that release and +# put the file back in the loop above. +unreleased=bb6c4a0729e237d4d08a33553ee2f0d4168cbd8c +curl -fsSL "https://raw.githubusercontent.com/lichdotdev/lich/$unreleased/docs/hooks/fixtures/session-state.jsonl" \ + -o "$here/fixtures/session-state.jsonl" +echo "refreshed session-state.jsonl at $unreleased (unreleased)" + # session-touched gained paths, hold, collect and the edit note in lich's merge # commit c62b0796 (feat/touched-edit-note), in no release yet, so it is read at # that commit. Once a release ships it, move tests/lich-ref to that release and