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
8 "amadan.net/oficina/equipo/internal/runner/provider"
9 "amadan.net/oficina/equipo/internal/runnerproto"
10)
11
12type coordinatorToolRecord struct {
13 ClaimRef string `json:"claim_ref"`
14 Call runnerproto.CoordinatorToolCall `json:"call"`
15}
16
17func coordinatorTurnKey(c runnerproto.CoordinatorClaim, s provider.Session) string {
18 return runnerproto.EventHash([]any{c.Scope, c.Handle.Attempt, c.Handle.Fence, c.Turn.ID, s.ID})
19}
20func coordinatorClaimRecord(v CoordinatorToolSubmission) (string, []byte, []byte, error) {
21 call := v.Call
22 v.Call = runnerproto.CoordinatorToolCall{}
23 raw, err := json.Marshal(v)
24 if err != nil {
25 return "", nil, nil, err
26 }
27 ref := runnerproto.EventHash(v)
28 body, _ := json.Marshal(coordinatorToolRecord{ClaimRef: ref, Call: call})
29 return ref, raw, body, nil
30}
31
32// Upgrade old full-claim rows without dropping submitted or acknowledged calls.
33func compactCoordinatorTools(ctx context.Context, tx *sql.Tx) error {
34 rows, err := tx.QueryContext(ctx, "SELECT request_key,body FROM coordinator_tools WHERE json_extract(body,'$.claim_ref') IS NULL")
35 if err != nil {
36 return err
37 }
38 type oldRow struct {
39 key string
40 raw []byte
41 }
42 var old []oldRow
43 for rows.Next() {
44 var v oldRow
45 if err = rows.Scan(&v.key, &v.raw); err != nil {
46 rows.Close()
47 return err
48 }
49 old = append(old, v)
50 }
51 err = rows.Err()
52 rows.Close()
53 if err != nil {
54 return err
55 }
56 for _, row := range old {
57 var v CoordinatorToolSubmission
58 if err = json.Unmarshal(row.raw, &v); err != nil {
59 return err
60 }
61 ref, claim, body, err := coordinatorClaimRecord(v)
62 if err != nil {
63 return err
64 }
65 if _, err = tx.ExecContext(ctx, "INSERT OR IGNORE INTO coordinator_tool_claims(claim_ref,turn_key,body) VALUES(?,?,?)", ref, coordinatorTurnKey(v.Claim, v.Session), claim); err != nil {
66 return err
67 }
68 if _, err = tx.ExecContext(ctx, "UPDATE coordinator_tools SET body=? WHERE request_key=?", body, row.key); err != nil {
69 return err
70 }
71 }
72 delivered, err := tx.QueryContext(ctx, "SELECT id FROM journal_events WHERE acked=1 AND json_extract(body,'$.kind')='coordinator.delivery'")
73 if err != nil {
74 return err
75 }
76 var ids []string
77 for delivered.Next() {
78 var id string
79 if err = delivered.Scan(&id); err != nil {
80 delivered.Close()
81 return err
82 }
83 ids = append(ids, id)
84 }
85 err = delivered.Err()
86 delivered.Close()
87 if err != nil {
88 return err
89 }
90 for _, id := range ids {
91 if err = retireCoordinatorTools(ctx, tx, id); err != nil {
92 return err
93 }
94 }
95 return nil
96}
97func coordinatorDelivered(ctx context.Context, tx *sql.Tx, c runnerproto.CoordinatorClaim, s provider.Session) (bool, error) {
98 var n int
99 err := tx.QueryRowContext(ctx, `SELECT count(*) FROM journal_events WHERE acked=1 AND json_extract(body,'$.kind')='coordinator.delivery' AND json_extract(body,'$.scope.Account')=? AND json_extract(body,'$.scope.Job')=? AND json_extract(body,'$.handle.Attempt')=? AND json_extract(body,'$.handle.Fence')=? AND json_extract(body,'$.turn_id')=? AND json_extract(body,'$.session_id')=?`, c.Scope.Account, c.Scope.Job, c.Handle.Attempt, c.Handle.Fence, c.Turn.ID, s.ID).Scan(&n)
100 return n > 0, err
101}
102func retireCoordinatorTools(ctx context.Context, tx *sql.Tx, id string) error {
103 var raw []byte
104 if err := tx.QueryRowContext(ctx, "SELECT body FROM journal_events WHERE event_key=(SELECT event_key FROM journal_aliases WHERE id=?) AND acked=1", id).Scan(&raw); err != nil {
105 return err
106 }
107 var e runnerproto.Event
108 if err := json.Unmarshal(raw, &e); err != nil {
109 return err
110 }
111 if e.Kind != "coordinator.delivery" {
112 return nil
113 }
114 c := runnerproto.CoordinatorClaim{Scope: e.Scope, Handle: e.Handle, Turn: runnerproto.CoordinatorTurn{ID: e.TurnID}}
115 key := coordinatorTurnKey(c, provider.Session{ID: e.SessionID})
116 if _, err := tx.ExecContext(ctx, `DELETE FROM coordinator_tools WHERE json_extract(body,'$.claim_ref') IN (SELECT claim_ref FROM coordinator_tool_claims WHERE turn_key=?)`, key); err != nil {
117 return err
118 }
119 _, err := tx.ExecContext(ctx, "DELETE FROM coordinator_tool_claims WHERE turn_key=?", key)
120 return err
121}
122