Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
feat(api): store Alexandria feedback in dedicated tables (#4722)
Nicolas04034c3fc9c8
diff --git a/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts b/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.tsindex 756f49d88..b902f12ca 100644--- a/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts+++ b/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts@@ -1,10 +1,38 @@import request from "supertest";-import { eq } from "drizzle-orm";+import { asc, eq } from "drizzle-orm";import { describeIf, TEST_API_URL, TEST_PRODUCTION } from "../lib";import { idmux, Identity } from "./lib";import { db } from "../../../db/connection";import * as schema from "../../../db/schema";+const feedbackRows = async (feedbackId: string) => {+ const [[parent], providers, capabilities] = await Promise.all([+ db+ .select()+ .from(schema.alexandria_feedback)+ .where(eq(schema.alexandria_feedback.id, feedbackId)),+ db+ .select()+ .from(schema.alexandria_feedback_providers)+ .where(eq(schema.alexandria_feedback_providers.feedback_id, feedbackId))+ .orderBy(asc(schema.alexandria_feedback_providers.position)),+ db+ .select()+ .from(schema.alexandria_feedback_capabilities)+ .where(+ eq(schema.alexandria_feedback_capabilities.feedback_id, feedbackId),+ )+ .orderBy(asc(schema.alexandria_feedback_capabilities.position)),+ ]);+ return { parent, providers, capabilities };+};++// Child rows cascade from the parent delete.+const deleteFeedback = (feedbackId: string) =>+ db+ .delete(schema.alexandria_feedback)+ .where(eq(schema.alexandria_feedback.id, feedbackId));+describeIf(TEST_PRODUCTION)("Alexandria session feedback", () => {let identity: Identity;const body = {@@ -33,30 +61,25 @@ describeIf(TEST_PRODUCTION)("Alexandria session feedback", () => {expect(response.body).toMatchObject({ success: true, creditsRefunded: 0 });expect(response.body.feedbackId).toEqual(expect.any(String));try {- const [row] = await db- .select()- .from(schema.search_feedback)- .where(eq(schema.search_feedback.id, response.body.feedbackId));- expect(row).toMatchObject({- endpoint: "alexandria",+ const { parent, providers, capabilities } = await feedbackRows(+ response.body.feedbackId,+ );+ expect(parent).toMatchObject({team_id: identity.teamId,- job_id: null,- search_id: null,- request_id: null,- job_status: null,- overall_rating: "partial",- comment: body.rationale,- credits_refunded: 0,- metadata: {- endpoint: "alexandria",- requestedWebsite: body.requestedWebsite,- rationale: body.rationale,- },+ api_version: "v2",+ rating: "partial",+ requested_url: body.requestedWebsite.url,+ requested_host: "sam.gov",+ requested_functionality: body.requestedWebsite.requestedFunctionality,+ rationale: body.rationale,+ origin: "api",+ integration: null,+ schema_version: 2,});+ expect(providers).toEqual([]);+ expect(capabilities).toEqual([]);} finally {- await db- .delete(schema.search_feedback)- .where(eq(schema.search_feedback.id, response.body.feedbackId));+ await deleteFeedback(response.body.feedbackId);}});@@ -76,6 +99,12 @@ describeIf(TEST_PRODUCTION)("Alexandria session feedback", () => {why: "Need source documents to compare contract requirements.",requestedFunctionality: "Return all attachment URLs for a contract ID.",},+ {+ name: "contracts",+ provider: "sam.gov",+ issue: "missing_capability",+ why: "The provider has no attachment download capability.",+ },{name: "contracts",provider: "sam.gov",@@ -90,25 +119,38 @@ describeIf(TEST_PRODUCTION)("Alexandria session feedback", () => {integration: "cli",});expect(response.statusCode).toBe(200);+ const feedbackId = response.body.feedbackId;try {- const [row] = await db- .select()- .from(schema.search_feedback)- .where(eq(schema.search_feedback.id, response.body.feedbackId));- expect(row.metadata).toEqual({- schemaVersion: 1,- endpoint: "alexandria",- requestedWebsite: body.requestedWebsite,+ const { parent, providers, capabilities } =+ await feedbackRows(feedbackId);+ expect(parent).toMatchObject({rationale: body.rationale,- providerFeedback,- capabilityFeedback,+ integration: "cli",});- expect(row.comment).toBe(body.rationale);- expect(row.integration).toBe("cli");+ expect(providers).toEqual(+ providerFeedback.map((entry, position) =>+ expect.objectContaining({+ feedback_id: feedbackId,+ team_id: identity.teamId,+ position,+ ...entry,+ }),+ ),+ );+ expect(capabilities).toEqual(+ capabilityFeedback.map(+ ({ requestedFunctionality, ...entry }, position) =>+ expect.objectContaining({+ feedback_id: feedbackId,+ team_id: identity.teamId,+ position,+ requested_functionality: requestedFunctionality ?? null,+ ...entry,+ }),+ ),+ );} finally {- await db- .delete(schema.search_feedback)- .where(eq(schema.search_feedback.id, response.body.feedbackId));+ await deleteFeedback(feedbackId);}});diff --git a/apps/api/src/controllers/v2/feedback/alexandria-schema.ts b/apps/api/src/controllers/v2/feedback/alexandria-schema.tsindex fe0226969..5f0a5ae9e 100644--- a/apps/api/src/controllers/v2/feedback/alexandria-schema.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria-schema.ts@@ -19,6 +19,7 @@ const capabilityFeedback = zprovider: name,issue: z.enum(["new_capability_request",+ "missing_capability","insufficient_functionality","incorrect_result","execution_error",@@ -67,6 +68,3 @@ export const alexandriaFeedbackSchema = zexport type AlexandriaFeedbackRequest = z.infer<typeof alexandriaFeedbackSchema>;-export type AlexandriaFeedbackRequestInput = z.input<- typeof alexandriaFeedbackSchema->;diff --git a/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts b/apps/api/src/controllers/v2/feedback/alexandria.integration.test.tsindex a2658deb7..be8a3efdb 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts@@ -14,9 +14,50 @@ vi.mock("./record", () => ({},}));-// Opt in with a local database containing the feedback migrations, including-// the Alexandria endpoint constraint. Each run owns an isolated schema.+// Opt in with a local PostgreSQL database. Each run owns an isolated schema+// holding a copy of the Alexandria feedback tables, including constraints.const databaseUrl = process.env.ALEXANDRIA_FEEDBACK_TEST_DATABASE_URL;+const ddl = `+CREATE TABLE alexandria_feedback (+ id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,+ team_id uuid NOT NULL,+ api_key_id bigint,+ api_version text NOT NULL DEFAULT 'v2',+ rating text NOT NULL CHECK (rating IN ('good', 'partial', 'bad')),+ requested_url text NOT NULL CHECK (char_length(requested_url) <= 2048),+ requested_host text GENERATED ALWAYS AS (lower(substring(requested_url from '^[A-Za-z][A-Za-z0-9+.-]*://(?:[^@/?#]*@)?([^/?#:]+)'))) STORED,+ requested_functionality text NOT NULL,+ rationale text NOT NULL,+ origin text,+ integration text,+ schema_version integer NOT NULL DEFAULT 2,+ created_at timestamptz NOT NULL DEFAULT now()+);+CREATE TABLE alexandria_feedback_providers (+ id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,+ feedback_id uuid NOT NULL REFERENCES alexandria_feedback (id) ON DELETE CASCADE,+ team_id uuid NOT NULL,+ position smallint NOT NULL,+ name text NOT NULL,+ issue text NOT NULL CHECK (issue IN ('missing_provider', 'insufficient_coverage', 'provider_unavailable', 'other')),+ why text NOT NULL,+ created_at timestamptz NOT NULL DEFAULT now(),+ UNIQUE (feedback_id, position)+);+CREATE TABLE alexandria_feedback_capabilities (+ id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,+ feedback_id uuid NOT NULL REFERENCES alexandria_feedback (id) ON DELETE CASCADE,+ team_id uuid NOT NULL,+ position smallint NOT NULL,+ name text NOT NULL,+ provider text NOT NULL,+ issue text NOT NULL CHECK (issue IN ('new_capability_request', 'missing_capability', 'insufficient_functionality', 'incorrect_result', 'execution_error', 'other')),+ why text NOT NULL,+ requested_functionality text CHECK (issue <> 'new_capability_request' OR requested_functionality IS NOT NULL),+ created_at timestamptz NOT NULL DEFAULT now(),+ UNIQUE (feedback_id, position)+);+`;const suite = databaseUrl ? describe : describe.skip;suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {const schemaName = `alexandria_feedback_${randomUUID().replaceAll("-", "")}`;@@ -52,9 +93,7 @@ suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {connectionString: databaseUrl,options: `-c search_path=${schemaName},public`,});- await pool.query(- "CREATE TABLE search_feedback (LIKE public.search_feedback INCLUDING ALL)",- );+ await pool.query(ddl);fixture.db = drizzle({ client: pool });config = (await import("../../../config.js")).config;originalAuthentication = config.USE_DB_AUTHENTICATION;@@ -88,34 +127,33 @@ suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {expect(second.status).toBe(200);expect(second.body.feedbackId).not.toBe(first.body.feedbackId);const { rows } = await pool.query(- "SELECT * FROM search_feedback WHERE team_id = $1",+ "SELECT * FROM alexandria_feedback WHERE team_id = $1",[teamId],);expect(rows).toHaveLength(2);for (const row of rows) {expect(row).toMatchObject({- endpoint: "alexandria",team_id: teamId,- overall_rating: "bad",- comment: minimal.rationale,- job_id: null,- search_id: null,- request_id: null,- job_status: null,- credits_refunded: 0,- credits_billed: 0,- refund_policy: null,- metadata: {- schemaVersion: 1,- endpoint: "alexandria",- requestedWebsite: minimal.requestedWebsite,- rationale: minimal.rationale,- },+ api_key_id: "42",+ api_version: "v2",+ rating: "bad",+ requested_url: minimal.requestedWebsite.url,+ requested_host: "sam.gov",+ requested_functionality:+ minimal.requestedWebsite.requestedFunctionality,+ rationale: minimal.rationale,+ origin: "api",+ integration: null,+ schema_version: 2,});}+ const children = await pool.query(+ "SELECT (SELECT count(*) FROM alexandria_feedback_providers) AS providers, (SELECT count(*) FROM alexandria_feedback_capabilities) AS capabilities",+ );+ expect(children.rows[0]).toEqual({ providers: "0", capabilities: "0" });});- it("round-trips website requirements and provider/capability feedback in existing columns", async () => {+ it("round-trips provider and capability feedback into ordered child rows", async () => {const providerFeedback = [{name: "sam.gov",@@ -132,6 +170,12 @@ suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {requestedFunctionality:"Given a contract ID, return all attachment URLs and document text.",},+ {+ name: "contracts",+ provider: "sam.gov",+ issue: "missing_capability",+ why: "The provider has no contract attachment capability.",+ },{name: "contracts",provider: "sam.gov",@@ -143,30 +187,50 @@ suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {...minimal,providerFeedback,capabilityFeedback,+ integration: "cli",});expect(response.status).toBe(200);- const { rows } = await pool.query(- "SELECT metadata, comment, overall_rating, job_id, credits_refunded FROM search_feedback WHERE id = $1",- [response.body.feedbackId],+ const feedbackId = response.body.feedbackId;+ const parent = await pool.query(+ "SELECT rating, rationale, integration FROM alexandria_feedback WHERE id = $1",+ [feedbackId],);- expect(rows[0]).toEqual({- overall_rating: minimal.rating,- comment: minimal.rationale,- job_id: null,- credits_refunded: 0,- metadata: {- schemaVersion: 1,- endpoint: "alexandria",- requestedWebsite: minimal.requestedWebsite,+ expect(parent.rows).toEqual([+ {+ rating: minimal.rating,rationale: minimal.rationale,- providerFeedback,- capabilityFeedback,+ integration: "cli",},- });+ ]);+ const providers = await pool.query(+ "SELECT team_id, position, name, issue, why FROM alexandria_feedback_providers WHERE feedback_id = $1 ORDER BY position",+ [feedbackId],+ );+ expect(providers.rows).toEqual(+ providerFeedback.map((entry, position) => ({+ team_id: teamId,+ position,+ ...entry,+ })),+ );+ const capabilities = await pool.query(+ "SELECT team_id, position, name, provider, issue, why, requested_functionality FROM alexandria_feedback_capabilities WHERE feedback_id = $1 ORDER BY position",+ [feedbackId],+ );+ expect(capabilities.rows).toEqual(+ capabilityFeedback.map(+ ({ requestedFunctionality, ...entry }, position) => ({+ team_id: teamId,+ position,+ requested_functionality: requestedFunctionality ?? null,+ ...entry,+ }),+ ),+ );});it("rejects invalid feedback before persistence", async () => {- const before = await pool.query("SELECT count(*) FROM search_feedback");+ const before = await pool.query("SELECT count(*) FROM alexandria_feedback");const response = await submit({...minimal,capabilityFeedback: [@@ -179,7 +243,25 @@ suite("Alexandria feedback HTTP and PostgreSQL persistence", () => {],});expect(response.status).toBe(400);- const after = await pool.query("SELECT count(*) FROM search_feedback");+ const after = await pool.query("SELECT count(*) FROM alexandria_feedback");expect(after.rows).toEqual(before.rows);});++ it("deletes child rows with their parent", async () => {+ const response = await submit({+ ...minimal,+ providerFeedback: [+ { name: "sam.gov", issue: "other", why: "Slow responses." },+ ],+ });+ expect(response.status).toBe(200);+ await pool.query("DELETE FROM alexandria_feedback WHERE id = $1", [+ response.body.feedbackId,+ ]);+ const orphans = await pool.query(+ "SELECT count(*) FROM alexandria_feedback_providers WHERE feedback_id = $1",+ [response.body.feedbackId],+ );+ expect(orphans.rows[0].count).toBe("0");+ });});diff --git a/apps/api/src/controllers/v2/feedback/alexandria.test.ts b/apps/api/src/controllers/v2/feedback/alexandria.test.tsindex 204c91b60..82a7072d7 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.test.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.test.ts@@ -1,16 +1,32 @@import express from "express";import request from "supertest";+import { getTableName, type Table } from "drizzle-orm";import { DrizzleQueryError } from "drizzle-orm/errors";import { alexandriaFeedbackSchema } from "./alexandria-schema";const mocks = vi.hoisted(() => ({- values: vi.fn(),+ transaction: vi.fn(),+ // Receives (tableName, rows[]) for every insert issued inside the transaction.+ insert: vi.fn(),recordEndpointFeedback: vi.fn(),logError: vi.fn(),}));vi.mock("../../../db/connection", () => ({- db: { insert: () => ({ values: mocks.values }) },+ db: { transaction: mocks.transaction },}));+const tx = {+ insert: (table: Table) => ({+ values: (rows: object | object[]) =>+ mocks.insert(getTableName(table), Array.isArray(rows) ? rows : [rows]),+ }),+};+const inserted = (table: string) =>+ mocks.insert.mock.calls+ .filter(([name]) => name === table)+ .flatMap(([, rows]) => rows as Record<string, unknown>[]);+const parentRows = () => inserted("alexandria_feedback");+const providerRows = () => inserted("alexandria_feedback_providers");+const capabilityRows = () => inserted("alexandria_feedback_capabilities");vi.mock("../../../lib/logger", () => ({ logger: { error: mocks.logError } }));vi.mock("./record", () => ({recordEndpointFeedback: mocks.recordEndpointFeedback,@@ -54,7 +70,10 @@ beforeEach(() => {config.USE_DB_AUTHENTICATION = true;authTeam = teamId;flags = {};- mocks.values.mockResolvedValue(undefined);+ mocks.transaction.mockImplementation(+ async (run: (tx: unknown) => Promise<void>) => run(tx),+ );+ mocks.insert.mockResolvedValue(undefined);});afterAll(() => {config.USE_DB_AUTHENTICATION = originalDbAuthentication;@@ -71,29 +90,25 @@ it.each(["good", "partial", "bad"])(creditsRefunded: 0,});expect(mocks.recordEndpointFeedback).not.toHaveBeenCalled();- expect(mocks.values).toHaveBeenCalledWith(- expect.objectContaining({+ expect(mocks.transaction).toHaveBeenCalledTimes(1);+ expect(parentRows()).toEqual([+ {id: response.body.feedbackId,- endpoint: "alexandria",team_id: teamId,api_key_id: 42,- overall_rating: rating,- comment: minimal.rationale,- job_id: null,- search_id: null,- request_id: null,- job_status: null,- credits_billed: 0,- credits_refunded: 0,- refund_policy: null,- metadata: {- schemaVersion: 1,- endpoint: "alexandria",- requestedWebsite: minimal.requestedWebsite,- rationale: minimal.rationale,- },- }),- );+ api_version: "v2",+ rating,+ requested_url: minimal.requestedWebsite.url,+ requested_functionality:+ minimal.requestedWebsite.requestedFunctionality,+ rationale: minimal.rationale,+ origin: "api",+ integration: null,+ schema_version: 2,+ },+ ]);+ expect(providerRows()).toEqual([]);+ expect(capabilityRows()).toEqual([]);},);@@ -103,7 +118,7 @@ it.each(["endpoint", "rating", "requestedWebsite", "rationale"])(const response = await submit({ ...minimal, [field]: undefined });expect(response.status).toBe(400);expect(response.body.feedbackErrorCode).toBe("INVALID_BODY");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();},);@@ -115,7 +130,7 @@ it.each(["url", "requestedFunctionality"])(requestedWebsite: { ...minimal.requestedWebsite, [field]: undefined },});expect(response.status).toBe(400);- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();},);@@ -127,7 +142,7 @@ it("rejects the unpublished categories discriminator", async () => {});expect(response.status).toBe(400);expect(response.body.feedbackErrorCode).toBe("INVALID_BODY");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();expect(mocks.recordEndpointFeedback).not.toHaveBeenCalled();});@@ -141,7 +156,7 @@ it.each(["search", "scrape", "parse", "map"])(});expect(response.status).toBe(400);expect(response.body.feedbackErrorCode).toBe("INVALID_BODY");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();expect(mocks.recordEndpointFeedback).not.toHaveBeenCalled();},);@@ -196,6 +211,11 @@ it.each([{ name: "sam.gov", issue: "execution_error", why: "Request failed" },],},+ {+ providerFeedback: [+ { name: "sam.gov", issue: "missing_capability", why: "No such tool" },+ ],+ },{ providerFeedback: [{ name: "sam.gov", issue: "other", why: " " }] },{providerFeedback: [@@ -251,7 +271,7 @@ it.each([const response = await submit({ ...minimal, ...fields });expect(response.status).toBe(400);expect(response.body.feedbackErrorCode).toBe("INVALID_BODY");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();expect(mocks.recordEndpointFeedback).not.toHaveBeenCalled();});@@ -283,11 +303,48 @@ it("preserves website requirements and provider/capability feedback for the sess};const response = await submit({ ...minimal, ...evidence });expect(response.status).toBe(200);- expect(mocks.values).toHaveBeenCalledWith(+ const feedbackId = response.body.feedbackId;+ expect(parentRows()).toEqual([expect.objectContaining({- metadata: expect.objectContaining(evidence),+ id: feedbackId,+ requested_url: minimal.requestedWebsite.url,+ requested_functionality: minimal.requestedWebsite.requestedFunctionality,+ rationale: minimal.rationale,}),- );+ ]);+ expect(providerRows()).toEqual([+ {+ feedback_id: feedbackId,+ team_id: teamId,+ position: 0,+ name: "sam.gov",+ issue: "insufficient_coverage",+ why: evidence.providerFeedback[0].why,+ },+ ]);+ expect(capabilityRows()).toEqual([+ {+ feedback_id: feedbackId,+ team_id: teamId,+ position: 0,+ name: "download-attachments",+ provider: "sam.gov",+ issue: "new_capability_request",+ why: evidence.capabilityFeedback[0].why,+ requested_functionality:+ evidence.capabilityFeedback[0].requestedFunctionality,+ },+ {+ feedback_id: feedbackId,+ team_id: teamId,+ position: 1,+ name: "contracts",+ provider: "sam.gov",+ issue: "execution_error",+ why: evidence.capabilityFeedback[1].why,+ requested_functionality: null,+ },+ ]);});it.each([@@ -302,15 +359,15 @@ it.each([{ name: "sam.gov", issue, why: "Need complete contract data." },];expect((await submit({ ...minimal, providerFeedback })).status).toBe(200);- expect(mocks.values).toHaveBeenCalledWith(- expect.objectContaining({- metadata: expect.objectContaining({ providerFeedback }),- }),- );+ expect(providerRows()).toEqual([+ expect.objectContaining({ position: 0, ...providerFeedback[0] }),+ ]);+ expect(capabilityRows()).toEqual([]);},);it.each([+ "missing_capability","insufficient_functionality","incorrect_result","execution_error",@@ -331,14 +388,22 @@ it.each([...(requestedFunctionality ? { requestedFunctionality } : {}),},];+ mocks.insert.mockClear();expect((await submit({ ...minimal, capabilityFeedback })).status).toBe(200,);- expect(mocks.values).toHaveBeenLastCalledWith(- expect.objectContaining({- metadata: expect.objectContaining({ capabilityFeedback }),- }),- );+ expect(capabilityRows()).toEqual([+ {+ feedback_id: parentRows()[0].id,+ team_id: teamId,+ position: 0,+ name: "contracts",+ provider: "sam.gov",+ issue,+ why: capabilityFeedback[0].why,+ requested_functionality: requestedFunctionality ?? null,+ },+ ]);}},);@@ -359,7 +424,7 @@ it.each([undefined, "", " "])(],};expect((await submit(payload)).status).toBe(400);- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();const parsed = alexandriaFeedbackSchema.safeParse(payload);expect(parsed.success).toBe(false);if (!parsed.success) {@@ -391,9 +456,9 @@ it.each([["_" + "x".repeat(99), "_" + "x".repeat(99)],])("accepts and normalizes integration %j", async (integration, expected) => {expect((await submit({ ...minimal, integration })).status).toBe(200);- expect(mocks.values).toHaveBeenCalledWith(+ expect(parentRows()).toEqual([expect.objectContaining({ integration: expected }),- );+ ]);});it.each(["unsupported", " ", "_" + "x".repeat(100)])(@@ -402,7 +467,7 @@ it.each(["unsupported", " ", "_" + "x".repeat(100)])(const response = await submit({ ...minimal, integration });expect(response.status).toBe(400);expect(response.body.feedbackErrorCode).toBe("INVALID_BODY");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();},);@@ -416,13 +481,13 @@ it.each(["providerFeedback", "capabilityFeedback"])(...(field === "capabilityFeedback" ? { provider: "sam.gov" } : {}),};for (const length of [20, 21]) {- mocks.values.mockClear();+ mocks.transaction.mockClear();const response = await submit({...minimal,[field]: Array.from({ length }, () => ({ ...entry })),});expect(response.status).toBe(length === 20 ? 200 : 400);- expect(mocks.values).toHaveBeenCalledTimes(length === 20 ? 1 : 0);+ expect(mocks.transaction).toHaveBeenCalledTimes(length === 20 ? 1 : 0);}},);@@ -442,13 +507,13 @@ it.each([...(array === "capabilityFeedback" ? { provider: "sam.gov" } : {}),};for (const length of [limit, limit + 1]) {- mocks.values.mockClear();+ mocks.transaction.mockClear();const response = await submit({...minimal,[array]: [{ ...entry, [field]: "x".repeat(length) }],});expect(response.status).toBe(length === limit ? 200 : 400);- expect(mocks.values).toHaveBeenCalledTimes(length === limit ? 1 : 0);+ expect(mocks.transaction).toHaveBeenCalledTimes(length === limit ? 1 : 0);}});@@ -478,7 +543,7 @@ it("bounds the complete UTF-8 evidence payload", async () => {})),});expect(response.status).toBe(400);- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();});it("applies the payload limit after normalizing feedback", async () => {@@ -487,9 +552,9 @@ it("applies the payload limit after normalizing feedback", async () => {rationale: " ".repeat(9 * 1024) + minimal.rationale,});expect(response.status).toBe(200);- expect(mocks.values).toHaveBeenCalledWith(- expect.objectContaining({ comment: minimal.rationale }),- );+ expect(parentRows()).toEqual([+ expect.objectContaining({ rationale: minimal.rationale }),+ ]);});it.each([@@ -503,7 +568,7 @@ it.each([const response = await submit(minimal);expect(response.status).toBe(200);expect(response.body.feedbackId).toBe("00000000-0000-0000-0000-000000000000");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();});it("honors team opt-out", async () => {@@ -511,7 +576,7 @@ it("honors team opt-out", async () => {const response = await submit(minimal);expect(response.status).toBe(403);expect(response.body.feedbackErrorCode).toBe("TEAM_OPTED_OUT");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();});it.each(["preview", "preview_example", "preview_keyless_example"])(@@ -521,7 +586,7 @@ it.each(["preview", "preview_example", "preview_keyless_example"])(const response = await submit(minimal);expect(response.status).toBe(403);expect(response.body.feedbackErrorCode).toBe("PREVIEW_TEAM_NOT_ALLOWED");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();},);@@ -530,7 +595,7 @@ it("rejects deployments without database authentication", async () => {const response = await submit(minimal);expect(response.status).toBe(503);expect(response.body.feedbackErrorCode).toBe("DB_DISABLED");- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();});it.each([@@ -538,7 +603,7 @@ it.each([[Object.assign(new Error("sensitive detail"), { code: "23514" }), "23514"],[new DrizzleQueryError(- "INSERT INTO search_feedback VALUES ($1)",+ "INSERT INTO alexandria_feedback VALUES ($1)",["sensitive feedback payload"],Object.assign(new Error("sensitive database detail"), { code: "23514" }),),@@ -548,7 +613,7 @@ it.each([[{ code: { detail: "sensitive feedback payload" } }, null],[null, null],])("logs only SQLSTATE on persistence failure %#", async (error, errorCode) => {- mocks.values.mockRejectedValueOnce(error);+ mocks.insert.mockRejectedValueOnce(error);const response = await submit(minimal);expect(response.status).toBe(500);expect(response.body).toEqual({@@ -584,6 +649,6 @@ it.each(["search", "scrape", "parse", "map"])(expect.anything(),expect.objectContaining({ endpoint, jobId }),);- expect(mocks.values).not.toHaveBeenCalled();+ expect(mocks.transaction).not.toHaveBeenCalled();},);diff --git a/apps/api/src/controllers/v2/feedback/alexandria.ts b/apps/api/src/controllers/v2/feedback/alexandria.tsindex 317a3b097..9b10bc772 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.ts@@ -64,26 +64,51 @@ export async function recordAlexandriaFeedback(}const feedbackId = uuidv7();- const { rating, origin, integration, ...metadata } = body;- try {- await db.insert(schema.search_feedback).values({- id: feedbackId,- endpoint: "alexandria",+ const providerRows = (body.providerFeedback ?? []).map((entry, position) => ({+ feedback_id: feedbackId,+ team_id: teamId,+ position,+ name: entry.name,+ issue: entry.issue,+ why: entry.why,+ }));+ const capabilityRows = (body.capabilityFeedback ?? []).map(+ (entry, position) => ({+ feedback_id: feedbackId,team_id: teamId,- api_key_id: req.acuc?.api_key_id ?? null,- search_id: null,- job_id: null,- request_id: null,- job_status: null,- api_version: "v2",- overall_rating: rating,- comment: body.rationale,- metadata: { schemaVersion: 1, ...metadata },- origin,- integration: integration ?? null,- credits_billed: 0,- credits_refunded: 0,- refund_policy: null,+ position,+ name: entry.name,+ provider: entry.provider,+ issue: entry.issue,+ why: entry.why,+ requested_functionality: entry.requestedFunctionality ?? null,+ }),+ );+ try {+ await db.transaction(async tx => {+ await tx.insert(schema.alexandria_feedback).values({+ id: feedbackId,+ team_id: teamId,+ api_key_id: req.acuc?.api_key_id ?? null,+ api_version: "v2",+ rating: body.rating,+ requested_url: body.requestedWebsite.url,+ requested_functionality: body.requestedWebsite.requestedFunctionality,+ rationale: body.rationale,+ origin: body.origin,+ integration: body.integration ?? null,+ schema_version: 2,+ });+ if (providerRows.length > 0) {+ await tx+ .insert(schema.alexandria_feedback_providers)+ .values(providerRows);+ }+ if (capabilityRows.length > 0) {+ await tx+ .insert(schema.alexandria_feedback_capabilities)+ .values(capabilityRows);+ }});} catch (error) {// Database errors may embed the submitted payload. Keep feedback contentdiff --git a/apps/api/src/controllers/v2/feedback/feedback-window.test.ts b/apps/api/src/controllers/v2/feedback/feedback-window.test.tsindex 8b2c96d67..489634064 100644--- a/apps/api/src/controllers/v2/feedback/feedback-window.test.ts+++ b/apps/api/src/controllers/v2/feedback/feedback-window.test.ts@@ -20,7 +20,10 @@ vi.mock("./feedback-store", () => ({updateFeedbackRefundDetails: async () => null,}));vi.mock("../../../db/connection", () => ({- db: { insert: () => ({ values: fixture.alexandriaInsert }) },+ db: {+ transaction: async (run: (tx: unknown) => Promise<void>) =>+ run({ insert: () => ({ values: fixture.alexandriaInsert }) }),+ },}));vi.mock("./refund-totals", () => ({sumCreditsRefundedToday: fixture.refundedToday,@@ -194,13 +197,12 @@ it.each(stores)(expect(fixture.lookup).not.toHaveBeenCalled();expect(fixture.insert).not.toHaveBeenCalled();expect(fixture.refund).not.toHaveBeenCalled();- expect(fixture.alexandriaInsert).toHaveBeenCalledWith(+ expect(fixture.alexandriaInsert).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({- endpoint: "alexandria",- search_id: null,- job_id: null,- request_id: null,- credits_refunded: 0,+ id: response.body.feedbackId,+ team_id: teamId,+ rating: "partial",+ requested_url: "https://example.com",}),);},diff --git a/apps/api/src/db/schema/public.ts b/apps/api/src/db/schema/public.tsindex bbf7720d9..badc57f6b 100644--- a/apps/api/src/db/schema/public.ts+++ b/apps/api/src/db/schema/public.ts@@ -1,4 +1,4 @@-import { sql } from "drizzle-orm";+import { sql, type SQL } from "drizzle-orm";import {pgTable,uuid,@@ -641,6 +641,97 @@ export const search_feedback = pgTable("search_feedback", {updated_at: ts("updated_at").notNull().defaultNow(),});+export const alexandria_feedback = pgTable(+ "alexandria_feedback",+ {+ id: uuid("id").notNull().defaultRandom().primaryKey(),+ team_id: uuid("team_id").notNull(),+ api_key_id: bigintNum("api_key_id"),+ api_version: text("api_version").notNull().default("v2"),+ rating: text("rating").notNull(),+ requested_url: text("requested_url").notNull(),+ // Stored generated column; never written by the API.+ requested_host: text("requested_host").generatedAlwaysAs(+ (): SQL =>+ sql`lower(substring(${alexandria_feedback.requested_url} from '^[A-Za-z][A-Za-z0-9+.-]*://(?:[^@/?#]*@)?([^/?#:]+)'))`,+ ),+ requested_functionality: text("requested_functionality").notNull(),+ rationale: text("rationale").notNull(),+ origin: text("origin"),+ integration: text("integration"),+ schema_version: integer("schema_version").notNull().default(2),+ created_at: ts("created_at").notNull().defaultNow(),+ },+ table => [+ check(+ "alexandria_feedback_rating_check",+ sql`${table.rating} IN ('good', 'partial', 'bad')`,+ ),+ check(+ "alexandria_feedback_requested_url_check",+ sql`char_length(${table.requested_url}) <= 2048`,+ ),+ ],+);++export const alexandria_feedback_providers = pgTable(+ "alexandria_feedback_providers",+ {+ id: uuid("id").notNull().defaultRandom().primaryKey(),+ feedback_id: uuid("feedback_id")+ .notNull()+ .references(() => alexandria_feedback.id, { onDelete: "cascade" }),+ team_id: uuid("team_id").notNull(),+ position: smallint("position").notNull(),+ name: text("name").notNull(),+ issue: text("issue").notNull(),+ why: text("why").notNull(),+ created_at: ts("created_at").notNull().defaultNow(),+ },+ table => [+ unique("alexandria_feedback_providers_feedback_id_position_key").on(+ table.feedback_id,+ table.position,+ ),+ check(+ "alexandria_feedback_providers_issue_check",+ sql`${table.issue} IN ('missing_provider', 'insufficient_coverage', 'provider_unavailable', 'other')`,+ ),+ ],+);++export const alexandria_feedback_capabilities = pgTable(+ "alexandria_feedback_capabilities",+ {+ id: uuid("id").notNull().defaultRandom().primaryKey(),+ feedback_id: uuid("feedback_id")+ .notNull()+ .references(() => alexandria_feedback.id, { onDelete: "cascade" }),+ team_id: uuid("team_id").notNull(),+ position: smallint("position").notNull(),+ name: text("name").notNull(),+ provider: text("provider").notNull(),+ issue: text("issue").notNull(),+ why: text("why").notNull(),+ requested_functionality: text("requested_functionality"),+ created_at: ts("created_at").notNull().defaultNow(),+ },+ table => [+ unique("alexandria_feedback_capabilities_feedback_id_position_key").on(+ table.feedback_id,+ table.position,+ ),+ check(+ "alexandria_feedback_capabilities_issue_check",+ sql`${table.issue} IN ('new_capability_request', 'missing_capability', 'insufficient_functionality', 'incorrect_result', 'execution_error', 'other')`,+ ),+ check(+ "alexandria_feedback_capabilities_requested_functionality_check",+ sql`${table.issue} <> 'new_capability_request' OR ${table.requested_functionality} IS NOT NULL`,+ ),+ ],+);+export const searches = pgTable("searches", {id: uuid("id").notNull(),request_id: uuid("request_id").notNull(),diff --git a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/cache.ts b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/cache.tsindex 968094bc2..aa2f1a9e7 100644--- a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/cache.ts+++ b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/cache.ts@@ -349,7 +349,7 @@ export function provenanceFromResponse(* expensive to redo, so those results are still cached; the stamp's counts* let a later policy refresh them first.*/-export function cacheRefusalReason(+function cacheRefusalReason(failedPages: readonly number[] | null | undefined,provenance: FirePdfProvenance | null | undefined,): CacheRefusedReason | null {diff --git a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/refresh-budget.ts b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/refresh-budget.tsindex e2c9262ea..e52714be4 100644--- a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/refresh-budget.ts+++ b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/refresh-budget.ts@@ -21,11 +21,7 @@ import { config } from "../../../../../config";* The rate-limit Redis client is imported lazily so importing the cache* module never opens a connection (tests, tooling).*/-export type RefreshDecision =- | "allowed"- | "limited"- | "disabled"- | "unavailable";+type RefreshDecision = "allowed" | "limited" | "disabled" | "unavailable";let limiter: RateLimiterRedis | null = null;diff --git a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/schema.ts b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/schema.tsindex a6cfb78fd..7ca4c17c5 100644--- a/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/schema.ts+++ b/apps/api/src/scraper/scrapeURL/engines/pdf/fire-pdf/schema.ts@@ -245,7 +245,7 @@ export type FirePdfProvenance = z.infer<typeof firePdfProvenanceSchema>;* a stamp this build cannot read degrades to `malformed` (the result is* served, not cached) instead of failing the response.*/-export type ProvenanceParse =+type ProvenanceParse =| { status: "absent" }| { status: "ok"; provenance: FirePdfProvenance }| { status: "malformed"; issue: string };