@@ -3,6 +3,7 @@ import path from "node:path";
33import type {
44 WorkboardCard ,
55 WorkboardExecution ,
6+ WorkboardLaunchState ,
67 WorkboardWorkspace ,
78} from "@openclaw/workboard-contract" ;
89import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime" ;
@@ -71,6 +72,8 @@ type WorkboardDispatchAndStartResult = WorkboardDispatchResult & {
7172 startFailures : WorkboardStartFailure [ ] ;
7273} ;
7374
75+ type WorkboardPreparedLaunch = Extract < WorkboardLaunchState , { phase : "prepared" } > ;
76+
7477type WorkboardDispatchStartParams = {
7578 store : WorkboardStore ;
7679 subagent : WorkboardSubagentRuntime ;
@@ -364,8 +367,8 @@ async function runWorkboardDispatch(
364367 let materializedWorkspace : WorkboardWorkspace | undefined ;
365368 let implicitWorkspaceCwd : string | undefined ;
366369 let runStarted = false ;
367- let launchIntentPersisted = false ;
368370 let workspaceMutation : { before : WorkboardCard ; after : WorkboardCard } | undefined ;
371+ let preparedLaunch : WorkboardPreparedLaunch | undefined ;
369372 const requestedWorkspace = card . metadata ?. automation ?. workspace ;
370373 let workspaceAccess : WorkboardWorkspaceAccess ;
371374 let targetWorkspace : string | undefined ;
@@ -502,28 +505,14 @@ async function runWorkboardDispatch(
502505 ) ;
503506 workspaceMutation = { before : workspaceBase , after : materializedCard } ;
504507 }
505- const launchBase = await params . store . get ( card . id ) ;
506- if ( ! launchBase ) {
507- throw new Error ( `card not found: ${ card . id } ` ) ;
508- }
509- const runId = `workboard:${ card . id } :${ launchBase . updatedAt } ` ;
510- const launched = await params . store . update (
511- card . id ,
512- {
513- sessionKey,
514- runId,
515- execution : buildExecution ( {
516- card : launchBase ,
517- sessionKey,
518- runId,
519- runtime : undefined ,
520- now,
521- } ) ,
522- ...( materializedWorkspace ? { workspace : materializedWorkspace } : { } ) ,
523- } ,
524- { expectedUpdatedAt : launchBase . updatedAt } ,
525- ) ;
526- launchIntentPersisted = true ;
508+ const prepared = await params . store . prepareExecutionLaunch ( card . id , {
509+ requestedSessionKey : sessionKey ,
510+ now,
511+ scope : { ownerId, token : claimValue } ,
512+ } ) ;
513+ const launched = prepared . card ;
514+ preparedLaunch = prepared . launch ;
515+ const runId = prepared . launch . provisionalRunId ;
527516 const run = await params . subagent . run ( {
528517 sessionKey,
529518 message : buildWorkerPrompt ( {
@@ -556,15 +545,18 @@ async function runWorkboardDispatch(
556545 runId : run . runId ,
557546 execution : acceptedExecution ,
558547 } ;
559- const updated = await params . store
560- . enrichExecutionAssociation ( card . id , {
561- expectedSessionKey : sessionKey ,
562- expectedRunId : runId ,
563- sessionKey : acceptedSessionKey ,
564- runId : run . runId ,
565- execution : acceptedExecution ,
566- } )
567- . catch ( ( ) => acceptedCard ) ;
548+ const updated =
549+ ( await params . store
550+ . acceptExecutionLaunch ( card . id , {
551+ expectedLaunch : prepared . launch ,
552+ acceptedAt : Math . max ( Date . now ( ) , prepared . launch . preparedAt ) ,
553+ expectedSessionKey : sessionKey ,
554+ expectedRunId : runId ,
555+ sessionKey : acceptedSessionKey ,
556+ runId : run . runId ,
557+ execution : acceptedExecution ,
558+ } )
559+ . catch ( ( ) => undefined ) ) ?? acceptedCard ;
568560 acceptedStarts += 1 ;
569561 startedOwners . add ( ownerId ) ;
570562 started . push ( {
@@ -614,16 +606,20 @@ async function runWorkboardDispatch(
614606 continue ;
615607 }
616608 try {
617- await params . store . block (
618- card . id ,
619- {
620- ownerId,
621- token : claimValue ,
622- reason : `Dispatcher could not start worker: ${ message } ` ,
623- } ,
624- { ownerId, token : claimValue } ,
625- { clearExecutionAssociation : launchIntentPersisted } ,
626- ) ;
609+ const reason = `Dispatcher could not start worker: ${ message } ` ;
610+ if ( preparedLaunch ) {
611+ await params . store . failPreparedLaunch ( card . id , {
612+ expectedLaunch : preparedLaunch ,
613+ reason,
614+ failedAt : Date . now ( ) ,
615+ } ) ;
616+ } else {
617+ await params . store . block (
618+ card . id ,
619+ { ownerId, token : claimValue , reason } ,
620+ { ownerId, token : claimValue } ,
621+ ) ;
622+ }
627623 } catch {
628624 // Leave the original start failure visible; dispatch will diagnose stale claims later.
629625 }
0 commit comments