Skip to content

Commit 25f141b

Browse files
committed
fix(gateway): retain catalog runtime during session creation
1 parent 7fd30e6 commit 25f141b

3 files changed

Lines changed: 133 additions & 71 deletions

File tree

‎src/gateway/server.sessions.create.test.ts‎

Lines changed: 129 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ import { resolveSqliteTargetFromSessionStorePath } from "../config/sessions/sess
3939
import { withTimeout } from "../infra/fs-safe.js";
4040
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
4141
import { setActivePluginRegistry } from "../plugins/runtime.js";
42+
import {
43+
createColdPluginFixture,
44+
isColdPluginRuntimeLoaded,
45+
} from "../plugins/test-helpers/cold-plugin-fixtures.js";
4246
import {
4347
beginSessionWorkAdmission,
4448
getSessionWorkAdmissionRelease,
@@ -4030,82 +4034,126 @@ test("sessions.create does not parent the main session to itself", async () => {
40304034
expect(created.payload?.entry?.parentSessionKey).toBeUndefined();
40314035
});
40324036

4033-
test("sessions.create resolves a catalog target server-side and pins its runtime", async () => {
4034-
const { storePath } = await createSessionStoreDir();
4035-
testState.agentConfig = { model: { primary: "anthropic/claude-opus-4-8" } };
4036-
agentDiscoveryMock.enabled = true;
4037-
agentDiscoveryMock.models = [
4038-
{ id: "claude-opus-4-8", name: "Claude Opus 4.8", provider: "anthropic" },
4039-
];
4040-
const resolveCreateSession = vi.fn(() => ({
4041-
model: "anthropic/claude-opus-4-8",
4042-
agentRuntime: "claude-cli",
4043-
}));
4044-
const registry = createEmptyPluginRegistry();
4045-
registry.sessionCatalogs.push({
4046-
pluginId: "anthropic",
4047-
source: "test",
4048-
provider: {
4049-
id: "claude",
4050-
label: "Claude Code",
4051-
resolveCreateSession,
4052-
list: vi.fn(async () => []),
4053-
read: vi.fn(async ({ hostId, threadId }) => ({ hostId, threadId, items: [] })),
4054-
},
4055-
});
4056-
setActivePluginRegistry(registry);
4037+
test.each(["cli", "enabled", "disabled"] as const)(
4038+
"sessions.create resolves a catalog target server-side with a %s harness",
4039+
async (harness) => {
4040+
const { dir, storePath } = await createSessionStoreDir();
4041+
testState.agentConfig = {
4042+
model: { primary: "anthropic/claude-opus-4-8" },
4043+
models: { "anthropic/claude-opus-4-8": { agentRuntime: { id: "missing-harness" } } },
4044+
};
4045+
agentDiscoveryMock.enabled = true;
4046+
agentDiscoveryMock.models = [
4047+
{ id: "claude-opus-4-8", name: "Claude Opus 4.8", provider: "anthropic" },
4048+
];
4049+
const agentRuntime = harness === "cli" ? "claude-cli" : "fixture-harness";
4050+
let fixture: ReturnType<typeof createColdPluginFixture> | undefined;
4051+
if (harness !== "cli") {
4052+
const rootDir = await fs.mkdtemp(path.join(dir, "catalog-harness-"));
4053+
fixture = createColdPluginFixture({
4054+
rootDir,
4055+
pluginId: "fixture-harness",
4056+
manifest: { activation: { onAgentHarnesses: ["fixture-harness"] } },
4057+
});
4058+
const { writeConfigFile } = await getGatewayConfigModule();
4059+
await writeConfigFile({
4060+
plugins: {
4061+
load: { paths: [rootDir] },
4062+
entries: { "fixture-harness": { enabled: harness === "enabled" } },
4063+
},
4064+
});
4065+
}
4066+
const resolveCreateSession = vi.fn(() => ({
4067+
model: "anthropic/claude-opus-4-8",
4068+
agentRuntime,
4069+
}));
4070+
const registry = createEmptyPluginRegistry();
4071+
if (harness === "cli") {
4072+
registry.cliBackends.push({
4073+
pluginId: "anthropic",
4074+
source: "test",
4075+
backend: {
4076+
id: "claude-cli",
4077+
modelProvider: "anthropic",
4078+
config: { command: "claude" },
4079+
bundleMcp: false,
4080+
},
4081+
});
4082+
}
4083+
registry.sessionCatalogs.push({
4084+
pluginId: "anthropic",
4085+
source: "test",
4086+
provider: {
4087+
id: "claude",
4088+
label: "Claude Code",
4089+
resolveCreateSession,
4090+
list: vi.fn(async () => []),
4091+
read: vi.fn(async ({ hostId, threadId }) => ({ hostId, threadId, items: [] })),
4092+
},
4093+
});
4094+
setActivePluginRegistry(registry);
40574095

4058-
try {
4059-
const created = await directSessionReq<{
4060-
entry?: {
4061-
providerOverride?: string;
4062-
modelOverride?: string;
4063-
agentRuntimeOverride?: string;
4064-
modelSelectionLocked?: boolean;
4065-
pluginOwnerId?: string;
4066-
};
4067-
key?: string;
4068-
}>("sessions.create", { agentId: "main", catalogId: "claude" });
4096+
try {
4097+
const created = await directSessionReq<{
4098+
entry?: {
4099+
providerOverride?: string;
4100+
modelOverride?: string;
4101+
agentRuntimeOverride?: string;
4102+
modelSelectionLocked?: boolean;
4103+
pluginOwnerId?: string;
4104+
};
4105+
key?: string;
4106+
}>("sessions.create", { agentId: "main", catalogId: "claude" });
40694107

4070-
expect(created.ok).toBe(true);
4071-
expect(created.payload?.entry).toMatchObject({
4072-
providerOverride: "anthropic",
4073-
modelOverride: "claude-opus-4-8",
4074-
agentRuntimeOverride: "claude-cli",
4075-
modelSelectionLocked: true,
4076-
pluginOwnerId: "anthropic",
4077-
});
4078-
expect(resolveCreateSession).toHaveBeenCalledWith({ agentId: "main" });
4108+
if (fixture) {
4109+
expect(isColdPluginRuntimeLoaded(fixture)).toBe(false);
4110+
}
4111+
if (harness === "disabled") {
4112+
expect(created.ok).toBe(false);
4113+
expect(created.error?.message).toContain('requires agent harness "fixture-harness"');
4114+
expect(created.payload).toBeUndefined();
4115+
return;
4116+
}
4117+
expect(created.ok, JSON.stringify(created.error)).toBe(true);
4118+
expect(created.payload?.entry).toMatchObject({
4119+
providerOverride: "anthropic",
4120+
modelOverride: "claude-opus-4-8",
4121+
agentRuntimeOverride: agentRuntime,
4122+
modelSelectionLocked: true,
4123+
pluginOwnerId: "anthropic",
4124+
});
4125+
expect(resolveCreateSession).toHaveBeenCalledWith({ agentId: "main" });
40794126

4080-
const patched = await directSessionReq("sessions.patch", {
4081-
key: created.payload?.key,
4082-
agentId: "main",
4083-
model: "anthropic/claude-opus-4-8",
4084-
});
4085-
expect(patched.ok).toBe(false);
4086-
expect(patched.error).toMatchObject({
4087-
code: "INVALID_REQUEST",
4088-
message: "Model selection is locked for this session.",
4089-
});
4127+
const patched = await directSessionReq("sessions.patch", {
4128+
key: created.payload?.key,
4129+
agentId: "main",
4130+
model: "anthropic/claude-opus-4-8",
4131+
});
4132+
expect(patched.ok).toBe(false);
4133+
expect(patched.error).toMatchObject({
4134+
code: "INVALID_REQUEST",
4135+
message: "Model selection is locked for this session.",
4136+
});
40904137

4091-
const deleted = await directSessionReq("sessions.delete", {
4092-
key: created.payload?.key,
4093-
agentId: "main",
4094-
deleteTranscript: false,
4095-
});
4096-
expect(deleted.ok).toBe(true);
4097-
expect(
4098-
loadSessionEntry({
4138+
const deleted = await directSessionReq("sessions.delete", {
4139+
key: created.payload?.key,
40994140
agentId: "main",
4100-
sessionKey: created.payload?.key ?? "",
4101-
storePath,
4102-
}),
4103-
).toBeUndefined();
4104-
} finally {
4105-
testState.agentConfig = undefined;
4106-
setActivePluginRegistry(createEmptyPluginRegistry());
4107-
}
4108-
});
4141+
deleteTranscript: false,
4142+
});
4143+
expect(deleted.ok).toBe(true);
4144+
expect(
4145+
loadSessionEntry({
4146+
agentId: "main",
4147+
sessionKey: created.payload?.key ?? "",
4148+
storePath,
4149+
}),
4150+
).toBeUndefined();
4151+
} finally {
4152+
testState.agentConfig = undefined;
4153+
setActivePluginRegistry(createEmptyPluginRegistry());
4154+
}
4155+
},
4156+
);
41094157

41104158
test("sessions.create rejects a caller-supplied key for a catalog target", async () => {
41114159
const { storePath } = await createSessionStoreDir();
@@ -4214,6 +4262,16 @@ test("sessions.create bypasses main-session reset for a catalog target", async (
42144262
},
42154263
});
42164264
const registry = createEmptyPluginRegistry();
4265+
registry.cliBackends.push({
4266+
pluginId: "anthropic",
4267+
source: "test",
4268+
backend: {
4269+
id: "claude-cli",
4270+
modelProvider: "anthropic",
4271+
config: { command: "claude" },
4272+
bundleMcp: false,
4273+
},
4274+
});
42174275
registry.sessionCatalogs.push({
42184276
pluginId: "anthropic",
42194277
source: "test",

‎src/gateway/session-create-service.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ export async function createGatewaySession(params: {
11091109
storeKey: target.canonicalKey,
11101110
agentId: target.agentId,
11111111
preparedSessionRoot: sessionRoot,
1112+
preparedAgentRuntime: catalogAgentRuntime,
11121113
// Patch appliers read key presence as caller intent (present = change,
11131114
// null = clear), so omitted create fields must stay absent: a present
11141115
// undefined model trips the selection lock and drops modelFallback,

‎src/gateway/sessions-patch.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export async function projectSessionsPatchEntry(params: {
131131
patch: SessionsPatchParams;
132132
/** Canonical root prepared by the trusted create path; never accepted from public patches. */
133133
preparedSessionRoot?: string;
134+
/** Trusted catalog runtime must own selection checks before the new row is persisted. */
135+
preparedAgentRuntime?: string;
134136
archivedBy?: SessionEntry["archivedBy"];
135137
loadGatewayModelCatalog?: () => Promise<ModelCatalogEntry[]>;
136138
providerAuthMetadataSnapshot?: Pick<PluginMetadataSnapshot, "plugins">;
@@ -185,6 +187,7 @@ export async function projectSessionsPatchEntry(params: {
185187
entry,
186188
});
187189
return (
190+
params.preparedAgentRuntime ??
188191
acpMeta?.backend ??
189192
resolveEffectiveAgentRuntime({
190193
cfg,

0 commit comments

Comments
 (0)