Skip to content

fix: shift watcher — close four state-machine gaps (M1.1) #603

Closed

Description

@Jammy2211

Overview

Programme task M1.1 of the review-to-autonomy programme (epic autonomous-orchestrators, phase 8). The ecosystem review of 2026-11-10 confirmed four state-machine gaps in the shift watcher (agents/conductors/batch/_orchestrator.py): a tick that fails part-way forgets its reaps, one failed claude agents read declares every worker dead, a parked worker leaves the state machine for good (the stranded PR of shift 2026-11-06-pm), and the merge count trusts a local file instead of GitHub. No further live shift runs until these are closed.

Plan

  • Persist each transition (reap, spawn, close-out start) as it happens, not at the end of the tick; a per-task spawn failure becomes a refusal and the tick carries on; .done/.closeout are deleted only after the write.
  • Treat a failed, timed-out or malformed liveness read as "unknown" and skip all liveness decisions that tick; count a crash only after consecutive unlisted reads or a positive ended state; grace ≥ two measured tick intervals.
  • Keep a parked worker's lease (state parked) so the reaper and the late sweep still pick up its .done.
  • Before counting a merge, freeing a cap slot or marking a close-out refused, ask GitHub for the PR state; a GitHub-confirmed merge counts (and writes its stage-2 row) even with no .closeout file.
  • Fix the KeyError 'spawned' on digest ticks in main() if it is one line.
  • One test per gap, plus the batch conductor's existing suite.

Tier: glance — merge mode: auto-merge on green if the Witness passes, post-merge summary

Detailed implementation plan

Affected Repositories

  • PyAutoBrain (primary)

Branch Survey

Repository Current Branch Dirty?
./organs/PyAutoBrain main clean

Suggested branch: feature/shift-watcher-state-gaps

Implementation Steps (line numbers as of Brain c41d016)

  1. CODE-1 — durable transitions. Reap deletes lease + .done (:446-447) and consumes .closeout, but counters are written only at tick end (:596, :638). Write counters (or an append-only event line) straight after each reap, spawn and close-out start; wrap each per-task spawn in a try/except that records a refusal and continues; unlink .done/.closeout only after the write succeeds.
  2. CODE-2 — liveness unknown. AgentsView._load returns [] on failure/timeout/bad JSON (:325-333). Return a distinct unknown sentinel; when unknown, skip every liveness decision this tick. Declare a crash only after N consecutive unlisted reads (N≥2) or a positive ended state. Raise lease grace (:52, 120 s) to ≥ 2× measured tick interval (median tick 184 s → ≥ 370 s).
  3. ORCH-1 — parked stays in the machine. Parking unlinks the lease (:457); leases() reads *.json only (:411-418); reaper and late sweep (:540-541) never see the parked task's .done. Keep the lease with state: parked; reaper and late sweep also consume .done for parked tasks (outcome, tokens, late-arrival line).
  4. ORCH-3 — GitHub is the merge truth. A close-out that merged and ended before writing .closeout is recorded refused (:824-830) and frees a cap slot (:853-854). Before counting/freeing/refusing, read gh pr view --json state,mergedAt,mergedBy; count a GitHub-confirmed merge with no file and write the stage-2 row from it. Keep the gh call behind the existing fake-worker seam so tests stay offline.
  5. KeyError 'spawned' in main() on digest ticks: fix if one line, else file a draft.

Witness

Four new tests pass and the batch conductor's existing suite still passes: (1) a tick that raises after a reap does not dispatch the finished task again and keeps its outcome; (2) a failing claude agents call leaves both live workers live with crashes 0; (3) park, then .done appears, and the next tick records the outcome, tokens and a late-arrival line; (4) a PR that GitHub reports merged with no .closeout file counts as 1 merge and writes a stage-2 row.

Key Files

  • agents/conductors/batch/_orchestrator.py — the watcher tick, leases, reaper, close-out
  • batch conductor tests (tests/ for _orchestrator)

Unattended: never — a shift must not change the watcher that runs it. Supervised: no shift is live.

Original Prompt

Click to expand starting prompt

Shift watcher: close four state-machine gaps before the next live shift

Type: bug
Target: PyAutoBrain
Repos:

  • PyAutoBrain
    Difficulty: small
    Autonomy: supervised
    Priority: high
    Status: draft
    Filed: 2026-11-10
    Epic: autonomous-orchestrators
    Consequence: glance
    Witness: four new tests pass and the batch conductor's existing suite still passes: (1) a tick that raises after a reap does not dispatch the finished task again and keeps its outcome; (2) a failing claude agents call leaves both live workers live with crashes 0; (3) park, then .done appears, and the next tick records the outcome, tokens and a late-arrival line; (4) a PR that GitHub reports merged with no .closeout file counts as 1 merge and writes a stage-2 row
    Review-minutes: 10
    Unattended: never
    Lane: any

Source: the ecosystem review of 2026-11-10, findings CODE-1, CODE-2, ORCH-1 and ORCH-3, each confirmed by an independent verifier. Report: https://claude.ai/artifact/4A94P1cevvkESoM22QoMWm (private) and Science/scratch/ecosystem_review_2026-11/REPORT.md, Appendix A. Verifier notes: verdicts/CODE-1.json, CODE-2.json, ORCH-1.json, ORCH-3.json in that folder; reproduction scripts in evidence/orch_code/.

All four are in agents/conductors/batch/_orchestrator.py. Line numbers are as of Brain c41d016.

  1. A tick that fails part-way forgets what it already did (CODE-1). The reap deletes a finished worker's lease and .done marker (:446-447) and consumes .closeout files, but counters are written only at the end of the tick (:596, :638). Any exception in between loses the transition. Reproduced: a finished task dispatched again, a second close-out session for the same PR, a merged verdict never counted.
    Fix: write counters (or an append-only event line) straight after the reap and after each spawn and close-out start; catch a per-task spawn failure, record it as a refusal and carry on; delete .done and .closeout only after the write.
  2. One failed liveness read declares every worker dead (CODE-2). AgentsView._load returns an empty list when claude agents --json --all fails, times out or prints bad JSON (:325-333). The lease grace is 120 s (:52) against a measured median tick of 184 s.
    Fix: return a distinct "unknown" result and skip every liveness decision for that tick; require consecutive unlisted reads, or a positive ended state, before a crash is counted; set the grace to at least two measured tick intervals.
  3. A parked worker leaves the state machine for good (ORCH-1). Parking unlinks the lease (:457); leases() reads *.json only (:411-418), so the reaper and the late sweep (:540-541) never read its .done. This is the stranded PR of the 2026-11-06-pm shift.
    Fix: keep the lease with state parked; let the reaper and the late sweep also pick up a .done for any parked task.
  4. The merge count trusts a local file, never GitHub (ORCH-3). A close-out that merges and then ends before writing .closeout is recorded as refused (:824-830) and frees a cap slot (:853-854).
    Fix: before counting a merge, freeing a cap slot or marking a close-out refused, read the PR state (gh pr view --json state,mergedAt,mergedBy). Count a merge GitHub confirms even with no file, and write the stage-2 row from it.

Not in scope: moving shift state off the laptop (that is the cloud watcher design, draft/feature/pyautobrain/shift_watcher_in_the_cloud.md), and the KeyError 'spawned' in main() on digest ticks, which is worth fixing in the same PR if it is one line.

Unattended: never because a shift should not change the watcher that is running it.

Activity

  1. Jammy2211 commented on Nov 12, 2026

    @Jammy2211
    ContributorAuthor

    Heart RED override (development only), recorded per AUTONOMY.md.

    The live human authorized it in the programme session on 2026-11-12: "Override + merge on green". That covers the development-only RED override for this issue and the merge command, on condition that every required check is green within the turn.

    Current RED reasons, verbatim (pyauto-heart readiness, ts 2026-11-12T16:29:40Z):

    • "PyAutoLens: on branch feature/stellar-dark-review (not main)"
    • "PyAutoLens: 2 uncommitted source change(s)"
    • "install verification FAILED (testpypi; checks F)"
    • "release validation FAILED (stage integrate)"

    None of these is in PyAutoBrain. Branch gates passed:

    • Batch suites: 134 passed, 1 skipped. The four witness tests pass.
    • Independent review: CLEAN after one round of fixes.
    • Smoke: not applicable.

    PR: #604

    🤖 Generated with Claude Code

  2. added a commit that references this issue on Nov 12, 2026
  3. Jammy2211 commented on Nov 12, 2026

    @Jammy2211
    ContributorAuthor

    Shipped. PR #604 was merged at 2026-11-12T17:03:58Z (merge commit eecd2d5), with both CI legs green (pytest 3.12 and 3.13).

    Witness: python -m pytest tests/test_orchestrator_*.py tests/test_batch_watch.py -q gives 134 passed, 1 skipped. The four witness tests pass, and every new test fails on the base.

    The four gaps (CODE-1, CODE-2, ORCH-1, ORCH-3) are closed. GitHub's PR state now decides every merge count.

    Follow-ups are filed as PyAutoMind/draft/bug/pyautobrain/shift_watcher_followups_after_state_gaps.md.

    Programme M1.1 is done.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions