返回 DeepSeek-Reasonix
tool_dispatch.go
根目录 / internal / agent / tool_dispatch.go
1 package agent
2
3 import (
4 "context"
5 "errors"
6 "os"
7 "strings"
8 "time"
9
10 "reasonix/internal/evidence"
11 "reasonix/internal/tool"
12 )
13
14 func (a *Agent) dispatchResolvedTool(ctx context.Context, plan *toolCallPlan) (result string, images []string, execution *tool.ShellExecution, err error) {
15 result, images, execution, err = a.invokeResolvedTool(ctx, plan)
16 if err == nil || ctx.Err() != nil || !plan.readOnly || !isTransientToolError(err) {
17 return result, images, execution, err
18 }
19 if plan.effects.StateMutation {
20 return result, images, execution, err
21 }
22 retryResult, retryImages, retryExec, retryErr := a.invokeResolvedTool(ctx, plan)
23 if retryExec != nil {
24 execution = retryExec
25 }
26 return retryResult, retryImages, execution, retryErr
27 }
28
29 func (a *Agent) invokeResolvedTool(ctx context.Context, plan *toolCallPlan) (result string, images []string, execution *tool.ShellExecution, err error) {
30 runTool, runArgs := plan.runTool, plan.runArgs
31 ctx = WithParentWriteClaimID(ctx, plan.parentWriteClaimID)
32 if reader, ok := runTool.(tool.ReadExecutor); ok {
33 start := time.Now()
34 var env tool.ReadResultEnvelope
35 result, env, err = reader.ExecuteRead(ctx, runArgs)
36 plan.readActiveMillis += max(1, time.Since(start).Milliseconds())
37 if err == nil {
38 plan.readEnvelope = &env
39 }
40 return result, nil, nil, err
41 }
42 if de, ok := runTool.(tool.DetailedExecutor); ok {
43 var detailed tool.DetailedResult
44 detailed, err = de.ExecuteDetailed(ctx, runArgs)
45 result, images, execution = detailed.Output, detailed.Images, detailed.Execution
46 if execution != nil && plan.verification {
47 switch {
48 case err != nil || (execution.ExitCode != nil && *execution.ExitCode != 0):
49 execution.Verification = tool.ShellVerificationFailed
50 default:
51 execution.Verification = tool.ShellVerificationPassed
52 }
53 } else if execution != nil && execution.Verification == "" {
54 execution.Verification = tool.ShellVerificationNotVerification
55 }
56 if execution != nil && evidence.BashCommandMayBeOpaqueMutation(runArgs) &&
57 execution.MutationRisk == tool.ShellMutationMayHaveCompleted {
58 execution.MutationRisk = tool.ShellMutationUnknown
59 }
60 return result, images, execution, err
61 }
62 if it, ok := runTool.(tool.ImageTool); ok {
63 result, images, err = it.ExecuteWithImages(ctx, runArgs)
64 return result, images, execution, err
65 }
66 result, err = runTool.Execute(ctx, runArgs)
67 var missing *os.PathError
68 if errors.Is(err, os.ErrNotExist) && errors.As(err, &missing) {
69 err = &tool.OperationError{Diagnostic: tool.OperationDiagnostic{Code: tool.FSNotFound, Path: missing.Path, Recovery: "read the target at its current path, or create a new file when required"}, Cause: err}
70 }
71 return result, images, execution, err
72 }
73
74 func isTransientToolError(err error) bool {
75 if err == nil {
76 return false
77 }
78 var classified interface{ RetryableToolError() bool }
79 if errors.As(err, &classified) {
80 return classified.RetryableToolError()
81 }
82 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
83 return false
84 }
85 msg := strings.ToLower(err.Error())
86 for _, token := range []string{
87 "execution may have completed", "execution result is unknown",
88 "after dispatch", "was not retried",
89 } {
90 if strings.Contains(msg, token) {
91 return false
92 }
93 }
94 for _, token := range []string{
95 "timeout", "temporar", "connection reset", "connection refused",
96 "broken pipe", "eof", "i/o timeout", "tls handshake", "unavailable",
97 } {
98 if strings.Contains(msg, token) {
99 return true
100 }
101 }
102 return false
103 }
104
104 lines GO