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 "database/sql"
6 "encoding/json"
7 "errors"
8
9 "amadan.net/oficina/equipo/internal/coordinator"
10 "amadan.net/oficina/equipo/internal/runner/provider"
11 "amadan.net/oficina/equipo/internal/runnerproto"
12)
13
14type CoordinatorToolSubmission struct {
15 Claim runnerproto.CoordinatorClaim `json:"claim"`
16 Session provider.Session `json:"session"`
17 Call runnerproto.CoordinatorToolCall `json:"call"`
18}
19
20func coordinatorToolKey(c runnerproto.CoordinatorClaim, call runnerproto.CoordinatorToolCall) string {
21 return runnerproto.EventHash([]any{c.Scope.Account, c.Binding.ID, c.Binding.Generation, c.Turn.ID, c.Handle.Attempt, c.Handle.Fence, call.RequestID})
22}
23func (j *Journal) reserveCoordinatorTool(ctx context.Context, c runnerproto.CoordinatorClaim, s provider.Session, call runnerproto.CoordinatorToolCall) error {
24 call, err := coordinator.Canonical(call)
25 if err != nil {
26 return err
27 }
28 v := CoordinatorToolSubmission{Claim: c, Session: s, Call: call}
29 ref, claimRaw, raw, err := coordinatorClaimRecord(v)
30 if err != nil {
31 return err
32 }
33 key, hash := coordinatorToolKey(c, call), runnerproto.EventHash(v)
34 if err = j.AdmissionAllowed(ctx); err != nil {
35 return err
36 }
37 tx, err := j.db.BeginTx(ctx, nil)
38 if err != nil {
39 return j.storageFailure(ctx, err)
40 }
41 defer tx.Rollback()
42 delivered, err := coordinatorDelivered(ctx, tx, c, s)
43 if err != nil {
44 return err
45 }
46 if delivered {
47 return runnerproto.ErrInvalid
48 }
49 var old string
50 err = tx.QueryRowContext(ctx, "SELECT content_hash FROM coordinator_tools WHERE request_key=?", key).Scan(&old)
51 if err == nil {
52 if old != hash {
53 return runnerproto.ErrInvalid
54 }
55 return nil
56 }
57 if !errors.Is(err, sql.ErrNoRows) {
58 return err
59 }
60 var size, count int64
61 if err = tx.QueryRowContext(ctx, "SELECT "+directPendingSize).Scan(&size); err != nil {
62 return err
63 }
64 if err = tx.QueryRowContext(ctx, "SELECT count(*) FROM coordinator_tools WHERE binding=? AND generation=? AND turn=?", c.Binding.ID, c.Binding.Generation, c.Turn.ID).Scan(&count); err != nil {
65 return err
66 }
67 var exists int
68 if err = tx.QueryRowContext(ctx, "SELECT count(*) FROM coordinator_tool_claims WHERE claim_ref=?", ref).Scan(&exists); err != nil {
69 return err
70 }
71 extra := len(raw)
72 if exists == 0 {
73 extra += len(claimRaw)
74 }
75 if count >= 128 || size+int64(extra) > j.maxBytes {
76 return ErrJournal
77 }
78 if _, err = tx.ExecContext(ctx, "INSERT OR IGNORE INTO coordinator_tool_claims(claim_ref,turn_key,body) VALUES(?,?,?)", ref, coordinatorTurnKey(c, s), claimRaw); err != nil {
79 return err
80 }
81 if _, err = tx.ExecContext(ctx, "INSERT INTO coordinator_tools(request_key,content_hash,binding,generation,turn,body,result,acked) VALUES(?,?,?,?,?,?,NULL,0)", key, hash, c.Binding.ID, c.Binding.Generation, c.Turn.ID, raw); err != nil {
82 return err
83 }
84 return tx.Commit()
85}
86func (j *Journal) ackCoordinatorTool(ctx context.Context, c runnerproto.CoordinatorClaim, s provider.Session, call runnerproto.CoordinatorToolCall, result runnerproto.CoordinatorToolResult) error {
87 if err := j.reserveCoordinatorTool(ctx, c, s, call); err != nil {
88 return err
89 }
90 raw, err := json.Marshal(result)
91 if err != nil || len(raw) > 65536 || result.ResourceID == "" || result.Revision < 1 || !json.Valid(result.Value) {
92 return runnerproto.ErrInvalid
93 }
94 key := coordinatorToolKey(c, call)
95 tx, err := j.db.BeginTx(ctx, nil)
96 if err != nil {
97 return j.storageFailure(ctx, err)
98 }
99 defer tx.Rollback()
100 var old []byte
101 if err = tx.QueryRowContext(ctx, "SELECT result FROM coordinator_tools WHERE request_key=?", key).Scan(&old); err != nil {
102 return err
103 }
104 if len(old) > 0 && !runnerproto.SameRequestJSON(old, raw) {
105 return runnerproto.ErrInvalid
106 }
107 var size int64
108 if err = tx.QueryRowContext(ctx, "SELECT "+directPendingSize).Scan(&size); err != nil {
109 return err
110 }
111 if size+int64(len(raw)-len(old)) > j.maxBytes {
112 return ErrJournal
113 }
114 if _, err = tx.ExecContext(ctx, "UPDATE coordinator_tools SET result=?,acked=1 WHERE request_key=?", raw, key); err != nil {
115 return err
116 }
117 return tx.Commit()
118}
119func (j *Journal) CoordinatorToolSubmissions(ctx context.Context, limit int) ([]CoordinatorToolSubmission, error) {
120 if limit < 1 || limit > 128 {
121 return nil, runnerproto.ErrInvalid
122 }
123 rows, err := j.db.QueryContext(ctx, "SELECT t.body,c.body FROM coordinator_tools t JOIN coordinator_tool_claims c ON c.claim_ref=json_extract(t.body,'$.claim_ref') WHERE t.acked=0 AND t.submitted=1 ORDER BY t.ordinal LIMIT ?", limit)
124 if err != nil {
125 return nil, err
126 }
127 defer rows.Close()
128 out := []CoordinatorToolSubmission{}
129 for rows.Next() {
130 var raw, claimRaw []byte
131 var record coordinatorToolRecord
132 var v CoordinatorToolSubmission
133 if err = rows.Scan(&raw, &claimRaw); err != nil {
134 return nil, err
135 }
136 if err = json.Unmarshal(claimRaw, &v); err != nil {
137 return nil, err
138 }
139 if err = json.Unmarshal(raw, &record); err != nil {
140 return nil, err
141 }
142 v.Call = record.Call
143 out = append(out, v)
144 }
145 return out, rows.Err()
146}
147
148func (j *Journal) submitCoordinatorTool(ctx context.Context, c runnerproto.CoordinatorClaim, s provider.Session, call runnerproto.CoordinatorToolCall) error {
149 if err := j.reserveCoordinatorTool(ctx, c, s, call); err != nil {
150 return err
151 }
152 _, err := j.db.ExecContext(ctx, "UPDATE coordinator_tools SET submitted=1 WHERE request_key=?", coordinatorToolKey(c, call))
153 if err != nil {
154 return j.storageFailure(ctx, err)
155 }
156 return nil
157}
158