Skip to content

Commit 649672a

Browse files
committed
fix(models): restore sticky new-thread selections
1 parent b8262b4 commit 649672a

17 files changed

Lines changed: 295 additions & 44 deletions

‎apps/mobile/src/lib/modelOptions.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("mobile model options", () => {
102102
]);
103103
});
104104

105-
it("normalizes a legacy fallback selection against current capabilities", () => {
105+
it("does not materialize catalog defaults for missing stored options", () => {
106106
const config = {
107107
providers: [
108108
{
@@ -140,11 +140,17 @@ describe("mobile model options", () => {
140140
const [option] = buildModelOptions(config, {
141141
instanceId: ProviderInstanceId.make("codex"),
142142
model: "gpt-test",
143-
options: [{ id: "fastMode", value: true }],
144143
});
145144

146145
expect(option?.capabilities?.optionDescriptors?.[0]?.id).toBe("serviceTier");
147-
expect(option?.selection.options).toEqual([{ id: "serviceTier", value: "default" }]);
146+
expect(option?.selection.options).toBeUndefined();
147+
148+
const [explicitOption] = buildModelOptions(config, {
149+
instanceId: ProviderInstanceId.make("codex"),
150+
model: "gpt-test",
151+
options: [{ id: "serviceTier", value: "priority" }],
152+
});
153+
expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]);
148154
});
149155

150156
it("rejects stored selections whose provider is not usable", () => {

‎apps/mobile/src/lib/modelOptions.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
ServerConfig as T3ServerConfig,
55
} from "@t3tools/contracts";
66
import {
7-
buildProviderOptionSelectionsFromDescriptors,
7+
buildExplicitProviderOptionSelectionsFromDescriptors,
88
getProviderOptionDescriptors,
99
} from "@t3tools/shared/model";
1010

@@ -45,11 +45,12 @@ function normalizeSelectionOptions(
4545
if (!capabilities) {
4646
return selection;
4747
}
48-
const options = buildProviderOptionSelectionsFromDescriptors(
48+
const options = buildExplicitProviderOptionSelectionsFromDescriptors(
4949
getProviderOptionDescriptors({
5050
caps: capabilities,
5151
selections: selection.options,
5252
}),
53+
selection.options,
5354
);
5455
return options
5556
? { ...selection, options }

‎apps/server/src/cli/project.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSn
3030
import { OrchestrationLayerLive } from "../orchestration/runtimeLayer.ts";
3131
import { layerConfig as SqlitePersistenceLayerLive } from "../persistence/Layers/Sqlite.ts";
3232
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
33-
import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts";
3433
import {
3534
clearPersistedServerRuntimeState,
3635
readPersistedServerRuntimeState,
@@ -481,7 +480,6 @@ const projectAddCommand = Command.make("add", {
481480
projectId,
482481
title,
483482
workspaceRoot,
484-
defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
485483
createdAt: DateTime.formatIso(yield* DateTime.now),
486484
});
487485
return `Added project ${projectId} (${title}) at ${workspaceRoot}.`;

‎apps/server/src/orchestration/decider.projectThreadEnvMode.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { CommandId, EventId, ProjectId, type OrchestrationEvent } from "@t3tools/contracts";
1+
import {
2+
CommandId,
3+
EventId,
4+
ProjectId,
5+
ProviderInstanceId,
6+
type ModelSelection,
7+
type OrchestrationEvent,
8+
} from "@t3tools/contracts";
29
import { expect, it } from "@effect/vitest";
310
import * as Effect from "effect/Effect";
411
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -31,7 +38,53 @@ const seedProjectCreated = (sequence: number): OrchestrationEvent => ({
3138
},
3239
});
3340

34-
it.layer(NodeServices.layer)("decider project defaultThreadEnvMode", (it) => {
41+
it.layer(NodeServices.layer)("decider project defaults", (it) => {
42+
it.effect("only treats metadata updates as explicit model defaults", () =>
43+
Effect.gen(function* () {
44+
const selection: ModelSelection = {
45+
instanceId: ProviderInstanceId.make("codex"),
46+
model: "gpt-5.6-sol",
47+
options: [{ id: "reasoningEffort", value: "high" }],
48+
};
49+
const created = yield* decideOrchestrationCommand({
50+
command: {
51+
type: "project.create",
52+
commandId: CommandId.make("cmd-project-model-create"),
53+
projectId,
54+
title: "Model default",
55+
workspaceRoot: "/tmp/model-default",
56+
defaultModelSelection: selection,
57+
createdAt: now,
58+
},
59+
readModel: createEmptyReadModel(now),
60+
});
61+
const createdEvent = Array.isArray(created) ? created[0] : created;
62+
expect(createdEvent.type).toBe("project.created");
63+
expect(
64+
(createdEvent.payload as { defaultModelSelection?: unknown }).defaultModelSelection,
65+
).toBeNull();
66+
67+
const withProject = yield* projectEvent(createEmptyReadModel(now), {
68+
...createdEvent,
69+
sequence: 1,
70+
});
71+
const updated = yield* decideOrchestrationCommand({
72+
command: {
73+
type: "project.meta.update",
74+
commandId: CommandId.make("cmd-project-model-update"),
75+
projectId,
76+
defaultModelSelection: selection,
77+
},
78+
readModel: withProject,
79+
});
80+
const updatedEvent = Array.isArray(updated) ? updated[0] : updated;
81+
expect(updatedEvent.type).toBe("project.meta-updated");
82+
expect(
83+
(updatedEvent.payload as { defaultModelSelection?: unknown }).defaultModelSelection,
84+
).toEqual(selection);
85+
}),
86+
);
87+
3588
it.effect("propagates defaultThreadEnvMode through meta.update into the read model", () =>
3689
Effect.gen(function* () {
3790
const readModel = yield* projectEvent(createEmptyReadModel(now), seedProjectCreated(1));

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
219219
projectId: command.projectId,
220220
title: command.title,
221221
workspaceRoot: command.workspaceRoot,
222-
defaultModelSelection: command.defaultModelSelection ?? null,
222+
// Project creation has no user model choice. Older clients sent an
223+
// automatic seed here, but only a metadata update records an
224+
// explicit project default.
225+
defaultModelSelection: null,
223226
faviconPath: null,
224227
scripts: [],
225228
createdAt: command.createdAt,

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Migration0040 from "./Migrations/040_ProjectionProjectFaviconPath.ts";
5555
import Migration0041 from "./Migrations/041_AuthSessionClientConnection.ts";
5656
import Migration0042 from "./Migrations/042_ProjectionThreadLinkedPullRequest.ts";
5757
import Migration0043 from "./Migrations/043_ProjectionThreadsUnsettledAt.ts";
58+
import Migration0044 from "./Migrations/044_ClearAutomaticProjectModelDefaults.ts";
5859

5960
/**
6061
* Migration loader with all migrations defined inline.
@@ -110,6 +111,7 @@ export const migrationEntries = [
110111
[41, "AuthSessionClientConnection", Migration0041],
111112
[42, "ProjectionThreadLinkedPullRequest", Migration0042],
112113
[43, "ProjectionThreadsUnsettledAt", Migration0043],
114+
[44, "ClearAutomaticProjectModelDefaults", Migration0044],
113115
] as const;
114116

115117
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);

‎apps/server/src/persistence/Migrations/016_CanonicalizeModelSelections.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,93 @@ layer("016_CanonicalizeModelSelections", (it) => {
380380
}),
381381
);
382382
});
383+
384+
layer("044_ClearAutomaticProjectModelDefaults", (it) => {
385+
it.effect("clears create-time seeds and preserves explicit project defaults", () =>
386+
Effect.gen(function* () {
387+
const sql = yield* SqlClient.SqlClient;
388+
yield* runMigrations({ toMigrationInclusive: 43 });
389+
390+
yield* sql`
391+
INSERT INTO projection_projects (
392+
project_id,
393+
title,
394+
workspace_root,
395+
default_model_selection_json,
396+
default_thread_env_mode,
397+
favicon_path,
398+
scripts_json,
399+
created_at,
400+
updated_at,
401+
deleted_at
402+
)
403+
VALUES
404+
('project-auto', 'Auto', '/tmp/auto', '{"instanceId":"codex","model":"gpt-5.6-sol"}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-01T00:00:00.000Z', NULL),
405+
('project-title-only', 'Title only', '/tmp/title-only', '{"instanceId":"codex","model":"gpt-5.6-sol"}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-02T00:00:00.000Z', NULL),
406+
('project-explicit', 'Explicit', '/tmp/explicit', '{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}', NULL, NULL, '[]', '2026-08-01T00:00:00.000Z', '2026-08-02T00:00:00.000Z', NULL)
407+
`;
408+
409+
yield* sql`
410+
INSERT INTO orchestration_events (
411+
event_id,
412+
aggregate_kind,
413+
stream_id,
414+
stream_version,
415+
event_type,
416+
occurred_at,
417+
command_id,
418+
causation_event_id,
419+
correlation_id,
420+
actor_kind,
421+
payload_json,
422+
metadata_json
423+
)
424+
VALUES
425+
('event-auto-create', 'project', 'project-auto', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-auto-create', NULL, 'command-auto-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
426+
('event-title-create', 'project', 'project-title-only', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-title-create', NULL, 'command-title-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
427+
('event-title-update', 'project', 'project-title-only', 1, 'project.meta-updated', '2026-08-02T00:00:00.000Z', 'command-title-update', NULL, 'command-title-update', 'client', '{"title":"Renamed"}', '{}'),
428+
('event-explicit-create', 'project', 'project-explicit', 0, 'project.created', '2026-08-01T00:00:00.000Z', 'command-explicit-create', NULL, 'command-explicit-create', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol"}}', '{}'),
429+
('event-explicit-update', 'project', 'project-explicit', 1, 'project.meta-updated', '2026-08-02T00:00:00.000Z', 'command-explicit-update', NULL, 'command-explicit-update', 'client', '{"defaultModelSelection":{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}}', '{}')
430+
`;
431+
432+
yield* runMigrations({ toMigrationInclusive: 44 });
433+
434+
const projects = yield* sql<{
435+
readonly projectId: string;
436+
readonly selection: string | null;
437+
}>`
438+
SELECT
439+
project_id AS "projectId",
440+
default_model_selection_json AS "selection"
441+
FROM projection_projects
442+
ORDER BY project_id
443+
`;
444+
assert.deepStrictEqual(projects, [
445+
{ projectId: "project-auto", selection: null },
446+
{
447+
projectId: "project-explicit",
448+
selection:
449+
'{"instanceId":"codex","model":"gpt-5.6-sol","options":[{"id":"reasoningEffort","value":"high"}]}',
450+
},
451+
{ projectId: "project-title-only", selection: null },
452+
]);
453+
454+
const createdEvents = yield* sql<{
455+
readonly streamId: string;
456+
readonly model: string | null;
457+
}>`
458+
SELECT
459+
stream_id AS "streamId",
460+
json_extract(payload_json, '$.defaultModelSelection.model') AS "model"
461+
FROM orchestration_events
462+
WHERE event_type = 'project.created'
463+
ORDER BY stream_id
464+
`;
465+
assert.deepStrictEqual(createdEvents, [
466+
{ streamId: "project-auto", model: null },
467+
{ streamId: "project-explicit", model: "gpt-5.6-sol" },
468+
{ streamId: "project-title-only", model: null },
469+
]);
470+
}),
471+
);
472+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as Effect from "effect/Effect";
2+
import * as SqlClient from "effect/unstable/sql/SqlClient";
3+
4+
export default Effect.gen(function* () {
5+
const sql = yield* SqlClient.SqlClient;
6+
7+
// Project creation never exposed a model choice. A later metadata event
8+
// containing this field is the evidence that the user set or reset one.
9+
yield* sql`
10+
WITH automatically_seeded_projects AS (
11+
SELECT created.stream_id AS project_id
12+
FROM orchestration_events AS created
13+
WHERE created.aggregate_kind = 'project'
14+
AND created.event_type = 'project.created'
15+
AND json_type(created.payload_json, '$.defaultModelSelection') IS NOT NULL
16+
AND json_type(created.payload_json, '$.defaultModelSelection') <> 'null'
17+
AND NOT EXISTS (
18+
SELECT 1
19+
FROM orchestration_events AS configured
20+
WHERE configured.aggregate_kind = 'project'
21+
AND configured.stream_id = created.stream_id
22+
AND configured.event_type = 'project.meta-updated'
23+
AND json_type(configured.payload_json, '$.defaultModelSelection') IS NOT NULL
24+
)
25+
)
26+
UPDATE projection_projects
27+
SET default_model_selection_json = NULL
28+
WHERE project_id IN (SELECT project_id FROM automatically_seeded_projects)
29+
`;
30+
31+
yield* sql`
32+
UPDATE orchestration_events AS created
33+
SET payload_json = json_set(
34+
created.payload_json,
35+
'$.defaultModelSelection',
36+
json('null')
37+
)
38+
WHERE created.aggregate_kind = 'project'
39+
AND created.event_type = 'project.created'
40+
AND json_type(created.payload_json, '$.defaultModelSelection') IS NOT NULL
41+
AND json_type(created.payload_json, '$.defaultModelSelection') <> 'null'
42+
AND NOT EXISTS (
43+
SELECT 1
44+
FROM orchestration_events AS configured
45+
WHERE configured.aggregate_kind = 'project'
46+
AND configured.stream_id = created.stream_id
47+
AND configured.event_type = 'project.meta-updated'
48+
AND json_type(configured.payload_json, '$.defaultModelSelection') IS NOT NULL
49+
)
50+
`;
51+
});

‎apps/server/src/serverRuntimeStartup.test.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSna
1616
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
1717
import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
1818

19-
it("uses the canonical Codex default for auto-bootstrapped model selection", () => {
20-
assert.deepStrictEqual(ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(), {
19+
it("uses the canonical Codex default for the auto-bootstrapped welcome thread", () => {
20+
assert.deepStrictEqual(ServerRuntimeStartup.getAutoBootstrapThreadModelSelection(), {
2121
instanceId: ProviderInstanceId.make("codex"),
2222
model: DEFAULT_MODEL,
2323
});
@@ -158,7 +158,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa
158158
id: bootstrapProjectId,
159159
title: "Startup Project",
160160
workspaceRoot: "/tmp/startup-project",
161-
defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
161+
defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapThreadModelSelection(),
162162
scripts: [],
163163
createdAt: "2026-01-01T00:00:00.000Z",
164164
updatedAt: "2026-01-01T00:00:00.000Z",
@@ -197,7 +197,13 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa
197197

198198
it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
199199
Effect.gen(function* () {
200-
const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
200+
const dispatchCalls = yield* Ref.make<
201+
ReadonlyArray<{
202+
readonly type: string;
203+
readonly defaultModelSelection?: unknown;
204+
readonly modelSelection?: unknown;
205+
}>
206+
>([]);
201207
const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
202208
Effect.provideService(ServerConfig.ServerConfig, {
203209
cwd: "/tmp/startup-project",
@@ -224,7 +230,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when
224230
Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
225231
readEvents: () => Stream.empty,
226232
dispatch: (command) =>
227-
Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
233+
Ref.update(dispatchCalls, (calls) => [...calls, command]).pipe(
228234
Effect.as({ sequence: 1 }),
229235
),
230236
streamDomainEvents: Stream.empty,
@@ -236,7 +242,16 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when
236242

237243
assert.equal(typeof targets.bootstrapProjectId, "string");
238244
assert.equal(typeof targets.bootstrapThreadId, "string");
239-
assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
245+
const commands = yield* Ref.get(dispatchCalls);
246+
assert.deepStrictEqual(
247+
commands.map((command) => command.type),
248+
["project.create", "thread.create"],
249+
);
250+
assert.equal("defaultModelSelection" in commands[0]!, false);
251+
assert.deepStrictEqual(
252+
commands[1]?.modelSelection,
253+
ServerRuntimeStartup.getAutoBootstrapThreadModelSelection(),
254+
);
240255
}),
241256
);
242257

0 commit comments

Comments
 (0)