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. The
54 // toolbox of a subagent has none, so spawn is an unknown tool there.
63 // Call logs the call of the tool name with its JSON arguments, makes the log
64 // durable, runs the tool, and logs its clipped output, which it returns. A
65 // failed tool gives its error as the output. An error of Call itself is an
66 // error of the log. When ctx ends before the flush, the tool does not run.
67 func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, error) {
68 _, err := b.log.Append("tool", toolCallText(name, arguments))
73 err = b.log.Flush(ctx)
74 if err != nil && ctx.Err() == nil {
78 output := "not run: the turn was interrupted"
80 output = toolClip(b.run(ctx, name, arguments))
83 _, err = b.log.Append("echo", output)
91 // run runs one tool and returns its output, or its error as text.
92 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
97 var args ToolZoomArguments
98 err = toolDecodeArguments(arguments, &args)
101 case args.Name != nil:
102 output, err = b.zoomAgent(ctx, *args.Name)
104 output, err = b.zoom(args.ID, args.N)
107 var args ToolDateArguments
108 err = toolDecodeArguments(arguments, &args)
110 output, err = b.date(args.ID)
113 var args ToolShellArguments
114 err = toolDecodeArguments(arguments, &args)
116 output = b.shell(ctx, args.Command)
119 var args ToolPathArguments
120 err = toolDecodeArguments(arguments, &args)
122 output, err = b.read(args.Path)
125 var args ToolWriteArguments
126 err = toolDecodeArguments(arguments, &args)
128 output, err = b.write(args.Path, args.Text)
131 var args ToolEditArguments
132 err = toolDecodeArguments(arguments, &args)
134 output, err = b.edit(args.Path, args.Old, args.New)
137 var args ToolSpawnArguments
138 err = toolDecodeArguments(arguments, &args)
141 err = fmt.Errorf("unknown tool %q", name)
143 output, err = b.runs.Spawn(ctx, args.Tasks, args.Effort)
146 err = fmt.Errorf("unknown tool %q", name)
150 return "error: " + err.Error()
156 // zoomAgent returns the whole log of the subagent name.
157 func (b *Toolbox) zoomAgent(ctx context.Context, name string) (string, error) {
158 subagent, err := b.subagents.Get(ctx, name)
163 return subagent.Show()
166 // zoom returns message id whole for n = 1, or else the two lines under the line
168 func (b *Toolbox) zoom(id, n int) (string, error) {
169 if n < 1 || bits.OnesCount(uint(n)) != 1 || id < 0 || id%n != 0 {
170 return "", fmt.Errorf("line %d+%d does not exist: n must be a power of 2 and id a multiple of n", id, n)
173 if id+n > b.chat.Len() {
174 return "", fmt.Errorf("line %d+%d goes past the last message, %d", id, n, b.chat.Len()-1)
178 message, err := b.chat.Message(id)
183 return leafSource(message), nil
186 level := bits.TrailingZeros(uint(n)) - 1
188 for _, index := range []int{2 * id / n, 2*id/n + 1} {
189 node, built, err := b.chat.Node(level, index)
196 text = strings.ReplaceAll(node.Text, "\n", " ")
199 lines = append(lines, fmt.Sprintf("%d+%d|%s", index<<level, 1<<level, text))
202 return strings.Join(lines, "\n"), nil
205 // date returns the date and time of message id, with its weekday.
206 func (b *Toolbox) date(id int) (string, error) {
207 if id < 0 || id >= b.chat.Len() {
208 return "", fmt.Errorf("message %d does not exist", id)
211 message, err := b.chat.Message(id)
216 date, err := time.Parse(time.RFC3339Nano, message.Date)
221 return date.Local().Format("Monday 2006-01-02 15:04:05 -07:00"), nil
224 // shell runs command with bash in the working directory and returns its output
225 // and error output. A failed command adds its exit status. When ctx ends, the
226 // whole process group of the command is killed.
227 func (b *Toolbox) shell(ctx context.Context, command string) string {
228 cmd := exec.CommandContext(ctx, "bash", "-c", command)
230 cmd.SysProcAttr = &syscall.SysProcAttr{
233 cmd.Cancel = func() error {
234 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
236 cmd.WaitDelay = toolShellWaitDelay
238 output := &ToolHeadTail{}
243 text := output.String()
244 var exit *exec.ExitError
246 case ctx.Err() != nil:
247 text += "\n[interrupted]"
248 case errors.As(err, &exit):
249 text += "\n[" + exit.String() + "]"
251 text += "\n[error: " + err.Error() + "]"
259 // read returns the text of the file at path.
260 func (b *Toolbox) read(path string) (text string, err error) {
261 file, err := os.Open(b.path(path))
267 err = errors.Join(err, file.Close())
270 output := &ToolHeadTail{}
271 _, err = io.Copy(output, file)
276 return output.String(), nil
279 // write replaces the file at path with text, and makes its missing
281 func (b *Toolbox) write(path, text string) (string, error) {
283 err := os.MkdirAll(filepath.Dir(path), 0o755)
288 err = os.WriteFile(path, []byte(text), 0o644)
293 return fmt.Sprintf("wrote %d bytes to %s", len(text), path), nil
296 // edit replaces the one occurrence of old in the file at path with
297 // replacement. It keeps the mode of the file.
298 func (b *Toolbox) edit(path, old, replacement string) (string, error) {
300 data, err := os.ReadFile(path)
306 return "", errors.New("old is empty")
309 count := strings.Count(string(data), old)
311 return "", fmt.Errorf("old occurs %d times in %s, not once", count, path)
314 info, err := os.Stat(path)
319 err = os.WriteFile(path, []byte(strings.Replace(string(data), old, replacement, 1)), info.Mode().Perm())
324 return "edited " + path, nil
327 // path returns path, relative to the working directory when it is not
329 func (b *Toolbox) path(path string) string {
330 if filepath.IsAbs(path) {
334 return filepath.Join(b.workdir, path)
337 // ToolZoomArguments are the arguments of zoom. A Name that is not nil names
338 // the subagent whose whole log zoom gives, and then ID and N are not used.
339 type ToolZoomArguments struct {
342 Name *string `json:"name"`
345 // ToolDateArguments are the arguments of date.
346 type ToolDateArguments struct {
350 // ToolShellArguments are the arguments of shell.
351 type ToolShellArguments struct {
352 Command string `json:"command"`
355 // ToolPathArguments are the arguments of read.
356 type ToolPathArguments struct {
357 Path string `json:"path"`
360 // ToolWriteArguments are the arguments of write.
361 type ToolWriteArguments struct {
362 Path string `json:"path"`
363 Text string `json:"text"`
366 // ToolEditArguments are the arguments of edit.
367 type ToolEditArguments struct {
368 Path string `json:"path"`
369 Old string `json:"old"`
370 New string `json:"new"`
373 // ToolSpawnArguments are the arguments of spawn. A nil Effort is the effort of
375 type ToolSpawnArguments struct {
376 Tasks []string `json:"tasks"`
377 Effort *string `json:"effort"`
380 // ToolHeadTail keeps the first and the last toolClipBytes bytes written to it,
381 // and counts the bytes between them that it drops.
382 type ToolHeadTail struct {
388 // String returns the output, clipped when it is too long. Invalid UTF-8 becomes
389 // the replacement character.
390 func (h *ToolHeadTail) String() string {
391 replacement := string(utf8.RuneError)
393 return toolClip(strings.ToValidUTF8(string(h.head)+string(h.tail), replacement))
396 head := toolFirstCharacters(strings.ToValidUTF8(string(h.head), replacement), toolClipCharacters/2)
397 tail := toolLastCharacters(strings.ToValidUTF8(string(h.tail), replacement), toolClipCharacters/2)
398 cut := len(h.head) + h.dropped + len(h.tail) - len(head) - len(tail)
400 return toolJoinClipped(head, tail, cut)
403 // Write keeps the bytes of p at the ends of the output.
404 func (h *ToolHeadTail) Write(p []byte) (int, error) {
406 room := min(toolClipBytes-len(h.head), len(p))
407 h.head = append(h.head, p[:room]...)
410 h.tail = append(h.tail, p...)
411 if len(h.tail) > toolClipBytes {
412 cut := len(h.tail) - toolClipBytes
414 h.tail = bytes.Clone(h.tail[cut:])
420 // toolCallText returns the log text of a tool call: the name and the JSON
422 func toolCallText(name, arguments string) string {
423 return name + " " + arguments
426 // toolClip returns text when it has at most toolClipCharacters characters.
427 // Otherwise it returns the first and the last toolClipCharacters/2 characters,
428 // with a mark of the cut between them.
429 func toolClip(text string) string {
430 if utf8.RuneCountInString(text) <= toolClipCharacters {
434 head := toolFirstCharacters(text, toolClipCharacters/2)
435 tail := toolLastCharacters(text, toolClipCharacters/2)
437 return toolJoinClipped(head, tail, len(text)-len(head)-len(tail))
440 // toolDecodeArguments decodes the JSON arguments of a call into args. An
441 // unknown field is an error.
442 func toolDecodeArguments(arguments string, args any) error {
443 decoder := json.NewDecoder(strings.NewReader(arguments))
444 decoder.DisallowUnknownFields()
445 err := decoder.Decode(args)
447 return fmt.Errorf("arguments: %w", err)
453 // toolFirstCharacters returns the first count characters of text.
454 func toolFirstCharacters(text string, count int) string {
457 if end >= len(text) {
461 _, size := utf8.DecodeRuneInString(text[end:])
468 // toolLastCharacters returns the last count characters of text.
469 func toolLastCharacters(text string, count int) string {
476 _, size := utf8.DecodeLastRuneInString(text[:start])
483 // toolJoinClipped joins the head and the tail of a clipped output with a mark
485 func toolJoinClipped(head, tail string, cut int) string {
486 return head + "\n[... " + strconv.Itoa(cut) + " bytes cut ...]\n" + tail