From 1ee3cba00a2aed43fa39238c701f4c367aaca2f2 Mon Sep 17 00:00:00 2001 From: Tarik Moody <81526818+tmoody1973@users.noreply.github.com> Date: Fri, 16 Oct 2026 17:35:12 -0500 Subject: [PATCH 1/2] fix(sync): honor --recent-days and --since for facts and todos Facts and todos were always fetched in full, so --recent-days and --since were silently ignored for them (#14). The list endpoints take no date filter, so bound them client-side by created_at and stop paging once a newest-first page reaches the cutoff. Pages that are not newest-first keep paging and are filtered, so results stay complete. Refs #14 --- sources/commands/sync/index.test.ts | 116 ++++++++++++++++++++++++++++ sources/commands/sync/index.ts | 82 ++++++++++++++++---- 2 files changed, 184 insertions(+), 14 deletions(-) diff --git a/sources/commands/sync/index.test.ts b/sources/commands/sync/index.test.ts index 6b885b0..0ed93f0 100644 --- a/sources/commands/sync/index.test.ts +++ b/sources/commands/sync/index.test.ts @@ -957,3 +957,119 @@ describe("sync regression", () => { expect(process.exitCode).toBe(1); }); }); + +describe("sync facts/todos honor --recent-days and --since (#14)", () => { + const DAY = 86_400_000; + + function fact(id: number, text: string, createdAt: number) { + return { id, text, tags: [], created_at: createdAt, confirmed: true }; + } + + function todo(id: number, text: string, createdAt: number) { + return { id, text, alarm_at: null, completed: false, created_at: createdAt }; + } + + it("createdAfterCutoff reads --since as epoch ms or a v1 cursor, else --recent-days", () => { + const { createdAfterCutoff, parseSyncArgs } = __testing; + expect(createdAfterCutoff(parseSyncArgs(["--since", "1700000000000"]))).toBe(1_700_000_000_000); + expect(createdAfterCutoff(parseSyncArgs(["--since", "v1-1700000000000"]))).toBe(1_700_000_000_000); + expect(createdAfterCutoff(parseSyncArgs([]))).toBeUndefined(); + const today = `${new Date().toISOString().slice(0, 10)}T00:00:00Z`; + expect(createdAfterCutoff(parseSyncArgs(["--recent-days", "1"]))).toBe(Date.parse(today)); + }); + + it("--recent-days keeps recent facts and stops paging once a newest-first page is older", async () => { + const now = Date.now(); + const outputDir = await makeTempDir(); + const { context, requests } = proxyContext((request) => { + const url = new URL(request.url); + if (url.pathname !== "/v1/facts") return Response.json({}); + const cursor = url.searchParams.get("cursor"); + if (cursor === null) { + return Response.json({ + facts: [fact(3, "fresh fact", now), fact(2, "day-old fact", now - DAY)], + next_cursor: "v1-2", + }); + } + if (cursor === "v1-2") { + return Response.json({ facts: [fact(1, "ancient fact", now - 10 * DAY)], next_cursor: "v1-1" }); + } + return Response.json({ facts: [fact(0, "never fetched", now - 20 * DAY)], next_cursor: null }); + }); + + await syncCommand.run(["--output", outputDir, "--only", "facts", "--recent-days", "3"], context); + + const md = readFileSync(path.join(outputDir, "facts.md"), "utf8"); + expect(md).toContain("fresh fact"); + expect(md).toContain("day-old fact"); + expect(md).not.toContain("ancient fact"); + expect(md).not.toContain("never fetched"); + expect(requests.filter((r) => r.pathname === "/v1/facts")).toHaveLength(2); + }); + + it("--since (epoch ms) bounds todos and stops after the first older page", async () => { + const now = Date.now(); + const outputDir = await makeTempDir(); + const { context, requests } = proxyContext((request) => { + const url = new URL(request.url); + if (url.pathname !== "/v1/todos") return Response.json({}); + return Response.json({ + todos: [todo(9, "new todo", now), todo(8, "old todo", now - 10 * DAY)], + next_cursor: "v1-8", + }); + }); + + await syncCommand.run( + ["--output", outputDir, "--only", "todos", "--since", String(now - 5 * DAY)], + context + ); + + const md = readFileSync(path.join(outputDir, "todos.md"), "utf8"); + expect(md).toContain("new todo"); + expect(md).not.toContain("old todo"); + expect(requests.filter((r) => r.pathname === "/v1/todos")).toHaveLength(1); + }); + + it("pages that are not newest-first keep paging and are still filtered", async () => { + const now = Date.now(); + const outputDir = await makeTempDir(); + const { context, requests } = proxyContext((request) => { + const url = new URL(request.url); + if (url.pathname !== "/v1/facts") return Response.json({}); + if (url.searchParams.get("cursor") === null) { + return Response.json({ + facts: [fact(1, "old first", now - 10 * DAY), fact(2, "fresh second", now)], + next_cursor: "v1-5", + }); + } + return Response.json({ facts: [fact(3, "fresh third", now - 3_600_000)], next_cursor: null }); + }); + + await syncCommand.run(["--output", outputDir, "--only", "facts", "--recent-days", "3"], context); + + const md = readFileSync(path.join(outputDir, "facts.md"), "utf8"); + expect(md).toContain("fresh second"); + expect(md).toContain("fresh third"); + expect(md).not.toContain("old first"); + expect(requests.filter((r) => r.pathname === "/v1/facts")).toHaveLength(2); + }); + + it("without --recent-days or --since every fact is still written", async () => { + const now = Date.now(); + const outputDir = await makeTempDir(); + const { context } = proxyContext((request) => { + const url = new URL(request.url); + if (url.pathname !== "/v1/facts") return Response.json({}); + return Response.json({ + facts: [fact(2, "fresh fact", now), fact(1, "ancient fact", now - 400 * DAY)], + next_cursor: null, + }); + }); + + await syncCommand.run(["--output", outputDir, "--only", "facts"], context); + + const md = readFileSync(path.join(outputDir, "facts.md"), "utf8"); + expect(md).toContain("fresh fact"); + expect(md).toContain("ancient fact"); + }); +}); diff --git a/sources/commands/sync/index.ts b/sources/commands/sync/index.ts index fb2ee62..ee7c30a 100644 --- a/sources/commands/sync/index.ts +++ b/sources/commands/sync/index.ts @@ -411,6 +411,51 @@ function recentDaysFrom(days: number): string { return start.toISOString().slice(0, 10); } +// Facts and todos are always re-fetched in full and their list endpoints take no +// date filter, so --since / --recent-days bound them client-side by created_at. +function createdAfterCutoff(options: SyncOptions): number | undefined { + if (options.since !== undefined) { + if (/^\d+$/.test(options.since)) { + return Number(options.since); + } + const fromCursor = cursorEpochMs(options.since); + if (fromCursor !== null) { + return fromCursor; + } + } + if (options.recentDays !== undefined) { + return Date.parse(`${recentDaysFrom(options.recentDays)}T00:00:00Z`); + } + return undefined; +} + +function keepCreatedSince( + items: T[], + cutoff: number | undefined +): T[] { + return cutoff === undefined ? items : items.filter((item) => item.created_at >= cutoff); +} + +// Stop paging once a newest-first page reaches the cutoff. A page that is not +// newest-first never stops paging; the filter alone keeps the result correct. +function pageEndsBefore( + items: { created_at: number }[], + cutoff: number | undefined +): boolean { + const last = items[items.length - 1]; + if (cutoff === undefined || last === undefined) { + return false; + } + for (let i = 1; i < items.length; i += 1) { + const previous = items[i - 1]; + const current = items[i]; + if (previous !== undefined && current !== undefined && previous.created_at < current.created_at) { + return false; + } + } + return last.created_at < cutoff; +} + function parseTargets(value: string): SyncTarget[] { const parts = value .split(",") @@ -721,14 +766,16 @@ async function syncAll( const syncPromises: Promise[] = []; const outcomes: Partial> = {}; + const createdAfter = createdAfterCutoff(options); + if (options.targets.has("facts")) { const task = progress.addTask("facts"); - syncPromises.push(syncFacts(context, options.outputDir, task)); + syncPromises.push(syncFacts(context, options.outputDir, task, createdAfter)); } if (options.targets.has("todos")) { const task = progress.addTask("todos"); - syncPromises.push(syncTodos(context, options.outputDir, task)); + syncPromises.push(syncTodos(context, options.outputDir, task, createdAfter)); } if (options.targets.has("daily")) { @@ -959,9 +1006,10 @@ async function persistManifest( async function syncFacts( context: CommandContext, outputDir: string, - task: ProgressTask + task: ProgressTask, + createdAfter?: number ): Promise { - const facts = await fetchAllFacts(context, task); + const facts = await fetchAllFacts(context, task, createdAfter); await writeFactsMarkdown(outputDir, facts); task.setLabel("facts done"); task.complete(); @@ -970,9 +1018,10 @@ async function syncFacts( async function syncTodos( context: CommandContext, outputDir: string, - task: ProgressTask + task: ProgressTask, + createdAfter?: number ): Promise { - const todos = await fetchAllTodos(context, task); + const todos = await fetchAllTodos(context, task, createdAfter); await writeTodosMarkdown(outputDir, todos); task.setLabel("todos done"); task.complete(); @@ -1250,7 +1299,8 @@ function conversationSortKey(conversation: ConversationSummary): number { async function fetchAllFacts( context: CommandContext, - task: ProgressTask + task: ProgressTask, + createdAfter?: number ): Promise { const items: Fact[] = []; let cursor: string | undefined; @@ -1265,9 +1315,10 @@ async function fetchAllFacts( const apiPath = params.toString() ? `/v1/facts?${params}` : "/v1/facts"; const data = await requestClientJson(context, apiPath, { method: "GET" }); const payload = parseFactsList(data); - items.push(...payload.facts); - task.advance(payload.facts.length); - if (!payload.next_cursor) { + const kept = keepCreatedSince(payload.facts, createdAfter); + items.push(...kept); + task.advance(kept.length); + if (!payload.next_cursor || pageEndsBefore(payload.facts, createdAfter)) { break; } task.addTotal(1); @@ -1279,7 +1330,8 @@ async function fetchAllFacts( async function fetchAllTodos( context: CommandContext, - task: ProgressTask + task: ProgressTask, + createdAfter?: number ): Promise { const items: Todo[] = []; let cursor: string | undefined; @@ -1294,9 +1346,10 @@ async function fetchAllTodos( const apiPath = params.toString() ? `/v1/todos?${params}` : "/v1/todos"; const data = await requestClientJson(context, apiPath, { method: "GET" }); const payload = parseTodosList(data); - items.push(...payload.todos); - task.advance(payload.todos.length); - if (!payload.next_cursor) { + const kept = keepCreatedSince(payload.todos, createdAfter); + items.push(...kept); + task.advance(kept.length); + if (!payload.next_cursor || pageEndsBefore(payload.todos, createdAfter)) { break; } task.addTotal(1); @@ -1820,5 +1873,6 @@ export const __testing = { persistCursor, accountFingerprint, normalizeSinceCursor, + createdAfterCutoff, CHANGES_OVERLAP_MS, }; From 6ace901a3c18362da9716369e5bd0e98fde820c1 Mon Sep 17 00:00:00 2001 From: Tarik Moody <81526818+tmoody1973@users.noreply.github.com> Date: Fri, 16 Oct 2026 18:01:33 -0500 Subject: [PATCH 2/2] docs(sync): note that --recent-days and --since limit facts and todos --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f5aa453..09f3df4 100644 --- a/README.md +++ b/README.md @@ -434,20 +434,20 @@ bee sync [--output ] [--recent-days N] [--only ` | `bee-sync` | Output directory for synced files | -| `--recent-days N` | all | Limit daily summaries and conversations to the last N days (facts and todos are always synced in full). Applies to full syncs only; ignored on an incremental run | +| `--recent-days N` | all | Limit daily summaries and conversations to the last N days; applies to full syncs only, ignored on an incremental run. Also limits facts and todos to those created in the last N days, on every run | | `--only ` | all | Limit sync to a comma-separated list: `facts`, `todos`, `daily`, `conversations` (or `all`) | | `--full` | off | Force a complete re-sync, ignoring the saved manifest, and rewrite it with fresh cursors | -| `--since ` | — | Advanced/recovery: override the saved incremental cursor with an explicit epoch-milliseconds timestamp for changefeed-driven targets | +| `--since ` | — | Advanced/recovery: override the saved incremental cursor with an explicit epoch-milliseconds timestamp for changefeed-driven targets. Also limits facts and todos to those created after it | ### Output Structure @@ -619,7 +619,7 @@ Sync only specific data: bee sync --only facts,todos ``` -Limit daily summaries and conversations to the last 7 days: +Limit daily summaries, conversations, facts and todos to the last 7 days: ```bash bee sync --recent-days 7 @@ -627,7 +627,7 @@ bee sync --recent-days 7 ### Notes -- The first sync (or `--full`) fetches everything; facts and todos are always re-fetched in full +- The first sync (or `--full`) fetches everything; facts and todos are re-fetched on every run, limited by `--recent-days` or `--since` when given (their files then hold only the matching items) - Re-runs are incremental: only daily summaries and conversations that changed since the last run are re-fetched, tracked via the `.bee-sync.json` manifest in the output directory - Conversations and daily summaries are fetched concurrently for faster syncing - All timestamps are in ISO 8601 format (UTC)