Skip to content

The agent that grows with you

fix(desktop): keep streaming turn out of history budget

worlldz65babb3bcb36
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.test.ts b/apps/desktop/src/components/assistant-ui/thread/list.test.ts
index 7979a6e583f..a3b11469000 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.test.ts
+++ b/apps/desktop/src/components/assistant-ui/thread/list.test.ts
@@ -264,6 +264,21 @@ describe('firstVisibleGroupIndex', () => {
expect(firstVisibleGroupIndex(groups, 600, 8)).toBe(0)
})
+
+ it('keeps the cut stable while the unbudgeted streaming tail grows', () => {
+ const history = [group('old', 50), group('mid', 30), group('recent', 30)]
+ const initial = [...history, group('streaming', 1)]
+ const grown = [...history, group('streaming', 5_000)]
+
+ expect(firstVisibleGroupIndex(initial, 60, 0, 1)).toBe(1)
+ expect(firstVisibleGroupIndex(grown, 60, 0, 1)).toBe(1)
+ })
+
+ it('charges a completed tail to the budget as before', () => {
+ const groups = [group('old', 50), group('recent', 30), group('completed', 5_000)]
+
+ expect(firstVisibleGroupIndex(groups, 60)).toBe(2)
+ })
})
describe('liveTailStart', () => {
diff --git a/apps/desktop/src/components/assistant-ui/thread/list.tsx b/apps/desktop/src/components/assistant-ui/thread/list.tsx
index 889fbda2e10..252e7cb9e56 100644
--- a/apps/desktop/src/components/assistant-ui/thread/list.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/list.tsx
@@ -281,11 +281,20 @@ export function buildGroups(signature: string): MessageGroup[] {
// Walk turns newest-first, summing their render weights until the budget is met;
// everything before the first kept turn is hidden. `minVisible` turns are kept
// regardless of weight. Returns the index of that first visible group.
-export function firstVisibleGroupIndex(groups: readonly MessageGroup[], budget: number, minVisible = 0): number {
+export function firstVisibleGroupIndex(
+ groups: readonly MessageGroup[],
+ budget: number,
+ minVisible = 0,
+ unbudgetedTail = 0
+): number {
let firstVisible = groups.length
+ const budgetedEnd = Math.max(0, groups.length - unbudgetedTail)
for (let i = groups.length - 1, weight = 0; i >= 0; i--) {
- weight += groups[i].weight
+ if (i < budgetedEnd) {
+ weight += groups[i].weight
+ }
+
firstVisible = i
if (weight >= budget) {
@@ -444,6 +453,8 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
s.thread.messages.map(message => messagePaintWeight(message.content)).join(',')
)
+ const threadRunning = useAuiState(s => s.thread.isRunning)
+
const { t } = useI18n()
// Row structure is memoized on the STRUCTURAL signature only, so streaming
// part-appends can't churn group identity (that would defeat the rows memo
@@ -586,7 +597,13 @@ const ThreadMessageListInner: FC<ThreadMessageListProps> = ({
const hiddenCount = firstVisibleGroupIndex(
weightedGroups,
renderBudget,
- renderBudget >= paneBudget ? MIN_VISIBLE_GROUPS : 0
+ renderBudget >= paneBudget ? MIN_VISIBLE_GROUPS : 0,
+ // The active turn has to stay mounted regardless of size. Charging its
+ // growing weight to the history budget made this cut advance mid-stream:
+ // old rows unmounted, scrollHeight shrank, and the browser clamp looked
+ // like a user scroll-up to use-stick-to-bottom. Budget the history around
+ // the live turn instead, so token flushes cannot move the cut boundary.
+ threadRunning ? 1 : 0
)
// Memoized for IDENTITY, not to save the slice: `rows` below keys off this