@noetic-tools/core 5.21.0 → 5.22.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builders/tool-builder.d.ts +20 -0
- package/dist/builders/tool-builder.js +23 -1
- package/dist/builders/workflow-hydrator.d.ts +19 -0
- package/dist/builders/workflow-hydrator.js +19 -0
- package/dist/builders/workflow-step.js +7 -1
- package/dist/connections/cron-schedule.js +12 -3
- package/dist/connections/durability.d.ts +1 -2
- package/dist/connections/durability.js +1 -4
- package/dist/connections/gates.js +6 -2
- package/dist/connections/harness-wiring.d.ts +5 -0
- package/dist/connections/harness-wiring.js +76 -10
- package/dist/connections/in-memory-connection.d.ts +4 -0
- package/dist/connections/in-memory-connection.js +10 -0
- package/dist/connections/model-tools.js +5 -1
- package/dist/connections/registry.d.ts +6 -2
- package/dist/connections/registry.js +10 -0
- package/dist/connections/sources.js +261 -20
- package/dist/connections/triggers.d.ts +62 -2
- package/dist/connections/triggers.js +375 -35
- package/dist/harness/agent-harness.d.ts +263 -4
- package/dist/harness/agent-harness.js +1156 -95
- package/dist/harness/connection-gate.d.ts +3 -0
- package/dist/harness/connection-gate.js +4 -0
- package/dist/harness/deps/runtime.d.ts +3 -0
- package/dist/harness/deps/runtime.js +2 -0
- package/dist/harness/flow-version.d.ts +13 -0
- package/dist/harness/flow-version.js +66 -0
- package/dist/harness/harness-options.d.ts +82 -0
- package/dist/harness/model-call.js +8 -0
- package/dist/harness/model-rounds.d.ts +30 -0
- package/dist/harness/model-rounds.js +146 -1
- package/dist/harness/model-tool-round.d.ts +3 -0
- package/dist/harness/model-tool-round.js +201 -19
- package/dist/harness/park-watchers.d.ts +93 -0
- package/dist/harness/park-watchers.js +217 -0
- package/dist/harness/read-decision.d.ts +23 -0
- package/dist/harness/read-decision.js +100 -0
- package/dist/harness/spend.js +4 -0
- package/dist/harness/steer-inbox.js +3 -1
- package/dist/harness/thread-record.d.ts +77 -1
- package/dist/harness/thread-record.js +272 -13
- package/dist/index.d.ts +17 -7
- package/dist/index.js +10 -2
- package/dist/interpreter/acp-resume.d.ts +77 -0
- package/dist/interpreter/acp-resume.js +164 -0
- package/dist/interpreter/acp-tools.d.ts +11 -0
- package/dist/interpreter/acp-tools.js +94 -7
- package/dist/interpreter/connection-steps.d.ts +24 -1
- package/dist/interpreter/connection-steps.js +77 -2
- package/dist/interpreter/execute-acp-agent.js +111 -15
- package/dist/interpreter/execute-action.js +7 -0
- package/dist/interpreter/execute-call.js +30 -4
- package/dist/interpreter/execute-control.js +21 -0
- package/dist/interpreter/execute-invoke-tool.js +149 -5
- package/dist/interpreter/execute-wait.js +67 -9
- package/dist/interpreter/execute.js +4 -0
- package/dist/interpreter/turn-ending.d.ts +15 -0
- package/dist/interpreter/turn-ending.js +46 -0
- package/dist/portable/builders.d.ts +1 -1
- package/dist/portable/builders.js +1 -1
- package/dist/runtime/durable/parking.d.ts +61 -0
- package/dist/runtime/durable/parking.js +137 -0
- package/dist/runtime/durable/session-store.d.ts +38 -0
- package/dist/runtime/durable/session-store.js +79 -1
- package/dist/runtime/durable/step-ledger.d.ts +121 -2
- package/dist/runtime/durable/step-ledger.js +352 -15
- package/dist/runtime/durable/turn-lease.d.ts +128 -0
- package/dist/runtime/durable/turn-lease.js +307 -0
- package/dist/runtime/held-calls.d.ts +192 -0
- package/dist/runtime/held-calls.js +496 -0
- package/dist/runtime/in-memory-storage.d.ts +10 -1
- package/dist/runtime/in-memory-storage.js +20 -2
- package/dist/runtime/message-queue.d.ts +2 -0
- package/dist/runtime/message-queue.js +8 -0
- package/dist/runtime/session-runner.d.ts +18 -2
- package/dist/runtime/session-runner.js +88 -3
- package/dist/runtime/tool-approval.d.ts +46 -6
- package/dist/runtime/tool-approval.js +160 -38
- package/dist/runtime/tool-call.d.ts +34 -2
- package/dist/runtime/tool-call.js +182 -13
- package/dist/runtime/tool-context.d.ts +15 -1
- package/dist/runtime/tool-context.js +38 -2
- package/package.json +3 -3
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The turn lease: what makes one harness the only writer of a thread's turn
|
|
3
|
+
* (spec 23 § One writer per turn).
|
|
4
|
+
*
|
|
5
|
+
* A lease is one small record per thread, taken with `StorageAdapter.swap`, so
|
|
6
|
+
* two harnesses that reach for the same turn cannot both win. It carries a
|
|
7
|
+
* `term` that goes up by one on every take. The step ledger tags what it writes
|
|
8
|
+
* with the term, which is how a harness that stalled and came back finds out it
|
|
9
|
+
* was replaced: its write loses to a higher term and it stops.
|
|
10
|
+
*
|
|
11
|
+
* Time bounds the lease, not correctness. `heldUntil` only decides when another
|
|
12
|
+
* harness may take over from a holder that went quiet; the term decides whose
|
|
13
|
+
* writes count.
|
|
14
|
+
*/
|
|
15
|
+
import type { StorageAdapter } from '@noetic-tools/context';
|
|
16
|
+
import { NoeticErrorImpl } from '@noetic-tools/types';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
declare const LeaseRecordSchema: z.ZodObject<{
|
|
19
|
+
term: z.ZodNumber;
|
|
20
|
+
holder: z.ZodString;
|
|
21
|
+
heldUntil: z.ZodNumber;
|
|
22
|
+
}, z.core.$strip>;
|
|
23
|
+
/** @internal The stored lease of one thread. */
|
|
24
|
+
export type LeaseRecord = z.infer<typeof LeaseRecordSchema>;
|
|
25
|
+
/** @internal Storage that can compare-and-set. */
|
|
26
|
+
export type SwapStorage = StorageAdapter & Required<Pick<StorageAdapter, 'swap'>>;
|
|
27
|
+
/** @public How long a turn lease lasts between renewals when a harness does not say. */
|
|
28
|
+
export declare const DEFAULT_LEASE_TTL_MS = 30000;
|
|
29
|
+
/** @internal What a lease needs from its harness. */
|
|
30
|
+
export interface TurnLeaseOptions {
|
|
31
|
+
readonly storage: SwapStorage;
|
|
32
|
+
/** This harness's name for itself in the lease record. */
|
|
33
|
+
readonly holder: string;
|
|
34
|
+
readonly ttlMs: number;
|
|
35
|
+
/** How long `take` waits for a lease another harness holds to run out. */
|
|
36
|
+
readonly waitMs: number;
|
|
37
|
+
readonly now: () => number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @internal
|
|
41
|
+
* What the step ledger needs from a lease: the term to tag entries with, and a
|
|
42
|
+
* way to stop the turn when a write proves another harness took over.
|
|
43
|
+
*/
|
|
44
|
+
export interface LedgerFence {
|
|
45
|
+
readonly term: number;
|
|
46
|
+
/** The holder's name in the lease, stamped on what it records. */
|
|
47
|
+
readonly holder?: string;
|
|
48
|
+
/** Throws `lease_lost` once the lease is lost or has run past `heldUntil`. */
|
|
49
|
+
assert(): void;
|
|
50
|
+
/**
|
|
51
|
+
* `assert`, after reading the lease back from storage. A holder whose clock runs
|
|
52
|
+
* slow can believe it still holds a lease another harness has taken; the stored
|
|
53
|
+
* term settles it. Costs one read, so it guards what must not be repeated.
|
|
54
|
+
*/
|
|
55
|
+
verify(): Promise<void>;
|
|
56
|
+
/** Marks the lease lost and returns the error to throw. */
|
|
57
|
+
lose(reason: string): NoeticErrorImpl;
|
|
58
|
+
}
|
|
59
|
+
/** @internal True when `storage` can compare-and-set. */
|
|
60
|
+
export declare function canSwap(storage: StorageAdapter | undefined): storage is SwapStorage;
|
|
61
|
+
/** @internal True when a harness other than `opts.holder` holds the thread's lease now. */
|
|
62
|
+
/**
|
|
63
|
+
* @internal
|
|
64
|
+
* When another harness's lease on the thread runs out, in epoch ms, or undefined
|
|
65
|
+
* when no other harness holds it now.
|
|
66
|
+
*/
|
|
67
|
+
export declare function heldElsewhereUntil(opts: TurnLeaseOptions, threadId: string): Promise<number | undefined>;
|
|
68
|
+
export declare function heldElsewhere(opts: TurnLeaseOptions, threadId: string): Promise<boolean>;
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
* One held lease. `take` is the only way to get one; `renew` keeps it while the
|
|
72
|
+
* turn runs and `release` hands it back when the turn ends.
|
|
73
|
+
*/
|
|
74
|
+
export declare class TurnLease implements LedgerFence {
|
|
75
|
+
private readonly opts;
|
|
76
|
+
private readonly threadId;
|
|
77
|
+
private record;
|
|
78
|
+
private lostReason;
|
|
79
|
+
private timer;
|
|
80
|
+
private onLost;
|
|
81
|
+
/** The execution this lease serves, once the turn knows it. */
|
|
82
|
+
private boundTo;
|
|
83
|
+
/** The renewal in flight, if one is. */
|
|
84
|
+
private renewing;
|
|
85
|
+
private constructor();
|
|
86
|
+
/**
|
|
87
|
+
* Take the thread's lease. A lease another harness holds is waited on for up to
|
|
88
|
+
* `waitMs`: a holder that died stops renewing, so its lease runs out and the turn
|
|
89
|
+
* can be picked up without the host retrying.
|
|
90
|
+
*
|
|
91
|
+
* @throws NoeticError `lease_lost` when another harness still holds it after the
|
|
92
|
+
* wait, or when another harness won the swap; `cancelled` when `signal` aborts
|
|
93
|
+
* while it waits.
|
|
94
|
+
*/
|
|
95
|
+
static take(opts: TurnLeaseOptions, threadId: string, wait?: {
|
|
96
|
+
/** Ends the wait for a lease held elsewhere. */
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
/** A term the new one must be above: what the thread's record was last written at. */
|
|
99
|
+
above?: number;
|
|
100
|
+
}): Promise<TurnLease>;
|
|
101
|
+
get term(): number;
|
|
102
|
+
get holder(): string;
|
|
103
|
+
/** Name the execution this lease serves, for the errors it raises. */
|
|
104
|
+
bind(executionId: string): void;
|
|
105
|
+
/** True once this harness is known not to hold the lease. */
|
|
106
|
+
get lost(): boolean;
|
|
107
|
+
assert(): void;
|
|
108
|
+
lose(reason: string): NoeticErrorImpl;
|
|
109
|
+
/**
|
|
110
|
+
* Renew on a timer for as long as the turn runs. `onLost` is called once, when a
|
|
111
|
+
* renewal or a ledger write shows the lease is gone, so the turn can be aborted.
|
|
112
|
+
*/
|
|
113
|
+
keep(onLost: (reason: string) => void): void;
|
|
114
|
+
/**
|
|
115
|
+
* Push `heldUntil` out. A failed swap means another harness took the lease. One
|
|
116
|
+
* renewal runs at a time: a second, started while storage is slow, would compare
|
|
117
|
+
* against a record the first is about to replace and wrongly conclude it lost.
|
|
118
|
+
*/
|
|
119
|
+
renew(): Promise<void>;
|
|
120
|
+
private renewOnce;
|
|
121
|
+
verify(): Promise<void>;
|
|
122
|
+
/** Hand the lease back so the next turn need not wait for it to run out. */
|
|
123
|
+
release(): Promise<void>;
|
|
124
|
+
private markLost;
|
|
125
|
+
private stopTimer;
|
|
126
|
+
private error;
|
|
127
|
+
}
|
|
128
|
+
export {};
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The turn lease: what makes one harness the only writer of a thread's turn
|
|
3
|
+
* (spec 23 § One writer per turn).
|
|
4
|
+
*
|
|
5
|
+
* A lease is one small record per thread, taken with `StorageAdapter.swap`, so
|
|
6
|
+
* two harnesses that reach for the same turn cannot both win. It carries a
|
|
7
|
+
* `term` that goes up by one on every take. The step ledger tags what it writes
|
|
8
|
+
* with the term, which is how a harness that stalled and came back finds out it
|
|
9
|
+
* was replaced: its write loses to a higher term and it stops.
|
|
10
|
+
*
|
|
11
|
+
* Time bounds the lease, not correctness. `heldUntil` only decides when another
|
|
12
|
+
* harness may take over from a holder that went quiet; the term decides whose
|
|
13
|
+
* writes count.
|
|
14
|
+
*/
|
|
15
|
+
import { NoeticErrorImpl } from '@noetic-tools/types';
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
//#region Types
|
|
18
|
+
const LeaseRecordSchema = z.object({
|
|
19
|
+
/** Goes up by one on every take. */
|
|
20
|
+
term: z.number().int().nonnegative(),
|
|
21
|
+
/** The harness that holds it. */
|
|
22
|
+
holder: z.string(),
|
|
23
|
+
/** Epoch ms after which another harness may take over. `0` once released. */
|
|
24
|
+
heldUntil: z.number(),
|
|
25
|
+
});
|
|
26
|
+
/** @public How long a turn lease lasts between renewals when a harness does not say. */
|
|
27
|
+
export const DEFAULT_LEASE_TTL_MS = 3e4;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region Helpers
|
|
30
|
+
/** @internal True when `storage` can compare-and-set. */
|
|
31
|
+
export function canSwap(storage) {
|
|
32
|
+
return typeof storage?.swap === 'function';
|
|
33
|
+
}
|
|
34
|
+
/** How often `take` looks again at a lease it is waiting on. */
|
|
35
|
+
const LEASE_POLL_MS = 100;
|
|
36
|
+
/** Resolves after `ms`, or as soon as `signal` aborts. */
|
|
37
|
+
function sleep(ms, signal) {
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
const done = () => {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
signal?.removeEventListener('abort', done);
|
|
42
|
+
resolve();
|
|
43
|
+
};
|
|
44
|
+
const timer = setTimeout(done, Math.max(1, ms));
|
|
45
|
+
signal?.addEventListener('abort', done, {
|
|
46
|
+
once: true,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** True when a harness other than `holder` holds the lease and its time has not run out. */
|
|
51
|
+
function isHeldBy(record, holder, now) {
|
|
52
|
+
return record !== null && record.holder !== holder && record.heldUntil > now;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A prefix of its own. Under `session:`, the lease of thread `a` and the session
|
|
56
|
+
* of a thread named `a:lease` would be one key.
|
|
57
|
+
*/
|
|
58
|
+
function leaseKey(threadId) {
|
|
59
|
+
return `turn-lease:${threadId}`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The term to take the lease at. It only ever goes up: a lease that went back to
|
|
63
|
+
* term 1 would lose every write to the entries a higher term left in the ledger,
|
|
64
|
+
* and the thread could never run again.
|
|
65
|
+
*/
|
|
66
|
+
function nextTerm(stored, now) {
|
|
67
|
+
if (stored.record) {
|
|
68
|
+
return stored.record.term + 1;
|
|
69
|
+
}
|
|
70
|
+
if (stored.raw === null) {
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
// Something is stored and it is not a lease. Keep its term if it has one. If not,
|
|
74
|
+
// the clock is above any term a thread counted up to, and still goes up by one.
|
|
75
|
+
const term = typeof stored.raw === 'object' && 'term' in stored.raw ? stored.raw.term : undefined;
|
|
76
|
+
return typeof term === 'number' && Number.isSafeInteger(term) && term >= 0
|
|
77
|
+
? term + 1
|
|
78
|
+
: Math.max(1, Math.floor(now));
|
|
79
|
+
}
|
|
80
|
+
async function readLease(storage, threadId) {
|
|
81
|
+
const raw = await storage.get(leaseKey(threadId));
|
|
82
|
+
const parsed = raw === null ? undefined : LeaseRecordSchema.safeParse(raw);
|
|
83
|
+
return {
|
|
84
|
+
raw,
|
|
85
|
+
record: parsed?.success ? parsed.data : null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
/** @internal True when a harness other than `opts.holder` holds the thread's lease now. */
|
|
90
|
+
/**
|
|
91
|
+
* @internal
|
|
92
|
+
* When another harness's lease on the thread runs out, in epoch ms, or undefined
|
|
93
|
+
* when no other harness holds it now.
|
|
94
|
+
*/
|
|
95
|
+
export async function heldElsewhereUntil(opts, threadId) {
|
|
96
|
+
const { record } = await readLease(opts.storage, threadId);
|
|
97
|
+
const held = record !== null && record.holder !== opts.holder && record.heldUntil > opts.now();
|
|
98
|
+
return held ? record.heldUntil : undefined;
|
|
99
|
+
}
|
|
100
|
+
export async function heldElsewhere(opts, threadId) {
|
|
101
|
+
const { record } = await readLease(opts.storage, threadId);
|
|
102
|
+
return record !== null && record.holder !== opts.holder && record.heldUntil > opts.now();
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region TurnLease
|
|
106
|
+
/**
|
|
107
|
+
* @internal
|
|
108
|
+
* One held lease. `take` is the only way to get one; `renew` keeps it while the
|
|
109
|
+
* turn runs and `release` hands it back when the turn ends.
|
|
110
|
+
*/
|
|
111
|
+
export class TurnLease {
|
|
112
|
+
opts;
|
|
113
|
+
threadId;
|
|
114
|
+
record;
|
|
115
|
+
lostReason;
|
|
116
|
+
timer;
|
|
117
|
+
onLost;
|
|
118
|
+
/** The execution this lease serves, once the turn knows it. */
|
|
119
|
+
boundTo;
|
|
120
|
+
/** The renewal in flight, if one is. */
|
|
121
|
+
renewing;
|
|
122
|
+
constructor(opts, threadId, record) {
|
|
123
|
+
this.opts = opts;
|
|
124
|
+
this.threadId = threadId;
|
|
125
|
+
this.record = record;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Take the thread's lease. A lease another harness holds is waited on for up to
|
|
129
|
+
* `waitMs`: a holder that died stops renewing, so its lease runs out and the turn
|
|
130
|
+
* can be picked up without the host retrying.
|
|
131
|
+
*
|
|
132
|
+
* @throws NoeticError `lease_lost` when another harness still holds it after the
|
|
133
|
+
* wait, or when another harness won the swap; `cancelled` when `signal` aborts
|
|
134
|
+
* while it waits.
|
|
135
|
+
*/
|
|
136
|
+
static async take(opts, threadId, wait) {
|
|
137
|
+
const signal = wait?.signal;
|
|
138
|
+
let stored = await readLease(opts.storage, threadId);
|
|
139
|
+
let now = opts.now();
|
|
140
|
+
const giveUpAt = Date.now() + opts.waitMs;
|
|
141
|
+
while (isHeldBy(stored.record, opts.holder, now)) {
|
|
142
|
+
const left = giveUpAt - Date.now();
|
|
143
|
+
if (signal?.aborted) {
|
|
144
|
+
throw new NoeticErrorImpl({
|
|
145
|
+
kind: 'cancelled',
|
|
146
|
+
reason: typeof signal.reason === 'string' ? signal.reason : 'aborted',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (left <= 0) {
|
|
150
|
+
throw new NoeticErrorImpl({
|
|
151
|
+
kind: 'lease_lost',
|
|
152
|
+
executionId: threadId,
|
|
153
|
+
term: stored.record?.term ?? 0,
|
|
154
|
+
reason: `thread "${threadId}" is held by "${stored.record?.holder ?? 'another harness'}"`,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const untilFree = (stored.record?.heldUntil ?? now) - now;
|
|
158
|
+
await sleep(Math.min(left, untilFree, LEASE_POLL_MS), signal);
|
|
159
|
+
stored = await readLease(opts.storage, threadId);
|
|
160
|
+
now = opts.now();
|
|
161
|
+
}
|
|
162
|
+
const next = {
|
|
163
|
+
term: Math.max(nextTerm(stored, now), (wait?.above ?? 0) + 1),
|
|
164
|
+
holder: opts.holder,
|
|
165
|
+
heldUntil: now + opts.ttlMs,
|
|
166
|
+
};
|
|
167
|
+
const won = await opts.storage.swap(leaseKey(threadId), stored.raw, next);
|
|
168
|
+
if (!won) {
|
|
169
|
+
throw new NoeticErrorImpl({
|
|
170
|
+
kind: 'lease_lost',
|
|
171
|
+
executionId: threadId,
|
|
172
|
+
term: next.term,
|
|
173
|
+
reason: `another harness took thread "${threadId}" first`,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return new TurnLease(opts, threadId, next);
|
|
177
|
+
}
|
|
178
|
+
get term() {
|
|
179
|
+
return this.record.term;
|
|
180
|
+
}
|
|
181
|
+
get holder() {
|
|
182
|
+
return this.record.holder;
|
|
183
|
+
}
|
|
184
|
+
/** Name the execution this lease serves, for the errors it raises. */
|
|
185
|
+
bind(executionId) {
|
|
186
|
+
this.boundTo = executionId;
|
|
187
|
+
}
|
|
188
|
+
/** True once this harness is known not to hold the lease. */
|
|
189
|
+
get lost() {
|
|
190
|
+
return this.lostReason !== undefined;
|
|
191
|
+
}
|
|
192
|
+
assert() {
|
|
193
|
+
if (this.lostReason === undefined && this.record.heldUntil <= this.opts.now()) {
|
|
194
|
+
this.markLost('the lease ran out before it was renewed');
|
|
195
|
+
}
|
|
196
|
+
if (this.lostReason !== undefined) {
|
|
197
|
+
throw this.error(this.lostReason);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
lose(reason) {
|
|
201
|
+
this.markLost(reason);
|
|
202
|
+
return this.error(reason);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Renew on a timer for as long as the turn runs. `onLost` is called once, when a
|
|
206
|
+
* renewal or a ledger write shows the lease is gone, so the turn can be aborted.
|
|
207
|
+
*/
|
|
208
|
+
keep(onLost) {
|
|
209
|
+
this.onLost = onLost;
|
|
210
|
+
const every = Math.max(1, Math.floor(this.opts.ttlMs / 3));
|
|
211
|
+
this.timer = setInterval(() => {
|
|
212
|
+
void this.renew();
|
|
213
|
+
}, every);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Push `heldUntil` out. A failed swap means another harness took the lease. One
|
|
217
|
+
* renewal runs at a time: a second, started while storage is slow, would compare
|
|
218
|
+
* against a record the first is about to replace and wrongly conclude it lost.
|
|
219
|
+
*/
|
|
220
|
+
renew() {
|
|
221
|
+
this.renewing ??= this.renewOnce().finally(() => {
|
|
222
|
+
this.renewing = undefined;
|
|
223
|
+
});
|
|
224
|
+
return this.renewing;
|
|
225
|
+
}
|
|
226
|
+
async renewOnce() {
|
|
227
|
+
if (this.lostReason !== undefined) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const next = {
|
|
231
|
+
...this.record,
|
|
232
|
+
heldUntil: this.opts.now() + this.opts.ttlMs,
|
|
233
|
+
};
|
|
234
|
+
let won;
|
|
235
|
+
try {
|
|
236
|
+
won = await this.opts.storage.swap(leaseKey(this.threadId), this.record, next);
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// Storage is unreachable: keep going until `heldUntil`, when `assert` stops the turn.
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (!won) {
|
|
243
|
+
// An earlier renewal may have landed though its swap threw: storage then holds
|
|
244
|
+
// a later `heldUntil` than this harness remembers, under its own name and term.
|
|
245
|
+
const stored = await readLease(this.opts.storage, this.threadId).catch(() => undefined);
|
|
246
|
+
const own = stored?.record;
|
|
247
|
+
if (own && own.term === this.record.term && own.holder === this.record.holder) {
|
|
248
|
+
this.record = own;
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
this.markLost('another harness took the lease');
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.record = next;
|
|
255
|
+
}
|
|
256
|
+
async verify() {
|
|
257
|
+
this.assert();
|
|
258
|
+
const { record } = await readLease(this.opts.storage, this.threadId);
|
|
259
|
+
if (record?.term !== this.record.term || record.holder !== this.record.holder) {
|
|
260
|
+
this.markLost('another harness took the lease');
|
|
261
|
+
}
|
|
262
|
+
this.assert();
|
|
263
|
+
}
|
|
264
|
+
/** Hand the lease back so the next turn need not wait for it to run out. */
|
|
265
|
+
async release() {
|
|
266
|
+
this.stopTimer();
|
|
267
|
+
// A renewal in flight would otherwise land after this and keep the lease held.
|
|
268
|
+
await this.renewing;
|
|
269
|
+
if (this.lostReason !== undefined) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const released = {
|
|
273
|
+
...this.record,
|
|
274
|
+
heldUntil: 0,
|
|
275
|
+
};
|
|
276
|
+
try {
|
|
277
|
+
await this.opts.storage.swap(leaseKey(this.threadId), this.record, released);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
// The lease runs out on its own.
|
|
281
|
+
}
|
|
282
|
+
this.lostReason = 'released';
|
|
283
|
+
}
|
|
284
|
+
markLost(reason) {
|
|
285
|
+
if (this.lostReason !== undefined) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.lostReason = reason;
|
|
289
|
+
this.stopTimer();
|
|
290
|
+
this.onLost?.(reason);
|
|
291
|
+
}
|
|
292
|
+
stopTimer() {
|
|
293
|
+
if (this.timer !== undefined) {
|
|
294
|
+
clearInterval(this.timer);
|
|
295
|
+
this.timer = undefined;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
error(reason) {
|
|
299
|
+
return new NoeticErrorImpl({
|
|
300
|
+
kind: 'lease_lost',
|
|
301
|
+
executionId: this.boundTo ?? this.threadId,
|
|
302
|
+
term: this.record.term,
|
|
303
|
+
reason,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Held tool calls: a call left open for a person (spec 08 § Held tool calls).
|
|
3
|
+
*
|
|
4
|
+
* A call is held when the approver answers `hold`, when a tool that needs
|
|
5
|
+
* approval has no approver, or when a tool returns `hold()` in place of a
|
|
6
|
+
* result. Nothing is written to the conversation for it. Its record sits in
|
|
7
|
+
* storage until a decision passed to `harness.execute()` settles it; then the round walks its calls
|
|
8
|
+
* again, and this time the call has an answer.
|
|
9
|
+
*
|
|
10
|
+
* The record is the whole state, so a held call survives the process: a parked
|
|
11
|
+
* turn resumed on another harness finds the same record and the same answer.
|
|
12
|
+
*/
|
|
13
|
+
import type { StorageAdapter } from '@noetic-tools/context';
|
|
14
|
+
import type { Context, HeldCall, HeldResolution, ToolApprover } from '@noetic-tools/types';
|
|
15
|
+
import { z } from 'zod';
|
|
16
|
+
declare const HeldRecordSchema: z.ZodObject<{
|
|
17
|
+
callId: z.ZodString;
|
|
18
|
+
name: z.ZodString;
|
|
19
|
+
args: z.ZodUnknown;
|
|
20
|
+
argsHash: z.ZodString;
|
|
21
|
+
reason: z.ZodEnum<{
|
|
22
|
+
result: "result";
|
|
23
|
+
outcome: "outcome";
|
|
24
|
+
approval: "approval";
|
|
25
|
+
}>;
|
|
26
|
+
dueAt: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
onDue: z.ZodOptional<z.ZodEnum<{
|
|
28
|
+
deny: "deny";
|
|
29
|
+
keep: "keep";
|
|
30
|
+
}>>;
|
|
31
|
+
request: z.ZodOptional<z.ZodUnknown>;
|
|
32
|
+
resolution: z.ZodOptional<z.ZodObject<{
|
|
33
|
+
action: z.ZodOptional<z.ZodEnum<{
|
|
34
|
+
allow: "allow";
|
|
35
|
+
deny: "deny";
|
|
36
|
+
guide: "guide";
|
|
37
|
+
}>>;
|
|
38
|
+
guidance: z.ZodOptional<z.ZodString>;
|
|
39
|
+
output: z.ZodOptional<z.ZodUnknown>;
|
|
40
|
+
hasOutput: z.ZodOptional<z.ZodBoolean>;
|
|
41
|
+
}, z.core.$strip>>;
|
|
42
|
+
}, z.core.$strip>;
|
|
43
|
+
/** @internal A held call as stored. */
|
|
44
|
+
export type HeldRecord = z.infer<typeof HeldRecordSchema>;
|
|
45
|
+
/** @internal What held calls need of a harness: where to keep them, and its clock. */
|
|
46
|
+
export interface HoldHost {
|
|
47
|
+
readonly config: {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly storage?: StorageAdapter;
|
|
50
|
+
readonly now?: () => number;
|
|
51
|
+
readonly approveToolCall?: ToolApprover;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** @internal How long a hold lasts and what happens when that runs out. */
|
|
55
|
+
export interface HoldDue {
|
|
56
|
+
dueAt?: number;
|
|
57
|
+
onDue?: 'deny' | 'keep';
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
* Thrown by an approval seam to say "hold this call". Caught where the call's
|
|
62
|
+
* record can be written; never reaches a caller of the harness.
|
|
63
|
+
*/
|
|
64
|
+
export declare class HoldSignal extends Error {
|
|
65
|
+
readonly due: HoldDue;
|
|
66
|
+
constructor(due?: HoldDue);
|
|
67
|
+
}
|
|
68
|
+
/** The hash that ties an answer to one call: its tool name and arguments. */
|
|
69
|
+
export declare function heldArgsHash(name: string, args: unknown): string;
|
|
70
|
+
/** A held record as hosts see it. */
|
|
71
|
+
export declare function toHeldCall(record: HeldRecord): HeldCall;
|
|
72
|
+
/** @internal One harness's held calls: their records, and who is waiting on them here. */
|
|
73
|
+
export declare class HeldCalls {
|
|
74
|
+
private readonly storage;
|
|
75
|
+
private readonly clock;
|
|
76
|
+
private readonly waiters;
|
|
77
|
+
constructor(storage: StorageAdapter, clock: () => number);
|
|
78
|
+
now(): number;
|
|
79
|
+
/** The record of `callId`, when the call is held. */
|
|
80
|
+
load(executionId: string, callId: string): Promise<HeldRecord | undefined>;
|
|
81
|
+
/** Answers being taken, by execution and call, so two never overlap. */
|
|
82
|
+
private readonly answering;
|
|
83
|
+
/** Every held call of an execution. */
|
|
84
|
+
list(executionId: string): Promise<HeldRecord[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Hold a call. A call already held for the same thing keeps its record, and with
|
|
87
|
+
* it any answer. A record held for another reason is an earlier stage of the call
|
|
88
|
+
* (its approval, since given) and is replaced: a call is held for one thing at a
|
|
89
|
+
* time, and an old answer must not pass for the new question.
|
|
90
|
+
*/
|
|
91
|
+
open(executionId: string, record: HeldRecord): Promise<HeldRecord>;
|
|
92
|
+
/** Drop every held call and intent of an execution that is over or was abandoned. */
|
|
93
|
+
clear(executionId: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Note that a `durable` tool call is about to run. Written before the tool does
|
|
96
|
+
* anything, so a resumed run that finds it knows the call had started.
|
|
97
|
+
*/
|
|
98
|
+
arm(executionId: string, callId: string, argsHash: string): Promise<void>;
|
|
99
|
+
/** True when a call with these arguments was started and never finished. */
|
|
100
|
+
wasStarted(executionId: string, callId: string, argsHash: string): Promise<boolean>;
|
|
101
|
+
/** The call finished and its result is on record. */
|
|
102
|
+
disarm(executionId: string, callId: string): Promise<void>;
|
|
103
|
+
/** The call was settled and its result recorded: drop its record. */
|
|
104
|
+
close(executionId: string, callId: string): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Answer a held call. The first answer wins: the same answer again is a no-op, a
|
|
107
|
+
* different one is refused.
|
|
108
|
+
*
|
|
109
|
+
* @throws NoeticError `hold_refused` when the call is not held, `argsHash` names a
|
|
110
|
+
* different call, the answer is the wrong kind for the hold, or another answer
|
|
111
|
+
* already landed.
|
|
112
|
+
*/
|
|
113
|
+
resolve(executionId: string, answer: {
|
|
114
|
+
callId: string;
|
|
115
|
+
argsHash?: string;
|
|
116
|
+
resolution: HeldResolution;
|
|
117
|
+
}): Promise<void>;
|
|
118
|
+
private resolveOne;
|
|
119
|
+
/** True when other processes may share this storage, and so may answer a call. */
|
|
120
|
+
get shared(): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Start listening for a change to the execution's held calls. Listening begins
|
|
123
|
+
* when this returns, so an answer that lands while the caller is still reading
|
|
124
|
+
* the records is not missed. `changed` resolves on the first change or when
|
|
125
|
+
* `signal` aborts; call `stop` when done either way.
|
|
126
|
+
*/
|
|
127
|
+
listen(executionId: string, signal: AbortSignal): {
|
|
128
|
+
changed: Promise<void>;
|
|
129
|
+
stop: () => void;
|
|
130
|
+
};
|
|
131
|
+
/** Wake whoever in this process waits on the execution's held calls. */
|
|
132
|
+
notify(executionId: string): void;
|
|
133
|
+
}
|
|
134
|
+
/** @internal The held calls of `harness`, over its storage and clock. */
|
|
135
|
+
export declare function heldCallsOf(harness: HoldHost): HeldCalls;
|
|
136
|
+
/** @internal The execution a context's held calls are kept under: its root. */
|
|
137
|
+
export declare function heldScopeOf(ctx: Context): string;
|
|
138
|
+
/**
|
|
139
|
+
* @internal
|
|
140
|
+
* Wait until something about these held calls may have changed: an answer arrived,
|
|
141
|
+
* or a hold's deadline passed. While it waits the step is only waiting, so the
|
|
142
|
+
* turn may park (spec 23 § Parked turns).
|
|
143
|
+
*
|
|
144
|
+
* @throws NoeticError `cancelled` when the context is aborted, which is also how a
|
|
145
|
+
* parked turn leaves here.
|
|
146
|
+
*/
|
|
147
|
+
export declare function waitOnHeldCalls(opts: {
|
|
148
|
+
harness: HoldHost;
|
|
149
|
+
ctx: Context;
|
|
150
|
+
callIds: ReadonlyArray<string>;
|
|
151
|
+
/** Ends the wait as well as the context's own abort does. */
|
|
152
|
+
signal?: AbortSignal;
|
|
153
|
+
/** False when the turn must stay live while it waits. Default true. */
|
|
154
|
+
park?: boolean;
|
|
155
|
+
}): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* @internal
|
|
158
|
+
* False once the context's execution writes under a lease it no longer holds: a
|
|
159
|
+
* harness that was replaced must not open, close or disarm the new holder's calls
|
|
160
|
+
* (spec 23 § One writer per turn). An answer (`resolve`) is not a turn's write and
|
|
161
|
+
* is not fenced; it is a compare-and-set any harness may make.
|
|
162
|
+
*/
|
|
163
|
+
export declare function mayWriteHolds(ctx: Context): boolean;
|
|
164
|
+
/** @internal How a call held by `holdAndWait` ended. */
|
|
165
|
+
export interface HeldAnswer {
|
|
166
|
+
/** The answer, or undefined when the hold's deadline passed with none. */
|
|
167
|
+
resolution: HeldRecord['resolution'];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* @internal
|
|
171
|
+
* Hold a call from a caller with no model round to come back to (a step), and wait
|
|
172
|
+
* for its answer. While it waits the step is only waiting, so the turn may park; a
|
|
173
|
+
* resumed step calls this again and finds the same record, and its answer.
|
|
174
|
+
*
|
|
175
|
+
* @throws NoeticError `cancelled` when the context is aborted while it waits.
|
|
176
|
+
*/
|
|
177
|
+
export declare function holdAndWait(opts: {
|
|
178
|
+
harness: HoldHost;
|
|
179
|
+
ctx: Context;
|
|
180
|
+
record: HeldRecord;
|
|
181
|
+
/** Called once per process, when the call is first seen held. */
|
|
182
|
+
onHeld?: (held: HeldCall) => void;
|
|
183
|
+
}): Promise<HeldAnswer>;
|
|
184
|
+
/**
|
|
185
|
+
* @internal
|
|
186
|
+
* Says on the stream that a call is held: `{name}:tool_call_held`, with the fields
|
|
187
|
+
* `harness.pending()` returns. What a hold does when its caller gives no `onHeld`.
|
|
188
|
+
*/
|
|
189
|
+
export declare function announcerOf(harness: HoldHost, ctx: Context): (held: HeldCall) => void;
|
|
190
|
+
/** True once a hold with a `deny` deadline has run out. */
|
|
191
|
+
export declare function isOverdue(record: HeldRecord, now: number): boolean;
|
|
192
|
+
export {};
|