@@ -185,8 +185,8 @@ import {SubagentAssociations} from "./subagents/SubagentAssociations";
185185import { SubagentInteractionHandlers } from "./subagents/SubagentInteractionHandlers" ;
186186import type { SubagentEventRouter } from "./subagents/SubagentEventRouter" ;
187187import { subagentConfigurationOptions , type ObservedSubagentConfiguration } from "./subagents/SubagentConfiguration" ;
188- import { listCodexHooks , trustCodexHooks , type CodexHookIdentity } from "./CodexHookTrust" ;
189- import type { HooksListEntry } from "./app-server/v2/HooksListEntry " ;
188+ import { type CodexHookIdentity } from "./CodexHookTrust" ;
189+ import { CodexHookReview , type ReviewedCodexHooks } from "./CodexHookReview " ;
190190import {
191191 accountFromUpdated ,
192192 agentConfiguredProviderStatus ,
@@ -559,6 +559,7 @@ export class CodexAcpServer {
559559 */
560560 private providerRequests = 0 ;
561561 private appliedProviderRequest = 0 ;
562+ private readonly hookReview = new CodexHookReview ( ( ) => this . codexAcpClient ) ;
562563 /** The `sessionIndex` extension, for AIR or a client-neutral opt-in, see `SessionIndexService.ts`. */
563564 readonly sessionIndex = new SessionIndexService ( {
564565 connection : ( ) => this . connection ,
@@ -783,6 +784,7 @@ export class CodexAcpServer {
783784 this . reportingConnection . reports . compareMeta = this . capabilities . airToolCallContract ;
784785 this . booleanConfigOptionsSupported = clientSupportsBooleanConfigOptions ( _params . clientCapabilities ) ;
785786 this . sessionIndex . configure ( _params . clientCapabilities ) ;
787+ this . hookReview . configure ( _params . clientCapabilities ?. _meta ?. [ "codexHooks" ] , this . capabilities . airClient ) ;
786788 await this . runWithProcessCheck ( ( ) => this . codexAcpClient . initialize ( _params ) ) ;
787789 this . sessionIndex . observe ( this . codexAcpClient ) ;
788790 this . publishFirstAuthStatusAfterResponse ( ) ;
@@ -931,6 +933,7 @@ export class CodexAcpServer {
931933 ...( this . capabilities . asyncTasks && ! this . capabilities . airClient ? {
932934 "async-tasks" : asyncTaskCapability ( ) ,
933935 } : { } ) ,
936+ ...( this . hookReview . enabled ? { codexHooks : { version : 1 , reviewToken : true } } : { } ) ,
934937 // The session index for a client that opted in without AIR, see `docs/air-extensions.md`.
935938 ...( neutralSessionIndex ? {
936939 [ NEUTRAL_SESSION_INDEX_META_KEY ] : neutralSessionIndex ,
@@ -951,18 +954,18 @@ export class CodexAcpServer {
951954 } ;
952955 }
953956
954- async listHooks ( cwd : string ) : Promise < HooksListEntry > {
957+ async listHooks ( cwd : string ) : Promise < ReviewedCodexHooks > {
955958 if ( this . providerUpdate !== null ) await this . waitForProviderUpdate ( ) ;
956959 const appServer = this . ensureAppServer ( ) ;
957960 if ( appServer ) await appServer ;
958- return await this . runWithProcessCheck ( ( ) => listCodexHooks ( this . codexAcpClient . appServerClient , cwd ) ) ;
961+ return await this . runWithProcessCheck ( ( ) => this . hookReview . list ( cwd ) ) ;
959962 }
960963
961- async trustHooks ( cwd : string , hooks : CodexHookIdentity [ ] ) : Promise < { trusted : boolean } > {
964+ async trustHooks ( cwd : string , hooks : CodexHookIdentity [ ] , reviewToken ?: string ) : Promise < { trusted : boolean } > {
962965 if ( this . providerUpdate !== null ) await this . waitForProviderUpdate ( ) ;
963966 const appServer = this . ensureAppServer ( ) ;
964967 if ( appServer ) await appServer ;
965- return { trusted : await this . runWithProcessCheck ( ( ) => trustCodexHooks ( this . codexAcpClient . appServerClient , cwd , hooks ) ) } ;
968+ return { trusted : await this . runWithProcessCheck ( ( ) => this . hookReview . trust ( cwd , hooks , reviewToken ) ) } ;
966969 }
967970
968971 async extMethod ( method : string , params : Record < string , unknown > ) : Promise < Record < string , unknown > > {
@@ -1820,6 +1823,10 @@ export class CodexAcpServer {
18201823
18211824 async loadSession ( params : acp . LoadSessionRequest ) : Promise < LegacyLoadSessionResponse > {
18221825 params = this . withV1SessionInstructionAppend ( params ) ;
1826+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . loadSessionReviewed ( params ) ) ;
1827+ }
1828+
1829+ private async loadSessionReviewed ( params : acp . LoadSessionRequest ) : Promise < LegacyLoadSessionResponse > {
18231830 return this . recovery === null
18241831 ? await this . loadSessionOnce ( params )
18251832 : await this . recovery . trackOpen ( params . sessionId , ( ) => this . loadSessionOnce ( params ) ) ;
@@ -1907,6 +1914,10 @@ export class CodexAcpServer {
19071914
19081915 async resumeSession ( params : WithAcpMcpServers < acp . ResumeSessionRequest > ) : Promise < LegacyResumeSessionResponse > {
19091916 params = this . withV1SessionInstructionAppend ( params ) ;
1917+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . resumeSessionReviewed ( params ) ) ;
1918+ }
1919+
1920+ private async resumeSessionReviewed ( params : WithAcpMcpServers < acp . ResumeSessionRequest > ) : Promise < LegacyResumeSessionResponse > {
19101921 return this . withSystemPromptUpdate ( params , async ( ) => this . recovery === null
19111922 ? await this . resumeSessionOnce ( params )
19121923 : await this . recovery . trackOpen ( params . sessionId , ( ) => this . resumeSessionOnce ( params ) ) ) ;
@@ -1960,7 +1971,8 @@ export class CodexAcpServer {
19601971 params = this . withV1SessionInstructionAppend ( params ) ;
19611972 clientSystemPromptText ( params . systemPrompt ) ;
19621973 this . assertInstructionsReady ( params . sessionId ) ;
1963- return this . sessionIndex . forking ( params . sessionId , ( ) => this . createFork ( params ) ) ;
1974+ return this . hookReview . run ( params . cwd , params . _meta ,
1975+ ( ) => this . sessionIndex . forking ( params . sessionId , ( ) => this . createFork ( params ) ) ) ;
19641976 }
19651977
19661978 private async createFork ( params : WithAcpMcpServers < acp . ForkSessionRequest > ) : Promise < acp . ForkSessionResponse > {
@@ -2119,6 +2131,10 @@ export class CodexAcpServer {
21192131 params : WithAcpMcpServers < acp . NewSessionRequest > ,
21202132 ) : Promise < LegacyNewSessionResponse > {
21212133 params = this . withV1SessionInstructionAppend ( params ) ;
2134+ return this . hookReview . run ( params . cwd , params . _meta , ( ) => this . newSessionReviewed ( params ) ) ;
2135+ }
2136+
2137+ private async newSessionReviewed ( params : WithAcpMcpServers < acp . NewSessionRequest > ) : Promise < LegacyNewSessionResponse > {
21222138 if ( this . providerUpdate !== null ) {
21232139 await this . waitForProviderUpdate ( ) ;
21242140 }
@@ -2159,6 +2175,7 @@ export class CodexAcpServer {
21592175 const appServer = this . ensureAppServer ( ) ;
21602176 if ( appServer ) await appServer ;
21612177 const elicitationRequester = this . createUrlElicitationRequester ( requestId ) ;
2178+ this . hookReview . invalidate ( ) ;
21622179 const isAuthenticated = await this . runWithProcessCheck ( ( ) => this . codexAcpClient . authenticate ( _params , elicitationRequester ) ) ;
21632180 if ( ! isAuthenticated ) {
21642181 logger . log ( "Authenticate request failed" ) ;
@@ -2231,13 +2248,15 @@ export class CodexAcpServer {
22312248 }
22322249 this . appliedProviderRequest = request ;
22332250 this . codexAcpClient . setProvider ( gatewayConfig ) ;
2251+ this . hookReview . invalidate ( ) ;
22342252 // The restarted app-server takes the listing too and does not list the models again.
22352253 await this . enqueueProviderUpdate ( ( client ) => client . setProvider ( gatewayConfig ) ) ;
22362254 return { } ;
22372255 }
22382256
22392257 async disableProvider ( params : acp . DisableProviderRequest ) : Promise < acp . DisableProviderResponse > {
22402258 this . codexAcpClient . disableProvider ( params ) ;
2259+ if ( params . providerId === OPENAI_PROVIDER_ID ) this . hookReview . invalidate ( ) ;
22412260 if ( params . providerId !== OPENAI_PROVIDER_ID ) {
22422261 return { } ;
22432262 }
@@ -2278,6 +2297,7 @@ export class CodexAcpServer {
22782297 throw new Error ( "Cannot restart Codex app-server before ACP initialization" ) ;
22792298 }
22802299 await replacement . initialize ( this . initializeRequest ) ;
2300+ this . hookReview . bind ( replacement ) ;
22812301 this . codexAcpClient = replacement ;
22822302 this . sessionIndex . observe ( replacement ) ;
22832303 this . availableCommands = this . createAvailableCommands ( replacement ) ;
@@ -2315,6 +2335,7 @@ export class CodexAcpServer {
23152335 // The new app-server died: the next use of each session resumes it in the restarted one.
23162336 break ;
23172337 }
2338+ if ( this . hookReview . enabled ) continue ;
23182339 // Registered before `resumeSession`, so a goal turn Codex auto-starts within a
23192340 // few ms of `thread/resume`'s response can't slip past an empty subscription
23202341 // registry on the new client (10(f1) `startCodexTurnTracker`).
@@ -2445,7 +2466,9 @@ export class CodexAcpServer {
24452466 currentClient : ( ) => this . codexAcpClient ,
24462467 createClient : ( connection ) => {
24472468 this . captureStderr ( ) ;
2448- return this . codexAcpClient . withAppServer ( new CodexAppServerClient ( connection . connection ) ) ;
2469+ const client = this . codexAcpClient . withAppServer ( new CodexAppServerClient ( connection . connection ) ) ;
2470+ this . hookReview . bind ( client ) ;
2471+ return client ;
24492472 } ,
24502473 handshake : ( client ) => this . initializeRequest !== null && this . codexAcpClient . initialized
24512474 ? client . initialize ( this . initializeRequest )
@@ -2493,6 +2516,7 @@ export class CodexAcpServer {
24932516 // A `providers/set` can have changed the routing of the old client while the restart ran.
24942517 client . adoptRoutingFrom ( this . codexAcpClient ) ;
24952518 this . codexAcpClient = client ;
2519+ this . hookReview . bind ( client ) ;
24962520 // The session index watches the thread notifications of the app-server that runs now.
24972521 this . sessionIndex . observe ( client ) ;
24982522 this . availableCommands = this . createAvailableCommands ( client ) ;
@@ -4828,6 +4852,16 @@ export class CodexAcpServer {
48284852 signal ?: AbortSignal ,
48294853 onTurnStarted ?: ( ) => void ,
48304854 insertion ?: UserMessageInsertion ,
4855+ ) : Promise < acp . PromptResponse > {
4856+ return this . hookReview . run ( this . getSessionState ( params . sessionId ) . cwd , params . _meta ,
4857+ ( ) => this . promptReviewed ( params , signal , onTurnStarted , insertion ) ) ;
4858+ }
4859+
4860+ private async promptReviewed (
4861+ params : acp . PromptRequest ,
4862+ signal ?: AbortSignal ,
4863+ onTurnStarted ?: ( ) => void ,
4864+ insertion ?: UserMessageInsertion ,
48314865 ) : Promise < acp . PromptResponse > {
48324866 if ( this . providerUpdate !== null ) {
48334867 await this . waitForProviderUpdate ( ) ;
0 commit comments