-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisolation_test.go
More file actions
248 lines (220 loc) · 8.09 KB
/
Copy pathisolation_test.go
File metadata and controls
248 lines (220 loc) · 8.09 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
package dun
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/iodesystems/agentkit/llm"
)
func gitrun(t *testing.T, dir string, args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = dir
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
func TestWorktree_IsolatesChanges(t *testing.T) {
repo := t.TempDir()
gitrun(t, repo, "init", "-q")
gitrun(t, repo, "config", "user.email", "t@t")
gitrun(t, repo, "config", "user.name", "t")
os.WriteFile(filepath.Join(repo, "a.txt"), []byte("hello\n"), 0o644)
gitrun(t, repo, "add", ".")
gitrun(t, repo, "commit", "-qm", "init")
wt, isRepo, err := NewWorktree(repo, nil)
if err != nil || !isRepo {
t.Fatalf("NewWorktree: isRepo=%v err=%v", isRepo, err)
}
defer wt.Cleanup()
if wt.Branch == "" || wt.Path == repo {
t.Fatalf("expected an isolated worktree, got %+v", wt)
}
// Edit in the worktree; the ORIGINAL checkout must be untouched.
os.WriteFile(filepath.Join(wt.Path, "a.txt"), []byte("hello\nworld\n"), 0o644)
orig, _ := os.ReadFile(filepath.Join(repo, "a.txt"))
if string(orig) != "hello\n" {
t.Fatalf("worktree edit leaked into the main checkout: %q", orig)
}
if !strings.Contains(wt.Diff(), "world") {
t.Fatalf("Diff should show the change:\n%s", wt.Diff())
}
}
// A worktree lives under .dun/worktrees/, so a go.mod "replace => ../agentkit"
// only resolves from inside it through a symlink beside it. Without the
// symlink the isolation is real and the first build in it fails on a
// dependency that resolves fine in the checkout it was made from.
func TestWorktree_SymlinksItsMounts(t *testing.T) {
repo := t.TempDir()
gitrun(t, repo, "init", "-q")
gitrun(t, repo, "config", "user.email", "t@t")
gitrun(t, repo, "config", "user.name", "t")
gitrun(t, repo, "commit", "-qm", "init", "--allow-empty")
// The sibling module the replace directive points at.
dep := t.TempDir()
if err := os.WriteFile(filepath.Join(dep, "go.mod"), []byte("module dep\n"), 0o644); err != nil {
t.Fatal(err)
}
wt, isRepo, err := NewWorktree(repo, []MountSpec{{Source: dep, Name: "dep"}})
if err != nil || !isRepo {
t.Fatalf("NewWorktree: isRepo=%v err=%v", isRepo, err)
}
defer wt.Cleanup()
// "../dep" FROM the worktree is the symlink in the worktree parent.
via := filepath.Join(wt.Path, "..", "dep", "go.mod")
if _, err := os.Stat(via); err != nil {
t.Fatalf("the mount must resolve from inside the worktree (%s): %v", via, err)
}
if len(wt.Mounts) != 1 || wt.Mounts[0].Name != "dep" {
t.Errorf("the worktree should carry its mounts, got %+v", wt.Mounts)
}
}
// Harness.Mounts is what a MID-SESSION worktree is built from (/worktree new).
// It used to pass nil, so a worktree made at runtime got no symlinks at all
// while one made at startup did.
func TestHarnessMounts_AreTheSessionsOwn(t *testing.T) {
want := []MountSpec{{Source: "/opt/agentkit", Name: "agentkit"}}
h := &Harness{cfg: Config{ExtraMounts: want}}
got := h.Mounts()
if len(got) != 1 || got[0] != want[0] {
t.Fatalf("Mounts() = %+v, want %+v", got, want)
}
if (&Harness{}).Mounts() != nil {
t.Error("a session with no mounts must report none, not an empty non-nil surprise")
}
}
func TestWorktree_NonRepoPassThrough(t *testing.T) {
dir := t.TempDir()
wt, isRepo, err := NewWorktree(dir, nil)
if err != nil {
t.Fatal(err)
}
if isRepo {
t.Fatal("a bare temp dir should not be a git repo")
}
if wt.Path != dir {
t.Fatalf("pass-through should use the dir, got %q", wt.Path)
}
wt.Cleanup() // no-op, must not panic
}
func TestHostExec(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "x.txt"), []byte("hi"), 0o644)
out := HostExec{Dir: dir}.Run(context.Background(), "ls", nil)
if !strings.Contains(out.Output, "x.txt") {
t.Fatalf("exec ls should list x.txt: %q", out.Output)
}
if out.Failed() {
t.Errorf("`ls` of a real dir must not be a failure: %+v", out)
}
// The exit status is a FACT on the result now, not a substring of the text.
e := (HostExec{Dir: dir}).Run(context.Background(), "exit 3", nil)
if e.Code != 3 || !e.Failed() {
t.Fatalf("want code 3, got %+v", e)
}
if !strings.Contains(e.Render(), "[exit:") {
t.Errorf("the model still needs the marker: %q", e.Render())
}
}
// The regression that motivated ExecResult: a check whose OUTPUT contains the
// marker is not a failing check. Pass/fail is the exit code.
func TestExecResult_OutputCannotFakeAFailure(t *testing.T) {
out := HostExec{Dir: t.TempDir()}.Run(context.Background(), `echo "[exit: 1] not really"`, nil)
if out.Failed() {
t.Errorf("a command that PRINTS the marker exited 0 and passed: %+v", out)
}
cfg := &ShipConfig{Checks: []map[string]string{{"compile": "printer"}}}
exec := func(context.Context, string) ExecResult { return out }
if fail := runChecks(context.Background(), cfg, exec); fail != "" {
t.Errorf("runChecks failed a passing check on its own output:\n%s", fail)
}
}
// Streaming is what makes a background job watchable: the tee sees bytes while
// the command is still running, and the result still carries all of them.
func TestHostExec_TeesOutputWhileRunning(t *testing.T) {
var mu sync.Mutex
var seen []string
w := writerFunc(func(p []byte) (int, error) {
mu.Lock()
seen = append(seen, string(p))
mu.Unlock()
return len(p), nil
})
res := HostExec{Dir: t.TempDir()}.Run(context.Background(), "echo one; echo two", w)
mu.Lock()
got := strings.Join(seen, "")
mu.Unlock()
if !strings.Contains(got, "one") || !strings.Contains(got, "two") {
t.Errorf("the tee missed output: %q", got)
}
if !strings.Contains(res.Output, "one") || !strings.Contains(res.Output, "two") {
t.Errorf("teeing must not consume the output: %q", res.Output)
}
}
type writerFunc func([]byte) (int, error)
func (f writerFunc) Write(p []byte) (int, error) { return f(p) }
func TestWithExec_RoutesExecLocallyElseMCP(t *testing.T) {
inner := agentDispatch(func(name string) string { return "MCP:" + name })
d := withExec(inner, HostExec{Dir: t.TempDir()}, nil, nil, nil)
var call llm.ToolCall
call.Function.Name = "exec"
call.Function.Arguments = `{"command":"echo hello-exec"}`
out, _ := d(context.Background(), call)
if !strings.Contains(out, "hello-exec") {
t.Fatalf("exec should run the backend: %q", out)
}
call = llm.ToolCall{}
call.Function.Name = "search"
out, _ = d(context.Background(), call)
if out != "MCP:search" {
t.Fatalf("non-exec tools should route to MCP: %q", out)
}
}
// A container that outlives the timeout that killed it is a leak: `docker run`
// dying does not stop what it started, so the run has to be NAMEABLE and the
// cancel path has to stop it by name.
func TestDockerExec_ContainerIsStoppableByName(t *testing.T) {
d := DockerExec{Dir: "/wt", Image: "golang:1.26"}
args := d.runArgs("dun-42-1", "go test ./...")
var named string
for i, a := range args {
if a == "--name" && i+1 < len(args) {
named = args[i+1]
}
}
if named != "dun-42-1" {
t.Fatalf("the run must be named or it cannot be stopped; args=%v", args)
}
if joined := strings.Join(args, " "); !strings.Contains(joined, "--network none") {
t.Errorf("the no-egress default was lost: %v", args)
}
stop := dockerStop(named)
if got := strings.Join(stop.Args, " "); !strings.Contains(got, "stop") || !strings.Contains(got, named) {
t.Errorf("cancel must stop THAT container: %q", got)
}
// Two runs must never share a name, or stopping one stops the other.
name1, name2 := containerName(), containerName()
if name1 == name2 {
t.Errorf("container names collide: %q", name1)
}
}
func TestExecToolDef(t *testing.T) {
td := execToolDef()
if td.Function.Name != "exec" {
t.Fatalf("name = %q", td.Function.Name)
}
params, _ := td.Function.Parameters.(map[string]any)
req, _ := params["required"].([]string)
if len(req) != 1 || req[0] != "command" {
t.Fatalf("exec should require command: %+v", params["required"])
}
}
// agentDispatch adapts a name→result func to a ToolDispatcher.
func agentDispatch(f func(name string) string) func(context.Context, llm.ToolCall) (string, error) {
return func(_ context.Context, tc llm.ToolCall) (string, error) {
return f(tc.Function.Name), nil
}
}