@zvada/agent-server 0.3.23 → 0.3.24
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 +34 -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 +686 -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 +330 -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 +91 -0
- package/dist/core/processes/lifeline.js +210 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +163 -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 +28 -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 +434 -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 +329 -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 +141 -0
- package/dist/protocol/errors.js +181 -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 +291 -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/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 +24 -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/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- 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 +30 -4
- package/docs/deploy.md +28 -0
- package/docs/harnesses.md +13 -1
- package/package.json +43 -18
- 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 +24 -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/presets.ts +3 -2
- package/src/server/bin.ts +25 -10
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { onAbort } from "../../utils/deadline.js";
|
|
2
|
+
import { SessionResumeError } from "../../utils/errors.js";
|
|
3
|
+
import { ProcessPool } from "../process-pool.js";
|
|
4
|
+
import { authFingerprint, logIn } from "./credentials.js";
|
|
5
|
+
import { CodexHomes, holdHome, homeIdentity } from "./homes.js";
|
|
6
|
+
import { codexLoginMark, codexLoginStore } from "./login.js";
|
|
7
|
+
import { CodexMcpState, codexTurnMcp, threadServersFingerprint } from "./mcp.js";
|
|
8
|
+
import { codexSessionCompatible, processIdentity, threadParams, } from "./options.js";
|
|
9
|
+
import { bindServerRequests } from "./requests.js";
|
|
10
|
+
import { CodexThread } from "./thread-settings.js";
|
|
11
|
+
/**
|
|
12
|
+
* Start the session's thread, or resume the one it names: never a new one in
|
|
13
|
+
* its place, so a resume that fails fails the turn.
|
|
14
|
+
*/
|
|
15
|
+
async function openThread(client, options, params) {
|
|
16
|
+
const resume = options.resumeSessionId;
|
|
17
|
+
const opened = (await client
|
|
18
|
+
.request(resume ? "thread/resume" : "thread/start", resume ? { threadId: resume, ...params } : params)
|
|
19
|
+
.catch((cause) => {
|
|
20
|
+
if (!resume)
|
|
21
|
+
throw cause;
|
|
22
|
+
throw new SessionResumeError(resume, cause instanceof Error ? cause.message : String(cause), {
|
|
23
|
+
cause,
|
|
24
|
+
});
|
|
25
|
+
}));
|
|
26
|
+
const threadId = opened?.thread?.id;
|
|
27
|
+
if (resume && threadId !== resume) {
|
|
28
|
+
throw new SessionResumeError(resume, `Codex returned ${threadId ? `a different thread: ${threadId}` : "no thread id"}`);
|
|
29
|
+
}
|
|
30
|
+
if (!threadId)
|
|
31
|
+
throw new Error("codex app-server thread/start returned no thread id");
|
|
32
|
+
return { ...opened, threadId };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The Codex harness's sessions: what is Codex's own of each logical
|
|
36
|
+
* session's process (its thread, its home, its account, its MCP table), on
|
|
37
|
+
* the process pool, which decides when one is reused, replaced or ended;
|
|
38
|
+
* what outlives a process (its thread settings, its home); and the managed
|
|
39
|
+
* sessions whose native cleanup was not confirmed.
|
|
40
|
+
*/
|
|
41
|
+
export class CodexSessions {
|
|
42
|
+
connect;
|
|
43
|
+
policy;
|
|
44
|
+
pool;
|
|
45
|
+
threads = new Map();
|
|
46
|
+
homes;
|
|
47
|
+
/** Managed sessions whose process was not confirmed stopped: no successor runs until it is. */
|
|
48
|
+
unsettled = new Map();
|
|
49
|
+
constructor(connect, idleMs,
|
|
50
|
+
/** The operator's: what a thread loads by itself, whether every turn needs a home, and Codex's own tools. */
|
|
51
|
+
policy = {}) {
|
|
52
|
+
this.connect = connect;
|
|
53
|
+
this.policy = policy;
|
|
54
|
+
this.homes = new CodexHomes(policy.inheritConfig);
|
|
55
|
+
this.pool = new ProcessPool({
|
|
56
|
+
// An app-server reads its login from its home at its start.
|
|
57
|
+
store: ({ options }) => codexLoginStore(options.env),
|
|
58
|
+
// A process on its store's own login keeps the one it read at its
|
|
59
|
+
// start, and no word of a change outside the engine reaches it: its
|
|
60
|
+
// store's file changing replaces it. One the engine hands a login to
|
|
61
|
+
// (a credential, the user's own login, the turn's key) does not run
|
|
62
|
+
// on the file, and is logged in again at each turn.
|
|
63
|
+
loginMark: ({ options, scope }) => (scope.auth ? undefined : codexLoginMark(options.env)),
|
|
64
|
+
resumeOf: ({ options }) => options.resumeSessionId,
|
|
65
|
+
conversationsOf: (session) => [session.threadId],
|
|
66
|
+
fits: (session, { options, scope }) => codexSessionCompatible(session, options) &&
|
|
67
|
+
session.authFingerprint === authFingerprint(scope.auth) &&
|
|
68
|
+
// A home removed or made again under the process is not the one it
|
|
69
|
+
// runs in: a new process opens the thread there, or fails to, as a
|
|
70
|
+
// cold start would.
|
|
71
|
+
session.home === homeIdentity(options.env?.CODEX_HOME),
|
|
72
|
+
applyLiveChange: (session, turn, signal) => this.applyLiveChange(session, turn, signal),
|
|
73
|
+
spawn: (turn, own, signal) => this.spawn(turn, own, signal),
|
|
74
|
+
}, { idleMs });
|
|
75
|
+
}
|
|
76
|
+
get(sessionId) {
|
|
77
|
+
return this.pool.get(sessionId);
|
|
78
|
+
}
|
|
79
|
+
/** The turn's MCP servers, in the `CODEX_HOME` the session claims when the turn names one. */
|
|
80
|
+
mcpFor(options) {
|
|
81
|
+
return codexTurnMcp(options, this.homes);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The session's process for the turn, which takes it (the pool's rule):
|
|
85
|
+
* the running one when the turn can run there as the same account, else a
|
|
86
|
+
* new one, on the same thread. Codex caches an authenticated model WebSocket across turns, so
|
|
87
|
+
* login alone cannot switch its identity: an account or key change
|
|
88
|
+
* restarts the process and resumes the thread. Same-account token renewal
|
|
89
|
+
* of a login the engine hands it keeps it warm. One on its store's own
|
|
90
|
+
* login is replaced once the store's `auth.json` changed, whatever the
|
|
91
|
+
* change: the engine reads no login to tell a renewal from another
|
|
92
|
+
* account. An unchanged `mcp_servers` table keeps it warm, the servers
|
|
93
|
+
* the thread carries. A thread's config is fixed once it is open, so
|
|
94
|
+
* another table resumes the thread in a new process, which checks them
|
|
95
|
+
* against Codex's configuration again before the thread opens: the user
|
|
96
|
+
* may change it while the old process exits. A home removed or made again
|
|
97
|
+
* under the process starts a new one too.
|
|
98
|
+
*/
|
|
99
|
+
acquire(options, turn, mcp) {
|
|
100
|
+
return this.pool.acquire(options.sessionId, { options, scope: turn, mcp }, turn.signal);
|
|
101
|
+
}
|
|
102
|
+
/** The turn is done with the session: an open process waits for the next one, at most `idleMs`. */
|
|
103
|
+
leave(sessionId, session, turn) {
|
|
104
|
+
if (session.turn !== turn)
|
|
105
|
+
return;
|
|
106
|
+
session.turn = undefined;
|
|
107
|
+
this.pool.leave(sessionId, session);
|
|
108
|
+
}
|
|
109
|
+
/** Keep a managed session whose process could not be confirmed stopped from another turn. */
|
|
110
|
+
quarantine(sessionId, client) {
|
|
111
|
+
this.unsettled.set(sessionId, client);
|
|
112
|
+
}
|
|
113
|
+
/** Whether releasing the session needs its native cleanup confirmed first. */
|
|
114
|
+
managed(sessionId) {
|
|
115
|
+
return this.unsettled.has(sessionId) || this.pool.get(sessionId)?.subagentMode === "turn";
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Stop a quarantined session's process: whether its stop is confirmed now.
|
|
119
|
+
* Nothing, at once, when the session left none.
|
|
120
|
+
*/
|
|
121
|
+
stopUnsettled(sessionId) {
|
|
122
|
+
return this.unsettled
|
|
123
|
+
.get(sessionId)
|
|
124
|
+
?.closeAndWait()
|
|
125
|
+
.then((confirmed) => {
|
|
126
|
+
if (confirmed)
|
|
127
|
+
this.unsettled.delete(sessionId);
|
|
128
|
+
return confirmed;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Unchanged MCP config reloads retain dead remote connections after VM
|
|
133
|
+
* resume: each session's next turn replaces its process and resumes its
|
|
134
|
+
* thread (the strict thread/resume path). Nothing is ended now; a turn
|
|
135
|
+
* still setting up sends no `turn/start` on its process (`CodexTurn`).
|
|
136
|
+
*/
|
|
137
|
+
invalidate() {
|
|
138
|
+
this.pool.invalidate();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The login changed in `store` (every store when none is named): an
|
|
142
|
+
* app-server keeps the one it read at its start, so each of that store's
|
|
143
|
+
* sessions starts its next turn on a new one, resuming its thread. A turn
|
|
144
|
+
* already setting up or running finishes on its own.
|
|
145
|
+
*/
|
|
146
|
+
loginChanged(store) {
|
|
147
|
+
this.pool.loginChanged(store);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Forget the session: its process, its thread settings and its home.
|
|
151
|
+
* Settles once every app-server of the session has exited, one an idle
|
|
152
|
+
* eviction or a replacement ended earlier included (the pool's
|
|
153
|
+
* `release`): a product removes the session's home on this answer, and an
|
|
154
|
+
* exiting app-server still writes there.
|
|
155
|
+
*/
|
|
156
|
+
release(sessionId) {
|
|
157
|
+
const released = this.pool.release(sessionId);
|
|
158
|
+
this.threads.delete(sessionId);
|
|
159
|
+
this.homes.release(sessionId);
|
|
160
|
+
return released;
|
|
161
|
+
}
|
|
162
|
+
/** End every app-server, and settle once each has exited. */
|
|
163
|
+
terminateAll() {
|
|
164
|
+
const closed = this.pool.closeAll();
|
|
165
|
+
this.threads.clear();
|
|
166
|
+
this.homes.clear();
|
|
167
|
+
return closed;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* What changes on the warm process without a restart, for the turn: its
|
|
171
|
+
* account logged in again (a renewed token), and the `mcp_servers` table
|
|
172
|
+
* its thread needs checked against Codex's own configuration. Another
|
|
173
|
+
* table cannot be changed on an open thread, so the thread resumes in a
|
|
174
|
+
* new process. A cancel meanwhile ends the process, as Codex's answer is
|
|
175
|
+
* not waited for; a refused login ends it too, and a refused table read
|
|
176
|
+
* leaves it for the session's next turn.
|
|
177
|
+
*/
|
|
178
|
+
async applyLiveChange(session, { scope: turn, mcp }, signal) {
|
|
179
|
+
session.turn = turn;
|
|
180
|
+
const offAbort = onAbort(signal, () => session.client.close());
|
|
181
|
+
try {
|
|
182
|
+
try {
|
|
183
|
+
await authenticate(session.client, turn);
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
session.turn = undefined;
|
|
187
|
+
session.client.close();
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
let servers;
|
|
191
|
+
try {
|
|
192
|
+
servers = await session.mcp.threadServers(mcp);
|
|
193
|
+
turn.signal.throwIfAborted();
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
session.turn = undefined;
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
if (session.threadServers === threadServersFingerprint(servers))
|
|
200
|
+
return true;
|
|
201
|
+
session.turn = undefined;
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
offAbort();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Start an app-server for the turn and open the session's thread on it.
|
|
210
|
+
* The pool starts it once every app-server of the session before it has
|
|
211
|
+
* exited.
|
|
212
|
+
*/
|
|
213
|
+
async spawn({ options, scope: turn, mcp }, own, signal) {
|
|
214
|
+
// @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — one writer to a thread
|
|
215
|
+
// Codex lets one process write a thread, and refuses a resume while the
|
|
216
|
+
// one before still exits: the pool waited for it.
|
|
217
|
+
const client = await this.connect(options, turn.auth, signal);
|
|
218
|
+
own(client);
|
|
219
|
+
// Held while the process lives, so the folder's identity stays its own.
|
|
220
|
+
const release = holdHome(options.env?.CODEX_HOME);
|
|
221
|
+
client.onClose(release);
|
|
222
|
+
const home = homeIdentity(options.env?.CODEX_HOME);
|
|
223
|
+
const offAbort = onAbort(signal, () => client.close());
|
|
224
|
+
let thread = this.threads.get(options.sessionId);
|
|
225
|
+
if (!thread) {
|
|
226
|
+
thread = new CodexThread();
|
|
227
|
+
this.threads.set(options.sessionId, thread);
|
|
228
|
+
}
|
|
229
|
+
const session = {
|
|
230
|
+
...processIdentity(options),
|
|
231
|
+
client,
|
|
232
|
+
threadId: "",
|
|
233
|
+
authFingerprint: authFingerprint(turn.auth),
|
|
234
|
+
startedBeforeResume: () => this.pool.startedBeforeResume(session),
|
|
235
|
+
...(home && { home }),
|
|
236
|
+
mcp: new CodexMcpState(client, options.cwd),
|
|
237
|
+
threadServers: "",
|
|
238
|
+
// @ref LLP 0017#each-turn-names-its-environments — a resumed thread is back on its local one
|
|
239
|
+
...(this.policy.builtInTools?.local === false && { environments: [] }),
|
|
240
|
+
thread,
|
|
241
|
+
turn,
|
|
242
|
+
};
|
|
243
|
+
client.onNotification((n) => session.mcp.observe(n, session.threadId));
|
|
244
|
+
bindServerRequests(session);
|
|
245
|
+
try {
|
|
246
|
+
signal.throwIfAborted();
|
|
247
|
+
await client.initialize();
|
|
248
|
+
await authenticate(client, turn);
|
|
249
|
+
// Before the thread opens: a home is cleared of the turn before's
|
|
250
|
+
// servers; a set the thread carries is checked against Codex's own.
|
|
251
|
+
await session.mcp.configure(mcp);
|
|
252
|
+
const servers = await session.mcp.threadServers(mcp);
|
|
253
|
+
session.threadServers = threadServersFingerprint(servers);
|
|
254
|
+
const opened = await openThread(client, options, threadParams(options, this.policy, servers));
|
|
255
|
+
signal.throwIfAborted();
|
|
256
|
+
session.threadId = opened.threadId;
|
|
257
|
+
thread.adopt(opened, options);
|
|
258
|
+
return session;
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
session.turn = undefined;
|
|
262
|
+
client.setRequestHandler(undefined);
|
|
263
|
+
client.close();
|
|
264
|
+
// A client closed before its hold was taken does not call it.
|
|
265
|
+
release();
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
offAbort();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** Log the turn's account in, unless the turn was cancelled before or meanwhile. */
|
|
274
|
+
async function authenticate(client, turn) {
|
|
275
|
+
turn.signal.throwIfAborted();
|
|
276
|
+
await logIn(client, turn.auth);
|
|
277
|
+
turn.signal.throwIfAborted();
|
|
278
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { pages } from "./client.js";
|
|
2
|
+
import { listBackgroundTerminals } from "./commands.js";
|
|
3
|
+
import { codexItem } from "./protocol.js";
|
|
4
|
+
function parentOf(thread) {
|
|
5
|
+
return (thread.parentThreadId ??
|
|
6
|
+
(typeof thread.source === "object"
|
|
7
|
+
? thread.source.subagent?.thread_spawn?.parent_thread_id
|
|
8
|
+
: undefined));
|
|
9
|
+
}
|
|
10
|
+
/** How many threads a managed turn may own before its cleanup gives up. */
|
|
11
|
+
const MAX_DESCENDANTS = 512;
|
|
12
|
+
/** How long Codex has to answer each request of a managed turn's cleanup. */
|
|
13
|
+
const CLEANUP_REQUEST_MS = 2_000;
|
|
14
|
+
/** How long a managed turn's whole cleanup may take before Codex's process is ended. */
|
|
15
|
+
const CLEANUP_MS = 5 * CLEANUP_REQUEST_MS;
|
|
16
|
+
/** Native ownership and cleanup for ONE execution; scheduling stays in Codex. */
|
|
17
|
+
export class CodexTurnSubagents {
|
|
18
|
+
client;
|
|
19
|
+
root;
|
|
20
|
+
parents = new Map();
|
|
21
|
+
candidates = new Set();
|
|
22
|
+
active = new Set();
|
|
23
|
+
activeAtParentEnd = new Set();
|
|
24
|
+
constructor(client, root) {
|
|
25
|
+
this.client = client;
|
|
26
|
+
this.root = root;
|
|
27
|
+
}
|
|
28
|
+
observe(notification) {
|
|
29
|
+
const id = notification.params.threadId;
|
|
30
|
+
if (id && id !== this.root) {
|
|
31
|
+
if (notification.method === "turn/started")
|
|
32
|
+
this.active.add(id);
|
|
33
|
+
if (notification.method === "turn/completed")
|
|
34
|
+
this.active.delete(id);
|
|
35
|
+
if (notification.method === "thread/status/changed") {
|
|
36
|
+
const status = notification.params.status;
|
|
37
|
+
if (status?.type === "active")
|
|
38
|
+
this.active.add(id);
|
|
39
|
+
else if (status?.type)
|
|
40
|
+
this.active.delete(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (id === this.root && notification.method === "turn/completed")
|
|
44
|
+
this.activeAtParentEnd = new Set(this.active);
|
|
45
|
+
if (notification.method === "thread/started") {
|
|
46
|
+
const thread = notification.params.thread;
|
|
47
|
+
const parent = thread && parentOf(thread);
|
|
48
|
+
if (thread?.id && parent) {
|
|
49
|
+
this.parents.set(thread.id, parent);
|
|
50
|
+
this.candidates.add(thread.id);
|
|
51
|
+
if (thread.status?.type === "active")
|
|
52
|
+
this.active.add(thread.id);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (notification.params.threadId !== this.root)
|
|
56
|
+
return;
|
|
57
|
+
const item = codexItem(notification);
|
|
58
|
+
if (item?.type === "subAgentActivity" && item.agentThreadId)
|
|
59
|
+
this.candidates.add(item.agentThreadId);
|
|
60
|
+
if (item?.type === "collabAgentToolCall") {
|
|
61
|
+
for (const id of item.receiverThreadIds ?? [])
|
|
62
|
+
this.candidates.add(id);
|
|
63
|
+
for (const [id, state] of Object.entries(item.agentsStates ?? {})) {
|
|
64
|
+
if (state.status === "running" || state.status === "pendingInit")
|
|
65
|
+
this.active.add(id);
|
|
66
|
+
else
|
|
67
|
+
this.active.delete(id);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async read(id) {
|
|
72
|
+
const result = (await this.client.request("thread/read", { threadId: id, includeTurns: true }, CLEANUP_REQUEST_MS));
|
|
73
|
+
if (result.thread?.id !== id)
|
|
74
|
+
throw new Error("Codex did not confirm the requested child thread");
|
|
75
|
+
const parent = parentOf(result.thread);
|
|
76
|
+
if (parent)
|
|
77
|
+
this.parents.set(id, parent);
|
|
78
|
+
return result.thread;
|
|
79
|
+
}
|
|
80
|
+
async owns(id) {
|
|
81
|
+
const visited = new Set();
|
|
82
|
+
let current = id;
|
|
83
|
+
while (current !== this.root) {
|
|
84
|
+
if (visited.has(current) || visited.size >= 32)
|
|
85
|
+
return false;
|
|
86
|
+
visited.add(current);
|
|
87
|
+
const parent = this.parents.get(current) ?? parentOf(await this.read(current));
|
|
88
|
+
if (!parent)
|
|
89
|
+
return false;
|
|
90
|
+
current = parent;
|
|
91
|
+
}
|
|
92
|
+
if (id !== this.root)
|
|
93
|
+
this.candidates.add(id);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
async descendants() {
|
|
97
|
+
// The native inventory closes missed/start-notification races, including nested workers.
|
|
98
|
+
for await (const page of pages(this.client, "thread/loaded/list", { limit: 100 }, { timeoutMs: CLEANUP_REQUEST_MS })) {
|
|
99
|
+
for (const id of page)
|
|
100
|
+
this.candidates.add(id);
|
|
101
|
+
if (this.candidates.size > MAX_DESCENDANTS)
|
|
102
|
+
throw new Error("Codex child inventory exceeds the managed turn limit");
|
|
103
|
+
}
|
|
104
|
+
const threads = [];
|
|
105
|
+
for (const id of this.candidates) {
|
|
106
|
+
if (id !== this.root && (await this.owns(id)))
|
|
107
|
+
threads.push(await this.read(id));
|
|
108
|
+
}
|
|
109
|
+
return threads;
|
|
110
|
+
}
|
|
111
|
+
/** Root must have stopped first. Returns whether it left unfinished delegated work. */
|
|
112
|
+
async settle() {
|
|
113
|
+
let unfinished = false;
|
|
114
|
+
for (const id of this.activeAtParentEnd) {
|
|
115
|
+
if (await this.owns(id))
|
|
116
|
+
unfinished = true;
|
|
117
|
+
}
|
|
118
|
+
let idleInventory;
|
|
119
|
+
for (let pass = 0; pass < 8; pass++) {
|
|
120
|
+
let stoppedWork = false;
|
|
121
|
+
const descendants = await this.descendants();
|
|
122
|
+
for (const thread of descendants) {
|
|
123
|
+
if (!thread.status)
|
|
124
|
+
throw new Error("Codex child liveness is unknown");
|
|
125
|
+
if (thread.status.type === "active") {
|
|
126
|
+
unfinished = true;
|
|
127
|
+
stoppedWork = true;
|
|
128
|
+
const active = thread.turns
|
|
129
|
+
?.slice()
|
|
130
|
+
.reverse()
|
|
131
|
+
.find((turn) => turn.status === "inProgress");
|
|
132
|
+
// Empty IDs are native startup interrupts, acknowledged on submission.
|
|
133
|
+
// The read below and final EOF cleanup still have to establish settlement.
|
|
134
|
+
// A worker can finish naturally between the inventory and the interrupt.
|
|
135
|
+
await this.client
|
|
136
|
+
.request("turn/interrupt", { threadId: thread.id, turnId: active?.id ?? "" }, CLEANUP_REQUEST_MS)
|
|
137
|
+
.catch(async (error) => {
|
|
138
|
+
if ((await this.read(thread.id)).status?.type !== "idle")
|
|
139
|
+
throw error;
|
|
140
|
+
});
|
|
141
|
+
const stopped = await this.read(thread.id);
|
|
142
|
+
if (!stopped.status || stopped.status.type === "active")
|
|
143
|
+
throw new Error("Codex child interrupt was not confirmed");
|
|
144
|
+
}
|
|
145
|
+
if (thread.status.type === "notLoaded")
|
|
146
|
+
continue;
|
|
147
|
+
if ((await listBackgroundTerminals(this.client, thread.id, CLEANUP_REQUEST_MS)).length) {
|
|
148
|
+
unfinished = true;
|
|
149
|
+
stoppedWork = true;
|
|
150
|
+
await this.client.request("thread/backgroundTerminals/clean", { threadId: thread.id }, CLEANUP_REQUEST_MS);
|
|
151
|
+
if ((await listBackgroundTerminals(this.client, thread.id, CLEANUP_REQUEST_MS)).length)
|
|
152
|
+
throw new Error("Codex child terminals did not stop");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Interrupting a worker may race its final spawn. Reconcile the native
|
|
156
|
+
// inventory after stops, while the root can no longer create new work.
|
|
157
|
+
const inventory = descendants
|
|
158
|
+
.map((thread) => thread.id)
|
|
159
|
+
.sort()
|
|
160
|
+
.join("\n");
|
|
161
|
+
if (!stoppedWork && inventory === idleInventory)
|
|
162
|
+
return unfinished;
|
|
163
|
+
idleInventory = stoppedWork ? undefined : inventory;
|
|
164
|
+
}
|
|
165
|
+
throw new Error("Codex descendants did not settle within the managed cleanup bound");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* How a managed turn (`subagentMode: "turn"`) stops: once Codex's turn ends,
|
|
170
|
+
* or a cancel comes, every worker it delegated to is stopped and its process
|
|
171
|
+
* ends by its own shutdown, whose confirmation is the cancel's. A managed turn
|
|
172
|
+
* never asks about a plan, never puts the thread's settings back and never
|
|
173
|
+
* holds steers: it ends with its native cleanup (`continues` is false). A
|
|
174
|
+
* cleanup that is not confirmed quarantines the session.
|
|
175
|
+
*/
|
|
176
|
+
export class ManagedStop {
|
|
177
|
+
subagents;
|
|
178
|
+
client;
|
|
179
|
+
threadId;
|
|
180
|
+
commands;
|
|
181
|
+
signal;
|
|
182
|
+
graceMs;
|
|
183
|
+
quarantine;
|
|
184
|
+
continues = false;
|
|
185
|
+
finishing;
|
|
186
|
+
constructor(subagents, client, threadId, commands, signal,
|
|
187
|
+
/**
|
|
188
|
+
* How long Codex has to answer the `turn/interrupt` that stops its turn
|
|
189
|
+
* before its process is ended (`cancelGraceMs`).
|
|
190
|
+
*/
|
|
191
|
+
graceMs,
|
|
192
|
+
/** Keep the session from another turn until its process is confirmed stopped. */
|
|
193
|
+
quarantine) {
|
|
194
|
+
this.subagents = subagents;
|
|
195
|
+
this.client = client;
|
|
196
|
+
this.threadId = threadId;
|
|
197
|
+
this.commands = commands;
|
|
198
|
+
this.signal = signal;
|
|
199
|
+
this.graceMs = graceMs;
|
|
200
|
+
this.quarantine = quarantine;
|
|
201
|
+
}
|
|
202
|
+
nativeEnded(native, n) {
|
|
203
|
+
native.queue.push(n);
|
|
204
|
+
void this.finish(native);
|
|
205
|
+
}
|
|
206
|
+
cancel(native) {
|
|
207
|
+
return this.finish(native);
|
|
208
|
+
}
|
|
209
|
+
async outcomeUnknown() {
|
|
210
|
+
// Managed turns stop with their process: its confirmation is the cancel's.
|
|
211
|
+
return (await this.finishing) ? [] : this.commands.notDone();
|
|
212
|
+
}
|
|
213
|
+
async dispose() {
|
|
214
|
+
if (this.finishing)
|
|
215
|
+
await this.finishing;
|
|
216
|
+
// Every exit owns cleanup, including turn/start failure and iterator.return().
|
|
217
|
+
if (!(await this.client.closeAndWait()))
|
|
218
|
+
this.quarantine();
|
|
219
|
+
}
|
|
220
|
+
finish(native) {
|
|
221
|
+
this.finishing ??= (async () => {
|
|
222
|
+
let confirmed = true;
|
|
223
|
+
let failure;
|
|
224
|
+
const deadline = setTimeout(() => {
|
|
225
|
+
void this.client.closeAndWait();
|
|
226
|
+
}, CLEANUP_MS);
|
|
227
|
+
try {
|
|
228
|
+
// An error ended Codex's turn for the stream, not for Codex.
|
|
229
|
+
if (!native?.outcome || native.outcome === "error") {
|
|
230
|
+
// A startup interrupt only acknowledges submission, not native settlement.
|
|
231
|
+
if (!native?.id)
|
|
232
|
+
throw new Error("Codex cancelled before turn admission was observed");
|
|
233
|
+
await this.client.request("turn/interrupt", { threadId: this.threadId, turnId: native.id }, this.graceMs);
|
|
234
|
+
}
|
|
235
|
+
const unfinished = await this.subagents.settle();
|
|
236
|
+
// Native terminal registration can land after turn/completed and
|
|
237
|
+
// after an empty terminal inventory. EOF awaits native thread/tool
|
|
238
|
+
// shutdown; an idle snapshot alone cannot release a managed turn.
|
|
239
|
+
confirmed = await this.client.closeAndWait();
|
|
240
|
+
if (!confirmed)
|
|
241
|
+
throw new Error("Codex native cleanup was not confirmed");
|
|
242
|
+
if (unfinished && !this.signal.aborted) {
|
|
243
|
+
failure = new Error("Parent finished with outstanding subagent work; workers were stopped and the result is incomplete");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
failure = error;
|
|
248
|
+
confirmed = await this.client.closeAndWait();
|
|
249
|
+
if (!confirmed)
|
|
250
|
+
this.quarantine();
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
clearTimeout(deadline);
|
|
254
|
+
}
|
|
255
|
+
native?.finish(this.signal.aborted ? new Error("turn aborted") : failure);
|
|
256
|
+
return confirmed;
|
|
257
|
+
})();
|
|
258
|
+
return this.finishing;
|
|
259
|
+
}
|
|
260
|
+
}
|