package acp import ( "context" "slices" "strings" "time" "albedo/cli/internal/daemon" "albedo/cli/internal/daemon/protocol" sdk "github.com/coder/acp-go-sdk" ) type resourceMask uint8 const ( titleResource resourceMask = 1 << iota commandsResource planResource allResources = titleResource | commandsResource | planResource ) // One worker per attachment reads the latest resource snapshots in sequence. // Both the dirty mask and the last emitted values are protected by session.mu. type sessionResources struct { title string commands []sdk.AvailableCommand plan []sdk.PlanEntry pending resourceMask sent resourceMask reading bool } func (s *session) invalidateResources(event daemon.ResourceInvalidation) { base := "/sessions/" + s.id switch { case event.Session && event.URL == base: s.refreshResources(titleResource) case event.Catalog && event.URL == base+"/catalog": s.refreshResources(commandsResource) case event.Extension && event.URL == base+"/acp-plan": s.refreshResources(planResource) } } // refreshResources requires session.mu, or exclusive ownership during setup. func (s *session) refreshResources(dirty resourceMask) { s.resources.pending |= dirty if s.client == nil || s.resources.reading || s.resources.pending == 0 || s.observation == buffering || s.observation == lost { return } s.resources.reading = true go s.readResources() } func (s *session) readResources() { for { s.mu.Lock() pending := s.resources.pending if pending == 0 || s.observation == buffering || s.observation == lost || s.ctx.Err() != nil { s.resources.reading = false s.mu.Unlock() return } s.resources.pending = 0 s.mu.Unlock() // Clear the dirty mask before I/O so invalidations received during a // read cause another pass. There is no second read that can overtake it. for _, resource := range []resourceMask{titleResource, commandsResource, planResource} { if pending&resource != 0 { s.readResource(resource) } } } } func (s *session) readResource(resource resourceMask) { ctx, cancel := context.WithTimeout(s.ctx, 10*time.Second) defer cancel() var apply func() var err error switch resource { case titleResource: var info daemon.Session info, err = s.client.SessionInfo(ctx) apply = func() { s.publishTitle(info.Title) } case commandsResource: var commands []daemon.SessionCommand commands, err = s.client.SessionCommands(ctx) apply = func() { s.advertiseCommands(commands) } case planResource: var plan protocol.ACPPlan plan, err = s.client.ACPPlan(ctx) apply = func() { s.publishPlan(plan) } } s.mu.Lock() defer s.mu.Unlock() switch { case s.observation == lost || s.ctx.Err() != nil: return case s.observation == buffering: s.resources.pending |= resource case err != nil: s.adapter.note("session %s: could not refresh %s: %v", s.id, resource.name(), err) // A failed first catalog read still leaves the local command usable. // Keep the last known skill commands on subsequent failures. if resource == commandsResource && s.resources.sent&commandsResource == 0 { s.advertiseCommands(nil) } default: apply() } } func (resource resourceMask) name() string { switch resource { case commandsResource: return "skill commands" case planResource: return "checklist" default: return "title" } } func (s *session) publishTitle(title string) { if s.resources.sent&titleResource != 0 && s.resources.title == title { return } s.resources.title = title s.resources.sent |= titleResource update := sdk.SessionSessionInfoUpdate{SessionUpdate: string(updateSessionInfo), Title: sdk.PatchNull[string]()} if title != "" { update.Title = sdk.PatchSet(title) } s.update(update) } func (s *session) advertiseCommands(skills []daemon.SessionCommand) { commands := []sdk.AvailableCommand{{Name: compactCommand, Description: "Compact this session's context and keep a summary"}} for _, skill := range skills { if skill.SkillName != "" { commands = append(commands, sdk.AvailableCommand{ Name: strings.TrimPrefix(skill.Name, "/"), Description: skill.Description, Input: &sdk.AvailableCommandInput{Unstructured: &sdk.UnstructuredCommandInput{Hint: "instructions"}}, }) } } // Discovery and loaded catalogs can present the same skills in different // orders. Keep the local command first and sort the skill commands. slices.SortFunc(commands[1:], func(a, b sdk.AvailableCommand) int { return strings.Compare(a.Name, b.Name) }) if slices.EqualFunc(s.resources.commands, commands, func(a, b sdk.AvailableCommand) bool { // Input is fixed for each non-compact command. return a.Name == b.Name && a.Description == b.Description }) { return } s.resources.commands = commands s.resources.sent |= commandsResource s.update(sdk.SessionAvailableCommandsUpdate{SessionUpdate: string(updateCommands), AvailableCommands: commands}) } func (s *session) publishPlan(plan protocol.ACPPlan) { entries := make([]sdk.PlanEntry, 0, len(plan.Items)) for _, item := range plan.Items { status := sdk.PlanEntryStatusPending if item.Done { status = sdk.PlanEntryStatusCompleted } entries = append(entries, sdk.PlanEntry{Content: item.Text, Priority: sdk.PlanEntryPriorityMedium, Status: status}) } if s.resources.sent&planResource != 0 && slices.EqualFunc(s.resources.plan, entries, func(a, b sdk.PlanEntry) bool { return a.Content == b.Content && a.Status == b.Status && a.Priority == b.Priority }) { return } s.resources.plan = entries s.resources.sent |= planResource if s.planUpdates { s.update(sdk.SessionPlanUpdate{SessionUpdate: "plan_update", Plan: sdk.PlanUpdateContent{Items: &sdk.PlanUpdateContentItems{PlanId: "checklist", Type: "items", Entries: entries}}}) return } s.update(sdk.SessionUpdatePlan{SessionUpdate: string(updatePlan), Entries: entries}) }