Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pkgs/atlas-search/worker/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,26 @@ export async function searchService(
k: number,
): Promise<{ result: Record<string, unknown>; ms: number }> {
const t0 = Date.now();
const raw = (await postJson(env, "/v1/search", {
const raw: unknown = await postJson(env, "/v1/search", {
query,
ceiling: CEILING,
k,
})) as Record<string, unknown>;
if (raw.effective_ceiling !== CEILING) {
});
// The response is untrusted until validated, exactly like the embed
// door (validateEmbed in space.ts): a JSON null 200 cannot even be
// dereferenced for the ceiling check, and any other non-object shape
// has no ceiling to verify - both are a bad upstream (502), never a
// raw TypeError out of this client (#6982).
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
const shape = raw === null ? "null" : Array.isArray(raw) ? "array" : typeof raw;
throw new ServiceError(502, `search service returned a non-object response (${shape})`);
}
const result = raw as Record<string, unknown>;
if (result.effective_ceiling !== CEILING) {
throw new ServiceError(
502,
`search service reported effective_ceiling ${JSON.stringify(raw.effective_ceiling)}, expected ${CEILING}`,
`search service reported effective_ceiling ${JSON.stringify(result.effective_ceiling)}, expected ${CEILING}`,
);
}
return { result: raw, ms: Date.now() - t0 };
return { result, ms: Date.now() - t0 };
}
31 changes: 31 additions & 0 deletions pkgs/atlas-search/worker/test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ beforeAll(() => {
.intercept({ path: "/v1/embed", method: "POST" })
.reply(embedReply("good"))
.persist();
origin
.intercept({
path: "/v1/search",
method: "POST",
body: (b: string) => b.includes("nullsearch query"),
})
.reply(() => {
counts.search++;
return {
statusCode: 200,
data: "null",
responseOptions: { headers: { "Content-Type": "application/json" } },
};
})
.persist();
origin
.intercept({ path: "/v1/search", method: "POST" })
.reply(searchReply)
Expand Down Expand Up @@ -159,4 +174,20 @@ describe("atlas-search worker", () => {
expect(meta.has_result ?? false).toBe(false);
expect(meta.has_embedding ?? false).toBe(false);
});

it("reports a null search-service response as a 502, not a raw TypeError (#6982)", async () => {
const resp = await search({ query: "nullsearch query" });
expect(resp.status).toBe(502);
const body = (await resp.json()) as { error?: { message?: string } };
expect(body.error?.message).toContain("non-object response (null)");
const inspect = await SELF.fetch(
"https://worker.test/v1/cache/inspect?query=nullsearch%20query",
{ headers: GATE },
);
const meta = (await inspect.json()) as Record<string, unknown>;
// The embedding was stored before the search ran (the entry stays
// retryable in the embedded state); no result was cached.
expect(meta.has_embedding).toBe(true);
expect(meta.has_result).toBe(false);
});
});
Loading