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
+15 -4
View File
@@ -835,11 +835,22 @@ type ChatAnswerRequest struct {
Answers map[string]any `json:"answers"`
}
// ChatShell carries only backend-recorded facts. Timing is epoch milliseconds;
// absent optional values mean unknown, never the time the list was read.
type ChatShell struct {
ID string `json:"id"`
Status string `json:"status,omitempty"`
Command string `json:"command,omitempty"`
Exit *int `json:"exit,omitempty"`
ID string `json:"id"`
Status string `json:"status,omitempty"`
Command string `json:"command,omitempty"`
Exit *int `json:"exit,omitempty"`
StartedAt int64 `json:"started_at,omitempty"`
UpdatedAt int64 `json:"updated_at,omitempty"`
TaskType string `json:"task_type,omitempty"`
Description string `json:"description,omitempty"`
Progress string `json:"progress,omitempty"`
LastTool string `json:"last_tool,omitempty"`
DurationMS *int64 `json:"duration_ms,omitempty"`
ToolCount *int64 `json:"tool_count,omitempty"`
TokenCount *int64 `json:"token_count,omitempty"`
}
// ChatShellOutputQuery names one selected shell and a bounded absolute-byte
+47
View File
@@ -0,0 +1,47 @@
package protocol
// PreserveChatShellMetadata carries recorded facts through partial lifecycle
// frames/readback. It never fills unknown times from a clock. Shell identities
// must match; different tasks cannot lend one another metadata.
func PreserveChatShellMetadata(shell, prior ChatShell) ChatShell {
if shell.ID == "" || shell.ID != prior.ID {
return shell
}
if shell.Command == "" {
shell.Command = prior.Command
}
if shell.StartedAt <= 0 {
shell.StartedAt = prior.StartedAt
}
if shell.UpdatedAt < prior.UpdatedAt {
shell.UpdatedAt = prior.UpdatedAt
}
if shell.TaskType == "" {
shell.TaskType = prior.TaskType
}
if shell.Description == "" {
shell.Description = prior.Description
}
if shell.Progress == "" {
shell.Progress = prior.Progress
}
if shell.LastTool == "" {
shell.LastTool = prior.LastTool
}
keepCounter := func(next, old *int64) *int64 {
if next == nil || *next < 0 || (old != nil && *next < *old) {
return old
}
return next
}
shell.DurationMS = keepCounter(shell.DurationMS, prior.DurationMS)
shell.ToolCount = keepCounter(shell.ToolCount, prior.ToolCount)
shell.TokenCount = keepCounter(shell.TokenCount, prior.TokenCount)
return shell
}
// ChatShellRetainedBytes counts the retained strings (not output pages) used by
// the server and in-flight readback byte budgets.
func ChatShellRetainedBytes(shell ChatShell) int {
return len(shell.Command) + len(shell.TaskType) + len(shell.Description) + len(shell.Progress) + len(shell.LastTool)
}
@@ -0,0 +1,34 @@
package protocol
import (
"encoding/json"
"testing"
)
func TestShellMetadataPartialFrameAndExplicitZero(t *testing.T) {
zero := int64(0)
tools := int64(4)
prior := ChatShell{ID: "task", Command: "sleep 30", StartedAt: 1793870280000, UpdatedAt: 1793870400000, Description: "Review", Progress: "Checking", TaskType: "agent", LastTool: "Bash", ToolCount: &tools, TokenCount: &zero}
current := PreserveChatShellMetadata(ChatShell{ID: "task", Status: ChatShellRunning}, prior)
if current.Command != "sleep 30" || current.StartedAt != prior.StartedAt || current.Description != "Review" || current.ToolCount == nil || *current.ToolCount != 4 || current.TokenCount == nil || *current.TokenCount != 0 {
t.Fatalf("metadata lost: %+v", current)
}
raw, err := json.Marshal(current)
if err != nil {
t.Fatal(err)
}
var wire map[string]any
if err = json.Unmarshal(raw, &wire); err != nil {
t.Fatal(err)
}
if value, exists := wire["token_count"]; !exists || value != float64(0) {
t.Fatalf("zero counter disappeared: %s", raw)
}
if _, exists := wire["duration_ms"]; exists {
t.Fatal("unknown duration was invented")
}
foreign := PreserveChatShellMetadata(ChatShell{ID: "foreign"}, prior)
if foreign.StartedAt != 0 || foreign.Description != "" {
t.Fatal("foreign metadata leaked")
}
}