Merge commit '4ec30f3d40a63fb90879f26a7cf15e331a074d7a' into pipelines/run-20261112-0142-notify-on-run-failures-and-from-the-noti-519b

This commit is contained in:
2026-11-12 02:52:52 +00:00
125 changed files with 4795 additions and 811 deletions
+29 -9
View File
@@ -39,25 +39,37 @@ type appNotifier struct {
}
type appNotificationSub struct {
queue chan protocol.Message
done chan struct{}
once sync.Once
ctx context.Context
requestID string
started time.Time
queue chan protocol.Message
done chan struct{}
once sync.Once
ended sync.Once
}
func newAppNotifier(store appNotificationStore) *appNotifier {
return &appNotifier{store: store, now: time.Now, subs: map[string]*appNotificationSub{}}
}
func (s *appNotificationSub) stop() { s.once.Do(func() { close(s.done) }) }
func (s *appNotificationSub) stop(reason string) {
s.once.Do(func() { close(s.done) })
s.ended.Do(func() {
log.Printf("notification subscription ended request_id=%q duration=%s reason=%s", s.requestID, time.Since(s.started), reason)
})
}
func (s *appNotificationSub) run(write func(protocol.Message) error) {
for {
select {
case <-s.done:
return
case <-s.ctx.Done():
s.stop("server stopping")
return
case msg := <-s.queue:
if write(msg) != nil {
s.stop()
s.stop("write error")
return
}
}
@@ -77,7 +89,7 @@ func (a *appNotifier) enqueueLocked(msg protocol.Message, exclude string) {
delete(a.subs, id)
default:
log.Printf("app notifications: dropping slow subscriber %s", id)
sub.stop()
sub.stop("write error")
delete(a.subs, id)
}
}
@@ -93,11 +105,15 @@ func (a *appNotifier) subscribe(ctx context.Context, clientID, requestID, after
return err
}
if old := a.subs[clientID]; old != nil {
old.stop()
old.stop("client closed")
}
sub := &appNotificationSub{
ctx: ctx, requestID: requestID, started: time.Now(),
queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{}),
}
sub := &appNotificationSub{queue: make(chan protocol.Message, appNotificationQueue), done: make(chan struct{})}
sub.queue <- protocol.Message{Type: protocol.NotificationsSubscribed, ID: requestID, Notifications: active, IDs: cleared}
a.subs[clientID] = sub
log.Printf("notification subscription started request_id=%q cursor=%q", requestID, after)
go sub.run(write)
return nil
}
@@ -106,7 +122,11 @@ func (a *appNotifier) detach(clientID string) {
a.mu.Lock()
defer a.mu.Unlock()
if sub := a.subs[clientID]; sub != nil {
sub.stop()
reason := "client closed"
if sub.ctx.Err() != nil {
reason = "server stopping"
}
sub.stop(reason)
delete(a.subs, clientID)
}
}
+27
View File
@@ -1,10 +1,13 @@
package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http/httptest"
"strings"
"testing"
"time"
@@ -17,6 +20,30 @@ import (
"git.a-wan.net/alan/pipelines/internal/summarise"
)
func TestAppNotificationSubscriptionLogsLifecycle(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
f := newAppNotificationFixture(t, ctx)
var output bytes.Buffer
previous := log.Writer()
log.SetOutput(&output)
t.Cleanup(func() { log.SetOutput(previous) })
phone := dialAckClient(t, ctx, f.url)
subscribeNotifications(t, ctx, phone, "cursor-1")
if !strings.Contains(output.String(), `notification subscription started request_id="sub" cursor="cursor-1"`) {
t.Fatalf("subscription start log = %q", output.String())
}
phone.CloseNow()
deadline := time.Now().Add(2 * time.Second)
for !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if !strings.Contains(output.String(), `notification subscription ended request_id="sub"`) || !strings.Contains(output.String(), "reason=client closed") {
t.Fatalf("subscription end log = %q", output.String())
}
}
// appNotificationFixture is a handler over a temp store with one pipeline,
// plus helpers to make a run end with a durable notification intent.
type appNotificationFixture struct {
+11 -12
View File
@@ -105,8 +105,8 @@ func TestStageNotificationEventsAndArtifactAudit(t *testing.T) {
}
}
// Regress the terminal-path recipe from the authoring guide (research -> report
// there) on the bundled two-stage example's draft -> check.
// Regress the terminal-path recipe from the authoring guide on the bundled
// research -> report starter.
// A completion-alert request must not require native delivery or change run routing.
func TestTerminalNotificationRecipe(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
@@ -118,15 +118,14 @@ func TestTerminalNotificationRecipe(t *testing.T) {
defer store.Close()
var source string
for _, template := range examples.Catalog() {
if template.Key == "two-stages" {
if template.Key == "research" {
source = template.Source
}
}
if source == "" {
t.Fatal("two-stages template missing")
t.Fatal("research template missing")
}
source = strings.Replace(source, "# stage draft", "# stage draft\n```notify\non=failure\n```", 1)
source = strings.Replace(source, "# stage check", "# stage check\n```notify\non=success,failure\n```", 1)
source = strings.Replace(source, "# stage report", "# stage report\n```notify\non=success,failure\n```", 1)
if _, err = store.Submit(ctx, "research notification recipe", source); err != nil {
t.Fatal(err)
}
@@ -160,12 +159,12 @@ func TestTerminalNotificationRecipe(t *testing.T) {
name, stage, outcome, status string
executionError, deliveryError bool
}{
{"success", "check", "success", "succeeded", false, false},
{"draft blocked", "draft", "blocked", "failed", false, false},
{"draft execution error", "draft", "error", "failed", true, false},
{"check blocked", "check", "blocked", "failed", false, false},
{"check execution error", "check", "error", "failed", true, false},
{"delivery error preserves success", "check", "success", "succeeded", false, true},
{"success", "report", "success", "succeeded", false, false},
{"research blocked", "research", "blocked", "failed", false, false},
{"research execution error", "research", "error", "failed", true, false},
{"report blocked", "report", "blocked", "failed", false, false},
{"report execution error", "report", "error", "failed", true, false},
{"delivery error preserves success", "report", "success", "succeeded", false, true},
} {
t.Run(test.name, func(t *testing.T) {
deliveryFailure.Store(test.deliveryError)