22 // clipCharacters is the most characters of a tool output: its head and
23 // its tail, half each.
24 clipCharacters = 30000
26 // clipBytes is the most bytes that a tool keeps of each end of an
27 // output, enough for clipCharacters/2 characters of 4 bytes.
28 clipBytes = 4 * clipCharacters / 2
30 // shellWaitDelay is how long a command can keep its output open after it
31 // ends or after its kill.
32 shellWaitDelay = 2 * time.Second
35 // chatLog is the log that the tools write their calls and results to.
36 type chatLog interface {
37 Append(kind, text string) (Message, error)
38 Flush(ctx context.Context) error
41 // chatReader gives the messages and the nodes that zoom and date read.
42 type chatReader 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
51 // an action outside the agent. Commands and files run as the user in the
52 // working directory, with no sandbox.
59 // shellArguments are the arguments of shell.
60 type shellArguments struct {
61 Command string `json:"command"`
64 // pathArguments are the arguments of read.
65 type pathArguments struct {
66 Path string `json:"path"`
69 // writeArguments are the arguments of write.
70 type writeArguments struct {
71 Path string `json:"path"`
72 Text string `json:"text"`
75 // editArguments are the arguments of edit.
76 type editArguments struct {
77 Path string `json:"path"`
78 Old string `json:"old"`
79 New string `json:"new"`
82 // zoomArguments are the arguments of zoom.
83 type zoomArguments struct {
88 // dateArguments are the arguments of date.
89 type dateArguments struct {
93 // Call logs the call of the tool name with its JSON arguments, makes the log
94 // durable, runs the tool, and logs its clipped output, which it returns. A
95 // failed tool gives its error as the output. An error of Call itself is an
96 // error of the log. When ctx ends before the flush, the tool does not run.
97 func (b *toolbox) Call(ctx context.Context, name, arguments string) (string, error) {
98 _, err := b.log.Append("tool", callText(name, arguments))
103 err = b.log.Flush(ctx)
104 if err != nil && ctx.Err() == nil {
108 output := "not run: the turn was interrupted"
110 output = clip(b.run(ctx, name, arguments))
113 _, err = b.log.Append("echo", output)
121 // run runs one tool and returns its output, or its error as text.
122 func (b *toolbox) run(ctx context.Context, name, arguments string) string {
127 var args zoomArguments
128 err = decodeArguments(arguments, &args)
130 output, err = b.zoom(args.ID, args.N)
133 var args dateArguments
134 err = decodeArguments(arguments, &args)
136 output, err = b.date(args.ID)
139 var args shellArguments
140 err = decodeArguments(arguments, &args)
142 output = b.shell(ctx, args.Command)
145 var args pathArguments
146 err = decodeArguments(arguments, &args)
148 output, err = b.read(args.Path)
151 var args writeArguments
152 err = decodeArguments(arguments, &args)
154 output, err = b.write(args.Path, args.Text)
157 var args editArguments
158 err = decodeArguments(arguments, &args)
160 output, err = b.edit(args.Path, args.Old, args.New)
163 err = fmt.Errorf("unknown tool %q", name)
167 return "error: " + err.Error()
173 // zoom returns message id whole for n = 1, or else the two lines under the line
175 func (b *toolbox) zoom(id, n int) (string, error) {
176 if n < 1 || bits.OnesCount(uint(n)) != 1 || id < 0 || id%n != 0 {
177 return "", fmt.Errorf("line %d+%d does not exist: n must be a power of 2 and id a multiple of n", id, n)
180 if id+n > b.chat.Len() {
181 return "", fmt.Errorf("line %d+%d goes past the last message, %d", id, n, b.chat.Len()-1)
185 message, err := b.chat.Message(id)
190 return leafSource(message), nil
193 level := bits.TrailingZeros(uint(n)) - 1
195 for _, index := range []int{2 * id / n, 2*id/n + 1} {
196 node, built, err := b.chat.Node(level, index)
203 text = strings.ReplaceAll(node.Text, "\n", " ")
206 lines = append(lines, fmt.Sprintf("%d+%d|%s", index<<level, 1<<level, text))
209 return strings.Join(lines, "\n"), nil
212 // date returns the date and time of message id, with its weekday.
213 func (b *toolbox) date(id int) (string, error) {
214 if id < 0 || id >= b.chat.Len() {
215 return "", fmt.Errorf("message %d does not exist", id)
218 message, err := b.chat.Message(id)
223 date, err := time.Parse(time.RFC3339Nano, message.Date)
228 return date.Local().Format("Monday 2006-01-02 15:04:05 -07:00"), nil
231 // shell runs command with bash in the working directory and returns its output
232 // and error output. A failed command adds its exit status. When ctx ends, the
233 // whole process group of the command is killed.
234 func (b *toolbox) shell(ctx context.Context, command string) string {
235 cmd := exec.CommandContext(ctx, "bash", "-c", command)
237 cmd.SysProcAttr = &syscall.SysProcAttr{
240 cmd.Cancel = func() error {
241 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
243 cmd.WaitDelay = shellWaitDelay
245 output := &headTail{}
250 text := output.String()
251 var exit *exec.ExitError
253 case ctx.Err() != nil:
254 text += "\n[interrupted]"
255 case errors.As(err, &exit):
256 text += "\n[" + exit.String() + "]"
258 text += "\n[error: " + err.Error() + "]"
266 // read returns the text of the file at path.
267 func (b *toolbox) read(path string) (text string, err error) {
268 file, err := os.Open(b.path(path))
274 err = errors.Join(err, file.Close())
277 output := &headTail{}
278 _, err = io.Copy(output, file)
283 return output.String(), nil
286 // write replaces the file at path with text, and makes its missing
288 func (b *toolbox) write(path, text string) (string, error) {
290 err := os.MkdirAll(filepath.Dir(path), 0o755)
295 err = os.WriteFile(path, []byte(text), 0o644)
300 return fmt.Sprintf("wrote %d bytes to %s", len(text), path), nil
303 // edit replaces the one occurrence of old in the file at path with
304 // replacement. It keeps the mode of the file.
305 func (b *toolbox) edit(path, old, replacement string) (string, error) {
307 data, err := os.ReadFile(path)
313 return "", errors.New("old is empty")
316 count := strings.Count(string(data), old)
318 return "", fmt.Errorf("old occurs %d times in %s, not once", count, path)
321 info, err := os.Stat(path)
326 err = os.WriteFile(path, []byte(strings.Replace(string(data), old, replacement, 1)), info.Mode().Perm())
331 return "edited " + path, nil
334 // path returns path, relative to the working directory when it is not
336 func (b *toolbox) path(path string) string {
337 if filepath.IsAbs(path) {
341 return filepath.Join(b.workdir, path)
344 // headTail keeps the first and the last clipBytes bytes written to it, and
345 // counts the bytes between them that it drops.
346 type headTail struct {
352 // Write keeps the bytes of p at the ends of the output.
353 func (h *headTail) Write(p []byte) (int, error) {
355 room := min(clipBytes-len(h.head), len(p))
356 h.head = append(h.head, p[:room]...)
359 h.tail = append(h.tail, p...)
360 if len(h.tail) > clipBytes {
361 cut := len(h.tail) - clipBytes
363 h.tail = bytes.Clone(h.tail[cut:])
369 // String returns the output, clipped when it is too long. Invalid UTF-8 becomes
370 // the replacement character.
371 func (h *headTail) String() string {
372 replacement := string(utf8.RuneError)
374 return clip(strings.ToValidUTF8(string(h.head)+string(h.tail), replacement))
377 head := firstCharacters(strings.ToValidUTF8(string(h.head), replacement), clipCharacters/2)
378 tail := lastCharacters(strings.ToValidUTF8(string(h.tail), replacement), clipCharacters/2)
379 cut := len(h.head) + h.dropped + len(h.tail) - len(head) - len(tail)
381 return joinClipped(head, tail, cut)
384 // callText returns the log text of a tool call: the name and the JSON
386 func callText(name, arguments string) string {
387 return name + " " + arguments
390 // decodeArguments decodes the JSON arguments of a call into args. An unknown
391 // field is an error.
392 func decodeArguments(arguments string, args any) error {
393 decoder := json.NewDecoder(strings.NewReader(arguments))
394 decoder.DisallowUnknownFields()
395 err := decoder.Decode(args)
397 return fmt.Errorf("arguments: %w", err)
403 // clip returns text when it has at most clipCharacters characters. Otherwise it
404 // returns the first and the last clipCharacters/2 characters, with a mark of
405 // the cut between them.
406 func clip(text string) string {
407 if utf8.RuneCountInString(text) <= clipCharacters {
411 head := firstCharacters(text, clipCharacters/2)
412 tail := lastCharacters(text, clipCharacters/2)
414 return joinClipped(head, tail, len(text)-len(head)-len(tail))
417 // joinClipped joins the head and the tail of a clipped output with a mark of
419 func joinClipped(head, tail string, cut int) string {
420 return head + "\n[... " + strconv.Itoa(cut) + " bytes cut ...]\n" + tail
423 // firstCharacters returns the first count characters of text.
424 func firstCharacters(text string, count int) string {
427 if end >= len(text) {
431 _, size := utf8.DecodeRuneInString(text[end:])
438 // lastCharacters returns the last count characters of text.
439 func lastCharacters(text string, count int) string {
446 _, size := utf8.DecodeLastRuneInString(text[:start])