Skip to content

Commit e770f7a

Browse files
committed
fix(server): archived threads no longer start work after setup finishes
Reject prepared-run.release before preparing checkpoints or scheduling provider work when the thread is archived or deleted. Regressions exercise terminal-thread refusal and successful release after unarchive.
1 parent bf7121d commit e770f7a

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Layer from "effect/Layer";
2323
import * as SqlClient from "effect/unstable/sql/SqlClient";
2424
import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
2525
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
26+
import * as EffectOutbox from "./EffectOutbox.ts";
2627
import * as Orchestrator from "./Orchestrator.ts";
2728
import * as ProjectionStore from "./ProjectionStore.ts";
2829
import type { ProviderAdapterV2Shape } from "./ProviderAdapter.ts";
@@ -42,13 +43,94 @@ const database = SqlitePersistenceMemory;
4243
const testLayer = Layer.mergeAll(
4344
database,
4445
ProjectionStore.layer.pipe(Layer.provide(database)),
46+
EffectOutbox.layer.pipe(Layer.provide(database)),
4547
makeOrchestratorV2ReplayLayerWithRegistry(
4648
{ name: "control-reads" },
4749
ProviderAdapterRegistry.makeLayer([adapter]),
4850
{ databaseLayer: database, runEffectWorker: false },
4951
),
5052
);
5153

54+
for (const terminalCommand of ["thread.archive", "thread.delete"] as const) {
55+
it.effect(`rejects prepared-run.release after ${terminalCommand}`, () =>
56+
Effect.gen(function* () {
57+
const orchestrator = yield* Orchestrator.OrchestratorV2;
58+
const projections = yield* ProjectionStore.ProjectionStoreV2;
59+
const outbox = yield* EffectOutbox.EffectOutboxV2;
60+
const threadId = ThreadId.make("thread:terminal-preparation");
61+
yield* orchestrator.dispatch({
62+
type: "thread.create",
63+
commandId: CommandId.make("create-terminal-preparation"),
64+
threadId,
65+
projectId: ProjectId.make("project:terminal-preparation"),
66+
title: "Preparing thread",
67+
modelSelection,
68+
runtimeMode: "full-access",
69+
interactionMode: "default",
70+
branch: null,
71+
worktreePath: null,
72+
createdBy: "user",
73+
creationSource: "web",
74+
});
75+
yield* orchestrator.dispatch({
76+
type: "message.dispatch",
77+
commandId: CommandId.make("prepare-terminal-message"),
78+
threadId,
79+
messageId: MessageId.make("preparing-input"),
80+
text: "Prepare workspace",
81+
attachments: [],
82+
dispatchMode: { type: "defer_start" },
83+
createdBy: "user",
84+
creationSource: "web",
85+
});
86+
yield* orchestrator.dispatch({
87+
type: terminalCommand,
88+
commandId: CommandId.make("close-terminal-preparation"),
89+
threadId,
90+
});
91+
const before = yield* projections.getThreadProjection(threadId);
92+
const runId = before.runs[0]!.id;
93+
const commandId = CommandId.make("release-terminal-preparation");
94+
const error = yield* orchestrator
95+
.dispatch({
96+
type: "prepared-run.release",
97+
commandId,
98+
threadId,
99+
runId,
100+
})
101+
.pipe(Effect.flip);
102+
103+
assert.equal(error._tag, "OrchestratorDispatchError");
104+
assert.equal(error.cause, `Thread ${threadId} is not active.`);
105+
assert.deepEqual(yield* projections.getThreadProjection(threadId), before);
106+
assert.deepEqual(yield* outbox.listByCommandId(commandId), []);
107+
if (terminalCommand === "thread.archive") {
108+
yield* orchestrator.dispatch({
109+
type: "thread.unarchive",
110+
commandId: CommandId.make("unarchive-terminal-preparation"),
111+
threadId,
112+
});
113+
const resumedCommandId = CommandId.make("release-unarchived-preparation");
114+
yield* orchestrator.dispatch({
115+
type: "prepared-run.release",
116+
commandId: resumedCommandId,
117+
threadId,
118+
runId,
119+
});
120+
assert.equal(
121+
(yield* projections.getThreadProjection(threadId)).runs[0]!.status,
122+
"starting",
123+
);
124+
assert.isTrue(
125+
(yield* outbox.listByCommandId(resumedCommandId)).some(
126+
(effect) => effect.request.type === "provider-turn.start",
127+
),
128+
);
129+
}
130+
}).pipe(Effect.provide(testLayer)),
131+
);
132+
}
133+
52134
it.effect(
53135
"dispatches metadata, queue resume and request controls without hydrating unrelated history",
54136
() =>

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7360,6 +7360,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
73607360
["runs", "attempts", "nodes", "providerThreads", "turnItems"],
73617361
{ turnItemTypes: ["command_execution"], turnItemRunId: command.runId },
73627362
);
7363+
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
7364+
return yield* new OrchestratorDispatchError({
7365+
commandId: command.commandId,
7366+
commandType: command.type,
7367+
cause: `Thread ${command.threadId} is not active.`,
7368+
});
7369+
}
73637370
const state = preparedRunState(command, projection);
73647371
if (state === null) {
73657372
return yield* new OrchestratorDispatchError({

0 commit comments

Comments
 (0)