Render run list miniatures from compact per-stage route evidence
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGraphEvidenceSubscribeSnapshot(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
store, handler, ids := eventStore(t, ctx, "a", "b", "c")
|
||||
definition, err := store.Get(ctx, ids[0], 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, _, err := store.CreateRun(ctx, definition, "graph-snapshot", "workspace", false, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for index, key := range []string{"a", "b", "c"} {
|
||||
sequence := index + 1
|
||||
if err := store.BeginStep(ctx, id, sequence, key, map[string]any{"prompt": "PRIVATE-PROMPT"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
status, outcome, target := "completed", "success", "b"
|
||||
if key == "b" {
|
||||
target = "c"
|
||||
}
|
||||
if key == "c" {
|
||||
status, outcome, target = "failed", "", ""
|
||||
}
|
||||
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: sequence, Status: status, Outcome: outcome, Target: target, Transcript: "PRIVATE-TRANSCRIPT"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
hub := newRunEventHub()
|
||||
defer hub.unsubscribeClient("snapshot-test")
|
||||
var messages []protocol.Message
|
||||
if !handleRunEvents(protocol.Message{Type: protocol.Subscribe}, hub, handler.runner, "snapshot-test", func(message protocol.Message) error { messages = append(messages, message); return nil }) {
|
||||
t.Fatal("unhandled subscribe")
|
||||
}
|
||||
if len(messages) != 2 || messages[0].RunEvent == nil || messages[1].Type != protocol.Subscribe {
|
||||
t.Fatalf("snapshot/ack: %+v", messages)
|
||||
}
|
||||
evidence := messages[0].RunEvent.GraphEvidence
|
||||
if evidence == nil || !evidence.Complete || len(evidence.Visits) != 3 || evidence.Visits[2].Status != "failed" {
|
||||
t.Fatalf("missing persisted graph evidence: %+v", evidence)
|
||||
}
|
||||
encoded, _ := json.Marshal(messages[0])
|
||||
if strings.Contains(string(encoded), "PRIVATE") {
|
||||
t.Fatal(string(encoded))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoalescedGraphVisitsRetainEveryStage(t *testing.T) {
|
||||
hub := newRunEventHub()
|
||||
hub.interval = time.Hour
|
||||
sub := hub.subscribe("graph-test", "", nil, func(protocol.Message) error { return nil })
|
||||
defer sub.close()
|
||||
// Hold subsequent frames in the coalescer without relying on wall-clock races.
|
||||
hub.emit(protocol.RunEventFrame{RunID: 1, Status: "running"}, false)
|
||||
for sequence, stage := range []string{"a", "b", "c"} {
|
||||
hub.emit(protocol.RunEventFrame{RunID: 1, Step: &protocol.RunStepDelta{Sequence: sequence + 1, Stage: stage, Status: "completed", Outcome: "success", Target: "next", Diagnostic: "PRIVATE"}}, false)
|
||||
}
|
||||
hub.mu.Lock()
|
||||
pending := hub.runs[1].pending
|
||||
hub.stopTimerLocked(hub.runs[1])
|
||||
hub.mu.Unlock()
|
||||
if pending == nil || len(pending.GraphVisits) != 3 || pending.Step.Stage != "c" {
|
||||
t.Fatalf("coalescing lost evidence: %+v", pending)
|
||||
}
|
||||
if pending.GraphVisits[0].Stage != "a" || pending.GraphVisits[1].Stage != "b" {
|
||||
t.Fatal(pending.GraphVisits)
|
||||
}
|
||||
}
|
||||
@@ -173,6 +173,9 @@ func (h *runEventHub) remove(sub *runSubscription) {
|
||||
// A terminal transition flushes any pending delta first, then the terminal frame
|
||||
// immediately, so a client always observes the final state as its own frame.
|
||||
func (h *runEventHub) emit(frame protocol.RunEventFrame, terminal bool) {
|
||||
if step := frame.Step; step != nil {
|
||||
frame.GraphVisits = append(frame.GraphVisits, protocol.RunGraphVisit{Sequence: step.Sequence, Stage: step.Stage, Status: step.Status, Outcome: step.Outcome, Target: step.Target})
|
||||
}
|
||||
if h == nil || frame.RunID <= 0 {
|
||||
return
|
||||
}
|
||||
@@ -470,6 +473,7 @@ func mergeRunEventFrame(into, in *protocol.RunEventFrame) {
|
||||
if in.Step != nil {
|
||||
into.Step = in.Step
|
||||
}
|
||||
into.GraphVisits = append(into.GraphVisits, in.GraphVisits...)
|
||||
}
|
||||
|
||||
// handleRunEvents dispatches one subscribe/unsubscribe request and reports
|
||||
@@ -505,6 +509,7 @@ func handleRunEvents(request protocol.Message, events *runEventHub, runs *runner
|
||||
continue
|
||||
}
|
||||
frame := protocol.RunEventFrame{
|
||||
GraphEvidence: summary.GraphEvidence,
|
||||
NotifyMode: summary.NotifyMode,
|
||||
RunID: summary.ID,
|
||||
Workspace: summary.Workspace,
|
||||
|
||||
Reference in New Issue
Block a user