Open History to see the last commit
1import {
2 ContextTransferId,
3 OrchestrationV2Actor,
4 OrchestrationV2AppThread,
5 OrchestrationV2ContextSourcePoint,
6 OrchestrationV2ContextTransfer,
7 OrchestrationV2CreationSource,
8 OrchestrationV2ProviderThread,
9 OrchestrationV2Run,
10 OrchestrationV2ThreadProjection,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as Context from "effect/Context";
14import * as DateTime from "effect/DateTime";
15import * as Effect from "effect/Effect";
16import * as Layer from "effect/Layer";
17import * as Schema from "effect/Schema";
18
19export interface ThreadForkPlanV2 {
20 readonly targetThread: OrchestrationV2AppThread;
21 readonly transfer: OrchestrationV2ContextTransfer;
22}
23
24export class ThreadForkPlanError extends Schema.TaggedError<ThreadForkPlanError>()(
25 "ThreadForkPlanError",
26 {
27 sourceThreadId: ThreadId,
28 targetThreadId: ThreadId,
29 cause: Schema.optional(Schema.Defect()),
30 },
31) {}
32
33/**
34 * `waiting` is provider-finished with checkpoint capture still pending.
35 */
36export function isForkableSourceRunStatus(status: OrchestrationV2Run["status"]): boolean {
37 return (
38 status === "completed" ||
39 status === "waiting" ||
40 status === "failed" ||
41 status === "interrupted" ||
42 status === "cancelled"
43 );
44}
45
46export function forkableSourceRunStatusError(
47 run: Pick<OrchestrationV2Run, "id" | "status">,
48): string {
49 return `Fork source run ${run.id} is ${run.status}; in-progress and rolled-back runs cannot be forked.`;
50}
51
52export interface ThreadForkServiceV2Shape {
53 readonly plan: (input: {
54 readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
55 readonly sourceRun: OrchestrationV2Run;
56 readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
57 readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint;
58 readonly transferId: ContextTransferId;
59 readonly targetThreadId: ThreadId;
60 readonly title?: string;
61 readonly createdBy: OrchestrationV2Actor;
62 readonly creationSource: OrchestrationV2CreationSource;
63 readonly createdAt: DateTime.Utc;
64 }) => Effect.Effect<ThreadForkPlanV2, ThreadForkPlanError>;
65}
66
67export class ThreadForkServiceV2 extends Context.Service<
68 ThreadForkServiceV2,
69 ThreadForkServiceV2Shape
70>()("t3/orchestration-v2/ThreadForkService/ThreadForkServiceV2") {}
71
72export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
73 ThreadForkServiceV2,
74 ThreadForkServiceV2.of({
75 plan: Effect.fn("ThreadForkService.plan")(function* (input) {
76 if (!isForkableSourceRunStatus(input.sourceRun.status)) {
77 return yield* new ThreadForkPlanError({
78 sourceThreadId: input.sourceProjection.thread.id,
79 targetThreadId: input.targetThreadId,
80 cause: forkableSourceRunStatusError(input.sourceRun),
81 });
82 }
83 const targetThread: OrchestrationV2AppThread = {
84 ...input.sourceProjection.thread,
85 // PR context follows the conversation; watching remains the source thread's responsibility.
86 pullRequests: input.sourceProjection.thread.pullRequests?.map(
87 ({ watch: _watch, ...link }) => link,
88 ),
89 createdBy: input.createdBy,
90 creationSource: input.creationSource,
91 id: input.targetThreadId,
92 title: input.title ?? `${input.sourceProjection.thread.title} fork`,
93 activeProviderThreadId: null,
94 lineage: {
95 parentThreadId: input.sourceProjection.thread.id,
96 relationshipToParent: "fork",
97 rootThreadId: input.sourceProjection.thread.lineage.rootThreadId,
98 },
99 forkedFrom: {
100 type: "run",
101 threadId: input.sourceProjection.thread.id,
102 runId: input.sourceRun.id,
103 },
104 createdAt: input.createdAt,
105 updatedAt: input.createdAt,
106 archivedAt: null,
107 settledOverride: null,
108 settledAt: null,
109 snoozedUntil: null,
110 snoozedAt: null,
111 lastVisitedAt: null,
112 deletedAt: null,
113 };
114 const transfer: OrchestrationV2ContextTransfer = {
115 id: input.transferId,
116 type: "fork",
117 sourceThreadId: input.sourceProjection.thread.id,
118 targetThreadId: input.targetThreadId,
119 sourcePoint: input.canonicalSourcePoint,
120 basePoint: null,
121 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
122 targetProviderInstanceId: null,
123 targetRunId: null,
124 status: "pending",
125 resolution: null,
126 createdBy: input.createdBy,
127 error:
128 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
129 ? null
130 : "Source provider thread does not expose a strong native thread ref.",
131 createdAt: input.createdAt,
132 updatedAt: input.createdAt,
133 consumedAt: null,
134 };
135 return { targetThread, transfer };
136 }),
137 }),
138);