docs/prds/execution-tree-state-machine.md: step_executions (0017) is now the
durable per-step record; the dashboard's run detail (GET /api/tasks/{id}/
attempts) still folded stage_start/stage_finish events into a rail by hand,
and tasks.stage duplicated what StartStep/FinishStep already record properly.
Added the missing read:
- ListSteps(execution_id, attempt) -- attempt 0 lists every attempt of the
execution, oldest first, so the rail renders every attempt from one call.
New storecontract.StepReader (separate from StepRecorder: this is a
dashboard/API/dispatch action per the PRD's wire contract, never
task-scoped-grant callable), postgresdb.ListStepExecutions, staterpc
client/server/values.go plumbing, wired through state_store.go's Deps and
archieui/compose.go's Server.Steps the same optional-capability pattern
every other dashboard read uses.
- internal/webui/api_tasks_attempts.go: stageViewsFromSteps replaces
foldStages/finishStage/stageOutcome. Attempt bounds, event_count and which
attempt numbers exist stay sourced from events (an attempt can carry
events -- agent calls -- with no step), only the stage list itself moves.
The wire JSON contract is unchanged, so no frontend change was needed;
`task ui` confirms ui/dist still matches a fresh build.
- A running step past the task's own current attempt reads interrupted, not
running: RecoverStale (archie-core, still open per #1214) is what corrects
a crashed run's stale "running" row, and until it does this is the honest
read rather than trusting a row that may be stale.
Dropped tasks.stage and every reader, no fallback:
- Migration 0018 drops the column; tasks.sql's INSERT/UPDATE/RETURNING lists
and the sqlc-generated code no longer carry it.
- workflow.Task.Stage is gone; workflow.Run's in-flight "which stage is this"
bookkeeping moves to TaskContext.Stage (execution-time only, never
persisted -- park/finish's event tagging is its only reader).
- pb.Task.stage (wire field 10) is `[deprecated = true]`, not deleted (never
reuse a field number); staterpc's taskProto/taskValue no longer map it.
- taskactions.Task.Stage and the retry event's "previous_stage" data field
are gone: the operator-action package had no reader for the column beyond
echoing it.
- The chat /tasks command's per-task "workflow/stage" display drops to
workflow-only (ChatTaskSummary.Stage removed, workflowStage() deleted):
it was a genuine reader with no replacement in scope for this change.
Read the archie-web-ui skill and the state-store/daemon docs/development
pages first, per instruction. Stayed out of cancel/stop code throughout
(Pi's concurrent CancelExecution work); the only shared files touched
(workflow.go, daemon-adjacent SQL) got the smallest possible diff.
523 lines
16 KiB
Go
523 lines
16 KiB
Go
package webui
|
|
|
|
// The dashboard moved from a hand-written index.html embedded in this package
|
|
// to the Vite build in ui/. Four tests were removed with it:
|
|
//
|
|
// - TestIndexContainsViewportMeta, TestIndexHasResponsiveFeatures asserted
|
|
// markup and CSS that now live in ui/index.html and ui/src/css/.
|
|
// - TestIndexEscapesSingleQuotesInOnclick, TestIndexEscDoesNotEscapeSingleQuotes
|
|
// guarded against injection through string-interpolated onclick handlers.
|
|
// That bug is no longer reachable: ui/src/base/dom.js builds nodes with
|
|
// createTextNode and addEventListener, so no markup is assembled from
|
|
// strings at all.
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
|
)
|
|
|
|
func newTestServer(t *testing.T) *Server {
|
|
t.Helper()
|
|
s := pgstore.Open(t)
|
|
t.Cleanup(func() { _ = s.Close() })
|
|
srv := &Server{Store: s, Steps: s, Log: slog.New(slog.DiscardHandler)}
|
|
// Composition always gives the dashboard a Gateway contract; task
|
|
// actions travel through it now, so a test server without one could
|
|
// only ever answer 503.
|
|
wireOperatorActions(srv)
|
|
return srv
|
|
}
|
|
|
|
// stubStore wraps a real TaskStore and overrides selected methods to
|
|
// inject errors. Methods that are not overridden delegate to the inner
|
|
// store via the embedded interface.
|
|
type stubStore struct {
|
|
storecontract.TaskStore
|
|
workflowStatsErr error
|
|
stageStatsErr error
|
|
tokensByDayErr error
|
|
eventsSinceErr error
|
|
taskEventsErr error
|
|
taskByIDErr error
|
|
requeueErr error
|
|
}
|
|
|
|
func (s *stubStore) TaskByID(ctx context.Context, id int64) (*workflow.Task, error) {
|
|
if s.taskByIDErr != nil {
|
|
return nil, s.taskByIDErr
|
|
}
|
|
return s.TaskStore.TaskByID(ctx, id)
|
|
}
|
|
|
|
func (s *stubStore) Requeue(ctx context.Context, id int64, from, workflow string) error {
|
|
if s.requeueErr != nil {
|
|
return s.requeueErr
|
|
}
|
|
return s.TaskStore.Requeue(ctx, id, from, workflow)
|
|
}
|
|
|
|
func (s *stubStore) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
|
if s.workflowStatsErr != nil {
|
|
return nil, s.workflowStatsErr
|
|
}
|
|
return s.TaskStore.WorkflowStats(ctx)
|
|
}
|
|
|
|
func (s *stubStore) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
|
if s.stageStatsErr != nil {
|
|
return nil, s.stageStatsErr
|
|
}
|
|
return s.TaskStore.StageStats(ctx)
|
|
}
|
|
|
|
func (s *stubStore) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
|
if s.tokensByDayErr != nil {
|
|
return nil, s.tokensByDayErr
|
|
}
|
|
return s.TaskStore.TokensByDay(ctx, days)
|
|
}
|
|
|
|
func (s *stubStore) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) {
|
|
if s.eventsSinceErr != nil {
|
|
return nil, s.eventsSinceErr
|
|
}
|
|
return s.TaskStore.EventsSince(ctx, cursor, limit)
|
|
}
|
|
|
|
func (s *stubStore) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
|
if s.taskEventsErr != nil {
|
|
return nil, s.taskEventsErr
|
|
}
|
|
return s.TaskStore.TaskEvents(ctx, taskID)
|
|
}
|
|
|
|
func TestHandleSummary(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx := t.Context()
|
|
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/summary", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
|
}
|
|
var got map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, key := range []string{"statuses", "workflows", "stages", "tokens_by_day"} {
|
|
if _, ok := got[key]; !ok {
|
|
t.Errorf("summary missing key %q: %+v", key, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestHandleSummaryWorkflowStatsError proves that handleSummary does not
|
|
// silently discard an error from WorkflowStats. Today the handler uses
|
|
// blank-identifier assignment (workflows, _ := ...), so this test FAILS
|
|
// (wants 500, gets 200 with nil workflows).
|
|
func TestHandleSummaryWorkflowStatsError(t *testing.T) {
|
|
base := newTestServer(t)
|
|
ctx := t.Context()
|
|
srv := &Server{
|
|
Store: &stubStore{
|
|
TaskStore: base.Store,
|
|
workflowStatsErr: fmt.Errorf("db locked"),
|
|
},
|
|
Log: base.Log,
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/summary", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status = %d, want 500 — WorkflowStats error was silently discarded", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestHandleSummaryStageStatsError proves that handleSummary does not
|
|
// silently discard an error from StageStats.
|
|
func TestHandleSummaryStageStatsError(t *testing.T) {
|
|
base := newTestServer(t)
|
|
ctx := t.Context()
|
|
srv := &Server{
|
|
Store: &stubStore{
|
|
TaskStore: base.Store,
|
|
stageStatsErr: fmt.Errorf("db locked"),
|
|
},
|
|
Log: base.Log,
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/summary", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status = %d, want 500 — StageStats error was silently discarded", w.Code)
|
|
}
|
|
}
|
|
|
|
// TestHandleSummaryTokensByDayError proves that handleSummary does not
|
|
// silently discard an error from TokensByDay.
|
|
func TestHandleSummaryTokensByDayError(t *testing.T) {
|
|
base := newTestServer(t)
|
|
ctx := t.Context()
|
|
srv := &Server{
|
|
Store: &stubStore{
|
|
TaskStore: base.Store,
|
|
tokensByDayErr: fmt.Errorf("db locked"),
|
|
},
|
|
Log: base.Log,
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/summary", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status = %d, want 500 — TokensByDay error was silently discarded", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleTasks(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx := t.Context()
|
|
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "hello", "b", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tasks", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
|
}
|
|
var got []workflow.Task
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].Title != "hello" {
|
|
t.Fatalf("tasks = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleTask(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx := t.Context()
|
|
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
|
|
if err != nil || task == nil {
|
|
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
|
|
}
|
|
if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: task.ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tasks/"+strconv.FormatInt(task.ID, 10), nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
|
}
|
|
var got []events.Event
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].Kind != "stage_start" {
|
|
t.Fatalf("task events = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleTaskBadID(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx := t.Context()
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tasks/not-a-number", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleIndex(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx := t.Context()
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
|
|
t.Fatalf("content-type = %q", ct)
|
|
}
|
|
}
|
|
|
|
// TestHealthzIsReachableWithoutAToken: the update watchdog script polls
|
|
// this endpoint from the local host after restarting archied, with no
|
|
// dashboard session -- it must never require the dashboard token, unlike
|
|
// every other route.
|
|
func TestHealthzIsReachableWithoutAToken(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
srv.Token = "dashboard-secret"
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/healthz", nil)
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body = %q", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestHandleSSEEventsSinceError proves that handleSSE does not silently
|
|
// discard an error from EventsSince. Today the handler has no else
|
|
// branch for err != nil, so the error is swallowed and the client
|
|
// receives no signal that catch-up was skipped: this test FAILS.
|
|
func TestHandleSSEEventsSinceError(t *testing.T) {
|
|
actual := newTestServer(t)
|
|
srv := &Server{
|
|
Store: &stubStore{
|
|
TaskStore: actual.Store,
|
|
eventsSinceErr: fmt.Errorf("db locked"),
|
|
},
|
|
Log: actual.Log,
|
|
}
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
t.Cleanup(ts.Close)
|
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream?since=0", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("EventsSince error silently swallowed — SSE handler never responded: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d", resp.StatusCode)
|
|
}
|
|
|
|
reader := bufio.NewReader(resp.Body)
|
|
// The SSE stream must surface the EventsSince error to the client.
|
|
// Today the error is silently swallowed, so no error line is ever
|
|
// sent and the test FAILS.
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
break
|
|
}
|
|
if strings.Contains(line, "error") {
|
|
return // PASS: error was surfaced to the client
|
|
}
|
|
}
|
|
t.Fatal("EventsSince error was silently swallowed — no error sent to SSE client")
|
|
}
|
|
|
|
func TestHandleSSEUsesLastEventIDForCatchUp(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
|
defer cancel()
|
|
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
first, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "first", At: at})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "second", At: at}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
t.Cleanup(ts.Close)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Last-Event-ID", storecontract.EventCursor(at, first))
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = resp.Body.Close() })
|
|
|
|
reader := bufio.NewReader(resp.Body)
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
line, readErr := reader.ReadString('\n')
|
|
if readErr != nil {
|
|
t.Fatalf("second event not received after Last-Event-ID: %v", readErr)
|
|
}
|
|
if strings.HasPrefix(line, "data:") {
|
|
if strings.Contains(line, "first") {
|
|
t.Fatalf("replayed event before Last-Event-ID: %q", line)
|
|
}
|
|
if strings.Contains(line, "second") {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.Fatal("second event not received after Last-Event-ID")
|
|
}
|
|
|
|
func TestHandleSSEBacklogAndLive(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
|
defer cancel()
|
|
t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog", At: t0})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
t.Cleanup(ts.Close)
|
|
|
|
// The request carries t.Context() so the SSE stream is torn down when
|
|
// the test ends rather than leaking the connection and its goroutine.
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = resp.Body.Close() })
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d", resp.StatusCode)
|
|
}
|
|
|
|
reader := bufio.NewReader(resp.Body)
|
|
line, err := readLineUntil(reader, "backlog")
|
|
if err != nil {
|
|
t.Fatalf("backlog event not received: %v (%q)", err, line)
|
|
}
|
|
|
|
// Now broadcast a live event and confirm it's delivered too. Give
|
|
// the SSE handler a moment to register its subscriber.
|
|
time.Sleep(50 * time.Millisecond)
|
|
srv.Broadcast(events.Event{ID: id + 1, Kind: "log", Detail: "live", At: t0.Add(time.Second)})
|
|
|
|
if _, err := readLineUntil(reader, "live"); err != nil {
|
|
t.Fatalf("live event not received: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestHandleSSEFiltersTurnCompletedFromLiveBroadcast covers the live half of
|
|
// the archie-core-521 fix: sendPage's filtering is exercised directly in
|
|
// sse_test.go, but the live broadcast path (drain) has its own send call.
|
|
// A turn_completed broadcast between two ordinary events must never reach
|
|
// the client, while the ordinary events either side of it still do.
|
|
func TestHandleSSEFiltersTurnCompletedFromLiveBroadcast(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
|
defer cancel()
|
|
t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog", At: t0})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ts := httptest.NewServer(srv.Handler())
|
|
t.Cleanup(ts.Close)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = resp.Body.Close() })
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d", resp.StatusCode)
|
|
}
|
|
|
|
reader := bufio.NewReader(resp.Body)
|
|
if _, err := readLineUntil(reader, "backlog"); err != nil {
|
|
t.Fatalf("backlog event not received: %v", err)
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
srv.Broadcast(events.Event{ID: id + 1, Kind: events.KindTurnCompleted, Detail: "100000000", At: t0.Add(time.Second)})
|
|
srv.Broadcast(events.Event{ID: id + 2, Kind: "log", Detail: "after-noise", At: t0.Add(2 * time.Second)})
|
|
|
|
seen, err := readLinesUntil(reader, "after-noise")
|
|
if err != nil {
|
|
t.Fatalf("event after the filtered one was not received: %v", err)
|
|
}
|
|
for _, line := range seen {
|
|
if strings.Contains(line, "turn_completed") {
|
|
t.Fatalf("the filtered event's frame reached the client: %q", line)
|
|
}
|
|
}
|
|
}
|
|
|
|
// readLineUntil scans SSE "data:" lines until one contains want.
|
|
func readLineUntil(r *bufio.Reader, want string) (string, error) {
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
line, err := r.ReadString('\n')
|
|
if err != nil {
|
|
return line, err
|
|
}
|
|
if strings.HasPrefix(line, "data:") && strings.Contains(line, want) {
|
|
return line, nil
|
|
}
|
|
}
|
|
return "", io.EOF
|
|
}
|
|
|
|
// readLinesUntil scans SSE "data:" lines, collecting every one seen, until
|
|
// one contains want -- unlike readLineUntil, callers get the full scan, not
|
|
// just the matched line, so a frame that arrived before the match but was
|
|
// never asserted on can't hide there.
|
|
func readLinesUntil(r *bufio.Reader, want string) ([]string, error) {
|
|
var seen []string
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
line, err := r.ReadString('\n')
|
|
if err != nil {
|
|
return seen, err
|
|
}
|
|
if !strings.HasPrefix(line, "data:") {
|
|
continue
|
|
}
|
|
seen = append(seen, line)
|
|
if strings.Contains(line, want) {
|
|
return seen, nil
|
|
}
|
|
}
|
|
return seen, io.EOF
|
|
}
|