43ea2e562d
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.
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package fs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"somegit.dev/Owlibou/gnoma/internal/tool"
|
|
)
|
|
|
|
const writeToolName = "fs.write"
|
|
|
|
var writeParams = json.RawMessage(`{
|
|
"type": "object",
|
|
"properties": {
|
|
"path": {
|
|
"type": "string",
|
|
"description": "Absolute path to the file to write"
|
|
},
|
|
"content": {
|
|
"type": "string",
|
|
"description": "Content to write to the file"
|
|
}
|
|
},
|
|
"required": ["path", "content"]
|
|
}`)
|
|
|
|
type WriteOption func(*WriteTool)
|
|
|
|
// WithMaxFileSize rejects writes where the content exceeds n bytes. 0 means no limit.
|
|
func WithMaxFileSize(n int64) WriteOption {
|
|
return func(t *WriteTool) { t.maxFileSize = n }
|
|
}
|
|
|
|
type WriteTool struct {
|
|
maxFileSize int64
|
|
guard *Guard
|
|
}
|
|
|
|
func (t *WriteTool) SetGuard(g *Guard) { t.guard = g }
|
|
|
|
func NewWriteTool(opts ...WriteOption) *WriteTool {
|
|
t := &WriteTool{}
|
|
for _, opt := range opts {
|
|
opt(t)
|
|
}
|
|
return t
|
|
}
|
|
|
|
func (t *WriteTool) Name() string { return writeToolName }
|
|
func (t *WriteTool) Description() string { return "Write content to a file, creating parent directories as needed" }
|
|
func (t *WriteTool) Parameters() json.RawMessage { return writeParams }
|
|
func (t *WriteTool) IsReadOnly() bool { return false }
|
|
func (t *WriteTool) IsDestructive() bool { return false }
|
|
func (t *WriteTool) Category() tool.Category { return tool.CategoryWrite }
|
|
|
|
func (t *WriteTool) ExtractPaths(args json.RawMessage) []string {
|
|
var a writeArgs
|
|
if err := json.Unmarshal(args, &a); err != nil {
|
|
return nil
|
|
}
|
|
return []string{a.Path}
|
|
}
|
|
|
|
type writeArgs struct {
|
|
Path string `json:"path"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
func (t *WriteTool) Execute(_ context.Context, args json.RawMessage) (tool.Result, error) {
|
|
var a writeArgs
|
|
if err := json.Unmarshal(args, &a); err != nil {
|
|
return tool.Result{}, fmt.Errorf("fs.write: invalid args: %w", err)
|
|
}
|
|
if a.Path == "" {
|
|
return tool.Result{}, fmt.Errorf("fs.write: path required")
|
|
}
|
|
|
|
if t.maxFileSize > 0 && int64(len(a.Content)) > t.maxFileSize {
|
|
return tool.Result{Output: fmt.Sprintf("Error: content too large (%d bytes, limit %d bytes)", len(a.Content), t.maxFileSize)}, nil
|
|
}
|
|
|
|
path := a.Path
|
|
if t.guard != nil {
|
|
resolved, err := t.guard.ResolveWrite(path)
|
|
if err != nil {
|
|
return tool.Result{Output: fmt.Sprintf("Error: %v", err)}, nil
|
|
}
|
|
path = resolved
|
|
}
|
|
|
|
// Create parent directories
|
|
dir := filepath.Dir(path)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return tool.Result{Output: fmt.Sprintf("Error creating directory: %v", err)}, nil
|
|
}
|
|
|
|
if err := os.WriteFile(path, []byte(a.Content), 0o644); err != nil {
|
|
return tool.Result{Output: fmt.Sprintf("Error writing file: %v", err)}, nil
|
|
}
|
|
|
|
return tool.Result{
|
|
Output: fmt.Sprintf("Wrote %d bytes to %s", len(a.Content), path),
|
|
Metadata: map[string]any{"bytes_written": len(a.Content), "path": path},
|
|
}, nil
|
|
}
|