Files
Codeman/test/session-custom-model-restart.test.ts
T
Codeman maintainer 942bf37e48 fix(custom-model): unset injected env on clear, resume on restart, select the model for pi/omp/grok
Custom Model Endpoint Profiles (#393) let a session point its CLI at a
custom OpenAI-compatible endpoint by injecting env vars or a config file
and restarting the CLI in place. Review of the apply path found four
things, two of them destructive. This lands all four plus the smaller
items from the same review.

1. Clearing a selection did not clear it. The injected vars reach the CLI
   via `tmux setenv`, which persists at the tmux-session level and is
   inherited by `respawn-pane` (measured: `setenv FOO bar` survived two
   successive `respawn-pane -k`), so deleting the keys from the session's
   envOverrides relaunched the CLI still pointed at the old endpoint, and
   for the configDir kinds at a HOME/CODEX_HOME/GROK_HOME that had just
   been deleted. `Session.setCustomModel()` now reports the removed keys,
   queues them (`_pendingEnvUnsets`), and `RespawnPaneOptions.unsetEnvKeys`
   carries them into `applyEnvOverrides()`, which `setenv -u`s them before
   re-applying the live overrides, on the same path that already unsets
   the legacy CLAUDE_CODE_EFFORT_LEVEL. Verified on a private tmux socket
   that `setenv -u HOME` hands the next respawn the global HOME back.

2. Applying a model to a local claude session killed the pane. The
   relaunch was `claude --session-id <id>` and Claude refuses an id that
   already has a transcript, and unlike the dead-pane respawn this one
   kills a working pane first. `restartCli()` now pins the live
   conversation id as the resume id for that respawn when the CLI's launch
   declares a `fallback` chain, which renders the same
   `--resume <id> || --session-id <id>` shape the docker and remote pane
   commands use. Gated on the registry shape, not the CLI id: an entry
   whose resume id is minted by the CLI itself never declares that chain.

3. pi, omp and grok wrote their config file and then launched without the
   `--model` that selects it, so the file was ignored. The registry entry
   now declares `customModelInjection.launchModel` (`custom/{modelId}` for
   pi and omp, grok's `[model.codeman-custom]` block name), the builder
   renders it, and `_withCustomModelLaunchModel()` applies it onto the
   respawn options through `legacyConfigField`, leaving the stored
   <Mode>Config untouched so a clear falls back to the user's own model.
   A model id the CLI's `model` token pattern cannot carry is refused
   with a 400 rather than silently dropped by the argv engine.

4. Remote (SSH) and Docker sessions reported `restarted: true` and changed
   nothing: their `restartCli()` reattaches the durable tmux rather than
   relaunching the agent, and the env lands on the local pane. Both are
   refused with a 400 until those paths are plumbed.

Smaller items from the same review:

- The selection survives a Codeman restart as the disk-only `__customModel`
  bookkeeping (endpoint, model, injected key NAMES, config dir, launch
  model; never the values, which carry the API key). Recovery re-derives
  the values from the endpoint store through the same apply path the route
  uses and keeps the bookkeeping even when the endpoint is gone, so a
  later clear still has keys to unset.
- Discovery goes through `webviewFetch()`, so the RESOLVED address is
  judged by the same egress guard the web-tab proxy uses, and `baseUrl`
  reuses `webviewUrlSchema` (http(s) only, no embedded credentials,
  link-local and cloud-metadata addresses refused). undici's `fetch failed`
  wrapper is unwrapped so the user sees the ECONNREFUSED underneath.
- `custom-model-hosts.json` is written 0600 via tmp+rename, the per-session
  config dir 0700/0600 (pi and omp embed the key literally), and that dir
  is removed with the session.
- `PR.md` is gone from the repo root and the design doc moved to
  `docs/custom-model-endpoints-plan.md` with the LAN address and the
  personal name scrubbed; every reference follows. The guide's `authStyle`
  text matches the shipped schema (`bearer | api-key`, default `bearer`)
  and says that `customModelEndpointsEnabled` is read by nothing until
  the picker lands.
- `config/tsconfig.scripts.json` typechecks `scripts/test-local-llm-harnesses.ts`
  (four real type errors fixed). It is not yet wired into `npm run typecheck`
  because that line differs on master; adding `&& tsc -p config/tsconfig.scripts.json`
  there is the one-line follow-up.

Tests: `test/session-custom-model-restart.test.ts` drives a real Session and
fails on the unfixed code for items 1 to 3; the route suite covers item 4
and the pattern refusal; `test/tmux-manager.test.ts` pins that the unsets
run before the overrides and that a shell-metachar key never reaches tmux.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-14 23:46:28 +02:00

184 lines
8.0 KiB
TypeScript

/**
* @fileoverview Custom Model Endpoint Profiles, the Session half of applying and
* clearing a selection against a live pane (`Session.setCustomModel()` +
* `Session.restartCli()`), pinned against the three ways the first cut broke a
* working session:
*
* 1. Clearing did not clear. The injected vars reach the CLI via `tmux setenv`,
* which persists at the tmux-session level and is inherited by `respawn-pane`,
* so removing them from `_envOverrides` relaunched the CLI still pointed at the
* endpoint (and, for the configDir kinds, at a `HOME`/`CODEX_HOME` that had just
* been deleted). The retired keys must ride `RespawnPaneOptions.unsetEnvKeys`.
* 2. Applying to a local claude session killed the pane: the relaunch was
* `claude --session-id <id>` and Claude refuses an id that already has a
* transcript, so it needs the `--resume <id> || --session-id <id>` shape the
* docker and remote pane commands use, i.e. a pinned resume id.
* 3. pi/omp/grok wrote their config file and then launched without the `--model`
* that selects it, so the file was ignored.
*
* Drives a real `Session` against the in-memory tmux layer vitest substitutes,
* spying on `respawnPane` to read the options the relaunch would get.
* Port: N/A.
*/
import { mkdirSync, rmSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { Session } from '../src/session.js';
import { TmuxManager } from '../src/tmux-manager.js';
import type { MuxSession, SessionMode } from '../src/types.js';
const workingDir = join(homedir(), 'codeman-cases', 'custom-model-restart');
const sessions: Session[] = [];
afterEach(() => {
for (const s of sessions.splice(0)) s.stop();
rmSync(workingDir, { recursive: true, force: true });
});
function liveSession(mode: SessionMode, extra: Record<string, unknown> = {}) {
mkdirSync(workingDir, { recursive: true });
const mux = new TmuxManager();
const muxSession: MuxSession = {
sessionId: 'placeholder',
muxName: 'codeman-cafe0001',
pid: 1,
createdAt: Date.now(),
workingDir,
mode,
attached: false,
};
const session = new Session({ workingDir, mode, mux, useMux: true, muxSession, ...extra });
sessions.push(session);
vi.spyOn(mux, 'muxSessionExists').mockReturnValue(true);
const respawn = vi.spyOn(mux, 'respawnPane').mockResolvedValue(4242);
return { session, respawn };
}
const CLAUDE_KEYS = ['ANTHROPIC_BASE_URL', 'ANTHROPIC_API_KEY', 'ANTHROPIC_DEFAULT_SONNET_MODEL'];
const claudeEnv = {
ANTHROPIC_BASE_URL: 'http://box:8080',
ANTHROPIC_API_KEY: 'k',
ANTHROPIC_DEFAULT_SONNET_MODEL: 'q',
};
describe('clearing a selection unsets what it injected', () => {
it('queues the retired keys for setenv -u on the next respawn and drops them from the overrides', async () => {
const { session, respawn } = liveSession('claude', { envOverrides: { CLAUDE_CODE_KEEP: '1' } });
session.setCustomModel({ endpointId: 'ep', modelId: 'q', envKeys: CLAUDE_KEYS }, claudeEnv);
const result = session.setCustomModel(undefined);
expect(result.removedEnvKeys).toEqual(CLAUDE_KEYS);
expect(session.customModel).toBeUndefined();
expect(await session.restartCli()).toBe(true);
const options = respawn.mock.calls[0][0];
expect(options.unsetEnvKeys).toEqual(CLAUDE_KEYS);
expect(options.envOverrides).toEqual({ CLAUDE_CODE_KEEP: '1' });
// Drained once the respawn succeeded: the next relaunch has nothing to unset.
respawn.mockClear();
await session.restartCli();
expect(respawn.mock.calls[0][0].unsetEnvKeys).toBeUndefined();
});
it('switching endpoints re-sets the shared keys instead of unsetting them', async () => {
const { session, respawn } = liveSession('claude');
session.setCustomModel({ endpointId: 'a', modelId: 'q', envKeys: CLAUDE_KEYS }, claudeEnv);
const next = { ...claudeEnv, ANTHROPIC_BASE_URL: 'http://other:8080' };
session.setCustomModel({ endpointId: 'b', modelId: 'q', envKeys: CLAUDE_KEYS }, next);
await session.restartCli();
const options = respawn.mock.calls[0][0];
expect(options.unsetEnvKeys).toBeUndefined();
expect(options.envOverrides?.ANTHROPIC_BASE_URL).toBe('http://other:8080');
});
it('reports the previous config dir so the caller can delete it, and never leaks bookkeeping on the wire', () => {
const { session } = liveSession('pi');
session.setCustomModel(
{ endpointId: 'ep', modelId: 'q', envKeys: ['HOME'], configDir: '/tmp/cfg-1', launchModel: 'custom/q' },
{ HOME: '/tmp/cfg-1' }
);
expect(session.customModel).toEqual({ endpointId: 'ep', modelId: 'q', label: undefined });
expect(session.toState().customModel).toEqual({ endpointId: 'ep', modelId: 'q', label: undefined });
expect(session.getCustomModelForPersist()).toEqual({
endpointId: 'ep',
modelId: 'q',
label: undefined,
envKeys: ['HOME'],
configDir: '/tmp/cfg-1',
launchModel: 'custom/q',
});
expect(session.setCustomModel(undefined).previousConfigDir).toBe('/tmp/cfg-1');
});
});
describe('restartCli() must not kill a working pane', () => {
it('claude: pins the live conversation id so the relaunch renders --resume <id> || --session-id <id>', async () => {
const { session, respawn } = liveSession('claude');
await session.restartCli();
const options = respawn.mock.calls[0][0];
expect(options.resumeSessionId).toBe(session.claudeSessionId);
expect(options.resumeSessionId).toBe(session.id);
// The pin is per-respawn: nothing about the session's own resume id changed.
expect(session.toState().resumeSessionId).toBeUndefined();
});
it('claude: an explicit resume id from a resume-from-history launch wins over the pin', async () => {
const RESUMED = '01a060f0-0361-7f91-abde-b283020db0d7';
const { session, respawn } = liveSession('claude', { resumeSessionId: RESUMED });
await session.restartCli();
expect(respawn.mock.calls[0][0].resumeSessionId).toBe(RESUMED);
});
it('pi: no top-level resume pin, its resume id is minted by the CLI and lives in piConfig', async () => {
const { session, respawn } = liveSession('pi');
await session.restartCli();
expect(respawn.mock.calls[0][0].resumeSessionId).toBeUndefined();
});
});
describe('launchModel reaches the CLI through its own launch param', () => {
it('pi: the selection forces piConfig.model on the respawn options, leaving the stored config alone', async () => {
const { session, respawn } = liveSession('pi', { piConfig: { model: 'anthropic/claude-x', thinking: 'low' } });
session.setCustomModel(
{ endpointId: 'ep', modelId: 'qwen3', envKeys: ['HOME'], configDir: '/tmp/cfg', launchModel: 'custom/qwen3' },
{ HOME: '/tmp/cfg' }
);
await session.restartCli();
expect(respawn.mock.calls[0][0].piConfig).toEqual({ model: 'custom/qwen3', thinking: 'low' });
// The user's own choice survives underneath, which is what a clear falls back to.
expect(session.toState().piConfig).toEqual({ model: 'anthropic/claude-x', thinking: 'low' });
session.setCustomModel(undefined);
respawn.mockClear();
await session.restartCli();
expect(respawn.mock.calls[0][0].piConfig).toEqual({ model: 'anthropic/claude-x', thinking: 'low' });
});
it('grok: the block name lands in grokConfig.model', async () => {
const { session, respawn } = liveSession('grok');
session.setCustomModel(
{
endpointId: 'ep',
modelId: 'qwen3',
envKeys: ['GROK_HOME'],
configDir: '/tmp/cfg',
launchModel: 'codeman-custom',
},
{ GROK_HOME: '/tmp/cfg' }
);
await session.restartCli();
expect(respawn.mock.calls[0][0].grokConfig).toEqual({ model: 'codeman-custom' });
});
it('claude: a selection without a launchModel leaves the model param untouched', async () => {
const { session, respawn } = liveSession('claude');
session.setCustomModel({ endpointId: 'ep', modelId: 'q', envKeys: CLAUDE_KEYS }, claudeEnv);
await session.restartCli();
expect(respawn.mock.calls[0][0].model).toBeUndefined();
});
});