@devries-cameron20 / t3code-1 / commits / 4a3be943c9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): retain scheduled dispatch refusal reasons (#32115)

Co-authored-by: marcel <marcel@marcels-MacBook-Air.local> Co-authored-by: Julius Marminge <julius0216@outlook.com>

maco committed Nov 15, 2026 at 05:08 UTC 4a3be943c9179b7dffe21e4f9e817a318e3736d1
2 files changed +75 -1
apps/server/src/scheduledTasks/ScheduledTaskService.test.ts
+63
@@ -4,6 +4,7 @@ import * as NodeUtil from "node:util";
4 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
5 import { assert, it, vi } from "@effect/vitest";
6 import {
7 + CommandId,
8 ProjectId,
9 ProviderInstanceId,
10 ScheduledTaskError,
@@ -29,6 +30,7 @@ import * as TestClock from "effect/testing/TestClock";
30
31 import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
32 import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
33 +import { OrchestratorDispatchError } from "../orchestration-v2/Orchestrator.ts";
34 import * as SecretRequests from "../secrets/SecretRequests.ts";
35 import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts";
36 import * as SqlitePersistence from "../persistence/Sqlite.ts";
@@ -663,6 +665,67 @@ it.effect("a fresh-session task launches one thread, then starts each later run
665 }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
666 );
667
668 +it.effect("a refused dispatch records its refusal, and opaque causes stay generic", () =>
669 + Effect.gen(function* () {
670 + const refusal = "Thread thread:refused was stopped.";
671 + const refused = (cause: unknown) =>
672 + new OrchestratorDispatchError({
673 + commandId: CommandId.make("command:refused"),
674 + commandType: "message.dispatch",
675 + cause,
676 + });
677 + const generic = refused(undefined).message;
678 + const cases = [
679 + [refusal, refusal],
680 + [" ", generic],
681 + [new Error("private-nested-secret"), generic],
682 + ] as const;
683 + const failure = yield* Ref.make<unknown>(undefined);
684 + const layer = ScheduledTaskService.layer.pipe(
685 + Layer.provide(
686 + Layer.mergeAll(
687 + ManagedPolicy.layerTest(),
688 + Layer.mock(ThreadLaunchService.ThreadLaunchService)({}),
689 + Layer.mock(ThreadManagementService.ThreadManagementService)({
690 + getThreadShell: () => Effect.succeed(threadShell(ProjectId.make("project:test"))),
691 + sendToThread: () =>
692 + Ref.get(failure).pipe(Effect.flatMap((cause) => Effect.fail(refused(cause)))),
693 + }),
694 + Layer.mock(SecretRequests.SecretRequests)({}),
695 + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
696 + namedProjectsRoot: "/projects",
697 + isScratchProject: () => Effect.succeed(false),
698 + }),
699 + NodeCrypto.layer,
700 + Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void }),
701 + ),
702 + ),
703 + );
704 + yield* Effect.gen(function* () {
705 + const service = yield* ScheduledTaskService.ScheduledTaskService;
706 + const { task } = yield* service.upsert({
707 + id: ScheduledTaskId.make("task:refused"),
708 + title: "Refused dispatch",
709 + prompt: "Check progress",
710 + enabled: true,
711 + schedule: { type: "interval", everyMs: 60_000 },
712 + projectId: ProjectId.make("project:test"),
713 + threadId: ThreadId.make("thread:refused"),
714 + workspaceStrategy: { type: "root" },
715 + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
716 + runtimeMode: "full-access",
717 + interactionMode: "default",
718 + });
719 + for (const [cause, expected] of cases) {
720 + yield* Ref.set(failure, cause);
721 + const result = yield* service.runNow({ id: task.id });
722 + assert.equal(result.task.lastRunStatus, "failed");
723 + assert.equal(result.task.lastRunError, expected);
724 + }
725 + }).pipe(Effect.provide(layer));
726 + }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
727 +);
728 +
729 it.effect("a first run whose thread was deleted before it bound removes the task", () =>
730 Effect.gen(function* () {
731 const layer = ScheduledTaskService.layer.pipe(
apps/server/src/scheduledTasks/ScheduledTaskService.ts
+12 -1
@@ -53,6 +53,8 @@ import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts
53 import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts";
54 import * as Metrics from "../observability/Metrics.ts";
55 import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
56 +import { OrchestratorDispatchError } from "../orchestration-v2/Orchestrator.ts";
57 +import { userFacingDispatchErrorMessage } from "../orchestration-v2/UserFacingErrors.ts";
58 import * as SecretRequests from "../secrets/SecretRequests.ts";
59 import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
60 import * as DueHint from "../scheduling/DueHint.ts";
@@ -79,6 +81,7 @@ import { constantTimeEquals, verifyWebhookSignature } from "./webhookVerificatio
81
82 export const WEBHOOK_ROUTE_PREFIX = "/api/hooks";
83 const isScheduledTaskError = Schema.is(ScheduledTaskError);
84 +const isOrchestratorDispatchError = Schema.is(OrchestratorDispatchError);
85 const PROMPT_TOO_LONG = "The filled-in prompt is too long.";
86 const WEBHOOK_DELIVERY_RETENTION = 50;
87 const WEBHOOK_DEDUPE_DELIVERY_RETENTION = 20;
@@ -364,6 +367,14 @@ function errorMessage(error: unknown): string {
367 return failure instanceof Error ? failure.message : String(failure);
368 }
369
370 +function dispatchErrorMessage(cause: Cause.Cause<unknown>): string {
371 + const failure = Cause.squash(cause);
372 + // Direct orchestration refusals are user-facing; nested defects can contain private data.
373 + return isOrchestratorDispatchError(failure) && typeof failure.cause === "string"
374 + ? (userFacingDispatchErrorMessage(failure.cause) ?? errorMessage(failure))
375 + : errorMessage(failure);
376 +}
377 +
378 function webhookPath(taskId: string, token: string): string {
379 return `${WEBHOOK_ROUTE_PREFIX}/${encodeURIComponent(taskId)}/${token}`;
380 }
@@ -1098,7 +1109,7 @@ export const layer = Layer.effect(
1109 if (webhook !== undefined && runSucceeded) yield* Ref.set(webhook.dispatched, true);
1110 const completedAt = yield* localNow;
1111 const lastRunStatus = runSucceeded ? ("succeeded" as const) : ("failed" as const);
1101 - const lastRunError = runSucceeded ? null : errorMessage(result.cause);
1112 + const lastRunError = runSucceeded ? null : dispatchErrorMessage(result.cause);
1113 const threadId = runSucceeded ? result.value : null;
1114 // One transaction, so an edit cannot land between reading the schedule and
1115 // writing the next run computed from it. The origin is read first: it reads