Files
ai-kit/scripts/probe-vision.mjs
CatoandClaude ae260fc51a feat(vision): a picture goes only to a model that can read one (#68)
`complete()` has forwarded `image_url` parts since the first release. What it
never did was CHOOSE a link for them — and `freeChain()` is ordered for text,
so a screenshot meets four Groq links that cannot read it before reaching one
that can. A blind model handed a picture does not fail usefully: it answers the
words around the image, fluently, about nothing.

Three products paid for that absence separately, in three repositories:

  heidi     had the users and REFUSED them — /api/chat returns 400 "Reading a
            picture needs your own model", and byok.ts states as fact that "the
            free chain has no model that can read a picture". Never true of
            free models; true of a chain with no vision routing.
  loki      built a SECOND chain beside this one (config/vision-models.ts) and
            it drifted immediately — never learned about Google, still carries
            a dead Groq seat.
  orangecat built the decision rule and had nobody to call it with. Its own
            capability-gate reason string reads 'no caller sends images'.

The rule, the verified model list and the users existed; none could reach the
other two.

What lands:

- `Provider.visionModels` — which ids accept an image, populated from evidence
  with the provenance stated beside each entry. Groq's empty list is a claim
  (live-probed 2026-08-13: no vision model on this account); OpenRouter's
  `google/gemma-4-26b-a4b-it:free` is the strong kind (probed live, answered
  "Red" correctly); Google's three are declared-not-probed and say so.
- `src/vision.ts` — three verdicts, never two. A model in `models` but not in
  `visionModels` is "no"; a model in NEITHER (env override, private
  deployment, an id a user brought) is "unknown", and unknown is TRIED. Reading
  silence as a denial is the inversion OrangeCat's ADR-0008 named, where the
  stronger the model a user brings, the less the agent can do.
- `complete()` and `completeStream()` route on it through one shared helper, so
  the two paths cannot disagree about which model can see — the same reason
  they already share `walkChain`.
- `NoVisionLinkError`, thrown BEFORE any request when the whole chain is blind,
  naming what it skipped. "No model here can see" and "every vendor failed"
  need different answers and are indistinguishable once both arrive as
  ChainExhaustedError; the first is knowable without spending a token.
- `scripts/probe-vision.mjs` (`pnpm run check:vision`) — a claim in a list is
  worth what the last check was. Sends a real image, asks one word, and treats
  HTTP 200 with EMPTY content as a failure: that is the shape loki found in
  nemotron-nano-12b-v2-vl, and it reads as a successful analysis of a picture
  nobody looked at.

`walkChain`'s chain resolution is extracted as `walkLinks` so the filter and
the walk resolve the same defaults once. Named `walkLinks`, not `resolveChain`
— that name is taken by resolve.ts, which answers a different question.

Inert for text: a plain turn returns the caller's own options object untouched
and costs one typeof per message. Pinned by a test, because breaking that would
break every text caller in the fleet.

301 tests pass, both directions pinned: a declared-blind model must never be
sent a picture, and an unclassified one must never be refused one.


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

Co-authored-by: Claude <noreply@anthropic.com>
2026-09-21 05:39:49 -07:00

160 lines
6.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Can the models this package CLAIMS can see, actually see?
*
* `freeChain()` now carries `visionModels` per provider, and a claim in a list
* is worth exactly as much as the last time somebody checked it. This is the
* check. It sends a real image — a solid red square, inline, no network
* fetch — and asks what colour it is.
*
* ── Why a trivial image and a trivial question ──────────────────────────────
*
* Because the failure this catches is total, not subtle. A model that cannot
* read pictures does not return a slightly worse answer: it answers the text
* and ignores the image entirely, fluently, with no marker that anything was
* dropped. "What colour is this?" has exactly one right answer and cannot be
* guessed from the prompt, so a blind model's confident paragraph fails
* visibly. Judging PROSE QUALITY over a screenshot would need a human; judging
* "did the pixels reach the model" needs one word.
*
* ── The third outcome ────────────────────────────────────────────────────────
*
* loki's own probe of this exact question found a model that answers HTTP 200
* with EMPTY content (`nvidia/nemotron-nano-12b-v2-vl`, 2026-08-13), which a
* naive client reports as a successful analysis of a picture it never read. So
* an empty answer is a FAILURE here, never a pass, and it is named as its own
* outcome rather than folded into "wrong".
*
* Run: node scripts/probe-vision.mjs (pnpm run check:vision)
* Needs a key per provider you want probed; a provider with no key is
* UNCHECKED, which is not a pass.
*
* Costs a handful of tokens and one image tile per model — cheap, but not
* free, so this is NOT on the daily schedule that `check-catalog.mjs` runs on.
* Run it when `visionModels` changes, and when a vision answer looks wrong.
*
* Exit 1 only on a CONFIRMED false claim: a model listed in `visionModels`
* that demonstrably did not read the image. A network failure, a 429 or a
* missing key exits 0 — a check that cannot see is not a check that failed,
* and a pipeline taught to ignore this one is worse than not having it.
*/
import { freeChain, providerModels, modelSeesImages } from "../dist/index.js";
/**
* An 8×8 solid red PNG, base64, written out rather than generated.
*
* Inline so the probe needs no network beyond the vendor itself: a fixture
* fetched from a URL turns "the model cannot see" and "the fixture host is
* down" into the same red line.
*/
const RED_PNG =
"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAHklEQVQoz2P8z8Dwn4GKgIlqJo0aOGrgqIGjBg4WAwFHVQEO2p2+CAAAAABJRU5ErkJggg==";
const QUESTION = "What colour is this image? Answer with one word: the colour name, nothing else.";
const TIMEOUT_MS = 45_000;
/** Did the answer name the colour in the picture? */
function readsRed(text) {
return /\bred\b/i.test(text);
}
async function probe(provider, model, key) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
try {
const res = await fetch(`${provider.baseUrl}/chat/completions`, {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${key}` },
signal: controller.signal,
body: JSON.stringify({
model,
max_tokens: 32,
messages: [
{
role: "user",
content: [
{ type: "text", text: QUESTION },
{ type: "image_url", image_url: { url: `data:image/png;base64,${RED_PNG}` } },
],
},
],
}),
});
if (!res.ok) {
const body = await res.text().catch(() => "");
return { outcome: "could_not_look", detail: `HTTP ${res.status} ${body.slice(0, 120)}` };
}
const json = await res.json();
const text = (json?.choices?.[0]?.message?.content ?? "").trim();
if (!text) return { outcome: "empty", detail: "HTTP 200 with no content" };
if (readsRed(text)) return { outcome: "sees", detail: text.slice(0, 60) };
return { outcome: "blind", detail: text.slice(0, 60) };
} catch (error) {
return { outcome: "could_not_look", detail: error.message };
} finally {
clearTimeout(timer);
}
}
async function main() {
const chain = freeChain();
const lines = [];
let confirmedFalse = 0;
let checked = 0;
for (const provider of chain) {
const key = process.env[provider.keyEnv]?.trim();
if (!key) {
lines.push(` UNCHECKED ${provider.id} — no ${provider.keyEnv} in env`);
continue;
}
// Only the ids this package CLAIMS can see. Probing the declared-blind
// ones would double the cost to re-prove something already decided; the
// claim is what rots.
const claimed = providerModels(provider, process.env).filter(
(m) => modelSeesImages(provider, m) === "yes",
);
if (claimed.length === 0) {
lines.push(` (none claimed) ${provider.id}`);
continue;
}
for (const model of claimed) {
const { outcome, detail } = await probe(provider, model, key);
checked++;
if (outcome === "sees") {
lines.push(` SEES ${provider.id}/${model} — "${detail}"`);
} else if (outcome === "could_not_look") {
lines.push(` UNCHECKED ${provider.id}/${model} — ${detail}`);
} else if (outcome === "empty") {
confirmedFalse++;
lines.push(` EMPTY ${provider.id}/${model} — ${detail} (200 is not an answer)`);
} else {
confirmedFalse++;
lines.push(` BLIND ${provider.id}/${model} — answered "${detail}" (image not read)`);
}
}
}
console.log("Vision claims in freeChain(), probed with a real image:\n");
console.log(lines.join("\n"));
if (confirmedFalse > 0) {
console.error(
`\n${confirmedFalse} model(s) listed in visionModels did NOT read the image. ` +
`Remove them from src/chain.ts or explain the exception beside the id.`,
);
process.exit(1);
}
console.log(
`\n${checked} claim(s) probed, none refuted. ` +
`UNCHECKED is not a pass — re-run with the missing key.`,
);
}
await main();