@@ -3,6 +3,7 @@ import { managedWorktrees } from "../../agents/worktrees/service.js";
33import {
44 abandonTaskSuggestionAcceptance ,
55 beginTaskSuggestionAcceptance ,
6+ completeTaskSuggestionAcceptance ,
67} from "../task-suggestion-registry.js" ;
78import { sessionsHandlers } from "./sessions.js" ;
89import { taskSuggestionsHandlers } from "./task-suggestions.js" ;
@@ -92,7 +93,7 @@ describe("task suggestion gateway methods", () => {
9293 expect ( empty . response ?. [ 1 ] ) . toEqual ( { suggestions : [ ] } ) ;
9394 } ) ;
9495
95- it ( "accepts a suggestion once and replays the created session key " , async ( ) => {
96+ it ( "preserves accepted-session replay when successful admission expires a pending suggestion " , async ( ) => {
9697 const created = await call ( "taskSuggestions.create" , {
9798 title : "Remove stale adapter" ,
9899 prompt : "Delete src/example.ts and update its tests." ,
@@ -120,6 +121,38 @@ describe("task suggestion gateway methods", () => {
120121 } ) ;
121122
122123 const first = await call ( "taskSuggestions.accept" , { taskId } ) ;
124+ for ( let index = 0 ; index < 100 ; index += 1 ) {
125+ requirePayload (
126+ await call ( "taskSuggestions.create" , {
127+ title : `Pending follow up ${ index } ` ,
128+ prompt : `Complete pending follow-up task ${ index } .` ,
129+ tldr : "The operator has not accepted this follow-up." ,
130+ cwd : "/repo" ,
131+ sessionKey : "agent:main:main" ,
132+ } ) ,
133+ ) ;
134+ }
135+
136+ const previous = await call ( "taskSuggestions.list" , { } ) ;
137+ const pending = ( requirePayload ( previous ) as { suggestions : Array < { id : string } > } )
138+ . suggestions ;
139+ const oldestPending = pending . at ( - 1 ) ;
140+ expect ( oldestPending ) . toBeDefined ( ) ;
141+ const admitted = await call ( "taskSuggestions.create" , {
142+ title : "Latest follow up" ,
143+ prompt : "Preserve the accepted task while admitting new work." ,
144+ tldr : "Evict only the oldest pending follow-up." ,
145+ cwd : "/repo" ,
146+ sessionKey : "agent:main:main" ,
147+ } ) ;
148+ expect ( admitted . response ?. [ 0 ] ) . toBe ( true ) ;
149+ expect ( admitted . broadcast ) . toHaveBeenNthCalledWith (
150+ 1 ,
151+ "task.suggestion" ,
152+ { action : "resolved" , taskId : oldestPending ?. id , resolution : "expired" } ,
153+ { dropIfSlow : true } ,
154+ ) ;
155+
123156 const retry = await call ( "taskSuggestions.accept" , { taskId } ) ;
124157
125158 expect ( first . response ?. [ 1 ] ) . toEqual ( { taskId, key : sessionKey } ) ;
@@ -132,6 +165,49 @@ describe("task suggestion gateway methods", () => {
132165 ) ;
133166 } ) ;
134167
168+ it ( "admits new work when every bounded registry entry has already been accepted" , async ( ) => {
169+ const acceptedTaskIds : string [ ] = [ ] ;
170+ const createSession = vi
171+ . spyOn ( sessionsHandlers , "sessions.create" )
172+ . mockImplementation ( async ( { params, respond } ) => {
173+ respond ( true , { key : ( params as { key : string } ) . key , runStarted : true } , undefined ) ;
174+ } ) ;
175+
176+ for ( let index = 0 ; index < 100 ; index += 1 ) {
177+ const created = await call ( "taskSuggestions.create" , {
178+ title : `Accepted follow up ${ index } ` ,
179+ prompt : `Complete accepted follow-up task ${ index } .` ,
180+ tldr : "This follow-up already created its managed task session." ,
181+ cwd : "/repo" ,
182+ sessionKey : "agent:main:main" ,
183+ } ) ;
184+ const taskId = ( requirePayload ( created ) as { taskId : string } ) . taskId ;
185+ const accepted = await call ( "taskSuggestions.accept" , { taskId } ) ;
186+ expect ( accepted . response ?. [ 1 ] ) . toMatchObject ( { taskId } ) ;
187+ acceptedTaskIds . push ( taskId ) ;
188+ }
189+
190+ const replacement = await call ( "taskSuggestions.create" , {
191+ title : "Latest follow up" ,
192+ prompt : "Keep accepting new suggestions after earlier tasks completed." ,
193+ tldr : "Accepted-session replay is bounded best-effort state." ,
194+ cwd : "/repo" ,
195+ sessionKey : "agent:main:main" ,
196+ } ) ;
197+
198+ expect ( replacement . response ?. [ 0 ] ) . toBe ( true ) ;
199+ expect ( replacement . broadcast ) . toHaveBeenCalledTimes ( 1 ) ;
200+ expect ( replacement . broadcast ) . toHaveBeenCalledWith (
201+ "task.suggestion" ,
202+ expect . objectContaining ( { action : "created" } ) ,
203+ { dropIfSlow : true } ,
204+ ) ;
205+ const oldestRetry = await call ( "taskSuggestions.accept" , { taskId : acceptedTaskIds [ 0 ] } ) ;
206+ expect ( oldestRetry . response ?. [ 0 ] ) . toBe ( false ) ;
207+ expect ( oldestRetry . response ?. [ 2 ] ) . toMatchObject ( { code : "INVALID_REQUEST" } ) ;
208+ expect ( createSession ) . toHaveBeenCalledTimes ( 100 ) ;
209+ } ) ;
210+
135211 it ( "coalesces concurrent acceptance requests" , async ( ) => {
136212 const created = await call ( "taskSuggestions.create" , {
137213 title : "Add coverage" ,
@@ -348,7 +424,6 @@ describe("task suggestion gateway methods", () => {
348424 } ) ;
349425
350426 it ( "broadcasts when the bounded registry expires a pending suggestion" , async ( ) => {
351- const taskIds : string [ ] = [ ] ;
352427 for ( let index = 0 ; index < 100 ; index += 1 ) {
353428 const created = await call ( "taskSuggestions.create" , {
354429 title : `Follow up ${ index } ` ,
@@ -357,9 +432,14 @@ describe("task suggestion gateway methods", () => {
357432 cwd : "/repo" ,
358433 sessionKey : "agent:main:main" ,
359434 } ) ;
360- taskIds . push ( ( requirePayload ( created ) as { taskId : string } ) . taskId ) ;
435+ requirePayload ( created ) ;
361436 }
362437
438+ const previous = await call ( "taskSuggestions.list" , { } ) ;
439+ const pending = ( requirePayload ( previous ) as { suggestions : Array < { id : string } > } )
440+ . suggestions ;
441+ const oldestPending = pending . at ( - 1 ) ;
442+ expect ( oldestPending ) . toBeDefined ( ) ;
363443 const replacement = await call ( "taskSuggestions.create" , {
364444 title : "Latest follow up" ,
365445 prompt : "Complete the latest follow-up task." ,
@@ -372,11 +452,80 @@ describe("task suggestion gateway methods", () => {
372452 expect ( replacement . broadcast ) . toHaveBeenNthCalledWith (
373453 1 ,
374454 "task.suggestion" ,
375- { action : "resolved" , taskId : taskIds [ 0 ] , resolution : "expired" } ,
455+ { action : "resolved" , taskId : oldestPending ?. id , resolution : "expired" } ,
376456 { dropIfSlow : true } ,
377457 ) ;
378458 const listed = await call ( "taskSuggestions.list" , { } ) ;
379- expect ( ( requirePayload ( listed ) as { suggestions : unknown [ ] } ) . suggestions ) . toHaveLength ( 100 ) ;
459+ expect ( ( requirePayload ( listed ) as { suggestions : unknown [ ] } ) . suggestions ) . toHaveLength (
460+ pending . length ,
461+ ) ;
462+ } ) ;
463+
464+ it ( "rejects impossible byte admission without evicting accepted or pending suggestions" , async ( ) => {
465+ const acceptingTaskIds : string [ ] = [ ] ;
466+ const largePrompt = "🦀" . repeat ( 16_000 ) ;
467+
468+ try {
469+ for ( let index = 0 ; index < 32 ; index += 1 ) {
470+ const created = await call ( "taskSuggestions.create" , {
471+ title : `Running follow up ${ index } ` ,
472+ prompt : largePrompt ,
473+ tldr : "This accepted task is still starting." ,
474+ cwd : "/repo" ,
475+ sessionKey : "agent:main:main" ,
476+ } ) ;
477+ const taskId = ( requirePayload ( created ) as { taskId : string } ) . taskId ;
478+ expect ( beginTaskSuggestionAcceptance ( taskId ) . status ) . toBe ( "claimed" ) ;
479+ acceptingTaskIds . push ( taskId ) ;
480+ }
481+
482+ const accepted = await call ( "taskSuggestions.create" , {
483+ title : "Preserve accepted task" ,
484+ prompt : "Keep its accepted result available for retries." ,
485+ tldr : "A rejected admission must not discard completed results." ,
486+ cwd : "/repo" ,
487+ sessionKey : "agent:main:main" ,
488+ } ) ;
489+ const acceptedTaskId = ( requirePayload ( accepted ) as { taskId : string } ) . taskId ;
490+ expect ( beginTaskSuggestionAcceptance ( acceptedTaskId ) . status ) . toBe ( "claimed" ) ;
491+ completeTaskSuggestionAcceptance ( acceptedTaskId , "agent:main:dashboard:accepted" ) ;
492+
493+ const pending = await call ( "taskSuggestions.create" , {
494+ title : "Keep this follow up" ,
495+ prompt : "Do not discard this pending task." ,
496+ tldr : "The operator has not accepted it yet." ,
497+ cwd : "/repo" ,
498+ sessionKey : "agent:main:main" ,
499+ } ) ;
500+ const pendingTaskId = ( requirePayload ( pending ) as { taskId : string } ) . taskId ;
501+ const rejected = await call ( "taskSuggestions.create" , {
502+ title : "One oversized follow up" ,
503+ prompt : largePrompt ,
504+ tldr : "This valid task cannot fit beside protected tasks." ,
505+ cwd : "/repo" ,
506+ sessionKey : "agent:main:main" ,
507+ } ) ;
508+
509+ expect ( rejected . response ?. [ 0 ] ) . toBe ( false ) ;
510+ expect ( rejected . response ?. [ 2 ] ) . toMatchObject ( {
511+ code : "UNAVAILABLE" ,
512+ message : "task suggestion registry is busy" ,
513+ retryable : true ,
514+ } ) ;
515+ expect ( rejected . broadcast ) . not . toHaveBeenCalled ( ) ;
516+ expect ( beginTaskSuggestionAcceptance ( acceptedTaskId ) ) . toEqual ( {
517+ status : "accepted" ,
518+ sessionKey : "agent:main:dashboard:accepted" ,
519+ } ) ;
520+ const listed = await call ( "taskSuggestions.list" , { } ) ;
521+ expect (
522+ ( requirePayload ( listed ) as { suggestions : Array < { id : string } > } ) . suggestions ,
523+ ) . toEqual ( [ expect . objectContaining ( { id : pendingTaskId } ) ] ) ;
524+ } finally {
525+ for ( const taskId of acceptingTaskIds ) {
526+ expect ( abandonTaskSuggestionAcceptance ( taskId ) ) . toBe ( true ) ;
527+ }
528+ }
380529 } ) ;
381530
382531 it ( "rejects a new suggestion when every bounded registry entry is accepting" , async ( ) => {
0 commit comments