Skip to content

Commit d63db06

Browse files
fix(contracts): side chats open after an abandoned worktree setup (#34686)
1 parent a21b43f commit d63db06

6 files changed

Lines changed: 83 additions & 22 deletions

File tree

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
DEFAULT_PROVIDER_INTERACTION_MODE,
3030
MessageId,
3131
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
32-
orchestrationV2WorktreePending,
3332
type EnvironmentId,
3433
type ModelSelection,
3534
type RuntimeMode,
@@ -90,6 +89,7 @@ import {
9089
useComposerDraftSettings,
9190
} from "./use-composer-drafts";
9291
import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger";
92+
import { orchestrationV2WorktreePending } from "@t3tools/shared/orchestrationV2ThreadError";
9393
import {
9494
compactionBlocksSteering,
9595
resolveComposerDispatchMode,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
import {
99
latestExecutedRun,
1010
latestRootProviderFailure,
11+
orchestrationV2WorktreePending,
1112
runRanAfter,
1213
usageLimitBlockedRun,
1314
} from "@t3tools/shared/orchestrationV2ThreadError";
@@ -62,7 +63,6 @@ import {
6263
latestProviderTurnForAttempt,
6364
parseGoalCommand,
6465
orchestrationV2RunWorkStartedAt,
65-
orchestrationV2WorktreePending,
6666
ProviderInstanceId,
6767
ProviderInteractionMode,
6868
isRuntimeModeAbove,

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,12 @@ const writeParentRun = (input: {
727727
queuePosition: null,
728728
requestedAt: now,
729729
startedAt: now,
730-
completedAt: input.status === "completed" ? now : null,
730+
completedAt:
731+
input.status === "preparing" ||
732+
input.status === "running" ||
733+
input.status === "queued"
734+
? null
735+
: now,
731736
checkpointId: input.stable ? CheckpointId.make(`${input.runId}:checkpoint`) : null,
732737
contextHandoffId: null,
733738
...(input.status === "preparing" ||
@@ -1029,6 +1034,61 @@ it.effect("a side chat waits for the parent's new worktree, also after its setup
10291034
}).pipe(Effect.provide(forkCases[0]!.layer)),
10301035
);
10311036

1037+
it.effect("a side chat opens once a later run has worked in the project checkout", () =>
1038+
Effect.gen(function* () {
1039+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1040+
const parentId = ThreadId.make("side-chat-worktree-abandoned-parent");
1041+
const sideChatId = ThreadId.make("side-chat-worktree-abandoned-child");
1042+
yield* createParent(parentId);
1043+
yield* writeParentRun({
1044+
threadId: parentId,
1045+
runId: RunId.make("side-chat-worktree-abandoned-run-1"),
1046+
ordinal: 1,
1047+
status: "interrupted",
1048+
text: "Stopped during setup",
1049+
});
1050+
yield* writeParentRun({
1051+
threadId: parentId,
1052+
runId: RunId.make("side-chat-worktree-abandoned-run-2"),
1053+
ordinal: 2,
1054+
status: "completed",
1055+
text: "Worked in the project checkout",
1056+
stable: true,
1057+
});
1058+
1059+
yield* forkSideChat("side-chat-worktree-abandoned-fork", parentId, sideChatId);
1060+
assert.isNull((yield* orchestrator.getThreadProjection(sideChatId)).thread.deletedAt);
1061+
1062+
// A retried setup that is running again, or failed again, comes after the later run.
1063+
for (const status of ["preparing", "failed"] as const) {
1064+
const retriedParentId = ThreadId.make(`side-chat-worktree-retried-parent-${status}`);
1065+
yield* createParent(retriedParentId);
1066+
yield* writeParentRun({
1067+
threadId: retriedParentId,
1068+
runId: RunId.make(`side-chat-worktree-retried-run-2-${status}`),
1069+
ordinal: 2,
1070+
status: "completed",
1071+
text: "Worked in the project checkout",
1072+
stable: true,
1073+
});
1074+
yield* TestClock.adjust("1 second");
1075+
yield* writeParentRun({
1076+
threadId: retriedParentId,
1077+
runId: RunId.make(`side-chat-worktree-retried-run-1-${status}`),
1078+
ordinal: 1,
1079+
status,
1080+
text: "Retried setup",
1081+
});
1082+
const rejected = yield* forkSideChat(
1083+
`side-chat-worktree-retried-fork-${status}`,
1084+
retriedParentId,
1085+
ThreadId.make(`side-chat-worktree-retried-child-${status}`),
1086+
).pipe(Effect.flip);
1087+
assert.include(String(rejected.cause), "has no worktree yet", status);
1088+
}
1089+
}).pipe(Effect.provide(forkCases[0]!.layer)),
1090+
);
1091+
10321092
it.effect("closing a side chat deletes it only while unused; a deleted parent takes no more", () =>
10331093
Effect.gen(function* () {
10341094
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ import {
6464
type AssistantCitation,
6565
type ChatFileAttachment,
6666
isProviderNativeSubagentThread,
67-
orchestrationV2WorktreePending,
6867
type EnvironmentId,
6968
type MessageId,
7069
type ModelSelection,
@@ -117,6 +116,7 @@ import {
117116
import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
118117
import { projectScriptCwd, resolveProjectScripts } from "@t3tools/shared/projectScripts";
119118
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
119+
import { orchestrationV2WorktreePending } from "@t3tools/shared/orchestrationV2ThreadError";
120120
import { resolveThreadReferenceCopyTarget } from "@t3tools/client-runtime/thread-reference";
121121
import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
122122
import { useAtomValue } from "@effect/atom-react";

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -728,24 +728,6 @@ export function orchestrationV2RunWorkStartedAt(
728728
return run.workStartedAt ?? run.startedAt ?? run.requestedAt;
729729
}
730730

731-
/**
732-
* Work started now would run in the project checkout, not the worktree the thread asked for. A
733-
* failed or interrupted setup leaves the request standing until a worktree exists.
734-
*/
735-
export function orchestrationV2WorktreePending(
736-
worktreePath: string | null,
737-
runs: ReadonlyArray<Pick<OrchestrationV2Run, "status" | "workspacePreparation">>,
738-
): boolean {
739-
return (
740-
worktreePath === null &&
741-
runs.some(
742-
(run) =>
743-
(run.status === "preparing" || run.status === "failed" || run.status === "interrupted") &&
744-
run.workspacePreparation?.type === "worktree",
745-
)
746-
);
747-
}
748-
749731
export const OrchestrationV2RunAttempt = Schema.Struct({
750732
id: RunAttemptId,
751733
// Provider-thread rows can be reused after recovery; retain the native input destination.

‎packages/shared/src/orchestrationV2ThreadError.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ export function latestExecutedRun(
6464
return latest;
6565
}
6666

67+
/**
68+
* Work started now would run in the project checkout, not the worktree the thread asked for. A
69+
* failed or interrupted setup leaves the request standing until a worktree exists or later work
70+
* has run past it.
71+
*/
72+
export function orchestrationV2WorktreePending(
73+
worktreePath: string | null,
74+
runs: ReadonlyArray<OrchestrationV2Run>,
75+
): boolean {
76+
const latest = latestExecutedRun(runs);
77+
return (
78+
worktreePath === null &&
79+
(latest?.status === "preparing" ||
80+
latest?.status === "failed" ||
81+
latest?.status === "interrupted") &&
82+
latest.workspacePreparation?.type === "worktree"
83+
);
84+
}
85+
6786
/** Held queues can execute out of ordinal order; unfinished runs count as latest. */
6887
export function runRanAfter(
6988
run: Pick<OrchestrationV2Run, "ordinal" | "completedAt">,

0 commit comments

Comments
 (0)