Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/server/src/provider/acp/AcpAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/appPlaceLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
32 changes: 5 additions & 27 deletions apps/web/src/appPlaceLinks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Place links in the chat (docs/design/app-guide.md, "Place links in the chat"):
* what a `threadlines-place:<id>` 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:<id>` 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,
Expand All @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/ChatMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/chat/PlaceLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/settings/SettingsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/chatMarkdownDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions apps/web/src/placeArrival.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion apps/web/src/projectBrief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 8 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/appPlaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`;

Expand Down
7 changes: 2 additions & 5 deletions packages/shared/src/diffStats.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
54 changes: 2 additions & 52 deletions packages/shared/src/diffStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions packages/shared/src/placeHref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The scheme of a place link (`threadlines-place:<id>`), 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);
57 changes: 57 additions & 0 deletions packages/shared/src/textReplacementStats.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
6 changes: 6 additions & 0 deletions scripts/check-web-bundle-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading