From 2c8590ed10ae5bef2aa4eee5c557391c1925c9af Mon Sep 17 00:00:00 2001 From: dawn <90008@klbr.net> Date: Wed, 25 Nov 2026 21:46:15 +0300 Subject: [PATCH] fix(sessions): keep removed worktree sessions filtered Signed-off-by: dawn <90008@klbr.net> --- .../sql/session/session_collection_sql.gleam | 2 +- .../albedo/session/session_collection.gleam | 18 +++++++--- .../sql/worktree_predicate.sql | 12 +++++-- .../session/session_collection_test.gleam | 33 ++++++++++++------- cli/internal/daemon/protocol/api.gen.go | 2 +- docs/http-sessions.md | 13 ++++---- docs/openapi.yaml | 5 +-- e2e/worktrees_test.py | 13 +++++++- robot-docs/worktrees.md | 12 +++---- web/src/lib/api.gen.d.ts | 2 +- 10 files changed, 74 insertions(+), 38 deletions(-) diff --git a/apps/albedo/src/albedo/generated/sql/session/session_collection_sql.gleam b/apps/albedo/src/albedo/generated/sql/session/session_collection_sql.gleam index cf090cb53..c345e21c1 100644 --- a/apps/albedo/src/albedo/generated/sql/session/session_collection_sql.gleam +++ b/apps/albedo/src/albedo/generated/sql/session/session_collection_sql.gleam @@ -115,4 +115,4 @@ pub const workspace_predicate = "AND s.cwd=?" /// Generated from src/albedo/session/session_collection/sql/worktree_predicate.sql pub const worktree_predicate = - "AND EXISTS (\n SELECT 1 FROM worktrees\n WHERE workspace = s.cwd OR path = s.cwd OR instr(s.cwd, path || '/') = 1\n) = ?" + "AND (\n (\n instr(s.cwd, ?) = 1\n AND instr(trim(substr(s.cwd, length(?) + 1), '/'), '/') > 0\n )\n OR EXISTS (\n SELECT 1 FROM worktrees\n WHERE workspace = s.cwd OR path = s.cwd OR instr(s.cwd, path || '/') = 1\n )\n) = ?" diff --git a/apps/albedo/src/albedo/session/session_collection.gleam b/apps/albedo/src/albedo/session/session_collection.gleam index 05c0048c3..66d4faf54 100644 --- a/apps/albedo/src/albedo/session/session_collection.gleam +++ b/apps/albedo/src/albedo/session/session_collection.gleam @@ -7,6 +7,7 @@ import albedo/history/conversation import albedo/refusal.{type Refusal} import albedo/session/family import albedo/session/session_lookup +import albedo/workspace/worktree import albedo_sdk/session_info import albedo_sdk/store import albedo_sqlite/template.{type Predicate} @@ -149,6 +150,17 @@ fn filter_in( ids(values), ]) } + let worktrees = case filter.worktree { + None -> #("", []) + Some(wanted) -> { + let prefix = sqlight.text(worktree.base_dir() <> "/") + #(session_collection_sql.worktree_predicate, [ + prefix, + prefix, + sqlight.bool(wanted), + ]) + } + } let clauses = [ #(session_collection_sql.visible_stage_predicate, [ sqlight.text(session_info.stage_name(session_info.Importing)), @@ -172,11 +184,7 @@ fn filter_in( sqlight.bool, ), value_clause(filter.acp, session_collection_sql.acp_predicate, sqlight.bool), - value_clause( - filter.worktree, - session_collection_sql.worktree_predicate, - sqlight.bool, - ), + worktrees, value_clause( filter.pinned, session_collection_sql.pinned_predicate, diff --git a/apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql b/apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql index e11ff2554..2ebaf4d49 100644 --- a/apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql +++ b/apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql @@ -1,5 +1,11 @@ -- albedo: fragment -AND EXISTS ( - SELECT 1 FROM worktrees - WHERE workspace = s.cwd OR path = s.cwd OR instr(s.cwd, path || '/') = 1 +AND ( + ( + instr(s.cwd, ?) = 1 + AND instr(trim(substr(s.cwd, length(?) + 1), '/'), '/') > 0 + ) + OR EXISTS ( + SELECT 1 FROM worktrees + WHERE workspace = s.cwd OR path = s.cwd OR instr(s.cwd, path || '/') = 1 + ) ) = ? diff --git a/apps/albedo/test/albedo/session/session_collection_test.gleam b/apps/albedo/test/albedo/session/session_collection_test.gleam index 453e32b64..bfc4ef9a3 100644 --- a/apps/albedo/test/albedo/session/session_collection_test.gleam +++ b/apps/albedo/test/albedo/session/session_collection_test.gleam @@ -1,17 +1,14 @@ //// The worktree filter of `session/session_collection`, against a model of the //// rule in docs/openapi.yaml: a session is in a worktree when its workspace is -//// a checkout's workspace, the checkout, or a directory inside the checkout. -//// The pinned filter combines with it. -//// -//// The defects are a prefix match that takes a sibling checkout for a -//// directory inside one (`/wt/ab` is not inside `/wt/a`), a false filter that -//// is not the complement of the true one, and a pinned filter that ignores or -//// replaces the worktree one. +//// a recorded checkout's workspace or inside its path, or under the managed +//// `wt//` layout after the record is gone. The pinned filter +//// combines with it. import albedo/session/registry import albedo/session/session_collection.{type Filter} import albedo/storage/core_store import albedo/support/store_fixture +import albedo/workspace/worktree import albedo_property import albedo_sdk/store import gleam/int @@ -21,7 +18,7 @@ import gleam/string import qcheck.{type Generator} import sqlight -fn directory() -> Generator(String) { +fn directory(root: String) -> Generator(String) { qcheck.generic_list( qcheck.from_generators(qcheck.return("a"), [ qcheck.return("ab"), @@ -29,7 +26,7 @@ fn directory() -> Generator(String) { ]), qcheck.bounded_int(1, 3), ) - |> qcheck.map(fn(parts) { "/wt/" <> string.join(parts, "/") }) + |> qcheck.map(fn(parts) { root <> "/" <> string.join(parts, "/") }) } fn inside(workspace: String, checkout: #(String, String)) -> Bool { @@ -62,16 +59,23 @@ pub fn the_worktree_filter_follows_the_checkouts_rule_test_() -> albedo_property use <- albedo_property.within(60) let assert Ok(ledger) = store_fixture.start(":memory:", core_store.tables()) let assert Ok(_) = registry.initialise_core(ledger) + let base = worktree.base_dir() + let workspace = + qcheck.from_generators(directory("/wt"), [ + directory(base), + directory(base <> "-other"), + directory("host:" <> base), + ]) let result = { use #(checkouts, workspaces) <- albedo_property.run( 100, qcheck.tuple2( qcheck.generic_list( - qcheck.tuple2(directory(), directory()), + qcheck.tuple2(directory("/wt"), directory("/wt")), qcheck.bounded_int(0, 3), ), qcheck.generic_list( - qcheck.tuple2(directory(), qcheck.bool()), + qcheck.tuple2(workspace, qcheck.bool()), qcheck.bounded_int(1, 8), ), ), @@ -116,7 +120,12 @@ pub fn the_worktree_filter_follows_the_checkouts_rule_test_() -> albedo_property let expected = fn(worktree, pinned) { sessions |> list.filter(fn(session) { - let in_worktree = list.any(checkouts, inside(session.1, _)) + let managed_path = case string.split_once(session.1, base <> "/") { + Ok(#("", path)) -> list.length(string.split(path, "/")) >= 2 + _ -> False + } + let in_worktree = + managed_path || list.any(checkouts, inside(session.1, _)) option.map(worktree, fn(wanted) { in_worktree == wanted }) |> option.unwrap(True) && option.map(pinned, fn(wanted) { session.2 == wanted }) diff --git a/cli/internal/daemon/protocol/api.gen.go b/cli/internal/daemon/protocol/api.gen.go index dd505894a..5dc55ccbb 100644 --- a/cli/internal/daemon/protocol/api.gen.go +++ b/cli/internal/daemon/protocol/api.gen.go @@ -5171,7 +5171,7 @@ type ListSessionsParams struct { // Acp true lists only ACP-owned sessions, false only the others. Combine with workspace for an exact canonical workspace match. Acp *bool `form:"acp,omitempty" json:"acp,omitempty"` - // Worktree true lists only sessions whose workspace is a managed worktree, inside one, or its checkout; false only the others. + // Worktree true lists only sessions in managed worktrees, including paths under the daemon's managed checkout directory after removal. false lists only the others. Worktree *bool `form:"worktree,omitempty" json:"worktree,omitempty"` // Pinned true lists only pinned sessions, false only the others. diff --git a/docs/http-sessions.md b/docs/http-sessions.md index c292d81b9..4a3fbeac1 100644 --- a/docs/http-sessions.md +++ b/docs/http-sessions.md @@ -56,12 +56,13 @@ projections. `GET /sessions` accepts `scope=roots|all` and optional `parent_id`, `family_id`, `workspace`, `search`, `archived`, `acp`, `worktree`, `pinned`, and -`needs_reload`. The `worktree` filter keeps only sessions in a managed worktree -when true, and leaves them out when false. The parent and family filters are -mutually exclusive. `family_id` selects the containing root and all descendants. -`search` matches display names and ID prefixes. `sort=activity|frequent` -defaults to activity. Frequent sorts by opens, activity, and ID. Pinned state -remains available for client grouping. +`needs_reload`. The `worktree` filter keeps only managed worktree sessions when +true, and leaves them out when false, including sessions in +[removed worktrees](../robot-docs/worktrees.md#filter-sessions-by-worktree). The +parent and family filters are mutually exclusive. `family_id` selects the +containing root and all descendants. `search` matches display names and ID +prefixes. `sort=activity|frequent` defaults to activity. Frequent sorts by +opens, activity, and ID. Pinned state remains available for client grouping. The CLI's prompt, read, send, and stop workflows resolve a full lowercase UUIDv7 with `GET /sessions/{id}/configuration`, including child sessions. Only diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 53fa92ce4..b164f73e6 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -1704,8 +1704,9 @@ paths: in: query required: false description: - true lists only sessions whose workspace is a managed worktree, - inside one, or its checkout; false only the others. + true lists only sessions in managed worktrees, including paths under + the daemon's managed checkout directory after removal. false lists + only the others. schema: type: boolean - name: pinned diff --git a/e2e/worktrees_test.py b/e2e/worktrees_test.py index ee711fa66..21856c91e 100644 --- a/e2e/worktrees_test.py +++ b/e2e/worktrees_test.py @@ -192,7 +192,7 @@ class WorktreesTests(unittest.TestCase): ) self.assertIn("filed in the main checkout", after["after"]) - def test_a_root_session_in_a_worktree_can_be_left_out_of_a_listing(self): + def test_removed_worktree_sessions_stay_out_of_the_filtered_listing(self): made = self.cell( "import json\n" "wt = await worktrees.create('listing-tree')\n" @@ -226,6 +226,17 @@ class WorktreesTests(unittest.TestCase): self.assertEqual({inside, self.parent} & listed("pinned=true"), {inside}) self.assertEqual({inside, self.parent} & listed("pinned=false"), {self.parent}) + removed = self.cell( + "import json, os\n" + "removed = await worktrees.remove('listing-tree')\n" + "print(json.dumps(dict(removed=removed, exists=os.path.isdir(wt.path))))\n" + ) + self.assertEqual(removed, {"removed": True, "exists": False}) + self.assertEqual({inside, self.parent} & listed("worktree=true"), {inside}) + self.assertEqual( + {inside, self.parent} & listed("worktree=false"), {self.parent} + ) + def test_spawn_true_makes_a_worktree_and_close_keeps_it(self): marker = "worktree child probe " + operation_id() self.child_probe_script(marker, "await mail.submit('parent', 'done')") diff --git a/robot-docs/worktrees.md b/robot-docs/worktrees.md index c7d6f6c56..11a3cc2f9 100644 --- a/robot-docs/worktrees.md +++ b/robot-docs/worktrees.md @@ -93,13 +93,13 @@ and [agents](agent-sessions.md). ## Filter sessions by worktree -A session is in a worktree when its workspace is a checkout's `workspace`, the -checkout `path`, or a directory inside it. The +The [`worktree` predicate](../apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql) -applies that rule to `GET /sessions?worktree=`. `true` lists only those sessions -and `false` only the others. The filter reads the `worktrees` table, so a -session whose checkout record is gone lists with the others. Clients choose -whether to send it; the daemon never applies it to a listing that did not ask. +matches sessions at a recorded checkout's `workspace`, its `path`, or inside +that path. It also matches sessions under the daemon's managed checkout layout, +`$ALBEDO_HOME/wt//`, even after removal deletes the checkout and +its record. `GET /sessions?worktree=true` lists these sessions and `false` lists +the others. A listing that doesn't ask for the filter is unchanged. ## Managed context diff --git a/web/src/lib/api.gen.d.ts b/web/src/lib/api.gen.d.ts index 1c326abc4..3a0bc1b14 100644 --- a/web/src/lib/api.gen.d.ts +++ b/web/src/lib/api.gen.d.ts @@ -8117,7 +8117,7 @@ export interface operations { needs_reload?: boolean; /** @description true lists only ACP-owned sessions, false only the others. Combine with workspace for an exact canonical workspace match. */ acp?: boolean; - /** @description true lists only sessions whose workspace is a managed worktree, inside one, or its checkout; false only the others. */ + /** @description true lists only sessions in managed worktrees, including paths under the daemon's managed checkout directory after removal. false lists only the others. */ worktree?: boolean; /** @description true lists only pinned sessions, false only the others. */ pinned?: boolean; -- knotmirror 0.0.1