-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(stats): dedup usage records by sessionId and skip in-progress writes #4995
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,7 +173,9 @@ export function metricsToUsageRecord( | |
| }; | ||
| } | ||
|
|
||
| async function rebuildFromSessionJsonl(): Promise<UsageSummaryRecord[]> { | ||
| async function rebuildFromSessionJsonl( | ||
| skipSessionInRebuild?: string, | ||
| ): Promise<UsageSummaryRecord[]> { | ||
| const projectsDir = path.join(Storage.getGlobalQwenDir(), 'projects'); | ||
| try { | ||
| if (!fs.existsSync(projectsDir)) return []; | ||
|
|
@@ -261,23 +263,44 @@ async function rebuildFromSessionJsonl(): Promise<UsageSummaryRecord[]> { | |
| if (results.length > 0) { | ||
| const usagePath = getUsageHistoryPath(); | ||
| for (const record of results) { | ||
| // Skip the in-progress current session: persistSessionUsage() will write | ||
| // its authoritative record on /clear or exit. Writing here would create | ||
| // a permanent duplicate in usage_record.jsonl (issue #4994). | ||
| if (skipSessionInRebuild && record.sessionId === skipSessionInRebuild) | ||
| continue; | ||
| jsonl.writeLineSync(usagePath, record); | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| export async function loadUsageHistory(): Promise<UsageSummaryRecord[]> { | ||
| function dedupBySessionId(records: UsageSummaryRecord[]): UsageSummaryRecord[] { | ||
| // Last-wins by sessionId. Protects existing users whose usage_record.jsonl | ||
| // already contains duplicates produced by the bug fixed in this change | ||
| // (issue #4994) — without this, every aggregate stays inflated forever. | ||
| const map = new Map<string, UsageSummaryRecord>(); | ||
| for (const r of records) map.set(r.sessionId, r); | ||
| if (map.size < records.length) { | ||
| debugLogger.debug( | ||
| `dedupBySessionId: removed ${records.length - map.size} duplicate record(s)`, | ||
| ); | ||
| } | ||
| return [...map.values()]; | ||
| } | ||
|
|
||
| export async function loadUsageHistory( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The new Consider either: (a) applying the skip consistently on both paths inside — DeepSeek/deepseek-v4-pro via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point — the parameter was misleading. Went with renaming over consolidating, in commit a7c7206:
I considered option (a) — apply the skip on both paths and drop the filter in
Let me know if you'd still prefer (a) — happy to follow up. |
||
| skipSessionInRebuild?: string, | ||
| ): Promise<UsageSummaryRecord[]> { | ||
| try { | ||
| const records = await jsonl.read<UsageSummaryRecord>(getUsageHistoryPath()); | ||
| const filtered = records.filter((r) => r.version === 1); | ||
| if (filtered.length > 0) return filtered; | ||
| if (filtered.length > 0) return dedupBySessionId(filtered); | ||
| } catch (e) { | ||
| debugLogger.debug(`loadUsageHistory: failed to read usage file: ${e}`); | ||
| } | ||
|
|
||
| return rebuildFromSessionJsonl(); | ||
| return dedupBySessionId(await rebuildFromSessionJsonl(skipSessionInRebuild)); | ||
| } | ||
|
|
||
| export function getTimeRangeBounds(range: TimeRange): { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion]
dedupBySessionIdsilently discards duplicatesessionIdrecords with no diagnostic trace. When duplicates are present (evidence of the #4994 bug having affected a user's data), there is no way to measure how many users were affected or detect recurrence.— DeepSeek/deepseek-v4-pro via Qwen Code /review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adopted in a7c7206 — added a
debugLogger.debugline that fires only when duplicates were actually removed:This gives us a way to measure #4994's blast radius on real user data without changing aggregate behavior. Thanks for the catch.