feat(cli): list proposed tuning strategies
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
e868266de6
commit
8466f47ceb
3 files changed
+161
No files matched your search
@@ -10,6 +10,7 @@ const usage = `Usage: zapret-autotune <command>
|
||||
|
||||
Commands:
|
||||
status Show latest recorded tune run/proposal (not live service health)
|
||||
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
|
||||
tune youtube Test youtube.com frontend only; print options without applying
|
||||
@@ -21,6 +22,9 @@ func main() {
|
||||
}
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) == 2 && args[0] == "strategies" && args[1] == "youtube" {
|
||||
return strategiesYoutube(stdout, stderr)
|
||||
}
|
||||
if len(args) == 2 && args[0] == "tune" && args[1] == "youtube" {
|
||||
return tuneYoutube(stdout, stderr, runBlockcheck)
|
||||
}
|
||||
|
||||
@@ -184,6 +184,57 @@ func latestRun(db *sql.DB) (storedRun, error) {
|
||||
return r, err
|
||||
}
|
||||
|
||||
func proposedStrategies(db *sql.DB) (string, error) {
|
||||
rows, err := db.Query(`SELECT id, finished_at, args,
|
||||
(SELECT count(*) 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 WHERE state = 'PROPOSED' AND target = 'youtube.com' ORDER BY id DESC`)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rows.Close()
|
||||
var text strings.Builder
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var finished, args string
|
||||
var results int
|
||||
if err := rows.Scan(&id, &finished, &args, &results); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if results != 2 {
|
||||
return "", fmt.Errorf("proposed run %d has incomplete protocol evidence", id)
|
||||
}
|
||||
fmt.Fprintf(&text, "Run %d; finished %s; youtube.com frontend only; PROPOSED, NOT ACTIVE\nArgs: %s\nRecorded combined check evidence: TLS1.2 3/3; TLS1.3 3/3.\n", id, finished, args)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if text.Len() == 0 {
|
||||
text.WriteString("No recorded strategies for youtube.com.\n")
|
||||
}
|
||||
text.WriteString("No configuration applied. Throughput, GGC and production health: NOT CHECKED by this command.\n")
|
||||
return text.String(), nil
|
||||
}
|
||||
|
||||
func strategiesYoutube(stdout, stderr io.Writer) int {
|
||||
db, err := openStore(databasePath())
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error: strategies youtube database:", err)
|
||||
return 1
|
||||
}
|
||||
defer db.Close()
|
||||
text, err := proposedStrategies(db)
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error: strategies youtube database:", err)
|
||||
return 1
|
||||
}
|
||||
if _, err := fmt.Fprint(stdout, text); err != nil {
|
||||
fmt.Fprintln(stderr, "error: cannot write strategies:", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func status(stdout, stderr io.Writer) int {
|
||||
db, err := openStore(databasePath())
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestStrategiesHistory(t *testing.T) {
|
||||
db := testStore(t)
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := run([]string{"strategies", "youtube"}, &stdout, &stderr); code != 0 || stderr.Len() != 0 || !strings.Contains(stdout.String(), "No recorded strategies") {
|
||||
t.Fatalf("empty history: code=%d out=%q err=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
if strings.Contains(stdout.String(), "TLS1.2 3/3") {
|
||||
t.Fatal("empty history must not claim evidence")
|
||||
}
|
||||
saveProposal(t, db, "--lua-desync=multisplit")
|
||||
if err := saveRun(db, time.Now(), "", "", nil, errors.New("failed scan")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
saveProposal(t, db, "--lua-desync=fake")
|
||||
for _, statement := range []string{
|
||||
`UPDATE tune_runs SET finished_at = '2026-10-01T01:00:00Z' WHERE id = 1`,
|
||||
`UPDATE tune_runs SET finished_at = '2026-10-02T01:00:00Z' WHERE id = 3`,
|
||||
} {
|
||||
if _, err := db.Exec(statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
stdout.Reset()
|
||||
if code := run([]string{"strategies", "youtube"}, &stdout, &stderr); code != 0 || stderr.Len() != 0 {
|
||||
t.Fatalf("history: code=%d err=%q", code, stderr.String())
|
||||
}
|
||||
out := stdout.String()
|
||||
for _, want := range []string{"Run 3; finished 2026-10-02T01:00:00Z", "Run 1; finished 2026-10-01T01:00:00Z", "Args: --lua-desync=fake", "Args: --lua-desync=multisplit", "No configuration applied", "Throughput, GGC and production health: NOT CHECKED"} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("output %q missing %q", out, want)
|
||||
}
|
||||
}
|
||||
for _, label := range []string{"youtube.com frontend only", "PROPOSED, NOT ACTIVE", "TLS1.2 3/3", "TLS1.3 3/3"} {
|
||||
if strings.Count(out, label) != 2 {
|
||||
t.Errorf("expected two %q labels in %q", label, out)
|
||||
}
|
||||
}
|
||||
if strings.Index(out, "Run 3;") >= strings.Index(out, "Run 1;") || strings.Contains(out, "Run 2;") || strings.Contains(out, "failed scan") {
|
||||
t.Fatalf("incorrect history order/filter: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrategiesRejectsIncompleteEvidence(t *testing.T) {
|
||||
for _, protocol := range []string{"tls12", "tls13"} {
|
||||
t.Run(protocol, func(t *testing.T) {
|
||||
db := testStore(t)
|
||||
saveProposal(t, db, "--lua-desync=multisplit")
|
||||
saveProposal(t, db, "--lua-desync=fake")
|
||||
// Even an older incomplete proposal invalidates the history output.
|
||||
if _, err := db.Exec(`DELETE FROM protocol_results WHERE run_id = 1 AND protocol = ?`, protocol); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := run([]string{"strategies", "youtube"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "incomplete protocol evidence") {
|
||||
t.Fatalf("incomplete history: code=%d out=%q err=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrategiesUnsupportedArgs(t *testing.T) {
|
||||
for _, args := range [][]string{{"strategies"}, {"strategies", "other"}, {"strategies", "YouTube"}, {"strategies", "youtube", "extra"}, {"strategies", "youtube", "--help"}} {
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := run(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 TestStrategiesErrors(t *testing.T) {
|
||||
t.Run("writer", func(t *testing.T) {
|
||||
db := testStore(t)
|
||||
saveProposal(t, db, "--lua-desync=multisplit")
|
||||
var stderr bytes.Buffer
|
||||
if code := run([]string{"strategies", "youtube"}, failingWriter{}, &stderr); code != 1 || !strings.Contains(stderr.String(), "cannot write strategies") {
|
||||
t.Fatalf("writer: code=%d err=%q", code, stderr.String())
|
||||
}
|
||||
})
|
||||
t.Run("database open", func(t *testing.T) {
|
||||
t.Setenv("ZAPRET_AUTOTUNE_DB", t.TempDir())
|
||||
var stdout, stderr bytes.Buffer
|
||||
if code := run([]string{"strategies", "youtube"}, &stdout, &stderr); code != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), "database") {
|
||||
t.Fatalf("database: 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{"strategies", "youtube"}, &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())
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user