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]>
196 lines
7.2 KiB
Go
196 lines
7.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Gitlawb/zero/internal/zeroruntime"
|
|
)
|
|
|
|
// bigToolResult builds a tool-result message with approximately tokens worth of
|
|
// body, paired to a tool call id. ApproxTextTokens counts non-space chars / 4,
|
|
// so 4*tokens non-space chars yields ~tokens.
|
|
func bigToolResult(id string, tokens int) zeroruntime.Message {
|
|
return zeroruntime.Message{
|
|
Role: zeroruntime.MessageRoleTool,
|
|
ToolCallID: id,
|
|
Content: strings.Repeat("x", tokens*4),
|
|
}
|
|
}
|
|
|
|
func toolCallMsg(id, name string) zeroruntime.Message {
|
|
return zeroruntime.Message{
|
|
Role: zeroruntime.MessageRoleAssistant,
|
|
ToolCalls: []zeroruntime.ToolCall{{ID: id, Name: name}},
|
|
}
|
|
}
|
|
|
|
func toolCallWithArgs(id, name, args string) zeroruntime.Message {
|
|
return zeroruntime.Message{
|
|
Role: zeroruntime.MessageRoleAssistant,
|
|
ToolCalls: []zeroruntime.ToolCall{{ID: id, Name: name, Arguments: args}},
|
|
}
|
|
}
|
|
|
|
func TestPruneSkipsSmallSessions(t *testing.T) {
|
|
// Total reclaimable is under the gate → no pruning.
|
|
msgs := []zeroruntime.Message{
|
|
toolCallMsg("c1", "read_file"),
|
|
bigToolResult("c1", 5000),
|
|
}
|
|
out, reclaimed := pruneStaleToolOutput(msgs, 4)
|
|
if reclaimed != 0 {
|
|
t.Fatalf("small session should reclaim nothing, got %d", reclaimed)
|
|
}
|
|
if out[1].Content != msgs[1].Content {
|
|
t.Fatal("content must be untouched when under the gate")
|
|
}
|
|
}
|
|
|
|
func TestPrunePreservesRecentAndPrunesOld(t *testing.T) {
|
|
// One huge OLD result (beyond the protect window) + recent small ones.
|
|
msgs := []zeroruntime.Message{
|
|
toolCallMsg("old", "grep"),
|
|
bigToolResult("old", 60000), // old, big → prunable
|
|
// Filler to push "old" past the recent-protection window:
|
|
toolCallMsg("mid", "read_file"),
|
|
bigToolResult("mid", 45000), // protected by the 40k recent window
|
|
zeroruntime.Message{Role: zeroruntime.MessageRoleAssistant, Content: "thinking"},
|
|
zeroruntime.Message{Role: zeroruntime.MessageRoleUser, Content: "next"},
|
|
}
|
|
out, reclaimed := pruneStaleToolOutput(msgs, 2)
|
|
if reclaimed <= 0 {
|
|
t.Fatalf("expected the old 60k tool result to be pruned, reclaimed=%d", reclaimed)
|
|
}
|
|
if !isPrunedPlaceholder(out[1].Content) {
|
|
t.Fatalf("old result should be replaced with a placeholder, got %q", out[1].Content[:40])
|
|
}
|
|
// The placeholder names the tool, and the message + id survive (provider replay).
|
|
if !strings.Contains(out[1].Content, "grep") {
|
|
t.Fatalf("placeholder should name the tool, got %q", out[1].Content)
|
|
}
|
|
if out[1].ToolCallID != "old" || out[1].Role != zeroruntime.MessageRoleTool {
|
|
t.Fatal("pruned message must keep its role and ToolCallID for replay")
|
|
}
|
|
// The recent big result stays verbatim.
|
|
if isPrunedPlaceholder(out[3].Content) {
|
|
t.Fatal("a result inside the recent-protection window must not be pruned")
|
|
}
|
|
}
|
|
|
|
func TestPruneIsIdempotent(t *testing.T) {
|
|
msgs := []zeroruntime.Message{
|
|
toolCallMsg("old", "bash"),
|
|
bigToolResult("old", 60000),
|
|
toolCallMsg("mid", "read_file"),
|
|
bigToolResult("mid", 45000),
|
|
zeroruntime.Message{Role: zeroruntime.MessageRoleUser, Content: "go"},
|
|
}
|
|
once, r1 := pruneStaleToolOutput(msgs, 1)
|
|
if r1 == 0 {
|
|
t.Fatal("first pass should prune")
|
|
}
|
|
_, r2 := pruneStaleToolOutput(once, 1)
|
|
if r2 != 0 {
|
|
t.Fatalf("second pass should be a no-op (already pruned), reclaimed=%d", r2)
|
|
}
|
|
}
|
|
|
|
func TestPruneNeverTouchesNonToolMessages(t *testing.T) {
|
|
msgs := []zeroruntime.Message{
|
|
{Role: zeroruntime.MessageRoleUser, Content: strings.Repeat("u ", 60000)},
|
|
{Role: zeroruntime.MessageRoleAssistant, Content: strings.Repeat("a ", 60000)},
|
|
}
|
|
out, reclaimed := pruneStaleToolOutput(msgs, 0)
|
|
if reclaimed != 0 {
|
|
t.Fatalf("non-tool messages must never be pruned, reclaimed=%d", reclaimed)
|
|
}
|
|
if out[0].Content != msgs[0].Content || out[1].Content != msgs[1].Content {
|
|
t.Fatal("non-tool content changed")
|
|
}
|
|
}
|
|
|
|
func TestPruneDoesNotMutateInput(t *testing.T) {
|
|
msgs := []zeroruntime.Message{
|
|
toolCallMsg("old", "grep"),
|
|
bigToolResult("old", 60000),
|
|
toolCallMsg("mid", "read_file"),
|
|
bigToolResult("mid", 45000),
|
|
zeroruntime.Message{Role: zeroruntime.MessageRoleUser, Content: "go"},
|
|
}
|
|
original := msgs[1].Content
|
|
_, reclaimed := pruneStaleToolOutput(msgs, 1)
|
|
if reclaimed == 0 {
|
|
t.Fatal("expected a prune")
|
|
}
|
|
if msgs[1].Content != original {
|
|
t.Fatal("pruneStaleToolOutput must not mutate the caller's slice")
|
|
}
|
|
}
|
|
|
|
func TestPruneSupersededReadResultsOnlyDropsIdenticalOlderReads(t *testing.T) {
|
|
body := strings.Repeat("same source line\n", 1000)
|
|
messages := []zeroruntime.Message{
|
|
toolCallWithArgs("old", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "old", Content: body},
|
|
toolCallWithArgs("different", "read_file", `{"path":"b.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "different", Content: body},
|
|
toolCallWithArgs("new", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "new", Content: body},
|
|
}
|
|
out, reclaimed := pruneSupersededReadResults(messages)
|
|
if reclaimed <= 0 || !strings.Contains(out[1].Content, "superseded identical") {
|
|
t.Fatalf("older duplicate was not pruned: reclaimed=%d output=%q", reclaimed, out[1].Content)
|
|
}
|
|
if out[3].Content != body || out[5].Content != body {
|
|
t.Fatal("different call and newest result must remain exact")
|
|
}
|
|
if messages[1].Content != body {
|
|
t.Fatal("input slice was mutated")
|
|
}
|
|
}
|
|
|
|
func TestPruneSupersededReadResultsKeepsChangedAndMutatingResults(t *testing.T) {
|
|
messages := []zeroruntime.Message{
|
|
toolCallWithArgs("r1", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "r1", Content: "version one"},
|
|
toolCallWithArgs("r2", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "r2", Content: "version two"},
|
|
toolCallWithArgs("e1", "edit_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "e1", Content: "edited"},
|
|
toolCallWithArgs("e2", "edit_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "e2", Content: "edited"},
|
|
}
|
|
out, reclaimed := pruneSupersededReadResults(messages)
|
|
if reclaimed != 0 {
|
|
t.Fatalf("unsafe results were pruned: %d", reclaimed)
|
|
}
|
|
for index := range messages {
|
|
if out[index].Content != messages[index].Content {
|
|
t.Fatalf("message %d changed unexpectedly", index)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPruneSupersededReadResultsFindsMatchingBodyAcrossChangedRead(t *testing.T) {
|
|
bodyA := strings.Repeat("version a\n", 100)
|
|
bodyB := strings.Repeat("version b\n", 100)
|
|
messages := []zeroruntime.Message{
|
|
toolCallWithArgs("old-a", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "old-a", Content: bodyA},
|
|
toolCallWithArgs("middle-b", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "middle-b", Content: bodyB},
|
|
toolCallWithArgs("new-a", "read_file", `{"path":"a.go"}`),
|
|
{Role: zeroruntime.MessageRoleTool, ToolCallID: "new-a", Content: bodyA},
|
|
}
|
|
|
|
out, reclaimed := pruneSupersededReadResults(messages)
|
|
|
|
if reclaimed <= 0 || !strings.Contains(out[1].Content, "superseded identical") {
|
|
t.Fatalf("older matching body was not pruned across a changed result: reclaimed=%d output=%q", reclaimed, out[1].Content)
|
|
}
|
|
if out[3].Content != bodyB || out[5].Content != bodyA {
|
|
t.Fatal("changed middle result and newest matching result must remain exact")
|
|
}
|
|
}
|