Files
Agent-Native/runtime/tools/checkpoint_hook_test.go
weichen e8f4e097a7
ci / vet-and-test (pull_request) Failing after 26s
ci / docker-checkout-hostgateway (pull_request) Successful in 1s
Close the checkpoint gaps: pinned eligibility, symlink guard, partial restore, retention (N3 WP3)
Why: rollback eligibility was implicit (any struct with a mark field),
restore followed symlinks out of the root, a single failing file
aborted the rewind leaving unreported partial state, and the log grew
unboundedly with an O(N) read per record.
Evidence: GOWORK=off go vet + go test -count=1 ./... green;
TestCheckpointedToolSetIsPinned (eligibility is exactly
{write_file, edit_file, multi_edit}), TestRewindSkipsSymlinkTarget
(link and outside target untouched, classified Skipped),
TestRewindPartialFailureContinues (Failed reported, rest restored),
TestCompactKeepsRecentAnchors (original seqs kept, monotonic after),
plus pinned edges for empty ordered, identical-rewrite=Already, and
per-record roots; all pre-existing rewind tests pass unchanged.
Impact: Result gains Failed (additive); Rewind no longer aborts on a
single file; symlinks classify as external changes; Compact bounds the
log; Seq is computed from a cached counter instead of a full read.
Refs: Agent-System-Documents#43 (N3 plan WP3)
2026-10-07 15:47:24 +00:00

64 lines
2.0 KiB
Go

package tools
import (
"context"
"os"
"path/filepath"
"testing"
"agent-native/runtime/checkpoint"
)
func TestWriteAndEditRecordCheckpoints(t *testing.T) {
root := t.TempDir()
log, err := checkpoint.Open(filepath.Join(t.TempDir(), "cp.jsonl"))
if err != nil {
t.Fatal(err)
}
reg := WorkspaceToolsCheckpointed(root, nil, log, func() string { return "m1" })
if reg.Execute(context.Background(), "write_file", []byte(`{"path":"a.txt","content":"one"}`)).IsError {
t.Fatal("write")
}
if reg.Execute(context.Background(), "edit_file", []byte(`{"path":"a.txt","old":"one","new":"two"}`)).IsError {
t.Fatal("edit")
}
if err := os.WriteFile(filepath.Join(root, "shell.txt"), []byte("nope"), 0o644); err != nil {
t.Fatal(err)
}
got, err := log.Rewind("m1", []string{"m1"})
if err != nil {
t.Fatal(err)
}
if len(got.Restored) != 1 || got.Restored[0] != "a.txt" {
t.Fatalf("restored %#v skipped %#v", got.Restored, got.Skipped)
}
if _, err := os.Stat(filepath.Join(root, "a.txt")); !os.IsNotExist(err) {
t.Fatal("created file was kept")
}
side, err := os.ReadFile(filepath.Join(root, "shell.txt"))
if err != nil || string(side) != "nope" {
t.Fatalf("shell file %q %v", side, err)
}
}
// ---- N3 WP3:回滚资格集合钉住(验收② 的机器证据)----
// 资格集合恰为 {write_file, edit_file, multi_edit}:三者在集且生产记录,
// 其余内置工具(read/grep/glob/list_dir)永不在集。
func TestCheckpointedToolSetIsPinned(t *testing.T) {
want := map[string]bool{"write_file": true, "edit_file": true, "multi_edit": true}
if len(CheckpointedTools) != len(want) {
t.Fatalf("资格集合漂移:%v", CheckpointedTools)
}
for _, n := range CheckpointedTools {
if !want[n] || !Checkpointed(n) {
t.Fatalf("资格集合漂移:%v", CheckpointedTools)
}
}
for _, n := range []string{"read_file", "grep", "glob", "list_dir", "run_python", "run_shell"} {
if Checkpointed(n) {
t.Fatalf("%s 不得在回滚资格集内", n)
}
}
}