cordisx 0.1.0-beta.33 → 0.1.0-beta.35

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 (192) hide show
  1. package/README.md +7 -0
  2. package/dist/assets/feedback/feedback-manifest.schema.json +1 -1
  3. package/dist/assets/launcher/native-app-server-intermediary.mjs +32 -599
  4. package/dist/assets/launcher/native-app-server-protocol.mjs +724 -0
  5. package/dist/assets/launcher/native-cli-invocation.mjs +56 -0
  6. package/dist/assets/launcher/native-turn-diagnostics.mjs +125 -0
  7. package/dist/native/native-helper-artifact.json +1 -1
  8. package/dist/skills/cordisx/SKILL.md +5 -1
  9. package/dist/skills/cordisx/version.json +1 -1
  10. package/dist/src/cli/help.d.ts +1 -1
  11. package/dist/src/cli/help.d.ts.map +1 -1
  12. package/dist/src/cli/help.js +2 -0
  13. package/dist/src/cli/help.js.map +1 -1
  14. package/dist/src/cli/host-lifecycle.d.ts +2 -1
  15. package/dist/src/cli/host-lifecycle.d.ts.map +1 -1
  16. package/dist/src/cli/host-lifecycle.js +6 -3
  17. package/dist/src/cli/host-lifecycle.js.map +1 -1
  18. package/dist/src/cli/launch-diagnostics.d.ts +6 -0
  19. package/dist/src/cli/launch-diagnostics.d.ts.map +1 -0
  20. package/dist/src/cli/launch-diagnostics.js +39 -0
  21. package/dist/src/cli/launch-diagnostics.js.map +1 -0
  22. package/dist/src/cli/model-export-choice.d.ts +11 -0
  23. package/dist/src/cli/model-export-choice.d.ts.map +1 -0
  24. package/dist/src/cli/model-export-choice.js +73 -0
  25. package/dist/src/cli/model-export-choice.js.map +1 -0
  26. package/dist/src/cli/model-export-command.d.ts +6 -0
  27. package/dist/src/cli/model-export-command.d.ts.map +1 -0
  28. package/dist/src/cli/model-export-command.js +136 -0
  29. package/dist/src/cli/model-export-command.js.map +1 -0
  30. package/dist/src/cli/model-export-parse.d.ts +18 -0
  31. package/dist/src/cli/model-export-parse.d.ts.map +1 -0
  32. package/dist/src/cli/model-export-parse.js +112 -0
  33. package/dist/src/cli/model-export-parse.js.map +1 -0
  34. package/dist/src/cli/parse.d.ts +3 -2
  35. package/dist/src/cli/parse.d.ts.map +1 -1
  36. package/dist/src/cli/parse.js +3 -0
  37. package/dist/src/cli/parse.js.map +1 -1
  38. package/dist/src/cli/production-host-bootstrap.d.ts.map +1 -1
  39. package/dist/src/cli/production-host-bootstrap.js +2 -0
  40. package/dist/src/cli/production-host-bootstrap.js.map +1 -1
  41. package/dist/src/cli/production-plugin-management.d.ts +6 -0
  42. package/dist/src/cli/production-plugin-management.d.ts.map +1 -1
  43. package/dist/src/cli/production-plugin-management.js +11 -0
  44. package/dist/src/cli/production-plugin-management.js.map +1 -1
  45. package/dist/src/cli/run-command-dispatch.d.ts.map +1 -1
  46. package/dist/src/cli/run-command-dispatch.js +5 -0
  47. package/dist/src/cli/run-command-dispatch.js.map +1 -1
  48. package/dist/src/cli/run-command.d.ts.map +1 -1
  49. package/dist/src/cli/run-command.js +16 -17
  50. package/dist/src/cli/run-command.js.map +1 -1
  51. package/dist/src/cli/run-injected-host.d.ts.map +1 -1
  52. package/dist/src/cli/run-injected-host.js +4 -1
  53. package/dist/src/cli/run-injected-host.js.map +1 -1
  54. package/dist/src/cli/run-runtime.d.ts +2 -0
  55. package/dist/src/cli/run-runtime.d.ts.map +1 -1
  56. package/dist/src/cli/run-runtime.js.map +1 -1
  57. package/dist/src/cli/startup-budget.test.d.ts +2 -0
  58. package/dist/src/cli/startup-budget.test.d.ts.map +1 -0
  59. package/dist/src/cli/startup-budget.test.js +94 -0
  60. package/dist/src/cli/startup-budget.test.js.map +1 -0
  61. package/dist/src/cli/supervisor-command.d.ts +5 -2
  62. package/dist/src/cli/supervisor-command.d.ts.map +1 -1
  63. package/dist/src/cli/supervisor-command.js +129 -28
  64. package/dist/src/cli/supervisor-command.js.map +1 -1
  65. package/dist/src/cli/supervisor-runtime.d.ts.map +1 -1
  66. package/dist/src/cli/supervisor-runtime.js +11 -3
  67. package/dist/src/cli/supervisor-runtime.js.map +1 -1
  68. package/dist/src/cli.js +5 -2
  69. package/dist/src/cli.js.map +1 -1
  70. package/dist/src/diagnostics/cdp-observation.test.d.ts +2 -0
  71. package/dist/src/diagnostics/cdp-observation.test.d.ts.map +1 -0
  72. package/dist/src/diagnostics/cdp-observation.test.js +86 -0
  73. package/dist/src/diagnostics/cdp-observation.test.js.map +1 -0
  74. package/dist/src/diagnostics/context.d.ts +22 -0
  75. package/dist/src/diagnostics/context.d.ts.map +1 -0
  76. package/dist/src/diagnostics/context.js +103 -0
  77. package/dist/src/diagnostics/context.js.map +1 -0
  78. package/dist/src/diagnostics/journal.d.ts +53 -0
  79. package/dist/src/diagnostics/journal.d.ts.map +1 -0
  80. package/dist/src/diagnostics/journal.js +351 -0
  81. package/dist/src/diagnostics/journal.js.map +1 -0
  82. package/dist/src/diagnostics/journal.test.d.ts +2 -0
  83. package/dist/src/diagnostics/journal.test.d.ts.map +1 -0
  84. package/dist/src/diagnostics/journal.test.js +117 -0
  85. package/dist/src/diagnostics/journal.test.js.map +1 -0
  86. package/dist/src/feedback/archive.js +1 -1
  87. package/dist/src/feedback/archive.js.map +1 -1
  88. package/dist/src/feedback/collector.d.ts.map +1 -1
  89. package/dist/src/feedback/collector.js +39 -10
  90. package/dist/src/feedback/collector.js.map +1 -1
  91. package/dist/src/launcher/cdp-session.d.ts +3 -0
  92. package/dist/src/launcher/cdp-session.d.ts.map +1 -1
  93. package/dist/src/launcher/cdp-session.js +34 -2
  94. package/dist/src/launcher/cdp-session.js.map +1 -1
  95. package/dist/src/launcher/cdp.d.ts.map +1 -1
  96. package/dist/src/launcher/cdp.js +47 -0
  97. package/dist/src/launcher/cdp.js.map +1 -1
  98. package/dist/src/launcher/managed-service-candidate.d.ts +12 -0
  99. package/dist/src/launcher/managed-service-candidate.d.ts.map +1 -0
  100. package/dist/src/launcher/managed-service-candidate.js +14 -0
  101. package/dist/src/launcher/managed-service-candidate.js.map +1 -0
  102. package/dist/src/launcher/managed-service-diagnostics.d.ts +7 -0
  103. package/dist/src/launcher/managed-service-diagnostics.d.ts.map +1 -0
  104. package/dist/src/launcher/managed-service-diagnostics.js +20 -0
  105. package/dist/src/launcher/managed-service-diagnostics.js.map +1 -0
  106. package/dist/src/launcher/managed-service-fleet.d.ts +11 -0
  107. package/dist/src/launcher/managed-service-fleet.d.ts.map +1 -0
  108. package/dist/src/launcher/managed-service-fleet.js +58 -0
  109. package/dist/src/launcher/managed-service-fleet.js.map +1 -0
  110. package/dist/src/launcher/managed-service-native-connection.d.ts +2 -0
  111. package/dist/src/launcher/managed-service-native-connection.d.ts.map +1 -1
  112. package/dist/src/launcher/managed-service-native-connection.js +4 -1
  113. package/dist/src/launcher/managed-service-native-connection.js.map +1 -1
  114. package/dist/src/launcher/managed-service-native-publications.d.ts +1 -1
  115. package/dist/src/launcher/managed-service-native-publications.d.ts.map +1 -1
  116. package/dist/src/launcher/managed-service-native-publications.js +25 -5
  117. package/dist/src/launcher/managed-service-native-publications.js.map +1 -1
  118. package/dist/src/launcher/managed-service-node-host.d.ts +7 -0
  119. package/dist/src/launcher/managed-service-node-host.d.ts.map +1 -1
  120. package/dist/src/launcher/managed-service-node-host.js +69 -23
  121. package/dist/src/launcher/managed-service-node-host.js.map +1 -1
  122. package/dist/src/launcher/managed-service-plugin-lifecycle.d.ts +3 -0
  123. package/dist/src/launcher/managed-service-plugin-lifecycle.d.ts.map +1 -1
  124. package/dist/src/launcher/managed-service-plugin-lifecycle.js +53 -15
  125. package/dist/src/launcher/managed-service-plugin-lifecycle.js.map +1 -1
  126. package/dist/src/launcher/managed-service-registration.d.ts +2 -0
  127. package/dist/src/launcher/managed-service-registration.d.ts.map +1 -1
  128. package/dist/src/launcher/managed-service-registration.js +6 -0
  129. package/dist/src/launcher/managed-service-registration.js.map +1 -1
  130. package/dist/src/launcher/managed-service-runtime-binding.d.ts.map +1 -1
  131. package/dist/src/launcher/managed-service-runtime-binding.js +5 -0
  132. package/dist/src/launcher/managed-service-runtime-binding.js.map +1 -1
  133. package/dist/src/launcher/managed-service-runtime-native.d.ts +15 -0
  134. package/dist/src/launcher/managed-service-runtime-native.d.ts.map +1 -0
  135. package/dist/src/launcher/managed-service-runtime-native.js +70 -0
  136. package/dist/src/launcher/managed-service-runtime-native.js.map +1 -0
  137. package/dist/src/launcher/managed-service-runtime-process.d.ts +3 -0
  138. package/dist/src/launcher/managed-service-runtime-process.d.ts.map +1 -1
  139. package/dist/src/launcher/managed-service-runtime-process.js +39 -9
  140. package/dist/src/launcher/managed-service-runtime-process.js.map +1 -1
  141. package/dist/src/launcher/managed-service-runtime.d.ts +8 -3
  142. package/dist/src/launcher/managed-service-runtime.d.ts.map +1 -1
  143. package/dist/src/launcher/managed-service-runtime.js +71 -55
  144. package/dist/src/launcher/managed-service-runtime.js.map +1 -1
  145. package/dist/src/launcher/managed-service-startup-diagnostic.d.ts.map +1 -1
  146. package/dist/src/launcher/managed-service-startup-diagnostic.js +9 -0
  147. package/dist/src/launcher/managed-service-startup-diagnostic.js.map +1 -1
  148. package/dist/src/launcher/management-rpc.d.ts +6 -0
  149. package/dist/src/launcher/management-rpc.d.ts.map +1 -1
  150. package/dist/src/launcher/management-rpc.js +7 -1
  151. package/dist/src/launcher/management-rpc.js.map +1 -1
  152. package/dist/src/launcher/model-catalog/cli-export.d.ts +35 -0
  153. package/dist/src/launcher/model-catalog/cli-export.d.ts.map +1 -0
  154. package/dist/src/launcher/model-catalog/cli-export.js +114 -0
  155. package/dist/src/launcher/model-catalog/cli-export.js.map +1 -0
  156. package/dist/src/launcher/native-agent-models.d.ts.map +1 -1
  157. package/dist/src/launcher/native-agent-models.js +9 -1
  158. package/dist/src/launcher/native-agent-models.js.map +1 -1
  159. package/dist/src/launcher/native-model-provider-catalog.d.ts.map +1 -1
  160. package/dist/src/launcher/native-model-provider-catalog.js +6 -1
  161. package/dist/src/launcher/native-model-provider-catalog.js.map +1 -1
  162. package/dist/src/launcher/native-predispatch-interception.d.ts.map +1 -1
  163. package/dist/src/launcher/native-predispatch-interception.js +36 -1
  164. package/dist/src/launcher/native-predispatch-interception.js.map +1 -1
  165. package/dist/src/launcher/native-submission-composition.d.ts +3 -0
  166. package/dist/src/launcher/native-submission-composition.d.ts.map +1 -1
  167. package/dist/src/launcher/native-submission-composition.js +21 -0
  168. package/dist/src/launcher/native-submission-composition.js.map +1 -1
  169. package/dist/src/launcher/native-submission-control-server.d.ts.map +1 -1
  170. package/dist/src/launcher/native-submission-control-server.js +55 -1
  171. package/dist/src/launcher/native-submission-control-server.js.map +1 -1
  172. package/dist/src/launcher/plugin-http-authority.d.ts +1 -0
  173. package/dist/src/launcher/plugin-http-authority.d.ts.map +1 -1
  174. package/dist/src/launcher/plugin-http-authority.js +20 -27
  175. package/dist/src/launcher/plugin-http-authority.js.map +1 -1
  176. package/dist/src/launcher/plugin-http-request-observation.d.ts +14 -0
  177. package/dist/src/launcher/plugin-http-request-observation.d.ts.map +1 -0
  178. package/dist/src/launcher/plugin-http-request-observation.js +65 -0
  179. package/dist/src/launcher/plugin-http-request-observation.js.map +1 -0
  180. package/dist/src/renderer/model-provider-reasoning.js +1 -1
  181. package/dist/src/renderer/model-provider-reasoning.js.map +1 -1
  182. package/dist/src/renderer/native-model-reasoning.d.ts.map +1 -1
  183. package/dist/src/renderer/native-model-reasoning.js +4 -1
  184. package/dist/src/renderer/native-model-reasoning.js.map +1 -1
  185. package/dist/src/renderer/react-runtime.d.ts +1 -1
  186. package/dist/src/shortcuts/dock.d.ts.map +1 -1
  187. package/dist/src/shortcuts/dock.js +25 -15
  188. package/dist/src/shortcuts/dock.js.map +1 -1
  189. package/dist/src/shortcuts/startup-cover.d.ts.map +1 -1
  190. package/dist/src/shortcuts/startup-cover.js +16 -1
  191. package/dist/src/shortcuts/startup-cover.js.map +1 -1
  192. package/package.json +2 -2
package/README.md CHANGED
@@ -66,3 +66,10 @@ and [CLI/distribution contract](https://github.com/cordisx/cordisx/blob/main/.ag
66
66
  The scoped Host form architecture and the reproducible official TDesign Web
67
67
  Components subset are documented in the
68
68
  [Host-owned form system](https://github.com/cordisx/cordisx/blob/main/.agents/docs/host-form-system.md).
69
+
70
+ ## Export model services
71
+
72
+ Use `cordisx model export --help` for portable service/model export and
73
+ `cordisx model export --list` for IDs. See the
74
+ [CLI export guide](https://github.com/cordisx/cordisx/blob/main/.agents/docs/model-service-cli-export.md)
75
+ for private file output, offline limits and target Manager Import instructions.
@@ -102,7 +102,7 @@
102
102
  "additionalProperties": false,
103
103
  "required": ["lookbackSeconds", "maxBytes", "maxEvents", "collectionTimeoutMs"],
104
104
  "properties": {
105
- "lookbackSeconds": { "type": "integer", "minimum": 1, "maximum": 86400 },
105
+ "lookbackSeconds": { "type": "integer", "minimum": 1, "maximum": 259200 },
106
106
  "maxBytes": { "type": "integer", "minimum": 1, "maximum": 10485760 },
107
107
  "maxEvents": { "type": "integer", "minimum": 1, "maximum": 10000 },
108
108
  "collectionTimeoutMs": { "type": "integer", "minimum": 1, "maximum": 30000 }
@@ -1,605 +1,38 @@
1
1
  #!/bin/sh
2
2
  ':' // ; exec "${CORDISX_NATIVE_NODE_PATH:-${CORDISX_NATIVE_REAL_CODEX_PATH%%/Contents/Resources/*}/Contents/Resources/cua_node/bin/node}" "$0" "$@"
3
3
  import { spawn } from 'node:child_process'
4
- import net from 'node:net'
5
- import { fileURLToPath } from 'node:url'
6
- import { NativeRequestTracker } from './native-request-tracker.mjs'
7
-
8
- const socketPath = process.env.CORDISX_NATIVE_CONTROL_SOCKET
9
- const nonce = process.env.CORDISX_NATIVE_CONTROL_NONCE
10
- const executable = process.env.CORDISX_NATIVE_REAL_CODEX_PATH
11
- const agentModelsBinding = process.env.CORDISX_NATIVE_AGENT_MODELS_BINDING
12
- const marker = 'cordisx.operation_token'
13
- // Native history and plugin catalogs can exceed the small private control-message budget.
14
- const maxNativeLineBytes = 64 * 1024 * 1024
15
- const maxControlLineBytes = 8 * 1024 * 1024
16
- if (!socketPath || !nonce || !executable?.startsWith('/')) throw new Error('Native intermediary is not configured')
17
- for (
18
- const key of [
19
- 'CODEX_CLI_PATH',
20
- 'CORDISX_NATIVE_CONTROL_SOCKET',
21
- 'CORDISX_NATIVE_CONTROL_NONCE',
22
- 'CORDISX_NATIVE_REAL_CODEX_PATH',
23
- 'CORDISX_NATIVE_NODE_PATH',
24
- 'CORDISX_NATIVE_AGENT_MODELS_BINDING',
25
- ]
26
- ) delete process.env[key]
27
-
28
- const nativeArgs = process.argv.slice(2)
29
- if (agentModelsBinding && nativeArgs.includes('app-server')) {
30
- nativeArgs.unshift(
31
- '-c',
32
- `mcp_servers.cordisx_models.command=${JSON.stringify(process.execPath)}`,
33
- '-c',
34
- `mcp_servers.cordisx_models.args=${
35
- JSON.stringify([
36
- fileURLToPath(new URL('./native-agent-models-mcp.mjs', import.meta.url)),
37
- agentModelsBinding,
38
- ])
39
- }`,
40
- )
41
- }
42
- const child = spawn(executable, nativeArgs, { stdio: ['pipe', 'pipe', 'inherit'], env: process.env })
43
- const control = net.connect(socketPath)
44
- const controlCalls = new Map()
45
- const privateCalls = new Map()
46
- const desktopCalls = new Map()
47
- const resumeCalls = new NativeRequestTracker()
48
- const unmarkedTurns = new NativeRequestTracker()
49
- // An unacknowledged lifecycle mutation stays uncertain; retain its fence, not its payload.
50
- const lifecycleCalls = new Map()
51
- // If the bounded map loses a mutation, its safety fence lasts until this process restarts.
52
- // Ordinary native requests still pass through; only automatic recovery/Host switching stops.
53
- let lifecycleUncertain = false
54
- const recoveringTurns = new Set()
55
- const cancelledRecovery = new Set()
56
- const firstTurns = new Map()
57
- const agentFirstTurns = new Map()
58
- const privatePrefix = `cordisx-private-${nonce}-`
59
- let sequence = 0
60
- let closed = false
61
- let inputQueue = Promise.resolve()
62
- const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
63
- const validToken = value => typeof value === 'string' && /^[^\0\r\n]{16,256}$/u.test(value)
64
- const lifecyclePending = threadId => lifecycleUncertain || [...lifecycleCalls.values()].includes(threadId)
65
- const write = (stream, value) =>
66
- new Promise((resolve, reject) => {
67
- if (stream.destroyed) {
68
- reject(new Error('Native transport is closed'))
69
- return
70
- }
71
- stream.write(`${typeof value === 'string' ? value : JSON.stringify(value)}\n`, error =>
72
- error ? reject(error) : resolve())
73
- })
74
-
75
- function rejectAll(calls) {
76
- for (const call of calls.values()) {
77
- clearTimeout(call.timer)
78
- call.reject(new Error('Native transport closed'))
79
- }
80
- calls.clear()
81
- }
82
- function stop(reason = 'transport-closed') {
83
- if (closed) return
84
- closed = true
85
- if (typeof reason === 'string' && reason !== 'transport-closed') {
86
- process.stderr.write(`[cordisx] Native intermediary stopped: ${reason}\n`)
87
- }
88
- rejectAll(controlCalls)
89
- rejectAll(privateCalls)
90
- desktopCalls.clear()
91
- resumeCalls.clear()
92
- unmarkedTurns.clear()
93
- recoveringTurns.clear()
94
- cancelledRecovery.clear()
95
- lifecycleCalls.clear()
96
- firstTurns.clear()
97
- agentFirstTurns.clear()
98
- control.destroy()
99
- process.stdin.destroy()
100
- child.stdin.destroy()
101
- if (child.exitCode === null) child.kill('SIGTERM')
102
- }
103
- function call(stream, calls, request, timeout = 30_000) {
104
- if (closed || calls.size >= 32) return Promise.reject(new Error('Native transport unavailable'))
105
- const id = `${privatePrefix}${++sequence}`
106
- return new Promise((resolve, reject) => {
107
- const settle = (error, value) => {
108
- const current = calls.get(id)
109
- if (!current) return
110
- calls.delete(id)
111
- clearTimeout(current.timer)
112
- error ? reject(error) : resolve(value)
113
- }
114
- const timer = setTimeout(() => settle(new Error('Native transport timeout')), timeout)
115
- calls.set(id, { resolve, reject, timer })
116
- void write(stream, { ...request, id }).catch(error => settle(error))
117
- })
118
- }
119
- function controlCall(type, input = {}) {
120
- return call(control, controlCalls, { type, ...input })
121
- }
122
- function nativeCall(method, params) {
123
- return call(child.stdin, privateCalls, { method, params })
124
- }
125
- function settle(calls, message, isControl) {
126
- const pending = calls.get(message.id)
127
- if (!pending) return false
128
- calls.delete(message.id)
129
- clearTimeout(pending.timer)
130
- const failed = isControl ? message.ok !== true : message.error !== undefined
131
- failed
132
- ? pending.reject(
133
- Object.assign(new Error('Native request rejected'), isControl ? {} : { nativeError: message.error }),
134
- )
135
- : pending.resolve(isControl ? message.value : message.result)
136
- return true
137
- }
138
-
139
- function missingManagedProvider(message) {
140
- if (message?.error?.code !== -32600 || typeof message.error.message !== 'string') return undefined
141
- const match = /^failed to load configuration: Model provider `([^`\0\r\n]{1,128})` not found$/u.exec(
142
- message.error.message,
143
- )
144
- return match?.[1]
145
- }
146
-
147
- // Only framed messages are buffered. Many small lines in one chunk do not count as one large request.
148
- function readLines(stream, receive, maxLineBytes = maxNativeLineBytes) {
149
- let buffer = ''
150
- stream.setEncoding('utf8')
151
- stream.on('data', chunk => {
152
- buffer += chunk
153
- for (;;) {
154
- const newline = buffer.indexOf('\n')
155
- if (newline < 0) break
156
- const line = buffer.slice(0, newline)
157
- buffer = buffer.slice(newline + 1)
158
- if (Buffer.byteLength(line) > maxLineBytes) {
159
- stop('frame-limit-exceeded')
160
- return
161
- }
162
- if (line.length > 0) receive(line)
163
- }
164
- if (Buffer.byteLength(buffer) > maxLineBytes) stop('frame-limit-exceeded')
4
+ import { nativeCliInvocation } from './native-cli-invocation.mjs'
5
+
6
+ const args = process.argv.slice(2)
7
+ const appServer = nativeCliInvocation(args) === 'app-server-stdio'
8
+ if (appServer) {
9
+ await import('./native-app-server-protocol.mjs')
10
+ } else {
11
+ // Version/help and other ordinary CLI calls must never reserve the single app-server control peer.
12
+ const executable = process.env.CORDISX_NATIVE_REAL_CODEX_PATH
13
+ if (!executable?.startsWith('/')) throw new Error('Native CLI path is not configured')
14
+ const environment = { ...process.env }
15
+ for (
16
+ const key of [
17
+ 'CODEX_CLI_PATH',
18
+ 'CORDISX_NATIVE_CONTROL_SOCKET',
19
+ 'CORDISX_NATIVE_CONTROL_NONCE',
20
+ 'CORDISX_NATIVE_REAL_CODEX_PATH',
21
+ 'CORDISX_NATIVE_NODE_PATH',
22
+ 'CORDISX_NATIVE_AGENT_MODELS_BINDING',
23
+ ]
24
+ ) delete environment[key]
25
+ const child = spawn(executable, args, { stdio: 'inherit', env: environment })
26
+ for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, () => child.kill(signal))
27
+ child.on('error', error => {
28
+ const code = ['ENOENT', 'EACCES', 'EPERM'].includes(error.code) ? error.code : 'unknown'
29
+ process.stderr.write(`[cordisx] Native CLI pass-through failed: ${code}\n`)
30
+ process.exitCode = 1
165
31
  })
166
- stream.once('end', () => {
167
- if (buffer.trim() !== '') stop('incomplete-frame')
32
+ child.on('exit', (code, signal) => {
33
+ if (signal) {
34
+ process.removeAllListeners(signal)
35
+ process.kill(process.pid, signal)
36
+ } else process.exitCode = code ?? 1
168
37
  })
169
38
  }
170
-
171
- let authenticate
172
- const ready = new Promise((resolve, reject) => {
173
- authenticate = { resolve, reject }
174
- })
175
- // A rejected readiness promise must also be observed when no desktop request is queued.
176
- void ready.catch(() => undefined)
177
- control.once('connect', () => {
178
- void controlCall('hello', { nonce }).then(authenticate.resolve, error => {
179
- authenticate.reject(error)
180
- stop()
181
- })
182
- })
183
- control.on('error', () => {
184
- authenticate.reject(new Error('Native control unavailable'))
185
- stop()
186
- })
187
- control.on('close', stop)
188
- child.on('error', stop)
189
- child.stdin.on('error', stop)
190
- process.stdout.on('error', stop)
191
- child.on('exit', (code, signal) => {
192
- process.exitCode = code ?? (signal ? 1 : 0)
193
- stop()
194
- })
195
- process.on('SIGTERM', stop)
196
- process.on('SIGINT', stop)
197
-
198
- async function forward(line) {
199
- if (closed) return
200
- let message
201
- try {
202
- message = JSON.parse(line)
203
- } catch {
204
- await write(child.stdin, line)
205
- return
206
- }
207
- const params = object(message?.params)
208
- if (
209
- message?.method === 'turn/start' && agentFirstTurns.has(params?.threadId)
210
- && params.config?.[marker] !== agentFirstTurns.get(params.threadId)
211
- ) {
212
- await write(process.stdout, {
213
- id: message.id,
214
- error: { code: -32000, message: 'Native task first turn association unavailable' },
215
- })
216
- return
217
- }
218
- const config = object(params?.config)
219
- const managed = config !== undefined && Object.hasOwn(config, marker)
220
- if (message?.method === 'turn/interrupt' && typeof params?.threadId === 'string') {
221
- unmarkedTurns.retireThread(params.threadId)
222
- if (recoveringTurns.has(params.threadId)) cancelledRecovery.add(params.threadId)
223
- }
224
- const mutatesThread = ['turn/start', 'turn/addUserMessage', 'turn/steer', 'thread/resume', 'thread/unsubscribe']
225
- .includes(message?.method)
226
- if (
227
- mutatesThread && (recoveringTurns.has(params?.threadId) || switching.has(params?.threadId))
228
- ) {
229
- await write(process.stdout, {
230
- id: message.id,
231
- error: { code: -32000, message: 'Thread recovery is in progress. Wait before sending another message.' },
232
- })
233
- return
234
- }
235
- if (!managed) {
236
- if (message?.method === 'turn/start' && firstTurns.has(params?.threadId)) {
237
- await write(process.stdout, {
238
- id: message.id,
239
- error: {
240
- code: -32000,
241
- message: 'Managed first turn is missing its Send association. Original request was not sent.',
242
- },
243
- })
244
- return
245
- }
246
- if (typeof message?.id === 'string' && message.id.startsWith(privatePrefix)) throw new Error('Reserved request id')
247
- if (
248
- message?.method === 'thread/resume' && typeof params?.threadId === 'string'
249
- && ['string', 'number'].includes(typeof message.id)
250
- ) resumeCalls.track(message.id, params)
251
- if (
252
- message?.method === 'turn/start' && typeof params?.threadId === 'string'
253
- && ['string', 'number'].includes(typeof message.id)
254
- ) unmarkedTurns.track(message.id, params)
255
- if (
256
- ['thread/resume', 'thread/unsubscribe'].includes(message?.method)
257
- && typeof params?.threadId === 'string' && ['string', 'number'].includes(typeof message.id)
258
- ) {
259
- if (lifecycleCalls.size >= 64 || lifecycleCalls.has(message.id)) {
260
- // Continue the native request; uncertain correlation disables only automatic recovery/switching.
261
- lifecycleUncertain = true
262
- } else lifecycleCalls.set(message.id, params.threadId)
263
- if (message.method === 'thread/unsubscribe') unmarkedTurns.retireThread(params.threadId)
264
- }
265
- await write(child.stdin, line)
266
- return
267
- }
268
- let admitted = false
269
- const token = config[marker]
270
- try {
271
- if (
272
- !validToken(token) || !['thread/start', 'turn/start'].includes(message.method)
273
- || !['string', 'number'].includes(typeof message.id) || desktopCalls.has(message.id)
274
- ) throw new Error('Invalid managed request')
275
- const dispatch = await controlCall('consume', {
276
- token,
277
- method: message.method,
278
- requestId: message.id,
279
- ...(typeof params.threadId === 'string' ? { threadId: params.threadId } : {}),
280
- })
281
- if (
282
- dispatch?.kind !== 'dispatch' || typeof dispatch.model !== 'string' || typeof dispatch.providerId !== 'string'
283
- ) throw new Error('Managed request rejected')
284
- admitted = true
285
- const nextConfig = { ...config }
286
- delete nextConfig[marker]
287
- if (message.method === 'thread/start') {
288
- params.modelProvider = dispatch.providerId
289
- params.model = dispatch.model
290
- params.config = { ...nextConfig, ...dispatch.configOverrides }
291
- if (typeof dispatch.reasoningEffort === 'string') {
292
- params.config.model_reasoning_effort = dispatch.reasoningEffort
293
- }
294
- } else {
295
- // The private resume sets provider config. The original turn retains all user input and controls.
296
- params.model = dispatch.model
297
- if (typeof dispatch.reasoningEffort === 'string') params.effort = dispatch.reasoningEffort
298
- if (Object.keys(nextConfig).length === 0) delete params.config
299
- else params.config = nextConfig
300
- const mode = object(params.collaborationMode)
301
- if (mode && object(mode.settings)) {
302
- params.collaborationMode = {
303
- ...mode,
304
- settings: {
305
- ...mode.settings,
306
- model: dispatch.model,
307
- ...(typeof dispatch.reasoningEffort === 'string'
308
- ? { reasoning_effort: dispatch.reasoningEffort }
309
- : {}),
310
- },
311
- }
312
- }
313
- }
314
- const authorized = await controlCall('authorize', { token, requestId: message.id })
315
- if (authorized !== true) throw new Error('Managed request authorization expired')
316
- desktopCalls.set(message.id, { token, method: message.method, agentTask: dispatch.agentTask === true })
317
- if (message.method === 'turn/start') {
318
- firstTurns.delete(params.threadId)
319
- agentFirstTurns.delete(params.threadId)
320
- }
321
- await write(child.stdin, JSON.stringify(message))
322
- } catch {
323
- desktopCalls.delete(message?.id)
324
- if (admitted) {
325
- await controlCall('complete', { token, requestId: message?.id, succeeded: false }).catch(() => undefined)
326
- }
327
- if (message?.id !== undefined) {
328
- await write(process.stdout, {
329
- id: message.id,
330
- error: { code: -32000, message: 'Provider switch could not be applied. Original request was not sent.' },
331
- })
332
- }
333
- }
334
- }
335
-
336
- // A restarted app-server no longer knows a launch-scoped managed provider that a persisted thread still names.
337
- async function recoverResume(id, params, providerId, failureLine) {
338
- if (
339
- recoveringTurns.has(params.threadId) || switching.has(params.threadId)
340
- || lifecyclePending(params.threadId)
341
- ) {
342
- await write(process.stdout, failureLine)
343
- return
344
- }
345
- recoveringTurns.add(params.threadId)
346
- try {
347
- const read = await nativeCall('thread/read', { threadId: params.threadId, includeTurns: false })
348
- const persistedProviderId = read?.thread?.modelProvider
349
- const model = read?.thread?.model
350
- if (
351
- persistedProviderId !== providerId || providerId === 'openai'
352
- || typeof model !== 'string' || model === ''
353
- ) throw new Error('Native thread is not managed')
354
- const prepared = await controlCall('resume', { threadId: params.threadId, providerId, model })
355
- const overrides = object(prepared?.configOverrides)
356
- if (prepared?.kind !== 'resume' || !validToken(prepared.resumeToken) || overrides === undefined) {
357
- throw new Error('Managed resume rejected')
358
- }
359
- let result
360
- try {
361
- result = await nativeCall('thread/resume', { ...params, config: { ...object(params.config), ...overrides } })
362
- if (result?.thread?.id !== params.threadId) throw new Error('Managed resume returned another thread')
363
- } catch (error) {
364
- await controlCall('resume-complete', {
365
- threadId: params.threadId,
366
- resumeToken: prepared.resumeToken,
367
- succeeded: false,
368
- }).catch(() => undefined)
369
- throw error
370
- }
371
- const completed = await controlCall('resume-complete', {
372
- threadId: params.threadId,
373
- resumeToken: prepared.resumeToken,
374
- succeeded: true,
375
- })
376
- if (completed !== true) throw new Error('Managed resume completion rejected')
377
- // Recovery restores the persisted provider; it does not prepare a Send or
378
- // provider change. Unchanged-provider sends/retries have no operation token.
379
- // Only thread/start creates a first-turn association fence; retain any
380
- // existing fence rather than introducing one for a historical thread.
381
- await write(process.stdout, { id, result })
382
- } catch {
383
- // The original native failure stays authoritative whenever the Host cannot vouch for the provider.
384
- await write(process.stdout, failureLine)
385
- } finally {
386
- recoveringTurns.delete(params.threadId)
387
- cancelledRecovery.delete(params.threadId)
388
- }
389
- }
390
-
391
- // A persisted history can exist after its executable session has been unloaded.
392
- // Replay only the exact missing-thread rejection, never a timeout or failed turn.
393
- async function recoverUnloadedTurn(id, params, failureLine) {
394
- const threadId = params.threadId
395
- if (recoveringTurns.has(threadId) || switching.has(threadId) || lifecyclePending(threadId)) {
396
- await write(process.stdout, failureLine)
397
- return
398
- }
399
- recoveringTurns.add(threadId)
400
- let resumeToken
401
- let committed = false
402
- let replayAttempted = false
403
- try {
404
- const read = await nativeCall('thread/read', { threadId, includeTurns: false })
405
- const thread = object(read?.thread)
406
- if (
407
- thread?.id !== threadId || thread.status?.type !== 'notLoaded'
408
- || typeof thread.modelProvider !== 'string' || thread.modelProvider === 'openai'
409
- || typeof thread.model !== 'string' || thread.model === ''
410
- ) throw new Error('Missing thread is not an unloaded managed session')
411
- const prepared = await controlCall('resume', {
412
- threadId,
413
- providerId: thread.modelProvider,
414
- model: thread.model,
415
- })
416
- if (prepared?.kind !== 'resume' || !validToken(prepared.resumeToken) || !object(prepared.configOverrides)) {
417
- throw new Error('Managed recovery rejected')
418
- }
419
- resumeToken = prepared.resumeToken
420
- const resumed = await nativeCall('thread/resume', {
421
- threadId,
422
- modelProvider: thread.modelProvider,
423
- model: thread.model,
424
- config: prepared.configOverrides,
425
- excludeTurns: true,
426
- })
427
- if (
428
- resumed?.thread?.id !== threadId || resumed.modelProvider !== thread.modelProvider
429
- || resumed.model !== thread.model || resumed.thread.status?.type !== 'idle'
430
- ) throw new Error('Recovery did not acknowledge the same idle thread and provider')
431
- committed = await controlCall('resume-complete', { threadId, resumeToken, succeeded: true }) === true
432
- if (!committed) throw new Error('Managed recovery completion rejected')
433
- const current = await nativeCall('thread/read', { threadId, includeTurns: false })
434
- if (
435
- cancelledRecovery.has(threadId) || current?.thread?.id !== threadId
436
- || current.thread.status?.type !== 'idle' || current.thread.modelProvider !== thread.modelProvider
437
- || current.thread.model !== thread.model
438
- ) {
439
- throw new Error('Recovered thread changed before replay')
440
- }
441
- // The replay uses a private id but preserves all original parameters and
442
- // native permission checks. Its result is delivered once under the original id.
443
- replayAttempted = true
444
- const result = await nativeCall('turn/start', params)
445
- await write(process.stdout, { id, result })
446
- } catch (error) {
447
- if (resumeToken !== undefined && !committed) {
448
- await controlCall('resume-complete', { threadId, resumeToken, succeeded: false }).catch(() => undefined)
449
- }
450
- // Once replay was attempted, the original missing-thread failure is no
451
- // longer authoritative. Preserve a native rejection or report uncertainty
452
- // rather than falsely telling Desktop that the message was never sent.
453
- await write(
454
- process.stdout,
455
- replayAttempted
456
- ? {
457
- id,
458
- error: error.nativeError ?? {
459
- code: -32000,
460
- message: 'Recovered turn result is unknown. Check thread history before retrying.',
461
- },
462
- }
463
- : failureLine,
464
- )
465
- } finally {
466
- recoveringTurns.delete(threadId)
467
- cancelledRecovery.delete(threadId)
468
- }
469
- }
470
-
471
- readLines(process.stdin, line => {
472
- inputQueue = inputQueue.then(async () => {
473
- await ready
474
- await forward(line)
475
- }).catch(stop)
476
- })
477
- process.stdin.once('end', () => {
478
- void inputQueue.finally(() => child.stdin.end())
479
- })
480
-
481
- readLines(child.stdout, line => {
482
- let message
483
- try {
484
- message = JSON.parse(line)
485
- } catch {
486
- void write(process.stdout, line).catch(stop)
487
- return
488
- }
489
- if (settle(privateCalls, message, false)) return
490
- // Late private responses must never escape into the Desktop's request table.
491
- if (typeof message?.id === 'string' && message.id.startsWith(privatePrefix)) return
492
- if (message.method === undefined) lifecycleCalls.delete(message.id)
493
- const turnParams = message.method === undefined ? unmarkedTurns.take(message.id) : undefined
494
- if (turnParams !== undefined) {
495
- if (message.error?.code === -32600 && message.error.message === `thread not found: ${turnParams.threadId}`) {
496
- void recoverUnloadedTurn(message.id, turnParams, line).catch(stop)
497
- return
498
- }
499
- }
500
- const resumeParams = message.method === undefined ? resumeCalls.take(message?.id) : undefined
501
- if (resumeParams !== undefined) {
502
- const providerId = missingManagedProvider(message)
503
- if (providerId !== undefined) {
504
- void recoverResume(message.id, resumeParams, providerId, line).catch(stop)
505
- return
506
- }
507
- }
508
- const operation = desktopCalls.get(message?.id)
509
- if (!operation || message.method !== undefined) {
510
- void write(process.stdout, line).catch(stop)
511
- return
512
- }
513
- desktopCalls.delete(message.id)
514
- if (
515
- operation.method === 'thread/start' && message.error === undefined && typeof message.result?.thread?.id === 'string'
516
- ) {
517
- firstTurns.set(message.result.thread.id, operation.token)
518
- if (operation.agentTask) agentFirstTurns.set(message.result.thread.id, operation.token)
519
- }
520
- void controlCall('complete', {
521
- token: operation.token,
522
- requestId: message.id,
523
- succeeded: message.error === undefined,
524
- ...(typeof message.result?.thread?.id === 'string' ? { threadId: message.result.thread.id } : {}),
525
- ...(operation.agentTask && operation.method === 'thread/start'
526
- ? {
527
- providerId: message.result?.modelProvider ?? message.result?.thread?.modelProvider,
528
- model: message.result?.model ?? message.result?.thread?.model,
529
- }
530
- : {}),
531
- }).then(() => write(process.stdout, line)).catch(stop)
532
- })
533
-
534
- const switching = new Set()
535
- readLines(control, line => {
536
- let message
537
- try {
538
- message = JSON.parse(line)
539
- } catch {
540
- stop()
541
- return
542
- }
543
- if (settle(controlCalls, message, true)) return
544
- if (message.type === 'task-create' && typeof message.id === 'string') {
545
- void nativeCall('mcpServer/tool/call', {
546
- server: 'codex_app',
547
- threadId: message.sourceThreadId,
548
- tool: 'create_thread',
549
- arguments: message.arguments,
550
- _meta: {
551
- 'openai/toolCallId': message.callId,
552
- 'openai/threadId': message.sourceThreadId,
553
- 'openai/turnId': message.sourceTurnId,
554
- },
555
- }).then(value => write(control, { id: message.id, ok: true, value }))
556
- .catch(() => write(control, { id: message.id, ok: false }).catch(stop))
557
- return
558
- }
559
- if (message.type === 'idle' && typeof message.id === 'string' && typeof message.threadId === 'string') {
560
- void nativeCall('thread/read', { threadId: message.threadId, includeTurns: false }).then(result => {
561
- const status = result?.thread?.status?.type
562
- return write(control, {
563
- id: message.id,
564
- ok: true,
565
- value: status === 'idle' || status === 'notLoaded' || status === 'systemError',
566
- })
567
- }).catch(() => write(control, { id: message.id, ok: false }).catch(stop))
568
- return
569
- }
570
- if (
571
- message.type !== 'switch' || typeof message.id !== 'string'
572
- || switching.has(message.threadId) || recoveringTurns.has(message.threadId)
573
- || lifecyclePending(message.threadId)
574
- ) {
575
- if (message.type === 'switch') void write(control, { id: message.id, ok: false }).catch(stop)
576
- return
577
- }
578
- switching.add(message.threadId)
579
- void (async () => {
580
- try {
581
- if (message.requireIdle === true) {
582
- const before = await nativeCall('thread/read', { threadId: message.threadId, includeTurns: false })
583
- if (before?.thread?.id !== message.threadId || before?.thread?.status?.type !== 'idle') {
584
- await write(control, { id: message.id, ok: true, value: { status: 'rejected', reason: 'thread-not-idle' } })
585
- return
586
- }
587
- }
588
- await nativeCall('thread/unsubscribe', { threadId: message.threadId })
589
- const result = await nativeCall('thread/resume', {
590
- threadId: message.threadId,
591
- model: message.model,
592
- modelProvider: message.providerId,
593
- config: message.config,
594
- excludeTurns: true,
595
- })
596
- const ok = result?.thread?.id === message.threadId && result?.modelProvider === message.providerId
597
- && result?.model === message.model
598
- await write(control, { id: message.id, ok })
599
- } catch {
600
- await write(control, { id: message.id, ok: false })
601
- } finally {
602
- switching.delete(message.threadId)
603
- }
604
- })().catch(stop)
605
- }, maxControlLineBytes)