mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-02 04:33:56 +08:00
Extracted cleanly onto becc47a without inherited #655/#662/#663 commits.
Adds an early return when !result.IsError in diagnosedShellTool.Call so a
successful command whose stdout/target data contains 'command not found' is
never misdiagnosed as a failed invocation, with regression test coverage.
Co-authored-by: AseemPrasad <aseemprasad0520@gmail.com>
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
var missingExecutableMessages = []*regexp.Regexp{
|
|
regexp.MustCompile(`(?m)^(?:[^\r\n:]+/)?bash:\s+(?:line\s+\d+:\s+)?([^\s:]+):\s+command not found\s*$`),
|
|
regexp.MustCompile(`(?m)^(?:[^\r\n:]+/)?(?:sh|dash|ash):\s+\d+:\s+([^\s:]+):\s+not found\s*$`),
|
|
regexp.MustCompile(`(?m)^(?:[^\r\n:]+/)?zsh:\s+command not found:\s+([^\s:]+)\s*$`),
|
|
}
|
|
|
|
func diagnoseBashTool(t actool.CoreTool) actool.CoreTool {
|
|
if t.Name() != "Bash" {
|
|
return t
|
|
}
|
|
return &diagnosedShellTool{CoreTool: t}
|
|
}
|
|
|
|
type diagnosedShellTool struct{ actool.CoreTool }
|
|
|
|
func (t *diagnosedShellTool) AcceptsRawInput() bool {
|
|
r, ok := t.CoreTool.(interface{ AcceptsRawInput() bool })
|
|
return ok && r.AcceptsRawInput()
|
|
}
|
|
|
|
func (t *diagnosedShellTool) Call(ctx context.Context, input json.RawMessage, tc *actool.ToolContext) (actool.Result, error) {
|
|
result, err := t.CoreTool.Call(ctx, input, tc)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if !result.IsError {
|
|
return result, nil
|
|
}
|
|
output := result.Flatten()
|
|
for _, pattern := range missingExecutableMessages {
|
|
match := pattern.FindStringSubmatch(output)
|
|
if len(match) != 2 {
|
|
continue
|
|
}
|
|
diagnosed := actool.Errorf(fmt.Sprintf(
|
|
"%s\n\n[ARTEX diagnostic: missing_executable]\nThe shell could not find executable %q in this runtime's PATH. This invocation failed and is not assessment evidence. Do not retry this executable; report the missing dependency instead.",
|
|
output, match[1],
|
|
))
|
|
diagnosed.Extra = result.Extra
|
|
return diagnosed, nil
|
|
}
|
|
return result, nil
|
|
}
|