Open History to see the last commit
1import {
2 OrchestrationGetTurnDiffResult,
3 type OrchestrationGetFullThreadDiffInput,
4 type OrchestrationGetFullThreadDiffResult,
5 type OrchestrationGetTurnDiffResult as OrchestrationGetTurnDiffResultType,
6} from "@t3tools/contracts";
7import { Effect, Layer, Schema } from "effect";
8
9import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
10import { CheckpointInvariantError, CheckpointUnavailableError } from "../Errors.ts";
11import { checkpointRefForThreadTurn, resolveThreadWorkspaceCwd } from "../Utils.ts";
12import { CheckpointStore } from "../Services/CheckpointStore.ts";
13import {
14 CheckpointDiffQuery,
15 type CheckpointDiffQueryShape,
16} from "../Services/CheckpointDiffQuery.ts";
17
18const isTurnDiffResult = Schema.is(OrchestrationGetTurnDiffResult);
19
20const make = Effect.gen(function* () {
21 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
22 const checkpointStore = yield* CheckpointStore;
23
24 const getTurnDiff: CheckpointDiffQueryShape["getTurnDiff"] = (input) =>
25 Effect.gen(function* () {
26 const operation = "CheckpointDiffQuery.getTurnDiff";
27
28 if (input.fromTurnCount === input.toTurnCount) {
29 const emptyDiff: OrchestrationGetTurnDiffResultType = {
30 threadId: input.threadId,
31 fromTurnCount: input.fromTurnCount,
32 toTurnCount: input.toTurnCount,
33 diff: "",
34 };
35 if (!isTurnDiffResult(emptyDiff)) {
36 return yield* new CheckpointInvariantError({
37 operation,
38 detail: "Computed turn diff result does not satisfy contract schema.",
39 });
40 }
41 return emptyDiff;
42 }
43
44 const snapshot = yield* projectionSnapshotQuery.getSnapshot();
45 const thread = snapshot.threads.find((entry) => entry.id === input.threadId);
46 if (!thread) {
47 return yield* new CheckpointInvariantError({
48 operation,
49 detail: `Thread '${input.threadId}' not found.`,
50 });
51 }
52
53 const maxTurnCount = thread.checkpoints.reduce(
54 (max, checkpoint) => Math.max(max, checkpoint.checkpointTurnCount),
55 0,
56 );
57 if (input.toTurnCount > maxTurnCount) {
58 return yield* new CheckpointUnavailableError({
59 threadId: input.threadId,
60 turnCount: input.toTurnCount,
61 detail: `Turn diff range exceeds current turn count: requested ${input.toTurnCount}, current ${maxTurnCount}.`,
62 });
63 }
64
65 const workspaceCwd = resolveThreadWorkspaceCwd({
66 thread,
67 projects: snapshot.projects,
68 });
69 if (!workspaceCwd) {
70 return yield* new CheckpointInvariantError({
71 operation,
72 detail: `Workspace path missing for thread '${input.threadId}' when computing turn diff.`,
73 });
74 }
75
76 const fromCheckpointRef =
77 input.fromTurnCount === 0
78 ? checkpointRefForThreadTurn(input.threadId, 0)
79 : thread.checkpoints.find(
80 (checkpoint) => checkpoint.checkpointTurnCount === input.fromTurnCount,
81 )?.checkpointRef;
82 if (!fromCheckpointRef) {
83 return yield* new CheckpointUnavailableError({
84 threadId: input.threadId,
85 turnCount: input.fromTurnCount,
86 detail: `Checkpoint ref is unavailable for turn ${input.fromTurnCount}.`,
87 });
88 }
89
90 const toCheckpointRef = thread.checkpoints.find(
91 (checkpoint) => checkpoint.checkpointTurnCount === input.toTurnCount,
92 )?.checkpointRef;
93 if (!toCheckpointRef) {
94 return yield* new CheckpointUnavailableError({
95 threadId: input.threadId,
96 turnCount: input.toTurnCount,
97 detail: `Checkpoint ref is unavailable for turn ${input.toTurnCount}.`,
98 });
99 }
100
101 const [fromExists, toExists] = yield* Effect.all(
102 [
103 checkpointStore.hasCheckpointRef({
104 cwd: workspaceCwd,
105 checkpointRef: fromCheckpointRef,
106 }),
107 checkpointStore.hasCheckpointRef({
108 cwd: workspaceCwd,
109 checkpointRef: toCheckpointRef,
110 }),
111 ],
112 { concurrency: "unbounded" },
113 );
114
115 if (!fromExists) {
116 return yield* new CheckpointUnavailableError({
117 threadId: input.threadId,
118 turnCount: input.fromTurnCount,
119 detail: `Filesystem checkpoint is unavailable for turn ${input.fromTurnCount}.`,
120 });
121 }
122
123 if (!toExists) {
124 return yield* new CheckpointUnavailableError({
125 threadId: input.threadId,
126 turnCount: input.toTurnCount,
127 detail: `Filesystem checkpoint is unavailable for turn ${input.toTurnCount}.`,
128 });
129 }
130
131 const diff = yield* checkpointStore.diffCheckpoints({
132 cwd: workspaceCwd,
133 fromCheckpointRef,
134 toCheckpointRef,
135 fallbackFromToHead: false,
136 });
137
138 const turnDiff: OrchestrationGetTurnDiffResultType = {
139 threadId: input.threadId,
140 fromTurnCount: input.fromTurnCount,
141 toTurnCount: input.toTurnCount,
142 diff,
143 };
144 if (!isTurnDiffResult(turnDiff)) {
145 return yield* new CheckpointInvariantError({
146 operation,
147 detail: "Computed turn diff result does not satisfy contract schema.",
148 });
149 }
150
151 return turnDiff;
152 });
153
154 const getFullThreadDiff: CheckpointDiffQueryShape["getFullThreadDiff"] = (
155 input: OrchestrationGetFullThreadDiffInput,
156 ) =>
157 getTurnDiff({
158 threadId: input.threadId,
159 fromTurnCount: 0,
160 toTurnCount: input.toTurnCount,
161 }).pipe(Effect.map((result): OrchestrationGetFullThreadDiffResult => result));
162
163 return {
164 getTurnDiff,
165 getFullThreadDiff,
166 } satisfies CheckpointDiffQueryShape;
167});
168
169export const CheckpointDiffQueryLive = Layer.effect(CheckpointDiffQuery, make);