@noetic-tools/core 5.22.0-next.2 → 5.22.0-next.3
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/adapters/in-memory-subprocess-adapter.js +4 -2
- package/dist/builders/custom-item.d.ts +1 -1
- package/dist/builders/custom-item.js +14 -3
- package/dist/connections/pool.d.ts +18 -0
- package/dist/connections/pool.js +54 -9
- package/dist/connections/registry.js +11 -0
- package/dist/harness/agent-harness.d.ts +76 -3
- package/dist/harness/agent-harness.js +229 -21
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/forget.d.ts +57 -0
- package/dist/harness/forget.js +496 -0
- package/dist/harness/harness-options.d.ts +15 -1
- package/dist/harness/model-rounds.d.ts +6 -0
- package/dist/harness/model-rounds.js +7 -3
- package/dist/harness/model-tool-round.js +19 -5
- package/dist/harness/recorded-tool-calls.d.ts +16 -0
- package/dist/harness/recorded-tool-calls.js +106 -0
- package/dist/harness/spend.d.ts +13 -1
- package/dist/harness/spend.js +82 -1
- package/dist/harness/steer-inbox.js +9 -5
- package/dist/harness/thread-record.d.ts +12 -1
- package/dist/harness/thread-record.js +63 -19
- package/dist/index.d.ts +4 -5
- package/dist/index.js +2 -3
- package/dist/interpreter/acp-tools.js +21 -4
- package/dist/interpreter/execute-action.js +7 -1
- package/dist/interpreter/execute.js +55 -3
- package/dist/interpreter/step-dispatcher.js +17 -1
- package/dist/interpreter/step-kind-dispatch.js +9 -1
- package/dist/runtime/durable/child-links.d.ts +61 -0
- package/dist/runtime/durable/child-links.js +127 -0
- package/dist/runtime/durable/session-store.d.ts +6 -0
- package/dist/runtime/durable/session-store.js +16 -2
- package/dist/runtime/durable/turn-lease.d.ts +10 -0
- package/dist/runtime/durable/turn-lease.js +10 -2
- package/dist/runtime/held-calls.d.ts +65 -6
- package/dist/runtime/held-calls.js +257 -5
- package/dist/runtime/output-store.d.ts +2 -0
- package/dist/runtime/output-store.js +3 -1
- package/dist/runtime/tool-approval.d.ts +1 -3
- package/dist/runtime/tool-approval.js +1 -1
- package/package.json +2 -2
|
@@ -113,6 +113,38 @@ function toResolutionRecord(resolution) {
|
|
|
113
113
|
hasOutput: true,
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
|
+
function watchKey(executionId, callId) {
|
|
117
|
+
return `${executionId}\u0000${callId}`;
|
|
118
|
+
}
|
|
119
|
+
function settledOf(held, by) {
|
|
120
|
+
return {
|
|
121
|
+
callId: held.callId,
|
|
122
|
+
name: held.name,
|
|
123
|
+
argsHash: held.argsHash,
|
|
124
|
+
reason: held.reason,
|
|
125
|
+
by,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Why a record that is being closed stopped being held, as the record says. */
|
|
129
|
+
function settledByOf(record, now) {
|
|
130
|
+
if (record?.resolution !== undefined) {
|
|
131
|
+
return 'answer';
|
|
132
|
+
}
|
|
133
|
+
if (record && isOverdue(record, now)) {
|
|
134
|
+
return 'due';
|
|
135
|
+
}
|
|
136
|
+
return 'cancelled';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Whether a watched call is still the question shown: held, unanswered, for the same
|
|
140
|
+
* arguments and reason. Anything else means another harness settled it.
|
|
141
|
+
*/
|
|
142
|
+
function stillShown(record, held) {
|
|
143
|
+
return (record !== undefined &&
|
|
144
|
+
record.resolution === undefined &&
|
|
145
|
+
record.argsHash === held.argsHash &&
|
|
146
|
+
record.reason === held.reason);
|
|
147
|
+
}
|
|
116
148
|
/** A held record as hosts see it. */
|
|
117
149
|
export function toHeldCall(record) {
|
|
118
150
|
return {
|
|
@@ -145,6 +177,12 @@ export class HeldCalls {
|
|
|
145
177
|
storage;
|
|
146
178
|
clock;
|
|
147
179
|
waiters = new Map();
|
|
180
|
+
/** Calls announced held in this process, by execution and call. */
|
|
181
|
+
watches = new Map();
|
|
182
|
+
/** Watched calls this process is closing: a sweep leaves them to `close`. */
|
|
183
|
+
closing = new Set();
|
|
184
|
+
/** Re-reads watched calls on shared storage, which another harness may settle. */
|
|
185
|
+
sweeper;
|
|
148
186
|
constructor(storage, clock) {
|
|
149
187
|
this.storage = storage;
|
|
150
188
|
this.clock = clock;
|
|
@@ -190,6 +228,13 @@ export class HeldCalls {
|
|
|
190
228
|
}
|
|
191
229
|
/** Drop every held call and intent of an execution that is over or was abandoned. */
|
|
192
230
|
async clear(executionId) {
|
|
231
|
+
for (const key of [
|
|
232
|
+
...this.watches.keys(),
|
|
233
|
+
]) {
|
|
234
|
+
if (key.startsWith(`${executionId}\u0000`)) {
|
|
235
|
+
this.settleWatch(key, 'cancelled');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
193
238
|
for (const prefix of [
|
|
194
239
|
heldPrefix(executionId),
|
|
195
240
|
intentPrefix(executionId),
|
|
@@ -218,9 +263,118 @@ export class HeldCalls {
|
|
|
218
263
|
async disarm(executionId, callId) {
|
|
219
264
|
await this.storage.delete(`${intentPrefix(executionId)}${callId}`);
|
|
220
265
|
}
|
|
221
|
-
/**
|
|
222
|
-
|
|
223
|
-
|
|
266
|
+
/**
|
|
267
|
+
* The call was settled and its result recorded, or it is being dropped: drop its
|
|
268
|
+
* record. `by` says why, for a call this process announced; without it the record
|
|
269
|
+
* says: answered, overdue, or else cancelled.
|
|
270
|
+
*/
|
|
271
|
+
async close(executionId, callId, by) {
|
|
272
|
+
const key = watchKey(executionId, callId);
|
|
273
|
+
const watched = this.watches.has(key);
|
|
274
|
+
if (watched) {
|
|
275
|
+
this.closing.add(key);
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const settledBy = watched && by === undefined
|
|
279
|
+
? settledByOf(await this.load(executionId, callId), this.now())
|
|
280
|
+
: by;
|
|
281
|
+
await this.storage.delete(`${heldPrefix(executionId)}${callId}`);
|
|
282
|
+
if (settledBy !== undefined) {
|
|
283
|
+
this.settleWatch(key, settledBy);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
finally {
|
|
287
|
+
this.closing.delete(key);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Note that this process announced `held`. Resolves the signal a handler waits on,
|
|
292
|
+
* or `undefined` when the same question is already being watched here. A different
|
|
293
|
+
* question under the same id replaces the old one: a call held for a new reason
|
|
294
|
+
* moved on because the old question was answered; one held for other arguments
|
|
295
|
+
* voided it.
|
|
296
|
+
*/
|
|
297
|
+
watch(executionId, held, emit) {
|
|
298
|
+
const key = watchKey(executionId, held.callId);
|
|
299
|
+
const existing = this.watches.get(key);
|
|
300
|
+
if (existing?.held.argsHash === held.argsHash && existing.held.reason === held.reason) {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
if (existing) {
|
|
304
|
+
this.settleWatch(key, existing.held.reason === held.reason ? 'cancelled' : 'answer');
|
|
305
|
+
}
|
|
306
|
+
const controller = new AbortController();
|
|
307
|
+
this.watches.set(key, {
|
|
308
|
+
executionId,
|
|
309
|
+
held,
|
|
310
|
+
controller,
|
|
311
|
+
emit,
|
|
312
|
+
});
|
|
313
|
+
this.armSweep();
|
|
314
|
+
return controller.signal;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* On shared storage another harness may answer, skip or drop a call this process
|
|
318
|
+
* announced, and finish its turn, so nothing here would ever close it. Watched
|
|
319
|
+
* calls are read again every so often while any are watched.
|
|
320
|
+
*/
|
|
321
|
+
armSweep() {
|
|
322
|
+
if (!this.shared || this.sweeper !== undefined) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const sweeper = setInterval(() => {
|
|
326
|
+
void this.sweep().catch(() => undefined);
|
|
327
|
+
}, HELD_POLL_MS);
|
|
328
|
+
if (typeof sweeper === 'object' && 'unref' in sweeper) {
|
|
329
|
+
sweeper.unref();
|
|
330
|
+
}
|
|
331
|
+
this.sweeper = sweeper;
|
|
332
|
+
}
|
|
333
|
+
stopSweep() {
|
|
334
|
+
clearInterval(this.sweeper);
|
|
335
|
+
this.sweeper = undefined;
|
|
336
|
+
}
|
|
337
|
+
/** Settle every watched call whose record says it is no longer the question shown. */
|
|
338
|
+
async sweep() {
|
|
339
|
+
for (const [key, watch] of [
|
|
340
|
+
...this.watches,
|
|
341
|
+
]) {
|
|
342
|
+
const record = await this.load(watch.executionId, watch.held.callId);
|
|
343
|
+
if (this.watches.get(key) !== watch || this.closing.has(key)) {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// Settled by another harness, which cannot say how: reported as answered.
|
|
347
|
+
if (!stillShown(record, watch.held)) {
|
|
348
|
+
this.settleWatch(key, 'answer');
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** The harness is closing: every handler still waiting is let go, with no event. */
|
|
353
|
+
releaseWatches() {
|
|
354
|
+
this.stopSweep();
|
|
355
|
+
for (const [key, watch] of [
|
|
356
|
+
...this.watches,
|
|
357
|
+
]) {
|
|
358
|
+
this.watches.delete(key);
|
|
359
|
+
watch.controller.abort(settledOf(watch.held, 'cancelled'));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
settleWatch(key, by) {
|
|
363
|
+
const watch = this.watches.get(key);
|
|
364
|
+
if (!watch) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
this.watches.delete(key);
|
|
368
|
+
if (this.watches.size === 0) {
|
|
369
|
+
this.stopSweep();
|
|
370
|
+
}
|
|
371
|
+
const settled = settledOf(watch.held, by);
|
|
372
|
+
try {
|
|
373
|
+
watch.emit(settled);
|
|
374
|
+
}
|
|
375
|
+
finally {
|
|
376
|
+
watch.controller.abort(settled);
|
|
377
|
+
}
|
|
224
378
|
}
|
|
225
379
|
/**
|
|
226
380
|
* Answer a held call. The first answer wins: the same answer again is a no-op, a
|
|
@@ -288,6 +442,7 @@ export class HeldCalls {
|
|
|
288
442
|
}
|
|
289
443
|
throw refused(callId, 'the call was already answered differently');
|
|
290
444
|
}
|
|
445
|
+
this.settleWatch(watchKey(executionId, callId), 'answer');
|
|
291
446
|
this.notify(executionId);
|
|
292
447
|
}
|
|
293
448
|
/** True when other processes may share this storage, and so may answer a call. */
|
|
@@ -450,7 +605,7 @@ export async function holdAndWait(opts) {
|
|
|
450
605
|
const { callId } = opts.record;
|
|
451
606
|
const opened = mayWriteHolds(opts.ctx) ? await holds.open(executionId, opts.record) : opts.record;
|
|
452
607
|
if (opened.resolution === undefined) {
|
|
453
|
-
|
|
608
|
+
announcerOf(opts.harness, opts.ctx)(toHeldCall(opened));
|
|
454
609
|
}
|
|
455
610
|
for (;;) {
|
|
456
611
|
const current = await holds.load(executionId, callId);
|
|
@@ -475,7 +630,7 @@ export async function holdAndWait(opts) {
|
|
|
475
630
|
/**
|
|
476
631
|
* @internal
|
|
477
632
|
* Says on the stream that a call is held: `{name}:tool_call_held`, with the fields
|
|
478
|
-
* `harness.pending()` returns
|
|
633
|
+
* `harness.pending()` returns, and puts it to the harness's `onHeld` handler.
|
|
479
634
|
*/
|
|
480
635
|
export function announcerOf(harness, ctx) {
|
|
481
636
|
return (held) => {
|
|
@@ -487,8 +642,105 @@ export function announcerOf(harness, ctx) {
|
|
|
487
642
|
...held,
|
|
488
643
|
},
|
|
489
644
|
});
|
|
645
|
+
noticeHeld({
|
|
646
|
+
harness,
|
|
647
|
+
ctx,
|
|
648
|
+
held,
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
/** Emits `{name}:tool_call_settled` on the context's stream. */
|
|
653
|
+
function settledAnnouncerOf(harness, ctx) {
|
|
654
|
+
return (settled) => {
|
|
655
|
+
emitFrameworkEvent({
|
|
656
|
+
broadcaster: getBroadcaster(ctx),
|
|
657
|
+
agentName: harness.config.name,
|
|
658
|
+
eventType: 'tool_call_settled',
|
|
659
|
+
data: {
|
|
660
|
+
...settled,
|
|
661
|
+
},
|
|
662
|
+
});
|
|
490
663
|
};
|
|
491
664
|
}
|
|
665
|
+
/**
|
|
666
|
+
* @internal
|
|
667
|
+
* A call was announced held in this process (spec 08 § Held tool calls): watch it, so
|
|
668
|
+
* `tool_call_settled` follows when it stops being held, and put it to the harness's
|
|
669
|
+
* `onHeld` handler, once per question. Never waits on the handler.
|
|
670
|
+
*
|
|
671
|
+
* @param emit - How the settling is said, when not on `ctx`'s stream as is.
|
|
672
|
+
*/
|
|
673
|
+
export function noticeHeld(opts) {
|
|
674
|
+
const { harness, ctx, held } = opts;
|
|
675
|
+
const executionId = heldScopeOf(ctx);
|
|
676
|
+
const signal = heldCallsOf(harness).watch(executionId, held, opts.emit ?? settledAnnouncerOf(harness, ctx));
|
|
677
|
+
const handler = harness.config.onHeld;
|
|
678
|
+
if (!signal || !handler) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
void askHandler({
|
|
682
|
+
harness,
|
|
683
|
+
handler,
|
|
684
|
+
held,
|
|
685
|
+
signal,
|
|
686
|
+
executionId,
|
|
687
|
+
threadId: harness.heldThreadOf?.(ctx),
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
/** Put a held call to the host's handler and take its answer as a decision. */
|
|
691
|
+
async function askHandler(opts) {
|
|
692
|
+
const { harness, held, signal, executionId, threadId } = opts;
|
|
693
|
+
let answer;
|
|
694
|
+
try {
|
|
695
|
+
answer = await opts.handler(held, {
|
|
696
|
+
signal,
|
|
697
|
+
executionId,
|
|
698
|
+
...(threadId === undefined
|
|
699
|
+
? {}
|
|
700
|
+
: {
|
|
701
|
+
threadId,
|
|
702
|
+
}),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
catch (e) {
|
|
706
|
+
console.warn(`[noetic] ${harness.config.name}: the onHeld handler failed for '${held.name}' (${held.callId}); the call stays held: ${messageOf(e)}`);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
// Settled some other way first: there is nothing left to answer.
|
|
710
|
+
if (answer === undefined || signal.aborted) {
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
try {
|
|
714
|
+
if (harness.answerHeld) {
|
|
715
|
+
await harness.answerHeld({
|
|
716
|
+
executionId,
|
|
717
|
+
...(threadId === undefined
|
|
718
|
+
? {}
|
|
719
|
+
: {
|
|
720
|
+
threadId,
|
|
721
|
+
}),
|
|
722
|
+
callId: held.callId,
|
|
723
|
+
argsHash: held.argsHash,
|
|
724
|
+
resolution: answer,
|
|
725
|
+
});
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
await heldCallsOf(harness).resolve(executionId, {
|
|
729
|
+
callId: held.callId,
|
|
730
|
+
argsHash: held.argsHash,
|
|
731
|
+
resolution: answer,
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
catch (e) {
|
|
735
|
+
if (signal.aborted) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
console.warn(`[noetic] ${harness.config.name}: the onHeld answer for '${held.name}' (${held.callId}) was refused; the call stays held: ${messageOf(e)}`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function messageOf(e) {
|
|
742
|
+
return e instanceof Error ? e.message : String(e);
|
|
743
|
+
}
|
|
492
744
|
/** True once a hold with a `deny` deadline has run out. */
|
|
493
745
|
export function isOverdue(record, now) {
|
|
494
746
|
return record.dueAt !== undefined && record.onDue !== 'keep' && record.dueAt <= now;
|
|
@@ -23,6 +23,8 @@ export declare class OutputStoreError extends Error {
|
|
|
23
23
|
cause: unknown;
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
+
/** @internal The prefix every thread's display output key starts with. */
|
|
27
|
+
export declare const OUTPUT_PREFIX = "output:";
|
|
26
28
|
/** @internal The storage key a thread's display output is kept under. */
|
|
27
29
|
export declare function outputKey(scope: string): string;
|
|
28
30
|
/**
|
|
@@ -29,9 +29,11 @@ const WRITE_ATTEMPTS = 3;
|
|
|
29
29
|
/** How many times a write may lose to another writer before it is a failure. */
|
|
30
30
|
const SWAP_ATTEMPTS = 20;
|
|
31
31
|
const WRITE_RETRY_MS = 10;
|
|
32
|
+
/** @internal The prefix every thread's display output key starts with. */
|
|
33
|
+
export const OUTPUT_PREFIX = 'output:';
|
|
32
34
|
/** @internal The storage key a thread's display output is kept under. */
|
|
33
35
|
export function outputKey(scope) {
|
|
34
|
-
return
|
|
36
|
+
return `${OUTPUT_PREFIX}${scope}`;
|
|
35
37
|
}
|
|
36
38
|
/** What makes two snapshots the same item: its id, else its call, else its content. */
|
|
37
39
|
function keyOf(item) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Context,
|
|
1
|
+
import type { Context, SteeringDecision } from '@noetic-tools/types';
|
|
2
2
|
import type { HoldHost } from './held-calls.js';
|
|
3
3
|
/** How the approval stage settled a call, as recorded on its `tool.call` span. */
|
|
4
4
|
export declare const ApprovalOutcome: {
|
|
@@ -58,8 +58,6 @@ export declare function awaitApproval(params: {
|
|
|
58
58
|
*/
|
|
59
59
|
recordId?: string;
|
|
60
60
|
ctx: Context;
|
|
61
|
-
/** Called once, when the call is first held. */
|
|
62
|
-
onHeld?: (held: HeldCall) => void;
|
|
63
61
|
/** False when the turn must stay live while the call is held. Default true. */
|
|
64
62
|
park?: boolean;
|
|
65
63
|
}): Promise<ToolApproval>;
|
|
@@ -173,7 +173,7 @@ export async function awaitApproval(params) {
|
|
|
173
173
|
});
|
|
174
174
|
if (!announced) {
|
|
175
175
|
announced = true;
|
|
176
|
-
|
|
176
|
+
announcerOf(params.harness, params.ctx)(toHeldCall(record));
|
|
177
177
|
}
|
|
178
178
|
await waitOnHeldCalls({
|
|
179
179
|
harness: params.harness,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.22.0-next.
|
|
3
|
+
"version": "5.22.0-next.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@noetic-tools/context": "^2.0.0-next.1",
|
|
72
|
-
"@noetic-tools/types": "^3.19.0-next.
|
|
72
|
+
"@noetic-tools/types": "^3.19.0-next.2",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|