Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980package acp
import ( "context" "encoding/json" "io" "testing" "time"
"albedo/cli/internal/daemon" "albedo/cli/internal/daemon/protocol")
// renewBackend attaches with a short advertised lease and records renewals.type renewBackend struct { *lifecycleBackend renewed chan time.Time lease protocol.ACPLease}
func (b *renewBackend) Resume(context.Context, string, string, []protocol.ACPMCPServer) (protocol.ACPAttachment, error) { return protocol.ACPAttachment{SessionID: lifecycleSession, Lease: b.lease}, nil}
func (b *renewBackend) Renew(context.Context, string) error { b.renewed <- time.Now() return nil}
// The daemon's lease lives only as long as the adapter renews it, and the// daemon tells the adapter how often to renew in the attach response. The// lease itself is checked against an injected clock in// apps/albedo/test/albedo/acp/acp_attachments_test.gleam; this checks the// other half: the adapter renews at the advertised heartbeat, not at a// constant of its own. With a 40 ms heartbeat and a lease far longer than the// test, an adapter that ignored the advertisement (its fallback is 5 s) would// not renew at all here, and one that renewed in a busy loop would renew far// faster than the heartbeat.func TestTheAdapterRenewsAtTheAdvertisedHeartbeat(t *testing.T) { const heartbeat = 40 * time.Millisecond ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) lifecycle := &lifecycleBackend{ fakeBackend: &fakeBackend{written: make(chan []byte, 64)}, releases: make(chan string, 8), deletes: make(chan string, 8), } backend := &renewBackend{ lifecycleBackend: lifecycle, lease: protocol.ACPLease{HeartbeatMs: heartbeat.Milliseconds(), LeaseMs: 600_000}, renewed: make(chan time.Time, 64), } a := &adapter{initialized: true, ctx: ctx, cancel: cancel, backend: backend, sessions: map[string]*session{}, diagnostics: io.Discard, writer: newWriter(capturedWriter{lifecycle.written})} a.watch = func(ctx context.Context, _ func(daemon.StreamEvent) error, _ func()) error { <-ctx.Done() return ctx.Err() } go a.writer.run(ctx) attached := time.Now() a.handle(request{ID: json.RawMessage(`1`), Method: methodResume, Params: lifecycleParams(lifecycleSession)}, "test") if response := readResponse(t, lifecycle.written, 1); response["error"] != nil { t.Fatalf("resume failed: %v", response["error"]) } for renewal := range 3 { select { case at := <-backend.renewed: // The ticker keeps its schedule from the attach, so a late tick is // followed by an early one: measure each renewal from the attach, // not from the previous renewal. A busy loop still renews long // before its slot. due := time.Duration(renewal+1) * heartbeat if elapsed := at.Sub(attached); elapsed < due-heartbeat/4 { t.Fatalf("renewal %d came %v after the attach; with a %v heartbeat it is due at %v", renewal+1, elapsed, heartbeat, due) } case <-time.After(2 * time.Second): t.Fatalf("renewal %d never came; the advertised heartbeat is %v", renewal+1, heartbeat) } }}