fix(codex): fold a codex session into its own rollout row

Review fixes for #386.

Duplicate rows. A codex conversation showed twice, once live and once as a
past rollout row, because nothing aliased a codex session to its thread id.
That is worse than cosmetic: the stale row still resumes, so clicking it
starts a second `codex resume` on a thread already open in another pane.

  - A RESUMED session knows its thread id up front, so it folds from its own
    side: add `codexConfig.resumeSessionId` to the `claudeSessionId` chain.
    Not only in the constructor — `start()` recomputes that id at two further
    points (the mux branch, and the unconditional "third reset point" whose
    own comment already warned that omitting omp's fallback there stomps the
    mux branch's resolved alias). Both listed Claude's and omp's ids only, so
    for codex every mux reattach and boot recovery reset the alias back to
    the Codeman id and the duplicate returned.
  - A FRESH session has no thread id until codex writes the rollout, so it is
    folded from the other side. The scanner now reports
    `session_meta.originator`, which is `codeman_<sessionId>` for every pane
    Codeman spawns, and `gatherUnifiedInputs()` stamps the matching live and
    persisted rows, newest rollout winning (`/new` inside the TUI leaves
    several rollouts sharing one originator).
  - Persisted rows read `codexConfig.resumeSessionId` too. A resumed session
    demoted to a persisted-only record would otherwise lose its alias, and
    the originator fallback cannot rescue that one: a resumed rollout keeps
    its ORIGINAL session_meta, so it still names the pane that created the
    thread rather than the pane that resumed it.

Identity cache. It was written as soon as the thread id was known, but codex
writes the first user message only when the user submits, so any scan in that
window pinned `firstPrompt: undefined` for the life of the process — and the
home screen, the command palette and the search-index refresh all scan.
`shouldCacheIdentity()` now keeps an identity only once the prompt is known or
the head read filled its whole window.

Also from review: both caps count emitted rows rather than file index, so a
store of sub-agent threads no longer spends the `lastPrompt` budget before the
first row that needed it; the cache is an `LRUMap` sized like the one beside
it; the unreachable filename fallback is gone; a rollout recording no cwd is
dropped rather than emitted with `workingDir: ''`; and the unified-session
module header names all three transcript stores.

Tests. The resume wiring now has cases for a row with a thread id, a row
without one, and a `resumeId` on a non-codex row; the "no continuation is
wired" case narrows to gemini/antigravity, which is no longer true of codex.
`codex-resume-alias-survives-start.test.ts` drives a real Session through
`start()` rather than asserting on pre-stamped inputs — that gap is why the
reset points went unnoticed. Plus the maintainer's own cache repro, the
tail-budget case, a no-cwd case, and merge cases for both folds.
This commit is contained in:
Michael Grundberg
2026-09-06 21:53:38 +02:00
parent 8285fff91c
commit 327e440607
9 changed files with 442 additions and 58 deletions
@@ -0,0 +1,82 @@
/**
* @fileoverview A resumed codex session must keep its thread-id alias across
* `start()`, not just at construction.
*
* `claudeSessionId` doubles as the generic "external transcript id" the unified
* list folds a Past-Sessions row into its owning session by. For codex that id
* is the rollout's thread id, and losing it is not cosmetic: the stale row stays
* in PAST and still resumes, so clicking it starts a SECOND `codex resume` on a
* thread already open in the live pane.
*
* The bug this pins: the alias was wired into the constructor only. `start()`
* recomputes `claudeSessionId` at two further points — the mux branch and the
* unconditional "third reset point" that runs after both the mux and direct-PTY
* paths — and both listed only Claude's `resumeSessionId` and omp's. For codex
* both are undefined, so every mux reattach and every boot recovery reset the
* alias back to the Codeman id and the duplicate came back. The existing comment
* at the third reset point already warned that omitting omp's fallback there
* "stomps the mux branch's correctly-resolved OMP alias"; codex needed the same.
*
* Mirrors `test/omp-fresh-run-no-resume.test.ts`, which drives a real `Session`
* against the in-memory tmux layer that vitest substitutes.
*/
import { mkdirSync, rmSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { Session } from '../src/session.js';
import { TmuxManager } from '../src/tmux-manager.js';
describe('codex: a resumed thread id survives start()', () => {
const workingDir = join(homedir(), 'codeman-cases', 'codex-resume-alias');
const THREAD_ID = '01a060f0-0361-7f91-abde-b283020db0d7';
const sessions: Session[] = [];
afterEach(() => {
for (const s of sessions.splice(0)) s.stop();
rmSync(workingDir, { recursive: true, force: true });
});
function makeSession(useMux: boolean): Session {
mkdirSync(workingDir, { recursive: true });
const session = new Session({
workingDir,
mode: 'codex',
codexConfig: { resumeSessionId: THREAD_ID },
mux: new TmuxManager(),
useMux,
});
sessions.push(session);
return session;
}
it('carries the thread id from construction', () => {
expect(makeSession(true).claudeSessionId).toBe(THREAD_ID);
});
it('still carries it after starting under mux', async () => {
const session = makeSession(true);
await session.startInteractive();
expect(session.claudeSessionId).toBe(THREAD_ID);
});
it('refuses to start without mux at all, so the mux path is the only one to cover', async () => {
// codex declares `requiresMux`, so there is no direct-PTY codex session for
// the third reset point to run against on its own — the assertion above is
// the whole surface.
await expect(makeSession(false).startInteractive()).rejects.toThrow(/require tmux/i);
});
it('a fresh codex session keeps the Codeman id, having no thread of its own', async () => {
mkdirSync(workingDir, { recursive: true });
const session = new Session({ workingDir, mode: 'codex', mux: new TmuxManager(), useMux: true });
sessions.push(session);
await session.startInteractive();
// Nothing to alias to yet — codex has not written the rollout. Such a
// session is folded from the other side, by originator (codexThreadBySessionId).
expect(session.claudeSessionId).toBe(session.id);
});
});