mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 09:39:53 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - The Paperclip runner launches Codex agents in an isolated app-server
with a sandboxed shell
> - On the Codex API path, the agent's OpenAI API key reaches only the
trusted app-server process, never the shell
> - The runner launched Codex with `--disable image_generation`, so
Codex's built-in image tool was not available
> - Codex then falls back to its `image_gen.py` script, which needs
`OPENAI_API_KEY` in the shell, so the agent asked the user for a second
key
> - This pull request keeps Codex's built-in image tool on, so it uses
the agent's existing provider auth, and lets the sandbox read only the
folder where Codex saves the images
> - The benefit is that a Codex agent can make images without a second
key, and the provider key still does not reach the shell
## Linked Issues or Issue Description
**What happened?**
Manual onboarding test on Paperclip Cloud staging (Codex API path):
"Open AI requests another api key for image model even though it has an
active api key powering the agent."
**Steps to reproduce**
1. Create a company with a Codex agent that uses an OpenAI API key.
2. Ask the agent to generate an image.
3. The agent asks for another OpenAI API key.
**Expected behavior**
The agent uses the OpenAI API key that already powers it.
**Root cause**
`createIsolatedCodexAppServerArgs` passed `--disable image_generation`
and the thread config set `features.image_generation: false`. The pinned
Codex 0.160.0 binary has two image paths. Its own instructions say the
built-in `image_gen` tool "Does not require `OPENAI_API_KEY`". The
fallback CLI path "Requires `OPENAI_API_KEY`". The runner removes
`OPENAI_API_KEY` from the shell on purpose
(`codex-shell-environment.ts`), so the fallback can never work and the
agent asks the user.
## What Changed
- `codex-security-config.ts`: remove `--disable image_generation` and
`features.image_generation: false`. Codex's built-in tool runs in the
trusted app-server with the agent's existing auth.
- `codex-security-config.ts`: in the workspace and plan permission
profiles, grant **read** on `CODEX_HOME/generated_images` only. Codex
saves images there and tells the agent to copy them into the workspace.
The rest of `CODEX_HOME` (for example `auth.json`) stays denied.
Preconfigured-home mode is unchanged (it does not deny `CODEX_HOME`).
- `eval-gateway-runtime.ts`: set `image_generation = false` in the
controller-gateway Codex config. That gateway admits no hosted tools
(`unqualified_hosted_tool`), so qualified evals keep the earlier
behavior.
- Tests: two new N-12 tests, and updates to the tests that asserted the
old disable flag.
## Verification
Red on `master` (819420a60a), same test file before the source change:
```
× N-12: lets an API-key Codex agent generate images with its existing provider key instead of asking for another 5ms
× N-12: the pinned Codex binary keeps built-in image generation enabled under the isolated launch 4400ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯
FAIL src/drivers/codex/codex-security-config.test.ts > Codex security configuration > N-12: lets an API-key Codex agent generate images with its existing provider key instead of asking for another
AssertionError: expected [ '-c', …(22) ] to not include 'image_generation'
378| expect(args).not.toContain("image_generation");
FAIL src/drivers/codex/codex-security-config.test.ts > Codex security configuration > N-12: the pinned Codex binary keeps built-in image generation enabled under the isolated launch
AssertionError: expected 'image_generation …' to match /\strue\s*$/
+ Received:
"image_generation stable false"
Tests 2 failed | 17 skipped (19)
```
Green after the fix:
```
Tests 2 passed | 17 skipped (19)
```
Neighbouring suites (`src/drivers/codex`,
`src/cli/eval-gateway-runtime.test.ts`, `src/provider-events.test.ts`):
```
Test Files 25 passed (25)
Tests 429 passed (429)
```
Real sandbox check with the pinned Codex 0.160.0 binary (`codex sandbox`
with the generated permission profile, macOS seatbelt):
```
generated_images/t1/img.png status=0 "PNGDATA" # agent can read the saved image
auth.json status=1 "cat: ...: Operation not permitted" # rest of CODEX_HOME stays denied
cp status=0 # agent can copy the image into the workspace
```
- `pnpm exec tsc --noEmit -p packages/paperclip-runner`: no errors in
changed files (only pre-existing errors from the unbuilt
`@paperclipai/paperclip-eval-kernel` package).
- `pnpm verify` exits 2 (changes outside resolved test graphs); required
CI supplies the broad coverage.
- **Test scope.** The red/green tests assert the launch configuration:
the disable flag, the thread config, the sandbox grant, and the
effective `image_generation` state that the pinned Codex binary reports.
They do not drive a model. So they do not directly observe the reported
"asks for a second key" behavior. The link from the flag to that
behavior comes from the pinned Codex 0.160.0 bundled instructions: the
built-in `image_gen` tool "Does not require `OPENAI_API_KEY`", and the
fallback `scripts/image_gen.py` CLI "Requires `OPENAI_API_KEY`". The
runner removes `OPENAI_API_KEY` from the shell.
- Not run: a live paid image generation with an API-key Codex agent.
There is no Codex staging company.
## Risks
- Image generation uses the agent's OpenAI account. Images cost money on
API-key billing. This is the expected behavior when the user asks for an
image.
- The agent can read images it (or an earlier thread of the same Codex
home) generated. It cannot read other files in `CODEX_HOME`.
- Codex's built-in `view_image` tool was already enabled, so the trusted
process already reads image files for the model. This change does not
add a new kind of file access to the provider.
- If a future Codex release changes the image save folder, the agent can
still see the image inline but cannot copy it. The N-12 binary test pins
the feature state for the current Codex version.
### Security policy change (needs maintainer sign-off)
**Why it was disabled.** #12364 ("isolate Codex security configuration")
disabled host apps, plugins, memories, multi-agent behavior, and image
generation as one group. Its goal was a closed launch configuration that
does not depend on ambient Codex defaults. The PR records no specific
threat for image generation. This PR keeps the other features disabled.
**Why re-enabling it is safe.**
- The tool is a hosted tool. It runs at OpenAI inside the same model
request that the trusted app-server already makes. It adds no new
network endpoint, process, or shell capability.
- No credential moves. `OPENAI_API_KEY` stays out of the shell, argv,
and serialized config (the N-12 test asserts this).
- The new sandbox grant is read-only and covers only
`CODEX_HOME/generated_images`. The rest of `CODEX_HOME` (`auth.json`,
config, sessions) stays denied. The real `codex sandbox` check above
confirms this. In plan mode the grant is also read-only.
- Codex's `view_image` tool was already enabled. So the trusted process
already sends local image content to the provider.
- Controller-gateway evals keep the tool off (`image_generation =
false`), because that gateway admits no hosted tools.
**Credentials and billing.**
- API-key path: the hosted tool uses the agent's existing OpenAI API key
through the app-server. OpenAI bills image generation to that API
account at image-model rates.
- ChatGPT-subscription path: the tool uses the agent's existing ChatGPT
sign-in. Use counts against that ChatGPT plan's limits. There is no
separate API bill.
- These two points follow from how Codex sends hosted tools in the
provider request. I did not observe them in a live paid run.
- Open question: Paperclip cost tracking uses provider token usage. I
did not confirm that image-generation charges appear in Paperclip's
per-agent cost and budget totals. If they do not, budget hard-stops
could undercount image spend.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (shell, file edits, test execution).
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
5.6 KiB
TypeScript
108 lines
5.6 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { EvalSessionRequest } from "./eval-session-contract.js";
|
|
|
|
export interface EvalGatewayDescriptor {
|
|
schema: "paperclip.runner-eval.gateway/v1";
|
|
provider: "openai" | "anthropic";
|
|
model: string;
|
|
baseUrl: string;
|
|
campaignId: string;
|
|
executionId: string;
|
|
attemptId: string;
|
|
upstreamRoute: "direct" | "braintrust";
|
|
}
|
|
|
|
const TOKEN_ENV = "PAPERCLIP_RUNNER_EVAL_GATEWAY_TOKEN";
|
|
const safeId = (value: unknown) => typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9_.:/-]{0,511}$/.test(value);
|
|
|
|
export async function prepareEvalGatewayRuntime(
|
|
descriptorPath: string,
|
|
request: EvalSessionRequest,
|
|
source: NodeJS.ProcessEnv = process.env,
|
|
) {
|
|
const raw = await readFile(descriptorPath, "utf8");
|
|
let descriptor: EvalGatewayDescriptor;
|
|
try {
|
|
if (Buffer.byteLength(raw) > 8192) throw new Error();
|
|
descriptor = JSON.parse(raw) as EvalGatewayDescriptor;
|
|
const fields = ["schema", "provider", "model", "baseUrl", "campaignId", "executionId", "attemptId", "upstreamRoute"];
|
|
if (!descriptor || typeof descriptor !== "object" || Array.isArray(descriptor)
|
|
|| Object.keys(descriptor).length !== fields.length || Object.keys(descriptor).some(key => !fields.includes(key))
|
|
|| descriptor.schema !== "paperclip.runner-eval.gateway/v1"
|
|
|| !["openai", "anthropic"].includes(descriptor.provider)
|
|
|| !["direct", "braintrust"].includes(descriptor.upstreamRoute)
|
|
|| ![descriptor.campaignId, descriptor.executionId, descriptor.attemptId].every(safeId)
|
|
|| descriptor.attemptId !== request.attemptId || descriptor.model !== request.model
|
|
|| typeof descriptor.baseUrl !== "string" || !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}$/.test(descriptor.baseUrl)) throw new Error();
|
|
const url = new URL(descriptor.baseUrl);
|
|
if (!url.port || Number(url.port) > 65535) throw new Error();
|
|
} catch {
|
|
throw new Error("Invalid controller gateway descriptor");
|
|
}
|
|
const provider = request.provider ?? "codex";
|
|
const codex = provider === "codex";
|
|
const claude = provider === "acpx" && request.acpxAgent === "claude";
|
|
if (!codex && !claude || descriptor.provider !== (codex ? "openai" : "anthropic")) {
|
|
throw new Error("Controller gateway does not support this eval provider");
|
|
}
|
|
const token = source[TOKEN_ENV];
|
|
if (typeof token !== "string" || !/^[a-f0-9]{64}$/.test(token)) throw new Error("Missing or invalid controller gateway token");
|
|
const root = await mkdtemp(join(tmpdir(), "paperclip-runner-eval-gateway-"));
|
|
try {
|
|
const home = join(root, "home"), codexHome = join(root, "codex"), claudeHome = join(root, "claude");
|
|
const environment: NodeJS.ProcessEnv = Object.fromEntries(
|
|
["PATH", "LANG", "LANGUAGE", "LC_ALL", "TZ", "SystemRoot", "WINDIR", "PATHEXT"]
|
|
.flatMap(key => source[key] === undefined ? [] : [[key, source[key]!]]),
|
|
);
|
|
Object.assign(environment, {
|
|
HOME: home, CODEX_HOME: codexHome, CLAUDE_CONFIG_DIR: claudeHome,
|
|
XDG_CONFIG_HOME: join(root, "config"), XDG_DATA_HOME: join(root, "data"), XDG_CACHE_HOME: join(root, "cache"),
|
|
TMPDIR: join(root, "tmp"), TMP: join(root, "tmp"), TEMP: join(root, "tmp"),
|
|
PAPERCLIP_RUNNER_HOME_MODE: "preconfigured",
|
|
});
|
|
for (const directory of [home, codexHome, claudeHome, environment.XDG_CONFIG_HOME!, environment.XDG_DATA_HOME!, environment.XDG_CACHE_HOME!, environment.TMPDIR!]) {
|
|
await mkdir(directory, { mode: 0o700 });
|
|
}
|
|
if (codex) {
|
|
environment.PAPERCLIP_AI_PROVIDER_KEY = token;
|
|
await writeFile(join(codexHome, "config.toml"), [
|
|
'model_provider = "paperclip_eval"',
|
|
'web_search = "disabled"',
|
|
`model = ${JSON.stringify(request.model)}`,
|
|
'[model_providers.paperclip_eval]',
|
|
'name = "Paperclip eval controller"',
|
|
`base_url = ${JSON.stringify(descriptor.baseUrl + "/v1")}`,
|
|
'wire_api = "responses"', 'requires_openai_auth = false', 'env_key = "PAPERCLIP_AI_PROVIDER_KEY"',
|
|
// The controller gateway admits no hosted tools, so qualified evals keep
|
|
// Codex's hosted image tool off even though product runs enable it.
|
|
'[features]', 'shell_snapshot = false', 'image_generation = false', '',
|
|
].join("\n"), { mode: 0o600, flag: "wx" });
|
|
} else {
|
|
Object.assign(environment, {
|
|
ANTHROPIC_API_KEY: token, ANTHROPIC_BASE_URL: descriptor.baseUrl,
|
|
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", ACPX_CLAUDE_INCLUDE_USER_SETTINGS: "1",
|
|
ANTHROPIC_MODEL: request.model, ANTHROPIC_DEFAULT_OPUS_MODEL: request.model,
|
|
ANTHROPIC_DEFAULT_SONNET_MODEL: request.model, ANTHROPIC_DEFAULT_HAIKU_MODEL: request.model,
|
|
CLAUDE_CODE_SUBAGENT_MODEL: request.model,
|
|
});
|
|
await writeFile(join(claudeHome, "settings.json"), JSON.stringify({ model: request.model, availableModels: [request.model] }) + "\n", { mode: 0o600, flag: "wx" });
|
|
}
|
|
return {
|
|
transportOptions: { homeMode: "preconfigured" as const, environment },
|
|
evidence: {
|
|
mode: "controller_gateway" as const, provider: descriptor.provider,
|
|
declaredUpstreamRoute: descriptor.upstreamRoute,
|
|
campaignId: descriptor.campaignId, executionId: descriptor.executionId, attemptId: descriptor.attemptId,
|
|
descriptorSha256: createHash("sha256").update(raw).digest("hex"),
|
|
},
|
|
close: () => rm(root, { recursive: true, force: true }),
|
|
};
|
|
} catch (error) {
|
|
await rm(root, { recursive: true, force: true });
|
|
throw error;
|
|
}
|
|
}
|