@noetic-tools/core 5.13.0 → 5.14.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/decide-builders.d.ts +2 -2
- package/dist/builders/dynamic-workflow.js +2 -0
- package/dist/builders/provide-builder.d.ts +2 -2
- package/dist/builders/spawn-builder.d.ts +2 -2
- package/dist/builders/step-builders.d.ts +5 -5
- package/dist/builders/workflow-hydrator.d.ts +1 -1
- package/dist/builders/workflow-hydrator.js +53 -3
- package/dist/expression/check.js +14 -0
- package/dist/expression/jsonata.d.ts +7 -0
- package/dist/expression/jsonata.js +11 -1
- package/dist/harness/agent-harness.d.ts +59 -5
- package/dist/harness/agent-harness.js +176 -25
- package/dist/harness/deps/runtime.d.ts +4 -0
- package/dist/harness/deps/runtime.js +2 -0
- package/dist/harness/model-call.js +69 -0
- package/dist/harness/model-rounds.d.ts +52 -0
- package/dist/harness/model-rounds.js +115 -0
- package/dist/harness/thread-record.d.ts +54 -0
- package/dist/harness/thread-record.js +159 -0
- package/dist/index.d.ts +2 -2
- package/dist/interpreter/emit-items.d.ts +19 -0
- package/dist/interpreter/emit-items.js +106 -0
- package/dist/interpreter/execute-action.js +4 -1
- package/dist/interpreter/execute-control.js +7 -3
- package/dist/interpreter/execute.js +66 -9
- package/dist/observability/span-impl.d.ts +9 -0
- package/dist/observability/span-impl.js +13 -0
- package/dist/runtime/context-impl.d.ts +156 -14
- package/dist/runtime/context-impl.js +326 -28
- package/dist/runtime/durable/harness-checkpoints.d.ts +13 -3
- package/dist/runtime/durable/harness-checkpoints.js +66 -7
- package/dist/runtime/durable/session-store.d.ts +37 -0
- package/dist/runtime/durable/session-store.js +58 -0
- package/dist/runtime/durable/step-ledger.d.ts +77 -40
- package/dist/runtime/durable/step-ledger.js +193 -73
- package/dist/runtime/item-log-impl.d.ts +6 -0
- package/dist/runtime/item-log-impl.js +12 -0
- package/dist/runtime/session-runner.d.ts +25 -1
- package/dist/runtime/session-runner.js +83 -11
- package/dist/schemas/workflow.d.ts +1 -1
- package/dist/types/checkpoint.d.ts +21 -4
- package/dist/types/checkpoint.js +33 -3
- package/package.json +2 -2
- package/schema/noetic-workflow.schema.json +391 -0
|
@@ -18,6 +18,7 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
18
18
|
stepCount: number;
|
|
19
19
|
tokens: TokenUsage;
|
|
20
20
|
cost: number;
|
|
21
|
+
billedCost: number;
|
|
21
22
|
state: unknown;
|
|
22
23
|
readonly parent: Context<ContextData> | null;
|
|
23
24
|
readonly usageParent: Context<ContextData> | null;
|
|
@@ -26,6 +27,8 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
26
27
|
readonly threadId: string;
|
|
27
28
|
readonly resourceId?: string;
|
|
28
29
|
readonly itemLog: ItemLog;
|
|
30
|
+
/** The same log, typed for `rebase`. */
|
|
31
|
+
private readonly log;
|
|
29
32
|
lastStepMeta: StepMeta | null;
|
|
30
33
|
lastLayerUsage: LastLayerUsage | undefined;
|
|
31
34
|
readonly harness: AgentHarnessContract;
|
|
@@ -77,25 +80,49 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
77
80
|
*/
|
|
78
81
|
private readonly _inFlight;
|
|
79
82
|
/**
|
|
80
|
-
* Execution path of the step whose lane this is. On the context itself it is
|
|
81
|
-
* prefix its steps hang under: empty on a root context, the parent's path plus
|
|
82
|
-
* discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
83
|
-
* step tree. A lane (see `enterStep`) reads its own step's
|
|
83
|
+
* Execution path node of the step whose lane this is. On the context itself it is
|
|
84
|
+
* the prefix its steps hang under: empty on a root context, the parent's path plus
|
|
85
|
+
* a discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
86
|
+
* whole step tree. A lane (see `enterStep`) reads its own step's node here instead.
|
|
87
|
+
* Each node counts the dispatches beneath it, so a loop body re-entered on each
|
|
88
|
+
* iteration gets `#0`, `#1`, … instead of colliding on one path.
|
|
84
89
|
* @internal
|
|
85
90
|
*/
|
|
86
|
-
private readonly
|
|
91
|
+
private readonly _node;
|
|
87
92
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
93
|
+
* Nodes on this context holding dispatch counts, so a run baseline can list every
|
|
94
|
+
* live count. Shared by every lane.
|
|
90
95
|
* @internal
|
|
91
96
|
*/
|
|
92
|
-
private readonly
|
|
97
|
+
private readonly _counted;
|
|
98
|
+
/**
|
|
99
|
+
* Dispatch counts restored from a checkpoint, keyed `<parent path>/<step id>`, not
|
|
100
|
+
* yet taken by a dispatch. A dispatch moves its count into its parent node; until
|
|
101
|
+
* this empties, each dispatch builds its parent's path to look its count up.
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
private readonly _restored;
|
|
93
105
|
/**
|
|
94
106
|
* Completion ledger for the execution this context belongs to. Shared by reference
|
|
95
107
|
* with inParallel/spawn children — one execution, one ledger.
|
|
96
108
|
* @internal
|
|
97
109
|
*/
|
|
98
110
|
readonly ledger?: StepLedger;
|
|
111
|
+
/**
|
|
112
|
+
* The dispatch this view belongs to. Undefined on the context itself; a lane (see
|
|
113
|
+
* `enterStep`) reads its own here.
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
private readonly _lane?;
|
|
117
|
+
/**
|
|
118
|
+
* The item log and dispatch counts as they stood when the outermost run on this
|
|
119
|
+
* context began. A checkpoint persists these rather than the live values, because a
|
|
120
|
+
* resumed run starts that run over: its recorded steps replay and re-append their
|
|
121
|
+
* items, so the log must not already hold them. Unset between runs, and kept after a
|
|
122
|
+
* run that failed — that run is what a resume repeats.
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
private _baseline?;
|
|
99
126
|
constructor(opts: {
|
|
100
127
|
harness: AgentHarnessContract;
|
|
101
128
|
parent?: Context;
|
|
@@ -108,8 +135,8 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
108
135
|
span?: Span;
|
|
109
136
|
channelStore?: ChannelStore;
|
|
110
137
|
checkpointFn?: (ctx: Context) => Promise<void>;
|
|
111
|
-
/** Path this context's steps hang under (inParallel/spawn children inherit one). */
|
|
112
|
-
|
|
138
|
+
/** Path node this context's steps hang under (inParallel/spawn children inherit one; see `branchPath`). */
|
|
139
|
+
path?: PathNode;
|
|
113
140
|
/** The execution's completion ledger, shared with children by reference. */
|
|
114
141
|
ledger?: StepLedger;
|
|
115
142
|
layers?: ContextLayer[];
|
|
@@ -174,12 +201,29 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
174
201
|
/**
|
|
175
202
|
* @internal
|
|
176
203
|
* Register `frame` as in flight and return the lane its step runs on: a view of
|
|
177
|
-
* this context that shares everything except its execution path
|
|
178
|
-
* the lane to the step, so steps the
|
|
179
|
-
* how other dispatches on this context
|
|
180
|
-
* here, at dispatch, never at settle.
|
|
204
|
+
* this context that shares everything except its execution path and the record of
|
|
205
|
+
* items appended through it. `execute()` hands the lane to the step, so steps the
|
|
206
|
+
* step dispatches hang under its path no matter how other dispatches on this context
|
|
207
|
+
* interleave. The occurrence ordinal is taken here, at dispatch, never at settle.
|
|
181
208
|
*/
|
|
182
209
|
enterStep(frame: FrontierFrame): ContextImpl;
|
|
210
|
+
/**
|
|
211
|
+
* @internal
|
|
212
|
+
* Mark this lane's step as kept out of the ledger (`durable: false`, `schedule`, a
|
|
213
|
+
* failed `replayKey`), so nothing running inside it records under its identity.
|
|
214
|
+
*/
|
|
215
|
+
skipLedger(): void;
|
|
216
|
+
/**
|
|
217
|
+
* @internal
|
|
218
|
+
* The dispatch this lane belongs to, or undefined on the context itself and on a
|
|
219
|
+
* lane kept out of the ledger. Lets code running inside a step (the model loop's
|
|
220
|
+
* rounds) record under the step's identity.
|
|
221
|
+
*/
|
|
222
|
+
currentDispatch(): {
|
|
223
|
+
path: string;
|
|
224
|
+
stepId: string;
|
|
225
|
+
inputHash: string | undefined;
|
|
226
|
+
} | undefined;
|
|
183
227
|
/**
|
|
184
228
|
* @internal
|
|
185
229
|
* The execution path of the step this lane belongs to — the ledger key identifying
|
|
@@ -187,12 +231,87 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
187
231
|
* what lets a resumed run line recorded outputs up with the steps that produced them.
|
|
188
232
|
*/
|
|
189
233
|
currentPath(): string;
|
|
234
|
+
/**
|
|
235
|
+
* @internal
|
|
236
|
+
* A path node under this lane's path, for an inParallel/spawn child context to hang
|
|
237
|
+
* its steps under. The child's path string is built only if something reads it.
|
|
238
|
+
*/
|
|
239
|
+
branchPath(segment: string): PathNode;
|
|
240
|
+
/**
|
|
241
|
+
* @internal
|
|
242
|
+
* Items appended to the item log through this lane, including by steps it
|
|
243
|
+
* dispatched on the same context. Empty on the context itself.
|
|
244
|
+
*/
|
|
245
|
+
appendedItems(): Item[];
|
|
246
|
+
/**
|
|
247
|
+
* @internal
|
|
248
|
+
* Indices into `appendedItems()` of the items appended through `appendEmitted`, which
|
|
249
|
+
* a replay streams again under the harness's `emitItemsOnReplay`.
|
|
250
|
+
*/
|
|
251
|
+
emittedIndices(): number[];
|
|
252
|
+
/**
|
|
253
|
+
* @internal
|
|
254
|
+
* Append an item a step emitted (`step.emitItems`), marking it in this lane's record
|
|
255
|
+
* and every record above it, so the ledger entry that holds it knows to stream it
|
|
256
|
+
* again on replay.
|
|
257
|
+
*/
|
|
258
|
+
appendEmitted(item: Item): void;
|
|
190
259
|
/**
|
|
191
260
|
* @internal
|
|
192
261
|
* Drop `frame` from the in-flight set. Called when its step resolves, success or
|
|
193
262
|
* failure.
|
|
194
263
|
*/
|
|
195
264
|
leaveStep(frame: FrontierFrame): void;
|
|
265
|
+
/**
|
|
266
|
+
* @internal
|
|
267
|
+
* Give back this lane's ordinal after its step failed, so a retry runs at the same
|
|
268
|
+
* path. A resumed run never sees the failed attempt — nothing records a failure — so
|
|
269
|
+
* without this a retry would take the next ordinal and every later dispatch would sit
|
|
270
|
+
* one position past where the resume looks for it. Only the latest dispatch of a key
|
|
271
|
+
* can give its ordinal back; once a later one has taken the next, the ordinal stays.
|
|
272
|
+
*/
|
|
273
|
+
releaseDispatch(): void;
|
|
274
|
+
/**
|
|
275
|
+
* @internal
|
|
276
|
+
* Mark the start of the outermost run on this context. See `_baseline`.
|
|
277
|
+
*/
|
|
278
|
+
beginRun(): void;
|
|
279
|
+
/**
|
|
280
|
+
* @internal
|
|
281
|
+
* Mark a run as finished, so checkpoints record the live log and counts again.
|
|
282
|
+
*/
|
|
283
|
+
endRun(): void;
|
|
284
|
+
/**
|
|
285
|
+
* @internal
|
|
286
|
+
* What a checkpoint persists for the item log and dispatch counts — see `_baseline`.
|
|
287
|
+
*/
|
|
288
|
+
runBaseline(): RunBaseline;
|
|
289
|
+
/**
|
|
290
|
+
* @internal
|
|
291
|
+
* Whether a run on this context began and never finished — it failed, or is in
|
|
292
|
+
* flight. A run starting now repeats it.
|
|
293
|
+
*/
|
|
294
|
+
hasUnfinishedRun(): boolean;
|
|
295
|
+
/**
|
|
296
|
+
* @internal
|
|
297
|
+
* Stand the context back at a stored run baseline: the item log and dispatch counts
|
|
298
|
+
* a checkpoint recorded. For a run retried in place, which must see what a resume on
|
|
299
|
+
* a new harness would — not the items and counts its failed attempt left in memory.
|
|
300
|
+
*/
|
|
301
|
+
rebase(baseline: RunBaseline): void;
|
|
302
|
+
/**
|
|
303
|
+
* @internal
|
|
304
|
+
* Seed the dispatch counts a restored run starts from, so a resumed context numbers
|
|
305
|
+
* its dispatches where the recorded run did rather than from zero.
|
|
306
|
+
*/
|
|
307
|
+
restoreOccurrences(occurrences: Record<string, number>): void;
|
|
308
|
+
/**
|
|
309
|
+
* Every live dispatch count, keyed `<parent path>/<step id>` — the form checkpoints
|
|
310
|
+
* persist and `restoreOccurrences` reads back.
|
|
311
|
+
*/
|
|
312
|
+
private liveCounts;
|
|
313
|
+
/** The restored count for `stepId` under `parent`, consumed on first use; 0 when none. */
|
|
314
|
+
private takeRestored;
|
|
196
315
|
/**
|
|
197
316
|
* @internal
|
|
198
317
|
* Return a defensive copy of the current frontier. Consumed by the
|
|
@@ -200,6 +319,29 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
200
319
|
*/
|
|
201
320
|
serialiseFrontier(): FrontierFrame[];
|
|
202
321
|
}
|
|
322
|
+
/** @internal Item log and dispatch counts at the start of a run — see `ContextImpl._baseline`. */
|
|
323
|
+
export interface RunBaseline {
|
|
324
|
+
items: Item[];
|
|
325
|
+
occurrences: Record<string, number>;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* @internal
|
|
329
|
+
* One point in the execution path tree: a context's root, or one dispatch. It
|
|
330
|
+
* holds the dispatch counts for steps directly beneath it, keyed by step id, and
|
|
331
|
+
* builds its path string only when something reads it. Deep recursion makes
|
|
332
|
+
* paths long — each level adds a segment and its ids grow — so building every
|
|
333
|
+
* dispatch's path eagerly cost time cubic in depth even when no ledger read it.
|
|
334
|
+
*/
|
|
335
|
+
export declare class PathNode {
|
|
336
|
+
private readonly parent;
|
|
337
|
+
private readonly segment;
|
|
338
|
+
/** Dispatches beneath this path, by step id; the count is the next ordinal. */
|
|
339
|
+
readonly counts: Map<string, number>;
|
|
340
|
+
private text?;
|
|
341
|
+
constructor(parent: PathNode | undefined, segment: string);
|
|
342
|
+
/** The full path: the parent's path plus this node's segment. Built once, on first read. */
|
|
343
|
+
get path(): string;
|
|
344
|
+
}
|
|
203
345
|
/**
|
|
204
346
|
* @internal
|
|
205
347
|
* The context a lane views, or `ctx` itself when it is not a lane. For per-execution
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { buildContextData, defaultItemSchemaRegistry } from './context-deps.js';
|
|
2
|
+
import { hashStepInput } from './durable/step-ledger.js';
|
|
2
3
|
import { ItemLogImpl } from './item-log-impl.js';
|
|
3
4
|
const EMPTY_CONTEXT = Object.freeze({});
|
|
4
5
|
class NoopSpan {
|
|
@@ -48,6 +49,7 @@ export class ContextImpl {
|
|
|
48
49
|
total: 0,
|
|
49
50
|
};
|
|
50
51
|
cost = 0;
|
|
52
|
+
billedCost = 0;
|
|
51
53
|
state;
|
|
52
54
|
parent;
|
|
53
55
|
usageParent;
|
|
@@ -56,6 +58,8 @@ export class ContextImpl {
|
|
|
56
58
|
threadId;
|
|
57
59
|
resourceId;
|
|
58
60
|
itemLog;
|
|
61
|
+
/** The same log, typed for `rebase`. */
|
|
62
|
+
log;
|
|
59
63
|
lastStepMeta = null;
|
|
60
64
|
lastLayerUsage = undefined;
|
|
61
65
|
harness;
|
|
@@ -107,25 +111,49 @@ export class ContextImpl {
|
|
|
107
111
|
*/
|
|
108
112
|
_inFlight = new Set();
|
|
109
113
|
/**
|
|
110
|
-
* Execution path of the step whose lane this is. On the context itself it is
|
|
111
|
-
* prefix its steps hang under: empty on a root context, the parent's path plus
|
|
112
|
-
* discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
113
|
-
* step tree. A lane (see `enterStep`) reads its own step's
|
|
114
|
+
* Execution path node of the step whose lane this is. On the context itself it is
|
|
115
|
+
* the prefix its steps hang under: empty on a root context, the parent's path plus
|
|
116
|
+
* a discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
117
|
+
* whole step tree. A lane (see `enterStep`) reads its own step's node here instead.
|
|
118
|
+
* Each node counts the dispatches beneath it, so a loop body re-entered on each
|
|
119
|
+
* iteration gets `#0`, `#1`, … instead of colliding on one path.
|
|
114
120
|
* @internal
|
|
115
121
|
*/
|
|
116
|
-
|
|
122
|
+
_node;
|
|
117
123
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
124
|
+
* Nodes on this context holding dispatch counts, so a run baseline can list every
|
|
125
|
+
* live count. Shared by every lane.
|
|
120
126
|
* @internal
|
|
121
127
|
*/
|
|
122
|
-
|
|
128
|
+
_counted = new Set();
|
|
129
|
+
/**
|
|
130
|
+
* Dispatch counts restored from a checkpoint, keyed `<parent path>/<step id>`, not
|
|
131
|
+
* yet taken by a dispatch. A dispatch moves its count into its parent node; until
|
|
132
|
+
* this empties, each dispatch builds its parent's path to look its count up.
|
|
133
|
+
* @internal
|
|
134
|
+
*/
|
|
135
|
+
_restored = new Map();
|
|
123
136
|
/**
|
|
124
137
|
* Completion ledger for the execution this context belongs to. Shared by reference
|
|
125
138
|
* with inParallel/spawn children — one execution, one ledger.
|
|
126
139
|
* @internal
|
|
127
140
|
*/
|
|
128
141
|
ledger;
|
|
142
|
+
/**
|
|
143
|
+
* The dispatch this view belongs to. Undefined on the context itself; a lane (see
|
|
144
|
+
* `enterStep`) reads its own here.
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
_lane;
|
|
148
|
+
/**
|
|
149
|
+
* The item log and dispatch counts as they stood when the outermost run on this
|
|
150
|
+
* context began. A checkpoint persists these rather than the live values, because a
|
|
151
|
+
* resumed run starts that run over: its recorded steps replay and re-append their
|
|
152
|
+
* items, so the log must not already hold them. Unset between runs, and kept after a
|
|
153
|
+
* run that failed — that run is what a resume repeats.
|
|
154
|
+
* @internal
|
|
155
|
+
*/
|
|
156
|
+
_baseline;
|
|
129
157
|
constructor(opts) {
|
|
130
158
|
this.id = opts.id ?? crypto.randomUUID();
|
|
131
159
|
this._createdAt = Date.now();
|
|
@@ -139,7 +167,7 @@ export class ContextImpl {
|
|
|
139
167
|
this.resourceId = opts.resourceId;
|
|
140
168
|
this.channelStore = opts.channelStore;
|
|
141
169
|
this._checkpointFn = opts.checkpointFn;
|
|
142
|
-
this.
|
|
170
|
+
this._node = opts.path ?? new PathNode(undefined, '');
|
|
143
171
|
this.ledger = opts.ledger;
|
|
144
172
|
this.layers = opts.layers;
|
|
145
173
|
this.unifiedTools = opts.unifiedTools;
|
|
@@ -155,6 +183,7 @@ export class ContextImpl {
|
|
|
155
183
|
}
|
|
156
184
|
}
|
|
157
185
|
this.itemLog = log;
|
|
186
|
+
this.log = log;
|
|
158
187
|
// Join the parent's abort cascade last, so the child is fully constructed
|
|
159
188
|
// before an already-aborted parent aborts it.
|
|
160
189
|
if (this.parent instanceof ContextImpl) {
|
|
@@ -295,17 +324,52 @@ export class ContextImpl {
|
|
|
295
324
|
/**
|
|
296
325
|
* @internal
|
|
297
326
|
* Register `frame` as in flight and return the lane its step runs on: a view of
|
|
298
|
-
* this context that shares everything except its execution path
|
|
299
|
-
* the lane to the step, so steps the
|
|
300
|
-
* how other dispatches on this context
|
|
301
|
-
* here, at dispatch, never at settle.
|
|
327
|
+
* this context that shares everything except its execution path and the record of
|
|
328
|
+
* items appended through it. `execute()` hands the lane to the step, so steps the
|
|
329
|
+
* step dispatches hang under its path no matter how other dispatches on this context
|
|
330
|
+
* interleave. The occurrence ordinal is taken here, at dispatch, never at settle.
|
|
302
331
|
*/
|
|
303
332
|
enterStep(frame) {
|
|
304
|
-
const
|
|
305
|
-
const occurrence =
|
|
306
|
-
|
|
333
|
+
const parent = this._node;
|
|
334
|
+
const occurrence = parent.counts.get(frame.stepId) ?? this.takeRestored(parent, frame.stepId);
|
|
335
|
+
parent.counts.set(frame.stepId, occurrence + 1);
|
|
336
|
+
this._counted.add(parent);
|
|
307
337
|
this._inFlight.add(frame);
|
|
308
|
-
return laneOf(this,
|
|
338
|
+
return laneOf(this, {
|
|
339
|
+
node: new PathNode(parent, `/${frame.stepId}#${occurrence}`),
|
|
340
|
+
parent,
|
|
341
|
+
occurrence,
|
|
342
|
+
stepId: frame.stepId,
|
|
343
|
+
inputHash: this.ledger ? hashStepInput(frame.input) : undefined,
|
|
344
|
+
recorded: true,
|
|
345
|
+
appended: new AppendRecord(this._lane?.appended),
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* @internal
|
|
350
|
+
* Mark this lane's step as kept out of the ledger (`durable: false`, `schedule`, a
|
|
351
|
+
* failed `replayKey`), so nothing running inside it records under its identity.
|
|
352
|
+
*/
|
|
353
|
+
skipLedger() {
|
|
354
|
+
if (this._lane) {
|
|
355
|
+
this._lane.recorded = false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* @internal
|
|
360
|
+
* The dispatch this lane belongs to, or undefined on the context itself and on a
|
|
361
|
+
* lane kept out of the ledger. Lets code running inside a step (the model loop's
|
|
362
|
+
* rounds) record under the step's identity.
|
|
363
|
+
*/
|
|
364
|
+
currentDispatch() {
|
|
365
|
+
const lane = this._lane;
|
|
366
|
+
return lane?.recorded
|
|
367
|
+
? {
|
|
368
|
+
path: lane.node.path,
|
|
369
|
+
stepId: lane.stepId,
|
|
370
|
+
inputHash: lane.inputHash,
|
|
371
|
+
}
|
|
372
|
+
: undefined;
|
|
309
373
|
}
|
|
310
374
|
/**
|
|
311
375
|
* @internal
|
|
@@ -314,7 +378,52 @@ export class ContextImpl {
|
|
|
314
378
|
* what lets a resumed run line recorded outputs up with the steps that produced them.
|
|
315
379
|
*/
|
|
316
380
|
currentPath() {
|
|
317
|
-
return this.
|
|
381
|
+
return this._node.path;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* @internal
|
|
385
|
+
* A path node under this lane's path, for an inParallel/spawn child context to hang
|
|
386
|
+
* its steps under. The child's path string is built only if something reads it.
|
|
387
|
+
*/
|
|
388
|
+
branchPath(segment) {
|
|
389
|
+
return new PathNode(this._node, segment);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* @internal
|
|
393
|
+
* Items appended to the item log through this lane, including by steps it
|
|
394
|
+
* dispatched on the same context. Empty on the context itself.
|
|
395
|
+
*/
|
|
396
|
+
appendedItems() {
|
|
397
|
+
return [
|
|
398
|
+
...(this._lane?.appended.items ?? []),
|
|
399
|
+
];
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* @internal
|
|
403
|
+
* Indices into `appendedItems()` of the items appended through `appendEmitted`, which
|
|
404
|
+
* a replay streams again under the harness's `emitItemsOnReplay`.
|
|
405
|
+
*/
|
|
406
|
+
emittedIndices() {
|
|
407
|
+
const record = this._lane?.appended;
|
|
408
|
+
if (!record) {
|
|
409
|
+
return [];
|
|
410
|
+
}
|
|
411
|
+
return record.items.flatMap((item, i) => record.emitted.has(item)
|
|
412
|
+
? [
|
|
413
|
+
i,
|
|
414
|
+
]
|
|
415
|
+
: []);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* @internal
|
|
419
|
+
* Append an item a step emitted (`step.emitItems`), marking it in this lane's record
|
|
420
|
+
* and every record above it, so the ledger entry that holds it knows to stream it
|
|
421
|
+
* again on replay.
|
|
422
|
+
*/
|
|
423
|
+
appendEmitted(item) {
|
|
424
|
+
this.itemLog.append(item);
|
|
425
|
+
const items = this.itemLog.items;
|
|
426
|
+
this._lane?.appended.markEmitted(items[items.length - 1]);
|
|
318
427
|
}
|
|
319
428
|
/**
|
|
320
429
|
* @internal
|
|
@@ -323,6 +432,132 @@ export class ContextImpl {
|
|
|
323
432
|
*/
|
|
324
433
|
leaveStep(frame) {
|
|
325
434
|
this._inFlight.delete(frame);
|
|
435
|
+
// Nothing dispatches under a finished step's path again, so the counts beneath it
|
|
436
|
+
// are dead weight. Its own count lives in its parent node and stays: the next
|
|
437
|
+
// dispatch of the same step id needs it.
|
|
438
|
+
const lane = this._lane;
|
|
439
|
+
if (lane) {
|
|
440
|
+
lane.node.counts.clear();
|
|
441
|
+
this._counted.delete(lane.node);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* @internal
|
|
446
|
+
* Give back this lane's ordinal after its step failed, so a retry runs at the same
|
|
447
|
+
* path. A resumed run never sees the failed attempt — nothing records a failure — so
|
|
448
|
+
* without this a retry would take the next ordinal and every later dispatch would sit
|
|
449
|
+
* one position past where the resume looks for it. Only the latest dispatch of a key
|
|
450
|
+
* can give its ordinal back; once a later one has taken the next, the ordinal stays.
|
|
451
|
+
*/
|
|
452
|
+
releaseDispatch() {
|
|
453
|
+
const lane = this._lane;
|
|
454
|
+
if (!lane || lane.parent.counts.get(lane.stepId) !== lane.occurrence + 1) {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
lane.parent.counts.set(lane.stepId, lane.occurrence);
|
|
458
|
+
lane.node.counts.clear();
|
|
459
|
+
this._counted.delete(lane.node);
|
|
460
|
+
if (this._restored.size === 0) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const beneath = `${lane.node.path}/`;
|
|
464
|
+
for (const key of [
|
|
465
|
+
...this._restored.keys(),
|
|
466
|
+
]) {
|
|
467
|
+
if (key.startsWith(beneath)) {
|
|
468
|
+
this._restored.delete(key);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* @internal
|
|
474
|
+
* Mark the start of the outermost run on this context. See `_baseline`.
|
|
475
|
+
*/
|
|
476
|
+
beginRun() {
|
|
477
|
+
this._baseline = {
|
|
478
|
+
items: [
|
|
479
|
+
...this.itemLog.items,
|
|
480
|
+
],
|
|
481
|
+
occurrences: this.liveCounts(),
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* @internal
|
|
486
|
+
* Mark a run as finished, so checkpoints record the live log and counts again.
|
|
487
|
+
*/
|
|
488
|
+
endRun() {
|
|
489
|
+
this._baseline = undefined;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* @internal
|
|
493
|
+
* What a checkpoint persists for the item log and dispatch counts — see `_baseline`.
|
|
494
|
+
*/
|
|
495
|
+
runBaseline() {
|
|
496
|
+
return (this._baseline ?? {
|
|
497
|
+
items: [
|
|
498
|
+
...this.itemLog.items,
|
|
499
|
+
],
|
|
500
|
+
occurrences: this.liveCounts(),
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* @internal
|
|
505
|
+
* Whether a run on this context began and never finished — it failed, or is in
|
|
506
|
+
* flight. A run starting now repeats it.
|
|
507
|
+
*/
|
|
508
|
+
hasUnfinishedRun() {
|
|
509
|
+
return this._baseline !== undefined;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* @internal
|
|
513
|
+
* Stand the context back at a stored run baseline: the item log and dispatch counts
|
|
514
|
+
* a checkpoint recorded. For a run retried in place, which must see what a resume on
|
|
515
|
+
* a new harness would — not the items and counts its failed attempt left in memory.
|
|
516
|
+
*/
|
|
517
|
+
rebase(baseline) {
|
|
518
|
+
this.log.replace(baseline.items);
|
|
519
|
+
for (const node of this._counted) {
|
|
520
|
+
node.counts.clear();
|
|
521
|
+
}
|
|
522
|
+
this._counted.clear();
|
|
523
|
+
this._restored.clear();
|
|
524
|
+
this.restoreOccurrences(baseline.occurrences);
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* @internal
|
|
528
|
+
* Seed the dispatch counts a restored run starts from, so a resumed context numbers
|
|
529
|
+
* its dispatches where the recorded run did rather than from zero.
|
|
530
|
+
*/
|
|
531
|
+
restoreOccurrences(occurrences) {
|
|
532
|
+
for (const [key, count] of Object.entries(occurrences)) {
|
|
533
|
+
this._restored.set(key, count);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Every live dispatch count, keyed `<parent path>/<step id>` — the form checkpoints
|
|
538
|
+
* persist and `restoreOccurrences` reads back.
|
|
539
|
+
*/
|
|
540
|
+
liveCounts() {
|
|
541
|
+
const counts = Object.fromEntries(this._restored);
|
|
542
|
+
for (const node of this._counted) {
|
|
543
|
+
for (const [stepId, count] of node.counts) {
|
|
544
|
+
counts[`${node.path}/${stepId}`] = count;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return counts;
|
|
548
|
+
}
|
|
549
|
+
/** The restored count for `stepId` under `parent`, consumed on first use; 0 when none. */
|
|
550
|
+
takeRestored(parent, stepId) {
|
|
551
|
+
if (this._restored.size === 0) {
|
|
552
|
+
return 0;
|
|
553
|
+
}
|
|
554
|
+
const key = `${parent.path}/${stepId}`;
|
|
555
|
+
const count = this._restored.get(key);
|
|
556
|
+
if (count === undefined) {
|
|
557
|
+
return 0;
|
|
558
|
+
}
|
|
559
|
+
this._restored.delete(key);
|
|
560
|
+
return count;
|
|
326
561
|
}
|
|
327
562
|
/**
|
|
328
563
|
* @internal
|
|
@@ -339,21 +574,84 @@ export class ContextImpl {
|
|
|
339
574
|
}));
|
|
340
575
|
}
|
|
341
576
|
}
|
|
342
|
-
|
|
577
|
+
/**
|
|
578
|
+
* @internal
|
|
579
|
+
* One point in the execution path tree: a context's root, or one dispatch. It
|
|
580
|
+
* holds the dispatch counts for steps directly beneath it, keyed by step id, and
|
|
581
|
+
* builds its path string only when something reads it. Deep recursion makes
|
|
582
|
+
* paths long — each level adds a segment and its ids grow — so building every
|
|
583
|
+
* dispatch's path eagerly cost time cubic in depth even when no ledger read it.
|
|
584
|
+
*/
|
|
585
|
+
export class PathNode {
|
|
586
|
+
parent;
|
|
587
|
+
segment;
|
|
588
|
+
/** Dispatches beneath this path, by step id; the count is the next ordinal. */
|
|
589
|
+
counts = new Map();
|
|
590
|
+
text;
|
|
591
|
+
constructor(parent, segment) {
|
|
592
|
+
this.parent = parent;
|
|
593
|
+
this.segment = segment;
|
|
594
|
+
}
|
|
595
|
+
/** The full path: the parent's path plus this node's segment. Built once, on first read. */
|
|
596
|
+
get path() {
|
|
597
|
+
if (this.text === undefined) {
|
|
598
|
+
this.text = (this.parent?.path ?? '') + this.segment;
|
|
599
|
+
}
|
|
600
|
+
return this.text;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Items appended through one lane. Each record also feeds its parent lane's, so a
|
|
605
|
+
* composite step's record holds everything appended beneath it on the same context.
|
|
606
|
+
*/
|
|
607
|
+
class AppendRecord {
|
|
608
|
+
parent;
|
|
609
|
+
items = [];
|
|
610
|
+
/** The items among `items` a step emitted through `emitItems`. */
|
|
611
|
+
emitted = new Set();
|
|
612
|
+
constructor(parent) {
|
|
613
|
+
this.parent = parent;
|
|
614
|
+
}
|
|
615
|
+
add(item) {
|
|
616
|
+
this.items.push(item);
|
|
617
|
+
this.parent?.add(item);
|
|
618
|
+
}
|
|
619
|
+
markEmitted(item) {
|
|
620
|
+
this.emitted.add(item);
|
|
621
|
+
this.parent?.markEmitted(item);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
343
624
|
/** The context each lane views, so `laneOf` never stacks a proxy on a proxy. */
|
|
344
625
|
const laneTargets = new WeakMap();
|
|
345
626
|
/**
|
|
346
|
-
* A view of `ctx` that reads `
|
|
347
|
-
*
|
|
348
|
-
*
|
|
627
|
+
* A view of `ctx` that reads `lane` as its dispatch and forwards every other read and
|
|
628
|
+
* write to the context itself, so state, usage, abort, and the in-flight set stay
|
|
629
|
+
* shared. Its item log appends to the shared log and notes each item in the lane's
|
|
630
|
+
* record. A Proxy rather than a subclass: steps assign `ctx.state` and friends
|
|
349
631
|
* directly, and a copy would keep those writes to itself.
|
|
350
632
|
*/
|
|
351
|
-
function laneOf(ctx,
|
|
633
|
+
function laneOf(ctx, lane) {
|
|
352
634
|
const target = laneTargets.get(ctx) ?? ctx;
|
|
353
|
-
const
|
|
635
|
+
const itemLog = {
|
|
636
|
+
get items() {
|
|
637
|
+
return target.itemLog.items;
|
|
638
|
+
},
|
|
639
|
+
append(item) {
|
|
640
|
+
target.itemLog.append(item);
|
|
641
|
+
const items = target.itemLog.items;
|
|
642
|
+
lane.appended.add(items[items.length - 1]);
|
|
643
|
+
},
|
|
644
|
+
};
|
|
645
|
+
const view = new Proxy(target, {
|
|
354
646
|
get(base, prop, receiver) {
|
|
355
|
-
if (prop === '
|
|
356
|
-
return
|
|
647
|
+
if (prop === '_node') {
|
|
648
|
+
return lane.node;
|
|
649
|
+
}
|
|
650
|
+
if (prop === '_lane') {
|
|
651
|
+
return lane;
|
|
652
|
+
}
|
|
653
|
+
if (prop === 'itemLog') {
|
|
654
|
+
return itemLog;
|
|
357
655
|
}
|
|
358
656
|
return Reflect.get(base, prop, receiver);
|
|
359
657
|
},
|
|
@@ -361,8 +659,8 @@ function laneOf(ctx, path) {
|
|
|
361
659
|
return Reflect.set(base, prop, value);
|
|
362
660
|
},
|
|
363
661
|
});
|
|
364
|
-
laneTargets.set(
|
|
365
|
-
return
|
|
662
|
+
laneTargets.set(view, target);
|
|
663
|
+
return view;
|
|
366
664
|
}
|
|
367
665
|
/**
|
|
368
666
|
* @internal
|