@@ -235,6 +235,78 @@ describe("API#getTaskApiConversationHistoryLength", () => {
235235 await expect ( api . hasTaskApiConversationHistorySequence ( "task-1" , expectedSequence ) ) . resolves . toBe ( true )
236236 } )
237237
238+ it ( "accepts attempt_completion after a persisted tool_result user message in the same generation" , async ( ) => {
239+ mockGetTaskWithId . mockResolvedValue ( {
240+ apiConversationHistory : [
241+ { role : "user" , content : [ { type : "text" , text : "RESTART_PERSISTENCE_SMOKE" } ] } ,
242+ {
243+ role : "assistant" ,
244+ content : [ { type : "tool_use" , id : "read-1" , name : "read_file" , input : { path : "f.ts" } } ] ,
245+ } ,
246+ // tool_result is role:"user" — must not terminate the generation search
247+ { role : "user" , content : [ { type : "tool_result" , tool_use_id : "read-1" , content : "file contents" } ] } ,
248+ {
249+ role : "assistant" ,
250+ content : [
251+ { type : "tool_use" , id : "completion" , name : "attempt_completion" , input : { result : "done" } } ,
252+ ] ,
253+ } ,
254+ ] ,
255+ } )
256+
257+ await expect ( api . hasTaskApiConversationHistorySequence ( "task-1" , expectedSequence ) ) . resolves . toBe ( true )
258+ } )
259+
260+ it ( "rejects attempt_completion that follows a later genuine user text turn" , async ( ) => {
261+ mockGetTaskWithId . mockResolvedValue ( {
262+ apiConversationHistory : [
263+ { role : "user" , content : [ { type : "text" , text : "RESTART_PERSISTENCE_SMOKE" } ] } ,
264+ {
265+ role : "assistant" ,
266+ content : [ { type : "tool_use" , id : "read-1" , name : "read_file" , input : { path : "f.ts" } } ] ,
267+ } ,
268+ // A genuine user text turn ends the generation — completion below is in the next generation
269+ { role : "user" , content : [ { type : "text" , text : "follow-up question" } ] } ,
270+ {
271+ role : "assistant" ,
272+ content : [
273+ { type : "tool_use" , id : "completion" , name : "attempt_completion" , input : { result : "done" } } ,
274+ ] ,
275+ } ,
276+ ] ,
277+ } )
278+
279+ await expect ( api . hasTaskApiConversationHistorySequence ( "task-1" , expectedSequence ) ) . resolves . toBe ( false )
280+ } )
281+
282+ it ( "treats a user turn with mixed text and tool_result content as a genuine boundary" , async ( ) => {
283+ mockGetTaskWithId . mockResolvedValue ( {
284+ apiConversationHistory : [
285+ { role : "user" , content : [ { type : "text" , text : "RESTART_PERSISTENCE_SMOKE" } ] } ,
286+ {
287+ role : "assistant" ,
288+ content : [ { type : "tool_use" , id : "read-1" , name : "read_file" , input : { path : "f.ts" } } ] ,
289+ } ,
290+ // Mixed content: has both a tool_result and a text block — counts as a genuine user turn
291+ {
292+ role : "user" ,
293+ content : [
294+ { type : "tool_result" , tool_use_id : "read-1" , content : "file contents" } ,
295+ { type : "text" , text : "what do you think?" } ,
296+ ] ,
297+ } ,
298+ {
299+ role : "assistant" ,
300+ content : [
301+ { type : "tool_use" , id : "completion" , name : "attempt_completion" , input : { result : "done" } } ,
302+ ] ,
303+ } ,
304+ ] ,
305+ } )
306+
307+ await expect ( api . hasTaskApiConversationHistorySequence ( "task-1" , expectedSequence ) ) . resolves . toBe ( false )
308+ } )
309+
238310 it ( "rejects an assistant completion that does not follow the expected user turn" , async ( ) => {
239311 mockGetTaskWithId . mockResolvedValue ( {
240312 apiConversationHistory : [
0 commit comments