Expose recorded background task timing and progress metadata
This commit is contained in:
@@ -377,16 +377,23 @@ func (t *chatTurn) applyShell(shell *protocol.ChatShell) {
|
||||
}
|
||||
t.shellOrder = removeShellID(t.shellOrder, shell.ID)
|
||||
delete(t.shells, shell.ID)
|
||||
t.shrink(len(old.Command))
|
||||
t.shrink(protocol.ChatShellRetainedBytes(old))
|
||||
return
|
||||
}
|
||||
if t.shells == nil {
|
||||
t.shells = map[string]protocol.ChatShell{}
|
||||
}
|
||||
if _, ok := t.shells[shell.ID]; !ok {
|
||||
if !t.retain(len(shell.Command)) {
|
||||
return
|
||||
}
|
||||
old, exists := t.shells[shell.ID]
|
||||
merged := protocol.PreserveChatShellMetadata(*shell, old)
|
||||
shell = &merged
|
||||
delta := protocol.ChatShellRetainedBytes(*shell) - protocol.ChatShellRetainedBytes(old)
|
||||
if delta > 0 && !t.retain(delta) {
|
||||
return
|
||||
}
|
||||
if delta < 0 {
|
||||
t.shrink(-delta)
|
||||
}
|
||||
if !exists {
|
||||
t.shellOrder = append(t.shellOrder, shell.ID)
|
||||
}
|
||||
t.shells[shell.ID] = *shell
|
||||
@@ -395,7 +402,7 @@ func (t *chatTurn) applyShell(shell *protocol.ChatShell) {
|
||||
t.shellOrder = t.shellOrder[1:]
|
||||
if dropped, ok := t.shells[id]; ok {
|
||||
delete(t.shells, id)
|
||||
t.shrink(len(dropped.Command))
|
||||
t.shrink(protocol.ChatShellRetainedBytes(dropped))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,13 +561,15 @@ func (h *chatHub) replaceShells(sessionID string, shells []protocol.ChatShell) {
|
||||
|
||||
func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatShell) {
|
||||
turn := h.turnLocked(sessionID)
|
||||
prior := turn.shells
|
||||
for _, shell := range turn.shells {
|
||||
turn.shrink(len(shell.Command))
|
||||
turn.shrink(protocol.ChatShellRetainedBytes(shell))
|
||||
}
|
||||
turn.shells = nil
|
||||
turn.shellOrder = nil
|
||||
for i := range shells {
|
||||
turn.applyShell(&shells[i])
|
||||
merged := protocol.PreserveChatShellMetadata(shells[i], prior[shells[i].ID])
|
||||
turn.applyShell(&merged)
|
||||
}
|
||||
h.noteActivityLocked(sessionID)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestShellMetadataSurvivesPartialReplayAndUsesByteBudget(t *testing.T) {
|
||||
turn := &chatTurn{}
|
||||
task := protocol.ChatShell{ID: "task", Status: protocol.ChatShellRunning, Command: "sleep 30", StartedAt: 1793870280000, Description: "Review release", Progress: "Checking tests"}
|
||||
turn.applyShell(&task)
|
||||
size := protocol.ChatShellRetainedBytes(task)
|
||||
if turn.size != size {
|
||||
t.Fatalf("metadata not counted: size=%d want=%d", turn.size, size)
|
||||
}
|
||||
turn.applyShell(&protocol.ChatShell{ID: "task", Status: protocol.ChatShellRunning})
|
||||
if turn.shells["task"].Command != "sleep 30" || turn.shells["task"].StartedAt != task.StartedAt || turn.shells["task"].Progress != "Checking tests" || turn.size != size {
|
||||
t.Fatal("partial frame lost metadata/budget")
|
||||
}
|
||||
turn.applyShell(&protocol.ChatShell{ID: "task", Status: protocol.ChatShellRunning, Command: "sleep 30", Description: strings.Repeat("x", chatRetainedLimit)})
|
||||
if !turn.overflow || turn.size != size {
|
||||
t.Fatal("metadata bypassed byte cap")
|
||||
}
|
||||
turn.applyShell(&protocol.ChatShell{ID: "task", Status: protocol.ChatShellExited})
|
||||
if turn.size != 0 || len(turn.shells) != 0 {
|
||||
t.Fatal("exit did not release metadata budget")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthoritativeShellReadbackPreservesKnownTiming(t *testing.T) {
|
||||
hub := newChatHub()
|
||||
task := protocol.ChatShell{ID: "task", Status: protocol.ChatShellRunning, Command: "sleep 30", StartedAt: 1793870280000, Description: "Review"}
|
||||
hub.replaceShells("session", []protocol.ChatShell{task})
|
||||
hub.replaceShells("session", []protocol.ChatShell{{ID: "task", Status: protocol.ChatShellRunning, Command: "sleep 30"}})
|
||||
hub.mu.Lock()
|
||||
turn := hub.turnLocked("session")
|
||||
got := turn.shells["task"]
|
||||
size := turn.size
|
||||
hub.mu.Unlock()
|
||||
if got.StartedAt != task.StartedAt || got.Description != "Review" || size != protocol.ChatShellRetainedBytes(task) {
|
||||
t.Fatalf("readback lost timing: %+v size=%d", got, size)
|
||||
}
|
||||
hub.replaceShells("session", nil)
|
||||
hub.mu.Lock()
|
||||
defer hub.mu.Unlock()
|
||||
if turn.size != 0 || len(turn.shells) != 0 {
|
||||
t.Fatal("stale shell metadata remained after authoritative removal")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user