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) }