@felan-ai/daemon 0.8.6 → 0.8.7
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/daemon/control-recovery.d.ts +4 -1
- package/dist/daemon/control-recovery.d.ts.map +1 -1
- package/dist/daemon/control-recovery.js +23 -2
- package/dist/daemon/control-recovery.js.map +1 -1
- package/dist/daemon/control-store.d.ts +11 -1
- package/dist/daemon/control-store.d.ts.map +1 -1
- package/dist/daemon/control-store.js +48 -2
- package/dist/daemon/control-store.js.map +1 -1
- package/dist/daemon/executions.d.ts +21 -0
- package/dist/daemon/executions.d.ts.map +1 -1
- package/dist/daemon/executions.js +77 -10
- package/dist/daemon/executions.js.map +1 -1
- package/dist/daemon/lifecycle.d.ts +19 -0
- package/dist/daemon/lifecycle.d.ts.map +1 -1
- package/dist/daemon/lifecycle.js +50 -2
- package/dist/daemon/lifecycle.js.map +1 -1
- package/dist/daemon/persistence.d.ts +10 -0
- package/dist/daemon/persistence.d.ts.map +1 -1
- package/dist/daemon/persistence.js +24 -3
- package/dist/daemon/persistence.js.map +1 -1
- package/dist/daemon/processes.d.ts +2 -0
- package/dist/daemon/processes.d.ts.map +1 -1
- package/dist/daemon/processes.js +11 -4
- package/dist/daemon/processes.js.map +1 -1
- package/dist/daemon/projection.d.ts +5 -0
- package/dist/daemon/projection.d.ts.map +1 -1
- package/dist/daemon/projection.js +13 -0
- package/dist/daemon/projection.js.map +1 -1
- package/dist/daemon/registry.d.ts +58 -2
- package/dist/daemon/registry.d.ts.map +1 -1
- package/dist/daemon/registry.js +368 -13
- package/dist/daemon/registry.js.map +1 -1
- package/dist/daemon/run-hold.d.ts +131 -0
- package/dist/daemon/run-hold.d.ts.map +1 -0
- package/dist/daemon/run-hold.js +89 -0
- package/dist/daemon/run-hold.js.map +1 -0
- package/dist/daemon/server.d.ts +4 -0
- package/dist/daemon/server.d.ts.map +1 -1
- package/dist/daemon/server.js +7 -5
- package/dist/daemon/server.js.map +1 -1
- package/dist/daemon/service.d.ts +2 -0
- package/dist/daemon/service.d.ts.map +1 -1
- package/dist/daemon/service.js +9 -0
- package/dist/daemon/service.js.map +1 -1
- package/package.json +3 -3
package/dist/daemon/registry.js
CHANGED
|
@@ -18,6 +18,7 @@ import { shutdownStep } from './shutdown-error.js';
|
|
|
18
18
|
import { HostWorkers } from './host-workers.js';
|
|
19
19
|
import { assertStagedWorker } from './worker.js';
|
|
20
20
|
import { syncOwnedDirectory } from './persistence.js';
|
|
21
|
+
import { installStepHold, RUN_CONTINUED_MESSAGE_TYPE, RUN_CONTINUED_TEXT } from './run-hold.js';
|
|
21
22
|
/** @internal */
|
|
22
23
|
export const PROJECTION_CLEANUP_LIMIT_MS = 30_000;
|
|
23
24
|
const nonRunMutations = new Set(['set_model', 'set_thinking', 'session.rename', 'session.entry.label', 'session.branch.select', 'session.export', 'command.invoke', 'shell.execute', 'processes.write', 'processes.stop', 'steer', 'follow_up', 'queue.clear', 'executions.steer', 'executions.cancel', 'ui.action']);
|
|
@@ -59,6 +60,10 @@ export class SessionRegistry {
|
|
|
59
60
|
cancellationFailures = new Set();
|
|
60
61
|
lossUnsubscribe;
|
|
61
62
|
commandDispatcher;
|
|
63
|
+
stepHold;
|
|
64
|
+
continuing = new Set();
|
|
65
|
+
heldWaiters = new Set();
|
|
66
|
+
restored = false;
|
|
62
67
|
constructor(options) {
|
|
63
68
|
this.options = options;
|
|
64
69
|
options.owner.assertCurrent();
|
|
@@ -67,7 +72,14 @@ export class SessionRegistry {
|
|
|
67
72
|
this.now = options.now ?? Date.now;
|
|
68
73
|
this.startedAt = this.now();
|
|
69
74
|
this.legacy = new LegacySessionIndex(options.owner);
|
|
70
|
-
|
|
75
|
+
if (options.store.needsRecovery) {
|
|
76
|
+
const held = options.store.heldRuns();
|
|
77
|
+
this.recovery = options.store.recover(held).value;
|
|
78
|
+
// Records are consumed at startup, so a crash during a continued run interrupts it instead of continuing it twice.
|
|
79
|
+
options.store.clearHeldRuns();
|
|
80
|
+
for (const record of this.recovery.continueRuns)
|
|
81
|
+
this.continuing.add(record.session);
|
|
82
|
+
}
|
|
71
83
|
this.processes = new RegistryProcesses({ ...(options.processAdapter ? { processAdapter: options.processAdapter } : {}), store: options.store, now: this.now, assertCurrent: () => options.owner.assertCurrent(),
|
|
72
84
|
changed: (id, process, rev) => this.publish({ v: 1, session: id, rev, change: { type: 'process.updated', process } }) });
|
|
73
85
|
this.hostWorkers = new HostWorkers({ owner: options.owner, processes: this.processes, lifecycle: () => this.options.lifecycle,
|
|
@@ -164,7 +176,181 @@ export class SessionRegistry {
|
|
|
164
176
|
/** Reopens admission after an abandoned drain; a registry that started shutting down cannot resume. */
|
|
165
177
|
endDrain() {
|
|
166
178
|
this.assertAdmission();
|
|
179
|
+
this.releaseHeld();
|
|
167
180
|
this.draining = false;
|
|
181
|
+
// Children queued while admission was closed could not start, and a hold may end without any job finishing to start them.
|
|
182
|
+
for (const entry of this.entries.values())
|
|
183
|
+
entry.host?.resumeQueued();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Parks every active run at its next step end. Once every resident session is idle or parked and nothing else runs, writes
|
|
187
|
+
* one continue record per parked run and resolves with their number. A run that parks later, while the daemon stays held,
|
|
188
|
+
* gets its record when it parks. When `signal` aborts or a record cannot be written, the parked runs continue and no
|
|
189
|
+
* record remains.
|
|
190
|
+
*/
|
|
191
|
+
async holdRuns(signal, outside) {
|
|
192
|
+
this.assertAdmission();
|
|
193
|
+
if (!this.draining || this.stepHold)
|
|
194
|
+
throw new Error('Runs are held only once, after admission closed.');
|
|
195
|
+
const hold = { held: false, changed: () => { } };
|
|
196
|
+
this.stepHold = hold;
|
|
197
|
+
try {
|
|
198
|
+
for (;;) {
|
|
199
|
+
signal.throwIfAborted();
|
|
200
|
+
const { busy, questions } = await outside();
|
|
201
|
+
// A run waiting for a person or a process that outlasts every held run ends only with outside help.
|
|
202
|
+
if (questions)
|
|
203
|
+
throw new Error('A run waits for a person\'s answer, so it cannot reach the end of its step.');
|
|
204
|
+
const work = await this.processes.interruptibleCount();
|
|
205
|
+
const live = await this.processes.liveCount();
|
|
206
|
+
// The synchronous check comes last, so no run can start between it and the record write.
|
|
207
|
+
const quiet = this.runsQuiet();
|
|
208
|
+
if (live && quiet)
|
|
209
|
+
throw new Error('A live process keeps running after every run reached the end of its step; a restart would interrupt it.');
|
|
210
|
+
if (!busy && !work && quiet)
|
|
211
|
+
break;
|
|
212
|
+
// Parks and settled runs wake the wait; the interval only re-reads process and provider work.
|
|
213
|
+
await new Promise(resolve => {
|
|
214
|
+
const done = () => { clearTimeout(timer); signal.removeEventListener('abort', done); hold.changed = () => { }; resolve(); };
|
|
215
|
+
const timer = setTimeout(done, 100);
|
|
216
|
+
hold.changed = done;
|
|
217
|
+
signal.addEventListener('abort', done, { once: true });
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
signal.throwIfAborted();
|
|
221
|
+
const parked = [...this.entries].filter(([, entry]) => entry.parked);
|
|
222
|
+
const records = parked.map(([id, entry]) => this.continueRecord(id, entry, entry.parked));
|
|
223
|
+
this.options.store.putHeldRuns(records);
|
|
224
|
+
parked.forEach(([, entry], index) => { entry.held = records[index]; });
|
|
225
|
+
hold.held = true;
|
|
226
|
+
return records.length;
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
if (this.stepHold === hold)
|
|
230
|
+
this.releaseHeld();
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** True while a hold waits for or holds runs; a subagent completion then reaches only an idle parent. */
|
|
235
|
+
get holdingRuns() { return this.stepHold !== undefined; }
|
|
236
|
+
isHeld(id) { return this.entries.get(id)?.held !== undefined; }
|
|
237
|
+
/** Ends a continued child run whose next step failed, as a restart interruption. */
|
|
238
|
+
interruptContinued(id) { this.interruptRun(id); }
|
|
239
|
+
/** Ends a continued child run that reached its turn limit before another step, as the limit's abort ends an unheld run. */
|
|
240
|
+
settleContinued(id) {
|
|
241
|
+
this.continuing.delete(id);
|
|
242
|
+
this.require(id).projection.settleRun(this.options.store.sessionMetadata(id).run.id);
|
|
243
|
+
}
|
|
244
|
+
/** True when the session is resident with no run, so a completion can start one even while runs are held. */
|
|
245
|
+
idleForCompletion(id) {
|
|
246
|
+
const entry = this.entries.get(id);
|
|
247
|
+
return !!entry && !entry.parked && !entry.held && !entry.dispatches.size && !entry.writers.size && entry.binding.runtime.session.isIdle;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Deletes every continue record before the parked runs take their next step, so a later crash cannot continue them twice.
|
|
251
|
+
* The steps are released even when the delete fails; a stale record then fails its transcript check once the run goes on.
|
|
252
|
+
*/
|
|
253
|
+
releaseHeld() {
|
|
254
|
+
if (!this.stepHold)
|
|
255
|
+
return;
|
|
256
|
+
this.stepHold = undefined;
|
|
257
|
+
try {
|
|
258
|
+
this.options.store.deleteHeldRuns([...this.entries].filter(([, entry]) => entry.held).map(([id]) => id));
|
|
259
|
+
}
|
|
260
|
+
finally {
|
|
261
|
+
for (const entry of this.entries.values()) {
|
|
262
|
+
delete entry.held;
|
|
263
|
+
entry.parked?.release('continue');
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Waits for `work`, or less once `held()` is true: a run held after `held` was reported waits for the next daemon, so a stop
|
|
269
|
+
* must not wait for it to end.
|
|
270
|
+
*/
|
|
271
|
+
async untilHeld(held, work) {
|
|
272
|
+
if (!work)
|
|
273
|
+
return;
|
|
274
|
+
if (!this.stepHold?.held) {
|
|
275
|
+
await work;
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
let wake;
|
|
279
|
+
const reached = new Promise(resolve => { wake = resolve; });
|
|
280
|
+
const check = () => { if (held())
|
|
281
|
+
wake(); };
|
|
282
|
+
this.heldWaiters.add(check);
|
|
283
|
+
check();
|
|
284
|
+
try {
|
|
285
|
+
await Promise.race([work, reached]);
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
this.heldWaiters.delete(check);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
rootHeld(root) {
|
|
292
|
+
return [...this.entries.values()].some(entry => entry.held && entry.binding.rootSessionId === root);
|
|
293
|
+
}
|
|
294
|
+
/** Forgets one run's hold when the run is cancelled, closed or interrupted, so no later daemon continues it. */
|
|
295
|
+
dropHeld(id) {
|
|
296
|
+
const entry = this.entries.get(id);
|
|
297
|
+
if (!entry?.held)
|
|
298
|
+
return;
|
|
299
|
+
this.options.store.deleteHeldRuns([id]);
|
|
300
|
+
delete entry.held;
|
|
301
|
+
}
|
|
302
|
+
parked(id, entry, step) {
|
|
303
|
+
const hold = this.stepHold;
|
|
304
|
+
if (!hold || this.entries.get(id) !== entry) {
|
|
305
|
+
step.release('continue');
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
entry.parked = step;
|
|
309
|
+
// A run an extension started after the hold was reported, such as for a background command's finish notice.
|
|
310
|
+
if (hold.held) {
|
|
311
|
+
try {
|
|
312
|
+
const record = this.continueRecord(id, entry, step);
|
|
313
|
+
this.options.store.putHeldRuns([record]);
|
|
314
|
+
entry.held = record;
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
// Without a record the next daemon cannot continue the run, so it goes on here instead of waiting parked.
|
|
318
|
+
this.diagnostic({ scope: 'registry', session: id, error });
|
|
319
|
+
step.release('continue');
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
for (const check of this.heldWaiters)
|
|
323
|
+
check();
|
|
324
|
+
}
|
|
325
|
+
hold.changed();
|
|
326
|
+
}
|
|
327
|
+
continueRecord(id, entry, step) {
|
|
328
|
+
const stored = this.options.store.sessionMetadata(id);
|
|
329
|
+
const queue = entry.projection.heldInput();
|
|
330
|
+
if (!stored.run.id || !queue || !step.leaf)
|
|
331
|
+
throw new Error('The parked run cannot be recorded.');
|
|
332
|
+
const turns = stored.execution ? this.executionHost(id).heldTurns(id) : undefined;
|
|
333
|
+
return { version: 1, session: id, root: stored.root, ...(stored.parent ? { parent: stored.parent } : {}),
|
|
334
|
+
run: { id: stored.run.id, ...(stored.run.origin ? { origin: stored.run.origin } : {}), ...(stored.run.turnId ? { turnId: stored.run.turnId } : {}) },
|
|
335
|
+
leaf: step.leaf, queue, ...(turns === undefined ? {} : { turns }) };
|
|
336
|
+
}
|
|
337
|
+
/** Every resident session is idle or parked at a step end with nothing else in flight. */
|
|
338
|
+
runsQuiet() {
|
|
339
|
+
if (this.opening.size || this.closes.size || this.lifecycleWriters.size || this.lateWriters.size)
|
|
340
|
+
return false;
|
|
341
|
+
for (const entry of this.entries.values()) {
|
|
342
|
+
if (entry.cancelling || entry.disposing || entry.projectionFailed)
|
|
343
|
+
return false;
|
|
344
|
+
// The run's own dispatch stays open while it is parked; anything else, such as a queued input still being admitted, must finish.
|
|
345
|
+
if (entry.parked) {
|
|
346
|
+
if (entry.dispatches.size > 1 || !entry.projection.heldInput())
|
|
347
|
+
return false;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (!entry.binding.runtime.session.isIdle || entry.dispatches.size || entry.writers.size || [...entry.services].some(service => service.busy()))
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
168
354
|
}
|
|
169
355
|
async waitForDrain(signal) {
|
|
170
356
|
this.assertAdmission();
|
|
@@ -176,11 +362,18 @@ export class SessionRegistry {
|
|
|
176
362
|
await this.join([...this.opening.values(), ...this.closes.values(), ...this.lifecycleWriters], undefined, true);
|
|
177
363
|
await this.hostWorkers.settle();
|
|
178
364
|
for (const entry of this.entries.values()) {
|
|
179
|
-
|
|
365
|
+
if (entry.held)
|
|
366
|
+
continue;
|
|
367
|
+
const root = entry.binding.rootSessionId;
|
|
368
|
+
// A held child keeps its execution job open, so its root's executions settle only in the next daemon.
|
|
369
|
+
if (!this.rootHeld(root))
|
|
370
|
+
await this.untilHeld(() => this.rootHeld(root), entry.host?.settle());
|
|
180
371
|
await entry.cancelling;
|
|
181
372
|
await entry.projectionCancellation;
|
|
182
373
|
if (!entry.binding.runtime.session.isIdle)
|
|
183
|
-
await entry.binding.runtime.session.waitForIdle();
|
|
374
|
+
await this.untilHeld(() => entry.held !== undefined, entry.binding.runtime.session.waitForIdle());
|
|
375
|
+
if (entry.held)
|
|
376
|
+
continue;
|
|
184
377
|
await this.join([...entry.writers]);
|
|
185
378
|
for (const service of entry.services)
|
|
186
379
|
await service.settle('drain');
|
|
@@ -200,8 +393,11 @@ export class SessionRegistry {
|
|
|
200
393
|
async activeWorkByKind() {
|
|
201
394
|
this.options.owner.assertCurrent();
|
|
202
395
|
const processes = await this.processes.interruptibleCount();
|
|
203
|
-
const
|
|
204
|
-
|
|
396
|
+
const held = new Set([...this.entries.values()].filter(entry => entry.held).map(entry => entry.binding.rootSessionId));
|
|
397
|
+
// A root with a held run keeps that run's execution job open, so only completions this daemon can still deliver count there.
|
|
398
|
+
const busy = [...this.entries].filter(([id, entry]) => !entry.held && (this.lateWriters.has(id) || !entry.binding.runtime.session.isIdle || entry.dispatches.size || entry.writers.size
|
|
399
|
+
|| (held.has(entry.binding.rootSessionId) ? entry.host?.hasDeliverableCompletion() : entry.host?.hasPendingWork() || entry.host?.hasActiveJobs())
|
|
400
|
+
|| [...entry.services].some(service => service.busy()))).length;
|
|
205
401
|
return { sessions: busy + [...this.lateWriters.keys()].filter(id => !this.entries.has(id)).length, processes };
|
|
206
402
|
}
|
|
207
403
|
create(request) {
|
|
@@ -289,10 +485,55 @@ export class SessionRegistry {
|
|
|
289
485
|
throw registryError('forbidden', 'Client session directory is reserved for daemon state.');
|
|
290
486
|
}
|
|
291
487
|
/**
|
|
292
|
-
*
|
|
293
|
-
* so their extensions watch the jobs and deliver
|
|
488
|
+
* Continues the runs a previous daemon held, parents before children, then restores the sessions startup recovery found
|
|
489
|
+
* with a background job that may still run or still owes a finish notice, so their extensions watch the jobs and deliver
|
|
490
|
+
* the notices without waiting for a prompt. A held run that cannot continue is interrupted, as an unheld run is at restart.
|
|
294
491
|
*/
|
|
295
492
|
async restoreRecovered() {
|
|
493
|
+
try {
|
|
494
|
+
await this.restoreRecoveredSessions();
|
|
495
|
+
}
|
|
496
|
+
finally {
|
|
497
|
+
this.restored = true;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
/** True until `restoreRecovered` settles when startup recovery found runs to continue or sessions to restore. */
|
|
501
|
+
get restoring() {
|
|
502
|
+
return !this.restored && ((this.recovery?.continueRuns.length ?? 0) + (this.recovery?.restoreSessions.length ?? 0)) > 0;
|
|
503
|
+
}
|
|
504
|
+
async restoreRecoveredSessions() {
|
|
505
|
+
for (const record of this.recovery?.continueRuns ?? []) {
|
|
506
|
+
try {
|
|
507
|
+
if (this.stopping || this.draining || this.failure)
|
|
508
|
+
throw new Error('The daemon stopped before the held run continued.');
|
|
509
|
+
// A continued subagent delivers its completion to its parent, so the parent chain must be resident.
|
|
510
|
+
for (const ancestor of this.ancestors(record.session))
|
|
511
|
+
await this.restore(ancestor);
|
|
512
|
+
const execution = this.options.store.sessionMetadata(record.session).execution;
|
|
513
|
+
await this.restore(record.session, execution ? { systemPromptAppend: execution.request.appendSystemPrompt ?? '' } : {});
|
|
514
|
+
const entry = this.require(record.session);
|
|
515
|
+
if (entry.binding.runtime.session.sessionManager.getLeafId() !== record.leaf)
|
|
516
|
+
throw new Error('The transcript changed after the run was held.');
|
|
517
|
+
const work = execution ? this.executionHost(record.session).resumeHeld(record.session, record.turns ?? 0, steer => this.continueFrom(entry, record, steer))
|
|
518
|
+
: this.continueRoot(record.session, entry, record);
|
|
519
|
+
void work.catch(error => this.diagnostic({ scope: 'registry', session: record.session, error }));
|
|
520
|
+
}
|
|
521
|
+
catch (error) {
|
|
522
|
+
this.continuing.delete(record.session);
|
|
523
|
+
this.diagnostic({ scope: 'registry', session: record.session, error });
|
|
524
|
+
try {
|
|
525
|
+
if (this.entries.has(record.session))
|
|
526
|
+
this.interruptRun(record.session);
|
|
527
|
+
else if (this.options.store.findSessionMetadata(record.session))
|
|
528
|
+
this.options.store.interruptSessionRun(record.session, record.run.id);
|
|
529
|
+
if (this.options.store.findSessionMetadata(record.session)?.execution)
|
|
530
|
+
this.executionHost(record.session).abandonHeld(record.session);
|
|
531
|
+
}
|
|
532
|
+
catch (cleanup) {
|
|
533
|
+
this.diagnostic({ scope: 'registry', session: record.session, error: cleanup });
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
296
537
|
for (const id of this.recovery?.restoreSessions ?? []) {
|
|
297
538
|
if (this.stopping || this.draining || this.failure)
|
|
298
539
|
return;
|
|
@@ -307,6 +548,72 @@ export class SessionRegistry {
|
|
|
307
548
|
}
|
|
308
549
|
}
|
|
309
550
|
}
|
|
551
|
+
/** The session's ancestors, root first. */
|
|
552
|
+
ancestors(id) {
|
|
553
|
+
const chain = [];
|
|
554
|
+
for (let parent = this.options.store.sessionMetadata(id).parent; parent; parent = this.options.store.sessionMetadata(parent).parent)
|
|
555
|
+
chain.unshift(parent);
|
|
556
|
+
return chain;
|
|
557
|
+
}
|
|
558
|
+
/** Continues a held root run as a daemon run under its original run ID. */
|
|
559
|
+
async continueRoot(id, entry, record) {
|
|
560
|
+
const origin = record.run.origin ?? { kind: 'automation', source: 'restart' };
|
|
561
|
+
const key = `hold:${record.run.id}`;
|
|
562
|
+
let finish;
|
|
563
|
+
const writer = new Promise(resolve => { finish = resolve; });
|
|
564
|
+
entry.writers.add(writer);
|
|
565
|
+
entry.dispatches.add(key);
|
|
566
|
+
entry.projection.setOrigin(origin);
|
|
567
|
+
this.continuing.delete(id);
|
|
568
|
+
let failed = false;
|
|
569
|
+
try {
|
|
570
|
+
entry.projection.syncCursor();
|
|
571
|
+
await this.executeRun(id, entry, () => this.continueFrom(entry, record)).catch((error) => {
|
|
572
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
573
|
+
return;
|
|
574
|
+
failed = true;
|
|
575
|
+
throw error;
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
finally {
|
|
579
|
+
try {
|
|
580
|
+
entry.dispatches.delete(key);
|
|
581
|
+
if (!this.failure && !entry.projectionFailed) {
|
|
582
|
+
// Nobody watched the continued run fail, so it reads as a restart interruption rather than a finished run.
|
|
583
|
+
if (failed && !entry.cancelling)
|
|
584
|
+
this.interruptRun(id);
|
|
585
|
+
else if (!entry.cancelling && !entry.held)
|
|
586
|
+
entry.projection.settleRun(record.run.id);
|
|
587
|
+
entry.projection.refresh();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
catch (error) {
|
|
591
|
+
this.projectionFailed(id, error);
|
|
592
|
+
throw error;
|
|
593
|
+
}
|
|
594
|
+
finally {
|
|
595
|
+
entry.writers.delete(writer);
|
|
596
|
+
finish();
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
/** Re-queues the held run's user input and then `steer`, and starts the next step through Pi's normal run start. */
|
|
601
|
+
async continueFrom(entry, record, steer) {
|
|
602
|
+
this.continuing.delete(entry.binding.sessionId);
|
|
603
|
+
const session = entry.binding.runtime.session;
|
|
604
|
+
for (const item of record.queue) {
|
|
605
|
+
await entry.projection.withQueueOrigin(item.origin, async () => {
|
|
606
|
+
if (item.kind === 'steer')
|
|
607
|
+
session.agent.steer(item.message);
|
|
608
|
+
else
|
|
609
|
+
session.agent.followUp(item.message);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
if (steer)
|
|
613
|
+
await session.steer(steer);
|
|
614
|
+
entry.projection.clearInterruptedQueue();
|
|
615
|
+
await session.sendCustomMessage({ customType: RUN_CONTINUED_MESSAGE_TYPE, content: RUN_CONTINUED_TEXT, display: false }, { triggerTurn: true });
|
|
616
|
+
}
|
|
310
617
|
restore(id, context = {}) {
|
|
311
618
|
this.assertAdmission();
|
|
312
619
|
const stored = this.options.store.findSessionMetadata(id);
|
|
@@ -544,7 +851,7 @@ export class SessionRegistry {
|
|
|
544
851
|
}
|
|
545
852
|
assertRunAvailable(id) {
|
|
546
853
|
const stored = this.options.store.sessionMetadata(id);
|
|
547
|
-
if (stored.state !== 'open' || stored.execution && !stored.execution.completedAt || this.closing.has(id) || this.copyClaims.has(stored.piPath) || this.running(id))
|
|
854
|
+
if (stored.state !== 'open' || stored.execution && !stored.execution.completedAt || this.closing.has(id) || this.copyClaims.has(stored.piPath) || this.running(id) || this.continuing.has(id))
|
|
548
855
|
throw registryError('busy', 'Wait for the current run to finish.');
|
|
549
856
|
}
|
|
550
857
|
async runAdmitted(requestId, id, origin, effect) {
|
|
@@ -576,7 +883,7 @@ export class SessionRegistry {
|
|
|
576
883
|
if (entry.command?.runId === run.id)
|
|
577
884
|
delete entry.command;
|
|
578
885
|
if (!this.failure && !entry.projectionFailed) {
|
|
579
|
-
if (!entry.cancelling)
|
|
886
|
+
if (!entry.cancelling && !entry.held)
|
|
580
887
|
entry.projection.settleRun(run.id);
|
|
581
888
|
entry.projection.refresh();
|
|
582
889
|
}
|
|
@@ -622,7 +929,7 @@ export class SessionRegistry {
|
|
|
622
929
|
try {
|
|
623
930
|
entry.dispatches.delete(continuationId);
|
|
624
931
|
if (!this.failure && !entry.projectionFailed) {
|
|
625
|
-
if (!entry.cancelling)
|
|
932
|
+
if (!entry.cancelling && !entry.held)
|
|
626
933
|
entry.projection.settleRun(run.id);
|
|
627
934
|
entry.projection.refresh();
|
|
628
935
|
}
|
|
@@ -963,6 +1270,7 @@ export class SessionRegistry {
|
|
|
963
1270
|
return entry.cancelling;
|
|
964
1271
|
if (['idle', 'interrupted'].includes(stored.run.state) && entry.binding.runtime.session.isIdle)
|
|
965
1272
|
return;
|
|
1273
|
+
this.dropHeld(id);
|
|
966
1274
|
entry.runAbort?.abort();
|
|
967
1275
|
const operation = Promise.resolve().then(async () => {
|
|
968
1276
|
if (!entry.projectionFailed)
|
|
@@ -1109,6 +1417,7 @@ export class SessionRegistry {
|
|
|
1109
1417
|
const descendants = this.list().filter(item => item.parent === id && item.state === 'open');
|
|
1110
1418
|
// Host shutdown cancels its full descendant tree and waits beyond terminal status records.
|
|
1111
1419
|
const entry = this.entries.get(id);
|
|
1420
|
+
this.dropHeld(id);
|
|
1112
1421
|
this.settleInteractions(id, 'close');
|
|
1113
1422
|
if (entry && !entry.projectionFailed)
|
|
1114
1423
|
await this.cancelViews(id);
|
|
@@ -1141,10 +1450,12 @@ export class SessionRegistry {
|
|
|
1141
1450
|
this.options.onShutdown?.();
|
|
1142
1451
|
if (options.interrupt)
|
|
1143
1452
|
for (const row of this.list()) {
|
|
1144
|
-
if (row.state === 'open' && ['running', 'waiting', 'cancelling'].includes(row.run.state) && row.run.id)
|
|
1453
|
+
if (row.state === 'open' && ['running', 'waiting', 'cancelling'].includes(row.run.state) && row.run.id && !this.isHeld(row.id))
|
|
1145
1454
|
this.shutdownInterruptions.set(row.id, row.run.id);
|
|
1146
1455
|
}
|
|
1147
1456
|
this.stopping = true;
|
|
1457
|
+
// Held runs end before anything waits on them: their continue records already describe how the next daemon resumes them.
|
|
1458
|
+
await this.join([...this.entries.values()].filter(entry => entry.held).map(entry => shutdownStep('session-abort', () => entry.binding.runtime.session.abort(), entry.binding.sessionId)));
|
|
1148
1459
|
if (this.failure) {
|
|
1149
1460
|
await shutdownStep('registry-fenced', async () => { await this.failureSettlement; throw new Error('Registry is fenced; verify settlement before releasing ownership.'); });
|
|
1150
1461
|
}
|
|
@@ -1200,7 +1511,7 @@ export class SessionRegistry {
|
|
|
1200
1511
|
}
|
|
1201
1512
|
await shutdownStep('session-writers', async () => { await entry.cancelling; await this.join([...entry.writers]); }, id);
|
|
1202
1513
|
}
|
|
1203
|
-
if (!entry.projectionFailed)
|
|
1514
|
+
if (!entry.projectionFailed && !entry.held)
|
|
1204
1515
|
await shutdownStep('session-run', () => entry.projection.settleRun(this.options.store.sessionMetadata(id).run.id), id);
|
|
1205
1516
|
await shutdownStep('session-dispose', () => this.disposeEntry(id, false), id);
|
|
1206
1517
|
}
|
|
@@ -1396,7 +1707,7 @@ export class SessionRegistry {
|
|
|
1396
1707
|
const projection = new SessionProjection({ binding, store: this.options.store, extras: () => this.extras(binding.sessionId), publish: event => this.publish(event), onError: error => { this.projectionFailed(binding.sessionId, error); },
|
|
1397
1708
|
commandAvailable: name => this.options.commandAvailable?.(binding.sessionId, name) === false ? false
|
|
1398
1709
|
: this.commandDispatcher?.(binding.sessionId, name) ?? this.options.commandAvailable?.(binding.sessionId, name) ?? false,
|
|
1399
|
-
canSettle: () => { const current = this.entries.get(binding.sessionId); return !current?.dispatches.size && !current?.cancelling && !current?.projectionFailed; },
|
|
1710
|
+
canSettle: () => { const current = this.entries.get(binding.sessionId); return !current?.dispatches.size && !current?.cancelling && !current?.projectionFailed && !current?.held; },
|
|
1400
1711
|
onRefresh: () => {
|
|
1401
1712
|
const current = this.entries.get(binding.sessionId);
|
|
1402
1713
|
if (current)
|
|
@@ -1406,6 +1717,12 @@ export class SessionRegistry {
|
|
|
1406
1717
|
const entry = { binding, projection, attachments: new Map(), dispatches: new Set(), writers: new Set(), services: this.retainedServices.get(binding.sessionId) ?? new Set(), lastDetached: this.now() };
|
|
1407
1718
|
this.retainedServices.delete(binding.sessionId);
|
|
1408
1719
|
this.entries.set(binding.sessionId, entry);
|
|
1720
|
+
installStepHold(binding.runtime.session, {
|
|
1721
|
+
active: () => this.stepHold !== undefined,
|
|
1722
|
+
parked: step => this.parked(binding.sessionId, entry, step),
|
|
1723
|
+
released: step => { if (entry.parked === step)
|
|
1724
|
+
delete entry.parked; this.stepHold?.changed(); },
|
|
1725
|
+
});
|
|
1409
1726
|
const host = this.executionHost(binding.sessionId);
|
|
1410
1727
|
host.activate();
|
|
1411
1728
|
entry.host = host;
|
|
@@ -1668,6 +1985,13 @@ export class SessionRegistry {
|
|
|
1668
1985
|
this.publish({ v: 1, session: id, rev: commit.value.revision,
|
|
1669
1986
|
change: { type: 'metadata.updated', title: commit.value.title, state: commit.value.state, residency: commit.value.residency } });
|
|
1670
1987
|
}
|
|
1988
|
+
else if (entry.held && !closed) {
|
|
1989
|
+
this.settleHeldLeaf(id, entry);
|
|
1990
|
+
const commit = this.options.store.evictHeldSession(id);
|
|
1991
|
+
if (commit.revisions[id] !== undefined)
|
|
1992
|
+
this.publish({ v: 1, session: id, rev: commit.value.revision,
|
|
1993
|
+
change: { type: 'metadata.updated', title: commit.value.title, state: commit.value.state, residency: commit.value.residency } });
|
|
1994
|
+
}
|
|
1671
1995
|
else
|
|
1672
1996
|
entry.projection.transitionForSafety(closed ? 'closed' : 'evicted');
|
|
1673
1997
|
entry.projection.dispose();
|
|
@@ -1688,7 +2012,32 @@ export class SessionRegistry {
|
|
|
1688
2012
|
throw error;
|
|
1689
2013
|
}
|
|
1690
2014
|
}
|
|
2015
|
+
/**
|
|
2016
|
+
* Ending a held run makes Pi append what it kept for the step's end, such as context-only custom messages and user bash
|
|
2017
|
+
* output. When the transcript gained only such entries after the parked leaf, the record moves to the final leaf, so the next
|
|
2018
|
+
* daemon still continues the run. Anything else keeps the parked leaf, and the next daemon interrupts the run.
|
|
2019
|
+
*/
|
|
2020
|
+
settleHeldLeaf(id, entry) {
|
|
2021
|
+
const record = entry.held;
|
|
2022
|
+
const manager = entry.binding.runtime.session.sessionManager;
|
|
2023
|
+
const leaf = manager.getLeafId();
|
|
2024
|
+
if (!leaf || leaf === record.leaf)
|
|
2025
|
+
return;
|
|
2026
|
+
const branch = manager.getBranch();
|
|
2027
|
+
const parked = branch.findIndex(item => item.id === record.leaf);
|
|
2028
|
+
if (parked < 0 || !branch.slice(parked + 1).every(contextOnly))
|
|
2029
|
+
return;
|
|
2030
|
+
try {
|
|
2031
|
+
const moved = { ...record, leaf };
|
|
2032
|
+
this.options.store.putHeldRuns([moved]);
|
|
2033
|
+
entry.held = moved;
|
|
2034
|
+
}
|
|
2035
|
+
catch (error) {
|
|
2036
|
+
this.diagnostic({ scope: 'registry', session: id, error });
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
1691
2039
|
interruptRun(id) {
|
|
2040
|
+
this.dropHeld(id);
|
|
1692
2041
|
const run = this.options.store.sessionMetadata(id).run;
|
|
1693
2042
|
if (!run.id || !['running', 'waiting', 'cancelling'].includes(run.state))
|
|
1694
2043
|
return;
|
|
@@ -1916,4 +2265,10 @@ function profileOptions(profile) {
|
|
|
1916
2265
|
}
|
|
1917
2266
|
function directoryOf(path) { return path.slice(0, path.lastIndexOf('/')) || '/'; }
|
|
1918
2267
|
function registryError(code, message) { return Object.assign(new Error(message), { code }); }
|
|
2268
|
+
/** Entries that add context or metadata without a model turn, so a continued run may follow them. */
|
|
2269
|
+
function contextOnly(entry) {
|
|
2270
|
+
if (entry.type === 'message')
|
|
2271
|
+
return entry.message.role === 'bashExecution';
|
|
2272
|
+
return entry.type === 'custom_message' || entry.type === 'custom' || entry.type === 'label' || entry.type === 'session_info';
|
|
2273
|
+
}
|
|
1919
2274
|
//# sourceMappingURL=registry.js.map
|