@zvada/agent-server 0.3.23 → 0.3.25
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.
- package/CHANGELOG.md +64 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +745 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +331 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +92 -0
- package/dist/core/processes/lifeline.js +218 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +164 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +29 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +437 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +339 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +142 -0
- package/dist/protocol/errors.js +190 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +292 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -0
- package/dist/types/client/client.d.ts +6 -1
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +28 -11
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +8 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +8 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -1
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
- package/dist/types/core/agents/scripted/steps.d.ts +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/identity.d.ts.map +1 -1
- package/dist/types/core/processes/lifeline.d.ts.map +1 -1
- package/dist/types/core/processes/process-table.d.ts.map +1 -1
- package/dist/types/core/provision/extract.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +4 -1
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +12 -7
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +3 -3
- package/dist/types/protocol/conversation.d.ts +6 -6
- package/dist/types/protocol/credentials.d.ts +13 -1
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/errors.d.ts +6 -4
- package/dist/types/protocol/errors.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +4 -4
- package/dist/types/protocol/wire-api.d.ts +12 -12
- package/dist/types/protocol/wire.d.ts +8 -8
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- package/dist/types/server/routes.d.ts.map +1 -1
- package/dist/types/server/transports.d.ts +2 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.d.ts.map +1 -1
- package/docs/consuming.md +56 -7
- package/docs/deploy.md +35 -0
- package/docs/harnesses.md +30 -5
- package/package.json +43 -18
- package/src/client/client.ts +114 -44
- package/src/core/agents/claude-code/approvals.ts +5 -1
- package/src/core/agents/claude-code/claude-agent.ts +32 -5
- package/src/core/agents/claude-code/options.ts +28 -11
- package/src/core/agents/claude-code/session.ts +13 -0
- package/src/core/agents/codex-app-server/homes.ts +11 -2
- package/src/core/agents/codex-app-server/login.ts +30 -0
- package/src/core/agents/codex-app-server/requests.ts +7 -6
- package/src/core/agents/codex-app-server/session.ts +11 -2
- package/src/core/agents/process-pool.ts +30 -4
- package/src/core/agents/scripted/scripted-agent.ts +2 -1
- package/src/core/agents/scripted/steps.ts +1 -1
- package/src/core/presets.ts +3 -2
- package/src/core/processes/identity.ts +1 -0
- package/src/core/processes/lifeline.ts +10 -1
- package/src/core/processes/process-table.ts +1 -0
- package/src/core/provision/extract.ts +1 -0
- package/src/core/runtime/agent-runtime.ts +6 -3
- package/src/core/runtime/turn.ts +21 -9
- package/src/protocol/credentials.ts +19 -5
- package/src/protocol/errors.ts +12 -3
- package/src/server/bin.ts +25 -10
- package/src/server/routes.ts +3 -2
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { AsyncQueue } from "../../../protocol/index.js";
|
|
2
|
+
import { InvalidRequestError } from "../../utils/errors.js";
|
|
3
|
+
import { isRecord } from "../../utils/is-record.js";
|
|
4
|
+
import { configFingerprint } from "../config-fingerprint.js";
|
|
5
|
+
import { UnavailableMcpServers, mcpConnectionErrors, settleMcpFailures, snapshotMcpServers, } from "../mcp-config.js";
|
|
6
|
+
import { pages } from "./client.js";
|
|
7
|
+
/** Codex supports stdio and Streamable HTTP, not the legacy SSE transport. */
|
|
8
|
+
function codexMcpServers(servers) {
|
|
9
|
+
return Object.fromEntries(Object.entries(servers).map(([name, server]) => {
|
|
10
|
+
if (name === "codex_apps")
|
|
11
|
+
throw new Error("MCP server name codex_apps is reserved by Codex");
|
|
12
|
+
if (server.type === "sse") {
|
|
13
|
+
throw new Error(`Codex MCP server ${name}: legacy SSE is unsupported; use Streamable HTTP`);
|
|
14
|
+
}
|
|
15
|
+
return [
|
|
16
|
+
name,
|
|
17
|
+
server.type === "stdio"
|
|
18
|
+
? {
|
|
19
|
+
command: server.command,
|
|
20
|
+
...(server.args && { args: server.args }),
|
|
21
|
+
...(server.env && { env: server.env }),
|
|
22
|
+
}
|
|
23
|
+
: { url: server.url, ...(server.headers && { http_headers: server.headers }) },
|
|
24
|
+
];
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
/** Only call against an explicitly isolated, engine-managed CODEX_HOME. */
|
|
28
|
+
async function writeCodexMcpServers(client, servers) {
|
|
29
|
+
const result = (await client.request("config/batchWrite", {
|
|
30
|
+
edits: [{ keyPath: "mcp_servers", value: servers, mergeStrategy: "replace" }],
|
|
31
|
+
// Check project-layer overrides before any connection can use credentials.
|
|
32
|
+
reloadUserConfig: false,
|
|
33
|
+
}));
|
|
34
|
+
if (result.status !== "ok" || result.overriddenMetadata) {
|
|
35
|
+
throw new Error("Codex MCP configuration was not applied; check higher-priority native settings");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function normalizedConfig(value) {
|
|
39
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
40
|
+
return value;
|
|
41
|
+
const config = { ...value };
|
|
42
|
+
// Defaults materialized by config/read; preserve every non-default field.
|
|
43
|
+
if (config.environment_id === "local")
|
|
44
|
+
config.environment_id = undefined;
|
|
45
|
+
if (config.enabled === true)
|
|
46
|
+
config.enabled = undefined;
|
|
47
|
+
if (config.tool_timeout_sec === null)
|
|
48
|
+
config.tool_timeout_sec = undefined;
|
|
49
|
+
if (typeof config.command === "string") {
|
|
50
|
+
config.args ??= [];
|
|
51
|
+
config.env ??= {};
|
|
52
|
+
}
|
|
53
|
+
else if (typeof config.url === "string") {
|
|
54
|
+
config.http_headers ??= {};
|
|
55
|
+
}
|
|
56
|
+
return config;
|
|
57
|
+
}
|
|
58
|
+
/** User-layer write acknowledgements do not account for repository overrides. */
|
|
59
|
+
async function assertCodexMcpConfig(client, cwd, servers) {
|
|
60
|
+
if (!Object.keys(servers).length)
|
|
61
|
+
return;
|
|
62
|
+
const result = (await client.request("config/read", { cwd }));
|
|
63
|
+
for (const [name, server] of Object.entries(servers)) {
|
|
64
|
+
if (configFingerprint(normalizedConfig(result.config?.mcp_servers?.[name])) !==
|
|
65
|
+
configFingerprint(normalizedConfig(server))) {
|
|
66
|
+
throw new Error(`Codex MCP server ${name} is overridden by native settings`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** A higher layer of Codex's configuration over a lower: tables key by key, anything else replaced. */
|
|
71
|
+
function mergedOver(base, over) {
|
|
72
|
+
if (!isRecord(base) || !isRecord(over))
|
|
73
|
+
return over;
|
|
74
|
+
const merged = { ...base };
|
|
75
|
+
for (const [key, value] of Object.entries(over))
|
|
76
|
+
merged[key] = mergedOver(base[key], value);
|
|
77
|
+
return merged;
|
|
78
|
+
}
|
|
79
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — a name Codex's own configuration has
|
|
80
|
+
/**
|
|
81
|
+
* Refuse a turn's server that Codex's own configuration (`native`) would
|
|
82
|
+
* change. A server in a thread's config is the top layer of Codex's, merged
|
|
83
|
+
* table by table over one of the same name in the user's or the
|
|
84
|
+
* repository's: the turn's values win, and whatever else the other sets
|
|
85
|
+
* stays (its headers and environment sent to the turn's server, its tool
|
|
86
|
+
* filter, `enabled = false`). One that sets nothing more is the turn's
|
|
87
|
+
* server whole, and goes on.
|
|
88
|
+
*/
|
|
89
|
+
function assertCodexThreadMcp(native, servers) {
|
|
90
|
+
const changed = Object.entries(servers).flatMap(([name, server]) => native[name] !== undefined &&
|
|
91
|
+
configFingerprint(normalizedConfig(mergedOver(native[name], server))) !==
|
|
92
|
+
configFingerprint(normalizedConfig(server))
|
|
93
|
+
? [name]
|
|
94
|
+
: []);
|
|
95
|
+
if (changed.length)
|
|
96
|
+
throw new InvalidRequestError(changed.map((name) => ({
|
|
97
|
+
path: `mcpServers.${name}`,
|
|
98
|
+
message: "Codex's own configuration (the user's or the repository's) has a server of this name, whose settings would apply to the turn's; rename one of them",
|
|
99
|
+
})));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Codex's startup error for a server, readable: its Rust type paths out, a
|
|
103
|
+
* chain it repeats said once, and no prefix naming the server again.
|
|
104
|
+
*/
|
|
105
|
+
export function codexMcpCause(name, error) {
|
|
106
|
+
const segments = error
|
|
107
|
+
// A Rust path (`[codex_rmcp_client::…]`), never an IPv6 host (`[fe80::1]`).
|
|
108
|
+
.replace(/\s*\[([^\]]*::[^\]]*)\]/g, (path, inside) => /^[0-9a-f:.%]+$/i.test(inside) ? path : "")
|
|
109
|
+
.replace(`MCP client for \`${name}\` failed to start: `, "")
|
|
110
|
+
.split(": ");
|
|
111
|
+
return segments.filter((segment, i) => segments.indexOf(segment) === i).join(": ");
|
|
112
|
+
}
|
|
113
|
+
/** How long a turn waits for its MCP servers to start. */
|
|
114
|
+
const MCP_STARTUP_MS = 30_000;
|
|
115
|
+
/**
|
|
116
|
+
* Wait for the thread's MCP servers to start, and settle the ones that did
|
|
117
|
+
* not: a required server's failure fails the turn (`McpUnavailableError`);
|
|
118
|
+
* the others are returned, each with why, for the turn to say and go on
|
|
119
|
+
* without. `servers` is the turn's wire configuration, by name, and `causes`
|
|
120
|
+
* why each server last failed, as Codex said.
|
|
121
|
+
*/
|
|
122
|
+
async function settleCodexMcp(client, threadId, servers, causes) {
|
|
123
|
+
const names = Object.keys(servers);
|
|
124
|
+
if (!names.length)
|
|
125
|
+
return {};
|
|
126
|
+
// A status snapshot can arrive while startup is still in progress. Subscribe
|
|
127
|
+
// first so completion during the request cannot be lost. Native notifications
|
|
128
|
+
// trigger another authoritative read; no setup, model or tool is retried.
|
|
129
|
+
const updates = new AsyncQueue();
|
|
130
|
+
const offStatus = client.onNotification(({ method, params }) => {
|
|
131
|
+
if (method === "mcpServer/startupStatus/updated" &&
|
|
132
|
+
params.threadId === threadId &&
|
|
133
|
+
names.includes(String(params.name)))
|
|
134
|
+
updates.push(undefined);
|
|
135
|
+
});
|
|
136
|
+
const offClose = client.onClose((error) => updates.fail(error ?? new Error("Codex closed during MCP startup")));
|
|
137
|
+
const deadline = Date.now() + MCP_STARTUP_MS;
|
|
138
|
+
// At the deadline the loop reads once more and settles what still starts:
|
|
139
|
+
// the timer says so itself, as a clock can read short of the deadline then.
|
|
140
|
+
let late = false;
|
|
141
|
+
const timer = setTimeout(() => {
|
|
142
|
+
late = true;
|
|
143
|
+
updates.push(undefined);
|
|
144
|
+
}, MCP_STARTUP_MS);
|
|
145
|
+
try {
|
|
146
|
+
for (;;) {
|
|
147
|
+
const statuses = [];
|
|
148
|
+
// Asked by name, Codex answers from the thread's own connection.
|
|
149
|
+
// Asked for every server, it connects to each again (initialize,
|
|
150
|
+
// tools/list) on every turn (observed on Codex 0.160, 2026-10-28).
|
|
151
|
+
for (const serverName of names) {
|
|
152
|
+
for await (const page of pages(client, "mcpServerStatus/list", { threadId, serverName, detail: "toolsAndAuthOnly", limit: 100 }, { timeoutMs: Math.max(5_000, deadline - Date.now()) }))
|
|
153
|
+
statuses.push(...page
|
|
154
|
+
.filter(({ name }) => name === serverName)
|
|
155
|
+
.map(({ name, runtimeStatus }) => {
|
|
156
|
+
const cause = causes.get(name);
|
|
157
|
+
return { name, status: runtimeStatus, ...(cause && { error: cause }) };
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
// Read once, after the pass: past the deadline every server settles.
|
|
161
|
+
const settling = late;
|
|
162
|
+
const starting = settling
|
|
163
|
+
? []
|
|
164
|
+
: names.filter((name) => statuses.find((entry) => entry.name === name)?.status === "starting");
|
|
165
|
+
const unavailable = settleMcpFailures(mcpConnectionErrors(names.filter((name) => !starting.includes(name)), statuses.map((entry) => entry.status === "starting" && !entry.error
|
|
166
|
+
? { ...entry, error: `did not start within ${MCP_STARTUP_MS / 1000} s` }
|
|
167
|
+
: entry)), servers);
|
|
168
|
+
if (!starting.length)
|
|
169
|
+
return unavailable;
|
|
170
|
+
await updates.next();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
offStatus();
|
|
176
|
+
offClose();
|
|
177
|
+
updates.end();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/** The turn's servers, snapshotted before anything asynchronous can let the caller change them. */
|
|
181
|
+
export function codexTurnMcp(options, homes) {
|
|
182
|
+
const wire = snapshotMcpServers(options.mcpServers);
|
|
183
|
+
const servers = codexMcpServers(wire);
|
|
184
|
+
if (homes.claim(options))
|
|
185
|
+
return { wire, servers };
|
|
186
|
+
return Object.keys(servers).length ? { wire, thread: servers } : { wire };
|
|
187
|
+
}
|
|
188
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — a thread's config is fixed once it is open
|
|
189
|
+
/**
|
|
190
|
+
* Which `mcp_servers` table a thread was opened with (`threadServers`): a
|
|
191
|
+
* turn that needs another runs on the thread opened again in a new process.
|
|
192
|
+
*/
|
|
193
|
+
export function threadServersFingerprint(table) {
|
|
194
|
+
return configFingerprint(table ?? {});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* A session's MCP servers on Codex, for one process: the configuration last
|
|
198
|
+
* written to its home, why each server last failed to start, and the ones
|
|
199
|
+
* its turns went without.
|
|
200
|
+
*/
|
|
201
|
+
export class CodexMcpState {
|
|
202
|
+
client;
|
|
203
|
+
cwd;
|
|
204
|
+
/** Last native config write; readiness is checked separately before every prompt. */
|
|
205
|
+
fingerprint;
|
|
206
|
+
/** A turn was prepared on this process: the servers its thread carries started when it opened. */
|
|
207
|
+
prepared = false;
|
|
208
|
+
/** The last turn's servers did not settle: the next one starts them again. */
|
|
209
|
+
unsettled = false;
|
|
210
|
+
/**
|
|
211
|
+
* Why each MCP server last failed to start, as Codex said, from the
|
|
212
|
+
* client's first notification: a fast failure can come before a turn asks.
|
|
213
|
+
*/
|
|
214
|
+
causes = new Map();
|
|
215
|
+
/** The servers a turn went without, and when each was last tried. */
|
|
216
|
+
unavailable = new UnavailableMcpServers();
|
|
217
|
+
constructor(client, cwd) {
|
|
218
|
+
this.client = client;
|
|
219
|
+
this.cwd = cwd;
|
|
220
|
+
}
|
|
221
|
+
/** Note what Codex says of a server's startup on the session's own thread, not a subagent's. */
|
|
222
|
+
observe({ method, params }, threadId) {
|
|
223
|
+
if (method !== "mcpServer/startupStatus/updated")
|
|
224
|
+
return;
|
|
225
|
+
if (threadId && params.threadId && params.threadId !== threadId)
|
|
226
|
+
return;
|
|
227
|
+
const name = String(params.name);
|
|
228
|
+
if (typeof params.error === "string" && params.error)
|
|
229
|
+
this.causes.set(name, codexMcpCause(name, params.error));
|
|
230
|
+
// Ready, or starting again: an old cause says nothing of now.
|
|
231
|
+
else if (params.status === "ready" || params.status === "starting")
|
|
232
|
+
this.causes.delete(name);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Before the thread opens, and before each turn: servers for the home the
|
|
236
|
+
* turn names are written there, checked as Codex took them, and reloaded
|
|
237
|
+
* when they changed.
|
|
238
|
+
*/
|
|
239
|
+
async configure({ servers }) {
|
|
240
|
+
if (servers === undefined)
|
|
241
|
+
return;
|
|
242
|
+
const fingerprint = configFingerprint(servers);
|
|
243
|
+
const changed = this.fingerprint !== fingerprint;
|
|
244
|
+
// A rejected write or changed project layer invalidates the cached identity.
|
|
245
|
+
this.fingerprint = undefined;
|
|
246
|
+
if (changed)
|
|
247
|
+
await writeCodexMcpServers(this.client, servers);
|
|
248
|
+
await assertCodexMcpConfig(this.client, this.cwd, servers);
|
|
249
|
+
if (changed)
|
|
250
|
+
await this.client.request("config/mcpServer/reload", undefined);
|
|
251
|
+
this.fingerprint = fingerprint;
|
|
252
|
+
}
|
|
253
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — checked against Codex's own
|
|
254
|
+
/**
|
|
255
|
+
* The `mcp_servers` table the thread carries, for a turn that names no
|
|
256
|
+
* home: the turn's servers, checked against Codex's own configuration.
|
|
257
|
+
* Read before the thread opens, so no connection is made first, and
|
|
258
|
+
* before each turn.
|
|
259
|
+
*/
|
|
260
|
+
async threadServers({ servers, thread, }) {
|
|
261
|
+
// A home the turn names holds the turn's servers, written there.
|
|
262
|
+
if (servers !== undefined || !thread)
|
|
263
|
+
return undefined;
|
|
264
|
+
const result = (await this.client.request("config/read", { cwd: this.cwd }));
|
|
265
|
+
assertCodexThreadMcp(result.config?.mcp_servers ?? {}, thread);
|
|
266
|
+
return thread;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Get the thread's servers ready for a turn: configured, started, and the
|
|
270
|
+
* ones it goes on without said through `onWarning`. A required server that
|
|
271
|
+
* cannot connect fails the turn.
|
|
272
|
+
*/
|
|
273
|
+
async prepare(threadId, mcp, onWarning) {
|
|
274
|
+
// A server the last turn went without is started again once it is due
|
|
275
|
+
// (not on every turn: a reload is a round trip to every server that is
|
|
276
|
+
// down), or at once when this turn requires it: its old failure must
|
|
277
|
+
// not decide. So is every server after a turn whose servers did not settle.
|
|
278
|
+
const again = this.unavailable.retryNow(mcp.wire).length > 0 || this.unsettled;
|
|
279
|
+
const started = mcp.thread
|
|
280
|
+
? await this.startThreadServers(again)
|
|
281
|
+
: await this.startHomeServers(mcp, again);
|
|
282
|
+
let unavailable;
|
|
283
|
+
try {
|
|
284
|
+
unavailable = await settleCodexMcp(this.client, threadId, mcp.wire, this.causes);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
this.unsettled = true;
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
this.unsettled = false;
|
|
291
|
+
// The turn goes on without a server it does not require, and says so.
|
|
292
|
+
// `started`: this turn tried the ones that were down, so their clock starts now.
|
|
293
|
+
this.unavailable.record(unavailable, () => started);
|
|
294
|
+
await this.unavailable.announce(onWarning);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The servers of a home the turn names: written, checked and reloaded when
|
|
298
|
+
* they changed or are to be started `again`. Whether this turn started them.
|
|
299
|
+
*/
|
|
300
|
+
async startHomeServers(mcp, again) {
|
|
301
|
+
// Forgetting the last write makes `configure` write and reload it again.
|
|
302
|
+
if (again)
|
|
303
|
+
this.fingerprint = undefined;
|
|
304
|
+
const starting = mcp.servers !== undefined && this.fingerprint !== configFingerprint(mcp.servers);
|
|
305
|
+
await this.configure(mcp);
|
|
306
|
+
return starting;
|
|
307
|
+
}
|
|
308
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — a reload starts a thread's own servers again
|
|
309
|
+
/**
|
|
310
|
+
* The servers a thread carries: they started when it opened, on the first
|
|
311
|
+
* turn this process prepares. After that a reload starts the ones that are
|
|
312
|
+
* not running `again` and leaves the others connected. Whether this turn
|
|
313
|
+
* started them.
|
|
314
|
+
*/
|
|
315
|
+
async startThreadServers(again) {
|
|
316
|
+
const reloading = this.prepared && again;
|
|
317
|
+
if (reloading)
|
|
318
|
+
await this.client.request("config/mcpServer/reload", undefined);
|
|
319
|
+
const started = reloading || !this.prepared;
|
|
320
|
+
this.prepared = true;
|
|
321
|
+
return started;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { isThinkingLevel } from "../../../protocol/index.js";
|
|
2
|
+
import { onAbort } from "../../utils/deadline.js";
|
|
3
|
+
import { knownThinkingLevels } from "../thinking-levels.js";
|
|
4
|
+
import { pages } from "./client.js";
|
|
5
|
+
const MODEL_LIST_TIMEOUT_MS = 5_000;
|
|
6
|
+
/** How many pages of `model/list` are read; Codex lists a dozen models in one. */
|
|
7
|
+
const MODEL_LIST_PAGES = 10;
|
|
8
|
+
/** The level a native Codex effort is: `none` and `minimal` are `off`. */
|
|
9
|
+
export function codexThinkingLevel(effort) {
|
|
10
|
+
if (effort === "none" || effort === "minimal")
|
|
11
|
+
return "off";
|
|
12
|
+
return typeof effort === "string" && effort !== "off" && isThinkingLevel(effort)
|
|
13
|
+
? effort
|
|
14
|
+
: undefined;
|
|
15
|
+
}
|
|
16
|
+
function efforts(model) {
|
|
17
|
+
return Array.isArray(model.supportedReasoningEfforts)
|
|
18
|
+
? model.supportedReasoningEfforts.map((option) => option?.reasoningEffort)
|
|
19
|
+
: [];
|
|
20
|
+
}
|
|
21
|
+
/** Every page of `model/list`, hidden models included. Throws when the CLI cannot answer. */
|
|
22
|
+
async function readCodexModels(client) {
|
|
23
|
+
const models = [];
|
|
24
|
+
for await (const page of pages(client, "model/list", { includeHidden: true }, { timeoutMs: MODEL_LIST_TIMEOUT_MS, maxPages: MODEL_LIST_PAGES }))
|
|
25
|
+
models.push(...page);
|
|
26
|
+
return models;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* What each model accepts, by its `model` and `id`. Codex's `none` or
|
|
30
|
+
* `minimal` is `off`, remembered so `off` sends the value the model
|
|
31
|
+
* advertised (`none` when it offers both). A model that lists no efforts at
|
|
32
|
+
* all has none (`levels: []`); one that lists only efforts this build does not
|
|
33
|
+
* know, or says nothing, is left out, so what it accepts stays unknown.
|
|
34
|
+
*/
|
|
35
|
+
function codexModelThinking(models) {
|
|
36
|
+
const thinking = new Map();
|
|
37
|
+
for (const model of models) {
|
|
38
|
+
const native = efforts(model);
|
|
39
|
+
const off = native.includes("none")
|
|
40
|
+
? "none"
|
|
41
|
+
: native.includes("minimal")
|
|
42
|
+
? "minimal"
|
|
43
|
+
: undefined;
|
|
44
|
+
const levels = knownThinkingLevels(native.map(codexThinkingLevel));
|
|
45
|
+
const none = Array.isArray(model.supportedReasoningEfforts) && native.length === 0;
|
|
46
|
+
if (!levels.length && !none)
|
|
47
|
+
continue;
|
|
48
|
+
const defaultEffort = typeof model.defaultReasoningEffort === "string" ? model.defaultReasoningEffort : undefined;
|
|
49
|
+
for (const name of [model.model, model.id])
|
|
50
|
+
if (typeof name === "string")
|
|
51
|
+
thinking.set(name, {
|
|
52
|
+
levels,
|
|
53
|
+
...(off && { off }),
|
|
54
|
+
...(defaultEffort && { defaultEffort }),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return thinking;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The thinking levels each model the logged-in account can run accepts, from
|
|
61
|
+
* `model/list`. `undefined` when the CLI cannot answer; callers fall back to
|
|
62
|
+
* the catalog.
|
|
63
|
+
*/
|
|
64
|
+
export async function readCodexModelThinking(client) {
|
|
65
|
+
try {
|
|
66
|
+
const thinking = codexModelThinking(await readCodexModels(client));
|
|
67
|
+
return thinking.size ? thinking : undefined;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// An older CLI, a slow answer or a closed process: the catalog decides.
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* What Codex's configuration (`~/.codex/config.toml`, a project layer)
|
|
76
|
+
* selects for a new thread in `cwd`. Throws when Codex cannot say.
|
|
77
|
+
*/
|
|
78
|
+
async function readCodexConfig(client, cwd) {
|
|
79
|
+
const result = (await client.request("config/read", { cwd }, MODEL_LIST_TIMEOUT_MS));
|
|
80
|
+
const config = result?.config;
|
|
81
|
+
if (!config)
|
|
82
|
+
throw new Error("codex config/read returned no config");
|
|
83
|
+
return {
|
|
84
|
+
...(typeof config.model === "string" && { model: config.model }),
|
|
85
|
+
effort: typeof config.model_reasoning_effort === "string" ? config.model_reasoning_effort : null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Codex's defaults for a new thread in `cwd`: the model and effort its
|
|
90
|
+
* configuration selects, else the model `model/list` marks default. Throws
|
|
91
|
+
* when Codex cannot say.
|
|
92
|
+
*/
|
|
93
|
+
export async function readCodexThreadDefaults(client, cwd) {
|
|
94
|
+
const { model, effort } = await readCodexConfig(client, cwd);
|
|
95
|
+
if (model !== undefined)
|
|
96
|
+
return { model, effort };
|
|
97
|
+
const listed = (await readCodexModels(client)).find((entry) => entry.isDefault === true);
|
|
98
|
+
return {
|
|
99
|
+
...(typeof listed?.model === "string" && { model: listed.model }),
|
|
100
|
+
effort,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The models Codex offers in a picker, in the shared shape. Hidden models are
|
|
105
|
+
* left out. `configuredModel` is the default when given; otherwise Codex's
|
|
106
|
+
* own default is.
|
|
107
|
+
*/
|
|
108
|
+
export function codexModelDescriptors(models, harness, configuredModel) {
|
|
109
|
+
return models.flatMap((model) => {
|
|
110
|
+
const id = typeof model.model === "string" ? model.model : model.id;
|
|
111
|
+
if (typeof id !== "string" || model.hidden === true)
|
|
112
|
+
return [];
|
|
113
|
+
const levels = knownThinkingLevels(efforts(model).map(codexThinkingLevel));
|
|
114
|
+
const fallback = codexThinkingLevel(model.defaultReasoningEffort);
|
|
115
|
+
return [
|
|
116
|
+
{
|
|
117
|
+
id,
|
|
118
|
+
harness,
|
|
119
|
+
label: typeof model.displayName === "string" && model.displayName ? model.displayName : id,
|
|
120
|
+
...(typeof model.description === "string" &&
|
|
121
|
+
model.description && { description: model.description }),
|
|
122
|
+
...((configuredModel ? id === configuredModel : model.isDefault === true) && {
|
|
123
|
+
isDefault: true,
|
|
124
|
+
}),
|
|
125
|
+
thinking: {
|
|
126
|
+
levels,
|
|
127
|
+
...(fallback && levels.includes(fallback) && { default: fallback }),
|
|
128
|
+
},
|
|
129
|
+
...(Array.isArray(model.inputModalities) && {
|
|
130
|
+
images: model.inputModalities.includes("image"),
|
|
131
|
+
}),
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/** Read the models and the configured default from a running app-server. */
|
|
137
|
+
export async function readCodexListing(client, cwd) {
|
|
138
|
+
const models = await readCodexModels(client);
|
|
139
|
+
const configuredModel = await readCodexConfig(client, cwd).then(({ model }) => model, () => undefined);
|
|
140
|
+
return { models, ...(configuredModel && { configuredModel }) };
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Start `codex app-server` only to list its models, for the login its
|
|
144
|
+
* environment selects (or `login` performs). No thread is started, and the
|
|
145
|
+
* process ends with the listing.
|
|
146
|
+
*/
|
|
147
|
+
export async function listCodexModels(client, cwd, { signal, login, } = {}) {
|
|
148
|
+
const offAbort = onAbort(signal, () => client.close());
|
|
149
|
+
try {
|
|
150
|
+
signal?.throwIfAborted();
|
|
151
|
+
await client.initialize();
|
|
152
|
+
await login?.(client);
|
|
153
|
+
signal?.throwIfAborted();
|
|
154
|
+
return await readCodexListing(client, cwd);
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
offAbort();
|
|
158
|
+
client.close();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { configFingerprint, directoriesFingerprint } from "../config-fingerprint.js";
|
|
2
|
+
/**
|
|
3
|
+
* Why a turn without tools (`disableTools`) fails on Codex. With every
|
|
4
|
+
* feature that offers one turned off, Codex 0.160 still gives a code-mode
|
|
5
|
+
* model its `exec` and `wait`, `apply_patch` inside `exec`, its sub-agent
|
|
6
|
+
* tools and `request_user_input` (LLP 0017, Codex's own tools, which found
|
|
7
|
+
* the same with every `builtInTools` group off), so the
|
|
8
|
+
* model would still act: the turn is refused instead of running tools the
|
|
9
|
+
* caller turned off.
|
|
10
|
+
*/
|
|
11
|
+
export const CODEX_TOOLS_REQUIRED = "Invalid request: Codex cannot run a turn without tools (disableTools), since it gives the model its tools however it is configured. Turn tools on; permission mode plan or default keeps it from changing anything unasked.";
|
|
12
|
+
export function mapSandbox(mode) {
|
|
13
|
+
switch (mode) {
|
|
14
|
+
case "bypass_permissions":
|
|
15
|
+
return "danger-full-access";
|
|
16
|
+
case "accept_edits":
|
|
17
|
+
// Inside the normal sandbox — danger-full is reserved for the
|
|
18
|
+
// explicitly dangerous mode.
|
|
19
|
+
return "workspace-write";
|
|
20
|
+
default:
|
|
21
|
+
// `dont_ask` never asks, so nothing is approved: it reads, and a write,
|
|
22
|
+
// which would need an approval, is refused, as Claude denies what was
|
|
23
|
+
// not approved beforehand.
|
|
24
|
+
return "read-only";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* `default`, `accept_edits` and `plan` ask through the engine broker when one
|
|
29
|
+
* is attached; every other posture auto-decides and the sandbox enforces
|
|
30
|
+
* safety. Under `never` Codex refuses every MCP tool call that needs consent,
|
|
31
|
+
* so a posture that should run them has to ask. In `accept_edits` Codex
|
|
32
|
+
* approves edits inside its workspace-write roots by itself: the file changes
|
|
33
|
+
* it asks about are writes beyond them, which a person decides. A plan turn
|
|
34
|
+
* reads, runs what the user allows and writes nothing: the edits it asks for
|
|
35
|
+
* are declined unasked.
|
|
36
|
+
*/
|
|
37
|
+
function approvalPolicyFor(options) {
|
|
38
|
+
const mode = options.permissionMode ?? "default";
|
|
39
|
+
const asks = mode === "default" || mode === "accept_edits" || mode === "plan";
|
|
40
|
+
return asks && options.onPermissionRequest ? "on-request" : "never";
|
|
41
|
+
}
|
|
42
|
+
/** The process a turn with these options runs in. */
|
|
43
|
+
export function processIdentity(options) {
|
|
44
|
+
return {
|
|
45
|
+
cwd: options.cwd,
|
|
46
|
+
approvalPolicy: approvalPolicyFor(options),
|
|
47
|
+
sandbox: mapSandbox(options.permissionMode),
|
|
48
|
+
systemPromptAppend: options.systemPromptAppend,
|
|
49
|
+
subagentMode: options.subagentMode,
|
|
50
|
+
envFingerprint: configFingerprint(options.env),
|
|
51
|
+
directoriesFingerprint: directoriesFingerprint(options.additionalDirectories),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Whether a turn with these options can run in a process of `existing`'s, on its thread. */
|
|
55
|
+
export function codexSessionCompatible(existing, options) {
|
|
56
|
+
const wanted = processIdentity(options);
|
|
57
|
+
return (existing.cwd === wanted.cwd &&
|
|
58
|
+
existing.approvalPolicy === wanted.approvalPolicy &&
|
|
59
|
+
existing.sandbox === wanted.sandbox &&
|
|
60
|
+
existing.directoriesFingerprint === wanted.directoriesFingerprint &&
|
|
61
|
+
(existing.systemPromptAppend ?? "") === (wanted.systemPromptAppend ?? "") &&
|
|
62
|
+
existing.subagentMode === wanted.subagentMode &&
|
|
63
|
+
existing.envFingerprint === wanted.envFingerprint &&
|
|
64
|
+
(!options.resumeSessionId || options.resumeSessionId === existing.threadId));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* How the harness starts `codex app-server` for a folder, its `env` and a
|
|
68
|
+
* login, at the operator's `inheritConfig`.
|
|
69
|
+
*/
|
|
70
|
+
export function clientOptionsFor(codexPath, { cwd, env }, auth, experimentalApi, inheritConfig = "all") {
|
|
71
|
+
return {
|
|
72
|
+
codexPath,
|
|
73
|
+
cwd,
|
|
74
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
75
|
+
...(experimentalApi && { experimentalApi: true }),
|
|
76
|
+
...(auth && { ephemeralCredentials: true }),
|
|
77
|
+
// @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — a session's home loads no plugins
|
|
78
|
+
// Below `all` a home a turn names is its session's own: none of the
|
|
79
|
+
// user's plugins belong there, and a fresh home clones Codex's plugin
|
|
80
|
+
// catalogue (about 84 MB) as the process starts, before a thread's
|
|
81
|
+
// config could turn it off.
|
|
82
|
+
...(inheritConfig !== "all" && env?.CODEX_HOME && { pluginsOff: true }),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** What a turn whose thread runs managed sub-agents tells the model. */
|
|
86
|
+
const MANAGED_SUBAGENTS = "Collect the results of every subagent before ending your turn. Stop any worker you no longer need. Delegated work cannot continue after this task completes.";
|
|
87
|
+
/**
|
|
88
|
+
* The params `thread/start` and `thread/resume` take for a turn with these
|
|
89
|
+
* options. `mcpServers`: the servers the thread carries in its own config,
|
|
90
|
+
* for a turn that names no home.
|
|
91
|
+
*/
|
|
92
|
+
export function threadParams(options, policy = {}, mcpServers) {
|
|
93
|
+
const identity = processIdentity(options);
|
|
94
|
+
const tools = policy.builtInTools ?? {};
|
|
95
|
+
// @ref LLP 0017#one-nested-config — dotted keys beside a table apply in no fixed order
|
|
96
|
+
// One nested object, never a dotted key: Codex applies a dotted key and a
|
|
97
|
+
// table of the same root in an order that varies.
|
|
98
|
+
const features = {
|
|
99
|
+
// @ref LLP 0001#d14--nothing-the-agent-does-is-off-the-stream — Codex continues a goal by itself
|
|
100
|
+
// While a thread goal is active, Codex starts the next turn itself.
|
|
101
|
+
goals: false,
|
|
102
|
+
// @ref LLP 0001#d12--what-the-agent-asks-the-user-is-first-class-protocol-opted-into-per-turn — questions outside plan mode
|
|
103
|
+
// Outside plan mode Codex answers its question tool "unavailable"
|
|
104
|
+
// without this. With it a question reaches the engine, which declines
|
|
105
|
+
// it for a turn whose client answers none, as in plan mode.
|
|
106
|
+
default_mode_request_user_input: true,
|
|
107
|
+
};
|
|
108
|
+
if (options.subagentMode === "turn")
|
|
109
|
+
features.multi_agent = true;
|
|
110
|
+
if (tools.local === false) {
|
|
111
|
+
features.shell_tool = false;
|
|
112
|
+
features.unified_exec = false;
|
|
113
|
+
}
|
|
114
|
+
if (tools.imageGeneration === false)
|
|
115
|
+
features.image_generation = false;
|
|
116
|
+
// @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — the account's connectors
|
|
117
|
+
// Codex's apps (`codex_apps`) are the ChatGPT account's connectors, which
|
|
118
|
+
// follow the login into any home: below `all` they stay out, as Claude's
|
|
119
|
+
// claude.ai connectors do. Plugins are off there too, on the command line
|
|
120
|
+
// (`clientOptionsFor`).
|
|
121
|
+
if (policy.inheritConfig !== undefined && policy.inheritConfig !== "all")
|
|
122
|
+
features.apps = false;
|
|
123
|
+
const config = { features };
|
|
124
|
+
// Extra directories become writable roots of the workspace-write sandbox;
|
|
125
|
+
// read-only reads everywhere already, full access writes too.
|
|
126
|
+
if (options.additionalDirectories?.length)
|
|
127
|
+
config.sandbox_workspace_write = {
|
|
128
|
+
writable_roots: [...new Set(options.additionalDirectories)],
|
|
129
|
+
};
|
|
130
|
+
if (tools.webSearch === false)
|
|
131
|
+
config.web_search = "disabled";
|
|
132
|
+
// @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — the repository's AGENTS.md
|
|
133
|
+
// Codex reads every AGENTS.md from the repository's root to the working
|
|
134
|
+
// folder by itself; at `none`, none of them. The home's own still loads.
|
|
135
|
+
if (policy.inheritConfig === "none")
|
|
136
|
+
config.project_doc_max_bytes = 0;
|
|
137
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — the thread's own config
|
|
138
|
+
// Beside the servers of Codex's own configuration, by name; no file is
|
|
139
|
+
// written, and a resume without them has none of them.
|
|
140
|
+
if (mcpServers)
|
|
141
|
+
config.mcp_servers = mcpServers;
|
|
142
|
+
const instructions = options.subagentMode === "turn"
|
|
143
|
+
? [options.systemPromptAppend, MANAGED_SUBAGENTS].filter(Boolean).join("\n\n")
|
|
144
|
+
: options.systemPromptAppend;
|
|
145
|
+
return {
|
|
146
|
+
cwd: options.cwd,
|
|
147
|
+
sandbox: identity.sandbox,
|
|
148
|
+
approvalPolicy: identity.approvalPolicy,
|
|
149
|
+
experimentalRawEvents: false,
|
|
150
|
+
persistExtendedHistory: false,
|
|
151
|
+
...(options.model && { model: options.model }),
|
|
152
|
+
...(instructions && { developerInstructions: instructions }),
|
|
153
|
+
config,
|
|
154
|
+
};
|
|
155
|
+
}
|