Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
Expand All @@ -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(
/^<!-- \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \[[^\]\n]+\] -->[ \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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<!-- ${today} 23:31:54 [01a197ba] -->\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");
Expand Down