From 31d78cc065317e4b98d0cbff422d040538198862 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:33:30 +0100 Subject: [PATCH] stage: expose resource targets to model harnesses --- docs/notes/resource-pool-runner-handoff.md | 6 +- internal/stage/model_prompt.go | 28 ++++-- internal/stage/model_run.go | 3 +- internal/stage/model_test.go | 103 +++++++++++++++++++++ 4 files changed, 128 insertions(+), 12 deletions(-) diff --git a/docs/notes/resource-pool-runner-handoff.md b/docs/notes/resource-pool-runner-handoff.md index d7c9d091..d92d1e47 100644 --- a/docs/notes/resource-pool-runner-handoff.md +++ b/docs/notes/resource-pool-runner-handoff.md @@ -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. diff --git a/internal/stage/model_prompt.go b/internal/stage/model_prompt.go index f6cc3db2..2a0f9f7f 100644 --- a/internal/stage/model_prompt.go +++ b/internal/stage/model_prompt.go @@ -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) } diff --git a/internal/stage/model_run.go b/internal/stage/model_run.go index 41300243..2bc5ee34 100644 --- a/internal/stage/model_run.go +++ b/internal/stage/model_run.go @@ -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 } diff --git a/internal/stage/model_test.go b/internal/stage/model_test.go index 0cd1ac04..a0070755 100644 --- a/internal/stage/model_test.go +++ b/internal/stage/model_test.go @@ -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"}`,