Skip to content

Commit b741286

Browse files
AndyS77zai-glm-52
andcommitted
fix(stats): treat orphaned sessions as top-level for cost aggregation
When a parent session is deleted (e.g. via direct DB deletion), its children become orphans with parentID still set. The aggregateSessionStats function only used totalCost for top-level sessions (no parentID). Orphaned sessions were treated as non-top-level, so their descendants' costs were lost. Now checks if the parentID actually exists in the session set. If not, the session is treated as top-level and totalCost (BFS) is used. Addresses janorga's comment about deleted-parent case (anomalyco#48306). Co-Authored-By: zai-glm-52 <[email protected]> Agent: @Feature-dev Scope: anomalyco#43645
1 parent d230002 commit b741286

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎packages/opencode/src/cli/cmd/stats.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const aggregateSessionStats = Effect.fn("Cli.stats.aggregate")(function* (
9292
) {
9393
const svc = yield* Session.Service
9494
const sessions = yield* getAllSessions()
95+
const sessionIDs = new Set(sessions.map((s) => s.id))
9596
const MS_IN_DAY = 24 * 60 * 60 * 1000
9697

9798
const cutoffTime = (() => {
@@ -168,7 +169,7 @@ const aggregateSessionStats = Effect.fn("Cli.stats.aggregate")(function* (
168169
.messages({ sessionID: session.id })
169170
.pipe(Effect.catchIf(NotFoundError.isInstance, () => Effect.succeed([])))
170171

171-
const isTopLevel = !session.parentID
172+
const isTopLevel = !session.parentID || !sessionIDs.has(session.parentID)
172173
const totalCost = isTopLevel
173174
? (yield* svc.totalCost(session.id).pipe(
174175
Effect.catchIf(NotFoundError.isInstance, () => Effect.succeed({ cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } } })),

0 commit comments

Comments
 (0)