diff --git a/apps/server/src/provider/acp/AcpAdapter.ts b/apps/server/src/provider/acp/AcpAdapter.ts index 3d5097d07..e46e96484 100644 --- a/apps/server/src/provider/acp/AcpAdapter.ts +++ b/apps/server/src/provider/acp/AcpAdapter.ts @@ -37,7 +37,7 @@ import { isProviderPlanGateReply, } from "@threadlines/shared/providerPlan"; import { renderThreadContextSeed, withContextSeedPreamble } from "@threadlines/shared/contextSeed"; -import { countTextReplacementStats } from "@threadlines/shared/diffStats"; +import { countTextReplacementStats } from "@threadlines/shared/textReplacementStats"; import { randomUUIDv4 } from "@threadlines/shared/uuid"; import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; diff --git a/apps/web/src/appPlaceLinks.test.ts b/apps/web/src/appPlaceLinks.test.ts index 14b224e77..3a778f669 100644 --- a/apps/web/src/appPlaceLinks.test.ts +++ b/apps/web/src/appPlaceLinks.test.ts @@ -2,7 +2,8 @@ import { EnvironmentId } from "@threadlines/contracts"; import { placeHref } from "@threadlines/shared/appPlaces"; import { describe, expect, it } from "vite-plus/test"; -import { placeArrivalHash, placeIdFromArrivalHash, resolvePlaceLink } from "./appPlaceLinks"; +import { placeIdFromArrivalHash, resolvePlaceLink } from "./appPlaceLinks"; +import { placeArrivalHash } from "./placeArrival"; const THIS_MAC = EnvironmentId.make("environment-this-mac"); const WINDOWS_PC = EnvironmentId.make("environment-windows-pc"); diff --git a/apps/web/src/appPlaceLinks.ts b/apps/web/src/appPlaceLinks.ts index 4cee5bc79..af99af3f9 100644 --- a/apps/web/src/appPlaceLinks.ts +++ b/apps/web/src/appPlaceLinks.ts @@ -1,14 +1,14 @@ /** * Place links in the chat (docs/design/app-guide.md, "Place links in the chat"): - * what a `threadlines-place:` link does when clicked, and the `#place=` - * hash that tells the page it opens which place to highlight. Pure, so the - * chip (components/chat/PlaceLink.tsx) and the arrival hook - * (hooks/usePlaceArrival.ts) read the same answers. + * what a `threadlines-place:` link does when clicked, and which place a + * page's `#place=` hash asks to highlight (placeArrival.ts makes the hash). + * Pure, so the chip (components/chat/PlaceLink.tsx) and the arrival hook + * (hooks/usePlaceArrival.ts) read the same answers. It reads the place + * catalog, so keep it out of code that loads at startup. */ import type { EnvironmentId } from "@threadlines/contracts"; import { currentPlaceId, - PLACE_HREF_SCHEME, type PlaceId, placeChain, placeIdFromHref, @@ -32,9 +32,6 @@ export type PlaceLinkTarget = } | { readonly kind: "desktopOnly"; readonly placeId: PlaceId }; -/** Whether an href is a place link at all, known to this build or not. */ -export const isPlaceLinkHref = (href: string): boolean => href.startsWith(PLACE_HREF_SCHEME); - /** Where a place's value lives: its own scope, else the nearest holder's, else the computer. */ export function placeScope(placeId: PlaceId) { for (const place of placeChain(placeId).toReversed()) { @@ -110,25 +107,6 @@ export function resolvePlaceLink(input: { : here; } -/** - * The hash a place link opens its page with. `click` changes on every click, so - * clicking the same link again highlights the place again. - */ -export const placeArrivalHash = (placeId: PlaceId, click: number): string => - new URLSearchParams({ place: placeId, n: String(click) }).toString(); - -let placeArrivals = 0; - -/** - * The hash for opening a place now: each opening gets its own number, so - * opening the same place again (a chip, a settings search result) highlights - * it again. - */ -export function nextPlaceArrivalHash(placeId: PlaceId): string { - placeArrivals += 1; - return placeArrivalHash(placeId, placeArrivals); -} - /** The place a route hash (without its `#`) asks to highlight, or null. */ export function placeIdFromArrivalHash(hash: string): PlaceId | null { if (!hash.startsWith("place=")) return null; diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 7a18cfee2..6386676d6 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -89,7 +89,7 @@ import { } from "./pull-requests/PullRequestHoverCard"; import { pullRequestBadgeTone } from "./pull-requests/pullRequests.logic"; import { parsePullRequestUrl } from "@threadlines/shared/sourceControl"; -import { isPlaceLinkHref } from "../appPlaceLinks"; +import { isPlaceLinkHref } from "@threadlines/shared/placeHref"; import { isBrowserPanelHref } from "./browser/openInBrowserPanel"; import { type MarkdownFileLinkKind, diff --git a/apps/web/src/components/chat/PlaceLink.tsx b/apps/web/src/components/chat/PlaceLink.tsx index 30f52ae47..3b3f13a72 100644 --- a/apps/web/src/components/chat/PlaceLink.tsx +++ b/apps/web/src/components/chat/PlaceLink.tsx @@ -3,7 +3,8 @@ import { type PlaceId, placePage } from "@threadlines/shared/appPlaces"; import { useRouter } from "@tanstack/react-router"; import { type ComponentType, createElement, type ReactNode } from "react"; -import { nextPlaceArrivalHash, placeArrivalHash, resolvePlaceLink } from "../../appPlaceLinks"; +import { resolvePlaceLink } from "../../appPlaceLinks"; +import { nextPlaceArrivalHash, placeArrivalHash } from "../../placeArrival"; import { usePrimaryEnvironmentId } from "../../environments/primary"; import { isElectron } from "../../env"; import { selectEnvironmentState, useStore } from "../../store"; diff --git a/apps/web/src/components/settings/SettingsSearch.tsx b/apps/web/src/components/settings/SettingsSearch.tsx index fb93e51e9..240f21881 100644 --- a/apps/web/src/components/settings/SettingsSearch.tsx +++ b/apps/web/src/components/settings/SettingsSearch.tsx @@ -4,7 +4,8 @@ import { useNavigate } from "@tanstack/react-router"; import { ChevronRightIcon } from "lucide-react"; import { createContext, type ReactNode, useCallback, useContext, useMemo } from "react"; -import { nextPlaceArrivalHash, placeScope, resolvePlaceLink } from "../../appPlaceLinks"; +import { placeScope, resolvePlaceLink } from "../../appPlaceLinks"; +import { nextPlaceArrivalHash } from "../../placeArrival"; import { PLACE_COPIES_ATTRIBUTE } from "../../hooks/usePlaceArrival"; import { usePrimaryEnvironmentId } from "../../environments/primary/context"; import { isElectron } from "../../env"; diff --git a/apps/web/src/lib/chatMarkdownDocument.ts b/apps/web/src/lib/chatMarkdownDocument.ts index cf49334de..89469f316 100644 --- a/apps/web/src/lib/chatMarkdownDocument.ts +++ b/apps/web/src/lib/chatMarkdownDocument.ts @@ -13,7 +13,7 @@ import remarkMath from "remark-math"; import { type PlaceId, placePathLabel } from "@threadlines/shared/appPlaces"; -import { isPlaceLinkHref } from "../appPlaceLinks"; +import { isPlaceLinkHref } from "@threadlines/shared/placeHref"; import { parseChatFileReference } from "../fileViewerStore"; import { fileLinkMetaForPath, diff --git a/apps/web/src/placeArrival.ts b/apps/web/src/placeArrival.ts new file mode 100644 index 000000000..d4b5b4cb0 --- /dev/null +++ b/apps/web/src/placeArrival.ts @@ -0,0 +1,26 @@ +/** + * The `#place=` hash a page opens with to highlight a place + * (docs/design/app-guide.md, "Place links in the chat"). Apart from + * appPlaceLinks.ts, which reads the place catalog, so ways into a place that + * load at startup (the project menu's brief link) don't load the catalog. + */ +import type { PlaceId } from "@threadlines/shared/appPlaces"; + +/** + * The hash a place link opens its page with. `click` changes on every click, so + * clicking the same link again highlights the place again. + */ +export const placeArrivalHash = (placeId: PlaceId, click: number): string => + new URLSearchParams({ place: placeId, n: String(click) }).toString(); + +let placeArrivals = 0; + +/** + * The hash for opening a place now: each opening gets its own number, so + * opening the same place again (a chip, a settings search result) highlights + * it again. + */ +export function nextPlaceArrivalHash(placeId: PlaceId): string { + placeArrivals += 1; + return placeArrivalHash(placeId, placeArrivals); +} diff --git a/apps/web/src/projectBrief.ts b/apps/web/src/projectBrief.ts index bae1cedc6..52ae2a1c4 100644 --- a/apps/web/src/projectBrief.ts +++ b/apps/web/src/projectBrief.ts @@ -17,7 +17,7 @@ import { PROJECT_BRIEF_MAX_CHARS } from "@threadlines/contracts"; import type { PlaceId } from "@threadlines/shared/appPlaces"; import { useCallback } from "react"; -import { nextPlaceArrivalHash } from "./appPlaceLinks"; +import { nextPlaceArrivalHash } from "./placeArrival"; import { usePrimaryEnvironmentDescriptor } from "./environments/primary"; import { readEnvironmentConnection, useSavedEnvironmentRuntimeStore } from "./environments/runtime"; import { useProjectContextSnapshots } from "./projectContext"; diff --git a/packages/shared/package.json b/packages/shared/package.json index b12055044..c51910aac 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -287,6 +287,10 @@ "types": "./src/diffStats.ts", "import": "./src/diffStats.ts" }, + "./textReplacementStats": { + "types": "./src/textReplacementStats.ts", + "import": "./src/textReplacementStats.ts" + }, "./contextSeed": { "types": "./src/contextSeed.ts", "import": "./src/contextSeed.ts" @@ -447,6 +451,10 @@ "types": "./src/appPlaces.ts", "import": "./src/appPlaces.ts" }, + "./placeHref": { + "types": "./src/placeHref.ts", + "import": "./src/placeHref.ts" + }, "./placeSearch": { "types": "./src/placeSearch.ts", "import": "./src/placeSearch.ts" diff --git a/packages/shared/src/appPlaces.ts b/packages/shared/src/appPlaces.ts index d02c2f671..fd39f78a3 100644 --- a/packages/shared/src/appPlaces.ts +++ b/packages/shared/src/appPlaces.ts @@ -13,6 +13,7 @@ * not listed one by one: their section is the place, and its `about` says what * each row holds. */ +import { PLACE_HREF_SCHEME } from "./placeHref.ts"; import type { AgentInvitesMode, AgentThreadsMode, @@ -2094,7 +2095,7 @@ export function placeRoute(id: PlaceId): AppPlaceRoute { } /** The link scheme for places in chat markdown (the desktop app owns `threadlines://`). */ -export const PLACE_HREF_SCHEME = "threadlines-place:"; +export { PLACE_HREF_SCHEME }; export const placeHref = (id: PlaceId): string => `${PLACE_HREF_SCHEME}${id}`; diff --git a/packages/shared/src/diffStats.test.ts b/packages/shared/src/diffStats.test.ts index 00668253c..841a50515 100644 --- a/packages/shared/src/diffStats.test.ts +++ b/packages/shared/src/diffStats.test.ts @@ -1,10 +1,7 @@ import { describe, expect, it } from "vite-plus/test"; -import { - countStructuredPatchStats, - countTextReplacementStats, - countUnifiedDiffStats, -} from "./diffStats.ts"; +import { countStructuredPatchStats, countUnifiedDiffStats } from "./diffStats.ts"; +import { countTextReplacementStats } from "./textReplacementStats.ts"; describe("countUnifiedDiffStats", () => { it("counts added and removed lines, skipping file headers", () => { diff --git a/packages/shared/src/diffStats.ts b/packages/shared/src/diffStats.ts index 3ce4a8ede..d5d49c332 100644 --- a/packages/shared/src/diffStats.ts +++ b/packages/shared/src/diffStats.ts @@ -5,10 +5,9 @@ * runtime events (`payload.data.changes`) so the client can render exact * per-file +/- counts without waiting on checkpoint diffs. The client uses * the same counters to derive stats from raw unified diffs (e.g. Codex - * patch payloads). + * patch payloads). Counting a whole-text replacement needs a line diff, so it + * lives in textReplacementStats.ts, which only the server loads. */ -import { diffLines } from "diff"; - export interface FileChangeStat { readonly path: string; readonly kind?: "add" | "update" | "delete" | undefined; @@ -57,55 +56,6 @@ export function countUnifiedDiffStats(diff: string): DiffLineStats { /** Past this many changed lines, or this long, an exact line diff is not * worth blocking the event path for; the count falls back to an upper bound. */ -const TEXT_DIFF_MAX_EDIT_LENGTH = 2_000; -const TEXT_DIFF_TIMEOUT_MS = 100; - -/** Exact +/- lines for replacing `oldText` with `newText`; a missing - * `oldText` is a new file. Very large rewrites count every line between the - * common leading and trailing lines instead. */ -export function countTextReplacementStats( - oldText: string | null | undefined, - newText: string, -): DiffLineStats { - const changes = diffLines(oldText ?? "", newText, { - stripTrailingCr: true, - maxEditLength: TEXT_DIFF_MAX_EDIT_LENGTH, - timeout: TEXT_DIFF_TIMEOUT_MS, - }); - if (changes !== undefined) { - let additions = 0; - let deletions = 0; - for (const change of changes) { - if (change.added) additions += change.count; - else if (change.removed) deletions += change.count; - } - return { additions, deletions }; - } - const lines = (text: string) => { - if (text.length === 0) return []; - const split = text.replace(/\r\n/gu, "\n").split("\n"); - return split.at(-1) === "" ? split.slice(0, -1) : split; - }; - const before = lines(oldText ?? ""); - const after = lines(newText); - let prefix = 0; - while (prefix < before.length && prefix < after.length && before[prefix] === after[prefix]) { - prefix += 1; - } - let suffix = 0; - while ( - suffix < before.length - prefix && - suffix < after.length - prefix && - before[before.length - 1 - suffix] === after[after.length - 1 - suffix] - ) { - suffix += 1; - } - return { - additions: after.length - prefix - suffix, - deletions: before.length - prefix - suffix, - }; -} - /** Counts +/- lines across `structuredPatch` hunks as emitted by Claude * Code's file tools (jsdiff hunks: lines prefixed `+`, `-`, or a space). */ export function countStructuredPatchStats(hunks: unknown): DiffLineStats | null { diff --git a/packages/shared/src/placeHref.ts b/packages/shared/src/placeHref.ts new file mode 100644 index 000000000..225ff5e08 --- /dev/null +++ b/packages/shared/src/placeHref.ts @@ -0,0 +1,9 @@ +/** + * The scheme of a place link (`threadlines-place:`), apart from the place + * catalog in appPlaces.ts, so code that only needs to recognize a place link + * (the chat's markdown, at startup) doesn't load every place. + */ +export const PLACE_HREF_SCHEME = "threadlines-place:"; + +/** Whether an href is a place link at all, known to this build or not. */ +export const isPlaceLinkHref = (href: string): boolean => href.startsWith(PLACE_HREF_SCHEME); diff --git a/packages/shared/src/textReplacementStats.ts b/packages/shared/src/textReplacementStats.ts new file mode 100644 index 000000000..9865f82d8 --- /dev/null +++ b/packages/shared/src/textReplacementStats.ts @@ -0,0 +1,57 @@ +/** + * +/- line counts for replacing a whole file's text, which needs a line diff. + * Apart from diffStats.ts because the `diff` library it loads keeps module + * state the bundler can't drop, and the web app reads diffStats at startup. + */ +import { diffLines } from "diff"; + +import type { DiffLineStats } from "./diffStats.ts"; + +const TEXT_DIFF_MAX_EDIT_LENGTH = 2_000; +const TEXT_DIFF_TIMEOUT_MS = 100; + +/** Exact +/- lines for replacing `oldText` with `newText`; a missing + * `oldText` is a new file. Very large rewrites count every line between the + * common leading and trailing lines instead. */ +export function countTextReplacementStats( + oldText: string | null | undefined, + newText: string, +): DiffLineStats { + const changes = diffLines(oldText ?? "", newText, { + stripTrailingCr: true, + maxEditLength: TEXT_DIFF_MAX_EDIT_LENGTH, + timeout: TEXT_DIFF_TIMEOUT_MS, + }); + if (changes !== undefined) { + let additions = 0; + let deletions = 0; + for (const change of changes) { + if (change.added) additions += change.count; + else if (change.removed) deletions += change.count; + } + return { additions, deletions }; + } + const lines = (text: string) => { + if (text.length === 0) return []; + const split = text.replace(/\r\n/gu, "\n").split("\n"); + return split.at(-1) === "" ? split.slice(0, -1) : split; + }; + const before = lines(oldText ?? ""); + const after = lines(newText); + let prefix = 0; + while (prefix < before.length && prefix < after.length && before[prefix] === after[prefix]) { + prefix += 1; + } + let suffix = 0; + while ( + suffix < before.length - prefix && + suffix < after.length - prefix && + before[before.length - 1 - suffix] === after[after.length - 1 - suffix] + ) { + suffix += 1; + } + return { + additions: after.length - prefix - suffix, + deletions: before.length - prefix - suffix, + }; +} diff --git a/scripts/check-web-bundle-size.ts b/scripts/check-web-bundle-size.ts index 719017231..8959ee40d 100644 --- a/scripts/check-web-bundle-size.ts +++ b/scripts/check-web-bundle-size.ts @@ -61,6 +61,12 @@ const DEFAULT_ENTRY_GZIP_BUDGET_BYTES = 255_000; // catalog loads with Settings, not at startup (about 14 KB), and the // Antigravity icon's PNG became a file of its own while the editor and // Jujutsu icons moved to ToolIcons.tsx (about 17 KB). +// Measured 894,411 B here (2026-11-20), down from 912,236 B: the project +// menu's brief link had pulled the place catalog back into startup through +// appPlaceLinks.ts (its arrival hash now comes from placeArrival.ts, and the +// chat's place-link check from @threadlines/shared/placeHref), about 13.5 KB; +// and the `diff` library left diffStats.ts for textReplacementStats.ts, which +// only the server loads, about 4.3 KB. const DEFAULT_STARTUP_GZIP_BUDGET_BYTES = 915_000; const entryBudgetBytes = Number(