Skip to content

Commit 1b09dda

Browse files
some-du6eclaude
andcommitted
refactor(server): only settled threads move between projects
Settling already rejects running, queued and blocked work, ends PR watches and detaches provider sessions, so the move guard no longer repeats those checks. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9a40ecf commit 1b09dda

3 files changed

Lines changed: 31 additions & 70 deletions

File tree

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

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,45 +2947,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
29472947
: undefined;
29482948
if (targetProjectId !== undefined) {
29492949
const project = yield* projects.get(targetProjectId).pipe(mapDispatchError(command));
2950-
if (Option.isNone(project) || project.value.deletedAt !== null) {
2951-
return yield* new OrchestratorDispatchError({
2952-
commandId: command.commandId,
2953-
commandType: command.type,
2954-
cause: `Project ${targetProjectId} does not exist.`,
2955-
});
2956-
}
2957-
const records = yield* projectionStore
2958-
.getThreadRecords(
2959-
command.threadId,
2960-
["runs", "runtimeRequests", "subagents", "providerThreads", "turnItems"],
2961-
{
2962-
turnItemTypes: ["command_execution", "dynamic_tool", "subagent"],
2963-
turnItemStatuses: ["pending", "running", "waiting"],
2964-
},
2965-
)
2950+
const { subagents } = yield* projectionStore
2951+
.getThreadRecords(command.threadId, ["subagents"])
29662952
.pipe(mapDispatchError(command));
2967-
const latestRun = latestExecutedRun(records.runs);
2968-
// Children share the project; failed worktree setup would retry in the old repo.
29692953
if (
2954+
Option.isNone(project) ||
2955+
project.value.deletedAt !== null ||
2956+
thread.settledAt === null ||
29702957
thread.lineage.relationshipToParent === "subagent" ||
2971-
records.subagents.some((task) => task.childThreadId !== null) ||
2972-
records.runs.some((run) => isBlockingRun(run) || run.status === "queued") ||
2973-
records.runtimeRequests.some((request) => request.status === "pending") ||
2974-
derivePendingBackgroundWork({
2975-
latestRun: records.runs.findLast((run) => run.status !== "queued"),
2976-
providerThreads: records.providerThreads,
2977-
turnItems: records.turnItems,
2978-
activeProviderThreadId: thread.activeProviderThreadId,
2979-
runs: records.runs,
2980-
}).length > 0 ||
2981-
((latestRun?.status === "failed" || latestRun?.status === "interrupted") &&
2982-
latestRun.workspacePreparation?.type === "worktree" &&
2983-
thread.worktreePath === null)
2958+
subagents.some((task) => task.childThreadId !== null)
29842959
) {
29852960
return yield* new OrchestratorDispatchError({
29862961
commandId: command.commandId,
29872962
commandType: command.type,
2988-
cause: `Thread ${command.threadId} must be idle, top-level, and without child threads or failed worktree setup to move.`,
2963+
cause: `Only a settled top-level thread without child threads can move to an existing project.`,
29892964
});
29902965
}
29912966
}
@@ -3145,18 +3120,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
31453120
}
31463121
}
31473122

3148-
const changesWorkspace =
3149-
targetProjectId !== undefined ||
3150-
(command.type === "thread.metadata.update" &&
3151-
command.worktreePath !== undefined &&
3152-
command.worktreePath !== thread.worktreePath);
31533123
const needsProviderState =
31543124
command.type === "thread.runtime-mode.set" ||
31553125
command.type === "thread.model-selection.set" ||
31563126
command.type === "provider.switch" ||
31573127
command.type === "thread.archive" ||
31583128
command.type === "thread.settle" ||
3159-
changesWorkspace;
3129+
(command.type === "thread.metadata.update" &&
3130+
command.worktreePath !== undefined &&
3131+
command.worktreePath !== thread.worktreePath);
31603132
const providerContext = needsProviderState
31613133
? yield* projectionStore
31623134
.getThreadProviderContext(
@@ -3417,7 +3389,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
34173389
snooze: command.limitRecovery.snooze ?? previousRecovery?.snooze ?? false,
34183390
requestId: command.commandId,
34193391
};
3420-
const updated: OrchestrationV2AppThread = {
3392+
return {
34213393
...thread,
34223394
...(command.title === undefined ? {} : { title: command.title }),
34233395
...(command.limitRecovery === undefined ? {} : { limitRecovery }),
@@ -3475,15 +3447,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
34753447
: {}),
34763448
updatedAt: now,
34773449
};
3478-
// Watches read through the project's checkout, which no longer holds these links.
3479-
return targetProjectId !== undefined
3480-
? {
3481-
...updated,
3482-
pullRequests: updated.pullRequests?.map((link) =>
3483-
withPullRequestWatch(link, undefined),
3484-
),
3485-
}
3486-
: updated;
34873450
}
34883451
case "thread.pull-request.link":
34893452
case "thread.pull-request.unlink":
@@ -3915,7 +3878,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39153878
const detachSessionIds = new Set(
39163879
command.type === "thread.archive" || command.type === "thread.settle"
39173880
? (providerContext?.providerSessions ?? []).map((session) => session.id)
3918-
: changesWorkspace
3881+
: command.type === "thread.metadata.update" &&
3882+
command.worktreePath !== undefined &&
3883+
command.worktreePath !== thread.worktreePath
39193884
? (providerContext?.providerSessions ?? []).map((session) => session.id)
39203885
: command.type === "thread.runtime-mode.set"
39213886
? (providerContext?.providerSessions ?? [])

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,19 +1872,21 @@ it.layer(layerLegacyImportTest)("OrchestrationV2 legacy import", (it) => {
18721872
});
18731873

18741874
it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
1875-
it.effect("moves an idle thread to another project", () =>
1875+
it.effect("moves a settled thread to another project", () =>
18761876
Effect.gen(function* () {
18771877
const orchestrator = yield* Orchestrator.OrchestratorV2;
18781878
const threadId = ThreadId.make("runtime-layer-move-thread");
18791879
const fromProjectId = ProjectId.make("runtime-layer-move-from");
18801880
const toProjectId = ProjectId.make("runtime-layer-move-to");
1881-
yield* seedProject({
1882-
projectId: toProjectId,
1883-
title: "Move target",
1884-
workspaceRoot: "/workspace/move-to",
1885-
defaultModelSelection: null,
1886-
createdAt: "2026-09-07T00:00:00.000Z",
1887-
});
1881+
for (const projectId of [fromProjectId, toProjectId]) {
1882+
yield* seedProject({
1883+
projectId,
1884+
title: projectId,
1885+
workspaceRoot: `/workspace/${projectId}`,
1886+
defaultModelSelection: null,
1887+
createdAt: "2026-09-07T00:00:00.000Z",
1888+
});
1889+
}
18881890
yield* orchestrator.dispatch({
18891891
type: "thread.create",
18901892
createdBy: "user",
@@ -1900,14 +1902,9 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19001902
worktreePath: "/workspace/move-from-worktree",
19011903
});
19021904
yield* orchestrator.dispatch({
1903-
type: "thread.pull-request.watch",
1904-
commandId: CommandId.make("runtime-layer-move-watch"),
1905+
type: "thread.settle",
1906+
commandId: CommandId.make("runtime-layer-move-settle"),
19051907
threadId,
1906-
host: "github.com",
1907-
repository: "pingdotgg/t3code",
1908-
number: 1,
1909-
watching: true,
1910-
link: { url: "https://github.com/pingdotgg/t3code/pull/1", source: "manual" },
19111908
});
19121909
yield* orchestrator.dispatch({
19131910
type: "thread.metadata.update",
@@ -1917,7 +1914,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19171914
});
19181915
const moved = (yield* orchestrator.getThreadProjection(threadId)).thread;
19191916
assert.deepInclude(moved, { projectId: toProjectId, branch: null, worktreePath: null });
1920-
assert.isUndefined(moved.pullRequests?.[0]?.watch);
19211917
const shell = yield* orchestrator.getShellSnapshot();
19221918
assert.equal(
19231919
shell.threads.find((candidate) => candidate.id === threadId)?.projectId,
@@ -1930,20 +1926,21 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19301926
commandId: CommandId.make("runtime-layer-move-message"),
19311927
threadId,
19321928
messageId: MessageId.make("runtime-layer-move-message"),
1933-
text: "Keep this run active.",
1929+
text: "Unsettle this thread.",
19341930
attachments: [],
19351931
modelSelection,
19361932
dispatchMode: { type: "start_immediately" },
19371933
});
1938-
const busy = yield* orchestrator
1934+
assert.isNull((yield* orchestrator.getThreadProjection(threadId)).thread.settledAt);
1935+
const unsettled = yield* orchestrator
19391936
.dispatch({
19401937
type: "thread.metadata.update",
1941-
commandId: CommandId.make("runtime-layer-move-busy"),
1938+
commandId: CommandId.make("runtime-layer-move-unsettled"),
19421939
threadId,
19431940
projectId: fromProjectId,
19441941
})
19451942
.pipe(Effect.flip);
1946-
assert.instanceOf(busy, Orchestrator.OrchestratorDispatchError);
1943+
assert.instanceOf(unsettled, Orchestrator.OrchestratorDispatchError);
19471944
}),
19481945
);
19491946

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2893,7 +2893,6 @@ export const OrchestrationV2Command = Schema.Union([
28932893
threadId: ThreadId,
28942894
title: Schema.optional(TrimmedNonEmptyString),
28952895
regenerateTitle: Schema.optional(Schema.Boolean),
2896-
/** Move an idle thread to another project; clears its branch and worktree. */
28972896
projectId: Schema.optional(ProjectId),
28982897
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
28992898
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),

0 commit comments

Comments
 (0)