-
Notifications
You must be signed in to change notification settings - Fork 2
fix(sidepanel): fix TODAY rollup lag — daily summary now updates on every turn #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DevanshuNEU
merged 4 commits into
OpenCodeIntel:main
from
DevanshuNEU:feat/lco-18-today-rollup-lag
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e6b184
fix(sidepanel): recompute daily summary on conv writes [GET-18]
DevanshuNEU a3cb81d
test(today-rollup): GET-18 invariant tests
DevanshuNEU 11c2f9e
refactor(get-18): sharpen comments, fix all pipeline issues
DevanshuNEU a9a55bf
fix(sidepanel): log computeDailySummary errors instead of swallowing …
DevanshuNEU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // tests/unit/today-rollup.test.ts | ||
| // | ||
| // GET-18 invariant: the TODAY daily summary must always be >= the active | ||
| // conversation's numbers. computeDailySummary is the function that produces | ||
| // this value; these tests prove its correctness properties directly. | ||
| // | ||
| // Hook integration gap: the useDashboardData.ts change that calls | ||
| // computeDailySummary on conv: storage events cannot be unit-tested here | ||
| // without renderHook + full Chrome API mocks (not present in this test setup). | ||
| // Manual verification covers it: open the side panel mid-conversation and | ||
| // confirm TODAY matches ACTIVE CONVERSATION to the exact turn and token count | ||
| // within one turn cycle, without waiting for the 30-min background alarm. | ||
|
|
||
| import { describe, it, expect, beforeEach } from 'vitest'; | ||
| import { | ||
| setStorage, | ||
| recordTurn, | ||
| computeDailySummary, | ||
| todayDateString, | ||
| type StorageArea, | ||
| } from '../../lib/conversation-store'; | ||
|
|
||
| const ORG = 'org-get18'; | ||
| const CONV_A = 'conv-get18-a'; | ||
| const CONV_B = 'conv-get18-b'; | ||
|
|
||
| // Inline mock matching the StorageArea interface. Plain async functions are | ||
| // sufficient here because these tests assert data values, not call counts. | ||
| function makeStoreMock(): StorageArea { | ||
| const data: Record<string, unknown> = {}; | ||
| return { | ||
| get: async (keys: string | string[] | null) => { | ||
| if (keys === null) return { ...data }; | ||
| const ks = typeof keys === 'string' ? [keys] : keys; | ||
| const out: Record<string, unknown> = {}; | ||
| for (const k of ks) if (k in data) out[k] = data[k]; | ||
| return out; | ||
| }, | ||
| set: async (items: Record<string, unknown>) => { Object.assign(data, items); }, | ||
| remove: async (keys: string | string[]) => { | ||
| for (const k of typeof keys === 'string' ? [keys] : keys) delete data[k]; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| beforeEach(() => { setStorage(makeStoreMock()); }); | ||
|
|
||
| describe('GET-18: TODAY rollup invariants', () => { | ||
| it('sums all conversations, not just the active one', async () => { | ||
| // The naive Math.max(today.totalTurns, activeConv.turnCount) fix is wrong | ||
| // for multi-conversation days: if conv A has 5 turns and conv B (active) | ||
| // has 12, Math.max(17, 12) = 17 when it should be 5+12=17 -- correct by | ||
| // coincidence. But if conv B has fewer turns than the daily total, Math.max | ||
| // silently drops conv A's contribution. computeDailySummary must sum all | ||
| // conversations, so it is immune to that class of bug. | ||
| const now = Date.now(); | ||
|
|
||
| for (let i = 0; i < 5; i++) { | ||
| await recordTurn(ORG, CONV_A, { | ||
| inputTokens: 100, outputTokens: 50, model: 'claude-sonnet-4-6', | ||
| contextPct: 5, cost: 0.001, completedAt: now, | ||
| }); | ||
| } | ||
| for (let i = 0; i < 12; i++) { | ||
| await recordTurn(ORG, CONV_B, { | ||
| inputTokens: 200, outputTokens: 80, model: 'claude-sonnet-4-6', | ||
| contextPct: 10, cost: 0.002, completedAt: now, | ||
| }); | ||
| } | ||
|
|
||
| const summary = await computeDailySummary(ORG, todayDateString()); | ||
|
|
||
| expect(summary.totalTurns).toBe(17); // 5 + 12, exact | ||
| expect(summary.totalTurns).toBeGreaterThanOrEqual(12); // invariant: >= conv B | ||
| expect(summary.totalTurns).toBeGreaterThanOrEqual(5); // invariant: >= conv A | ||
| expect(summary.totalInputTokens).toBeGreaterThanOrEqual(12 * 200); | ||
| expect(summary.estimatedCost).not.toBeNull(); | ||
| expect(summary.estimatedCost!).toBeGreaterThanOrEqual(12 * 0.002); | ||
| expect(summary.conversationCount).toBe(2); | ||
| }); | ||
|
|
||
| it('reflects a new turn immediately without waiting for the alarm', async () => { | ||
| // Simulates the exact GET-18 scenario: daily summary was last written by | ||
| // the background alarm when the conversation had N turns. One more turn | ||
| // completes. The next computeDailySummary call must include that turn | ||
| // immediately, not on the next 30-min alarm cycle. | ||
| const now = Date.now(); | ||
| const base = { | ||
| inputTokens: 500, outputTokens: 100, model: 'claude-sonnet-4-6', | ||
| contextPct: 8, cost: 0.005, completedAt: now, | ||
| }; | ||
|
|
||
| for (let i = 0; i < 3; i++) await recordTurn(ORG, CONV_A, base); | ||
| const stale = await computeDailySummary(ORG, todayDateString()); | ||
|
|
||
| // One more turn lands (the one the alarm has not seen yet). | ||
| const extra = { ...base, inputTokens: 800, outputTokens: 150, cost: 0.009 }; | ||
| await recordTurn(ORG, CONV_A, extra); | ||
| const fresh = await computeDailySummary(ORG, todayDateString()); | ||
|
|
||
| expect(fresh.totalTurns).toBe(stale.totalTurns + 1); | ||
| expect(fresh.totalInputTokens).toBe(stale.totalInputTokens + extra.inputTokens); | ||
| expect(fresh.totalOutputTokens).toBe(stale.totalOutputTokens + extra.outputTokens); | ||
| expect(fresh.estimatedCost).not.toBeNull(); | ||
| expect(fresh.estimatedCost!).toBeCloseTo(stale.estimatedCost! + extra.cost, 6); | ||
| }); | ||
|
|
||
| it('no double-counting when two conversations share a day', async () => { | ||
| // computeDailySummary iterates the conv index and aggregates. A bug that | ||
| // counted the same conv twice would produce 2x totals here. Fixed turn | ||
| // counts make the exact expected values unambiguous. | ||
| const now = Date.now(); | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| await recordTurn(ORG, CONV_A, { | ||
| inputTokens: 100, outputTokens: 50, model: 'claude-haiku-4-5', | ||
| contextPct: 3, cost: 0.001, completedAt: now, | ||
| }); | ||
| } | ||
| for (let i = 0; i < 2; i++) { | ||
| await recordTurn(ORG, CONV_B, { | ||
| inputTokens: 200, outputTokens: 100, model: 'claude-haiku-4-5', | ||
| contextPct: 5, cost: 0.003, completedAt: now, | ||
| }); | ||
| } | ||
|
|
||
| const summary = await computeDailySummary(ORG, todayDateString()); | ||
|
|
||
| expect(summary.conversationCount).toBe(2); | ||
| expect(summary.totalTurns).toBe(5); // 3 + 2, not 6 | ||
| expect(summary.totalInputTokens).toBe(3 * 100 + 2 * 200); // 700 | ||
| expect(summary.totalOutputTokens).toBe(3 * 50 + 2 * 100); // 350 | ||
| expect(summary.estimatedCost).not.toBeNull(); | ||
| expect(summary.estimatedCost!).toBeCloseTo(3 * 0.001 + 2 * 0.003, 6); | ||
| }); | ||
|
|
||
| it('propagates null estimatedCost when no turn has a known cost', async () => { | ||
| // An unrecognized model returns null from the pricing agent. The daily | ||
| // summary must stay null rather than coercing to 0: null means "cost | ||
| // unknown" and 0 means "this session was free". Conflating the two would | ||
| // silently misreport usage to any downstream consumer (traction exports, | ||
| // BIP posts, YC metrics). | ||
| await recordTurn(ORG, CONV_A, { | ||
| inputTokens: 300, outputTokens: 100, model: 'claude-unknown-future-model', | ||
| contextPct: 5, cost: null, completedAt: Date.now(), | ||
| }); | ||
|
|
||
| const summary = await computeDailySummary(ORG, todayDateString()); | ||
|
|
||
| expect(summary.estimatedCost).toBeNull(); | ||
| expect(summary.totalTurns).toBe(1); | ||
| expect(summary.totalInputTokens).toBe(300); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.