# Background-task lifecycle fixture.
#
#   HAX_PROVIDER=mock HAX_MOCK_SCRIPT=scripts/mock/tasks.txt build/hax
#
# Run from the repository root and type one message. Timing assumes the default
# five-second background yield; HAX_BASH_BACKGROUND_YIELD=500ms gives a faster
# run. Named task IDs must be unused, and the first unnamed task must be `t3`.

# Turn 1: a background request that finishes during the yield stays synchronous.
# The launch note naming the never-created task is a model-only tail — the display
# shows an ordinary call (HAX_TRANSCRIPT captures the note).
text A backgrounded command that finishes quickly returns synchronously — no task. The launch note saying so goes to the model only; below, this looks like a plain call.
tool bash {"command":"echo quick result","background":true,"name":"quick"}
end-turn

# Turns 2–3: launch two paced producers in parallel.
text Launching two long jobs in parallel; each detaches as a task at the yield window.
tool bash {"command":"scripts/stream_demo.py slow --prefix tests --delay 0.4 --count 30","background":true,"name":"tests"}
end-turn

tool bash {"command":"scripts/stream_demo.py slow --prefix lint --delay 0.5 --count 30","background":true,"name":"lint"}
end-turn

# Turn 4: stream `lint` until the other task finishes and interrupts the wait.
text Waiting on the lint run first — the test run will interrupt us when it finishes.
tool task_wait {"id":"lint","timeout_seconds":30}
end-turn

# Turn 5: collect the completed task's remaining output immediately.
text The test run was announced; collecting its output.
tool task_wait {"id":"tests"}
end-turn

# Turn 6: resume `lint` without missing or repeating output.
text Back to the lint run.
tool task_wait {"id":"lint","timeout_seconds":30}
end-turn

# Turn 7: a foreground timeout detaches the command instead of killing it.
text A foreground command that outlives its timeout detaches instead of dying.
tool bash {"command":"echo long build; sleep 60; echo never printed","timeout_seconds":1}
end-turn

# Turn 8: stop the detached command; the kill also collects its final output.
text That build is doomed; stopping it.
tool task_wait {"id":"t3","kill":true}
end-turn

# Turn 9: launch a task that completes after control returns to the user.
text Finally, launching a job I will not wait for.
tool bash {"command":"sleep 7; echo background answer 42","background":true,"name":"oracle"}
end-turn

# Turn 10: end the chain while `oracle` is running.
text Done for now — task oracle is still running. Try /tasks, give it a few seconds, then send another message: its completion note arrives with your next prompt.
end-turn

# Turn 11: collect `oracle` after its injected completion note.
text I was notified about oracle while you were away; collecting its answer.
tool task_wait {"id":"oracle"}
end-turn

text No polling needed. Demo complete.
end-turn
