package thin import ( "context" "time" "amadan.net/oficina/equipo/internal/wire" ) // Warming the Assistant (Paul's live check, 2026-11-14): after a release // the Assistant's rule changes, so its old session cannot resume, and // the first message waited for a fresh provider process and session // before the model saw a word. On every sync with the instance (the // runner's start, a reconnect, and so the first start under a new rule) // the runner now starts that process and session ahead of the turn, for // each person who used their Assistant lately. Nothing is sent to the // model: Claude starts and waits for its first user frame; Codex // initializes and starts or resumes its thread, which are app-server // requests, not turns. // warmWithin is how recent an Assistant's use must be to warm it: its // session saved, or a turn of its run, in that time. const warmWithin = 7 * 24 * time.Hour // warmLimit bounds one warm-up (the lease check and the start). const warmLimit = time.Minute // WarmAssistants asks the Assistant worker of each person who used it // within warmWithin to start its harness, if it is not up already. func (e *Engine) WarmAssistants() { e.warmAssistants("") } // WarmAssistant does the same for one person, when the instance says // they opened Base (its warm command, protocol 24), so the harness is up // before they type: only an Assistant they used within warmWithin, and // the glance only while a glance slot is free (warmGlance). func (e *Engine) WarmAssistant(person string) { if person != "" { e.warmAssistants(person) } } // warmAssistants warms every recent user's Assistant, or when only is // set that one person's. func (e *Engine) warmAssistants(only string) { if e == nil { return } e.mu.Lock() ctx := e.ctx e.mu.Unlock() if ctx == nil || ctx.Err() != nil { return } persons, err := e.State.assistantUsers(ctx, time.Now().Add(-warmWithin)) if err != nil { e.log("runner warm", err) return } e.mu.Lock() defer e.mu.Unlock() for _, p := range persons { if only != "" && p != only { continue } w := e.worker(workerKey{p, wire.LaneAssistant}) select { case w.warm <- struct{}{}: default: } } } // warmOne starts the lane's harness on the connection a turn would take // now, with the execution lease confirmed, as a turn would. No // connection, no lease or a harness that cannot warm: nothing happens, // and the first turn starts it as before. func (e *Engine) warmOne(k workerKey, w *worker) { ctx, cancel := context.WithTimeout(e.ctx, warmLimit) defer cancel() conn := connKey(k.Person, k.Lane) b, _, err := e.choose(ctx, k.Person, conn, w.connID) if err != nil { return } if e.Authorize != nil && e.Authorize(ctx) != nil { return } if err := e.seat(ctx, k.Person, k.Lane, conn, w, b); err != nil { e.log("coordinator open", err) return } h, ok := w.coord.(Warmer) if !ok { return } if u, ok := w.coord.(interface{ Up() bool }); ok && u.Up() { // Up already (a turn ran, or a sync or Base's opening warmed // it): only the glance may still need warming. if k.Lane == wire.LaneAssistant { e.warmGlance(k.Person, w, b) } return } start := time.Now() err = h.Warm(ctx) e.log("runner warm", err) if err == nil && k.Lane == wire.LaneAssistant { e.warmGlance(k.Person, w, b) } if e.Logger != nil { outcome := "warm" if err != nil { outcome = "failed" } e.Logger.Debug("assistant warm", "outcome", outcome, "ms", time.Since(start).Milliseconds()) } }