Skip to content

Commit 8b875b3

Browse files
some-du6eclaude
andcommitted
refactor(server): fold project-move guards into one check
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0741333 commit 8b875b3

3 files changed

Lines changed: 24 additions & 68 deletions

File tree

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

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,28 +2941,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
29412941
cause: `Thread ${command.threadId} is no longer empty.`,
29422942
});
29432943
}
2944-
const movesProject =
2945-
command.type === "thread.metadata.update" &&
2946-
command.projectId !== undefined &&
2947-
command.projectId !== thread.projectId;
2948-
if (
2949-
command.type === "thread.metadata.update" &&
2950-
command.projectId !== undefined &&
2951-
command.projectId !== thread.projectId
2952-
) {
2953-
if (thread.lineage.relationshipToParent === "subagent") {
2954-
return yield* new OrchestratorDispatchError({
2955-
commandId: command.commandId,
2956-
commandType: command.type,
2957-
cause: `Thread ${command.threadId} is a subagent and moves with its parent.`,
2958-
});
2959-
}
2960-
const project = yield* projects.get(command.projectId).pipe(mapDispatchError(command));
2944+
const targetProjectId =
2945+
command.type === "thread.metadata.update" && command.projectId !== thread.projectId
2946+
? command.projectId
2947+
: undefined;
2948+
const movesProject = targetProjectId !== undefined;
2949+
if (targetProjectId !== undefined) {
2950+
const project = yield* projects.get(targetProjectId).pipe(mapDispatchError(command));
29612951
if (Option.isNone(project) || project.value.deletedAt !== null) {
29622952
return yield* new OrchestratorDispatchError({
29632953
commandId: command.commandId,
29642954
commandType: command.type,
2965-
cause: `Project ${command.projectId} does not exist.`,
2955+
cause: `Project ${targetProjectId} does not exist.`,
29662956
});
29672957
}
29682958
const records = yield* projectionStore
@@ -2975,44 +2965,28 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
29752965
},
29762966
)
29772967
.pipe(mapDispatchError(command));
2978-
// Children inherit the parent's project and workspace; moving only the parent would strand them.
2979-
if (records.subagents.some((task) => task.childThreadId !== null)) {
2980-
return yield* new OrchestratorDispatchError({
2981-
commandId: command.commandId,
2982-
commandType: command.type,
2983-
cause: `Thread ${command.threadId} has child threads and cannot move to another project.`,
2984-
});
2985-
}
2968+
const latestRun = latestExecutedRun(records.runs);
2969+
// Children share the project; failed worktree setup would retry in the old repo.
29862970
if (
2987-
records.runs.some((run) =>
2988-
["preparing", "queued", "starting", "running", "waiting"].includes(run.status),
2989-
) ||
2971+
thread.lineage.relationshipToParent === "subagent" ||
2972+
records.subagents.some((task) => task.childThreadId !== null) ||
2973+
records.runs.some((run) => isBlockingRun(run) || run.status === "queued") ||
29902974
records.runtimeRequests.some((request) => request.status === "pending") ||
29912975
derivePendingBackgroundWork({
29922976
latestRun: records.runs.findLast((run) => run.status !== "queued"),
29932977
providerThreads: records.providerThreads,
29942978
turnItems: records.turnItems,
29952979
activeProviderThreadId: thread.activeProviderThreadId,
29962980
runs: records.runs,
2997-
}).length > 0
2998-
) {
2999-
return yield* new OrchestratorDispatchError({
3000-
commandId: command.commandId,
3001-
commandType: command.type,
3002-
cause: `Thread ${command.threadId} is busy. Stop it or clear its queue before moving it.`,
3003-
});
3004-
}
3005-
// Failed worktree setup holds the queue and would retry in the old project's repo.
3006-
const latestRun = latestExecutedRun(records.runs);
3007-
if (
3008-
(latestRun?.status === "failed" || latestRun?.status === "interrupted") &&
3009-
latestRun.workspacePreparation?.type === "worktree" &&
3010-
thread.worktreePath === null
2981+
}).length > 0 ||
2982+
((latestRun?.status === "failed" || latestRun?.status === "interrupted") &&
2983+
latestRun.workspacePreparation?.type === "worktree" &&
2984+
thread.worktreePath === null)
30112985
) {
30122986
return yield* new OrchestratorDispatchError({
30132987
commandId: command.commandId,
30142988
commandType: command.type,
3015-
cause: `Thread ${command.threadId} has failed worktree setup. Retry it or continue locally before moving.`,
2989+
cause: `Thread ${command.threadId} must be idle, top-level, and without child threads or failed worktree setup to move.`,
30162990
});
30172991
}
30182992
}
@@ -3465,13 +3439,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
34653439
: {}),
34663440
...(command.branch === undefined ? {} : { branch: command.branch }),
34673441
...(command.worktreePath === undefined ? {} : { worktreePath: command.worktreePath }),
3468-
...(command.projectId === undefined || command.projectId === thread.projectId
3442+
...(targetProjectId === undefined
34693443
? {}
3470-
: {
3471-
projectId: command.projectId,
3472-
branch: command.branch ?? null,
3473-
worktreePath: command.worktreePath ?? null,
3474-
}),
3444+
: { projectId: targetProjectId, branch: null, worktreePath: null }),
34753445
...(command.linkedPullRequest === undefined
34763446
? {}
34773447
: {

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ 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 and back", () =>
1875+
it.effect("moves an idle thread to another project", () =>
18761876
Effect.gen(function* () {
18771877
const orchestrator = yield* Orchestrator.OrchestratorV2;
18781878
const threadId = ThreadId.make("runtime-layer-move-thread");
@@ -1915,9 +1915,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19151915
watching: true,
19161916
link: { url: "https://github.com/pingdotgg/t3code/pull/1", source: "manual" },
19171917
});
1918-
assert.isDefined(
1919-
(yield* orchestrator.getThreadProjection(threadId)).thread.pullRequests?.[0]?.watch,
1920-
);
19211918

19221919
const missingProject = yield* orchestrator
19231920
.dispatch({
@@ -1947,17 +1944,6 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19471944
toProjectId,
19481945
);
19491946

1950-
yield* orchestrator.dispatch({
1951-
type: "thread.metadata.update",
1952-
commandId: CommandId.make("runtime-layer-move-back"),
1953-
threadId,
1954-
projectId: fromProjectId,
1955-
});
1956-
assert.equal(
1957-
(yield* orchestrator.getThreadProjection(threadId)).thread.projectId,
1958-
fromProjectId,
1959-
);
1960-
19611947
yield* orchestrator.dispatch({
19621948
type: "message.dispatch",
19631949
createdBy: "user",
@@ -1975,13 +1961,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
19751961
type: "thread.metadata.update",
19761962
commandId: CommandId.make("runtime-layer-move-busy"),
19771963
threadId,
1978-
projectId: toProjectId,
1964+
projectId: fromProjectId,
19791965
})
19801966
.pipe(Effect.flip);
19811967
assert.instanceOf(busy, Orchestrator.OrchestratorDispatchError);
19821968
assert.equal(
19831969
(yield* orchestrator.getThreadProjection(threadId)).thread.projectId,
1984-
fromProjectId,
1970+
toProjectId,
19851971
);
19861972
}),
19871973
);

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2893,7 +2893,7 @@ 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 branch and worktree unless also given. */
2896+
/** Move an idle thread to another project; clears its branch and worktree. */
28972897
projectId: Schema.optional(ProjectId),
28982898
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
28992899
worktreePath: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),

0 commit comments

Comments
 (0)