Preserve null model options in projections and restarts
- Store missing model options as SQL NULL in project and thread projections - Stop rehydrating derived model options when restarting provider sessions - Share model option conversion logic in `packages/shared`
This commit is contained in:
1 parent
71ff626e3e
commit
07e86e938b
9 files changed
+231
-56
No files matched your search
@@ -43,7 +43,7 @@ import {
|
||||
type ProjectionSnapshotQueryShape,
|
||||
} from "../Services/ProjectionSnapshotQuery.ts";
|
||||
|
||||
const decodeReadModelSync = Schema.decodeUnknownSync(OrchestrationReadModel);
|
||||
const decodeReadModel = Schema.decodeUnknownEffect(OrchestrationReadModel);
|
||||
const ProjectionProjectDbRowSchema = Schema.Struct({
|
||||
projectId: ProjectionProject.fields.projectId,
|
||||
title: ProjectionProject.fields.title,
|
||||
@@ -103,7 +103,7 @@ const ProjectionLatestTurnDbRowSchema = Schema.Struct({
|
||||
sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),
|
||||
});
|
||||
const ProjectionStateDbRowSchema = ProjectionState;
|
||||
const decodeModelSelectionSync = Schema.decodeUnknownSync(ModelSelection);
|
||||
const decodeModelSelectionSchema = Schema.decodeUnknownEffect(ModelSelection);
|
||||
|
||||
const REQUIRED_SNAPSHOT_PROJECTORS = [
|
||||
ORCHESTRATION_PROJECTOR_NAMES.projects,
|
||||
@@ -158,20 +158,13 @@ const decodeModelSelection = (input: {
|
||||
readonly model: string;
|
||||
readonly options: unknown;
|
||||
}) =>
|
||||
Effect.try({
|
||||
try: () =>
|
||||
decodeModelSelectionSync({
|
||||
provider: input.provider,
|
||||
model: input.model,
|
||||
...(input.options !== null && input.options !== undefined
|
||||
? { options: input.options }
|
||||
: {}),
|
||||
}),
|
||||
catch: (error) =>
|
||||
Schema.isSchemaError(error)
|
||||
? toPersistenceDecodeError("ProjectionSnapshotQuery.decodeModelSelection")(error)
|
||||
: toPersistenceSqlError("ProjectionSnapshotQuery.decodeModelSelection")(error),
|
||||
});
|
||||
decodeModelSelectionSchema({
|
||||
provider: input.provider,
|
||||
model: input.model,
|
||||
...(input.options !== null && input.options !== undefined ? { options: input.options } : {}),
|
||||
}).pipe(
|
||||
Effect.mapError(toPersistenceDecodeError("ProjectionSnapshotQuery.decodeModelSelection")),
|
||||
);
|
||||
|
||||
const makeProjectionSnapshotQuery = Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
@@ -646,13 +639,11 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
|
||||
updatedAt: updatedAt ?? new Date(0).toISOString(),
|
||||
};
|
||||
|
||||
return yield* Effect.try({
|
||||
try: () => decodeReadModelSync(snapshot),
|
||||
catch: (error) =>
|
||||
toPersistenceDecodeError("ProjectionSnapshotQuery.getSnapshot:decodeReadModel")(
|
||||
error as Schema.SchemaError,
|
||||
),
|
||||
});
|
||||
return yield* decodeReadModel(snapshot).pipe(
|
||||
Effect.mapError(
|
||||
toPersistenceDecodeError("ProjectionSnapshotQuery.getSnapshot:decodeReadModel"),
|
||||
),
|
||||
);
|
||||
}),
|
||||
)
|
||||
.pipe(
|
||||
|
||||
@@ -804,6 +804,50 @@ describe("ProviderCommandReactor", () => {
|
||||
expect(thread?.session?.runtimeMode).toBe("approval-required");
|
||||
});
|
||||
|
||||
it("does not inject derived model options when restarting claude on runtime mode changes", async () => {
|
||||
const harness = await createHarness({
|
||||
threadModel: "claude-opus-4-6",
|
||||
});
|
||||
const now = new Date().toISOString();
|
||||
|
||||
await Effect.runPromise(
|
||||
harness.engine.dispatch({
|
||||
type: "thread.session.set",
|
||||
commandId: CommandId.makeUnsafe("cmd-session-set-runtime-mode-claude"),
|
||||
threadId: ThreadId.makeUnsafe("thread-1"),
|
||||
session: {
|
||||
threadId: ThreadId.makeUnsafe("thread-1"),
|
||||
status: "ready",
|
||||
providerName: "claudeAgent",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: now,
|
||||
},
|
||||
createdAt: now,
|
||||
}),
|
||||
);
|
||||
|
||||
await Effect.runPromise(
|
||||
harness.engine.dispatch({
|
||||
type: "thread.runtime-mode.set",
|
||||
commandId: CommandId.makeUnsafe("cmd-runtime-mode-set-claude-no-options"),
|
||||
threadId: ThreadId.makeUnsafe("thread-1"),
|
||||
runtimeMode: "approval-required",
|
||||
createdAt: now,
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => harness.startSession.mock.calls.length === 1);
|
||||
|
||||
expect(harness.startSession.mock.calls[0]?.[1]).toMatchObject({
|
||||
provider: "claudeAgent",
|
||||
model: "claude-opus-4-6",
|
||||
runtimeMode: "approval-required",
|
||||
});
|
||||
expect(harness.startSession.mock.calls[0]?.[1]).not.toHaveProperty("modelOptions");
|
||||
});
|
||||
|
||||
it("rejects provider changes after a thread is already bound to a session provider", async () => {
|
||||
const harness = await createHarness();
|
||||
const now = new Date().toISOString();
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from "@t3tools/contracts";
|
||||
import { Cache, Cause, Duration, Effect, Layer, Option, Schema, Stream } from "effect";
|
||||
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
|
||||
import { toProviderModelOptions } from "@t3tools/shared/model";
|
||||
|
||||
import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts";
|
||||
import { GitCore } from "../../git/Services/GitCore.ts";
|
||||
@@ -81,17 +82,6 @@ const sameModelOptions = (
|
||||
right: ProviderModelOptions | undefined,
|
||||
): boolean => JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
|
||||
|
||||
function toProviderModelOptions(
|
||||
modelSelection: ModelSelection | undefined,
|
||||
): ProviderModelOptions | undefined {
|
||||
if (!modelSelection?.options) {
|
||||
return undefined;
|
||||
}
|
||||
return modelSelection.provider === "codex"
|
||||
? { codex: modelSelection.options }
|
||||
: { claudeAgent: modelSelection.options };
|
||||
}
|
||||
|
||||
function isUnknownPendingApprovalRequestError(cause: Cause.Cause<ProviderServiceError>): boolean {
|
||||
const error = Cause.squash(cause);
|
||||
if (Schema.is(ProviderAdapterRequestError)(error)) {
|
||||
@@ -705,9 +695,7 @@ const make = Effect.gen(function* () {
|
||||
return;
|
||||
}
|
||||
const cachedProviderOptions = threadProviderOptions.get(event.payload.threadId);
|
||||
const cachedModelOptions =
|
||||
threadModelOptions.get(event.payload.threadId) ??
|
||||
toProviderModelOptions(thread.modelSelection);
|
||||
const cachedModelOptions = threadModelOptions.get(event.payload.threadId);
|
||||
yield* ensureSessionForThread(event.payload.threadId, event.occurredAt, {
|
||||
...(cachedProviderOptions !== undefined
|
||||
? { providerOptions: cachedProviderOptions }
|
||||
|
||||
@@ -77,7 +77,11 @@ const makeProjectionProjectRepository = Effect.gen(function* () {
|
||||
${row.workspaceRoot},
|
||||
${row.defaultModelSelection?.provider ?? null},
|
||||
${row.defaultModelSelection?.model ?? null},
|
||||
${JSON.stringify(row.defaultModelSelection?.options ?? null)},
|
||||
${
|
||||
row.defaultModelSelection?.options != null
|
||||
? JSON.stringify(row.defaultModelSelection.options)
|
||||
: null
|
||||
},
|
||||
${JSON.stringify(row.scripts)},
|
||||
${row.createdAt},
|
||||
${row.updatedAt},
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import { ProjectId, ThreadId } from "@t3tools/contracts";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, Layer, Option } from "effect";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
|
||||
import { SqlitePersistenceMemory } from "./Sqlite.ts";
|
||||
import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts";
|
||||
import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts";
|
||||
import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts";
|
||||
import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts";
|
||||
|
||||
const projectionRepositoriesLayer = it.layer(
|
||||
Layer.mergeAll(
|
||||
ProjectionProjectRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
|
||||
ProjectionThreadRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
|
||||
SqlitePersistenceMemory,
|
||||
),
|
||||
);
|
||||
|
||||
projectionRepositoriesLayer("Projection repositories", (it) => {
|
||||
it.effect("stores SQL NULL for missing project model options", () =>
|
||||
Effect.gen(function* () {
|
||||
const projects = yield* ProjectionProjectRepository;
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
|
||||
yield* projects.upsert({
|
||||
projectId: ProjectId.makeUnsafe("project-null-options"),
|
||||
title: "Null options project",
|
||||
workspaceRoot: "/tmp/project-null-options",
|
||||
defaultModelSelection: {
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
},
|
||||
scripts: [],
|
||||
createdAt: "2026-03-24T00:00:00.000Z",
|
||||
updatedAt: "2026-03-24T00:00:00.000Z",
|
||||
deletedAt: null,
|
||||
});
|
||||
|
||||
const rows = yield* sql<{
|
||||
readonly defaultModelOptions: string | null;
|
||||
}>`
|
||||
SELECT default_model_options_json AS "defaultModelOptions"
|
||||
FROM projection_projects
|
||||
WHERE project_id = 'project-null-options'
|
||||
`;
|
||||
const row = rows[0];
|
||||
if (!row) {
|
||||
return yield* Effect.fail(new Error("Expected projection_projects row to exist."));
|
||||
}
|
||||
|
||||
assert.strictEqual(row.defaultModelOptions, null);
|
||||
|
||||
const persisted = yield* projects.getById({
|
||||
projectId: ProjectId.makeUnsafe("project-null-options"),
|
||||
});
|
||||
assert.deepStrictEqual(Option.getOrNull(persisted)?.defaultModelSelection, {
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("stores SQL NULL for missing thread model options", () =>
|
||||
Effect.gen(function* () {
|
||||
const threads = yield* ProjectionThreadRepository;
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
|
||||
yield* threads.upsert({
|
||||
threadId: ThreadId.makeUnsafe("thread-null-options"),
|
||||
projectId: ProjectId.makeUnsafe("project-null-options"),
|
||||
title: "Null options thread",
|
||||
modelSelection: {
|
||||
provider: "claudeAgent",
|
||||
model: "claude-opus-4-6",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
latestTurnId: null,
|
||||
createdAt: "2026-03-24T00:00:00.000Z",
|
||||
updatedAt: "2026-03-24T00:00:00.000Z",
|
||||
deletedAt: null,
|
||||
});
|
||||
|
||||
const rows = yield* sql<{
|
||||
readonly modelOptions: string | null;
|
||||
}>`
|
||||
SELECT model_options_json AS "modelOptions"
|
||||
FROM projection_threads
|
||||
WHERE thread_id = 'thread-null-options'
|
||||
`;
|
||||
const row = rows[0];
|
||||
if (!row) {
|
||||
return yield* Effect.fail(new Error("Expected projection_threads row to exist."));
|
||||
}
|
||||
|
||||
assert.strictEqual(row.modelOptions, null);
|
||||
|
||||
const persisted = yield* threads.getById({
|
||||
threadId: ThreadId.makeUnsafe("thread-null-options"),
|
||||
});
|
||||
assert.deepStrictEqual(Option.getOrNull(persisted)?.modelSelection, {
|
||||
provider: "claudeAgent",
|
||||
model: "claude-opus-4-6",
|
||||
});
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -89,7 +89,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
|
||||
${row.title},
|
||||
${row.modelSelection.provider},
|
||||
${row.modelSelection.model},
|
||||
${JSON.stringify(row.modelSelection.options ?? null)},
|
||||
${row.modelSelection.options != null ? JSON.stringify(row.modelSelection.options) : null},
|
||||
${row.runtimeMode},
|
||||
${row.interactionMode},
|
||||
${row.branch},
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
getDefaultModel,
|
||||
normalizeModelSlug,
|
||||
resolveModelSlugForProvider,
|
||||
toProviderModelOptions,
|
||||
} from "@t3tools/shared/model";
|
||||
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
@@ -210,17 +211,6 @@ function extractModelSelectionOptions(
|
||||
): ModelSelection["options"] | undefined {
|
||||
return provider === "codex" ? modelOptions?.codex : modelOptions?.claudeAgent;
|
||||
}
|
||||
|
||||
function toProviderModelOptions(
|
||||
modelSelection: ModelSelection | null | undefined,
|
||||
): ProviderModelOptions | undefined {
|
||||
if (!modelSelection?.options) {
|
||||
return undefined;
|
||||
}
|
||||
return modelSelection.provider === "codex"
|
||||
? { codex: modelSelection.options }
|
||||
: { claudeAgent: modelSelection.options };
|
||||
}
|
||||
const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
|
||||
const SCRIPT_TERMINAL_COLS = 120;
|
||||
const SCRIPT_TERMINAL_ROWS = 30;
|
||||
@@ -3035,12 +3025,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
text: implementationPrompt,
|
||||
});
|
||||
const nextThreadTitle = truncateTitle(buildPlanImplementationThreadTitle(planMarkdown));
|
||||
const nextThreadModelSelection: ModelSelection = selectedModelSelection ??
|
||||
activeThread.modelSelection ??
|
||||
activeProject.defaultModelSelection ?? {
|
||||
provider: "codex",
|
||||
model: DEFAULT_MODEL_BY_PROVIDER.codex,
|
||||
};
|
||||
const nextThreadModelSelection: ModelSelection = selectedModelSelection;
|
||||
|
||||
sendInFlightRef.current = true;
|
||||
beginSendPhase("sending-turn");
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
supportsClaudeFastMode,
|
||||
supportsClaudeMaxEffort,
|
||||
supportsClaudeThinkingToggle,
|
||||
toProviderModelOptions,
|
||||
supportsClaudeUltrathinkKeyword,
|
||||
} from "./model";
|
||||
|
||||
@@ -272,6 +273,45 @@ describe("normalizeClaudeModelOptions", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("toProviderModelOptions", () => {
|
||||
it("returns undefined when no model options exist", () => {
|
||||
expect(toProviderModelOptions({ provider: "codex", model: "gpt-5.4" })).toBeUndefined();
|
||||
expect(toProviderModelOptions(null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("scopes codex options under the codex provider key", () => {
|
||||
expect(
|
||||
toProviderModelOptions({
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
options: {
|
||||
fastMode: true,
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
codex: {
|
||||
fastMode: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("scopes claude options under the claudeAgent provider key", () => {
|
||||
expect(
|
||||
toProviderModelOptions({
|
||||
provider: "claudeAgent",
|
||||
model: "claude-opus-4-6",
|
||||
options: {
|
||||
effort: "max",
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
claudeAgent: {
|
||||
effort: "max",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("supportsClaudeAdaptiveReasoning", () => {
|
||||
it("only enables adaptive reasoning for Opus 4.6 and Sonnet 4.6", () => {
|
||||
expect(supportsClaudeAdaptiveReasoning("claude-opus-4-6")).toBe(true);
|
||||
|
||||
@@ -10,7 +10,9 @@ import {
|
||||
type ClaudeCodeEffort,
|
||||
type CodexModelOptions,
|
||||
type CodexReasoningEffort,
|
||||
type ModelSelection,
|
||||
type ModelSlug,
|
||||
type ProviderModelOptions,
|
||||
type ProviderReasoningEffort,
|
||||
type ProviderKind,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -266,4 +268,15 @@ export function applyClaudePromptEffortPrefix(
|
||||
return `Ultrathink:\n${trimmed}`;
|
||||
}
|
||||
|
||||
export function toProviderModelOptions(
|
||||
modelSelection: ModelSelection | null | undefined,
|
||||
): ProviderModelOptions | undefined {
|
||||
if (!modelSelection?.options) {
|
||||
return undefined;
|
||||
}
|
||||
return modelSelection.provider === "codex"
|
||||
? { codex: modelSelection.options }
|
||||
: { claudeAgent: modelSelection.options };
|
||||
}
|
||||
|
||||
export { CLAUDE_CODE_EFFORT_OPTIONS, CODEX_REASONING_EFFORT_OPTIONS };
|
||||
Reference in new issue
Block a user