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.

9.6 KB
Raw Blame
// Purpose: v0.1 — does unchecking an image the model already described make it doubt itself? Replays the user's
// report of 2026-09-25 with the new placeholder wording: see the image → describe it → uncheck it → "can you see it?"
// → recall the earlier description → ask a detail never described (must ask for the image instead of guessing).
// Runs a test engine of this checkout on its own port and data folder, so the user's running engine is not touched.
// v0.1-14 — --lang en runs the same turns in English, with the engine told the panel shows English, so the English
// text for the model is checked the same way.
// v0.1-25 — then the user checks a.png again while b.png, from the same message, is left out, and asks the detail
// again: the model must answer from the image, not say it cannot see it (GPT-6 Luna did, user 2026-09-28).
// Input: [--label name] [--port 17899] [--lang zh|en]. Output: local/v01/placeholder-<label>.json (synthetic test thread
// 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 { 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 { archiveThreads, runTurn, TEST_EFFORT, TEST_MODEL, testImages, text } from "./testkit.ts";
import { loadThreadIndex } from "../../plugin/src/adapters/codex/thread-index.ts";

type Json = Record<string, any>;
const here = dirname(fileURLToPath(import.meta.url));
const localRoot = join(resolve(here, "../.."), "local");
// Taking back an earlier answer, in the ways the model phrased it in the report (and their English counterparts).
const RETRACTION = /猜|不可靠|没有依据|错误回答|说错|抱歉|收回|撤回|并不准确/;
const RETRACTION_EN = /\b(guess(ed|ing)?|unreliable|no basis|mistaken|was wrong|incorrect|apologi[sz]e|sorry|take (it|that|them) back|retract)/i;

// The five turns and what each reply must show, per language.
const SCRIPT = {
  zh: {
    name: (label: string) => `[CAM测试] v0.1 占位符自测 ${label}`,
    upload: "这是两张测试图,先不用描述,只回复“收到”。",
    describe: "a.png 是什么背景色、什么主要形状?角落里有什么小标记,在哪个角?不要调用任何工具。",
    canYouSee: "现在能看到a.png吗?",
    recall: "你刚才说 a.png 的背景是什么颜色、中间是什么形状?",
    newDetail: "a.png 里那个红色正方形的四个角,是直角还是圆角?",
    recheck: "我已经重新勾上 a.png 了。那个红色正方形的四个角,是直角还是圆角?",
    colors: [/白/, /红/, /黑/],
    recalled: [/白/, /(方|正方)/],
    retraction: RETRACTION,
    asks: (id: string) => new RegExp(`需要\\s*[“"*]*\\s*${id}`),
    corners: /直角|尖角/,
    cannotSee: /看不到|无法看到|没有看到|看不见/,
  },
  en: {
    name: (label: string) => `[CAM test] v0.1 placeholder self-test ${label}`,
    upload: "Here are two test images. Don't describe them yet; just reply \"received\".",
    describe: "What is the background color of a.png, and what is its main shape? What small mark is in a corner, and which corner? Do not call any tools.",
    canYouSee: "Can you see a.png now?",
    recall: "What did you say earlier about the background color of a.png and the shape in the middle?",
    newDetail: "Are the four corners of the red square in a.png sharp right angles or rounded?",
    recheck: "I checked a.png again. Are the four corners of the red square sharp right angles or rounded?",
    colors: [/white/i, /red/i, /black/i],
    recalled: [/white/i, /square/i],
    retraction: RETRACTION_EN,
    asks: (id: string) => new RegExp(`need\\s*["“*]*\\s*${id}`, "i"),
    corners: /sharp|right angle|square corners|90/i,
    cannotSee: /can[’']?t see|cannot see|can not see|not visible|don[’']?t see|unable to see/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 port = Number(option("--port", "17899"));
  const lang = option("--lang", "zh") === "en" ? "en" : "zh";
  const words = SCRIPT[lang];
  const root = join(localRoot, "v01");
  const data = join(root, `data-${label}`);
  const work = join(root, "work");
  mkdirSync(work, { recursive: true });
  writeFileSync(join(work, "a.png"), testImages.redSquare());
  writeFileSync(join(work, "b.png"), testImages.blueCircle());
  // What the panel would have told the plugin: Codex shows this language.
  rememberLang(lang, "codex", languageFileOf(data));

  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);
  const startedAt = new Date().toISOString();
  const server = new AppServer(findBundledCodex(), ["-c", `openai_base_url="http://localhost:${port}/backend-api/codex"`, "-c", "features.respect_system_proxy=true"], { NO_PROXY: undefined, no_proxy: undefined }, join(root, `appserver-${label}.log`));
  const result: Json = { createdAt: startedAt, label, lang, model: TEST_MODEL, effort: TEST_EFFORT, turns: {} };
  try {
    await server.initialize();
    const { thread } = await server.request<{ thread: Json }>("thread/start", { model: TEST_MODEL, cwd: work, approvalPolicy: "never", sandbox: "read-only", ephemeral: false });
    const threadId: string = thread.id;
    result.threadId = threadId;
    await server.request("thread/name/set", { threadId, name: words.name(label) });
    // Manual mode on purpose, from the first turn: from v0.6 a new task has automatic selection on by default, which
    // would leave the uploads out of the describe turn already (v0.8 release run, 2026-10-19).
    writeSelection({ threadId, unchecked: {}, auto: false }, selectionDirOf(data));
    const turn = async (name: string, prompt: string, images: string[] = []) => {
      result.turns[name] = await runTurn(server, threadId, [text(prompt), ...images.map((file) => ({ type: "localImage", path: join(work, file) }))]);
    };

    await turn("upload", words.upload, ["a.png", "b.png"]);
    await turn("describe", words.describe);
    const index = loadThreadIndex(join(codexHome(), "sessions"), threadId);
    const a = index.images.find((image) => image.name === "a.png")!;
    result.aId = a.id;
    writeSelection({ threadId, unchecked: { [a.key]: { id: a.id, at: new Date().toISOString() } }, auto: false }, selectionDirOf(data));
    await sleep(2500);
    await turn("canYouSee", words.canYouSee);
    await turn("recall", words.recall);
    await turn("newDetail", words.newDetail);
    // The engine records a request once its response has ended, which can be just after the turn completes.
    const lastRewrite = async () => { await sleep(1500); return readRequestStats(threadId, join(data, "state", "requests"))?.lastHttp?.rewrite ?? null; };
    result.lastRequest = await lastRewrite();
    const b = index.images.find((image) => image.name === "b.png")!;
    result.bId = b.id;
    writeSelection({ threadId, unchecked: { [b.key]: { id: b.id, at: new Date().toISOString() } }, auto: false }, selectionDirOf(data));
    await sleep(2500);
    await turn("recheck", words.recheck);
    result.recheckRequest = await lastRewrite();
  } finally {
    if (result.threadId) result.notArchived = await archiveThreads(server, [result.threadId]);
    await server.stop();
    engine.kill();
  }

  const reply = (name: string) => String(result.turns[name]?.reply ?? "");
  result.checks = {
    describeSeesImage: words.colors.every((color) => color.test(reply("describe"))),
    canYouSeeNoRetraction: !words.retraction.test(reply("canYouSee")),
    recallKeepsEarlierAnswer: words.recalled.every((word) => word.test(reply("recall"))) && !words.retraction.test(reply("recall")),
    newDetailAsksForImage: words.asks(result.aId).test(reply("newDetail")),
    lastRequestReplacedA: JSON.stringify(result.lastRequest?.replaced?.map((r: Json) => `${r.id}:${r.mode}`)) === JSON.stringify([`${result.aId}:plain`]),
    recheckRequestSentA: JSON.stringify(result.recheckRequest?.replaced?.map((r: Json) => `${r.id}:${r.mode}`)) === JSON.stringify([`${result.bId}:plain`]),
    recheckAnswersFromImage: words.corners.test(reply("recheck")) && !words.cannotSee.test(reply("recheck")),
    // A retried reconnect is Codex moving the task to HTTP (the engine declines its WebSocket), not a failure.
    noErrors: Object.values(result.turns as Record<string, Json>).every((t) => t.status === "completed" && !t.errors.some((error: Json) => !error.willRetry)),
  };
  writeFileSync(join(root, `placeholder-${label}.json`), JSON.stringify(result, null, 2));
  console.log(JSON.stringify({ threadId: result.threadId, model: `${TEST_MODEL} ${TEST_EFFORT}`, checks: result.checks, replies: Object.fromEntries(Object.entries(result.turns as Record<string, Json>).map(([name, t]) => [name, t.reply])) }, null, 2));
}

await main();