@@ -404,6 +404,57 @@ func TestRunDaemonRemotePollInjectsRemoteTriggers(t *testing.T) {
404404 }
405405}
406406
407+ func TestRunDaemonRemotePollRetriesTransientPoll (t * testing.T ) {
408+ dir := t .TempDir ()
409+ transcriptPath := filepath .Join (dir , "session.jsonl" )
410+ sessionID := contracts .ID ("sess_daemon_remote_poll_retry" )
411+ calls := 0
412+ var auths []string
413+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
414+ calls ++
415+ auths = append (auths , r .Header .Get ("Authorization" ))
416+ if calls == 1 {
417+ w .Header ().Set ("Retry-After" , "0" )
418+ http .Error (w , "try again" , http .StatusServiceUnavailable )
419+ return
420+ }
421+ w .Header ().Set ("content-type" , "application/json" )
422+ _ , _ = w .Write ([]byte (`{"next_cursor":"cursor-retry","events":[]}` ))
423+ }))
424+ defer server .Close ()
425+ if err := remotepkg .WriteRegistrationState (remotepkg .SessionRegistrationPath (transcriptPath , sessionID ), remotepkg.RegistrationState {
426+ SessionID : sessionID ,
427+ RuntimeState : remotepkg .RegistrationRegistered ,
428+ PollURL : server .URL + "/poll" ,
429+ RemoteSessionID : "remote-session" ,
430+ }); err != nil {
431+ t .Fatal (err )
432+ }
433+ runner := conversation.Runner {
434+ SessionID : sessionID ,
435+ SessionPath : transcriptPath ,
436+ WorkingDirectory : dir ,
437+ MCP : & conversation.MCPConfig {UserSettings : contracts.Settings {
438+ Remote : & contracts.RemoteSetting {AuthToken : "poll-token" },
439+ }},
440+ }
441+
442+ result := runDaemonRemotePoll (context .Background (), runner , time .Unix (200 , 0 ).UTC ())
443+ if result .StructuredContent ["runtime_state" ] != remotepkg .PumpRunning || result .StructuredContent ["status_code" ] != http .StatusOK || result .StructuredContent ["attempt_count" ] != 2 || result .StructuredContent ["last_cursor" ] != "cursor-retry" || result .StructuredContent ["event_count" ] != 0 || result .StructuredContent ["error_count" ] != 0 {
444+ t .Fatalf ("retry poll = %#v" , result .StructuredContent )
445+ }
446+ if calls != 2 || len (auths ) != 2 || auths [0 ] != "Bearer poll-token" || auths [1 ] != "Bearer poll-token" {
447+ t .Fatalf ("calls/auths = %d %#v" , calls , auths )
448+ }
449+ pump , err := remotepkg .LoadPumpState (remotepkg .SessionPumpPath (transcriptPath , sessionID ))
450+ if err != nil {
451+ t .Fatal (err )
452+ }
453+ if pump .StatusCode != http .StatusOK || pump .AttemptCount != 2 || pump .LastCursor != "cursor-retry" || pump .ErrorCount != 0 {
454+ t .Fatalf ("pump = %#v" , pump )
455+ }
456+ }
457+
407458func TestRunDaemonRemotePollSkipsExpiredLease (t * testing.T ) {
408459 dir := t .TempDir ()
409460 transcriptPath := filepath .Join (dir , "session.jsonl" )
0 commit comments