pi-durable-subagents 1.0.33 → 1.0.37
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/CHANGELOG.md +54 -0
- package/README.md +97 -8
- package/dist/agent/main/tool.js +26 -1
- package/dist/agent/main.js +6 -8
- package/dist/cli/control.js +6 -6
- package/dist/cli/doctor.js +20 -1
- package/dist/cli/main.js +15 -9
- package/dist/cli/requests.js +9 -10
- package/dist/cli/restart.js +2 -1
- package/dist/cli/service.js +6 -4
- package/dist/compat/model.js +12 -6
- package/dist/events/derive.js +13 -4
- package/dist/events/waiting.js +36 -13
- package/dist/kernel/mailbox.js +2 -0
- package/dist/orchestrator/config.js +18 -3
- package/dist/orchestrator/engine.js +77 -20
- package/dist/orchestrator/executor/index.js +248 -66
- package/dist/orchestrator/ledger.js +6 -3
- package/dist/orchestrator/main.js +24 -9
- package/dist/orchestrator/notes.js +27 -0
- package/dist/orchestrator/snapshot.js +67 -17
- package/dist/platform/launch.js +127 -0
- package/dist/platform/lease.js +58 -14
- package/dist/platform/proctable.js +6 -3
- package/dist/ui/cards.js +1 -0
- package/dist/ui/screen.js +4 -3
- package/dist/ui/tool.js +3 -3
- package/dist/ui/view.js +1 -1
- package/package.json +1 -1
package/dist/compat/model.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export const thinkingLevels = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
2
|
+
/** The model without scheduling fields, as recorded and launched. */
|
|
3
|
+
export const plainModel = (m) => ({ ...(m.provider !== undefined ? { provider: m.provider } : {}), id: m.id, ...(m.thinking ? { thinking: m.thinking } : {}) });
|
|
2
4
|
/** P35: Parse Pi model identifiers, preserving colons that are not thinking suffixes. */
|
|
3
5
|
export function parseModel(value) {
|
|
4
6
|
let id = value.trim();
|
|
@@ -13,17 +15,21 @@ export function parseModel(value) {
|
|
|
13
15
|
throw new Error(`Invalid model: ${value}`);
|
|
14
16
|
return { ...(slash < 0 ? {} : { provider: id.slice(0, slash) }), id: slash < 0 ? id : id.slice(slash + 1), ...(thinking ? { thinking } : {}) };
|
|
15
17
|
}
|
|
16
|
-
/** P11, P35: Resolve a pool into ordered candidates without making an unrecorded selection.
|
|
18
|
+
/** P11, P35: Resolve a pool into ordered candidates without making an unrecorded selection. An object entry's
|
|
19
|
+
* `afterMs` applies to every model it expands to; nested ones keep the larger value. */
|
|
17
20
|
export function resolveModel(value, pools = {}) {
|
|
18
|
-
const expand = (name, seen) => {
|
|
19
|
-
if (!Object.hasOwn(pools, name))
|
|
20
|
-
|
|
21
|
+
const expand = (name, seen, after) => {
|
|
22
|
+
if (!Object.hasOwn(pools, name)) {
|
|
23
|
+
const m = parseModel(name);
|
|
24
|
+
return [after > 0 ? { ...m, afterMs: after } : m];
|
|
25
|
+
}
|
|
21
26
|
if (seen.has(name))
|
|
22
27
|
throw new Error(`Cyclic model pool: ${name}`);
|
|
23
28
|
const entries = pools[name];
|
|
24
29
|
if (!entries.length)
|
|
25
30
|
throw new Error(`Empty model pool: ${name}`);
|
|
26
|
-
return entries.flatMap(entry => expand(entry, new Set([...seen, name]))
|
|
31
|
+
return entries.flatMap(entry => typeof entry === "string" ? expand(entry, new Set([...seen, name]), after)
|
|
32
|
+
: expand(entry.model, new Set([...seen, name]), Math.max(after, entry.afterMs ?? 0)));
|
|
27
33
|
};
|
|
28
|
-
return expand(value.trim(), new Set());
|
|
34
|
+
return expand(value.trim(), new Set(), 0);
|
|
29
35
|
}
|
package/dist/events/derive.js
CHANGED
|
@@ -104,19 +104,28 @@ export function deriveEntry(wid, entries, i, orch, id) {
|
|
|
104
104
|
const parts = callParts(item.call);
|
|
105
105
|
return [{ ...base("asking"), type: "asking", ...onCall(item.call), qid: item.qid, rev: item.rev, question: item.text, to: `${wid}/${parts?.key ?? ""}` }];
|
|
106
106
|
}
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
// `answered` at the binding of an answer when that `answer-bound` is marked `event: true` (written so from 1.0.36 on);
|
|
108
|
+
// otherwise (an older journal) at the question's `attention-resolved{answered}`, as before, so a rebuilt event log
|
|
109
|
+
// keeps the events (and ids) an older version derived.
|
|
110
|
+
if (e.type === "answer-bound" && e.event === true || isEntry(e, JT.attentionResolved)) {
|
|
111
|
+
const bound = e.type === "answer-bound";
|
|
112
|
+
if (!bound && e.resolution !== "answered")
|
|
109
113
|
return [];
|
|
114
|
+
const questionId = bound ? `q:${e.call}:${e.qid}` : e.id;
|
|
110
115
|
let item;
|
|
111
116
|
for (let j = i - 1; j >= 0 && !item; j--) {
|
|
112
117
|
const a = entries[j];
|
|
113
|
-
if (isEntry(a, JT.attention) && a.item.id ===
|
|
118
|
+
if (isEntry(a, JT.attention) && a.item.id === questionId && a.item.rev === e.rev)
|
|
114
119
|
item = a;
|
|
115
120
|
}
|
|
116
121
|
const q = item?.item;
|
|
117
122
|
if (!q || q.kind !== "question" || !q.call || q.qid === undefined)
|
|
118
123
|
return [];
|
|
119
|
-
|
|
124
|
+
// A marked binding reports answered immediately; the resolution after it, or a duplicate resolution, reports nothing.
|
|
125
|
+
if (entries.slice(0, i).some(a => a.type === "answer-bound" && a.event === true && a.call === q.call && a.qid === q.qid && a.rev === q.rev ||
|
|
126
|
+
a.type === JT.attentionResolved && a.id === q.id && a.rev === q.rev && a.resolution === "answered"))
|
|
127
|
+
return [];
|
|
128
|
+
const answer = answerOf(entries, bound ? i + 1 : i, before(orch, e), q.call, q.qid, q.rev);
|
|
120
129
|
return [{ ...base("answered"), type: "answered", ...onCall(q.call), qid: q.qid, rev: q.rev, ...answeredBy(answer.request),
|
|
121
130
|
digest: createHash("sha256").update(answer.text, "utf8").digest("hex"), length: answer.text.length }];
|
|
122
131
|
}
|
package/dist/events/waiting.js
CHANGED
|
@@ -19,13 +19,12 @@
|
|
|
19
19
|
// silent an open `stall:`/`noprogress:` attention item (kind stall; its text names the running command) of
|
|
20
20
|
// the current execution while it works (selected, not fenced): a drained call is not waiting.
|
|
21
21
|
// An asking call is not waiting, a sealed one never. Asking = an open question raised in the call's current execution
|
|
22
|
-
// (the call's latest `exec` before the question's attention entry is its current one), hibernated or not
|
|
23
|
-
// answer
|
|
24
|
-
// the new execution reads it (describe's `state: "asking"` still follows the open item).
|
|
22
|
+
// (the call's latest `exec` before the question's attention entry is its current one), hibernated or not. Binding an
|
|
23
|
+
// answer closes that question immediately and queues the call; its waits count before its next execution starts.
|
|
25
24
|
import { JT } from "../types.js";
|
|
26
25
|
import { EVENT_SEQ_SKIP, WAIT_REASONS } from "./types.js";
|
|
27
26
|
import { emptyLedger, foldLedger, settingsOf } from "../orchestrator/ledger.js";
|
|
28
|
-
import { exhaustedLine, slotLine } from "../orchestrator/snapshot.js";
|
|
27
|
+
import { exhaustedLine, heldIn, slotLine } from "../orchestrator/snapshot.js";
|
|
29
28
|
import { parseModel, resolveModel } from "../compat/model.js";
|
|
30
29
|
import { leaseCalls, leaseState } from "../platform/lease.js";
|
|
31
30
|
import { requestId } from "../requests.js";
|
|
@@ -86,10 +85,19 @@ function applyWait(f, e) {
|
|
|
86
85
|
case JT.done:
|
|
87
86
|
f.done = true;
|
|
88
87
|
break;
|
|
89
|
-
case "resumed":
|
|
88
|
+
case "resumed": {
|
|
90
89
|
if (!e.call)
|
|
91
90
|
f.done = false;
|
|
91
|
+
else {
|
|
92
|
+
const c = f.calls.get(String(e.call));
|
|
93
|
+
if (c && (!e.exec || c.exec === e.exec)) {
|
|
94
|
+
c.selected = true;
|
|
95
|
+
c.fenced = false;
|
|
96
|
+
delete c.answered;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
92
99
|
break;
|
|
100
|
+
}
|
|
93
101
|
case "call":
|
|
94
102
|
case "generation": {
|
|
95
103
|
const key = String(e.key), gen = Number(e.gen) || 1, call = `${f.wid}@${f.rev}/${key}@${gen}`, spec = e.spec;
|
|
@@ -104,6 +112,17 @@ function applyWait(f, e) {
|
|
|
104
112
|
c.queued = e.ts;
|
|
105
113
|
c.selected = false;
|
|
106
114
|
c.fenced = false;
|
|
115
|
+
delete c.answered;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "answer-bound": {
|
|
120
|
+
const c = f.calls.get(String(e.call)), id = `q:${e.call}:${e.qid}`, item = f.items.get(id);
|
|
121
|
+
if (item?.rev === e.rev)
|
|
122
|
+
f.items.delete(id);
|
|
123
|
+
if (c) {
|
|
124
|
+
c.answered = String(e.qid);
|
|
125
|
+
c.queued = e.ts;
|
|
107
126
|
}
|
|
108
127
|
break;
|
|
109
128
|
}
|
|
@@ -112,8 +131,10 @@ function applyWait(f, e) {
|
|
|
112
131
|
const c = ofExec(f, e.exec), model = modelName(e.model);
|
|
113
132
|
if (c && model)
|
|
114
133
|
c.model = model;
|
|
115
|
-
if (c && e.type === "selected")
|
|
134
|
+
if (c && e.type === "selected") {
|
|
116
135
|
c.selected = true;
|
|
136
|
+
delete c.answered;
|
|
137
|
+
}
|
|
117
138
|
break;
|
|
118
139
|
}
|
|
119
140
|
case JT.fenced: {
|
|
@@ -191,7 +212,9 @@ catch {
|
|
|
191
212
|
} };
|
|
192
213
|
/** The call's state as `whyWaiting` reads it. `agentModel` names the model of a pinned agent (for a call naming none). */
|
|
193
214
|
export function waitInput(f, c, env, agentModel) {
|
|
194
|
-
|
|
215
|
+
// An answered asker is queued (live) although its hibernated execution is fenced, unless a drain holds its workflow.
|
|
216
|
+
const answered = c.answered !== undefined && !env.held?.(f.wid);
|
|
217
|
+
const settings = settingsOf(env.ledger, env.config ?? {}), live = c.exec && (!c.fenced || answered), selected = c.selected && c.answered === undefined;
|
|
195
218
|
const attention = [];
|
|
196
219
|
let asking = false;
|
|
197
220
|
for (const [id, item] of f.items)
|
|
@@ -201,7 +224,7 @@ export function waitInput(f, c, env, agentModel) {
|
|
|
201
224
|
asking = true;
|
|
202
225
|
}
|
|
203
226
|
let providers = [];
|
|
204
|
-
if (live &&
|
|
227
|
+
if (live && selected) {
|
|
205
228
|
const p = providerOf(c.model);
|
|
206
229
|
if (p)
|
|
207
230
|
providers = [p];
|
|
@@ -222,8 +245,8 @@ export function waitInput(f, c, env, agentModel) {
|
|
|
222
245
|
providers = [p];
|
|
223
246
|
}
|
|
224
247
|
}
|
|
225
|
-
let full = live && !
|
|
226
|
-
const slot = live && !
|
|
248
|
+
let full = live && !selected && providers.length > 0;
|
|
249
|
+
const slot = live && !selected && providers.length ? providers.map(p => {
|
|
227
250
|
let held = 0;
|
|
228
251
|
for (const h of env.ledger.held.values())
|
|
229
252
|
if (h.pool === p)
|
|
@@ -233,10 +256,10 @@ export function waitInput(f, c, env, agentModel) {
|
|
|
233
256
|
full = false;
|
|
234
257
|
return slotLine(p, held, limit);
|
|
235
258
|
}).join(", ") : undefined;
|
|
236
|
-
const lease = live &&
|
|
259
|
+
const lease = live && selected ? env.leases?.get(c.call) : undefined;
|
|
237
260
|
return {
|
|
238
261
|
asking,
|
|
239
|
-
...(live ? { exec: { id: c.exec, since: c.queued ?? 0, selected: !!
|
|
262
|
+
...(live ? { exec: { id: c.exec, since: c.queued ?? 0, selected: !!selected } } : {}),
|
|
240
263
|
...(providers.length ? { providers } : {}), exhausted: env.ledger.exhausted,
|
|
241
264
|
...(c.writer ? { writerWait: c.writer } : {}), ...(lease ? { lease } : {}),
|
|
242
265
|
...(attention.length ? { attention } : {}), ...(slot ? { slot } : {}), ...(full ? { full } : {}),
|
|
@@ -385,7 +408,7 @@ export function waitCollector(src) {
|
|
|
385
408
|
foldLedger(ledger, orch);
|
|
386
409
|
foldRuns(orch);
|
|
387
410
|
let leases;
|
|
388
|
-
const env = { now, ledger, ...(src.config ? { config: src.config } : {}), get leases() { return leases ??= leaseWaits((src.leases ?? (() => leaseState(src.home)))(), now); } };
|
|
411
|
+
const env = { now, ledger, held: heldIn(orch), ...(src.config ? { config: src.config } : {}), get leases() { return leases ??= leaseWaits((src.leases ?? (() => leaseState(src.home)))(), now); } };
|
|
389
412
|
const current = new Map(), meta = new Map(), present = new Set();
|
|
390
413
|
for (const wf of src.workflows()) {
|
|
391
414
|
present.add(wf.wid);
|
package/dist/kernel/mailbox.js
CHANGED
|
@@ -136,6 +136,8 @@ export class Outbox {
|
|
|
136
136
|
return structuredClone(req);
|
|
137
137
|
});
|
|
138
138
|
}
|
|
139
|
+
/** The envelope recorded as sent under `rid`, if any. */
|
|
140
|
+
recorded(rid) { const req = this.sent.get(rid); return req && structuredClone(req); }
|
|
139
141
|
/** P38: Re-publish every unresolved envelope under its original identity. */
|
|
140
142
|
republishPending() { return this.serial(async () => { for (const req of this.pending.values())
|
|
141
143
|
await this.publish(req); }); }
|
|
@@ -32,9 +32,24 @@ export function configProblem(raw) {
|
|
|
32
32
|
if (pools !== undefined) {
|
|
33
33
|
if (!record(pools))
|
|
34
34
|
return "pools must map names to model lists";
|
|
35
|
-
for (const [name, list] of Object.entries(pools))
|
|
36
|
-
if (!Array.isArray(list) || !list.length
|
|
37
|
-
return `pools.${name} must be a nonempty list of "provider/id[:thinking]"`;
|
|
35
|
+
for (const [name, list] of Object.entries(pools)) {
|
|
36
|
+
if (!Array.isArray(list) || !list.length)
|
|
37
|
+
return `pools.${name} must be a nonempty list of "provider/id[:thinking]" or { model, afterMs }`;
|
|
38
|
+
for (const [i, m] of list.entries()) {
|
|
39
|
+
if (typeof m === "string" && m)
|
|
40
|
+
continue;
|
|
41
|
+
const at = `pools.${name}[${i}]`;
|
|
42
|
+
if (!record(m))
|
|
43
|
+
return `${at} must be "provider/id[:thinking]" (or a pool name) or { model, afterMs }`;
|
|
44
|
+
if (typeof m.model !== "string" || !m.model)
|
|
45
|
+
return `${at}.model must be "provider/id[:thinking]" or a pool name`;
|
|
46
|
+
if (m.afterMs !== undefined && !(count(m.afterMs) && Number(m.afterMs) > 0))
|
|
47
|
+
return `${at}.afterMs must be a positive number of milliseconds`;
|
|
48
|
+
const extra = Object.keys(m).find(k => k !== "model" && k !== "afterMs");
|
|
49
|
+
if (extra)
|
|
50
|
+
return `${at}.${extra} is not a pool entry key (model, afterMs)`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
38
53
|
}
|
|
39
54
|
if (providers !== undefined) {
|
|
40
55
|
if (!record(providers))
|
|
@@ -33,7 +33,7 @@ import { WaitTracker, waitCollector, startWaiting } from "../events/waiting.js";
|
|
|
33
33
|
import { readPage } from "../events/log.js";
|
|
34
34
|
import { eventsLog } from "../paths.js";
|
|
35
35
|
import { parseModel } from "../compat/model.js";
|
|
36
|
-
import { PENDING_NOTE, pendingNotes,
|
|
36
|
+
import { PENDING_NOTE, generationRids, openingMessage, pendingFollowUps, pendingNotes, unsettledForward, unsettledNotify } from "./notes.js";
|
|
37
37
|
import { journalAppends } from "../kernel/journal.js";
|
|
38
38
|
import { STATS_EVERY_MS, readBytes, writeStats } from "./stats.js";
|
|
39
39
|
const tail = (text, n) => text.length > n ? `…${text.slice(-(n - 1))}` : text;
|
|
@@ -99,6 +99,10 @@ export function finishedText(wid, entries, call) {
|
|
|
99
99
|
return `${prefix}${lines.join('')}${footer}`;
|
|
100
100
|
}
|
|
101
101
|
/** A1, P2, P10, P11: Serialize decisions while executions run independently. */
|
|
102
|
+
/** A key's latest call in the current revision: its first call or its newest generation. */
|
|
103
|
+
function latestCall(wf, key) {
|
|
104
|
+
return revisionEntries(wf).findLast(e => (e.type === 'call' || e.type === 'generation') && String(e.key) === key);
|
|
105
|
+
}
|
|
102
106
|
export class Engine {
|
|
103
107
|
store;
|
|
104
108
|
/** The cross-workflow event log's writer (derives milestones from the journals; waiting/moving emits through it). */
|
|
@@ -114,6 +118,8 @@ export class Engine {
|
|
|
114
118
|
closed = false;
|
|
115
119
|
restarting = false;
|
|
116
120
|
generations = new Set();
|
|
121
|
+
/** The tail of each workflow's generation openings, see serialOpen. */
|
|
122
|
+
opening = new Map();
|
|
117
123
|
// wid -> executor runs whose follow-up has not run yet (prune never closes a journal they may still append to).
|
|
118
124
|
running = new Map();
|
|
119
125
|
/** Drain: held by a drain that applies to it (all, its session's origin, or itself) and recorded after its creation. */
|
|
@@ -229,6 +235,7 @@ export class Engine {
|
|
|
229
235
|
await this.attention(wf);
|
|
230
236
|
for (const entry of revisionEntries(wf).filter(e => e.type === 'generation'))
|
|
231
237
|
this.dispatchGeneration(wf, entry);
|
|
238
|
+
await this.reopenForwarded(wf);
|
|
232
239
|
}
|
|
233
240
|
await this.intake();
|
|
234
241
|
for (const wf of this.store.workflows.values())
|
|
@@ -442,7 +449,7 @@ export class Engine {
|
|
|
442
449
|
await this.startWorkflow(wf);
|
|
443
450
|
}
|
|
444
451
|
else if (req.kind === 'send') {
|
|
445
|
-
const existing = [...this.store.workflows.values()].flatMap(wf => wf.journal.entries().filter(e => e.type === 'generation' && e.
|
|
452
|
+
const existing = [...this.store.workflows.values()].flatMap(wf => wf.journal.entries().filter(e => e.type === 'generation' && generationRids(e).includes(req.rid)).map(entry => ({ wf, entry })))[0];
|
|
446
453
|
if (existing) {
|
|
447
454
|
this.dispatchGeneration(existing.wf, existing.entry);
|
|
448
455
|
return { action: 'apply' };
|
|
@@ -468,24 +475,21 @@ export class Engine {
|
|
|
468
475
|
return { action: 'reject', reason: 'unknown-model' };
|
|
469
476
|
}
|
|
470
477
|
}
|
|
471
|
-
if (seal && send.kind === 'follow-up')
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
await this.
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
this.dispatchGeneration(wf, opened);
|
|
487
|
-
return { action: 'apply' };
|
|
488
|
-
}
|
|
478
|
+
if (seal && send.kind === 'follow-up')
|
|
479
|
+
return this.serialOpen(wf.wid, async () => {
|
|
480
|
+
// A generation opened meanwhile (undelivered follow-ups after a seal) is the call this follow-up now reaches.
|
|
481
|
+
if (latestCall(wf, String(entry.key))?.gen !== entry.gen)
|
|
482
|
+
return { action: 'defer' };
|
|
483
|
+
// Pending notes and undelivered follow-ups ride on the opening message, recorded in the same entry (consumed
|
|
484
|
+
// exactly once). A notify or follow-up whose fate the seal has not settled yet may still become one: wait for it.
|
|
485
|
+
if (unsettledForward(revisionEntries(wf), from))
|
|
486
|
+
return { action: 'defer' };
|
|
487
|
+
if (send.model !== undefined)
|
|
488
|
+
await this.note(req.rid, send.model, 'next-generation');
|
|
489
|
+
const opened = await this.openGeneration(wf, entry, from, { rid: req.rid, message: send.message ?? '', ...(send.model !== undefined ? { model: send.model } : {}) });
|
|
490
|
+
this.dispatchGeneration(wf, opened);
|
|
491
|
+
return { action: 'apply' };
|
|
492
|
+
});
|
|
489
493
|
// A follow-up naming a model, queued on unfinished work: the executor records its model request with the message.
|
|
490
494
|
return this.executor.forward(req, this.context(wf, entry));
|
|
491
495
|
}
|
|
@@ -684,6 +688,56 @@ export class Engine {
|
|
|
684
688
|
console.error(`durable-subagents: removal of pruned workflow ${wf.wid} failed, retrying at next start: ${String(error)}`);
|
|
685
689
|
}
|
|
686
690
|
}
|
|
691
|
+
/** Open the next generation of `entry`'s key (its latest call), continuing sealed call `from`. The opening message
|
|
692
|
+
* carries the key's pending notes, then its undelivered follow-ups, then `own` (a follow-up sent to the sealed call;
|
|
693
|
+
* without one the undelivered follow-ups open it by themselves). The latest model named replaces the session's. */
|
|
694
|
+
async openGeneration(wf, entry, from, own) {
|
|
695
|
+
const log = revisionEntries(wf), key = String(entry.key), notes = pendingNotes(log, key), carried = pendingFollowUps(log, key);
|
|
696
|
+
const asked = (rid) => this.ledgers.orch.entries().findLast(e => e.type === 'request' && e.request?.rid === rid)?.request?.body?.model;
|
|
697
|
+
const model = [...carried.map(c => asked(c.rid)), own?.model].filter((m) => typeof m === 'string').at(-1);
|
|
698
|
+
const pools = this.ledgers.config.pools, pool = model !== undefined && pools && Object.hasOwn(pools, model);
|
|
699
|
+
const rid = own?.rid ?? carried[0].rid, follows = carried.map(c => c.rid).filter(r => r !== rid);
|
|
700
|
+
const gen = Math.max(0, ...wf.journal.entries().filter(e => ['call', 'generation'].includes(e.type) && e.key === entry.key).map(e => Number(e.gen))) + 1;
|
|
701
|
+
// A follow-up's model replaces the continued session's for this generation and those continuing it.
|
|
702
|
+
const spec = model !== undefined ? { ...entry.spec, model } : entry.spec;
|
|
703
|
+
const message = openingMessage(notes.map(n => String(n.message)), [...carried.map(c => c.message), own?.message ?? '']);
|
|
704
|
+
return wf.journal.append('generation', { rid, key: entry.key, gen, from, spec, revision: wf.revision, opening: { rid, kind: 'follow-up', message },
|
|
705
|
+
...(notes.length ? { notes: notes.map(n => String(n.rid)) } : {}), ...(follows.length ? { follows } : {}), ...(model !== undefined && !pool ? { model } : {}) });
|
|
706
|
+
}
|
|
707
|
+
/** Follow-ups forwarded into running work whose call sealed before taking them (`undelivered-follow-up`) open the
|
|
708
|
+
* key's next generation, as a follow-up sent after the seal would: once its latest generation has sealed, unless a
|
|
709
|
+
* later follow-up took them along. Idempotent; run after every seal and at start. */
|
|
710
|
+
reopenForwarded(wf) { return this.serialOpen(wf.wid, () => this.reopenForwardedNow(wf)); }
|
|
711
|
+
async reopenForwardedNow(wf) {
|
|
712
|
+
const log = revisionEntries(wf);
|
|
713
|
+
if (!log.some(e => e.type === 'forward-retired' && e.reason === 'undelivered-follow-up'))
|
|
714
|
+
return;
|
|
715
|
+
for (const key of new Set(log.filter(e => e.type === 'call' || e.type === 'generation').map(e => String(e.key)))) {
|
|
716
|
+
if (!pendingFollowUps(revisionEntries(wf), key).length)
|
|
717
|
+
continue;
|
|
718
|
+
const latest = latestCall(wf, key);
|
|
719
|
+
const from = `${wf.wid}@${wf.revision}/${key}@${latest.gen}`, entries = wf.journal.entries();
|
|
720
|
+
if (!entries.some(e => e.type === JT.sealed && e.call === from) || entries.some(e => e.type === 'retired' && e.call === from))
|
|
721
|
+
continue;
|
|
722
|
+
if (unsettledForward(revisionEntries(wf), from))
|
|
723
|
+
continue;
|
|
724
|
+
this.dispatchGeneration(wf, await this.openGeneration(wf, latest, from));
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
/** One generation opening at a time per workflow, so the check that a key's latest call is sealed and unopened and
|
|
728
|
+
* the `generation` it appends are one step. Otherwise the reopen at start, the one after a seal and a follow-up's
|
|
729
|
+
* each see the key unopened and open it twice. */
|
|
730
|
+
async serialOpen(wid, fn) {
|
|
731
|
+
const run = (this.opening.get(wid) ?? Promise.resolve()).then(fn), tail = run.then(() => { }, () => { });
|
|
732
|
+
this.opening.set(wid, tail);
|
|
733
|
+
try {
|
|
734
|
+
return await run;
|
|
735
|
+
}
|
|
736
|
+
finally {
|
|
737
|
+
if (this.opening.get(wid) === tail)
|
|
738
|
+
this.opening.delete(wid);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
687
741
|
dispatchGeneration(wf, entry) {
|
|
688
742
|
const ticket = this.ticket({ wf }, entry), id = ticket.callId;
|
|
689
743
|
if (this.generations.has(id) || this.held(wf.wid) || wf.journal.entries().some(e => e.type === 'retired' && e.call === id))
|
|
@@ -693,6 +747,7 @@ export class Engine {
|
|
|
693
747
|
void this.executor.run(ticket).then(() => this.background(async () => {
|
|
694
748
|
if (!wf.journal.entries().some(e => e.type === JT.sealed && e.call === id))
|
|
695
749
|
throw new Error(`Generation returned without seal: ${id}`);
|
|
750
|
+
await this.reopenForwarded(wf);
|
|
696
751
|
if (!wf.journal.entries().some(e => isEntry(e, JT.attention) && e.item.id === `finished:${id}`))
|
|
697
752
|
await wf.journal.append(JT.attention, { item: { id: `finished:${id}`, rev: 1, kind: 'finished', wid: wf.wid, call: id, text: finishedText(wf.wid, wf.journal.entries(), id), origin: wf.origin } });
|
|
698
753
|
this.generations.delete(id);
|
|
@@ -783,6 +838,8 @@ export class Engine {
|
|
|
783
838
|
const release = this.hold(st.wf.wid);
|
|
784
839
|
void this.executor.run(this.ticket(st, entry)).then(() => this.background(async () => {
|
|
785
840
|
try {
|
|
841
|
+
if (this.states.get(st.wf.wid) === st)
|
|
842
|
+
await this.reopenForwarded(st.wf);
|
|
786
843
|
if (this.states.get(st.wf.wid) !== st || this.terminal(st.wf))
|
|
787
844
|
return;
|
|
788
845
|
const result = this.sealed(st, entry);
|