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/**
35 * `waiting` is provider-finished with checkpoint capture still pending.
36 */
37export function isForkableSourceRunStatus(status: OrchestrationV2Run["status"]): boolean {
38 return (
39 status === "completed" ||
40 status === "waiting" ||
41 status === "failed" ||
42 status === "interrupted" ||
43 status === "cancelled"
44 );
45}
46
47export function forkableSourceRunStatusError(
48 run: Pick<OrchestrationV2Run, "id" | "status">,
49): string {
50 return `Fork source run ${run.id} is ${run.status}; in-progress and rolled-back runs cannot be forked.`;
51}
52
53export interface ThreadForkServiceV2Shape {
54 readonly plan: (input: {
55 readonly sourceProjection: Pick<OrchestrationV2ThreadProjection, "thread">;
56 readonly sourceRun: OrchestrationV2Run;
57 readonly sourceProviderThread: OrchestrationV2ProviderThread | undefined;
58 readonly sourceTokenUsage?: OrchestrationV2ProviderTurnTokenUsage | undefined;
59 readonly canonicalSourcePoint: OrchestrationV2ContextSourcePoint;
60 readonly transferId: ContextTransferId;
61 readonly targetThreadId: ThreadId;
62 readonly title?: string;
63 readonly createdBy: OrchestrationV2Actor;
64 readonly creationSource: OrchestrationV2CreationSource;
65 readonly createdAt: DateTime.Utc;
66 }) => Effect.Effect<ThreadForkPlanV2, ThreadForkPlanError>;
67}
68
69export class ThreadForkServiceV2 extends Context.Service<
70 ThreadForkServiceV2,
71 ThreadForkServiceV2Shape
72>()("t3/orchestration-v2/ThreadForkService/ThreadForkServiceV2") {}
73
74export const layer: Layer.Layer<ThreadForkServiceV2> = Layer.succeed(
75 ThreadForkServiceV2,
76 ThreadForkServiceV2.of({
77 plan: Effect.fn("ThreadForkService.plan")(function* (input) {
78 if (!isForkableSourceRunStatus(input.sourceRun.status)) {
79 return yield* new ThreadForkPlanError({
80 sourceThreadId: input.sourceProjection.thread.id,
81 targetThreadId: input.targetThreadId,
82 cause: forkableSourceRunStatusError(input.sourceRun),
83 });
84 }
85 const targetThread: OrchestrationV2AppThread = {
86 ...input.sourceProjection.thread,
87 // PR context follows the conversation; watching remains the source thread's responsibility.
88 pullRequests: input.sourceProjection.thread.pullRequests?.map(
89 ({ watch: _watch, ...link }) => link,
90 ),
91 createdBy: input.createdBy,
92 creationSource: input.creationSource,
93 id: input.targetThreadId,
94 title: input.title ?? `${input.sourceProjection.thread.title} fork`,
95 activeProviderThreadId: null,
96 lineage: {
97 parentThreadId: input.sourceProjection.thread.id,
98 relationshipToParent: "fork",
99 rootThreadId: input.sourceProjection.thread.lineage.rootThreadId,
100 },
101 forkedFrom: {
102 type: "run",
103 threadId: input.sourceProjection.thread.id,
104 runId: input.sourceRun.id,
105 },
106 createdAt: input.createdAt,
107 updatedAt: input.createdAt,
108 archivedAt: null,
109 settledOverride: null,
110 settledAt: null,
111 snoozedUntil: null,
112 snoozedAt: 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);