grix-connector 4.16.1 → 4.17.0
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/adapter/acp/acp-adapter.js +8 -8
- package/dist/adapter/claude/claude-bridge-server.js +1 -1
- package/dist/adapter/claude/claude-tools.js +1 -1
- package/dist/adapter/claude/claude-worker-client.js +1 -1
- package/dist/adapter/claude/mcp-http-launcher.js +2 -2
- package/dist/adapter/claude/result-timeout.js +1 -1
- package/dist/adapter/codex/codex-bridge.js +10 -10
- package/dist/adapter/codex/rollout-import.js +1 -0
- package/dist/adapter/codex/thread-archive.js +6 -0
- package/dist/adapter/codex/thread-resume.js +1 -0
- package/dist/adapter/deepseek-harness/jsonrpc-adapter.js +3 -3
- package/dist/adapter/deepseek-harness/prompt-builder.js +4 -4
- package/dist/adapter/deepseek-harness/workspace-attachments.js +3 -0
- package/dist/assets/dsh-bridge/{grix-dsh-bridge-4.16.1.tgz → grix-dsh-bridge-4.17.0.tgz} +0 -0
- package/dist/assets/dsh-bridge/manifest.json +4 -4
- package/dist/bridge/bridge.js +2 -2
- package/dist/bridge/send-controller.js +3 -3
- package/dist/bridge/session-list.js +2 -2
- package/dist/core/access/allowlist-store.js +1 -1
- package/dist/core/aibot/client.js +2 -2
- package/dist/core/file-ops/list-files.js +1 -1
- package/dist/kimi-plugins/index.js +2 -2
- package/dist/log.js +2 -2
- package/dist/manager.js +1 -1
- package/dist/mcp/stream-http/config.js +1 -1
- package/dist/mcp/stream-http/connection-binding.js +1 -1
- package/dist/mcp/stream-http/tool-executor.js +1 -1
- package/dist/mcp/stream-http/tool-registry.js +1 -1
- package/dist/mcp/stream-http/tool-schemas.js +1 -1
- package/package.json +1 -1
- package/dist/kimi-plugins/grix-layer/SKILL.md +0 -68
- package/dist/kimi-plugins/grix-layer/SYSTEM.md +0 -16
- package/dist/kimi-plugins/grix-layer/kimi.plugin.json +0 -10
- package/dist/kimi-plugins/grix-layer/references/gateway-architecture.md +0 -157
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import{execFileSync as
|
|
2
|
-
\u2026(truncated)`,le=80,ue=120*1e3,B=600*1e3,rt=60*1e3,ot=300*1e3,at=q("GRIX_CODEX_DELTA_COALESCE_MS",1e3),dt=q("GRIX_CODEX_DELTA_COALESCE_CHARS",4096),ct=[{id:"gpt-5.3-codex",displayName:"GPT-5.3 Codex",defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!0}],he=[{id:"default",displayName:"Default"},{id:"plan",displayName:"Plan"}],lt=new Set(["rawResponse/completed","codex/rawResponse/completed","rawResponseItem/completed","codex/rawResponseItem/completed","thread/tokenUsage/updated","codex/thread/tokenUsage/updated","thread/compacted","codex/thread/compacted","turn/started","codex/turn/started","turn/completed","codex/turn/completed","item/started","codex/item/started","item/completed","codex/item/completed","error","codex/error"]);async function pe(a,e){const t=String(a??"").trim()||"codex",i=Ae(t,e);if(i!==t)return i;if(t==="codex"){const n=process.platform==="win32"?[]:["/opt/homebrew/bin/codex","/usr/local/bin/codex","/usr/bin/codex"],r=process.platform!=="win32"?[]:[P(process.env.LOCALAPPDATA??"","npm","codex.cmd"),P(process.env.APPDATA??"","npm","codex.cmd"),P(process.env.LOCALAPPDATA??"","Programs","codex","codex.exe")];for(const o of[...n,...r])try{return require("node:fs").accessSync(o,require("node:fs").constants.X_OK),o}catch{continue}}const s=await ne(t);return s||(c.warn("codex-adapter",`resolveCodexCommandPath: failed to resolve "${t}" via PATH, codex fallbacks, or resolveCliPath. envPath=${e?"provided":"missing (using process.env.PATH)"}`),t)}class O extends ee{type="codex";config;callbacks;process=null;alive=!1;stopped=!1;internalApi=null;bridgeStatus="starting";pendingRequests=new Map;requestId=0;threadId=null;initialized=!1;cwd;approvalPolicy;sandboxMode;model;collaborationMode;reasoningEffort;serviceTier;idleTimer=null;waitLoopTimer=null;waitLoopCheckRunning=!1;waitLoopTracker=new ie;waitLoopProgressGeneration=0;waitLoopChildTools=new Set;rejectedTurnStarts=new Set;startingEventId=null;rejectedNativeOwner=null;rejectedNativeTimer=null;orphanTurnTimer=null;orphanTurnSince=0;lastProgressAt=0;lastIdleCheckAt=0;activityGeneration=0;lastIdleCheckGeneration=0;idleNoProgressCount=0;inFlightToolOps=0;consecutivePingFailures=0;currentTurnStartedAt=0;preflightCheckComplete=!1;turnInactivityTimeoutMs=tt("GRIX_CODEX_TURN_INACTIVITY_TIMEOUT_MS");pendingApprovals=new Map;needsHistoryInjection=!1;lastInjectedSessionId=null;identityInputInjectedThreadId=null;protocolInstructionInputInjectedThreadId=null;legacyDeveloperInstructionsUnsupported=!1;profileIdentityRefreshPending=!1;bindingStore=null;aibotSessionId="";threadResumePending=!1;autoTrustProject="auto";codexHome;codexDirect=null;directEnv={};configOverrideArgs=[];rawCaptureOptions;rawCaptureRelay;rawCaptureConfigOverrides=[];rawCaptureUnavailableReason;codexModelOptions=[];codexModelOptionsCachedAt=0;static MODEL_CACHE_TTL_MS=3600*1e3;rateLimitSnapshot=null;lastNonDefaultLimitRefreshAt=0;static NON_DEFAULT_LIMIT_REFRESH_INTERVAL_MS=6e4;currentThreadTokenUsage=null;currentModelContextWindow=null;auditCaptureByEventId=new Map;constructor(e,t){super(),this.config=e,this.callbacks=t;const i=e.options??{};this.approvalPolicy=i.approvalPolicy??"never",this.sandboxMode=typeof i.sandboxMode=="string"&&i.sandboxMode.trim()&&i.sandboxMode.trim()!=="default"?i.sandboxMode.trim():void 0,this.aibotSessionId=String(i.aibotSessionId??"").trim(),this.bindingStore=i.bindingStore instanceof De?i.bindingStore:null,this.model=i.model,this.collaborationMode=i.collaborationMode,this.reasoningEffort=i.reasoningEffort,this.serviceTier=i.serviceTier,this.bindingStore&&this.aibotSessionId&&(this.model=this.model??this.bindingStore.getCodexModelId(this.aibotSessionId),this.collaborationMode=this.collaborationMode??this.bindingStore.getCodexModeId(this.aibotSessionId),this.reasoningEffort=this.reasoningEffort??this.bindingStore.getCodexReasoningEffort(this.aibotSessionId),this.serviceTier=this.serviceTier??this.bindingStore.getCodexServiceTier(this.aibotSessionId),this.sandboxMode=this.sandboxMode??this.bindingStore.getCodexSandboxMode(this.aibotSessionId)),this.autoTrustProject=typeof i.autoTrustProject=="boolean"?i.autoTrustProject:"auto",this.codexHome=typeof i.codexHome=="string"&&i.codexHome.trim()?i.codexHome.trim():void 0;const s=i.codexDirect;if(this.codexDirect=s&&typeof s=="object"&&!Array.isArray(s)?s:null,this.configOverrideArgs=re(i.codexConfigOverrides,"codexConfigOverrides"),this.rawCaptureOptions=it(i.rawApiCapture),this.cwd=this.resolveCwd(),this.bindingStore&&this.aibotSessionId){const n=this.bindingStore.getCodexThreadId(this.aibotSessionId);n&&(this.threadId=n,this.threadResumePending=!0)}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}return process.cwd()}async start(){await this.prepareDirectHome(),await this.ensureProjectTrusted(),await this.prepareRawApiCapture();try{await this.startInternalApiAndRegisterMcp(),await this.spawnCodex(),await this.initializeHandshake(),await this.notifyBindingReadyWithContext(),c.info("codex-adapter",`Ready (pid=${this.process?.pid})`)}catch(e){throw await this.stopRawApiCapture(),e}}async prepareRawApiCapture(){if(this.rawCaptureOptions)try{const e=await ze(this.rawCaptureOptions,{adapterType:"codex",sessionId:this.rawCaptureOptions.sessionId,cwd:this.cwd,env:this.resolveChildEnv(),configOverrides:this.configOverrideArgs,codexHome:this.codexHome});this.rawCaptureRelay=e.relay,this.rawCaptureConfigOverrides=e.overlay.configOverrides??[],this.rawCaptureUnavailableReason=void 0,c.info("codex-adapter","Raw API audit capture relay is ready")}catch(e){this.rawCaptureRelay=void 0,this.rawCaptureConfigOverrides=[],this.rawCaptureUnavailableReason=e instanceof Error?e.message:String(e),c.warn("codex-adapter",`Raw API audit capture unavailable: ${this.rawCaptureUnavailableReason}`)}}async stopRawApiCapture(){const e=this.rawCaptureRelay;this.rawCaptureRelay=void 0,this.rawCaptureConfigOverrides=[],e&&await e.stop()}async prepareDirectHome(){if(!this.codexDirect)return;const e=await $e(this.codexDirect);this.codexHome=e.codexHome,this.directEnv=e.env;const t=re(e.configOverrides,"codexDirect.configOverrides");this.configOverrideArgs=[...this.configOverrideArgs,...t],c.info("codex-adapter",`Codex direct home ready: ${e.codexHome} (catalog=${e.catalogVersion??"none"}, token=${e.tokenDigest?`present sha256:${e.tokenDigest}`:"absent"})`)}resolveChildEnv(){const e={...process.env,...this.config.env};return this.codexHome&&(e.CODEX_HOME=this.codexHome),e}async ensureProjectTrusted(){if(this.shouldAutoTrustProject())try{const e=await Oe(this.cwd,{codexHome:this.codexHome});e.changed&&c.info("codex-adapter",`Trusted Codex project ${this.cwd} in ${e.configPath}`)}catch(e){const t=e instanceof Error?e.message:String(e);throw c.error("codex-adapter",`Failed to trust Codex project ${this.cwd}: ${t}`),e}}async startInternalApiAndRegisterMcp(){try{this.internalApi=new Re,this.internalApi.setInvokeHandler(async(d,h,f,p)=>this.callbacks.agentInvoke(d,h,p)),await this.internalApi.start(0),c.info("codex-adapter",`Internal API started at ${this.internalApi.url}`);const e=this.getMcpConfig(),t=this.resolveChildEnv(),i=typeof t.PATH=="string"?t.PATH:void 0,s=await pe(this.config.command||"codex",i),n=d=>{const h=Se(s,d);Te(h.command,h.args,{env:t,timeout:1e4,stdio:"ignore",windowsHide:!0,windowsVerbatimArguments:h.windowsVerbatimArguments})};for(const d of[...Me,e.name])try{n(["mcp","remove",d])}catch{}n(["mcp","add",e.name,"--",e.command,...e.args]),c.info("codex-adapter",`Registered MCP server: ${e.name}`);const r=P(Le(this.codexHome),"skills"),o=_e(r);o.length>0&&c.info("codex-adapter",`Synced connector skills to ${r}: [${o.join(", ")}]`)}catch(e){c.warn("codex-adapter",`Failed to register MCP tools (non-fatal): ${e instanceof Error?e.message:String(e)}`)}}shouldAutoTrustProject(){return this.autoTrustProject===!1?!1:this.autoTrustProject===!0?!0:Ne(this.config.command)}notifyBindingReady(){!this.aibotSessionId||!this.cwd||this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd)}async notifyBindingReadyWithContext(){if(!(!this.aibotSessionId||!this.cwd))try{this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd,await this.buildToolbarContextResult("binding_ready"))}catch(e){c.warn("codex-adapter",`Failed to attach toolbar context to binding update: ${e instanceof Error?e.message:String(e)}`),this.notifyBindingReady()}}scheduleToolbarContextPush(){this.notifyBindingReadyWithContext()}externalProviderCheckedAt=0;externalProviderCached=!1;static EXTERNAL_PROVIDER_CACHE_TTL_MS=60*1e3;isExternalProvider(){const e=Date.now();if(e-this.externalProviderCheckedAt<O.EXTERNAL_PROVIDER_CACHE_TTL_MS)return this.externalProviderCached;const t=U(this.codexHome);return this.externalProviderCached=!!(t.baseUrl&&ge(t.baseUrl)),this.externalProviderCheckedAt=e,this.externalProviderCached}effectiveServiceTier(){if(!this.serviceTier||this.serviceTier.trim().toLowerCase()==="default"||this.isExternalProvider())return;const e=this.getModelOption(this.currentModelId());if(!(e&&!e.serviceTiers.some(t=>t.id===this.serviceTier)))return this.serviceTier}resolveReasoningEffortForModel(e,t,i={}){const s=String(e??"").trim();if(!s||this.isExternalProvider())return;const n=this.getModelOption(t),r=n?.supportedReasoningEfforts??[];if(r.length===0)return;const o=r.find(d=>d.trim().toLowerCase()===s.toLowerCase());if(o)return o;if(!i.strict&&["max","ultra"].includes(s.toLowerCase())){const d=r.find(h=>h.trim().toLowerCase()==="xhigh");if(d)return d}return i.strict?void 0:n?.defaultReasoningEffort??void 0}effectiveReasoningEffort(){return this.resolveReasoningEffortForModel(this.reasoningEffort,this.currentModelId())}getEffortMeta(){if(this.isExternalProvider())return{};const e=this.currentModelId(),t=this.getModelOption(e),i={},s=t?.supportedReasoningEfforts??[];s.length>0&&(i.available_efforts=s,i.reasoning_effort=this.effectiveReasoningEffort()??t?.defaultReasoningEffort??null);const n=t?.serviceTiers??[];return n.length>0&&(i.available_service_tiers=n,i.service_tier=this.effectiveServiceTier()??t?.defaultServiceTier??null),i}async stop(){this.stopped=!0,this.alive=!1,this.stopComposing(),this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.stopOrphanTurnWatch(),this.currentTurnId=null,this.finishActiveInputs("canceled","adapter stopped"),this.rejectAllPending("adapter stopped"),this.pendingAutoCompact=!1,this.clearNativeSettlement("adapter stopped"),this.finishCompaction({status:"failed",message:"adapter stopped"});for(const t of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed","adapter stopped");this.internalApi&&(await this.internalApi.stop(),this.internalApi=null);const e=this.process;if(this.process=null,e?.pid&&typeof e.once=="function")$(e,"SIGTERM"),await Promise.race([new Promise(i=>{e.once("exit",()=>i(!0))}),new Promise(i=>{setTimeout(()=>i(!1),5e3)})])||$(e,"SIGKILL");else if(e)try{e.kill("SIGTERM")}catch{}await this.stopRawApiCapture()}isAlive(){return this.alive}async createSession(e){if(this.initialized||await this.initializeHandshake(),this.threadId||await this.startNewThread(),!this.threadId)throw new Error("Failed to create session: thread ID is missing");return await this.notifyBindingReadyWithContext(),this.threadId}async resumeSession(e,t){await this.ensureThreadResumed()}async destroySession(e){this.threadId=null,this.threadResumePending=!1,this.persistThreadId(void 0),this.protocolInstructionInputInjectedThreadId=null}sendPrompt(e){const t=new X(e.adapterSessionId);return this.runTurn(e,t).catch(i=>{t.emitError(i instanceof Error?i:new Error(String(i)))}),t}async cancel(e){if(this.compacting&&this.finishCompaction({status:"failed",message:"Compaction interrupted"},!1),this.threadId)try{await this.sendRequest("turn/interrupt",{threadId:this.threadId,turnId:this.currentTurnId??""},5e3)}catch{}}setPermissionHandler(e){this.permissionHandler=e}permissionHandler=null;async ping(e){if(!this.alive||!this.process)return!1;if(!this.process.pid){const t=!!(this.process.stdin&&!this.process.stdin.destroyed),i=!!(this.process.stdout&&!this.process.stdout.destroyed);return t&&i}if(!this.process.stdin||this.process.stdin.destroyed)return!!(this.process.stdout&&!this.process.stdout.destroyed);try{const t=++this.requestId,i={jsonrpc:"2.0",id:t,method:"ping",params:{}};return await new Promise(s=>{const n=setTimeout(()=>{this.pendingRequests.delete(String(t)),s(!1)},e);this.pendingRequests.set(String(t),{resolve:()=>s(!0),reject:()=>s(!1),timer:n}),this.process.stdin.write(`${JSON.stringify(i)}
|
|
3
|
-
`)})}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.currentTurnId!==null||this.compacting,sessions:this.threadId?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.activeInputs.clear(),this.clearIdleTimer(),this.clearWaitLoopWatch(),this.clearCodexDeltaBuffer(),this.activeEventId=null}rejectEventOutput(e){if(this.compactionDeferred=this.compactionDeferred.filter(t=>t.event_id!==e),this.steerDeferred=this.steerDeferred.filter(t=>t.event_id!==e),this.activeInputs.delete(e)){this.emit("eventDone",e);return}this.activeEventId===e&&(this.clearCodexDeltaBuffer(),this.pendingAutoCompact=!1,this.currentTurnId?this.threadId&&this.interruptRejectedTurn(e,this.threadId,this.currentTurnId):this.startingEventId===e&&(this.rejectedTurnStarts.add(e),this.emit("pauseIntake","barrier")),this.clearActive({runPendingAutoCompact:!1}))}interruptRejectedTurn(e,t,i){const s=this.rejectedNativeOwner;if(s?.eventId===e&&s.threadId===t&&s.turnId===i&&s.process===this.process)return;this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer);const n={eventId:e,threadId:t,turnId:i,process:this.process,terminationAttempted:!1};this.rejectedNativeOwner=n,this.rejectedNativeTimer=setTimeout(()=>{this.rejectedNativeOwner===n&&(this.rejectedNativeTimer=null,this.terminateRejectedNativeOwner(n))},B),this.rejectedNativeTimer.unref?.(),this.sendRequest("turn/interrupt",{threadId:t,turnId:i},5e3).catch(()=>{this.terminateRejectedNativeOwner(n)})}terminateRejectedNativeOwner(e){if(!(this.rejectedNativeOwner!==e||e.terminationAttempted||!e.process||this.process!==e.process||this.threadId!==e.threadId||this.currentTurnId!==e.turnId||this.activeEventId||this.stopped||!this.alive)){e.terminationAttempted=!0,this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer),this.rejectedNativeTimer=null;try{$(e.process,"SIGTERM")}catch(t){c.error("codex-adapter",`Rejected producer termination failed; retaining native barrier: ${t instanceof Error?t.message:String(t)}`)}}}setModel(e){this.model=e,this.reasoningEffort=this.resolveReasoningEffortForModel(this.reasoningEffort,this.currentModelId()),this.persistCodexContext(),c.info("codex-adapter",`Model set to: ${e}`),this.scheduleToolbarContextPush()}setMode(e){const t=M(e);if(!t){c.info("codex-adapter",`Ignoring unsupported mode: ${e}`);return}this.collaborationMode=t,this.persistCodexContext(),c.info("codex-adapter",`Mode set to: ${t}`),this.scheduleToolbarContextPush()}setReasoningEffort(e){const t=this.resolveReasoningEffortForModel(e,this.currentModelId(),{strict:!0});if(!t){c.info("codex-adapter",`Ignoring unsupported reasoning effort for ${this.currentModelId()}: ${e}`);return}this.reasoningEffort=t,this.persistCodexContext(),c.info("codex-adapter",`Reasoning effort set to: ${t}`)}setServiceTier(e){const t=e.trim();this.serviceTier=t.toLowerCase()==="default"?"default":t,this.persistCodexContext(),c.info("codex-adapter",`Service tier set to: ${this.serviceTier}`)}setSandboxMode(e){this.sandboxMode=e,this.persistCodexContext(),c.info("codex-adapter",`Sandbox mode set to: ${e} (applied on next restart)`)}async threadCompact(){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/compact/start",{threadId:this.threadId},12e4)}async threadRollback(e){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/rollback",{threadId:this.threadId,numTurns:e},1e4)}getThreadId(){return this.threadId}hasRawApiCaptureRelay(){return!!this.rawCaptureRelay}enableAuditCapture(e,t={}){const i=e.trim();if(!i||this.auditCaptureByEventId.has(i))return;const s=t.rawProviderBody??!!this.rawCaptureOptions;this.auditCaptureByEventId.set(i,{auditId:t.auditId,threadId:this.threadId??void 0,modelId:this.currentModelId()??void 0,rawProviderBody:s,sequence:0,events:[]}),s&&this.rawCaptureRelay?.beginEvent(i,t.auditId)}takeAuditBoundary(e){const t=this.auditCaptureByEventId.get(e);if(t)return this.auditCaptureByEventId.delete(e),{adapterType:this.type,providerTurnId:t.providerTurnId,threadId:t.threadId??this.threadId??void 0,...t.sharedTurnOwner?{sharedTurnOwner:t.sharedTurnOwner}:{},codexEvidence:{events:t.events,...t.modelId?{modelId:t.modelId}:{},...t.rawProviderBody?{rawApiCapture:this.rawCaptureRelay?this.rawCaptureRelay.takeEvent(e):Je(this.rawCaptureUnavailableReason??"raw_capture_relay_not_prepared_before_adapter_start")}:{}}}}getMcpConfig(){if(!this.internalApi)return null;const e=ye(Ce(import.meta.url),"../../../mcp/acp-mcp-server.js");return{name:ke(this.callbacks.getAgentId?.()),command:process.execPath,args:[e,"--api-url",this.internalApi.url]}}async hasBackgroundWork(){const e=this.process?.pid;return e?Ee(e,[e]):!1}onAgentProfileChanged(){this.threadId&&(this.profileIdentityRefreshPending=!0,this.identityInputInjectedThreadId=null,c.info("codex-adapter",`agent profile changed; scheduling identity refresh for thread=${this.threadId}`))}async probe(e){const t=this.config.command||"codex",i=await ne(t),s=i!==null;let n=null,r;if(s){const v=await Ue(t);n=v.version,v.error&&(r=v.error)}else r={code:"cli_not_found",message:`command not found: ${t}`};const d=U(this.codexHome).baseUrl??null,h=this.currentModelId()||null,f=this.getStatus(),p=e?.conversation?{attempted:!0,ok:!1,latency_ms:null,error:{code:"unsupported",message:"codex does not support minimal prompt probe"}}:{attempted:!1,ok:!1,latency_ms:null},g=await this.probeSessionRecord(),T=se({codexHome:this.codexHome});return{cli:{command:t,installed:s,path:i,version:n,...r?{error:r}:{}},conversation:p,config:{model:h,base_url:d,source:{model:h?"runtime":"unknown",base_url:d?"file":"unknown"}},process:{started:!!this.process,alive:f.alive,busy:f.busy},session:g,lock:{present:T.present,locked:T.locked,stale:T.stale,path:T.path}}}async probeSessionRecord(){const e=this.threadId?He(this.threadId,this.codexHome):null;if(!e)return{recordPath:null,lastActivityMs:null,freshMs:null};try{const t=await te(e);return{recordPath:e,lastActivityMs:t.mtimeMs,freshMs:Date.now()-t.mtimeMs}}catch{return{recordPath:e,lastActivityMs:null,freshMs:null}}}getSupportedCommands(){return[{name:"compact",description:"Compact thread to reduce context size"},{name:"model",description:"List or set model",args:"[model_id]"},{name:"mode",description:"List or set collaboration mode",args:"[default|plan]"},{name:"rollback",description:"Roll back thread turns",args:"[num_turns]"},{name:"interrupt",description:"Interrupt current turn"},{name:"rate_limits",description:"Show current rate limits"},{name:"status",description:"Show thread and session status"},{name:"skills",description:"List available skills"}]}async execCommand(e,t,i){try{switch(e){case"compact":{if(!this.threadId)return{status:"failed",message:"No active thread"};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};if(this.activeEventId&&!this.currentTurnId)return{status:"failed",message:"agent busy"};if(this.currentTurnId&&await this.cancelActiveTurnForCompaction(),await this.ensureThreadResumed(),this.currentTurnId||this.activeEventId)return{status:"failed",message:"agent busy"};if(this.needsHistoryInjection)return{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};try{const s=await this.runCompaction();if(s.status!=="ok")throw new Error(s.message);return s}catch(s){const n=s instanceof Error?s.message:String(s);if(/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n))return this.threadId=null,this.persistThreadId(void 0),{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};throw s}}case"model":{const s=t.trim();return s?this.getModelOptions().some(r=>r.id===s)?(this.setModel(s),{status:"ok",message:`Model set to ${s}`}):{status:"failed",message:`Unknown model: ${s}`}:{status:"ok",message:`Current: ${this.currentModelId()}`,data:{models:this.getModelOptions()}}}case"mode":{const s=t.trim();if(s){const n=M(s);return n?(this.setMode(n),{status:"ok",message:`Mode set to ${n}`}):{status:"failed",message:`Unknown mode: ${s}. Supported: default, plan`}}return{status:"ok",message:`Current: ${this.collaborationMode??"default"}`,data:{modes:he}}}case"rollback":{if(!this.threadId)return{status:"failed",message:"No active thread"};const s=Math.max(1,parseInt(t.trim(),10)||1);return this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","rolled back"),this.clearActive()),await this.threadRollback(s),{status:"ok",message:`Rolled back ${s} turn(s)`}}case"interrupt":{if(!this.threadId)return{status:"failed",message:"No active thread"};const s=this.compacting;return await this.cancel(this.threadId),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","interrupted"),s||this.clearActive(),{status:"ok",message:"Turn interrupted"}}case"rate_limits":return{status:"ok",message:this.rateLimitSnapshot?`Primary: ${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}%, Secondary: ${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`:"Rate limits not available",data:{rateLimits:this.rateLimitSnapshot}};case"status":return{status:"ok",message:`Thread: ${this.threadId??"none"}, Model: ${this.currentModelId()}, Mode: ${this.collaborationMode??"default"}`,data:{threadId:this.threadId,model:this.currentModelId(),mode:this.collaborationMode??"default",cwd:this.cwd,alive:this.alive}};case"skills":{const s=qe({mode:"codex",projectDir:this.cwd}),n=s.map(r=>`- ${r.name}${r.trigger?` (${r.trigger})`:""}: ${r.description}`);return{status:"ok",message:n.length>0?n.join(`
|
|
4
|
-
`):"No skills found",data:s}}default:return{status:"unsupported",message:`Unknown command: ${e}`}}}catch(s){return{status:"failed",message:s instanceof Error?s.message:String(s)}}}async handleLocalAction(e){const t=e.action_type??"",i=e.params??{};switch(c.info("codex-adapter",`handleLocalAction action_type=${t} action_id=${e.action_id} session_id=${i.session_id??""}`),t){case"set_model":{const s=w(u(i.model_id),u(i.modelId),u(i.value));return s&&this.setModel(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("model_set")),{handled:!0,kind:"set_model"}}case"set_mode":{const s=i.mode_id;return s&&this.setMode(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("mode_set")),{handled:!0,kind:"set_mode"}}case"set_reasoning_effort":{const s=w(u(i.reasoning_effort),u(i.reasoning_eff),u(i.effort)),n=this.resolveReasoningEffortForModel(s,this.currentModelId(),{strict:!0});if(!n){const r=this.getModelOption(this.currentModelId())?.supportedReasoningEfforts??[];return this.callbacks.sendLocalActionResult(e.action_id,"failed",{outcome:"effort_rejected",model_id:this.currentModelId(),available_efforts:r},"unsupported_reasoning_effort",r.length>0?`reasoning_effort must be one of: ${r.join(", ")}`:`reasoning_effort is not supported by model ${this.currentModelId()}`),{handled:!0,kind:"set_reasoning_effort_rejected"}}return this.setReasoningEffort(n),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("effort_set",!1)),{handled:!0,kind:"set_reasoning_effort"}}case"set_service_tier":{const s=w(u(i.service_tier),u(i.serviceTier),u(i.value));return s&&this.setServiceTier(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("service_tier_set",!1)),{handled:!0,kind:"set_service_tier"}}case"set_sandbox_mode":{const s=w(u(i.sandbox_mode),u(i.sandboxMode),u(i.value));return s&&this.setSandboxMode(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("sandbox_mode_set",!1)),{handled:!0,kind:"set_sandbox_mode"}}case"thread_compact":{if(c.info("codex-adapter",`thread_compact start action_id=${e.action_id} threadId=${this.threadId}`),this.compacting)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,"Compaction already in progress"),{handled:!0,kind:"thread_compact"};if(this.activeEventId&&!this.currentTurnId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,"agent busy"),{handled:!0,kind:"thread_compact"};this.currentTurnId&&await this.cancelActiveTurnForCompaction();try{if(await this.ensureThreadResumed(),this.needsHistoryInjection)return c.info("codex-adapter",`thread_compact aborted: thread expired, new thread created action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again."),{handled:!0,kind:"thread_compact"};if(this.currentTurnId||this.activeEventId||this.compacting)throw new Error("agent busy");const s=await this.runCompaction();if(s.status!=="ok")throw new Error(s.message);c.info("codex-adapter",`thread_compact done action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(s){const n=s instanceof Error?s.message:String(s);/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n)?(c.warn("codex-adapter",`thread_compact thread expired, resetting threadId action_id=${e.action_id} err=${n}`),this.threadId=null,this.persistThreadId(void 0),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again.")):(c.info("codex-adapter",`thread_compact failed action_id=${e.action_id} err=${n}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n))}return{handled:!0,kind:"thread_compact"}}case"get_context":return this.callbacks.sendLocalActionResult(e.action_id,"ok",{threadId:this.threadId,model:this.model,mode:this.collaborationMode,approvalPolicy:this.approvalPolicy,cwd:this.cwd,...await this.buildToolbarContextResult("context")}),{handled:!0,kind:"get_context"};case"exec_approve":case"file_approve":{const s=this.resolvePendingApproval(e);if(!s)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t};const n=i.decision||"allow-once",r=fe[n]??"accept";return this.sendApprovalDecision(s.requestId,r),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,approval_command_id:s.approvalCommandId,decision:n}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t}}case"exec_reject":case"file_reject":{const s=this.resolvePendingApproval(e);return s?(this.sendApprovalDecision(s.requestId,"deny"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,approval_command_id:s.approvalCommandId,decision:"deny"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t}):(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t})}case"permission_approve":{const s=this.resolvePendingApproval(e);return!s||s.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(s.requestId,"accept"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,decision:"approve"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t})}case"permission_reject":{const s=this.resolvePendingApproval(e);return!s||s.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(s.requestId,"deny"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,decision:"deny"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t})}case"turn_interrupt":{try{await this.cancel(this.threadId??""),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(s){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,s instanceof Error?s.message:String(s))}return{handled:!0,kind:"turn_interrupt"}}case"thread_rollback":{const s=Number(i.numTurns??i.num_turns??1);if(!this.threadId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"no_thread","No active thread"),{handled:!0,kind:"thread_rollback"};this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","rolled back"),this.clearActive());try{await this.threadRollback(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",{numTurns:s})}catch(n){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n instanceof Error?n.message:String(n))}return{handled:!0,kind:"thread_rollback"}}case"get_rate_limits":{const s=this.rateLimitSnapshot,n=this.buildContextWindowSnapshot(),r=Date.now(),o=this.currentThreadTokenUsage;return s?c.info("codex-adapter",`[rate-limits] responding: primary=${s.primary.usedPercent.toFixed(1)}% window=${s.primary.windowMinutes}min resetsAt=${s.primary.resetsAt} secondary=${s.secondary.usedPercent.toFixed(1)}% window=${s.secondary.windowMinutes}min resetsAt=${s.secondary.resetsAt}`):c.info("codex-adapter","[rate-limits] responding: no rateLimitSnapshot available"),c.debug("codex-adapter",`[cp-diagnose] get_rate_limits: threadId=${this.threadId??"-"} contextWindow=${JSON.stringify(n)} tokenUsage=${JSON.stringify(o)}`),this.callbacks.sendLocalActionResult(e.action_id,"ok",{adapterType:"codex",available:s!==null,cached:!1,sampledAt:r,rateLimits:s,contextWindow:n,tokenUsage:o}),{handled:!0,kind:"get_rate_limits"}}default:return{handled:!1,kind:""}}}async buildToolbarContextResult(e,t=!0){t&&await this.refreshCodexModelOptions();const i=M(this.collaborationMode)??"default",s=this.currentModelId(),n=this.getModelOption(s),r=this.getModelOptions(),o=he,d=this.effectiveReasoningEffort()??n?.defaultReasoningEffort??null,h=n?.supportedReasoningEfforts??[],f=this.effectiveServiceTier()??n?.defaultServiceTier??null,p=n?.serviceTiers??[],g=this.isExternalProvider(),T=g?[]:h,v=g?null:d,x=g?[]:p,y=g?null:f,A={outcome:e,session_context:{modelId:s,modeId:i,reasoningEffort:v,serviceTier:y,approvalPolicy:this.approvalPolicy,sandboxMode:this.sandboxMode??null},model_id:s,mode_id:i,currentModelId:s,currentModeId:i,available_models:r,available_modes:o,available_efforts:T,availableModels:r,availableModes:o,reasoning_effort:v,available_service_tiers:x,service_tier:y,sandbox_mode:this.sandboxMode??null,models:r.map(C=>({modelId:C.id,name:C.displayName})),modes:o.map(C=>({id:C.id,name:C.displayName}))};return this.rateLimitSnapshot&&(A.rate_limit_primary_percent=this.rateLimitSnapshot.primary.usedPercent,A.rate_limit_secondary_percent=this.rateLimitSnapshot.secondary.usedPercent,A.rate_limit_primary_window_min=this.rateLimitSnapshot.primary.windowMinutes,A.rate_limit_secondary_window_min=this.rateLimitSnapshot.secondary.windowMinutes),A}currentModelId(){const e=String(this.model??"").trim();if(e)return e;const t=this.getModelOptions();return t.find(i=>i.isDefault)?.id??t[0]?.id??""}getModelOption(e){const t=e.trim().toLowerCase();return this.getModelOptions().find(i=>i.id.trim().toLowerCase()===t)}getModelOptions(){const e=this.config.options??{},t=e.available_models??e.availableModels??e.models,i=vt(t),s=this.codexModelOptions,n=[],r=String(this.model??"").trim(),o=r&&!s.some(p=>p.id.trim().toLowerCase()===r.toLowerCase()),d=s.length>0?s:o?[]:ct;if(r){const p=[...s,...i].find(g=>g.id.trim().toLowerCase()===r.toLowerCase());n.push({id:r,displayName:p?.displayName??R(r),defaultReasoningEffort:p?.defaultReasoningEffort??null,supportedReasoningEfforts:p?.supportedReasoningEfforts??[],defaultServiceTier:p?.defaultServiceTier??null,serviceTiers:p?.serviceTiers??[],isDefault:p?.isDefault??!1})}n.push(...i),n.push(...d);const h=[],f=new Set;for(const p of n){const g=p.id.trim(),T=g.toLowerCase();!g||f.has(T)||(f.add(T),h.push({id:g,displayName:p.displayName.trim()||g,defaultReasoningEffort:p.defaultReasoningEffort,supportedReasoningEfforts:[...p.supportedReasoningEfforts],defaultServiceTier:p.defaultServiceTier,serviceTiers:[...p.serviceTiers],isDefault:p.isDefault}))}return h}async refreshCodexModelOptions(){if(!this.initialized||!this.alive||!this.process?.stdin||Date.now()-this.codexModelOptionsCachedAt<O.MODEL_CACHE_TTL_MS)return;const e=U(this.codexHome);if(e.baseUrl&&ge(e.baseUrl)){try{const t=await wt(e.baseUrl,e.apiKey);if(t.length>0){this.codexModelOptions=ve(t),this.codexModelOptionsCachedAt=Date.now(),c.info("codex-adapter",`Loaded ${t.length} models from external provider ${e.baseUrl}`);return}}catch(t){c.warn("codex-adapter",`External provider /v1/models failed (${t instanceof Error?t.message:String(t)})`)}if(!It(e.baseUrl)){this.codexModelOptions=[],this.codexModelOptionsCachedAt=Date.now();return}c.info("codex-adapter",`External provider ${e.baseUrl} returned no models; falling back to Codex model/list`)}try{const t=[];let i;do{const s=await this.sendRequest("model/list",{cursor:i??null,includeHidden:!1,limit:100},15e3),n=xt(s);t.push(...n.models),i=n.nextCursor}while(i);t.length>0&&(this.codexModelOptions=ve(t),this.codexModelOptionsCachedAt=Date.now())}catch(t){c.warn("codex-adapter",`Failed to load Codex model list: ${t instanceof Error?t.message:String(t)}`)}}currentTurnId=null;compacting=!1;pendingAutoCompact=!1;compactionDeferred=[];steerDeferred=[];compactingTimer=null;compactionState=null;retiredTurnIds=new Set;nativeSettlement=null;nativeSettlementTimer=null;activeEventId=null;activeSessionId=null;visibleAgentMessageIds=new Set;hiddenAgentMessageIds=new Set;agentMessagePhases=new Map;deliveredAgentMessageIds=new Set;deliveredThinkingAgentMessageIds=new Set;codexSequence=0;pendingCodexDeltaFlush=null;turnError=null;turnErrorIsRetryableOnly=!0;turnHasVisibleOutput=!1;turnSawVisibleFinalAnswer=!1;turnSawNonEmptyFinalAnswer=!1;completedEmptyFinalAnswerItems=new Map;turnHasDeliveredFinalAnswer=!1;activeInputs=new Map;canAcceptActiveInput(e){const t=this.activeEventId?this.auditCaptureByEventId.get(this.activeEventId):void 0;return(e.audit?.enabled!==!0||!!t&&(!(e.audit.rawProviderBody??!!this.rawCaptureOptions)||t.rawProviderBody))&&this.alive&&!this.stopped&&this.initialized&&!!this.threadId&&!!this.currentTurnId&&!!this.activeEventId&&e.session_id===this.activeSessionId&&e.msg_type===1&&e.content.trim().length>0&&!this.compacting&&!this.nativeSettlement&&this.rejectedTurnStarts.size===0&&this.compactionDeferred.length===0&&this.steerDeferred.length===0}deliverActiveInput(e){if(this.activeInputs.has(e.event_id)||this.activeEventId===e.event_id)return;if(!this.canAcceptActiveInput(e)){this.sendEventResultAfterFlushing(e.event_id,"failed","Active Codex turn no longer accepts input; input was not submitted"),this.emit("eventDone",e.event_id);return}const t={accepted:!1};if(e.audit?.enabled===!0){this.enableAuditCapture(e.event_id,{auditId:e.audit.auditId,rawProviderBody:!1});const r=this.auditCaptureByEventId.get(e.event_id);r.providerTurnId=this.currentTurnId,r.sharedTurnOwner={eventId:this.activeEventId,auditId:this.auditCaptureByEventId.get(this.activeEventId)?.auditId}}this.activeInputs.set(e.event_id,t),this.emit("eventStarted",e.event_id,e.session_id);const i=F(e.content,{messageId:e.msg_id}),s={threadId:this.threadId,turnId:this.currentTurnId,inputEventId:e.event_id};this.captureActiveInputAudit(e.event_id,"turn/steer",s);const n=()=>{t.accepted=!0,this.captureActiveInputAudit(e.event_id,"turn/steer/accepted",s)};this.steerTurn(i,e.context_messages_json,n).then(()=>{this.activeInputs.get(e.event_id)===t&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++)}).catch(r=>{this.activeInputs.get(e.event_id)===t&&(this.activeInputs.delete(e.event_id),this.sendEventResultAfterFlushing(e.event_id,"failed",`Codex input delivery unconfirmed: ${r instanceof Error?r.message:String(r)}`),this.emit("eventDone",e.event_id))})}captureActiveInputAudit(e,t,i){const s=this.auditCaptureByEventId.get(e);s?.sharedTurnOwner&&s.events.push({sequence:++s.sequence,method:t,params:JSON.parse(JSON.stringify(i)),observedAt:new Date().toISOString()})}finishActiveInputs(e,t){const i=[...this.activeInputs];this.activeInputs.clear();for(const[s,n]of i){const r=e==="responded"&&!n.accepted;this.callbacks.sendEventResult(s,r?"failed":e,r?"Codex turn ended before input delivery was confirmed; check native history before retrying":t),this.emit("eventDone",s)}}deliverInboundEvent(e){if(this.stopped){this.sendEventResultAfterFlushing(e.event_id,"failed","adapter stopped");return}if(this.rejectedTurnStarts.size>0&&!this.nativeSettlement){this.compactionDeferred.push(e);return}if(this.nativeSettlement){this.nativeSettlement.waiting?this.compactionDeferred.push(e):(this.sendEventResultAfterFlushing(e.event_id,"failed","Native turn completion is unconfirmed; input was not submitted. Retry after native completion to recheck status."),this.checkNativeSettlement());return}if(this.compacting&&!this.compactionState?.resolve){this.sendEventResultAfterFlushing(e.event_id,"failed","Compaction completion is unconfirmed; input was not submitted. Retry after native completion to recheck status."),this.checkCompactionCompletion();return}if(this.compacting||this.compactionDeferred.length>0||this.activeEventId&&!this.currentTurnId){c.info("codex-adapter",`Event ${e.event_id} deferred: compaction in progress or inputs draining`),this.compactionDeferred.push(e);return}const t=F(e.content,{messageId:e.msg_id});if(this.currentTurnId&&t.trim()){c.info("codex-adapter",`Event ${e.event_id}: steering active turn ${this.currentTurnId}`);const i=this.activeEventId,s=this.currentTurnId;this.steerTurn(t).catch(n=>{if(this.nativeSettlement||this.compacting||this.activeEventId!==i||this.currentTurnId!==s){this.deliverInboundEvent(e);return}const r=n instanceof Error?n.message:String(n);if(c.info("codex-adapter",`Steer failed, falling through: ${r}`),_t(r)){c.info("codex-adapter","Unsteerable turn detected, closing local ownership and retaining native barrier"),i&&this.sendEventResultAfterFlushing(i,"failed",r),this.clearActive(),this.deliverInboundEvent(e);return}c.info("codex-adapter",`Steer failed but turn still active; deferring event ${e.event_id} for replay after turn ends`),this.steerDeferred.push(e)});return}if(this.currentTurnId){c.info("codex-adapter",`Event ${e.event_id} rejected: busy and empty content`),this.sendEventResultAfterFlushing(e.event_id,"failed","agent busy");return}this.startNewTurn(e,t)}startNewTurn(e,t){this.activeEventId=e.event_id,this.activeSessionId=e.session_id,this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,e.audit?.enabled===!0&&this.enableAuditCapture(e.event_id,{auditId:e.audit.auditId,rawProviderBody:e.audit.rawProviderBody}),this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.codexSequence=0,this.startComposing();const i=this.threadId??"",s=new X(i),n={adapterSessionId:i,text:t,contextMessages:e.context_messages_json?JSON.parse(e.context_messages_json).map(r=>({senderId:r.sender_id??"unknown",content:r.content})):void 0};this.runTurn(n,s,e.event_id,e.session_id).catch(r=>{this.activeEventId!==e.event_id||this.compacting||(c.error("codex-adapter",`Turn failed: ${r}`),this.sendEventResultAfterFlushing(e.event_id,"failed",r instanceof Error?r.message:String(r)),this.clearActive())}),this.resetIdleTimer(e.event_id)}deliverStopEvent(e,t){const i=this.compactionDeferred.findIndex(s=>s.event_id===e);if(i>=0){this.compactionDeferred.splice(i,1),this.sendEventResultAfterFlushing(e,"canceled","stopped by user");return}this.activeEventId&&(this.activeEventId===e||this.activeInputs.has(e))&&(this.cancel(this.threadId??"").catch(()=>{}),this.sendEventResultAfterFlushing(this.activeEventId,"canceled","stopped by user"),this.clearActive())}async spawnCodex(){const e={...this.resolveChildEnv(),...this.directEnv},t=typeof e.PATH=="string"?e.PATH:void 0;t||c.warn("codex-adapter",`spawnCodex: env.PATH is missing! process.env.PATH=${process.env.PATH?"set":"missing"}, config.env=${JSON.stringify(this.config.env)}`);const i=await pe(this.config.command||"codex",t),s=pt(this.config.args);for(const o of this.configOverrideArgs)s.push("-c",o);for(const o of this.resolveRelayTransportConfigOverrides())s.push("-c",o);for(const o of this.rawCaptureConfigOverrides)s.push("-c",o);this.sandboxMode&&s.push("-c",`sandbox_mode="${this.sandboxMode}"`);const n=s.map(o=>this.rawCaptureConfigOverrides.includes(o)?"<raw-api-capture-overlay>":o);c.info("codex-adapter",`Spawning: ${i} ${n.join(" ")}`);try{if(!(await te(this.cwd)).isDirectory())throw new Error(`Bound path is not a directory: ${this.cwd}`)}catch(o){throw String(o?.code??"")==="ENOENT"?new Error(`Bound directory does not exist: ${this.cwd}. Please rebind with /grix open <valid-directory>.`):o}const r=se({codexHome:this.codexHome});if(r.locked&&!r.stale)throw new Error(`Codex session appears locked by another process: ${r.path}`);r.stale&&c.warn("codex-adapter",`Ignoring stale Codex lock: ${r.path}`);try{this.process=be(i,s,{env:e,cwd:this.cwd}).process}catch(o){throw c.error("codex-adapter",`Codex spawn threw: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",o}this.process.on("error",o=>{c.error("codex-adapter",`Codex process spawn error: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.currentTurnId=null,this.clearNativeSettlement("Codex process spawn failed"),this.finishCompaction({status:"failed",message:"Codex process spawn failed"}),this.rejectAllPending("spawn failed: "+(o instanceof Error?o.message:String(o))),this.stopComposing(),this.activeEventId?(this.flushCodexDeltaBuffer(),this.sendEventResultAfterFlushing(this.activeEventId,"failed","Codex process spawn failed"),this.activeEventId=null):this.clearCodexDeltaBuffer(),this.emit("exit",1)}),this.process.on("exit",o=>{c.info("codex-adapter",`Codex process exited (code=${o})`),this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.currentTurnId=null,this.clearNativeSettlement("Codex process exited"),this.finishCompaction({status:"failed",message:"Codex process exited"}),this.rejectAllPending("process exited"),this.stopComposing(),this.activeEventId?this.flushCodexDeltaBuffer():this.clearCodexDeltaBuffer(),this.emit("exit",o)}),this.process.stderr?.on("data",o=>{const d=o.toString().trim();d&&c.info("codex-adapter",`[codex stderr] ${d}`)}),this.bridgeStatus="starting",this.bindStdout(),this.alive=!0,this.startOrphanTurnWatch()}resolveRelayTransportConfigOverrides(){if(this.config.env?.[Ze]!=="1")return[];const e=Ge(this.codexHome,this.configOverrideArgs);if(e==="openai"){const i=Ve(this.codexHome,this.configOverrideArgs);return We(Be,"Grix OpenAI Relay",i.targetBaseUrl)}const t=Xe(e);return t?[`${t}=false`]:[]}bindStdout(){if(!this.process?.stdout)return;this.process.stdout.setEncoding("utf8"),xe({input:this.process.stdout}).on("line",t=>{if(!t.trim())return;let i;try{i=JSON.parse(t)}catch{c.error("codex-adapter",`Invalid JSON from Codex: ${t.slice(0,200)}`);return}this.bridgeStatus==="starting"&&(this.bridgeStatus="ready");const s=Object.prototype.hasOwnProperty.call(i,"result"),n=Object.prototype.hasOwnProperty.call(i,"error"),r=!Object.prototype.hasOwnProperty.call(i,"method")&&s!==n&&(s||typeof i.error?.code=="number"&&typeof i.error.message=="string");if(i.id!=null&&r){const o=String(i.id),d=this.pendingRequests.get(o);if(d){clearTimeout(d.timer),this.pendingRequests.delete(o),d.resolve(i);return}}this.handleNotification(i)})}handleNotification(e){const t=e.method,i=e.params??{};if(this.stopped||this.bridgeStatus==="closed")return;if(this.shouldIgnoreOutOfScopeNotification(e,t,i)){const n=w(u(i.threadId),u(i.threadID)),r=i.item;if(n&&n!==this.threadId&&r?.id&&this.isLongRunningToolItemType(r.type)){const o=`${n}:${r.id}`;t==="item/started"&&this.waitLoopChildTools.add(o),t==="item/completed"&&this.waitLoopChildTools.delete(o),this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++}this.activeEventId&&this.resetIdleTimer(this.activeEventId);return}switch(this.captureAuditNotification(t,i),this.captureContextWindowFromPayload(t,i),this.activeEventId&&t&&t.startsWith("item/")&&!t.endsWith("/requestApproval")&&this.startComposing(),t){case"thread/status/changed":{const n=i.status;this.nativeSettlement&&i.threadId===this.nativeSettlement.threadId&&n?.type==="idle"&&this.checkNativeSettlement(),this.compactionState&&!this.compactionState.resolve&&i.threadId===this.compactionState.threadId&&n?.type==="idle"&&this.checkCompactionCompletion();break}case"item/started":{const n=i.item,r=n?.id,o=n?.type,d=this.normalizeAgentMessagePhase(n?.phase);if(this.isLongRunningToolItemType(o)&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++,this.inFlightToolOps+=1),o==="agentMessage"&&d==="final_answer"&&D(d)&&this.recordFinalAnswerEvidence(r,this.extractAgentMessageText(n)),o==="agentMessage"&&r){d?this.agentMessagePhases.set(r,d):this.agentMessagePhases.delete(r);const h=D(d),f=H(d);h?(this.visibleAgentMessageIds.add(r),this.hiddenAgentMessageIds.delete(r)):f?(this.visibleAgentMessageIds.delete(r),this.hiddenAgentMessageIds.delete(r)):(this.hiddenAgentMessageIds.add(r),this.visibleAgentMessageIds.delete(r))}break}case"item/agentMessage/delta":{const n=i.delta;n&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++);const r=i.itemId,o=this.resolveAgentMessagePhase(r,i.phase),d=(!r||!this.hiddenAgentMessageIds.has(r))&&D(o),h=(!r||!this.hiddenAgentMessageIds.has(r))&&H(o);if(o==="final_answer"&&d&&this.recordFinalAnswerEvidence(r,n),n&&this.activeEventId&&h){this.recordDeliveredThinkingAgentMessage(r),this.callbacks.sendThinking?.(this.activeEventId,this.activeSessionId??"",n);break}n&&this.activeEventId&&d&&!this.shouldSuppressAgentMessageDeltaDuringToolRun(r,o)&&(this.recordDeliveredAgentMessage(r,o),this.queueCodexDeltaEvent({...e,params:{...i,...o?{phase:o}:{}}}));break}case"item/completed":{const n=i.item,r=n?.id,o=n?.type;if(this.isLongRunningToolItemType(o)&&(this.inFlightToolOps=Math.max(0,this.inFlightToolOps-1)),this.activeEventId&&o==="commandExecution"&&(this.turnHasVisibleOutput=!0,this.emitCodexEvent("item/completed",e)),o==="agentMessage"){const d=this.resolveAgentMessagePhase(r,n?.phase),h=(!r||!this.hiddenAgentMessageIds.has(r))&&D(d);if(d==="final_answer"&&h){const f=this.extractAgentMessageText(n);this.recordFinalAnswerEvidence(r,f,!0);const p=r?this.deliveredAgentMessageIds.has(r):!1;this.activeEventId&&f&&!p&&(c.warn("codex-adapter",`Recovering final_answer from item/completed item_id=${r??"unknown"}`),this.recordDeliveredAgentMessage(r,d),this.queueCodexDeltaEvent({method:"item/agentMessage/delta",params:{...r?{itemId:r}:{},delta:f,phase:d}}))}else if(H(d)){const f=this.extractAgentMessageText(n),p=r?this.deliveredThinkingAgentMessageIds.has(r):!1;this.activeEventId&&f&&!p&&(this.recordDeliveredThinkingAgentMessage(r),this.callbacks.sendThinking?.(this.activeEventId,this.activeSessionId??"",f))}r&&(this.visibleAgentMessageIds.delete(r),this.hiddenAgentMessageIds.delete(r),this.agentMessagePhases.delete(r))}break}case"turn/started":{const r=i.turn?.id;if(r){if(this.compactionState&&(this.compactionState.turnId=r),this.currentTurnId=r,this.rejectedTurnStarts.size>0&&!this.activeEventId){this.retainUnconfirmedNativeTurn();const o=this.rejectedTurnStarts.values().next().value;this.threadId&&o&&this.interruptRejectedTurn(o,this.threadId,r);break}this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.currentTurnStartedAt=Date.now(),this.preflightCheckComplete=!1,c.info("codex-adapter",`Turn started: ${r}`),this.startComposing(),this.emit("activeInputReady")}break}case"item/fileChange/requestApproval":{this.handleApprovalRequest(e,"file");break}case"item/commandExecution/requestApproval":{this.handleApprovalRequest(e,"exec");break}case"item/permissions/requestApproval":{this.handleApprovalRequest(e,"permission");break}case"turn/completed":{this.handleTurnCompleted(i);break}case"error":{const n=i.error,r=i.message??n?.message??JSON.stringify(i);c.error("codex-adapter",`Codex error: ${r}`);const o=i.willRetry===!0;r&&(this.turnError=r,o||(this.turnErrorIsRetryableOnly=!1)),this.activeEventId&&!o&&(this.flushCodexDeltaBuffer(),this.callbacks.sendRunError(this.activeEventId,this.activeSessionId??"",r));break}case"account/rateLimits/updated":{const n=this.extractRateLimitPushLimitId(i),r=this.parseRateLimitSnapshot(i,this.rateLimitSnapshot);r&&(this.rateLimitSnapshot=r,c.info("codex-adapter",`Rate limits updated: primary=${r.primary.usedPercent.toFixed(1)}% secondary=${r.secondary.usedPercent.toFixed(1)}% extras=${r.extras?.length??0}`),this.callbacks.onRateLimitsUpdated?.(r)),n&&n!=="codex"&&this.refreshMainRateLimitsThrottled();break}case"thread/tokenUsage/updated":{const n=i.threadId,r=i.tokenUsage,o=r?.last;(!n||!r||!o)&&c.info("codex-adapter",`[cp-diagnose] token_usage payload_incomplete: method=thread/tokenUsage/updated hasThreadId=${String(!!n)} hasTokenUsage=${String(!!r)} hasLast=${String(!!o)} params=${JSON.stringify(i)}`),n&&r&&o&&(this.currentThreadTokenUsage={inputTokens:o.inputTokens??0,outputTokens:o.outputTokens??0,cacheReadInputTokens:o.cachedInputTokens??0,cacheCreationInputTokens:0},c.info("codex-adapter",`[cp-diagnose] token_usage raw thread=${n}: ${JSON.stringify(r)}`),c.info("codex-adapter",`[cp-diagnose] token_usage parsed thread=${n}: ${JSON.stringify(this.currentThreadTokenUsage)}`),this.callbacks.onTokenUsageUpdated?.(this.currentThreadTokenUsage),this.publishContextWindowSnapshot());break}default:break}const s=t?.endsWith("/requestApproval")===!0;t&&t!=="turn/started"&&(t.startsWith("item/")||t==="error"||t==="turn/completed")&&(this.preflightCheckComplete=!0),this.activeEventId&&(!s||this.pendingApprovals.size===0)&&this.resetIdleTimer(this.activeEventId)}shouldIgnoreOutOfScopeNotification(e,t,i){if(!t||e.id!=null)return!1;const s=w(u(i.threadId),u(i.threadID));if(s&&this.threadId&&s!==this.threadId)return this.logIgnoredNotificationScope(t,s,this.readNotificationTurnId(i)),!0;if(this.nativeSettlement&&(t.startsWith("turn/")||t.startsWith("item/")||t==="error")){const r=i.turn;return t!=="turn/completed"||s!==this.nativeSettlement.threadId||this.readNotificationTurnId(i)!==this.nativeSettlement.turnId||!["completed","failed","interrupted"].includes(String(r?.status))}if(this.compactionState&&t.startsWith("turn/")){if(s!==this.compactionState.threadId)return!0;const r=this.readNotificationTurnId(i);if(!r||this.retiredTurnIds.has(r)||t==="turn/completed"&&r!==this.compactionState.turnId)return!0}const n=this.readNotificationTurnId(i);return n&&this.retiredTurnIds.has(n)?!0:n&&this.currentTurnId&&n!==this.currentTurnId?(this.logIgnoredNotificationScope(t,s,n),!0):!1}readNotificationTurnId(e){const t=e.turn&&typeof e.turn=="object"?e.turn:void 0;return w(u(e.turnId),u(e.turnID),u(t?.id))}logIgnoredNotificationScope(e,t,i){e!=="error"&&!e.startsWith("turn/")||c.info("codex-adapter",`[scope] ignored ${e} thread=${t??"<none>"} turn=${i??"<none>"} active_thread=${this.threadId??"<none>"} active_turn=${this.currentTurnId??"<none>"}`)}captureAuditNotification(e,t){const i=this.activeEventId;if(!e||!i)return;const s=this.auditCaptureByEventId.get(i);if(!s)return;const n=t.turn,r=typeof t.turnId=="string"&&t.turnId?t.turnId:typeof t.turn_id=="string"&&t.turn_id?t.turn_id:typeof n?.id=="string"&&n.id?n.id:this.currentTurnId;r&&(s.providerTurnId=r);const o=typeof t.threadId=="string"&&t.threadId?t.threadId:typeof t.thread_id=="string"&&t.thread_id?t.thread_id:this.threadId;if(o&&(s.threadId=o),!!lt.has(e)){try{const d=JSON.parse(JSON.stringify(t));s.events.push({sequence:++s.sequence,method:e,params:d,observedAt:new Date().toISOString()})}catch(d){const h=d instanceof Error?d.message:String(d);c.warn("codex-adapter",`Audit evidence capture skipped event=${i} method=${e}: ${h}`)}if(e==="turn/completed"||e==="codex/turn/completed")for(const d of this.activeInputs.keys())this.captureActiveInputAudit(d,e,{threadId:o,turnId:r,turn:n?{id:n.id,status:n.status,error:n.error}:void 0})}}captureContextWindowFromPayload(e,t){const i=["model_context_window","modelContextWindow","context_window_size","contextWindowSize"],s=this.findNumericField(t,i);if(s==null||s<=0){const n=this.probeContextWindowFields(t,i);n.foundAnyCandidateField&&c.info("codex-adapter",`[cp-diagnose] context_window missing_or_invalid: method=${e??"-"} matchedFields=${n.matchedFields.join(",")||"-"} raw=${n.rawValues.join(",")||"-"} extracted=${String(s)}`);return}this.currentModelContextWindow!==s&&(this.currentModelContextWindow=s,c.info("codex-adapter",`[cp-diagnose] context_window updated: method=${e??"-"} size=${s}`),this.publishContextWindowSnapshot())}probeContextWindowFields(e,t){const i=[],s=[],n=r=>{if(!r||typeof r!="object")return;if(Array.isArray(r)){for(const d of r)n(d);return}const o=r;for(const d of t)Object.prototype.hasOwnProperty.call(o,d)&&(i.push(d),s.push(String(o[d])));for(const d of Object.values(o))n(d)};return n(e),{foundAnyCandidateField:i.length>0,matchedFields:i,rawValues:s}}findNumericField(e,t){if(!e||typeof e!="object")return null;const i=e;for(const s of t){const n=i[s];if(typeof n=="number"&&Number.isFinite(n))return n;if(typeof n=="string"){const r=Number(n);if(Number.isFinite(r))return r}}for(const s of Object.values(i)){if(Array.isArray(s)){for(const r of s){const o=this.findNumericField(r,t);if(o!=null)return o}continue}const n=this.findNumericField(s,t);if(n!=null)return n}return null}buildContextWindowSnapshot(){const e=this.currentModelContextWindow;if(!e||e<=0)return c.debug("codex-adapter",`[cp-diagnose] context_window snapshot skipped: invalid size=${String(e)}`),null;const t=this.currentThreadTokenUsage,i=t?t.inputTokens+t.outputTokens:null,s=i==null?null:Math.max(0,e-i),n=i==null?null:Math.min(100,i/e*100),r=i==null?null:Math.max(0,100-(n??0));return c.info("codex-adapter",`[cp-diagnose] context_window computed: threadId=${this.threadId??"-"} size=${e} usedTokens=${String(i)} usedPercentage=${String(n)}`),{sizeTokens:e,usedTokens:i,remainingTokens:s,usedPercentage:n,remainingPercentage:r,source:"codex:model_context_window"}}publishContextWindowSnapshot(){const e=this.buildContextWindowSnapshot();this.callbacks.onContextWindowUpdated?.(e),this.maybeScheduleAutoCompact(e)}maybeScheduleAutoCompact(e){const t=e?.usedPercentage;typeof t!="number"||t<le||this.pendingAutoCompact||this.compacting||(this.pendingAutoCompact=!0,c.info("codex-adapter",`[auto-compact] context_window usedPercentage=${t.toFixed(1)}% >= ${le}%, scheduling compact`),this.tryRunPendingAutoCompact())}tryRunPendingAutoCompact(){this.pendingAutoCompact&&(this.stopped||this.bridgeStatus==="closed"||this.currentTurnId||this.activeEventId||this.compacting||(this.pendingAutoCompact=!1,this.execCommand("compact","",this.activeSessionId??"").then(e=>{c.info("codex-adapter",`[auto-compact] compact done status=${e.status} msg=${e.message??""}`),e.status==="failed"&&/agent busy/i.test(e.message??"")&&(this.pendingAutoCompact=!0)}).catch(e=>{c.warn("codex-adapter",`[auto-compact] compact error: ${e instanceof Error?e.message:e}`)})))}parseRateLimitSnapshot(e,t=null){try{const i=e.rateLimitsByLimitId,s=this.extractRateLimitPushLimitId(e),n=!i?.codex&&!!s&&s!=="codex",r=i?.codex??(n?{}:e.rateLimits??e),o=r.primary,d=r.secondary,h=r.credits,f=l=>{if(typeof l=="number"&&Number.isFinite(l))return l;if(typeof l=="string"){const m=Number(l);if(Number.isFinite(m))return m}return 0},p=l=>typeof l=="boolean"?l:typeof l=="string"?l.toLowerCase()==="true":!1,g=l=>{if(l==null)return null;if(typeof l=="string"){const m=l.trim();if(!m)return null;const I=Number(m);return Number.isFinite(I)&&I>1e9?new Date(I*1e3).toISOString():m}if(typeof l=="number"&&Number.isFinite(l)&&l>0){const m=l>1e12?l/1e3:l;return new Date(m*1e3).toISOString()}return null},T=l=>{const m=l.resets_in_seconds??l.resetsInSeconds,I=typeof m=="number"||typeof m=="string"?Number(m):NaN;return!Number.isFinite(I)||I<=0?null:new Date(Date.now()+I*1e3).toISOString()},v=l=>({usedPercent:f(l.used_percent??l.usedPercent),windowMinutes:f(l.window_minutes??l.windowDurationMins??l.windowMinutes),resetsAt:g(l.resets_at??l.resetsAt)??T(l)}),x=()=>({usedPercent:0,windowMinutes:0,resetsAt:null}),y=(l,m)=>l??m??x(),A=()=>{const l=[];if(o&&l.push(o),d&&l.push(d),Array.isArray(r.windows))for(const m of r.windows)m&&typeof m=="object"&&l.push(m);return l.map(v).filter(m=>m.windowMinutes>0)},C=(l,m,I)=>{const S=l.filter(b=>I(b.windowMinutes));return S.length===0?null:S.reduce((b,E)=>Math.abs(E.windowMinutes-m)<Math.abs(b.windowMinutes-m)?E:b)},N=A(),L=n?t?{primary:t.primary,secondary:t.secondary}:null:N.length>0?{primary:y(C(N,300,l=>l<1440),t?.primary),secondary:y(C(N,10080,l=>l>=1440),t?.secondary)}:o||d?{primary:o?v(o):t?.primary??x(),secondary:d?v(d):t?.secondary??x()}:null;if(!L)return null;const J=(l,m)=>{if(!m||typeof m!="object"||Array.isArray(m))return null;const I=m,S=I.limitName??I.limit_name,b=typeof S=="string"&&S.trim()?S.trim():l,E=[],Y=I.primary,K=I.secondary;Y&&E.push(Y),K&&E.push(K);const Z=E.map(v).filter(k=>k.windowMinutes>0),Q=C(Z,10080,k=>k>=1440)??C(Z,300,k=>k<1440);return Q?{id:l,label:b,...Q}:null};let _;if(i&&typeof i=="object"){_=[];for(const[l,m]of Object.entries(i)){if(l==="codex")continue;const I=J(l,m);I&&_.push(I)}}else if(_=t?.extras?[...t.extras]:[],n&&s){const l=J(s,e.rateLimits??e);if(l){const m=_.findIndex(I=>I.id===l.id);m>=0?_[m]=l:_.push(l)}}return{primary:L.primary,secondary:L.secondary,credits:h?{hasCredits:p(h.has_credits??h.hasCredits),unlimited:p(h.unlimited),balance:h.balance==null?null:f(h.balance)}:t?.credits??{hasCredits:!1,unlimited:!1,balance:null},..._.length>0?{extras:_}:{}}}catch{return null}}handleApprovalRequest(e,t){const i=e.id;if(i==null)return;const s=e.method,n=`${i}`.trim(),r=`codex_${Ie(this.activeSessionId??"")}_${Ie(n)}`;if(this.approvalPolicy==="never"){this.sendApprovalDecision(i,"accept");return}this.activeEventId&&this.emitCodexEvent(s,e),this.pendingApprovals.set(r,{approvalId:r,approvalCommandId:n,sourceEventId:this.activeEventId??"",requestId:i,kind:t}),c.info("codex-adapter",`Pending approval stored: ${r} (kind=${t})`),this.clearIdleTimer(),this.stopComposing()}resolvePendingApproval(e){const t=e.params??{},i=w(t.approval_id,t.approvalId);if(i)return this.pendingApprovals.get(i)??null;const s=w(t.approval_command_id,t.approvalCommandId);if(s){for(const[,n]of this.pendingApprovals)if(n.approvalCommandId===s)return n}return null}async handleExecApprovalEvent(e){const t={approval_id:e.approval_id,approval_command_id:e.approval_command_id,session_id:e.session_id},i={action_id:"exec_approval_event",action_type:e.decision==="deny"?"exec_reject":"exec_approve",params:t},s=this.resolvePendingApproval(i);if(!s)return{delivered:!1,errorCode:"approval_not_found",errorMsg:"That approval request is no longer pending."};const n=fe[e.decision]??"accept";return this.sendApprovalDecision(s.requestId,n),this.pendingApprovals.delete(s.approvalId),this.resumeAfterApproval({...i,event_id:s.sourceEventId},t,s),{delivered:!0}}sendApprovalDecision(e,t){if(e==null||!this.process?.stdin)return;const i={jsonrpc:"2.0",id:e,result:{decision:t}};this.process.stdin.write(`${JSON.stringify(i)}
|
|
5
|
-
`)}async cancelActiveTurnForCompaction(){const e=this.threadId,t=this.currentTurnId,i=this.activeEventId;await this.cancel(e??"").catch(()=>{}),!(this.threadId!==e||this.currentTurnId!==t||this.activeEventId!==i||this.compacting)&&(i&&this.sendEventResultAfterFlushing(i,"canceled","compacting"),this.clearActive())}runCompaction(){if(this.currentTurnId||this.activeEventId||this.compacting)return Promise.resolve({status:"failed",message:"agent busy"});this.compacting=!0;const e=new Promise(i=>{this.compactionState={threadId:this.threadId,turnId:null,deadline:Date.now()+B,checking:!1,resolve:i}}),t=this.compactionState;return this.armCompactionCheck(),this.threadCompact().catch(i=>{if(this.compactionState!==t)return;const s=i instanceof Error?i.message:String(i),n=!t?.turnId&&(/thread.*(not found|not exist|unknown|invalid)/i.test(s)||/not found.*thread/i.test(s));this.finishCompaction({status:"failed",message:s},n)}),e}armCompactionCheck(){this.compactingTimer&&clearTimeout(this.compactingTimer);const e=this.compactionState;e?.resolve&&(this.compactingTimer=setTimeout(()=>{this.compactingTimer=null,this.checkCompactionCompletion()},Math.min(ue,Math.max(0,e.deadline-Date.now()))),this.compactingTimer.unref?.())}async checkCompactionCompletion(){const e=this.compactionState;if(!e||e.checking||this.stopped||!this.alive||this.threadId!==e.threadId||!e.resolve&&!e.turnId)return;const t=i=>{this.compactionState===e&&this.finishCompaction({status:"failed",message:i},!1)};if(e.resolve&&Date.now()>=e.deadline){t("Compaction completion was not confirmed within 10 minutes; input was not submitted");return}e.checking=!0;try{const i=await this.sendRequest("thread/read",{threadId:e.threadId,includeTurns:!0},5e3);if(this.compactionState!==e||this.stopped||!this.alive||this.threadId!==e.threadId)return;const s=i.thread?.id===e.threadId&&e.turnId?i.thread.turns?.find(n=>n.id===e.turnId):void 0;i.thread?.status?.type==="idle"&&s&&["completed","failed","interrupted"].includes(String(s.status))?this.handleTurnCompleted({threadId:e.threadId,turn:s}):s?.status==="inProgress"||i.thread?.id===e.threadId&&i.thread.status?.type==="active"?(c.info("codex-adapter","Compaction still in progress; retaining input barrier"),this.armCompactionCheck()):t("Compaction native state could not be confirmed; input was not submitted")}catch(i){t(`Compaction status check failed; input was not submitted: ${i instanceof Error?i.message:String(i)}`)}finally{e.checking=!1}}retainUnconfirmedNativeTurn(){this.compacting||this.nativeSettlement||!this.currentTurnId||!this.threadId||!this.alive||this.stopped||(this.nativeSettlement={threadId:this.threadId,turnId:this.currentTurnId,deadline:Date.now()+B,checking:!1,waiting:!0},this.armNativeSettlementCheck())}clearNativeSettlement(e){if(this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer),this.nativeSettlementTimer=null,this.nativeSettlement=null,this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer),this.rejectedNativeTimer=null,this.rejectedNativeOwner=null,e){this.failCompactionDeferred(e);for(const t of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed",e)}}armNativeSettlementCheck(){this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer);const e=this.nativeSettlement;e?.waiting&&(this.nativeSettlementTimer=setTimeout(()=>{this.nativeSettlementTimer=null,this.checkNativeSettlement()},Math.min(ue,Math.max(0,e.deadline-Date.now()))),this.nativeSettlementTimer.unref?.())}async checkNativeSettlement(){const e=this.nativeSettlement;if(!e||e.checking||this.stopped||!this.alive||this.threadId!==e.threadId)return;const t=i=>{if(this.nativeSettlement===e){e.waiting=!1,this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer),this.nativeSettlementTimer=null,this.failCompactionDeferred(i);for(const s of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(s.event_id,"failed",i)}};if(e.waiting&&Date.now()>=e.deadline){t("Native turn completion was not confirmed within 10 minutes; input was not submitted");return}e.checking=!0;try{const i=await this.sendRequest("thread/read",{threadId:e.threadId,includeTurns:!0},5e3);if(this.nativeSettlement!==e||this.stopped||!this.alive||this.threadId!==e.threadId)return;const s=i.thread?.id===e.threadId?i.thread.turns?.find(n=>n.id===e.turnId):void 0;i.thread?.status?.type==="idle"&&s&&["completed","failed","interrupted"].includes(String(s.status))?this.handleTurnCompleted({threadId:e.threadId,turn:s}):s?.status==="inProgress"||i.thread?.id===e.threadId&&i.thread.status?.type==="active"?this.armNativeSettlementCheck():t("Native turn state could not be confirmed; input was not submitted")}catch(i){t(`Native turn status check failed; input was not submitted: ${i instanceof Error?i.message:String(i)}`)}finally{e.checking=!1}}startOrphanTurnWatch(){this.orphanTurnTimer||(this.orphanTurnTimer=setInterval(()=>this.checkOrphanTurn(),rt),this.orphanTurnTimer.unref?.())}stopOrphanTurnWatch(){this.orphanTurnTimer&&(clearInterval(this.orphanTurnTimer),this.orphanTurnTimer=null),this.orphanTurnSince=0}checkOrphanTurn(){if(!(this.currentTurnId!==null&&this.activeEventId===null&&!this.compacting&&!this.nativeSettlement)){this.orphanTurnSince=0;return}const t=Date.now();if(this.orphanTurnSince===0){this.orphanTurnSince=t;return}if(t-this.orphanTurnSince<ot)return;const i=this.currentTurnId,s=t-this.orphanTurnSince;this.currentTurnId=null,this.orphanTurnSince=0,c.warn("codex-adapter",`Orphan turn ${i} cleared after ${Math.round(s/1e3)}s with no active event: turn/completed never arrived. Adapter was reporting busy the whole time, which blocks upgrade/restart idle waits.`)}finishCompaction(e,t=!0){const i=this.compactionState;if(!i){this.failCompactionDeferred(e.message??"Compaction failed");return}this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);const s=i.resolve;i.resolve=null,t&&(i.turnId&&this.retiredTurnIds.add(i.turnId),this.compactionState=null,this.compacting=!1,this.currentTurnId=null,this.stopComposing()),c.info("codex-adapter",`Compaction settled status=${e.status} nativeSettled=${t}`),s?.(e),e.status==="ok"&&t&&s?this.replayCompactionDeferred():this.failCompactionDeferred(e.message??"Compaction failed")}failCompactionDeferred(e){for(const t of this.compactionDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed",e)}replayCompactionDeferred(){if(this.compacting||this.activeEventId||this.currentTurnId||this.stopped||!this.alive)return;const e=this.compactionDeferred.shift();e&&this.startNewTurn(e,F(e.content,{messageId:e.msg_id}))}handleTurnCompleted(e){if(this.nativeSettlement){const s=this.nativeSettlement,n=e.turn;if(e.threadId!==s.threadId&&e.threadID!==s.threadId||n?.id!==s.turnId||!["completed","failed","interrupted"].includes(String(n.status)))return;this.retiredTurnIds.add(s.turnId),this.clearNativeSettlement(),this.currentTurnId=null,this.rejectedTurnStarts.size>0&&(this.rejectedTurnStarts.clear(),this.emit("resumeIntake","barrier")),this.replayCompactionDeferred(),this.replaySteerDeferred(),this.tryRunPendingAutoCompact();return}if(this.compactionState){const s=e.turn;if(e.threadId!==this.compactionState.threadId||s?.id!==this.compactionState.turnId)return;const n=s?.status;if(!["completed","failed","interrupted"].includes(String(n)))return;const r=s?.error,o=n==="completed"&&!(this.turnError&&!this.turnErrorIsRetryableOnly);this.finishCompaction({status:o?"ok":"failed",message:o?"Thread compacted":r?.message??this.turnError??`Compaction ${n}`});return}const t=this.readNotificationTurnId(e),i=e.turn;if(!(!i||!t||e.threadId!==this.threadId&&e.threadID!==this.threadId||!["completed","failed","interrupted"].includes(String(i.status))||this.currentTurnId&&t!==this.currentTurnId)&&(this.retiredTurnIds.add(t),this.currentTurnId=null,this.stopComposing(),this.activeEventId)){this.flushCodexDeltaBuffer();const s=this.activeEventId,n={event_id:s,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:"turn/completed",codex_sequence:++this.codexSequence},r=this.turnError,o=this.turnErrorIsRetryableOnly,d=this.turnHasVisibleOutput,h=i.status==="completed"&&this.completedEmptyFinalAnswerItems.size>0&&[...this.completedEmptyFinalAnswerItems.values()].every(x=>x)&&!this.turnSawNonEmptyFinalAnswer,f=this.turnSawVisibleFinalAnswer&&!this.turnHasDeliveredFinalAnswer&&!h;this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1;const T=i.error?.message??(i.status!=="completed"?r??`Codex turn ${i.status}`:null)??(r&&!(o&&d)?r:null)??(f?"Codex final answer was observed but not delivered":null)??(!d&&!h?"Codex completed without producing any visible output":null),v=T!==null;this.callbacks.sendCodexEvent(n),this.sendEventResultAfterFlushing(s,v?"failed":"responded",T??void 0),this.clearActive()}}emitCodexEvent(e,t){if(!this.activeEventId)return;this.flushCodexDeltaBuffer();const i=we(e,t);if(i===void 0&&e==="item/completed")return;const s={event_id:this.activeEventId,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:e,codex_sequence:++this.codexSequence};i!==void 0&&(s.codex_payload=i),this.callbacks.sendCodexEvent(s)}queueCodexDeltaEvent(e){if(!this.activeEventId)return;const t=we("item/agentMessage/delta",e),i=t?.params?.delta;if(typeof i!="string"||i.length===0)return;const s=typeof t?.params?.phase=="string"?t.params.phase:void 0,n=this.activeEventId,r=this.activeSessionId??"",o=this.threadId??void 0,d=this.pendingCodexDeltaFlush;if(d&&(d.eventId!==n||d.sessionId!==r||d.threadId!==o||d.phase!==s)&&this.flushCodexDeltaBuffer(),this.pendingCodexDeltaFlush||(this.pendingCodexDeltaFlush={eventId:n,sessionId:r,threadId:o,delta:"",phase:s,timer:null}),this.pendingCodexDeltaFlush.delta+=i,this.pendingCodexDeltaFlush.delta.length>=dt){this.flushCodexDeltaBuffer();return}this.pendingCodexDeltaFlush.timer||(this.pendingCodexDeltaFlush.timer=setTimeout(()=>{if(this.activeEventId!==n){this.clearCodexDeltaBuffer();return}this.flushCodexDeltaBuffer()},at))}flushCodexDeltaBuffer(){const e=this.pendingCodexDeltaFlush;if(!e||(this.pendingCodexDeltaFlush=null,e.timer&&clearTimeout(e.timer),!e.delta))return;const t={event_id:e.eventId,session_id:e.sessionId,thread_id:e.threadId,codex_event_type:"codex",codex_method:"item/agentMessage/delta",codex_sequence:++this.codexSequence,codex_payload:{params:{delta:e.delta,...e.phase?{phase:e.phase}:{}}}};this.callbacks.sendCodexEvent(t)}clearCodexDeltaBuffer(){const e=this.pendingCodexDeltaFlush;this.pendingCodexDeltaFlush=null,e?.timer&&clearTimeout(e.timer)}sendEventResultAfterFlushing(e,t,i){e===this.activeEventId&&t!=="responded"&&this.retainUnconfirmedNativeTurn(),this.pendingCodexDeltaFlush?.eventId===e&&this.flushCodexDeltaBuffer(),e===this.activeEventId&&this.finishActiveInputs(t,i),this.callbacks.sendEventResult(e,t,i)}startComposing(){}stopComposing(){}async sendRequest(e,t,i=3e4,s){return new Promise((n,r)=>{if(!this.process?.stdin){r(new Error("Codex process not available"));return}const o=++this.requestId,d={jsonrpc:"2.0",id:o,method:e,params:t??{}},h=setTimeout(()=>{this.pendingRequests.delete(String(o)),r(new Error(`Request timeout: ${e}`))},i);this.pendingRequests.set(String(o),{resolve:p=>{p.error?r(new Error(`JSON-RPC error: ${p.error.message}`)):(s?.(),n(p.result))},reject:r,timer:h});const f=JSON.stringify(d);this.process.stdin.write(`${f}
|
|
1
|
+
import{execFileSync as xe}from"node:child_process";import{createInterface as ye}from"node:readline";import{EventEmitter as te}from"node:events";import{stat as ie}from"node:fs/promises";import{join as D,resolve as Ce}from"node:path";import{fileURLToPath as _e}from"node:url";import{syncDefaultSkillsToDir as Ae}from"../../default-skills/index.js";import{resolveCommandPath as Se,resolveShimInvocation as Ee,spawnCommand as be,killProcessGroup as L,hasChildProcesses as Re}from"../../core/runtime/spawn.js";import{InternalApiServer as ke}from"../../core/mcp/internal-api-server.js";import{resolveMcpServerName as Me,LEGACY_MCP_SERVER_NAMES as Pe}from"../../core/mcp/server-name.js";import{formatInboundMessageReferenceText as F}from"../../core/protocol/message-reference.js";import{INBOUND_MESSAGE_ID_PROTOCOL_INSTRUCTION as j}from"../shared/inbound-message-id-injector.js";import{log as d}from"../../core/log/index.js";import{resolveClientVersion as De}from"../../core/util/client-version.js";import{isThinkingAgentMessagePhase as H,isUserVisibleAgentMessagePhase as O}from"../../core/util/codex-output-policy.js";import{SessionBindingStore as Oe}from"../../core/persistence/session-binding-store.js";import{ensureCodexProjectTrusted as $e,isCodexCommand as Ne,readCodexProviderSettings as U,resolveCodexHome as Le}from"./codex-trust.js";import{prepareCodexDirectHome as Fe}from"./direct-home.js";import{checkCodexSessionActivity as je,inspectCodexTurnProgress as He}from"./session-activity.js";import{CodexWaitLoopTracker as se}from"./wait-loop.js";import{findRolloutFile as Ue}from"./rollout-locator.js";import{ensureRolloutInPrivateHome as qe}from"./rollout-import.js";import{DEFAULT_RESUME_RETRY_DELAYS_MS as We,formatResumeError as q,isCodexThreadMissingError as Be}from"./thread-resume.js";import{archiveCodexThreadIfOwned as ze}from"./thread-archive.js";import{checkCodexLock as ne}from"./lock-monitor.js";import{resolveCliPath as re,getCliVersion as Ge}from"../../core/util/cli-probe.js";import{scanSkills as Ve}from"../claude/skill-scanner.js";import{normalizeCodexConfigOverrides as oe}from"./config-overrides.js";import{buildCodexOpenAiCompatibleProviderOverrides as Xe,CODEX_RELAY_OPENAI_PROVIDER_ID as Je,prepareCodexRawCapture as Ye,resolveCodexProviderName as Ke,resolveCodexRawCaptureTarget as Ze,resolveCodexProviderSupportsWebsocketsConfigKey as Qe}from"./raw-capture-hook.js";import{unavailableRawApiCaptureBoundary as et}from"../../audit/raw-capture/capture-relay.js";const ae=60*1e3,tt=600*1e3,it=300*1e3,st="GRIX_RELAY_PROXY",nt=5*1e3,rt=15*1e3,de=3;function W(c,e){const t=Number(process.env[c]);return Number.isFinite(t)&&t>0?t:e}function ot(c){const e=Number(process.env[c]);return Number.isFinite(e)&&e>0?e:null}function at(c){if(!c||typeof c!="object"||Array.isArray(c))return;const e=c,t=typeof e.auditId=="string"?e.auditId.trim():"",i=typeof e.sessionId=="string"?e.sessionId.trim():"",s=typeof e.spoolRootDir=="string"?e.spoolRootDir.trim():"";return t&&i&&s?{auditId:t,sessionId:i,spoolRootDir:s}:void 0}const B=W("GRIX_CODEX_PREFLIGHT_STALL_TIMEOUT_MS",120*1e3),dt=20,ct=8e3,ce=2*1024,le=`
|
|
2
|
+
\u2026(truncated)`,ue=80,he=120*1e3,z=600*1e3,lt=60*1e3,ut=300*1e3,ht=W("GRIX_CODEX_DELTA_COALESCE_MS",1e3),pt=W("GRIX_CODEX_DELTA_COALESCE_CHARS",4096);function ft(c){return`codex_${c}`}const mt=[{id:"gpt-5.3-codex",displayName:"GPT-5.3 Codex",defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!0}],pe=[{id:"default",displayName:"Default"},{id:"plan",displayName:"Plan"}],gt=new Set(["rawResponse/completed","codex/rawResponse/completed","rawResponseItem/completed","codex/rawResponseItem/completed","thread/tokenUsage/updated","codex/thread/tokenUsage/updated","thread/compacted","codex/thread/compacted","turn/started","codex/turn/started","turn/completed","codex/turn/completed","item/started","codex/item/started","item/completed","codex/item/completed","error","codex/error"]);async function fe(c,e){const t=String(c??"").trim()||"codex",i=Se(t,e);if(i!==t)return i;if(t==="codex"){const n=process.platform==="win32"?[]:["/opt/homebrew/bin/codex","/usr/local/bin/codex","/usr/bin/codex"],r=process.platform!=="win32"?[]:[D(process.env.LOCALAPPDATA??"","npm","codex.cmd"),D(process.env.APPDATA??"","npm","codex.cmd"),D(process.env.LOCALAPPDATA??"","Programs","codex","codex.exe")];for(const o of[...n,...r])try{return require("node:fs").accessSync(o,require("node:fs").constants.X_OK),o}catch{continue}}const s=await re(t);return s||(d.warn("codex-adapter",`resolveCodexCommandPath: failed to resolve "${t}" via PATH, codex fallbacks, or resolveCliPath. envPath=${e?"provided":"missing (using process.env.PATH)"}`),t)}class _ extends te{type="codex";config;callbacks;process=null;alive=!1;stopped=!1;internalApi=null;bridgeStatus="starting";pendingRequests=new Map;requestId=0;threadId=null;initialized=!1;cwd;approvalPolicy;sandboxMode;model;collaborationMode;reasoningEffort;serviceTier;idleTimer=null;waitLoopTimer=null;waitLoopCheckRunning=!1;waitLoopTracker=new se;waitLoopProgressGeneration=0;waitLoopChildTools=new Set;rejectedTurnStarts=new Set;startingEventId=null;rejectedNativeOwner=null;rejectedNativeTimer=null;orphanTurnTimer=null;orphanTurnSince=0;lastProgressAt=0;lastIdleCheckAt=0;activityGeneration=0;lastIdleCheckGeneration=0;idleNoProgressCount=0;inFlightToolOps=0;consecutivePingFailures=0;currentTurnStartedAt=0;preflightCheckComplete=!1;turnInactivityTimeoutMs=ot("GRIX_CODEX_TURN_INACTIVITY_TIMEOUT_MS");pendingApprovals=new Map;needsHistoryInjection=!1;lastInjectedSessionId=null;identityInputInjectedThreadId=null;protocolInstructionInputInjectedThreadId=null;legacyDeveloperInstructionsUnsupported=!1;profileIdentityRefreshPending=!1;bindingStore=null;aibotSessionId="";threadResumePending=!1;resumeRetryDelaysMs=We;rolloutImportUserHome;autoTrustProject="auto";codexHome;codexDirect=null;directEnv={};configOverrideArgs=[];rawCaptureOptions;rawCaptureRelay;rawCaptureConfigOverrides=[];rawCaptureUnavailableReason;codexModelOptions=[];codexModelOptionsCachedAt=0;static MODEL_CACHE_TTL_MS=3600*1e3;rateLimitSnapshot=null;lastNonDefaultLimitRefreshAt=0;static NON_DEFAULT_LIMIT_REFRESH_INTERVAL_MS=6e4;currentThreadTokenUsage=null;currentModelContextWindow=null;auditCaptureByEventId=new Map;constructor(e,t){super(),this.config=e,this.callbacks=t;const i=e.options??{};this.approvalPolicy=i.approvalPolicy??"never",this.sandboxMode=typeof i.sandboxMode=="string"&&i.sandboxMode.trim()&&i.sandboxMode.trim()!=="default"?i.sandboxMode.trim():void 0,this.aibotSessionId=String(i.aibotSessionId??"").trim(),this.bindingStore=i.bindingStore instanceof Oe?i.bindingStore:null,this.model=i.model,this.collaborationMode=i.collaborationMode,this.reasoningEffort=i.reasoningEffort,this.serviceTier=i.serviceTier,this.bindingStore&&this.aibotSessionId&&(this.model=this.model??this.bindingStore.getCodexModelId(this.aibotSessionId),this.collaborationMode=this.collaborationMode??this.bindingStore.getCodexModeId(this.aibotSessionId),this.reasoningEffort=this.reasoningEffort??this.bindingStore.getCodexReasoningEffort(this.aibotSessionId),this.serviceTier=this.serviceTier??this.bindingStore.getCodexServiceTier(this.aibotSessionId),this.sandboxMode=this.sandboxMode??this.bindingStore.getCodexSandboxMode(this.aibotSessionId)),this.autoTrustProject=typeof i.autoTrustProject=="boolean"?i.autoTrustProject:"auto",this.codexHome=typeof i.codexHome=="string"&&i.codexHome.trim()?i.codexHome.trim():void 0;const s=i.codexDirect;if(this.codexDirect=s&&typeof s=="object"&&!Array.isArray(s)?s:null,this.configOverrideArgs=oe(i.codexConfigOverrides,"codexConfigOverrides"),this.rawCaptureOptions=at(i.rawApiCapture),this.cwd=this.resolveCwd(),this.bindingStore&&this.aibotSessionId){const n=this.bindingStore.getCodexThreadId(this.aibotSessionId);n&&(this.threadId=n,this.threadResumePending=!0)}}resolveCwd(){if(this.bindingStore&&this.aibotSessionId){const e=this.bindingStore.get(this.aibotSessionId);if(e?.cwd)return e.cwd}return process.cwd()}async start(){await this.prepareDirectHome(),await this.ensureProjectTrusted(),await this.prepareRawApiCapture();try{await this.startInternalApiAndRegisterMcp(),await this.spawnCodex(),await this.initializeHandshake(),await this.notifyBindingReadyWithContext(),d.info("codex-adapter",`Ready (pid=${this.process?.pid})`)}catch(e){throw await this.stopRawApiCapture(),e}}async prepareRawApiCapture(){if(this.rawCaptureOptions)try{const e=await Ye(this.rawCaptureOptions,{adapterType:"codex",sessionId:this.rawCaptureOptions.sessionId,cwd:this.cwd,env:this.resolveChildEnv(),configOverrides:this.configOverrideArgs,codexHome:this.codexHome});this.rawCaptureRelay=e.relay,this.rawCaptureConfigOverrides=e.overlay.configOverrides??[],this.rawCaptureUnavailableReason=void 0,d.info("codex-adapter","Raw API audit capture relay is ready")}catch(e){this.rawCaptureRelay=void 0,this.rawCaptureConfigOverrides=[],this.rawCaptureUnavailableReason=e instanceof Error?e.message:String(e),d.warn("codex-adapter",`Raw API audit capture unavailable: ${this.rawCaptureUnavailableReason}`)}}async stopRawApiCapture(){const e=this.rawCaptureRelay;this.rawCaptureRelay=void 0,this.rawCaptureConfigOverrides=[],e&&await e.stop()}async prepareDirectHome(){if(!this.codexDirect)return;const e=await Fe(this.codexDirect);this.codexHome=e.codexHome,this.directEnv=e.env;const t=oe(e.configOverrides,"codexDirect.configOverrides");this.configOverrideArgs=[...this.configOverrideArgs,...t],d.info("codex-adapter",`Codex direct home ready: ${e.codexHome} (catalog=${e.catalogVersion??"none"}, token=${e.tokenDigest?`present sha256:${e.tokenDigest}`:"absent"})`)}resolveChildEnv(){const e={...process.env,...this.config.env};return this.codexHome&&(e.CODEX_HOME=this.codexHome),e}async ensureProjectTrusted(){if(this.shouldAutoTrustProject())try{const e=await $e(this.cwd,{codexHome:this.codexHome});e.changed&&d.info("codex-adapter",`Trusted Codex project ${this.cwd} in ${e.configPath}`)}catch(e){const t=e instanceof Error?e.message:String(e);throw d.error("codex-adapter",`Failed to trust Codex project ${this.cwd}: ${t}`),e}}async startInternalApiAndRegisterMcp(){try{this.internalApi=new ke,this.internalApi.setInvokeHandler(async(a,h,f,p)=>this.callbacks.agentInvoke(a,h,p)),await this.internalApi.start(0),d.info("codex-adapter",`Internal API started at ${this.internalApi.url}`);const e=this.getMcpConfig(),t=this.resolveChildEnv(),i=typeof t.PATH=="string"?t.PATH:void 0,s=await fe(this.config.command||"codex",i),n=a=>{const h=Ee(s,a);xe(h.command,h.args,{env:t,timeout:1e4,stdio:"ignore",windowsHide:!0,windowsVerbatimArguments:h.windowsVerbatimArguments})};for(const a of[...Pe,e.name])try{n(["mcp","remove",a])}catch{}n(["mcp","add",e.name,"--",e.command,...e.args]),d.info("codex-adapter",`Registered MCP server: ${e.name}`);const r=D(Le(this.codexHome),"skills"),o=Ae(r);o.length>0&&d.info("codex-adapter",`Synced connector skills to ${r}: [${o.join(", ")}]`)}catch(e){d.warn("codex-adapter",`Failed to register MCP tools (non-fatal): ${e instanceof Error?e.message:String(e)}`)}}shouldAutoTrustProject(){return this.autoTrustProject===!1?!1:this.autoTrustProject===!0?!0:Ne(this.config.command)}notifyBindingReady(){!this.aibotSessionId||!this.cwd||this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd)}async notifyBindingReadyWithContext(){if(!(!this.aibotSessionId||!this.cwd))try{this.callbacks.sendUpdateBindingCard(this.aibotSessionId,"ready",this.cwd,await this.buildToolbarContextResult("binding_ready"))}catch(e){d.warn("codex-adapter",`Failed to attach toolbar context to binding update: ${e instanceof Error?e.message:String(e)}`),this.notifyBindingReady()}}scheduleToolbarContextPush(){this.notifyBindingReadyWithContext()}externalProviderCheckedAt=0;externalProviderCached=!1;static EXTERNAL_PROVIDER_CACHE_TTL_MS=60*1e3;isExternalProvider(){const e=Date.now();if(e-this.externalProviderCheckedAt<_.EXTERNAL_PROVIDER_CACHE_TTL_MS)return this.externalProviderCached;const t=U(this.codexHome);return this.externalProviderCached=!!(t.baseUrl&&ve(t.baseUrl)),this.externalProviderCheckedAt=e,this.externalProviderCached}effectiveServiceTier(){if(!this.serviceTier||this.serviceTier.trim().toLowerCase()==="default"||this.isExternalProvider())return;const e=this.getModelOption(this.currentModelId());if(!(e&&!e.serviceTiers.some(t=>t.id===this.serviceTier)))return this.serviceTier}resolveReasoningEffortForModel(e,t,i={}){const s=String(e??"").trim();if(!s||this.isExternalProvider())return;const n=this.getModelOption(t),r=n?.supportedReasoningEfforts??[];if(r.length===0)return;const o=r.find(a=>a.trim().toLowerCase()===s.toLowerCase());if(o)return o;if(!i.strict&&["max","ultra"].includes(s.toLowerCase())){const a=r.find(h=>h.trim().toLowerCase()==="xhigh");if(a)return a}return i.strict?void 0:n?.defaultReasoningEffort??void 0}effectiveReasoningEffort(){return this.resolveReasoningEffortForModel(this.reasoningEffort,this.currentModelId())}getEffortMeta(){if(this.isExternalProvider())return{};const e=this.currentModelId(),t=this.getModelOption(e),i={},s=t?.supportedReasoningEfforts??[];s.length>0&&(i.available_efforts=s,i.reasoning_effort=this.effectiveReasoningEffort()??t?.defaultReasoningEffort??null);const n=t?.serviceTiers??[];return n.length>0&&(i.available_service_tiers=n,i.service_tier=this.effectiveServiceTier()??t?.defaultServiceTier??null),i}async stop(){this.stopped=!0,this.alive=!1,this.stopComposing(),this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.stopOrphanTurnWatch(),this.currentTurnId=null,this.finishActiveInputs("canceled","adapter stopped"),this.rejectAllPending("adapter stopped"),this.pendingAutoCompact=!1,this.clearNativeSettlement("adapter stopped"),this.finishCompaction({status:"failed",message:"adapter stopped"});for(const t of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed","adapter stopped");this.internalApi&&(await this.internalApi.stop(),this.internalApi=null);const e=this.process;if(this.process=null,e?.pid&&typeof e.once=="function")L(e,"SIGTERM"),await Promise.race([new Promise(i=>{e.once("exit",()=>i(!0))}),new Promise(i=>{setTimeout(()=>i(!1),5e3)})])||L(e,"SIGKILL");else if(e)try{e.kill("SIGTERM")}catch{}await this.stopRawApiCapture()}isAlive(){return this.alive}async createSession(e){if(this.initialized||await this.initializeHandshake(),this.threadId||await this.startNewThread(),!this.threadId)throw new Error("Failed to create session: thread ID is missing");return await this.notifyBindingReadyWithContext(),this.threadId}async resumeSession(e,t){await this.ensureThreadResumed()}async destroySession(e){this.threadId=null,this.threadResumePending=!1,this.persistThreadId(void 0),this.protocolInstructionInputInjectedThreadId=null}sendPrompt(e){const t=new J(e.adapterSessionId);return this.runTurn(e,t).catch(i=>{t.emitError(i instanceof Error?i:new Error(String(i)))}),t}async cancel(e){if(this.compacting&&this.finishCompaction({status:"failed",message:"Compaction interrupted"},!1),this.threadId)try{await this.sendRequest("turn/interrupt",{threadId:this.threadId,turnId:this.currentTurnId??""},5e3)}catch{}}setPermissionHandler(e){this.permissionHandler=e}permissionHandler=null;async ping(e){if(!this.alive||!this.process)return!1;if(!this.process.pid){const t=!!(this.process.stdin&&!this.process.stdin.destroyed),i=!!(this.process.stdout&&!this.process.stdout.destroyed);return t&&i}if(!this.process.stdin||this.process.stdin.destroyed)return!!(this.process.stdout&&!this.process.stdout.destroyed);try{const t=++this.requestId,i={jsonrpc:"2.0",id:t,method:"ping",params:{}};return await new Promise(s=>{const n=setTimeout(()=>{this.pendingRequests.delete(String(t)),s(!1)},e);this.pendingRequests.set(String(t),{resolve:()=>s(!0),reject:()=>s(!1),timer:n}),this.process.stdin.write(`${JSON.stringify(i)}
|
|
3
|
+
`)})}catch{return!1}}getStatus(){return{alive:this.alive,busy:this.currentTurnId!==null||this.compacting,sessions:this.threadId?1:0}}getActiveEventIds(){return this.activeEventId?[this.activeEventId]:[]}clearActiveEventForShutdown(){this.activeInputs.clear(),this.clearIdleTimer(),this.clearWaitLoopWatch(),this.clearCodexDeltaBuffer(),this.activeEventId=null}rejectEventOutput(e){if(this.compactionDeferred=this.compactionDeferred.filter(t=>t.event_id!==e),this.steerDeferred=this.steerDeferred.filter(t=>t.event_id!==e),this.activeInputs.delete(e)){this.emit("eventDone",e);return}this.activeEventId===e&&(this.clearCodexDeltaBuffer(),this.pendingAutoCompact=!1,this.currentTurnId?this.threadId&&this.interruptRejectedTurn(e,this.threadId,this.currentTurnId):this.startingEventId===e&&(this.rejectedTurnStarts.add(e),this.emit("pauseIntake","barrier")),this.clearActive({runPendingAutoCompact:!1}))}interruptRejectedTurn(e,t,i){const s=this.rejectedNativeOwner;if(s?.eventId===e&&s.threadId===t&&s.turnId===i&&s.process===this.process)return;this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer);const n={eventId:e,threadId:t,turnId:i,process:this.process,terminationAttempted:!1};this.rejectedNativeOwner=n,this.rejectedNativeTimer=setTimeout(()=>{this.rejectedNativeOwner===n&&(this.rejectedNativeTimer=null,this.terminateRejectedNativeOwner(n))},z),this.rejectedNativeTimer.unref?.(),this.sendRequest("turn/interrupt",{threadId:t,turnId:i},5e3).catch(()=>{this.terminateRejectedNativeOwner(n)})}terminateRejectedNativeOwner(e){if(!(this.rejectedNativeOwner!==e||e.terminationAttempted||!e.process||this.process!==e.process||this.threadId!==e.threadId||this.currentTurnId!==e.turnId||this.activeEventId||this.stopped||!this.alive)){e.terminationAttempted=!0,this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer),this.rejectedNativeTimer=null;try{L(e.process,"SIGTERM")}catch(t){d.error("codex-adapter",`Rejected producer termination failed; retaining native barrier: ${t instanceof Error?t.message:String(t)}`)}}}setModel(e){this.model=e,this.reasoningEffort=this.resolveReasoningEffortForModel(this.reasoningEffort,this.currentModelId()),this.persistCodexContext(),d.info("codex-adapter",`Model set to: ${e}`),this.scheduleToolbarContextPush()}setMode(e){const t=P(e);if(!t){d.info("codex-adapter",`Ignoring unsupported mode: ${e}`);return}this.collaborationMode=t,this.persistCodexContext(),d.info("codex-adapter",`Mode set to: ${t}`),this.scheduleToolbarContextPush()}setReasoningEffort(e){const t=this.resolveReasoningEffortForModel(e,this.currentModelId(),{strict:!0});if(!t){d.info("codex-adapter",`Ignoring unsupported reasoning effort for ${this.currentModelId()}: ${e}`);return}this.reasoningEffort=t,this.persistCodexContext(),d.info("codex-adapter",`Reasoning effort set to: ${t}`)}setServiceTier(e){const t=e.trim();this.serviceTier=t.toLowerCase()==="default"?"default":t,this.persistCodexContext(),d.info("codex-adapter",`Service tier set to: ${this.serviceTier}`)}setSandboxMode(e){this.sandboxMode=e,this.persistCodexContext(),d.info("codex-adapter",`Sandbox mode set to: ${e} (applied on next restart)`)}async threadCompact(){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/compact/start",{threadId:this.threadId},12e4)}async threadRollback(e){if(!this.threadId)throw new Error("No active thread");return this.sendRequest("thread/rollback",{threadId:this.threadId,numTurns:e},1e4)}getThreadId(){return this.threadId}getCodexHome(){return this.codexHome}async archiveOwnedThread(e){const t=this.alive&&this.initialized&&this.process?.stdin?async()=>{await this.sendRequest("thread/archive",{threadId:e},15e3)}:void 0;return ze({threadId:e,codexHome:this.codexHome,liveArchive:t,command:this.config.command||"codex"})}hasRawApiCaptureRelay(){return!!this.rawCaptureRelay}enableAuditCapture(e,t={}){const i=e.trim();if(!i||this.auditCaptureByEventId.has(i))return;const s=t.rawProviderBody??!!this.rawCaptureOptions;this.auditCaptureByEventId.set(i,{auditId:t.auditId,threadId:this.threadId??void 0,modelId:this.currentModelId()??void 0,rawProviderBody:s,sequence:0,events:[]}),s&&this.rawCaptureRelay?.beginEvent(i,t.auditId)}takeAuditBoundary(e){const t=this.auditCaptureByEventId.get(e);if(t)return this.auditCaptureByEventId.delete(e),{adapterType:this.type,providerTurnId:t.providerTurnId,threadId:t.threadId??this.threadId??void 0,...t.sharedTurnOwner?{sharedTurnOwner:t.sharedTurnOwner}:{},codexEvidence:{events:t.events,...t.modelId?{modelId:t.modelId}:{},...t.rawProviderBody?{rawApiCapture:this.rawCaptureRelay?this.rawCaptureRelay.takeEvent(e):et(this.rawCaptureUnavailableReason??"raw_capture_relay_not_prepared_before_adapter_start")}:{}}}}getMcpConfig(){if(!this.internalApi)return null;const e=Ce(_e(import.meta.url),"../../../mcp/acp-mcp-server.js");return{name:Me(this.callbacks.getAgentId?.()),command:process.execPath,args:[e,"--api-url",this.internalApi.url]}}async hasBackgroundWork(){const e=this.process?.pid;return e?Re(e,[e]):!1}onAgentProfileChanged(){this.threadId&&(this.profileIdentityRefreshPending=!0,this.identityInputInjectedThreadId=null,d.info("codex-adapter",`agent profile changed; scheduling identity refresh for thread=${this.threadId}`))}async probe(e){const t=this.config.command||"codex",i=await re(t),s=i!==null;let n=null,r;if(s){const v=await Ge(t);n=v.version,v.error&&(r=v.error)}else r={code:"cli_not_found",message:`command not found: ${t}`};const a=U(this.codexHome).baseUrl??null,h=this.currentModelId()||null,f=this.getStatus(),p=e?.conversation?{attempted:!0,ok:!1,latency_ms:null,error:{code:"unsupported",message:"codex does not support minimal prompt probe"}}:{attempted:!1,ok:!1,latency_ms:null},g=await this.probeSessionRecord(),T=ne({codexHome:this.codexHome});return{cli:{command:t,installed:s,path:i,version:n,...r?{error:r}:{}},conversation:p,config:{model:h,base_url:a,source:{model:h?"runtime":"unknown",base_url:a?"file":"unknown"}},process:{started:!!this.process,alive:f.alive,busy:f.busy},session:g,lock:{present:T.present,locked:T.locked,stale:T.stale,path:T.path}}}async probeSessionRecord(){const e=this.threadId?Ue(this.threadId,this.codexHome):null;if(!e)return{recordPath:null,lastActivityMs:null,freshMs:null};try{const t=await ie(e);return{recordPath:e,lastActivityMs:t.mtimeMs,freshMs:Date.now()-t.mtimeMs}}catch{return{recordPath:e,lastActivityMs:null,freshMs:null}}}getSupportedCommands(){return[{name:"compact",description:"Compact thread to reduce context size"},{name:"model",description:"List or set model",args:"[model_id]"},{name:"mode",description:"List or set collaboration mode",args:"[default|plan]"},{name:"rollback",description:"Roll back thread turns",args:"[num_turns]"},{name:"interrupt",description:"Interrupt current turn"},{name:"rate_limits",description:"Show current rate limits"},{name:"status",description:"Show thread and session status"},{name:"skills",description:"List available skills"}]}async execCommand(e,t,i){try{switch(e){case"compact":{if(!this.threadId)return{status:"failed",message:"No active thread"};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};if(this.activeEventId&&!this.currentTurnId)return{status:"failed",message:"agent busy"};if(this.currentTurnId&&await this.cancelActiveTurnForCompaction(),await this.ensureThreadResumed(),this.currentTurnId||this.activeEventId)return{status:"failed",message:"agent busy"};if(this.needsHistoryInjection)return{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};if(this.compacting)return{status:"failed",message:"Compaction already in progress"};try{const s=await this.runCompaction();if(s.status!=="ok")throw new Error(s.message);return s}catch(s){const n=s instanceof Error?s.message:String(s);if(/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n))return this.threadId=null,this.persistThreadId(void 0),{status:"failed",message:"The session has expired and there is no history to compact. Please send a new message and try again."};throw s}}case"model":{const s=t.trim();return s?this.getModelOptions().some(r=>r.id===s)?(this.setModel(s),{status:"ok",message:`Model set to ${s}`}):{status:"failed",message:`Unknown model: ${s}`}:{status:"ok",message:`Current: ${this.currentModelId()}`,data:{models:this.getModelOptions()}}}case"mode":{const s=t.trim();if(s){const n=P(s);return n?(this.setMode(n),{status:"ok",message:`Mode set to ${n}`}):{status:"failed",message:`Unknown mode: ${s}. Supported: default, plan`}}return{status:"ok",message:`Current: ${this.collaborationMode??"default"}`,data:{modes:pe}}}case"rollback":{if(!this.threadId)return{status:"failed",message:"No active thread"};const s=Math.max(1,parseInt(t.trim(),10)||1);return this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","rolled back"),this.clearActive()),await this.threadRollback(s),{status:"ok",message:`Rolled back ${s} turn(s)`}}case"interrupt":{if(!this.threadId)return{status:"failed",message:"No active thread"};const s=this.compacting;return await this.cancel(this.threadId),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","interrupted"),s||this.clearActive(),{status:"ok",message:"Turn interrupted"}}case"rate_limits":return{status:"ok",message:this.rateLimitSnapshot?`Primary: ${this.rateLimitSnapshot.primary.usedPercent.toFixed(1)}%, Secondary: ${this.rateLimitSnapshot.secondary.usedPercent.toFixed(1)}%`:"Rate limits not available",data:{rateLimits:this.rateLimitSnapshot}};case"status":return{status:"ok",message:`Thread: ${this.threadId??"none"}, Model: ${this.currentModelId()}, Mode: ${this.collaborationMode??"default"}`,data:{threadId:this.threadId,model:this.currentModelId(),mode:this.collaborationMode??"default",cwd:this.cwd,alive:this.alive}};case"skills":{const s=Ve({mode:"codex",projectDir:this.cwd}),n=s.map(r=>`- ${r.name}${r.trigger?` (${r.trigger})`:""}: ${r.description}`);return{status:"ok",message:n.length>0?n.join(`
|
|
4
|
+
`):"No skills found",data:s}}default:return{status:"unsupported",message:`Unknown command: ${e}`}}}catch(s){return{status:"failed",message:s instanceof Error?s.message:String(s)}}}async handleLocalAction(e){const t=e.action_type??"",i=e.params??{};switch(d.info("codex-adapter",`handleLocalAction action_type=${t} action_id=${e.action_id} session_id=${i.session_id??""}`),t){case"set_model":{const s=w(u(i.model_id),u(i.modelId),u(i.value));return s&&this.setModel(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("model_set")),{handled:!0,kind:"set_model"}}case"set_mode":{const s=i.mode_id;return s&&this.setMode(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("mode_set")),{handled:!0,kind:"set_mode"}}case"set_reasoning_effort":{const s=w(u(i.reasoning_effort),u(i.reasoning_eff),u(i.effort)),n=this.resolveReasoningEffortForModel(s,this.currentModelId(),{strict:!0});if(!n){const r=this.getModelOption(this.currentModelId())?.supportedReasoningEfforts??[];return this.callbacks.sendLocalActionResult(e.action_id,"failed",{outcome:"effort_rejected",model_id:this.currentModelId(),available_efforts:r},"unsupported_reasoning_effort",r.length>0?`reasoning_effort must be one of: ${r.join(", ")}`:`reasoning_effort is not supported by model ${this.currentModelId()}`),{handled:!0,kind:"set_reasoning_effort_rejected"}}return this.setReasoningEffort(n),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("effort_set",!1)),{handled:!0,kind:"set_reasoning_effort"}}case"set_service_tier":{const s=w(u(i.service_tier),u(i.serviceTier),u(i.value));return s&&this.setServiceTier(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("service_tier_set",!1)),{handled:!0,kind:"set_service_tier"}}case"set_sandbox_mode":{const s=w(u(i.sandbox_mode),u(i.sandboxMode),u(i.value));return s&&this.setSandboxMode(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",await this.buildToolbarContextResult("sandbox_mode_set",!1)),{handled:!0,kind:"set_sandbox_mode"}}case"thread_compact":{if(d.info("codex-adapter",`thread_compact start action_id=${e.action_id} threadId=${this.threadId}`),this.compacting)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,"Compaction already in progress"),{handled:!0,kind:"thread_compact"};if(this.activeEventId&&!this.currentTurnId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,"agent busy"),{handled:!0,kind:"thread_compact"};this.currentTurnId&&await this.cancelActiveTurnForCompaction();try{if(await this.ensureThreadResumed(),this.needsHistoryInjection)return d.info("codex-adapter",`thread_compact aborted: thread expired, new thread created action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again."),{handled:!0,kind:"thread_compact"};if(this.currentTurnId||this.activeEventId||this.compacting)throw new Error("agent busy");const s=await this.runCompaction();if(s.status!=="ok")throw new Error(s.message);d.info("codex-adapter",`thread_compact done action_id=${e.action_id}`),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(s){const n=s instanceof Error?s.message:String(s);/thread.*(not found|not exist|unknown|invalid)/i.test(n)||/not found.*thread/i.test(n)?(d.warn("codex-adapter",`thread_compact thread expired, resetting threadId action_id=${e.action_id} err=${n}`),this.threadId=null,this.persistThreadId(void 0),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"thread_expired","The session has expired and there is no history to compact. Please send a new message and try again.")):(d.info("codex-adapter",`thread_compact failed action_id=${e.action_id} err=${n}`),this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n))}return{handled:!0,kind:"thread_compact"}}case"get_context":return this.callbacks.sendLocalActionResult(e.action_id,"ok",{threadId:this.threadId,model:this.model,mode:this.collaborationMode,approvalPolicy:this.approvalPolicy,cwd:this.cwd,...await this.buildToolbarContextResult("context")}),{handled:!0,kind:"get_context"};case"exec_approve":case"file_approve":{const s=this.resolvePendingApproval(e);if(!s)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t};const n=i.decision||"allow-once",r=me[n]??"accept";return this.sendApprovalDecision(s.requestId,r),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,approval_command_id:s.approvalCommandId,decision:n}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t}}case"exec_reject":case"file_reject":{const s=this.resolvePendingApproval(e);return s?(this.sendApprovalDecision(s.requestId,"deny"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,approval_command_id:s.approvalCommandId,decision:"deny"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t}):(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That approval request is no longer pending."),{handled:!0,kind:t})}case"permission_approve":{const s=this.resolvePendingApproval(e);return!s||s.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(s.requestId,"accept"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,decision:"approve"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t})}case"permission_reject":{const s=this.resolvePendingApproval(e);return!s||s.kind!=="permission"?(this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"unknown_or_expired_approval_id","That permission approval request is no longer pending."),{handled:!0,kind:t}):(this.sendApprovalDecision(s.requestId,"deny"),this.pendingApprovals.delete(s.approvalId),this.callbacks.sendLocalActionResult(e.action_id,"ok",{approval_id:s.approvalId,decision:"deny"}),this.resumeAfterApproval(e,i,s),{handled:!0,kind:t})}case"turn_interrupt":{try{await this.cancel(this.threadId??""),this.callbacks.sendLocalActionResult(e.action_id,"ok")}catch(s){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,s instanceof Error?s.message:String(s))}return{handled:!0,kind:"turn_interrupt"}}case"thread_rollback":{const s=Number(i.numTurns??i.num_turns??1);if(!this.threadId)return this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,"no_thread","No active thread"),{handled:!0,kind:"thread_rollback"};this.currentTurnId&&(await this.cancel(this.threadId).catch(()=>{}),this.activeEventId&&this.sendEventResultAfterFlushing(this.activeEventId,"canceled","rolled back"),this.clearActive());try{await this.threadRollback(s),this.callbacks.sendLocalActionResult(e.action_id,"ok",{numTurns:s})}catch(n){this.callbacks.sendLocalActionResult(e.action_id,"failed",void 0,void 0,n instanceof Error?n.message:String(n))}return{handled:!0,kind:"thread_rollback"}}case"get_rate_limits":{const s=this.rateLimitSnapshot,n=this.buildContextWindowSnapshot(),r=Date.now(),o=this.currentThreadTokenUsage;return s?d.info("codex-adapter",`[rate-limits] responding: primary=${s.primary.usedPercent.toFixed(1)}% window=${s.primary.windowMinutes}min resetsAt=${s.primary.resetsAt} secondary=${s.secondary.usedPercent.toFixed(1)}% window=${s.secondary.windowMinutes}min resetsAt=${s.secondary.resetsAt}`):d.info("codex-adapter","[rate-limits] responding: no rateLimitSnapshot available"),d.debug("codex-adapter",`[cp-diagnose] get_rate_limits: threadId=${this.threadId??"-"} contextWindow=${JSON.stringify(n)} tokenUsage=${JSON.stringify(o)}`),this.callbacks.sendLocalActionResult(e.action_id,"ok",{adapterType:"codex",available:s!==null,cached:!1,sampledAt:r,rateLimits:s,contextWindow:n,tokenUsage:o}),{handled:!0,kind:"get_rate_limits"}}default:return{handled:!1,kind:""}}}async buildToolbarContextResult(e,t=!0){t&&await this.refreshCodexModelOptions();const i=P(this.collaborationMode)??"default",s=this.currentModelId(),n=this.getModelOption(s),r=this.getModelOptions(),o=pe,a=this.effectiveReasoningEffort()??n?.defaultReasoningEffort??null,h=n?.supportedReasoningEfforts??[],f=this.effectiveServiceTier()??n?.defaultServiceTier??null,p=n?.serviceTiers??[],g=this.isExternalProvider(),T=g?[]:h,v=g?null:a,x=g?[]:p,y=g?null:f,S={outcome:e,session_context:{modelId:s,modeId:i,reasoningEffort:v,serviceTier:y,approvalPolicy:this.approvalPolicy,sandboxMode:this.sandboxMode??null},model_id:s,mode_id:i,currentModelId:s,currentModeId:i,available_models:r,available_modes:o,available_efforts:T,availableModels:r,availableModes:o,reasoning_effort:v,available_service_tiers:x,service_tier:y,sandbox_mode:this.sandboxMode??null,models:r.map(C=>({modelId:C.id,name:C.displayName})),modes:o.map(C=>({id:C.id,name:C.displayName}))};return this.rateLimitSnapshot&&(S.rate_limit_primary_percent=this.rateLimitSnapshot.primary.usedPercent,S.rate_limit_secondary_percent=this.rateLimitSnapshot.secondary.usedPercent,S.rate_limit_primary_window_min=this.rateLimitSnapshot.primary.windowMinutes,S.rate_limit_secondary_window_min=this.rateLimitSnapshot.secondary.windowMinutes),S}currentModelId(){const e=String(this.model??"").trim();if(e)return e;const t=this.getModelOptions();return t.find(i=>i.isDefault)?.id??t[0]?.id??""}getModelOption(e){const t=e.trim().toLowerCase();return this.getModelOptions().find(i=>i.id.trim().toLowerCase()===t)}getModelOptions(){const e=this.config.options??{},t=e.available_models??e.availableModels??e.models,i=Ct(t),s=this.codexModelOptions,n=[],r=String(this.model??"").trim(),o=r&&!s.some(p=>p.id.trim().toLowerCase()===r.toLowerCase()),a=s.length>0?s:o?[]:mt;if(r){const p=[...s,...i].find(g=>g.id.trim().toLowerCase()===r.toLowerCase());n.push({id:r,displayName:p?.displayName??k(r),defaultReasoningEffort:p?.defaultReasoningEffort??null,supportedReasoningEfforts:p?.supportedReasoningEfforts??[],defaultServiceTier:p?.defaultServiceTier??null,serviceTiers:p?.serviceTiers??[],isDefault:p?.isDefault??!1})}n.push(...i),n.push(...a);const h=[],f=new Set;for(const p of n){const g=p.id.trim(),T=g.toLowerCase();!g||f.has(T)||(f.add(T),h.push({id:g,displayName:p.displayName.trim()||g,defaultReasoningEffort:p.defaultReasoningEffort,supportedReasoningEfforts:[...p.supportedReasoningEfforts],defaultServiceTier:p.defaultServiceTier,serviceTiers:[...p.serviceTiers],isDefault:p.isDefault}))}return h}async refreshCodexModelOptions(){if(!this.initialized||!this.alive||!this.process?.stdin||Date.now()-this.codexModelOptionsCachedAt<_.MODEL_CACHE_TTL_MS)return;const e=U(this.codexHome);if(e.baseUrl&&ve(e.baseUrl)){try{const t=await At(e.baseUrl,e.apiKey);if(t.length>0){this.codexModelOptions=Ie(t),this.codexModelOptionsCachedAt=Date.now(),d.info("codex-adapter",`Loaded ${t.length} models from external provider ${e.baseUrl}`);return}}catch(t){d.warn("codex-adapter",`External provider /v1/models failed (${t instanceof Error?t.message:String(t)})`)}if(!_t(e.baseUrl)){this.codexModelOptions=[],this.codexModelOptionsCachedAt=Date.now();return}d.info("codex-adapter",`External provider ${e.baseUrl} returned no models; falling back to Codex model/list`)}try{const t=[];let i;do{const s=await this.sendRequest("model/list",{cursor:i??null,includeHidden:!1,limit:100},15e3),n=Et(s);t.push(...n.models),i=n.nextCursor}while(i);t.length>0&&(this.codexModelOptions=Ie(t),this.codexModelOptionsCachedAt=Date.now())}catch(t){d.warn("codex-adapter",`Failed to load Codex model list: ${t instanceof Error?t.message:String(t)}`)}}currentTurnId=null;compacting=!1;pendingAutoCompact=!1;compactionDeferred=[];steerDeferred=[];compactingTimer=null;compactionState=null;retiredTurnIds=new Set;nativeSettlement=null;nativeSettlementTimer=null;activeEventId=null;activeSessionId=null;visibleAgentMessageIds=new Set;hiddenAgentMessageIds=new Set;agentMessagePhases=new Map;deliveredAgentMessageIds=new Set;deliveredThinkingAgentMessageIds=new Set;static sequenceFloor=0;codexSequence=_.allocateCodexSequenceBase();pendingCodexDeltaFlush=null;turnError=null;turnErrorIsRetryableOnly=!0;turnHasVisibleOutput=!1;turnSawVisibleFinalAnswer=!1;turnSawNonEmptyFinalAnswer=!1;completedEmptyFinalAnswerItems=new Map;turnHasDeliveredFinalAnswer=!1;activeInputs=new Map;canAcceptActiveInput(e){const t=this.activeEventId?this.auditCaptureByEventId.get(this.activeEventId):void 0;return(e.audit?.enabled!==!0||!!t&&(!(e.audit.rawProviderBody??!!this.rawCaptureOptions)||t.rawProviderBody))&&this.alive&&!this.stopped&&this.initialized&&!!this.threadId&&!!this.currentTurnId&&!!this.activeEventId&&e.session_id===this.activeSessionId&&e.msg_type===1&&e.content.trim().length>0&&!this.compacting&&!this.nativeSettlement&&this.rejectedTurnStarts.size===0&&this.compactionDeferred.length===0&&this.steerDeferred.length===0}deliverActiveInput(e){if(this.activeInputs.has(e.event_id)||this.activeEventId===e.event_id)return;if(!this.canAcceptActiveInput(e)){this.sendEventResultAfterFlushing(e.event_id,"failed","Active Codex turn no longer accepts input; input was not submitted"),this.emit("eventDone",e.event_id);return}const t={accepted:!1};if(e.audit?.enabled===!0){this.enableAuditCapture(e.event_id,{auditId:e.audit.auditId,rawProviderBody:!1});const r=this.auditCaptureByEventId.get(e.event_id);r.providerTurnId=this.currentTurnId,r.sharedTurnOwner={eventId:this.activeEventId,auditId:this.auditCaptureByEventId.get(this.activeEventId)?.auditId}}this.activeInputs.set(e.event_id,t),this.emit("eventStarted",e.event_id,e.session_id);const i=F(e.content,{messageId:e.msg_id}),s={threadId:this.threadId,turnId:this.currentTurnId,inputEventId:e.event_id};this.captureActiveInputAudit(e.event_id,"turn/steer",s);const n=()=>{t.accepted=!0,this.captureActiveInputAudit(e.event_id,"turn/steer/accepted",s)};this.steerTurn(i,e.context_messages_json,n).then(()=>{this.activeInputs.get(e.event_id)===t&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++)}).catch(r=>{this.activeInputs.get(e.event_id)===t&&(this.activeInputs.delete(e.event_id),this.sendEventResultAfterFlushing(e.event_id,"failed",`Codex input delivery unconfirmed: ${r instanceof Error?r.message:String(r)}`),this.emit("eventDone",e.event_id))})}captureActiveInputAudit(e,t,i){const s=this.auditCaptureByEventId.get(e);s?.sharedTurnOwner&&s.events.push({sequence:++s.sequence,method:t,params:JSON.parse(JSON.stringify(i)),observedAt:new Date().toISOString()})}finishActiveInputs(e,t){const i=[...this.activeInputs];this.activeInputs.clear();for(const[s,n]of i){const r=e==="responded"&&!n.accepted;this.callbacks.sendEventResult(s,r?"failed":e,r?"Codex turn ended before input delivery was confirmed; check native history before retrying":t),this.emit("eventDone",s)}}deliverInboundEvent(e){if(this.stopped){this.sendEventResultAfterFlushing(e.event_id,"failed","adapter stopped");return}if(this.rejectedTurnStarts.size>0&&!this.nativeSettlement){this.compactionDeferred.push(e);return}if(this.nativeSettlement){this.nativeSettlement.waiting?this.compactionDeferred.push(e):(this.sendEventResultAfterFlushing(e.event_id,"failed","Native turn completion is unconfirmed; input was not submitted. Retry after native completion to recheck status."),this.checkNativeSettlement());return}if(this.compacting&&!this.compactionState?.resolve){this.sendEventResultAfterFlushing(e.event_id,"failed","Compaction completion is unconfirmed; input was not submitted. Retry after native completion to recheck status."),this.checkCompactionCompletion();return}if(this.compacting||this.compactionDeferred.length>0||this.activeEventId&&!this.currentTurnId){d.info("codex-adapter",`Event ${e.event_id} deferred: compaction in progress or inputs draining`),this.compactionDeferred.push(e);return}const t=F(e.content,{messageId:e.msg_id});if(this.currentTurnId&&t.trim()){d.info("codex-adapter",`Event ${e.event_id}: steering active turn ${this.currentTurnId}`);const i=this.activeEventId,s=this.currentTurnId;this.steerTurn(t).catch(n=>{if(this.nativeSettlement||this.compacting||this.activeEventId!==i||this.currentTurnId!==s){this.deliverInboundEvent(e);return}const r=n instanceof Error?n.message:String(n);if(d.info("codex-adapter",`Steer failed, falling through: ${r}`),kt(r)){d.info("codex-adapter","Unsteerable turn detected, closing local ownership and retaining native barrier"),i&&this.sendEventResultAfterFlushing(i,"failed",r),this.clearActive(),this.deliverInboundEvent(e);return}d.info("codex-adapter",`Steer failed but turn still active; deferring event ${e.event_id} for replay after turn ends`),this.steerDeferred.push(e)});return}if(this.currentTurnId){d.info("codex-adapter",`Event ${e.event_id} rejected: busy and empty content`),this.sendEventResultAfterFlushing(e.event_id,"failed","agent busy");return}this.startNewTurn(e,t)}startNewTurn(e,t){this.activeEventId=e.event_id,this.activeSessionId=e.session_id,this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,e.audit?.enabled===!0&&this.enableAuditCapture(e.event_id,{auditId:e.audit.auditId,rawProviderBody:e.audit.rawProviderBody}),this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.startComposing();const i=this.threadId??"",s=new J(i),n={adapterSessionId:i,text:t,contextMessages:e.context_messages_json?JSON.parse(e.context_messages_json).map(r=>({senderId:r.sender_id??"unknown",content:r.content})):void 0};this.runTurn(n,s,e.event_id,e.session_id).catch(r=>{this.activeEventId!==e.event_id||this.compacting||(d.error("codex-adapter",`Turn failed: ${r}`),this.sendEventResultAfterFlushing(e.event_id,"failed",r instanceof Error?r.message:String(r)),this.clearActive())}),this.resetIdleTimer(e.event_id)}deliverStopEvent(e,t){const i=this.compactionDeferred.findIndex(s=>s.event_id===e);if(i>=0){this.compactionDeferred.splice(i,1),this.sendEventResultAfterFlushing(e,"canceled","stopped by user");return}this.activeEventId&&(this.activeEventId===e||this.activeInputs.has(e))&&(this.cancel(this.threadId??"").catch(()=>{}),this.sendEventResultAfterFlushing(this.activeEventId,"canceled","stopped by user"),this.clearActive())}async spawnCodex(){const e={...this.resolveChildEnv(),...this.directEnv},t=typeof e.PATH=="string"?e.PATH:void 0;t||d.warn("codex-adapter",`spawnCodex: env.PATH is missing! process.env.PATH=${process.env.PATH?"set":"missing"}, config.env=${JSON.stringify(this.config.env)}`);const i=await fe(this.config.command||"codex",t),s=wt(this.config.args);for(const o of this.configOverrideArgs)s.push("-c",o);for(const o of this.resolveRelayTransportConfigOverrides())s.push("-c",o);for(const o of this.rawCaptureConfigOverrides)s.push("-c",o);this.sandboxMode&&s.push("-c",`sandbox_mode="${this.sandboxMode}"`);const n=s.map(o=>this.rawCaptureConfigOverrides.includes(o)?"<raw-api-capture-overlay>":o);d.info("codex-adapter",`Spawning: ${i} ${n.join(" ")}`);try{if(!(await ie(this.cwd)).isDirectory())throw new Error(`Bound path is not a directory: ${this.cwd}`)}catch(o){throw String(o?.code??"")==="ENOENT"?new Error(`Bound directory does not exist: ${this.cwd}. Please rebind with /grix open <valid-directory>.`):o}const r=ne({codexHome:this.codexHome});if(r.locked&&!r.stale)throw new Error(`Codex session appears locked by another process: ${r.path}`);r.stale&&d.warn("codex-adapter",`Ignoring stale Codex lock: ${r.path}`);try{this.process=be(i,s,{env:e,cwd:this.cwd}).process}catch(o){throw d.error("codex-adapter",`Codex spawn threw: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",o}this.process.on("error",o=>{d.error("codex-adapter",`Codex process spawn error: ${o}`),this.process=null,this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.currentTurnId=null,this.clearNativeSettlement("Codex process spawn failed"),this.finishCompaction({status:"failed",message:"Codex process spawn failed"}),this.rejectAllPending("spawn failed: "+(o instanceof Error?o.message:String(o))),this.stopComposing(),this.activeEventId?(this.flushCodexDeltaBuffer(),this.sendEventResultAfterFlushing(this.activeEventId,"failed","Codex process spawn failed"),this.activeEventId=null):this.clearCodexDeltaBuffer(),this.emit("exit",1)}),this.process.on("exit",o=>{d.info("codex-adapter",`Codex process exited (code=${o})`),this.alive=!1,this.bridgeStatus="closed",this.clearIdleTimer(),this.clearWaitLoopWatch(),this.rejectedTurnStarts.clear(),this.currentTurnId=null,this.clearNativeSettlement("Codex process exited"),this.finishCompaction({status:"failed",message:"Codex process exited"}),this.rejectAllPending("process exited"),this.stopComposing(),this.activeEventId?this.flushCodexDeltaBuffer():this.clearCodexDeltaBuffer(),this.emit("exit",o)}),this.process.stderr?.on("data",o=>{const a=o.toString().trim();a&&d.info("codex-adapter",`[codex stderr] ${a}`)}),this.bridgeStatus="starting",this.bindStdout(),this.alive=!0,this.startOrphanTurnWatch()}resolveRelayTransportConfigOverrides(){if(this.config.env?.[st]!=="1")return[];const e=Ke(this.codexHome,this.configOverrideArgs);if(e==="openai"){const i=Ze(this.codexHome,this.configOverrideArgs);return Xe(Je,"Grix OpenAI Relay",i.targetBaseUrl)}const t=Qe(e);return t?[`${t}=false`]:[]}bindStdout(){if(!this.process?.stdout)return;this.process.stdout.setEncoding("utf8"),ye({input:this.process.stdout}).on("line",t=>{if(!t.trim())return;let i;try{i=JSON.parse(t)}catch{d.error("codex-adapter",`Invalid JSON from Codex: ${t.slice(0,200)}`);return}this.bridgeStatus==="starting"&&(this.bridgeStatus="ready");const s=Object.prototype.hasOwnProperty.call(i,"result"),n=Object.prototype.hasOwnProperty.call(i,"error"),r=!Object.prototype.hasOwnProperty.call(i,"method")&&s!==n&&(s||typeof i.error?.code=="number"&&typeof i.error.message=="string");if(i.id!=null&&r){const o=String(i.id),a=this.pendingRequests.get(o);if(a){clearTimeout(a.timer),this.pendingRequests.delete(o),a.resolve(i);return}}this.handleNotification(i)})}handleNotification(e){const t=e.method,i=e.params??{};if(this.stopped||this.bridgeStatus==="closed")return;if(this.shouldIgnoreOutOfScopeNotification(e,t,i)){const n=w(u(i.threadId),u(i.threadID)),r=i.item;if(n&&n!==this.threadId&&r?.id&&this.isLongRunningToolItemType(r.type)){const o=`${n}:${r.id}`;t==="item/started"&&this.waitLoopChildTools.add(o),t==="item/completed"&&this.waitLoopChildTools.delete(o),this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++}this.activeEventId&&this.resetIdleTimer(this.activeEventId);return}switch(this.captureAuditNotification(t,i),this.captureContextWindowFromPayload(t,i),this.activeEventId&&t&&t.startsWith("item/")&&!t.endsWith("/requestApproval")&&this.startComposing(),t){case"thread/status/changed":{const n=i.status;this.nativeSettlement&&i.threadId===this.nativeSettlement.threadId&&n?.type==="idle"&&this.checkNativeSettlement(),this.compactionState&&!this.compactionState.resolve&&i.threadId===this.compactionState.threadId&&n?.type==="idle"&&this.checkCompactionCompletion();break}case"item/started":{const n=i.item,r=n?.id,o=n?.type,a=this.normalizeAgentMessagePhase(n?.phase);if(this.isLongRunningToolItemType(o)&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++,this.inFlightToolOps+=1),o==="agentMessage"&&a==="final_answer"&&O(a)&&this.recordFinalAnswerEvidence(r,this.extractAgentMessageText(n)),o==="agentMessage"&&r){a?this.agentMessagePhases.set(r,a):this.agentMessagePhases.delete(r);const h=O(a),f=H(a);h?(this.visibleAgentMessageIds.add(r),this.hiddenAgentMessageIds.delete(r)):f?(this.visibleAgentMessageIds.delete(r),this.hiddenAgentMessageIds.delete(r)):(this.hiddenAgentMessageIds.add(r),this.visibleAgentMessageIds.delete(r))}break}case"item/agentMessage/delta":{const n=i.delta;n&&(this.waitLoopTracker.noteProgress(),this.waitLoopProgressGeneration++);const r=i.itemId,o=this.resolveAgentMessagePhase(r,i.phase),a=(!r||!this.hiddenAgentMessageIds.has(r))&&O(o),h=(!r||!this.hiddenAgentMessageIds.has(r))&&H(o);if(o==="final_answer"&&a&&this.recordFinalAnswerEvidence(r,n),n&&this.activeEventId&&h){this.recordDeliveredThinkingAgentMessage(r),this.callbacks.sendThinking?.(this.activeEventId,this.activeSessionId??"",n);break}n&&this.activeEventId&&a&&!this.shouldSuppressAgentMessageDeltaDuringToolRun(r,o)&&(this.recordDeliveredAgentMessage(r,o),this.queueCodexDeltaEvent({...e,params:{...i,...o?{phase:o}:{}}}));break}case"item/completed":{const n=i.item,r=n?.id,o=n?.type;if(this.isLongRunningToolItemType(o)&&(this.inFlightToolOps=Math.max(0,this.inFlightToolOps-1)),this.activeEventId&&o==="commandExecution"&&(this.turnHasVisibleOutput=!0,this.emitCodexEvent("item/completed",e)),o==="agentMessage"){const a=this.resolveAgentMessagePhase(r,n?.phase),h=(!r||!this.hiddenAgentMessageIds.has(r))&&O(a);if(a==="final_answer"&&h){const f=this.extractAgentMessageText(n);this.recordFinalAnswerEvidence(r,f,!0);const p=r?this.deliveredAgentMessageIds.has(r):!1;this.activeEventId&&f&&!p&&(d.warn("codex-adapter",`Recovering final_answer from item/completed item_id=${r??"unknown"}`),this.recordDeliveredAgentMessage(r,a),this.queueCodexDeltaEvent({method:"item/agentMessage/delta",params:{...r?{itemId:r}:{},delta:f,phase:a}}))}else if(H(a)){const f=this.extractAgentMessageText(n),p=r?this.deliveredThinkingAgentMessageIds.has(r):!1;this.activeEventId&&f&&!p&&(this.recordDeliveredThinkingAgentMessage(r),this.callbacks.sendThinking?.(this.activeEventId,this.activeSessionId??"",f))}r&&(this.visibleAgentMessageIds.delete(r),this.hiddenAgentMessageIds.delete(r),this.agentMessagePhases.delete(r))}break}case"turn/started":{const r=i.turn?.id;if(r){if(this.compactionState&&(this.compactionState.turnId=r),this.currentTurnId=r,this.rejectedTurnStarts.size>0&&!this.activeEventId){this.retainUnconfirmedNativeTurn();const o=this.rejectedTurnStarts.values().next().value;this.threadId&&o&&this.interruptRejectedTurn(o,this.threadId,r);break}this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.currentTurnStartedAt=Date.now(),this.preflightCheckComplete=!1,d.info("codex-adapter",`Turn started: ${r}`),this.startComposing(),this.emit("activeInputReady")}break}case"item/fileChange/requestApproval":{this.handleApprovalRequest(e,"file");break}case"item/commandExecution/requestApproval":{this.handleApprovalRequest(e,"exec");break}case"item/permissions/requestApproval":{this.handleApprovalRequest(e,"permission");break}case"turn/completed":{this.handleTurnCompleted(i);break}case"error":{const n=i.error,r=i.message??n?.message??JSON.stringify(i);d.error("codex-adapter",`Codex error: ${r}`);const o=i.willRetry===!0;r&&(this.turnError=r,o||(this.turnErrorIsRetryableOnly=!1)),this.activeEventId&&!o&&(this.flushCodexDeltaBuffer(),this.callbacks.sendRunError(this.activeEventId,this.activeSessionId??"",r));break}case"account/rateLimits/updated":{const n=this.extractRateLimitPushLimitId(i),r=this.parseRateLimitSnapshot(i,this.rateLimitSnapshot);r&&(this.rateLimitSnapshot=r,d.info("codex-adapter",`Rate limits updated: primary=${r.primary.usedPercent.toFixed(1)}% secondary=${r.secondary.usedPercent.toFixed(1)}% extras=${r.extras?.length??0}`),this.callbacks.onRateLimitsUpdated?.(r)),n&&n!=="codex"&&this.refreshMainRateLimitsThrottled();break}case"thread/tokenUsage/updated":{const n=i.threadId,r=i.tokenUsage,o=r?.last;(!n||!r||!o)&&d.info("codex-adapter",`[cp-diagnose] token_usage payload_incomplete: method=thread/tokenUsage/updated hasThreadId=${String(!!n)} hasTokenUsage=${String(!!r)} hasLast=${String(!!o)} params=${JSON.stringify(i)}`),n&&r&&o&&(this.currentThreadTokenUsage={inputTokens:o.inputTokens??0,outputTokens:o.outputTokens??0,cacheReadInputTokens:o.cachedInputTokens??0,cacheCreationInputTokens:0},d.info("codex-adapter",`[cp-diagnose] token_usage raw thread=${n}: ${JSON.stringify(r)}`),d.info("codex-adapter",`[cp-diagnose] token_usage parsed thread=${n}: ${JSON.stringify(this.currentThreadTokenUsage)}`),this.callbacks.onTokenUsageUpdated?.(this.currentThreadTokenUsage),this.publishContextWindowSnapshot());break}default:break}const s=t?.endsWith("/requestApproval")===!0;t&&t!=="turn/started"&&(t.startsWith("item/")||t==="error"||t==="turn/completed")&&(this.preflightCheckComplete=!0),this.activeEventId&&(!s||this.pendingApprovals.size===0)&&this.resetIdleTimer(this.activeEventId)}shouldIgnoreOutOfScopeNotification(e,t,i){if(!t||e.id!=null)return!1;const s=w(u(i.threadId),u(i.threadID));if(s&&this.threadId&&s!==this.threadId)return this.logIgnoredNotificationScope(t,s,this.readNotificationTurnId(i)),!0;if(this.nativeSettlement&&(t.startsWith("turn/")||t.startsWith("item/")||t==="error")){const r=i.turn;return t!=="turn/completed"||s!==this.nativeSettlement.threadId||this.readNotificationTurnId(i)!==this.nativeSettlement.turnId||!["completed","failed","interrupted"].includes(String(r?.status))}if(this.compactionState&&t.startsWith("turn/")){if(s!==this.compactionState.threadId)return!0;const r=this.readNotificationTurnId(i);if(!r||this.retiredTurnIds.has(r)||t==="turn/completed"&&r!==this.compactionState.turnId)return!0}const n=this.readNotificationTurnId(i);return n&&this.retiredTurnIds.has(n)?!0:n&&this.currentTurnId&&n!==this.currentTurnId?(this.logIgnoredNotificationScope(t,s,n),!0):!1}readNotificationTurnId(e){const t=e.turn&&typeof e.turn=="object"?e.turn:void 0;return w(u(e.turnId),u(e.turnID),u(t?.id))}logIgnoredNotificationScope(e,t,i){e!=="error"&&!e.startsWith("turn/")||d.info("codex-adapter",`[scope] ignored ${e} thread=${t??"<none>"} turn=${i??"<none>"} active_thread=${this.threadId??"<none>"} active_turn=${this.currentTurnId??"<none>"}`)}captureAuditNotification(e,t){const i=this.activeEventId;if(!e||!i)return;const s=this.auditCaptureByEventId.get(i);if(!s)return;const n=t.turn,r=typeof t.turnId=="string"&&t.turnId?t.turnId:typeof t.turn_id=="string"&&t.turn_id?t.turn_id:typeof n?.id=="string"&&n.id?n.id:this.currentTurnId;r&&(s.providerTurnId=r);const o=typeof t.threadId=="string"&&t.threadId?t.threadId:typeof t.thread_id=="string"&&t.thread_id?t.thread_id:this.threadId;if(o&&(s.threadId=o),!!gt.has(e)){try{const a=JSON.parse(JSON.stringify(t));s.events.push({sequence:++s.sequence,method:e,params:a,observedAt:new Date().toISOString()})}catch(a){const h=a instanceof Error?a.message:String(a);d.warn("codex-adapter",`Audit evidence capture skipped event=${i} method=${e}: ${h}`)}if(e==="turn/completed"||e==="codex/turn/completed")for(const a of this.activeInputs.keys())this.captureActiveInputAudit(a,e,{threadId:o,turnId:r,turn:n?{id:n.id,status:n.status,error:n.error}:void 0})}}captureContextWindowFromPayload(e,t){const i=["model_context_window","modelContextWindow","context_window_size","contextWindowSize"],s=this.findNumericField(t,i);if(s==null||s<=0){const n=this.probeContextWindowFields(t,i);n.foundAnyCandidateField&&d.info("codex-adapter",`[cp-diagnose] context_window missing_or_invalid: method=${e??"-"} matchedFields=${n.matchedFields.join(",")||"-"} raw=${n.rawValues.join(",")||"-"} extracted=${String(s)}`);return}this.currentModelContextWindow!==s&&(this.currentModelContextWindow=s,d.info("codex-adapter",`[cp-diagnose] context_window updated: method=${e??"-"} size=${s}`),this.publishContextWindowSnapshot())}probeContextWindowFields(e,t){const i=[],s=[],n=r=>{if(!r||typeof r!="object")return;if(Array.isArray(r)){for(const a of r)n(a);return}const o=r;for(const a of t)Object.prototype.hasOwnProperty.call(o,a)&&(i.push(a),s.push(String(o[a])));for(const a of Object.values(o))n(a)};return n(e),{foundAnyCandidateField:i.length>0,matchedFields:i,rawValues:s}}findNumericField(e,t){if(!e||typeof e!="object")return null;const i=e;for(const s of t){const n=i[s];if(typeof n=="number"&&Number.isFinite(n))return n;if(typeof n=="string"){const r=Number(n);if(Number.isFinite(r))return r}}for(const s of Object.values(i)){if(Array.isArray(s)){for(const r of s){const o=this.findNumericField(r,t);if(o!=null)return o}continue}const n=this.findNumericField(s,t);if(n!=null)return n}return null}buildContextWindowSnapshot(){const e=this.currentModelContextWindow;if(!e||e<=0)return d.debug("codex-adapter",`[cp-diagnose] context_window snapshot skipped: invalid size=${String(e)}`),null;const t=this.currentThreadTokenUsage,i=t?t.inputTokens+t.outputTokens:null,s=i==null?null:Math.max(0,e-i),n=i==null?null:Math.min(100,i/e*100),r=i==null?null:Math.max(0,100-(n??0));return d.info("codex-adapter",`[cp-diagnose] context_window computed: threadId=${this.threadId??"-"} size=${e} usedTokens=${String(i)} usedPercentage=${String(n)}`),{sizeTokens:e,usedTokens:i,remainingTokens:s,usedPercentage:n,remainingPercentage:r,source:"codex:model_context_window"}}publishContextWindowSnapshot(){const e=this.buildContextWindowSnapshot();this.callbacks.onContextWindowUpdated?.(e),this.maybeScheduleAutoCompact(e)}maybeScheduleAutoCompact(e){const t=e?.usedPercentage;typeof t!="number"||t<ue||this.pendingAutoCompact||this.compacting||(this.pendingAutoCompact=!0,d.info("codex-adapter",`[auto-compact] context_window usedPercentage=${t.toFixed(1)}% >= ${ue}%, scheduling compact`),this.tryRunPendingAutoCompact())}tryRunPendingAutoCompact(){this.pendingAutoCompact&&(this.stopped||this.bridgeStatus==="closed"||this.currentTurnId||this.activeEventId||this.compacting||(this.pendingAutoCompact=!1,this.execCommand("compact","",this.activeSessionId??"").then(e=>{d.info("codex-adapter",`[auto-compact] compact done status=${e.status} msg=${e.message??""}`),e.status==="failed"&&/agent busy/i.test(e.message??"")&&(this.pendingAutoCompact=!0)}).catch(e=>{d.warn("codex-adapter",`[auto-compact] compact error: ${e instanceof Error?e.message:e}`)})))}parseRateLimitSnapshot(e,t=null){try{const i=e.rateLimitsByLimitId,s=this.extractRateLimitPushLimitId(e),n=!i?.codex&&!!s&&s!=="codex",r=i?.codex??(n?{}:e.rateLimits??e),o=r.primary,a=r.secondary,h=r.credits,f=l=>{if(typeof l=="number"&&Number.isFinite(l))return l;if(typeof l=="string"){const m=Number(l);if(Number.isFinite(m))return m}return 0},p=l=>typeof l=="boolean"?l:typeof l=="string"?l.toLowerCase()==="true":!1,g=l=>{if(l==null)return null;if(typeof l=="string"){const m=l.trim();if(!m)return null;const I=Number(m);return Number.isFinite(I)&&I>1e9?new Date(I*1e3).toISOString():m}if(typeof l=="number"&&Number.isFinite(l)&&l>0){const m=l>1e12?l/1e3:l;return new Date(m*1e3).toISOString()}return null},T=l=>{const m=l.resets_in_seconds??l.resetsInSeconds,I=typeof m=="number"||typeof m=="string"?Number(m):NaN;return!Number.isFinite(I)||I<=0?null:new Date(Date.now()+I*1e3).toISOString()},v=l=>({usedPercent:f(l.used_percent??l.usedPercent),windowMinutes:f(l.window_minutes??l.windowDurationMins??l.windowMinutes),resetsAt:g(l.resets_at??l.resetsAt)??T(l)}),x=()=>({usedPercent:0,windowMinutes:0,resetsAt:null}),y=(l,m)=>l??m??x(),S=()=>{const l=[];if(o&&l.push(o),a&&l.push(a),Array.isArray(r.windows))for(const m of r.windows)m&&typeof m=="object"&&l.push(m);return l.map(v).filter(m=>m.windowMinutes>0)},C=(l,m,I)=>{const E=l.filter(b=>I(b.windowMinutes));return E.length===0?null:E.reduce((b,R)=>Math.abs(R.windowMinutes-m)<Math.abs(b.windowMinutes-m)?R:b)},$=S(),N=n?t?{primary:t.primary,secondary:t.secondary}:null:$.length>0?{primary:y(C($,300,l=>l<1440),t?.primary),secondary:y(C($,10080,l=>l>=1440),t?.secondary)}:o||a?{primary:o?v(o):t?.primary??x(),secondary:a?v(a):t?.secondary??x()}:null;if(!N)return null;const Y=(l,m)=>{if(!m||typeof m!="object"||Array.isArray(m))return null;const I=m,E=I.limitName??I.limit_name,b=typeof E=="string"&&E.trim()?E.trim():l,R=[],K=I.primary,Z=I.secondary;K&&R.push(K),Z&&R.push(Z);const Q=R.map(v).filter(M=>M.windowMinutes>0),ee=C(Q,10080,M=>M>=1440)??C(Q,300,M=>M<1440);return ee?{id:l,label:b,...ee}:null};let A;if(i&&typeof i=="object"){A=[];for(const[l,m]of Object.entries(i)){if(l==="codex")continue;const I=Y(l,m);I&&A.push(I)}}else if(A=t?.extras?[...t.extras]:[],n&&s){const l=Y(s,e.rateLimits??e);if(l){const m=A.findIndex(I=>I.id===l.id);m>=0?A[m]=l:A.push(l)}}return{primary:N.primary,secondary:N.secondary,credits:h?{hasCredits:p(h.has_credits??h.hasCredits),unlimited:p(h.unlimited),balance:h.balance==null?null:f(h.balance)}:t?.credits??{hasCredits:!1,unlimited:!1,balance:null},...A.length>0?{extras:A}:{}}}catch{return null}}handleApprovalRequest(e,t){const i=e.id;if(i==null)return;const s=e.method,n=`${i}`.trim(),r=`codex_${we(this.activeSessionId??"")}_${we(n)}`;if(this.approvalPolicy==="never"){this.sendApprovalDecision(i,"accept");return}this.activeEventId&&this.emitCodexEvent(s,e),this.pendingApprovals.set(r,{approvalId:r,approvalCommandId:n,sourceEventId:this.activeEventId??"",requestId:i,kind:t}),d.info("codex-adapter",`Pending approval stored: ${r} (kind=${t})`),this.clearIdleTimer(),this.stopComposing()}resolvePendingApproval(e){const t=e.params??{},i=w(t.approval_id,t.approvalId);if(i)return this.pendingApprovals.get(i)??null;const s=w(t.approval_command_id,t.approvalCommandId);if(s){for(const[,n]of this.pendingApprovals)if(n.approvalCommandId===s)return n}return null}async handleExecApprovalEvent(e){const t={approval_id:e.approval_id,approval_command_id:e.approval_command_id,session_id:e.session_id},i={action_id:"exec_approval_event",action_type:e.decision==="deny"?"exec_reject":"exec_approve",params:t},s=this.resolvePendingApproval(i);if(!s)return{delivered:!1,errorCode:"approval_not_found",errorMsg:"That approval request is no longer pending."};const n=me[e.decision]??"accept";return this.sendApprovalDecision(s.requestId,n),this.pendingApprovals.delete(s.approvalId),this.resumeAfterApproval({...i,event_id:s.sourceEventId},t,s),{delivered:!0}}sendApprovalDecision(e,t){if(e==null||!this.process?.stdin)return;const i={jsonrpc:"2.0",id:e,result:{decision:t}};this.process.stdin.write(`${JSON.stringify(i)}
|
|
5
|
+
`)}async cancelActiveTurnForCompaction(){const e=this.threadId,t=this.currentTurnId,i=this.activeEventId;await this.cancel(e??"").catch(()=>{}),!(this.threadId!==e||this.currentTurnId!==t||this.activeEventId!==i||this.compacting)&&(i&&this.sendEventResultAfterFlushing(i,"canceled","compacting"),this.clearActive())}runCompaction(){if(this.currentTurnId||this.activeEventId||this.compacting)return Promise.resolve({status:"failed",message:"agent busy"});this.compacting=!0;const e=new Promise(i=>{this.compactionState={threadId:this.threadId,turnId:null,deadline:Date.now()+z,checking:!1,resolve:i}}),t=this.compactionState;return this.armCompactionCheck(),this.threadCompact().catch(i=>{if(this.compactionState!==t)return;const s=i instanceof Error?i.message:String(i),n=!t?.turnId&&(/thread.*(not found|not exist|unknown|invalid)/i.test(s)||/not found.*thread/i.test(s));this.finishCompaction({status:"failed",message:s},n)}),e}armCompactionCheck(){this.compactingTimer&&clearTimeout(this.compactingTimer);const e=this.compactionState;e?.resolve&&(this.compactingTimer=setTimeout(()=>{this.compactingTimer=null,this.checkCompactionCompletion()},Math.min(he,Math.max(0,e.deadline-Date.now()))),this.compactingTimer.unref?.())}async checkCompactionCompletion(){const e=this.compactionState;if(!e||e.checking||this.stopped||!this.alive||this.threadId!==e.threadId||!e.resolve&&!e.turnId)return;const t=i=>{this.compactionState===e&&this.finishCompaction({status:"failed",message:i},!1)};if(e.resolve&&Date.now()>=e.deadline){t("Compaction completion was not confirmed within 10 minutes; input was not submitted");return}e.checking=!0;try{const i=await this.sendRequest("thread/read",{threadId:e.threadId,includeTurns:!0},5e3);if(this.compactionState!==e||this.stopped||!this.alive||this.threadId!==e.threadId)return;const s=i.thread?.id===e.threadId&&e.turnId?i.thread.turns?.find(n=>n.id===e.turnId):void 0;i.thread?.status?.type==="idle"&&s&&["completed","failed","interrupted"].includes(String(s.status))?this.handleTurnCompleted({threadId:e.threadId,turn:s}):s?.status==="inProgress"||i.thread?.id===e.threadId&&i.thread.status?.type==="active"?(d.info("codex-adapter","Compaction still in progress; retaining input barrier"),this.armCompactionCheck()):t("Compaction native state could not be confirmed; input was not submitted")}catch(i){t(`Compaction status check failed; input was not submitted: ${i instanceof Error?i.message:String(i)}`)}finally{e.checking=!1}}retainUnconfirmedNativeTurn(){this.compacting||this.nativeSettlement||!this.currentTurnId||!this.threadId||!this.alive||this.stopped||(this.nativeSettlement={threadId:this.threadId,turnId:this.currentTurnId,deadline:Date.now()+z,checking:!1,waiting:!0},this.armNativeSettlementCheck())}clearNativeSettlement(e){if(this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer),this.nativeSettlementTimer=null,this.nativeSettlement=null,this.rejectedNativeTimer&&clearTimeout(this.rejectedNativeTimer),this.rejectedNativeTimer=null,this.rejectedNativeOwner=null,e){this.failCompactionDeferred(e);for(const t of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed",e)}}armNativeSettlementCheck(){this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer);const e=this.nativeSettlement;e?.waiting&&(this.nativeSettlementTimer=setTimeout(()=>{this.nativeSettlementTimer=null,this.checkNativeSettlement()},Math.min(he,Math.max(0,e.deadline-Date.now()))),this.nativeSettlementTimer.unref?.())}async checkNativeSettlement(){const e=this.nativeSettlement;if(!e||e.checking||this.stopped||!this.alive||this.threadId!==e.threadId)return;const t=i=>{if(this.nativeSettlement===e){e.waiting=!1,this.nativeSettlementTimer&&clearTimeout(this.nativeSettlementTimer),this.nativeSettlementTimer=null,this.failCompactionDeferred(i);for(const s of this.steerDeferred.splice(0))this.sendEventResultAfterFlushing(s.event_id,"failed",i)}};if(e.waiting&&Date.now()>=e.deadline){t("Native turn completion was not confirmed within 10 minutes; input was not submitted");return}e.checking=!0;try{const i=await this.sendRequest("thread/read",{threadId:e.threadId,includeTurns:!0},5e3);if(this.nativeSettlement!==e||this.stopped||!this.alive||this.threadId!==e.threadId)return;const s=i.thread?.id===e.threadId?i.thread.turns?.find(n=>n.id===e.turnId):void 0;i.thread?.status?.type==="idle"&&s&&["completed","failed","interrupted"].includes(String(s.status))?this.handleTurnCompleted({threadId:e.threadId,turn:s}):s?.status==="inProgress"||i.thread?.id===e.threadId&&i.thread.status?.type==="active"?this.armNativeSettlementCheck():t("Native turn state could not be confirmed; input was not submitted")}catch(i){t(`Native turn status check failed; input was not submitted: ${i instanceof Error?i.message:String(i)}`)}finally{e.checking=!1}}startOrphanTurnWatch(){this.orphanTurnTimer||(this.orphanTurnTimer=setInterval(()=>this.checkOrphanTurn(),lt),this.orphanTurnTimer.unref?.())}stopOrphanTurnWatch(){this.orphanTurnTimer&&(clearInterval(this.orphanTurnTimer),this.orphanTurnTimer=null),this.orphanTurnSince=0}checkOrphanTurn(){if(!(this.currentTurnId!==null&&this.activeEventId===null&&!this.compacting&&!this.nativeSettlement)){this.orphanTurnSince=0;return}const t=Date.now();if(this.orphanTurnSince===0){this.orphanTurnSince=t;return}if(t-this.orphanTurnSince<ut)return;const i=this.currentTurnId,s=t-this.orphanTurnSince;this.currentTurnId=null,this.orphanTurnSince=0,d.warn("codex-adapter",`Orphan turn ${i} cleared after ${Math.round(s/1e3)}s with no active event: turn/completed never arrived. Adapter was reporting busy the whole time, which blocks upgrade/restart idle waits.`)}finishCompaction(e,t=!0){const i=this.compactionState;if(!i){this.failCompactionDeferred(e.message??"Compaction failed");return}this.compactingTimer&&(clearTimeout(this.compactingTimer),this.compactingTimer=null);const s=i.resolve;i.resolve=null,t&&(i.turnId&&this.retiredTurnIds.add(i.turnId),this.compactionState=null,this.compacting=!1,this.currentTurnId=null,this.stopComposing()),d.info("codex-adapter",`Compaction settled status=${e.status} nativeSettled=${t}`),s?.(e),e.status==="ok"&&t&&s?this.replayCompactionDeferred():this.failCompactionDeferred(e.message??"Compaction failed")}failCompactionDeferred(e){for(const t of this.compactionDeferred.splice(0))this.sendEventResultAfterFlushing(t.event_id,"failed",e)}replayCompactionDeferred(){if(this.compacting||this.activeEventId||this.currentTurnId||this.stopped||!this.alive)return;const e=this.compactionDeferred.shift();e&&this.startNewTurn(e,F(e.content,{messageId:e.msg_id}))}handleTurnCompleted(e){if(this.nativeSettlement){const s=this.nativeSettlement,n=e.turn;if(e.threadId!==s.threadId&&e.threadID!==s.threadId||n?.id!==s.turnId||!["completed","failed","interrupted"].includes(String(n.status)))return;this.retiredTurnIds.add(s.turnId),this.clearNativeSettlement(),this.currentTurnId=null,this.rejectedTurnStarts.size>0&&(this.rejectedTurnStarts.clear(),this.emit("resumeIntake","barrier")),this.replayCompactionDeferred(),this.replaySteerDeferred(),this.tryRunPendingAutoCompact();return}if(this.compactionState){const s=e.turn;if(e.threadId!==this.compactionState.threadId||s?.id!==this.compactionState.turnId)return;const n=s?.status;if(!["completed","failed","interrupted"].includes(String(n)))return;const r=s?.error,o=n==="completed"&&!(this.turnError&&!this.turnErrorIsRetryableOnly);this.finishCompaction({status:o?"ok":"failed",message:o?"Thread compacted":r?.message??this.turnError??`Compaction ${n}`});return}const t=this.readNotificationTurnId(e),i=e.turn;if(!(!i||!t||e.threadId!==this.threadId&&e.threadID!==this.threadId||!["completed","failed","interrupted"].includes(String(i.status))||this.currentTurnId&&t!==this.currentTurnId)&&(this.retiredTurnIds.add(t),this.currentTurnId=null,this.stopComposing(),this.activeEventId)){this.flushCodexDeltaBuffer();const s=this.activeEventId,n={event_id:s,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:"turn/completed",codex_sequence:this.nextCodexSequence()},r=this.turnError,o=this.turnErrorIsRetryableOnly,a=this.turnHasVisibleOutput,h=i.status==="completed"&&this.completedEmptyFinalAnswerItems.size>0&&[...this.completedEmptyFinalAnswerItems.values()].every(x=>x)&&!this.turnSawNonEmptyFinalAnswer,f=this.turnSawVisibleFinalAnswer&&!this.turnHasDeliveredFinalAnswer&&!h;this.turnError=null,this.turnErrorIsRetryableOnly=!0,this.turnHasVisibleOutput=!1,this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1;const T=i.error?.message??(i.status!=="completed"?r??`Codex turn ${i.status}`:null)??(r&&!(o&&a)?r:null)??(f?"Codex final answer was observed but not delivered":null)??(!a&&!h?"Codex completed without producing any visible output":null),v=T!==null;this.callbacks.sendCodexEvent(n),this.sendEventResultAfterFlushing(s,v?"failed":"responded",T??void 0),this.clearActive()}}static allocateCodexSequenceBase(){const e=Date.now()*1e3,t=e>_.sequenceFloor?e:_.sequenceFloor+1e3;return _.sequenceFloor=t,t}nextCodexSequence(){const e=++this.codexSequence;return e>_.sequenceFloor&&(_.sequenceFloor=e),e}emitCodexEvent(e,t){if(!this.activeEventId)return;this.flushCodexDeltaBuffer();const i=Te(e,t);if(i===void 0&&e==="item/completed")return;const s={event_id:this.activeEventId,session_id:this.activeSessionId??"",thread_id:this.threadId??void 0,codex_event_type:"codex",codex_method:e,codex_sequence:this.nextCodexSequence()};i!==void 0&&(s.codex_payload=i),this.callbacks.sendCodexEvent(s)}queueCodexDeltaEvent(e){if(!this.activeEventId)return;const t=Te("item/agentMessage/delta",e),i=t?.params?.delta;if(typeof i!="string"||i.length===0)return;const s=typeof t?.params?.phase=="string"?t.params.phase:void 0,n=this.activeEventId,r=this.activeSessionId??"",o=this.threadId??void 0,a=this.pendingCodexDeltaFlush;if(a&&(a.eventId!==n||a.sessionId!==r||a.threadId!==o||a.phase!==s)&&this.flushCodexDeltaBuffer(),this.pendingCodexDeltaFlush||(this.pendingCodexDeltaFlush={eventId:n,sessionId:r,threadId:o,delta:"",phase:s,timer:null}),this.pendingCodexDeltaFlush.delta+=i,this.pendingCodexDeltaFlush.delta.length>=pt){this.flushCodexDeltaBuffer();return}this.pendingCodexDeltaFlush.timer||(this.pendingCodexDeltaFlush.timer=setTimeout(()=>{if(this.activeEventId!==n){this.clearCodexDeltaBuffer();return}this.flushCodexDeltaBuffer()},ht))}flushCodexDeltaBuffer(){const e=this.pendingCodexDeltaFlush;e&&(this.pendingCodexDeltaFlush=null,e.timer&&clearTimeout(e.timer),e.delta&&(this.nextCodexSequence(),this.callbacks.sendStreamChunk(e.eventId,e.sessionId,e.delta,0,!1,ft(e.eventId),void 0,{...e.phase?{phase:e.phase}:{},...e.threadId?{threadId:e.threadId}:{}})))}clearCodexDeltaBuffer(){const e=this.pendingCodexDeltaFlush;this.pendingCodexDeltaFlush=null,e?.timer&&clearTimeout(e.timer)}sendEventResultAfterFlushing(e,t,i){e===this.activeEventId&&t!=="responded"&&this.retainUnconfirmedNativeTurn(),this.pendingCodexDeltaFlush?.eventId===e&&this.flushCodexDeltaBuffer(),e===this.activeEventId&&this.finishActiveInputs(t,i),this.callbacks.sendEventResult(e,t,i)}startComposing(){}stopComposing(){}async sendRequest(e,t,i=3e4,s){return new Promise((n,r)=>{if(!this.process?.stdin){r(new Error("Codex process not available"));return}const o=++this.requestId,a={jsonrpc:"2.0",id:o,method:e,params:t??{}},h=setTimeout(()=>{this.pendingRequests.delete(String(o)),r(new Error(`Request timeout: ${e}`))},i);this.pendingRequests.set(String(o),{resolve:p=>{p.error?r(new Error(`JSON-RPC error: ${p.error.message}`)):(s?.(),n(p.result))},reject:r,timer:h});const f=JSON.stringify(a);this.process.stdin.write(`${f}
|
|
6
6
|
`)})}sendNotification(e,t){if(!this.process?.stdin)return;const i={jsonrpc:"2.0",method:e,params:t??{}};this.process.stdin.write(`${JSON.stringify(i)}
|
|
7
|
-
`)}async initializeHandshake(){if(this.initialized)return;const e=
|
|
7
|
+
`)}async initializeHandshake(){if(this.initialized)return;const e=De(),t=await this.sendRequest("initialize",{clientInfo:{name:"grix-connector",title:"Grix Connector",version:e},capabilities:{experimentalApi:!0}},15e3);d.info("codex-adapter",`Initialized: ${JSON.stringify(t)}`),this.sendNotification("initialized"),this.initialized=!0,await this.refreshCodexModelOptions(),this.fetchRateLimits()}extractRateLimitPushLimitId(e){const t=s=>{if(!s||typeof s!="object"||Array.isArray(s))return null;const n=s.limitId??s.limit_id;return typeof n=="string"&&n.trim()?n.trim():null},i=e.rateLimitsByLimitId;if(i&&typeof i=="object"&&!Array.isArray(i)){const s=i;return s.codex?"codex":t(Object.values(s)[0])}return t(e.rateLimits??e)}refreshMainRateLimitsThrottled(){const e=Date.now();e-this.lastNonDefaultLimitRefreshAt<_.NON_DEFAULT_LIMIT_REFRESH_INTERVAL_MS||(this.lastNonDefaultLimitRefreshAt=e,this.fetchRateLimits())}fetchRateLimits(){Promise.resolve(this.sendRequest("account/rateLimits/read",void 0,1e4)).then(e=>{const t=this.parseRateLimitSnapshot(e);t&&(this.rateLimitSnapshot=t,d.info("codex-adapter",`Initial rate limits: primary=${t.primary.usedPercent.toFixed(1)}% secondary=${t.secondary.usedPercent.toFixed(1)}% credits=${t.credits.balance??"N/A"}`),this.callbacks.onRateLimitsUpdated?.(t))}).catch(e=>{d.warn("codex-adapter",`Failed to fetch rate limits: ${e instanceof Error?e.message:e}`)})}async runTurn(e,t,i,s){this.threadId||await this.createSession({cwd:this.cwd}),await this.ensureThreadResumed();const n=[],r=[],o=this.legacyDeveloperInstructionsUnsupported&&this.protocolInstructionInputInjectedThreadId!==this.threadId;o&&r.push(j);const h=(this.legacyDeveloperInstructionsUnsupported||this.profileIdentityRefreshPending)&&this.identityInputInjectedThreadId!==this.threadId;if(h){const x=ge(this.callbacks.getAgentProfile?.());x&&(r.push(x.instructions),d.info("codex-adapter",`Injected agent identity through compatibility input for thread=${this.threadId} name="${x.agentName}"`))}if(this.needsHistoryInjection&&s){this.needsHistoryInjection=!1;const x=await this.loadHistoryForInjection(s);for(const y of x)n.push(y);x.length>0&&d.info("codex-adapter",`Injected ${x.length} history items`)}if(n.push({type:"text",text:e.text,text_elements:[]}),e.contextMessages&&e.contextMessages.length>0){const x=e.contextMessages.map(y=>`[${y.senderId??"unknown"}]: ${y.content}`).join(`
|
|
8
8
|
`);n.push({type:"text",text:`Conversation context:
|
|
9
9
|
${x}`,text_elements:[]})}r.length>0&&n.push({type:"text",text:r.join(`
|
|
10
10
|
|
|
11
|
-
`),text_elements:[]});const f={threadId:this.threadId,approvalPolicy:this.approvalPolicy,input:n,model:this.model},p=
|
|
11
|
+
`),text_elements:[]});const f={threadId:this.threadId,approvalPolicy:this.approvalPolicy,input:n,model:this.model},p=yt(this.collaborationMode,this.model);p&&(f.collaborationMode=p);const g=this.effectiveReasoningEffort();g&&(f.effort=g);const T=this.effectiveServiceTier();if(T&&(f.serviceTier=T),i&&this.activeEventId!==i)return;if(i){try{await this.waitLoopTracker.prepare(this.threadId,this.codexHome)}catch{}if(this.activeEventId!==i)return}this.startingEventId=i??null;let v;try{v=await this.sendRequest("turn/start",f)}finally{this.startingEventId===i&&(this.startingEventId=null)}if(i&&this.rejectedTurnStarts.has(i)){v.turn?.id&&(this.rejectedTurnStarts.delete(i),this.nativeSettlement||(this.currentTurnId=v.turn.id,this.retainUnconfirmedNativeTurn(),this.threadId&&this.interruptRejectedTurn(i,this.threadId,v.turn.id)),this.emit("resumeIntake","barrier"));return}i&&this.activeEventId===i&&!this.compacting&&!this.nativeSettlement&&!this.currentTurnId&&v.turn?.id&&!this.retiredTurnIds.has(v.turn.id)&&(this.currentTurnId=v.turn.id,this.emit("activeInputReady")),o&&(this.protocolInstructionInputInjectedThreadId=this.threadId),h&&(this.identityInputInjectedThreadId=this.threadId,this.profileIdentityRefreshPending=!1),t.emitDone({status:"completed"})}async ensureThreadResumed(){if(!this.threadId||!this.threadResumePending)return;const e=this.threadId;if(this.codexDirect&&this.codexHome)try{await qe({threadId:e,privateHome:this.codexHome,userHome:this.rolloutImportUserHome})}catch(n){d.warn("codex-adapter",`rollout import before resume failed threadId=${e} error=${q(n)}`)}const t=1+this.resumeRetryDelaysMs.length;let i;for(let n=1;n<=t;n++)try{const r=this.effectiveReasoningEffort(),o=this.effectiveServiceTier(),a=await this.sendRequest("thread/resume",{threadId:e,approvalPolicy:this.approvalPolicy,model:this.model,excludeTurns:!0,persistExtendedHistory:!1,...r?{effort:r}:{},...o?{serviceTier:o}:{}});this.updateModelFromThreadResult(a),this.threadResumePending=!1,n>1&&d.info("codex-adapter",`thread/resume succeeded after retry threadId=${e} attempt=${n}/${t}`);return}catch(r){i=r;const o=q(r);if(Be(r)){d.warn("codex-adapter",`thread/resume missing thread \u2192 startNewThread+historyInjection threadId=${e} error=${o}`),this.threadId=null,this.needsHistoryInjection=!0,await this.startNewThread(),this.threadResumePending=!1;return}if(n<t){const a=this.resumeRetryDelaysMs[n-1]??0;d.warn("codex-adapter",`thread/resume transient error \u2192 retry threadId=${e} attempt=${n}/${t} delayMs=${a} error=${o}`),a>0&&await new Promise(h=>{setTimeout(h,a)});continue}throw d.error("codex-adapter",`thread/resume failed after retries \u2192 keep threadId threadId=${e} attempts=${t} error=${o}`),new Error(`Failed to resume Codex thread ${e}: ${o}`)}const s=q(i);throw new Error(`Failed to resume Codex thread ${e}: ${s}`)}async startNewThread(){const e=ge(this.callbacks.getAgentProfile?.()),t=vt(e),i=this.effectiveReasoningEffort(),s=this.effectiveServiceTier(),n={approvalPolicy:this.approvalPolicy,model:this.model,cwd:this.cwd,experimentalRawEvents:!1,persistExtendedHistory:!1,...i?{effort:i}:{},...s?{serviceTier:s}:{}};try{let r;if(this.legacyDeveloperInstructionsUnsupported)r=await this.sendRequest("thread/start",n);else try{r=await this.sendRequest("thread/start",{...n,developerInstructions:t})}catch(a){if(!It(a))throw a;this.legacyDeveloperInstructionsUnsupported=!0,d.warn("codex-adapter","Codex app-server does not support thread/start.developerInstructions; falling back to first-turn compatibility input"),r=await this.sendRequest("thread/start",n)}const o=Tt(r);if(!o)throw new Error("Codex thread id is missing in thread/start result");this.threadId=o,this.identityInputInjectedThreadId=null,this.protocolInstructionInputInjectedThreadId=null,this.profileIdentityRefreshPending=!1,this.updateModelFromThreadResult(r),this.threadResumePending=!1,this.persistThreadId(o)}catch(r){const o=r instanceof Error?r.message:String(r);throw new Error(`Failed to start Codex thread: ${o}`)}}updateModelFromThreadResult(e){if(this.model)return;const t=xt(e);t&&(this.model=t,this.persistCodexContext())}persistThreadId(e){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexThreadId(this.aibotSessionId,e)}persistCodexContext(){!this.bindingStore||!this.aibotSessionId||this.bindingStore.setCodexContext(this.aibotSessionId,{modelId:this.model,modeId:P(this.collaborationMode)??void 0,reasoningEffort:this.effectiveReasoningEffort(),serviceTier:this.serviceTier,sandboxMode:this.sandboxMode})}async loadHistoryForInjection(e){const t=this.callbacks.getConversationLog();if(!t)return[];try{const i=await t.readHistory(e,dt),s=[];for(const n of i){const r=(n.content??"").slice(0,ct);n.direction==="inbound"?s.push({type:"text",text:r,text_elements:[]}):n.direction==="outbound"&&s.push({type:"assistant",text:r,text_elements:[]})}return s}catch(i){return d.error("codex-adapter",`Failed to load history: ${i}`),[]}}async steerTurn(e,t,i){if(!this.threadId||!this.currentTurnId)throw new Error("No active turn to steer");const s=[{type:"text",text:e,text_elements:[]}];if(t){const n=JSON.parse(t);n.length>0&&s.push({type:"text",text:`Conversation context:
|
|
12
12
|
${n.map(r=>`[${r.sender_id??"unknown"}]: ${r.content}`).join(`
|
|
13
|
-
`)}`,text_elements:[]})}await this.sendRequest("turn/steer",{threadId:this.threadId,expectedTurnId:this.currentTurnId,input:s},3e4,i)}resumeAfterApproval(e,t,i){if(!this.threadId)return;const s=e.event_id?.trim()||`local_action_${e.action_id.trim()}`,n=w(t.session_id,t.sessionId)??this.activeSessionId??"";if(!n)return;const r=this.threadId,o=new
|
|
14
|
-
`)}}function
|
|
13
|
+
`)}`,text_elements:[]})}await this.sendRequest("turn/steer",{threadId:this.threadId,expectedTurnId:this.currentTurnId,input:s},3e4,i)}resumeAfterApproval(e,t,i){if(!this.threadId)return;const s=e.event_id?.trim()||`local_action_${e.action_id.trim()}`,n=w(t.session_id,t.sessionId)??this.activeSessionId??"";if(!n)return;const r=this.threadId,o=new J(r),a={adapterSessionId:r,text:""};this.activeEventId=s,this.activeSessionId=n,this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.runTurn(a,o,s,n).catch(h=>{d.error("codex-adapter",`Post-approval turn failed: ${h}`),this.sendEventResultAfterFlushing(s,"failed",h instanceof Error?h.message:String(h)),this.clearActive()}),this.resetIdleTimer(s)}clearActive(e){const t=this.activeEventId;if(this.finishActiveInputs("failed","Codex turn ownership ended before input completion"),this.flushCodexDeltaBuffer(),this.stopComposing(),this.activeEventId=null,this.activeSessionId=null,this.compacting||(this.retainUnconfirmedNativeTurn(),this.nativeSettlement||(this.currentTurnId=null)),this.visibleAgentMessageIds.clear(),this.hiddenAgentMessageIds.clear(),this.agentMessagePhases.clear(),this.deliveredAgentMessageIds.clear(),this.deliveredThinkingAgentMessageIds.clear(),this.turnSawVisibleFinalAnswer=!1,this.turnSawNonEmptyFinalAnswer=!1,this.completedEmptyFinalAnswerItems.clear(),this.turnHasDeliveredFinalAnswer=!1,this.inFlightToolOps=0,this.consecutivePingFailures=0,this.currentTurnStartedAt=0,this.preflightCheckComplete=!1,this.clearIdleTimer(),this.clearWaitLoopWatch(),this.pendingApprovals.clear(),t&&e?.emitEventDone!==!1){const i=this.rawCaptureRelay;i?i.completeEvent(t).catch(s=>{d.warn("codex-adapter",`Raw API audit capture did not settle cleanly: ${s instanceof Error?s.message:String(s)}`)}).finally(()=>this.emit("eventDone",t)):this.emit("eventDone",t)}this.nativeSettlement||(this.replayCompactionDeferred(),this.replaySteerDeferred(),e?.runPendingAutoCompact!==!1&&this.tryRunPendingAutoCompact())}replaySteerDeferred(){if(this.steerDeferred.length===0)return;const e=this.steerDeferred.splice(0);d.info("codex-adapter",`Replaying ${e.length} steer-deferred event(s) after turn ended`);for(const t of e)this.deliverInboundEvent(t)}resetIdleTimer(e){const t=Date.now();this.waitLoopTracker.pureWait||(this.lastProgressAt=t),this.lastIdleCheckAt=t,this.activityGeneration+=1,this.lastIdleCheckGeneration=this.activityGeneration,this.idleNoProgressCount=0,this.consecutivePingFailures=0,this.waitLoopTimer||(this.waitLoopTimer=setInterval(()=>{this.checkWaitLoop(e)},ae),this.waitLoopTimer.unref?.()),this.armIdleTimer(e)}clearWaitLoopWatch(){this.waitLoopTimer&&clearInterval(this.waitLoopTimer),this.waitLoopTimer=null,this.waitLoopTracker=new se,this.waitLoopChildTools.clear()}async checkWaitLoop(e){if(this.waitLoopCheckRunning||this.activeEventId!==e||!this.threadId||!this.currentTurnId||this.compacting||this.pendingApprovals.size>0)return;const t=this.waitLoopTracker,i=this.currentTurnId,s=this.waitLoopProgressGeneration;this.waitLoopCheckRunning=!0;try{const n=await t.inspect(this.threadId,i,this.codexHome,Date.now());if(this.activeEventId!==e||this.currentTurnId!==i||t!==this.waitLoopTracker)return;if(this.waitLoopProgressGeneration!==s||this.waitLoopChildTools.size>0||this.inFlightToolOps>0){t.noteProgress();return}if(n!==null&&Date.now()-n>=it){const r="repeated clock.sleep without useful progress for 5 minutes; end this turn and resume on owner input";this.rejectEventOutput(e),this.callbacks.sendEventResult(e,"failed",r)}}catch{t.reset()}finally{this.waitLoopCheckRunning=!1}}armIdleTimer(e,t=Number.POSITIVE_INFINITY){this.clearIdleTimer();const i=Date.now(),s=this.inFlightToolOps>0?tt:ae,n=this.turnInactivityTimeoutMs===null||this.waitLoopTracker.pureWait?Number.POSITIVE_INFINITY:this.lastProgressAt+this.turnInactivityTimeoutMs-i,r=this.currentTurnStartedAt>0&&!this.preflightCheckComplete?this.currentTurnStartedAt+B-i:Number.POSITIVE_INFINITY,o=Math.max(0,Math.min(s,n,r,t));this.idleTimer=setTimeout(()=>{this.onIdleTimeout(e,o)},o)}async onIdleTimeout(e,t){if(this.activeEventId!==e)return;const i=this.lastIdleCheckAt,s=this.lastIdleCheckGeneration,n=this.ping(nt).catch(()=>!1),r=this.checkSessionActivitySince(i),[o,a]=await Promise.all([n,r]);if(this.activeEventId!==e)return;const h=this.activityGeneration>s;if(this.lastIdleCheckAt=Date.now(),this.lastIdleCheckGeneration=this.activityGeneration,!this.preflightCheckComplete&&this.currentTurnStartedAt>0&&Date.now()-this.currentTurnStartedAt>=B){const p=this.activityGeneration,g=await this.inspectCurrentTurnProgress();if(this.activeEventId!==e)return;if(this.activityGeneration>p)this.preflightCheckComplete=!0;else if(g==="task_started_only"){this.declareRecoverablePreflightStuck(e,`Codex preflight stalled for ${Math.round(B/1e3)}s before model execution`);return}else g==="unknown"?this.currentTurnStartedAt=Date.now():this.preflightCheckComplete=!0}if(h||a){a&&(d.info("codex-adapter",`Idle timer: no stream output but rollout still being written, continuing: ${e}`),this.waitLoopTracker.pureWait||(this.lastProgressAt=Date.now())),this.idleNoProgressCount=0,this.consecutivePingFailures=0,this.armIdleTimer(e);return}const f=Date.now()-this.lastProgressAt;if(this.turnInactivityTimeoutMs!==null&&f>=this.turnInactivityTimeoutMs){d.error("codex-adapter",`Turn inactive ${Math.round(f/1e3)}s \u2265 cap ${Math.round(this.turnInactivityTimeoutMs/1e3)}s, declaring stuck: ${e}`),this.declareStuck(e,`turn inactivity timeout after ${Math.round(f/1e3)}s with no Codex activity`);return}if(!o){if(this.consecutivePingFailures+=1,this.consecutivePingFailures<de){d.warn("codex-adapter",`Agent idle ${t/1e3}s and ping failed (${this.consecutivePingFailures}/${de}); retrying liveness probe: ${e}`),this.armIdleTimer(e,rt);return}d.error("codex-adapter",`Agent idle ${t/1e3}s and ping failed ${this.consecutivePingFailures}x, declaring stuck: ${e}`),this.declareStuck(e,`agent idle ${t/1e3}s, ping failed ${this.consecutivePingFailures}x`);return}this.consecutivePingFailures=0,this.idleNoProgressCount++,d.info("codex-adapter",`Idle timer: ping ok, no stream progress (cycle=${this.idleNoProgressCount}), keeping active turn alive${this.turnInactivityTimeoutMs===null?"":` until inactivity cap (${Math.round(this.turnInactivityTimeoutMs/6e4)}min)`}: ${e}`),this.armIdleTimer(e)}declareStuck(e,t){this.sendEventResultAfterFlushing(e,"failed",t),this.clearActive(),this.emit("stuck")}declareRecoverablePreflightStuck(e,t){const i=this.activeSessionId??"";d.error("codex-adapter",`${t}; restarting adapter and replaying event: ${e}`),this.clearActive({runPendingAutoCompact:!1,emitEventDone:!1}),this.emit("internalError",{eventId:e,sessionId:i,errorMsg:t})}async inspectCurrentTurnProgress(){if(!this.threadId||!this.currentTurnId)return"unknown";try{return await He(this.threadId,this.currentTurnId,this.codexHome)}catch{return"unknown"}}async checkSessionActivitySince(e){if(!this.threadId)return!1;try{return await je(this.threadId,e,this.codexHome)}catch{return!1}}isLongRunningToolItemType(e){if(!e)return!1;const t=e.toLowerCase();return t==="commandexecution"||t==="command_execution"||t==="item/commandexecution"||t==="filechange"||t==="file_change"||t==="item/filechange"}normalizeAgentMessagePhase(e){return typeof e!="string"?void 0:e.trim().toLowerCase()||void 0}resolveAgentMessagePhase(e,t){return this.normalizeAgentMessagePhase(t)??(e?this.agentMessagePhases.get(e):void 0)}extractAgentMessageText(e){if(!e)return;if(typeof e.text=="string"&&e.text.length>0)return e.text;if(typeof e.message=="string"&&e.message.length>0)return e.message;if(!Array.isArray(e.content))return e.text===""||e.message===""?"":void 0;const t=e.content.map(i=>{if(typeof i=="string")return i;if(!i||typeof i!="object"||Array.isArray(i))return;const s=i;return typeof s.text=="string"?s.text:void 0});return t.join("")||(t.every(i=>i!==void 0)?"":void 0)}recordFinalAnswerEvidence(e,t,i=!1){this.turnSawVisibleFinalAnswer=!0,t&&(this.turnSawNonEmptyFinalAnswer=!0);const s=typeof e=="string"&&e?e:void 0;(i||!this.completedEmptyFinalAnswerItems.has(s))&&this.completedEmptyFinalAnswerItems.set(s,s!==void 0&&i&&t==="")}recordDeliveredAgentMessage(e,t){this.turnHasVisibleOutput=!0,e&&this.deliveredAgentMessageIds.add(e),t==="final_answer"&&(this.turnSawVisibleFinalAnswer=!0,this.turnSawNonEmptyFinalAnswer=!0,this.turnHasDeliveredFinalAnswer=!0)}recordDeliveredThinkingAgentMessage(e){e&&this.deliveredThinkingAgentMessageIds.add(e)}shouldSuppressAgentMessageDeltaDuringToolRun(e,t){return t==="final_answer"||this.inFlightToolOps<=0?!1:(d.info("codex-adapter",`Suppressing agentMessage delta during tool run item_id=${e??"unknown"}`),!0)}clearIdleTimer(){this.idleTimer&&(clearTimeout(this.idleTimer),this.idleTimer=null)}rejectAllPending(e){for(const[,t]of this.pendingRequests)clearTimeout(t.timer),t.reject(new Error(`Request canceled: ${e}`));this.pendingRequests.clear(),this.pendingApprovals.clear()}}const me={allow:"accept","allow-once":"accept","allow-always":"acceptForSession",deny:"deny"};function ge(c){const e=c?.agentName?.trim()??"",t=c?.introduction?.trim()??"";if(!e&&!t)return;const i=["[system-identity]"];return e&&i.push(`Your name is "${e}".`),t&&i.push(`Your self-introduction: ${t}`),i.push("Treat this as an out-of-band instruction; do not echo or repeat it in your replies."),i.push("[/system-identity]"),{agentName:e,instructions:i.join(`
|
|
14
|
+
`)}}function vt(c){return c?`${c.instructions}
|
|
15
15
|
|
|
16
|
-
${j}`:j}function
|
|
16
|
+
${j}`:j}function It(c){return c instanceof Error?/unknown\s+(?:field|parameter|property)[^\n]*(?:developerInstructions|developer_instructions)/i.test(c.message):!1}function wt(c){return c&&c.length>0?[...c]:["app-server"]}function u(c){if(typeof c!="string")return;const e=c.trim();return e||void 0}function Tt(c){if(!c||typeof c!="object")return null;const e=c,t=e.thread&&typeof e.thread=="object"?e.thread:void 0;return w(u(e.threadId),u(e.threadID),u(t?.id))??null}function xt(c){return!c||typeof c!="object"?null:u(c.model)??null}function P(c){const e=c?.trim().toLowerCase();return e==="default"||e==="plan"?e:null}function yt(c,e){const t=P(c),i=u(e);if(!(!t||!i))return{mode:t,settings:{model:i}}}function Ct(c){if(!Array.isArray(c))return[];const e=[];for(const t of c){if(typeof t=="string"){const r=t.trim();r&&e.push(bt(r,k(r)));continue}if(!t||typeof t!="object")continue;const i=t,s=w(u(i.id),u(i.model_id),u(i.modelId),u(i.value));if(!s)continue;const n=w(u(i.displayName),u(i.display_name),u(i.name),u(i.label))??k(s);e.push({id:s,displayName:n,defaultReasoningEffort:u(i.defaultReasoningEffort)??null,supportedReasoningEfforts:G(i.supportedReasoningEfforts),defaultServiceTier:u(i.defaultServiceTier)??null,serviceTiers:V(i.serviceTiers),isDefault:i.isDefault===!0})}return e}function ve(c){const e=c.toLowerCase().replace(/\/+$/,"");return!(e==="https://api.openai.com"||e==="https://api.openai.com/v1"||e.endsWith(".openai.com"))}function _t(c){try{const e=new URL(c).hostname.toLowerCase();return e==="localhost"||e==="127.0.0.1"||e==="::1"}catch{return!1}}async function At(c,e){const i=`${c.replace(/\/+$/,"")}/models`,s={Accept:"application/json"};e&&(s.Authorization=`Bearer ${e}`);const n=new AbortController,r=setTimeout(()=>n.abort(),1e4);try{const o=await fetch(i,{headers:s,signal:n.signal});if(!o.ok)return[];const a=await o.json();return St(a)}finally{clearTimeout(r)}}function St(c){if(!c||typeof c!="object")return[];const e=c,t=Array.isArray(e.data)?e.data:[],i=Array.isArray(e.models)?e.models:[],s=t.length>0?t:i,n=[];for(const r of s){if(typeof r=="string"){const f=r.trim();if(!f)continue;n.push({id:f,displayName:k(f),defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!1});continue}if(!r||typeof r!="object")continue;const o=r,a=w(u(o.id),u(o.model));if(!a)continue;const h=w(u(o.displayName),u(o.display_name),u(o.name))??k(a);n.push({id:a,displayName:h,defaultReasoningEffort:u(o.defaultReasoningEffort)??null,supportedReasoningEfforts:G(o.supportedReasoningEfforts),defaultServiceTier:u(o.defaultServiceTier)??null,serviceTiers:V(o.serviceTiers),isDefault:o.isDefault===!0})}return n}function Et(c){if(!c||typeof c!="object")return{models:[],nextCursor:null};const e=c,t=Array.isArray(e.data)?e.data:[],i=[];for(const s of t){if(!s||typeof s!="object")continue;const n=s;if(n.hidden===!0)continue;const r=w(u(n.id),u(n.model));if(!r)continue;const o=w(u(n.displayName),u(n.display_name),u(n.name))??k(r);i.push({id:r,displayName:o,defaultReasoningEffort:u(n.defaultReasoningEffort)??null,supportedReasoningEfforts:G(n.supportedReasoningEfforts),defaultServiceTier:u(n.defaultServiceTier)??null,serviceTiers:V(n.serviceTiers),isDefault:n.isDefault===!0})}return{models:i,nextCursor:u(e.nextCursor)??null}}function Ie(c){const e=[],t=new Set;for(const i of c){const s=i.id.trim(),n=s.toLowerCase();!s||t.has(n)||(t.add(n),e.push({id:s,displayName:i.displayName.trim()||s,defaultReasoningEffort:i.defaultReasoningEffort,supportedReasoningEfforts:[...i.supportedReasoningEfforts],defaultServiceTier:i.defaultServiceTier,serviceTiers:[...i.serviceTiers],isDefault:i.isDefault}))}return e}function bt(c,e){return{id:c,displayName:e,defaultReasoningEffort:null,supportedReasoningEfforts:[],defaultServiceTier:null,serviceTiers:[],isDefault:!1}}function G(c){if(!Array.isArray(c))return[];const e=[];for(const t of c){if(typeof t=="string"){const s=t.trim();s&&e.push(s);continue}if(!t||typeof t!="object")continue;const i=u(t.reasoningEffort);i&&e.push(i)}return e}function V(c){if(!Array.isArray(c))return[];const e=[];for(const t of c){if(typeof t=="string"){const n=t.trim();n&&e.push({id:n,displayName:n,description:null});continue}if(!t||typeof t!="object")continue;const i=t,s=u(i.id);s&&e.push({id:s,displayName:w(u(i.name),u(i.displayName),u(i.display_name))??s,description:u(i.description)??null})}return e}function k(c){return c.split(/[-_\s]+/).filter(Boolean).map(e=>/^(gpt|codex|o\d+)$/i.test(e)?e.toUpperCase():e.charAt(0).toUpperCase()+e.slice(1)).join(" ")}function w(...c){for(const e of c)if(e!=null){if(typeof e=="string"&&!e.trim())continue;return e}}function we(c){return c.trim().replace(/[^a-zA-Z0-9._:-]+/g,"_")}function Te(c,e){switch(c){case"turn/completed":return;case"item/agentMessage/delta":{const t=e?.params??{};if(typeof t.delta!="string"||t.delta.length===0)return;const i=typeof t.phase=="string"&&t.phase.trim()?t.phase.trim().toLowerCase():void 0;return{params:{delta:t.delta,...i?{phase:i}:{}}}}case"item/completed":{const i=(e?.params??{}).item;if(!i||typeof i!="object"||i.type!=="commandExecution")return;const s=Rt(i.command);return{params:{item:{type:"commandExecution",...s!==void 0?{command:s}:{},...i.exitCode!==void 0?{exitCode:i.exitCode}:{},...i.durationMs!==void 0?{durationMs:i.durationMs}:{}}}}}case"item/permissions/requestApproval":return X(e,"permission");case"item/commandExecution/requestApproval":return X(e,"exec");case"item/fileChange/requestApproval":return X(e,"file");default:return}}function X(c,e){if(c==null||typeof c!="object")return;const t=c.params??{};if(t==null||typeof t!="object"||Array.isArray(t))return;const i={},s=(...r)=>{for(const o of r)t[o]!==void 0&&(i[o]=t[o])};e==="permission"?s("threadId","turnId","itemId","reason","permissions"):e==="exec"?s("command","commandLine","command_line","argv","args","availableDecisions","cwd","workingDirectory","working_directory"):s("path","filePath","file_path","targetPath","target_path","paths","files","availableDecisions","cwd","workingDirectory","working_directory");const n={params:i};return c.id!==void 0&&(n.id=c.id),typeof c.method=="string"&&c.method&&(n.method=c.method),n}function Rt(c){if(typeof c!="string")return;const e=c.trim();if(e)return e.length<=ce?e:e.slice(0,ce-le.length)+le}class J extends te{adapterSessionId;constructor(e){super(),this.adapterSessionId=e}emitDone(e){this.emit("done",e)}emitError(e){if(this.listenerCount("error")===0){d.warn("codex-adapter",`Prompt handle error (no listeners): ${e.message}`);return}this.emit("error",e)}async cancel(){}}function kt(c){return["cannot steer a compact turn","turn not found","turn already completed","no active turn"].some(t=>c.includes(t))}export{_ as CodexAdapter,ft as codexAssistantStreamClientMsgId,fe as resolveCodexCommandPath,Te as trimCodexPayloadForWire};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{copyFile as l,mkdir as p}from"node:fs/promises";import{dirname as f,join as d,relative as h,resolve as e,sep as x}from"node:path";import{homedir as v}from"node:os";import{findRolloutFile as m}from"./rollout-locator.js";import{log as a}from"../../core/log/index.js";function H(){return d(v(),".codex")}async function F(t){const r=String(t.threadId??"").trim(),i=e(String(t.privateHome??"").trim());if(!r||!i)return null;const n=m(r,i);if(n)return{path:n,copied:!1};const c=e(t.userHome??H()),o=m(r,c);if(!o)return null;const s=h(c,o);if(!s||s.startsWith("..")||e(c,s)!==e(o))return a.warn("codex-adapter",`rollout import refused: source escapes user home threadId=${r} source=${o}`),null;const u=d(i,s);return await p(f(u),{recursive:!0}),await l(o,u),a.info("codex-adapter",`rollout imported for resume threadId=${r} from=${o} to=${u}`),{path:u,copied:!0}}function P(t,r){const i=e(t)+x,n=e(r);return n===e(t)||n.startsWith(i)}export{F as ensureRolloutInPrivateHome,P as isPathInsideHome,H as resolveUserCodexHome};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{spawn as w}from"node:child_process";import{readFileSync as y,existsSync as O}from"node:fs";import{join as S}from"node:path";import{findRolloutFile as E}from"./rollout-locator.js";import{resolveCodexHome as m}from"./codex-trust.js";import{openReadOnlyDatabase as $}from"../../core/util/sqlite-reader.js";import{log as h}from"../../core/log/index.js";const T="grix-connector";function v(i,e){const r=String(i??"").trim();if(!r)return;const a=m(e),l=E(r,a);if(l)try{const s=y(l,"utf8").slice(0,131072).split(`
|
|
2
|
+
`).find(o=>o.trim());if(s){const o=JSON.parse(s);if(o.type==="session_meta"&&o.payload?.originator)return String(o.payload.originator)}}catch{}const t=S(a,"state_5.sqlite");if(!O(t))return;const d=$(t);if(d)try{const s=d.all("SELECT originator FROM threads WHERE id = ?",r)[0]?.originator;return s?String(s):void 0}catch{return}finally{d.close()}}function C(i,e){return v(i,e)===T}async function J(i){const e=String(i.threadId??"").trim();if(!e)return"skipped";const r=m(i.codexHome);if(!C(e,r))return h.info("codex-adapter",`thread/archive skipped (not connector-owned) threadId=${e} originator=${v(e,r)??"unknown"} home=${r}`),"skipped";try{return i.liveArchive?(await i.liveArchive(),h.info("codex-adapter",`thread/archive ok (live) threadId=${e} home=${r}`),"archived"):(await(i.offlineArchive??I)(e,r,i.command||"codex"),h.info("codex-adapter",`thread/archive ok (offline) threadId=${e} home=${r}`),"archived")}catch(a){const l=a instanceof Error?a.message:String(a);return h.warn("codex-adapter",`thread/archive failed threadId=${e} home=${r} error=${l}`),"failed"}}async function I(i,e,r="codex"){await new Promise((a,l)=>{const t=w(r,["app-server"],{env:{...process.env,CODEX_HOME:e},stdio:["pipe","pipe","pipe"],windowsHide:!0});let d=!1,f="",s=!1;const o=c=>{if(!d){d=!0,clearTimeout(p);try{t.kill("SIGTERM")}catch{}l(c)}},x=()=>{if(!d){d=!0,clearTimeout(p);try{t.kill("SIGTERM")}catch{}a()}},p=setTimeout(()=>o(new Error("thread/archive offline timeout")),15e3),g=c=>{let n;try{n=JSON.parse(c)}catch{return}if(n.id===1){if(n.error){o(new Error(`initialize failed: ${n.error.message??"unknown"}`));return}s||(s=!0,t.stdin?.write(`${JSON.stringify({jsonrpc:"2.0",method:"initialized",params:{}})}
|
|
3
|
+
`),t.stdin?.write(`${JSON.stringify({jsonrpc:"2.0",id:2,method:"thread/archive",params:{threadId:i}})}
|
|
4
|
+
`))}n.id===2&&(n.error?o(new Error(n.error.message??"thread/archive failed")):x())};t.stdout?.setEncoding("utf8"),t.stdout?.on("data",c=>{f+=c;let n;for(;(n=f.indexOf(`
|
|
5
|
+
`))>=0;){const u=f.slice(0,n).trim();f=f.slice(n+1),u&&g(u)}}),t.on("error",c=>o(c)),t.on("exit",c=>{d||o(new Error(`app-server exited before archive ack (code=${c})`))}),t.stdin?.write(`${JSON.stringify({jsonrpc:"2.0",id:1,method:"initialize",params:{clientInfo:{name:"grix-connector",title:"Grix Connector",version:"0"},capabilities:{}}})}
|
|
6
|
+
`)})}export{T as CODEX_CONNECTOR_ORIGINATOR,J as archiveCodexThreadIfOwned,I as archiveCodexThreadViaTempAppServer,C as isConnectorOwnedCodexThread,v as readCodexThreadOriginator};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const o=/no rollout found for thread|thread not found|rollout path missing for thread|invalid thread id/i;function r(t){const n=t instanceof Error?t.message:String(t??"");return o.test(n)}const e=[150,350];function s(t){return t instanceof Error?t.message:String(t??"")}export{e as DEFAULT_RESUME_RETRY_DELAYS_MS,s as formatResumeError,r as isCodexThreadMissingError};
|