mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-02 16:59:32 +01:00
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>
189 lines
8.4 KiB
TypeScript
189 lines
8.4 KiB
TypeScript
/**
|
|
* @fileoverview Boot recovery re-derives a saved custom-model selection through
|
|
* `_rebuildCustomModelEnv` and rebuilds the BOOKKEEPING from what the CURRENT recipe
|
|
* applied, not from the saved record. A selection persisted while deepseek still used
|
|
* the old env redirect comes back env-shaped (envKeys `DEEPSEEK_BASE_URL`/
|
|
* `DEEPSEEK_API_KEY`, no `configDir`) although the recipe now writes a per-session patch
|
|
* home: restoring the saved record unchanged left `DSH_HOME` on a generated config dir
|
|
* that clearing could never delete.
|
|
*
|
|
* Real-home resolution takes no session lookup on this path: the first apply's links are
|
|
* still in the generated directory, and one someone removed links from the server's
|
|
* `DSH_HOME` or the recipe's `realHomeFallback` (~/.dsh), see `_rebuildCustomModelEnv`'s
|
|
* comment. A session's own home comes back on clear from `ownEnv`, the last block here.
|
|
*
|
|
* `_restoreCustomModel` reads no `this` state beyond the rebuild helper (it re-derives
|
|
* everything from the endpoint store and the Session it is handed), so it is driven off
|
|
* the prototype with a minimal receiver: `restoreMuxSessions()` itself cannot be
|
|
* reached under vitest, the same constraint recorded in test/session-model-recovery.test.ts.
|
|
*/
|
|
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 { getDataDir } from '../src/config/instance.js';
|
|
import { writeCustomModelHosts, type CustomModelHost } from '../src/custom-model-hosts.js';
|
|
import { customModelConfigDir, removeConfigDir } from '../src/custom-model-injection-apply.js';
|
|
import { Session } from '../src/session.js';
|
|
import { WebServer } from '../src/web/server.js';
|
|
import type { CustomModelBookkeeping } from '../src/types/session.js';
|
|
|
|
/** The real pair, on a receiver that only carries the rebuild helper, see the header. */
|
|
async function restoreCustomModel(session: Session, saved: CustomModelBookkeeping): Promise<void> {
|
|
const proto = WebServer.prototype as unknown as {
|
|
_rebuildCustomModelEnv: (session: Session, saved: CustomModelBookkeeping) => Promise<unknown>;
|
|
_restoreCustomModel: (session: Session, saved: CustomModelBookkeeping) => Promise<void>;
|
|
};
|
|
expect(typeof proto._restoreCustomModel, '_restoreCustomModel vanished from WebServer.prototype').toBe('function');
|
|
await proto._restoreCustomModel.call({ _rebuildCustomModelEnv: proto._rebuildCustomModelEnv }, session, saved);
|
|
}
|
|
|
|
function endpoint(): CustomModelHost {
|
|
return {
|
|
id: 'ep',
|
|
label: 'Ep',
|
|
baseUrl: 'http://localhost:8080',
|
|
apiKey: 'recovery-probe-key',
|
|
models: ['qwen3'],
|
|
};
|
|
}
|
|
|
|
describe('_restoreCustomModel: bookkeeping rebuilt from the current recipe', () => {
|
|
const configs: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const id of configs.splice(0)) removeConfigDir(customModelConfigDir(id));
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it('carries the configDir and env-key union for a selection saved by the old env recipe', async () => {
|
|
// Saved exactly as 1.31.0-1.47.0 persisted a deepseek selection: the env recipe's two
|
|
// keys, no configDir. The rebuilt record must carry the per-session config dir the
|
|
// CURRENT recipe wrote (so clearing deletes it) while KEEPING the saved keys, which
|
|
// the tmux session may still carry `setenv`s for (the union is what a clear unsets).
|
|
const dshHome = join(homedir(), 'codeman-recovery-dsh-home');
|
|
mkdirSync(join(dshHome, 'profiles'), { recursive: true });
|
|
vi.stubEnv('DSH_HOME', dshHome);
|
|
await writeCustomModelHosts(getDataDir(), [endpoint()]);
|
|
const session = new Session({ workingDir: '/tmp', mode: 'deepseek' });
|
|
configs.push(session.id);
|
|
|
|
await restoreCustomModel(session, {
|
|
endpointId: 'ep',
|
|
modelId: 'qwen3',
|
|
label: 'Ep',
|
|
envKeys: ['DEEPSEEK_BASE_URL', 'DEEPSEEK_API_KEY'],
|
|
});
|
|
|
|
const persisted = session.getCustomModelForPersist();
|
|
expect(persisted?.configDir).toBe(customModelConfigDir(session.id));
|
|
expect(persisted?.launchModel).toBeUndefined();
|
|
expect(new Set(persisted?.envKeys)).toEqual(
|
|
new Set(['DEEPSEEK_BASE_URL', 'DEEPSEEK_API_KEY', 'DSH_HOME', 'CODEMAN_CUSTOM_MODEL_API_KEY'])
|
|
);
|
|
rmSync(dshHome, { recursive: true, force: true });
|
|
});
|
|
|
|
it('restores an env-shaped record unchanged when the endpoint is gone, so a clear still works', async () => {
|
|
await writeCustomModelHosts(getDataDir(), []);
|
|
const session = new Session({ workingDir: '/tmp', mode: 'deepseek' });
|
|
const saved: CustomModelBookkeeping = {
|
|
endpointId: 'gone',
|
|
modelId: 'qwen3',
|
|
label: 'Gone',
|
|
envKeys: ['DEEPSEEK_BASE_URL', 'DEEPSEEK_API_KEY'],
|
|
};
|
|
|
|
await restoreCustomModel(session, saved);
|
|
|
|
expect(session.getCustomModelForPersist()).toEqual(saved);
|
|
});
|
|
|
|
describe("a session's own value across a restart", () => {
|
|
const OWN_HOME = '/home/u/my-dsh';
|
|
const ORIGINAL_MULTIUSER = process.env.CODEMAN_MULTIUSER;
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_MULTIUSER === undefined) delete process.env.CODEMAN_MULTIUSER;
|
|
else process.env.CODEMAN_MULTIUSER = ORIGINAL_MULTIUSER;
|
|
});
|
|
|
|
/** What a previous run left in state.json for a deepseek session on its own home. */
|
|
function savedByPreviousRun(): CustomModelBookkeeping {
|
|
const previous = new Session({ workingDir: '/tmp', mode: 'deepseek', envOverrides: { DSH_HOME: OWN_HOME } });
|
|
configs.push(previous.id);
|
|
previous.setCustomModel(
|
|
{
|
|
endpointId: 'ep',
|
|
modelId: 'qwen3',
|
|
envKeys: ['DSH_HOME', 'CODEMAN_CUSTOM_MODEL_API_KEY'],
|
|
configDir: customModelConfigDir(previous.id),
|
|
homeEnvVar: 'DSH_HOME',
|
|
},
|
|
{ DSH_HOME: customModelConfigDir(previous.id), CODEMAN_CUSTOM_MODEL_API_KEY: 'k' }
|
|
);
|
|
return JSON.parse(JSON.stringify(previous.getCustomModelForPersist())) as CustomModelBookkeeping;
|
|
}
|
|
|
|
it('gets DSH_HOME back from state.json, which is the only copy left', async () => {
|
|
await writeCustomModelHosts(getDataDir(), [endpoint()]);
|
|
const session = new Session({ workingDir: '/tmp', mode: 'deepseek' });
|
|
configs.push(session.id);
|
|
|
|
await restoreCustomModel(session, savedByPreviousRun());
|
|
expect(session.envOverrideValue('DSH_HOME')).toBe(customModelConfigDir(session.id));
|
|
|
|
session.setCustomModel(undefined);
|
|
expect(session.envOverrideValue('DSH_HOME')).toBe(OWN_HOME);
|
|
});
|
|
|
|
it('keeps it when the endpoint is gone', async () => {
|
|
await writeCustomModelHosts(getDataDir(), []);
|
|
const session = new Session({ workingDir: '/tmp', mode: 'deepseek' });
|
|
|
|
await restoreCustomModel(session, savedByPreviousRun());
|
|
|
|
session.setCustomModel(undefined);
|
|
expect(session.envOverrideValue('DSH_HOME')).toBe(OWN_HOME);
|
|
});
|
|
|
|
it('never reads what the previous run injected as the session own value', async () => {
|
|
// A record from before `ownEnv` existed. `__envOverrides` holds the isolated
|
|
// CLAUDE_CONFIG_DIR and the injected limit, which must go on clear, or the session
|
|
// stays pinned to a directory the clear just deleted.
|
|
await writeCustomModelHosts(getDataDir(), [endpoint()]);
|
|
const session = new Session({
|
|
workingDir: '/tmp',
|
|
mode: 'claude',
|
|
envOverrides: { CLAUDE_CONFIG_DIR: '/isolated/old', CLAUDE_CODE_MAX_CONTEXT_TOKENS: '32768' },
|
|
});
|
|
configs.push(session.id);
|
|
|
|
await restoreCustomModel(session, {
|
|
endpointId: 'ep',
|
|
modelId: 'qwen3',
|
|
envKeys: ['CLAUDE_CONFIG_DIR', 'CLAUDE_CODE_MAX_CONTEXT_TOKENS'],
|
|
});
|
|
|
|
session.setCustomModel(undefined);
|
|
expect(session.envOverrideValue('CLAUDE_CONFIG_DIR')).toBeUndefined();
|
|
expect(session.envOverrideValue('CLAUDE_CODE_MAX_CONTEXT_TOKENS')).toBeUndefined();
|
|
});
|
|
|
|
it('does not give a revoked owner their DSH_HOME back from disk', async () => {
|
|
// DSH_HOME is a privileged key: the reboot-restore clamp strips `__envOverrides`, and
|
|
// this record must not be a way around it.
|
|
await writeCustomModelHosts(getDataDir(), [endpoint()]);
|
|
const saved = savedByPreviousRun();
|
|
process.env.CODEMAN_MULTIUSER = '1';
|
|
const session = new Session({ workingDir: '/tmp', mode: 'deepseek', owner: 'nobody' });
|
|
configs.push(session.id);
|
|
|
|
await restoreCustomModel(session, saved);
|
|
|
|
session.setCustomModel(undefined);
|
|
expect(session.envOverrideValue('DSH_HOME')).toBeUndefined();
|
|
});
|
|
});
|
|
});
|