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, Option, Schema } from "effect";
8
9import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
10import { CheckpointInvariantError, CheckpointUnavailableError } from "../Errors.ts";
11import { checkpointRefForThreadTurn } 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"] = Effect.fn("getTurnDiff")(
25 function* (input) {
26 const operation = "CheckpointDiffQuery.getTurnDiff";
27 const ignoreWhitespace = input.ignoreWhitespace ?? true;
28
29 if (input.fromTurnCount === input.toTurnCount) {
30 const emptyDiff: OrchestrationGetTurnDiffResultType = {
31 threadId: input.threadId,
32 fromTurnCount: input.fromTurnCount,
33 toTurnCount: input.toTurnCount,
34 diff: "",
35 };
36 if (!isTurnDiffResult(emptyDiff)) {
37 return yield* new CheckpointInvariantError({
38 operation,
39 detail: "Computed turn diff result does not satisfy contract schema.",
40 });
41 }
42 return emptyDiff;
43 }
44
45 const threadContext = yield* projectionSnapshotQuery.getThreadCheckpointContext(
46 input.threadId,
47 );
48 if (Option.isNone(threadContext)) {
49 return yield* new CheckpointInvariantError({
50 operation,
51 detail: `Thread '${input.threadId}' not found.`,
52 });
53 }
54
55 const maxTurnCount = threadContext.value.checkpoints.reduce(
56 (max, checkpoint) => Math.max(max, checkpoint.checkpointTurnCount),
57 0,
58 );
59 if (input.toTurnCount > maxTurnCount) {
60 return yield* new CheckpointUnavailableError({
61 threadId: input.threadId,
62 turnCount: input.toTurnCount,
63 detail: `Turn diff range exceeds current turn count: requested ${input.toTurnCount}, current ${maxTurnCount}.`,
64 });
65 }
66
67 const workspaceCwd = threadContext.value.worktreePath ?? threadContext.value.workspaceRoot;
68 if (!workspaceCwd) {
69 return yield* new CheckpointInvariantError({
70 operation,
71 detail: `Workspace path missing for thread '${input.threadId}' when computing turn diff.`,
72 });
73 }
74
75 const fromCheckpointRef =
76 input.fromTurnCount === 0
77 ? checkpointRefForThreadTurn(input.threadId, 0)
78 : threadContext.value.checkpoints.find(
79 (checkpoint) => checkpoint.checkpointTurnCount === input.fromTurnCount,
80 )?.checkpointRef;
81 if (!fromCheckpointRef) {
82 return yield* new CheckpointUnavailableError({
83 threadId: input.threadId,
84 turnCount: input.fromTurnCount,
85 detail: `Checkpoint ref is unavailable for turn ${input.fromTurnCount}.`,
86 });
87 }
88
89 const toCheckpointRef = threadContext.value.checkpoints.find(
90 (checkpoint) => checkpoint.checkpointTurnCount === input.toTurnCount,
91 )?.checkpointRef;
92 if (!toCheckpointRef) {
93 return yield* new CheckpointUnavailableError({
94 threadId: input.threadId,
95 turnCount: input.toTurnCount,
96 detail: `Checkpoint ref is unavailable for turn ${input.toTurnCount}.`,
97 });
98 }
99
100 const [fromExists, toExists] = yield* Effect.all(
101 [
102 checkpointStore.hasCheckpointRef({
103 cwd: workspaceCwd,
104 checkpointRef: fromCheckpointRef,
105 }),
106 checkpointStore.hasCheckpointRef({
107 cwd: workspaceCwd,
108 checkpointRef: toCheckpointRef,
109 }),
110 ],
111 { concurrency: "unbounded" },
112 );
113
114 if (!fromExists) {
115 return yield* new CheckpointUnavailableError({
116 threadId: input.threadId,
117 turnCount: input.fromTurnCount,
118 detail: `Filesystem checkpoint is unavailable for turn ${input.fromTurnCount}.`,
119 });
120 }
121
122 if (!toExists) {
123 return yield* new CheckpointUnavailableError({
124 threadId: input.threadId,
125 turnCount: input.toTurnCount,
126 detail: `Filesystem checkpoint is unavailable for turn ${input.toTurnCount}.`,
127 });
128 }
129
130 const diff = yield* checkpointStore.diffCheckpoints({
131 cwd: workspaceCwd,
132 fromCheckpointRef,
133 toCheckpointRef,
134 fallbackFromToHead: false,
135 ignoreWhitespace,
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
155 const getFullThreadDiff: CheckpointDiffQueryShape["getFullThreadDiff"] = (
156 input: OrchestrationGetFullThreadDiffInput,
157 ) =>
158 getTurnDiff({
159 threadId: input.threadId,
160 fromTurnCount: 0,
161 toTurnCount: input.toTurnCount,
162 ignoreWhitespace: input.ignoreWhitespace ?? true,
163 }).pipe(Effect.map((result): OrchestrationGetFullThreadDiffResult => result));
164
165 return {
166 getTurnDiff,
167 getFullThreadDiff,
168 } satisfies CheckpointDiffQueryShape;
169});
170
171export const CheckpointDiffQueryLive = Layer.effect(CheckpointDiffQuery, make);