@nathanstephenson/flow 0.1.3 → 0.1.5
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 +46 -2
- package/build/workflow-runtime.cjs +1 -46
- package/dist/backend/claude/index.js +109 -35
- package/dist/backend/claude/permissions.js +26 -15
- package/dist/backend/claude/subagents.js +4 -0
- package/dist/backend/mcp-stdio-supervisor.js +196 -0
- package/dist/backend/mcp.js +146 -51
- package/dist/backend/permission-errors.js +3 -0
- package/dist/backend/pi/agent-permissions.js +74 -0
- package/dist/backend/pi/index.js +29 -9
- package/dist/backend/registry.js +5 -30
- package/dist/backend/worker/assets.js +16 -0
- package/dist/backend/worker/claude-state.js +183 -0
- package/dist/backend/worker/entry.js +186 -0
- package/dist/backend/worker/index.js +257 -0
- package/dist/backend/worker/launcher.js +67 -0
- package/dist/backend/worker/protocol.js +11 -0
- package/dist/backend/worker/rpc.js +108 -0
- package/dist/build-id +1 -1
- package/dist/cli/bootstrap.js +1 -1
- package/dist/cli/main.js +9 -1
- package/dist/client/connection.js +107 -2
- package/dist/client/diff.js +43 -0
- package/dist/client/reduce.js +3 -0
- package/dist/daemon/config-store.js +36 -1
- package/dist/daemon/config.js +54 -18
- package/dist/daemon/host.js +279 -29
- package/dist/daemon/log.js +98 -0
- package/dist/daemon/runtime.js +10 -1
- package/dist/daemon/server.js +67 -1
- package/dist/daemon/workflow-execution-routes.js +1 -1
- package/dist/daemon/workflow-executions.js +63 -25
- package/dist/daemon/workflow-mcp.js +2 -4
- package/dist/daemon/workflow-routes.js +4 -3
- package/dist/isolation/filesystem.js +382 -0
- package/dist/isolation/launcher.js +40 -0
- package/dist/isolation/node-assets.js +84 -0
- package/dist/isolation/support.js +40 -0
- package/dist/isolation/toolchain-assets.js +62 -0
- package/dist/protocol/presentation.js +230 -0
- package/dist/protocol/settings.js +1 -4
- package/dist/workflows/executors.js +118 -62
- package/dist/workflows/graph.js +3 -6
- package/dist/workflows/json-schema.js +8 -3
- package/dist/workflows/mcp.js +15 -1
- package/dist/workflows/runtime-settings.js +7 -7
- package/dist/workflows/runtime.js +1 -34
- package/package.json +1 -1
- package/web/dist/assets/index-2oJWC9JF.css +2 -0
- package/web/dist/assets/index-6BD3ul5N.js +174 -0
- package/web/dist/assets/input-CqyJUgIE.js +6 -0
- package/web/dist/assets/settings-mcp-DOQOULdG.js +1 -0
- package/web/dist/assets/settings-secrets-D3zCO2H1.js +1 -0
- package/web/dist/assets/settings-workflows-BSVIqm4R.js +23 -0
- package/web/dist/assets/switch-ehZe7Kr5.js +1 -0
- package/web/dist/assets/workflows-DC1k1O8g.js +7 -0
- package/web/dist/assets/workflows-pane-B0hL69SD.js +1 -0
- package/web/dist/index.html +4 -4
- package/web/dist/assets/index-BmX_AxCZ.js +0 -170
- package/web/dist/assets/index-DWF4-3Fh.css +0 -2
- package/web/dist/assets/select-6i3wxovH.js +0 -1
- package/web/dist/assets/settings-mcp-CSLXJftV.js +0 -1
- package/web/dist/assets/settings-secrets-DpEwZLL4.js +0 -1
- package/web/dist/assets/settings-workflows-Dt-GaRtX.js +0 -23
- package/web/dist/assets/switch-ftZleVB8.js +0 -6
- package/web/dist/assets/workflows-DDRfFnAE.js +0 -7
- package/web/dist/assets/workflows-pane-g5njqean.js +0 -1
package/dist/daemon/host.js
CHANGED
|
@@ -204,13 +204,17 @@ export class SessionHost {
|
|
|
204
204
|
autoCompaction;
|
|
205
205
|
compactionModel;
|
|
206
206
|
defaultEffort;
|
|
207
|
+
defaultPermissionMode;
|
|
207
208
|
summaryModel;
|
|
208
209
|
closedListeners = new Set();
|
|
210
|
+
backendFailures = new Set();
|
|
209
211
|
keptListeners = new Set();
|
|
212
|
+
filesystemIsolation;
|
|
210
213
|
mcpConnections;
|
|
211
214
|
mcpAuth;
|
|
212
215
|
resolveSecret;
|
|
213
216
|
constructor(options = {}) {
|
|
217
|
+
this.filesystemIsolation = options.filesystemIsolationEnabled;
|
|
214
218
|
this.mcpConnections = options.mcpConnections;
|
|
215
219
|
this.mcpAuth = options.mcpAuth;
|
|
216
220
|
this.resolveSecret = options.resolveSecret;
|
|
@@ -223,6 +227,7 @@ export class SessionHost {
|
|
|
223
227
|
this.autoCompaction = options.autoCompaction;
|
|
224
228
|
this.compactionModel = options.compactionModel;
|
|
225
229
|
this.defaultEffort = options.defaultEffort;
|
|
230
|
+
this.defaultPermissionMode = options.defaultPermissionMode;
|
|
226
231
|
this.summaryModel = options.summaryModel;
|
|
227
232
|
}
|
|
228
233
|
/**
|
|
@@ -411,6 +416,7 @@ export class SessionHost {
|
|
|
411
416
|
scope: record.scope,
|
|
412
417
|
backend: record.backendName,
|
|
413
418
|
status,
|
|
419
|
+
permissionMode: record.permissionMode,
|
|
414
420
|
title: record.title,
|
|
415
421
|
...(record.outputPreview === undefined ? {} : { outputPreview: record.outputPreview }),
|
|
416
422
|
restingAt: record.restingAt,
|
|
@@ -499,6 +505,7 @@ export class SessionHost {
|
|
|
499
505
|
resumeToken: meta.resumeToken,
|
|
500
506
|
modelId: meta.modelId,
|
|
501
507
|
effort: meta.effort,
|
|
508
|
+
permissionMode: meta.permissionMode ?? this.permissionDefault(meta.backend),
|
|
502
509
|
initialEffortUnconfirmed: meta.initialEffortUnconfirmed ?? false,
|
|
503
510
|
createdAt: meta.createdAt,
|
|
504
511
|
updatedAt: meta.updatedAt,
|
|
@@ -507,6 +514,8 @@ export class SessionHost {
|
|
|
507
514
|
// Empties all three sets seeded above: a torn turn's prompts and Subagents are closed here,
|
|
508
515
|
// and nothing can be open on an Agent Session with no Backend Session attached.
|
|
509
516
|
this.closeTornTurn(record, entries);
|
|
517
|
+
if (!meta.permissionMode)
|
|
518
|
+
record.log.append({ type: "permission_mode_changed", mode: record.permissionMode });
|
|
510
519
|
// Dormancy has to be visible to a client reducing the transcript, or a session with nothing
|
|
511
520
|
// running still looks ready to type at. A clean shutdown already recorded it.
|
|
512
521
|
if (record.lifecycle === "dormant" && lastEventType(record.log.since(0)) !== "session_dormant") {
|
|
@@ -525,11 +534,14 @@ export class SessionHost {
|
|
|
525
534
|
if (backendName === undefined)
|
|
526
535
|
throw new Error("No backends available");
|
|
527
536
|
const backend = this.backendFor(backendName);
|
|
537
|
+
// Reject an unsupported policy before creating a worktree or any durable Agent Session state.
|
|
538
|
+
const permissionMode = options.permissionMode ?? this.permissionDefault(backend.name);
|
|
539
|
+
this.validatePermissionMode(backend.name, permissionMode);
|
|
528
540
|
const worktree = options.worktree ? await this.cutWorktree(options.scope, options.worktree) : undefined;
|
|
529
541
|
// The Scope from here down, and for this Agent Session's whole life. Resolved before any record
|
|
530
542
|
// exists so that a `worktree add` which failed leaves nothing persisted pointing at a directory
|
|
531
543
|
// that is not there.
|
|
532
|
-
const scope = worktree?.path ?? options.scope;
|
|
544
|
+
const scope = scopeKey(worktree?.path ?? options.scope);
|
|
533
545
|
const id = randomUUID();
|
|
534
546
|
const now = new Date().toISOString();
|
|
535
547
|
const modelId = options.modelId ?? this.defaultModel?.(backend.name);
|
|
@@ -581,6 +593,7 @@ export class SessionHost {
|
|
|
581
593
|
*/
|
|
582
594
|
modelId,
|
|
583
595
|
effort,
|
|
596
|
+
permissionMode,
|
|
584
597
|
initialEffortUnconfirmed: effort !== undefined,
|
|
585
598
|
createdAt: now,
|
|
586
599
|
updatedAt: now,
|
|
@@ -590,7 +603,7 @@ export class SessionHost {
|
|
|
590
603
|
record.buffered = [];
|
|
591
604
|
let session;
|
|
592
605
|
try {
|
|
593
|
-
session = await this.
|
|
606
|
+
session = await this.startPermissionBackend(record);
|
|
594
607
|
if (session.capabilities.models.length === 0) {
|
|
595
608
|
throw new CommandRefused("Model capabilities are unavailable. Check the backend and try again.");
|
|
596
609
|
}
|
|
@@ -610,6 +623,7 @@ export class SessionHost {
|
|
|
610
623
|
backend: backend.name,
|
|
611
624
|
scope,
|
|
612
625
|
capabilities: session.capabilities,
|
|
626
|
+
permissionMode: record.permissionMode,
|
|
613
627
|
...(worktree === undefined ? {} : { worktree: true }),
|
|
614
628
|
});
|
|
615
629
|
this.flushBuffered(record);
|
|
@@ -696,6 +710,11 @@ export class SessionHost {
|
|
|
696
710
|
throw new Error(`Session ${sessionId} has ended`);
|
|
697
711
|
if (record.reviving)
|
|
698
712
|
return await record.reviving;
|
|
713
|
+
if (record.permissionChanging) {
|
|
714
|
+
await this.waitForPermissionChange(record);
|
|
715
|
+
// Another caller may have Revived or Ended it while the policy transition was in flight.
|
|
716
|
+
return await this.revive(sessionId);
|
|
717
|
+
}
|
|
699
718
|
const reviving = this.reviveOnce(record);
|
|
700
719
|
record.reviving = reviving;
|
|
701
720
|
try {
|
|
@@ -716,7 +735,7 @@ export class SessionHost {
|
|
|
716
735
|
this.closeOpenSubagents(record, record.log.since(0));
|
|
717
736
|
this.closeOpenBackgroundCalls(record, record.log.since(0));
|
|
718
737
|
record.backendEpoch += 1;
|
|
719
|
-
await this.
|
|
738
|
+
await this.startPermissionBackend(record);
|
|
720
739
|
record.lifecycle = "live";
|
|
721
740
|
// Un-settled, so the retention window starts again from the next Settle rather than from the
|
|
722
741
|
// one this Revive just undid.
|
|
@@ -742,6 +761,8 @@ export class SessionHost {
|
|
|
742
761
|
// ADR 0003: the first message revives a Dormant session, so resuming work is one action.
|
|
743
762
|
if (!record.session)
|
|
744
763
|
await this.revive(sessionId);
|
|
764
|
+
if (record.permissionChanging)
|
|
765
|
+
await this.waitForPermissionChange(record);
|
|
745
766
|
// Checked before anything is written, so a refused send leaves no bytes behind.
|
|
746
767
|
this.refuseUnservableAttachments(record, attachments);
|
|
747
768
|
const ids = (attachments ?? []).map((attachment) => this.storeOrThrow().writeAttachment(record.id, attachment.mediaType, attachment.data));
|
|
@@ -751,13 +772,7 @@ export class SessionHost {
|
|
|
751
772
|
this.touch(record);
|
|
752
773
|
return;
|
|
753
774
|
}
|
|
754
|
-
|
|
755
|
-
await this.dispatch(record, { text, attachments: ids });
|
|
756
|
-
}
|
|
757
|
-
catch (error) {
|
|
758
|
-
this.recordDispatchFailure(record, error);
|
|
759
|
-
throw error;
|
|
760
|
-
}
|
|
775
|
+
await this.dispatch(record, { text, attachments: ids });
|
|
761
776
|
}
|
|
762
777
|
/**
|
|
763
778
|
* Whether this send's Attachments can be carried at all, refused before any are written down.
|
|
@@ -835,6 +850,116 @@ export class SessionHost {
|
|
|
835
850
|
record.initialEffortUnconfirmed = false;
|
|
836
851
|
this.touch(record);
|
|
837
852
|
}
|
|
853
|
+
permissionDefault(backend) {
|
|
854
|
+
return this.defaultPermissionMode?.(backend) ?? (backend === "claude" ? "auto" : backend === "pi" ? "always" : "ask");
|
|
855
|
+
}
|
|
856
|
+
validatePermissionMode(backend, mode) {
|
|
857
|
+
if (!["ask", "auto", "always"].includes(mode) || (mode === "auto" && backend !== "claude"))
|
|
858
|
+
throw new CommandRefused(`${backend} does not support ${mode} permissions`);
|
|
859
|
+
}
|
|
860
|
+
async waitForPermissionChange(record) {
|
|
861
|
+
// A failed selection leaves the last confirmed mode in force; it must not discard a send.
|
|
862
|
+
while (record.permissionChanging)
|
|
863
|
+
await record.permissionChanging.catch(() => { });
|
|
864
|
+
}
|
|
865
|
+
async setPermissionMode(sessionId, mode) {
|
|
866
|
+
const record = this.record(sessionId);
|
|
867
|
+
this.validatePermissionMode(record.backendName, mode);
|
|
868
|
+
const previous = record.permissionChanging;
|
|
869
|
+
const reviving = record.reviving;
|
|
870
|
+
// Reserve before yielding, including for a Dormant Agent Session. Capture the existing Revive
|
|
871
|
+
// rather than reading it later: a new Revive waits for this reservation, not vice versa.
|
|
872
|
+
const changing = Promise.resolve().then(async () => {
|
|
873
|
+
await previous?.catch(() => { });
|
|
874
|
+
await reviving;
|
|
875
|
+
await this.setPermissionModeOnce(record, mode);
|
|
876
|
+
});
|
|
877
|
+
record.permissionChanging = changing;
|
|
878
|
+
try {
|
|
879
|
+
await changing;
|
|
880
|
+
}
|
|
881
|
+
finally {
|
|
882
|
+
if (record.permissionChanging === changing) {
|
|
883
|
+
delete record.permissionChanging;
|
|
884
|
+
if (record.session && record.lifecycle === "live" && !record.turnInFlight)
|
|
885
|
+
void this.drain(record);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
async setPermissionModeOnce(record, mode) {
|
|
890
|
+
if (record.lifecycle === "ended" || record.turnInFlight || record.openPermissionAttention.size ||
|
|
891
|
+
openPermissions(record.log.since(0)).length)
|
|
892
|
+
throw new CommandRefused("Finish the turn and decide all Permission Prompts first");
|
|
893
|
+
if (record.permissionMode === mode)
|
|
894
|
+
return;
|
|
895
|
+
const session = record.session;
|
|
896
|
+
const priorMode = record.permissionMode;
|
|
897
|
+
const activityVersion = record.permissionActivityVersion ?? 0;
|
|
898
|
+
let interrupted = false;
|
|
899
|
+
const apply = async (next) => {
|
|
900
|
+
try {
|
|
901
|
+
await session?.setPermissionMode?.(next);
|
|
902
|
+
}
|
|
903
|
+
finally {
|
|
904
|
+
// The host reservation cannot stop an SDK-owned background completion or Subagent prompt.
|
|
905
|
+
// Do not confirm a policy that raced either, even if that work already ended again.
|
|
906
|
+
interrupted = record.session !== session || record.lifecycle === "ended" || record.turnInFlight ||
|
|
907
|
+
record.openPermissionAttention.size > 0 || (record.permissionActivityVersion ?? 0) !== activityVersion;
|
|
908
|
+
if (interrupted) {
|
|
909
|
+
if (session && record.session === session) {
|
|
910
|
+
try {
|
|
911
|
+
await session.setPermissionMode?.(priorMode);
|
|
912
|
+
}
|
|
913
|
+
catch (error) {
|
|
914
|
+
await this.stopUnconfirmedPermissionBackend(record, session, error);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
throw new CommandRefused("Permission mode change interrupted by Backend Session activity; try again when idle");
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
let warning;
|
|
922
|
+
// No optimistic event: a failed SDK transition must not change either client or disk.
|
|
923
|
+
try {
|
|
924
|
+
await apply(mode);
|
|
925
|
+
}
|
|
926
|
+
catch (error) {
|
|
927
|
+
if (interrupted || mode !== "auto" || record.backendName !== "claude")
|
|
928
|
+
throw error;
|
|
929
|
+
await apply("ask");
|
|
930
|
+
mode = "ask";
|
|
931
|
+
warning = `Claude Auto permissions unavailable: ${String(error)}. Using Ask.`;
|
|
932
|
+
}
|
|
933
|
+
record.permissionMode = mode;
|
|
934
|
+
if (warning)
|
|
935
|
+
record.log.append({ type: "notice", level: "warn", text: warning });
|
|
936
|
+
record.log.append({ type: "permission_mode_changed", mode });
|
|
937
|
+
this.touch(record);
|
|
938
|
+
}
|
|
939
|
+
async stopUnconfirmedPermissionBackend(record, session, error) {
|
|
940
|
+
// Rollback itself awaited the SDK: do not stop a replacement, or revive an Ended/Settled record.
|
|
941
|
+
if (record.session !== session)
|
|
942
|
+
return;
|
|
943
|
+
// A rejected rollback leaves SDK policy unknown. Stop rather than display the old confirmed
|
|
944
|
+
// mode over a backend that may still be executing under the unconfirmed one.
|
|
945
|
+
record.session = undefined;
|
|
946
|
+
record.lifecycle = "dormant";
|
|
947
|
+
record.turnInFlight = false;
|
|
948
|
+
record.queue.length = 0;
|
|
949
|
+
try {
|
|
950
|
+
await this.stopBackendSession(record.scope, session, record.id);
|
|
951
|
+
}
|
|
952
|
+
finally {
|
|
953
|
+
// An explicit End/Settle while shutdown awaited must remain the final lifecycle transition.
|
|
954
|
+
if (record.lifecycle === "dormant" && record.session === undefined) {
|
|
955
|
+
this.closeTornTurn(record, record.log.since(0));
|
|
956
|
+
record.log.append({ type: "queue_changed", pending: [] });
|
|
957
|
+
record.log.append({ type: "notice", level: "error", text: `Could not restore permission mode; Backend Session stopped: ${errorMessage(error)}` });
|
|
958
|
+
record.log.append({ type: "session_dormant", reason: "permission mode could not be confirmed" });
|
|
959
|
+
this.touch(record);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
838
963
|
async setEffort(sessionId, effort) {
|
|
839
964
|
const record = this.record(sessionId);
|
|
840
965
|
await record.session?.setEffort(effort);
|
|
@@ -1007,6 +1132,8 @@ export class SessionHost {
|
|
|
1007
1132
|
}
|
|
1008
1133
|
if (!record.session)
|
|
1009
1134
|
await this.revive(sessionId);
|
|
1135
|
+
if (record.permissionChanging)
|
|
1136
|
+
await this.waitForPermissionChange(record);
|
|
1010
1137
|
this.refuseGitOperation(record.scope);
|
|
1011
1138
|
const session = record.session;
|
|
1012
1139
|
if (!session) {
|
|
@@ -1229,7 +1356,7 @@ export class SessionHost {
|
|
|
1229
1356
|
* must not become "after every message that happened to be queued while it ran".
|
|
1230
1357
|
*/
|
|
1231
1358
|
async drainWorkflowNotification(record) {
|
|
1232
|
-
if (record.turnInFlight || !record.session || record.lifecycle !== 'live' || this.workflowShutdown || this.workflowStopping.has(record.id))
|
|
1359
|
+
if (record.permissionChanging || record.turnInFlight || !record.session || record.lifecycle !== 'live' || this.workflowShutdown || this.workflowStopping.has(record.id))
|
|
1233
1360
|
return false;
|
|
1234
1361
|
let kind;
|
|
1235
1362
|
let text;
|
|
@@ -1273,19 +1400,23 @@ export class SessionHost {
|
|
|
1273
1400
|
record.turnInFlight = false;
|
|
1274
1401
|
return false;
|
|
1275
1402
|
}
|
|
1403
|
+
const session = record.session;
|
|
1404
|
+
const generation = record.turnGeneration = (record.turnGeneration ?? 0) + 1;
|
|
1405
|
+
const failed = (error) => {
|
|
1406
|
+
if (record.lifecycle !== 'live' || record.session !== session || record.turnGeneration !== generation)
|
|
1407
|
+
return;
|
|
1408
|
+
if (kind === 'input')
|
|
1409
|
+
this.workflowOwner?.rearmInput?.(record.id);
|
|
1410
|
+
this.recordDispatchFailure(record, new Error(`Could not notify parent: ${errorMessage(error)}`));
|
|
1411
|
+
};
|
|
1276
1412
|
try {
|
|
1277
|
-
|
|
1278
|
-
return true;
|
|
1413
|
+
void session.prompt(text).catch(failed);
|
|
1279
1414
|
}
|
|
1280
1415
|
catch (error) {
|
|
1281
|
-
|
|
1282
|
-
if (kind === 'input')
|
|
1283
|
-
this.workflowOwner?.rearmInput?.(record.id);
|
|
1284
|
-
record.log.append({ type: 'notice', level: 'warn', text: `Could not notify parent: ${errorMessage(error)}` });
|
|
1285
|
-
// The notification was consumed but no turn began. Keep draining in case another workflow
|
|
1286
|
-
// event is ready; returning false when there is not one lets drain() release the Steering Queue.
|
|
1287
|
-
return this.drainWorkflowNotification(record);
|
|
1416
|
+
failed(error);
|
|
1288
1417
|
}
|
|
1418
|
+
// Consumed, not completed: turn events (or the guarded rejection) release subsequent work.
|
|
1419
|
+
return true;
|
|
1289
1420
|
}
|
|
1290
1421
|
/** Confirmation belongs to this exact action, never a standing grant or model assertion. */
|
|
1291
1422
|
confirmWorkflow(sessionId, description, signal) {
|
|
@@ -1426,7 +1557,10 @@ export class SessionHost {
|
|
|
1426
1557
|
const record = this.record(sessionId);
|
|
1427
1558
|
if (record.lifecycle === 'ended')
|
|
1428
1559
|
throw new CommandRefused('Agent Session has ended');
|
|
1429
|
-
|
|
1560
|
+
const scope = scopeKey(record.scope);
|
|
1561
|
+
if (scope !== resolve(record.scope))
|
|
1562
|
+
throw new Error('Scope changed since selection; refusing redirected execution');
|
|
1563
|
+
return { sessionId, backend: record.backendName, scope, projectId: scopeKey(record.worktree?.repo ?? record.scope), session: record.session };
|
|
1430
1564
|
}
|
|
1431
1565
|
assertWorkflowSession(sessionId, session) {
|
|
1432
1566
|
const record = this.record(sessionId);
|
|
@@ -1683,6 +1817,7 @@ export class SessionHost {
|
|
|
1683
1817
|
// that window is a child process nothing is left to dispose. Not awaited, and cannot be: see
|
|
1684
1818
|
// `SummaryModelSpare.dispose`.
|
|
1685
1819
|
this.summarySpare.dispose();
|
|
1820
|
+
await Promise.all(this.backendFailures);
|
|
1686
1821
|
for (const record of this.sessions.values()) {
|
|
1687
1822
|
if (!record.session)
|
|
1688
1823
|
continue;
|
|
@@ -1709,6 +1844,7 @@ export class SessionHost {
|
|
|
1709
1844
|
...(command.backend === undefined ? {} : { backend: command.backend }),
|
|
1710
1845
|
...(command.modelId === undefined ? {} : { modelId: command.modelId }),
|
|
1711
1846
|
...(command.effort === undefined ? {} : { effort: command.effort }),
|
|
1847
|
+
...(command.permissionMode === undefined ? {} : { permissionMode: command.permissionMode }),
|
|
1712
1848
|
...(command.mcpConnectionIds === undefined ? {} : { mcpConnectionIds: command.mcpConnectionIds }),
|
|
1713
1849
|
...(command.worktree === undefined ? {} : { worktree: command.worktree }),
|
|
1714
1850
|
});
|
|
@@ -1730,6 +1866,8 @@ export class SessionHost {
|
|
|
1730
1866
|
return this.acknowledge(command.sessionId, command.throughVersion);
|
|
1731
1867
|
case "set_model":
|
|
1732
1868
|
return await this.setModel(command.sessionId, command.modelId);
|
|
1869
|
+
case "set_permission_mode":
|
|
1870
|
+
return await this.setPermissionMode(command.sessionId, command.mode);
|
|
1733
1871
|
case "set_effort":
|
|
1734
1872
|
return await this.setEffort(command.sessionId, command.effort);
|
|
1735
1873
|
case "pull_branch":
|
|
@@ -1814,13 +1952,17 @@ export class SessionHost {
|
|
|
1814
1952
|
throw new Error('Unknown Agent Session');
|
|
1815
1953
|
return structuredClone((this.mcpConnections?.() ?? []).filter(connection => record.mcpConnectionIds?.includes(connection.id)));
|
|
1816
1954
|
}
|
|
1817
|
-
|
|
1955
|
+
/** Include an SDK-supplied owning root in every local execution's filesystem policy. */
|
|
1956
|
+
filesystemStateRoot() { return this.store?.root; }
|
|
1957
|
+
/** A host without configured Settings keeps the safe, restricted library default. */
|
|
1958
|
+
filesystemIsolationEnabled() { return this.filesystemIsolation?.() ?? true; }
|
|
1959
|
+
async openWorkflowMcp(id, connectionId, isolationEnabled = this.filesystemIsolationEnabled()) {
|
|
1818
1960
|
const connection = this.workflowMcpConnections(id).find(connection => connection.id === connectionId);
|
|
1819
1961
|
if (!connection)
|
|
1820
1962
|
throw new Error('MCP connection is removed or disabled for this Agent Session. Enable it in the Agent Session settings and reconfigure the step.');
|
|
1821
1963
|
const { McpSession } = await import("../backend/mcp.js");
|
|
1822
1964
|
const scope = this.workflowSession(id).scope;
|
|
1823
|
-
return new McpSession([connection], scope, this.mcpAuth ? connection => this.mcpAuth.provider(connection) : undefined, true, this.resolveSecret);
|
|
1965
|
+
return new McpSession([connection], scope, this.mcpAuth ? connection => this.mcpAuth.provider(connection) : undefined, true, this.resolveSecret, undefined, this.filesystemStateRoot(), isolationEnabled);
|
|
1824
1966
|
}
|
|
1825
1967
|
mcpStatus(id) {
|
|
1826
1968
|
const record = this.sessions.get(id);
|
|
@@ -1854,16 +1996,66 @@ export class SessionHost {
|
|
|
1854
1996
|
delete record.mcpReady;
|
|
1855
1997
|
});
|
|
1856
1998
|
}
|
|
1999
|
+
async backendFailed(record, session) {
|
|
2000
|
+
if (record.session !== session)
|
|
2001
|
+
return;
|
|
2002
|
+
this.captureResumeToken(record);
|
|
2003
|
+
record.session = undefined;
|
|
2004
|
+
record.lifecycle = "dormant";
|
|
2005
|
+
record.turnInFlight = false;
|
|
2006
|
+
record.queue.length = 0;
|
|
2007
|
+
try {
|
|
2008
|
+
await this.stopBackendSession(record.scope, session, record.id);
|
|
2009
|
+
}
|
|
2010
|
+
finally {
|
|
2011
|
+
this.closeTornTurn(record, record.log.since(0), "error");
|
|
2012
|
+
const marker = record.log.append({ type: "session_dormant", reason: "backend worker lost" });
|
|
2013
|
+
this.markAttention(record, "Failed", `backend-loss:${record.backendEpoch}`, marker.at);
|
|
2014
|
+
this.touch(record);
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
async startPermissionBackend(record) {
|
|
2018
|
+
try {
|
|
2019
|
+
return await this.startBackendSession(record);
|
|
2020
|
+
}
|
|
2021
|
+
catch (error) {
|
|
2022
|
+
if (record.backendName !== "claude" || record.permissionMode !== "auto" || !(error instanceof (await import("../backend/permission-errors.js")).AutoPermissionUnavailable))
|
|
2023
|
+
throw error;
|
|
2024
|
+
record.permissionMode = "ask";
|
|
2025
|
+
const fallback = [
|
|
2026
|
+
{ type: "notice", level: "warn", text: `Claude Auto permissions unavailable: ${String(error)}. Using Ask.` },
|
|
2027
|
+
{ type: "permission_mode_changed", mode: "ask" },
|
|
2028
|
+
];
|
|
2029
|
+
// Creation must still open with session_started. A Revive already has a transcript: write
|
|
2030
|
+
// its effective policy now, even if the replacement backend fails and the buffer is lost.
|
|
2031
|
+
if (record.log.lastSeq === 0)
|
|
2032
|
+
record.buffered?.push(...fallback);
|
|
2033
|
+
else
|
|
2034
|
+
for (const event of fallback)
|
|
2035
|
+
record.log.append(event);
|
|
2036
|
+
this.persist(record);
|
|
2037
|
+
return await this.startBackendSession(record);
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
1857
2040
|
async startBackendSession(record) {
|
|
2041
|
+
record.endedTurnIds = new Set();
|
|
1858
2042
|
const backend = this.backendFor(record.backendName);
|
|
1859
2043
|
const { McpSession } = await import("../backend/mcp.js");
|
|
1860
|
-
const mcp = new McpSession((this.mcpConnections?.() ?? []).filter((connection) => record.mcpConnectionIds?.includes(connection.id)), record.scope, this.mcpAuth ? (connection) => this.mcpAuth.provider(connection) : undefined, false, this.resolveSecret);
|
|
2044
|
+
const mcp = new McpSession((this.mcpConnections?.() ?? []).filter((connection) => record.mcpConnectionIds?.includes(connection.id)), record.scope, this.mcpAuth ? (connection) => this.mcpAuth.provider(connection) : undefined, false, this.resolveSecret, undefined, this.filesystemStateRoot(), this.filesystemIsolationEnabled());
|
|
1861
2045
|
record.mcp = mcp;
|
|
1862
2046
|
const openingMcp = mcp.open();
|
|
1863
2047
|
const autoCompaction = this.autoCompaction?.(backend.name);
|
|
1864
2048
|
const compactionModelId = this.compactionModel?.(backend.name);
|
|
2049
|
+
let attached;
|
|
1865
2050
|
const session = await backend.create({
|
|
1866
2051
|
mcp,
|
|
2052
|
+
onFailure: () => {
|
|
2053
|
+
if (!attached || record.session !== attached)
|
|
2054
|
+
return;
|
|
2055
|
+
const stopped = this.backendFailed(record, attached).catch(() => { });
|
|
2056
|
+
this.backendFailures.add(stopped);
|
|
2057
|
+
void stopped.finally(() => this.backendFailures.delete(stopped));
|
|
2058
|
+
},
|
|
1867
2059
|
workflow: {
|
|
1868
2060
|
inspect: input => { this.assertWorkflowSession(record.id, session); if (!this.workflowOwner)
|
|
1869
2061
|
throw new Error('Workflow execution is unavailable'); return this.workflowOwner.parent(record.id).inspect(input); },
|
|
@@ -1875,11 +2067,15 @@ export class SessionHost {
|
|
|
1875
2067
|
throw new Error('Workflow execution is unavailable'); return this.workflowOwner.parent(record.id).relayPermission(input, signal); },
|
|
1876
2068
|
},
|
|
1877
2069
|
scope: record.scope,
|
|
1878
|
-
emit: (event) =>
|
|
2070
|
+
emit: (event) => {
|
|
2071
|
+
if (!attached || record.session === attached)
|
|
2072
|
+
this.onBackendEvent(record.id, event);
|
|
2073
|
+
},
|
|
1879
2074
|
...(autoCompaction === undefined ? {} : { autoCompaction }),
|
|
1880
2075
|
...(compactionModelId === undefined ? {} : { compactionModelId }),
|
|
1881
2076
|
...(record.modelId === undefined ? {} : { modelId: record.modelId }),
|
|
1882
2077
|
...(record.effort === undefined ? {} : { effort: record.effort }),
|
|
2078
|
+
permissionMode: record.permissionMode,
|
|
1883
2079
|
...(record.resumeToken === undefined ? {} : { resume: record.resumeToken }),
|
|
1884
2080
|
...(record.spend === undefined ? {} : { priorSpend: record.spend }),
|
|
1885
2081
|
...(this.store ? { stateDir: this.store.backendDir(record.id) } : {}),
|
|
@@ -1889,6 +2085,7 @@ export class SessionHost {
|
|
|
1889
2085
|
? { standingAuthorisations: this.standingAuthorisations() }
|
|
1890
2086
|
: {}),
|
|
1891
2087
|
}).catch(async (error) => { await mcp.dispose(); throw error; });
|
|
2088
|
+
attached = session;
|
|
1892
2089
|
const dispose = session.dispose.bind(session);
|
|
1893
2090
|
let disposed = false;
|
|
1894
2091
|
session.dispose = async () => { disposed = true; try {
|
|
@@ -1983,7 +2180,7 @@ export class SessionHost {
|
|
|
1983
2180
|
* transcript is append-only (ADR 0001), so we record that we now know it ended instead of
|
|
1984
2181
|
* rewriting the turn that never finished.
|
|
1985
2182
|
*/
|
|
1986
|
-
closeTornTurn(record, entries) {
|
|
2183
|
+
closeTornTurn(record, entries, reason = "aborted") {
|
|
1987
2184
|
this.closeOpenSubagents(record, entries);
|
|
1988
2185
|
this.closeOpenBackgroundCalls(record, entries);
|
|
1989
2186
|
// The daemon-restart case for an Enquiry: nothing was in memory to abandon its callback, and the
|
|
@@ -1993,7 +2190,7 @@ export class SessionHost {
|
|
|
1993
2190
|
const openTurn = openTurnId(entries);
|
|
1994
2191
|
if (!openTurn)
|
|
1995
2192
|
return;
|
|
1996
|
-
record.log.append({ type: "turn_ended", turnId: openTurn, reason
|
|
2193
|
+
record.log.append({ type: "turn_ended", turnId: openTurn, reason });
|
|
1997
2194
|
}
|
|
1998
2195
|
/**
|
|
1999
2196
|
* Close every Subagent the transcript still has running.
|
|
@@ -2064,6 +2261,8 @@ export class SessionHost {
|
|
|
2064
2261
|
record.openPermissionAttention.clear();
|
|
2065
2262
|
}
|
|
2066
2263
|
async dispatch(record, message) {
|
|
2264
|
+
if (record.permissionChanging)
|
|
2265
|
+
await this.waitForPermissionChange(record);
|
|
2067
2266
|
this.refuseGitOperation(record.scope);
|
|
2068
2267
|
if (!record.session)
|
|
2069
2268
|
throw new Error(`Session ${record.id} has no Backend Session`);
|
|
@@ -2098,6 +2297,9 @@ export class SessionHost {
|
|
|
2098
2297
|
* the note work: it arrives before the model acts either way.
|
|
2099
2298
|
*/
|
|
2100
2299
|
const sent = note === undefined ? text : `${text}\n\n${note}`;
|
|
2300
|
+
const session = record.session;
|
|
2301
|
+
const startingTurn = !record.turnInFlight;
|
|
2302
|
+
const generation = record.turnGeneration = (record.turnGeneration ?? 0) + (startingTurn ? 1 : 0);
|
|
2101
2303
|
record.turnInFlight = true;
|
|
2102
2304
|
record.log.append({
|
|
2103
2305
|
type: "user_message",
|
|
@@ -2125,7 +2327,20 @@ export class SessionHost {
|
|
|
2125
2327
|
}
|
|
2126
2328
|
this.touch(record);
|
|
2127
2329
|
const workflowContext = this.workflowOwner?.context(record.id);
|
|
2128
|
-
|
|
2330
|
+
// Acceptance is durable now. SDK prompt promises may last for the entire turn (including
|
|
2331
|
+
// human input), so never keep the HTTP command open awaiting one. Outcomes belong to the
|
|
2332
|
+
// Presentation Transcript, not to the response that acknowledged this message.
|
|
2333
|
+
const failed = (error) => {
|
|
2334
|
+
if (record.lifecycle !== "live" || record.session !== session || record.turnGeneration !== generation)
|
|
2335
|
+
return;
|
|
2336
|
+
this.recordDispatchFailure(record, error, startingTurn);
|
|
2337
|
+
};
|
|
2338
|
+
try {
|
|
2339
|
+
void session.prompt(workflowContext ? `${sent}\n\n${workflowContext}` : sent, this.loadAttachments(record.id, attachments)).catch(failed);
|
|
2340
|
+
}
|
|
2341
|
+
catch (error) {
|
|
2342
|
+
failed(error);
|
|
2343
|
+
}
|
|
2129
2344
|
}
|
|
2130
2345
|
/**
|
|
2131
2346
|
* The bytes for a dispatch, base64 for whichever SDK is about to receive them.
|
|
@@ -2161,6 +2376,19 @@ export class SessionHost {
|
|
|
2161
2376
|
record.buffered.push(event);
|
|
2162
2377
|
return;
|
|
2163
2378
|
}
|
|
2379
|
+
if (record.lifecycle === "dormant")
|
|
2380
|
+
return;
|
|
2381
|
+
if (event.type === "turn_ended") {
|
|
2382
|
+
const ended = record.endedTurnIds ??= new Set();
|
|
2383
|
+
if (ended.has(event.turnId))
|
|
2384
|
+
return;
|
|
2385
|
+
// A delayed SDK terminal event after our synthetic end must not end a newer turn,
|
|
2386
|
+
// cancel its human requests or release its Steering Queue a second time.
|
|
2387
|
+
ended.add(event.turnId);
|
|
2388
|
+
}
|
|
2389
|
+
if (event.type === "turn_started" || (event.type === "permission" && event.state === "asked")) {
|
|
2390
|
+
record.permissionActivityVersion = (record.permissionActivityVersion ?? 0) + 1;
|
|
2391
|
+
}
|
|
2164
2392
|
const before = this.activityOf(record);
|
|
2165
2393
|
const permissionWasOpen = event.type === "permission" && record.openPermissionAttention.has(event.callId);
|
|
2166
2394
|
const enquiryWasOpen = event.type === "enquiry" && record.openEnquiryAttention.has(event.askId);
|
|
@@ -2203,9 +2431,12 @@ export class SessionHost {
|
|
|
2203
2431
|
* dispatch into it, which is the very thing holding the turn open used to prevent.
|
|
2204
2432
|
*/
|
|
2205
2433
|
if (event.type === "turn_started") {
|
|
2434
|
+
if (!record.turnInFlight)
|
|
2435
|
+
record.turnGeneration = (record.turnGeneration ?? 0) + 1;
|
|
2206
2436
|
record.turnInFlight = true;
|
|
2207
2437
|
}
|
|
2208
2438
|
if (event.type === "turn_ended") {
|
|
2439
|
+
record.turnGeneration = (record.turnGeneration ?? 0) + 1;
|
|
2209
2440
|
this.workflowOwner?.rearmInput?.(sessionId);
|
|
2210
2441
|
for (const pending of this.workflowConfirmations.values())
|
|
2211
2442
|
if (pending.sessionId === sessionId)
|
|
@@ -2306,15 +2537,33 @@ export class SessionHost {
|
|
|
2306
2537
|
this.persist(record);
|
|
2307
2538
|
}
|
|
2308
2539
|
}
|
|
2309
|
-
recordDispatchFailure(record, error) {
|
|
2540
|
+
recordDispatchFailure(record, error, endTurn = true) {
|
|
2541
|
+
if (record.lifecycle === "dormant")
|
|
2542
|
+
return; // Fatal worker loss already recorded the failure.
|
|
2310
2543
|
const before = this.activityOf(record);
|
|
2311
2544
|
const failure = record.log.append({ type: "notice", level: "error", text: errorMessage(error) });
|
|
2545
|
+
if (!endTurn) {
|
|
2546
|
+
// A rejected steering request does not stop the already-running original prompt.
|
|
2547
|
+
this.markAttention(record, "Failed", `dispatch-failure:${failure.seq}`, failure.at);
|
|
2548
|
+
this.touch(record);
|
|
2549
|
+
return;
|
|
2550
|
+
}
|
|
2551
|
+
const turnId = openTurnId(record.log.since(0));
|
|
2552
|
+
if (turnId) {
|
|
2553
|
+
// A backend can reject without sending turn_ended. Close the visible turn too, otherwise
|
|
2554
|
+
// the host is Idle while the composer remains Running forever.
|
|
2555
|
+
this.onBackendEvent(record.id, { type: "turn_ended", turnId, reason: "error" });
|
|
2556
|
+
return;
|
|
2557
|
+
}
|
|
2312
2558
|
record.turnInFlight = false;
|
|
2313
2559
|
this.markAttention(record, "Failed", `dispatch-failure:${failure.seq}`, failure.at);
|
|
2314
2560
|
this.noteResting(record, before);
|
|
2315
2561
|
this.touch(record);
|
|
2562
|
+
void this.drain(record);
|
|
2316
2563
|
}
|
|
2317
2564
|
async drain(record) {
|
|
2565
|
+
if (record.permissionChanging)
|
|
2566
|
+
return;
|
|
2318
2567
|
// Avoid an `await` at all when there is no workflow notification. Yielding here lets a backend
|
|
2319
2568
|
// mint a new turn between the check and the queue shift, which would dispatch into that turn.
|
|
2320
2569
|
if ((this.workflowInputs.has(record.id) || this.workflowNotifications.has(record.id) || this.workflowCompletions.has(record.id)) &&
|
|
@@ -2380,6 +2629,7 @@ export class SessionHost {
|
|
|
2380
2629
|
...(record.resumeToken === undefined ? {} : { resumeToken: record.resumeToken }),
|
|
2381
2630
|
...(record.modelId === undefined ? {} : { modelId: record.modelId }),
|
|
2382
2631
|
...(record.effort === undefined ? {} : { effort: record.effort }),
|
|
2632
|
+
permissionMode: record.permissionMode,
|
|
2383
2633
|
...(record.initialEffortUnconfirmed ? { initialEffortUnconfirmed: true } : {}),
|
|
2384
2634
|
...(record.worktree === undefined ? {} : { worktree: record.worktree }),
|
|
2385
2635
|
};
|