fix(review): dedupe resumed-session rows + newest-wins lifecycle name/mode (PR #139)

- Duplicate rows: transcript-history rows are keyed by the Claude
  conversation UUID (.jsonl filename stem), which diverges from the Codeman
  session id for resumed (claudeSessionId = resumeSessionId != id) and
  /clear-respawned sessions, so one conversation surfaced as both a live row
  and a history-only row. mergeUnifiedSessions now builds an alias map
  (claudeSessionId -> Codeman id) from the live + persisted views and
  resolves history/lifecycle keys through it; the route feeds
  SessionState.resumeSessionId as the persisted alias.
- Inverted precedence: SessionLifecycleLog.query() returns entries
  NEWEST-first, but the merge loop unconditionally overwrote name/mode so
  the OLDEST entry in the window won (stale rename/mode). First-seen now
  wins, mirroring the existing lastActivityAt guard.
- Tests: resumed session yields ONE row (service unit + route end-to-end
  with a real transcript fixture); renamed-then-deleted session surfaces
  the NEWEST name/mode. All 4 new tests fail against the pre-fix code.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer
2026-07-12 12:41:48 +02:00
parent ce4c5dd584
commit 0ad673794f
4 changed files with 119 additions and 11 deletions
+30 -1
View File
@@ -11,7 +11,7 @@
import { describe, it, expect, afterEach, beforeAll, afterAll, vi } from 'vitest';
import Fastify, { type FastifyInstance } from 'fastify';
import fastifyCookie from '@fastify/cookie';
import { mkdtempSync, rmSync } from 'node:fs';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createMockRouteContext, type MockRouteContext } from '../mocks/index.js';
@@ -92,6 +92,8 @@ describe('GET /api/sessions/unified', () => {
afterEach(async () => {
if (harness) await harness.app.close();
// Drop any per-test transcript fixtures so other tests see an empty home.
rmSync(join(tmpHome, '.claude'), { recursive: true, force: true });
});
it('returns the {sessions,total} envelope with default (testMode) ctx', async () => {
@@ -135,4 +137,31 @@ describe('GET /api/sessions/unified', () => {
// limit clamps to a minimum of 1, so at most 1 row is returned.
expect(body.data.sessions.length).toBeLessThanOrEqual(1);
});
it('folds a resumed session transcript (claudeSessionId != id) into ONE row', async () => {
// Seed a real transcript fixture keyed by the Claude conversation UUID
// (the .jsonl filename stem), like a resumed session leaves behind.
const uuid = 'aabbccdd-1111-2222-3333-444455556666';
const projDir = join(tmpHome, '.claude', 'projects', '-tmp-test-workdir');
mkdirSync(projDir, { recursive: true });
const line = JSON.stringify({ type: 'user', message: { role: 'user', content: 'resumed prompt' } }) + '\n';
writeFileSync(join(projDir, `${uuid}.jsonl`), line.repeat(60)); // >4000 bytes so the scanner keeps it
const ctx = makeLiveCtx();
// Resumed session: the live Codeman session owns that conversation UUID.
const live = ctx.sessions.get('test-session-1') as unknown as { claudeSessionId?: string };
live.claudeSessionId = uuid;
harness = await createEnvelopeHarness(ctx);
const res = await harness.app.inject({ method: 'GET', url: '/api/sessions/unified' });
expect(res.statusCode).toBe(200);
const rows = res.json().data.sessions as Array<{ sessionId: string; sources: string[] }>;
// No separate history-only row keyed by the conversation UUID…
expect(rows.map((r) => r.sessionId)).not.toContain(uuid);
// …the transcript merged into the owning live session instead.
const row = rows.find((r) => r.sessionId === 'test-session-1');
expect(row).toBeDefined();
expect(row!.sources).toContain('live');
expect(row!.sources).toContain('history');
});
});