diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts index aeee73a255..5e94e5d470 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts @@ -179,8 +179,9 @@ function derivePendingUserInputCountFromActivities( // Activity kinds that can change the shell summary's pending-approval or // pending-user-input counters. All other activity kinds (command output, // file edits, streaming progress, ...) leave the summary untouched, so they -// must not trigger the history-wide summary rebuild. -const SHELL_SUMMARY_ACTIVITY_KINDS: ReadonlySet = new Set([ +// must not trigger the history-wide summary rebuild — and the shell stream +// (ws.ts toShellStreamEvent) skips re-broadcasting the thread row for them. +export const SHELL_SUMMARY_ACTIVITY_KINDS: ReadonlySet = new Set([ "approval.requested", "approval.resolved", "provider.approval.respond.failed", @@ -758,6 +759,14 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti } case "thread.activity-appended": { + // Non-summary activities (command output, file edits, progress, ...) + // change no shell-visible field: the sidebar timestamp prefers + // latestUserMessageAt and status pills derive from session/approval + // state. Skip the row churn entirely so high-volume activity streams + // don't bump updatedAt (and fan out shell upserts) per event. + if (!SHELL_SUMMARY_ACTIVITY_KINDS.has(event.payload.activity.kind)) { + return; + } const existingRow = yield* projectionThreadRepository.getById({ threadId: event.payload.threadId, }); @@ -768,9 +777,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti ...existingRow.value, updatedAt: event.occurredAt, }); - if (SHELL_SUMMARY_ACTIVITY_KINDS.has(event.payload.activity.kind)) { - yield* refreshThreadShellSummary(event.payload.threadId); - } + yield* refreshThreadShellSummary(event.payload.threadId); return; } diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 67bb74225c..b9385154ec 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -68,6 +68,7 @@ import * as ServerConfig from "./config.ts"; import * as Keybindings from "./keybindings.ts"; import * as ExternalLauncher from "./process/externalLauncher.ts"; import { normalizeDispatchCommand } from "./orchestration/Normalizer.ts"; +import { SHELL_SUMMARY_ACTIVITY_KINDS } from "./orchestration/Layers/ProjectionPipeline.ts"; import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts"; import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts"; import { @@ -677,6 +678,15 @@ const makeWsRpcLayer = ( ) { return Effect.succeed(Option.none()); } + // Same for high-volume activity kinds (command output, file edits, + // progress): the projection skips the thread row for them, so a + // shell broadcast would be a no-op re-send of unchanged data. + if ( + event.type === "thread.activity-appended" && + !SHELL_SUMMARY_ACTIVITY_KINDS.has(event.payload.activity.kind) + ) { + return Effect.succeed(Option.none()); + } return projectionSnapshotQuery .getThreadShellById(ThreadId.make(event.aggregateId)) .pipe( diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 996b383c66..0f6a7e1f1e 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -142,7 +142,10 @@ const highlightedCodeCache = new LRUCache( const highlighterPromiseCache = new Map>(); // Languages Shiki has no grammar for (e.g. `sh` fences). Without this negative // cache every render of such a block retries the grammar load and throws again. +// Fence labels are arbitrary text, so cap both caches: past the cap, unknown +// languages just retry (the pre-cache behavior) instead of growing memory. const fallbackHighlightLanguages = new Set(); +const MAX_FALLBACK_HIGHLIGHT_LANGUAGES = 100; function findTaskListMarkerOffset(markdown: string, listItemStart: number): number | null { const firstLineEnd = markdown.indexOf("\n", listItemStart); @@ -276,6 +279,9 @@ function estimateHighlightedSize(html: string, code: string): number { } function getHighlighterPromise(language: string): Promise { + if (fallbackHighlightLanguages.has(language)) { + return getHighlighterPromise("text"); + } const cached = highlighterPromiseCache.get(language); if (cached) return cached; @@ -284,15 +290,19 @@ function getHighlighterPromise(language: string): Promise { langs: [language as SupportedLanguages], preferredHighlighter: "shiki-js", }).catch((err) => { + // Failed loads never stay in the promise cache: unsupported languages are + // remembered in the (bounded) negative cache instead, and transient + // failures for real languages stay retryable. + highlighterPromiseCache.delete(language); if (language === "text") { // "text" itself failed — Shiki cannot initialize at all, surface the error - highlighterPromiseCache.delete(language); throw err; } - // Language not supported by Shiki — remember that and fall back to "text", - // keeping the resolved promise cached so the grammar load isn't retried on - // every render of a block with that language. - fallbackHighlightLanguages.add(language); + // Language not supported by Shiki — remember that and fall back to "text" + // so blocks with that language stop retrying the grammar load per render. + if (fallbackHighlightLanguages.size < MAX_FALLBACK_HIGHLIGHT_LANGUAGES) { + fallbackHighlightLanguages.add(language); + } return getHighlighterPromise("text"); }); highlighterPromiseCache.set(language, promise); @@ -694,13 +704,14 @@ function UncachedShikiCodeBlock({ try { return highlighter.codeToHtml(code, { lang: effectiveLanguage, theme: themeName }); } catch (error) { - // Log highlighting failures for debugging while falling back to plain text + // Log highlighting failures for debugging while falling back to plain + // text. Render-time throws can be content-specific, so they don't join + // the negative cache — one pathological block must not disable + // highlighting for the whole language. Retries are bounded by the memo. console.warn( `Code highlighting failed for language "${language}", falling back to plain text.`, error instanceof Error ? error.message : error, ); - // Remember the failure so subsequent renders skip the throwing path. - fallbackHighlightLanguages.add(language); return highlighter.codeToHtml(code, { lang: "text", theme: themeName }); } }, [code, highlighter, language, themeName]); diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index f0eee3769f..1c4de4480c 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -260,7 +260,9 @@ function SidebarHoverThreadPrewarmer() { onPrewarmTargetChange: setPrewarmThreadKey, }); const onPointerOver = (event: globalThis.PointerEvent) => { - const target = event.target instanceof HTMLElement ? event.target : null; + // Element, not HTMLElement: row icons are SVG and must still resolve to + // their row's key instead of cancelling the hover. + const target = event.target instanceof Element ? event.target : null; controller.hover( target?.closest("[data-thread-prewarm-key]")?.getAttribute("data-thread-prewarm-key") ?? null,