|
1 | 1 | import { assert, it } from "@effect/vitest"; |
2 | 2 | import { |
| 3 | + CheckpointId, |
3 | 4 | CommandId, |
4 | 5 | MessageId, |
5 | 6 | EventId, |
@@ -624,3 +625,164 @@ it.effect("keeps delegated child pull-request links independent of the parent", |
624 | 625 | assert.deepEqual(parentAfterChildLink.thread.pullRequests, parent.thread.pullRequests); |
625 | 626 | }).pipe(Effect.provide(layerTest)), |
626 | 627 | ); |
| 628 | + |
| 629 | +it.effect("links a side chat under its parent without claiming a fork, and promotes it", () => |
| 630 | + Effect.gen(function* () { |
| 631 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 632 | + const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 633 | + const projectId = ProjectId.make("project:side-chat"); |
| 634 | + const parentId = ThreadId.make("thread:side-chat-parent"); |
| 635 | + const sideChatId = ThreadId.make("thread:side-chat-child"); |
| 636 | + const base = { |
| 637 | + projectId, |
| 638 | + modelSelection, |
| 639 | + runtimeMode: "approval-required", |
| 640 | + interactionMode: "default", |
| 641 | + branch: null, |
| 642 | + worktreePath: null, |
| 643 | + createdBy: "user", |
| 644 | + creationSource: "web", |
| 645 | + } as const; |
| 646 | + yield* orchestrator.dispatch({ |
| 647 | + type: "thread.create", |
| 648 | + commandId: CommandId.make("create-side-chat-parent"), |
| 649 | + threadId: parentId, |
| 650 | + title: "Parent", |
| 651 | + ...base, |
| 652 | + }); |
| 653 | + yield* orchestrator.dispatch({ |
| 654 | + type: "thread.create", |
| 655 | + commandId: CommandId.make("create-side-chat-child"), |
| 656 | + threadId: sideChatId, |
| 657 | + title: "Side chat", |
| 658 | + parentThreadId: parentId, |
| 659 | + sideChat: true, |
| 660 | + ...base, |
| 661 | + }); |
| 662 | + |
| 663 | + const child = yield* projections.getThread(sideChatId); |
| 664 | + assert.deepEqual(child.lineage, { |
| 665 | + parentThreadId: parentId, |
| 666 | + relationshipToParent: null, |
| 667 | + rootThreadId: parentId, |
| 668 | + }); |
| 669 | + assert.isNull(child.forkedFrom); |
| 670 | + assert.equal(child.sideChat, true); |
| 671 | + assert.equal((yield* projections.getThreadShell(sideChatId))?.sideChat, true); |
| 672 | + assert.isUndefined((yield* projections.getThread(parentId)).sideChat); |
| 673 | + |
| 674 | + yield* orchestrator.dispatch({ |
| 675 | + type: "thread.metadata.update", |
| 676 | + commandId: CommandId.make("promote-side-chat"), |
| 677 | + threadId: sideChatId, |
| 678 | + sideChat: false, |
| 679 | + }); |
| 680 | + assert.equal((yield* projections.getThread(sideChatId)).sideChat, false); |
| 681 | + assert.equal((yield* projections.getThreadShell(sideChatId))?.sideChat, false); |
| 682 | + |
| 683 | + const crossProject = yield* Effect.exit( |
| 684 | + orchestrator.dispatch({ |
| 685 | + type: "thread.create", |
| 686 | + commandId: CommandId.make("create-side-chat-cross-project"), |
| 687 | + threadId: ThreadId.make("thread:side-chat-cross-project"), |
| 688 | + title: "Elsewhere", |
| 689 | + parentThreadId: parentId, |
| 690 | + sideChat: true, |
| 691 | + ...base, |
| 692 | + projectId: ProjectId.make("project:side-chat-other"), |
| 693 | + }), |
| 694 | + ); |
| 695 | + assert.equal(crossProject._tag, "Failure"); |
| 696 | + }).pipe(Effect.provide(layerTest)), |
| 697 | +); |
| 698 | + |
| 699 | +it.effect("forks a side chat from the last finished run while the source is mid-run", () => |
| 700 | + Effect.gen(function* () { |
| 701 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 702 | + const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 703 | + const projectId = ProjectId.make("project:side-chat-active"); |
| 704 | + const sourceId = ThreadId.make("thread:side-chat-active-source"); |
| 705 | + const targetId = ThreadId.make("thread:side-chat-active-target"); |
| 706 | + const now = yield* DateTime.now; |
| 707 | + yield* orchestrator.dispatch({ |
| 708 | + type: "thread.create", |
| 709 | + commandId: CommandId.make("create-side-chat-active-source"), |
| 710 | + threadId: sourceId, |
| 711 | + projectId, |
| 712 | + title: "Busy parent", |
| 713 | + modelSelection, |
| 714 | + runtimeMode: "full-access", |
| 715 | + interactionMode: "default", |
| 716 | + branch: null, |
| 717 | + worktreePath: null, |
| 718 | + createdBy: "user", |
| 719 | + creationSource: "web", |
| 720 | + }); |
| 721 | + const run = (ordinal: number, status: "completed" | "running") => ({ |
| 722 | + id: RunId.make(`run:side-chat-active-${ordinal}`), |
| 723 | + threadId: sourceId, |
| 724 | + ordinal, |
| 725 | + providerInstanceId: instanceId, |
| 726 | + modelSelection, |
| 727 | + providerThreadId: null, |
| 728 | + userMessageId: MessageId.make(`message:side-chat-active-${ordinal}`), |
| 729 | + rootNodeId: null, |
| 730 | + activeAttemptId: null, |
| 731 | + status, |
| 732 | + requestedAt: now, |
| 733 | + startedAt: now, |
| 734 | + completedAt: status === "completed" ? now : null, |
| 735 | + checkpointId: |
| 736 | + status === "completed" ? CheckpointId.make(`checkpoint:side-chat-${ordinal}`) : null, |
| 737 | + contextHandoffId: null, |
| 738 | + }); |
| 739 | + for (const next of [run(1, "completed"), run(2, "running")]) { |
| 740 | + yield* projections.apply({ |
| 741 | + id: EventId.make(`event:side-chat-active-${next.ordinal}`), |
| 742 | + type: "run.updated", |
| 743 | + threadId: sourceId, |
| 744 | + runId: next.id, |
| 745 | + providerInstanceId: instanceId, |
| 746 | + occurredAt: now, |
| 747 | + payload: next, |
| 748 | + }); |
| 749 | + } |
| 750 | + |
| 751 | + yield* orchestrator.dispatch({ |
| 752 | + type: "thread.fork", |
| 753 | + commandId: CommandId.make("fork-side-chat-active"), |
| 754 | + sourceThreadId: sourceId, |
| 755 | + targetThreadId: targetId, |
| 756 | + sourcePoint: { type: "latest_stable" }, |
| 757 | + sideChat: true, |
| 758 | + runtimeMode: "approval-required", |
| 759 | + createdBy: "user", |
| 760 | + creationSource: "web", |
| 761 | + }); |
| 762 | + |
| 763 | + const target = yield* projections.getThread(targetId); |
| 764 | + assert.deepEqual(target.forkedFrom, { |
| 765 | + type: "run", |
| 766 | + threadId: sourceId, |
| 767 | + runId: RunId.make("run:side-chat-active-1"), |
| 768 | + }); |
| 769 | + assert.equal(target.sideChat, true); |
| 770 | + assert.equal(target.runtimeMode, "approval-required"); |
| 771 | + assert.equal(target.lineage.relationshipToParent, "fork"); |
| 772 | + |
| 773 | + // Pinning the run that is still streaming is refused: it is not a stable point. |
| 774 | + const midRun = yield* Effect.exit( |
| 775 | + orchestrator.dispatch({ |
| 776 | + type: "thread.fork", |
| 777 | + commandId: CommandId.make("fork-side-chat-mid-run"), |
| 778 | + sourceThreadId: sourceId, |
| 779 | + targetThreadId: ThreadId.make("thread:side-chat-active-refused"), |
| 780 | + sourcePoint: { type: "run", runId: RunId.make("run:side-chat-active-2") }, |
| 781 | + sideChat: true, |
| 782 | + createdBy: "user", |
| 783 | + creationSource: "web", |
| 784 | + }), |
| 785 | + ); |
| 786 | + assert.equal(midRun._tag, "Failure"); |
| 787 | + }).pipe(Effect.provide(layerTest)), |
| 788 | +); |
0 commit comments