oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/equipo/internal/runner/provider"
12 "amadan.net/oficina/equipo/internal/runnerproto"
13)
14
15func TestCoordinatorToolJournalSettledBudget(t *testing.T) {
16 ctx := context.Background()
17 path := filepath.Join(t.TempDir(), "private", "journal.db")
18 j, err := OpenJournal(ctx, path)
19 mustJournal(t, err)
20 defer func() { j.Close() }()
21 j.maxBytes = 64 << 10
22 c := runnerproto.CoordinatorClaim{}
23 c.Binding.ID = "binding"
24 c.Binding.Generation = 1
25 c.Context.Messages = []runnerproto.CoordinatorMessage{{Body: strings.Repeat("context", 1024)}}
26 session := provider.Session{ID: "session", AttemptKey: "attempt", Provider: "claude"}
27 call := runnerproto.CoordinatorToolCall{RequestID: "r", Name: "post_message", Arguments: json.RawMessage(`{"body":"hello","source_refs":[]}`)}
28 result := runnerproto.CoordinatorToolResult{ResourceID: "message", Revision: 1, Value: json.RawMessage(`{}`)}
29 for i := 0; i < 40; i++ {
30 e := journalEvent(fmt.Sprintf("delivery-%d", i), int64(i+1))
31 e.Kind = "coordinator.delivery"
32 e.TurnID = fmt.Sprintf("turn-%d", i)
33 e.TaskID = e.TurnID
34 e.AssignmentID = e.TurnID
35 c.Turn.ID = e.TurnID
36 c.Scope = e.Scope
37 c.Handle = e.Handle
38 mustJournal(t, j.submitCoordinatorTool(ctx, c, session, call))
39 mustJournal(t, j.ackCoordinatorTool(ctx, c, session, call, result))
40 mustJournal(t, j.Append(ctx, e))
41 mustJournal(t, j.Ack(ctx, e.ID))
42 mustJournal(t, j.AdmissionAllowed(ctx))
43 var count int
44 mustJournal(t, j.db.QueryRow("SELECT count(*) FROM coordinator_tools").Scan(&count))
45 if count != 0 {
46 t.Fatalf("settled turn retained %d tool rows", count)
47 }
48 if err = j.reserveCoordinatorTool(ctx, c, session, call); err == nil {
49 t.Fatal("delivered turn reopened")
50 }
51 }
52 c.Turn.ID = "open"
53 for i := 0; i < 20; i++ {
54 call.RequestID = fmt.Sprint(i)
55 mustJournal(t, j.submitCoordinatorTool(ctx, c, session, call))
56 }
57 var size int64
58 mustJournal(t, j.db.QueryRow("SELECT "+directPendingSize).Scan(&size))
59 if size > 24<<10 {
60 t.Fatalf("claim context duplicated across tools: %d", size)
61 }
62 mustJournal(t, j.Close())
63 j, err = OpenJournal(ctx, path)
64 mustJournal(t, err)
65 pending, err := j.CoordinatorToolSubmissions(ctx, 128)
66 mustJournal(t, err)
67 if len(pending) != 20 || runnerproto.EventHash(pending[0].Claim) != runnerproto.EventHash(c) {
68 t.Fatal("open lost-ack replay lost its full claim", len(pending))
69 }
70 for _, v := range pending {
71 mustJournal(t, j.ackCoordinatorTool(ctx, v.Claim, v.Session, v.Call, result))
72 }
73 // Acknowledged tools stay hash-bound until delivery, including across reopen.
74 changed := call
75 changed.Arguments = json.RawMessage(`{"body":"changed","source_refs":[]}`)
76 if j.reserveCoordinatorTool(ctx, c, session, changed) == nil {
77 t.Fatal("open acknowledged call lost replay hash")
78 }
79}
80
81func TestCoordinatorToolJournalLegacyUpgrade(t *testing.T) {
82 ctx := context.Background()
83 path := filepath.Join(t.TempDir(), "private", "journal.db")
84 j, err := OpenJournal(ctx, path)
85 mustJournal(t, err)
86 c := runnerproto.CoordinatorClaim{}
87 c.Binding.ID = "legacy"
88 c.Turn.ID = "legacy-turn"
89 session := provider.Session{ID: "legacy-session"}
90 call := runnerproto.CoordinatorToolCall{RequestID: "legacy-request", Name: "post_message", Arguments: json.RawMessage(`{"body":"hi","source_refs":[]}`)}
91 v := CoordinatorToolSubmission{Claim: c, Session: session, Call: call}
92 raw, err := json.Marshal(v)
93 mustJournal(t, err)
94 _, err = j.db.Exec(`INSERT INTO coordinator_tools(request_key,content_hash,binding,generation,turn,body,submitted,acked) VALUES(?,?,?,?,?,?,1,0)`, coordinatorToolKey(c, call), runnerproto.EventHash(v), c.Binding.ID, c.Binding.Generation, c.Turn.ID, raw)
95 mustJournal(t, err)
96 _, err = j.db.Exec(`DROP TABLE coordinator_tool_claims; DROP INDEX coordinator_tools_claim; DROP INDEX journal_coordinator_delivery; PRAGMA user_version=5`)
97 mustJournal(t, err)
98 mustJournal(t, j.Close())
99 j, err = OpenJournal(ctx, path)
100 mustJournal(t, err)
101 defer j.Close()
102 pending, err := j.CoordinatorToolSubmissions(ctx, 10)
103 mustJournal(t, err)
104 if len(pending) != 1 || runnerproto.EventHash(pending[0]) != runnerproto.EventHash(v) {
105 t.Fatal("migration lost legacy pending submission")
106 }
107 mustJournal(t, j.reserveCoordinatorTool(ctx, c, session, call))
108 var body string
109 mustJournal(t, j.db.QueryRow("SELECT body FROM coordinator_tools").Scan(&body))
110 if strings.Contains(body, `"claim":`) {
111 t.Fatal("legacy claim not compacted")
112 }
113}
114
115func TestCoordinatorToolJournalRetirementScope(t *testing.T) {
116 ctx := context.Background()
117 j, err := OpenJournal(ctx, filepath.Join(t.TempDir(), "private", "journal.db"))
118 mustJournal(t, err)
119 defer j.Close()
120 if j.Ack(ctx, "unknown") == nil {
121 t.Fatal("unknown event acknowledged")
122 }
123 mustJournal(t, j.AdmissionAllowed(ctx))
124 e := journalEvent("delivery", 1)
125 e.Kind = "coordinator.delivery"
126 e.TurnID = "settled"
127 c := runnerproto.CoordinatorClaim{Scope: e.Scope, Handle: e.Handle, Turn: runnerproto.CoordinatorTurn{ID: e.TurnID}}
128 session := provider.Session{ID: e.SessionID}
129 call := runnerproto.CoordinatorToolCall{RequestID: "request", Name: "post_message", Arguments: json.RawMessage(`{"body":"hello","source_refs":[]}`)}
130 mustJournal(t, j.submitCoordinatorTool(ctx, c, session, call))
131 open := c
132 open.Turn.ID = "uncertain"
133 mustJournal(t, j.submitCoordinatorTool(ctx, open, session, call))
134 mustJournal(t, j.Append(ctx, e))
135 pending, err := j.CoordinatorToolSubmissions(ctx, 10)
136 mustJournal(t, err)
137 if len(pending) != 2 {
138 t.Fatal("unacknowledged delivery pruned replay")
139 }
140 mustJournal(t, j.Ack(ctx, e.ID))
141 pending, err = j.CoordinatorToolSubmissions(ctx, 10)
142 mustJournal(t, err)
143 if len(pending) != 1 || pending[0].Claim.Turn.ID != "uncertain" {
144 t.Fatal("delivery pruned the uncertain turn", pending)
145 }
146}
147