Skip to content

Commit f7af0ec

Browse files
committed
Fixed checkpoint diff being unavailable
1 parent 53ad7bf commit f7af0ec

4 files changed

Lines changed: 120 additions & 50 deletions

File tree

‎apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function makeSnapshot(input: {
2222
readonly worktreePath: string | null;
2323
readonly checkpointTurnCount: number;
2424
readonly checkpointRef: CheckpointRef;
25+
readonly checkpointStatus?: "ready" | "missing" | "error";
2526
}): OrchestrationReadModel {
2627
return {
2728
snapshotSequence: 0,
@@ -67,7 +68,7 @@ function makeSnapshot(input: {
6768
turnId: TurnId.makeUnsafe("turn-1"),
6869
checkpointTurnCount: input.checkpointTurnCount,
6970
checkpointRef: input.checkpointRef,
70-
status: "ready",
71+
status: input.checkpointStatus ?? "ready",
7172
files: [],
7273
assistantMessageId: null,
7374
completedAt: "2026-01-01T00:00:00.000Z",
@@ -194,4 +195,57 @@ describe("CheckpointDiffQueryLive", () => {
194195
),
195196
).rejects.toThrow("Thread 'thread-missing' not found.");
196197
});
198+
199+
it("returns an empty diff when a non-ready checkpoint ref is absent on disk", async () => {
200+
const projectId = ProjectId.makeUnsafe("project-1");
201+
const threadId = ThreadId.makeUnsafe("thread-1");
202+
const toCheckpointRef = CheckpointRef.makeUnsafe("provider-diff:evt-turn-diff-updated");
203+
204+
const snapshot = makeSnapshot({
205+
projectId,
206+
threadId,
207+
workspaceRoot: "/tmp/workspace",
208+
worktreePath: null,
209+
checkpointTurnCount: 1,
210+
checkpointRef: toCheckpointRef,
211+
checkpointStatus: "missing",
212+
});
213+
214+
const checkpointStore: CheckpointStoreShape = {
215+
isGitRepository: () => Effect.succeed(true),
216+
captureCheckpoint: () => Effect.void,
217+
hasCheckpointRef: ({ checkpointRef }) =>
218+
Effect.succeed(checkpointRef !== toCheckpointRef),
219+
restoreCheckpoint: () => Effect.succeed(true),
220+
diffCheckpoints: () => Effect.succeed("diff should not be read"),
221+
deleteCheckpointRefs: () => Effect.void,
222+
};
223+
224+
const layer = CheckpointDiffQueryLive.pipe(
225+
Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
226+
Layer.provideMerge(
227+
Layer.succeed(ProjectionSnapshotQuery, {
228+
getSnapshot: () => Effect.succeed(snapshot),
229+
}),
230+
),
231+
);
232+
233+
const result = await Effect.runPromise(
234+
Effect.gen(function* () {
235+
const query = yield* CheckpointDiffQuery;
236+
return yield* query.getTurnDiff({
237+
threadId,
238+
fromTurnCount: 0,
239+
toTurnCount: 1,
240+
});
241+
}).pipe(Effect.provide(layer)),
242+
);
243+
244+
expect(result).toEqual({
245+
threadId,
246+
fromTurnCount: 0,
247+
toTurnCount: 1,
248+
diff: "",
249+
});
250+
});
197251
});

‎apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,28 @@ const make = Effect.gen(function* () {
2424
const getTurnDiff: CheckpointDiffQueryShape["getTurnDiff"] = (input) =>
2525
Effect.gen(function* () {
2626
const operation = "CheckpointDiffQuery.getTurnDiff";
27+
const emptyTurnDiffResult = (
28+
fromTurnCount: number,
29+
toTurnCount: number,
30+
): Effect.Effect<OrchestrationGetTurnDiffResultType, CheckpointInvariantError> =>
31+
Effect.gen(function* () {
32+
const emptyDiff: OrchestrationGetTurnDiffResultType = {
33+
threadId: input.threadId,
34+
fromTurnCount,
35+
toTurnCount,
36+
diff: "",
37+
};
38+
if (!isTurnDiffResult(emptyDiff)) {
39+
return yield* new CheckpointInvariantError({
40+
operation,
41+
detail: "Computed turn diff result does not satisfy contract schema.",
42+
});
43+
}
44+
return emptyDiff;
45+
});
2746

2847
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;
48+
return yield* emptyTurnDiffResult(input.fromTurnCount, input.toTurnCount);
4249
}
4350

4451
const snapshot = yield* projectionSnapshotQuery.getSnapshot();
@@ -73,12 +80,16 @@ const make = Effect.gen(function* () {
7380
});
7481
}
7582

76-
const fromCheckpointRef =
83+
const fromCheckpoint =
7784
input.fromTurnCount === 0
78-
? checkpointRefForThreadTurn(input.threadId, 0)
85+
? undefined
7986
: thread.checkpoints.find(
8087
(checkpoint) => checkpoint.checkpointTurnCount === input.fromTurnCount,
81-
)?.checkpointRef;
88+
);
89+
const fromCheckpointRef =
90+
input.fromTurnCount === 0
91+
? checkpointRefForThreadTurn(input.threadId, 0)
92+
: fromCheckpoint?.checkpointRef;
8293
if (!fromCheckpointRef) {
8394
return yield* new CheckpointUnavailableError({
8495
threadId: input.threadId,
@@ -87,9 +98,10 @@ const make = Effect.gen(function* () {
8798
});
8899
}
89100

90-
const toCheckpointRef = thread.checkpoints.find(
101+
const toCheckpoint = thread.checkpoints.find(
91102
(checkpoint) => checkpoint.checkpointTurnCount === input.toTurnCount,
92-
)?.checkpointRef;
103+
);
104+
const toCheckpointRef = toCheckpoint?.checkpointRef;
93105
if (!toCheckpointRef) {
94106
return yield* new CheckpointUnavailableError({
95107
threadId: input.threadId,
@@ -113,6 +125,9 @@ const make = Effect.gen(function* () {
113125
);
114126

115127
if (!fromExists) {
128+
if (input.fromTurnCount > 0 && fromCheckpoint && fromCheckpoint.status !== "ready") {
129+
return yield* emptyTurnDiffResult(input.fromTurnCount, input.toTurnCount);
130+
}
116131
return yield* new CheckpointUnavailableError({
117132
threadId: input.threadId,
118133
turnCount: input.fromTurnCount,
@@ -121,6 +136,9 @@ const make = Effect.gen(function* () {
121136
}
122137

123138
if (!toExists) {
139+
if (toCheckpoint && toCheckpoint.status !== "ready") {
140+
return yield* emptyTurnDiffResult(input.fromTurnCount, input.toTurnCount);
141+
}
124142
return yield* new CheckpointUnavailableError({
125143
threadId: input.threadId,
126144
turnCount: input.toTurnCount,

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ type ProviderRuntimeTestThread = ProviderRuntimeTestReadModel["threads"][number]
104104
type ProviderRuntimeTestMessage = ProviderRuntimeTestThread["messages"][number];
105105
type ProviderRuntimeTestProposedPlan = ProviderRuntimeTestThread["proposedPlans"][number];
106106
type ProviderRuntimeTestActivity = ProviderRuntimeTestThread["activities"][number];
107-
type ProviderRuntimeTestCheckpoint = ProviderRuntimeTestThread["checkpoints"][number];
108107

109108
describe("ProviderRuntimeIngestion", () => {
110109
let runtime: ManagedRuntime.ManagedRuntime<
@@ -1214,8 +1213,8 @@ describe("ProviderRuntimeIngestion", () => {
12141213
entry.activities.some(
12151214
(activity: ProviderRuntimeTestActivity) => activity.kind === "runtime.warning",
12161215
) &&
1217-
entry.checkpoints.some(
1218-
(checkpoint: ProviderRuntimeTestCheckpoint) => checkpoint.turnId === "turn-p1",
1216+
entry.activities.some(
1217+
(activity: ProviderRuntimeTestActivity) => activity.kind === "turn.diff.updated",
12191218
),
12201219
);
12211220

@@ -1252,12 +1251,16 @@ describe("ProviderRuntimeIngestion", () => {
12521251
expect(warning?.kind).toBe("runtime.warning");
12531252
expect(warningPayload?.message).toBe("Provider got slow");
12541253

1255-
const checkpoint = thread.checkpoints.find(
1256-
(entry: ProviderRuntimeTestCheckpoint) => entry.turnId === "turn-p1",
1254+
const turnDiffActivity = thread.activities.find(
1255+
(activity: ProviderRuntimeTestActivity) => activity.id === "evt-turn-diff-updated",
12571256
);
1258-
expect(checkpoint?.status).toBe("missing");
1259-
expect(checkpoint?.assistantMessageId).toBe("assistant:item-p1-assistant");
1260-
expect(checkpoint?.checkpointRef).toBe("provider-diff:evt-turn-diff-updated");
1257+
const turnDiffPayload =
1258+
turnDiffActivity?.payload && typeof turnDiffActivity.payload === "object"
1259+
? (turnDiffActivity.payload as Record<string, unknown>)
1260+
: undefined;
1261+
expect(turnDiffActivity?.kind).toBe("turn.diff.updated");
1262+
expect(turnDiffPayload?.diffBytes).toBe(40);
1263+
expect(thread.checkpoints).toHaveLength(0);
12611264
});
12621265

12631266
it("projects Codex task lifecycle chunks into thread activities", async () => {

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
CommandId,
55
MessageId,
66
type OrchestrationEvent,
7-
CheckpointRef,
87
ThreadId,
98
TurnId,
109
type OrchestrationThreadActivity,
@@ -304,6 +303,24 @@ function runtimeEventToActivities(
304303
];
305304
}
306305

306+
case "turn.diff.updated": {
307+
const diffBytes = event.payload.unifiedDiff.length;
308+
return [
309+
{
310+
id: event.eventId,
311+
createdAt: event.createdAt,
312+
tone: "info",
313+
kind: "turn.diff.updated",
314+
summary: diffBytes > 0 ? "Turn diff updated" : "Turn diff cleared",
315+
payload: {
316+
diffBytes,
317+
},
318+
turnId: toTurnId(event.turnId) ?? null,
319+
...maybeSequence,
320+
},
321+
];
322+
}
323+
307324
case "user-input.requested": {
308325
return [
309326
{
@@ -1039,28 +1056,6 @@ const make = Effect.gen(function* () {
10391056
});
10401057
}
10411058

1042-
if (event.type === "turn.diff.updated") {
1043-
const turnId = toTurnId(event.turnId);
1044-
if (turnId) {
1045-
const assistantMessageId = MessageId.makeUnsafe(
1046-
`assistant:${event.itemId ?? event.turnId ?? event.eventId}`,
1047-
);
1048-
yield* orchestrationEngine.dispatch({
1049-
type: "thread.turn.diff.complete",
1050-
commandId: providerCommandId(event, "thread-turn-diff-complete"),
1051-
threadId: thread.id,
1052-
turnId,
1053-
completedAt: now,
1054-
checkpointRef: CheckpointRef.makeUnsafe(`provider-diff:${event.eventId}`),
1055-
status: "missing",
1056-
files: [],
1057-
assistantMessageId,
1058-
checkpointTurnCount: thread.checkpoints.length + 1,
1059-
createdAt: now,
1060-
});
1061-
}
1062-
}
1063-
10641059
const activities = runtimeEventToActivities(event);
10651060
yield* Effect.forEach(activities, (activity) =>
10661061
orchestrationEngine.dispatch({

0 commit comments

Comments
 (0)