Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(hooks): treat a transcript in a not-yet-created project folder as absent (#6739)
garrytane9901ae5ac55
diff --git a/src/core/transcripts/claude-code-jsonl.ts b/src/core/transcripts/claude-code-jsonl.tsindex bffbb16729..9b1a29e317 100644--- a/src/core/transcripts/claude-code-jsonl.ts+++ b/src/core/transcripts/claude-code-jsonl.ts@@ -82,8 +82,10 @@ export type ConfineTranscriptResult =* exception (#5465): an ENOENT leaf whose parent resolves inside the tree* validates ok with `absent: true`, because Claude Code writes transcripts* asynchronously and a not-yet-created file has nothing to read (the same- * trust as no transcript_path at all). An absent parent, or any other lstat- * failure, still fails closed.+ * trust as no transcript_path at all). #6739 extends it to an absent project+ * folder that is a direct child of an existing ancestor inside the tree, with+ * no ".." segment. Any deeper absent path, or any other lstat failure, still+ * fails closed.** Cross-OS install (#4522, Claude Code on the Windows host + gbrain in WSL):* the hook stdin's transcript_path arrives as a Windows drive literal@@ -145,10 +147,21 @@ export function confineTranscriptPath(if ((e as NodeJS.ErrnoException).code !== 'ENOENT') return { ok: false, reason: 'unreadable' };const parent = dirname(candidate);// No directory component (a bare filename — e.g. an untranslated Windows- // drive literal on a non-WSL host) or an absent parent: containment is- // unprovable, so fail closed exactly as before.- if (parent === '.' || !existsSync(parent)) return { ok: false, reason: 'unreadable' };- if (!containedIn(parent)) return { ok: false, reason: 'outside_projects_dir' };+ // drive literal on a non-WSL host): containment is unprovable.+ if (parent === '.') return { ok: false, reason: 'unreadable' };+ if (existsSync(parent)) {+ if (!containedIn(parent)) return { ok: false, reason: 'outside_projects_dir' };+ return { ok: true, path: candidate, size: 0, absent: true };+ }+ // #6739: Claude Code creates projects/<project>/ only after the first+ // prompt in a new working folder, so the parent can be absent too. The+ // path is judged lexically: no ".." segment, and the missing parent must+ // be a direct child of an existing ancestor that resolves inside the+ // tree. Anything deeper or less certain fails closed.+ if (candidate.split(/[\\/]/).includes('..')) return { ok: false, reason: 'unreadable' };+ const ancestor = dirname(parent);+ if (ancestor === parent || !existsSync(ancestor)) return { ok: false, reason: 'unreadable' };+ if (!containedIn(ancestor)) return { ok: false, reason: 'outside_projects_dir' };return { ok: true, path: candidate, size: 0, absent: true };}if (st.isSymbolicLink()) return { ok: false, reason: 'symlink' };diff --git a/test/claude-code-jsonl.test.ts b/test/claude-code-jsonl.test.tsindex 832017400d..832ab824e6 100644--- a/test/claude-code-jsonl.test.ts+++ b/test/claude-code-jsonl.test.ts@@ -343,8 +343,10 @@ describe('confineTranscriptPath [S3#8]', () => {// Absent OUTSIDE the tree: still an escape, not a missing file.const outside = tdir();expect(confineTranscriptPath(join(outside, 'nope.jsonl'), { root })).toEqual({ ok: false, reason: 'outside_projects_dir' });- // Absent parent (containment unprovable): fail closed, as before.- expect(confineTranscriptPath(join(root, 'no-such-dir', 'nope.jsonl'), { root })).toEqual({ ok: false, reason: 'unreadable' });+ // #6739: an absent project folder that is a direct child of the root is+ // the first prompt in a new working folder, so it validates absent too.+ const freshProject = join(root, 'no-such-dir', 'nope.jsonl');+ expect(confineTranscriptPath(freshProject, { root })).toEqual({ ok: true, path: freshProject, size: 0, absent: true });// A symlinked parent that escapes the tree is still seen (the parent// resolves outside → not contained).const escape = join(root, 'escape');@@ -352,6 +354,40 @@ describe('confineTranscriptPath [S3#8]', () => {expect(confineTranscriptPath(join(escape, 'nope.jsonl'), { root })).toEqual({ ok: false, reason: 'outside_projects_dir' });});+ // #6739: Claude Code creates projects/<project>/ only after the first+ // prompt in a new working folder, so the transcript's PARENT can be absent.+ // It validates absent only when the missing folder is a direct child of an+ // existing ancestor contained in the root and no segment is "..".+ test('#6739: an absent project folder directly under the root validates absent', () => {+ const root = tdir();+ const p = join(root, '-home-alice-example-new-project', '18c0ffee-1234-4321-9999-abcdefabcdef.jsonl');+ expect(confineTranscriptPath(p, { root })).toEqual({ ok: true, path: p, size: 0, absent: true });+ const existingProject = join(root, 'proj-a');+ mkdirSync(existingProject);+ const nested = join(existingProject, 'new-sub', 'sess.jsonl');+ expect(confineTranscriptPath(nested, { root })).toEqual({ ok: true, path: nested, size: 0, absent: true });+ });++ test('#6739: an absent parent fails closed outside the root, across "..", two levels deep, or through a symlinked ancestor', () => {+ const base = tdir();+ const root = join(base, 'projects');+ mkdirSync(root);+ const outside = join(base, 'outside');+ mkdirSync(outside);+ expect(confineTranscriptPath(join(outside, 'new', 'sess.jsonl'), { root }))+ .toEqual({ ok: false, reason: 'outside_projects_dir' });+ expect(confineTranscriptPath(`${root}/new/../../outside/new/sess.jsonl`, { root }))+ .toEqual({ ok: false, reason: 'unreadable' });+ expect(confineTranscriptPath(`${root}/new/../sess-dir/sess.jsonl`, { root }))+ .toEqual({ ok: false, reason: 'unreadable' });+ expect(confineTranscriptPath(join(root, 'a', 'b', 'sess.jsonl'), { root }))+ .toEqual({ ok: false, reason: 'unreadable' });+ const escape = join(root, 'escape');+ symlinkSync(outside, escape);+ expect(confineTranscriptPath(join(escape, 'new', 'sess.jsonl'), { root }))+ .toEqual({ ok: false, reason: 'outside_projects_dir' });+ });+test('rejects: directory named like a transcript', () => {const root = tdir();const dirAsFile = join(root, 'dir.jsonl');diff --git a/test/hook-command.serial.test.ts b/test/hook-command.serial.test.tsindex 055e9aad43..954e377ba4 100644--- a/test/hook-command.serial.test.ts+++ b/test/hook-command.serial.test.ts@@ -596,6 +596,32 @@ describe('user-prompt', () => {expect(hb?.turns).toBe(1);});+ // #6739: Claude Code creates projects/<project>/ only after the first+ // prompt in a new working folder, so the first user-prompt hook there sees+ // a transcript whose PARENT folder is missing too. It is the same absent+ // transcript as #5465 and must not record transcript_unreadable.+ test('#6739: a transcript whose project folder does not exist yet delivers prompt-only context', async () => {+ const dataDir = join(tmp, 'data');+ writePgliteConfig(dataDir);+ await startServer({ dataDir, blockText: 'CTX: new-folder context' });+ const projectsRoot = join(tmp, 'projects-root-6739');+ mkdirSync(projectsRoot, { recursive: true });+ const absent = join(projectsRoot, '-home-alice-example-new-project', 'fresh-session.jsonl');+ const out = collectStdout();+ expect(+ await runHook(['user-prompt'], {+ ...out.io,+ stdin: JSON.stringify({ prompt: 'first prompt in a new folder', transcript_path: absent }),+ transcriptRoot: projectsRoot,+ }),+ ).toBe(0);+ const parsed = JSON.parse(out.get().trim());+ expect(parsed.hookSpecificOutput.additionalContext).toBe('CTX: new-folder context');+ const hb = await lastHeartbeat();+ expect(hb?.outcome).toBe('ok');+ expect(hb?.reason).not.toBe('transcript_unreadable');+ });+test('unauthorized (server holds a different secret) degrades cleanly', async () => {const dataDir = join(tmp, 'data');writePgliteConfig(dataDir);