| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | goaldomain "reasonix/internal/goal" |
| 10 | "reasonix/internal/hook" |
| 11 | "reasonix/internal/session" |
| 12 | ) |
| 13 | |
| 14 | func TestGoalWorkPinsAppendDestinationAcrossBindingChange(t *testing.T) { |
| 15 | c, original := goalWorkController(t) |
| 16 | service, _, _ := c.v3Binding() |
| 17 | replacement, err := service.Create(t.Context(), session.CreateOptions{SessionID: "replacement"}) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | defer func() { c.v3BindingMu.Lock(); c.sessionRuntime = original; c.v3BindingMu.Unlock() }() |
| 22 | _, err = c.applyHostGoalMutationForRuntime(t.Context(), "work-duration", original, func(machine *goaldomain.Machine) (*goaldomain.View, error) { |
| 23 | view, err := machine.AddWorkDuration(machine.Get().Ref(), 1000) |
| 24 | // Rebind after the candidate is prepared but before its destination is read. |
| 25 | c.v3BindingMu.Lock() |
| 26 | c.sessionRuntime = replacement |
| 27 | c.v3BindingMu.Unlock() |
| 28 | return &view, err |
| 29 | }) |
| 30 | if !errors.Is(err, errStaleGoalWork) { |
| 31 | t.Fatalf("stale binding error = %v", err) |
| 32 | } |
| 33 | if raw := replacement.Session().StateSnapshot().Projection.GoalState; len(raw) != 0 { |
| 34 | t.Fatalf("old goal written to replacement: %s", raw) |
| 35 | } |
| 36 | if got := c.GoalRuntime().WorkDurationMs; got != 0 { |
| 37 | t.Fatalf("stale work published = %d", got) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestGoalWorkSurvivesCancellationActivityRevision(t *testing.T) { |
| 42 | c, runtime := goalWorkController(t) |
| 43 | work := c.beginGoalWork() |
| 44 | before := runtime.StateSnapshot().ActivityRevision |
| 45 | _, cancel := context.WithCancel(t.Context()) |
| 46 | defer cancel() |
| 47 | done := make(chan struct{}) |
| 48 | close(done) |
| 49 | c.mu.Lock() |
| 50 | c.turns.cancel = cancel |
| 51 | c.turns.done = done |
| 52 | c.mu.Unlock() |
| 53 | defer func() { |
| 54 | c.mu.Lock() |
| 55 | c.turns.cancel, c.turns.done = nil, nil |
| 56 | c.mu.Unlock() |
| 57 | }() |
| 58 | if !runtime.Cancel() { |
| 59 | t.Fatal("runtime did not cancel") |
| 60 | } |
| 61 | snapshot := runtime.StateSnapshot() |
| 62 | if snapshot.ActivityRevision == before || snapshot.Phase != session.RuntimeCancelling { |
| 63 | t.Fatalf("cancellation did not change runtime revision: %+v", snapshot) |
| 64 | } |
| 65 | if err := c.finishGoalWork(work, work.started.Add(time.Second)); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if got := c.GoalRuntime().WorkDurationMs; got != 1000 { |
| 69 | t.Fatalf("cancelled work = %d", got) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestGoalWorkDoesNotCountRejectedSubagentSubmission(t *testing.T) { |
| 74 | c, _ := goalWorkController(t) |
| 75 | called := false |
| 76 | c.hooks = hook.NewRunner([]hook.ResolvedHook{{ |
| 77 | HookConfig: hook.HookConfig{Command: "reject"}, Event: hook.UserPromptSubmit, |
| 78 | }}, t.TempDir(), func(context.Context, hook.SpawnInput) hook.SpawnResult { |
| 79 | called = true |
| 80 | return hook.SpawnResult{ExitCode: 2, Stderr: "rejected"} |
| 81 | }, nil) |
| 82 | err := newTurnOrchestrator(c).runSubagentSkillTurns(t.Context(), nil, "work", "work", "work", nil, false, nil, nil) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if !called { |
| 87 | t.Fatal("submission gate was not exercised") |
| 88 | } |
| 89 | if got := c.GoalRuntime().WorkDurationMs; got != 0 { |
| 90 | t.Fatalf("rejected submission work = %d", got) |
| 91 | } |
| 92 | } |
| 93 |