cordisx 0.1.0-beta.45 → 0.1.0-beta.46
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-protocol.mjs +123 -26
- package/dist/assets/launcher/native-managed-resume.mjs +3 -2
- package/dist/assets/launcher/native-thread-switch.mjs +112 -0
- package/dist/native/native-helper-artifact.json +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-cdp-channel.js +55 -2
- package/dist/src/launcher/native-submission-cdp-channel.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 +102 -6
- package/dist/src/launcher/native-submission-control-server.js.map +1 -1
- package/dist/src/launcher/native-submission-controller-contracts.d.ts +5 -3
- package/dist/src/launcher/native-submission-controller-contracts.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-controller.d.ts.map +1 -1
- package/dist/src/launcher/native-submission-controller.js +20 -5
- package/dist/src/launcher/native-submission-controller.js.map +1 -1
- package/dist/src/launcher/native-thread-switch-error.d.ts +8 -0
- package/dist/src/launcher/native-thread-switch-error.d.ts.map +1 -0
- package/dist/src/launcher/native-thread-switch-error.js +12 -0
- package/dist/src/launcher/native-thread-switch-error.js.map +1 -0
- package/dist/src/renderer/model-provider-copy.d.ts +4 -0
- package/dist/src/renderer/model-provider-copy.d.ts.map +1 -1
- package/dist/src/renderer/model-provider-copy.js +18 -0
- package/dist/src/renderer/model-provider-copy.js.map +1 -1
- package/dist/src/renderer/model-provider-selector.d.ts.map +1 -1
- package/dist/src/renderer/model-provider-selector.js +6 -5
- package/dist/src/renderer/model-provider-selector.js.map +1 -1
- package/dist/src/renderer/native-model-provider-transport.js +1 -1
- package/dist/src/renderer/native-model-provider-transport.js.map +1 -1
- package/dist/src/renderer/native-provider-selection-client.d.ts +7 -1
- package/dist/src/renderer/native-provider-selection-client.d.ts.map +1 -1
- package/dist/src/renderer/native-provider-selection-client.js +61 -6
- package/dist/src/renderer/native-provider-selection-client.js.map +1 -1
- package/dist/src/renderer/native-provider-submission-policy.d.ts +1 -0
- package/dist/src/renderer/native-provider-submission-policy.d.ts.map +1 -1
- package/dist/src/renderer/native-provider-submission-policy.js.map +1 -1
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ import { prepareManagedResume } from './native-managed-resume.mjs'
|
|
|
8
8
|
import { nativeInput } from './native-app-server-input.mjs'
|
|
9
9
|
import { createNativeProviderTransport } from './native-provider-transport.mjs'
|
|
10
10
|
import { nativeProviderFailure } from './native-provider-errors.mjs'
|
|
11
|
+
import { switchNativeThread } from './native-thread-switch.mjs'
|
|
11
12
|
|
|
12
13
|
const socketPath = process.env.CORDISX_NATIVE_CONTROL_SOCKET
|
|
13
14
|
const nonce = process.env.CORDISX_NATIVE_CONTROL_NONCE
|
|
@@ -76,6 +77,8 @@ const preparedResumes = new Map()
|
|
|
76
77
|
const unmarkedTurns = new NativeRequestTracker()
|
|
77
78
|
// An unacknowledged lifecycle mutation stays uncertain; retain its fence, not its payload.
|
|
78
79
|
const lifecycleCalls = new Map()
|
|
80
|
+
const uncertainSwitches = new Map()
|
|
81
|
+
const switchReconciliations = new NativeRequestTracker()
|
|
79
82
|
// If the bounded map loses a mutation, its safety fence lasts until this process restarts.
|
|
80
83
|
// Ordinary native requests still pass through; only automatic recovery/Host switching stops.
|
|
81
84
|
let lifecycleUncertain = false
|
|
@@ -166,7 +169,9 @@ const nativeErrorCode = error =>
|
|
|
166
169
|
['ENOENT', 'EACCES', 'EPERM', 'EPIPE', 'ECONNRESET', 'ECONNREFUSED'].includes(error?.code) ? error.code : 'unknown'
|
|
167
170
|
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
|
|
168
171
|
const validToken = value => typeof value === 'string' && /^[^\0\r\n]{16,256}$/u.test(value)
|
|
169
|
-
const lifecyclePending = threadId =>
|
|
172
|
+
const lifecyclePending = threadId =>
|
|
173
|
+
lifecycleUncertain || uncertainSwitches.has(threadId)
|
|
174
|
+
|| [...lifecycleCalls.values()].includes(threadId)
|
|
170
175
|
const write = (stream, value) =>
|
|
171
176
|
new Promise((resolve, reject) => {
|
|
172
177
|
if (stream.destroyed) {
|
|
@@ -210,6 +215,9 @@ function stop(reason = 'transport-closed') {
|
|
|
210
215
|
recoveringTurns.clear()
|
|
211
216
|
cancelledRecovery.clear()
|
|
212
217
|
lifecycleCalls.clear()
|
|
218
|
+
uncertainSwitches.clear()
|
|
219
|
+
switchReconciliations.clear()
|
|
220
|
+
switchRequests.clear()
|
|
213
221
|
firstTurns.clear()
|
|
214
222
|
agentFirstTurns.clear()
|
|
215
223
|
control.destroy()
|
|
@@ -240,6 +248,18 @@ function controlCall(type, input = {}) {
|
|
|
240
248
|
function nativeCall(method, params) {
|
|
241
249
|
return call(nativeTransport.stdin, privateCalls, { method, params })
|
|
242
250
|
}
|
|
251
|
+
function acknowledgedSwitchResume(message, expected) {
|
|
252
|
+
return expected && message.error === undefined && message.result?.thread?.id === expected.threadId
|
|
253
|
+
&& message.result.thread.status?.type === 'idle' && message.result.modelProvider === expected.providerId
|
|
254
|
+
&& message.result.model === expected.model
|
|
255
|
+
}
|
|
256
|
+
function reconcileSwitch(message, expected, hostAdmitted = false) {
|
|
257
|
+
if (
|
|
258
|
+
acknowledgedSwitchResume(message, expected) && uncertainSwitches.get(expected.threadId) === expected
|
|
259
|
+
&& (expected.providerId === 'openai' || hostAdmitted) && !lifecycleUncertain
|
|
260
|
+
&& ![...lifecycleCalls.values()].includes(expected.threadId)
|
|
261
|
+
) uncertainSwitches.delete(expected.threadId)
|
|
262
|
+
}
|
|
243
263
|
function settle(calls, message, isControl) {
|
|
244
264
|
const pending = calls.get(message.id)
|
|
245
265
|
if (!pending) return false
|
|
@@ -382,10 +402,24 @@ async function forward(line) {
|
|
|
382
402
|
const managed = config !== undefined && Object.hasOwn(config, marker)
|
|
383
403
|
if (message?.method === 'turn/interrupt' && typeof params?.threadId === 'string') {
|
|
384
404
|
unmarkedTurns.retireThread(params.threadId)
|
|
385
|
-
if (recoveringTurns.has(params.threadId)) cancelledRecovery.add(params.threadId)
|
|
405
|
+
if (recoveringTurns.has(params.threadId) || switching.has(params.threadId)) cancelledRecovery.add(params.threadId)
|
|
386
406
|
}
|
|
387
407
|
const mutatesThread = ['turn/start', 'turn/addUserMessage', 'turn/steer', 'thread/resume', 'thread/unsubscribe']
|
|
388
408
|
.includes(message?.method)
|
|
409
|
+
if (
|
|
410
|
+
uncertainSwitches.has(params?.threadId)
|
|
411
|
+
&& ['thread/resume', 'thread/unsubscribe', 'turn/interrupt'].includes(message?.method)
|
|
412
|
+
) uncertainSwitches.set(params.threadId, undefined)
|
|
413
|
+
if (
|
|
414
|
+
uncertainSwitches.has(params?.threadId)
|
|
415
|
+
&& ['turn/start', 'turn/addUserMessage', 'turn/steer'].includes(message?.method)
|
|
416
|
+
) {
|
|
417
|
+
await write(process.stdout, {
|
|
418
|
+
id: message.id,
|
|
419
|
+
error: { code: -32000, message: 'Provider switch status is uncertain. Reopen this conversation before sending.' },
|
|
420
|
+
})
|
|
421
|
+
return
|
|
422
|
+
}
|
|
389
423
|
if (
|
|
390
424
|
mutatesThread && (recoveringTurns.has(params?.threadId) || switching.has(params?.threadId))
|
|
391
425
|
) {
|
|
@@ -428,7 +462,37 @@ async function forward(line) {
|
|
|
428
462
|
if (
|
|
429
463
|
message?.method === 'thread/resume' && typeof params?.threadId === 'string'
|
|
430
464
|
&& ['string', 'number'].includes(typeof message.id)
|
|
431
|
-
)
|
|
465
|
+
) {
|
|
466
|
+
trackedResume = resumeCalls.track(message.id, params)
|
|
467
|
+
if (uncertainSwitches.has(params.threadId)) {
|
|
468
|
+
let expected
|
|
469
|
+
try {
|
|
470
|
+
const before = await nativeCall('thread/read', { threadId: params.threadId, includeTurns: false })
|
|
471
|
+
const thread = before?.thread
|
|
472
|
+
if (
|
|
473
|
+
thread?.id === params.threadId && typeof thread.modelProvider === 'string'
|
|
474
|
+
&& typeof thread.model === 'string'
|
|
475
|
+
&& ['idle', 'notLoaded', 'systemError'].includes(thread.status?.type)
|
|
476
|
+
&& params.path == null && params.history == null
|
|
477
|
+
&& (params.modelProvider == null || params.modelProvider === thread.modelProvider)
|
|
478
|
+
&& (params.config?.model_provider === undefined || params.config.model_provider === thread.modelProvider)
|
|
479
|
+
&& (params.model == null || params.model === thread.model)
|
|
480
|
+
) expected = { threadId: params.threadId, providerId: thread.modelProvider, model: thread.model }
|
|
481
|
+
} catch {}
|
|
482
|
+
if (!expected || !switchReconciliations.track(message.id, expected)) {
|
|
483
|
+
resumeCalls.take(message.id)
|
|
484
|
+
await write(process.stdout, {
|
|
485
|
+
id: message.id,
|
|
486
|
+
error: {
|
|
487
|
+
code: -32000,
|
|
488
|
+
message: 'Provider switch status is uncertain. Reopen the original conversation and service.',
|
|
489
|
+
},
|
|
490
|
+
})
|
|
491
|
+
return
|
|
492
|
+
}
|
|
493
|
+
uncertainSwitches.set(params.threadId, expected)
|
|
494
|
+
}
|
|
495
|
+
}
|
|
432
496
|
if (
|
|
433
497
|
message?.method === 'turn/start' && typeof params?.threadId === 'string'
|
|
434
498
|
&& ['string', 'number'].includes(typeof message.id)
|
|
@@ -450,7 +514,11 @@ async function forward(line) {
|
|
|
450
514
|
) {
|
|
451
515
|
let prepared
|
|
452
516
|
try {
|
|
453
|
-
prepared = await prepareManagedResume(message, {
|
|
517
|
+
prepared = await prepareManagedResume(message, {
|
|
518
|
+
nativeCall,
|
|
519
|
+
controlCall,
|
|
520
|
+
allowConfigured: uncertainSwitches.has(params.threadId),
|
|
521
|
+
})
|
|
454
522
|
} catch {}
|
|
455
523
|
if (prepared) {
|
|
456
524
|
preparedResumes.set(message.id, { threadId: prepared.threadId, resumeToken: prepared.resumeToken })
|
|
@@ -459,6 +527,21 @@ async function forward(line) {
|
|
|
459
527
|
return
|
|
460
528
|
}
|
|
461
529
|
}
|
|
530
|
+
const reconciliation = uncertainSwitches.get(params?.threadId)
|
|
531
|
+
if (message?.method === 'thread/resume' && reconciliation && reconciliation.providerId !== 'openai') {
|
|
532
|
+
resumeCalls.take(message.id)
|
|
533
|
+
switchReconciliations.take(message.id)
|
|
534
|
+
lifecycleCalls.delete(message.id)
|
|
535
|
+
uncertainSwitches.set(params.threadId, undefined)
|
|
536
|
+
await write(process.stdout, {
|
|
537
|
+
id: message.id,
|
|
538
|
+
error: {
|
|
539
|
+
code: -32000,
|
|
540
|
+
message: 'Provider switch status is uncertain. Original service preparation was not admitted.',
|
|
541
|
+
},
|
|
542
|
+
})
|
|
543
|
+
return
|
|
544
|
+
}
|
|
462
545
|
if (['thread/start', 'thread/resume'].includes(message?.method) && params) {
|
|
463
546
|
try {
|
|
464
547
|
let providerId = params.modelProvider
|
|
@@ -724,6 +807,7 @@ readLines(nativeTransport.stdout, line => {
|
|
|
724
807
|
// Late private responses must never escape into the Desktop's request table.
|
|
725
808
|
if (typeof message?.id === 'string' && message.id.startsWith(privatePrefix)) return
|
|
726
809
|
if (message.method === undefined) lifecycleCalls.delete(message.id)
|
|
810
|
+
const reconciliation = message.method === undefined ? switchReconciliations.take(message.id) : undefined
|
|
727
811
|
const turnParams = message.method === undefined ? unmarkedTurns.take(message.id) : undefined
|
|
728
812
|
if (turnParams !== undefined) {
|
|
729
813
|
const providerId = nativeProviderFailure(message)
|
|
@@ -741,8 +825,14 @@ readLines(nativeTransport.stdout, line => {
|
|
|
741
825
|
preparedResumes.delete(message.id)
|
|
742
826
|
resumeCalls.take(message.id)
|
|
743
827
|
const succeeded = message.error === undefined && message.result?.thread?.id === preparedResume.threadId
|
|
828
|
+
&& (reconciliation === undefined
|
|
829
|
+
|| (acknowledgedSwitchResume(message, reconciliation)
|
|
830
|
+
&& uncertainSwitches.get(reconciliation.threadId) === reconciliation))
|
|
744
831
|
void controlCall('resume-complete', { ...preparedResume, succeeded })
|
|
745
|
-
.then(
|
|
832
|
+
.then(completed => {
|
|
833
|
+
if (completed === true) reconcileSwitch(message, reconciliation, true)
|
|
834
|
+
return write(process.stdout, line)
|
|
835
|
+
}).catch(stop)
|
|
746
836
|
return
|
|
747
837
|
}
|
|
748
838
|
const resumeParams = message.method === undefined ? resumeCalls.take(message?.id) : undefined
|
|
@@ -753,6 +843,7 @@ readLines(nativeTransport.stdout, line => {
|
|
|
753
843
|
return
|
|
754
844
|
}
|
|
755
845
|
}
|
|
846
|
+
reconcileSwitch(message, reconciliation)
|
|
756
847
|
const operation = desktopCalls.get(message?.id)
|
|
757
848
|
if (!operation || message.method !== undefined) {
|
|
758
849
|
void write(process.stdout, line).catch(stop)
|
|
@@ -780,6 +871,7 @@ readLines(nativeTransport.stdout, line => {
|
|
|
780
871
|
})
|
|
781
872
|
|
|
782
873
|
const switching = new Set()
|
|
874
|
+
const switchRequests = new Map()
|
|
783
875
|
const BRIDGE_NATIVE_METHODS = new Set([
|
|
784
876
|
'thread/list',
|
|
785
877
|
'thread/read',
|
|
@@ -798,6 +890,14 @@ readLines(control, line => {
|
|
|
798
890
|
return
|
|
799
891
|
}
|
|
800
892
|
if (settle(controlCalls, message, true)) return
|
|
893
|
+
if (
|
|
894
|
+
message.type === 'switch-cancel' && typeof message.switchId === 'string'
|
|
895
|
+
&& switchRequests.get(message.switchId) === message.threadId
|
|
896
|
+
) {
|
|
897
|
+
cancelledRecovery.add(message.threadId)
|
|
898
|
+
uncertainSwitches.set(message.threadId, undefined)
|
|
899
|
+
return
|
|
900
|
+
}
|
|
801
901
|
if (message.type === 'task-create' && typeof message.id === 'string') {
|
|
802
902
|
void nativeCall('mcpServer/tool/call', {
|
|
803
903
|
server: 'codex_app',
|
|
@@ -821,6 +921,13 @@ readLines(control, line => {
|
|
|
821
921
|
void write(control, { id: message.id, ok: false }).catch(stop)
|
|
822
922
|
return
|
|
823
923
|
}
|
|
924
|
+
if (uncertainSwitches.has(params.threadId) && message.method === 'turn/start') {
|
|
925
|
+
void write(control, { id: message.id, ok: false, reason: 'provider-switch-reconciliation-required' }).catch(stop)
|
|
926
|
+
return
|
|
927
|
+
}
|
|
928
|
+
if (
|
|
929
|
+
uncertainSwitches.has(params.threadId) && ['thread/resume', 'turn/interrupt'].includes(message.method)
|
|
930
|
+
) uncertainSwitches.set(params.threadId, undefined)
|
|
824
931
|
void nativeCall(message.method, params)
|
|
825
932
|
.then(value => write(control, { id: message.id, ok: true, value }))
|
|
826
933
|
.catch(() => write(control, { id: message.id, ok: false }).catch(stop))
|
|
@@ -846,34 +953,24 @@ readLines(control, line => {
|
|
|
846
953
|
return
|
|
847
954
|
}
|
|
848
955
|
switching.add(message.threadId)
|
|
956
|
+
switchRequests.set(message.id, message.threadId)
|
|
849
957
|
void (async () => {
|
|
850
958
|
try {
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
// Ephemeral side chats have no rollout to resume. Unsubscribe would destroy
|
|
859
|
-
// the only live session even when the following resume fails.
|
|
860
|
-
const current = await nativeCall('thread/read', { threadId: message.threadId, includeTurns: false })
|
|
861
|
-
if (current?.thread?.ephemeral === true) throw new Error('Ephemeral provider switching requires a new side chat')
|
|
862
|
-
await nativeCall('thread/unsubscribe', { threadId: message.threadId })
|
|
863
|
-
const result = await nativeCall('thread/resume', {
|
|
864
|
-
threadId: message.threadId,
|
|
865
|
-
model: message.model,
|
|
866
|
-
modelProvider: message.providerId,
|
|
867
|
-
config: message.config,
|
|
868
|
-
excludeTurns: true,
|
|
959
|
+
const result = await switchNativeThread(message, {
|
|
960
|
+
nativeCall,
|
|
961
|
+
controlCall,
|
|
962
|
+
current: () => !closed && !cancelledRecovery.has(message.threadId) && !lifecyclePending(message.threadId),
|
|
869
963
|
})
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
964
|
+
if (result.reason === 'provider-switch-reconciliation-required') {
|
|
965
|
+
uncertainSwitches.set(message.threadId, undefined)
|
|
966
|
+
}
|
|
967
|
+
await write(control, { id: message.id, ...result })
|
|
873
968
|
} catch {
|
|
874
969
|
await write(control, { id: message.id, ok: false })
|
|
875
970
|
} finally {
|
|
876
971
|
switching.delete(message.threadId)
|
|
972
|
+
switchRequests.delete(message.id)
|
|
973
|
+
cancelledRecovery.delete(message.threadId)
|
|
877
974
|
}
|
|
878
975
|
})().catch(stop)
|
|
879
976
|
}, maxControlLineBytes)
|
|
@@ -2,7 +2,7 @@ const object = value => value !== null && typeof value === 'object' && !Array.is
|
|
|
2
2
|
const validToken = value => typeof value === 'string' && /^[^\0\r\n]{16,256}$/u.test(value)
|
|
3
3
|
|
|
4
4
|
/** Refresh recognized persisted managed routes without changing the selected provider or inventing a Send. */
|
|
5
|
-
export async function prepareManagedResume(message, { nativeCall, controlCall }) {
|
|
5
|
+
export async function prepareManagedResume(message, { nativeCall, controlCall, allowConfigured = false }) {
|
|
6
6
|
const params = message.params
|
|
7
7
|
// Native path/history overrides can select history other than the named thread.
|
|
8
8
|
if (params.path != null || params.history != null) return undefined
|
|
@@ -15,7 +15,8 @@ export async function prepareManagedResume(message, { nativeCall, controlCall })
|
|
|
15
15
|
|| (params.modelProvider != null && params.modelProvider !== thread.modelProvider)
|
|
16
16
|
|| (params.config?.model_provider !== undefined && params.config.model_provider !== thread.modelProvider)
|
|
17
17
|
) return undefined
|
|
18
|
-
|
|
18
|
+
// Only an exact persisted-pair reconciliation uses ordinary configured-provider admission.
|
|
19
|
+
const prepared = await controlCall(allowConfigured ? 'resume' : 'managed-resume', {
|
|
19
20
|
threadId: params.threadId,
|
|
20
21
|
providerId: thread.modelProvider,
|
|
21
22
|
model: typeof params.model === 'string' ? params.model : thread.model,
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { nativeProviderFailure } from './native-provider-errors.mjs'
|
|
2
|
+
|
|
3
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : undefined
|
|
4
|
+
const valid = (value, max = 512) =>
|
|
5
|
+
typeof value === 'string' && value.length > 0 && value.length <= max
|
|
6
|
+
&& !/[\0\r\n]/u.test(value)
|
|
7
|
+
const acknowledged = (result, threadId, providerId, model) =>
|
|
8
|
+
result?.thread?.id === threadId
|
|
9
|
+
&& result.thread.status?.type === 'idle' && result.modelProvider === providerId && result.model === model
|
|
10
|
+
|
|
11
|
+
/** A rejected provider change restores only the exact previous native session, never a Send. */
|
|
12
|
+
export async function switchNativeThread(message, { nativeCall, controlCall, current }) {
|
|
13
|
+
const threadId = message.threadId
|
|
14
|
+
const before = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
15
|
+
if (message.requireIdle === true && (before?.thread?.id !== threadId || before.thread.status?.type !== 'idle')) {
|
|
16
|
+
return { ok: true, value: { status: 'rejected', reason: 'thread-not-idle' } }
|
|
17
|
+
}
|
|
18
|
+
const prior = before?.thread
|
|
19
|
+
if (prior?.ephemeral === true) return { ok: false, reason: 'provider-switch-unsupported', restored: true }
|
|
20
|
+
let unsubscribeAttempted = false
|
|
21
|
+
let unsubscribed = false
|
|
22
|
+
try {
|
|
23
|
+
if (!current()) throw new Error('Native switch cancelled')
|
|
24
|
+
unsubscribeAttempted = true
|
|
25
|
+
await nativeCall('thread/unsubscribe', { threadId })
|
|
26
|
+
unsubscribed = true
|
|
27
|
+
if (!current()) throw new Error('Native switch cancelled')
|
|
28
|
+
const result = await nativeCall('thread/resume', {
|
|
29
|
+
threadId,
|
|
30
|
+
model: message.model,
|
|
31
|
+
modelProvider: message.providerId,
|
|
32
|
+
config: message.config,
|
|
33
|
+
excludeTurns: true,
|
|
34
|
+
})
|
|
35
|
+
return current() && acknowledged(result, threadId, message.providerId, message.model)
|
|
36
|
+
? { ok: true }
|
|
37
|
+
: { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (!unsubscribed) {
|
|
40
|
+
return unsubscribeAttempted && !object(error.nativeError)
|
|
41
|
+
? { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
42
|
+
: { ok: false }
|
|
43
|
+
}
|
|
44
|
+
// A timeout or uncertain acknowledgement cannot authorize another lifecycle mutation.
|
|
45
|
+
if (!object(error.nativeError) || !current()) {
|
|
46
|
+
return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
47
|
+
}
|
|
48
|
+
const failureProvider = nativeProviderFailure({ error: error.nativeError })
|
|
49
|
+
const unsupported = prior?.modelProvider !== message.providerId
|
|
50
|
+
&& [prior?.modelProvider, message.providerId].includes(failureProvider)
|
|
51
|
+
&& failureProvider !== undefined
|
|
52
|
+
&& (error.nativeError.data?.type === 'modelProviderMismatch'
|
|
53
|
+
|| failureProvider === prior?.modelProvider)
|
|
54
|
+
let resumeToken
|
|
55
|
+
let completed = false
|
|
56
|
+
try {
|
|
57
|
+
if (
|
|
58
|
+
prior?.id !== threadId || !valid(prior.modelProvider, 128) || !valid(prior.model)
|
|
59
|
+
|| !['idle', 'notLoaded', 'systemError'].includes(prior.status?.type)
|
|
60
|
+
) throw new Error('Prior native session is unknown')
|
|
61
|
+
const read = await nativeCall('thread/read', { threadId, includeTurns: false })
|
|
62
|
+
if (
|
|
63
|
+
!current() || read?.thread?.id !== threadId || read.thread.modelProvider !== prior.modelProvider
|
|
64
|
+
|| read.thread.model !== prior.model || read.thread.status?.type !== 'notLoaded'
|
|
65
|
+
) throw new Error('Native session changed during switch')
|
|
66
|
+
const prepared = await controlCall('switch-resume', {
|
|
67
|
+
switchId: message.id,
|
|
68
|
+
threadId,
|
|
69
|
+
providerId: prior.modelProvider,
|
|
70
|
+
model: prior.model,
|
|
71
|
+
})
|
|
72
|
+
resumeToken = prepared?.resumeToken
|
|
73
|
+
if (
|
|
74
|
+
!current() || !['resume', 'native'].includes(prepared?.kind) || !object(prepared.configOverrides)
|
|
75
|
+
|| (prepared.kind === 'resume' && !valid(prepared.resumeToken, 256))
|
|
76
|
+
) throw new Error('Native restoration was not admitted')
|
|
77
|
+
const restored = await nativeCall('thread/resume', {
|
|
78
|
+
threadId,
|
|
79
|
+
modelProvider: prior.modelProvider,
|
|
80
|
+
model: prior.model,
|
|
81
|
+
config: prepared.configOverrides,
|
|
82
|
+
excludeTurns: true,
|
|
83
|
+
})
|
|
84
|
+
if (
|
|
85
|
+
!current() || !acknowledged(restored, threadId, prior.modelProvider, prior.model)
|
|
86
|
+
) throw new Error('Native restoration differed')
|
|
87
|
+
completed = await controlCall('switch-resume-complete', {
|
|
88
|
+
switchId: message.id,
|
|
89
|
+
threadId,
|
|
90
|
+
providerId: prior.modelProvider,
|
|
91
|
+
model: prior.model,
|
|
92
|
+
...(resumeToken === undefined ? {} : { resumeToken }),
|
|
93
|
+
succeeded: true,
|
|
94
|
+
}) === true
|
|
95
|
+
if (!completed || !current()) throw new Error('Native restoration completion was stale')
|
|
96
|
+
return { ok: false, ...(unsupported ? { reason: 'provider-switch-unsupported' } : {}), restored: true }
|
|
97
|
+
} catch {
|
|
98
|
+
return { ok: false, reason: 'provider-switch-reconciliation-required' }
|
|
99
|
+
} finally {
|
|
100
|
+
if (resumeToken !== undefined && !completed) {
|
|
101
|
+
await controlCall('switch-resume-complete', {
|
|
102
|
+
switchId: message.id,
|
|
103
|
+
threadId,
|
|
104
|
+
providerId: prior.modelProvider,
|
|
105
|
+
model: prior.model,
|
|
106
|
+
resumeToken,
|
|
107
|
+
succeeded: false,
|
|
108
|
+
}).catch(() => undefined)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native-submission-cdp-channel.d.ts","sourceRoot":"","sources":["../../../src/launcher/native-submission-cdp-channel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,wBAAwB,EACxB,uBAAuB,EACvB,yBAAyB,EAC1B,MAAM,gCAAgC,CAAA;AACvC,OAAO,KAAK,EAGV,4BAA4B,EAC5B,4BAA4B,EAC5B,kCAAkC,EAClC,sCAAsC,EACtC,4BAA4B,EAC5B,2BAA2B,EAC3B,sCAAsC,EACtC,4BAA4B,EAC5B,2BAA2B,EAC5B,MAAM,8BAA8B,CAAA;AAMrC,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,oCAAoC,CAAA;AACzF,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,KAAK,EACV,0BAA0B,EAC1B,gCAAgC,EAEhC,kCAAkC,EACnC,MAAM,mCAAmC,CAAA;AAO1C,OAAO,KAAK,EACV,uBAAuB,EACvB,wBAAwB,EACxB,uBAAuB,EACxB,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"native-submission-cdp-channel.d.ts","sourceRoot":"","sources":["../../../src/launcher/native-submission-cdp-channel.ts"],"names":[],"mappings":"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"}
|
|
@@ -57,6 +57,7 @@ const projection = (state) => ({
|
|
|
57
57
|
model: state.pending.model,
|
|
58
58
|
generation: state.pending.selectionGeneration,
|
|
59
59
|
...(state.pending.sendOnly ? { sendOnly: true } : {}),
|
|
60
|
+
...(state.pending.blockedReason ? { blockedReason: state.pending.blockedReason } : {}),
|
|
60
61
|
},
|
|
61
62
|
}),
|
|
62
63
|
...(state.draftPreference === undefined ? {} : { draftPreference: state.draftPreference }),
|
|
@@ -484,6 +485,10 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
484
485
|
if (!state || input?.expectedRevision !== state.revision || state.pending && !state.pending.sendOnly) {
|
|
485
486
|
throw new Error('Stale selection discard');
|
|
486
487
|
}
|
|
488
|
+
if (state.pending?.blockedReason) {
|
|
489
|
+
await respond(requestId, projection(state));
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
487
492
|
if (state.pending) {
|
|
488
493
|
delete state.pending;
|
|
489
494
|
state.revision++;
|
|
@@ -525,6 +530,9 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
525
530
|
}
|
|
526
531
|
if (!state || input?.expectedRevision !== state.revision)
|
|
527
532
|
throw new Error('Stale provider intent');
|
|
533
|
+
if (state.pending?.blockedReason === 'provider-switch-reconciliation-required') {
|
|
534
|
+
throw new Error('Native provider connection requires reconciliation');
|
|
535
|
+
}
|
|
528
536
|
if (envelope.operation === 'selectionProviderIntent') {
|
|
529
537
|
if (!text(input?.providerId, 128))
|
|
530
538
|
throw new Error('Invalid provider intent');
|
|
@@ -544,6 +552,10 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
544
552
|
else if (envelope.operation === 'selectionSelect') {
|
|
545
553
|
if (!state?.effective)
|
|
546
554
|
throw new Error('Native selection not initialized');
|
|
555
|
+
if (state.pending?.blockedReason === 'provider-switch-reconciliation-required') {
|
|
556
|
+
await respond(requestId, { status: 'reject', reason: state.pending.blockedReason, ...projection(state) });
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
547
559
|
const expectedRevision = state.revision;
|
|
548
560
|
if (input?.expectedRevision !== undefined && input.expectedRevision !== expectedRevision) {
|
|
549
561
|
throw new Error('Stale selection');
|
|
@@ -596,6 +608,11 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
596
608
|
if (stageSend && input?.expectedRevision !== state.revision) {
|
|
597
609
|
throw new Error('Concurrent next-send selection');
|
|
598
610
|
}
|
|
611
|
+
if (state.pending?.blockedReason && state.pending.providerId === selected.providerId
|
|
612
|
+
&& state.pending.model === selected.model) {
|
|
613
|
+
await respond(requestId, { status: 'reject', reason: state.pending.blockedReason, ...projection(state) });
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
599
616
|
delete state.draftPreference;
|
|
600
617
|
state.revision++;
|
|
601
618
|
if (!stageSend && selected.providerId === state.effective.providerId) {
|
|
@@ -620,6 +637,16 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
620
637
|
await respond(requestId, { status: 'accepted', ...committed.projection });
|
|
621
638
|
}
|
|
622
639
|
else {
|
|
640
|
+
if (committed.reason === 'provider-switch-unsupported'
|
|
641
|
+
|| committed.reason === 'provider-switch-reconciliation-required') {
|
|
642
|
+
if (state.revision !== revision || owner.disposed || !await runtime.revalidate(scope, true)
|
|
643
|
+
|| state.pending?.providerId !== selected.providerId || state.pending.model !== selected.model)
|
|
644
|
+
throw new Error('Stale native provider switch');
|
|
645
|
+
state.pending = { ...state.pending, blockedReason: committed.reason };
|
|
646
|
+
state.revision++;
|
|
647
|
+
await respond(requestId, { status: 'reject', reason: committed.reason, ...projection(state) });
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
623
650
|
if (state.pendingProviderIntent) {
|
|
624
651
|
throw new Error('Provider intent requires successful model acknowledgement');
|
|
625
652
|
}
|
|
@@ -646,25 +673,51 @@ export function createNativeSubmissionCdpAuthority(options) {
|
|
|
646
673
|
}
|
|
647
674
|
}
|
|
648
675
|
else if (envelope.operation === 'submissionPrepare') {
|
|
676
|
+
const beforePending = state?.pending;
|
|
649
677
|
const result = await controller.prepareSubmission(scope, actionOf(input?.action));
|
|
678
|
+
let rejectedProjection;
|
|
679
|
+
if (result.kind === 'reject' && state?.pending && state.pending === beforePending
|
|
680
|
+
&& (result.reason === 'provider-switch-unsupported'
|
|
681
|
+
|| result.reason === 'provider-switch-reconciliation-required')) {
|
|
682
|
+
if (state.pending.blockedReason !== result.reason) {
|
|
683
|
+
state.pending = { ...state.pending, blockedReason: result.reason };
|
|
684
|
+
state.revision++;
|
|
685
|
+
}
|
|
686
|
+
rejectedProjection = projection(state);
|
|
687
|
+
}
|
|
650
688
|
await respond(requestId, result.kind === 'confirm'
|
|
651
689
|
? {
|
|
652
690
|
status: result.kind,
|
|
653
691
|
confirmationId: result.confirmationId,
|
|
654
692
|
expectedSelectionRevision: result.selectionRevision,
|
|
655
693
|
}
|
|
656
|
-
: { ...result, status: result.kind });
|
|
694
|
+
: { ...result, status: result.kind, ...(rejectedProjection ? { projection: rejectedProjection } : {}) });
|
|
657
695
|
}
|
|
658
696
|
else if (envelope.operation === 'submissionConfirm') {
|
|
659
697
|
if (!text(input?.confirmationId, 256) || !Number.isSafeInteger(input?.expectedSelectionRevision)) {
|
|
660
698
|
throw new Error('Invalid confirmation');
|
|
661
699
|
}
|
|
700
|
+
const beforePending = state?.pending;
|
|
662
701
|
const result = await controller.confirmSubmission({
|
|
663
702
|
scope,
|
|
664
703
|
confirmationId: input.confirmationId,
|
|
665
704
|
expectedSelectionRevision: input.expectedSelectionRevision,
|
|
666
705
|
});
|
|
667
|
-
|
|
706
|
+
let rejectedProjection;
|
|
707
|
+
if (result.kind === 'reject' && state?.pending && state.pending === beforePending
|
|
708
|
+
&& (result.reason === 'provider-switch-unsupported'
|
|
709
|
+
|| result.reason === 'provider-switch-reconciliation-required')) {
|
|
710
|
+
if (state.pending.blockedReason !== result.reason) {
|
|
711
|
+
state.pending = { ...state.pending, blockedReason: result.reason };
|
|
712
|
+
state.revision++;
|
|
713
|
+
}
|
|
714
|
+
rejectedProjection = projection(state);
|
|
715
|
+
}
|
|
716
|
+
await respond(requestId, {
|
|
717
|
+
...result,
|
|
718
|
+
status: result.kind,
|
|
719
|
+
...(rejectedProjection ? { projection: rejectedProjection } : {}),
|
|
720
|
+
});
|
|
668
721
|
}
|
|
669
722
|
else if (envelope.operation === 'submissionCancel') {
|
|
670
723
|
if (!text(input?.id, 256))
|