Skip to content

Commit a0e8182

Browse files
fix(shared): judge a pending worktree by the latest executed run
A setup retry can fail again after a later run completed in the project checkout. Order runs the way the server does (latestExecutedRun) instead of by ordinal, so side chats stay refused exactly while the server holds messages for the worktree. The helper moves next to latestExecutedRun. AI-assisted; verified with the tests and tools listed above.
1 parent 86c3d1b commit a0e8182

6 files changed

Lines changed: 55 additions & 57 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
@@ -24,7 +24,6 @@ import {
2424
DEFAULT_PROVIDER_INTERACTION_MODE,
2525
MessageId,
2626
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
27-
orchestrationV2WorktreePending,
2827
type EnvironmentId,
2928
type ModelSelection,
3029
type RuntimeMode,
@@ -85,6 +84,7 @@ import {
8584
useComposerDraftSettings,
8685
} from "./use-composer-drafts";
8786
import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger";
87+
import { orchestrationV2WorktreePending } from "@t3tools/shared/orchestrationV2ThreadError";
8888
import {
8989
compactionBlocksSteering,
9090
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: 33 additions & 25 deletions
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" ||
@@ -1054,30 +1059,33 @@ it.effect("a side chat opens once a later run has worked in the project checkout
10541059
yield* forkSideChat("side-chat-worktree-abandoned-fork", parentId, sideChatId);
10551060
assert.isNull((yield* orchestrator.getThreadProjection(sideChatId)).thread.deletedAt);
10561061

1057-
// Retrying the failed setup puts the same run back into preparing.
1058-
const retriedParentId = ThreadId.make("side-chat-worktree-retried-parent");
1059-
yield* createParent(retriedParentId);
1060-
yield* writeParentRun({
1061-
threadId: retriedParentId,
1062-
runId: RunId.make("side-chat-worktree-retried-run-2"),
1063-
ordinal: 2,
1064-
status: "completed",
1065-
text: "Worked in the project checkout",
1066-
stable: true,
1067-
});
1068-
yield* writeParentRun({
1069-
threadId: retriedParentId,
1070-
runId: RunId.make("side-chat-worktree-retried-run-1"),
1071-
ordinal: 1,
1072-
status: "preparing",
1073-
text: "Retrying setup",
1074-
});
1075-
const rejected = yield* forkSideChat(
1076-
"side-chat-worktree-retried-fork",
1077-
retriedParentId,
1078-
ThreadId.make("side-chat-worktree-retried-child"),
1079-
).pipe(Effect.flip);
1080-
assert.include(String(rejected.cause), "has no worktree yet");
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+
}
10811089
}).pipe(Effect.provide(forkCases[0]!.layer)),
10821090
);
10831091

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import {
6161
type AssistantCitation,
6262
type ChatFileAttachment,
6363
isProviderNativeSubagentThread,
64-
orchestrationV2WorktreePending,
6564
type EnvironmentId,
6665
type MessageId,
6766
type ModelSelection,
@@ -114,6 +113,7 @@ import {
114113
import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
115114
import { projectScriptCwd, resolveProjectScripts } from "@t3tools/shared/projectScripts";
116115
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
116+
import { orchestrationV2WorktreePending } from "@t3tools/shared/orchestrationV2ThreadError";
117117
import { resolveThreadReferenceCopyTarget } from "@t3tools/client-runtime/thread-reference";
118118
import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
119119
import { useAtomValue } from "@effect/atom-react";

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -728,35 +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, or until a
734-
* later run has started work in the project checkout anyway. A retried setup is pending again.
735-
*/
736-
export function orchestrationV2WorktreePending(
737-
worktreePath: string | null,
738-
runs: ReadonlyArray<
739-
Pick<OrchestrationV2Run, "ordinal" | "status" | "startedAt" | "workspacePreparation">
740-
>,
741-
): boolean {
742-
return (
743-
worktreePath === null &&
744-
runs.some(
745-
(run) =>
746-
run.workspacePreparation?.type === "worktree" &&
747-
(run.status === "preparing" ||
748-
((run.status === "failed" || run.status === "interrupted") &&
749-
!runs.some(
750-
(later) =>
751-
later.ordinal > run.ordinal &&
752-
later.status !== "queued" &&
753-
later.status !== "preparing" &&
754-
!(later.status === "cancelled" && later.startedAt === null),
755-
))),
756-
)
757-
);
758-
}
759-
760731
export const OrchestrationV2RunAttempt = Schema.Struct({
761732
id: RunAttemptId,
762733
// 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)