18 // probeName is the agent name of a probe run. The cache key comes from
23 // probe runs "onechat probe LOG COUNT [BUILDS]". It appends the first COUNT
24 // messages of LOG, one JSON object {kind, text} per line, to a new chat in
25 // probe/ in the data directory, and builds its tree with the real compactor.
26 // Before each user message it waits for the builds, as a turn does. It stops at
27 // the first failed call, after buildCallLimit calls, or when the used percent
28 // of a quota window grows by more than buildQuotaLimit points. At the end it
29 // reports the calls, the time per call, the calls at once, the cached share,
30 // the quota, the retries, and the line sizes. BUILDS, from 1 to buildMax,
31 // limits the builds that call the model at once in this run only. The core
32 // still queues up to buildMax builds. The run holds the lock of the token file,
33 // so it does not run while the daemon runs.
34 func probe(ctx context.Context, args []string, out io.Writer) (err error) {
35 if len(args) != 2 && len(args) != 3 {
36 return errors.New("usage: onechat probe LOG COUNT [BUILDS]")
39 count, err := strconv.Atoi(args[1])
41 return fmt.Errorf("parse probe count: %w", err)
45 return fmt.Errorf("COUNT %q is not a positive number", args[1])
50 builds, err = strconv.Atoi(args[2])
52 return fmt.Errorf("parse probe builds: %w", err)
55 if builds < 1 || builds > buildMax {
56 return fmt.Errorf("BUILDS %q is not a number from 1 to %d", args[2], buildMax)
60 entries, err := probeReadLog(args[0], count)
62 return fmt.Errorf("read probe input: %w", err)
67 return fmt.Errorf("find probe data directory: %w", err)
70 config, err := loadConfig(dir)
72 return fmt.Errorf("load probe configuration: %w", err)
75 tokens, err := lockTokens(dir)
77 return fmt.Errorf("lock probe tokens: %w", err)
81 closeErr := tokens.Close()
83 err = errors.Join(err, fmt.Errorf("close probe token lock: %w", closeErr))
87 address, auth, err := modelEndpoint(config, dir)
89 return fmt.Errorf("select probe endpoint: %w", err)
92 root := filepath.Join(dir, "probe", time.Now().Format("20060102-150405"))
93 err = os.MkdirAll(root, 0o700)
95 return fmt.Errorf("create probe directory: %w", err)
98 usagePath := filepath.Join(root, "usage.jsonl")
99 client := newModelClient(address, auth, "onechat-"+probeName, usagePath)
100 client.LimitCalls(buildCallLimit)
102 summarizer := ProbeSlotSummarizer{
103 inner: newCompactor(client, probePrompt, config.Compactor),
104 slots: make(chan struct{}, builds),
107 // The report of the probe counts the failed calls, so the lines of the
108 // failed builds are not needed.
109 memory, err := openMemory(ctx, root, time.Now, summarizer, log.New(io.Discard, "", 0))
111 return fmt.Errorf("open probe memory: %w", err)
114 _, err = fmt.Fprintf(out, "probe: %d messages into %s with %s at %s effort, %d builds at once\n", len(entries), root, config.Compactor.Model, config.Compactor.Effort, builds)
116 return fmt.Errorf("write probe header: %w", errors.Join(err, memory.Close()))
122 usagePath: usagePath,
125 appended, stop := run.Run(ctx, entries)
126 sizes, err := memory.buildModelLines()
127 err = errors.Join(err, memory.Close())
129 return fmt.Errorf("finish probe memory: %w", err)
132 records, err := buildReadUsage(usagePath)
134 return fmt.Errorf("read probe usage: %w", err)
137 err = buildWriteReport(out, appended, stop, records, sizes)
139 return fmt.Errorf("write probe report: %w", err)
145 // ProbeSlotSummarizer lets at most cap(slots) builds call inner at once. A
146 // build past the limit waits for a slot.
147 type ProbeSlotSummarizer struct {
152 // Summarize waits for a slot and builds the node with inner.
153 func (s ProbeSlotSummarizer) Summarize(ctx context.Context, task Task) (string, error) {
155 case s.slots <- struct{}{}:
164 return s.inner.Summarize(ctx, task)
167 // probeReadLog reads the first count messages of the probe log at path. Source
168 // dates are ignored because a probe uses its own clock.
169 func probeReadLog(path string, count int) (entries []DatedMessage, err error) {
170 file, err := os.Open(path)
172 return nil, fmt.Errorf("open probe log: %w", err)
176 closeErr := file.Close()
178 err = errors.Join(err, fmt.Errorf("close probe log: %w", closeErr))
182 scanner := bufio.NewScanner(file)
183 scanner.Buffer(make([]byte, 64*1024), buildLineBytes)
185 for len(entries) < count && scanner.Scan() {
186 var entry DatedMessage
188 err = json.Unmarshal(scanner.Bytes(), &entry)
190 return nil, fmt.Errorf("probe log line %d: %w", len(entries)+1, err)
194 entries = append(entries, entry)
199 return nil, fmt.Errorf("read probe log: %w", err)
202 if len(entries) < count {
203 return nil, fmt.Errorf("probe log has %d messages, fewer than %d", len(entries), count)
209 // probePrompt returns the system prompt of a probe run: the prompt of the agent
210 // probeName, without instructions and with a fixed instructions file path.
211 func probePrompt() (string, error) {
212 return prompt(probeName, "/PROBE/instructions.md", ""), nil