From e2b87e0416e6c877b6f7656af05e3087cd6d5a32 Mon Sep 17 00:00:00 2001 From: Zeusina Date: Fri, 9 Oct 2026 21:50:08 +0300 Subject: [PATCH] feat(cli): add read-only tuning history Co-authored-by: GPT-6 --- cmd/zapret-autotune/history.go | 93 ++++++++++ cmd/zapret-autotune/history_test.go | 259 ++++++++++++++++++++++++++++ cmd/zapret-autotune/main.go | 4 + cmd/zapret-autotune/store.go | 41 ++++- 4 files changed, 393 insertions(+), 4 deletions(-) create mode 100644 cmd/zapret-autotune/history.go create mode 100644 cmd/zapret-autotune/history_test.go diff --git a/cmd/zapret-autotune/history.go b/cmd/zapret-autotune/history.go new file mode 100644 index 0000000..ad282f7 --- /dev/null +++ b/cmd/zapret-autotune/history.go @@ -0,0 +1,93 @@ +package main + +import ( + "database/sql" + "fmt" + "io" + "strconv" + "strings" +) + +const emptyHistory = "No recorded tune runs.\n" +const historyFooter = "No configuration applied. No network requests made; production health: NOT CHECKED by this command.\n" + +func history(args []string, stdout, stderr io.Writer) int { + limit := 20 + valid := len(args) == 0 + if len(args) == 2 && args[0] == "--limit" && args[1] != "" && strings.Trim(args[1], "0123456789") == "" { + var err error + limit, err = strconv.Atoi(args[1]) + valid = err == nil && limit > 0 + } + if !valid { + fmt.Fprintln(stderr, "error: history accepts only --limit N with a positive integer") + fmt.Fprint(stderr, usage) + return 2 + } + db, err := openHistoryStore(databasePath()) + if err != nil { + fmt.Fprintln(stderr, "error: history database:", err) + return 1 + } + text := emptyHistory + historyFooter + if db != nil { + defer db.Close() + text, err = tuneHistory(db, limit) + if err != nil { + fmt.Fprintln(stderr, "error: history database:", err) + return 1 + } + } + if _, err := fmt.Fprint(stdout, text); err != nil { + fmt.Fprintln(stderr, "error: cannot write history:", err) + return 1 + } + return 0 +} + +func tuneHistory(db *sql.DB, limit int) (string, error) { + rows, err := db.Query(`SELECT id, finished_at, target, state, args, error, + (SELECT count(*) FROM protocol_results WHERE run_id = tune_runs.id), + (SELECT count(DISTINCT protocol) FROM protocol_results WHERE run_id = tune_runs.id + AND protocol IN ('tls12', 'tls13') AND passes_ok = 3 AND passes_total = 3) + FROM tune_runs ORDER BY id DESC LIMIT ?`, limit) + if err != nil { + return "", err + } + defer rows.Close() + var text strings.Builder + for rows.Next() { + var id int64 + var finished, target, state string + var args, runError sql.NullString + var total, verified int + if err := rows.Scan(&id, &finished, &target, &state, &args, &runError, &total, &verified); err != nil { + return "", err + } + switch state { + case "PROPOSED": + if total != 2 || verified != 2 { + return "", fmt.Errorf("proposed run %d has incomplete protocol evidence", id) + } + if !args.Valid || args.String == "" || runError.Valid { + return "", fmt.Errorf("proposed run %d has malformed outcome", id) + } + fmt.Fprintf(&text, "Run %d; finished %s; %s frontend only; PROPOSED, NOT ACTIVE; Args: %q; recorded TLS1.2 3/3; TLS1.3 3/3\n", id, finished, target, args.String) + case "FAILED": + if !runError.Valid || runError.String == "" || args.Valid { + return "", fmt.Errorf("failed run %d has malformed outcome", id) + } + fmt.Fprintf(&text, "Run %d; finished %s; %s frontend only; FAILED; Error: %q\n", id, finished, target, runError.String) + default: + return "", fmt.Errorf("run %d has unknown state %q", id, state) + } + } + if err := rows.Err(); err != nil { + return "", err + } + if text.Len() == 0 { + text.WriteString(emptyHistory) + } + text.WriteString(historyFooter) + return text.String(), nil +} diff --git a/cmd/zapret-autotune/history_test.go b/cmd/zapret-autotune/history_test.go new file mode 100644 index 0000000..935588a --- /dev/null +++ b/cmd/zapret-autotune/history_test.go @@ -0,0 +1,259 @@ +package main + +import ( + "bytes" + "database/sql" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestHistoryMissingDatabase(t *testing.T) { + dir := filepath.Join(t.TempDir(), "missing") + path := filepath.Join(dir, "history.db") + t.Setenv("ZAPRET_AUTOTUNE_DB", path) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 0 || stderr.Len() != 0 || stdout.String() != emptyHistory+historyFooter { + t.Fatalf("missing: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + for _, name := range []string{path, dir} { + if _, err := os.Stat(name); !os.IsNotExist(err) { + t.Fatalf("history created %q or stat failed: %v", name, err) + } + } +} + +func TestHistoryStoreReadOnly(t *testing.T) { + for _, name := range []string{"history.db", "history?.db#%", "history.db?mode=rw&cache=shared"} { + t.Run(name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), name) + db, err := openStore(path) + if err != nil { + t.Fatal(err) + } + if err := saveRun(db, time.Now(), "", "", nil, errors.New("scan failed")); err != nil { + db.Close() + t.Fatal(err) + } + if err := db.Close(); err != nil { + t.Fatal(err) + } + if err := os.Chmod(path, 0400); err != nil { + t.Fatal(err) + } + ro, err := openHistoryStore(path) + if err != nil || ro == nil { + t.Fatalf("open read-only: db=%v err=%v", ro, err) + } + defer ro.Close() + if text, err := tuneHistory(ro, 20); err != nil || !strings.Contains(text, `FAILED; Error: "scan failed"`) { + t.Fatalf("query: text=%q err=%v", text, err) + } + // Disabling query_only cannot bypass the SQLite connection's mode=ro. + if _, err := ro.Exec("PRAGMA query_only = OFF"); err != nil { + t.Fatal(err) + } + for _, query := range []string{"DELETE FROM tune_runs", "CREATE TABLE history_write (id INTEGER)", "PRAGMA user_version = 99"} { + if _, err := ro.Exec(query); err == nil { + t.Fatalf("read-only store accepted %q", query) + } + } + t.Setenv("ZAPRET_AUTOTUNE_DB", path) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 0 || stderr.Len() != 0 || !strings.Contains(stdout.String(), "Run 1;") { + t.Fatalf("history: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + if info, err := os.Stat(path); err != nil || info.Mode().Perm() != 0400 { + t.Fatalf("history changed permissions: info=%v err=%v", info, err) + } + }) + } +} + +func TestHistoryDoesNotMigrate(t *testing.T) { + path := filepath.Join(t.TempDir(), "empty.db") + if err := os.WriteFile(path, nil, 0640); err != nil { + t.Fatal(err) + } + before, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + t.Setenv("ZAPRET_AUTOTUNE_DB", path) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "database") { + t.Fatalf("uninitialized: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + if after, err := os.Stat(path); err != nil || after.Size() != 0 || after.Mode() != before.Mode() { + t.Fatalf("history initialized/chmodded database: info=%v err=%v", after, err) + } +} + +func TestHistoryRejectsDatabaseDSN(t *testing.T) { + for _, path := range []string{"", ":memory:", ":memory:?cache=shared", "file:missing.db?mode=rwc", "FILE:missing.db"} { + if db, err := openHistoryStore(path); err == nil || db != nil { + if db != nil { + db.Close() + } + t.Fatalf("accepted DSN %q: err=%v", path, err) + } + } +} + +func historySnapshot(t *testing.T, db *sql.DB) string { + t.Helper() + var runs, results string + for query, dest := range map[string]*string{ + `SELECT coalesce(group_concat(row, char(10)), '') FROM (SELECT quote(id)||'|'||quote(started_at)||'|'||quote(finished_at)||'|'||quote(target)||'|'||quote(state)||'|'||quote(args)||'|'||quote(output)||'|'||quote(error) AS row FROM tune_runs ORDER BY id)`: &runs, + `SELECT coalesce(group_concat(row, char(10)), '') FROM (SELECT quote(run_id)||'|'||quote(protocol)||'|'||quote(passes_ok)||'|'||quote(passes_total) AS row FROM protocol_results ORDER BY run_id, protocol)`: &results, + } { + if err := db.QueryRow(query).Scan(dest); err != nil { + t.Fatal(err) + } + } + return runs + "\n---\n" + results +} + +func TestHistoryEmpty(t *testing.T) { + db := testStore(t) + before := historySnapshot(t, db) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 0 || stderr.Len() != 0 || !strings.Contains(stdout.String(), "No recorded tune runs") || strings.Contains(stdout.String(), "3/3") { + t.Fatalf("empty: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + if after := historySnapshot(t, db); after != before { + t.Fatal("history changed rows") + } +} + +func TestHistoryMixedAndLimit(t *testing.T) { + db := testStore(t) + saveProposal(t, db, "--lua-desync=multisplit") + if err := saveRun(db, time.Now(), "", "", nil, errors.New("failed scan\nsecond line")); err != nil { + t.Fatal(err) + } + saveProposal(t, db, "--lua-desync=fake") + if _, err := db.Exec(`UPDATE tune_runs SET finished_at = '2026-10-09T01:00:00Z' WHERE id = 3`); err != nil { + t.Fatal(err) + } + before := historySnapshot(t, db) + for _, args := range [][]string{{"history"}, {"history", "--limit", "2"}, {"history", "--limit", "1"}} { + var stdout, stderr bytes.Buffer + if code := run(args, &stdout, &stderr); code != 0 || stderr.Len() != 0 { + t.Fatalf("args=%v code=%d err=%q", args, code, stderr.String()) + } + out := stdout.String() + for _, want := range []string{"Run 3; finished 2026-10-09T01:00:00Z", "youtube.com frontend only", "PROPOSED, NOT ACTIVE", `Args: "--lua-desync=fake"`, "TLS1.2 3/3", "TLS1.3 3/3", "No configuration applied", "No network requests made", "production health: NOT CHECKED"} { + if !strings.Contains(out, want) { + t.Errorf("output %q missing %q", out, want) + } + } + wantRows := 3 + if len(args) == 3 { + if args[2] == "1" { + wantRows = 1 + } else { + wantRows = 2 + } + } + if strings.Count(out, "Run ") != wantRows || strings.Contains(out, "Run 1;") != (wantRows == 3) { + t.Fatalf("incorrect limit: %q", out) + } + if wantRows >= 2 && (!strings.Contains(out, `FAILED; Error: "failed scan\nsecond line"`) || strings.Index(out, "Run 3;") >= strings.Index(out, "Run 2;")) { + t.Fatalf("missing failure or wrong order: %q", out) + } + if wantRows == 3 && strings.Index(out, "Run 2;") >= strings.Index(out, "Run 1;") { + t.Fatalf("wrong order: %q", out) + } + if after := historySnapshot(t, db); after != before { + t.Fatal("history changed rows") + } + } +} + +func TestHistoryDefaultLimit(t *testing.T) { + db := testStore(t) + for i := 0; i < 21; i++ { + if err := saveRun(db, time.Now(), "", "", nil, errors.New("scan failed")); err != nil { + t.Fatal(err) + } + } + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 0 || strings.Count(stdout.String(), "Run ") != 20 || strings.Contains(stdout.String(), "Run 1;") { + t.Fatalf("default limit: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } +} + +func TestHistoryInvalidArgs(t *testing.T) { + for _, args := range [][]string{{"--limit"}, {"--limit=1"}, {"extra"}, {"--limit", "1", "extra"}, {"--other", "1"}, {"--limit", ""}, {"--limit", "0"}, {"--limit", "-1"}, {"--limit", "+1"}, {"--limit", "1.5"}, {"--limit", " 1"}, {"--limit", "1x"}, {"--limit", "999999999999999999999999"}} { + var stdout, stderr bytes.Buffer + if code := run(append([]string{"history"}, args...), &stdout, &stderr); code != 2 || stdout.Len() != 0 || !strings.Contains(stderr.String(), usage) { + t.Fatalf("args=%v code=%d out=%q err=%q", args, code, stdout.String(), stderr.String()) + } + } +} + +func TestHistoryRejectsMalformedEvidence(t *testing.T) { + for _, statement := range []string{ + `DELETE FROM protocol_results WHERE run_id = 1 AND protocol = 'tls12'`, + `DELETE FROM protocol_results WHERE run_id = 1 AND protocol = 'tls13'`, + `UPDATE protocol_results SET passes_ok = 2 WHERE run_id = 1 AND protocol = 'tls12'`, + `UPDATE protocol_results SET passes_total = 4 WHERE run_id = 1 AND protocol = 'tls13'`, + `INSERT INTO protocol_results VALUES (1, 'quic', 3, 3)`, + } { + t.Run(statement, func(t *testing.T) { + db := testStore(t) + saveProposal(t, db, "--lua-desync=multisplit") + saveProposal(t, db, "--lua-desync=fake") + if _, err := db.Exec("PRAGMA ignore_check_constraints = ON"); err != nil { + t.Fatal(err) + } + if _, err := db.Exec(statement); err != nil { + t.Fatal(err) + } + before := historySnapshot(t, db) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "incomplete protocol evidence") { + t.Fatalf("evidence: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + if after := historySnapshot(t, db); after != before { + t.Fatal("history changed malformed rows") + } + }) + } +} + +func TestHistoryErrors(t *testing.T) { + t.Run("writer", func(t *testing.T) { + db := testStore(t) + saveProposal(t, db, "--lua-desync=fake") + before := historySnapshot(t, db) + var stderr bytes.Buffer + if code := run([]string{"history"}, failingWriter{}, &stderr); code != 1 || !strings.Contains(stderr.String(), "cannot write history") { + t.Fatalf("writer: code=%d err=%q", code, stderr.String()) + } + if after := historySnapshot(t, db); after != before { + t.Fatal("writer failure changed rows") + } + }) + t.Run("database open", func(t *testing.T) { + t.Setenv("ZAPRET_AUTOTUNE_DB", t.TempDir()) + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "database") { + t.Fatalf("open: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + }) + t.Run("query", func(t *testing.T) { + db := testStore(t) + if _, err := db.Exec(`DROP TABLE protocol_results`); err != nil { + t.Fatal(err) + } + var stdout, stderr bytes.Buffer + if code := run([]string{"history"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "database") { + t.Fatalf("query: code=%d out=%q err=%q", code, stdout.String(), stderr.String()) + } + }) +} diff --git a/cmd/zapret-autotune/main.go b/cmd/zapret-autotune/main.go index cd5e7a5..0a8f6ab 100644 --- a/cmd/zapret-autotune/main.go +++ b/cmd/zapret-autotune/main.go @@ -10,6 +10,7 @@ const usage = `Usage: zapret-autotune Commands: status Show latest recorded tune run/proposal (not live service health) + history [--limit N] List recorded tune attempts (default 20; proposals not active) strategies youtube List recorded proposals (not active), newest first discover youtube Refuse discovery until provider/config is agreed check youtube Refuse checks until probe URLs/video IDs are configured and agreed @@ -22,6 +23,9 @@ func main() { } func run(args []string, stdout, stderr io.Writer) int { + if len(args) > 0 && args[0] == "history" { + return history(args[1:], stdout, stderr) + } if len(args) == 2 && args[0] == "strategies" && args[1] == "youtube" { return strategiesYoutube(stdout, stderr) } diff --git a/cmd/zapret-autotune/store.go b/cmd/zapret-autotune/store.go index d384f11..c7eff99 100644 --- a/cmd/zapret-autotune/store.go +++ b/cmd/zapret-autotune/store.go @@ -23,7 +23,7 @@ func databasePath() string { return "/var/lib/zapret-autotune/autotune.db" } -func openStore(path string) (*sql.DB, error) { +func storeURI(path string) (*url.URL, error) { // Configuration is a filesystem path, never a caller-supplied SQLite DSN. if path == "" || path == ":memory:" || strings.HasPrefix(path, ":memory:?") || strings.HasPrefix(strings.ToLower(path), "file:") { return nil, fmt.Errorf("database location must be a filesystem path, not a SQLite DSN") @@ -32,6 +32,41 @@ func openStore(path string) (*sql.DB, error) { if err != nil { return nil, err } + // Escape path characters so they cannot become URI options or fragments. + return &url.URL{Scheme: "file", Path: filepath.ToSlash(path)}, nil +} + +// A nil database means the validated path does not exist. Never initialize it. +func openHistoryStore(path string) (*sql.DB, error) { + uri, err := storeURI(path) + if err != nil { + return nil, err + } + if _, err := os.Stat(filepath.FromSlash(uri.Path)); err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, err + } + uri.RawQuery = "mode=ro" + db, err := sql.Open("sqlite", uri.String()) + if err != nil { + return nil, err + } + db.SetMaxOpenConns(1) + if err := db.Ping(); err != nil { + db.Close() + return nil, err + } + return db, nil +} + +func openStore(path string) (*sql.DB, error) { + uri, err := storeURI(path) + if err != nil { + return nil, err + } + path = filepath.FromSlash(uri.Path) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { return nil, err } @@ -46,9 +81,7 @@ func openStore(path string) (*sql.DB, error) { if err := file.Close(); err != nil { return nil, err } - // Escape path characters so they cannot become URI options or fragments. - dsn := (&url.URL{Scheme: "file", Path: filepath.ToSlash(path)}).String() - db, err := sql.Open("sqlite", dsn) + db, err := sql.Open("sqlite", uri.String()) if err != nil { return nil, err }