Skip to content

Commit 68b5524

Browse files
kevlingoclaude
andcommitted
fix(server): a scheduled task queues behind a busy thread instead of steering into it
Scheduled runs dispatched with mode "auto", which steers into a bound thread's running turn: it cut into the agent's work and aborted an open AskUserQuestion (pingdotgg#15517). They now queue; an idle thread still starts the run at once. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d3287f1 commit 68b5524

3 files changed

Lines changed: 292 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Always available to every agent session, with no setting to turn on:
5858
- A thread waiting on a background test or build can stay Working, and counted in its project's active number, until the command ends: press Wait on the background work strip, or an agent calls `wait_for_background_commands`. Carried from open upstream PR [#15315](https://github.com/pingdotgg/t3code/pull/15315); drop it when that lands.
5959
- Preview tool waits are capped at 45 seconds so they finish inside the agent's 60 second tool-call limit, and a browser host reset by a timeout reports that it is reconnecting instead of telling the agent to give up.
6060
- The desktop window keeps its size and position across every restart on scaled Windows displays, instead of falling back to the default size when Windows nudged it a few pixels past the screen edge.
61+
- A scheduled task bound to a busy thread waits for its turn to end instead of interrupting it.
6162

6263
### Windows test suite
6364

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2+
import { assert, it } from "@effect/vitest";
3+
import {
4+
CommandId,
5+
EventId,
6+
MessageId,
7+
NodeId,
8+
ProjectId,
9+
ProviderDriverKind,
10+
ProviderInstanceId,
11+
ProviderThreadId,
12+
ProviderTurnId,
13+
RunAttemptId,
14+
RunId,
15+
ScheduledTaskId,
16+
ThreadId,
17+
} from "@t3tools/contracts";
18+
import * as DateTime from "effect/DateTime";
19+
import * as Effect from "effect/Effect";
20+
import * as Layer from "effect/Layer";
21+
import * as Option from "effect/Option";
22+
import * as Stream from "effect/Stream";
23+
24+
import { CodexProviderCapabilitiesV2 } from "../orchestration-v2/Adapters/CodexAdapterV2.ts";
25+
import * as EventSink from "../orchestration-v2/EventSink.ts";
26+
import type { ProviderAdapterV2Shape } from "../orchestration-v2/ProviderAdapter.ts";
27+
import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts";
28+
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
29+
import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
30+
import { makeOrchestratorV2ReplayLayerWithRegistry } from "../orchestration-v2/testkit/ProviderReplayHarness.ts";
31+
import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
32+
import * as Scheduler from "../scheduling/Scheduler.ts";
33+
import * as ScheduledTaskService from "./ScheduledTaskService.ts";
34+
35+
// Fork: a scheduled task bound to a thread queues behind that thread's running
36+
// turn instead of steering into it, on a real v2 orchestrator. Provider
37+
// processes never start, so a turn stays running until a test ends it.
38+
39+
const projectId = ProjectId.make("project:scheduled-fork");
40+
const codex = ProviderInstanceId.make("codex");
41+
const modelSelection = { instanceId: codex, model: "gpt-5.5" };
42+
43+
const adapter = {
44+
instanceId: codex,
45+
driver: ProviderDriverKind.make("codex"),
46+
getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
47+
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
48+
openSession: () => Effect.die("provider processes never start in these tests"),
49+
} as ProviderAdapterV2Shape;
50+
51+
const makeHarness = () => {
52+
const database = SqlitePersistenceMemory;
53+
const orchestrator = makeOrchestratorV2ReplayLayerWithRegistry(
54+
{ name: "scheduled-fork" },
55+
ProviderAdapterRegistry.makeLayer([adapter]),
56+
{ databaseLayer: database, runEffectWorker: false },
57+
);
58+
// Merged so a test can write provider events through the real event sink.
59+
const threadManagement = ThreadManagement.layer.pipe(Layer.provideMerge(orchestrator));
60+
const scheduledTasks = ScheduledTaskService.layer.pipe(
61+
Layer.provide(
62+
Layer.mergeAll(
63+
threadManagement,
64+
database,
65+
// Only an unbound task launches a thread.
66+
Layer.mock(ThreadLaunchService.ThreadLaunchService)({}),
67+
NodeCrypto.layer,
68+
Scheduler.layer,
69+
),
70+
),
71+
);
72+
return Layer.mergeAll(scheduledTasks, threadManagement);
73+
};
74+
75+
const createThread = (id: string) =>
76+
Effect.gen(function* () {
77+
const threads = yield* ThreadManagement.ThreadManagementService;
78+
const threadId = ThreadId.make(id);
79+
yield* threads.dispatch({
80+
type: "thread.create",
81+
commandId: CommandId.make(`command:create:${id}`),
82+
threadId,
83+
projectId,
84+
title: id,
85+
modelSelection,
86+
runtimeMode: "full-access",
87+
interactionMode: "default",
88+
branch: null,
89+
worktreePath: null,
90+
createdBy: "user",
91+
creationSource: "web",
92+
});
93+
return threadId;
94+
});
95+
96+
/** Puts a steerable turn on an idle thread, as a provider mid-turn leaves it. */
97+
const runningTurn = (threadId: ThreadId) =>
98+
Effect.gen(function* () {
99+
const sink = yield* EventSink.EventSinkV2;
100+
const now = yield* DateTime.now;
101+
const runId = RunId.make(`run:${threadId}`);
102+
const attemptId = RunAttemptId.make(`attempt:${threadId}`);
103+
const nodeId = NodeId.make(`node:${threadId}`);
104+
const providerThreadId = ProviderThreadId.make(`provider-thread:${threadId}`);
105+
yield* sink.write({
106+
events: [
107+
{
108+
id: EventId.make(`${threadId}:provider-thread`),
109+
type: "provider-thread.updated",
110+
threadId,
111+
occurredAt: now,
112+
payload: {
113+
id: providerThreadId,
114+
driver: ProviderDriverKind.make("codex"),
115+
providerInstanceId: codex,
116+
providerSessionId: null,
117+
appThreadId: threadId,
118+
ownerNodeId: null,
119+
nativeThreadRef: null,
120+
nativeConversationHeadRef: null,
121+
status: "active",
122+
firstRunOrdinal: 1,
123+
lastRunOrdinal: 1,
124+
handoffIds: [],
125+
forkedFrom: null,
126+
createdAt: now,
127+
updatedAt: now,
128+
},
129+
},
130+
{
131+
id: EventId.make(`${threadId}:run`),
132+
type: "run.created",
133+
threadId,
134+
occurredAt: now,
135+
payload: {
136+
id: runId,
137+
threadId,
138+
ordinal: 1,
139+
providerInstanceId: codex,
140+
modelSelection,
141+
providerThreadId,
142+
userMessageId: MessageId.make(`message:${threadId}`),
143+
rootNodeId: nodeId,
144+
activeAttemptId: attemptId,
145+
status: "running",
146+
requestedAt: now,
147+
startedAt: now,
148+
completedAt: null,
149+
checkpointId: null,
150+
contextHandoffId: null,
151+
},
152+
},
153+
{
154+
id: EventId.make(`${threadId}:attempt`),
155+
type: "run-attempt.created",
156+
threadId,
157+
occurredAt: now,
158+
payload: {
159+
id: attemptId,
160+
runId,
161+
attemptOrdinal: 1,
162+
rootNodeId: nodeId,
163+
providerInstanceId: codex,
164+
providerThreadId,
165+
providerTurnId: ProviderTurnId.make(`provider-turn:${threadId}`),
166+
reason: "initial",
167+
status: "running",
168+
startedAt: now,
169+
completedAt: null,
170+
},
171+
},
172+
{
173+
id: EventId.make(`${threadId}:turn`),
174+
type: "provider-turn.updated",
175+
threadId,
176+
occurredAt: now,
177+
payload: {
178+
id: ProviderTurnId.make(`provider-turn:${threadId}`),
179+
providerThreadId,
180+
nodeId,
181+
runAttemptId: attemptId,
182+
nativeTurnRef: null,
183+
ordinal: 1,
184+
status: "running",
185+
startedAt: now,
186+
completedAt: null,
187+
},
188+
},
189+
],
190+
});
191+
return runId;
192+
});
193+
194+
const runBoundTask = (threadId: ThreadId) =>
195+
Effect.gen(function* () {
196+
const tasks = yield* ScheduledTaskService.ScheduledTaskService;
197+
const { task } = yield* tasks.upsert({
198+
id: ScheduledTaskId.make(`scheduled-task:${threadId}`),
199+
title: "Upstream sync",
200+
prompt: "Sync with upstream.",
201+
enabled: false,
202+
schedule: { type: "interval", everyMs: 60_000 },
203+
projectId,
204+
threadId,
205+
workspaceStrategy: { type: "root" },
206+
modelSelection,
207+
runtimeMode: "full-access",
208+
interactionMode: "default",
209+
createdBy: "user",
210+
creationSource: "web",
211+
});
212+
// A due run and a manual one take the same dispatch path.
213+
return (yield* tasks.runNow({ id: task.id })).task;
214+
});
215+
216+
it.effect("queues a bound task behind the thread's running turn, which then starts it", () =>
217+
Effect.gen(function* () {
218+
const threads = yield* ThreadManagement.ThreadManagementService;
219+
const sink = yield* EventSink.EventSinkV2;
220+
const threadId = yield* createThread("thread:busy");
221+
const activeRunId = yield* runningTurn(threadId);
222+
223+
// A steer here would need the turn's live provider session, which no
224+
// process provides, so the run would fail instead of succeeding.
225+
const ran = yield* runBoundTask(threadId);
226+
assert.equal(ran.lastRunStatus, "succeeded");
227+
const queued = yield* threads.getThreadProjection(threadId);
228+
assert.deepEqual(
229+
queued.runs.map((run) => [run.id === activeRunId, run.status]),
230+
[
231+
[true, "running"],
232+
[false, "queued"],
233+
],
234+
);
235+
assert.deepEqual(
236+
queued.providerTurns.map((turn) => turn.status),
237+
["running"],
238+
);
239+
const scheduledRun = queued.runs.find((run) => run.id !== activeRunId)!;
240+
assert.equal(
241+
queued.messages.find((message) => message.id === scheduledRun.userMessageId)?.scheduledTaskId,
242+
ran.id,
243+
);
244+
245+
// The turn ends, and the queue starts the scheduled run.
246+
const activeRun = queued.runs.find((run) => run.id === activeRunId)!;
247+
const afterSequence = yield* sink.latestSequence({ threadId });
248+
const now = yield* DateTime.now;
249+
yield* sink.write({
250+
events: [
251+
{
252+
id: EventId.make(`${threadId}:run-completed`),
253+
type: "run.updated",
254+
threadId,
255+
occurredAt: now,
256+
payload: { ...activeRun, status: "completed", completedAt: now },
257+
},
258+
],
259+
});
260+
const promoted = yield* sink.stream({ threadId, afterSequence, eventType: "run.updated" }).pipe(
261+
Stream.filter(
262+
(stored) =>
263+
stored.event.type === "run.updated" && stored.event.payload.id === scheduledRun.id,
264+
),
265+
Stream.runHead,
266+
);
267+
assert.isTrue(Option.isSome(promoted));
268+
const started = yield* threads.getThreadProjection(threadId);
269+
assert.notEqual(started.runs.find((run) => run.id === scheduledRun.id)?.status, "queued");
270+
}).pipe(Effect.provide(makeHarness())),
271+
);
272+
273+
it.effect("starts a bound task's run at once on an idle thread", () =>
274+
Effect.gen(function* () {
275+
const threads = yield* ThreadManagement.ThreadManagementService;
276+
const threadId = yield* createThread("thread:idle");
277+
278+
const ran = yield* runBoundTask(threadId);
279+
assert.equal(ran.lastRunStatus, "succeeded");
280+
const projection = yield* threads.getThreadProjection(threadId);
281+
assert.equal(projection.runs.length, 1);
282+
assert.notEqual(projection.runs[0]?.status, "queued");
283+
assert.equal(
284+
projection.messages.find((message) => message.id === projection.runs[0]?.userMessageId)
285+
?.scheduledTaskId,
286+
ran.id,
287+
);
288+
}).pipe(Effect.provide(makeHarness())),
289+
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ export const layer = Layer.effect(
545545
text: prompt,
546546
attachments: [],
547547
modelSelection: active.modelSelection,
548-
mode: "auto",
548+
// Fork: queue, never steer; a steer cuts into the turn (upstream #15517).
549+
mode: "queue",
549550
createdBy: active.createdBy,
550551
creationSource: active.creationSource,
551552
}),

0 commit comments

Comments
 (0)