Skip to content

fix: count only visible messages toward the session tail budget - #810

Merged
agegr merged 10 commits into
agegr:mainfrom
cy7372:fix/session-tail-visible-messages
Sep 19, 2026
Merged

agegr merged 10 commits into
agegr:mainfrom
cy7372:fix/session-tail-visible-messages

Conversation

@cy7372

@cy7372 cy7372 commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Problem

The session tail window was counted in raw entries, so toolResult and other non-rendering entries consumed the budget. In agent-heavy sessions the window starved user messages out of the chat: a real session with 15 user messages across 562 entries loaded exactly one user message; every earlier question needed manual "load earlier" paging.

Fix

countsTowardTail(): only user / assistant messages plus the compaction divider consume the tail budget; toolResult entries, hidden custom messages and session meta ride along inside the span without consuming it. Older history still loads on demand via pagination.

Includes a regression test for the pagination path.

The tail window was counted in raw entries, so toolResult and other
non-rendering entries consumed the budget. In agent-heavy sessions the
window starved user messages out of the chat: a real session with 15
user messages across 562 entries loaded exactly ONE user message; every
earlier question needed manual "load earlier" paging.

- countsTowardTail(): user / assistant messages plus the compaction
  divider consume the budget; toolResult and hidden custom entries ride
  along inside the span
- regression test for the pagination path
@cy7372
cy7372 force-pushed the fix/session-tail-visible-messages branch from 9983363 to a72e763 Compare September 13, 2026 01:25
agegr and others added 9 commits September 13, 2026 23:22
SameSite=Strict drops the session cookie on external top-level navigations (common on mobile), forcing a re-login while in-page XHR still works.

Fixes agegr#806
Patches GHSA-2xp9-vwfh-vxw4 / GHSA-p293-qw3h-jr36 (unauthenticated RCE in
[email protected]) and pulls sharp 0.35.4 for the libheif advisories via
npm audit fix. next/image is only used for the static logo, so the
/_next/image endpoint is turned off entirely with images.unoptimized.

Co-authored-by: Cursor <[email protected]>
scrollIntoView({ block: "nearest" }) parked the keyboard-selected option flush
against the scroll container, where whole-pixel scroll snapping left it a
fraction of a pixel outside the viewport and overflow clipping cut off its
focus ring. Give options a scroll-margin equal to the container padding so
navigation always lands them fully visible; this also unblocks the e2e
"Keyboard selection must scroll into view" assertion.

Co-authored-by: Cursor <[email protected]>
Route handlers have no reliable client address, so a per-IP limiter would
be spoofable via x-forwarded-for and empty for direct 127.0.0.1 access.
Use one shared exponential backoff (1s doubling to 60s) for POST
/api/web-auth instead; a success or five idle minutes resets it. Return
429 with Retry-After while blocked and surface the wait on the login page.

Co-authored-by: Cursor <[email protected]>

@agegr agegr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: with tail counted in raw entries, a tool-heavy session (user → assistant+toolCall → toolResult → assistant, ×N) fits roughly one user turn into the default 50-entry page. Counting user/assistant messages plus the compaction divider is the right unit, and hasMore keeps paging working.

I merged main and pushed two adjustments:

  • Raw cap lowered from max(1500, tail·30) to max(200, tail·6). Tool results can be large, and 1500 raw entries per page (45 000 at tail=1000) undoes the payload bound this slice exists for. With a 1:1 assistant:toolResult ratio tail·6 is still 3× headroom, and the search path (tail: 200) gets 1200. Added a test for the cap.
  • Restored the sliceActiveBranch JSDoc (the new helper's comment had been inserted between the doc block and the function) and dropped the dated anecdote from the comment.

tsc, eslint, and lib/session-reader* + app/api/sessions/** tests pass. Approving.

@agegr
agegr merged commit 50a2fd4 into agegr:main Sep 19, 2026
agegr added a commit that referenced this pull request Sep 19, 2026
Co-authored-by: Cursor <[email protected]>

#810 stopped counting toolResults toward the default page, so the old 24-turn compacted session no longer pushed the user prompt off the first page.
lclichen pushed a commit to lclichen/pi-web that referenced this pull request Sep 22, 2026
…r#810)

* fix: count only visible messages toward the session tail budget

The tail window was counted in raw entries, so toolResult and other
non-rendering entries consumed the budget. In agent-heavy sessions the
window starved user messages out of the chat: a real session with 15
user messages across 562 entries loaded exactly ONE user message; every
earlier question needed manual "load earlier" paging.

- countsTowardTail(): user / assistant messages plus the compaction
  divider consume the budget; toolResult and hidden custom entries ride
  along inside the span
- regression test for the pagination path

* Align new session branding with chat input

* fix: preserve UNC roots in file API paths

* fix(auth): use SameSite=Lax for pi_web_session cookie (agegr#818)

SameSite=Strict drops the session cookie on external top-level navigations (common on mobile), forcing a re-login while in-page XHR still works.

Fixes agegr#806

* fix: preserve newlines in extension dialog titles like pi's TUI

* fix(deps): upgrade Next.js to 16.3.5 and disable image optimizer

Patches GHSA-2xp9-vwfh-vxw4 / GHSA-p293-qw3h-jr36 (unauthenticated RCE in
[email protected]) and pulls sharp 0.35.4 for the libheif advisories via
npm audit fix. next/image is only used for the static logo, so the
/_next/image endpoint is turned off entirely with images.unoptimized.

Co-authored-by: Cursor <[email protected]>

* fix: keep focused extension select option clear of the scroller edge

scrollIntoView({ block: "nearest" }) parked the keyboard-selected option flush
against the scroll container, where whole-pixel scroll snapping left it a
fraction of a pixel outside the viewport and overflow clipping cut off its
focus ring. Give options a scroll-margin equal to the container padding so
navigation always lands them fully visible; this also unblocks the e2e
"Keyboard selection must scroll into view" assertion.

Co-authored-by: Cursor <[email protected]>

* fix(auth): throttle web password failures with global backoff

Route handlers have no reliable client address, so a per-IP limiter would
be spoofable via x-forwarded-for and empty for direct 127.0.0.1 access.
Use one shared exponential backoff (1s doubling to 60s) for POST
/api/web-auth instead; a success or five idle minutes resets it. Return
429 with Retry-After while blocked and surface the wait on the login page.

Co-authored-by: Cursor <[email protected]>

* Tighten the raw-entry page cap and restore the sliceActiveBranch doc comment

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: richardmilles <[email protected]>
Co-authored-by: Cursor <[email protected]>
(cherry picked from commit 50a2fd4)
lclichen pushed a commit to lclichen/pi-web that referenced this pull request Sep 22, 2026
Co-authored-by: Cursor <[email protected]>

agegr#810 stopped counting toolResults toward the default page, so the old 24-turn compacted session no longer pushed the user prompt off the first page.
(cherry picked from commit eac6f14)
HeRockOne pushed a commit to HeRockOne/pi-web that referenced this pull request Sep 30, 2026
…r#810)

* fix: count only visible messages toward the session tail budget

The tail window was counted in raw entries, so toolResult and other
non-rendering entries consumed the budget. In agent-heavy sessions the
window starved user messages out of the chat: a real session with 15
user messages across 562 entries loaded exactly ONE user message; every
earlier question needed manual "load earlier" paging.

- countsTowardTail(): user / assistant messages plus the compaction
  divider consume the budget; toolResult and hidden custom entries ride
  along inside the span
- regression test for the pagination path

* Align new session branding with chat input

* fix: preserve UNC roots in file API paths

* fix(auth): use SameSite=Lax for pi_web_session cookie (agegr#818)

SameSite=Strict drops the session cookie on external top-level navigations (common on mobile), forcing a re-login while in-page XHR still works.

Fixes agegr#806

* fix: preserve newlines in extension dialog titles like pi's TUI

* fix(deps): upgrade Next.js to 16.3.5 and disable image optimizer

Patches GHSA-2xp9-vwfh-vxw4 / GHSA-p293-qw3h-jr36 (unauthenticated RCE in
[email protected]) and pulls sharp 0.35.4 for the libheif advisories via
npm audit fix. next/image is only used for the static logo, so the
/_next/image endpoint is turned off entirely with images.unoptimized.

Co-authored-by: Cursor <[email protected]>

* fix: keep focused extension select option clear of the scroller edge

scrollIntoView({ block: "nearest" }) parked the keyboard-selected option flush
against the scroll container, where whole-pixel scroll snapping left it a
fraction of a pixel outside the viewport and overflow clipping cut off its
focus ring. Give options a scroll-margin equal to the container padding so
navigation always lands them fully visible; this also unblocks the e2e
"Keyboard selection must scroll into view" assertion.

Co-authored-by: Cursor <[email protected]>

* fix(auth): throttle web password failures with global backoff

Route handlers have no reliable client address, so a per-IP limiter would
be spoofable via x-forwarded-for and empty for direct 127.0.0.1 access.
Use one shared exponential backoff (1s doubling to 60s) for POST
/api/web-auth instead; a success or five idle minutes resets it. Return
429 with Retry-After while blocked and surface the wait on the login page.

Co-authored-by: Cursor <[email protected]>

* Tighten the raw-entry page cap and restore the sliceActiveBranch doc comment

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: richardmilles <[email protected]>
Co-authored-by: Cursor <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…r#810)

* fix: count only visible messages toward the session tail budget

The tail window was counted in raw entries, so toolResult and other
non-rendering entries consumed the budget. In agent-heavy sessions the
window starved user messages out of the chat: a real session with 15
user messages across 562 entries loaded exactly ONE user message; every
earlier question needed manual "load earlier" paging.

- countsTowardTail(): user / assistant messages plus the compaction
  divider consume the budget; toolResult and hidden custom entries ride
  along inside the span
- regression test for the pagination path

* Align new session branding with chat input

* fix: preserve UNC roots in file API paths

* fix(auth): use SameSite=Lax for pi_web_session cookie (agegr#818)

SameSite=Strict drops the session cookie on external top-level navigations (common on mobile), forcing a re-login while in-page XHR still works.

Fixes agegr#806

* fix: preserve newlines in extension dialog titles like pi's TUI

* fix(deps): upgrade Next.js to 16.3.5 and disable image optimizer

Patches GHSA-2xp9-vwfh-vxw4 / GHSA-p293-qw3h-jr36 (unauthenticated RCE in
[email protected]) and pulls sharp 0.35.4 for the libheif advisories via
npm audit fix. next/image is only used for the static logo, so the
/_next/image endpoint is turned off entirely with images.unoptimized.

Co-authored-by: Cursor <[email protected]>

* fix: keep focused extension select option clear of the scroller edge

scrollIntoView({ block: "nearest" }) parked the keyboard-selected option flush
against the scroll container, where whole-pixel scroll snapping left it a
fraction of a pixel outside the viewport and overflow clipping cut off its
focus ring. Give options a scroll-margin equal to the container padding so
navigation always lands them fully visible; this also unblocks the e2e
"Keyboard selection must scroll into view" assertion.

Co-authored-by: Cursor <[email protected]>

* fix(auth): throttle web password failures with global backoff

Route handlers have no reliable client address, so a per-IP limiter would
be spoofable via x-forwarded-for and empty for direct 127.0.0.1 access.
Use one shared exponential backoff (1s doubling to 60s) for POST
/api/web-auth instead; a success or five idle minutes resets it. Return
429 with Retry-After while blocked and surface the wait on the login page.

Co-authored-by: Cursor <[email protected]>

* Tighten the raw-entry page cap and restore the sliceActiveBranch doc comment

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: richardmilles <[email protected]>
Co-authored-by: Cursor <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
Co-authored-by: Cursor <[email protected]>

agegr#810 stopped counting toolResults toward the default page, so the old 24-turn compacted session no longer pushed the user prompt off the first page.
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…r#810)

* fix: count only visible messages toward the session tail budget

The tail window was counted in raw entries, so toolResult and other
non-rendering entries consumed the budget. In agent-heavy sessions the
window starved user messages out of the chat: a real session with 15
user messages across 562 entries loaded exactly ONE user message; every
earlier question needed manual "load earlier" paging.

- countsTowardTail(): user / assistant messages plus the compaction
  divider consume the budget; toolResult and hidden custom entries ride
  along inside the span
- regression test for the pagination path

* Align new session branding with chat input

* fix: preserve UNC roots in file API paths

* fix(auth): use SameSite=Lax for pi_web_session cookie (agegr#818)

SameSite=Strict drops the session cookie on external top-level navigations (common on mobile), forcing a re-login while in-page XHR still works.

Fixes agegr#806

* fix: preserve newlines in extension dialog titles like pi's TUI

* fix(deps): upgrade Next.js to 16.3.5 and disable image optimizer

Patches GHSA-2xp9-vwfh-vxw4 / GHSA-p293-qw3h-jr36 (unauthenticated RCE in
[email protected]) and pulls sharp 0.35.4 for the libheif advisories via
npm audit fix. next/image is only used for the static logo, so the
/_next/image endpoint is turned off entirely with images.unoptimized.

Co-authored-by: Cursor <[email protected]>

* fix: keep focused extension select option clear of the scroller edge

scrollIntoView({ block: "nearest" }) parked the keyboard-selected option flush
against the scroll container, where whole-pixel scroll snapping left it a
fraction of a pixel outside the viewport and overflow clipping cut off its
focus ring. Give options a scroll-margin equal to the container padding so
navigation always lands them fully visible; this also unblocks the e2e
"Keyboard selection must scroll into view" assertion.

Co-authored-by: Cursor <[email protected]>

* fix(auth): throttle web password failures with global backoff

Route handlers have no reliable client address, so a per-IP limiter would
be spoofable via x-forwarded-for and empty for direct 127.0.0.1 access.
Use one shared exponential backoff (1s doubling to 60s) for POST
/api/web-auth instead; a success or five idle minutes resets it. Return
429 with Retry-After while blocked and surface the wait on the login page.

Co-authored-by: Cursor <[email protected]>

* Tighten the raw-entry page cap and restore the sliceActiveBranch doc comment

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Alex Yang <[email protected]>
Co-authored-by: richardmilles <[email protected]>
Co-authored-by: Cursor <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
Co-authored-by: Cursor <[email protected]>

agegr#810 stopped counting toolResults toward the default page, so the old 24-turn compacted session no longer pushed the user prompt off the first page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants