fix(custom-model): give a session its own env values back when the selection goes

Applying a custom model writes the endpoint's values over the keys it
injects, and clearing the model unset those keys. A session that had set
one of them itself lost that value: its own DSH_HOME, CODEX_HOME,
GROK_HOME or GEMINI_API_KEY was gone once the model was cleared, and so
was CLAUDE_CONFIG_DIR, which silently moved a Claude session onto the
default account. Switching endpoints did the same.

setCustomModel() now records what the session held under each injected
key and restores it on clear and on switch. Quick-start passes the
caller's values to the Session constructor so they are seen before the
selection lands.

The selection overwrites the only other copy, tmux's setenv, so the
session's own home directory has to survive a Codeman restart on its
own. __customModel therefore stores that one path, the single exception
to persisting keys only, and recovery runs it through
clampEnvOverridesForOwner() so a revoked grant cannot bring it back. Any
other own value, a credential included, stays in memory only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-05 14:25:49 +01:00
1 parent aa463d9d28
commit 10d43b993e
12 files changed
+335 -43

No files matched your search

@@ -92,6 +92,35 @@ describe('POST /api/quick-start: customModel (one-shot custom-model launch)', ()
safeRmHomeTree(CASES_DIR);
});
it("learns the caller's own value under an injected key, so a clear can put it back", async () => {
const own = join(getDataDir(), 'client-claude');
const res = await quickStart({
caseName: 'cm-own-home',
mode: 'claude',
envOverrides: { CLAUDE_CONFIG_DIR: own },
customModel: { endpointId: 'ep1', modelId: 'qwen3' },
});
expect(res.statusCode).toBe(200);
const { sessionId } = res.json();
const session = ctx.sessions.get(sessionId) as unknown as Session;
const isolatedDir = customModelConfigDir(sessionId);
// It launches on the isolated directory, with the caller's value set aside rather
// than recorded as the injected one.
expect(session.envOverrideValue('CLAUDE_CONFIG_DIR')).toBe(isolatedDir);
expect(session.getCustomModelForPersist()?.ownEnv).toEqual({ CLAUDE_CONFIG_DIR: own });
const clear = await app.inject({
method: 'POST',
url: `/api/sessions/${sessionId}/custom-model`,
payload: { clear: true },
});
expect(clear.statusCode).toBe(200);
expect(session.customModel).toBeUndefined();
expect(session.envOverrideValue('CLAUDE_CONFIG_DIR')).toBe(own);
expect(existsSync(isolatedDir)).toBe(false);
});
it('launches a claude session already pointed at the endpoint — no restart at all', async () => {
const res = await quickStart({
caseName: 'cm-claude',