@north-light/crouter 0.3.273 → 0.3.274

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.
@@ -9,6 +9,7 @@
9
9
  import { test } from 'node:test';
10
10
  import assert from 'node:assert/strict';
11
11
  import { classify } from '../fault-classifier.js';
12
+ import { __classifyProviderErrorForTest } from '../../builtin-pi-packages/pi-crtr-extensions/extensions/provider-rotation.js';
12
13
  test('invalid_grant text classifies as fatal auth (issue #94)', () => {
13
14
  const c = classify('pi→provider', 'Anthropic token refresh failed: invalid_grant');
14
15
  assert.equal(c.kind, 'auth');
@@ -89,3 +90,49 @@ test('"terminated" matches as a whole word only (no false positive on substrings
89
90
  // guard the word-boundary choice explicitly.
90
91
  assert.notEqual(classify('pi→provider', 'preterminated').kind, 'connection');
91
92
  });
93
+ test('shared recognition preserves each existing policy eligibility, except bare quota', () => {
94
+ const assertPair = (raw, faultKind, rotationKind, observed = {}) => {
95
+ assert.equal(classify('pi→provider', raw).kind, faultKind, `fault: ${JSON.stringify(raw)}`);
96
+ const error = typeof raw === 'string' ? { message: raw } : raw;
97
+ assert.equal(__classifyProviderErrorForTest(error, observed).kind, rotationKind, `rotation: ${JSON.stringify(raw)}`);
98
+ };
99
+ for (const [status, faultKind, rotationKind] of [
100
+ [401, 'auth', 'fatal'], [403, 'auth', 'fatal'], [429, 'rate-limit', 'rate_limit'],
101
+ [500, 'overloaded', 'transient'], [502, 'overloaded', 'transient'], [503, 'overloaded', 'transient'], [504, 'overloaded', 'transient'],
102
+ [529, 'overloaded', 'fatal'], [501, 'overloaded', 'fatal'], [400, 'protocol', 'fatal'], [413, 'protocol', 'fatal'],
103
+ ])
104
+ assertPair({ status }, faultKind, rotationKind);
105
+ assertPair({ status: 503 }, 'overloaded', 'rate_limit', { retryAfterMs: 1 });
106
+ for (const code of ['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'ENOENT', 'ENOTFOUND', 'EAI_AGAIN', 'EPIPE', 'EHOSTUNREACH', 'ENETUNREACH']) {
107
+ assertPair({ code }, 'connection', 'fatal');
108
+ }
109
+ assertPair({ code: 'ELOCKED' }, 'overloaded', 'fatal');
110
+ assertPair({ code: 1008 }, 'protocol', 'fatal');
111
+ assertPair({ code: 1006 }, 'connection', 'fatal');
112
+ assertPair({ code: 1011, reason: 'no running broker' }, 'connection', 'fatal');
113
+ assertPair({ code: 1011, reason: 'unrelated failure' }, 'other', 'fatal');
114
+ for (const text of ['rate limit', '429', 'too many requests', 'usage limit', 'usage_not_included', 'usagelimiterror', 'insufficient_quota', 'quota exceeded']) {
115
+ assertPair(text, 'rate-limit', 'rate_limit');
116
+ }
117
+ assertPair('quota', 'other', 'fatal');
118
+ assertPair('ordinary usage discussion', 'other', 'fatal');
119
+ for (const text of ['connection', 'network', 'fetch failed', 'econnreset', 'econnrefused', 'enotfound', 'etimedout', 'timed out', 'timeout', 'WebSocket closed', 'WebSocket stream closed before response.completed', 'WebSocket error', 'WebSocket interruption', 'socket hang', 'overloaded', 'temporarily unavailable']) {
120
+ const faultKind = text === 'overloaded' || text === 'temporarily unavailable' ? 'overloaded' : 'connection';
121
+ assertPair(text, faultKind, 'transient');
122
+ }
123
+ for (const text of ['socket', 'service unavailable'])
124
+ assertPair(text, 'other', 'transient');
125
+ for (const text of ['network error: rate limit exceeded', 'service unavailable: too many requests'])
126
+ assertPair(text, 'rate-limit', 'transient');
127
+ for (const text of ['413 Payload Too Large', 'invalid_grant', 'context overflow recovery failed']) {
128
+ const faultKind = text.includes('413') ? 'protocol' : text.includes('invalid_grant') ? 'auth' : 'context-overflow';
129
+ assertPair(text, faultKind, 'fatal');
130
+ }
131
+ assertPair('WebSocket closed: 1008 policy violation', 'protocol', 'transient');
132
+ for (const text of ['lock file is already being held', 'Internal server error', 'APIError', '500', 'capacity', 'server busy']) {
133
+ assertPair(text, 'overloaded', 'fatal');
134
+ }
135
+ for (const text of ['EPIPE', 'EAI_AGAIN', 'EHOSTUNREACH', 'ENETUNREACH', 'terminated', 'other side closed', 'premature close']) {
136
+ assertPair(text, 'connection', 'fatal');
137
+ }
138
+ });
@@ -25,19 +25,4 @@ export interface FaultClassification {
25
25
  disposition: FaultRetryDisposition;
26
26
  }
27
27
  export declare function errorClassFromFault(fault: Pick<Fault, 'kind' | 'retry'>): ErrorClass;
28
- export interface FaultErrnoLike {
29
- code?: unknown;
30
- errno?: unknown;
31
- message?: unknown;
32
- }
33
- export interface FaultHttpLike {
34
- status?: unknown;
35
- statusText?: unknown;
36
- ok?: unknown;
37
- message?: unknown;
38
- }
39
- export interface FaultWsCloseLike {
40
- code?: unknown;
41
- reason?: unknown;
42
- }
43
28
  export declare function classify(link: FaultLink, raw: unknown): FaultClassification;
@@ -1,4 +1,5 @@
1
1
  import { hasManagedProviderCoolingDiagnostic } from './runtime/managed-provider-cooling.js';
2
+ import { recognizeProviderError } from './provider-error-recognition.js';
2
3
  import { isRecord } from '../shared/predicates.js';
3
4
  const errorClassByFaultKind = {
4
5
  'rate-limit': 'rate_limit',
@@ -17,85 +18,21 @@ export function errorClassFromFault(fault) {
17
18
  retry_disposition: fault.retry.disposition,
18
19
  };
19
20
  }
20
- function text(value) {
21
- return typeof value === 'string' ? value : '';
22
- }
23
- function classifyProviderText(message) {
24
- const m = message ?? '';
25
- // ORDER MATTERS: rate-limit → auth/protocol → overloaded → connection → other.
26
- // Auth must win over a generic API-error wrapper, and a policy-violation close
27
- // must not become a retryable transport interruption.
28
- if (/rate.?limit|\b429\b|too many requests|quota/i.test(m))
29
- return 'rate-limit';
30
- // A body the other side refuses for SIZE is the request itself being
31
- // unacceptable, never a transient server condition: the retry re-posts the
32
- // same conversation and fails identically, forever. This must precede the
33
- // overloaded rule, which otherwise claims a size rejection wrapped in 5xx
34
- // prose ("500 ... request entity too large") and arms an unwinnable retry.
35
- if (/\b413\b|payload too large|entity too large|request too large|entity\.too\.large|body exceeds/i.test(m))
36
- return 'protocol';
37
- if (/invalid_grant|refresh token|unauthori[sz]ed|invalid.?api.?key|authentication failed|\b401\b|\b403\b/i.test(m))
38
- return 'auth';
39
- // pi compacts and retries a raw provider overflow. Its failed-recovery result
40
- // is the point at which the session cannot become viable without a new launch.
41
- if (/context overflow recovery failed/i.test(m))
42
- return 'context-overflow';
43
- if (/websocket.*(?:closed.*\b(?:1002|1003|1007|1008|1009|1010)\b|protocol error|policy violation|unsupported data|invalid (?:frame|payload)|message too big|mandatory extension)/i.test(m))
44
- return 'protocol';
45
- // proper-lockfile contention on a credential file: another process holds the
46
- // mutex right now, so the same request wins once it releases.
47
- if (/lock file is already being held/i.test(m))
48
- return 'overloaded';
49
- if (/overloaded|\b5\d\d\b|internal server error|api.?error|capacity|server.{0,3}busy|temporarily unavailable/i.test(m))
50
- return 'overloaded';
51
- if (/connection|econnreset|etimedout|enotfound|econnrefused|network|fetch failed|socket hang|timed? out|timeout|\bterminated\b|other side closed|premature close|websocket\s+(?:was\s+)?closed|websocket stream closed before response\.completed|websocket\s+error|websocket.*interrupt/i.test(m))
52
- return 'connection';
53
- return 'other';
54
- }
55
- function classifyErrno(raw) {
56
- const code = text(raw.code) || text(raw.errno);
57
- if (code === '')
58
- return null;
59
- if (/^(ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENOENT|ENOTFOUND|EAI_AGAIN|EPIPE|EHOSTUNREACH|ENETUNREACH)$/i.test(code)) {
60
- return 'connection';
61
- }
62
- // A held filesystem mutex, not a broken link: back off on time rather than
63
- // behind the online probe a connection fault waits on.
64
- if (/^ELOCKED$/i.test(code))
65
- return 'overloaded';
66
- return null;
67
- }
68
- function classifyHttp(raw) {
69
- const status = typeof raw.status === 'number' ? raw.status : null;
70
- if (status === null)
71
- return null;
72
- if (status === 401 || status === 403)
73
- return 'auth';
74
- if (status === 429)
75
- return 'rate-limit';
76
- if (status === 503 || status === 529)
77
- return 'overloaded';
78
- if (status >= 500 && status <= 599)
79
- return 'overloaded';
80
- if (status >= 400 && status <= 499)
81
- return 'protocol';
82
- return null;
83
- }
84
- function classifyWsClose(raw) {
85
- const code = typeof raw.code === 'number' ? raw.code : null;
86
- if (code === null)
87
- return null;
88
- if (code === 1008)
89
- return 'protocol';
90
- if (code === 1006)
91
- return 'connection';
92
- if (code === 1011) {
93
- const reason = text(raw.reason);
94
- if (/no running broker/i.test(reason))
95
- return 'connection';
96
- return classifyProviderText(reason) === 'connection' ? 'connection' : 'other';
21
+ function faultKindFromProviderSignal(signal) {
22
+ switch (signal.kind) {
23
+ case 'rate-limit': return 'rate-limit';
24
+ case 'auth': return 'auth';
25
+ case 'protocol': return 'protocol';
26
+ case 'context-overflow': return 'context-overflow';
27
+ case 'overloaded':
28
+ case 'overloaded-only':
29
+ case 'transient-http': return 'overloaded';
30
+ case 'connection':
31
+ case 'connection-only': return 'connection';
32
+ case 'socket':
33
+ case 'service-unavailable':
34
+ case 'other': return 'other';
97
35
  }
98
- return null;
99
36
  }
100
37
  function classifyDisposition(link, kind) {
101
38
  if (kind === 'auth' || kind === 'protocol' || kind === 'context-overflow')
@@ -140,17 +77,15 @@ export function classify(link, raw) {
140
77
  if (link === 'pi→provider' && hasManagedProviderCoolingDiagnostic(raw)) {
141
78
  return { kind: 'rate-limit', disposition: 'auto' };
142
79
  }
143
- if (isRecord(raw)) {
144
- const errno = classifyErrno(raw);
145
- if (errno !== null)
146
- return { kind: errno, disposition: classifyDisposition(link, errno) };
147
- const http = classifyHttp(raw);
148
- if (http !== null)
149
- return { kind: http, disposition: classifyDisposition(link, http) };
150
- const ws = classifyWsClose(raw);
151
- if (ws !== null)
152
- return { kind: ws, disposition: classifyDisposition(link, ws) };
153
- }
154
- const kind = classifyProviderText(rawText(raw));
80
+ const value = isRecord(raw) ? raw : {};
81
+ const signal = recognizeProviderError({
82
+ code: value['code'],
83
+ errno: value['errno'],
84
+ status: value['status'],
85
+ wsCloseCode: value['code'],
86
+ wsCloseReason: value['reason'],
87
+ message: rawText(raw),
88
+ });
89
+ const kind = faultKindFromProviderSignal(signal);
155
90
  return { kind, disposition: classifyDisposition(link, kind) };
156
91
  }
@@ -0,0 +1,27 @@
1
+ export type ProviderErrorSignalKind = 'rate-limit' | 'auth' | 'protocol' | 'context-overflow' | 'overloaded' | 'overloaded-only' | 'transient-http' | 'connection' | 'connection-only' | 'socket' | 'service-unavailable' | 'other';
2
+ export type ProviderErrorMatchedOn = 'errno' | 'status' | 'websocket-close' | 'text' | 'none';
3
+ export interface ProviderErrorTextMatch {
4
+ kind: ProviderErrorSignalKind;
5
+ match: string;
6
+ }
7
+ export interface ProviderErrorSignal {
8
+ kind: ProviderErrorSignalKind;
9
+ matchedOn: ProviderErrorMatchedOn;
10
+ status?: number;
11
+ retryAfterMs?: number;
12
+ match?: string;
13
+ textKind?: ProviderErrorSignalKind;
14
+ textMatch?: string;
15
+ textMatches?: readonly ProviderErrorTextMatch[];
16
+ }
17
+ export interface ProviderErrorRecognitionInput {
18
+ status?: unknown;
19
+ retryAfterMs?: number;
20
+ code?: unknown;
21
+ errno?: unknown;
22
+ wsCloseCode?: unknown;
23
+ wsCloseReason?: unknown;
24
+ message?: string;
25
+ }
26
+ /** Recognizes provider failure facts. Consumers apply their own retry policy. */
27
+ export declare function recognizeProviderError(input: ProviderErrorRecognitionInput): ProviderErrorSignal;
@@ -0,0 +1,74 @@
1
+ function text(value) {
2
+ return typeof value === 'string' ? value : '';
3
+ }
4
+ function signal(kind, matchedOn, options = {}) {
5
+ return { kind, matchedOn, ...options };
6
+ }
7
+ function withTextMatch(signal, textSignal) {
8
+ return textSignal.matchedOn === 'text'
9
+ ? { ...signal, textKind: textSignal.kind, textMatch: textSignal.match, textMatches: textSignal.textMatches }
10
+ : signal;
11
+ }
12
+ const TEXT_PATTERNS = [
13
+ ['rate-limit', /rate.?limit|\b429\b|too many requests|usage[ _]limit|usage_not_included|usagelimiterror|insufficient_quota|quota exceeded/i],
14
+ ['protocol', /\b413\b|payload too large|entity too large|request too large|entity\.too\.large|body exceeds/i],
15
+ ['auth', /invalid_grant|refresh token|unauthori[sz]ed|invalid.?api.?key|authentication failed|\b401\b|\b403\b/i],
16
+ ['context-overflow', /context overflow recovery failed/i],
17
+ ['protocol', /websocket.*(?:closed.*\b(?:1002|1003|1007|1008|1009|1010)\b|protocol error|policy violation|unsupported data|invalid (?:frame|payload)|message too big|mandatory extension)/i],
18
+ ['overloaded-only', /lock file is already being held/i],
19
+ ['overloaded', /overloaded|temporarily unavailable/i],
20
+ ['service-unavailable', /service.?unavailable/i],
21
+ ['overloaded-only', /internal server error|api.?error|\b5\d\d\b|capacity|server.{0,3}busy/i],
22
+ ['connection', /connection|econnreset|etimedout|enotfound|econnrefused|network|fetch failed|socket hang|timed? out|timeout|websocket\s+(?:was\s+)?closed|websocket stream closed before response\.completed|websocket\s+error|websocket.*interrupt/i],
23
+ ['socket', /socket/i],
24
+ ['connection-only', /eai_again|epipe|ehostunreach|enetunreach|\bterminated\b|other side closed|premature close/i],
25
+ ];
26
+ function recognizeText(message) {
27
+ // The order preserves the canonical taxonomy: size wins over wrapped 5xx prose,
28
+ // and auth wins over a generic API-error wrapper. Every match remains available
29
+ // because rotation deliberately gives transient text priority over rate-limit text.
30
+ const matches = TEXT_PATTERNS.flatMap(([kind, pattern]) => {
31
+ const match = message.match(pattern);
32
+ return match ? [{ kind, match: match[0] }] : [];
33
+ });
34
+ const first = matches[0];
35
+ return first === undefined ? signal('other', 'none') : { ...signal(first.kind, 'text', { match: first.match }), textMatches: matches };
36
+ }
37
+ /** Recognizes provider failure facts. Consumers apply their own retry policy. */
38
+ export function recognizeProviderError(input) {
39
+ const retryAfterMs = input.retryAfterMs;
40
+ const textSignal = recognizeText(input.message ?? '');
41
+ const errno = text(input.code) || text(input.errno);
42
+ if (/^(ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENOENT|ENOTFOUND|EAI_AGAIN|EPIPE|EHOSTUNREACH|ENETUNREACH)$/i.test(errno)) {
43
+ return withTextMatch(signal('connection', 'errno', { retryAfterMs }), textSignal);
44
+ }
45
+ if (/^ELOCKED$/i.test(errno))
46
+ return withTextMatch(signal('overloaded', 'errno', { retryAfterMs }), textSignal);
47
+ const status = typeof input.status === 'number' ? input.status : undefined;
48
+ if (status !== undefined) {
49
+ if (status === 401 || status === 403)
50
+ return withTextMatch(signal('auth', 'status', { status, retryAfterMs }), textSignal);
51
+ if (status === 429)
52
+ return withTextMatch(signal('rate-limit', 'status', { status, retryAfterMs }), textSignal);
53
+ if (status === 500 || status === 502 || status === 503 || status === 504)
54
+ return withTextMatch(signal('transient-http', 'status', { status, retryAfterMs }), textSignal);
55
+ if (status >= 500 && status <= 599)
56
+ return withTextMatch(signal('overloaded', 'status', { status, retryAfterMs }), textSignal);
57
+ if (status >= 400 && status <= 499)
58
+ return withTextMatch(signal('protocol', 'status', { status, retryAfterMs }), textSignal);
59
+ }
60
+ const wsCloseCode = typeof input.wsCloseCode === 'number' ? input.wsCloseCode : undefined;
61
+ if (wsCloseCode === 1008)
62
+ return withTextMatch(signal('protocol', 'websocket-close', { retryAfterMs }), textSignal);
63
+ if (wsCloseCode === 1006)
64
+ return withTextMatch(signal('connection', 'websocket-close', { retryAfterMs }), textSignal);
65
+ if (wsCloseCode === 1011) {
66
+ const reason = text(input.wsCloseReason);
67
+ const reasonSignal = recognizeText(reason);
68
+ if (/no running broker/i.test(reason) || reasonSignal.kind === 'connection' || reasonSignal.kind === 'connection-only') {
69
+ return withTextMatch(signal('connection', 'websocket-close', { retryAfterMs }), textSignal);
70
+ }
71
+ return withTextMatch(signal('other', 'websocket-close', { retryAfterMs }), textSignal);
72
+ }
73
+ return retryAfterMs === undefined ? textSignal : { ...textSignal, retryAfterMs };
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/crouter",
3
- "version": "0.3.273",
3
+ "version": "0.3.274",
4
4
  "description": "crtr — agent runtime with memory, plugins, and marketplaces",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/runtime.lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@north-light/crouter",
3
- "version": "0.3.273",
3
+ "version": "0.3.274",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@north-light/crouter",
9
- "version": "0.3.273",
9
+ "version": "0.3.274",
10
10
  "hasInstallScript": true,
11
11
  "license": "MIT",
12
12
  "dependencies": {