Expose recorded background task timing and progress metadata

This commit is contained in:
2026-11-06 15:08:34 +00:00
parent c9a4cd0bf6
commit 6421554bfe
16 changed files with 688 additions and 31 deletions
+17 -8
View File
@@ -818,12 +818,14 @@ func (c *Chat) handleNotification(msg rpcMessage) {
return
}
var params struct {
ThreadID string `json:"threadId"`
TurnID string `json:"turnId"`
ItemID string `json:"itemId"`
Delta string `json:"delta"`
Item json.RawMessage `json:"item"`
Turn struct {
StartedAtMS int64 `json:"startedAtMs"`
CompletedAtMS int64 `json:"completedAtMs"`
ThreadID string `json:"threadId"`
TurnID string `json:"turnId"`
ItemID string `json:"itemId"`
Delta string `json:"delta"`
Item json.RawMessage `json:"item"`
Turn struct {
ID string `json:"id"`
Status string `json:"status"`
} `json:"turn"`
@@ -861,9 +863,9 @@ func (c *Chat) handleNotification(msg rpcMessage) {
c.mu.Unlock()
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded, completedID))
case "item/started":
c.handleItem(params.ThreadID, params.ItemID, params.Item, false, params.TurnID)
c.handleTimedItem(params.ThreadID, params.ItemID, params.Item, false, params.StartedAtMS, 0, params.TurnID)
case "item/completed":
c.handleItem(params.ThreadID, params.ItemID, params.Item, true, params.TurnID)
c.handleTimedItem(params.ThreadID, params.ItemID, params.Item, true, 0, params.CompletedAtMS, params.TurnID)
case "thread/compacted":
// Deprecated ContextCompactedNotification has only threadId/turnId.
// Modern item lifecycles are correlated more precisely; never let this
@@ -890,6 +892,10 @@ func (c *Chat) handleNotification(msg rpcMessage) {
}
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool, nativeTurnID ...string) {
c.handleTimedItem(threadID, itemID, raw, completed, 0, 0, nativeTurnID...)
}
func (c *Chat) handleTimedItem(threadID, itemID string, raw json.RawMessage, completed bool, startedAt, completedAt int64, nativeTurnID ...string) {
var record item
if err := json.Unmarshal(raw, &record); err != nil {
return
@@ -897,6 +903,7 @@ func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, complete
if record.ID == "" {
record.ID = itemID
}
record.startedAtMS, record.completedAtMS = startedAt, completedAt
switch record.Type {
case "contextCompaction":
if record.ID == "" {
@@ -994,6 +1001,8 @@ func (t turn) createdAtMillis() int64 {
}
type item struct {
startedAtMS int64
completedAtMS int64
Raw json.RawMessage `json:"-"`
ID string `json:"id"`
ClientID string `json:"clientId"`
+38
View File
@@ -0,0 +1,38 @@
package codex
import (
"encoding/json"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestCommandLifecycleUsesRecordedTimingAndKeepsItAcrossReadback(t *testing.T) {
c := &Chat{subs: map[string]map[chan protocol.ChatEventFrame]struct{}{}}
c.handleNotification(rpcMessage{Method: "item/started", Params: json.RawMessage(`{"threadId":"thread","turnId":"turn","startedAtMs":1793870280123,"item":{"id":"cmd","type":"commandExecution","command":"sleep 30","status":"inProgress"}}`)})
c.recordCommand("thread", item{ID: "cmd", Command: "sleep 30", Status: "inProgress"}, false)
id := codexShellID("thread", "cmd")
c.mu.Lock()
task := c.commands["thread"][id].shell
c.mu.Unlock()
if task.StartedAt != 1793870280123 || task.TaskType != "command" || task.UpdatedAt != 0 {
t.Fatalf("start=%+v", task)
}
c.handleNotification(rpcMessage{Method: "item/completed", Params: json.RawMessage(`{"threadId":"thread","turnId":"turn","completedAtMs":1793870310123,"item":{"id":"cmd","type":"commandExecution","command":"sleep 30","status":"completed","durationMs":30000}}`)})
c.mu.Lock()
task = c.commands["thread"][id].shell
c.mu.Unlock()
if task.StartedAt != 1793870280123 || task.UpdatedAt != 1793870310123 || task.DurationMS == nil || *task.DurationMS != 30000 || task.Status != protocol.ChatShellExited {
t.Fatalf("completion=%+v", task)
}
}
func TestHistoricalCommandDoesNotInventAStart(t *testing.T) {
c := &Chat{}
duration := int64(1200)
c.recordCommand("thread", item{ID: "cmd", Status: "inProgress", DurationMS: &duration}, false)
task := c.commands["thread"][codexShellID("thread", "cmd")].shell
if task.StartedAt != 0 || task.UpdatedAt != 0 || task.DurationMS == nil || *task.DurationMS != 1200 {
t.Fatalf("history=%+v", task)
}
}
+13 -1
View File
@@ -51,7 +51,19 @@ func (c *Chat) recordCommand(thread string, i item, emit bool) {
status = protocol.ChatShellRunning
}
snapshot := old
snapshot.shell = protocol.ChatShell{ID: id, Command: i.Command, Status: status, Exit: i.ExitCode}
snapshot.shell.ID, snapshot.shell.Status = id, status
snapshot.shell.Command, snapshot.shell.Exit = i.Command, i.ExitCode
snapshot.shell.TaskType = "command"
if i.startedAtMS > 0 && i.startedAtMS <= 8640000000000000 && snapshot.shell.StartedAt == 0 {
snapshot.shell.StartedAt = i.startedAtMS
}
if i.completedAtMS > snapshot.shell.UpdatedAt && i.completedAtMS <= 8640000000000000 {
snapshot.shell.UpdatedAt = i.completedAtMS
}
if i.DurationMS != nil && *i.DurationMS >= 0 && (snapshot.shell.DurationMS == nil || *i.DurationMS >= *snapshot.shell.DurationMS) {
duration := *i.DurationMS
snapshot.shell.DurationMS = &duration
}
if snapshot.shell.Command == "" {
snapshot.shell.Command = old.shell.Command
}