@@ -33,6 +33,7 @@ import {
3333} from "../cli-output.js" ;
3434import { classifyFailoverReason } from "../embedded-agent-helpers.js" ;
3535import { FailoverError , resolveFailoverStatus } from "../failover-error.js" ;
36+ import { prepareCliBundleMcpCaptureAttempt } from "./bundle-mcp.js" ;
3637import { buildClaudeOwnerKey } from "./helpers.js" ;
3738import { cliBackendLog , formatCliBackendOutputDigest } from "./log.js" ;
3839import type { PreparedCliRunContext } from "./types.js" ;
@@ -1063,39 +1064,49 @@ async function createClaudeLiveSession(params: {
10631064 cleanup : ( ) => Promise < void > ;
10641065} ) : Promise < ClaudeLiveSession > {
10651066 let session : ClaudeLiveSession | null = null ;
1066- const managedRun = await params . supervisor . spawn ( {
1067- sessionId : params . context . params . sessionId ,
1068- backendId : params . context . backendResolved . id ,
1069- scopeKey : `claude-live:${ params . key } ` ,
1070- replaceExistingScope : true ,
1071- mode : "child" ,
1072- argv : params . argv ,
1073- cwd : params . context . cwd ?? params . context . workspaceDir ,
1074- env : params . mcpCaptureKey
1075- ? { ...params . env , OPENCLAW_MCP_CLI_CAPTURE_KEY : params . mcpCaptureKey }
1076- : params . env ,
1077- stdinMode : "pipe-open" ,
1078- captureOutput : false ,
1079- onStdout : ( chunk ) => {
1080- if ( session ) {
1081- handleClaudeStdout ( session , chunk ) ;
1082- }
1083- } ,
1084- onStderr : ( chunk ) => {
1085- if ( session ) {
1086- session . stderr += chunk ;
1087- if ( session . stderr . length > CLAUDE_LIVE_MAX_STDERR_CHARS ) {
1088- closeLiveSession (
1089- session ,
1090- "abort" ,
1091- createOutputLimitError ( session , "Claude CLI stderr exceeded limit." ) ,
1092- ) ;
1093- return ;
1094- }
1095- resetNoOutputTimer ( session ) ;
1096- }
1097- } ,
1067+ const mcpCaptureAttempt = await prepareCliBundleMcpCaptureAttempt ( {
1068+ mode : params . context . backendResolved . bundleMcpMode ,
1069+ backend : params . context . preparedBackend . backend ,
1070+ env : params . env ,
1071+ captureKey : params . mcpCaptureKey ,
10981072 } ) ;
1073+ let managedRun : ManagedRun ;
1074+ try {
1075+ managedRun = await params . supervisor . spawn ( {
1076+ sessionId : params . context . params . sessionId ,
1077+ backendId : params . context . backendResolved . id ,
1078+ scopeKey : `claude-live:${ params . key } ` ,
1079+ replaceExistingScope : true ,
1080+ mode : "child" ,
1081+ argv : params . argv ,
1082+ cwd : params . context . cwd ?? params . context . workspaceDir ,
1083+ env : mcpCaptureAttempt . env ?? params . env ,
1084+ stdinMode : "pipe-open" ,
1085+ captureOutput : false ,
1086+ onStdout : ( chunk ) => {
1087+ if ( session ) {
1088+ handleClaudeStdout ( session , chunk ) ;
1089+ }
1090+ } ,
1091+ onStderr : ( chunk ) => {
1092+ if ( session ) {
1093+ session . stderr += chunk ;
1094+ if ( session . stderr . length > CLAUDE_LIVE_MAX_STDERR_CHARS ) {
1095+ closeLiveSession (
1096+ session ,
1097+ "abort" ,
1098+ createOutputLimitError ( session , "Claude CLI stderr exceeded limit." ) ,
1099+ ) ;
1100+ return ;
1101+ }
1102+ resetNoOutputTimer ( session ) ;
1103+ }
1104+ } ,
1105+ } ) ;
1106+ } catch ( error ) {
1107+ await mcpCaptureAttempt . cleanup ?.( ) ;
1108+ throw error ;
1109+ }
10991110 session = {
11001111 key : params . key ,
11011112 fingerprint : params . fingerprint ,
@@ -1109,7 +1120,10 @@ async function createClaudeLiveSession(params: {
11091120 drainTimer : null ,
11101121 drainingAbortedTurn : false ,
11111122 idleTimer : null ,
1112- cleanup : params . cleanup ,
1123+ cleanup : async ( ) => {
1124+ await mcpCaptureAttempt . cleanup ?.( ) ;
1125+ await params . cleanup ( ) ;
1126+ } ,
11131127 cleanupPromise : null ,
11141128 closing : false ,
11151129 mcpCaptureKey : params . mcpCaptureKey ,
0 commit comments