-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsession_fingerprint_test.go
More file actions
155 lines (147 loc) · 7.64 KB
/
Copy pathsession_fingerprint_test.go
File metadata and controls
155 lines (147 loc) · 7.64 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
package claude
import (
"strings"
"testing"
"time"
"github.com/agent-dance/agent-adaptor/driver"
)
func TestConfiguredDriverSessionFingerprintCoversEveryConfigField(t *testing.T) {
t.Parallel()
base := Config{
CommonConfig: CommonConfig{
Command: "claude", CWD: "/repo",
Env: []driver.EnvBinding{{Name: "TOKEN", Value: "secret-one"}},
Instructions: &driver.InstructionsBundleRef{
ID: "instructions", Path: "/rules", Content: "rules", Fingerprint: "rules-v1",
Scope: driver.InstructionScopeProject, Mode: driver.InstructionModeReplace,
Native: map[string]any{"nested": map[string]any{"enabled": true}},
},
PromptTemplate: "{{prompt}}", BootstrapPromptTemplate: "bootstrap {{prompt}}",
WorkspaceStrategy: &driver.WorkspaceStrategy{
Type: driver.WorkspaceStrategyGitWorktree, BaseRef: "main",
BranchTemplate: "agent/{id}", WorktreeParentDir: "/worktrees",
},
WorkspaceRuntime: &driver.WorkspaceRuntimeConfig{Services: []driver.RuntimeServiceSpec{{
ID: "service", Name: "runtime", URL: "http://127.0.0.1", Description: "service",
Lifecycle: driver.RuntimeLifecycleEphemeral, ReuseKey: "shared", Command: "server",
CWD: "/service", Port: 8080, Metadata: map[string]string{"region": "local"},
}}},
Timeout: time.Minute, GracePeriod: time.Second, ExtraArgs: []string{"--verbose"},
},
Model: "claude-model", Effort: "high", MaxTurnsPerRun: 7,
}
baseFingerprint := configuredSessionFingerprint(t, Driver(base))
cases := []struct {
name string
mutate func(*Config)
}{
{name: "command", mutate: func(c *Config) { c.Command = "claude-custom" }},
{name: "cwd", mutate: func(c *Config) { c.CWD = "/other" }},
{name: "env name", mutate: func(c *Config) { c.Env[0].Name = "OTHER_TOKEN" }},
{name: "secret env value", mutate: func(c *Config) { c.Env[0].Value = "secret-two" }},
{name: "instructions id", mutate: func(c *Config) { c.Instructions.ID = "other" }},
{name: "instructions path", mutate: func(c *Config) { c.Instructions.Path = "/other-rules" }},
{name: "instructions content", mutate: func(c *Config) { c.Instructions.Content = "other rules" }},
{name: "instructions fingerprint", mutate: func(c *Config) { c.Instructions.Fingerprint = "rules-v2" }},
{name: "instructions scope", mutate: func(c *Config) { c.Instructions.Scope = driver.InstructionScopeUser }},
{name: "instructions mode", mutate: func(c *Config) { c.Instructions.Mode = driver.InstructionModeAdditive }},
{name: "instructions native", mutate: func(c *Config) { c.Instructions.Native["nested"].(map[string]any)["enabled"] = false }},
{name: "prompt template", mutate: func(c *Config) { c.PromptTemplate = "changed {{prompt}}" }},
{name: "bootstrap prompt template", mutate: func(c *Config) { c.BootstrapPromptTemplate = "changed" }},
{name: "workspace type", mutate: func(c *Config) { c.WorkspaceStrategy.Type = driver.WorkspaceStrategyCloudSandbox }},
{name: "workspace base ref", mutate: func(c *Config) { c.WorkspaceStrategy.BaseRef = "develop" }},
{name: "workspace branch template", mutate: func(c *Config) { c.WorkspaceStrategy.BranchTemplate = "changed/{id}" }},
{name: "workspace parent", mutate: func(c *Config) { c.WorkspaceStrategy.WorktreeParentDir = "/other-worktrees" }},
{name: "runtime service id", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].ID = "other" }},
{name: "runtime service name", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Name = "other" }},
{name: "runtime service url", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].URL = "http://localhost" }},
{name: "runtime service description", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Description = "other" }},
{name: "runtime service lifecycle", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Lifecycle = driver.RuntimeLifecycleShared }},
{name: "runtime service reuse key", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].ReuseKey = "other" }},
{name: "runtime service command", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Command = "other-server" }},
{name: "runtime service cwd", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].CWD = "/other-service" }},
{name: "runtime service port", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Port = 9090 }},
{name: "runtime service metadata", mutate: func(c *Config) { c.WorkspaceRuntime.Services[0].Metadata["region"] = "remote" }},
{name: "timeout", mutate: func(c *Config) { c.Timeout = 2 * time.Minute }},
{name: "grace period", mutate: func(c *Config) { c.GracePeriod = 2 * time.Second }},
{name: "extra args", mutate: func(c *Config) { c.ExtraArgs[0] = "--debug" }},
{name: "model", mutate: func(c *Config) { c.Model = "other-model" }},
{name: "effort", mutate: func(c *Config) { c.Effort = "low" }},
{name: "max turns", mutate: func(c *Config) { c.MaxTurnsPerRun = 8 }},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
changed := cloneConfig(base)
tc.mutate(&changed)
if got := configuredSessionFingerprint(t, Driver(changed)); got == baseFingerprint {
t.Fatalf("changing %s did not change the session config fingerprint", tc.name)
}
})
}
}
func TestConfiguredDriverSessionFingerprintUsesConstructionSnapshot(t *testing.T) {
t.Parallel()
firstNative := map[string]any{"alpha": "one", "beta": []string{"two"}}
firstMetadata := map[string]string{"alpha": "one", "beta": "two"}
cfg := Config{CommonConfig: CommonConfig{
Env: []driver.EnvBinding{{Name: "TOKEN", Value: "secret"}},
Instructions: &driver.InstructionsBundleRef{Native: firstNative},
WorkspaceRuntime: &driver.WorkspaceRuntimeConfig{Services: []driver.RuntimeServiceSpec{{
ID: "service", Metadata: firstMetadata,
}}},
}}
bound := Driver(cfg)
want := configuredSessionFingerprint(t, bound)
cfg.Env[0].Value = "mutated"
firstNative["alpha"] = "mutated"
firstMetadata["alpha"] = "mutated"
if got := configuredSessionFingerprint(t, bound); got != want {
t.Fatalf("construction snapshot changed after caller mutation: %q != %q", got, want)
}
secondNative := map[string]any{}
secondNative["beta"] = []string{"two"}
secondNative["alpha"] = "one"
secondMetadata := map[string]string{}
secondMetadata["beta"] = "two"
secondMetadata["alpha"] = "one"
equivalent := Driver(Config{CommonConfig: CommonConfig{
Env: []driver.EnvBinding{{Name: "TOKEN", Value: "secret"}},
Instructions: &driver.InstructionsBundleRef{Native: secondNative},
WorkspaceRuntime: &driver.WorkspaceRuntimeConfig{Services: []driver.RuntimeServiceSpec{{
ID: "service", Metadata: secondMetadata,
}}},
}})
if got := configuredSessionFingerprint(t, equivalent); got != want {
t.Fatalf("map insertion order changed fingerprint: %q != %q", got, want)
}
}
func TestConfiguredDriverSessionFingerprintErrorDoesNotLeakSecret(t *testing.T) {
t.Parallel()
const secret = "claude-secret-value"
d := Driver(Config{CommonConfig: CommonConfig{Instructions: &driver.InstructionsBundleRef{
Native: map[string]any{secret: func() {}},
}}})
fingerprinter := d.(driver.SessionConfigFingerprinter)
_, err := fingerprinter.SessionConfigFingerprint()
if err == nil {
t.Fatal("expected unstable native config to fail")
}
if strings.Contains(err.Error(), secret) {
t.Fatalf("fingerprint error leaked secret: %q", err)
}
}
func configuredSessionFingerprint(t *testing.T, d driver.Driver) string {
t.Helper()
fingerprinter, ok := d.(driver.SessionConfigFingerprinter)
if !ok {
t.Fatalf("%T does not implement driver.SessionConfigFingerprinter", d)
}
fingerprint, err := fingerprinter.SessionConfigFingerprint()
if err != nil {
t.Fatal(err)
}
if fingerprint == "" {
t.Fatal("SessionConfigFingerprint returned empty fingerprint")
}
return fingerprint
}