@noetic-tools/core 5.22.0-next.3 → 5.22.0-next.5
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/harness/agent-harness.d.ts +34 -1
- package/dist/harness/agent-harness.js +166 -19
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/harness-options.d.ts +33 -1
- package/dist/harness/park-watchers.d.ts +3 -2
- package/dist/harness/thread-record.d.ts +11 -0
- package/dist/harness/thread-record.js +35 -2
- package/dist/harness/turn-cuts.d.ts +50 -0
- package/dist/harness/turn-cuts.js +108 -0
- package/dist/index.d.ts +4 -4
- package/dist/interpreter/acp-events.d.ts +7 -0
- package/dist/interpreter/acp-events.js +23 -1
- package/dist/interpreter/acp-resume.d.ts +1 -0
- package/dist/interpreter/acp-resume.js +2 -0
- package/dist/interpreter/execute-acp-agent.js +106 -22
- package/dist/runtime/acp-session-store.js +0 -0
- package/dist/runtime/durable/session-store.d.ts +5 -0
- package/dist/runtime/durable/session-store.js +12 -0
- package/dist/runtime/session-runner.d.ts +9 -1
- package/dist/runtime/session-runner.js +34 -0
- package/dist/util/error-message.d.ts +24 -0
- package/dist/util/error-message.js +94 -0
- package/package.json +2 -2
|
@@ -72,6 +72,12 @@ export declare class AgentHarness<TParams extends Record<string, unknown> = Reco
|
|
|
72
72
|
private readonly leaseOptions?;
|
|
73
73
|
/** The flow version this harness runs, when the host names one. */
|
|
74
74
|
private readonly version?;
|
|
75
|
+
/** Set with the lease: how cut turns are run again, and each thread's cut mark. */
|
|
76
|
+
private readonly cuts?;
|
|
77
|
+
/** The cut each cut turn's context ended with, for its response. */
|
|
78
|
+
private readonly cutOfContext;
|
|
79
|
+
/** The `execute()` options a stored turn is woken with when none are given. */
|
|
80
|
+
private readonly turnOptionsFor?;
|
|
75
81
|
/** Set when turns park (`environment.storage.park`): the grace before one does. */
|
|
76
82
|
private readonly parkGraceMs?;
|
|
77
83
|
/** Set when this harness wakes the turns it parked (`park.autoWake`). */
|
|
@@ -332,8 +338,11 @@ export declare class AgentHarness<TParams extends Record<string, unknown> = Reco
|
|
|
332
338
|
* it knows the event a parked turn waits for has arrived. A turn another harness
|
|
333
339
|
* holds, or one already running here, is left alone.
|
|
334
340
|
*
|
|
341
|
+
* A turn that was cut (spec 23 § Cut turns) is due once its back-off is over.
|
|
342
|
+
*
|
|
335
343
|
* A resumed turn replays what it finished and goes on. `options` are the
|
|
336
|
-
* `execute()` options it runs with; a stored turn keeps none of its own
|
|
344
|
+
* `execute()` options it runs with; a stored turn keeps none of its own, so
|
|
345
|
+
* without them the harness asks `AgentHarnessOpts.turnOptions`.
|
|
337
346
|
*
|
|
338
347
|
* Resolves the ids of the threads it resumed. Durable sessions only.
|
|
339
348
|
* @public
|
|
@@ -342,6 +351,28 @@ export declare class AgentHarness<TParams extends Record<string, unknown> = Reco
|
|
|
342
351
|
threadId?: string;
|
|
343
352
|
options?: ExecuteOptions;
|
|
344
353
|
}): Promise<string[]>;
|
|
354
|
+
/**
|
|
355
|
+
* A turn stopped by `lease_lost` or `ledger_write_failed` is cut, not failed
|
|
356
|
+
* (spec 23 § Cut turns): its record stays, marked, and a wake runs it again once
|
|
357
|
+
* its back-off is over. Resolves false when `error` does not cut the turn, or the
|
|
358
|
+
* turn has been cut as often as it may be and fails instead.
|
|
359
|
+
*/
|
|
360
|
+
private closeOutCut;
|
|
361
|
+
/**
|
|
362
|
+
* Another holder took the turn (spec 23 § Cut turns, A replaced harness). The
|
|
363
|
+
* turn is theirs: this harness writes nothing to it, counts nothing, and leaves
|
|
364
|
+
* its own queue alone. It is cut only while it is still on record under this
|
|
365
|
+
* execution, when a wake can pick it up should that holder die; otherwise the
|
|
366
|
+
* other holder ended it, and the response fails as before.
|
|
367
|
+
*/
|
|
368
|
+
private closeOutLostLease;
|
|
369
|
+
/**
|
|
370
|
+
* Record the end of a turn whose work is done. Resolves true when that write was
|
|
371
|
+
* refused and the turn was cut instead: a wake replays it to here.
|
|
372
|
+
*/
|
|
373
|
+
private keepOrCut;
|
|
374
|
+
/** The options a stored turn is woken with when the wake names none: `turnOptions`, or none. */
|
|
375
|
+
private storedTurnOptions;
|
|
345
376
|
/**
|
|
346
377
|
* The earliest time the harness has something to do, in epoch ms: a parked turn
|
|
347
378
|
* that needs waking, or the next event of a scheduled source a trigger listens
|
|
@@ -697,6 +728,8 @@ export declare class AgentHarness<TParams extends Record<string, unknown> = Reco
|
|
|
697
728
|
getChannelHandle<T>(channel: ExternalChannel<T>, executionId: string): ChannelHandle<T>;
|
|
698
729
|
getChannelStream<T>(channel: ExternalChannel<T>, executionId: string): AsyncIterable<T>;
|
|
699
730
|
/** Resolves layer-provided tools and merges with step tools into ctx.unifiedTools. */
|
|
731
|
+
/** The tools a session turn offers: its own first (one named like another replaces it), then the flow's, the harness's and its connections'. */
|
|
732
|
+
private setTurnTools;
|
|
700
733
|
private setUnifiedTools;
|
|
701
734
|
private toExecCtx;
|
|
702
735
|
initLayers(layers: ContextLayer[], ctx: Context, storage: StorageAdapter): Promise<void>;
|
|
@@ -24,6 +24,7 @@ import { readDecision } from './read-decision.js';
|
|
|
24
24
|
import { recordedToolCalls } from './recorded-tool-calls.js';
|
|
25
25
|
import { Spend } from './spend.js';
|
|
26
26
|
import { ThreadRecord } from './thread-record.js';
|
|
27
|
+
import { TurnCuts } from './turn-cuts.js';
|
|
27
28
|
/** Keep `message` in storage for after the thread's parked turn, with its options. */
|
|
28
29
|
function keepForLater(session, record, message) {
|
|
29
30
|
session.keptOptions.set(message.id, message.options);
|
|
@@ -77,6 +78,8 @@ function isLeaseLost(error) {
|
|
|
77
78
|
const LEASE_WAIT_MARGIN_MS = 1e3;
|
|
78
79
|
/** What a turn's run resolves to when it parked instead of finishing. */
|
|
79
80
|
const PARKED = Symbol('parked');
|
|
81
|
+
/** What a turn's run resolves to when it was cut instead of failing (spec 23 § Cut turns). */
|
|
82
|
+
const CUT = Symbol('cut');
|
|
80
83
|
/** When the turn on `ctx` parked until, or undefined when it did not park. */
|
|
81
84
|
function parkedUntil(ctx) {
|
|
82
85
|
if (ctx.abortReason !== PARK_REASON || !(ctx instanceof ContextImpl)) {
|
|
@@ -205,6 +208,12 @@ export class AgentHarness {
|
|
|
205
208
|
leaseOptions;
|
|
206
209
|
/** The flow version this harness runs, when the host names one. */
|
|
207
210
|
version;
|
|
211
|
+
/** Set with the lease: how cut turns are run again, and each thread's cut mark. */
|
|
212
|
+
cuts;
|
|
213
|
+
/** The cut each cut turn's context ended with, for its response. */
|
|
214
|
+
cutOfContext = new WeakMap();
|
|
215
|
+
/** The `execute()` options a stored turn is woken with when none are given. */
|
|
216
|
+
turnOptionsFor;
|
|
208
217
|
/** Set when turns park (`environment.storage.park`): the grace before one does. */
|
|
209
218
|
parkGraceMs;
|
|
210
219
|
/** Set when this harness wakes the turns it parked (`park.autoWake`). */
|
|
@@ -340,6 +349,7 @@ export class AgentHarness {
|
|
|
340
349
|
onHeld: opts.onHeld,
|
|
341
350
|
steerWhileHeld: opts.steerWhileHeld,
|
|
342
351
|
now: opts.now,
|
|
352
|
+
killAgentsOnExit: opts.killAgentsOnExit,
|
|
343
353
|
};
|
|
344
354
|
this.outputStorage = this.config.storage ?? createInMemoryStorage();
|
|
345
355
|
const adapters = resolveEnvironmentAdapters(environment);
|
|
@@ -360,6 +370,8 @@ export class AgentHarness {
|
|
|
360
370
|
now: opts.now,
|
|
361
371
|
...storageEnv?.lease,
|
|
362
372
|
});
|
|
373
|
+
this.cuts = this.leaseOptions ? new TurnCuts(storageEnv?.lease?.cut) : undefined;
|
|
374
|
+
this.turnOptionsFor = opts.turnOptions;
|
|
363
375
|
this.parkGraceMs = resolveParkGrace(this.leaseOptions ? storageEnv?.park : undefined);
|
|
364
376
|
this.parkWatchers = this.createParkWatchers(storageEnv?.park);
|
|
365
377
|
this.spend = this.checkpointStore
|
|
@@ -915,6 +927,7 @@ export class AgentHarness {
|
|
|
915
927
|
await record.answered(ctx, answer);
|
|
916
928
|
}
|
|
917
929
|
await record.end(ctx, ctx.itemLog.items, session.runner.queue.peekAll().map((message) => message.id));
|
|
930
|
+
this.cuts?.forget(threadId);
|
|
918
931
|
return;
|
|
919
932
|
}
|
|
920
933
|
session.accumulatedItems = [
|
|
@@ -971,6 +984,7 @@ export class AgentHarness {
|
|
|
971
984
|
}
|
|
972
985
|
}
|
|
973
986
|
},
|
|
987
|
+
cutOf: (ctx) => this.cutOfContext.get(ctx),
|
|
974
988
|
heldCalls: async (ctx) => (await heldCallsOf(this).list(rootIdOf(ctx)))
|
|
975
989
|
.filter((held) => held.resolution === undefined)
|
|
976
990
|
.map(toHeldCall),
|
|
@@ -988,6 +1002,7 @@ export class AgentHarness {
|
|
|
988
1002
|
session.turnOptions = perTurnOptions;
|
|
989
1003
|
this.parkWatchers?.stop(threadId);
|
|
990
1004
|
const messageIds = messages.map((m) => m.id);
|
|
1005
|
+
session.turnMessageIds = messageIds;
|
|
991
1006
|
const history = record ? await record.history() : session.accumulatedItems;
|
|
992
1007
|
const graph = this.agentGraph ? frameworkCast(this.agentGraph) : undefined;
|
|
993
1008
|
const views = this.#connections.turnViews(perTurnOptions.connections, graph);
|
|
@@ -1031,14 +1046,7 @@ export class AgentHarness {
|
|
|
1031
1046
|
frameworkCast(ctx)._sessionQueue = session.runner.queue;
|
|
1032
1047
|
await this.start();
|
|
1033
1048
|
const connectionTools = await this.#connections.openTurn(ctx, perTurnOptions.connections, graph);
|
|
1034
|
-
|
|
1035
|
-
const stepTools = this.agentGraph ? collectAllTools(this.agentGraph) : [];
|
|
1036
|
-
this.setUnifiedTools(ctx, [
|
|
1037
|
-
...stepTools,
|
|
1038
|
-
...this.harnessTools,
|
|
1039
|
-
...connectionTools,
|
|
1040
|
-
]);
|
|
1041
|
-
}
|
|
1049
|
+
this.setTurnTools(ctx, perTurnOptions.tools ?? [], connectionTools);
|
|
1042
1050
|
// Steers the last harness accepted but never applied go back in the queue;
|
|
1043
1051
|
// ones already in the conversation are skipped when offered.
|
|
1044
1052
|
for (const steer of (await record?.steers()) ?? []) {
|
|
@@ -1091,11 +1099,12 @@ export class AgentHarness {
|
|
|
1091
1099
|
}
|
|
1092
1100
|
const until = Number.isFinite(wakeAt) ? wakeAt : undefined;
|
|
1093
1101
|
await record.park(ctx, until);
|
|
1102
|
+
this.cuts?.forget(threadId);
|
|
1094
1103
|
await this.#connections.release(ctx.id);
|
|
1095
1104
|
this.parkWatchers?.watch(threadId, {
|
|
1096
1105
|
executionId: ctx.id,
|
|
1097
1106
|
until,
|
|
1098
|
-
options: session.turnOptions
|
|
1107
|
+
options: session.turnOptions,
|
|
1099
1108
|
});
|
|
1100
1109
|
// Before the turn answers as parked: whoever wakes it has to know when.
|
|
1101
1110
|
await this.announceWakeTime();
|
|
@@ -1132,6 +1141,11 @@ export class AgentHarness {
|
|
|
1132
1141
|
if (await closeOutPark(ctx)) {
|
|
1133
1142
|
return PARKED;
|
|
1134
1143
|
}
|
|
1144
|
+
// Cut short with everything before the stop on record: run again later.
|
|
1145
|
+
if (await this.closeOutCut(session, ctx, error)) {
|
|
1146
|
+
return CUT;
|
|
1147
|
+
}
|
|
1148
|
+
this.cuts?.forget(threadId);
|
|
1135
1149
|
// The turn ended here, in a live process. Say so, or a routine wake would
|
|
1136
1150
|
// take it for one cut off by a crash and run it again, every time.
|
|
1137
1151
|
// It stopped rather than redo a recorded effect under a newer flow version:
|
|
@@ -1158,10 +1172,12 @@ export class AgentHarness {
|
|
|
1158
1172
|
});
|
|
1159
1173
|
// A flow that caught the wait's cancellation and returned is parked all the
|
|
1160
1174
|
// same: the wait it was in has not ended, and the woken turn goes back to it.
|
|
1161
|
-
if (result === PARKED || (await closeOutPark(ctx))) {
|
|
1175
|
+
if (result === CUT || result === PARKED || (await closeOutPark(ctx))) {
|
|
1176
|
+
return '';
|
|
1177
|
+
}
|
|
1178
|
+
if (await this.keepOrCut(session, ctx, () => keepHistory(ctx, result))) {
|
|
1162
1179
|
return '';
|
|
1163
1180
|
}
|
|
1164
|
-
await keepHistory(ctx, result);
|
|
1165
1181
|
this.parkWatchers?.forget(threadId, ctx.id);
|
|
1166
1182
|
await this.announceWakeTimeQuietly();
|
|
1167
1183
|
return result;
|
|
@@ -1185,8 +1201,11 @@ export class AgentHarness {
|
|
|
1185
1201
|
* it knows the event a parked turn waits for has arrived. A turn another harness
|
|
1186
1202
|
* holds, or one already running here, is left alone.
|
|
1187
1203
|
*
|
|
1204
|
+
* A turn that was cut (spec 23 § Cut turns) is due once its back-off is over.
|
|
1205
|
+
*
|
|
1188
1206
|
* A resumed turn replays what it finished and goes on. `options` are the
|
|
1189
|
-
* `execute()` options it runs with; a stored turn keeps none of its own
|
|
1207
|
+
* `execute()` options it runs with; a stored turn keeps none of its own, so
|
|
1208
|
+
* without them the harness asks `AgentHarnessOpts.turnOptions`.
|
|
1190
1209
|
*
|
|
1191
1210
|
* Resolves the ids of the threads it resumed. Durable sessions only.
|
|
1192
1211
|
* @public
|
|
@@ -1221,14 +1240,16 @@ export class AgentHarness {
|
|
|
1221
1240
|
// A routine wake leaves alone a parked turn that is not due, one with no
|
|
1222
1241
|
// deadline at all (it waits for an answer, not for a time), and one that
|
|
1223
1242
|
// failed or was cancelled here: only a turn cut off by a crash resumes unasked.
|
|
1224
|
-
const
|
|
1243
|
+
const cut = turn.parked ? undefined : this.cuts?.markOf(threadId, turn);
|
|
1244
|
+
const notDue = (turn.parked && (turn.parked.until === undefined || turn.parked.until > now)) ||
|
|
1245
|
+
(cut !== undefined && this.cuts !== undefined && this.cuts.dueAt(cut) > now);
|
|
1225
1246
|
if (!scope?.threadId && (notDue || (turn.stopped && !turn.parked))) {
|
|
1226
1247
|
// A parked turn found in storage, as at boot, is watched over from here on.
|
|
1227
1248
|
if (turn.parked && this.parkWatchers && !this.parkWatchers.has(threadId)) {
|
|
1228
1249
|
this.parkWatchers.watch(threadId, {
|
|
1229
1250
|
executionId: turn.executionId,
|
|
1230
1251
|
until: turn.parked.until,
|
|
1231
|
-
options: scope?.options
|
|
1252
|
+
options: scope?.options,
|
|
1232
1253
|
});
|
|
1233
1254
|
}
|
|
1234
1255
|
continue;
|
|
@@ -1244,6 +1265,7 @@ export class AgentHarness {
|
|
|
1244
1265
|
if (held && !scope?.threadId) {
|
|
1245
1266
|
continue;
|
|
1246
1267
|
}
|
|
1268
|
+
const options = scope?.options ?? (await this.storedTurnOptions(threadId, turn, cut));
|
|
1247
1269
|
// Re-deliver the turn's own messages: the same ids resume it, and a resumed
|
|
1248
1270
|
// turn reads its input from its checkpoint, not from these. They go back as
|
|
1249
1271
|
// one arrival: handed over one at a time, the first would start a turn of
|
|
@@ -1252,13 +1274,124 @@ export class AgentHarness {
|
|
|
1252
1274
|
id,
|
|
1253
1275
|
input: [],
|
|
1254
1276
|
deliveryMode: 'next-turn',
|
|
1255
|
-
options
|
|
1277
|
+
options,
|
|
1256
1278
|
enqueuedAt: Date.now(),
|
|
1257
1279
|
})));
|
|
1258
1280
|
woken.push(threadId);
|
|
1259
1281
|
}
|
|
1260
1282
|
return woken;
|
|
1261
1283
|
}
|
|
1284
|
+
/**
|
|
1285
|
+
* A turn stopped by `lease_lost` or `ledger_write_failed` is cut, not failed
|
|
1286
|
+
* (spec 23 § Cut turns): its record stays, marked, and a wake runs it again once
|
|
1287
|
+
* its back-off is over. Resolves false when `error` does not cut the turn, or the
|
|
1288
|
+
* turn has been cut as often as it may be and fails instead.
|
|
1289
|
+
*/
|
|
1290
|
+
async closeOutCut(session, ctx, error) {
|
|
1291
|
+
const { record } = session;
|
|
1292
|
+
const { threadId } = session.runner;
|
|
1293
|
+
const reason = this.cuts?.reasonOf(error);
|
|
1294
|
+
if (!reason || !record || !this.cuts || !this.leaseOptions) {
|
|
1295
|
+
return false;
|
|
1296
|
+
}
|
|
1297
|
+
// The storage that refused a write may refuse this read: the mark in memory counts then.
|
|
1298
|
+
const stored = await record.turn().catch(() => undefined);
|
|
1299
|
+
if (reason === 'lease_lost') {
|
|
1300
|
+
return this.closeOutLostLease(threadId, ctx, stored);
|
|
1301
|
+
}
|
|
1302
|
+
const messageIds = stored?.executionId === ctx.id ? stored.messageIds : session.turnMessageIds;
|
|
1303
|
+
const mark = this.cuts.cut(threadId, {
|
|
1304
|
+
messageIds: messageIds ?? [],
|
|
1305
|
+
cut: stored?.executionId === ctx.id ? stored.cut : undefined,
|
|
1306
|
+
}, reason, this.now());
|
|
1307
|
+
if (!mark) {
|
|
1308
|
+
return false;
|
|
1309
|
+
}
|
|
1310
|
+
await record.cut(ctx, mark);
|
|
1311
|
+
// Messages still queued wait in storage, as for a park: one that started a turn
|
|
1312
|
+
// now would throw the cut turn away.
|
|
1313
|
+
const interrupts = [];
|
|
1314
|
+
for (const queued of session.runner.queue.drainAll()) {
|
|
1315
|
+
if (queued.deliveryMode === 'interrupt') {
|
|
1316
|
+
interrupts.push(queued);
|
|
1317
|
+
continue;
|
|
1318
|
+
}
|
|
1319
|
+
await keepForLater(session, record, queued);
|
|
1320
|
+
}
|
|
1321
|
+
for (const interrupt of interrupts.reverse()) {
|
|
1322
|
+
session.runner.queue.prepend(interrupt);
|
|
1323
|
+
}
|
|
1324
|
+
this.cutOfContext.set(ctx, {
|
|
1325
|
+
reason,
|
|
1326
|
+
attempt: mark.count,
|
|
1327
|
+
wakeAt: this.cuts.dueAt(mark),
|
|
1328
|
+
});
|
|
1329
|
+
// Before the turn answers as cut: whoever wakes it has to know when.
|
|
1330
|
+
await this.announceWakeTimeQuietly();
|
|
1331
|
+
return true;
|
|
1332
|
+
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Another holder took the turn (spec 23 § Cut turns, A replaced harness). The
|
|
1335
|
+
* turn is theirs: this harness writes nothing to it, counts nothing, and leaves
|
|
1336
|
+
* its own queue alone. It is cut only while it is still on record under this
|
|
1337
|
+
* execution, when a wake can pick it up should that holder die; otherwise the
|
|
1338
|
+
* other holder ended it, and the response fails as before.
|
|
1339
|
+
*/
|
|
1340
|
+
async closeOutLostLease(threadId, ctx, stored) {
|
|
1341
|
+
if (!this.leaseOptions || stored?.executionId !== ctx.id || stored.parked || stored.stopped) {
|
|
1342
|
+
return false;
|
|
1343
|
+
}
|
|
1344
|
+
const heldUntil = await heldElsewhereUntil(this.leaseOptions, threadId).catch(() => undefined);
|
|
1345
|
+
this.cutOfContext.set(ctx, {
|
|
1346
|
+
reason: 'lease_lost',
|
|
1347
|
+
wakeAt: heldUntil ?? this.now(),
|
|
1348
|
+
});
|
|
1349
|
+
await this.announceWakeTimeQuietly();
|
|
1350
|
+
return true;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Record the end of a turn whose work is done. Resolves true when that write was
|
|
1354
|
+
* refused and the turn was cut instead: a wake replays it to here.
|
|
1355
|
+
*/
|
|
1356
|
+
async keepOrCut(session, ctx, keep) {
|
|
1357
|
+
try {
|
|
1358
|
+
await keep();
|
|
1359
|
+
return false;
|
|
1360
|
+
}
|
|
1361
|
+
catch (error) {
|
|
1362
|
+
if (await this.closeOutCut(session, ctx, error)) {
|
|
1363
|
+
return true;
|
|
1364
|
+
}
|
|
1365
|
+
this.cuts?.forget(session.runner.threadId);
|
|
1366
|
+
throw error;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
/** The options a stored turn is woken with when the wake names none: `turnOptions`, or none. */
|
|
1370
|
+
async storedTurnOptions(threadId, turn, cut) {
|
|
1371
|
+
if (!this.turnOptionsFor) {
|
|
1372
|
+
return {};
|
|
1373
|
+
}
|
|
1374
|
+
return await this.turnOptionsFor({
|
|
1375
|
+
threadId,
|
|
1376
|
+
executionId: turn.executionId,
|
|
1377
|
+
messageIds: turn.messageIds,
|
|
1378
|
+
...(turn.version === undefined
|
|
1379
|
+
? {}
|
|
1380
|
+
: {
|
|
1381
|
+
version: turn.version,
|
|
1382
|
+
}),
|
|
1383
|
+
...(turn.parked
|
|
1384
|
+
? {
|
|
1385
|
+
parked: turn.parked,
|
|
1386
|
+
}
|
|
1387
|
+
: {}),
|
|
1388
|
+
...(cut
|
|
1389
|
+
? {
|
|
1390
|
+
cut,
|
|
1391
|
+
}
|
|
1392
|
+
: {}),
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1262
1395
|
/**
|
|
1263
1396
|
* The earliest time the harness has something to do, in epoch ms: a parked turn
|
|
1264
1397
|
* that needs waking, or the next event of a scheduled source a trigger listens
|
|
@@ -1298,15 +1431,17 @@ export class AgentHarness {
|
|
|
1298
1431
|
return undefined;
|
|
1299
1432
|
}
|
|
1300
1433
|
// A turn with no park on record is cut off, unless it is simply running here.
|
|
1301
|
-
// (One that is parked may still be closing out in this process: it counts.)
|
|
1434
|
+
// (One that is parked, or cut, may still be closing out in this process: it counts.)
|
|
1302
1435
|
const runner = this.sessions.get(threadId)?.runner;
|
|
1303
|
-
|
|
1436
|
+
const closingCut = runner?.context !== undefined && this.cutOfContext.has(runner.context);
|
|
1437
|
+
if (!turn.parked && runner && runner.getStatus().kind !== 'idle' && !closingCut) {
|
|
1304
1438
|
// Running here. Should this process die, the turn is cut off, and it can be
|
|
1305
1439
|
// picked up once the lease it held has run out: that is when to look again.
|
|
1306
1440
|
// The turn ending, or parking, replaces this time with the real one.
|
|
1307
1441
|
return this.leaseOptions ? now + this.leaseOptions.ttlMs + LEASE_WAIT_MARGIN_MS : undefined;
|
|
1308
1442
|
}
|
|
1309
|
-
const
|
|
1443
|
+
const cut = turn.parked ? undefined : this.cuts?.markOf(threadId, turn);
|
|
1444
|
+
const due = turn.parked?.until ?? (cut && this.cuts ? this.cuts.dueAt(cut) : now);
|
|
1310
1445
|
const heldUntil = this.leaseOptions
|
|
1311
1446
|
? await heldElsewhereUntil(this.leaseOptions, threadId)
|
|
1312
1447
|
: undefined;
|
|
@@ -1583,6 +1718,7 @@ export class AgentHarness {
|
|
|
1583
1718
|
}
|
|
1584
1719
|
finally {
|
|
1585
1720
|
this.forgetting.delete(threadId);
|
|
1721
|
+
this.cuts?.forget(threadId);
|
|
1586
1722
|
await this.announceWakeTimeQuietly();
|
|
1587
1723
|
}
|
|
1588
1724
|
}
|
|
@@ -1735,7 +1871,8 @@ export class AgentHarness {
|
|
|
1735
1871
|
*/
|
|
1736
1872
|
async setAside(session, record, messages) {
|
|
1737
1873
|
const turn = await record.turn();
|
|
1738
|
-
|
|
1874
|
+
const cut = turn ? this.cuts?.markOf(session.runner.threadId, turn) : undefined;
|
|
1875
|
+
if (!turn || (!turn.parked && !turn.stuck && !cut)) {
|
|
1739
1876
|
return false;
|
|
1740
1877
|
}
|
|
1741
1878
|
if (messages.some((message) => message.deliveryMode === 'interrupt')) {
|
|
@@ -2360,6 +2497,16 @@ export class AgentHarness {
|
|
|
2360
2497
|
return this.channelStore.subscribe(channel, executionId);
|
|
2361
2498
|
}
|
|
2362
2499
|
/** Resolves layer-provided tools and merges with step tools into ctx.unifiedTools. */
|
|
2500
|
+
/** The tools a session turn offers: its own first (one named like another replaces it), then the flow's, the harness's and its connections'. */
|
|
2501
|
+
setTurnTools(ctx, turnTools, connectionTools) {
|
|
2502
|
+
const stepTools = this.agentGraph ? collectAllTools(this.agentGraph) : [];
|
|
2503
|
+
this.setUnifiedTools(ctx, [
|
|
2504
|
+
...turnTools,
|
|
2505
|
+
...stepTools,
|
|
2506
|
+
...this.harnessTools,
|
|
2507
|
+
...connectionTools,
|
|
2508
|
+
]);
|
|
2509
|
+
}
|
|
2363
2510
|
setUnifiedTools(ctx, stepTools) {
|
|
2364
2511
|
const layers = ctx.layers;
|
|
2365
2512
|
const layerTools = layers && layers.length > 0 ? resolveLayerTools(layers, this, ctx) : [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { ContextCacheConfig, ContextCacheStore, ContextData, ContextLayer, ExecutionContext, ProjectionPolicy, StorageAdapter, } from '@noetic-tools/context';
|
|
2
|
-
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, DrainReport, ExecuteInput, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, HeldCallHandler, HeldSteerPolicy, Item, ItemSchemaConfig, ItemSchemaExtensions, LLMResponse, LlmProviderConfig, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, Steps, StepsInput, StepsOutput, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, UsageCharge, ValidStepChain, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
2
|
+
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, DrainReport, ExecuteInput, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, HeldCallHandler, HeldSteerPolicy, Item, ItemSchemaConfig, ItemSchemaExtensions, LLMResponse, LlmProviderConfig, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, Steps, StepsInput, StepsOutput, StoredTurn, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, TurnCut, TurnCutReason, UsageCharge, ValidStepChain, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
3
3
|
export { SteeringAction } from '@noetic-tools/types';
|
|
4
4
|
export type { ZodType } from 'zod';
|
|
@@ -2,7 +2,7 @@ import type { ArtifactStore, ConnectionEntries, ConnectionTrigger, SecretResolve
|
|
|
2
2
|
import type { Verifier } from '@noetic-tools/types/verify';
|
|
3
3
|
import type { LayerStateStore } from './deps/context.js';
|
|
4
4
|
import type { CheckpointStore, StepLedgerRetention } from './deps/runtime.js';
|
|
5
|
-
import type { AgentHooks, CallModelRequest, ContextCacheConfig, ContextData, ContextLayer, CustomItem, CustomItemsBrand, DecideRequest, DecideResponse, DeliveryMode, FsAdapter, HeldCallHandler, HeldSteerPolicy, ItemSchemaConfig, LLMResponse, LlmProviderConfig, ProjectionPolicy, ShellAdapter, Steps, StorageAdapter, SubprocessAdapter, Tool, ToolApprover, TraceExporter, ZodType } from './deps/types.js';
|
|
5
|
+
import type { AgentHooks, CallModelRequest, ContextCacheConfig, ContextData, ContextLayer, CustomItem, CustomItemsBrand, DecideRequest, DecideResponse, DeliveryMode, ExecuteOptions, FsAdapter, HeldCallHandler, HeldSteerPolicy, ItemSchemaConfig, LLMResponse, LlmProviderConfig, ProjectionPolicy, ShellAdapter, Steps, StorageAdapter, StoredTurn, SubprocessAdapter, Tool, ToolApprover, TraceExporter, ZodType } from './deps/types.js';
|
|
6
6
|
/**
|
|
7
7
|
* Storage-scoped environment configuration for `AgentHarness`.
|
|
8
8
|
*
|
|
@@ -99,6 +99,23 @@ export interface TurnLeaseConfig {
|
|
|
99
99
|
waitMs?: number;
|
|
100
100
|
/** The clock leases are timed by. Default `Date.now`. */
|
|
101
101
|
now?: () => number;
|
|
102
|
+
/**
|
|
103
|
+
* How a turn cut by `lease_lost` or `ledger_write_failed` is run again (spec 23
|
|
104
|
+
* § Cut turns). Every field has a default.
|
|
105
|
+
*/
|
|
106
|
+
cut?: TurnCutConfig;
|
|
107
|
+
}
|
|
108
|
+
/** @public Settings for cut turns (spec 23 § Cut turns). */
|
|
109
|
+
export interface TurnCutConfig {
|
|
110
|
+
/**
|
|
111
|
+
* How many times a turn may be cut and run again. The cut after that fails the
|
|
112
|
+
* turn. Default `DEFAULT_CUT_ATTEMPTS` (3); `0` fails every turn that would be cut.
|
|
113
|
+
*/
|
|
114
|
+
attempts?: number;
|
|
115
|
+
/** How long after its first cut a turn is due again, in ms; doubled for each cut after. Default 1 000. */
|
|
116
|
+
delayMs?: number;
|
|
117
|
+
/** The longest a cut turn waits before it is due again, in ms. Default 60 000. */
|
|
118
|
+
maxDelayMs?: number;
|
|
102
119
|
}
|
|
103
120
|
/**
|
|
104
121
|
* Execution-environment configuration for `AgentHarness`: the storage,
|
|
@@ -188,6 +205,13 @@ export interface AgentHarnessOpts<TParams extends Record<string, unknown> = Reco
|
|
|
188
205
|
* the progress it missed.
|
|
189
206
|
*/
|
|
190
207
|
emitItemsOnReplay?: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Stop every ACP agent this harness started when its process goes away
|
|
210
|
+
* without closing them — a crash, Ctrl-C, a killed process. By default
|
|
211
|
+
* (`false`) a local agent keeps working and holds what it sends until a
|
|
212
|
+
* resumed run reattaches to it (spec 27 § A local agent outlives its host).
|
|
213
|
+
*/
|
|
214
|
+
killAgentsOnExit?: boolean;
|
|
191
215
|
/**
|
|
192
216
|
* The version of the flow this harness runs: any string the host changes when the
|
|
193
217
|
* flow changes, such as a release tag or a hash (spec 23 § Versions). Every step a
|
|
@@ -224,6 +248,14 @@ export interface AgentHarnessOpts<TParams extends Record<string, unknown> = Reco
|
|
|
224
248
|
* so a question a person was shown stays open.
|
|
225
249
|
*/
|
|
226
250
|
steerWhileHeld?: HeldSteerPolicy;
|
|
251
|
+
/**
|
|
252
|
+
* The `execute()` options a stored turn runs with when `wake()` is given none
|
|
253
|
+
* (spec 23 § Parked turns). A stored turn keeps no options of its own, since they
|
|
254
|
+
* hold live objects: tools, layers, connections and their credentials. A host that
|
|
255
|
+
* passes per-turn options to `execute()` rebuilds them here, so a turn woken by an
|
|
256
|
+
* alarm in a fresh process runs as it began.
|
|
257
|
+
*/
|
|
258
|
+
turnOptions?: (turn: StoredTurn) => ExecuteOptions | Promise<ExecuteOptions>;
|
|
227
259
|
/**
|
|
228
260
|
* The live things the agent works with — browsers, devices, apps, chat workspaces,
|
|
229
261
|
* sources — keyed by name, or as a list of named entries (`[mcp({ name, … })]`)
|
|
@@ -40,7 +40,8 @@ export interface ParkWatcherHost {
|
|
|
40
40
|
/** The state of one of them now, or undefined when it cannot be read. */
|
|
41
41
|
taskState(task: ParkedTask, signal: AbortSignal): Promise<string | undefined>;
|
|
42
42
|
/** Resume the thread's turn. Resolves false when it could not be resumed just now. */
|
|
43
|
-
|
|
43
|
+
/** `options` undefined: the harness finds them (`turnOptions`). */
|
|
44
|
+
wake(threadId: string, options: ExecuteOptions | undefined): Promise<boolean>;
|
|
44
45
|
/** The execution of the turn the thread has parked now, or undefined when it has none. */
|
|
45
46
|
parked(threadId: string): Promise<string | undefined>;
|
|
46
47
|
now(): number;
|
|
@@ -51,7 +52,7 @@ export interface ParkedTurn {
|
|
|
51
52
|
/** When its earliest wait runs out, or undefined when none has a deadline. */
|
|
52
53
|
readonly until: number | undefined;
|
|
53
54
|
/** The `execute()` options the turn ran with, to resume it with. */
|
|
54
|
-
readonly options
|
|
55
|
+
readonly options?: ExecuteOptions;
|
|
55
56
|
}
|
|
56
57
|
/** @public How long auto-wake waits after an event before waking a turn, so a burst wakes it once. */
|
|
57
58
|
export declare const DEFAULT_WAKE_DEBOUNCE_MS = 250;
|
|
@@ -93,6 +93,17 @@ export declare class ThreadRecord {
|
|
|
93
93
|
* checkpoint and ledger stay, since they are what the woken turn resumes from.
|
|
94
94
|
*/
|
|
95
95
|
park(ctx: Context, until: number | undefined): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* The turn on `ctx` was cut (spec 23 § Cut turns): keep its record, marked with how
|
|
98
|
+
* often and when. Best-effort and unfenced: storage that just refused a write may
|
|
99
|
+
* refuse this one, and the harness keeps the mark in memory as well. Written only
|
|
100
|
+
* while the record still names the turn's execution.
|
|
101
|
+
*/
|
|
102
|
+
cut(ctx: Context, cut: {
|
|
103
|
+
reason: string;
|
|
104
|
+
count: number;
|
|
105
|
+
at: number;
|
|
106
|
+
}): Promise<void>;
|
|
96
107
|
/**
|
|
97
108
|
* The turn on `ctx` ended here without finishing. Best-effort: a turn whose mark
|
|
98
109
|
* does not land is simply taken for one that was cut off.
|
|
@@ -163,6 +163,14 @@ export class ThreadRecord {
|
|
|
163
163
|
: {
|
|
164
164
|
version: this.host.version,
|
|
165
165
|
}),
|
|
166
|
+
// Kept until the turn ends, even when it resumes under a new execution (its
|
|
167
|
+
// checkpoint was not written): how often it was cut counts toward giving up.
|
|
168
|
+
...(record.turn?.cut !== undefined &&
|
|
169
|
+
(record.turn.executionId === ctx.id || isSameList(record.turn.messageIds, messageIds))
|
|
170
|
+
? {
|
|
171
|
+
cut: record.turn.cut,
|
|
172
|
+
}
|
|
173
|
+
: {}),
|
|
166
174
|
// Kept until the turn ends: a host that dies part way through going on
|
|
167
175
|
// must not find the same step stuck again.
|
|
168
176
|
...(record.turn?.executionId === ctx.id && record.turn.accept !== undefined
|
|
@@ -226,7 +234,7 @@ export class ThreadRecord {
|
|
|
226
234
|
? {
|
|
227
235
|
...record,
|
|
228
236
|
turn: {
|
|
229
|
-
...record.turn,
|
|
237
|
+
...withoutCut(record.turn),
|
|
230
238
|
parked: until === undefined
|
|
231
239
|
? {}
|
|
232
240
|
: {
|
|
@@ -236,6 +244,23 @@ export class ThreadRecord {
|
|
|
236
244
|
}
|
|
237
245
|
: record);
|
|
238
246
|
}
|
|
247
|
+
/**
|
|
248
|
+
* The turn on `ctx` was cut (spec 23 § Cut turns): keep its record, marked with how
|
|
249
|
+
* often and when. Best-effort and unfenced: storage that just refused a write may
|
|
250
|
+
* refuse this one, and the harness keeps the mark in memory as well. Written only
|
|
251
|
+
* while the record still names the turn's execution.
|
|
252
|
+
*/
|
|
253
|
+
cut(ctx, cut) {
|
|
254
|
+
return this.update((record) => record.turn?.executionId === ctx.id
|
|
255
|
+
? {
|
|
256
|
+
...record,
|
|
257
|
+
turn: {
|
|
258
|
+
...record.turn,
|
|
259
|
+
cut,
|
|
260
|
+
},
|
|
261
|
+
}
|
|
262
|
+
: record);
|
|
263
|
+
}
|
|
239
264
|
/**
|
|
240
265
|
* The turn on `ctx` ended here without finishing. Best-effort: a turn whose mark
|
|
241
266
|
* does not land is simply taken for one that was cut off.
|
|
@@ -245,7 +270,7 @@ export class ThreadRecord {
|
|
|
245
270
|
? {
|
|
246
271
|
...record,
|
|
247
272
|
turn: {
|
|
248
|
-
...record.turn,
|
|
273
|
+
...withoutCut(record.turn),
|
|
249
274
|
stopped: {
|
|
250
275
|
at,
|
|
251
276
|
reason,
|
|
@@ -438,6 +463,11 @@ export class ThreadRecord {
|
|
|
438
463
|
return next;
|
|
439
464
|
}
|
|
440
465
|
}
|
|
466
|
+
function withoutCut(turn) {
|
|
467
|
+
const { cut, ...rest } = turn;
|
|
468
|
+
void cut;
|
|
469
|
+
return rest;
|
|
470
|
+
}
|
|
441
471
|
function withoutTurn(record) {
|
|
442
472
|
const { turn, ...rest } = record;
|
|
443
473
|
void turn;
|
|
@@ -450,6 +480,9 @@ function withoutTurn(record) {
|
|
|
450
480
|
function isSameDelivery(turn, delivered) {
|
|
451
481
|
return delivered.length > 0 && delivered.every((id) => turn.includes(id));
|
|
452
482
|
}
|
|
483
|
+
function isSameList(a, b) {
|
|
484
|
+
return a.length === b.length && a.every((id, i) => id === b[i]);
|
|
485
|
+
}
|
|
453
486
|
function addUsage(total, spent) {
|
|
454
487
|
const cost = (total.cost ?? 0) + spent.cost;
|
|
455
488
|
const billedCost = (total.billedCost ?? 0) + spent.billedCost;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cut turns (spec 23 § Cut turns): a turn stopped by `lease_lost` or
|
|
3
|
+
* `ledger_write_failed` has everything before the stop on record, so it is run
|
|
4
|
+
* again by a later wake, with back-off, rather than failed. The harness keeps each
|
|
5
|
+
* thread's mark here as well as in the thread record, since the storage that just
|
|
6
|
+
* refused a write may refuse the mark too.
|
|
7
|
+
*/
|
|
8
|
+
import type { TurnCutReason } from './deps/types.js';
|
|
9
|
+
import type { TurnCutConfig } from './harness-options.js';
|
|
10
|
+
/** How often a turn was cut, and when last. */
|
|
11
|
+
export interface CutMark {
|
|
12
|
+
readonly reason: string;
|
|
13
|
+
readonly count: number;
|
|
14
|
+
readonly at: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The parts of a stored turn a cut is read from. A turn is known by its messages,
|
|
18
|
+
* not its execution: one whose checkpoint could not be written resumes under a
|
|
19
|
+
* new execution id, and its cuts still count.
|
|
20
|
+
*/
|
|
21
|
+
interface CutTurn {
|
|
22
|
+
readonly messageIds: ReadonlyArray<string>;
|
|
23
|
+
readonly cut?: CutMark;
|
|
24
|
+
}
|
|
25
|
+
/** @public How many times a turn may be cut and run again before it fails. */
|
|
26
|
+
export declare const DEFAULT_CUT_ATTEMPTS = 3;
|
|
27
|
+
/** @public How long after its first cut a turn is due again, in ms. */
|
|
28
|
+
export declare const DEFAULT_CUT_DELAY_MS = 1000;
|
|
29
|
+
/** @public The longest a cut turn waits before it is due again, in ms. */
|
|
30
|
+
export declare const DEFAULT_CUT_MAX_DELAY_MS = 60000;
|
|
31
|
+
/** @internal The harness's cut marks, by thread, and the policy that runs cut turns again. */
|
|
32
|
+
export declare class TurnCuts {
|
|
33
|
+
private readonly policy;
|
|
34
|
+
private readonly marks;
|
|
35
|
+
constructor(config: TurnCutConfig | undefined);
|
|
36
|
+
/** Why `error` cuts the turn rather than failing it, or undefined when it does not. */
|
|
37
|
+
reasonOf(error: unknown): TurnCutReason | undefined;
|
|
38
|
+
/** The turn's mark: the stored one or this harness's, whichever counts more cuts. */
|
|
39
|
+
markOf(threadId: string, turn: CutTurn): CutMark | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* The mark for one more cut of the turn, kept here; undefined when the turn has
|
|
42
|
+
* been cut as often as the policy allows, and fails instead.
|
|
43
|
+
*/
|
|
44
|
+
cut(threadId: string, turn: CutTurn, reason: TurnCutReason, at: number): CutMark | undefined;
|
|
45
|
+
/** When a turn with `mark` is due again. */
|
|
46
|
+
dueAt(mark: CutMark): number;
|
|
47
|
+
/** The thread's turn ended, parked or was dropped: its cuts no longer count. */
|
|
48
|
+
forget(threadId: string): void;
|
|
49
|
+
}
|
|
50
|
+
export {};
|