Enforce provider-model consistency for thread turn starts

- Reject turn starts that request a provider switch from the thread-bound provider
- Reject models that do not belong to the thread provider and log start failures
- Add shared `inferProviderForModel` helper and tests; reuse it in web store inference
This commit is contained in:
Julius Marminge committed 2026-03-16 17:09:38 -07:00
1 parent b403ca3183
commit fc9660f238
5 files changed
+160 -39

No files matched your search

@@ -389,7 +389,7 @@ describe("ProviderCommandReactor", () => {
});
});
it("starts first turn with requested provider when provider is specified", async () => {
it("rejects a first turn when requested provider conflicts with the thread model", async () => {
const harness = await createHarness();
const now = new Date().toISOString();
@@ -411,19 +411,78 @@ describe("ProviderCommandReactor", () => {
}),
);
await waitFor(() => harness.startSession.mock.calls.length === 1);
await waitFor(() => harness.sendTurn.mock.calls.length === 1);
expect(harness.startSession.mock.calls[0]?.[1]).toMatchObject({
provider: "claudeAgent",
cwd: "/tmp/provider-project",
model: "gpt-5-codex",
runtimeMode: "approval-required",
await waitFor(async () => {
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find(
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
);
return (
thread?.activities.some((activity) => activity.kind === "provider.turn.start.failed") ??
false
);
});
expect(harness.startSession).not.toHaveBeenCalled();
expect(harness.sendTurn).not.toHaveBeenCalled();
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find((entry) => entry.id === ThreadId.makeUnsafe("thread-1"));
expect(thread?.session?.providerName).toBe("claudeAgent");
expect(thread?.session?.threadId).toBe("thread-1");
expect(thread?.session).toBeNull();
expect(
thread?.activities.find((activity) => activity.kind === "provider.turn.start.failed"),
).toMatchObject({
summary: "Provider turn start failed",
payload: {
detail: expect.stringContaining("cannot switch to 'claudeAgent'"),
},
});
});
it("rejects a turn when the requested model belongs to a different provider", async () => {
const harness = await createHarness();
const now = new Date().toISOString();
await Effect.runPromise(
harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.makeUnsafe("cmd-turn-start-model-provider-mismatch"),
threadId: ThreadId.makeUnsafe("thread-1"),
message: {
messageId: asMessageId("user-message-model-provider-mismatch"),
role: "user",
text: "hello",
attachments: [],
},
model: "claude-sonnet-4-6",
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt: now,
}),
);
await waitFor(async () => {
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find(
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
);
return (
thread?.activities.some((activity) => activity.kind === "provider.turn.start.failed") ??
false
);
});
expect(harness.startSession).not.toHaveBeenCalled();
expect(harness.sendTurn).not.toHaveBeenCalled();
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find((entry) => entry.id === ThreadId.makeUnsafe("thread-1"));
expect(
thread?.activities.find((activity) => activity.kind === "provider.turn.start.failed"),
).toMatchObject({
payload: {
detail: expect.stringContaining("does not belong to provider 'codex'"),
},
});
});
it("reuses the same provider session when runtime mode is unchanged", async () => {
@@ -559,7 +618,7 @@ describe("ProviderCommandReactor", () => {
expect(thread?.session?.runtimeMode).toBe("approval-required");
});
it("switches provider by restarting the session when turn request provider changes", async () => {
it("rejects provider changes after a thread is already bound to a session provider", async () => {
const harness = await createHarness();
const now = new Date().toISOString();
@@ -601,22 +660,33 @@ describe("ProviderCommandReactor", () => {
}),
);
await waitFor(() => harness.startSession.mock.calls.length === 2);
await waitFor(() => harness.sendTurn.mock.calls.length === 2);
expect(harness.stopSession.mock.calls.length).toBe(0);
expect(harness.startSession.mock.calls[1]?.[1]).toMatchObject({
threadId: ThreadId.makeUnsafe("thread-1"),
provider: "claudeAgent",
runtimeMode: "approval-required",
await waitFor(async () => {
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find(
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
);
return (
thread?.activities.some((activity) => activity.kind === "provider.turn.start.failed") ??
false
);
});
expect(harness.startSession.mock.calls[1]?.[1]).not.toHaveProperty("resumeCursor");
expect(harness.startSession.mock.calls.length).toBe(1);
expect(harness.sendTurn.mock.calls.length).toBe(1);
expect(harness.stopSession.mock.calls.length).toBe(0);
const readModel = await Effect.runPromise(harness.engine.getReadModel());
const thread = readModel.threads.find((entry) => entry.id === ThreadId.makeUnsafe("thread-1"));
expect(thread?.session?.threadId).toBe("thread-1");
expect(thread?.session?.providerName).toBe("claudeAgent");
expect(thread?.session?.providerName).toBe("codex");
expect(thread?.session?.runtimeMode).toBe("approval-required");
expect(
thread?.activities.find((activity) => activity.kind === "provider.turn.start.failed"),
).toMatchObject({
payload: {
detail: expect.stringContaining("cannot switch to 'claudeAgent'"),
},
});
});
it("does not stop the active session when restart fails before rebind", async () => {
@@ -25,6 +25,7 @@ import {
ProviderCommandReactor,
type ProviderCommandReactorShape,
} from "../Services/ProviderCommandReactor.ts";
import { inferProviderForModel } from "@t3tools/shared/model";
type ProviderIntentEvent = Extract<
OrchestrationEvent,
@@ -210,7 +211,25 @@ const make = Effect.gen(function* () {
)
? thread.session.providerName
: undefined;
const preferredProvider: ProviderKind | undefined = options?.provider ?? currentProvider;
const threadProvider: ProviderKind = currentProvider ?? inferProviderForModel(thread.model);
if (options?.provider !== undefined && options.provider !== threadProvider) {
return yield* new ProviderAdapterRequestError({
provider: threadProvider,
method: "thread.turn.start",
detail: `Thread '${threadId}' is bound to provider '${threadProvider}' and cannot switch to '${options.provider}'.`,
});
}
if (
options?.model !== undefined &&
inferProviderForModel(options.model, threadProvider) !== threadProvider
) {
return yield* new ProviderAdapterRequestError({
provider: threadProvider,
method: "thread.turn.start",
detail: `Model '${options.model}' does not belong to provider '${threadProvider}' for thread '${threadId}'.`,
});
}
const preferredProvider: ProviderKind = currentProvider ?? options?.provider ?? threadProvider;
const desiredModel = options?.model ?? thread.model;
const effectiveCwd = resolveThreadWorkspaceCwd({
thread,
@@ -478,7 +497,18 @@ const make = Effect.gen(function* () {
: {}),
interactionMode: event.payload.interactionMode,
createdAt: event.payload.createdAt,
});
}).pipe(
Effect.catchCause((cause) =>
appendProviderFailureActivity({
threadId: event.payload.threadId,
kind: "provider.turn.start.failed",
summary: "Provider turn start failed",
detail: Cause.pretty(cause),
turnId: null,
createdAt: event.payload.createdAt,
}),
),
);
});
const processTurnInterruptRequested = Effect.fnUntraced(function* (
+2 -16
View File
@@ -7,8 +7,7 @@ import {
type OrchestrationSessionStatus,
} from "@t3tools/contracts";
import {
getModelOptions,
normalizeModelSlug,
inferProviderForModel,
resolveModelSlug,
resolveModelSlugForProvider,
} from "@t3tools/shared/model";
@@ -195,11 +194,6 @@ function toLegacyProvider(providerName: string | null): ProviderKind {
return "codex";
}
const CODEX_MODEL_SLUGS = new Set<string>(getModelOptions("codex").map((option) => option.slug));
const CLAUDE_MODEL_SLUGS = new Set<string>(
getModelOptions("claudeAgent").map((option) => option.slug),
);
function inferProviderForThreadModel(input: {
readonly model: string;
readonly sessionProviderName: string | null;
@@ -207,15 +201,7 @@ function inferProviderForThreadModel(input: {
if (input.sessionProviderName === "codex" || input.sessionProviderName === "claudeAgent") {
return input.sessionProviderName;
}
const normalizedClaude = normalizeModelSlug(input.model, "claudeAgent");
if (normalizedClaude && CLAUDE_MODEL_SLUGS.has(normalizedClaude)) {
return "claudeAgent";
}
const normalizedCodex = normalizeModelSlug(input.model, "codex");
if (normalizedCodex && CODEX_MODEL_SLUGS.has(normalizedCodex)) {
return "codex";
}
return input.model.trim().startsWith("claude-") ? "claudeAgent" : "codex";
return inferProviderForModel(input.model);
}
function resolveWsHttpOrigin(): string {
+18
View File
@@ -13,6 +13,7 @@ import {
getDefaultReasoningEffort,
getModelOptions,
getReasoningEffortOptions,
inferProviderForModel,
normalizeModelSlug,
resolveModelSlug,
resolveModelSlugForProvider,
@@ -92,6 +93,23 @@ describe("getReasoningEffortOptions", () => {
});
});
describe("inferProviderForModel", () => {
it("detects known provider model slugs", () => {
expect(inferProviderForModel("gpt-5.3-codex")).toBe("codex");
expect(inferProviderForModel("claude-sonnet-4-6")).toBe("claudeAgent");
expect(inferProviderForModel("sonnet")).toBe("claudeAgent");
});
it("falls back when the model is unknown", () => {
expect(inferProviderForModel("custom/internal-model")).toBe("codex");
expect(inferProviderForModel("custom/internal-model", "claudeAgent")).toBe("claudeAgent");
});
it("treats claude-prefixed custom slugs as claude", () => {
expect(inferProviderForModel("claude-custom-internal")).toBe("claudeAgent");
});
});
describe("getDefaultReasoningEffort", () => {
it("returns provider-scoped defaults", () => {
expect(getDefaultReasoningEffort("codex")).toBe(DEFAULT_REASONING_EFFORT_BY_PROVIDER.codex);
+17
View File
@@ -64,6 +64,23 @@ export function resolveModelSlugForProvider(
return resolveModelSlug(model, provider);
}
export function inferProviderForModel(
model: string | null | undefined,
fallback: ProviderKind = "codex",
): ProviderKind {
const normalizedClaude = normalizeModelSlug(model, "claudeAgent");
if (normalizedClaude && MODEL_SLUG_SET_BY_PROVIDER.claudeAgent.has(normalizedClaude)) {
return "claudeAgent";
}
const normalizedCodex = normalizeModelSlug(model, "codex");
if (normalizedCodex && MODEL_SLUG_SET_BY_PROVIDER.codex.has(normalizedCodex)) {
return "codex";
}
return typeof model === "string" && model.trim().startsWith("claude-") ? "claudeAgent" : fallback;
}
export function getReasoningEffortOptions(
provider: ProviderKind = "codex",
): ReadonlyArray<CodexReasoningEffort> {