@@ -16,14 +16,14 @@ export class CodexHookReviewError extends RequestError {
1616}
1717
1818export type ReviewedCodexHooks = HooksListEntry & { reviewToken ?: string ; codexHome ?: string } ;
19- type Review = { cwd : string ; token : string ; fingerprint : string ; revision : number ; client : CodexAppServerClient } ;
19+ type Review = { cwd : string ; token : string ; fingerprint : string ; revision : number ; client : CodexAppServerClient ; provider : string ; providerFingerprint : string } ;
2020
2121/** Owns ephemeral reviews. Request context also reaches lazy recovery and internal first-turn operations. */
2222export class CodexHookReview {
2323 enabled = false ;
2424 private revision = 0 ;
2525 private readonly reviews = new Map < string , Review > ( ) ;
26- private readonly context = new AsyncLocalStorage < { cwd : string ; token : unknown } > ( ) ;
26+ private readonly context = new AsyncLocalStorage < { cwd : string ; token : unknown ; dispatched : boolean } > ( ) ;
2727
2828 constructor ( private readonly client : ( ) => CodexAcpClient ) { }
2929
@@ -40,29 +40,49 @@ export class CodexHookReview {
4040 }
4141
4242 bind ( client : CodexAcpClient ) : void {
43- client . appServerClient . hookExecutionGate = this . enabled ? async cwd => {
43+ client . appServerClient . hookExecutionGate = this . enabled ? async ( cwd , provider ) => {
4444 const context = this . context . getStore ( ) ;
45- if ( context === undefined || ( cwd !== undefined && cwd !== context . cwd ) ) throw new CodexHookReviewError ( "codex_hook_review_required" ) ;
46- return await this . validate ( client . appServerClient , context . cwd , context . token ) ;
45+ const fail = ( error : unknown ) : never => {
46+ if ( context ?. dispatched && error instanceof CodexHookReviewError ) {
47+ throw RequestError . internalError ( undefined ,
48+ "Codex hook review changed after this operation began executing. Review again before sending new work; do not retry the completed work automatically." ) ;
49+ }
50+ throw error ;
51+ } ;
52+ try {
53+ if ( context === undefined || ( cwd !== undefined && cwd !== context . cwd ) ) throw new CodexHookReviewError ( "codex_hook_review_required" ) ;
54+ const fence = await this . validate ( client , context . cwd , context . token , provider ) ;
55+ return ( ) => {
56+ try { fence ( ) ; } catch ( error ) { fail ( error ) ; }
57+ context . dispatched = true ;
58+ } ;
59+ } catch ( error ) { return fail ( error ) ; }
4760 } : undefined ;
4861 }
4962
5063 run < T > ( cwd : string , meta : unknown , operation : ( ) => Promise < T > ) : Promise < T > {
5164 if ( ! this . enabled ) return operation ( ) ;
5265 const hooks = ( meta as { codexHooks ?: { reviewToken ?: unknown } } | undefined ) ?. codexHooks ;
53- return this . context . run ( { cwd, token : hooks ?. reviewToken } , operation ) ;
66+ return this . context . run ( { cwd, token : hooks ?. reviewToken , dispatched : false } , operation ) ;
67+ }
68+
69+ continueOperation < T > ( cwd : string , meta : unknown , operation : ( ) => Promise < T > ) : Promise < T > {
70+ return this . context . getStore ( ) === undefined ? this . run ( cwd , meta , operation ) : operation ( ) ;
5471 }
5572
5673 async list ( cwd : string ) : Promise < ReviewedCodexHooks > {
5774 const client = this . client ( ) ;
5875 const revision = this . revision ;
59- const entry = await listCodexHooks ( client . appServerClient , cwd ) ;
60- if ( ! this . enabled ) return entry ;
76+ if ( ! this . enabled ) return await listCodexHooks ( client . appServerClient , cwd ) ;
77+ const [ entry , provider ] = await Promise . all ( [
78+ listCodexHooks ( client . appServerClient , cwd ) , client . hookReviewProviderInputs ( ) ,
79+ ] ) ;
6180 this . assertCurrent ( client . appServerClient , revision ) ;
62- const fingerprint = definitionFingerprint ( entry ) ;
81+ const definitions = definitionFingerprint ( entry ) ;
82+ const providerFingerprint = fingerprint ( provider ) ;
6383 const previous = this . reviews . get ( cwd ) ;
64- const review = previous ?. client === client . appServerClient && previous . revision === revision && previous . fingerprint === fingerprint
65- ? previous : { cwd, token : randomUUID ( ) , fingerprint, revision, client : client . appServerClient } ;
84+ const review = previous ?. client === client . appServerClient && previous . revision === revision && previous . fingerprint === definitions && previous . providerFingerprint === providerFingerprint
85+ ? previous : { cwd, token : randomUUID ( ) , fingerprint : definitions , revision, client : client . appServerClient , provider : provider . modelProvider , providerFingerprint } ;
6686 this . reviews . set ( cwd , review ) ;
6787 const home = client . getHomePath ( ) ;
6888 return { ...entry , reviewToken : review . token , ...( home === null ? { } : { codexHome : home } ) } ;
@@ -72,6 +92,8 @@ export class CodexHookReview {
7292 const client = this . client ( ) . appServerClient ;
7393 if ( ! this . enabled ) return await trustCodexHooks ( client , cwd , requested ) ;
7494 const review = this . requireReview ( client , cwd , token ) ;
95+ const fence = await this . validate ( this . client ( ) , cwd , token ) ;
96+ fence ( ) ;
7597 if ( requested . length === 0 || new Set ( requested . map ( hook => hook . key ) ) . size !== requested . length
7698 || requested . some ( hook => ! hook . key || ! hook . currentHash ) ) throw new CodexHookReviewError ( "codex_hook_changed" ) ;
7799 return await trustCodexHooks ( client , cwd , requested , entry => {
@@ -85,12 +107,18 @@ export class CodexHookReview {
85107 } , ( ) => { this . requireReview ( client , cwd , token ) ; } ) ;
86108 }
87109
88- private async validate ( client : CodexAppServerClient , cwd : string , token : unknown ) : Promise < ( ) => void > {
89- const review = this . requireReview ( client , cwd , token ) ;
90- const entry = await listCodexHooks ( client , cwd ) ;
91- this . checkDefinition ( client , review , entry ) ;
110+ private async validate ( client : CodexAcpClient , cwd : string , token : unknown , requestedProvider ?: string | null ) : Promise < ( ) => void > {
111+ const review = this . requireReview ( client . appServerClient , cwd , token ) ;
112+ const [ entry , provider ] = await Promise . all ( [
113+ listCodexHooks ( client . appServerClient , cwd ) , client . hookReviewProviderInputs ( ) ,
114+ ] ) ;
115+ this . checkDefinition ( client . appServerClient , review , entry ) ;
116+ if ( fingerprint ( provider ) !== review . providerFingerprint
117+ || ( requestedProvider != null && requestedProvider !== review . provider ) ) {
118+ throw new CodexHookReviewError ( "codex_hook_changed" ) ;
119+ }
92120 // The caller invokes this synchronously at dispatch, after the await and any thread-load queue.
93- return ( ) => { this . requireReview ( client , cwd , token ) ; } ;
121+ return ( ) => { this . requireReview ( client . appServerClient , cwd , token ) ; } ;
94122 }
95123
96124 private requireReview ( client : CodexAppServerClient , cwd : string , token : unknown ) : Review {
@@ -117,5 +145,15 @@ export class CodexHookReview {
117145function definitionFingerprint ( entry : HooksListEntry ) : string {
118146 // Trust-state changes are expected after consent; every definition field and diagnostic remains bound.
119147 const value = { ...entry , hooks : entry . hooks . map ( ( { trustStatus : _trust , ...definition } ) => definition ) } ;
120- return createHash ( "sha256" ) . update ( JSON . stringify ( value , ( _key , value ) => typeof value === "bigint" ? value . toString ( ) : value ) ) . digest ( "hex" ) ;
148+ return fingerprint ( value ) ;
149+ }
150+
151+ function fingerprint ( value : unknown ) : string {
152+ return createHash ( "sha256" ) . update ( JSON . stringify ( value , ( _key , value ) => {
153+ if ( typeof value === "bigint" ) return value . toString ( ) ;
154+ if ( value !== null && typeof value === "object" && ! Array . isArray ( value ) ) {
155+ return Object . fromEntries ( Object . entries ( value ) . sort ( ( [ a ] , [ b ] ) => a . localeCompare ( b ) ) ) ;
156+ }
157+ return value ;
158+ } ) ) . digest ( "hex" ) ;
121159}
0 commit comments