| 1 | package evidence |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | |
| 6 | "reasonix/internal/shellsafe" |
| 7 | ) |
| 8 | |
| 9 | // ToolEffects projects shell effects onto policy and evidence boundaries. |
| 10 | type ToolEffects struct { |
| 11 | StateMutation, WorkspaceMutation, ContentMutation, RepositoryMutation bool |
| 12 | Known bool |
| 13 | Reason string |
| 14 | } |
| 15 | |
| 16 | // ClassifyToolCall returns durable effects for one concrete invocation. |
| 17 | func ClassifyToolCall(toolName string, args json.RawMessage, readOnly bool) ToolEffects { |
| 18 | return ClassifyEffect(EffectInput{ |
| 19 | ToolName: toolName, |
| 20 | Args: args, |
| 21 | StaticReadOnly: readOnly, |
| 22 | }).ToolEffects() |
| 23 | } |
| 24 | |
| 25 | // ClassifyBashToolCall parses once and returns effects plus permission trust. |
| 26 | func ClassifyBashToolCall(args json.RawMessage) (ToolEffects, bool) { |
| 27 | profile := ClassifyEffect(EffectInput{ToolName: "bash", Args: args}) |
| 28 | var fields map[string]json.RawMessage |
| 29 | if err := json.Unmarshal(args, &fields); err != nil { |
| 30 | return profile.ToolEffects(), false |
| 31 | } |
| 32 | effect := shellsafe.ClassifyBash(stringField(fields, "command")) |
| 33 | return profile.ToolEffects(), effect.IsPermissionReader() |
| 34 | } |
| 35 | |
| 36 | func commandEffectReason(effect shellsafe.CommandEffect) string { |
| 37 | domain := "" |
| 38 | switch { |
| 39 | case effect.Writes&shellsafe.WriteWorkspaceContent != 0: |
| 40 | domain = "workspace content write" |
| 41 | case effect.Writes&shellsafe.WriteRepositoryMetadata != 0: |
| 42 | domain = "repository metadata write" |
| 43 | case effect.Writes&shellsafe.WriteHostState != 0: |
| 44 | domain = "host state write" |
| 45 | case effect.Writes&shellsafe.WriteExternalState != 0: |
| 46 | domain = "external state write" |
| 47 | } |
| 48 | if domain == "" { |
| 49 | return effect.Reason |
| 50 | } |
| 51 | if effect.CommandFamily == "" { |
| 52 | return domain |
| 53 | } |
| 54 | return domain + " by " + effect.CommandFamily |
| 55 | } |
| 56 | |
| 57 | // ToolCallMutates is the compatibility projection for durable state changes. |
| 58 | func ToolCallMutates(toolName string, args json.RawMessage, readOnly bool) bool { |
| 59 | return ClassifyToolCall(toolName, args, readOnly).StateMutation |
| 60 | } |
| 61 |