fix(codex): load persisted threads only on explicit send after refusal
This commit is contained in:
@@ -3,6 +3,7 @@ package codex
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -75,3 +76,72 @@ func TestLateCompletionDoesNotClearNewCodexTurn(t *testing.T) {
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitPromptLoadsOnlyAConfirmedUnloadedThread(t *testing.T) {
|
||||
var methods []string
|
||||
c := modelTestChat(t, func(method string, params json.RawMessage) any {
|
||||
methods = append(methods, method)
|
||||
if method == "turn/start" {
|
||||
var p map[string]any
|
||||
json.Unmarshal(params, &p)
|
||||
if p["model"] != "fixture-model" || p["clientUserMessageId"] != "msg_same" {
|
||||
t.Errorf("recovery changed explicit turn identity/model: %s", params)
|
||||
}
|
||||
}
|
||||
if len(methods) == 1 {
|
||||
return &rpcError{Code: -32602, Message: "thread not found: " + fixtureThread}
|
||||
}
|
||||
if method == "thread/resume" {
|
||||
var p map[string]any
|
||||
json.Unmarshal(params, &p)
|
||||
if p["threadId"] != fixtureThread || p["excludeTurns"] != true || len(p) != 2 {
|
||||
t.Errorf("resume changed configuration: %s", params)
|
||||
}
|
||||
return map[string]any{"thread": map[string]any{"id": fixtureThread}}
|
||||
}
|
||||
return map[string]any{"turn": map[string]any{"id": "loaded-turn"}}
|
||||
})
|
||||
if err := c.PromptWithMessageIDModel(context.Background(), fixtureThread, "explicit user message", "msg_same", "fixture-model"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(methods) != 3 || methods[0] != "turn/start" || methods[1] != "thread/resume" || methods[2] != "turn/start" {
|
||||
t.Fatalf("unsafe recovery sequence: %v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnrelatedPromptRefusalIsNeverRetriedOrResumed(t *testing.T) {
|
||||
methods := []string{}
|
||||
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
|
||||
methods = append(methods, method)
|
||||
return &rpcError{Code: -32602, Message: "controlled provider rejection"}
|
||||
})
|
||||
if err := c.Prompt(context.Background(), fixtureThread, "explicit user message"); err == nil {
|
||||
t.Fatal("refusal ignored")
|
||||
}
|
||||
if len(methods) != 1 || methods[0] != "turn/start" {
|
||||
t.Fatalf("unsafe retry: %v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportFailureCannotTriggerAnUnloadedThreadRetry(t *testing.T) {
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
var once sync.Once
|
||||
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
|
||||
if method != "turn/start" {
|
||||
t.Errorf("transport failure retried through %s", method)
|
||||
}
|
||||
once.Do(func() { close(entered) })
|
||||
<-release
|
||||
return map[string]any{"turn": map[string]any{"id": "possibly-admitted"}}
|
||||
})
|
||||
result := make(chan error, 1)
|
||||
go func() { result <- c.Prompt(context.Background(), fixtureThread, "explicit message") }()
|
||||
<-entered
|
||||
// Even a transport failure whose text resembles a provider refusal is not
|
||||
// evidence that the original turn was never admitted.
|
||||
c.failPending(errors.New("thread not found: " + fixtureThread))
|
||||
if err := <-result; err == nil {
|
||||
t.Fatal("transport failure reported success")
|
||||
}
|
||||
close(release)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user