| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner" |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // A lookup step (protocol 36; spec 2026-11-26-assistant-abilities §4.2 |
| 15 | // steps 3 and 6) is one isolated model call for the instance: the query |
| 16 | // or the answer. It runs as a glance does, on the person's model |
| 17 | // connection with the glance action (no tools, the small model), never |
| 18 | // resumed, with no notebook and no desk, in a directory made for it and |
| 19 | // removed when it returns. Only the stage, a code and byte counts are |
| 20 | // logged; the pack, the pages and the answer go nowhere but the reply. |
| 21 | |
| 22 | // LookupTimeout bounds one lookup step. |
| 23 | const LookupTimeout = 20 * time.Second |
| 24 | |
| 25 | // lookupRule versions the lookup prompts (wire.LookupPrompt). |
| 26 | const lookupRule = "lookup-2026-11-28.1" |
| 27 | |
| 28 | // lookupProfile is a lookup step's harness: the glance's action and tools |
| 29 | // (none), sessions never resumed. |
| 30 | var lookupProfile = laneProfile{action: provider.GlanceAction, rule: lookupRule, set: glanceSet, ephemeral: true} |
| 31 | |
| 32 | // OpenLookup opens a lookup step's harness for person on connection b, in |
| 33 | // a new directory under their state; the caller removes dir. The process |
| 34 | // starts on Warm. |
| 35 | func OpenLookup(o ClaudeOptions, x CodexOptions, person string, b runner.AccountBinding) (g Glancer, dir string, err error) { |
| 36 | o.defaults() |
| 37 | if b.PersonID != person { |
| 38 | return nil, "", provider.ErrIdentity |
| 39 | } |
| 40 | root := personDir(o.SessionRoot, person) |
| 41 | if err := os.MkdirAll(root, 0o700); err != nil { |
| 42 | return nil, "", err |
| 43 | } |
| 44 | if dir, err = os.MkdirTemp(root, "lookup-"); err != nil { |
| 45 | return nil, "", err |
| 46 | } |
| 47 | home := filepath.Join(dir, "home") |
| 48 | switch b.Provider { |
| 49 | case "claude": |
| 50 | c := &claudeCoordinator{} |
| 51 | if err := c.openAt(o, person, wire.LaneAssistant, home, lookupProfile, b); err != nil { |
| 52 | _ = os.RemoveAll(dir) |
| 53 | return nil, "", err |
| 54 | } |
| 55 | return c, dir, nil |
| 56 | case "codex": |
| 57 | x.defaults() |
| 58 | c := &codexCoordinator{x: x} |
| 59 | if err := c.openAt(o, person, wire.LaneAssistant, home, lookupProfile, b); err != nil { |
| 60 | _ = os.RemoveAll(dir) |
| 61 | return nil, "", err |
| 62 | } |
| 63 | return c, dir, nil |
| 64 | } |
| 65 | _ = os.RemoveAll(dir) |
| 66 | return nil, "", provider.ErrIdentity |
| 67 | } |
| 68 | |
| 69 | // Lookup runs one lookup step and its result: the stage's strict JSON, |
| 70 | // checked, or code lookup_failed. It takes at most LookupTimeout. |
| 71 | func (e *Engine) Lookup(ctx context.Context, s wire.LookupStep) wire.LookupResult { |
| 72 | start := time.Now() |
| 73 | out := wire.LookupResult{Lookup: s.Lookup, Attempt: s.Attempt, Nonce: s.Nonce, Stage: s.Stage} |
| 74 | timeout := e.LookupTimeout |
| 75 | if timeout == 0 { |
| 76 | timeout = LookupTimeout |
| 77 | } |
| 78 | ctx, cancel := context.WithTimeout(ctx, timeout) |
| 79 | defer cancel() |
| 80 | reply, code := e.lookup(ctx, s) |
| 81 | if code == "" { |
| 82 | if r, err := wire.ParseLookupReply(s, reply); err == nil { |
| 83 | out = r |
| 84 | } else { |
| 85 | code = wire.CodeLookupFailed |
| 86 | } |
| 87 | } |
| 88 | out.Code = code |
| 89 | e.debug("assistant lookup", "stage", s.Stage, "code", code, "bytes", len(reply), "ms", time.Since(start).Milliseconds()) |
| 90 | return out |
| 91 | } |
| 92 | |
| 93 | // lookup asks the model; code is set when it could not. |
| 94 | func (e *Engine) lookup(ctx context.Context, s wire.LookupStep) (reply, code string) { |
| 95 | if e.OpenLookup == nil || s.Person == "" { |
| 96 | return "", wire.CodeLookupFailed |
| 97 | } |
| 98 | prompt, err := wire.LookupPrompt(s) |
| 99 | if err != nil { |
| 100 | return "", wire.CodeLookupFailed |
| 101 | } |
| 102 | b, _, err := e.choose(ctx, s.Person, "", "") |
| 103 | if err != nil { |
| 104 | return "", wire.CodeLookupFailed |
| 105 | } |
| 106 | if e.Authorize != nil { |
| 107 | if err := e.Authorize(ctx); err != nil { |
| 108 | return "", wire.CodeLookupFailed |
| 109 | } |
| 110 | } |
| 111 | g, dir, err := e.OpenLookup(s.Person, b) |
| 112 | if err != nil { |
| 113 | return "", wire.CodeLookupFailed |
| 114 | } |
| 115 | defer func() { |
| 116 | g.Close() |
| 117 | _ = os.RemoveAll(dir) |
| 118 | }() |
| 119 | if err := g.Warm(ctx); err != nil { |
| 120 | return "", wire.CodeLookupFailed |
| 121 | } |
| 122 | text, err := g.Glance(ctx, prompt) |
| 123 | if err != nil { |
| 124 | return "", wire.CodeLookupFailed |
| 125 | } |
| 126 | return text, "" |
| 127 | } |
| 128 | |