pi-braid 0.42.1 → 0.43.1
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.
- package/CHANGELOG.md +92 -0
- package/README.md +61 -228
- package/TOOLS.md +12 -14
- package/build-info.json +4 -4
- package/dist/extensions/core/polyfill/index.d.ts +3 -4
- package/dist/extensions/core/polyfill/index.js +34 -131
- package/dist/extensions/enforcement/handlers/register-agent-end-api-error.js +13 -0
- package/dist/extensions/enforcement/handlers/register-turn-end-hygiene.js +8 -3
- package/dist/extensions/enforcement/session-health.d.ts +52 -4
- package/dist/extensions/enforcement/session-health.js +75 -5
- package/dist/extensions/enforcement/whisper.js +15 -1
- package/dist/extensions/heartbeat/heartbeat-scheduler/index.d.ts +17 -0
- package/dist/extensions/heartbeat/heartbeat-scheduler/index.js +40 -78
- package/dist/extensions/heartbeat/heartbeat-scheduler/policies.d.ts +2 -1
- package/dist/extensions/heartbeat/heartbeat-scheduler/policies.js +26 -28
- package/dist/extensions/heartbeat/heartbeat-scheduler/whisper-batcher.d.ts +62 -31
- package/dist/extensions/heartbeat/heartbeat-scheduler/whisper-batcher.js +130 -55
- package/dist/extensions/memory/context-lifecycle/index.js +11 -10
- package/dist/extensions/memory/context-manager/analysis.d.ts +2 -0
- package/dist/extensions/memory/context-manager/analysis.js +6 -2
- package/dist/extensions/memory/context-manager/handlers/compaction-retry.js +3 -3
- package/dist/extensions/memory/context-manager/handlers/events.js +13 -1
- package/dist/extensions/memory/context-manager/handlers/tools/context-compress.d.ts +10 -14
- package/dist/extensions/memory/context-manager/handlers/tools/context-compress.js +62 -88
- package/dist/extensions/self/watchdog/engine.d.ts +8 -1
- package/dist/extensions/self/watchdog/engine.js +25 -2
- package/dist/extensions/self/watchdog/index.js +14 -1
- package/dist/lib/ai/llm-call.js +23 -0
- package/dist/lib/ai/model-resolver.d.ts +11 -10
- package/dist/lib/ai/model-resolver.js +16 -19
- package/dist/lib/ai/presend-budget.d.ts +68 -0
- package/dist/lib/ai/presend-budget.js +83 -0
- package/dist/lib/config/braid-settings.d.ts +38 -0
- package/dist/lib/config/braid-settings.js +84 -0
- package/dist/lib/config/per-tool-settings.d.ts +4 -4
- package/dist/lib/config/per-tool-settings.js +4 -4
- package/dist/lib/detection/context-tiers.d.ts +18 -5
- package/dist/lib/detection/context-tiers.js +51 -33
- package/dist/lib/detection/context-usage.d.ts +62 -1
- package/dist/lib/detection/context-usage.js +158 -1
- package/dist/lib/errors/llm-error.d.ts +14 -0
- package/dist/lib/errors/llm-error.js +44 -1
- package/dist/lib/state/global-state.d.ts +4 -8
- package/dist/lib/state/global-state.js +11 -28
- package/dist/lib/telemetry/context-heartbeat.d.ts +9 -0
- package/dist/lib/telemetry/context-heartbeat.js +18 -8
- package/dist/lib/utils/wake-gate.d.ts +6 -0
- package/dist/lib/utils/wake-gate.js +52 -4
- package/docs/SETUP.md +7 -6
- package/package.json +8 -11
- package/src/extensions/core/polyfill/index.ts +29 -157
- package/src/extensions/enforcement/AGENTS.md +38 -1
- package/src/extensions/enforcement/handlers/register-agent-end-api-error.ts +13 -0
- package/src/extensions/enforcement/handlers/register-turn-end-hygiene.ts +8 -2
- package/src/extensions/enforcement/session-health.ts +94 -5
- package/src/extensions/enforcement/whisper.ts +15 -1
- package/src/extensions/heartbeat/heartbeat-scheduler/index.ts +53 -80
- package/src/extensions/heartbeat/heartbeat-scheduler/policies.ts +34 -43
- package/src/extensions/heartbeat/heartbeat-scheduler/whisper-batcher.ts +154 -52
- package/src/extensions/memory/AGENTS.md +37 -38
- package/src/extensions/memory/context-lifecycle/index.ts +11 -10
- package/src/extensions/memory/context-manager/analysis.ts +8 -1
- package/src/extensions/memory/context-manager/handlers/compaction-retry.ts +3 -3
- package/src/extensions/memory/context-manager/handlers/events.ts +18 -1
- package/src/extensions/memory/context-manager/handlers/tools/context-compress.ts +81 -115
- package/src/extensions/scheduling/timer/AGENTS.md +14 -4
- package/src/extensions/self/watchdog/AGENTS.md +36 -18
- package/src/extensions/self/watchdog/engine.ts +30 -2
- package/src/extensions/self/watchdog/index.ts +13 -1
- package/src/lib/ai/AGENTS.md +2 -0
- package/src/lib/ai/llm-call.ts +30 -0
- package/src/lib/ai/model-resolver.ts +16 -19
- package/src/lib/ai/presend-budget.ts +117 -0
- package/src/lib/config/braid-settings.ts +125 -0
- package/src/lib/config/per-tool-settings.ts +4 -4
- package/src/lib/detection/context-tiers.ts +52 -41
- package/src/lib/detection/context-usage.ts +211 -2
- package/src/lib/errors/llm-error.ts +55 -1
- package/src/lib/state/global-state.ts +11 -29
- package/src/lib/telemetry/context-heartbeat.ts +27 -7
- package/src/lib/utils/wake-gate.ts +59 -4
|
@@ -66,6 +66,138 @@ export function estimateMessagesTokens(messages: readonly unknown[]): number {
|
|
|
66
66
|
return tokens;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Tokens an OUTGOING REQUEST carries: system prompt, tool definitions and
|
|
71
|
+
* messages. The session-tree walk sees only the messages, so anything that
|
|
72
|
+
* compares a request against a window has to start here - the system prompt
|
|
73
|
+
* and the tool schemas never appear in the tree at all.
|
|
74
|
+
*/
|
|
75
|
+
export function estimatePayloadTokens(parts: {
|
|
76
|
+
systemPrompt?: string;
|
|
77
|
+
messages?: readonly unknown[];
|
|
78
|
+
tools?: unknown;
|
|
79
|
+
}): number {
|
|
80
|
+
let tokens = 0;
|
|
81
|
+
if (parts.systemPrompt) tokens += Math.ceil(parts.systemPrompt.length / 4);
|
|
82
|
+
if (parts.messages) tokens += estimateMessagesTokens(parts.messages);
|
|
83
|
+
if (parts.tools !== undefined) {
|
|
84
|
+
try {
|
|
85
|
+
tokens += Math.ceil(JSON.stringify(parts.tools).length / 4);
|
|
86
|
+
} catch {
|
|
87
|
+
/* non-serializable tool definitions: leave them out rather than throw */
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return tokens;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── Calibration against the provider's own count ──────────────────────
|
|
94
|
+
//
|
|
95
|
+
// chars/4 is not a tokenizer. It is a guess about a distribution, and the
|
|
96
|
+
// distribution moves with the content: prose runs near 4 chars/token, dense
|
|
97
|
+
// tool-call JSON and base64 payloads run near 2, and characters the encoder
|
|
98
|
+
// spends several tokens on (non-Latin scripts) sit lower still. A single
|
|
99
|
+
// divisor therefore cannot be right everywhere, and the guard that reads it
|
|
100
|
+
// is wrong by the same factor in the same direction.
|
|
101
|
+
//
|
|
102
|
+
// Every successful call returns usage. Pairing that count with the estimate
|
|
103
|
+
// for the SAME span yields the one number that can correct the guess without
|
|
104
|
+
// a tokenizer: the ratio. It is kept as the median of the last few pairs
|
|
105
|
+
// because the ratio is content-dependent - a single oversized tool result
|
|
106
|
+
// moves one sample, not the level - and applied to the estimated part of the
|
|
107
|
+
// live number.
|
|
108
|
+
|
|
109
|
+
export type UsageSource = "measured" | "estimated";
|
|
110
|
+
|
|
111
|
+
/** Paired observations kept for the ratio. */
|
|
112
|
+
const CALIBRATION_SAMPLES = 8;
|
|
113
|
+
/**
|
|
114
|
+
* A pair smaller than this measures nothing: below it the fixed part of the
|
|
115
|
+
* request (system prompt, tool schemas) dominates the measured count, so the
|
|
116
|
+
* ratio would describe the harness rather than the tokenizer's view of content.
|
|
117
|
+
*/
|
|
118
|
+
const CALIBRATION_MIN_ESTIMATE_TOKENS = 2_000;
|
|
119
|
+
/**
|
|
120
|
+
* Clamp. Below 0.5 the estimate would over-count by more than half (a span
|
|
121
|
+
* whose shape changed under the pair, not a tokenizer effect); above 6 the
|
|
122
|
+
* correction exceeds anything a character-count can explain for text, so the
|
|
123
|
+
* pair is not describing the tokenizer either.
|
|
124
|
+
*/
|
|
125
|
+
const CALIBRATION_MIN_RATIO = 0.5;
|
|
126
|
+
const CALIBRATION_MAX_RATIO = 6;
|
|
127
|
+
|
|
128
|
+
let _calibrationRatios: number[] = [];
|
|
129
|
+
let _lastCalibrationKey: string | null = null;
|
|
130
|
+
let _calibrationSession: string | null = null;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Ratios belong to one session: a new session has its own content mix, and a
|
|
134
|
+
* process that outlives sessions must not apply yesterday's ratio to today's
|
|
135
|
+
* tree. Called from the measure path with whatever session key the context
|
|
136
|
+
* exposes; a context without one leaves the store alone.
|
|
137
|
+
*/
|
|
138
|
+
function scopeCalibrationToSession(sessionKey: string | undefined): void {
|
|
139
|
+
if (!sessionKey) return;
|
|
140
|
+
if (_calibrationSession === sessionKey) return;
|
|
141
|
+
_calibrationRatios = [];
|
|
142
|
+
_lastCalibrationKey = null;
|
|
143
|
+
_calibrationSession = sessionKey;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Record one paired observation: the chars/4 estimate for a request span and
|
|
148
|
+
* the provider's own count for that same span. Returns the median ratio after
|
|
149
|
+
* the insert, or null when the pair measures nothing (a span under the harness
|
|
150
|
+
* floor, or a non-positive measured count).
|
|
151
|
+
*
|
|
152
|
+
* `key` dedupes: callers re-measure the same span many times per turn, and the
|
|
153
|
+
* same pair must not weight the median more than once.
|
|
154
|
+
*/
|
|
155
|
+
export function recordCalibrationSample(
|
|
156
|
+
estimatedTokens: number,
|
|
157
|
+
measuredTokens: number,
|
|
158
|
+
key?: string,
|
|
159
|
+
): number | null {
|
|
160
|
+
if (estimatedTokens < CALIBRATION_MIN_ESTIMATE_TOKENS || measuredTokens <= 0) return null;
|
|
161
|
+
if (key && key === _lastCalibrationKey) return calibrationRatio();
|
|
162
|
+
const raw = measuredTokens / estimatedTokens;
|
|
163
|
+
const ratio = Math.min(CALIBRATION_MAX_RATIO, Math.max(CALIBRATION_MIN_RATIO, raw));
|
|
164
|
+
_calibrationRatios.push(ratio);
|
|
165
|
+
if (_calibrationRatios.length > CALIBRATION_SAMPLES) _calibrationRatios.shift();
|
|
166
|
+
_lastCalibrationKey = key ?? null;
|
|
167
|
+
return calibrationRatio();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Median of the retained ratios; null before the first usable pair. */
|
|
171
|
+
export function calibrationRatio(): number | null {
|
|
172
|
+
if (_calibrationRatios.length === 0) return null;
|
|
173
|
+
const sorted = [..._calibrationRatios].sort((a, b) => a - b);
|
|
174
|
+
const mid = Math.floor(sorted.length / 2);
|
|
175
|
+
return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Apply the session's ratio to a chars-based estimate. No ratio (first call of
|
|
180
|
+
* a session) leaves the number as the plain estimate - it never pretends to be
|
|
181
|
+
* measured, and it never invents a correction it has not observed.
|
|
182
|
+
*/
|
|
183
|
+
export function calibrateEstimate(rawTokens: number): {
|
|
184
|
+
tokens: number;
|
|
185
|
+
ratio: number | null;
|
|
186
|
+
calibrated: boolean;
|
|
187
|
+
} {
|
|
188
|
+
const ratio = calibrationRatio();
|
|
189
|
+
if (ratio === null || rawTokens <= 0)
|
|
190
|
+
return { tokens: rawTokens, ratio: null, calibrated: false };
|
|
191
|
+
return { tokens: Math.round(rawTokens * ratio), ratio, calibrated: true };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Test hook: clear the retained pairs (also clears the dedupe key). */
|
|
195
|
+
export function resetCalibrationForTests(): void {
|
|
196
|
+
_calibrationRatios = [];
|
|
197
|
+
_lastCalibrationKey = null;
|
|
198
|
+
_calibrationSession = null;
|
|
199
|
+
}
|
|
200
|
+
|
|
69
201
|
/** Tokens carried by one session entry (message, compaction summary, etc.). */
|
|
70
202
|
function entryTokens(entry: any): number {
|
|
71
203
|
if (!entry || typeof entry !== "object") return 0;
|
|
@@ -107,8 +239,19 @@ export function resolveWindow(modelWindow?: number | null): ResolvedWindow {
|
|
|
107
239
|
}
|
|
108
240
|
|
|
109
241
|
export interface LiveUsage {
|
|
110
|
-
/**
|
|
242
|
+
/**
|
|
243
|
+
* Tokens of the next request payload, calibrated. This is the number guards
|
|
244
|
+
* read: a measured base plus a calibrated estimate of the trailing entries.
|
|
245
|
+
*/
|
|
111
246
|
tokens: number;
|
|
247
|
+
/** Where `tokens` came from: an API response, or the chars-based estimate. */
|
|
248
|
+
source: UsageSource;
|
|
249
|
+
/** The uncalibrated chars/4 walk over the same span (never rendered as the total). */
|
|
250
|
+
estimatedTokens: number;
|
|
251
|
+
/** The trailing part that only the estimate covers, after calibration. 0 when measured. */
|
|
252
|
+
estimatedTrailingTokens: number;
|
|
253
|
+
/** The session ratio applied, when one was observed. */
|
|
254
|
+
calibrationRatio?: number;
|
|
112
255
|
contextWindow: number;
|
|
113
256
|
percent: number;
|
|
114
257
|
windowSource: "configured" | "provider";
|
|
@@ -139,6 +282,15 @@ export function measureLiveUsage(ctx: unknown): LiveUsage | undefined {
|
|
|
139
282
|
|
|
140
283
|
const modelWindow = (ctx as { model?: { contextWindow?: number } })?.model?.contextWindow;
|
|
141
284
|
const window = resolveWindow(modelWindow);
|
|
285
|
+
try {
|
|
286
|
+
scopeCalibrationToSession(
|
|
287
|
+
(
|
|
288
|
+
ctx as { sessionManager?: { getSessionId?: () => string } }
|
|
289
|
+
).sessionManager?.getSessionId?.(),
|
|
290
|
+
);
|
|
291
|
+
} catch {
|
|
292
|
+
/* no session key: keep the store as it is */
|
|
293
|
+
}
|
|
142
294
|
|
|
143
295
|
// Latest compaction boundary: entries strictly before it are compacted
|
|
144
296
|
// away and are not part of the live payload.
|
|
@@ -188,14 +340,43 @@ export function measureLiveUsage(ctx: unknown): LiveUsage | undefined {
|
|
|
188
340
|
break;
|
|
189
341
|
}
|
|
190
342
|
}
|
|
343
|
+
const estimatedTokens = liveTokens;
|
|
344
|
+
let source: UsageSource = "estimated";
|
|
345
|
+
let estimatedTrailingTokens: number;
|
|
346
|
+
let calibrationRatioApplied: number | undefined;
|
|
347
|
+
|
|
191
348
|
if (usageBacked) {
|
|
349
|
+
// The provider's count covers exactly the entries from the boundary through
|
|
350
|
+
// the message that carries the usage; pair that count with the walk over
|
|
351
|
+
// the same slice. This is the only place both numbers describe one request.
|
|
352
|
+
let rawUpToUsage = 0;
|
|
353
|
+
for (let i = liveStart; i <= usageBacked.index; i++) rawUpToUsage += entryTokens(branch[i]);
|
|
354
|
+
recordCalibrationSample(
|
|
355
|
+
rawUpToUsage,
|
|
356
|
+
usageBacked.usageTokens,
|
|
357
|
+
`${liveStart}:${usageBacked.index}:${usageBacked.usageTokens}`,
|
|
358
|
+
);
|
|
359
|
+
|
|
192
360
|
let trailing = 0;
|
|
193
361
|
for (let i = usageBacked.index + 1; i < branch.length; i++) trailing += entryTokens(branch[i]);
|
|
194
|
-
|
|
362
|
+
const calibratedTrailing = calibrateEstimate(trailing);
|
|
363
|
+
liveTokens = usageBacked.usageTokens + calibratedTrailing.tokens;
|
|
364
|
+
source = "measured";
|
|
365
|
+
estimatedTrailingTokens = calibratedTrailing.tokens;
|
|
366
|
+
if (calibratedTrailing.ratio !== null) calibrationRatioApplied = calibratedTrailing.ratio;
|
|
367
|
+
} else {
|
|
368
|
+
const calibrated = calibrateEstimate(liveTokens);
|
|
369
|
+
liveTokens = calibrated.tokens;
|
|
370
|
+
estimatedTrailingTokens = calibrated.tokens;
|
|
371
|
+
if (calibrated.ratio !== null) calibrationRatioApplied = calibrated.ratio;
|
|
195
372
|
}
|
|
196
373
|
|
|
197
374
|
return {
|
|
198
375
|
tokens: liveTokens,
|
|
376
|
+
source,
|
|
377
|
+
estimatedTokens,
|
|
378
|
+
estimatedTrailingTokens,
|
|
379
|
+
calibrationRatio: calibrationRatioApplied,
|
|
199
380
|
contextWindow: window.contextWindow,
|
|
200
381
|
percent: window.contextWindow > 0 ? (liveTokens / window.contextWindow) * 100 : 0,
|
|
201
382
|
windowSource: window.windowSource,
|
|
@@ -268,3 +449,31 @@ export function tokenArms(providerWindow?: number | null): TokenArms {
|
|
|
268
449
|
export function formatTokensK(tokens: number): string {
|
|
269
450
|
return `${Math.round(tokens / 1000)}K`;
|
|
270
451
|
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* One label for any rendered usage number, naming its provenance. The word is
|
|
455
|
+
* `measured` only when an API response supplied the anchor; everything else is
|
|
456
|
+
* `estimated`, with the calibration factor shown when one was applied. A
|
|
457
|
+
* trailing estimate behind a measured anchor is named rather than folded in -
|
|
458
|
+
* rounding it away is how the number stops meaning anything.
|
|
459
|
+
*/
|
|
460
|
+
export function describeUsage(usage: {
|
|
461
|
+
source: UsageSource;
|
|
462
|
+
estimatedTrailingTokens?: number;
|
|
463
|
+
calibrationRatio?: number;
|
|
464
|
+
}): string {
|
|
465
|
+
const ratio =
|
|
466
|
+
usage.calibrationRatio !== undefined ? ` x${usage.calibrationRatio.toFixed(2)}` : "";
|
|
467
|
+
if (usage.source === "measured") {
|
|
468
|
+
const trailing = usage.estimatedTrailingTokens ?? 0;
|
|
469
|
+
return trailing > 0
|
|
470
|
+
? `measured + ${formatTokensK(trailing)} estimated${ratio}`
|
|
471
|
+
: `measured${ratio}`;
|
|
472
|
+
}
|
|
473
|
+
return `estimated${ratio}`;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** `<pct>% <provenance>` - the percent form used wherever a percentage renders. */
|
|
477
|
+
export function describeUsagePercent(usage: LiveUsage): string {
|
|
478
|
+
return `${usage.percent.toFixed(1)}% ${describeUsage(usage)}`;
|
|
479
|
+
}
|
|
@@ -43,6 +43,13 @@ export interface ClassifiedLlmError {
|
|
|
43
43
|
fallbackToNonLlm: boolean;
|
|
44
44
|
/** Does the user need to do something (top up balance, fix key)? */
|
|
45
45
|
userActionRequired: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Set when the failure is about the SHAPE of the request rather than its
|
|
48
|
+
* timing: the same request will be refused the same way, so the retry path
|
|
49
|
+
* and any wake that would start a fresh turn are both wrong. The ring
|
|
50
|
+
* records the same class as a request-shape death.
|
|
51
|
+
*/
|
|
52
|
+
requestShape?: "context_length";
|
|
46
53
|
/** Plain-text hint for the user. Empty for transient/unknown. */
|
|
47
54
|
userMessage?: string;
|
|
48
55
|
/** Hint from Retry-After header or computed backoff (ms). */
|
|
@@ -117,6 +124,47 @@ const CONTEXT_WINDOW_PATTERNS = [
|
|
|
117
124
|
/prompt[_ ]is[_ ]too[_ ]long/i,
|
|
118
125
|
];
|
|
119
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Context-length refusals in their 400-shaped wordings. A provider that
|
|
129
|
+
* rejects an oversized body with `400 invalid_request_error` is naming a
|
|
130
|
+
* request-shape problem: the numbers change, the verdict does not. Kept apart
|
|
131
|
+
* from CONTEXT_WINDOW_PATTERNS so the bare `413` arm keeps its meaning and a
|
|
132
|
+
* 5xx body cannot be pulled in by a phrase like "too many tokens".
|
|
133
|
+
*/
|
|
134
|
+
const REQUEST_SHAPE_CONTEXT_PATTERNS = [
|
|
135
|
+
/maximum[_ ]context[_ ]length/i,
|
|
136
|
+
/context[_ ]length/i,
|
|
137
|
+
/reduce[ _]the[ _]length[ _]of[ _]the/i,
|
|
138
|
+
/(input|prompt|request|messages?)[ _]is[ _]too[ _]long/i,
|
|
139
|
+
/too[ _]many[ _](input[ _])?tokens/i,
|
|
140
|
+
/exceeds?[ _]the[ _](model'?s?[ _])?(maximum|max)/i,
|
|
141
|
+
];
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* True when the text names a context-length refusal. Used by consumers that
|
|
145
|
+
* only have text (pi-core surfaces session failures as the assistant's
|
|
146
|
+
* errorMessage string) and must tell "the request must change" apart from
|
|
147
|
+
* "try again later" without re-deriving the pattern list.
|
|
148
|
+
*/
|
|
149
|
+
export function isContextLengthRequestShape(err: unknown): boolean {
|
|
150
|
+
const text =
|
|
151
|
+
typeof err === "string"
|
|
152
|
+
? err
|
|
153
|
+
: typeof err === "object" && err !== null
|
|
154
|
+
? String((err as { message?: unknown }).message ?? "")
|
|
155
|
+
: "";
|
|
156
|
+
if (!text) return false;
|
|
157
|
+
return (
|
|
158
|
+
matchesAny(text, [
|
|
159
|
+
/context[_ ]length[_ ]exceeded/i,
|
|
160
|
+
/context[_ ]window/i,
|
|
161
|
+
/maximum[_ ]context/i,
|
|
162
|
+
/prompt[_ ]is[_ ]too[_ ]long/i,
|
|
163
|
+
...REQUEST_SHAPE_CONTEXT_PATTERNS,
|
|
164
|
+
]) && !matchesAny(text, TRANSIENT_PATTERNS)
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
120
168
|
const TRANSIENT_PATTERNS = [
|
|
121
169
|
/500/,
|
|
122
170
|
/502/,
|
|
@@ -276,7 +324,13 @@ export function classifyLlmError(err: unknown): ClassifiedLlmError {
|
|
|
276
324
|
return makeError("auth", err);
|
|
277
325
|
}
|
|
278
326
|
if (status === 413 || matchesAny(text, CONTEXT_WINDOW_PATTERNS)) {
|
|
279
|
-
return makeError("context_window", err);
|
|
327
|
+
return makeError("context_window", err, { requestShape: "context_length" });
|
|
328
|
+
}
|
|
329
|
+
// A 400 whose body names a context-length refusal is the same class as a
|
|
330
|
+
// 413: the request shape is wrong, so it must never fall through to
|
|
331
|
+
// `unknown` (which is retryable, and retrying it unchanged cannot work).
|
|
332
|
+
if (status === 400 && matchesAny(text, REQUEST_SHAPE_CONTEXT_PATTERNS)) {
|
|
333
|
+
return makeError("context_window", err, { requestShape: "context_length" });
|
|
280
334
|
}
|
|
281
335
|
if (status === 500 || status === 502 || status === 503 || status === 504 || status === 529) {
|
|
282
336
|
return makeError("transient", err);
|
|
@@ -17,9 +17,10 @@
|
|
|
17
17
|
// compiled DIST (dist/lib/state/global-state.js via the pi-braid/lib export).
|
|
18
18
|
// That yields TWO module instances of this file in one process; a module-level
|
|
19
19
|
// Map would be populated by the src instance and read (empty) by the dist
|
|
20
|
-
// instance
|
|
21
|
-
// first-available
|
|
22
|
-
// KEY_RING_FEEDBACK_KEY above; the slot must also
|
|
20
|
+
// instance, so the role setting would read as unset and resolution would fall
|
|
21
|
+
// away from it (first-available is where the MiniMax 429 lived). Same
|
|
22
|
+
// cross-bundle hazard as KEY_RING_FEEDBACK_KEY above; the slot must also
|
|
23
|
+
// survive /reload.
|
|
23
24
|
export const CUSTOM_MODEL_KEYS_KEY = "__braid_custom_model_keys";
|
|
24
25
|
|
|
25
26
|
/**
|
|
@@ -31,7 +32,7 @@ export type CustomModelKeyValue = string | string[];
|
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Get the shared custom model keys map.
|
|
34
|
-
* polyfill writes to it (/
|
|
35
|
+
* polyfill writes to it (/utility-model command).
|
|
35
36
|
* model-resolver reads from it (model resolution).
|
|
36
37
|
*/
|
|
37
38
|
export function getCustomModelKeys(): Map<string, CustomModelKeyValue> {
|
|
@@ -43,8 +44,8 @@ export function getCustomModelKeys(): Map<string, CustomModelKeyValue> {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
* Read a custom model key (e.g. "utilityModel"
|
|
47
|
-
*
|
|
47
|
+
* Read a custom model key (e.g. "utilityModel"): a pattern or an ordered list
|
|
48
|
+
* of them.
|
|
48
49
|
*/
|
|
49
50
|
export function getCustomModelKey(key: string): CustomModelKeyValue | undefined {
|
|
50
51
|
return getCustomModelKeys().get(key);
|
|
@@ -52,35 +53,16 @@ export function getCustomModelKey(key: string): CustomModelKeyValue | undefined
|
|
|
52
53
|
|
|
53
54
|
/**
|
|
54
55
|
* Set a custom model key: one pattern or an ordered list. Used by polyfill for
|
|
55
|
-
* /utility-model
|
|
56
|
+
* /utility-model.
|
|
56
57
|
*/
|
|
57
58
|
export function setCustomModelKey(key: string, value: CustomModelKeyValue): void {
|
|
58
59
|
getCustomModelKeys().set(key, value);
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
// The
|
|
62
|
-
//
|
|
63
|
-
|
|
62
|
+
// The auxiliary role's key. Both the polyfill (which writes it) and the
|
|
63
|
+
// resolver (which reads it) address the one merged role: vision analysis falls
|
|
64
|
+
// back to its first image-capable entry, so there is no separate vision key.
|
|
64
65
|
export const UTILITY_MODEL_KEY = "utilityModel";
|
|
65
|
-
// The max-power role is folded into utility; the constant survives so the
|
|
66
|
-
// polyfill can recognize and clean the legacy key from settings.json; nothing
|
|
67
|
-
// resolves it as a live role.
|
|
68
|
-
export const MAX_POWER_MODEL_KEY = "maxPowerModel";
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* One-shot latch for the maxPowerModel deprecation notice. Process-global for
|
|
72
|
-
* the same reason as the keys above: a src-loaded and a dist-loaded instance
|
|
73
|
-
* of this module must agree on whether the notice already fired.
|
|
74
|
-
*/
|
|
75
|
-
const MAX_POWER_NOTICE_KEY = "__braid_max_power_deprecation_notice";
|
|
76
|
-
|
|
77
|
-
/** True the first time it is called in this process, false afterwards. */
|
|
78
|
-
export function consumeMaxPowerDeprecationNotice(): boolean {
|
|
79
|
-
const slot = globalThis as Record<string, unknown>;
|
|
80
|
-
if (slot[MAX_POWER_NOTICE_KEY]) return false;
|
|
81
|
-
slot[MAX_POWER_NOTICE_KEY] = true;
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
66
|
|
|
85
67
|
export interface RotationState {
|
|
86
68
|
lastRotationTimestamp: number;
|
|
@@ -46,7 +46,7 @@ import {
|
|
|
46
46
|
safeSessionManagerTyped,
|
|
47
47
|
} from "../state/extension-state.js";
|
|
48
48
|
import { getContextUsage } from "../detection/context-tiers.js";
|
|
49
|
-
import { measureLiveUsage } from "../detection/context-usage.js";
|
|
49
|
+
import { measureLiveUsage, describeUsage } from "../detection/context-usage.js";
|
|
50
50
|
|
|
51
51
|
const log = createLogger("context-heartbeat");
|
|
52
52
|
|
|
@@ -479,6 +479,15 @@ export function collectAndEmitContextHeartbeat(
|
|
|
479
479
|
export interface SelfStateSample {
|
|
480
480
|
/** Current context size in tokens (live read, not the last-tick snapshot). */
|
|
481
481
|
contextTokens: number | null;
|
|
482
|
+
/** Percent of the resolved window the same reading fills; null when unmeasured. */
|
|
483
|
+
contextPercent: number | null;
|
|
484
|
+
/**
|
|
485
|
+
* The meter's own provenance word for the two fields above (`measured`,
|
|
486
|
+
* `estimated`, or a measured anchor plus its estimated trailing part). Null
|
|
487
|
+
* when no reading was available. A reader that cannot tell the two apart
|
|
488
|
+
* cannot tell a large context from a badly counted one.
|
|
489
|
+
*/
|
|
490
|
+
contextProvenance: string | null;
|
|
482
491
|
/** Cache-hit share, percent 0-100 (last assistant message). */
|
|
483
492
|
cacheHitShare: number | null;
|
|
484
493
|
/** Near-duplicate message count from the context-density evaluator. */
|
|
@@ -498,19 +507,30 @@ export interface SelfStateSample {
|
|
|
498
507
|
export function getSelfStateSample(ctx: ExtensionContext): SelfStateSample {
|
|
499
508
|
const sample: SelfStateSample = {
|
|
500
509
|
contextTokens: null,
|
|
510
|
+
contextPercent: null,
|
|
511
|
+
contextProvenance: null,
|
|
501
512
|
cacheHitShare: null,
|
|
502
513
|
nearDupCount: null,
|
|
503
514
|
tpsEma: null,
|
|
504
515
|
hygieneActs: null,
|
|
505
516
|
};
|
|
506
517
|
try {
|
|
507
|
-
// Context
|
|
508
|
-
//
|
|
509
|
-
//
|
|
510
|
-
//
|
|
518
|
+
// Context size: live read (compaction-aware session-tree measure), not the
|
|
519
|
+
// last-tick snapshot - a summary rendered right after a slide must not
|
|
520
|
+
// report the pre-slide size.
|
|
521
|
+
//
|
|
522
|
+
// One instrument, and it is this one. Size, percent and provenance come
|
|
523
|
+
// from a single reading here; the provider accessor answers a different
|
|
524
|
+
// question over a different tree (session projection vs branch), and a
|
|
525
|
+
// delivery that prints both is two ages of one fact. When this meter has
|
|
526
|
+
// no reading, the line says n/a rather than borrowing a second opinion:
|
|
527
|
+
// the reader cannot act on a number whose instrument it cannot name.
|
|
511
528
|
const live = measureLiveUsage(ctx);
|
|
512
|
-
|
|
513
|
-
|
|
529
|
+
if (live) {
|
|
530
|
+
sample.contextTokens = live.tokens;
|
|
531
|
+
sample.contextPercent = live.percent;
|
|
532
|
+
sample.contextProvenance = describeUsage(live);
|
|
533
|
+
}
|
|
514
534
|
} catch {
|
|
515
535
|
/* keep null */
|
|
516
536
|
}
|
|
@@ -155,6 +155,30 @@ let _failSafeHandle: ReturnType<typeof setTimeout> | null = null;
|
|
|
155
155
|
// Test hook: override the fail-safe bound (null restores the default).
|
|
156
156
|
let _failSafeMsOverride: number | null = null;
|
|
157
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Why the last turn could not be sent at all, when the reason is the SHAPE of
|
|
160
|
+
* the request rather than the state of the provider. A context-length refusal
|
|
161
|
+
* is permanent for a given context: every later turn carrying the same payload
|
|
162
|
+
* is refused the same way, so an ungated wake that starts one is not a retry,
|
|
163
|
+
* it is the same failure under a fresh request id.
|
|
164
|
+
*/
|
|
165
|
+
let _requestShapeFailure: string | null = null;
|
|
166
|
+
|
|
167
|
+
/** Record that the last turn failed on request shape (a context-length refusal). */
|
|
168
|
+
export function noteRequestShapeFailure(detail: string): void {
|
|
169
|
+
_requestShapeFailure = detail;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The recorded request-shape failure, or null. */
|
|
173
|
+
export function requestShapeFailure(): string | null {
|
|
174
|
+
return _requestShapeFailure;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Clear the record (a request that fits went through, or a new session). */
|
|
178
|
+
export function clearRequestShapeFailure(): void {
|
|
179
|
+
_requestShapeFailure = null;
|
|
180
|
+
}
|
|
181
|
+
|
|
158
182
|
function failSafeMs(): number {
|
|
159
183
|
return _failSafeMsOverride ?? RUN_REGION_MAX_MS;
|
|
160
184
|
}
|
|
@@ -170,18 +194,47 @@ function clearFailSafe(): void {
|
|
|
170
194
|
function armRunRegion(): void {
|
|
171
195
|
_runRegionActive = true;
|
|
172
196
|
clearFailSafe();
|
|
197
|
+
scheduleFailSafe();
|
|
198
|
+
// Do not keep the process alive for a stuck gate.
|
|
199
|
+
unrefFailSafe();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Let the process exit while a fail-safe window is pending. */
|
|
203
|
+
function unrefFailSafe(): void {
|
|
204
|
+
const handle = _failSafeHandle as { unref?: () => void } | null;
|
|
205
|
+
if (typeof handle?.unref === "function") handle.unref();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* One fail-safe window. Normally the gate opens here: a gate stuck closed by a
|
|
210
|
+
* pre-flight abort rewrites every wake into a queued steer that never starts a
|
|
211
|
+
* turn, which is its own failure.
|
|
212
|
+
*
|
|
213
|
+
* The exception is a recorded request-shape failure. There, opening the gate
|
|
214
|
+
* buys no safety - it starts the same doomed request again, and again. So the
|
|
215
|
+
* gate stays closed and the window re-arms: wakes keep arriving as steers into
|
|
216
|
+
* the session (the shape that reads them when a run does start) without
|
|
217
|
+
* starting one.
|
|
218
|
+
*/
|
|
219
|
+
function scheduleFailSafe(): void {
|
|
173
220
|
_failSafeHandle = setTimeout(() => {
|
|
174
221
|
_failSafeHandle = null;
|
|
222
|
+
if (_requestShapeFailure) {
|
|
223
|
+
log.warn(
|
|
224
|
+
`[wake-gate] fail-safe: keeping triggerTurn wakes gated - the last turn was refused on ` +
|
|
225
|
+
`request shape (context length), so the request it would start cannot succeed. ` +
|
|
226
|
+
`Remedy: compact the session. Detail: ${_requestShapeFailure}`,
|
|
227
|
+
);
|
|
228
|
+
scheduleFailSafe();
|
|
229
|
+
unrefFailSafe();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
175
232
|
_runRegionActive = false;
|
|
176
233
|
log.warn(
|
|
177
234
|
`[wake-gate] fail-safe: run region auto-cleared after ${failSafeMs()}ms without a release event ` +
|
|
178
235
|
`(pre-flight abort?) — triggerTurn wakes ungated again`,
|
|
179
236
|
);
|
|
180
237
|
}, failSafeMs());
|
|
181
|
-
// Do not keep the process alive for a stuck gate.
|
|
182
|
-
if (typeof (_failSafeHandle as { unref?: () => void } | null)?.unref === "function") {
|
|
183
|
-
(_failSafeHandle as { unref: () => void }).unref();
|
|
184
|
-
}
|
|
185
238
|
}
|
|
186
239
|
|
|
187
240
|
/** Release the gate and cancel the fail-safe timer. */
|
|
@@ -200,6 +253,7 @@ export function resetWakeGateForTests(): void {
|
|
|
200
253
|
releaseRunRegion();
|
|
201
254
|
_lastAgentCtx = null;
|
|
202
255
|
_failSafeMsOverride = null;
|
|
256
|
+
_requestShapeFailure = null;
|
|
203
257
|
}
|
|
204
258
|
|
|
205
259
|
/** Test hook: shorten/extend the fail-safe bound (null restores the default). */
|
|
@@ -274,6 +328,7 @@ export function registerWakeGate(pi: {
|
|
|
274
328
|
);
|
|
275
329
|
(pi.on as (event: string, handler: (...args: any[]) => any) => void)(SESSION_SHUTDOWN, () => {
|
|
276
330
|
releaseRunRegion();
|
|
331
|
+
_requestShapeFailure = null; // a new session starts without a shape verdict
|
|
277
332
|
_lastAgentCtx = null; // no live session after shutdown: fail open until the next capture
|
|
278
333
|
});
|
|
279
334
|
}
|