|
| 1 | +# Scheduler duplicate-candidate stranding proof contract |
| 2 | + |
| 3 | +## Claim |
| 4 | + |
| 5 | +`selectDueCandidates` fills the review batch up to capacity from the candidates |
| 6 | +it is given, even when the same item appears more than once in the due list. |
| 7 | +Before this change, a run of duplicates could end selection early and strand |
| 8 | +every remaining candidate while capacity was still free. |
| 9 | + |
| 10 | +## Exercised surface |
| 11 | + |
| 12 | +`run-proof.sh` drives the real `plan` CLI end to end — **no application function |
| 13 | +is substituted**: |
| 14 | + |
| 15 | +``` |
| 16 | +node dist/clawsweeper.js plan |
| 17 | + -> createReviewPlanningSelection().selectCandidates() |
| 18 | + -> fetchOpenItemPage() -> ghJsonLines() -> spawnSync -> `gh` subprocess |
| 19 | + -> JSON-lines parse -> item mapping |
| 20 | + -> dueCandidate() -> bucket / priority / due derivation |
| 21 | + -> selectDueCandidates() |
| 22 | +``` |
| 23 | + |
| 24 | +Only the `gh` **binary** is replaced, standing in for GitHub's server. |
| 25 | +ClawSweeper's own transport — subprocess spawn, argument construction, |
| 26 | +JSON-lines parsing, pagination loop — runs unchanged. The `selectedKeys` dedup |
| 27 | +set inside `selectDueCandidates` exists precisely because duplicate page entries |
| 28 | +are expected. |
| 29 | + |
| 30 | +## Controlled scenario and fixture |
| 31 | + |
| 32 | +Three distinct open PRs, `--batch_size 10` (far above the due count, so capacity |
| 33 | +is never the limiting factor), `--max_pages 5`. The stub `gh` returns `#1` on four |
| 34 | +consecutive pages, reproducing the `updated`-sorted pagination race in which an |
| 35 | +item touched mid-scan is listed again on later pages: |
| 36 | + |
| 37 | +``` |
| 38 | +page 1: [#1, #2] page 2: [#1, #3] page 3: [#1] page 4: [#1] page 5+: [] |
| 39 | +``` |
| 40 | + |
| 41 | +Four entries are needed because these land in `hot_pull_request`, whose weight is |
| 42 | +2 — a stall requires one whole weighted pass to consume nothing but duplicates. |
| 43 | +See **Reachability** below for the per-bucket thresholds. |
| 44 | + |
| 45 | +### Two details that are load bearing |
| 46 | + |
| 47 | +Both of these silently turn the proof vacuous — it passes on a pre-fix build and |
| 48 | +demonstrates nothing. Do not "simplify" either one away. |
| 49 | + |
| 50 | +1. **Clock.** `selectCandidates()` calls `selectDueCandidates()` **without** a `now` |
| 51 | + argument, so the scheduler uses the real `Date.now()`. Candidates must be recent |
| 52 | + enough to stay *out* of the weekly-coverage preselect lane (which needs 6 days). |
| 53 | + An earlier version used a fixed past timestamp; every candidate then qualified as |
| 54 | + weekly-coverage-due and was taken by the plain |
| 55 | + `for (const candidate of weeklyCoverageDue) take(candidate)` loop, which never |
| 56 | + reaches the weighted drain. |
| 57 | + |
| 58 | +2. **Prior reports.** Items with no existing report are *coverage-untracked* and go |
| 59 | + through `takeWeighted`, whose cohort is a separate copy of the bucket lists — the |
| 60 | + final loop then recovers the survivors and nothing is stranded. The fixture |
| 61 | + therefore writes a prior report per item so the candidates are coverage-tracked |
| 62 | + and the final weighted drain owns selection. |
| 63 | + |
| 64 | +## Expected observation |
| 65 | + |
| 66 | +`shards[0].itemNumbers` from the real `plan` CLI, same fixture and invocation: |
| 67 | + |
| 68 | +| build | selected | dueBacklog | |
| 69 | +|---|---|---| |
| 70 | +| pre-fix | `[1]` — `#2` and `#3` stranded | 6 | |
| 71 | +| post-fix | `[1, 2, 3]` | 6 | |
| 72 | + |
| 73 | +The proof also asserts the selection stays deduplicated, so the fix cannot be |
| 74 | +satisfied by simply letting the duplicate through. |
| 75 | + |
| 76 | +## Artifact and command |
| 77 | + |
| 78 | +Supported-environment run (Node 24, Crabbox `local-container`). `run-proof.sh` |
| 79 | +drives the **real `plan` CLI** with only the `gh` binary stubbed: |
| 80 | + |
| 81 | +```bash |
| 82 | +crabbox run \ |
| 83 | + --provider local-container \ |
| 84 | + --local-container-image node:24 \ |
| 85 | + --no-hydrate \ |
| 86 | + --timing-json \ |
| 87 | + --artifact-glob '.artifacts/scheduler-duplicate-proof/**' \ |
| 88 | + --script docs/proof/scheduler-duplicate-candidate-stranding/run-proof.sh |
| 89 | +``` |
| 90 | + |
| 91 | +It installs the pinned pnpm into a user-writable prefix (the lease runs as the |
| 92 | +unprivileged `crabbox` user, so `corepack enable` cannot symlink into |
| 93 | +`/usr/local/bin`), builds, runs the planner fixture, and runs the focused suite. |
| 94 | + |
| 95 | +Host-only quick check of the selection logic (injects `fetchOpenItemPage` rather |
| 96 | +than the `gh` binary, so it does **not** exercise the transport): |
| 97 | + |
| 98 | +```bash |
| 99 | +pnpm run build |
| 100 | +node docs/proof/scheduler-duplicate-candidate-stranding/run-proof.mjs # exit 0 = PASS |
| 101 | +``` |
| 102 | + |
| 103 | +## Provenance |
| 104 | + |
| 105 | +- provider: Crabbox `local-container` (Docker/OrbStack) |
| 106 | +- crabbox: `0.15.0` |
| 107 | +- image: `node:24` @ `sha256:934240a162082fd8b8a2f90cd5114446443f1eba1c5378f6687167ca405e6584` |
| 108 | +- container node: `v24.19.0` (satisfies `engines.node >= 24`) |
| 109 | +- lease: `cbx_7c89c5c91f66` (`brave-lobster`) |
| 110 | +- run: `run_71734de37d18` |
| 111 | +- artifact: `.crabbox/runs/run_71734de37d18/run_71734de37d18-artifacts.tgz` |
| 112 | + (`plan.json`, `summary.json`, `focused-tests.txt`, `install.log`, `build.log`) |
| 113 | +- result: exit `0`; selected `[1,2,3]`, `dueBacklog` 6, deduped; focused suite `24/24` |
| 114 | +- privacy: synthetic `contributor` login and public-shaped item numbers only. The |
| 115 | + stub `gh` makes no network call; the proof performs no queue, GitHub, or |
| 116 | + production mutation, and no credential is present in the lease. |
| 117 | + |
| 118 | +## Reachability |
| 119 | + |
| 120 | +A stall needs a whole weighted pass to consume nothing but duplicates, so the |
| 121 | +threshold is per bucket. Derived by driving the real `dueCandidate()` logic |
| 122 | +against a pre-fix build: |
| 123 | + |
| 124 | +| bucket | weight | page entries for one item needed to strand | |
| 125 | +|---|---:|---:| |
| 126 | +| `hot_pull_request`, `activity`, `recent_issue` | 2 | 4 | |
| 127 | +| `daily_pull_request` | 3 | 6 | |
| 128 | +| `hot_issue` | 4 | 8 | |
| 129 | +| `weekly_issue` | 1 | unreachable | |
| 130 | + |
| 131 | +`weekly_issue` is unreachable because "due" and "weekly-coverage-due" share the |
| 132 | +same 6-day threshold there, so the coverage preselect lane always claims it. |
| 133 | + |
| 134 | +Focused tests: |
| 135 | + |
| 136 | +```bash |
| 137 | +node --test test/scheduler-policy.test.ts |
| 138 | +``` |
| 139 | + |
| 140 | +Red/green was verified by swapping only the compiled `dist/scheduler-policy.js` |
| 141 | +for a pre-fix build of the same file and re-running the focused suite with the new |
| 142 | +tests present: 4 fail pre-fix, 24/24 pass post-fix, with all 20 pre-existing |
| 143 | +assertions unchanged in both directions. |
| 144 | + |
| 145 | +## Limits |
| 146 | + |
| 147 | +Covers the batch-selection path only. The duplicate is injected at the `gh` |
| 148 | +**binary** boundary rather than produced by a real pagination race, since that race |
| 149 | +is timing dependent and not reproducible on demand — so GitHub's own server |
| 150 | +behavior is modeled, not exercised. No live GitHub, Worker, or queue is contacted, |
| 151 | +and the proof performs no mutation. The lease is a local Docker container, not a |
| 152 | +cloud host, so it proves the Linux/Node 24 runtime but not cross-host isolation. |
| 153 | + |
| 154 | +The fix changes only the loop's termination signal (candidates drained instead of |
| 155 | +candidates selected). It does not deduplicate the `due` list up front, does not |
| 156 | +change bucket weights, ordering, or the dedup key, and does not change behavior at |
| 157 | +all for a due list with no duplicates. A duplicate still consumes one weighted |
| 158 | +slot in its pass; only the premature `break` is removed. |
0 commit comments