Skip to content

fix(benchmarks): stop leaking 'latest' placeholder to postgres date cast - #6

Merged
aistackdev merged 1 commit into
masterfrom
fix/dont-leak-latest-placeholder
May 26, 2026
Merged

fix(benchmarks): stop leaking 'latest' placeholder to postgres date cast#6
aistackdev merged 1 commit into
masterfrom
fix/dont-leak-latest-placeholder

Conversation

@aistackdev

Copy link
Copy Markdown

Summary

/api/v1/benchmarks was throwing HTTP 500 with Invalid time value on initial page load. From the container logs the user shared:

```
Error fetching benchmarks: RangeError: Invalid time value
at Date1.toISOString ()
at Object.serialize (.next/server/chunks/packages_db_src_connection_ts...)
at Object.ea [as execute] (...)
```

Root cause chain:

```
useBenchmarks(model, undefined)
→ benchmarkQueryOptions(model, date ?? 'latest') // 'latest' fallback
→ fetchBenchmarks(model, 'latest', …) // leaks through!
→ GET /api/v1/benchmarks?date=latest
→ SQL: br.date <= 'latest'::date
→ postgres-js: new Date('latest') = Invalid Date
→ toISOString() → RangeError → 500
```

'latest' was meant as a queryKey-cache marker but leaked into the fetch URL. Upstream doesn't see it because their defaults race-load fast; our small DB exposes the window.

Fix

  • benchmarkQueryOptions — keep 'latest' in the queryKey (cache discrimination), but pass undefined to fetchBenchmarks when the marker is detected. URL omits the date param → API uses the no-date latest_benchmarks MV path.
  • /api/v1/benchmarks GET — defensive ISO-date regex (YYYY-MM-DD). Anything else is treated as no date. Prevents future garbage from hitting the same 500.

Test plan

  • pnpm typecheck clean
  • Post-deploy: hard-refresh /evaluation — the 500 in container logs should stop. Page should render the 12-row recipe table immediately.

🤖 Generated with Claude Code

The /api/v1/benchmarks GET was throwing a 500 with 'Invalid time value'
on initial page load. Root cause:

  useBenchmarks(model, undefined)
    → benchmarkQueryOptions(model, date ?? 'latest')   // fallback marker
    → fetchBenchmarks(model, 'latest', …)              // passes 'latest'!
    → GET /api/v1/benchmarks?date=latest
    → getLatestBenchmarks(sql, dbModelKeys, 'latest')
    → SQL: AND br.date <= ${'latest'}::date
    → postgres-js: new Date('latest') → NaN
    → Date.prototype.toISOString → RangeError('Invalid time value')
    → 500

The 'latest' string was only intended as a queryKey marker for "no
specific date", but it leaked through to the fetch URL. Fix:

  - benchmarkQueryOptions: skip the date when it's the 'latest' sentinel,
    keep it in the queryKey for cache discrimination. dateForFetch is
    undefined → no `date` param in the URL → API uses the no-date
    latest_benchmarks MV path.

  - /api/v1/benchmarks GET: defensive ISO-date regex validation. Anything
    that isn't `YYYY-MM-DD` is treated as no date. Prevents future leaks
    from hitting the same 500.

Worked in upstream's data shape because the upstream defaults race-loaded
quickly, but on our small DB the empty-date initial render persisted
long enough to show the 500 to users.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@aistackdev
aistackdev merged commit be0a170 into master May 26, 2026
3 of 9 checks passed
@aistackdev
aistackdev deleted the fix/dont-leak-latest-placeholder branch May 26, 2026 07:58
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