mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-18 12:19:33 +01:00
The viewer looked for a Claude transcript under ~/.claude/projects only, and for the conversation after a /clear in ~/.claude/history.jsonl only. A session started with its own CLAUDE_CONFIG_DIR (a second account, #255), or any session on a server that exports one, writes both elsewhere, so the eye button showed "No response yet" or a terminal dump, and codeman agent read and the Discord bridge's last message came back empty. This is the third part of the report in issue 701; the other two are a design choice and a feature request. lastResponseFor() now asks claudeConversationDirs() for the folder the session's claude writes to: its own override, then the server's, then ~/.claude, through claudeProjectsDir(), which the relaunch pin and the Edit history panel already use. The rename's /resume title takes the same lookup when no hook has named the transcript yet. - Local sessions only. In a Docker case or on a remote host the override names a folder over there, and the same path on this host is someone else's. - A relocated folder is a path somebody chose, so it is probed bounded first (the rule from issue 516). Anything but a directory reads as no transcript, never as a reason to look under ~/.claude, where a file of the same name is another account's conversation. - The default folder is not probed, so that path is as before. Not changed: the resume pre-validation at session create and the host-wide history scans still read the default folder. Tests: five route cases for the viewer (own folder, the server's, no fallback, /clear through the relocated history, Docker left alone) and one for the rename. Each part of the change fails one of them when reverted. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
180 lines
7.6 KiB
TypeScript
180 lines
7.6 KiB
TypeScript
/**
|
|
* @fileoverview PUT /api/sessions/:id/name hands the name to the user (#376).
|
|
*
|
|
* A rename flips `nameSource` to `manual`, persists it and broadcasts it, so
|
|
* auto-naming can never overwrite a name a person chose, on this server or
|
|
* on the one that restores the session after a restart.
|
|
*
|
|
* The rename also reaches Claude's own `/resume` title: a `custom-title` row is
|
|
* appended to the conversation's transcript, the row `/rename` writes.
|
|
*
|
|
* Uses app.inject() — no real HTTP ports needed.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
|
|
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { registerSessionRoutes } from '../../src/web/routes/session-routes.js';
|
|
import { createRouteTestHarness, type RouteTestHarness } from './_route-test-utils.js';
|
|
import { Session } from '../../src/session.js';
|
|
import { SseEvent } from '../../src/web/sse-events.js';
|
|
|
|
describe('PUT /api/sessions/:id/name', () => {
|
|
let harness: RouteTestHarness;
|
|
let session: Session;
|
|
const updateSessionName = vi.fn(() => true);
|
|
const transcriptDir = mkdtempSync(join(tmpdir(), 'codeman-rename-title-'));
|
|
const transcriptPath = join(transcriptDir, '6f1c1a2e-0000-4000-8000-000000000001.jsonl');
|
|
const transcriptRows = () =>
|
|
readFileSync(transcriptPath, 'utf8')
|
|
.split('\n')
|
|
.filter(Boolean)
|
|
.map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
|
|
beforeAll(async () => {
|
|
writeFileSync(transcriptPath, `${JSON.stringify({ type: 'user', message: { role: 'user', content: 'hi' } })}\n`);
|
|
harness = await createRouteTestHarness(registerSessionRoutes);
|
|
// A REAL session, since the ownership flag lives on the class, not the mock.
|
|
session = new Session({ id: 'name-route-test', workingDir: '/tmp', name: 'w1-demo' });
|
|
harness.ctx.sessions.set(session.id, session as never);
|
|
(harness.ctx.mux as Record<string, unknown>).updateSessionName = updateSessionName;
|
|
(harness.ctx as Record<string, unknown>).getTranscriptPath = (id: string) =>
|
|
id === session.id ? transcriptPath : null;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await harness.app.close();
|
|
rmSync(transcriptDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('treats a same-name PUT as a no-op: stays placeholder, writes no title row', async () => {
|
|
// The Session Options field saves on blur and recomposes the unchanged placeholder.
|
|
const before = transcriptRows().length;
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: 'w1-demo' },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toMatchObject({ name: 'w1-demo' });
|
|
expect(session.nameSource).toBe('placeholder');
|
|
expect(transcriptRows()).toHaveLength(before);
|
|
expect(updateSessionName).not.toHaveBeenCalled();
|
|
expect(harness.ctx.persistSessionState).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('flips a placeholder to manual, then persists and broadcasts the ownership', async () => {
|
|
expect(session.nameSource).toBe('placeholder');
|
|
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: 'my window' },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
// The harness registers the bare route; the {success,data} envelope is a server-level hook.
|
|
expect(res.json()).toMatchObject({ name: 'my window' });
|
|
expect(session.name).toBe('my window');
|
|
expect(session.nameSource).toBe('manual');
|
|
expect(session.applyAutoName('w1-demo: fix it')).toBe(false);
|
|
expect(session.name).toBe('my window');
|
|
|
|
expect(updateSessionName).toHaveBeenCalledWith(session.id, 'my window');
|
|
expect(harness.ctx.persistSessionState).toHaveBeenCalledWith(session);
|
|
expect(harness.ctx.broadcast).toHaveBeenCalledWith(
|
|
SseEvent.SessionUpdated,
|
|
expect.objectContaining({ id: session.id, name: 'my window', nameSource: 'manual' })
|
|
);
|
|
// What the restore path will read back: the persisted state carries the flag.
|
|
expect(session.toState().nameSource).toBe('manual');
|
|
});
|
|
|
|
it("appends the name as the conversation's custom-title, the row /resume reads", async () => {
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: '修复登录跳转' },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(transcriptRows().at(-1)).toEqual({
|
|
type: 'custom-title',
|
|
customTitle: '修复登录跳转',
|
|
sessionId: '6f1c1a2e-0000-4000-8000-000000000001',
|
|
});
|
|
});
|
|
|
|
it('writes no title row for an empty name, which would blank the /resume entry', async () => {
|
|
const before = transcriptRows().length;
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: ' ' },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(transcriptRows()).toHaveLength(before);
|
|
});
|
|
|
|
it('appends a title row only once when the same name is PUT twice', async () => {
|
|
const before = transcriptRows().length;
|
|
for (let i = 0; i < 2; i++) {
|
|
await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: 'twice' },
|
|
});
|
|
}
|
|
expect(transcriptRows()).toHaveLength(before + 1);
|
|
});
|
|
|
|
it("finds the transcript in the session's relocated Claude config folder when no hook has named it (#701)", async () => {
|
|
// Before the first hook after a restart the watcher has no path, and the scan used to
|
|
// look under ~/.claude alone, so a session on another account kept its old /resume title.
|
|
const configDir = join(transcriptDir, 'claude-clients', 'acme');
|
|
const projectDir = join(configDir, 'projects', '-tmp');
|
|
mkdirSync(projectDir, { recursive: true });
|
|
const relocatedPath = join(projectDir, `${session.id}.jsonl`);
|
|
writeFileSync(relocatedPath, `${JSON.stringify({ type: 'user', message: { role: 'user', content: 'hi' } })}\n`);
|
|
const hookPath = harness.ctx.getTranscriptPath;
|
|
(harness.ctx as Record<string, unknown>).getTranscriptPath = () => null;
|
|
Object.defineProperty(session, 'claudeConfigDir', { configurable: true, value: () => configDir });
|
|
try {
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: 'client work' },
|
|
});
|
|
expect(res.statusCode).toBe(200);
|
|
const rows = readFileSync(relocatedPath, 'utf8')
|
|
.split('\n')
|
|
.filter(Boolean)
|
|
.map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
expect(rows.at(-1)).toEqual({ type: 'custom-title', customTitle: 'client work', sessionId: session.id });
|
|
} finally {
|
|
delete (session as unknown as Record<string, unknown>).claudeConfigDir;
|
|
(harness.ctx as Record<string, unknown>).getTranscriptPath = hookPath;
|
|
}
|
|
});
|
|
|
|
it('writes no title row for a docker session, whose transcript lives in the container', async () => {
|
|
const before = transcriptRows().length;
|
|
Object.defineProperty(session, 'docker', { configurable: true, get: () => ({ caseName: 'c' }) });
|
|
try {
|
|
const res = await harness.app.inject({
|
|
method: 'PUT',
|
|
url: `/api/sessions/${session.id}/name`,
|
|
payload: { name: 'in a container' },
|
|
});
|
|
expect(res.statusCode).toBe(200);
|
|
expect(session.name).toBe('in a container');
|
|
expect(transcriptRows()).toHaveLength(before);
|
|
} finally {
|
|
delete (session as unknown as Record<string, unknown>).docker;
|
|
}
|
|
});
|
|
});
|