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/equipo/team"
10 "amadan.net/oficina/equipo/internal/runner/provider"
11 "amadan.net/oficina/equipo/internal/runnerproto"
12)
13
14type ToolSubmission struct {
15 InstanceID string `json:"instance_id"`
16 TaskID string `json:"task_id"`
17 Attempt int64 `json:"attempt"`
18 Fence int64 `json:"fence"`
19 RequestID string `json:"request_id"`
20 Name string `json:"name"`
21 Arguments json.RawMessage `json:"arguments"`
22 Agent provider.Agent `json:"agent"`
23 Isolation team.IsolationSnapshot `json:"isolation"`
24}
25
26func (j *Journal) appendTool(ctx context.Context, v ToolSubmission, inputHash string) error {
27 tx, err := j.db.BeginTx(ctx, nil)
28 if err != nil {
29 return j.storageFailure(ctx, err)
30 }
31 defer tx.Rollback()
32 key := runnerproto.EventHash([]any{v.InstanceID, v.TaskID, v.Attempt, v.Fence, v.RequestID})
33 hash := runnerproto.EventHash([]any{v.Name, inputHash, v.Agent})
34 var old string
35 err = tx.QueryRowContext(ctx, "SELECT content_hash FROM journal_tools WHERE request_key=?", key).Scan(&old)
36 if err == nil {
37 if old != hash {
38 return runnerproto.ErrInvalid
39 }
40 return nil
41 }
42 if !errors.Is(err, sql.ErrNoRows) {
43 tx.Rollback()
44 return j.storageFailure(ctx, err)
45 }
46 raw, err := json.Marshal(v)
47 if err != nil {
48 return runnerproto.ErrInvalid
49 }
50 var size int64
51 if err = tx.QueryRowContext(ctx, "SELECT "+directPendingSize).Scan(&size); err != nil {
52 tx.Rollback()
53 return j.storageFailure(ctx, err)
54 }
55 if size+int64(len(raw)) > j.maxBytes {
56 return ErrJournalBudget
57 }
58 var pending, attemptBytes int64
59 if err = tx.QueryRowContext(ctx, `SELECT count(*), COALESCE(SUM(length(body)),0) FROM journal_tools WHERE acked=0
60 AND json_extract(body,'$.instance_id')=? AND json_extract(body,'$.task_id')=?
61 AND json_extract(body,'$.attempt')=? AND json_extract(body,'$.fence')=?`, v.InstanceID, v.TaskID, v.Attempt, v.Fence).Scan(&pending, &attemptBytes); err != nil {
62 tx.Rollback()
63 return j.storageFailure(ctx, err)
64 }
65 if pending >= 128 || attemptBytes+int64(len(raw)) > j.maxBytes/4 {
66 return ErrJournalBudget
67 }
68 if _, err = tx.ExecContext(ctx, "INSERT INTO journal_tools(request_key,content_hash,body,acked) VALUES(?,?,?,0)", key, hash, raw); err != nil {
69 tx.Rollback()
70 return j.storageFailure(ctx, err)
71 }
72 if err = tx.Commit(); err != nil {
73 return j.storageFailure(ctx, err)
74 }
75 return nil
76}
77func (j *Journal) ToolSubmissions(ctx context.Context, limit int) ([]ToolSubmission, error) {
78 if limit < 1 || limit > 1024 {
79 return nil, runnerproto.ErrInvalid
80 }
81 rows, err := j.db.QueryContext(ctx, "SELECT body FROM journal_tools WHERE acked=0 ORDER BY rowid LIMIT ?", limit)
82 if err != nil {
83 return nil, err
84 }
85 defer rows.Close()
86 out := []ToolSubmission{}
87 for rows.Next() {
88 var raw []byte
89 var v ToolSubmission
90 if err = rows.Scan(&raw); err != nil {
91 return nil, err
92 }
93 if err = json.Unmarshal(raw, &v); err != nil {
94 return nil, err
95 }
96 out = append(out, v)
97 }
98 return out, rows.Err()
99}
100func (j *Journal) AckTool(ctx context.Context, v ToolSubmission) error {
101 key := runnerproto.EventHash([]any{v.InstanceID, v.TaskID, v.Attempt, v.Fence, v.RequestID})
102 _, err := j.db.ExecContext(ctx, "UPDATE journal_tools SET acked=1 WHERE request_key=?", key)
103 if err != nil {
104 return j.storageFailure(ctx, err)
105 }
106 return nil
107}
108
109func (j *Journal) finishTools(ctx context.Context, c RunClaim, outcome string) error {
110 tx, err := j.db.BeginTx(ctx, nil)
111 if err != nil {
112 return err
113 }
114 defer tx.Rollback()
115 rows, err := tx.QueryContext(ctx, `SELECT request_key FROM journal_tools WHERE acked=0 AND json_extract(body,'$.task_id')=? AND json_extract(body,'$.attempt')=? AND json_extract(body,'$.fence')=?`, c.Assignment.TaskID, c.Handle.Attempt, c.Handle.Fence)
116 if err != nil {
117 return err
118 }
119 var keys []string
120 for rows.Next() {
121 var key string
122 if err = rows.Scan(&key); err != nil {
123 rows.Close()
124 return err
125 }
126 keys = append(keys, key)
127 }
128 err = rows.Err()
129 closeErr := rows.Close()
130 if err != nil {
131 return err
132 }
133 if closeErr != nil {
134 return closeErr
135 }
136 body, err := json.Marshal(struct {
137 Outcome string `json:"outcome"`
138 }{outcome})
139 if err != nil {
140 return err
141 }
142 for _, key := range keys {
143 if _, err = tx.ExecContext(ctx, "INSERT INTO journal_supervisor(kind,state_key,body) VALUES('tool-outcome',?,?) ON CONFLICT(kind,state_key) DO NOTHING", key, body); err != nil {
144 return err
145 }
146 if _, err = tx.ExecContext(ctx, "UPDATE journal_tools SET acked=1 WHERE request_key=?", key); err != nil {
147 return err
148 }
149 }
150 return tx.Commit()
151}
152
153// validateToolReplay applies the same immutable request key before automatic
154// approval that appendTool enforces again when the endpoint executes the call.
155func (j *Journal) validateToolReplay(ctx context.Context, v ToolSubmission) error {
156 key := runnerproto.EventHash([]any{v.InstanceID, v.TaskID, v.Attempt, v.Fence, v.RequestID})
157 hash := runnerproto.EventHash([]any{v.Name, runnerproto.EventHash(json.RawMessage(v.Arguments)), v.Agent})
158 var old string
159 err := j.db.QueryRowContext(ctx, "SELECT content_hash FROM journal_tools WHERE request_key=?", key).Scan(&old)
160 if errors.Is(err, sql.ErrNoRows) {
161 return nil
162 }
163 if err != nil {
164 return j.storageFailure(ctx, err)
165 }
166 if old != hash {
167 return runnerproto.ErrInvalid
168 }
169 return nil
170}
171
172func (j *Journal) toolSlot(ctx context.Context, b ToolBinding, id, name string, args json.RawMessage) error {
173 rows, err := j.db.QueryContext(ctx, `SELECT body FROM journal_tools WHERE acked=0 AND json_extract(body,'$.instance_id')=? AND json_extract(body,'$.task_id')=? AND json_extract(body,'$.attempt')=? AND json_extract(body,'$.fence')=? AND json_extract(body,'$.request_id')<>?`, b.InstanceID, b.TaskID, b.Attempt, b.Fence, id)
174 if err != nil {
175 return err
176 }
177 defer rows.Close()
178 count := 0
179 for rows.Next() {
180 count++
181 var raw []byte
182 if err = rows.Scan(&raw); err != nil {
183 return err
184 }
185 var old ToolSubmission
186 if json.Unmarshal(raw, &old) != nil {
187 return runnerproto.ErrInvalid
188 }
189 var before, after CandidateSubmission
190 if count > 1 || name != "submit_render" || old.Name != "submit_candidate" || old.Agent != b.Agent || json.Unmarshal(old.Arguments, &before) != nil || json.Unmarshal(args, &after) != nil || before != after {
191 return runnerproto.ErrInvalid
192 }
193 }
194 return rows.Err()
195}
196