ctxtop 4.0.0 → 4.1.0

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 (89) hide show
  1. package/.antigravity-plugin/plugin.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHECKSUMS.sha256 +59 -42
  4. package/README.md +351 -330
  5. package/bin/ah-statusline.mjs +30 -0
  6. package/bin/ctxtop.mjs +330 -5
  7. package/config/default_flightcheck.json +19 -0
  8. package/config/modelCatalog.json +165 -0
  9. package/hooks/handlers/compact-lifecycle.mjs +323 -8
  10. package/hooks/handlers/config-change.mjs +402 -0
  11. package/hooks/handlers/hookIo.mjs +56 -0
  12. package/hooks/handlers/post-model-switch.mjs +111 -0
  13. package/hooks/handlers/post-tool-use.mjs +117 -16
  14. package/hooks/handlers/pre-model-switch.mjs +823 -24
  15. package/hooks/handlers/pre-tool-use.mjs +271 -4
  16. package/hooks/handlers/stop-gate.mjs +297 -11
  17. package/hooks/handlers/user-prompt-submit.mjs +224 -0
  18. package/hooks/hooks.json +50 -0
  19. package/hooks/post_tool_use.mjs +8 -0
  20. package/hooks/register.js +48 -9
  21. package/hooks/session_start.mjs +11 -1
  22. package/hooks/stop.mjs +11 -1
  23. package/package.json +11 -4
  24. package/packages/antigravity/package.json +1 -1
  25. package/packages/claude-code/package.json +1 -1
  26. package/packages/core/package.json +1 -1
  27. package/packages/engine-node/package.json +1 -1
  28. package/packages/mcp/package.json +1 -1
  29. package/packages/mcp/src/server.mjs +1 -1
  30. package/packages/pro/package.json +1 -1
  31. package/plugin.json +1 -1
  32. package/sidecars/horizon-daemon/server.mjs +38 -2
  33. package/skills/handoff/SKILL.md +8 -0
  34. package/src/adapters/antigravity/index.mjs +31 -1
  35. package/src/adapters/claude-code/index.mjs +40 -1
  36. package/src/cli/horizon_hud.mjs +254 -52
  37. package/src/config/modelCatalog.json +84 -0
  38. package/src/core/bucket_b_runtime.mjs +1200 -0
  39. package/src/core/constants.mjs +191 -4
  40. package/src/core/detectors_det01_to_16.mjs +54 -5
  41. package/src/core/doc_indexer.mjs +338 -0
  42. package/src/core/layer5_hygiene.mjs +118 -0
  43. package/src/core/parser.mjs +2 -1
  44. package/src/core/paths.mjs +3 -2
  45. package/src/core/rsl_clash_auditor.mjs +423 -0
  46. package/src/core/rsl_distiller.mjs +372 -0
  47. package/src/core/rsl_ledger.mjs +969 -0
  48. package/src/core/rsl_triage.mjs +909 -0
  49. package/src/core/session_auditor.mjs +658 -0
  50. package/src/core/shield_manager.mjs +1417 -0
  51. package/src/core/task_quota_router.mjs +1 -1
  52. package/src/core-isomorphic/coach/cacheGravityRouter.mjs +874 -47
  53. package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
  54. package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
  55. package/src/core-isomorphic/coach/ttlWatchdog.mjs +13 -6
  56. package/src/core-isomorphic/config/anthropicModelCatalog.json +404 -1
  57. package/src/core-isomorphic/config/catalogLoader.mjs +546 -46
  58. package/src/core-isomorphic/config/horizonConfig.mjs +47 -1
  59. package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +266 -0
  60. package/src/core-isomorphic/economics/cacheGravity.mjs +18 -0
  61. package/src/core-isomorphic/economics/cliffBreaker.mjs +98 -12
  62. package/src/core-isomorphic/economics/driftLatency.mjs +114 -0
  63. package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
  64. package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
  65. package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
  66. package/src/core-isomorphic/guards/circuitBreakers.mjs +191 -191
  67. package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +154 -5
  68. package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +103 -11
  69. package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +311 -1
  70. package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
  71. package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +16 -0
  72. package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
  73. package/src/core-isomorphic/index.js +1 -0
  74. package/src/core-isomorphic/telemetry/telemetryBridge.mjs +36 -12
  75. package/src/core-isomorphic/ui/horizonPanelHtml.mjs +293 -237
  76. package/src/core-isomorphic/ui/modViews.mjs +35 -9
  77. package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
  78. package/src/diagnostics/doctorEngine.mjs +95 -3
  79. package/src/engine-node/db/probe.mjs +234 -2
  80. package/src/engine-node/index.mjs +3 -0
  81. package/src/engine-node/sensors/blindspots.mjs +13 -6
  82. package/src/hooks/antigravity_rsl_hook.mjs +250 -0
  83. package/src/hooks/claude_rsl_hook.mjs +288 -0
  84. package/src/hooks/post_invocation.mjs +196 -4
  85. package/src/hooks/pre_invocation.mjs +732 -22
  86. package/src/hooks/session_start.mjs +39 -1
  87. package/src/utils/modelSwitchTelemetry.mjs +308 -0
  88. package/src/utils/sessionCalibration.mjs +657 -0
  89. package/src/webview/horizon_panel.html +1325 -317
@@ -14,7 +14,10 @@ import {
14
14
  } from '../utils/runtime.mjs';
15
15
 
16
16
  export const BUILTIN_CATALOG = {
17
- schemaVersion: '2026.11.1',
17
+ schemaVersion: '2026.11.15',
18
+ catalogVersion: '2026-11-15',
19
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models, 2026-11-15) & Claude Code 2.1.325 bakedCatalog',
20
+ dateRead: '2026-11-15',
18
21
  defaultModel: 'claude-sonnet-5-5',
19
22
  cacheWriteMultipliers: {
20
23
  '5m': 1.25,
@@ -37,6 +40,8 @@ export const BUILTIN_CATALOG = {
37
40
  operationalSoftCeiling: 180000,
38
41
  ratesPer1M: { input: 8.0, output: 40.0, write5m: 10.0, write1h: 16.0, read: 0.2 },
39
42
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
43
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
44
+ dateRead: '2026-11-15',
40
45
  pricingCliff: null
41
46
  },
42
47
  'claude-fable-5-1': {
@@ -49,6 +54,22 @@ export const BUILTIN_CATALOG = {
49
54
  operationalSoftCeiling: 180000,
50
55
  ratesPer1M: { input: 10.0, output: 50.0, write5m: 12.5, write1h: 20.0, read: 0.25 },
51
56
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
57
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
58
+ dateRead: '2026-11-15',
59
+ pricingCliff: null
60
+ },
61
+ 'claude-fable-5': {
62
+ id: 'claude-fable-5',
63
+ label: 'Claude Fable 5',
64
+ shortLabel: 'Fable 5',
65
+ family: 'fable',
66
+ generation: '5',
67
+ contextCeiling: 1000000,
68
+ operationalSoftCeiling: 180000,
69
+ ratesPer1M: { input: 10.0, output: 50.0, write5m: 12.5, write1h: 20.0, read: 1.0 },
70
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
71
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models) & Claude Code 2.1.325 bakedCatalog (tier_10_50)',
72
+ dateRead: '2026-11-15',
52
73
  pricingCliff: null
53
74
  },
54
75
  'claude-opus-5-5': {
@@ -61,6 +82,8 @@ export const BUILTIN_CATALOG = {
61
82
  operationalSoftCeiling: 180000,
62
83
  ratesPer1M: { input: 4.0, output: 20.0, write5m: 5.0, write1h: 8.0, read: 0.2 },
63
84
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.05 },
85
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
86
+ dateRead: '2026-11-15',
64
87
  pricingCliff: null
65
88
  },
66
89
  'claude-sonnet-5-5': {
@@ -73,6 +96,8 @@ export const BUILTIN_CATALOG = {
73
96
  operationalSoftCeiling: 180000,
74
97
  ratesPer1M: { input: 2.0, output: 10.0, write5m: 2.5, write1h: 4.0, read: 0.1 },
75
98
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.05 },
99
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
100
+ dateRead: '2026-11-15',
76
101
  pricingCliff: null
77
102
  },
78
103
  'claude-haiku-5-5': {
@@ -85,6 +110,8 @@ export const BUILTIN_CATALOG = {
85
110
  operationalSoftCeiling: 90000,
86
111
  ratesPer1M: { input: 0.1, output: 0.5, write5m: 0.125, write1h: 0.2, read: 0.01 },
87
112
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
113
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models) & Claude Code 2.1.325 bakedCatalog (haiku_55)',
114
+ dateRead: '2026-11-15',
88
115
  pricingCliff: {
89
116
  thresholdTokens: 100000,
90
117
  preCliffWarningTokens: 90000,
@@ -98,6 +125,62 @@ export const BUILTIN_CATALOG = {
98
125
  }
99
126
  }
100
127
  },
128
+ 'claude-opus-5': {
129
+ id: 'claude-opus-5',
130
+ label: 'Claude Opus 5',
131
+ shortLabel: 'Opus 5',
132
+ family: 'opus',
133
+ generation: '5',
134
+ contextCeiling: 1000000,
135
+ operationalSoftCeiling: 180000,
136
+ ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
137
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
138
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
139
+ dateRead: '2026-11-15',
140
+ pricingCliff: null
141
+ },
142
+ 'claude-sonnet-5': {
143
+ id: 'claude-sonnet-5',
144
+ label: 'Claude Sonnet 5',
145
+ shortLabel: 'Sonnet 5',
146
+ family: 'sonnet',
147
+ generation: '5',
148
+ contextCeiling: 1000000,
149
+ operationalSoftCeiling: 180000,
150
+ ratesPer1M: { input: 2.0, output: 10.0, write5m: 2.5, write1h: 4.0, read: 0.2 },
151
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
152
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
153
+ dateRead: '2026-11-15',
154
+ pricingCliff: null
155
+ },
156
+ 'claude-opus-4-8': {
157
+ id: 'claude-opus-4-8',
158
+ label: 'Claude Opus 4.8',
159
+ shortLabel: 'Opus 4.8',
160
+ family: 'opus',
161
+ generation: '4.8',
162
+ contextCeiling: 1000000,
163
+ operationalSoftCeiling: 180000,
164
+ ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
165
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
166
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
167
+ dateRead: '2026-11-15',
168
+ pricingCliff: null
169
+ },
170
+ 'claude-opus-4-7': {
171
+ id: 'claude-opus-4-7',
172
+ label: 'Claude Opus 4.7',
173
+ shortLabel: 'Opus 4.7',
174
+ family: 'opus',
175
+ generation: '4.7',
176
+ contextCeiling: 1000000,
177
+ operationalSoftCeiling: 180000,
178
+ ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
179
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
180
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
181
+ dateRead: '2026-11-15',
182
+ pricingCliff: null
183
+ },
101
184
  'claude-opus-4-6': {
102
185
  id: 'claude-opus-4-6',
103
186
  label: 'Claude Opus 4.6',
@@ -108,6 +191,50 @@ export const BUILTIN_CATALOG = {
108
191
  operationalSoftCeiling: 160000,
109
192
  ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
110
193
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
194
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
195
+ dateRead: '2026-11-15',
196
+ pricingCliff: null
197
+ },
198
+ 'claude-opus-4-5': {
199
+ id: 'claude-opus-4-5',
200
+ label: 'Claude Opus 4.5',
201
+ shortLabel: 'Opus 4.5',
202
+ family: 'opus',
203
+ generation: '4.5',
204
+ contextCeiling: 200000,
205
+ operationalSoftCeiling: 160000,
206
+ ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
207
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
208
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
209
+ dateRead: '2026-11-15',
210
+ pricingCliff: null
211
+ },
212
+ 'claude-opus-4-1': {
213
+ id: 'claude-opus-4-1',
214
+ label: 'Claude Opus 4.1',
215
+ shortLabel: 'Opus 4.1',
216
+ family: 'opus',
217
+ generation: '4.1',
218
+ contextCeiling: 200000,
219
+ operationalSoftCeiling: 160000,
220
+ ratesPer1M: { input: 15.0, output: 75.0, write5m: 18.75, write1h: 30.0, read: 1.5 },
221
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
222
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
223
+ dateRead: '2026-11-15',
224
+ pricingCliff: null
225
+ },
226
+ 'claude-opus-4-0': {
227
+ id: 'claude-opus-4-0',
228
+ label: 'Claude Opus 4',
229
+ shortLabel: 'Opus 4',
230
+ family: 'opus',
231
+ generation: '4.0',
232
+ contextCeiling: 200000,
233
+ operationalSoftCeiling: 160000,
234
+ ratesPer1M: { input: 15.0, output: 75.0, write5m: 18.75, write1h: 30.0, read: 1.5 },
235
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
236
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_15_75) & OpenRouter /api/v1/models (anthropic/claude-opus-4)',
237
+ dateRead: '2026-11-15',
111
238
  pricingCliff: null
112
239
  },
113
240
  'claude-sonnet-4-6': {
@@ -120,6 +247,36 @@ export const BUILTIN_CATALOG = {
120
247
  operationalSoftCeiling: 160000,
121
248
  ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
122
249
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
250
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
251
+ dateRead: '2026-11-15',
252
+ pricingCliff: null
253
+ },
254
+ 'claude-sonnet-4-5': {
255
+ id: 'claude-sonnet-4-5',
256
+ label: 'Claude Sonnet 4.5',
257
+ shortLabel: 'Sonnet 4.5',
258
+ family: 'sonnet',
259
+ generation: '4.5',
260
+ contextCeiling: 200000,
261
+ operationalSoftCeiling: 160000,
262
+ ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
263
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
264
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
265
+ dateRead: '2026-11-15',
266
+ pricingCliff: null
267
+ },
268
+ 'claude-sonnet-4-0': {
269
+ id: 'claude-sonnet-4-0',
270
+ label: 'Claude Sonnet 4',
271
+ shortLabel: 'Sonnet 4',
272
+ family: 'sonnet',
273
+ generation: '4.0',
274
+ contextCeiling: 200000,
275
+ operationalSoftCeiling: 160000,
276
+ ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
277
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
278
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_3_15) & OpenRouter /api/v1/models (anthropic/claude-sonnet-4)',
279
+ dateRead: '2026-11-15',
123
280
  pricingCliff: null
124
281
  },
125
282
  'claude-haiku-4-5': {
@@ -132,6 +289,92 @@ export const BUILTIN_CATALOG = {
132
289
  operationalSoftCeiling: 160000,
133
290
  ratesPer1M: { input: 1.0, output: 5.0, write5m: 1.25, write1h: 2.0, read: 0.1 },
134
291
  multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
292
+ source: 'phase0_capabilities_and_cost_gate.md (OpenRouter /api/v1/models)',
293
+ dateRead: '2026-11-15',
294
+ pricingCliff: null
295
+ },
296
+ 'claude-3-7-sonnet': {
297
+ id: 'claude-3-7-sonnet',
298
+ label: 'Claude Sonnet 3.7',
299
+ shortLabel: 'Sonnet 3.7',
300
+ family: 'sonnet',
301
+ generation: '3.7',
302
+ contextCeiling: 200000,
303
+ operationalSoftCeiling: 160000,
304
+ ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
305
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
306
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_3_15) & OpenRouter /api/v1/models (anthropic/claude-3.7-sonnet)',
307
+ dateRead: '2026-11-15',
308
+ pricingCliff: null
309
+ },
310
+ 'claude-3-5-sonnet': {
311
+ id: 'claude-3-5-sonnet',
312
+ label: 'Claude Sonnet 3.5',
313
+ shortLabel: 'Sonnet 3.5',
314
+ family: 'sonnet',
315
+ generation: '3.5',
316
+ contextCeiling: 200000,
317
+ operationalSoftCeiling: 160000,
318
+ ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
319
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
320
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_3_15) & OpenRouter /api/v1/models',
321
+ dateRead: '2026-11-15',
322
+ pricingCliff: null
323
+ },
324
+ 'claude-3-5-haiku': {
325
+ id: 'claude-3-5-haiku',
326
+ label: 'Claude Haiku 3.5',
327
+ shortLabel: 'Haiku 3.5',
328
+ family: 'haiku',
329
+ generation: '3.5',
330
+ contextCeiling: 200000,
331
+ operationalSoftCeiling: 160000,
332
+ ratesPer1M: { input: 0.8, output: 4.0, write5m: 1.0, write1h: 1.6, read: 0.08 },
333
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
334
+ source: 'Claude Code 2.1.325 bakedCatalog (haiku_35) & OpenRouter /api/v1/models (anthropic/claude-3.5-haiku)',
335
+ dateRead: '2026-11-15',
336
+ pricingCliff: null
337
+ },
338
+ 'claude-mythos-5': {
339
+ id: 'claude-mythos-5',
340
+ label: 'Claude Mythos 5',
341
+ shortLabel: 'Mythos 5',
342
+ family: 'mythos',
343
+ generation: '5',
344
+ contextCeiling: 1000000,
345
+ operationalSoftCeiling: 180000,
346
+ ratesPer1M: { input: 10.0, output: 50.0, write5m: 12.5, write1h: 20.0, read: 1.0 },
347
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
348
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_10_50; OpenRouter price unverified)',
349
+ dateRead: '2026-11-15',
350
+ pricingCliff: null
351
+ },
352
+ 'claude-mythos-5-1': {
353
+ id: 'claude-mythos-5-1',
354
+ label: 'Claude Mythos 5.1',
355
+ shortLabel: 'Mythos 5.1',
356
+ family: 'mythos',
357
+ generation: '5.1',
358
+ contextCeiling: 1000000,
359
+ operationalSoftCeiling: 180000,
360
+ ratesPer1M: { input: 10.0, output: 50.0, write5m: 12.5, write1h: 20.0, read: 0.25 },
361
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
362
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_10_50_cache_read_0_25; OpenRouter price unverified)',
363
+ dateRead: '2026-11-15',
364
+ pricingCliff: null
365
+ },
366
+ 'claude-mythos-5-2': {
367
+ id: 'claude-mythos-5-2',
368
+ label: 'Claude Mythos 5.2',
369
+ shortLabel: 'Mythos 5.2',
370
+ family: 'mythos',
371
+ generation: '5.2',
372
+ contextCeiling: 1000000,
373
+ operationalSoftCeiling: 180000,
374
+ ratesPer1M: { input: 8.0, output: 40.0, write5m: 10.0, write1h: 16.0, read: 0.2 },
375
+ multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
376
+ source: 'Claude Code 2.1.325 bakedCatalog (tier_8_40_cache_read_0_20; OpenRouter price unverified)',
377
+ dateRead: '2026-11-15',
135
378
  pricingCliff: null
136
379
  }
137
380
  }
@@ -166,54 +409,165 @@ export function hasExtended1MHint(rawModelId = '', options = {}) {
166
409
  return /\[1m\]|(?:^|[-_:\s])1m(?:$|[-_:\s\]])/.test(raw);
167
410
  }
168
411
 
412
+ export const UNKNOWN_MODEL_ID = 'unknown';
413
+
169
414
  /**
170
- * Normalizes any raw Anthropic model string or shorthand alias to its canonical catalog ID.
415
+ * Formats a concise, zero-USD advisory for models absent from the price catalog (Item 1.2).
416
+ *
417
+ * @param {string} [rawModelId='']
418
+ * @returns {string}
419
+ */
420
+ export function formatUnknownModelAdvisory(rawModelId = '') {
421
+ const displayId = String(rawModelId || '').trim() || UNKNOWN_MODEL_ID;
422
+ return `Prices for model "${displayId}" are unknown (not in price catalog); dollar figures and price-based holds are disabled.`;
423
+ }
424
+
425
+ const SHORT_MODEL_ALIASES = Object.freeze({
426
+ opus: 'claude-opus-5-5',
427
+ best: 'claude-opus-5-5',
428
+ opusplan: 'claude-opus-5-5',
429
+ sonnet: 'claude-sonnet-5-5',
430
+ haiku: 'claude-haiku-5-5',
431
+ fable: 'claude-fable-5-2',
432
+ mythos: 'claude-mythos-5-2',
433
+ 'claude-opus-4': 'claude-opus-4-0',
434
+ 'claude-sonnet-4': 'claude-sonnet-4-0',
435
+ 'claude-3-5-sonnet-v2': 'claude-3-5-sonnet'
436
+ });
437
+
438
+ /**
439
+ * Canonicalizes a raw model identifier syntax (provider prefixes, `[1m]`, dates, version dots)
440
+ * without guessing unknown model families or falling back to a default model.
441
+ *
171
442
  * @param {string} [rawModelId='']
172
443
  * @returns {string}
173
444
  */
174
- export function normalizeModelId(rawModelId = '') {
175
- const cleaned = String(rawModelId || '')
445
+ function canonicalizeModelToken(rawModelId = '') {
446
+ let s = String(rawModelId || '')
176
447
  .toLowerCase()
177
448
  .trim()
178
- .replace(/\[1m\]/g, '')
449
+ .replace(/\[[12]m\]/gi, '')
179
450
  .replace(/[-_:\s]+1m$/g, '')
180
- .replace(/\s+/g, '-')
181
- .replace(/\./g, '-');
451
+ .replace(/\s+/g, '-');
452
+
453
+ if (!s) return '';
454
+
455
+ // Strip provider prefixes (e.g. openrouter/anthropic/, anthropic/, us.anthropic.)
456
+ const slashIdx = s.lastIndexOf('/');
457
+ if (slashIdx !== -1) {
458
+ const providerPart = s.slice(0, slashIdx);
459
+ const tail = s.slice(slashIdx + 1);
460
+ if (providerPart === 'anthropic' || providerPart.endsWith('/anthropic')) {
461
+ s = tail;
462
+ } else {
463
+ // Non-Anthropic provider prefix (e.g. google/gemini-2.5-pro, openai/gpt-5) stays intact
464
+ return s.replace(/\./g, '-');
465
+ }
466
+ }
182
467
 
183
- if (!cleaned) return BUILTIN_CATALOG.defaultModel;
184
- if (BUILTIN_CATALOG.models[cleaned]) return cleaned;
468
+ const bedrockMatch = s.match(/^(?:[a-z-]+\.)?anthropic\.(claude-.*)$/);
469
+ if (bedrockMatch) {
470
+ s = bedrockMatch[1];
471
+ }
185
472
 
186
- // Fable family
187
- if (cleaned.includes('fable')) {
188
- if (cleaned.includes('5-1')) return 'claude-fable-5-1';
189
- return 'claude-fable-5-2';
473
+ s = s.replace(/\./g, '-');
474
+ // Strip Bedrock version tag (-v1:0 / -v2), date trailer (-20251001 / @20251001), or tier tag (:batch is NOT stripped as base rate)
475
+ s = s
476
+ .replace(/-v\d+(?::\d+)?$/i, '')
477
+ .replace(/[@-]\d{8}$/i, '')
478
+ .replace(/-(?:thinking|fast|latest)$/i, '');
479
+
480
+ // Handle alternate ordering like claude-4-6-sonnet -> claude-sonnet-4-6 (4.x+ only; 3.x is claude-3-x-family)
481
+ const altOrder = s.match(/^claude-([4-9]\d*-\d+)-(opus|sonnet|haiku|fable|mythos)$/);
482
+ if (altOrder) {
483
+ s = `claude-${altOrder[2]}-${altOrder[1]}`;
484
+ }
485
+ const threeXOrder = s.match(/^claude-(haiku|sonnet)-(3-[57])$/);
486
+ if (threeXOrder) {
487
+ s = `claude-${threeXOrder[2]}-${threeXOrder[1]}`;
190
488
  }
191
489
 
192
- // Opus family
193
- if (cleaned.includes('opus')) {
194
- if (cleaned.includes('4-6') || cleaned.includes('4-5') || cleaned.includes('4-0') || cleaned.includes('3-')) {
195
- return 'claude-opus-4-6';
196
- }
197
- return 'claude-opus-5-5';
490
+ if (Object.prototype.hasOwnProperty.call(SHORT_MODEL_ALIASES, s)) {
491
+ return SHORT_MODEL_ALIASES[s];
492
+ }
493
+ if (/^(?:opus|sonnet|haiku|fable)-/.test(s) && !s.startsWith('claude-')) {
494
+ return `claude-${s}`;
198
495
  }
199
496
 
200
- // Sonnet family
201
- if (cleaned.includes('sonnet')) {
202
- if (cleaned.includes('4-6') || cleaned.includes('4-5') || cleaned.includes('3-7') || cleaned.includes('3-5')) {
203
- return 'claude-sonnet-4-6';
204
- }
205
- return 'claude-sonnet-5-5';
497
+ return s;
498
+ }
499
+
500
+ /**
501
+ * Checks whether a raw model identifier resolves to a known Anthropic catalog model
502
+ * (or an explicit custom rate card in `customCeilings.json`), returning `false` for
503
+ * unrecognized model IDs (including uncatalogued Claude or third-party slugs).
504
+ *
505
+ * @param {string} [rawModelId='']
506
+ * @param {object} [options={}]
507
+ * @returns {boolean}
508
+ */
509
+ export function isKnownCatalogModel(rawModelId = '', options = {}) {
510
+ const raw = String(rawModelId || '').trim();
511
+ if (!raw || raw.toLowerCase() === UNKNOWN_MODEL_ID) return false;
512
+
513
+ const cleaned = canonicalizeModelToken(raw);
514
+ if (!cleaned) return false;
515
+
516
+ const catalog = options.catalog || loadModelCatalog(options);
517
+ if (catalog?.models && Object.prototype.hasOwnProperty.call(catalog.models, cleaned)) {
518
+ return true;
206
519
  }
207
520
 
208
- // Haiku family
209
- if (cleaned.includes('haiku')) {
210
- if (cleaned.includes('4-5') || cleaned.includes('3-5') || cleaned.includes('3-0')) {
211
- return 'claude-haiku-4-5';
521
+ const customCeilings = options.customCeilings !== undefined ? options.customCeilings : loadCustomCeilings(options);
522
+ if (customCeilings && typeof customCeilings === 'object') {
523
+ const sourceMap =
524
+ customCeilings.models && typeof customCeilings.models === 'object'
525
+ ? customCeilings.models
526
+ : customCeilings.ceilings && typeof customCeilings.ceilings === 'object'
527
+ ? customCeilings.ceilings
528
+ : customCeilings;
529
+ for (const [key, val] of Object.entries(sourceMap)) {
530
+ if (
531
+ (key === raw || canonicalizeModelToken(key) === cleaned) &&
532
+ val &&
533
+ typeof val === 'object' &&
534
+ val.ratesPer1M &&
535
+ typeof val.ratesPer1M === 'object'
536
+ ) {
537
+ return true;
538
+ }
212
539
  }
213
- return 'claude-haiku-5-5';
214
540
  }
215
541
 
216
- return BUILTIN_CATALOG.defaultModel;
542
+ return false;
543
+ }
544
+
545
+ /**
546
+ * Normalizes a raw Anthropic model string or official Claude Code alias to its canonical catalog ID.
547
+ * Returns `BUILTIN_CATALOG.defaultModel` (`'claude-sonnet-5-5'`) only when `rawModelId` is empty/omitted;
548
+ * returns `'unknown'` (`UNKNOWN_MODEL_ID`) for any unrecognized non-empty model identifier.
549
+ *
550
+ * @param {string} [rawModelId='']
551
+ * @param {object} [options={}]
552
+ * @returns {string}
553
+ */
554
+ export function normalizeModelId(rawModelId = '', options = {}) {
555
+ const raw = String(rawModelId || '').trim();
556
+ if (!raw) return BUILTIN_CATALOG.defaultModel;
557
+
558
+ const cleaned = canonicalizeModelToken(raw);
559
+ if (!cleaned) return BUILTIN_CATALOG.defaultModel;
560
+
561
+ const catalog = options.catalog || BUILTIN_CATALOG;
562
+ if (catalog?.models && Object.prototype.hasOwnProperty.call(catalog.models, cleaned)) {
563
+ return cleaned;
564
+ }
565
+
566
+ if (options.customCeilings && isKnownCatalogModel(raw, options)) {
567
+ return cleaned;
568
+ }
569
+
570
+ return UNKNOWN_MODEL_ID;
217
571
  }
218
572
 
219
573
  /**
@@ -295,7 +649,8 @@ export function resolveCustomCeilingForModel(modelId = '', options = {}) {
295
649
  return null;
296
650
  };
297
651
 
298
- const normId = normalizeModelId(modelId);
652
+ const normId = normalizeModelId(modelId, options);
653
+ const cleanedRaw = canonicalizeModelToken(modelId);
299
654
  if (modelId && Object.prototype.hasOwnProperty.call(sourceMap, modelId)) {
300
655
  const direct = extractCeilingNumber(sourceMap[modelId]);
301
656
  if (direct !== null) return direct;
@@ -303,7 +658,10 @@ export function resolveCustomCeilingForModel(modelId = '', options = {}) {
303
658
 
304
659
  for (const [rawKey, overrideVal] of Object.entries(sourceMap)) {
305
660
  if (rawKey === '*' || rawKey === 'schemaVersion') continue;
306
- if (rawKey === normId || normalizeModelId(rawKey) === normId) {
661
+ const keyMatched =
662
+ (cleanedRaw && (rawKey === cleanedRaw || canonicalizeModelToken(rawKey) === cleanedRaw)) ||
663
+ (normId !== UNKNOWN_MODEL_ID && (rawKey === normId || normalizeModelId(rawKey, options) === normId));
664
+ if (keyMatched) {
307
665
  const matched = extractCeilingNumber(overrideVal);
308
666
  if (matched !== null) return matched;
309
667
  }
@@ -352,8 +710,32 @@ function applyCeilingOverrides(catalog, customCeilings) {
352
710
 
353
711
  for (const [rawKey, overrideVal] of Object.entries(sourceMap)) {
354
712
  if (rawKey === '*' || rawKey === 'schemaVersion') continue;
355
- const normId = catalog.models[rawKey] ? rawKey : normalizeModelId(rawKey);
356
- const target = catalog.models[normId];
713
+ const cleanedKey = canonicalizeModelToken(rawKey);
714
+ const normId = catalog.models[rawKey]
715
+ ? rawKey
716
+ : catalog.models[cleanedKey]
717
+ ? cleanedKey
718
+ : normalizeModelId(rawKey, { catalog });
719
+ let target = normId !== UNKNOWN_MODEL_ID ? catalog.models[normId] : null;
720
+ if (!target && overrideVal && typeof overrideVal === 'object' && overrideVal.ratesPer1M) {
721
+ const customId = cleanedKey || rawKey;
722
+ target = {
723
+ id: customId,
724
+ label: overrideVal.label || customId,
725
+ shortLabel: overrideVal.shortLabel || overrideVal.label || customId,
726
+ family: overrideVal.family || 'custom',
727
+ generation: overrideVal.generation || null,
728
+ contextCeiling: Number(overrideVal.contextCeiling || overrideVal.ceiling || 200000),
729
+ operationalSoftCeiling: Number(overrideVal.operationalSoftCeiling || 160000),
730
+ ratesPer1M: { ...overrideVal.ratesPer1M },
731
+ multipliers: overrideVal.multipliers || { write5m: 1.25, write1h: 2.0, read: 0.1 },
732
+ source: overrideVal.source || 'customCeilings.json',
733
+ dateRead: overrideVal.dateRead || null,
734
+ pricingCliff: overrideVal.pricingCliff || null
735
+ };
736
+ catalog.models[customId] = target;
737
+ continue;
738
+ }
357
739
  if (!target) continue;
358
740
 
359
741
  if (typeof overrideVal === 'number' && overrideVal > 0) {
@@ -399,8 +781,11 @@ export function loadModelCatalog(options = {}) {
399
781
  }
400
782
 
401
783
  /**
402
- * Retrieves the model specification for a given model ID (with safe fallback to `claude-sonnet-5-5`).
403
- * Honors `[1m]` / `-1m` / `options.extendedContext1M` 1M context windows on Claude 4.6 models (`ISS-002`).
784
+ * Retrieves the model specification for a given model ID.
785
+ * Empty/omitted `modelId` uses `catalog.defaultModel` (`claude-sonnet-5-5`).
786
+ * Any non-empty unrecognized model ID returns an explicit `{ id: 'unknown', unknownModel: true, ratesPer1M: null }`
787
+ * state rather than silently falling back to `claude-sonnet-5-5` (Item 1.2).
788
+ * Honors `[1m]` / `-1m` / `options.extendedContext1M` 1M context windows on Claude 4.x models (`ISS-002`).
404
789
  *
405
790
  * @param {string} [modelId='']
406
791
  * @param {object} [options={}]
@@ -408,24 +793,71 @@ export function loadModelCatalog(options = {}) {
408
793
  */
409
794
  export function getModelSpec(modelId = '', options = {}) {
410
795
  const catalog = options.catalog || loadModelCatalog(options);
411
- const normId = normalizeModelId(modelId);
412
- const spec =
413
- catalog.models[normId] ||
414
- catalog.models[catalog.defaultModel] ||
415
- BUILTIN_CATALOG.models['claude-sonnet-5-5'];
796
+ const rawTrimmed = String(modelId || '').trim();
797
+ if (!rawTrimmed) {
798
+ const fallbackSpec =
799
+ catalog.models[catalog.defaultModel] || BUILTIN_CATALOG.models['claude-sonnet-5-5'];
800
+ if (
801
+ hasExtended1MHint(modelId, options) &&
802
+ resolveCustomCeilingForModel(fallbackSpec.id, options) === null &&
803
+ fallbackSpec.contextCeiling < 1000000
804
+ ) {
805
+ return {
806
+ ...fallbackSpec,
807
+ contextCeiling: 1000000,
808
+ operationalSoftCeiling: 800000,
809
+ unknownModel: false
810
+ };
811
+ }
812
+ return { ...fallbackSpec, unknownModel: false };
813
+ }
814
+
815
+ const cleaned = canonicalizeModelToken(rawTrimmed);
816
+ const normId = normalizeModelId(rawTrimmed, { ...options, catalog });
817
+ const directCustomSpec =
818
+ catalog.models[cleaned] ||
819
+ (normId !== UNKNOWN_MODEL_ID ? catalog.models[normId] : null);
820
+
821
+ if (!directCustomSpec || normId === UNKNOWN_MODEL_ID) {
822
+ const customCeiling = resolveCustomCeilingForModel(rawTrimmed, options);
823
+ const defaultCeiling = hasExtended1MHint(rawTrimmed, options) ? 1000000 : 200000;
824
+ const contextCeiling = Number(customCeiling || defaultCeiling);
825
+ return {
826
+ id: UNKNOWN_MODEL_ID,
827
+ rawModelId: rawTrimmed,
828
+ unknownModel: true,
829
+ label: `Unknown Model (${rawTrimmed})`,
830
+ shortLabel: 'Unknown Model',
831
+ family: 'unknown',
832
+ generation: null,
833
+ contextCeiling,
834
+ operationalSoftCeiling: Math.round(contextCeiling * 0.8),
835
+ ratesPer1M: null,
836
+ multipliers: null,
837
+ source: null,
838
+ dateRead: null,
839
+ pricingCliff: null,
840
+ advisory: formatUnknownModelAdvisory(rawTrimmed)
841
+ };
842
+ }
416
843
 
417
844
  if (
418
- (normId === 'claude-opus-4-6' || normId === 'claude-sonnet-4-6') &&
845
+ (normId === 'claude-opus-4-6' ||
846
+ normId === 'claude-sonnet-4-6' ||
847
+ normId === 'claude-opus-4-5' ||
848
+ normId === 'claude-sonnet-4-5') &&
419
849
  hasExtended1MHint(modelId, options) &&
420
850
  resolveCustomCeilingForModel(modelId, options) === null
421
851
  ) {
422
852
  return {
423
- ...spec,
424
- contextCeiling: 1000000
853
+ ...directCustomSpec,
854
+ contextCeiling: 1000000,
855
+ operationalSoftCeiling: 800000,
856
+ unknownModel: false
425
857
  };
426
858
  }
427
859
 
428
- return spec;
860
+ return { ...directCustomSpec, unknownModel: false };
429
861
  }
430
862
 
431
863
  /**
@@ -648,6 +1080,32 @@ export function resolveEffectiveRates(modelId = 'claude-sonnet-5-5', totalPrompt
648
1080
  const spec = getModelSpec(modelId, options);
649
1081
  const promptTokens = Math.max(0, Number(totalPromptTokens || 0));
650
1082
 
1083
+ if (spec.unknownModel) {
1084
+ return {
1085
+ modelId: UNKNOWN_MODEL_ID,
1086
+ rawModelId: spec.rawModelId || String(modelId || '').trim(),
1087
+ unknownModel: true,
1088
+ label: spec.label,
1089
+ shortLabel: spec.shortLabel || 'Unknown Model',
1090
+ family: 'unknown',
1091
+ contextCeiling: spec.contextCeiling,
1092
+ operationalSoftCeiling: spec.operationalSoftCeiling,
1093
+ inputPer1M: null,
1094
+ outputPer1M: null,
1095
+ write5mPer1M: null,
1096
+ write1hPer1M: null,
1097
+ readPer1M: null,
1098
+ multipliers: null,
1099
+ source: null,
1100
+ dateRead: null,
1101
+ cliffTriggered: false,
1102
+ preCliffWarningTriggered: false,
1103
+ cliffThresholdTokens: null,
1104
+ cliffMultiplier: 1.0,
1105
+ advisory: spec.advisory || formatUnknownModelAdvisory(modelId)
1106
+ };
1107
+ }
1108
+
651
1109
  const cliff = spec.pricingCliff || null;
652
1110
  const cliffTriggered = Boolean(cliff && promptTokens > cliff.thresholdTokens);
653
1111
  const preCliffWarningTriggered = Boolean(
@@ -660,6 +1118,7 @@ export function resolveEffectiveRates(modelId = 'claude-sonnet-5-5', totalPrompt
660
1118
 
661
1119
  return {
662
1120
  modelId: spec.id,
1121
+ unknownModel: false,
663
1122
  label: spec.label,
664
1123
  shortLabel: spec.shortLabel || spec.label,
665
1124
  family: spec.family,
@@ -671,6 +1130,8 @@ export function resolveEffectiveRates(modelId = 'claude-sonnet-5-5', totalPrompt
671
1130
  write1hPer1M: activeRates.write1h,
672
1131
  readPer1M: activeRates.read,
673
1132
  multipliers: { ...spec.multipliers },
1133
+ source: spec.source || null,
1134
+ dateRead: spec.dateRead || null,
674
1135
  cliffTriggered,
675
1136
  preCliffWarningTriggered,
676
1137
  cliffThresholdTokens: cliff ? cliff.thresholdTokens : null,
@@ -709,6 +1170,41 @@ export function calculateTurnCostUSD(usage = {}, options = {}) {
709
1170
  const rawModel = usage.model || usage.modelId || 'claude-sonnet-5-5';
710
1171
  const rates = resolveEffectiveRates(rawModel, totalPromptTokens, options);
711
1172
 
1173
+ if (rates.unknownModel) {
1174
+ return {
1175
+ modelId: UNKNOWN_MODEL_ID,
1176
+ rawModelId: rates.rawModelId || String(rawModel),
1177
+ unknownModel: true,
1178
+ advisory: rates.advisory,
1179
+ rates,
1180
+ tokens: {
1181
+ uncachedInput,
1182
+ cacheWrite5m: write5mTokens,
1183
+ cacheWrite1h: write1hTokens,
1184
+ totalCacheWrite: totalCacheWriteTokens,
1185
+ cacheRead: cacheReadTokens,
1186
+ totalPrompt: totalPromptTokens,
1187
+ output: outputTokens,
1188
+ thinking: thinkingTokens,
1189
+ totalOutput: outputTokens + thinkingTokens
1190
+ },
1191
+ costsUSD: {
1192
+ uncachedInput: null,
1193
+ write5m: null,
1194
+ write1h: null,
1195
+ cacheRead: null,
1196
+ output: null,
1197
+ actualCacheSpendUSD: null,
1198
+ uncachedCacheEquivalentUSD: null,
1199
+ totalActualSpendUSD: null,
1200
+ totalUncachedEquivalentUSD: null,
1201
+ netCacheSavingsUSD: null
1202
+ },
1203
+ cliffTriggered: false,
1204
+ preCliffWarningTriggered: false
1205
+ };
1206
+ }
1207
+
712
1208
  const inputCostUSD = (uncachedInput / 1_000_000) * rates.inputPer1M;
713
1209
  const write5mCostUSD = (write5mTokens / 1_000_000) * rates.write5mPer1M;
714
1210
  const write1hCostUSD = (write1hTokens / 1_000_000) * rates.write1hPer1M;
@@ -724,6 +1220,7 @@ export function calculateTurnCostUSD(usage = {}, options = {}) {
724
1220
 
725
1221
  return {
726
1222
  modelId: rates.modelId,
1223
+ unknownModel: false,
727
1224
  rates,
728
1225
  tokens: {
729
1226
  uncachedInput,
@@ -755,7 +1252,10 @@ export function calculateTurnCostUSD(usage = {}, options = {}) {
755
1252
 
756
1253
  export default {
757
1254
  BUILTIN_CATALOG,
1255
+ UNKNOWN_MODEL_ID,
1256
+ formatUnknownModelAdvisory,
758
1257
  hasExtended1MHint,
1258
+ isKnownCatalogModel,
759
1259
  normalizeModelId,
760
1260
  loadCustomCeilings,
761
1261
  resolveCustomCeilingForModel,