@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.
Files changed (170) hide show
  1. package/README.md +1 -1
  2. package/apps/cli/README.md +1 -1
  3. package/apps/cli/dist/adapters.js +40 -14
  4. package/apps/cli/dist/build-info.json +2 -2
  5. package/apps/cli/dist/command-tree.js +2 -3
  6. package/apps/cli/dist/extras.js +11 -1
  7. package/apps/cli/dist/index.js +14 -1
  8. package/apps/cli/dist/main.js +58 -23
  9. package/apps/cli/dist/module-manifest.js +21 -3
  10. package/apps/cli/dist/output.d.ts +19 -1
  11. package/apps/cli/dist/output.js +179 -27
  12. package/apps/cli/dist/playboard.js +2 -2
  13. package/apps/cli/dist/state.d.ts +5 -2
  14. package/apps/cli/dist/state.js +48 -13
  15. package/dist/public/codex.d.ts +1 -0
  16. package/dist/public/codex.js +1 -0
  17. package/dist/public/executor-kernel.d.ts +4 -0
  18. package/dist/public/executor-kernel.js +2 -0
  19. package/dist/public/extra.d.ts +1 -1
  20. package/dist/public/host.d.ts +5 -0
  21. package/dist/public/host.js +2 -0
  22. package/node_modules/@mininglamp-oss/octo-runtime-acp-client/package.json +3 -3
  23. package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/dist/index.js +20 -0
  24. package/node_modules/@mininglamp-oss/octo-runtime-adapter-acp/package.json +4 -4
  25. package/node_modules/@mininglamp-oss/octo-runtime-adapter-antigravity/package.json +3 -3
  26. package/node_modules/@mininglamp-oss/octo-runtime-adapter-claude-code/package.json +3 -3
  27. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codebuddy/package.json +4 -4
  28. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.d.ts +6 -6
  29. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/app-server.js +68 -253
  30. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/index.d.ts +11 -6
  31. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/index.js +198 -52
  32. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/service.d.ts +58 -0
  33. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/service.js +650 -0
  34. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.d.ts +3 -0
  35. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/dist/stdin-writer.js +15 -0
  36. package/node_modules/@mininglamp-oss/octo-runtime-adapter-codex/package.json +3 -3
  37. package/node_modules/@mininglamp-oss/octo-runtime-adapter-dsh/package.json +3 -3
  38. package/node_modules/@mininglamp-oss/octo-runtime-adapter-grok-build/package.json +5 -5
  39. package/node_modules/@mininglamp-oss/octo-runtime-adapter-hermes/package.json +2 -2
  40. package/node_modules/@mininglamp-oss/octo-runtime-adapter-kimi-code/package.json +2 -2
  41. package/node_modules/@mininglamp-oss/octo-runtime-adapter-mcode/package.json +2 -2
  42. package/node_modules/@mininglamp-oss/octo-runtime-adapter-openclaw/package.json +3 -3
  43. package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/dist/index.d.ts +2 -0
  44. package/node_modules/@mininglamp-oss/octo-runtime-adapter-opencode/package.json +4 -4
  45. package/node_modules/@mininglamp-oss/octo-runtime-adapter-qodercli/package.json +2 -2
  46. package/node_modules/@mininglamp-oss/octo-runtime-adapter-qoderclicn/package.json +2 -2
  47. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/activity.d.ts +5 -0
  48. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/execution-scope.d.ts +25 -0
  49. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/execution-scope.js +1 -0
  50. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/dist/index.d.ts +3 -0
  51. package/node_modules/@mininglamp-oss/octo-runtime-adapter-sdk/package.json +2 -2
  52. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/adapters.js +40 -14
  53. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/build-info.json +2 -2
  54. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/command-tree.js +2 -3
  55. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/extras.js +11 -1
  56. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/index.js +14 -1
  57. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/main.js +58 -23
  58. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/module-manifest.js +21 -3
  59. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.d.ts +19 -1
  60. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/output.js +179 -27
  61. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/playboard.js +2 -2
  62. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/state.d.ts +5 -2
  63. package/node_modules/@mininglamp-oss/octo-runtime-cli/dist/state.js +48 -13
  64. package/node_modules/@mininglamp-oss/octo-runtime-cli/package.json +23 -23
  65. package/node_modules/@mininglamp-oss/octo-runtime-client/build-info.json +5 -0
  66. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/async-question-reply.d.ts +30 -0
  67. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/async-question-reply.js +199 -0
  68. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/browser.d.ts +1 -0
  69. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/browser.js +1 -0
  70. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/client.d.ts +10 -0
  71. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/client.js +465 -0
  72. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/index.d.ts +6 -0
  73. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/index.js +6 -0
  74. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/internal.d.ts +4 -0
  75. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/internal.js +5 -0
  76. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/invocation-lifecycle.d.ts +59 -0
  77. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/invocation-lifecycle.js +287 -0
  78. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/notification.d.ts +1 -0
  79. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/notification.js +1 -0
  80. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/protocol.d.ts +1 -0
  81. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/protocol.js +1 -0
  82. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/runtime-path.d.ts +21 -0
  83. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/runtime-path.js +138 -0
  84. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-catalog.d.ts +8 -0
  85. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-catalog.js +65 -0
  86. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-diagnostics.d.ts +82 -0
  87. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/startup-diagnostics.js +371 -0
  88. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/submission-error.d.ts +10 -0
  89. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/submission-error.js +23 -0
  90. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/types.d.ts +72 -0
  91. package/node_modules/@mininglamp-oss/octo-runtime-client/dist/types.js +10 -0
  92. package/node_modules/@mininglamp-oss/octo-runtime-client/package.json +54 -0
  93. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/context-configuration-fixtures.json +1219 -0
  94. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/context-fixtures.json +5988 -0
  95. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-client.behavior.json +56 -0
  96. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-ipc-fixtures.json +53 -0
  97. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/kernel-ipc.schema.json +538 -0
  98. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/native-invoke-fixtures.json +116 -0
  99. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/protocol.schema.json +4525 -0
  100. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/run-protocol-fixtures.json +761 -0
  101. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/runtime-selection-fixtures.json +663 -0
  102. package/node_modules/@mininglamp-oss/octo-runtime-client/schema/session-name-fixtures.json +191 -0
  103. package/node_modules/@mininglamp-oss/octo-runtime-cross-service/package.json +1 -1
  104. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.d.ts +26 -18
  105. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/dist/index.js +151 -11
  106. package/node_modules/@mininglamp-oss/octo-runtime-executor-core/package.json +3 -3
  107. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.d.ts +1 -30
  108. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/async-question-reply.js +2 -199
  109. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.d.ts +1 -10
  110. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/client.js +2 -446
  111. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/endpoint.js +2 -0
  112. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.d.ts +2 -0
  113. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/host-run-service.js +13 -2
  114. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.d.ts +4 -1
  115. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/index.js +4 -1
  116. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-lifecycle.d.ts +1 -53
  117. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/invocation-lifecycle.js +2 -280
  118. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/lifecycle.d.ts +5 -10
  119. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/lifecycle.js +4 -1
  120. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-home.d.ts +20 -0
  121. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/runtime-home.js +180 -0
  122. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/server.js +31 -8
  123. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.d.ts +5 -2
  124. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/service.js +48 -5
  125. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-catalog.d.ts +1 -0
  126. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-catalog.js +1 -0
  127. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-diagnostics.d.ts +1 -0
  128. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-diagnostics.js +1 -0
  129. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-logging.d.ts +4 -0
  130. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/startup-logging.js +18 -0
  131. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/submission-error.d.ts +1 -10
  132. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/submission-error.js +2 -23
  133. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/types.d.ts +2 -72
  134. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/dist/types.js +2 -10
  135. package/node_modules/@mininglamp-oss/octo-runtime-executor-kernel/package.json +5 -4
  136. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/extra.js +1 -1
  137. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/dist/ports.d.ts +2 -2
  138. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-gateway/package.json +6 -6
  139. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/dist/extra.js +1 -1
  140. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/dist/ports.d.ts +2 -2
  141. package/node_modules/@mininglamp-oss/octo-runtime-extra-channel-wecom/package.json +5 -5
  142. package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/dist/index.js +4 -4
  143. package/node_modules/@mininglamp-oss/octo-runtime-extra-webui/package.json +2 -2
  144. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/endpoint.d.ts +1 -1
  145. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/endpoint.js +12 -5
  146. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/errors.d.ts +5 -1
  147. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/errors.js +9 -0
  148. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/execution.d.ts +44 -0
  149. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/execution.js +207 -0
  150. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-config.js +23 -3
  151. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-service.d.ts +27 -4
  152. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extra-service.js +133 -25
  153. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extras.d.ts +10 -3
  154. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/extras.js +18 -4
  155. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/home.d.ts +5 -6
  156. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/home.js +33 -45
  157. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/identity.js +6 -0
  158. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/index.d.ts +4 -0
  159. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/dist/index.js +3 -0
  160. package/node_modules/@mininglamp-oss/octo-runtime-host-sdk/package.json +5 -4
  161. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/adapter-metadata.d.ts +7 -0
  162. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/adapter-metadata.js +1 -0
  163. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/generated/protocol-types.d.ts +19 -0
  164. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.d.ts +3 -1
  165. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/index.js +1 -0
  166. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/service-processes.d.ts +5 -0
  167. package/node_modules/@mininglamp-oss/octo-runtime-protocol/dist/service-processes.js +65 -0
  168. package/node_modules/@mininglamp-oss/octo-runtime-protocol/package.json +1 -1
  169. package/package.json +33 -27
  170. package/packages/octo-runtime-protocol/schema/protocol.schema.json +42 -0
@@ -1,17 +1,20 @@
1
- import { invocationEnvironment, mergeProcessEnvironment, processEnvironmentValue } from "@mininglamp-oss/octo-runtime-adapter-sdk";
1
+ import { invocationEnvironment, invocationMcpServers, mergeProcessEnvironment, processEnvironmentValue } from "@mininglamp-oss/octo-runtime-adapter-sdk";
2
2
  import { codexMcpConfig } from "./mcp.js";
3
3
  /**
4
4
  * Assembly entry point for the Codex adapter.
5
5
  *
6
- * This file only handles "how to prepare one invocation": selecting the real
7
- * CODEX_HOME, declaring adapter metadata, and delegating execution to the
8
- * app-server transport. Per-run developer instructions travel in native thread
9
- * params, so app-server sees the complete state database and plugin runtime.
6
+ * 描述对象注册时创建独立 scope;按兼容配置选择常驻服务,再绑定本轮 channel。
7
+ * scope 负责最终关闭,逐轮 developer instructions 仍放在原生 thread 参数中。
10
8
  */
11
9
  import { defineAdapter, probeCommand } from "@mininglamp-oss/octo-runtime-adapter-sdk";
10
+ import { hasImageInput, resolveStreamLimitPolicy } from "@mininglamp-oss/octo-runtime-protocol";
12
11
  import { homedir } from "node:os";
13
12
  import { join } from "node:path";
14
13
  import { buildCodexAppServerArgs, runCodexAppServerTurn } from "./app-server.js";
14
+ import { CodexService } from "./service.js";
15
+ import { createHmac, randomBytes, randomUUID } from "node:crypto";
16
+ import { realpathSync, statSync } from "node:fs";
17
+ import { delimiter, isAbsolute, resolve } from "node:path";
15
18
  export { buildCodexAppServerArgs, CodexEventNormalizer, CodexAppServerProcessError, CodexAppServerRpcError, normalizeCodexEvent, runCodexAppServerTurn } from "./app-server.js";
16
19
  /**
17
20
  * Convert an Octo Runtime InvokeRequest into a launchable app-server process spec.
@@ -59,8 +62,6 @@ function nativeCapacityOverrides(request) {
59
62
  ];
60
63
  }
61
64
  export function createCodexAdapter({ command = "codex", appServerArgs = buildCodexAppServerArgs(), spawnFn, env = process.env } = {}) {
62
- const interactionResponders = new Map();
63
- const nativeResponders = new Map();
64
65
  return defineAdapter({
65
66
  metadata: {
66
67
  mcp: true,
@@ -82,60 +83,205 @@ export function createCodexAdapter({ command = "codex", appServerArgs = buildCod
82
83
  },
83
84
  type: "codex",
84
85
  agentSources: ["provisioned"],
85
- processModel: "subprocess-per-invocation",
86
+ processModel: "service-owned-subprocess",
86
87
  transport: "json-rpc-2.0/stdio"
87
88
  },
88
89
  probe: () => probeCommand(command),
89
- async respondNative(invocationId, response) {
90
- const responder = nativeResponders.get(invocationId);
91
- if (!responder)
92
- throw new Error("INTERACTION_NOT_ACTIVE");
93
- await responder(response);
94
- },
95
- async respondInteraction(invocationId, response) {
96
- const responder = interactionResponders.get(invocationId);
97
- if (!responder)
98
- throw new Error(`no active Codex interaction invocation: ${invocationId}`);
99
- await responder(response);
100
- },
101
- async *invoke(request, context) {
102
- if (interactionResponders.has(request.invocationId)) {
103
- throw new Error(`duplicate active Codex invocation: ${request.invocationId}`);
104
- }
105
- // The transport owns subprocess cleanup and observation ordering.
106
- const spec = buildCodexCommand(request, command, appServerArgs, env);
107
- let activeResponder;
108
- const onInteractionResponder = (responder) => {
109
- if (responder) {
110
- activeResponder = responder;
111
- interactionResponders.set(request.invocationId, responder);
112
- }
113
- else if (!activeResponder ||
114
- interactionResponders.get(request.invocationId) === activeResponder) {
115
- interactionResponders.delete(request.invocationId);
116
- activeResponder = undefined;
90
+ createExecutionScope: ({ log }) => {
91
+ const scopeId = randomUUID();
92
+ const salt = randomBytes(32);
93
+ const services = new Map();
94
+ const interactionResponders = new Map();
95
+ const nativeResponders = new Map();
96
+ const active = new Set();
97
+ let closing = false;
98
+ let close;
99
+ const unresolved = new Set();
100
+ const failed = new Set();
101
+ const scope = {
102
+ metadata: {
103
+ type: "codex",
104
+ agentSources: ["provisioned"],
105
+ mcp: true,
106
+ protocolSupport: {
107
+ protocol: "codex.app-server",
108
+ modes: ["native", "canonical"],
109
+ nativeMethods: []
110
+ }
111
+ },
112
+ async *invoke(request, context) {
113
+ if (closing)
114
+ throw new Error("CODEX_SERVICE_CLOSED");
115
+ if (active.has(request.invocationId))
116
+ throw new Error("CODEX_INVOCATION_BUSY");
117
+ active.add(request.invocationId);
118
+ try {
119
+ const runEnv = invocationEnvironment(request, {}, env);
120
+ const rawMessageMaxBytes = resolveStreamLimitPolicy(context?.streamLimits).rawMessageMaxBytes;
121
+ // 比较解析后的语义,而非每轮随机 MCP header 环境名;私有键从不进入诊断。
122
+ const semantic = stableJson({
123
+ command: executableIdentity(command, runEnv, request.workdir),
124
+ args: [...appServerArgs, ...nativeCapacityOverrides(request)],
125
+ cwd: request.workdir,
126
+ env: runEnv,
127
+ home: processEnvironmentValue(runEnv, "CODEX_HOME") || join(homedir(), ".codex"),
128
+ mcp: invocationMcpServers(request, runEnv),
129
+ revision: request.agentConfig.providerOptions?.codexServiceRevision ?? "",
130
+ rawMessageMaxBytes
131
+ });
132
+ const key = createHmac("sha256", salt).update(semantic).digest("hex");
133
+ let entry = services.get(key);
134
+ if (entry && entry.semantic !== semantic)
135
+ throw new Error("CODEX_SERVICE_KEY_COLLISION");
136
+ if (!entry) {
137
+ if (services.size >= 8)
138
+ throw new Error("CODEX_SERVICE_CONFIGURATIONS_EXCEEDED");
139
+ entry = {
140
+ semantic,
141
+ service: new CodexService(scopeId, buildCodexCommand(request, command, appServerArgs, env), spawnFn, rawMessageMaxBytes)
142
+ };
143
+ services.set(key, entry);
144
+ }
145
+ const channel = await entry.service.acquire(request.signal, Buffer.byteLength(JSON.stringify(request)), context?.protocolMode !== "native" && hasImageInput(request));
146
+ try {
147
+ yield* runCodexAppServerTurn({
148
+ request,
149
+ spec: entry.service.spec,
150
+ channel,
151
+ context,
152
+ onInteractionResponder: (responder) => {
153
+ if (responder)
154
+ interactionResponders.set(request.invocationId, responder);
155
+ else
156
+ interactionResponders.delete(request.invocationId);
157
+ },
158
+ onNativeResponder: (responder) => {
159
+ if (responder)
160
+ nativeResponders.set(request.invocationId, responder);
161
+ else
162
+ nativeResponders.delete(request.invocationId);
163
+ }
164
+ });
165
+ }
166
+ finally {
167
+ await channel.close();
168
+ }
169
+ }
170
+ finally {
171
+ active.delete(request.invocationId);
172
+ interactionResponders.delete(request.invocationId);
173
+ nativeResponders.delete(request.invocationId);
174
+ }
175
+ },
176
+ async respondNative(invocationId, response) {
177
+ const responder = nativeResponders.get(invocationId);
178
+ if (!responder)
179
+ throw new Error("INTERACTION_NOT_ACTIVE");
180
+ await responder(response);
181
+ },
182
+ async respondInteraction(invocationId, response) {
183
+ const responder = interactionResponders.get(invocationId);
184
+ if (!responder)
185
+ throw new Error("INTERACTION_NOT_ACTIVE");
186
+ await responder(response);
187
+ },
188
+ processes: () => [...services.values()].map(({ service }) => service.snapshot()),
189
+ beginShutdown() {
190
+ closing = true;
191
+ for (const { service } of services.values())
192
+ service.beginShutdown();
193
+ },
194
+ close({ deadline }) {
195
+ scope.beginShutdown();
196
+ if (!close) {
197
+ for (const { service } of services.values())
198
+ unresolved.add(service.id);
199
+ close = Promise.all([...services.values()].map(async ({ service }) => {
200
+ try {
201
+ await service.close();
202
+ unresolved.delete(service.id);
203
+ }
204
+ catch {
205
+ failed.add(service.id);
206
+ log("warn", "codex.service.cleanup_incomplete", { service_id: service.id });
207
+ }
208
+ })).then(() => { });
209
+ }
210
+ return (async () => {
211
+ let timer;
212
+ try {
213
+ await Promise.race([
214
+ close,
215
+ new Promise((resolve) => {
216
+ timer = setTimeout(resolve, Math.max(0, deadline - performance.now()));
217
+ })
218
+ ]);
219
+ }
220
+ finally {
221
+ clearTimeout(timer);
222
+ }
223
+ return {
224
+ complete: unresolved.size === 0,
225
+ unresolvedServiceIds: [...unresolved],
226
+ failedCleanupServiceIds: [...failed]
227
+ };
228
+ })();
117
229
  }
118
230
  };
231
+ const defined = defineAdapter(scope);
232
+ return { ...scope, invoke: defined.invoke };
233
+ },
234
+ respondNative(invocationId, response) {
235
+ void invocationId;
236
+ void response;
237
+ return Promise.reject(new Error("CODEX_SERVICE_SCOPE_REQUIRED"));
238
+ },
239
+ respondInteraction(invocationId, response) {
240
+ void invocationId;
241
+ void response;
242
+ return Promise.reject(new Error("CODEX_SERVICE_SCOPE_REQUIRED"));
243
+ },
244
+ invoke(request, context) {
245
+ void request;
246
+ void context;
247
+ throw new Error("CODEX_SERVICE_SCOPE_REQUIRED");
248
+ }
249
+ });
250
+ }
251
+ function stableJson(value) {
252
+ if (Array.isArray(value))
253
+ return `[${value.map(stableJson).join(",")}]`;
254
+ if (value && typeof value === "object")
255
+ return `{${Object.entries(value)
256
+ .filter(([, entry]) => entry !== undefined)
257
+ .sort(([a], [b]) => a.localeCompare(b))
258
+ .map(([key, entry]) => `${JSON.stringify(key)}:${stableJson(entry)}`)
259
+ .join(",")}}`;
260
+ return JSON.stringify(value);
261
+ }
262
+ /** 每轮校验入口文件身份;安装文件变化会换服务,不额外启动版本探针。 */
263
+ function executableIdentity(command, env, cwd) {
264
+ const extensions = process.platform === "win32" && !/\.[a-z]+$/i.test(command)
265
+ ? (processEnvironmentValue(env, "PATHEXT") ?? ".EXE;.CMD;.BAT").split(";")
266
+ : [""];
267
+ // 带路径的命令只相对 cwd 解析一次;裸命令仍按 PATH 搜索。
268
+ const directories = isAbsolute(command) || /[/\\]/.test(command)
269
+ ? [""]
270
+ : (processEnvironmentValue(env, "PATH") ?? "").split(delimiter);
271
+ for (const directory of directories)
272
+ for (const extension of extensions) {
119
273
  try {
120
- yield* runCodexAppServerTurn({
121
- request,
122
- spec,
123
- context,
124
- onInteractionResponder,
125
- onNativeResponder: (responder) => {
126
- if (responder)
127
- nativeResponders.set(request.invocationId, responder);
128
- else
129
- nativeResponders.delete(request.invocationId);
130
- },
131
- ...(spawnFn ? { spawnFn } : {})
132
- });
274
+ const path = realpathSync(resolve(cwd, directory, command + extension));
275
+ const info = statSync(path);
276
+ if (info.isFile())
277
+ return { path, dev: info.dev, ino: info.ino, size: info.size, mtime: info.mtimeMs };
133
278
  }
134
- finally {
135
- onInteractionResponder(undefined);
279
+ catch (error) {
280
+ if (!["ENOENT", "ENOTDIR"].includes(error.code ?? ""))
281
+ throw error;
136
282
  }
137
283
  }
138
- });
284
+ return { command };
139
285
  }
140
286
  export const codexAdapter = createCodexAdapter();
141
287
  export default codexAdapter;
@@ -0,0 +1,58 @@
1
+ import { type ServiceProcessSnapshot } from "@mininglamp-oss/octo-runtime-adapter-sdk";
2
+ import type { CodexAppServerCommand, CodexAppServerSpawn } from "./app-server.js";
3
+ type Message = Record<string, unknown>;
4
+ type Id = string | number;
5
+ /**
6
+ * 一个调用的协议所有权;只有 service 持有进程和 stdio。
7
+ * channel 关闭只退订自己的 thread,不能终止共享进程或撤销其他调用的写入。
8
+ */
9
+ export declare class CodexTurnChannel {
10
+ #private;
11
+ readonly service: CodexService;
12
+ readonly incarnation: number;
13
+ readonly allowInputEcho: boolean;
14
+ onMessage: (message: Message) => void;
15
+ onFailure: (error: Error) => void;
16
+ threadId?: string;
17
+ turnId?: string;
18
+ terminal: boolean;
19
+ closed: boolean;
20
+ startingTurn: boolean;
21
+ startingThread: boolean;
22
+ readonly buffered: Message[];
23
+ bufferedBytes: number;
24
+ constructor(service: CodexService, incarnation: number, allowInputEcho?: boolean);
25
+ reserveRequestId(): Id;
26
+ write(message: Message, onError: (error: Error) => void, onWritten?: () => void, options?: {
27
+ control?: boolean;
28
+ timeoutMs?: number;
29
+ }): () => void;
30
+ cancelPending(error: Error): void;
31
+ close(): Promise<void>;
32
+ errorMessage(error: unknown): string;
33
+ }
34
+ /** Core 作用域内的一种兼容配置;惰性首次启动,之后空闲退出也主动恢复,业务动作从不重放。 */
35
+ export declare class CodexService {
36
+ #private;
37
+ readonly scopeId: string;
38
+ readonly spec: CodexAppServerCommand;
39
+ readonly spawnFn: CodexAppServerSpawn;
40
+ readonly rawMessageMaxBytes: number;
41
+ readonly id: `${string}-${string}-${string}-${string}-${string}`;
42
+ constructor(scopeId: string, spec: CodexAppServerCommand, spawnFn?: CodexAppServerSpawn, rawMessageMaxBytes?: number);
43
+ /** 取消等待不取消共享启动;等待者数和活动 thread 数分别有界。 */
44
+ acquire(signal?: AbortSignal, retainedBytes?: number, allowInputEcho?: boolean): Promise<CodexTurnChannel>;
45
+ snapshot(): ServiceProcessSnapshot;
46
+ stderr(generation: number): string;
47
+ reserveRequestId(): Id;
48
+ write(channel: CodexTurnChannel, message: Message, onError: (error: Error) => void, onWritten?: () => void, options?: {
49
+ control?: boolean;
50
+ timeoutMs?: number;
51
+ }): () => void;
52
+ cancelPending(channel: CodexTurnChannel, error: Error): void;
53
+ /** 未确认中断/退订时保留 thread 和容量,阻止迟到通知进入后续调用。 */
54
+ release(channel: CodexTurnChannel): Promise<void>;
55
+ beginShutdown(): void;
56
+ close(): Promise<void>;
57
+ }
58
+ export {};