Skip to content

fix: shift watcher — close four state-machine gaps (#603) - #604

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/shift-watcher-state-gaps
Nov 12, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/shift-watcher-state-gaps

Conversation

@Jammy2211

Copy link
Copy Markdown
Contributor

Summary

Programme task M1.1 (epic autonomous-orchestrators, phase 8). This closes the four state-machine gaps in the shift watcher that the ecosystem review of 2026-11-10 confirmed. Fixes #603.

  • CODE-1, a failed tick forgets what it did. Counters are now written after every reap, spawn and close-out start, and .done, .closeout and lease files are deleted only after that write. A failed spawn becomes a refusal and the tick carries on.
  • CODE-2, one failed liveness read kills every worker. AgentsView.known() is False when the claude agents read fails, times out or returns bad JSON, and that tick then skips every liveness decision. A crash now needs two consecutive unlisted reads or a positive ended state. LEASE_GRACE_S goes from 120 to 420.
  • ORCH-1, a parked worker leaves the state machine. Its lease is kept with state: parked and excluded from the worker cap. The reaper and the late sweep consume its .done and record the outcome, tokens and a late-arrival line.
  • ORCH-3, the merge count trusts a local file. GitHub (gh pr view --json state,mergedAt,mergedBy, behind a pr_state seam) now decides every merge count:
    • A PR GitHub shows MERGED counts even with no .closeout.
    • A merged verdict counts only when GitHub says MERGED. While the PR is OPEN or GitHub can't be read, the slot stays held. A CLOSED PR is a refusal.
    • A non-merged verdict on a PR GitHub shows MERGED is recorded as "merged outside the close-out", with no count and no stage-2 row.
  • main() no longer raises KeyError 'spawned' on digest ticks.

API Changes

None public. tick() gains an optional pr_state= seam. Callers that don't pass it, and spawners without a pr_state method, keep the old behaviour.

Test plan

  • python -m pytest tests/test_orchestrator_*.py tests/test_batch_watch.py -q: 134 passed, 1 skipped (before this branch: 116 passed, 1 skipped).
  • Witness tests, all passing:
    • test_a_tick_that_raises_after_a_reap_keeps_the_outcome_and_never_redispatches
    • test_a_failing_claude_agents_read_leaves_live_workers_live[rc1|timeout|badjson]
    • test_a_parked_worker_that_finishes_after_review_at_is_a_late_arrival
    • test_a_github_merged_pr_with_no_closeout_file_counts_as_one_merge
  • 18 new or rewritten tests, all of which fail against the base source. The independent reviewer checked this.
  • The review's reproduction scripts (evidence/orch_code/) reproduce each bug on the base and not on this branch.
  • Full suite: 1659 passed, 1 skipped, 1 failed. The failure, test_grouped_organ_consumers.py::test_hygiene_map_and_heart_board_find_grouped_checkouts, also fails on the base. It is caused by the PYAUTO_* variables that activate.sh sets.
  • Independent review (fresh Opus context, review faculty rubric): FINDINGS on ab48caa, with two must-fix items on ORCH-3, fixed in 1898e11. Re-review: CLEAN.
  • Smoke: not applicable (organ code, no workspace).
  • Follow-ups are out of scope and filed as PyAutoMind/draft/bug/pyautobrain/shift_watcher_followups_after_state_gaps.md.

Heart RED override (development only)

Authorization from the live human in the programme session, 2026-11-12: "Override + merge on green". This authorizes the development-only RED override for #603 and gives the merge command, conditional on every required check being green within the turn.

Heart verdict RED (pyauto-heart readiness, ts 2026-11-12T16:29:40Z). The current RED reasons, verbatim:

  • "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 reasons is in PyAutoBrain. Branch gates passed: tests (above), review CLEAN, smoke not applicable. This PR does not fix Heart, and Heart stays RED for release.

Tier: glance. Merge mode: auto-merge on green if the Witness passes, with a post-merge summary. A RED override additionally needs the human's merge command, which was given in the same turn.

🤖 Generated with Claude Code

Jammy2211 and others added 2 commits November 12, 2026 16:43
- CODE-1: every reap, park, crash, spawn and close-out start/settle is
  written to counters.json as it happens; a .done / .closeout file and a
  lease are deleted only after that write. A per-task spawn failure (or a
  close-out that cannot start) is recorded as a refusal and the tick
  carries on instead of raising.
- CODE-2: a failed, timed-out or malformed `claude agents` read is
  unknown (AgentsView.known() False), not an empty list; every liveness
  decision is skipped that tick. A real worker is a crash only after
  UNLISTED_READS_FOR_CRASH=2 consecutive unlisted reads past the grace, or
  a positive ended/done state. LEASE_GRACE_S 120 -> 420 (>= 2 x the
  measured 184 s median tick).
- ORCH-1: parking keeps the lease with state `parked` (holding no worker
  slot); the reaper and the late sweep consume a parked task's .done
  (outcome, tokens, late-arrival line) and release a parked session that
  ends without one (never a crash, never re-dispatched).
- ORCH-3: before a finished close-out is counted merged or refused (which
  frees its merge-cap slot) the PR state is read from GitHub
  (gh_pr_state: `gh pr view --json state,mergedAt,mergedBy`, behind a
  pr_state seam on tick() and the spawners; FakeSpawner never calls gh).
  A GitHub-confirmed merge counts and writes its stage-2 row with or
  without a .closeout; an unreadable GitHub keeps the slot held.
- main(): no KeyError on digest/late ticks that carry no `spawned`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A `merged` .closeout counts only when GitHub says MERGED. GitHub OPEN
  (e.g. --auto pending) or unreadable: not counted, no stage-2 row, the
  cap slot stays held and is re-checked each tick. GitHub CLOSED: refused,
  "close-out said merged, GitHub closed".
- A refused (non-merged) verdict on a PR GitHub shows MERGED was merged
  by someone else: never counted in glance_auto_merges, no stage-2 row,
  slot freed, recorded as "merged outside the close-out (mergedBy=...)"
  (event list `merged_outside`).
- No .closeout + GitHub MERGED still counts as one unattended merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jammy2211
Jammy2211 merged commit eecd2d5 into main Nov 12, 2026
2 checks passed
@Jammy2211
Jammy2211 deleted the feature/shift-watcher-state-gaps branch November 12, 2026 17:04
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.

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

1 participant