| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | // A definitive response is a committed fence, with no unresolved containment. |
| 11 | // Authentication failures and failed lookups never establish that fact. |
| 12 | func (e *Engine) coordinatorRefusal(ctx context.Context, p RunnerPrincipal, id string, cause error) error { |
| 13 | if cause == nil { |
| 14 | return nil |
| 15 | } |
| 16 | fenced := false |
| 17 | err := e.Atomic(ctx, func(ctx context.Context, tx *gorm.DB) error { |
| 18 | if err := e.RunnerCurrent(tx, p); err != nil { |
| 19 | return err |
| 20 | } |
| 21 | var row TurnRecord |
| 22 | if err := tx.Where("id=? AND person=?", id, p.Person).First(&row).Error; err != nil { |
| 23 | return err |
| 24 | } |
| 25 | var b BindingRecord |
| 26 | if err := tx.First(&b, "id=? AND person=?", row.BindingID, p.Person).Error; err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if row.State == "delivered" || row.State == "uncertain" && b.Generation == row.Generation { |
| 30 | return nil |
| 31 | } |
| 32 | if row.State != "uncertain" && row.State != "superseded" && row.State != "fenced" && b.Generation == row.Generation { |
| 33 | if row.JobID == 0 && (row.State == "queued" || row.State == "admission_denied") { |
| 34 | return e.waitCoordinator(tx, b, row, "admission_denied") |
| 35 | } |
| 36 | return nil |
| 37 | } |
| 38 | if row.AttemptID != 0 { |
| 39 | var containment string |
| 40 | if err := tx.Table("attempts").Select("containment").Where("id=?", row.AttemptID).Take(&containment).Error; err != nil { |
| 41 | return err |
| 42 | } |
| 43 | if containment != "contained" { |
| 44 | return nil |
| 45 | } |
| 46 | } |
| 47 | if row.State != "fenced" { |
| 48 | if err := tx.Model(&TurnRecord{}).Where("id=?", id).Select("state", "revision").Updates(map[string]any{"state": "fenced", "revision": gorm.Expr("revision+1")}).Error; err != nil { |
| 49 | return err |
| 50 | } |
| 51 | if err := tx.Model(&Main{}).Where("id=?", row.ConversationID).UpdateColumn("revision", gorm.Expr("revision+1")).Error; err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if err := e.Audit(tx, b, "coordinator.fenced", id); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | } |
| 58 | fenced = true |
| 59 | return nil |
| 60 | }) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | if fenced { |
| 65 | return &runnerproto.RefusalError{Status: 409, Reason: "coordinator.fenced"} |
| 66 | } |
| 67 | return cause |
| 68 | } |
| 69 | func (e *Engine) ClaimCoordinator(ctx context.Context, p RunnerPrincipal, id string, revision int64) (CoordinatorClaim, error) { |
| 70 | c, err := e.claimCoordinator(ctx, p, id, revision) |
| 71 | return c, e.coordinatorRefusal(ctx, p, id, err) |
| 72 | } |
| 73 | func (e *Engine) RecoverCoordinator(ctx context.Context, p RunnerPrincipal, id string, revision int64) (CoordinatorClaim, error) { |
| 74 | c, err := e.recoverCoordinator(ctx, p, id, revision) |
| 75 | return c, e.coordinatorRefusal(ctx, p, id, err) |
| 76 | } |
| 77 | func (e *Engine) ContainCoordinator(ctx context.Context, p RunnerPrincipal, c CoordinatorClaim, proof runnerproto.ContainmentProof) error { |
| 78 | return e.coordinatorRefusal(ctx, p, c.Turn.ID, e.containCoordinator(ctx, p, c, proof)) |
| 79 | } |
| 80 | func (e *Engine) CoordinatorEvent(ctx context.Context, p RunnerPrincipal, c CoordinatorClaim, event runnerproto.Event) error { |
| 81 | return e.coordinatorRefusal(ctx, p, c.Turn.ID, e.coordinatorEvent(ctx, p, c, event)) |
| 82 | } |
| 83 | func (e *Engine) CoordinatorToolResult(ctx context.Context, p RunnerPrincipal, c CoordinatorClaim, call CoordinatorToolCall) (CoordinatorToolResult, error) { |
| 84 | out, err := e.coordinatorToolResult(ctx, p, c, call) |
| 85 | return out, e.coordinatorRefusal(ctx, p, c.Turn.ID, err) |
| 86 | } |
| 87 | |