Skip to content

Commit bf1592e

Browse files
committed
refactor(server): tighten fork model selection and its tests
`resolveForkSourceSelection` only matches candidates whose instance id equals the source's, and the caller already proved the thread's own selection differs, so passing it as a candidate was dead. The comment explaining how an unstarted fork picks its instance now sits directly above the code it describes. Documents what each `sessionFork` mode accepts, including that omitting the field means unsupported. Test fixes: the fork decider cases silently passed when a command returned the wrong event type, the projection case checked only that fork lineage was non-null after promotion rather than what it held, the normalizer case asserted an absent failure instead of the selection it resolves, and the title case asserted against invented strings rather than titles `forkThreadTitle` actually produces.
1 parent 8a80331 commit bf1592e

6 files changed

Lines changed: 51 additions & 15 deletions

File tree

‎apps/server/src/orchestration/Layers/ProjectionPipeline.fork.test.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ layer("OrchestrationProjectionPipeline fork projection", (it) => {
7474
});
7575
yield* projectionPipeline.bootstrap;
7676

77-
let rows = yield* sql<{ readonly forkJson: string | null; readonly sideChat: number }>`
77+
const rows = yield* sql<{ readonly forkJson: string | null; readonly sideChat: number }>`
7878
SELECT fork_json AS "forkJson", side_chat AS "sideChat"
7979
FROM projection_threads
8080
WHERE thread_id = ${threadId}
@@ -100,13 +100,32 @@ layer("OrchestrationProjectionPipeline fork projection", (it) => {
100100
payload: { threadId, sideChat: false, updatedAt: now },
101101
});
102102
yield* projectionPipeline.bootstrap;
103-
rows = yield* sql<{ readonly forkJson: string | null; readonly sideChat: number }>`
104-
SELECT fork_json AS "forkJson", side_chat AS "sideChat"
103+
// Promotion clears the side-chat flag but must leave the lineage intact.
104+
const promotedRows = yield* sql<{
105+
readonly sourceThreadId: string | null;
106+
readonly sourceTurnId: string | null;
107+
readonly sourceMessageId: string | null;
108+
readonly forkedAt: string | null;
109+
readonly sideChat: number;
110+
}>`
111+
SELECT
112+
json_extract(fork_json, '$.sourceThreadId') AS "sourceThreadId",
113+
json_extract(fork_json, '$.sourceTurnId') AS "sourceTurnId",
114+
json_extract(fork_json, '$.sourceMessageId') AS "sourceMessageId",
115+
json_extract(fork_json, '$.forkedAt') AS "forkedAt",
116+
side_chat AS "sideChat"
105117
FROM projection_threads
106118
WHERE thread_id = ${threadId}
107119
`;
108-
assert.equal(rows[0]?.sideChat, 0);
109-
assert.isNotNull(rows[0]?.forkJson ?? null);
120+
assert.deepEqual(promotedRows, [
121+
{
122+
sourceThreadId: "thread-source-projection",
123+
sourceTurnId: "turn-source-projection",
124+
sourceMessageId: null,
125+
forkedAt: now,
126+
sideChat: 0,
127+
},
128+
]);
110129
}),
111130
);
112131
});

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,16 +622,16 @@ const make = Effect.gen(function* () {
622622
thread.fork != null && !forkHasOwnResumeCursor
623623
? yield* resolveThreadShell(thread.fork.sourceThreadId)
624624
: undefined;
625+
const resolveActiveSession = (threadId: ThreadId) =>
626+
providerService
627+
.listSessions()
628+
.pipe(Effect.map((sessions) => sessions.find((session) => session.threadId === threadId)));
625629
// An unstarted fork must start where the source conversation actually
626630
// lives. The source's live session is the only proof it moved; without
627631
// one, the source's persisted binding says which instance still holds
628632
// it, and a source with neither stays on the selection the fork
629633
// inherited. The source's stored selection alone is not enough: it can
630634
// change before any turn moves the conversation.
631-
const resolveActiveSession = (threadId: ThreadId) =>
632-
providerService
633-
.listSessions()
634-
.pipe(Effect.map((sessions) => sessions.find((session) => session.threadId === threadId)));
635635
const forkSourceSession =
636636
forkSource?.session != null &&
637637
forkSource.session.status !== "stopped" &&
@@ -692,7 +692,7 @@ const make = Effect.gen(function* () {
692692
forkSourceRuntimeSession.providerInstanceId === forkSourceInstanceId
693693
? forkSourceRuntimeSession.model
694694
: undefined,
695-
candidates: [forkSource.modelSelection, thread.modelSelection],
695+
candidates: [forkSource.modelSelection],
696696
provider: (yield* providerRegistry.getProviders).find(
697697
(provider) => provider.instanceId === forkSourceInstanceId,
698698
),

‎apps/server/src/orchestration/Normalizer.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,21 @@ describe("normalizeDispatchCommand thread.fork", () => {
265265
Effect.gen(function* () {
266266
// The stored selection points at a latest-turn instance, but the live
267267
// session moved to an any-turn one; the live instance decides.
268-
const error = yield* normalizeFork(
268+
const normalized = yield* normalizeFork(
269269
Option.some({ state: "completed", assistantMessageId: sourceMessageId }),
270270
{
271271
sessionFork: "latest-turn",
272272
latestTurn: makeLatestTurn(latestSourceTurnId),
273273
liveSessionSessionFork: "any-turn",
274274
},
275-
).pipe(Effect.flip, Effect.option);
276-
expect(Option.isNone(error)).toBe(true);
275+
);
276+
277+
expect(normalized.type).toBe("thread.fork");
278+
if (normalized.type !== "thread.fork") return;
279+
expect(normalized.modelSelection).toEqual({
280+
instanceId: liveSourceInstanceId,
281+
model: "claude-opus-4-6",
282+
});
277283
}),
278284
);
279285

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ it.layer(NodeServices.layer)("thread fork decider", (it) => {
180180
readModel: makeReadModel({ hasLatestTurn: false }),
181181
});
182182
const noTurnsCreated = Array.isArray(noTurns) ? noTurns[0] : noTurns;
183+
expect(noTurnsCreated?.type).toBe("thread.created");
183184
if (noTurnsCreated?.type !== "thread.created") return;
184185
expect(noTurnsCreated.payload.fork?.sourceTurnId).toBeNull();
185186

@@ -189,6 +190,7 @@ it.layer(NodeServices.layer)("thread fork decider", (it) => {
189190
readModel: makeReadModel(),
190191
});
191192
const olderCreated = Array.isArray(older) ? older[0] : older;
193+
expect(olderCreated?.type).toBe("thread.created");
192194
if (olderCreated?.type !== "thread.created") return;
193195
expect(olderCreated.payload.fork?.sourceTurnId).toBe(olderTurnId);
194196
}),

‎apps/server/src/orchestration/threadTitles.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { canReplaceThreadTitle, forkThreadTitle, DEFAULT_THREAD_TITLE } from "./
44

55
it("only replaces known auto-generated thread titles", () => {
66
expect(canReplaceThreadTitle(DEFAULT_THREAD_TITLE)).toBe(true);
7-
expect(canReplaceThreadTitle("Fork: Parent thread")).toBe(false);
8-
expect(canReplaceThreadTitle("Side chat: Parent thread")).toBe(false);
7+
// A generated fork title carries the source's real title, so it is never
8+
// overwritten by a provider-generated one.
9+
expect(
10+
canReplaceThreadTitle(forkThreadTitle("Fix the parser", [], { sourceIsFork: false })),
11+
).toBe(false);
912
expect(canReplaceThreadTitle("A deliberate title")).toBe(false);
1013
});
1114

‎apps/server/src/provider/Services/ProviderAdapter.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export interface ProviderAdapterCapabilities {
5757
* Declares whether changing the model on an existing session is supported.
5858
*/
5959
readonly sessionModelSwitch: ProviderSessionModelSwitchMode;
60+
/**
61+
* Which source turns a native session fork can branch from. `any-turn`
62+
* accepts any completed turn in the source conversation, `latest-turn` only
63+
* accepts the source's current completed head, and `unsupported` rejects
64+
* forks outright. Omitted means unsupported.
65+
*/
6066
readonly sessionFork?: ProviderSessionForkMode;
6167
/** Starts a resumed turn with no synthetic user prompt. Omitted means the
6268
adapter needs an explicit continuation instruction. */

0 commit comments

Comments
 (0)