Skip to content

fix(admin): batch param is unclamped and batch=0 spins 50 no-op batches in all=true mode #28

Closed

Description

@mrgoonie

Summary

The batch query parameter is neither clamped nor range-checked on POST /api/admin/recompute. parseIntSafe rejects only NaN and negative values, so batch=0 is accepted and there is no upper bound either. With all=true, batch=0 makes one request burn all 50 permitted batch iterations while processing nothing.

Current behavior

  • apps/web/app/routes/api.admin.recompute.ts:28-32 — the only validation, which lets 0 through and caps nothing:
    function parseIntSafe(value: string | null, fallback: number): number {
      if (value == null) return fallback;
      const n = parseInt(value, 10);
      return Number.isNaN(n) || n < 0 ? fallback : n;
    }
  • apps/web/app/routes/api.admin.recompute.ts:58 — that value becomes batchSize and reaches .limit(batchSize) in the query builder, so batch=0 becomes LIMIT 0.
  • apps/web/app/lib/leaderboard/recompute-all-skills.ts:177 — with an empty batch the completion test is 0 < 0, which is false:
    const isComplete = batch.length < batchSize;
    The batch is reported as incomplete, and nextOffset is set to offset unchanged, so the all=true loop at apps/web/app/routes/api.admin.recompute.ts:81 keeps calling with the same offset until MAX_BATCHES_PER_REQUEST (50) is reached, then returns "Paused after 50 batches" with nothing processed.
  • There is also no upper bound: batch=100000 is accepted and would defeat the guard added at line 21 to keep the Worker under its CPU limit.

Expected behavior

Clamp the value to a sane range, as the review recommended, and reject nonsense input explicitly:

const batchSize = Math.min(Math.max(parseIntSafe(batch, 100), 1), 500);

Reproduction

  1. Set batch to 0 and select all-at-once mode:
    ADMIN_SECRET=... curl -sS -X POST "https://skillx.sh/api/admin/recompute?all=true&batch=0"
  2. The response reports batches: 50, totalProcessed: 0, and the "Paused after 50 batches" message, while GET /api/admin/recompute shows the checkpoint unchanged.

Conditions: requires ADMIN_SECRET plus the D1 and KV bindings; admin-only endpoint. Behavior read from main at 9d0f9e9; the exact request was not executed against production.

Flow

flowchart LR
  A["batch=0"] --> B[parseIntSafe returns 0]
  B --> C["LIMIT 0 -> empty batch"]
  C --> D{{"isComplete = 0 < 0 = false"}}
  D -->|nextOffset unchanged| E[all=true loop retries same offset]
  E --> F[50 no-op batches, then Paused]
Loading

Evidence

  • Affected code: apps/web/app/routes/api.admin.recompute.ts:28, apps/web/app/routes/api.admin.recompute.ts:58, apps/web/app/routes/api.admin.recompute.ts:81, apps/web/app/lib/leaderboard/recompute-all-skills.ts:177
  • Introduced by: 88a5bff, shipped on main via 9d0f9e9 (PR feat: skill references & scripts support #9)
  • Related: plans/reports/code-reviewer-260306-1531-admin-recompute-endpoint.md, section "8. Input validation for batch and offset params"

Open questions

  • Upper bound: 500 as suggested in the review, or align it with the existing default of 100?
  • Should batch=0 return HTTP 400 instead of being silently clamped to 1?

Notes

  • offset has the same missing upper clamp, but an out-of-range offset only yields an empty batch and a correct completion, so it is far less harmful than batch=0.
  • MAX_BATCHES_PER_REQUEST at line 21 already handles the Worker-timeout risk from the review; the missing piece is bounding the work each iteration can request.

Activity

  1. clark-cant commented on Oct 30, 2026

    @clark-cant

    Verified on main c95ab77 (2026-10-30) — still reproducible, no fix in flight

    Read apps/web/app/routes/api.admin.recompute.ts and apps/web/app/lib/leaderboard/recompute-all-skills.ts at c95ab77. Nothing clamps batch, and no open PR or issue covers it (duplicate check across all PR/issue states for "recompute" and "batch").

    Trace, exactly as reported

    • parseIntSafe (api.admin.recompute.ts:28-32) still rejects only NaN and negatives, so batch=0 yields 0 and batch=100000 yields 100000. A non-numeric value silently falls back to 100, which hides typos.
    • :58 passes it straight into recomputeBatch, which uses .limit(batchSize).
    • recompute-all-skills.ts:177 computes isComplete = batch.length < batchSize; with batchSize=0 an empty batch gives 0 < 0 = false, so nextOffset becomes offset + 0 and stays non-null.
    • The all=true loop at :81 re-requests the same offset until MAX_BATCHES_PER_REQUEST (50), then answers "Paused after 50 batches" with totalProcessed: 0. Single-batch mode answers Batch done. Next offset: 0, so a polling client loops forever.

    Recommended fix (two layers)

    1. Route: validate explicitly, clamp only the top. Named constants beside MAX_BATCHES_PER_REQUEST:
    const DEFAULT_BATCH_SIZE = 100;
    const MAX_BATCH_SIZE = 500;

    Return 400 { error: "invalid_batch" } when the parameter is present but not a positive integer (parseInt result <= 0 or NaN), and clamp the upper bound with Math.min(n, MAX_BATCH_SIZE). Silently clamping 0 to 1 would leave a broken caller looking healthy, so the 400 is the better answer to the open question in this issue. There is already a clamp-with-named-constants precedent in apps/web/app/lib/search/compatibility-filter.ts:41-47.

    1. recomputeBatch: defense in depth so the loop cannot spin even if a caller bypasses the route — treat an empty batch as complete (isComplete = batch.length === 0 || batch.length < batchSize), or coerce batchSize to at least 1 at the top of the function. This also makes the isComplete rule consistent with newOffset = offset + batch.length.

    Regression test

    Route actions are already unit-tested by importing action({ request, context }) with a mocked db (api.cookie-mutation-origin.test.ts, api.mcp.test.ts). A new api.admin.recompute.test.ts asserting batch=0 gives 400, batch=999999 reaches the mocked recomputeBatch as 500, and an absent batch gives 100 would pin all three behaviours.

    Scope note: recompute-all-skills.ts is 210 LOC against the 200 limit in docs/code-standards.md:9, which is tracked separately as #27. This fix adds about two lines to the route and one to the lib file, so it does not need to wait for the split. The KV concurrency guard in #26 is a separate concern and does not interact with it.

    Posted by the github-maintain skill at 2026-10-30T01:07:00Z

  2. zuey-cloud-harness commented on Oct 31, 2026

    @zuey-cloud-harness
    Contributor

    🤖 AKAdmin is picking this up.

    • Claimed: 2026-10-31 19:00 (Asia/Saigon)
    • Plan: reject non-positive/non-numeric batch, clamp to 1..500 in apps/web/app/routes/api.admin.recompute.ts, and add a regression test for batch=0 and batch=100000.
    • Branch: fix/akadmin-admin-recompute-batch-clamp

    Label akadmin-wip added so this does not get picked up twice. If a human wants it instead, say so here and I'll stand down.

  3. added
    akadmin-wipAKAdmin is implementing this right now — do not start parallel work
    on Oct 31, 2026
  4. zuey-cloud-harness commented on Oct 31, 2026

    @zuey-cloud-harness
    Contributor

    AKAdmin progress — fix pushed, PR open.

    Branch fix/akadmin-admin-recompute-batch-clamp, commit d2e7c44, PR #169: #169

    What changed:

    • New apps/web/app/lib/admin/batch-limit.ts — parseBatchSize(raw, limits) returns the fallback (100) for anything absent, blank, non-finite or below 1, clamps above 500, floors fractions. It uses Number rather than parseInt so "1e9" is treated as a magnitude and clamped instead of being truncated to 1.
    • New apps/web/app/lib/admin/batch-limit.test.ts — vitest cases for fallback, zero/negatives, in-range, fractions, the cap, and a caller-supplied bound.
    • apps/web/app/routes/api.admin.recompute.ts now calls parseBatchSize for batch; parseIntSafe is kept for offset only and its doc comment says so. File is 133 lines, under the 200 LOC limit in CLAUDE.md.

    The parser was checked against 21 inputs with Node type stripping, all passing. The full vitest suite was not run here because the workspace had no node_modules; CI will cover it.

    Claimed 2026-10-31, Asia/Saigon. akadmin-wip label stays on until the PR merges.

  5. mrgoonie commented on Oct 31, 2026

    @mrgoonie
    ContributorAuthor

    CI is green on the fix: web-contracts-cli passed, and the PR is now CLEAN / MERGEABLE — #169

    Keeping the akadmin-wip label on until this merges, then I will drop it.

  6. clark-cant commented on Oct 31, 2026

    @clark-cant

    Status — fix is in flight at d2e7c44 (PR #169)

    Re-checked the state at 2026-10-31T13:10Z.

    What landed on the branch

    • PR fix(admin): clamp the recompute batch size to 1..500 #169, 3 files, +99/-1: new apps/web/app/lib/admin/batch-limit.ts, its test, and api.admin.recompute.ts now calling parseBatchSize for batch while parseIntSafe stays for offset.
    • mergeable: MERGEABLE, mergeStateStatus: CLEAN, web-contracts-cli SUCCESS at 12:11:33Z, reviewDecision empty.
    • The clamp matches the bounds from the earlier review: RECOMPUTE_BATCH_LIMIT = { max: 500, fallback: 100 }, Number instead of parseInt so 1e9 is read as a magnitude and clamped rather than truncated to 1, fractions floored because .limit() needs an integer, and 0 / negative / blank / non-finite falling back to 100. The upper-bound half is therefore closed: batch=100000 can no longer defeat the Worker budget.

    Two things to settle before this closes

    1. The open question in the issue — 400 or silent clamp — was answered by clamping. batch=0 now yields 100 with HTTP 200, so a caller that sent a broken value looks healthy. Both answers are defensible, but if the loud one is wanted it is a small change: parseBatchSize already distinguishes unusable input (!Number.isFinite(trimmed) or floored < 1) from an absent parameter, so returning that distinction to the caller and answering 400 for the former is a few lines, not a redesign.
    2. The defence-in-depth half is still missing in both in-flight PRs. recompute-all-skills.ts keeps isComplete = batch.length < batchSize at refactor(leaderboard): extract one pure score builder for both recompute paths #170's head as well, so once fix(admin): clamp the recompute batch size to 1..500 #169 merges, a caller that bypasses the route — a script, a worker cron, a direct import — can still hit the 50-no-op-batch spin with batchSize: 0. One line closes it (batch.length === 0 || batch.length < batchSize), or coerce batchSize to at least 1 at the top of recomputeBatch. Cheapest moment is right after fix(admin): clamp the recompute batch size to 1..500 #169 lands, while the loop is already open.

    No file overlap between #169 and #170 (api.admin.recompute.ts + new files vs. the leaderboard modules), so neither needs a rebase.

    Keeping this open with akadmin-wip per your note. Closing condition: #169 merged, plus ideally the empty-batch guard above.

    Posted by the github-maintain skill at 2026-10-31T13:10:00Z

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

    akadmin-wipAKAdmin is implementing this right now — do not start parallel workbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions