Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): address the code search review findings

Karan Lokchandani5149023875e0
diff --git a/apps/api/src/__tests__/snips/v2/code.test.ts b/apps/api/src/__tests__/snips/v2/code.test.ts
index 5d4fbcb42..0ab3caaac 100644
--- a/apps/api/src/__tests__/snips/v2/code.test.ts
+++ b/apps/api/src/__tests__/snips/v2/code.test.ts
@@ -232,7 +232,9 @@ describeIf(HAS_RESEARCH)("Code Search API", () => {
k: 1,
});
- expect(res.statusCode).not.toBe(401);
+ expect(res.statusCode).toBe(200);
+ expect(res.body.success).toBe(true);
+ expect(Array.isArray(res.body.results)).toBe(true);
}, 120000);
it("refuses keyless access on the upstream-mirror mount", async () => {
diff --git a/apps/api/src/search/code.test.ts b/apps/api/src/search/code.test.ts
index b7fa46a33..71781a979 100644
--- a/apps/api/src/search/code.test.ts
+++ b/apps/api/src/search/code.test.ts
@@ -88,8 +88,13 @@ describe("searchCodeCategory", () => {
]);
});
- it("returns no results when the upstream fails", async () => {
- fetchMock.mockResolvedValue({ ok: false, status: 503 } as any);
+ it("returns no results and drains the body when the upstream fails", async () => {
+ const cancel = vi.fn(async () => {});
+ fetchMock.mockResolvedValue({
+ ok: false,
+ status: 503,
+ body: { cancel },
+ } as any);
await expect(
searchCodeCategory(
@@ -97,6 +102,7 @@ describe("searchCodeCategory", () => {
logger,
),
).resolves.toEqual([]);
+ expect(cancel).toHaveBeenCalledTimes(1);
});
it("returns no results when the upstream throws", async () => {
diff --git a/apps/api/src/search/code.ts b/apps/api/src/search/code.ts
index 6cdb58ea8..139b19652 100644
--- a/apps/api/src/search/code.ts
+++ b/apps/api/src/search/code.ts
@@ -29,6 +29,7 @@ export async function searchCodeCategory(
return [];
}
if (!upstream.ok) {
+ await upstream.body?.cancel().catch(() => {});
logger.warn("Code category upstream failed", {
status: upstream.status,
});
diff --git a/apps/api/src/search/execute.ts b/apps/api/src/search/execute.ts
index 95297f2d0..231a36b69 100644
--- a/apps/api/src/search/execute.ts
+++ b/apps/api/src/search/execute.ts
@@ -126,6 +126,13 @@ export async function executeSearch(
enterprise: options.enterprise,
})) as SearchV2Response;
+ if (codeResults) {
+ const code = await codeResults;
+ if (code.length > 0) {
+ searchResponse.code = code;
+ }
+ }
+
// Threat protection: remove blocked results entirely — before
// slicing/counting, before scraping, and before returning. Checks are
// URL-level and deduped within this request; scan fees bill +2 per unique
@@ -138,6 +145,7 @@ export async function executeSearch(
...(searchResponse.web ?? []).map(x => x.url),
...(searchResponse.news ?? []).map(x => x.url),
...(searchResponse.images ?? []).map(x => x.url),
+ ...(searchResponse.code ?? []).map(x => x.url),
].filter((x): x is string => !!x);
if (urlsToCheck.length > 0) {
@@ -163,6 +171,9 @@ export async function executeSearch(
isAllowed(x.url),
);
}
+ if (searchResponse.code) {
+ searchResponse.code = searchResponse.code.filter(x => isAllowed(x.url));
+ }
}
}
@@ -205,12 +216,8 @@ export async function executeSearch(
totalResultsCount += searchResponse.news.length;
}
- if (codeResults) {
- const code = await codeResults;
- if (code.length > 0) {
- searchResponse.code = code;
- totalResultsCount += code.length;
- }
+ if (searchResponse.code && searchResponse.code.length > 0) {
+ totalResultsCount += searchResponse.code.length;
}
const isZDR = options.enterprise?.includes("zdr");