Add activity tracker server, Hyprland agent and Gitea image publishing
Test and publish / test (push) Successful in 2m38s
Test and publish / publish (push) Failing after 2m30s

The Go API on PostgreSQL ingests desktop, browser, presence, manual and
Health Auto Export events and serves activities and daily goal summaries.
The Hyprland agent records window focus, idle and lock intervals.

The CI workflow publishes adam-french.co.uk/adamf/activity-tracker to the
Gitea registry and rolls the web_server stack onto it. The image probes
itself through a new `tracker healthcheck` command, because distroless
has no wget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-14 20:38:12 +01:00
co-authored by Claude Opus 5.5
parent 0cbbb76dee
commit f4ce04875c
29 changed files with 2810 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
// 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
}