@@ -32,6 +32,7 @@ import {
3232import {
3333 markJournalInjectedState ,
3434 journaledInjectedOpenaiBaseUrl ,
35+ journaledInjectedRealtimeWsBaseUrl ,
3536 journaledInjectedCatalogPath ,
3637 removeJournal ,
3738 restoreJournalState ,
@@ -49,9 +50,11 @@ import {
4950} from "./history-job" ;
5051import {
5152 OCX_SECTION_MARKER ,
53+ REALTIME_WS_BASE_URL_KEY ,
5254 hasInjectedCodexRouting ,
5355 hasInjectedOpenaiBaseUrl ,
5456 isRootOpenaiBaseUrlLine ,
57+ isRootRealtimeWsBaseUrlLine ,
5558 providerTableStart ,
5659 providerTableString ,
5760 rootTomlString ,
@@ -346,6 +349,20 @@ function buildOpenaiBaseUrlLineForTarget(target: CodexRoutingTarget): string {
346349 return `openai_base_url = ${ tomlString ( target . baseUrl ) } ` ;
347350}
348351
352+ /**
353+ * Realtime sideband override (codex-rs `experimental_realtime_ws_base_url`), written with the
354+ * SAME value as `openai_base_url`. Desktop voice creates its WebRTC call through the proxy
355+ * (`POST /v1/live`, answered under the Pool account the proxy selects) but, since openai/codex
356+ * 438c9e98d (#35830), joins the sideband at `wss://api.openai.com/v1/live/{callId}` with the
357+ * app's own login unless this key redirects it. Two accounts, one call: the join 404s. Pointing
358+ * the key at the proxy sends the join through `GET /v1/live/{callId}` (src/server/live.ts),
359+ * where the same Pool account is reused. codex-rs turns `http` into `ws` and appends
360+ * `/live/{callId}` itself; the value must stay the canonical `/v1` root.
361+ */
362+ export function buildRealtimeWsBaseUrlLine ( target : CodexRoutingTarget ) : string {
363+ return `${ REALTIME_WS_BASE_URL_KEY } = ${ tomlString ( target . baseUrl ) } ` ;
364+ }
365+
349366/**
350367 * Design B root-key injection: place `OCX_SECTION_MARKER` + `openai_base_url` at the document
351368 * ROOT (before the first table header). Idempotent: an existing marker-owned line is rewritten
@@ -427,10 +444,56 @@ function setRootOpenaiBaseUrlForTarget(
427444 return { content : lines . join ( "\n" ) , keptUserBaseUrl : false } ;
428445}
429446
447+ /**
448+ * True when line `index` is inside the marker-owned root block: the line right after the
449+ * marker, or the line right after a marker-owned `openai_base_url`. One marker owns both
450+ * routing keys so the "exactly one marker" invariant the rest of injection relies on holds.
451+ */
452+ function markerOwnedRootLine ( lines : string [ ] , index : number ) : boolean {
453+ if ( index > 0 && lines [ index - 1 ] . includes ( OCX_SECTION_MARKER ) ) return true ;
454+ return index > 1
455+ && isRootOpenaiBaseUrlLine ( lines [ index - 1 ] )
456+ && lines [ index - 2 ] . includes ( OCX_SECTION_MARKER ) ;
457+ }
458+
459+ /**
460+ * Companion to `setRootOpenaiBaseUrlForTarget` for the realtime sideband override. Same
461+ * ownership rule: a marker-owned line is rewritten in place, a user's own line (not in the
462+ * marker block) is kept and nothing is injected. Placement: directly after the marker-owned
463+ * `openai_base_url` so the marker owns both keys as one block. Only ever called on the
464+ * Design B (loopback) path right after the routing override was written — the legacy
465+ * provider-table form needs the admission-token header, which the sideband cannot carry.
466+ */
467+ export function setRootRealtimeWsBaseUrl (
468+ content : string ,
469+ target : CodexRoutingTarget ,
470+ ) : { content : string ; keptUserRealtimeWsBaseUrl : boolean } {
471+ const lines = content . split ( "\n" ) ;
472+ const firstTable = lines . findIndex ( ( line ) => / ^ \s * \[ / . test ( line ) ) ;
473+ const rootEnd = firstTable === - 1 ? lines . length : firstTable ;
474+ const key = buildRealtimeWsBaseUrlLine ( validateCodexRoutingTarget ( target ) ) ;
475+ for ( let index = 0 ; index < rootEnd ; index += 1 ) {
476+ if ( ! isRootRealtimeWsBaseUrlLine ( lines [ index ] ) ) continue ;
477+ if ( ! markerOwnedRootLine ( lines , index ) ) return { content, keptUserRealtimeWsBaseUrl : true } ;
478+ lines [ index ] = key ;
479+ return { content : lines . join ( "\n" ) , keptUserRealtimeWsBaseUrl : false } ;
480+ }
481+ for ( let index = 0 ; index < rootEnd ; index += 1 ) {
482+ if ( ! isRootOpenaiBaseUrlLine ( lines [ index ] ) ) continue ;
483+ if ( ! ( index > 0 && lines [ index - 1 ] . includes ( OCX_SECTION_MARKER ) ) ) continue ;
484+ lines . splice ( index + 1 , 0 , key ) ;
485+ return { content : lines . join ( "\n" ) , keptUserRealtimeWsBaseUrl : false } ;
486+ }
487+ // No marker-owned routing override to attach to: the override has no owner, so inject nothing.
488+ return { content, keptUserRealtimeWsBaseUrl : false } ;
489+ }
490+
430491/**
431492 * Remove the marker-owned root `openai_base_url` (marker line + the key line right after it).
432493 * A user's own root override (no marker) survives; an orphaned marker with no key line after
433494 * it is dropped too so repeated strip/inject cycles cannot accumulate marker comments.
495+ * The `experimental_realtime_ws_base_url` companion directly under that pair is part of the
496+ * same owned block and goes with it.
434497 */
435498export function stripInjectedOpenaiBaseUrl ( content : string ) : string {
436499 const lines = content . split ( "\n" ) ;
@@ -442,6 +505,7 @@ export function stripInjectedOpenaiBaseUrl(content: string): string {
442505 if ( i + 1 < rootEnd && isRootOpenaiBaseUrlLine ( lines [ i + 1 ] ) ) {
443506 drop . add ( i ) ;
444507 drop . add ( i + 1 ) ;
508+ if ( i + 2 < rootEnd && isRootRealtimeWsBaseUrlLine ( lines [ i + 2 ] ) ) drop . add ( i + 2 ) ;
445509 } else if ( i + 1 >= rootEnd || lines [ i + 1 ] . trim ( ) === "" ) {
446510 drop . add ( i ) ; // orphaned marker at root
447511 }
@@ -959,6 +1023,7 @@ export async function injectCodexConfig(
9591023 // Provider-table form: non-loopback admission (legacy) or the authless Desktop opt-in (#1107).
9601024 const legacyMode = usesProviderTable ( routingTarget ) ;
9611025 let keptUserBaseUrl = false ;
1026+ let keptUserRealtimeWsBaseUrl = false ;
9621027 if ( legacyMode ) {
9631028 // Legacy (non-loopback) injection: the built-in openai provider cannot carry the
9641029 // x-opencodex-api-key env header, so keep the opencodex provider table + root re-tag.
@@ -975,9 +1040,24 @@ export async function injectCodexConfig(
9751040 // Design B (loopback): a single root override; codex keeps its native `openai` provider id
9761041 // so thread history is never remapped. Any legacy form was already stripped above.
9771042 content = stripInjectedOpenaiBaseUrl ( content ) ; // normalize before idempotent re-insert
1043+ // #1798: after a Codex app rewrite the marker is gone but the values we recorded writing
1044+ // are still ours. Consume them by value so re-inject does not mistake our own routing
1045+ // for a user override (and then journal it as not-ours, making it unrestorable).
1046+ content = stripJournaledOpenaiBaseUrl (
1047+ content ,
1048+ journaledInjectedOpenaiBaseUrl ( ) ,
1049+ journaledInjectedRealtimeWsBaseUrl ( ) ,
1050+ ) ;
9781051 const result = setRootOpenaiBaseUrlForTarget ( content , routingTarget ) ;
9791052 content = result . content ;
9801053 keptUserBaseUrl = result . keptUserBaseUrl ;
1054+ // Voice sideband override rides on the routing override: same value, same ownership rule,
1055+ // and never when the user owns the routing line (we inject nothing in that case).
1056+ if ( ! keptUserBaseUrl ) {
1057+ const realtime = setRootRealtimeWsBaseUrl ( content , routingTarget ) ;
1058+ content = realtime . content ;
1059+ keptUserRealtimeWsBaseUrl = realtime . keptUserRealtimeWsBaseUrl ;
1060+ }
9811061 }
9821062
9831063 const desiredSubagentDefaults = configuredManagedSubagentDefaults ( config ) ;
@@ -1091,8 +1171,18 @@ export async function injectCodexConfig(
10911171 }
10921172
10931173 const applyNativeArtifacts = ( ) : void => {
1174+ // #1798 again: a Codex app rewrite keeps values and drops the ownership comments, so
1175+ // marker evidence alone would classify our own routed config as the user's native
1176+ // baseline and replace the real original snapshot. Value evidence from the journal
1177+ // (the URLs the last injection recorded writing) blocks that misclassification.
1178+ const journaledBaseUrl = journaledInjectedOpenaiBaseUrl ( ) ;
1179+ const journaledRealtimeWsBaseUrl = journaledInjectedRealtimeWsBaseUrl ( ) ;
1180+ const looksInjectedByValue =
1181+ ( journaledBaseUrl !== null && rootTomlString ( rawContent , "openai_base_url" ) === journaledBaseUrl )
1182+ || ( journaledRealtimeWsBaseUrl !== null
1183+ && rootTomlString ( rawContent , REALTIME_WS_BASE_URL_KEY ) === journaledRealtimeWsBaseUrl ) ;
10941184 writeJournal ( {
1095- currentStateIsNative : ! hasInjectedCodexRouting ( rawContent ) ,
1185+ currentStateIsNative : ! hasInjectedCodexRouting ( rawContent ) && ! looksInjectedByValue ,
10961186 configContent : baselineContent ,
10971187 owner : options . journalOwner ,
10981188 } ) ;
@@ -1103,6 +1193,11 @@ export async function injectCodexConfig(
11031193 injectedOpenaiBaseUrl : legacyMode || keptUserBaseUrl
11041194 ? null
11051195 : rootTomlString ( content , "openai_base_url" ) ,
1196+ // The sideband override is ours only when we wrote it this pass (never in legacy mode,
1197+ // never when the user owns either key).
1198+ injectedRealtimeWsBaseUrl : legacyMode || keptUserBaseUrl || keptUserRealtimeWsBaseUrl
1199+ ? null
1200+ : rootTomlString ( content , REALTIME_WS_BASE_URL_KEY ) ,
11061201 // This is the catalog artifact selected for this injection, even when config.toml
11071202 // already points at that path and therefore needs no textual rewrite.
11081203 injectedCatalogPath : catalogPath ,
@@ -1301,7 +1396,7 @@ export async function injectCodexConfig(
13011396 ? `Injected opencodex as default provider into Codex config (authless Desktop mode: requires_openai_auth = false).\n`
13021397 : legacyMode
13031398 ? `Injected opencodex as default provider into Codex config.\n`
1304- : `Pointed Codex's built-in openai provider at the opencodex proxy (openai_base_url).\n` ;
1399+ : `Pointed Codex's built-in openai provider at the opencodex proxy (openai_base_url + realtime sideband override ).\n` ;
13051400 return {
13061401 success : true ,
13071402 ...( nativeSubagentDefaultsWarning ? { nativeSubagentDefaultsWarning } : { } ) ,
@@ -1388,6 +1483,7 @@ interface StripOpencodexConfigResult {
13881483function stripOpencodexConfigResult (
13891484 content : string ,
13901485 journaledBaseUrl : string | null = null ,
1486+ journaledRealtimeWsBaseUrl : string | null = null ,
13911487) : StripOpencodexConfigResult {
13921488 let out = content ;
13931489 const hadRootOcxProvider =
@@ -1398,7 +1494,7 @@ function stripOpencodexConfigResult(
13981494 const hadInjectedBaseUrl = hasInjectedOpenaiBaseUrl ( out )
13991495 || ( journaledBaseUrl !== null && rootTomlString ( out , "openai_base_url" ) === journaledBaseUrl ) ;
14001496 out = stripInjectedOpenaiBaseUrl ( out ) ; // before removeOcxSection — it keys on the marker line too
1401- out = stripJournaledOpenaiBaseUrl ( out , journaledBaseUrl ) ;
1497+ out = stripJournaledOpenaiBaseUrl ( out , journaledBaseUrl , journaledRealtimeWsBaseUrl ) ;
14021498 if ( hasOcxProviderTable ( out ) ) {
14031499 out = removeOcxSection ( out ) ;
14041500 }
@@ -1453,9 +1549,12 @@ export function removeCodexConfig(
14531549 // Read the recorded injection once: the strip below consumes it, and so does the
14541550 // ownership verdict, which must agree with what was actually removed.
14551551 const journaledBaseUrl = journaledInjectedOpenaiBaseUrl ( ) ;
1552+ const journaledRealtimeWsBaseUrl = journaledInjectedRealtimeWsBaseUrl ( ) ;
14561553 const had = hasOpencodexRouting ( content )
1457- || ( journaledBaseUrl !== null && rootTomlString ( content , "openai_base_url" ) === journaledBaseUrl ) ;
1458- const stripped = stripOpencodexConfigResult ( content , journaledBaseUrl ) ;
1554+ || ( journaledBaseUrl !== null && rootTomlString ( content , "openai_base_url" ) === journaledBaseUrl )
1555+ || ( journaledRealtimeWsBaseUrl !== null
1556+ && rootTomlString ( content , REALTIME_WS_BASE_URL_KEY ) === journaledRealtimeWsBaseUrl ) ;
1557+ const stripped = stripOpencodexConfigResult ( content , journaledBaseUrl , journaledRealtimeWsBaseUrl ) ;
14591558 if ( had || stripped . content !== content ) {
14601559 atomicWriteFile ( CODEX_CONFIG_PATH , applyEol ( stripped . content , eol ) ) ;
14611560 }
0 commit comments