From b38b990bc4e76ba8edf3a222c96df27d6a3550f3 Mon Sep 17 00:00:00 2001 From: saymrwulf Date: Tue, 7 Jul 2026 15:43:05 +0200 Subject: [PATCH] Add `evote cockpit --tmux`: one terminal pane per stakeholder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The terminal systems-view half of the cockpit, fed by the SAME trace stream as the browser math view — no double-maintenance. - cockpit_tmux.go: spawns a tmux session with one pane per stakeholder (setup, CC0–CC3, electoral board, voting server, verifier, and an aggregate voters pane), each running `evote panelview`. The ceremony runs in the background, appending paced NDJSON events to a shared temp file the panes tail. Guards for missing tmux and for being run inside an existing tmux session. - panelview.go: a hidden subcommand each pane runs — follows the shared event file, filters to its stakeholder, and renders that party's operations as colored ASCII/Unicode math with elided live values. - Attribute setup-phase Schnorr challenges to the generating CC and verify-phase challenges to the verifier (trace.SetContext), so every pane gets its ops. Verified: panelview filters and renders correctly per role; a 9-pane tiled tmux session builds with the real pane commands. (Interactive attach needs a TTY, so the live attach is exercised when you run it.) Co-Authored-By: Claude Fable 5 --- cmd/evote/cockpit.go | 7 +- cmd/evote/cockpit_tmux.go | 113 +++++++++++++++++++++++++++ cmd/evote/panelview.go | 156 ++++++++++++++++++++++++++++++++++++++ pkg/party/setup.go | 1 + pkg/party/verify.go | 2 +- 5 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 cmd/evote/cockpit_tmux.go create mode 100644 cmd/evote/panelview.go diff --git a/cmd/evote/cockpit.go b/cmd/evote/cockpit.go index 0e09d13..7a9c149 100644 --- a/cmd/evote/cockpit.go +++ b/cmd/evote/cockpit.go @@ -21,6 +21,7 @@ var ( cockpitVoters int cockpitOptions int cockpitDelayMs int + cockpitTmux bool cockpitRunning atomic.Bool ) @@ -39,15 +40,19 @@ var cockpitCmd = &cobra.Command{ if cockpitOptions < 1 || cockpitOptions > 200 { return fmt.Errorf("--options must be in [1, 200]") } + if cockpitTmux { + return runCockpitTmux() + } return runCockpit() }, } func init() { - cockpitCmd.Flags().IntVar(&cockpitPort, "port", 8090, "HTTP port") + cockpitCmd.Flags().IntVar(&cockpitPort, "port", 8090, "HTTP port (browser mode)") cockpitCmd.Flags().IntVar(&cockpitVoters, "voters", 3, "Number of voters") cockpitCmd.Flags().IntVar(&cockpitOptions, "options", 3, "Number of voting options") cockpitCmd.Flags().IntVar(&cockpitDelayMs, "delay", 350, "Milliseconds between events (pacing so it's watchable)") + cockpitCmd.Flags().BoolVar(&cockpitTmux, "tmux", false, "Terminal mode: one tmux pane per stakeholder instead of the browser") rootCmd.AddCommand(cockpitCmd) } diff --git a/cmd/evote/cockpit_tmux.go b/cmd/evote/cockpit_tmux.go new file mode 100644 index 0000000..7fb6e48 --- /dev/null +++ b/cmd/evote/cockpit_tmux.go @@ -0,0 +1,113 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "time" + + "github.com/user/evote/pkg/party" + "github.com/user/evote/pkg/trace" +) + +// runCockpitTmux runs the ceremony and shows one tmux pane per stakeholder, each +// rendering that party's live crypto activity as ASCII/Unicode math. The panes +// follow a shared NDJSON event file that the ceremony writes to, paced. +func runCockpitTmux() error { + if _, err := exec.LookPath("tmux"); err != nil { + return fmt.Errorf("tmux not found on PATH: %w", err) + } + if os.Getenv("TMUX") != "" { + return fmt.Errorf("already inside a tmux session — run `evote cockpit --tmux` from a plain terminal (tmux cannot nest an attach)") + } + exe, err := os.Executable() + if err != nil { + return err + } + + // Shared event file the panes tail and the ceremony appends to. + f, err := os.CreateTemp("", "evote-cockpit-*.ndjson") + if err != nil { + return err + } + eventPath := f.Name() + defer os.Remove(eventPath) + + // One pane per infrastructure party, plus one aggregate pane for all voters. + panes := []string{ + party.NameSetup, + party.CCName(0), party.CCName(1), party.CCName(2), party.CCName(3), + party.NameEB, party.NameServer, party.NameVerifier, + "voters", + } + + session := "evote-cockpit" + _ = exec.Command("tmux", "kill-session", "-t", session).Run() // best-effort clean slate + + paneCmd := func(role string) string { + return fmt.Sprintf("%s panelview --role=%s --file=%s", exe, role, eventPath) + } + + // First pane creates the (detached) session. + if out, err := exec.Command("tmux", "new-session", "-d", "-s", session, paneCmd(panes[0])).CombinedOutput(); err != nil { + return fmt.Errorf("tmux new-session: %v: %s", err, out) + } + for _, role := range panes[1:] { + if out, err := exec.Command("tmux", "split-window", "-t", session, paneCmd(role)).CombinedOutput(); err != nil { + exec.Command("tmux", "kill-session", "-t", session).Run() + return fmt.Errorf("tmux split-window: %v: %s", err, out) + } + // Re-tile after each split so panes stay balanced. + exec.Command("tmux", "select-layout", "-t", session, "tiled").Run() + } + exec.Command("tmux", "select-layout", "-t", session, "tiled").Run() + exec.Command("tmux", "set-option", "-t", session, "mouse", "on").Run() + + // Run the ceremony in the background, writing paced NDJSON to the file. + go writeCeremonyEvents(f) + + fmt.Printf("Launching tmux cockpit '%s' — %d panes. Detach with Ctrl-b d; it closes when you exit.\n", session, len(panes)) + time.Sleep(400 * time.Millisecond) // let panes open the file first + + // Attach in the foreground; blocks until the user detaches or exits. + attach := exec.Command("tmux", "attach", "-t", session) + attach.Stdin, attach.Stdout, attach.Stderr = os.Stdin, os.Stdout, os.Stderr + err = attach.Run() + + exec.Command("tmux", "kill-session", "-t", session).Run() + if err != nil { + return fmt.Errorf("tmux attach failed (run this from an interactive terminal): %w", err) + } + return nil +} + +// writeCeremonyEvents subscribes a paced file sink, runs the full ceremony, and +// writes a final done marker. +func writeCeremonyEvents(f *os.File) { + delay := time.Duration(cockpitDelayMs) * time.Millisecond + sink := &fileSink{f: f, delay: delay, enc: json.NewEncoder(f)} + unsub := trace.Subscribe(sink) + defer unsub() + + _ = runCockpitCeremony() + + f.WriteString(`{"done":true}` + "\n") + f.Sync() +} + +// fileSink appends each event as one JSON line, pacing between events so the +// panes render at a watchable rate. +type fileSink struct { + f *os.File + delay time.Duration + enc *json.Encoder +} + +func (s *fileSink) Handle(e trace.Event) { + _ = s.enc.Encode(e) // Encode writes the trailing newline + s.f.Sync() + if s.delay > 0 { + time.Sleep(s.delay) + } +} diff --git a/cmd/evote/panelview.go b/cmd/evote/panelview.go new file mode 100644 index 0000000..e3eca6f --- /dev/null +++ b/cmd/evote/panelview.go @@ -0,0 +1,156 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "os" + "strings" + "time" + + "github.com/spf13/cobra" + "github.com/user/evote/pkg/trace" +) + +var ( + panelRole string + panelFile string + panelWidth int +) + +// panelViewCmd is run inside each tmux pane. It follows a shared NDJSON event +// file and renders the operations belonging to one stakeholder as ASCII/Unicode +// math. It is an internal helper for `evote cockpit --tmux`, but works standalone +// against any NDJSON trace file. +var panelViewCmd = &cobra.Command{ + Use: "panelview", + Short: "Render one stakeholder's live crypto activity (used by cockpit --tmux)", + Hidden: true, + RunE: func(cmd *cobra.Command, args []string) error { + if panelRole == "" || panelFile == "" { + return fmt.Errorf("--role and --file are required") + } + return runPanelView() + }, +} + +func init() { + panelViewCmd.Flags().StringVar(&panelRole, "role", "", "Stakeholder role to display (or 'voters', 'all')") + panelViewCmd.Flags().StringVar(&panelFile, "file", "", "Shared NDJSON event file to follow") + panelViewCmd.Flags().IntVar(&panelWidth, "width", 0, "Wrap width (0 = terminal default)") + rootCmd.AddCommand(panelViewCmd) +} + +// ANSI colors keyed by operation kind. +var kindColor = map[trace.Kind]string{ + trace.KindSign: "\x1b[35m", // magenta + trace.KindShuffle: "\x1b[95m", // bright magenta + trace.KindEncrypt: "\x1b[32m", // green + trace.KindDecrypt: "\x1b[36m", // cyan + trace.KindChallenge: "\x1b[33m", // yellow + trace.KindKeyEx: "\x1b[34m", // blue + trace.KindProof: "\x1b[36m", + trace.KindVerify: "\x1b[92m", + trace.KindSample: "\x1b[36m", +} + +const ( + ansiReset = "\x1b[0m" + ansiBold = "\x1b[1m" + ansiDim = "\x1b[2m" +) + +func panelMatches(role, party string) bool { + if role == "all" { + return true + } + if role == "voters" { + return strings.HasPrefix(party, "voter") + } + return party == role +} + +func runPanelView() error { + // Header. + title := panelRole + switch { + case panelRole == "voters": + title = "Voters" + case strings.HasPrefix(panelRole, "control-component-"): + title = "CC" + strings.TrimPrefix(panelRole, "control-component-") + default: + title = strings.Title(strings.ReplaceAll(panelRole, "-", " ")) + } + fmt.Printf("%s%s▐ %s ▌%s\n", ansiBold, "\x1b[7m", title, ansiReset) + fmt.Printf("%swaiting for activity…%s\n", ansiDim, ansiReset) + + f, err := openWithRetry(panelFile, 5*time.Second) + if err != nil { + return err + } + defer f.Close() + reader := bufio.NewReader(f) + + shown := 0 + for { + line, err := reader.ReadString('\n') + if len(line) > 0 { + line = strings.TrimSpace(line) + if line == "" { + continue + } + if line == `{"done":true}` { + fmt.Printf("\n%s%s✓ ceremony complete%s\n", ansiBold, "\x1b[32m", ansiReset) + return nil + } + var e trace.Event + if json.Unmarshal([]byte(line), &e) == nil && panelMatches(panelRole, e.Party) { + if shown == 0 { + fmt.Print("\x1b[1A\x1b[2K") // erase the "waiting…" line + } + printPanelEvent(e) + shown++ + } + } + if err != nil { // EOF: wait for more lines + time.Sleep(80 * time.Millisecond) + } + } +} + +func printPanelEvent(e trace.Event) { + col := kindColor[e.Kind] + if col == "" { + col = "\x1b[37m" + } + fmt.Printf("%s%s%-9s%s %s#%d%s %s\n", ansiBold, col, strings.ToUpper(string(e.Kind)), ansiReset, + ansiDim, e.Seq, ansiReset, e.Caption) + if e.ASCII != "" { + fmt.Printf(" %s%s%s\n", col, e.ASCII, ansiReset) + } else if e.LaTeX != "" { + fmt.Printf(" %s%s%s\n", ansiDim, e.LaTeX, ansiReset) + } + if len(e.Values) > 0 { + parts := make([]string, 0, len(e.Values)) + for k, v := range e.Values { + parts = append(parts, fmt.Sprintf("%s=%s", k, trace.Short(v))) + } + fmt.Printf(" %s%s%s\n", ansiDim, strings.Join(parts, " "), ansiReset) + } +} + +// openWithRetry waits up to timeout for the file to appear (the writer may start +// a moment after the panes). +func openWithRetry(path string, timeout time.Duration) (*os.File, error) { + deadline := time.Now().Add(timeout) + for { + f, err := os.Open(path) + if err == nil { + return f, nil + } + if time.Now().After(deadline) { + return nil, err + } + time.Sleep(50 * time.Millisecond) + } +} diff --git a/pkg/party/setup.go b/pkg/party/setup.go index 7a926f3..0ff6251 100644 --- a/pkg/party/setup.go +++ b/pkg/party/setup.go @@ -170,6 +170,7 @@ func (p *ControlComponent) handleGenCCKeys(env *transport.Envelope) (*transport. group := cfg.Group zq := emath.ZqGroupFromGqGroup(group) + trace.SetContext(p.id.Name, "setup") kp := elgamal.GenKeyPair(group, req.NumOptions) proofs := make([]zkp.SchnorrProof, req.NumOptions) wireProofs := make([]wireSchnorr, req.NumOptions) diff --git a/pkg/party/verify.go b/pkg/party/verify.go index 94b6cef..b0a7efe 100644 --- a/pkg/party/verify.go +++ b/pkg/party/verify.go @@ -14,7 +14,7 @@ import ( // decryptions). It returns nil only if every check passes. The verifier holds no // secret — it works purely from the bulletin-board transcript. func (c *Ceremony) RunVerify() error { - trace.Phase("verify") + trace.SetContext(NameVerifier, "verify") c.logf("\n--- VERIFICATION PHASE (multi-party) ---") tr := c.Transcript group := c.Config.Group