Skip to content

feat(schedule): in-flight fire state, progress, and wall-clock timeout (#386) - #400

Merged
JAORMX merged 7 commits into
mainfrom
feat/386-fire-state
Aug 5, 2026
Merged

JAORMX merged 7 commits into
mainfrom
feat/386-fire-state

Conversation

@JAORMX

@JAORMX JAORMX commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #386 — a scheduled task could be claimed and run for a long time with no useful indication it started, is progressing, or became stuck. ScheduleQuery inspect rendered a claimed-but-running fire as fires: none (ambiguous: running / stuck / crashed / RecordFire failed), the minted sched-- session id was only discoverable after the run completed, and there was no per-fire wall-clock bound. (Observed with monitor-pr-6208: 16 turns, 26 tool calls, 1.2M tokens, all invisible until the operator inspected storage by hand.)

The fix promotes the in-flight fire to a first-class, persisted lifecycle stage (ADR 0097) and gives each fire a wall-clock deadline.

Changes (6 commits)

  1. State model + store adapters — engine/port/schedule.go: ScheduleState gains LastFireStartedAt/LastFireProgressAt/FireDeadline; ScheduleFire gains StartedAt/ProgressAt/Deadline; ScheduleSpec gains FireTimeout. ScheduleStore gains RecordFireStart (persist the in-flight fire + stamp the real session id at start) and RecordFireProgress. RecordFire flips terminal + clears the in-flight fields; Claim zeroes them. New StopTimeout reason (clean, recoverable, a StopBudget sibling). memschedulestore/jsonlstore/redisstore implement it; 5 conformance subtests.
  2. Fire lifecycle (makeFireFunc) — early RecordFireStart after session create; a time.AfterFunc watchdog cancels the run via Service.Cancel after effectiveFireDeadline (FireTimeout, else 30m default), overriding a deadline-fired StopCancelled to StopTimeout with an honest error; RecordFireProgress on turn boundaries; create-failure still records a terminal fire.
  3. Started notice (deliverFireStarted) — a fenced-untrusted, harness-authored [scheduled task <name> started (fire <id>)] notice on the shared DeliveryQueue exactly-once ledger (distinct from the terminal note; no model content), state-aware.
  4. Stale-fire reconcile — the scheduler detects stale in-flight state store-only (pending sentinel past the window, or a real in-flight fire whose lease is no longer live); a composition ReconcileStaleFire callback settles crash-after-Claim (terminal StopError) and crash-after-session (session cancelled + StopError). A live fire (lease held) is never reconciled.
  5. Surfaces agree — renderScheduleInspect, the gRPC/HTTP wire mappers (proto gained the fields), and the TUI overlay all render a claimed fire as in-flight: claimed (session pending) (never fires: none) and an in-flight fire with started/last-progress/deadline.
  6. Review fixes — RecordFireProgress now takes the fire id (no O(N) store scan on the hot path) and the redisstore progress write is an atomic Lua CAS (a terminal record can never be reverted to in-flight by a cross-replica race).

Review

Adversarial panel (spec + concurrency) over the committed branch: all 9 acceptance criteria MET, no scope creep. Two Medium findings (the Redis non-atomic progress write and the jsonlstore hot-path scan) — both fixed in the final commit with regression-pinned conformance subtests. The watchdog run-scoping, the reconcile TOCTOU fencing, and the StopTimeout classification were all verified clean.

Tests

Per-criterion coverage: live fire, completed fire, timed-out fire (StopTimeout + recoverable session), crash after Claim, crash after session creation, RecordFire/create failure, the started-notice exactly-once/no-model-content, the stale-reconcile (incl. live-fire-not-reconciled), the wire projection, and the agent + TUI render. task build, task lint, task test, task api:check, task docs:check, task site:build all green.

Docs

  • docs/adr/0097-scheduled-fire-inflight-state.md (new) + README index.
  • docs/design/IMPLEMENTATION-NOTES.md — "In-flight fire state" note.
  • docs/adr/0027-cloud-native.md — List-1 re-audit (no new row; the state lives in the durable store).
  • docs/usage/mecated.md — the fire_timeout spec field + in-flight inspect.
  • user-docs/what-you-get/scheduled-tasks.md — "Watching a fire while it runs".
  • engine/CHANGELOG.md + engine/api/*.txt — the ScheduleStore widening + StopTimeout (Added/minor).

Note on the port widening

ScheduleStore gains two methods — a breaking change for any external implementer, but every implementation is in-tree (monorepo lockstep), so there is no external break. Recorded per ADR 0037 (task api:update + changelog).

JAORMX added 7 commits August 6, 2026 00:12
…ters (#386)

Phase 1+2 of the in-flight scheduled-fire lifecycle. Today a claimed fire
is invisible until RecordFire: inspect renders "fires: none" while one
runs (ambiguous: running / stuck / crashed / RecordFire-failed), and the
minted sched-- session id is only discoverable after the run completes.

Promote the in-flight fire to a first-class persisted stage:

- engine/port/schedule.go: ScheduleState gains LastFireStartedAt /
  LastFireProgressAt / FireDeadline; ScheduleFire gains StartedAt /
  ProgressAt / Deadline; ScheduleSpec gains FireTimeout (per-fire
  wall-clock deadline knob). ScheduleStore gains RecordFireStart (persist
  the in-flight fire + stamp the real session id early) and
  RecordFireProgress (advance the last-progress timestamp). RecordFire
  flips terminal + clears the in-flight fields; Claim/ClaimNow zero them.
- engine/session: new StopTimeout StopReason ("timeout") — a clean,
  recoverable wall-clock-deadline terminal (StopBudget sibling), string
  passthrough on the wire.
- memschedulestore (reference) + jsonlstore + redisstore implement the
  two methods; the scheduleconformance suite gains 5 in-flight subtests
  (persist-start, idempotent, progress-advances, record-clears,
  claim-alone-leaves-zero) and all three backends pass.
- engine/api/*.txt regenerated; engine/CHANGELOG.md notes the Added
  (minor) surface.

Composition wiring (fire lifecycle, wall-clock enforcement, started
notice, stale reconcile) lands in the next commit.
…line, progress (#386)

Phase 3 wires the in-flight lifecycle into the composition FireFunc
(makeFireFunc):

- Early persistence: after CreateSessionWithProfile, RecordFireStart
  stamps the real sched-- session id + StartedAt + the computed Deadline
  (best-effort WARN on failure; the at-most-once Claim already advanced).
- Wall-clock deadline: a time.AfterFunc watchdog cancels the in-flight
  run via the blessed Service.Cancel/run.Cancel seam after
  effectiveFireDeadline (spec.FireTimeout, else defaultFireTimeout=30m).
  A deadline-fired run that drained to StopCancelled is overridden to
  StopTimeout with an Err naming the timeout; the session settles to a
  recoverable terminal snapshot via settleFireTerminalSnapshot.
- Progress: RecordFireProgress fires on EvToolCall/EvTurnEnd/EvResult
  (not per chunk), advancing LastFireProgressAt.
- Create-failure after Claim still records a terminal StopError fire.

startScheduler rediscovers the ScheduleStore and threads it + the default
timeout into makeFireFunc (storage-agnostic). Tests cover early-persist,
StopTimeout on deadline, progress advance, and create-failure terminal.
Phase 4a. The originating conversation now gets a lightweight, fenced-
untrusted, harness-authored notice when a fire STARTS (schedule name +
fire id + session id), distinct from the terminal result report — the
operator sees "it triggered, here's the fire" instead of silence until
completion.

deliverFireStarted mirrors deliverFireResult: enqueues to the same
DeliveryQueue exactly-once ledger with a distinct "started" header so it
never duplicates or collides with the terminal note, state-aware
(busy/awaiting origin enqueues only, empty origin no-op), and carries NO
model-authored fire content. makeFireFunc invokes it (nil-safe) right
after the early RecordFireStart; wired in startScheduler. Tests cover
enqueue-with-ids, busy/awaiting enqueue-only, empty-origin no-op,
non-duplication of the terminal note, and fenced harness-authored body.
Phase 4b. A crashed process's claimed-but-never-terminal fire is now
distinguishable from a live run after restart, instead of sitting
"pending"/in-flight forever.

The scheduler detects stale in-flight state store-only (pending sentinel
past the window, or a real in-flight fire whose prior-fire lease is no
longer live and whose start is past the window); a genuinely-live fire
(lease held) is never reconciled. Reconciliation is a composition-injected
ReconcileStaleFire callback (the scheduler stays storage-agnostic, never
importing the server) that settles the two crash cases: crash-after-Claim
records a terminal StopError fire; crash-after-session settles the session
to cancelled (Interrupt-recoverable) and records StopError.

Also fix TestFireDelivery_EmbeddedEndToEnd: it now waits for the terminal
"completed with stop reason:" marker specifically, since the new started
notice shares the "[scheduled task " prefix and would otherwise satisfy
the looser match first.
#386)

Phase 5 makes the in-flight fire VISIBLE and keeps the three surfaces
(agent inspect / gRPC+HTTP / TUI overlay) in agreement.

- renderScheduleInspect (engine/agent): a claimed fire (pending sentinel,
  run not started) renders "in-flight: claimed (session pending)" instead
  of the ambiguous "fires: none"; an in-flight fire (Stop empty) renders
  the in-flight marker + started/last-progress/deadline; a schedule-level
  in-flight summary shows when a live fire exists. fire_timeout surfaces
  in the spec summary.
- schedule.proto: ScheduleState + last_fire_started_at /
  last_fire_progress_at / fire_deadline; ScheduleFire + started_at /
  progress_at / deadline; ScheduleSpec + fire_timeout (Duration).
  Regenerated contracts/gen.
- grpc_schedule.go: all four wire mappers project the new fields (the ONE
  projection path gRPC + HTTP share).
- TUI overlay: client structs + mappers carry the fields; the inspect
  render mirrors the agent (in-flight marker, claimed-pending, never
  "no fires recorded" when claimed).

Tests: agent render (in-flight / claimed-pending / never-fired /
terminal), a wire-projection test driving the real store, and TUI render.
- docs/adr/0097-scheduled-fire-inflight-state.md (new): the in-flight
  fire as a first-class persisted lifecycle stage; early session-id
  persistence; progress timestamps; the wall-clock deadline + StopTimeout;
  the started notice; the storage-agnostic detect-in-scheduler /
  reconcile-in-composition stale-fire seam.
- docs/adr/README.md: index row.
- docs/design/IMPLEMENTATION-NOTES.md: "In-flight fire state" paragraph.
- docs/adr/0027-cloud-native.md: List-1 re-audit (no new row; the state
  lives in the durable store).
- docs/usage/mecated.md: the fire_timeout spec field + in-flight inspect.
- user-docs/what-you-get/scheduled-tasks.md: "Watching a fire while it
  runs" (in-flight state, timeout, started notice, crash recovery).
- llms.txt regenerated.
Two Medium findings from the #386 panel:

- RecordFireProgress now takes the fire ID (RecordFireProgress(ctx, name,
  fireID, at)) so the caller passes the known id instead of the store
  scanning for the in-flight fire. jsonlstore writes the single fire file
  directly (no ReadDir scan under the store mutex on every turn boundary);
  redisstore addresses the single key (no SCAN); memschedulestore looks it
  up directly. Engine-port widening; engine/CHANGELOG.md amended.
- redisstore's progress write is now an atomic Lua CAS (byte-compare
  optimistic lock) so a concurrent terminal RecordFire landing mid-write
  can never revert a terminal record back to in-flight (the cross-replica
  lease-lapse race). The "Stop empty" guard + the byte-compare together
  guarantee the terminal record wins.

Conformance gains two subtests across all three backends: progress targets
the named fire only, and progress never reverts a terminal fire.
@JAORMX
JAORMX merged commit 209b07f into main Aug 5, 2026
16 checks passed
@JAORMX
JAORMX deleted the feat/386-fire-state branch August 5, 2026 23:50
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.

Scheduled tasks: expose in-flight fire state, progress, and wall-clock timeout

1 participant