mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-02 21:49:42 +02:00
Which conversation a pane is on was re-derived by correlating ~/.claude/history.jsonl against Session.lastSubmitAt — and lastSubmitAt is bumped only by input that flows through Codeman's own write path (Session.write / writeViaMux). A user who attaches to the pane's tmux session directly never set it, so resolveActiveClaudeSessionIdFromHistory() returned at its first line for that pane's whole life and the response viewer stayed pinned to the launch conversation, showing a pre-/clear transcript indefinitely. A UserPromptSubmit hook reports the live conversation id from inside the CLI process, delivered under the pane's own $CODEMAN_SESSION_ID. That binding is a fact rather than a correlation: it never consults workingDir, so it cannot be claimed by a sibling pane on the same folder, a closed tab, or a bare `claude` in the user's terminal. A pane holding such an id skips the correlation entirely, so the number of prompts eligible for cwd-based guessing goes DOWN, never up — the naive alternative (relax the guard, or synthesize an anchor from PTY activity) is the reverted bug the resolver's own comment describes. The hook also stamps lastSubmitAt, so it finally means "a prompt was submitted" rather than "typed into Codeman's web terminal". Conversations vouched for first-hand — and only those — extend a persisted claudeSessionChain, whose tail re-pins the conversation when a surviving tmux session is re-attached after a restart. ⚠️ start() resets the id at THREE points and the last one runs unconditionally after the mux branch, so the tail is applied there too; patching only the mux branch looks right and silently does nothing. ⚠️ The hook's stdout is discarded with curl's own -o /dev/null. Claude Code injects a UserPromptSubmit hook's stdout into the model's context ("Exit code 0 - stdout shown to Claude"), and a trailing >/dev/null does NOT work: curlCmd already ends `... 2>/dev/null || true`, and in `pipeline || true >/dev/null` the shell binds the redirection to `true`, which never runs on the success path. The discard is opt-in so the five SSE-fed events keep byte-identical command text and no workspace's settings file is rewritten for them. The staleness marker is quote-free for the matching reason: hooksJson is JSON.stringify'd, so a quoted needle never matches and the gate would rewrite every workspace on every spawn. Existing workspaces heal on their next Claude spawn through the staleness sweep.
119 lines
5.7 KiB
TypeScript
119 lines
5.7 KiB
TypeScript
/**
|
|
* @fileoverview Session.claudeSessionChain — the record of which Claude
|
|
* conversations a pane has actually been on.
|
|
*
|
|
* Which conversation the response viewer reads is `Session.claudeSessionId`,
|
|
* and `start()` reassigns it to the launch id at THREE separate points. That is
|
|
* correct for a fresh pane and a lie for a re-attached one: a mux session that
|
|
* survived a Codeman restart never stopped, so the CLI may have `/clear`ed hours
|
|
* ago and moved to a conversation the launch id knows nothing about. The chain
|
|
* is what carries that across the restart, and its tail must therefore outrank
|
|
* the launch id on the restored path only.
|
|
*
|
|
* Two properties are pinned here because both were broken in ways nothing else
|
|
* caught:
|
|
*
|
|
* 1. **Only a first-hand adoption extends the chain.** The id has to come from
|
|
* the CLI's own hook payload, delivered under the pane's `$CODEMAN_SESSION_ID`.
|
|
* A history-correlated guess writing into this record would make the
|
|
* "showed a stranger's conversation" bug permanent instead of transient.
|
|
* 2. **A restored conversation survives every reset point.** The mux branch and
|
|
* the unconditional "third reset point" after it both reassign the field, so
|
|
* patching only the first leaves the restore silently undone.
|
|
*
|
|
* Port: N/A
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { Session } from '../src/session.js';
|
|
|
|
describe('Session claude conversation chain', () => {
|
|
it('extends the chain only for a first-hand adoption', () => {
|
|
const session = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
|
|
// A correlated guess: adopted for display, but never recorded.
|
|
session.adoptClaudeSessionId('guessed-conversation');
|
|
expect(session.claudeSessionId).toBe('guessed-conversation');
|
|
expect(session.claudeSessionChain).toEqual([]);
|
|
expect(session.claudeSessionIdIsFirstHand).toBe(false);
|
|
|
|
// The CLI's own hook: recorded.
|
|
session.adoptClaudeSessionId('hook-conversation', { firstHand: true });
|
|
expect(session.claudeSessionChain).toEqual(['hook-conversation']);
|
|
expect(session.claudeSessionIdIsFirstHand).toBe(true);
|
|
});
|
|
|
|
it('records a /clear successor once, however many prompts report it', () => {
|
|
const session = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
|
|
session.adoptClaudeSessionId('conv-1', { firstHand: true });
|
|
session.adoptClaudeSessionId('conv-1', { firstHand: true }); // every prompt reports the same id
|
|
session.adoptClaudeSessionId('conv-2', { firstHand: true }); // the user ran /clear
|
|
|
|
expect(session.claudeSessionChain).toEqual(['conv-1', 'conv-2']);
|
|
expect(session.claudeSessionId).toBe('conv-2');
|
|
});
|
|
|
|
it('moves a resumed conversation to the tail instead of duplicating it', () => {
|
|
const session = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
|
|
session.adoptClaudeSessionId('conv-1', { firstHand: true });
|
|
session.adoptClaudeSessionId('conv-2', { firstHand: true });
|
|
session.adoptClaudeSessionId('conv-1', { firstHand: true }); // /resume back
|
|
|
|
expect(session.claudeSessionChain).toEqual(['conv-2', 'conv-1']);
|
|
});
|
|
|
|
it('round-trips the chain through toState and re-pins the conversation on restore', () => {
|
|
const original = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
original.adoptClaudeSessionId('conv-1', { firstHand: true });
|
|
original.adoptClaudeSessionId('conv-2', { firstHand: true });
|
|
|
|
const state = original.toState() as { claudeSessionChain?: string[] };
|
|
expect(state.claudeSessionChain).toEqual(['conv-1', 'conv-2']);
|
|
|
|
// Boot recovery rebuilds the pane from that state. The launch id would point
|
|
// the viewer at the pre-/clear conversation; the chain's tail corrects it.
|
|
const restored = new Session({
|
|
workingDir: '/tmp',
|
|
mode: 'claude',
|
|
id: original.id,
|
|
claudeSessionChain: state.claudeSessionChain,
|
|
});
|
|
expect(restored.claudeSessionId).toBe('conv-2');
|
|
// ⚠️ NOT restored: a persisted claim is not a fact. The pane re-earns the
|
|
// guess-free path from its next hook.
|
|
expect(restored.claudeSessionIdIsFirstHand).toBe(false);
|
|
});
|
|
|
|
it('omits the chain from toState when the pane never moved conversation', () => {
|
|
const session = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
expect((session.toState() as { claudeSessionChain?: string[] }).claudeSessionChain).toBeUndefined();
|
|
});
|
|
|
|
it('applies the restored conversation at EVERY reset point in start()', () => {
|
|
// ⚠️ Structural pin, not a behavioural one: exercising start() needs a real
|
|
// PTY and mux. start() reassigns _claudeSessionId at three points, and the
|
|
// last one runs unconditionally AFTER the mux branch — so patching only the
|
|
// mux branch leaves the restore silently undone, which is what shipped
|
|
// before this existed. Every assignment built from the launch-id fallback
|
|
// must therefore carry `restoredConversation` first.
|
|
const source = readFileSync(resolve(import.meta.dirname, '../src/session.ts'), 'utf8');
|
|
const fallbackAssignments = source.match(
|
|
/_claudeSessionId =\s*\n?\s*[^;]*?_resumeSessionId \|\| this\._ompConfig\?\.resumeSessionId \|\| this\.id;/g
|
|
);
|
|
expect(fallbackAssignments).not.toBeNull();
|
|
expect(fallbackAssignments!.length).toBeGreaterThanOrEqual(2);
|
|
for (const assignment of fallbackAssignments!) {
|
|
expect(assignment).toContain('restoredConversation ||');
|
|
}
|
|
});
|
|
|
|
it('leaves a fresh pane on its launch id', () => {
|
|
const session = new Session({ workingDir: '/tmp', mode: 'claude' });
|
|
expect(session.claudeSessionId).toBe(session.id);
|
|
expect(session.claudeSessionChain).toEqual([]);
|
|
});
|
|
});
|