Files
ARTEX/server/tool_usage.go
T
BaaRai c5cb1afad7 fix(agent): keep the record_fact harness opt-out through tool wrappers
record_fact opts out of the harness schema check (norma Spec.RawInput) and
validates its own input. The harness finds the opt-out with a type
assertion on the tool it is handed, and that tool is a wrapper: catalog
tools are metered (meteredTool), and guardPanic wraps every tool last
(guardedTool). Both embed CoreTool, which hides AcceptsRawInput, so the
opt-out was lost and a malformed call was answered by the SDK ("invalid
tool input: facts: expected array") instead of by the handler.

guardedTool and meteredTool now forward AcceptsRawInput, as overriddenTool
already does.

Test: TestFix410AcceptsRawInputSurvivesWrappers covers each wrapper and
record_fact through wireTools, AugmentTools and harness.Query.
2026-11-20 18:11:16 +01:00

57 lines
1.6 KiB
Go

package server
import (
"context"
"encoding/json"
"log"
actool "github.com/Autumn-27/norma/tool"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
)
type toolUsageRecorder interface {
InsertToolUsage(*db.ToolUsage) error
}
// meteredTool records one row immediately before a catalog tool is invoked. It
// embeds the resolved tool so schema overrides, defaults and permission behavior
// remain unchanged.
type meteredTool struct {
actool.CoreTool
recorder toolUsageRecorder
toolKey string
agentKey string
ri agent.RunInfo
}
func meterTool(t actool.CoreTool, recorder toolUsageRecorder, toolKey, agentKey string, ri agent.RunInfo) actool.CoreTool {
if recorder == nil {
return t
}
return &meteredTool{CoreTool: t, recorder: recorder, toolKey: toolKey, agentKey: agentKey, ri: ri}
}
// AcceptsRawInput forwards the resolved tool's opt-out of the harness schema
// check; the embedded interface alone would hide it from the harness's type
// assertion.
func (m *meteredTool) AcceptsRawInput() bool {
r, ok := m.CoreTool.(interface{ AcceptsRawInput() bool })
return ok && r.AcceptsRawInput()
}
func (m *meteredTool) Call(ctx context.Context, input json.RawMessage, tc *actool.ToolContext) (actool.Result, error) {
if err := m.recorder.InsertToolUsage(&db.ToolUsage{
ToolKey: m.toolKey,
AgentKey: m.agentKey,
TaskID: m.ri.TaskID,
ExplorationID: m.ri.ExplorationID,
IntentID: m.ri.IntentID,
SessionID: m.ri.SessionID,
}); err != nil {
log.Printf("[toolusage] insert %s: %v", m.toolKey, err)
}
return m.CoreTool.Call(ctx, input, tc)
}