pi-smart-router 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.pi/extensions/smart-router/delegate-stream.ts +7 -1
  2. package/.pi/extensions/smart-router/delegation-runtime.ts +12 -2
  3. package/.pi/extensions/smart-router/fleet-bootstrap.ts +2 -0
  4. package/.pi/extensions/smart-router/pi-model-scope.ts +18 -3
  5. package/.pi/extensions/smart-router/routing-context.ts +95 -11
  6. package/dist/config/pi-model-mapper.d.ts +12 -1
  7. package/dist/config/pi-model-mapper.d.ts.map +1 -1
  8. package/dist/config/pi-model-mapper.js +33 -8
  9. package/dist/config/pi-model-mapper.js.map +1 -1
  10. package/dist/domain/delegation/delegation-context.d.ts +49 -0
  11. package/dist/domain/delegation/delegation-context.d.ts.map +1 -1
  12. package/dist/domain/delegation/delegation-context.js +92 -0
  13. package/dist/domain/delegation/delegation-context.js.map +1 -1
  14. package/dist/domain/matching/hydra-input.d.ts.map +1 -1
  15. package/dist/domain/matching/hydra-input.js +4 -17
  16. package/dist/domain/matching/hydra-input.js.map +1 -1
  17. package/dist/domain/pinning/loop-escalation.d.ts.map +1 -1
  18. package/dist/domain/pinning/loop-escalation.js +1 -49
  19. package/dist/domain/pinning/loop-escalation.js.map +1 -1
  20. package/dist/domain/pinning/tool-failure.d.ts +20 -0
  21. package/dist/domain/pinning/tool-failure.d.ts.map +1 -0
  22. package/dist/domain/pinning/tool-failure.js +62 -0
  23. package/dist/domain/pinning/tool-failure.js.map +1 -0
  24. package/dist/index.d.ts +1 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/infrastructure/persistence/sqlite-store.d.ts.map +1 -1
  29. package/dist/infrastructure/persistence/sqlite-store.js +37 -9
  30. package/dist/infrastructure/persistence/sqlite-store.js.map +1 -1
  31. package/dist/infrastructure/pricing/price-broker.d.ts +6 -1
  32. package/dist/infrastructure/pricing/price-broker.d.ts.map +1 -1
  33. package/dist/infrastructure/pricing/price-broker.js +16 -1
  34. package/dist/infrastructure/pricing/price-broker.js.map +1 -1
  35. package/package.json +3 -3
  36. package/src/config/pi-model-mapper.ts +48 -9
  37. package/src/domain/delegation/delegation-context.ts +123 -0
  38. package/src/domain/matching/hydra-input.ts +4 -19
  39. package/src/domain/pinning/loop-escalation.ts +1 -52
  40. package/src/domain/pinning/tool-failure.ts +66 -0
  41. package/src/index.ts +6 -0
  42. package/src/infrastructure/persistence/sqlite-store.ts +45 -9
  43. package/src/infrastructure/pricing/price-broker.ts +18 -2
@@ -1 +1 @@
1
- {"version":3,"file":"price-broker.js","sourceRoot":"","sources":["../../../src/infrastructure/pricing/price-broker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,OAAO,EACL,YAAY,EACZ,yBAAyB,GAE1B,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,YAAY,GAEb,MAAM,0CAA0C,CAAC;AAOlD,SAAS,oBAAoB,CAC3B,KAAmB,EACnB,OAAqB;IAErB,MAAM,QAAQ,GAAG,OAAO,CAAC,wBAAwB,CAAC;IAClD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,IAAI,KAAK,CAAC,EAAE,CAAC;IAC3D,OAAO,QAAQ,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAC3B,KAAmB,EACnB,OAA4B;IAE5B,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC;IAChC,MAAM,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,oBAAoB,CAAC,KAAK,EAAE,O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AAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACvC,KAA8B,EAC9B,OAA4B;IAE5B,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,yBAAyB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,MAAM,GAAG,kBAAkB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAElD,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QACnC,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,eAAe,EAAE,CAAC;YACrD,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,OAAO;YACL,GAAG,OAAO;YACV,OAAO,EAAE;gBACP,GAAG,OAAO,CAAC,OAAO;gBAClB,oBAAoB,EAAE,QAAQ,CAAC,kBAAkB;aAClD;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,yBAAyB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACpD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-smart-router",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Auto-model router middleware for the pi.dev coding agent",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -110,7 +110,8 @@
110
110
  }
111
111
  },
112
112
  "devDependencies": {
113
- "@earendil-works/pi-coding-agent": "^0.85.1",
113
+ "@earendil-works/pi-ai": "^1.3.0",
114
+ "@earendil-works/pi-coding-agent": "^1.3.0",
114
115
  "@types/better-sqlite3": "^7.6.13",
115
116
  "@types/node": "^22.15.21",
116
117
  "@typescript-eslint/eslint-plugin": "^8.62.1",
@@ -124,7 +125,6 @@
124
125
  "vitest": "^3.2.3"
125
126
  },
126
127
  "dependencies": {
127
- "@earendil-works/pi-ai": "^0.85.1",
128
128
  "@huggingface/transformers": "^4.2.0",
129
129
  "better-sqlite3": "^12.11.1",
130
130
  "yaml": "^2.9.0",
@@ -33,6 +33,13 @@ export type CapabilitySource = 'benchmark' | 'pattern_default';
33
33
  /** ModelProfile plus operator-visible capability provenance (SP-174). */
34
34
  export interface MappedModelProfile extends ModelProfile {
35
35
  readonly capability_source: CapabilitySource;
36
+ /**
37
+ * Context limits carried from the pi model registry (`Model.contextWindow` /
38
+ * `Model.maxTokens`) — SP-299 / #177. Kept separate from `limits` (YAML
39
+ * overrides) so `resolveLimits` can rank them below LiteLLM catalog data
40
+ * and above tier defaults.
41
+ */
42
+ readonly pi_registry_limits?: ModelLimits;
36
43
  }
37
44
 
38
45
  const benchmarkCapabilitiesSchema = z.object({
@@ -181,7 +188,7 @@ function withBenchmarkCapabilities(
181
188
  };
182
189
  }
183
190
 
184
- /** Pi registry `Model.cost` shape — per-token USD rates. */
191
+ /** Pi registry `Model.cost` shape — per-1M-token USD rates (SP-298). */
185
192
  export interface PiRegistryCost {
186
193
  readonly input: number;
187
194
  readonly output: number;
@@ -194,6 +201,10 @@ export interface PiModelInput {
194
201
  readonly id: string;
195
202
  readonly name?: string;
196
203
  readonly cost?: PiRegistryCost;
204
+ /** Pi registry `Model.contextWindow` — total input token window (SP-299 / #177). */
205
+ readonly contextWindow?: number;
206
+ /** Pi registry `Model.maxTokens` — max output tokens (SP-299 / #177). */
207
+ readonly maxTokens?: number;
197
208
  }
198
209
 
199
210
  interface ModelPatternRule {
@@ -355,19 +366,24 @@ function matchPatternRules(id: string): ModelFamilyDefaults | undefined {
355
366
  }
356
367
 
357
368
  /**
358
- * Registry cost → USD per 1M tokens: average of input and output rates scaled to 1M.
359
- * Matches `computeCostPer1MTokens` in litellm-fetch (SP-045/SP-046).
360
- * Returns undefined when both input and output rates are zero (use pattern default).
369
+ * Registry cost → USD per 1M tokens: average of input and output rates.
370
+ * Pi registry `Model.cost` rates are already denominated per 1M tokens
371
+ * (SP-298), so no scaling is applied — unlike LiteLLM per-token rates handled
372
+ * by `computeCostPer1MTokens` in litellm-fetch (SP-045/SP-046).
373
+ * Returns undefined when both input and output rates are zero (use pattern
374
+ * default), or when either rate is negative — negative values are vendor
375
+ * unknown/sentinel rates, not valid pricing, so cost stays unknown (SP-298).
361
376
  */
362
377
  function deriveFallbackCostPer1M(cost: PiRegistryCost): number | undefined {
363
378
  const { input, output } = cost;
364
379
  if (input === 0 && output === 0) {
365
380
  return undefined;
366
381
  }
382
+ if (input < 0 || output < 0) {
383
+ return undefined;
384
+ }
367
385
 
368
- const inputPer1M = input * 1_000_000;
369
- const outputPer1M = output * 1_000_000;
370
- return (inputPer1M + outputPer1M) / 2;
386
+ return (input + output) / 2;
371
387
  }
372
388
 
373
389
  function resolveFallbackCost(
@@ -382,6 +398,23 @@ function resolveFallbackCost(
382
398
  return fromRegistry ?? defaults.pricing.fallback_cost_per_1m;
383
399
  }
384
400
 
401
+ /**
402
+ * Pi registry contextWindow/maxTokens → ModelLimits (SP-299 / #177).
403
+ * Non-positive or non-finite registry values are vendor sentinels, not real
404
+ * limits — ignored so tier defaults still apply.
405
+ */
406
+ function derivePiRegistryLimits(input: PiModelInput): ModelLimits | undefined {
407
+ const limits: { max_input_tokens?: number; max_output_tokens?: number } = {};
408
+ const { contextWindow, maxTokens } = input;
409
+ if (contextWindow !== undefined && Number.isFinite(contextWindow) && contextWindow > 0) {
410
+ limits.max_input_tokens = Math.floor(contextWindow);
411
+ }
412
+ if (maxTokens !== undefined && Number.isFinite(maxTokens) && maxTokens > 0) {
413
+ limits.max_output_tokens = Math.floor(maxTokens);
414
+ }
415
+ return Object.keys(limits).length > 0 ? limits : undefined;
416
+ }
417
+
385
418
  function buildProfile(
386
419
  input: PiModelInput,
387
420
  defaults: ModelFamilyDefaults,
@@ -410,11 +443,17 @@ function buildProfile(
410
443
  ? { ...profile, performance: { ...defaults.performance } }
411
444
  : profile;
412
445
 
446
+ const piRegistryLimits = derivePiRegistryLimits(input);
447
+ const withLimits =
448
+ piRegistryLimits !== undefined
449
+ ? { ...withPerformance, pi_registry_limits: piRegistryLimits }
450
+ : withPerformance;
451
+
413
452
  if (defaults.endpoint !== undefined) {
414
- return { ...withPerformance, endpoint: defaults.endpoint };
453
+ return { ...withLimits, endpoint: defaults.endpoint };
415
454
  }
416
455
 
417
- return withPerformance;
456
+ return withLimits;
418
457
  }
419
458
 
420
459
  /**
@@ -85,6 +85,30 @@ export function isGoogleOriginAssistantMessage(message: AssistantMessage): boole
85
85
  return provider === 'cursor' && /gemini/i.test(message.model);
86
86
  }
87
87
 
88
+ const CURSOR_PROVIDER = 'cursor' as const;
89
+
90
+ /**
91
+ * Env gate for the Cursor lifecycle strip (SP-300, #180). Default on;
92
+ * `SMART_ROUTER_STRIP_CURSOR_LIFECYCLE=0` disables the sanitizer.
93
+ */
94
+ export const STRIP_CURSOR_LIFECYCLE_ENV = 'SMART_ROUTER_STRIP_CURSOR_LIFECYCLE' as const;
95
+
96
+ /** Whether the delegation target is a Cursor model (cursor/auto, composer-*). */
97
+ export function isCursorDelegationTarget<TApi extends Api>(model: Model<TApi>): boolean {
98
+ return model.provider.trim().toLowerCase() === CURSOR_PROVIDER;
99
+ }
100
+
101
+ /**
102
+ * Whether an assistant message was produced by the Cursor provider (e.g.
103
+ * `cursor/auto`). Cursor-origin thinking blocks carry pi-cursor-sdk lifecycle
104
+ * progress lines (`Cursor shell: …`, `Cursor MCP: …`) rather than model
105
+ * reasoning — replaying them cross-provider teaches the target to imitate
106
+ * shell commands as prose (issue #180).
107
+ */
108
+ export function isCursorOriginAssistantMessage(message: AssistantMessage): boolean {
109
+ return message.provider.trim().toLowerCase() === CURSOR_PROVIDER;
110
+ }
111
+
88
112
  export function hasReplaySensitiveState(messages: readonly Message[]): boolean {
89
113
  for (const message of messages) {
90
114
  if (message.role !== 'assistant') {
@@ -236,3 +260,102 @@ export function repairGeminiReplayContext<TApi extends Api>(
236
260
 
237
261
  return { ...context, messages };
238
262
  }
263
+
264
+ export interface StripCursorLifecycleOptions {
265
+ /** Env override for tests; defaults to `process.env`. */
266
+ readonly env?: Record<string, string | undefined>;
267
+ }
268
+
269
+ export interface CursorLifecycleStripResult {
270
+ readonly context: Context;
271
+ /** Thinking blocks removed from Cursor-origin assistant messages. */
272
+ readonly strippedThinkingBlocks: number;
273
+ /** Total characters of removed thinking text (observability for #180). */
274
+ readonly strippedThinkingChars: number;
275
+ }
276
+
277
+ /**
278
+ * SP-300 (#180): drop Cursor lifecycle thinking from Cursor-origin assistant
279
+ * messages before delegating to a non-Cursor target.
280
+ *
281
+ * The third-party pi-cursor-sdk provider emits tool progress as thinking lines
282
+ * (`Cursor shell: …`). pi-ai's transformMessages converts foreign thinking
283
+ * blocks to plain text when the provider/model differs, so a Cursor-origin
284
+ * history handed to GLM/Gemini replays those progress lines as assistant
285
+ * prose — the target then imitates shell commands as text instead of emitting
286
+ * real toolCalls (2026-10-01 glm-5.3-flash runaway).
287
+ *
288
+ * Scope guardrails:
289
+ * - Cursor targets are returned unchanged (Cursor→Cursor replay is native).
290
+ * - Non-Cursor thinking (Gemini, Anthropic, …) is never touched.
291
+ * - Text, toolCall, and other blocks are preserved; messages are never
292
+ * dropped — a message left with an empty content array is kept so
293
+ * tool-call/result pairing stays intact.
294
+ * - Disabled via `SMART_ROUTER_STRIP_CURSOR_LIFECYCLE=0`.
295
+ *
296
+ * Call after {@link normalizeDelegationContext} so virtual-tagged messages
297
+ * already resolved to a Cursor session execution are caught as Cursor-origin.
298
+ */
299
+ export function stripCursorLifecycleThinking<TApi extends Api>(
300
+ context: Context,
301
+ targetModel: Model<TApi>,
302
+ options?: StripCursorLifecycleOptions,
303
+ ): CursorLifecycleStripResult {
304
+ const unchanged: CursorLifecycleStripResult = {
305
+ context,
306
+ strippedThinkingBlocks: 0,
307
+ strippedThinkingChars: 0,
308
+ };
309
+
310
+ const env = options?.env ?? process.env;
311
+ if (env[STRIP_CURSOR_LIFECYCLE_ENV] === '0') {
312
+ return unchanged;
313
+ }
314
+
315
+ if (isCursorDelegationTarget(targetModel)) {
316
+ return unchanged;
317
+ }
318
+
319
+ let strippedThinkingBlocks = 0;
320
+ let strippedThinkingChars = 0;
321
+ let changed = false;
322
+
323
+ const messages: Message[] = [];
324
+ for (const message of context.messages) {
325
+ if (message.role !== 'assistant' || !isCursorOriginAssistantMessage(message)) {
326
+ messages.push(message);
327
+ continue;
328
+ }
329
+
330
+ const kept: typeof message.content = [];
331
+ for (const block of message.content) {
332
+ if (block.type === 'thinking') {
333
+ strippedThinkingBlocks += 1;
334
+ strippedThinkingChars += block.thinking.length;
335
+ changed = true;
336
+ continue;
337
+ }
338
+ kept.push(block);
339
+ }
340
+
341
+ if (kept.length === message.content.length) {
342
+ messages.push(message);
343
+ continue;
344
+ }
345
+
346
+ // Never drop messages: keep the message with its remaining blocks (an
347
+ // empty content array is fine) so user/toolResult turn pairings stay
348
+ // intact in the delegated history.
349
+ messages.push({ ...message, content: kept });
350
+ }
351
+
352
+ if (!changed) {
353
+ return unchanged;
354
+ }
355
+
356
+ return {
357
+ context: { ...context, messages },
358
+ strippedThinkingBlocks,
359
+ strippedThinkingChars,
360
+ };
361
+ }
@@ -8,18 +8,7 @@
8
8
 
9
9
  import type { Message, RoutingRequest } from '../types/index.js';
10
10
  import type { TriageResult } from '../triage/triage-engine.js';
11
-
12
- const FAILURE_PATTERNS = [
13
- 'error',
14
- 'fail',
15
- 'exception',
16
- 'timed out',
17
- 'timeout',
18
- 'econnrefused',
19
- 'enotfound',
20
- 'econnreset',
21
- 'epipe',
22
- ] as const;
11
+ import { isToolFailure } from '../pinning/tool-failure.js';
23
12
 
24
13
  function resolveMessageCount(request: RoutingRequest): number {
25
14
  return request.messages?.length ?? 0;
@@ -45,14 +34,10 @@ function resolveCompactionFlag(request: RoutingRequest): 0 | 1 {
45
34
  return request.compaction_flag ? 1 : 0;
46
35
  }
47
36
 
48
- function looksLikeToolFailure(content: string): boolean {
49
- const lower = content.toLowerCase();
50
- return FAILURE_PATTERNS.some((pattern) => lower.includes(pattern));
51
- }
52
-
53
37
  /**
54
38
  * Observational loop-pressure flag: 1 when the latest tool message looks
55
- * like a failure. Mirrors loop-escalation heuristics without session pin state.
39
+ * like a failure. Uses the shared loop-escalation failure detection
40
+ * (`is_error` → `status` → tightened body heuristic; SP-303 / #186).
56
41
  */
57
42
  function resolveLoopPressure(request: RoutingRequest): 0 | 1 {
58
43
  const messages = request.messages;
@@ -63,7 +48,7 @@ function resolveLoopPressure(request: RoutingRequest): 0 | 1 {
63
48
  for (let i = messages.length - 1; i >= 0; i--) {
64
49
  const message = messages[i]!;
65
50
  if (message.role === 'tool') {
66
- return looksLikeToolFailure(message.content) ? 1 : 0;
51
+ return isToolFailure(message) ? 1 : 0;
67
52
  }
68
53
  }
69
54
 
@@ -22,6 +22,7 @@
22
22
  */
23
23
 
24
24
  import type { Message, ModelProfile, RoutingRequest, SessionPin } from '../types/index.js';
25
+ import { isToolFailure } from './tool-failure.js';
25
26
 
26
27
  // ─── Types ────────────────────────────────────────────────────────────────────
27
28
 
@@ -46,32 +47,6 @@ export const ZERO_TIER_TOOL_CHURN_SIGNATURE = 'zt:tool_churn' as const;
46
47
 
47
48
  // ─── Failure signature extraction ─────────────────────────────────────────────
48
49
 
49
- const FAILURE_PATTERNS = [
50
- 'error',
51
- 'fail',
52
- 'exception',
53
- 'timed out',
54
- 'timeout',
55
- 'econnrefused',
56
- 'enotfound',
57
- 'econnreset',
58
- 'epipe',
59
- ] as const;
60
-
61
- /** Rate-limit and quota failures whose body text may omit generic error keywords. */
62
- const RATE_LIMIT_PATTERNS = [
63
- 'rate limit',
64
- '429',
65
- 'too many requests',
66
- 'quota exceeded',
67
- ] as const;
68
-
69
- /** Auth/access failures that are not rate limits but still indicate tool failure. */
70
- const AUTH_DENIED_PATTERNS = [
71
- 'permission denied',
72
- 'access denied',
73
- ] as const;
74
-
75
50
  /** Host/agent signals that the model invoked a tool the runtime does not expose. */
76
51
  const UNSUPPORTED_TOOL_PATTERNS = [
77
52
  'unknown tool',
@@ -125,32 +100,6 @@ function lastToolMessage(request: RoutingRequest): Message | null {
125
100
  return null;
126
101
  }
127
102
 
128
- /**
129
- * Decide whether a tool-result message represents a failure.
130
- * Prefers structured host signals (`is_error` / `status`); falls back to a
131
- * tightened body heuristic only when no structured signal is present.
132
- */
133
- function isToolFailure(msg: Message): boolean {
134
- if (msg.is_error === true) return true;
135
- if (msg.is_error === false) return false;
136
- if (msg.status !== undefined) {
137
- return msg.status >= 400;
138
- }
139
- return looksLikeFailure(msg.content ?? '');
140
- }
141
-
142
- function looksLikeFailure(content: string): boolean {
143
- const lower = content.toLowerCase();
144
- // Benign negations must not be counted as failures.
145
- if (/\bno (error|errors|failure|failures)\b/.test(lower)) return false;
146
- if (/\bwithout (an? )?(error|failure)\b/.test(lower)) return false;
147
- return (
148
- FAILURE_PATTERNS.some((p) => lower.includes(p)) ||
149
- RATE_LIMIT_PATTERNS.some((p) => lower.includes(p)) ||
150
- AUTH_DENIED_PATTERNS.some((p) => lower.includes(p))
151
- );
152
- }
153
-
154
103
  /** Deterministic djb2 hash of normalised error content. */
155
104
  function computeSignature(content: string): string {
156
105
  const normalized = content.trim().slice(0, 256).toLowerCase();
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Shared tool-failure detection — SP-303, GitHub #186.
3
+ *
4
+ * Single source of truth for deciding whether a tool-result message
5
+ * represents a failure. Used by loop escalation (pin break) and the
6
+ * HyDRA `loop:` metadata flag.
7
+ *
8
+ * Precedence: structured host signals (`is_error`, then `status`) win;
9
+ * a tightened body heuristic applies only when no structured signal
10
+ * is present. Benign negations ("no errors", "without failure") never
11
+ * count as failures.
12
+ */
13
+
14
+ import type { Message } from '../types/index.js';
15
+
16
+ const FAILURE_PATTERNS = [
17
+ 'error',
18
+ 'fail',
19
+ 'exception',
20
+ 'timed out',
21
+ 'timeout',
22
+ 'econnrefused',
23
+ 'enotfound',
24
+ 'econnreset',
25
+ 'epipe',
26
+ ] as const;
27
+
28
+ /** Rate-limit and quota failures whose body text may omit generic error keywords. */
29
+ const RATE_LIMIT_PATTERNS = [
30
+ 'rate limit',
31
+ '429',
32
+ 'too many requests',
33
+ 'quota exceeded',
34
+ ] as const;
35
+
36
+ /** Auth/access failures that are not rate limits but still indicate tool failure. */
37
+ const AUTH_DENIED_PATTERNS = [
38
+ 'permission denied',
39
+ 'access denied',
40
+ ] as const;
41
+
42
+ /**
43
+ * Decide whether a tool-result message represents a failure.
44
+ * Prefers structured host signals (`is_error` / `status`); falls back to a
45
+ * tightened body heuristic only when no structured signal is present.
46
+ */
47
+ export function isToolFailure(msg: Message): boolean {
48
+ if (msg.is_error === true) return true;
49
+ if (msg.is_error === false) return false;
50
+ if (msg.status !== undefined) {
51
+ return msg.status >= 400;
52
+ }
53
+ return looksLikeFailure(msg.content ?? '');
54
+ }
55
+
56
+ function looksLikeFailure(content: string): boolean {
57
+ const lower = content.toLowerCase();
58
+ // Benign negations must not be counted as failures.
59
+ if (/\bno (error|errors|failure|failures)\b/.test(lower)) return false;
60
+ if (/\bwithout (an? )?(error|failure)\b/.test(lower)) return false;
61
+ return (
62
+ FAILURE_PATTERNS.some((p) => lower.includes(p)) ||
63
+ RATE_LIMIT_PATTERNS.some((p) => lower.includes(p)) ||
64
+ AUTH_DENIED_PATTERNS.some((p) => lower.includes(p))
65
+ );
66
+ }
package/src/index.ts CHANGED
@@ -190,8 +190,14 @@ export {
190
190
  } from './domain/delegation/adaptive-reasoning.js';
191
191
  export {
192
192
  isGoogleDelegationTarget,
193
+ isCursorDelegationTarget,
194
+ isCursorOriginAssistantMessage,
193
195
  normalizeDelegationContext,
194
196
  repairGeminiReplayContext,
197
+ stripCursorLifecycleThinking,
198
+ STRIP_CURSOR_LIFECYCLE_ENV,
199
+ type CursorLifecycleStripResult,
200
+ type StripCursorLifecycleOptions,
195
201
  } from './domain/delegation/delegation-context.js';
196
202
  export { ExecutionLedger } from './domain/delegation/execution-ledger.js';
197
203
  export {
@@ -48,7 +48,7 @@ import { EMBEDDING_DIM } from '../../domain/matching/embedding-provider.js';
48
48
 
49
49
  // ─── Schema version & migrations ────────────────────────────────────────────
50
50
 
51
- const CURRENT_SCHEMA_VERSION = 8;
51
+ const CURRENT_SCHEMA_VERSION = 9;
52
52
 
53
53
  const MIGRATION_V1 = `
54
54
  CREATE TABLE IF NOT EXISTS pins (
@@ -203,6 +203,18 @@ const MIGRATION_V8 = `
203
203
  ALTER TABLE dataset ADD COLUMN embedding_json TEXT;
204
204
  `;
205
205
 
206
+ // SP-301 / #171: persist context-fit gate telemetry so router-explain and
207
+ // exports can surface why a request overflowed after restart. Previously the
208
+ // fields were dropped on insert and read back as null literals.
209
+ const MIGRATION_V9 = `
210
+ ALTER TABLE telemetry ADD COLUMN estimated_input_tokens INTEGER;
211
+ ALTER TABLE telemetry ADD COLUMN context_fit_viable_count INTEGER;
212
+ ALTER TABLE telemetry ADD COLUMN context_fit_rejected_json TEXT;
213
+ ALTER TABLE telemetry ADD COLUMN context_overflow_pin_break INTEGER NOT NULL DEFAULT 0;
214
+ ALTER TABLE telemetry ADD COLUMN selected_model_max_input_tokens INTEGER;
215
+ ALTER TABLE telemetry ADD COLUMN context_fit_reason_code TEXT;
216
+ `;
217
+
206
218
  // ─── Token bucket result ────────────────────────────────────────────────────
207
219
 
208
220
  export interface TokenBucketResult {
@@ -708,14 +720,20 @@ export class SqliteStore implements StorePort {
708
720
  estimated_cost_usd, routing_latency_ms, pin_reason,
709
721
  actual_cost_usd, actual_input_tokens, actual_output_tokens,
710
722
  actual_cache_read_tokens, actual_cache_write_tokens,
711
- reasoning_level_requested, reasoning_level_applied, reasoning_reason_code
723
+ reasoning_level_requested, reasoning_level_applied, reasoning_reason_code,
724
+ estimated_input_tokens, context_fit_viable_count,
725
+ context_fit_rejected_json, context_overflow_pin_break,
726
+ selected_model_max_input_tokens, context_fit_reason_code
712
727
  ) VALUES (
713
728
  @timestamp, @session_id, @request_id, @turn_type,
714
729
  @stage, @reason_code, @selected_model_id,
715
730
  @estimated_cost_usd, @routing_latency_ms, @pin_reason,
716
731
  @actual_cost_usd, @actual_input_tokens, @actual_output_tokens,
717
732
  @actual_cache_read_tokens, @actual_cache_write_tokens,
718
- @reasoning_level_requested, @reasoning_level_applied, @reasoning_reason_code
733
+ @reasoning_level_requested, @reasoning_level_applied, @reasoning_reason_code,
734
+ @estimated_input_tokens, @context_fit_viable_count,
735
+ @context_fit_rejected_json, @context_overflow_pin_break,
736
+ @selected_model_max_input_tokens, @context_fit_reason_code
719
737
  )`,
720
738
  )
721
739
  .run({
@@ -737,6 +755,12 @@ export class SqliteStore implements StorePort {
737
755
  reasoning_level_requested: entry.reasoning_level_requested ?? null,
738
756
  reasoning_level_applied: entry.reasoning_level_applied ?? null,
739
757
  reasoning_reason_code: entry.reasoning_reason_code ?? null,
758
+ estimated_input_tokens: entry.estimated_input_tokens ?? null,
759
+ context_fit_viable_count: entry.context_fit_viable_count ?? null,
760
+ context_fit_rejected_json: entry.context_fit_rejected_json ?? null,
761
+ context_overflow_pin_break: entry.context_overflow_pin_break ? 1 : 0,
762
+ selected_model_max_input_tokens: entry.selected_model_max_input_tokens ?? null,
763
+ context_fit_reason_code: entry.context_fit_reason_code ?? null,
740
764
  });
741
765
  }
742
766
 
@@ -935,6 +959,12 @@ export class SqliteStore implements StorePort {
935
959
  if (version < 8) {
936
960
  this.db.exec(MIGRATION_V8);
937
961
  version = 8;
962
+ this.db.pragma(`user_version = ${version}`);
963
+ }
964
+
965
+ if (version < 9) {
966
+ this.db.exec(MIGRATION_V9);
967
+ version = 9;
938
968
  this.db.pragma(`user_version = ${CURRENT_SCHEMA_VERSION}`);
939
969
  }
940
970
  }
@@ -1033,6 +1063,12 @@ interface TelemetryRow {
1033
1063
  reasoning_level_requested: string | null;
1034
1064
  reasoning_level_applied: string | null;
1035
1065
  reasoning_reason_code: string | null;
1066
+ estimated_input_tokens: number | null;
1067
+ context_fit_viable_count: number | null;
1068
+ context_fit_rejected_json: string | null;
1069
+ context_overflow_pin_break: number;
1070
+ selected_model_max_input_tokens: number | null;
1071
+ context_fit_reason_code: string | null;
1036
1072
  }
1037
1073
 
1038
1074
  interface DatasetRow {
@@ -1142,12 +1178,12 @@ function telemetryRowToEntity(row: TelemetryRow): RoutingTelemetry {
1142
1178
  estimated_cost_usd: row.estimated_cost_usd,
1143
1179
  routing_latency_ms: row.routing_latency_ms,
1144
1180
  pin_reason: row.pin_reason,
1145
- estimated_input_tokens: null,
1146
- context_fit_viable_count: null,
1147
- context_fit_rejected_json: null,
1148
- context_overflow_pin_break: false,
1149
- selected_model_max_input_tokens: null,
1150
- context_fit_reason_code: null,
1181
+ estimated_input_tokens: row.estimated_input_tokens,
1182
+ context_fit_viable_count: row.context_fit_viable_count,
1183
+ context_fit_rejected_json: row.context_fit_rejected_json,
1184
+ context_overflow_pin_break: row.context_overflow_pin_break === 1,
1185
+ selected_model_max_input_tokens: row.selected_model_max_input_tokens,
1186
+ context_fit_reason_code: row.context_fit_reason_code,
1151
1187
  cluster_id: null,
1152
1188
  cluster_similarity: null,
1153
1189
  cluster_margin: null,
@@ -11,10 +11,14 @@
11
11
  * to finish the domain→infra inversion; this module re-exports them for
12
12
  * import-path stability. What remains here is limit resolution and
13
13
  * fleet-mapping, which depend on `config/pi-model-mapper` tier defaults.
14
+ *
15
+ * SP-299 (#177): limit resolution is a four-level cascade — YAML profile
16
+ * override → LiteLLM registry snapshot → pi registry (`contextWindow` /
17
+ * `maxTokens` carried on the mapped profile) → tier default.
14
18
  */
15
19
 
16
20
  import type { ModelProfile, ModelLimits, PriceCatalog } from '../../domain/types/index.js';
17
- import { getDefaultLimitsForTier } from '../../config/pi-model-mapper.js';
21
+ import { getDefaultLimitsForTier, type MappedModelProfile } from '../../config/pi-model-mapper.js';
18
22
 
19
23
  export {
20
24
  resolvePrice,
@@ -44,10 +48,19 @@ function lookupRegistryLimits(
44
48
  return snapshot[registryKey] ?? snapshot[model.id];
45
49
  }
46
50
 
51
+ /**
52
+ * Pi registry limits attached by `mapPiModelToProfile` (SP-299 / #177).
53
+ * Profiles built from fleet YAML never carry this field.
54
+ */
55
+ function lookupPiRegistryLimits(model: ModelProfile): ModelLimits | undefined {
56
+ return (model as MappedModelProfile).pi_registry_limits;
57
+ }
58
+
47
59
  /**
48
60
  * Resolve effective context limits for a model.
49
61
  *
50
- * Priority: YAML/profile override → LiteLLM registry snapshot → tier default.
62
+ * Priority: YAML/profile override → LiteLLM registry snapshot → pi registry
63
+ * (`contextWindow`/`maxTokens`) → tier default (SP-299 / #177).
51
64
  */
52
65
  export function resolveLimits(
53
66
  model: ModelProfile,
@@ -55,6 +68,7 @@ export function resolveLimits(
55
68
  ): ResolvedLimits {
56
69
  const yamlLimits = model.limits;
57
70
  const registryLimits = catalog ? lookupRegistryLimits(model, catalog) : undefined;
71
+ const piRegistryLimits = lookupPiRegistryLimits(model);
58
72
  const defaults = getDefaultLimitsForTier(model.tier);
59
73
 
60
74
  return {
@@ -63,10 +77,12 @@ export function resolveLimits(
63
77
  max_input_tokens:
64
78
  yamlLimits?.max_input_tokens ??
65
79
  registryLimits?.max_input_tokens ??
80
+ piRegistryLimits?.max_input_tokens ??
66
81
  defaults.max_input_tokens,
67
82
  max_output_tokens:
68
83
  yamlLimits?.max_output_tokens ??
69
84
  registryLimits?.max_output_tokens ??
85
+ piRegistryLimits?.max_output_tokens ??
70
86
  defaults.max_output_tokens,
71
87
  },
72
88
  };