Fix drift sync: per-window verdicts, forgiving class match, event-driven status bar
Three independent defects made the focus state feel flaky and let the OS
status bar disagree with the web UI:
- Status file lagged the web by up to a minute: it rendered only on a 60s
ticker with no hook into state changes. notify() now fans out to multiple
listeners (AddOnChange) and the writer has a coalesced Wake() so drift
reaches the bar as promptly as the browser.
- A brief off-task visit could latch drift for the whole session. Sibling
windows of one app (a browser's reading tab vs its chat tab) share a window
class, but the judge verdict was cached by class alone, so one tab's verdict
poisoned the rest and never re-judged. Cache is now keyed by class + scrubbed
title (judgedWindows) so siblings are judged independently.
- Allowed-class matching was exact equality, so a short token ("brave") never
matched the real WM_CLASS ("Brave-browser") and every window was routed to
the LLM. Matching is now substring-based, and planning surfaces the live
window class with a click-to-add chip so users pick a token that matches.
Also fix the planning checkbox alignment: the global full-width input rule was
stretching the "Enforce focus" checkbox; scope it out for checkboxes.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -76,6 +76,7 @@ type Writer struct {
|
||||
path string
|
||||
state func() session.State
|
||||
now func() time.Time
|
||||
wake chan struct{}
|
||||
|
||||
last string
|
||||
wrote bool
|
||||
@@ -83,12 +84,26 @@ type Writer struct {
|
||||
|
||||
// NewWriter builds a Writer for path, reading state via the given accessor.
|
||||
func NewWriter(path string, state func() session.State) *Writer {
|
||||
return &Writer{path: path, state: state, now: time.Now}
|
||||
return &Writer{path: path, state: state, now: time.Now, wake: make(chan struct{}, 1)}
|
||||
}
|
||||
|
||||
// Run writes the status file immediately, then on every tick when the rendered
|
||||
// line has changed. It removes the file on ctx cancellation so a stale status
|
||||
// does not linger after shutdown.
|
||||
// Wake asks the writer to re-render now rather than at the next tick. It is the
|
||||
// hook the controller's change notifications fire through, so drift transitions
|
||||
// reach the status bar promptly instead of lagging the web UI by up to a tick.
|
||||
// The signal is coalesced (buffered, size 1): a burst of changes collapses into
|
||||
// a single re-render, and the actual write still happens on the Run goroutine,
|
||||
// so concurrent callers never race on the file.
|
||||
func (w *Writer) Wake() {
|
||||
select {
|
||||
case w.wake <- struct{}{}:
|
||||
default: // a re-render is already pending
|
||||
}
|
||||
}
|
||||
|
||||
// Run writes the status file immediately, then re-renders on each wake or tick
|
||||
// when the rendered line has changed. The tick still advances the minute
|
||||
// countdown when nothing else changes. It removes the file on ctx cancellation
|
||||
// so a stale status does not linger after shutdown.
|
||||
func (w *Writer) Run(ctx context.Context) {
|
||||
t := time.NewTicker(interval)
|
||||
defer t.Stop()
|
||||
@@ -98,6 +113,8 @@ func (w *Writer) Run(ctx context.Context) {
|
||||
case <-ctx.Done():
|
||||
_ = os.Remove(w.path)
|
||||
return
|
||||
case <-w.wake:
|
||||
w.write()
|
||||
case <-t.C:
|
||||
w.write()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -41,7 +42,7 @@ func TestRender(t *testing.T) {
|
||||
{"active nudge", activeState(in24m, "ontask", "wandered off"), "● 24m ·?"},
|
||||
{"drift outranks nudge", activeState(in24m, "drifting", "wandered off"), "⚠ DRIFT 24m"},
|
||||
{"active no deadline", session.State{RuntimeState: domain.RuntimeActive}, "● 0m"},
|
||||
{"active past deadline", activeState(now.Add(-5 * time.Minute).Unix(), "ontask", ""), "● 0m"},
|
||||
{"active past deadline", activeState(now.Add(-5*time.Minute).Unix(), "ontask", ""), "● 0m"},
|
||||
{"active partial minute rounds up", activeState(now.Add(10*time.Second).Unix(), "ontask", ""), "● 1m"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -101,6 +102,38 @@ func TestWriterRemovesFileOnCancel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Wake makes the writer re-render on a state change instead of waiting for the
|
||||
// coarse minute tick, so the status bar tracks drift transitions promptly
|
||||
// instead of lagging the web UI by up to a minute.
|
||||
func TestWriterWakeRendersPromptly(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "status")
|
||||
now := time.Unix(1_000_000, 0)
|
||||
in10m := now.Add(10 * time.Minute).Unix()
|
||||
|
||||
var mu sync.Mutex
|
||||
st := activeState(in10m, "ontask", "")
|
||||
w := NewWriter(path, func() session.State { mu.Lock(); defer mu.Unlock(); return st })
|
||||
w.now = func() time.Time { return now }
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go w.Run(ctx)
|
||||
|
||||
waitFor(t, func() bool { return fileEquals(path, "● 10m") })
|
||||
|
||||
mu.Lock()
|
||||
st = activeState(in10m, "drifting", "")
|
||||
mu.Unlock()
|
||||
w.Wake()
|
||||
|
||||
waitFor(t, func() bool { return fileEquals(path, "⚠ DRIFT 10m") })
|
||||
}
|
||||
|
||||
func fileEquals(path, want string) bool {
|
||||
b, err := os.ReadFile(path)
|
||||
return err == nil && string(b) == want
|
||||
}
|
||||
|
||||
func readFile(t *testing.T, path string) string {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(path)
|
||||
|
||||
Reference in New Issue
Block a user