Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(dream): date undated-filename transcripts from their frontmatter (#7267)
garrytan8d2296f40d61
diff --git a/src/core/cycle/transcript-discovery.ts b/src/core/cycle/transcript-discovery.tsindex f41af7bb37..ae1f65bbd8 100644--- a/src/core/cycle/transcript-discovery.ts+++ b/src/core/cycle/transcript-discovery.ts@@ -14,6 +14,7 @@import { readFileSync, readdirSync, statSync } from 'node:fs';import { join, basename, dirname, sep } from 'node:path';import { createHash } from 'node:crypto';+import { parseDataFrontmatter } from '../data-frontmatter.ts';import { pruneDir } from '../sync.ts';import { realpathOrResolve, resolvedPrefixContained } from '../path-confine.ts';import { CORPUS_SPOOL_SUBDIR, corpusFileSessionId } from '../context/corpus-segments.ts';@@ -32,7 +33,7 @@ export interface DiscoveredTranscript {content: string;/** Filename basename without extension; used as a topic-slug seed. */basename: string;- /** Inferred date if the basename matches `YYYY-MM-DD...` (or null). */+ /** The basename's leading `YYYY-MM-DD`, else the frontmatter `date` (#7267), else null. */inferredDate: string | null;/** #4618: the capturing seat from the session's `.seat.json` sidecar, when one exists. */seat?: string;@@ -47,7 +48,7 @@ export interface DiscoverOpts {minChars?: number;/** Word-boundary regex strings. The discoverer auto-wraps bare words. */excludePatterns?: string[];- /** Restrict to a single date (YYYY-MM-DD basename match). */+ /** Restrict to a single date (YYYY-MM-DD, matched against `inferredDate`). */date?: string;/** Inclusive range start (YYYY-MM-DD). */from?: string;@@ -78,6 +79,24 @@ export interface DiscoverOpts {const DATE_RE = /^(\d{4}-\d{2}-\d{2})/;const WORD_BOUNDARY_HEURISTIC = /^[a-zA-Z][a-zA-Z0-9_-]*$/;+/** A transcript's frontmatter; none, or unreadable, is `{}` (a plain-text header is not an error). */+function transcriptFrontmatter(content: string): Record<string, unknown> {+ try {+ return parseDataFrontmatter(content).data;+ } catch {+ return {};+ }+}++/** The calendar day of a frontmatter `date`; an impossible day (2026-02-30) is none. */+function frontmatterDay(fm: Record<string, unknown>): string | null {+ const raw = fm.date instanceof Date ? fm.date.toISOString() : typeof fm.date === 'string' ? fm.date : '';+ const day = DATE_RE.exec(raw)?.[1];+ if (!day) return null;+ const ms = Date.parse(`${day}T00:00:00Z`);+ return Number.isFinite(ms) && new Date(ms).toISOString().startsWith(day) ? day : null;+}+/*** Self-consumption guard: identity-marker check against `dream_generated: true`* stamped by the synthesize phase's render paths.@@ -280,7 +299,7 @@ function listTextFiles(dir: string): string[] {** Skips files that:* - aren't `.txt`- * - have date-prefixed basenames outside the requested window+ * - have a date (basename prefix, else frontmatter `date`, #7267) outside the requested window* - have content shorter than `minChars`* - are gbrain claude-cli self-captures (`selfCaptureSessionIds`) or sit under a* dream output directory (`excludeDirs`); each class is counted in one stderr line@@ -308,9 +327,8 @@ export function discoverTranscripts(opts: DiscoverOpts): DiscoveredTranscript[]for (const filePath of listTextFiles(dir)) {const ext = filePath.endsWith('.md') ? '.md' : '.txt';const baseName = transcriptBaseName(filePath, ext);- const dateMatch = DATE_RE.exec(baseName);- const inferredDate = dateMatch ? dateMatch[1] : null;- if (!isInDateRange(inferredDate, opts)) continue;+ const nameDate = DATE_RE.exec(baseName)?.[1] ?? null;+ if (nameDate && !isInDateRange(nameDate, opts)) continue;if (ext === '.txt' && opts.selfCaptureSessionIds?.has(corpusFileSessionId(basename(filePath)))) {selfCaptureSkips += 1;continue;@@ -332,6 +350,8 @@ export function discoverTranscripts(opts: DiscoverOpts): DiscoveredTranscript[]// #6091: turns captured under memory.auto_writeback off never feed synthesis.if (ext === '.txt') content = withoutOffPeriodTurns(filePath, content);if (content === null || content.length < minChars) continue;+ const inferredDate = nameDate ?? frontmatterDay(transcriptFrontmatter(content));+ if (!isInDateRange(inferredDate, opts)) continue;if (isDreamOutput(content, bypass)) {process.stderr.write(`[dream] skipped ${baseName}: dream_generated marker (self-consumption guard)\n`);continue;@@ -400,14 +420,13 @@ export function readSingleTranscript(}const ext = filePath.endsWith('.md') ? '.md' : '.txt';const baseName = transcriptBaseName(filePath, ext);- const dateMatch = DATE_RE.exec(baseName);const seat = transcriptSeat(filePath);return {filePath,contentHash: hashContent(content),content,basename: baseName,- inferredDate: dateMatch ? dateMatch[1] : null,+ inferredDate: DATE_RE.exec(baseName)?.[1] ?? frontmatterDay(transcriptFrontmatter(content)),...(seat ? { seat } : {}),};}@@ -491,8 +510,7 @@ function frontmatterOf(row: ConversationRow): Record<string, unknown> {}function pageDate(fm: Record<string, unknown>, slug: string): string | null {- const raw = fm.date instanceof Date ? fm.date.toISOString() : typeof fm.date === 'string' ? fm.date : '';- return DATE_RE.exec(raw)?.[1] ?? DATE_RE.exec(slug.split('/').pop() ?? '')?.[1] ?? null;+ return frontmatterDay(fm) ?? DATE_RE.exec(slug.split('/').pop() ?? '')?.[1] ?? null;}/** Live conversation pages in one source (used for the not-consumed warning). */diff --git a/test/transcript-discovery-frontmatter-date.test.ts b/test/transcript-discovery-frontmatter-date.test.tsnew file mode 100644index 0000000000..f6d039f774--- /dev/null+++ b/test/transcript-discovery-frontmatter-date.test.ts@@ -0,0 +1,57 @@+/**+ * #7267: a transcript whose filename has no leading YYYY-MM-DD is dated by+ * its frontmatter `date:`, so synthesis names and dates its notes after the+ * day the conversation happened instead of the day the cycle ran, and the+ * --date/--from/--to window sees that date. A filename date still wins, and+ * an impossible calendar day or unreadable frontmatter counts as no date.+ */+import { afterEach, beforeEach, describe, expect, test } from 'bun:test';+import { mkdtempSync, rmSync, writeFileSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';+import { discoverTranscripts, readSingleTranscript } from '../src/core/cycle/transcript-discovery.ts';++const body = 'alice-example and bob-example review the launch checklist. '.repeat(60);+let dir: string;++beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'gbrain-transcript-fm-date-')); });+afterEach(() => { rmSync(dir, { recursive: true, force: true }); });++function dated(): Map<string, string | null> {+ return new Map(discoverTranscripts({ corpusDir: dir, minChars: 1000 }).map(t => [t.basename, t.inferredDate]));+}++describe('#7267 transcript date from frontmatter', () => {+ test('an undated filename takes the frontmatter date, in discovery and --input', () => {+ const file = join(dir, 'Weekly sync.md');+ writeFileSync(file, `---\ndate: 2026-08-13\n---\n${body}`);+ writeFileSync(join(dir, 'Quoted date.md'), `---\ntitle: Planning\ndate: '2026-08-14T15:00:00Z'\n---\n${body}`);+ expect(dated().get('Weekly sync')).toBe('2026-08-13');+ expect(dated().get('Quoted date')).toBe('2026-08-14');+ expect(readSingleTranscript(file, { minChars: 1000 })?.inferredDate).toBe('2026-08-13');+ });++ test('a filename date wins over the frontmatter date', () => {+ writeFileSync(join(dir, '2026-09-01-standup.md'), `---\ndate: 2026-08-13\n---\n${body}`);+ expect(dated().get('2026-09-01-standup')).toBe('2026-09-01');+ });++ test('no date, an impossible day, a body-only date line or malformed frontmatter stays undated', () => {+ writeFileSync(join(dir, 'No date.txt'), body);+ writeFileSync(join(dir, 'Impossible day.md'), `---\ndate: 2026-02-30\n---\n${body}`);+ writeFileSync(join(dir, 'Body line.md'), `---\ntitle: Notes\n---\ndate: 2026-08-13\n${body}`);+ writeFileSync(join(dir, 'Malformed.md'), `---\ndate: [unclosed\n---\n${body}`);+ writeFileSync(join(dir, 'Header.txt'), `--- BEGIN TRANSCRIPT\n${body}`);+ expect([...dated().entries()].sort()).toEqual([+ ['Body line', null], ['Header', null], ['Impossible day', null], ['Malformed', null], ['No date', null],+ ]);+ });++ test('the date window filters on the frontmatter date', () => {+ writeFileSync(join(dir, 'Weekly sync.md'), `---\ndate: 2026-08-13\n---\n${body}`);+ writeFileSync(join(dir, 'Undated.md'), body);+ expect(discoverTranscripts({ corpusDir: dir, minChars: 1000, date: '2026-08-13' }).map(t => t.basename)).toEqual(['Weekly sync']);+ expect(discoverTranscripts({ corpusDir: dir, minChars: 1000, from: '2026-08-01', to: '2026-08-31' }).map(t => t.basename)).toEqual(['Weekly sync']);+ expect(discoverTranscripts({ corpusDir: dir, minChars: 1000, from: '2026-09-01' })).toEqual([]);+ });+});