Repository navigation
Expand file tree
/
Copy pathsettle_test.go
More file actions
237 lines (228 loc) · 7.29 KB
/
Copy pathsettle_test.go
File metadata and controls
237 lines (228 loc) · 7.29 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package exec
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/Agent-Field/codeaf/internal/plan"
"github.com/Agent-Field/codeaf/internal/provider"
)
// plan.Node.Checked is the marker because it is a string an ending replaces;
// nothing accumulates into it, so a door copying a leaf's ending writes a plain
// assignment and this walk catches it. The tree's other Checked is an unrelated
// counter on resident.WatchPass in cmd/codeaf/wake.go and is reached only by +=;
// naming it here keeps the law from being widened until ordinary watchkeeping
// makes it red and somebody deletes it. Reading the plain assignments across
// the checkout makes a second copy of the ending fail in the file that
// introduces it instead of leaving a comment to be remembered.
func TestOnlyTheSettlingSeamWritesALeafsEnding(t *testing.T) {
root := repoRoot(t)
var writers []string
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() {
switch entry.Name() {
case ".git", "third_party", "bin":
if path != root {
return filepath.SkipDir
}
}
return nil
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
fset := token.NewFileSet()
file, parseErr := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution)
if parseErr != nil {
return parseErr
}
ast.Inspect(file, func(node ast.Node) bool {
assignment, ok := node.(*ast.AssignStmt)
if !ok || assignment.Tok != token.ASSIGN {
return true
}
for _, left := range assignment.Lhs {
selector, ok := left.(*ast.SelectorExpr)
if !ok || selector.Sel.Name != "Checked" {
continue
}
position := fset.Position(selector.Pos())
relative, relErr := filepath.Rel(root, position.Filename)
if relErr != nil {
relative = position.Filename
}
writers = append(writers, fmt.Sprintf("%s:%d", filepath.ToSlash(relative), position.Line))
}
return true
})
return nil
})
if err != nil {
t.Fatalf("walking the checkout for leaf-ending writers: %v", err)
}
if len(writers) != 1 || !strings.HasPrefix(writers[0], "internal/exec/settle.go:") {
t.Fatalf("a leaf's ending writes Checked at %v; internal/exec/settle.go must be its one writer. "+
"Call exec.Settle from every door instead of copying the ending's assignments.", writers)
}
}
// The chat and headless doors both settle a leaf, but only the chat door has a
// journal, so reaching the same call is what keeps their endings from drifting.
// declaredFunction matches a name alone; recordOutcome and apply are each unique
// in their file. If either door is renamed or moved, this test moves with it
// rather than deleting the line that keeps the doors together.
func TestBothDoorsSettleThroughTheSeam(t *testing.T) {
tests := []struct {
path string
function string
call string
door string
}{
{"cmd/codeaf/chat.go", "recordOutcome", "exec.Settle", "chat recordOutcome"},
{"internal/exec/schedule.go", "apply", "Settle", "headless scheduler apply"},
}
for _, test := range tests {
t.Run(test.door, func(t *testing.T) {
path := filepath.Join(repoRoot(t), filepath.FromSlash(test.path))
file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.SkipObjectResolution)
if err != nil {
t.Fatalf("parse %s: %v", test.path, err)
}
function := declaredFunction(file, test.function)
if function == nil || function.Body == nil {
t.Fatalf("%s no longer has %s; move this law with the door that settles its node", test.path, test.function)
}
if !bodyCalls(function.Body, test.call) {
t.Fatalf("the %s door stopped reaching %s; every door that settles a leaf must call the one seam", test.door, test.call)
}
})
}
}
func TestSettleWritesTheWholeEndingAndThenJournals(t *testing.T) {
node := &plan.Node{}
outcome := &Outcome{
Text: "the finished result",
Artifacts: []string{"answer.txt", "report.json"},
Turns: 4,
Usage: Usage{
PromptTokens: 50,
CompletionTokens: 7,
Cost: 1.25,
},
Stop: StopDone,
Verdict: provider.ReadingVerifiedSuccess,
Account: &Account{
Files: []FileChange{{Path: "answer.txt", Change: ChangeAdded, Added: 3}},
Checks: []Check{{Command: "go test ./...", Passed: true}},
},
Calibration: []string{"comfortably inside the envelope"},
}
journaled := 0
Settle(node, outcome, nil, func() {
journaled++
assertSettledEnding(t, node, outcome)
if node.State != plan.StateDone {
t.Errorf("state at journal time = %q, want %q", node.State, plan.StateDone)
}
})
assertSettledEnding(t, node, outcome)
if node.State != plan.StateDone {
t.Errorf("state = %q, want %q", node.State, plan.StateDone)
}
if journaled != 1 {
t.Errorf("journal calls = %d, want 1", journaled)
}
}
func TestSettleWithNoJournalStillSettlesTheNode(t *testing.T) {
tests := []struct {
name string
outcome *Outcome
err error
}{
{"an error", &Outcome{Text: "partial"}, errors.New("the attempt failed")},
{"no outcome", nil, nil},
{"an empty result", &Outcome{Text: " \n"}, nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
node := &plan.Node{}
Settle(node, test.outcome, test.err, nil)
if node.State != plan.StateFailed {
t.Errorf("state = %q, want %q", node.State, plan.StateFailed)
}
})
}
}
func assertSettledEnding(t *testing.T, node *plan.Node, outcome *Outcome) {
t.Helper()
if node.Turns != outcome.Turns {
t.Errorf("turns = %d, want %d", node.Turns, outcome.Turns)
}
wantTokens := outcome.Usage.PromptTokens + outcome.Usage.CompletionTokens
if node.Tokens != wantTokens {
t.Errorf("tokens = %d, want %d", node.Tokens, wantTokens)
}
if node.Cost != outcome.Usage.Cost {
t.Errorf("cost = %v, want %v", node.Cost, outcome.Usage.Cost)
}
if node.Stop != string(outcome.Stop) {
t.Errorf("stop = %q, want %q", node.Stop, outcome.Stop)
}
if node.Verdict != outcome.Verdict {
t.Errorf("verdict = %q, want %q", node.Verdict, outcome.Verdict)
}
if !reflect.DeepEqual(node.Artifacts, outcome.Artifacts) {
t.Errorf("artifacts = %#v, want %#v", node.Artifacts, outcome.Artifacts)
}
if node.Result != outcome.Text {
t.Errorf("result = %q, want %q", node.Result, outcome.Text)
}
if node.Checked != outcome.Account.Summary() {
t.Errorf("checked = %q, want %q", node.Checked, outcome.Account.Summary())
}
if !reflect.DeepEqual(node.Calibration, outcome.Calibration) {
t.Errorf("calibration = %#v, want %#v", node.Calibration, outcome.Calibration)
}
}
func declaredFunction(file *ast.File, name string) *ast.FuncDecl {
for _, declaration := range file.Decls {
function, ok := declaration.(*ast.FuncDecl)
if ok && function.Name.Name == name {
return function
}
}
return nil
}
func bodyCalls(body *ast.BlockStmt, wanted string) bool {
found := false
ast.Inspect(body, func(node ast.Node) bool {
if found {
return false
}
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
switch function := call.Fun.(type) {
case *ast.Ident:
if wanted == function.Name {
found = true
}
case *ast.SelectorExpr:
owner, ok := function.X.(*ast.Ident)
if ok && wanted == owner.Name+"."+function.Sel.Name {
found = true
}
}
return !found
})
return found
}