Skip to content

Phase 7b: the automatic re-pull dispatch record - #1950

Merged
neuromechanist merged 10 commits into
feature/issue-1046-epic-upstream-freshnessfrom
feature/issue-1913-phase7b-dispatch-record
Oct 25, 2026
Merged

neuromechanist merged 10 commits into
feature/issue-1046-epic-upstream-freshnessfrom
feature/issue-1913-phase7b-dispatch-record

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Builds the record the automatic re-pull path keeps of each dispatch the Worker sends, each run's report back, and the one way an automatic re-pull is recorded (ADR 0111, dated 2026-10-25). Migration 0109 adds upstream_repull_dispatches, a dispatch token kind signed with the callback secret, the report callback POST /webhooks/repull-state, the automatic create in upstream-repull.ts, a watchdog that ends a dispatch the run never reported, and the delete cascade. Nothing here sends a dispatch, schedules anything or merges; those are later slices, so this slice is inert until they land. No mail from any new route or callback.

Phase 7b of epic #1046, part of #1913 (which stays open until Phase 7 completes). It builds on 7a (the switches and caps, merged as aa7234d) and on the merged 6a record.

What is in it

  • backend/src/db/migrations/0109_upstream_repull_automatic.sql: the dispatch table, the scheduler row, and hold_reason and hold_since on upstream_repulls (automatic rows only).
  • backend/src/services/upstream-repull-dispatch.ts (new), backend/src/services/github/callback-tokens.ts (the dispatch token kind), backend/src/routes/callbacks/repull-state.ts (new), backend/src/routes/webhooks/index.ts, backend/src/services/upstream-repull.ts (the automatic create), backend/src/services/deletion.ts (the cascade), backend/src/services/github.ts.
  • Tests: upstream-repull-dispatch.test.ts, upstream-repull-dispatches-migration.test.ts (Miniflare D1), repull-dispatch-token.test.ts, repull-dispatch-words.unit.test.ts, extended upstream-repulls-route.test.ts, the no-mail scan, and the export and webhook route inventory pins.
  • ADR 0111 amendment slice for 7b (last in the file).

Test plan

  • Typecheck and lint clean (the implementer's branch, rebased on the epic head aa7234d).
  • The eight suites that cover this slice: 250 pass, 0 fail.
  • Full bun test run by CI on this head (the lead runs it through the Tests workflow).
  • Only a canary or a production run can check: GitHub's repository_dispatch acceptance and its response shape (7c and 7d), and the watchdog against real queue timing.

Migration 0109: upstream_repull_dispatches (one row per dispatch of
the automatic re-pull, on datasets only, a nonce held exactly while
live, one live dispatch per dataset), the scheduler's one row, and
hold_reason/hold_since on upstream_repulls (automatic rows only, set
together). The shared words for kinds, states, codes, hold reasons,
the planner pin and the copy bound. The dataset cascade deletes the
dispatches before the re-pull rows.

Tested: every CHECK and index on bun:sqlite and Miniflare D1; the
cascade against a loopback GitHub stand-in.
A callback token kind of its own for the automatic re-pull's run:
HMAC-SHA256 with the shared callback secret over a domain line,
the dispatch id, its dataset and its one-shot nonce; a field with a
line break is refused by the signer and fails the verifier.
Re-exported from the services/github barrel; the surface pin gains
the three names.

Tested: round trip, every field bound, and domain separation in
both directions against all eight other token kinds.
The dispatch service: claimOpenDispatch (exact production and the
open switch; one unit of REPULL_DAILY_OPEN_CAP counted in the claim's
own statement; a first dispatch needs no open re-pull, a resume an
automatic row that stalled before pr_open), claimFinishDispatch
(exact production only, no cap), markDispatchSendFailed, the check
an automatic create makes of its dispatch, and the watchdog that
ends a dispatch silent for 8 hours as unreported. POST
/webhooks/repull-state stores a run's report: started once, then
opened, finished, refused or failed, a compare-and-set on the nonce,
one answer for every token refusal, codes from the closed list. No
mail; the no-mail scan gains both roots.

Tested: real SQLite through the webhooks router, the statements on
Miniflare D1, the Resend capture empty, each guard mutated.
POST /admin/repulls takes dispatch_id and dispatch_token, both or
neither; origin is in no request. A create with a live dispatch
whose token the Worker signed records origin automatic, after exact
production, the open switch and an on dataset; the dispatch must
name the dataset, ds and both tags; the plan must be planner 2 with
unexplained, conflicts and corrections stated and 0, and a stated
copy of at most 1 TiB. The insert links the dispatch by the row's
key. An open record is returned only to a caller of its own origin,
and a dispatch that names its record takes only that one.

Tested: the route with real SQLite, each refusal with nothing
written; a scan that every literal refusal word is a dispatch code.
The 7b slice of the Phase 7 amendment: the dispatch record and its
states, production only, the open cap counted at the claim, the
dispatch token and the report callback with its closed code list,
the watchdog, origin automatic only through a Worker-signed
dispatch with the planner pin and the zero-unexplained check, and
the same-origin reading of "the same plan is the same record".

Not tested (prose).
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1950

Version: 0.10.24-PR1950.7faecfa
Commit: 7faecfa


🤖 Updated on each push.

The automatic create checks the token before it says anything about
a dispatch, so an unknown, ended or wrong-token dispatch is one 401.
The link is guarded by liveness and rolls the create back. A claim
that cannot be handed out is ended, an odd tag and an unreadable cap
are their own refusals, the claim audit is built in SQL, and the
watchdog throws on an absent count. Comments match the code.

Tested with the backend dispatch, route and D1 suites.
Covers the token oracle, the callback without switches, the
unconfigured refusals, the stale-dispatch race, resume tags, closed
rows after pr_open, the stranded claim, the link guard, the
watchdog bound and count, the cap's kept units, run URLs and the
Miniflare D1 cap and live races.
States the single token answer, token masking, the cap's scope and
deletion, the guarded link and the stranded-claim end.
The watchdog is called from the production-only daily block, is
declared prod-only in the sweep wiring test, and ADR 0111 records
that its wiring lands in 7b. Tested with the cron wiring suites.
When the claim insert reports no id the dispatch is ended by its
nonce, a failed cleanup logs both errors and throws the original,
and the ADR says a racing create's loser gets the token 401.
Tested with fault injection on the real engine.
@neuromechanist
neuromechanist merged commit 974f7c2 into feature/issue-1046-epic-upstream-freshness Oct 25, 2026
20 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1913-phase7b-dispatch-record branch October 25, 2026 17:28

This branch was previously deployed

1 inactive deployment
npm — 7faecfa9 Deployed Oct 25, 2026 by neuromechanist via publish #3959
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.

1 participant