talentos 1.4.20 → 1.4.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,7 +14,7 @@ import { codexRoomArgs, ROOM_DIRECT_TOOLS, ROOM_PROXY_ONLY_TOOLS, } from "./code
14
14
  import { binaryIdentity, CodexBoundaryError, CodexProbeInterrupted, codexWebProven, probeCodexBoundary, } from "./codex-probe.js";
15
15
  import { prepareCodexOwnerLaunch } from "./native/codex-launch.js";
16
16
  import { prepareOwnerLaunch } from "./native/launch.js";
17
- import { readReceipts } from "./native/receipts.js";
17
+ import { readReceipts, readWorkspaceResultReceipts, } from "./native/receipts.js";
18
18
  import { orderPacketForPrompt } from "./packet-order.js";
19
19
  import { classifyProviderFailure, matchFailureText, ProviderFailureError, } from "./provider-failure.js";
20
20
  import { READ_PAGE_TOOL, ROOM_WEB_MCP_ENTRY, ROOM_WEB_SERVER, } from "./room-page.js";
@@ -514,9 +514,11 @@ export async function runProvider(input) {
514
514
  askSocket: watching?.socketPath,
515
515
  binary: input.owner.codex.binary,
516
516
  bundledCatalog: input.owner.codex.bundledCatalog,
517
+ codexHome: input.owner.codex.home,
517
518
  directory: dirname(workDir),
518
519
  env: input.owner.env,
519
520
  home: input.owner.home,
521
+ imagesProven: input.owner.codex.imagesProven,
520
522
  instructions,
521
523
  jobId: job.jobId,
522
524
  prompt,
@@ -813,8 +815,13 @@ export async function runProvider(input) {
813
815
  stopWatching?.();
814
816
  if (ownerLaunch && input.receipts) {
815
817
  // Best effort: a lost record never changes how the job ended.
816
- await readReceipts(ownerLaunch.jobDir)
817
- .then((rows) => (rows.length > 0 ? input.receipts?.(rows) : undefined))
818
+ await Promise.all([
819
+ readReceipts(ownerLaunch.jobDir),
820
+ input.owner
821
+ ? readWorkspaceResultReceipts(input.owner.workspace)
822
+ : Promise.resolve([]),
823
+ ])
824
+ .then(([rows, result]) => input.receipts?.(rows, result))
818
825
  .catch(() => undefined);
819
826
  }
820
827
  await ownerLaunch?.cleanup();
@@ -23,7 +23,7 @@ import { binaryIdentity, CodexBoundaryError, codexAdvertisedTools, digest, execu
23
23
  // Anything else (no OS sandbox, a changed tool surface) leaves images off, and
24
24
  // the job runs as before. The OS sandbox is part of the proof's cache key, so a
25
25
  // computer that loses it is proven again, and fails.
26
- const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", "base64");
26
+ export const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", "base64");
27
27
  const REQUESTS_MAX = 80;
28
28
  // What the model asks the image tool for, in order: the outside reference must
29
29
  // be refused, the inside one must go through, the plain one must generate.
@@ -42,7 +42,7 @@ function chunk(type, data) {
42
42
  return Buffer.concat([length, body, crc]);
43
43
  }
44
44
  /** A 1x1 PNG of a random colour: no other file, and no response, equals it. */
45
- function randomPng() {
45
+ export function randomPng() {
46
46
  const [red, green, blue] = randomBytes(3);
47
47
  const header = Buffer.alloc(13);
48
48
  header.writeUInt32BE(1, 0);
@@ -60,7 +60,7 @@ function randomPng() {
60
60
  * this computer now: bubblewrap on Linux, Seatbelt on macOS. Part of the proof's
61
61
  * key, so a proof made with it is not honored once it is gone or replaced.
62
62
  */
63
- async function osSandboxState() {
63
+ export async function osSandboxState() {
64
64
  let candidates = [];
65
65
  if (process.platform === "linux") {
66
66
  candidates = (process.env.PATH ?? "")
@@ -114,7 +114,7 @@ export async function codexImagesProven(binary, catalog) {
114
114
  }
115
115
  const b64url = (value) => Buffer.from(JSON.stringify(value)).toString("base64url");
116
116
  // A ChatGPT-mode login Codex will use but cannot refresh: it holds no secret.
117
- function fakeLogin() {
117
+ export function fakeLogin() {
118
118
  const token = `${b64url({ alg: "none" })}.${b64url({
119
119
  email: "probe@example.com",
120
120
  exp: 4_102_444_800,
@@ -17,6 +17,8 @@ export const PROBE_REVISION = 3;
17
17
  // to /mcp, so the stand-in lists later than that: only a launch that makes Codex
18
18
  // wait for the room server passes.
19
19
  export const SLOW_LIST_MS = 1500;
20
+ /** Test hook: when the boundary proof's stand-in lists. A test that shortens it shortens its fixture's grace with it. */
21
+ export const proofTiming = { slowListMs: SLOW_LIST_MS };
20
22
  const RESOURCE_REFUSAL = /Method not found|unknown MCP server/u;
21
23
  // Codex 0.165+ refuses a read aimed at a server it does not have with this line
22
24
  // alone, without the "resources/read failed:" prefix older ones put first
@@ -545,7 +547,7 @@ async function proveCodexBoundary(binary, version, catalog, stop, web = false) {
545
547
  catalogPath,
546
548
  mcpCommand: process.execPath,
547
549
  // One slow listing is enough to prove Codex waits; keep resume quick.
548
- mcpArgs: [mcp, String(turn === 0 ? SLOW_LIST_MS : 0)],
550
+ mcpArgs: [mcp, String(turn === 0 ? proofTiming.slowListMs : 0)],
549
551
  prompt: "Verify the room boundary.",
550
552
  sessionId: session,
551
553
  web,
@@ -26,7 +26,7 @@ const COLLAB_TOOL = /agent|spawn/u;
26
26
  const RUN_MS = 30_000;
27
27
  // The room MCP stand-in advertises these; run_command's real classification and
28
28
  // one-shot execution are covered by codex-run-command.test.ts.
29
- const STANDIN_ROOM_TOOLS = [
29
+ export const STANDIN_ROOM_TOOLS = [
30
30
  "read_room",
31
31
  "read_file",
32
32
  "create_file",
@@ -69,7 +69,7 @@ process.stdin.on("end", () => {
69
69
  // calls, and returns ok. run_command execution and classification live in
70
70
  // codex-run-command.ts and its test; here we only prove the tool is exposed and
71
71
  // the exact command string reaches the proxy (so the approval shows what runs).
72
- function roomServerSource(recordPath, listDelayMs) {
72
+ export function roomServerSource(recordPath, listDelayMs) {
73
73
  const all = [...STANDIN_ROOM_TOOLS, ...ROOM_LOCAL_TOOLS];
74
74
  return `import {Server} from ${JSON.stringify(import.meta.resolve("@modelcontextprotocol/sdk/server/index.js"))};
75
75
  import {StdioServerTransport} from ${JSON.stringify(import.meta.resolve("@modelcontextprotocol/sdk/server/stdio.js"))};
@@ -252,7 +252,7 @@ function textOf(output) {
252
252
  }
253
253
  return "";
254
254
  }
255
- async function bundledCatalog(binary, home, signal) {
255
+ export async function bundledCatalog(binary, home, signal) {
256
256
  return await new Promise((resolve, reject) => {
257
257
  const child = spawn(binary, ["debug", "models", "--bundled"], {
258
258
  env: { CODEX_HOME: home, HOME: home, PATH: process.env.PATH },
@@ -11,9 +11,13 @@ import { ROOM_LOCAL_TOOLS, ROOM_TOOLS } from "../codex-policy.js";
11
11
  // without exec_command). apply_patch and view_image stay, both hooked.
12
12
  // 2. run_command, served by our LOCAL stdio proxy, is the only way to run a
13
13
  // command; the proxy classifies it through the same hook as Claude's Bash.
14
- // 3. Everything the hook cannot see is turned off (web search, image
15
- // generation, goals, tool search, request_user_input, multi-agent), so the
16
- // session's whole tool list is exactly the allowlist below.
14
+ // 3. Everything the hook cannot see is turned off (web search, goals, tool
15
+ // search, request_user_input, multi-agent), so the session's whole tool
16
+ // list is exactly the allowlist below.
17
+ // Image generation is the one tool the launch may add: the hook sees each call
18
+ // and its reference images before Codex reads them, on a Codex that passed the
19
+ // owner image proof (codex-owner-images.ts). It is on only for the owner's own
20
+ // ask.
17
21
  export const ROOM_SERVER = "talentos_room";
18
22
  // R2 (#10987): the exact Codex versions allowed full setup live on the server
19
23
  // (convex/externalAgents/codexFence.model.ts) and arrive with each heartbeat and each
@@ -47,16 +51,19 @@ export function codexFenceEligible(versionOutput, tested) {
47
51
  * are inert because our room server exposes no resources, and the hook holds
48
52
  * read_mcp_resource like any unknown tool.
49
53
  */
50
- export function ownerToolAllowlist(roomTools = [...ROOM_TOOLS, ...ROOM_LOCAL_TOOLS]) {
54
+ export function ownerToolAllowlist(roomTools = [...ROOM_TOOLS, ...ROOM_LOCAL_TOOLS], { images = false } = {}) {
51
55
  return new Set([
52
56
  "apply_patch",
53
57
  "view_image",
54
58
  "list_mcp_resources",
55
59
  "list_mcp_resource_templates",
56
60
  "read_mcp_resource",
61
+ ...(images ? [OWNER_IMAGE_TOOL] : []),
57
62
  ...roomTools.map((tool) => `mcp__${ROOM_SERVER}__${tool}`),
58
63
  ]);
59
64
  }
65
+ /** Codex's image tool as a job's tool list names it (`image_gen.imagegen`). */
66
+ export const OWNER_IMAGE_TOOL = "image_gen__imagegen";
60
67
  /**
61
68
  * The owner-native catalog: the bundled catalog with every model's shell
62
69
  * removed (shell_type="disabled", node_repl off) and the hosted web-search tool
@@ -93,11 +100,11 @@ export function ownerCatalog(bundled) {
93
100
  // Everything the hook cannot see, turned off so the tool list is the allowlist.
94
101
  // Kept ON: apply_patch (freeform, hooked every path), view_image (hooked), the
95
102
  // room MCP server (room tools + run_command). Multi-agent is off in slice 1, so
96
- // no subagent can reach a tool the parent cannot; it is a stated v1 gap.
103
+ // no subagent can reach a tool the parent cannot; it is a stated v1 gap. Image
104
+ // generation is the launch's own input (`images`).
97
105
  const OWNER_FEATURE_ARGS = [
98
106
  'web_search="disabled"',
99
107
  "features.view_image=true",
100
- "features.image_generation=false",
101
108
  "features.apps=false",
102
109
  "features.plugins=false",
103
110
  "features.tool_search=false",
@@ -129,6 +136,8 @@ export function codexOwnerArgs(input) {
129
136
  "-c",
130
137
  'approval_policy="never"',
131
138
  ...OWNER_FEATURE_ARGS.flatMap((flag) => ["-c", flag]),
139
+ "-c",
140
+ `features.image_generation=${input.images === true}`,
132
141
  // Never list a skill the agent cannot open (#14449): include_instructions
133
142
  // hides the whole list, skip_host_skill_discovery the owner's own folders.
134
143
  ...(input.ownAsk
@@ -3,7 +3,7 @@ import { join } from "node:path";
3
3
  import { writePrivateFile } from "../../agent-config.js";
4
4
  import { codexOwnerArgs, ownerCatalog } from "./codex-fence.js";
5
5
  import { codexHookArgs, trustHookArgs } from "./codex-owner.js";
6
- import { HOOK_ENTRY, ownerRules, roomMcpEntry, warmHook } from "./launch.js";
6
+ import { HOOK_ENTRY, OWNER_IMAGE_RULES, ownerRules, roomMcpEntry, warmHook, } from "./launch.js";
7
7
  /** The room MCP proxy command for a Codex owner job: adds the owner job dir. */
8
8
  export function codexRoomMcpEntry(input) {
9
9
  // Claude's entry, so both fall back to the CLI when no launcher is installed.
@@ -17,6 +17,12 @@ export function codexRoomMcpEntry(input) {
17
17
  export async function prepareCodexOwnerLaunch(input) {
18
18
  const jobDir = join(input.directory, "jobs", input.jobId.replace(/[^\w-]/gu, ""));
19
19
  await mkdir(jobDir, { recursive: true, mode: 0o700 });
20
+ // The stored requester gates use of the owner's image tool, even in shared Rooms.
21
+ const images = input.imagesProven === true &&
22
+ input.ownAsk === true &&
23
+ input.codexHome !== undefined
24
+ ? { dir: join(input.codexHome, "generated_images") }
25
+ : undefined;
20
26
  const catalogPath = join(jobDir, "catalog.json");
21
27
  await writePrivateFile(catalogPath, ownerCatalog(input.bundledCatalog));
22
28
  // A tiny per-job wrapper so the hook is a single executable path (as Codex's
@@ -38,6 +44,7 @@ export async function prepareCodexOwnerLaunch(input) {
38
44
  ...(input.askSocket ? { askSocket: input.askSocket } : {}),
39
45
  ...(warm ? { hookSocket: warm.socketPath } : {}),
40
46
  home: input.home,
47
+ ...(images ? { images } : {}),
41
48
  ...(input.ownAsk ? { ownAsk: true } : {}),
42
49
  ...(input.roomId ? { roomId: input.roomId } : {}),
43
50
  ...(input.roomFileIds ? { roomFileIds: input.roomFileIds } : {}),
@@ -54,11 +61,12 @@ export async function prepareCodexOwnerLaunch(input) {
54
61
  // The owner's own setup reaches the model as data (room is untrusted); the
55
62
  // fence's own refusal is expected. Same rules as Claude owner-native.
56
63
  "-c",
57
- `developer_instructions=${toml(`${input.instructions}\n\n${ownerRules(input.sharedRoom)}`)}`,
64
+ `developer_instructions=${toml(`${input.instructions}\n\n${ownerRules(input.sharedRoom)}${images ? `\n\n${OWNER_IMAGE_RULES}` : ""}`)}`,
58
65
  ...codexHookArgs(hookPath),
59
66
  ...trustArgs,
60
67
  ...codexOwnerArgs({
61
68
  catalogPath,
69
+ images: images !== undefined,
62
70
  mcpArgs: mcp.args,
63
71
  mcpCommand: mcp.command,
64
72
  ownAsk: input.ownAsk,
@@ -0,0 +1,392 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { mkdir, mkdtemp, readFile, realpath, rm, writeFile, } from "node:fs/promises";
3
+ import { createServer } from "node:http";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { isDeepStrictEqual } from "node:util";
7
+ import { writePrivateFile } from "../../agent-config.js";
8
+ import { fakeLogin, osSandboxState, PNG, randomPng, } from "../codex-image-probe.js";
9
+ import { ROOM_LOCAL_TOOLS } from "../codex-policy.js";
10
+ import { binaryIdentity, digest, execute, MAX_BYTES, PROBE_MS, probeResponse, proofCache, } from "../codex-probe.js";
11
+ import { codexOwnerArgs, OWNER_IMAGE_TOOL, ownerCatalog, ownerToolAllowlist, } from "./codex-fence.js";
12
+ import { bundledCatalog, roomServerSource, STANDIN_ROOM_TOOLS, } from "./codex-fence-probe.js";
13
+ import { codexHookArgs, trustHookArgs } from "./codex-owner.js";
14
+ import { OWNER_IMAGE_HOOK_TOOL, ownerPicturePath } from "./owner-pictures.js";
15
+ // The proof behind Codex's own image generation in an owner-native job. The
16
+ // tool reads any local image it is handed a path to and sends its bytes with
17
+ // the request, so a reference to a private image is a private read. Codex has
18
+ // no setting that turns references off or limits them to a folder short of a
19
+ // filesystem profile, which would also stop view_image outside the workspace.
20
+ // It does run our PreToolUse hook on the tool, before it reads a reference: the
21
+ // hook judges each reference as it judges view_image's path (touch.ts, policy.ts)
22
+ // and reports a private one before the call runs. And `codex exec --json`
23
+ // prints no item for an image call, so the hook is the only place to see one.
24
+ //
25
+ // So a job gets the tool only on a Codex and a computer where, on loopback with
26
+ // a made-up login, for a nested (code mode) call and a direct one:
27
+ // 1. the job's whole tool list is the owner allowlist plus the image tool;
28
+ // 2. every image call reaches the hook, named as policy.ts knows it, with the
29
+ // reference paths it was given;
30
+ // 3. a call the hook refuses reads nothing: no image request is made and the
31
+ // reference image's bytes are in no request;
32
+ // 4. a call the hook lets run saves its picture where the hook names it
33
+ // (owner-pictures.ts), before the next call reaches the hook.
34
+ // Anything else leaves images off, and owner jobs run as before.
35
+ export const OWNER_IMAGES_REVISION = 1;
36
+ const REQUESTS_MAX = 80;
37
+ const ROOM_TOOL = `mcp__talentos_room__${STANDIN_ROOM_TOOLS[0]}`;
38
+ // Records every call with the pictures on disk at that moment; refuses an image
39
+ // call that names a reference, allows everything else.
40
+ const HOOK = (records, pictures) => `#!/usr/bin/env node
41
+ const fs = require("node:fs");
42
+ let raw = "";
43
+ process.stdin.on("data", (c) => (raw += c));
44
+ process.stdin.on("end", () => {
45
+ const call = JSON.parse(raw);
46
+ const list = [];
47
+ const walk = (dir) => { try { for (const e of fs.readdirSync(dir, { withFileTypes: true })) { const p = dir + "/" + e.name; e.isDirectory() ? walk(p) : list.push(p); } } catch {} };
48
+ walk(${JSON.stringify(pictures)});
49
+ fs.appendFileSync(${JSON.stringify(records)}, JSON.stringify({ tool_name: call.tool_name, tool_input: call.tool_input, session_id: call.session_id, tool_use_id: call.tool_use_id, pictures: list }) + "\\n");
50
+ const refs = call.tool_input && call.tool_input.referenced_image_paths;
51
+ const deny = call.tool_name === ${JSON.stringify(OWNER_IMAGE_HOOK_TOOL)} && Array.isArray(refs) && refs.length > 0;
52
+ process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: deny ? "deny" : "allow", permissionDecisionReason: "probe" } }));
53
+ });
54
+ `;
55
+ export async function ownerImagesProofKey(identity, catalog) {
56
+ return digest(JSON.stringify([
57
+ OWNER_IMAGES_REVISION,
58
+ "owner-images",
59
+ identity,
60
+ catalog,
61
+ await osSandboxState(),
62
+ [...ownerToolAllowlist(undefined, { images: true })],
63
+ codexOwnerArgs({
64
+ catalogPath: "catalog",
65
+ images: true,
66
+ mcpArgs: [],
67
+ mcpCommand: "mcp",
68
+ prompt: "probe",
69
+ writableRoot: "work",
70
+ }),
71
+ ]));
72
+ }
73
+ /** Whether this Codex binary passed the owner image proof on this computer. */
74
+ export async function codexOwnerImagesProven(binary, bundled) {
75
+ try {
76
+ const key = await ownerImagesProofKey(await binaryIdentity(binary), ownerCatalog(bundled));
77
+ return ((await readFile(proofCache(key), "utf8")) ===
78
+ JSON.stringify({ key, safe: true }));
79
+ }
80
+ catch {
81
+ return false;
82
+ }
83
+ }
84
+ /**
85
+ * Proves this Codex for owner image jobs, once per binary, catalog and OS
86
+ * sandbox (a pass is cached). Never throws: a Codex that fails keeps owner jobs
87
+ * without images and is tried again when the gate probes next.
88
+ */
89
+ export async function ensureCodexOwnerImages(binary, stop) {
90
+ try {
91
+ await proveCodexOwnerImages(binary, stop);
92
+ return true;
93
+ }
94
+ catch {
95
+ return false;
96
+ }
97
+ }
98
+ /** The proof itself; throws, with the reason, unless every check holds. */
99
+ export async function proveCodexOwnerImages(binary, stop) {
100
+ const before = await binaryIdentity(binary);
101
+ const root = await realpath(await mkdtemp(join(tmpdir(), "talentos-codex-owner-images-")));
102
+ const controller = new AbortController();
103
+ const timer = setTimeout(() => controller.abort(), 2 * PROBE_MS);
104
+ const signal = stop
105
+ ? AbortSignal.any([stop, controller.signal])
106
+ : controller.signal;
107
+ try {
108
+ const catalogHome = join(root, "catalog-home");
109
+ await mkdir(catalogHome, { mode: 0o700 });
110
+ const catalog = ownerCatalog(await bundledCatalog(binary, catalogHome, signal));
111
+ const key = await ownerImagesProofKey(before, catalog);
112
+ const cache = proofCache(key);
113
+ if ((await readFile(cache, "utf8").catch(() => "")) ===
114
+ JSON.stringify({ key, safe: true })) {
115
+ return;
116
+ }
117
+ // A real image the hook refuses: it sits beside the job, not in it, and
118
+ // its pixel is random, so nothing else in a request can equal it.
119
+ const canary = join(root, "private-canary.png");
120
+ const canaryBytes = randomPng();
121
+ await writeFile(canary, canaryBytes, { mode: 0o600 });
122
+ const catalogPath = join(root, "catalog.json");
123
+ await writeFile(catalogPath, catalog, { mode: 0o600 });
124
+ const nested = await runOwnerImages(binary, {
125
+ canary,
126
+ canaryBytes,
127
+ catalogPath,
128
+ directory: join(root, "nested"),
129
+ mode: "nested",
130
+ signal,
131
+ });
132
+ const direct = await runOwnerImages(binary, {
133
+ canary,
134
+ canaryBytes,
135
+ catalogPath,
136
+ directory: join(root, "direct"),
137
+ mode: "direct",
138
+ signal,
139
+ });
140
+ const allowed = ownerToolAllowlist([...STANDIN_ROOM_TOOLS, ...ROOM_LOCAL_TOOLS], { images: true });
141
+ if (!isDeepStrictEqual(nested.tools, [...allowed].sort())) {
142
+ throw new Error(`Tool list is not the allowlist: ${nested.tools.join()}`);
143
+ }
144
+ for (const run of [nested, direct]) {
145
+ checkRun(run, canary);
146
+ }
147
+ if ((await binaryIdentity(binary)) !== before) {
148
+ throw new Error("Codex changed during probe");
149
+ }
150
+ signal.throwIfAborted();
151
+ await writePrivateFile(cache, JSON.stringify({ key, safe: true })).catch(() => undefined);
152
+ }
153
+ finally {
154
+ clearTimeout(timer);
155
+ controller.abort();
156
+ await rm(root, { recursive: true, force: true });
157
+ }
158
+ }
159
+ function checkRun(run, canary) {
160
+ const images = run.records.filter((record) => record.tool_name === OWNER_IMAGE_HOOK_TOOL);
161
+ const [refused, made] = images;
162
+ if (images.length !== 2 ||
163
+ !refused ||
164
+ !made ||
165
+ !isDeepStrictEqual(refused.tool_input?.referenced_image_paths, [canary])) {
166
+ throw new Error("The hook did not see every image call as asked");
167
+ }
168
+ // The refused call read nothing and sent nothing; the allowed one made one.
169
+ if (run.leaked || run.edits !== 0 || run.generations !== 1) {
170
+ throw new Error("A refused image call still reached its reference");
171
+ }
172
+ if (typeof made.session_id !== "string" ||
173
+ typeof made.tool_use_id !== "string") {
174
+ throw new Error("The hook was not told where the picture goes");
175
+ }
176
+ const picture = ownerPicturePath(run.pictures, made.session_id, made.tool_use_id);
177
+ const next = run.records.at(-1);
178
+ if (next?.tool_name !== ROOM_TOOL || !next.pictures.includes(picture)) {
179
+ throw new Error("The picture was not saved where the hook named it");
180
+ }
181
+ }
182
+ async function runOwnerImages(binary, input) {
183
+ const { canary, canaryBytes, directory, signal } = input;
184
+ const home = join(directory, "home");
185
+ const work = join(directory, "work");
186
+ const pictures = join(home, "generated_images");
187
+ await mkdir(home, { recursive: true, mode: 0o700 });
188
+ await mkdir(work, { recursive: true, mode: 0o700 });
189
+ await writeFile(join(home, "auth.json"), fakeLogin(), { mode: 0o600 });
190
+ const records = join(directory, "hook-records");
191
+ await writeFile(records, "");
192
+ const hook = join(directory, "hook.js");
193
+ await writeFile(hook, HOOK(records, pictures), { mode: 0o700 });
194
+ const roomServer = join(directory, "room.mjs");
195
+ await writeFile(roomServer, roomServerSource(join(directory, "room-calls"), 0));
196
+ const run = {
197
+ edits: 0,
198
+ generations: 0,
199
+ leaked: false,
200
+ pictures,
201
+ records: [],
202
+ tools: [],
203
+ };
204
+ // What the model asks for, in order; then a plain answer.
205
+ const steps = input.mode === "nested"
206
+ ? [
207
+ {
208
+ type: "custom_tool_call",
209
+ id: "call",
210
+ call_id: "call",
211
+ name: "exec",
212
+ input: `text(JSON.stringify(Object.keys(tools).sort()));
213
+ try { await tools.${OWNER_IMAGE_TOOL}({ prompt: "a single pixel", referenced_image_paths: [${JSON.stringify(canary)}] }); } catch {}
214
+ await tools.${OWNER_IMAGE_TOOL}({ prompt: "a single pixel" });
215
+ await tools.${ROOM_TOOL}({});`,
216
+ status: "completed",
217
+ },
218
+ ]
219
+ : [
220
+ imageCall("call-0", [canary]),
221
+ imageCall("call-1", null),
222
+ {
223
+ type: "function_call",
224
+ id: "call-2",
225
+ call_id: "call-2",
226
+ name: STANDIN_ROOM_TOOLS[0],
227
+ namespace: "mcp__talentos_room",
228
+ arguments: "{}",
229
+ status: "completed",
230
+ },
231
+ ];
232
+ let failure;
233
+ let requests = 0;
234
+ let turn = 0;
235
+ const server = createServer(async (request, response) => {
236
+ try {
237
+ if (++requests > REQUESTS_MAX) {
238
+ throw new Error("Unexpected probe request");
239
+ }
240
+ const parts = [];
241
+ let size = 0;
242
+ for await (const part of request) {
243
+ parts.push(part);
244
+ size += part.length;
245
+ if (size > MAX_BYTES) {
246
+ throw new Error("Request limit");
247
+ }
248
+ }
249
+ const body = Buffer.concat(parts);
250
+ if (body.includes(canaryBytes) ||
251
+ body.includes(canaryBytes.toString("base64"))) {
252
+ run.leaked = true;
253
+ }
254
+ const path = request.url ?? "";
255
+ if (path.includes("/images/")) {
256
+ if (path.includes("/edits")) {
257
+ run.edits++;
258
+ }
259
+ else {
260
+ run.generations++;
261
+ }
262
+ response.writeHead(200, { "Content-Type": "application/json" });
263
+ response.end(JSON.stringify({
264
+ created: 1,
265
+ data: [{ b64_json: PNG.toString("base64") }],
266
+ }));
267
+ return;
268
+ }
269
+ if (path.includes("/oauth/")) {
270
+ // The made-up login cannot refresh; nothing leaves loopback.
271
+ response.writeHead(401).end();
272
+ return;
273
+ }
274
+ if (request.method !== "POST" || !path.endsWith("/v1/responses")) {
275
+ // Account and analytics calls of a ChatGPT login: nothing to prove.
276
+ response.writeHead(200, { "Content-Type": "application/json" });
277
+ response.end("{}");
278
+ return;
279
+ }
280
+ const turnBody = JSON.parse(body.toString());
281
+ if (input.mode === "nested" && turn === 1) {
282
+ run.tools = toolList(turnBody.input ?? []);
283
+ }
284
+ probeResponse(response, steps[turn++] ?? {
285
+ type: "message",
286
+ id: "answer",
287
+ role: "assistant",
288
+ status: "completed",
289
+ content: [{ type: "output_text", text: "Done.", annotations: [] }],
290
+ });
291
+ }
292
+ catch (error) {
293
+ failure = error;
294
+ response.writeHead(400).end();
295
+ }
296
+ });
297
+ await new Promise((resolve, reject) => {
298
+ server.once("error", reject);
299
+ server.listen(0, "127.0.0.1", resolve);
300
+ });
301
+ try {
302
+ const address = server.address();
303
+ if (!address || typeof address === "string") {
304
+ throw new Error("Missing probe port");
305
+ }
306
+ const base = `http://127.0.0.1:${address.port}`;
307
+ const env = {
308
+ HOME: home,
309
+ CODEX_HOME: home,
310
+ PATH: process.env.PATH,
311
+ CODEX_REFRESH_TOKEN_URL_OVERRIDE: `${base}/oauth/token`,
312
+ ...(process.env.SystemRoot ? { SystemRoot: process.env.SystemRoot } : {}),
313
+ };
314
+ const trust = await trustHookArgs({
315
+ binary,
316
+ command: hook,
317
+ cwd: work,
318
+ env,
319
+ signal,
320
+ });
321
+ const args = [
322
+ "-c",
323
+ `chatgpt_base_url="${base}/backend-api/"`,
324
+ "-c",
325
+ 'model_provider="talentos_probe"',
326
+ "-c",
327
+ 'model_providers.talentos_probe.name="Loopback"',
328
+ "-c",
329
+ `model_providers.talentos_probe.base_url="${base}/v1"`,
330
+ "-c",
331
+ 'model_providers.talentos_probe.wire_api="responses"',
332
+ "-c",
333
+ "model_providers.talentos_probe.requires_openai_auth=true",
334
+ "-c",
335
+ "model_providers.talentos_probe.supports_websockets=false",
336
+ ...codexHookArgs(hook),
337
+ ...trust,
338
+ ...codexOwnerArgs({
339
+ catalogPath: input.catalogPath,
340
+ images: true,
341
+ mcpArgs: [roomServer],
342
+ mcpCommand: process.execPath,
343
+ prompt: `Verify the image hook ${randomUUID()}.`,
344
+ writableRoot: work,
345
+ }),
346
+ ];
347
+ const out = await execute(binary, args, work, env, signal);
348
+ if (failure ||
349
+ turn <= steps.length ||
350
+ !out.includes('"type":"turn.completed"')) {
351
+ throw new Error("Incomplete owner image proof");
352
+ }
353
+ run.records = (await readFile(records, "utf8"))
354
+ .split("\n")
355
+ .filter(Boolean)
356
+ .map((line) => JSON.parse(line));
357
+ return run;
358
+ }
359
+ finally {
360
+ server.closeAllConnections();
361
+ await new Promise((resolve) => server.close(() => resolve()));
362
+ }
363
+ }
364
+ function imageCall(id, references) {
365
+ return {
366
+ type: "function_call",
367
+ id,
368
+ call_id: id,
369
+ name: "imagegen",
370
+ namespace: "image_gen",
371
+ arguments: JSON.stringify({
372
+ prompt: "a single pixel",
373
+ ...(references ? { referenced_image_paths: references } : {}),
374
+ }),
375
+ status: "completed",
376
+ };
377
+ }
378
+ // The nested script's first line: the tool names, as the exec output's text.
379
+ function toolList(items) {
380
+ for (const item of items) {
381
+ if (item.type !== "custom_tool_call_output" ||
382
+ !Array.isArray(item.output)) {
383
+ continue;
384
+ }
385
+ for (const part of item.output) {
386
+ if (typeof part.text === "string" && part.text.startsWith("[")) {
387
+ return JSON.parse(part.text);
388
+ }
389
+ }
390
+ }
391
+ return [];
392
+ }