-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy paththread_checkpoint_test.go
More file actions
351 lines (319 loc) · 12 KB
/
Copy paththread_checkpoint_test.go
File metadata and controls
351 lines (319 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
package adaptor_test
// Thread codec assertions replayed through the two v1 touch points that
// consume the codec: the persist pipeline and
// Thread.Checkpoint (the audit window onto the driver resume handle).
import (
"context"
"errors"
"maps"
"sync"
"testing"
"time"
adaptor "github.com/agent-dance/agent-adaptor"
"github.com/agent-dance/agent-adaptor/driver"
"github.com/agent-dance/agent-adaptor/memory"
"github.com/agent-dance/agent-adaptor/threadstore"
)
// codecFake is a fakeDriver that additionally implements
// driver.SessionCodecProvider.
type codecFake struct {
*fakeDriver
codec driver.SessionCodec
}
type typedNilCodecFake struct{ *fakeDriver }
type noCodecFake struct {
inner *fakeDriver
supports bool
}
func (d *noCodecFake) Descriptor() driver.Descriptor {
desc := d.inner.Descriptor()
desc.Sessions.SupportsResume = d.supports
return desc
}
func (d *noCodecFake) ValidateConfig(cfg any) error { return d.inner.ValidateConfig(cfg) }
func (d *noCodecFake) SessionConfigFingerprint() (string, error) {
return d.inner.SessionConfigFingerprint()
}
func (d *noCodecFake) Run(ctx context.Context, req driver.Request, sink driver.EventSink) (driver.Response, error) {
return d.inner.Run(ctx, req, sink)
}
type observingStore struct {
inner *memory.Store
mu sync.Mutex
calls int
}
func newObservingStore() *observingStore { return &observingStore{inner: memory.NewStore()} }
func (s *observingStore) called() {
s.mu.Lock()
s.calls++
s.mu.Unlock()
}
func (s *observingStore) callCount() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.calls
}
func (s *observingStore) Resolve(ctx context.Context, q threadstore.Query) (*threadstore.Record, error) {
s.called()
return s.inner.Resolve(ctx, q)
}
func (s *observingStore) Finalize(ctx context.Context, req threadstore.FinalizeRequest) error {
s.called()
return s.inner.Finalize(ctx, req)
}
func (s *observingStore) AcquireLease(ctx context.Context, target, owner string, ttl time.Duration) (threadstore.Lease, error) {
s.called()
return s.inner.AcquireLease(ctx, target, owner, ttl)
}
func (s *observingStore) RenewLease(ctx context.Context, lease threadstore.Lease, ttl time.Duration) error {
s.called()
return s.inner.RenewLease(ctx, lease, ttl)
}
func (s *observingStore) ReleaseLease(ctx context.Context, lease threadstore.Lease) error {
s.called()
return s.inner.ReleaseLease(ctx, lease)
}
func (*typedNilCodecFake) SessionCodec() driver.SessionCodec {
var codec *allowlistCodec
return codec
}
var (
_ driver.Driver = (*codecFake)(nil)
_ driver.SessionCodecProvider = (*codecFake)(nil)
)
func (d *codecFake) SessionCodec() driver.SessionCodec { return d.codec }
// allowlistCodec keeps only the "cwd" session parameter — everything else
// in State.Data is transient and must not survive normalization.
type allowlistCodec struct{}
type rejectingAllowlistCodec struct{ allowlistCodec }
func (rejectingAllowlistCodec) FromParams(driver.SessionParams) *driver.SessionState { return nil }
func (allowlistCodec) Name() string { return "allowlist" }
func (allowlistCodec) ToParams(state *driver.SessionState) driver.SessionParams {
if state == nil {
return driver.SessionParams{}
}
values := map[string]string{}
if cwd, ok := state.Data["cwd"]; ok {
values["cwd"] = cwd
}
return driver.SessionParams{ResumeID: state.ResumeID, DisplayID: state.DisplayID, Values: values}
}
func (allowlistCodec) FromParams(params driver.SessionParams) *driver.SessionState {
if params.ResumeID == "" && params.DisplayID == "" && len(params.Values) == 0 {
return nil
}
displayID := params.DisplayID
if displayID == "" {
displayID = params.ResumeID
}
return &driver.SessionState{ResumeID: params.ResumeID, DisplayID: displayID, Data: maps.Clone(params.Values)}
}
func (allowlistCodec) GuardFingerprint(params driver.SessionParams) string {
return "allowlist:" + params.ResumeID
}
// checkpointing returns a runFunc producing the given checkpoint state.
func checkpointing(state driver.SessionState) func(context.Context, driver.Request, driver.EventSink) (driver.Response, error) {
return func(_ context.Context, _ driver.Request, _ driver.EventSink) (driver.Response, error) {
copyState := state
copyState.Data = maps.Clone(state.Data)
return driver.Response{Output: "ok", Checkpoint: &driver.Checkpoint{State: ©State, Valid: true}}, nil
}
}
func TestThreadCheckpointNotFound(t *testing.T) {
agent := adaptor.New(newFakeDriver(), adaptor.WithThreadStore(memory.NewStore()))
if _, err := agent.Thread("never-ran").Checkpoint(context.Background()); !errors.Is(err, adaptor.ErrThreadNotFound) {
t.Fatalf("Checkpoint on missing thread: err = %v, want ErrThreadNotFound", err)
}
}
func TestThreadCheckpointRejectsInvalidDurableState(t *testing.T) {
tests := []struct {
name string
mutate func(*threadstore.Record)
want error
}{
{name: "nil_state", mutate: func(record *threadstore.Record) { record.State = nil }, want: adaptor.ErrThreadCheckpointMissing},
{name: "empty_resume_id", mutate: func(record *threadstore.Record) {
record.State = &driver.SessionState{DisplayID: "display-only"}
}, want: adaptor.ErrThreadIncompatible},
{name: "codec_mismatch", mutate: func(record *threadstore.Record) {
record.SessionCodec = "other/codec"
}, want: adaptor.ErrThreadIncompatible},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
fake := newFakeDriver()
fake.runFunc = checkpointing(driver.SessionState{ResumeID: "resume-1"})
store := memory.NewStore()
agent := adaptor.New(fake, adaptor.WithThreadStore(store))
thread := agent.Thread("invalid-checkpoint")
if _, err := thread.Run(ctx, "seed"); err != nil {
t.Fatalf("seed: %v", err)
}
record := *activeRecord(t, store, thread.Key())
tc.mutate(&record)
overwriteActiveRecord(t, store, record)
checkpoint, err := thread.Checkpoint(ctx)
if !errors.Is(err, tc.want) {
t.Fatalf("Checkpoint error = %v, want %v", err, tc.want)
}
if checkpoint != nil {
t.Fatalf("Checkpoint = %+v, want nil on invalid durable state", checkpoint)
}
})
}
}
func TestThreadCheckpointRejectsStateCodecCannotNormalize(t *testing.T) {
ctx := context.Background()
store := memory.NewStore()
seedFake := newFakeDriver()
seedFake.runFunc = checkpointing(driver.SessionState{ResumeID: "resume-1"})
seedDriver := &codecFake{fakeDriver: seedFake, codec: allowlistCodec{}}
if _, err := adaptor.New(seedDriver, adaptor.WithThreadStore(store)).Thread("codec-reject").Run(ctx, "seed"); err != nil {
t.Fatalf("seed: %v", err)
}
readDriver := &codecFake{fakeDriver: newFakeDriver(), codec: rejectingAllowlistCodec{}}
checkpoint, err := adaptor.New(readDriver, adaptor.WithThreadStore(store)).Thread("codec-reject").Checkpoint(ctx)
if !errors.Is(err, adaptor.ErrThreadIncompatible) {
t.Fatalf("Checkpoint error = %v, want ErrThreadIncompatible", err)
}
if checkpoint != nil {
t.Fatalf("Checkpoint = %+v, want nil when codec rejects state", checkpoint)
}
}
func TestThreadSessionCapabilityPrelaunchMatrix(t *testing.T) {
tests := []struct {
name string
driver func(*fakeDriver) driver.Driver
wantErr bool
}{
{name: "unsupported_without_provider", driver: func(f *fakeDriver) driver.Driver {
return &noCodecFake{inner: f, supports: false}
}, wantErr: true},
{name: "declared_without_provider", driver: func(f *fakeDriver) driver.Driver {
return &noCodecFake{inner: f, supports: true}
}, wantErr: true},
{name: "declared_with_typed_nil_codec", driver: func(f *fakeDriver) driver.Driver {
return &typedNilCodecFake{fakeDriver: f}
}, wantErr: true},
{name: "declared_with_valid_codec", driver: func(f *fakeDriver) driver.Driver {
return &codecFake{fakeDriver: f, codec: allowlistCodec{}}
}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fake := newFakeDriver()
fake.runFunc = checkpointing(driver.SessionState{ResumeID: "resume-1", Data: map[string]string{"cwd": "/repo"}})
store := newObservingStore()
agent := adaptor.New(tc.driver(fake), adaptor.WithThreadStore(store))
_, err := agent.Thread("capability-matrix").Run(context.Background(), "go")
if tc.wantErr {
if !errors.Is(err, adaptor.ErrThreadIncompatible) {
t.Fatalf("error = %v, want ErrThreadIncompatible", err)
}
if fake.runCount() != 0 {
t.Fatalf("Driver.Run called %d times after prelaunch rejection", fake.runCount())
}
if calls := store.callCount(); calls != 0 {
t.Fatalf("thread store observed %d calls after prelaunch rejection, want 0", calls)
}
return
}
if err != nil {
t.Fatalf("valid resume capability rejected: %v", err)
}
if fake.runCount() != 1 || store.callCount() == 0 {
t.Fatalf("valid path: runs=%d store calls=%d", fake.runCount(), store.callCount())
}
})
}
}
// TestThreadCheckpointExplicitCodecRoundTrip proves an explicitly declared
// codec preserves Data, defaults DisplayID to ResumeID, and returns snapshots
// isolated from the store.
func TestThreadCheckpointExplicitCodecRoundTrip(t *testing.T) {
ctx := context.Background()
fake := newFakeDriver()
fake.runFunc = checkpointing(driver.SessionState{
ResumeID: "pt-resume-1",
// DisplayID intentionally empty: passthrough defaults it.
Data: map[string]string{"cwd": "/repo"},
})
agent := adaptor.New(fake, adaptor.WithThreadStore(memory.NewStore()))
th := agent.Thread("k")
if _, err := th.Run(ctx, "seed"); err != nil {
t.Fatalf("seed: %v", err)
}
cp, err := th.Checkpoint(ctx)
if err != nil {
t.Fatalf("Checkpoint: %v", err)
}
if !cp.Valid || cp.State == nil {
t.Fatalf("checkpoint = %+v, want a valid state", cp)
}
if cp.State.ResumeID != "pt-resume-1" {
t.Fatalf("ResumeID = %q, want pt-resume-1", cp.State.ResumeID)
}
if cp.State.DisplayID != "pt-resume-1" {
t.Fatalf("DisplayID = %q, want the ResumeID default", cp.State.DisplayID)
}
if cp.State.Data["cwd"] != "/repo" {
t.Fatalf("Data = %+v, want cwd preserved", cp.State.Data)
}
// Audit snapshots are copies: mutating one must not corrupt the store.
cp.State.Data["cwd"] = "/mutated"
again, err := th.Checkpoint(ctx)
if err != nil {
t.Fatalf("second Checkpoint: %v", err)
}
if again.State.Data["cwd"] != "/repo" {
t.Fatalf("stored state mutated through the snapshot: %+v", again.State.Data)
}
}
// TestThreadCodecNormalizesPersistedAndExposedState: a driver-owned codec
// participates at both consumption points — the persisted record holds the
// codec-normalized snapshot (transient keys dropped at Finalize time) and
// Thread.Checkpoint reports the same normalized view.
func TestThreadCodecNormalizesPersistedAndExposedState(t *testing.T) {
ctx := context.Background()
fake := newFakeDriver()
fake.runFunc = checkpointing(driver.SessionState{
ResumeID: "codec-resume-1",
DisplayID: "session one",
Data: map[string]string{
"cwd": "/repo",
"transient": "scratch-buffer",
},
})
cd := &codecFake{fakeDriver: fake, codec: allowlistCodec{}}
store := memory.NewStore()
agent := adaptor.New(cd, adaptor.WithThreadStore(store))
th := agent.Thread("k")
if _, err := th.Run(ctx, "seed"); err != nil {
t.Fatalf("seed: %v", err)
}
// The persisted record is the codec snapshot, not the raw driver state.
rec := activeRecord(t, store, "k")
if rec.State == nil || rec.State.ResumeID != "codec-resume-1" {
t.Fatalf("stored state = %+v, want codec-resume-1", rec.State)
}
if _, leaked := rec.State.Data["transient"]; leaked {
t.Fatalf("transient key persisted past the codec: %+v", rec.State.Data)
}
if rec.State.Data["cwd"] != "/repo" {
t.Fatalf("allowlisted key lost: %+v", rec.State.Data)
}
cp, err := th.Checkpoint(ctx)
if err != nil {
t.Fatalf("Checkpoint: %v", err)
}
if !cp.Valid || cp.State == nil || cp.State.ResumeID != "codec-resume-1" {
t.Fatalf("checkpoint = %+v, want the codec-normalized resume handle", cp)
}
if cp.State.DisplayID != "session one" {
t.Fatalf("DisplayID = %q, want the driver-provided label", cp.State.DisplayID)
}
if _, leaked := cp.State.Data["transient"]; leaked {
t.Fatalf("transient key exposed through Checkpoint: %+v", cp.State.Data)
}
}