The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 08:30 UTC
fix(server): remove one-time scheduled tasks after their turn completes (#37991)
Julius Marminge committed
Nov 17, 2026 at 00:42 UTC
17c142588b090a30ed33439375113891668982b8
2 files changed
+86
-8
apps/server/src/scheduledTasks/ScheduledTaskService.test.ts
+71
@@ -509,6 +509,77 @@ it.effect.each([
509
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
510
);
511
512
+it.effect("a one-time task is removed once its turn completes", () =>
513
+ Effect.gen(function* () {
514
+ yield* TestClock.setTime(Date.parse("2026-11-01T07:00:00.000Z"));
515
+ const projectId = ProjectId.make("project:test");
516
+ const ended = yield* Deferred.make<void>();
517
+ const sent = yield* Ref.make("");
518
+ const run = { id: "run:1", status: "completed", rootNodeId: "node:root" };
519
+ const layer = ScheduledTaskService.layer.pipe(
520
+ Layer.provide(
521
+ Layer.mergeAll(
522
+ ManagedPolicy.layerTest(),
523
+ Layer.mock(ThreadLaunchService.ThreadLaunchService)({}),
524
+ Layer.mock(ThreadManagementService.ThreadManagementService)({
525
+ sendToThread: (input) => Ref.set(sent, input.messageId).pipe(Effect.as({} as never)),
526
+ getThreadShell: () => Effect.succeed(threadShell(projectId)),
527
+ getProjectThreadRecords: () =>
528
+ Effect.map(
529
+ Ref.get(sent),
530
+ (userMessageId) => ({ runs: [{ ...run, userMessageId }] }) as never,
531
+ ),
532
+ waitForThread: (input) =>
533
+ Deferred.await(ended).pipe(
534
+ Effect.as({ threadId: input.threadId, run, timedOut: false } as never),
535
+ ),
536
+ }),
537
+ Layer.mock(SecretRequests.SecretRequests)({}),
538
+ Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
539
+ namedProjectsRoot: "/projects",
540
+ isScratchProject: () => Effect.succeed(false),
541
+ }),
542
+ NodeCrypto.layer,
543
+ Scheduler.layer,
544
+ ),
545
+ ),
546
+ );
547
+ yield* Effect.gen(function* () {
548
+ const service = yield* ScheduledTaskService.ScheduledTaskService;
549
+ const { task } = yield* service.upsert({
550
+ id: ScheduledTaskId.make("task:once"),
551
+ title: "Release notes",
552
+ prompt: "Draft the release notes",
553
+ enabled: true,
554
+ schedule: { type: "once", runAt: "2026-11-01T07:01:00.000Z" },
555
+ projectId,
556
+ threadId: ThreadId.make("thread:once"),
557
+ workspaceStrategy: { type: "root" },
558
+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
559
+ runtimeMode: "full-access",
560
+ interactionMode: "default",
561
+ });
562
+ const dispatched = service.subscribeList().pipe(
563
+ Stream.map(({ tasks }) => tasks.find((candidate) => candidate.id === task.id)),
564
+ Stream.filter((current) => current?.lastRunStatus === "succeeded"),
565
+ Stream.runHead,
566
+ );
567
+ const removed = service.subscribeList().pipe(
568
+ Stream.filter(({ tasks }) => tasks.every((candidate) => candidate.id !== task.id)),
569
+ Stream.runHead,
570
+ );
571
+ const recorded = yield* Effect.forkChild(dispatched);
572
+ const gone = yield* Effect.forkChild(removed);
573
+ yield* TestClock.adjust("2 minutes");
574
+ // The dispatch alone keeps it: the turn can still fail.
575
+ assert.isNull(Option.getOrThrow(yield* Fiber.join(recorded))?.nextRunAt);
576
+ yield* Deferred.succeed(ended, undefined);
577
+ yield* Fiber.join(gone);
578
+ assert.lengthOf((yield* service.list()).tasks, 0);
579
+ }).pipe(Effect.provide(layer));
580
+ }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
581
+);
582
+
583
it.effect("a bound run follows its thread when promotion moves it mid-send", () =>
584
Effect.gen(function* () {
585
const scratch = ProjectId.make("project:scratch");
apps/server/src/scheduledTasks/ScheduledTaskService.ts
+15
-8
@@ -884,8 +884,9 @@ export const layer = Layer.effect(
884
);
885
886
// A dispatch is recorded once the message is accepted; the provider can still
887
- // reject the turn after that, so follow the run and record its failure.
888
- const recordRunFailure = Effect.fn("ScheduledTaskService.recordRunFailure")(function* (
887
+ // reject the turn after that, so follow the run and record its failure. A one-time
888
+ // task whose turn completed is spent, so it is removed; its thread stays.
889
+ const recordRunOutcome = Effect.fn("ScheduledTaskService.recordRunOutcome")(function* (
890
taskId: ScheduledTaskId,
891
startedAtIso: string,
892
threadId: ThreadId,
@@ -902,12 +903,18 @@ export const layer = Layer.effect(
903
runId: dispatched.id,
904
timeoutMs: RUN_OUTCOME_WAIT_MS,
905
});
905
- if (
906
- run === null ||
907
- run.status === "completed" ||
908
- !ThreadManagementService.isTerminalRunStatus(run.status)
909
- )
906
+ if (run === null || !ThreadManagementService.isTerminalRunStatus(run.status)) return;
907
+ if (run.status === "completed") {
908
+ const removed = yield* sql`
909
+ DELETE FROM scheduled_tasks
910
+ WHERE task_id = ${taskId} AND last_run_at = ${startedAtIso}
911
+ AND last_run_status = 'succeeded' AND next_run_at IS NULL
912
+ AND json_extract(schedule_json, '$.type') = 'once'
913
+ RETURNING task_id
914
+ `;
915
+ if (removed.length > 0) yield* notifyChanged;
916
return;
917
+ }
918
const { turnItems, messages } = yield* threadManagement.getProjectThreadRecords(thread, [
919
"turnItems",
920
"messages",
@@ -1176,7 +1183,7 @@ export const layer = Layer.effect(
1183
);
1184
if (written) yield* notifyChanged;
1185
if (written && threadId !== null) {
1179
- yield* recordRunFailure(active.id, startedAtIso, threadId, messageId).pipe(
1186
+ yield* recordRunOutcome(active.id, startedAtIso, threadId, messageId).pipe(
1187
Effect.catchCause((cause) =>
1188
Effect.logWarning("Could not record scheduled run outcome", {
1189
taskId: active.id,