Skip to content

Commit f5ae53f

Browse files
voida6claude
andcommitted
fix(server): giving up on a catch-up read forgets only that run
The catch-up failure branch dropped the task's tracked run, which after a Run now during the retries is the newer run. Forget the one message instead. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 229911c commit f5ae53f

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎apps/server/src/scheduledTasks/ScheduledTaskService.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ const makeTurnHarness = (
503503
const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
504504
const dispatched = yield* Queue.unbounded<MessageId>();
505505
const subscriptions = yield* Ref.make(0);
506+
const failedReads = yield* Queue.unbounded<void>();
506507
const layer = ScheduledTaskService.layer.pipe(
507508
Layer.provide(
508509
Layer.mergeAll(
@@ -515,8 +516,10 @@ const makeTurnHarness = (
515516
),
516517
getThreadRecords: (_threadId, fields: ReadonlyArray<string>, filter) =>
517518
Effect.gen(function* () {
518-
if (options.recordsUnreadable)
519+
if (options.recordsUnreadable) {
520+
yield* Queue.offer(failedReads, undefined);
519521
return yield* Effect.fail(new Error("no records") as never);
522+
}
520523
if (fields.includes("runs")) yield* options.duringRunsRead?.(events) ?? Effect.void;
521524
return {
522525
thread: { deletedAt: options.threadDeleted ? epoch : null },
@@ -551,7 +554,7 @@ const makeTurnHarness = (
551554
),
552555
),
553556
);
554-
return { events, dispatched, subscriptions, layer };
557+
return { events, dispatched, subscriptions, failedReads, layer };
555558
});
556559

557560
const upsertBoundTask = (
@@ -879,3 +882,24 @@ it.effect("a thread deleted while the stream was down fails its run on catch-up"
879882
}).pipe(Effect.provide(harness.layer));
880883
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
881884
);
885+
886+
it.effect("giving up on a catch-up read leaves a newer run of the same task tracked", () =>
887+
Effect.gen(function* () {
888+
const harness = yield* makeTurnHarness({ recordsUnreadable: true });
889+
yield* Effect.gen(function* () {
890+
const service = yield* ScheduledTaskService.ScheduledTaskService;
891+
const { task } = yield* upsertBoundTask(service);
892+
yield* service.runNow({ id: task.id });
893+
yield* Queue.take(harness.dispatched);
894+
// The listener's catch-up for that run starts, fails its first read and backs off.
895+
yield* Queue.take(harness.failedReads);
896+
yield* TestClock.adjust("1 second");
897+
yield* service.runNow({ id: task.id });
898+
const newer = yield* Queue.take(harness.dispatched);
899+
// The catch-up for the first run runs out of retries and gives up.
900+
yield* TestClock.adjust("30 seconds");
901+
yield* Queue.offer(harness.events, runEvent(runOf("run:newer", newer, "completed")));
902+
yield* untilStatus(service, task.id, "succeeded");
903+
}).pipe(Effect.provide(harness.layer));
904+
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
905+
);

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ export const layer = Layer.effect(
11961196
cause,
11971197
}).pipe(
11981198
Effect.andThen(
1199-
Effect.sync(() => trackedRuns.forgetTask(pending.taskId)).pipe(
1199+
Effect.sync(() => trackedRuns.forgetMessage(pending.messageId)).pipe(
12001200
Effect.andThen(releaseRun(pending, "Could not read how the run ended.")),
12011201
),
12021202
),

‎apps/server/src/scheduledTasks/TrackedRuns.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export function makeTrackedRuns() {
106106
});
107107
},
108108
forgetTask,
109+
/** Drops one run only, for a caller whose task may have started a newer run since. */
110+
forgetMessage: (messageId: MessageId) => {
111+
runs.delete(messageId);
112+
},
109113
/**
110114
* Marks the dispatch bookkeeping as saved, and returns the outcome if the
111115
* run already ended while that was happening.

0 commit comments

Comments
 (0)