From 3b148687c9a1008d37ea84b0c128532e8af0a051 Mon Sep 17 00:00:00 2001 From: Alfabete-spec <196493132+Alfabete-spec@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:05:08 +0800 Subject: [PATCH] fix: preserve daily log entry headers when truncating context Signed-off-by: Alfabete-spec <196493132+Alfabete-spec@users.noreply.github.com> --- index.ts | 63 ++++++++++++++++++++++++++++++++++++----------- test/unit.test.ts | 14 +++++++++++ 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 2f27901..8637d29 100644 --- a/index.ts +++ b/index.ts @@ -286,6 +286,10 @@ function formatPreviewBlock(label: string, content: string, mode: TruncateMode) function formatContextSection(label: string, content: string, mode: TruncateMode, maxLines: number, maxChars: number) { const result = buildPreview(content, { maxLines, maxChars, mode }); + return formatContextPreview(label, result); +} + +function formatContextPreview(label: string, result: PreviewResult) { if (!result.preview) { return ""; } @@ -295,6 +299,49 @@ function formatContextSection(label: string, content: string, mode: TruncateMode return `${label}\n\n${result.preview}${note}`; } +function formatDailyLogSection(label: string, content: string) { + const normalized = normalizeContent(content).replace(/\r\n/g, "\n"); + const limits = { maxLines: CONTEXT_DAILY_MAX_LINES, maxChars: CONTEXT_DAILY_MAX_CHARS, mode: "end" as const }; + const result = buildPreview(normalized, limits); + if (result.truncated) { + const cut = normalized.length - result.preview.length; + // Find the entry containing the start of the retained tail. + const headers = normalized.matchAll( + /^[ \t]*(?:\n## [^\n]+)?/gm, + ); + let header: RegExpMatchArray | undefined; + let nextHeaderIndex = normalized.length; + for (const candidate of headers) { + if (candidate.index >= cut) { + nextHeaderIndex = candidate.index; + break; + } + header = candidate; + } + + if (header?.index !== undefined) { + const prefix = `${header[0]}\n\n`; + const maxChars = limits.maxChars - prefix.length; + if (maxChars > 0) { + // Reserve space for the header within the existing line and character budgets. + const tail = buildPreview(normalized.slice(header.index + header[0].length), { + ...limits, + maxLines: limits.maxLines - (prefix.split("\n").length - 1), + maxChars, + }); + // If reserving space dropped the entire entry, the tail starts in a newer one. + const tailStart = normalized.length - tail.preview.length; + if (tail.preview && tailStart < nextHeaderIndex) { + result.preview = prefix + tail.preview; + result.previewChars = result.preview.length; + result.previewLines = result.preview.split("\n").length; + } + } + } + } + return formatContextPreview(label, result); +} + type ExitSummaryReason = "ctrl+d" | "slash-quit" | "session-end"; interface ExitSummaryResult { @@ -822,13 +869,7 @@ export function buildMemoryContext(searchResults?: string): string { const todayContent = readFileSafe(dailyPath(today)); if (todayContent?.trim()) { - const section = formatContextSection( - `## Daily log: ${today} (today)`, - todayContent, - "end", - CONTEXT_DAILY_MAX_LINES, - CONTEXT_DAILY_MAX_CHARS, - ); + const section = formatDailyLogSection(`## Daily log: ${today} (today)`, todayContent); if (section) sections.push(section); } @@ -857,13 +898,7 @@ export function buildMemoryContext(searchResults?: string): string { const yesterdayContent = readFileSafe(dailyPath(yesterday)); if (yesterdayContent?.trim()) { - const section = formatContextSection( - `## Daily log: ${yesterday} (yesterday)`, - yesterdayContent, - "end", - CONTEXT_DAILY_MAX_LINES, - CONTEXT_DAILY_MAX_CHARS, - ); + const section = formatDailyLogSection(`## Daily log: ${yesterday} (yesterday)`, yesterdayContent); if (section) sections.push(section); } diff --git a/test/unit.test.ts b/test/unit.test.ts index 5d7725b..1dd47ed 100644 --- a/test/unit.test.ts +++ b/test/unit.test.ts @@ -603,6 +603,20 @@ describe("buildMemoryContext", () => { expect(ctx).toContain("Yesterday's work"); }); + test("preserves the newest entry header when truncating a daily log", () => { + ensureDirs(); + const today = todayStr(); + const header = `\n## Latest work`; + const content = `## Earlier work\n\n${header}\n\n${"old detail\n".repeat(400)}\n- latest detail`; + fs.writeFileSync(path.join(tmpDir, "daily", `${today}.md`), content, "utf-8"); + + const ctx = buildMemoryContext(); + expect(ctx).toContain(header); + expect(ctx).toContain("- latest detail"); + expect(ctx).not.toContain("## Earlier work"); + expect(ctx).toContain("[truncated:"); + }); + test("combines all sections with separators", () => { ensureDirs(); fs.writeFileSync(path.join(tmpDir, "MEMORY.md"), "Memory content", "utf-8");