Skip to content

A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.

16.3 KB
Raw Blame
// Purpose: v0.3 — automatic selection end to end (spec v0.3, acceptance 3 and 4): with the switch on, does the model
// fetch an omitted image with cam_view_image when the answer needs it, leave the tool alone when what it said before is
// enough, and answer from what it fetched? Is the copy recognized and left out of the next turn?
// A test engine of this checkout (its own port and data folder) rewrites the requests; this checkout's plugin server is
// attached to the test app-server as an MCP server of its own (engine supervision off), for cam_view_image. The switch
// is set in the test's selection file, as the panel would set it. Computer use, the browsers and the other plugins that
// act on the machine are off, the sandbox is read-only, and the tasks are archived at the end.
// Thread A asks outright for a fetch; thread B asks questions that need an image or do not; thread C has one image, so
// nothing else in the context can mislead the reading. Each test image hides a digit in a corner that the descriptions
// leave out. Drafts of the wording were tried the same way before it was built, with the rewrite done by a stand-in.
// v0.6 — thread D sets no switch: a new task has automatic selection on by default. Its question needs two images the
// model has not fetched before, at once (user 2026-10-13: does it fetch every image it needs?).
// v0.8 — the model now notes each new image in a feature line, often with the corner digit ("右下黑色7"), and the
// placeholder carries it: in threads B and C, answering from that note without fetching counts as right too (release
// run, 2026-10-19). Thread D's upload keeps the corners out of the description, so its notes leave them out.
// Input: [--label name] [--lang zh|en] [--port 17897] [--threads A,B,C,D]; CAM_TEST_MODEL, CAM_TEST_EFFORT.
// Output: local/v03/auto-<label>.json (synthetic images only).

import { spawn } from "node:child_process";
import { mkdirSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { setTimeout as sleep } from "node:timers/promises";
import { fileURLToPath } from "node:url";
import { AppServer, findBundledCodex } from "./appserver.ts";
import { codexHome } from "../../plugin/src/adapters/codex/codexconfig.ts";
import { engineHealth } from "../../plugin/src/engine.ts";
import { languageFileOf } from "../../plugin/src/core/language.ts";
import { rememberLang } from "../../plugin/src/adapters/codex/language.ts";
import type { Lang } from "../../plugin/src/core/lang.ts";
import { requestStatsDirOf, selectionDirOf } from "../../plugin/src/core/paths.ts";
import { readRequestStats } from "../../plugin/src/core/request-stats.ts";
import { writeSelection } from "../../plugin/src/core/selection.ts";
import { loadThreadIndex } from "../../plugin/src/adapters/codex/thread-index.ts";
import { archiveThreads, CORNER_IMAGES, runTurn, says, TEST_EFFORT, TEST_MODEL, text } from "./testkit.ts";

type Json = Record<string, any>;
const here = dirname(fileURLToPath(import.meta.url));
const localRoot = join(resolve(here, "../.."), "local");

const SCRIPT = {
  zh: {
    name: (label: string, thread: string) => `[CAM测试] 自动选图自测 ${thread} ${label}`,
    uploadOne: "这是一张测试图,先不用描述,只回复“收到”。",
    explicit: "请调用 cam_view_image 工具查看 IMG-001,然后告诉我图片右下角的数字是几。",
    again: "IMG-001 右下角的数字是几?",
    uploadThree: "这是三张测试图,先不用描述,只回复“收到”。",
    describe: "分别用一句话说说这三张图的背景色和中间的主要形状。只说这两样,不要提角落里的东西。",
    recall: "b.png 的背景是什么颜色?中间是什么形状?",
    detail: "a.png 的右下角有一个数字,是几?",
    byLook: "那张紫色背景的图,右上角的数字是几?",
    textOnly: "把“今天天气很好”翻译成英文。",
    sum: "黄色背景那张图左上角的数字,和 a.png 右下角的数字,加起来是多少?",
    corner: "这张图右上角的数字是几?",
    uploadDescribe: "这是三张测试图。分别用一句话说说它们的背景色和中间的主要形状,只说这两样,不要提角落里的东西。",
    pair: "a.png 右下角的数字,和紫色背景那张图右上角的数字,加起来是多少?",
    recallWords: [/黄/, /圆/],
  },
  en: {
    name: (label: string, thread: string) => `[CAM test] auto image selection ${thread} ${label}`,
    uploadOne: "Here is a test image. Don't describe it yet; just reply \"received\".",
    explicit: "Please call the cam_view_image tool to view IMG-001, then tell me the number in the bottom-right corner of the image.",
    again: "What is the number in the bottom-right corner of IMG-001?",
    uploadThree: "Here are three test images. Don't describe them yet; just reply \"received\".",
    describe: "In one sentence each, give the background color and the main shape in the middle of these three images. Only those two things: don't mention anything in the corners.",
    recall: "What is the background color of b.png, and what shape is in the middle?",
    detail: "There is a number in the bottom-right corner of a.png. What is it?",
    byLook: "In the image with the purple background, what is the number in the top-right corner?",
    textOnly: "Translate \"今天天气很好\" into English.",
    sum: "What do the number in the top-left corner of the yellow image and the number in the bottom-right corner of a.png add up to?",
    corner: "What is the number in the top-right corner of this image?",
    uploadDescribe: "Here are three test images. In one sentence each, give their background color and the main shape in the middle. Only those two things: don't mention anything in the corners.",
    pair: "What do the number in the bottom-right corner of a.png and the number in the top-right corner of the purple image add up to?",
    recallWords: [/yellow/i, /circle|disc|disk/i],
  },
};

async function main(): Promise<void> {
  const option = (name: string, fallback: string) => (process.argv.includes(name) ? process.argv[process.argv.indexOf(name) + 1] : fallback);
  const label = option("--label", "run1");
  const lang: Lang = option("--lang", "zh") === "en" ? "en" : "zh";
  const port = Number(option("--port", "17897"));
  const only = option("--threads", "A,B,C,D").split(",");
  const words = SCRIPT[lang];
  const root = join(localRoot, "v03");
  const data = join(root, `data-${label}`);
  // A folder per run, so runs side by side never rewrite an image another one is uploading.
  const work = join(root, "work", label);
  mkdirSync(work, { recursive: true });
  for (const [name, draw] of Object.entries(CORNER_IMAGES)) writeFileSync(join(work, name), draw());
  // What the panel would have told the plugin: Codex shows this language.
  rememberLang(lang, "codex", languageFileOf(data));
  const sessionsDir = join(codexHome(), "sessions");

  const engine = spawn(process.execPath, [join(here, "..", "..", "plugin", "src", "proxy.ts"), "--port", String(port), "--stay"], { env: { ...process.env, CAM_DATA_DIR: data }, stdio: "ignore", windowsHide: true });
  for (let i = 0; i < 50 && !(await engineHealth(port)); i++) await sleep(200);
  // Nothing that acts on the machine: no computer use, browsers or app tools, no notify program.
  const off = ["computer-use@openai-bundled", "unified-computer-use@openai-bundled", "chrome@openai-bundled", "browser@openai-bundled", "codex-app-tools@openai-bundled"]
    .flatMap((plugin) => ["-c", `plugins.${plugin}.enabled=false`]);
  // This checkout's plugin server, for cam_view_image; it reads the same data folder as the test engine.
  const env = { CODEX_HOME: codexHome(), CAM_DATA_DIR: data, CAM_NO_ENGINE: "1" };
  const plugin = [
    "-c", `mcp_servers.cam_dev.command=${JSON.stringify(process.execPath)}`,
    "-c", `mcp_servers.cam_dev.args=${JSON.stringify([join(here, "..", "..", "plugin", "src", "plugin-server.ts")])}`,
    "-c", `mcp_servers.cam_dev.env={ ${Object.entries(env).map(([name, value]) => `${name} = ${JSON.stringify(value)}`).join(", ")} }`,
    "-c", `mcp_servers.cam_dev.default_tools_approval_mode="approve"`,
    "-c", "mcp_servers.cam_dev.startup_timeout_sec=20",
  ];
  const server = new AppServer(findBundledCodex(), ["-c", `openai_base_url="http://localhost:${port}/backend-api/codex"`, "-c", "features.respect_system_proxy=true", "-c", "notify=[]", "-c", "mcp_servers.node_repl.enabled=false", ...off, ...plugin], { NO_PROXY: undefined, no_proxy: undefined }, join(root, `appserver-${label}.log`));
  const result: Json = { createdAt: new Date().toISOString(), label, lang, model: TEST_MODEL, effort: TEST_EFFORT, threads: {} };
  const created: string[] = [];
  try {
    await server.initialize();
    const start = async (name: string) => {
      const { thread } = await server.request<{ thread: Json }>("thread/start", { model: TEST_MODEL, cwd: work, approvalPolicy: "never", sandbox: "read-only", ephemeral: false });
      created.push(thread.id);
      await server.request("thread/name/set", { threadId: thread.id, name: words.name(label, name) });
      return thread.id as string;
    };
    // As the panel's switch does.
    const switchOn = async (threadId: string) => {
      const at = new Date().toISOString();
      writeSelection({ threadId, unchecked: {}, auto: true, autoAt: at, autoSince: at }, selectionDirOf(data));
      await sleep(2500);
    };
    // A turn, what the model fetched in it (the copies the index found there) and what the engine did to its last request.
    const turn = async (threadId: string, prompt: string, images: string[] = []) => {
      const done = await runTurn(server, threadId, [text(prompt), ...images.map((file) => ({ type: "localImage", path: join(work, file) }))]);
      // The engine records a request once its response has ended, which can be just after the turn completes.
      await sleep(1500);
      const index = loadThreadIndex(sessionsDir, threadId);
      const number = index.turnNumbers.get(done.turnId) ?? null;
      const fetched = [...new Set([...index.copies.values()].filter((copy) => copy.turn === number).map((copy) => copy.of))];
      const rewrite = readRequestStats(threadId, requestStatsDirOf(data))?.lastHttp?.rewrite ?? null;
      return { ...done, fetched, lastRewrite: rewrite && { auto: rewrite.auto ?? false, replaced: (rewrite.replaced ?? []).map((r: Json) => `${r.id}:${r.mode}`), copies: rewrite.copies ?? [] } };
    };
    // v0.8: the feature line the model wrote for each image, by id.
    const notesOf = (threadId: string) => Object.fromEntries([...loadThreadIndex(sessionsDir, threadId).notes].map(([image, notes]) => [image, notes.line]));
    const threads: Record<string, () => Promise<Json>> = {
      A: async () => {
        const id = await start("A");
        const turns: Json = { upload: await turn(id, words.uploadOne, ["a.png"]) };
        await switchOn(id);
        turns.explicit = await turn(id, words.explicit);
        turns.again = await turn(id, words.again);
        return { threadId: id, turns, images: loadThreadIndex(sessionsDir, id).images.map((image) => image.id) };
      },
      B: async () => {
        const id = await start("B");
        const turns: Json = { upload: await turn(id, words.uploadThree, ["a.png", "b.png", "c.png"]) };
        turns.describe = await turn(id, words.describe);
        await switchOn(id);
        for (const name of ["recall", "detail", "byLook", "textOnly", "sum"] as const) turns[name] = await turn(id, words[name]);
        return { threadId: id, turns, images: loadThreadIndex(sessionsDir, id).images.map((image) => image.id), notes: notesOf(id) };
      },
      C: async () => {
        const id = await start("C");
        const turns: Json = { upload: await turn(id, words.uploadOne, ["c.png"]) };
        await switchOn(id);
        turns.corner = await turn(id, words.corner);
        return { threadId: id, turns, notes: notesOf(id) };
      },
      // v0.6: no switch set; described while the images are still this turn's.
      D: async () => {
        const id = await start("D");
        const turns: Json = { upload: await turn(id, words.uploadDescribe, ["a.png", "b.png", "c.png"]) };
        turns.pair = await turn(id, words.pair);
        return { threadId: id, turns };
      },
    };
    for (const name of only) if (threads[name]) result.threads[name] = await threads[name]();
  } finally {
    result.notArchived = await archiveThreads(server, created);
    await server.stop();
    engine.kill();
  }

  const A = result.threads.A?.turns ?? {};
  const B = result.threads.B?.turns ?? {};
  const C = result.threads.C?.turns ?? {};
  const D = result.threads.D?.turns ?? {};
  const reply = (t: Json) => String(t?.reply ?? "");
  const ran = (name: string) => !!result.threads[name];
  // v0.8: the digit is in the image's feature line (a numeral, or the English word).
  const WORDS: Record<number, string> = { 3: "three", 5: "five", 7: "seven" };
  const noted = (thread: string, id: string, n: number) => {
    const line = String(result.threads[thread]?.notes?.[id] ?? "");
    return new RegExp(`(^|[^0-9])${n}([^0-9]|$)`).test(line) || new RegExp(`\\b${WORDS[n]}\\b`, "i").test(line);
  };
  const fetchedOrNoted = (thread: string, t: Json, id: string, n: number) => !!t?.fetched.includes(id) || noted(thread, id, n);
  result.checks = {
    ...(ran("A") ? {
      A_explicitFetchesImg001: A.explicit.fetched.includes("IMG-001"),
      A_explicitAnswers7: says(reply(A.explicit), 7),
      // The copy is no image of its own, and the next turn's requests leave it out.
      A_copyHasNoId: JSON.stringify(result.threads.A.images) === JSON.stringify(["IMG-001"]),
      A_copyLeftOutNextTurn: !!A.again.lastRewrite?.copies.includes("IMG-001"),
      A_againAnswers7: says(reply(A.again), 7),
    } : {}),
    ...(ran("B") ? {
      B_earlierTurnsLeftOut: !!B.recall.lastRewrite?.auto && ["IMG-001", "IMG-002", "IMG-003"].every((id) => B.recall.lastRewrite.replaced.includes(`${id}:plain`)),
      B_recallNoFetch: B.recall.fetched.length === 0 && words.recallWords.every((word) => word.test(reply(B.recall))),
      B_detailFetchesOrNotedImg001: fetchedOrNoted("B", B.detail, "IMG-001", 7),
      B_detailAnswers7: says(reply(B.detail), 7),
      B_byLookFetchesOrNotedImg003: fetchedOrNoted("B", B.byLook, "IMG-003", 5),
      B_byLookAnswers5: says(reply(B.byLook), 5),
      B_textOnlyNoFetch: B.textOnly.fetched.length === 0,
      B_sumFetchesOrNotedImg002: fetchedOrNoted("B", B.sum, "IMG-002", 3),
      B_sumAnswers10: says(reply(B.sum), 10),
    } : {}),
    ...(ran("C") ? {
      C_cornerFetchesOrNotedImg001: fetchedOrNoted("C", C.corner, "IMG-001", 5),
      C_cornerAnswers5: says(reply(C.corner), 5),
    } : {}),
    ...(ran("D") ? {
      D_onByDefault: !!D.pair.lastRewrite?.auto && ["IMG-001", "IMG-002", "IMG-003"].every((id) => D.pair.lastRewrite.replaced.includes(`${id}:plain`)),
      D_pairFetchesBoth: ["IMG-001", "IMG-003"].every((id) => D.pair.fetched.includes(id)),
      D_pairAnswers12: says(reply(D.pair), 12),
    } : {}),
    // A retried reconnect is Codex moving the task to HTTP (the engine declines its WebSocket), not a failure.
    noErrors: [...Object.values(A), ...Object.values(B), ...Object.values(C), ...Object.values(D)].every((t: Json) => t.status === "completed" && !t.errors.some((error: Json) => !error.willRetry)),
  };
  // Fetching an image it already answered about is not wrong, only slower.
  result.notes = {
    refetched: [A.again?.fetched.length ? "A.again" : null, B.sum?.fetched.includes("IMG-001") ? "B.sum" : null].filter(Boolean),
    // v0.8: answered from the feature line, without fetching.
    fromNotes: ([["B.detail", B.detail, "IMG-001"], ["B.byLook", B.byLook, "IMG-003"], ["B.sum", B.sum, "IMG-002"], ["C.corner", C.corner, "IMG-001"]] as Array<[string, Json, string]>)
      .filter(([, t, id]) => t && !t.fetched.includes(id)).map(([name]) => name),
  };
  writeFileSync(join(root, `auto-${label}.json`), JSON.stringify(result, null, 2));
  const brief = (t: Json) => ({ fetched: t.fetched, reply: t.reply });
  console.log(JSON.stringify({
    model: `${TEST_MODEL} ${TEST_EFFORT}`, lang, checks: result.checks, notes: result.notes, notArchived: result.notArchived,
    turns: Object.fromEntries(Object.entries(result.threads as Record<string, Json>).map(([name, thread]) => [name, Object.fromEntries(Object.entries(thread.turns as Record<string, Json>).map(([key, t]) => [key, brief(t)]))])),
  }, null, 2));
}

await main();