@askalf/dario 6.12.6 → 6.12.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.
@@ -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
@@ -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
@@ -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/proxy.d.ts CHANGED
@@ -70,6 +70,23 @@ export declare function applyModelAlias(model: string | null | undefined, aliase
70
70
  * change is inert until the operator opts in.
71
71
  */
72
72
  export declare function selectModelOverride(incomingModel: string, modelOverride: string | null, fastModelOverride: string | null): string | null;
73
+ /**
74
+ * The model ids a request can be billed as, for pricing its key-budget
75
+ * reservation, which runs before routing. The client's model goes through
76
+ * the resolution the request path applies (operator alias, provider prefix,
77
+ * effort suffix, family shorthand, the OpenAI-name map on the chat shape),
78
+ * and a server-wide --model / --fast-model override is added beside it,
79
+ * since routing has not yet decided whether it replaces the name. The
80
+ * reservation is an upper bound, so it prices the costliest of these.
81
+ */
82
+ export declare function budgetModelsFor(clientModel: string, ctx: {
83
+ modelAliases?: Record<string, string>;
84
+ modelOverride: string | null;
85
+ fastModelOverride: string | null;
86
+ cliProviderOverride: 'openai' | 'claude' | 'codex' | null;
87
+ cliModelRaw: string | null | undefined;
88
+ isOpenAI: boolean;
89
+ }): string[];
73
90
  /**
74
91
  * Rebuild an OpenAI-shape request body with the model swapped to the pool-
75
92
  * fallback target. Null when the body isn't a JSON object — the caller
package/dist/proxy.js CHANGED
@@ -9,14 +9,14 @@ import { getAccessToken, getStatus, ignoreCcCredentials } from './oauth.js';
9
9
  import { buildHealthResponse, derivePoolStatus, probeRequested, shouldDiscloseHealthInternals, shouldRunServingProbe } from './health-response.js';
10
10
  import { getServingProbe } from './serving-probe.js';
11
11
  import { darioVersion } from './version.js';
12
- import { CC_TOOL_DEFINITIONS_UNADVERTISABLE, CC_TEMPLATE_PROMPT_BYTES, resolveMaxTokens, buildCCRequest, applyCcPromptCaching, isGenuineCCClient, parseEffortSuffix, reverseMapResponse, createStreamingReverseMapper, orderHeadersForOutbound, overlayTemplateHeaderValues, forwardClientCCIdentityHeaders, isMcpToolName, CC_TEMPLATE, effectiveCacheControl, withForced1hBeta } from './cc-template.js';
12
+ import { CC_TOOL_DEFINITIONS_UNADVERTISABLE, CC_TEMPLATE_PROMPT_BYTES, resolveMaxTokens, buildCCRequest, applyCcPromptCaching, isGenuineCCClient, parseEffortSuffix, reverseMapResponse, createStreamingReverseMapper, orderHeadersForOutbound, overlayTemplateHeaderValues, forwardClientCCIdentityHeaders, isMcpToolName, UnsupportedClientToolError, CLIENT_TOOL_MCP_PREFIX, CC_TEMPLATE, effectiveCacheControl, withForced1hBeta } from './cc-template.js';
13
13
  import { stampCch, hasCchSeed } from './cch.js';
14
14
  import { foldTiming, timingHeaders, timingLogFields } from './timing.js';
15
15
  import { describeTemplate, detectDrift, checkCCCompat, probeInstalledCCVersion } from './live-fingerprint.js';
16
16
  import { AccountPool, computeStickyKey, parseRateLimits, modelFamily, isInAuthCooldown, authCooldownMs, accountIneligibility, reportedAccountStatus, activeParkedBuckets, reconcilePoolAccounts, resolvePoolStrategy, resolvePoolHeadroomFloor, describePoolStrategy, DEFAULT_POOL_HEADROOM_FLOOR, utilFreshness, rateLimitWindow, accountAction, accountPeers, distinctAccounts, describeRejection, maskEmail, isAccountEligible } from './pool.js';
17
17
  import { backfillIdentity } from './accounts.js';
18
18
  import { PoolSync, DEFAULT_POOL_SYNC_INTERVAL_MS } from './pool-sync.js';
19
- import { Analytics, billingBucketFromClaim, costOfTokens, formatUsageLogLine, SUBSCRIPTION_CLAIMS, consumerFromHeader, consumerFromBody, CONSUMER_HEADER, CODEX_CLAIM } from './analytics.js';
19
+ import { Analytics, billingBucketFromClaim, costOfTokensFailClosed, formatUsageLogLine, SUBSCRIPTION_CLAIMS, consumerFromHeader, consumerFromBody, CONSUMER_HEADER, CODEX_CLAIM } from './analytics.js';
20
20
  import { Ledger, resolveLedgerPath, ledgerDisabledByEnv } from './ledger.js';
21
21
  import { renderPrometheus } from './metrics.js';
22
22
  import { renderSpendDonuts, renderAnalyticsView, ANALYTICS_UI_SHELL } from './donuts.js';
@@ -54,7 +54,8 @@ import { seedStaleCodexUsage } from './codex-backend.js';
54
54
  import { route as routeProvider } from './provider-adapter.js';
55
55
  import { selectPoolFallbackModels } from './pool-fallback-tier.js';
56
56
  import { RequestQueue, QueueFullError, QueueTimeoutError, DEFAULT_MAX_CONCURRENT, DEFAULT_MAX_QUEUED, DEFAULT_QUEUE_TIMEOUT_MS, resolveMaxConcurrent } from './request-queue.js';
57
- import { waitForIdle, DEFAULT_SHUTDOWN_GRACE_MS } from './shutdown-drain.js';
57
+ import { waitForIdle, drainThenClose, shutdownTimers, DEFAULT_SHUTDOWN_GRACE_MS } from './shutdown-drain.js';
58
+ import { clampTimerMs } from './timer-ms.js';
58
59
  import { redactSecrets } from './redact.js';
59
60
  import { BAKED_BASE_MODELS, withLongContextVariants, buildOpenAIModelsList, getModelCatalog, getCachedBases, resolveAliasAgainst, prewarmModelCatalog, retryModelCatalogNow, isSuspendedModel } from './model-catalog.js';
60
61
  import { classifyUpstreamRejection, diagnosticSnippet, POOL_PARKED } from './upstream-rejection.js';
@@ -302,7 +303,8 @@ const MODEL_ALIASES = {
302
303
  * that needs to Just Work.
303
304
  */
304
305
  export function resolveClaudeAlias(model) {
305
- return resolveAliasAgainst(model, getCachedBases()) ?? MODEL_ALIASES[model] ?? model;
306
+ // Own keys only: the model is client input, and `__proto__` would read Object.prototype.
307
+ return resolveAliasAgainst(model, getCachedBases()) ?? (Object.hasOwn(MODEL_ALIASES, model) ? MODEL_ALIASES[model] : model);
306
308
  }
307
309
  /**
308
310
  * User-defined model aliases: client-visible name → target model.
@@ -337,7 +339,9 @@ export function parseModelAliasSpecs(specs) {
337
339
  export function applyModelAlias(model, aliases) {
338
340
  if (!aliases || !model)
339
341
  return null;
340
- const target = aliases[model.trim().toLowerCase()];
342
+ const name = model.trim().toLowerCase();
343
+ // Own keys only, as in resolveClaudeAlias: the name is client input.
344
+ const target = Object.hasOwn(aliases, name) ? aliases[name] : undefined;
341
345
  if (target === undefined || target === model)
342
346
  return null;
343
347
  return target;
@@ -361,6 +365,36 @@ export function selectModelOverride(incomingModel, modelOverride, fastModelOverr
361
365
  return fastModelOverride;
362
366
  return modelOverride;
363
367
  }
368
+ /**
369
+ * The model ids a request can be billed as, for pricing its key-budget
370
+ * reservation, which runs before routing. The client's model goes through
371
+ * the resolution the request path applies (operator alias, provider prefix,
372
+ * effort suffix, family shorthand, the OpenAI-name map on the chat shape),
373
+ * and a server-wide --model / --fast-model override is added beside it,
374
+ * since routing has not yet decided whether it replaces the name. The
375
+ * reservation is an upper bound, so it prices the costliest of these.
376
+ */
377
+ export function budgetModelsFor(clientModel, ctx) {
378
+ const out = new Set();
379
+ const aliased = applyModelAlias(clientModel, ctx.modelAliases) ?? clientModel;
380
+ const prefix = parseProviderPrefix(aliased);
381
+ if (prefix) {
382
+ out.add(prefix.provider === 'claude' ? resolveClaudeAlias(parseEffortSuffix(prefix.model).model) : prefix.model);
383
+ }
384
+ else if (ctx.cliProviderOverride === 'openai' && ctx.cliModelRaw) {
385
+ out.add(ctx.cliModelRaw);
386
+ }
387
+ else {
388
+ const bare = isOpenAIModel(aliased) ? aliased : parseEffortSuffix(aliased).model;
389
+ out.add(resolveClaudeAlias(bare));
390
+ if (ctx.isOpenAI && Object.hasOwn(OPENAI_MODEL_MAP, bare))
391
+ out.add(OPENAI_MODEL_MAP[bare]);
392
+ }
393
+ const override = selectModelOverride(aliased, ctx.modelOverride, ctx.fastModelOverride);
394
+ if (override)
395
+ out.add(override);
396
+ return [...out];
397
+ }
364
398
  // Provider prefix in the `model` field — `<provider>:<model>`. Forces
365
399
  // routing regardless of model-name regex. Only recognized prefixes are
366
400
  // parsed, so ollama-style `llama3:8b` (without a recognized prefix)
@@ -400,7 +434,9 @@ export function parseProviderPrefix(model) {
400
434
  if (idx <= 0)
401
435
  return null;
402
436
  const prefix = model.slice(0, idx).toLowerCase();
403
- const provider = PROVIDER_PREFIXES[prefix];
437
+ // Own keys only: PROVIDER_PREFIXES is a plain object, so `__proto__:x` or `constructor:x` would
438
+ // otherwise read Object.prototype's members as a provider (found by fuzz/reject_parsers.fuzz.js).
439
+ const provider = Object.hasOwn(PROVIDER_PREFIXES, prefix) ? PROVIDER_PREFIXES[prefix] : undefined;
404
440
  if (!provider)
405
441
  return null;
406
442
  const stripped = model.slice(idx + 1);
@@ -1274,6 +1310,8 @@ export async function startProxy(opts = {}) {
1274
1310
  const toolSubLogged = new Set();
1275
1311
  // Same de-dup contract for the verbose-only MCP-passthrough note.
1276
1312
  const mcpPassthroughLogged = new Set();
1313
+ // Same de-dup contract for the note on client tools carried MCP-shaped.
1314
+ const mcpCarryLogged = new Set();
1277
1315
  // One-shot log for the genuine-CC byte-faithful passthrough path.
1278
1316
  let ccPassthroughLogged = false;
1279
1317
  // Body-dump mode: set via --verbose=2 / -vv or DARIO_LOG_BODIES=1.
@@ -1910,7 +1948,8 @@ export async function startProxy(opts = {}) {
1910
1948
  else if (pool.size > 0 && opts.maxConcurrent !== undefined && opts.maxConcurrent < pool.size) {
1911
1949
  console.error(`[dario] --max-concurrent=${opts.maxConcurrent} is below the ${pool.size} seats in the pool. That flag is a proxy-wide ceiling on in-flight requests, not a per-seat one: at most ${opts.maxConcurrent} requests can be in flight across all ${pool.size} seats, and the rest wait in dario. (dario#1244)`);
1912
1950
  }
1913
- const upstreamTimeoutMs = opts.upstreamTimeoutMs ?? UPSTREAM_TIMEOUT_MS;
1951
+ // Every upstream abort timer (Claude, codex, openai-compat, continuation) takes this value.
1952
+ const upstreamTimeoutMs = clampTimerMs(opts.upstreamTimeoutMs ?? UPSTREAM_TIMEOUT_MS);
1914
1953
  // Cache context-1m beta availability. Set false once per account after the
1915
1954
  // first "long context" rejection, so we skip sending context-1m on every
1916
1955
  // subsequent request instead of paying the round-trip + retry cost each time.
@@ -3597,13 +3636,20 @@ export async function startProxy(opts = {}) {
3597
3636
  // it — bounded by what dario will SEND: the client's body plus the
3598
3637
  // template's prompt (passthrough adds nothing), and the max_tokens
3599
3638
  // that will go on the wire (the template pins its own default unless
3600
- // --max-tokens=client; passthrough forwards the client's).
3639
+ // --max-tokens=client; passthrough forwards the client's). Priced at
3640
+ // the model it will be billed as (dario#1378): a short name or a
3641
+ // --model override priced as written fell back to the sonnet-4-6
3642
+ // rate, a third of fable's; a model with no published rate is priced
3643
+ // at the highest one.
3601
3644
  const pb = parsedBody;
3602
3645
  const clientMax = pb ? (pb.max_tokens ?? pb.max_completion_tokens ?? pb.max_output_tokens) : undefined;
3603
3646
  const outboundMax = passthrough
3604
3647
  ? (typeof clientMax === 'number' ? clientMax : null)
3605
3648
  : resolveMaxTokens(opts.maxTokens, { max_tokens: clientMax });
3606
- budgetReserved = requestBudgetReservation(typeof pb?.model === 'string' ? pb.model : '', body.length, outboundMax, costOfTokens, Date.now(), passthrough ? 0 : CC_TEMPLATE_PROMPT_BYTES);
3649
+ const billedAs = budgetModelsFor(typeof pb?.model === 'string' ? pb.model : '', {
3650
+ modelAliases, modelOverride, fastModelOverride, cliProviderOverride, cliModelRaw, isOpenAI,
3651
+ });
3652
+ budgetReserved = requestBudgetReservation(billedAs, body.length, outboundMax, costOfTokensFailClosed, Date.now(), passthrough ? 0 : CC_TEMPLATE_PROMPT_BYTES);
3607
3653
  budgetInflightKey = requestAuth.key.name;
3608
3654
  keyInflight.set(budgetInflightKey, addReservation(inflightNow, budgetReserved));
3609
3655
  }
@@ -4449,18 +4495,38 @@ export async function startProxy(opts = {}) {
4449
4495
  // resolved just above so body and header agree.
4450
4496
  const idSource = poolAccount ? poolAccount.identity : identity;
4451
4497
  const bodyIdentity = { deviceId: idSource.deviceId, accountUuid: idSource.accountUuid, sessionId: preBodySessionId };
4452
- const { body: ccBody, toolMap, detectedClient, unmappedTools, genuineCC } = buildCCRequest(r, billingTag, CACHE_EPHEMERAL, bodyIdentity, {
4453
- preserveTools: opts.preserveTools ?? false,
4454
- hybridTools: opts.hybridTools ?? false,
4455
- mergeTools: opts.mergeTools ?? false,
4456
- noAutoDetect: opts.noAutoDetect ?? false,
4457
- effort: requestEffort ?? opts.effort,
4458
- maxTokens: opts.maxTokens,
4459
- systemPrompt: opts.systemPrompt,
4460
- skipFields,
4461
- honorClientThinking: opts.honorClientThinking ?? false,
4462
- preserveOutputFormat: opts.preserveOutputFormat ?? false,
4463
- });
4498
+ let built;
4499
+ try {
4500
+ built = buildCCRequest(r, billingTag, CACHE_EPHEMERAL, bodyIdentity, {
4501
+ preserveTools: opts.preserveTools ?? false,
4502
+ hybridTools: opts.hybridTools ?? false,
4503
+ mergeTools: opts.mergeTools ?? false,
4504
+ noAutoDetect: opts.noAutoDetect ?? false,
4505
+ effort: requestEffort ?? opts.effort,
4506
+ maxTokens: opts.maxTokens,
4507
+ systemPrompt: opts.systemPrompt,
4508
+ skipFields,
4509
+ honorClientThinking: opts.honorClientThinking ?? false,
4510
+ preserveOutputFormat: opts.preserveOutputFormat ?? false,
4511
+ });
4512
+ }
4513
+ catch (err) {
4514
+ // A client tool the Claude Code-shaped request cannot carry is
4515
+ // refused by name. The catch around this block forwards the body
4516
+ // as received on any other throw, which would drop the tool.
4517
+ if (!(err instanceof UnsupportedClientToolError))
4518
+ throw err;
4519
+ console.log(`[dario] #${requestCount} refused: ${err.message}`);
4520
+ requestCount++;
4521
+ res.writeHead(400, JSON_HEADERS);
4522
+ res.end(JSON.stringify(isOpenAI || isResponses
4523
+ ? { error: { message: err.message, type: 'invalid_request_error', param: 'tools', code: null } }
4524
+ : { type: 'error', error: { type: 'invalid_request_error', message: err.message } }));
4525
+ return;
4526
+ }
4527
+ const { body: ccBody, toolMap, detectedClient, unmappedTools: notInToolMap, carriedAsMcp = [], genuineCC } = built;
4528
+ // The tools that rode a fallback slot; the MCP-shaped ones have their own note below.
4529
+ const unmappedTools = notInToolMap.filter((n) => !carriedAsMcp.includes(n));
4464
4530
  // Prompt-cache the tools + conversation prefix (the system prompt
4465
4531
  // is already cached in ccBody's system blocks). Mirrors CC's cache
4466
4532
  // breakpoints so a long session doesn't re-bill them as fresh input
@@ -4515,6 +4581,15 @@ export async function startProxy(opts = {}) {
4515
4581
  const more = unmappedTools.length > 5 ? `, +${unmappedTools.length - 5} more` : '';
4516
4582
  console.log(`[dario] tool substitution: ${unmappedTools.length}/${totalTools} client tool${unmappedTools.length === 1 ? '' : 's'} not in TOOL_MAP — remapped onto CC fallback slots (${sample}${more}). Pass --preserve-tools to forward your schemas verbatim instead.`);
4517
4583
  }
4584
+ // Client tools outside CC's set on a request that names CC tools go
4585
+ // out under MCP-shaped names with their own schemas: say so once
4586
+ // per client family, so the renamed names in a body dump are expected.
4587
+ if (carriedAsMcp.length > 0 && !preserveToolsEffective && !mcpCarryLogged.has(subKey)) {
4588
+ mcpCarryLogged.add(subKey);
4589
+ const sample = carriedAsMcp.slice(0, 5).join(', ');
4590
+ const more = carriedAsMcp.length > 5 ? `, +${carriedAsMcp.length - 5} more` : '';
4591
+ console.log(`[dario] ${carriedAsMcp.length} client tool${carriedAsMcp.length === 1 ? '' : 's'} outside Claude Code's set carried as ${CLIENT_TOOL_MCP_PREFIX}<name> with ${carriedAsMcp.length === 1 ? 'its' : 'their'} own schema${carriedAsMcp.length === 1 ? '' : 's'} (${sample}${more}); calls come back under the client's names.`);
4592
+ }
4518
4593
  // MCP tools (mcp__<server>__<tool>) forward verbatim in default
4519
4594
  // mode — real CC advertises session-attached MCP schemas as-is,
4520
4595
  // so this is passthrough, not substitution. Verbose-only note so
@@ -4707,7 +4782,7 @@ export async function startProxy(opts = {}) {
4707
4782
  const totalDelay = Math.max(pacingDelay, thinkDelay, sessionStartDelay);
4708
4783
  if (totalDelay > 0) {
4709
4784
  pacingMs += totalDelay;
4710
- await new Promise(r => setTimeout(r, totalDelay));
4785
+ await new Promise(r => setTimeout(r, clampTimerMs(totalDelay)));
4711
4786
  }
4712
4787
  seatClock.lastRequestTime = Date.now();
4713
4788
  // Session ID: resolved through the rotation registry keyed by the selected
@@ -6030,19 +6105,21 @@ export async function startProxy(opts = {}) {
6030
6105
  clearInterval(presenceInterval);
6031
6106
  clearInterval(refreshInterval);
6032
6107
  poolSync?.stop();
6033
- if (logFileStream)
6034
- logFileStream.end();
6035
- keyStore?.close();
6036
6108
  // Stop accepting connections now; responses already streaming keep going.
6037
- // Then flush tokens (best-effort), drain what is in flight up to the
6038
- // grace, and exit. The force-exit guard sits past the grace so a stream
6039
- // that never ends cannot wedge shutdown, and a hung fsync cannot either.
6109
+ // Tokens and what the ledger already holds are written while the drain
6110
+ // waits. The ledger, the key store and the request log close only after
6111
+ // the drain: a request that finishes inside the grace still records its
6112
+ // spend, its key's daily total and its log line (dario#1370). The
6113
+ // force-exit guard sits past the grace so a stream that never ends cannot
6114
+ // wedge shutdown, and a hung fsync cannot either.
6040
6115
  server.close();
6041
- const graceMs = opts.shutdownGraceMs ?? DEFAULT_SHUTDOWN_GRACE_MS;
6042
- void Promise.all([flushPoolTokens(), ledger?.close()])
6043
- .then(() => waitForIdle(() => queue.snapshot().active, { graceMs }))
6044
- .finally(() => process.exit(0));
6045
- setTimeout(() => process.exit(0), graceMs + 5000).unref();
6116
+ const { graceMs, forceExitMs } = shutdownTimers(opts.shutdownGraceMs ?? DEFAULT_SHUTDOWN_GRACE_MS);
6117
+ void drainThenClose(() => waitForIdle(() => queue.snapshot().active, { graceMs }), {
6118
+ before: [flushPoolTokens, () => ledger?.flush()],
6119
+ after: [() => ledger?.close(), () => keyStore?.close(), () => { if (logFileStream)
6120
+ logFileStream.end(); }],
6121
+ }).finally(() => process.exit(0));
6122
+ setTimeout(() => process.exit(0), forceExitMs).unref();
6046
6123
  };
6047
6124
  process.on('SIGINT', shutdown);
6048
6125
  process.on('SIGTERM', shutdown);
@@ -22,6 +22,7 @@
22
22
  *
23
23
  * dario#80 (Gemini review push-back).
24
24
  */
25
+ import { clampTimerMs } from './timer-ms.js';
25
26
  /** Pure admission decision — no side effects, no clock dep. */
26
27
  export function decideAdmit(state) {
27
28
  if (state.active < state.maxConcurrent)
@@ -94,7 +95,8 @@ export class RequestQueue {
94
95
  constructor(opts = {}) {
95
96
  this.maxConcurrent = opts.maxConcurrent ?? DEFAULT_MAX_CONCURRENT;
96
97
  this.maxQueued = opts.maxQueued ?? DEFAULT_MAX_QUEUED;
97
- this.queueTimeoutMs = opts.queueTimeoutMs ?? DEFAULT_QUEUE_TIMEOUT_MS;
98
+ // A timer's delay: capped where Node would otherwise fire it after 1 ms (timer-ms.ts).
99
+ this.queueTimeoutMs = clampTimerMs(opts.queueTimeoutMs ?? DEFAULT_QUEUE_TIMEOUT_MS);
98
100
  this.maxConcurrentPerConsumer = Math.max(0, opts.maxConcurrentPerConsumer ?? 0);
99
101
  this.unrefTimers = opts.unrefTimers ?? true;
100
102
  this.now = opts.now ?? Date.now;
@@ -3,6 +3,7 @@
3
3
  * Opt-in, trusted-callers-only, cached and single-flighted.
4
4
  */
5
5
  import { classifyUpstreamRejection, rejectionRemediation } from './upstream-rejection.js';
6
+ import { clampTimerMs } from './timer-ms.js';
6
7
  const ANTHROPIC_MESSAGES = 'https://api.anthropic.com/v1/messages';
7
8
  const ANTHROPIC_VERSION = '2023-06-01';
8
9
  const OAUTH_BETA = 'oauth-2025-04-20';
@@ -45,7 +46,7 @@ async function runProbe(deps) {
45
46
  const f = deps.fetchImpl ?? fetch;
46
47
  const now = deps.now ?? Date.now;
47
48
  const model = deps.model ?? process.env.DARIO_PROBE_MODEL ?? DEFAULT_PROBE_MODEL;
48
- const timeoutMs = deps.timeoutMs ?? envInt('DARIO_PROBE_TIMEOUT_MS', DEFAULT_PROBE_TIMEOUT_MS);
49
+ const timeoutMs = clampTimerMs(deps.timeoutMs ?? envInt('DARIO_PROBE_TIMEOUT_MS', DEFAULT_PROBE_TIMEOUT_MS));
49
50
  const startedAt = now();
50
51
  const ctl = new AbortController();
51
52
  const timer = setTimeout(() => ctl.abort(), timeoutMs);
@@ -14,6 +14,18 @@
14
14
  */
15
15
  /** How long a SIGTERM waits for in-flight requests by default. */
16
16
  export declare const DEFAULT_SHUTDOWN_GRACE_MS = 90000;
17
+ /** How long past the grace the force-exit guard waits before exiting anyway. */
18
+ export declare const SHUTDOWN_FORCE_EXIT_MARGIN_MS = 5000;
19
+ /**
20
+ * The drain's grace and the force-exit guard's delay for a configured grace.
21
+ * The guard is a timer, so the grace is capped where grace plus margin still
22
+ * fits one (timer-ms.ts): past that the guard would fire at once and skip
23
+ * the drain.
24
+ */
25
+ export declare function shutdownTimers(graceMs: number): {
26
+ graceMs: number;
27
+ forceExitMs: number;
28
+ };
17
29
  /** How often the drain re-reads the in-flight count. */
18
30
  export declare const SHUTDOWN_POLL_MS = 250;
19
31
  export interface DrainOptions {
@@ -41,3 +53,17 @@ export interface DrainResult {
41
53
  * poll, so a long drain is two lines rather than a scroll.
42
54
  */
43
55
  export declare function waitForIdle(getActive: () => number, opts: DrainOptions): Promise<DrainResult>;
56
+ /**
57
+ * The order `shutdown` runs its steps in: `before` and the drain start
58
+ * together, and `after` runs only once the drain has returned.
59
+ *
60
+ * Anything a finishing request writes to belongs in `after`. The ledger
61
+ * refuses rows once closed and an ended log stream drops lines, so closing
62
+ * either before the drain loses exactly the requests the drain waits for:
63
+ * the long streamed runs that finish inside the grace. A step that throws
64
+ * or rejects is skipped; the others still run and shutdown still exits.
65
+ */
66
+ export declare function drainThenClose(drain: () => Promise<unknown>, steps: {
67
+ before?: Array<() => unknown>;
68
+ after?: Array<() => unknown>;
69
+ }): Promise<void>;
@@ -12,8 +12,21 @@
12
12
  * runs out. It is pure over its inputs — the count, the clock and the sleep
13
13
  * are injected — so the policy is tested without a server or real timers.
14
14
  */
15
+ import { clampTimerMs, MAX_TIMER_MS } from './timer-ms.js';
15
16
  /** How long a SIGTERM waits for in-flight requests by default. */
16
17
  export const DEFAULT_SHUTDOWN_GRACE_MS = 90_000;
18
+ /** How long past the grace the force-exit guard waits before exiting anyway. */
19
+ export const SHUTDOWN_FORCE_EXIT_MARGIN_MS = 5_000;
20
+ /**
21
+ * The drain's grace and the force-exit guard's delay for a configured grace.
22
+ * The guard is a timer, so the grace is capped where grace plus margin still
23
+ * fits one (timer-ms.ts): past that the guard would fire at once and skip
24
+ * the drain.
25
+ */
26
+ export function shutdownTimers(graceMs) {
27
+ const grace = Math.min(clampTimerMs(graceMs), MAX_TIMER_MS - SHUTDOWN_FORCE_EXIT_MARGIN_MS);
28
+ return { graceMs: grace, forceExitMs: grace + SHUTDOWN_FORCE_EXIT_MARGIN_MS };
29
+ }
17
30
  /** How often the drain re-reads the in-flight count. */
18
31
  export const SHUTDOWN_POLL_MS = 250;
19
32
  const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -46,3 +59,18 @@ export async function waitForIdle(getActive, opts) {
46
59
  }
47
60
  }
48
61
  }
62
+ /**
63
+ * The order `shutdown` runs its steps in: `before` and the drain start
64
+ * together, and `after` runs only once the drain has returned.
65
+ *
66
+ * Anything a finishing request writes to belongs in `after`. The ledger
67
+ * refuses rows once closed and an ended log stream drops lines, so closing
68
+ * either before the drain loses exactly the requests the drain waits for:
69
+ * the long streamed runs that finish inside the grace. A step that throws
70
+ * or rejects is skipped; the others still run and shutdown still exits.
71
+ */
72
+ export async function drainThenClose(drain, steps) {
73
+ const run = (fns) => Promise.all((fns ?? []).map((fn) => Promise.resolve().then(fn).catch(() => undefined)));
74
+ await Promise.all([run(steps.before), drain().catch(() => undefined)]);
75
+ await run(steps.after);
76
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Delays that reach setTimeout / setInterval from an operator's env var,
3
+ * flag, config file or startProxy option.
4
+ *
5
+ * Node keeps a timer delay in a signed 32-bit int: anything above
6
+ * 2^31 - 1 ms (about 24.8 days), and anything non-finite, is replaced with
7
+ * 1 ms and a TimeoutOverflowWarning. A "very long" setting therefore becomes
8
+ * the shortest one: DARIO_CODEX_USAGE_POLL_MS=3000000000 turned the idle-seat
9
+ * poll into a 1 ms loop of account reloads and /wham/usage calls (dario#1400),
10
+ * and a huge DARIO_SHUTDOWN_GRACE_MS fired the force-exit at once, skipping
11
+ * the drain (dario#1370). Every such delay goes through clampTimerMs first.
12
+ */
13
+ /** The longest delay setTimeout and setInterval honour. */
14
+ export declare const MAX_TIMER_MS: number;
15
+ /**
16
+ * `ms` held to [0, MAX_TIMER_MS]: above the ceiling (Infinity included) it is
17
+ * the ceiling, the longest wait a timer can express; negative or NaN is 0.
18
+ */
19
+ export declare function clampTimerMs(ms: number): number;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Delays that reach setTimeout / setInterval from an operator's env var,
3
+ * flag, config file or startProxy option.
4
+ *
5
+ * Node keeps a timer delay in a signed 32-bit int: anything above
6
+ * 2^31 - 1 ms (about 24.8 days), and anything non-finite, is replaced with
7
+ * 1 ms and a TimeoutOverflowWarning. A "very long" setting therefore becomes
8
+ * the shortest one: DARIO_CODEX_USAGE_POLL_MS=3000000000 turned the idle-seat
9
+ * poll into a 1 ms loop of account reloads and /wham/usage calls (dario#1400),
10
+ * and a huge DARIO_SHUTDOWN_GRACE_MS fired the force-exit at once, skipping
11
+ * the drain (dario#1370). Every such delay goes through clampTimerMs first.
12
+ */
13
+ /** The longest delay setTimeout and setInterval honour. */
14
+ export const MAX_TIMER_MS = 2 ** 31 - 1;
15
+ /**
16
+ * `ms` held to [0, MAX_TIMER_MS]: above the ceiling (Infinity included) it is
17
+ * the ceiling, the longest wait a timer can express; negative or NaN is 0.
18
+ */
19
+ export function clampTimerMs(ms) {
20
+ if (Number.isNaN(ms) || ms <= 0)
21
+ return 0;
22
+ return ms >= MAX_TIMER_MS ? MAX_TIMER_MS : ms;
23
+ }
@@ -19,12 +19,14 @@ For a one-page status table of every tool dario supports — working / inferred
19
19
 
20
20
  Text-tool clients (Cline / Kilo Code / Roo Code and forks) are auto-detected via system-prompt identity markers and automatically flipped into preserve-tools mode, because mixing CC's `tools` array with their XML protocol makes the model emit `<function_calls><invoke>` that their parsers can't read. The same identity path also catches `hands` (askalf's computer-use agent) — its tool names overlap with `TOOL_MAP` but its schemas diverge, so identity match → preserve-tools is the only correct routing. If you run dario specifically for wire-level fidelity and would rather pick `--preserve-tools` yourself, `--no-auto-detect` (v3.20.1, aka `--no-auto-preserve`) disables the heuristic — explicit operator choice then wins.
21
21
 
22
- Beyond the identity path, dario falls back to a **structural** check: when a request carries 3+ tools and ≥80% of them aren't in `TOOL_MAP`, that's a custom client whose tool surface has effectively no overlap with CC's, and round-robin remap onto CC fallback slots silently corrupts the calls. The structural fallback flips those requests to preserve-tools too, with `client: 'unknown-non-cc'` in the request log. This catches in-house agents and OpenClaw derivatives that we haven't added an explicit pattern for, without needing per-client maintenance. `--no-auto-detect` disables both paths.
22
+ Beyond the identity path, dario falls back to a **structural** check: when a request carries 3+ tools and ≥80% of them aren't in `TOOL_MAP`, that's a custom client whose tool surface has effectively no overlap with CC's, and round-robin remap onto CC fallback slots silently corrupts the calls. The structural fallback flips those requests to preserve-tools too, with `client: 'unknown-non-cc'` in the request log. This catches in-house agents and OpenClaw derivatives that we haven't added an explicit pattern for, without needing per-client maintenance. A smaller surface flips too when it carries a tool outside `TOOL_MAP`, no Claude Code tool name in exact case, and a lowercase alias that shares a Claude Code tool's name (`grep`, `bash`): Claude Code names its own tools in exact case, so such a surface is another client's and goes out as declared. `--no-auto-detect` disables both paths.
23
23
 
24
24
  Platform-scoped CC natives (`PowerShell`, `Glob`, `Grep` — win32-only in CC's own tool list) map by the **client's** declaration, not the proxy host's platform (v4.8.136): a win32 CC client through a Linux-hosted dario gets their canonical definitions instead of having them dropped from the advertised array.
25
25
 
26
26
  MCP tools take a third path: anything named `mcp__<server>__<tool>` is neither remapped nor flipped to preserve — it forwards **verbatim** and its calls flow back untouched (v4.8.135). That's what real CC does with session-attached MCP servers; their schemas are operator-supplied and have no canonical template entry, so passthrough *is* the CC wire shape. Before this, a CC session with an MCP server attached had its `mcp__*` surface dropped from the advertised array and its history references round-robined onto fallback slots (`tool substitution: 28/52 client tools not in TOOL_MAP` in the live log), and two or three attached servers were enough to trip the 80% structural threshold on their own. MCP names no longer count toward that threshold.
27
27
 
28
+ A request that names Claude Code tools and also declares a tool outside `TOOL_MAP` (`Grep`, `Read`, `submit_review`) stays in remap, and the extra tool gets the MCP treatment: it goes out as `mcp__client__<name>` with the client's own schema, after the CC tools, and the model's calls come back under the client's name. Remap advertises only the CC tools such a request declares, so the fallback slot it used to ride was never on the wire. A tool that cannot be carried that way (an Anthropic-defined tool such as `computer_20251124`, a tool with no `input_schema`, or a name longer than 51 characters) fails the request with a 400 that names it. `--preserve-tools` forwards all of them verbatim instead.
29
+
28
30
  If your agent's tool names aren't pre-mapped and its tools carry fields CC's schema doesn't have, there are two escape hatches: **`--preserve-tools`** (forward your schema verbatim, lose the CC wire shape) or **`--hybrid-tools`** (keep the CC wire shape, fill request-context fields from headers). See [Custom tool schemas](#custom-tool-schemas).
29
31
 
30
32
  The OpenAI-compat backend forwards tool definitions byte-for-byte and doesn't need any of this.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@askalf/dario",
3
- "version": "6.12.6",
3
+ "version": "6.12.7",
4
4
  "description": "Use your Claude and ChatGPT subscriptions in Cursor, Cline, Aider, Claude Code and the Agent SDK — at subscription pricing, not per-token API bills. One local Anthropic + OpenAI-compatible endpoint: either plan answers either wire shape, with automatic failover when one hits its limit.",
5
5
  "type": "module",
6
6
  "bin": {