{"record":{"id":"2e61d209ec2e6218","repo":"koala73/worldmonitor","slug":"rundailyramp-inflight-bounds-check-invariant-vio","errorCode":null,"errorMessage":"[runDailyRamp] inFlight bounds-check invariant violated","messagePattern":"\\[runDailyRamp\\] inFlight bounds-check invariant violated","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"convex/broadcast/rampRunner.ts","lineNumber":1058,"sourceCode":"    // module so the operator-recovery commands (`recoverFromPartialFailure`,\n    // `clearPartialFailure`, `forceReleaseLease`) keep working on any\n    // legacy partial-failure rows. New runs go through the state machine.\n    //\n    // In-flight guard: refuse to start a new wave while any waveRuns row is\n    // active. The new state machine maintains its own `pendingRunId` lease\n    // on `broadcastRampConfig` via `_claimWaveRunLease` — this guard is\n    // belt-and-suspenders against a force-cleared lease leaving an orphaned\n    // active waveRuns row.\n    const inFlight = await ctx.runQuery(\n      internal.broadcast.waveRuns._listInFlightWaveRuns,\n      {},\n    );\n    if (inFlight.length > 0) {\n      const top = inFlight[0];\n      // top is guaranteed non-null by the length check (silences\n      // noUncheckedIndexedAccess and protects future code changes that\n      // might break the bounds check).\n      if (!top) throw new Error(\"[runDailyRamp] inFlight bounds-check invariant violated\");\n      const ageMin = (Date.now() - top.lastActivityAt) / 60_000;\n      if (ageMin >= 15) {\n        console.error(\n          `[runDailyRamp] STALLED waveRun runId=${top.runId} status=${top.status} (last activity ${ageMin.toFixed(1)}min ago) — operator must resume (resumeStalledWaveRun / resumeFinalizeWaveRun) or discard (discardWaveRun) before next tick.`,\n        );\n        return { status: \"stalled-wave-run\", detail: top.runId };\n      }\n      console.log(\n        `[runDailyRamp] wave already in flight (runId=${top.runId} status=${top.status}, last activity ${ageMin.toFixed(1)}min ago) — skip`,\n      );\n      return { status: \"wave-in-flight\", detail: top.runId };\n    }\n\n    // Belt-and-suspenders lease check. `_listInFlightWaveRuns` only returns\n    // runs in active states (picking/segment-created/pushing/broadcast-created)\n    // — it does NOT include `failed` runs. But `failed` runs may STILL hold\n    // the lease (e.g. `persist-failed`, `segment-create-failed`,\n    // `batch-failure-rate-exceeded` all preserve the lease until the","sourceCodeStart":1040,"sourceCodeEnd":1076,"githubUrl":"https://github.com/koala73/worldmonitor/blob/ffec79ac339946fd2d24e85845da5755dcaa534b/convex/broadcast/rampRunner.ts#L1040-L1076","documentation":"In runDailyRamp, after confirming inFlight.length > 0 the code reads inFlight[0] and defensively guards against it being undefined (silences noUncheckedIndexedAccess and protects against a future change breaking the length check). This throw is an invariant violation: it cannot fire under normal JS semantics if the length check is intact.","triggerScenarios":"A future code change breaks the inFlight.length > 0 guard without updating this read; an exotic concurrency/retry artifact where the array mutated between the check and the index; effectively unreachable in correct code.","commonSituations":"Code regression introduced during a refactor of the in-flight guard; should never occur in a correct build.","solutions":["Treat as a code defect: review the surrounding inFlight length check and index logic for a regression.","If seen in production, capture the waveRuns query result and report — it indicates a logic bug, not an operational condition.","No operator recovery action is appropriate; fix the code."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Invariant guard — should never fire; if it does, capture diagnostics\ntry {\n  // ... the in-flight read path ...\n} catch (e) {\n  if ((e as Error).message.includes('inFlight bounds-check invariant violated')) {\n    // capture full waveRuns state and file a bug — this is a code defect, not an op condition\n    console.error('[runDailyRamp] INVARIANT BROKEN — dump waveRuns and report a bug');\n  }\n  throw e;\n}","preventionTips":["Treat any occurrence as a code regression — review the inFlight length check and index logic.","Keep the length-check and index-read adjacent so a refactor cannot separate them.","Add a unit test asserting inFlight[0] is defined whenever inFlight.length > 0 on the query result."],"tags":["convex","broadcast-ramp","invariant","unreachable","defensive","code-defect"],"backgroundTag":null,"analyzedSha":"ffec79ac339946fd2d24e85845da5755dcaa534b","analyzedAt":"2026-08-12T11:24:56.012Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}