Skip to content

fix(dispatcher): reclaim dead sessions without waiting on the cron #288

Closed

Description

@Ardnived

Summary

The residue sweeper's */30 cron in each repo's workflow-state.yml now fires about 3.4 times a day, with gaps of 7 hours or more. A lock past its deadline is only reclaimed by that cron, so a dead session holds its item for up to 10 hours, and three attempts can take more than a day before anyone is told. On 2026-11-20 this turned an overnight usage limit into a stall that lasted all day: the SahajCloud locks that expired at 17:26Z were still held at 22:16Z, and were cleared only by a manual workflow_dispatch. The same cron is the only thing that clears a missed unlock: a session that removed its own lock, but whose unlock run failed, leaves its lease ref behind, and every later fire for that item is refused until the next sweep.

Approach

  1. Reclaim expired locks on every event run. In resolve (dispatcher/resolve.mjs:63-71), an event run adds reconcileMaxPerEvent drift targets, but a locked item reads Working on the board, so driftTargets (dispatcher/reconcile.mjs:113-127) never lists it. Factor the lock loop at the top of listSweepTargets (dispatcher/sweep.mjs:26-36) into a helper, for example expiredLockTargets, that takes the locked items rather than querying for them. The event run already calls cheapView, and every item it returns carries its labels (dispatcher/reconcile.mjs:64-81), so it passes the items whose labels hold labels.lock and needs no second label query. Append the sweep-timeout targets before the drift ones, both bounded by one shared reconcileMaxPerEvent cap and the same skip set, so the event's own item is never swept. The only new cost is one record read per locked item, and on a normal day only a handful are locked. cheapView returns nothing on a read failure, so that pass reclaims nothing and the cron is still the backstop. CI status and check_suite events arrive many times an hour across the five repos, so an expired lock is reclaimed within minutes while the loop is busy.
  2. Read a missed unlock on the item's own event. When an event reaches an item that carries no lock, but whose record still holds current and was not unlocked by the dispatcher itself (ownUnlock), route (dispatcher/decide.mjs:1143) routes the unlock first (decide.mjs:1362), and then the event as usual. reconcile (decide.mjs:1469-1473) and sweep-lease (decide.mjs:1447-1452) already read the record this way. The unlocked action (dispatcher/apply.mjs:183) then releases the lease, archives current and drops a refused pending, before any fire tries the lease. Two events on WeMeditateWeb#326 missed this. A Cloudflare comment (23:10Z) set the board to Pending without the unlock, so the board no longer drifted and no reconcile target came. Then a member's address (23:32Z) was refused with "another pass holds the lease".
  3. Keep the cron as the backstop for quiet hours, and move */30 * * * * (.github/workflows/workflow-state.yml:41) off the top of the hour (GitHub drops or delays schedule runs most at :00). Only the claude-workflow copy of workflow-state.yml changes here. Each product repo's own copy is a one-line follow-up there.
  4. Docs. Update docs/why.md#the-reconciler-replaces-the-drift-window with the measured cadence and the event-driven reclaim, and the "The schedule is also not the schedule" paragraph (docs/why.md:1575, under #the-journal-tally-needs-a-writer-that-cannot-stop), which still reads "about eight a day" from the 2026-09-09 to 2026-10-06 window. No test asserts either figure, so each is replaced in place. Update docs/why.md#a-lease-past-its-deadline-is-reclaimed to say that any event on the item reads a missed unlock.

Acceptance criteria

  • An event run in a repo emits sweep-timeout for every locked item in that repo whose record deadline has passed, up to dispatch.reconcileMaxPerEvent, and never for the event's own item.
  • A lock still inside its deadline yields no target from an event run, and a locked item with no record is swept, exactly as the cron does today.
  • An event on an unlocked item whose record still holds a session (current) first runs that session's unlock: the lease is released and current is archived, and the event is then handled as usual. A fire it calls for is not refused by the stale lease.
  • An item the dispatcher unlocked itself (ownUnlock), and an item with no current, are handled exactly as before.
  • Scheduled and workflow_dispatch runs behave exactly as before.
  • The claude-workflow sweeper cron no longer fires at :00 or :30.
  • docs/why.md records the measured cadence and the new behaviour.

Verification checklist

Decisions

  • Should a reliable outside clock also drive the sweep? → A — no, events plus the cron are enough. No new service and no new secret. Expired locks clear within minutes while there is activity, and a quiet night waits for the cron, where few sessions run. (Ardnived, 2026-11-21)

Notes

Measured from schedule runs of workflow-state.yml, 2026-11-13 to 2026-11-20:

Repo Runs / day Median gap Longest gap
SahajCloud 3.4 7.3h 10.3h
SahajAtlasWeb 3.5 7.2h 9.7h
WeMeditateWeb 3.4 6.9h 10.2h
SahajAtlasWordpress 3.4 7.4h 10.3h
claude-workflow (four crons) 6.5 3.5h 8.7h

docs/why.md measured about 8 runs a day for 2026-09-09 to 2026-10-06, so the cadence has fallen by half since then.

Related: #268 stops charging an attempt to a session that is blocked on a prompt or never started. It reads session status during the sweep, so this ticket decides how soon that check runs.

The missed unlock, sydevs/WeMeditateWeb#326 on 2026-11-20. sydevs-bot's REST quota (5,000/hr) ran out from 23:00Z to the 23:10Z reset, and every dispatcher run in that window failed. One of them was the pull_request_target.unlabeled run (23:01:35Z) for the review-pr session that had just removed its lock. So refs/sydevs-lease/326 stayed until a manual workflow_dispatch at 23:56Z, where sweep-lease read the unlock and fired the refused address-review. Step 2 would have cleared it on the 23:32Z comment. The exposure is the same as the rule's two current users: the cross-repo window in the sweep-lease comment (decide.mjs:1444-1446) is unchanged, because events for one item in one repo are already serialized by the dispatch-<repo>-<n> concurrency group.

A scheduled run can read cancelled when one matrix leg's pending job is replaced in its dispatch-<repo>-<n> concurrency group (SahajCloud, 2026-11-20 07:36Z and 16:40Z). The newer run covers that item, so this is expected and is not part of this ticket.

Activity

  1. added theissue type on Nov 20, 2026
  2. changed the title [-]fix(dispatcher): reclaim expired locks without waiting on the cron[/-] [+]fix(dispatcher): reclaim dead sessions without waiting on the cron[/+] on Nov 21, 2026
  3. Ardnived commented on Nov 21, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot implement

  4. sydevs-bot commented on Nov 21, 2026

    @sydevs-bot
    Contributor

    Not started: 1 open question(s) in the body still need an answer. Reply @sydevs-bot revise with your choices (for example 1A), then say @sydevs-bot implement again.

    🤖 sydevs dispatcher

  5. Ardnived commented on Nov 21, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot revise 1A

  6. added
    bot:working
    on Nov 21, 2026
  7. sydevs-bot commented on Nov 21, 2026

    @sydevs-bot
    Contributor

    🤖 no session running
    approved by Ardnived

    recent dispatches
    • 02:17Z implement · attempt 1 · session · done
    • 02:17Z implement · attempt 1 · session
    • 00:29Z revise · attempt 1 · session · done
    • 00:29Z revise · attempt 1 · session

    Written by the sydevs dispatcher. Sessions never edit this comment.

  8. moved this from Awaiting to Working in SYDevelopers Technical Roadmapon Nov 21, 2026
  9. sydevs-bot commented on Nov 21, 2026

    @sydevs-bot
    Contributor

    Answered 1A: events plus the cron, no outside clock. The question is now a decision in the body, and none is left open.

    Grounding the body against the code corrected five things:

    • The export is resolve, not resolveTargets (dispatcher/resolve.mjs:63-71).
    • route and its unlock case moved when feat(review): a fresh streamliner justifies every new piece, and the critic seconds each question #293 landed: decide.mjs:1143 and :1362, and the sweep-lease window comment is :1444-1446.
    • Step 1 needs no extra label query. cheapView already returns each item's labels (dispatcher/reconcile.mjs:64-81), so the helper takes the locked items.
    • The sweeper cron is workflow-state.yml:41, and the stale cadence figure is docs/why.md:1575. No test asserts it.
    • The gate line is the repo's lean gate, not claude plugin validate ..

    Say @sydevs-bot implement to start the work.

    🤖 Written by the sydevs autonomous loop — see what this is


    Generated by Claude Code

  10. removed
    bot:working
    on Nov 21, 2026
  11. moved this from Working to Awaiting in SYDevelopers Technical Roadmapon Nov 21, 2026
  12. Ardnived commented on Nov 21, 2026

    @Ardnived
    ContributorAuthor

    @sydevs-bot implement

  13. added
    bot:working
    on Nov 21, 2026
  14. moved this from Awaiting to Working in SYDevelopers Technical Roadmapon Nov 21, 2026
  15. removed
    bot:working
    on Nov 21, 2026
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

    Fields

    Priority

    High

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions