package thin import ( "context" "os" "path/filepath" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/wire" ) // A lookup step (protocol 36; spec 2026-11-26-assistant-abilities ยง4.2 // steps 3 and 6) is one isolated model call for the instance: the query // or the answer. It runs as a glance does, on the person's model // connection with the glance action (no tools, the small model), never // resumed, with no notebook and no desk, in a directory made for it and // removed when it returns. Only the stage, a code and byte counts are // logged; the pack, the pages and the answer go nowhere but the reply. // LookupTimeout bounds one lookup step. const LookupTimeout = 20 * time.Second // lookupRule versions the lookup prompts (wire.LookupPrompt). const lookupRule = "lookup-2026-11-28.1" // lookupProfile is a lookup step's harness: the glance's action and tools // (none), sessions never resumed. var lookupProfile = laneProfile{action: provider.GlanceAction, rule: lookupRule, set: glanceSet, ephemeral: true} // OpenLookup opens a lookup step's harness for person on connection b, in // a new directory under their state; the caller removes dir. The process // starts on Warm. func OpenLookup(o ClaudeOptions, x CodexOptions, person string, b runner.AccountBinding) (g Glancer, dir string, err error) { o.defaults() if b.PersonID != person { return nil, "", provider.ErrIdentity } root := personDir(o.SessionRoot, person) if err := os.MkdirAll(root, 0o700); err != nil { return nil, "", err } if dir, err = os.MkdirTemp(root, "lookup-"); err != nil { return nil, "", err } home := filepath.Join(dir, "home") switch b.Provider { case "claude": c := &claudeCoordinator{} if err := c.openAt(o, person, wire.LaneAssistant, home, lookupProfile, b); err != nil { _ = os.RemoveAll(dir) return nil, "", err } return c, dir, nil case "codex": x.defaults() c := &codexCoordinator{x: x} if err := c.openAt(o, person, wire.LaneAssistant, home, lookupProfile, b); err != nil { _ = os.RemoveAll(dir) return nil, "", err } return c, dir, nil } _ = os.RemoveAll(dir) return nil, "", provider.ErrIdentity } // Lookup runs one lookup step and its result: the stage's strict JSON, // checked, or code lookup_failed. It takes at most LookupTimeout. func (e *Engine) Lookup(ctx context.Context, s wire.LookupStep) wire.LookupResult { start := time.Now() out := wire.LookupResult{Lookup: s.Lookup, Attempt: s.Attempt, Nonce: s.Nonce, Stage: s.Stage} timeout := e.LookupTimeout if timeout == 0 { timeout = LookupTimeout } ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() reply, code := e.lookup(ctx, s) if code == "" { if r, err := wire.ParseLookupReply(s, reply); err == nil { out = r } else { code = wire.CodeLookupFailed } } out.Code = code e.debug("assistant lookup", "stage", s.Stage, "code", code, "bytes", len(reply), "ms", time.Since(start).Milliseconds()) return out } // lookup asks the model; code is set when it could not. func (e *Engine) lookup(ctx context.Context, s wire.LookupStep) (reply, code string) { if e.OpenLookup == nil || s.Person == "" { return "", wire.CodeLookupFailed } prompt, err := wire.LookupPrompt(s) if err != nil { return "", wire.CodeLookupFailed } b, _, err := e.choose(ctx, s.Person, "", "") if err != nil { return "", wire.CodeLookupFailed } if e.Authorize != nil { if err := e.Authorize(ctx); err != nil { return "", wire.CodeLookupFailed } } g, dir, err := e.OpenLookup(s.Person, b) if err != nil { return "", wire.CodeLookupFailed } defer func() { g.Close() _ = os.RemoveAll(dir) }() if err := g.Warm(ctx); err != nil { return "", wire.CodeLookupFailed } text, err := g.Glance(ctx, prompt) if err != nil { return "", wire.CodeLookupFailed } return text, "" }