@apmantza/greedysearch-pi 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/README.md +11 -1
- package/bin/launch-visible.mjs +65 -0
- package/bin/launch.mjs +442 -417
- package/bin/search.mjs +757 -679
- package/extractors/bing-copilot.mjs +490 -374
- package/extractors/common.mjs +703 -596
- package/extractors/consent.mjs +421 -388
- package/extractors/selectors.mjs +55 -54
- package/index.ts +176 -177
- package/package.json +8 -3
- package/skills/greedy-search/skill.md +5 -19
- package/src/fetcher.mjs +666 -652
- package/src/formatters/synthesis.ts +1 -5
- package/src/search/output.mjs +23 -1
- package/src/search/research.mjs +1581 -0
- package/src/search/sources.mjs +488 -466
- package/src/search/synthesis-runner.mjs +52 -46
- package/src/tools/greedy-search-handler.ts +298 -124
- package/test.mjs +971 -534
|
@@ -0,0 +1,1581 @@
|
|
|
1
|
+
// src/search/research.mjs — Iterative deep-research orchestration
|
|
2
|
+
//
|
|
3
|
+
// Research mode borrows the small-loop architecture from open deep-research:
|
|
4
|
+
// plan focused queries, run broad search, extract compact learnings + follow-up
|
|
5
|
+
// directions, then produce a final report. It deliberately reuses GreedySearch's
|
|
6
|
+
// no-API browser engines and source fetchers instead of Firecrawl/OpenAI.
|
|
7
|
+
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import {
|
|
12
|
+
buildSourceRegistry,
|
|
13
|
+
computeCompositeScore,
|
|
14
|
+
mergeFetchDataIntoSources,
|
|
15
|
+
normalizeUrl,
|
|
16
|
+
trimText,
|
|
17
|
+
} from "./sources.mjs";
|
|
18
|
+
import { parseStructuredJson } from "./synthesis.mjs";
|
|
19
|
+
import { runGeminiPrompt } from "./synthesis-runner.mjs";
|
|
20
|
+
|
|
21
|
+
const __dir = fileURLToPath(new URL(".", import.meta.url)).replace(
|
|
22
|
+
/^\/([A-Z]:)/,
|
|
23
|
+
"$1",
|
|
24
|
+
);
|
|
25
|
+
const SEARCH_BIN = join(__dir, "..", "..", "bin", "search.mjs");
|
|
26
|
+
|
|
27
|
+
async function fetchMultipleResearchSources(...args) {
|
|
28
|
+
const { fetchMultipleSources } = await import("./fetch-source.mjs");
|
|
29
|
+
return fetchMultipleSources(...args);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function writeResearchSourcesToFiles(...args) {
|
|
33
|
+
const { writeSourcesToFiles } = await import("./file-sources.mjs");
|
|
34
|
+
return writeSourcesToFiles(...args);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function clampResearchOptions({
|
|
38
|
+
breadth = 3,
|
|
39
|
+
iterations = 2,
|
|
40
|
+
maxSources,
|
|
41
|
+
}) {
|
|
42
|
+
const safeBreadth = clampInt(breadth, 1, 5, 3);
|
|
43
|
+
const safeIterations = clampInt(iterations, 1, 3, 2);
|
|
44
|
+
const safeMaxSources = clampInt(
|
|
45
|
+
maxSources ?? Math.max(5, safeBreadth * safeIterations * 2),
|
|
46
|
+
3,
|
|
47
|
+
12,
|
|
48
|
+
8,
|
|
49
|
+
);
|
|
50
|
+
return {
|
|
51
|
+
breadth: safeBreadth,
|
|
52
|
+
iterations: safeIterations,
|
|
53
|
+
maxSources: safeMaxSources,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function clampInt(value, min, max, fallback) {
|
|
58
|
+
const n = Number.parseInt(String(value ?? ""), 10);
|
|
59
|
+
if (!Number.isFinite(n)) return fallback;
|
|
60
|
+
return Math.min(max, Math.max(min, n));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function normalizeResearchQueries(
|
|
64
|
+
plan,
|
|
65
|
+
originalQuery,
|
|
66
|
+
breadth,
|
|
67
|
+
{ expand = true, includeOriginal = true, exclude = [] } = {},
|
|
68
|
+
) {
|
|
69
|
+
const rawQueries = Array.isArray(plan?.queries) ? plan.queries : [];
|
|
70
|
+
const queries = [];
|
|
71
|
+
const excluded = new Set(
|
|
72
|
+
[...exclude].map((item) => sanitizeResearchQuery(item).toLowerCase()),
|
|
73
|
+
);
|
|
74
|
+
for (const item of rawQueries) {
|
|
75
|
+
const query = typeof item === "string" ? item : item?.query;
|
|
76
|
+
const researchGoal =
|
|
77
|
+
typeof item === "string" ? "" : item?.researchGoal || "";
|
|
78
|
+
addResearchQuery(queries, query, researchGoal, { exclude: excluded });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (includeOriginal) {
|
|
82
|
+
addResearchQuery(queries, originalQuery, "Original user query", {
|
|
83
|
+
prepend: true,
|
|
84
|
+
exclude: excluded,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (expand) {
|
|
89
|
+
const expansionQueries = [
|
|
90
|
+
{
|
|
91
|
+
query: `${originalQuery} official docs GitHub`,
|
|
92
|
+
researchGoal:
|
|
93
|
+
"Find primary project docs, repository details, and maintainer claims.",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
query: `${originalQuery} benchmarks limitations compatibility`,
|
|
97
|
+
researchGoal:
|
|
98
|
+
"Validate performance claims and uncover unsupported APIs or caveats.",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
query: `${originalQuery} alternatives comparison production use cases`,
|
|
102
|
+
researchGoal:
|
|
103
|
+
"Compare against conventional headless browsers and identify when to choose it.",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
query: `${originalQuery} anti bot detection Cloudflare screenshots visual rendering`,
|
|
107
|
+
researchGoal:
|
|
108
|
+
"Check automation risks, rendering gaps, screenshots, and bot-detection behavior.",
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
for (const item of expansionQueries) {
|
|
112
|
+
if (queries.length >= breadth) break;
|
|
113
|
+
addResearchQuery(queries, item.query, item.researchGoal, {
|
|
114
|
+
exclude: excluded,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return queries.slice(0, breadth);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function addResearchQuery(
|
|
123
|
+
queries,
|
|
124
|
+
query,
|
|
125
|
+
researchGoal = "",
|
|
126
|
+
{ prepend = false, exclude = new Set() } = {},
|
|
127
|
+
) {
|
|
128
|
+
if (!query || typeof query !== "string") return;
|
|
129
|
+
const clean = sanitizeResearchQuery(query);
|
|
130
|
+
if (
|
|
131
|
+
!clean ||
|
|
132
|
+
exclude.has(clean.toLowerCase()) ||
|
|
133
|
+
queries.some((q) => q.query.toLowerCase() === clean.toLowerCase())
|
|
134
|
+
) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const item = { query: clean, researchGoal: trimText(researchGoal, 320) };
|
|
138
|
+
if (prepend) queries.unshift(item);
|
|
139
|
+
else queries.push(item);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function sanitizeResearchQuery(query) {
|
|
143
|
+
return collapseWhitespace(stripMarkdownLinks(String(query)));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function stripMarkdownLinks(value) {
|
|
147
|
+
let output = "";
|
|
148
|
+
let index = 0;
|
|
149
|
+
while (index < value.length) {
|
|
150
|
+
const openLabel = value.indexOf("[", index);
|
|
151
|
+
if (openLabel === -1) {
|
|
152
|
+
output += value.slice(index);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
const closeLabel = value.indexOf("]", openLabel + 1);
|
|
156
|
+
if (
|
|
157
|
+
closeLabel === -1 ||
|
|
158
|
+
value[closeLabel + 1] !== "(" ||
|
|
159
|
+
closeLabel === openLabel + 1
|
|
160
|
+
) {
|
|
161
|
+
output += value.slice(index, openLabel + 1);
|
|
162
|
+
index = openLabel + 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const closeUrl = value.indexOf(")", closeLabel + 2);
|
|
166
|
+
if (closeUrl === -1) {
|
|
167
|
+
output += value.slice(index, openLabel + 1);
|
|
168
|
+
index = openLabel + 1;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const url = value.slice(closeLabel + 2, closeUrl).trimStart();
|
|
172
|
+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
|
173
|
+
output += value.slice(index, openLabel + 1);
|
|
174
|
+
index = openLabel + 1;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
output += value.slice(index, openLabel);
|
|
178
|
+
output += value.slice(openLabel + 1, closeLabel);
|
|
179
|
+
index = closeUrl + 1;
|
|
180
|
+
}
|
|
181
|
+
return output;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function collapseWhitespace(value) {
|
|
185
|
+
let output = "";
|
|
186
|
+
let previousWasWhitespace = false;
|
|
187
|
+
for (const char of value) {
|
|
188
|
+
if (char === " " || char === "\t" || char === "\n" || char === "\r") {
|
|
189
|
+
if (!previousWasWhitespace) output += " ";
|
|
190
|
+
previousWasWhitespace = true;
|
|
191
|
+
} else {
|
|
192
|
+
output += char;
|
|
193
|
+
previousWasWhitespace = false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return output.trim();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Tokenize a string into lowercase word tokens for Jaccard similarity.
|
|
201
|
+
*/
|
|
202
|
+
export function tokenSet(value) {
|
|
203
|
+
return new Set(
|
|
204
|
+
String(value)
|
|
205
|
+
.toLowerCase()
|
|
206
|
+
.normalize("NFD")
|
|
207
|
+
.replaceAll(/[\u0300-\u036f]/g, "")
|
|
208
|
+
.split(/[^\w]+/)
|
|
209
|
+
.filter((t) => t.length > 1),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Jaccard similarity between two strings based on word tokens.
|
|
215
|
+
* Returns 0..1 where 1 = identical token sets.
|
|
216
|
+
*/
|
|
217
|
+
export function jaccardSimilarity(a, b) {
|
|
218
|
+
const tokensA = tokenSet(a);
|
|
219
|
+
const tokensB = tokenSet(b);
|
|
220
|
+
const unionSize = new Set([...tokensA, ...tokensB]).size;
|
|
221
|
+
if (unionSize === 0) return 1;
|
|
222
|
+
let intersection = 0;
|
|
223
|
+
for (const t of tokensA) {
|
|
224
|
+
if (tokensB.has(t)) intersection++;
|
|
225
|
+
}
|
|
226
|
+
return intersection / unionSize;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Check if a query is a duplicate or near-duplicate of already-used queries.
|
|
231
|
+
* Returns true if the query should be rejected.
|
|
232
|
+
*/
|
|
233
|
+
export function isDuplicateQuery(
|
|
234
|
+
query,
|
|
235
|
+
usedQueries,
|
|
236
|
+
{ threshold = 0.75, roundIndex = 0, originalQuery = null } = {},
|
|
237
|
+
) {
|
|
238
|
+
const normalized = sanitizeResearchQuery(query).toLowerCase();
|
|
239
|
+
|
|
240
|
+
// Exact duplicate check
|
|
241
|
+
if (usedQueries.has(normalized)) return true;
|
|
242
|
+
|
|
243
|
+
// Reject the original query after round 1
|
|
244
|
+
if (
|
|
245
|
+
originalQuery &&
|
|
246
|
+
roundIndex > 0 &&
|
|
247
|
+
normalized === sanitizeResearchQuery(originalQuery).toLowerCase()
|
|
248
|
+
) {
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Near-duplicate check via Jaccard similarity
|
|
253
|
+
for (const used of usedQueries) {
|
|
254
|
+
if (jaccardSimilarity(normalized, used) >= threshold) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Evaluate research quality using Gemini and return structured assessment.
|
|
264
|
+
*/
|
|
265
|
+
function buildQualityEvaluationPrompt(
|
|
266
|
+
originalQuery,
|
|
267
|
+
rounds,
|
|
268
|
+
allLearnings,
|
|
269
|
+
allGaps,
|
|
270
|
+
) {
|
|
271
|
+
const roundSummaries = rounds.map((round) => ({
|
|
272
|
+
queries: round.queries?.map((q) => q.query || "") || [],
|
|
273
|
+
learnings: round.learnings || [],
|
|
274
|
+
gaps: round.gaps || [],
|
|
275
|
+
}));
|
|
276
|
+
|
|
277
|
+
return [
|
|
278
|
+
"You are evaluating the quality of an iterative research run.",
|
|
279
|
+
"Assess coverage across: official sources, limitations/risks, benchmarks/performance, production usage, and counter-evidence.",
|
|
280
|
+
"Score each dimension 0-10. Overall score 0-10.",
|
|
281
|
+
"Identify remaining knowledge gaps.",
|
|
282
|
+
"Propose targeted next actions (search queries or direct URL fetches) that would most improve the research.",
|
|
283
|
+
"Decide whether to continue or stop.",
|
|
284
|
+
"terminationReason must be one of: quality_threshold | max_rounds | no_novel_actions | insufficient_evidence.",
|
|
285
|
+
"",
|
|
286
|
+
`Original research question: ${originalQuery}`,
|
|
287
|
+
`Rounds completed: ${JSON.stringify(roundSummaries, null, 2)}`,
|
|
288
|
+
`Accumulated learnings: ${JSON.stringify(allLearnings.slice(0, 12), null, 2)}`,
|
|
289
|
+
`Known gaps: ${JSON.stringify(allGaps.slice(0, 8), null, 2)}`,
|
|
290
|
+
"",
|
|
291
|
+
"Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:",
|
|
292
|
+
"BEGIN_JSON",
|
|
293
|
+
JSON.stringify(
|
|
294
|
+
{
|
|
295
|
+
score: 7.5,
|
|
296
|
+
coverage: {
|
|
297
|
+
officialSources: 8,
|
|
298
|
+
limitations: 5,
|
|
299
|
+
benchmarks: 7,
|
|
300
|
+
productionUseCases: 6,
|
|
301
|
+
counterEvidence: 4,
|
|
302
|
+
},
|
|
303
|
+
knowledgeGaps: ["specific gap or missing evidence"],
|
|
304
|
+
shouldContinue: true,
|
|
305
|
+
terminationReason: "quality_threshold",
|
|
306
|
+
nextActions: [
|
|
307
|
+
{ type: "search", query: "targeted search query" },
|
|
308
|
+
{ type: "fetchUrl", url: "https://example.com/primary-doc" },
|
|
309
|
+
],
|
|
310
|
+
},
|
|
311
|
+
null,
|
|
312
|
+
2,
|
|
313
|
+
),
|
|
314
|
+
"END_JSON",
|
|
315
|
+
].join("\n");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Generate fallback queries based on identified gaps when the planner produces insufficient novel actions.
|
|
320
|
+
*/
|
|
321
|
+
export function buildFallbackQueriesFromGaps(
|
|
322
|
+
gaps,
|
|
323
|
+
originalQuery,
|
|
324
|
+
usedQueries,
|
|
325
|
+
nextBreadth,
|
|
326
|
+
roundIndex,
|
|
327
|
+
) {
|
|
328
|
+
const fallbacks = [];
|
|
329
|
+
const angles = [
|
|
330
|
+
{ template: (g) => `${g} official documentation`, label: "official docs" },
|
|
331
|
+
{
|
|
332
|
+
template: (g) => `${g} GitHub issues discussions`,
|
|
333
|
+
label: "community signals",
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
template: (g) => `${g} benchmarks performance comparison`,
|
|
337
|
+
label: "benchmarks",
|
|
338
|
+
},
|
|
339
|
+
{ template: (g) => `${g} limitations risks caveats`, label: "limitations" },
|
|
340
|
+
{
|
|
341
|
+
template: (g) => `${g} production deployment experience`,
|
|
342
|
+
label: "production usage",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
template: (g) => `${originalQuery} ${g} counter evidence`,
|
|
346
|
+
label: "counter-evidence",
|
|
347
|
+
},
|
|
348
|
+
];
|
|
349
|
+
|
|
350
|
+
for (let i = 0; i < gaps.length && fallbacks.length < nextBreadth; i++) {
|
|
351
|
+
const gap = gaps[i];
|
|
352
|
+
const angle = angles[i % angles.length];
|
|
353
|
+
const candidate = angle.template(originalQuery, gap);
|
|
354
|
+
if (!isDuplicateQuery(candidate, usedQueries, { roundIndex })) {
|
|
355
|
+
fallbacks.push({
|
|
356
|
+
query: candidate,
|
|
357
|
+
researchGoal: `Gap-driven: ${gap} (${angle.label})`,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return fallbacks;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function evaluateResearchQuality(
|
|
366
|
+
originalQuery,
|
|
367
|
+
rounds,
|
|
368
|
+
allLearnings,
|
|
369
|
+
allGaps,
|
|
370
|
+
qualityHistory,
|
|
371
|
+
) {
|
|
372
|
+
try {
|
|
373
|
+
const rawEvaluation = await runGeminiPrompt(
|
|
374
|
+
buildQualityEvaluationPrompt(
|
|
375
|
+
originalQuery,
|
|
376
|
+
rounds,
|
|
377
|
+
allLearnings,
|
|
378
|
+
allGaps,
|
|
379
|
+
),
|
|
380
|
+
{ timeoutMs: 120000 },
|
|
381
|
+
);
|
|
382
|
+
const evaluation = parseGeminiJson(rawEvaluation, {});
|
|
383
|
+
|
|
384
|
+
// Normalize score
|
|
385
|
+
const score =
|
|
386
|
+
typeof evaluation.score === "number"
|
|
387
|
+
? Math.min(10, Math.max(0, evaluation.score))
|
|
388
|
+
: qualityHistory.length > 0
|
|
389
|
+
? qualityHistory[qualityHistory.length - 1]
|
|
390
|
+
: 5;
|
|
391
|
+
|
|
392
|
+
const gaps = Array.isArray(evaluation.knowledgeGaps)
|
|
393
|
+
? evaluation.knowledgeGaps
|
|
394
|
+
.map((g) => String(g))
|
|
395
|
+
.filter(Boolean)
|
|
396
|
+
.slice(0, 6)
|
|
397
|
+
: [];
|
|
398
|
+
|
|
399
|
+
const nextActions = Array.isArray(evaluation.nextActions)
|
|
400
|
+
? evaluation.nextActions.slice(0, 5)
|
|
401
|
+
: [];
|
|
402
|
+
|
|
403
|
+
const shouldContinue =
|
|
404
|
+
typeof evaluation.shouldContinue === "boolean"
|
|
405
|
+
? evaluation.shouldContinue
|
|
406
|
+
: score < 8;
|
|
407
|
+
|
|
408
|
+
const terminationReason = evaluation.terminationReason || null;
|
|
409
|
+
|
|
410
|
+
return {
|
|
411
|
+
score,
|
|
412
|
+
coverage: evaluation.coverage || {},
|
|
413
|
+
knowledgeGaps: gaps,
|
|
414
|
+
shouldContinue,
|
|
415
|
+
nextActions,
|
|
416
|
+
terminationReason:
|
|
417
|
+
terminationReason || (score >= 8.5 ? "quality_threshold" : null),
|
|
418
|
+
evaluationError: "",
|
|
419
|
+
};
|
|
420
|
+
} catch (error) {
|
|
421
|
+
process.stderr.write(
|
|
422
|
+
`[greedysearch] Quality evaluation failed: ${error.message}\n`,
|
|
423
|
+
);
|
|
424
|
+
return {
|
|
425
|
+
score:
|
|
426
|
+
qualityHistory.length > 0
|
|
427
|
+
? qualityHistory[qualityHistory.length - 1]
|
|
428
|
+
: 5,
|
|
429
|
+
coverage: {},
|
|
430
|
+
knowledgeGaps: [],
|
|
431
|
+
shouldContinue: true,
|
|
432
|
+
nextActions: [],
|
|
433
|
+
terminationReason: null,
|
|
434
|
+
evaluationError: error.message,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function summarizeEngineAnswers(result) {
|
|
440
|
+
const summaries = {};
|
|
441
|
+
for (const engine of ["perplexity", "bing", "google"]) {
|
|
442
|
+
const value = result?.[engine];
|
|
443
|
+
if (!value) continue;
|
|
444
|
+
summaries[engine] = value.error
|
|
445
|
+
? { status: "error", error: String(value.error) }
|
|
446
|
+
: {
|
|
447
|
+
status: "ok",
|
|
448
|
+
answer: trimText(value.answer || "", 1400),
|
|
449
|
+
sources: Array.isArray(value.sources)
|
|
450
|
+
? value.sources.slice(0, 5).map((s) => ({
|
|
451
|
+
title: trimText(s.title || "", 160),
|
|
452
|
+
url: s.url || "",
|
|
453
|
+
}))
|
|
454
|
+
: [],
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
return summaries;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Action-based research planning prompt.
|
|
462
|
+
* Returns actions: { type: "search" | "fetchUrl", query?, url?, researchGoal? }
|
|
463
|
+
*/
|
|
464
|
+
function buildResearchActionPrompt(
|
|
465
|
+
query,
|
|
466
|
+
breadth,
|
|
467
|
+
learnings = [],
|
|
468
|
+
gaps = [],
|
|
469
|
+
usedUrls = [],
|
|
470
|
+
) {
|
|
471
|
+
const gapSection =
|
|
472
|
+
gaps.length > 0
|
|
473
|
+
? `\nKnown knowledge gaps to target:\n${gaps.map((g) => `- ${g}`).join("\n")}`
|
|
474
|
+
: "";
|
|
475
|
+
const usedUrlSection =
|
|
476
|
+
usedUrls.length > 0
|
|
477
|
+
? `\nAlready fetched URLs (do not re-fetch):\n${usedUrls.map((u) => `- ${u}`).join("\n")}`
|
|
478
|
+
: "";
|
|
479
|
+
|
|
480
|
+
return [
|
|
481
|
+
"You are planning web research actions for a multi-engine search agent.",
|
|
482
|
+
"You can plan two types of actions:",
|
|
483
|
+
' - "search": run a multi-engine SERP search query',
|
|
484
|
+
' - "fetchUrl": directly fetch a specific URL (docs page, GitHub repo, specification, etc.)',
|
|
485
|
+
'Prefer "fetchUrl" when a specific primary source URL is known or obvious.',
|
|
486
|
+
'Use "search" for broad discovery or when specific URLs are unknown.',
|
|
487
|
+
`Return at most ${breadth} actions.`,
|
|
488
|
+
"Avoid near-duplicate search queries and already-fetched URLs.",
|
|
489
|
+
"",
|
|
490
|
+
`User topic: ${query}`,
|
|
491
|
+
learnings.length
|
|
492
|
+
? `\nPrior learnings to build on:\n${learnings.map((l) => `- ${l}`).join("\n")}`
|
|
493
|
+
: "",
|
|
494
|
+
gapSection,
|
|
495
|
+
usedUrlSection,
|
|
496
|
+
"",
|
|
497
|
+
"Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:",
|
|
498
|
+
"BEGIN_JSON",
|
|
499
|
+
JSON.stringify(
|
|
500
|
+
{
|
|
501
|
+
actions: [
|
|
502
|
+
{
|
|
503
|
+
type: "search",
|
|
504
|
+
query: "specific search query",
|
|
505
|
+
researchGoal: "what this action should clarify",
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
type: "fetchUrl",
|
|
509
|
+
url: "https://example.com/docs/relevant-page",
|
|
510
|
+
researchGoal: "extract specific information from this page",
|
|
511
|
+
},
|
|
512
|
+
],
|
|
513
|
+
},
|
|
514
|
+
null,
|
|
515
|
+
2,
|
|
516
|
+
),
|
|
517
|
+
"END_JSON",
|
|
518
|
+
].join("\n");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Validate and normalize a single research action.
|
|
523
|
+
*/
|
|
524
|
+
export function validateAction(action) {
|
|
525
|
+
if (!action || typeof action !== "object") return null;
|
|
526
|
+
const type = action.type;
|
|
527
|
+
const researchGoal = trimText(action.researchGoal || "", 320);
|
|
528
|
+
|
|
529
|
+
if (type === "search") {
|
|
530
|
+
if (action.query == null) return null;
|
|
531
|
+
const query = sanitizeResearchQuery(action.query);
|
|
532
|
+
return query ? { type: "search", query, researchGoal } : null;
|
|
533
|
+
}
|
|
534
|
+
if (type === "fetchUrl") {
|
|
535
|
+
if (action.url == null) return null;
|
|
536
|
+
const url = normalizeUrl(action.url);
|
|
537
|
+
return url ? { type: "fetchUrl", url, researchGoal } : null;
|
|
538
|
+
}
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Execute a research action. Returns { ok, result?, error?, sources?, fetchResult? }
|
|
544
|
+
*/
|
|
545
|
+
async function executeResearchAction(
|
|
546
|
+
action,
|
|
547
|
+
{ locale = null, short = true, usedQueries, usedUrls, maxChars = 8000 } = {},
|
|
548
|
+
) {
|
|
549
|
+
if (action.type === "search") {
|
|
550
|
+
const normalizedQuery = sanitizeResearchQuery(action.query).toLowerCase();
|
|
551
|
+
usedQueries.add(normalizedQuery);
|
|
552
|
+
|
|
553
|
+
try {
|
|
554
|
+
const result = await runFastAllSearch(action.query, { locale, short });
|
|
555
|
+
const sources = buildSourceRegistry(result, action.query);
|
|
556
|
+
return {
|
|
557
|
+
ok: true,
|
|
558
|
+
action,
|
|
559
|
+
result,
|
|
560
|
+
sources,
|
|
561
|
+
};
|
|
562
|
+
} catch (error) {
|
|
563
|
+
return {
|
|
564
|
+
ok: false,
|
|
565
|
+
action,
|
|
566
|
+
error: error.message,
|
|
567
|
+
sources: [],
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (action.type === "fetchUrl") {
|
|
573
|
+
const normalizedUrl = normalizeUrl(action.url);
|
|
574
|
+
if (usedUrls.has(normalizedUrl)) {
|
|
575
|
+
return {
|
|
576
|
+
ok: false,
|
|
577
|
+
action,
|
|
578
|
+
error: `URL already fetched: ${normalizedUrl}`,
|
|
579
|
+
sources: [],
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
try {
|
|
584
|
+
const fetchResult = await fetchSingleResearchSource(
|
|
585
|
+
normalizedUrl,
|
|
586
|
+
maxChars,
|
|
587
|
+
);
|
|
588
|
+
usedUrls.add(normalizedUrl);
|
|
589
|
+
|
|
590
|
+
// Build a source entry from the fetch result
|
|
591
|
+
const domain = getDomainFromUrl(normalizedUrl);
|
|
592
|
+
const source = {
|
|
593
|
+
id: "",
|
|
594
|
+
canonicalUrl: fetchResult.finalUrl || normalizedUrl,
|
|
595
|
+
displayUrl: fetchResult.url || normalizedUrl,
|
|
596
|
+
domain,
|
|
597
|
+
title: fetchResult.title || normalizedUrl,
|
|
598
|
+
engines: ["fetch"],
|
|
599
|
+
engineCount: 1,
|
|
600
|
+
perEngine: {},
|
|
601
|
+
sourceType: classifySourceTypeFromDomain(
|
|
602
|
+
domain,
|
|
603
|
+
fetchResult.title || "",
|
|
604
|
+
),
|
|
605
|
+
isOfficial: false,
|
|
606
|
+
smartScore: 0,
|
|
607
|
+
fetch: {
|
|
608
|
+
attempted: true,
|
|
609
|
+
ok: !fetchResult.error && (fetchResult.contentChars || 0) > 100,
|
|
610
|
+
status: fetchResult.status || null,
|
|
611
|
+
finalUrl: fetchResult.finalUrl || normalizedUrl,
|
|
612
|
+
content: fetchResult.content || "",
|
|
613
|
+
contentChars: fetchResult.contentChars || 0,
|
|
614
|
+
snippet: fetchResult.snippet || "",
|
|
615
|
+
error: fetchResult.error || "",
|
|
616
|
+
},
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
ok: true,
|
|
621
|
+
action,
|
|
622
|
+
result: null,
|
|
623
|
+
sources: [source],
|
|
624
|
+
fetchResult: {
|
|
625
|
+
id: source.id,
|
|
626
|
+
url: normalizedUrl,
|
|
627
|
+
finalUrl: fetchResult.finalUrl || normalizedUrl,
|
|
628
|
+
title: fetchResult.title || "",
|
|
629
|
+
content: fetchResult.content || "",
|
|
630
|
+
contentChars: fetchResult.contentChars || 0,
|
|
631
|
+
snippet: fetchResult.snippet || "",
|
|
632
|
+
status: fetchResult.status || null,
|
|
633
|
+
error: fetchResult.error || "",
|
|
634
|
+
source: fetchResult.source || "http",
|
|
635
|
+
duration: fetchResult.duration || 0,
|
|
636
|
+
},
|
|
637
|
+
};
|
|
638
|
+
} catch (error) {
|
|
639
|
+
return {
|
|
640
|
+
ok: false,
|
|
641
|
+
action,
|
|
642
|
+
error: error.message,
|
|
643
|
+
sources: [],
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
return {
|
|
649
|
+
ok: false,
|
|
650
|
+
action,
|
|
651
|
+
error: `Unknown action type: ${action.type}`,
|
|
652
|
+
sources: [],
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async function fetchSingleResearchSource(url, maxChars) {
|
|
657
|
+
return await fetchSourceContentDirect(url, maxChars);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
async function fetchSourceContentDirect(url, maxChars = 8000) {
|
|
661
|
+
const start = Date.now();
|
|
662
|
+
|
|
663
|
+
// GitHub URL — use API for rich content
|
|
664
|
+
try {
|
|
665
|
+
const { parseGitHubUrl, fetchGitHubContent } = await import(
|
|
666
|
+
"../github.mjs"
|
|
667
|
+
);
|
|
668
|
+
const parsed = parseGitHubUrl(url);
|
|
669
|
+
if (
|
|
670
|
+
parsed &&
|
|
671
|
+
(parsed.type === "root" ||
|
|
672
|
+
parsed.type === "tree" ||
|
|
673
|
+
(parsed.type === "blob" && !parsed.path?.includes(".")))
|
|
674
|
+
) {
|
|
675
|
+
const ghResult = await fetchGitHubContent(url);
|
|
676
|
+
if (ghResult.ok) {
|
|
677
|
+
const { trimContentHeadTail } = await import("../utils/content.mjs");
|
|
678
|
+
const content = trimContentHeadTail(ghResult.content, maxChars);
|
|
679
|
+
return {
|
|
680
|
+
url,
|
|
681
|
+
finalUrl: url,
|
|
682
|
+
status: 200,
|
|
683
|
+
title: ghResult.title,
|
|
684
|
+
snippet: content.slice(0, 320),
|
|
685
|
+
content,
|
|
686
|
+
contentChars: content.length,
|
|
687
|
+
source: "github-api",
|
|
688
|
+
duration: Date.now() - start,
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
} catch {
|
|
693
|
+
// Not a GitHub URL or API failed — fall through to HTTP
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// Standard HTTP fetch
|
|
697
|
+
try {
|
|
698
|
+
const { fetchSourceHttp } = await import("../fetcher.mjs");
|
|
699
|
+
const { trimContentHeadTail } = await import("../utils/content.mjs");
|
|
700
|
+
const httpResult = await fetchSourceHttp(url, { timeoutMs: 10000 });
|
|
701
|
+
if (httpResult.ok) {
|
|
702
|
+
const content = trimContentHeadTail(httpResult.markdown, maxChars);
|
|
703
|
+
return {
|
|
704
|
+
url,
|
|
705
|
+
finalUrl: httpResult.finalUrl,
|
|
706
|
+
status: httpResult.status,
|
|
707
|
+
title: httpResult.title,
|
|
708
|
+
snippet: httpResult.excerpt,
|
|
709
|
+
content,
|
|
710
|
+
contentChars: content.length,
|
|
711
|
+
source: "http",
|
|
712
|
+
duration: Date.now() - start,
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
} catch {
|
|
716
|
+
// HTTP failed — return error
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
return {
|
|
720
|
+
url,
|
|
721
|
+
title: "",
|
|
722
|
+
content: "",
|
|
723
|
+
contentChars: 0,
|
|
724
|
+
snippet: "",
|
|
725
|
+
error: "HTTP fetch failed",
|
|
726
|
+
source: "error",
|
|
727
|
+
duration: Date.now() - start,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function getDomainFromUrl(rawUrl) {
|
|
732
|
+
try {
|
|
733
|
+
const domain = new URL(rawUrl).hostname.toLowerCase();
|
|
734
|
+
return domain.replace(/^www\./, "");
|
|
735
|
+
} catch {
|
|
736
|
+
return "";
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function classifySourceTypeFromDomain(domain, title = "") {
|
|
741
|
+
const { matchesDomain, SOCIAL_HOSTS, COMMUNITY_HOSTS, NEWS_HOSTS } =
|
|
742
|
+
require("./sources.mjs");
|
|
743
|
+
const lowerTitle = title.toLowerCase();
|
|
744
|
+
|
|
745
|
+
if (domain === "github.com" || domain === "gitlab.com") return "repo";
|
|
746
|
+
if (matchesDomain(domain, SOCIAL_HOSTS)) return "social";
|
|
747
|
+
if (matchesDomain(domain, COMMUNITY_HOSTS)) return "community";
|
|
748
|
+
if (matchesDomain(domain, NEWS_HOSTS)) return "news";
|
|
749
|
+
if (
|
|
750
|
+
domain.startsWith("docs.") ||
|
|
751
|
+
domain.startsWith("developer.") ||
|
|
752
|
+
domain.startsWith("developers.") ||
|
|
753
|
+
domain.startsWith("api.") ||
|
|
754
|
+
lowerTitle.includes("documentation") ||
|
|
755
|
+
lowerTitle.includes("docs") ||
|
|
756
|
+
lowerTitle.includes("reference")
|
|
757
|
+
) {
|
|
758
|
+
return "official-docs";
|
|
759
|
+
}
|
|
760
|
+
if (domain.startsWith("blog.")) return "maintainer-blog";
|
|
761
|
+
return "website";
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Normalize a GitHub root/tree URL into specific fetchable pages.
|
|
766
|
+
* Expands github.com/owner/repo into [README, CONTRIBUTING, CHANGELOG, key files].
|
|
767
|
+
*/
|
|
768
|
+
async function normalizeGitHubFetchActions(actions, usedUrls) {
|
|
769
|
+
const normalized = [];
|
|
770
|
+
const { parseGitHubUrl } = await import("../github.mjs");
|
|
771
|
+
|
|
772
|
+
for (const action of actions) {
|
|
773
|
+
if (action.type !== "fetchUrl") {
|
|
774
|
+
normalized.push(action);
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const parsed = parseGitHubUrl(action.url);
|
|
779
|
+
if (!parsed || parsed.type !== "root") {
|
|
780
|
+
normalized.push(action);
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const { owner, repo } = parsed;
|
|
785
|
+
const base = `https://github.com/${owner}/${repo}`;
|
|
786
|
+
|
|
787
|
+
// Check if we already fetched the root
|
|
788
|
+
if (usedUrls.has(base)) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
// Expand into specific fetch targets (limit to avoid overwhelming)
|
|
793
|
+
const targets = [
|
|
794
|
+
base, // root (gets README + tree)
|
|
795
|
+
];
|
|
796
|
+
|
|
797
|
+
// Add docs/CONTRIBUTING/CHANGELOG if they exist in the tree
|
|
798
|
+
const candidatePaths = [
|
|
799
|
+
`${base}/blob/main/CONTRIBUTING.md`,
|
|
800
|
+
`${base}/blob/master/CONTRIBUTING.md`,
|
|
801
|
+
`${base}/blob/main/CHANGELOG.md`,
|
|
802
|
+
`${base}/blob/master/CHANGELOG.md`,
|
|
803
|
+
`${base}/blob/main/docs/README.md`,
|
|
804
|
+
];
|
|
805
|
+
|
|
806
|
+
// Only add a few supplemental targets to avoid excessive fetches
|
|
807
|
+
for (const candidate of candidatePaths) {
|
|
808
|
+
if (targets.length >= 3) break;
|
|
809
|
+
if (!usedUrls.has(candidate)) {
|
|
810
|
+
targets.push(candidate);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
for (const url of targets) {
|
|
815
|
+
normalized.push({
|
|
816
|
+
type: "fetchUrl",
|
|
817
|
+
url,
|
|
818
|
+
researchGoal:
|
|
819
|
+
action.researchGoal || `Fetch GitHub content for ${owner}/${repo}`,
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
return normalized;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Parse action plan from Gemini response into validated actions.
|
|
829
|
+
*/
|
|
830
|
+
export function parseActionPlan(rawJson, breadth) {
|
|
831
|
+
const parsed = parseStructuredJson(rawJson?.answer || "") || {};
|
|
832
|
+
const rawActions = Array.isArray(parsed?.actions) ? parsed.actions : [];
|
|
833
|
+
const actions = [];
|
|
834
|
+
|
|
835
|
+
for (const item of rawActions) {
|
|
836
|
+
const action = validateAction(item);
|
|
837
|
+
if (action && actions.length < breadth) {
|
|
838
|
+
actions.push(action);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
return actions;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Backward-compatible: convert old query-only plan to action list.
|
|
847
|
+
*/
|
|
848
|
+
export function queriesToActions(queries) {
|
|
849
|
+
return (queries || [])
|
|
850
|
+
.map((q) => ({
|
|
851
|
+
type: "search",
|
|
852
|
+
query: typeof q === "string" ? q : q.query,
|
|
853
|
+
researchGoal: typeof q === "string" ? "" : q.researchGoal || "",
|
|
854
|
+
}))
|
|
855
|
+
.filter((a) => a.query);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
function buildLearningPrompt(
|
|
859
|
+
originalQuery,
|
|
860
|
+
roundQueries,
|
|
861
|
+
searchSummaries,
|
|
862
|
+
fetchedSources,
|
|
863
|
+
) {
|
|
864
|
+
const sourceSnippets = fetchedSources
|
|
865
|
+
.filter((source) => source?.content || source?.snippet)
|
|
866
|
+
.slice(0, 10)
|
|
867
|
+
.map((source, index) => ({
|
|
868
|
+
id: `F${index + 1}`,
|
|
869
|
+
title: source.title || "",
|
|
870
|
+
url: source.finalUrl || source.url || "",
|
|
871
|
+
snippet: trimText(source.content || source.snippet || "", 3000),
|
|
872
|
+
}));
|
|
873
|
+
|
|
874
|
+
return [
|
|
875
|
+
"You are extracting compact research state from live multi-engine search results.",
|
|
876
|
+
"Create dense, non-overlapping learnings with exact names, numbers, dates, limitations, and caveats where available.",
|
|
877
|
+
"Also propose follow-up search queries that would most improve confidence or fill gaps.",
|
|
878
|
+
"",
|
|
879
|
+
`Original research question: ${originalQuery}`,
|
|
880
|
+
`Round queries: ${JSON.stringify(roundQueries, null, 2)}`,
|
|
881
|
+
`Engine summaries: ${JSON.stringify(searchSummaries, null, 2)}`,
|
|
882
|
+
`Fetched source snippets: ${JSON.stringify(sourceSnippets, null, 2)}`,
|
|
883
|
+
"",
|
|
884
|
+
"Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:",
|
|
885
|
+
"BEGIN_JSON",
|
|
886
|
+
JSON.stringify(
|
|
887
|
+
{
|
|
888
|
+
learnings: ["concise, information-dense learning"],
|
|
889
|
+
followUpQueries: ["specific next search query"],
|
|
890
|
+
gaps: ["important uncertainty or missing evidence"],
|
|
891
|
+
},
|
|
892
|
+
null,
|
|
893
|
+
2,
|
|
894
|
+
),
|
|
895
|
+
"END_JSON",
|
|
896
|
+
].join("\n");
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
function buildFinalReportPrompt(originalQuery, rounds, sources) {
|
|
900
|
+
const learnings = rounds.flatMap((round) => round.learnings || []);
|
|
901
|
+
const gaps = rounds.flatMap((round) => round.gaps || []);
|
|
902
|
+
const sourceRegistry = sources.slice(0, 12).map((source) => ({
|
|
903
|
+
id: source.id,
|
|
904
|
+
title: source.title,
|
|
905
|
+
domain: source.domain,
|
|
906
|
+
url: source.canonicalUrl,
|
|
907
|
+
type: source.sourceType,
|
|
908
|
+
engines: source.engines,
|
|
909
|
+
fetch: source.fetch?.attempted
|
|
910
|
+
? {
|
|
911
|
+
ok: source.fetch.ok,
|
|
912
|
+
snippet: trimText(source.fetch.snippet || "", 1200),
|
|
913
|
+
publishedTime: source.fetch.publishedTime || "",
|
|
914
|
+
}
|
|
915
|
+
: undefined,
|
|
916
|
+
}));
|
|
917
|
+
|
|
918
|
+
return [
|
|
919
|
+
"You are writing the final research report for an iterative deep-research run.",
|
|
920
|
+
"Produce a thorough markdown report organized into clear sections.",
|
|
921
|
+
"",
|
|
922
|
+
"Use the learnings and source registry below. Every substantive claim MUST be backed by an [S1] citation.",
|
|
923
|
+
'Where engines disagree, surface the conflicting claims explicitly in the "differences" array.',
|
|
924
|
+
'Include a "Key Claims" structure that maps each distinct claim to its supporting source IDs.',
|
|
925
|
+
"",
|
|
926
|
+
"Report structure:",
|
|
927
|
+
"1. ## Summary — A 2-4 sentence executive summary of findings",
|
|
928
|
+
"2. ## Key Findings — The main findings, organized by theme or question, each with inline citations",
|
|
929
|
+
"3. ## Areas of Disagreement — Where engines or sources conflict (if any)",
|
|
930
|
+
"4. ## Limitations & Caveats — Important qualifiers, gaps, or uncertainties",
|
|
931
|
+
"",
|
|
932
|
+
`Original research question: ${originalQuery}`,
|
|
933
|
+
`Learnings: ${JSON.stringify(learnings, null, 2)}`,
|
|
934
|
+
`Known gaps/caveats: ${JSON.stringify(gaps, null, 2)}`,
|
|
935
|
+
`Source registry: ${JSON.stringify(sourceRegistry, null, 2)}`,
|
|
936
|
+
"",
|
|
937
|
+
"Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:",
|
|
938
|
+
"BEGIN_JSON",
|
|
939
|
+
JSON.stringify(
|
|
940
|
+
{
|
|
941
|
+
answer: "markdown report with sections and inline [S1] citations",
|
|
942
|
+
agreement: {
|
|
943
|
+
level: "high|medium|low|mixed|conflicting",
|
|
944
|
+
summary: "one-sentence confidence summary",
|
|
945
|
+
},
|
|
946
|
+
differences: ["notable disagreement or conflict between sources"],
|
|
947
|
+
caveats: ["important caveat or qualification"],
|
|
948
|
+
claims: [
|
|
949
|
+
{
|
|
950
|
+
claim: "specific factual statement from the research",
|
|
951
|
+
support: "strong|moderate|weak|conflicting",
|
|
952
|
+
sourceIds: ["S1", "S2"],
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
recommendedSources: ["S1", "S2"],
|
|
956
|
+
},
|
|
957
|
+
null,
|
|
958
|
+
2,
|
|
959
|
+
),
|
|
960
|
+
"END_JSON",
|
|
961
|
+
].join("\n");
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
async function runFastAllSearch(query, { locale = null, short = true } = {}) {
|
|
965
|
+
const args = [SEARCH_BIN, "all", "--inline", "--stdin", "--fast"];
|
|
966
|
+
if (!short) args.push("--full");
|
|
967
|
+
if (locale) args.push("--locale", locale);
|
|
968
|
+
|
|
969
|
+
return new Promise((resolve, reject) => {
|
|
970
|
+
const proc = spawn(process.execPath, args, {
|
|
971
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
972
|
+
env: { ...process.env, GREEDY_SEARCH_RESEARCH_CHILD: "1" },
|
|
973
|
+
});
|
|
974
|
+
proc.stdin.write(query);
|
|
975
|
+
proc.stdin.end();
|
|
976
|
+
|
|
977
|
+
let out = "";
|
|
978
|
+
let err = "";
|
|
979
|
+
let stderrBuffer = "";
|
|
980
|
+
proc.stdout.on("data", (d) => (out += d));
|
|
981
|
+
proc.stderr.on("data", (d) => {
|
|
982
|
+
err += d;
|
|
983
|
+
stderrBuffer += d.toString();
|
|
984
|
+
const lines = stderrBuffer.split("\n");
|
|
985
|
+
stderrBuffer = lines.pop() || "";
|
|
986
|
+
for (const line of lines) {
|
|
987
|
+
if (shouldForwardChildStderr(line)) {
|
|
988
|
+
process.stderr.write(`${line}\n`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
const t = setTimeout(() => {
|
|
993
|
+
proc.kill();
|
|
994
|
+
reject(new Error(`research child search timed out for: ${query}`));
|
|
995
|
+
}, 140000);
|
|
996
|
+
proc.on("close", (code) => {
|
|
997
|
+
clearTimeout(t);
|
|
998
|
+
if (code !== 0) {
|
|
999
|
+
reject(
|
|
1000
|
+
new Error(err.trim() || `search child exited with code ${code}`),
|
|
1001
|
+
);
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
try {
|
|
1005
|
+
resolve(JSON.parse(out.trim()));
|
|
1006
|
+
} catch {
|
|
1007
|
+
reject(
|
|
1008
|
+
new Error(`Invalid JSON from research child: ${out.slice(0, 200)}`),
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
function dedupeSources(sourceLists) {
|
|
1016
|
+
const seen = new Map();
|
|
1017
|
+
for (const source of sourceLists.flat()) {
|
|
1018
|
+
const canonicalUrl = normalizeUrl(source.canonicalUrl || source.url);
|
|
1019
|
+
if (!canonicalUrl) continue;
|
|
1020
|
+
const existing = seen.get(canonicalUrl);
|
|
1021
|
+
if (!existing) {
|
|
1022
|
+
seen.set(canonicalUrl, { ...source, canonicalUrl });
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
existing.engines = [
|
|
1026
|
+
...new Set([...(existing.engines || []), ...(source.engines || [])]),
|
|
1027
|
+
];
|
|
1028
|
+
existing.engineCount = existing.engines.length;
|
|
1029
|
+
existing.smartScore = Math.max(
|
|
1030
|
+
existing.smartScore || 0,
|
|
1031
|
+
source.smartScore || 0,
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
return Array.from(seen.values())
|
|
1036
|
+
.sort((a, b) => {
|
|
1037
|
+
const diff = computeCompositeScore(b) - computeCompositeScore(a);
|
|
1038
|
+
if (diff !== 0) return diff;
|
|
1039
|
+
return (a.domain || "").localeCompare(b.domain || "");
|
|
1040
|
+
})
|
|
1041
|
+
.slice(0, 12)
|
|
1042
|
+
.map((source, index) => ({ ...source, id: `S${index + 1}` }));
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
function shouldForwardChildStderr(line) {
|
|
1046
|
+
return (
|
|
1047
|
+
/^PROGRESS:/.test(line) ||
|
|
1048
|
+
/^\[greedysearch\]/.test(line) ||
|
|
1049
|
+
/^\[(bing|perplexity|google|gemini)\]/.test(line) ||
|
|
1050
|
+
/^GreedySearch Chrome/.test(line) ||
|
|
1051
|
+
/^Launching GreedySearch Chrome/.test(line) ||
|
|
1052
|
+
/^Headless mode/.test(line) ||
|
|
1053
|
+
/^Ready\.?$/.test(line)
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
function parseGeminiJson(raw, fallback = {}) {
|
|
1058
|
+
return parseStructuredJson(raw?.answer || "") || fallback;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* Audit citations in the final answer against known sources.
|
|
1063
|
+
* Extracts source IDs (e.g. "S1", "S2") from the answer text and verifies
|
|
1064
|
+
* each maps to a valid source with fetch data.
|
|
1065
|
+
*/
|
|
1066
|
+
export function auditCitations(answer, sources) {
|
|
1067
|
+
if (!answer || !Array.isArray(sources)) {
|
|
1068
|
+
return {
|
|
1069
|
+
cited: [],
|
|
1070
|
+
missing: [],
|
|
1071
|
+
unfetched: [],
|
|
1072
|
+
ok: true,
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// Extract source IDs: matches patterns like [S1], [S2], [S3, S4], (S1), S1,
|
|
1077
|
+
// and also F1, F2 (fetched source IDs)
|
|
1078
|
+
const idPattern = /\b[SF](\d+)\b/g;
|
|
1079
|
+
const citedIds = new Set();
|
|
1080
|
+
let match;
|
|
1081
|
+
while ((match = idPattern.exec(answer)) !== null) {
|
|
1082
|
+
citedIds.add(`S${match[1]}`);
|
|
1083
|
+
citedIds.add(`F${match[1]}`);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// Also check for "recommendedSources" or "sources" array in synthesis
|
|
1087
|
+
// Build lookup map
|
|
1088
|
+
const sourceMap = new Map();
|
|
1089
|
+
for (const source of sources) {
|
|
1090
|
+
const id = source?.id;
|
|
1091
|
+
if (id) {
|
|
1092
|
+
sourceMap.set(id, source);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// Check each cited ID
|
|
1097
|
+
const cited = Array.from(citedIds);
|
|
1098
|
+
const missing = [];
|
|
1099
|
+
const unfetched = [];
|
|
1100
|
+
|
|
1101
|
+
for (const id of cited) {
|
|
1102
|
+
const source = sourceMap.get(id);
|
|
1103
|
+
if (!source) {
|
|
1104
|
+
// Try matching by index: S1 -> sources[0]
|
|
1105
|
+
const indexMatch = id.match(/^(S|F)(\d+)$/);
|
|
1106
|
+
if (indexMatch) {
|
|
1107
|
+
const idx = parseInt(indexMatch[2], 10) - 1;
|
|
1108
|
+
if (idx >= 0 && idx < sources.length) {
|
|
1109
|
+
const matched = sources[idx];
|
|
1110
|
+
if (matched) {
|
|
1111
|
+
// Check if source was fetched successfully
|
|
1112
|
+
const fetchOk =
|
|
1113
|
+
matched.fetch?.ok ||
|
|
1114
|
+
(matched.content && matched.content.length > 100) ||
|
|
1115
|
+
(matched.contentChars && matched.contentChars > 100);
|
|
1116
|
+
if (!fetchOk) {
|
|
1117
|
+
unfetched.push(id);
|
|
1118
|
+
}
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
missing.push(id);
|
|
1124
|
+
} else {
|
|
1125
|
+
// Source exists but check if it was fetched
|
|
1126
|
+
const fetchOk =
|
|
1127
|
+
source.fetch?.ok ||
|
|
1128
|
+
(source.content && source.content.length > 100) ||
|
|
1129
|
+
(source.contentChars && source.contentChars > 100);
|
|
1130
|
+
if (!fetchOk) {
|
|
1131
|
+
unfetched.push(id);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
return {
|
|
1137
|
+
cited,
|
|
1138
|
+
missing,
|
|
1139
|
+
unfetched,
|
|
1140
|
+
ok: missing.length === 0,
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
export async function runResearchMode({
|
|
1145
|
+
query,
|
|
1146
|
+
breadth = 3,
|
|
1147
|
+
iterations = 2,
|
|
1148
|
+
maxSources,
|
|
1149
|
+
locale = null,
|
|
1150
|
+
short = false,
|
|
1151
|
+
qualityThreshold = 8.5,
|
|
1152
|
+
} = {}) {
|
|
1153
|
+
const options = clampResearchOptions({ breadth, iterations, maxSources });
|
|
1154
|
+
const rounds = [];
|
|
1155
|
+
let allLearnings = [];
|
|
1156
|
+
let allGaps = [];
|
|
1157
|
+
let activeActions = null;
|
|
1158
|
+
let combinedSources = [];
|
|
1159
|
+
let fetchedSources = [];
|
|
1160
|
+
const usedQueries = new Set();
|
|
1161
|
+
const usedUrls = new Set();
|
|
1162
|
+
const qualityHistory = [];
|
|
1163
|
+
let terminationReason = "max_rounds";
|
|
1164
|
+
|
|
1165
|
+
// Manifest tracking
|
|
1166
|
+
const startedAt = new Date().toISOString();
|
|
1167
|
+
const startMs = Date.now();
|
|
1168
|
+
let totalActionsRun = 0;
|
|
1169
|
+
let totalSearches = 0;
|
|
1170
|
+
let totalFetches = 0;
|
|
1171
|
+
const engineFailures = [];
|
|
1172
|
+
|
|
1173
|
+
process.stderr.write(
|
|
1174
|
+
`[greedysearch] Research mode: breadth ${options.breadth}, iterations ${options.iterations}, qualityThreshold ${qualityThreshold}\n`,
|
|
1175
|
+
);
|
|
1176
|
+
|
|
1177
|
+
for (let roundIndex = 0; roundIndex < options.iterations; roundIndex++) {
|
|
1178
|
+
const roundNumber = roundIndex + 1;
|
|
1179
|
+
const roundBreadth = Math.max(
|
|
1180
|
+
1,
|
|
1181
|
+
Math.ceil(options.breadth / 2 ** roundIndex),
|
|
1182
|
+
);
|
|
1183
|
+
process.stderr.write(`PROGRESS:research:round-${roundNumber}:planning\n`);
|
|
1184
|
+
|
|
1185
|
+
if (!activeActions) {
|
|
1186
|
+
try {
|
|
1187
|
+
// Action-based planning: produces search + fetchUrl actions
|
|
1188
|
+
const rawPlan = await runGeminiPrompt(
|
|
1189
|
+
buildResearchActionPrompt(
|
|
1190
|
+
query,
|
|
1191
|
+
roundBreadth,
|
|
1192
|
+
allLearnings,
|
|
1193
|
+
allGaps,
|
|
1194
|
+
[...usedUrls],
|
|
1195
|
+
),
|
|
1196
|
+
{ timeoutMs: 120000 },
|
|
1197
|
+
);
|
|
1198
|
+
let planActions = parseActionPlan(rawPlan, roundBreadth);
|
|
1199
|
+
|
|
1200
|
+
// On first round, ensure the original query is included
|
|
1201
|
+
if (roundIndex === 0) {
|
|
1202
|
+
planActions.unshift({
|
|
1203
|
+
type: "search",
|
|
1204
|
+
query,
|
|
1205
|
+
researchGoal: "Original user query",
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// Normalize GitHub root URLs into specific fetch targets
|
|
1210
|
+
planActions = await normalizeGitHubFetchActions(planActions, usedUrls);
|
|
1211
|
+
activeActions = planActions;
|
|
1212
|
+
} catch (error) {
|
|
1213
|
+
process.stderr.write(
|
|
1214
|
+
`[greedysearch] Action planning failed, using fallback queries: ${error.message}\n`,
|
|
1215
|
+
);
|
|
1216
|
+
// Fallback: use query-only planning
|
|
1217
|
+
const fallbackQueries = normalizeResearchQueries(
|
|
1218
|
+
null,
|
|
1219
|
+
query,
|
|
1220
|
+
roundBreadth,
|
|
1221
|
+
{
|
|
1222
|
+
includeOriginal: roundIndex === 0,
|
|
1223
|
+
exclude: usedQueries,
|
|
1224
|
+
},
|
|
1225
|
+
);
|
|
1226
|
+
activeActions = queriesToActions(fallbackQueries);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
// Novelty gate: reject exact and near-duplicate search actions
|
|
1231
|
+
const noveltyFiltered = (activeActions || []).filter((action) => {
|
|
1232
|
+
if (action.type === "search") {
|
|
1233
|
+
const pass = !isDuplicateQuery(action.query, usedQueries, {
|
|
1234
|
+
roundIndex,
|
|
1235
|
+
originalQuery: query,
|
|
1236
|
+
});
|
|
1237
|
+
if (!pass) {
|
|
1238
|
+
process.stderr.write(
|
|
1239
|
+
`[greedysearch] Novelty gate rejected search: ${action.query}\n`,
|
|
1240
|
+
);
|
|
1241
|
+
}
|
|
1242
|
+
return pass;
|
|
1243
|
+
}
|
|
1244
|
+
if (action.type === "fetchUrl") {
|
|
1245
|
+
const pass = !usedUrls.has(action.url);
|
|
1246
|
+
if (!pass) {
|
|
1247
|
+
process.stderr.write(
|
|
1248
|
+
`[greedysearch] Novelty gate rejected fetch: ${action.url}\n`,
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
return pass;
|
|
1252
|
+
}
|
|
1253
|
+
return false;
|
|
1254
|
+
});
|
|
1255
|
+
|
|
1256
|
+
const roundActions = noveltyFiltered.slice(0, roundBreadth);
|
|
1257
|
+
const actionRuns = [];
|
|
1258
|
+
for (let i = 0; i < roundActions.length; i++) {
|
|
1259
|
+
const action = roundActions[i];
|
|
1260
|
+
process.stderr.write(
|
|
1261
|
+
`PROGRESS:research:round-${roundNumber}:action-${i + 1}/${roundActions.length}\n`,
|
|
1262
|
+
);
|
|
1263
|
+
process.stderr.write(
|
|
1264
|
+
`[greedysearch] Action ${i + 1}/${roundActions.length} [${action.type}]: ${(action.query || action.url).slice(0, 80)}\n`,
|
|
1265
|
+
);
|
|
1266
|
+
const run = await executeResearchAction(action, {
|
|
1267
|
+
locale,
|
|
1268
|
+
short,
|
|
1269
|
+
usedQueries,
|
|
1270
|
+
usedUrls,
|
|
1271
|
+
maxChars: 8000,
|
|
1272
|
+
});
|
|
1273
|
+
actionRuns.push(run);
|
|
1274
|
+
totalActionsRun++;
|
|
1275
|
+
if (action.type === "search") totalSearches++;
|
|
1276
|
+
if (action.type === "fetchUrl") totalFetches++;
|
|
1277
|
+
if (!run.ok) {
|
|
1278
|
+
engineFailures.push({
|
|
1279
|
+
round: roundNumber,
|
|
1280
|
+
type: action.type,
|
|
1281
|
+
target: action.query || action.url,
|
|
1282
|
+
error: run.error,
|
|
1283
|
+
});
|
|
1284
|
+
process.stderr.write(`[greedysearch] Action failed: ${run.error}\n`);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
// Collect sources from search actions
|
|
1289
|
+
const searchActionRuns = actionRuns.filter(
|
|
1290
|
+
(r) => r.action.type === "search",
|
|
1291
|
+
);
|
|
1292
|
+
const fetchActionRuns = actionRuns.filter(
|
|
1293
|
+
(r) => r.action.type === "fetchUrl",
|
|
1294
|
+
);
|
|
1295
|
+
|
|
1296
|
+
combinedSources = dedupeSources([
|
|
1297
|
+
combinedSources,
|
|
1298
|
+
searchActionRuns.flatMap((run) => run.sources || []),
|
|
1299
|
+
fetchActionRuns.flatMap((run) => run.sources || []),
|
|
1300
|
+
]);
|
|
1301
|
+
|
|
1302
|
+
// Merge direct fetch results into fetchedSources
|
|
1303
|
+
for (const fetchRun of fetchActionRuns) {
|
|
1304
|
+
if (fetchRun.fetchResult) {
|
|
1305
|
+
fetchedSources.push(fetchRun.fetchResult);
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
fetchedSources = dedupeFetchedSources(fetchedSources);
|
|
1309
|
+
|
|
1310
|
+
// Fetch additional top-ranked sources from search results
|
|
1311
|
+
const remainingFetchBudget = Math.max(
|
|
1312
|
+
0,
|
|
1313
|
+
options.maxSources -
|
|
1314
|
+
fetchedSources.filter(
|
|
1315
|
+
(source) => source?.content || source?.contentChars > 100,
|
|
1316
|
+
).length,
|
|
1317
|
+
);
|
|
1318
|
+
if (remainingFetchBudget > 0 && combinedSources.length > 0) {
|
|
1319
|
+
process.stderr.write(`PROGRESS:research:round-${roundNumber}:fetching\n`);
|
|
1320
|
+
const fetched = await fetchMultipleResearchSources(
|
|
1321
|
+
combinedSources,
|
|
1322
|
+
Math.min(remainingFetchBudget, combinedSources.length),
|
|
1323
|
+
8000,
|
|
1324
|
+
Math.min(3, remainingFetchBudget || 1),
|
|
1325
|
+
);
|
|
1326
|
+
fetchedSources = dedupeFetchedSources([...fetchedSources, ...fetched]);
|
|
1327
|
+
combinedSources = mergeFetchDataIntoSources(
|
|
1328
|
+
combinedSources,
|
|
1329
|
+
fetchedSources,
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
// Build round query summary for learning extraction
|
|
1334
|
+
const roundQueries = actionRuns.map((run) => ({
|
|
1335
|
+
query: run.action.query || run.action.url || "",
|
|
1336
|
+
researchGoal: run.action.researchGoal || "",
|
|
1337
|
+
}));
|
|
1338
|
+
|
|
1339
|
+
process.stderr.write(`PROGRESS:research:round-${roundNumber}:learning\n`);
|
|
1340
|
+
let learningPayload = { learnings: [], followUpQueries: [], gaps: [] };
|
|
1341
|
+
let learningError = "";
|
|
1342
|
+
try {
|
|
1343
|
+
const rawLearning = await runGeminiPrompt(
|
|
1344
|
+
buildLearningPrompt(
|
|
1345
|
+
query,
|
|
1346
|
+
roundQueries,
|
|
1347
|
+
searchActionRuns.map((run) => ({
|
|
1348
|
+
query: run.action.query,
|
|
1349
|
+
researchGoal: run.action.researchGoal,
|
|
1350
|
+
error: run.error || "",
|
|
1351
|
+
engines: summarizeEngineAnswers(run.result),
|
|
1352
|
+
})),
|
|
1353
|
+
fetchedSources,
|
|
1354
|
+
),
|
|
1355
|
+
{ timeoutMs: 120000 },
|
|
1356
|
+
);
|
|
1357
|
+
learningPayload = {
|
|
1358
|
+
...learningPayload,
|
|
1359
|
+
...parseGeminiJson(rawLearning, learningPayload),
|
|
1360
|
+
};
|
|
1361
|
+
} catch (error) {
|
|
1362
|
+
learningError = error.message;
|
|
1363
|
+
process.stderr.write(
|
|
1364
|
+
`[greedysearch] Learning extraction failed: ${error.message}\n`,
|
|
1365
|
+
);
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
const learnings = Array.isArray(learningPayload.learnings)
|
|
1369
|
+
? learningPayload.learnings
|
|
1370
|
+
.map((l) => String(l))
|
|
1371
|
+
.filter(Boolean)
|
|
1372
|
+
.slice(0, 8)
|
|
1373
|
+
: [];
|
|
1374
|
+
const gaps = Array.isArray(learningPayload.gaps)
|
|
1375
|
+
? learningPayload.gaps
|
|
1376
|
+
.map((g) => String(g))
|
|
1377
|
+
.filter(Boolean)
|
|
1378
|
+
.slice(0, 6)
|
|
1379
|
+
: [];
|
|
1380
|
+
allLearnings = [...new Set([...allLearnings, ...learnings])];
|
|
1381
|
+
allGaps = [...new Set([...allGaps, ...gaps])];
|
|
1382
|
+
rounds.push({
|
|
1383
|
+
round: roundNumber,
|
|
1384
|
+
actions: actionRuns.map((run) => ({
|
|
1385
|
+
type: run.action.type,
|
|
1386
|
+
query: run.action.query || "",
|
|
1387
|
+
url: run.action.url || "",
|
|
1388
|
+
researchGoal: run.action.researchGoal || "",
|
|
1389
|
+
error: run.error || "",
|
|
1390
|
+
sourceCount: run.sources?.length || 0,
|
|
1391
|
+
})),
|
|
1392
|
+
learnings,
|
|
1393
|
+
gaps,
|
|
1394
|
+
learningError,
|
|
1395
|
+
});
|
|
1396
|
+
|
|
1397
|
+
// Quality evaluation
|
|
1398
|
+
process.stderr.write(`PROGRESS:research:round-${roundNumber}:evaluating\n`);
|
|
1399
|
+
const evaluation = await evaluateResearchQuality(
|
|
1400
|
+
query,
|
|
1401
|
+
rounds,
|
|
1402
|
+
allLearnings,
|
|
1403
|
+
allGaps,
|
|
1404
|
+
qualityHistory,
|
|
1405
|
+
);
|
|
1406
|
+
qualityHistory.push(evaluation.score);
|
|
1407
|
+
process.stderr.write(
|
|
1408
|
+
`[greedysearch] Quality score round ${roundNumber}: ${evaluation.score.toFixed(1)} (shouldContinue: ${evaluation.shouldContinue})\n`,
|
|
1409
|
+
);
|
|
1410
|
+
|
|
1411
|
+
// Early termination
|
|
1412
|
+
if (
|
|
1413
|
+
evaluation.score >= qualityThreshold &&
|
|
1414
|
+
(!evaluation.shouldContinue ||
|
|
1415
|
+
evaluation.terminationReason === "quality_threshold")
|
|
1416
|
+
) {
|
|
1417
|
+
terminationReason = evaluation.terminationReason || "quality_threshold";
|
|
1418
|
+
process.stderr.write(
|
|
1419
|
+
`[greedysearch] Quality threshold ${qualityThreshold} reached (score: ${evaluation.score.toFixed(1)}). Terminating early.\n`,
|
|
1420
|
+
);
|
|
1421
|
+
break;
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
const nextBreadth = Math.max(1, Math.ceil(roundBreadth / 2));
|
|
1425
|
+
|
|
1426
|
+
// Convert learning follow-ups to search actions
|
|
1427
|
+
const followUpActions = (learningPayload.followUpQueries || [])
|
|
1428
|
+
.map((q) => ({
|
|
1429
|
+
type: "search",
|
|
1430
|
+
query: sanitizeResearchQuery(String(q)),
|
|
1431
|
+
researchGoal: "Follow-up from learning extraction",
|
|
1432
|
+
}))
|
|
1433
|
+
.filter((a) => a.query && a.query.toLowerCase() !== query.toLowerCase())
|
|
1434
|
+
.slice(0, nextBreadth);
|
|
1435
|
+
|
|
1436
|
+
// Augment with evaluator's nextActions if follow-ups are insufficient
|
|
1437
|
+
let nextActiveActions = followUpActions;
|
|
1438
|
+
if (
|
|
1439
|
+
nextActiveActions.length < nextBreadth &&
|
|
1440
|
+
evaluation.nextActions.length > 0
|
|
1441
|
+
) {
|
|
1442
|
+
const evaluatorActions = evaluation.nextActions
|
|
1443
|
+
.map((a) => validateAction(a))
|
|
1444
|
+
.filter(Boolean);
|
|
1445
|
+
const merged = [...nextActiveActions, ...evaluatorActions];
|
|
1446
|
+
nextActiveActions = merged.slice(0, nextBreadth);
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// Gap-driven fallback actions (search type)
|
|
1450
|
+
if (nextActiveActions.length < nextBreadth && allGaps.length > 0) {
|
|
1451
|
+
const fallbacks = buildFallbackQueriesFromGaps(
|
|
1452
|
+
allGaps,
|
|
1453
|
+
query,
|
|
1454
|
+
usedQueries,
|
|
1455
|
+
nextBreadth - nextActiveActions.length,
|
|
1456
|
+
roundIndex + 1,
|
|
1457
|
+
);
|
|
1458
|
+
const fallbackActions = fallbacks.map((f) => ({
|
|
1459
|
+
type: "search",
|
|
1460
|
+
query: f.query,
|
|
1461
|
+
researchGoal: f.researchGoal,
|
|
1462
|
+
}));
|
|
1463
|
+
nextActiveActions = [...nextActiveActions, ...fallbackActions].slice(
|
|
1464
|
+
0,
|
|
1465
|
+
nextBreadth,
|
|
1466
|
+
);
|
|
1467
|
+
if (fallbacks.length > 0) {
|
|
1468
|
+
process.stderr.write(
|
|
1469
|
+
`[greedysearch] Generated ${fallbacks.length} gap-driven fallback actions.\n`,
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// If still insufficient, re-plan from accumulated learnings
|
|
1475
|
+
activeActions =
|
|
1476
|
+
nextActiveActions.length >= nextBreadth ? nextActiveActions : null;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
process.stderr.write("PROGRESS:research:final-report\n");
|
|
1480
|
+
let synthesis = {
|
|
1481
|
+
answer: allLearnings.length
|
|
1482
|
+
? allLearnings.map((learning) => `- ${learning}`).join("\n")
|
|
1483
|
+
: "Research completed, but no structured learnings were extracted.",
|
|
1484
|
+
agreement: { level: "mixed", summary: "Research synthesis fallback." },
|
|
1485
|
+
differences: [],
|
|
1486
|
+
caveats: [],
|
|
1487
|
+
claims: [],
|
|
1488
|
+
recommendedSources: combinedSources.slice(0, 4).map((source) => source.id),
|
|
1489
|
+
synthesized: false,
|
|
1490
|
+
};
|
|
1491
|
+
try {
|
|
1492
|
+
const rawReport = await runGeminiPrompt(
|
|
1493
|
+
buildFinalReportPrompt(query, rounds, combinedSources),
|
|
1494
|
+
{ timeoutMs: 180000 },
|
|
1495
|
+
);
|
|
1496
|
+
const parsed = parseGeminiJson(rawReport, {});
|
|
1497
|
+
synthesis = {
|
|
1498
|
+
...synthesis,
|
|
1499
|
+
...parsed,
|
|
1500
|
+
rawAnswer: rawReport.answer || "",
|
|
1501
|
+
geminiSources: rawReport.sources || [],
|
|
1502
|
+
synthesized: true,
|
|
1503
|
+
};
|
|
1504
|
+
} catch (error) {
|
|
1505
|
+
process.stderr.write(
|
|
1506
|
+
`[greedysearch] Final report failed: ${error.message}\n`,
|
|
1507
|
+
);
|
|
1508
|
+
synthesis.error = error.message;
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
const fetchedFiles = await writeResearchSourcesToFiles(fetchedSources);
|
|
1512
|
+
const finishedAt = new Date().toISOString();
|
|
1513
|
+
const durationMs = Date.now() - startMs;
|
|
1514
|
+
|
|
1515
|
+
// Citation audit
|
|
1516
|
+
process.stderr.write("PROGRESS:research:audit-citations\n");
|
|
1517
|
+
const citationAudit = auditCitations(synthesis.answer || "", combinedSources);
|
|
1518
|
+
|
|
1519
|
+
process.stderr.write("PROGRESS:research:done\n");
|
|
1520
|
+
|
|
1521
|
+
return {
|
|
1522
|
+
query,
|
|
1523
|
+
_research: {
|
|
1524
|
+
mode: "iterative",
|
|
1525
|
+
breadth: options.breadth,
|
|
1526
|
+
iterations: options.iterations,
|
|
1527
|
+
maxSources: options.maxSources,
|
|
1528
|
+
rounds,
|
|
1529
|
+
learnings: allLearnings,
|
|
1530
|
+
qualityHistory,
|
|
1531
|
+
terminationReason,
|
|
1532
|
+
qualityThreshold,
|
|
1533
|
+
manifest: {
|
|
1534
|
+
startedAt,
|
|
1535
|
+
finishedAt,
|
|
1536
|
+
durationMs,
|
|
1537
|
+
rounds: rounds.length,
|
|
1538
|
+
actionsRun: totalActionsRun,
|
|
1539
|
+
searches: totalSearches,
|
|
1540
|
+
fetches: totalFetches,
|
|
1541
|
+
sourcesFetched: fetchedSources.filter((s) => s?.contentChars > 100)
|
|
1542
|
+
.length,
|
|
1543
|
+
engineFailures,
|
|
1544
|
+
},
|
|
1545
|
+
},
|
|
1546
|
+
_citationAudit: citationAudit,
|
|
1547
|
+
_sources: combinedSources,
|
|
1548
|
+
_fetchedSources: fetchedFiles,
|
|
1549
|
+
_synthesis: synthesis,
|
|
1550
|
+
_confidence: {
|
|
1551
|
+
sourcesCount: combinedSources.length,
|
|
1552
|
+
fetchedSourceSuccessRate:
|
|
1553
|
+
fetchedSources.length > 0
|
|
1554
|
+
? Number(
|
|
1555
|
+
(
|
|
1556
|
+
fetchedSources.filter((source) => source.contentChars > 100)
|
|
1557
|
+
.length / fetchedSources.length
|
|
1558
|
+
).toFixed(2),
|
|
1559
|
+
)
|
|
1560
|
+
: 0,
|
|
1561
|
+
agreementLevel: synthesis.agreement?.level || "mixed",
|
|
1562
|
+
},
|
|
1563
|
+
};
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
function dedupeFetchedSources(sources) {
|
|
1567
|
+
const seen = new Map();
|
|
1568
|
+
for (const source of sources) {
|
|
1569
|
+
const key =
|
|
1570
|
+
source?.id || normalizeUrl(source?.finalUrl || source?.url || "");
|
|
1571
|
+
if (!key) continue;
|
|
1572
|
+
const existing = seen.get(key);
|
|
1573
|
+
if (
|
|
1574
|
+
!existing ||
|
|
1575
|
+
(source.contentChars || 0) > (existing.contentChars || 0)
|
|
1576
|
+
) {
|
|
1577
|
+
seen.set(key, source);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
return Array.from(seen.values());
|
|
1581
|
+
}
|