Merge branch 'worktree-agent-a3cb3553b5c91a345'
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// mappedShownRuns is a run store that also reads the card's graph map.
|
||||
type mappedShownRuns struct {
|
||||
mu sync.Mutex
|
||||
runs fakeShownRuns
|
||||
maps map[int64]*protocol.ChatShownRunMap
|
||||
}
|
||||
|
||||
func (m *mappedShownRuns) RunSummaryByID(ctx context.Context, id int64) (protocol.RunSummary, bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.runs.RunSummaryByID(ctx, id)
|
||||
}
|
||||
|
||||
func (m *mappedShownRuns) RunShownMap(_ context.Context, id int64) (*protocol.ChatShownRunMap, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.maps[id], nil
|
||||
}
|
||||
|
||||
func TestShowRunCarriesTheGraphMapAndRefreshesItsProgress(t *testing.T) {
|
||||
previous := shownRunRefreshDelay
|
||||
shownRunRefreshDelay = 10 * time.Millisecond
|
||||
t.Cleanup(func() { shownRunRefreshDelay = previous })
|
||||
f := newChatToolFixture(t)
|
||||
stages := func(implement string) []protocol.ChatShownRunStage {
|
||||
return []protocol.ChatShownRunStage{
|
||||
{Key: "plan", Edges: map[string]string{"success": "implement"}, Visits: 1, Status: "completed", Seconds: 30},
|
||||
{Key: "implement", Edges: map[string]string{"success": "#"}, Visits: 1, Status: implement},
|
||||
}
|
||||
}
|
||||
store := &mappedShownRuns{
|
||||
runs: fakeShownRuns{7: {ID: 7, Name: "feature", Status: "running", CurrentStage: "implement", CreatedAt: "2026-12-01T10:00:00Z"}},
|
||||
maps: map[int64]*protocol.ChatShownRunMap{7: {Stages: stages("running"), Visits: []protocol.RunGraphVisit{
|
||||
{Sequence: 1, Stage: "plan", Status: "completed", Outcome: "success", Target: "implement"},
|
||||
{Sequence: 2, Stage: "implement", Status: "running"},
|
||||
}}},
|
||||
}
|
||||
f.service.runs = store
|
||||
token := f.token(t, chatToolSessionScope+"ses_m")
|
||||
if r := f.call(token, "show_run", `{"run_id":7}`); r.IsError {
|
||||
t.Fatalf("show_run: %v", r)
|
||||
}
|
||||
item := f.items(t, "ses_m")[0]
|
||||
if item.Run.Map == nil || len(item.Run.Map.Stages) != 2 || item.Run.Map.Stages[1].Status != "running" {
|
||||
t.Fatalf("run map = %+v", item.Run.Map)
|
||||
}
|
||||
|
||||
// Only the stage progress changes: the summary is the same, the card is not.
|
||||
store.mu.Lock()
|
||||
store.maps[7] = &protocol.ChatShownRunMap{Stages: stages("completed"), Visits: store.maps[7].Visits}
|
||||
store.mu.Unlock()
|
||||
f.service.observeRun(protocol.RunEventFrame{RunID: 7, Status: "running"})
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
item = f.items(t, "ses_m")[0]
|
||||
if item.Run.Map != nil && item.Run.Map.Stages[1].Status == "completed" {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("the run map was not refreshed: %+v", item.Run.Map)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -44,7 +45,7 @@ func (s *chatToolService) runShowRun(ctx context.Context, runID int64, title str
|
||||
if !found {
|
||||
return fail(fmt.Sprintf("there is no run %d.", runID))
|
||||
}
|
||||
body := shownRunBody(summary)
|
||||
body := s.shownRunBody(ctx, summary)
|
||||
item := protocol.ChatShownItem{Kind: protocol.ChatShownRun, Title: title, Run: &body}
|
||||
text := fmt.Sprintf("Shown in the operator's Pipelines chat: run %d of %s, %s", runID, body.Pipeline, body.Status)
|
||||
if body.CurrentStage != "" && !shownRunTerminal(body.Status) {
|
||||
@@ -53,12 +54,26 @@ func (s *chatToolService) runShowRun(ctx context.Context, runID int64, title str
|
||||
return &item, chattools.Result{Text: text + "; the card follows the run live."}
|
||||
}
|
||||
|
||||
func shownRunBody(summary protocol.RunSummary) protocol.ChatShownRunBody {
|
||||
return protocol.ChatShownRunBody{
|
||||
// chatShownRunMapStore reads the graph and stage progress a run card draws
|
||||
// (pipeline.Store). It is optional: without it a card has no mini-map.
|
||||
type chatShownRunMapStore interface {
|
||||
RunShownMap(ctx context.Context, id int64) (*protocol.ChatShownRunMap, error)
|
||||
}
|
||||
|
||||
func (s *chatToolService) shownRunBody(ctx context.Context, summary protocol.RunSummary) protocol.ChatShownRunBody {
|
||||
body := protocol.ChatShownRunBody{
|
||||
ID: summary.ID, Pipeline: summary.Name, Title: summary.Title, Status: summary.Status, Outcome: summary.Outcome,
|
||||
CurrentStage: summary.CurrentStage, Workspace: summary.Workspace,
|
||||
StartedAt: shownRunTime(summary.CreatedAt), FinishedAt: shownRunTime(summary.FinishedAt),
|
||||
}
|
||||
if maps, ok := s.runs.(chatShownRunMapStore); ok {
|
||||
runMap, err := maps.RunShownMap(ctx, summary.ID)
|
||||
if err != nil {
|
||||
log.Printf("chat tools: run %d card map: %v", summary.ID, err)
|
||||
}
|
||||
body.Map = runMap
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func shownRunTime(value string) int64 {
|
||||
@@ -152,13 +167,13 @@ func (s *chatToolService) refreshRunItem(ctx context.Context, sessionID string,
|
||||
if err != nil || !found {
|
||||
return item
|
||||
}
|
||||
body := shownRunBody(summary)
|
||||
body := s.shownRunBody(ctx, summary)
|
||||
if shownRunTerminal(body.Status) {
|
||||
s.unwatchRun(body.ID, item.ID)
|
||||
} else {
|
||||
s.watchRun(body.ID, sessionID, item.ID)
|
||||
}
|
||||
if body == *item.Run {
|
||||
if reflect.DeepEqual(body, *item.Run) {
|
||||
return item
|
||||
}
|
||||
item.Run = &body
|
||||
|
||||
Reference in New Issue
Block a user