Skip to content

Commit d530ef3

Browse files
fix(acp): classify web search by title and omit fake filename results (pingdotgg#38106)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent f7041fa commit d530ef3

3 files changed

Lines changed: 184 additions & 16 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,33 @@ const program = Effect.gen(function* () {
12681268
status: "completed",
12691269
rawInput: { query: "TODO", path: "apps/web" },
12701270
},
1271+
{
1272+
sessionUpdate: "tool_call_update",
1273+
toolCallId: "structured-search-explicit-location",
1274+
title: "Grep",
1275+
kind: "search",
1276+
status: "completed",
1277+
rawInput: { query: " README.md " },
1278+
locations: [{ path: "README.md" }],
1279+
rawOutput: "# Title",
1280+
},
1281+
{
1282+
sessionUpdate: "tool_call_update",
1283+
toolCallId: "structured-search-query-only",
1284+
title: "Grep",
1285+
kind: "search",
1286+
status: "completed",
1287+
rawInput: { query: "TODO" },
1288+
rawOutput: "src/a.ts: TODO",
1289+
},
1290+
{
1291+
sessionUpdate: "tool_call_update",
1292+
toolCallId: "structured-search-query-no-output",
1293+
title: "Grep",
1294+
kind: "search",
1295+
status: "completed",
1296+
rawInput: { query: "FIXME" },
1297+
},
12711298
// Grok backend searches: the query only arrives in the completed rawOutput.
12721299
{
12731300
sessionUpdate: "tool_call_update",
@@ -1332,6 +1359,43 @@ const program = Effect.gen(function* () {
13321359
rawInput: { command: "cat probe.txt" },
13331360
rawOutput: { commandLine: "cat probe.txt", exitCode: 0, combinedOutput: "after\n" },
13341361
},
1362+
{
1363+
sessionUpdate: "tool_call_update",
1364+
toolCallId: "antigravity-web-search",
1365+
title: "Web search",
1366+
kind: "search",
1367+
status: "completed",
1368+
rawInput: { query: " top story on Hacker News today " },
1369+
rawOutput:
1370+
"Sources:\n<https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGYNzfz>.",
1371+
},
1372+
{
1373+
sessionUpdate: "tool_call_update",
1374+
toolCallId: "antigravity-run-search-web",
1375+
title: "Running search_web",
1376+
kind: "search",
1377+
status: "completed",
1378+
rawInput: { query: "top story on Hacker News today" },
1379+
},
1380+
{
1381+
sessionUpdate: "tool_call_update",
1382+
toolCallId: "antigravity-web-search-streamed",
1383+
title: "Web search",
1384+
kind: "search",
1385+
status: "in_progress",
1386+
rawInput: { query: "streamed citations" },
1387+
rawOutput: "Sources: https://example.com/streamed_(detail)",
1388+
},
1389+
{
1390+
sessionUpdate: "tool_call_content_chunk",
1391+
toolCallId: "antigravity-web-search-streamed",
1392+
content: { type: "content", content: { type: "text", text: " (updated)" } },
1393+
},
1394+
{
1395+
sessionUpdate: "tool_call_update",
1396+
toolCallId: "antigravity-web-search-streamed",
1397+
status: "completed",
1398+
},
13351399
{
13361400
sessionUpdate: "compaction_update",
13371401
compactionId: "compact-1",

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4647,6 +4647,28 @@ describe("AcpAdapterV2", () => {
46474647
search?.type === "file_search" ? { title: search.title, pattern: search.pattern } : null,
46484648
{ title: "Searched TODO in web", pattern: "apps/web" },
46494649
);
4650+
const fileSearch = (nativeId: string) => {
4651+
const item = items.find(
4652+
(candidate) =>
4653+
candidate.type === "file_search" &&
4654+
candidate.nativeItemRef?.nativeId?.endsWith(nativeId) === true,
4655+
);
4656+
return item?.type === "file_search"
4657+
? { pattern: item.pattern, results: item.results }
4658+
: null;
4659+
};
4660+
assert.deepEqual(fileSearch("structured-search-explicit-location"), {
4661+
pattern: "README.md",
4662+
results: [{ fileName: "README.md", preview: "# Title" }],
4663+
});
4664+
assert.deepEqual(fileSearch("structured-search-query-only"), {
4665+
pattern: "TODO",
4666+
results: [{ fileName: "TODO", preview: "src/a.ts: TODO" }],
4667+
});
4668+
assert.deepEqual(fileSearch("structured-search-query-no-output"), {
4669+
pattern: "FIXME",
4670+
results: undefined,
4671+
});
46504672
const webItem = (nativeId: string, status: string) => {
46514673
const item = items.findLast(
46524674
(candidate) =>
@@ -4673,6 +4695,25 @@ describe("AcpAdapterV2", () => {
46734695
patterns: ["t3 code"],
46744696
results: [{ url: "https://t3.codes" }, { url: "https://github.com/pingdotgg/t3code" }],
46754697
});
4698+
assert.deepEqual(webItem("antigravity-web-search", "completed"), {
4699+
title: "Web search: top story on Hacker News today",
4700+
patterns: ["top story on Hacker News today"],
4701+
results: [
4702+
{
4703+
url: "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGYNzfz",
4704+
},
4705+
],
4706+
});
4707+
assert.deepEqual(webItem("antigravity-run-search-web", "completed"), {
4708+
title: "Web search: top story on Hacker News today",
4709+
patterns: ["top story on Hacker News today"],
4710+
results: undefined,
4711+
});
4712+
assert.deepEqual(webItem("antigravity-web-search-streamed", "completed"), {
4713+
title: "Web search: streamed citations",
4714+
patterns: ["streamed citations"],
4715+
results: [{ url: "https://example.com/streamed_(detail)" }],
4716+
});
46764717
assert.deepEqual(webItem("grok-web-fetch", "completed")?.results, [
46774718
{ url: "https://t3.codes", snippet: "T3 Code page" },
46784719
]);

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,41 +1330,66 @@ export function acpToolCallDiffPatch(content: unknown): string | undefined {
13301330
.join("\n");
13311331
}
13321332

1333-
function pathFromToolCall(toolCall: AcpToolCallState): string | undefined {
1333+
function resolvedPathInfo(
1334+
toolCall: AcpToolCallState,
1335+
): { readonly path: string; readonly fromQuery: boolean } | undefined {
13341336
const locations = toolCall.data.locations;
13351337
if (Array.isArray(locations)) {
13361338
for (const location of locations) {
13371339
const path = unknownRecord(location)?.path;
13381340
if (typeof path === "string" && path.trim().length > 0) {
1339-
return path.trim();
1341+
return { path: path.trim(), fromQuery: false };
13401342
}
13411343
}
13421344
}
13431345
const rawInput = unknownRecord(toolCall.data.rawInput);
1344-
for (const key of ["path", "filePath", "file_path", "url", "query", "pattern"]) {
1346+
for (const key of ["path", "filePath", "file_path", "url"]) {
1347+
const candidate = rawInput?.[key];
1348+
if (typeof candidate === "string" && candidate.trim().length > 0) {
1349+
return { path: candidate.trim(), fromQuery: false };
1350+
}
1351+
}
1352+
for (const key of ["query", "pattern"]) {
13451353
const candidate = rawInput?.[key];
13461354
if (typeof candidate === "string" && candidate.trim().length > 0) {
1347-
return candidate.trim();
1355+
return { path: candidate.trim(), fromQuery: true };
13481356
}
13491357
}
13501358
return undefined;
13511359
}
13521360

1361+
function pathFromToolCall(toolCall: AcpToolCallState): string | undefined {
1362+
return resolvedPathInfo(toolCall)?.path;
1363+
}
1364+
13531365
function acpBackendWebSearch(
13541366
rawInput: Record<string, unknown> | undefined,
1355-
rawOutput: Record<string, unknown> | undefined,
1367+
rawOutput: unknown,
1368+
title?: string | null,
13561369
):
13571370
| { readonly query: string | undefined; readonly results: OrchestrationV2WebSearchResult[] }
13581371
| undefined {
13591372
const variant = typeof rawInput?.variant === "string" ? rawInput.variant.toLowerCase() : "";
1360-
const action = unknownRecord(rawOutput?.action);
1361-
if (variant !== "xsearch" && variant !== "websearch" && action?.type !== "search") {
1373+
const rawOutputRecord = unknownRecord(rawOutput);
1374+
const action = unknownRecord(rawOutputRecord?.action);
1375+
const isWebSearchTitle =
1376+
/^\s*(?:web search|search the web|(?:run(?:ning)?\s+)?(?:web_search|search_web)\s*$)/iu.test(
1377+
title ?? "",
1378+
);
1379+
const isWebSearchVariant = [
1380+
"xsearch",
1381+
"websearch",
1382+
"web_search",
1383+
"search_web",
1384+
"web-search",
1385+
].includes(variant);
1386+
if (!isWebSearchVariant && action?.type !== "search" && !isWebSearchTitle) {
13621387
return undefined;
13631388
}
13641389
let args: Record<string, unknown> | undefined;
1365-
if (typeof rawOutput?.input === "string") {
1390+
if (typeof rawOutputRecord?.input === "string") {
13661391
try {
1367-
args = unknownRecord(JSON.parse(rawOutput.input));
1392+
args = unknownRecord(JSON.parse(rawOutputRecord.input));
13681393
} catch {
13691394
args = undefined;
13701395
}
@@ -1373,17 +1398,46 @@ function acpBackendWebSearch(
13731398
.filter(([, value]) => typeof value === "string" || typeof value === "number")
13741399
.map(([key, value]) => `${key}: ${value}`)
13751400
.join(", ");
1376-
const query = [action?.query, args?.query, argsText]
1401+
const query = [
1402+
action?.query,
1403+
args?.query,
1404+
rawInput?.query,
1405+
rawInput?.search_query,
1406+
rawInput?.q,
1407+
argsText,
1408+
]
13771409
.find((value): value is string => typeof value === "string" && value.trim().length > 0)
13781410
?.trim();
13791411
const urls = new Set<string>();
13801412
for (const source of Array.isArray(action?.sources) ? action.sources : []) {
13811413
const url = unknownRecord(source)?.url;
13821414
if (typeof url === "string" && url.trim().length > 0) urls.add(url.trim());
13831415
}
1416+
if (urls.size === 0) {
1417+
for (const match of textFromUnknown(rawOutput)?.matchAll(/https?:\/\/[^\s<>"]+/gu) ?? []) {
1418+
urls.add(trimCitationUrl(match[0]));
1419+
}
1420+
}
13841421
return { query, results: [...urls].map((url) => ({ url })) };
13851422
}
13861423

1424+
// Prose citations end in punctuation or a closing bracket that is not part of the URL.
1425+
function trimCitationUrl(raw: string): string {
1426+
let url = raw;
1427+
for (;;) {
1428+
const trimmed = url.replace(/[.,;:]+$/u, "");
1429+
const close = trimmed.at(-1);
1430+
const open = close === ")" ? "(" : close === "]" ? "[" : undefined;
1431+
if (open && close && trimmed.split(open).length < trimmed.split(close).length) {
1432+
url = trimmed.slice(0, -1);
1433+
} else if (trimmed === url) {
1434+
return url;
1435+
} else {
1436+
url = trimmed;
1437+
}
1438+
}
1439+
}
1440+
13871441
function providerRequestKind(kind: string | "unknown"): ProviderRequestKind {
13881442
switch (kind) {
13891443
case "execute":
@@ -4579,12 +4633,19 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
45794633
};
45804634
break;
45814635
case "search": {
4582-
const backendSearch = acpBackendWebSearch(rawInputRecord, rawOutputRecord);
4636+
const backendSearch = acpBackendWebSearch(
4637+
rawInputRecord,
4638+
rawOutput,
4639+
typeof toolCall.data.title === "string" ? toolCall.data.title : title,
4640+
);
45834641
if (backendSearch !== undefined) {
4584-
const label = nonEmptyText(toolCall.data.title, title ?? "Web search").replace(
4585-
/:\s*$/u,
4586-
"",
4587-
);
4642+
const label = nonEmptyText(toolCall.data.title, title ?? "Web search")
4643+
.replace(/:\s*$/u, "")
4644+
// Antigravity titles the call "Running search_web".
4645+
.replace(
4646+
/^\s*(?:run(?:ning)?\s+)?(?:web_search|search_web)\s*$/iu,
4647+
"Web search",
4648+
);
45884649
turnItem = {
45894650
...base,
45904651
title:
@@ -4601,6 +4662,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
46014662
};
46024663
break;
46034664
}
4665+
const pathIsQuery = resolvedPathInfo(toolCall)?.fromQuery === true;
46044666
turnItem = {
46054667
...base,
46064668
title:
@@ -4611,7 +4673,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
46114673
}) ?? title,
46124674
type: "file_search",
46134675
...(path === undefined ? {} : { pattern: path }),
4614-
...(path === undefined
4676+
...(path === undefined ||
4677+
(pathIsQuery && textFromUnknown(rawOutput) === undefined)
46154678
? {}
46164679
: {
46174680
results: [

0 commit comments

Comments
 (0)