Wire pool resource targets through runner and repair quarantined-member handoff
This commit is contained in:
@@ -11,7 +11,19 @@ import (
|
||||
)
|
||||
|
||||
func (r *runner) acquireResource(ctx context.Context, visit pipeline.ResourceVisit, workspace string, completed int) (pipeline.ResourceLease, error) {
|
||||
lease, ready, wait, err := r.store.RequestResource(ctx, visit)
|
||||
return r.acquireResourceWithPool(ctx, visit, "", workspace, completed)
|
||||
}
|
||||
|
||||
func (r *runner) acquireResourceWithPool(ctx context.Context, visit pipeline.ResourceVisit, pool string, workspace string, completed int) (pipeline.ResourceLease, error) {
|
||||
var lease pipeline.ResourceLease
|
||||
var ready <-chan pipeline.ResourceLease
|
||||
var wait *protocol.ResourceWait
|
||||
var err error
|
||||
if pool != "" {
|
||||
lease, ready, wait, err = r.store.RequestResourcePool(ctx, pool, visit)
|
||||
} else {
|
||||
lease, ready, wait, err = r.store.RequestResource(ctx, visit)
|
||||
}
|
||||
if err != nil {
|
||||
return lease, err
|
||||
}
|
||||
|
||||
@@ -137,6 +137,97 @@ func TestResourcePauseBeyondTimeoutAndAutomaticResume(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcePoolRunnerHandoffUsesConcreteTargetAfterQueue(t *testing.T) {
|
||||
r, events, _ := resourceRunner(t)
|
||||
ctx := context.Background()
|
||||
source := pipeline.Template([]string{"work"}, true) + "```resources\n{\"pool\":\"targets\"}\n```\n"
|
||||
if _, err := r.store.Submit(ctx, "pool-runner", source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := r.store.List(ctx)
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatal("pool definition missing", err)
|
||||
}
|
||||
definition, err := r.store.Get(ctx, items[0].ID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.store.ConfigureResourcePool(ctx, protocol.ResourcePool{Name: "targets", Members: []protocol.ResourcePoolMember{
|
||||
{Member: "phone", Enabled: true, Metadata: map[string]string{"serial": "phone-serial", "type": "phone"}},
|
||||
{Member: "emulator", Enabled: true, Metadata: map[string]string{"serial": "emu-serial", "type": "emulator"}},
|
||||
}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ownerRun, _, err := r.store.CreateRun(ctx, definition, "phone-owner", t.TempDir(), false, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
phone, ch, _, err := r.store.RequestResource(ctx, pipeline.ResourceVisit{Resource: "phone", RunID: ownerRun, Stage: "work", Sequence: 1})
|
||||
if err != nil || ch != nil {
|
||||
t.Fatalf("phone owner acquire: channel=%v err=%v", ch, err)
|
||||
}
|
||||
defer r.releaseResource(phone, true)
|
||||
|
||||
aStarted, releaseA := make(chan struct{}), make(chan struct{})
|
||||
bRan := make(chan *protocol.ResourceTarget, 1)
|
||||
r.execute = func(stageCtx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) {
|
||||
target, ok := stage.ResourceTarget(stageCtx)
|
||||
if !ok || target == nil {
|
||||
return stage.Result{}, errors.New("missing resource target context")
|
||||
}
|
||||
if input["prompt"] == "A" {
|
||||
if target.Member != "emulator" || target.Metadata["serial"] != "emu-serial" || target.Metadata["type"] != "emulator" {
|
||||
return stage.Result{}, fmt.Errorf("wrong first target: %#v", target)
|
||||
}
|
||||
close(aStarted)
|
||||
<-releaseA
|
||||
} else {
|
||||
if deadline, ok := stageCtx.Deadline(); !ok || time.Until(deadline) < 15*time.Millisecond {
|
||||
return stage.Result{}, errors.New("execution deadline was consumed in pool queue")
|
||||
}
|
||||
bRan <- target
|
||||
}
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
||||
}
|
||||
start := func(key, prompt string) *protocol.RunDetail {
|
||||
run, err := r.start(protocol.Message{PipelineID: definition.ID, Version: 1, RequestKey: key, Workspace: t.TempDir(), Input: map[string]any{"prompt": prompt}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return run
|
||||
}
|
||||
a := start("pool-a", "A")
|
||||
<-aStarted
|
||||
b := start("pool-b", "B")
|
||||
awaitResourceEvent(t, events, b.ID, func(e protocol.RunEventFrame) bool { return e.ResourceWait != nil })
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
select {
|
||||
case <-bRan:
|
||||
t.Fatal("pool waiter ran before either member was released")
|
||||
default:
|
||||
}
|
||||
if err := r.releaseResource(phone, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var target *protocol.ResourceTarget
|
||||
select {
|
||||
case target = <-bRan:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("pool waiter did not resume")
|
||||
}
|
||||
if target.Member != "phone" || target.Metadata["serial"] != "phone-serial" {
|
||||
t.Fatalf("wrong queued target: %#v", target)
|
||||
}
|
||||
awaitResourceEvent(t, events, b.ID, func(e protocol.RunEventFrame) bool { return e.Status == "succeeded" })
|
||||
close(releaseA)
|
||||
awaitResourceEvent(t, events, a.ID, func(e protocol.RunEventFrame) bool { return e.Status == "succeeded" })
|
||||
r.wg.Wait()
|
||||
detail, err := r.store.GetRun(ctx, b.ID)
|
||||
if err != nil || len(detail.Steps) != 1 || detail.Steps[0].Control.ResourceTarget == nil {
|
||||
t.Fatalf("pool target evidence missing: detail=%#v err=%v", detail, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceCancellationWaitsForOwnerCleanup(t *testing.T) {
|
||||
r, events, start := resourceRunner(t)
|
||||
started, cleaning, cleanupDone := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
||||
|
||||
@@ -1241,11 +1241,16 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
_, declaresSkip := def.Edges["skipped"]
|
||||
skip := !isHuman && declaresSkip && !state.uiSurface
|
||||
if selected.Resources != nil && !skip {
|
||||
if isHuman || !pipeline.ValidResourceKey(selected.Resources.Exclusive) {
|
||||
if isHuman || (selected.Resources.Exclusive == "" && selected.Resources.Pool == "") || (selected.Resources.Exclusive != "" && selected.Resources.Pool != "") || (selected.Resources.Exclusive != "" && !pipeline.ValidResourceKey(selected.Resources.Exclusive)) || (selected.Resources.Pool != "" && !pipeline.ValidResourceKey(selected.Resources.Pool)) {
|
||||
finish("failed", "error", "invalid persisted resource declaration")
|
||||
return
|
||||
}
|
||||
lease, err := r.acquireResource(ctx, pipeline.ResourceVisit{Resource: selected.Resources.Exclusive, RunID: id, Stage: key, Sequence: sequence}, workspace, state.completed)
|
||||
resource := selected.Resources.Exclusive
|
||||
pool := selected.Resources.Pool
|
||||
if pool != "" {
|
||||
resource = pool
|
||||
}
|
||||
lease, err := r.acquireResourceWithPool(ctx, pipeline.ResourceVisit{Resource: resource, RunID: id, Stage: key, Sequence: sequence}, pool, workspace, state.completed)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
finish("canceled", "", err.Error())
|
||||
@@ -1392,6 +1397,9 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
var piClient *stage.PiClient
|
||||
var err error
|
||||
stageCtx := ctx
|
||||
if held != nil && held.Target != nil {
|
||||
stageCtx = stage.WithResourceTarget(stageCtx, held.Target)
|
||||
}
|
||||
var modelConfig stage.ModelConfig
|
||||
var budgetBefore stage.BudgetSnapshot
|
||||
if stage.EffectiveType(def.Type) == stage.ModelType {
|
||||
@@ -1408,7 +1416,7 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
// its cumulative usage before the visit and record only this visit's
|
||||
// increment on the step, never the cumulative total again.
|
||||
budgetBefore = budgets[key].Snapshot()
|
||||
stageCtx = stage.WithBudget(ctx, budgets[key])
|
||||
stageCtx = stage.WithBudget(stageCtx, budgets[key])
|
||||
}
|
||||
snapshotCtx, snapshotCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
before := filediff.Capture(snapshotCtx, workspace)
|
||||
@@ -1470,7 +1478,7 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
result, err = (stage.ModelStage{Client: plain}).Run(stageCtx, def, input)
|
||||
client.Transcript = plain.FormatAudit
|
||||
} else {
|
||||
result, err = stage.DefaultRegistry().Run(ctx, def, input)
|
||||
result, err = stage.DefaultRegistry().Run(stageCtx, def, input)
|
||||
}
|
||||
}()
|
||||
if held != nil {
|
||||
@@ -1571,6 +1579,9 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
}
|
||||
step.Target = def.Edges[step.Outcome]
|
||||
step.Control = &protocol.ControlEvidence{Visit: state.visits[key], MaxVisits: maxVisits, Memory: reads}
|
||||
if held != nil && held.Target != nil {
|
||||
step.Control.ResourceTarget = held.Target
|
||||
}
|
||||
if err == nil && selected.Limits != nil && state.visits[key] >= maxVisits {
|
||||
for _, outcome := range selected.Limits.On {
|
||||
if outcome == step.Outcome {
|
||||
|
||||
Reference in New Issue
Block a user