@blockrun/franklin 3.35.1 → 3.35.3

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.
@@ -13,7 +13,14 @@ import type { Dialogue } from './types.js';
13
13
  export declare const CAPPED_MAX_TOKENS = 16384;
14
14
  /** Escalated max_tokens after hitting the cap */
15
15
  export declare const ESCALATED_MAX_TOKENS = 65536;
16
- /** Get max output tokens for a model */
16
+ /**
17
+ * Get max output tokens for a model.
18
+ *
19
+ * The static table above wins. The gateway catalog is consulted only for
20
+ * models with no entry — its own max_output values are demonstrably wrong for
21
+ * models we do have knowledge of (it reports 8192 for claude-haiku-4.5, which
22
+ * Anthropic documents as 64000), so it is a gap-filler, not a source of truth.
23
+ */
17
24
  export declare function getMaxOutputTokens(model: string): number;
18
25
  /**
19
26
  * Cap tool result sizes to prevent context bloat.
@@ -8,6 +8,7 @@
8
8
  * 4. Adaptive max_tokens — start low (8K), escalate on hit
9
9
  * 5. Pre-compact stripping — remove images/docs before summarization
10
10
  */
11
+ import { peekGatewayModel, warmGatewayModelsCache } from '../gateway-models.js';
11
12
  // ─── Constants ─────────────────────────────────────────────────────────────
12
13
  /** Max chars per individual tool result before truncation (history-level safety net) */
13
14
  const MAX_TOOL_RESULT_CHARS = 32_000;
@@ -62,9 +63,23 @@ const MODEL_MAX_OUTPUT = {
62
63
  // 16K default and truncates mid-answer, burning USDC on continuation calls.
63
64
  'qwen/qwen3.7-max': 65_536,
64
65
  };
65
- /** Get max output tokens for a model */
66
+ /**
67
+ * Get max output tokens for a model.
68
+ *
69
+ * The static table above wins. The gateway catalog is consulted only for
70
+ * models with no entry — its own max_output values are demonstrably wrong for
71
+ * models we do have knowledge of (it reports 8192 for claude-haiku-4.5, which
72
+ * Anthropic documents as 64000), so it is a gap-filler, not a source of truth.
73
+ */
66
74
  export function getMaxOutputTokens(model) {
67
- return MODEL_MAX_OUTPUT[model] ?? 16_384;
75
+ const known = MODEL_MAX_OUTPUT[model];
76
+ if (known)
77
+ return known;
78
+ const live = peekGatewayModel(model)?.max_output;
79
+ if (live && live > 0)
80
+ return live;
81
+ warmGatewayModelsCache();
82
+ return 16_384;
68
83
  }
69
84
  /** Idle gap (minutes) after which old tool results are cleared.
70
85
  * Set to 30 min — a coffee break shouldn't lose tool context.
@@ -3,6 +3,7 @@
3
3
  * Uses byte-based heuristic (no external tokenizer dependency).
4
4
  * Anchors to actual API counts when available, estimates on top for new messages.
5
5
  */
6
+ import { peekGatewayModel, warmGatewayModelsCache } from '../gateway-models.js';
6
7
  const DEFAULT_BYTES_PER_TOKEN = 4;
7
8
  /**
8
9
  * Model-specific bytes-per-token ratios for more accurate estimation.
@@ -274,8 +275,22 @@ const MODEL_CONTEXT_WINDOWS = {
274
275
  * Get the context window size for a model, with a conservative default.
275
276
  */
276
277
  export function getContextWindow(model) {
278
+ // The static table wins. It is not merely a cache of the gateway catalog —
279
+ // several entries are deliberate DOWNGRADES from what the gateway advertises
280
+ // (see the Anthropic block above: the gateway reports 1M, but its 1M beta
281
+ // header is not enabled, so anything over 200k 413s). Letting the catalog
282
+ // override these would reintroduce the exact bug those comments prevent.
277
283
  if (MODEL_CONTEXT_WINDOWS[model])
278
284
  return MODEL_CONTEXT_WINDOWS[model];
285
+ // No static entry — a live catalog value beats the blind default below.
286
+ // This is the qwen3.7-max class: a real model nobody has catalogued yet,
287
+ // which would otherwise silently compact at 128k.
288
+ const live = peekGatewayModel(model)?.context_window;
289
+ if (live && live > 0)
290
+ return live;
291
+ // Cache is cold. Kick a fetch (deduped in-flight, errors swallowed) so the
292
+ // next call in this session gets a real number instead of the blind default.
293
+ warmGatewayModelsCache();
279
294
  // Pattern-based inference for unknown models
280
295
  if (model.includes('gemini'))
281
296
  return 1_000_000;
@@ -56,6 +56,38 @@ export interface GatewayModel {
56
56
  }
57
57
  /** Test / reset helper. */
58
58
  export declare function clearGatewayModelsCache(): void;
59
+ /**
60
+ * Synchronous, cache-only lookup. Returns null when the catalog has never been
61
+ * fetched in this process — it never triggers a fetch, so it is safe to call
62
+ * from the hot path and from sync functions like getContextWindow().
63
+ *
64
+ * Deliberately ignores the TTL: a stale gateway record still beats no record
65
+ * at all, and the only callers are fallbacks for models we have no static
66
+ * entry for. Callers must treat the static tables as authoritative — the
67
+ * gateway's own metadata has been wrong for models we already know. As of
68
+ * 2026-07-20 it reported max_output 8192 for claude-haiku-4.5 (Anthropic
69
+ * documents 64000) and 64000 for claude-sonnet-4.6 (documented 128000).
70
+ * Those two specific numbers are being corrected upstream, so do not treat
71
+ * them as current — the point that outlives them is that the catalog can be
72
+ * wrong, and a wrong value here is not cosmetic.
73
+ *
74
+ * Correction to an earlier note here: these values are NOT inert metadata.
75
+ * The gateway clamps with them — Math.min(request.max_tokens, model.maxOutput)
76
+ * in both the messages and chat/completions handlers — and derives its price
77
+ * quote from the clamped ceiling. An over-cap request is accepted rather than
78
+ * rejected: the handler logs "capping to <limit>" server-side and continues,
79
+ * so from here the clamp is invisible until a reply is long enough to hit it.
80
+ * That is why a short smoke test against a wrongly-capped model looks healthy.
81
+ * Treat this as "better than a blind default", nothing more.
82
+ */
83
+ export declare function peekGatewayModel(id: string): GatewayModel | null;
84
+ /** Test helper — seed the cache without a network call. */
85
+ export declare function __primeGatewayModelsCache(models: GatewayModel[]): void;
86
+ /**
87
+ * Fire-and-forget catalog warm. Populates the cache so the sync peek above has
88
+ * something to read. Errors are swallowed — every caller has a static fallback.
89
+ */
90
+ export declare function warmGatewayModelsCache(): void;
59
91
  /**
60
92
  * Fetch the model catalog, honoring the 5-minute cache. Concurrent callers
61
93
  * during a cold cache share a single in-flight promise so we don't stampede
@@ -24,6 +24,46 @@ export function clearGatewayModelsCache() {
24
24
  cache = null;
25
25
  inflight = null;
26
26
  }
27
+ /**
28
+ * Synchronous, cache-only lookup. Returns null when the catalog has never been
29
+ * fetched in this process — it never triggers a fetch, so it is safe to call
30
+ * from the hot path and from sync functions like getContextWindow().
31
+ *
32
+ * Deliberately ignores the TTL: a stale gateway record still beats no record
33
+ * at all, and the only callers are fallbacks for models we have no static
34
+ * entry for. Callers must treat the static tables as authoritative — the
35
+ * gateway's own metadata has been wrong for models we already know. As of
36
+ * 2026-07-20 it reported max_output 8192 for claude-haiku-4.5 (Anthropic
37
+ * documents 64000) and 64000 for claude-sonnet-4.6 (documented 128000).
38
+ * Those two specific numbers are being corrected upstream, so do not treat
39
+ * them as current — the point that outlives them is that the catalog can be
40
+ * wrong, and a wrong value here is not cosmetic.
41
+ *
42
+ * Correction to an earlier note here: these values are NOT inert metadata.
43
+ * The gateway clamps with them — Math.min(request.max_tokens, model.maxOutput)
44
+ * in both the messages and chat/completions handlers — and derives its price
45
+ * quote from the clamped ceiling. An over-cap request is accepted rather than
46
+ * rejected: the handler logs "capping to <limit>" server-side and continues,
47
+ * so from here the clamp is invisible until a reply is long enough to hit it.
48
+ * That is why a short smoke test against a wrongly-capped model looks healthy.
49
+ * Treat this as "better than a blind default", nothing more.
50
+ */
51
+ export function peekGatewayModel(id) {
52
+ if (!cache)
53
+ return null;
54
+ return cache.models.find(m => m.id === id) ?? null;
55
+ }
56
+ /** Test helper — seed the cache without a network call. */
57
+ export function __primeGatewayModelsCache(models) {
58
+ cache = { models, expiresAt: Date.now() + CACHE_TTL_MS };
59
+ }
60
+ /**
61
+ * Fire-and-forget catalog warm. Populates the cache so the sync peek above has
62
+ * something to read. Errors are swallowed — every caller has a static fallback.
63
+ */
64
+ export function warmGatewayModelsCache() {
65
+ void getGatewayModels().catch(() => { });
66
+ }
27
67
  // ─── Fetch ──────────────────────────────────────────────────────────────
28
68
  async function doFetch() {
29
69
  const chain = loadChain();
@@ -9,6 +9,7 @@ import { appendAudit } from '../stats/audit.js';
9
9
  import { buildFallbackChain, DEFAULT_FALLBACK_CONFIG, ROUTING_PROFILES, } from './fallback.js';
10
10
  import { routeRequest, parseRoutingProfile, isVisionModel, messagesNeedVision, pickVisionSibling, } from '../router/index.js';
11
11
  import { estimateCost } from '../pricing.js';
12
+ import { getMaxOutputTokens } from '../agent/optimize.js';
12
13
  import { VERSION } from '../config.js';
13
14
  // User-Agent for backend requests
14
15
  const USER_AGENT = `franklin/${VERSION}`;
@@ -317,12 +318,11 @@ export function createProxy(options) {
317
318
  }
318
319
  {
319
320
  const original = parsed.max_tokens;
320
- const model = (parsed.model || '').toLowerCase();
321
- const modelCap = model.includes('deepseek') ||
322
- model.includes('haiku') ||
323
- model.includes('gpt-oss')
324
- ? 8192
325
- : 16384;
321
+ // Was a hardcoded substring ladder (deepseek|haiku|gpt-oss → 8192,
322
+ // everything else → 16384) that ignored MODEL_MAX_OUTPUT entirely,
323
+ // so proxy users got a 16K ceiling on models the CLI already knew
324
+ // could emit 65K+ (Kimi K3, GPT-5.6 Sol). Same table both paths now.
325
+ const modelCap = getMaxOutputTokens(parsed.model || '');
326
326
  // Use max of (last output × 2, default 4096) capped by model limit
327
327
  // This ensures short replies don't starve the next request
328
328
  const lastOut = lastOutputByModel.get(requestModel) ?? 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/franklin",
3
- "version": "3.35.1",
3
+ "version": "3.35.3",
4
4
  "description": "Franklin Agent — The AI agent with a wallet. Spends USDC autonomously to get real work done. Pay per action, no subscriptions.",
5
5
  "type": "module",
6
6
  "exports": {