1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
package executor
import (
"context"
"errors"
"log/slog"
"os"
"strings"
"testing"
"time"
"github.com/thepeterstone/claudomator/internal/storage"
"github.com/thepeterstone/claudomator/internal/task"
)
// depTask returns a task with the given DependsOn and state, using the same
// shape makeTask() produces (see executor_test.go) so it satisfies
// task.Validate-adjacent expectations (non-nil Tags/DependsOn slices etc).
func depTask(id string, dependsOn []string, state task.State) *task.Task {
tk := makeTask(id)
if dependsOn == nil {
dependsOn = []string{}
}
tk.DependsOn = dependsOn
tk.State = state
return tk
}
func cascadeTestPool(t *testing.T, store Store) *Pool {
t.Helper()
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
return NewPool(2, map[string]Runner{"claude": &mockRunner{}}, store, logger)
}
// TestPool_CascadeFail_SingleLevel: A fails; B depends on A and is PENDING.
// cascadeFail must mark B CANCELLED with a message referencing A.
func TestPool_CascadeFail_SingleLevel(t *testing.T) {
store := testStore(t)
a := depTask("cf-a", nil, task.StateFailed)
b := depTask("cf-b", []string{"cf-a"}, task.StatePending)
if err := store.CreateTask(a); err != nil {
t.Fatal(err)
}
if err := store.CreateTask(b); err != nil {
t.Fatal(err)
}
pool := cascadeTestPool(t, store)
pool.cascadeFail("cf-a", "build failed")
got, err := store.GetTask("cf-b")
if err != nil {
t.Fatal(err)
}
if got.State != task.StateCancelled {
t.Fatalf("cf-b state: want CANCELLED, got %s", got.State)
}
execs, err := store.ListExecutions("cf-b")
if err != nil {
t.Fatal(err)
}
if len(execs) != 1 {
t.Fatalf("expected 1 execution recorded for cf-b, got %d", len(execs))
}
if execs[0].Status != "CANCELLED" {
t.Errorf("execution status: want CANCELLED, got %q", execs[0].Status)
}
if !strings.Contains(execs[0].ErrorMsg, "cf-a") {
t.Errorf("execution ErrorMsg should reference upstream task cf-a, got %q", execs[0].ErrorMsg)
}
}
// TestPool_CascadeFail_MultiLevelRecursion: A fails, B depends on A, C
// depends on B. Both B and C must end up CANCELLED — cascadeFail must
// recurse through the whole subtree, not just cancel direct dependents.
func TestPool_CascadeFail_MultiLevelRecursion(t *testing.T) {
store := testStore(t)
a := depTask("cfm-a", nil, task.StateFailed)
b := depTask("cfm-b", []string{"cfm-a"}, task.StatePending)
c := depTask("cfm-c", []string{"cfm-b"}, task.StatePending)
for _, tk := range []*task.Task{a, b, c} {
if err := store.CreateTask(tk); err != nil {
t.Fatal(err)
}
}
pool := cascadeTestPool(t, store)
pool.cascadeFail("cfm-a", "build failed")
gotB, err := store.GetTask("cfm-b")
if err != nil {
t.Fatal(err)
}
gotC, err := store.GetTask("cfm-c")
if err != nil {
t.Fatal(err)
}
if gotB.State != task.StateCancelled {
t.Errorf("cfm-b state: want CANCELLED, got %s", gotB.State)
}
if gotC.State != task.StateCancelled {
t.Errorf("cfm-c state: want CANCELLED, got %s", gotC.State)
}
}
// TestPool_CascadeFail_LeavesRunningAndCompletedAlone: dependents that are
// already RUNNING or already terminal (COMPLETED) when the cascade fires
// must be left untouched, not force-cancelled.
func TestPool_CascadeFail_LeavesRunningAndCompletedAlone(t *testing.T) {
store := testStore(t)
a := depTask("cfr-a", nil, task.StateFailed)
running := depTask("cfr-running", []string{"cfr-a"}, task.StateRunning)
completed := depTask("cfr-completed", []string{"cfr-a"}, task.StateCompleted)
for _, tk := range []*task.Task{a, running, completed} {
if err := store.CreateTask(tk); err != nil {
t.Fatal(err)
}
}
pool := cascadeTestPool(t, store)
pool.cascadeFail("cfr-a", "build failed")
gotRunning, err := store.GetTask("cfr-running")
if err != nil {
t.Fatal(err)
}
if gotRunning.State != task.StateRunning {
t.Errorf("cfr-running state: want unchanged RUNNING, got %s", gotRunning.State)
}
gotCompleted, err := store.GetTask("cfr-completed")
if err != nil {
t.Fatal(err)
}
if gotCompleted.State != task.StateCompleted {
t.Errorf("cfr-completed state: want unchanged COMPLETED, got %s", gotCompleted.State)
}
// No execution records should have been created for either — cascadeFail
// should not have touched them at all.
if execs, _ := store.ListExecutions("cfr-running"); len(execs) != 0 {
t.Errorf("expected no executions recorded for cfr-running, got %d", len(execs))
}
if execs, _ := store.ListExecutions("cfr-completed"); len(execs) != 0 {
t.Errorf("expected no executions recorded for cfr-completed, got %d", len(execs))
}
}
// TestPool_CascadeFail_CycleGuard proves cascadeFail terminates on a
// dependency cycle instead of recursing forever. task.Validate and
// storage.CreateTask do not reject dependency cycles today (there is no
// cycle-detection anywhere in task creation), so this guard is load-bearing,
// not just defense-in-depth.
func TestPool_CascadeFail_CycleGuard(t *testing.T) {
store := testStore(t)
a := depTask("cyc-a", []string{"cyc-b"}, task.StatePending)
b := depTask("cyc-b", []string{"cyc-a"}, task.StatePending)
if err := store.CreateTask(a); err != nil {
t.Fatal(err)
}
if err := store.CreateTask(b); err != nil {
t.Fatal(err)
}
pool := cascadeTestPool(t, store)
done := make(chan struct{})
go func() {
pool.cascadeFail("cyc-a", "boom")
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("cascadeFail did not terminate on a dependency cycle — infinite recursion?")
}
gotA, err := store.GetTask("cyc-a")
if err != nil {
t.Fatal(err)
}
gotB, err := store.GetTask("cyc-b")
if err != nil {
t.Fatal(err)
}
if gotA.State != task.StateCancelled {
t.Errorf("cyc-a state: want CANCELLED, got %s", gotA.State)
}
if gotB.State != task.StateCancelled {
t.Errorf("cyc-b state: want CANCELLED, got %s", gotB.State)
}
}
// TestPool_CascadeFail_WiredIntoHandleRunResult verifies cascadeFail is
// actually invoked when a task's run terminates in a failure state via the
// normal handleRunResult path (not just when called directly), using a real
// store so ListDependents sees real data.
func TestPool_CascadeFail_WiredIntoHandleRunResult(t *testing.T) {
store := testStore(t)
a := depTask("hrr-cascade-a", nil, task.StateRunning)
b := depTask("hrr-cascade-b", []string{"hrr-cascade-a"}, task.StatePending)
if err := store.CreateTask(a); err != nil {
t.Fatal(err)
}
if err := store.CreateTask(b); err != nil {
t.Fatal(err)
}
pool := cascadeTestPool(t, store)
execRec := &storage.Execution{ID: "hrr-cascade-exec", TaskID: a.ID, Status: "RUNNING"}
pool.handleRunResult(context.Background(), a, execRec, errors.New("boom"), "claude")
<-pool.resultCh
gotB, err := store.GetTask("hrr-cascade-b")
if err != nil {
t.Fatal(err)
}
if gotB.State != task.StateCancelled {
t.Errorf("hrr-cascade-b state: want CANCELLED after upstream FAILED, got %s", gotB.State)
}
}
|