Files
Pulse/internal/agentcapabilities/tool_response.go
rcourtman 4a090045a8 Tell Patrol when an agent has commands off, not disconnected
Agents install with command execution off. On such a host every container
action and every log or command read is refused, and Pulse reported all of
it as "command agent is not connected". Nothing is disconnected: commands
were never enabled. Patrol's prompt still listed restart, stop and update,
the model proposed one, was refused, and told the user to restore a
connection that is not broken. On the six-fault set a default agent
resolved 1 of 6 against 5 of 6 with commands on, and no Ask first install
produced a plan in 30 days.

The two states now have two codes everywhere a refusal surfaces. The Docker
executor reports host_commands_disabled when the container's host carries a
reporting Pulse agent whose own report says commands are off, with the one
step that fixes it: the install command with command access ticked. A
monitoring-only agent refuses a server request to enable commands, so there
is no server-side switch to offer. A stopped agent, an integration-only
host and an unknown host stay command_agent_disconnected. Read tools return
COMMANDS_NOT_ENABLED in place of NO_AGENT for the same case, found by asking
routing where the target would go if every reporting agent held a session.

Patrol's catalog and patrol_action_capabilities list only what planning
would accept now. Refused capabilities arrive separately with the
executor's own reason, and the catalog names a commands-off host, so the
prompt can say so before the model spends a call. Service.Capabilities
returns the runnable set, DeclaredCapabilities keeps the declarations for
policy evaluation.

No default changes, no agent is enabled, and no read or command policy is
loosened: nothing that was refused is now allowed.
2026-11-10 09:04:23 +00:00

136 lines
4.2 KiB
Go

package agentcapabilities
import (
"encoding/json"
"strings"
)
const (
ErrCodeFSMBlocked = "FSM_BLOCKED"
ErrCodeStrictResolution = "STRICT_RESOLUTION"
ErrCodeRoutingMismatch = "ROUTING_MISMATCH"
ErrCodeExecutionContextUnavailable = "EXECUTION_CONTEXT_UNAVAILABLE"
ErrCodeNotFound = "NOT_FOUND"
ErrCodeActionNotAllowed = "ACTION_NOT_ALLOWED"
ErrCodePolicyBlocked = "POLICY_BLOCKED"
ErrCodeApprovalRequired = "APPROVAL_REQUIRED"
ErrCodeInvalidInput = "INVALID_INPUT"
ErrCodeExecutionFailed = "EXECUTION_FAILED"
ErrCodeNoAgent = "NO_AGENT"
// ErrCodeCommandsNotEnabled is not NO_AGENT: the agent is reporting and
// commands were never enabled on it. Nothing is disconnected, a retry
// cannot succeed, and the fix is the install command, not a repair.
ErrCodeCommandsNotEnabled = "COMMANDS_NOT_ENABLED"
)
// ToolResponse is the structured tool-result envelope used when a tool needs
// machine-readable success/failure fields inside the shared text result
// wrapper. It is shared so Assistant tools and agent-facing adapters branch on
// the same blocked/failed/retryable vocabulary.
type ToolResponse struct {
OK bool `json:"ok"`
Data any `json:"data,omitempty"`
Error *ToolError `json:"error,omitempty"`
Meta map[string]any `json:"meta,omitempty"`
}
// ToolError provides the structured failure shape for ToolResponse.
type ToolError struct {
Code string `json:"code"`
Message string `json:"message"`
Blocked bool `json:"blocked,omitempty"`
Failed bool `json:"failed,omitempty"`
Retryable bool `json:"retryable,omitempty"`
Details map[string]any `json:"details,omitempty"`
}
// NewToolBlockedError creates a policy or validation blocked tool response.
func NewToolBlockedError(code, message string, details map[string]any) ToolResponse {
return ToolResponse{
OK: false,
Error: &ToolError{
Code: code,
Message: message,
Blocked: true,
Details: details,
},
}
}
// NewToolResponseResult wraps a structured ToolResponse in the shared tool
// result envelope while preserving failure state through isError.
func NewToolResponseResult(resp ToolResponse) ToolResult {
return NewToolJSONResultWithIsError(resp, !resp.OK)
}
func toolResultJSONObject(resultText string) (map[string]any, bool) {
if resultText == "" {
return nil, false
}
jsonStart := strings.Index(resultText, "{")
if jsonStart == -1 {
return nil, false
}
var obj map[string]any
if err := json.Unmarshal([]byte(resultText[jsonStart:]), &obj); err != nil {
return nil, false
}
return obj, true
}
// ToolResultErrorCode extracts the shared ToolResponse error code from a tool
// result text. It also accepts the legacy top-level error_code shape so old
// persisted transcripts and compatibility paths branch through the same helper.
func ToolResultErrorCode(resultText string) (string, bool) {
obj, ok := toolResultJSONObject(resultText)
if !ok {
return "", false
}
if errorAny, ok := obj["error"]; ok {
if errorMap, ok := errorAny.(map[string]any); ok {
if code, ok := errorMap["code"].(string); ok && strings.TrimSpace(code) != "" {
return code, true
}
}
}
if code, ok := obj["error_code"].(string); ok && strings.TrimSpace(code) != "" {
return code, true
}
return "", false
}
// ToolResultHasErrorCode reports whether a tool result text carries the given
// shared ToolResponse error code.
func ToolResultHasErrorCode(resultText, code string) bool {
got, ok := ToolResultErrorCode(resultText)
return ok && got == code
}
// ToolResultHasVerificationOK checks whether a tool result text includes
// structured verification evidence indicating a mutation was confirmed.
//
// Expected shape, possibly after leading human-readable text:
//
// { "verification": { "ok": true, ... } }
func ToolResultHasVerificationOK(resultText string) bool {
obj, ok := toolResultJSONObject(resultText)
if !ok {
return false
}
vAny, ok := obj["verification"]
if !ok {
return false
}
vMap, ok := vAny.(map[string]any)
if !ok {
return false
}
okAny, ok := vMap["ok"]
if !ok {
return false
}
okBool, ok := okAny.(bool)
return ok && okBool
}