Skip to content

Commit 7208335

Browse files
fix(providers): preserve workspace model options (#21531)
1 parent f1d0f2a commit 7208335

6 files changed

Lines changed: 148 additions & 8 deletions

File tree

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
ProviderDriverKind,
2929
ProviderInstanceId,
3030
ServerSettings,
31-
type ServerProvider,
31+
ServerProvider,
3232
type ServerProviderSlashCommand,
3333
type ServerSettings as ContractServerSettings,
3434
} from "@t3tools/contracts";
@@ -68,6 +68,8 @@ import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
6868
import * as ProviderRegistry from "./ProviderRegistry.ts";
6969
import { ProviderDriverError } from "./Errors.ts";
7070
import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver";
71+
const decodeServerProvider = Schema.decodeSync(ServerProvider);
72+
const encodeServerProvider = Schema.encodeSync(ServerProvider);
7173
const decodeServerSettings = Schema.decodeSync(ServerSettings);
7274
const encodeServerSettings = Schema.encodeSync(ServerSettings);
7375
const encodedDefaultServerSettings = encodeServerSettings(DEFAULT_SERVER_SETTINGS);
@@ -630,7 +632,7 @@ it.layer(
630632
});
631633

632634
describe("ProviderRegistry.layer", () => {
633-
it("stores workspace skills and commands without changing machine metadata", () => {
635+
it("stores workspace skills, commands and options without changing machine metadata", () => {
634636
const provider = {
635637
instanceId: ProviderInstanceId.make("codex"),
636638
driver: ProviderDriverKind.make("codex"),
@@ -649,7 +651,15 @@ it.layer(
649651
checkedAt: "2026-03-25T00:01:00.000Z",
650652
slashCommands: [{ name: "project" }],
651653
skills: [{ name: "project", path: "/project/SKILL.md", enabled: true }],
652-
} satisfies ServerProvider;
654+
optionDescriptors: [
655+
{
656+
id: "agent",
657+
label: "Agent",
658+
type: "select",
659+
options: [{ id: "readonly", label: "Readonly" }],
660+
},
661+
],
662+
} satisfies ProviderWorkspaceSnapshot;
653663

654664
const result = ProviderRegistry.upsertProviderWorkspaceSnapshot(
655665
provider,
@@ -659,12 +669,15 @@ it.layer(
659669

660670
assert.deepStrictEqual(result.slashCommands, provider.slashCommands);
661671
assert.deepStrictEqual(result.skills, provider.skills);
662-
assert.deepStrictEqual(result.workspaceSnapshots, [
672+
assert.strictEqual(result.models, provider.models);
673+
const decoded = decodeServerProvider(encodeServerProvider(result));
674+
assert.deepStrictEqual(decoded.workspaceSnapshots, [
663675
{
664676
cwd: "/project",
665677
checkedAt: scopedSnapshot.checkedAt,
666678
slashCommands: scopedSnapshot.slashCommands,
667679
skills: scopedSnapshot.skills,
680+
optionDescriptors: scopedSnapshot.optionDescriptors,
668681
},
669682
]);
670683

@@ -697,6 +710,47 @@ it.layer(
697710
{ name: "replacement" },
698711
]);
699712
assert.strictEqual(recovered.workspaceSnapshots?.[0]?.slashCommandsPending, undefined);
713+
714+
const { optionDescriptors, ...failedSnapshot } = scopedSnapshot;
715+
const firstFailure = ProviderRegistry.upsertProviderWorkspaceSnapshot(
716+
provider,
717+
"/project",
718+
failedSnapshot,
719+
);
720+
assert.strictEqual(
721+
Object.hasOwn(firstFailure.workspaceSnapshots![0]!, "optionDescriptors"),
722+
false,
723+
);
724+
const failedOtherProject = ProviderRegistry.upsertProviderWorkspaceSnapshot(
725+
recovered,
726+
"/other-project",
727+
failedSnapshot,
728+
);
729+
assert.strictEqual(
730+
Object.hasOwn(failedOtherProject.workspaceSnapshots![1]!, "optionDescriptors"),
731+
false,
732+
);
733+
734+
for (const clearedDescriptors of [[], [{ ...optionDescriptors[0]!, options: [] }]]) {
735+
const cleared = ProviderRegistry.upsertProviderWorkspaceSnapshot(recovered, "/project", {
736+
...scopedSnapshot,
737+
optionDescriptors: clearedDescriptors,
738+
});
739+
assert.deepStrictEqual(
740+
cleared.workspaceSnapshots?.[0]?.optionDescriptors,
741+
clearedDescriptors,
742+
);
743+
}
744+
745+
const failedRefresh = ProviderRegistry.upsertProviderWorkspaceSnapshot(
746+
failedOtherProject,
747+
"/project",
748+
failedSnapshot,
749+
);
750+
assert.deepStrictEqual(
751+
failedRefresh.workspaceSnapshots?.[1]?.optionDescriptors,
752+
optionDescriptors,
753+
);
700754
});
701755

702756
it("preserves previously discovered provider models when a refresh returns none", () => {

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ export function upsertProviderWorkspaceSnapshot(
124124
cwd: string,
125125
scopedSnapshot: ProviderWorkspaceSnapshot,
126126
): ServerProvider {
127+
const previousSnapshot = provider.workspaceSnapshots?.find((snapshot) => snapshot.cwd === cwd);
128+
const optionDescriptors = scopedSnapshot.optionDescriptors ?? previousSnapshot?.optionDescriptors;
127129
const workspaceSnapshot = {
128130
cwd,
129131
checkedAt: scopedSnapshot.checkedAt,
130132
slashCommands: scopedSnapshot.slashCommandsPending
131-
? (provider.workspaceSnapshots?.find((snapshot) => snapshot.cwd === cwd)?.slashCommands ??
132-
scopedSnapshot.slashCommands)
133+
? (previousSnapshot?.slashCommands ?? scopedSnapshot.slashCommands)
133134
: scopedSnapshot.slashCommands,
134135
...(scopedSnapshot.slashCommandsPending ? { slashCommandsPending: true } : {}),
135136
skills: scopedSnapshot.skills,
137+
...(optionDescriptors === undefined ? {} : { optionDescriptors }),
136138
} satisfies NonNullable<ServerProvider["workspaceSnapshots"]>[number];
137139
return {
138140
...provider,

‎packages/client-runtime/src/providerSkills.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getProviderSkillsForSlashMenu,
1414
hasCompleteProviderWorkspaceSnapshot,
1515
hasCurrentProviderWorkspaceSnapshot,
16+
resolveProviderForCwd,
1617
resolveProviderSkillsForCwd,
1718
resolveProviderSlashCommandsForCwd,
1819
resolveProviderSkillSourceKind,
@@ -303,3 +304,61 @@ describe("workspace provider snapshots", () => {
303304
);
304305
});
305306
});
307+
308+
describe("resolveProviderForCwd", () => {
309+
it("switches workspace options for every model without changing the machine catalog", () => {
310+
const reasoning = {
311+
id: "reasoning",
312+
label: "Reasoning",
313+
type: "select" as const,
314+
options: [{ id: "high", label: "High" }],
315+
};
316+
const globalAgent = {
317+
id: "agent",
318+
label: "Agent",
319+
type: "select" as const,
320+
options: [{ id: "build", label: "Build" }],
321+
};
322+
const workspaceAgent = {
323+
...globalAgent,
324+
options: [{ id: "readonly", label: "Readonly" }],
325+
};
326+
const emptyAgent = { ...globalAgent, options: [] };
327+
const catalog: ServerProvider = {
328+
...provider,
329+
models: [
330+
{
331+
slug: "model",
332+
name: "Model",
333+
isCustom: false,
334+
capabilities: { optionDescriptors: [globalAgent, reasoning] },
335+
},
336+
{ slug: "custom", name: "Custom", isCustom: true, capabilities: null },
337+
{ slug: "legacy", name: "Legacy", isCustom: false, capabilities: {} },
338+
],
339+
workspaceSnapshots: [
340+
...provider.workspaceSnapshots,
341+
...[workspaceAgent, emptyAgent].map((agent, index) => ({
342+
...provider.workspaceSnapshots[0]!,
343+
cwd: `/workspace/options-${index}`,
344+
optionDescriptors: [agent],
345+
})),
346+
],
347+
};
348+
const original = structuredClone(catalog);
349+
350+
for (const [index, agent] of [workspaceAgent, emptyAgent, workspaceAgent].entries()) {
351+
const scoped = resolveProviderForCwd(catalog, `/workspace/options-${index % 2}`);
352+
expect(scoped.models.map((model) => model.capabilities?.optionDescriptors)).toEqual([
353+
expect.arrayContaining([reasoning, agent]),
354+
[agent],
355+
[agent],
356+
]);
357+
expect(scoped.models[0]?.capabilities?.optionDescriptors).toHaveLength(2);
358+
}
359+
expect(catalog).toEqual(original);
360+
expect(resolveProviderForCwd(catalog, "/workspace/unknown")).toBe(catalog);
361+
expect(resolveProviderForCwd(catalog, "/workspace/project-a")).toBe(catalog);
362+
expect(resolveProviderForCwd(catalog, null)).toBe(catalog);
363+
});
364+
});

‎packages/client-runtime/src/providerSkills.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ export function hasCurrentProviderWorkspaceSnapshot(
114114
);
115115
}
116116

117+
export function resolveProviderForCwd(
118+
provider: ServerProvider,
119+
cwd: string | null | undefined,
120+
): ServerProvider {
121+
const descriptors = resolveProviderWorkspaceSnapshot(provider, cwd)?.optionDescriptors;
122+
if (descriptors === undefined) return provider;
123+
const overriddenIds = new Set(descriptors.map((descriptor) => descriptor.id));
124+
return {
125+
...provider,
126+
models: provider.models.map((model) => ({
127+
...model,
128+
capabilities: {
129+
...model.capabilities,
130+
optionDescriptors: [
131+
...(model.capabilities?.optionDescriptors ?? []).filter(
132+
(descriptor) => !overriddenIds.has(descriptor.id),
133+
),
134+
...descriptors,
135+
],
136+
},
137+
})),
138+
};
139+
}
140+
117141
export function resolveProviderSkillsForCwd(
118142
provider: ServerProvider,
119143
cwd: string | null | undefined,

‎packages/contracts/src/server.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
ResolvedKeybindingsConfig,
2424
} from "./keybindings.ts";
2525
import { EditorId, FileManagerRevealKind, RemoteOpenTarget } from "./editor.ts";
26-
import { ModelCapabilities } from "./model.ts";
26+
import { ModelCapabilities, ProviderOptionDescriptor } from "./model.ts";
2727
import { RuntimeMode } from "./providerPolicy.ts";
2828
import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
2929
import { ServerProviderUsageLimits, UsageLimitSourceSnapshots } from "./providerUsageLimits.ts";
@@ -134,6 +134,7 @@ export const ServerProviderWorkspaceSnapshot = Schema.Struct({
134134
slashCommands: Schema.Array(ServerProviderSlashCommand),
135135
slashCommandsPending: Schema.optional(Schema.Boolean),
136136
skills: Schema.Array(ServerProviderSkill),
137+
optionDescriptors: Schema.optional(Schema.Array(ProviderOptionDescriptor)),
137138
});
138139
export type ServerProviderWorkspaceSnapshot = typeof ServerProviderWorkspaceSnapshot.Type;
139140

‎packages/provider-core/src/server/driver.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface ProviderDriverMetadata {
3737
}
3838

3939
export type ProviderWorkspaceSnapshot = ServerProvider &
40-
Pick<ServerProviderWorkspaceSnapshot, "slashCommandsPending">;
40+
Pick<ServerProviderWorkspaceSnapshot, "slashCommandsPending" | "optionDescriptors">;
4141

4242
export interface ProviderInstance {
4343
readonly instanceId: ProviderInstanceId;

0 commit comments

Comments
 (0)