package runner import ( "context" "database/sql" "encoding/json" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) type coordinatorToolRecord struct { ClaimRef string `json:"claim_ref"` Call runnerproto.CoordinatorToolCall `json:"call"` } func coordinatorTurnKey(c runnerproto.CoordinatorClaim, s provider.Session) string { return runnerproto.EventHash([]any{c.Scope, c.Handle.Attempt, c.Handle.Fence, c.Turn.ID, s.ID}) } func coordinatorClaimRecord(v CoordinatorToolSubmission) (string, []byte, []byte, error) { call := v.Call v.Call = runnerproto.CoordinatorToolCall{} raw, err := json.Marshal(v) if err != nil { return "", nil, nil, err } ref := runnerproto.EventHash(v) body, _ := json.Marshal(coordinatorToolRecord{ClaimRef: ref, Call: call}) return ref, raw, body, nil } // Upgrade old full-claim rows without dropping submitted or acknowledged calls. func compactCoordinatorTools(ctx context.Context, tx *sql.Tx) error { rows, err := tx.QueryContext(ctx, "SELECT request_key,body FROM coordinator_tools WHERE json_extract(body,'$.claim_ref') IS NULL") if err != nil { return err } type oldRow struct { key string raw []byte } var old []oldRow for rows.Next() { var v oldRow if err = rows.Scan(&v.key, &v.raw); err != nil { rows.Close() return err } old = append(old, v) } err = rows.Err() rows.Close() if err != nil { return err } for _, row := range old { var v CoordinatorToolSubmission if err = json.Unmarshal(row.raw, &v); err != nil { return err } ref, claim, body, err := coordinatorClaimRecord(v) if err != nil { return err } 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 { return err } if _, err = tx.ExecContext(ctx, "UPDATE coordinator_tools SET body=? WHERE request_key=?", body, row.key); err != nil { return err } } delivered, err := tx.QueryContext(ctx, "SELECT id FROM journal_events WHERE acked=1 AND json_extract(body,'$.kind')='coordinator.delivery'") if err != nil { return err } var ids []string for delivered.Next() { var id string if err = delivered.Scan(&id); err != nil { delivered.Close() return err } ids = append(ids, id) } err = delivered.Err() delivered.Close() if err != nil { return err } for _, id := range ids { if err = retireCoordinatorTools(ctx, tx, id); err != nil { return err } } return nil } func coordinatorDelivered(ctx context.Context, tx *sql.Tx, c runnerproto.CoordinatorClaim, s provider.Session) (bool, error) { var n int 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) return n > 0, err } func retireCoordinatorTools(ctx context.Context, tx *sql.Tx, id string) error { var raw []byte 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 { return err } var e runnerproto.Event if err := json.Unmarshal(raw, &e); err != nil { return err } if e.Kind != "coordinator.delivery" { return nil } c := runnerproto.CoordinatorClaim{Scope: e.Scope, Handle: e.Handle, Turn: runnerproto.CoordinatorTurn{ID: e.TurnID}} key := coordinatorTurnKey(c, provider.Session{ID: e.SessionID}) 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 { return err } _, err := tx.ExecContext(ctx, "DELETE FROM coordinator_tool_claims WHERE turn_key=?", key) return err }