fix(custom-model): keep a session's own config dir conversation across an endpoint

A claude session on its own CLAUDE_CONFIG_DIR launched on an endpoint with
<isolated>/projects linked to the server's tree. The resume pin looks there, so
apply found no transcript and started a new conversation, and on clear the
endpoint's transcript stayed behind in the server's tree.

The link now goes to <own dir>/projects when the session set one, so the
conversation and what the endpoint run adds stay on the session's own account
across an apply, a clear and a restart. It goes through linkRealHomeEntry(),
which also creates a missing projects folder instead of leaving a dangling
link, in the own dir and in ~/.claude/projects on the default path. A relative
CLAUDE_CONFIG_DIR of the server gets no link, since joining it would create
projects under the server's working directory.

Rule #516: guardSessionConfigDirHome() now covers configDirVar, so all three
apply sites (route, quick-start, boot recovery) probe the own dir bounded before
the apply touches it, and a dir that does not answer falls back to the server's
tree. Under test, HOME is a temp dir and CLAUDE_CONFIG_DIR is stripped by
test/setup.ts, which a new test pins.

Measured on a real Codeman server with a fake claude: apply and re-apply resume
under the isolated dir with the transcript landing in the own dir, a clear
resumes on the own dir, the isolated dir goes while the transcript stays, and the
same holds across a server restart. The documented limits come out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-06 06:10:41 +01:00
1 parent 5559aa8294
commit 9b8f853c94
10 files changed
+213 -49

No files matched your search

+79 -3
View File
@@ -13,10 +13,20 @@
*
* Port: N/A (no server; filesystem-only, under a temp CODEMAN data dir from test/setup.ts).
*/
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import {
existsSync,
lstatSync,
mkdirSync,
readFileSync,
readdirSync,
readlinkSync,
rmSync,
statSync,
writeFileSync,
} from 'node:fs';
import { homedir, tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getCli } from '../src/config/cli-registry/index.js';
import { applyCustomModelInjection, customModelConfigDir } from '../src/custom-model-injection-apply.js';
import type { CustomModelEndpoint } from '../src/custom-model-injection.js';
@@ -90,6 +100,72 @@ describe('applyCustomModelInjection: CLAUDE_CONFIG_DIR isolation', () => {
}
});
it('claude: creates the missing projects folder under the temp HOME, never a real one', () => {
// test/setup.ts points HOME at a temp dir and drops CLAUDE_CONFIG_DIR. Without both, the
// apply below would create `projects` in the real `~/.claude` of whoever runs the suite.
expect(homedir().startsWith(tmpdir())).toBe(true);
expect(process.env.CLAUDE_CONFIG_DIR).toBeUndefined();
const sessionId = 'sess-cfgdir-default';
sessionsToClean.push(sessionId);
const realProjects = join(homedir(), '.claude', 'projects');
rmSync(realProjects, { recursive: true, force: true });
const applied = applyCustomModelInjection(entryOrThrow('claude'), endpoint, 'qwen3', sessionId);
expect(readlinkSync(join(applied!.configDir!, 'projects'))).toBe(realProjects);
expect(statSync(realProjects).isDirectory()).toBe(true);
});
it('claude: a relative CLAUDE_CONFIG_DIR of the server gets no link, nothing lands under its cwd', () => {
const sessionId = 'sess-cfgdir-relative';
sessionsToClean.push(sessionId);
vi.stubEnv('CLAUDE_CONFIG_DIR', 'relative-server-claude');
try {
const applied = applyCustomModelInjection(entryOrThrow('claude'), endpoint, 'qwen3', sessionId);
expect(existsSync(join(applied!.configDir!, 'projects'))).toBe(false);
expect(existsSync('relative-server-claude')).toBe(false);
} finally {
vi.unstubAllEnvs();
}
});
it("claude: links projects from the session's own CLAUDE_CONFIG_DIR, creating it for a new account", () => {
const sessionId = 'sess-cfgdir-own';
sessionsToClean.push(sessionId);
const ownDir = join(homedir(), 'client-account');
const applied = applyCustomModelInjection(
entryOrThrow('claude'),
endpoint,
'qwen3',
sessionId,
undefined,
undefined,
(envVar) => (envVar === 'CLAUDE_CONFIG_DIR' ? ownDir : undefined)
);
expect(readlinkSync(join(applied!.configDir!, 'projects'))).toBe(join(ownDir, 'projects'));
// A dangling directory link is not inert: the CLI's mkdir through it fails ENOENT.
expect(statSync(join(ownDir, 'projects')).isDirectory()).toBe(true);
rmSync(ownDir, { recursive: true, force: true });
});
it('claude: links projects from the real config dir when the own value is relative or the isolated dir', () => {
for (const own of ['relative/claude', customModelConfigDir('sess-cfgdir-self')]) {
sessionsToClean.push('sess-cfgdir-self');
const applied = applyCustomModelInjection(
entryOrThrow('claude'),
endpoint,
'qwen3',
'sess-cfgdir-self',
undefined,
undefined,
() => own
);
expect(readlinkSync(join(applied!.configDir!, 'projects'))).toBe(join(homedir(), '.claude', 'projects'));
rmSync(applied!.configDir!, { recursive: true, force: true });
}
});
it('claude: re-applying to the same session is idempotent (boot-recovery re-apply)', () => {
const sessionId = 'sess-cfgdir-3';
sessionsToClean.push(sessionId);
@@ -58,6 +58,16 @@ describe('guardSessionConfigDirHome', () => {
expect(warn).not.toHaveBeenCalled();
});
it("guards claude's own config dir too: the apply makes its projects folder", async () => {
probe.mockResolvedValue('unknown');
vi.spyOn(console, 'warn').mockImplementation(() => {});
const lookup = await guardSessionConfigDirHome(getCli('claude')!, env({ CLAUDE_CONFIG_DIR: '/mnt/dead/claude' }));
expect(lookup('CLAUDE_CONFIG_DIR')).toBeUndefined();
expect(probe).toHaveBeenCalledWith('/mnt/dead/claude', { pastCap: true });
});
it('boot recovery probes the own home under the stall cap: it is a bulk pass, not one request', async () => {
probe.mockResolvedValue('unknown');
vi.spyOn(console, 'warn').mockImplementation(() => {});
+76 -1
View File
@@ -24,7 +24,7 @@
* spying on `respawnPane` to read the options the relaunch would get.
* Port: N/A.
*/
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -32,6 +32,11 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import { Session } from '../src/session.js';
import { getCli } from '../src/config/cli-registry/index.js';
import { buildCustomModelInjection } from '../src/custom-model-injection.js';
import {
applyCustomModelInjection,
customModelConfigDir,
removeConfigDir,
} from '../src/custom-model-injection-apply.js';
import { TmuxManager } from '../src/tmux-manager.js';
import type { MuxSession, SessionMode } from '../src/types.js';
@@ -382,3 +387,73 @@ describe('a value the session set itself comes back with the selection gone', ()
expect(session.getCustomModelForPersist()).toEqual({ endpointId: 'ep', modelId: 'q', envKeys: ['GEMINI_API_KEY'] });
});
});
describe('a claude session on its own config dir keeps its conversation', () => {
const ownDir = join(homedir(), 'custom-model-restart-own-claude');
const endpoint = { id: 'ep', label: 'Ep', baseUrl: 'http://box:8080', apiKey: 'k' };
afterEach(() => rmSync(ownDir, { recursive: true, force: true }));
function writeTranscript(configDir: string, conversationId: string): string {
const dir = join(configDir, 'projects', '-custom-model-restart');
mkdirSync(dir, { recursive: true });
const file = join(dir, `${conversationId}.jsonl`);
writeFileSync(file, '{"type":"user"}\n');
return file;
}
/** The route's apply, with the real isolated directory and its `projects` link. */
function applyEndpoint(session: Session) {
const applied = applyCustomModelInjection(
getCli('claude')!,
endpoint,
'q',
session.id,
undefined,
workingDir,
(envVar) => session.ownEnvValue(envVar)
)!;
session.setCustomModel(
{
endpointId: 'ep',
modelId: 'q',
envKeys: applied.envKeys,
configDir: applied.configDir,
homeEnvVar: applied.homeEnvVar,
},
applied.envOverrides
);
return applied;
}
it('resumes it on apply, from under the own dir', async () => {
const { session, respawn } = liveSession('claude', { envOverrides: { CLAUDE_CONFIG_DIR: ownDir } });
writeTranscript(ownDir, session.id);
const applied = applyEndpoint(session);
await session.restartCli();
const options = respawn.mock.calls[0][0];
expect(options.envOverrides?.CLAUDE_CONFIG_DIR).toBe(applied.configDir);
expect(options.resumeSessionId).toBe(session.id);
removeConfigDir(applied.configDir);
});
it('resumes what the endpoint run wrote on clear, on the own account', async () => {
// The server's tree exists, as it does on any machine that has run claude: this is
// where the `projects` link used to send the transcript, out of the own dir's sight.
mkdirSync(join(homedir(), '.claude', 'projects'), { recursive: true });
const { session, respawn } = liveSession('claude', { envOverrides: { CLAUDE_CONFIG_DIR: ownDir } });
const applied = applyEndpoint(session);
writeTranscript(applied.configDir!, session.id);
const { previousConfigDir } = session.setCustomModel(undefined);
removeConfigDir(previousConfigDir);
await session.restartCli();
expect(existsSync(customModelConfigDir(session.id))).toBe(false);
expect(existsSync(join(ownDir, 'projects', '-custom-model-restart', `${session.id}.jsonl`))).toBe(true);
const options = respawn.mock.calls[0][0];
expect(options.envOverrides?.CLAUDE_CONFIG_DIR).toBe(ownDir);
expect(options.resumeSessionId).toBe(session.id);
});
});