fix(invoice-inbox): read whole PDFs (last-page slice + truncation retry) (#2014)
* fix(invoice-inbox): read whole PDFs (last-page slice + truncation retry) PDF extraction read only part of well-structured PDFs, two confirmed mechanisms (21-day prod window: 49 sliced docs, 29 silent empties): - The auto-extract page budget was 3 (Bedrock-latency legacy, issue #553) and the slice kept only the first pages, so multi-page invoices lost the final page where totals, OCR and 'Att betala' sit. The budget is now 8 on pdf-native backends (Claude reads PDFs directly); the slice always keeps the last page. Rasterizing self-host backends keep the old budget of 3. - A max_tokens-truncated model answer was parsed as-is, failed, and became an all-null extraction with no trace. extractFromDocument now reports stop_reason max_tokens / finish_reason length as truncated; the extractor retries once at double AI_EXTRACTION_MAX_TOKENS and logs ai_extraction_truncated either way. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xosyW53HUa9JoFiDayhSk * fix(invoice-inbox): sweep cutoff covers the slower two-call extraction Skeptic finding on #2014: the crash-recovery sweep flipped 'processing' rows to an empty skeleton after 2 minutes, but a deferred extraction can now legitimately run 3-5 minutes (8 native pages plus one truncation retry at a doubled token cap), so the sweep stole the row and the CAS discarded the worker's real result. Cutoff raised to 10 minutes. Also: pages_partial_note made period-agnostic (old rows were extracted from first-pages-only slices, so naming the last page was retroactively wrong for them), and two stale first-pages-only comments updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xosyW53HUa9JoFiDayhSk * fix(invoice-inbox): keep the first extraction response when the retry throws CodeRabbit finding on #2014: a throttled/failed retry call bubbled to the outer catch before rawText was assigned, discarding a first response whose text may parse fine despite the truncation flag. The retry is now caught locally (logged as ai_extraction_retry_failed) and the first result flows on. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xosyW53HUa9JoFiDayhSk --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
e8aa0670ca
commit
a1cafe495f
@@ -114,6 +114,24 @@ describe('extractFromDocument request shape (hosted regression net)', () => {
|
||||
model: 'eu.anthropic.claude-sonnet-5',
|
||||
usage: { inputTokens: 10, outputTokens: 5, cacheCreationInputTokens: 0, cacheReadInputTokens: 7 },
|
||||
})
|
||||
expect(result.ok && result.truncated).toBeFalsy()
|
||||
})
|
||||
|
||||
it('flags a max_tokens stop as truncated so the caller can retry with a higher cap', async () => {
|
||||
mockCreate.mockResolvedValueOnce({
|
||||
content: [{ type: 'text', text: '{"lineItems":[{"description":"cut of' }],
|
||||
stop_reason: 'max_tokens',
|
||||
usage: { input_tokens: 10, output_tokens: 100, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
|
||||
})
|
||||
const svc = createAnthropicFamilyService(readAiConfig())
|
||||
const result = await svc.extractFromDocument({
|
||||
document: { kind: 'text', text: 'x' },
|
||||
system: SYSTEM,
|
||||
instruction: INSTRUCTION,
|
||||
maxTokens: 100,
|
||||
})
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.ok && result.truncated).toBe(true)
|
||||
})
|
||||
|
||||
it('honours the extraction tier override (legacy BEDROCK_MODEL_ID)', async () => {
|
||||
|
||||
@@ -272,7 +272,13 @@ export function createAnthropicFamilyService(cfg: ResolvedAiConfig): AiService {
|
||||
messages: [{ role: 'user', content: buildAnthropicDocumentContent(req.document, req.instruction) }],
|
||||
}
|
||||
const resp = await getClient().messages.create(params)
|
||||
return { ok: true, text: textOf(resp), model, usage: usageOf(resp) }
|
||||
return {
|
||||
ok: true,
|
||||
text: textOf(resp),
|
||||
model,
|
||||
usage: usageOf(resp),
|
||||
...(resp.stop_reason === 'max_tokens' ? { truncated: true } : {}),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +233,7 @@ export function createOpenAICompatibleService(cfg: ResolvedAiConfig): AiService
|
||||
model,
|
||||
usage: usageOf(result),
|
||||
...(built.pagesRasterized ? { pagesRasterized: built.pagesRasterized } : {}),
|
||||
...(result.finishReason === 'length' ? { truncated: true } : {}),
|
||||
}
|
||||
}
|
||||
const result = await generateText({
|
||||
@@ -247,6 +248,7 @@ export function createOpenAICompatibleService(cfg: ResolvedAiConfig): AiService
|
||||
model,
|
||||
usage: usageOf(result),
|
||||
...(built.pagesRasterized ? { pagesRasterized: built.pagesRasterized } : {}),
|
||||
...(result.finishReason === 'length' ? { truncated: true } : {}),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
+14
-1
@@ -149,7 +149,20 @@ export type ExtractionSkipReason =
|
||||
| 'pdf_rasterize_failed'
|
||||
|
||||
export type ExtractFromDocumentResult =
|
||||
| { ok: true; text: string; model: string; usage: AiUsage; pagesRasterized?: number }
|
||||
| {
|
||||
ok: true
|
||||
text: string
|
||||
model: string
|
||||
usage: AiUsage
|
||||
pagesRasterized?: number
|
||||
/**
|
||||
* The model stopped because it hit maxTokens, so `text` is cut mid-answer
|
||||
* (Anthropic stop_reason 'max_tokens', OpenAI finish_reason 'length').
|
||||
* Callers that parse the text should retry with a higher cap instead of
|
||||
* treating the truncated output as a failed parse.
|
||||
*/
|
||||
truncated?: boolean
|
||||
}
|
||||
| { ok: false; skipped: ExtractionSkipReason }
|
||||
|
||||
export interface AiService {
|
||||
|
||||
Reference in New Issue
Block a user