11 // compactionTries is the most calls for one node.
12 const compactionTries = 5
14 // ruler shows the model the size limit of a line. Models cannot count bytes,
15 // and a sample line as the ruler gets its content copied.
16 var ruler = strings.Repeat("-", nodeBytes)
18 // Compactor builds the nodes of one agent with a model. A compaction is a call
19 // like a turn, with the same system prompt and tools, so it reads them from the
20 // prompt cache of the turns. The tools are never called. After them comes one
21 // user message with the compaction view up to the node and the task.
23 // When the line is over nodeBytes, the next try sends the same view and task,
24 // plus a note with the size of the newest line and its cut at the limit. A try
25 // never sends the model's earlier reply or reasoning back, so every try has the
26 // same cached prefix and about the same size. After at most compactionTries
27 // tries, the shortest line is the node. A few bytes over the limit is fine,
28 // because the view measures the real sizes. A call that fails before any line
29 // fails the build, and the core tries it again at the next message.
30 type Compactor struct {
33 instructions func() (string, error)
37 // newCompactor returns the compactor of an agent, which sends its calls through
38 // client with model. instructions gives the system prompt of the agent at each
39 // build, so a build sends the same prompt as the turns of that time.
40 func newCompactor(client *ModelClient, instructions func() (string, error), model ModelConfig) *Compactor {
44 instructions: instructions,
49 // Summarize returns the line of the node of task.
50 func (c *Compactor) Summarize(ctx context.Context, task Task) (string, error) {
51 view := task.Context + "\n"
52 text, err := compactionTask(task)
57 instructions, err := c.instructions()
59 return "", fmt.Errorf("compact node (%d, %d): %w", task.Level, task.Index, err)
64 for len(lines) < compactionTries {
65 parts := []string{view, text}
67 parts = append(parts, note)
70 var response ModelResponse
71 response, err = c.client.Create(ctx, ModelRequest{
73 Effort: c.model.Effort,
74 Instructions: instructions,
76 Input: []any{modelUserMessage(parts...)},
77 Node: &nodeKey{task.Level, task.Index},
83 line := strings.TrimSpace(response.Text)
85 err = errors.New("compaction gave no line")
89 lines = append(lines, line)
90 if len(line) <= nodeBytes {
98 return "", fmt.Errorf("compact node (%d, %d): %w", task.Level, task.Index, err)
101 return shortest(lines), nil
104 // compactionTask returns the task of a build, with its input.
105 func compactionTask(task Task) (string, error) {
106 if task.Level == 0 && len(task.Input) == 1 {
107 return fmt.Sprintf(`Compaction: compress message %d into one line of at most %d bytes
108 (about 70 words), the length of this ruler:
112 </input>`, task.Index, nodeBytes, ruler, task.Input[0]), nil
115 if task.Level > 0 && len(task.Input) == 2 {
116 first := task.Index << task.Level
117 half := 1 << (task.Level - 1)
119 return fmt.Sprintf(`Compaction: merge lines %d+%d and %d+%d, adjacent, into one line of at most
120 %d bytes (about 70 words), the length of this ruler:
122 <chat> may hold their messages, %d to %d, in more detail: take details
123 of them from there too.
127 </input>`, first, half, first+half, half, nodeBytes, ruler, first, nodeEnd(task.Level, task.Index)-1, task.Input[0], task.Input[1]), nil
130 return "", fmt.Errorf("node (%d, %d) has %d inputs", task.Level, task.Index, len(task.Input))
133 // tooLong returns the note for a line over nodeBytes: its size and its first
134 // nodeBytes bytes, which show the model where the limit cuts it.
135 func tooLong(line string) string {
136 return fmt.Sprintf(`Too long: your last line for this <input> was %d bytes,
137 over the %d-byte limit. Write the whole line again, cutting just
138 enough of the least valuable items to fit before this cut:
139 %s| <- LIMIT`, len(line), nodeBytes, cutLine(line))
142 // cutLine returns the first nodeBytes bytes of line, without a part of a
143 // character at the end.
144 func cutLine(line string) string {
145 if len(line) <= nodeBytes {
149 cut := line[:nodeBytes]
150 for !utf8.ValidString(cut) {
151 cut = cut[:len(cut)-1]
157 // shortest returns the first of the shortest lines.
158 func shortest(lines []string) string {
160 for _, line := range lines[1:] {
161 if len(line) < len(best) {