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

runGather catches each retrieval stream's failure (hybrid pages,
takes-keyword, takes-vector, graph walk) and degrades fail-open, but
only wrote labeled stderr diagnostics — no typed code reached the
think result's warnings[]. For MCP/remote callers whose stderr goes to
server logs, diagnostics counts alone made pagesFromHybrid: 0
ambiguous (stream errored vs legitimately empty).

Follow the existing D6 code-only-on-the-wire idiom
(QUESTION_EMBED_FAILED / CALIBRATION_FETCH_FAILED /
TRAJECTORY_INJECTION_FAILED in src/core/think/index.ts): each catch
now also pushes a machine-stable code — GATHER_HYBRID_FAILED,
GATHER_TAKES_KEYWORD_FAILED, GATHER_TAKES_VECTOR_FAILED,
GATHER_GRAPH_FAILED — into a new ThinkGatherResult.warnings[], and
runThink folds them into ThinkResult.warnings (same fold pattern as
resolveCitations warnings). Stderr diagnostics, fail-open behavior,
and diagnostics counts are unchanged.


Claude-Session: https://claude.ai/code/session_01VQtBAN6mdxpH1ZnmBQxaVm

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Masa
2026-08-16 20:50:06 +09:00
committed by GitHub
parent 9a19666b2c
commit 1fec58e5fe
3 changed files with 90 additions and 0 deletions

View File

@@ -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,

View File

@@ -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);

View File

@@ -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 = {