From ed57a31cfc0b262c02101ba9de9a60357c27cd71 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:15:48 +0000 Subject: [PATCH] Digest: what changed in the last days, printed locally personakit digest [--days 7] [--json] lists new memories, what decay archived, what awaits review and read counts. It prints memory ids and text, never quotes. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_014dGcvUc3LqzKojYnqVGCjK --- README.md | 4 +++- docs/ARCHITECTURE.md | 3 +++ docs/PRIVACY.md | 2 ++ docs/REVIEW-LOG.md | 1 + personakit/cli.py | 15 ++++++++++++- personakit/digest.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ personakit/store.py | 5 +++-- tests/test_cli.py | 6 ++++++ tests/test_digest.py | 47 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 personakit/digest.py create mode 100644 tests/test_digest.py diff --git a/README.md b/README.md index d5b94a8..24a5276 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,12 @@ personakit list # see what personakit stats # counts only, never memory text personakit tidy # plan: archive unconfirmed memories unseen for 90 days personakit tidy --apply # carry the plan out (reverse any with `personakit undo `) +personakit digest # what changed in the last 7 days (local, prints memory text) personakit serve # MCP server over stdio for your assistants personakit forget # delete a memory and its quotes for good ``` -`sweep`, `list`, `stats` and `tidy` also take `--json` for stable, sorted machine-readable output. +`sweep`, `list`, `stats`, `tidy` and `digest` also take `--json` for stable, sorted machine-readable output. The model name is always yours to choose. Nothing leaves your machine except the turns you wrote, sent to the model you named. See [docs/PRIVACY.md](docs/PRIVACY.md). @@ -140,6 +141,7 @@ Stated plainly, so you know what you are installing: | Codex sessions | Available in 0.1 | | Any MCP client (read and propose) | Available in 0.1 | | Decay of stale unconfirmed memories (reversible) | Available in 0.2 | +| Digest of the last days | Available in 0.2 | | ChatGPT, claude.ai and Gemini export import | Planned for 0.3 | ## Documentation diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 543e2c2..f03b3f7 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -18,6 +18,7 @@ flowchart TB MCP[mcp_server] SS[stats: local counts] TD[tidy: decay, on request] + DG[digest: last days, local] end CC --> IN CX --> IN @@ -28,6 +29,7 @@ flowchart TB ST --> MCP ST --> SS ST <--> TD + ST --> DG MCP --> AS[Assistants] ``` @@ -46,6 +48,7 @@ flowchart TB | `mcp_server` | Two tools for assistants: `personakit_read` and `personakit_propose`. Holds no read-policy constants of its own. Assistants cannot approve, restrict, hide or forget; only the owner can, in `review`. | `store`, `retrieve`, `extract` | | `decay` | Finds unconfirmed memories not seen again for a long time (90 days by default). Pure functions. | `model` | | `tidy` | Turns `decay` into a plan (read-only), applies it only on request, and reverses single steps. | `decay`, `store` | +| `digest` | A summary of the last days: new and archived memories, what awaits review, read counts. Prints memory text, never quotes, to your terminal only. | `store`, `decay` | | `stats` | Counts only (memories by state, sessions extracted, reads and how many returned something). Never content. | `store` | | `cli` | Argument parsing and wiring; no logic. | all | diff --git a/docs/PRIVACY.md b/docs/PRIVACY.md index 3ed5de5..1bef935 100644 --- a/docs/PRIVACY.md +++ b/docs/PRIVACY.md @@ -19,3 +19,5 @@ - **Tidy and undo:** `personakit tidy` reads your store and, only with `--apply`, archives stale unconfirmed memories. `personakit undo` brings one back. Nothing leaves your machine, nothing here calls a model, and assistants cannot archive or restore. +- **Digest:** `personakit digest` prints memory ids and text (never evidence quotes) to your + terminal. Unlike `stats` it is not counts-only, so look before pasting it anywhere. diff --git a/docs/REVIEW-LOG.md b/docs/REVIEW-LOG.md index 4f949c0..aec16f6 100644 --- a/docs/REVIEW-LOG.md +++ b/docs/REVIEW-LOG.md @@ -27,3 +27,4 @@ duplicated logic. Each entry records the result. | `tests/*` (new) | 2026-11-08 | invented data; a key-shaped string is assembled at runtime | clean | | `personakit/decay.py`, `tidy.py`; `store.py`, `model.py`, `mcp_server.py`, `cli.py` (changes) | 2026-11-14 | none; migration 3 adds `last_seen_at`; an assistant proposal cannot revive or refresh a memory. Checked on a copy of a real store: upgrade, archive, undo and reads behave as documented | clean | | `tests/test_decay.py`, `test_store_decay.py`, `test_tidy.py`; `test_cli.py`, `test_mcp_server.py` (changes) | 2026-11-14 | invented sentences only | clean | +| `personakit/digest.py`, `tests/test_digest.py`; `store.py`, `cli.py` (changes) | 2026-11-14 | none; local output only, text without quotes; invented test data | clean | diff --git a/personakit/cli.py b/personakit/cli.py index dd589ae..97d1694 100644 --- a/personakit/cli.py +++ b/personakit/cli.py @@ -8,7 +8,7 @@ import sys from pathlib import Path -from personakit import decay, llm, mcp_server, review, stats, sweep, tidy +from personakit import decay, digest, llm, mcp_server, review, stats, sweep, tidy from personakit.ingest import ADAPTERS from personakit.model import State from personakit.store import Store @@ -53,6 +53,10 @@ def build_parser() -> argparse.ArgumentParser: undo_cmd = sub.add_parser("undo", help="bring back one memory that tidy archived") undo_cmd.add_argument("memory_id") + digest_cmd = sub.add_parser("digest", help="what changed in the last days (prints memory text, locally)") + digest_cmd.add_argument("--days", type=int, default=7) + digest_cmd.add_argument("--json", action="store_true", help="print a JSON object") + forget = sub.add_parser("forget", help="permanently delete a memory and its quotes") forget.add_argument("memory_id") @@ -96,6 +100,15 @@ def _dispatch(args: argparse.Namespace, store: Store) -> int: print(f"Nothing to undo for {args.memory_id}: tidy did not archive it", file=sys.stderr) return 1 print("Restored.") + elif args.command == "digest": + if not 1 <= args.days <= MAX_DAYS: + print(f"--days must be between 1 and {MAX_DAYS}", file=sys.stderr) + return 2 + collected = digest.collect(store, store.now(), args.days) + if args.json: + _print_json(collected) + else: + print(digest.format_digest(collected)) elif args.command == "forget": if not store.forget(args.memory_id): print(f"No memory with id {args.memory_id}", file=sys.stderr) diff --git a/personakit/digest.py b/personakit/digest.py new file mode 100644 index 0000000..99b7bbc --- /dev/null +++ b/personakit/digest.py @@ -0,0 +1,51 @@ +"""A short summary of the last few days in your own store. + +Unlike `stats`, a digest shows memory ids and text (never evidence quotes) so you can see what +changed. It is printed to your terminal and goes nowhere else. Run it weekly from cron or a +scheduler if you want a routine; PersonaKit does not schedule anything itself. +""" + +from __future__ import annotations + +from personakit import decay +from personakit.model import Decider, Memory, State +from personakit.store import Store + +LISTED = 10 # most memories shown per section + + +def collect(store: Store, now: str, days: int = 7) -> dict: + since = decay.days_ago(now, days) + everything = store.list() + new = [m for m in everything if (m.created_at or "") >= since] + archived = [ + m for m in everything if m.state == State.ARCHIVED and m.decided_by == Decider.POLICY and (m.decided_at or "") >= since + ] + return { + "days": days, + "new": len(new), + "new_items": _items(new), + "archived_by_decay": len(archived), + "archived_items": _items(archived), + "awaiting_review": store.counts()[State.UNCONFIRMED.value], + **store.read_stats(since), + } + + +def _items(memories: list[Memory]) -> list[dict[str, str]]: + return [{"id": m.id, "text": m.text} for m in memories[:LISTED]] + + +def format_digest(data: dict) -> str: + lines = [f"PersonaKit digest, last {data['days']} days"] + for label, count, items in ( + ("New memories", data["new"], data["new_items"]), + ("Archived as stale (undo with `personakit undo `)", data["archived_by_decay"], data["archived_items"]), + ): + lines.append(f"{label}: {count}") + lines += [f" {item['id']} {item['text']}" for item in items] + if count > len(items): + lines.append(f" ... and {count - len(items)} more") + lines.append(f"Awaiting your review: {data['awaiting_review']}") + lines.append(f"Assistant reads: {data['reads']} ({data['reads_with_results']} returned something)") + return "\n".join(lines) diff --git a/personakit/store.py b/personakit/store.py index 28b301d..aa63301 100644 --- a/personakit/store.py +++ b/personakit/store.py @@ -182,9 +182,10 @@ def log_read(self, returned: int, withheld: int) -> None: # -- reading -------------------------------------------------------------------- - def read_stats(self) -> dict[str, int]: + def read_stats(self, since: str | None = None) -> dict[str, int]: row = self._conn.execute( - "SELECT COUNT(*) AS reads, COALESCE(SUM(returned > 0), 0) AS with_results FROM reads" + "SELECT COUNT(*) AS reads, COALESCE(SUM(returned > 0), 0) AS with_results FROM reads WHERE at >= ?", + (since or "",), ).fetchone() return {"reads": row["reads"], "reads_with_results": row["with_results"]} diff --git a/tests/test_cli.py b/tests/test_cli.py index d8f7744..930c9a6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -150,6 +150,12 @@ def test_tidy_json_lists_ids_only(self): def test_nothing_to_tidy_on_a_fresh_store(self): self.assertIn("Nothing to tidy.", self.run_cli("tidy")[1]) + def test_digest_prints_text_or_json_and_rejects_bad_numbers(self): + self.seed() + self.assertIn("PersonaKit digest, last 7 days", self.run_cli("digest")[1]) + self.assertEqual(json.loads(self.run_cli("digest", "--days", "36500", "--json")[1])["new"], 2) + self.assertEqual(self.run_cli("digest", "--days", "-5")[0], 2) + def test_tidy_rejects_bad_numbers(self): self.assertEqual(self.run_cli("tidy", "--days", "0")[0], 2) self.assertEqual(self.run_cli("tidy", "--days", "99999999")[0], 2) diff --git a/tests/test_digest.py b/tests/test_digest.py new file mode 100644 index 0000000..088d9ad --- /dev/null +++ b/tests/test_digest.py @@ -0,0 +1,47 @@ +import unittest + +from personakit import digest, tidy +from personakit.model import Evidence, Memory +from personakit.store import Store + +NOW = "2026-06-01T00:00:00Z" + + +def mem(text, turn=0): + return Memory(text=text, category="other", evidence=(Evidence("s1", turn, f"quote {turn}"),)) + + +class DigestTest(unittest.TestCase): + def setUp(self): + self.clock = ["2026-01-01T00:00:00Z"] + self.store = Store(":memory:", now=lambda: self.clock[0]) + self.addCleanup(self.store.close) + + def test_digest_covers_only_the_window_and_lists_ids_with_text(self): + self.store.add(mem("You are an old memory about tabs.")) + self.store.log_read(1, 0) + self.clock[0] = "2026-05-30T00:00:00Z" + recent = mem("You prefer direct, practical feedback.", turn=1) + self.store.add(recent) + self.store.log_read(2, 0) + self.store.log_read(0, 0) + data = digest.collect(self.store, NOW, days=7) + self.assertEqual((data["new"], data["reads"], data["reads_with_results"], data["awaiting_review"]), (1, 2, 1, 2)) + self.assertEqual(data["new_items"], [{"id": recent.id, "text": recent.text}]) + text = digest.format_digest(data) + self.assertIn(recent.id, text) + self.assertIn("Awaiting your review: 2", text) + self.assertNotIn("quote", text) + + def test_digest_shows_what_decay_archived_and_caps_long_lists(self): + for i in range(digest.LISTED + 2): + self.store.add(mem(f"You track item number {i} in notes.", turn=i)) + self.clock[0] = NOW + tidy.apply(self.store, tidy.make_plan(self.store, NOW, days=30)) + data = digest.collect(self.store, NOW, days=7) + self.assertEqual((data["archived_by_decay"], len(data["archived_items"])), (digest.LISTED + 2, digest.LISTED)) + self.assertIn("... and 2 more", digest.format_digest(data)) + + +if __name__ == "__main__": + unittest.main()