@askalf/dario 6.12.6 → 6.12.8

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.
@@ -249,6 +249,28 @@ export declare function providerOfModel(model: string): PricingProvider;
249
249
  * repeated `[` backtrack quadratically (200k of them held the event loop ~25s).
250
250
  */
251
251
  export declare function pricingRateFor(model: string, atMs: number): Rate;
252
+ /** True when `model` has a row of its own in PRICING or OPENAI_PRICING, rather than a fallback's. */
253
+ export declare function hasPublishedRate(model: string): boolean;
254
+ /**
255
+ * The highest rate either price list carries, bucket by bucket (intro rates
256
+ * included). An upper bound on what any listed model bills, for pricing a
257
+ * model that has no row of its own where a low estimate would let spend
258
+ * through.
259
+ */
260
+ export declare function highestPublishedRate(): Rate;
261
+ /**
262
+ * costOfTokens for an upper bound (the key-budget reservation): a model with
263
+ * no published rate is priced at highestPublishedRate, never at the
264
+ * sonnet-4-6 / gpt-5.6-terra fallback, which is cheaper than fable, astra
265
+ * and every opus. Matched case-insensitively, as the Claude path lowercases
266
+ * the model it sends.
267
+ */
268
+ export declare function costOfTokensFailClosed(model: string, atMs: number, t: {
269
+ inputTokens: number;
270
+ outputTokens: number;
271
+ cacheReadTokens: number;
272
+ cacheCreateTokens: number;
273
+ }): number;
252
274
  /**
253
275
  * USD the four token buckets would bill at `model`'s rate in effect at `atMs`.
254
276
  * The ledger prices its per-day rows through this too, so a pricing
package/dist/analytics.js CHANGED
@@ -287,7 +287,7 @@ export function providerOfModel(model) {
287
287
  * repeated `[` backtrack quadratically (200k of them held the event loop ~25s).
288
288
  */
289
289
  export function pricingRateFor(model, atMs) {
290
- const baseModel = model.replace(/\[[^[\]]*\]$/, '').replace(/:[a-z]+$/i, '').replace(/-\d{8}$/, '');
290
+ const baseModel = pricingBaseModel(model);
291
291
  if (providerOfModel(baseModel) === 'openai') {
292
292
  const rate = OPENAI_PRICING[baseModel] ?? OPENAI_PRICING[OPENAI_FALLBACK_MODEL];
293
293
  return { ...rate };
@@ -299,6 +299,49 @@ export function pricingRateFor(model, atMs) {
299
299
  }
300
300
  return { input: entry.input, output: entry.output, cacheRead: entry.cacheRead, cacheCreate: entry.cacheCreate };
301
301
  }
302
+ /** The id a price list is keyed by: context tag, effort suffix and date stamp removed. */
303
+ function pricingBaseModel(model) {
304
+ return model.replace(/\[[^[\]]*\]$/, '').replace(/:[a-z]+$/i, '').replace(/-\d{8}$/, '');
305
+ }
306
+ /** True when `model` has a row of its own in PRICING or OPENAI_PRICING, rather than a fallback's. */
307
+ export function hasPublishedRate(model) {
308
+ const base = pricingBaseModel(model);
309
+ return Object.hasOwn(PRICING, base) || Object.hasOwn(OPENAI_PRICING, base);
310
+ }
311
+ /**
312
+ * The highest rate either price list carries, bucket by bucket (intro rates
313
+ * included). An upper bound on what any listed model bills, for pricing a
314
+ * model that has no row of its own where a low estimate would let spend
315
+ * through.
316
+ */
317
+ export function highestPublishedRate() {
318
+ const rates = [...Object.values(OPENAI_PRICING)];
319
+ for (const e of Object.values(PRICING)) {
320
+ rates.push(e);
321
+ if (e.intro)
322
+ rates.push(e.intro);
323
+ }
324
+ return {
325
+ input: Math.max(...rates.map((r) => r.input)),
326
+ output: Math.max(...rates.map((r) => r.output)),
327
+ cacheRead: Math.max(...rates.map((r) => r.cacheRead)),
328
+ cacheCreate: Math.max(...rates.map((r) => r.cacheCreate)),
329
+ };
330
+ }
331
+ /**
332
+ * costOfTokens for an upper bound (the key-budget reservation): a model with
333
+ * no published rate is priced at highestPublishedRate, never at the
334
+ * sonnet-4-6 / gpt-5.6-terra fallback, which is cheaper than fable, astra
335
+ * and every opus. Matched case-insensitively, as the Claude path lowercases
336
+ * the model it sends.
337
+ */
338
+ export function costOfTokensFailClosed(model, atMs, t) {
339
+ const id = model.toLowerCase();
340
+ if (hasPublishedRate(id))
341
+ return costOfTokens(id, atMs, t);
342
+ const p = highestPublishedRate();
343
+ return ((t.inputTokens * p.input) + (t.outputTokens * p.output) + (t.cacheReadTokens * p.cacheRead) + (t.cacheCreateTokens * p.cacheCreate)) / 1_000_000;
344
+ }
302
345
  /**
303
346
  * USD the four token buckets would bill at `model`'s rate in effect at `atMs`.
304
347
  * The ledger prices its per-day rows through this too, so a pricing
@@ -79,6 +79,30 @@ export declare const CC_NATIVE_NAMES: Set<string>;
79
79
  * a CC session with an MCP server attached. Like CC_NATIVE_NAMES, these
80
80
  * identity-map and skip TOOL_MAP entirely. */
81
81
  export declare function isMcpToolName(name: unknown): boolean;
82
+ /** Server segment of the MCP-shaped name a client tool outside Claude Code's
83
+ * set is carried under on a Claude Code-shaped request: `submit_review` goes
84
+ * out as `mcp__client__submit_review`. Every tool real Claude Code sends
85
+ * beyond its built-ins is named `mcp__<server>__<tool>`, so a bare foreign
86
+ * name next to Claude Code's tools is a shape it never produces. */
87
+ export declare const CLIENT_TOOL_MCP_PREFIX = "mcp__client__";
88
+ /**
89
+ * A client tool that cannot go out on a Claude Code-shaped request: not a
90
+ * custom tool with an input_schema (an Anthropic-defined tool such as
91
+ * `computer_20251124` keeps its fixed name and cannot be renamed), or a name
92
+ * that does not fit Anthropic's tool-name limit once MCP-shaped. The proxy
93
+ * answers 400 naming each tool; dropping one would leave the model unable to
94
+ * call a tool the client declared.
95
+ */
96
+ export declare class UnsupportedClientToolError extends Error {
97
+ readonly tools: ReadonlyArray<{
98
+ name: string;
99
+ reason: string;
100
+ }>;
101
+ constructor(tools: Array<{
102
+ name: string;
103
+ reason: string;
104
+ }>);
105
+ }
82
106
  /** CC's static system prompt (~25KB). The shared base — baked from a non-Fable
83
107
  * model (Opus). CC ships some models a larger, model-specific prompt; see
84
108
  * CC_SYSTEM_PROMPT_FABLE / systemPromptForModel. */
@@ -302,10 +326,15 @@ export declare function detectTextToolClient(systemText: string): string | null;
302
326
  * floor ([memory_store, db_query]). Before this, those 2-tool surfaces fell
303
327
  * under a len<3 guard and got round-robined onto CC fallback slots, which
304
328
  * silently corrupts every call (the model upstream never sees the real tool).
305
- * - Mixed surface (some tools map): require 3+ tools AND ≥80% unmapped. The 80%
306
- * leaves room for a non-CC client that legitimately reuses 1-2 of TOOL_MAP's
307
- * bash/grep/read aliases; the 3-tool floor avoids mis-flagging a partial CC
308
- * load (1-2 tools, some mapped) mid-handshake — those stay null and remap.
329
+ * - A foreign tool, no CC-native name, and an alias that shares a CC tool's
330
+ * name (`grep`, `bash`): non-CC at any ratio. Claude Code declares its own
331
+ * tools by their exact names, so such a surface is some other client's, and
332
+ * its own schemas are what it parses; remap would put its aliases through
333
+ * TOOL_MAP's argument translation. A surface that names no CC tool gets the
334
+ * full template instead and stays in remap, fallback slots visible.
335
+ * - Mixed surface otherwise: require 3+ tools AND ≥80% unmapped. The 80% leaves
336
+ * room for a non-CC client that legitimately reuses 1-2 of TOOL_MAP's
337
+ * aliases; the 3-tool floor keeps a 1-2 tool partial CC load in remap.
309
338
  */
310
339
  export declare function detectNonCCByTools(clientTools: Array<Record<string, unknown>> | undefined): string | null;
311
340
  /**
@@ -564,6 +593,7 @@ export declare function buildCCRequest(clientBody: Record<string, unknown>, bill
564
593
  body: Record<string, unknown>;
565
594
  toolMap: Map<string, ToolMapping>;
566
595
  unmappedTools: string[];
596
+ carriedAsMcp?: string[];
567
597
  detectedClient?: string;
568
598
  genuineCC?: boolean;
569
599
  };
@@ -76,6 +76,9 @@ export const CC_TEMPLATE_PROMPT_BYTES = (() => {
76
76
  })();
77
77
  /** Every name the bundle knows — including one whose definition is not advertisable (dario#1376). */
78
78
  export const CC_NATIVE_NAMES_UNION = new Set(TEMPLATE.tools.map((t) => String(t.name)));
79
+ /** Lowercased names of the advertisable non-MCP CC tools: the set buildCCRequest
80
+ * matches a client declaration against, case-insensitively, when it advertises. */
81
+ const CC_TOOL_NAMES_LOWER = new Set(CC_TOOL_DEFINITIONS_UNION.filter((t) => !isMcpToolName(t.name)).map((t) => t.name.toLowerCase()));
79
82
  /** CC's own tool names, EXACT case ("Read", "Bash", "Agent", …). A CC client's
80
83
  * tools identity-map to themselves and OVERRIDE TOOL_MAP — whose lowercase
81
84
  * cross-client aliases ('read' → {path}/{filePath}) would otherwise mistranslate
@@ -99,6 +102,33 @@ export const CC_NATIVE_NAMES = new Set(CC_TOOL_DEFINITIONS.map((t) => String(t.n
99
102
  export function isMcpToolName(name) {
100
103
  return typeof name === 'string' && name.startsWith('mcp__');
101
104
  }
105
+ /** Server segment of the MCP-shaped name a client tool outside Claude Code's
106
+ * set is carried under on a Claude Code-shaped request: `submit_review` goes
107
+ * out as `mcp__client__submit_review`. Every tool real Claude Code sends
108
+ * beyond its built-ins is named `mcp__<server>__<tool>`, so a bare foreign
109
+ * name next to Claude Code's tools is a shape it never produces. */
110
+ export const CLIENT_TOOL_MCP_PREFIX = 'mcp__client__';
111
+ /** Anthropic's limit on a tool name. The MCP-shaped name has to fit it. */
112
+ const TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
113
+ /**
114
+ * A client tool that cannot go out on a Claude Code-shaped request: not a
115
+ * custom tool with an input_schema (an Anthropic-defined tool such as
116
+ * `computer_20251124` keeps its fixed name and cannot be renamed), or a name
117
+ * that does not fit Anthropic's tool-name limit once MCP-shaped. The proxy
118
+ * answers 400 naming each tool; dropping one would leave the model unable to
119
+ * call a tool the client declared.
120
+ */
121
+ export class UnsupportedClientToolError extends Error {
122
+ tools;
123
+ constructor(tools) {
124
+ super(`dario cannot carry ${tools.length === 1 ? 'this client tool' : 'these client tools'} on a Claude Code-shaped request: `
125
+ + tools.map((t) => `"${t.name}" (${t.reason})`).join('; ')
126
+ + '. Declare each as a custom tool with an input_schema and a name of at most '
127
+ + `${64 - CLIENT_TOOL_MCP_PREFIX.length} characters, or run dario with --preserve-tools to forward your tools verbatim.`);
128
+ this.name = 'UnsupportedClientToolError';
129
+ this.tools = tools;
130
+ }
131
+ }
102
132
  /** CC's static system prompt (~25KB). The shared base — baked from a non-Fable
103
133
  * model (Opus). CC ships some models a larger, model-specific prompt; see
104
134
  * CC_SYSTEM_PROMPT_FABLE / systemPromptForModel. */
@@ -666,15 +696,22 @@ export function detectTextToolClient(systemText) {
666
696
  * floor ([memory_store, db_query]). Before this, those 2-tool surfaces fell
667
697
  * under a len<3 guard and got round-robined onto CC fallback slots, which
668
698
  * silently corrupts every call (the model upstream never sees the real tool).
669
- * - Mixed surface (some tools map): require 3+ tools AND ≥80% unmapped. The 80%
670
- * leaves room for a non-CC client that legitimately reuses 1-2 of TOOL_MAP's
671
- * bash/grep/read aliases; the 3-tool floor avoids mis-flagging a partial CC
672
- * load (1-2 tools, some mapped) mid-handshake — those stay null and remap.
699
+ * - A foreign tool, no CC-native name, and an alias that shares a CC tool's
700
+ * name (`grep`, `bash`): non-CC at any ratio. Claude Code declares its own
701
+ * tools by their exact names, so such a surface is some other client's, and
702
+ * its own schemas are what it parses; remap would put its aliases through
703
+ * TOOL_MAP's argument translation. A surface that names no CC tool gets the
704
+ * full template instead and stays in remap, fallback slots visible.
705
+ * - Mixed surface otherwise: require 3+ tools AND ≥80% unmapped. The 80% leaves
706
+ * room for a non-CC client that legitimately reuses 1-2 of TOOL_MAP's
707
+ * aliases; the 3-tool floor keeps a 1-2 tool partial CC load in remap.
673
708
  */
674
709
  export function detectNonCCByTools(clientTools) {
675
710
  if (!clientTools || clientTools.length === 0)
676
711
  return null;
677
712
  let unmapped = 0;
713
+ let native = false;
714
+ let sharesCCName = false;
678
715
  for (const tool of clientTools) {
679
716
  const rawName = tool.name || '';
680
717
  // A tool is "foreign" only if dario can neither map it (TOOL_MAP, by
@@ -693,7 +730,13 @@ export function detectNonCCByTools(clientTools) {
693
730
  // verbatim rather than falling back to the full CC template.
694
731
  // Union set, not the host-filtered one: a win32 client's PowerShell is
695
732
  // native regardless of the platform dario itself runs on (v4.8.136).
696
- if (!TOOL_MAP[rawName.toLowerCase()] && !CC_NATIVE_NAMES_UNION.has(rawName) && !isMcpToolName(rawName))
733
+ if (CC_NATIVE_NAMES_UNION.has(rawName)) {
734
+ native = true;
735
+ continue;
736
+ }
737
+ if (CC_TOOL_NAMES_LOWER.has(rawName.toLowerCase()))
738
+ sharesCCName = true;
739
+ if (!TOOL_MAP[rawName.toLowerCase()] && !isMcpToolName(rawName))
697
740
  unmapped++;
698
741
  }
699
742
  const ratio = unmapped / clientTools.length;
@@ -702,6 +745,11 @@ export function detectNonCCByTools(clientTools) {
702
745
  // genuinely foreign client hits it.
703
746
  if (ratio === 1)
704
747
  return 'unknown-non-cc';
748
+ // Claude Code declares its own tools by their exact names. A foreign tool
749
+ // next to an alias sharing a CC tool's name, with no exact-case CC name, is
750
+ // another client's surface: its tools go out as declared.
751
+ if (unmapped > 0 && !native && sharesCCName)
752
+ return 'unknown-non-cc';
705
753
  // Mixed surface: only flag once there are enough tools to be confident.
706
754
  if (clientTools.length >= 3 && ratio >= 0.8)
707
755
  return 'unknown-non-cc';
@@ -1845,13 +1893,18 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
1845
1893
  // the fingerprint risk on their own account.
1846
1894
  const activeToolMap = new Map();
1847
1895
  const unmappedTools = [];
1896
+ // CC tools a declared client tool maps onto (identity or TOOL_MAP alias, not
1897
+ // the round-robin fallback slots). Read by the advertise step below.
1898
+ const claimedCC = new Set();
1899
+ // Client tools outside CC's set, under their MCP-shaped names, advertised
1900
+ // after the MCP tools by the step below.
1901
+ const mcpShapedTools = [];
1848
1902
  if (clientTools && !effectivePreserveTools && !effectiveMergeTools) {
1849
1903
  // Two passes so the unmapped-tool distributor can avoid colliding with
1850
1904
  // CC tools the client already uses directly. Without this, a client
1851
1905
  // sending both `WebSearch` and some unmapped tool like `memory_get`
1852
1906
  // could have both forward-map to `WebSearch`, and the reverse map would
1853
1907
  // then rewrite real `WebSearch` responses to the collided client name.
1854
- const claimedCC = new Set();
1855
1908
  for (const tool of clientTools) {
1856
1909
  const name = (tool.name || '').toLowerCase();
1857
1910
  // A CC client's OWN tools map to THEMSELVES (identity), and this OVERRIDES
@@ -1906,7 +1959,18 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
1906
1959
  // wants {action: "run"}). Better to let the model not see those tools
1907
1960
  // than to pretend they exist and corrupt every call. Users needing
1908
1961
  // every client tool to actually work must use --preserve-tools.
1962
+ //
1963
+ // - A declaration that names a CC tool: the advertise step sends only the
1964
+ // CC tools the client declared, so a fallback slot is never on the wire
1965
+ // and a round-robined tool would be invisible to the model. Each
1966
+ // unmapped tool goes out instead the way real CC sends a tool beyond its
1967
+ // built-ins: the client's own definition under an `mcp__<server>__<tool>`
1968
+ // name, arguments untouched, renamed back on the response. A tool that
1969
+ // cannot be carried that way fails the request with its name.
1909
1970
  const CC_FALLBACK_TOOLS = ['Bash', 'Read', 'Grep', 'Glob', 'WebSearch', 'WebFetch'];
1971
+ const namesCCTool = clientTools.some((t) => typeof t.name === 'string' && !isMcpToolName(t.name) && CC_TOOL_NAMES_LOWER.has(t.name.toLowerCase()));
1972
+ const declaredNames = new Set(clientTools.map((t) => t.name));
1973
+ const uncarried = [];
1910
1974
  for (const tool of clientTools) {
1911
1975
  const name = (tool.name || '').toLowerCase();
1912
1976
  if (CC_NATIVE_NAMES_UNION.has(tool.name) || isMcpToolName(tool.name) || TOOL_MAP[name])
@@ -1914,6 +1978,28 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
1914
1978
  unmappedTools.push(tool.name);
1915
1979
  if (opts.hybridTools)
1916
1980
  continue; // dropped — see comment above
1981
+ if (namesCCTool) {
1982
+ const clientName = typeof tool.name === 'string' ? tool.name : '';
1983
+ const wireName = `${CLIENT_TOOL_MCP_PREFIX}${clientName}`;
1984
+ const toolType = tool.type;
1985
+ if (!clientName) {
1986
+ uncarried.push({ name: String(tool.name ?? ''), reason: 'no name' });
1987
+ }
1988
+ else if ((toolType !== undefined && toolType !== 'custom') || !isAdvertisableToolDefinition(tool)) {
1989
+ uncarried.push({ name: clientName, reason: 'not a custom tool with an input_schema' });
1990
+ }
1991
+ else if (!TOOL_NAME_PATTERN.test(wireName)) {
1992
+ uncarried.push({ name: clientName, reason: `${wireName} is not a valid tool name of at most 64 characters` });
1993
+ }
1994
+ else if (declaredNames.has(wireName)) {
1995
+ uncarried.push({ name: clientName, reason: `the client also declares ${wireName}` });
1996
+ }
1997
+ else {
1998
+ activeToolMap.set(clientName, { ccTool: wireName, translateArgs: (a) => a, translateBack: (a) => a });
1999
+ mcpShapedTools.push({ ...tool, name: wireName });
2000
+ }
2001
+ continue;
2002
+ }
1917
2003
  // Default mode: round-robin distribution. Exclude CC tools the client
1918
2004
  // already uses so we never create a two-client-names-to-one-CC-tool
1919
2005
  // collision. If every fallback is claimed (rare: client already uses 6+
@@ -1944,6 +2030,8 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
1944
2030
  reverseScore: 0,
1945
2031
  });
1946
2032
  }
2033
+ if (uncarried.length > 0)
2034
+ throw new UnsupportedClientToolError(uncarried);
1947
2035
  }
1948
2036
  // ── Remap tool_use and tool_result references in message history ──
1949
2037
  // Skip in preserveTools mode — leave conversation history untouched.
@@ -2117,7 +2205,14 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
2117
2205
  // whole request with 400 "tools: Tool names must be unique". MCP
2118
2206
  // schemas are operator-supplied; the client's declaration is the only
2119
2207
  // authoritative source, never the template.
2120
- const availableCC = CC_TOOL_DEFINITIONS_UNION.filter((t) => !isMcpToolName(t.name) && clientToolNames.has(t.name.toLowerCase()));
2208
+ const namedCC = CC_TOOL_DEFINITIONS_UNION.filter((t) => !isMcpToolName(t.name) && clientToolNames.has(t.name.toLowerCase()));
2209
+ // Once the declaration names any CC tool, every alias also declares the
2210
+ // CC tool it maps onto (`read_file` declares Read): advertising only the
2211
+ // name matches would leave the model unable to call the other mapped
2212
+ // tools. A declaration that names no CC tool keeps the full template.
2213
+ const availableCC = namedCC.length > 0
2214
+ ? CC_TOOL_DEFINITIONS_UNION.filter((t) => !isMcpToolName(t.name) && (clientToolNames.has(t.name.toLowerCase()) || claimedCC.has(t.name)))
2215
+ : namedCC;
2121
2216
  const mcpTools = clientTools.filter((t) => isMcpToolName(t.name));
2122
2217
  // A CC-native name the bundle knows but cannot advertise (dario#1376:
2123
2218
  // `advisor` captured with an empty schema) is still identity-mapped
@@ -2125,8 +2220,10 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
2125
2220
  // verbatim, as MCP tools do. The client's schema is what its parser
2126
2221
  // expects back in any case.
2127
2222
  const clientOwnNative = clientTools.filter((t) => typeof t.name === 'string' && CC_TOOL_DEFINITIONS_UNADVERTISABLE.has(t.name) && isAdvertisableToolDefinition(t));
2128
- ccRequest.tools = availableCC.length > 0 || mcpTools.length > 0 || clientOwnNative.length > 0
2129
- ? dedupeToolsByName([...availableCC, ...clientOwnNative, ...mcpTools])
2223
+ // Client tools outside CC's set follow the MCP tools, under the
2224
+ // MCP-shaped names the tool map gave them.
2225
+ ccRequest.tools = availableCC.length > 0 || mcpTools.length > 0 || clientOwnNative.length > 0 || mcpShapedTools.length > 0
2226
+ ? dedupeToolsByName([...availableCC, ...clientOwnNative, ...mcpTools, ...mcpShapedTools])
2130
2227
  : CC_TOOL_DEFINITIONS;
2131
2228
  }
2132
2229
  }
@@ -2259,7 +2356,10 @@ export function buildCCRequest(clientBody, billingTag, cacheControl, identity, o
2259
2356
  // to match that. Future CC releases that reshuffle or add a field are then
2260
2357
  // picked up by the next live refresh without a dario release.
2261
2358
  const orderedBody = orderBodyForOutbound(ccRequest);
2262
- return { body: orderedBody, toolMap: activeToolMap, unmappedTools, detectedClient };
2359
+ // Client names of the unmapped tools that went out MCP-shaped (the rest of
2360
+ // unmappedTools rode fallback slots or, in hybrid mode, were dropped).
2361
+ const carriedAsMcp = unmappedTools.filter((n) => activeToolMap.get(n)?.ccTool === `${CLIENT_TOOL_MCP_PREFIX}${n}`);
2362
+ return { body: orderedBody, toolMap: activeToolMap, unmappedTools, carriedAsMcp, detectedClient };
2263
2363
  }
2264
2364
  /**
2265
2365
  * Build the CC-name → {clientName, mapping} reverse lookup used by both
package/dist/cli.js CHANGED
@@ -536,8 +536,9 @@ async function proxy() {
536
536
  ?? fileCfg.pool?.strategy;
537
537
  // --pool-headroom-floor=<ratio|percent> — the headroom at/below which a seat
538
538
  // counts as drained: sticky sessions rebind off it and new conversations
539
- // skip it. Default 2%; dario#1333 asked to leave a seat alone at 95% used
540
- // rather than ride it into the 429.
539
+ // skip it. Setting the floor makes it a hard stop; the implicit 2% default
540
+ // stays a routing preference. dario#1333 asked to leave a seat alone at 95%
541
+ // used rather than ride it into the 429.
541
542
  const poolHeadroomFloorFromFlag = args.find((a) => a.startsWith('--pool-headroom-floor='))?.split('=')[1];
542
543
  if (poolHeadroomFloorFromFlag !== undefined && parsePoolHeadroomFloor(poolHeadroomFloorFromFlag) === null) {
543
544
  console.error(`[dario] Invalid --pool-headroom-floor "${poolHeadroomFloorFromFlag}". Use a ratio or percent between 2% and 50% (e.g. 0.05 or 5%).`);
@@ -2203,8 +2204,9 @@ async function help() {
2203
2204
  --pool-headroom-floor=<ratio|percent>
2204
2205
  Headroom at or below which a seat counts as
2205
2206
  drained: a sticky session rebinds off it and
2206
- new conversations skip it. 0.05 or 5%;
2207
- default 2%, max 50%. Use it when your seats
2207
+ new conversations skip it. Setting this makes
2208
+ the floor a hard stop. 0.05 or 5%; implicit
2209
+ default 2% is a preference, max 50%. Use it when your seats
2208
2210
  answer with API errors in the last percent
2209
2211
  of a window (dario#1333).
2210
2212
  Env: DARIO_POOL_HEADROOM_FLOOR;
@@ -38,7 +38,11 @@ export declare function parseCodexUsageHeaders(h: HeaderReader, now?: number): C
38
38
  export declare function parseCodexUsageEndpoint(body: unknown, now?: number): CodexUsage | null;
39
39
  /** How often an IDLE seat is re-read from /wham/usage. A seat that is serving is read from every answer and never polled. */
40
40
  export declare const DEFAULT_CODEX_USAGE_POLL_MS: number;
41
- /** `DARIO_CODEX_USAGE_POLL_MS`: unset/garbage → default, `0` → off, anything else floored at one minute. */
41
+ /**
42
+ * `DARIO_CODEX_USAGE_POLL_MS`: unset/garbage → default, `0` → off, anything
43
+ * else floored at one minute and capped at the longest interval a timer can
44
+ * hold (timer-ms.ts), since a larger one fires every millisecond.
45
+ */
42
46
  export declare function resolveCodexUsagePollMs(raw: string | undefined): number;
43
47
  /** Record a reading for a seat. A newer reading replaces an older one; an older one never replaces a newer one. */
44
48
  export declare function recordCodexUsage(alias: string, usage: CodexUsage): void;
@@ -8,6 +8,7 @@
8
8
  * which slot is which. Pure and memory-only, so codex-accounts and
9
9
  * codex-backend can both import it without a cycle.
10
10
  */
11
+ import { clampTimerMs } from './timer-ms.js';
11
12
  function finite(raw) {
12
13
  if (raw === null || raw === undefined || raw === '')
13
14
  return null;
@@ -77,7 +78,11 @@ export function parseCodexUsageEndpoint(body, now = Date.now()) {
77
78
  }
78
79
  /** How often an IDLE seat is re-read from /wham/usage. A seat that is serving is read from every answer and never polled. */
79
80
  export const DEFAULT_CODEX_USAGE_POLL_MS = 30 * 60 * 1000;
80
- /** `DARIO_CODEX_USAGE_POLL_MS`: unset/garbage → default, `0` → off, anything else floored at one minute. */
81
+ /**
82
+ * `DARIO_CODEX_USAGE_POLL_MS`: unset/garbage → default, `0` → off, anything
83
+ * else floored at one minute and capped at the longest interval a timer can
84
+ * hold (timer-ms.ts), since a larger one fires every millisecond.
85
+ */
81
86
  export function resolveCodexUsagePollMs(raw) {
82
87
  if (raw === undefined || raw.trim() === '')
83
88
  return DEFAULT_CODEX_USAGE_POLL_MS;
@@ -86,7 +91,7 @@ export function resolveCodexUsagePollMs(raw) {
86
91
  return DEFAULT_CODEX_USAGE_POLL_MS;
87
92
  if (n === 0)
88
93
  return 0;
89
- return Math.max(60_000, Math.round(n));
94
+ return clampTimerMs(Math.max(60_000, Math.round(n)));
90
95
  }
91
96
  const readings = new Map();
92
97
  /** Record a reading for a seat. A newer reading replaces an older one; an older one never replaces a newer one. */
@@ -23,6 +23,7 @@ import { describeTemplate, detectDrift, checkCCCompat, findInstalledCC, missingV
23
23
  import { detectCCOAuthConfig } from './cc-oauth-detect.js';
24
24
  import { runAuthorizeProbe } from './cc-authorize-probe.js';
25
25
  import { MIGRATED_LOGIN_ALIAS } from './accounts.js';
26
+ import { clampTimerMs } from './timer-ms.js';
26
27
  const __dirname = dirname(fileURLToPath(import.meta.url));
27
28
  /**
28
29
  * Decide what `doctor` should say about pool-exhaustion failover, from
@@ -1138,7 +1139,7 @@ export async function runChecks(opts = {}) {
1138
1139
  // default: `new AccountPool()` here always predicted a max-headroom
1139
1140
  // pick, which is the wrong seat on a fill-first pool.
1140
1141
  const routing = configuredPoolRouting(loadConfig().config.pool);
1141
- const pool = new AccountPool(routing.strategy, routing.headroomFloor);
1142
+ const pool = new AccountPool(routing.strategy, routing.headroomFloorExplicit ? routing.headroomFloor : undefined);
1142
1143
  for (const acc of loaded) {
1143
1144
  pool.add(acc.alias, {
1144
1145
  accessToken: acc.accessToken,
@@ -1454,7 +1455,7 @@ function suggestAuthFix(result) {
1454
1455
  * proxy, does not log, does not persist.
1455
1456
  */
1456
1457
  export async function runAuthCheck(opts = {}) {
1457
- const timeoutMs = opts.timeoutMs ?? 30_000;
1458
+ const timeoutMs = clampTimerMs(opts.timeoutMs ?? 30_000);
1458
1459
  const expected = opts.expectedKey ?? process.env.DARIO_API_KEY ?? '';
1459
1460
  if (!expected) {
1460
1461
  return {
package/dist/keys.d.ts CHANGED
@@ -83,10 +83,12 @@ export declare const BUDGET_DEFAULT_MAX_TOKENS = 8192;
83
83
  export declare const EMPTY_RESERVATION: KeyBudgetReservation;
84
84
  /**
85
85
  * The reservation for one request, from what is known before it is sent.
86
- * `priceOf` is analytics' costOfTokens, injected so this module stays free of
87
- * the pricing table (the ledger injects the same way).
86
+ * `priceOf` is analytics' costOfTokensFailClosed on the proxy path, injected
87
+ * so this module stays free of the pricing table (the ledger injects the same
88
+ * way). `model` is what the request will be billed as; when routing could
89
+ * still bill it as one of several, pass them all and the costliest prices it.
88
90
  */
89
- export declare function requestBudgetReservation(model: string, bodyBytes: number, maxTokens: number | null | undefined, priceOf: (model: string, atMs: number, cell: {
91
+ export declare function requestBudgetReservation(model: string | readonly string[], bodyBytes: number, maxTokens: number | null | undefined, priceOf: (model: string, atMs: number, cell: {
90
92
  requests: number;
91
93
  inputTokens: number;
92
94
  outputTokens: number;
package/dist/keys.js CHANGED
@@ -44,15 +44,19 @@ export const BUDGET_DEFAULT_MAX_TOKENS = 8_192;
44
44
  export const EMPTY_RESERVATION = { count: 0, usd: 0, tokens: 0 };
45
45
  /**
46
46
  * The reservation for one request, from what is known before it is sent.
47
- * `priceOf` is analytics' costOfTokens, injected so this module stays free of
48
- * the pricing table (the ledger injects the same way).
47
+ * `priceOf` is analytics' costOfTokensFailClosed on the proxy path, injected
48
+ * so this module stays free of the pricing table (the ledger injects the same
49
+ * way). `model` is what the request will be billed as; when routing could
50
+ * still bill it as one of several, pass them all and the costliest prices it.
49
51
  */
50
52
  export function requestBudgetReservation(model, bodyBytes, maxTokens, priceOf, now = Date.now(),
51
53
  /** Bytes dario adds to the prompt beyond the client's body (the template's system prompt and tools). */
52
54
  extraPromptBytes = 0) {
53
55
  const inputTokens = Math.ceil((Math.max(0, bodyBytes) + Math.max(0, extraPromptBytes)) / BUDGET_BYTES_PER_TOKEN);
54
56
  const outputTokens = Number.isFinite(maxTokens) && maxTokens > 0 ? Math.ceil(maxTokens) : BUDGET_DEFAULT_MAX_TOKENS;
55
- const usd = priceOf(model, now, { requests: 1, inputTokens: 0, outputTokens, cacheReadTokens: 0, cacheCreateTokens: inputTokens });
57
+ const cell = { requests: 1, inputTokens: 0, outputTokens, cacheReadTokens: 0, cacheCreateTokens: inputTokens };
58
+ const models = typeof model === 'string' ? [model] : model.length > 0 ? model : [''];
59
+ const usd = Math.max(...models.map((m) => priceOf(m, now, cell)));
56
60
  return { count: 1, usd: Number.isFinite(usd) ? usd : 0, tokens: inputTokens + outputTokens };
57
61
  }
58
62
  export function addReservation(a, b) {
@@ -34,6 +34,7 @@
34
34
  */
35
35
  import { EventEmitter } from 'node:events';
36
36
  import { isNonSubscriptionBilling } from './analytics.js';
37
+ import { clampTimerMs } from './timer-ms.js';
37
38
  export class OverageGuard extends EventEmitter {
38
39
  opts;
39
40
  halted = null;
@@ -44,7 +45,8 @@ export class OverageGuard extends EventEmitter {
44
45
  // /analytics/stream + TUI tabs each register a listener; the in-proc
45
46
  // event listeners ceiling matches the Analytics class's choice.
46
47
  this.setMaxListeners(100);
47
- this.opts = opts;
48
+ // The cooldown is a timer's delay; capped so a huge one cannot fire after 1 ms (timer-ms.ts).
49
+ this.opts = { ...opts, cooldownMs: clampTimerMs(opts.cooldownMs) };
48
50
  }
49
51
  /**
50
52
  * Subscribe to an Analytics instance. Every record whose claim is a
package/dist/pool-sync.js CHANGED
@@ -28,6 +28,7 @@
28
28
  */
29
29
  import { randomUUID } from 'node:crypto';
30
30
  import { describeRateLimitSnapshot, rateLimitWindowPassed } from './pool.js';
31
+ import { clampTimerMs } from './timer-ms.js';
31
32
  export const DEFAULT_POOL_SYNC_INTERVAL_MS = 2_000;
32
33
  /** A peer reading older than this describes windows that have long rolled. */
33
34
  export const SHARED_SEAT_MAX_AGE_MS = 6 * 3_600_000;
@@ -66,7 +67,7 @@ export class PoolSync {
66
67
  this.baseUrl = opts.baseUrl.replace(/\/+$/, '');
67
68
  this.token = opts.token;
68
69
  this.instance = opts.instance ?? randomUUID();
69
- this.intervalMs = Math.max(200, opts.intervalMs ?? DEFAULT_POOL_SYNC_INTERVAL_MS);
70
+ this.intervalMs = clampTimerMs(Math.max(200, opts.intervalMs ?? DEFAULT_POOL_SYNC_INTERVAL_MS));
70
71
  this.stickyTtlMs = opts.stickyTtlMs ?? 6 * 3_600_000;
71
72
  this.log = opts.log ?? ((line) => console.error(line));
72
73
  }
package/dist/pool.d.ts CHANGED
@@ -437,6 +437,7 @@ export declare function configuredPoolRouting(file: {
437
437
  } | undefined, env?: NodeJS.ProcessEnv): {
438
438
  strategy: PoolStrategy;
439
439
  headroomFloor: number;
440
+ headroomFloorExplicit: boolean;
440
441
  };
441
442
  /**
442
443
  * Which wire buckets bind which model family, when the wire does not say it
@@ -602,8 +603,6 @@ export declare function resolvePoolHeadroomFloor(explicit?: string | number | nu
602
603
  export declare function seatExpiry(account: PoolAccount, now?: number): number;
603
604
  export declare class AccountPool {
604
605
  readonly strategy: PoolStrategy;
605
- /** Headroom at/below which a seat counts as drained — see DEFAULT_POOL_HEADROOM_FLOOR. */
606
- readonly headroomFloor: number;
607
606
  private accounts;
608
607
  private queue;
609
608
  private queueMaxSize;
@@ -611,9 +610,11 @@ export declare class AccountPool {
611
610
  private drainTimer;
612
611
  private sticky;
613
612
  private lastStickyCleanup;
614
- constructor(strategy?: PoolStrategy,
615
613
  /** Headroom at/below which a seat counts as drained — see DEFAULT_POOL_HEADROOM_FLOOR. */
616
- headroomFloor?: number);
614
+ readonly headroomFloor: number;
615
+ /** Only an explicitly configured floor is a hard stop; the implicit 2% remains a preference. */
616
+ readonly hardHeadroomFloor: boolean;
617
+ constructor(strategy?: PoolStrategy, headroomFloor?: number);
617
618
  add(alias: string, opts: {
618
619
  accessToken: string;
619
620
  refreshToken: string;
@@ -729,6 +730,8 @@ export declare class AccountPool {
729
730
  adoptSnapshot(alias: string, snapshot: RateLimitSnapshot, rejected: boolean, from: string): boolean;
730
731
  updateTokens(alias: string, accessToken: string, refreshToken: string, expiresAt: number): void;
731
732
  get(alias: string): PoolAccount | undefined;
733
+ /** Whether a preferred (but not pinned) seat may bypass normal pool ordering. */
734
+ canPrefer(account: PoolAccount, now?: number, family?: string | null): boolean;
732
735
  all(): PoolAccount[];
733
736
  status(): PoolStatus;
734
737
  /**