-
Notifications
You must be signed in to change notification settings - Fork 726
fix: bound session history load to a tail window (fixes #509, #555) #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48fa607
fix: bound session history load to a tail window (fixes #509, #555)
Nuctori 6d0e9ee
chore: bump version to 0.8.10
Nuctori 9780da2
test: cover tail/before pagination and 1000-tail cap at the route and…
Nuctori 7c9b27f
fix: address Codex review on pagination branch (multi-root, prepend v…
Nuctori de5d77a
fix: restore package.json mermaid dep and drop temporary e2e probe sc…
Nuctori ff87d45
fix: complete bounded session pagination
agegr 331c62d
Merge main into fix/session-history-tail
agegr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,4 +39,5 @@ yarn-error.log* | |
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
| .factory | ||
| .factory | ||
| e2e_*.mjs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Static + behavior coverage for the context pagination API (the #555 transfer fix): | ||
| // ?tail bounds the returned chain, ?before rewinds the walk and excludes its own | ||
| // boundary so prepending the page never duplicates it. Data behavior is covered | ||
| // end-to-end in lib/session-reader.pagination.test.mjs; here we assert the route wires | ||
| // the params through to buildSessionContext (excludeLeaf on ?before). | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import test from "node:test"; | ||
| import { createJiti } from "jiti"; | ||
|
|
||
| const routeSrc = await readFileSync(new URL("./[id]/context/route.ts", import.meta.url), "utf8"); | ||
| const jiti = createJiti(import.meta.url, { | ||
| alias: { "@": process.cwd() }, | ||
| interopDefault: true, | ||
| moduleCache: false, | ||
| }); | ||
| const { buildSessionContext } = await jiti.import("@/lib/session-reader"); | ||
|
|
||
| test("context route parses ?tail and ?before, excluding the boundary on paging", () => { | ||
| assert.match(routeSrc, /const tail = Number\.isFinite\(rawTail\) && rawTail > 0 \? Math\.min\(rawTail, 1000\) : 50/); | ||
| assert.match(routeSrc, /const before = url\.searchParams\.get\("before"\)/); | ||
| assert.match(routeSrc, /buildSessionContext\(sm\.getEntries\(\) as never, before \?\? leafId, \{[^}]*excludeLeaf: Boolean\(before\)/); | ||
| }); | ||
|
|
||
| test("context route: ?before pages upward without duplicating the boundary", () => { | ||
| const entries = []; | ||
| for (let i = 0; i < 100; i++) { | ||
| entries.push({ id: `e${i}`, parentId: i === 0 ? null : `e${i - 1}`, type: "message", timestamp: new Date(1000 + i * 1000).toISOString(), message: { role: "user", content: `m${i}` } }); | ||
| } | ||
| const page1 = buildSessionContext(entries, "e99", { tail: 5 }).entryIds; | ||
| assert.deepEqual(page1, ["e95", "e96", "e97", "e98", "e99"]); | ||
| const oldest = page1[0]; // e95 | ||
| const page2 = buildSessionContext(entries, oldest, { tail: 5, excludeLeaf: true }).entryIds; | ||
| assert.equal(page2[page2.length - 1], "e94"); | ||
| assert.ok(!page2.includes(oldest), "boundary `before` must not be duplicated"); | ||
| assert.ok(page1.every((id) => !page2.includes(id)), "adjacent pages share no entry"); | ||
| }); | ||
|
|
||
| test("context route data reports when pagination reaches the root", () => { | ||
| const entries = [ | ||
| { id: "e0", parentId: null, type: "message", timestamp: new Date(1000).toISOString(), message: { role: "user", content: "root" } }, | ||
| ]; | ||
| const page = buildSessionContext(entries, "e0", { tail: 50, excludeLeaf: true }); | ||
| assert.deepEqual(page.entryIds, []); | ||
| assert.equal(page.hasMore, false); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Static + behavior coverage for the session detail API's tail bound (the #509/#555 | ||
| // transfer fix). Mirrors runtime-route.test.mjs: source assertions confirm the route | ||
| // parses ?tail (default 50, NaN-safe, capped at 1000) and feeds only the sliced chain | ||
| // to buildSessionContext. The data-slicing behavior itself is covered end-to-end in | ||
| // lib/session-reader.pagination.test.mjs (sliceActiveBranch + buildSessionContext). | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import test from "node:test"; | ||
| import { createJiti } from "jiti"; | ||
|
|
||
| const routeSrc = await readFileSync(new URL("./[id]/route.ts", import.meta.url), "utf8"); | ||
| const jiti = createJiti(import.meta.url, { | ||
| alias: { "@": process.cwd() }, | ||
| interopDefault: true, | ||
| moduleCache: false, | ||
| }); | ||
| const { buildSessionContext } = await jiti.import("@/lib/session-reader"); | ||
|
|
||
| test("detail route parses ?tail: default 50, NaN-safe, capped at 1000", () => { | ||
| assert.match(routeSrc, /const rawTail = Number\(searchParams\.get\("tail"\)\)/); | ||
| assert.match(routeSrc, /Math\.min\(rawTail, 1000\)/); | ||
| assert.match(routeSrc, /Number\.isFinite\(rawTail\) && rawTail > 0 \? Math\.min\(rawTail, 1000\) : 50/); | ||
| assert.match(routeSrc, /buildSessionContext\(entries as never, leafId, \{[^}]*tail,[^}]*sessionId: id[^}]*\}\)/); | ||
| assert.match(routeSrc, /computeSessionStats\(entries as unknown as SessionEntry\[\]\)/); | ||
| assert.match(routeSrc, /messageCount: stats\.totalMessages/); | ||
| assert.match(routeSrc, /stats,/); | ||
| }); | ||
|
|
||
| test("detail route bounds history to the tail window (default 50 over 5000 entries)", () => { | ||
| const entries = []; | ||
| for (let i = 0; i < 5000; i++) { | ||
| entries.push({ | ||
| id: `e${i}`, | ||
| parentId: i === 0 ? null : `e${i - 1}`, | ||
| type: "message", | ||
| timestamp: new Date(1000 + i * 1000).toISOString(), | ||
| message: { role: i % 2 === 0 ? "user" : "assistant", content: `m${i}` }, | ||
| }); | ||
| } | ||
| const ctx = buildSessionContext(entries, "e4999", { tail: 50 }); | ||
| assert.equal(ctx.messages.length, 50); | ||
| // The transferred window is the tail, not the full 5000-entry forest. | ||
| assert.equal(ctx.entryIds[0], "e4950"); | ||
| assert.equal(ctx.entryIds[ctx.entryIds.length - 1], "e4999"); | ||
| }); | ||
|
|
||
| test("detail route with an out-of-range tail still caps at 1000", () => { | ||
| const entries = []; | ||
| for (let i = 0; i < 5000; i++) { | ||
| entries.push({ id: `e${i}`, parentId: i === 0 ? null : `e${i - 1}`, type: "message", timestamp: new Date(1000 + i * 1000).toISOString(), message: { role: "user", content: `m${i}` } }); | ||
| } | ||
| const ctx = buildSessionContext(entries, "e4999", { tail: 5000 }); | ||
| assert.equal(ctx.messages.length, 5000); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This iterative rewrite no longer treats
nodes.length > 1as a branch. Sessions branched from the first message have multiple root nodes (andselectTopLevelBranchesstill returns those roots), buthasBranchnow returns false when each root has at most one child, causing the UI to show the no-branches state and hide the branch choices. Re-add the top-levelnodes.length > 1check before walking the stack.Useful? React with 👍 / 👎.