package harness import ( "encoding/json" "fmt" "github.com/Autumn-27/norma/llm" "github.com/Autumn-27/norma/permission" "github.com/Autumn-27/norma/tool" ) // rawInputTool is the optional opt-out from schema validation. Discovered by // assertion rather than declared on tool.CoreTool, so hosts implementing that // interface are unaffected. type rawInputTool interface{ AcceptsRawInput() bool } func acceptsRawInput(t tool.CoreTool) bool { r, ok := t.(rawInputTool) return ok && r.AcceptsRawInput() } // execOne resolves, permission-checks, and runs a single tool call, returning a // tool_result block and any extra messages the tool injects after its result // (Result.Extra — e.g. the Skill tool's instructions message). It never panics; // failures become error results. It is safe to call from the streaming // executor's goroutines (read-only access to loop config; permission/hook // callbacks are the host's responsibility to make safe). func (l *loop) execOne(use llm.ContentBlock, emitProgress func(tool.ProgressInfo)) (llm.ContentBlock, []llm.Message) { t, ok := l.in.Tools.Get(use.Name) if !ok { return llm.ToolResultText(use.ID, fmt.Sprintf("Error: unknown tool %q", use.Name), true), nil } input := use.Input // Schema validation (FR-04.5). // // A tool may opt out by reporting AcceptsRawInput: it parses its own // arguments and would rather repair a malformed call than have it rejected // here, where the only possible answer is a generic error. The schema is // still advertised to the model either way — see tool.Spec.RawInput. if !acceptsRawInput(t) { if err := tool.ValidateInput(t.InputSchema(), input); err != nil { return llm.ToolResultText(use.ID, "Error: invalid tool input: "+err.Error(), true), nil } } mode := l.in.PermissionMode if l.in.PermissionModeFunc != nil { mode = l.in.PermissionModeFunc() } pc := permission.Context{ Mode: mode, WorkingDir: l.in.WorkingDir, Allowed: l.in.Allowed, Disallowed: l.in.Disallowed, } dec := permission.Evaluate(l.ctx, permission.Request{ ToolName: use.Name, Input: input, IsReadOnly: t.IsReadOnly(input), ToolDecision: t.CheckPermissions(l.ctx, input, pc), Ctx: pc, Ask: l.in.CanUseTool, }) if dec.Behavior != permission.Allow { msg := dec.Message if msg == "" { msg = "denied" } return llm.ToolResultText(use.ID, "Tool call was not permitted: "+msg, true), nil } if len(dec.UpdatedInput) > 0 { input = dec.UpdatedInput } // Pre-tool hook. if l.in.Hooks != nil { if block, msg, updated := l.in.Hooks.PreToolUse(l.ctx, use.Name, input); block { return llm.ToolResultText(use.ID, "Blocked by hook: "+msg, true), nil } else if len(updated) > 0 { input = updated } } tc := &tool.ToolContext{ WorkingDir: l.in.WorkingDir, AgentID: l.in.AgentID, ToolUseID: use.ID, Emit: emitProgress, OutputDir: l.in.ToolOutputDir, MaxOutputChars: l.in.MaxToolOutputChars, Tasks: l.in.Tasks, Env: l.in.BashEnv, } // toolCtx carries the run's MaxDuration deadline, so a tool that overruns the // wall-clock budget is interrupted here (the turn loop then enters wrap-up). During // the wrap-up phase toolCtx is the live parent ctx, so settlement tools run freely. res, err := t.Call(l.toolCtx, input, tc) if err != nil { res = tool.Errorf("Error: " + err.Error()) } // Global output cap at the single post-tool choke point: EVERY tool — // built-in, MCP, custom, host — is bounded here regardless of whether it // remembered to Capture itself, so no tool can overflow the context window // (the failure mode that motivated this net). CaptureOnce is idempotent: // output a tool already spilled/truncated carries a marker and is left // untouched, so this never double-truncates or double-spills. res = capOutput(tc, res) if l.in.Hooks != nil { raw, _ := json.Marshal(res.Flatten()) l.in.Hooks.PostToolUse(l.ctx, use.Name, input, raw, res.IsError) } content := res.Content if len(content) == 0 { content = []llm.ContentBlock{llm.TextBlock("(no output)")} } return llm.ContentBlock{Type: llm.BlockToolResult, ToolUseID: use.ID, Content: content, IsError: res.IsError}, res.Extra } // capOutput runs every text block of a tool result through the idempotent // output cap (tool.CaptureOnce). Non-text blocks pass through untouched. It is // the one place that guarantees no tool's output — however constructed — can // blow past the context budget. func capOutput(tc *tool.ToolContext, res tool.Result) tool.Result { for i, b := range res.Content { if b.Type == llm.BlockText { res.Content[i].Text = tool.CaptureOnce(tc, b.Text) } } return res }