| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log/slog" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // moveInRetry is how soon the same request is repeated when nothing |
| 16 | // came of it (a failed move-in, an instance that refused it). |
| 17 | const moveInRetry = time.Hour |
| 18 | |
| 19 | // settler asks the instance for a move_in turn when a coordinator's desk |
| 20 | // is empty or it never moved in, and for an explore turn when the |
| 21 | // namespaces its agent reaches differ from those it explored. It runs |
| 22 | // after each index refresh, so it never moves in on an empty catalog. |
| 23 | type settler struct { |
| 24 | State *State |
| 25 | Link *Link |
| 26 | Index *Index |
| 27 | SessionRoot string |
| 28 | Persons func() []string |
| 29 | Logger *slog.Logger |
| 30 | |
| 31 | mu sync.Mutex |
| 32 | sent map[string]sentRequest |
| 33 | } |
| 34 | |
| 35 | type sentRequest struct { |
| 36 | sig string |
| 37 | at time.Time |
| 38 | } |
| 39 | |
| 40 | // coordinatorPersons are the persons this runner keeps a coordinator for: |
| 41 | // those with a Claude binding. |
| 42 | func coordinatorPersons(c runner.Config) func() []string { |
| 43 | return func() []string { |
| 44 | bs, err := runner.LoadBindings(c.AccountBindingsPath) |
| 45 | if err != nil { |
| 46 | return nil |
| 47 | } |
| 48 | var out []string |
| 49 | for _, b := range bs { |
| 50 | if b.Provider == "claude" && b.PersonID != "" && !slices.Contains(out, b.PersonID) { |
| 51 | out = append(out, b.PersonID) |
| 52 | } |
| 53 | } |
| 54 | return out |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // withPerson adds the person the runner is enrolled for: before their |
| 59 | // first connection they have no binding, and adding one must work. |
| 60 | func withPerson(persons []string, person string) []string { |
| 61 | if person != "" && !slices.Contains(persons, person) { |
| 62 | persons = append(persons, person) |
| 63 | } |
| 64 | return persons |
| 65 | } |
| 66 | |
| 67 | // Check sends what each person's desk needs, at most once per change |
| 68 | // (and again after moveInRetry if nothing came of it). |
| 69 | func (s *settler) Check(ctx context.Context) { |
| 70 | if !s.Index.Ready() { |
| 71 | return |
| 72 | } |
| 73 | ns := namespacesOf(s.Index.Catalog()) |
| 74 | for _, person := range s.Persons() { |
| 75 | desk, err := OpenDesk(s.SessionRoot, person) |
| 76 | if err != nil { |
| 77 | s.log(err) |
| 78 | continue |
| 79 | } |
| 80 | st := desk.State() |
| 81 | kind := "" |
| 82 | switch { |
| 83 | case !st.MovedIn || desk.Empty(): |
| 84 | kind = wire.TurnMoveIn |
| 85 | case !slices.Equal(st.Namespaces, ns): |
| 86 | if s.foldAccess(ctx, person, st.Namespaces, ns) { |
| 87 | // The durable approvals suffice across restarts. Do not |
| 88 | // overwrite desk state while the resumed turn is taking notes. |
| 89 | continue |
| 90 | } |
| 91 | kind = wire.TurnExplore |
| 92 | } |
| 93 | s.mu.Lock() |
| 94 | if s.sent == nil { |
| 95 | s.sent = map[string]sentRequest{} |
| 96 | } |
| 97 | prev, seen := s.sent[person] |
| 98 | sig := kind + "|" + strings.Join(ns, ",") |
| 99 | if kind == "" || seen && prev.sig == sig && time.Since(prev.at) < moveInRetry { |
| 100 | if kind == "" { |
| 101 | delete(s.sent, person) |
| 102 | } |
| 103 | s.mu.Unlock() |
| 104 | continue |
| 105 | } |
| 106 | s.sent[person] = sentRequest{sig: sig, at: time.Now()} |
| 107 | s.mu.Unlock() |
| 108 | if err := s.State.SendMoveIn(ctx, wire.MoveIn{Person: person, Kind: kind}); err != nil { |
| 109 | s.log(err) |
| 110 | continue |
| 111 | } |
| 112 | s.Link.Notify() |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // forget drops what was asked for a person, so a reset asks again now. |
| 117 | func (s *settler) forget(person string) { |
| 118 | s.mu.Lock() |
| 119 | delete(s.sent, person) |
| 120 | s.mu.Unlock() |
| 121 | } |
| 122 | |
| 123 | func (s *settler) log(err error) { |
| 124 | if s.Logger != nil && err != nil { |
| 125 | s.Logger.Warn("move in", "err", sanitize(err)) |
| 126 | } |
| 127 | } |
| 128 | |