Open History to see the last commit
1import {
2 ContextTransferId,
3 OrchestrationV2Actor,
4 OrchestrationV2AppThread,
5 OrchestrationV2ContextSourcePoint,
6 OrchestrationV2ContextTransfer,
7 OrchestrationV2CreationSource,
8 OrchestrationV2ProviderThread,
9 OrchestrationV2ProviderTurnTokenUsage,
10 OrchestrationV2Run,
11 OrchestrationV2ThreadProjection,
12 ThreadId,
13} from "@t3tools/contracts";
14import * as Context from "effect/Context";
15import * as DateTime from "effect/DateTime";
16import * as Effect from "effect/Effect";
17import * as Layer from "effect/Layer";
18import * as Schema from "effect/Schema";
19
20export interface ThreadForkPlanV2 {
21 readonly targetThread: OrchestrationV2AppThread;
22 readonly transfer: OrchestrationV2ContextTransfer;
23}
24
25export class ThreadForkPlanError extends Schema.TaggedError<ThreadForkPlanError>()(
26 "ThreadForkPlanError",
27 {
28 sourceThreadId: ThreadId,
29 targetThreadId: ThreadId,
30 cause: Schema.optional(Schema.Defect()),
31 },
32) {}
33
34/** waiting means provider-finished with checkpoint capture pending. */
35export function isForkableSourceRunStatus(status: OrchestrationV2Run["status"]): boolean {
36 return (
37 status === "completed" ||
38 status === "waiting" ||
39 status === "failed" ||
40 status === "interrupted" ||
41 status === "cancelled"
42 );
43}
44
45export function forkableSourceRunStatusError(
46 run: Pick<OrchestrationV2Run, "id" | "status">,
47): string {
48 return `Fork source run ${run.id} is ${run.status}; in-progress and rolled-back runs cannot be forked.`;
49}
50
51export interface ThreadForkServiceV2Shape {
52 readonly plan: (input: {
53 readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
54 readonly sourceRun: OrchestrationV2Run;
55 readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
56 readonly sourceTokenUsage?: OrchestrationV2ProviderTurnTokenUsage | 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 // A guest link belongs to the thread it was made for.
112 guestShare: null,
113 lastVisitedAt: null,
114 // A fork is a new thread; it does not inherit how often the source was opened.
115 opens: null,
116 deletedAt: null,
117 };
118 const transfer: OrchestrationV2ContextTransfer = {
119 id: input.transferId,
120 type: "fork",
121 sourceThreadId: input.sourceProjection.thread.id,
122 targetThreadId: input.targetThreadId,
123 sourcePoint: input.canonicalSourcePoint,
124 basePoint: null,
125 sourceProviderInstanceId: input.sourceRun.providerInstanceId,
126 // Only native forks retain this context; portable handoffs rebuild it.
127 sourceTokenUsage:
128 (input.sourceRun.status === "completed" || input.sourceRun.status === "waiting") &&
129 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
130 ? input.sourceTokenUsage
131 : undefined,
132 targetProviderInstanceId: null,
133 targetRunId: null,
134 status: "pending",
135 resolution: null,
136 createdBy: input.createdBy,
137 error:
138 input.sourceProviderThread?.nativeThreadRef?.strength === "strong"
139 ? null
140 : "Source provider thread does not expose a strong native thread ref.",
141 createdAt: input.createdAt,
142 updatedAt: input.createdAt,
143 consumedAt: null,
144 };
145 return { targetThread, transfer };
146 }),
147 }),
148);