bots-garden/mini-mepublic Fork
⑂ 3d09337
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

♻️ Quality: agentsmd, ask and detector without smells (3 → 0)

- agentsmd.Load with unreadable and classify
- ask: the child's elicitation answer as elicitResponse
- detector.record as consecutive, seen, flagFailure, remember, stuck

Checked: go test, and the loop detector byte-identical old vs new on
1600 random actions over 4 configurations (flags, stuck, reports, loop
errors, resets).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
agent committed 2026-11-28T12:44:02+01:00 Browse files
3d09337 parent: 84d5b8e
modified internal/agentsmd/agentsmd.go +17 -6
@@ -73,19 +73,30 @@ func Load(workDir string) File {
7373 }
7474 path := resolve(workDir, settings.Path)
7575 data, err := os.ReadFile(path)
76- switch {
77- case errors.Is(err, fs.ErrNotExist):
76+ if err != nil {
77+ return unreadable(path, err)
78+ }
79+ return classify(path, strings.TrimSpace(string(data)), settings.MaxChars)
80+}
81+
82+// unreadable is the file that could not be read: missing, or the error.
83+func unreadable(path string, err error) File {
84+ if errors.Is(err, fs.ErrNotExist) {
7885 return File{Status: Missing, Path: path}
79- case err != nil:
80- return File{Status: Unreadable, Path: path, Err: err}
8186 }
82- text := strings.TrimSpace(string(data))
87+ return File{Status: Unreadable, Path: path, Err: err}
88+}
89+
90+// classify says what becomes of the file's text: nothing when it is empty,
91+// ignored by a structured or decision agent, skipped when longer than
92+// maxChars (0: no limit), loaded otherwise.
93+func classify(path, text string, maxChars int) File {
8394 switch {
8495 case text == "":
8596 return File{Status: Missing, Path: path}
8697 case config.Cfg.OutputSchema != nil || config.Cfg.Decision != nil:
8798 return File{Status: Ignored, Path: path, Text: text}
88- case settings.MaxChars > 0 && utf8.RuneCountInString(text) > settings.MaxChars:
99+ case maxChars > 0 && utf8.RuneCountInString(text) > maxChars:
89100 return File{Status: Skipped, Path: path, Text: text}
90101 }
91102 return File{Status: Loaded, Path: path, Text: text}
@@ -73,19 +73,30 @@ func Load(workDir string) File {
73 }73 }
74 path := resolve(workDir, settings.Path)74 path := resolve(workDir, settings.Path)
75 data, err := os.ReadFile(path)75 data, err := os.ReadFile(path)
76- switch {76+ if err != nil {
77- case errors.Is(err, fs.ErrNotExist):77+ return unreadable(path, err)
78+ }
79+ return classify(path, strings.TrimSpace(string(data)), settings.MaxChars)
80+}
81+
82+// unreadable is the file that could not be read: missing, or the error.
83+func unreadable(path string, err error) File {
84+ if errors.Is(err, fs.ErrNotExist) {
78 return File{Status: Missing, Path: path}85 return File{Status: Missing, Path: path}
79- case err != nil:
80- return File{Status: Unreadable, Path: path, Err: err}
81 }86 }
82- text := strings.TrimSpace(string(data))87+ return File{Status: Unreadable, Path: path, Err: err}
88+}
89+
90+// classify says what becomes of the file's text: nothing when it is empty,
91+// ignored by a structured or decision agent, skipped when longer than
92+// maxChars (0: no limit), loaded otherwise.
93+func classify(path, text string, maxChars int) File {
83 switch {94 switch {
84 case text == "":95 case text == "":
85 return File{Status: Missing, Path: path}96 return File{Status: Missing, Path: path}
86 case config.Cfg.OutputSchema != nil || config.Cfg.Decision != nil:97 case config.Cfg.OutputSchema != nil || config.Cfg.Decision != nil:
87 return File{Status: Ignored, Path: path, Text: text}98 return File{Status: Ignored, Path: path, Text: text}
88- case settings.MaxChars > 0 && utf8.RuneCountInString(text) > settings.MaxChars:99+ case maxChars > 0 && utf8.RuneCountInString(text) > maxChars:
89 return File{Status: Skipped, Path: path, Text: text}100 return File{Status: Skipped, Path: path, Text: text}
90 }101 }
91 return File{Status: Loaded, Path: path, Text: text}102 return File{Status: Loaded, Path: path, Text: text}
modified internal/ask/elicit.go +6 -1
@@ -44,7 +44,12 @@ func (c *client) UnstableCreateElicitation(ctx context.Context, p sdk.UnstableCr
4444 form.Questions[i].Prompt = agent + " asks: " + form.Questions[i].Prompt
4545 }
4646 ev := ui.ToolEvent{ID: c.shownID(childToolCall(p.Form.Meta)), Tool: agent + "/ask_user", Title: agent + " · ask_user: " + p.Form.Message, Kind: "other"}
47- res := asker.Elicit(ctx, ev, form)
47+ return elicitResponse(form, asker.Elicit(ctx, ev, form))
48+}
49+
50+// elicitResponse is the asker's result as the child's answer: accepted with
51+// the content, declined, or cancelled; nobody to ask otherwise.
52+func elicitResponse(form elicit.Form, res elicit.Result) (sdk.UnstableCreateElicitationResponse, error) {
4853 switch res.Status {
4954 case elicit.Answered:
5055 return sdk.UnstableCreateElicitationResponse{Accept: &sdk.UnstableCreateElicitationAccept{Action: "accept", Content: elicit.Content(form, res.Answers)}}, nil
@@ -44,7 +44,12 @@ func (c *client) UnstableCreateElicitation(ctx context.Context, p sdk.UnstableCr
44 form.Questions[i].Prompt = agent + " asks: " + form.Questions[i].Prompt44 form.Questions[i].Prompt = agent + " asks: " + form.Questions[i].Prompt
45 }45 }
46 ev := ui.ToolEvent{ID: c.shownID(childToolCall(p.Form.Meta)), Tool: agent + "/ask_user", Title: agent + " · ask_user: " + p.Form.Message, Kind: "other"}46 ev := ui.ToolEvent{ID: c.shownID(childToolCall(p.Form.Meta)), Tool: agent + "/ask_user", Title: agent + " · ask_user: " + p.Form.Message, Kind: "other"}
47- res := asker.Elicit(ctx, ev, form)47+ return elicitResponse(form, asker.Elicit(ctx, ev, form))
48+}
49+
50+// elicitResponse is the asker's result as the child's answer: accepted with
51+// the content, declined, or cancelled; nobody to ask otherwise.
52+func elicitResponse(form elicit.Form, res elicit.Result) (sdk.UnstableCreateElicitationResponse, error) {
48 switch res.Status {53 switch res.Status {
49 case elicit.Answered:54 case elicit.Answered:
50 return sdk.UnstableCreateElicitationResponse{Accept: &sdk.UnstableCreateElicitationAccept{Action: "accept", Content: elicit.Content(form, res.Answers)}}, nil55 return sdk.UnstableCreateElicitationResponse{Accept: &sdk.UnstableCreateElicitationAccept{Action: "accept", Content: elicit.Content(form, res.Answers)}}, nil
modified internal/detector/detector.go +57 -43
@@ -145,64 +145,78 @@ func (d *LoopDetector) record(action Action) (bool, *Loop, func(Loop)) {
145145 d.failures = 0
146146 }
147147
148- // Check if the current action (tool + input + output) has occurred consecutively.
149- count := 0
150- for i := len(d.history) - 1; i >= 0; i-- {
151- if d.history[i].ToolName == action.ToolName &&
152- d.history[i].Input == action.Input &&
153- d.history[i].Output == action.Output {
154- count++
155- if count >= d.threshold {
156- d.flagged++
157- loop := Loop{Tool: action.ToolName, Repeats: d.threshold + d.flagged, Stuck: d.stopAfter > 0 && d.flagged >= d.stopAfter}
158- return true, &loop, d.report
159- }
160- } else {
161- break
162- }
148+ // The current action (tool + input + output) repeated consecutively.
149+ if d.consecutive(action) >= d.threshold {
150+ d.flagged++
151+ return true, &Loop{Tool: action.ToolName, Repeats: d.threshold + d.flagged, Stuck: d.stuck()}, d.report
163152 }
164153 // The same FAILED call, coming back between other calls (ticket 240):
165154 // Mellum2 sent the same refused edit_file 26 times, a read_file between
166155 // each, and the consecutive count above never got past 2 — the turn ran
167156 // to maxTurns. A failure that already occurred threshold times in the
168- // history window is a loop too, whatever came in between. Successful
169- // calls keep the consecutive rule: a file read again is not a loop.
170- if action.Failed {
171- seen := 0
172- for _, h := range d.history {
173- if h == action {
174- seen++
175- }
157+ // history window is a loop too, whatever came in between (as many
158+ // earlier ones as the consecutive rule needs). Successful calls keep the
159+ // consecutive rule: a file read again is not a loop.
160+ if seen := d.seen(action); action.Failed && seen >= d.threshold {
161+ d.flagFailure(action)
162+ return true, &Loop{Tool: action.ToolName, Repeats: seen + 1, Stuck: d.stuck()}, d.report
163+ }
164+ d.flagged = 0
165+ d.remember(action)
166+
167+ if d.failAfter > 0 && d.failures >= d.failAfter {
168+ return false, &Loop{Tool: action.ToolName, Repeats: d.failures, Failing: true}, d.report
169+ }
170+ return false, nil, nil
171+}
172+
173+// consecutive is how many times action ends the history, in a row.
174+func (d *LoopDetector) consecutive(action Action) int {
175+ count := 0
176+ for i := len(d.history) - 1; i >= 0; i-- {
177+ h := d.history[i]
178+ if h.ToolName != action.ToolName || h.Input != action.Input || h.Output != action.Output {
179+ break
176180 }
177- if seen >= d.threshold { // as many earlier ones as the consecutive rule needs
178- if d.failedFlags == nil {
179- d.failedFlags = map[string]int{}
180- }
181- key := action.ToolName + "\x00" + action.Input + "\x00" + action.Output
182- d.failedFlags[key]++
183- d.flagged = d.failedFlags[key]
184- d.history = append(d.history, action)
185- if len(d.history) > d.maxHistory {
186- d.history = d.history[1:]
187- }
188- loop := Loop{Tool: action.ToolName, Repeats: seen + 1, Stuck: d.stopAfter > 0 && d.flagged >= d.stopAfter}
189- return true, &loop, d.report
181+ count++
182+ }
183+ return count
184+}
185+
186+// seen is how many times action is in the history window.
187+func (d *LoopDetector) seen(action Action) int {
188+ seen := 0
189+ for _, h := range d.history {
190+ if h == action {
191+ seen++
190192 }
191193 }
192- d.flagged = 0
194+ return seen
195+}
193196
194- // Append the new action.
197+// flagFailure counts one more flag for this failed call, and remembers it.
198+func (d *LoopDetector) flagFailure(action Action) {
199+ if d.failedFlags == nil {
200+ d.failedFlags = map[string]int{}
201+ }
202+ key := action.ToolName + "\x00" + action.Input + "\x00" + action.Output
203+ d.failedFlags[key]++
204+ d.flagged = d.failedFlags[key]
205+ d.remember(action)
206+}
207+
208+// remember appends action to the history window.
209+func (d *LoopDetector) remember(action Action) {
195210 d.history = append(d.history, action)
196211 if len(d.history) > d.maxHistory {
197212 d.history = d.history[1:]
198213 }
199-
200- if d.failAfter > 0 && d.failures >= d.failAfter {
201- return false, &Loop{Tool: action.ToolName, Repeats: d.failures, Failing: true}, d.report
202- }
203- return false, nil, nil
204214 }
205215
216+// stuck reports whether the flagged repeats have reached the stop count;
217+// called under the lock.
218+func (d *LoopDetector) stuck() bool { return d.stopAfter > 0 && d.flagged >= d.stopAfter }
219+
206220 // Stuck reports whether the flagged repeats have reached the stop count:
207221 // the last Record returned true, and so did the stopAfter-1 before it.
208222 func (d *LoopDetector) Stuck() bool {
@@ -145,64 +145,78 @@ func (d *LoopDetector) record(action Action) (bool, *Loop, func(Loop)) {
145 d.failures = 0145 d.failures = 0
146 }146 }
147 147
148- // Check if the current action (tool + input + output) has occurred consecutively.148+ // The current action (tool + input + output) repeated consecutively.
149- count := 0149+ if d.consecutive(action) >= d.threshold {
150- for i := len(d.history) - 1; i >= 0; i-- {150+ d.flagged++
151- if d.history[i].ToolName == action.ToolName &&151+ return true, &Loop{Tool: action.ToolName, Repeats: d.threshold + d.flagged, Stuck: d.stuck()}, d.report
152- d.history[i].Input == action.Input &&
153- d.history[i].Output == action.Output {
154- count++
155- if count >= d.threshold {
156- d.flagged++
157- loop := Loop{Tool: action.ToolName, Repeats: d.threshold + d.flagged, Stuck: d.stopAfter > 0 && d.flagged >= d.stopAfter}
158- return true, &loop, d.report
159- }
160- } else {
161- break
162- }
163 }152 }
164 // The same FAILED call, coming back between other calls (ticket 240):153 // The same FAILED call, coming back between other calls (ticket 240):
165 // Mellum2 sent the same refused edit_file 26 times, a read_file between154 // Mellum2 sent the same refused edit_file 26 times, a read_file between
166 // each, and the consecutive count above never got past 2 — the turn ran155 // each, and the consecutive count above never got past 2 — the turn ran
167 // to maxTurns. A failure that already occurred threshold times in the156 // to maxTurns. A failure that already occurred threshold times in the
168- // history window is a loop too, whatever came in between. Successful157+ // history window is a loop too, whatever came in between (as many
169- // calls keep the consecutive rule: a file read again is not a loop.158+ // earlier ones as the consecutive rule needs). Successful calls keep the
170- if action.Failed {159+ // consecutive rule: a file read again is not a loop.
171- seen := 0160+ if seen := d.seen(action); action.Failed && seen >= d.threshold {
172- for _, h := range d.history {161+ d.flagFailure(action)
173- if h == action {162+ return true, &Loop{Tool: action.ToolName, Repeats: seen + 1, Stuck: d.stuck()}, d.report
174- seen++163+ }
175- }164+ d.flagged = 0
165+ d.remember(action)
166+
167+ if d.failAfter > 0 && d.failures >= d.failAfter {
168+ return false, &Loop{Tool: action.ToolName, Repeats: d.failures, Failing: true}, d.report
169+ }
170+ return false, nil, nil
171+}
172+
173+// consecutive is how many times action ends the history, in a row.
174+func (d *LoopDetector) consecutive(action Action) int {
175+ count := 0
176+ for i := len(d.history) - 1; i >= 0; i-- {
177+ h := d.history[i]
178+ if h.ToolName != action.ToolName || h.Input != action.Input || h.Output != action.Output {
179+ break
176 }180 }
177- if seen >= d.threshold { // as many earlier ones as the consecutive rule needs181+ count++
178- if d.failedFlags == nil {182+ }
179- d.failedFlags = map[string]int{}183+ return count
180- }184+}
181- key := action.ToolName + "\x00" + action.Input + "\x00" + action.Output185+
182- d.failedFlags[key]++186+// seen is how many times action is in the history window.
183- d.flagged = d.failedFlags[key]187+func (d *LoopDetector) seen(action Action) int {
184- d.history = append(d.history, action)188+ seen := 0
185- if len(d.history) > d.maxHistory {189+ for _, h := range d.history {
186- d.history = d.history[1:]190+ if h == action {
187- }191+ seen++
188- loop := Loop{Tool: action.ToolName, Repeats: seen + 1, Stuck: d.stopAfter > 0 && d.flagged >= d.stopAfter}
189- return true, &loop, d.report
190 }192 }
191 }193 }
192- d.flagged = 0194+ return seen
195+}
193 196
194- // Append the new action.197+// flagFailure counts one more flag for this failed call, and remembers it.
198+func (d *LoopDetector) flagFailure(action Action) {
199+ if d.failedFlags == nil {
200+ d.failedFlags = map[string]int{}
201+ }
202+ key := action.ToolName + "\x00" + action.Input + "\x00" + action.Output
203+ d.failedFlags[key]++
204+ d.flagged = d.failedFlags[key]
205+ d.remember(action)
206+}
207+
208+// remember appends action to the history window.
209+func (d *LoopDetector) remember(action Action) {
195 d.history = append(d.history, action)210 d.history = append(d.history, action)
196 if len(d.history) > d.maxHistory {211 if len(d.history) > d.maxHistory {
197 d.history = d.history[1:]212 d.history = d.history[1:]
198 }213 }
199-
200- if d.failAfter > 0 && d.failures >= d.failAfter {
201- return false, &Loop{Tool: action.ToolName, Repeats: d.failures, Failing: true}, d.report
202- }
203- return false, nil, nil
204 }214 }
205 215
216+// stuck reports whether the flagged repeats have reached the stop count;
217+// called under the lock.
218+func (d *LoopDetector) stuck() bool { return d.stopAfter > 0 && d.flagged >= d.stopAfter }
219+
206 // Stuck reports whether the flagged repeats have reached the stop count:220 // Stuck reports whether the flagged repeats have reached the stop count:
207 // the last Record returned true, and so did the stopAfter-1 before it.221 // the last Record returned true, and so did the stopAfter-1 before it.
208 func (d *LoopDetector) Stuck() bool {222 func (d *LoopDetector) Stuck() bool {