feat(agent): event-driven uplink — sync on every state transition

The agent reported its state only on the adaptive sync tick (3s watching /
10s idle), so a resolving→downloading→verifying→organizing→completed
transition could lag up to a full interval before the server (and the web
UI) saw it. Now every successful Task.Transition fires an onChange hook
wired to TriggerSync, pushing the new state immediately. Bursts are safe:
TriggerSync is a buffered-1 send, so clustered transitions coalesce into
one sync.

- Task gains an onChange hook fired AFTER the status mutex is released
  (so a future heavier hook can't deadlock on task.mu); nil is a no-op.
- Manager.OnStateChange is set on each task at Submit; the daemon wires it
  to TriggerSync alongside the existing OnTaskDone.
- Stream tasks transition outside the Manager, so handleStreamTask wires
  the same hook explicitly (gap found in review) — resolving/downloading/
  completed/failed on the stream path now push too.

The adaptive ticker stays as a reconciliation heartbeat; it's just no
longer the latency floor for state changes.
This commit is contained in:
Deivid Soto 2026-06-01 19:09:44 +02:00
parent 1052529ca2
commit 864b6ea832
5 changed files with 83 additions and 4 deletions

View file

@ -216,3 +216,41 @@ func TestHasUntried(t *testing.T) {
t.Error("all methods tried")
}
}
func TestTransitionFiresOnChange(t *testing.T) {
task := NewTaskFromAgent(agent.Task{ID: "t1"}) // StatusClaimed
var fired int
task.SetOnChange(func() { fired++ })
// Valid transition fires the hook.
if err := task.Transition(StatusResolving); err != nil {
t.Fatalf("Transition: %v", err)
}
if fired != 1 {
t.Errorf("onChange fired %d times, want 1 after a valid transition", fired)
}
// Another valid transition fires again (event-driven, every transition).
if err := task.Transition(StatusDownloading); err != nil {
t.Fatalf("Transition: %v", err)
}
if fired != 2 {
t.Errorf("onChange fired %d times, want 2", fired)
}
// Invalid transition must NOT fire the hook.
if err := task.Transition(StatusClaimed); err == nil {
t.Error("expected error on invalid transition downloading→claimed")
}
if fired != 2 {
t.Errorf("onChange fired %d times, want still 2 (no fire on invalid transition)", fired)
}
}
func TestTransitionNilOnChangeNoPanic(t *testing.T) {
task := NewTaskFromAgent(agent.Task{ID: "t2"}) // no onChange set
if err := task.Transition(StatusResolving); err != nil {
t.Fatalf("Transition with nil onChange must not error: %v", err)
}
}