Protocol B append: failed-turn retry (L3–4), git source tokens (L5), image documents (L6 ingest), chat image questions (L6 chat side). TODO.md items now live in .agents/phases/todo/ and the file is cleared. LLM-Generated: true
4.5 KiB
4.5 KiB
Task 03 — Image description: the vision model writes the only embedded text
Phase: 122_image_documents · Source: TODO.md:6 — "the only embedded part of an image will be the summary generated by the model."
Objective
describe_image generates the image's description with the CHAT model (vision), the description becomes BOTH Document.content and Document.summary (so the chunk pipeline embeds exactly that text — and only that text), and a failed description fails soft (skip + count + log, sync continues).
Work
app/rag/summarizer.py— newasync def describe_image(llm, data: bytes, mime: str, settings=None) -> str | None(the summarizer module owns model-text generation; follow the existing summary-call conventions — client, model, timeout, thesummary_max_charscap):- ONE chat-model call (
settings.llm_chat_model— LOCKED A3; the lite summary model is not assumed vision-capable) with messages[{role: "user", content: [{type: "text", text: <DESCRIBE_PROMPT>}, {type: "image_url", image_url: {url: f"data:{mime};base64,{b64}"}}]}]— the multimodal content-list shape the OpenAI-compatible API expects. DESCRIBE_PROMPT(a module constant, pinned by a unit test): a faithful, retrieval-oriented description — what is depicted, any visible text/labels/titles, diagram/table structure, salient details; 2–4 sentences of substance (the description is the ONLY retrievable text of the doc, so it must carry the image's meaning).- Return the stripped text capped at
settings.summary_max_chars(the phase-30 cap — the description IS the summary); returnNoneon any client error, empty response, or non-2xx (the caller fails soft). No retries beyond the SDK's own — a description failure must not stall a sync.
- ONE chat-model call (
app/rag/importer.py— wire the task-02 seam: the image branch'scontent/summarycome fromdescribe_image—- description
None→ skip the doc entirely (no row, noimage_dircopy kept — delete the copy if it was made, or make the copy AFTER a successful description so a failure never leaves an orphan),summary.images_failed += 1,logger.warning("import: image description failed source=%s path=%s", source, rel)— the fail-soft skip (LOCKED A3). - success →
content = description, then the existing_store_summarypath (L582) runs with the description as the summary (theis_summaryposition −1 chunk mirrors it — phase-30 behavior, unchanged), and the normal content chunks embed the description (for a short description that is typically ONE content chunk + the summary chunk — the chunker's existing behavior, no special case). - the phase-118 backfill branch (unchanged image doc,
summary is None) calls the SAME path — a description failure there keeps the doc as-is and logs (no row mutation).
- description
app/rag/llm.py— no new client:describe_imagereuses the existingllm.chat-equivalent client the summarizer already uses for text summaries (verify the exact client method name inapp/rag/summarizer.pyand match it — the multimodal payload is a plainlist[dict]message, so no client change is needed; IF the existing client hard-codes text-onlycontent: strtyping, extend its signature to acceptcontent: str | list— pyright-clean).- ASSUMPTION (A3 re-stated): the CHAT model describes; if the owner's chat model lacks vision,
describe_imagereturnsNone(the SDK errors) and every image doc is skipped + logged — honest, visible failure (theimages_failedcounter in the sync log is the signal).
Testing & Quality
- Unit:
tests/unit/test_image_documents.py(task 06) — with a MOCK client: the prompt shape (text part +image_urldata-URL part, correct model), the cap is applied, whitespace stripped;Noneon mock error / empty string / client exception; the importer's skip path (no row,images_failed == 1, warning logged, no orphan copy) and the success path (content == summary == description,is_summarychunk present, embedding called with the description text — the ONLY text embedded). - Integration: the mock-vision
import_sourcesend-to-end (task 06). - Coverage: >90% on the touched modules.
Completion Criteria
- A fixture PNG through the mock vision client yields a doc whose
content==summary== the description, with its embedding(s) derived from that text only. - A failing mock client skips the doc, bumps
images_failed, logs, and the sync completes with the other docs indexed. uv run pytestgreen;uv run ruff check . && uv run pyrightclean.