@@ -171,18 +171,19 @@ function recoveryDetailsForClaimFailure(
171171 store : MemoryStore ,
172172 code :
173173 | 'PLAN_SUBTASK_NOT_FOUND'
174+ | 'PLAN_SUBTASK_STALE'
174175 | 'PLAN_SUBTASK_DEPS_UNMET'
175176 | 'PLAN_SUBTASK_NOT_AVAILABLE'
176177 | 'PLAN_ARCHIVED'
177178 | 'CLAIM_TAKEOVER_RECOMMENDED'
178179 | 'CLAIM_HELD_BY_ACTIVE_OWNER' ,
179180 args : { plan_slug : string ; subtask_index : number ; repo_root ?: string } ,
180181) : Record < string , unknown > {
181- if ( code !== 'PLAN_SUBTASK_NOT_AVAILABLE' ) return { } ;
182+ if ( code !== 'PLAN_SUBTASK_NOT_AVAILABLE' && code !== 'PLAN_SUBTASK_STALE' ) return { } ;
182183 try {
183184 const plan = listPlans ( store , {
184185 ...( args . repo_root !== undefined ? { repo_root : args . repo_root } : { } ) ,
185- limit : 200 ,
186+ limit : 2000 ,
186187 } ) . find ( ( candidate ) => candidate . plan_slug === args . plan_slug ) ;
187188 if ( ! plan ) return { } ;
188189 const candidates = plan . next_available
@@ -421,18 +422,20 @@ export function register(server: McpServer, ctx: ToolContext): void {
421422 // bad to good without requiring an operator to call colony plan
422423 // close manually.
423424 sweepCompletedPlansForAutoArchive ( store , args . repo_root ) ;
425+ const limit = args . limit ?? 10 ;
424426 const plans = listPlans ( store , {
425427 ...( args . repo_root !== undefined ? { repo_root : args . repo_root } : { } ) ,
426428 ...( args . only_with_available_subtasks !== undefined
427429 ? { only_with_available_subtasks : args . only_with_available_subtasks }
428430 : { } ) ,
429431 ...( args . capability_match !== undefined ? { capability_match : args . capability_match } : { } ) ,
430- ... ( args . limit !== undefined ? { limit : args . limit } : { } ) ,
432+ limit : 2000 ,
431433 } ) ;
432434 const includeUnpublished = args . include_unpublished ?? true ;
433435 const merged = includeUnpublished ? mergeUnpublishedDiskPlans ( plans , args . repo_root ) : plans ;
434436 const detail = args . detail ?? 'compact' ;
435- const payload = detail === 'full' ? merged : merged . map ( toCompactPlan ) ;
437+ const limited = merged . slice ( 0 , limit ) ;
438+ const payload = detail === 'full' ? limited : limited . map ( toCompactPlan ) ;
436439 return { content : [ { type : 'text' , text : JSON . stringify ( payload ) } ] } ;
437440 } ) ,
438441 ) ;
@@ -648,6 +651,7 @@ export type ClaimPlanSubtaskArgs = {
648651 subtask_index : number ;
649652 session_id : string ;
650653 agent : string ;
654+ repo_root ?: string | undefined ;
651655} ;
652656
653657export type ClaimPlanSubtaskResult =
@@ -657,6 +661,7 @@ export type ClaimPlanSubtaskResult =
657661 ok : false ;
658662 code :
659663 | 'PLAN_SUBTASK_NOT_FOUND'
664+ | 'PLAN_SUBTASK_STALE'
660665 | 'PLAN_SUBTASK_DEPS_UNMET'
661666 | 'PLAN_SUBTASK_NOT_AVAILABLE'
662667 | 'CLAIM_TAKEOVER_RECOMMENDED'
@@ -686,7 +691,22 @@ export function attemptClaimPlanSubtask(
686691 session_id : args . session_id ,
687692 } ) ;
688693 if ( archived ) return { ok : false , ...archived } ;
689- return { ok : false , code : 'PLAN_SUBTASK_NOT_FOUND' , message : `no sub-task at ${ branch } ` } ;
694+ const planExists = listPlans ( store , {
695+ ...( args . repo_root !== undefined ? { repo_root : args . repo_root } : { } ) ,
696+ limit : 2000 ,
697+ } ) . some ( ( candidate ) => candidate . plan_slug === args . plan_slug ) ;
698+ if ( planExists ) {
699+ return {
700+ ok : false ,
701+ code : 'PLAN_SUBTASK_STALE' ,
702+ message : `stale sub-task pointer: no sub-task at ${ branch } ` ,
703+ } ;
704+ }
705+ return {
706+ ok : false ,
707+ code : 'PLAN_SUBTASK_NOT_FOUND' ,
708+ message : `unknown plan or sub-task: no sub-task at ${ branch } ` ,
709+ } ;
690710 }
691711
692712 const allTasks = store . storage . listTasks ( 2000 ) ;
@@ -720,7 +740,7 @@ export function attemptClaimPlanSubtask(
720740 const fresh = readSubtaskByBranch ( store , branch ) ;
721741 if ( ! fresh ) {
722742 const err : CodedError = new Error ( `no sub-task at ${ branch } ` ) ;
723- err . __code = 'PLAN_SUBTASK_NOT_AVAILABLE ' ;
743+ err . __code = 'PLAN_SUBTASK_STALE ' ;
724744 throw err ;
725745 }
726746 if ( fresh . info . status !== 'available' ) {
@@ -802,6 +822,7 @@ export function attemptClaimPlanSubtask(
802822 const code = ( err as CodedError ) . __code ;
803823 if (
804824 code === 'PLAN_SUBTASK_NOT_AVAILABLE' ||
825+ code === 'PLAN_SUBTASK_STALE' ||
805826 code === 'CLAIM_TAKEOVER_RECOMMENDED' ||
806827 code === 'CLAIM_HELD_BY_ACTIVE_OWNER'
807828 ) {
0 commit comments