| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strconv" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | "gorm.io/gorm" |
| 11 | ) |
| 12 | |
| 13 | // The lookups' live channel (spec 2026-11-26-assistant-abilities §4.2 |
| 14 | // step 3; plan AD8): a lookup step goes to the person's primary runner on |
| 15 | // its current connection only, never to runner_commands, and its answer |
| 16 | // is accepted only from that connection, for that lookup, attempt, nonce |
| 17 | // and stage. A connection that ends fails the steps waiting on it; a |
| 18 | // step isn't resent. |
| 19 | |
| 20 | // lookupStepWait is the most a lookup step is waited for. |
| 21 | const lookupStepWait = 20 * time.Second |
| 22 | |
| 23 | // liveConn is one runner connection's live channel. |
| 24 | type liveConn struct { |
| 25 | runner string |
| 26 | protocol int |
| 27 | send func(ctx context.Context, f wire.Frame) error |
| 28 | done chan struct{} |
| 29 | } |
| 30 | |
| 31 | // lookupWaiter is a lookup step waiting on its answer. |
| 32 | type lookupWaiter struct { |
| 33 | conn *liveConn |
| 34 | ch chan wire.LookupResult |
| 35 | } |
| 36 | |
| 37 | // lookupLive is the store's live channels and waiters. |
| 38 | type lookupLive struct { |
| 39 | mu sync.Mutex |
| 40 | conns map[string]*liveConn |
| 41 | waiters map[string]*lookupWaiter |
| 42 | seq int64 |
| 43 | } |
| 44 | |
| 45 | func lookupKey(lookup string, attempt int, nonce, stage string) string { |
| 46 | return lookup + "\x00" + strconv.Itoa(attempt) + "\x00" + nonce + "\x00" + stage |
| 47 | } |
| 48 | |
| 49 | // attachLive makes runner's connection, at protocol, the one its lookup |
| 50 | // steps go to, until detach: then every step waiting on it fails. c is |
| 51 | // the connection's handle, which its answers come back with. |
| 52 | func (s *Store) attachLive(runner string, protocol int, send func(context.Context, wire.Frame) error) (c *liveConn, detach func()) { |
| 53 | c = &liveConn{runner: runner, protocol: protocol, send: send, done: make(chan struct{})} |
| 54 | l := &s.live |
| 55 | l.mu.Lock() |
| 56 | if l.conns == nil { |
| 57 | l.conns, l.waiters = map[string]*liveConn{}, map[string]*lookupWaiter{} |
| 58 | } |
| 59 | if old := l.conns[runner]; old != nil { |
| 60 | l.end(old) |
| 61 | } |
| 62 | l.conns[runner] = c |
| 63 | l.mu.Unlock() |
| 64 | return c, func() { |
| 65 | l.mu.Lock() |
| 66 | defer l.mu.Unlock() |
| 67 | if l.conns[runner] == c { |
| 68 | delete(l.conns, runner) |
| 69 | l.end(c) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // end ends c: its waiters go (their steps fail on done), so nothing that |
| 75 | // comes on it later counts. l.mu is held. |
| 76 | func (l *lookupLive) end(c *liveConn) { |
| 77 | for key, w := range l.waiters { |
| 78 | if w.conn == c { |
| 79 | delete(l.waiters, key) |
| 80 | } |
| 81 | } |
| 82 | close(c.done) |
| 83 | } |
| 84 | |
| 85 | // deliverLookup hands a lookup_result that came on connection c to the |
| 86 | // step waiting on it there; anything else (stale, from another |
| 87 | // connection, unasked) is dropped. |
| 88 | func (s *Store) deliverLookup(c *liveConn, r wire.LookupResult) { |
| 89 | l := &s.live |
| 90 | l.mu.Lock() |
| 91 | defer l.mu.Unlock() |
| 92 | key := lookupKey(r.Lookup, r.Attempt, r.Nonce, r.Stage) |
| 93 | w := l.waiters[key] |
| 94 | if w == nil || c == nil || w.conn != c || l.conns[c.runner] != c { |
| 95 | return |
| 96 | } |
| 97 | delete(l.waiters, key) |
| 98 | w.ch <- r |
| 99 | } |
| 100 | |
| 101 | // lookupStep sends step to the person's primary runner, live, at |
| 102 | // protocol 36 or later, and waits at most lookupStepWait for its answer. |
| 103 | // No primary, an older one, a connection that ends or no answer in time |
| 104 | // is code lookup_failed. The nonce is the store's. |
| 105 | func (s *Store) lookupStep(ctx context.Context, person string, step wire.LookupStep) (wire.LookupResult, error) { |
| 106 | failed := wire.LookupResult{Lookup: step.Lookup, Attempt: step.Attempt, Stage: step.Stage, Code: wire.CodeLookupFailed} |
| 107 | var runner string |
| 108 | if err := s.read(ctx, func(tx *gorm.DB) error { |
| 109 | var err error |
| 110 | runner, err = s.authority.PrimaryRunner(tx, person) |
| 111 | return err |
| 112 | }); err != nil || runner == "" { |
| 113 | return failed, err |
| 114 | } |
| 115 | l := &s.live |
| 116 | l.mu.Lock() |
| 117 | c := l.conns[runner] |
| 118 | if c == nil || c.protocol < wire.ProtocolLookup { |
| 119 | l.mu.Unlock() |
| 120 | return failed, nil |
| 121 | } |
| 122 | l.seq++ |
| 123 | step.Person, step.Nonce = person, newID("n")+strconv.FormatInt(l.seq, 36) |
| 124 | failed.Nonce = step.Nonce |
| 125 | key := lookupKey(step.Lookup, step.Attempt, step.Nonce, step.Stage) |
| 126 | w := &lookupWaiter{conn: c, ch: make(chan wire.LookupResult, 1)} |
| 127 | l.waiters[key] = w |
| 128 | l.mu.Unlock() |
| 129 | defer func() { |
| 130 | l.mu.Lock() |
| 131 | if l.waiters[key] == w { |
| 132 | delete(l.waiters, key) |
| 133 | } |
| 134 | l.mu.Unlock() |
| 135 | }() |
| 136 | f, err := wire.NewFrame("lk"+newID(""), wire.FrameLookupStep, step) |
| 137 | if err != nil { |
| 138 | return failed, err |
| 139 | } |
| 140 | // The runner's frame budget, checked as sent. |
| 141 | if len(f.Data) > wire.MaxLookupStepBytes { |
| 142 | return failed, nil |
| 143 | } |
| 144 | wctx, cancel := context.WithTimeout(ctx, lookupStepWait) |
| 145 | defer cancel() |
| 146 | if err := c.send(wctx, f); err != nil { |
| 147 | return failed, nil |
| 148 | } |
| 149 | select { |
| 150 | case r := <-w.ch: |
| 151 | return r, nil |
| 152 | case <-c.done: |
| 153 | return failed, nil |
| 154 | case <-wctx.Done(): |
| 155 | return failed, nil |
| 156 | } |
| 157 | } |
| 158 | |