oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package teamcontract
2
3import (
4 "context"
5 "path/filepath"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo/team"
10)
11
12type unusedAdapter struct{}
13
14func (unusedAdapter) Classify(team.Operation) (team.ReplayClass, error) {
15 return team.ReplayIdempotent, nil
16}
17func (unusedAdapter) Execute(context.Context, team.Dispatch) (team.Receipt, error) {
18 panic("contract test must not dispatch")
19}
20func (unusedAdapter) Reconcile(context.Context, team.Dispatch) (team.ReconcileResult, error) {
21 panic("contract test must not reconcile")
22}
23
24func contractStore(t *testing.T) (*team.Store, context.Context) {
25 t.Helper()
26 ctx := context.Background()
27 clock := team.NewFakeClock(time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC))
28 auth := team.NewFakeAuthority(clock, 5*time.Second)
29 auth.Issue("grant", team.Grant{Account: "account", Operations: map[team.Operation]bool{
30 team.OpJobContinue: true, team.OpAttemptContinue: true, team.OpProviderCall: true,
31 }})
32 s, err := team.Open(ctx, filepath.Join(t.TempDir(), "jobs.db"), team.Options{
33 Clock: clock, Authority: auth,
34 Adapters: map[team.Operation]team.Adapter{team.OpProviderCall: unusedAdapter{}},
35 })
36 if err != nil {
37 t.Fatal(err)
38 }
39 t.Cleanup(func() { _ = s.Close() })
40 if _, err := s.Resume(ctx); err != nil {
41 t.Fatal(err)
42 }
43 return s, ctx
44}
45
46func businessJob(p team.Policy) team.JobSpec {
47 return team.JobSpec{
48 Account: "account", Requester: "requester", GrantRef: "grant",
49 Target: team.TargetBinding{Kind: team.TargetBusiness, Subject: "October"},
50 Evidence: team.EvidenceRequirement{Predicate: team.PredicateAttestedReconciliation,
51 RequiredSources: 1},
52 Policy: p, Steps: []team.StepSpec{{Key: "collect", Capability: team.CapCollect, Version: 1}},
53 }
54}
55
56func TestChildCannotRaiseParentsDepthLimit(t *testing.T) {
57 s, ctx := contractStore(t)
58 p := finitePolicy()
59 p.MaxDepth = 0
60 parent, err := s.CreateJob(ctx, businessJob(p))
61 if err != nil {
62 t.Fatal(err)
63 }
64 child := businessJob(finitePolicy())
65 child.Parent = parent.ID
66 if _, err := s.CreateJob(ctx, child); err == nil {
67 t.Fatal("child raised a parent's no-delegation policy")
68 }
69}
70
71func TestNewJobsCannotUnderpriceAccountReservations(t *testing.T) {
72 s, ctx := contractStore(t)
73 p := finitePolicy()
74 parent, err := s.CreateJob(ctx, businessJob(p))
75 if err != nil {
76 t.Fatal(err)
77 }
78 for _, parentID := range []team.JobID{0, parent.ID} {
79 proposal := businessJob(p)
80 proposal.Parent = parentID
81 proposal.Policy.Rates = team.Rates{Version: 1}
82 if _, err := s.CreateJob(ctx, proposal); err == nil {
83 t.Fatalf("job with parent %d replaced the account price table", parentID)
84 }
85 proposal.Policy = p
86 proposal.Policy.PerCall.OutputTokens--
87 if _, err := s.CreateJob(ctx, proposal); err == nil {
88 t.Fatalf("job with parent %d replaced the account call ceiling", parentID)
89 }
90 }
91}
92
93func TestNewJobCannotRaiseAccountCallLimit(t *testing.T) {
94 s, ctx := contractStore(t)
95 p := finitePolicy()
96 p.Account.Calls = 1
97 first, err := s.CreateJob(ctx, businessJob(p))
98 if err != nil {
99 t.Fatal(err)
100 }
101 firstScope := team.Scope{Account: first.Account, Job: first.ID}
102 h, err := s.Claim(ctx, firstScope, "collect", "runner-1", "workspace-1")
103 if err != nil {
104 t.Fatal(err)
105 }
106 in := team.IntentSpec{Key: "read", Operation: team.OpProviderCall,
107 Payload: []byte(`{"prompt":"read"}`), Reserve: team.Usage{Calls: 1, InputTokens: 1, OutputTokens: 1}}
108 if _, err := s.Intend(ctx, firstScope, h, in); err != nil {
109 t.Fatal(err)
110 }
111 if _, err := s.CreateJob(ctx, businessJob(p)); err != nil {
112 t.Fatalf("another job with the existing account policy: %v", err)
113 }
114 second, err := s.CreateJob(ctx, businessJob(finitePolicy()))
115 if err != nil {
116 return // Refusing a different account policy at creation also preserves the bound.
117 }
118 secondScope := team.Scope{Account: second.Account, Job: second.ID}
119 h2, err := s.Claim(ctx, secondScope, "collect", "runner-2", "workspace-2")
120 if err != nil {
121 t.Fatal(err)
122 }
123 if _, err := s.Intend(ctx, secondScope, h2, in); err == nil {
124 t.Fatal("new job raised the account's one-call limit and admitted a second call")
125 }
126}
127
128func TestParentCancellationStopsChildWork(t *testing.T) {
129 s, ctx := contractStore(t)
130 parent, err := s.CreateJob(ctx, businessJob(finitePolicy()))
131 if err != nil {
132 t.Fatal(err)
133 }
134 spec := businessJob(finitePolicy())
135 spec.Parent = parent.ID
136 child, err := s.CreateJob(ctx, spec)
137 if err != nil {
138 t.Fatal(err)
139 }
140 childScope := team.Scope{Account: child.Account, Job: child.ID}
141 h, err := s.Claim(ctx, childScope, "collect", "child-runner", "child-workspace")
142 if err != nil {
143 t.Fatal(err)
144 }
145 if _, err := s.Cancel(ctx, team.Scope{Account: parent.Account, Job: parent.ID}, "operator", "stop"); err != nil {
146 t.Fatal(err)
147 }
148 child, err = s.GetJob(ctx, childScope)
149 if err != nil {
150 t.Fatal(err)
151 }
152 if child.State != team.JobCancelled {
153 t.Errorf("child remains %s after parent cancellation", child.State)
154 }
155 if _, err := s.Renew(ctx, childScope, h); err == nil {
156 t.Error("child renewed after parent cancellation")
157 }
158 attempts, err := s.Attempts(ctx, childScope)
159 if err != nil {
160 t.Fatal(err)
161 }
162 if len(attempts) != 1 || attempts[0].StopRequested.IsZero() {
163 t.Error("child supervisor was not asked to stop")
164 }
165}
166
167func TestNewJobCannotRaiseAccountConcurrency(t *testing.T) {
168 s, ctx := contractStore(t)
169 p := finitePolicy()
170 p.MaxConcurrentAttempts = 1
171 first, err := s.CreateJob(ctx, businessJob(p))
172 if err != nil {
173 t.Fatal(err)
174 }
175 if _, err := s.Claim(ctx, team.Scope{Account: first.Account, Job: first.ID}, "collect", "runner-1", "workspace-1"); err != nil {
176 t.Fatal(err)
177 }
178 second, err := s.CreateJob(ctx, businessJob(finitePolicy()))
179 if err != nil {
180 t.Fatal(err)
181 }
182 if _, err := s.Claim(ctx, team.Scope{Account: second.Account, Job: second.ID}, "collect", "runner-2", "workspace-2"); err == nil {
183 t.Fatal("a new job raised the account concurrency limit")
184 }
185}
186
187func TestReplacedStoreCannotResumeOrCloseNewOwner(t *testing.T) {
188 ctx := context.Background()
189 clock := team.NewFakeClock(time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC))
190 auth := team.NewFakeAuthority(clock, 5*time.Second)
191 auth.Issue("grant", team.Grant{Account: "account", Operations: map[team.Operation]bool{team.OpJobContinue: true}})
192 opts := team.Options{Clock: clock, Authority: auth}
193 path := filepath.Join(t.TempDir(), "jobs.db")
194 old, err := team.Open(ctx, path, opts)
195 if err != nil {
196 t.Fatal(err)
197 }
198 t.Cleanup(func() { _ = old.Close() })
199 if _, err := old.Resume(ctx); err != nil {
200 t.Fatal(err)
201 }
202 replacement, err := team.Open(ctx, path, opts)
203 if err != nil {
204 t.Fatal(err)
205 }
206 t.Cleanup(func() { _ = replacement.Close() })
207 if _, err := replacement.Resume(ctx); err != nil {
208 t.Fatal(err)
209 }
210 if _, err := old.CreateJob(ctx, businessJob(finitePolicy())); err == nil {
211 t.Error("replaced store still admitted a job")
212 }
213 if _, err := old.Resume(ctx); err == nil {
214 t.Error("replaced store still controlled recovery")
215 }
216 _ = old.Close()
217 if _, err := replacement.CreateJob(ctx, businessJob(finitePolicy())); err != nil {
218 t.Errorf("old store's close affected the new owner: %v", err)
219 }
220}
221