| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // Warming the Assistant (Paul's live check, 2026-11-14): after a release |
| 11 | // the Assistant's rule changes, so its old session cannot resume, and |
| 12 | // the first message waited for a fresh provider process and session |
| 13 | // before the model saw a word. On every sync with the instance (the |
| 14 | // runner's start, a reconnect, and so the first start under a new rule) |
| 15 | // the runner now starts that process and session ahead of the turn, for |
| 16 | // each person who used their Assistant lately. Nothing is sent to the |
| 17 | // model: Claude starts and waits for its first user frame; Codex |
| 18 | // initializes and starts or resumes its thread, which are app-server |
| 19 | // requests, not turns. |
| 20 | |
| 21 | // warmWithin is how recent an Assistant's use must be to warm it: its |
| 22 | // session saved, or a turn of its run, in that time. |
| 23 | const warmWithin = 7 * 24 * time.Hour |
| 24 | |
| 25 | // warmLimit bounds one warm-up (the lease check and the start). |
| 26 | const warmLimit = time.Minute |
| 27 | |
| 28 | // WarmAssistants asks the Assistant worker of each person who used it |
| 29 | // within warmWithin to start its harness, if it is not up already. |
| 30 | func (e *Engine) WarmAssistants() { e.warmAssistants("") } |
| 31 | |
| 32 | // WarmAssistant does the same for one person, when the instance says |
| 33 | // they opened Base (its warm command, protocol 24), so the harness is up |
| 34 | // before they type: only an Assistant they used within warmWithin, and |
| 35 | // the glance only while a glance slot is free (warmGlance). |
| 36 | func (e *Engine) WarmAssistant(person string) { |
| 37 | if person != "" { |
| 38 | e.warmAssistants(person) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // warmAssistants warms every recent user's Assistant, or when only is |
| 43 | // set that one person's. |
| 44 | func (e *Engine) warmAssistants(only string) { |
| 45 | if e == nil { |
| 46 | return |
| 47 | } |
| 48 | e.mu.Lock() |
| 49 | ctx := e.ctx |
| 50 | e.mu.Unlock() |
| 51 | if ctx == nil || ctx.Err() != nil { |
| 52 | return |
| 53 | } |
| 54 | persons, err := e.State.assistantUsers(ctx, time.Now().Add(-warmWithin)) |
| 55 | if err != nil { |
| 56 | e.log("runner warm", err) |
| 57 | return |
| 58 | } |
| 59 | e.mu.Lock() |
| 60 | defer e.mu.Unlock() |
| 61 | for _, p := range persons { |
| 62 | if only != "" && p != only { |
| 63 | continue |
| 64 | } |
| 65 | w := e.worker(workerKey{p, wire.LaneAssistant}) |
| 66 | select { |
| 67 | case w.warm <- struct{}{}: |
| 68 | default: |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // warmOne starts the lane's harness on the connection a turn would take |
| 74 | // now, with the execution lease confirmed, as a turn would. No |
| 75 | // connection, no lease or a harness that cannot warm: nothing happens, |
| 76 | // and the first turn starts it as before. |
| 77 | func (e *Engine) warmOne(k workerKey, w *worker) { |
| 78 | ctx, cancel := context.WithTimeout(e.ctx, warmLimit) |
| 79 | defer cancel() |
| 80 | conn := connKey(k.Person, k.Lane) |
| 81 | b, _, err := e.choose(ctx, k.Person, conn, w.connID) |
| 82 | if err != nil { |
| 83 | return |
| 84 | } |
| 85 | if e.Authorize != nil && e.Authorize(ctx) != nil { |
| 86 | return |
| 87 | } |
| 88 | if err := e.seat(ctx, k.Person, k.Lane, conn, w, b); err != nil { |
| 89 | e.log("coordinator open", err) |
| 90 | return |
| 91 | } |
| 92 | h, ok := w.coord.(Warmer) |
| 93 | if !ok { |
| 94 | return |
| 95 | } |
| 96 | if u, ok := w.coord.(interface{ Up() bool }); ok && u.Up() { |
| 97 | // Up already (a turn ran, or a sync or Base's opening warmed |
| 98 | // it): only the glance may still need warming. |
| 99 | if k.Lane == wire.LaneAssistant { |
| 100 | e.warmGlance(k.Person, w, b) |
| 101 | } |
| 102 | return |
| 103 | } |
| 104 | start := time.Now() |
| 105 | err = h.Warm(ctx) |
| 106 | e.log("runner warm", err) |
| 107 | if err == nil && k.Lane == wire.LaneAssistant { |
| 108 | e.warmGlance(k.Person, w, b) |
| 109 | } |
| 110 | if e.Logger != nil { |
| 111 | outcome := "warm" |
| 112 | if err != nil { |
| 113 | outcome = "failed" |
| 114 | } |
| 115 | e.Logger.Debug("assistant warm", "outcome", outcome, "ms", time.Since(start).Milliseconds()) |
| 116 | } |
| 117 | } |
| 118 | |