@@ -177,8 +177,8 @@ import {
177177 type SubagentModels ,
178178} from "./subagents/CodexSubagentEventRouter" ;
179179import { nameFromAgentPath } from "./subagents/CodexAgentPath" ;
180- import { listCodexHooks , trustCodexHooks , type CodexHookIdentity } from "./CodexHookTrust" ;
181- import type { HooksListEntry } from "./app-server/v2/HooksListEntry " ;
180+ import { type CodexHookIdentity } from "./CodexHookTrust" ;
181+ import { CodexHookReview , type ReviewedCodexHooks } from "./CodexHookReview " ;
182182import {
183183 accountFromUpdated ,
184184 agentConfiguredProviderStatus ,
@@ -548,6 +548,7 @@ export class CodexAcpServer {
548548 */
549549 private providerRequests = 0 ;
550550 private appliedProviderRequest = 0 ;
551+ private readonly hookReview = new CodexHookReview ( ( ) => this . codexAcpClient ) ;
551552 /** The `sessionIndex` extension, for AIR or a client-neutral opt-in, see `SessionIndexService.ts`. */
552553 readonly sessionIndex = new SessionIndexService ( {
553554 connection : ( ) => this . connection ,
@@ -649,6 +650,7 @@ export class CodexAcpServer {
649650 this . reportingConnection . reports . compareMeta = this . capabilities . airToolCallContract ;
650651 this . booleanConfigOptionsSupported = clientSupportsBooleanConfigOptions ( _params . clientCapabilities ) ;
651652 this . sessionIndex . configure ( _params . clientCapabilities ) ;
653+ this . hookReview . configure ( _params . clientCapabilities ?. _meta ?. [ "codexHooks" ] , this . capabilities . airClient ) ;
652654 await this . runWithProcessCheck ( ( ) => this . codexAcpClient . initialize ( _params ) ) ;
653655 this . sessionIndex . observe ( this . codexAcpClient ) ;
654656 this . publishFirstAuthStatusAfterResponse ( ) ;
@@ -797,6 +799,7 @@ export class CodexAcpServer {
797799 ...( this . capabilities . asyncTasks && ! this . capabilities . airClient ? {
798800 "async-tasks" : asyncTaskCapability ( ) ,
799801 } : { } ) ,
802+ ...( this . hookReview . enabled ? { codexHooks : { version : 1 , reviewToken : true } } : { } ) ,
800803 // The session index for a client that opted in without AIR, see `docs/air-extensions.md`.
801804 ...( neutralSessionIndex ? {
802805 [ NEUTRAL_SESSION_INDEX_META_KEY ] : neutralSessionIndex ,
@@ -817,18 +820,18 @@ export class CodexAcpServer {
817820 } ;
818821 }
819822
820- async listHooks ( cwd : string ) : Promise < HooksListEntry > {
823+ async listHooks ( cwd : string ) : Promise < ReviewedCodexHooks > {
821824 if ( this . providerUpdate !== null ) await this . waitForProviderUpdate ( ) ;
822825 const appServer = this . ensureAppServer ( ) ;
823826 if ( appServer ) await appServer ;
824- return await this . runWithProcessCheck ( ( ) => listCodexHooks ( this . codexAcpClient . appServerClient , cwd ) ) ;
827+ return await this . runWithProcessCheck ( ( ) => this . hookReview . list ( cwd ) ) ;
825828 }
826829
827- async trustHooks ( cwd : string , hooks : CodexHookIdentity [ ] ) : Promise < { trusted : boolean } > {
830+ async trustHooks ( cwd : string , hooks : CodexHookIdentity [ ] , reviewToken ?: string ) : Promise < { trusted : boolean } > {
828831 if ( this . providerUpdate !== null ) await this . waitForProviderUpdate ( ) ;
829832 const appServer = this . ensureAppServer ( ) ;
830833 if ( appServer ) await appServer ;
831- return { trusted : await this . runWithProcessCheck ( ( ) => trustCodexHooks ( this . codexAcpClient . appServerClient , cwd , hooks ) ) } ;
834+ return { trusted : await this . runWithProcessCheck ( ( ) => this . hookReview . trust ( cwd , hooks , reviewToken ) ) } ;
832835 }
833836
834837 async extMethod ( method : string , params : Record < string , unknown > ) : Promise < Record < string , unknown > > {
@@ -1679,6 +1682,10 @@ export class CodexAcpServer {
16791682
16801683 async loadSession ( params : acp . LoadSessionRequest ) : Promise < LegacyLoadSessionResponse > {
16811684 params = this . withV1SessionInstructionAppend ( params ) ;
1685+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . loadSessionReviewed ( params ) ) ;
1686+ }
1687+
1688+ private async loadSessionReviewed ( params : acp . LoadSessionRequest ) : Promise < LegacyLoadSessionResponse > {
16821689 return this . recovery === null
16831690 ? await this . loadSessionOnce ( params )
16841691 : await this . recovery . trackOpen ( params . sessionId , ( ) => this . loadSessionOnce ( params ) ) ;
@@ -1766,6 +1773,10 @@ export class CodexAcpServer {
17661773
17671774 async resumeSession ( params : WithAcpMcpServers < acp . ResumeSessionRequest > ) : Promise < LegacyResumeSessionResponse > {
17681775 params = this . withV1SessionInstructionAppend ( params ) ;
1776+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . resumeSessionReviewed ( params ) ) ;
1777+ }
1778+
1779+ private async resumeSessionReviewed ( params : WithAcpMcpServers < acp . ResumeSessionRequest > ) : Promise < LegacyResumeSessionResponse > {
17691780 return this . withSystemPromptUpdate ( params , async ( ) => this . recovery === null
17701781 ? await this . resumeSessionOnce ( params )
17711782 : await this . recovery . trackOpen ( params . sessionId , ( ) => this . resumeSessionOnce ( params ) ) ) ;
@@ -1819,7 +1830,8 @@ export class CodexAcpServer {
18191830 params = this . withV1SessionInstructionAppend ( params ) ;
18201831 clientSystemPromptText ( params . systemPrompt ) ;
18211832 this . assertInstructionsReady ( params . sessionId ) ;
1822- return this . sessionIndex . forking ( params . sessionId , ( ) => this . createFork ( params ) ) ;
1833+ return this . hookReview . run ( params . cwd , params . _meta ,
1834+ ( ) => this . sessionIndex . forking ( params . sessionId , ( ) => this . createFork ( params ) ) ) ;
18231835 }
18241836
18251837 private async createFork ( params : WithAcpMcpServers < acp . ForkSessionRequest > ) : Promise < acp . ForkSessionResponse > {
@@ -1976,6 +1988,10 @@ export class CodexAcpServer {
19761988 params : WithAcpMcpServers < acp . NewSessionRequest > ,
19771989 ) : Promise < LegacyNewSessionResponse > {
19781990 params = this . withV1SessionInstructionAppend ( params ) ;
1991+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . newSessionReviewed ( params ) ) ;
1992+ }
1993+
1994+ private async newSessionReviewed ( params : WithAcpMcpServers < acp . NewSessionRequest > ) : Promise < LegacyNewSessionResponse > {
19791995 if ( this . providerUpdate !== null ) {
19801996 await this . waitForProviderUpdate ( ) ;
19811997 }
@@ -2016,6 +2032,7 @@ export class CodexAcpServer {
20162032 const appServer = this . ensureAppServer ( ) ;
20172033 if ( appServer ) await appServer ;
20182034 const elicitationRequester = this . createUrlElicitationRequester ( requestId ) ;
2035+ this . hookReview . invalidate ( ) ;
20192036 const isAuthenticated = await this . runWithProcessCheck ( ( ) => this . codexAcpClient . authenticate ( _params , elicitationRequester ) ) ;
20202037 if ( ! isAuthenticated ) {
20212038 logger . log ( "Authenticate request failed" ) ;
@@ -2088,13 +2105,15 @@ export class CodexAcpServer {
20882105 }
20892106 this . appliedProviderRequest = request ;
20902107 this . codexAcpClient . setProvider ( gatewayConfig ) ;
2108+ this . hookReview . invalidate ( ) ;
20912109 // The restarted app-server takes the listing too and does not list the models again.
20922110 await this . enqueueProviderUpdate ( ( client ) => client . setProvider ( gatewayConfig ) ) ;
20932111 return { } ;
20942112 }
20952113
20962114 async disableProvider ( params : acp . DisableProviderRequest ) : Promise < acp . DisableProviderResponse > {
20972115 this . codexAcpClient . disableProvider ( params ) ;
2116+ if ( params . providerId === OPENAI_PROVIDER_ID ) this . hookReview . invalidate ( ) ;
20982117 if ( params . providerId !== OPENAI_PROVIDER_ID ) {
20992118 return { } ;
21002119 }
@@ -2132,6 +2151,7 @@ export class CodexAcpServer {
21322151 throw new Error ( "Cannot restart Codex app-server before ACP initialization" ) ;
21332152 }
21342153 await replacement . initialize ( this . initializeRequest ) ;
2154+ this . hookReview . bind ( replacement ) ;
21352155 this . codexAcpClient = replacement ;
21362156 this . sessionIndex . observe ( replacement ) ;
21372157 this . availableCommands = this . createAvailableCommands ( replacement ) ;
@@ -2168,6 +2188,7 @@ export class CodexAcpServer {
21682188 // The new app-server died: the next use of each session resumes it in the restarted one.
21692189 break ;
21702190 }
2191+ if ( this . hookReview . enabled ) continue ;
21712192 // Registered before `resumeSession`, so a goal turn Codex auto-starts within a
21722193 // few ms of `thread/resume`'s response can't slip past an empty subscription
21732194 // registry on the new client (10(f1) `startCodexTurnTracker`).
@@ -2298,7 +2319,9 @@ export class CodexAcpServer {
22982319 currentClient : ( ) => this . codexAcpClient ,
22992320 createClient : ( connection ) => {
23002321 this . captureStderr ( ) ;
2301- return this . codexAcpClient . withAppServer ( new CodexAppServerClient ( connection . connection ) ) ;
2322+ const client = this . codexAcpClient . withAppServer ( new CodexAppServerClient ( connection . connection ) ) ;
2323+ this . hookReview . bind ( client ) ;
2324+ return client ;
23022325 } ,
23032326 handshake : ( client ) => this . initializeRequest !== null && this . codexAcpClient . initialized
23042327 ? client . initialize ( this . initializeRequest )
@@ -2341,6 +2364,7 @@ export class CodexAcpServer {
23412364 // A `providers/set` can have changed the routing of the old client while the restart ran.
23422365 client . adoptRoutingFrom ( this . codexAcpClient ) ;
23432366 this . codexAcpClient = client ;
2367+ this . hookReview . bind ( client ) ;
23442368 // The session index watches the thread notifications of the app-server that runs now.
23452369 this . sessionIndex . observe ( client ) ;
23462370 this . availableCommands = this . createAvailableCommands ( client ) ;
@@ -4553,6 +4577,16 @@ export class CodexAcpServer {
45534577 signal ?: AbortSignal ,
45544578 onTurnStarted ?: ( ) => void ,
45554579 insertion ?: UserMessageInsertion ,
4580+ ) : Promise < acp . PromptResponse > {
4581+ return this . hookReview . run ( this . getSessionState ( params . sessionId ) . cwd , params . _meta ,
4582+ ( ) => this . promptReviewed ( params , signal , onTurnStarted , insertion ) ) ;
4583+ }
4584+
4585+ private async promptReviewed (
4586+ params : acp . PromptRequest ,
4587+ signal ?: AbortSignal ,
4588+ onTurnStarted ?: ( ) => void ,
4589+ insertion ?: UserMessageInsertion ,
45564590 ) : Promise < acp . PromptResponse > {
45574591 if ( this . providerUpdate !== null ) {
45584592 await this . waitForProviderUpdate ( ) ;
0 commit comments