Skip to content

Commit e16fab3

Browse files
sbansal1999claude
andcommitted
fix(server): run_scheduled_task_now returns the thread the run launched
Fresh-thread-per-run tasks have no bound thread, so the tool returned threadId: null. runTask now reports the thread it posted to or launched, and runNow surfaces it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ec80933 commit e16fab3

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎apps/server/src/mcp/toolkits/thread/handlers.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
8585
code: "invalid_request",
8686
message: "The scheduled task was not found.",
8787
});
88-
const { task } = yield* scheduler
88+
const { task, threadId } = yield* scheduler
8989
.runNow({ id: input.taskId })
9090
.pipe(Effect.mapError(unavailable));
9191
return {
9292
taskId: task.id,
93-
threadId: task.threadId,
93+
threadId,
9494
lastRunStatus: task.lastRunStatus,
9595
runCount: task.runCount,
9696
nextRunAt: task.nextRunAt,

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ it.effect.each(
342342
projectThreads.find((candidate) => candidate.id === existing?.threadId) ??
343343
projectThreads[0];
344344
assert.isDefined(thread);
345+
assert.equal(result.threadId, thread!.id);
345346
const projection = yield* threads.getThreadProjection(thread!.id);
346347
// Encoding the persisted projection exercises both message and turn-item wire schemas.
347348
const wire = yield* encodeThreadProjection(projection);

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ export class ScheduledTaskService extends Context.Service<
232232
readonly delete: (
233233
input: ScheduledTaskDeleteInput,
234234
) => Effect.Effect<ScheduledTaskDeleteResult, ScheduledTaskError>;
235+
/** `threadId` is the bound thread, or the thread this run launched (null if the launch failed). */
235236
readonly runNow: (
236237
input: ScheduledTaskRunNowInput,
237-
) => Effect.Effect<ScheduledTaskRunNowResult, ScheduledTaskError>;
238+
) => Effect.Effect<
239+
ScheduledTaskRunNowResult & { readonly threadId: ThreadId | null },
240+
ScheduledTaskError
241+
>;
238242
/** Issues a new URL token for a webhook task; the old URL stops working at once. */
239243
readonly rotateWebhookToken: (
240244
input: ScheduledTaskRotateWebhookTokenInput,
@@ -722,7 +726,7 @@ export const layer = Layer.effect(
722726
if (trigger !== "scheduled") {
723727
return yield* taskError("Schedule task is already running.", { taskId: task.id });
724728
}
725-
return task;
729+
return { task, threadId: null };
726730
}
727731

728732
return yield* Effect.gen(function* () {
@@ -744,7 +748,7 @@ export const layer = Layer.effect(
744748
if (trigger !== "scheduled") {
745749
return yield* taskError("Schedule task not found.", { taskId: task.id });
746750
}
747-
return task;
751+
return { task, threadId: null };
748752
}
749753
// A next_run_at corrupted between the poll read and this re-read must
750754
// not defect the poll; an unparseable value is treated as not due.
@@ -756,7 +760,7 @@ export const layer = Layer.effect(
756760
Option.isNone(parsedNextRunAt) ||
757761
DateTime.toEpochMillis(parsedNextRunAt.value) > DateTime.toEpochMillis(startedAt))
758762
) {
759-
return active;
763+
return { task: active, threadId: null };
760764
}
761765
// A queued delivery must not run a task that was paused, deleted and
762766
// recreated under the same id, or switched to another trigger while
@@ -834,6 +838,12 @@ export const layer = Layer.effect(
834838
const runSucceeded = result._tag === "Success";
835839
const lastRunStatus = runSucceeded ? ("succeeded" as const) : ("failed" as const);
836840
const lastRunError = runSucceeded ? null : errorMessage(result.cause);
841+
const threadId =
842+
active.threadId !== null
843+
? ThreadId.make(active.threadId)
844+
: result._tag === "Success" && "threadId" in result.value
845+
? result.value.threadId
846+
: null;
837847
// Re-read the task so the next run is computed from the schedule as it
838848
// is *now* (the user may have edited or deleted it while we ran).
839849
const current = yield* findTask(task.id);
@@ -861,7 +871,7 @@ export const layer = Layer.effect(
861871
});
862872
yield* notifyChanged;
863873
}
864-
return completed;
874+
return { task: completed, threadId };
865875
}).pipe(
866876
Effect.onError((cause) => releaseStuckRun(task, errorMessage(cause))),
867877
Effect.ensuring(
@@ -1165,12 +1175,11 @@ export const layer = Layer.effect(
11651175
taskId: input.id,
11661176
});
11671177
}
1168-
const next = yield* runTask(task, "manual").pipe(
1178+
return yield* runTask(task, "manual").pipe(
11691179
Effect.mapError((cause) =>
11701180
taskError("Could not run schedule task.", { taskId: input.id, cause }),
11711181
),
11721182
);
1173-
return { task: next };
11741183
});
11751184

11761185
const rotateWebhookToken: ScheduledTaskService["Service"]["rotateWebhookToken"] = (input) =>
@@ -1621,7 +1630,7 @@ export const layer = Layer.effect(
16211630
Metrics.increment(Metrics.webhookRunsTotal, { outcome }),
16221631
]);
16231632
yield* runTask(task, "webhook", { deliveryId, prompt: rendered.prompt }).pipe(
1624-
Effect.flatMap((completed) =>
1633+
Effect.flatMap(({ task: completed }) =>
16251634
completed.lastRunStatus === "failed"
16261635
? runOutcome("failed").pipe(
16271636
Effect.andThen(markDeliveryFailed(deliveryId, "The run failed to start.")),

0 commit comments

Comments
 (0)