The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 22:15 UTC
fix(opencode): show prompts other OpenCode 2 clients send into a T3 session (#27839)
Julius Marminge committed
Nov 11, 2026 at 15:33 UTC
4d1808dc4cf32b331e3042a897ba4a2d8865a79d
2 files changed
+488
-7
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+406
-1
@@ -5701,7 +5701,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
5701
}).pipe(Effect.scoped),
5702
);
5703
5704
- const continued = (after: ReadonlyArray<ProviderReplayEntry>) =>
5704
+ const continued = (after: ReadonlyArray<ProviderReplayEntry>, external?: string) =>
5705
Effect.gen(function* () {
5706
const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
5707
const offered = yield* Deferred.make<void>();
@@ -5709,6 +5709,15 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
5709
...backgroundLaunch(CHILD),
5710
event("session.execution.succeeded", { sessionID: SESSION }),
5711
event("session.execution.succeeded", { sessionID: CHILD }),
5712
+ ...(external === undefined
5713
+ ? []
5714
+ : [
5715
+ event("session.inbox.enqueued", {
5716
+ inboxID: external,
5717
+ sessionID: SESSION,
5718
+ item: { type: "user", payload: { text: "qwe!", files: [] }, delivery: "steer" },
5719
+ }),
5720
+ ]),
5721
event("session.inbox.enqueued", {
5722
inboxID: "msg_report",
5723
sessionID: SESSION,
@@ -5728,6 +5737,9 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
5737
},
5738
}),
5739
event("session.execution.started", { sessionID: SESSION }),
5740
+ ...(external === undefined
5741
+ ? []
5742
+ : [event("session.inbox.delivered", { sessionID: SESSION, inboxID: external })]),
5743
event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_report" }),
5744
event("session.text.ended", {
5745
sessionID: SESSION,
@@ -5813,6 +5825,59 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
5825
}).pipe(Effect.scoped),
5826
);
5827
5828
+ it.effect(
5829
+ "reverts OpenCode at another client's prompt queued before a continuation's report",
5830
+ () =>
5831
+ Effect.gen(function* () {
5832
+ const { runtime, thread, first, second } = yield* continued(
5833
+ [
5834
+ out("message.list", "<any>"),
5835
+ reply("message.list", {
5836
+ data: [
5837
+ {
5838
+ id: "msg_report",
5839
+ time: { created: 3 },
5840
+ text: CONTINUED_REPORT,
5841
+ type: "synthetic",
5842
+ },
5843
+ { id: "msg_external", time: { created: 2 }, text: "qwe!", type: "user" },
5844
+ { id: CONTINUED_PROMPT, time: { created: 1 }, text: "hi", type: "user" },
5845
+ ],
5846
+ cursor: {},
5847
+ }),
5848
+ out("session.revert.stage", {
5849
+ sessionID: SESSION,
5850
+ messageID: "msg_external",
5851
+ files: false,
5852
+ }),
5853
+ replyData("session.revert.stage", { messageID: "msg_external", files: [] }),
5854
+ out("session.revert.commit", { sessionID: SESSION }),
5855
+ reply("session.revert.commit", null),
5856
+ out("message.list", "<any>"),
5857
+ reply("message.list", {
5858
+ data: [{ id: CONTINUED_PROMPT, time: { created: 1 }, text: "hi", type: "user" }],
5859
+ cursor: {},
5860
+ }),
5861
+ ],
5862
+ "msg_external",
5863
+ );
5864
+ const snapshot = yield* runtime.rollbackThread({
5865
+ providerThread: thread,
5866
+ target: {
5867
+ type: "provider_turn",
5868
+ checkpointId: CheckpointId.make("checkpoint:first"),
5869
+ appRunOrdinal: 1,
5870
+ providerTurn: first,
5871
+ },
5872
+ providerThreadTurns: [first, second],
5873
+ });
5874
+ assert.deepEqual(
5875
+ snapshot.providerTurns.map((turn) => turn.id),
5876
+ [first.id],
5877
+ );
5878
+ }).pipe(Effect.scoped),
5879
+ );
5880
+
5881
it.effect("keeps a subagent a stopped background reply started off the next turn", () =>
5882
Effect.gen(function* () {
5883
const CHILD_B = "ses_f1485c529ffeBBBBBBBBBBBBBB";
@@ -6190,6 +6255,346 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
6255
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
6256
);
6257
6258
+ it.effect(
6259
+ "reverts a report's continuation at its report after a Stop dropped another client's prompt",
6260
+ () =>
6261
+ Effect.gen(function* () {
6262
+ const kept = `msg_t3_turn_${SESSION}:attempt:opencode2-adapter`;
6263
+ const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
6264
+ const reportOffered = yield* Deferred.make<void>();
6265
+ const { runtime, thread } = yield* resumed([
6266
+ event("session.inbox.enqueued", {
6267
+ inboxID: "msg_stale",
6268
+ sessionID: SESSION,
6269
+ item: { type: "user", payload: { text: "qwe!", files: [] }, delivery: "steer" },
6270
+ }),
6271
+ event("session.execution.started", { sessionID: SESSION }),
6272
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_stale" }),
6273
+ event("session.execution.succeeded", { sessionID: SESSION }),
6274
+ event("session.model.selected", {
6275
+ sessionID: SESSION,
6276
+ model: { id: "buffered", providerID: "opencode" },
6277
+ }),
6278
+ out("session.switchModel", { sessionID: SESSION, model: "<any>" }),
6279
+ reply("session.switchModel", null),
6280
+ ...backgroundLaunch(CHILD),
6281
+ event("session.execution.succeeded", { sessionID: SESSION }),
6282
+ event("session.execution.succeeded", { sessionID: CHILD }),
6283
+ event("session.inbox.enqueued", {
6284
+ inboxID: "msg_report",
6285
+ sessionID: SESSION,
6286
+ item: {
6287
+ type: "synthetic",
6288
+ payload: {
6289
+ text: CONTINUED_REPORT,
6290
+ description: "Sleep",
6291
+ metadata: {
6292
+ source: "subagent",
6293
+ childID: CHILD,
6294
+ agent: "General",
6295
+ state: "completed",
6296
+ },
6297
+ },
6298
+ delivery: "steer",
6299
+ },
6300
+ }),
6301
+ event("session.execution.started", { sessionID: SESSION }),
6302
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_report" }),
6303
+ event("session.execution.succeeded", { sessionID: SESSION }),
6304
+ out("message.list", "<any>"),
6305
+ reply("message.list", {
6306
+ data: [
6307
+ { id: "msg_report", time: { created: 3 }, text: CONTINUED_REPORT, type: "synthetic" },
6308
+ { id: kept, time: { created: 2 }, text: "hi", type: "user" },
6309
+ { id: "msg_stale", time: { created: 1 }, text: "qwe!", type: "user" },
6310
+ ],
6311
+ cursor: {},
6312
+ }),
6313
+ out("session.revert.stage", {
6314
+ sessionID: SESSION,
6315
+ messageID: "msg_report",
6316
+ files: false,
6317
+ }),
6318
+ replyData("session.revert.stage", { messageID: "msg_report", files: [] }),
6319
+ out("session.revert.commit", { sessionID: SESSION }),
6320
+ reply("session.revert.commit", null),
6321
+ out("message.list", "<any>"),
6322
+ reply("message.list", {
6323
+ data: [
6324
+ { id: kept, time: { created: 2 }, text: "hi", type: "user" },
6325
+ { id: "msg_stale", time: { created: 1 }, text: "qwe!", type: "user" },
6326
+ ],
6327
+ cursor: {},
6328
+ }),
6329
+ ]).pipe(
6330
+ Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
6331
+ offer: (request) =>
6332
+ Effect.sync(() => void offers.push(request)).pipe(
6333
+ Effect.andThen(() =>
6334
+ offers.length === 2 ? Deferred.succeed(reportOffered, undefined) : Effect.void,
6335
+ ),
6336
+ Effect.asVoid,
6337
+ ),
6338
+ take: Effect.never,
6339
+ }),
6340
+ );
6341
+ const turns: Array<OrchestrationV2ProviderTurn> = [];
6342
+ const buffered = yield* Deferred.make<void>();
6343
+ const bothEnded = yield* Deferred.make<void>();
6344
+ yield* runtime.events.pipe(
6345
+ Stream.tap((event) =>
6346
+ Effect.gen(function* () {
6347
+ if (
6348
+ event.type === "provider_thread.updated" &&
6349
+ event.providerThread.nativeMetadata?.modelSelection?.model === "opencode/buffered"
6350
+ ) {
6351
+ yield* Deferred.succeed(buffered, undefined);
6352
+ }
6353
+ if (
6354
+ event.type === "provider_turn.updated" &&
6355
+ event.providerTurn.status !== "running"
6356
+ ) {
6357
+ turns.push(event.providerTurn);
6358
+ if (turns.length === 2) yield* Deferred.succeed(bothEnded, undefined);
6359
+ }
6360
+ }),
6361
+ ),
6362
+ Stream.runDrain,
6363
+ Effect.forkScoped,
6364
+ );
6365
+ yield* Deferred.await(buffered);
6366
+ yield* runtime.interruptTurn({
6367
+ providerThread: thread,
6368
+ providerTurnId: yield* providerTurnId,
6369
+ requestRuntimeRestart: true,
6370
+ });
6371
+ yield* runtime.startTurn(withLineage(thread));
6372
+ yield* Deferred.await(reportOffered);
6373
+ yield* runtime.startTurn(continuationTurn(thread, offers[1]!));
6374
+ yield* Deferred.await(bothEnded);
6375
+ const [first, continuation] = turns;
6376
+ const snapshot = yield* runtime.rollbackThread({
6377
+ providerThread: thread,
6378
+ target: {
6379
+ type: "provider_turn",
6380
+ checkpointId: CheckpointId.make("checkpoint:first"),
6381
+ appRunOrdinal: 1,
6382
+ providerTurn: first!,
6383
+ },
6384
+ providerThreadTurns: [first!, continuation!],
6385
+ });
6386
+ assert.deepEqual(
6387
+ snapshot.providerTurns.map((turn) => turn.id),
6388
+ [first!.id],
6389
+ );
6390
+ }).pipe(Effect.scoped),
6391
+ );
6392
+
6393
+ it.effect(
6394
+ "reverts a report's continuation at its report after a reconnect lost another client's prompt start",
6395
+ () =>
6396
+ Effect.gen(function* () {
6397
+ const kept = `msg_t3_turn_${SESSION}:attempt:opencode2-adapter`;
6398
+ const offered =
6399
+ yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>();
6400
+ const { runtime, thread } = yield* resumed([
6401
+ event("session.inbox.enqueued", {
6402
+ inboxID: "msg_stale",
6403
+ sessionID: SESSION,
6404
+ item: { type: "user", payload: { text: "qwe!", files: [] }, delivery: "steer" },
6405
+ }),
6406
+ { type: "runtime_exit", status: "success" } as const,
6407
+ out("event.subscribe"),
6408
+ event("session.model.selected", {
6409
+ sessionID: SESSION,
6410
+ model: { id: "buffered", providerID: "opencode" },
6411
+ }),
6412
+ out("session.switchModel", { sessionID: SESSION, model: "<any>" }),
6413
+ reply("session.switchModel", null),
6414
+ ...backgroundLaunch(CHILD),
6415
+ event("session.execution.succeeded", { sessionID: SESSION }),
6416
+ event("session.execution.succeeded", { sessionID: CHILD }),
6417
+ event("session.inbox.enqueued", {
6418
+ inboxID: "msg_report",
6419
+ sessionID: SESSION,
6420
+ item: {
6421
+ type: "synthetic",
6422
+ payload: {
6423
+ text: CONTINUED_REPORT,
6424
+ description: "Sleep",
6425
+ metadata: {
6426
+ source: "subagent",
6427
+ childID: CHILD,
6428
+ agent: "General",
6429
+ state: "completed",
6430
+ },
6431
+ },
6432
+ delivery: "steer",
6433
+ },
6434
+ }),
6435
+ event("session.execution.started", { sessionID: SESSION }),
6436
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_report" }),
6437
+ event("session.execution.succeeded", { sessionID: SESSION }),
6438
+ out("message.list", "<any>"),
6439
+ reply("message.list", {
6440
+ data: [
6441
+ { id: "msg_report", time: { created: 3 }, text: CONTINUED_REPORT, type: "synthetic" },
6442
+ { id: kept, time: { created: 2 }, text: "hi", type: "user" },
6443
+ { id: "msg_stale", time: { created: 1 }, text: "qwe!", type: "user" },
6444
+ ],
6445
+ cursor: {},
6446
+ }),
6447
+ out("session.revert.stage", {
6448
+ sessionID: SESSION,
6449
+ messageID: "msg_report",
6450
+ files: false,
6451
+ }),
6452
+ replyData("session.revert.stage", { messageID: "msg_report", files: [] }),
6453
+ out("session.revert.commit", { sessionID: SESSION }),
6454
+ reply("session.revert.commit", null),
6455
+ out("message.list", "<any>"),
6456
+ reply("message.list", {
6457
+ data: [
6458
+ { id: kept, time: { created: 2 }, text: "hi", type: "user" },
6459
+ { id: "msg_stale", time: { created: 1 }, text: "qwe!", type: "user" },
6460
+ ],
6461
+ cursor: {},
6462
+ }),
6463
+ ]).pipe(
6464
+ Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
6465
+ offer: (request) => Deferred.succeed(offered, request).pipe(Effect.asVoid),
6466
+ take: Effect.never,
6467
+ }),
6468
+ );
6469
+ const turns: Array<OrchestrationV2ProviderTurn> = [];
6470
+ const reconnected = yield* Deferred.make<void>();
6471
+ const bothEnded = yield* Deferred.make<void>();
6472
+ yield* runtime.events.pipe(
6473
+ Stream.tap((event) =>
6474
+ Effect.gen(function* () {
6475
+ if (
6476
+ event.type === "provider_thread.updated" &&
6477
+ event.providerThread.nativeMetadata?.modelSelection?.model === "opencode/buffered"
6478
+ ) {
6479
+ yield* Deferred.succeed(reconnected, undefined);
6480
+ }
6481
+ if (
6482
+ event.type === "provider_turn.updated" &&
6483
+ event.providerTurn.status !== "running"
6484
+ ) {
6485
+ turns.push(event.providerTurn);
6486
+ if (turns.length === 2) yield* Deferred.succeed(bothEnded, undefined);
6487
+ }
6488
+ }),
6489
+ ),
6490
+ Stream.runDrain,
6491
+ Effect.forkScoped,
6492
+ );
6493
+ yield* Deferred.await(reconnected);
6494
+ yield* runtime.startTurn(withLineage(thread));
6495
+ yield* runtime.startTurn(continuationTurn(thread, yield* Deferred.await(offered)));
6496
+ yield* Deferred.await(bothEnded);
6497
+ const [first, continuation] = turns;
6498
+ const snapshot = yield* runtime.rollbackThread({
6499
+ providerThread: thread,
6500
+ target: {
6501
+ type: "provider_turn",
6502
+ checkpointId: CheckpointId.make("checkpoint:first"),
6503
+ appRunOrdinal: 1,
6504
+ providerTurn: first!,
6505
+ },
6506
+ providerThreadTurns: [first!, continuation!],
6507
+ });
6508
+ assert.deepEqual(
6509
+ snapshot.providerTurns.map((turn) => turn.id),
6510
+ [first!.id],
6511
+ );
6512
+ }).pipe(Effect.scoped),
6513
+ );
6514
+
6515
+ it.effect("shows a second prompt another client sent while its continuation was pending", () =>
6516
+ Effect.gen(function* () {
6517
+ const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = [];
6518
+ const prompt = (inboxID: string, text: string) =>
6519
+ event("session.inbox.enqueued", {
6520
+ inboxID,
6521
+ sessionID: SESSION,
6522
+ item: { type: "user", payload: { text, files: [] }, delivery: "steer" },
6523
+ });
6524
+ // T3's own prompt can be admitted after a failed submit ended its turn.
6525
+ const late = `msg_t3_turn_${SESSION}:attempt:late`;
6526
+ const { runtime, thread } = yield* resumed([
6527
+ prompt(late, "mine"),
6528
+ prompt("msg_external_a", "first?"),
6529
+ event("session.execution.started", { sessionID: SESSION }),
6530
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: late }),
6531
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_external_a" }),
6532
+ prompt("msg_external_b", "second?"),
6533
+ event("session.inbox.delivered", { sessionID: SESSION, inboxID: "msg_external_b" }),
6534
+ event("session.text.ended", {
6535
+ sessionID: SESSION,
6536
+ assistantMessageID: "msg_external_reply",
6537
+ ordinal: 0,
6538
+ text: "Both!",
6539
+ }),
6540
+ event("session.execution.succeeded", { sessionID: SESSION }),
6541
+ // Handled only after the wake above has buffered everything before it.
6542
+ event("session.model.selected", {
6543
+ sessionID: SESSION,
6544
+ model: { id: "buffered", providerID: "opencode" },
6545
+ }),
6546
+ ]).pipe(
6547
+ Effect.provideService(ProviderContinuationRequests.ProviderContinuationRequests, {
6548
+ offer: (request) => Effect.sync(() => void offers.push(request)),
6549
+ take: Effect.never,
6550
+ }),
6551
+ );
6552
+ const collected: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
6553
+ const buffered = yield* Deferred.make<void>();
6554
+ const ended = yield* Deferred.make<void>();
6555
+ yield* runtime.events.pipe(
6556
+ Stream.tap((event) =>
6557
+ Effect.gen(function* () {
6558
+ collected.push(event);
6559
+ if (
6560
+ event.type === "provider_thread.updated" &&
6561
+ event.providerThread.nativeMetadata?.modelSelection?.model === "opencode/buffered"
6562
+ ) {
6563
+ yield* Deferred.succeed(buffered, undefined);
6564
+ }
6565
+ if (event.type === "turn.terminal") yield* Deferred.succeed(ended, undefined);
6566
+ }),
6567
+ ),
6568
+ Stream.runDrain,
6569
+ Effect.forkScoped,
6570
+ );
6571
+ yield* Deferred.await(buffered);
6572
+ yield* runtime.startTurn(continuationTurn(thread, offers[0]!));
6573
+ yield* Deferred.await(ended);
6574
+ assert.deepEqual(
6575
+ collected.flatMap((event) =>
6576
+ event.type === "turn_item.updated" &&
6577
+ (event.turnItem.type === "user_message" || event.turnItem.type === "assistant_message")
6578
+ ? [`${event.turnItem.type}: ${event.turnItem.text}`]
6579
+ : [],
6580
+ ),
6581
+ ["user_message: first?", "user_message: second?", "assistant_message: Both!"],
6582
+ );
6583
+ assert.deepEqual(
6584
+ [
6585
+ ...new Set(
6586
+ collected.flatMap((event) =>
6587
+ event.type === "message.updated"
6588
+ ? [`${event.message.role}: ${event.message.text}`]
6589
+ : [],
6590
+ ),
6591
+ ),
6592
+ ],
6593
+ ["user: first?", "user: second?", "assistant: Both!"],
6594
+ );
6595
+ }).pipe(Effect.scoped),
6596
+ );
6597
+
6598
it.effect("shows a question asked while OpenCode wakes for a background reply", () =>
6599
Effect.gen(function* () {
6600
const OTHER = "ses_other00000000000000000000";
packages/provider-opencode/src/server/v2/adapter.ts
+82
-6
@@ -359,6 +359,8 @@ interface ThreadState {
359
>;
360
readonly stoppedChildren: Set<string>;
361
readonly strandedSteers: Set<string>;
362
+ /** Prompts other clients queued while no T3 turn ran, shown once delivered; `leads` the next wake if no report preceded it. */
363
+ readonly externalPrompts: Map<string, { readonly text: string; leads: boolean }>;
364
mcp:
365
| { readonly name: string; readonly directory: string; readonly credential: string }
366
| undefined;
@@ -618,6 +620,7 @@ const turnPromptId = (sessionId: string, attemptId: string) =>
620
SessionMessage.ID.make(`msg_t3_turn_${sessionId}:${attemptId}`);
621
const steerPromptId = (sessionId: string, messageId: string) =>
622
SessionMessage.ID.make(`msg_t3_steer_${sessionId}:${messageId}`);
623
+const isOwnPrompt = (inboxId: string) => inboxId.startsWith("msg_t3_");
624
625
const promptOf = (turn: OrchestrationV2ProviderTurn) => {
626
const nativeId = turn.nativeTurnRef?.nativeId;
@@ -836,6 +839,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
839
reports: new Map(),
840
stoppedChildren: new Set(),
841
strandedSteers: new Set(),
842
+ externalPrompts: new Map(),
843
mcp: undefined,
844
instructions: undefined,
845
});
@@ -1118,6 +1122,57 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1122
});
1123
});
1124
1125
+ // T3's own prompts are sent while their turn is active, so only other clients reach here.
1126
+ const emitExternalPrompt = Effect.fnUntraced(function* (
1127
+ state: ThreadState,
1128
+ turn: ActiveTurn,
1129
+ inboxId: string,
1130
+ text: string,
1131
+ ) {
1132
+ const now = yield* DateTime.now;
1133
+ const base = {
1134
+ ...itemBase(state, turn, inboxId, "completed", now, now, now),
1135
+ nodeId: turn.input.rootNodeId,
1136
+ };
1137
+ // History derives the same ID from the message ID, so a later snapshot does not duplicate it.
1138
+ const messageId = idAllocator.derive.messageFromProviderItem({
1139
+ driver,
1140
+ nativeItemId: inboxId,
1141
+ });
1142
+ yield* emit({
1143
+ type: "message.updated",
1144
+ driver,
1145
+ message: {
1146
+ createdBy: "user",
1147
+ creationSource: "provider",
1148
+ id: messageId,
1149
+ threadId: turn.input.threadId,
1150
+ runId: turn.input.runId,
1151
+ nodeId: base.nodeId,
1152
+ role: "user",
1153
+ text,
1154
+ attachments: [],
1155
+ streaming: false,
1156
+ createdAt: now,
1157
+ updatedAt: now,
1158
+ },
1159
+ });
1160
+ yield* emit({
1161
+ type: "turn_item.updated",
1162
+ driver,
1163
+ turnItem: {
1164
+ ...base,
1165
+ createdBy: "user",
1166
+ creationSource: "provider",
1167
+ type: "user_message",
1168
+ messageId,
1169
+ inputIntent: "turn_start",
1170
+ text,
1171
+ attachments: [],
1172
+ },
1173
+ });
1174
+ });
1175
+
1176
// Todo updates share the provider event span.
1177
const emitTodo = Effect.fnUntraced(function* (
1178
state: ThreadState,
@@ -2757,6 +2812,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2812
const { delivered, stopped } = yield* takeReports(state);
2813
if (stopped) return;
2814
const route = state.providerThread.appThreadId;
2815
+ // History begins at whichever OpenCode queued first: a report or another client's prompt.
2816
+ // A prompt leads only this wake, so one dropped undelivered cannot move a later boundary.
2817
+ const leading = [...state.externalPrompts].find(([, prompt]) => prompt.leads)?.[0];
2818
+ for (const prompt of state.externalPrompts.values()) prompt.leads = false;
2819
const wake: Wake = {
2820
messageId:
2821
route === null
@@ -2768,7 +2827,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2827
detail: delivered.length === 0 ? null : delivered.map((entry) => entry.text).join("\n\n"),
2828
dropped: false,
2829
after: delivered.at(-1)?.inboxId,
2771
- first: delivered[0]?.inboxId,
2830
+ first: leading ?? delivered[0]?.inboxId,
2831
children: new Set(),
2832
};
2833
state.wakes.push(wake);
@@ -3173,21 +3232,36 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3232
}
3233
if (event.type === "session.inbox.cancelled" || event.type === "session.inbox.delivered") {
3234
state.reports.delete(event.data.inboxID);
3235
+ const external = state.externalPrompts.get(event.data.inboxID);
3236
+ state.externalPrompts.delete(event.data.inboxID);
3237
const turn = state.active;
3238
if (turn !== undefined && turn.steers.delete(event.data.inboxID)) {
3239
turn.settledInbox.add(event.data.inboxID);
3240
if (event.type === "session.inbox.cancelled") yield* endIfSettled(state, turn);
3241
+ } else if (
3242
+ event.type === "session.inbox.delivered" &&
3243
+ external !== undefined &&
3244
+ turn !== undefined
3245
+ ) {
3246
+ yield* emitExternalPrompt(state, turn, event.data.inboxID, external.text);
3247
}
3248
return;
3249
}
3250
+ // No T3 turn existed while a wake held these, so its user items came from another client.
3251
if (
3252
event.type === "session.inbox.enqueued" &&
3253
event.data.item.type === "user" &&
3186
- state.subagent !== undefined &&
3187
- state.subagent.prompt === undefined &&
3188
- state.active === undefined
3254
+ (state.active === undefined || (held && state.subagent === undefined))
3255
) {
3190
- state.subagent.prompt = event.data.item.payload.text;
3256
+ const text = event.data.item.payload.text;
3257
+ if (state.subagent !== undefined) state.subagent.prompt ??= text;
3258
+ // T3's own prompts can be admitted after a failed submit ended their turn.
3259
+ else if (!isOwnPrompt(event.data.inboxID)) {
3260
+ state.externalPrompts.set(event.data.inboxID, {
3261
+ text,
3262
+ leads: !held && state.reports.size === 0,
3263
+ });
3264
+ }
3265
return;
3266
}
3267
const started =
@@ -3482,11 +3556,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3556
currentScope = scope;
3557
yield* Scope.close(previous, Exit.void);
3558
// A restarted server forgot T3's MCP servers; the next turn adds them again.
3559
+ // The gap may have started a pending prompt unseen, so none may set a later wake's boundary.
3560
for (const state of threads.values()) {
3561
state.mcp = undefined;
3562
state.goalFresh = false;
3563
state.goalRefreshing = false;
3564
state.goalRevision += 1;
3565
+ for (const prompt of state.externalPrompts.values()) prompt.leads = false;
3566
}
3567
if (supportsNativeGoals()) {
3568
yield* Effect.forEach(
@@ -4126,7 +4202,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
4202
const wake = state.wakes.shift()!;
4203
const turn = state.active;
4204
if (turn !== undefined && wake.after !== undefined) turn.before = wake.after;
4129
- // Its first report is where its history begins, so fork and rollback cut there.
4205
+ // Fork and rollback cut where its history begins.
4206
if (turn !== undefined && wake.first !== undefined) {
4207
turn.providerTurn = { ...turn.providerTurn, nativeTurnRef: ref(wake.first, "weak") };
4208
}