Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(markdown): a body's own dated timeline section round-trips; enrich keeps its write-up
garrytan676fd0747b84
diff --git a/src/core/markdown.ts b/src/core/markdown.tsindex cc5069efdd..71bf41a28f 100644--- a/src/core/markdown.ts+++ b/src/core/markdown.ts@@ -1007,6 +1007,9 @@ export function bareTimelineSectionLine(raw: string, datedBullets: number): 'endreturn 'other';}+/** #7738: a timeline-less body whose own dated '## Timeline …' section the bare-section fallback would split off; it ends with an empty sentinel. */+export const needsEmptyTimelineSentinel = (body: string): boolean => { const lines = body.split('\n'); return findTimelineSplitIndex(lines) === -1 && findBareTimelineSection(lines) !== null; };+/*** Serialize a page back to markdown format.* Produces: frontmatter + compiled_truth + --- + timeline@@ -1029,10 +1032,8 @@ export function serializeMarkdown(const yamlContent = matter.stringify('', fullFrontmatter).trim();- let body = compiled_truth;- if (timeline) {- body += '\n\n<!-- timeline -->\n\n' + timeline;- }+ const body = compiled_truth+ + (timeline ? '\n\n<!-- timeline -->\n\n' + timeline : needsEmptyTimelineSentinel(compiled_truth) ? '\n\n<!-- timeline -->' : '');return yamlContent + '\n\n' + body + '\n';}diff --git a/src/core/persistence/page-edit.ts b/src/core/persistence/page-edit.tsindex 64ee7e685c..e8f692f3db 100644--- a/src/core/persistence/page-edit.ts+++ b/src/core/persistence/page-edit.ts@@ -18,7 +18,7 @@ import { FACTS_FENCE_BEGIN, FACTS_FENCE_END } from '../facts-fence.ts';import { TAKES_FENCE_BEGIN, TAKES_FENCE_END } from '../takes-fence.ts';import { sanitizeRemoteBody } from '../remote-body.ts';import { sanitizeText } from '../batch-rows.ts';-import { serializePageToMarkdown } from '../markdown.ts';+import { needsEmptyTimelineSentinel, serializePageToMarkdown } from '../markdown.ts';import { insertNoNewlineMarkers, splitLines, unifiedDiff } from '../skillpack/diff-text.ts';import { redactRetrievalOutput } from '../search/output-redaction.ts';import { scanCanonicalFences, targetFenceRefusal } from '../fence-repair/refusal.ts';@@ -164,7 +164,7 @@ function pageSegments(page: Page, tags: string[], remote: boolean): Segment[] {// A remote view whose whole timeline is protected has no sentinel either.const sentinelSegment = timeline.some(segment => segment.view.length > 0) ? text(sentinel) : { canonical: sentinel, view: '', editable: false };const pieces = [text(head), ...bodySegments(page.compiled_truth ?? '', remote, segmented),- ...(page.timeline ? [sentinelSegment, ...timeline] : []), text('\n')];+ ...(page.timeline ? [sentinelSegment, ...timeline] : needsEmptyTimelineSentinel(page.compiled_truth ?? '') ? [text('\n\n<!-- timeline -->')] : []), text('\n')];return pieces.reduce<Segment[]>((out, piece) => {const last = out[out.length - 1];if (last?.editable && piece.editable) out[out.length - 1] = text(last.canonical + piece.canonical);diff --git a/src/core/timeline-write-through.ts b/src/core/timeline-write-through.tsindex 0c60bd3723..b9faeb2e8c 100644--- a/src/core/timeline-write-through.ts+++ b/src/core/timeline-write-through.ts@@ -248,6 +248,8 @@ export function spliceTimelineIntoFileText(fileText: string, date: string, blockif (m) bullets.push({ index: i, date: m[1] });}+ // #7738: a body that ends with an empty sentinel gets the same headed section spliceTimelineBlock('') writes.+ if (lines.slice(sentinel + 1).every(line => !line.trim())) return `${lines.slice(0, sentinel + 1).join('\n')}\n\n## Timeline\n\n${block}\n`;let insertAt: number;if (bullets.length === 0) {// Empty timeline section (possibly followed by other sections): insertdiff --git a/test/markdown-own-timeline-section-7738.test.ts b/test/markdown-own-timeline-section-7738.test.tsnew file mode 100644index 0000000000..75282281ee--- /dev/null+++ b/test/markdown-own-timeline-section-7738.test.ts@@ -0,0 +1,68 @@+/**+ * #7738 — a compiled body that carries its own dated '## Timeline …' section+ * (enrich's person write-up asks for '## Timeline highlights') must round-trip+ * through serializeMarkdown/parseMarkdown unchanged when the page has no+ * timeline. Without a sentinel the bare-section fallback re-read that section+ * as the page timeline, so enrich refused its own paid write-up as lossy.+ * Bodies that never split keep their exact bytes.+ */+import { describe, test, expect } from 'bun:test';+import { parseMarkdown, serializeMarkdown } from '../src/core/markdown.ts';+import { preserveCanonicalFences } from '../src/core/cycle/concept-publication.ts';+import { editableView } from '../src/core/persistence/page-edit.ts';+import { spliceTimelineBlock, spliceTimelineIntoFileText } from '../src/core/timeline-write-through.ts';+import { serializePageToMarkdown } from '../src/core/markdown.ts';++const meta = { type: 'person' as const, title: 'Alice Example', tags: [] as string[] };++function roundTrip(compiled: string, timeline: string) {+ const parsed = parseMarkdown(serializeMarkdown({}, compiled, timeline, meta), 'people/alice-example.md');+ return { compiled_truth: parsed.compiled_truth, timeline: parsed.timeline };+}++describe('a body with its own dated timeline section round-trips (#7738)', () => {+ const body = '## Overview\nAlice Example works at Acme Example.\n\n## Timeline highlights\n- 2024-01-01: joined Acme Example\n- 2025-03-02: promoted';++ test('no page timeline: the section stays in compiled_truth', () => {+ expect(roundTrip(body, '')).toEqual({ compiled_truth: body, timeline: '' });+ });++ test('with a page timeline: both halves survive', () => {+ expect(roundTrip(body, '- 2026-01-01: met')).toEqual({ compiled_truth: body, timeline: '- 2026-01-01: met' });+ });++ test('a later unrelated section after the dated one also stays', () => {+ const withTail = body + '\n\n## Notes\nprose';+ expect(roundTrip(withTail, '')).toEqual({ compiled_truth: withTail, timeline: '' });+ });++ test('a body that would not split serializes byte for byte as before', () => {+ const plain = '## Overview\nprose\n\n## History\nThe company was founded long ago, in prose.';+ const out = serializeMarkdown({}, plain, '', meta);+ expect(out).not.toContain('<!-- timeline -->');+ expect(out.endsWith(plain + '\n')).toBe(true);+ expect(roundTrip(plain, '')).toEqual({ compiled_truth: plain, timeline: '' });+ });++ test('enrich keeps a write-up whose own section is dated instead of refusing it as lossy', () => {+ const page = { compiled_truth: 'Alice Example is a stub.', timeline: '', body_storage: 'inline' } as any;+ expect(preserveCanonicalFences(page, body)).toBe(body);+ });+test('edit_page sees the same bytes get_page serves, sentinel included', () => {+ const page = { slug: 'people/alice-example', source_id: 'default', type: 'person', title: 'Alice Example', compiled_truth: body, timeline: '', frontmatter: {} } as any;+ expect(editableView(page, [], false)).toBe(serializePageToMarkdown(page, []));+ });++ test('the first timeline write after such a body agrees between file and database', () => {+ const file = serializeMarkdown({}, body, '', meta);+ const block = '- **2026-05-01** | met';+ const written = parseMarkdown(spliceTimelineIntoFileText(file, '2026-05-01', block), 'people/alice-example.md');+ expect(written.compiled_truth).toBe(body);+ expect(written.timeline).toBe(spliceTimelineBlock('', '2026-05-01', block).trim());+ });++ test('a body split by its own --- and ## Timeline gets no stray marker', () => {+ const dashed = 'Intro prose.\n\n---\n\n## Timeline\n- 2024-01-01: x';+ expect(serializeMarkdown({}, dashed, '', meta)).not.toContain('<!-- timeline -->');+ });+});