Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(plan): show inline comments in large statement previews (#21538)

boojackf520a869841d
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 8a133fa328..b8d70a1794 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1544,6 +1544,7 @@
"sort": "Sort",
"updated": "Update time"
},
+ "statement-exceeds-preview-limit": "This statement exceeds the preview limit. After reloading, only a preview will be shown.",
"statement-from-sheet-warning": "This statement is loaded from a large sheet. Preview may be truncated.",
"status-transition": {
"modal": {
@@ -1826,6 +1827,7 @@
"add-comment-on-line": "Add a comment on line {{line}}",
"add-comment-on-lines": "Add a comment on lines {{startLine}} to {{endLine}}",
"anchor": {
+ "comparison-size-limit": "The original or current statement exceeds the comparison limit.",
"line_one": "Line {{range}}",
"line_other": "Lines {{range}}",
"outdated": "Outdated",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index f827005433..6ca550a655 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1544,6 +1544,7 @@
"sort": "Ordenar",
"updated": "Fecha de actualización"
},
+ "statement-exceeds-preview-limit": "Esta sentencia supera el límite de vista previa. Después de recargar, solo se mostrará una vista previa.",
"statement-from-sheet-warning": "Esta sentencia se carga desde una hoja de gran tamaño. La previsualización puede estar truncada.",
"status-transition": {
"modal": {
@@ -1826,6 +1827,7 @@
"add-comment-on-line": "Añadir un comentario en la línea {{line}}",
"add-comment-on-lines": "Añadir un comentario en las líneas {{startLine}} a {{endLine}}",
"anchor": {
+ "comparison-size-limit": "La sentencia original o actual supera el límite de comparación.",
"line_one": "Línea {{range}}",
"line_other": "Líneas {{range}}",
"outdated": "Obsoleto",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 98dba6345a..dcab0dfb32 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1544,6 +1544,7 @@
"sort": "並べ替え",
"updated": "更新日時"
},
+ "statement-exceeds-preview-limit": "このステートメントはプレビューの上限を超えています。再読み込み後はプレビューのみ表示されます。",
"statement-from-sheet-warning": "このステートメントは大きなシートから読み込まれています。プレビューは切り詰められる場合があります。",
"status-transition": {
"modal": {
@@ -1826,6 +1827,7 @@
"add-comment-on-line": "{{line}} 行目にコメントを追加",
"add-comment-on-lines": "{{startLine}}~{{endLine}} 行目にコメントを追加",
"anchor": {
+ "comparison-size-limit": "元のステートメントまたは現在のステートメントが比較の上限を超えています。",
"line_one": "{{range}} 行目",
"line_other": "{{range}} 行",
"outdated": "最新ではありません",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 1969147d2e..82611e7385 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1544,6 +1544,7 @@
"sort": "Sắp xếp",
"updated": "Thời gian cập nhật"
},
+ "statement-exceeds-preview-limit": "Câu lệnh này vượt quá giới hạn xem trước. Sau khi tải lại, chỉ phần xem trước được hiển thị.",
"statement-from-sheet-warning": "Câu lệnh này được tải từ một sheet lớn. Bản xem trước có thể bị cắt ngắn.",
"status-transition": {
"modal": {
@@ -1826,6 +1827,7 @@
"add-comment-on-line": "Thêm bình luận vào dòng {{line}}",
"add-comment-on-lines": "Thêm bình luận vào các dòng từ {{startLine}} đến {{endLine}}",
"anchor": {
+ "comparison-size-limit": "Câu lệnh gốc hoặc hiện tại vượt quá giới hạn so sánh.",
"line_one": "Dòng {{range}}",
"line_other": "Dòng {{range}}",
"outdated": "Đã lỗi thời",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 1e737743c8..f706aa0307 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1544,6 +1544,7 @@
"sort": "排序",
"updated": "更新时间"
},
+ "statement-exceeds-preview-limit": "该语句超出预览限制。重新加载后将只显示预览内容。",
"statement-from-sheet-warning": "该 SQL 来自较大的 Sheet,预览内容可能已截断。",
"status-transition": {
"modal": {
@@ -1826,6 +1827,7 @@
"add-comment-on-line": "在第 {{line}} 行添加评论",
"add-comment-on-lines": "在第 {{startLine}} 至 {{endLine}} 行添加评论",
"anchor": {
+ "comparison-size-limit": "原始语句或当前语句超出比较限制。",
"line_one": "第 {{range}} 行",
"line_other": "第 {{range}} 行",
"outdated": "已过期",
diff --git a/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.test.tsx b/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.test.tsx
index 37fb359976..1f5ed8e4b3 100644
--- a/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.test.tsx
+++ b/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.test.tsx
@@ -19,6 +19,10 @@ import {
const mocks = vi.hoisted(() => ({
issueComments: [] as unknown[],
+ sheetsByName: {} as Record<
+ string,
+ { content: Uint8Array; contentSize: bigint; name: string }
+ >,
fetchDatabases: vi.fn(),
fetchDBGroupListByProjectName: vi.fn(),
getDatabaseByName: vi.fn(),
@@ -318,6 +322,7 @@ vi.mock("@/stores/app", async () => {
dbGroupViewByName: Record<string, unknown>;
projectsByName: Record<string, unknown>;
environmentList: unknown[];
+ sheetsByName: typeof mocks.sheetsByName;
getIssueComments: (issueName: string) => unknown[];
}) => unknown
) =>
@@ -327,6 +332,7 @@ vi.mock("@/stores/app", async () => {
dbGroupViewByName,
projectsByName,
environmentList: [],
+ sheetsByName: mocks.sheetsByName,
getIssueComments: () => mocks.issueComments,
}),
{
@@ -542,6 +548,7 @@ beforeEach(() => {
vi.clearAllMocks();
mocks.localSheets.clear();
mocks.issueComments = [];
+ mocks.sheetsByName = {};
mocks.getPlanOptionVisibility.mockReturnValue({
shouldShow: false,
showGhost: false,
@@ -726,7 +733,7 @@ describe("PlanDetailChangesBranch", () => {
{ id: "spec-orders", config: { case: "changeDatabaseConfig", value: { targets: [DB_WIDGETS], sheet: `projects/foo/sheets/${sha}` } } },
{ id: "spec-cogs", config: { case: "changeDatabaseConfig", value: { targets: [DB_COGS], sheet: `projects/foo/sheets/${sha}` } } },
] as unknown as PlanDetailPageState["plan"]["specs"];
- const thread = (id: string, spec: string, opts: { resolved?: boolean; sheetSha256?: string } = {}) => ({
+ const thread = (id: string, spec: string, opts: { resolved?: boolean; sheetSha256?: string; line?: number } = {}) => ({
name: `projects/foo/issues/1/issueComments/${id}`,
comment: id,
threadState: opts.resolved
@@ -735,13 +742,13 @@ describe("PlanDetailChangesBranch", () => {
statementAnchor: {
spec,
sheetSha256: opts.sheetSha256 ?? sha,
- startPosition: { line: 1, column: 0 },
- endPosition: { line: 1, column: 0 },
+ startPosition: { line: opts.line ?? 1, column: 0 },
+ endPosition: { line: opts.line ?? 1, column: 0 },
},
});
mocks.issueComments = [
thread("a", "spec-orders"),
- thread("b", "spec-orders"),
+ thread("b", "spec-orders", { line: 2 }),
thread("c", "spec-orders", { resolved: true }),
// Anchored to an older statement: unresolved, but not shown in the editor.
thread("stale", "spec-orders", { sheetSha256: "b".repeat(64) }),
@@ -761,6 +768,23 @@ describe("PlanDetailChangesBranch", () => {
const pills = [...container.querySelectorAll("[data-testid='spec-unresolved-threads']")];
expect(pills.map((pill) => pill.textContent)).toEqual(["2"]);
expect(pills[0].closest("[aria-label]")?.getAttribute("aria-label")).toContain("widgets");
+
+ const content = new TextEncoder().encode("first\nsecond");
+ mocks.sheetsByName = {
+ [`projects/foo/sheets/${sha}`]: {
+ name: `projects/foo/sheets/${sha}`,
+ content,
+ contentSize: BigInt(content.byteLength + 1),
+ },
+ };
+ act(() => {
+ root.render(
+ <PlanDetailProvider value={page}>
+ <PlanDetailChangesBranch selectedSpecId="spec-orders" onSelectedSpecIdChange={vi.fn()} />
+ </PlanDetailProvider>
+ );
+ });
+ expect(container.querySelector("[data-testid='spec-unresolved-threads']")?.textContent).toBe("1");
});
it("renders target-derived change references instead of generic types", async () => {
diff --git a/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.tsx b/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.tsx
index c7193cf12a..48b71c6d16 100644
--- a/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.tsx
+++ b/frontend/src/routes/project/plan-detail/components/PlanDetailChangesBranch.tsx
@@ -207,7 +207,8 @@ export function PlanDetailChangesBranch({
const currentUser = useCurrentUser();
const unresolvedThreadsBySpec = usePlacedUnresolvedThreadCounts(
page.issue?.name,
- page.plan.specs
+ page.plan.specs,
+ `projects/${page.projectId}`
);
// subscribe to re-render on project cache change
const projectsByName = useAppStore((s) => s.projectsByName);
diff --git a/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.test.tsx b/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.test.tsx
new file mode 100644
index 0000000000..c8684afe1c
--- /dev/null
+++ b/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.test.tsx
@@ -0,0 +1,141 @@
+import { create } from "@bufbuild/protobuf";
+import { cleanup, fireEvent, render, waitFor } from "@testing-library/react";
+import { afterEach, expect, test, vi } from "vitest";
+import {
+ Plan_ChangeDatabaseConfigSchema,
+ Plan_SpecSchema,
+ PlanSchema,
+} from "@/types/proto-es/v1/plan_service_pb";
+import { type Sheet, SheetSchema } from "@/types/proto-es/v1/sheet_service_pb";
+import { PlanDetailStatementSection } from "./PlanDetailStatementSection";
+
+const mocks = vi.hoisted(() => ({
+ page: undefined as unknown,
+ sheets: new Map<string, Sheet>(),
+}));
+
+vi.mock("react-i18next", () => ({
+ initReactI18next: { type: "3rdParty", init: () => {} },
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+vi.mock("@/components/monaco", () => ({
+ MonacoEditor: ({ onChange }: { onChange: (value: string) => void }) => (
+ <>
+ <button onClick={() => onChange("SELECT 2;\n".repeat(220_000))}>
+ Make large draft
+ </button>
+ <button onClick={() => onChange("SELECT 2;")}>Make small draft</button>
+ </>
+ ),
+ ReadonlyMonaco: ({ content }: { content: string }) => (
+ <div data-testid="statement">{content.slice(0, 24)}</div>
+ ),
+}));
+vi.mock("@/stores", () => ({ pushNotification: vi.fn() }));
+vi.mock("@/stores/app", () => ({
+ useAppStore: Object.assign(
+ (selector: (state: { databasesByName: object }) => unknown) =>
+ selector({ databasesByName: {} }),
+ {
+ getState: () => ({
+ getSheetByName: (name: string) => mocks.sheets.get(name),
+ getOrFetchSheetByName: vi.fn(),
+ }),
+ }
+ ),
+}));
+vi.mock("../shell/PlanDetailContext", () => ({
+ usePlanDetailContext: () => mocks.page,
+}));
+vi.mock("./SchemaEditorSheet", () => ({ SchemaEditorSheet: () => null }));
+vi.mock("./threads/StatementThreadsLayer", () => ({
+ StatementThreadsLayer: () => null,
+}));
+
+afterEach(() => {
+ cleanup();
+ mocks.sheets.clear();
+});
+
+test("shows the large-sheet warning as soon as the saved sheet changes", async () => {
+ const smallName = `projects/p/sheets/${"a".repeat(64)}`;
+ const largeName = `projects/p/sheets/${"b".repeat(64)}`;
+ const specOf = (sheet: string) =>
+ create(Plan_SpecSchema, {
+ id: "spec",
+ config: {
+ case: "changeDatabaseConfig",
+ value: create(Plan_ChangeDatabaseConfigSchema, { sheet }),
+ },
+ });
+ const smallSpec = specOf(smallName);
+ const largeSpec = specOf(largeName);
+ const addSheet = (name: string, statement: string) => {
+ const content = new TextEncoder().encode(statement);
+ mocks.sheets.set(
+ name,
+ create(SheetSchema, { name, content, contentSize: BigInt(content.length) })
+ );
+ };
+ addSheet(smallName, "SELECT 1;");
+ addSheet(largeName, "SELECT 2;\n".repeat(220_000));
+ const page = {
+ currentUser: { name: "users/test" },
+ isCreating: false,
+ plan: create(PlanSchema, { specs: [smallSpec] }),
+ project: undefined,
+ readonly: true,
+ setEditing: vi.fn(),
+ };
+ mocks.page = page;
+
+ const { queryByText, rerender, getByTestId } = render(
+ <PlanDetailStatementSection spec={smallSpec} />
+ );
+ expect(queryByText("issue.statement-from-sheet-warning")).toBeNull();
+
+ mocks.page = {
+ ...page,
+ plan: create(PlanSchema, { specs: [largeSpec] }),
+ };
+ rerender(<PlanDetailStatementSection spec={largeSpec} />);
+ await waitFor(() =>
+ expect(queryByText("issue.statement-from-sheet-warning")).not.toBeNull()
+ );
+ expect(getByTestId("statement").textContent).toContain("SELECT 2;");
+});
+
+test("shows a draft-specific warning before saving and removes it below the limit", () => {
+ const sheetName = `projects/p/sheets/${"c".repeat(64)}`;
+ const spec = create(Plan_SpecSchema, {
+ id: "spec",
+ config: {
+ case: "changeDatabaseConfig",
+ value: create(Plan_ChangeDatabaseConfigSchema, { sheet: sheetName }),
+ },
+ });
+ const content = new TextEncoder().encode("SELECT 1;");
+ mocks.sheets.set(
+ sheetName,
+ create(SheetSchema, { name: sheetName, content, contentSize: BigInt(content.length) })
+ );
+ mocks.page = {
+ currentUser: { name: "users/test" },
+ isCreating: false,
+ plan: create(PlanSchema, { creator: "users/test", specs: [spec] }),
+ project: { name: "projects/p" },
+ readonly: false,
+ setEditing: vi.fn(),
+ };
+
+ const { getByText, queryByText } = render(
+ <PlanDetailStatementSection spec={spec} />
+ );
+ fireEvent.click(getByText("common.edit"));
+ fireEvent.click(getByText("Make large draft"));
+ expect(getByText("issue.statement-exceeds-preview-limit")).toBeTruthy();
+ expect(queryByText("common.download")).toBeNull();
+
+ fireEvent.click(getByText("Make small draft"));
+ expect(queryByText("issue.statement-exceeds-preview-limit")).toBeNull();
+});
diff --git a/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.tsx b/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.tsx
index c481d61eb6..0ea474b88f 100644
--- a/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.tsx
+++ b/frontend/src/routes/project/plan-detail/components/PlanDetailStatementSection.tsx
@@ -45,6 +45,7 @@ import { getStatementSize, MAX_UPLOAD_FILE_SIZE_MB } from "@/utils/sheet";
import { getInstanceResource } from "@/utils/v1/database";
import { sheetNameOfSpec } from "@/utils/v1/issue/plan";
import {
+ exceedsSheetPreviewLimit,
extractSheetUID,
getSheetStatement,
setSheetStatement,
@@ -59,7 +60,10 @@ import {
import { getSQLAdviceMarkers } from "../utils/sqlAdvice";
import { SchemaEditorSheet } from "./SchemaEditorSheet";
import { StatementThreadsLayer } from "./threads/StatementThreadsLayer";
-import { sheetSha256OfName } from "./threads/threadModel";
+import {
+ completePreviewLineCount,
+ sheetSha256OfName,
+} from "./threads/threadModel";
// Both modes reserve the same gutter so line numbers and SQL stay aligned
// when entering or leaving edit mode. Keep these options stable: MonacoEditor
@@ -483,16 +487,34 @@ export function PlanDetailStatementSection({
const editorContent = page.isCreating ? statement : draftStatement;
- // Inline threads anchor to the saved sheet of a persisted spec, so they
- // need an issue, a content-addressed sheet, and the complete statement.
+ // Inline threads anchor to the saved sheet of a persisted spec. A truncated
+ // preview can still show threads on its complete lines.
const sheetSha256 = sheetSha256OfName(sheetName);
const issue = page.issue;
+ const showLargeSheetWarning = useMemo(
+ () =>
+ isEditing
+ ? exceedsSheetPreviewLimit(draftStatement)
+ : isSheetOversize ||
+ (!isLoading &&
+ Boolean(sheetSha256) &&
+ exceedsSheetPreviewLimit(statement)),
+ [
+ draftStatement,
+ isEditing,
+ isLoading,
+ isSheetOversize,
+ sheetSha256,
+ statement,
+ ]
+ );
+ const previewEndLine = useMemo(
+ () =>
+ isSheetOversize ? completePreviewLineCount(statement, true) : undefined,
+ [isSheetOversize, statement]
+ );
const threadsEnabled = Boolean(
- issue &&
- !page.isCreating &&
- !isPendingDraft &&
- sheetSha256 &&
- !isSheetOversize
+ issue && !page.isCreating && !isPendingDraft && sheetSha256
);
return (
@@ -574,13 +596,17 @@ export function PlanDetailStatementSection({
</div>
)}
</div>
- {isSheetOversize && (
+ {showLargeSheetWarning && (
<Alert
variant="warning"
description={
<div className="flex items-center justify-between gap-x-4">
- <span>{t("issue.statement-from-sheet-warning")}</span>
- {sheetName && (
+ <span>
+ {isEditing
+ ? t("issue.statement-exceeds-preview-limit")
+ : t("issue.statement-from-sheet-warning")}
+ </span>
+ {!isEditing && sheetName && (
<Button
disabled={isDownloading}
onClick={() => void downloadSheet()}
@@ -641,6 +667,7 @@ export function PlanDetailStatementSection({
issue={issue}
key={sheetSha256}
monaco={readonlyEditor.monaco}
+ previewEndLine={previewEndLine}
sheetSha256={sheetSha256}
spec={spec}
/>
diff --git a/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.test.tsx b/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.test.tsx
index 7521b7c52c..859ffc4574 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.test.tsx
+++ b/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.test.tsx
@@ -1,10 +1,11 @@
import { create } from "@bufbuild/protobuf";
import { cleanup, fireEvent, render, waitFor } from "@testing-library/react";
+import type { ReactNode } from "react";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { Plan_ChangeDatabaseConfigSchema, Plan_SpecSchema, PlanSchema } from "@/types/proto-es/v1/plan_service_pb";
import { ProjectSchema } from "@/types/proto-es/v1/project_service_pb";
import { type Sheet, SheetSchema } from "@/types/proto-es/v1/sheet_service_pb";
-import { current, OUTDATED } from "./placement/place";
+import { current, OUTDATED, UNAVAILABLE, UNAVAILABLE_SIZE_LIMIT } from "./placement/place";
import { StatementAnchorContext } from "./StatementAnchorContext";
import { buildWholeLineAnchor } from "./threadModel";
@@ -19,6 +20,11 @@ vi.mock("react-i18next", () => ({
options ? `${(options.count ?? 1) > 1 ? "Lines" : "Line"} ${options.range}` : key}),
}));
vi.mock("@/components/monaco/core", () => ({ colorizeStatement: (text: string) => mocks.colorize(text) }));
+vi.mock("@/components/ui/tooltip", () => ({
+ Tooltip: ({ children, content }: { children: ReactNode; content: string }) => (
+ <span data-tooltip={content}>{children}</span>
+ ),
+}));
vi.mock("@/stores/app", () => ({
useAppStore: Object.assign(
(selector: (state: unknown) => unknown) => selector({ sheetsByName: mocks.sheets }),
@@ -115,22 +121,53 @@ describe("StatementAnchorContext", () => {
test("mapped placements still render context from the recorded revision", () => {
const { container, getByText } = render(<StatementAnchorContext {...props} anchor={anchor(4, 5)} placement={current(10, 11)} />);
expect(getByText("Lines 4–5")).toBeTruthy();
+ expect(getByText("plan.review.thread.anchor.view-in-statement")).toBeTruthy();
expect(container.textContent).toContain("comment line");
expect(container.textContent).not.toContain("CURRENT REVISION");
});
+ test("shows Outdated with a reason when the comparison exceeds the limit", () => {
+ const { container, rerender } = render(
+ <StatementAnchorContext {...props} anchor={anchor(4, 5)} placement={UNAVAILABLE_SIZE_LIMIT} />
+ );
+ expect(container.querySelector("[data-anchor-state]")?.getAttribute("data-anchor-state")).toBe("OUTDATED");
+ expect(container.textContent).toContain("plan.review.thread.anchor.outdated");
+ expect(container.textContent).not.toContain("plan.review.thread.anchor.view-in-statement");
+ expect(container.querySelector("[data-tooltip]")?.getAttribute("data-tooltip")).toBe(
+ "plan.review.thread.anchor.comparison-size-limit"
+ );
+ rerender(<StatementAnchorContext {...props} anchor={anchor(4, 5)} placement={UNAVAILABLE} />);
+ expect(container.querySelector("[data-anchor-state]")?.getAttribute("data-anchor-state")).toBe("OUTDATED");
+ expect(container.querySelector("[data-tooltip]")).toBeNull();
+ });
+
+ test("keeps the anchor unavailable when its original lines are outside the preview", () => {
+ mocks.sheets[savedName] = create(SheetSchema, {
+ name: savedName,
+ content: new TextEncoder().encode("SELECT 1;"),
+ });
+ const { container } = render(
+ <StatementAnchorContext {...props} anchor={anchor(4, 5)} placement={UNAVAILABLE_SIZE_LIMIT} />
+ );
+ expect(container.querySelector("[data-anchor-state]")?.getAttribute("data-anchor-state")).toBe("UNAVAILABLE");
+ expect(container.textContent).toContain("plan.review.thread.anchor.statement-unavailable");
+ expect(container.querySelector("[data-tooltip]")).toBeNull();
+ });
+
test.each([
- { sheet: "complete", text: original, extra: 0, state: "CURRENT", action: true },
- { sheet: "truncated", text: original, extra: 1, state: "UNAVAILABLE", action: false },
- { sheet: "empty", text: "", extra: 0, state: "UNAVAILABLE", action: false },
- ])("a hash-matched anchor on a $sheet sheet is $state", ({ text, extra, state, action }) => {
+ { sheet: "complete", text: original, extra: 0, start: 1, end: 1, state: "CURRENT", action: true },
+ { sheet: "visible preview line", text: "SELECT 1;\nSELECT", extra: 1, start: 1, end: 1, state: "CURRENT", action: true },
+ { sheet: "partial preview line", text: "SELECT 1;\nSELECT", extra: 1, start: 2, end: 2, state: "UNAVAILABLE", action: false },
+ { sheet: "range crossing the preview end", text: "SELECT 1;\nSELECT", extra: 1, start: 1, end: 2, state: "UNAVAILABLE", action: false },
+ { sheet: "empty", text: "", extra: 0, start: 1, end: 1, state: "UNAVAILABLE", action: false },
+ ])("a hash-matched anchor on a $sheet sheet is $state", ({ text, extra, start, end, state, action }) => {
const content = new TextEncoder().encode(text);
mocks.sheets[currentName] = create(SheetSchema, {
name: currentName,
content,
contentSize: BigInt(content.byteLength + extra),
});
- const onCurrent = buildWholeLineAnchor({ spec: "spec", sheetSha256: currentHash, startLine: 1, endLine: 1 });
+ const onCurrent = buildWholeLineAnchor({ spec: "spec", sheetSha256: currentHash, startLine: start, endLine: end });
const { container, queryByText } = render(<StatementAnchorContext {...props} anchor={onCurrent} placement={undefined} />);
expect(container.querySelector("[data-anchor-state]")?.getAttribute("data-anchor-state")).toBe(state);
expect(queryByText("plan.review.thread.anchor.view-in-statement") !== null).toBe(action);
diff --git a/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.tsx b/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.tsx
index 307d9dd323..437546759c 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.tsx
+++ b/frontend/src/routes/project/plan-detail/components/threads/StatementAnchorContext.tsx
@@ -5,6 +5,7 @@ import type { PlanChangeReferenceRenderer } from "@/components/issue-activity/Is
import { colorizeStatement } from "@/components/monaco/core";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
+import { Tooltip } from "@/components/ui/tooltip";
import { useInViewOnce } from "@/hooks/useInViewOnce";
import { useSheetStatement } from "@/hooks/useSheetStatement";
import { cn } from "@/lib/utils";
@@ -16,6 +17,7 @@ import type { Placement } from "./placement/place";
import {
type AnchorState,
anchorLineRange,
+ completePreviewLineCount,
excerptLines,
type LineRange,
lineRangeLabel,
@@ -62,19 +64,39 @@ export function StatementAnchorContext({
sheetName,
});
const loaded = enabled && !isLoading;
- // The editor mounts its thread layer only for a complete, non-empty
- // statement. An anchor on the spec's current sheet is CURRENT by hash
- // alone, so that sheet must be loaded and pass those checks before the
- // card offers to show it; until then it is pending, and a truncated or
- // empty sheet makes it unavailable rather than a dead "view" action. A
- // diff-mapped CURRENT anchor needs none of this: its target is complete.
const resolved = resolveAnchorState(anchor, plan, placement);
- const onCurrentSheet = anchor.sheetSha256 === targetSha256OfSpec(spec);
- const state = ((): AnchorState => {
- if (resolved !== "CURRENT" || !onCurrentSheet) return resolved;
- if (!loaded) return "PENDING";
- return isTruncated || statement === "" ? "UNAVAILABLE" : "CURRENT";
- })();
+ const targetSha256 = targetSha256OfSpec(spec);
+ const onCurrentSheet = anchor.sheetSha256 === targetSha256;
+ const previewEndLine = useMemo(
+ () => completePreviewLineCount(statement, isTruncated),
+ [isTruncated, statement]
+ );
+ const recordedLinesVisible = Boolean(
+ statement && range && range.endLine <= previewEndLine
+ );
+ let state: AnchorState = resolved;
+ // A hash match is current only when its complete lines are in the preview.
+ if (resolved === "CURRENT" && onCurrentSheet) {
+ if (!loaded) state = "PENDING";
+ else if (!recordedLinesVisible) state = "UNAVAILABLE";
+ }
+ // A prior revision with visible original lines remains discoverable as
+ // Outdated when a comparison cannot establish a current position.
+ if (
+ resolved === "UNAVAILABLE" &&
+ project &&
+ targetSha256 &&
+ !onCurrentSheet
+ ) {
+ if (!loaded) state = "PENDING";
+ else if (recordedLinesVisible) state = "OUTDATED";
+ }
+ const statusTooltip =
+ state === "OUTDATED" &&
+ placement?.state === "UNAVAILABLE" &&
+ placement.reason === "SIZE_LIMIT"
+ ? t("plan.review.thread.anchor.comparison-size-limit")
+ : undefined;
return (
<div
@@ -102,6 +124,7 @@ export function StatementAnchorContext({
<AnchorStatePill
onViewInStatement={onViewInStatement}
state={state}
+ statusTooltip={statusTooltip}
/>
{onCollapse && (
<Button
@@ -130,9 +153,11 @@ export function StatementAnchorContext({
function AnchorStatePill({
onViewInStatement,
state,
+ statusTooltip,
}: {
onViewInStatement?: () => void;
state: AnchorState;
+ statusTooltip?: string;
}) {
const { t } = useTranslation();
if (state === "CURRENT") {
@@ -149,22 +174,27 @@ function AnchorStatePill({
</Button>
);
}
- if (state === "OUTDATED") {
- return (
+ if (state === "PENDING") return null;
+ const badge =
+ state === "OUTDATED" ? (
<Badge className="gap-x-1 px-2 text-xs" variant="warning">
<History className="size-3" />
{t("plan.review.thread.anchor.outdated")}
</Badge>
+ ) : (
+ <Badge className="gap-x-1 px-2 text-xs" variant="default">
+ <Ban className="size-3" />
+ {t("plan.review.thread.anchor.statement-unavailable")}
+ </Badge>
);
- }
- // The diff has not settled the comment yet; show nothing rather than a
- // state that may flip in a moment.
- if (state === "PENDING") return null;
+ if (!statusTooltip) return badge;
return (
- <Badge className="gap-x-1 px-2 text-xs" variant="default">
- <Ban className="size-3" />
- {t("plan.review.thread.anchor.statement-unavailable")}
- </Badge>
+ <Tooltip
+ content={statusTooltip}
+ render={<span className="inline-flex" tabIndex={0} />}
+ >
+ {badge}
+ </Tooltip>
);
}
diff --git a/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.test.tsx b/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.test.tsx
index c91d85287f..1f8009de1e 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.test.tsx
+++ b/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.test.tsx
@@ -422,13 +422,18 @@ const giveElementsHeight = () =>
.spyOn(HTMLElement.prototype, "getBoundingClientRect")
.mockReturnValue(new DOMRect(0, 0, 700, 100));
-const mount = (editor: IStandaloneCodeEditor, currentIssue = issue) =>
+const mount = (
+ editor: IStandaloneCodeEditor,
+ currentIssue = issue,
+ previewEndLine?: number
+) =>
act(() =>
root.render(
<StatementThreadsLayer
editor={editor}
issue={currentIssue}
monaco={fakeMonaco}
+ previewEndLine={previewEndLine}
sheetSha256={SHA}
spec={spec}
/>
@@ -480,6 +485,29 @@ const pressWalker = (widgets: Set<{ getDomNode: () => HTMLElement }>, label: str
});
describe("StatementThreadsLayer", () => {
+ test("shows only fully visible preview threads and disables new anchors", () => {
+ mocks.comments = [
+ threadRoot("visible", 2, 3),
+ threadRoot("crossing", 3, 4, { createdAt: 2 }),
+ threadRoot("beyond", 6, 6, { createdAt: 3 }),
+ ];
+ const fake = createFakeEditor();
+ mount(fake.editor, issue, 3);
+
+ expect(classesOf(fake.decorations.current, 3)).toContain(
+ "bb-thread-glyph bb-thread-glyph--active"
+ );
+ expect(classesOf(fake.decorations.current, 4)).toEqual([]);
+ expect(classesOf(fake.decorations.current, 6)).toEqual([]);
+ expect(cardRoot(fake.widgets)).toBe(`${ISSUE}/issueComments/visible`);
+ expect(hosted(fake.widgets, "[data-testid='thread-walker']")?.getAttribute("title")).toBe(
+ "plan.review.thread.walker.count:1 · plan.review.thread.walker.remainder:2"
+ );
+ fake.fire("move", 2, MouseTargetType.GUTTER_LINE_NUMBERS);
+ expect(classesOf(fake.decorations.current, 2)).not.toContain("bb-thread-add-glyph");
+ expect(fake.editor.addAction).not.toHaveBeenCalled();
+ });
+
test("decorates current anchors and expands the earliest unresolved thread", () => {
mocks.comments = [
threadRoot("later", 6, 9, { createdAt: 5 }),
diff --git a/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.tsx b/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.tsx
index 31829befd4..f3da84a7a6 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.tsx
+++ b/frontend/src/routes/project/plan-detail/components/threads/StatementThreadsLayer.tsx
@@ -33,11 +33,11 @@ import {
type EditorThread,
groupMarkersByLine,
groupThreads,
+ isEditorThreadVisible,
type LineRange,
lineRangeLabel,
selectEditorThreads,
selectionLineRange,
- selectUnresolvedEditorThreads,
} from "./threadModel";
import { canReplyToThread, useThreadActions } from "./useThreadActions";
@@ -74,12 +74,14 @@ export function StatementThreadsLayer({
editor,
issue,
monaco: monacoModule,
+ previewEndLine,
sheetSha256,
spec,
}: {
editor: IStandaloneCodeEditor;
issue: Issue;
monaco: MonacoModule;
+ previewEndLine?: number;
sheetSha256: string;
spec: Plan_Spec;
}) {
@@ -94,7 +96,8 @@ export function StatementThreadsLayer({
placementsForSheet(s, spec.id, sheetSha256)
);
const actions = useThreadActions(issue.name);
- const canCreate = canReplyToThread(project, issue);
+ const canCreate =
+ previewEndLine === undefined && canReplyToThread(project, issue);
const threads = useMemo(() => groupThreads(comments), [comments]);
const editorThreads = useMemo(
@@ -103,19 +106,14 @@ export function StatementThreadsLayer({
threads,
{ specId: spec.id, sheetSha256 },
placements
- ),
- [placements, sheetSha256, spec.id, threads]
+ ).filter((entry) => isEditorThreadVisible(entry, previewEndLine)),
+ [placements, previewEndLine, sheetSha256, spec.id, threads]
);
// What the walker can visit, in editor order, and how many unresolved
// threads of this change it cannot because they are not placed here.
const visitableThreads = useMemo(
- () =>
- selectUnresolvedEditorThreads(
- threads,
- { specId: spec.id, sheetSha256 },
- placements
- ),
- [placements, sheetSha256, spec.id, threads]
+ () => editorThreads.filter(({ thread }) => !thread.resolved),
+ [editorThreads]
);
const unplacedUnresolved = useMemo(
() =>
diff --git a/frontend/src/routes/project/plan-detail/components/threads/placement/budgets.ts b/frontend/src/routes/project/plan-detail/components/threads/placement/budgets.ts
index eefa08d144..b6f2b9370f 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/placement/budgets.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/placement/budgets.ts
@@ -1,10 +1,13 @@
+import { SHEET_PREVIEW_CHARACTER_LIMIT } from "@/utils/v1/sheet";
+
+const MAX_SHEET_BYTES = 4 * SHEET_PREVIEW_CHARACTER_LIMIT;
+
// Fixed limits for computing placements in the browser. Fetching is bounded
// before it starts and the diff is bounded while it runs; anything past a limit
// is UNAVAILABLE, never a partial answer.
//
-// The values are provisional placeholders until page-load measurements on
-// representative plans set them (design open question 3). Keep them fixed in
-// code rather than configurable so results are reproducible.
+// The resource budgets are provisional until page-load measurements on
+// representative plans set them. Keep them fixed so results are reproducible.
export interface PlacementBudgets {
// Distinct sheets one run may download, and again the distinct sheets one
// run may hand to the worker, cached or not.
@@ -26,9 +29,10 @@ export interface PlacementBudgets {
export const PLACEMENT_BUDGETS: PlacementBudgets = Object.freeze({
maxSheets: 16,
- // Matches the backend preview cutoff (common.MaxSheetSize).
- maxBytesPerSheet: 2 * 1024 * 1024,
- maxTotalBytes: 8 * 1024 * 1024,
+ // UTF-8 uses at most four bytes per character. These separate byte caps
+ // bound downloads and worker payloads without rejecting valid previews.
+ maxBytesPerSheet: MAX_SHEET_BYTES,
+ maxTotalBytes: 2 * MAX_SHEET_BYTES,
maxLinesPerSheet: 50_000,
maxWorkPerPair: 5_000_000,
maxWorkPerRun: 20_000_000,
diff --git a/frontend/src/routes/project/plan-detail/components/threads/placement/place.test.ts b/frontend/src/routes/project/plan-detail/components/threads/placement/place.test.ts
index f5a99ebe3c..4ed0c4210d 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/placement/place.test.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/placement/place.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
+import { SHEET_PREVIEW_CHARACTER_LIMIT } from "@/utils/v1/sheet";
import {
current,
diffPair,
@@ -7,9 +8,13 @@ import {
placeWithHunks,
tokenizeSheet,
UNAVAILABLE,
+ UNAVAILABLE_SIZE_LIMIT,
} from "./place";
-const limits = { maxLinesPerSheet: 10_000, maxWork: 1_000_000 };
+const limits = {
+ maxLinesPerSheet: 10_000,
+ maxWork: 1_000_000,
+};
const at = (saved: string, currentText: string, first: number, last = first) =>
placeOnPair(
diffPair(tokenizeSheet(saved), tokenizeSheet(currentText), limits),
@@ -195,7 +200,7 @@ describe("place: invalid input and limits", () => {
});
expect(pair.hunks).toBeUndefined();
expect(placeOnPair(pair, { startLine: 1, endLine: 1 })).toEqual(
- UNAVAILABLE
+ UNAVAILABLE_SIZE_LIMIT
);
// The cap is inclusive.
expect(
@@ -205,4 +210,19 @@ describe("place: invalid input and limits", () => {
}).hunks
).toEqual([]);
});
+
+ it("uses the API's character limit even when UTF-8 content is larger in bytes", () => {
+ const within = tokenizeSheet("表".repeat(SHEET_PREVIEW_CHARACTER_LIMIT));
+ expect(
+ placeOnPair(diffPair(within, within, limits), {
+ startLine: 1,
+ endLine: 1,
+ })
+ ).toEqual(current(1, 1));
+
+ const over = tokenizeSheet(`${within.text}表`);
+ expect(
+ placeOnPair(diffPair(over, over, limits), { startLine: 1, endLine: 1 })
+ ).toEqual(UNAVAILABLE_SIZE_LIMIT);
+ });
});
diff --git a/frontend/src/routes/project/plan-detail/components/threads/placement/place.ts b/frontend/src/routes/project/plan-detail/components/threads/placement/place.ts
index 9953fb38e8..63a753f66d 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/placement/place.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/placement/place.ts
@@ -1,4 +1,5 @@
import type { StatementAnchor } from "@/types/proto-es/v1/issue_service_pb";
+import { exceedsSheetPreviewLimit } from "@/utils/v1/sheet";
import { tokenizeLines } from "./lineTokens";
import { diffLines, type Hunk } from "./myersDiff";
@@ -16,10 +17,14 @@ export interface LineRange {
export type Placement =
| { readonly state: "CURRENT"; readonly range: LineRange }
| { readonly state: "OUTDATED" }
- | { readonly state: "UNAVAILABLE" };
+ | { readonly state: "UNAVAILABLE"; readonly reason?: "SIZE_LIMIT" };
export const OUTDATED: Placement = Object.freeze({ state: "OUTDATED" });
export const UNAVAILABLE: Placement = Object.freeze({ state: "UNAVAILABLE" });
+export const UNAVAILABLE_SIZE_LIMIT: Placement = Object.freeze({
+ state: "UNAVAILABLE",
+ reason: "SIZE_LIMIT",
+});
export const current = (startLine: number, endLine: number): Placement => ({
state: "CURRENT",
@@ -125,9 +130,9 @@ export interface DiffedPair {
readonly saved: readonly string[];
readonly current: readonly string[];
// Undefined when the diff did not run to completion; `reason` says why.
- // A "budget" miss is retriable with more budget, a "lines" miss is not.
+ // A work-budget miss is retriable; a size limit is not.
readonly hunks?: readonly Hunk[];
- readonly reason?: "budget" | "lines";
+ readonly reason?: "budget" | "size";
readonly work: number;
}
@@ -137,8 +142,7 @@ export interface DiffPairLimits {
}
// Diffs one pair once. Identical contents skip the diff. A sheet over the
-// line cap or a diff over the work budget yields no hunks, and every anchor
-// on the pair then resolves UNAVAILABLE.
+// character or line cap, or a diff over the work budget, yields no hunks.
export function diffPair(
saved: SheetText,
current: SheetText,
@@ -146,10 +150,12 @@ export function diffPair(
): DiffedPair {
const base = { saved: saved.lines, current: current.lines };
if (
+ exceedsSheetPreviewLimit(saved.text) ||
+ exceedsSheetPreviewLimit(current.text) ||
saved.lines.length > limits.maxLinesPerSheet ||
current.lines.length > limits.maxLinesPerSheet
) {
- return { ...base, reason: "lines", work: 0 };
+ return { ...base, reason: "size", work: 0 };
}
if (saved.text === current.text) {
return { ...base, hunks: [], work: 0 };
@@ -161,6 +167,8 @@ export function diffPair(
}
export function placeOnPair(pair: DiffedPair, range: LineRange): Placement {
- if (!pair.hunks) return UNAVAILABLE;
+ if (!pair.hunks) {
+ return pair.reason === "size" ? UNAVAILABLE_SIZE_LIMIT : UNAVAILABLE;
+ }
return placeWithHunks(pair.saved, pair.current, pair.hunks, range);
}
diff --git a/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.test.ts b/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.test.ts
index 8ee10990ac..dfe636ad6f 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.test.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.test.ts
@@ -10,7 +10,13 @@ import {
Plan_ChangeDatabaseConfigSchema,
Plan_SpecSchema,
} from "@/types/proto-es/v1/plan_service_pb";
-import { current, OUTDATED, UNAVAILABLE, wholeLineRange } from "./place";
+import {
+ current,
+ OUTDATED,
+ UNAVAILABLE,
+ UNAVAILABLE_SIZE_LIMIT,
+ wholeLineRange,
+} from "./place";
import {
type PlacementPlanInput,
pairKey,
@@ -70,16 +76,19 @@ const plan = (
overrides: Partial<PlacementPlanInput> & {
sizes?: Record<string, number>;
complete?: string[];
+ capped?: string[];
}
) => {
const sizes = overrides.sizes ?? {};
const complete = new Set(overrides.complete ?? []);
+ const capped = new Set(overrides.capped ?? []);
return planPlacements({
comments: [],
specs: [spec("spec-1", SHA_B)],
projectName: PROJECT,
sizeOf: (name) => (name in sizes ? BigInt(sizes[name]) : undefined),
isComplete: (name) => complete.has(name),
+ isCappedPreview: (name) => capped.has(name),
budgets,
...overrides,
});
@@ -186,7 +195,9 @@ describe("planPlacements", () => {
specs: [spec("spec-1", SHA_B), spec("spec-2", SHA_D)],
sizes: { [sheet(SHA_B)]: 5000 },
});
- expect(result.settled.get(nameOf("doomed"))).toEqual(UNAVAILABLE);
+ expect(result.settled.get(nameOf("doomed"))).toEqual(
+ UNAVAILABLE_SIZE_LIMIT
+ );
// Only the viable pair's sheets are worth a probe.
expect(result.unknownSizes).toEqual([sheet(SHA_C), sheet(SHA_D)]);
});
@@ -204,7 +215,7 @@ describe("planPlacements", () => {
sizes: { [sheet(SHA_B)]: 5000 },
settleUnknownSizes: false,
});
- expect(oversize.settled.get(nameOf("c"))).toEqual(UNAVAILABLE);
+ expect(oversize.settled.get(nameOf("c"))).toEqual(UNAVAILABLE_SIZE_LIMIT);
});
test("enforces the per-sheet byte cap inclusively", () => {
@@ -212,7 +223,7 @@ describe("planPlacements", () => {
comments: [comment("c", anchor(SHA_A, 1))],
sizes: { [sheet(SHA_A)]: 1001, [sheet(SHA_B)]: 10 },
});
- expect(over.settled.get(nameOf("c"))).toEqual(UNAVAILABLE);
+ expect(over.settled.get(nameOf("c"))).toEqual(UNAVAILABLE_SIZE_LIMIT);
const at = plan({
comments: [comment("c", anchor(SHA_A, 1))],
sizes: { [sheet(SHA_A)]: 1000, [sheet(SHA_B)]: 10 },
@@ -220,13 +231,23 @@ describe("planPlacements", () => {
expect(at.pairs).toHaveLength(1);
});
+ test("settles a capped API preview without requesting raw content", () => {
+ const result = plan({
+ comments: [comment("c", anchor(SHA_A, 1))],
+ sizes: { [sheet(SHA_A)]: 300, [sheet(SHA_B)]: 200 },
+ capped: [sheet(SHA_A)],
+ });
+ expect(result.settled.get(nameOf("c"))).toEqual(UNAVAILABLE_SIZE_LIMIT);
+ expect(result.fetches).toEqual([]);
+ });
+
test("applies the per-sheet cap even to a sheet that is already cached", () => {
const result = plan({
comments: [comment("c", anchor(SHA_A, 1))],
sizes: { [sheet(SHA_A)]: 10, [sheet(SHA_B)]: 5000 },
complete: [sheet(SHA_B)],
});
- expect(result.settled.get(nameOf("c"))).toEqual(UNAVAILABLE);
+ expect(result.settled.get(nameOf("c"))).toEqual(UNAVAILABLE_SIZE_LIMIT);
});
test("keeps earlier pairs and drops later ones past the sheet-count cap", () => {
@@ -288,7 +309,7 @@ describe("planPlacements", () => {
],
sizes: { [sheet(SHA_A)]: 2000, [sheet(SHA_B)]: 10, [sheet(SHA_C)]: 10 },
});
- expect(result.settled.get(nameOf("big"))).toEqual(UNAVAILABLE);
+ expect(result.settled.get(nameOf("big"))).toEqual(UNAVAILABLE_SIZE_LIMIT);
expect(result.pairs.map((pair) => pair.sourceSha256)).toEqual([SHA_C]);
expect(result.fetches).toEqual([sheet(SHA_C), sheet(SHA_B)]);
});
diff --git a/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.ts b/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.ts
index 1a5a4495ce..dc9552cd22 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/placement/placementPlan.ts
@@ -12,6 +12,7 @@ import {
type Placement,
trivialPlacement,
UNAVAILABLE,
+ UNAVAILABLE_SIZE_LIMIT,
wholeLineRange,
} from "./place";
@@ -51,6 +52,8 @@ export interface PlacementPlanInput {
readonly sizeOf: (sheetName: string) => bigint | undefined;
// Whether the cache holds the sheet's complete content.
readonly isComplete: (sheetName: string) => boolean;
+ // Whether the cached preview reached the API's character cutoff.
+ readonly isCappedPreview: (sheetName: string) => boolean;
readonly budgets: Pick<
PlacementBudgets,
"maxSheets" | "maxBytesPerSheet" | "maxTotalBytes"
@@ -133,9 +136,12 @@ export function planPlacements(input: PlacementPlanInput): PlacementPlan {
const unknownSizes = new Set<string>();
const fetchSet = new Set<string>();
let totalBytes = input.spentBytes ?? 0n;
- const settleUnavailable = (candidate: CandidatePair) => {
+ const settleUnavailable = (
+ candidate: CandidatePair,
+ placement: Placement = UNAVAILABLE
+ ) => {
for (const names of candidate.comments.values()) {
- for (const name of names) settled.set(name, UNAVAILABLE);
+ for (const name of names) settled.set(name, placement);
}
};
@@ -155,7 +161,10 @@ export function planPlacements(input: PlacementPlanInput): PlacementPlan {
unknown.push(name);
continue;
}
- if (size > BigInt(input.budgets.maxBytesPerSheet)) {
+ if (
+ size > BigInt(input.budgets.maxBytesPerSheet) ||
+ input.isCappedPreview(name)
+ ) {
unavailable = true;
continue;
}
@@ -176,7 +185,10 @@ export function planPlacements(input: PlacementPlanInput): PlacementPlan {
fetchSet.size + pairFetches.length > input.budgets.maxSheets ||
totalBytes + pairBytes > BigInt(input.budgets.maxTotalBytes)
) {
- settleUnavailable(candidate);
+ settleUnavailable(
+ candidate,
+ unavailable ? UNAVAILABLE_SIZE_LIMIT : UNAVAILABLE
+ );
continue;
}
for (const name of pairFetches) {
diff --git a/frontend/src/routes/project/plan-detail/components/threads/threadModel.test.ts b/frontend/src/routes/project/plan-detail/components/threads/threadModel.test.ts
index b0ad3dcc9e..333b0e6161 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/threadModel.test.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/threadModel.test.ts
@@ -16,6 +16,7 @@ import { current, OUTDATED, UNAVAILABLE } from "./placement/place";
import {
anchorLineRange,
buildWholeLineAnchor,
+ completePreviewLineCount,
countPlacedUnresolvedBySpec,
countUnresolvedThreads,
defaultExpandedThread,
@@ -94,6 +95,17 @@ describe("sheetSha256OfName", () => {
});
});
+describe("completePreviewLineCount", () => {
+ test("excludes the final line of a truncated preview, even after a newline", () => {
+ expect(completePreviewLineCount("first\nsecond", true)).toBe(1);
+ expect(completePreviewLineCount("first\nsecond\n", true)).toBe(2);
+ expect(completePreviewLineCount("first\r\nsecond\r\n", true)).toBe(2);
+ expect(completePreviewLineCount("first\rsecond", true)).toBe(1);
+ expect(completePreviewLineCount("first", true)).toBe(0);
+ expect(completePreviewLineCount("first\nsecond", false)).toBe(2);
+ });
+});
+
describe("countUnresolvedThreads", () => {
test("counts open roots, anchored or not, and never replies", () => {
const threads = groupThreads([
@@ -123,6 +135,14 @@ describe("countUnresolvedThreads", () => {
expect([
...countPlacedUnresolvedBySpec(threads, specs, () => placed),
]).toEqual([["spec-1", 2]]);
+ expect([
+ ...countPlacedUnresolvedBySpec(
+ threads,
+ specs,
+ () => placed,
+ () => 1
+ ),
+ ]).toEqual([["spec-1", 1]]);
expect(countPlacedUnresolvedBySpec(threads, [], () => undefined).size).toBe(
0
);
diff --git a/frontend/src/routes/project/plan-detail/components/threads/threadModel.ts b/frontend/src/routes/project/plan-detail/components/threads/threadModel.ts
index f2d9b1e338..ce95ef3159 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/threadModel.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/threadModel.ts
@@ -93,7 +93,8 @@ export function countPlacedUnresolvedBySpec(
placementsFor: (
specId: string,
sheetSha256: string
- ) => ReadonlyMap<string, Placement> | undefined
+ ) => ReadonlyMap<string, Placement> | undefined,
+ previewEndLineFor?: (specId: string) => number | undefined
): ReadonlyMap<string, number> {
const counts = new Map<string, number>();
for (const spec of specs) {
@@ -103,6 +104,8 @@ export function countPlacedUnresolvedBySpec(
threads,
{ specId: spec.id, sheetSha256 },
placementsFor(spec.id, sheetSha256)
+ ).filter((entry) =>
+ isEditorThreadVisible(entry, previewEndLineFor?.(spec.id))
).length;
if (placed > 0) counts.set(spec.id, placed);
}
@@ -141,6 +144,25 @@ export function anchorLineRange(
return { startLine: start.line, endLine: Math.max(endLine, start.line) };
}
+// A truncated preview may end in the middle of a line, including the empty
+// line after a trailing newline. Only preceding lines have complete content.
+export const completePreviewLineCount = (
+ statement: string,
+ isTruncated: boolean
+): number => {
+ let completeLines = 0;
+ for (let i = 0; i < statement.length; i++) {
+ const code = statement.charCodeAt(i);
+ if (code === 13) {
+ if (statement.charCodeAt(i + 1) === 10) i++;
+ completeLines++;
+ } else if (code === 10) {
+ completeLines++;
+ }
+ }
+ return completeLines + (isTruncated ? 0 : 1);
+};
+
export function buildWholeLineAnchor(input: {
spec: string;
sheetSha256: string;
@@ -215,6 +237,12 @@ export function selectEditorThreads(
return placed.sort(compareEditorThreads);
}
+export const isEditorThreadVisible = (
+ entry: EditorThread,
+ previewEndLine: number | undefined
+): boolean =>
+ previewEndLine === undefined || entry.range.endLine <= previewEndLine;
+
// The placed threads still open: what the editor walker visits and what the
// change tab counts.
export const selectUnresolvedEditorThreads = (
diff --git a/frontend/src/routes/project/plan-detail/components/threads/useUnresolvedThreadCounts.ts b/frontend/src/routes/project/plan-detail/components/threads/useUnresolvedThreadCounts.ts
index f90eed6655..71d133b8db 100644
--- a/frontend/src/routes/project/plan-detail/components/threads/useUnresolvedThreadCounts.ts
+++ b/frontend/src/routes/project/plan-detail/components/threads/useUnresolvedThreadCounts.ts
@@ -2,15 +2,20 @@ import { useMemo } from "react";
import { useAppStore } from "@/stores/app";
import type { IssueComment } from "@/types/proto-es/v1/issue_service_pb";
import type { Plan_Spec } from "@/types/proto-es/v1/plan_service_pb";
+import { getSheetStatement, isSheetContentComplete } from "@/utils/v1/sheet";
import { placementsForSheet } from "../../shared/stores/placementSlice";
import { usePlanDetailStore } from "../../shared/stores/usePlanDetailStore";
import {
+ completePreviewLineCount,
countPlacedUnresolvedBySpec,
countUnresolvedThreads,
groupThreads,
+ sheetNameOfSha256,
+ targetSha256OfSpec,
} from "./threadModel";
const NO_COMMENTS: IssueComment[] = [];
+const previewEndLinesByContent = new WeakMap<Uint8Array, number>();
const useIssueThreads = (issueName: string | undefined) => {
const comments = useAppStore((state) =>
@@ -29,16 +34,39 @@ export function useUnresolvedThreadTotal(issueName: string | undefined) {
// change tab and its editor walker agree.
export function usePlacedUnresolvedThreadCounts(
issueName: string | undefined,
- specs: Plan_Spec[]
+ specs: Plan_Spec[],
+ projectName: string
): ReadonlyMap<string, number> {
const threads = useIssueThreads(issueName);
+ const sheetsByName = useAppStore((s) => s.sheetsByName);
const placements = usePlanDetailStore((s) => s.placements);
const placementTargets = usePlanDetailStore((s) => s.placementTargets);
+ const previewEndLines = useMemo(() => {
+ const lines = new Map<string, number>();
+ for (const spec of specs) {
+ const sha = targetSha256OfSpec(spec);
+ if (!sha) continue;
+ const sheet = sheetsByName[sheetNameOfSha256(projectName, sha)];
+ if (sheet && !isSheetContentComplete(sheet)) {
+ let endLine = previewEndLinesByContent.get(sheet.content);
+ if (endLine === undefined) {
+ endLine = completePreviewLineCount(getSheetStatement(sheet), true);
+ previewEndLinesByContent.set(sheet.content, endLine);
+ }
+ lines.set(spec.id, endLine);
+ }
+ }
+ return lines;
+ }, [projectName, sheetsByName, specs]);
return useMemo(
() =>
- countPlacedUnresolvedBySpec(threads, specs, (specId, sha) =>
- placementsForSheet({ placements, placementTargets }, specId, sha)
+ countPlacedUnresolvedBySpec(
+ threads,
+ specs,
+ (specId, sha) =>
+ placementsForSheet({ placements, placementTargets }, specId, sha),
+ (specId) => previewEndLines.get(specId)
),
- [placementTargets, placements, specs, threads]
+ [placementTargets, placements, previewEndLines, specs, threads]
);
}
diff --git a/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.test.ts b/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.test.ts
index c8cd27b88c..dc068bc523 100644
--- a/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.test.ts
+++ b/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.test.ts
@@ -11,12 +11,14 @@ import {
Plan_SpecSchema,
} from "@/types/proto-es/v1/plan_service_pb";
import { type Sheet, SheetSchema } from "@/types/proto-es/v1/sheet_service_pb";
+import { SHEET_PREVIEW_CHARACTER_LIMIT } from "@/utils/v1/sheet";
import { PLACEMENT_BUDGETS } from "../../components/threads/placement/budgets";
import {
current,
diffPair,
tokenizeSheet,
UNAVAILABLE,
+ UNAVAILABLE_SIZE_LIMIT,
} from "../../components/threads/placement/place";
import {
type PlacementClient,
@@ -420,6 +422,58 @@ describe("placementSlice", () => {
expect(request.generation).toBe(1);
});
+ test("compares multibyte sheets within the API character limit", async () => {
+ const sheets = fakeSheets({
+ [sheetName(SHA_OLD)]: "表表表",
+ [sheetName(SHA_NEW)]: "表表表",
+ [sheetName(SHA_NEWER)]: "表表表表",
+ });
+ for (const name of [
+ sheetName(SHA_OLD),
+ sheetName(SHA_NEW),
+ sheetName(SHA_NEWER),
+ ]) {
+ sheets.prime(name, true);
+ }
+ const { store } = setup({
+ sheets,
+ budgets: {
+ ...PLACEMENT_BUDGETS,
+ maxBytesPerSheet: 12,
+ maxTotalBytes: 24,
+ },
+ });
+ const comments = [comment("multibyte", anchor(SHA_OLD, 1))];
+ await compute(store, comments);
+ expect(store.getState().placements.get(nameOf("multibyte"))).toEqual(
+ current(1, 1)
+ );
+
+ await compute(store, comments, SHA_NEWER);
+ expect(store.getState().placements.get(nameOf("multibyte"))).toEqual({
+ state: "OUTDATED",
+ });
+ });
+
+ test("skips the raw fetch when the preview reaches the API cutoff", async () => {
+ const sheets = fakeSheets(
+ {
+ [sheetName(SHA_OLD)]: "a".repeat(SHEET_PREVIEW_CHARACTER_LIMIT + 1),
+ [sheetName(SHA_NEW)]: NEW_TEXT,
+ },
+ SHEET_PREVIEW_CHARACTER_LIMIT
+ );
+ sheets.prime(sheetName(SHA_NEW), true);
+ const { store, client } = setup({ sheets });
+ await compute(store, [comment("large", anchor(SHA_OLD, 1))]);
+
+ expect(sheets.calls).toEqual([{ name: sheetName(SHA_OLD), raw: false }]);
+ expect(store.getState().placements.get(nameOf("large"))).toEqual(
+ UNAVAILABLE_SIZE_LIMIT
+ );
+ expect((client as ReturnType<typeof immediateClient>).requests).toEqual([]);
+ });
+
test("bounds the size probe by the sheet cap", async () => {
const contents: Record<string, string> = {
[sheetName(SHA_NEW)]: NEW_TEXT,
@@ -509,7 +563,9 @@ test("caches a definitive UNAVAILABLE so the pair is not diffed again", async ()
specs: specs(SHA_NEW),
};
await store.getState().computePlacements(input);
- expect(store.getState().placements.get(nameOf("moved"))).toEqual(UNAVAILABLE);
+ expect(store.getState().placements.get(nameOf("moved"))).toEqual(
+ UNAVAILABLE_SIZE_LIMIT
+ );
await store.getState().computePlacements(input);
const requests = (client as ReturnType<typeof immediateClient>).requests;
expect(requests).toHaveLength(1);
diff --git a/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.ts b/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.ts
index 098cc6ecef..7a6a5c983a 100644
--- a/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.ts
+++ b/frontend/src/routes/project/plan-detail/shared/stores/placementSlice.ts
@@ -1,6 +1,10 @@
import { useAppStore } from "@/stores/app";
import type { Sheet } from "@/types/proto-es/v1/sheet_service_pb";
-import { getSheetStatement, isSheetContentComplete } from "@/utils/v1/sheet";
+import {
+ getSheetStatement,
+ isCappedSheetPreview,
+ isSheetContentComplete,
+} from "@/utils/v1/sheet";
import {
PLACEMENT_BUDGETS,
type PlacementBudgets,
@@ -42,12 +46,19 @@ export const defaultPlacementDeps = (): PlacementDeps => ({
now: () => performance.now(),
});
-const samePlacement = (a: Placement, b: Placement): boolean =>
- a.state === b.state &&
- (a.state !== "CURRENT" ||
- b.state !== "CURRENT" ||
- (a.range.startLine === b.range.startLine &&
- a.range.endLine === b.range.endLine));
+const samePlacement = (a: Placement, b: Placement): boolean => {
+ if (a.state !== b.state) return false;
+ if (a.state === "CURRENT" && b.state === "CURRENT") {
+ return (
+ a.range.startLine === b.range.startLine &&
+ a.range.endLine === b.range.endLine
+ );
+ }
+ if (a.state === "UNAVAILABLE" && b.state === "UNAVAILABLE") {
+ return a.reason === b.reason;
+ }
+ return true;
+};
// The placements of the latest run apply to a spec only while it still
// points at the sheet that run was computed against; a consumer showing
@@ -154,6 +165,10 @@ export const createPlacementSlice =
projectName,
sizeOf: (name: string) => deps.getSheet(name)?.contentSize,
isComplete: (name: string) => completeSheet(name) !== undefined,
+ isCappedPreview: (name: string) => {
+ const sheet = deps.getSheet(name);
+ return sheet !== undefined && isCappedSheetPreview(sheet);
+ },
budgets,
};
const targets = new Map<string, string>();
@@ -201,17 +216,22 @@ export const createPlacementSlice =
settle(false);
publish(placements, targets);
- // A sheet the cache has never seen has no known size. A preview fetch
- // is capped by the server at the per-sheet budget, so it doubles as
- // the bounded download. Each probe reserves that cap before it starts
- // and settles to the real size when it lands, so concurrent probes
- // cannot overshoot the total budget between them.
+ // A sheet the cache has never seen has no known size. Each preview
+ // probe reserves the maximum UTF-8 bytes permitted by the character
+ // limit and settles to its actual size when it lands.
if (plan.unknownSizes.length > 0) {
const byteCap = BigInt(budgets.maxTotalBytes);
const perSheet = BigInt(budgets.maxBytesPerSheet);
+ const probeConcurrency = Math.min(
+ FETCH_CONCURRENCY,
+ Math.max(
+ 1,
+ Math.floor(budgets.maxTotalBytes / budgets.maxBytesPerSheet)
+ )
+ );
await runWithConcurrency(
plan.unknownSizes.slice(0, budgets.maxSheets),
- FETCH_CONCURRENCY,
+ probeConcurrency,
async (name) => {
if (spentBytes + perSheet > byteCap) return undefined;
spentBytes += perSheet;
@@ -225,7 +245,7 @@ export const createPlacementSlice =
publish(placements);
}
- // Sheets the probe left incomplete are re-fetched raw and verified.
+ // Eligible sheets the probe left incomplete are re-fetched raw.
const needed = new Set(
pending.flatMap((pair) => [pair.sourceName, pair.targetName])
);
diff --git a/frontend/src/utils/v1/sheet.test.ts b/frontend/src/utils/v1/sheet.test.ts
new file mode 100644
index 0000000000..f78b934088
--- /dev/null
+++ b/frontend/src/utils/v1/sheet.test.ts
@@ -0,0 +1,48 @@
+import { create } from "@bufbuild/protobuf";
+import { describe, expect, test } from "vitest";
+import { SheetSchema } from "@/types/proto-es/v1/sheet_service_pb";
+import {
+ exceedsSheetPreviewLimit,
+ isCappedSheetPreview,
+ SHEET_PREVIEW_CHARACTER_LIMIT,
+} from "./sheet";
+
+describe("exceedsSheetPreviewLimit", () => {
+ test("matches the API's character boundary", () => {
+ const statement = "a".repeat(SHEET_PREVIEW_CHARACTER_LIMIT);
+ expect(exceedsSheetPreviewLimit(statement)).toBe(false);
+ expect(exceedsSheetPreviewLimit(`${statement}a`)).toBe(true);
+ });
+
+ test("does not treat a multibyte sheet as truncated solely because of its byte size", () => {
+ const statement = "表".repeat(1_000_000);
+ expect(new TextEncoder().encode(statement).byteLength).toBeGreaterThan(
+ 2 * 1024 * 1024
+ );
+ expect(exceedsSheetPreviewLimit(statement)).toBe(false);
+ });
+
+ test("counts supplementary Unicode characters once", () => {
+ const statement = `${"a".repeat(SHEET_PREVIEW_CHARACTER_LIMIT - 1)}😀`;
+ expect(exceedsSheetPreviewLimit(statement)).toBe(false);
+ expect(exceedsSheetPreviewLimit(`${statement}a`)).toBe(true);
+ });
+});
+
+test("recognizes only incomplete previews that reach the API cutoff", () => {
+ const content = new TextEncoder().encode(
+ "a".repeat(SHEET_PREVIEW_CHARACTER_LIMIT)
+ );
+ const sheet = (size: bigint) =>
+ create(SheetSchema, { content, contentSize: size });
+ expect(isCappedSheetPreview(sheet(BigInt(content.length + 1)))).toBe(true);
+ expect(isCappedSheetPreview(sheet(BigInt(content.length)))).toBe(false);
+ expect(
+ isCappedSheetPreview(
+ create(SheetSchema, {
+ content: new TextEncoder().encode("short"),
+ contentSize: 100n,
+ })
+ )
+ ).toBe(false);
+});
diff --git a/frontend/src/utils/v1/sheet.ts b/frontend/src/utils/v1/sheet.ts
index 86ed1ab428..0fe289f9f8 100644
--- a/frontend/src/utils/v1/sheet.ts
+++ b/frontend/src/utils/v1/sheet.ts
@@ -32,3 +32,35 @@ export const getSheetStatement = (sheet: Sheet | SavedQuery) => {
// already the encoded bytes, so this is an O(1) size check.
export const isSheetContentComplete = (sheet: Sheet | SavedQuery): boolean =>
BigInt(sheet.content.byteLength) >= sheet.contentSize;
+
+// GetSheet(raw=false) returns at most this many characters, not UTF-8 bytes.
+export const SHEET_PREVIEW_CHARACTER_LIMIT = 2 * 1024 * 1024;
+
+export const exceedsSheetPreviewLimit = (
+ statement: string,
+ limit = SHEET_PREVIEW_CHARACTER_LIMIT
+): boolean => {
+ if (statement.length <= limit) return false;
+ // Without surrogate pairs, UTF-16 length already counts characters.
+ if (!/[\uD800-\uDBFF]/.test(statement)) return true;
+ let characters = 0;
+ for (const _ of statement) {
+ if (++characters > limit) return true;
+ }
+ return false;
+};
+
+// Sheet content is immutable; share the character count across review threads.
+const cappedPreviews = new WeakMap<Uint8Array, boolean>();
+export const isCappedSheetPreview = (sheet: Sheet): boolean => {
+ if (isSheetContentComplete(sheet)) return false;
+ let capped = cappedPreviews.get(sheet.content);
+ if (capped === undefined) {
+ capped = exceedsSheetPreviewLimit(
+ getSheetStatement(sheet),
+ SHEET_PREVIEW_CHARACTER_LIMIT - 1
+ );
+ cappedPreviews.set(sheet.content, capped);
+ }
+ return capped;
+};