@nathanstephenson/flow 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/backend/fake/index.js +3 -1
- package/dist/backend/pi/effort-capabilities.js +12 -0
- package/dist/backend/pi/index.js +25 -36
- package/dist/backend/pi/workflow-subagent.js +8 -3
- package/dist/build-id +1 -1
- package/dist/cli/application.js +9 -1
- package/dist/cli/bootstrap.js +258 -22
- package/dist/cli/install-guard.js +52 -9
- package/dist/cli/update.js +30 -13
- package/dist/cli/web-update.js +357 -0
- package/dist/client/model-choices.js +22 -4
- package/dist/client/reduce.js +18 -6
- package/dist/client/status.js +34 -20
- package/dist/client/workflow-effort.js +30 -0
- package/dist/daemon/credential-redaction.js +97 -4
- package/dist/daemon/host.js +341 -74
- package/dist/daemon/release-checker.js +104 -0
- package/dist/daemon/runtime.js +12 -4
- package/dist/daemon/server.js +81 -14
- package/dist/daemon/summariser.js +81 -15
- package/dist/daemon/update-api.js +3 -0
- package/dist/daemon/workflow-executions.js +146 -15
- package/dist/protocol/update.js +1 -0
- package/dist/tui/app.js +104 -10
- package/dist/tui/keys.js +46 -10
- package/dist/tui/render.js +58 -13
- package/dist/workflows/records.js +3 -1
- package/dist/workflows/scheduler.js +15 -1
- package/package.json +1 -1
- package/web/dist/apple-touch-icon.png +0 -0
- package/web/dist/assets/index-CaJfF2tQ.js +170 -0
- package/web/dist/assets/index-DWF4-3Fh.css +2 -0
- package/web/dist/assets/{settings-mcp-CcT0ADJR.js → settings-mcp-COi1p_1o.js} +1 -1
- package/web/dist/assets/{settings-secrets-CK4uF7fd.js → settings-secrets-C599riFl.js} +1 -1
- package/web/dist/assets/settings-workflows-BPjeFoaT.js +23 -0
- package/web/dist/assets/{workflows-D4T4Gy_7.css → workflows-9aO6cptD.css} +1 -1
- package/web/dist/assets/workflows-Bmsrl5kE.js +7 -0
- package/web/dist/assets/workflows-pane-DJ3UwHjd.js +1 -0
- package/web/dist/favicon-16x16.png +0 -0
- package/web/dist/favicon-32x32.png +0 -0
- package/web/dist/favicon.ico +0 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +8 -3
- package/web/dist/safari-pinned-tab.svg +4 -0
- package/web/dist/assets/index-BC0gLS53.js +0 -106
- package/web/dist/assets/index-CCeZYKES.css +0 -2
- package/web/dist/assets/settings-workflows-CrbZF-nM.js +0 -23
- package/web/dist/assets/workflow-editors-naPqAmYO.js +0 -65
- package/web/dist/assets/workflows-hh6ioeEA.js +0 -7
- package/web/dist/assets/workflows-pane-Db-MHf7Z.js +0 -1
package/dist/daemon/host.js
CHANGED
|
@@ -7,14 +7,14 @@ import { stackStatus, actionStackStatus, cleanStack, stackFingerprint, changeSta
|
|
|
7
7
|
import { gitStatus, snapshot, target, branchChanges, canNameBranch, publish } from "./publish.js";
|
|
8
8
|
import { resolveDefaultBackend } from "../protocol/settings.js";
|
|
9
9
|
import { isAttachmentMediaType, mediaTypeOf, MAX_ATTACHMENTS_PER_MESSAGE, MAX_ATTACHMENT_BASE64_BYTES, } from "../protocol/attachments.js";
|
|
10
|
-
import { deriveStatus,
|
|
10
|
+
import { deriveStatus, railGroupRank } from "../client/status.js";
|
|
11
11
|
// `switchBranch` is aliased because this class has a method of that name: the method is the
|
|
12
12
|
// Session Host's refusal-and-record wrapper, and the import is the git invocation it wraps.
|
|
13
13
|
import { createWorktree, head, isClean, isRepository, removeWorktree, switchBranch as gitSwitchBranch, } from "./git.js";
|
|
14
14
|
import { SessionLog } from "./log.js";
|
|
15
15
|
import { probeModels } from "./models.js";
|
|
16
16
|
import { probeSkills } from "./skills.js";
|
|
17
|
-
import { nameInput, summarisePublish, suggestedBranch, SummaryModelSpare
|
|
17
|
+
import { nameInput, summarisePublish, suggestedBranch, SummaryModelSpare } from "./summariser.js";
|
|
18
18
|
/**
|
|
19
19
|
* A command the Session Host will not carry out in the state the thing is in — a turn in flight, a
|
|
20
20
|
* Scope that is not a repository, a checkout git itself refused.
|
|
@@ -325,58 +325,118 @@ export class SessionHost {
|
|
|
325
325
|
statusOf(sessionId) {
|
|
326
326
|
return this.activityOf(this.record(sessionId));
|
|
327
327
|
}
|
|
328
|
-
/**
|
|
329
|
-
* Three `Set.size` reads and a comparison, because this runs for every Agent Session on every
|
|
330
|
-
* `GET /api/sessions` — the same budget `branch` is held on the record to stay inside. Independent
|
|
331
|
-
* workflow activity is a separate scheduler-map lookup when summaries are assembled below.
|
|
332
|
-
*/
|
|
333
328
|
activityOf(record) {
|
|
334
329
|
return deriveStatus({
|
|
335
330
|
lifecycle: record.lifecycle,
|
|
336
331
|
turnInFlight: record.turnInFlight,
|
|
337
|
-
awaiting: record.
|
|
332
|
+
awaiting: hasParentRequest(record.openPermissionAttention)
|
|
333
|
+
|| hasParentRequest(record.openEnquiryAttention),
|
|
338
334
|
});
|
|
339
335
|
}
|
|
336
|
+
/** Record a qualifying event without changing activity, Resting, or Lifecycle. */
|
|
337
|
+
markAttention(record, reason, key, at = new Date().toISOString()) {
|
|
338
|
+
if (record.seenAttentionKeys.has(key)) {
|
|
339
|
+
return { at: record.latestAttention?.at ?? at, version: record.latestAttention?.version ?? record.readAttentionVersion };
|
|
340
|
+
}
|
|
341
|
+
record.seenAttentionKeys.add(key);
|
|
342
|
+
while (record.seenAttentionKeys.size > 256) {
|
|
343
|
+
const oldest = record.seenAttentionKeys.values().next().value;
|
|
344
|
+
if (oldest !== undefined)
|
|
345
|
+
record.seenAttentionKeys.delete(oldest);
|
|
346
|
+
}
|
|
347
|
+
const version = (record.latestAttention?.version ?? record.readAttentionVersion) + 1;
|
|
348
|
+
record.latestAttention = {
|
|
349
|
+
reason,
|
|
350
|
+
at,
|
|
351
|
+
version,
|
|
352
|
+
observedSeq: record.log.lastSeq,
|
|
353
|
+
key,
|
|
354
|
+
};
|
|
355
|
+
if (reason !== "Input needed")
|
|
356
|
+
record.latestOutcome = record.latestAttention;
|
|
357
|
+
return { at, version };
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Machine-wide acknowledgement with a compare-through boundary. Clamping to the version currently
|
|
361
|
+
* known means neither a buggy client nor a delayed request can pre-consume a future event.
|
|
362
|
+
*/
|
|
363
|
+
acknowledge(sessionId, throughVersion) {
|
|
364
|
+
const record = this.record(sessionId);
|
|
365
|
+
if (!Number.isSafeInteger(throughVersion) || throughVersion < 0) {
|
|
366
|
+
throw new CommandRefused("Invalid attention boundary");
|
|
367
|
+
}
|
|
368
|
+
const latest = record.latestAttention?.version ?? 0;
|
|
369
|
+
const next = Math.max(record.readAttentionVersion, Math.min(throughVersion, latest));
|
|
370
|
+
if (next !== record.readAttentionVersion) {
|
|
371
|
+
record.readAttentionVersion = next;
|
|
372
|
+
this.persist(record);
|
|
373
|
+
}
|
|
374
|
+
// An explicit result lets clients distinguish idempotent success from a refused/networked void
|
|
375
|
+
// command and retry the latter without turning repeated observations into a command storm.
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
340
378
|
list() {
|
|
341
379
|
return [...this.sessions.values()]
|
|
342
|
-
.map((record) =>
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
380
|
+
.map((record) => {
|
|
381
|
+
const status = this.activityOf(record);
|
|
382
|
+
const workflowInput = this.workflowOwner?.pendingInput?.(record.id);
|
|
383
|
+
const parentInput = newestInput(record);
|
|
384
|
+
// A stopped Backend Session has no live callback to answer. Adapters normally emit terminal
|
|
385
|
+
// prompt snapshots while stopping; the Lifecycle guard also keeps a stale index from
|
|
386
|
+
// lifting an Ended row back out of Filed away.
|
|
387
|
+
const input = record.lifecycle === "live" ? newestAt(parentInput, workflowInput) : undefined;
|
|
388
|
+
const outcome = record.latestOutcome;
|
|
389
|
+
const unread = outcome !== undefined && outcome.version > record.readAttentionVersion;
|
|
390
|
+
const attention = input
|
|
391
|
+
? {
|
|
392
|
+
group: "needs-input",
|
|
393
|
+
reason: "Input needed",
|
|
394
|
+
at: input.at,
|
|
395
|
+
version: record.latestAttention?.version ?? input.version,
|
|
396
|
+
observedSeq: record.latestAttention?.observedSeq ?? record.log.lastSeq,
|
|
397
|
+
}
|
|
398
|
+
: unread && outcome
|
|
399
|
+
? {
|
|
400
|
+
group: "unread",
|
|
401
|
+
reason: outcome.reason,
|
|
402
|
+
at: outcome.at,
|
|
403
|
+
version: outcome.version,
|
|
404
|
+
observedSeq: outcome.observedSeq,
|
|
405
|
+
}
|
|
406
|
+
: undefined;
|
|
407
|
+
return {
|
|
408
|
+
id: record.id,
|
|
409
|
+
scope: record.scope,
|
|
410
|
+
backend: record.backendName,
|
|
411
|
+
status,
|
|
412
|
+
title: record.title,
|
|
413
|
+
...(record.outputPreview === undefined ? {} : { outputPreview: record.outputPreview }),
|
|
414
|
+
restingAt: record.restingAt,
|
|
415
|
+
activeSubagents: record.openSubagentIds.size,
|
|
416
|
+
activeBackgroundCalls: record.openBackgroundCallIds.size,
|
|
417
|
+
activeWorkflows: this.workflowOwner?.active(record.id) ?? 0,
|
|
418
|
+
...(record.settledAt === undefined ? {} : { settledAt: record.settledAt }),
|
|
419
|
+
...(attention === undefined ? {} : { attention }),
|
|
420
|
+
lastSeq: record.log.lastSeq,
|
|
421
|
+
...(record.capabilities ? { capabilities: record.capabilities } : {}),
|
|
422
|
+
...(record.branch ? { branch: record.branch } : {}),
|
|
423
|
+
...(record.worktree ? { worktree: true } : {}),
|
|
424
|
+
};
|
|
425
|
+
})
|
|
426
|
+
/* Attention groups are newest qualifying event first. Ordinary groups retain Resting/filed
|
|
427
|
+
* ordering, so streaming still cannot move a row. Identity is the deterministic final tie. */
|
|
371
428
|
.sort((left, right) => {
|
|
372
|
-
const band =
|
|
429
|
+
const band = railGroupRank(left) - railGroupRank(right);
|
|
373
430
|
if (band !== 0)
|
|
374
431
|
return band;
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
432
|
+
const attentionAt = left.attention && right.attention
|
|
433
|
+
? right.attention.at.localeCompare(left.attention.at)
|
|
434
|
+
: 0;
|
|
435
|
+
if (attentionAt !== 0)
|
|
436
|
+
return attentionAt;
|
|
378
437
|
const by = (summary) => summary.settledAt ?? summary.restingAt;
|
|
379
|
-
|
|
438
|
+
const recency = by(right).localeCompare(by(left));
|
|
439
|
+
return recency !== 0 ? recency : left.id.localeCompare(right.id);
|
|
380
440
|
});
|
|
381
441
|
}
|
|
382
442
|
/**
|
|
@@ -400,13 +460,21 @@ export class SessionHost {
|
|
|
400
460
|
reviving: undefined,
|
|
401
461
|
lifecycle: lifecycleFrom(meta),
|
|
402
462
|
turnInFlight: false,
|
|
403
|
-
openPermissionIds: new Set(openPermissions(entries).map((open) => open.callId)),
|
|
404
|
-
openEnquiryIds: new Set(openEnquiries(entries).map((open) => open.askId)),
|
|
405
463
|
openSubagentIds: new Set(openSubagents(entries).map((open) => open.subagentId)),
|
|
406
464
|
openBackgroundCallIds: new Set(openBackgroundCalls(entries).map((open) => open.callId)),
|
|
407
465
|
// A meta written before the split has neither, and `updatedAt` is what both used to be.
|
|
408
466
|
restingAt: meta.restingAt ?? meta.updatedAt,
|
|
409
467
|
settledAt: meta.settledAt ?? (lifecycleFrom(meta) === "settled" ? meta.updatedAt : undefined),
|
|
468
|
+
backendEpoch: backendEpoch(entries),
|
|
469
|
+
// No metadata means a legacy record starts read. Open requests are still indexed below and
|
|
470
|
+
// therefore remain Needs input for as long as they are genuinely answerable.
|
|
471
|
+
latestAttention: meta.latestAttention,
|
|
472
|
+
latestOutcome: meta.latestOutcome ?? (meta.latestAttention?.reason !== "Input needed" ? meta.latestAttention : undefined),
|
|
473
|
+
seenAttentionKeys: new Set((meta.seenAttentionKeys ?? (meta.latestAttention ? [meta.latestAttention.key] : [])).slice(-256)),
|
|
474
|
+
readAttentionVersion: meta.readAttentionVersion ?? meta.latestAttention?.version ?? 0,
|
|
475
|
+
outputPreview: meta.outputPreview ?? latestParentOutputPreview(entries),
|
|
476
|
+
openPermissionAttention: openInputAttention(entries, "permission"),
|
|
477
|
+
openEnquiryAttention: openInputAttention(entries, "enquiry"),
|
|
410
478
|
buffered: undefined,
|
|
411
479
|
queue: [],
|
|
412
480
|
title: meta.title,
|
|
@@ -429,6 +497,7 @@ export class SessionHost {
|
|
|
429
497
|
resumeToken: meta.resumeToken,
|
|
430
498
|
modelId: meta.modelId,
|
|
431
499
|
effort: meta.effort,
|
|
500
|
+
initialEffortUnconfirmed: meta.initialEffortUnconfirmed ?? false,
|
|
432
501
|
createdAt: meta.createdAt,
|
|
433
502
|
updatedAt: meta.updatedAt,
|
|
434
503
|
};
|
|
@@ -461,6 +530,8 @@ export class SessionHost {
|
|
|
461
530
|
const scope = worktree?.path ?? options.scope;
|
|
462
531
|
const id = randomUUID();
|
|
463
532
|
const now = new Date().toISOString();
|
|
533
|
+
const modelId = options.modelId ?? this.defaultModel?.(backend.name);
|
|
534
|
+
const effort = options.effort ?? this.defaultEffort?.(backend.name);
|
|
464
535
|
const record = {
|
|
465
536
|
id,
|
|
466
537
|
scope,
|
|
@@ -470,14 +541,20 @@ export class SessionHost {
|
|
|
470
541
|
reviving: undefined,
|
|
471
542
|
lifecycle: "live",
|
|
472
543
|
turnInFlight: false,
|
|
473
|
-
openPermissionIds: new Set(),
|
|
474
|
-
openEnquiryIds: new Set(),
|
|
475
544
|
openSubagentIds: new Set(),
|
|
476
545
|
openBackgroundCallIds: new Set(),
|
|
477
546
|
// A brand new Agent Session is its owner's turn from the moment it exists, which is what puts
|
|
478
547
|
// it at the top of the rail.
|
|
479
548
|
restingAt: now,
|
|
480
549
|
settledAt: undefined,
|
|
550
|
+
backendEpoch: 1,
|
|
551
|
+
latestAttention: undefined,
|
|
552
|
+
latestOutcome: undefined,
|
|
553
|
+
seenAttentionKeys: new Set(),
|
|
554
|
+
readAttentionVersion: 0,
|
|
555
|
+
outputPreview: undefined,
|
|
556
|
+
openPermissionAttention: new Map(),
|
|
557
|
+
openEnquiryAttention: new Map(),
|
|
481
558
|
buffered: undefined,
|
|
482
559
|
queue: [],
|
|
483
560
|
title: scope,
|
|
@@ -500,8 +577,9 @@ export class SessionHost {
|
|
|
500
577
|
* and persisting to `SessionMeta.modelId` is what makes the choice stick for this session's
|
|
501
578
|
* life — the same shape as the Standing Authorisations snapshot in BackendCreateOptions.
|
|
502
579
|
*/
|
|
503
|
-
modelId
|
|
504
|
-
effort
|
|
580
|
+
modelId,
|
|
581
|
+
effort,
|
|
582
|
+
initialEffortUnconfirmed: effort !== undefined,
|
|
505
583
|
createdAt: now,
|
|
506
584
|
updatedAt: now,
|
|
507
585
|
};
|
|
@@ -619,6 +697,7 @@ export class SessionHost {
|
|
|
619
697
|
this.closeOpenPermissions(record, record.log.since(0));
|
|
620
698
|
this.closeOpenSubagents(record, record.log.since(0));
|
|
621
699
|
this.closeOpenBackgroundCalls(record, record.log.since(0));
|
|
700
|
+
record.backendEpoch += 1;
|
|
622
701
|
await this.startBackendSession(record);
|
|
623
702
|
record.lifecycle = "live";
|
|
624
703
|
// Un-settled, so the retention window starts again from the next Settle rather than from the
|
|
@@ -654,7 +733,13 @@ export class SessionHost {
|
|
|
654
733
|
this.touch(record);
|
|
655
734
|
return;
|
|
656
735
|
}
|
|
657
|
-
|
|
736
|
+
try {
|
|
737
|
+
await this.dispatch(record, { text, attachments: ids });
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
this.recordDispatchFailure(record, error);
|
|
741
|
+
throw error;
|
|
742
|
+
}
|
|
658
743
|
}
|
|
659
744
|
/**
|
|
660
745
|
* Whether this send's Attachments can be carried at all, refused before any are written down.
|
|
@@ -729,6 +814,7 @@ export class SessionHost {
|
|
|
729
814
|
const record = this.record(sessionId);
|
|
730
815
|
await record.session?.setModel(modelId);
|
|
731
816
|
record.modelId = modelId;
|
|
817
|
+
record.initialEffortUnconfirmed = false;
|
|
732
818
|
this.touch(record);
|
|
733
819
|
}
|
|
734
820
|
async setEffort(sessionId, effort) {
|
|
@@ -736,6 +822,7 @@ export class SessionHost {
|
|
|
736
822
|
await record.session?.setEffort(effort);
|
|
737
823
|
// Remembered as asked for, not as clamped: a Revive onto a model that can serve it should.
|
|
738
824
|
record.effort = effort;
|
|
825
|
+
record.initialEffortUnconfirmed = false;
|
|
739
826
|
this.touch(record);
|
|
740
827
|
}
|
|
741
828
|
/**
|
|
@@ -1066,28 +1153,52 @@ export class SessionHost {
|
|
|
1066
1153
|
if (pending.sessionId === sessionId)
|
|
1067
1154
|
pending.finish();
|
|
1068
1155
|
}
|
|
1069
|
-
workflowInput(sessionId) {
|
|
1156
|
+
workflowInput(sessionId, requestId) {
|
|
1070
1157
|
if (this.workflowShutdown || this.workflowStopping.has(sessionId))
|
|
1071
1158
|
return;
|
|
1072
1159
|
this.workflowInputs.add(sessionId);
|
|
1160
|
+
const record = this.sessions.get(sessionId);
|
|
1161
|
+
const attention = record && requestId
|
|
1162
|
+
? this.markAttention(record, "Input needed", `workflow-input:${requestId}`)
|
|
1163
|
+
: undefined;
|
|
1164
|
+
if (record && attention)
|
|
1165
|
+
this.persist(record);
|
|
1073
1166
|
this.queueWorkflowDrain(sessionId);
|
|
1167
|
+
return attention;
|
|
1074
1168
|
}
|
|
1075
1169
|
workflowWake(sessionId, executionId, revision) {
|
|
1076
1170
|
if (this.workflowShutdown || this.workflowStopping.has(sessionId))
|
|
1077
1171
|
return;
|
|
1078
1172
|
this.workflowNotifications.set(sessionId, { executionId, revision });
|
|
1173
|
+
const record = this.sessions.get(sessionId);
|
|
1174
|
+
const key = `workflow-failure:${executionId}:${revision}`;
|
|
1175
|
+
if (record && !record.seenAttentionKeys.has(key)) {
|
|
1176
|
+
const boundary = record.log.append({ type: 'notice', level: 'info', text: 'Workflow requires recovery. The parent will inspect it when free.' });
|
|
1177
|
+
this.markAttention(record, 'Failed', key, boundary.at);
|
|
1178
|
+
this.persist(record);
|
|
1179
|
+
}
|
|
1079
1180
|
this.queueWorkflowDrain(sessionId);
|
|
1080
1181
|
}
|
|
1081
1182
|
workflowComplete(sessionId, executionId) {
|
|
1082
1183
|
if (this.workflowShutdown || this.workflowStopping.has(sessionId))
|
|
1083
1184
|
return;
|
|
1084
1185
|
const pending = this.workflowCompletions.get(sessionId) ?? [];
|
|
1085
|
-
if (
|
|
1086
|
-
|
|
1186
|
+
if (pending.includes(executionId))
|
|
1187
|
+
return;
|
|
1188
|
+
pending.push(executionId);
|
|
1087
1189
|
this.workflowCompletions.set(sessionId, pending);
|
|
1190
|
+
const record = this.sessions.get(sessionId);
|
|
1191
|
+
const key = `workflow-completion:${executionId}`;
|
|
1192
|
+
if (record && !record.seenAttentionKeys.has(key)) {
|
|
1193
|
+
const boundary = record.log.append({ type: 'notice', level: 'info', text: 'Workflow completed. The parent will prepare the result when free.' });
|
|
1194
|
+
this.markAttention(record, 'Completed', key, boundary.at);
|
|
1195
|
+
this.persist(record);
|
|
1196
|
+
}
|
|
1088
1197
|
this.queueWorkflowDrain(sessionId);
|
|
1089
1198
|
}
|
|
1090
1199
|
queueWorkflowDrain(sessionId) {
|
|
1200
|
+
// Relay registration and callers' current state transition must finish before takeInput can
|
|
1201
|
+
// consume the wake. The drain still runs at the next microtask, ahead of unrelated timers.
|
|
1091
1202
|
queueMicrotask(() => {
|
|
1092
1203
|
const record = this.sessions.get(sessionId);
|
|
1093
1204
|
if (record)
|
|
@@ -1121,9 +1232,9 @@ export class SessionHost {
|
|
|
1121
1232
|
if (!completions.length)
|
|
1122
1233
|
this.workflowCompletions.delete(record.id);
|
|
1123
1234
|
text = this.workflowOwner?.takeCompletion(record.id, completion);
|
|
1124
|
-
kind =
|
|
1235
|
+
kind = 'completion';
|
|
1125
1236
|
}
|
|
1126
|
-
if (!kind || !text) {
|
|
1237
|
+
if (!kind || (kind !== 'completion' && !text)) {
|
|
1127
1238
|
// A request can be canceled, or a recovery can become stale, before the parent becomes free.
|
|
1128
1239
|
// Continue to the next host-driven item rather than waiting for an unrelated future wake.
|
|
1129
1240
|
if (this.workflowInputs.has(record.id) || this.workflowNotifications.has(record.id) || this.workflowCompletions.has(record.id))
|
|
@@ -1140,6 +1251,10 @@ export class SessionHost {
|
|
|
1140
1251
|
? 'Workflow requires recovery. The parent is inspecting it.'
|
|
1141
1252
|
: 'Workflow completed. The parent is preparing the result.',
|
|
1142
1253
|
});
|
|
1254
|
+
if (!text) {
|
|
1255
|
+
record.turnInFlight = false;
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1143
1258
|
try {
|
|
1144
1259
|
await record.session.prompt(text);
|
|
1145
1260
|
return true;
|
|
@@ -1316,6 +1431,10 @@ export class SessionHost {
|
|
|
1316
1431
|
record.log.append({ type: 'workflow_spend', executionId, spend });
|
|
1317
1432
|
}
|
|
1318
1433
|
authoriseWorkflowTool(tool) { this.allowTool?.(tool); }
|
|
1434
|
+
workflowNamingAllowed(sessionId) {
|
|
1435
|
+
const record = this.sessions.get(sessionId);
|
|
1436
|
+
return !this.workflowShutdown && !this.workflowStopping.has(sessionId) && !!record && record.lifecycle !== "ended" && record.lifecycle !== "settled";
|
|
1437
|
+
}
|
|
1319
1438
|
async dispose(sessionId, reason = "disposed") {
|
|
1320
1439
|
const record = this.sessions.get(sessionId);
|
|
1321
1440
|
if (!record)
|
|
@@ -1328,6 +1447,7 @@ export class SessionHost {
|
|
|
1328
1447
|
record.queue.length = 0;
|
|
1329
1448
|
await this.stopBackendSession(record.scope, session, sessionId);
|
|
1330
1449
|
record.log.append({ type: "session_ended", reason });
|
|
1450
|
+
record.readAttentionVersion = record.latestAttention?.version ?? record.readAttentionVersion;
|
|
1331
1451
|
this.touch(record);
|
|
1332
1452
|
this.announceClosed(sessionId);
|
|
1333
1453
|
}
|
|
@@ -1372,6 +1492,7 @@ export class SessionHost {
|
|
|
1372
1492
|
if (openTurn)
|
|
1373
1493
|
record.log.append({ type: "turn_ended", turnId: openTurn, reason: "aborted" });
|
|
1374
1494
|
record.log.append({ type: "session_settled" });
|
|
1495
|
+
record.readAttentionVersion = record.latestAttention?.version ?? record.readAttentionVersion;
|
|
1375
1496
|
// Stamps updatedAt, which is what starts the retention clock: a Settled Agent Session runs
|
|
1376
1497
|
// nothing and so records no further activity, and it always gets a full window.
|
|
1377
1498
|
this.touch(record);
|
|
@@ -1496,20 +1617,20 @@ export class SessionHost {
|
|
|
1496
1617
|
return name;
|
|
1497
1618
|
}
|
|
1498
1619
|
/**
|
|
1499
|
-
* Automatically name
|
|
1620
|
+
* Automatically name an Agent Session from bounded, redacted Workflow Execution context.
|
|
1500
1621
|
*
|
|
1501
|
-
*
|
|
1502
|
-
*
|
|
1503
|
-
*
|
|
1622
|
+
* The execution owns when its durable one-shot request is consumed. The Session Host owns only
|
|
1623
|
+
* the title race: like first-message naming, this never blocks or fails execution, and only the
|
|
1624
|
+
* untouched Scope placeholder may yield. Chat, manual and summary titles always win.
|
|
1504
1625
|
*/
|
|
1505
|
-
async nameWorkflow(sessionId,
|
|
1626
|
+
async nameWorkflow(sessionId, input) {
|
|
1506
1627
|
const record = this.sessions.get(sessionId);
|
|
1507
1628
|
if (record)
|
|
1508
|
-
await this.nameAutomatically(record,
|
|
1629
|
+
await this.nameAutomatically(record, input, "scope");
|
|
1509
1630
|
}
|
|
1510
1631
|
/** Generate and apply a convenience title only while the placeholder which requested it remains. */
|
|
1511
1632
|
async nameAutomatically(record, input, expectedSource) {
|
|
1512
|
-
if (record.titleSource !== expectedSource)
|
|
1633
|
+
if (!this.workflowNamingAllowed(record.id) || record.titleSource !== expectedSource)
|
|
1513
1634
|
return;
|
|
1514
1635
|
const summary = this.summaryModel?.(record.backendName);
|
|
1515
1636
|
if (!summary?.automatic)
|
|
@@ -1525,7 +1646,7 @@ export class SessionHost {
|
|
|
1525
1646
|
if (name === undefined)
|
|
1526
1647
|
return;
|
|
1527
1648
|
const current = this.sessions.get(record.id);
|
|
1528
|
-
if (!current || current !== record ||
|
|
1649
|
+
if (!current || current !== record || !this.workflowNamingAllowed(record.id))
|
|
1529
1650
|
return;
|
|
1530
1651
|
if (current.titleSource !== expectedSource || current.titleGeneration !== generation)
|
|
1531
1652
|
return;
|
|
@@ -1587,6 +1708,8 @@ export class SessionHost {
|
|
|
1587
1708
|
return await this.dispose(command.sessionId);
|
|
1588
1709
|
case "settle":
|
|
1589
1710
|
return await this.settle(command.sessionId);
|
|
1711
|
+
case "acknowledge":
|
|
1712
|
+
return this.acknowledge(command.sessionId, command.throughVersion);
|
|
1590
1713
|
case "set_model":
|
|
1591
1714
|
return await this.setModel(command.sessionId, command.modelId);
|
|
1592
1715
|
case "set_effort":
|
|
@@ -1901,7 +2024,7 @@ export class SessionHost {
|
|
|
1901
2024
|
for (const open of openEnquiries(entries)) {
|
|
1902
2025
|
record.log.append({ type: "enquiry", ...open, state: "aborted" });
|
|
1903
2026
|
}
|
|
1904
|
-
record.
|
|
2027
|
+
record.openEnquiryAttention.clear();
|
|
1905
2028
|
}
|
|
1906
2029
|
/**
|
|
1907
2030
|
* Record that a Permission Prompt nobody decided is over.
|
|
@@ -1917,12 +2040,30 @@ export class SessionHost {
|
|
|
1917
2040
|
for (const open of openPermissions(entries)) {
|
|
1918
2041
|
record.log.append({ type: "permission", ...open, state: "aborted" });
|
|
1919
2042
|
}
|
|
1920
|
-
record.
|
|
2043
|
+
record.openPermissionAttention.clear();
|
|
1921
2044
|
}
|
|
1922
2045
|
async dispatch(record, message) {
|
|
1923
2046
|
this.refuseGitOperation(record.scope);
|
|
1924
2047
|
if (!record.session)
|
|
1925
2048
|
throw new Error(`Session ${record.id} has no Backend Session`);
|
|
2049
|
+
if (record.session.capabilities.models.length === 0) {
|
|
2050
|
+
throw new CommandRefused("Model capabilities are unavailable. Retry after the Backend Session has discovered its models.");
|
|
2051
|
+
}
|
|
2052
|
+
if (record.initialEffortUnconfirmed && record.effort) {
|
|
2053
|
+
const announced = record.log.since(0).findLast(({ event }) => event.type === "model_changed")?.event;
|
|
2054
|
+
const modelId = announced?.type === "model_changed" ? announced.model.id : record.modelId;
|
|
2055
|
+
const model = record.session.capabilities.models.find(candidate => candidate.id === modelId);
|
|
2056
|
+
if (!model)
|
|
2057
|
+
throw new CommandRefused("The saved Default Effort cannot be checked because the model in force is unknown. Select a model and Effort explicitly.");
|
|
2058
|
+
const levels = model.effortLevels ?? [];
|
|
2059
|
+
const accepted = levels.length > 0 ? levels.includes(record.effort) : record.effort === "off";
|
|
2060
|
+
if (!accepted) {
|
|
2061
|
+
throw new CommandRefused(levels.length > 0
|
|
2062
|
+
? `Saved Default Effort “${record.effort}” is unsupported for ${model.label ?? model.id}. Choose one of ${levels.join(", ")} before sending.`
|
|
2063
|
+
: `Saved Default Effort “${record.effort}” is invalid because ${model.label ?? model.id} has no Effort control. Clear it before sending.`);
|
|
2064
|
+
}
|
|
2065
|
+
record.initialEffortUnconfirmed = false;
|
|
2066
|
+
}
|
|
1926
2067
|
const { text, attachments } = message;
|
|
1927
2068
|
const note = record.pendingBranchNote;
|
|
1928
2069
|
record.pendingBranchNote = undefined;
|
|
@@ -2000,9 +2141,33 @@ export class SessionHost {
|
|
|
2000
2141
|
return;
|
|
2001
2142
|
}
|
|
2002
2143
|
const before = this.activityOf(record);
|
|
2003
|
-
record.
|
|
2144
|
+
const permissionWasOpen = event.type === "permission" && record.openPermissionAttention.has(event.callId);
|
|
2145
|
+
const enquiryWasOpen = event.type === "enquiry" && record.openEnquiryAttention.has(event.askId);
|
|
2146
|
+
const subagentWasOpen = event.type === "subagent" && record.openSubagentIds.has(event.subagentId);
|
|
2147
|
+
const backgroundWasOpen = event.type === "background_call" && record.openBackgroundCallIds.has(event.callId);
|
|
2148
|
+
const logged = record.log.append(event);
|
|
2149
|
+
if (event.type === "message" && event.producer === undefined) {
|
|
2150
|
+
const preview = event.text.replace(/\s+/g, " ").trim();
|
|
2151
|
+
record.outputPreview = preview ? preview.slice(0, 500) : undefined;
|
|
2152
|
+
}
|
|
2153
|
+
let inputAttention;
|
|
2154
|
+
if (event.type === "permission" && event.state === "asked" && !permissionWasOpen) {
|
|
2155
|
+
inputAttention = this.markAttention(record, "Input needed", `backend:${record.backendEpoch}:permission:${event.callId}`, logged.at);
|
|
2156
|
+
}
|
|
2157
|
+
else if (event.type === "enquiry" && event.state === "asked" && !enquiryWasOpen) {
|
|
2158
|
+
inputAttention = this.markAttention(record, "Input needed", `backend:${record.backendEpoch}:enquiry:${event.askId}`, logged.at);
|
|
2159
|
+
}
|
|
2160
|
+
else if (event.type === "turn_ended" && event.reason !== "aborted") {
|
|
2161
|
+
this.markAttention(record, event.reason === "error" ? "Failed" : "Completed", `backend:${record.backendEpoch}:turn:${event.turnId}`, logged.at);
|
|
2162
|
+
}
|
|
2163
|
+
else if (event.type === "subagent" && event.state === "error" && subagentWasOpen) {
|
|
2164
|
+
this.markAttention(record, "Failed", `backend:${record.backendEpoch}:subagent:${event.subagentId}`, logged.at);
|
|
2165
|
+
}
|
|
2166
|
+
else if (event.type === "background_call" && event.state === "error" && backgroundWasOpen) {
|
|
2167
|
+
this.markAttention(record, "Failed", `backend:${record.backendEpoch}:background:${event.callId}`, logged.at);
|
|
2168
|
+
}
|
|
2169
|
+
this.indexOpen(record, event, logged.at, inputAttention);
|
|
2004
2170
|
this.touch(record);
|
|
2005
|
-
this.indexOpen(record, event);
|
|
2006
2171
|
if (event.type === 'notice' && event.level === 'error')
|
|
2007
2172
|
void this.workflowOwner?.stop(sessionId).catch(() => { });
|
|
2008
2173
|
// Kept current so a Revive can hand the running total back to the next Backend Session, which
|
|
@@ -2043,14 +2208,32 @@ export class SessionHost {
|
|
|
2043
2208
|
* Keep the open-prompt and open-Subagent index in step with the transcript.
|
|
2044
2209
|
*
|
|
2045
2210
|
* Only ever called from `onBackendEvent`, which is the one path every adapter event takes. The
|
|
2046
|
-
* host's own terminal snapshots go through `closeOpen*`, which clear these
|
|
2211
|
+
* host's own terminal snapshots go through `closeOpen*`, which clear these indexes themselves.
|
|
2047
2212
|
*/
|
|
2048
|
-
indexOpen(record, event) {
|
|
2213
|
+
indexOpen(record, event, at, inputAttention) {
|
|
2049
2214
|
if (event.type === "permission") {
|
|
2050
|
-
|
|
2215
|
+
if (event.state === "asked") {
|
|
2216
|
+
if (!record.openPermissionAttention.has(event.callId)) {
|
|
2217
|
+
record.openPermissionAttention.set(event.callId, {
|
|
2218
|
+
...(inputAttention ?? { at, version: record.latestAttention?.version ?? 0 }),
|
|
2219
|
+
independent: event.producer !== undefined,
|
|
2220
|
+
});
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
else
|
|
2224
|
+
record.openPermissionAttention.delete(event.callId);
|
|
2051
2225
|
}
|
|
2052
2226
|
if (event.type === "enquiry") {
|
|
2053
|
-
|
|
2227
|
+
if (event.state === "asked") {
|
|
2228
|
+
if (!record.openEnquiryAttention.has(event.askId)) {
|
|
2229
|
+
record.openEnquiryAttention.set(event.askId, {
|
|
2230
|
+
...(inputAttention ?? { at, version: record.latestAttention?.version ?? 0 }),
|
|
2231
|
+
independent: event.producer !== undefined,
|
|
2232
|
+
});
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
else
|
|
2236
|
+
record.openEnquiryAttention.delete(event.askId);
|
|
2054
2237
|
}
|
|
2055
2238
|
if (event.type === "subagent") {
|
|
2056
2239
|
toggle(record.openSubagentIds, event.subagentId, event.state === "running" || event.state === "waiting");
|
|
@@ -2067,8 +2250,12 @@ export class SessionHost {
|
|
|
2067
2250
|
* outlives the turn that started it and reports into a later one (ADR 0016, ADR 0021).
|
|
2068
2251
|
*/
|
|
2069
2252
|
if (event.type === "turn_ended") {
|
|
2070
|
-
record.
|
|
2071
|
-
|
|
2253
|
+
for (const [id, request] of record.openEnquiryAttention)
|
|
2254
|
+
if (!request.independent)
|
|
2255
|
+
record.openEnquiryAttention.delete(id);
|
|
2256
|
+
for (const [id, request] of record.openPermissionAttention)
|
|
2257
|
+
if (!request.independent)
|
|
2258
|
+
record.openPermissionAttention.delete(id);
|
|
2072
2259
|
}
|
|
2073
2260
|
}
|
|
2074
2261
|
/**
|
|
@@ -2098,6 +2285,14 @@ export class SessionHost {
|
|
|
2098
2285
|
this.persist(record);
|
|
2099
2286
|
}
|
|
2100
2287
|
}
|
|
2288
|
+
recordDispatchFailure(record, error) {
|
|
2289
|
+
const before = this.activityOf(record);
|
|
2290
|
+
const failure = record.log.append({ type: "notice", level: "error", text: errorMessage(error) });
|
|
2291
|
+
record.turnInFlight = false;
|
|
2292
|
+
this.markAttention(record, "Failed", `dispatch-failure:${failure.seq}`, failure.at);
|
|
2293
|
+
this.noteResting(record, before);
|
|
2294
|
+
this.touch(record);
|
|
2295
|
+
}
|
|
2101
2296
|
async drain(record) {
|
|
2102
2297
|
// Avoid an `await` at all when there is no workflow notification. Yielding here lets a backend
|
|
2103
2298
|
// mint a new turn between the check and the queue shift, which would dispatch into that turn.
|
|
@@ -2112,7 +2307,7 @@ export class SessionHost {
|
|
|
2112
2307
|
await this.dispatch(record, next);
|
|
2113
2308
|
}
|
|
2114
2309
|
catch (error) {
|
|
2115
|
-
|
|
2310
|
+
this.recordDispatchFailure(record, error);
|
|
2116
2311
|
}
|
|
2117
2312
|
}
|
|
2118
2313
|
newLog(sessionId, existing) {
|
|
@@ -2147,6 +2342,7 @@ export class SessionHost {
|
|
|
2147
2342
|
backend: record.backendName,
|
|
2148
2343
|
title: record.title,
|
|
2149
2344
|
titleSource: record.titleSource,
|
|
2345
|
+
...(record.outputPreview === undefined ? {} : { outputPreview: record.outputPreview }),
|
|
2150
2346
|
createdAt: record.createdAt,
|
|
2151
2347
|
updatedAt: record.updatedAt,
|
|
2152
2348
|
lifecycle: record.lifecycle,
|
|
@@ -2155,10 +2351,15 @@ export class SessionHost {
|
|
|
2155
2351
|
status: record.lifecycle === "live" ? "idle" : record.lifecycle,
|
|
2156
2352
|
restingAt: record.restingAt,
|
|
2157
2353
|
...(record.settledAt === undefined ? {} : { settledAt: record.settledAt }),
|
|
2354
|
+
...(record.latestAttention === undefined ? {} : { latestAttention: record.latestAttention }),
|
|
2355
|
+
...(record.latestOutcome === undefined ? {} : { latestOutcome: record.latestOutcome }),
|
|
2356
|
+
seenAttentionKeys: [...record.seenAttentionKeys],
|
|
2357
|
+
readAttentionVersion: record.readAttentionVersion,
|
|
2158
2358
|
mcpConnectionIds: record.mcpConnectionIds ?? [],
|
|
2159
2359
|
...(record.resumeToken === undefined ? {} : { resumeToken: record.resumeToken }),
|
|
2160
2360
|
...(record.modelId === undefined ? {} : { modelId: record.modelId }),
|
|
2161
2361
|
...(record.effort === undefined ? {} : { effort: record.effort }),
|
|
2362
|
+
...(record.initialEffortUnconfirmed ? { initialEffortUnconfirmed: true } : {}),
|
|
2162
2363
|
...(record.worktree === undefined ? {} : { worktree: record.worktree }),
|
|
2163
2364
|
};
|
|
2164
2365
|
this.store.writeMeta(meta);
|
|
@@ -2205,6 +2406,59 @@ function branchFrom(entries) {
|
|
|
2205
2406
|
* The whole `questions` array comes back with it, because a snapshot carries the whole state and the
|
|
2206
2407
|
* terminal one the host is about to append needs it again.
|
|
2207
2408
|
*/
|
|
2409
|
+
function backendEpoch(entries) {
|
|
2410
|
+
// Adapter-owned IDs are unique only within one Backend Session. Creation contributes the first
|
|
2411
|
+
// epoch and every Revive another, so the same `turn-1` after a restart is still a new outcome.
|
|
2412
|
+
return Math.max(1, entries.filter(({ event }) => event.type === "session_started" || event.type === "revived").length);
|
|
2413
|
+
}
|
|
2414
|
+
function newestAt(left, right) {
|
|
2415
|
+
if (!left)
|
|
2416
|
+
return right;
|
|
2417
|
+
if (!right)
|
|
2418
|
+
return left;
|
|
2419
|
+
return right.at > left.at ? right : left;
|
|
2420
|
+
}
|
|
2421
|
+
function hasParentRequest(requests) {
|
|
2422
|
+
for (const request of requests.values())
|
|
2423
|
+
if (!request.independent)
|
|
2424
|
+
return true;
|
|
2425
|
+
return false;
|
|
2426
|
+
}
|
|
2427
|
+
function newestInput(record) {
|
|
2428
|
+
let newest;
|
|
2429
|
+
for (const input of record.openPermissionAttention.values())
|
|
2430
|
+
newest = newestAt(newest, input);
|
|
2431
|
+
for (const input of record.openEnquiryAttention.values())
|
|
2432
|
+
newest = newestAt(newest, input);
|
|
2433
|
+
return newest;
|
|
2434
|
+
}
|
|
2435
|
+
/** Rebuild unresolved request ages for a lenient legacy load. The restart path closes torn ones. */
|
|
2436
|
+
function openInputAttention(entries, type) {
|
|
2437
|
+
const open = new Map();
|
|
2438
|
+
const clearParent = () => {
|
|
2439
|
+
for (const [id, request] of open)
|
|
2440
|
+
if (!request.independent)
|
|
2441
|
+
open.delete(id);
|
|
2442
|
+
};
|
|
2443
|
+
for (const entry of entries) {
|
|
2444
|
+
const event = entry.event;
|
|
2445
|
+
if (type === "permission" && event.type === "permission") {
|
|
2446
|
+
if (event.state === "asked")
|
|
2447
|
+
open.set(event.callId, { at: entry.at, version: 0, independent: event.producer !== undefined });
|
|
2448
|
+
else
|
|
2449
|
+
open.delete(event.callId);
|
|
2450
|
+
}
|
|
2451
|
+
if (type === "enquiry" && event.type === "enquiry") {
|
|
2452
|
+
if (event.state === "asked")
|
|
2453
|
+
open.set(event.askId, { at: entry.at, version: 0, independent: event.producer !== undefined });
|
|
2454
|
+
else
|
|
2455
|
+
open.delete(event.askId);
|
|
2456
|
+
}
|
|
2457
|
+
if (event.type === "turn_ended")
|
|
2458
|
+
clearParent();
|
|
2459
|
+
}
|
|
2460
|
+
return open;
|
|
2461
|
+
}
|
|
2208
2462
|
function toggle(ids, id, open) {
|
|
2209
2463
|
if (open)
|
|
2210
2464
|
ids.add(id);
|
|
@@ -2223,6 +2477,14 @@ function lifecycleFrom(meta) {
|
|
|
2223
2477
|
const stored = meta.lifecycle ?? meta.status;
|
|
2224
2478
|
return stored === "ended" || stored === "settled" || stored === "dormant" ? stored : "dormant";
|
|
2225
2479
|
}
|
|
2480
|
+
/** Backfill the rail preview for metadata written before outputPreview existed. */
|
|
2481
|
+
function latestParentOutputPreview(entries) {
|
|
2482
|
+
const message = entries.findLast(({ event }) => event.type === "message" && event.producer === undefined)?.event;
|
|
2483
|
+
if (message?.type !== "message")
|
|
2484
|
+
return undefined;
|
|
2485
|
+
const preview = message.text.replace(/\s+/g, " ").trim();
|
|
2486
|
+
return preview ? preview.slice(0, 500) : undefined;
|
|
2487
|
+
}
|
|
2226
2488
|
function openEnquiries(entries) {
|
|
2227
2489
|
const open = new Map();
|
|
2228
2490
|
for (const entry of entries) {
|
|
@@ -2230,7 +2492,11 @@ function openEnquiries(entries) {
|
|
|
2230
2492
|
if (event.type !== "enquiry")
|
|
2231
2493
|
continue;
|
|
2232
2494
|
if (event.state === "asked")
|
|
2233
|
-
open.set(event.askId, {
|
|
2495
|
+
open.set(event.askId, {
|
|
2496
|
+
questions: event.questions,
|
|
2497
|
+
...(event.context === undefined ? {} : { context: event.context }),
|
|
2498
|
+
...(event.producer === undefined ? {} : { producer: event.producer }),
|
|
2499
|
+
});
|
|
2234
2500
|
else
|
|
2235
2501
|
open.delete(event.askId);
|
|
2236
2502
|
}
|
|
@@ -2258,6 +2524,7 @@ function openPermissions(entries) {
|
|
|
2258
2524
|
...(event.context === undefined ? {} : { context: event.context }),
|
|
2259
2525
|
...(event.allowAlways === undefined ? {} : { allowAlways: event.allowAlways }),
|
|
2260
2526
|
...(event.authorizationScope === undefined ? {} : { authorizationScope: event.authorizationScope }),
|
|
2527
|
+
...(event.producer === undefined ? {} : { producer: event.producer }),
|
|
2261
2528
|
});
|
|
2262
2529
|
else
|
|
2263
2530
|
open.delete(event.callId);
|