Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-7227 perf(transcript): reuse timeline work for messages that have not changed (#8242)
Bohan Jianga60713570a32
diff --git a/packages/views/common/task-transcript/build-timeline.test.ts b/packages/views/common/task-transcript/build-timeline.test.tsindex 8dad6d8ffa..9fa202d79f 100644--- a/packages/views/common/task-transcript/build-timeline.test.ts+++ b/packages/views/common/task-transcript/build-timeline.test.ts@@ -150,3 +150,44 @@ describe("tool output completeness", () => {expect(others.some(isOutputTruncated)).toBe(false);});});++describe("reusing work across rebuilds", () => {+ it("reuses the item for a run whose messages have not changed", () => {+ // Redaction is the whole cost of building a timeline and a live run rebuilds+ // on every 100ms flush, so a message that has not changed must not be+ // scanned again (MUL-7227). Identity of the returned item is the observable+ // form of that: a fresh scan would produce a fresh object.+ const msgs = [message(1, "text", "hello"), message(2, "tool_result", "out")];++ const first = buildTimeline(msgs);+ const second = buildTimeline([...msgs]);++ expect(second[0]).toBe(first[0]);+ expect(second[1]).toBe(first[1]);+ });++ it("rebuilds a run that gained a fragment, so redaction still sees the merged text", () => {+ // The reuse key is the run's exact message list, precisely so a coalescing+ // run that grew is redacted again as one string. Split so neither half+ // matches alone: reusing the first half's item, or redacting the fragments+ // separately, would leave the key on screen.+ const first = message(1, "text", "key AKIA123456");+ const partial = buildTimeline([first]);+ expect(partial[0]?.content).toBe("key AKIA123456");++ const merged = buildTimeline([first, message(2, "text", "7890ABCDEF")]);++ expect(merged[0]?.content).toBe("key [REDACTED AWS KEY]");+ expect(merged[0]).not.toBe(partial[0]);+ });++ it("rebuilds when a message is replaced at the same seq", () => {+ // A fetch response is the authority and replaces what the cache held, so a+ // same-seq record with different content arrives as a different object.+ const before = buildTimeline([message(1, "text", "first body")]);+ const after = buildTimeline([message(1, "text", "second body")]);++ expect(before[0]?.content).toBe("first body");+ expect(after[0]?.content).toBe("second body");+ });+});diff --git a/packages/views/common/task-transcript/build-timeline.ts b/packages/views/common/task-transcript/build-timeline.tsindex 3557fa9697..8a8085fdb4 100644--- a/packages/views/common/task-transcript/build-timeline.ts+++ b/packages/views/common/task-transcript/build-timeline.ts@@ -68,20 +68,84 @@ export function isOutputTruncated(item: TimelineItem): boolean {);}+/**+ * Timeline items already built, keyed on the first message behind each one.+ *+ * Redaction is essentially the whole cost of building a timeline: coalescing a+ * 3000-message transcript takes ~0.1ms and redacting it ~20ms, because every+ * rule scans every byte of every body. A live run rebuilds its timeline on each+ * 100ms flush window, so that scan was repeating over the entire transcript+ * several times a second while all but its newest messages were byte for byte+ * what they had been on the previous pass (MUL-7227).+ *+ * A message is immutable once it lands — the realtime merge appends unseen seqs+ * and keeps the objects it already holds — so identity is a sound proof that a+ * body has not changed. Each entry records the exact messages it was built+ * from, and is reused only when that list still matches: a coalescing run that+ * gained a fragment is rebuilt, which is what keeps redaction applied to merged+ * text rather than to the pieces.+ *+ * Weak, so entries are collected with the messages they belong to. They hold+ * little: `String.replace` returns its input unchanged when nothing matches, so+ * a body with no secrets in it is shared rather than copied.+ */+const builtRuns = new WeakMap<+ TaskMessagePayload,+ { members: readonly TaskMessagePayload[]; item: TimelineItem }+>();++function sameMembers(a: readonly TaskMessagePayload[], b: readonly TaskMessagePayload[]): boolean {+ if (a.length !== b.length) return false;+ return a.every((message, index) => message === b[index]);+}++/** Merge one coalescing run into its item, exactly as `coalesceTimelineItems` would. */+function mergeRun(run: readonly TaskMessagePayload[]): TimelineItem {+ const first = run[0]!;+ let content = first.content;+ let createdAt = first.created_at;+ for (const message of run.slice(1)) {+ content = `${content ?? ""}${message.content ?? ""}`;+ createdAt = message.created_at ?? createdAt;+ }+ return {+ seq: first.seq,+ type: first.type,+ tool: first.tool,+ content,+ input: first.input,+ output: first.output,+ output_truncated: first.output_truncated,+ created_at: createdAt,+ };+}++function buildRun(run: readonly TaskMessagePayload[]): TimelineItem {+ const cached = builtRuns.get(run[0]!);+ if (cached && sameMembers(cached.members, run)) return cached.item;+ const item = redactTimelineItems([mergeRun(run)])[0]!;+ builtRuns.set(run[0]!, { members: [...run], item });+ return item;+}+/** Build a chronologically ordered timeline from raw task messages. */export function buildTimeline(msgs: TaskMessagePayload[]): TimelineItem[] {- const items: TimelineItem[] = [];- for (const msg of msgs) {- items.push({- seq: msg.seq,- type: msg.type,- tool: msg.tool,- content: msg.content,- input: msg.input,- output: msg.output,- output_truncated: msg.output_truncated,- created_at: msg.created_at,- });+ const sorted = [...msgs].sort((a, b) => a.seq - b.seq);+ const out: TimelineItem[] = [];+ let run: TaskMessagePayload[] = [];++ for (const msg of sorted) {+ const previous = run[run.length - 1];+ // Same rule as `canMergeStreamingText`, read off the messages: a run's type+ // is its first message's, and every member shares it.+ if (previous && (previous.type === "text" || previous.type === "thinking") && previous.type === msg.type) {+ run.push(msg);+ continue;+ }+ if (run.length > 0) out.push(buildRun(run));+ run = [msg];}- return redactTimelineItems(coalesceTimelineItems(items));+ if (run.length > 0) out.push(buildRun(run));++ return out;}