142
delivery: "steer",
143
});
144
145
+const emptyShellInventory: ReadonlyArray<ProviderReplayEntry> = [
146
+ out("shell.list", "<any>"),
147
+ reply("shell.list", { location: { directory: WORK }, data: [] }),
148
+];
149
+
150
const noOpenRequests: ReadonlyArray<ProviderReplayEntry> = [
151
out("permission.list", { sessionID: SESSION }),
152
replyData("permission.list", []),
322
const resumed = (
323
entries: ReadonlyArray<ProviderReplayEntry>,
324
options?: {
320
- readonly version?: "2.2.5" | "2.2.6";
325
+ readonly version?: "2.2.1" | "2.2.5" | "2.2.6";
326
+ readonly shellRecovery?: ReadonlyArray<ProviderReplayEntry>;
327
+ readonly lastRunOrdinal?: number;
328
readonly goal?: OrchestrationV2ProviderThread["goal"];
329
readonly external?: boolean;
330
readonly supervised?: boolean;
343
out("session.get", { sessionID: SESSION }),
344
replyData("session.get", sessionInfo()),
345
...noOpenRequests,
339
- ...(options?.version !== undefined
346
+ ...(options?.version === "2.2.5" || options?.version === "2.2.6"
347
? [out("rpc.call", "<any>"), reply("rpc.call", { output: null })]
348
: []),
349
+ ...(options?.shellRecovery ??
350
+ (options?.version === undefined
351
+ ? []
352
+ : [
353
+ ...(options.version === "2.2.6"
354
+ ? [out("job.list", { kind: "shell" }), reply("job.list", { data: [] })]
355
+ : emptyShellInventory),
356
+ ])),
357
...withInstructions([
358
...(options?.supervised === true
359
? [
384
const thread = yield* runtime.resumeThread({
385
providerThread: {
386
...providerThread(yield* DateTime.now),
387
+ ...(options?.lastRunOrdinal === undefined
388
+ ? {}
389
+ : { lastRunOrdinal: options.lastRunOrdinal }),
390
...(options?.goal === undefined ? {} : { goal: options.goal }),
391
},
392
threadId,
904
);
905
return { attached: Deferred.await(attached), terminal: Fiber.join(terminal) };
906
});
907
+ it.effect.each([false, true, "thread", "failed", "burst", "poll"] as const)(
908
+ "tracks native background shells through launch, retained output, stop=%s",
909
+ (stop) =>
910
+ Effect.gen(function* () {
911
+ const stopped = stop === true || stop === "thread" || stop === "poll";
912
+ const exitCode = stop === "failed" ? 7 : 0;
913
+ const shellID = "sh_background";
914
+ const tool = {
915
+ sessionID: SESSION,
916
+ assistantMessageID: "msg_assistant",
917
+ id: "call-shell",
918
+ };
919
+ const gate = makeProviderReplayGate(["shell-exit"]);
920
+ const { runtime, thread } = yield* resumed(
921
+ [
922
+ out("session.prompt", { sessionID: SESSION, text: "<any>" }),
923
+ promptAccepted,
924
+ event("session.execution.started", { sessionID: SESSION }),
925
+ event("session.tool.input.started", { ...tool, name: "shell" }),
926
+ event("session.tool.called", {
927
+ ...tool,
928
+ name: "shell",
929
+ input: { command: "echo retained", background: true },
930
+ executed: false,
931
+ }),
932
+ event(
933
+ "session.tool.success",
934
+ {
935
+ ...tool,
936
+ content: [{ type: "text", text: "Launched" }],
937
+ metadata: { shellID },
938
+ executed: false,
939
+ },
940
+ undefined,
941
+ 2,
942
+ ),
943
+ out("shell.output", "<any>"),
944
+ reply("shell.output", {
945
+ location: { directory: WORK },
946
+ data: { output: "retained\n", cursor: 9, size: 9, truncated: false },
947
+ }),
948
+ ...(stop === "poll"
949
+ ? [out("shell.output", "<any>"), reply("shell.output", "<hang>")]
950
+ : []),
951
+ event("session.execution.succeeded", { sessionID: SESSION }),
952
+ ...(stopped
953
+ ? []
954
+ : [
955
+ event(
956
+ "shell.exited",
957
+ { id: shellID, exit: exitCode, status: "exited" },
958
+ "shell-exit",
959
+ ),
960
+ ]),
961
+ ...(stop === "burst"
962
+ ? Array.from({ length: 5 }, (_, page) => [
963
+ out("shell.output", { cursor: String(9 + page * 65536) }),
964
+ reply("shell.output", {
965
+ location: { directory: WORK },
966
+ data: {
967
+ output:
968
+ page === 4 ? "x".repeat(65519) + "FINAL DIAGNOSTIC\n" : "x".repeat(65536),
969
+ cursor: 9 + (page + 1) * 65536,
970
+ size: 9 + 5 * 65536,
971
+ truncated: false,
972
+ },
973
+ }),
974
+ ]).flat()
975
+ : [
976
+ out("shell.output", "<any>"),
977
+ reply("shell.output", {
978
+ location: { directory: WORK },
979
+ data: { output: "", cursor: 9, size: 9, truncated: false },
980
+ }),
981
+ ]),
982
+ ...(stopped
983
+ ? [
984
+ out("shell.remove", { id: shellID, "location[directory]": WORK }),
985
+ event("shell.exited", { id: shellID, exit: 143, status: "exited" }),
986
+ reply("shell.remove", null),
987
+ ]
988
+ : []),
989
+ ],
990
+ { replayGate: gate },
991
+ );
992
+ const items: Array<import("@t3tools/contracts").OrchestrationV2TurnItem> = [];
993
+ const shellStarted = yield* Deferred.make<void>();
994
+ const terminal = yield* runtime.events.pipe(
995
+ Stream.tap((e) =>
996
+ Effect.gen(function* () {
997
+ if (e.type !== "turn_item.updated") return;
998
+ items.push(e.turnItem);
999
+ if (e.turnItem.type === "command_execution" && e.turnItem.output === "retained\n")
1000
+ yield* Deferred.succeed(shellStarted, undefined);
1001
+ }),
1002
+ ),
1003
+ Stream.filter((e) => e.type === "turn.terminal"),
1004
+ Stream.runHead,
1005
+ Effect.forkScoped,
1006
+ );
1007
+ yield* runtime.startTurn(withLineage(thread));
1008
+ if (stop === "poll") {
1009
+ yield* Deferred.await(shellStarted);
1010
+ yield* TestClock.adjust("2 seconds");
1011
+ }
1012
+ yield* Fiber.join(terminal);
1013
+ assert.isTrue(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1014
+ const settled = yield* runtime.events.pipe(
1015
+ Stream.tap((e) =>
1016
+ Effect.sync(() => {
1017
+ if (e.type === "turn_item.updated") items.push(e.turnItem);
1018
+ }),
1019
+ ),
1020
+ Stream.filter(
1021
+ (e) =>
1022
+ e.type === "provider_thread.updated" &&
1023
+ e.providerThread.pendingBackgroundTasks?.length === 0,
1024
+ ),
1025
+ Stream.runHead,
1026
+ Effect.forkScoped,
1027
+ );
1028
+ if (stop === "thread")
1029
+ yield* runtime.interruptTurn({
1030
+ providerThread: thread,
1031
+ providerTurnId: yield* providerTurnId,
1032
+ requestRuntimeRestart: true,
1033
+ });
1034
+ else if (stopped) yield* runtime.stopTask!({ providerThread: thread, taskId: shellID });
1035
+ else gate.release("shell-exit");
1036
+ yield* Fiber.join(settled);
1037
+ assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1038
+ assert.deepInclude(items.filter((i) => i.type === "command_execution").at(-1), {
1039
+ type: "command_execution",
1040
+ status: stopped ? "interrupted" : stop === "failed" ? "failed" : "completed",
1041
+ ...(stop === "failed" ? { exitCode: 7 } : {}),
1042
+ output: stop === "burst" ? "x".repeat(262127) + "FINAL DIAGNOSTIC\n" : "retained\n",
1043
+ });
1044
+ assert.isFalse(items.some((i) => i.type === "subagent"));
1045
+ }).pipe(Effect.scoped),
1046
+ );
1047
+
1048
+ it.effect.each([
1049
+ "shell",
1050
+ "job",
1051
+ "fallback",
1052
+ "descendant",
1053
+ "settled",
1054
+ "exited",
1055
+ "anchored",
1056
+ ] as const)("recovers only owned native shells using %s inventory", (discovery) =>
1057
+ Effect.gen(function* () {
1058
+ const idAllocator = yield* IdAllocator.IdAllocatorV2.pipe(Effect.provide(IdAllocator.layer));
1059
+ const id = "sh_recovered";
1060
+ const info = {
1061
+ id,
1062
+ command: "sleep 600",
1063
+ status: discovery === "settled" ? "exited" : "running",
1064
+ ...(discovery === "settled" ? { exit: 0 } : {}),
1065
+ cwd: WORK,
1066
+ shell: "/bin/sh",
1067
+ file: "/remote/output",
1068
+ metadata: { sessionID: SESSION },
1069
+ time: { started: 1 },
1070
+ };
1071
+ const response = (data: unknown) => ({ location: { directory: WORK }, data });
1072
+ const inventory: Array<ProviderReplayEntry> =
1073
+ discovery === "shell"
1074
+ ? []
1075
+ : [
1076
+ out("job.list", { sessionID: SESSION, kind: "shell", descendants: "true" }),
1077
+ reply(
1078
+ "job.list",
1079
+ discovery === "fallback"
1080
+ ? { status: 404, body: {} }
1081
+ : {
1082
+ data: [
1083
+ {
1084
+ kind: "shell",
1085
+ shellID: id,
1086
+ sessionID: discovery === "descendant" ? "ses_descendant" : SESSION,
1087
+ command: info.command,
1088
+ status: discovery === "settled" ? "completed" : "running",
1089
+ },
1090
+ ],
1091
+ },
1092
+ ),
1093
+ ];
1094
+ if (discovery === "shell" || discovery === "fallback")
1095
+ inventory.push(
1096
+ out("shell.list", "<any>"),
1097
+ reply(
1098
+ "shell.list",
1099
+ response([info, { ...info, id: "sh_other", metadata: { sessionID: "ses_other" } }]),
1100
+ ),
1101
+ );
1102
+ inventory.push(
1103
+ out("shell.get", { id }),
1104
+ reply("shell.get", response(info)),
1105
+ out("shell.output", { id, cursor: "0" }),
1106
+ reply(
1107
+ "shell.output",
1108
+ response({ output: "retained", cursor: 8, size: 8, truncated: false }),
1109
+ ),
1110
+ );
1111
+ const gate = makeProviderReplayGate(["recovered-exit"]);
1112
+ const { runtime, thread } = yield* resumed(
1113
+ discovery === "settled"
1114
+ ? []
1115
+ : [
1116
+ ...(discovery === "exited"
1117
+ ? [event("shell.exited", { id, exit: 0, status: "exited" }, "recovered-exit")]
1118
+ : []),
1119
+ out("shell.output", { id, cursor: "8" }),
1120
+ reply(
1121
+ "shell.output",
1122
+ response({
1123
+ output: discovery === "exited" ? " finished" : "",
1124
+ cursor: discovery === "exited" ? 17 : 8,
1125
+ size: discovery === "exited" ? 17 : 8,
1126
+ truncated: false,
1127
+ }),
1128
+ ),
1129
+ ...(discovery === "exited"
1130
+ ? []
1131
+ : [out("shell.remove", { id }), reply("shell.remove", null)]),
1132
+ ],
1133
+ {
1134
+ version: discovery === "shell" ? "2.2.1" : "2.2.6",
1135
+ shellRecovery: inventory,
1136
+ external: true,
1137
+ ...(discovery === "anchored" ? { lastRunOrdinal: 7 } : {}),
1138
+ replayGate: gate,
1139
+ },
1140
+ );
1141
+ const items: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
1142
+ const shellEnded = yield* Deferred.make<void>();
1143
+ const rosterEnded = yield* Deferred.make<void>();
1144
+ yield* runtime.events.pipe(
1145
+ Stream.runForEach((event) =>
1146
+ Effect.gen(function* () {
1147
+ if (
1148
+ event.type === "provider_thread.updated" &&
1149
+ event.sessionScoped === true &&
1150
+ event.providerThread.pendingBackgroundTasks?.length === 0
1151
+ )
1152
+ yield* Deferred.succeed(rosterEnded, undefined);
1153
+ if (event.type !== "turn_item.updated" || event.turnItem.type !== "command_execution")
1154
+ return;
1155
+ items.push(event);
1156
+ if (event.turnItem.status !== "running") yield* Deferred.succeed(shellEnded, undefined);
1157
+ }),
1158
+ ),
1159
+ Effect.forkScoped,
1160
+ );
1161
+ assert.deepEqual(
1162
+ thread.pendingBackgroundTasks ?? [],
1163
+ discovery === "settled" ? [] : [{ taskId: id, kind: "command", description: info.command }],
1164
+ );
1165
+ assert.equal(
1166
+ yield* runtime.hasPendingBackgroundWorkForThread!(thread),
1167
+ discovery !== "settled",
1168
+ );
1169
+ yield* runtime.stopTask!({ providerThread: thread, taskId: "sh_other" });
1170
+ assert.equal(
1171
+ yield* runtime.hasPendingBackgroundWorkForThread!(thread),
1172
+ discovery !== "settled",
1173
+ );
1174
+ if (discovery === "exited") gate.release("recovered-exit");
1175
+ else yield* runtime.stopTask!({ providerThread: thread, taskId: id });
1176
+ yield* Deferred.await(shellEnded);
1177
+ yield* Deferred.await(rosterEnded);
1178
+ assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1179
+ const item = items;
1180
+ const projected = item.at(0);
1181
+ const stopped = item.at(-1);
1182
+ if (stopped?.type === "turn_item.updated" && stopped.turnItem.type === "command_execution") {
1183
+ assert.equal(
1184
+ stopped.turnItem.status,
1185
+ discovery === "settled" || discovery === "exited" ? "completed" : "interrupted",
1186
+ );
1187
+ assert.equal(
1188
+ stopped.turnItem.output,
1189
+ discovery === "exited" ? "retained finished" : "retained",
1190
+ );
1191
+ assert.equal(
1192
+ stopped.turnItem.id,
1193
+ projected?.type === "turn_item.updated" ? projected.turnItem.id : undefined,
1194
+ );
1195
+ }
1196
+ assert.equal(projected?.type, "turn_item.updated");
1197
+ if (projected?.type === "turn_item.updated") {
1198
+ assert.isTrue(
1199
+ item.every(
1200
+ (event) =>
1201
+ event.type === "turn_item.updated" &&
1202
+ event.turnItem.runId ===
1203
+ (discovery === "anchored"
1204
+ ? idAllocator.derive.run({ threadId, ordinal: 7 })
1205
+ : null),
1206
+ ),
1207
+ );
1208
+ assert.deepInclude(projected.turnItem, {
1209
+ type: "command_execution",
1210
+ input: info.command,
1211
+ output: "retained",
1212
+ });
1213
+ }
1214
+ }).pipe(Effect.scoped),
1215
+ );
1216
+
1217
+ it.effect.each(["remove", "lookup", "output", "timeout"] as const)(
1218
+ "continues recovering and stopping native work when shell %s fails",
1219
+ (failure) =>
1220
+ Effect.gen(function* () {
1221
+ const ids = ["sh_unreachable", "sh_stoppable"];
1222
+ const info = (id: string) => ({
1223
+ id,
1224
+ command: "sleep 600",
1225
+ status: "running",
1226
+ cwd: WORK,
1227
+ shell: "/bin/sh",
1228
+ file: "/remote/output",
1229
+ metadata: { sessionID: SESSION },
1230
+ time: { started: 1 },
1231
+ });
1232
+ const response = (data: unknown) => ({ location: { directory: WORK }, data });
1233
+ const output = (id: string) => [
1234
+ out("shell.output", { id }),
1235
+ reply("shell.output", response({ output: "", cursor: 0, size: 0, truncated: false })),
1236
+ ];
1237
+ const gate = makeProviderReplayGate(["recovery-pending", "polled"]);
1238
+ const recovery = yield* resumed(
1239
+ [
1240
+ ...backgroundLaunch(CHILD),
1241
+ ...output(ids[0]!),
1242
+ out("shell.remove", { id: ids[0] }),
1243
+ reply("shell.remove", failure === "remove" ? { status: 500, body: {} } : null),
1244
+ ...output(ids[1]!),
1245
+ out("shell.remove", { id: ids[1] }),
1246
+ reply("shell.remove", null),
1247
+ out("session.interrupt", { sessionID: CHILD }),
1248
+ reply("session.interrupt", { interrupted: true }),
1249
+ out("session.interrupt", { sessionID: SESSION }),
1250
+ reply("session.interrupt", { interrupted: true }),
1251
+ event("session.execution.interrupted", { sessionID: SESSION }),
1252
+ ...(failure === "remove"
1253
+ ? [
1254
+ ...output(ids[0]!),
1255
+ out("shell.remove", { id: ids[0] }),
1256
+ reply("shell.remove", null),
1257
+ ]
1258
+ : []),
1259
+ ],
1260
+ {
1261
+ version: "2.2.1",
1262
+ replayGate: gate,
1263
+ shellRecovery: [
1264
+ out("shell.list", "<any>"),
1265
+ reply("shell.list", response(ids.map(info))),
1266
+ ...(failure === "timeout"
1267
+ ? [
1268
+ out("shell.get", { id: ids[0] }),
1269
+ reply("shell.get", "<hang>"),
1270
+ event(
1271
+ "session.execution.succeeded",
1272
+ { sessionID: "ses_unrelated" },
1273
+ "recovery-pending",
1274
+ ),
1275
+ ...ids.map((id) => out("shell.output", { id })),
1276
+ ...ids.map(() =>
1277
+ reply(
1278
+ "shell.output",
1279
+ response({ output: "", cursor: 0, size: 0, truncated: false }),
1280
+ ),
1281
+ ),
1282
+ event("session.execution.succeeded", { sessionID: "ses_unrelated" }, "polled"),
1283
+ ]
1284
+ : ids.flatMap((id) => [
1285
+ out("shell.get", { id }),
1286
+ reply(
1287
+ "shell.get",
1288
+ id === ids[0] && failure === "lookup"
1289
+ ? { status: 500, body: {} }
1290
+ : response(info(id)),
1291
+ ),
1292
+ ...(id === ids[0] && failure === "lookup"
1293
+ ? []
1294
+ : id === ids[0] && failure === "output"
1295
+ ? [
1296
+ out("shell.output", { id }),
1297
+ reply("shell.output", { status: 500, body: {} }),
1298
+ ]
1299
+ : output(id)),
1300
+ ])),
1301
+ ],
1302
+ },
1303
+ ).pipe(Effect.forkScoped);
1304
+ if (failure === "timeout") {
1305
+ yield* Effect.promise(() => gate.waitForReached("recovery-pending"));
1306
+ yield* TestClock.adjust("5 seconds");
1307
+ }
1308
+ const { runtime, thread } = yield* Fiber.join(recovery);
1309
+ const recovered = yield* runtime.events.pipe(
1310
+ Stream.takeUntil(
1311
+ (event) =>
1312
+ event.type === "provider_thread.updated" &&
1313
+ event.providerThread.pendingBackgroundTasks?.length === 2,
1314
+ ),
1315
+ Stream.runCollect,
1316
+ );
1317
+ assert.sameMembers(
1318
+ recovered.flatMap((event) =>
1319
+ event.type === "turn_item.updated" && event.turnItem.type === "command_execution"
1320
+ ? [event.turnItem.nativeItemRef?.nativeId]
1321
+ : [],
1322
+ ),
1323
+ ids,
1324
+ );
1325
+ if (failure === "timeout") {
1326
+ gate.release("recovery-pending");
1327
+ yield* Effect.promise(() => gate.waitForReached("polled"));
1328
+ gate.release("polled");
1329
+ }
1330
+ const watch = yield* watchBackgroundTurn(runtime);
1331
+ yield* runtime.startTurn(withLineage(thread));
1332
+ yield* watch.attached;
1333
+ yield* runtime.interruptTurn({
1334
+ providerThread: thread,
1335
+ providerTurnId: yield* providerTurnId,
1336
+ requestRuntimeRestart: true,
1337
+ });
1338
+ assert.equal((yield* watch.terminal)?.status, "interrupted");
1339
+ assert.equal(
1340
+ yield* runtime.hasPendingBackgroundWorkForThread!(thread),
1341
+ failure === "remove",
1342
+ );
1343
+ if (failure === "remove")
1344
+ yield* runtime.stopTask!({ providerThread: thread, taskId: ids[0]! });
1345
+ assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1346
+ }).pipe(Effect.scoped),
1347
+ );
1348
+
1349
it.effect("keeps background subagents running when a turn is interrupted to restart it", () =>
1350
Effect.gen(function* () {
1351
const { runtime, thread } = yield* resumed([
1633
event("session.execution.started", { sessionID: CHILD }),
1634
];
1635
1176
- it.effect("settles a background subagent that fails, so no work is left pending", () =>
1177
- Effect.gen(function* () {
1178
- const { runtime, thread } = yield* resumed([
1179
- ...launchedAndRunning,
1180
- // The parent turn ends, and the background child fails: it reports
1181
- // nothing back, so its call has to end with the failure.
1182
- event("session.execution.succeeded", { sessionID: SESSION }),
1183
- event("session.execution.failed", {
1184
- sessionID: CHILD,
1185
- error: { type: "provider.quota", message: "Rate limit exceeded." },
1186
- }),
1187
- ]);
1188
- const watch = yield* watchCalls(runtime, thread);
1189
- yield* runtime.startTurn(withLineage(thread));
1190
- yield* watch.until(
1191
- () => watch.calls.get("Sleep")?.status === "failed" && watch.state.roster?.length === 0,
1192
- );
1193
- assert.deepEqual(watch.state.roster, []);
1194
- assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1195
- }).pipe(Effect.scoped),
1636
+ it.effect.each([false, true])(
1637
+ "settles a failed background subagent with recovered child=%s",
1638
+ (recoveredChild) =>
1639
+ Effect.gen(function* () {
1640
+ const ids = yield* IdAllocator.IdAllocatorV2.pipe(Effect.provide(IdAllocator.layer));
1641
+ const childThreadId = ids.derive.threadFromProviderThread({
1642
+ driver: OPENCODE_PROVIDER,
1643
+ nativeThreadId: CHILD,
1644
+ providerInstanceId: instanceId,
1645
+ });
1646
+ const childProviderThread = {
1647
+ ...providerThread(yield* DateTime.now),
1648
+ id: ids.derive.providerThread({
1649
+ driver: OPENCODE_PROVIDER,
1650
+ nativeThreadId: CHILD,
1651
+ providerInstanceId: instanceId,
1652
+ }),
1653
+ appThreadId: childThreadId,
1654
+ nativeThreadRef: {
1655
+ driver: OPENCODE_PROVIDER,
1656
+ nativeId: CHILD,
1657
+ strength: "strong" as const,
1658
+ },
1659
+ };
1660
+ const shell = {
1661
+ id: "sh_old_child",
1662
+ command: "echo done",
1663
+ status: "exited",
1664
+ exit: 0,
1665
+ cwd: WORK,
1666
+ shell: "/bin/sh",
1667
+ file: "/remote/output",
1668
+ metadata: { sessionID: CHILD },
1669
+ time: { started: 1, completed: 2 },
1670
+ };
1671
+ const response = (data: unknown) => ({ location: { directory: WORK }, data });
1672
+ const { runtime, thread } = yield* resumed(
1673
+ [
1674
+ ...(recoveredChild
1675
+ ? [
1676
+ out("session.get", { sessionID: CHILD }),
1677
+ replyData("session.get", sessionInfo({ id: CHILD })),
1678
+ out("permission.list", { sessionID: CHILD }),
1679
+ replyData("permission.list", []),
1680
+ out("session.form.list", { sessionID: CHILD }),
1681
+ replyData("session.form.list", []),
1682
+ out("shell.list", "<any>"),
1683
+ reply("shell.list", response([shell])),
1684
+ out("shell.get", { id: shell.id }),
1685
+ reply("shell.get", response(shell)),
1686
+ out("shell.output", { id: shell.id }),
1687
+ reply(
1688
+ "shell.output",
1689
+ response({ output: "done", cursor: 4, size: 4, truncated: false }),
1690
+ ),
1691
+ out("session.update", { sessionID: CHILD, permissions: "<any>" }),
1692
+ reply("session.update", null),
1693
+ ]
1694
+ : []),
1695
+ ...launchedAndRunning,
1696
+ // The parent turn ends, and the background child fails: it reports
1697
+ // nothing back, so its call has to end with the failure.
1698
+ event("session.execution.succeeded", { sessionID: SESSION }),
1699
+ event("session.execution.failed", {
1700
+ sessionID: CHILD,
1701
+ error: { type: "provider.quota", message: "Rate limit exceeded." },
1702
+ }),
1703
+ ],
1704
+ recoveredChild ? { version: "2.2.1" } : undefined,
1705
+ );
1706
+ if (recoveredChild)
1707
+ yield* runtime.resumeThread({
1708
+ providerThread: childProviderThread,
1709
+ threadId: childThreadId,
1710
+ modelSelection: bigPickle,
1711
+ runtimePolicy: policy(),
1712
+ });
1713
+ const childUpdates: Array<
1714
+ Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "provider_thread.updated" }>
1715
+ > = [];
1716
+ const watch = yield* watchCalls(
1717
+ {
1718
+ ...runtime,
1719
+ events: runtime.events.pipe(
1720
+ Stream.tap((event) =>
1721
+ Effect.sync(() => {
1722
+ if (
1723
+ event.type === "provider_thread.updated" &&
1724
+ event.providerThread.id === childProviderThread.id &&
1725
+ event.providerThread.ownerNodeId !== null
1726
+ )
1727
+ childUpdates.push(event);
1728
+ }),
1729
+ ),
1730
+ ),
1731
+ },
1732
+ thread,
1733
+ );
1734
+ yield* runtime.startTurn(withLineage(thread));
1735
+ yield* watch.until(
1736
+ () => watch.calls.get("Sleep")?.status === "failed" && watch.state.roster?.length === 0,
1737
+ );
1738
+ assert.includeMembers(
1739
+ childUpdates.map((event) => event.providerThread.status),
1740
+ ["active", "idle"],
1741
+ );
1742
+ assert.isFalse(
1743
+ childUpdates.some((event) => event.sessionScoped === true),
1744
+ "reattached child status must route through the parent run",
1745
+ );
1746
+ assert.deepEqual(watch.state.roster, []);
1747
+ assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
1748
+ }).pipe(Effect.scoped),
1749
);
1750
1751
// regression: #21979 a failed background subagent pinned its thread as busy
2977
...(delivery === "pending"
2978
? [out("rpc.call", "<any>"), reply("rpc.call", { output: { unreadable: true } })]
2979
: []),
2980
+ ...(delivery === "pending" ? emptyShellInventory : []),
2981
...reconnect.slice(2),
2982
...(delivery === "pending"
2983
? [
3583
],
3584
});
3585
if (source !== "history")
3032
- frames.splice(2, 0, out("rpc.call", "<any>"), reply("rpc.call", { output: null }));
3586
+ frames.splice(
3587
+ 2,
3588
+ 0,
3589
+ out("rpc.call", "<any>"),
3590
+ reply("rpc.call", { output: null }),
3591
+ out("job.list", { kind: "shell" }),
3592
+ reply("job.list", { data: [] }),
3593
+ );
3594
return frames;
3595
})(),
3596
out("message.list", { sessionID: SESSION, type: "synthetic", limit: "100" }),
6459
input: { sessionID: SESSION },
6460
}),
6461
reply("rpc.call", { output: nativeGoal("paused") }),
6462
+ ...emptyShellInventory,
6463
],
6464
{ version: "2.2.5" },
6465
);
6504
...recovered.slice(0, 2),
6505
out("rpc.call", "<any>"),
6506
reply("rpc.call", { output: nativeGoal("active") }),
6507
+ ...emptyShellInventory,
6508
...recovered.slice(2),
6509
...noOpenRequests,
6510
]
6636
),
6637
]
6638
: []),
6639
+ ...emptyShellInventory,
6640
...reconnect.slice(2),
6641
],
6642
{ version: "2.2.5", replayGate: gate },