9130ae8c7a
Add durable FIFO capacity-one resource leases for executable stages, including cleanup-confirmed release, conservative restart quarantine, queue state events, and CLI/TUI/Android waiting presentation. Add scheduler, cleanup, protocol, client, and Android coverage plus a proposed Android v3 resource draft. Checked packaged templates and authoring skill references: neither needs updating in this item. The per-item docs note records the shared documentation that docs-fold must update; no shared README or packaged reference was edited.
155 lines
5.6 KiB
Go
155 lines
5.6 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/cli"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
)
|
|
|
|
// This is a real CLI/WebSocket/scratch-SQLite exercise with an explicitly FAKE
|
|
// executor. It never calls inference, a phone, or a deployed server.
|
|
func TestResourceScratchCLIWaitingThenProgress(t *testing.T) {
|
|
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "scratch.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
source := pipeline.Template([]string{"work"}, true) + "```resources\n{\"exclusive\":\"fake-phone\"}\n```\n"
|
|
if _, err := store.Submit(ctx, "scratch-resource", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := Handler(ctx, store).(*runHandler)
|
|
started, release := make(chan struct{}), make(chan struct{})
|
|
handler.runner.execute = func(ctx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
|
|
if input["prompt"] == "A" {
|
|
close(started)
|
|
select {
|
|
case <-release:
|
|
case <-ctx.Done():
|
|
return stage.Result{}, ctx.Err()
|
|
}
|
|
}
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"execution": "FAKE executor; no device or inference"}}, nil
|
|
}
|
|
handler.runner.events.interval = time.Millisecond
|
|
events := make(chan protocol.RunEventFrame, 64)
|
|
sub := handler.runner.events.subscribe("cli-evidence", "", nil, func(m protocol.Message) error {
|
|
if m.RunEvent != nil {
|
|
events <- *m.RunEvent
|
|
}
|
|
return nil
|
|
})
|
|
sub.ready()
|
|
defer sub.close()
|
|
srv := httptest.NewServer(handler)
|
|
defer srv.Close()
|
|
defer func() { cancel(); WaitRuns(handler) }()
|
|
endpoint := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
|
|
run := func(args ...string) string {
|
|
var out bytes.Buffer
|
|
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
|
|
if err := app.Run(ctx, append([]string{"--url", endpoint}, args...)); err != nil {
|
|
t.Fatalf("CLI %v: %v\n%s", args, err, out.String())
|
|
}
|
|
t.Logf("pipelines %s\n%s", strings.Join(args, " "), out.String())
|
|
return out.String()
|
|
}
|
|
parseRun := func(output string) int64 {
|
|
var m protocol.Message
|
|
if err := json.Unmarshal([]byte(output), &m); err != nil || m.RunDetail == nil {
|
|
t.Fatal("CLI run response", output, err)
|
|
}
|
|
return m.RunDetail.ID
|
|
}
|
|
run("validate", "testdata/android-resource-draft.md", "--json")
|
|
a := parseRun(run("run", "scratch-resource", "A", "--workspace", t.TempDir(), "--yes", "--json", "--request-key", "a"))
|
|
<-started
|
|
b := parseRun(run("run", "scratch-resource", "B", "--workspace", t.TempDir(), "--yes", "--json", "--request-key", "b"))
|
|
awaitResourceEvent(t, events, b, func(e protocol.RunEventFrame) bool { return e.ResourceWait != nil })
|
|
var detail protocol.RunDetail
|
|
if err := json.Unmarshal([]byte(run("inspect", fmt.Sprint(b), "--json")), &detail); err != nil || detail.ResourceWait == nil || detail.Status != "queued" || len(detail.Steps) != 0 {
|
|
t.Fatal("CLI did not observe queue", err)
|
|
}
|
|
if output := run("runs"); !strings.Contains(output, "Waiting for resource fake-phone") {
|
|
t.Fatal("list omitted resource status", output)
|
|
}
|
|
var snapshot *protocol.RunEventFrame
|
|
handleRunEvents(protocol.Message{Type: protocol.Subscribe, RunIDs: []int64{b}}, handler.runner.events, handler.runner, "snapshot-evidence", func(m protocol.Message) error {
|
|
if m.RunEvent != nil {
|
|
snapshot = m.RunEvent
|
|
}
|
|
return nil
|
|
})
|
|
handler.runner.events.unsubscribeClient("snapshot-evidence")
|
|
if snapshot == nil || snapshot.ResourceWait == nil || !snapshot.ResourceWaitSet {
|
|
t.Fatal("subscription snapshot omitted resource wait")
|
|
}
|
|
close(release)
|
|
run("wait", fmt.Sprint(a), fmt.Sprint(b), "--json")
|
|
detail = protocol.RunDetail{}
|
|
if err := json.Unmarshal([]byte(run("inspect", fmt.Sprint(b), "--json")), &detail); err != nil || detail.Status != "succeeded" || detail.ResourceWait != nil || len(detail.Steps) != 1 {
|
|
t.Fatal("automatic progress not observed", err)
|
|
}
|
|
t.Log("Observed queued resource wait followed by automatic progress; FAKE executor only, no phone/inference/deployment.")
|
|
}
|
|
|
|
func TestResourceAndroidDraftPublishedV3Provenance(t *testing.T) {
|
|
baseline, err := os.ReadFile("testdata/android-v3-published.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hash := fmt.Sprintf("%x", sha256.Sum256(baseline)); hash != "4a3a4b8b947eb20c115c5470e6e378f117d5fa291a7b6c8ae966a3745bc826de" {
|
|
t.Fatal("baseline is not retrieved Android v3", hash)
|
|
}
|
|
draft, err := os.ReadFile("testdata/android-resource-draft.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, err := pipeline.Parse(string(baseline))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
updated, err := pipeline.Parse(string(draft))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(old.Stages) != len(updated.Stages) {
|
|
t.Fatal("draft changed graph shape")
|
|
}
|
|
for i := range old.Stages {
|
|
a, b := old.Stages[i], updated.Stages[i]
|
|
if b.Configs["provider"] != "openai" || b.Configs["model"] != a.Configs["model"] {
|
|
t.Fatal("draft changed approved model")
|
|
}
|
|
if b.Key == "ui_reviewer" {
|
|
if b.Resources == nil || b.Resources.Exclusive != "phone-47241FDAS00DLK" {
|
|
t.Fatal("reviewer resource missing")
|
|
}
|
|
} else if b.Resources != nil {
|
|
t.Fatal("non-UI stage acquired phone")
|
|
}
|
|
a.Prompt, b.Prompt = "", ""
|
|
a.Resources, b.Resources = nil, nil
|
|
if !reflect.DeepEqual(a, b) {
|
|
t.Fatal("draft changed fields outside approved role split/resource", a.Key)
|
|
}
|
|
}
|
|
}
|