Merge branch 'main' into resource-pool-operator-config

This commit is contained in:
2026-10-18 23:37:50 +01:00
6 changed files with 287 additions and 12 deletions
+4 -2
View File
@@ -2,8 +2,10 @@
Stage `resources` declarations now accept exactly one of `exclusive` or `pool`.
Pool stages wait on the named pool, then receive the selected concrete member and
bounded metadata through explicit runtime context. The model input remains
unchanged and capability tokens remain private to the store. Retained control
bounded metadata through explicit runtime context. Model execution serializes that
server-granted context as a separate `resource_target` runtime field for both
native and plain harnesses; the task input map remains unchanged and cannot select
the target. Capability tokens remain private to the store. Retained control
evidence records the concrete target; queued run state names the pool while an
active owner names the selected member.
@@ -0,0 +1,7 @@
# Committed and merged stage diffs remain retained
The server captures workspace contents around each stage visit rather than comparing
Git status or HEAD. Regression coverage now exercises a stage that commits its edit,
a later stage that resolves and commits a merge conflict, and an empty commit-only
stage. After reopening SQLite, the first two patches remain available while the final
stage correctly records no source-content change.
+152
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
@@ -162,3 +163,154 @@ func TestRepeatedStageVisitsRetainIndependentDiffs(t *testing.T) {
}
}
}
func TestRetainedStageDiffsSurviveCommittedMergeAndNoopVisit(t *testing.T) {
ctx := context.Background()
db := filepath.Join(t.TempDir(), "committed-diffs.db")
store, err := pipeline.Open(db)
if err != nil {
t.Fatal(err)
}
source := pipeline.Template([]string{"commit", "merge", "noop"}, true)
if _, err := store.Submit(ctx, "committed-diffs", source); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
workspace := t.TempDir()
runGit(t, workspace, "init", "-q")
runGit(t, workspace, "config", "user.email", "test@example.invalid")
runGit(t, workspace, "config", "user.name", "test")
path := filepath.Join(workspace, "source.txt")
if err := os.WriteFile(path, []byte("base\n"), 0600); err != nil {
t.Fatal(err)
}
runGit(t, workspace, "add", "source.txt")
runGit(t, workspace, "commit", "-qm", "base")
branch := runGitOutput(t, workspace, "branch", "--show-current")
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
git := func(args ...string) error {
cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git %v: %w: %s", args, err, output)
}
return nil
}
gitOutput := func(args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...)
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("git %v: %w", args, err)
}
return strings.TrimSpace(string(output)), nil
}
r.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
switch def.Key {
case "commit":
if err := os.WriteFile(path, []byte("committed change\n"), 0600); err != nil {
return stage.Result{}, err
}
if err := git("add", "source.txt"); err != nil {
return stage.Result{}, err
}
if err := git("commit", "-qm", "stage commit"); err != nil {
return stage.Result{}, err
}
case "merge":
if err := git("checkout", "-qb", "conflict-side"); err != nil {
return stage.Result{}, err
}
if err := os.WriteFile(path, []byte("side change\n"), 0600); err != nil {
return stage.Result{}, err
}
if err := git("add", "source.txt"); err != nil {
return stage.Result{}, err
}
if err := git("commit", "-qm", "side"); err != nil {
return stage.Result{}, err
}
if err := git("checkout", "-q", branch); err != nil {
return stage.Result{}, err
}
if err := os.WriteFile(path, []byte("main change\n"), 0600); err != nil {
return stage.Result{}, err
}
if err := git("add", "source.txt"); err != nil {
return stage.Result{}, err
}
if err := git("commit", "-qm", "main"); err != nil {
return stage.Result{}, err
}
if err := git("merge", "--no-ff", "conflict-side", "-m", "merge"); err == nil {
return stage.Result{}, errors.New("expected merge conflict")
}
conflicted, err := gitOutput("diff", "--name-only", "--diff-filter=U")
if err != nil || conflicted != "source.txt" {
return stage.Result{}, fmt.Errorf("merge did not leave the expected conflict: %q, %v", conflicted, err)
}
if err := os.WriteFile(path, []byte("resolved merge\n"), 0600); err != nil {
return stage.Result{}, err
}
if err := git("add", "source.txt"); err != nil {
return stage.Result{}, err
}
if err := git("commit", "-qm", "resolve merge"); err != nil {
return stage.Result{}, err
}
case "noop":
if err := git("commit", "--allow-empty", "-qm", "no content change"); err != nil {
return stage.Result{}, err
}
}
return stage.Result{Outcome: "success"}, nil
}
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "committed-diffs", Workspace: workspace, Input: map[string]any{"prompt": "task"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = pipeline.Open(db)
if err != nil {
t.Fatal(err)
}
defer store.Close()
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "succeeded" || len(detail.Steps) != 3 {
t.Fatal(detail)
}
for _, assertion := range []struct {
step int
old string
new string
}{
{step: 0, old: "base", new: "committed change"},
{step: 1, old: "committed change", new: "resolved merge"},
} {
changes := detail.Steps[assertion.step].FileChanges
if changes == nil || len(changes.Files) != 1 || !strings.Contains(changes.Files[0].Patch, "-"+assertion.old) || !strings.Contains(changes.Files[0].Patch, "+"+assertion.new) {
t.Fatalf("committed stage patch was not retained after reopen: %+v", changes)
}
}
if changes := detail.Steps[2].FileChanges; changes == nil || len(changes.Files) != 0 {
t.Fatalf("commit-only stage changed source evidence: %+v", changes)
}
data, err := os.ReadFile(path)
if err != nil || string(data) != "resolved merge\n" {
t.Fatalf("merge resolution was not preserved: %q, %v", data, err)
}
}
+19 -9
View File
@@ -8,6 +8,8 @@ import (
"sort"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
var ErrInvalidModelResult = errors.New("invalid model stage result")
@@ -36,6 +38,13 @@ func harnessUnreachable(err error) error {
}
func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) {
return modelPromptForTarget(def, spec, input, nil)
}
// modelPromptForTarget carries a server-selected resource target separately from
// task input. The target comes only from the runner context; task input remains
// data and cannot select or replace it.
func modelPromptForTarget(def Definition, spec ModelSpec, input Values, target *protocol.ResourceTarget) (string, error) {
outcomes := make([]string, 0, len(def.Edges))
for name := range def.Edges {
outcomes = append(outcomes, name)
@@ -59,15 +68,16 @@ func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) {
toolsLine = "This call has these stage tools: " + strings.Join(tools, ", ") + "."
}
data, err := json.Marshal(struct {
Key string `json:"stage"`
Instructions string `json:"instructions"`
OutputContract string `json:"output_contract"`
Outcomes []string `json:"allowed_outcomes"`
Input Values `json:"input"`
ReportFileArtifacts bool `json:"report_file_artifacts"`
ExecutionTime string `json:"execution_time"`
Timezone string `json:"timezone"`
}{def.Key, spec.Prompt, spec.Output, outcomes, input, def.ReportFileArtifacts, time.Now().Format(time.RFC3339), time.Local.String()})
Key string `json:"stage"`
Instructions string `json:"instructions"`
OutputContract string `json:"output_contract"`
Outcomes []string `json:"allowed_outcomes"`
Input Values `json:"input"`
ResourceTarget *protocol.ResourceTarget `json:"resource_target,omitempty"`
ReportFileArtifacts bool `json:"report_file_artifacts"`
ExecutionTime string `json:"execution_time"`
Timezone string `json:"timezone"`
}{def.Key, spec.Prompt, spec.Output, outcomes, input, target, def.ReportFileArtifacts, time.Now().Format(time.RFC3339), time.Local.String()})
if err != nil {
return "", fmt.Errorf("model stage input is not JSON-serializable: %w", err)
}
+2 -1
View File
@@ -16,7 +16,8 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
return Result{}, err
}
spec.Config.ResultSchema = resultSchema(def.Edges)
prompt, err := modelPrompt(def, spec, input)
target, _ := ResourceTarget(ctx)
prompt, err := modelPromptForTarget(def, spec, input, target)
if err != nil {
return Result{}, err
}
+103
View File
@@ -9,6 +9,8 @@ import (
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func modelDefinition() Definition {
@@ -194,6 +196,107 @@ func TestModelRunUsesInstructionsAndStructuredOutput(t *testing.T) {
}
}
func TestModelRunForwardsOnlyServerGrantedResourceTarget(t *testing.T) {
target := &protocol.ResourceTarget{Member: "emulator", Metadata: map[string]string{"serial": "emu-5554", "type": "emulator"}}
budget := NewBudget(0, 0)
input := Values{"resource_target": map[string]any{"member": "user-selected"}, "prompt": "task"}
ctx := WithBudget(WithResourceTarget(context.Background(), target), budget)
var received ModelConfig
client := generatorFunc(func(_ context.Context, config ModelConfig, prompt string) (string, error) {
received = config
assertPromptResourceTarget(t, prompt, target, input)
return `{"outcome":"success","output":{}}`, nil
})
if _, err := (ModelStage{Client: client}).Run(ctx, modelDefinition(), input); err != nil {
t.Fatal(err)
}
if received.Budget != budget {
t.Fatal("budget context was not preserved with resource target")
}
}
func TestModelRunForwardsResourceTargetToNativeOpenCode(t *testing.T) {
target := &protocol.ResourceTarget{Member: "phone", Metadata: map[string]string{"serial": "phone-serial"}}
var prompt string
client := &OpenCodeClient{Directory: "/work/repo", command: func(_ context.Context, _ string, args ...string) ([]byte, error) {
switch args[2] {
case "/api/session":
return []byte(`{"data":{"id":"ses_target"}}`), nil
case "/api/session/ses_target/generate":
var body struct {
Prompt string `json:"prompt"`
}
if err := json.Unmarshal([]byte(args[4]), &body); err != nil {
t.Fatal(err)
}
prompt = body.Prompt
return []byte(`{"data":{"text":"{\"outcome\":\"success\",\"output\":{}}"}}`), nil
default:
t.Fatalf("unexpected fake OpenCode endpoint %q", args[2])
return nil, nil
}
}}
if _, err := (ModelStage{Client: client}).Run(WithResourceTarget(context.Background(), target), modelDefinition(), Values{"prompt": "task"}); err != nil {
t.Fatal(err)
}
assertPromptResourceTarget(t, prompt, target, Values{"prompt": "task"})
}
func TestModelPromptWithoutResourceTargetPreservesLegacyInput(t *testing.T) {
input := Values{"prompt": "task", "count": "1"}
spec, err := (ModelStage{}).Spec(modelDefinition())
if err != nil {
t.Fatal(err)
}
prompt, err := modelPrompt(modelDefinition(), spec, input)
if err != nil {
t.Fatal(err)
}
var payload struct {
Input Values `json:"input"`
ResourceTarget *protocol.ResourceTarget `json:"resource_target"`
}
if _, data, ok := strings.Cut(prompt, "Stage definition and runtime input:\n"); !ok || json.Unmarshal([]byte(data), &payload) != nil {
t.Fatal("could not decode legacy runtime payload")
}
if payload.ResourceTarget != nil || !reflect.DeepEqual(payload.Input, input) {
t.Fatalf("legacy runtime input changed: %#v", payload)
}
}
func assertPromptResourceTarget(t *testing.T, prompt string, want *protocol.ResourceTarget, wantInput Values) {
t.Helper()
_, data, ok := strings.Cut(prompt, "Stage definition and runtime input:\n")
if !ok {
t.Fatal("missing runtime payload")
}
var payload struct {
Input Values `json:"input"`
ResourceTarget *protocol.ResourceTarget `json:"resource_target"`
}
if err := json.Unmarshal([]byte(data), &payload); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(payload.ResourceTarget, want) {
t.Fatalf("resource target = %#v, want %#v", payload.ResourceTarget, want)
}
var fields map[string]json.RawMessage
if err := json.Unmarshal([]byte(data), &fields); err != nil {
t.Fatal(err)
}
var targetFields map[string]json.RawMessage
if err := json.Unmarshal(fields["resource_target"], &targetFields); err != nil {
t.Fatal(err)
}
if len(targetFields) != 2 || targetFields["member"] == nil || targetFields["metadata"] == nil {
t.Fatalf("resource target leaked non-target fields: %s", fields["resource_target"])
}
if !reflect.DeepEqual(payload.Input, wantInput) {
t.Fatalf("input = %#v, want %#v", payload.Input, wantInput)
}
}
func TestModelResultValidationAndRetryBudget(t *testing.T) {
for _, text := range []string{
`not json`, `{"outcome":"missing","output":{}}`, `{"outcome":"success"}`,