// Command hypr-agent records focused-window time on Hyprland and uploads it // to the tracker server. // // hypr-agent run run the collector (usually via systemd --user) // hypr-agent idle|resume sent by hypridle on-timeout / on-resume // hypr-agent lock|unlock sent around your screen locker package main import ( "context" "errors" "flag" "fmt" "log/slog" "net/http" "os" "os/signal" "path/filepath" "regexp" "strconv" "strings" "syscall" "time" "ccp/internal/agent/hypr" "ccp/internal/ingest" ) func main() { if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "usage: hypr-agent run|idle|resume|lock|unlock [flags]") os.Exit(2) } var err error switch cmd := os.Args[1]; { case cmd == "run": err = run(os.Args[2:]) case hypr.Commands[cmd]: err = hypr.SendControl(cmd) default: err = fmt.Errorf("unknown command %q", cmd) } if err != nil { fmt.Fprintln(os.Stderr, "error:", err) os.Exit(1) } } func run(args []string) error { fs := flag.NewFlagSet("run", flag.ContinueOnError) server := fs.String("server", os.Getenv("TRACKER_URL"), "tracker server base URL [TRACKER_URL]") key := fs.String("key", os.Getenv("TRACKER_API_KEY"), "API key [TRACKER_API_KEY]") keyFile := fs.String("key-file", os.Getenv("TRACKER_API_KEY_FILE"), "file containing the API key [TRACKER_API_KEY_FILE]") titles := fs.Bool("titles", envBool("HYPR_AGENT_TITLES", true), "record window titles [HYPR_AGENT_TITLES]") redact := fs.String("redact", os.Getenv("HYPR_AGENT_REDACT"), "regexp; matching titles are replaced with [redacted] [HYPR_AGENT_REDACT]") minDur := fs.Duration("min-duration", 2*time.Second, "drop focus intervals shorter than this") flush := fs.Duration("flush", time.Minute, "upload interval") bufPath := fs.String("buffer", defaultBufferPath(), "pending-events file") if err := fs.Parse(args); err != nil { return err } if *server == "" { return errors.New("--server (or TRACKER_URL) is required") } if *keyFile != "" { b, err := os.ReadFile(*keyFile) if err != nil { return err } *key = strings.TrimSpace(string(b)) } if *key == "" { return errors.New("--key, --key-file, TRACKER_API_KEY or TRACKER_API_KEY_FILE is required") } var redactRe *regexp.Regexp if *redact != "" { var err error if redactRe, err = regexp.Compile(*redact); err != nil { return fmt.Errorf("--redact: %w", err) } } log := slog.New(slog.NewTextHandler(os.Stderr, nil)) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() buf, err := hypr.OpenBuffer(*bufPath) if err != nil { return fmt.Errorf("open buffer: %w", err) } up := &hypr.Uploader{ URL: *server, Key: *key, Buf: buf, Client: &http.Client{Timeout: 15 * time.Second}, Log: log, } sess := &hypr.Sessionizer{ MinDuration: *minDur, Emit: func(e ingest.Event) { if err := buf.Add(e); err != nil { log.Error("buffer event", "err", err) } }, } cleanTitle := func(f hypr.Focus) hypr.Focus { switch { case !*titles: f.Title = "" case redactRe != nil && redactRe.MatchString(f.Title): f.Title = "[redacted]" } return f } focus := make(chan hypr.FocusEvent, 64) control := make(chan hypr.ControlEvent, 8) errc := make(chan error, 2) go func() { errc <- hypr.Listen(ctx, focus, log) }() go func() { errc <- hypr.ServeControl(ctx, control) }() go up.Run(ctx, *flush) log.Info("hypr-agent started", "server", *server, "pending", buf.Len()) loop: for { select { case <-ctx.Done(): break loop case err := <-errc: if err != nil { stop() return err } case f := <-focus: sess.Focus(f.At, cleanTitle(f.Focus)) case c := <-control: switch c.Command { case "idle": sess.Pause(c.At, "idle") case "lock": sess.Pause(c.At, "locked") case "resume", "unlock": sess.Resume(c.At) } } } sess.Close(time.Now()) flushCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := up.Flush(flushCtx); err != nil { log.Warn("final upload failed; events kept for next run", "err", err, "pending", buf.Len()) } return nil } func defaultBufferPath() string { dir := os.Getenv("XDG_STATE_HOME") if dir == "" { home, _ := os.UserHomeDir() dir = filepath.Join(home, ".local", "state") } return filepath.Join(dir, "hypr-agent", "buffer.jsonl") } func envBool(key string, def bool) bool { if v, err := strconv.ParseBool(os.Getenv(key)); err == nil { return v } return def }