Skip to content
Merged
1 change: 1 addition & 0 deletions prompts/dream.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ You have access ONLY to filesystem tools (Read, Write, Edit, Bash, Glob, Grep).
- Add new entries where appropriate
- Keep entries concise and factual — no padding, no narrative
- Preserve all existing structure and sections
- Also write daily memory summaries to `{{dailyMemoryDir}}/YYYY-MM-DD.md` for each day of logs you processed. Include key learnings, conversation summaries, and follow-ups. Keep these concise — they are injected into conversation context.
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved

### Stage 4 — Prune

Expand Down
4 changes: 3 additions & 1 deletion prompts/heartbeat.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You have access ONLY to filesystem tools (Read, Write, Edit, Bash, Glob, Grep).
- Logs directory: `{{logsDir}}`
- Last heartbeat: `{{lastRunIso}}`
- Run number: #{{runCount}}
- Today's daily memory: `{{dailyMemoryFile}}`

## Instructions

Expand All @@ -18,7 +19,8 @@ If the instructions file does not exist or is empty, perform these default tasks

1. **Review recent logs** — Check `{{logsDir}}/` for log files dated after `{{lastRunIso}}`. If `{{lastRunIso}}` is `never`, treat it as the beginning of time and review all available logs. Extract any new facts, preferences, or notable events.
2. **Update memory** — Merge any new information into `{{memoryFile}}`, keeping entries concise and factual.
3. **Workspace hygiene** — Note any issues but do not delete files unless the instructions explicitly say to.
3. **Update daily notes** — Write today's learnings, observations, corrections, and follow-ups to `{{dailyMemoryFile}}`. Keep entries concise — this file is injected into conversation context.
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
4. **Workspace hygiene** — Note any issues but do not delete files unless the instructions explicitly say to.

## Rules

Expand Down
83 changes: 83 additions & 0 deletions src/__tests__/daily-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,89 @@ describe("daily-log", () => {
});
});

describe("cleanupOldLogs — daily memory files", () => {
const DAILY_MEM_DIR = join(
TEST_ROOT,
".talon",
"workspace",
"memory",
"daily",
);

it("deletes daily memory files older than 30 days", async () => {
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
const { cleanupOldLogs } = await import("../storage/daily-log.js");
mkdirSync(LOGS_DIR, { recursive: true });
mkdirSync(DAILY_MEM_DIR, { recursive: true });

// Create an old daily memory file (40 days ago)
const oldDate = new Date();
oldDate.setDate(oldDate.getDate() - 40);
const oldName = oldDate.toISOString().slice(0, 10) + ".md";
writeFileSync(join(DAILY_MEM_DIR, oldName), "old daily memory");

// Create a recent daily memory file (5 days ago)
const recentDate = new Date();
recentDate.setDate(recentDate.getDate() - 5);
const recentName = recentDate.toISOString().slice(0, 10) + ".md";
writeFileSync(join(DAILY_MEM_DIR, recentName), "recent daily memory");
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved

cleanupOldLogs();

const remaining = readdirSync(DAILY_MEM_DIR);
expect(remaining).not.toContain(oldName);
expect(remaining).toContain(recentName);
});

it("handles missing daily memory directory gracefully", async () => {
const { cleanupOldLogs } = await import("../storage/daily-log.js");
// Create logs dir but NOT the daily memory dir
mkdirSync(LOGS_DIR, { recursive: true });
expect(() => cleanupOldLogs()).not.toThrow();
});

it("ignores non-YYYY-MM-DD.md files in daily memory dir", async () => {
const { cleanupOldLogs } = await import("../storage/daily-log.js");
mkdirSync(LOGS_DIR, { recursive: true });
mkdirSync(DAILY_MEM_DIR, { recursive: true });

// A file that would sort before the cutoff but doesn't match the pattern
writeFileSync(join(DAILY_MEM_DIR, "2020-summary.md"), "should survive");
writeFileSync(join(DAILY_MEM_DIR, "notes.md"), "also should survive");

cleanupOldLogs();

const remaining = readdirSync(DAILY_MEM_DIR);
expect(remaining).toContain("2020-summary.md");
expect(remaining).toContain("notes.md");
});

it("ignores non-YYYY-MM-DD.md files in logs dir", async () => {
const { cleanupOldLogs } = await import("../storage/daily-log.js");
mkdirSync(LOGS_DIR, { recursive: true });

writeFileSync(join(LOGS_DIR, "2020-summary.md"), "should survive");

cleanupOldLogs();

expect(readdirSync(LOGS_DIR)).toContain("2020-summary.md");
});

it("does not delete recent daily memory files", async () => {
const { cleanupOldLogs } = await import("../storage/daily-log.js");
mkdirSync(LOGS_DIR, { recursive: true });
mkdirSync(DAILY_MEM_DIR, { recursive: true });

const recentDate = new Date();
recentDate.setDate(recentDate.getDate() - 3);
const recentName = recentDate.toISOString().slice(0, 10) + ".md";
writeFileSync(join(DAILY_MEM_DIR, recentName), "keep me");

cleanupOldLogs();

expect(readdirSync(DAILY_MEM_DIR)).toContain(recentName);
});
});

describe("appendDailyLogResponse", () => {
it("writes bot response with chat title context", async () => {
const { appendDailyLogResponse, getLogsDir } =
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/dream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -180,6 +181,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -215,6 +217,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
});
Expand Down Expand Up @@ -465,6 +468,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -510,6 +514,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -555,6 +560,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -606,6 +612,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
vi.doMock("@anthropic-ai/claude-agent-sdk", () => ({
Expand Down Expand Up @@ -649,6 +656,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
const queryMock = vi.fn(async function* () {});
Expand Down Expand Up @@ -702,6 +710,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
vi.doMock("@anthropic-ai/claude-agent-sdk", () => ({
Expand Down Expand Up @@ -737,9 +746,9 @@
}));
vi.doMock("write-file-atomic", () => ({ default: { sync: vi.fn() } }));
vi.doMock("@anthropic-ai/claude-agent-sdk", () => ({
query: vi.fn(async function* () {
throw new Error("agent crashed unexpectedly");
}),

Check warning on line 751 in src/__tests__/dream.test.ts

View workflow job for this annotation

GitHub Actions / Code Quality

eslint(require-yield)

This generator function does not have `yield`
}));
vi.doMock("../util/paths.js", () => ({
files: {
Expand All @@ -753,6 +762,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down Expand Up @@ -796,6 +806,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
vi.doMock("@anthropic-ai/claude-agent-sdk", () => ({
Expand Down Expand Up @@ -844,6 +855,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
vi.doMock("@anthropic-ai/claude-agent-sdk", () => ({
Expand Down Expand Up @@ -892,6 +904,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
const queryMock = vi.fn(async function* () {});
Expand Down Expand Up @@ -938,6 +951,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));
// query never resolves — so the 10-minute timeout wins the race
Expand Down Expand Up @@ -995,6 +1009,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
},
}));

Expand Down
1 change: 1 addition & 0 deletions src/__tests__/heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
workspace: "/fake/.talon/workspace",
data: "/fake/.talon/data",
memory: "/fake/.talon/workspace/memory",
dailyMemory: "/fake/.talon/workspace/memory/daily",
prompts: "/fake/.talon/prompts",
},
}));
Expand Down Expand Up @@ -195,9 +196,9 @@
);

// Make agent throw
queryMock.mockImplementationOnce(async function* () {
throw new Error("Agent exploded");
});

Check warning on line 201 in src/__tests__/heartbeat.test.ts

View workflow job for this annotation

GitHub Actions / Code Quality

eslint(require-yield)

This generator function does not have `yield`

await expect(forceHeartbeat()).rejects.toThrow("Agent exploded");

Expand All @@ -215,9 +216,9 @@
it("sets last_started even on failure", async () => {
existsSyncMock.mockReturnValue(false);

queryMock.mockImplementationOnce(async function* () {
throw new Error("Boom");
});

Check warning on line 221 in src/__tests__/heartbeat.test.ts

View workflow job for this annotation

GitHub Actions / Code Quality

eslint(require-yield)

This generator function does not have `yield`

await expect(forceHeartbeat()).rejects.toThrow("Boom");

Expand Down Expand Up @@ -338,9 +339,9 @@
resolveAgent = r;
});

queryMock.mockImplementationOnce(async function* () {
await agentPromise;
});

Check warning on line 344 in src/__tests__/heartbeat.test.ts

View workflow job for this annotation

GitHub Actions / Code Quality

eslint(require-yield)

This generator function does not have `yield`

const runPromise = forceHeartbeat().catch(() => {});

Expand Down
3 changes: 2 additions & 1 deletion src/core/dream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ async function runDreamAgent(lastRunTimestamp: number): Promise<string> {
.replace(/\{\{dreamStateFile\}\}/g, dreamStateFile)
.replace(/\{\{logsDir\}\}/g, logsDir)
.replace(/\{\{lastRunIso\}\}/g, lastRunIso)
.replace(/\{\{memoryFile\}\}/g, memoryFile);
.replace(/\{\{memoryFile\}\}/g, memoryFile)
.replace(/\{\{dailyMemoryDir\}\}/g, dirs.dailyMemory);
} catch {
throw new Error(`Failed to read dream prompt from ${promptPath}`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ async function runHeartbeatAgent(
const memoryFile = pathFiles.memory;
const workspace = configRef.workspace ?? dirs.workspace;
const instructionsFile = resolve(workspace, "heartbeat-instructions.md");
const dailyMemoryFile = resolve(
dirs.dailyMemory,
`${new Date().toISOString().slice(0, 10)}.md`,
);
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved

// Load prompt template from the prompts directory (seeded to ~/.talon/prompts/)
const promptPath = resolve(dirs.prompts, "heartbeat.md");
Expand All @@ -252,6 +256,7 @@ async function runHeartbeatAgent(
.replace(/\{\{lastRunIso\}\}/g, lastRunIso)
.replace(/\{\{memoryFile\}\}/g, memoryFile)
.replace(/\{\{instructionsFile\}\}/g, instructionsFile)
.replace(/\{\{dailyMemoryFile\}\}/g, dailyMemoryFile)
.replace(/\{\{runCount\}\}/g, String(runCount))
.replace(/\{\{intervalMinutes\}\}/g, String(intervalMinutesRef));
} catch {
Expand Down
32 changes: 30 additions & 2 deletions src/storage/daily-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export function getLogsDir(): string {
return LOGS_DIR;
}

/** Matches YYYY-MM-DD.md filenames strictly. */
const DAILY_FILE_RE = /^\d{4}-\d{2}-\d{2}\.md$/;

/** Remove daily logs older than MAX_LOG_DAYS. Called on startup. */
export function cleanupOldLogs(): void {
try {
Expand All @@ -101,8 +104,7 @@ export function cleanupOldLogs(): void {

Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
let deleted = 0;
for (const file of readdirSync(LOGS_DIR)) {
// Log files are named YYYY-MM-DD.md
if (file.endsWith(".md") && file < cutoffStr) {
if (DAILY_FILE_RE.test(file) && file < cutoffStr) {
try {
unlinkSync(resolve(LOGS_DIR, file));
deleted++;
Expand All @@ -117,4 +119,30 @@ export function cleanupOldLogs(): void {
} catch {
/* skip */
}

// Also clean up old daily memory files
try {
const dailyMemDir = dirs.dailyMemory;
if (!existsSync(dailyMemDir)) return;
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - MAX_LOG_DAYS);
const cutoffMem = cutoffDate.toISOString().slice(0, 10);

let deletedMem = 0;
for (const file of readdirSync(dailyMemDir)) {
if (DAILY_FILE_RE.test(file) && file < cutoffMem) {
try {
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
unlinkSync(resolve(dailyMemDir, file));
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
deletedMem++;
} catch {
/* skip */
}
}
}
if (deletedMem > 0) {
logInfo("workspace", `Cleaned up ${deletedMem} old daily memory file(s)`);
}
} catch {
/* skip */
}
}
19 changes: 19 additions & 0 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ function loadSystemPrompt(
loaded.push("memory");
}

// Inject daily memory notes (today + yesterday)
const dailyMemoryDir = dirs.dailyMemory;
const today = new Date().toISOString().slice(0, 10);
const yesterday = new Date(Date.now() - 86_400_000)
.toISOString()
.slice(0, 10);
const dailyParts: string[] = [];
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
for (const dateStr of [today, yesterday]) {
const content = readOptionalFile(resolve(dailyMemoryDir, `${dateStr}.md`));
if (content) dailyParts.push(content);
}
if (dailyParts.length > 0) {
parts.push(
`## Recent Daily Notes\n\nYour daily notes from recent days. Update today's file at \`~/.talon/workspace/memory/daily/${today}.md\` as you learn things.\n\n${dailyParts.join("\n\n---\n\n")}`,
);
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
loaded.push("daily-memory");
}

const loadedKey = loaded.join(" + ");
if (loadedKey && loadedKey !== lastLoggedPromptKey) {
log("config", `System prompt: ${loadedKey}`);
Expand Down Expand Up @@ -217,6 +235,7 @@ function loadSystemPrompt(

You have a workspace directory at \`~/.talon/workspace/\`. This is your home — organize it however you want.
- \`~/.talon/workspace/memory/memory.md\` is your persistent memory file. Update it when you learn important things.
- \`~/.talon/workspace/memory/daily/YYYY-MM-DD.md\` is your daily notes file. Write observations, learnings, corrections, and follow-ups here throughout the day. Keep entries concise.
- Daily interaction logs are saved to \`~/.talon/workspace/logs/\` automatically.
- Files users send you (photos, docs, voice) are saved to \`~/.talon/workspace/uploads/\`.
- Persistent cron jobs are managed via the cron tools.
Expand Down
2 changes: 2 additions & 0 deletions src/util/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const dirs = {
logs: resolve(TALON_ROOT, "workspace", "logs"),
/** Memory: ~/.talon/workspace/memory/ */
memory: resolve(TALON_ROOT, "workspace", "memory"),
/** Daily memory notes: ~/.talon/workspace/memory/daily/ */
dailyMemory: resolve(TALON_ROOT, "workspace", "memory", "daily"),
/** Sticker packs: ~/.talon/workspace/stickers/ */
Comment thread
dylanneve1 marked this conversation as resolved.
Resolved
stickers: resolve(TALON_ROOT, "workspace", "stickers"),
/** Prompt files: ~/.talon/prompts/ */
Expand Down
1 change: 1 addition & 0 deletions src/util/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export function initWorkspace(root: string): void {
// Ensure subdirectories exist
for (const sub of [
dirs.memory,
dirs.dailyMemory,
dirs.uploads,
dirs.logs,
dirs.stickers,
Expand Down
Loading