@noetic-tools/core 5.12.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/in-memory-subprocess/step-completion.d.ts +2 -2
- package/dist/adapters/in-memory-subprocess/step-completion.js +48 -52
- package/dist/adapters/in-memory-subprocess/types.d.ts +3 -6
- package/dist/adapters/in-memory-subprocess-adapter.js +30 -48
- package/dist/adapters/live-handles.d.ts +26 -0
- package/dist/adapters/live-handles.js +50 -0
- package/dist/builders/workflow-hydrator.js +1 -3
- package/dist/harness/agent-harness.d.ts +52 -5
- package/dist/harness/agent-harness.js +175 -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 +5 -1
- package/dist/index.js +2 -0
- package/dist/interpreter/execute-action.js +3 -0
- package/dist/interpreter/execute-control.js +3 -0
- package/dist/interpreter/execute.js +59 -12
- package/dist/interpreter/step-dispatcher.d.ts +2 -2
- package/dist/interpreter/step-dispatcher.js +3 -5
- package/dist/runtime/context-impl.d.ts +93 -4
- package/dist/runtime/context-impl.js +191 -14
- 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 +76 -40
- package/dist/runtime/durable/step-ledger.js +188 -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/types/checkpoint.d.ts +21 -4
- package/dist/types/checkpoint.js +33 -3
- package/dist/util/detached-handle.d.ts +9 -20
- package/dist/util/detached-handle.js +7 -108
- package/package.json +2 -2
|
@@ -17,11 +17,19 @@
|
|
|
17
17
|
* `key:<replayKey>` instead: content-addressed, so it finds its output wherever it
|
|
18
18
|
* runs. A `durable: false` step has no key and is never recorded.
|
|
19
19
|
*
|
|
20
|
+
* An entry also carries what replay must put back: the items the step appended to its
|
|
21
|
+
* context's item log (a checkpoint holds the log as it stood when the run began, so the
|
|
22
|
+
* replayed steps rebuild the rest), and a hash of the step's input, so a step whose input
|
|
23
|
+
* changed since it was recorded runs again instead of replaying an answer to a
|
|
24
|
+
* different question.
|
|
25
|
+
*
|
|
20
26
|
* Sharding bounds the cost of one append but not the total, so retention is bounded on
|
|
21
27
|
* two axes (`StepLedgerRetention`): an oversized entry is dropped rather than written,
|
|
22
|
-
* and
|
|
23
|
-
*
|
|
24
|
-
*
|
|
28
|
+
* and once `maxEntries` are live nothing more is recorded. Entries beneath a parent that
|
|
29
|
+
* recorded are pruned, since a resumed run replays the parent without descending, so the
|
|
30
|
+
* live count tracks the open part of the run rather than its length. Both degradations
|
|
31
|
+
* are safe by construction — a missing entry re-runs its step, which costs work rather
|
|
32
|
+
* than correctness.
|
|
25
33
|
*/
|
|
26
34
|
import type { StorageAdapter } from '@noetic-tools/context';
|
|
27
35
|
import { z } from 'zod';
|
|
@@ -33,6 +41,8 @@ export declare const StepLedgerEntrySchema: z.ZodObject<{
|
|
|
33
41
|
stepId: z.ZodString;
|
|
34
42
|
kind: z.ZodString;
|
|
35
43
|
output: z.ZodUnknown;
|
|
44
|
+
inputHash: z.ZodOptional<z.ZodString>;
|
|
45
|
+
items: z.ZodOptional<z.ZodArray<z.ZodUnknown>>;
|
|
36
46
|
completedAt: z.ZodString;
|
|
37
47
|
}, z.core.$strip>;
|
|
38
48
|
/** @public A completed step recorded for replay. */
|
|
@@ -46,8 +56,9 @@ export type StepLedgerEntry = z.infer<typeof StepLedgerEntrySchema>;
|
|
|
46
56
|
*/
|
|
47
57
|
export interface StepLedgerRetention {
|
|
48
58
|
/**
|
|
49
|
-
* Largest
|
|
50
|
-
*
|
|
59
|
+
* Largest entry that will be recorded, in UTF-8 bytes of the JSON encoding of the
|
|
60
|
+
* step's output plus the items it appended. A step whose entry exceeds this is not
|
|
61
|
+
* recorded at all and re-runs on resume. Keeps one
|
|
51
62
|
* entry inside the per-value limit real storage backends impose (128 KiB on several)
|
|
52
63
|
* instead of failing the write. Pass `Infinity` to record any size.
|
|
53
64
|
*
|
|
@@ -55,9 +66,10 @@ export interface StepLedgerRetention {
|
|
|
55
66
|
*/
|
|
56
67
|
maxEntryBytes?: number;
|
|
57
68
|
/**
|
|
58
|
-
* Most entries
|
|
59
|
-
*
|
|
60
|
-
*
|
|
69
|
+
* Most live entries for one execution. Once the cap is reached nothing more is
|
|
70
|
+
* recorded, so resume replays the head of the run and runs the rest again. Entries
|
|
71
|
+
* beneath a parent that recorded do not count — they are pruned. Pass `Infinity` to
|
|
72
|
+
* record without limit.
|
|
61
73
|
*
|
|
62
74
|
* Defaults to `DEFAULT_STEP_LEDGER_RETENTION.maxEntries` (1000).
|
|
63
75
|
*/
|
|
@@ -77,22 +89,30 @@ export declare const DEFAULT_STEP_LEDGER_RETENTION: {
|
|
|
77
89
|
* @throws NoeticConfigError `code: 'STEP_LEDGER_RETENTION_INVALID'`
|
|
78
90
|
*/
|
|
79
91
|
export declare function resolveStepLedgerRetention(retention?: StepLedgerRetention): Required<StepLedgerRetention>;
|
|
92
|
+
/**
|
|
93
|
+
* @internal
|
|
94
|
+
* A short, stable hash of a step input's JSON encoding, or undefined when the input
|
|
95
|
+
* cannot be encoded (a cycle, a `BigInt`). A replayed output becomes the next step's
|
|
96
|
+
* input after a JSON round trip, and `JSON.stringify` gives the same text for a value
|
|
97
|
+
* and its round-tripped copy, so a resumed run hashes the same inputs the first run did.
|
|
98
|
+
*/
|
|
99
|
+
export declare function hashStepInput(input: unknown): string | undefined;
|
|
80
100
|
/** Storage key prefix owning every ledger entry for one execution. */
|
|
81
101
|
export declare function stepLedgerPrefix(executionId: string): string;
|
|
82
102
|
/**
|
|
83
103
|
* @public
|
|
84
104
|
* Everything one execution's ledger recovered from storage: the entries available for
|
|
85
105
|
* replay plus the sequence bookkeeping a further append needs. `nextSeq` cannot be
|
|
86
|
-
* derived from `entries.size` —
|
|
87
|
-
*
|
|
106
|
+
* derived from `entries.size` — pruning leaves gaps, and reusing a live sequence number
|
|
107
|
+
* would overwrite an entry the resumed run still has to replay.
|
|
88
108
|
*/
|
|
89
109
|
export interface StepLedgerWindow {
|
|
90
110
|
/** Recovered entries, keyed by execution path. Corrupt rows are skipped. */
|
|
91
111
|
entries: Map<string, StepLedgerEntry>;
|
|
112
|
+
/** Sequence number holding each recovered path, so pruning can delete it. */
|
|
113
|
+
seqs: Map<string, number>;
|
|
92
114
|
/** Sequence number the next append takes: highest recorded, plus one. */
|
|
93
115
|
nextSeq: number;
|
|
94
|
-
/** Lowest sequence number still in storage — where eviction resumes. */
|
|
95
|
-
oldestSeq: number;
|
|
96
116
|
}
|
|
97
117
|
/**
|
|
98
118
|
* @public
|
|
@@ -124,8 +144,16 @@ export interface StepLedgerStats {
|
|
|
124
144
|
droppedOversize: number;
|
|
125
145
|
/** Entries not written because their output does not survive JSON encoding. */
|
|
126
146
|
droppedUnserialisable: number;
|
|
127
|
-
/** Entries
|
|
128
|
-
|
|
147
|
+
/** Entries not written because `maxEntries` were already live. */
|
|
148
|
+
droppedFull: number;
|
|
149
|
+
/** Entries deleted because a parent above them recorded. */
|
|
150
|
+
pruned: number;
|
|
151
|
+
}
|
|
152
|
+
/** What a recorded step leaves for replay: the step's identity and input hash. */
|
|
153
|
+
export interface StepLedgerMatch {
|
|
154
|
+
id: string;
|
|
155
|
+
kind: string;
|
|
156
|
+
inputHash: string | undefined;
|
|
129
157
|
}
|
|
130
158
|
/**
|
|
131
159
|
* @public
|
|
@@ -134,16 +162,16 @@ export interface StepLedgerStats {
|
|
|
134
162
|
*
|
|
135
163
|
* Shared by reference across inParallel/spawn children so one execution has one ledger —
|
|
136
164
|
* path keys are globally unique across the step tree, so a flat map is correct. That
|
|
137
|
-
* sharing is also why retention lives here rather than in the store: the
|
|
138
|
-
*
|
|
165
|
+
* sharing is also why retention lives here rather than in the store: the live set that
|
|
166
|
+
* the entry cap counts belongs to the execution, not to the harness.
|
|
139
167
|
*/
|
|
140
168
|
export declare class StepLedger {
|
|
141
|
-
/** Recovered entries, by path.
|
|
169
|
+
/** Recovered entries, by path. */
|
|
142
170
|
private readonly replayable;
|
|
171
|
+
/** Sequence number holding each live entry, recovered or recorded, by path. */
|
|
172
|
+
private readonly live;
|
|
143
173
|
/** Sequence number the next recorded entry takes. */
|
|
144
174
|
private seq;
|
|
145
|
-
/** Lowest sequence number still in storage — advances as eviction proceeds. */
|
|
146
|
-
private oldestSeq;
|
|
147
175
|
private readonly store?;
|
|
148
176
|
private readonly executionId;
|
|
149
177
|
private readonly retention;
|
|
@@ -155,38 +183,46 @@ export declare class StepLedger {
|
|
|
155
183
|
recovered?: StepLedgerWindow;
|
|
156
184
|
retention?: StepLedgerRetention;
|
|
157
185
|
});
|
|
186
|
+
/**
|
|
187
|
+
* Take what storage now holds for this execution in place of what this ledger has
|
|
188
|
+
* in memory — for a run retried in place, which replays what was stored.
|
|
189
|
+
*/
|
|
190
|
+
adopt(recovered: StepLedgerWindow): void;
|
|
158
191
|
/** True when this execution has nothing recovered to replay (the common, fresh case). */
|
|
159
192
|
get isEmpty(): boolean;
|
|
160
193
|
/** What retention has done so far. A snapshot — safe to hold. */
|
|
161
194
|
get stats(): StepLedgerStats;
|
|
162
195
|
/**
|
|
163
|
-
* The recorded
|
|
164
|
-
* A divergence
|
|
165
|
-
* runs fresh. At a positional path it also discards every entry
|
|
166
|
-
* the subtree's
|
|
167
|
-
* entry discards only itself:
|
|
168
|
-
*
|
|
196
|
+
* The recorded entry for `path`, when a previous run completed the same step there on
|
|
197
|
+
* the same input. A divergence — a different step id, kind, or input — returns
|
|
198
|
+
* undefined so the step runs fresh. At a positional path it also discards every entry
|
|
199
|
+
* recorded beneath it: the subtree's outputs belong to a run that no longer exists. A
|
|
200
|
+
* `key:` entry discards only itself: nothing is nested under a content key.
|
|
201
|
+
*
|
|
202
|
+
* An entry recorded without an input hash matches on identity alone.
|
|
203
|
+
*/
|
|
204
|
+
take(path: string, step: StepLedgerMatch): StepLedgerEntry | undefined;
|
|
205
|
+
/**
|
|
206
|
+
* Record a completed step, then prune what it makes dead: a resumed run replays this
|
|
207
|
+
* entry without descending, so nothing recorded beneath it will be read again.
|
|
208
|
+
* Best-effort: a failed write costs resumability, not the run.
|
|
169
209
|
*/
|
|
170
|
-
take(path: string, step: {
|
|
171
|
-
id: string;
|
|
172
|
-
kind: string;
|
|
173
|
-
}): StepLedgerEntry | undefined;
|
|
174
|
-
/** Drop `path` and everything recorded below it. */
|
|
175
|
-
private discardSubtree;
|
|
176
|
-
/** Record a completed step. Best-effort: a failed write costs resumability, not the run. */
|
|
177
210
|
record(entry: StepLedgerEntry): Promise<void>;
|
|
178
211
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
212
|
+
* Drop every positional entry. A host calls this once a run has finished and the
|
|
213
|
+
* checkpoint holds its result: every later resume starts past them, so they are dead.
|
|
214
|
+
* `key:` entries stay — they are keyed by content, and a later run may ask the same.
|
|
182
215
|
*/
|
|
183
|
-
|
|
216
|
+
dropPaths(): Promise<void>;
|
|
217
|
+
private prune;
|
|
218
|
+
private deleteSeq;
|
|
184
219
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* correct without a `list()` per append.
|
|
220
|
+
* Whether this entry is small enough, encodable, and within the live cap. An entry
|
|
221
|
+
* that fails any test is not recorded, so its step re-runs on resume.
|
|
188
222
|
*/
|
|
189
|
-
private
|
|
223
|
+
private retainable;
|
|
224
|
+
/** Live entries once recording at `path` has pruned what lies beneath it. */
|
|
225
|
+
private liveAfter;
|
|
190
226
|
/** One warning per reason per execution — a long run would otherwise flood the log. */
|
|
191
227
|
private warnOnFirstDrop;
|
|
192
228
|
}
|
|
@@ -17,11 +17,19 @@
|
|
|
17
17
|
* `key:<replayKey>` instead: content-addressed, so it finds its output wherever it
|
|
18
18
|
* runs. A `durable: false` step has no key and is never recorded.
|
|
19
19
|
*
|
|
20
|
+
* An entry also carries what replay must put back: the items the step appended to its
|
|
21
|
+
* context's item log (a checkpoint holds the log as it stood when the run began, so the
|
|
22
|
+
* replayed steps rebuild the rest), and a hash of the step's input, so a step whose input
|
|
23
|
+
* changed since it was recorded runs again instead of replaying an answer to a
|
|
24
|
+
* different question.
|
|
25
|
+
*
|
|
20
26
|
* Sharding bounds the cost of one append but not the total, so retention is bounded on
|
|
21
27
|
* two axes (`StepLedgerRetention`): an oversized entry is dropped rather than written,
|
|
22
|
-
* and
|
|
23
|
-
*
|
|
24
|
-
*
|
|
28
|
+
* and once `maxEntries` are live nothing more is recorded. Entries beneath a parent that
|
|
29
|
+
* recorded are pruned, since a resumed run replays the parent without descending, so the
|
|
30
|
+
* live count tracks the open part of the run rather than its length. Both degradations
|
|
31
|
+
* are safe by construction — a missing entry re-runs its step, which costs work rather
|
|
32
|
+
* than correctness.
|
|
25
33
|
*/
|
|
26
34
|
import { storageGetMany } from '@noetic-tools/context';
|
|
27
35
|
import { NoeticConfigError } from '@noetic-tools/types';
|
|
@@ -40,6 +48,13 @@ export const StepLedgerEntrySchema = z.object({
|
|
|
40
48
|
kind: z.string(),
|
|
41
49
|
/** Replayed verbatim in place of re-running the step. */
|
|
42
50
|
output: z.unknown(),
|
|
51
|
+
/**
|
|
52
|
+
* Hash of the step's input when it ran (`hashStepInput`). Replay requires the new
|
|
53
|
+
* dispatch's input to hash the same. Absent when the input could not be encoded.
|
|
54
|
+
*/
|
|
55
|
+
inputHash: z.string().optional(),
|
|
56
|
+
/** Items the step appended to its context's item log, re-appended on replay. */
|
|
57
|
+
items: z.array(z.unknown()).optional(),
|
|
43
58
|
completedAt: z.string(),
|
|
44
59
|
});
|
|
45
60
|
/** @public Retention applied when a harness does not configure `stepLedgerRetention`. */
|
|
@@ -90,6 +105,41 @@ function fitsByteCap(json, cap) {
|
|
|
90
105
|
return new TextEncoder().encode(json).length <= cap;
|
|
91
106
|
}
|
|
92
107
|
//#endregion
|
|
108
|
+
//#region Input hash
|
|
109
|
+
/**
|
|
110
|
+
* @internal
|
|
111
|
+
* A short, stable hash of a step input's JSON encoding, or undefined when the input
|
|
112
|
+
* cannot be encoded (a cycle, a `BigInt`). A replayed output becomes the next step's
|
|
113
|
+
* input after a JSON round trip, and `JSON.stringify` gives the same text for a value
|
|
114
|
+
* and its round-tripped copy, so a resumed run hashes the same inputs the first run did.
|
|
115
|
+
*/
|
|
116
|
+
export function hashStepInput(input) {
|
|
117
|
+
let json;
|
|
118
|
+
try {
|
|
119
|
+
json = JSON.stringify(input);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return cyrb53(json ?? 'undefined');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 53-bit string hash (cyrb53, public domain: https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js).
|
|
128
|
+
* Not cryptographic — it tells "same input" from "changed input", nothing more.
|
|
129
|
+
*/
|
|
130
|
+
function cyrb53(text) {
|
|
131
|
+
let h1 = 0xdeadbeef;
|
|
132
|
+
let h2 = 0x41c6ce57;
|
|
133
|
+
for (let i = 0; i < text.length; i++) {
|
|
134
|
+
const ch = text.charCodeAt(i);
|
|
135
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
136
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
137
|
+
}
|
|
138
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
139
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
140
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
93
143
|
//#region Store
|
|
94
144
|
/** Storage key prefix owning every ledger entry for one execution. */
|
|
95
145
|
export function stepLedgerPrefix(executionId) {
|
|
@@ -123,20 +173,21 @@ export function createStepLedgerStore(opts) {
|
|
|
123
173
|
* moment a network-backed adapter can least afford an N+1. */
|
|
124
174
|
const raws = await storageGetMany(storage, keys);
|
|
125
175
|
const entries = new Map();
|
|
126
|
-
/*
|
|
127
|
-
* parse still occupies its key, so it must not be handed back out
|
|
176
|
+
/* The next sequence number comes from the KEYS, not the parsed rows: a row that
|
|
177
|
+
* fails to parse still occupies its key, so it must not be handed back out. */
|
|
128
178
|
let highestSeq = -1;
|
|
129
|
-
|
|
130
|
-
// Walk
|
|
131
|
-
//
|
|
132
|
-
for (const key of
|
|
179
|
+
const seqs = new Map();
|
|
180
|
+
// Walk the keys sorted: `seqKey`'s zero-padding makes key order dispatch order,
|
|
181
|
+
// and a later entry at a path must win over an earlier one.
|
|
182
|
+
for (const key of [
|
|
183
|
+
...keys,
|
|
184
|
+
].sort()) {
|
|
133
185
|
const seq = seqFromKey(executionId, key);
|
|
134
186
|
if (seq !== null) {
|
|
135
187
|
highestSeq = Math.max(highestSeq, seq);
|
|
136
|
-
oldestSeq = oldestSeq === -1 ? seq : Math.min(oldestSeq, seq);
|
|
137
188
|
}
|
|
138
189
|
const raw = raws.get(key);
|
|
139
|
-
if (raw === undefined) {
|
|
190
|
+
if (raw === undefined || seq === null) {
|
|
140
191
|
continue;
|
|
141
192
|
}
|
|
142
193
|
const parsed = StepLedgerEntrySchema.safeParse(raw);
|
|
@@ -147,11 +198,12 @@ export function createStepLedgerStore(opts) {
|
|
|
147
198
|
continue;
|
|
148
199
|
}
|
|
149
200
|
entries.set(parsed.data.path, parsed.data);
|
|
201
|
+
seqs.set(parsed.data.path, seq);
|
|
150
202
|
}
|
|
151
203
|
return {
|
|
152
204
|
entries,
|
|
205
|
+
seqs,
|
|
153
206
|
nextSeq: highestSeq + 1,
|
|
154
|
-
oldestSeq: oldestSeq === -1 ? 0 : oldestSeq,
|
|
155
207
|
};
|
|
156
208
|
},
|
|
157
209
|
clear: async (executionId) => {
|
|
@@ -169,16 +221,16 @@ export function createStepLedgerStore(opts) {
|
|
|
169
221
|
*
|
|
170
222
|
* Shared by reference across inParallel/spawn children so one execution has one ledger —
|
|
171
223
|
* path keys are globally unique across the step tree, so a flat map is correct. That
|
|
172
|
-
* sharing is also why retention lives here rather than in the store: the
|
|
173
|
-
*
|
|
224
|
+
* sharing is also why retention lives here rather than in the store: the live set that
|
|
225
|
+
* the entry cap counts belongs to the execution, not to the harness.
|
|
174
226
|
*/
|
|
175
227
|
export class StepLedger {
|
|
176
|
-
/** Recovered entries, by path.
|
|
177
|
-
replayable;
|
|
228
|
+
/** Recovered entries, by path. */
|
|
229
|
+
replayable = new Map();
|
|
230
|
+
/** Sequence number holding each live entry, recovered or recorded, by path. */
|
|
231
|
+
live;
|
|
178
232
|
/** Sequence number the next recorded entry takes. */
|
|
179
233
|
seq;
|
|
180
|
-
/** Lowest sequence number still in storage — advances as eviction proceeds. */
|
|
181
|
-
oldestSeq;
|
|
182
234
|
store;
|
|
183
235
|
executionId;
|
|
184
236
|
retention;
|
|
@@ -186,15 +238,33 @@ export class StepLedger {
|
|
|
186
238
|
recorded: 0,
|
|
187
239
|
droppedOversize: 0,
|
|
188
240
|
droppedUnserialisable: 0,
|
|
189
|
-
|
|
241
|
+
droppedFull: 0,
|
|
242
|
+
pruned: 0,
|
|
190
243
|
};
|
|
191
244
|
constructor(opts) {
|
|
192
245
|
this.executionId = opts.executionId;
|
|
193
246
|
this.store = opts.store;
|
|
194
247
|
this.retention = resolveStepLedgerRetention(opts.retention);
|
|
195
|
-
this.
|
|
196
|
-
this.seq =
|
|
197
|
-
|
|
248
|
+
this.live = new Map();
|
|
249
|
+
this.seq = 0;
|
|
250
|
+
if (opts.recovered) {
|
|
251
|
+
this.adopt(opts.recovered);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Take what storage now holds for this execution in place of what this ledger has
|
|
256
|
+
* in memory — for a run retried in place, which replays what was stored.
|
|
257
|
+
*/
|
|
258
|
+
adopt(recovered) {
|
|
259
|
+
this.replayable.clear();
|
|
260
|
+
for (const [path, entry] of recovered.entries) {
|
|
261
|
+
this.replayable.set(path, entry);
|
|
262
|
+
}
|
|
263
|
+
this.live.clear();
|
|
264
|
+
for (const [path, seq] of recovered.seqs) {
|
|
265
|
+
this.live.set(path, seq);
|
|
266
|
+
}
|
|
267
|
+
this.seq = Math.max(this.seq, recovered.nextSeq);
|
|
198
268
|
}
|
|
199
269
|
/** True when this execution has nothing recovered to replay (the common, fresh case). */
|
|
200
270
|
get isEmpty() {
|
|
@@ -207,39 +277,40 @@ export class StepLedger {
|
|
|
207
277
|
};
|
|
208
278
|
}
|
|
209
279
|
/**
|
|
210
|
-
* The recorded
|
|
211
|
-
* A divergence
|
|
212
|
-
* runs fresh. At a positional path it also discards every entry
|
|
213
|
-
* the subtree's
|
|
214
|
-
* entry discards only itself:
|
|
215
|
-
*
|
|
280
|
+
* The recorded entry for `path`, when a previous run completed the same step there on
|
|
281
|
+
* the same input. A divergence — a different step id, kind, or input — returns
|
|
282
|
+
* undefined so the step runs fresh. At a positional path it also discards every entry
|
|
283
|
+
* recorded beneath it: the subtree's outputs belong to a run that no longer exists. A
|
|
284
|
+
* `key:` entry discards only itself: nothing is nested under a content key.
|
|
285
|
+
*
|
|
286
|
+
* An entry recorded without an input hash matches on identity alone.
|
|
216
287
|
*/
|
|
217
288
|
take(path, step) {
|
|
218
289
|
const entry = this.replayable.get(path);
|
|
219
290
|
if (!entry) {
|
|
220
291
|
return undefined;
|
|
221
292
|
}
|
|
222
|
-
if (entry
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
this.discardSubtree(path);
|
|
293
|
+
if (matches(entry, step)) {
|
|
294
|
+
return entry;
|
|
295
|
+
}
|
|
296
|
+
if (path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
297
|
+
this.replayable.delete(path);
|
|
228
298
|
return undefined;
|
|
229
299
|
}
|
|
230
|
-
return entry;
|
|
231
|
-
}
|
|
232
|
-
/** Drop `path` and everything recorded below it. */
|
|
233
|
-
discardSubtree(path) {
|
|
234
300
|
for (const key of [
|
|
235
301
|
...this.replayable.keys(),
|
|
236
302
|
]) {
|
|
237
|
-
if (key === path || key
|
|
303
|
+
if (key === path || isBeneath(key, path)) {
|
|
238
304
|
this.replayable.delete(key);
|
|
239
305
|
}
|
|
240
306
|
}
|
|
307
|
+
return undefined;
|
|
241
308
|
}
|
|
242
|
-
/**
|
|
309
|
+
/**
|
|
310
|
+
* Record a completed step, then prune what it makes dead: a resumed run replays this
|
|
311
|
+
* entry without descending, so nothing recorded beneath it will be read again.
|
|
312
|
+
* Best-effort: a failed write costs resumability, not the run.
|
|
313
|
+
*/
|
|
243
314
|
async record(entry) {
|
|
244
315
|
if (!this.store) {
|
|
245
316
|
return;
|
|
@@ -249,9 +320,7 @@ export class StepLedger {
|
|
|
249
320
|
}
|
|
250
321
|
/* Reserve the sequence number BEFORE awaiting: concurrent inParallel legs record through
|
|
251
322
|
* the one shared ledger, and two of them reading the same counter would write the
|
|
252
|
-
* same key, silently overwriting a sibling's entry.
|
|
253
|
-
* gap, which only makes the window below a slight over-estimate of the live count —
|
|
254
|
-
* eviction runs marginally early, never late. */
|
|
323
|
+
* same key, silently overwriting a sibling's entry. */
|
|
255
324
|
const seq = this.seq;
|
|
256
325
|
this.seq = seq + 1;
|
|
257
326
|
try {
|
|
@@ -261,56 +330,92 @@ export class StepLedger {
|
|
|
261
330
|
console.warn(`StepLedger: failed to record "${entry.path}":`, error instanceof Error ? error.message : String(error));
|
|
262
331
|
return;
|
|
263
332
|
}
|
|
333
|
+
const replaced = this.live.get(entry.path);
|
|
334
|
+
this.live.set(entry.path, seq);
|
|
264
335
|
this.counts.recorded += 1;
|
|
265
|
-
|
|
336
|
+
if (replaced !== undefined) {
|
|
337
|
+
await this.deleteSeq(replaced);
|
|
338
|
+
}
|
|
339
|
+
if (!entry.path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
340
|
+
await this.prune((path) => isBeneath(path, entry.path));
|
|
341
|
+
}
|
|
266
342
|
}
|
|
267
343
|
/**
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
344
|
+
* Drop every positional entry. A host calls this once a run has finished and the
|
|
345
|
+
* checkpoint holds its result: every later resume starts past them, so they are dead.
|
|
346
|
+
* `key:` entries stay — they are keyed by content, and a later run may ask the same.
|
|
347
|
+
*/
|
|
348
|
+
async dropPaths() {
|
|
349
|
+
await this.prune((path) => !path.startsWith(REPLAY_KEY_PREFIX));
|
|
350
|
+
for (const path of [
|
|
351
|
+
...this.replayable.keys(),
|
|
352
|
+
]) {
|
|
353
|
+
if (!path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
354
|
+
this.replayable.delete(path);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async prune(dead) {
|
|
359
|
+
for (const [path, seq] of [
|
|
360
|
+
...this.live,
|
|
361
|
+
]) {
|
|
362
|
+
if (!dead(path)) {
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
this.live.delete(path);
|
|
366
|
+
this.replayable.delete(path);
|
|
367
|
+
this.counts.pruned += 1;
|
|
368
|
+
await this.deleteSeq(seq);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async deleteSeq(seq) {
|
|
372
|
+
try {
|
|
373
|
+
await this.store?.delete(this.executionId, seq);
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
console.warn(`StepLedger: failed to delete entry ${seq} of "${this.executionId}":`, error instanceof Error ? error.message : String(error));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Whether this entry is small enough, encodable, and within the live cap. An entry
|
|
381
|
+
* that fails any test is not recorded, so its step re-runs on resume.
|
|
271
382
|
*/
|
|
272
383
|
retainable(entry) {
|
|
273
|
-
|
|
384
|
+
if (!this.live.has(entry.path) && this.liveAfter(entry.path) >= this.retention.maxEntries) {
|
|
385
|
+
this.counts.droppedFull += 1;
|
|
386
|
+
this.warnOnFirstDrop(this.counts.droppedFull, `StepLedger: not recording "${entry.path}" — ${this.retention.maxEntries} entries (maxEntries) are already live.`);
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
let bytes;
|
|
274
390
|
try {
|
|
275
|
-
|
|
391
|
+
// `undefined` stringifies to nothing; there is no size to bound.
|
|
392
|
+
bytes =
|
|
393
|
+
(JSON.stringify(entry.output) ?? '') + (entry.items ? JSON.stringify(entry.items) : '');
|
|
276
394
|
}
|
|
277
395
|
catch {
|
|
278
396
|
this.counts.droppedUnserialisable += 1;
|
|
279
397
|
this.warnOnFirstDrop(this.counts.droppedUnserialisable, `StepLedger: not recording "${entry.path}" — its output does not survive JSON encoding.`);
|
|
280
398
|
return false;
|
|
281
399
|
}
|
|
282
|
-
|
|
283
|
-
if (json !== undefined && !fitsByteCap(json, this.retention.maxEntryBytes)) {
|
|
400
|
+
if (!fitsByteCap(bytes, this.retention.maxEntryBytes)) {
|
|
284
401
|
this.counts.droppedOversize += 1;
|
|
285
|
-
this.warnOnFirstDrop(this.counts.droppedOversize, `StepLedger: not recording "${entry.path}" — output
|
|
402
|
+
this.warnOnFirstDrop(this.counts.droppedOversize, `StepLedger: not recording "${entry.path}" — output and items exceed maxEntryBytes (${this.retention.maxEntryBytes}).`);
|
|
286
403
|
return false;
|
|
287
404
|
}
|
|
288
405
|
return true;
|
|
289
406
|
}
|
|
290
|
-
/**
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
*/
|
|
295
|
-
async evictOverflow() {
|
|
296
|
-
const store = this.store;
|
|
297
|
-
if (!store) {
|
|
298
|
-
return;
|
|
407
|
+
/** Live entries once recording at `path` has pruned what lies beneath it. */
|
|
408
|
+
liveAfter(path) {
|
|
409
|
+
if (this.live.size < this.retention.maxEntries || path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
410
|
+
return this.live.size;
|
|
299
411
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
/* Leave the cursor where it is and stop: the next append retries this victim
|
|
307
|
-
* rather than spinning on a store that is currently refusing deletes. */
|
|
308
|
-
console.warn(`StepLedger: failed to evict entry ${victim} of "${this.executionId}":`, error instanceof Error ? error.message : String(error));
|
|
309
|
-
return;
|
|
412
|
+
let beneath = 0;
|
|
413
|
+
for (const key of this.live.keys()) {
|
|
414
|
+
if (isBeneath(key, path)) {
|
|
415
|
+
beneath += 1;
|
|
310
416
|
}
|
|
311
|
-
this.oldestSeq = victim + 1;
|
|
312
|
-
this.counts.evicted += 1;
|
|
313
417
|
}
|
|
418
|
+
return this.live.size - beneath;
|
|
314
419
|
}
|
|
315
420
|
/** One warning per reason per execution — a long run would otherwise flood the log. */
|
|
316
421
|
warnOnFirstDrop(count, message) {
|
|
@@ -320,4 +425,14 @@ export class StepLedger {
|
|
|
320
425
|
console.warn(`${message} The step will re-run on resume; further drops are silent.`);
|
|
321
426
|
}
|
|
322
427
|
}
|
|
428
|
+
function matches(entry, step) {
|
|
429
|
+
if (entry.stepId !== step.id || entry.kind !== step.kind) {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
return entry.inputHash === undefined || entry.inputHash === step.inputHash;
|
|
433
|
+
}
|
|
434
|
+
/** Whether `path` lies in the subtree under `parent` — a lane below it or a child context's prefix. */
|
|
435
|
+
function isBeneath(path, parent) {
|
|
436
|
+
return path.startsWith(`${parent}/`) || path.startsWith(`${parent}[`);
|
|
437
|
+
}
|
|
323
438
|
//#endregion
|
|
@@ -5,5 +5,11 @@ export declare class ItemLogImpl implements ItemLog {
|
|
|
5
5
|
private _frozenCache;
|
|
6
6
|
constructor(itemSchemas?: ItemSchemaRegistry);
|
|
7
7
|
get items(): ReadonlyArray<Item>;
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
* Swap the whole log for `items`. Only for a context rebuilding itself from storage
|
|
11
|
+
* before a run starts; nothing reads a log while it is replaced.
|
|
12
|
+
*/
|
|
13
|
+
replace(items: ReadonlyArray<Item>): void;
|
|
8
14
|
append(item: Item): void;
|
|
9
15
|
}
|
|
@@ -18,6 +18,18 @@ export class ItemLogImpl {
|
|
|
18
18
|
}
|
|
19
19
|
return frozenItems;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
* Swap the whole log for `items`. Only for a context rebuilding itself from storage
|
|
24
|
+
* before a run starts; nothing reads a log while it is replaced.
|
|
25
|
+
*/
|
|
26
|
+
replace(items) {
|
|
27
|
+
this._items.length = 0;
|
|
28
|
+
for (const item of items) {
|
|
29
|
+
this._items.push(this.itemSchemas.parse(item));
|
|
30
|
+
}
|
|
31
|
+
this._frozenCache = null;
|
|
32
|
+
}
|
|
21
33
|
append(item) {
|
|
22
34
|
this._items.push(this.itemSchemas.parse(item));
|
|
23
35
|
this._frozenCache = null;
|