12 // compactorTries is the most calls for one node.
13 const compactorTries = 5
15 // compactorRuler shows the model the size limit of a line. Models cannot count
16 // bytes, and a sample line as the ruler gets its content copied.
17 var compactorRuler = strings.Repeat("-", nodeBytes)
19 // newCompactor returns the compactor of an agent, which sends its calls through
20 // client with model. instructions gives the system prompt of the agent at each
21 // build. tools are the tools of that agent, so a build sends the same prompt
22 // and tools as its turns.
23 func newCompactor(client *ModelClient, instructions func() (string, error), model ModelConfig, tools []ModelTool) *Compactor {
27 instructions: instructions,
32 // Compactor builds the nodes of one agent with a model. A compaction is a call
33 // like a turn, with the same system prompt and tools, so it reads them from the
34 // prompt cache of the turns. The tools are never called. After them comes one
35 // user message with the compaction view up to the node, its closing tag, a
36 // newline, and the task.
38 // When the line is over nodeBytes, the next try sends the same message, plus
39 // one more user message with a note with the size of the newest line and its
40 // cut at the limit. A try never sends the model's earlier reply or reasoning
41 // back, so every try begins with the same view and task and has about the same
42 // size. After at most compactorTries tries, the shortest line is the node. A
43 // few bytes over the limit is fine, because the view measures the real sizes. A
44 // call that fails before any line fails the build, and the core tries it again
45 // at the next message.
46 type Compactor struct {
49 instructions func() (string, error)
53 // Summarize returns the line of the node of task.
54 func (c *Compactor) Summarize(ctx context.Context, task Task) (string, error) {
55 text, err := compactorTask(task)
60 instructions, err := c.instructions()
65 first := []any{modelUserMessage(task.Context + "\n" + text)}
70 for len(lines) < compactorTries {
73 input = append(slices.Clip(first), modelUserMessage(note))
76 var response ModelResponse
78 response, err = c.client.Create(ctx, ModelRequest{
80 Effort: c.model.Effort,
81 Instructions: instructions,
84 Node: &NodeKey{task.Level, task.Index},
90 line := strings.TrimSpace(response.Text)
92 err = errors.New("compaction gave no line")
96 lines = append(lines, line)
97 if len(line) <= nodeBytes {
101 note = compactorTooLong(line)
108 return compactorShortest(lines), nil
111 // compactorTask returns the task of a build, with its input.
112 func compactorTask(task Task) (string, error) {
113 if task.Level == 0 && len(task.Input) == 1 {
114 return fmt.Sprintf(`Compaction: compress message %d into one line of at most %d bytes
115 (about 70 words), the length of this ruler:
119 </input>`, task.Index, nodeBytes, compactorRuler, task.Input[0]), nil
122 if task.Level > 0 && len(task.Input) == 2 {
123 first := task.Index << task.Level
124 half := 1 << (task.Level - 1)
126 return fmt.Sprintf(`Compaction: merge lines %d+%d and %d+%d, adjacent, into one line of at most
127 %d bytes (about 70 words), the length of this ruler:
129 <chat> may hold their messages, %d to %d, in more detail: take details
130 of them from there too.
134 </input>`, first, half, first+half, half, nodeBytes, compactorRuler, first, nodeEnd(task.Level, task.Index)-1, task.Input[0], task.Input[1]), nil
137 return "", fmt.Errorf("node (%d, %d) has %d inputs", task.Level, task.Index, len(task.Input))
140 // compactorTooLong returns the note for a line over nodeBytes: its size and its
141 // first nodeBytes bytes, which show the model where the limit cuts it.
142 func compactorTooLong(line string) string {
143 return fmt.Sprintf(`Too long: your last line for this <input> was %d bytes,
144 over the %d-byte limit. Write the whole line again, cutting just
145 enough of the least valuable items to fit before this cut:
146 %s| <- LIMIT`, len(line), nodeBytes, compactorCutLine(line))
149 // compactorShortest returns the first of the compactorShortest lines.
150 func compactorShortest(lines []string) string {
152 for _, line := range lines[1:] {
153 if len(line) < len(best) {
161 // compactorCutLine returns the first nodeBytes bytes of line, without a part of
162 // a character at the end.
163 func compactorCutLine(line string) string {
164 if len(line) <= nodeBytes {
168 cut := line[:nodeBytes]
169 for !utf8.ValidString(cut) {
170 cut = cut[:len(cut)-1]