Skip to content

fix(submit): aggregate device time totals - #627

Merged
junhoyeo merged 1 commit into
junhoyeo:mainfrom
IvGolovach:codex/submit-device-time-metrics-20260528
May 30, 2026
Merged

fix(submit): aggregate device time totals#627
junhoyeo merged 1 commit into
junhoyeo:mainfrom
IvGolovach:codex/submit-device-time-metrics-20260528

Conversation

@IvGolovach

@IvGolovach IvGolovach commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Derive submissions.totalActiveTimeMs from persisted daily_breakdown.active_time_ms rows across all submitted devices.
  • Preserve per-device replace semantics so resubmitting one device updates that device's row without double-counting prior active time.
  • Keep session-shape metrics (longestContinuousMs, maxConcurrentSessions, sessionCount) tied to incoming timeMetrics because they cannot be safely recomputed from daily active-time buckets.
  • Add focused submit route regressions for multi-device active-time sums, same-device replacement, and submissions without incoming timeMetrics.

Why

The submit path already recalculates token and cost totals from all persisted daily rows, but totalActiveTimeMs was still copied from the latest incoming payload. With multiple devices, all-time profile and leaderboard time totals could represent only the latest submitting device instead of the user's aggregate submitted history. This PR makes active-time totals follow the same persisted-row aggregate model as token totals.

Diff scope

  • packages/frontend/src/app/api/submit/route.ts
  • packages/frontend/__tests__/api/submitAuth.test.ts

Branch integrity

  • Base branch: main
  • Validated base SHA: c21f0f533205ad63e9154666c294bb789d2415c9
  • Head branch: IvGolovach:codex/submit-device-time-metrics-20260528
  • Head SHA: fdd4e0f191b434e47601e089d8d59d3d2fa00b70
  • Ahead/behind vs fetched origin/main: 1 ahead / 0 behind
  • Commit: fdd4e0f191b434e47601e089d8d59d3d2fa00b70 fix(submit): aggregate device time totals

Validation

  • bun run --cwd packages/frontend test __tests__/api/submitAuth.test.ts __tests__/api/submit.test.ts: PASS, 60 tests passed.
  • bun run --cwd packages/frontend lint -- src/app/api/submit/route.ts __tests__/api/submitAuth.test.ts: PASS, no output.
  • git diff --check origin/main...HEAD: PASS, no output.
  • git status --short --untracked-files=all: PASS, clean worktree.

Runtime safety

  • No new external services, queues, timers, or background jobs.
  • The submit transaction already recalculates persisted totals after daily rows are inserted or updated; this PR extends that aggregate query to active_time_ms.
  • Same-device replacement still updates the existing daily row before totals are recomputed.
  • No invariant regression introduced.

Migration notes

Not applicable - no database migration or schema change.

Rollback plan

Rollback: revert this PR. DB downgrade: not applicable. Data repair: not applicable. Operational caveats: reverting restores latest-payload totalActiveTimeMs behavior.

Known residual risks

  • longestContinuousMs, maxConcurrentSessions, and sessionCount remain sourced from incoming timeMetrics when present because daily active-time buckets do not contain enough information to recompute those session-shape metrics correctly.
  • Remote CI should provide the final full-suite proof for the PR SHA.

Summary by cubic

Aggregates all-time active time from persisted daily device rows in the api/submit route so multi-device totals are correct. Preserves same-device replacement and keeps session metrics from incoming timeMetrics.

  • Bug Fixes
    • Compute submissions.totalActiveTimeMs from SUM of daily_breakdown.active_time_ms across all submitted devices.
    • Recompute totals after same-device row replacement to avoid double-counting; session-shape metrics (longestContinuousMs, maxConcurrentSessions, sessionCount) use incoming timeMetrics when provided.

Written for commit 7d06744. Summary will update on new commits.

Review in cubic

@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tokscale Ready Ready Preview, Comment May 30, 2026 1:31pm

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Validation
* Validation tier: Tier 2 - Narrow Runtime Change, localized /api/submit aggregation behavior.
* TDD red: bun run --cwd packages/frontend test -- __tests__/api/submitAuth.test.ts -t "active time": FAIL before fix, 3 failed assertions showed latest-payload active time or missing totalActiveTimeMs instead of persisted daily-row aggregate.
* bun install: PASS, installed workspace dependencies after the first test run could not resolve frontend packages.
* bun run --cwd packages/frontend test -- __tests__/api/submitAuth.test.ts -t "active time": PASS, 3 passed / 8 skipped.
* bun run --cwd packages/frontend test -- __tests__/api/submitAuth.test.ts __tests__/api/submit.test.ts: PASS, 60 passed.
* bun run --cwd packages/frontend lint -- src/app/api/submit/route.ts __tests__/api/submitAuth.test.ts: PASS.
* git diff --check: PASS.
* git diff --cached --check: PASS.
* Ledger: not applicable - not required for selected validation tier/change family.
* Version: not applicable - no package or release manifest version sync is required for this localized frontend route fix.
* Not run: full frontend build/full test suite - not required for selected Tier 2 validation; targeted submit route tests and lint cover the changed behavior.

Rollback
* git revert HEAD
@junhoyeo
junhoyeo force-pushed the codex/submit-device-time-metrics-20260528 branch from fdd4e0f to 7d06744 Compare May 30, 2026 13:30
@junhoyeo
junhoyeo merged commit 734f685 into junhoyeo:main May 30, 2026
6 checks passed
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.

2 participants