From 00a167634e17637d3cf59d674bb9d5adc763434f Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sun, 15 Nov 2026 21:51:33 -0500 Subject: [PATCH] fix(checklist): keep at most 50 checked items per session The 50-item cap counted only open items, so repeating add and check kept every completed row until the session was deleted, and every transfer carried them all. ledger.check now deletes the session's oldest checked items past max_done (50) in the same transaction, through the (session, done, id) index, so a session holds at most 100 rows. The guide states the rule for open and finished rows, the model and the Python docstring say only the 50 newest checked items are kept, and the hook section notes that held is the loop's in-memory count across every hook, so another hook's hold pre-empts the reminder. checklist_test checks that 55 add-and-check rounds leave the 50 newest. --- docs/extensions.md | 19 +++++---- priv/python/albedo_plugins/checklist.py | 3 +- .../extensions/checklist/extension.gleam | 2 +- .../harness/extensions/checklist/ledger.gleam | 39 ++++++++++++++----- test/e2e/checklist_test.py | 17 ++++++++ 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/docs/extensions.md b/docs/extensions.md index 547f1eb29..f160e716a 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -126,11 +126,14 @@ that commit together. `transaction` takes a body whose error is a `String`. To return a domain error from inside it, answer `Ok(Error(domain))` and flatten the result outside, as `ledger.add` does for a full checklist. -Bound what each session can store. The checklist refuses a 51st open item and a -text over 500 Unicode scalars, measured with -[`text_scalars`](../src/albedo/text_scalars.gleam). The `albedo_text_scalars` -lint refuses the grapheme functions of `gleam/string`, such as `length`, under -`src/`. +Bound what each session can store, open and finished rows alike: a transfer +carries every row, and the rows stay until the session is deleted. The checklist +refuses a 51st open item and a text over 500 Unicode scalars. When an item is +checked off, `ledger.check` deletes the session's oldest checked items past 50 +in the same transaction, through the `(session, done, id)` index. Scalars are +measured with [`text_scalars`](../src/albedo/text_scalars.gleam). The +`albedo_text_scalars` lint refuses the grapheme functions of `gleam/string`, +such as `length`, under `src/`. To add a column later, call `storage.add_columns(db, "table", [#(name, type)])` from the initializer, or add a `MigrationPlugin`. A transfer carries every @@ -237,8 +240,10 @@ The checklist's `remind` answers `hook.Stopping(held: 0, ..)`, the model ending a turn that no hook has held yet. With items open it answers a `block` that lists them. The loop commits the reason as input the model reads and runs another step. Every other event, and a turn hooks already held, gets -`hook.proceed`, so the reminder cannot loop. A read failure becomes a note the -person sees, and the turn proceeds. +`hook.proceed`, so the reminder cannot loop. `held` is the loop's count of holds +in this turn by every hook, kept in memory only. If another hook holds the turn +first, the checklist does not remind in that turn. A read failure becomes a note +the person sees, and the turn proceeds. A hook runs in the turn, so keep it to a bounded indexed read. See the [hook reference](../robot-docs/hooks.md) for every event, the merge rules, and diff --git a/priv/python/albedo_plugins/checklist.py b/priv/python/albedo_plugins/checklist.py index 5a3355ceb..fab59ea9f 100644 --- a/priv/python/albedo_plugins/checklist.py +++ b/priv/python/albedo_plugins/checklist.py @@ -23,7 +23,8 @@ class Checklist: return cast(list[dict], await host("checklist.items", {})) async def check(self, id: int) -> dict: - """Check off one of this session's items once it is done.""" + """Check off one of this session's items once it is done. Only the + 50 most recently checked items are kept.""" return cast(dict, await host("checklist.check", {"id": id})) diff --git a/src/albedo/harness/extensions/checklist/extension.gleam b/src/albedo/harness/extensions/checklist/extension.gleam index 65bf72954..e7164bdf0 100644 --- a/src/albedo/harness/extensions/checklist/extension.gleam +++ b/src/albedo/harness/extensions/checklist/extension.gleam @@ -60,7 +60,7 @@ pub fn extension() -> extension.Extension { pub const uses = [capability.Inbox] const instructions = - "This session has a checklist that outlives compaction. Use it for things you must not forget before you finish: a follow-up the person asked for, a check you postponed, a file to revisit. await checklist.add(text) adds an open item and returns it with its id; await checklist.items() lists the items, open ones first; await checklist.check(id) checks one off once it is done. Before you end a turn with items still open you are shown them once: check off what is done, and say why anything else stays open. The person can add an item with /checklist." + "This session has a checklist that outlives compaction. Use it for things you must not forget before you finish: a follow-up the person asked for, a check you postponed, a file to revisit. await checklist.add(text) adds an open item and returns it with its id; await checklist.items() lists the items, open ones first; await checklist.check(id) checks one off once it is done, and only the 50 most recently checked items are kept. Before you end a turn with items still open you are shown them once: check off what is done, and say why anything else stays open. The person can add an item with /checklist." /// The kernel's `checklist.*` calls, for the calling session only. fn route( diff --git a/src/albedo/harness/extensions/checklist/ledger.gleam b/src/albedo/harness/extensions/checklist/ledger.gleam index 5fda2ff40..6057e94aa 100644 --- a/src/albedo/harness/extensions/checklist/ledger.gleam +++ b/src/albedo/harness/extensions/checklist/ledger.gleam @@ -22,6 +22,11 @@ pub const max_text = 500 /// a runaway loop cannot grow the table or the stop reminder without bound. pub const max_open = 50 +/// How many checked items one session keeps. Checking one past it deletes +/// the oldest checked item, so with `max_open` a session holds at most 100 +/// rows, which every transfer of it carries. +pub const max_done = 50 + /// How many items one listing answers, open items first. pub const page = 100 @@ -139,21 +144,37 @@ pub fn open_in( storage.rows(db, open_sql, [sqlight.text(session)], item()) } -/// Checks off item `id` of `session`. An item of another session is not -/// found, so a model cannot close what it cannot list. +/// Checks off item `id` of `session`, and deletes the session's oldest +/// checked items past `max_done` in the same transaction. An item of +/// another session is not found, so a model cannot close what it cannot +/// list. pub fn check( store: storage.Storage, session: String, id: Int, ) -> Result(Item, Error) { storage.query(store, fn(db) { - storage.rows( - db, - "UPDATE checklist_items SET done=1 WHERE id=? AND session=? RETURNING " - <> columns, - [sqlight.int(id), sqlight.text(session)], - item(), - ) + storage.transaction(db, fn() { + use found <- result.try(storage.rows( + db, + "UPDATE checklist_items SET done=1 WHERE id=? AND session=? RETURNING " + <> columns, + [sqlight.int(id), sqlight.text(session)], + item(), + )) + // The `(session, done, id)` index finds the newest kept checked item + // and the few older ones past it: one check adds at most one. + use _ <- result.try( + storage.run( + db, + "DELETE FROM checklist_items WHERE session=?1 AND done=1 AND id < " + <> "(SELECT id FROM checklist_items WHERE session=?1 AND done=1 " + <> "ORDER BY id DESC LIMIT 1 OFFSET ?2)", + [sqlight.text(session), sqlight.int(max_done - 1)], + ), + ) + Ok(found) + }) }) |> result.map_error(Storage) |> result.try(fn(found) { diff --git a/test/e2e/checklist_test.py b/test/e2e/checklist_test.py index 9fd817ef0..7f605c1ba 100644 --- a/test/e2e/checklist_test.py +++ b/test/e2e/checklist_test.py @@ -187,6 +187,23 @@ class ChecklistTests(unittest.TestCase): self.stored(self.app.home, session), [("update the changelog", 0)] ) + def test_a_session_keeps_only_its_newest_checked_items(self): + # 55 add-and-check rounds, then one open item: the open item stays, + # and only the 50 newest checked items do. + self.cells = { + "churn": ( + "for n in range(55):\n" + " await checklist.check((await checklist.add(f'done {n}'))['id'])\n" + "await checklist.add('still open')" + ) + } + session = self.session() + self.say(session, "churn through items") + rows = self.stored(self.app.home, session) + self.assertEqual(len([row for row in rows if row[1] == 1]), 50) + self.assertEqual(rows[0], ("done 5", 1)) + self.assertEqual(rows[-1], ("still open", 0)) + def test_deleting_a_session_deletes_its_items(self): self.cells = {"remember": "await checklist.add('a doomed item')"} session = self.session() -- knotmirror 0.0.1