fix(codex): load persisted threads only on explicit send after refusal

This commit is contained in:
2026-10-20 18:46:17 +01:00
parent d564765ca5
commit 03a508fa2b
4 changed files with 119 additions and 5 deletions
+36 -4
View File
@@ -74,8 +74,9 @@ type pendingForm struct {
}
type rpcResponse struct {
Result json.RawMessage
Error *rpcError
Result json.RawMessage
Error *rpcError
Transport bool
}
type rpcError struct {
@@ -83,6 +84,13 @@ type rpcError struct {
Message string `json:"message"`
}
type rpcCallError struct {
Code int
Message string
}
func (e *rpcCallError) Error() string { return e.Message }
type rpcMessage struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
@@ -195,7 +203,7 @@ func (c *Chat) failPending(err error) {
c.subs = map[string]map[chan protocol.ChatEventFrame]struct{}{}
c.mu.Unlock()
for _, ch := range pending {
ch <- rpcResponse{Error: &rpcError{Message: err.Error()}}
ch <- rpcResponse{Error: &rpcError{Message: err.Error()}, Transport: true}
}
for _, set := range subs {
for ch := range set {
@@ -241,7 +249,10 @@ func (c *Chat) call(ctx context.Context, method string, params any) (json.RawMes
return nil, ctx.Err()
case resp := <-ch:
if resp.Error != nil {
return nil, errors.New(resp.Error.Message)
if resp.Transport {
return nil, errors.New(resp.Error.Message)
}
return nil, &rpcCallError{Code: resp.Error.Code, Message: resp.Error.Message}
}
return resp.Result, nil
}
@@ -350,6 +361,27 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess
c.starting[id] = true
c.mu.Unlock()
raw, err := c.call(ctx, "turn/start", params)
// A read-only browser connection may point at a persisted, unloaded thread.
// Only an explicit Send can load it. Retry solely after the provider's exact
// application-level refusal: transport/timeout failures may have admitted a
// turn and must never be blindly repeated.
var refused *rpcCallError
if errors.As(err, &refused) && refused.Message == "thread not found: "+id {
var resumed json.RawMessage
resumed, err = c.call(ctx, "thread/resume", map[string]any{"threadId": id, "excludeTurns": true})
if err == nil {
var response struct {
Thread struct {
ID string `json:"id"`
} `json:"thread"`
}
if json.Unmarshal(resumed, &response) != nil || response.Thread.ID != id {
err = errors.New("Codex did not confirm the requested thread was loaded")
} else {
raw, err = c.call(ctx, "turn/start", params)
}
}
}
var reply struct {
Turn struct {
ID string `json:"id"`