mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-05 07:54:38 +00:00
* perf(app): stop re-walking the timeline on every streamed delta Every coalesced delta rebuilds the stream render model with the same tail. The model re-sliced the tail at the history-window start on every call, so the rendered tail was a fresh array per delta and every WeakMap keyed on it (ordering, history split, turn timing, the layout history cache, row revision) missed each time. Cache the slice by tail identity and window start. Turn timing then still walked the whole rendered tail per delta and allocated a new assistant-timing Map, which the layout memo depends on. Split the walk: the tail is walked once per tail identity and the head is walked from the carried open turn per delta. The Map keeps its identity while the head adds no completed turn. Adds identity-contract tests and a vitest bench for the per-delta pipeline. 200 deltas over a 2,000-item timeline, mean per run: default mounted window 5.68ms -> 2.82ms, history fully revealed 16.95ms -> 1.81ms. Fixes #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * perf(app): keep the stream view's per-delta work bounded by the head The chat outline concatenated tail and head, reduced over the result in the render body, and rebuilt jumpToPrompt on every delta even when the outline was disabled, which is every native viewport. Memoize the tail's contribution, scan only the head per delta, do nothing while disabled, make jumpToPrompt a stable event so the rail's memo holds, and republish the active prompt only when the index or reading position changes. The view built a Set of every mounted id plus the live head per delta for the outline's pending-jump check; key it on mounted history only and treat live head rows as always mounted. Split plugin projection so a head change does not re-project the tail, memoize the history-window boundary lookup, and read the turn presentation's submission flag with some() instead of allocating a filtered array. Refs #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(app): confirm full history before measuring streaming --------- Co-authored-by: Ivan Malison <IvanMalison@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
146 lines
4.1 KiB
TypeScript
146 lines
4.1 KiB
TypeScript
import type { StreamItem } from "@/types/stream";
|
|
import { startsNewTurn } from "@/agent-stream/turn-membership";
|
|
|
|
export interface TurnTiming {
|
|
completedAt: Date;
|
|
durationMs: number | null;
|
|
}
|
|
|
|
export interface StreamTurnTiming {
|
|
byAssistantId: Map<string, TurnTiming>;
|
|
runningStartedAt: Date | null;
|
|
}
|
|
|
|
interface OpenTurn {
|
|
userAt: Date | null;
|
|
lastItemAt: Date | null;
|
|
assistantIds: readonly string[];
|
|
previousItem: StreamItem | null;
|
|
}
|
|
|
|
interface HeadTurnTiming {
|
|
additions: Map<string, TurnTiming>;
|
|
byAssistantId: Map<string, TurnTiming>;
|
|
}
|
|
|
|
interface TailTurnTiming {
|
|
completed: Map<string, TurnTiming>;
|
|
openTurn: OpenTurn;
|
|
lastHead: HeadTurnTiming | null;
|
|
}
|
|
|
|
const EMPTY_OPEN_TURN: OpenTurn = {
|
|
userAt: null,
|
|
lastItemAt: null,
|
|
assistantIds: [],
|
|
previousItem: null,
|
|
};
|
|
|
|
function closeTurn(turn: OpenTurn, into: Map<string, TurnTiming>): void {
|
|
if (!turn.lastItemAt || turn.assistantIds.length === 0) {
|
|
return;
|
|
}
|
|
const timing: TurnTiming = {
|
|
completedAt: turn.lastItemAt,
|
|
durationMs: turn.userAt ? Math.max(0, turn.lastItemAt.getTime() - turn.userAt.getTime()) : null,
|
|
};
|
|
for (const id of turn.assistantIds) {
|
|
into.set(id, timing);
|
|
}
|
|
}
|
|
|
|
function walkTurns(
|
|
items: readonly StreamItem[],
|
|
from: OpenTurn,
|
|
into: Map<string, TurnTiming>,
|
|
): OpenTurn {
|
|
let userAt = from.userAt;
|
|
let lastItemAt = from.lastItemAt;
|
|
let assistantIds = [...from.assistantIds];
|
|
let previousItem = from.previousItem;
|
|
for (const item of items) {
|
|
if (startsNewTurn(item, previousItem)) {
|
|
closeTurn({ userAt, lastItemAt, assistantIds, previousItem }, into);
|
|
userAt = item.kind === "user_message" ? item.timestamp : null;
|
|
lastItemAt = null;
|
|
assistantIds = [];
|
|
}
|
|
lastItemAt = item.timestamp;
|
|
if (item.kind === "assistant_message") {
|
|
assistantIds.push(item.id);
|
|
}
|
|
previousItem = item;
|
|
}
|
|
return { userAt, lastItemAt, assistantIds, previousItem };
|
|
}
|
|
|
|
function areTimingsEqual(a: Map<string, TurnTiming>, b: Map<string, TurnTiming>): boolean {
|
|
if (a.size !== b.size) {
|
|
return false;
|
|
}
|
|
for (const [id, timing] of a) {
|
|
const other = b.get(id);
|
|
if (
|
|
!other ||
|
|
other.completedAt.getTime() !== timing.completedAt.getTime() ||
|
|
other.durationMs !== timing.durationMs
|
|
) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// The tail walk is cached by array identity so a streamed delta, which only replaces the
|
|
// head, walks the head from the carried open turn. The returned Map keeps its identity
|
|
// while the head adds no completed turn, so memos keyed on it do not miss per tick.
|
|
const tailTurnTimingCache = new WeakMap<StreamItem[], TailTurnTiming>();
|
|
|
|
function getTailTurnTiming(tail: StreamItem[]): TailTurnTiming {
|
|
const cached = tailTurnTimingCache.get(tail);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const completed = new Map<string, TurnTiming>();
|
|
const openTurn = walkTurns(tail, EMPTY_OPEN_TURN, completed);
|
|
const timing: TailTurnTiming = { completed, openTurn, lastHead: null };
|
|
tailTurnTimingCache.set(tail, timing);
|
|
return timing;
|
|
}
|
|
|
|
function resolveByAssistantId(
|
|
tail: TailTurnTiming,
|
|
additions: Map<string, TurnTiming>,
|
|
): Map<string, TurnTiming> {
|
|
if (additions.size === 0) {
|
|
return tail.completed;
|
|
}
|
|
if (tail.lastHead && areTimingsEqual(tail.lastHead.additions, additions)) {
|
|
return tail.lastHead.byAssistantId;
|
|
}
|
|
const byAssistantId = new Map(tail.completed);
|
|
for (const [id, timing] of additions) {
|
|
byAssistantId.set(id, timing);
|
|
}
|
|
tail.lastHead = { additions, byAssistantId };
|
|
return byAssistantId;
|
|
}
|
|
|
|
export function deriveStreamTurnTiming(params: {
|
|
isTurnActive: boolean;
|
|
activeTurnStartedAt: Date | null;
|
|
tail: StreamItem[];
|
|
head: StreamItem[];
|
|
}): StreamTurnTiming {
|
|
const tail = getTailTurnTiming(params.tail);
|
|
const additions = new Map<string, TurnTiming>();
|
|
const openTurn = walkTurns(params.head, tail.openTurn, additions);
|
|
if (!params.isTurnActive) {
|
|
closeTurn(openTurn, additions);
|
|
}
|
|
return {
|
|
byAssistantId: resolveByAssistantId(tail, additions),
|
|
runningStartedAt: params.isTurnActive ? params.activeTurnStartedAt : null,
|
|
};
|
|
}
|