Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions docs/session-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions hooks/report-state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/session-state.jsonl
Original file line number Diff line number Diff line change
@@ -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"}}
Expand Down
49 changes: 49 additions & 0 deletions tests/hooks.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 10 additions & 1 deletion tests/refresh-fixtures.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading