Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(think): surface gather per-stream failures as typed warning codes (#4166)

Masa1fec58e5fe2f
diff --git a/src/core/think/gather.ts b/src/core/think/gather.ts
index f809c60031..c8afe7599e 100644
--- a/src/core/think/gather.ts
+++ b/src/core/think/gather.ts
@@ -48,6 +48,13 @@ export interface ThinkGatherResult {
takes: TakeHit[];
/** Graph nodes — slugs reachable from anchor within graphDepth. Empty when no anchor. */
graphSlugs: string[];
+ /**
+ * Machine-stable warning codes for per-stream failures (D6: code-only on the
+ * wire; the raw error text goes to stderr/server logs). Lets MCP/remote
+ * callers distinguish "stream errored" from "stream legitimately returned 0"
+ * — the diagnostics counts alone can't. Empty when every stream succeeded.
+ */
+ warnings: string[];
/** Diagnostics for telemetry / `--explain` path (Lane D follow-up). */
diagnostics: {
pagesFromHybrid: number;
@@ -116,12 +123,15 @@ export async function runGather(
// (Direct DB search is fine — those are parameterized queries.)
const sanitizedQuestion = sanitizeQueryForPrompt(opts.question);
+ const warnings: string[] = [];
+
// Stream 1: hybrid page search (existing primitive).
const pagesPromise = hybridSearch(engine, opts.question, {
limit: gatherLimit,
expansion: false, // think provides its own anchor + graph context; no need for re-expansion
...sourceScope,
}).catch((e) => {
+ warnings.push('GATHER_HYBRID_FAILED');
process.stderr.write(`[think.gather] hybrid stream failed: ${(e as Error).message}\n`);
return [] as SearchResult[];
});
@@ -132,6 +142,7 @@ export async function runGather(
takesHoldersAllowList: opts.takesHoldersAllowList,
...sourceScope,
}).catch((e) => {
+ warnings.push('GATHER_TAKES_KEYWORD_FAILED');
process.stderr.write(`[think.gather] takes-keyword stream failed: ${(e as Error).message}\n`);
return [] as TakeHit[];
});
@@ -143,6 +154,7 @@ export async function runGather(
takesHoldersAllowList: opts.takesHoldersAllowList,
...sourceScope,
}).catch((e) => {
+ warnings.push('GATHER_TAKES_VECTOR_FAILED');
process.stderr.write(`[think.gather] takes-vector stream failed: ${(e as Error).message}\n`);
return [] as TakeHit[];
})
@@ -160,6 +172,7 @@ export async function runGather(
return Array.from(slugs);
})
.catch((e) => {
+ warnings.push('GATHER_GRAPH_FAILED');
process.stderr.write(`[think.gather] graph stream failed: ${(e as Error).message}\n`);
return [] as string[];
})
@@ -179,6 +192,7 @@ export async function runGather(
pages: pages.slice(0, gatherLimit),
takes: fusedTakes,
graphSlugs,
+ warnings,
diagnostics: {
pagesFromHybrid: pages.length,
takesFromKeyword: takesKw.length,
diff --git a/src/core/think/index.ts b/src/core/think/index.ts
index b71ab4a486..905a3b0c8e 100644
--- a/src/core/think/index.ts
+++ b/src/core/think/index.ts
@@ -403,6 +403,10 @@ export async function runThink(
...(opts.sourceId !== undefined ? { sourceId: opts.sourceId } : {}),
...(opts.allowedSources !== undefined ? { sourceIds: opts.allowedSources } : {}),
});
+ // D6: per-stream gather failures surface as typed codes (GATHER_*_FAILED);
+ // raw error text stays on stderr. Distinguishes an errored stream from a
+ // legitimately-empty one for MCP/remote callers.
+ for (const w of gather.warnings) warnings.push(w);
// Render evidence blocks for the prompt
const pagesBlock = renderPagesBlock(gather.pages, 600, opts.question);
diff --git a/test/think-pipeline.serial.test.ts b/test/think-pipeline.serial.test.ts
index 3b41f7fbd2..ccce8adcd2 100644
--- a/test/think-pipeline.serial.test.ts
+++ b/test/think-pipeline.serial.test.ts
@@ -141,6 +141,78 @@ describe('runGather', () => {
});
});
+describe('runGather — per-stream typed warnings (GATHER_*_FAILED)', () => {
+ /** Delegating wrapper: listed methods reject; everything else hits the real engine. */
+ function withFailing(methods: string[]): PGLiteEngine {
+ return new Proxy(engine, {
+ get(target, prop) {
+ if (typeof prop === 'string' && methods.includes(prop)) {
+ return async () => { throw new Error(`${prop} boom`); };
+ }
+ const v = Reflect.get(target, prop, target);
+ return typeof v === 'function' ? v.bind(target) : v;
+ },
+ }) as PGLiteEngine;
+ }
+
+ test('clean run reports no gather warnings', async () => {
+ const r = await runGather(engine, { question: 'technical founder' });
+ expect(r.warnings).toEqual([]);
+ });
+
+ test('a throwing hybrid stream surfaces GATHER_HYBRID_FAILED and stays fail-open', async () => {
+ const r = await runGather(withFailing(['searchKeyword']), { question: 'technical founder' });
+ expect(r.warnings).toContain('GATHER_HYBRID_FAILED');
+ expect(r.pages).toEqual([]);
+ // Fail-open: the takes stream keeps working.
+ expect(r.takes.length).toBeGreaterThan(0);
+ });
+
+ test('each failing stream maps to its own code', async () => {
+ const r = await runGather(
+ withFailing(['searchKeyword', 'searchTakes', 'searchTakesVector', 'traversePaths']),
+ {
+ question: 'technical founder',
+ anchor: 'people/alice-example',
+ questionEmbedding: new Float32Array(8),
+ },
+ );
+ expect([...r.warnings].sort()).toEqual([
+ 'GATHER_GRAPH_FAILED',
+ 'GATHER_HYBRID_FAILED',
+ 'GATHER_TAKES_KEYWORD_FAILED',
+ 'GATHER_TAKES_VECTOR_FAILED',
+ ]);
+ expect(r.pages).toEqual([]);
+ expect(r.takes).toEqual([]);
+ expect(r.graphSlugs).toEqual([]);
+ });
+
+ test('runThink folds gather warnings into ThinkResult.warnings', async () => {
+ const stubClient: ThinkLLMClient = {
+ create: async () => ({
+ id: 'msg_gather_warn',
+ type: 'message',
+ role: 'assistant',
+ model: 'stub',
+ stop_reason: 'end_turn',
+ stop_sequence: null,
+ usage: { input_tokens: 1, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0, server_tool_use: null, service_tier: null },
+ content: [{
+ type: 'text',
+ text: JSON.stringify({ answer: 'stubbed answer', citations: [], gaps: [] }),
+ }],
+ }),
+ };
+ const result = await runThink(withFailing(['searchTakes']), {
+ question: 'technical founder',
+ client: stubClient,
+ });
+ expect(result.warnings).toContain('GATHER_TAKES_KEYWORD_FAILED');
+ expect(result.warnings).not.toContain('GATHER_HYBRID_FAILED');
+ });
+});
+
describe('runThink (with stub client)', () => {
test('full pipeline: gather → stub synthesize → result', async () => {
const stubClient: ThinkLLMClient = {