Skip to content

fix(test): Codex usage-limit tests read the real clock — main goes red 2026-10-09 → 10-16 when the fixture's reset enters the 7-day horizon #1248

Closed

Description

@admarble

Tier: mechanical Urgency: main's test job goes red on 2026-10-09 ~01:04 local and stays red until 2026-10-16 ~01:04. Land this before then.

Problem. classifyCodexTurnFailure (src/lib/workflow/drivers/codex.ts:350) takes now: number = Date.now(). It classifies a usage-limit message as a RateLimitError when its reset time falls within CODEX_WAIT_HORIZON_MS (7 days), and as a BillingError otherwise. The fixture src/lib/workflow/drivers/__fixtures__/codex-turn-failed-usage-limit.jsonl carries the verbatim #1087 message "…try again at Oct 16th, 2026 1:04 AM." Two tests expect BillingError for it and read the real clock through evaluateCodexRun:

Evidence. classifyCodexTurnFailure(msg, Date.parse(d)) with the fixture message:

now result
2026-09-30T20:00 BillingError
2026-10-08T23:00 BillingError
2026-10-09T02:00 RateLimitError
2026-10-15T12:00 RateLimitError
2026-10-16T02:00 BillingError

The #1087 PR's QA flagged this: "will fail for a seven-day window in October". It was never filed; it surfaced again in a decision-trail audit on 2026-09-30 (tracker #1236).

AC

  • AC-1: Both tests are independent of the wall clock. Either they pin time (for example vi.useFakeTimers() + vi.setSystemTime to a date well before the fixture's reset), or evaluateCodexRun/codexOutcome accepts an injected now that the tests set. Evidence: npx vitest run src/lib/workflow/drivers/__tests__/driver-conformance.test.ts src/lib/workflow/batch-executor.test.ts
  • AC-2: A test runs the fixture with now set inside the 7-day window and asserts RateLimitError, so the horizon branch is covered on purpose rather than by the calendar. Evidence: npx vitest run src/lib/workflow/drivers/codex.test.ts
  • AC-3: Mutation check: drop the time pin from AC-1's tests and set the system time to 2026-10-10. The BillingError cases then fail, which proves the pin is what keeps them stable. Record it with a Mutation-verified: marker. Evidence: npx vitest run src/lib/workflow/drivers/__tests__/driver-conformance.test.ts

Graph metadata

  • No dependency edges.

May touch

  • src/lib/workflow/drivers/__tests__/driver-conformance.test.ts
  • src/lib/workflow/batch-executor.test.ts
  • src/lib/workflow/drivers/codex.test.ts
  • src/lib/workflow/drivers/codex.ts (only to thread an optional now through evaluateCodexRun)

Activity

  1. added
    bug
    on Sep 30, 2026
  2. admarble commented on Oct 1, 2026

    @admarble
    ContributorAuthor

    Runner context (re-checked against main @ 5e8f0aa)

    Line refs (current):

    • src/lib/workflow/drivers/codex.ts:350-353: classifyCodexTurnFailure(message, now = Date.now()). Horizon check at :365-366.
    • src/lib/workflow/drivers/codex.ts:388: evaluateCodexRun(parsed, outcome). At :419 it calls classifyCodexTurnFailure(parsed.turnFailure) with no now, so the real clock leaks in here. Production caller: :716.
    • src/lib/workflow/drivers/__tests__/driver-conformance.test.ts:210-219 (codexOutcome) and the case at :620-628.
    • src/lib/workflow/batch-executor.test.ts:3036-3056 (#1087 Codex usage-limit turn failure under -Q).
    • src/lib/workflow/drivers/codex.test.ts:665+. This file already pins NOW = 2026-10-01 for the direct classifyCodexTurnFailure tests, so AC-2's new case goes there.

    Prior fixes / producers (checked with commands):

    Standard block:

    • No full npm test in exec or in QA. This applies to the qa phase too. Run only the three test files named in the ACs, plus grep -rlE 'evaluateCodexRun|classifyCodexTurnFailure|codex-turn-failed-usage-limit' --include='*.test.ts' src __tests__ scripts | xargs npx vitest run.
    • Commit WIP early, and always before a mutation (AC-3). Use a type-preserving mutation, since the global setup builds.
    • The mutation marker's failedTest must start with the test file path: src/lib/workflow/drivers/__tests__/driver-conformance.test.ts > … > ….
    • PR body: Closes #1248. Use only Refs for any tracker.
    • Any settings read must be JSONC-safe (not expected here).
  3. admarble commented on Oct 1, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 2 directories)

    AC Quality Check

    ⚠️ AC-1: "for example" phrasing reads as open-ended, but the AC itself is an exhaustive either/or (pin the clock vs. inject now) — not flagged further; the wording is fine as written.
    ✅ AC-2, AC-3: Clear and testable.


    Scope Assessment

    Non-Goals: ⚠️ Non-Goals section not found in the issue body.

    Metric Value Status
    Feature count 1 ✅
    AC items 3 ✅
    Directory spread 2 ✅

    Verdict: ⚠️ SCOPE_WARNING — driven solely by the missing Non-Goals heading; every quantitative metric is green. Given the main-goes-red deadline (2026-10-09), not worth blocking on; flagged for completeness only.


    Acceptance Criteria

    AC-1: Both tests are independent of the wall clock

    Verification: Unit Test
    Scenario: Given driver-conformance.test.ts's codexOutcome() helper and batch-executor.test.ts's inline evaluateCodexRun() call both currently omit now → When the real clock crosses 2026-10-09 ~01:04 → Then both suites currently flip from BillingError to RateLimitError and fail; after the fix, both pin an explicit now so the result is clock-independent.
    Assumptions: The owner's runner-context comment pre-approved threading an optional now through CodexRunOutcome rather than vi.useFakeTimers() — see Design Review Q2.

    AC-2: Horizon branch covered on purpose, not by the calendar

    Verification: Unit Test
    Scenario: Given the real codex-turn-failed-usage-limit.jsonl fixture parsed through CodexStreamParser → When evaluateCodexRun is called with now set inside the 7-day window (e.g. 2026-10-10T00:00, since the fixture's reset is 2026-10-16T01:04) → Then structuredError is a RateLimitError with metadata.resetsAt === Date(2026,9,16,1,4).getTime().
    Assumptions: None — this exercises the real parse path, not just classifyCodexTurnFailure in isolation (codex.test.ts already has synthetic-message horizon tests via its at() helper; AC-2 asks specifically for "the fixture").

    AC-3: Mutation check proves the pin is load-bearing

    Verification: Unit Test
    Scenario: Given AC-1's pinned now values → When the pin is removed/overridden and the system clock is set to 2026-10-10 → Then the BillingError assertions in driver-conformance.test.ts:620 (and the batch-executor.test.ts case) fail → Restore the pin, record Mutation-verified: per the constitution's §6i marker format.
    Assumptions: failedTest in the marker must start with the test file path (src/lib/workflow/drivers/__tests__/driver-conformance.test.ts > ...), per the owner's comment and project_mutation_marker_failedtest_file_prefix.


    Implementation Plan

    Phase 1: Thread an optional now through the production path

    1. Add now?: number to the CodexRunOutcome interface (src/lib/workflow/drivers/codex.ts:281-288). Confirmed additive/safe — grepped all 8 call sites across codex.ts, codex.test.ts, driver-conformance.test.ts, and batch-executor.test.ts; none break from an optional field.
    2. In evaluateCodexRun's turnFailure branch (:416-420), change classifyCodexTurnFailure(parsed.turnFailure) → classifyCodexTurnFailure(parsed.turnFailure, outcome.now). The production caller (:716) never sets now, so outcome.now is undefined there and classifyCodexTurnFailure's own default parameter (now: number = Date.now()) keeps production behavior unchanged.

    Phase 2: Pin the two wall-clock-dependent tests (AC-1)

    1. driver-conformance.test.ts: give the codexOutcome() helper (:209-219) a pinned default, e.g. const PINNED_NOW = new Date(2026, 8, 1).getTime(); passed as now in the returned evaluateCodexRun(..., { ..., now: PINNED_NOW }). The existing call codexOutcome("codex-turn-failed-usage-limit.jsonl") at :623 needs no edit — it inherits the pin.
    2. batch-executor.test.ts: add the same pinned now to the inline evaluateCodexRun(parser.end(), {...}) call (~:3049).

    Phase 3: Cover the horizon branch on purpose (AC-2)

    1. codex.test.ts: add a test — in or alongside the existing "1087: classifyCodexTurnFailure" describe block — that loads the real fixture (readFileSync + CodexStreamParser, same pattern already used in the other two files) and calls evaluateCodexRun with now inside the window (2026-10-10). Assert RateLimitError with the exact resetsAt epoch.

    Phase 4: Mutation verification (AC-3)

    1. Temporarily neutralize the Phase 2 pin (e.g. override PINNED_NOW to 2026-10-10), run npx vitest run src/lib/workflow/drivers/__tests__/driver-conformance.test.ts, confirm the BillingError case at :620 fails, restore the pin, and record the result in the PR body with the Mutation-verified: / SEQUANT_MUTATION marker.

    Commands to run (per the owner's comment — no full npm test, in exec or qa):

    grep -rlE 'evaluateCodexRun|classifyCodexTurnFailure|codex-turn-failed-usage-limit' --include='*.test.ts' src __tests__ scripts | xargs npx vitest run
    

    Design Review

    1. Where does this logic belong? CodexRunOutcome/evaluateCodexRun in src/lib/workflow/drivers/codex.ts — the same module that already owns classifyCodexTurnFailure's explicit now parameter. Test-side pins live in each test file's own helper/call site.
    2. What's the simplest correct approach? Thread an optional now through CodexRunOutcome (owner-blessed). Rejected: vi.useFakeTimers() + vi.setSystemTime() — both driver-conformance.test.ts and batch-executor.test.ts are large, multi-suite files; a global fake-timer scope risks leaking into unrelated tests in the same file and needs careful afterEach restore. An explicit, optional constructor field is localized, auditable in a diff, and mirrors a pattern the codebase already uses one layer down.
    3. What existing pattern does this follow? codex.test.ts's own NOW constant + classifyCodexTurnFailure(msg, NOW) calls — this just extends the same explicit-clock-injection pattern one layer up through evaluateCodexRun.
    4. What would a senior reviewer challenge? "Why grow the production-facing CodexRunOutcome interface for a test concern?" — because both the real driver (:716) and tests already construct this object inline by hand; one optional field keeps a single code path instead of a parallel test-only overload, and defaults to real-clock behavior everywhere it isn't set.

    No ADR required — this is a localized test-determinism fix (mechanical tier per the issue itself), not a cross-cutting architectural decision.


    Feature Quality Planning

    All standard checks pass. Notable items:

    • Pre-existing worktree feature/1248-fix-test-codex-usage-limit-tests-read-the-real-clo exists but is clean (identical to origin/main, no commits, no uncommitted changes) — no conflict, safe to reuse.
    • CodexRunOutcome field addition verified additive via whole-repo grep (8 call sites, all optional-field-safe).

    Open Questions

    1. Should the pinned now constant be shared across the three touched test files (a test-utils export) instead of one literal per file? — Recommendation: keep them independent per-file literals; CODEX_WAIT_HORIZON_MS has exactly one prior commit in its history (git log -S), so cross-file coupling isn't worth it for a one-off determinism fix. Impact if wrong: a future horizon-constant change needs 2 edits instead of 1 — low cost.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: enabled
    Reasoning: testgen is mechanically indicated (all 3 ACs carry declared Unit Test evidence, which outranks the bug-label short-circuit per #938) but is overridden here: the AC evidence is editing the existing test files directly (pinning a clock value, adding one assertion) — there's no separate production code path to stub ahead of the tests, so a generated stub would just collide with the hand-written edits above. Quality loop stays enabled per the mechanical SCOPE_WARNING verdict, even though the only driver is the missing Non-Goals heading — cheap safety net given the fixed landing deadline.


    Label Review

    Current: bug
    Recommended: bug, planned
    Reason: Plan reviewed and accepted.
    Quality Loop: Will auto-enable (SCOPE_WARNING, missing Non-Goals heading only).

    → gh issue edit 1248 --add-label planned


    --- DRAFT GITHUB ISSUE COMMENT (PLAN) ---

    Spec Plan — #1248

    AC Checklist

    • AC-1 (Unit Test): Pin now through CodexRunOutcome in both driver-conformance.test.ts's codexOutcome() helper and batch-executor.test.ts's inline evaluateCodexRun() call.
    • AC-2 (Unit Test): New codex.test.ts case — real fixture parsed, now inside the 7-day window, asserts RateLimitError.
    • AC-3 (Unit Test): Mutation check — pull the pin, set clock to 2026-10-10, confirm BillingError cases fail, restore, record Mutation-verified: marker.

    Plan

    1. Add optional now?: number to CodexRunOutcome; thread it into classifyCodexTurnFailure at codex.ts:419 (production caller at :716 unaffected — stays on Date.now()).
    2. Pin now in both AC-1 test helpers to a fixed pre-horizon date (e.g. 2026-09-01).
    3. Add AC-2's horizon-branch test in codex.test.ts using the real .jsonl fixture, now = 2026-10-10.
    4. Run the mutation check from AC-3, record the marker.

    Key decision: Explicit now-threading over vi.useFakeTimers() — matches the owner's comment, avoids fake-timer leakage risk in two large shared test files.

    Open question: Keep the pinned-clock constants per-file rather than centralizing — fine given CODEX_WAIT_HORIZON_MS barely changes.

    Workflow: exec → qa (quality loop enabled; testgen mechanically indicated but overridden — the AC evidence is the test edits, nothing else to stub).

  4. added
    planned
    on Oct 1, 2026
  5. admarble commented on Oct 1, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 3/3 met

    Gaps:

    • CHANGELOG entry missing for a fix-type commit (test-only in effect, but the §10a gate counts it)
  6. added a commit that references this issue on Oct 1, 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

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions