mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-10 05:01:28 -10:00
* Improve harness context efficiency
* Address harness efficiency review findings
* Make recaps idle and cancellable
Delay recap generation until the session has been inactive, orient it around the active goal and next task, cancel stale work on activity, and render the result ephemerally above the composer.
Also strengthen the formatter safety regression to exercise formatter-modified edit output.
* Track every replace-all edit span
Translate each replacement into its post-edit line range, including cumulative shifts from earlier replacements, so follow-up edits remain authorized for every model-supplied change.
* Make review regressions cross-platform
Verify replace-all tracking through the canonical edit tool path instead of a platform-sensitive temp path, and cover cancellation when recaps are disabled during generation.
* fix(tools): derive SeenWhole from the recorded ranges
SeenWhole was a flag set only by a SINGLE read covering the file, while
SeenRange judged coverage from the accumulated ranges. The two then
disagreed about identical reads: after reading lines 1-50 and 51-100 of a
100-line file, SeenRange(1,100) was true and SeenWhole was false.
That matters because write_file gates overwrites on SeenWhole. A file past
the read byte budget cannot be read whole in one call — the read is
truncated and RecordSeenRange is correctly skipped, since the model did not
see those lines — so no sequence of reads could ever set the flag, and the
overwrite was refused permanently with:
the intended change depends on content that has not been read exactly
in this session. Read the affected lines with read_file, then retry.
Advice that could not change the answer. edit_file was unaffected because it
gates on SeenRange, so the escape was to edit rather than rewrite, but a
model following the error text would loop.
SeenWhole is now computed from the ranges against the file's recorded line
count, so scoped reads that together cover a file make it writable and the
error text becomes actionable. The observation carries that line count, and
a read reporting a different one clears the accumulated ranges: they
describe a shape the file no longer has.
Coverage is judged by merging sorted ranges rather than walking every line
against every range, which the previous SeenRange did — same answer, and it
no longer scans line-by-line over a large file.
Co-Authored-By: Claude Opus 5 <[email protected]>
* test(tools): key the large-file test on the resolved path
read_file resolves the workspace root through EvalSymlinks before recording
anything, so the tracker is keyed on the resolved path. The test asserted
against the raw t.TempDir() path, which is the same string on Windows and a
different one on macOS, where the temp dir sits under /var — a symlink to
/private/var. The SeenWhole assertions would have looked up a key that never
existed: passing here, failing on macOS CI.
Also switches the line generator to fmt.Fprintf (staticcheck QF1012).
Both from CodeRabbit on #842.
Co-Authored-By: Claude Opus 5 <[email protected]>
* test(tools): actually switch the line generator to fmt.Fprintf
The previous commit's message claimed this was done. It was not: the edit
silently did not apply and I reported it fixed without re-reading the file,
so builder.WriteString(fmt.Sprintf(...)) survived at the head — caught by
@anandh8x, not by me.
Verified this time by running the linter rather than by reading the diff:
golangci-lint --enable-only unused,ineffassign,staticcheck ./internal/tools/
reports 0 issues.
* fix: keep exact-read workflows recoverable
* test: cover permission normalization wiring
* fix: commit file observations after output budgeting
* fix: defer file observations across retries
* test: normalize patch line endings
---------
Co-authored-by: Vasanthdev2004 <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
262 lines
8.1 KiB
Go
262 lines
8.1 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/Gitlawb/zero/internal/localcontrol"
|
|
)
|
|
|
|
func NewLocalDesktopTools(options localcontrol.DesktopOptions) []Tool {
|
|
return []Tool{
|
|
newDesktopWindowsTool(options),
|
|
newDesktopSnapshotTool(options),
|
|
newDesktopActionTool(options),
|
|
}
|
|
}
|
|
|
|
type desktopWindowsTool struct {
|
|
baseTool
|
|
desktop localcontrol.Desktop
|
|
}
|
|
|
|
func newDesktopWindowsTool(options localcontrol.DesktopOptions) Tool {
|
|
return desktopWindowsTool{
|
|
baseTool: baseTool{
|
|
name: "desktop_windows",
|
|
deferred: true,
|
|
description: "List native desktop windows through the local desktop automation helper.",
|
|
parameters: Schema{
|
|
Type: "object",
|
|
Properties: map[string]PropertySchema{
|
|
"pid": {Type: "integer", Description: "Optional process id to list windows for.", Minimum: intPtr(1)},
|
|
},
|
|
AdditionalProperties: false,
|
|
},
|
|
safety: localControlSafety(options.Enabled, SideEffectLocalDesktop, "Reads native desktop window metadata."),
|
|
capabilities: ToolCapabilities{Effect: EffectInteractive, ThreadSafe: false},
|
|
},
|
|
desktop: localcontrol.NewDesktop(options),
|
|
}
|
|
}
|
|
|
|
func (tool desktopWindowsTool) RejectBeforePermission(args map[string]any) (Result, bool) {
|
|
if _, err := desktopWindowsInput(args); err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_windows: " + err.Error()), true
|
|
}
|
|
return Result{}, false
|
|
}
|
|
|
|
func (tool desktopWindowsTool) Run(ctx context.Context, args map[string]any) Result {
|
|
input, err := desktopWindowsInput(args)
|
|
if err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_windows: " + err.Error())
|
|
}
|
|
payload, err := json.Marshal(input)
|
|
if err != nil {
|
|
return errorResult("Error: Failed to encode desktop_windows input: " + err.Error())
|
|
}
|
|
return desktopCommandResult(ctx, tool.desktop, "list_windows", []string{"list_windows", string(payload)})
|
|
}
|
|
|
|
type desktopSnapshotTool struct {
|
|
baseTool
|
|
desktop localcontrol.Desktop
|
|
}
|
|
|
|
func newDesktopSnapshotTool(options localcontrol.DesktopOptions) Tool {
|
|
return desktopSnapshotTool{
|
|
baseTool: baseTool{
|
|
name: "desktop_snapshot",
|
|
deferred: true,
|
|
description: "Read a native desktop window accessibility snapshot through the local desktop automation helper.",
|
|
parameters: Schema{
|
|
Type: "object",
|
|
Properties: map[string]PropertySchema{
|
|
"pid": {Type: "integer", Description: "Target process id.", Minimum: intPtr(1)},
|
|
"window_id": {Type: "integer", Description: "Target window id.", Minimum: intPtr(1)},
|
|
"query": {Type: "string", Description: "Optional query/filter for the snapshot."},
|
|
"session": {Type: "string", Description: "Optional desktop automation session id."},
|
|
},
|
|
Required: []string{"pid", "window_id"},
|
|
AdditionalProperties: false,
|
|
},
|
|
safety: localControlSafety(options.Enabled, SideEffectLocalDesktop, "Reads a native desktop window state."),
|
|
capabilities: ToolCapabilities{Effect: EffectInteractive, ThreadSafe: false},
|
|
},
|
|
desktop: localcontrol.NewDesktop(options),
|
|
}
|
|
}
|
|
|
|
func (tool desktopSnapshotTool) RejectBeforePermission(args map[string]any) (Result, bool) {
|
|
if _, err := desktopSnapshotInput(args); err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_snapshot: " + err.Error()), true
|
|
}
|
|
return Result{}, false
|
|
}
|
|
|
|
func (tool desktopSnapshotTool) Run(ctx context.Context, args map[string]any) Result {
|
|
input, err := desktopSnapshotInput(args)
|
|
if err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_snapshot: " + err.Error())
|
|
}
|
|
payload, err := json.Marshal(input)
|
|
if err != nil {
|
|
return errorResult("Error: Failed to encode desktop_snapshot input: " + err.Error())
|
|
}
|
|
return desktopCommandResult(ctx, tool.desktop, "get_window_state", []string{"get_window_state", string(payload)})
|
|
}
|
|
|
|
type desktopActionTool struct {
|
|
baseTool
|
|
desktop localcontrol.Desktop
|
|
}
|
|
|
|
func newDesktopActionTool(options localcontrol.DesktopOptions) Tool {
|
|
return desktopActionTool{
|
|
baseTool: baseTool{
|
|
name: "desktop_action",
|
|
deferred: true,
|
|
description: "Run an allowed native desktop action through the local desktop automation helper.",
|
|
parameters: Schema{
|
|
Type: "object",
|
|
Properties: map[string]PropertySchema{
|
|
"command": {Type: "string", Description: "Allowed desktop command to run.", Enum: desktopActionCommandNames()},
|
|
"input": {Type: "object", Description: "JSON object passed to the desktop command."},
|
|
},
|
|
Required: []string{"command", "input"},
|
|
AdditionalProperties: false,
|
|
},
|
|
safety: localControlSafety(options.Enabled, SideEffectLocalDesktop, "Interacts with native desktop windows."),
|
|
capabilities: ToolCapabilities{Effect: EffectInteractive, ThreadSafe: false},
|
|
},
|
|
desktop: localcontrol.NewDesktop(options),
|
|
}
|
|
}
|
|
|
|
func (tool desktopActionTool) RejectBeforePermission(args map[string]any) (Result, bool) {
|
|
if _, _, err := desktopActionArgs(args); err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_action: " + err.Error()), true
|
|
}
|
|
return Result{}, false
|
|
}
|
|
|
|
func (tool desktopActionTool) Run(ctx context.Context, args map[string]any) Result {
|
|
command, payload, err := desktopActionArgs(args)
|
|
if err != nil {
|
|
return errorResult("Error: Invalid arguments for desktop_action: " + err.Error())
|
|
}
|
|
return desktopCommandResult(ctx, tool.desktop, command, []string{command, payload})
|
|
}
|
|
|
|
var desktopActionCommands = map[string]bool{
|
|
"start_session": true,
|
|
"end_session": true,
|
|
"launch_app": true,
|
|
"click": true,
|
|
"type_text": true,
|
|
"press_key": true,
|
|
"hotkey": true,
|
|
"scroll": true,
|
|
"drag": true,
|
|
}
|
|
|
|
func desktopActionCommandNames() []string {
|
|
names := make([]string, 0, len(desktopActionCommands))
|
|
for name := range desktopActionCommands {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
return names
|
|
}
|
|
|
|
func desktopActionArgs(args map[string]any) (string, string, error) {
|
|
command, err := stringArg(args, "command", "", true)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
command = strings.ToLower(strings.TrimSpace(command))
|
|
if !desktopActionCommands[command] {
|
|
return "", "", fmt.Errorf("command must be one of: %s", strings.Join(desktopActionCommandNames(), ", "))
|
|
}
|
|
input, err := objectArg(args, "input", true)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
payload, err := json.Marshal(input)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return command, string(payload), nil
|
|
}
|
|
|
|
func desktopWindowsInput(args map[string]any) (map[string]any, error) {
|
|
input := map[string]any{}
|
|
if _, ok := args["pid"]; ok {
|
|
pid, err := intArg(args, "pid", 0, 1, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
input["pid"] = pid
|
|
}
|
|
return input, nil
|
|
}
|
|
|
|
func desktopSnapshotInput(args map[string]any) (map[string]any, error) {
|
|
input, err := desktopWindowInput(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, key := range []string{"query", "session"} {
|
|
value, err := stringArgWithEmpty(args, key, "", false, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.TrimSpace(value) != "" {
|
|
input[key] = value
|
|
}
|
|
}
|
|
return input, nil
|
|
}
|
|
|
|
func desktopWindowInput(args map[string]any) (map[string]any, error) {
|
|
pid, err := requiredIntArg(args, "pid", 1, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
windowID, err := requiredIntArg(args, "window_id", 1, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"pid": pid, "window_id": windowID}, nil
|
|
}
|
|
|
|
func requiredIntArg(args map[string]any, key string, min int, max int) (int, error) {
|
|
if _, ok := args[key]; !ok {
|
|
return 0, fmt.Errorf("%s is required", key)
|
|
}
|
|
return intArg(args, key, 0, min, max)
|
|
}
|
|
|
|
func objectArg(args map[string]any, key string, required bool) (map[string]any, error) {
|
|
value, ok := args[key]
|
|
if !ok || value == nil {
|
|
if required {
|
|
return nil, fmt.Errorf("%s is required", key)
|
|
}
|
|
return map[string]any{}, nil
|
|
}
|
|
object, ok := value.(map[string]any)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s must be an object", key)
|
|
}
|
|
return object, nil
|
|
}
|
|
|
|
func desktopCommandResult(ctx context.Context, desktop localcontrol.Desktop, command string, args []string) Result {
|
|
return localControlCommandResult(ctx, desktop, "desktop", command, args)
|
|
}
|