@noetic-tools/core 5.12.0 → 5.13.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/index.d.ts +5 -1
- package/dist/index.js +2 -0
- package/dist/interpreter/execute.js +1 -3
- package/dist/interpreter/step-dispatcher.d.ts +2 -2
- package/dist/interpreter/step-dispatcher.js +3 -5
- package/dist/util/detached-handle.d.ts +9 -20
- package/dist/util/detached-handle.js +7 -108
- package/package.json +2 -2
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { CompleteProcessRunParams, CompleteStepRunParams } from './types.js';
|
|
2
|
-
export declare function completeProcessRun({ request, handle, run,
|
|
3
|
-
export declare function completeStepRun({ request, handle, stepRunner,
|
|
2
|
+
export declare function completeProcessRun({ request, handle, run, live, clearIfDurable, }: CompleteProcessRunParams): Promise<void>;
|
|
3
|
+
export declare function completeStepRun({ request, handle, stepRunner, live, clearIfDurable, }: CompleteStepRunParams): Promise<void>;
|
|
@@ -1,42 +1,26 @@
|
|
|
1
1
|
import { nowIso, serializeError } from './metadata.js';
|
|
2
2
|
//#region Completion handlers
|
|
3
|
-
export async function completeProcessRun({ request, handle, run,
|
|
3
|
+
export async function completeProcessRun({ request, handle, run, live, clearIfDurable, }) {
|
|
4
4
|
try {
|
|
5
5
|
await run?.(request, handle);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
active.delete(handle.id);
|
|
11
|
-
await save({
|
|
12
|
-
...latest,
|
|
6
|
+
await finish({
|
|
7
|
+
live,
|
|
8
|
+
clearIfDurable,
|
|
9
|
+
}, handle.id, {
|
|
13
10
|
status: 'completed',
|
|
14
|
-
updatedAt: nowIso(),
|
|
15
11
|
});
|
|
16
|
-
// Clear the durable handle manifest once the run has terminated so
|
|
17
|
-
// `listLive()` stops returning this handle. Without this call the
|
|
18
|
-
// manifest would persist until `stop()` was called, making every
|
|
19
|
-
// completed step look live from the adapter's persistence layer.
|
|
20
|
-
await clearIfDurable(handle.id);
|
|
21
12
|
}
|
|
22
13
|
catch (err) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
await finish({
|
|
15
|
+
live,
|
|
16
|
+
clearIfDurable,
|
|
17
|
+
}, handle.id, {
|
|
27
18
|
status: 'failed',
|
|
28
|
-
|
|
29
|
-
metadata: {
|
|
30
|
-
...(latest.metadata ?? {}),
|
|
31
|
-
error: serializeError(err),
|
|
32
|
-
},
|
|
19
|
+
error: serializeError(err),
|
|
33
20
|
});
|
|
34
|
-
// Same rationale as the success path above — clear the durable
|
|
35
|
-
// manifest so `listLive()` doesn't resurrect this handle on reboot.
|
|
36
|
-
await clearIfDurable(handle.id);
|
|
37
21
|
}
|
|
38
22
|
}
|
|
39
|
-
export async function completeStepRun({ request, handle, stepRunner,
|
|
23
|
+
export async function completeStepRun({ request, handle, stepRunner, live, clearIfDurable, }) {
|
|
40
24
|
try {
|
|
41
25
|
const executor = request._localExecutor;
|
|
42
26
|
let result;
|
|
@@ -50,37 +34,49 @@ export async function completeStepRun({ request, handle, stepRunner, handles, ac
|
|
|
50
34
|
throw new Error(`In-memory subprocess adapter has no executor for step "${request.stepId}". ` +
|
|
51
35
|
'Provide a stepRunner option or dispatch via AgentHarness.');
|
|
52
36
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
active.delete(handle.id);
|
|
58
|
-
await save({
|
|
59
|
-
...latest,
|
|
37
|
+
await finish({
|
|
38
|
+
live,
|
|
39
|
+
clearIfDurable,
|
|
40
|
+
}, handle.id, {
|
|
60
41
|
status: 'completed',
|
|
61
|
-
|
|
62
|
-
metadata: {
|
|
63
|
-
...(latest.metadata ?? {}),
|
|
64
|
-
result,
|
|
65
|
-
},
|
|
42
|
+
result,
|
|
66
43
|
});
|
|
67
|
-
// Clear the durable step manifest once the run has terminated —
|
|
68
|
-
// see `completeProcessRun` above for the rationale.
|
|
69
|
-
await clearIfDurable(handle.id);
|
|
70
44
|
}
|
|
71
45
|
catch (err) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
46
|
+
await finish({
|
|
47
|
+
live,
|
|
48
|
+
clearIfDurable,
|
|
49
|
+
}, handle.id, {
|
|
76
50
|
status: 'failed',
|
|
77
|
-
|
|
78
|
-
metadata: {
|
|
79
|
-
...(latest.metadata ?? {}),
|
|
80
|
-
error: serializeError(err),
|
|
81
|
-
},
|
|
51
|
+
error: serializeError(err),
|
|
82
52
|
});
|
|
83
|
-
await clearIfDurable(handle.id);
|
|
84
53
|
}
|
|
85
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Settles a run that ended on its own. The caller hears first; the durable
|
|
57
|
+
* manifest clears after, so a slow storage write never delays the result.
|
|
58
|
+
* A handle already stopped has settled, so its late outcome is dropped.
|
|
59
|
+
*/
|
|
60
|
+
async function finish({ live, clearIfDurable }, handleId, outcome) {
|
|
61
|
+
const latest = live.get(handleId);
|
|
62
|
+
if (!latest) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const metadata = {
|
|
66
|
+
...(latest.metadata ?? {}),
|
|
67
|
+
};
|
|
68
|
+
if (outcome.result !== undefined) {
|
|
69
|
+
metadata.result = outcome.result;
|
|
70
|
+
}
|
|
71
|
+
if (outcome.error !== undefined) {
|
|
72
|
+
metadata.error = outcome.error;
|
|
73
|
+
}
|
|
74
|
+
live.settle({
|
|
75
|
+
...latest,
|
|
76
|
+
status: outcome.status,
|
|
77
|
+
updatedAt: nowIso(),
|
|
78
|
+
metadata,
|
|
79
|
+
});
|
|
80
|
+
await clearIfDurable(handleId);
|
|
81
|
+
}
|
|
86
82
|
//#endregion
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { StorageAdapter } from '@noetic-tools/context';
|
|
2
2
|
import type { ProcessSubprocessRequest, StepSubprocessRequest, SubprocessHandle, SubprocessHandleMetadata, SubprocessRequest } from '@noetic-tools/types';
|
|
3
|
+
import type { LiveHandles } from '../live-handles.js';
|
|
3
4
|
/**
|
|
4
5
|
* @public
|
|
5
6
|
* Options for `createInMemorySubprocessAdapter`. All hooks are optional;
|
|
@@ -48,9 +49,7 @@ export interface CompleteProcessRunParams {
|
|
|
48
49
|
request: ProcessSubprocessRequest;
|
|
49
50
|
handle: SubprocessHandle;
|
|
50
51
|
run: CreateInMemorySubprocessAdapterOptions['run'];
|
|
51
|
-
|
|
52
|
-
active: Set<string>;
|
|
53
|
-
save: (handle: SubprocessHandle) => Promise<SubprocessHandle>;
|
|
52
|
+
live: LiveHandles;
|
|
54
53
|
/** Invoked on terminal transitions so the durable handle manifest
|
|
55
54
|
* does not linger after the step is done — otherwise `listLive()`
|
|
56
55
|
* would return phantom handles for every step ever completed
|
|
@@ -61,9 +60,7 @@ export interface CompleteStepRunParams {
|
|
|
61
60
|
request: StepSubprocessRequest;
|
|
62
61
|
handle: SubprocessHandle;
|
|
63
62
|
stepRunner: CreateInMemorySubprocessAdapterOptions['stepRunner'];
|
|
64
|
-
|
|
65
|
-
active: Set<string>;
|
|
66
|
-
save: (handle: SubprocessHandle) => Promise<SubprocessHandle>;
|
|
63
|
+
live: LiveHandles;
|
|
67
64
|
/** See `CompleteProcessRunParams.clearIfDurable`. */
|
|
68
65
|
clearIfDurable: (handleId: string) => Promise<void>;
|
|
69
66
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { clearManifest, listManifests, loadManifest, persistStepManifest, } from './in-memory-subprocess/manifest-persistence.js';
|
|
2
2
|
import { buildProcessMetadata, buildStepMetadata, isProcessRequest, isStepRequest, nowIso, } from './in-memory-subprocess/metadata.js';
|
|
3
3
|
import { completeProcessRun, completeStepRun } from './in-memory-subprocess/step-completion.js';
|
|
4
|
+
import { createLiveHandles } from './live-handles.js';
|
|
4
5
|
//#region Factory
|
|
5
6
|
/**
|
|
6
7
|
* @public
|
|
@@ -12,13 +13,8 @@ import { completeProcessRun, completeStepRun } from './in-memory-subprocess/step
|
|
|
12
13
|
* a live process (there is none to bind to for in-memory).
|
|
13
14
|
*/
|
|
14
15
|
export function createInMemorySubprocessAdapter(options = {}) {
|
|
15
|
-
const
|
|
16
|
-
const active = new Set();
|
|
16
|
+
const live = createLiveHandles();
|
|
17
17
|
const storage = options.storage;
|
|
18
|
-
async function save(handle) {
|
|
19
|
-
handles.set(handle.id, handle);
|
|
20
|
-
return handle;
|
|
21
|
-
}
|
|
22
18
|
/**
|
|
23
19
|
* Persist a step manifest after spawn so `reattach` and `listLive` can
|
|
24
20
|
* find this handle again on a subsequent adapter instance. Errors are
|
|
@@ -58,48 +54,40 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
58
54
|
}
|
|
59
55
|
async function spawnStepHandle(request) {
|
|
60
56
|
const now = nowIso();
|
|
61
|
-
const
|
|
57
|
+
const spawned = live.add({
|
|
62
58
|
id: `subprocess-${crypto.randomUUID()}`,
|
|
63
59
|
status: 'running',
|
|
64
60
|
startedAt: now,
|
|
65
61
|
updatedAt: now,
|
|
66
62
|
metadata: injectMetadata(request, buildStepMetadata(request)),
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
await save(handle);
|
|
70
|
-
await persistIfDurable(request, handle);
|
|
63
|
+
});
|
|
64
|
+
await persistIfDurable(request, spawned.handle);
|
|
71
65
|
void completeStepRun({
|
|
72
66
|
request,
|
|
73
|
-
handle,
|
|
67
|
+
handle: spawned.handle,
|
|
74
68
|
stepRunner: options.stepRunner,
|
|
75
|
-
|
|
76
|
-
active,
|
|
77
|
-
save,
|
|
69
|
+
live,
|
|
78
70
|
clearIfDurable,
|
|
79
71
|
});
|
|
80
|
-
return
|
|
72
|
+
return spawned;
|
|
81
73
|
}
|
|
82
|
-
|
|
74
|
+
function spawnProcessHandle(request) {
|
|
83
75
|
const now = nowIso();
|
|
84
|
-
const
|
|
76
|
+
const spawned = live.add({
|
|
85
77
|
id: `subprocess-${crypto.randomUUID()}`,
|
|
86
78
|
status: 'running',
|
|
87
79
|
startedAt: now,
|
|
88
80
|
updatedAt: now,
|
|
89
81
|
metadata: injectMetadata(request, buildProcessMetadata(request)),
|
|
90
|
-
};
|
|
91
|
-
active.add(handle.id);
|
|
92
|
-
await save(handle);
|
|
82
|
+
});
|
|
93
83
|
void completeProcessRun({
|
|
94
84
|
request,
|
|
95
|
-
handle,
|
|
85
|
+
handle: spawned.handle,
|
|
96
86
|
run: options.run,
|
|
97
|
-
|
|
98
|
-
active,
|
|
99
|
-
save,
|
|
87
|
+
live,
|
|
100
88
|
clearIfDurable,
|
|
101
89
|
});
|
|
102
|
-
return
|
|
90
|
+
return spawned;
|
|
103
91
|
}
|
|
104
92
|
/**
|
|
105
93
|
* Build a hydrated handle from a persisted manifest. Status is `running`
|
|
@@ -134,19 +122,17 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
134
122
|
throw new Error('Unsupported subprocess request variant.');
|
|
135
123
|
},
|
|
136
124
|
async get(handleId) {
|
|
137
|
-
return
|
|
125
|
+
return live.get(handleId) ?? null;
|
|
138
126
|
},
|
|
139
127
|
async stop(handleId, reason) {
|
|
140
|
-
const handle =
|
|
128
|
+
const handle = live.get(handleId);
|
|
141
129
|
if (!handle) {
|
|
142
130
|
return {
|
|
143
131
|
kind: 'not_found',
|
|
144
132
|
handleId,
|
|
145
133
|
};
|
|
146
134
|
}
|
|
147
|
-
|
|
148
|
-
await clearIfDurable(handleId);
|
|
149
|
-
const next = await save({
|
|
135
|
+
const next = {
|
|
150
136
|
...handle,
|
|
151
137
|
status: 'stopped',
|
|
152
138
|
updatedAt: nowIso(),
|
|
@@ -154,7 +140,9 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
154
140
|
...(handle.metadata ?? {}),
|
|
155
141
|
stopReason: reason,
|
|
156
142
|
},
|
|
157
|
-
}
|
|
143
|
+
};
|
|
144
|
+
live.settle(next);
|
|
145
|
+
await clearIfDurable(handleId);
|
|
158
146
|
return {
|
|
159
147
|
kind: 'stopped',
|
|
160
148
|
handleId,
|
|
@@ -162,7 +150,7 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
162
150
|
};
|
|
163
151
|
},
|
|
164
152
|
async pause(handleId) {
|
|
165
|
-
const handle =
|
|
153
|
+
const handle = live.get(handleId);
|
|
166
154
|
if (!handle) {
|
|
167
155
|
return {
|
|
168
156
|
kind: 'not_found',
|
|
@@ -176,7 +164,7 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
176
164
|
};
|
|
177
165
|
},
|
|
178
166
|
async resume(handleId) {
|
|
179
|
-
const handle =
|
|
167
|
+
const handle = live.get(handleId);
|
|
180
168
|
if (!handle) {
|
|
181
169
|
return {
|
|
182
170
|
kind: 'not_found',
|
|
@@ -190,7 +178,7 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
190
178
|
};
|
|
191
179
|
},
|
|
192
180
|
async isAlive(handle) {
|
|
193
|
-
return
|
|
181
|
+
return live.get(handle.id) !== undefined;
|
|
194
182
|
},
|
|
195
183
|
/**
|
|
196
184
|
* Idempotent re-run of a persisted step, not a "resume" — for in-memory
|
|
@@ -207,29 +195,23 @@ export function createInMemorySubprocessAdapter(options = {}) {
|
|
|
207
195
|
if (!manifest) {
|
|
208
196
|
return null;
|
|
209
197
|
}
|
|
210
|
-
|
|
211
|
-
active.add(handle.id);
|
|
212
|
-
await save(handle);
|
|
213
|
-
return handle;
|
|
198
|
+
return live.add(handleFromManifest(manifest)).handle;
|
|
214
199
|
},
|
|
215
200
|
async listLive() {
|
|
216
|
-
const
|
|
217
|
-
for (const
|
|
218
|
-
|
|
219
|
-
if (handle) {
|
|
220
|
-
live.set(handle.id, handle);
|
|
221
|
-
}
|
|
201
|
+
const found = new Map();
|
|
202
|
+
for (const handle of live.values()) {
|
|
203
|
+
found.set(handle.id, handle);
|
|
222
204
|
}
|
|
223
205
|
if (storage) {
|
|
224
206
|
const manifests = await listManifests(storage);
|
|
225
207
|
for (const manifest of manifests) {
|
|
226
|
-
if (!
|
|
227
|
-
|
|
208
|
+
if (!found.has(manifest.handleId)) {
|
|
209
|
+
found.set(manifest.handleId, handleFromManifest(manifest));
|
|
228
210
|
}
|
|
229
211
|
}
|
|
230
212
|
}
|
|
231
213
|
return [
|
|
232
|
-
...
|
|
214
|
+
...found.values(),
|
|
233
215
|
];
|
|
234
216
|
},
|
|
235
217
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running handles a `SubprocessAdapter` holds, each paired with the
|
|
3
|
+
* promise its `spawn()` caller waits on. A handle leaves the table the moment
|
|
4
|
+
* it settles, so callers hear about a finished child at once (no polling) and
|
|
5
|
+
* the adapter keeps nothing once the result is delivered.
|
|
6
|
+
*/
|
|
7
|
+
import type { SpawnedSubprocess, SubprocessHandle } from '@noetic-tools/types';
|
|
8
|
+
/** @public Running handles keyed by id; see `createLiveHandles`. */
|
|
9
|
+
export interface LiveHandles {
|
|
10
|
+
/** Tracks a running handle. Returns what `spawn()` hands back. */
|
|
11
|
+
add(handle: SubprocessHandle): SpawnedSubprocess;
|
|
12
|
+
/** The running handle with this id, or `undefined` once it has settled. */
|
|
13
|
+
get(handleId: string): SubprocessHandle | undefined;
|
|
14
|
+
/** Replaces a running handle, e.g. on pause or resume. Ignored once it has settled. */
|
|
15
|
+
update(handle: SubprocessHandle): void;
|
|
16
|
+
/**
|
|
17
|
+
* Ends a handle: resolves its `settled` with `handle` (which carries the
|
|
18
|
+
* final status) and forgets it. Returns `false`, changing nothing, when the
|
|
19
|
+
* handle already settled — a stopped child that finishes late, say.
|
|
20
|
+
*/
|
|
21
|
+
settle(handle: SubprocessHandle): boolean;
|
|
22
|
+
/** Every running handle. */
|
|
23
|
+
values(): IterableIterator<SubprocessHandle>;
|
|
24
|
+
}
|
|
25
|
+
/** @public Creates an empty `LiveHandles` table for a `SubprocessAdapter`. */
|
|
26
|
+
export declare function createLiveHandles(): LiveHandles;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running handles a `SubprocessAdapter` holds, each paired with the
|
|
3
|
+
* promise its `spawn()` caller waits on. A handle leaves the table the moment
|
|
4
|
+
* it settles, so callers hear about a finished child at once (no polling) and
|
|
5
|
+
* the adapter keeps nothing once the result is delivered.
|
|
6
|
+
*/
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region Public API
|
|
9
|
+
/** @public Creates an empty `LiveHandles` table for a `SubprocessAdapter`. */
|
|
10
|
+
export function createLiveHandles() {
|
|
11
|
+
const entries = new Map();
|
|
12
|
+
return {
|
|
13
|
+
add(handle) {
|
|
14
|
+
const settled = new Promise((resolve) => {
|
|
15
|
+
entries.set(handle.id, {
|
|
16
|
+
handle,
|
|
17
|
+
resolve,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
handle,
|
|
22
|
+
settled,
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
get(handleId) {
|
|
26
|
+
return entries.get(handleId)?.handle;
|
|
27
|
+
},
|
|
28
|
+
update(handle) {
|
|
29
|
+
const entry = entries.get(handle.id);
|
|
30
|
+
if (entry) {
|
|
31
|
+
entry.handle = handle;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
settle(handle) {
|
|
35
|
+
const entry = entries.get(handle.id);
|
|
36
|
+
if (!entry) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
entries.delete(handle.id);
|
|
40
|
+
entry.resolve(handle);
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
*values() {
|
|
44
|
+
for (const entry of entries.values()) {
|
|
45
|
+
yield entry.handle;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
@@ -791,12 +791,10 @@ async function runCodeViaSubprocess(opts) {
|
|
|
791
791
|
input,
|
|
792
792
|
},
|
|
793
793
|
};
|
|
794
|
-
const spawnPromise = adapter.spawn(request);
|
|
795
794
|
const handle = new DetachedHandleImpl({
|
|
796
795
|
id: `run-${nodeId}`,
|
|
797
796
|
stepId: nodeId,
|
|
798
|
-
adapter,
|
|
799
|
-
spawnPromise,
|
|
797
|
+
spawned: adapter.spawn(request),
|
|
800
798
|
});
|
|
801
799
|
const result = await handle.await();
|
|
802
800
|
return stringifyResult(result);
|
package/dist/index.d.ts
CHANGED
|
@@ -181,11 +181,15 @@ export { ACP_AGENT_STEP_KIND, AcpCapabilityError, AcpConnectError, AcpPermission
|
|
|
181
181
|
export type { FsAdapter, FsStats } from '@noetic-tools/types';
|
|
182
182
|
/** @public */
|
|
183
183
|
/** @public */
|
|
184
|
-
export type { ProcessSubprocessRequest, SerializedError, ShellAdapter, ShellExecOptions, ShellExecResult, StepSubprocessOverrides, StepSubprocessRequest, SubprocessAdapter, SubprocessControlResult, SubprocessHandle, SubprocessHandleMetadata, SubprocessRequest, SubprocessStatus, SubprocessStopResult, } from '@noetic-tools/types';
|
|
184
|
+
export type { ProcessSubprocessRequest, SerializedError, ShellAdapter, ShellExecOptions, ShellExecResult, SpawnedSubprocess, StepSubprocessOverrides, StepSubprocessRequest, SubprocessAdapter, SubprocessControlResult, SubprocessHandle, SubprocessHandleMetadata, SubprocessRequest, SubprocessStatus, SubprocessStopResult, } from '@noetic-tools/types';
|
|
185
185
|
/** @public */
|
|
186
186
|
export { TIMEOUT_ERROR_PREFIX } from '@noetic-tools/types';
|
|
187
187
|
/** @public Reusable error-serialiser used by custom SubprocessAdapter implementations. */
|
|
188
188
|
export { serializeError } from './adapters/in-memory-subprocess/metadata.js';
|
|
189
|
+
/** @public Running-handle table that custom SubprocessAdapter implementations settle through. */
|
|
190
|
+
export type { LiveHandles } from './adapters/live-handles.js';
|
|
191
|
+
/** @public */
|
|
192
|
+
export { createLiveHandles } from './adapters/live-handles.js';
|
|
189
193
|
/** @public */
|
|
190
194
|
export type { ToolContext, ToolExecutionContext } from '@noetic-tools/types';
|
|
191
195
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -110,6 +110,8 @@ export { ACP_AGENT_STEP_KIND, AcpCapabilityError, AcpConnectError, AcpPermission
|
|
|
110
110
|
export { TIMEOUT_ERROR_PREFIX } from '@noetic-tools/types';
|
|
111
111
|
/** @public Reusable error-serialiser used by custom SubprocessAdapter implementations. */
|
|
112
112
|
export { serializeError } from './adapters/in-memory-subprocess/metadata.js';
|
|
113
|
+
/** @public */
|
|
114
|
+
export { createLiveHandles } from './adapters/live-handles.js';
|
|
113
115
|
//#endregion
|
|
114
116
|
//#region Until
|
|
115
117
|
/** @public */
|
|
@@ -81,12 +81,10 @@ async function dispatchViaAdapter(step, input, ctx, executionId, executor) {
|
|
|
81
81
|
},
|
|
82
82
|
_localExecutor: () => executor(),
|
|
83
83
|
};
|
|
84
|
-
const spawnPromise = adapter.spawn(request);
|
|
85
84
|
const handle = new DetachedHandleImpl({
|
|
86
85
|
id: executionId,
|
|
87
86
|
stepId: step.id,
|
|
88
|
-
adapter,
|
|
89
|
-
spawnPromise,
|
|
87
|
+
spawned: adapter.spawn(request),
|
|
90
88
|
});
|
|
91
89
|
return handle.await();
|
|
92
90
|
}
|
|
@@ -43,8 +43,8 @@ export interface DetachedSpawnOverrides {
|
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Route a step through the configured subprocess adapter and return a
|
|
46
|
-
* `DetachedHandle` that
|
|
47
|
-
*
|
|
46
|
+
* `DetachedHandle` that settles when the adapter reports the child
|
|
47
|
+
* finished. The adapter's `spawn()` may be async (the default in-memory
|
|
48
48
|
* adapter schedules the step body on the microtask queue, and
|
|
49
49
|
* out-of-process adapters may await an OS spawn).
|
|
50
50
|
*
|
|
@@ -24,8 +24,8 @@ export function resolveStepAdapter(step, callOverride, fallback) {
|
|
|
24
24
|
//#region Dispatcher
|
|
25
25
|
/**
|
|
26
26
|
* Route a step through the configured subprocess adapter and return a
|
|
27
|
-
* `DetachedHandle` that
|
|
28
|
-
*
|
|
27
|
+
* `DetachedHandle` that settles when the adapter reports the child
|
|
28
|
+
* finished. The adapter's `spawn()` may be async (the default in-memory
|
|
29
29
|
* adapter schedules the step body on the microtask queue, and
|
|
30
30
|
* out-of-process adapters may await an OS spawn).
|
|
31
31
|
*
|
|
@@ -57,12 +57,10 @@ export function dispatchStepThroughAdapter(h, s, input, parentCtx, overrides) {
|
|
|
57
57
|
},
|
|
58
58
|
_localExecutor: () => executeNoAdapter(s, input, childCtx),
|
|
59
59
|
};
|
|
60
|
-
const spawnPromise = adapter.spawn(request);
|
|
61
60
|
return new DetachedHandleImpl({
|
|
62
61
|
id: childCtx.id,
|
|
63
62
|
stepId: s.id,
|
|
64
|
-
adapter,
|
|
65
|
-
spawnPromise,
|
|
63
|
+
spawned: adapter.spawn(request),
|
|
66
64
|
});
|
|
67
65
|
}
|
|
68
66
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DetachedHandle,
|
|
1
|
+
import type { DetachedHandle, SpawnedSubprocess } from '@noetic-tools/types';
|
|
2
2
|
import { DetachedStatus } from '@noetic-tools/types';
|
|
3
3
|
interface DetachedHandleOpts {
|
|
4
4
|
/** Id surfaced to callers — usually the execution id (childCtx.id in
|
|
@@ -7,33 +7,23 @@ interface DetachedHandleOpts {
|
|
|
7
7
|
/** Step id — used when the adapter surfaces a generic failure and we need
|
|
8
8
|
* to produce a `NoeticError` with a meaningful `stepId`. */
|
|
9
9
|
stepId: string;
|
|
10
|
-
/** Adapter that produced the handle. */
|
|
11
|
-
adapter: SubprocessAdapter;
|
|
12
10
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* is reachable even before the spawn itself has settled.
|
|
11
|
+
* The in-flight `adapter.spawn(request)` call, passed unawaited so the
|
|
12
|
+
* handle exists (and `.await()` works) before the spawn itself resolves.
|
|
16
13
|
*/
|
|
17
|
-
|
|
14
|
+
spawned: Promise<SpawnedSubprocess>;
|
|
18
15
|
}
|
|
19
16
|
/**
|
|
20
17
|
* @public
|
|
21
|
-
* Thin wrapper over a
|
|
22
|
-
* contract.
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* The polling cadence starts at 10ms and ramps to 100ms after the first
|
|
28
|
-
* second — responsive for short-lived in-memory steps without burning cycles
|
|
29
|
-
* on long-running ones.
|
|
18
|
+
* Thin wrapper over a spawned subprocess that implements the `DetachedHandle`
|
|
19
|
+
* contract. It waits on the adapter's `settled` promise and reads the result
|
|
20
|
+
* (or error) off the final handle's metadata. Waiting starts in the
|
|
21
|
+
* constructor, so `status`, `result`, and `error` settle whether or not the
|
|
22
|
+
* caller ever invokes `.await()`.
|
|
30
23
|
*/
|
|
31
24
|
export declare class DetachedHandleImpl<O> implements DetachedHandle<O> {
|
|
32
25
|
readonly id: string;
|
|
33
26
|
private readonly _stepId;
|
|
34
|
-
private readonly _adapter;
|
|
35
|
-
private readonly _spawnPromise;
|
|
36
|
-
private readonly _startTime;
|
|
37
27
|
private _status;
|
|
38
28
|
private _result;
|
|
39
29
|
private _error;
|
|
@@ -49,7 +39,6 @@ export declare class DetachedHandleImpl<O> implements DetachedHandle<O> {
|
|
|
49
39
|
* observes the final value.
|
|
50
40
|
*/
|
|
51
41
|
await(timeout?: number): Promise<O>;
|
|
52
|
-
private pollUntilSettled;
|
|
53
42
|
private finalise;
|
|
54
43
|
}
|
|
55
44
|
export {};
|
|
@@ -1,35 +1,6 @@
|
|
|
1
1
|
import { DetachedStatus, frameworkCast, NoeticErrorImpl } from '@noetic-tools/types';
|
|
2
2
|
//#endregion
|
|
3
3
|
//#region Helpers
|
|
4
|
-
const INITIAL_POLL_MS = 10;
|
|
5
|
-
const MAX_POLL_MS = 100;
|
|
6
|
-
const POLL_RAMP_AFTER_MS = 1_000;
|
|
7
|
-
/** Upper bound on microtask yields before falling back to timer-based
|
|
8
|
-
* polling. Chosen empirically: in-memory step bodies settle within a
|
|
9
|
-
* handful of microtasks, and 64 gives plenty of headroom for chained
|
|
10
|
-
* awaits inside complex step trees without busy-spinning. */
|
|
11
|
-
const MAX_MICROTASK_YIELDS = 64;
|
|
12
|
-
/** Grace period before treating persistent null `adapter.get(handleId)`
|
|
13
|
-
* results as an eviction. Tolerates transient nulls from adapters that
|
|
14
|
-
* register the handle asynchronously relative to the spawn promise
|
|
15
|
-
* settling, but bounds the worst case so a corrupt adapter doesn't
|
|
16
|
-
* produce an infinite poll. */
|
|
17
|
-
const HANDLE_EVICTED_GRACE_MS = 500;
|
|
18
|
-
/**
|
|
19
|
-
* Yield control to the timer queue so the adapter has a chance to
|
|
20
|
-
* transition the handle before we issue the next `adapter.get()` call.
|
|
21
|
-
* Using a timer (even 0ms) ensures we don't spin-lock when the adapter
|
|
22
|
-
* schedules its completion on a later microtask.
|
|
23
|
-
*/
|
|
24
|
-
function sleep(ms) {
|
|
25
|
-
if (ms <= 0) {
|
|
26
|
-
return Promise.resolve();
|
|
27
|
-
}
|
|
28
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
29
|
-
}
|
|
30
|
-
function isTerminalStatus(status) {
|
|
31
|
-
return (status === 'completed' || status === 'failed' || status === 'stopped' || status === 'stale');
|
|
32
|
-
}
|
|
33
4
|
function isRecord(value) {
|
|
34
5
|
return typeof value === 'object' && value !== null;
|
|
35
6
|
}
|
|
@@ -90,22 +61,15 @@ function extractError(metadata) {
|
|
|
90
61
|
//#region DetachedHandleImpl
|
|
91
62
|
/**
|
|
92
63
|
* @public
|
|
93
|
-
* Thin wrapper over a
|
|
94
|
-
* contract.
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* The polling cadence starts at 10ms and ramps to 100ms after the first
|
|
100
|
-
* second — responsive for short-lived in-memory steps without burning cycles
|
|
101
|
-
* on long-running ones.
|
|
64
|
+
* Thin wrapper over a spawned subprocess that implements the `DetachedHandle`
|
|
65
|
+
* contract. It waits on the adapter's `settled` promise and reads the result
|
|
66
|
+
* (or error) off the final handle's metadata. Waiting starts in the
|
|
67
|
+
* constructor, so `status`, `result`, and `error` settle whether or not the
|
|
68
|
+
* caller ever invokes `.await()`.
|
|
102
69
|
*/
|
|
103
70
|
export class DetachedHandleImpl {
|
|
104
71
|
id;
|
|
105
72
|
_stepId;
|
|
106
|
-
_adapter;
|
|
107
|
-
_spawnPromise;
|
|
108
|
-
_startTime;
|
|
109
73
|
_status = DetachedStatus.Running;
|
|
110
74
|
_result;
|
|
111
75
|
_error;
|
|
@@ -113,13 +77,8 @@ export class DetachedHandleImpl {
|
|
|
113
77
|
constructor(opts) {
|
|
114
78
|
this.id = opts.id;
|
|
115
79
|
this._stepId = opts.stepId;
|
|
116
|
-
this.
|
|
117
|
-
|
|
118
|
-
this._startTime = Date.now();
|
|
119
|
-
// One shared poll for the handle's life, so repeated `.await()` calls
|
|
120
|
-
// don't start parallel loops. The rejection is swallowed here only to
|
|
121
|
-
// record it; `.await()` surfaces it to the caller.
|
|
122
|
-
this._settlement = this.pollUntilSettled();
|
|
80
|
+
this._settlement = opts.spawned.then((s) => s.settled).then((h) => this.finalise(h));
|
|
81
|
+
// Swallowed here only to record the failure; `.await()` surfaces it.
|
|
123
82
|
this._settlement.catch((err) => {
|
|
124
83
|
if (this._status === DetachedStatus.Running) {
|
|
125
84
|
this._status = DetachedStatus.Failed;
|
|
@@ -170,66 +129,6 @@ export class DetachedHandleImpl {
|
|
|
170
129
|
}
|
|
171
130
|
}
|
|
172
131
|
}
|
|
173
|
-
async pollUntilSettled() {
|
|
174
|
-
// Wait for the spawn itself to succeed before we start polling —
|
|
175
|
-
// otherwise we'd be polling with an undefined handle id.
|
|
176
|
-
const initialHandle = await this._spawnPromise;
|
|
177
|
-
if (isTerminalStatus(initialHandle.status)) {
|
|
178
|
-
return this.finalise(initialHandle);
|
|
179
|
-
}
|
|
180
|
-
// Track when the adapter persistently returns null for this handle.
|
|
181
|
-
// Transient nulls are tolerated for `HANDLE_EVICTED_GRACE_MS`; after
|
|
182
|
-
// that we raise `handle_evicted` instead of looping forever. A
|
|
183
|
-
// non-null observation resets the grace window.
|
|
184
|
-
let firstNullAt = null;
|
|
185
|
-
const observeNull = () => {
|
|
186
|
-
if (firstNullAt === null) {
|
|
187
|
-
firstNullAt = Date.now();
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
if (Date.now() - firstNullAt >= HANDLE_EVICTED_GRACE_MS) {
|
|
191
|
-
throw new NoeticErrorImpl({
|
|
192
|
-
kind: 'handle_evicted',
|
|
193
|
-
handleId: initialHandle.id,
|
|
194
|
-
stepId: this._stepId,
|
|
195
|
-
gracePeriod: HANDLE_EVICTED_GRACE_MS,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
const observeHandle = (h) => {
|
|
200
|
-
if (h === null) {
|
|
201
|
-
observeNull();
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
firstNullAt = null;
|
|
205
|
-
return h;
|
|
206
|
-
};
|
|
207
|
-
// Fast path: yield through the microtask queue first. The in-memory
|
|
208
|
-
// adapter schedules completion on a microtask, so a handful of yields
|
|
209
|
-
// typically resolves the handle in sub-millisecond time. This keeps the
|
|
210
|
-
// interpreter's "route through adapter" overhead negligible.
|
|
211
|
-
for (let i = 0; i < MAX_MICROTASK_YIELDS; i++) {
|
|
212
|
-
const handle = observeHandle(await this._adapter.get(initialHandle.id));
|
|
213
|
-
if (handle && isTerminalStatus(handle.status)) {
|
|
214
|
-
return this.finalise(handle);
|
|
215
|
-
}
|
|
216
|
-
await Promise.resolve();
|
|
217
|
-
}
|
|
218
|
-
// Slow path: fall back to timer-based polling for genuinely long-running
|
|
219
|
-
// handles (out-of-process adapters, async work that doesn't resolve on
|
|
220
|
-
// the microtask queue).
|
|
221
|
-
let delay = INITIAL_POLL_MS;
|
|
222
|
-
for (;;) {
|
|
223
|
-
const handle = observeHandle(await this._adapter.get(initialHandle.id));
|
|
224
|
-
if (handle && isTerminalStatus(handle.status)) {
|
|
225
|
-
return this.finalise(handle);
|
|
226
|
-
}
|
|
227
|
-
await sleep(delay);
|
|
228
|
-
if (Date.now() - this._startTime >= POLL_RAMP_AFTER_MS) {
|
|
229
|
-
delay = MAX_POLL_MS;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
132
|
finalise(handle) {
|
|
234
133
|
const metadata = handle.metadata;
|
|
235
134
|
if (handle.status === 'completed') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@noetic-tools/context": "^1.9.0",
|
|
72
|
-
"@noetic-tools/types": "^3.
|
|
72
|
+
"@noetic-tools/types": "^3.11.0",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|