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