fix(codex): reconcile cold connections without resuming queued goals
This commit is contained in:
+54
-24
@@ -58,6 +58,7 @@ type Chat struct {
|
||||
approvals map[string]pendingApproval
|
||||
commands map[string]map[string]commandSnapshot
|
||||
commandOrder uint64
|
||||
starting map[string]bool
|
||||
turns map[string]string
|
||||
emitted map[string]bool
|
||||
closed bool
|
||||
@@ -299,24 +300,7 @@ func (c *Chat) unsubscribe(id string, ch chan protocol.ChatEventFrame) {
|
||||
|
||||
// Stream exposes one thread's notifications as chat frames.
|
||||
func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
||||
ch := c.subscribe(id)
|
||||
defer c.unsubscribe(id, ch)
|
||||
if ready != nil {
|
||||
ready()
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case frame, ok := <-ch:
|
||||
if !ok {
|
||||
return errors.New("Codex connection closed")
|
||||
}
|
||||
if err := emit(frame); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return c.streamWithReadback(ctx, id, ready, emit)
|
||||
}
|
||||
|
||||
// Prompt starts one turn. The user turn is already broadcast by the server, so
|
||||
@@ -359,7 +343,30 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess
|
||||
if model != "" {
|
||||
params["model"] = model
|
||||
}
|
||||
_, err = c.call(ctx, "turn/start", params)
|
||||
c.mu.Lock()
|
||||
if c.starting == nil {
|
||||
c.starting = map[string]bool{}
|
||||
}
|
||||
c.starting[id] = true
|
||||
c.mu.Unlock()
|
||||
raw, err := c.call(ctx, "turn/start", params)
|
||||
var reply struct {
|
||||
Turn struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"turn"`
|
||||
}
|
||||
if err == nil {
|
||||
_ = json.Unmarshal(raw, &reply)
|
||||
}
|
||||
c.mu.Lock()
|
||||
delete(c.starting, id)
|
||||
if reply.Turn.ID != "" {
|
||||
c.turns[id] = reply.Turn.ID
|
||||
}
|
||||
c.mu.Unlock()
|
||||
if err == nil && reply.Turn.ID != "" {
|
||||
c.emit(stepFrame(id, protocol.ChatStepStarted, reply.Turn.ID))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -493,6 +500,10 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
}
|
||||
if turn.ID != "" && rawText(turn.Error) != "" {
|
||||
part := item{ID: turn.ID + "#error", Type: "Codex turn", Status: "failed", Error: turn.Error, Input: map[string]any{"turn_id": turn.ID}}.toolPart(true)
|
||||
messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{part}})
|
||||
}
|
||||
}
|
||||
page := protocol.ChatHistoryPage{Messages: messages}
|
||||
if result.NextCursor != nil {
|
||||
@@ -664,13 +675,25 @@ func (c *Chat) handleNotification(msg rpcMessage) {
|
||||
if c.turns[params.ThreadID] == "" {
|
||||
c.turns[params.ThreadID] = params.Turn.ID
|
||||
}
|
||||
nativeID := c.turns[params.ThreadID]
|
||||
c.mu.Unlock()
|
||||
c.emit(stepFrame(params.ThreadID, "started"))
|
||||
c.emit(stepFrame(params.ThreadID, "started", nativeID))
|
||||
case "turn/completed":
|
||||
completedID := params.TurnID
|
||||
if completedID == "" {
|
||||
completedID = params.Turn.ID
|
||||
}
|
||||
if completedID == "" {
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
if current := c.turns[params.ThreadID]; current != "" && current != completedID {
|
||||
c.mu.Unlock()
|
||||
return
|
||||
}
|
||||
delete(c.turns, params.ThreadID)
|
||||
c.mu.Unlock()
|
||||
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded))
|
||||
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded, completedID))
|
||||
case "item/started":
|
||||
c.handleItem(params.ThreadID, params.ItemID, params.Item, false)
|
||||
case "item/completed":
|
||||
@@ -740,10 +763,14 @@ func textFrame(threadID, itemID, kind, delta string) protocol.ChatEventFrame {
|
||||
}
|
||||
}
|
||||
|
||||
func stepFrame(threadID, status string) protocol.ChatEventFrame {
|
||||
func stepFrame(threadID, status string, nativeID ...string) protocol.ChatEventFrame {
|
||||
step := &protocol.ChatStep{Status: status}
|
||||
if len(nativeID) > 0 {
|
||||
step.TurnID = nativeID[0]
|
||||
}
|
||||
return protocol.ChatEventFrame{
|
||||
SessionID: threadID, Kind: protocol.ChatEventStep,
|
||||
Step: &protocol.ChatStep{Status: status},
|
||||
Step: step,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -760,7 +787,10 @@ type thread struct {
|
||||
}
|
||||
|
||||
type turn struct {
|
||||
Items []item `json:"items"`
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Error json.RawMessage `json:"error"`
|
||||
Items []item `json:"items"`
|
||||
}
|
||||
|
||||
type item struct {
|
||||
|
||||
Reference in New Issue
Block a user