@mininglamp-oss/octo-runtime 1.1.10 → 1.1.11
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/README.md +1 -1
- package/apps/cli/README.md +1 -1
- package/apps/cli/dist/adapters.js +40 -14
- package/apps/cli/dist/build-info.json +2 -2
- package/apps/cli/dist/command-tree.js +2 -3
- package/apps/cli/dist/extras.js +11 -1
- package/apps/cli/dist/index.js +14 -1
- package/apps/cli/dist/main.js +58 -23
- package/apps/cli/dist/module-manifest.js +21 -3
- package/apps/cli/dist/output.d.ts +19 -1
- package/apps/cli/dist/output.js +179 -27
- package/apps/cli/dist/playboard.js +2 -2
- package/apps/cli/dist/state.d.ts +5 -2
- package/apps/cli/dist/state.js +48 -13
- package/dist/public/codex.d.ts +1 -0
- package/dist/public/codex.js +1 -0
- package/dist/public/executor-kernel.d.ts +4 -0
- package/dist/public/executor-kernel.js +2 -0
- package/dist/public/extra.d.ts +1 -1
- package/dist/public/host.d.ts +5 -0
- package/dist/public/host.js +2 -0
- package/node_modules/@mininglamp-oss/octo-runtime-acp-client/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/dist/index.js +20 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-antigravity/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-claude-code/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codebuddy/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.d.ts +6 -6
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.js +68 -253
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/index.d.ts +11 -6
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/index.js +198 -52
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/service.d.ts +58 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/service.js +650 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.d.ts +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.js +15 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-grok-build/package.json +5 -5
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-hermes/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-kimi-code/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-mcode/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/dist/index.d.ts +2 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/package.json +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-qodercli/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-qoderclicn/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/activity.d.ts +5 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/execution-scope.d.ts +25 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/execution-scope.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/index.d.ts +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapters.js +40 -14
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/build-info.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/command-tree.js +2 -3
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/extras.js +11 -1
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/index.js +14 -1
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.js +58 -23
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/module-manifest.js +21 -3
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.d.ts +19 -1
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.js +179 -27
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard.js +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/state.d.ts +5 -2
- package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/state.js +48 -13
- package/node_modules/@mininglamp-oss/octo-runtime-cli/package.json +23 -23
- package/node_modules/@mininglamp-oss/octo-runtime-client/build-info.json +5 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/async-question-reply.d.ts +30 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/async-question-reply.js +199 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/browser.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/browser.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/client.d.ts +10 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/client.js +465 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/index.d.ts +6 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/index.js +6 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/internal.d.ts +4 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/internal.js +5 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/invocation-lifecycle.d.ts +59 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/invocation-lifecycle.js +287 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/notification.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/notification.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/protocol.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/protocol.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/runtime-path.d.ts +21 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/runtime-path.js +138 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-catalog.d.ts +8 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-catalog.js +65 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-diagnostics.d.ts +82 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-diagnostics.js +371 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/submission-error.d.ts +10 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/submission-error.js +23 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/types.d.ts +72 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/dist/types.js +10 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/package.json +54 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/context-configuration-fixtures.json +1219 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/context-fixtures.json +5988 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-client.behavior.json +56 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-ipc-fixtures.json +53 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-ipc.schema.json +538 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/native-invoke-fixtures.json +116 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/protocol.schema.json +4525 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/run-protocol-fixtures.json +761 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/runtime-selection-fixtures.json +663 -0
- package/node_modules/@mininglamp-oss/octo-runtime-client/schema/session-name-fixtures.json +191 -0
- package/node_modules/@mininglamp-oss/octo-runtime-cross-service/package.json +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.d.ts +26 -18
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.js +151 -11
- package/node_modules/@mininglamp-oss/octo-runtime-executor-core/package.json +3 -3
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.d.ts +1 -30
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.js +2 -199
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.d.ts +1 -10
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.js +2 -446
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/endpoint.js +2 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.d.ts +2 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.js +13 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.d.ts +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.js +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-lifecycle.d.ts +1 -53
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-lifecycle.js +2 -280
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/lifecycle.d.ts +5 -10
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/lifecycle.js +4 -1
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-home.d.ts +20 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-home.js +180 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.js +31 -8
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.d.ts +5 -2
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.js +48 -5
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-catalog.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-catalog.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-diagnostics.d.ts +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-diagnostics.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-logging.d.ts +4 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-logging.js +18 -0
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/submission-error.d.ts +1 -10
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/submission-error.js +2 -23
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/types.d.ts +2 -72
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/types.js +2 -10
- package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/package.json +5 -4
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/extra.js +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/ports.d.ts +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/package.json +6 -6
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/dist/extra.js +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/dist/ports.d.ts +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/package.json +5 -5
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/index.js +4 -4
- package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/package.json +2 -2
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/endpoint.d.ts +1 -1
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/endpoint.js +12 -5
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/errors.d.ts +5 -1
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/errors.js +9 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/execution.d.ts +44 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/execution.js +207 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-config.js +23 -3
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-service.d.ts +27 -4
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-service.js +133 -25
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extras.d.ts +10 -3
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extras.js +18 -4
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/home.d.ts +5 -6
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/home.js +33 -45
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/identity.js +6 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/index.d.ts +4 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/index.js +3 -0
- package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/package.json +5 -4
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/adapter-metadata.d.ts +7 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/adapter-metadata.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/generated/protocol-types.d.ts +19 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.d.ts +3 -1
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.js +1 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/service-processes.d.ts +5 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/service-processes.js +65 -0
- package/node_modules/@mininglamp-oss/octo-runtime-protocol/package.json +1 -1
- package/package.json +33 -27
- package/packages/octo-runtime-protocol/schema/protocol.schema.json +42 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
6
|
+
import { createProcessCleanup } from "@mininglamp-oss/octo-runtime-adapter-sdk/process-cleanup";
|
|
7
|
+
import { resolveProcessLaunch } from "@mininglamp-oss/octo-runtime-adapter-sdk/process-launch";
|
|
8
|
+
import { watchLineLimit } from "@mininglamp-oss/octo-runtime-adapter-sdk";
|
|
9
|
+
import { DEFAULT_STREAM_LIMIT_POLICY, INVOKE_REQUEST_MAX_BYTES } from "@mininglamp-oss/octo-runtime-protocol";
|
|
10
|
+
import { CodexStdinWriter, codexTransportError } from "./stdin-writer.js";
|
|
11
|
+
/**
|
|
12
|
+
* 一个调用的协议所有权;只有 service 持有进程和 stdio。
|
|
13
|
+
* channel 关闭只退订自己的 thread,不能终止共享进程或撤销其他调用的写入。
|
|
14
|
+
*/
|
|
15
|
+
export class CodexTurnChannel {
|
|
16
|
+
service;
|
|
17
|
+
incarnation;
|
|
18
|
+
allowInputEcho;
|
|
19
|
+
onMessage = () => { };
|
|
20
|
+
onFailure = () => { };
|
|
21
|
+
threadId;
|
|
22
|
+
turnId;
|
|
23
|
+
terminal = false;
|
|
24
|
+
closed = false;
|
|
25
|
+
startingTurn = false;
|
|
26
|
+
startingThread = false;
|
|
27
|
+
buffered = [];
|
|
28
|
+
bufferedBytes = 0;
|
|
29
|
+
#close;
|
|
30
|
+
constructor(service, incarnation, allowInputEcho = false) {
|
|
31
|
+
this.service = service;
|
|
32
|
+
this.incarnation = incarnation;
|
|
33
|
+
this.allowInputEcho = allowInputEcho;
|
|
34
|
+
}
|
|
35
|
+
reserveRequestId() {
|
|
36
|
+
return this.service.reserveRequestId();
|
|
37
|
+
}
|
|
38
|
+
write(message, onError, onWritten, options = {}) {
|
|
39
|
+
return this.service.write(this, message, onError, onWritten, options);
|
|
40
|
+
}
|
|
41
|
+
cancelPending(error) {
|
|
42
|
+
this.service.cancelPending(this, error);
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
return (this.#close ??= this.service.release(this));
|
|
46
|
+
}
|
|
47
|
+
errorMessage(error) {
|
|
48
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
49
|
+
const tail = this.service.stderr(this.incarnation);
|
|
50
|
+
return tail.trim() ? `${text}\n\n[codex stderr]\n${tail.trim()}` : text;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Core 作用域内的一种兼容配置;惰性首次启动,之后空闲退出也主动恢复,业务动作从不重放。 */
|
|
54
|
+
export class CodexService {
|
|
55
|
+
scopeId;
|
|
56
|
+
spec;
|
|
57
|
+
spawnFn;
|
|
58
|
+
rawMessageMaxBytes;
|
|
59
|
+
id = randomUUID();
|
|
60
|
+
#state = "not_started";
|
|
61
|
+
#current;
|
|
62
|
+
#generation = 0;
|
|
63
|
+
#nextId = 0;
|
|
64
|
+
#closing = false;
|
|
65
|
+
#supervision;
|
|
66
|
+
#close;
|
|
67
|
+
#failure;
|
|
68
|
+
#episodeStart = 0;
|
|
69
|
+
#replacements = 0;
|
|
70
|
+
#stableTimer;
|
|
71
|
+
#stop = new AbortController();
|
|
72
|
+
#pending = new Map();
|
|
73
|
+
#channels = new Set();
|
|
74
|
+
#threads = new Map();
|
|
75
|
+
#usedThreads = new Set();
|
|
76
|
+
#serverRequests = new Map();
|
|
77
|
+
#early = new Map();
|
|
78
|
+
#earlyBytes = 0;
|
|
79
|
+
#earlyItems = 0;
|
|
80
|
+
#waiters = 0;
|
|
81
|
+
#waitingBytes = 0;
|
|
82
|
+
constructor(scopeId, spec, spawnFn = ownedSpawn, rawMessageMaxBytes = DEFAULT_STREAM_LIMIT_POLICY.rawMessageMaxBytes) {
|
|
83
|
+
this.scopeId = scopeId;
|
|
84
|
+
this.spec = spec;
|
|
85
|
+
this.spawnFn = spawnFn;
|
|
86
|
+
this.rawMessageMaxBytes = rawMessageMaxBytes;
|
|
87
|
+
}
|
|
88
|
+
/** 取消等待不取消共享启动;等待者数和活动 thread 数分别有界。 */
|
|
89
|
+
async acquire(signal, retainedBytes = 0, allowInputEcho = false) {
|
|
90
|
+
if (this.#closing)
|
|
91
|
+
throw codexTransportError("CODEX_SERVICE_CLOSED");
|
|
92
|
+
const queuedBytes = this.#state === "ready" ? 0 : retainedBytes;
|
|
93
|
+
if (this.#waitingBytes + queuedBytes > 32 * 1024 * 1024)
|
|
94
|
+
throw codexTransportError("CODEX_SERVICE_WAITING_BYTES_EXCEEDED");
|
|
95
|
+
if (++this.#waiters > 128) {
|
|
96
|
+
this.#waiters--;
|
|
97
|
+
throw codexTransportError("CODEX_SERVICE_WAITERS_EXCEEDED");
|
|
98
|
+
}
|
|
99
|
+
this.#waitingBytes += queuedBytes;
|
|
100
|
+
try {
|
|
101
|
+
signal?.throwIfAborted();
|
|
102
|
+
const ready = this.#state === "ready" ? Promise.resolve() : this.#supervise(false);
|
|
103
|
+
let abort;
|
|
104
|
+
try {
|
|
105
|
+
await (signal
|
|
106
|
+
? Promise.race([
|
|
107
|
+
ready,
|
|
108
|
+
new Promise((_, reject) => {
|
|
109
|
+
abort = () => reject(signal.reason instanceof Error
|
|
110
|
+
? signal.reason
|
|
111
|
+
: codexTransportError("CODEX_INVOCATION_CANCELLED"));
|
|
112
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
113
|
+
if (signal.aborted)
|
|
114
|
+
abort();
|
|
115
|
+
})
|
|
116
|
+
])
|
|
117
|
+
: ready);
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
if (abort)
|
|
121
|
+
signal?.removeEventListener("abort", abort);
|
|
122
|
+
}
|
|
123
|
+
signal?.throwIfAborted();
|
|
124
|
+
if (this.#closing || this.#state !== "ready")
|
|
125
|
+
throw codexTransportError("CODEX_SERVICE_UNAVAILABLE");
|
|
126
|
+
if (this.#channels.size >= 8)
|
|
127
|
+
throw codexTransportError("CODEX_SERVICE_TURNS_EXCEEDED");
|
|
128
|
+
const channel = new CodexTurnChannel(this, this.#generation, allowInputEcho);
|
|
129
|
+
this.#channels.add(channel);
|
|
130
|
+
return channel;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
this.#waiters--;
|
|
134
|
+
this.#waitingBytes -= queuedBytes;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
snapshot() {
|
|
138
|
+
const current = this.#current;
|
|
139
|
+
return {
|
|
140
|
+
scope_id: this.scopeId,
|
|
141
|
+
service_id: this.id,
|
|
142
|
+
incarnation: this.#generation,
|
|
143
|
+
provider: "codex",
|
|
144
|
+
owner_pid: process.pid,
|
|
145
|
+
...(current?.child.pid ? { pid: current.child.pid } : {}),
|
|
146
|
+
state: this.#state,
|
|
147
|
+
observation: !current
|
|
148
|
+
? "unknown"
|
|
149
|
+
: current.exited || current.child.exitCode !== null || current.child.signalCode !== null
|
|
150
|
+
? "exited"
|
|
151
|
+
: "alive",
|
|
152
|
+
ownership: "owned"
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
stderr(generation) {
|
|
156
|
+
return this.#current?.number === generation ? this.#current.stderr : "";
|
|
157
|
+
}
|
|
158
|
+
reserveRequestId() {
|
|
159
|
+
return ++this.#nextId;
|
|
160
|
+
}
|
|
161
|
+
write(channel, message, onError, onWritten, options = {}) {
|
|
162
|
+
const current = this.#current;
|
|
163
|
+
if (channel.closed ||
|
|
164
|
+
!current ||
|
|
165
|
+
current.failed ||
|
|
166
|
+
channel.incarnation !== current.number ||
|
|
167
|
+
this.#closing) {
|
|
168
|
+
onError(codexTransportError("CODEX_SERVICE_INCARNATION_LOST"));
|
|
169
|
+
return () => { };
|
|
170
|
+
}
|
|
171
|
+
const id = rpcId(message.id);
|
|
172
|
+
const method = typeof message.method === "string" ? message.method : undefined;
|
|
173
|
+
if (id !== undefined && method) {
|
|
174
|
+
const params = record(message.params);
|
|
175
|
+
try {
|
|
176
|
+
if (method === "thread/start") {
|
|
177
|
+
this.#reserveThreadHistory();
|
|
178
|
+
channel.startingThread = true;
|
|
179
|
+
}
|
|
180
|
+
if (method === "thread/resume")
|
|
181
|
+
this.#claimThread(channel, text(params.threadId));
|
|
182
|
+
if (method === "turn/start") {
|
|
183
|
+
if (params.threadId !== channel.threadId)
|
|
184
|
+
throw codexTransportError("CODEX_THREAD_OWNER_MISMATCH");
|
|
185
|
+
channel.startingTurn = true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
onError(asError(error));
|
|
190
|
+
return () => { };
|
|
191
|
+
}
|
|
192
|
+
this.#pending.set(id, { method, channel });
|
|
193
|
+
}
|
|
194
|
+
else if (id !== undefined) {
|
|
195
|
+
// Provider request IDs belong to the original incarnation and channel, including native replies.
|
|
196
|
+
if (this.#serverRequests.get(id) !== channel) {
|
|
197
|
+
onError(codexTransportError("INTERACTION_NOT_ACTIVE"));
|
|
198
|
+
return () => { };
|
|
199
|
+
}
|
|
200
|
+
this.#serverRequests.delete(id);
|
|
201
|
+
}
|
|
202
|
+
const cancel = current.writer.write(message, (error) => {
|
|
203
|
+
if (method && id !== undefined)
|
|
204
|
+
this.#pending.delete(id);
|
|
205
|
+
onError(error);
|
|
206
|
+
}, onWritten, { ...options, owner: channel });
|
|
207
|
+
return () => {
|
|
208
|
+
cancel();
|
|
209
|
+
if (method && id !== undefined)
|
|
210
|
+
this.#pending.delete(id);
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
cancelPending(channel, error) {
|
|
214
|
+
this.#current?.writer.cancelOwner(channel, error);
|
|
215
|
+
}
|
|
216
|
+
/** 未确认中断/退订时保留 thread 和容量,阻止迟到通知进入后续调用。 */
|
|
217
|
+
async release(channel) {
|
|
218
|
+
if (channel.closed)
|
|
219
|
+
return;
|
|
220
|
+
channel.closed = true;
|
|
221
|
+
this.cancelPending(channel, codexTransportError("INTERACTION_NOT_ACTIVE"));
|
|
222
|
+
for (const [id, pending] of this.#pending)
|
|
223
|
+
if (pending.channel === channel)
|
|
224
|
+
this.#pending.delete(id);
|
|
225
|
+
for (const [id, owner] of this.#serverRequests)
|
|
226
|
+
if (owner === channel) {
|
|
227
|
+
this.#serverRequests.delete(id);
|
|
228
|
+
this.#current?.writer.write({ id, error: { code: -32000, message: "invocation ended" } }, () => { }, undefined, { control: true });
|
|
229
|
+
}
|
|
230
|
+
channel.onMessage = () => { };
|
|
231
|
+
channel.onFailure = () => { };
|
|
232
|
+
channel.buffered.length = 0;
|
|
233
|
+
if (this.#current?.number !== channel.incarnation || this.#current.failed || this.#closing) {
|
|
234
|
+
this.#forget(channel);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
if (channel.startingThread || channel.startingTurn)
|
|
239
|
+
throw codexTransportError("CODEX_TURN_DELIVERY_UNKNOWN");
|
|
240
|
+
if (channel.threadId) {
|
|
241
|
+
if (!channel.terminal && channel.turnId) {
|
|
242
|
+
await this.#rpc("turn/interrupt", { threadId: channel.threadId, turnId: channel.turnId }, 300);
|
|
243
|
+
}
|
|
244
|
+
// Unsubscribe is the native ownership boundary; an ACK for interrupt alone isn't a terminal.
|
|
245
|
+
await this.#rpc("thread/unsubscribe", { threadId: channel.threadId }, 300);
|
|
246
|
+
}
|
|
247
|
+
this.#forget(channel);
|
|
248
|
+
}
|
|
249
|
+
catch (cause) {
|
|
250
|
+
if (this.#current?.failed || this.#closing) {
|
|
251
|
+
this.#forget(channel);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
throw codexTransportError("CODEX_TURN_CLEANUP_INCOMPLETE", cause);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
#forget(channel) {
|
|
258
|
+
this.#channels.delete(channel);
|
|
259
|
+
if (channel.threadId)
|
|
260
|
+
this.#usedThreads.add(channel.threadId);
|
|
261
|
+
if (channel.threadId && this.#threads.get(channel.threadId) === channel)
|
|
262
|
+
this.#threads.delete(channel.threadId);
|
|
263
|
+
}
|
|
264
|
+
#claimThread(channel, id) {
|
|
265
|
+
if (!id)
|
|
266
|
+
throw codexTransportError("CODEX_THREAD_ID_MISSING");
|
|
267
|
+
if (!this.#usedThreads.has(id) && !this.#threads.has(id))
|
|
268
|
+
this.#reserveThreadHistory(channel.startingThread ? 1 : 0);
|
|
269
|
+
const owner = this.#threads.get(id);
|
|
270
|
+
if (owner && owner !== channel)
|
|
271
|
+
throw codexTransportError("CODEX_THREAD_BUSY");
|
|
272
|
+
if (channel.threadId && channel.threadId !== id)
|
|
273
|
+
this.#threads.delete(channel.threadId);
|
|
274
|
+
channel.threadId = id;
|
|
275
|
+
this.#threads.set(id, channel);
|
|
276
|
+
}
|
|
277
|
+
/** 发出新 thread 前预留历史槽,避免 ACK 里的容量拒绝污染健康传输。 */
|
|
278
|
+
#reserveThreadHistory(ownReservation = 0) {
|
|
279
|
+
const live = [...this.#threads.keys()].filter((id) => !this.#usedThreads.has(id)).length;
|
|
280
|
+
const starting = [...this.#channels].filter((channel) => channel.startingThread).length;
|
|
281
|
+
if (this.#usedThreads.size + live + starting - ownReservation >= 4096)
|
|
282
|
+
throw codexTransportError("CODEX_SERVICE_THREAD_HISTORY_EXCEEDED");
|
|
283
|
+
}
|
|
284
|
+
#rpc(method, params, timeoutMs = 30_000) {
|
|
285
|
+
const current = this.#current;
|
|
286
|
+
if (!current || current.failed)
|
|
287
|
+
return Promise.reject(codexTransportError("CODEX_SERVICE_UNAVAILABLE"));
|
|
288
|
+
return new Promise((resolve, reject) => {
|
|
289
|
+
const id = this.reserveRequestId();
|
|
290
|
+
const timer = setTimeout(() => {
|
|
291
|
+
this.#pending.delete(id);
|
|
292
|
+
cancel();
|
|
293
|
+
reject(codexTransportError("RPC_TIMEOUT"));
|
|
294
|
+
}, timeoutMs);
|
|
295
|
+
this.#pending.set(id, { method, resolve, reject, timer });
|
|
296
|
+
const cancel = current.writer.write({ id, method, params }, (error) => {
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
this.#pending.delete(id);
|
|
299
|
+
reject(error);
|
|
300
|
+
}, undefined, { control: method === "turn/interrupt", timeoutMs });
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
#handle(current, message) {
|
|
304
|
+
if (current !== this.#current || current.failed)
|
|
305
|
+
return;
|
|
306
|
+
const id = rpcId(message.id);
|
|
307
|
+
if (id !== undefined && ("result" in message || "error" in message)) {
|
|
308
|
+
const pending = this.#pending.get(id);
|
|
309
|
+
if (!pending)
|
|
310
|
+
return;
|
|
311
|
+
this.#pending.delete(id);
|
|
312
|
+
clearTimeout(pending.timer);
|
|
313
|
+
if (pending.channel) {
|
|
314
|
+
const channel = pending.channel;
|
|
315
|
+
if (!message.error) {
|
|
316
|
+
if (pending.method === "thread/start" || pending.method === "thread/resume") {
|
|
317
|
+
const result = record(message.result);
|
|
318
|
+
const thread = record(result.thread);
|
|
319
|
+
this.#claimThread(channel, text(thread.id ?? result.threadId));
|
|
320
|
+
channel.startingThread = false;
|
|
321
|
+
}
|
|
322
|
+
if (pending.method === "turn/start") {
|
|
323
|
+
const result = record(message.result);
|
|
324
|
+
channel.turnId = text(record(result.turn).id ?? result.turnId);
|
|
325
|
+
if (!channel.turnId)
|
|
326
|
+
throw codexTransportError("CODEX_TURN_ID_MISSING");
|
|
327
|
+
channel.startingTurn = false;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
if (pending.method === "turn/start")
|
|
332
|
+
channel.startingTurn = false;
|
|
333
|
+
if (pending.method === "thread/start")
|
|
334
|
+
channel.startingThread = false;
|
|
335
|
+
}
|
|
336
|
+
if (!channel.closed)
|
|
337
|
+
channel.onMessage(message);
|
|
338
|
+
if (channel.threadId) {
|
|
339
|
+
const early = this.#early.get(channel.threadId) ?? [];
|
|
340
|
+
this.#early.delete(channel.threadId);
|
|
341
|
+
for (const value of early) {
|
|
342
|
+
this.#earlyItems--;
|
|
343
|
+
this.#earlyBytes -= Buffer.byteLength(JSON.stringify(value));
|
|
344
|
+
this.#route(channel, value);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (pending.method === "turn/start") {
|
|
348
|
+
const buffered = channel.buffered.splice(0);
|
|
349
|
+
channel.bufferedBytes = 0;
|
|
350
|
+
for (const value of buffered)
|
|
351
|
+
this.#route(channel, value);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else if (message.error)
|
|
355
|
+
pending.reject?.(codexTransportError("CODEX_SERVICE_RPC_REJECTED"));
|
|
356
|
+
else
|
|
357
|
+
pending.resolve?.(message.result);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const params = record(message.params);
|
|
361
|
+
const threadId = text(params.threadId ?? record(params.thread).id);
|
|
362
|
+
const channel = this.#threads.get(threadId);
|
|
363
|
+
if (channel)
|
|
364
|
+
this.#route(channel, message);
|
|
365
|
+
else if (threadId &&
|
|
366
|
+
[...this.#pending.values()].some((value) => value.method === "thread/start")) {
|
|
367
|
+
const bytes = Buffer.byteLength(JSON.stringify(message));
|
|
368
|
+
if (this.#earlyItems >= 128 || this.#earlyBytes + bytes > 32 * 1024 * 1024)
|
|
369
|
+
throw codexTransportError("CODEX_SERVICE_ROUTE_BUFFER_EXCEEDED");
|
|
370
|
+
const entries = this.#early.get(threadId) ?? [];
|
|
371
|
+
entries.push(message);
|
|
372
|
+
this.#early.set(threadId, entries);
|
|
373
|
+
this.#earlyBytes += bytes;
|
|
374
|
+
this.#earlyItems++;
|
|
375
|
+
}
|
|
376
|
+
else if (id !== undefined)
|
|
377
|
+
this.#rejectRequest(current, id, threadId ? -32000 : -32601);
|
|
378
|
+
}
|
|
379
|
+
#route(channel, message) {
|
|
380
|
+
const params = record(message.params);
|
|
381
|
+
const turnId = text(params.turnId ?? record(params.turn).id);
|
|
382
|
+
if (channel.startingTurn && !channel.turnId) {
|
|
383
|
+
const bytes = Buffer.byteLength(JSON.stringify(message));
|
|
384
|
+
if (channel.buffered.length >= 128 || channel.bufferedBytes + bytes > 32 * 1024 * 1024)
|
|
385
|
+
throw codexTransportError("CODEX_SERVICE_ROUTE_BUFFER_EXCEEDED");
|
|
386
|
+
channel.buffered.push(message);
|
|
387
|
+
channel.bufferedBytes += bytes;
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const id = rpcId(message.id);
|
|
391
|
+
if (turnId && turnId !== channel.turnId) {
|
|
392
|
+
if (id !== undefined && this.#current)
|
|
393
|
+
this.#rejectRequest(this.#current, id);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (message.method === "turn/completed")
|
|
397
|
+
channel.terminal = true;
|
|
398
|
+
if (id !== undefined) {
|
|
399
|
+
// A nullable elicitation on a reused thread cannot prove which subscription caused it.
|
|
400
|
+
if (channel.closed ||
|
|
401
|
+
!channel.turnId ||
|
|
402
|
+
this.#serverRequests.has(id) ||
|
|
403
|
+
(!turnId && this.#usedThreads.has(channel.threadId))) {
|
|
404
|
+
this.#rejectRequest(this.#current, id);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (this.#serverRequests.size >= 4096)
|
|
408
|
+
throw codexTransportError("CODEX_SERVICE_INTERACTIONS_EXCEEDED");
|
|
409
|
+
this.#serverRequests.set(id, channel);
|
|
410
|
+
}
|
|
411
|
+
if (!channel.closed)
|
|
412
|
+
channel.onMessage(message);
|
|
413
|
+
}
|
|
414
|
+
#rejectRequest(current, id, code = -32000) {
|
|
415
|
+
current.writer.write({ id, error: { code, message: "request ownership is unknown or inactive" } }, () => { }, undefined, { control: true });
|
|
416
|
+
}
|
|
417
|
+
#fault(current, error) {
|
|
418
|
+
if (current !== this.#current || current.failed)
|
|
419
|
+
return;
|
|
420
|
+
current.failed = true;
|
|
421
|
+
this.#failure = error;
|
|
422
|
+
clearTimeout(this.#stableTimer);
|
|
423
|
+
current.writer.stop(error);
|
|
424
|
+
current.reader.close();
|
|
425
|
+
current.removeGuard();
|
|
426
|
+
for (const pending of this.#pending.values()) {
|
|
427
|
+
clearTimeout(pending.timer);
|
|
428
|
+
pending.reject?.(error);
|
|
429
|
+
}
|
|
430
|
+
this.#pending.clear();
|
|
431
|
+
for (const channel of this.#channels) {
|
|
432
|
+
channel.onFailure(error);
|
|
433
|
+
channel.closed = true;
|
|
434
|
+
}
|
|
435
|
+
this.#channels.clear();
|
|
436
|
+
this.#threads.clear();
|
|
437
|
+
this.#usedThreads.clear();
|
|
438
|
+
this.#serverRequests.clear();
|
|
439
|
+
this.#early.clear();
|
|
440
|
+
this.#earlyBytes = 0;
|
|
441
|
+
this.#earlyItems = 0;
|
|
442
|
+
if (!this.#closing) {
|
|
443
|
+
this.#state = "recovering";
|
|
444
|
+
// The supervisor observes idle failures as well; no business invocation is needed to restart.
|
|
445
|
+
queueMicrotask(() => {
|
|
446
|
+
void this.#supervise(true).catch(() => { });
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
#supervise(recovery) {
|
|
451
|
+
if (this.#supervision)
|
|
452
|
+
return this.#supervision;
|
|
453
|
+
if (this.#closing)
|
|
454
|
+
return Promise.reject(codexTransportError("CODEX_SERVICE_CLOSED"));
|
|
455
|
+
if (this.#state === "degraded" || this.#state === "cleanup_incomplete")
|
|
456
|
+
return Promise.reject(this.#failure ?? codexTransportError("CODEX_SERVICE_DEGRADED"));
|
|
457
|
+
this.#supervision = (async () => {
|
|
458
|
+
if (!this.#episodeStart)
|
|
459
|
+
this.#episodeStart = performance.now();
|
|
460
|
+
let replace = recovery || this.#generation > 0;
|
|
461
|
+
while (!this.#closing) {
|
|
462
|
+
if (this.#current) {
|
|
463
|
+
try {
|
|
464
|
+
await this.#current.cleanup();
|
|
465
|
+
}
|
|
466
|
+
catch (error) {
|
|
467
|
+
this.#state = "cleanup_incomplete";
|
|
468
|
+
this.#failure = asError(error);
|
|
469
|
+
throw error;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (replace) {
|
|
473
|
+
if (++this.#replacements > 5 || performance.now() - this.#episodeStart >= 90_000)
|
|
474
|
+
break;
|
|
475
|
+
this.#state = "recovering";
|
|
476
|
+
try {
|
|
477
|
+
await delay(250 * 2 ** (this.#replacements - 1) * (1 + Math.random() * 0.25), undefined, { signal: this.#stop.signal });
|
|
478
|
+
}
|
|
479
|
+
catch {
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (this.#closing)
|
|
484
|
+
break;
|
|
485
|
+
this.#state = replace ? "recovering" : "starting";
|
|
486
|
+
try {
|
|
487
|
+
await this.#start(Math.min(30_000, 90_000 - (performance.now() - this.#episodeStart)));
|
|
488
|
+
if (this.#closing)
|
|
489
|
+
break;
|
|
490
|
+
this.#state = "ready";
|
|
491
|
+
this.#stableTimer = setTimeout(() => {
|
|
492
|
+
this.#replacements = 0;
|
|
493
|
+
this.#episodeStart = 0;
|
|
494
|
+
}, 60_000);
|
|
495
|
+
this.#stableTimer.unref();
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
catch (error) {
|
|
499
|
+
this.#failure = asError(error);
|
|
500
|
+
if (["ENOENT", "ENOTDIR", "EACCES", "EPERM"].includes(String(error.code)))
|
|
501
|
+
break;
|
|
502
|
+
replace = true;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (!this.#closing)
|
|
506
|
+
this.#state = "degraded";
|
|
507
|
+
throw this.#failure ?? codexTransportError("CODEX_SERVICE_CLOSED");
|
|
508
|
+
})().finally(() => {
|
|
509
|
+
this.#supervision = undefined;
|
|
510
|
+
if (!this.#closing && this.#state === "recovering")
|
|
511
|
+
queueMicrotask(() => {
|
|
512
|
+
void this.#supervise(true).catch(() => { });
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
return this.#supervision;
|
|
516
|
+
}
|
|
517
|
+
async #start(timeoutMs) {
|
|
518
|
+
if (timeoutMs <= 0)
|
|
519
|
+
throw codexTransportError("CODEX_SERVICE_RECOVERY_EXHAUSTED");
|
|
520
|
+
const child = this.spawnFn(this.spec.command, this.spec.args, {
|
|
521
|
+
cwd: this.spec.cwd,
|
|
522
|
+
env: this.spec.env
|
|
523
|
+
});
|
|
524
|
+
// 同预算调用才共享解析边界;仅活动 canonical 图片调用允许有界输入回显。
|
|
525
|
+
const removeGuard = watchLineLimit(child.stdout, () => [...this.#channels].some((channel) => !channel.closed && channel.allowInputEcho)
|
|
526
|
+
? Math.max(this.rawMessageMaxBytes, INVOKE_REQUEST_MAX_BYTES)
|
|
527
|
+
: this.rawMessageMaxBytes, (error) => this.#fault(current, error));
|
|
528
|
+
const current = {
|
|
529
|
+
number: ++this.#generation,
|
|
530
|
+
child,
|
|
531
|
+
writer: new CodexStdinWriter(child.stdin, (error) => this.#fault(current, error), {
|
|
532
|
+
maxItems: 128,
|
|
533
|
+
maxBytes: 32 * 1024 * 1024
|
|
534
|
+
}),
|
|
535
|
+
reader: createInterface({ input: child.stdout }),
|
|
536
|
+
removeGuard,
|
|
537
|
+
cleanup: createProcessCleanup(child, {
|
|
538
|
+
processGroup: groups.has(child),
|
|
539
|
+
processTree: ownedChildren.has(child)
|
|
540
|
+
}),
|
|
541
|
+
failed: false,
|
|
542
|
+
exited: false,
|
|
543
|
+
stderr: ""
|
|
544
|
+
};
|
|
545
|
+
this.#current = current;
|
|
546
|
+
current.reader.on("line", (line) => {
|
|
547
|
+
try {
|
|
548
|
+
this.#handle(current, record(JSON.parse(line)));
|
|
549
|
+
}
|
|
550
|
+
catch (error) {
|
|
551
|
+
this.#fault(current, asError(error));
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
child.stderr.on("data", (data) => {
|
|
555
|
+
current.stderr = (current.stderr + data.toString()).slice(-64 * 1024);
|
|
556
|
+
});
|
|
557
|
+
for (const source of [child, child.stdin, child.stdout, child.stderr, current.reader])
|
|
558
|
+
source.on("error", (error) => this.#fault(current, error));
|
|
559
|
+
// EOF can occur with a live child; it is independently a transport failure and requires owned cleanup.
|
|
560
|
+
child.stdout.once("end", () => this.#fault(current, codexTransportError("CODEX_TRANSPORT_FAILED")));
|
|
561
|
+
child.once("close", () => {
|
|
562
|
+
current.exited = true;
|
|
563
|
+
this.#fault(current, codexTransportError("CODEX_TRANSPORT_FAILED"));
|
|
564
|
+
});
|
|
565
|
+
try {
|
|
566
|
+
await this.#rpc("initialize", {
|
|
567
|
+
clientInfo: { name: "@mininglamp-oss/octo-runtime-adapter-codex", version: "0.1.0" },
|
|
568
|
+
capabilities: {
|
|
569
|
+
experimentalApi: true,
|
|
570
|
+
requestAttestation: false,
|
|
571
|
+
optOutNotificationMethods: [
|
|
572
|
+
"mcpServer/startupStatus/updated",
|
|
573
|
+
"remoteControl/status/changed"
|
|
574
|
+
]
|
|
575
|
+
}
|
|
576
|
+
}, timeoutMs);
|
|
577
|
+
if (current.failed || this.#closing)
|
|
578
|
+
throw codexTransportError("CODEX_SERVICE_CLOSED");
|
|
579
|
+
await new Promise((resolve, reject) => current.writer.write({ method: "initialized" }, reject, resolve));
|
|
580
|
+
}
|
|
581
|
+
catch (error) {
|
|
582
|
+
this.#fault(current, asError(error));
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
beginShutdown() {
|
|
587
|
+
if (this.#closing)
|
|
588
|
+
return;
|
|
589
|
+
this.#closing = true;
|
|
590
|
+
this.#state = "closing";
|
|
591
|
+
this.#stop.abort();
|
|
592
|
+
clearTimeout(this.#stableTimer);
|
|
593
|
+
if (this.#current)
|
|
594
|
+
this.#fault(this.#current, codexTransportError("CODEX_SERVICE_CLOSED"));
|
|
595
|
+
}
|
|
596
|
+
close() {
|
|
597
|
+
return (this.#close ??= (async () => {
|
|
598
|
+
this.beginShutdown();
|
|
599
|
+
const failures = [];
|
|
600
|
+
try {
|
|
601
|
+
await this.#current?.cleanup();
|
|
602
|
+
await this.#supervision?.catch(() => { });
|
|
603
|
+
await this.#current?.cleanup();
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
failures.push(error);
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
await this.spec.cleanup?.();
|
|
610
|
+
}
|
|
611
|
+
catch (error) {
|
|
612
|
+
failures.push(error);
|
|
613
|
+
}
|
|
614
|
+
this.#state = failures.length ? "cleanup_incomplete" : "closed";
|
|
615
|
+
if (failures.length === 1)
|
|
616
|
+
throw failures[0];
|
|
617
|
+
if (failures.length > 1)
|
|
618
|
+
throw new AggregateError(failures, "CODEX_SERVICE_CLEANUP_FAILED");
|
|
619
|
+
})());
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
const groups = new WeakSet();
|
|
623
|
+
const ownedChildren = new WeakSet();
|
|
624
|
+
function ownedSpawn(command, args, options) {
|
|
625
|
+
const launch = resolveProcessLaunch(command, args, options);
|
|
626
|
+
const child = spawn(launch.command, [...launch.args], {
|
|
627
|
+
...options,
|
|
628
|
+
stdio: "pipe",
|
|
629
|
+
detached: process.platform !== "win32",
|
|
630
|
+
windowsHide: true
|
|
631
|
+
});
|
|
632
|
+
ownedChildren.add(child);
|
|
633
|
+
if (process.platform !== "win32")
|
|
634
|
+
groups.add(child);
|
|
635
|
+
return child;
|
|
636
|
+
}
|
|
637
|
+
function record(value) {
|
|
638
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
639
|
+
? value
|
|
640
|
+
: {};
|
|
641
|
+
}
|
|
642
|
+
function rpcId(value) {
|
|
643
|
+
return typeof value === "string" || typeof value === "number" ? value : undefined;
|
|
644
|
+
}
|
|
645
|
+
function asError(value) {
|
|
646
|
+
return value instanceof Error ? value : new Error(String(value));
|
|
647
|
+
}
|
|
648
|
+
function text(value) {
|
|
649
|
+
return typeof value === "string" ? value : "";
|
|
650
|
+
}
|
|
@@ -22,9 +22,12 @@ export declare class CodexStdinWriter {
|
|
|
22
22
|
write(message: Record<string, unknown>, onError: (error: Error) => void, onWritten?: () => void, options?: {
|
|
23
23
|
control?: boolean;
|
|
24
24
|
timeoutMs?: number;
|
|
25
|
+
owner?: object;
|
|
25
26
|
}): () => void;
|
|
26
27
|
/** 终态先撤销未送达决定,再 best-effort 中断;不能等 drain 才处理取消。 */
|
|
27
28
|
cancelPending(error: Error): void;
|
|
29
|
+
/** 共享服务只撤销原调用的未写入条目;已写出的字节不重放,也不关闭其他 owner 的准入。 */
|
|
30
|
+
cancelOwner(owner: object, error: Error): void;
|
|
28
31
|
/** 所有者关闭时只结算资源;不额外制造新的执行失败。 */
|
|
29
32
|
stop(error: Error): void;
|
|
30
33
|
}
|
|
@@ -76,6 +76,7 @@ export class CodexStdinWriter {
|
|
|
76
76
|
if (!this.#active && !this.#normal.length && !this.#control.length)
|
|
77
77
|
this.#lastProgress = performance.now();
|
|
78
78
|
const entry = {
|
|
79
|
+
owner: options.owner,
|
|
79
80
|
line,
|
|
80
81
|
bytes,
|
|
81
82
|
control,
|
|
@@ -111,6 +112,20 @@ export class CodexStdinWriter {
|
|
|
111
112
|
this.#reject(entry, error);
|
|
112
113
|
this.#arm();
|
|
113
114
|
}
|
|
115
|
+
/** 共享服务只撤销原调用的未写入条目;已写出的字节不重放,也不关闭其他 owner 的准入。 */
|
|
116
|
+
cancelOwner(owner, error) {
|
|
117
|
+
for (const queue of [this.#normal, this.#control]) {
|
|
118
|
+
for (let i = queue.length - 1; i >= 0; i--) {
|
|
119
|
+
if (queue[i].owner !== owner)
|
|
120
|
+
continue;
|
|
121
|
+
const [entry] = queue.splice(i, 1);
|
|
122
|
+
this.#reject(entry, error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (this.#active?.owner === owner)
|
|
126
|
+
this.#reject(this.#active, error);
|
|
127
|
+
this.#arm();
|
|
128
|
+
}
|
|
114
129
|
/** 所有者关闭时只结算资源;不额外制造新的执行失败。 */
|
|
115
130
|
stop(error) {
|
|
116
131
|
if (this.#failure)
|