package thin import ( "context" "log/slog" "slices" "strings" "sync" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/wire" ) // moveInRetry is how soon the same request is repeated when nothing // came of it (a failed move-in, an instance that refused it). const moveInRetry = time.Hour // settler asks the instance for a move_in turn when a coordinator's desk // is empty or it never moved in, and for an explore turn when the // namespaces its agent reaches differ from those it explored. It runs // after each index refresh, so it never moves in on an empty catalog. type settler struct { State *State Link *Link Index *Index SessionRoot string Persons func() []string Logger *slog.Logger mu sync.Mutex sent map[string]sentRequest } type sentRequest struct { sig string at time.Time } // coordinatorPersons are the persons this runner keeps a coordinator for: // those with a Claude binding. func coordinatorPersons(c runner.Config) func() []string { return func() []string { bs, err := runner.LoadBindings(c.AccountBindingsPath) if err != nil { return nil } var out []string for _, b := range bs { if b.Provider == "claude" && b.PersonID != "" && !slices.Contains(out, b.PersonID) { out = append(out, b.PersonID) } } return out } } // withPerson adds the person the runner is enrolled for: before their // first connection they have no binding, and adding one must work. func withPerson(persons []string, person string) []string { if person != "" && !slices.Contains(persons, person) { persons = append(persons, person) } return persons } // Check sends what each person's desk needs, at most once per change // (and again after moveInRetry if nothing came of it). func (s *settler) Check(ctx context.Context) { if !s.Index.Ready() { return } ns := namespacesOf(s.Index.Catalog()) for _, person := range s.Persons() { desk, err := OpenDesk(s.SessionRoot, person) if err != nil { s.log(err) continue } st := desk.State() kind := "" switch { case !st.MovedIn || desk.Empty(): kind = wire.TurnMoveIn case !slices.Equal(st.Namespaces, ns): if s.foldAccess(ctx, person, st.Namespaces, ns) { // The durable approvals suffice across restarts. Do not // overwrite desk state while the resumed turn is taking notes. continue } kind = wire.TurnExplore } s.mu.Lock() if s.sent == nil { s.sent = map[string]sentRequest{} } prev, seen := s.sent[person] sig := kind + "|" + strings.Join(ns, ",") if kind == "" || seen && prev.sig == sig && time.Since(prev.at) < moveInRetry { if kind == "" { delete(s.sent, person) } s.mu.Unlock() continue } s.sent[person] = sentRequest{sig: sig, at: time.Now()} s.mu.Unlock() if err := s.State.SendMoveIn(ctx, wire.MoveIn{Person: person, Kind: kind}); err != nil { s.log(err) continue } s.Link.Notify() } } // forget drops what was asked for a person, so a reset asks again now. func (s *settler) forget(person string) { s.mu.Lock() delete(s.sent, person) s.mu.Unlock() } func (s *settler) log(err error) { if s.Logger != nil && err != nil { s.Logger.Warn("move in", "err", sanitize(err)) } }