Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion scripts/check-test-seam-comments.allowlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{ "file": "src/browser/stores/WorkspaceStore.ts", "symbol": "workspaceStore.addWorkspace", "reason": "production caller: App.tsx (workspace creation/restore paths)" },
{ "file": "src/browser/stores/WorkspaceStore.ts", "symbol": "workspaceStore.setActiveWorkspaceId", "reason": "production caller: WorkspaceContext.tsx active-workspace effect" },
{ "file": "src/browser/stores/WorkspaceStore.ts", "symbol": "findRenderedRefineProposalHash", "reason": "production caller: getDisplayedRefineProposalHash in the same file" },
{ "file": "src/browser/utils/aiSelectionIntent.ts", "symbol": "resetAiSelectionIntentForTests", "reason": "module-singleton reset hook named ...ForTests" },
{ "file": "src/browser/utils/highlighting/highlightWorkerClient.ts", "symbol": "highlightWithBudget", "reason": "production caller: enqueueHighlightWithBudget in the same file" },
{ "file": "src/browser/utils/highlighting/highlightWorkerClient.ts", "symbol": "enqueueHighlightWithBudget", "reason": "production caller: highlightCode in the same file" },
{ "file": "src/browser/utils/highlighting/highlightWorkerClient.ts", "symbol": "__resetForTests", "reason": "module-singleton reset hook named ...ForTests" },
Expand Down
30 changes: 13 additions & 17 deletions src/browser/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ import {
LEFT_SIDEBAR_WIDTH_KEY,
} from "@/common/constants/storage";
import { normalizeToCanonical } from "@/common/utils/ai/models";
import { getDefaultModel } from "@/browser/hooks/useModelsFromSettings";
import type { BranchListResult } from "@/common/orpc/types";
import type { UpdateChannel } from "@/common/types/project";
import { useTelemetry } from "./hooks/useTelemetry";
import { getRuntimeTypeForTelemetry } from "@/common/telemetry";
import { useStartWorkspaceCreation } from "./hooks/useStartWorkspaceCreation";
import { useAPI } from "@/browser/contexts/API";
import { requestActiveTurnThinkingLevel } from "@/browser/utils/activeTurnThinking";
import { resolveEffectiveComposerModel } from "@/browser/utils/workspaceAiSettingsSync";
import { getWorkspaceAiSelection } from "@/browser/utils/workspaceAiSettingsSync";
import { AuthTokenModal } from "@/browser/components/AuthTokenModal/AuthTokenModal";

import { ScratchPage } from "@/browser/components/ScratchPage/ScratchPage";
Expand Down Expand Up @@ -125,7 +124,6 @@ import { RosettaBanner } from "./components/RosettaBanner/RosettaBanner";

import { useExperimentValue } from "@/browser/hooks/useExperiments";
import { getAutoRoutingKey, setAutoRoutingChoice } from "@/browser/utils/modelChange";
import { readScopedAiDefault } from "@/browser/utils/scopedAiDefaults";
import { useProvidersConfig } from "@/browser/hooks/useProvidersConfig";
import { useRouting } from "@/browser/hooks/useRouting";
import { EXPERIMENT_IDS } from "@/common/constants/experiments";
Expand Down Expand Up @@ -498,25 +496,23 @@ function AppInner() {
} = useCommandRegistry();

/**
* Get the selected model for a workspace, preserving explicit gateway prefixes.
* The workspace's effective AI selection, preserving explicit gateway prefixes. A sub-agent
* resolves for its own agent.
*/
const getModelForWorkspace = useCallback(
(workspaceId: string): string => {
const defaultModel = getDefaultModel();
const preferredModel = readScopedAiDefault(workspaceId, "model") ?? null;
const getAiSelectionForWorkspace = useCallback(
(workspaceId: string) => {
const metadata = workspaceMetadata.get(workspaceId);
const persistedAgentId =
(readScopedAiDefault(workspaceId, "agentId") ?? WORKSPACE_DEFAULTS.agentId)
.trim()
.toLowerCase() || WORKSPACE_DEFAULTS.agentId;
const agentId =
metadata?.parentWorkspaceId != null && metadata.agentId
? metadata.agentId
: persistedAgentId;
return resolveEffectiveComposerModel(preferredModel, metadata, agentId, defaultModel);
return getWorkspaceAiSelection(
workspaceId,
metadata?.parentWorkspaceId != null && metadata.agentId ? metadata.agentId : undefined
);
},
[workspaceMetadata]
);
const getModelForWorkspace = useCallback(
(workspaceId: string): string => getAiSelectionForWorkspace(workspaceId).model,
[getAiSelectionForWorkspace]
);

const getThinkingLevelForWorkspace = useCallback(
(workspaceId: string): ThinkingLevel => {
Expand Down
16 changes: 3 additions & 13 deletions src/browser/components/TitleBar/TitleBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useAboutDialog } from "@/browser/contexts/AboutDialogContext";
import { useRouter } from "@/browser/contexts/RouterContext";
import { useSettings } from "@/browser/contexts/SettingsContext";
import { useRouting } from "@/browser/hooks/useRouting";
import { usePersistedState } from "@/browser/hooks/usePersistedState";
import { formatKeybind, KEYBINDS } from "@/browser/utils/ui/keybinds";
import {
formatMuxGatewayBalance,
Expand All @@ -26,7 +25,7 @@ import {
DESKTOP_TITLEBAR_HEIGHT_CLASS,
} from "@/browser/hooks/useDesktopTitlebar";

import { getModelKey } from "@/common/constants/storage";
import { useWorkspaceAiSelection } from "@/browser/utils/workspaceAiSettingsSync";
import { useAppConfig } from "@/browser/stores/AppConfigStore";
import { PROVIDER_DISPLAY_NAMES } from "@/common/constants/providers";
import {
Expand Down Expand Up @@ -77,17 +76,8 @@ export function TitleBar(props: TitleBarProps) {
useRouter();
const routing = useRouting();
const defaultModel = useAppConfig((config) => config.defaultModel) ?? WORKSPACE_DEFAULTS.model;
const workspaceModelKey = getModelKey(currentWorkspaceId ?? "__titlebar__");
const [workspaceModelOverride] = usePersistedState<string | null>(workspaceModelKey, null, {
listener: true,
});
const rawActiveModel =
currentWorkspaceId &&
typeof workspaceModelOverride === "string" &&
workspaceModelOverride.trim().length > 0
? workspaceModelOverride.trim()
: defaultModel;
const activeModel = normalizeSelectedModel(rawActiveModel);
const workspaceModel = useWorkspaceAiSelection(currentWorkspaceId ?? "").model;
const activeModel = normalizeSelectedModel(currentWorkspaceId ? workspaceModel : defaultModel);
const explicitGateway = getExplicitGatewayPrefix(activeModel);
const canonicalActiveModel = normalizeToCanonical(activeModel);
// Explicit gateway selections short-circuit route resolution: the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,36 +200,29 @@ describe("WorkspaceModeAISync", () => {
test("restores workspace-by-agent override on explicit agent switch when defaults inherit", async () => {
const workspaceId = nextWorkspaceId();

const planModel = "anthropic:claude-sonnet-4-5";
const planThinking = "high";
const execWorkspaceModel = "openai:gpt-5.2-pro";
const execWorkspaceThinking = "medium";

setTestAgentAiDefaults({});
updatePersistedState(getWorkspaceAISettingsByAgentKey(workspaceId), {
exec: { model: execWorkspaceModel, thinkingLevel: execWorkspaceThinking },
exec: { model: "openai:gpt-5.2-pro", thinkingLevel: execWorkspaceThinking },
});

updatePersistedState(getModelKey(workspaceId), planModel);
updatePersistedState(getThinkingLevelKey(workspaceId), planThinking);

const { rerender } = renderSync({ workspaceId, agentId: "plan" });

await waitFor(() => {
expect(readPersistedState(getModelKey(workspaceId), "")).toBe(planModel);
expect(readPersistedState(getThinkingLevelKey(workspaceId), "off")).toBe(planThinking);
});

rerender(<SyncHarness workspaceId={workspaceId} agentId="exec" />);

await waitFor(() => {
expect(readPersistedState(getModelKey(workspaceId), "")).toBe(execWorkspaceModel);
expect(readPersistedState(getThinkingLevelKey(workspaceId), "off")).toBe(
execWorkspaceThinking
);
});

expect(consumeWorkspaceModelChange(workspaceId, execWorkspaceModel)).toBe("agent");
});

test("ignores same-agent workspace overrides when agent defaults are missing", async () => {
Expand Down Expand Up @@ -307,15 +300,13 @@ describe("WorkspaceModeAISync", () => {
exec: { thinkingLevel: baseConfiguredThinking },
});

updatePersistedState(getModelKey(workspaceId), "some-legacy-model");
updatePersistedState(getThinkingLevelKey(workspaceId), "high");

// Unknown non-plan agent IDs still use exec as fallback agent; this verifies
// a partial custom settings entry blocks inheriting exec thinking defaults.
renderSync({ workspaceId, agentId: "custom" });

await waitFor(() => {
expect(readPersistedState(getModelKey(workspaceId), "")).toBe(customConfiguredModel);
expect(readPersistedState(getThinkingLevelKey(workspaceId), "off")).toBe("high");
});
});
Expand Down Expand Up @@ -354,7 +345,6 @@ describe("WorkspaceModeAISync", () => {
await waitFor(() => {
expect(readAuto(workspaceId)).toEqual({ model: true, thinkingLevel: true });
});
expect(readPersistedState(getModelKey(workspaceId), "")).toBe("openai:gpt-5.2");

switchTo(rerender, workspaceId, "plan");
await waitFor(() => {
Expand Down
64 changes: 35 additions & 29 deletions src/browser/components/WorkspaceModeAISync/WorkspaceModeAISync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { readPersistedState, updatePersistedState } from "@/browser/hooks/usePer
import { useAgentAiDefaults } from "@/browser/stores/AppConfigStore";
import {
getAutoRoutingChoiceByAgentKey,
getModelKey,
getReasoningModeKey,
getThinkingLevelKey,
getWorkspaceAISettingsByAgentKey,
} from "@/common/constants/storage";
import { getDefaultModel } from "@/browser/hooks/useModelsFromSettings";
import {
applyAutoRoutingOutcome,
setWorkspaceModelWithOrigin,
recordWorkspaceModelChange,
setWorkspaceThinkingLevelWithOrigin,
} from "@/browser/utils/modelChange";
import {
Expand All @@ -21,6 +20,7 @@ import {
type AutoRoutingChoiceByAgent,
type WorkspaceAISettingsCache,
} from "@/browser/utils/workspaceModeAi";
import { getWorkspaceAiSelection } from "@/browser/utils/workspaceAiSettingsSync";
import { useExperimentValue } from "@/browser/hooks/useExperiments";
import { EXPERIMENT_IDS } from "@/common/constants/experiments";
import type { OpenAIReasoningMode, ThinkingLevel } from "@/common/types/thinking";
Expand All @@ -41,15 +41,15 @@ export function WorkspaceModeAISync(props: { workspaceId: string }): null {

useEffect(() => {
const fallbackModel = getDefaultModel();
const modelKey = getModelKey(workspaceId);
const thinkingKey = getThinkingLevelKey(workspaceId);

const normalizedAgentId = normalizeAgentId(agentId, "exec");
const previousAgentId = prevAgentIdRef.current;

const isExplicitAgentSwitch =
prevAgentIdRef.current !== null &&
previousAgentId !== null &&
prevWorkspaceIdRef.current === workspaceId &&
prevAgentIdRef.current !== normalizedAgentId;
previousAgentId !== normalizedAgentId;

// Update refs for the next run (even if no model changes).
prevAgentIdRef.current = normalizedAgentId;
Expand All @@ -63,27 +63,41 @@ export function WorkspaceModeAISync(props: { workspaceId: string }): null {
{}
);

const existingModel = readPersistedState<string>(modelKey, fallbackModel);
const existingThinking = readPersistedState<ThinkingLevel>(thinkingKey, "off");
const reasoningKey = getReasoningModeKey(workspaceId);
const existingReasoning = readPersistedState<OpenAIReasoningMode>(reasoningKey, "standard");

const agentBaseById = new Map(agents.map((agent) => [agent.id, agent.base]));
const { resolvedModel, resolvedThinking, resolvedReasoningMode } =
resolveWorkspaceAiSettingsForAgent({
agentId: normalizedAgentId,
agentAiDefaults,
// Keep deterministic handoff behavior: background sync should trust the
// currently active workspace model, but explicit mode switches should
// restore the selected agent's per-workspace override (if any).
workspaceByAgent,
useWorkspaceByAgentFallback: isExplicitAgentSwitch,
fallbackModel,
existingModel,
existingThinking,
existingReasoningMode: existingReasoning,
agentBaseById,
});
const resolvedModel = getWorkspaceAiSelection(
workspaceId,
normalizedAgentId,
agentBaseById
).model;
if (isExplicitAgentSwitch) {
// Each agent resolves its own model, so the switch itself is the explicit model change.
recordWorkspaceModelChange(
workspaceId,
resolvedModel,
"agent",
getWorkspaceAiSelection(workspaceId, previousAgentId, agentBaseById).model
);
}

// The resolver owns the model; only thinking and reasoning come from here.
const { resolvedThinking, resolvedReasoningMode } = resolveWorkspaceAiSettingsForAgent({
agentId: normalizedAgentId,
agentAiDefaults,
// Keep deterministic handoff behavior: background sync should trust the
// currently active workspace settings, but explicit mode switches should
// restore the selected agent's per-workspace override (if any).
workspaceByAgent,
useWorkspaceByAgentFallback: isExplicitAgentSwitch,
fallbackModel,
existingModel: resolvedModel,
existingThinking,
existingReasoningMode: existingReasoning,
agentBaseById,
});
const autoRoutingOutcome = resolveAutoRoutingForAgent({
agentId: normalizedAgentId,
agentAiDefaults,
Expand All @@ -97,14 +111,6 @@ export function WorkspaceModeAISync(props: { workspaceId: string }): null {
workspaceByAgent,
});

if (existingModel !== resolvedModel) {
setWorkspaceModelWithOrigin(
workspaceId,
resolvedModel,
isExplicitAgentSwitch ? "agent" : "sync"
);
}

if (existingThinking !== resolvedThinking) {
setWorkspaceThinkingLevelWithOrigin(
workspaceId,
Expand Down
9 changes: 0 additions & 9 deletions src/browser/contexts/ThinkingContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type { FrontendWorkspaceMetadata } from "@/common/types/workspace";
import type { ThinkingLevel } from "@/common/types/thinking";
import {
getAutoThinkingLevelKey,
getModelKey,
getProjectScopeId,
getReasoningModeKey,
getThinkingLevelByModelKey,
Expand Down Expand Up @@ -174,11 +173,7 @@ function applyWorkspaceStorageOverrides(props: {
modelOverride?: string | null;
thinkingOverride?: "off" | null;
}) {
if (props.modelOverride === null) {
window.localStorage.removeItem(getModelKey(props.workspaceId));
}
if (props.modelOverride != null) {
updatePersistedState(getModelKey(props.workspaceId), props.modelOverride);
markAiSelectionIntent(props.workspaceId, "model", props.modelOverride);
}

Expand Down Expand Up @@ -569,7 +564,6 @@ describe("ThinkingContext", () => {
test("switching models does not remount children", async () => {
const workspaceId = "ws-1";

updatePersistedState(getModelKey(workspaceId), "openai:gpt-5.2");
markAiSelectionIntent(workspaceId, "model", "openai:gpt-5.2");
updatePersistedState(getThinkingLevelKey(workspaceId), "high");

Expand Down Expand Up @@ -597,7 +591,6 @@ describe("ThinkingContext", () => {
});

act(() => {
updatePersistedState(getModelKey(workspaceId), "anthropic:claude-3.5");
markAiSelectionIntent(workspaceId, "model", "anthropic:claude-3.5");
});

Expand All @@ -611,7 +604,6 @@ describe("ThinkingContext", () => {
test("migrates legacy per-model thinking to the workspace-scoped key", async () => {
const workspaceId = "ws-1";

updatePersistedState(getModelKey(workspaceId), "openai:gpt-5.2");
markAiSelectionIntent(workspaceId, "model", "openai:gpt-5.2");
updatePersistedState(getThinkingLevelByModelKey("openai:gpt-5.2"), "low");

Expand All @@ -632,7 +624,6 @@ describe("ThinkingContext", () => {

// Switching models should not change the workspace-scoped value.
act(() => {
updatePersistedState(getModelKey(workspaceId), "anthropic:claude-3.5");
markAiSelectionIntent(workspaceId, "model", "anthropic:claude-3.5");
});

Expand Down
Loading
Loading