diff --git a/scripts/lib/session-manager.js b/scripts/lib/session-manager.js index a4b900058..bfd3517b2 100644 --- a/scripts/lib/session-manager.js +++ b/scripts/lib/session-manager.js @@ -167,9 +167,11 @@ function buildSessionRecord(sessionPath, metadata) { }; } -function sessionMatchesId(metadata, normalizedSessionId) { +function sessionMatchesId(metadata, normalizedSessionId, exact = false) { const filename = metadata.filename; - const shortIdMatch = metadata.shortId !== 'no-id' && metadata.shortId.startsWith(normalizedSessionId); + const shortIdMatch = metadata.shortId !== 'no-id' && (exact + ? metadata.shortId === normalizedSessionId + : metadata.shortId.startsWith(normalizedSessionId)); const filenameMatch = filename === normalizedSessionId || filename === `${normalizedSessionId}.tmp`; const noIdMatch = metadata.shortId === 'no-id' && filename === `${normalizedSessionId}-session.tmp`; @@ -216,7 +218,11 @@ function getMatchingSessionCandidates(normalizedSessionId) { } } - matches.sort((a, b) => b.modifiedTime - a.modifiedTime); + // All exact aliases share priority; keep newest-first ordering for ties. + matches.sort((a, b) => + Number(sessionMatchesId(b, normalizedSessionId, true)) - Number(sessionMatchesId(a, normalizedSessionId, true)) || + b.modifiedTime - a.modifiedTime + ); return matches; } diff --git a/tests/lib/session-id-collisions.test.js b/tests/lib/session-id-collisions.test.js new file mode 100644 index 000000000..27f5fc46b --- /dev/null +++ b/tests/lib/session-id-collisions.test.js @@ -0,0 +1,51 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const test = require('node:test'); +const { getSessionById } = require('../../scripts/lib/session-manager'); + +for (const [label, filename, query] of [ + ['extensionless filename', '2026-04-01-frontend-session.tmp', '2026-04-01-frontend-session'], + ['legacy date alias', '2026-04-01-session.tmp', '2026-04-01'], +]) { + test(`ambiguous exact ${label} and short ID retain modification-time ordering`, t => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-session-id-collision-')); + const sessions = path.join(root, 'session-data'); + fs.mkdirSync(sessions); + const prior = process.env.ECC_AGENT_DATA_HOME; + process.env.ECC_AGENT_DATA_HOME = root; + t.after(() => { + if (prior === undefined) delete process.env.ECC_AGENT_DATA_HOME; + else process.env.ECC_AGENT_DATA_HOME = prior; + fs.rmSync(root, { recursive: true, force: true }); + }); + + const shortIdFilename = `2026-04-02-${query}-session.tmp`; + const baseTime = Date.parse('2026-11-11T12:00:00.000Z'); + const write = (name, age) => { + const file = path.join(sessions, name); + fs.writeFileSync(file, `# ${name}\n`); + const timestamp = new Date(baseTime + age * 1000); + fs.utimesSync(file, timestamp, timestamp); + }; + write(filename, 2); + write(shortIdFilename, 1); + + assert.equal(getSessionById(query, true).filename, filename, + 'Equally exact aliases retain the previous newest-first behavior'); + assert.equal(getSessionById(filename, true).filename, filename, + 'The full .tmp filename selects its own record without ambiguity'); + + write(`2026-04-03-${query}-more-session.tmp`, 3); + assert.equal(getSessionById(query).filename, filename, + 'A newer prefix match must not displace an exact filename or date alias'); + + write(shortIdFilename, 4); + assert.equal(getSessionById(query).filename, shortIdFilename, + 'The newer exact short ID wins when the exact-match timestamps reverse'); + assert.equal(getSessionById(filename).filename, filename); + }); +} diff --git a/tests/lib/session-manager.test.js b/tests/lib/session-manager.test.js index 507ab3c91..d1fab07a4 100644 --- a/tests/lib/session-manager.test.js +++ b/tests/lib/session-manager.test.js @@ -454,6 +454,33 @@ src/main.ts // getSessionById tests console.log('\ngetSessionById:'); + if (test('getSessionById prefers an exact ID over newer prefix matches', () => { + const fixtures = [ + ['2026-03-01-frontend-session.tmp', 1000], + ['2026-03-02-frontend-session.tmp', 2000], + ['2026-03-03-frontend-2-session.tmp', 3000] + ]; + const files = fixtures.map(([filename, mtime]) => { + const file = path.join(tmpCanonicalSessionsDir, filename); + fs.writeFileSync(file, `# ${filename}\n`); + fs.utimesSync(file, mtime, mtime); + return file; + }); + const savedAgentDataHome = process.env.ECC_AGENT_DATA_HOME; + process.env.ECC_AGENT_DATA_HOME = path.join(tmpHome, '.claude'); + try { + const exact = sessionManager.getSessionById('frontend', true); + assert.strictEqual(exact.filename, fixtures[1][0]); + assert.ok(exact.content.includes(fixtures[1][0])); + assert.strictEqual(sessionManager.getSessionById('front').filename, fixtures[2][0]); + assert.strictEqual(sessionManager.getSessionById(fixtures[0][0]).filename, fixtures[0][0]); + } finally { + if (savedAgentDataHome === undefined) delete process.env.ECC_AGENT_DATA_HOME; + else process.env.ECC_AGENT_DATA_HOME = savedAgentDataHome; + for (const file of files) fs.unlinkSync(file); + } + })) passed++; else failed++; + if (test('getSessionById finds by short ID prefix', () => { const result = sessionManager.getSessionById('abcd1234'); assert.ok(result, 'Should find session by exact short ID');