Design 47 H1: a step's named path may leave the module's directory, never the repository

A package artifact's tree names same-repository siblings its go.mod requires
as "../<module>/<dir>"; the fetch request's check and the narrowing refused
them as leaving the module. They are now accepted when their join with the
module's directory stays inside the repository, narrowed from the root, and
refused when they leave the repository.

Builds: design 47 H H-A-198, H-A-199, H-A-157, H-A-172
This commit is contained in:
2026-11-20 22:14:53 +01:00
parent d40f314e52
commit 6c65e82926
4 changed files with 37 additions and 4 deletions
@@ -146,9 +146,15 @@ func (r FetchRequest) Check() error {
if r.Dir != "" && (outside(r.Dir) || strings.HasPrefix(r.Dir, "/")) {
return fmt.Errorf("the module directory %q leaves the repository", r.Dir)
}
// A path is relative to the module's directory; one that leaves it but stays inside the repository (a sibling the
// artifact's go.mod requires, "../<module>/<dir>") is accepted and narrowed from the repository's root, and one that
// leaves the repository is refused (design 47 H-A-198, H-A-199).
for _, p := range r.Paths {
if p == "" || strings.HasPrefix(p, "/") || outside(p) || path.Clean(p) == "." {
return fmt.Errorf("the path %q leaves the module's directory or is not a path in it", p)
if p == "" || strings.HasPrefix(p, "/") || path.Clean(p) == "." {
return fmt.Errorf("the path %q is not a path relative to the module's directory", p)
}
if outside(path.Join(r.Dir, p)) {
return fmt.Errorf("the path %q leaves the repository", p)
}
}
return nil
@@ -37,6 +37,11 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) {
if dirOnly, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a"}); err != nil || dirOnly.Dir != "modules/a" || dirOnly.Paths != nil {
t.Fatalf("a directory with no paths: %+v, %v", dirOnly, err)
}
// A sibling of the same repository is a path that leaves the module's directory, not the repository (H-A-199).
if sib, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a",
"paths": []any{"pkg", "../b/client"}}); err != nil || len(sib.Paths) != 2 {
t.Fatalf("a sibling path inside the repository: %+v, %v", sib, err)
}
for _, c := range []struct {
set map[string]any
says string
@@ -46,7 +51,8 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) {
{map[string]any{"commit": "abc"}, "is not a full commit"},
{map[string]any{"base": "abc"}, "is not a trunk commit"},
{map[string]any{"paths": []any{"src"}}, "names no module directory"},
{map[string]any{"dir": "modules/a", "paths": []any{"../b"}}, "leaves the module's directory"},
{map[string]any{"dir": "modules/a", "paths": []any{"../../../b"}}, "leaves the repository"},
{map[string]any{"dir": "modules/a", "paths": []any{"/b"}}, "not a path relative to the module's directory"},
{map[string]any{"dir": "../a", "paths": []any{"src"}}, "leaves the repository"},
{map[string]any{"dir": "modules/a", "paths": []any{3}}, "paths is a list of strings"},
{map[string]any{"packages": map[string]any{"git.novox.be/novox/mesh-sdk/go": "main"}}, "neither a full commit nor a Go semantic version"},
+3 -1
View File
@@ -25,9 +25,11 @@ func Narrow(tree string, r sdkstep.FetchRequest) error {
}
keep := map[string]bool{".git": true}
for _, p := range r.Paths {
// Kept by its path from the repository's root: a path leaving the module's directory but not the repository is
// narrowed from the root like any other (design 47 H-A-199).
rel := path.Join(r.Dir, p)
if strings.HasPrefix(rel, "../") || rel == ".." || path.IsAbs(p) {
return fmt.Errorf("the named path %q leaves the module's directory", p)
return fmt.Errorf("the named path %q leaves the repository", p)
}
if _, err := os.Lstat(filepath.Join(tree, filepath.FromSlash(rel))); err != nil {
return fmt.Errorf("the named path %q is not in the fetched tree: %w", p, err)
@@ -68,6 +68,25 @@ func TestNarrowKeepsAModuleDirectorysPathsAndFailsOnAnAbsentOne(t *testing.T) {
}
}
// A path leaving the module's directory but not the repository is kept from the root; one leaving the repository is
// refused, naming the repository (design 47 H-A-198, H-A-199).
func TestNarrowKeepsASiblingOfTheRepositoryAndRefusesOneOutsideIt(t *testing.T) {
tree := t.TempDir()
for _, f := range []string{"modules/a/module.json", "modules/a/pkg/go.mod", "modules/a/notes.md", "modules/b/client/go.mod",
"modules/b/other.go", "root.txt"} {
writeTreeFile(t, tree, f, "x")
}
if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"../../../x"}}); err == nil || !strings.Contains(err.Error(), "leaves the repository") {
t.Fatalf("a path leaving the repository gave %v", err)
}
if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"pkg", "../b/client"}}); err != nil {
t.Fatal(err)
}
if got := strings.Join(filesUnder(t, tree), ","); got != "modules/a/module.json,modules/a/pkg/go.mod,modules/b/client/go.mod" {
t.Fatalf("the narrowed tree holds %s", got)
}
}
// A build that needs package.json or go.mod names it among its paths, and so keys on it (ADR 0499 clause 4).
func TestNarrowKeepsAManifestOfTheLanguageOnlyWhenNamed(t *testing.T) {
tree := t.TempDir()