@@ -75,6 +75,7 @@ import {
7575 TrimmedNonEmptyString,
7676 managedRuntimeModeCeilingMessage,
7777} from "@t3tools/contracts";
78+ import { threadToolPresetsEqual } from "@t3tools/shared/agentTools";
7879import { modelSelectionsEqual } from "@t3tools/shared/model";
7980import { modelSwitchSince } from "./modelSwitch.ts";
8081import { recordThreadOpen } from "@t3tools/shared/paletteSearch";
@@ -549,6 +550,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
549550 case "thread.pin.reorder":
550551 case "thread.active.reorder":
551552 case "thread.tag.set":
553+ case "thread.tool-presets.set":
552554 case "thread.visit":
553555 case "thread.mark-unread":
554556 case "thread.metadata.update":
@@ -2759,6 +2761,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
27592761 interactionMode: command.interactionMode,
27602762 branch: command.branch,
27612763 worktreePath: command.worktreePath,
2764+ ...(command.toolPresets === undefined ? {} : { toolPresets: command.toolPresets }),
27622765 activeProviderThreadId: null,
27632766 pullRequests: [],
27642767 ...(command.linkOrigin === undefined ? {} : { linkOrigin: command.linkOrigin }),
@@ -3045,6 +3048,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
30453048 | "thread.unpin"
30463049 | "thread.pin.reorder"
30473050 | "thread.tag.set"
3051+ | "thread.tool-presets.set"
30483052 | "thread.active.reorder"
30493053 | "thread.mark-unread"
30503054 | "thread.metadata.update"
@@ -3325,7 +3329,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33253329 }
33263330 }
33273331
3332+ const toolPresetsChanged =
3333+ command.type === "thread.tool-presets.set" &&
3334+ !threadToolPresetsEqual(command.toolPresets, thread.toolPresets);
33283335 const needsProviderState =
3336+ toolPresetsChanged ||
33293337 command.type === "thread.runtime-mode.set" ||
33303338 command.type === "thread.model-selection.set" ||
33313339 command.type === "provider.switch" ||
@@ -3367,6 +3375,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33673375 : null;
33683376
33693377 const now = yield* DateTime.now;
3378+ if (toolPresetsChanged) {
3379+ const projection = yield* loadProjectionForCommand(command, ["runs", "runtimeRequests"], {
3380+ turnItemTypes: [],
3381+ });
3382+ // A session's tools are fixed when it opens, and replacing it would end the run.
3383+ if (threadHasUnfinishedWork(projection)) {
3384+ return yield* new OrchestratorDispatchError({
3385+ commandId: command.commandId,
3386+ commandType: command.type,
3387+ cause: "Tools can change once the thread's current work finishes.",
3388+ });
3389+ }
3390+ }
33703391 if (command.type === "thread.metadata.update" && command.limitRecovery != null) {
33713392 const projection = yield* loadProjectionForCommand(
33723393 command,
@@ -3580,6 +3601,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
35803601 updatedAt: thread.updatedAt,
35813602 };
35823603 }
3604+ case "thread.tool-presets.set":
3605+ return { ...thread, toolPresets: command.toolPresets };
35833606 case "thread.tag.set": {
35843607 const current = thread.tags ?? [];
35853608 return {
@@ -3964,6 +3987,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39643987 return "thread.active-reordered" as const;
39653988 case "thread.tag.set":
39663989 return "thread.tags-updated" as const;
3990+ case "thread.tool-presets.set":
3991+ return "thread.tool-presets-updated" as const;
39673992 case "thread.mark-unread":
39683993 return "thread.marked-unread" as const;
39693994 case "thread.metadata.update":
@@ -4121,7 +4146,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41214146 (session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
41224147 )
41234148 .map((session) => session.id)
4124- : (providerSwitchPlan?.releaseProviderSessionIds ?? []),
4149+ : toolPresetsChanged
4150+ ? (providerContext?.providerSessions ?? []).map((session) => session.id)
4151+ : (providerSwitchPlan?.releaseProviderSessionIds ?? []),
41254152 );
41264153 if (detachSessionIds.size > 0) {
41274154 const liveSessions = (providerContext?.providerSessions ?? []).filter(
@@ -4155,7 +4182,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41554182 ? "Workspace changed."
41564183 : command.type === "thread.runtime-mode.set"
41574184 ? "Runtime mode changed."
4158- : "Provider or model selection changed.",
4185+ : command.type === "thread.tool-presets.set"
4186+ ? "Tools changed."
4187+ : "Provider or model selection changed.",
41594188 },
41604189 });
41614190 const pendingEffect = {
@@ -4174,7 +4203,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41744203 ? "Workspace changed."
41754204 : command.type === "thread.runtime-mode.set"
41764205 ? "Runtime mode changed."
4177- : "Provider or model selection changed.",
4206+ : command.type === "thread.tool-presets.set"
4207+ ? "Tools changed."
4208+ : "Provider or model selection changed.",
41784209 // Terminal detaches revoke MCP credentials; reattachments need other detach reasons to preserve them.
41794210 ...(command.type === "thread.archive" ? { revokeMcpCredential: true } : {}),
41804211 },
@@ -13100,6 +13131,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1310013131 case "thread.pin.reorder":
1310113132 case "thread.active.reorder":
1310213133 case "thread.tag.set":
13134+ case "thread.tool-presets.set":
1310313135 case "thread.mark-unread":
1310413136 case "thread.metadata.update":
1310513137 case "thread.pull-request.link":
0 commit comments