Repository navigation
Expand file tree
/
Copy pathhost_surface_test.go
More file actions
430 lines (401 loc) · 12 KB
/
Copy pathhost_surface_test.go
File metadata and controls
430 lines (401 loc) · 12 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package engine_test
import (
"go/ast"
"go/build"
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"testing"
)
// The host contract is the exported surface of engine, llm, graph and tools.
// Some of that surface is spelled with types from engine-internal packages
// (aliases such as engine.MapStore, Options fields such as SecretStore, and
// re-exported functions such as engine.SetDefaultStore). Those symbols are
// frozen: changing them breaks hosts exactly like changing engine itself.
//
// This test walks the exported surface of the four contract packages, follows
// every referenced engine-internal symbol transitively (struct fields,
// signatures, exported methods), and requires the reachable set to equal
// frozenInternalSymbols. A new leak, or a removed one, fails until the list
// and docs/architecture/HOST-ENGINE-BOUNDARY.md are updated deliberately.
const fluxModule = "github.com/GrayCodeAI/flux"
// fluxRoot is the module root relative to the engine package directory.
const fluxRoot = ".."
var hostContractPackages = []string{
fluxModule + "/engine",
fluxModule + "/graph",
fluxModule + "/llm",
fluxModule + "/tools",
}
// frozenInternalSymbols is the complete set of engine-internal symbols
// reachable from the host contract. Keep it sorted and in sync with the
// "Frozen engine-internal types" section of HOST-ENGINE-BOUNDARY.md.
var frozenInternalSymbols = []string{
fluxModule + "/credentials.DefaultStore",
fluxModule + "/credentials.MapStore",
fluxModule + "/credentials.SetDefaultStore",
fluxModule + "/credentials.Store",
fluxModule + "/operationsgraph.Export",
fluxModule + "/operationsgraph.Input",
fluxModule + "/provider/cache.CacheConfig",
fluxModule + "/provider/resilience.AdaptiveRateLimitConfig",
fluxModule + "/provider/resilience.HeaderExtractor",
fluxModule + "/provider/resilience.RateLimitHeaders",
}
type surfaceDecl struct {
file *ast.File
typeSpec *ast.TypeSpec
funcDecl *ast.FuncDecl
valueSpec *ast.ValueSpec
index int // position of the name inside valueSpec
}
type surfacePkg struct {
path string
name string
decls map[string]surfaceDecl
methods map[string][]surfaceDecl // receiver base type name -> methods
imports map[*ast.File]map[string]string
}
type surfaceScanner struct {
t *testing.T
fset *token.FileSet
pkgs map[string]*surfacePkg
visited map[string]bool
internal map[string][]string // internal symbol -> contract paths that reach it
}
func newSurfaceScanner(t *testing.T) *surfaceScanner {
return &surfaceScanner{
t: t,
fset: token.NewFileSet(),
pkgs: map[string]*surfacePkg{},
visited: map[string]bool{},
internal: map[string][]string{},
}
}
func isHostContract(path string) bool {
for _, p := range hostContractPackages {
if p == path {
return true
}
}
return false
}
func isFluxPackage(path string) bool {
return path == fluxModule || strings.HasPrefix(path, fluxModule+"/")
}
// isStdlib mirrors the go command's rule: standard-library import paths have
// no dot in their first element.
func isStdlib(path string) bool {
first, _, _ := strings.Cut(path, "/")
return !strings.Contains(first, ".")
}
var majorVersionElem = regexp.MustCompile(`^v[0-9]+$`)
// assumedPackageName guesses the name of a non-Flux package imported without
// an explicit name, the same way goimports does. A wrong guess cannot hide a
// leak: an unresolved qualifier in a type expression fails the test.
func assumedPackageName(path string) string {
elems := strings.Split(path, "/")
name := elems[len(elems)-1]
if majorVersionElem.MatchString(name) && len(elems) > 1 {
name = elems[len(elems)-2]
}
if i := strings.Index(name, ".v"); i > 0 {
name = name[:i]
}
name = strings.TrimPrefix(name, "go-")
name = strings.TrimSuffix(name, "-go")
return strings.ReplaceAll(name, "-", "_")
}
func (s *surfaceScanner) load(path string) *surfacePkg {
if pkg, ok := s.pkgs[path]; ok {
return pkg
}
dir := filepath.Join(fluxRoot, filepath.FromSlash(strings.TrimPrefix(path, fluxModule)))
bp, err := build.ImportDir(dir, 0)
if err != nil {
s.t.Fatalf("load %s: %v", path, err)
}
pkg := &surfacePkg{
path: path,
name: bp.Name,
decls: map[string]surfaceDecl{},
methods: map[string][]surfaceDecl{},
imports: map[*ast.File]map[string]string{},
}
s.pkgs[path] = pkg
for _, name := range bp.GoFiles {
file, err := parser.ParseFile(s.fset, filepath.Join(dir, name), nil, parser.SkipObjectResolution)
if err != nil {
s.t.Fatalf("parse %s/%s: %v", path, name, err)
}
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
if d.Recv == nil {
pkg.decls[d.Name.Name] = surfaceDecl{file: file, funcDecl: d}
} else if len(d.Recv.List) == 1 {
recv := receiverTypeName(d.Recv.List[0].Type)
pkg.methods[recv] = append(pkg.methods[recv], surfaceDecl{file: file, funcDecl: d})
}
case *ast.GenDecl:
for _, spec := range d.Specs {
switch sp := spec.(type) {
case *ast.TypeSpec:
pkg.decls[sp.Name.Name] = surfaceDecl{file: file, typeSpec: sp}
case *ast.ValueSpec:
for i, n := range sp.Names {
pkg.decls[n.Name] = surfaceDecl{file: file, valueSpec: sp, index: i}
}
}
}
}
}
}
return pkg
}
func receiverTypeName(expr ast.Expr) string {
for {
switch e := expr.(type) {
case *ast.StarExpr:
expr = e.X
case *ast.IndexExpr:
expr = e.X
case *ast.IndexListExpr:
expr = e.X
case *ast.Ident:
return e.Name
default:
return ""
}
}
}
func (s *surfaceScanner) importsOf(pkg *surfacePkg, file *ast.File) map[string]string {
if m, ok := pkg.imports[file]; ok {
return m
}
m := map[string]string{}
for _, imp := range file.Imports {
path, err := strconv.Unquote(imp.Path.Value)
if err != nil {
s.t.Fatalf("%s: bad import %s", pkg.path, imp.Path.Value)
}
var name string
switch {
case imp.Name != nil:
name = imp.Name.Name
case isFluxPackage(path):
name = s.load(path).name
default:
name = assumedPackageName(path)
}
if name == "." {
s.t.Errorf("%s: dot import of %s hides package qualifiers from this check", pkg.path, path)
continue
}
if name != "_" {
m[name] = path
}
}
pkg.imports[file] = m
return m
}
// reference records that the contract surface reaches path.name and walks it.
func (s *surfaceScanner) reference(path, name, from string) {
switch {
case isHostContract(path):
return // scanned as a root
case isStdlib(path):
return
}
key := path + "." + name
s.internal[key] = append(s.internal[key], from)
if isFluxPackage(path) {
s.visit(s.load(path), name, key)
}
}
// local handles an unqualified identifier declared in pkg.
func (s *surfaceScanner) local(pkg *surfacePkg, name, from string) {
if _, ok := pkg.decls[name]; !ok {
return // predeclared identifier or type parameter
}
if isHostContract(pkg.path) {
s.visit(pkg, name, from)
return
}
s.reference(pkg.path, name, from)
}
func (s *surfaceScanner) visit(pkg *surfacePkg, name, from string) {
key := pkg.path + "." + name
if s.visited[key] {
return
}
s.visited[key] = true
d, ok := pkg.decls[name]
if !ok {
s.t.Errorf("%s references %s, which is not a top-level declaration", from, key)
return
}
switch {
case d.typeSpec != nil:
if d.typeSpec.TypeParams != nil {
s.walkType(pkg, d.file, d.typeSpec.TypeParams, key)
}
s.walkType(pkg, d.file, d.typeSpec.Type, key)
for _, m := range pkg.methods[name] {
if m.funcDecl.Name.IsExported() {
s.walkType(pkg, m.file, m.funcDecl.Type, key+"."+m.funcDecl.Name.Name)
}
}
case d.funcDecl != nil:
s.walkType(pkg, d.file, d.funcDecl.Type, key)
case d.valueSpec != nil:
switch {
case d.valueSpec.Type != nil:
s.walkType(pkg, d.file, d.valueSpec.Type, key)
case d.index < len(d.valueSpec.Values):
s.walkValue(pkg, d.file, d.valueSpec.Values[d.index], key)
}
// An untyped spec without values repeats the previous const spec,
// which is visited on its own.
}
}
// walkType follows every named type in a type expression, skipping
// unexported struct fields, which are not part of the surface.
func (s *surfaceScanner) walkType(pkg *surfacePkg, file *ast.File, expr ast.Node, from string) {
ast.Inspect(expr, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.StructType:
for _, f := range n.Fields.List {
if len(f.Names) > 0 && !anyExported(f.Names) {
continue
}
s.walkType(pkg, file, f.Type, from)
}
return false
case *ast.Field:
s.walkType(pkg, file, n.Type, from)
return false
case *ast.SelectorExpr:
id, ok := n.X.(*ast.Ident)
if !ok {
s.t.Errorf("%s: unexpected qualified type %T", from, n.X)
return false
}
path, ok := s.importsOf(pkg, file)[id.Name]
if !ok {
s.t.Errorf("%s: cannot resolve package qualifier %q; teach assumedPackageName about it", from, id.Name)
return false
}
s.reference(path, n.Sel.Name, from)
return false
case *ast.Ident:
s.local(pkg, n.Name, from)
return false
}
return true
})
}
// walkValue follows the declared type of an initializer for a var or const
// declared without an explicit type.
func (s *surfaceScanner) walkValue(pkg *surfacePkg, file *ast.File, expr ast.Expr, from string) {
switch e := expr.(type) {
case *ast.BasicLit:
case *ast.Ident:
s.local(pkg, e.Name, from)
case *ast.SelectorExpr:
if id, ok := e.X.(*ast.Ident); ok {
if path, ok := s.importsOf(pkg, file)[id.Name]; ok {
s.reference(path, e.Sel.Name, from)
return
}
}
s.t.Errorf("%s: cannot type initializer %s; declare the value with an explicit type", from, exprString(s.fset, e))
case *ast.CallExpr:
s.walkValue(pkg, file, e.Fun, from)
case *ast.CompositeLit:
s.walkType(pkg, file, e.Type, from)
case *ast.FuncLit:
s.walkType(pkg, file, e.Type, from)
case *ast.ParenExpr:
s.walkValue(pkg, file, e.X, from)
case *ast.UnaryExpr:
s.walkValue(pkg, file, e.X, from)
case *ast.BinaryExpr:
s.walkValue(pkg, file, e.X, from)
s.walkValue(pkg, file, e.Y, from)
default:
s.t.Errorf("%s: unsupported initializer %s; declare the value with an explicit type", from, exprString(s.fset, e))
}
}
func exprString(fset *token.FileSet, e ast.Expr) string {
return fset.Position(e.Pos()).String()
}
func anyExported(names []*ast.Ident) bool {
for _, n := range names {
if n.IsExported() {
return true
}
}
return false
}
func (s *surfaceScanner) scanContract() {
for _, path := range hostContractPackages {
pkg := s.load(path)
names := make([]string, 0, len(pkg.decls))
for name := range pkg.decls {
if ast.IsExported(name) {
names = append(names, name)
}
}
sort.Strings(names)
for _, name := range names {
s.visit(pkg, name, path+"."+name)
}
}
}
func TestHostContractExposesOnlyFrozenInternalSymbols(t *testing.T) {
s := newSurfaceScanner(t)
s.scanContract()
frozen := map[string]bool{}
for _, sym := range frozenInternalSymbols {
frozen[sym] = true
}
var reached []string
for sym := range s.internal {
reached = append(reached, sym)
}
sort.Strings(reached)
for _, sym := range reached {
if !frozen[sym] {
from := s.internal[sym]
sort.Strings(from)
t.Errorf("engine-internal symbol %s is reachable from the host contract via %s: stop exposing it, or freeze it in frozenInternalSymbols and HOST-ENGINE-BOUNDARY.md", sym, strings.Join(from, ", "))
}
}
for _, sym := range frozenInternalSymbols {
if _, ok := s.internal[sym]; !ok {
t.Errorf("%s is frozen but no longer reachable from the host contract: remove it from frozenInternalSymbols and HOST-ENGINE-BOUNDARY.md", sym)
}
}
if !sort.StringsAreSorted(frozenInternalSymbols) {
t.Error("keep frozenInternalSymbols sorted")
}
}
func TestFrozenInternalSymbolsAreDocumented(t *testing.T) {
data, err := os.ReadFile(filepath.Join(fluxRoot, "docs", "architecture", "HOST-ENGINE-BOUNDARY.md"))
if err != nil {
t.Fatal(err)
}
doc := string(data)
for _, sym := range frozenInternalSymbols {
short := "`" + strings.TrimPrefix(sym, fluxModule+"/") + "`"
if !strings.Contains(doc, short) {
t.Errorf("HOST-ENGINE-BOUNDARY.md does not list frozen symbol %s", short)
}
}
}