diff --git a/CHANGELOG.md b/CHANGELOG.md index 2383f982..2408241f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,21 @@ new version heading in the same commit. ## [Unreleased] +## [0.471.1] - 2026-11-19 +### Fixed +- **A session whose transcript copy is empty can be reopened again.** If the credential dir a resume runs + under held a copy of the conversation with no turns in it, while the real conversation sat in another + root (an archived `.orphan-*` dir), the session could never be reopened. Resume staging read the + empty copy as "present" and copied nothing. `claude --resume` then answered "No conversation found", and + the `--session-id` fallback answered "Session ID … is already in use" because the file existed. Staging + now looks past a copy KNOWN to hold no conversation: it copies the real one in first, then renames the + empty copy aside (`.empty-`, never deleted), audited `session.transcript.empty-replaced`. An + unreadable copy is treated as possibly real and left alone. `findTranscript` ranks a copy with turns + above one without, so the console's conversation view stops reading the empty copy too; a caller's + `preferRoot` (account blame) still wins outright. + The holding prompt's **[r]** key now runs the same resume preparation; before this, pressing it could + never recover a moved or empty transcript. + ## [0.471.0] - 2026-11-19 ### Fixed - **A busy console can no longer starve the server: the sessions poll stops reading the whole connectors diff --git a/package-lock.json b/package-lock.json index 94ae1af2..facd0856 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "agent-os", - "version": "0.471.0", + "version": "0.471.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "agent-os", - "version": "0.471.0", + "version": "0.471.1", "license": "MIT", "bin": { "agent-os": "bin/agent-os" diff --git a/package.json b/package.json index 07366a75..c4139fe6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "agent-os", - "version": "0.471.0", + "version": "0.471.1", "description": "A generic, governed operating system for running autonomous agents safely across brands. Ships with a local web console.", "license": "MIT", "type": "commonjs", diff --git a/scripts/resume-transcript-stage-test.cjs b/scripts/resume-transcript-stage-test.cjs index 4bbd7ff8..5edc0c67 100644 --- a/scripts/resume-transcript-stage-test.cjs +++ b/scripts/resume-transcript-stage-test.cjs @@ -77,6 +77,63 @@ const before = fs.readFileSync(transcriptAt(dirV), 'utf8'); assert(tm.stageResumeTranscript('ses_1', dirV) === 'present', 'second stage → present'); assert(fs.readFileSync(transcriptAt(dirV), 'utf8') === before, 'the newer turn survives'); +console.log('\n\x1b[1m3b) an EMPTY copy in the target does not block staging\x1b[0m'); +// Live incident (globex, 2026-11-19): the re-added dir held a copy with no conversation in it, so staging +// answered "present", `claude --resume` said "No conversation found", and the `--session-id` fallback said +// "Session ID … is already in use". The session could not be reopened at all. +const { transcriptHasTurns } = require(path.join(ROOT, 'dist/edge/conversation.js')); +const EMPTY_ID = '44444444-0000-4000-8000-000000000004'; +const STUB = '{"type":"permission-mode","permissionMode":"auto"}\n{"type":"file-history-snapshot","snapshot":{}}\n'; +const at = (dir, id) => path.join(dir, 'projects', PROJECT, `${id}.jsonl`); +fs.mkdirSync(path.dirname(at(archived, EMPTY_ID)), { recursive: true }); +fs.writeFileSync(at(archived, EMPTY_ID), REAL); +fs.writeFileSync(at(dirV, EMPTY_ID), STUB); // newer mtime, in the preferred root +aos.db.prepare("INSERT INTO term_sessions (id,agent,title,task,tmux,status,headless,resident,spawned_by,claude_session_id,runtime_account,created_at,updated_at) VALUES ('ses_3','ceo','t','x','aos-ses_3','crashed',0,0,'m_alice',?,'vikas',?,?)") + .run(EMPTY_ID, Date.now(), Date.now()); +assert(!transcriptHasTurns(at(dirV, EMPTY_ID)) && transcriptHasTurns(at(archived, EMPTY_ID)), 'transcriptHasTurns tells the empty copy from the real one'); +const pick = findTranscript(EMPTY_ID); +assert(pick && pick.startsWith(archived + path.sep), 'findTranscript skips an empty copy, even the newest one', String(pick)); +const pinned = findTranscript(EMPTY_ID, { preferRoot: dirV }); +assert(pinned && pinned.startsWith(dirV + path.sep), 'but preferRoot still wins outright (account-blame judges a run by its OWN copy)', String(pinned)); +assert(tm.stageResumeTranscript('ses_3', dirV) === 'staged', 'stage over an empty copy → staged'); +assert(fs.readFileSync(at(dirV, EMPTY_ID), 'utf8') === REAL, 'the real conversation is now where `claude --resume` looks'); +const asideFiles = fs.readdirSync(path.dirname(at(dirV, EMPTY_ID))).filter((f) => f.startsWith(`${EMPTY_ID}.jsonl.empty-`)); +assert(asideFiles.length === 1 && fs.readFileSync(path.join(path.dirname(at(dirV, EMPTY_ID)), asideFiles[0]), 'utf8') === STUB, 'the empty copy is set aside, not deleted'); +assert(auditTypes().includes('session.transcript.empty-replaced'), 'audited session.transcript.empty-replaced'); +assert(tm.stageResumeTranscript('ses_3', dirV) === 'present', 'staging again → present (idempotent)'); +// An empty copy with no real one anywhere is left alone — there is nothing better to put there. +const LONE_ID = '55555555-0000-4000-8000-000000000005'; +fs.writeFileSync(at(dirV, LONE_ID), STUB); +aos.db.prepare("INSERT INTO term_sessions (id,agent,title,task,tmux,status,headless,resident,spawned_by,claude_session_id,runtime_account,created_at,updated_at) VALUES ('ses_4','ceo','t','x','aos-ses_4','crashed',0,0,'m_alice',?,'vikas',?,?)") + .run(LONE_ID, Date.now(), Date.now()); +assert(tm.stageResumeTranscript('ses_4', dirV) === 'present', 'an empty copy with no real one elsewhere → present, untouched'); +assert(fs.readFileSync(at(dirV, LONE_ID), 'utf8') === STUB, 'it is not renamed away'); +// Empty copies in TWO roots: nothing better exists, so nothing is renamed. +const TWO_ID = '66666666-0000-4000-8000-000000000006'; +fs.mkdirSync(path.dirname(at(archived, TWO_ID)), { recursive: true }); +fs.writeFileSync(at(dirV, TWO_ID), STUB); +fs.writeFileSync(at(archived, TWO_ID), STUB); +fs.utimesSync(at(archived, TWO_ID), new Date(), new Date(Date.now() + 60e3)); // the OTHER root's empty copy is newest +aos.db.prepare("INSERT INTO term_sessions (id,agent,title,task,tmux,status,headless,resident,spawned_by,claude_session_id,runtime_account,created_at,updated_at) VALUES ('ses_5','ceo','t','x','aos-ses_5','crashed',0,0,'m_alice',?,'vikas',?,?)") + .run(TWO_ID, Date.now(), Date.now()); +assert(tm.stageResumeTranscript('ses_5', dirV) === 'present', 'empty copies in two roots → present'); +assert(fs.readFileSync(at(dirV, TWO_ID), 'utf8') === STUB && !fs.readdirSync(path.dirname(at(dirV, TWO_ID))).some((f) => f.startsWith(`${TWO_ID}.jsonl.empty-`)), 'nothing renamed aside'); +// An UNREADABLE copy in the target may be the newest real conversation: never set it aside. +if (process.getuid && process.getuid() !== 0) { + const LOCK_ID = '77777777-0000-4000-8000-000000000007'; + fs.mkdirSync(path.dirname(at(archived, LOCK_ID)), { recursive: true }); + fs.writeFileSync(at(archived, LOCK_ID), REAL); + fs.writeFileSync(at(dirV, LOCK_ID), REAL + '{"type":"user","message":{"role":"user","content":"newest turn"}}\n'); + fs.utimesSync(at(archived, LOCK_ID), new Date(), new Date(Date.now() + 60e3)); // so the other root's copy is what staging would pick + fs.chmodSync(at(dirV, LOCK_ID), 0o000); + aos.db.prepare("INSERT INTO term_sessions (id,agent,title,task,tmux,status,headless,resident,spawned_by,claude_session_id,runtime_account,created_at,updated_at) VALUES ('ses_6','ceo','t','x','aos-ses_6','crashed',0,0,'m_alice',?,'vikas',?,?)") + .run(LOCK_ID, Date.now(), Date.now()); + assert(transcriptHasTurns(at(dirV, LOCK_ID)) === undefined, 'an unreadable copy reads as UNKNOWN, not empty'); + assert(tm.stageResumeTranscript('ses_6', dirV) === 'present', 'an unreadable copy in the target → present'); + fs.chmodSync(at(dirV, LOCK_ID), 0o600); + assert(fs.readFileSync(at(dirV, LOCK_ID), 'utf8').includes('newest turn'), 'and it is left in place, newest turn intact'); +} else console.log(' # skip unreadable-copy case under root'); + console.log('\n\x1b[1m4) never writes outside a dir the server vouches for\x1b[0m'); const rogue = path.join(HOME, 'somewhere-else'); assert(tm.stageResumeTranscript('ses_1', rogue) === 'skipped', 'a path that is neither a pool dir nor the box default → skipped'); @@ -137,6 +194,8 @@ const lanes = sh.indexOf('if [ "${RESIDENT:-}" = "1" ] || [ "${UNATTENDED:-}" = const firstResume = sh.search(/^\s+claude --resume "\$CLAUDE_SESSION_ID"/m); assert(call > 0 && call < lanes, 'prepare_resume runs before the unattended/interactive split'); assert(call < firstResume, 'prepare_resume runs before any `claude --resume`'); +const rKey = sh.indexOf(' r|R)'); +assert(rKey > 0 && /prepare_resume/.test(sh.slice(rKey, sh.indexOf('claude --resume', rKey))), 'the [r] key prepares before it resumes too'); const srv = fs.readFileSync(path.join(ROOT, 'src/server.ts'), 'utf8'); const route = srv.indexOf("p === '/api/resume-prepare'"); assert(route > 0 && srv.slice(route, route + 600).includes('sessionSecretOk(session)'), '/api/resume-prepare is session-secret gated'); diff --git a/src/edge/conversation.ts b/src/edge/conversation.ts index 68fe5634..f1315805 100644 --- a/src/edge/conversation.ts +++ b/src/edge/conversation.ts @@ -99,6 +99,37 @@ export function registerTranscriptRoot(dir: string | undefined | null): void { if (dir) extraRoots.add(dir); } +/** + * Whether a transcript file holds any conversation at all — one `user` or `assistant` entry. A copy can + * exist with none (only metadata lines, or empty), and that copy is a trap: `claude --resume ` answers + * "No conversation found", while `claude --session-id ` refuses the id as "already in use" because + * the file exists. The launcher tries one then the other, so the session can never be reopened. + * `undefined` = could not read it: callers must treat that as "may hold turns", never as empty, because + * "empty" licenses setting the file aside. Reads a small first chunk, then 64 KB ones, stopping at the + * first hit, so a long transcript costs one small read. + */ +export function transcriptHasTurns(file: string): boolean | undefined { + const re = /"type"\s*:\s*"(?:user|assistant)"/; + let fd: number | undefined; + try { + fd = fs.openSync(file, 'r'); + let buf = Buffer.allocUnsafe(8 * 1024); + let tail = ''; + let n: number; + while ((n = fs.readSync(fd, buf, 0, buf.length, null)) > 0) { + const text = tail + buf.subarray(0, n).toString('utf8'); + if (re.test(text)) return true; + tail = text.slice(-64); // a match split across two chunks + if (buf.length < 64 * 1024) buf = Buffer.allocUnsafe(64 * 1024); + } + return false; + } catch { + return undefined; + } finally { + if (fd !== undefined) try { fs.closeSync(fd); } catch { /* already closed */ } + } +} + /** The copy of `.jsonl` under ONE root's `projects/`, if it has one. */ export function transcriptInRoot(root: string, claudeSessionId: string): string | undefined { const projects = path.join(root, 'projects'); @@ -130,6 +161,9 @@ export function transcriptInRoot(root: string, claudeSessionId: string): string * * So: gather every copy and answer with the one that actually describes the run being asked about — * `preferRoot` (the credential dir the run used) when it has a copy, else the most recently modified. + * Among copies OUTSIDE the preferred root, one with no conversation in it ({@link transcriptHasTurns}) + * describes nothing, so a copy that may hold turns beats it. The preferred root still wins outright: + * `preferRoot` is how account-blame callers judge a run by its OWN copy, never another account's. */ export function findTranscript(claudeSessionId: string, opts: { preferRoot?: string } = {}): string | undefined { const wanted = `${claudeSessionId}.jsonl`; @@ -154,7 +188,11 @@ export function findTranscript(claudeSessionId: string, opts: { preferRoot?: str } } if (!hits.length) return undefined; - const pool = hits.some((h) => h.preferred) ? hits.filter((h) => h.preferred) : hits; + let pool = hits.some((h) => h.preferred) ? hits.filter((h) => h.preferred) : hits; + if (pool.length > 1) { + const withTurns = pool.filter((h) => transcriptHasTurns(h.file) !== false); + if (withTurns.length) pool = withTurns; + } return pool.reduce((a, b) => (b.mtimeMs > a.mtimeMs ? b : a)).file; } diff --git a/src/terminal.ts b/src/terminal.ts index 33c5bb0b..56044395 100644 --- a/src/terminal.ts +++ b/src/terminal.ts @@ -53,7 +53,7 @@ import { readSessionCost } from './edge/session-cost'; import { readTranscriptEnd } from './edge/outcome'; import { pendingBackgroundWork, BACKGROUND_GRACE_MS, UNATTENDED_TURN_BRIEF, WAITING_BRIEF } from './edge/background-work'; import { findCodexRollout, readCodexCost, readCodexConversation } from './edge/codex-transcript'; -import { readConversation, readConversationBounded, findTranscript, registerTranscriptRoot, transcriptInRoot, claudeConfigDir, Conversation } from './edge/conversation'; +import { readConversation, readConversationBounded, findTranscript, registerTranscriptRoot, transcriptInRoot, transcriptHasTurns, claudeConfigDir, Conversation } from './edge/conversation'; import { digestConversation, sessionConsoleUrl, toSessionFtsQuery, SESSION_TEXT_READ_HEAD, SESSION_TEXT_READ_MAX, SESSION_TEXT_REREAD_MS } from './edge/session-search'; // Video render tuning: a submitted job renders async. The in-call path polls briefly for the fast case; @@ -1312,7 +1312,8 @@ export class TerminalManager { * re-seeded with the original prompt — under the same id, so it looks like a resume that lost its memory. * * Copies the newest copy from any known root (pool dirs, archived dirs, the box default) into the target. - * Never overwrites a copy the target already has, and only ever writes into a dir the server itself + * Never overwrites a copy the target already has, unless that copy holds no conversation and another + * root's copy does (the empty one is renamed aside, not deleted). Only ever writes into a dir the server itself * vouches for (a pool account's dir or the box default): the route is reachable with a session secret an * agent holds, so a client-supplied path must never become a write target. */ @@ -1326,17 +1327,39 @@ export class TerminalManager { let pool: string[] = []; try { pool = this.os.runtimeAccounts.configDirs(); } catch { /* pool is optional */ } if (![claudeConfigDir(), ...pool].some((d) => real(d) === real(target))) return 'skipped'; - if (transcriptInRoot(target, row.claude_session_id)) return 'present'; + // A copy that holds no conversation is not "present": `claude --resume` says "No conversation found", + // the `--session-id` fallback says "already in use", and the session can never reopen. Look past it — + // but only when it is KNOWN empty; an unreadable copy may be the newest real conversation. + const existing = transcriptInRoot(target, row.claude_session_id); + if (existing && transcriptHasTurns(existing) !== false) return 'present'; this.refreshTranscriptRoots(); const src = findTranscript(row.claude_session_id); + if (existing && (!src || transcriptHasTurns(src) !== true)) return 'present'; if (!src) { this.audit(sessionId, row.agent, 'session.transcript.missing', { claudeSessionId: row.claude_session_id, dir: target }); return 'missing'; } - const destDir = path.join(target, 'projects', path.basename(path.dirname(src))); + const destDir = existing ? path.dirname(existing) : path.join(target, 'projects', path.basename(path.dirname(src))); + const dest = path.join(destDir, `${row.claude_session_id}.jsonl`); fs.mkdirSync(destDir, { recursive: true }); - fs.copyFileSync(src, path.join(destDir, `${row.claude_session_id}.jsonl`)); + // Copy FIRST, then swap: a failure part-way must never leave the path empty, because then the + // `--session-id` fallback starts a blank conversation there and the real one stays hidden for good. + const tmp = `${dest}.staging-${Date.now()}`; + fs.copyFileSync(src, tmp); + let aside: string | undefined; + try { + if (existing) { + aside = `${existing}.empty-${Date.now()}`; + fs.renameSync(existing, aside); // set aside, never deleted + } + fs.renameSync(tmp, dest); + } catch (e) { + if (aside) try { fs.renameSync(aside, existing!); } catch { /* best effort */ } + try { fs.unlinkSync(tmp); } catch { /* already gone */ } + throw e; + } registerTranscriptRoot(target); + if (aside) this.audit(sessionId, row.agent, 'session.transcript.empty-replaced', { empty: aside, from: src }); this.audit(sessionId, row.agent, 'session.transcript.staged', { from: src, to: destDir }); return 'staged'; } catch { diff --git a/terminal/claude-launch.sh b/terminal/claude-launch.sh index 5bfba3ae..51526560 100755 --- a/terminal/claude-launch.sh +++ b/terminal/claude-launch.sh @@ -597,6 +597,9 @@ while true; do IFS= read -rsn1 key || { sleep 2; continue; } # blocked read is fine; EOF/detached → idle, no spin case "$key" in r|R) + # Same preparation as a fresh resume: [r] reopens the conversation under whatever dir we hold, and + # without this a transcript that was moved or left empty is never staged, so [r] fails forever. + [ -n "${CLAUDE_SESSION_ID:-}" ] && prepare_resume notify_resumed claude --resume "${CLAUDE_SESSION_ID:-}" "${COMMON_ARGS[@]}" \ || claude --session-id "${CLAUDE_SESSION_ID:-}" "${COMMON_ARGS[@]}" "$TASK"