Files
gnoma/internal/tool/fs/read.go
T
vikingowl 43ea2e562d feat(engine): two-stage tool routing for small local arms
Plan A from docs/superpowers/plans/2026-05-19-post-slm-unlock.md.

Small local SLMs (<=16k context) waste ~1500 tokens per turn on the
full tool catalogue. Two-stage routing replaces round-1 tools with a
single synthetic select_category schema; round-2+ sends only the
selected category's real tool schemas plus select_category for
re-selection.

- internal/tool/category.go: Category type, optional Categorized
  interface, CategoryOf() with meta fallback. fs.read/fs.ls -> read,
  fs.write/fs.edit -> write, fs.glob/fs.grep -> search, bash -> exec.
- internal/engine/twostage.go: synthetic select_category tool,
  intercept helper, per-turn selectedCategory state under e.mu.
- Engine round 1 forces ToolChoiceRequired so SLMs don't fall back to
  prose. State resets at the top and end of every runLoop.
- Activates automatically on a forced local arm with ContextWindow
  <=16384, or via [router].force_two_stage TOML key.
- Integration test drives a 3-round trip and asserts: round 1 emits
  exactly one schema (synthetic) with ToolChoiceRequired, round 2
  contains only write-category schemas + select_category, real
  fs.write executes. Invalid-category fallback round-trips back to
  round-1 mode.
2026-05-19 20:53:21 +02:00

145 lines
3.3 KiB
Go

package fs
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"somegit.dev/Owlibou/gnoma/internal/tool"
)
const (
readToolName = "fs.read"
defaultMaxLines = 2000
)
var readParams = json.RawMessage(`{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Absolute path to the file to read"
},
"offset": {
"type": "integer",
"description": "Line number to start reading from (0-based)"
},
"limit": {
"type": "integer",
"description": "Maximum number of lines to read"
}
},
"required": ["path"]
}`)
type ReadTool struct {
maxLines int
guard *Guard
}
func (t *ReadTool) SetGuard(g *Guard) { t.guard = g }
type ReadOption func(*ReadTool)
func WithMaxLines(n int) ReadOption {
return func(t *ReadTool) { t.maxLines = n }
}
func NewReadTool(opts ...ReadOption) *ReadTool {
t := &ReadTool{maxLines: defaultMaxLines}
for _, opt := range opts {
opt(t)
}
return t
}
func (t *ReadTool) Name() string { return readToolName }
func (t *ReadTool) Description() string { return "Read a file from the filesystem with optional offset and line limit" }
func (t *ReadTool) Parameters() json.RawMessage { return readParams }
func (t *ReadTool) IsReadOnly() bool { return true }
func (t *ReadTool) IsDestructive() bool { return false }
func (t *ReadTool) Category() tool.Category { return tool.CategoryRead }
func (t *ReadTool) ExtractPaths(args json.RawMessage) []string {
var a readArgs
if err := json.Unmarshal(args, &a); err != nil {
return nil
}
return []string{a.Path}
}
type readArgs struct {
Path string `json:"path"`
Offset int `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
}
func (t *ReadTool) Execute(_ context.Context, args json.RawMessage) (tool.Result, error) {
var a readArgs
if err := json.Unmarshal(args, &a); err != nil {
return tool.Result{}, fmt.Errorf("fs.read: invalid args: %w", err)
}
if a.Path == "" {
return tool.Result{}, fmt.Errorf("fs.read: path required")
}
path := a.Path
if t.guard != nil {
resolved, err := t.guard.ResolveRead(path)
if err != nil {
return tool.Result{Output: fmt.Sprintf("Error: %v", err)}, nil
}
path = resolved
}
data, err := os.ReadFile(path)
if err != nil {
return tool.Result{Output: fmt.Sprintf("Error: %v", err)}, nil
}
lines := strings.Split(string(data), "\n")
totalLines := len(lines)
// Apply offset
offset := a.Offset
if offset < 0 {
offset = 0
}
if offset >= totalLines {
return tool.Result{
Output: fmt.Sprintf("(file has %d lines, offset %d is past end)", totalLines, offset),
Metadata: map[string]any{"total_lines": totalLines},
}, nil
}
lines = lines[offset:]
// Apply limit
limit := a.Limit
if limit <= 0 {
limit = t.maxLines
}
truncated := false
if len(lines) > limit {
lines = lines[:limit]
truncated = true
}
// Format with line numbers (1-based, matching cat -n)
var b strings.Builder
for i, line := range lines {
fmt.Fprintf(&b, "%d\t%s\n", offset+i+1, line)
}
output := strings.TrimRight(b.String(), "\n")
meta := map[string]any{"total_lines": totalLines}
if truncated {
meta["truncated"] = true
meta["showing"] = fmt.Sprintf("lines %d-%d of %d", offset+1, offset+len(lines), totalLines)
}
return tool.Result{Output: output, Metadata: meta}, nil
}