| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/coordinator" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "gorm.io/gorm" |
| 10 | ) |
| 11 | |
| 12 | // RecoverCoordinator returns only the original claim for reconciliation. It |
| 13 | // neither renews admission nor grants permission to start a provider process. |
| 14 | func (e *Engine) recoverCoordinator(ctx context.Context, p RunnerPrincipal, id string, revision int64) (CoordinatorClaim, error) { |
| 15 | var out CoordinatorClaim |
| 16 | err := e.Atomic(ctx, func(ctx context.Context, tx *gorm.DB) error { |
| 17 | if err := e.RunnerCurrent(tx, p); err != nil { |
| 18 | return err |
| 19 | } |
| 20 | var row TurnRecord |
| 21 | if err := tx.Where("id=? AND person=?", id, p.Person).First(&row).Error; err != nil { |
| 22 | return err |
| 23 | } |
| 24 | if row.State == "fenced" || row.State == "superseded" { |
| 25 | return e.Conflict |
| 26 | } |
| 27 | if row.State == "uncertain" && row.AttemptID != 0 { |
| 28 | var containment string |
| 29 | if err := tx.Table("attempts").Select("containment").Where("id=?", row.AttemptID).Take(&containment).Error; err != nil { |
| 30 | return err |
| 31 | } |
| 32 | if containment == "contained" { |
| 33 | return e.Conflict |
| 34 | } |
| 35 | } |
| 36 | if row.Claim == "" { |
| 37 | return nil |
| 38 | } |
| 39 | if err := json.Unmarshal([]byte(row.Claim), &out); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | if out.Handle.Runner != p.RunnerID || out.Turn.Revision != revision { |
| 43 | return e.Forbidden |
| 44 | } |
| 45 | return nil |
| 46 | }) |
| 47 | return out, err |
| 48 | } |
| 49 | func (e *Engine) coordinatorToolResult(ctx context.Context, p RunnerPrincipal, c CoordinatorClaim, call CoordinatorToolCall) (CoordinatorToolResult, error) { |
| 50 | var out CoordinatorToolResult |
| 51 | canonical, err := coordinator.Canonical(call) |
| 52 | if err != nil { |
| 53 | return out, err |
| 54 | } |
| 55 | err = e.Atomic(ctx, func(ctx context.Context, tx *gorm.DB) error { |
| 56 | _, row, err := e.validateCoordinatorClaim(ctx, tx, p, c) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | args, err := coordinator.Validate(canonical) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | if err = e.toolScope(tx, e.Conversation(tx), row, c.Brief.Selection, args); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | var saved requestRecord |
| 68 | err = tx.Where("principal=? AND operation='coordinator.tool' AND resource=? AND request_id=?", p.Person, toolResource(c, row), call.RequestID).First(&saved).Error |
| 69 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 70 | return nil |
| 71 | } |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | hash, err := kernel.InputHash(canonical) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if hash != saved.InputHash { |
| 80 | return e.Conflict |
| 81 | } |
| 82 | return json.Unmarshal([]byte(saved.Result), &out) |
| 83 | }) |
| 84 | return out, err |
| 85 | } |
| 86 | |