@@ -111,13 +111,20 @@ describe("Workboard dispatcher ownership", () => {
111111 cards [ 0 ] ?. id ,
112112 cards [ 1 ] ?. id ,
113113 ] ) ;
114- await expect ( Promise . all ( cards . map ( ( card ) => store . get ( card . id ) ) ) ) . resolves . toMatchObject ( [
114+ const persisted = await Promise . all ( cards . map ( ( card ) => store . get ( card . id ) ) ) ;
115+ expect ( persisted ) . toMatchObject ( [
115116 { status : "blocked" } ,
116117 { status : "blocked" } ,
117118 { status : "ready" } ,
118119 { status : "ready" } ,
119120 { status : "ready" } ,
120121 ] ) ;
122+ for ( const rejected of persisted . slice ( 0 , 2 ) ) {
123+ expect ( rejected ) . not . toHaveProperty ( "metadata.claim" ) ;
124+ expect ( rejected ) . not . toHaveProperty ( "sessionKey" ) ;
125+ expect ( rejected ) . not . toHaveProperty ( "runId" ) ;
126+ expect ( rejected ) . not . toHaveProperty ( "execution" ) ;
127+ }
121128 } ) ;
122129
123130 it ( "does not spend worker attempts on cards that fail workspace preflight" , async ( ) => {
@@ -551,50 +558,234 @@ describe("Workboard dispatcher ownership", () => {
551558 expect ( persisted . filter ( ( card ) => card . status === "ready" ) ) . toHaveLength ( 24 ) ;
552559 } ) ;
553560
554- it ( "preserves an accepted worker claim when execution persistence fails" , async ( ) => {
561+ it . each ( [ "scheduled dispatch" , "dashboard exact-card start" ] as const ) (
562+ "persists launch association before %s and keeps accepted runs visible" ,
563+ async ( origin ) => {
564+ const store = new WorkboardStore ( createMemoryStore ( ) ) ;
565+ const card = await store . create ( {
566+ title : "Worker with unavailable execution persistence" ,
567+ status : "ready" ,
568+ workspaceAccess : { unrestricted : true } ,
569+ } ) ;
570+ vi . spyOn ( store , "enrichExecutionAssociation" ) . mockRejectedValue (
571+ new Error ( "execution enrichment unavailable" ) ,
572+ ) ;
573+ let provisionalRunId = "" ;
574+ const canonicalSessionKey = `agent:worker:subagent:workboard-default-${ card . id } ` ;
575+ const run = vi . fn ( ) . mockImplementation ( async ( input ) => {
576+ provisionalRunId = input . idempotencyKey ;
577+ const persisted = await store . get ( card . id ) ;
578+ expect ( persisted ) . toMatchObject ( {
579+ status : "running" ,
580+ sessionKey : input . sessionKey ,
581+ runId : provisionalRunId ,
582+ execution : {
583+ status : "running" ,
584+ sessionKey : input . sessionKey ,
585+ runId : provisionalRunId ,
586+ } ,
587+ } ) ;
588+ return { sessionKey : canonicalSessionKey , runId : "accepted-run" } ;
589+ } ) ;
590+
591+ const result = await dispatchAndStartWorkboardCards ( {
592+ store,
593+ subagent : { run } ,
594+ options : {
595+ maxStarts : 1 ,
596+ ...( origin === "dashboard exact-card start" ? { cardId : card . id } : { } ) ,
597+ } ,
598+ } ) ;
599+
600+ expect ( run ) . toHaveBeenCalledOnce ( ) ;
601+ expect ( result . started ) . toEqual ( [
602+ expect . objectContaining ( {
603+ cardId : card . id ,
604+ sessionKey : canonicalSessionKey ,
605+ runId : "accepted-run" ,
606+ ...( origin === "dashboard exact-card start"
607+ ? {
608+ card : expect . objectContaining ( {
609+ sessionKey : canonicalSessionKey ,
610+ runId : "accepted-run" ,
611+ } ) ,
612+ }
613+ : { } ) ,
614+ } ) ,
615+ ] ) ;
616+ expect ( result . startFailures ) . toEqual ( [ ] ) ;
617+ await expect ( store . get ( card . id ) ) . resolves . toMatchObject ( {
618+ status : "running" ,
619+ runId : provisionalRunId ,
620+ execution : { status : "running" , runId : provisionalRunId } ,
621+ metadata : {
622+ claim : { ownerId : "workboard-dispatcher" } ,
623+ workerLogs : [ expect . objectContaining ( { runId : "accepted-run" } ) ] ,
624+ } ,
625+ } ) ;
626+ await expect (
627+ store . heartbeat ( card . id , { ownerId : "workboard-dispatcher" } ) ,
628+ ) . resolves . toMatchObject ( {
629+ status : "running" ,
630+ metadata : { claim : { ownerId : "workboard-dispatcher" } } ,
631+ } ) ;
632+
633+ const retry = await dispatchAndStartWorkboardCards ( {
634+ store,
635+ subagent : { run } ,
636+ options : { maxStarts : 1 } ,
637+ } ) ;
638+
639+ expect ( retry . started ) . toEqual ( [ ] ) ;
640+ expect ( retry . startFailures ) . toEqual ( [ ] ) ;
641+ expect ( run ) . toHaveBeenCalledOnce ( ) ;
642+ } ,
643+ ) ;
644+
645+ it . each ( [ "backlog" , "todo" , "ready" ] as const ) (
646+ "starts an exact dashboard card from %s" ,
647+ async ( status ) => {
648+ const store = new WorkboardStore ( createMemoryStore ( ) ) ;
649+ const card = await store . create ( {
650+ title : `Exact ${ status } ` ,
651+ status,
652+ agentId : `worker-${ status } ` ,
653+ workspaceAccess : { unrestricted : true } ,
654+ } ) ;
655+ const run = vi . fn ( ) . mockResolvedValue ( { runId : `run-${ status } ` } ) ;
656+
657+ const result = await dispatchAndStartWorkboardCards ( {
658+ store,
659+ subagent : { run } ,
660+ options : { cardId : card . id , maxStarts : 1 } ,
661+ } ) ;
662+
663+ expect ( result . started ) . toEqual ( [ expect . objectContaining ( { cardId : card . id } ) ] ) ;
664+ expect ( result . startFailures ) . toEqual ( [ ] ) ;
665+ expect ( ( await store . get ( card . id ) ) ?. status ) . toBe ( "running" ) ;
666+ } ,
667+ ) ;
668+
669+ it . each ( [ "review" , "blocked" , "done" ] as const ) (
670+ "rejects an exact dashboard card in %s" ,
671+ async ( status ) => {
672+ const store = new WorkboardStore ( createMemoryStore ( ) ) ;
673+ const card = await store . create ( {
674+ title : `Invalid exact ${ status } ` ,
675+ status,
676+ agentId : `worker-${ status } ` ,
677+ workspaceAccess : { unrestricted : true } ,
678+ } ) ;
679+ const run = vi . fn ( ) ;
680+
681+ const result = await dispatchAndStartWorkboardCards ( {
682+ store,
683+ subagent : { run } ,
684+ options : { cardId : card . id , maxStarts : 1 } ,
685+ } ) ;
686+
687+ expect ( run ) . not . toHaveBeenCalled ( ) ;
688+ expect ( result . started ) . toEqual ( [ ] ) ;
689+ expect ( result . startFailures ) . toEqual ( [
690+ expect . objectContaining ( {
691+ cardId : card . id ,
692+ error : expect . stringContaining ( status ) ,
693+ } ) ,
694+ ] ) ;
695+ await expect ( store . get ( card . id ) ) . resolves . toMatchObject ( {
696+ status,
697+ metadata : expect . not . objectContaining ( { claim : expect . anything ( ) } ) ,
698+ } ) ;
699+ } ,
700+ ) ;
701+
702+ it . each ( [
703+ { label : "due" , offsetMs : - 1_000 , starts : true } ,
704+ { label : "future" , offsetMs : 60_000 , starts : false } ,
705+ ] ) ( "handles a $label scheduled exact dashboard card" , async ( { offsetMs, starts } ) => {
555706 const store = new WorkboardStore ( createMemoryStore ( ) ) ;
707+ const now = Date . now ( ) ;
556708 const card = await store . create ( {
557- title : "Worker with unavailable execution persistence" ,
709+ title : "Scheduled exact" ,
710+ status : "scheduled" ,
711+ scheduledAt : now + offsetMs ,
712+ agentId : "scheduled-worker" ,
713+ workspaceAccess : { unrestricted : true } ,
714+ } ) ;
715+ const run = vi . fn ( ) . mockResolvedValue ( { runId : "scheduled-run" } ) ;
716+
717+ const result = await dispatchAndStartWorkboardCards ( {
718+ store,
719+ subagent : { run } ,
720+ options : { cardId : card . id , maxStarts : 1 , now } ,
721+ } ) ;
722+
723+ expect ( run ) . toHaveBeenCalledTimes ( starts ? 1 : 0 ) ;
724+ expect ( result . started ) . toHaveLength ( starts ? 1 : 0 ) ;
725+ expect ( result . startFailures ) . toHaveLength ( starts ? 0 : 1 ) ;
726+ await expect ( store . get ( card . id ) ) . resolves . toMatchObject ( {
727+ status : starts ? "running" : "scheduled" ,
728+ } ) ;
729+ } ) ;
730+
731+ it ( "keeps global owner capacity for exact dashboard starts across boards" , async ( ) => {
732+ const store = new WorkboardStore ( createMemoryStore ( ) ) ;
733+ const active = await store . create ( {
734+ title : "Active owner" ,
558735 status : "ready" ,
736+ boardId : "ops" ,
737+ agentId : "shared-owner" ,
559738 workspaceAccess : { unrestricted : true } ,
560739 } ) ;
561- const run = vi . fn ( ) . mockResolvedValue ( { runId : "run-awaiting-persistence" } ) ;
562- vi . spyOn ( store , "update" ) . mockRejectedValueOnce ( new Error ( "execution persistence unavailable" ) ) ;
740+ await store . claim ( active . id , { ownerId : "shared-owner" } ) ;
741+ const target = await store . create ( {
742+ title : "Blocked exact owner" ,
743+ status : "todo" ,
744+ boardId : "product" ,
745+ agentId : "shared-owner" ,
746+ workspaceAccess : { unrestricted : true } ,
747+ } ) ;
748+ const run = vi . fn ( ) ;
563749
564750 const result = await dispatchAndStartWorkboardCards ( {
565751 store,
566752 subagent : { run } ,
567- options : { maxStarts : 1 } ,
753+ options : { cardId : target . id , maxStarts : 1 } ,
568754 } ) ;
569755
570- expect ( run ) . toHaveBeenCalledOnce ( ) ;
571- expect ( result . started ) . toEqual ( [ ] ) ;
756+ expect ( run ) . not . toHaveBeenCalled ( ) ;
572757 expect ( result . startFailures ) . toEqual ( [
573- expect . objectContaining ( {
574- cardId : card . id ,
575- error : "execution persistence unavailable" ,
576- } ) ,
758+ expect . objectContaining ( { error : expect . stringContaining ( "shared-owner" ) } ) ,
577759 ] ) ;
578- await expect ( store . get ( card . id ) ) . resolves . toMatchObject ( {
579- status : "running" ,
580- metadata : { claim : { ownerId : "workboard-dispatcher" } } ,
760+ await expect ( store . get ( target . id ) ) . resolves . toMatchObject ( { status : "todo" } ) ;
761+ } ) ;
762+
763+ it ( "starts the exact lower-priority card without selecting a sibling" , async ( ) => {
764+ const store = new WorkboardStore ( createMemoryStore ( ) ) ;
765+ const urgent = await store . create ( {
766+ title : "Urgent sibling" ,
767+ status : "ready" ,
768+ priority : "urgent" ,
769+ agentId : "urgent-owner" ,
770+ workspaceAccess : { unrestricted : true } ,
581771 } ) ;
582- await expect (
583- store . heartbeat ( card . id , { ownerId : "workboard-dispatcher" } ) ,
584- ) . resolves . toMatchObject ( {
585- status : "running" ,
586- metadata : { claim : { ownerId : "workboard-dispatcher" } } ,
772+ const target = await store . create ( {
773+ title : "Exact low priority" ,
774+ status : "todo" ,
775+ priority : "low" ,
776+ agentId : "target-owner" ,
777+ workspaceAccess : { unrestricted : true } ,
587778 } ) ;
779+ const run = vi . fn ( ) . mockResolvedValue ( { runId : "target-run" } ) ;
588780
589- const retry = await dispatchAndStartWorkboardCards ( {
781+ const result = await dispatchAndStartWorkboardCards ( {
590782 store,
591783 subagent : { run } ,
592- options : { maxStarts : 1 } ,
784+ options : { cardId : target . id , maxStarts : 1 } ,
593785 } ) ;
594786
595- expect ( retry . started ) . toEqual ( [ ] ) ;
596- expect ( retry . startFailures ) . toEqual ( [ ] ) ;
597- expect ( run ) . toHaveBeenCalledOnce ( ) ;
787+ expect ( result . started ) . toEqual ( [ expect . objectContaining ( { cardId : target . id } ) ] ) ;
788+ await expect ( store . get ( urgent . id ) ) . resolves . toMatchObject ( { status : "ready" } ) ;
598789 } ) ;
599790
600791 it ( "keeps an accepted worker running when its worker log cannot be recorded" , async ( ) => {
0 commit comments