Skip to content

Commit 0831b5f

Browse files
Claude Agentclaude
andcommitted
fix(app): keep nested session descendants when trimming the store
Expand the keep set transitively over the parentID chain (fixpoint loop) so grandchildren of a kept root survive store trimming while subtrees of trimmed roots are still dropped. Open-permission and recency keeps remain as OR criteria. Cycle-safe and covered by new trim tests. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 4c6968a commit 0831b5f

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎packages/app/src/context/global-sync/session-trim.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,82 @@ describe("trimSessions", () => {
5656
"root-2",
5757
])
5858
})
59+
60+
test("keeps descendants of a kept root transitively", () => {
61+
const now = 1_000_000
62+
const old = now - 20_000_000
63+
const list = [
64+
session({ id: "root-1", created: now - 1000 }),
65+
session({ id: "child", parentID: "root-1", created: old }),
66+
session({ id: "grandchild", parentID: "child", created: old }),
67+
session({ id: "great-grandchild", parentID: "grandchild", created: old }),
68+
]
69+
70+
const result = trimSessions(list, { limit: 2, permission: {}, now })
71+
expect(result.map((x) => x.id)).toEqual(["child", "grandchild", "great-grandchild", "root-1"])
72+
})
73+
74+
test("trims descendants of a trimmed root transitively", () => {
75+
const now = 1_000_000
76+
const old = now - 20_000_000
77+
const list = [
78+
session({ id: "root-1", created: now - 1000 }),
79+
session({ id: "root-2", created: now - 2000 }),
80+
session({ id: "z-root", created: now - 30_000_000 }),
81+
session({ id: "z-child", parentID: "z-root", created: old }),
82+
session({ id: "z-grandchild", parentID: "z-child", created: old }),
83+
]
84+
85+
const result = trimSessions(list, { limit: 2, permission: {}, now })
86+
expect(result.map((x) => x.id)).toEqual(["root-1", "root-2"])
87+
})
88+
89+
test("permission and recency still keep deep children of a trimmed root", () => {
90+
const now = 1_000_000
91+
const old = now - 20_000_000
92+
const list = [
93+
session({ id: "root-1", created: now - 1000 }),
94+
session({ id: "root-2", created: now - 2000 }),
95+
session({ id: "z-root", created: now - 30_000_000 }),
96+
session({ id: "z-child", parentID: "z-root", created: old }),
97+
session({ id: "z-grandchild-permission", parentID: "z-child", created: old }),
98+
session({ id: "z-grandchild-recent", parentID: "z-child", created: now - 500 }),
99+
session({ id: "z-grandchild-trimmed", parentID: "z-child", created: old }),
100+
]
101+
102+
const result = trimSessions(list, {
103+
limit: 2,
104+
permission: {
105+
"z-grandchild-permission": [{ id: "perm-1" } as PermissionRequest],
106+
},
107+
now,
108+
})
109+
expect(result.map((x) => x.id)).toEqual(["root-1", "root-2", "z-grandchild-permission", "z-grandchild-recent"])
110+
})
111+
112+
test("drops old orphan children whose parent chain never reaches a kept root", () => {
113+
const now = 1_000_000
114+
const old = now - 20_000_000
115+
const list = [
116+
session({ id: "root-1", created: now - 1000 }),
117+
session({ id: "orphan", parentID: "missing-parent", created: old }),
118+
session({ id: "orphan-child", parentID: "orphan", created: old }),
119+
]
120+
121+
const result = trimSessions(list, { limit: 2, permission: {}, now })
122+
expect(result.map((x) => x.id)).toEqual(["root-1"])
123+
})
124+
125+
test("does not hang on a parentID cycle", () => {
126+
const now = 1_000_000
127+
const old = now - 20_000_000
128+
const list = [
129+
session({ id: "root-1", created: now - 1000 }),
130+
session({ id: "cycle-a", parentID: "cycle-b", created: old }),
131+
session({ id: "cycle-b", parentID: "cycle-a", created: old }),
132+
]
133+
134+
const result = trimSessions(list, { limit: 2, permission: {}, now })
135+
expect(result.map((x) => x.id)).toEqual(["root-1"])
136+
})
59137
})

‎packages/app/src/context/global-sync/session-trim.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ export function trimSessions(
4747
const recent = takeRecentSessions(roots.slice(limit), SESSION_RECENT_LIMIT, cutoff)
4848
const keepRoots = [...base, ...recent]
4949
const keepRootIds = new Set(keepRoots.map((s) => s.id))
50+
// Expand the keep set transitively: a child survives when its parent chain
51+
// reaches a kept root, no matter how deep (fixpoint loop, cycle-safe since
52+
// cycle members are never anchored to a kept root).
53+
const keptByAncestor = new Set(keepRootIds)
54+
let changed = true
55+
while (changed) {
56+
changed = false
57+
for (const child of children) {
58+
if (keptByAncestor.has(child.id)) continue
59+
if (child.parentID && keptByAncestor.has(child.parentID)) {
60+
keptByAncestor.add(child.id)
61+
changed = true
62+
}
63+
}
64+
}
5065
const keepChildren = children.filter((s) => {
51-
if (s.parentID && keepRootIds.has(s.parentID)) return true
66+
if (keptByAncestor.has(s.id)) return true
5267
const perms = options.permission[s.id] ?? []
5368
if (perms.length > 0) return true
5469
return sessionUpdatedAt(s) > cutoff

0 commit comments

Comments
 (0)