perf: fix sustained-CPU hot paths from the July 2026 web performance audit

Addresses the audit's findings with minimal, desktop-first changes (no
payload deferral, no streaming-path work since streaming is off by default):

- Sidebar: replace the 10-thread bulk detail prewarm with a single
  hover-based prewarm; at idle one shell stream + at most one detail stream
- Server replay: thread catch-up reads only the thread's aggregate stream
  via the (aggregate_kind, stream_id, sequence) index instead of scanning
  the global log; shell catch-up is bounded (500 events) with a shell
  snapshot fallback for stale cursors
- Shell summaries: thread.message-sent updates latestUserMessageAt
  incrementally; activity-appended triggers the history-wide summary
  rebuild only for approval/user-input activity kinds; streaming assistant
  chunks no longer fan out per-chunk shell upserts
- Client runtime: O(1) activity appends when order is preserved; skip
  IndexedDB full-snapshot encodes while a turn is running (settle event and
  scope finalizer persist the final state)
- Timeline derivation: share one ordered activities view across the four
  derivations (identity-cached, O(n) sorted check); k-way merge for the
  combined timeline instead of concat+sort
- Composer drafts: debounce JSON serialization itself, and cache per-draft
  partialization by object identity so typing doesn't re-clone every
  retained draft
- Shiki: negative-cache languages without grammars (sh, yaml) so those
  fences stop retrying and throwing on every render
- Bootstrap polling: one shared change-detecting poller instead of one
  2s interval per consumer; minimap only writes data-in-view on change

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-09 04:09:08 -07:00
1 parent 3201e00ad4
commit bc23ec5073
24 files changed
+963 -173

No files matched your search

@@ -457,12 +457,21 @@ export function applyThreadDetailEvent(
// ── Activities ──────────────────────────────────────────────────
case "thread.activity-appended": {
const activities = pipe(
thread.activities,
Arr.filter((activity) => activity.id !== event.payload.activity.id),
Arr.append(event.payload.activity),
Arr.sort(activityOrder),
);
const activity = event.payload.activity;
// Live activities arrive in order and are new: keep the sorted invariant
// with a single append instead of filter+append+sort over the (possibly
// very long) history on every event.
const lastActivity = thread.activities.at(-1);
const activities =
(lastActivity === undefined || activityOrder(lastActivity, activity) <= 0) &&
!thread.activities.some((entry) => entry.id === activity.id)
? Arr.append(thread.activities, activity)
: pipe(
thread.activities,
Arr.filter((entry) => entry.id !== activity.id),
Arr.append(activity),
Arr.sort(activityOrder),
);
return {
kind: "updated",
@@ -5,6 +5,7 @@ import {
ProjectId,
ProviderInstanceId,
ThreadId,
TurnId,
type OrchestrationThread,
type OrchestrationThreadDetailSnapshot,
type OrchestrationThreadStreamItem,
@@ -304,6 +305,67 @@ describe("EnvironmentThreads", () => {
}),
);
it.effect("skips snapshot persistence while the session is running a turn", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: BASE_THREAD });
yield* Queue.offer(harness.inputs, snapshot(BASE_THREAD));
const sessionSet = (
status: "running" | "idle",
sequence: number,
): OrchestrationThreadStreamItem => ({
kind: "event",
event: {
eventId: EventId.make(`event-session-${sequence}`),
sequence,
occurredAt: "2026-04-01T01:00:00.000Z",
commandId: null,
causationEventId: null,
correlationId: null,
metadata: {},
aggregateKind: "thread",
aggregateId: THREAD_ID,
type: "thread.session-set",
payload: {
threadId: THREAD_ID,
session: {
threadId: THREAD_ID,
status,
providerName: null,
runtimeMode: "full-access",
activeTurnId: status === "running" ? TurnId.make("turn-1") : null,
lastError: null,
updatedAt: "2026-04-01T01:00:00.000Z",
},
},
},
});
yield* Queue.offer(harness.inputs, sessionSet("running", 2));
yield* Queue.offer(harness.inputs, titleUpdated("Streaming title", 3));
yield* awaitThreadState(
harness.observed,
(value) => Option.isSome(value.data) && value.data.value.title === "Streaming title",
);
yield* TestClock.adjust("500 millis");
yield* Effect.yieldNow;
const savedWhileRunning = yield* Ref.get(harness.savedThreads);
expect(savedWhileRunning.some((saved) => saved.thread.title === "Streaming title")).toBe(
false,
);
yield* Queue.offer(harness.inputs, sessionSet("idle", 4));
yield* awaitThreadState(
harness.observed,
(value) => Option.isSome(value.data) && value.data.value.session?.status === "idle",
);
yield* TestClock.adjust("500 millis");
yield* Effect.yieldNow;
const savedAfterSettle = yield* Ref.get(harness.savedThreads);
expect(savedAfterSettle.at(-1)?.thread.title).toBe("Streaming title");
expect(savedAfterSettle.at(-1)?.snapshotSequence).toBe(4);
}),
);
it.effect("seeds the thread from the HTTP snapshot and resumes live events", () =>
Effect.gen(function* () {
const httpThread: OrchestrationThread = { ...BASE_THREAD, title: "HTTP title" };
+8 -1
View File
@@ -129,7 +129,14 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
error: Option.none(),
});
// Persist the thread together with the sequence it reflects so the next warm
// cache can resume from exactly here.
// cache can resume from exactly here. While a turn is actively running,
// events arrive many times per second and each snapshot encode covers the
// whole thread history — skip those; the session leaving "running" (and the
// scope finalizer) persists the settled state, and a stale cache only means
// resuming via a slightly longer `afterSequence` replay.
if (thread.session?.status === "running") {
return;
}
const snapshotSequence = yield* SubscriptionRef.get(lastSequence);
yield* Queue.offer(persistence, { snapshotSequence, thread });
});