@noetic-tools/core 5.21.0 → 5.22.0-next.2
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/openrouter.d.ts +6 -1
- package/dist/adapters/openrouter.js +10 -0
- package/dist/builders/decide-builders.d.ts +7 -0
- package/dist/builders/tool-builder.d.ts +25 -1
- package/dist/builders/tool-builder.js +25 -1
- package/dist/builders/workflow-control-nodes.js +1 -0
- 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 +317 -4
- package/dist/harness/agent-harness.js +1264 -96
- package/dist/harness/connection-gate.d.ts +3 -0
- package/dist/harness/connection-gate.js +4 -0
- package/dist/harness/decide-call.d.ts +15 -0
- package/dist/harness/decide-call.js +57 -7
- 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 +91 -0
- package/dist/harness/llm-client.d.ts +2 -0
- package/dist/harness/llm-client.js +9 -5
- 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 +249 -22
- 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 +22 -8
- package/dist/index.js +12 -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-decide.js +5 -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/portable/runtime.d.ts +4 -0
- package/dist/portable/runtime.js +2 -0
- 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/event-broadcaster.d.ts +9 -1
- package/dist/runtime/event-broadcaster.js +16 -1
- 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/output-store.d.ts +88 -0
- package/dist/runtime/output-store.js +241 -0
- package/dist/runtime/session-runner.d.ts +18 -2
- package/dist/runtime/session-runner.js +88 -3
- package/dist/runtime/session-streams.d.ts +28 -7
- package/dist/runtime/session-streams.js +112 -67
- 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
- package/schema/noetic-workflow.schema.json +27 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-wake: a live harness watching over the turns it parked
|
|
3
|
+
* (spec 23 § Parked turns).
|
|
4
|
+
*
|
|
5
|
+
* Parking closes a turn's own listener, so without this a parked turn in a
|
|
6
|
+
* long-lived process would sit until something outside called `wake()`. A
|
|
7
|
+
* watcher stands in for the turn: a timer for its deadline, and a listener on
|
|
8
|
+
* each event it waits for, from the cursor the wait recorded. Either one wakes
|
|
9
|
+
* the turn.
|
|
10
|
+
*
|
|
11
|
+
* A watcher knows the event's type, not the wait's `filter`, which is code in
|
|
12
|
+
* the flow. So it wakes on any event of that type, and a turn the filter turns
|
|
13
|
+
* down parks again. It remembers the last cursor it saw, so each event wakes
|
|
14
|
+
* the turn at most once, and a burst of events wakes it once.
|
|
15
|
+
*/
|
|
16
|
+
/** @public How long auto-wake waits after an event before waking a turn, so a burst wakes it once. */
|
|
17
|
+
export const DEFAULT_WAKE_DEBOUNCE_MS = 250;
|
|
18
|
+
/** @public How often auto-wake reads the state of a long operation a parked turn waits on. */
|
|
19
|
+
export const DEFAULT_TASK_POLL_MS = 5e3;
|
|
20
|
+
/** The longest delay a timer takes; a later deadline is reached in several steps. */
|
|
21
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
22
|
+
/** How many times a wake is tried while the runner is still closing the parked turn out. */
|
|
23
|
+
const WAKE_ATTEMPTS = 100;
|
|
24
|
+
const WAKE_RETRY_MS = 20;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region Helpers
|
|
27
|
+
/** Resolves after `ms`, or as soon as `signal` aborts. Leaves no listener behind. */
|
|
28
|
+
function pause(ms, signal) {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
const done = () => {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
signal?.removeEventListener('abort', done);
|
|
33
|
+
resolve();
|
|
34
|
+
};
|
|
35
|
+
const timer = setTimeout(done, ms);
|
|
36
|
+
signal?.addEventListener('abort', done, {
|
|
37
|
+
once: true,
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region ParkWatchers
|
|
43
|
+
/** One harness's watchers, by thread. */
|
|
44
|
+
export class ParkWatchers {
|
|
45
|
+
host;
|
|
46
|
+
debounceMs;
|
|
47
|
+
taskPollMs;
|
|
48
|
+
watchers = new Map();
|
|
49
|
+
/** The last cursor seen for a wait, so a turn that parks again does not see it twice. */
|
|
50
|
+
seen = new Map();
|
|
51
|
+
/** The harness closed: a wake still being tried is given up. */
|
|
52
|
+
closed = false;
|
|
53
|
+
constructor(host, debounceMs, taskPollMs) {
|
|
54
|
+
this.host = host;
|
|
55
|
+
this.debounceMs = debounceMs;
|
|
56
|
+
this.taskPollMs = taskPollMs;
|
|
57
|
+
}
|
|
58
|
+
/** True when the thread's parked turn is being watched. */
|
|
59
|
+
has(threadId) {
|
|
60
|
+
return this.watchers.has(threadId);
|
|
61
|
+
}
|
|
62
|
+
/** Watch over a turn that just parked, or that storage says is parked. */
|
|
63
|
+
watch(threadId, turn) {
|
|
64
|
+
this.closed = false;
|
|
65
|
+
this.stop(threadId);
|
|
66
|
+
const watcher = {
|
|
67
|
+
controller: new AbortController(),
|
|
68
|
+
timer: undefined,
|
|
69
|
+
debounce: undefined,
|
|
70
|
+
};
|
|
71
|
+
this.watchers.set(threadId, watcher);
|
|
72
|
+
this.armDeadline(threadId, watcher, turn);
|
|
73
|
+
void this.listenForWaits(threadId, watcher, turn);
|
|
74
|
+
void this.pollTasks(threadId, watcher, turn).catch(() => undefined);
|
|
75
|
+
}
|
|
76
|
+
/** Stop watching a thread: its turn was woken, or is being. */
|
|
77
|
+
stop(threadId) {
|
|
78
|
+
const watcher = this.watchers.get(threadId);
|
|
79
|
+
if (!watcher) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.watchers.delete(threadId);
|
|
83
|
+
watcher.controller.abort();
|
|
84
|
+
clearTimeout(watcher.timer);
|
|
85
|
+
clearTimeout(watcher.debounce);
|
|
86
|
+
}
|
|
87
|
+
/** The turn ended: drop what was remembered about its waits. */
|
|
88
|
+
forget(threadId, executionId) {
|
|
89
|
+
this.stop(threadId);
|
|
90
|
+
for (const key of [
|
|
91
|
+
...this.seen.keys(),
|
|
92
|
+
]) {
|
|
93
|
+
if (key.startsWith(`${executionId}\u0000`)) {
|
|
94
|
+
this.seen.delete(key);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
stopAll() {
|
|
99
|
+
this.closed = true;
|
|
100
|
+
for (const threadId of [
|
|
101
|
+
...this.watchers.keys(),
|
|
102
|
+
]) {
|
|
103
|
+
this.stop(threadId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
armDeadline(threadId, watcher, turn) {
|
|
107
|
+
if (turn.until === undefined) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const until = turn.until;
|
|
111
|
+
const tick = () => {
|
|
112
|
+
const left = until - this.host.now();
|
|
113
|
+
if (left <= 0) {
|
|
114
|
+
void this.fire(threadId, watcher, turn);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
watcher.timer = setTimeout(tick, Math.min(left, MAX_TIMER_MS));
|
|
118
|
+
};
|
|
119
|
+
tick();
|
|
120
|
+
}
|
|
121
|
+
async listenForWaits(threadId, watcher, turn) {
|
|
122
|
+
let waits;
|
|
123
|
+
try {
|
|
124
|
+
waits = await this.host.waits(turn.executionId);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// The deadline still wakes it.
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
for (const wait of waits) {
|
|
131
|
+
void this.listenFor(threadId, watcher, turn, wait).catch(() => undefined);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Listen for one awaited event; the first one seen wakes the turn, after the debounce. */
|
|
135
|
+
async listenFor(threadId, watcher, turn, wait) {
|
|
136
|
+
const signal = watcher.controller.signal;
|
|
137
|
+
const key = `${turn.executionId}\u0000${wait.connection}\u0000${wait.event}`;
|
|
138
|
+
const stream = await this.host.listen(wait.connection, {
|
|
139
|
+
event: wait.event,
|
|
140
|
+
cursor: this.seen.get(key) ?? wait.cursor,
|
|
141
|
+
signal,
|
|
142
|
+
});
|
|
143
|
+
await stream.ready;
|
|
144
|
+
for await (const message of stream) {
|
|
145
|
+
if (signal.aborted) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (message.type !== 'event' || message.event.type !== wait.event) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (message.event.cursor !== null) {
|
|
152
|
+
this.seen.set(key, message.event.cursor);
|
|
153
|
+
}
|
|
154
|
+
// Keep reading during the debounce: later events of the burst move the cursor on.
|
|
155
|
+
watcher.debounce ??= setTimeout(() => {
|
|
156
|
+
void this.fire(threadId, watcher, turn);
|
|
157
|
+
}, this.debounceMs);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A long operation has no event to listen for, so its state is read every so
|
|
162
|
+
* often. The turn is woken when the state is no longer the one it parked on:
|
|
163
|
+
* finished, failed, or asking a question. A state it already woke for does not
|
|
164
|
+
* wake it again, or a turn waiting on a person's answer would never rest.
|
|
165
|
+
*/
|
|
166
|
+
async pollTasks(threadId, watcher, turn) {
|
|
167
|
+
const signal = watcher.controller.signal;
|
|
168
|
+
const tasks = await this.host.tasks(turn.executionId);
|
|
169
|
+
while (tasks.length > 0 && !signal.aborted) {
|
|
170
|
+
for (const task of tasks) {
|
|
171
|
+
const state = await this.host.taskState(task, signal);
|
|
172
|
+
const key = `${turn.executionId}\u0000task\u0000${task.taskId}`;
|
|
173
|
+
if (state === 'working') {
|
|
174
|
+
// Back at work: the state it left is news again the next time it comes,
|
|
175
|
+
// as when a task asks a second question.
|
|
176
|
+
this.seen.delete(key);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (state === undefined || this.seen.get(key) === state) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
this.seen.set(key, state);
|
|
183
|
+
void this.fire(threadId, watcher, turn);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
await pause(this.taskPollMs, signal);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Wake the turn. The runner may still be closing the parked turn out, so try for a while. */
|
|
190
|
+
async fire(threadId, watcher, turn) {
|
|
191
|
+
if (this.watchers.get(threadId) !== watcher) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.stop(threadId);
|
|
195
|
+
for (let attempt = 0; attempt < WAKE_ATTEMPTS; attempt += 1) {
|
|
196
|
+
// Watched over, replaced by a newer watch, or the harness closed: not ours to wake.
|
|
197
|
+
if (this.closed || this.watchers.has(threadId)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
// The turn this watcher stood in for is gone: another harness woke it, and
|
|
201
|
+
// what the thread holds now, if anything, is a different turn or the same
|
|
202
|
+
// one stopped. Waking by thread id would resume that one.
|
|
203
|
+
if ((await this.host.parked(threadId)) !== turn.executionId) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (await this.host.wake(threadId, turn.options)) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
await pause(WAKE_RETRY_MS);
|
|
210
|
+
}
|
|
211
|
+
// Every try failed and the turn is still parked: go on watching it, or nothing would.
|
|
212
|
+
if (!this.closed && !this.watchers.has(threadId)) {
|
|
213
|
+
this.watch(threadId, turn);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a `DecisionItem` a host passed to `execute()`: what it asks for, or why
|
|
3
|
+
* it cannot be taken. Reading changes nothing, so a list of decisions is read
|
|
4
|
+
* whole before any is acted on.
|
|
5
|
+
*/
|
|
6
|
+
import type { DecisionItem, ExecutionScope, HeldResolution, SessionScope } from '@noetic-tools/types';
|
|
7
|
+
/** A decision, read: the stuck turn to settle, or the held call to answer. */
|
|
8
|
+
export type ReadDecision = {
|
|
9
|
+
readonly threadId: string;
|
|
10
|
+
readonly settle: 'continue' | 'restart' | 'abandon';
|
|
11
|
+
} | {
|
|
12
|
+
readonly scope: SessionScope | ExecutionScope;
|
|
13
|
+
readonly answer: HeldResolution & {
|
|
14
|
+
readonly callId: string;
|
|
15
|
+
readonly argsHash?: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @throws NoeticError `decision_refused` when the decision carries both an action
|
|
20
|
+
* and an output, or an action of the wrong kind for a decision with, or without,
|
|
21
|
+
* a `callId`; `hold_refused` when its `output` is not a JSON value.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readDecision(threadId: string, decision: DecisionItem): ReadDecision;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a `DecisionItem` a host passed to `execute()`: what it asks for, or why
|
|
3
|
+
* it cannot be taken. Reading changes nothing, so a list of decisions is read
|
|
4
|
+
* whole before any is acted on.
|
|
5
|
+
*/
|
|
6
|
+
import { NoeticErrorImpl } from '@noetic-tools/types';
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region Helpers
|
|
9
|
+
/** True for a value storage can hold and hand back: JSON, with nothing dropped. */
|
|
10
|
+
function isJsonValue(value) {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.stringify(value) !== undefined;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function isSettling(action) {
|
|
19
|
+
return action === 'continue' || action === 'restart' || action === 'abandon';
|
|
20
|
+
}
|
|
21
|
+
function isAnswering(action) {
|
|
22
|
+
return action === 'allow' || action === 'deny' || action === 'guide';
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region Public API
|
|
26
|
+
/**
|
|
27
|
+
* @throws NoeticError `decision_refused` when the decision carries both an action
|
|
28
|
+
* and an output, or an action of the wrong kind for a decision with, or without,
|
|
29
|
+
* a `callId`; `hold_refused` when its `output` is not a JSON value.
|
|
30
|
+
*/
|
|
31
|
+
export function readDecision(threadId, decision) {
|
|
32
|
+
const loose = decision;
|
|
33
|
+
const { callId, action } = loose;
|
|
34
|
+
const hasOutput = 'output' in loose;
|
|
35
|
+
const refuse = (reason) => {
|
|
36
|
+
throw new NoeticErrorImpl({
|
|
37
|
+
kind: 'decision_refused',
|
|
38
|
+
threadId,
|
|
39
|
+
action: action === undefined ? 'output' : String(action),
|
|
40
|
+
reason,
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
if (hasOutput && action !== undefined) {
|
|
44
|
+
return refuse('it carries both an action and an output: give one');
|
|
45
|
+
}
|
|
46
|
+
if (callId === undefined) {
|
|
47
|
+
if (isSettling(action)) {
|
|
48
|
+
return {
|
|
49
|
+
threadId,
|
|
50
|
+
settle: action,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return refuse('it answers a held call: give its callId');
|
|
54
|
+
}
|
|
55
|
+
const scope = loose.executionId === undefined
|
|
56
|
+
? {
|
|
57
|
+
threadId,
|
|
58
|
+
}
|
|
59
|
+
: {
|
|
60
|
+
executionId: loose.executionId,
|
|
61
|
+
};
|
|
62
|
+
const target = {
|
|
63
|
+
callId,
|
|
64
|
+
argsHash: loose.argsHash,
|
|
65
|
+
};
|
|
66
|
+
if (hasOutput) {
|
|
67
|
+
if (!isJsonValue(loose.output)) {
|
|
68
|
+
throw new NoeticErrorImpl({
|
|
69
|
+
kind: 'hold_refused',
|
|
70
|
+
callId,
|
|
71
|
+
reason: 'its output is not a JSON value, so it cannot be recorded',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
scope,
|
|
76
|
+
answer: {
|
|
77
|
+
...target,
|
|
78
|
+
output: loose.output,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (!isAnswering(action)) {
|
|
83
|
+
return refuse(isSettling(action)
|
|
84
|
+
? 'it settles a stuck turn: it takes no callId'
|
|
85
|
+
: 'a decision for a held call is allow, deny or guide, or carries an output');
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
scope,
|
|
89
|
+
answer: {
|
|
90
|
+
...target,
|
|
91
|
+
action,
|
|
92
|
+
...(loose.guidance === undefined
|
|
93
|
+
? {}
|
|
94
|
+
: {
|
|
95
|
+
guidance: loose.guidance,
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
package/dist/harness/spend.js
CHANGED
|
@@ -48,6 +48,10 @@ export class Spend {
|
|
|
48
48
|
}
|
|
49
49
|
/** Write the record for a call about to be sent. A failed write never blocks the call. */
|
|
50
50
|
async open(params) {
|
|
51
|
+
// A harness that was replaced stops here, before it pays for anything more.
|
|
52
|
+
if (params.ctx instanceof ContextImpl) {
|
|
53
|
+
await params.ctx.ledger?.verifyHeld();
|
|
54
|
+
}
|
|
51
55
|
const root = rootOf(params.ctx);
|
|
52
56
|
const path = pathOf(params.ctx);
|
|
53
57
|
const { json, inputTokens } = measureRequest(params.body);
|
|
@@ -178,7 +178,9 @@ function findSessionQueue(ctx) {
|
|
|
178
178
|
}
|
|
179
179
|
/** What each delivery mode does at each point before any layer has a say. */
|
|
180
180
|
function defaultAction(point, mode) {
|
|
181
|
-
|
|
181
|
+
// At a round boundary, and at a round that is stuck on held calls and so has no
|
|
182
|
+
// boundary to reach, a steer lands.
|
|
183
|
+
if (point === SteerPoint.BeforeRound || point === SteerPoint.Held) {
|
|
182
184
|
return SteerAction.Apply;
|
|
183
185
|
}
|
|
184
186
|
if (point === SteerPoint.ItemDone && mode === 'next-item') {
|
|
@@ -8,10 +8,14 @@
|
|
|
8
8
|
* a harness built a moment ago behave the same.
|
|
9
9
|
*/
|
|
10
10
|
import type { Context, Item, SessionUsage } from '@noetic-tools/types';
|
|
11
|
+
import type { SessionRecord } from '../runtime/durable/session-store.js';
|
|
12
|
+
import type { LedgerFence } from '../runtime/durable/turn-lease.js';
|
|
11
13
|
import type { QueuedMessage, RestoreCheckpointOptions, SessionStore, TurnUsage } from './deps/runtime.js';
|
|
12
14
|
/** @internal The harness surface a thread record needs. */
|
|
13
15
|
export interface ThreadRecordHost {
|
|
14
16
|
readonly store: SessionStore;
|
|
17
|
+
/** The flow version this harness runs, when it names one. */
|
|
18
|
+
readonly version?: string;
|
|
15
19
|
restore(executionId: string, opts: RestoreCheckpointOptions): Promise<Context | null>;
|
|
16
20
|
clearCheckpoint(executionId: string): Promise<void>;
|
|
17
21
|
parseItems(raw: unknown[]): Item[];
|
|
@@ -28,7 +32,17 @@ export declare class ThreadRecord {
|
|
|
28
32
|
private readonly host;
|
|
29
33
|
private readonly threadId;
|
|
30
34
|
private queue;
|
|
35
|
+
/** The lease the turn in flight runs under, when storage can compare-and-set. */
|
|
36
|
+
private fence;
|
|
31
37
|
constructor(host: ThreadRecordHost, threadId: string);
|
|
38
|
+
/**
|
|
39
|
+
* Run the turn's own writes (`begin`, `end`) under `fence`: a harness that lost
|
|
40
|
+
* the lease does not overwrite the record, and a write that fails stops the turn
|
|
41
|
+
* (spec 23 § One writer per turn, § Fail-stop). `undefined` goes back to best-effort.
|
|
42
|
+
*/
|
|
43
|
+
hold(fence: LedgerFence | undefined): void;
|
|
44
|
+
/** The lease term the stored record was last written at, when it was written under one. */
|
|
45
|
+
term(): Promise<number | undefined>;
|
|
32
46
|
/** The history the thread's next turn builds on: what storage holds, or none. */
|
|
33
47
|
history(): Promise<Item[]>;
|
|
34
48
|
/** Replace the stored history (`seedSessionHistory`). */
|
|
@@ -39,8 +53,16 @@ export declare class ThreadRecord {
|
|
|
39
53
|
* dropped, its checkpoint cleared, and the new messages start a fresh turn.
|
|
40
54
|
*/
|
|
41
55
|
resume(messageIds: ReadonlyArray<string>, opts: RestoreCheckpointOptions): Promise<Context | undefined>;
|
|
56
|
+
/** Forget the turn in flight and what it recorded: the host aborted a parked turn. */
|
|
57
|
+
drop(): Promise<SessionRecord['turn']>;
|
|
42
58
|
/** Keep a steer sent into the running turn, so a resumed turn still applies it. */
|
|
43
59
|
keepSteer(message: QueuedMessage): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Keep a message a parked turn has not taken yet, steer or not: with no process
|
|
62
|
+
* holding the turn, storage is the only queue there is. An `interrupt` is never
|
|
63
|
+
* kept; it replaces the turn.
|
|
64
|
+
*/
|
|
65
|
+
keepQueued(message: QueuedMessage): Promise<void>;
|
|
44
66
|
/** The steers kept for the turn in flight, as queue messages. */
|
|
45
67
|
steers(): Promise<QueuedMessage[]>;
|
|
46
68
|
/**
|
|
@@ -48,6 +70,47 @@ export declare class ThreadRecord {
|
|
|
48
70
|
* delivers as input stop being steers: re-delivering the turn re-sends them.
|
|
49
71
|
*/
|
|
50
72
|
begin(ctx: Context, messageIds: ReadonlyArray<string>): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* The turn on `ctx` stopped rather than redo a recorded effect under a newer flow
|
|
75
|
+
* version. It stays, marked, until the host settles it.
|
|
76
|
+
*/
|
|
77
|
+
stick(ctx: Context, stuck: {
|
|
78
|
+
path: string;
|
|
79
|
+
from: string;
|
|
80
|
+
to: string;
|
|
81
|
+
}): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Settle a stuck turn. `continue` and `restart` clear the mark so the turn can
|
|
84
|
+
* run; `continue` also lets its next run discard what no longer matches.
|
|
85
|
+
* `abandon` forgets the turn. Resolves the turn it settled, or undefined when
|
|
86
|
+
* the thread has no stuck turn.
|
|
87
|
+
*/
|
|
88
|
+
settle(action: 'continue' | 'restart' | 'abandon'): Promise<SessionRecord['turn']>;
|
|
89
|
+
/**
|
|
90
|
+
* The turn on `ctx` parked: keep its record, marked with when to wake it. Its
|
|
91
|
+
* checkpoint and ledger stay, since they are what the woken turn resumes from.
|
|
92
|
+
*/
|
|
93
|
+
park(ctx: Context, until: number | undefined): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* The turn on `ctx` ended here without finishing. Best-effort: a turn whose mark
|
|
96
|
+
* does not land is simply taken for one that was cut off.
|
|
97
|
+
*/
|
|
98
|
+
stop(ctx: Context, reason: string, at: number): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* The turn on `ctx` gave its answer: remember it under the turn's message ids, so
|
|
101
|
+
* the same messages delivered again get it back (`answerFor`). Call before `end`,
|
|
102
|
+
* which forgets the turn. Only the last few turns are kept.
|
|
103
|
+
*/
|
|
104
|
+
answered(ctx: Context, text: string): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* The answer a finished turn gave to the message `messageId`, when one of the
|
|
107
|
+
* thread's recent turns delivered it.
|
|
108
|
+
*/
|
|
109
|
+
answerFor(messageId: string): Promise<string | undefined>;
|
|
110
|
+
/** The executions of the thread's turns that have a trail: recent finished ones, then the one in flight. */
|
|
111
|
+
executions(): Promise<string[]>;
|
|
112
|
+
/** The turn in flight, cut off or parked, when one is recorded. */
|
|
113
|
+
turn(): Promise<SessionRecord['turn']>;
|
|
51
114
|
/**
|
|
52
115
|
* The turn on `ctx` finished: store the history it left and drop its checkpoint.
|
|
53
116
|
* Only steers still queued are kept; the rest were applied or dropped.
|
|
@@ -58,8 +121,21 @@ export declare class ThreadRecord {
|
|
|
58
121
|
/** The stored totals. */
|
|
59
122
|
usage(): Promise<SessionUsage>;
|
|
60
123
|
private update;
|
|
61
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* A write the turn itself makes. Under a lease it carries the lease's term and is
|
|
126
|
+
* refused when the record already holds a higher one — another harness took the
|
|
127
|
+
* turn, whatever this one's clock says — and a write that fails throws instead of
|
|
128
|
+
* being logged (spec 23 § One writer per turn, § Fail-stop).
|
|
129
|
+
*/
|
|
130
|
+
private updateHeld;
|
|
131
|
+
/** Read-modify-write the stored record, best-effort. Call only from inside `run`. */
|
|
62
132
|
private write;
|
|
133
|
+
/**
|
|
134
|
+
* Apply `change` to what storage holds now. On storage that can compare-and-set
|
|
135
|
+
* the write lands only if nothing else wrote meanwhile, and is tried again on what
|
|
136
|
+
* the other writer left, so two harnesses never overwrite each other's fields.
|
|
137
|
+
*/
|
|
138
|
+
private commit;
|
|
63
139
|
private parseInput;
|
|
64
140
|
private load;
|
|
65
141
|
private run;
|