agent-nuvira 3.5.5 → 3.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/.agents/skills/index.json +16 -0
  2. package/.agents/skills/motion/SKILL.md +27 -0
  3. package/README.md +122 -1
  4. package/dist/cli/chat.d.ts +76 -17
  5. package/dist/cli/chat.d.ts.map +1 -1
  6. package/dist/cli/chat.js +330 -137
  7. package/dist/cli/chat.js.map +1 -1
  8. package/dist/cli/config.d.ts.map +1 -1
  9. package/dist/cli/config.js +17 -3
  10. package/dist/cli/config.js.map +1 -1
  11. package/dist/cli/doctor.d.ts +31 -2
  12. package/dist/cli/doctor.d.ts.map +1 -1
  13. package/dist/cli/doctor.js +143 -7
  14. package/dist/cli/doctor.js.map +1 -1
  15. package/dist/cli/execute.d.ts.map +1 -1
  16. package/dist/cli/execute.js +63 -22
  17. package/dist/cli/execute.js.map +1 -1
  18. package/dist/cli/failover-runner.js +4 -4
  19. package/dist/cli/failover-runner.js.map +1 -1
  20. package/dist/cli/gateway.js +13 -13
  21. package/dist/cli/gateway.js.map +1 -1
  22. package/dist/cli/loop-executor.d.ts +22 -0
  23. package/dist/cli/loop-executor.d.ts.map +1 -1
  24. package/dist/cli/loop-executor.js +60 -13
  25. package/dist/cli/loop-executor.js.map +1 -1
  26. package/dist/cli/parity.d.ts +12 -0
  27. package/dist/cli/parity.d.ts.map +1 -1
  28. package/dist/cli/parity.js +46 -1
  29. package/dist/cli/parity.js.map +1 -1
  30. package/dist/cli/stats.d.ts +62 -4
  31. package/dist/cli/stats.d.ts.map +1 -1
  32. package/dist/cli/stats.js +282 -5
  33. package/dist/cli/stats.js.map +1 -1
  34. package/dist/cli/tool-install-prompt.d.ts +2 -7
  35. package/dist/cli/tool-install-prompt.d.ts.map +1 -1
  36. package/dist/cli/tool-install-prompt.js +11 -28
  37. package/dist/cli/tool-install-prompt.js.map +1 -1
  38. package/dist/cli/ui/index.d.ts +2 -2
  39. package/dist/cli/ui/index.d.ts.map +1 -1
  40. package/dist/cli/ui/index.js +1 -1
  41. package/dist/cli/ui/index.js.map +1 -1
  42. package/dist/cli/ui/presenter.d.ts +43 -0
  43. package/dist/cli/ui/presenter.d.ts.map +1 -1
  44. package/dist/cli/ui/presenter.js +88 -1
  45. package/dist/cli/ui/presenter.js.map +1 -1
  46. package/dist/config/manager.d.ts +29 -1
  47. package/dist/config/manager.d.ts.map +1 -1
  48. package/dist/config/manager.js +178 -143
  49. package/dist/config/manager.js.map +1 -1
  50. package/dist/config/paths.d.ts +48 -1
  51. package/dist/config/paths.d.ts.map +1 -1
  52. package/dist/config/paths.js +59 -9
  53. package/dist/config/paths.js.map +1 -1
  54. package/dist/config/scratch-workspace.d.ts +134 -0
  55. package/dist/config/scratch-workspace.d.ts.map +1 -0
  56. package/dist/config/scratch-workspace.js +213 -0
  57. package/dist/config/scratch-workspace.js.map +1 -0
  58. package/dist/config/service-catalog.js +1 -1
  59. package/dist/config/service-catalog.js.map +1 -1
  60. package/dist/config/service-probe.js +1 -1
  61. package/dist/config/service-probe.js.map +1 -1
  62. package/dist/config/types.d.ts +22 -0
  63. package/dist/config/types.d.ts.map +1 -1
  64. package/dist/gateway/adapters.d.ts +24 -0
  65. package/dist/gateway/adapters.d.ts.map +1 -1
  66. package/dist/gateway/adapters.js +20 -8
  67. package/dist/gateway/adapters.js.map +1 -1
  68. package/dist/gateway/oversized-reply.d.ts +39 -0
  69. package/dist/gateway/oversized-reply.d.ts.map +1 -0
  70. package/dist/gateway/oversized-reply.js +54 -0
  71. package/dist/gateway/oversized-reply.js.map +1 -0
  72. package/dist/gateway/registry.d.ts +0 -10
  73. package/dist/gateway/registry.d.ts.map +1 -1
  74. package/dist/gateway/registry.js +149 -5
  75. package/dist/gateway/registry.js.map +1 -1
  76. package/dist/learning/auto-router.d.ts +30 -4
  77. package/dist/learning/auto-router.d.ts.map +1 -1
  78. package/dist/learning/auto-router.js +54 -38
  79. package/dist/learning/auto-router.js.map +1 -1
  80. package/dist/learning/cost-classification.d.ts +188 -0
  81. package/dist/learning/cost-classification.d.ts.map +1 -0
  82. package/dist/learning/cost-classification.js +294 -0
  83. package/dist/learning/cost-classification.js.map +1 -0
  84. package/dist/learning/cost-ledger-maintenance.d.ts +122 -0
  85. package/dist/learning/cost-ledger-maintenance.d.ts.map +1 -0
  86. package/dist/learning/cost-ledger-maintenance.js +289 -0
  87. package/dist/learning/cost-ledger-maintenance.js.map +1 -0
  88. package/dist/learning/cost-reconcile.d.ts +160 -0
  89. package/dist/learning/cost-reconcile.d.ts.map +1 -0
  90. package/dist/learning/cost-reconcile.js +238 -0
  91. package/dist/learning/cost-reconcile.js.map +1 -0
  92. package/dist/learning/cost-tracker.d.ts +134 -13
  93. package/dist/learning/cost-tracker.d.ts.map +1 -1
  94. package/dist/learning/cost-tracker.js +190 -68
  95. package/dist/learning/cost-tracker.js.map +1 -1
  96. package/dist/learning/model-pricing.d.ts +240 -0
  97. package/dist/learning/model-pricing.d.ts.map +1 -0
  98. package/dist/learning/model-pricing.js +309 -0
  99. package/dist/learning/model-pricing.js.map +1 -0
  100. package/dist/learning/model-selection.d.ts +15 -0
  101. package/dist/learning/model-selection.d.ts.map +1 -1
  102. package/dist/learning/model-selection.js +22 -2
  103. package/dist/learning/model-selection.js.map +1 -1
  104. package/dist/learning/model-warmup.d.ts.map +1 -1
  105. package/dist/learning/model-warmup.js +4 -1
  106. package/dist/learning/model-warmup.js.map +1 -1
  107. package/dist/learning/quota-ledger.d.ts +24 -0
  108. package/dist/learning/quota-ledger.d.ts.map +1 -1
  109. package/dist/learning/quota-ledger.js +40 -0
  110. package/dist/learning/quota-ledger.js.map +1 -1
  111. package/dist/learning/runtime-dependencies.d.ts +183 -0
  112. package/dist/learning/runtime-dependencies.d.ts.map +1 -0
  113. package/dist/learning/runtime-dependencies.js +349 -0
  114. package/dist/learning/runtime-dependencies.js.map +1 -0
  115. package/dist/learning/session-store.d.ts +1 -5
  116. package/dist/learning/session-store.d.ts.map +1 -1
  117. package/dist/learning/session-store.js +10 -1
  118. package/dist/learning/session-store.js.map +1 -1
  119. package/dist/learning/skill-store.d.ts.map +1 -1
  120. package/dist/learning/skill-store.js +6 -5
  121. package/dist/learning/skill-store.js.map +1 -1
  122. package/dist/learning/step-checkpoint.d.ts +32 -3
  123. package/dist/learning/step-checkpoint.d.ts.map +1 -1
  124. package/dist/learning/step-checkpoint.js +33 -4
  125. package/dist/learning/step-checkpoint.js.map +1 -1
  126. package/dist/mcp/settings.d.ts +15 -3
  127. package/dist/mcp/settings.d.ts.map +1 -1
  128. package/dist/mcp/settings.js +30 -15
  129. package/dist/mcp/settings.js.map +1 -1
  130. package/dist/parity/drivers.d.ts.map +1 -1
  131. package/dist/parity/drivers.js +31 -5
  132. package/dist/parity/drivers.js.map +1 -1
  133. package/dist/skills/bundled-skills-phase3.d.ts +14 -0
  134. package/dist/skills/bundled-skills-phase3.d.ts.map +1 -1
  135. package/dist/skills/bundled-skills-phase3.js +50 -0
  136. package/dist/skills/bundled-skills-phase3.js.map +1 -1
  137. package/dist/tools/child-agent-runtime.d.ts.map +1 -1
  138. package/dist/tools/child-agent-runtime.js +13 -7
  139. package/dist/tools/child-agent-runtime.js.map +1 -1
  140. package/dist/tools/followup-utils.d.ts +36 -0
  141. package/dist/tools/followup-utils.d.ts.map +1 -1
  142. package/dist/tools/followup-utils.js +66 -0
  143. package/dist/tools/followup-utils.js.map +1 -1
  144. package/dist/tools/modality/browser.js +2 -2
  145. package/dist/tools/modality/browser.js.map +1 -1
  146. package/dist/tools/modality/image-gen.d.ts +1 -1
  147. package/dist/tools/modality/image-gen.js +1 -1
  148. package/dist/tools/modality/image-providers.js +1 -1
  149. package/dist/tools/modality/image-providers.js.map +1 -1
  150. package/dist/tools/modality/motion-presets.d.ts +62 -0
  151. package/dist/tools/modality/motion-presets.d.ts.map +1 -0
  152. package/dist/tools/modality/motion-presets.js +399 -0
  153. package/dist/tools/modality/motion-presets.js.map +1 -0
  154. package/dist/tools/modality/motion-tool.d.ts +62 -0
  155. package/dist/tools/modality/motion-tool.d.ts.map +1 -0
  156. package/dist/tools/modality/motion-tool.js +220 -0
  157. package/dist/tools/modality/motion-tool.js.map +1 -0
  158. package/dist/tools/modality/motion.d.ts +298 -0
  159. package/dist/tools/modality/motion.d.ts.map +1 -0
  160. package/dist/tools/modality/motion.js +514 -0
  161. package/dist/tools/modality/motion.js.map +1 -0
  162. package/dist/tools/modality/vision.d.ts +1 -1
  163. package/dist/tools/modality/vision.js +1 -1
  164. package/dist/tools/modality/voice.js +2 -2
  165. package/dist/tools/modality/voice.js.map +1 -1
  166. package/dist/tools/neutts-synth.d.ts +3 -3
  167. package/dist/tools/neutts-synth.js +3 -3
  168. package/dist/tools/neutts-synth.js.map +1 -1
  169. package/dist/tools/process-liveness.d.ts +50 -0
  170. package/dist/tools/process-liveness.d.ts.map +1 -1
  171. package/dist/tools/process-liveness.js +82 -9
  172. package/dist/tools/process-liveness.js.map +1 -1
  173. package/dist/tools/registry.d.ts +1 -1
  174. package/dist/tools/registry.d.ts.map +1 -1
  175. package/dist/tools/registry.js +102 -4
  176. package/dist/tools/registry.js.map +1 -1
  177. package/dist/tools/run-terminal.d.ts.map +1 -1
  178. package/dist/tools/run-terminal.js +18 -1
  179. package/dist/tools/run-terminal.js.map +1 -1
  180. package/dist/tools/tool-loop.d.ts.map +1 -1
  181. package/dist/tools/tool-loop.js +71 -19
  182. package/dist/tools/tool-loop.js.map +1 -1
  183. package/dist/tools/toolsets.js +2 -2
  184. package/dist/tools/toolsets.js.map +1 -1
  185. package/dist/tools/web-research.d.ts +1 -1
  186. package/dist/tools/web-research.js +3 -3
  187. package/dist/tools/web-research.js.map +1 -1
  188. package/dist/utils/workspace-need.d.ts +84 -0
  189. package/dist/utils/workspace-need.d.ts.map +1 -0
  190. package/dist/utils/workspace-need.js +227 -0
  191. package/dist/utils/workspace-need.js.map +1 -0
  192. package/dist/web-dashboard/chat-console.d.ts +78 -0
  193. package/dist/web-dashboard/chat-console.d.ts.map +1 -1
  194. package/dist/web-dashboard/chat-console.js +122 -12
  195. package/dist/web-dashboard/chat-console.js.map +1 -1
  196. package/dist/web-dashboard/hub-data.d.ts +9 -1
  197. package/dist/web-dashboard/hub-data.d.ts.map +1 -1
  198. package/dist/web-dashboard/hub-data.js +2 -0
  199. package/dist/web-dashboard/hub-data.js.map +1 -1
  200. package/dist/web-dashboard/project-context.d.ts +1 -1
  201. package/dist/web-dashboard/project-context.d.ts.map +1 -1
  202. package/dist/web-dashboard/project-context.js +7 -1
  203. package/dist/web-dashboard/project-context.js.map +1 -1
  204. package/dist/web-dashboard/server.d.ts +33 -0
  205. package/dist/web-dashboard/server.d.ts.map +1 -1
  206. package/dist/web-dashboard/server.js +326 -23
  207. package/dist/web-dashboard/server.js.map +1 -1
  208. package/dist/web-dashboard/src/types.d.ts +109 -0
  209. package/dist/web-dashboard/src/types.d.ts.map +1 -1
  210. package/dist/web-dashboard/task-runner.d.ts +17 -1
  211. package/dist/web-dashboard/task-runner.d.ts.map +1 -1
  212. package/dist/web-dashboard/task-runner.js +40 -4
  213. package/dist/web-dashboard/task-runner.js.map +1 -1
  214. package/dist/web-dashboard/workspace-guard.d.ts +20 -21
  215. package/dist/web-dashboard/workspace-guard.d.ts.map +1 -1
  216. package/dist/web-dashboard/workspace-guard.js +20 -153
  217. package/dist/web-dashboard/workspace-guard.js.map +1 -1
  218. package/dist/web-dashboard/workspace-resolution.d.ts +23 -5
  219. package/dist/web-dashboard/workspace-resolution.d.ts.map +1 -1
  220. package/dist/web-dashboard/workspace-resolution.js +22 -4
  221. package/dist/web-dashboard/workspace-resolution.js.map +1 -1
  222. package/package.json +1 -1
  223. package/src/web-dashboard/public/assets/index-CzRU84ET.css +1 -0
  224. package/src/web-dashboard/public/assets/index-V0rIqVbg.js +236 -0
  225. package/src/web-dashboard/public/assets/index-V0rIqVbg.js.map +1 -0
  226. package/src/web-dashboard/public/index.html +2 -2
  227. package/src/web-dashboard/public/assets/index-DycGBWwy.js +0 -236
  228. package/src/web-dashboard/public/assets/index-DycGBWwy.js.map +0 -1
  229. package/src/web-dashboard/public/assets/index-aO3aG7ex.css +0 -1
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Cost classification — the ONE place that decides which bucket a ledger row
3
+ * belongs to, and what "billed" means for it.
4
+ *
5
+ * WHY IT IS ITS OWN MODULE. Three surfaces judge the same rows — `cost-tracker`
6
+ * (the CLI summary), `web-dashboard/server` (the Cost page + `/api/cost`) and the
7
+ * reconciliation check — and the defect being fixed is precisely that two places
8
+ * priced the same call differently (a hardcoded ledger table vs the provider
9
+ * catalog, disagreeing 25×–100×). So the rule lives here, once, and every surface
10
+ * asks this module instead of deriving its own answer.
11
+ *
12
+ * THE ONE THING TO UNDERSTAND. A ledger row carries two different dollar amounts,
13
+ * and the bug was conflating them:
14
+ *
15
+ * billedUsd money we have EVIDENCE was charged.
16
+ * - exact when the provider reported the call's cost,
17
+ * - an estimate when the account is declared `paid` and a list
18
+ * price is known,
19
+ * - otherwise 0 (free tier, unknown price, pass-through gateway).
20
+ * listValueUsd what the same tokens would cost at the resolved list price —
21
+ * the VALUE a free/subscription tier absorbed, and the figure an
22
+ * undeclared account's rows sit at until the tier is declared.
23
+ *
24
+ * Neither is ever presented as the other, and "unpriced" is a first-class answer
25
+ * rather than a fabricated rate (see `model-pricing.ts` for why: a generic
26
+ * `$0.00010/1K` fallback invented $0.40 of spend on a localhost gateway and
27
+ * under-reported the one provider the user actually pays for).
28
+ *
29
+ * ─── LEGACY ROWS (read before changing classifyEntry) ───────────────────────
30
+ *
31
+ * Rows written before this module existed have no `costSource`: their `costUsd`
32
+ * came from the old hardcoded table. They are NOT trusted for classification and
33
+ * NOT silently relabelled. They are re-derived HERE, at read time, from the fields
34
+ * they do carry (provider, model, tokens, `costReported`) using TODAY's rules —
35
+ * and counted in `recomputedCalls` so a reader can tell re-derived rows from
36
+ * recorded ones. The file is never rewritten by a read: mutating a profile's
37
+ * history to make a dashboard look right is the failure this whole change exists
38
+ * to remove. `scripts/prune-cost-ledger-rows.mjs` is the explicit, dry-run-default
39
+ * way to rewrite them.
40
+ */
41
+ import { ConfigManager } from '../config/manager.js';
42
+ import { type BillingTier, type CostBucket, type CostSource } from './model-pricing.js';
43
+ /**
44
+ * Where the classification gets its account facts and price overrides. A seam,
45
+ * because `cost-tracker` records calls from deep inside adapters that hold no
46
+ * config, while tests need a deterministic account tier with no config file.
47
+ */
48
+ export interface PricingContext {
49
+ /** The account's declared billing tier for a provider (default `unknown`). */
50
+ billingTierFor(provider: string): BillingTier;
51
+ /** `pricing.<provider>` override from config, when the user set one. */
52
+ configPricingFor(provider: string): {
53
+ inputPer1K?: number;
54
+ outputPer1K?: number;
55
+ } | undefined;
56
+ /**
57
+ * A per-model price from a live catalogue, when a host has one wired in.
58
+ *
59
+ * Left unset by default on purpose: the live catalogue feed
60
+ * (`learning/catalog-feed.ts`, opt-in and OFF by default) imports the router,
61
+ * which imports `cost-tracker`, so importing it here would close a module
62
+ * cycle for a source of data that is usually absent anyway. A host that has
63
+ * both (the CLI's model surfaces do) can hand it over; a test can inject one to
64
+ * prove a per-model price outranks the provider default.
65
+ */
66
+ catalogRowFor?(provider: string, model: string): {
67
+ inputPer1K?: number;
68
+ outputPer1K?: number;
69
+ basis?: string;
70
+ } | undefined;
71
+ }
72
+ /** Everything is `unknown`/unset — the honest floor when config cannot be read. */
73
+ export declare const UNKNOWN_PRICING_CONTEXT: PricingContext;
74
+ /** Build a context from a loaded config (the production path). */
75
+ export declare function configPricingContext(cm: ConfigManager): PricingContext;
76
+ export declare function setPricingContext(ctx: PricingContext | null): void;
77
+ /** Drop the cached config-backed context (tests that switch profiles mid-file). */
78
+ export declare function resetPricingContextCache(): void;
79
+ /**
80
+ * The context to classify with. Config is read LAZILY and cached, and a failure
81
+ * to read it degrades to `unknown` rather than to an assumption — an accounting
82
+ * layer must never invent a tier.
83
+ */
84
+ export declare function getPricingContext(): PricingContext;
85
+ /**
86
+ * The fields classification reads. Deliberately minimal and structural: the
87
+ * ledger's own `CostEntry` satisfies it, and so does a plain object in a test —
88
+ * so this module never has to import the ledger it classifies.
89
+ */
90
+ export interface ClassifiableEntry {
91
+ provider: string;
92
+ model: string;
93
+ inputTokens: number;
94
+ outputTokens: number;
95
+ totalTokens?: number;
96
+ costUsd: number;
97
+ measured?: boolean;
98
+ /** The provider stated this call's exact charge. */
99
+ costReported?: boolean;
100
+ /** Written by this module's rules (absent on legacy rows). */
101
+ costSource?: CostSource;
102
+ /** List-price VALUE of the call, when a price exists (absent on legacy rows). */
103
+ listPriceUsd?: number;
104
+ /** The tier in force when the row was recorded. */
105
+ billingTier?: BillingTier;
106
+ /** One line naming the evidence. */
107
+ pricingBasis?: string;
108
+ /** `live` or `test` (see `telemetryOrigin`). Absent on legacy rows. */
109
+ origin?: 'live' | 'test';
110
+ timestamp?: number;
111
+ }
112
+ /** One classified row. */
113
+ export interface ClassifiedEntry {
114
+ billedUsd: number;
115
+ listValueUsd?: number;
116
+ costSource: CostSource;
117
+ bucket: CostBucket;
118
+ billingTier: BillingTier;
119
+ basis: string;
120
+ /** True when a legacy row was re-derived here instead of read as recorded. */
121
+ recomputed: boolean;
122
+ }
123
+ /**
124
+ * Classify ONE ledger row.
125
+ *
126
+ * A row that already carries `costSource` is taken at its word — it was written
127
+ * by these rules, and re-deriving it with a config that may have changed since
128
+ * would rewrite history. A legacy row (no `costSource`) is re-derived from the
129
+ * fields it has; `costReported` is the one thing a legacy row can be exact about.
130
+ */
131
+ export declare function classifyEntry(entry: ClassifiableEntry, ctx?: PricingContext): ClassifiedEntry;
132
+ /** Per-provider / per-model roll-up of one bucket set. */
133
+ export interface CostBucketTotals {
134
+ billedUsd: number;
135
+ unverifiedListedUsd: number;
136
+ notBilledValueUsd: number;
137
+ unpricedCalls: number;
138
+ /** Calls whose list price is known but whose billing tier is undeclared. */
139
+ unverifiedCalls: number;
140
+ /** Calls a free/subscription tier absorbed. */
141
+ valueCalls: number;
142
+ requests: number;
143
+ }
144
+ export interface CostTotals {
145
+ /** Exact, provider-stated. */
146
+ billedReportedUsd: number;
147
+ billedReportedCalls: number;
148
+ /** List-priced with the account declared paid — an estimate of money owed. */
149
+ billedListedUsd: number;
150
+ billedListedCalls: number;
151
+ /** The two above together: the evidence-backed bill. */
152
+ billedUsd: number;
153
+ /** List-priced with the tier undeclared: shown, never presented as spend. */
154
+ unverifiedListedUsd: number;
155
+ unverifiedListedCalls: number;
156
+ /** Value a free/subscription tier absorbed — never spend. */
157
+ notBilledValueUsd: number;
158
+ notBilledValueCalls: number;
159
+ /** Calls with no dollars at all (unknown price, or a pass-through gateway). */
160
+ unpricedCalls: number;
161
+ unpricedTokens: number;
162
+ /** Rows re-derived at read time because they predate `costSource`. */
163
+ recomputedCalls: number;
164
+ /** Rows tagged test-origin (a display surface may exclude these). */
165
+ testOriginCalls: number;
166
+ /** Sum of the STORED `costUsd` — the ledger total as recorded. */
167
+ recordedTotalUsd: number;
168
+ tokens: number;
169
+ requests: number;
170
+ byProvider: Record<string, CostBucketTotals>;
171
+ byModel: Record<string, CostBucketTotals>;
172
+ }
173
+ /**
174
+ * Classify a list of rows and sum them.
175
+ *
176
+ * `excludeTestOrigin` defaults to FALSE, deliberately: this function is also how
177
+ * a TEST measures its own writes (`costSince` reads back rows that are, by
178
+ * definition, test-origin in a test process), so dropping them here would make
179
+ * the ledger read as empty the moment a test runner was involved. The exclusion
180
+ * belongs to the DISPLAY surfaces — the dashboard's cost endpoint and the CLI
181
+ * summary opt in — because that is the claim being made: "this is YOUR usage".
182
+ * `testOriginCalls` is always reported, so a surface can say how many it hid.
183
+ */
184
+ export declare function classifyEntries(entries: readonly ClassifiableEntry[], opts?: {
185
+ ctx?: PricingContext;
186
+ excludeTestOrigin?: boolean;
187
+ }): CostTotals;
188
+ //# sourceMappingURL=cost-classification.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost-classification.d.ts","sourceRoot":"","sources":["../../src/learning/cost-classification.ts"],"names":[],"mappings":"AAAA;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,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,wDAAwD;IACxD,SAAS,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,mBAAmB,EAAE,MAAM,CAAC;IAC5B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,6DAA6D;IAC7D,iBAAiB,EAAE,MAAM,CAAC;IAC1B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,+EAA+E;IAC/E,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,eAAe,EAAE,MAAM,CAAC;IACxB,qEAAqE;IACrE,eAAe,EAAE,MAAM,CAAC;IACxB,kEAAkE;IAClE,gBAAgB,EAAE,MAAM,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC7C,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAC3C;AAcD;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,IAAI,GAAE;IAAE,GAAG,CAAC,EAAE,cAAc,CAAC;IAAC,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAAO,GAC/D,UAAU,CAoFZ"}
@@ -0,0 +1,294 @@
1
+ /**
2
+ * Cost classification — the ONE place that decides which bucket a ledger row
3
+ * belongs to, and what "billed" means for it.
4
+ *
5
+ * WHY IT IS ITS OWN MODULE. Three surfaces judge the same rows — `cost-tracker`
6
+ * (the CLI summary), `web-dashboard/server` (the Cost page + `/api/cost`) and the
7
+ * reconciliation check — and the defect being fixed is precisely that two places
8
+ * priced the same call differently (a hardcoded ledger table vs the provider
9
+ * catalog, disagreeing 25×–100×). So the rule lives here, once, and every surface
10
+ * asks this module instead of deriving its own answer.
11
+ *
12
+ * THE ONE THING TO UNDERSTAND. A ledger row carries two different dollar amounts,
13
+ * and the bug was conflating them:
14
+ *
15
+ * billedUsd money we have EVIDENCE was charged.
16
+ * - exact when the provider reported the call's cost,
17
+ * - an estimate when the account is declared `paid` and a list
18
+ * price is known,
19
+ * - otherwise 0 (free tier, unknown price, pass-through gateway).
20
+ * listValueUsd what the same tokens would cost at the resolved list price —
21
+ * the VALUE a free/subscription tier absorbed, and the figure an
22
+ * undeclared account's rows sit at until the tier is declared.
23
+ *
24
+ * Neither is ever presented as the other, and "unpriced" is a first-class answer
25
+ * rather than a fabricated rate (see `model-pricing.ts` for why: a generic
26
+ * `$0.00010/1K` fallback invented $0.40 of spend on a localhost gateway and
27
+ * under-reported the one provider the user actually pays for).
28
+ *
29
+ * ─── LEGACY ROWS (read before changing classifyEntry) ───────────────────────
30
+ *
31
+ * Rows written before this module existed have no `costSource`: their `costUsd`
32
+ * came from the old hardcoded table. They are NOT trusted for classification and
33
+ * NOT silently relabelled. They are re-derived HERE, at read time, from the fields
34
+ * they do carry (provider, model, tokens, `costReported`) using TODAY's rules —
35
+ * and counted in `recomputedCalls` so a reader can tell re-derived rows from
36
+ * recorded ones. The file is never rewritten by a read: mutating a profile's
37
+ * history to make a dashboard look right is the failure this whole change exists
38
+ * to remove. `scripts/prune-cost-ledger-rows.mjs` is the explicit, dry-run-default
39
+ * way to rewrite them.
40
+ */
41
+ import { ConfigManager } from '../config/manager.js';
42
+ import { asBillingTier, resolveCallCost, } from './model-pricing.js';
43
+ /** Everything is `unknown`/unset — the honest floor when config cannot be read. */
44
+ export const UNKNOWN_PRICING_CONTEXT = {
45
+ billingTierFor: () => 'unknown',
46
+ configPricingFor: () => undefined,
47
+ };
48
+ /** Build a context from a loaded config (the production path). */
49
+ export function configPricingContext(cm) {
50
+ return {
51
+ billingTierFor(provider) {
52
+ try {
53
+ const p = (cm.getAll().providers || {})[provider];
54
+ return asBillingTier(p?.billingTier);
55
+ }
56
+ catch {
57
+ return 'unknown';
58
+ }
59
+ },
60
+ configPricingFor(provider) {
61
+ try {
62
+ const p = (cm.getAll().pricing || {})[provider];
63
+ if (!p)
64
+ return undefined;
65
+ const input = typeof p.inputPer1K === 'number' ? p.inputPer1K : undefined;
66
+ const output = typeof p.outputPer1K === 'number' ? p.outputPer1K : undefined;
67
+ if (input === undefined && output === undefined)
68
+ return undefined;
69
+ // A half-declared override is not a price: filling the missing side with
70
+ // the other one would invent a rate the user never wrote.
71
+ if (input === undefined || output === undefined)
72
+ return undefined;
73
+ return { inputPer1K: input, outputPer1K: output };
74
+ }
75
+ catch {
76
+ return undefined;
77
+ }
78
+ },
79
+ };
80
+ }
81
+ /** A process-wide override for hosts and tests. `null` restores the config path. */
82
+ let overrideContext = null;
83
+ let cachedConfigContext = null;
84
+ export function setPricingContext(ctx) {
85
+ overrideContext = ctx;
86
+ }
87
+ /** Drop the cached config-backed context (tests that switch profiles mid-file). */
88
+ export function resetPricingContextCache() {
89
+ cachedConfigContext = null;
90
+ overrideContext = null;
91
+ }
92
+ /**
93
+ * The context to classify with. Config is read LAZILY and cached, and a failure
94
+ * to read it degrades to `unknown` rather than to an assumption — an accounting
95
+ * layer must never invent a tier.
96
+ */
97
+ export function getPricingContext() {
98
+ if (overrideContext)
99
+ return overrideContext;
100
+ if (!cachedConfigContext) {
101
+ try {
102
+ cachedConfigContext = configPricingContext(new ConfigManager());
103
+ }
104
+ catch {
105
+ cachedConfigContext = UNKNOWN_PRICING_CONTEXT;
106
+ }
107
+ }
108
+ return cachedConfigContext;
109
+ }
110
+ const round6 = (usd) => Math.round(usd * 100000) / 100000;
111
+ /**
112
+ * Classify ONE ledger row.
113
+ *
114
+ * A row that already carries `costSource` is taken at its word — it was written
115
+ * by these rules, and re-deriving it with a config that may have changed since
116
+ * would rewrite history. A legacy row (no `costSource`) is re-derived from the
117
+ * fields it has; `costReported` is the one thing a legacy row can be exact about.
118
+ */
119
+ export function classifyEntry(entry, ctx = getPricingContext()) {
120
+ if (entry.costReported === true) {
121
+ // The provider's own figure, exact — for a legacy row too (the flag was
122
+ // written by the old code and means the same thing).
123
+ return {
124
+ billedUsd: round6(entry.costUsd ?? 0),
125
+ ...(typeof entry.listPriceUsd === 'number' ? { listValueUsd: entry.listPriceUsd } : {}),
126
+ costSource: 'reported',
127
+ bucket: 'billed-reported',
128
+ billingTier: entry.billingTier ?? ctx.billingTierFor(entry.provider),
129
+ basis: entry.pricingBasis ?? 'provider-reported cost for this call (exact)',
130
+ recomputed: entry.costSource === undefined,
131
+ };
132
+ }
133
+ if (entry.costSource !== undefined) {
134
+ // Recorded by the current rules: its numbers stand, its bucket is derived.
135
+ const tier = entry.billingTier ?? ctx.billingTierFor(entry.provider);
136
+ return {
137
+ billedUsd: round6(entry.costUsd ?? 0),
138
+ ...(typeof entry.listPriceUsd === 'number' ? { listValueUsd: entry.listPriceUsd } : {}),
139
+ costSource: entry.costSource,
140
+ bucket: bucketOf(entry.costSource, tier),
141
+ billingTier: tier,
142
+ basis: entry.pricingBasis ?? 'recorded by the pricing rules',
143
+ recomputed: false,
144
+ };
145
+ }
146
+ // Legacy row: re-derive with today's rules. `reportedCostUsd` is deliberately
147
+ // NOT passed — the stored `costUsd` is the OLD table's number, and feeding it
148
+ // in as though the provider had said it would launder an estimate into an exact
149
+ // figure, which is the single worst mistake this module could make.
150
+ const resolved = resolveCallCost({
151
+ provider: entry.provider,
152
+ model: entry.model,
153
+ inputTokens: entry.inputTokens ?? 0,
154
+ outputTokens: entry.outputTokens ?? 0,
155
+ billingTier: ctx.billingTierFor(entry.provider),
156
+ configPricing: ctx.configPricingFor(entry.provider),
157
+ catalogRow: ctx.catalogRowFor?.(entry.provider, entry.model),
158
+ });
159
+ return {
160
+ billedUsd: resolved.billedUsd,
161
+ ...(resolved.listUsd !== undefined ? { listValueUsd: resolved.listUsd } : {}),
162
+ costSource: resolved.costSource,
163
+ bucket: resolved.bucket,
164
+ billingTier: resolved.billingTier,
165
+ basis: resolved.basis,
166
+ recomputed: true,
167
+ };
168
+ }
169
+ /** The bucket for a recorded row — mirrors `model-pricing.bucketFor` for stored data. */
170
+ function bucketOf(source, tier) {
171
+ switch (source) {
172
+ case 'reported':
173
+ return 'billed-reported';
174
+ case 'free':
175
+ return 'not-billed-value';
176
+ case 'passthrough':
177
+ case 'unknown':
178
+ return 'unpriced';
179
+ case 'catalog':
180
+ return tier === 'paid' ? 'billed-listed' : 'unverified-listed';
181
+ }
182
+ }
183
+ function emptyProviderTotals() {
184
+ return {
185
+ billedUsd: 0,
186
+ unverifiedListedUsd: 0,
187
+ notBilledValueUsd: 0,
188
+ unpricedCalls: 0,
189
+ unverifiedCalls: 0,
190
+ valueCalls: 0,
191
+ requests: 0,
192
+ };
193
+ }
194
+ /**
195
+ * Classify a list of rows and sum them.
196
+ *
197
+ * `excludeTestOrigin` defaults to FALSE, deliberately: this function is also how
198
+ * a TEST measures its own writes (`costSince` reads back rows that are, by
199
+ * definition, test-origin in a test process), so dropping them here would make
200
+ * the ledger read as empty the moment a test runner was involved. The exclusion
201
+ * belongs to the DISPLAY surfaces — the dashboard's cost endpoint and the CLI
202
+ * summary opt in — because that is the claim being made: "this is YOUR usage".
203
+ * `testOriginCalls` is always reported, so a surface can say how many it hid.
204
+ */
205
+ export function classifyEntries(entries, opts = {}) {
206
+ const ctx = opts.ctx ?? getPricingContext();
207
+ const totals = {
208
+ billedReportedUsd: 0,
209
+ billedReportedCalls: 0,
210
+ billedListedUsd: 0,
211
+ billedListedCalls: 0,
212
+ billedUsd: 0,
213
+ unverifiedListedUsd: 0,
214
+ unverifiedListedCalls: 0,
215
+ notBilledValueUsd: 0,
216
+ notBilledValueCalls: 0,
217
+ unpricedCalls: 0,
218
+ unpricedTokens: 0,
219
+ recomputedCalls: 0,
220
+ testOriginCalls: 0,
221
+ recordedTotalUsd: 0,
222
+ tokens: 0,
223
+ requests: 0,
224
+ byProvider: {},
225
+ byModel: {},
226
+ };
227
+ for (const entry of entries) {
228
+ if (entry.origin === 'test') {
229
+ totals.testOriginCalls += 1;
230
+ if (opts.excludeTestOrigin)
231
+ continue;
232
+ }
233
+ const c = classifyEntry(entry, ctx);
234
+ const tokens = entry.totalTokens ?? (entry.inputTokens ?? 0) + (entry.outputTokens ?? 0);
235
+ totals.requests += 1;
236
+ totals.tokens += tokens;
237
+ totals.recordedTotalUsd += entry.costUsd ?? 0;
238
+ if (c.recomputed)
239
+ totals.recomputedCalls += 1;
240
+ if (c.bucket === 'billed-reported') {
241
+ totals.billedReportedUsd += c.billedUsd;
242
+ totals.billedReportedCalls += 1;
243
+ }
244
+ else if (c.bucket === 'billed-listed') {
245
+ totals.billedListedUsd += c.billedUsd;
246
+ totals.billedListedCalls += 1;
247
+ }
248
+ else if (c.bucket === 'unverified-listed') {
249
+ totals.unverifiedListedUsd += c.listValueUsd ?? 0;
250
+ totals.unverifiedListedCalls += 1;
251
+ }
252
+ else if (c.bucket === 'not-billed-value') {
253
+ totals.notBilledValueUsd += c.listValueUsd ?? 0;
254
+ totals.notBilledValueCalls += 1;
255
+ }
256
+ else {
257
+ totals.unpricedCalls += 1;
258
+ totals.unpricedTokens += tokens;
259
+ }
260
+ const provider = entry.provider || 'unknown';
261
+ const p = (totals.byProvider[provider] ??= emptyProviderTotals());
262
+ const m = (totals.byModel[entry.model || 'unknown'] ??= emptyProviderTotals());
263
+ p.requests += 1;
264
+ m.requests += 1;
265
+ if (c.bucket === 'billed-reported' || c.bucket === 'billed-listed') {
266
+ p.billedUsd += c.billedUsd;
267
+ m.billedUsd += c.billedUsd;
268
+ }
269
+ else if (c.bucket === 'unverified-listed') {
270
+ p.unverifiedListedUsd += c.listValueUsd ?? 0;
271
+ p.unverifiedCalls += 1;
272
+ m.unverifiedListedUsd += c.listValueUsd ?? 0;
273
+ m.unverifiedCalls += 1;
274
+ }
275
+ else if (c.bucket === 'not-billed-value') {
276
+ p.notBilledValueUsd += c.listValueUsd ?? 0;
277
+ p.valueCalls += 1;
278
+ m.notBilledValueUsd += c.listValueUsd ?? 0;
279
+ m.valueCalls += 1;
280
+ }
281
+ else {
282
+ p.unpricedCalls += 1;
283
+ m.unpricedCalls += 1;
284
+ }
285
+ }
286
+ totals.billedUsd = round6(totals.billedReportedUsd + totals.billedListedUsd);
287
+ totals.billedReportedUsd = round6(totals.billedReportedUsd);
288
+ totals.billedListedUsd = round6(totals.billedListedUsd);
289
+ totals.unverifiedListedUsd = round6(totals.unverifiedListedUsd);
290
+ totals.notBilledValueUsd = round6(totals.notBilledValueUsd);
291
+ totals.recordedTotalUsd = round6(totals.recordedTotalUsd);
292
+ return totals;
293
+ }
294
+ //# sourceMappingURL=cost-classification.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost-classification.js","sourceRoot":"","sources":["../../src/learning/cost-classification.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EACL,aAAa,EACb,eAAe,GAIhB,MAAM,oBAAoB,CAAC;AA8B5B,mFAAmF;AACnF,MAAM,CAAC,MAAM,uBAAuB,GAAmB;IACrD,cAAc,EAAE,GAAG,EAAE,CAAC,SAAS;IAC/B,gBAAgB,EAAE,GAAG,EAAE,CAAC,SAAS;CAClC,CAAC;AAEF,kEAAkE;AAClE,MAAM,UAAU,oBAAoB,CAAC,EAAiB;IACpD,OAAO;QACL,cAAc,CAAC,QAAgB;YAC7B,IAAI,CAAC;gBACH,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,QAAQ,CAA0C,CAAC;gBAC3F,OAAO,aAAa,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;YACvC,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;QACD,gBAAgB,CAAC,QAAgB;YAC/B,IAAI,CAAC;gBACH,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,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,UAAU,IAAI,CAAC,CAAC;YAClB,CAAC,CAAC,iBAAiB,IAAI,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC;YAC3C,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC;QACpB,CAAC;aAAM,CAAC;YACN,CAAC,CAAC,aAAa,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC,aAAa,IAAI,CAAC,CAAC;QACvB,CAAC;IACH,CAAC;IAED,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;IAC7E,MAAM,CAAC,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAC5D,MAAM,CAAC,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IACxD,MAAM,CAAC,mBAAmB,GAAG,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;IAChE,MAAM,CAAC,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAC5D,MAAM,CAAC,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;IAC1D,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Cost-ledger maintenance — the EXPLICIT, dry-run-default way to repair the rows
3
+ * already on disk. Nothing here runs on a read: the ledger is repaired only when
4
+ * an operator asks for it, and every figure it changes is reported before and
5
+ * after.
6
+ *
7
+ * WHY IT EXISTS. Two classes of row in a real profile are not what they look like,
8
+ * and neither can be fixed by a read:
9
+ *
10
+ * 1. LEGACY PRICES. Rows written before `costSource` existed carry a `costUsd`
11
+ * computed from the retired hardcoded table (`cost-tracker.ts`'s five-entry
12
+ * `DEFAULT_PRICING` plus a `$0.00010/1K` generic fallback). Read-side
13
+ * classification already re-derives them for DISPLAY (`cost-classification.ts`
14
+ * counts them in `recomputedCalls`), and deliberately never rewrites the file
15
+ * — but `recordedTotalUsd` ("the ledger total as recorded") keeps showing the
16
+ * old number, and a reprice is what turns the file itself honest.
17
+ * 2. TEST-ORIGIN ROWS. A row a test run wrote into a real profile. Rows written
18
+ * since `origin` was added say so (`origin: 'test'`); rows from before have no
19
+ * field at all, and the only sound evidence left in them is the model id — a
20
+ * fixture id (`wire-stub-model`: MEASURED 747 rows in a real
21
+ * `~/.nuvira/memory/cost-tracker.json`) could not have come from a real call.
22
+ *
23
+ * ─── WHAT IT REFUSES TO DO (read before changing either rule) ───────────────
24
+ *
25
+ * - It does NOT re-derive a row that already carries `costSource`. That row was
26
+ * written by today's rules under the config in force at the time; re-pricing it
27
+ * with a config that has changed since would rewrite history to match the
28
+ * present — the exact dishonesty this whole change removes.
29
+ * - It does NOT pass a legacy row's stored `costUsd` back in as though the
30
+ * provider had reported it. The stored number is the old estimate; feeding it
31
+ * into `resolveCallCost` as `reportedCostUsd` would LAUNDER an estimate into an
32
+ * exact-looking figure. Only a row already flagged `costReported: true` keeps
33
+ * its number verbatim, because that flag was written by the old code and means
34
+ * the same thing.
35
+ * - It does NOT guess `origin`. A legacy row gets no origin field: we do not know
36
+ * which process wrote it, and stamping `live` would make it unprunable forever.
37
+ * - It NEVER writes without `apply`, and a write always leaves a `.bak` beside the
38
+ * file (the ledger is the user's billing history; a reprice is reversible).
39
+ */
40
+ import type { CostEntry } from './cost-tracker.js';
41
+ import { type PricingContext } from './cost-classification.js';
42
+ /** One grouped removal, so the report says WHAT is being dropped, not just how many. */
43
+ export interface RemovedRows {
44
+ provider: string;
45
+ model: string;
46
+ count: number;
47
+ /** True when the id itself is the evidence (a fixture id), not an `origin` tag. */
48
+ fixtureId: boolean;
49
+ }
50
+ export interface LedgerMaintenanceReport {
51
+ file: string;
52
+ applied: boolean;
53
+ backupPath?: string;
54
+ rows: {
55
+ total: number;
56
+ /** Legacy rows re-derived by today's rules (they gained `costSource`). */
57
+ repriced: number;
58
+ /** Rows already written by these rules — left exactly as they were. */
59
+ alreadyClassified: number;
60
+ /** Rows flagged `origin: 'test'`. */
61
+ testOriginRemoved: number;
62
+ /** Rows whose model id could only be a test fixture (no `origin` field). */
63
+ fixtureRemoved: number;
64
+ kept: number;
65
+ };
66
+ /** The ledger's own recorded sum before and after — the number a reader sees. */
67
+ before: {
68
+ recordedUsd: number;
69
+ tokens: number;
70
+ unpricedCalls: number;
71
+ };
72
+ /**
73
+ * Re-priced totals, classified: `recordedUsd` is the new sum of the rows'
74
+ * `costUsd` (the evidence-backed figure), and the rest is the honest split.
75
+ */
76
+ after: {
77
+ recordedUsd: number;
78
+ tokens: number;
79
+ billedUsd: number;
80
+ billedReportedUsd: number;
81
+ billedListedUsd: number;
82
+ unverifiedListedUsd: number;
83
+ notBilledValueUsd: number;
84
+ unpricedCalls: number;
85
+ };
86
+ removed: RemovedRows[];
87
+ /** Plain-language notes: what changed and what still needs the operator. */
88
+ notes: string[];
89
+ }
90
+ /** True when the row is test-origin by evidence we can actually read. */
91
+ export declare function testRowVerdict(entry: {
92
+ origin?: string;
93
+ model?: string;
94
+ }): 'test-origin' | 'fixture-id' | null;
95
+ /**
96
+ * Re-price a set of ledger rows and (optionally) drop the test rows among them.
97
+ * Pure: takes rows, returns rows — the file is written by `maintainCostLedger`.
98
+ */
99
+ export declare function repriceLedgerEntries(entries: readonly CostEntry[], opts?: {
100
+ ctx?: PricingContext;
101
+ pruneTestRows?: boolean;
102
+ }): {
103
+ entries: CostEntry[];
104
+ report: Omit<LedgerMaintenanceReport, 'file' | 'applied' | 'backupPath'>;
105
+ };
106
+ /**
107
+ * The whole pass against a FILE. `apply: false` (the default) reads and reports
108
+ * only — the dry run is the default because the ledger is billing history.
109
+ *
110
+ * On `apply` the previous file is copied to `<file>.bak` first (a fixed name, so
111
+ * the backup is the state immediately before THIS run and cannot itself grow a
112
+ * chain), and the rewrite goes through a temp-free single write.
113
+ */
114
+ export declare function maintainCostLedger(opts?: {
115
+ file?: string;
116
+ apply?: boolean;
117
+ pruneTestRows?: boolean;
118
+ ctx?: PricingContext;
119
+ }): LedgerMaintenanceReport;
120
+ /** The report as text — shared by the CLI and the scripts wrapper. */
121
+ export declare function formatMaintenanceReport(rep: LedgerMaintenanceReport): string;
122
+ //# sourceMappingURL=cost-ledger-maintenance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cost-ledger-maintenance.d.ts","sourceRoot":"","sources":["../../src/learning/cost-ledger-maintenance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,EAIL,KAAK,cAAc,EACpB,MAAM,0BAA0B,CAAC;AAWlC,wFAAwF;AACxF,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,mFAAmF;IACnF,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE;QACJ,KAAK,EAAE,MAAM,CAAC;QACd,0EAA0E;QAC1E,QAAQ,EAAE,MAAM,CAAC;QACjB,uEAAuE;QACvE,iBAAiB,EAAE,MAAM,CAAC;QAC1B,qCAAqC;QACrC,iBAAiB,EAAE,MAAM,CAAC;QAC1B,4EAA4E;QAC5E,cAAc,EAAE,MAAM,CAAC;QACvB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,iFAAiF;IACjF,MAAM,EAAE;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;IACvE;;;OAGG;IACH,KAAK,EAAE;QACL,WAAW,EAAE,MAAM,CAAC;QAC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