{"version":3,"file":"stats.d.ts","sourceRoot":"","sources":["../../../../src/core/long-horizon/adaptive/stats.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,KAAK,YAAY,EAAkC,MAAM,oBAAoB,CAAC;AACvF,OAAO,EAAE,KAAK,aAAa,EAAoB,MAAM,eAAe,CAAC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,MAAM,WAAW,UAAU;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,YAAY,CAAC;IACrB,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uBAAuB,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,CAgDpE;AAED,wBAAgB,cAAc,CAC7B,MAAM,EAAE,YAAY,GAClB,MAAM,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAetE","sourcesContent":["/**\n * Session and run statistics.\n *\n * Deterministic aggregation of durable telemetry: cost/tokens by\n * role/provider/model, tool calls by tool, mutating calls, web/LSP/job usage,\n * subagent usage, retries, pivots, escalations, stall periods, criteria\n * completion, and finalization-reserve usage. Exposes machine-readable output;\n * never hides reasoning, secrets, or credentials.\n */\n\nimport { type BudgetLedger, resourcesOfBudget, sumResource } from \"./budget-ledger.js\";\nimport { type CriteriaState, criterionSummary } from \"./criteria.js\";\nimport type { RunStatistics } from \"./types.js\";\n\nexport interface StatsInput {\n\trunId: string;\n\tledger: BudgetLedger;\n\tcriteria?: CriteriaState;\n\twebSearches?: number;\n\twebFetches?: number;\n\tlspRequests?: number;\n\tjobStarts?: number;\n\tsubagentRuns?: number;\n\tretryCount?: number;\n\tpivotCount?: number;\n\tescalationCount?: number;\n\tstallPeriods?: number;\n\tfinalizationReserveUsed?: number;\n}\n\nexport function deriveRunStatistics(input: StatsInput): RunStatistics {\n\tconst ledger = input.ledger;\n\n\t// Tool calls by tool from ledger resource maxToolCalls is not per-tool, so we\n\t// derive by summing the tool-call resource.\n\tconst toolCalls = sumResource(ledger, \"maxToolCalls\");\n\tconst mutating = sumResource(ledger, \"maxMutatingToolCalls\");\n\n\t// Cost by role: aggregate maxCostUsd entries per role.\n\tconst costByRole: Record<string, number> = {};\n\tfor (const e of ledger.entries) {\n\t\tif (e.resource !== \"maxCostUsd\") continue;\n\t\tconst key = e.role ?? \"unattributed\";\n\t\tcostByRole[key] = (costByRole[key] ?? 0) + e.amount;\n\t}\n\n\tconst tokensByRole: Record<string, { input: number; output: number; cachedInput: number }> = {};\n\tfor (const e of ledger.entries) {\n\t\tif (e.resource !== \"maxInputTokens\" && e.resource !== \"maxOutputTokens\" && e.resource !== \"maxCachedInputTokens\")\n\t\t\tcontinue;\n\t\tconst key = e.role ?? \"unattributed\";\n\t\tconst entry = tokensByRole[key] ?? { input: 0, output: 0, cachedInput: 0 };\n\t\tif (e.resource === \"maxInputTokens\") entry.input += e.amount;\n\t\telse if (e.resource === \"maxOutputTokens\") entry.output += e.amount;\n\t\telse if (e.resource === \"maxCachedInputTokens\") entry.cachedInput += e.amount;\n\t\ttokensByRole[key] = entry;\n\t}\n\n\tconst criteriaComplete = input.criteria ? criterionSummary(input.criteria).satisfied : 0;\n\n\treturn {\n\t\trunId: input.runId,\n\t\tcostByRole,\n\t\ttokensByRole,\n\t\ttoolCallsByTool: { total: toolCalls },\n\t\tmutatingToolCalls: mutating,\n\t\twebSearches: input.webSearches ?? 0,\n\t\twebFetches: input.webFetches ?? 0,\n\t\tlspRequests: input.lspRequests ?? 0,\n\t\tjobStarts: input.jobStarts ?? 0,\n\t\tsubagentRuns: input.subagentRuns ?? 0,\n\t\tretryCount: input.retryCount ?? 0,\n\t\tpivotCount: input.pivotCount ?? 0,\n\t\tescalationCount: input.escalationCount ?? 0,\n\t\tstallPeriods: input.stallPeriods ?? 0,\n\t\tcriteriaComplete,\n\t\tfinalizationReserveUsed: input.finalizationReserveUsed ?? 0,\n\t};\n}\n\nexport function resourcesUsage(\n\tledger: BudgetLedger,\n): Record<string, { total: number; estimated: number; actual: number }> {\n\tconst out: Record<string, { total: number; estimated: number; actual: number }> = {};\n\tfor (const resource of resourcesOfBudget()) {\n\t\tout[resource] = {\n\t\t\ttotal: sumResource(ledger, resource),\n\t\t\testimated: 0,\n\t\t\tactual: 0,\n\t\t};\n\t\tfor (const e of ledger.entries) {\n\t\t\tif (e.resource !== resource) continue;\n\t\t\tif (e.estimatedOrActual === \"actual\") out[resource].actual += e.amount;\n\t\t\telse out[resource].estimated += e.amount;\n\t\t}\n\t}\n\treturn out;\n}\n"]}