From 7a200915b4778c535fabc79dc22390bdbe70c8b5 Mon Sep 17 00:00:00 2001 From: Aditya Vikram Singh <247195684+avs-io@users.noreply.github.com> Date: Wed, 29 Jul 2026 22:58:10 +0530 Subject: [PATCH] fix(web): accept a session id prefix in /api/context/tree `codeburn context ` accepts an 8-char session id prefix (findClaudeSession/ findCodexSession resolve it via startsWith), but /api/context/tree additionally required the resolved ref's sessionId to equal the id passed in verbatim - so the same prefix that works on the CLI 404s through the API. findClaudeSession/findCodexSession are already the single source of truth for prefix resolution used by the CLI; the API now trusts that resolution instead of re-validating for an exact match afterwards, so both surfaces behave the same way for a prefix. Fixes getagentseal/codeburn#767 (item 2). --- src/web-dashboard.ts | 6 +- tests/context-tree-api-prefix.test.ts | 81 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/context-tree-api-prefix.test.ts diff --git a/src/web-dashboard.ts b/src/web-dashboard.ts index 83d42903..283146b8 100644 --- a/src/web-dashboard.ts +++ b/src/web-dashboard.ts @@ -322,8 +322,12 @@ export async function runWebDashboard(opts: { writeJsonError(res, 400, 'provider (claude|codex) and id are required') return } + // findClaudeSession/findCodexSession resolve an id prefix the same way + // `codeburn context ` does (see context-tree.ts's resolveSession) - + // trust that match instead of re-requiring the full id here, so a prefix + // that works on the CLI works through the API too. const ref = provider === 'claude' ? await findClaudeSession(id) : await findCodexSession(id) - if (!ref || ref.sessionId !== id) { + if (!ref) { writeJsonError(res, 404, `no ${provider} session ${id}`) return } diff --git a/tests/context-tree-api-prefix.test.ts b/tests/context-tree-api-prefix.test.ts new file mode 100644 index 00000000..f95ee096 --- /dev/null +++ b/tests/context-tree-api-prefix.test.ts @@ -0,0 +1,81 @@ +import { mkdtemp, mkdir, rm, writeFile } from 'fs/promises' +import { tmpdir } from 'os' +import { join } from 'path' +import type { AddressInfo } from 'net' +import type { Server } from 'http' + +import { afterEach, beforeEach, describe, expect, it } from 'vitest' + +import { runWebDashboard } from '../src/web-dashboard.js' + +// Issue #767 item 2: `codeburn context ` accepts an 8-char session id +// prefix (findClaudeSession does a startsWith match), but /api/context/tree +// additionally required the resolved ref's sessionId to equal the id passed +// in verbatim -- so the same prefix that works on the CLI 404s through the +// API. findClaudeSession is the single source of truth for prefix resolution +// on both surfaces; the API should trust it exactly like the CLI does instead +// of re-validating for an exact match afterwards. +const SESSION_ID = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' +const SESSION_PREFIX = SESSION_ID.slice(0, 8) + +describe('web dashboard /api/context/tree: session id prefix', () => { + let server: Server + let base: string + let homeDir: string + let cacheDir: string + + beforeEach(async () => { + homeDir = await mkdtemp(join(tmpdir(), 'codeburn-context-home-')) + cacheDir = await mkdtemp(join(tmpdir(), 'codeburn-context-cache-')) + process.env['HOME'] = homeDir + process.env['CODEBURN_CACHE_DIR'] = cacheDir + delete process.env['CLAUDE_CONFIG_DIR'] + + const projectDir = join(homeDir, '.claude', 'projects', '-tmp-fixture-project') + await mkdir(projectDir, { recursive: true }) + await writeFile(join(projectDir, `${SESSION_ID}.jsonl`), JSON.stringify({ + type: 'assistant', + sessionId: SESSION_ID, + timestamp: '2026-07-01T00:00:00.000Z', + cwd: '/tmp/fixture-project', + message: { + id: 'msg-1', + type: 'message', + role: 'assistant', + model: 'claude-sonnet-4-5', + content: [{ type: 'text', text: 'hi' }], + usage: { input_tokens: 100, output_tokens: 50 }, + }, + }) + '\n') + + server = await runWebDashboard({ + period: 'today', provider: 'all', project: [], exclude: [], port: 0, open: false, + }) + base = `http://127.0.0.1:${(server.address() as AddressInfo).port}` + }) + + afterEach(async () => { + await new Promise((resolve) => server.close(() => resolve())) + await rm(homeDir, { recursive: true, force: true }) + await rm(cacheDir, { recursive: true, force: true }) + }) + + it('resolves a full session id (control case)', async () => { + const res = await fetch(`${base}/api/context/tree?provider=claude&id=${SESSION_ID}`) + expect(res.status).toBe(200) + const payload = await res.json() as { session: { sessionId: string } } + expect(payload.session.sessionId).toBe(SESSION_ID) + }) + + it('resolves an 8-char session id prefix, matching the CLI', async () => { + const res = await fetch(`${base}/api/context/tree?provider=claude&id=${SESSION_PREFIX}`) + expect(res.status).toBe(200) + const payload = await res.json() as { session: { sessionId: string } } + expect(payload.session.sessionId).toBe(SESSION_ID) + }) + + it('still 404s for an id that matches nothing', async () => { + const res = await fetch(`${base}/api/context/tree?provider=claude&id=deadbeef`) + expect(res.status).toBe(404) + }) +})