@@ -7,6 +7,7 @@ const readline = require('readline');
77const { success, error, info, warn, highlight, dim, bold } = require ( '../util/output' ) ;
88const systemAutomation = require ( '../../main/system-automation' ) ;
99const preferences = require ( '../../main/preferences' ) ;
10+ const { recordChatContinuityTurn } = require ( '../../main/session-intent-state' ) ;
1011const {
1112 getLogLevel : getUiAutomationLogLevel ,
1213 resetLogSettings : resetUiAutomationLogSettings ,
@@ -264,6 +265,67 @@ function buildForcedObservationAnswerPrompt(userMessage) {
264265 ] . join ( ' ' ) ;
265266}
266267
268+ function inferContinuationVerificationStatus ( execResult ) {
269+ if ( ! execResult ) return 'unknown' ;
270+ if ( execResult . cancelled ) return 'cancelled' ;
271+ if ( execResult . success === false ) return 'failed' ;
272+ if ( execResult . postVerificationFailed ) return 'unverified' ;
273+ if ( execResult . postVerification ?. verified ) return 'verified' ;
274+ if ( execResult . focusVerification ?. verified ) return 'verified' ;
275+ if ( execResult . focusVerification ?. applicable && ! execResult . focusVerification ?. verified ) return 'unverified' ;
276+ return execResult . success ? 'not-applicable' : 'unknown' ;
277+ }
278+
279+ function inferNextRecommendedStep ( execResult ) {
280+ if ( ! execResult ) return 'Continue from the last committed subgoal using the current app state.' ;
281+ if ( execResult . cancelled ) return 'Ask whether to retry the interrupted step or choose a different path.' ;
282+ if ( execResult . success === false ) return 'Review the failed step and gather fresh evidence before continuing.' ;
283+ if ( execResult . postVerification ?. needsFollowUp ) return 'Continue with the detected follow-up flow for the current app state.' ;
284+ if ( execResult . screenshotCaptured ) return 'Continue from the latest visual evidence and current app state.' ;
285+ if ( inferContinuationVerificationStatus ( execResult ) === 'unverified' ) return 'Gather fresh evidence before claiming the requested state change is complete.' ;
286+ return 'Continue from the current subgoal using the latest execution results.' ;
287+ }
288+
289+ function recordContinuityFromExecution ( ai , actionData , execResult , details = { } ) {
290+ try {
291+ const latestVisual = typeof ai ?. getLatestVisualContext === 'function'
292+ ? ai . getLatestVisualContext ( )
293+ : null ;
294+ const captureMode = String ( latestVisual ?. scope || '' ) . trim ( ) || ( execResult ?. screenshotCaptured ? 'screen' : null ) ;
295+ const captureTrusted = captureMode ? ( captureMode === 'window' || captureMode === 'region' ) : null ;
296+ const targetWindowHandle = Number ( details . targetWindowHandle || execResult ?. focusVerification ?. expectedWindowHandle || 0 ) || null ;
297+ recordChatContinuityTurn ( {
298+ recordedAt : new Date ( ) . toISOString ( ) ,
299+ userMessage : details . userMessage || '' ,
300+ executionIntent : details . executionIntent || details . userMessage || '' ,
301+ activeGoal : details . executionIntent || details . userMessage || '' ,
302+ committedSubgoal : actionData ?. thought || details . executionIntent || details . userMessage || '' ,
303+ thought : actionData ?. thought || '' ,
304+ actionPlan : Array . isArray ( actionData ?. actions ) ? actionData . actions : [ ] ,
305+ success : ! ! execResult ?. success ,
306+ cancelled : ! ! execResult ?. cancelled ,
307+ postVerificationFailed : ! ! execResult ?. postVerificationFailed ,
308+ postVerification : execResult ?. postVerification || null ,
309+ focusVerification : execResult ?. focusVerification || null ,
310+ screenshotCaptured : ! ! execResult ?. screenshotCaptured ,
311+ executedCount : Array . isArray ( actionData ?. actions ) ? actionData . actions . length : 0 ,
312+ targetWindowHandle,
313+ windowTitle : latestVisual ?. windowTitle || null ,
314+ observationEvidence : {
315+ captureMode,
316+ captureTrusted,
317+ windowHandle : Number ( latestVisual ?. windowHandle || 0 ) || targetWindowHandle || null
318+ } ,
319+ verification : {
320+ status : inferContinuationVerificationStatus ( execResult )
321+ } ,
322+ nextRecommendedStep : inferNextRecommendedStep ( execResult )
323+ } , { cwd : process . cwd ( ) } ) ;
324+ } catch ( continuityError ) {
325+ warn ( `Could not record chat continuity state: ${ continuityError . message } ` ) ;
326+ }
327+ }
328+
267329function shouldAutoCaptureObservationAfterActions ( userMessage , actions , execResult ) {
268330 if ( ! isLikelyObservationInput ( userMessage ) ) return false ;
269331 if ( ! Array . isArray ( actions ) || actions . length === 0 ) return false ;
@@ -1136,6 +1198,14 @@ async function runChatLoop(ai, options) {
11361198 }
11371199 }
11381200
1201+ recordContinuityFromExecution ( ai , actionData , execResult , {
1202+ userMessage : line ,
1203+ executionIntent : effectiveUserMessage ,
1204+ targetWindowHandle : actionData ?. actions ?. find ( ( action ) => action ?. windowHandle || action ?. targetWindowHandle ) ?. windowHandle
1205+ || actionData ?. actions ?. find ( ( action ) => action ?. windowHandle || action ?. targetWindowHandle ) ?. targetWindowHandle
1206+ || null
1207+ } ) ;
1208+
11391209 // ===== VISION AUTO-CONTINUATION =====
11401210 // If the AI requested a screenshot during its action sequence AND we captured it,
11411211 // automatically send a follow-up message so the AI can analyze the capture and
@@ -1252,6 +1322,14 @@ async function runChatLoop(ai, options) {
12521322 break ;
12531323 }
12541324
1325+ recordContinuityFromExecution ( ai , contActionData , contExecResult , {
1326+ userMessage : line ,
1327+ executionIntent : effectiveUserMessage ,
1328+ targetWindowHandle : contActionData ?. actions ?. find ( ( action ) => action ?. windowHandle || action ?. targetWindowHandle ) ?. windowHandle
1329+ || contActionData ?. actions ?. find ( ( action ) => action ?. windowHandle || action ?. targetWindowHandle ) ?. targetWindowHandle
1330+ || null
1331+ } ) ;
1332+
12551333 // If the continuation itself requested another screenshot, loop again
12561334 if ( ! contExecResult ?. screenshotCaptured ) break ;
12571335 }
0 commit comments