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): give Alexandria feedback the search feedback time window (#4982)
Nicolasc23f0c04565d
diff --git a/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts b/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.tsindex 51602a938..a4c580717 100644--- a/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts+++ b/apps/api/src/__tests__/routes/exchange-agent-managed-key.routes.test.ts@@ -74,6 +74,7 @@ vi.mock("undici", async importOriginal => ({import { config } from "../../config";import { exchangeRouter } from "../../routes/exchange";+import { redisRateLimitClient } from "../../services/rate-limiter";const MANAGED_KEY = "22222222-2222-4222-8222-222222222222";const SECRET = "agent-secret";@@ -250,6 +251,35 @@ describe("hosted MCP keys on the Exchange routes the agent calls", () => {expect(mocks.upstreamFetch).toHaveBeenCalledTimes(2);});+ it.each([+ [200, true],+ [502, false],+ ])(+ "discover answering %i opens the Alexandria feedback window before responding: %s",+ async (status, opens) => {+ mocks.upstreamFetch.mockImplementation(+ async () => new Response(JSON.stringify({ status }), { status }),+ );+ const response = await send("get", "/exchange/discover?q=gdp", "header");+ expect(response.status).toBe(status);+ const activity = vi+ .mocked(redisRateLimitClient.set)+ .mock.calls.filter(([key]) => key === "alexandria:activity:team-mcp");+ expect(activity).toEqual(+ opens+ ? [+ [+ "alexandria:activity:team-mcp",+ "1",+ "EX",+ config.SEARCH_FEEDBACK_MAX_AGE_SEC,+ ],+ ]+ : [],+ );+ },+ );+it.each([["POST /exchange/provider-terms/accept",diff --git a/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts b/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.tsindex 3b05afb50..43b39a31f 100644--- a/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts+++ b/apps/api/src/__tests__/snips/v2/alexandria-feedback.test.ts@@ -56,169 +56,204 @@ describeIf(TEST_PRODUCTION)("Alexandria session feedback", () => {identity = await idmux({ name: "alexandria-feedback", credits: 1000 });});- // Billing confirms refunds, so environments without it report 0 credits.- it("records the minimum session feedback without any search or scrape job and reports its refund", async () => {- const response = await submit(body);- expect(response.statusCode).toBe(200);- expect(response.body).toMatchObject({- success: true,- creditsRefundedToday: expect.any(Number),- dailyRefundCap: expect.any(Number),+ // A successful catalogue read opens the window, and the API records it before+ // responding, so feedback sent right after sees it.+ describeIf(!!config.FIRE_EXCHANGE_URL)("within the feedback window", () => {+ const openWindow = async (apiKey = identity.apiKey) => {+ const response = await request(TEST_API_URL)+ .get("/exchange/discover")+ .set("Authorization", `Bearer ${apiKey}`);+ expect(response.statusCode).toBe(200);+ };++ beforeEach(async () => {+ await openWindow();});- expect([0, 1]).toContain(response.body.creditsRefunded);- expect(response.body.feedbackId).toEqual(expect.any(String));- try {- const { parent, providers, capabilities } = await feedbackRows(- response.body.feedbackId,- );- expect(parent).toMatchObject({- team_id: identity.teamId,- api_version: "v2",- rating: "partial",- requested_url: body.requestedWebsite.url,- requested_host: "sam.gov",- requested_functionality: body.requestedWebsite.requestedFunctionality,- rationale: body.rationale,- objective: null,- origin: "api",- integration: null,- schema_version: 2,- credits_refunded: response.body.creditsRefunded,- });- expect(parent.refund_policy).toMatchObject({- endpoint: "alexandria",- matchedReason:- response.body.creditsRefunded === 1- ? "alexandria_feedback"- : "refund_not_confirmed",++ it("rejects feedback from a team with no recent Alexandria call", async ctx => {+ // A unique name gives a team with no activity, even on reruns.+ const fresh = await idmux({+ name: `alexandria-feedback-no-window-${Date.now()}`,+ credits: 100,});- expect(providers).toEqual([]);- expect(capabilities).toEqual([]);- } finally {- await deleteFeedback(response.body.feedbackId);- }- });+ // Without idmux every identity is the same team, whose window is open.+ if (fresh.teamId === identity.teamId) ctx.skip();- it("persists provider issues and both new and existing capability feedback", async () => {- const providerFeedback = [- {- name: "sam.gov",- issue: "insufficient_coverage",- why: "The provider returned summaries without attachments.",- },- ];- const capabilityFeedback = [- {- name: "download-attachments",- provider: "sam.gov",- issue: "new_capability_request",- 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",- issue: "execution_error",- why: "The second page request timed out.",- },- ];- const objective = "Build a list of open federal IT contracts to bid on.";- const response = await submit({- ...body,- objective,- providerFeedback,- capabilityFeedback,- integration: "cli",+ const response = await submit(body, fresh.apiKey);+ expect(response.statusCode).toBe(409);+ expect(response.body.feedbackErrorCode).toBe("FEEDBACK_WINDOW_EXPIRED");++ await openWindow(fresh.apiKey);+ const accepted = await submit(body, fresh.apiKey);+ expect(accepted.statusCode).toBe(200);+ await deleteFeedback(accepted.body.feedbackId);});- expect(response.statusCode).toBe(200);- const feedbackId = response.body.feedbackId;- try {- const { parent, providers, capabilities } =- await feedbackRows(feedbackId);- expect(parent).toMatchObject({- rationale: body.rationale,++ // Billing confirms refunds, so environments without it report 0 credits.+ it("records the minimum session feedback without any search or scrape job and reports its refund", async () => {+ const response = await submit(body);+ expect(response.statusCode).toBe(200);+ expect(response.body).toMatchObject({+ success: true,+ creditsRefundedToday: expect.any(Number),+ dailyRefundCap: expect.any(Number),+ });+ expect([0, 1]).toContain(response.body.creditsRefunded);+ expect(response.body.feedbackId).toEqual(expect.any(String));+ try {+ const { parent, providers, capabilities } = await feedbackRows(+ response.body.feedbackId,+ );+ expect(parent).toMatchObject({+ team_id: identity.teamId,+ api_version: "v2",+ rating: "partial",+ requested_url: body.requestedWebsite.url,+ requested_host: "sam.gov",+ requested_functionality: body.requestedWebsite.requestedFunctionality,+ rationale: body.rationale,+ objective: null,+ origin: "api",+ integration: null,+ schema_version: 2,+ credits_refunded: response.body.creditsRefunded,+ });+ expect(parent.refund_policy).toMatchObject({+ endpoint: "alexandria",+ matchedReason:+ response.body.creditsRefunded === 1+ ? "alexandria_feedback"+ : "refund_not_confirmed",+ });+ expect(providers).toEqual([]);+ expect(capabilities).toEqual([]);+ } finally {+ await deleteFeedback(response.body.feedbackId);+ }+ });++ it("persists provider issues and both new and existing capability feedback", async () => {+ const providerFeedback = [+ {+ name: "sam.gov",+ issue: "insufficient_coverage",+ why: "The provider returned summaries without attachments.",+ },+ ];+ const capabilityFeedback = [+ {+ name: "download-attachments",+ provider: "sam.gov",+ issue: "new_capability_request",+ 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",+ issue: "execution_error",+ why: "The second page request timed out.",+ },+ ];+ const objective = "Build a list of open federal IT contracts to bid on.";+ const response = await submit({+ ...body,objective,+ providerFeedback,+ capabilityFeedback,integration: "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(response.statusCode).toBe(200);+ const feedbackId = response.body.feedbackId;+ try {+ const { parent, providers, capabilities } =+ await feedbackRows(feedbackId);+ expect(parent).toMatchObject({+ rationale: body.rationale,+ objective,+ integration: "cli",+ });+ expect(providers).toEqual(+ providerFeedback.map((entry, position) =>expect.objectContaining({feedback_id: feedbackId,team_id: identity.teamId,position,- requested_functionality: requestedFunctionality ?? null,...entry,}),- ),- );- } finally {- await deleteFeedback(feedbackId);- }- });-- it("stops refunding a website at its daily cap", async () => {- const websiteCap = config.ALEXANDRIA_FEEDBACK_WEBSITE_DAILY_CAP_CREDITS;- const website = {- url: "https://www.usaspending.gov/search",- requestedFunctionality: "List federal awards by recipient.",- };- const responses: request.Response[] = [];- try {- for (let i = 0; i <= websiteCap; i++) {- responses.push(await submit({ ...body, requestedWebsite: website }));- }- for (const response of responses) {- expect(response.statusCode).toBe(200);+ ),+ );+ 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 deleteFeedback(feedbackId);}- const last = responses.at(-1)!;- expect(last.body).toMatchObject({ success: true, creditsRefunded: 0 });- const first = responses[0].body;- if (first.creditsRefunded === 1) {- // The team cap and earlier refunds today can stop refunds before the- // website cap does.- const teamRemaining =- first.dailyRefundCap - (first.creditsRefundedToday - 1);- const refunded = Math.min(websiteCap, teamRemaining);- expect(responses.map(r => r.body.creditsRefunded)).toEqual([- ...Array(refunded).fill(1),- ...Array(websiteCap + 1 - refunded).fill(0),- ]);- if (websiteCap <= teamRemaining) {- expect(last.body.websiteCapReached).toBe(true);+ });++ it("stops refunding a website at its daily cap", async () => {+ const websiteCap = config.ALEXANDRIA_FEEDBACK_WEBSITE_DAILY_CAP_CREDITS;+ const website = {+ url: "https://www.usaspending.gov/search",+ requestedFunctionality: "List federal awards by recipient.",+ };+ const responses: request.Response[] = [];+ try {+ for (let i = 0; i <= websiteCap; i++) {+ responses.push(await submit({ ...body, requestedWebsite: website }));}- if (teamRemaining <= websiteCap) {- expect(last.body.dailyCapReached).toBe(true);- } else {- expect(last.body.warning).toContain("www.usaspending.gov");+ for (const response of responses) {+ expect(response.statusCode).toBe(200);}+ const last = responses.at(-1)!;+ expect(last.body).toMatchObject({ success: true, creditsRefunded: 0 });+ const first = responses[0].body;+ if (first.creditsRefunded === 1) {+ // The team cap and earlier refunds today can stop refunds before the+ // website cap does.+ const teamRemaining =+ first.dailyRefundCap - (first.creditsRefundedToday - 1);+ const refunded = Math.min(websiteCap, teamRemaining);+ expect(responses.map(r => r.body.creditsRefunded)).toEqual([+ ...Array(refunded).fill(1),+ ...Array(websiteCap + 1 - refunded).fill(0),+ ]);+ if (websiteCap <= teamRemaining) {+ expect(last.body.websiteCapReached).toBe(true);+ }+ if (teamRemaining <= websiteCap) {+ expect(last.body.dailyCapReached).toBe(true);+ } else {+ expect(last.body.warning).toContain("www.usaspending.gov");+ }+ }+ const { parent } = await feedbackRows(last.body.feedbackId);+ expect(parent.credits_refunded).toBe(0);+ } finally {+ await Promise.all(+ responses+ .map(response => response.body.feedbackId)+ .filter(Boolean)+ .map(deleteFeedback),+ );}- const { parent } = await feedbackRows(last.body.feedbackId);- expect(parent.credits_refunded).toBe(0);- } finally {- await Promise.all(- responses- .map(response => response.body.feedbackId)- .filter(Boolean)- .map(deleteFeedback),- );- }+ });});it.each(["endpoint", "rating", "requestedWebsite", "rationale"])(diff --git a/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts b/apps/api/src/controllers/v2/feedback/alexandria.integration.test.tsindex ba32e554b..1cae15329 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.integration.test.ts@@ -13,6 +13,9 @@ vi.mock("./record", () => ({throw new Error("Alexandria must not enter the job/refund path");},}));+vi.mock("../../../lib/alexandria-activity", () => ({+ hasRecentAlexandriaActivity: async () => true,+}));vi.mock("../../../services/autumn/autumn.service", () => ({CREDITS_FEATURE_ID: "CREDITS",autumnService: { refundCredits: async () => true },diff --git a/apps/api/src/controllers/v2/feedback/alexandria.test.ts b/apps/api/src/controllers/v2/feedback/alexandria.test.tsindex 25293118b..892d9b9ca 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.test.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.test.ts@@ -10,11 +10,15 @@ const mocks = vi.hoisted(() => ({insert: vi.fn(),recordEndpointFeedback: vi.fn(),refundAlexandriaFeedback: vi.fn(),+ hasRecentAlexandriaActivity: vi.fn(),logError: vi.fn(),}));vi.mock("./alexandria-refund", () => ({refundAlexandriaFeedback: mocks.refundAlexandriaFeedback,}));+vi.mock("../../../lib/alexandria-activity", () => ({+ hasRecentAlexandriaActivity: mocks.hasRecentAlexandriaActivity,+}));vi.mock("../../../db/connection", () => ({db: { transaction: mocks.transaction },}));@@ -78,6 +82,7 @@ beforeEach(() => {async (run: (tx: unknown) => Promise<void>) => run(tx),);mocks.insert.mockResolvedValue(undefined);+ mocks.hasRecentAlexandriaActivity.mockResolvedValue(true);mocks.refundAlexandriaFeedback.mockResolvedValue({creditsRefunded: 1,creditsRefundedToday: 1,@@ -626,6 +631,41 @@ it("returns the refund outcome, including cap and duplicate warnings", async ()});});+it("rejects feedback outside the window after the team's last Alexandria call", async () => {+ mocks.hasRecentAlexandriaActivity.mockResolvedValue(false);+ const response = await submit(minimal);+ expect(response.status).toBe(409);+ expect(response.body).toEqual({+ success: false,+ feedbackErrorCode: "FEEDBACK_WINDOW_EXPIRED",+ error: `Alexandria feedback must be submitted within ${config.SEARCH_FEEDBACK_MAX_AGE_SEC} seconds of an Alexandria search, discovery, or execution.`,+ });+ expect(mocks.hasRecentAlexandriaActivity).toHaveBeenCalledWith(teamId);+ expect(mocks.transaction).not.toHaveBeenCalled();+ expect(mocks.refundAlexandriaFeedback).not.toHaveBeenCalled();+});++it("fails closed when the feedback window cannot be checked", async () => {+ const error = new Error("redis down");+ mocks.hasRecentAlexandriaActivity.mockRejectedValue(error);+ const response = await submit(minimal);+ expect(response.status).toBe(500);+ expect(response.body.feedbackErrorCode).toBe("INTERNAL");+ expect(mocks.logError).toHaveBeenCalledWith(+ "Failed to check the Alexandria feedback window",+ { error, teamId },+ );+ expect(mocks.transaction).not.toHaveBeenCalled();+ expect(mocks.refundAlexandriaFeedback).not.toHaveBeenCalled();+});++it("validates the body before checking the window", async () => {+ mocks.hasRecentAlexandriaActivity.mockResolvedValue(false);+ const response = await submit({ ...minimal, rationale: undefined });+ expect(response.status).toBe(400);+ expect(mocks.hasRecentAlexandriaActivity).not.toHaveBeenCalled();+});+it("honors team opt-out", async () => {flags = { searchFeedbackOptOut: true };const response = await submit(minimal);diff --git a/apps/api/src/controllers/v2/feedback/alexandria.ts b/apps/api/src/controllers/v2/feedback/alexandria.tsindex 4193e85c0..0bfdc6429 100644--- a/apps/api/src/controllers/v2/feedback/alexandria.ts+++ b/apps/api/src/controllers/v2/feedback/alexandria.ts@@ -3,6 +3,7 @@ import { DrizzleQueryError } from "drizzle-orm/errors";import { config } from "../../../config";import { db } from "../../../db/connection";import * as schema from "../../../db/schema";+import { hasRecentAlexandriaActivity } from "../../../lib/alexandria-activity";import { logger } from "../../../lib/logger";import { getScrapeZDR, getSearchZDR } from "../../../lib/zdr-helpers";import type { RequestWithAuth, EndpointFeedbackErrorCode } from "../types";@@ -66,6 +67,24 @@ export async function recordAlexandriaFeedback(};}+ let withinWindow: boolean;+ try {+ withinWindow = await hasRecentAlexandriaActivity(teamId);+ } catch (error) {+ logger.error("Failed to check the Alexandria feedback window", {+ error,+ teamId,+ });+ return failure(500, "INTERNAL", "Failed to check the feedback window.");+ }+ if (!withinWindow) {+ return failure(+ 409,+ "FEEDBACK_WINDOW_EXPIRED",+ `Alexandria feedback must be submitted within ${config.SEARCH_FEEDBACK_MAX_AGE_SEC} seconds of an Alexandria search, discovery, or execution.`,+ );+ }+const feedbackId = uuidv7();const providerRows = (body.providerFeedback ?? []).map((entry, position) => ({feedback_id: feedbackId,diff --git a/apps/api/src/controllers/v2/feedback/feedback-window.test.ts b/apps/api/src/controllers/v2/feedback/feedback-window.test.tsindex b9a10244a..c09349811 100644--- a/apps/api/src/controllers/v2/feedback/feedback-window.test.ts+++ b/apps/api/src/controllers/v2/feedback/feedback-window.test.ts@@ -9,6 +9,7 @@ const fixture = vi.hoisted(() => ({alexandriaInsert: vi.fn(),refund: vi.fn(),refundedToday: vi.fn(),+ alexandriaActivity: vi.fn(),}));vi.mock("./feedback-store", () => ({lookupFeedbackJob: fixture.lookup,@@ -28,6 +29,9 @@ vi.mock("../../../db/connection", () => ({vi.mock("./refund-totals", () => ({sumCreditsRefundedToday: fixture.refundedToday,}));+vi.mock("../../../lib/alexandria-activity", () => ({+ hasRecentAlexandriaActivity: fixture.alexandriaActivity,+}));vi.mock("./alexandria-refund", () => ({refundAlexandriaFeedback: async () => ({creditsRefunded: 1,@@ -125,6 +129,7 @@ beforeEach(() => {fixture.insert.mockResolvedValue(null);fixture.alexandriaInsert.mockResolvedValue(undefined);fixture.refundedToday.mockResolvedValue(0);+ fixture.alexandriaActivity.mockResolvedValue(true);});afterEach(() => {vi.restoreAllMocks();@@ -171,8 +176,30 @@ describe.each(routes)("%s job feedback keeps its submission window", route => {});});+const alexandriaFeedback = () =>+ request(app)+ .post("/v2/feedback")+ .send({+ endpoint: "alexandria",+ rating: "partial",+ requestedWebsite: {+ url: "https://example.com",+ requestedFunctionality: "Retrieve records and their attachments.",+ },+ rationale: "Found record summaries but could not retrieve attachments.",+ });++it("rejects Alexandria feedback once the window after the team's last Alexandria call closes", async () => {+ fixture.alexandriaActivity.mockResolvedValue(false);+ const response = await alexandriaFeedback();+ expect(response.status).toBe(409);+ expect(response.body.feedbackErrorCode).toBe("FEEDBACK_WINDOW_EXPIRED");+ expect(fixture.alexandriaActivity).toHaveBeenCalledWith(teamId);+ expect(fixture.alexandriaInsert).not.toHaveBeenCalled();+});+it.each(stores)(- "accepts Alexandria feedback when the session's original %s search has expired",+ "accepts Alexandria feedback within the window of a later Alexandria call after an earlier %s search expired",async store => {job("search", store, 30 * 60);const expiredSearch = await submit("search");@@ -183,17 +210,7 @@ it.each(stores)(expect(fixture.lookup).toHaveBeenCalledTimes(1);fixture.lookup.mockClear();- const response = await request(app)- .post("/v2/feedback")- .send({- endpoint: "alexandria",- rating: "partial",- requestedWebsite: {- url: "https://example.com",- requestedFunctionality: "Retrieve records and their attachments.",- },- rationale: "Found record summaries but could not retrieve attachments.",- });+ const response = await alexandriaFeedback();expect(response.status).toBe(200);expect(response.body).toEqual({diff --git a/apps/api/src/controllers/v2/scrape-alexandria.ts b/apps/api/src/controllers/v2/scrape-alexandria.tsindex ba19f4cc3..de78d1403 100644--- a/apps/api/src/controllers/v2/scrape-alexandria.ts+++ b/apps/api/src/controllers/v2/scrape-alexandria.ts@@ -10,6 +10,7 @@ import { getScrapeZDR } from "../../lib/zdr-helpers";import { checkKeyFormatRestriction } from "../../lib/key-restriction";import { orgIdFromAcuc } from "../../lib/team-org";import { logProviderScrape, logRequest } from "../../services/logging/log_job";+import { markAlexandriaActivity } from "../../lib/alexandria-activity";import {callsSchema,callSchema,@@ -147,6 +148,7 @@ export async function providerScrapeController(});}const timeTaken = (Date.now() - startedAt) / 1000;+ markAlexandriaActivity(req.auth.team_id);if (result.executed && !body.__agentInterop) {const apiKeyId = req.acuc.api_key_id ?? null;const served = answerSchema.safeParse(result.body);diff --git a/apps/api/src/controllers/v2/scrape.ts b/apps/api/src/controllers/v2/scrape.tsindex d8a854d1a..f01f5bd2e 100644--- a/apps/api/src/controllers/v2/scrape.ts+++ b/apps/api/src/controllers/v2/scrape.ts@@ -51,6 +51,7 @@ import { applyAgentAuthDiscoveryHeader } from "../../lib/agent-auth-discovery";import { resolveThreatProtection } from "../../lib/threat-protection/request";import { emitRejectedScrapeActivityEvent } from "../../lib/siem-logging";import { isAgentInteropSecretValid } from "../../lib/agent-interop";+import { markAlexandriaActivity } from "../../lib/alexandria-activity";import { DEFAULT_TEAM_LIMITS } from "../../services/autumn/autumn.service";const AGENT_INTEROP_CONCURRENCY_BOOST = 3;@@ -759,6 +760,7 @@ export async function scrapeController(return undefined;}): undefined;+ if (tools) markAlexandriaActivity(req.auth.team_id);return res.status(200).json({success: true,diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.tsindex 16cc36351..67714a8f5 100644--- a/apps/api/src/controllers/v2/search.ts+++ b/apps/api/src/controllers/v2/search.ts@@ -55,6 +55,7 @@ import { wantsGovCategory } from "../../search/gov";import { requestOrigin } from "../../lib/request-origin";import { isAgentInteropSecretValid } from "../../lib/agent-interop";import { applyNotice, type Notice } from "../../lib/deprecations";+import { markAlexandriaActivity } from "../../lib/alexandria-activity";const RESEARCH_CATEGORY_NOTICE: Notice = {message:@@ -548,6 +549,7 @@ async function searchControllerInner(scrapeful: result.shouldScrape,});+ if (result.response.tools) markAlexandriaActivity(req.auth.team_id);return res.status(200).json({success: true,data: result.response,diff --git a/apps/api/src/lib/alexandria-activity.test.ts b/apps/api/src/lib/alexandria-activity.test.tsnew file mode 100644index 000000000..711792c9d--- /dev/null+++ b/apps/api/src/lib/alexandria-activity.test.ts@@ -0,0 +1,66 @@+const mocks = vi.hoisted(() => ({+ set: vi.fn(),+ exists: vi.fn(),+ warn: vi.fn(),+}));+vi.mock("../services/rate-limiter", () => ({+ redisRateLimitClient: { set: mocks.set, exists: mocks.exists },+}));+vi.mock("./logger", () => ({ logger: { warn: mocks.warn } }));++import { config } from "../config";+import {+ hasRecentAlexandriaActivity,+ markAlexandriaActivity,+ recordAlexandriaActivity,+} from "./alexandria-activity";++const teamId = "01933161-0000-7000-8000-000000000001";++beforeEach(() => {+ vi.clearAllMocks();+ mocks.set.mockResolvedValue("OK");+});++it("opens a feedback window as long as the search feedback window", async () => {+ await recordAlexandriaActivity(teamId);+ expect(mocks.set).toHaveBeenCalledExactlyOnceWith(+ `alexandria:activity:${teamId}`,+ "1",+ "EX",+ config.SEARCH_FEEDBACK_MAX_AGE_SEC,+ );+});++it("resolves and logs when recording activity fails", async () => {+ const error = new Error("redis down");+ mocks.set.mockRejectedValue(error);+ await expect(recordAlexandriaActivity(teamId)).resolves.toBeUndefined();+ expect(mocks.warn).toHaveBeenCalledWith(+ "Failed to record Alexandria activity",+ { error, teamId },+ );+});++it("records activity without blocking or throwing in the fire-and-forget form", async () => {+ mocks.set.mockRejectedValue(new Error("redis down"));+ expect(markAlexandriaActivity(teamId)).toBeUndefined();+ await vi.waitFor(() => expect(mocks.warn).toHaveBeenCalled());+});++it.each([+ [1, true],+ [0, false],+])("reports recent activity when the key exists (%i)", async (count, open) => {+ mocks.exists.mockResolvedValue(count);+ await expect(hasRecentAlexandriaActivity(teamId)).resolves.toBe(open);+ expect(mocks.exists).toHaveBeenCalledWith(`alexandria:activity:${teamId}`);+});++// The feedback controller turns this rejection into a 500; swallowing it into+// false would wrongly report an expired window.+it("rejects when the window lookup fails", async () => {+ const error = new Error("redis down");+ mocks.exists.mockRejectedValue(error);+ await expect(hasRecentAlexandriaActivity(teamId)).rejects.toBe(error);+});diff --git a/apps/api/src/lib/alexandria-activity.ts b/apps/api/src/lib/alexandria-activity.tsnew file mode 100644index 000000000..a6ae8f202--- /dev/null+++ b/apps/api/src/lib/alexandria-activity.ts@@ -0,0 +1,34 @@+import { config } from "../config";+import { redisRateLimitClient } from "../services/rate-limiter";+import { logger } from "./logger";++const activityKey = (teamId: string) => `alexandria:activity:${teamId}`;++/**+ * Opens (or extends) the team's Alexandria feedback window, which lasts as long+ * as the search feedback window. Never rejects; a failed write is only logged.+ */+export async function recordAlexandriaActivity(teamId: string): Promise<void> {+ try {+ await redisRateLimitClient.set(+ activityKey(teamId),+ "1",+ "EX",+ config.SEARCH_FEEDBACK_MAX_AGE_SEC,+ );+ } catch (error) {+ logger.warn("Failed to record Alexandria activity", { error, teamId });+ }+}++/** Fire-and-forget form of recordAlexandriaActivity for latency-sensitive paths. */+export function markAlexandriaActivity(teamId: string): void {+ void recordAlexandriaActivity(teamId);+}++/** Whether the team used Alexandria within the feedback window. */+export async function hasRecentAlexandriaActivity(+ teamId: string,+): Promise<boolean> {+ return (await redisRateLimitClient.exists(activityKey(teamId))) === 1;+}diff --git a/apps/api/src/routes/exchange.ts b/apps/api/src/routes/exchange.tsindex 982ebd3b6..88106a186 100644--- a/apps/api/src/routes/exchange.ts+++ b/apps/api/src/routes/exchange.ts@@ -8,6 +8,7 @@ import {import express, { Request, Response } from "express";import { Agent, fetch } from "undici";import { config } from "../config";+import { recordAlexandriaActivity } from "../lib/alexandria-activity";import { logger as rootLogger } from "../lib/logger";import type { RequestWithAuth } from "../controllers/v1/types";import { RateLimiterMode } from "../types";@@ -43,7 +44,10 @@ function upstreamBase(): string | null {function exchangeProxy(timeout: number,- options: { requiresRetrieveFlag?: boolean } = {},+ options: {+ requiresRetrieveFlag?: boolean;+ opensFeedbackWindow?: boolean;+ } = {},) {const requiresRetrieveFlag = options.requiresRetrieveFlag !== false;const dispatcher = dispatcherFor(timeout);@@ -97,6 +101,10 @@ function exchangeProxy(}const text = await upstream.text();+ // Awaited so feedback sent right after this response sees the window.+ if (options.opensFeedbackWindow && upstream.ok) {+ await recordAlexandriaActivity(authedReq.auth.team_id);+ }let body: unknown;try {body = text ? JSON.parse(text) : null;@@ -160,7 +168,12 @@ exchangeRouter.get(authMiddleware(RateLimiterMode.ExchangeDiscover, {allowAgentManagedKey: true,}),- wrap(exchangeProxy(DISCOVER_TIMEOUT_MS, { requiresRetrieveFlag: false })),+ wrap(+ exchangeProxy(DISCOVER_TIMEOUT_MS, {+ requiresRetrieveFlag: false,+ opensFeedbackWindow: true,+ }),+ ),);// These read-only discovery routes remain authenticated; they do not execute paid tools.