Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions internal/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@ func (s *Supervisor) startSide(ctx context.Context, reason prompt.Reason, facts
if s.fast == nil || s.side != nil || s.paused() {
return false
}
s.renewToken(ctx)
rctx, rcancel := context.WithTimeout(ctx, 20*time.Second)
rules, hold, rerr := s.core.StandingNotes(rctx, s.cfg.Name)
rcancel()
Expand Down Expand Up @@ -1936,6 +1937,28 @@ func (s *Supervisor) interject(ctx context.Context, inj Interjector) {
}

// turn runs one wake.
// tokenRenewer is a channel to the core whose access token ends when a wake is
// reported (triggers.Link: the core suspends the actor then).
type tokenRenewer interface {
RenewAfterReport(context.Context) error
}

// renewToken takes a new access token before a turn's first request to the
// core, when the last one ended with the wake that was reported. Without it a
// turn that follows another at once reads no standing notes: the core refuses
// the request, and only a later tool call says hello again.
func (s *Supervisor) renewToken(ctx context.Context) {
r, ok := s.trig.(tokenRenewer)
if !ok {
return
}
rctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
if err := r.RenewAfterReport(rctx); err != nil && ctx.Err() == nil {
s.log.Warn("no new access token before the turn; its first requests to the core may be refused", "err", err)
}
}

func (s *Supervisor) turn(ctx context.Context, p *pending) {
sess := s.led.LoadSession()
interrupted := sess.Running
Expand All @@ -1953,6 +1976,7 @@ func (s *Supervisor) turn(ctx context.Context, p *pending) {
s.status.HeldEdges = 0
}
used := s.led.Usage().Tokens
s.renewToken(ctx)
s.refreshIdentity(ctx, false, identityEvery)
rctx, rcancel := context.WithTimeout(ctx, 20*time.Second)
rules, hold, rerr := s.core.StandingNotes(rctx, s.cfg.Name)
Expand Down
25 changes: 25 additions & 0 deletions internal/triggers/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Link struct {
// running and waits for a wake report to mark it suspended.
owed bool
bearer string
reported bool // a wake report reached the core since the token was taken: the core ends the token
prevBearer string // the token before the last hello: the core may still be sending it
tokenExp time.Time
actor string
Expand Down Expand Up @@ -148,6 +149,21 @@ func (l *Link) Refresh(ctx context.Context) error {
return l.connect(ctx)
}

// RenewAfterReport says hello again when a wake was reported to the core since
// the access token was taken, and does nothing otherwise. The supervisor calls
// it before the first request of a turn: after a wake report the core ends the
// token, and a turn that follows at once, in the same run, would have its
// first requests to the core refused.
func (l *Link) RenewAfterReport(ctx context.Context) error {
l.mu.Lock()
reported := l.reported
l.mu.Unlock()
if !reported {
return nil
}
return l.Refresh(ctx)
}

// WaitReady blocks until the first hello is done: the spot has its access token,
// the link is connected and the schedule is with the core.
func (l *Link) WaitReady(ctx context.Context) error {
Expand Down Expand Up @@ -268,6 +284,14 @@ func (l *Link) Idle() {
sent++
}
wakes = wakes[sent:]
if sent > 0 {
// The core suspends an actor a moment after its wake report, and the
// access token ends there. A turn that starts before the suspend has
// happened would use a token the core no longer takes.
l.mu.Lock()
l.reported = true
l.mu.Unlock()
}
}
if len(turns) == 0 && st == nil && len(wakes) == 0 {
return
Expand Down Expand Up @@ -430,6 +454,7 @@ func (l *Link) takeToken(t *tokenFrame) {
l.prevBearer = l.bearer
}
l.bearer, l.tokenExp = t.Value, exp
l.reported = false
l.mu.Unlock()
}

Expand Down
34 changes: 34 additions & 0 deletions internal/triggers/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,37 @@ func TestLinkIdentityToken(t *testing.T) {
t.Error("no new hello was asked for")
}
}

// After a wake report the core ends the actor's token. A turn that follows in
// the same run takes a new one first, and a turn that follows none does not.
func TestLinkRenewsItsTokenAfterAWakeReport(t *testing.T) {
c := newLinkCore(t)
l, _, _ := upLink(t, c)
hellos := func() int {
c.lmu.Lock()
defer c.lmu.Unlock()
return len(c.hellos)
}
first := hellos()
if err := l.RenewAfterReport(context.Background()); err != nil {
t.Fatal(err)
}
if hellos() != first {
t.Fatal("a hello with no wake reported")
}
l.ReportWake(Wake{TS: time.Now().UnixMilli(), Reason: "tick", Lane: "long", Outcome: "turn"})
l.Idle()
if err := l.RenewAfterReport(context.Background()); err != nil {
t.Fatal(err)
}
if hellos() != first+1 {
t.Fatalf("hellos after a reported wake: %d, want %d", hellos(), first+1)
}
// the new token stands until the next report
if err := l.RenewAfterReport(context.Background()); err != nil {
t.Fatal(err)
}
if hellos() != first+1 {
t.Fatalf("a second hello with nothing reported since: %d", hellos())
}
}