@zenera/neo 1.3.35 → 1.3.37
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/events.d.ts +20 -1
- package/dist/failure.js +1 -1
- package/dist/kernel.d.ts +8 -1
- package/dist/kernel.js +15 -0
- package/dist/models/factory.js +4 -2
- package/dist/runner.d.ts +22 -1
- package/dist/runner.js +121 -30
- package/dist/state.d.ts +21 -1
- package/package.json +2 -2
package/dist/events.d.ts
CHANGED
|
@@ -95,7 +95,26 @@ export type CheckpointEvent = EventBase & ({
|
|
|
95
95
|
state: AgentState;
|
|
96
96
|
result: RunResult<unknown>;
|
|
97
97
|
});
|
|
98
|
-
|
|
98
|
+
/**
|
|
99
|
+
* What the runner says while a provider keeps failing a model call it is
|
|
100
|
+
* about to make again (see `RunnerOptions.modelRetry`). No state, so like a
|
|
101
|
+
* delta it is safe to drop.
|
|
102
|
+
*/
|
|
103
|
+
export type RetryNotice = {
|
|
104
|
+
type: 'model_retry';
|
|
105
|
+
/** the attempt that failed, from 1 */
|
|
106
|
+
attempt: number;
|
|
107
|
+
error: string;
|
|
108
|
+
status?: number;
|
|
109
|
+
/** when the next attempt goes, ISO */
|
|
110
|
+
retryAt: string;
|
|
111
|
+
model: string;
|
|
112
|
+
} | {
|
|
113
|
+
type: 'model_retry_recovered';
|
|
114
|
+
attempts: number;
|
|
115
|
+
};
|
|
116
|
+
export type RetryEvent = EventBase & RetryNotice;
|
|
117
|
+
export type AgentEvent = StreamEvent | CheckpointEvent | RetryEvent;
|
|
99
118
|
export interface PendingToolCall {
|
|
100
119
|
callId: string;
|
|
101
120
|
name: string;
|
package/dist/failure.js
CHANGED
|
@@ -69,7 +69,7 @@ export async function called(site, run) {
|
|
|
69
69
|
}
|
|
70
70
|
catch (err) {
|
|
71
71
|
// The SDK's own retries are inside this: an OpenAI or Vertex client
|
|
72
|
-
// gives up only after
|
|
72
|
+
// gives up only after a few of them, so the elapsed time is the only
|
|
73
73
|
// report of what the wait was spent on.
|
|
74
74
|
throw failed(site, err, { attempts: 1, elapsedMs: Date.now() - started });
|
|
75
75
|
}
|
package/dist/kernel.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { MemoryOpSpec, MemoryRecallSpec } from './memory/types.ts';
|
|
|
6
6
|
import type { ModelRequest, ModelResponse } from './model.ts';
|
|
7
7
|
import type { Services } from './services.ts';
|
|
8
8
|
import { type Skill } from './skills.ts';
|
|
9
|
-
import type { AgentState } from './state.ts';
|
|
9
|
+
import type { AgentState, Stall } from './state.ts';
|
|
10
10
|
import { type CompactionNode, type FinalOutputNode, type UserInputNode } from './trajectory.ts';
|
|
11
11
|
import { type AnyTool, type Input, type SuspendSpec, type TokenUsage, type ToolOutcome } from './types.ts';
|
|
12
12
|
/**
|
|
@@ -170,6 +170,13 @@ export interface CompactionSpec {
|
|
|
170
170
|
*/
|
|
171
171
|
export declare function applyCompaction(state: AgentState, spec: CompactionSpec, env: KernelEnv): Promise<AgentState>;
|
|
172
172
|
export declare function applyFailure(state: AgentState, error: string): AgentState;
|
|
173
|
+
/**
|
|
174
|
+
* The run stops before a model call its provider kept refusing, still able to
|
|
175
|
+
* go on: unlike `applyFailure` the phase stays, so the call is the next step.
|
|
176
|
+
*/
|
|
177
|
+
export declare function applyStall(state: AgentState, stall: Stall): AgentState;
|
|
178
|
+
/** The run goes on from a stall: the call is about to be made again. */
|
|
179
|
+
export declare function clearStall(state: AgentState): AgentState;
|
|
173
180
|
/**
|
|
174
181
|
* Derives a child state seeded with *real nodes* copied from the parent, not a
|
|
175
182
|
* frozen `Message[]` blob: the branch can compact its own inherited context,
|
package/dist/kernel.js
CHANGED
|
@@ -771,6 +771,21 @@ export async function applyCompaction(state, spec, env) {
|
|
|
771
771
|
export function applyFailure(state, error) {
|
|
772
772
|
return { ...state, phase: 'failed', error };
|
|
773
773
|
}
|
|
774
|
+
/**
|
|
775
|
+
* The run stops before a model call its provider kept refusing, still able to
|
|
776
|
+
* go on: unlike `applyFailure` the phase stays, so the call is the next step.
|
|
777
|
+
*/
|
|
778
|
+
export function applyStall(state, stall) {
|
|
779
|
+
return { ...state, stalled: stall, error: stall.error };
|
|
780
|
+
}
|
|
781
|
+
/** The run goes on from a stall: the call is about to be made again. */
|
|
782
|
+
export function clearStall(state) {
|
|
783
|
+
if (!state.stalled) {
|
|
784
|
+
return state;
|
|
785
|
+
}
|
|
786
|
+
const { stalled: _stalled, error: _error, ...rest } = state;
|
|
787
|
+
return rest;
|
|
788
|
+
}
|
|
774
789
|
// ---------------------------------------------------------------------------
|
|
775
790
|
// Fork / join
|
|
776
791
|
// ---------------------------------------------------------------------------
|
package/dist/models/factory.js
CHANGED
|
@@ -615,9 +615,11 @@ export class ModelRegistry {
|
|
|
615
615
|
*
|
|
616
616
|
* Retries live at the transport, not around `Model.generate`, so a retried call
|
|
617
617
|
* is one that never started: no partial stream is replayed, and a `Retry-After`
|
|
618
|
-
* header is honoured by the SDK that read it.
|
|
618
|
+
* header is honoured by the SDK that read it. They are few and quick, for a
|
|
619
|
+
* blip; a provider that stays down is the runner's to wait for
|
|
620
|
+
* (`RunnerOptions.modelRetry`), where the wait can be shown and stopped.
|
|
619
621
|
*/
|
|
620
|
-
const DEFAULT_MAX_RETRIES =
|
|
622
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
621
623
|
/** wait after the first failure; doubles per attempt up to `RETRY_MAX_MS` */
|
|
622
624
|
const RETRY_INITIAL_MS = 1_000;
|
|
623
625
|
const RETRY_MAX_MS = 60_000;
|
package/dist/runner.d.ts
CHANGED
|
@@ -69,6 +69,26 @@ export interface RunnerOptions<TCtx = unknown> {
|
|
|
69
69
|
* answer was streamed yet; an abort is never offered.
|
|
70
70
|
*/
|
|
71
71
|
onModelError?: ModelErrorHandler;
|
|
72
|
+
/**
|
|
73
|
+
* How long a model call that keeps failing for a reason that may pass (a
|
|
74
|
+
* 5xx, a dropped connection, a 429) is made again before the run stops.
|
|
75
|
+
* Each wait is announced with a `model_retry` event. A run that runs out of
|
|
76
|
+
* patience stops `failed` with `state.stalled` set, at that call, so
|
|
77
|
+
* `runner.resume` makes it again.
|
|
78
|
+
*/
|
|
79
|
+
modelRetry?: ModelRetryPolicy;
|
|
80
|
+
}
|
|
81
|
+
/** See `RunnerOptions.modelRetry`. */
|
|
82
|
+
export interface ModelRetryPolicy {
|
|
83
|
+
/**
|
|
84
|
+
* how long a call may keep failing, from its first attempt (default 15
|
|
85
|
+
* minutes); 0: neither retried nor stalled, the error ends the run
|
|
86
|
+
*/
|
|
87
|
+
windowMs?: number;
|
|
88
|
+
/** the wait after the first failure, doubling after each (default 5 s) */
|
|
89
|
+
initialMs?: number;
|
|
90
|
+
/** the longest wait between attempts (default 60 s) */
|
|
91
|
+
maxMs?: number;
|
|
72
92
|
}
|
|
73
93
|
/** What `onModelError` is told about the call that failed. */
|
|
74
94
|
export interface ModelErrorContext {
|
|
@@ -127,7 +147,8 @@ export declare class AgentRunner<TCtx = unknown> {
|
|
|
127
147
|
run<T = string>(agent: Agent<TCtx> | string, input?: Input, opts?: RunOptions<T, TCtx>): RunStream<T>;
|
|
128
148
|
/**
|
|
129
149
|
* Continues a persisted run from its own snapshot. A run that stopped
|
|
130
|
-
* `suspended` continues once `answers` answer its suspended calls
|
|
150
|
+
* `suspended` continues once `answers` answer its suspended calls; one that
|
|
151
|
+
* stalled at a model call (`state.stalled`) makes that call again.
|
|
131
152
|
*/
|
|
132
153
|
resume<T = string>(state: AgentState, opts?: ResumeOptions<T, TCtx>): RunStream<T>;
|
|
133
154
|
/**
|
package/dist/runner.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Agent, AgentRegistry } from "./agent.js";
|
|
2
2
|
import { describeArchitecture } from "./architecture.js";
|
|
3
|
+
import { classify, retryAfterMs, statusOf } from "./embeddings/rate-limit.js";
|
|
3
4
|
import { EventQueue, RunStream, } from "./events.js";
|
|
4
5
|
import { systemClock } from "./ids.js";
|
|
5
6
|
import * as Kernel from "./kernel.js";
|
|
@@ -51,6 +52,11 @@ export function modelSummarizer(model, instructions = SUMMARY_INSTRUCTIONS) {
|
|
|
51
52
|
},
|
|
52
53
|
};
|
|
53
54
|
}
|
|
55
|
+
const DEFAULT_MODEL_RETRY = {
|
|
56
|
+
windowMs: 15 * 60_000,
|
|
57
|
+
initialMs: 5_000,
|
|
58
|
+
maxMs: 60_000,
|
|
59
|
+
};
|
|
54
60
|
/** What a new message says to a call still waiting for a person. */
|
|
55
61
|
export const SUPERSEDED_MESSAGE = 'cancelled: the user sent a new message instead of answering this; continue without it';
|
|
56
62
|
// ---------------------------------------------------------------------------
|
|
@@ -76,11 +82,13 @@ export class AgentRunner {
|
|
|
76
82
|
#recordRequests;
|
|
77
83
|
#clock;
|
|
78
84
|
#onModelError;
|
|
85
|
+
#retry;
|
|
79
86
|
constructor(opts = {}) {
|
|
80
87
|
this.#model = opts.model;
|
|
81
88
|
this.#context = opts.context;
|
|
82
89
|
this.#clock = opts.clock ?? systemClock;
|
|
83
90
|
this.#onModelError = opts.onModelError;
|
|
91
|
+
this.#retry = { ...DEFAULT_MODEL_RETRY, ...opts.modelRetry };
|
|
84
92
|
this.services =
|
|
85
93
|
opts.services ??
|
|
86
94
|
new Services({
|
|
@@ -132,7 +140,8 @@ export class AgentRunner {
|
|
|
132
140
|
}
|
|
133
141
|
/**
|
|
134
142
|
* Continues a persisted run from its own snapshot. A run that stopped
|
|
135
|
-
* `suspended` continues once `answers` answer its suspended calls
|
|
143
|
+
* `suspended` continues once `answers` answer its suspended calls; one that
|
|
144
|
+
* stalled at a model call (`state.stalled`) makes that call again.
|
|
136
145
|
*/
|
|
137
146
|
resume(state, opts = {}) {
|
|
138
147
|
return new RunStream(this.#resumeAndDrive(state, opts));
|
|
@@ -222,7 +231,8 @@ export class AgentRunner {
|
|
|
222
231
|
Kernel.checkOutputSchema(initial, env);
|
|
223
232
|
const branch = opts.branch;
|
|
224
233
|
const signal = opts.signal;
|
|
225
|
-
|
|
234
|
+
// A run that stalled at a model call goes on from it (a retry, or a new message).
|
|
235
|
+
let state = Kernel.clearStall(initial);
|
|
226
236
|
for (;;) {
|
|
227
237
|
if (signal?.aborted) {
|
|
228
238
|
return yield* this.#finish(state, branch, 'aborted');
|
|
@@ -243,37 +253,84 @@ export class AgentRunner {
|
|
|
243
253
|
const req = await Kernel.buildRequest(state, this.registry, env);
|
|
244
254
|
const digest = Kernel.requestDigest(req);
|
|
245
255
|
const model = this.#modelFor(state);
|
|
246
|
-
const
|
|
247
|
-
let
|
|
248
|
-
const call = (this.#stream && model.stream
|
|
249
|
-
? model.stream({ ...req, signal }, (d) => {
|
|
250
|
-
streamed++;
|
|
251
|
-
deltas.push(tag(state, branch, d));
|
|
252
|
-
})
|
|
253
|
-
: model.generate({ ...req, signal })).finally(() => deltas.close());
|
|
254
|
-
// Deltas arrive in a callback, which a generator cannot
|
|
255
|
-
// yield from; draining the queue while the call is in
|
|
256
|
-
// flight bridges the two.
|
|
257
|
-
yield* deltas.drain();
|
|
256
|
+
const firstTry = Date.now();
|
|
257
|
+
let attempt = 1;
|
|
258
258
|
let res;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
:
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
259
|
+
while (!res) {
|
|
260
|
+
const deltas = new EventQueue();
|
|
261
|
+
let streamed = 0;
|
|
262
|
+
const call = (this.#stream && model.stream
|
|
263
|
+
? model.stream({ ...req, signal }, (d) => {
|
|
264
|
+
streamed++;
|
|
265
|
+
deltas.push(tag(state, branch, d));
|
|
266
|
+
})
|
|
267
|
+
: model.generate({ ...req, signal })).finally(() => deltas.close());
|
|
268
|
+
// Deltas arrive in a callback, which a generator cannot
|
|
269
|
+
// yield from; draining the queue while the call is in
|
|
270
|
+
// flight bridges the two.
|
|
271
|
+
yield* deltas.drain();
|
|
272
|
+
try {
|
|
273
|
+
res = await call;
|
|
274
|
+
}
|
|
275
|
+
catch (err) {
|
|
276
|
+
// Nothing of the answer is on screen yet, and the
|
|
277
|
+
// provider may be back soon: the same call again,
|
|
278
|
+
// after telling whoever watches.
|
|
279
|
+
const retryable = !streamed && !signal?.aborted && classify(err) !== 'fatal';
|
|
280
|
+
const delay = retryable
|
|
281
|
+
? nextRetryDelay(this.#retry, attempt, err)
|
|
282
|
+
: undefined;
|
|
283
|
+
if (delay !== undefined &&
|
|
284
|
+
Date.now() - firstTry + delay <= this.#retry.windowMs) {
|
|
285
|
+
yield tag(state, branch, {
|
|
286
|
+
type: 'model_retry',
|
|
287
|
+
attempt,
|
|
288
|
+
error: errorText(err),
|
|
289
|
+
status: statusOf(err),
|
|
290
|
+
retryAt: new Date(Date.now() + delay).toISOString(),
|
|
291
|
+
model: model.id,
|
|
292
|
+
});
|
|
293
|
+
await pause(delay, signal);
|
|
294
|
+
if (signal?.aborted) {
|
|
295
|
+
return yield* this.#finish(state, branch, 'aborted');
|
|
296
|
+
}
|
|
297
|
+
attempt++;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
// Waiting is only sound while nothing of the answer is
|
|
301
|
+
// on screen; otherwise the call would be shown twice.
|
|
302
|
+
const spec = branch || streamed || signal?.aborted
|
|
303
|
+
? undefined
|
|
304
|
+
: await this.#onModelError?.(err, {
|
|
305
|
+
callId: modelCallId(state),
|
|
306
|
+
model: model.id,
|
|
307
|
+
state,
|
|
308
|
+
});
|
|
309
|
+
if (spec) {
|
|
310
|
+
state = Kernel.applyModelSuspension(state, { callId: modelCallId(state), name: model.id }, spec, this.#clock.now());
|
|
311
|
+
return yield* this.#finish(state, branch, 'suspended');
|
|
312
|
+
}
|
|
313
|
+
// Out of patience with a provider that may yet come
|
|
314
|
+
// back: the run stops at this call, kept, so `retry`
|
|
315
|
+
// makes it again rather than the message being lost.
|
|
316
|
+
if (retryable && !branch && this.#retry.windowMs > 0) {
|
|
317
|
+
state = Kernel.applyStall(state, {
|
|
318
|
+
callId: modelCallId(state),
|
|
319
|
+
model: model.id,
|
|
320
|
+
at: this.#clock.now(),
|
|
321
|
+
attempts: attempt,
|
|
322
|
+
error: `${errorText(err)} (gave up after ${attempt} attempt${attempt === 1 ? '' : 's'} in ${elapsed(Date.now() - firstTry)})`,
|
|
323
|
+
});
|
|
324
|
+
return yield* this.#finish(state, branch, 'failed');
|
|
325
|
+
}
|
|
273
326
|
throw err;
|
|
274
327
|
}
|
|
275
|
-
|
|
276
|
-
|
|
328
|
+
}
|
|
329
|
+
if (attempt > 1) {
|
|
330
|
+
yield tag(state, branch, {
|
|
331
|
+
type: 'model_retry_recovered',
|
|
332
|
+
attempts: attempt,
|
|
333
|
+
});
|
|
277
334
|
}
|
|
278
335
|
const before = state;
|
|
279
336
|
state = await Kernel.applyLlmResponse(state, res, model.id, env, {
|
|
@@ -622,6 +679,40 @@ function tag(state, branch, event) {
|
|
|
622
679
|
function appended(before, after) {
|
|
623
680
|
return after.trajectory.slice(before.trajectory.length);
|
|
624
681
|
}
|
|
682
|
+
/**
|
|
683
|
+
* The wait before attempt `attempt + 1`: doubling from `initialMs` up to
|
|
684
|
+
* `maxMs`, ±20 % so runs that failed together do not come back together. A
|
|
685
|
+
* provider's own `Retry-After` wins when it asks for longer.
|
|
686
|
+
*/
|
|
687
|
+
function nextRetryDelay(policy, attempt, err) {
|
|
688
|
+
if (policy.windowMs <= 0) {
|
|
689
|
+
return undefined;
|
|
690
|
+
}
|
|
691
|
+
const base = Math.min(policy.initialMs * 2 ** (attempt - 1), policy.maxMs);
|
|
692
|
+
const jittered = Math.round(base * (0.8 + Math.random() * 0.4));
|
|
693
|
+
return Math.max(jittered, retryAfterMs(err) ?? 0);
|
|
694
|
+
}
|
|
695
|
+
/** Sleeps `ms`, or less when `signal` aborts. */
|
|
696
|
+
function pause(ms, signal) {
|
|
697
|
+
return new Promise((resolve) => {
|
|
698
|
+
if (signal?.aborted) {
|
|
699
|
+
resolve();
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const done = () => {
|
|
703
|
+
clearTimeout(timer);
|
|
704
|
+
signal?.removeEventListener('abort', done);
|
|
705
|
+
resolve();
|
|
706
|
+
};
|
|
707
|
+
const timer = setTimeout(done, ms);
|
|
708
|
+
signal?.addEventListener('abort', done, { once: true });
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
const errorText = (err) => (err instanceof Error ? err.message : String(err));
|
|
712
|
+
function elapsed(ms) {
|
|
713
|
+
const s = Math.round(ms / 1000);
|
|
714
|
+
return s < 60 ? `${s}s` : `${Math.floor(s / 60)}m${String(s % 60).padStart(2, '0')}s`;
|
|
715
|
+
}
|
|
625
716
|
/** The id a suspended model call goes by: stable until that call is made. */
|
|
626
717
|
function modelCallId(state) {
|
|
627
718
|
return `llm:${turns(state)}`;
|
package/dist/state.d.ts
CHANGED
|
@@ -66,8 +66,28 @@ export interface AgentState {
|
|
|
66
66
|
usage: TokenUsage;
|
|
67
67
|
/** serialized user context (must be JSON) */
|
|
68
68
|
context?: unknown;
|
|
69
|
-
/** set when phase === 'failed' */
|
|
69
|
+
/** set when phase === 'failed', and with `stalled` */
|
|
70
70
|
error?: string;
|
|
71
|
+
/**
|
|
72
|
+
* The model call the run stopped at because its provider kept failing for
|
|
73
|
+
* a reason that may pass (see `RunnerOptions.modelRetry`). The run is
|
|
74
|
+
* otherwise intact, still before that call: `runner.resume` makes it again,
|
|
75
|
+
* and a new message is answered from there.
|
|
76
|
+
*/
|
|
77
|
+
stalled?: Stall;
|
|
78
|
+
}
|
|
79
|
+
/** See `AgentState.stalled`. */
|
|
80
|
+
export interface Stall {
|
|
81
|
+
/** the model call's id, `llm:<n>` like a suspended one */
|
|
82
|
+
callId: string;
|
|
83
|
+
/** the model's id */
|
|
84
|
+
model: string;
|
|
85
|
+
/** when the run gave up, as the run's clock writes times */
|
|
86
|
+
at: string;
|
|
87
|
+
/** how many times the call was made */
|
|
88
|
+
attempts: number;
|
|
89
|
+
/** the last failure, and how long it was tried */
|
|
90
|
+
error: string;
|
|
71
91
|
}
|
|
72
92
|
export interface RunResult<T = string> {
|
|
73
93
|
/** `z.infer` of the output schema when one was given */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenera/neo",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.37",
|
|
4
4
|
"description": "A multi-agent runtime for Node.js: agents, models, tools, skills, memory and an append-only trajectory.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"prepack": "tsc -b"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@zenera/sandbox": "^1.3.
|
|
65
|
+
"@zenera/sandbox": "^1.3.37",
|
|
66
66
|
"graphology": "^0.26.0",
|
|
67
67
|
"yaml": "^2.9.1",
|
|
68
68
|
"zod": "^4.6.7"
|