22 // toolClipCharacters is the most characters of a tool output: its head
23 // and its tail, half each.
24 toolClipCharacters = 30000
26 // toolClipBytes is the most bytes that a tool keeps of each end of an
27 // output, enough for toolClipCharacters/2 characters of 4 bytes.
28 toolClipBytes = 4 * toolClipCharacters / 2
30 // toolShellWaitDelay is how long a command can keep its output open
31 // after it ends or after its kill.
32 toolShellWaitDelay = 2 * time.Second
35 // ToolLog is the log that the tools write their calls and results to.
36 type ToolLog interface {
37 Append(kind, text string) (Message, error)
38 Flush(ctx context.Context) error
41 // ToolChat gives the messages and the nodes that zoom and date read.
42 type ToolChat interface {
44 Message(id int) (Message, error)
45 Node(level, index int) (Node, bool, error)
48 // Toolbox runs the tool calls of one agent. Each call and each result goes to
49 // the log as it happens: the call as a tool message, the result as an echo
50 // message. The call is durable before its tool runs, so the log never misses an
51 // action outside the agent. Commands and files run as the user in the working
52 // directory, with no sandbox. Zoom with a name reads the log of a subagent.
53 // The toolbox of the agent has the runs of its subagents for spawn and tell.
54 // The toolbox of a subagent has none, so spawn and tell are unknown tools
64 // Call logs the call of the tool name with its JSON arguments, makes the log
65 // durable, runs the tool, and logs its clipped output, which it returns. A
66 // failed tool gives its error as the output. An error of Call itself is an
67 // error of the log. When ctx ends before the flush, the tool does not run.
68 func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, error) {
69 _, err := b.log.Append("tool", toolCallText(name, arguments))
74 err = b.log.Flush(ctx)
75 if err != nil && ctx.Err() == nil {
79 output := "not run: the turn was interrupted"
81 output = toolClip(b.run(ctx, name, arguments))
84 _, err = b.log.Append("echo", output)
92 // run runs one tool and returns its output, or its error as text.
93 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
98 var args ToolZoomArguments
99 err = toolDecodeArguments(arguments, &args)
102 case args.Name != nil:
103 output, err = b.zoomAgent(ctx, *args.Name)
105 output, err = b.zoom(args.ID, args.N)
108 var args ToolDateArguments
109 err = toolDecodeArguments(arguments, &args)
111 output, err = b.date(args.ID)
114 var args ToolShellArguments
115 err = toolDecodeArguments(arguments, &args)
117 output = b.shell(ctx, args.Command)
120 var args ToolPathArguments
121 err = toolDecodeArguments(arguments, &args)
123 output, err = b.read(args.Path)
126 var args ToolWriteArguments
127 err = toolDecodeArguments(arguments, &args)
129 output, err = b.write(args.Path, args.Text)
132 var args ToolEditArguments
133 err = toolDecodeArguments(arguments, &args)
135 output, err = b.edit(args.Path, args.Old, args.New)
138 var args ToolSpawnArguments
139 err = toolDecodeArguments(arguments, &args)
142 err = fmt.Errorf("unknown tool %q", name)
144 output, err = b.runs.Spawn(ctx, args.Tasks, args.Effort)
147 var args ToolTellArguments
148 err = toolDecodeArguments(arguments, &args)
151 err = fmt.Errorf("unknown tool %q", name)
153 output, err = b.runs.Tell(ctx, args.Name, args.Message)
156 err = fmt.Errorf("unknown tool %q", name)
160 return "error: " + err.Error()
166 // zoomAgent returns the whole log of the subagent name.
167 func (b *Toolbox) zoomAgent(ctx context.Context, name string) (string, error) {
168 subagent, err := b.subagents.Get(ctx, name)
173 return subagent.Show()
176 // zoom returns message id whole for n = 1, or else the two lines under the line
178 func (b *Toolbox) zoom(id, n int) (string, error) {
179 if n < 1 || bits.OnesCount(uint(n)) != 1 || id < 0 || id%n != 0 {
180 return "", fmt.Errorf("line %d+%d does not exist: n must be a power of 2 and id a multiple of n", id, n)
183 if id+n > b.chat.Len() {
184 return "", fmt.Errorf("line %d+%d goes past the last message, %d", id, n, b.chat.Len()-1)
188 message, err := b.chat.Message(id)
193 return leafSource(message), nil
196 level := bits.TrailingZeros(uint(n)) - 1
198 for _, index := range []int{2 * id / n, 2*id/n + 1} {
199 node, built, err := b.chat.Node(level, index)
206 text = strings.ReplaceAll(node.Text, "\n", " ")
209 lines = append(lines, fmt.Sprintf("%d+%d|%s", index<<level, 1<<level, text))
212 return strings.Join(lines, "\n"), nil
215 // date returns the date and time of message id, with its weekday.
216 func (b *Toolbox) date(id int) (string, error) {
217 if id < 0 || id >= b.chat.Len() {
218 return "", fmt.Errorf("message %d does not exist", id)
221 message, err := b.chat.Message(id)
226 date, err := time.Parse(time.RFC3339Nano, message.Date)
231 return date.Local().Format("Monday 2006-01-02 15:04:05 -07:00"), nil
234 // shell runs command with bash in the working directory and returns its output
235 // and error output. A failed command adds its exit status. When ctx ends, the
236 // whole process group of the command is killed.
237 func (b *Toolbox) shell(ctx context.Context, command string) string {
238 cmd := exec.CommandContext(ctx, "bash", "-c", command)
240 cmd.SysProcAttr = &syscall.SysProcAttr{
243 cmd.Cancel = func() error {
244 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
246 cmd.WaitDelay = toolShellWaitDelay
248 output := &ToolHeadTail{}
253 text := output.String()
254 var exit *exec.ExitError
256 case ctx.Err() != nil:
257 text += "\n[interrupted]"
258 case errors.As(err, &exit):
259 text += "\n[" + exit.String() + "]"
261 text += "\n[error: " + err.Error() + "]"
269 // read returns the text of the file at path.
270 func (b *Toolbox) read(path string) (text string, err error) {
271 file, err := os.Open(b.path(path))
277 err = errors.Join(err, file.Close())
280 output := &ToolHeadTail{}
281 _, err = io.Copy(output, file)
286 return output.String(), nil
289 // write replaces the file at path with text, and makes its missing
291 func (b *Toolbox) write(path, text string) (string, error) {
293 err := os.MkdirAll(filepath.Dir(path), 0o755)
298 err = os.WriteFile(path, []byte(text), 0o644)
303 return fmt.Sprintf("wrote %d bytes to %s", len(text), path), nil
306 // edit replaces the one occurrence of old in the file at path with
307 // replacement. It keeps the mode of the file.
308 func (b *Toolbox) edit(path, old, replacement string) (string, error) {
310 data, err := os.ReadFile(path)
316 return "", errors.New("old is empty")
319 count := strings.Count(string(data), old)
321 return "", fmt.Errorf("old occurs %d times in %s, not once", count, path)
324 info, err := os.Stat(path)
329 err = os.WriteFile(path, []byte(strings.Replace(string(data), old, replacement, 1)), info.Mode().Perm())
334 return "edited " + path, nil
337 // path returns path, relative to the working directory when it is not
339 func (b *Toolbox) path(path string) string {
340 if filepath.IsAbs(path) {
344 return filepath.Join(b.workdir, path)
347 // ToolZoomArguments are the arguments of zoom. A Name that is not nil names
348 // the subagent whose whole log zoom gives, and then ID and N are not used.
349 type ToolZoomArguments struct {
352 Name *string `json:"name"`
355 // ToolDateArguments are the arguments of date.
356 type ToolDateArguments struct {
360 // ToolShellArguments are the arguments of shell.
361 type ToolShellArguments struct {
362 Command string `json:"command"`
365 // ToolPathArguments are the arguments of read.
366 type ToolPathArguments struct {
367 Path string `json:"path"`
370 // ToolWriteArguments are the arguments of write.
371 type ToolWriteArguments struct {
372 Path string `json:"path"`
373 Text string `json:"text"`
376 // ToolEditArguments are the arguments of edit.
377 type ToolEditArguments struct {
378 Path string `json:"path"`
379 Old string `json:"old"`
380 New string `json:"new"`
383 // ToolSpawnArguments are the arguments of spawn. A nil Effort is the effort of
385 type ToolSpawnArguments struct {
386 Tasks []string `json:"tasks"`
387 Effort *string `json:"effort"`
390 // ToolTellArguments are the arguments of tell.
391 type ToolTellArguments struct {
392 Name string `json:"name"`
393 Message string `json:"message"`
396 // ToolHeadTail keeps the first and the last toolClipBytes bytes written to it,
397 // and counts the bytes between them that it drops.
398 type ToolHeadTail struct {
404 // String returns the output, clipped when it is too long. Invalid UTF-8 becomes
405 // the replacement character.
406 func (h *ToolHeadTail) String() string {
407 replacement := string(utf8.RuneError)
409 return toolClip(strings.ToValidUTF8(string(h.head)+string(h.tail), replacement))
412 head := toolFirstCharacters(strings.ToValidUTF8(string(h.head), replacement), toolClipCharacters/2)
413 tail := toolLastCharacters(strings.ToValidUTF8(string(h.tail), replacement), toolClipCharacters/2)
414 cut := len(h.head) + h.dropped + len(h.tail) - len(head) - len(tail)
416 return toolJoinClipped(head, tail, cut)
419 // Write keeps the bytes of p at the ends of the output.
420 func (h *ToolHeadTail) Write(p []byte) (int, error) {
422 room := min(toolClipBytes-len(h.head), len(p))
423 h.head = append(h.head, p[:room]...)
426 h.tail = append(h.tail, p...)
427 if len(h.tail) > toolClipBytes {
428 cut := len(h.tail) - toolClipBytes
430 h.tail = bytes.Clone(h.tail[cut:])
436 // toolCallText returns the log text of a tool call: the name and the JSON
438 func toolCallText(name, arguments string) string {
439 return name + " " + arguments
442 // toolClip returns text when it has at most toolClipCharacters characters.
443 // Otherwise it returns the first and the last toolClipCharacters/2 characters,
444 // with a mark of the cut between them.
445 func toolClip(text string) string {
446 if utf8.RuneCountInString(text) <= toolClipCharacters {
450 head := toolFirstCharacters(text, toolClipCharacters/2)
451 tail := toolLastCharacters(text, toolClipCharacters/2)
453 return toolJoinClipped(head, tail, len(text)-len(head)-len(tail))
456 // toolDecodeArguments decodes the JSON arguments of a call into args. An
457 // unknown field is an error.
458 func toolDecodeArguments(arguments string, args any) error {
459 decoder := json.NewDecoder(strings.NewReader(arguments))
460 decoder.DisallowUnknownFields()
461 err := decoder.Decode(args)
463 return fmt.Errorf("arguments: %w", err)
469 // toolFirstCharacters returns the first count characters of text.
470 func toolFirstCharacters(text string, count int) string {
473 if end >= len(text) {
477 _, size := utf8.DecodeRuneInString(text[end:])
484 // toolLastCharacters returns the last count characters of text.
485 func toolLastCharacters(text string, count int) string {
492 _, size := utf8.DecodeLastRuneInString(text[:start])
499 // toolJoinClipped joins the head and the tail of a clipped output with a mark
501 func toolJoinClipped(head, tail string, cut int) string {
502 return head + "\n[... " + strconv.Itoa(cut) + " bytes cut ...]\n" + tail