| 1 | // Package chatstate maintains replaceable conversation display state. |
| 2 | package chatstate |
| 3 | |
| 4 | import ( |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Only unclaimed work can be coalesced. Active and uncertain attempts retain |
| 14 | // their fences; superseded messages remain in the next turn's history. |
| 15 | func Coalesce(tx *gorm.DB, binding string, generation int64, audit func(string) error) error { |
| 16 | const waiting = "state IN ('queued','waiting_connection','waiting_task','budget_needed','admission_denied')" |
| 17 | var newest struct { |
| 18 | ID string |
| 19 | Sequence int64 |
| 20 | } |
| 21 | err := tx.Table("coordinator_turns").Where("binding_id=? AND generation=? AND "+waiting, binding, generation).Order("sequence DESC").Take(&newest).Error |
| 22 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 23 | return nil |
| 24 | } |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | older := tx.Table("coordinator_turns").Where("binding_id=? AND generation=? AND "+waiting+" AND sequence<?", binding, generation, newest.Sequence) |
| 29 | var ids []string |
| 30 | if err := older.Pluck("id", &ids).Error; err != nil { |
| 31 | return err |
| 32 | } |
| 33 | if len(ids) == 0 { |
| 34 | return nil |
| 35 | } |
| 36 | var represented []string |
| 37 | if err := tx.Table("coordinator_turns").Where("binding_id=? AND generation=? AND superseded_by IN ?", binding, generation, ids).Pluck("id", &represented).Error; err != nil { |
| 38 | return err |
| 39 | } |
| 40 | ids = append(ids, represented...) |
| 41 | if err := tx.Table("coordinator_turns").Where("id IN ?", ids).Select("state", "revision", "superseded_by").Updates(map[string]any{"state": "superseded", "revision": gorm.Expr("revision+1"), "superseded_by": newest.ID}).Error; err != nil { |
| 42 | return err |
| 43 | } |
| 44 | for _, id := range ids { |
| 45 | if err := audit(id); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | func ApplyText(tx *gorm.DB, id string, sequence, at int64, now time.Time, event runnerproto.Event, redact func([]byte) []byte, invalid error) error { |
| 52 | var text runnerproto.CoordinatorText |
| 53 | if json.Unmarshal(event.Payload, &text) != nil || text.Validate() != nil || event.Sequence == nil || *event.Sequence <= sequence || *event.Sequence > runnerproto.MaxCoordinatorTextEvents || now.UnixMilli()-at < 100 { |
| 54 | return invalid |
| 55 | } |
| 56 | if err := json.Unmarshal(redact(event.Payload), &text); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | return tx.Table("coordinator_turns").Where("id=?", id).Select("partial_text", "partial_sequence", "partial_at").Updates(map[string]any{"partial_text": text.Text, "partial_sequence": *event.Sequence, "partial_at": now.UnixMilli()}).Error |
| 60 | } |
| 61 | |
| 62 | // Reusing a pending decision brings its card beside the current proposing reply. |
| 63 | func AnchorProjects(tx *gorm.DB, person, binding string, generation int64, conversation, body string, sequence int64) error { |
| 64 | var pending []struct{ ID, Repository string } |
| 65 | if err := tx.Table("project_proposals").Where("person=? AND binding_id=? AND generation=? AND state='pending'", person, binding, generation).Find(&pending).Error; err != nil { |
| 66 | return err |
| 67 | } |
| 68 | for _, p := range pending { |
| 69 | if !strings.Contains(body, p.Repository) { |
| 70 | continue |
| 71 | } |
| 72 | if err := tx.Table("conversation_cards").Where("stream_id=? AND kind='project_setup' AND resource_id=?", conversation, p.ID).Select("sequence").Updates(map[string]any{"sequence": sequence}).Error; err != nil { |
| 73 | return err |
| 74 | } |
| 75 | } |
| 76 | return nil |
| 77 | } |
| 78 | |