@aragon-agent/core 0.2.88 → 0.2.89
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/dist/engine/agent-loop.d.ts +6 -0
- package/dist/engine/agent-loop.js +3 -0
- package/dist/engine/agent.js +1 -0
- package/dist/llm/provider.d.ts +20 -0
- package/dist/llm/providers/anthropic.js +8 -1
- package/dist/llm/providers/google.js +7 -1
- package/dist/llm/providers/index.js +11 -1
- package/dist/llm/providers/openai.js +8 -3
- package/dist/llm/retry.d.ts +80 -1
- package/dist/llm/retry.js +191 -16
- package/package.json +1 -1
|
@@ -52,6 +52,12 @@ export interface AgentLoopContext {
|
|
|
52
52
|
llmCallTimeout: number;
|
|
53
53
|
toolTimeout: number;
|
|
54
54
|
codeTimeout: number;
|
|
55
|
+
/**
|
|
56
|
+
* The host's idle-watchdog window, forwarded so the request ladder can bound
|
|
57
|
+
* its read deadline BELOW it (F1′). Optional: a host that sets none leaves the
|
|
58
|
+
* policy's own number in charge.
|
|
59
|
+
*/
|
|
60
|
+
idleTimeout?: number;
|
|
55
61
|
/**
|
|
56
62
|
* The engine's ceiling on ONE `compact()` call. Defaults to
|
|
57
63
|
* `COMPACTION_HARD_TIMEOUT_MS`.
|
|
@@ -322,6 +322,9 @@ export async function runAgentLoop(ctx) {
|
|
|
322
322
|
apiKey,
|
|
323
323
|
baseUrl: ctx.model.baseUrl,
|
|
324
324
|
signal: ctx.signal,
|
|
325
|
+
...(ctx.timeouts.idleTimeout !== undefined
|
|
326
|
+
? { idleWatchdogMs: ctx.timeouts.idleTimeout }
|
|
327
|
+
: {}),
|
|
325
328
|
thinkingLevel: ctx.thinkingLevel,
|
|
326
329
|
maxTokens: ctx.maxTokens,
|
|
327
330
|
// THE ONLY PLACE IN THE WHOLE REPOSITORY THAT SETS `retryEscalation`
|
package/dist/engine/agent.js
CHANGED
|
@@ -476,6 +476,7 @@ export class Agent {
|
|
|
476
476
|
timeouts: {
|
|
477
477
|
llmCallTimeout: this.llmCallTimeout,
|
|
478
478
|
toolTimeout: this.toolTimeout,
|
|
479
|
+
idleTimeout: this.idleTimeout,
|
|
479
480
|
codeTimeout: this.codeTimeout,
|
|
480
481
|
...(this.compactionHardTimeout !== undefined
|
|
481
482
|
? { compactionHardTimeout: this.compactionHardTimeout }
|
package/dist/llm/provider.d.ts
CHANGED
|
@@ -18,6 +18,26 @@ export interface LLMRequest {
|
|
|
18
18
|
baseUrl?: string;
|
|
19
19
|
/** Abort signal for cancellation. */
|
|
20
20
|
signal?: AbortSignal;
|
|
21
|
+
/**
|
|
22
|
+
* INTERNAL, set only by the retrying registry: ends THIS attempt when the wire has
|
|
23
|
+
* said nothing for the resolved read window (F1′). Deliberately separate from
|
|
24
|
+
* `signal`, which every adapter reads as “the user pressed Esc” and answers with a
|
|
25
|
+
* silent return. An adapter that honours it ends the read as a truncation, which is
|
|
26
|
+
* a retryable failure the ladder already understands.
|
|
27
|
+
*/
|
|
28
|
+
readDeadline?: AbortSignal;
|
|
29
|
+
/**
|
|
30
|
+
* INTERNAL, set only by the retrying registry: one call per wire frame, whatever the
|
|
31
|
+
* frame was. `ping` / `message_start` / `message_delta` / a text-or-thinking
|
|
32
|
+
* `content_block_stop` produce no `StreamEvent` but do prove the connection is alive.
|
|
33
|
+
*/
|
|
34
|
+
onWireFrame?: () => void;
|
|
35
|
+
/**
|
|
36
|
+
* INTERNAL, set only by the agent loop: the host's idle-watchdog window, so the read
|
|
37
|
+
* deadline can be bounded BELOW it. A deadline that fires after the watchdog has
|
|
38
|
+
* already killed the run recovers nothing.
|
|
39
|
+
*/
|
|
40
|
+
idleWatchdogMs?: number;
|
|
21
41
|
temperature?: number;
|
|
22
42
|
/**
|
|
23
43
|
* The user's explicit output-token cap. `undefined` means AUTO — the adapter
|
|
@@ -93,9 +93,16 @@ export class AnthropicProvider {
|
|
|
93
93
|
const blockAccumulators = new Map();
|
|
94
94
|
let currentBlockIndex = -1;
|
|
95
95
|
try {
|
|
96
|
-
|
|
96
|
+
// THE READ SIGNAL COVERS ONE ATTEMPT, not the request: it aborts on the caller's
|
|
97
|
+
// Esc (so Esc still means Esc) and on silence. Ending the read is what turns a
|
|
98
|
+
// stall into the `truncated before message_stop` failure below, which is
|
|
99
|
+
// `retryable` and therefore reaches the ladder (F1′).
|
|
100
|
+
const readSignal = request.readDeadline ?? request.signal;
|
|
101
|
+
for await (const sse of parseSSEStream(response.body, readSignal)) {
|
|
97
102
|
if (request.signal?.aborted)
|
|
98
103
|
return;
|
|
104
|
+
// LIVENESS, BEFORE ANY FILTERING: a `ping` is still a live connection.
|
|
105
|
+
request.onWireFrame?.();
|
|
99
106
|
// Anthropic uses the `event` field to distinguish event types
|
|
100
107
|
const eventType = sse.event;
|
|
101
108
|
if (!eventType || !sse.data)
|
|
@@ -94,9 +94,15 @@ export class GoogleProvider {
|
|
|
94
94
|
*/
|
|
95
95
|
let sawTerminal = false;
|
|
96
96
|
try {
|
|
97
|
-
|
|
97
|
+
// One attempt, one read signal: aborting it (silence, or the caller's Esc) ends
|
|
98
|
+
// the read, and the `truncated before a terminal chunk` branch below is what the
|
|
99
|
+
// retry ladder sees (F1′).
|
|
100
|
+
const readSignal = request.readDeadline ?? request.signal;
|
|
101
|
+
for await (const sse of parseSSEStream(response.body, readSignal)) {
|
|
98
102
|
if (request.signal?.aborted)
|
|
99
103
|
return;
|
|
104
|
+
// LIVENESS, BEFORE ANY FILTERING: usage-only and empty chunks are frames too.
|
|
105
|
+
request.onWireFrame?.();
|
|
100
106
|
const raw = sse.data.trim();
|
|
101
107
|
if (!raw)
|
|
102
108
|
continue;
|
|
@@ -70,7 +70,14 @@ export class ProviderRegistry {
|
|
|
70
70
|
const policy = this.retryPolicy;
|
|
71
71
|
if (!policy || policy.maxRetries <= 0)
|
|
72
72
|
return provider.stream(request);
|
|
73
|
-
|
|
73
|
+
// THE ATTEMPT'S DEADLINE IS THE DERIVED ONE, never `request.signal` (F1′): the
|
|
74
|
+
// adapters read an aborted `signal` as a user Esc and return silently, which is
|
|
75
|
+
// exactly the defect this wiring removes. Everything else is forwarded as-is.
|
|
76
|
+
return withRetry((attempt) => provider.stream({
|
|
77
|
+
...request,
|
|
78
|
+
...(attempt?.readDeadline ? { readDeadline: attempt.readDeadline } : {}),
|
|
79
|
+
...(attempt ? { onWireFrame: attempt.onFrame } : {}),
|
|
80
|
+
}), {
|
|
74
81
|
policy,
|
|
75
82
|
providerId,
|
|
76
83
|
modelId: request.model,
|
|
@@ -82,6 +89,9 @@ export class ProviderRegistry {
|
|
|
82
89
|
? { escalation: request.retryEscalation }
|
|
83
90
|
: {}),
|
|
84
91
|
...(request.startRung !== undefined ? { startRung: request.startRung } : {}),
|
|
92
|
+
...(request.idleWatchdogMs !== undefined
|
|
93
|
+
? { watchdogWindowMs: request.idleWatchdogMs }
|
|
94
|
+
: {}),
|
|
85
95
|
});
|
|
86
96
|
}
|
|
87
97
|
/**
|
|
@@ -88,7 +88,7 @@ export class OpenAIProvider {
|
|
|
88
88
|
if (!response.body) {
|
|
89
89
|
throw new LLMError('Response body is null', this.id, 'network_error', true);
|
|
90
90
|
}
|
|
91
|
-
for await (const event of readOpenAIResponse(response.body, scope)) {
|
|
91
|
+
for await (const event of readOpenAIResponse(response.body, scope, request.readDeadline, request.onWireFrame)) {
|
|
92
92
|
if (request.signal?.aborted)
|
|
93
93
|
return;
|
|
94
94
|
yield event;
|
|
@@ -214,12 +214,17 @@ function sendOpenAIRequest(request, defaultBaseUrl, signal) {
|
|
|
214
214
|
}),
|
|
215
215
|
});
|
|
216
216
|
}
|
|
217
|
-
async function* readOpenAIResponse(body, scope) {
|
|
217
|
+
async function* readOpenAIResponse(body, scope, readDeadline, onWireFrame) {
|
|
218
218
|
const state = new OpenAIStreamState();
|
|
219
219
|
try {
|
|
220
|
-
|
|
220
|
+
// The read signal covers ONE attempt: it aborts on the caller's Esc (so Esc still
|
|
221
|
+
// means Esc) and on silence. Aborting the read is what turns a stall into the
|
|
222
|
+
// `AbortError` -> `timeout`/`retryable` failure the ladder already handles (F1′).
|
|
223
|
+
for await (const sse of parseSSEStream(body, readDeadline ?? scope.signal)) {
|
|
221
224
|
if (scope.callerAborted)
|
|
222
225
|
return;
|
|
226
|
+
// LIVENESS, BEFORE `[DONE]`: a usage-only chunk is still a frame.
|
|
227
|
+
onWireFrame?.();
|
|
223
228
|
const raw = sse.data.trim();
|
|
224
229
|
if (raw === '[DONE]') {
|
|
225
230
|
state.sawTerminal = true;
|
package/dist/llm/retry.d.ts
CHANGED
|
@@ -77,8 +77,47 @@ export interface RetryPolicy {
|
|
|
77
77
|
maxElapsedMs: number;
|
|
78
78
|
/** Allow a restart after content has already been forwarded (§3.5). */
|
|
79
79
|
onPartialStream: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* How long ONE attempt may go without a single wire frame before it is treated
|
|
82
|
+
* as a retryable transport failure (`agent-idle-watchdog-no-recovery` F1′).
|
|
83
|
+
*
|
|
84
|
+
* OPTIONAL so every existing `RetryPolicy` literal — the CLI's
|
|
85
|
+
* `clampRetryConfig`, every test, an embedder's own object — keeps
|
|
86
|
+
* compiling; `normalizePolicy` supplies the default. `0` DISABLES the deadline,
|
|
87
|
+
* which is what a caller wanting pre-feature behaviour passes.
|
|
88
|
+
*
|
|
89
|
+
* WHY THIS IS NOT `idleTimeout`: the watchdog kills the whole RUN one layer up,
|
|
90
|
+
* so a run that went quiet ended with a verdict no request-level ladder could
|
|
91
|
+
* see — there is no `error` frame to react to. This deadline turns the same
|
|
92
|
+
* silence into an ordinary retryable failure INSIDE one attempt, which is the
|
|
93
|
+
* only layer able to act on it.
|
|
94
|
+
*
|
|
95
|
+
* 45 s, NOT 180 s AND NOT THE WATCHDOG WINDOW. Measured against the user's own
|
|
96
|
+
* budget (`maxRetries: 10`, `maxElapsedMs: 240 000`): 210 s buys two attempts
|
|
97
|
+
* and a 633 s wall clock — worse than the bug — while 45–60 s buys 4–6
|
|
98
|
+
* attempts and starts repairing more than two minutes earlier. Silence ALSO
|
|
99
|
+
* counts towards `maxElapsedMs` (see `decide`), so this is a budget decision
|
|
100
|
+
* and not a taste one. Table: the diagnosis' §5.2.
|
|
101
|
+
*/
|
|
102
|
+
readSilenceMs?: number;
|
|
80
103
|
}
|
|
81
104
|
export declare const DEFAULT_RETRY_POLICY: RetryPolicy;
|
|
105
|
+
/**
|
|
106
|
+
* The read-side silence window a policy that says nothing gets: **45 s**, and
|
|
107
|
+
* DELIBERATELY BELOW core's own `DEFAULT_IDLE_TIMEOUT` (60 s), because the
|
|
108
|
+
* deadline must fire before the watchdog or the ladder never gets a turn. A host
|
|
109
|
+
* with a WIDER window keeps 45 s; a host with a NARROWER one has it narrowed
|
|
110
|
+
* further by `resolveReadSilenceMs`, never left behind.
|
|
111
|
+
*
|
|
112
|
+
* NOT a field of `DEFAULT_RETRY_POLICY`, and that is load-bearing (F1′).
|
|
113
|
+
* `DEFAULT_RETRY_POLICY` is PUBLIC and is compared WHOLE: the CLI's
|
|
114
|
+
* `retry-config.test.ts` pins `toRetryPolicy(DEFAULT_RETRY_CONFIG)` deep-equal to
|
|
115
|
+
* it, and that transform copies the eight keys a user can actually configure. A
|
|
116
|
+
* ninth key here would make every host-built policy unequal to this object for a
|
|
117
|
+
* value the host cannot express — so the default lives here, and `normalizePolicy`
|
|
118
|
+
* folds it in at the one place a policy is made ready to run.
|
|
119
|
+
*/
|
|
120
|
+
export declare const DEFAULT_READ_SILENCE_MS = 45000;
|
|
82
121
|
/**
|
|
83
122
|
* The events that mean "the consumer has already rendered answer content", i.e.
|
|
84
123
|
* the commit point (§4.5a).
|
|
@@ -91,6 +130,21 @@ export declare const DEFAULT_RETRY_POLICY: RetryPolicy;
|
|
|
91
130
|
* variants are ours and never round-trip through `makeStream()`.
|
|
92
131
|
*/
|
|
93
132
|
export declare const CONTENT_EVENTS: ReadonlySet<StreamEvent['type']>;
|
|
133
|
+
/**
|
|
134
|
+
* The window ONE attempt may stay silent, given the host's idle-watchdog window.
|
|
135
|
+
*
|
|
136
|
+
* TWO BOUNDS, ONE NUMBER. The policy states the intent (45 s by default); the
|
|
137
|
+
* watchdog states the hard ceiling, because a deadline that fires AFTER the
|
|
138
|
+
* watchdog has already aborted the run is not a recovery mechanism — it is the
|
|
139
|
+
* same death with extra steps. 60 % of the window leaves the remaining 40 % for
|
|
140
|
+
* the backoff and the next attempt to land before the watchdog's own timer would
|
|
141
|
+
* have expired; each retry re-kicks that timer as well.
|
|
142
|
+
*
|
|
143
|
+
* Returns `0` — "no deadline" — for a policy that opts out or has the ladder
|
|
144
|
+
* switched off (`maxRetries <= 0`), so an opted-out embedder stays byte-identical
|
|
145
|
+
* to a build that never had this feature.
|
|
146
|
+
*/
|
|
147
|
+
export declare function resolveReadSilenceMs(policy: RetryPolicy | null | undefined, watchdogWindowMs?: number): number;
|
|
94
148
|
/**
|
|
95
149
|
* Clamp a policy to `RETRY_LIMITS`. Pure, total, and it NEVER THROWS — a bad
|
|
96
150
|
* policy is corrected rather than rejected, because the alternative is a
|
|
@@ -251,6 +305,31 @@ export interface WithRetryOptions {
|
|
|
251
305
|
escalation?: EscalationPolicy | null;
|
|
252
306
|
/** Tier to start from. Default 1; `> 1` only changes the pace AFTER a failure. */
|
|
253
307
|
startRung?: RetryTierId;
|
|
308
|
+
/**
|
|
309
|
+
* The host's idle-watchdog window, used ONLY to bound the read deadline
|
|
310
|
+
* (`resolveReadSilenceMs`): a deadline that fires after the watchdog has already
|
|
311
|
+
* killed the run recovers nothing. Absent = the policy's own number.
|
|
312
|
+
*/
|
|
313
|
+
watchdogWindowMs?: number;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* What ONE attempt may use to end itself (F1′, `agent-idle-watchdog-no-recovery`).
|
|
317
|
+
*
|
|
318
|
+
* `readDeadline` is DERIVED, PER-ATTEMPT and DELIBERATELY NOT `request.signal`. Every
|
|
319
|
+
* adapter reads an aborted `request.signal` as “the user pressed Esc” and returns
|
|
320
|
+
* SILENTLY, so a deadline delivered that way would reproduce the bug it exists to
|
|
321
|
+
* fix. It aborts for two reasons only — the caller aborted (so Esc still means Esc)
|
|
322
|
+
* and genuine silence — and an adapter that honours it ends the read the way a
|
|
323
|
+
* truncation does, which is a channel the ladder already handles.
|
|
324
|
+
*
|
|
325
|
+
* `onFrame` reports raw WIRE liveness. A frame that produces no `StreamEvent`
|
|
326
|
+
* (`ping`, `message_start`, `message_delta`, a text/thinking `content_block_stop`)
|
|
327
|
+
* is still evidence the connection is alive; without it a relay that forwards pings
|
|
328
|
+
* while swallowing deltas would be killed for a stall that is not happening.
|
|
329
|
+
*/
|
|
330
|
+
export interface RetryAttemptControls {
|
|
331
|
+
readDeadline?: AbortSignal;
|
|
332
|
+
onFrame: () => void;
|
|
254
333
|
}
|
|
255
334
|
/**
|
|
256
335
|
* Wrap a stream factory with retry.
|
|
@@ -289,5 +368,5 @@ export interface WithRetryOptions {
|
|
|
289
368
|
* `retryable` field and any downstream `instanceof` continue to behave. Only
|
|
290
369
|
* the retry COUNT is new information, and it travels on the events.
|
|
291
370
|
*/
|
|
292
|
-
export declare function withRetry(makeStream: () => AsyncIterableIterator<StreamEvent>, opts: WithRetryOptions): AsyncIterableIterator<StreamEvent>;
|
|
371
|
+
export declare function withRetry(makeStream: (attempt?: RetryAttemptControls) => AsyncIterableIterator<StreamEvent>, opts: WithRetryOptions): AsyncIterableIterator<StreamEvent>;
|
|
293
372
|
export {};
|
package/dist/llm/retry.js
CHANGED
|
@@ -98,6 +98,22 @@ export const DEFAULT_RETRY_POLICY = {
|
|
|
98
98
|
maxElapsedMs: 240_000,
|
|
99
99
|
onPartialStream: true,
|
|
100
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* The read-side silence window a policy that says nothing gets: **45 s**, and
|
|
103
|
+
* DELIBERATELY BELOW core's own `DEFAULT_IDLE_TIMEOUT` (60 s), because the
|
|
104
|
+
* deadline must fire before the watchdog or the ladder never gets a turn. A host
|
|
105
|
+
* with a WIDER window keeps 45 s; a host with a NARROWER one has it narrowed
|
|
106
|
+
* further by `resolveReadSilenceMs`, never left behind.
|
|
107
|
+
*
|
|
108
|
+
* NOT a field of `DEFAULT_RETRY_POLICY`, and that is load-bearing (F1′).
|
|
109
|
+
* `DEFAULT_RETRY_POLICY` is PUBLIC and is compared WHOLE: the CLI's
|
|
110
|
+
* `retry-config.test.ts` pins `toRetryPolicy(DEFAULT_RETRY_CONFIG)` deep-equal to
|
|
111
|
+
* it, and that transform copies the eight keys a user can actually configure. A
|
|
112
|
+
* ninth key here would make every host-built policy unequal to this object for a
|
|
113
|
+
* value the host cannot express — so the default lives here, and `normalizePolicy`
|
|
114
|
+
* folds it in at the one place a policy is made ready to run.
|
|
115
|
+
*/
|
|
116
|
+
export const DEFAULT_READ_SILENCE_MS = 45_000;
|
|
101
117
|
/**
|
|
102
118
|
* The events that mean "the consumer has already rendered answer content", i.e.
|
|
103
119
|
* the commit point (§4.5a).
|
|
@@ -117,6 +133,39 @@ export const CONTENT_EVENTS = new Set([
|
|
|
117
133
|
'tool_call_delta',
|
|
118
134
|
'tool_call_end',
|
|
119
135
|
]);
|
|
136
|
+
/**
|
|
137
|
+
* The window ONE attempt may stay silent, given the host's idle-watchdog window.
|
|
138
|
+
*
|
|
139
|
+
* TWO BOUNDS, ONE NUMBER. The policy states the intent (45 s by default); the
|
|
140
|
+
* watchdog states the hard ceiling, because a deadline that fires AFTER the
|
|
141
|
+
* watchdog has already aborted the run is not a recovery mechanism — it is the
|
|
142
|
+
* same death with extra steps. 60 % of the window leaves the remaining 40 % for
|
|
143
|
+
* the backoff and the next attempt to land before the watchdog's own timer would
|
|
144
|
+
* have expired; each retry re-kicks that timer as well.
|
|
145
|
+
*
|
|
146
|
+
* Returns `0` — "no deadline" — for a policy that opts out or has the ladder
|
|
147
|
+
* switched off (`maxRetries <= 0`), so an opted-out embedder stays byte-identical
|
|
148
|
+
* to a build that never had this feature.
|
|
149
|
+
*/
|
|
150
|
+
export function resolveReadSilenceMs(policy, watchdogWindowMs) {
|
|
151
|
+
if (!policy || policy.maxRetries <= 0)
|
|
152
|
+
return 0;
|
|
153
|
+
// `0` IS A VALUE, not an absence: it is how a caller says "no read deadline",
|
|
154
|
+
// and reading it as "unset" would hand back the default and silently re-enable
|
|
155
|
+
// exactly what the caller switched off.
|
|
156
|
+
const configured = policy.readSilenceMs;
|
|
157
|
+
const wanted = typeof configured === 'number' && Number.isFinite(configured)
|
|
158
|
+
? Math.max(0, configured)
|
|
159
|
+
: DEFAULT_READ_SILENCE_MS;
|
|
160
|
+
if (wanted <= 0)
|
|
161
|
+
return 0;
|
|
162
|
+
if (typeof watchdogWindowMs === 'number' &&
|
|
163
|
+
Number.isFinite(watchdogWindowMs) &&
|
|
164
|
+
watchdogWindowMs > 0) {
|
|
165
|
+
return Math.max(RETRY_LIMITS.minDelayMs, Math.min(wanted, Math.floor(watchdogWindowMs * 0.6)));
|
|
166
|
+
}
|
|
167
|
+
return wanted;
|
|
168
|
+
}
|
|
120
169
|
// ---------------------------------------------------------------------------
|
|
121
170
|
// normalizePolicy
|
|
122
171
|
// ---------------------------------------------------------------------------
|
|
@@ -161,6 +210,11 @@ export function normalizePolicy(policy) {
|
|
|
161
210
|
respectRetryAfter: policy.respectRetryAfter !== false,
|
|
162
211
|
maxElapsedMs,
|
|
163
212
|
onPartialStream: policy.onPartialStream !== false,
|
|
213
|
+
// `0` is a MEANING ("no read deadline"), never a typo: only a negative or
|
|
214
|
+
// non-finite value falls back to the default.
|
|
215
|
+
readSilenceMs: typeof policy.readSilenceMs === 'number' && Number.isFinite(policy.readSilenceMs)
|
|
216
|
+
? Math.max(0, Math.floor(policy.readSilenceMs))
|
|
217
|
+
: DEFAULT_READ_SILENCE_MS,
|
|
164
218
|
};
|
|
165
219
|
}
|
|
166
220
|
const DEFAULT_TIER = {
|
|
@@ -593,6 +647,107 @@ async function* announceAndWait(input, sleep, signal) {
|
|
|
593
647
|
: {}),
|
|
594
648
|
};
|
|
595
649
|
}
|
|
650
|
+
function createAttemptDeadline(caller, readSilenceMs) {
|
|
651
|
+
if (readSilenceMs <= 0) {
|
|
652
|
+
return {
|
|
653
|
+
controls: undefined,
|
|
654
|
+
onFrame: () => { },
|
|
655
|
+
fired: false,
|
|
656
|
+
whenSilent: undefined,
|
|
657
|
+
dispose: () => { },
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
const controller = new AbortController();
|
|
661
|
+
const onCallerAbort = () => controller.abort();
|
|
662
|
+
caller?.addEventListener('abort', onCallerAbort, { once: true });
|
|
663
|
+
if (caller?.aborted === true)
|
|
664
|
+
controller.abort();
|
|
665
|
+
let timer;
|
|
666
|
+
/** The deadline fired, as opposed to the caller aborting. */
|
|
667
|
+
let fired = false;
|
|
668
|
+
let announceSilence = () => { };
|
|
669
|
+
// ONE promise for the whole attempt, not one per event: a long stream would
|
|
670
|
+
// otherwise leave a pending promise behind for every frame it produced.
|
|
671
|
+
const whenSilent = new Promise((resolve) => {
|
|
672
|
+
announceSilence = resolve;
|
|
673
|
+
});
|
|
674
|
+
const arm = () => {
|
|
675
|
+
if (controller.signal.aborted)
|
|
676
|
+
return;
|
|
677
|
+
if (timer !== undefined)
|
|
678
|
+
clearTimeout(timer);
|
|
679
|
+
timer = setTimeout(() => {
|
|
680
|
+
fired = true;
|
|
681
|
+
// Abort FIRST, then announce: an adapter that honours the signal gets to
|
|
682
|
+
// emit its own truncation error before the race resolves.
|
|
683
|
+
controller.abort();
|
|
684
|
+
announceSilence();
|
|
685
|
+
}, readSilenceMs);
|
|
686
|
+
};
|
|
687
|
+
arm();
|
|
688
|
+
const controls = { readDeadline: controller.signal, onFrame: arm };
|
|
689
|
+
return {
|
|
690
|
+
controls,
|
|
691
|
+
onFrame: arm,
|
|
692
|
+
get fired() {
|
|
693
|
+
return fired && caller?.aborted !== true;
|
|
694
|
+
},
|
|
695
|
+
whenSilent,
|
|
696
|
+
dispose: () => {
|
|
697
|
+
if (timer !== undefined) {
|
|
698
|
+
clearTimeout(timer);
|
|
699
|
+
timer = undefined;
|
|
700
|
+
}
|
|
701
|
+
caller?.removeEventListener('abort', onCallerAbort);
|
|
702
|
+
},
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Iterate `source` while enforcing the attempt's silence window — and enforce it
|
|
707
|
+
* HERE, not only in the adapters.
|
|
708
|
+
*
|
|
709
|
+
* A read deadline the adapter ignores would leave `withRetry` awaiting `next()`
|
|
710
|
+
* forever, so silence would still kill the RUN and never reach `decide()`. Racing
|
|
711
|
+
* `next()` against `whenSilent` makes the recovery a property of the ladder, which
|
|
712
|
+
* is what the user asked for, and not a property of three hand-written adapters.
|
|
713
|
+
*
|
|
714
|
+
* `source.return()` runs on every exit path, so a pending read is released and the
|
|
715
|
+
* SSE reader is not leaked when the deadline wins.
|
|
716
|
+
*/
|
|
717
|
+
async function* guardSilence(source, deadline) {
|
|
718
|
+
const whenSilent = deadline.whenSilent;
|
|
719
|
+
if (!whenSilent) {
|
|
720
|
+
yield* source;
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
try {
|
|
724
|
+
for (;;) {
|
|
725
|
+
const pending = source.next();
|
|
726
|
+
// Abandoned when the deadline wins: without this an adapter rejecting later
|
|
727
|
+
// would surface as an unhandled rejection with no user-visible cause.
|
|
728
|
+
pending.catch(() => { });
|
|
729
|
+
const step = await Promise.race([
|
|
730
|
+
pending.then((result) => ({ kind: 'event', result })),
|
|
731
|
+
whenSilent.then(() => ({ kind: 'silence' })),
|
|
732
|
+
]);
|
|
733
|
+
if (step.kind === 'silence')
|
|
734
|
+
return;
|
|
735
|
+
if (step.result.done === true)
|
|
736
|
+
return;
|
|
737
|
+
deadline.onFrame();
|
|
738
|
+
yield step.result.value;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
finally {
|
|
742
|
+
// NOT AWAITED, and that is load-bearing. A generator suspended inside an
|
|
743
|
+
// `await` does not settle `return()` until that await does, so awaiting it here
|
|
744
|
+
// would hang on exactly the provider this guard exists for — one that ignores
|
|
745
|
+
// the deadline. Firing it is still worth doing: a real adapter's read is already
|
|
746
|
+
// rejecting because the deadline aborted it, so its `finally` releases the reader
|
|
747
|
+
// on the next tick. The rejection is swallowed because nobody is left to read it.
|
|
748
|
+
void source.return?.(undefined)?.catch(() => { });
|
|
749
|
+
}
|
|
750
|
+
}
|
|
596
751
|
/**
|
|
597
752
|
* Wrap a stream factory with retry.
|
|
598
753
|
*
|
|
@@ -639,6 +794,9 @@ export async function* withRetry(makeStream, opts) {
|
|
|
639
794
|
const random = opts.random ?? Math.random;
|
|
640
795
|
const signal = opts.signal;
|
|
641
796
|
const withTier = escalation !== null;
|
|
797
|
+
// THE READ DEADLINE, resolved once: “a request that has said nothing for N ms is a
|
|
798
|
+
// transport failure, hand it to the ladder” (F1′). `0` = the caller opted out.
|
|
799
|
+
const readSilenceMs = resolveReadSilenceMs(policy, opts.watchdogWindowMs);
|
|
642
800
|
/** Clock for the WHOLE ladder (only `totalBudgetMs` reads it). */
|
|
643
801
|
let firstFailureAtLadder;
|
|
644
802
|
/** Clock for the CURRENT tier (only `decide`'s budget reads it). */
|
|
@@ -655,28 +813,45 @@ export async function* withRetry(makeStream, opts) {
|
|
|
655
813
|
const toolCallIds = [];
|
|
656
814
|
let sawDone = false;
|
|
657
815
|
let terminalError;
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
816
|
+
// A FRESH deadline per attempt: a silence that ended attempt N must not be
|
|
817
|
+
// visible to attempt N+1, or the next one would look pre-cancelled.
|
|
818
|
+
const deadline = createAttemptDeadline(signal, readSilenceMs);
|
|
819
|
+
try {
|
|
820
|
+
// `guardSilence` owns the per-event re-arm, so a frame that arrives as a
|
|
821
|
+
// `StreamEvent` and a frame that only re-armed the wire both count.
|
|
822
|
+
for await (const event of guardSilence(makeStream(deadline.controls), deadline)) {
|
|
823
|
+
if (signal?.aborted === true)
|
|
824
|
+
return; // G1
|
|
825
|
+
if (CONTENT_EVENTS.has(event.type))
|
|
826
|
+
committed = true;
|
|
827
|
+
if (event.type === 'tool_call_start')
|
|
828
|
+
toolCallIds.push(event.toolCallId);
|
|
829
|
+
if (event.type === 'error') {
|
|
830
|
+
// NOT FORWARDED YET: whether the consumer ever sees it depends on the
|
|
831
|
+
// decision below. Breaking here also calls the source iterator's
|
|
832
|
+
// `return()`, which runs its `finally` and releases the SSE reader.
|
|
833
|
+
terminalError = event.error;
|
|
834
|
+
break;
|
|
835
|
+
}
|
|
836
|
+
if (event.type === 'done')
|
|
837
|
+
sawDone = true;
|
|
838
|
+
yield event;
|
|
671
839
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
840
|
+
}
|
|
841
|
+
finally {
|
|
842
|
+
deadline.dispose();
|
|
675
843
|
}
|
|
676
844
|
if (Boolean(signal?.aborted))
|
|
677
845
|
return;
|
|
678
846
|
if (sawDone)
|
|
679
847
|
return; // success; the hot path ends here
|
|
848
|
+
// THE DEADLINE SPEAKS AS A PROVIDER WOULD (F1′). An adapter that honours
|
|
849
|
+
// `readDeadline` has already produced its own truncation error above; this
|
|
850
|
+
// covers a provider that ignored the field, so a silence still reaches
|
|
851
|
+
// `decide()` as an ordinary retryable failure instead of escaping the generator.
|
|
852
|
+
if (terminalError === undefined && deadline.fired) {
|
|
853
|
+
terminalError = new LLMError(`${opts.providerId}/${opts.modelId} stream was silent for ${readSilenceMs} ms`, opts.providerId, 'timeout', true);
|
|
854
|
+
}
|
|
680
855
|
if (terminalError === undefined) {
|
|
681
856
|
// Defensive only: with the adapters' terminal-sentinel contract in place
|
|
682
857
|
// (§4.5a) every adapter terminates in `done` or `error`. Reaching here
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aragon-agent/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.89",
|
|
4
4
|
"description": "AragonAgent core engine — LLM provider adapters, streaming, tool registry/execution, steering, idle watchdog, and an optional isolated-vm sandbox.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "AragonAgent contributors",
|