diff --git a/internal/codex/chat.go b/internal/codex/chat.go index 3785b3c9..dc38be94 100644 --- a/internal/codex/chat.go +++ b/internal/codex/chat.go @@ -13,6 +13,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net" "net/http" "net/url" @@ -568,14 +569,16 @@ func (c *Chat) ReleaseSession(ctx context.Context, id string) error { return err } } - _, err := c.call(ctx, "thread/unsubscribe", map[string]any{"threadId": id}) - var refused *rpcCallError - if errors.As(err, &refused) && refused.Code == -32601 { - // An App Server without thread/unsubscribe keeps the thread loaded; - // the interrupt above already stopped its work. - return nil + // Unloading is cleanup: a thread the App Server no longer holds, or a + // server without thread/unsubscribe (-32601), must not make the close or + // /new fail; the interrupt above already stopped its work. + if _, err := c.call(ctx, "thread/unsubscribe", map[string]any{"threadId": id}); err != nil { + var refused *rpcCallError + if !errors.As(err, &refused) || refused.Code != -32601 { + log.Printf("codex release session=%s: unsubscribe: %v", id, err) + } } - return err + return nil } // Create starts one Codex thread in directory. diff --git a/internal/codex/detach_test.go b/internal/codex/detach_test.go index 920215aa..9c396330 100644 --- a/internal/codex/detach_test.go +++ b/internal/codex/detach_test.go @@ -102,3 +102,14 @@ func TestReleaseSessionUnloadsIdleThreadAndToleratesOldServers(t *testing.T) { t.Fatalf("an idle release must only unsubscribe: %v", methods) } } + +// Unloading is cleanup: a thread the App Server no longer holds must not make +// a close or /new fail. +func TestReleaseSessionToleratesAnUnsubscribeRefusal(t *testing.T) { + c := modelTestChat(t, func(string, json.RawMessage) any { + return &rpcError{Code: -32600, Message: "thread not found"} + }) + if err := c.ReleaseSession(context.Background(), fixtureThread); err != nil { + t.Fatalf("a refused unsubscribe must not fail the release: %v", err) + } +}