From 946aaa82679ac83bceb54a16270c6eb31f6d117f Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 17:28:22 +0000 Subject: [PATCH 1/6] feat: add document sources from connected accounts Use the signed-in account to check and add selected document links. Keep shared credentials limited to administrators and show provider configuration requirements. Signed-off-by: zlareb1 --- frontend/e2e/connected-source.spec.ts | 331 ++++++++++++++++++ frontend/src/api/connections.ts | 1 + .../KnowledgeSourceDialog.connected.test.tsx | 256 ++++++++++++++ .../src/flows/KnowledgeSourceDialog.test.tsx | 30 +- frontend/src/flows/KnowledgeSourceDialog.tsx | 253 +++++++------ frontend/src/flows/SourceAccountField.tsx | 106 ++++++ .../src/flows/SourceRegistrationFields.tsx | 202 +++++++++++ frontend/src/lib/knowledgeSources.test.ts | 83 ++++- frontend/src/lib/knowledgeSources.ts | 48 ++- frontend/src/views/ConnectedAccounts.test.tsx | 52 +++ frontend/src/views/ConnectedAccounts.tsx | 35 +- frontend/src/views/Documents.tsx | 9 +- frontend/src/views/RepositoryCard.tsx | 7 +- frontend/src/views/SourcesList.test.tsx | 121 ++++++- frontend/src/views/SourcesList.tsx | 55 ++- sdk/python/loom/ops.py | 1 + sdk/typescript/src/generated/ops.ts | 2 + server/knowledge/api/models_connections.py | 3 + server/knowledge/app/connections.py | 12 + .../test_connected_account_availability.py | 99 ++++++ 20 files changed, 1522 insertions(+), 184 deletions(-) create mode 100644 frontend/e2e/connected-source.spec.ts create mode 100644 frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx create mode 100644 frontend/src/flows/SourceAccountField.tsx create mode 100644 frontend/src/flows/SourceRegistrationFields.tsx create mode 100644 tests/unit/test_connected_account_availability.py diff --git a/frontend/e2e/connected-source.spec.ts b/frontend/e2e/connected-source.spec.ts new file mode 100644 index 0000000000..161d3afc1f --- /dev/null +++ b/frontend/e2e/connected-source.spec.ts @@ -0,0 +1,331 @@ +import { expect, test, type Locator, type Page } from "@playwright/test"; +import { boxOf, installApi, isRecord, MIN_TARGET_PX } from "./fixtures"; +import { violations } from "./axe"; + +const DESTINATION = "col-research"; +const SUBJECT = "writer@example.com"; +const LINKS = [ + "https://drive.google.com/file/d/1ExampleSelectedPdf/view", + "https://docs.google.com/document/d/1ExampleSelectedDoc/edit", +]; + +// These fixtures exercise browser wiring and ordinary-user permissions. They do not +// qualify provider consent, ingestion, extraction or provider-permission mirroring. +async function installConnectedSource(page: Page) { + await installApi(page, { + viewer: true, + savedNamespace: "bench", + claims: ["bench", DESTINATION], + mine: [ + { + namespace: "bench", + role: "writer", + status: "active", + visibility: "private", + kind: "memory", + description: "", + joined_at: "2026-01-01T00:00:00Z", + is_new: false, + }, + ], + collections: [ + { namespace: DESTINATION, parent: "", title: "Research", personal: false, role: "writer", description: "" }, + ], + knowledgeSources: [], + }); + const state = { + connected: true, + added: false, + complete: false, + refusedCheck: false, + consentCalls: 0, + accountReads: 0, + sourceCalls: [] as { path: string; body: Record }[], + }; + await page.route("**/v1/whoami", (route) => + route.fulfill({ + json: { + authenticated: true, + subject: SUBJECT, + org: "dev", + namespaces: ["bench", DESTINATION], + roles: ["WRITER"], + is_service_account: false, + }, + }), + ); + await page.route("**/v1/connection.list", (route) => { + state.accountReads++; + const body: unknown = route.request().postDataJSON(); + expect(body).toMatchObject({ org: "dev" }); + expect(body).not.toHaveProperty("subject"); + expect(body).not.toHaveProperty("namespace"); + return route.fulfill({ + json: { + connections: ["gdrive", "notion", "linear"].map((kind) => ({ + kind, + connected: kind === "gdrive" && state.connected, + connect_available: true, + account_email: kind === "gdrive" && state.connected ? SUBJECT : null, + scopes: [], + connect_url: `/settings/connected-accounts?kind=${kind}`, + })), + }, + }); + }); + await page.route("**/v1/connect/**", (route) => { + state.consentCalls++; + return route.fulfill({ status: 400, json: { detail: "Consent must not start during source registration." } }); + }); + await page.route("**/v1/knowledge.source.*", (route) => { + const path = new URL(route.request().url()).pathname; + const raw: unknown = route.request().postDataJSON(); + if (!isRecord(raw)) throw new Error("Expected a source request body"); + state.sourceCalls.push({ path, body: raw }); + if (path.endsWith(".list")) { + return route.fulfill({ + json: { sources: state.added ? [sourceRow(state.complete)] : [], enabled: true, accepts_sources: true }, + }); + } + if (path.endsWith(".add")) { + if (raw.dry_run && !state.refusedCheck) { + state.refusedCheck = true; + return route.fulfill({ + status: 403, + json: { detail: { message: "Selected content could not be read. Check its sharing settings." } }, + }); + } + if (!raw.dry_run) state.added = true; + return route.fulfill({ + json: { + name: "selected-reading", + kind: "gdrive", + namespace: DESTINATION, + config: {}, + dry_run: raw.dry_run, + probe: { documents: 0, more: false, sample: [], checked: 2, sampled: false }, + }, + }); + } + return route.fulfill({ status: 400, json: { detail: "Unexpected source operation in this flow." } }); + }); + return state; +} + +function sourceRow(complete: boolean) { + return { + name: "selected-reading", + kind: "gdrive", + credential_mode: "connected", + owner_subject: SUBJECT, + config: {}, + items: [], + created_by: SUBJECT, + created_at: "2026-01-01T00:00:00Z", + sync: { + requested_at: "", + last_synced_at: complete ? "2026-01-01T00:01:00Z" : "", + status: complete ? "ok" : "", + documents_seen: complete ? 2 : 0, + changed: complete ? 2 : 0, + removed: 0, + unconverted: 0, + unconverted_reasons: {}, + skipped: 0, + error: "", + }, + }; +} + +async function checkLayout(page: Page, dialog: Locator, width: number) { + expect(await violations(page)).toEqual([]); + expect(await dialog.evaluate((el) => el.scrollWidth <= el.clientWidth + 1)).toBe(true); + expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(true); + if (width === 375) { + for (const button of await dialog.getByRole("button").all()) { + if (!(await button.isVisible())) continue; + const box = await boxOf(button); + expect(box.width).toBeGreaterThanOrEqual(MIN_TARGET_PX); + expect(box.height).toBeGreaterThanOrEqual(MIN_TARGET_PX); + } + } +} + +async function checkConnectionTab( + page: Page, + dialog: Locator, + state: { connected: boolean; consentCalls: number }, + screenshot: string, +) { + const origin = new URL(page.url()).origin; + const blocked = { external: 0, mcp: 0 }; + // Page routes remain authoritative for the source tab. The popup gets explicit + // own-account fixtures; every other API request is refused before reaching a server. + await page.context().route("**/*", (route) => { + const url = new URL(route.request().url()); + if (url.origin !== origin) { + blocked.external++; + return route.abort("blockedbyclient"); + } + if (url.pathname === "/mcp" || url.pathname.startsWith("/mcp/")) { + blocked.mcp++; + return route.abort("blockedbyclient"); + } + if (!url.pathname.startsWith("/v1/") && url.pathname !== "/v1") return route.continue(); + if (url.pathname === "/v1/whoami") { + return route.fulfill({ + json: { + authenticated: true, + subject: SUBJECT, + org: "dev", + namespaces: ["bench", DESTINATION], + roles: ["WRITER"], + is_service_account: false, + }, + }); + } + if (url.pathname === "/v1/namespace.list_mine") { + return route.fulfill({ + json: { rows: [{ namespace: "bench", role: "writer", status: "active", kind: "memory" }] }, + }); + } + if (url.pathname === "/v1/connection.list") { + const body: unknown = route.request().postDataJSON(); + expect(body).toMatchObject({ org: "dev" }); + expect(body).not.toHaveProperty("subject"); + expect(body).not.toHaveProperty("namespace"); + return route.fulfill({ + json: { + connections: ["gdrive", "notion", "linear"].map((kind) => ({ + kind, + connected: kind === "gdrive" && state.connected, + connect_available: true, + account_email: kind === "gdrive" && state.connected ? SUBJECT : null, + scopes: [], + connect_url: `/settings/connected-accounts?kind=${kind}`, + })), + }, + }); + } + if (url.pathname.startsWith("/v1/connect/")) state.consentCalls++; + return route.fulfill({ status: 503, json: { detail: "This request has no browser contract fixture." } }); + }); + const [popup] = await Promise.all([ + page.context().waitForEvent("page"), + dialog.getByRole("link", { name: /^Connect Google Drive/ }).click(), + ]); + try { + await expect(popup.getByRole("heading", { name: "Connected accounts", exact: true })).toBeVisible(); + await expect(popup.getByText("After connecting, return to the tab where you are adding a source.")).toBeVisible(); + const url = new URL(popup.url()); + expect(url.origin).toBe(origin); + expect(url.pathname).toBe("/settings/connected-accounts"); + expect([...url.searchParams.keys()].sort()).toEqual(["from", "kind"]); + expect(url.searchParams.get("kind")).toBe("gdrive"); + expect(url.searchParams.get("from")).toBe("source"); + for (const provider of ["Google Drive", "Notion", "Linear"]) + await expect(popup.getByRole("button", { name: `Connect ${provider}`, exact: true })).toBeVisible(); + await expect(popup.getByRole("heading", { name: "Google Drive", exact: true })).toBeFocused(); + await expect(popup.locator('input[type="password"]')).toHaveCount(0); + expect(state.consentCalls).toBe(0); + expect(blocked).toEqual({ external: 0, mcp: 0 }); + await popup.screenshot({ path: screenshot, fullPage: true }); + } finally { + await popup.close(); + } + await page.bringToFront(); + await expect(dialog).toBeVisible(); + await expect(dialog.getByLabel(/^Name /)).toHaveValue("selected-reading"); + await expect(dialog.getByLabel("Content links (one per line)")).toHaveValue(LINKS.join("\n")); +} + +for (const width of [1400, 375]) { + test.describe(`${width} px connected source`, () => { + test.use({ viewport: { width, height: 900 }, hasTouch: width === 375 }); + + test("an ordinary writer checks selected content, preserves a refused draft and adds to the chosen collection", async ({ + page, + }, testInfo) => { + const state = await installConnectedSource(page); + await page.goto(`/library?collection=${DESTINATION}`); + const sources = page.getByRole("button", { name: "Sources", exact: true }); + await sources.focus(); + await page.keyboard.press("Enter"); + await expect(page.getByRole("heading", { name: "Sources", exact: true })).toBeFocused(); + const opener = page.getByRole("button", { name: "Add a source", exact: true }); + await opener.focus(); + await page.keyboard.press("Enter"); + const dialog = page.getByRole("dialog", { name: "Add a document source · Research", exact: true }); + const name = dialog.getByLabel(/^Name /); + const links = dialog.getByLabel("Content links (one per line)"); + const check = dialog.getByRole("button", { name: "Check", exact: true }); + const add = dialog.getByRole("button", { name: "Add source", exact: true }); + await expect(name).toBeFocused(); + await expect(dialog.getByRole("radio", { name: "My connected account", exact: true })).toBeChecked(); + await expect(dialog.getByText(`Using ${SUBJECT}.`, { exact: true })).toBeVisible(); + await expect(dialog.getByRole("radio", { name: "Shared credential", exact: true })).toHaveCount(0); + await expect(dialog.locator('input[type="password"]')).toHaveCount(0); + await name.fill("selected-reading"); + await links.fill(LINKS.join("\n")); + await expect(add).toBeDisabled(); + await check.click(); + await expect(dialog.getByRole("alert")).toContainText("Selected content could not be read."); + await expect(name).toHaveValue("selected-reading"); + await expect(links).toHaveValue(LINKS.join("\n")); + await expect(add).toBeDisabled(); + await check.click(); + await expect(dialog.getByText("Access checked for 2 items. This check does not import documents.")).toBeVisible(); + await expect(add).toBeEnabled(); + + state.connected = false; + await dialog.getByRole("button", { name: "Refresh account", exact: true }).click(); + await expect(dialog.getByText("Connect your Google Drive account to continue.", { exact: true })).toBeVisible(); + await expect(add).toBeDisabled(); + await expect(check).toBeDisabled(); + await expect(links).toHaveValue(LINKS.join("\n")); + await checkConnectionTab(page, dialog, state, testInfo.outputPath(`connected-source-${width}-accounts.png`)); + state.connected = true; + await dialog.getByRole("button", { name: "Refresh account", exact: true }).click(); + await expect(dialog.getByText(`Using ${SUBJECT}.`, { exact: true })).toBeVisible(); + await expect(add).toBeDisabled(); + await check.click(); + await expect(add).toBeEnabled(); + await checkLayout(page, dialog, width); + await page.screenshot({ path: testInfo.outputPath(`connected-source-${width}-checked.png`), fullPage: true }); + await add.click(); + await expect(dialog).toHaveCount(0); + await expect(opener).toBeFocused(); + const rows = page.getByRole("list", { name: "Document sources" }); + await expect(rows).toContainText("Never synced"); + state.complete = true; + await page.getByRole("button", { name: "Refresh sources", exact: true }).click(); + await expect(rows).toContainText("Synced"); + await expect(rows).toContainText("2 documents listed · 2 written"); + await rows.getByRole("button", { name: "Actions for selected-reading", exact: true }).click(); + await expect(page.getByRole("menuitem", { name: "Manage connection", exact: true })).toBeVisible(); + await expect(page.getByRole("menuitem", { name: "Rotate credential", exact: true })).toHaveCount(0); + await expect(page.getByRole("menuitem", { name: "Push notifications", exact: true })).toHaveCount(0); + await page.keyboard.press("Escape"); + expect(await violations(page)).toEqual([]); + await page.screenshot({ path: testInfo.outputPath(`connected-source-${width}-synced.png`), fullPage: true }); + + const adds = state.sourceCalls.filter(({ path }) => path.endsWith(".add")); + expect(adds.map(({ body }) => body.dry_run)).toEqual([true, true, true, false]); + for (const { path, body } of state.sourceCalls) { + expect(body).toMatchObject({ org: "dev", namespace: DESTINATION }); + if (!path.endsWith(".add")) continue; + expect(body).toMatchObject({ + name: "selected-reading", + kind: "gdrive", + credential_mode: "connected", + links: LINKS, + include: ["docs", "slides", "pdf", "pptx"], + }); + for (const field of ["credential", "config", "subject", "owner_subject", "account_id"]) + expect(body).not.toHaveProperty(field); + } + expect(state.accountReads).toBeGreaterThanOrEqual(3); + expect(state.consentCalls).toBe(0); + }); + }); +} diff --git a/frontend/src/api/connections.ts b/frontend/src/api/connections.ts index 9a596491bd..7128e1bc05 100644 --- a/frontend/src/api/connections.ts +++ b/frontend/src/api/connections.ts @@ -7,6 +7,7 @@ export type ConnectionKind = (typeof CONNECTION_KINDS)[number]; export interface ConnectionEntry { kind: ConnectionKind; connected: boolean; + connect_available?: boolean; account_email: string | null; scopes: string[]; connect_url: string; diff --git a/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx b/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx new file mode 100644 index 0000000000..6f173c30c8 --- /dev/null +++ b/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx @@ -0,0 +1,256 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import userEvent from "@testing-library/user-event"; +import { act, cleanup, fireEvent, render, screen, waitFor } from "../test/rtl"; +import { axeViolations, unlabeledControls } from "../test/axe"; +import { listConnections } from "../api/connections"; +import { addKnowledgeSource } from "../api/knowledgeSources"; +import { ApiError } from "../lib/errors"; +import { sourceAddBody } from "../lib/knowledgeSources"; +import { useStore } from "../lib/state"; +import { AddSourceDialog } from "./KnowledgeSourceDialog"; + +vi.mock("../api/connections", async (load) => ({ + ...(await load()), + listConnections: vi.fn(), +})); +vi.mock("../api/knowledgeSources", async (load) => ({ + ...(await load()), + addKnowledgeSource: vi.fn(), +})); + +const LINK = "https://drive.google.com/file/d/selected-file/view"; +const OTHER = "https://docs.google.com/document/d/selected-doc/edit"; +const NS = { org: "acme", namespace: "col-team" }; +const connections = (connected = true, available = true) => ({ + connections: [ + { + kind: "gdrive" as const, + connected, + connect_available: available, + account_email: connected ? "reader@example.com" : null, + scopes: [], + connect_url: "", + }, + { + kind: "notion" as const, + connected: true, + connect_available: true, + account_email: null, + scopes: [], + connect_url: "", + }, + { + kind: "linear" as const, + connected: true, + connect_available: true, + account_email: null, + scopes: [], + connect_url: "", + }, + ], +}); +const result = (dry_run = true) => ({ + name: "selected", + kind: "gdrive", + namespace: NS.namespace, + config: {}, + probe: { documents: 0, more: false, sample: [], checked: 2, sampled: false }, + dry_run, +}); + +beforeEach(() => { + vi.resetAllMocks(); + useStore.setState({ + authed: true, + sessionExpired: false, + identity: { ...NS, agent: "loom-ui" }, + whoami: { authenticated: true, subject: "reader", org: NS.org, roles: ["writer"] }, + }); + vi.mocked(listConnections).mockResolvedValue(connections()); + vi.mocked(addKnowledgeSource).mockImplementation(async (_id, _form, dry) => result(dry)); +}); +afterEach(() => { + cleanup(); + useStore.setState({ whoami: null, identity: null, authed: false, sessionExpired: false }); +}); + +function open(onAdded = vi.fn()) { + render( + {}} {...NS} label="Team library" allowShared={false} onAdded={onAdded} />, + ); + return onAdded; +} +async function fill() { + await screen.findByText("Using reader@example.com."); + fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "selected" } }); + fireEvent.input(screen.getByLabelText(/^Content links/), { target: { value: `${LINK}\n${OTHER}` } }); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(`${LINK}\n${OTHER}`); + expect(screen.queryByRole("list", { name: "Before checking" })?.textContent ?? "").toBe(""); + await waitFor(() => expect(screen.getByRole("button", { name: "Check" })).toBeEnabled()); +} +async function check() { + await waitFor(() => expect(screen.getByRole("button", { name: "Check" })).toBeEnabled()); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + await screen.findByText(/Access checked for 2 items/); +} + +describe("connected account to document source", () => { + it("checks the exact selection before Add and sends no credential or unselected config", async () => { + const added = open(); + await fill(); + expect(screen.queryByLabelText(/Service account key/)).toBeNull(); + expect(screen.getByText("Team library")).toBeVisible(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + await check(); + await userEvent.click(screen.getByRole("button", { name: "Add source" })); + await waitFor(() => expect(added).toHaveBeenCalledWith("selected")); + const bodies = vi + .mocked(addKnowledgeSource) + .mock.calls.map(([id, form, dry]) => sourceAddBody(id.org, id.namespace, form, dry)); + expect(bodies).toEqual( + [true, false].map((dry_run) => ({ + ...NS, + name: "selected", + kind: "gdrive", + links: [LINK, OTHER], + include: ["docs", "slides", "pdf", "pptx"], + credential_mode: "connected", + dry_run, + })), + ); + expect(unlabeledControls(document.body)).toEqual([]); + expect(await axeViolations(document.body)).toEqual([]); + }); + + it("keeps the draft on a refused check and invalidates a passed check after content or account refresh", async () => { + open(); + await fill(); + vi.mocked(addKnowledgeSource).mockRejectedValueOnce( + new ApiError({ + status: 400, + code: "SOURCE_UNREACHABLE", + message: "Share the selected file with your connected account.", + }), + ); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + expect(await screen.findByRole("alert")).toHaveTextContent("Share the selected file"); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(`${LINK}\n${OTHER}`); + await check(); + fireEvent.input(screen.getByLabelText(/^Content links/), { target: { value: LINK } }); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + await check(); + await userEvent.click(screen.getByRole("button", { name: "Refresh account" })); + await screen.findByText("Using reader@example.com."); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(LINK); + }); + + it.each([ + ["Notion", "notion", "https://www.notion.so/Selected-1234567890abcdef1234567890abcdef"], + ["Linear", "linear", "https://linear.app/team/project/selected-project"], + ] as const)("uses the caller's %s account and only the selected links", async (label, kind, link) => { + open(); + await screen.findByText("Using reader@example.com."); + await userEvent.click(screen.getByRole("radio", { name: label })); + await screen.findByText(`Using your ${label} account.`); + fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "selected" } }); + fireEvent.input(screen.getByLabelText(/^Content links/), { target: { value: link } }); + await check(); + const [id, form, dry] = vi.mocked(addKnowledgeSource).mock.calls[0]; + expect(sourceAddBody(id.org, id.namespace, form, dry)).toEqual({ + ...NS, + name: "selected", + kind, + links: [link], + credential_mode: "connected", + dry_run: true, + }); + }); + + it("clears a shared credential draft when administration is removed while the dialog is open", async () => { + const props = { open: true, onOpenChange: vi.fn(), ...NS, label: "Team library", onAdded: vi.fn() }; + const { rerender } = render(); + await screen.findByText("Using reader@example.com."); + await userEvent.click(screen.getByRole("radio", { name: "Shared credential" })); + fireEvent.change(screen.getByLabelText(/^Service account key/), { target: { value: "private-shared-key" } }); + fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "old-admin-draft" } }); + rerender(); + expect(screen.queryByLabelText(/^Service account key/)).toBeNull(); + expect(screen.getByLabelText(/^Name/)).toHaveValue(""); + expect(screen.getByRole("radio", { name: "My connected account" })).toBeChecked(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("keeps keyboard focus on Refresh account during its own read", async () => { + open(); + await screen.findByText("Using reader@example.com."); + let resolve!: (value: ReturnType) => void; + vi.mocked(listConnections).mockImplementationOnce( + () => + new Promise((yes) => { + resolve = yes; + }), + ); + const refresh = screen.getByRole("button", { name: "Refresh account" }); + await userEvent.click(refresh); + expect(refresh).toHaveFocus(); + expect(refresh).toHaveAttribute("aria-busy", "true"); + await userEvent.click(refresh); + expect(listConnections).toHaveBeenCalledTimes(2); + await act(async () => resolve(connections())); + expect(refresh).toHaveFocus(); + expect(refresh).not.toHaveAttribute("aria-busy"); + }); + + it("shows an actionable operator requirement without asking for OAuth app secrets", async () => { + vi.mocked(listConnections).mockResolvedValue(connections(false, false)); + open(); + expect(await screen.findByText(/An operator must configure Google Drive/)).toBeVisible(); + expect(screen.queryByRole("link", { name: /Connect Google Drive/ })).toBeNull(); + expect(screen.getByRole("button", { name: "Check" })).toBeDisabled(); + expect(document.querySelector('input[type="password"]')).toBeNull(); + }); + + it("opens account management separately and keeps private content out of history and storage", async () => { + vi.mocked(listConnections).mockResolvedValue(connections(false)); + open(); + const link = await screen.findByRole("link", { name: /Connect Google Drive/ }); + expect(link).toHaveAttribute("target", "_blank"); + expect(link).toHaveAttribute("rel", "noreferrer"); + expect(link).toHaveAttribute("href", "/settings/connected-accounts?kind=gdrive&from=source"); + fireEvent.input(screen.getByLabelText(/^Content links/), { target: { value: LINK } }); + expect(`${location.href}${JSON.stringify(localStorage)}${JSON.stringify(sessionStorage)}`).not.toContain(LINK); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("drops late account and check answers after the signed-in principal changes", async () => { + let resolve!: (value: ReturnType) => void; + vi.mocked(addKnowledgeSource).mockImplementation( + () => + new Promise((yes) => { + resolve = yes; + }), + ); + const added = open(); + await fill(); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + await waitFor(() => expect(addKnowledgeSource).toHaveBeenCalledTimes(1)); + act(() => useStore.setState({ whoami: { authenticated: true, subject: "other", org: NS.org, roles: ["writer"] } })); + await act(async () => resolve(result())); + expect(screen.queryByText(/Access checked for/)).toBeNull(); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(""); + expect(added).not.toHaveBeenCalled(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + }); + + it("does not list personal accounts for a service account", async () => { + useStore.setState({ + whoami: { authenticated: true, subject: "sa:robot", org: NS.org, roles: ["writer"], is_service_account: true }, + }); + open(); + expect(screen.getByText(/Sign in with a personal account/)).toBeVisible(); + await act(async () => {}); + expect(listConnections).not.toHaveBeenCalled(); + }); +}); diff --git a/frontend/src/flows/KnowledgeSourceDialog.test.tsx b/frontend/src/flows/KnowledgeSourceDialog.test.tsx index 3024fc2902..755c6f90d6 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.test.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.test.tsx @@ -5,7 +5,7 @@ * carries it. Check probes on open and stops when the dialog closes; Sync now records a * request; Retire needs the typed name and keeps the documents unless asked. */ -import { act, cleanup, fireEvent, render, screen, waitFor } from "../test/rtl"; +import { act, cleanup, fireEvent, render, screen, waitFor, within } from "../test/rtl"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; import { @@ -188,14 +188,16 @@ describe("AddSourceDialog's kinds", () => { it("offers each kind, the shared folder first", () => { renderAdd(); - const kinds = screen.getAllByRole("radio").map((r) => r.getAttribute("aria-label")); - expect(kinds).toEqual(["Shared folder", "Project documents", "Pages", "Zoom transcripts"]); - expect(screen.getByRole("radio", { name: "Shared folder" })).toHaveAttribute("aria-checked", "true"); + const kinds = within(screen.getByRole("radiogroup", { name: "Kind" })) + .getAllByRole("radio") + .map((r) => r.getAttribute("aria-label")); + expect(kinds).toEqual(["Google Drive", "Linear", "Notion", "Zoom transcripts"]); + expect(screen.getByRole("radio", { name: "Google Drive" })).toHaveAttribute("aria-checked", "true"); }); it("adds a project tracker's projects with a read-only API key", async () => { renderAdd(); - fireEvent.click(screen.getByRole("radio", { name: "Project documents" })); + fireEvent.click(screen.getByRole("radio", { name: "Linear" })); expect(screen.queryByLabelText(/^Folders/)).toBeNull(); fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "roadmap" } }); await typeInto(/^Projects/, PROJECT); @@ -219,7 +221,7 @@ describe("AddSourceDialog's kinds", () => { it("adds a page store's pages and databases with an integration token", async () => { renderAdd(); - fireEvent.click(screen.getByRole("radio", { name: "Pages" })); + fireEvent.click(screen.getByRole("radio", { name: "Notion" })); fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "wiki" } }); await typeInto(/^Pages/, `https://pages.example/team/Home-${PAGE}`); await typeInto(/^Databases/, DATABASE); @@ -267,9 +269,9 @@ describe("AddSourceDialog's kinds", () => { it("routes Meet notes and transcripts through the shared folder form", async () => { renderAdd(); fireEvent.click(screen.getByRole("radio", { name: "Zoom transcripts" })); - expect(screen.getByText(/For Meet notes and transcripts, choose Shared folder/)).toBeInTheDocument(); + expect(screen.getByText(/For Meet notes and transcripts, choose Google Drive/)).toBeInTheDocument(); fireEvent.change(screen.getByLabelText(/^Read-only credential/), { target: { value: ZOOM_KEY } }); - fireEvent.click(screen.getByRole("radio", { name: "Shared folder" })); + fireEvent.click(screen.getByRole("radio", { name: "Google Drive" })); expect(screen.getByText(/including Meet notes and transcripts saved as documents/)).toBeInTheDocument(); expect(screen.getByLabelText(/^Service account key/)).toHaveValue(""); expect(screen.getByRole("checkbox", { name: "Documents" })).toBeChecked(); @@ -291,13 +293,13 @@ describe("AddSourceDialog's kinds", () => { await screen.findByText(/lists 12 documents/); const add = screen.getByRole("button", { name: "Add source" }); expect(add).toBeEnabled(); - fireEvent.click(screen.getByRole("radio", { name: "Project documents" })); + fireEvent.click(screen.getByRole("radio", { name: "Linear" })); expect(add).toBeDisabled(); // The key was typed for the shared folder: another kind never sends it. expect(screen.getByLabelText(/^Read-only API key/)).toHaveValue(""); // Back to the shared folder: the folders typed are still there; the key is asked // for again, and the same key gives the form that passed. - fireEvent.click(screen.getByRole("radio", { name: "Shared folder" })); + fireEvent.click(screen.getByRole("radio", { name: "Google Drive" })); expect(screen.getByLabelText(/^Folders/)).toHaveValue(FOLDER); expect(screen.getByLabelText(/^Service account key/)).toHaveValue(""); fireEvent.change(screen.getByLabelText(/^Service account key/), { target: { value: KEY } }); @@ -313,10 +315,10 @@ describe("AddSourceDialog's kinds", () => { await typeInto(/^Meetings/, "81234567890"); fireEvent.change(screen.getByLabelText(/^Read-only credential/), { target: { value: ZOOM_KEY } }); fireEvent.click(screen.getByRole("button", { name: "Check" })); - expect(screen.getByRole("radio", { name: "Pages" })).toBeDisabled(); + expect(screen.getByRole("radio", { name: "Notion" })).toBeDisabled(); expect(screen.queryByRole("radio", { name: "meet" })).toBeNull(); await act(async () => answer(addResult(true))); - expect(screen.getByRole("radio", { name: "Pages" })).toBeEnabled(); + expect(screen.getByRole("radio", { name: "Notion" })).toBeEnabled(); expect(screen.queryByRole("radio", { name: "zoom" })).toBeNull(); }); @@ -324,7 +326,7 @@ describe("AddSourceDialog's kinds", () => { const message = "config.project_ids: at most 20 projects"; vi.mocked(addKnowledgeSource).mockRejectedValue(new ApiError({ status: 400, code: "BAD_CONFIG", message })); renderAdd(); - fireEvent.click(screen.getByRole("radio", { name: "Project documents" })); + fireEvent.click(screen.getByRole("radio", { name: "Linear" })); fireEvent.change(screen.getByLabelText(/^Name/), { target: { value: "roadmap" } }); await typeInto(/^Projects/, PROJECT); fireEvent.change(screen.getByLabelText(/^Read-only API key/), { target: { value: SECRET } }); @@ -347,7 +349,7 @@ describe("AddSourceDialog's kinds", () => { fireEvent.click(screen.getByRole("button", { name: "Check" })); expect(await screen.findByRole("alert")).toHaveTextContent(message); // The error was the meeting store's: another kind does not show it. - fireEvent.click(screen.getByRole("radio", { name: "Pages" })); + fireEvent.click(screen.getByRole("radio", { name: "Notion" })); expect(screen.queryByRole("alert")).toBeNull(); expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); expectKeyNowhere(); diff --git a/frontend/src/flows/KnowledgeSourceDialog.tsx b/frontend/src/flows/KnowledgeSourceDialog.tsx index 6b6b7a540a..249829f434 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.tsx @@ -3,18 +3,14 @@ * Loaded on first use, behind `lazy()`, by the Sources list (a repository card's, or the * Library's). * - * Add probes first: the form takes a kind and that kind's ids, Check sends - * `knowledge.source.add` with `dry_run` (the credential is tried, nothing is stored) and - * shows what the source holds; Add stays off until the form it would send is the one - * that passed. A credential is typed into a password field, held in the dialog's state - * only, sent once and never shown back. Changing the kind clears it, so - * a credential is only ever sent to the store it was typed for. + * Check probes the selected links or IDs before Add stores a source. Own connected + * accounts use the server's caller binding. Shared credentials remain in dialog + * memory, clear when their provider or account mode changes, and are never displayed. */ -import { useState } from "react"; +import { useRef, useState } from "react"; import { Button } from "@clickhouse/click-ui/Button"; import { Checkbox } from "@clickhouse/click-ui/Checkbox"; import { PasswordField } from "@clickhouse/click-ui/PasswordField"; -import { TextAreaField } from "@clickhouse/click-ui/TextAreaField"; import { TextField } from "@clickhouse/click-ui/TextField"; import { addKnowledgeSource, @@ -23,27 +19,27 @@ import { rotateKnowledgeSource, syncKnowledgeSourceNow, } from "../api/knowledgeSources"; -import { humanizeError } from "../lib/errorWords"; -import { useAbortableEffect } from "../lib/hooks"; +import { committedAnyway, humanizeError } from "../lib/errorWords"; +import { canCallOwnConnections } from "../lib/canCall"; +import { useStore } from "../lib/state"; +import { useAbortableEffect, useMounted } from "../lib/hooks"; +import { useSourceAccount } from "./SourceAccountField"; +import { SourceRegistrationFields, type SetField } from "./SourceRegistrationFields"; import { credentialLabel, emptySourceForm, - INCLUDE_OPTIONS, KIND_FORM, - KIND_OPTIONS, normalisedName, probeCount, retiredMessage, sourceFormFingerprint, sourceFormProblems, sourceFormTouched, - toggleInclude, type KnowledgeSourceCheckResult, type KnowledgeSourceProbe, type SourceForm, } from "../lib/knowledgeSources"; import { toast } from "../components/Toast"; -import { ChoiceGroup } from "../components/ui/ChoiceGroup"; import { Alert } from "../components/ui/Alert"; import { Loading } from "../components/ui/Loading"; import { CancelButton, DialogFrame, ErrorNote } from "./TaskDialogFrame"; @@ -56,13 +52,23 @@ interface DialogProps { } /** What a probe found: the count and up to five titles. */ -function ProbeResult({ probe, lead }: { probe: KnowledgeSourceProbe; lead: string }) { +function ProbeResult({ + probe, + lead, + connected = false, +}: { + probe: KnowledgeSourceProbe; + lead: string; + connected?: boolean; +}) { return (

- {lead} {probeCount(probe)}. + {connected + ? `Access checked for ${probe.checked ?? 0}${probe.sampled ? " sampled" : ""} items. This check does not import documents.` + : `${lead} ${probeCount(probe)}.`}

- {probe.sample.length > 0 && ( + {!connected && probe.sample.length > 0 && (
    {probe.sample.map((t, i) => ( // Titles can repeat, so the position is the key. @@ -76,125 +82,119 @@ function ProbeResult({ probe, lead }: { probe: KnowledgeSourceProbe; lead: strin ); } -type SetField = (key: K, value: SourceForm[K]) => void; +type AddSourceProps = DialogProps & { label: string; onAdded: (name: string) => void; allowShared?: boolean }; -/** The chosen kind's ids and filters, as the server's config for that kind takes them. */ -function SourceFields({ form, set }: { form: SourceForm; set: SetField }) { - switch (form.kind) { - case "gdrive": - return ( - <> - set("folders", v)} - /> -
    - File kinds to pull -
    - {INCLUDE_OPTIONS.map((o) => ( - set("include", toggleInclude(form.include, o.value))} - /> - ))} -
    -
    - - ); - case "linear": - return ( - set("projects", v)} - /> - ); - case "notion": - return ( - <> - set("pages", v)} - /> - set("databases", v)} - /> - - ); - case "recording": - return ( - set("meetings", v)} - /> - ); - } +export function AddSourceDialog(props: AddSourceProps) { + const scope = useStore((s) => + JSON.stringify([ + s.identity?.org, + s.whoami?.org, + s.whoami?.subject, + s.whoami?.roles, + s.whoami?.is_service_account, + s.authed, + s.sessionExpired, + ]), + ); + return ; } -export function AddSourceDialog({ +function SourceRegistration({ open, onOpenChange, org, namespace, label, onAdded, -}: DialogProps & { label: string; onAdded: (name: string) => void }) { - const [form, setForm] = useState(emptySourceForm); + allowShared = true, +}: AddSourceProps) { + const whoami = useStore((s) => s.whoami); + const mounted = useMounted(); + const actor = JSON.stringify([ + useStore.getState().identity?.org, + whoami?.org, + whoami?.subject, + whoami?.roles, + whoami?.is_service_account, + useStore.getState().authed, + useStore.getState().sessionExpired, + ]); + const current = () => { + const s = useStore.getState(); + return ( + mounted.current && + actor === + JSON.stringify([ + s.identity?.org, + s.whoami?.org, + s.whoami?.subject, + s.whoami?.roles, + s.whoami?.is_service_account, + s.authed, + s.sessionExpired, + ]) + ); + }; + const running = useRef(false); + const [form, setForm] = useState(() => { + const f = emptySourceForm(); + return !allowShared || (whoami?.org === org && canCallOwnConnections(whoami)) + ? { ...f, credentialMode: "connected", inputMode: "links" } + : f; + }); + const connected = form.credentialMode === "connected"; + const account = useSourceAccount(org, form.kind === "recording" ? null : form.kind, connected); const [checked, setChecked] = useState<{ fingerprint: string; probe: KnowledgeSourceProbe } | null>(null); const [busy, setBusy] = useState<"check" | "add" | null>(null); const [error, setError] = useState(null); const set: SetField = (key, value) => { // The credential and last error belong to the kind they were entered for. - const switched = key === "kind" && value !== form.kind; + const switched = (key === "kind" && value !== form.kind) || key === "credentialMode"; if (switched) setError(null); setForm((f) => ({ ...f, [key]: value, ...(switched ? { credential: "" } : {}), + ...(key === "kind" && value === "recording" ? { credentialMode: "shared" } : {}), })); }; const problems = sourceFormProblems(form); - const fingerprint = sourceFormFingerprint(form); - const passed = checked !== null && checked.fingerprint === fingerprint; + const fingerprint = JSON.stringify([ + org, + namespace, + whoami?.subject, + sourceFormFingerprint(form), + connected ? account.stamp : "shared", + ]); + const accountReady = !connected || account.ready; + const passed = checked !== null && checked.fingerprint === fingerprint && accountReady; const run = async (dryRun: boolean) => { + if (!current() || running.current || problems.length || !accountReady || (!dryRun && !passed)) return; + running.current = true; setBusy(dryRun ? "check" : "add"); setError(null); try { const out = await addKnowledgeSource({ org, namespace }, form, dryRun); + if (!current()) return; if (dryRun) setChecked({ fingerprint, probe: out.probe }); else { onAdded(out.name); onOpenChange(false); } } catch (e) { - // A failed add keeps the check that passed, so Add can be tried again. + if (!current()) return; + if (!dryRun && committedAnyway("Add source", e)) { + onAdded(normalisedName(form.name)); + onOpenChange(false); + return; + } + // A refused add preserves the draft; it never switches account or content. if (dryRun) setChecked(null); setError(humanizeError(e)); } finally { - setBusy(null); + running.current = false; + if (mounted.current) setBusy(null); } }; @@ -204,7 +204,9 @@ export function AddSourceDialog({ onOpenChange={onOpenChange} width={560} title={`Add a document source · ${label}`} - description={KIND_FORM[form.kind].about} + description={ + connected ? "Choose your account and the content to keep in this destination." : KIND_FORM[form.kind].about + } footer={ <> @@ -212,7 +214,7 @@ export function AddSourceDialog({ type="secondary" htmlType="button" loading={busy === "check"} - disabled={busy !== null || problems.length > 0} + disabled={busy !== null || problems.length > 0 || !accountReady} onClick={() => void run(true)} > Check @@ -229,29 +231,13 @@ export function AddSourceDialog({ } > - set("name", v)} - autoFocus - /> - {/* Locked while a call runs, so its answer lands on the form it was sent for. */} - set("kind", v)} - options={KIND_OPTIONS} - /> - - set("credential", v)} + {problems.length > 0 && sourceFormTouched(form) && (
      @@ -261,7 +247,11 @@ export function AddSourceDialog({
    )} {checked && passed && ( - + )} {checked && !passed &&

    The form changed since the check: check again.

    } {error && {error}} @@ -269,7 +259,14 @@ export function AddSourceDialog({ ); } -export function CheckSourceDialog({ open, onOpenChange, org, namespace, name }: DialogProps & { name: string }) { +export function CheckSourceDialog({ + open, + onOpenChange, + org, + namespace, + name, + connected = false, +}: DialogProps & { name: string; connected?: boolean }) { const [result, setResult] = useState(null); const [error, setError] = useState(null); const [nonce, setNonce] = useState(0); @@ -293,7 +290,7 @@ export function CheckSourceDialog({ open, onOpenChange, org, namespace, name }: onOpenChange={onOpenChange} width={460} title={`Check ${name}`} - description="The stored credential lists the source now; nothing is written." + description="Checks access to this source; nothing is imported." footer={ <> @@ -304,7 +301,7 @@ export function CheckSourceDialog({ open, onOpenChange, org, namespace, name }: } > {running && } - {result?.reachable && } + {result?.reachable && } {result && !result.reachable && } {error && {error}} diff --git a/frontend/src/flows/SourceAccountField.tsx b/frontend/src/flows/SourceAccountField.tsx new file mode 100644 index 0000000000..fe94cec047 --- /dev/null +++ b/frontend/src/flows/SourceAccountField.tsx @@ -0,0 +1,106 @@ +import { useEffect, useRef, useState } from "react"; +import { BusyButton } from "../components/ui/BusyButton"; +import { listConnections, type ConnectionEntry, type ConnectionKind } from "../api/connections"; +import { Alert } from "../components/ui/Alert"; +import { ExternalLink } from "../components/ui/ExternalLink"; +import { Loading } from "../components/ui/Loading"; +import { canCallOwnConnections } from "../lib/canCall"; +import { humanizeError } from "../lib/errorWords"; +import { useAbortableEffect } from "../lib/hooks"; +import { useStore } from "../lib/state"; + +const LABELS: Record = { gdrive: "Google Drive", notion: "Notion", linear: "Linear" }; + +/** A refresh invalidates a prior probe even when consent returns the same display email. */ +export function useSourceAccount(org: string, kind: ConnectionKind | null, enabled: boolean) { + const whoami = useStore((s) => s.whoami); + const identityOrg = useStore((s) => s.identity?.org); + const authed = useStore((s) => s.authed); + const expired = useStore((s) => s.sessionExpired); + const allowed = authed && !expired && identityOrg === org && canCallOwnConnections(whoami) && whoami?.org === org; + const scope = JSON.stringify([org, whoami?.subject, kind, enabled, allowed]); + const currentScope = useRef(scope); + currentScope.current = scope; + const sequence = useRef(0); + const [nonce, setNonce] = useState(0); + const [state, setState] = useState<{ + scope: string; + entry?: ConnectionEntry; + error?: unknown; + reading: boolean; + version: number; + }>({ scope: "", reading: true, version: 0 }); + const reload = () => setNonce((n) => n + 1); + useEffect(() => { + if (!enabled || !allowed) return; + const refresh = () => setNonce((n) => n + 1); + window.addEventListener("focus", refresh); + return () => window.removeEventListener("focus", refresh); + }, [enabled, allowed]); + useAbortableEffect( + async (signal, live) => { + if (!enabled || !allowed || !kind) return; + const version = ++sequence.current; + setState({ scope, reading: true, version }); + try { + const result = await listConnections(org, { signal }); + if (live() && currentScope.current === scope) + setState({ scope, entry: result.connections.find((entry) => entry.kind === kind), reading: false, version }); + } catch (error) { + if (live() && currentScope.current === scope) setState({ scope, error, reading: false, version }); + } + }, + [scope, nonce], + ); + const current = state.scope === scope; + const entry = current ? state.entry : undefined; + const reading = allowed && (!current || state.reading); + return { + allowed, + entry, + reading, + reload, + error: current ? state.error : undefined, + ready: allowed && current && !reading && entry?.connected === true && entry.connect_available !== false, + stamp: JSON.stringify([scope, state.version]), + }; +} + +export function SourceAccountField({ + kind, + account, + busy, +}: { + kind: ConnectionKind; + account: ReturnType; + busy: boolean; +}) { + const label = LABELS[kind]; + if (!account.allowed) return

    Sign in with a personal account to choose your connected account.

    ; + return ( +
    + {account.reading && } + {!!account.error && } + {!account.reading && !account.error && ( +

    + {account.entry?.connect_available === false + ? `An operator must configure ${label} connections on this deployment before you can use this account.` + : account.entry?.connected + ? `Using ${account.entry.account_email || `your ${label} account`}.` + : `Connect your ${label} account to continue.`} +

    + )} + {account.entry?.connect_available !== false && ( +
    + + {account.entry?.connected ? `Manage ${label} connection` : `Connect ${label}`} + +
    + )} +

    After connecting, return to this tab. Your source details stay here.

    + + Refresh account + +
    + ); +} diff --git a/frontend/src/flows/SourceRegistrationFields.tsx b/frontend/src/flows/SourceRegistrationFields.tsx new file mode 100644 index 0000000000..127cd5b498 --- /dev/null +++ b/frontend/src/flows/SourceRegistrationFields.tsx @@ -0,0 +1,202 @@ +import { Checkbox } from "@clickhouse/click-ui/Checkbox"; +import { PasswordField } from "@clickhouse/click-ui/PasswordField"; +import { TextAreaField } from "@clickhouse/click-ui/TextAreaField"; +import { TextField } from "@clickhouse/click-ui/TextField"; +import { ChoiceGroup } from "../components/ui/ChoiceGroup"; +import { + credentialLabel, + INCLUDE_OPTIONS, + KIND_OPTIONS, + toggleInclude, + type SourceForm, +} from "../lib/knowledgeSources"; +import { SourceAccountField, type useSourceAccount } from "./SourceAccountField"; + +export type SetField = (key: K, value: SourceForm[K]) => void; + +function SourceIncludes({ form, set }: { form: SourceForm; set: SetField }) { + return ( +
    + File kinds to pull +
    + {INCLUDE_OPTIONS.map((o) => ( + set("include", toggleInclude(form.include, o.value))} + /> + ))} +
    +
    + ); +} + +/** The chosen kind's ids and filters, as the server's config for that kind takes them. */ +function SourceFields({ form, set }: { form: SourceForm; set: SetField }) { + switch (form.kind) { + case "gdrive": + return ( + <> + set("folders", v)} + /> + + + ); + case "linear": + return ( + set("projects", v)} + /> + ); + case "notion": + return ( + <> + set("pages", v)} + /> + set("databases", v)} + /> + + ); + case "recording": + return ( + set("meetings", v)} + /> + ); + } +} + +export function SourceRegistrationFields({ + form, + set, + label, + account, + busy, + allowShared, +}: { + form: SourceForm; + set: SetField; + label: string; + account: ReturnType; + busy: boolean; + allowShared: boolean; +}) { + const connected = form.credentialMode === "connected"; + return ( + <> +

    + Destination: {label} +

    +

    + To change the destination, close this dialog and choose a place in the Library. +

    +
    + set("name", v)} + autoFocus + /> + {/* Locked while a call runs, so its answer lands on the form it was sent for. */} + set("kind", v)} + options={KIND_OPTIONS.filter((option) => allowShared || option.value !== "recording")} + /> + {form.kind !== "recording" && ( + set("credentialMode", value)} + options={[ + { value: "connected", label: "My connected account" }, + ...(allowShared ? [{ value: "shared" as const, label: "Shared credential" }] : []), + ]} + /> + )} + {connected && form.kind !== "recording" ? ( + + ) : ( + set("credential", v)} + /> + )} +

    + {connected + ? "Only readers whose access Loom can verify with the provider can read connected content. If access cannot be verified, it stays hidden." + : "Everyone who can read this destination can read content imported with this shared credential. Provider permissions are not mirrored."} +

    + {form.kind !== "recording" && ( + set("inputMode", value)} + options={[ + { value: "links", label: "Paste links" }, + { value: "ids", label: "Enter IDs" }, + ]} + /> + )} + {form.inputMode === "links" && form.kind !== "recording" ? ( + <> + set("links", value)} + aria-describedby="ksrc-link-help" + /> + + {form.kind === "gdrive" && } + + ) : ( + + )} +
    + + ); +} diff --git a/frontend/src/lib/knowledgeSources.test.ts b/frontend/src/lib/knowledgeSources.test.ts index 03c393b204..7f11adea1f 100644 --- a/frontend/src/lib/knowledgeSources.test.ts +++ b/frontend/src/lib/knowledgeSources.test.ts @@ -18,6 +18,7 @@ import { toggleInclude, unconvertedDetail, type KnowledgeSourceSync, + type SourceForm, } from "./knowledgeSources"; function sync(over: Partial = {}): KnowledgeSourceSync { @@ -36,6 +37,12 @@ function sync(over: Partial = {}): KnowledgeSourceSync { }; } +function idConfig(form: SourceForm) { + const body = sourceAddBody("o", "code-x", form, true); + if (!("config" in body)) throw new Error("Expected the ID form to send config"); + return body.config; +} + const FOLDER = "1AbCdEfGhIjKlMnOp"; describe("syncBadge", () => { @@ -47,11 +54,11 @@ describe("syncBadge", () => { expect(syncBadge(sync({ status: "failed", error: "http_403" })).tip).toContain("http_403"); }); - it("directs retired Meet sources to Shared folder without suggesting credential rotation", () => { + it("directs retired Meet sources to Google Drive without suggesting credential rotation", () => { expect(syncBadge(sync({ status: "provider_removed" }))).toEqual({ text: "Meet sync retired", tone: "warn", - tip: "Add a Shared folder source for Meet transcripts and notes. Existing documents stay unless you ask to delete them.", + tip: "Add a Google Drive source for Meet transcripts and notes. Existing documents stay unless you ask to delete them.", }); }); @@ -182,9 +189,10 @@ describe("the add form's other kinds", () => { expect(pageIds(badPeek)).toEqual([badPeek]); // In the databases field the same link is its database. expect(databaseIds(peekLink)).toEqual([DATABASE]); - expect(sourceAddBody("o", "x", { ...emptySourceForm(), kind: "notion", databases: peekLink }, true).config).toEqual( - { page_ids: [], database_ids: [DATABASE] }, - ); + expect(idConfig({ ...emptySourceForm(), kind: "notion", databases: peekLink })).toEqual({ + page_ids: [], + database_ids: [DATABASE], + }); // Kept as typed, so the problem names it: an id one digit long is not cut to 32. expect(pageIds(`${PAGE}0`)).toEqual([`${PAGE}0`]); // A link whose id is mistyped one digit long is not cut to another id. @@ -192,8 +200,8 @@ describe("the add form's other kinds", () => { expect(pageIds(`https://pages.example/team/${PAGE}0`)).toEqual([`https://pages.example/team/${PAGE}0`]); const f = { ...named, kind: "notion" as const, pages: PAGE, databases: DATABASE.toUpperCase() }; expect(sourceFormProblems(f)).toEqual([]); - expect(sourceAddBody("o", "code-x", f, true).config).toEqual({ page_ids: [PAGE], database_ids: [DATABASE] }); - expect(sourceAddBody("o", "code-x", { ...f, databases: "" }, true).config).toEqual({ + expect(idConfig(f)).toEqual({ page_ids: [PAGE], database_ids: [DATABASE] }); + expect(idConfig({ ...f, databases: "" })).toEqual({ page_ids: [PAGE], database_ids: [], }); @@ -212,7 +220,7 @@ describe("the add form's other kinds", () => { it("sends Zoom meeting ids and rejects space ids and meeting links", () => { const zoom = { ...named, kind: "recording" as const, meetings: "81234567890" }; expect(sourceFormProblems(zoom)).toEqual([]); - expect(sourceAddBody("o", "code-x", zoom, true).config).toEqual({ provider: "zoom", ids: ["81234567890"] }); + expect(idConfig(zoom)).toEqual({ provider: "zoom", ids: ["81234567890"] }); expect(sourceFormProblems({ ...zoom, meetings: "abc-DEF_1" })).toEqual([ "Not a meeting id (9-12 digits): abc-DEF_1.", ]); @@ -248,11 +256,11 @@ describe("the add form's other kinds", () => { it("sends only the chosen kind's config, so switching kinds keeps the other kinds' ids out of the body", () => { const f = { ...named, folders: FOLDER, projects: PROJECT }; - expect(sourceAddBody("o", "x", f, true).config).toEqual({ + expect(idConfig(f)).toEqual({ folder_ids: [FOLDER], include: ["docs", "slides", "pdf", "pptx"], }); - expect(sourceAddBody("o", "x", { ...f, kind: "linear" }, true).config).toEqual({ project_ids: [PROJECT] }); + expect(idConfig({ ...f, kind: "linear" })).toEqual({ project_ids: [PROJECT] }); expect(sourceFormFingerprint(f)).not.toBe(sourceFormFingerprint({ ...f, kind: "linear" })); }); }); @@ -282,7 +290,7 @@ describe("retiredMessage", () => { describe("kind words", () => { it("names each served kind and its credential, and an unknown one by its wire name", () => { - expect(kindLabel("gdrive")).toBe("shared folder"); + expect(kindLabel("gdrive")).toBe("Google Drive"); expect(kindLabel("linear")).toBe("project documents"); expect(kindLabel("notion")).toBe("pages"); expect(kindLabel("recording")).toBe("meeting transcripts"); @@ -293,3 +301,56 @@ describe("kind words", () => { expect(credentialLabel("other")).toMatch(/^Credential/); }); }); + +describe("connected source requests", () => { + it("sends the selected links and own-account mode, without any credential or unrelated ID config", () => { + const first = "https://drive.google.com/file/d/selected-file/view"; + const second = "https://docs.google.com/document/d/selected-doc/edit"; + const f: SourceForm = { + ...emptySourceForm(), + name: "selected", + credentialMode: "connected", + inputMode: "links", + links: `${first}\n${second}\n${first}`, + credential: "must-never-leave", + folders: "unselected-folder", + }; + expect(sourceFormProblems(f)).toEqual([]); + const body = sourceAddBody("o", "col-library", f, true); + expect(body).toEqual({ + org: "o", + namespace: "col-library", + name: "selected", + kind: "gdrive", + credential_mode: "connected", + links: [first, second], + include: ["docs", "slides", "pdf", "pptx"], + dry_run: true, + }); + expect(body).not.toHaveProperty("credential"); + expect(body).not.toHaveProperty("config"); + expect(sourceFormFingerprint(f)).not.toBe(sourceFormFingerprint({ ...f, links: second })); + expect(sourceFormFingerprint(f)).not.toBe(sourceFormFingerprint({ ...f, credentialMode: "shared" })); + }); + + it("bounds content selection and leaves unsupported link resolution to Check", () => { + const f: SourceForm = { + ...emptySourceForm(), + name: "selected", + kind: "linear", + credentialMode: "connected", + inputMode: "links", + links: "", + }; + expect(sourceFormProblems(f)).toContain("Paste at least one content link."); + expect(sourceFormProblems({ ...f, links: "http://linear.app/project" })).toContain( + "Use complete https links, one per line.", + ); + expect( + sourceFormProblems({ + ...f, + links: Array.from({ length: 21 }, (_, n) => `https://linear.app/project/${n}`).join("\n"), + }), + ).toContain("Use at most 20 links."); + }); +}); diff --git a/frontend/src/lib/knowledgeSources.ts b/frontend/src/lib/knowledgeSources.ts index c77b1a3a61..27a32aca08 100644 --- a/frontend/src/lib/knowledgeSources.ts +++ b/frontend/src/lib/knowledgeSources.ts @@ -1,5 +1,5 @@ /** - * The document sources a namespace's admins register for Loom to pull its documents from + * The document sources registered in a namespace for Loom to pull its documents from * (`knowledge.source.*`), as the repository card and the Library show them: the wire * shapes, the last sync in plain words, and the add form. Pure: no I/O, no React. */ @@ -28,6 +28,8 @@ export interface KnowledgeSourceSync { export interface KnowledgeSource { name: string; kind: string; + credential_mode?: "shared" | "connected"; + owner_subject?: string; /** The ids and filters the source reads, in the form that names ids per kind; {} when * an item has no place in it. Never a credential. */ config: Record; @@ -67,6 +69,8 @@ export interface KnowledgeSourceProbe { /** More than `documents` match: the count is a lower bound. */ more: boolean; sample: string[]; + checked?: number; + sampled?: boolean; } export interface KnowledgeSourceAddResult { @@ -106,7 +110,7 @@ export const SYNC_OFF = "Sync is off on this deployment: sources are stored and /** What each kind reads, in the words the card uses. */ const KIND_LABEL: Record = { - gdrive: "shared folder", + gdrive: "Google Drive", linear: "project documents", notion: "pages", recording: "meeting transcripts", @@ -162,7 +166,7 @@ export function syncBadge(sync: KnowledgeSourceSync): SyncBadge { return { text: "Meet sync retired", tone: "warn", - tip: "Add a Shared folder source for Meet transcripts and notes. Existing documents stay unless you ask to delete them.", + tip: "Add a Google Drive source for Meet transcripts and notes. Existing documents stay unless you ask to delete them.", }; case "rate_limited": return { text: "Rate limited", tone: "warn", tip: "The source limited the last sync; the next one retries." }; @@ -242,7 +246,7 @@ export const KIND_FORM: Record { - const label = k === "recording" ? "Zoom transcripts" : kindLabel(k); + const label = { gdrive: "Google Drive", notion: "Notion", linear: "Linear", recording: "Zoom transcripts" }[k]; return { value: k, label: label.charAt(0).toUpperCase() + label.slice(1) }; }); @@ -272,6 +276,9 @@ export const DEFAULT_INCLUDE = ["docs", "slides", "pdf", "pptx"] as const; export interface SourceForm { name: string; kind: SourceKind; + credentialMode: "shared" | "connected"; + inputMode: "ids" | "links"; + links: string; /** gdrive: folder ids or folder links, one per line or comma-separated. */ folders: string; include: string[]; @@ -297,6 +304,9 @@ export function emptySourceForm(): SourceForm { return { name: "", kind: "gdrive", + credentialMode: "shared", + inputMode: "ids", + links: "", folders: "", include: [...DEFAULT_INCLUDE], projects: "", @@ -420,6 +430,15 @@ function idProblems(ids: string[], re: RegExp, what: string, notOne: string): st } function configProblems(f: SourceForm): string[] { + if (f.inputMode === "links" && f.kind !== "recording") { + const links = sourceLinks(f.links); + const max = f.kind === "gdrive" ? 300 : MAX_IDS; + const problems = links.length ? [] : ["Paste at least one content link."]; + if (links.length > max) problems.push(`Use at most ${max} links.`); + if (links.some((link) => !/^https:\/\//i.test(link))) problems.push("Use complete https links, one per line."); + if (f.kind === "gdrive" && !f.include.length) problems.push("Pick at least one file kind."); + return problems; + } switch (f.kind) { case "gdrive": { const problems = idProblems(folderIds(f.folders), FOLDER_ID_RE, "folder", "a folder id"); @@ -453,7 +472,9 @@ export function sourceFormProblems(f: SourceForm): string[] { if (!NAME_RE.test(normalisedName(f.name))) problems.push("Name: 2-32 lower-case letters, digits, - or _, starting with a letter."); problems.push(...configProblems(f)); - if (!f.credential.trim()) problems.push(KIND_FORM[f.kind].noCredential); + if (f.credentialMode === "shared" && !f.credential.trim()) problems.push(KIND_FORM[f.kind].noCredential); + if (f.credentialMode === "connected" && f.kind === "recording") + problems.push("Use a shared credential for Zoom transcripts."); return problems; } @@ -473,7 +494,12 @@ function sourceConfig(f: SourceForm): Record { /** Whether anything but the kind was typed, so the problems list can wait. */ export function sourceFormTouched(f: SourceForm): boolean { - return [f.name, f.folders, f.projects, f.pages, f.databases, f.meetings, f.credential].some((v) => v !== ""); + return [f.name, f.links, f.folders, f.projects, f.pages, f.databases, f.meetings, f.credential].some((v) => v !== ""); +} + +/** Preserve each selected address; the server resolves and checks its provider and type. */ +export function sourceLinks(text: string): string[] { + return [...new Set(text.split(/\s+/).filter(Boolean))]; } export function sourceAddBody(org: string, namespace: string, f: SourceForm, dryRun: boolean) { @@ -482,8 +508,12 @@ export function sourceAddBody(org: string, namespace: string, f: SourceForm, dry namespace, name: normalisedName(f.name), kind: f.kind, - config: sourceConfig(f), - credential: f.credential.trim(), + ...(f.inputMode === "links" && f.kind !== "recording" + ? { links: sourceLinks(f.links), ...(f.kind === "gdrive" ? { include: [...f.include] } : {}) } + : { config: sourceConfig(f) }), + ...(f.credentialMode === "connected" + ? { credential_mode: "connected" as const } + : { credential: f.credential.trim() }), dry_run: dryRun, }; } diff --git a/frontend/src/views/ConnectedAccounts.test.tsx b/frontend/src/views/ConnectedAccounts.test.tsx index 319bff6fdd..2ceed72d7f 100644 --- a/frontend/src/views/ConnectedAccounts.test.tsx +++ b/frontend/src/views/ConnectedAccounts.test.tsx @@ -92,6 +92,58 @@ describe("Connected accounts", () => { expect(await axeViolations(container)).toEqual([]); }); + it("keeps an unconfigured provider's reason keyboard reachable without starting consent", async () => { + vi.mocked(api.listConnections).mockResolvedValue({ + connections: entries().map((entry) => (entry.kind === "notion" ? { ...entry, connect_available: false } : entry)), + }); + const { container } = render(); + const connect = await screen.findByRole("button", { name: "Connect Notion" }); + expect(connect).toHaveAttribute("aria-disabled", "true"); + expect(connect).not.toBeDisabled(); + expect(connect).toHaveAccessibleDescription( + "An operator must configure Notion connections before you can connect your account.", + ); + connect.focus(); + expect(connect).toHaveFocus(); + await userEvent.keyboard("{Enter}"); + await userEvent.click(connect); + expect(api.startConnection).not.toHaveBeenCalled(); + expect(goToConnectionConsent).not.toHaveBeenCalled(); + expect(container.querySelector("input")).toBeNull(); + expect(await axeViolations(container)).toEqual([]); + }); + + it("allows disconnect while reconnect needs operator configuration", async () => { + vi.mocked(api.listConnections).mockResolvedValue({ + connections: entries().map((entry) => (entry.kind === "gdrive" ? { ...entry, connect_available: false } : entry)), + }); + render(); + const reconnect = await screen.findByRole("button", { name: "Reconnect Google Drive" }); + expect(reconnect).toHaveAttribute("aria-disabled", "true"); + await userEvent.click(screen.getByRole("button", { name: "Disconnect Google Drive" })); + expect(api.disconnectConnection).toHaveBeenCalledWith("acme", "gdrive", { signal: expect.any(AbortSignal) }); + expect(api.startConnection).not.toHaveBeenCalled(); + }); + + it("explains returning to the source tab without starting consent or storing a draft", async () => { + history.replaceState(null, "", "/settings/connected-accounts?kind=notion&from=source"); + const localBefore = JSON.stringify(localStorage); + const sessionBefore = JSON.stringify(sessionStorage); + render(); + expect(screen.getByText("After connecting, return to the tab where you are adding a source.")).toBeInTheDocument(); + await screen.findByRole("button", { name: "Connect Notion" }); + expect(api.startConnection).not.toHaveBeenCalled(); + expect(goToConnectionConsent).not.toHaveBeenCalled(); + expect(JSON.stringify(localStorage)).toBe(localBefore); + expect(JSON.stringify(sessionStorage)).toBe(sessionBefore); + }); + + it("omits source-tab instructions for an ordinary settings visit", async () => { + render(); + await screen.findByRole("button", { name: "Connect Notion" }); + expect(screen.queryByText("After connecting, return to the tab where you are adding a source.")).toBeNull(); + }); + it("reads a personal viewer's connections without a namespace and refuses other known identities", () => { const whoami = useStore.getState().whoami; expect(canCallOwnConnections(whoami)).toBe(true); diff --git a/frontend/src/views/ConnectedAccounts.tsx b/frontend/src/views/ConnectedAccounts.tsx index 0466618784..55f713bcea 100644 --- a/frontend/src/views/ConnectedAccounts.tsx +++ b/frontend/src/views/ConnectedAccounts.tsx @@ -15,6 +15,7 @@ import { Loading, PageHeader } from "../components/ui"; import { BusyButton } from "../components/ui/BusyButton"; import { ListState } from "../components/ui/ListState"; import { SectionHeading } from "../components/ui/SectionHeading"; +import { Unavailable } from "../components/ui/Unavailable"; import { PageContainer } from "../layout/PageContainer"; import { canCallOwnConnections } from "../lib/canCall"; import { useAbortableEffect, useMounted } from "../lib/hooks"; @@ -44,6 +45,11 @@ export function ConnectedAccounts() {

    Connect your own account to use it with document sources. These connections belong to you in this organization.

    + {urlParam("from") === "source" && ( +

    + After connecting, return to the tab where you are adding a source. +

    + )} {available && whoami?.subject ? ( ) : ( @@ -110,7 +116,7 @@ function OwnConnections({ org, subject }: { org: string; subject: string }) { ); const act = async (entry: ConnectionEntry, disconnect: boolean) => { - if (busyRef.current || reading || !current()) return; + if (busyRef.current || reading || !current() || (!disconnect && entry.connect_available === false)) return; busyRef.current = true; setBusy({ kind: entry.kind, disconnect }); try { @@ -168,6 +174,10 @@ function OwnConnections({ org, subject }: { org: string; subject: string }) {
      {CONNECTION_KINDS.map((kind) => { const entry = connections.find((row) => row.kind === kind); + const unavailable = + entry?.connect_available === false + ? `An operator must configure ${LABELS[kind]} connections before you can connect your account.` + : null; return (
    • @@ -180,19 +190,22 @@ function OwnConnections({ org, subject }: { org: string; subject: string }) { state={entry?.connected ? "success" : "neutral"} size="sm" /> + {unavailable &&

      {unavailable}

      }
      {entry && (
      - + + + {entry.connected && (
    )} - {manageable && state.kind === "ready" && state.accepts && ( + {canAdd && state.kind === "ready" && state.accepts && ( @@ -255,6 +273,7 @@ export function SourcesList({ org={org} namespace={namespace} label={place.label} + allowShared={manageable} onOpenChange={(v) => { if (!v) setAdding(false); }} @@ -265,6 +284,19 @@ export function SourcesList({ /> )} + {connectionKind && ( +
    + + Manage your connection + {" "} + Return here after connecting, then check the source again. +
    + )} + {state.kind === "ready" && ( + + )} {acting && ( {acting.verb === "push" && ( @@ -277,7 +309,14 @@ export function SourcesList({ /> )} {acting.verb === "check" && ( - + )} {acting.verb === "sync" && ( bool: + try: + accounts.client(kind) + except ValidationError as exc: + if exc.extra.get("code") != "CONNECTION_UNAVAILABLE": + raise + return False + return True + + async def handle_connection_list(input: ConnectionListInput) -> ConnectionListOutput: org, subject = await accounts.own_subject(current_auth()) if org != input.org: @@ -30,6 +41,7 @@ async def handle_connection_list(input: ConnectionListInput) -> ConnectionListOu ConnectionEntry( kind=cast(Literal["gdrive", "notion", "linear"], kind), connected=connected, + connect_available=_connect_available(kind), account_email=(unseal(EMAIL_PURPOSE, account.account_email_enc, warn=False) or None) if connected and account is not None else None, diff --git a/tests/unit/test_connected_account_availability.py b/tests/unit/test_connected_account_availability.py new file mode 100644 index 0000000000..6748c9b669 --- /dev/null +++ b/tests/unit/test_connected_account_availability.py @@ -0,0 +1,99 @@ +"""Consent availability reflects deployment configuration without provider requests.""" + +from typing import Any + +import pytest + +from server.core.errors import AuthInsufficientError, ValidationError +from server.knowledge.api.models_connections import ConnectionListInput +from server.knowledge.app import connected_accounts as accounts +from server.knowledge.app import connections + + +@pytest.fixture +def configured(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(accounts.settings, "public_base_url", "https://app.example") + monkeypatch.setattr(accounts.settings, "connect_notion_read_content_only", True) + for kind in ("gdrive", "notion", "linear"): + monkeypatch.setattr(accounts.settings, f"connect_{kind}_client_id", "private-client-id") + monkeypatch.setattr(accounts.settings, f"connect_{kind}_client_secret", "private-secret") + + +@pytest.mark.parametrize("kind", ["gdrive", "notion", "linear"]) +@pytest.mark.parametrize("missing", ["", "client_id", "client_secret"]) +def test_consent_availability_checks_each_provider_registration( + configured: None, monkeypatch: pytest.MonkeyPatch, kind: str, missing: str +) -> None: + if missing: + monkeypatch.setattr(accounts.settings, f"connect_{kind}_{missing}", "") + assert connections._connect_available(kind) is (not missing) + + +@pytest.mark.parametrize("kind", ["gdrive", "notion", "linear"]) +def test_consent_availability_requires_a_valid_public_callback_base( + configured: None, monkeypatch: pytest.MonkeyPatch, kind: str +) -> None: + monkeypatch.setattr(accounts.settings, "public_base_url", "http://app.example") + assert not connections._connect_available(kind) + + +def test_notion_consent_requires_operator_read_content_attestation( + configured: None, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(accounts.settings, "connect_notion_read_content_only", False) + assert not connections._connect_available("notion") + assert connections._connect_available("gdrive") + assert connections._connect_available("linear") + + +async def test_list_reports_configuration_separately_from_own_connection_status( + configured: None, monkeypatch: pytest.MonkeyPatch +) -> None: + async def own_subject(_auth: Any) -> tuple[str, str]: + return "team", "person" + + async def owned(subjects: list[str], *, org: str) -> list[Any]: + assert subjects == ["person"] and org == "team" + return [] + + monkeypatch.setattr(accounts, "own_subject", own_subject) + monkeypatch.setattr(connections.store, "owned", owned) + monkeypatch.setattr(accounts.settings, "connect_linear_client_secret", "") + result = await connections.handle_connection_list(ConnectionListInput(org="team")) + assert [ + (entry.kind, entry.connected, entry.connect_available) for entry in result.connections + ] == [ + ("gdrive", False, True), + ("linear", False, False), + ("notion", False, True), + ] + wire = result.model_dump_json() + assert "private-client-id" not in wire and "private-secret" not in wire + assert all(entry.account_email is None and entry.scopes == [] for entry in result.connections) + + +@pytest.mark.parametrize("denied", ["identity", "organization"]) +async def test_list_authorizes_before_reading_provider_configuration( + monkeypatch: pytest.MonkeyPatch, denied: str +) -> None: + async def own_subject(_auth: Any) -> tuple[str, str]: + if denied == "identity": + raise AuthInsufficientError("signed-in session required") + return "other", "person" + + def unexpected(_kind: str) -> bool: + raise AssertionError("configuration read before authorization") + + monkeypatch.setattr(accounts, "own_subject", own_subject) + monkeypatch.setattr(connections, "_connect_available", unexpected) + with pytest.raises((AuthInsufficientError, ValidationError)): + await connections.handle_connection_list(ConnectionListInput(org="team")) + + +def test_unexpected_configuration_validation_is_not_hidden(monkeypatch: pytest.MonkeyPatch) -> None: + def client(_kind: str) -> Any: + raise ValidationError("unexpected validation") + + monkeypatch.setattr(accounts, "client", client) + with pytest.raises(ValidationError, match="unexpected validation"): + connections._connect_available("gdrive") From e6226d7383be3accc6a850f6e72da0552b01b5bb Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 17:41:55 +0000 Subject: [PATCH 2/6] fix: use verified ownership for connected source controls Signed-off-by: zlareb1 --- docs/reference/operations/knowledge.md | 2 +- frontend/e2e/connected-source.spec.ts | 2 + frontend/src/lib/knowledgeSources.ts | 1 + frontend/src/views/SourcesList.test.tsx | 28 +++- frontend/src/views/SourcesList.tsx | 4 +- sdk/python/loom/ops.py | 5 +- sdk/typescript/src/generated/ops.ts | 6 +- server/knowledge/api/models_sources.py | 12 +- server/knowledge/app/sources.py | 5 +- .../test_connected_account_availability.py | 158 +++++++++++++++++- 10 files changed, 211 insertions(+), 12 deletions(-) diff --git a/docs/reference/operations/knowledge.md b/docs/reference/operations/knowledge.md index ce11319251..b285e8a829 100644 --- a/docs/reference/operations/knowledge.md +++ b/docs/reference/operations/knowledge.md @@ -703,7 +703,7 @@ The document sources registered on a namespace, with each one's sync state. | Output field | Type | Meaning | | --- | --- | --- | -| `sources` | `list[KnowledgeSourceEntry]` | The namespace's active sources, by name. | +| `sources` | `list[KnowledgeSourceListEntry]` | The namespace's active sources, by name. | | `enabled` | `bool` | Whether this deployment runs the sync worker (LOOM_KNOWLEDGE_SOURCE_SYNC_ENABLED); false: sources are stored and probed, but nothing pulls from them. | | `accepts_sources` | `bool` | False where add would refuse the namespace (PERSONAL_NAMESPACE, NAMESPACE_INACTIVE). | diff --git a/frontend/e2e/connected-source.spec.ts b/frontend/e2e/connected-source.spec.ts index 161d3afc1f..c16ac12b11 100644 --- a/frontend/e2e/connected-source.spec.ts +++ b/frontend/e2e/connected-source.spec.ts @@ -118,6 +118,7 @@ function sourceRow(complete: boolean) { kind: "gdrive", credential_mode: "connected", owner_subject: SUBJECT, + owned_by_caller: true, config: {}, items: [], created_by: SUBJECT, @@ -172,6 +173,7 @@ async function checkConnectionTab( return route.abort("blockedbyclient"); } if (!url.pathname.startsWith("/v1/") && url.pathname !== "/v1") return route.continue(); + if (url.pathname === "/v1/health") return route.fulfill({ json: {} }); if (url.pathname === "/v1/whoami") { return route.fulfill({ json: { diff --git a/frontend/src/lib/knowledgeSources.ts b/frontend/src/lib/knowledgeSources.ts index 27a32aca08..69fd231ad6 100644 --- a/frontend/src/lib/knowledgeSources.ts +++ b/frontend/src/lib/knowledgeSources.ts @@ -30,6 +30,7 @@ export interface KnowledgeSource { kind: string; credential_mode?: "shared" | "connected"; owner_subject?: string; + owned_by_caller?: boolean; /** The ids and filters the source reads, in the form that names ids per kind; {} when * an item has no place in it. Never a credential. */ config: Record; diff --git a/frontend/src/views/SourcesList.test.tsx b/frontend/src/views/SourcesList.test.tsx index 2200b9b9c3..8e689932cb 100644 --- a/frontend/src/views/SourcesList.test.tsx +++ b/frontend/src/views/SourcesList.test.tsx @@ -51,7 +51,13 @@ const failure = () => const place = { label: "acme/docs", noun: "repository" } as const; function connectedSource(name: string, owner?: string): KnowledgeSource { - return { ...LISTED.sources[0]!, name, credential_mode: "connected", owner_subject: owner }; + return { + ...LISTED.sources[0]!, + name, + credential_mode: "connected", + owner_subject: owner, + owned_by_caller: owner === "alice", + }; } function writer() { @@ -115,6 +121,26 @@ describe("SourcesList", () => { expect(screen.getByRole("button", { name: "Refresh sources" })).toBeEnabled(); }); + it("uses server-proven ownership when the signed-in subject is an alias", async () => { + useStore.setState({ whoami: { authenticated: true, subject: "alice-alias", org: "acme", roles: ["writer"] } }); + sources.listKnowledgeSources.mockResolvedValue({ enabled: true, sources: [connectedSource("own-alias", "alice")] }); + render(); + await userEvent.click(await screen.findByRole("button", { name: "Actions for own-alias" })); + expect(await screen.findByRole("menuitem", { name: "Manage connection" })).toBeInTheDocument(); + expect(screen.getByRole("menuitem", { name: "Sync now" })).toBeInTheDocument(); + }); + + it("does not offer an admin mutation controls over another person's connected source", async () => { + useStore.setState({ whoami: { authenticated: true, subject: "alice", org: "acme", roles: ["admin"] } }); + sources.listKnowledgeSources.mockResolvedValue({ + enabled: true, + sources: [connectedSource("another-owner", "bob")], + }); + render(); + await screen.findByRole("button", { name: "another-owner" }); + expect(screen.queryByRole("button", { name: "Actions for another-owner" })).not.toBeInTheDocument(); + }); + it("gives a writer no mutation controls for another owner's connection or an admin's shared source", async () => { writer(); sources.listKnowledgeSources.mockResolvedValue({ diff --git a/frontend/src/views/SourcesList.tsx b/frontend/src/views/SourcesList.tsx index 0ad5a1d98f..8d084149f8 100644 --- a/frontend/src/views/SourcesList.tsx +++ b/frontend/src/views/SourcesList.tsx @@ -183,8 +183,8 @@ export function SourcesList({ ); const menu = (s: KnowledgeSource): RowMenuItem[] => { - const owned = !!subject && s.credential_mode === "connected" && s.owner_subject === subject; - if (!manageable && !(connectedManageable && owned)) return []; + const owned = !!subject && s.credential_mode === "connected" && s.owned_by_caller === true; + if (s.credential_mode === "connected" ? !(connectedManageable && owned) : !manageable) return []; const act = (verb: Verb) => () => setActing({ verb, source: s }); const check: RowMenuItem = { label: "Check", icon: "check", onSelect: act("check") }; const sync: RowMenuItem = { label: "Sync now", icon: "refresh", onSelect: act("sync") }; diff --git a/sdk/python/loom/ops.py b/sdk/python/loom/ops.py index aa5449db5a..b8e7b7c863 100644 --- a/sdk/python/loom/ops.py +++ b/sdk/python/loom/ops.py @@ -11861,12 +11861,12 @@ class KnowledgeSourceListBody(TypedDict, total=False): class KnowledgeSourceListOutput(TypedDict, total=False): - sources: NotRequired[list["KnowledgeSourceEntry"]] + sources: NotRequired[list["KnowledgeSourceListEntry"]] enabled: NotRequired[bool] accepts_sources: NotRequired[bool] -class KnowledgeSourceEntry(TypedDict, total=False): +class KnowledgeSourceListEntry(TypedDict, total=False): name: Required[str] kind: Required[str] credential_mode: NotRequired[str] @@ -11878,6 +11878,7 @@ class KnowledgeSourceEntry(TypedDict, total=False): created_by: NotRequired[str] created_at: NotRequired[str] sync: NotRequired["KnowledgeSourceSync"] + owned_by_caller: NotRequired[bool] class KnowledgeSourceParseLinksInput(TypedDict, total=False): diff --git a/sdk/typescript/src/generated/ops.ts b/sdk/typescript/src/generated/ops.ts index 249eb17633..ed695a9158 100644 --- a/sdk/typescript/src/generated/ops.ts +++ b/sdk/typescript/src/generated/ops.ts @@ -4402,7 +4402,7 @@ export interface KnowledgeSourceListInput { namespace: string; } -export interface KnowledgeSourceEntry { +export interface KnowledgeSourceListEntry { name: string; kind: string; credential_mode?: string; @@ -4414,10 +4414,12 @@ export interface KnowledgeSourceEntry { created_by?: string; created_at?: string; sync?: KnowledgeSourceSync; + /** Whether the caller may read their own connected source's details; false when ownership or access cannot be verified. */ + owned_by_caller?: boolean; } export interface KnowledgeSourceListOutput { - sources?: KnowledgeSourceEntry[]; + sources?: KnowledgeSourceListEntry[]; enabled?: boolean; accepts_sources?: boolean; } diff --git a/server/knowledge/api/models_sources.py b/server/knowledge/api/models_sources.py index 2a8c6c7e06..f8fafe5f34 100644 --- a/server/knowledge/api/models_sources.py +++ b/server/knowledge/api/models_sources.py @@ -137,9 +137,19 @@ class KnowledgeSourceListInput(BaseModel): namespace: str = Field(..., description="The namespace the sources live in.") +class KnowledgeSourceListEntry(KnowledgeSourceEntry): + owned_by_caller: bool = Field( + default=False, + description=( + "Whether the caller may read their own connected source's details; " + "false when ownership or access cannot be verified." + ), + ) + + class KnowledgeSourceListOutput(BaseModel): # The namespace's active sources, by name. - sources: list[KnowledgeSourceEntry] = Field(default_factory=list) + sources: list[KnowledgeSourceListEntry] = Field(default_factory=list) # Whether this deployment runs the sync worker (LOOM_KNOWLEDGE_SOURCE_SYNC_ENABLED); # false: sources are stored and probed, but nothing pulls from them. enabled: bool = False diff --git a/server/knowledge/app/sources.py b/server/knowledge/app/sources.py index fcbc9f0d73..a7eed6c75b 100644 --- a/server/knowledge/app/sources.py +++ b/server/knowledge/app/sources.py @@ -59,9 +59,9 @@ KnowledgeSourceCheckInput, KnowledgeSourceCheckOutput, KnowledgeSourceDocument, - KnowledgeSourceEntry, KnowledgeSourceInspectInput, KnowledgeSourceInspectOutput, + KnowledgeSourceListEntry, KnowledgeSourceListInput, KnowledgeSourceListOutput, KnowledgeSourceParsedLink, @@ -433,12 +433,13 @@ async def handle_knowledge_source_list( for s in found: details = await _details_allowed(s) entries.append( - KnowledgeSourceEntry( + KnowledgeSourceListEntry( name=s.name, kind=s.kind, credential_mode=s.credential_mode, access=s.access, owner_subject=s.owner_subject if details else "", + owned_by_caller=s.credential_mode == "connected" and details, items=s.items if details else [], include=_answered_include(s) if details else [], config=(domain.id_list_config(s) or {}) if details else {}, diff --git a/tests/unit/test_connected_account_availability.py b/tests/unit/test_connected_account_availability.py index 6748c9b669..88527eda03 100644 --- a/tests/unit/test_connected_account_availability.py +++ b/tests/unit/test_connected_account_availability.py @@ -1,11 +1,14 @@ -"""Consent availability reflects deployment configuration without provider requests.""" +"""Own-account availability and source-list ownership without provider requests.""" +from contextlib import nullcontext +from types import SimpleNamespace from typing import Any import pytest from server.core.errors import AuthInsufficientError, ValidationError from server.knowledge.api.models_connections import ConnectionListInput +from server.knowledge.api.models_sources import KnowledgeSourceListInput, KnowledgeSourceSync from server.knowledge.app import connected_accounts as accounts from server.knowledge.app import connections @@ -97,3 +100,156 @@ def client(_kind: str) -> Any: monkeypatch.setattr(accounts, "client", client) with pytest.raises(ValidationError, match="unexpected validation"): connections._connect_available("gdrive") + + +@pytest.fixture +def normal_source_list(monkeypatch: pytest.MonkeyPatch) -> tuple[Any, SimpleNamespace]: + from server.knowledge.app import sources + + source = SimpleNamespace( + org="team", + namespace="docs", + name="selected", + kind="gdrive", + credential_mode="connected", + access="mirrored", + owner_subject="canonical-person", + items=[{"type": "file", "id": "selected-file"}], + created_by="private-creator", + created_at=None, + ) + + async def listed(org: str, namespace: str) -> list[Any]: + assert (org, namespace) == ("team", "docs") + return [source] + + async def states(org: str, namespace: str, names: list[str]) -> dict[str, Any]: + assert (org, namespace, names) == ("team", "docs", ["selected"]) + return {} + + async def refusal(org: str, namespace: str) -> None: + assert (org, namespace) == ("team", "docs") + + monkeypatch.setattr(sources.store, "list_sources", listed) + monkeypatch.setattr(sources.store, "read_states", states) + monkeypatch.setattr(sources, "_source_refusal", refusal) + monkeypatch.setattr(sources.ch, "set_query_tag", lambda _tag: None) + monkeypatch.setattr(sources, "_answered_include", lambda _source: ["pdf"]) + monkeypatch.setattr(sources.domain, "id_list_config", lambda _source: {"selected": "file"}) + monkeypatch.setattr(sources, "_iso", lambda _value: "") + monkeypatch.setattr(sources, "_sync", lambda _source, _state: KnowledgeSourceSync(status="ok")) + # These tests exercise the predicate's decisions, not private-work authority admission. + monkeypatch.setattr(sources, "private_work", nullcontext) + monkeypatch.setattr(sources.request_work, "mark_mirrored", lambda: True) + return sources, source + + +@pytest.mark.parametrize("subject", ["canonical-person", "person-alias"]) +async def test_normal_source_list_uses_verified_canonical_ownership_for_aliases( + normal_source_list: tuple[Any, SimpleNamespace], monkeypatch: pytest.MonkeyPatch, subject: str +) -> None: + from server.knowledge.store import connected_accounts as account_store + + sources, source = normal_source_list + caller = SimpleNamespace(org="team", subject=subject) + account = object() + checked = [] + + async def canonical(auth: Any) -> tuple[str, str]: + assert auth is caller + return "team", "canonical-person" + + async def get(org: str, owner: str, kind: str) -> Any: + assert (org, owner, kind) == ("team", "canonical-person", "gdrive") + return account + + async def standing(found: Any, connected: Any) -> None: + assert found is source and connected is account + checked.append(True) + + monkeypatch.setattr(sources, "current_auth", lambda: caller) + monkeypatch.setattr(accounts, "own_subject", canonical) + monkeypatch.setattr(account_store, "get", get) + monkeypatch.setattr(accounts, "_source_standing", standing) + result = await sources.handle_knowledge_source_list( + KnowledgeSourceListInput(org="team", namespace="docs") + ) + entry = result.model_dump()["sources"][0] + assert entry["owned_by_caller"] is True + assert entry["owner_subject"] == "canonical-person" + assert entry["items"] == [{"type": "file", "id": "selected-file"}] + assert entry["sync"]["status"] == "ok" + assert checked == [True] + + +@pytest.mark.parametrize("refused", ["foreign", "other-org", "missing", "standing", "unmarked"]) +async def test_normal_source_list_never_marks_redacted_connected_details_as_owned( + normal_source_list: tuple[Any, SimpleNamespace], monkeypatch: pytest.MonkeyPatch, refused: str +) -> None: + from server.knowledge.store import connected_accounts as account_store + + sources, _source = normal_source_list + canonical_reads = [] + account_reads = [] + standing_reads = [] + + async def canonical(_auth: Any) -> tuple[str, str]: + canonical_reads.append(True) + if refused == "unmarked": + raise AssertionError("unmarked work must not read account authority") + return ( + "other-team" if refused == "other-org" else "team", + "other-person" if refused == "foreign" else "canonical-person", + ) + + async def get(org: str, owner: str, kind: str) -> Any: + account_reads.append((org, owner, kind)) + assert refused in {"missing", "standing"} + assert (org, owner, kind) == ("team", "canonical-person", "gdrive") + return None if refused == "missing" else object() + + async def standing(_source: Any, _account: Any) -> None: + standing_reads.append(True) + assert refused == "standing" + raise ValidationError("standing cannot be verified") + + monkeypatch.setattr(accounts, "own_subject", canonical) + monkeypatch.setattr(account_store, "get", get) + monkeypatch.setattr(accounts, "_source_standing", standing) + monkeypatch.setattr(sources.request_work, "mark_mirrored", lambda: refused != "unmarked") + result = await sources.handle_knowledge_source_list( + KnowledgeSourceListInput(org="team", namespace="docs") + ) + entry = result.model_dump()["sources"][0] + assert entry["owned_by_caller"] is False + assert entry["owner_subject"] == entry["created_by"] == "" + assert entry["items"] == entry["include"] == [] + assert entry["config"] == {} + assert entry["sync"]["status"] == "" + assert canonical_reads == ([] if refused == "unmarked" else [True]) + assert account_reads == ( + [("team", "canonical-person", "gdrive")] if refused in {"missing", "standing"} else [] + ) + assert standing_reads == ([True] if refused == "standing" else []) + + +async def test_normal_source_list_shared_details_do_not_mean_a_personal_connection_is_owned( + normal_source_list: tuple[Any, SimpleNamespace], monkeypatch: pytest.MonkeyPatch +) -> None: + sources, source = normal_source_list + source.credential_mode = "shared" + source.access = "namespace" + source.owner_subject = "" + + async def no_personal_account(_auth: Any) -> tuple[str, str]: + raise AssertionError("shared details do not resolve personal account ownership") + + monkeypatch.setattr(accounts, "own_subject", no_personal_account) + result = await sources.handle_knowledge_source_list( + KnowledgeSourceListInput(org="team", namespace="docs") + ) + entry = result.model_dump()["sources"][0] + assert entry["owned_by_caller"] is False + assert entry["config"] == {"selected": "file"} + assert entry["created_by"] == "private-creator" + assert entry["sync"]["status"] == "ok" From 7da5bfa54fd6df365270dbd7d8a7f615fa0a68a5 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 18:03:39 +0000 Subject: [PATCH 3/6] feat: preview source links and edit selected items Signed-off-by: zlareb1 --- frontend/e2e/connected-source.spec.ts | 13 + frontend/src/api/knowledgeSources.ts | 47 +++- .../src/flows/EditSourceItemsDialog.test.tsx | 243 ++++++++++++++++ frontend/src/flows/EditSourceItemsDialog.tsx | 266 ++++++++++++++++++ .../KnowledgeSourceDialog.connected.test.tsx | 123 +++++++- frontend/src/flows/KnowledgeSourceDialog.tsx | 42 ++- .../src/flows/SourceDetailsDialog.test.tsx | 10 + frontend/src/flows/SourceDetailsDialog.tsx | 46 ++- frontend/src/flows/SourceLinkPreview.tsx | 81 ++++++ frontend/src/lib/knowledgeSources.ts | 29 ++ frontend/src/views/SourcesList.test.tsx | 31 ++ frontend/src/views/SourcesList.tsx | 64 ++++- server/operations/dashboard.py | 8 - server/operations/reach_manifest.py | 2 + 14 files changed, 947 insertions(+), 58 deletions(-) create mode 100644 frontend/src/flows/EditSourceItemsDialog.test.tsx create mode 100644 frontend/src/flows/EditSourceItemsDialog.tsx create mode 100644 frontend/src/flows/SourceLinkPreview.tsx diff --git a/frontend/e2e/connected-source.spec.ts b/frontend/e2e/connected-source.spec.ts index c16ac12b11..74fdcbdf5c 100644 --- a/frontend/e2e/connected-source.spec.ts +++ b/frontend/e2e/connected-source.spec.ts @@ -87,6 +87,19 @@ async function installConnectedSource(page: Page) { json: { sources: state.added ? [sourceRow(state.complete)] : [], enabled: true, accepts_sources: true }, }); } + if (path.endsWith(".parse_links")) { + expect(raw.links).toEqual(LINKS); + return route.fulfill({ + json: { + links: LINKS.map((link, index) => ({ + link, + kind: "gdrive", + item: { type: "file", id: index === 0 ? "selected-file" : "selected-doc" }, + unreadable: "", + })), + }, + }); + } if (path.endsWith(".add")) { if (raw.dry_run && !state.refusedCheck) { state.refusedCheck = true; diff --git a/frontend/src/api/knowledgeSources.ts b/frontend/src/api/knowledgeSources.ts index 9000ac7014..f1b18aaf5d 100644 --- a/frontend/src/api/knowledgeSources.ts +++ b/frontend/src/api/knowledgeSources.ts @@ -8,6 +8,7 @@ import type { KnowledgeSourceCheckResult, KnowledgeSourceDetail, KnowledgeSourceList, + KnowledgeSourceProbe, KnowledgeSourceRemoveResult, KnowledgeSourceRotateResult, KnowledgeSourceSyncNowResult, @@ -15,11 +16,53 @@ import type { } from "../lib/knowledgeSources"; import { sourceAddBody } from "../lib/knowledgeSources"; -// A namespace's document sources, addressed by that namespace. Reading them needs the -// namespace's viewer role; every other call its admin. +// Namespace-scoped requests; the server authorizes each source-management action. type Ns = Pick; +export interface KnowledgeSourceParsedLink { + link: string; + kind: string; + item: Record; + unreadable: string; +} + +export interface KnowledgeSourceUpdateChanges { + add_items?: Record[]; + add_links?: string[]; + remove_items?: Record[]; + remove_links?: string[]; + include?: string[]; +} + +export interface KnowledgeSourceUpdateResult { + name: string; + items: Record[]; + include: string[]; + config: Record; + added: Record[]; + removed: Record[]; + probe: KnowledgeSourceProbe; +} + +/** Resolve link types without accessing the provider or importing content. */ +export function parseKnowledgeSourceLinks( + id: Ns, + links: string[], + opts: RequestOpts = {}, +): Promise<{ links: KnowledgeSourceParsedLink[] }> { + return post("/v1/knowledge.source.parse_links", { org: id.org, namespace: id.namespace, links }, opts.signal); +} + +export function updateKnowledgeSource( + id: Ns, + name: string, + changes: KnowledgeSourceUpdateChanges, + opts: RequestOpts = {}, +): Promise { + return post("/v1/knowledge.source.update", { ...changes, org: id.org, namespace: id.namespace, name }, opts.signal); +} + export async function listKnowledgeSources(id: Ns, opts: RequestOpts = {}): Promise { return post("/v1/knowledge.source.list", { org: id.org, namespace: id.namespace }, opts.signal); } diff --git a/frontend/src/flows/EditSourceItemsDialog.test.tsx b/frontend/src/flows/EditSourceItemsDialog.test.tsx new file mode 100644 index 0000000000..05899045b8 --- /dev/null +++ b/frontend/src/flows/EditSourceItemsDialog.test.tsx @@ -0,0 +1,243 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import userEvent from "@testing-library/user-event"; +import { act, cleanup, fireEvent, render, screen, waitFor, within } from "../test/rtl"; +import { axeViolations, unlabeledControls } from "../test/axe"; +import { listKnowledgeSources, parseKnowledgeSourceLinks, updateKnowledgeSource } from "../api/knowledgeSources"; +import type { KnowledgeSource } from "../lib/knowledgeSources"; +import { useStore } from "../lib/state"; +import { EditSourceItemsDialog } from "./EditSourceItemsDialog"; + +vi.mock("../api/knowledgeSources", async (load) => ({ + ...(await load()), + listKnowledgeSources: vi.fn(), + parseKnowledgeSourceLinks: vi.fn(), + updateKnowledgeSource: vi.fn(), +})); + +const NS = { org: "acme", namespace: "col-team" }; +const LINK = "https://drive.google.com/file/d/new-file/view"; +const ITEM = { type: "folder", id: "existing-folder", recursive: true }; +const SOURCE: KnowledgeSource = { + name: "notes", + kind: "gdrive", + credential_mode: "connected", + owned_by_caller: true, + owner_subject: "canonical-reader", + access: "mirrored", + items: [ITEM, { type: "file", id: "keep-file" }], + include: ["docs", "pdf"], + config: {}, + created_by: "", + created_at: "", + sync: { + requested_at: "", + last_synced_at: "", + status: "", + documents_seen: 0, + changed: 0, + removed: 0, + unconverted: 0, + unconverted_reasons: {}, + skipped: 0, + error: "", + }, +}; +const PARSED = { links: [{ link: LINK, kind: "gdrive", item: { type: "file", id: "new-file" }, unreadable: "" }] }; +const UPDATED = { + name: "notes", + items: [{ type: "file", id: "new-file" }], + include: ["docs", "pdf"], + config: {}, + added: [{ type: "file", id: "new-file" }], + removed: [ITEM], + probe: { documents: 0, more: false, sample: [] }, +}; + +beforeEach(() => { + vi.resetAllMocks(); + useStore.setState({ + authed: true, + sessionExpired: false, + identity: { ...NS, agent: "loom-ui" }, + whoami: { authenticated: true, subject: "reader-alias", org: "acme", roles: ["writer"] }, + }); + vi.mocked(parseKnowledgeSourceLinks).mockResolvedValue(PARSED); + vi.mocked(updateKnowledgeSource).mockResolvedValue(UPDATED); + vi.mocked(listKnowledgeSources).mockResolvedValue({ enabled: true, sources: [SOURCE] }); +}); +afterEach(() => { + cleanup(); + useStore.setState({ whoami: null }); +}); + +function open(source = SOURCE, canManage = true) { + const onClose = vi.fn(), + onUpdated = vi.fn(); + render( + , + ); + return { onClose, onUpdated }; +} +function addLink(value = LINK) { + fireEvent.input(screen.getByLabelText(/^Content links to add/), { target: { value } }); +} +async function review() { + await userEvent.click(screen.getByRole("button", { name: "Review changes" })); +} + +describe("EditSourceItemsDialog", () => { + it("saves only the reviewed incremental selection with labels and no credential or access override", async () => { + const callbacks = open(); + await userEvent.click(screen.getByRole("checkbox", { name: "Folder existing-folder (with subfolders)" })); + addLink(); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + await review(); + expect(await screen.findByText(/1 links to add · 1 items to remove/)).toBeInTheDocument(); + expect(parseKnowledgeSourceLinks).toHaveBeenCalledWith(NS, [LINK], { signal: expect.any(AbortSignal) }); + expect(await axeViolations(document.body)).toEqual([]); + expect(unlabeledControls(document.body)).toEqual([]); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + expect(updateKnowledgeSource).toHaveBeenCalledExactlyOnceWith(NS, "notes", { + add_links: [LINK], + remove_items: [ITEM], + }); + expect(callbacks.onUpdated).toHaveBeenCalledOnce(); + expect(callbacks.onClose).toHaveBeenCalledOnce(); + }); + + it("reviews removal-only edits without calling a parser with an empty selection", async () => { + open(); + await userEvent.click(screen.getByRole("checkbox", { name: "File keep-file" })); + await review(); + expect(parseKnowledgeSourceLinks).not.toHaveBeenCalled(); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + expect(updateKnowledgeSource).toHaveBeenCalledExactlyOnceWith(NS, "notes", { + remove_items: [{ type: "file", id: "keep-file" }], + }); + }); + + it.each([ + { kind: "notion", item: { type: "page", id: "other-page" }, unreadable: "" }, + { kind: "", item: {}, unreadable: "Unsupported content" }, + ])("keeps failed link previews visible and refuses $kind content: $unreadable", async (row) => { + vi.mocked(parseKnowledgeSourceLinks).mockResolvedValue({ + links: [{ link: LINK, ...row }], + }); + open(); + addLink(); + await review(); + await screen.findByRole("alert"); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + expect(within(screen.getByRole("region", { name: "Content link preview" })).getByText(LINK)).toBeInTheDocument(); + expect(updateKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("rejects a reordered or incomplete preview instead of silently changing the saved selection", async () => { + vi.mocked(parseKnowledgeSourceLinks).mockResolvedValue({ links: [] }); + open(); + addLink(); + await review(); + expect(await screen.findByRole("alert")).toHaveTextContent(/did not match/); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + expect(updateKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("invalidates review when a link or removal selection changes", async () => { + open(); + addLink(); + await review(); + await waitFor(() => expect(screen.getByRole("button", { name: "Save changes" })).toBeEnabled()); + await userEvent.click(screen.getByRole("checkbox", { name: "File keep-file" })); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + await review(); + await waitFor(() => expect(screen.getByRole("button", { name: "Save changes" })).toBeEnabled()); + addLink("https://drive.google.com/file/d/different/view"); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + expect(updateKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("preserves input and selected removals after a save refusal without retrying it", async () => { + vi.mocked(updateKnowledgeSource).mockRejectedValue(new Error("Provider refused the selected file")); + const callbacks = open(); + addLink(); + await userEvent.click(screen.getByRole("checkbox", { name: "File keep-file" })); + await review(); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + expect(await screen.findByText(/Save could not be confirmed/)).toBeInTheDocument(); + expect(screen.getByLabelText(/^Content links to add/)).toHaveValue(LINK); + expect(screen.getByRole("checkbox", { name: "File keep-file" })).toBeChecked(); + expect(updateKnowledgeSource).toHaveBeenCalledTimes(1); + expect(callbacks.onUpdated).not.toHaveBeenCalled(); + expect(callbacks.onClose).not.toHaveBeenCalled(); + }); + + it("requires a fresh source read and a new review after a lost save response", async () => { + vi.mocked(updateKnowledgeSource).mockRejectedValueOnce(new TypeError("Failed to fetch")); + vi.mocked(listKnowledgeSources).mockResolvedValue({ + enabled: true, + sources: [ + { + ...SOURCE, + items: [ + { type: "file", id: "keep-file" }, + { type: "file", id: "new-file" }, + ], + }, + ], + }); + open(); + addLink(); + await userEvent.click(screen.getByRole("checkbox", { name: "Folder existing-folder (with subfolders)" })); + await review(); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + await screen.findByText(/Save could not be confirmed/); + expect(screen.queryByText(/These changes have not been saved/)).not.toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + expect(screen.getByRole("button", { name: "Review changes" })).toBeDisabled(); + expect(updateKnowledgeSource).toHaveBeenCalledTimes(1); + await userEvent.click(screen.getByRole("button", { name: "Reload current items" })); + await waitFor(() => expect(screen.getByRole("button", { name: "Review changes" })).toBeEnabled()); + expect(listKnowledgeSources).toHaveBeenCalledWith(NS, { signal: expect.any(AbortSignal) }); + expect( + screen.queryByRole("checkbox", { name: "Folder existing-folder (with subfolders)" }), + ).not.toBeInTheDocument(); + expect(screen.getByRole("checkbox", { name: "File keep-file" })).not.toBeChecked(); + expect(screen.getByLabelText(/^Content links to add/)).toHaveValue(LINK); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + await userEvent.click(screen.getByRole("checkbox", { name: "File keep-file" })); + await review(); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + expect(updateKnowledgeSource).toHaveBeenLastCalledWith(NS, "notes", { + add_links: [LINK], + remove_items: [{ type: "file", id: "keep-file" }], + }); + expect(updateKnowledgeSource).toHaveBeenCalledTimes(2); + }); + + it("closes stale ownership and drops a late review after the signed-in identity changes", async () => { + let finish!: (value: typeof PARSED) => void; + vi.mocked(parseKnowledgeSourceLinks).mockReturnValue( + new Promise((resolve) => { + finish = resolve; + }), + ); + open(); + addLink(); + await review(); + await act(async () => { + useStore.setState({ whoami: { authenticated: true, subject: "other-reader", org: "acme", roles: ["writer"] } }); + }); + await act(async () => { + finish(PARSED); + }); + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); + expect(updateKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("offers no edit when ownership or current manage permission is missing", () => { + open({ ...SOURCE, owned_by_caller: false }, true); + expect(screen.getByRole("alert")).toHaveTextContent(/no longer have access/); + expect(screen.getByRole("button", { name: "Review changes" })).toBeDisabled(); + expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); + }); +}); diff --git a/frontend/src/flows/EditSourceItemsDialog.tsx b/frontend/src/flows/EditSourceItemsDialog.tsx new file mode 100644 index 0000000000..f7ed0582e1 --- /dev/null +++ b/frontend/src/flows/EditSourceItemsDialog.tsx @@ -0,0 +1,266 @@ +import { useEffect, useRef, useState } from "react"; +import { Button } from "@clickhouse/click-ui/Button"; +import { Checkbox } from "@clickhouse/click-ui/Checkbox"; +import { TextAreaField } from "@clickhouse/click-ui/TextAreaField"; +import { + listKnowledgeSources, + parseKnowledgeSourceLinks, + updateKnowledgeSource, + type KnowledgeSourceParsedLink, +} from "../api/knowledgeSources"; +import { committedAnyway, humanizeError } from "../lib/errorWords"; +import { useMounted } from "../lib/hooks"; +import { sourceItemText, sourceLinks, type KnowledgeSource } from "../lib/knowledgeSources"; +import { useStore } from "../lib/state"; +import { CancelButton, DialogFrame, ErrorNote } from "./TaskDialogFrame"; +import { SourceLinkPreview, validateSourceLinkPreview } from "./SourceLinkPreview"; + +interface Props { + org: string; + namespace: string; + source: KnowledgeSource; + canManage: boolean; + onClose: () => void; + onUpdated: () => void; +} + +function actorScope() { + const s = useStore.getState(); + return JSON.stringify([ + s.identity?.org, + s.whoami?.org, + s.whoami?.subject, + s.whoami?.roles, + s.whoami?.is_service_account, + s.authed, + s.sessionExpired, + ]); +} + +export function EditSourceItemsDialog(props: Props) { + const scope = useStore(() => actorScope()); + const initial = useRef(scope); + // The source's ownership projection belongs to the identity that loaded it. + if (scope !== initial.current) return null; + return ( + + ); +} + +function EditSourceItems({ org, namespace, source, canManage, onClose, onUpdated }: Props) { + const mounted = useMounted(); + const whoami = useStore((s) => s.whoami); + const actor = actorScope(); + const current = () => mounted.current && actor === actorScope(); + const allowed = + canManage && + whoami?.org === org && + (source.credential_mode !== "connected" || (!!whoami?.subject && source.owned_by_caller === true)); + const running = useRef(false); + const previewRequest = useRef(null); + useEffect(() => () => previewRequest.current?.abort(), []); + const [linksText, setLinksText] = useState(""); + const [items, setItems] = useState(source.items ?? []); + const [removed, setRemoved] = useState([]); + const [review, setReview] = useState<{ stamp: string; links: KnowledgeSourceParsedLink[]; valid: boolean } | null>( + null, + ); + const [error, setError] = useState(null); + const [busy, setBusy] = useState<"preview" | "save" | "reload" | null>(null); + const [needsReload, setNeedsReload] = useState(false); + const links = sourceLinks(linksText); + const stamp = JSON.stringify([links, removed]); + const reviewed = !needsReload && review?.stamp === stamp && review.valid; + const selected = items.filter((_, index) => removed.includes(index)); + const changed = links.length > 0 || selected.length > 0; + const limit = source.kind === "gdrive" ? 300 : 20; + const problems = links.length > limit ? [`Choose at most ${limit} links.`] : []; + if ( + links.some((link) => { + try { + return new URL(link).protocol !== "https:"; + } catch { + return true; + } + }) + ) + problems.push("Use a complete HTTPS link for each item."); + + const run = async (save: boolean) => { + if (!current() || !allowed || needsReload || running.current || !changed || problems.length || (save && !reviewed)) + return; + running.current = true; + setBusy(save ? "save" : "preview"); + setError(null); + try { + if (!save) { + const controller = new AbortController(); + previewRequest.current = controller; + const parsed = links.length + ? (await parseKnowledgeSourceLinks({ org, namespace }, links, { signal: controller.signal })).links + : []; + if (!current()) return; + const { matches, problems: failures } = validateSourceLinkPreview(parsed, source.kind, links); + setReview(matches ? { stamp, links: parsed, valid: failures.length === 0 } : null); + if (failures.length) { + setError(failures.join(" ")); + } + } else { + await updateKnowledgeSource({ org, namespace }, source.name, { + ...(links.length ? { add_links: links } : {}), + ...(selected.length ? { remove_items: selected } : {}), + }); + if (!current()) return; + onUpdated(); + onClose(); + } + } catch (e) { + if (!current()) return; + if (save && committedAnyway("Update source", e)) { + onUpdated(); + onClose(); + } else { + setReview(null); + if (save) setNeedsReload(true); + setError(humanizeError(e)); + } + } finally { + running.current = false; + if (mounted.current) setBusy(null); + } + }; + + const reloadItems = async () => { + if (!current() || running.current) return; + running.current = true; + setBusy("reload"); + setError(null); + const controller = new AbortController(); + previewRequest.current = controller; + try { + const result = await listKnowledgeSources({ org, namespace }, { signal: controller.signal }); + if (!current()) return; + const fresh = result.sources.find((entry) => entry.name === source.name); + if ( + !fresh || + fresh.kind !== source.kind || + (fresh.credential_mode ?? "shared") !== (source.credential_mode ?? "shared") || + (fresh.credential_mode === "connected" && fresh.owned_by_caller !== true) + ) { + setError("This source is no longer available to edit. Close this dialog and refresh sources."); + return; + } + setItems(fresh.items ?? []); + setRemoved([]); + setReview(null); + setNeedsReload(false); + } catch (e) { + if (current()) setError(humanizeError(e, { read: true })); + } finally { + running.current = false; + if (mounted.current) setBusy(null); + } + }; + + return ( + { + if (!open) onClose(); + }} + title={`Edit items · ${source.name}`} + description="Add content links or remove selected items from this source." + footer={ + <> + + + + + } + > +

    + These changes apply to the source's current items without replacing its other selections, account or access + mode. Removing an item stops future imports from it. Documents held by another item or source remain. +

    + {!allowed && You no longer have access to edit this source. Close it and refresh sources.} + {needsReload && ( + <> + + Save could not be confirmed. Reload current items before trying again, then select removals and review any + links you still want to add. + + + + )} +
    +
    + Select items to remove + {items.length ? ( + items.map((item, index) => ( + + setRemoved((values) => + values.includes(index) ? values.filter((value) => value !== index) : [...values, index], + ) + } + /> + )) + ) : ( +

    Current items are not available. You can still add links.

    + )} +
    + +
    + {problems.length > 0 && {problems.join(" ")}} + {review?.stamp === stamp && ( + <> + + {reviewed && ( +

    + {links.length} links to add · {selected.length} items to remove. Link format reviewed. These changes have + not been saved. +

    + )} + + )} + {error && {error}} +
    + ); +} diff --git a/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx b/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx index 6f173c30c8..64b00be6e8 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.connected.test.tsx @@ -1,9 +1,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import userEvent from "@testing-library/user-event"; -import { act, cleanup, fireEvent, render, screen, waitFor } from "../test/rtl"; +import { act, cleanup, fireEvent, render, screen, waitFor, within } from "../test/rtl"; import { axeViolations, unlabeledControls } from "../test/axe"; import { listConnections } from "../api/connections"; -import { addKnowledgeSource } from "../api/knowledgeSources"; +import { addKnowledgeSource, parseKnowledgeSourceLinks } from "../api/knowledgeSources"; import { ApiError } from "../lib/errors"; import { sourceAddBody } from "../lib/knowledgeSources"; import { useStore } from "../lib/state"; @@ -16,6 +16,7 @@ vi.mock("../api/connections", async (load) => ({ vi.mock("../api/knowledgeSources", async (load) => ({ ...(await load()), addKnowledgeSource: vi.fn(), + parseKnowledgeSourceLinks: vi.fn(), })); const LINK = "https://drive.google.com/file/d/selected-file/view"; @@ -57,6 +58,17 @@ const result = (dry_run = true) => ({ probe: { documents: 0, more: false, sample: [], checked: 2, sampled: false }, dry_run, }); +const parsedLinks = (links: string[]) => ({ + links: links.map((link) => { + const kind = link.includes("notion.so") ? "notion" : link.includes("linear.app") ? "linear" : "gdrive"; + return { + link, + kind, + item: { type: kind === "notion" ? "page" : kind === "linear" ? "project" : "file", id: "selected-item" }, + unreadable: "", + }; + }), +}); beforeEach(() => { vi.resetAllMocks(); @@ -68,6 +80,7 @@ beforeEach(() => { }); vi.mocked(listConnections).mockResolvedValue(connections()); vi.mocked(addKnowledgeSource).mockImplementation(async (_id, _form, dry) => result(dry)); + vi.mocked(parseKnowledgeSourceLinks).mockImplementation(async (_id, links) => parsedLinks(links)); }); afterEach(() => { cleanup(); @@ -102,6 +115,13 @@ describe("connected account to document source", () => { expect(screen.getByText("Team library")).toBeVisible(); expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); await check(); + expect(parseKnowledgeSourceLinks).toHaveBeenCalledWith(NS, [LINK, OTHER], { signal: expect.any(AbortSignal) }); + expect(within(screen.getByRole("region", { name: "Content link preview" })).getAllByRole("listitem")).toHaveLength( + 2, + ); + expect( + screen.getByText("This preview identifies content. It does not check access or import documents."), + ).toBeVisible(); await userEvent.click(screen.getByRole("button", { name: "Add source" })); await waitFor(() => expect(added).toHaveBeenCalledWith("selected")); const bodies = vi @@ -138,6 +158,7 @@ describe("connected account to document source", () => { await check(); fireEvent.input(screen.getByLabelText(/^Content links/), { target: { value: LINK } }); expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + expect(screen.queryByRole("region", { name: "Content link preview" })).toBeNull(); await check(); await userEvent.click(screen.getByRole("button", { name: "Refresh account" })); await screen.findByText("Using reader@example.com."); @@ -145,6 +166,104 @@ describe("connected account to document source", () => { expect(screen.getByLabelText(/^Content links/)).toHaveValue(LINK); }); + it("keeps selected links when link resolution fails, and retries preview before checking access", async () => { + open(); + await fill(); + vi.mocked(parseKnowledgeSourceLinks).mockRejectedValueOnce( + new ApiError({ status: 503, code: "BACKEND_UNAVAILABLE", message: "The link preview is unavailable." }), + ); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + expect(await screen.findByRole("alert")).toHaveTextContent("The link preview is unavailable."); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(`${LINK}\n${OTHER}`); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + await check(); + expect(parseKnowledgeSourceLinks).toHaveBeenCalledTimes(2); + expect(addKnowledgeSource).toHaveBeenCalledTimes(1); + }); + + it.each(["unreadable", "different-provider"])( + "shows each link and blocks access Check for %s content", + async (problem) => { + open(); + await fill(); + const preview = parsedLinks([LINK, OTHER]); + preview.links[1] = + problem === "unreadable" + ? { link: OTHER, kind: "", item: { type: "", id: "" }, unreadable: "This file type is not supported." } + : { link: OTHER, kind: "notion", item: { type: "page", id: "selected-page" }, unreadable: "" }; + vi.mocked(parseKnowledgeSourceLinks).mockResolvedValueOnce(preview); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + expect(await screen.findByRole("alert")).toHaveTextContent( + problem === "unreadable" ? "This file type is not supported." : "This is Notion content.", + ); + const rows = within(screen.getByRole("region", { name: "Content link preview" })).getAllByRole("listitem"); + expect(rows).toHaveLength(2); + expect(rows[0]).toHaveTextContent(LINK); + expect(rows[1]).toHaveTextContent(OTHER); + expect(rows[1]).toHaveTextContent( + problem === "unreadable" ? "This file type is not supported." : "This is Notion content.", + ); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(`${LINK}\n${OTHER}`); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + }, + ); + + it.each(["missing", "reordered"])("does not check access for a %s preview result", async (problem) => { + open(); + await fill(); + vi.mocked(parseKnowledgeSourceLinks).mockResolvedValueOnce( + parsedLinks(problem === "missing" ? [LINK] : [OTHER, LINK]), + ); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + expect(await screen.findByRole("alert")).toHaveTextContent("The link preview did not match your selection"); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + expect(screen.queryByRole("region", { name: "Content link preview" })).toBeNull(); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + }); + + it("drops a late link preview after the signed-in principal changes", async () => { + let resolve!: (value: ReturnType) => void; + vi.mocked(parseKnowledgeSourceLinks).mockImplementationOnce( + () => + new Promise((yes) => { + resolve = yes; + }), + ); + open(); + await fill(); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + await waitFor(() => expect(parseKnowledgeSourceLinks).toHaveBeenCalledTimes(1)); + act(() => useStore.setState({ whoami: { authenticated: true, subject: "other", org: NS.org, roles: ["writer"] } })); + await act(async () => resolve(parsedLinks([LINK, OTHER]))); + expect(screen.queryByRole("region", { name: "Content link preview" })).toBeNull(); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(""); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + }); + + it("does not reuse a late preview after account refresh invalidates the check", async () => { + let resolve!: (value: ReturnType) => void; + vi.mocked(parseKnowledgeSourceLinks).mockImplementationOnce( + () => + new Promise((yes) => { + resolve = yes; + }), + ); + open(); + await fill(); + fireEvent.click(screen.getByRole("button", { name: "Check" })); + await waitFor(() => expect(parseKnowledgeSourceLinks).toHaveBeenCalledTimes(1)); + fireEvent(window, new Event("focus")); + await waitFor(() => expect(listConnections).toHaveBeenCalledTimes(2)); + await screen.findByText("Using reader@example.com."); + await act(async () => resolve(parsedLinks([LINK, OTHER]))); + expect(screen.queryByRole("region", { name: "Content link preview" })).toBeNull(); + expect(screen.getByLabelText(/^Content links/)).toHaveValue(`${LINK}\n${OTHER}`); + expect(screen.getByRole("button", { name: "Add source" })).toBeDisabled(); + expect(addKnowledgeSource).not.toHaveBeenCalled(); + }); + it.each([ ["Notion", "notion", "https://www.notion.so/Selected-1234567890abcdef1234567890abcdef"], ["Linear", "linear", "https://linear.app/team/project/selected-project"], diff --git a/frontend/src/flows/KnowledgeSourceDialog.tsx b/frontend/src/flows/KnowledgeSourceDialog.tsx index 249829f434..588231d60d 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.tsx @@ -7,7 +7,7 @@ * accounts use the server's caller binding. Shared credentials remain in dialog * memory, clear when their provider or account mode changes, and are never displayed. */ -import { useRef, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { Button } from "@clickhouse/click-ui/Button"; import { Checkbox } from "@clickhouse/click-ui/Checkbox"; import { PasswordField } from "@clickhouse/click-ui/PasswordField"; @@ -18,6 +18,8 @@ import { removeKnowledgeSource, rotateKnowledgeSource, syncKnowledgeSourceNow, + parseKnowledgeSourceLinks, + type KnowledgeSourceParsedLink, } from "../api/knowledgeSources"; import { committedAnyway, humanizeError } from "../lib/errorWords"; import { canCallOwnConnections } from "../lib/canCall"; @@ -25,6 +27,7 @@ import { useStore } from "../lib/state"; import { useAbortableEffect, useMounted } from "../lib/hooks"; import { useSourceAccount } from "./SourceAccountField"; import { SourceRegistrationFields, type SetField } from "./SourceRegistrationFields"; +import { SourceLinkPreview, validateSourceLinkPreview } from "./SourceLinkPreview"; import { credentialLabel, emptySourceForm, @@ -35,6 +38,7 @@ import { sourceFormFingerprint, sourceFormProblems, sourceFormTouched, + sourceLinks, type KnowledgeSourceCheckResult, type KnowledgeSourceProbe, type SourceForm, @@ -136,6 +140,8 @@ function SourceRegistration({ ); }; const running = useRef(false); + const previewRequest = useRef(null); + useEffect(() => () => previewRequest.current?.abort(), []); const [form, setForm] = useState(() => { const f = emptySourceForm(); return !allowShared || (whoami?.org === org && canCallOwnConnections(whoami)) @@ -145,6 +151,11 @@ function SourceRegistration({ const connected = form.credentialMode === "connected"; const account = useSourceAccount(org, form.kind === "recording" ? null : form.kind, connected); const [checked, setChecked] = useState<{ fingerprint: string; probe: KnowledgeSourceProbe } | null>(null); + const [preview, setPreview] = useState<{ + fingerprint: string; + links: KnowledgeSourceParsedLink[]; + valid: boolean; + } | null>(null); const [busy, setBusy] = useState<"check" | "add" | null>(null); const [error, setError] = useState(null); const set: SetField = (key, value) => { @@ -167,23 +178,44 @@ function SourceRegistration({ connected ? account.stamp : "shared", ]); const accountReady = !connected || account.ready; - const passed = checked !== null && checked.fingerprint === fingerprint && accountReady; + const currentFingerprint = useRef(fingerprint); + currentFingerprint.current = fingerprint; + const usesLinks = form.inputMode === "links" && form.kind !== "recording"; + const previewReady = !usesLinks || (preview?.fingerprint === fingerprint && preview.valid); + const passed = checked !== null && checked.fingerprint === fingerprint && accountReady && previewReady; const run = async (dryRun: boolean) => { if (!current() || running.current || problems.length || !accountReady || (!dryRun && !passed)) return; running.current = true; setBusy(dryRun ? "check" : "add"); setError(null); + const controller = new AbortController(); + if (dryRun) { + setChecked(null); + setPreview(null); + } try { + if (dryRun && usesLinks) { + const selected = sourceLinks(form.links); + previewRequest.current = controller; + const parsed = await parseKnowledgeSourceLinks({ org, namespace }, selected, { signal: controller.signal }); + if (!current() || currentFingerprint.current !== fingerprint) return; + const { matches, problems } = validateSourceLinkPreview(parsed.links, form.kind, selected); + if (matches) setPreview({ fingerprint, links: parsed.links, valid: problems.length === 0 }); + if (problems.length) { + setError(problems.join(" ")); + return; + } + } const out = await addKnowledgeSource({ org, namespace }, form, dryRun); - if (!current()) return; + if (!current() || (dryRun && currentFingerprint.current !== fingerprint)) return; if (dryRun) setChecked({ fingerprint, probe: out.probe }); else { onAdded(out.name); onOpenChange(false); } } catch (e) { - if (!current()) return; + if (!current() || (dryRun && currentFingerprint.current !== fingerprint)) return; if (!dryRun && committedAnyway("Add source", e)) { onAdded(normalisedName(form.name)); onOpenChange(false); @@ -194,6 +226,7 @@ function SourceRegistration({ setError(humanizeError(e)); } finally { running.current = false; + if (previewRequest.current === controller) previewRequest.current = null; if (mounted.current) setBusy(null); } }; @@ -254,6 +287,7 @@ function SourceRegistration({ /> )} {checked && !passed &&

    The form changed since the check: check again.

    } + {preview?.fingerprint === fingerprint && } {error && {error}} ); diff --git a/frontend/src/flows/SourceDetailsDialog.test.tsx b/frontend/src/flows/SourceDetailsDialog.test.tsx index 012eadface..9ac1a5b396 100644 --- a/frontend/src/flows/SourceDetailsDialog.test.tsx +++ b/frontend/src/flows/SourceDetailsDialog.test.tsx @@ -94,4 +94,14 @@ describe("SourceDetailsDialog", () => { expect(await screen.findByText("123456789")).toBeInTheDocument(); expect(screen.getByText("zoom")).toBeInTheDocument(); }); + + it.each([ + ["mirrored", "Provider access must be verified for each reader. Unverified access stays hidden."], + ["namespace", "Everyone who can read this destination can read the imported content."], + [undefined, "Access mode is not available from this server."], + ] as const)("shows the stored %s access mode without promising a provider check", async (access, text) => { + vi.mocked(api.inspectKnowledgeSource).mockResolvedValueOnce({ ...DETAIL, access }); + render( {}} />); + expect(await screen.findByText(text)).toBeInTheDocument(); + }); }); diff --git a/frontend/src/flows/SourceDetailsDialog.tsx b/frontend/src/flows/SourceDetailsDialog.tsx index 16e25c5962..5029649959 100644 --- a/frontend/src/flows/SourceDetailsDialog.tsx +++ b/frontend/src/flows/SourceDetailsDialog.tsx @@ -1,7 +1,13 @@ import { useState } from "react"; import { Button } from "@clickhouse/click-ui/Button"; import { inspectKnowledgeSource } from "../api/knowledgeSources"; -import { kindLabel, syncBadge, type KnowledgeSourceDetail } from "../lib/knowledgeSources"; +import { + kindLabel, + sourceConfigText, + sourceItemText, + syncBadge, + type KnowledgeSourceDetail, +} from "../lib/knowledgeSources"; import { humanizeError } from "../lib/errorWords"; import { useAbortableEffect } from "../lib/hooks"; import { Modal, ModalFooter } from "../components/ui/Modal"; @@ -39,18 +45,25 @@ export function SourceDetailsDialog({
    {kindLabel(detail.kind)} + + {detail.access === "mirrored" + ? "Provider access must be verified for each reader. Unverified access stays hidden." + : detail.access === "namespace" + ? "Everyone who can read this destination can read the imported content." + : "Access mode is not available from this server."} + {/* What it reads: its items; a recording source's config, or an older server's, which answers no items. */} {detail.items?.length ? ( {detail.items.map((item) => ( -
    {itemText(item)}
    +
    {sourceItemText(item)}
    ))}
    ) : ( Object.entries(detail.config).map(([k, v]) => ( - {configText(v)} + {sourceConfigText(v)} )) )} @@ -97,30 +110,3 @@ export function SourceDetailsDialog({ ); } - -const ITEM_LABELS: Record = { - folder: "Folder", - file: "File", - shared_drive: "Shared drive", - filter: "Files matching", - page: "Page", - database: "Database", - project: "Project", - name_contains: "name contains", - modified_after: "changed after", - in: "in folder", -}; - -/** One item as a line: its type, its id or filter fields, and `recursive`. */ -function itemText(item: Record): string { - const { type, recursive, ...rest } = item; - const label = (k: string) => ITEM_LABELS[k] ?? k; - const fields = Object.entries(rest).map(([k, v]) => (k === "id" ? configText(v) : `${label(k)}: ${configText(v)}`)); - return [label(String(type)), ...fields, ...(recursive ? ["(with subfolders)"] : [])].join(" "); -} - -/** A config value as text: a list of ids reads as one comma-separated line. */ -function configText(v: unknown): string { - if (Array.isArray(v)) return v.join(", "); - return typeof v === "string" ? v : JSON.stringify(v); -} diff --git a/frontend/src/flows/SourceLinkPreview.tsx b/frontend/src/flows/SourceLinkPreview.tsx new file mode 100644 index 0000000000..11700584ec --- /dev/null +++ b/frontend/src/flows/SourceLinkPreview.tsx @@ -0,0 +1,81 @@ +import type { KnowledgeSourceParsedLink } from "../api/knowledgeSources"; +import { KIND_OPTIONS } from "../lib/knowledgeSources"; + +const ITEM_LABELS: Record = { + folder: "Folder", + file: "File", + shared_drive: "Shared drive", + filter: "Files matching a filter", + page: "Page", + database: "Database", + project: "Project", +}; + +function providerLabel(kind: string): string { + return KIND_OPTIONS.find((option) => option.value === kind)?.label ?? "another provider"; +} + +function linkProblem(link: KnowledgeSourceParsedLink, expectedKind?: string): string | null { + if (link.unreadable) return link.unreadable; + if (!link.kind || !Object.keys(link.item).length) return "This link did not resolve to supported content."; + if (expectedKind && link.kind !== expectedKind) + return `This is ${providerLabel(link.kind)} content. Choose that provider or remove this link.`; + return null; +} + +export function validateSourceLinkPreview( + links: KnowledgeSourceParsedLink[], + expectedKind: string, + requestedLinks: readonly string[], +): { matches: boolean; problems: string[] } { + if (links.length !== requestedLinks.length || links.some((row, i) => row.link !== requestedLinks[i])) + return { matches: false, problems: ["The link preview did not match your selection. Check the links again."] }; + return { + matches: true, + problems: links.flatMap((row) => { + const problem = linkProblem(row, expectedKind); + return problem ? [`${row.link}: ${problem}`] : []; + }), + }; +} + +/** Every selected link keeps its own result; resolving a type proves no provider access. */ +export function SourceLinkPreview({ + links, + expectedKind, +}: { + links: KnowledgeSourceParsedLink[]; + expectedKind?: string; +}) { + const problems = links.filter((row) => linkProblem(row, expectedKind)); + return ( +
    +

    + {links.length} {links.length === 1 ? "link" : "links"} previewed. + {problems.length > 0 && ` ${problems.length} ${problems.length === 1 ? "needs" : "need"} attention.`} +

    +

    + This preview identifies content. It does not check access or import documents. +

    +
      + {links.map((row, index) => { + const problem = linkProblem(row, expectedKind); + return ( +
    • +

      {row.link}

      + {problem ? ( +

      Cannot use this link: {problem}

      + ) : ( +

      + {providerLabel(row.kind)} · {ITEM_LABELS[String(row.item.type)] ?? "Selected content"} + {typeof row.item.id === "string" && ` · ${row.item.id}`} + {row.item.recursive === true && " · with subfolders"} +

      + )} +
    • + ); + })} +
    +
    + ); +} diff --git a/frontend/src/lib/knowledgeSources.ts b/frontend/src/lib/knowledgeSources.ts index 69fd231ad6..8e2b6ef343 100644 --- a/frontend/src/lib/knowledgeSources.ts +++ b/frontend/src/lib/knowledgeSources.ts @@ -31,6 +31,7 @@ export interface KnowledgeSource { credential_mode?: "shared" | "connected"; owner_subject?: string; owned_by_caller?: boolean; + access?: "namespace" | "mirrored"; /** The ids and filters the source reads, in the form that names ids per kind; {} when * an item has no place in it. Never a credential. */ config: Record; @@ -121,6 +122,34 @@ export function kindLabel(kind: string): string { return KIND_LABEL[kind] ?? kind; } +const ITEM_LABELS: Record = { + folder: "Folder", + file: "File", + shared_drive: "Shared drive", + filter: "Files matching", + page: "Page", + database: "Database", + project: "Project", + name_contains: "name contains", + modified_after: "changed after", + in: "in folder", +}; + +export function sourceConfigText(value: unknown): string { + if (Array.isArray(value)) return value.join(", "); + return typeof value === "string" ? value : JSON.stringify(value); +} + +/** A stored item as a label, retaining its id and filter values. */ +export function sourceItemText(item: Record): string { + const { type, recursive, ...rest } = item; + const label = (key: string) => ITEM_LABELS[key] ?? key; + const fields = Object.entries(rest).map(([key, value]) => + key === "id" ? sourceConfigText(value) : `${label(key)}: ${sourceConfigText(value)}`, + ); + return [label(String(type)), ...fields, ...(recursive ? ["(with subfolders)"] : [])].join(" "); +} + /** The credential field's label for a kind, in the add and rotate forms. */ const CREDENTIAL_LABEL: Record = { gdrive: "Service account key (JSON; stored encrypted, never shown again)", diff --git a/frontend/src/views/SourcesList.test.tsx b/frontend/src/views/SourcesList.test.tsx index 8e689932cb..193a7d27d4 100644 --- a/frontend/src/views/SourcesList.test.tsx +++ b/frontend/src/views/SourcesList.test.tsx @@ -128,6 +128,37 @@ describe("SourcesList", () => { await userEvent.click(await screen.findByRole("button", { name: "Actions for own-alias" })); expect(await screen.findByRole("menuitem", { name: "Manage connection" })).toBeInTheDocument(); expect(screen.getByRole("menuitem", { name: "Sync now" })).toBeInTheDocument(); + expect(screen.getByRole("menuitem", { name: "Edit items" })).toBeInTheDocument(); + }); + + it("discards the prior caller's ownership projection and reloads after a sign-in change", async () => { + writer(); + sources.listKnowledgeSources.mockResolvedValueOnce({ + enabled: true, + sources: [connectedSource("private-selection", "alice")], + }); + let finish!: (value: KnowledgeSourceList) => void; + sources.listKnowledgeSources.mockImplementationOnce( + () => + new Promise((resolve) => { + finish = resolve; + }), + ); + render(); + await screen.findByRole("button", { name: "Actions for private-selection" }); + await act(async () => { + useStore.setState({ whoami: { authenticated: true, subject: "bob", org: "acme", roles: ["writer"] } }); + }); + await waitFor(() => expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(2)); + expect(screen.queryByRole("button", { name: "private-selection" })).not.toBeInTheDocument(); + await act(async () => { + finish({ + enabled: true, + sources: [{ ...connectedSource("private-selection", "alice"), owned_by_caller: false }], + }); + }); + await screen.findByRole("button", { name: "private-selection" }); + expect(screen.queryByRole("button", { name: "Actions for private-selection" })).not.toBeInTheDocument(); }); it("does not offer an admin mutation controls over another person's connected source", async () => { diff --git a/frontend/src/views/SourcesList.tsx b/frontend/src/views/SourcesList.tsx index 8d084149f8..b8dc11ed3a 100644 --- a/frontend/src/views/SourcesList.tsx +++ b/frontend/src/views/SourcesList.tsx @@ -49,6 +49,9 @@ const RotateSourceDialog = lazy(() => const SourceDetailsDialog = lazy(() => import("../flows/SourceDetailsDialog").then((m) => ({ default: m.SourceDetailsDialog })), ); +const EditSourceItemsDialog = lazy(() => + import("../flows/EditSourceItemsDialog").then((m) => ({ default: m.EditSourceItemsDialog })), +); const SourcePushDialog = lazy(() => import("../flows/SourcePushDialog").then((m) => ({ default: m.SourcePushDialog }))); const RetireSourceDialog = lazy(() => import("../flows/KnowledgeSourceDialog").then((m) => ({ default: m.RetireSourceDialog })), @@ -63,7 +66,7 @@ type State = | { kind: "refused" } | { kind: "error"; error: unknown }; -type Verb = "check" | "sync" | "rotate" | "retire" | "push"; +type Verb = "check" | "sync" | "rotate" | "retire" | "push" | "edit"; function SourceRow({ s, items, onOpen }: { s: KnowledgeSource; items: RowMenuItem[]; onOpen: () => void }) { const badge = syncBadge(s.sync); @@ -117,16 +120,7 @@ function SourceRow({ s, items, onOpen }: { s: KnowledgeSource; items: RowMenuIte /** The place a list belongs to: what the add dialog's title names, and what the copy calls it. */ export type SourcesPlace = { label: string; noun: "repository" | "namespace" | "collection" }; -export function SourcesList({ - org, - namespace, - place, - manageable, - connectedManageable = false, - busy = false, - section, - panel, -}: { +interface SourcesListProps { org: string; /** The namespace the sources live in. */ namespace: string; @@ -143,7 +137,38 @@ export function SourcesList({ section?: string; /** Where the open section goes (below the page header). */ panel?: HTMLElement | null; -}) { +} + +export function SourcesList(props: SourcesListProps) { + const scope = useStore((s) => + JSON.stringify([ + s.identity?.org, + s.whoami?.org, + s.whoami?.subject, + s.whoami?.roles, + s.whoami?.is_service_account, + s.authed, + s.sessionExpired, + ]), + ); + return ( + + ); +} + +function ScopedSourcesList({ + org, + namespace, + place, + manageable, + connectedManageable = false, + busy = false, + section, + panel, +}: SourcesListProps) { const subject = useStore((s) => s.whoami?.subject); const canAdd = manageable || connectedManageable; const [state, setState] = useState({ kind: "loading" }); @@ -195,6 +220,8 @@ export function SourcesList({ const items = closed ? [check, retire] : [check, sync, ...(s.credential_mode === "connected" ? [] : [rotate]), retire]; + if (!closed && ["gdrive", "notion", "linear"].includes(s.kind)) + items.splice(1, 0, { label: "Edit items", onSelect: act("edit") }); if (!closed && owned) items.splice(2, 0, { label: "Manage connection", @@ -299,6 +326,19 @@ export function SourcesList({ )} {acting && ( + {acting.verb === "edit" && ( + setActing(null)} + onUpdated={() => { + toast(`Updated ${acting.source.name}`, "ok"); + reload(); + }} + /> + )} {acting.verb === "push" && ( dict[str, str]: "the assistant agent's SQL tool; no dashboard page calls it until the Assistant " "page's chat" ), - "knowledge.source.update": ( - "agents and `loom knowledge source update` change a source's items; the source " - "dialog registers a source with knowledge.source.add" - ), - "knowledge.source.parse_links": ( - "agents preview pasted links with it; add and update take links directly; the " - "source dialog takes ids" - ), }, ) diff --git a/server/operations/reach_manifest.py b/server/operations/reach_manifest.py index 0ebff31d12..2d2d835412 100644 --- a/server/operations/reach_manifest.py +++ b/server/operations/reach_manifest.py @@ -341,12 +341,14 @@ "knowledge.source.check": ("dashboard",), "knowledge.source.inspect": ("dashboard",), "knowledge.source.list": ("dashboard",), + "knowledge.source.parse_links": ("dashboard",), "knowledge.source.push_configure": ("dashboard",), "knowledge.source.push_confirm": ("dashboard",), "knowledge.source.push_status": ("dashboard",), "knowledge.source.remove": ("dashboard",), "knowledge.source.rotate": ("dashboard",), "knowledge.source.sync_now": ("dashboard",), + "knowledge.source.update": ("dashboard",), "knowledge.synthesize": ("sdk_method",), "knowledge.update_metadata": ("dashboard",), "knowledge.upsert": ( From e18da55a22813112719c7d40d63288262368d86c Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 19:31:22 +0000 Subject: [PATCH 4/6] fix: preserve source editor recovery and phone actions Refresh source items before another edit after an uncertain save and return focus to the source list. Keep the phone action label compact and cover connected-account permissions at both entrypoints. Signed-off-by: zlareb1 --- .../src/flows/EditSourceItemsDialog.test.tsx | 26 ++++++ frontend/src/flows/EditSourceItemsDialog.tsx | 8 +- frontend/src/views/Documents.sources.test.tsx | 29 +++++-- frontend/src/views/Documents.tsx | 11 ++- frontend/src/views/RepositoryCard.test.tsx | 20 ++++- frontend/src/views/SourcesList.test.tsx | 81 ++++++++++++++++++- frontend/src/views/SourcesList.tsx | 32 ++++++-- 7 files changed, 185 insertions(+), 22 deletions(-) diff --git a/frontend/src/flows/EditSourceItemsDialog.test.tsx b/frontend/src/flows/EditSourceItemsDialog.test.tsx index 05899045b8..fb98efb43d 100644 --- a/frontend/src/flows/EditSourceItemsDialog.test.tsx +++ b/frontend/src/flows/EditSourceItemsDialog.test.tsx @@ -240,4 +240,30 @@ describe("EditSourceItemsDialog", () => { expect(screen.getByRole("button", { name: "Review changes" })).toBeDisabled(); expect(screen.getByRole("button", { name: "Save changes" })).toBeDisabled(); }); + + it("keeps the dialog open while an incremental save is pending", async () => { + let reject!: (error: Error) => void; + vi.mocked(updateKnowledgeSource).mockImplementationOnce( + () => + new Promise((_, fail) => { + reject = fail; + }), + ); + const callbacks = open(); + await userEvent.click(screen.getByRole("checkbox", { name: "File keep-file" })); + await review(); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + await userEvent.click(screen.getByRole("button", { name: "Cancel" })); + await userEvent.keyboard("{Escape}"); + const closesWhilePending = callbacks.onClose.mock.calls.length; + await act(async () => { + reject(new TypeError("Failed to fetch")); + }); + await screen.findByText(/Save could not be confirmed/); + expect(closesWhilePending).toBe(0); + expect(screen.getByRole("button", { name: "Cancel" })).toBeEnabled(); + expect(updateKnowledgeSource).toHaveBeenCalledTimes(1); + await userEvent.click(screen.getByRole("button", { name: "Cancel" })); + expect(callbacks.onClose).toHaveBeenCalledOnce(); + }); }); diff --git a/frontend/src/flows/EditSourceItemsDialog.tsx b/frontend/src/flows/EditSourceItemsDialog.tsx index f7ed0582e1..eea67d2bad 100644 --- a/frontend/src/flows/EditSourceItemsDialog.tsx +++ b/frontend/src/flows/EditSourceItemsDialog.tsx @@ -22,6 +22,7 @@ interface Props { canManage: boolean; onClose: () => void; onUpdated: () => void; + onCloseAutoFocus?: (event: Event) => void; } function actorScope() { @@ -50,7 +51,7 @@ export function EditSourceItemsDialog(props: Props) { ); } -function EditSourceItems({ org, namespace, source, canManage, onClose, onUpdated }: Props) { +function EditSourceItems({ org, namespace, source, canManage, onClose, onUpdated, onCloseAutoFocus }: Props) { const mounted = useMounted(); const whoami = useStore((s) => s.whoami); const actor = actorScope(); @@ -168,14 +169,15 @@ function EditSourceItems({ org, namespace, source, canManage, onClose, onUpdated return ( { - if (!open) onClose(); + if (!open && busy !== "save") onClose(); }} title={`Edit items · ${source.name}`} description="Add content links or remove selected items from this source." footer={ <> - + )} diff --git a/frontend/src/views/RepositoryCard.test.tsx b/frontend/src/views/RepositoryCard.test.tsx index 1a62de078c..97dc70c33b 100644 --- a/frontend/src/views/RepositoryCard.test.tsx +++ b/frontend/src/views/RepositoryCard.test.tsx @@ -32,7 +32,7 @@ vi.mock("../api/knowledgeSources", async (importOriginal) => { return { ...actual, listKnowledgeSources: vi.fn(async () => ({ sources: SOURCES, enabled: false })) }; }); -// One synced document source, for the Sources facet (hoisted: the module mock reads it). +// One synced shared-credential source (hoisted: the module mock reads it). const SOURCES = vi.hoisted(() => [ { name: "design-docs", @@ -952,12 +952,12 @@ describe("RepositoryCard — sources", () => { within(menu) .getAllByRole("menuitem") .map((m) => m.textContent), - ).toEqual(["Check", "Sync now", "Push notifications", "Rotate credential", "Retire"]); + ).toEqual(["Check", "Edit items", "Push notifications", "Sync now", "Rotate credential", "Retire"]); }); }); // Both facets offer their verbs on the rule the server gates the ops on (`canCallOn`): -// Add a document and Remove need writer, every source verb needs admin. +// Writers add documents and their own connected sources; shared-source actions need admin. describe("RepositoryCard — who gets the Docs and Sources verbs", () => { afterEach(() => { useStore.setState({ whoami: null }); @@ -991,7 +991,7 @@ describe("RepositoryCard — who gets the Docs and Sources verbs", () => { return out; } const none = { addDocument: false, addSource: false, sourceMenu: false }; - const writer = { addDocument: true, addSource: false, sourceMenu: false }; + const writer = { addDocument: true, addSource: true, sourceMenu: false }; const all = { addDocument: true, addSource: true, sourceMenu: true }; it("auth off and an admin claim get every verb without a grant", async () => { @@ -1025,6 +1025,18 @@ describe("RepositoryCard — who gets the Docs and Sources verbs", () => { expect(await verbs({ my_role: "" }, null)).toEqual(none); }); + it.each([{ is_service_account: true }, { org: "another-org" }])( + "does not offer connected-source Add to an ineligible principal (%j)", + async (over) => { + const principal = { ...token(["WRITER"]), ...over }; + expect(await verbs({ my_role: "writer" }, principal)).toEqual({ + addDocument: true, + addSource: false, + sourceMenu: false, + }); + }, + ); + it("a restricted namespace needs the grant and a token naming it, never a claim alone", async () => { const restricted = { classification: "restricted" as const }; const naming = token(["ADMIN"], ["code-clickhouse"]); diff --git a/frontend/src/views/SourcesList.test.tsx b/frontend/src/views/SourcesList.test.tsx index 193a7d27d4..c86afc20ab 100644 --- a/frontend/src/views/SourcesList.test.tsx +++ b/frontend/src/views/SourcesList.test.tsx @@ -9,7 +9,7 @@ import { SourcesList } from "./SourcesList"; // A failed read of the document sources shows the error, its request id and a Retry that // reads them again; a reader who cannot manage the sources gets no section at all. -const sources = vi.hoisted(() => ({ listKnowledgeSources: vi.fn() })); +const sources = vi.hoisted(() => ({ listKnowledgeSources: vi.fn(), updateKnowledgeSource: vi.fn() })); vi.mock("../api/knowledgeSources", async (importOriginal) => ({ ...(await importOriginal()), ...sources, @@ -272,4 +272,83 @@ describe("SourcesList", () => { expect(screen.queryByText(/sync requested/)).not.toBeInTheDocument(); expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(2); }); + + it.each(["settled", "pending"])("reloads current items before reopening an unconfirmed %s edit", async (outcome) => { + writer(); + const initial = { + ...connectedSource("own-selection", "alice"), + items: [ + { type: "file", id: "removed-file" }, + { type: "file", id: "remaining-file" }, + ], + }; + let finish!: (value: KnowledgeSourceList) => void; + sources.listKnowledgeSources + .mockReset() + .mockResolvedValue({ enabled: true, sources: [{ ...initial, items: [] }] }) + .mockResolvedValueOnce({ enabled: true, sources: [initial] }); + sources.listKnowledgeSources.mockImplementationOnce( + () => + new Promise((resolve) => { + finish = resolve; + }), + ); + let failSave!: (error: Error) => void; + sources.updateKnowledgeSource.mockReset(); + if (outcome === "pending") { + sources.updateKnowledgeSource.mockImplementationOnce( + () => + new Promise((_, reject) => { + failSave = reject; + }), + ); + } else { + sources.updateKnowledgeSource.mockRejectedValueOnce(new TypeError("Failed to fetch")); + } + sources.updateKnowledgeSource.mockResolvedValueOnce({}); + render(); + await userEvent.click(await screen.findByRole("button", { name: "Actions for own-selection" })); + await userEvent.click(await screen.findByRole("menuitem", { name: "Edit items" })); + await userEvent.click(await screen.findByRole("checkbox", { name: "File removed-file" })); + await userEvent.click(screen.getByRole("button", { name: "Review changes" })); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + if (outcome === "pending") { + await userEvent.click(screen.getByRole("button", { name: "Cancel" })); + await userEvent.keyboard("{Escape}"); + expect(screen.getByRole("dialog")).toBeInTheDocument(); + expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(1); + expect(sources.updateKnowledgeSource).toHaveBeenCalledTimes(1); + await act(async () => { + failSave(new TypeError("Failed to fetch")); + }); + } + await screen.findByText(/Save could not be confirmed/); + await userEvent.click(screen.getByRole("button", { name: "Cancel" })); + await waitFor(() => expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(2)); + const action = screen.getByRole("button", { name: "Actions for own-selection" }); + await waitFor(() => expect(screen.getByRole("region", { name: "Document sources" })).toHaveFocus()); + await userEvent.click(action); + const edit = await screen.findByRole("menuitem", { name: "Edit items" }); + expect(edit).toHaveAttribute("aria-disabled", "true"); + await userEvent.click(edit); + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); + expect(sources.updateKnowledgeSource).toHaveBeenCalledTimes(1); + await userEvent.keyboard("{Escape}"); + await act(async () => { + finish({ enabled: true, sources: [{ ...initial, items: [{ type: "file", id: "remaining-file" }] }] }); + }); + await userEvent.click(await screen.findByRole("button", { name: "Actions for own-selection" })); + await userEvent.click(await screen.findByRole("menuitem", { name: "Edit items" })); + expect(await screen.findByRole("checkbox", { name: "File remaining-file" })).not.toBeChecked(); + expect(screen.queryByRole("checkbox", { name: "File removed-file" })).not.toBeInTheDocument(); + await userEvent.click(screen.getByRole("checkbox", { name: "File remaining-file" })); + await userEvent.click(screen.getByRole("button", { name: "Review changes" })); + await userEvent.click(screen.getByRole("button", { name: "Save changes" })); + expect(sources.updateKnowledgeSource).toHaveBeenNthCalledWith( + 2, + { org: "acme", namespace: "docs" }, + "own-selection", + { remove_items: [{ type: "file", id: "remaining-file" }] }, + ); + }); }); diff --git a/frontend/src/views/SourcesList.tsx b/frontend/src/views/SourcesList.tsx index b8dc11ed3a..3f1330377b 100644 --- a/frontend/src/views/SourcesList.tsx +++ b/frontend/src/views/SourcesList.tsx @@ -177,12 +177,14 @@ function ScopedSourcesList({ const [open, setOpen] = useState(false); const phone = useMedia(PHONE_QUERY); const heading = useRef(null); + const listFocus = useRef(null); // The panel sits below the header, past the page's other controls: opening it moves // the focus to its heading, so the list is next in the tab order. useEffect(() => { if (open) heading.current?.focus(); }, [open]); const [nonce, setNonce] = useState(0); + const [reloadingItems, setReloadingItems] = useState(false); const [adding, setAdding] = useState(false); const [acting, setActing] = useState<{ verb: Verb; source: KnowledgeSource } | null>(null); // Details is a read: any reader may open it. @@ -202,6 +204,8 @@ function ScopedSourcesList({ if (isOpAbsent(e)) setState({ kind: "absent" }); else if (e instanceof ApiError && (e.status === 403 || e.status === 401)) setState({ kind: "refused" }); else setState({ kind: "error", error: e }); + } finally { + if (live()) setReloadingItems(false); } }, [org, namespace, nonce], @@ -231,7 +235,7 @@ function ScopedSourcesList({ if (!closed && manageable && ["gdrive", "notion", "linear"].includes(s.kind)) { items.splice(2, 0, { label: "Push notifications", icon: "refresh", onSelect: act("push") }); } - return items.map((item) => ({ ...item, disabled: busy })); + return items.map((item) => ({ ...item, disabled: busy || reloadingItems })); }; const close = (v: boolean) => { if (!v) setActing(null); @@ -246,7 +250,8 @@ function ScopedSourcesList({ const failedForReader = state.kind !== "ready" && !loading && !canAdd; if (section && (closedAndEmpty || nothingForReader || loading || failedForReader)) return null; const list = ( -
    +
    + {reloadingItems &&

    Refreshing source items…

    } {state.kind === "loading" && ( @@ -284,7 +289,13 @@ function ScopedSourcesList({
)} {canAdd && state.kind === "ready" && state.accepts && ( - )} @@ -320,7 +331,7 @@ function ScopedSourcesList({
)} {state.kind === "ready" && ( - )} @@ -332,10 +343,17 @@ function ScopedSourcesList({ namespace={namespace} source={acting.source} canManage={acting.source.credential_mode === "connected" ? connectedManageable : manageable} - onClose={() => setActing(null)} + onCloseAutoFocus={(event) => { + event.preventDefault(); + listFocus.current?.focus(); + }} + onClose={() => { + setActing(null); + setReloadingItems(true); + reload(); + }} onUpdated={() => { toast(`Updated ${acting.source.name}`, "ok"); - reload(); }} /> )} @@ -395,7 +413,7 @@ function ScopedSourcesList({ )} )} - + ); if (!section) return list; const count = state.kind === "ready" ? state.sources.length : 0; From e8ceb7d10f8afd4c0598e80b91f9a46057f18f9e Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 19:50:24 +0000 Subject: [PATCH 5/6] fix: explain source retirement and preserve connection context Use stored access policy for retirement confirmation and count reporting. Keep the source return marker during account-page navigation and align browser fixtures with the connected-account default. Signed-off-by: zlareb1 --- frontend/e2e/connected-source.spec.ts | 3 +- frontend/e2e/repositories.spec.ts | 10 +++-- .../src/flows/KnowledgeSourceDialog.test.tsx | 43 +++++++++++++++++-- frontend/src/flows/KnowledgeSourceDialog.tsx | 31 +++++++++---- frontend/src/lib/knowledgeSources.test.ts | 10 +++-- frontend/src/lib/knowledgeSources.ts | 16 +++++-- frontend/src/lib/state.ts | 2 +- frontend/src/views/ConnectedAccounts.test.tsx | 18 ++++++++ frontend/src/views/SourcesList.test.tsx | 39 +++++++++++++++++ frontend/src/views/SourcesList.tsx | 1 + 10 files changed, 149 insertions(+), 24 deletions(-) diff --git a/frontend/e2e/connected-source.spec.ts b/frontend/e2e/connected-source.spec.ts index 74fdcbdf5c..4af1846ec0 100644 --- a/frontend/e2e/connected-source.spec.ts +++ b/frontend/e2e/connected-source.spec.ts @@ -235,9 +235,10 @@ async function checkConnectionTab( const url = new URL(popup.url()); expect(url.origin).toBe(origin); expect(url.pathname).toBe("/settings/connected-accounts"); - expect([...url.searchParams.keys()].sort()).toEqual(["from", "kind"]); + expect([...url.searchParams.keys()].sort()).toEqual(["from", "kind", "namespace"]); expect(url.searchParams.get("kind")).toBe("gdrive"); expect(url.searchParams.get("from")).toBe("source"); + expect(url.searchParams.get("namespace")).toBe("bench"); for (const provider of ["Google Drive", "Notion", "Linear"]) await expect(popup.getByRole("button", { name: `Connect ${provider}`, exact: true })).toBeVisible(); await expect(popup.getByRole("heading", { name: "Google Drive", exact: true })).toBeFocused(); diff --git a/frontend/e2e/repositories.spec.ts b/frontend/e2e/repositories.spec.ts index 14348a87cc..3c99b8a663 100644 --- a/frontend/e2e/repositories.spec.ts +++ b/frontend/e2e/repositories.spec.ts @@ -280,7 +280,7 @@ test.describe("document sources", () => { docs_namespace: "code-clickhouse", })); - test("a reader sees each source's last sync and no verb, read once when the card opens", async ({ page }) => { + test("a reader sees each source's last sync and no mutation, read once when the card opens", async ({ page }) => { const calls = await installApi(page, { codeRepos: reposWith("viewer"), knowledgeSources: knowledgeSourcesFixture(), @@ -300,8 +300,9 @@ test.describe("document sources", () => { await expect(sources).toContainText("design-docs"); await expect(sources).toContainText("Synced"); await expect(sources).toContainText("12 documents listed · 3 written · 1 not converted"); - // The only button is the source's name, which opens its details. - await expect(sources.getByRole("button")).toHaveText(["design-docs"]); + await expect(sources.getByRole("button")).toHaveText(["design-docs", "Refresh sources"]); + await expect(sources.getByRole("button", { name: "Add a source" })).toHaveCount(0); + await expect(sources.getByRole("button", { name: "Actions for design-docs" })).toHaveCount(0); await page.waitForTimeout(300); expect(requested).toEqual(["/v1/knowledge.source.list"]); expect(calls.knowledgeSources).toBe(1); @@ -326,6 +327,9 @@ test.describe("document sources", () => { await sources.getByRole("button", { name: "Add a source" }).click(); const dialog = page.getByRole("dialog"); await expect(dialog.getByRole("button", { name: "Add source" })).toBeDisabled(); + await expect(dialog.getByRole("radio", { name: "My connected account", exact: true })).toBeChecked(); + await expect(dialog.locator('input[type="password"]')).toHaveCount(0); + await dialog.getByRole("radio", { name: "Shared credential", exact: true }).click(); await expect(dialog.getByLabel(/^Service account key/)).toHaveAttribute("type", "password"); await dialog.getByRole("button", { name: "Cancel" }).click(); for (const verb of ["Check", "Sync now", "Rotate credential", "Retire"]) { diff --git a/frontend/src/flows/KnowledgeSourceDialog.test.tsx b/frontend/src/flows/KnowledgeSourceDialog.test.tsx index 755c6f90d6..c7ade1aca3 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.test.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.test.tsx @@ -457,7 +457,7 @@ describe("SyncNowDialog", () => { }); describe("RetireSourceDialog", () => { - function renderRetire(enabled: boolean, onRetired = vi.fn()) { + function renderRetire(enabled: boolean, onRetired: () => void, access: "namespace" | "mirrored" | undefined) { render( { {...NS} name="design-docs" enabled={enabled} + access={access} onRetired={onRetired} />, ); return onRetired; } + it.each([true, false])("explains mandatory mirrored-copy retirement with sync enabled=%s", async (enabled) => { + vi.mocked(removeKnowledgeSource).mockResolvedValue({ name: "design-docs", documents_pending: 0 }); + const onRetired = renderRetire(enabled, vi.fn(), "mirrored"); + expect(screen.getByText(/Mirrored copies are always queued for retirement/)).toBeInTheDocument(); + expect(screen.queryByRole("checkbox")).not.toBeInTheDocument(); + fireEvent.change(screen.getByLabelText(/to confirm/), { target: { value: "design-docs" } }); + fireEvent.click(screen.getByRole("button", { name: "Retire source" })); + await waitFor(() => expect(onRetired).toHaveBeenCalled()); + expect(removeKnowledgeSource).toHaveBeenCalledWith(NS, "design-docs", "design-docs", true); + const message = String(vi.mocked(toast).mock.calls[0]?.[0]); + expect(message).toContain("mirrored copies are queued for retirement"); + expect(message).not.toMatch(/documents stay|no documents left/); + if (!enabled) expect(message).toContain("once sync is on"); + }); + + it.each([ + { enabled: true, requested: false, pending: 0 }, + { enabled: false, requested: false, pending: 7 }, + { enabled: false, requested: true, pending: 7 }, + ])("keeps unknown-policy retirement explicit: %j", async ({ enabled, requested, pending }) => { + vi.mocked(removeKnowledgeSource).mockResolvedValue({ name: "design-docs", documents_pending: pending }); + const onRetired = renderRetire(enabled, vi.fn(), undefined); + expect(screen.getByText(/stored access policy is unavailable/)).toBeInTheDocument(); + expect(screen.queryByText(/documents it wrote stay/)).not.toBeInTheDocument(); + const request = screen.getByRole("checkbox", { name: "Request retirement of imported documents" }); + expect(request).not.toBeChecked(); + if (requested) fireEvent.click(request); + fireEvent.change(screen.getByLabelText(/to confirm/), { target: { value: "design-docs" } }); + fireEvent.click(screen.getByRole("button", { name: "Retire source" })); + await waitFor(() => expect(onRetired).toHaveBeenCalled()); + expect(removeKnowledgeSource).toHaveBeenCalledWith(NS, "design-docs", "design-docs", requested); + expect(String(vi.mocked(toast).mock.calls[0]?.[0])).toBe( + "Retired design-docs; document retirement follows the source's stored access policy", + ); + }); + it("needs the typed name and keeps the documents unless asked", async () => { vi.mocked(removeKnowledgeSource).mockResolvedValue({ name: "design-docs", documents_pending: 0 }); - const onRetired = renderRetire(true); + const onRetired = renderRetire(true, vi.fn(), "namespace"); const retire = screen.getByRole("button", { name: "Retire source" }); expect(retire).toBeDisabled(); fireEvent.change(screen.getByLabelText(/to confirm/), { target: { value: "design-docs" } }); @@ -485,7 +522,7 @@ describe("RetireSourceDialog", () => { it("with sync off, says the documents wait until sync is on", async () => { vi.mocked(removeKnowledgeSource).mockResolvedValue({ name: "design-docs", documents_pending: 1 }); - const onRetired = renderRetire(false); + const onRetired = renderRetire(false, vi.fn(), "namespace"); fireEvent.click(screen.getByRole("checkbox", { name: /Also retire the documents/ })); fireEvent.change(screen.getByLabelText(/to confirm/), { target: { value: "design-docs" } }); fireEvent.click(screen.getByRole("button", { name: "Retire source" })); diff --git a/frontend/src/flows/KnowledgeSourceDialog.tsx b/frontend/src/flows/KnowledgeSourceDialog.tsx index 588231d60d..008d6e235a 100644 --- a/frontend/src/flows/KnowledgeSourceDialog.tsx +++ b/frontend/src/flows/KnowledgeSourceDialog.tsx @@ -39,6 +39,7 @@ import { sourceFormProblems, sourceFormTouched, sourceLinks, + type KnowledgeSource, type KnowledgeSourceCheckResult, type KnowledgeSourceProbe, type SourceForm, @@ -464,18 +465,26 @@ export function RetireSourceDialog({ namespace, name, enabled, + access, onRetired, -}: DialogProps & { name: string; enabled: boolean; onRetired: () => void }) { +}: DialogProps & { name: string; enabled: boolean; access: KnowledgeSource["access"]; onRetired: () => void }) { const [typed, setTyped] = useState(""); const [deleteDocuments, setDeleteDocuments] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); + const retireDocuments = access === "mirrored" || deleteDocuments; + const description = + access === "mirrored" + ? "Loom stops pulling from this source. Mirrored copies are always queued for retirement. Only this source's imported copies are retired over future sync passes; retirement waits until sync is on." + : access === "namespace" + ? "Loom stops pulling from the source and clears its stored credential. The documents it wrote stay unless you ask for them to go too." + : "Loom stops pulling from this source. Its stored access policy is unavailable: imported copies may be retired even if you leave the retirement request unchecked."; const submit = async () => { setBusy(true); setError(null); try { - const out = await removeKnowledgeSource({ org, namespace }, name, typed.trim(), deleteDocuments); - toast(retiredMessage(name, deleteDocuments, out.documents_pending, enabled), "ok"); + const out = await removeKnowledgeSource({ org, namespace }, name, typed.trim(), retireDocuments); + toast(retiredMessage(name, retireDocuments, out.documents_pending, enabled, access), "ok"); onRetired(); onOpenChange(false); } catch (e) { @@ -490,7 +499,7 @@ export function RetireSourceDialog({ onOpenChange={onOpenChange} width={460} title={`Retire ${name}?`} - description="Loom stops pulling from the source and clears its stored credential. The documents it wrote stay unless you ask for them to go too." + description={description} footer={ <> @@ -506,11 +515,15 @@ export function RetireSourceDialog({ } > - setDeleteDocuments(v === true)} - /> + {access !== "mirrored" && ( + setDeleteDocuments(v === true)} + /> + )} diff --git a/frontend/src/lib/knowledgeSources.test.ts b/frontend/src/lib/knowledgeSources.test.ts index 7f11adea1f..9ca62bf247 100644 --- a/frontend/src/lib/knowledgeSources.test.ts +++ b/frontend/src/lib/knowledgeSources.test.ts @@ -279,10 +279,12 @@ describe("toggleInclude", () => { describe("retiredMessage", () => { it("says whether the documents stay, how many go, and that they wait while sync is off", () => { - expect(retiredMessage("d", false, 5, true)).toBe("Retired d; its documents stay"); - expect(retiredMessage("d", true, 0, true)).toBe("Retired d; it has no documents left to retire"); - expect(retiredMessage("d", true, 1, true)).toBe("Retired d; its 1 document is retired over the next sync passes"); - expect(retiredMessage("d", true, 3, false)).toBe( + expect(retiredMessage("d", false, 5, true, "namespace")).toBe("Retired d; its documents stay"); + expect(retiredMessage("d", true, 0, true, "namespace")).toBe("Retired d; it has no documents left to retire"); + expect(retiredMessage("d", true, 1, true, "namespace")).toBe( + "Retired d; its 1 document is retired over the next sync passes", + ); + expect(retiredMessage("d", true, 3, false, "namespace")).toBe( "Retired d; its 3 documents are retired once sync is on for this deployment", ); }); diff --git a/frontend/src/lib/knowledgeSources.ts b/frontend/src/lib/knowledgeSources.ts index 8e2b6ef343..1213932984 100644 --- a/frontend/src/lib/knowledgeSources.ts +++ b/frontend/src/lib/knowledgeSources.ts @@ -554,9 +554,19 @@ export function sourceFormFingerprint(f: SourceForm): string { return JSON.stringify(sourceAddBody("", "", f, true)); } -/** The toast after Retire: whether the documents stay, how many go, and that the - * retirement waits for the sync worker when it is off. */ -export function retiredMessage(name: string, deleteDocuments: boolean, pending: number, enabled: boolean): string { +/** Mirrored counts are withheld; only namespace access can report a document count. */ +export function retiredMessage( + name: string, + deleteDocuments: boolean, + pending: number, + enabled: boolean, + access: KnowledgeSource["access"], +): string { + if (access === "mirrored") + return enabled + ? `Retired ${name}; its mirrored copies are queued for retirement over the next sync passes` + : `Retired ${name}; its mirrored copies are queued for retirement once sync is on for this deployment`; + if (access !== "namespace") return `Retired ${name}; document retirement follows the source's stored access policy`; if (!deleteDocuments) return `Retired ${name}; its documents stay`; if (pending === 0) return `Retired ${name}; it has no documents left to retire`; const docs = pending === 1 ? "1 document is" : `${pending} documents are`; diff --git a/frontend/src/lib/state.ts b/frontend/src/lib/state.ts index 3c36d1cda8..3400d93377 100644 --- a/frontend/src/lib/state.ts +++ b/frontend/src/lib/state.ts @@ -96,7 +96,7 @@ const VIEW_QUERY_KEYS: Partial> = { tasks: ["inbox", "task"], // Settings carries its open tab (`?tab=memory|system`). setup: ["tab"], - "connected-accounts": ["kind"], + "connected-accounts": ["kind", "from"], // The object open on a page rides its URL, so a deep link, a reload and Back reopen it: // the memory in the drawer, the document in the preview, the thread in the replay. memories: ["memory"], diff --git a/frontend/src/views/ConnectedAccounts.test.tsx b/frontend/src/views/ConnectedAccounts.test.tsx index 2ceed72d7f..6bc5a61e99 100644 --- a/frontend/src/views/ConnectedAccounts.test.tsx +++ b/frontend/src/views/ConnectedAccounts.test.tsx @@ -81,6 +81,24 @@ describe("Connected accounts", () => { expect(useStore.getState().view).toBe("setup"); }); + it("keeps source return guidance through boot while dropping unrelated query keys", async () => { + useStore.setState({ identity: { org: "acme", namespace: "team", agent: "loom-ui" } }); + history.replaceState( + null, + "", + "/settings/connected-accounts?kind=notion&from=source&state=drop&code=drop&return_url=https%3A%2F%2Felsewhere.example", + ); + act(() => useStore.getState().bootView("connected-accounts")); + expect(window.location.pathname + window.location.search).toBe( + "/settings/connected-accounts?namespace=team&kind=notion&from=source", + ); + render(); + await screen.findByText("reader@example.com"); + expect(screen.getByText("After connecting, return to the tab where you are adding a source.")).toBeVisible(); + await waitFor(() => expect(screen.getByRole("heading", { name: "Notion" })).toHaveFocus()); + expect(api.startConnection).not.toHaveBeenCalled(); + }); + it("shows three statuses and only supplied emails, without scopes or credential controls", async () => { const { container } = render(); await screen.findByText("reader@example.com"); diff --git a/frontend/src/views/SourcesList.test.tsx b/frontend/src/views/SourcesList.test.tsx index c86afc20ab..4627f70927 100644 --- a/frontend/src/views/SourcesList.test.tsx +++ b/frontend/src/views/SourcesList.test.tsx @@ -222,6 +222,45 @@ describe("SourcesList", () => { expect(screen.queryByLabelText(/password|integration token|service account key/i)).not.toBeInTheDocument(); }); + it.each(["connected", "shared"] as const)("uses stored mirrored policy for %s source retirement", async (mode) => { + writer(); + sources.listKnowledgeSources.mockResolvedValue({ + enabled: true, + sources: [ + { + ...connectedSource("own-pages", "alice"), + credential_mode: mode, + access: "mirrored", + }, + ], + }); + render( + , + ); + await userEvent.click(await screen.findByRole("button", { name: "Actions for own-pages" })); + await userEvent.click(await screen.findByRole("menuitem", { name: "Retire" })); + expect(await screen.findByText(/Mirrored copies are always queued for retirement/)).toBeInTheDocument(); + expect(screen.queryByRole("checkbox")).not.toBeInTheDocument(); + }); + + it("keeps the namespace retirement choice for a connected source", async () => { + writer(); + sources.listKnowledgeSources.mockResolvedValue({ + enabled: true, + sources: [ + { + ...connectedSource("own-pages", "alice"), + access: "namespace", + }, + ], + }); + render(); + await userEvent.click(await screen.findByRole("button", { name: "Actions for own-pages" })); + await userEvent.click(await screen.findByRole("menuitem", { name: "Retire" })); + expect(await screen.findByRole("checkbox", { name: /Also retire the documents/ })).not.toBeChecked(); + expect(screen.queryByText(/Mirrored copies are always/)).not.toBeInTheDocument(); + }); + it("preserves an admin's shared-source credential rotation without offering another person's account", async () => { useStore.setState({ whoami: { authenticated: true, subject: "alice", org: "acme", roles: ["admin"] } }); sources.listKnowledgeSources.mockResolvedValue(LISTED); diff --git a/frontend/src/views/SourcesList.tsx b/frontend/src/views/SourcesList.tsx index 3f1330377b..a6a7c3968d 100644 --- a/frontend/src/views/SourcesList.tsx +++ b/frontend/src/views/SourcesList.tsx @@ -407,6 +407,7 @@ function ScopedSourcesList({ namespace={namespace} name={acting.source.name} enabled={state.kind === "ready" && state.enabled} + access={acting.source.access} onOpenChange={close} onRetired={reload} /> From f92692fc04aad7a28469154711a948a218bad1d3 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 23:14:47 +0000 Subject: [PATCH 6/6] Keep source refresh focused while its read is busy Signed-off-by: zlareb1 --- frontend/src/views/SourcesList.test.tsx | 56 +++++++++++++++++++++++++ frontend/src/views/SourcesList.tsx | 24 ++++++++--- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/frontend/src/views/SourcesList.test.tsx b/frontend/src/views/SourcesList.test.tsx index 4627f70927..150ddc2050 100644 --- a/frontend/src/views/SourcesList.test.tsx +++ b/frontend/src/views/SourcesList.test.tsx @@ -121,6 +121,62 @@ describe("SourcesList", () => { expect(screen.getByRole("button", { name: "Refresh sources" })).toBeEnabled(); }); + it("keeps refresh focused and rows visible while ignoring repeated presses until the read settles", async () => { + let finish!: (value: KnowledgeSourceList) => void; + sources.listKnowledgeSources + .mockResolvedValueOnce(LISTED) + .mockImplementationOnce( + () => + new Promise((resolve) => { + finish = resolve; + }), + ) + .mockResolvedValueOnce(LISTED); + render(); + const refresh = await screen.findByRole("button", { name: "Refresh sources" }); + await userEvent.click(refresh); + expect(refresh).toHaveFocus(); + expect(refresh).toHaveAttribute("aria-busy", "true"); + expect(refresh).toBeEnabled(); + expect(screen.getByRole("list", { name: "Document sources" })).toHaveAttribute("aria-busy", "true"); + expect(screen.getByRole("button", { name: "design-docs" })).toBeInTheDocument(); + await userEvent.click(refresh); + expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(2); + await act(async () => { + finish(LISTED); + }); + expect(refresh).not.toHaveAttribute("aria-busy"); + expect(screen.getByRole("list", { name: "Document sources" })).not.toHaveAttribute("aria-busy"); + expect(refresh).toHaveFocus(); + await userEvent.click(refresh); + await waitFor(() => expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(3)); + }); + + it("clears refresh busy state after a failed read and allows recovery", async () => { + let fail!: (error: unknown) => void; + sources.listKnowledgeSources + .mockResolvedValueOnce(LISTED) + .mockImplementationOnce( + () => + new Promise((_resolve, reject) => { + fail = reject; + }), + ) + .mockResolvedValueOnce(LISTED); + render(); + await userEvent.click(await screen.findByRole("button", { name: "Refresh sources" })); + expect(screen.getByRole("button", { name: "Refresh sources" })).toHaveAttribute("aria-busy", "true"); + await act(async () => { + fail(failure()); + }); + const alert = await screen.findByRole("alert"); + expect(alert).toHaveTextContent("the store did not answer"); + await userEvent.click(within(alert).getByRole("button", { name: "Retry" })); + expect(await screen.findByRole("button", { name: "Refresh sources" })).not.toHaveAttribute("aria-busy"); + expect(screen.getByRole("list", { name: "Document sources" })).not.toHaveAttribute("aria-busy"); + expect(sources.listKnowledgeSources).toHaveBeenCalledTimes(3); + }); + it("uses server-proven ownership when the signed-in subject is an alias", async () => { useStore.setState({ whoami: { authenticated: true, subject: "alice-alias", org: "acme", roles: ["writer"] } }); sources.listKnowledgeSources.mockResolvedValue({ enabled: true, sources: [connectedSource("own-alias", "alice")] }); diff --git a/frontend/src/views/SourcesList.tsx b/frontend/src/views/SourcesList.tsx index a6a7c3968d..6342fe0789 100644 --- a/frontend/src/views/SourcesList.tsx +++ b/frontend/src/views/SourcesList.tsx @@ -31,6 +31,7 @@ import { isOpAbsent } from "../lib/trackerSources"; import { PHONE_QUERY, useMedia } from "../lib/useMedia"; import { Skeleton } from "../components/Skeleton"; import { IconButton } from "../components/IconButton"; +import { BusyButton } from "../components/ui/BusyButton"; import { toast } from "../components/Toast"; import { ListTime, LoadingSkeleton, TipTarget } from "../components/ui"; import { ListState } from "../components/ui/ListState"; @@ -184,6 +185,7 @@ function ScopedSourcesList({ if (open) heading.current?.focus(); }, [open]); const [nonce, setNonce] = useState(0); + const [reading, setReading] = useState(true); const [reloadingItems, setReloadingItems] = useState(false); const [adding, setAdding] = useState(false); const [acting, setActing] = useState<{ verb: Verb; source: KnowledgeSource } | null>(null); @@ -191,7 +193,10 @@ function ScopedSourcesList({ const [connectionKind, setConnectionKind] = useState(null); const [details, setDetails] = useState(null); const id = { org, namespace }; - const reload = () => setNonce((n) => n + 1); + const reload = () => { + setReading(true); + setNonce((n) => n + 1); + }; useAbortableEffect( async (signal, live) => { @@ -205,7 +210,10 @@ function ScopedSourcesList({ else if (e instanceof ApiError && (e.status === 403 || e.status === 401)) setState({ kind: "refused" }); else setState({ kind: "error", error: e }); } finally { - if (live()) setReloadingItems(false); + if (live()) { + setReading(false); + setReloadingItems(false); + } } }, [org, namespace, nonce], @@ -282,7 +290,7 @@ function ScopedSourcesList({

)} {state.kind === "ready" && state.sources.length > 0 && ( -
    +
      {state.sources.map((s) => ( setDetails(s.name)} /> ))} @@ -331,9 +339,15 @@ function ScopedSourcesList({ )} {state.kind === "ready" && ( - + )} {acting && (