@@ -112,6 +112,7 @@ import {
112112 type ReplyOperation ,
113113} from "./reply-run-registry.js" ;
114114import { resolveReplyToMode } from "./reply-threading.js" ;
115+ import { admitReplyTurn } from "./reply-turn-admission.js" ;
115116import { resolveRoutedDeliveryThreadId } from "./routed-delivery-thread.js" ;
116117import { resolveRuntimePolicySessionKey } from "./runtime-policy-session-key.js" ;
117118import type { ReplySessionEntryHandle } from "./session-entry-handle.js" ;
@@ -1076,7 +1077,23 @@ export async function runPreparedReply(
10761077 }
10771078 }
10781079 const providedReplyOperation = opts ?. replyOperation ;
1080+ // Native command turns reserve their operation under the slash SOURCE key so
1081+ // commands never contend with the target's active run (#104144). When such a
1082+ // turn continues into a full agent turn (/steer fallback, /goal, /learn,
1083+ // unhandled body), it mutates the TARGET session: it must adopt the target's
1084+ // run slot before running or concurrent target inbounds double-admit and
1085+ // split the session (#104844).
1086+ const commandTurnContinuationTargetKey =
1087+ providedReplyOperation !== undefined &&
1088+ providedReplyOperation . result === null &&
1089+ providedReplyOperation . phase === "queued" &&
1090+ sessionKey !== undefined &&
1091+ providedReplyOperation . key !== sessionKey &&
1092+ resolveCommandTurnTargetSessionKey ( ctx ) !== undefined
1093+ ? sessionKey
1094+ : undefined ;
10791095 if (
1096+ commandTurnContinuationTargetKey === undefined &&
10801097 providedReplyOperation !== undefined &&
10811098 providedReplyOperation . result === null &&
10821099 providedReplyOperation . phase === "queued" &&
@@ -1085,7 +1102,9 @@ export async function runPreparedReply(
10851102 ) {
10861103 // Dispatch reserves a queued operation before session init. If stale init
10871104 // rotates the session, move the reservation so later steer/abort paths
1088- // target the session that will actually run.
1105+ // target the session that will actually run. Command-turn continuations
1106+ // rebind after slot adoption below: rebinding first would collide with a
1107+ // still-active target operation that owns the same session ID.
10891108 providedReplyOperation . updateSessionId ( sessionId ) ;
10901109 }
10911110 const isOwnPreDispatchOperationSession = ( candidateSessionId : string | undefined ) : boolean =>
@@ -1295,6 +1314,35 @@ export async function runPreparedReply(
12951314 isReplyRunStreamingForSessionId ( replyOperationActiveSessionId ) ) ,
12961315 } ;
12971316 } ;
1317+ if ( commandTurnContinuationTargetKey && providedReplyOperation ) {
1318+ const adoption = await admitReplyTurn ( {
1319+ sessionKey : commandTurnContinuationTargetKey ,
1320+ sessionId : providedReplyOperation . sessionId ,
1321+ expectedSessionId : preparedSessionState . sessionEntry ?. sessionId ,
1322+ storePath,
1323+ kind : "visible" ,
1324+ resetTriggered : effectiveResetTriggered ,
1325+ routeThreadId : currentRouteThreadId ,
1326+ upstreamAbortSignal : opts ?. abortSignal ,
1327+ // Never wait on the target's active turn: a blocked continuation would
1328+ // re-create the #104142 command-lane wedge. When the slot is owned, the
1329+ // queue policy below sees the target-keyed owner and steers/queues this
1330+ // turn instead of running it concurrently.
1331+ waitForActive : false ,
1332+ adoptOperation : providedReplyOperation ,
1333+ } ) ;
1334+ if ( adoption . status === "skipped" && adoption . reason === "aborted" ) {
1335+ typing . cleanup ( ) ;
1336+ return undefined ;
1337+ }
1338+ if (
1339+ adoption . status === "owned" &&
1340+ sessionId !== undefined &&
1341+ sessionId !== providedReplyOperation . sessionId
1342+ ) {
1343+ providedReplyOperation . updateSessionId ( sessionId ) ;
1344+ }
1345+ }
12981346 const { activeSessionId, isActive, isStreaming } = resolveQueueBusyState ( ) ;
12991347 const activeRunAcceptsCurrentThread = resolveActiveRunAcceptsCurrentThread ( { isActive } ) ;
13001348 const isHeartbeatRun = opts ?. isHeartbeat === true ;
0 commit comments