@@ -201,6 +201,8 @@ export interface SessionState {
201201 clientCapabilities : ClientCapabilities ;
202202 currentGoal ?: ThreadGoalSnapshot | null ;
203203 goalRevision : number ;
204+ /** Settles once app-server notifications for this thread reach an ACP event handler. */
205+ eventSubscription ?: Promise < void > ;
204206 sessionTitle : string | null ;
205207 sessionTitleSource : "unset" | "fallback" | "explicit" | "unknown" ;
206208 sessionFailure ?: SessionFailure ;
@@ -531,6 +533,7 @@ export class CodexAcpServer {
531533 if ( ! sessionState ) {
532534 throw RequestError . invalidParams ( undefined , `Unknown session: ${ methodRequest . params . sessionId } ` ) ;
533535 }
536+ await this . ensureSessionEventSubscription ( sessionState ) ;
534537 const sessionGeneration = this . getSessionGeneration ( sessionState . sessionId ) ;
535538 const goalControlGeneration = this . bumpGoalControlGeneration ( sessionState . sessionId ) ;
536539 if ( methodRequest . params . action === "set" ) {
@@ -1886,6 +1889,61 @@ export class CodexAcpServer {
18861889 return { outcome : "startedNewTurn" } ;
18871890 }
18881891
1892+ /**
1893+ * Subscribes a session that has not run a prompt yet to its app-server notifications.
1894+ *
1895+ * A prompt subscribes the thread and its handler keeps forwarding session-scoped
1896+ * notifications after the prompt ends. Before the first prompt nothing is subscribed, so
1897+ * goal control would drop the goal update and the goal turn app-server starts for it.
1898+ * The next prompt replaces this handler.
1899+ */
1900+ private ensureSessionEventSubscription ( sessionState : SessionState ) : Promise < void > {
1901+ sessionState . eventSubscription ??= this . subscribeSessionEvents ( sessionState ) . catch ( ( error : unknown ) => {
1902+ delete sessionState . eventSubscription ;
1903+ throw error ;
1904+ } ) ;
1905+ return sessionState . eventSubscription ;
1906+ }
1907+
1908+ private async subscribeSessionEvents ( sessionState : SessionState ) : Promise < void > {
1909+ const eventHandler = new CodexEventHandler (
1910+ this . connection ,
1911+ sessionState ,
1912+ clientSupportsTypedSessionFailures ( this . clientCapabilities ) ,
1913+ this . sessionFailureEpoch ,
1914+ sessionState . subagents ,
1915+ ( accountUpdated ) => this . handleAccountUpdated ( accountUpdated ) ,
1916+ false ,
1917+ clientSupportsCompaction ( this . clientCapabilities ) ,
1918+ clientSupportsNotices ( this . clientCapabilities ) ,
1919+ ) ;
1920+ const permissionContext = this . permissionLifecycleContext ( sessionState ) . beginPrompt ( ) ;
1921+ const toolCallRenderer = new AcpToolCallRenderer ( this . capabilities ) ;
1922+ const signal = new AbortController ( ) . signal ;
1923+ const approvalHandler = new CodexApprovalHandler ( this . connection , permissionContext , signal , toolCallRenderer ) ;
1924+ const elicitationHandler = new CodexElicitationHandler (
1925+ this . connection ,
1926+ permissionContext ,
1927+ this . clientCapabilities ,
1928+ signal ,
1929+ toolCallRenderer ,
1930+ ) ;
1931+ const observeInteraction = async ( event : ServerNotification ) : Promise < void > => {
1932+ permissionContext . handleNotification ( event ) ;
1933+ await elicitationHandler . handleNotification ( event ) ;
1934+ } ;
1935+ await this . codexAcpClient . subscribeToSessionEvents ( sessionState . sessionId ,
1936+ async ( event ) => {
1937+ await observeInteraction ( event ) ;
1938+ await eventHandler . handleSessionScopedNotification ( event ) ;
1939+ } ,
1940+ approvalHandler ,
1941+ elicitationHandler ,
1942+ clientSupportsSubagents ( this . clientCapabilities ) ,
1943+ observeInteraction ,
1944+ childThreadId => eventHandler . waitForNativeSubagentSession ( childThreadId ) ) ;
1945+ }
1946+
18891947 private async startGoalContinuationIfCurrent (
18901948 sessionState : SessionState ,
18911949 sessionGeneration : number ,
@@ -3059,6 +3117,7 @@ export class CodexAcpServer {
30593117 clientSupportsSubagents ( this . clientCapabilities ) ,
30603118 observeInteraction ,
30613119 childThreadId => promptEventHandler . waitForNativeSubagentSession ( childThreadId ) ) ;
3120+ sessionState . eventSubscription = Promise . resolve ( ) ;
30623121
30633122 if ( activePrompt . signal . aborted ) {
30643123 return cancelledPromptResponse ( ) ;
0 commit comments