Skip to content

Commit ba6a483

Browse files
feat: tool presets turn a set of skills and MCP servers on or off for a thread
Switching skills and servers one by one in Settings changes them for every thread, and there was no way to give one thread a browser server and its testing skills without giving them to all of them. A preset is a named list of skills and MCP servers, kept in each environment's settings and edited on a new Presets tab in Settings → Tools. A thread stores which presets it turns on or off; the composer's Tools menu switches them for that thread, and a new thread's draft carries its choice into thread creation. Forks and delegated tasks inherit them. The server applies a thread's presets after the project's overrides when it prepares a session (on wins where two presets share a tool). A change to an idle thread detaches its session, as a runtime-mode change does, so the next message resumes the conversation with the new tools; while work is unfinished the change is refused and the menu is disabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fcb20d8 commit ba6a483

37 files changed

Lines changed: 1153 additions & 53 deletions

‎apps/server/src/mcp/resolveAgentTools.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
import type { ProjectId, ResolvedMcpServer, ServerSettings } from "@t3tools/contracts";
1+
import type {
2+
ProjectId,
3+
ResolvedMcpServer,
4+
ServerSettings,
5+
ThreadToolPresets,
6+
} from "@t3tools/contracts";
7+
import { applyThreadToolPresets } from "@t3tools/shared/agentTools";
28
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
39

410
import type { McpProviderSessionTools } from "@t3tools/provider-core/server/mcpSession";
511

612
/**
7-
* A thread's tools from settings whose secrets are already materialized:
8-
* the project's overrides applied, disabled servers dropped. The fingerprint
9-
* covers the full config so a rotated secret also counts as a change; it
10-
* stays on the server and is only ever compared, never sent or logged.
13+
* A thread's tools from secret-materialized settings: project overrides, then
14+
* the thread's presets, disabled servers dropped. The fingerprint covers the
15+
* full config (a rotated secret counts) and is only compared, never sent.
1116
*/
1217
export function resolveAgentTools(
1318
settings: ServerSettings,
1419
projectId: ProjectId | null,
20+
threadPresets?: ThreadToolPresets,
1521
): McpProviderSessionTools {
16-
const effective = resolveProjectSettings(settings, projectId).settings;
22+
const effective = applyThreadToolPresets(
23+
resolveProjectSettings(settings, projectId).settings,
24+
settings.toolPresets,
25+
threadPresets,
26+
);
1727
const servers: ResolvedMcpServer[] = Object.entries(effective.mcpServers)
1828
.filter(([, server]) => server.enabled)
1929
.map(([name, server]) => ({ name, transport: server.transport }))

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,11 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
841841
const messageId = MessageId.make("message:foundation-compact");
842842
const threadStateEvent = (
843843
suffix: string,
844-
type: "thread.visited" | "thread.metadata-updated" | "thread.tags-updated",
844+
type:
845+
| "thread.visited"
846+
| "thread.metadata-updated"
847+
| "thread.tags-updated"
848+
| "thread.tool-presets-updated",
845849
): OrchestrationV2DomainEvent => ({
846850
id: EventId.make(`event:foundation-compact:${suffix}`),
847851
type,
@@ -941,7 +945,9 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
941945
messageEvent("message-2", "final"),
942946
nodeEvent("node-2", "completed"),
943947
itemEvent("item-2", "final"),
944-
// Older clients skip tag events, so these must not remove the rename above.
948+
threadStateEvent("presets", "thread.tool-presets-updated"),
949+
// Older clients skip tag and preset events, so these must not remove the rename
950+
// above, nor each other.
945951
threadStateEvent("tags-2", "thread.tags-updated"),
946952
],
947953
});
@@ -1005,6 +1011,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
10051011
"event:foundation-compact:message-2",
10061012
"event:foundation-compact:node-2",
10071013
"event:foundation-compact:item-2",
1014+
"event:foundation-compact:presets",
10081015
"event:foundation-compact:tags-2",
10091016
],
10101017
);

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,98 @@ it.effect(
198198
}).pipe(Effect.provide(layerTest)),
199199
);
200200

201+
it.effect("a change to a thread's tool presets waits for its work and replaces its session", () =>
202+
Effect.gen(function* () {
203+
const orchestrator = yield* Orchestrator.OrchestratorV2;
204+
const projections = yield* ProjectionStore.ProjectionStoreV2;
205+
const outbox = yield* EffectOutbox.EffectOutboxV2;
206+
const threadId = ThreadId.make("thread:tool-presets");
207+
const sessionId = ProviderSessionId.make("session:tool-presets");
208+
const now = yield* DateTime.now;
209+
yield* orchestrator.dispatch({
210+
type: "thread.create",
211+
commandId: CommandId.make("create-tool-presets"),
212+
threadId,
213+
projectId: ProjectId.make("project:tool-presets"),
214+
title: "Tools",
215+
modelSelection,
216+
runtimeMode: "full-access",
217+
interactionMode: "default",
218+
branch: null,
219+
worktreePath: null,
220+
createdBy: "user",
221+
creationSource: "web",
222+
});
223+
yield* projections.apply({
224+
id: EventId.make("session:tool-presets"),
225+
type: "provider-session.attached",
226+
threadId,
227+
occurredAt: now,
228+
payload: {
229+
id: sessionId,
230+
driver: adapter.driver,
231+
providerInstanceId: instanceId,
232+
status: "ready",
233+
cwd: "/repo",
234+
model: modelSelection.model,
235+
capabilities: CodexProviderCapabilitiesV2,
236+
createdAt: now,
237+
updatedAt: now,
238+
lastError: null,
239+
},
240+
});
241+
const request = (status: "pending" | "resolved") =>
242+
projections.apply({
243+
id: EventId.make(`request:tool-presets:${status}`),
244+
type: "runtime-request.updated",
245+
threadId,
246+
occurredAt: now,
247+
payload: {
248+
id: RuntimeRequestId.make("tool-presets"),
249+
nodeId: NodeId.make("node:tool-presets"),
250+
providerTurnId: null,
251+
nativeRequestRef: null,
252+
kind: "command",
253+
status,
254+
responseCapability: {
255+
type: "live",
256+
providerSessionId: sessionId,
257+
supportsFeedback: false,
258+
},
259+
createdAt: now,
260+
resolvedAt: status === "resolved" ? now : null,
261+
},
262+
});
263+
const setPresets = (commandId: CommandId) =>
264+
orchestrator.dispatch({
265+
type: "thread.tool-presets.set",
266+
commandId,
267+
threadId,
268+
toolPresets: { frontend: true },
269+
});
270+
271+
yield* request("pending");
272+
const busy = CommandId.make("tool-presets:busy");
273+
assert.isTrue(Result.isFailure(yield* setPresets(busy).pipe(Effect.result)));
274+
assert.deepEqual(yield* outbox.listByCommandId(busy), []);
275+
276+
yield* request("resolved");
277+
const changed = CommandId.make("tool-presets:changed");
278+
yield* setPresets(changed);
279+
assert.deepEqual((yield* projections.getThreadProjection(threadId)).thread.toolPresets, {
280+
frontend: true,
281+
});
282+
assert.deepEqual(
283+
(yield* outbox.listByCommandId(changed)).map((effect) => effect.request),
284+
[{ type: "provider-session.detach", providerSessionId: sessionId, detail: "Tools changed." }],
285+
);
286+
// The same switches again leave the session alone.
287+
const unchanged = CommandId.make("tool-presets:unchanged");
288+
yield* setPresets(unchanged);
289+
assert.deepEqual(yield* outbox.listByCommandId(unchanged), []);
290+
}).pipe(Effect.provide(layerTest)),
291+
);
292+
201293
it.effect("marks a never-visited thread's first completion unread", () =>
202294
Effect.gen(function* () {
203295
const orchestrator = yield* Orchestrator.OrchestratorV2;

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
TrimmedNonEmptyString,
7676
managedRuntimeModeCeilingMessage,
7777
} from "@t3tools/contracts";
78+
import { threadToolPresetsEqual } from "@t3tools/shared/agentTools";
7879
import { modelSelectionsEqual } from "@t3tools/shared/model";
7980
import { modelSwitchSince } from "./modelSwitch.ts";
8081
import { 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":

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ export const layer: Layer.Layer<
219219
];
220220
// Read-state events preserve updatedAt and supersede only each other. Activity events supersede both groups.
221221
const READ_STATE_THREAD_EVENT_TYPES = ["thread.visited", "thread.marked-unread"];
222-
// Older clients skip tag events, so these supersede only each other.
223-
const TAG_THREAD_EVENT_TYPE = "thread.tags-updated";
222+
// Older clients skip these events, so each type supersedes only itself.
223+
const SKIPPED_THREAD_EVENT_TYPES = new Set([
224+
"thread.tags-updated",
225+
"thread.tool-presets-updated",
226+
]);
224227

225228
const supersedableThreadEventTypes = new Set(SUPERSEDABLE_THREAD_EVENT_TYPES);
226229
const readStateThreadEventTypes = new Set(READ_STATE_THREAD_EVENT_TYPES);
@@ -245,7 +248,7 @@ export const layer: Layer.Layer<
245248
let throughReceiptRowId = bounds[0]?.receipt_row_id ?? 0;
246249
const retainedThreadIds = new Set<string>();
247250
const retainedReadStateThreadIds = new Set<string>();
248-
const retainedTagThreadIds = new Set<string>();
251+
const retainedSkippedThreadEvents = new Set<string>();
249252
// Retain each key's oldest candidate, deleting it only when an older event appears.
250253
const oldestCandidateByEntityKey = new Map<string, number | null>();
251254
let deletedEventCount = 0;
@@ -321,11 +324,12 @@ export const layer: Layer.Layer<
321324
} else retainedReadStateThreadIds.add(row.stream_id);
322325
} else if (
323326
row.aggregate_kind === "thread" &&
324-
row.event_type === TAG_THREAD_EVENT_TYPE
327+
SKIPPED_THREAD_EVENT_TYPES.has(row.event_type)
325328
) {
326-
if (retainedThreadIds.has(row.stream_id) || retainedTagThreadIds.has(row.stream_id)) {
329+
const key = encodeEntityKey([row.event_type, row.stream_id, null]);
330+
if (retainedThreadIds.has(row.stream_id) || retainedSkippedThreadEvents.has(key)) {
327331
obsolete.push(row.sequence);
328-
} else retainedTagThreadIds.add(row.stream_id);
332+
} else retainedSkippedThreadEvents.add(key);
329333
} else if (
330334
row.event_type === "thread.pull-request-link-updated" &&
331335
retainedThreadIds.has(row.stream_id)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ export function applyToProjection(
811811
// Reordering and PR refreshes preserve the orchestrator's activity timestamp.
812812
case "thread.active-reordered":
813813
case "thread.tags-updated":
814+
case "thread.tool-presets-updated":
814815
case "thread.pull-request-synced":
815816
return {
816817
...base,
@@ -1594,6 +1595,9 @@ export function threadShellFromProjection(
15941595
autoSettleDisabledAt: projection.thread.autoSettleDisabledAt ?? null,
15951596
pinOrderKey: projection.thread.pinOrderKey ?? null,
15961597
...(projection.thread.tags?.length ? { tags: projection.thread.tags } : {}),
1598+
...(projection.thread.toolPresets === undefined
1599+
? {}
1600+
: { toolPresets: projection.thread.toolPresets }),
15971601
lastVisitedAt: projection.thread.lastVisitedAt,
15981602
opens: projection.thread.opens ?? null,
15991603
titleRegeneration: projection.thread.titleRegeneration ?? null,
@@ -1860,6 +1864,9 @@ function shellFromState(input: {
18601864
autoSettleDisabledAt: input.state.thread.autoSettleDisabledAt ?? null,
18611865
pinOrderKey: input.state.thread.pinOrderKey ?? null,
18621866
...(input.state.thread.tags?.length ? { tags: input.state.thread.tags } : {}),
1867+
...(input.state.thread.toolPresets === undefined
1868+
? {}
1869+
: { toolPresets: input.state.thread.toolPresets }),
18631870
lastVisitedAt: input.state.thread.lastVisitedAt,
18641871
opens: input.state.thread.opens ?? null,
18651872
titleRegeneration: input.state.thread.titleRegeneration ?? null,
@@ -1919,6 +1926,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
19191926
case "thread.pin-reordered":
19201927
case "thread.active-reordered":
19211928
case "thread.tags-updated":
1929+
case "thread.tool-presets-updated":
19221930
case "thread.visited":
19231931
case "thread.marked-unread":
19241932
case "thread.metadata-updated":
@@ -2732,6 +2740,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
27322740
event.type !== "thread.pin-reordered" &&
27332741
event.type !== "thread.active-reordered" &&
27342742
event.type !== "thread.tags-updated" &&
2743+
event.type !== "thread.tool-presets-updated" &&
27352744
event.type !== "thread.visited" &&
27362745
event.type !== "thread.marked-unread" &&
27372746
event.type !== "thread.metadata-updated" &&

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ export const layerWithOptions = (
374374
return {
375375
browser: browserOverridden ? false : settings.enableAgentBrowserAccess,
376376
device: deviceOverridden ? false : settings.enableAgentDeviceAccess,
377-
tools: resolveAgentTools(settings, null),
377+
tools: resolveAgentTools(settings, null, thread.toolPresets),
378378
};
379379
}
380380
const effective = resolveProjectSettings(settings, thread.projectId).settings;
381381
return {
382382
browser: effective.enableAgentBrowserAccess,
383383
device: effective.enableAgentDeviceAccess,
384-
tools: resolveAgentTools(settings, thread.projectId),
384+
tools: resolveAgentTools(settings, thread.projectId, thread.toolPresets),
385385
};
386386
}).pipe(
387387
Effect.catch((cause) =>

0 commit comments

Comments
 (0)