Repository navigation
Expand file tree
/
Copy pathchatv3_cells_test.go
More file actions
390 lines (365 loc) · 14.1 KB
/
Copy pathchatv3_cells_test.go
File metadata and controls
390 lines (365 loc) · 14.1 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
package main
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/Agent-Field/codeaf/internal/cell"
"github.com/Agent-Field/codeaf/internal/cell/celltest"
"github.com/Agent-Field/codeaf/internal/cellstore"
"github.com/Agent-Field/codeaf/internal/executor"
"github.com/Agent-Field/codeaf/internal/session"
)
func mintOpened(t *testing.T, bucket, workspace string) session.Place {
t.Helper()
place, err := v3MintSession(bucket, workspace, workspace, false)
if err != nil {
t.Fatal(err)
}
agent, _, _, err := openV3Agent(session.Config{
Workspace: workspace, Model: "test/model", APIKey: "test-key",
BaseURL: "https://example.invalid/v1",
Place: place, SessionFile: place.Transcript(),
}, workspace, v3OpenSession)
if err != nil {
t.Fatalf("session did not open: %v", err)
}
// An agent left open keeps writing into the session folder after the test
// returns, which races the temp-dir cleanup and fails the test at random.
t.Cleanup(func() { _ = agent.Close() })
return place
}
// TestNewChatIsACellByDefault proves C27.1 and C23 at the real chat mint door.
func TestNewChatIsACellByDefault(t *testing.T) {
celltest.Available(t)
bucket, workspace := t.TempDir(), t.TempDir()
place := mintOpened(t, bucket, workspace)
want := filepath.Join(place.Dir, cell.TranscriptPath)
if place.Transcript() != want {
t.Fatalf("transcript = %s, want %s", place.Transcript(), want)
}
if _, err := os.Stat(want); err != nil {
t.Fatalf("transcript not written in .cell: %v", err)
}
if _, err := os.Stat(filepath.Join(place.Dir, "transcript.jsonl")); !os.IsNotExist(err) {
t.Fatalf("a legacy transcript sits beside the cell: %v", err)
}
raw, err := os.ReadFile(filepath.Join(place.Dir, cell.MetaPath))
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), bucket) || strings.Contains(string(raw), workspace) {
t.Fatalf("cell meta holds an absolute path: %s", raw)
}
c, err := cell.OpenAt(place.Dir, place.ID())
if err != nil || c.Meta().Class != cell.HostBound {
t.Fatalf("new chat class = %s, %v; want host-bound (C23)", c.Meta().Class, err)
}
// Resume and listing find it through the same scan every launch uses.
spoken, empty := v3ScanBucket(bucket)
if len(spoken)+len(empty) != 1 || append(spoken, empty...)[0].dir != place.Dir {
t.Fatalf("listing = %v %v, want the cell", spoken, empty)
}
if folder, ok := session.FolderOf(place.Transcript()); !ok || folder != place.Dir {
t.Fatalf("FolderOf = %q %v", folder, ok)
}
if got := v3PlaceOf(place.Transcript(), workspace); got.Dir != place.Dir {
t.Fatalf("v3PlaceOf dir = %q, want %q", got.Dir, place.Dir)
}
}
// C27.3 preserves host execution when the engine is unavailable.
func TestNewChatIsALegacyFolderWithoutAnEngine(t *testing.T) {
t.Cleanup(cell.UseAvailabilityInTests(false))
bucket, workspace := t.TempDir(), t.TempDir()
place := mintOpened(t, bucket, workspace)
if want := filepath.Join(place.Dir, "transcript.jsonl"); place.Transcript() != want {
t.Fatalf("transcript = %s, want %s", place.Transcript(), want)
}
if _, err := os.Stat(filepath.Join(place.Dir, cell.StateDir)); !os.IsNotExist(err) {
t.Fatalf("unavailable engine must not create .cell: %v", err)
}
}
// legacyFolder writes a session folder the way a build without cells does: the
// real mint and the real agent, with a task journal beside the transcript.
func legacyFolder(t *testing.T, bucket, workspace string) session.Place {
t.Helper()
t.Cleanup(cell.UseAvailabilityInTests(false))
place, err := v3MintSession(bucket, workspace, workspace, false)
if err != nil {
t.Fatal(err)
}
agent, _, _, err := openV3Agent(v3TestConfig(workspace, place), workspace, v3OpenSession)
if err != nil {
t.Fatalf("legacy session did not open: %v", err)
}
if err := agent.Close(); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(place.NodeJournals(), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(place.NodeJournals(), "n1.jsonl"), []byte("{}\n"), 0o600); err != nil {
t.Fatal(err)
}
return place
}
func v3TestConfig(workspace string, place session.Place) session.Config {
return session.Config{
Workspace: workspace, Model: "test/model", APIKey: "test-key",
BaseURL: "https://example.invalid/v1",
Place: place, SessionFile: place.Transcript(),
}
}
// TestLegacyFolderResumesWithoutMigration proves C24 through the actual open
// and command doors, using the top-level names and JSONL layout dev writes.
func TestLegacyFolderResumesWithoutMigration(t *testing.T) {
celltest.Available(t)
for _, enabled := range []string{"0", "1"} {
t.Run(enabled, func(t *testing.T) {
t.Setenv("CODEAF_CELLS", enabled)
t.Setenv("CODEAF_SYNC_URL", "off")
workspace := t.TempDir()
dir := filepath.Join(t.TempDir(), "0123456789abcdef")
files := map[string]string{
"transcript.jsonl": `{"type":"session","version":1,"id":"0123456789abcdef","model":"test/model"}` + "\n" + `{"type":"message","role":"user","content":"old words"}` + "\n" + `{"type":"message","role":"assistant","content":"old answer"}` + "\n",
"state.json": `{ "type":"state", "version":1, "records":[] }` + "\n", "card.json": "{}\n", "tasks.json": `{ "type":"tasks", "version":1, "seq":0, "nodes":[] }` + "\n",
"tasks/n1.jsonl": "{}\n", "meta.json": `{"id":"0123456789abcdef","workspace":"` + workspace + `"}` + "\n",
}
for name, body := range files {
path := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
place := session.Place{Dir: dir, Workspace: workspace}
agent, cfg, _, err := openV3Agent(v3TestConfig(workspace, place), workspace, v3OpenSession)
if err != nil {
t.Fatal(err)
}
defer agent.Close()
if cfg.Seat != nil || cfg.Seals != nil || cfg.SessionFile != filepath.Join(dir, "transcript.jsonl") {
t.Fatalf("legacy chat acquired a cell: %+v", cfg)
}
for name, body := range files {
raw, err := os.ReadFile(filepath.Join(dir, name))
if err != nil || string(raw) != body {
t.Fatalf("open changed %s: %q, %v", name, raw, err)
}
}
events, err := agent.SubmitBash(t.Context(), "!printf continued")
if err != nil {
t.Fatal(err)
}
for event := range events {
if event.Kind == session.EventToolFailed {
t.Fatalf("command: %+v", event)
}
}
if err := agent.Close(); err != nil {
t.Fatal(err)
}
raw, err := os.ReadFile(filepath.Join(dir, "transcript.jsonl"))
if err != nil || !strings.HasPrefix(string(raw), files["transcript.jsonl"]) || !strings.Contains(string(raw), "continued") {
t.Fatalf("turn was not appended: %q %v", raw, err)
}
if _, err := os.Stat(filepath.Join(dir, ".cell")); !os.IsNotExist(err) {
t.Fatalf("open created .cell: %v", err)
}
})
}
}
// C27.5 and C24 preserve old conversations without an engine.
func TestLegacyFolderStaysLegacyWithoutAnEngine(t *testing.T) {
bucket, workspace := t.TempDir(), t.TempDir()
place := legacyFolder(t, bucket, workspace)
cfg := v3Migrated(v3TestConfig(workspace, place))
if cfg.SessionFile != place.Transcript() {
t.Fatalf("session file moved: %s", cfg.SessionFile)
}
if _, err := os.Stat(filepath.Join(place.Dir, cell.StateDir)); !os.IsNotExist(err) {
t.Fatalf("unavailable engine must not create .cell: %v", err)
}
if _, err := os.Stat(filepath.Join(place.Dir, "transcript.jsonl")); err != nil {
t.Fatalf("legacy transcript gone: %v", err)
}
}
func seatClass(t *testing.T, cfg session.Config, dir string) executor.Class {
t.Helper()
if cfg.Seat == nil {
t.Fatal("the session has no seat")
}
local, ok := cfg.Seat.In(dir).(executor.Local)
if !ok {
t.Fatalf("seat runner is %T; an unsealed workspace must hand out a plain local", cfg.Seat.In(dir))
}
return local.Class
}
// closeClassSeat gives E2's background work an explicit fixture lifetime.
// Class and policy fixtures use the availability seam without requiring an
// engine; capture tests use RequireEngine and assert the saving result itself.
func closeClassSeat(t *testing.T, seat executor.Seat) {
t.Helper()
if seat == nil {
return
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), cellstore.SealWithin)
defer cancel()
err := executor.Drain(ctx, seat)
if ctx.Err() != nil {
t.Error(err)
} else if err != nil {
t.Logf("class fixture capture: %v", err)
}
})
}
// TestSeatFollowsTheCellsClass proves C23 preserves an existing sandboxed
// cell, including its metadata, when the conversation resumes.
func TestSeatFollowsTheCellsClass(t *testing.T) {
celltest.Available(t)
c, err := cell.CreateIn(t.TempDir(), cell.Options{Class: cell.Sandboxed})
if err != nil {
t.Fatal(err)
}
workspace := t.TempDir()
before, err := os.ReadFile(filepath.Join(c.Root, cell.MetaPath))
if err != nil {
t.Fatal(err)
}
cfg := v3Seated(v3Migrated(session.Config{Place: session.Place{Dir: c.Root, Workspace: workspace}}))
closeClassSeat(t, cfg.Seat)
after, err := os.ReadFile(filepath.Join(c.Root, cell.MetaPath))
if err != nil || string(before) != string(after) {
t.Fatalf("resuming rewrote the declared class: %s, %v", after, err)
}
if got := seatClass(t, cfg, workspace); got != executor.Sandboxed {
t.Fatalf("class = %d, want sandboxed", got)
}
}
// Two sessions in one process on the same path each keep the class their own
// cell declared: nothing is looked up by path.
// C27.7 exercises this behaviour without an opt-in switch.
func TestTwoSessionsOnOnePathKeepTheirOwnClasses(t *testing.T) {
celltest.Available(t)
workspace := t.TempDir()
seated := func(class cell.Class) session.Config {
c, err := cell.CreateIn(t.TempDir(), cell.Options{Class: class})
if err != nil {
t.Fatal(err)
}
cfg := v3Seated(session.Config{Place: session.Place{Dir: c.Root, Workspace: workspace}})
closeClassSeat(t, cfg.Seat)
return cfg
}
sandboxed, host := seated(cell.Sandboxed), seated(cell.HostBound)
if a, b := seatClass(t, sandboxed, workspace), seatClass(t, host, workspace); a != executor.Sandboxed || b != executor.HostBound {
t.Fatalf("classes = %d and %d, want sandboxed and host-bound", a, b)
}
}
// C27.3 and C24 leave unavailable and existing host conversations on the host.
func TestNoSeatWithoutAnEngineOrTheFolderIsNoCell(t *testing.T) {
celltest.Available(t)
t.Cleanup(cell.UseAvailabilityInTests(false))
c, err := cell.CreateIn(t.TempDir(), cell.Options{Class: cell.Sandboxed})
if err != nil {
t.Fatal(err)
}
place := session.Place{Dir: c.Root, Workspace: t.TempDir()}
if cfg := v3Seated(session.Config{Place: place, Seat: executor.Host}); cfg.Seat != nil {
t.Fatal("unavailable engine must leave the session on the host, and clear a stale seat")
}
t.Cleanup(cell.UseAvailabilityInTests(true))
other := session.Place{Dir: t.TempDir(), Workspace: t.TempDir()}
if cfg := v3Seated(session.Config{Place: other}); cfg.Seat != nil {
t.Fatal("a folder that is no cell must keep the legacy host rule")
}
}
// TestC23NewChatCommandsHaveOpenNetworkWithoutConfinement proves C23 using
// the mint and seat doors, with the executor's jail and observer seams so no
// network connection or working kernel jail is needed.
func TestC23NewChatCommandsHaveOpenNetworkWithoutConfinement(t *testing.T) {
celltest.Available(t)
t.Setenv("CODEAF_HOME", t.TempDir())
workspace := t.TempDir()
place, err := v3MintSession(t.TempDir(), workspace, workspace, false)
if err != nil {
t.Fatal(err)
}
c, err := cell.OpenAt(place.Dir, place.ID())
if err != nil || c.Meta().Class != cell.HostBound {
t.Fatalf("minted class = %s, %v; want host-bound", c.Meta().Class, err)
}
seat, _, _ := v3SeatOf(place, nil)
closeClassSeat(t, seat)
if seat == nil {
t.Fatal("new chat has no seat")
}
local, ok := seat.In(workspace).(executor.Local)
if !ok {
t.Fatalf("runner = %T, want local", seat.In(workspace))
}
probe := &c23CommandProbe{}
local.Jail, local.Observer = probe, probe
res, err := local.Exec(context.Background(), executor.ExecRequest{Argv: []string{"sh", "-c", "printf ordinary"}}, nil)
if err != nil || res.Exit != 0 || string(res.Stdout) != "ordinary" {
t.Fatalf("ordinary command: %+v, %v", res, err)
}
if probe.confined || !probe.request.Net.Open || probe.request.Setup || probe.request.Class != executor.HostBound || res.JailDegraded {
t.Fatalf("ordinary call policy = %+v, confined = %v, degraded = %v", probe.request, probe.confined, res.JailDegraded)
}
}
type c23CommandProbe struct {
confined bool
request executor.ExecRequest
}
func (p *c23CommandProbe) Confine(*exec.Cmd, executor.ExecRequest) error {
p.confined = true
return errors.New("C23: a new chat must not enter a jail")
}
func (p *c23CommandProbe) Observe(req executor.ExecRequest, _ executor.ExecResult) {
p.request = req
}
// TestC23NewChatCanWriteToASiblingThroughItsSeat proves C23 through a real
// sealed call, so cells being on cannot narrow the host's write access. D4 also
// requires the pending capture to finish before its files are removed.
func TestC23NewChatCanWriteToASiblingThroughItsSeat(t *testing.T) {
celltest.RequireEngine(t)
t.Setenv("CODEAF_HOME", t.TempDir())
parent := t.TempDir()
workspace, sibling := filepath.Join(parent, "workspace"), filepath.Join(parent, "sibling")
for _, dir := range []string{workspace, sibling} {
if err := os.Mkdir(dir, 0o700); err != nil {
t.Fatal(err)
}
}
place, err := v3MintSession(t.TempDir(), workspace, workspace, false)
if err != nil {
t.Fatal(err)
}
var sealError error
seat, _, _ := v3SeatOf(place, func(line string) { t.Log(line) })
if seat == nil {
t.Fatal("new chat has no seat")
}
path := filepath.Join(sibling, "x")
var result executor.ExecResult
err = seat.Around(context.Background(), executor.Call{Tool: "bash"}, func() ([]byte, bool) {
result, sealError = seat.In(workspace).Exec(context.Background(), executor.ExecRequest{Argv: []string{"touch", path}}, nil)
return result.Stdout, sealError != nil || result.Exit != 0
})
if err != nil || sealError != nil || result.Exit != 0 {
t.Fatalf("sibling write = %+v, %v, %v", result, sealError, err)
}
if _, err := os.Stat(path); err != nil {
t.Fatalf("sibling file was not written: %v", err)
}
if err := executor.Drain(context.Background(), seat); err != nil {
t.Fatalf("sibling write was not sealed: %v", err)
}
}