Files
antidrift/cmd/antidriftd/main.go
T
felixm 097678e839 Mirror runtime status to ~/.antidrift_status
A window-manager status bar can now read a single-line status file: a
glyph plus minutes-remaining (● 24m / ⚠ DRIFT 24m / ● 24m ·? / ◔ planning
/ ✓ review), empty when idle. Rewritten on a one-minute tick, only when
the line changes, and removed on shutdown. Degrades to no file if $HOME
is unresolvable.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 20:49:48 -04:00

90 lines
2.5 KiB
Go

// Command antidriftd is the AntiDrift focus daemon: it serves a local web UI
// and owns the commitment state machine.
package main
import (
"context"
"log"
"os"
"os/exec"
"runtime"
"time"
"antidrift/internal/ai"
"antidrift/internal/evidence"
"antidrift/internal/session"
"antidrift/internal/statusfile"
"antidrift/internal/store"
"antidrift/internal/web"
)
const addr = "localhost:7777"
func main() {
path, err := store.DefaultPath()
if err != nil {
log.Fatalf("resolve snapshot path: %v", err)
}
ctrl, err := session.New(path)
if err != nil {
log.Fatalf("load session: %v", err)
}
srv := web.NewServer(ctrl)
srv.Init() // re-arm or expire a restored Active session
// Wire the AI planning coach. Backend selectable via ANTIDRIFT_AI_BACKEND
// (claude default, or codex). Misconfiguration degrades to no coach rather
// than failing startup — manual planning still works.
if backend, err := ai.NewBackend(os.Getenv("ANTIDRIFT_AI_BACKEND")); err != nil {
log.Printf("ai disabled: %v", err)
} else {
svc := ai.NewService(backend)
ctrl.SetCoach(svc)
ctrl.SetDriftJudge(svc)
ctrl.SetNudge(svc)
log.Printf("ai: %s backend (coach + drift judge + nudge)", backend.Name())
}
// Mirror runtime status to ~/.antidrift_status for a window-manager bar.
// A misconfigured home dir degrades to no status file, not a failed start.
if statusPath, err := statusfile.DefaultPath(); err != nil {
log.Printf("status file disabled: %v", err)
} else {
go statusfile.NewWriter(statusPath, ctrl.State).Run(context.Background())
log.Printf("status: writing %s", statusPath)
}
src := evidence.NewSource()
go src.Watch(context.Background(), ctrl.RecordWindow)
go openBrowser("http://" + addr)
log.Printf("antidriftd listening on http://%s", addr)
if err := srv.Router().Run(addr); err != nil {
log.Fatalf("server: %v", err)
}
}
// openBrowser best-effort launches the default browser after a short delay so
// the server is listening first. Failures are logged, not fatal.
func openBrowser(url string) {
time.Sleep(300 * time.Millisecond)
var cmd string
var args []string
switch runtime.GOOS {
case "linux":
cmd, args = "xdg-open", []string{url}
case "darwin":
cmd, args = "open", []string{url}
case "windows":
cmd, args = "rundll32", []string{"url.dll,FileProtocolHandler", url}
default:
log.Printf("open browser manually: %s", url)
return
}
if err := exec.Command(cmd, args...).Start(); err != nil {
log.Printf("could not open browser (%v); visit %s", err, url)
}
}