feat(cli): add read-only tuning history
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
8466f47ceb
commit
e2b87e0416
4 files changed
+393
-4
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user