@@ -27,6 +27,7 @@ import { claimAgentRunContext } from "../../infra/agent-run-registry.js";
2727import type { InputProvenance } from "../../sessions/input-provenance.js" ;
2828import type { SessionWorkAdmissionLease } from "../../sessions/session-lifecycle-admission.js" ;
2929import { registerChatAbortController , resolveAgentRunExpiresAtMs } from "../chat-abort.js" ;
30+ import type { ChatImageContent , OffloadedRef } from "../chat-attachments.js" ;
3031import { errorShapeFromError } from "../error-shape.js" ;
3132import type { AgentRunRequest } from "../server-methods/agent-request-types.js" ;
3233import {
@@ -49,6 +50,11 @@ import {
4950} from "./agent-dedupe.js" ;
5051import type { AgentDeliveryPhaseResult } from "./agent-delivery-phase.js" ;
5152import type { RestoredCronContinuation } from "./agent-handler-helpers.js" ;
53+ import {
54+ prepareAgentRunUserTurn ,
55+ releasePreparedAgentRunUserTurn ,
56+ type PreparedAgentRunUserTurn ,
57+ } from "./agent-run-user-turn.js" ;
5258import type { AgentTurnContext , AgentTurnIo , AgentTurnPrincipal } from "./types.js" ;
5359
5460export type PreparedAgentRunDispatch = {
@@ -65,6 +71,7 @@ export type PreparedAgentRunDispatch = {
6571 lifecycleStorePath : string ;
6672 resolvedThreadId ?: string | number ;
6773 dispatchTaskTrackingMode : Exclude < GatewayAgentTaskTrackingMode , "plugin_subagent" > ;
74+ userTurn : PreparedAgentRunUserTurn ;
6875 restoreAdmittedRestartRecoveryInterrupted ?: ( ) => Promise <
6976 MainSessionRecoveryPendingTarget | undefined
7077 > ;
@@ -76,6 +83,7 @@ export async function prepareAgentRunDispatch(params: {
7683 cfgForAgent ?: OpenClawConfig ;
7784 sessionEntry ?: SessionEntry ;
7885 resolvedSessionKey ?: string ;
86+ requestedSessionKeyRaw ?: string ;
7987 requestedSessionKey ?: string ;
8088 preAcceptedReservedSessionKey ?: string ;
8189 activeSessionAgentId : string ;
@@ -97,6 +105,13 @@ export async function prepareAgentRunDispatch(params: {
97105 inputProvenance ?: InputProvenance ;
98106 isOneShotModelRun : boolean ;
99107 isRestartRecoveryResumeRun : boolean ;
108+ canUseInternalRuntimeHandoff : boolean ;
109+ execApprovalFollowupApprovalId ?: string ;
110+ message : string ;
111+ effectiveTranscriptInputText : string ;
112+ images : ChatImageContent [ ] ;
113+ offloadedRefs : OffloadedRef [ ] ;
114+ requestedPromptPersistenceSuppression : boolean ;
100115 runId : string ;
101116 agentDedupeKeys : readonly string [ ] ;
102117 context : AgentTurnContext ;
@@ -436,6 +451,58 @@ export async function prepareAgentRunDispatch(params: {
436451 return undefined ;
437452 }
438453 }
454+ let userTurn : PreparedAgentRunUserTurn ;
455+ try {
456+ userTurn = await prepareAgentRunUserTurn ( {
457+ request : params . request ,
458+ cfg : params . cfg ,
459+ cfgForAgent : params . cfgForAgent ,
460+ sessionEntry : params . sessionEntry ,
461+ resolvedSessionKey : params . resolvedSessionKey ,
462+ requestedSessionKeyRaw : params . requestedSessionKeyRaw ,
463+ admittedSessionId : params . getAdmittedSessionId ( ) ,
464+ activeSessionAgentId : params . activeSessionAgentId ,
465+ resolvedThreadId,
466+ suppressVisibleSessionEffects : params . suppressVisibleSessionEffects ,
467+ requestedPromptPersistenceSuppression : params . requestedPromptPersistenceSuppression ,
468+ restoredCronContinuation : params . restoredCronContinuation ,
469+ canUseInternalRuntimeHandoff : params . canUseInternalRuntimeHandoff ,
470+ execApprovalFollowupApprovalId : params . execApprovalFollowupApprovalId ,
471+ message : params . message ,
472+ effectiveTranscriptInputText : params . effectiveTranscriptInputText ,
473+ images : params . images ,
474+ offloadedRefs : params . offloadedRefs ,
475+ inputProvenance : params . inputProvenance ,
476+ runId : params . runId ,
477+ client : params . client ,
478+ context : params . context ,
479+ } ) ;
480+ } catch ( err ) {
481+ activeRunAbort . cleanup ( { force : true } ) ;
482+ activeGatewayWorkAdmission . release ( ) ;
483+ params . io . emitAcceptance ( [ false , undefined , errorShapeFromError ( ErrorCodes . UNAVAILABLE , err ) ] ) ;
484+ return undefined ;
485+ }
486+ try {
487+ // Transcript persistence can yield. Revalidate the exact live admission
488+ // before its durable turn is allowed to cross the acceptance boundary.
489+ params . assertGatewayWorkAdmissionAllowed ( ) ;
490+ } catch ( err ) {
491+ releasePreparedAgentRunUserTurn ( userTurn ) ;
492+ activeRunAbort . cleanup ( { force : true } ) ;
493+ activeGatewayWorkAdmission . release ( ) ;
494+ params . io . emitAcceptance ( [
495+ false ,
496+ undefined ,
497+ errorShapeFromError ( ErrorCodes . INVALID_REQUEST , err ) ,
498+ ] ) ;
499+ return undefined ;
500+ }
501+ if ( params . respondToGatewayAdmissionOutcome ( ) ) {
502+ releasePreparedAgentRunUserTurn ( userTurn ) ;
503+ activeRunAbort . cleanup ( { force : true } ) ;
504+ return undefined ;
505+ }
439506 const accepted = {
440507 runId : params . runId ,
441508 sessionKey : params . resolvedSessionKey ,
@@ -486,6 +553,7 @@ export async function prepareAgentRunDispatch(params: {
486553 lifecycleStorePath,
487554 resolvedThreadId,
488555 dispatchTaskTrackingMode,
556+ userTurn,
489557 restoreAdmittedRestartRecoveryInterrupted,
490558 } ;
491559}
0 commit comments