oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "time"
8
9 "gorm.io/gorm"
10
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// A Design attempt's render records (spec 2026-11-28-dynamic-specialists.md,
15// section 4, "Audit": each render's counts and timing on the step;
16// protocol 38): the runner keeps one per accepted render_page call and
17// sends them on the attempt's reports (wire.StepReport.Renders). They are
18// kept here as plain data, per step attempt: evidence of what ran, never
19// authority for anything.
20
21// stepRenderRecord is a step attempt's render records (JSON, a list of
22// wire.RenderAudit the wire validated).
23type stepRenderRecord struct {
24 StepID string `gorm:"primaryKey"`
25 Attempt int64 `gorm:"primaryKey;autoIncrement:false"`
26 Renders string `gorm:"not null;default:''"`
27 UpdatedAt time.Time
28}
29
30func (stepRenderRecord) TableName() string { return "chat_step_renders" }
31
32// keepRenders keeps a step report's render records, each for the attempt
33// it belongs to (its own Attempt), replacing what was kept for that
34// attempt before. The report must be of a step of its task bound to a
35// specialist, its person's, from the runner it is placed on, for the
36// step's current attempt or the one that advances it (a failover: the
37// runner's row carries the ending attempt's records, and at the autonomy
38// limit nothing more of that attempt is taken), and the wire must take
39// the records (wire.ValidRenderAudits). Only records of attempts the
40// step has reached are kept: none of an attempt that never ran. Anything
41// else keeps nothing and refuses nothing: the report goes on as it would
42// without them.
43func (t *txn) keepRenders(l Lease, v *wire.StepReport) error {
44 if len(v.Renders) == 0 || wire.ValidRenderAudits(v.Renders, v.Attempt) != nil {
45 return nil
46 }
47 var s stepRecord
48 err := t.db.Where("id = ? AND task_id = ?", v.StepID, v.TaskID).First(&s).Error
49 if errors.Is(err, gorm.ErrRecordNotFound) {
50 return nil
51 }
52 if err != nil {
53 return err
54 }
55 if s.SpecialistID == "" || s.Person != l.Runner.Person || s.RunnerID != l.Runner.RunnerID || v.Attempt != s.Attempt && v.Attempt != s.Attempt+1 {
56 return nil
57 }
58 byAttempt := map[int64][]wire.RenderAudit{}
59 var attempts []int64
60 for _, rec := range v.Renders {
61 if rec.Attempt > s.Attempt {
62 continue
63 }
64 if _, ok := byAttempt[rec.Attempt]; !ok {
65 attempts = append(attempts, rec.Attempt)
66 }
67 byAttempt[rec.Attempt] = append(byAttempt[rec.Attempt], rec)
68 }
69 for _, a := range attempts {
70 raw, err := json.Marshal(byAttempt[a])
71 if err != nil {
72 return err
73 }
74 if err := t.db.Save(&stepRenderRecord{StepID: s.ID, Attempt: a, Renders: string(raw), UpdatedAt: t.now()}).Error; err != nil {
75 return err
76 }
77 }
78 return nil
79}
80
81// stepRenders are the render records kept for a step's attempt.
82func (s *Store) stepRenders(ctx context.Context, step string, attempt int64) ([]wire.RenderAudit, error) {
83 var rec stepRenderRecord
84 err := s.db.G.WithContext(ctx).Where("step_id = ? AND attempt = ?", step, attempt).First(&rec).Error
85 if errors.Is(err, gorm.ErrRecordNotFound) {
86 return nil, nil
87 }
88 if err != nil {
89 return nil, err
90 }
91 var out []wire.RenderAudit
92 return out, json.Unmarshal([]byte(rec.Renders), &out)
93}
94