GeezerVison is showing github.com — fix(opencode): cut forks before slash commands (#44982) · pingdotgg/t3code@1237bd9
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2 packages/provider-opencode/src/server/v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/OpenCode2OrchestratorV2.integration.test.ts +45-5Lines changed: 45 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -442,24 +442,64 @@ describe("OpenCode 2 through the orchestrator", () => { | 442 | 442 | }), | 443 | 443 | ); | 444 | 444 |
| 445 | | - it.effect("forks from an earlier turn before the next turn's user message", () => | | 445 | + it.effect.each(["prompt", "slash command"])("forks before the next %s", (nextTurn) => | 446 | 446 | Effect.gen(function* () { | 447 | 447 | const name = "opencode2-fork"; | 448 | 448 | const cwd = yield* checkpointWorkspace(name); | 449 | 449 | const recorded = yield* readProviderReplayTranscript( | 450 | 450 | new URL("./testkit/fixtures/opencode2_fork/opencode_transcript.ndjson", import.meta.url), | 451 | 451 | ); | 452 | 452 |
| | 453 | + const sessionID = String(recorded.metadata?.["nativeSessionId"]); | | 454 | + const entries = recorded.entries.flatMap((entry): ReadonlyArray<ProviderReplayEntry> => { | | 455 | + if (nextTurn === "prompt" || entry.type === "runtime_exit") return [entry]; | | 456 | + if (entry.label === "session.prompt.2") { | | 457 | + return [ | | 458 | + out("command.list", "<any>"), | | 459 | + reply("command.list", { location: { directory: cwd }, data: [{ name: "hello" }] }), | | 460 | + out("message.list", { sessionID, order: "desc", limit: "1" }), | | 461 | + reply("message.list", { | | 462 | + data: [ | | 463 | + { id: "msg_idle_one", type: "idle", time: { created: 1 }, outcome: "succeeded" }, | | 464 | + ], | | 465 | + cursor: { previous: "after-turn-one" }, | | 466 | + }), | | 467 | + out("session.command", { sessionID, name: "hello", text: "TWO" }), | | 468 | + reply("session.command", null), | | 469 | + out("session.active"), | | 470 | + reply("session.active", { data: { [sessionID]: { type: "running" } } }), | | 471 | + ]; | | 472 | + } | | 473 | + if (entry.label === "message.list") { | | 474 | + return [ | | 475 | + out("message.list", { sessionID, cursor: "after-turn-one", limit: "1" }), | | 476 | + reply("message.list", { | | 477 | + data: [ | | 478 | + { | | 479 | + id: "msg_0eb7b1f9f001g95Bduh3bi8syS", | | 480 | + type: "user", | | 481 | + time: { created: 2 }, | | 482 | + text: "Reply exactly TWO.", | | 483 | + }, | | 484 | + ], | | 485 | + cursor: {}, | | 486 | + }), | | 487 | + ]; | | 488 | + } | | 489 | + return entry.label === "session.prompt.response.2" || | | 490 | + entry.label?.startsWith("message.list.") | | 491 | + ? [] | | 492 | + : [entry]; | | 493 | + }); | 453 | 494 | const transcript = yield* OpenCode2OrchestratorReplayHarness.decodeTranscript( | 454 | | - withDirectory(recorded, cwd), | | 495 | + withDirectory({ ...recorded, entries }, cwd), | 455 | 496 | ); | 456 | 497 | const source = threadCommands({ name, worktreePath: cwd }); | 457 | 498 | const target = ThreadId.make(`thread:${name}:target`); | 458 | | - const [one, two] = [source.message("one"), source.message("two")]; | 459 | 499 | const commands: ReadonlyArray<OrchestrationV2Command> = [ | 460 | 500 | source.create, | 461 | | - one, | 462 | | - two, | | 501 | + source.message("one"), | | 502 | + source.message("two", bigPickle, nextTurn === "slash command" ? "/hello TWO" : undefined), | 463 | 503 | { | 464 | 504 | type: "thread.fork", | 465 | 505 | createdBy: "user", | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-opencode/src/server/v2/adapter.ts +53-36Lines changed: 53 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -740,33 +740,7 @@ const promptOf = (turn: OrchestrationV2ProviderTurn) => { | 740 | 740 | return nativeId?.startsWith("msg_") === true ? nativeId : undefined; | 741 | 741 | }; | 742 | 742 |
| 743 | | -const boundaryAfter = ( | 744 | | - turns: ReadonlyArray<OrchestrationV2ProviderTurn>, | 745 | | - providerThreadId: OrchestrationV2ProviderThread["id"], | 746 | | - kept: OrchestrationV2ProviderTurn | undefined, | 747 | | - prompts: ReadonlySet<string>, | 748 | | -) => { | 749 | | - const later = turns | 750 | | - .filter( | 751 | | - (turn) => | 752 | | - turn.providerThreadId === providerThreadId && | 753 | | - (kept === undefined || turn.ordinal > kept.ordinal), | 754 | | - ) | 755 | | - .toSorted((left, right) => left.ordinal - right.ordinal); | 756 | | - if (later.some((turn) => promptOf(turn) === undefined && !isWakeTurn(turn))) { | 757 | | - return Effect.fail( | 758 | | - new ProviderAdapter.ProviderAdapterProtocolError({ | 759 | | - driver: OPENCODE_PROVIDER, | 760 | | - detail: | 761 | | - "This OpenCode conversation has turns from an earlier T3 Code version, so it can't be cut there.", | 762 | | - }), | 763 | | - ); | 764 | | - } | 765 | | - return Effect.succeed( | 766 | | - later.map(promptOf).find((messageId) => messageId !== undefined && prompts.has(messageId)) ?? | 767 | | - null, | 768 | | - ); | 769 | | -}; | | 743 | +const COMMAND_CURSOR = "command-cursor:"; | 770 | 744 |
| 771 | 745 | const isProviderAdapterError = Schema.is(ProviderAdapter.ProviderAdapterV2Error); | 772 | 746 |
| @@ -4364,6 +4338,47 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 4364 | 4338 | ), | 4365 | 4339 | ); | 4366 | 4340 |
| | 4341 | + const boundaryAfter = Effect.fnUntraced(function* ( | | 4342 | + sessionId: string, | | 4343 | + turns: ReadonlyArray<OrchestrationV2ProviderTurn>, | | 4344 | + providerThreadId: OrchestrationV2ProviderThread["id"], | | 4345 | + kept: OrchestrationV2ProviderTurn | undefined, | | 4346 | + operation: "fork" | "rollback", | | 4347 | + ) { | | 4348 | + const later = turns | | 4349 | + .filter( | | 4350 | + (turn) => | | 4351 | + turn.providerThreadId === providerThreadId && | | 4352 | + turn.ordinal > (kept?.ordinal ?? 0) && | | 4353 | + !turn.promptDropped && | | 4354 | + !isWakeTurn(turn), | | 4355 | + ) | | 4356 | + .toSorted((left, right) => left.ordinal - right.ordinal); | | 4357 | + let prompts = operation === "rollback" ? yield* userMessages(sessionId) : undefined; | | 4358 | + for (const turn of later) { | | 4359 | + const id = turn.nativeTurnRef?.nativeId; | | 4360 | + if (id?.startsWith(COMMAND_CURSOR)) { | | 4361 | + const cursor = id.slice(COMMAND_CURSOR.length); | | 4362 | + const page = yield* client.message.list({ | | 4363 | + sessionID: Session.ID.make(sessionId), | | 4364 | + ...(cursor ? { cursor } : { order: "asc" }), | | 4365 | + limit: 1, | | 4366 | + }); | | 4367 | + return page.data[0]?.id ?? null; | | 4368 | + } | | 4369 | + prompts ??= yield* userMessages(sessionId, ["user", "synthetic"]); | | 4370 | + if (id && prompts.has(id)) return id; | | 4371 | + if (operation === "fork" || promptOf(turn) === undefined) { | | 4372 | + return yield* new ProviderAdapter.ProviderAdapterProtocolError({ | | 4373 | + driver: OPENCODE_PROVIDER, | | 4374 | + detail: | | 4375 | + "T3 Code could not locate the exact turn boundary in this OpenCode conversation.", | | 4376 | + }); | | 4377 | + } | | 4378 | + } | | 4379 | + return null; | | 4380 | + }); | | 4381 | + | 4367 | 4382 | const snapshotOf = Effect.fnUntraced(function* ( | 4368 | 4383 | providerThread: OrchestrationV2ProviderThread, | 4369 | 4384 | sessionId: string, | @@ -4534,12 +4549,6 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 4534 | 4549 | } | 4535 | 4550 | }); | 4536 | 4551 |
| 4537 | | - const markBefore = (sessionId: string, before: string | null) => | 4538 | | - Effect.sync(() => { | 4539 | | - const turn = threads.get(sessionId)?.active; | 4540 | | - if (turn !== undefined) turn.before = before; | 4541 | | - }); | 4542 | | - | 4543 | 4552 | const beginTurn = Effect.fn("OpenCode2Adapter.beginTurn")(function* ( | 4544 | 4553 | state: ThreadState, | 4545 | 4554 | turnInput: ProviderAdapter.ProviderAdapterV2TurnInput, | @@ -4812,8 +4821,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 4812 | 4821 | // session.command neither accepts nor returns our ID; record the preceding history boundary. | 4813 | 4822 | const newest = yield* client.message.list({ sessionID, order: "desc", limit: 1 }); | 4814 | 4823 | const before = newest.data[0]?.id ?? null; | 4815 | | - yield* markBefore(sessionId, before); | 4816 | 4824 | if (!sending()) return; | | 4825 | + turn.before = before; | | 4826 | + if (newest.cursor.previous !== undefined || before === null) { | | 4827 | + turn.providerTurn = { | | 4828 | + ...turn.providerTurn, | | 4829 | + nativeTurnRef: ref(COMMAND_CURSOR + (newest.cursor.previous ?? ""), "weak"), | | 4830 | + }; | | 4831 | + } | 4817 | 4832 | if (goalCommand?.type === "pause" || goalCommand?.type === "clear") { | 4818 | 4833 | // OpenCode rejects pausing or clearing a missing goal; Codex treats that as a no-op. | 4819 | 4834 | yield* client.session.command({ sessionID, ...command }).pipe( | @@ -5568,10 +5583,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 5568 | 5583 | yield* ensureQuiet(sessionId, "roll back"); | 5569 | 5584 | } | 5570 | 5585 | const boundary = yield* boundaryAfter( | | 5586 | + sessionId, | 5571 | 5587 | rollbackInput.providerThreadTurns, | 5572 | 5588 | providerThread.id, | 5573 | 5589 | target.type === "provider_turn" ? target.providerTurn : undefined, | 5574 | | - yield* userMessages(sessionId), | | 5590 | + "rollback", | 5575 | 5591 | ); | 5576 | 5592 | if (boundary !== null) { | 5577 | 5593 | yield* rollBackTo(sessionId, SessionMessage.ID.make(boundary)); | @@ -5613,10 +5629,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr | 5613 | 5629 | selected === undefined | 5614 | 5630 | ? null | 5615 | 5631 | : yield* boundaryAfter( | | 5632 | + sourceId, | 5616 | 5633 | forkInput.sourceProviderTurns ?? [], | 5617 | 5634 | source.id, | 5618 | 5635 | selected, | 5619 | | - yield* userMessages(sourceId, ["user", "synthetic"]), | | 5636 | + "fork", | 5620 | 5637 | ); | 5621 | 5638 | const forked = yield* client.session.fork({ | 5622 | 5639 | sessionID: Session.ID.make(sourceId), | |
|