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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589package acp
import ( "context" "encoding/json" "errors" "fmt" "slices" "strconv" "strings" "sync" "time"
"albedo/cli/internal/app" "albedo/cli/internal/daemon" "albedo/cli/internal/daemon/protocol"
sdk "github.com/coder/acp-go-sdk")
// streamFunc subscribes to one session's events and calls subscribed once the// response is accepted.type streamFunc func(context.Context, func(daemon.StreamEvent) error, func()) error
type session struct { sessionState reconciler ctx context.Context bufferFailure error watch streamFunc client *daemon.ChatClient replayCancel context.CancelFunc ready <-chan struct{} closeWait chan struct{} // signalled after the outstanding prompt response is written adapter *adapter stop context.CancelFunc acceptedKeys map[string]string seenMessages map[string]bool held []*activity id string cwd string settings app.ACPSettings cursor protocol.Cursor buffered []daemon.StreamEvent servers []protocol.ACPMCPServer resources sessionResources usage usageState lease protocol.ACPLease keyHistoryCutoff int64 // observedHistory is the newest transcript position this session has // seen; a reattach must report the same cutoff. observedHistory int64 historyCutoff int64 bufferedBytes int mu sync.Mutex // compactionEnabled is whether the editor advertised compaction updates. compactionEnabled bool planUpdates bool v2 bool replayV2 bool}
func newSession(a *adapter, attachment protocol.ACPAttachment) *session { ctx, cancel := context.WithCancel(a.ctx) current := &session{ adapter: a, ctx: ctx, stop: cancel, id: attachment.SessionID, cursor: attachment.Cursor, client: a.backend.Observer(attachment.SessionID, attachment.Cursor), lease: attachment.Lease, historyCutoff: attachment.HistoryCutoff, observedHistory: attachment.HistoryCutoff, reconciler: newReconciler(), watch: a.watch, } if current.watch == nil { current.watch = current.client.StreamSubscribed } current.inherited = slices.Clone(attachment.PendingInputIDs) if attachment.ActiveTurn != nil { current.turn = attachment.ActiveTurn.TurnID if attachment.ActiveTurn.AcpInputID != nil { current.inherited = append(current.inherited, *attachment.ActiveTurn.AcpInputID) } } for _, boundary := range attachment.OutputBoundaries { if len(boundary.LiveID) > maxReconciliationIDBytes { current.fail("Output identity exceeds reconciliation limit") break } current.boundaries[boundary.LiveID] = boundary.UTF8Bytes current.limitReconciliation() if current.observation == lost { break } } current.limitReconciliation() return current}
func (s *session) start() { ready := make(chan struct{}) s.ready = ready s.refreshResources(allResources) subscribed := sync.OnceFunc(func() { close(ready) }) go func() { err := s.watch(s.ctx, func(event daemon.StreamEvent) error { s.mu.Lock() defer s.mu.Unlock() return s.consumeLocked(event) }, subscribed) if s.ctx.Err() == nil { s.mu.Lock() streamErr, classified := errors.AsType[*daemon.StreamError](err) if err == nil || classified && streamErr.Kind == daemon.StreamTransient { s.disconnect("The session stream disconnected") } else { s.fail(fmt.Sprintf("Session stream ended: %v", err)) } s.mu.Unlock() } // A load waits for this stream; a stream that failed before it was // accepted must answer that wait with the failure instead of nothing. subscribed() }()
go func() { interval := time.Duration(s.lease.HeartbeatMs) * time.Millisecond if interval <= 0 { interval = 5 * time.Second } lease := time.Duration(s.lease.LeaseMs) * time.Millisecond renewed := time.Now() timer := time.NewTicker(interval) defer timer.Stop() for { select { case <-s.ctx.Done(): return case <-timer.C: s.mu.Lock() done := s.observation == lost || s.ctx.Err() != nil s.mu.Unlock() if done { return } err := s.adapter.backend.Renew(s.ctx, s.id) if err == nil { renewed = time.Now() continue } api, refused := errors.AsType[*daemon.APIError](err) if (refused && api.Code == "attachment_lost") || time.Since(renewed) >= lease { s.mu.Lock() s.disconnect("The attachment was lost") s.mu.Unlock() return } } } }()}
// A fresh capture can replace an idle observation only if the durable history// still matches. Stop the old observer and heartbeat while the editor is away.func (s *session) disconnect(message string) { if s.ctx.Err() != nil { return } if s.observation == observing && s.active == nil && s.queued == nil && !s.blocked() && len(s.live) == 0 && len(s.boundaries) == 0 { s.observation = disconnected s.stop() s.adapter.note("session %s: %s while idle; the next prompt reattaches", s.id, message) return } s.fail(message + "; reload the session history")}
func (s *session) prompt(message request, blocks []json.RawMessage) bool { s.mu.Lock() defer s.mu.Unlock() if s.observation == disconnected { return false } if s.v2 { s.v2Prompt(message, blocks) return true } if isCompactPrompt(blocks) { effects, refusal := s.acceptCompact(message) s.runOrRefuse(message, effects, refusal) return true } effects, refusal := s.acceptPrompt(message, blocks) s.runOrRefuse(message, effects, refusal) return true}
func (s *session) runOrRefuse(message request, effects []effect, refusal *rpcError) { if refusal != nil { s.adapter.respond(message.ID, nil, refusal) return } s.runEffects(effects)}
func (s *session) admit(a *activity) { if a.key != "" && s.keyHistoryCutoff != 0 { go s.restoreInputKeys(a, s.keyHistoryCutoff) return } s.prepareInput(a)}
func (s *session) prepareInput(a *activity) { if id, ok := s.acceptedKeys[a.key]; a.key != "" && ok { s.finishPending(a, map[string]string{"messageId": id}, nil) return } handle, err := s.adapter.backend.Prepare(s.id, a.blocks, a.key) if err != nil { s.runEffects(s.submitted(a, err)) return } a.handle = handle s.runEffects([]effect{submitEffect{activity: a}})}
func (s *session) cancelPrompt() { s.mu.Lock() defer s.mu.Unlock() s.cancelHeld() s.runEffects(s.cancel())}
// cancelForClose keeps the attachment alive until the outstanding prompt has// answered. Pending admission and daemon interrupts use the same state// transitions as a session/cancel notification.func (s *session) cancelForClose(ctx context.Context) error { s.mu.Lock() s.cancelHeld() s.runEffects(s.cancel()) if s.active == nil { s.mu.Unlock() return nil } if s.closeWait == nil { s.closeWait = make(chan struct{}) } done := s.closeWait s.mu.Unlock() select { case <-done: return nil case <-s.ctx.Done(): return s.ctx.Err() case <-ctx.Done(): return ctx.Err() }}
func (s *session) cancelInput(id string, a *activity) { go func() { receipt, err := s.adapter.backend.Cancel(s.ctx, s.id, id) s.mu.Lock() defer s.mu.Unlock() s.runEffects(s.cancelled(id, a, err, receipt)) }()}
func removeID(ids []string, id string) []string { return slices.DeleteFunc(ids, func(value string) bool { return value == id })}
func (s *session) fail(message string) { s.runEffects(s.markLost("observation_uncertain", message))}
func (s *session) compact(a *activity) { ctx, cancel := context.WithTimeout(s.ctx, 195*time.Second) defer cancel() result, err := s.adapter.backend.Compact(ctx, s.id) s.mu.Lock() defer s.mu.Unlock() enabled := s.compactionEnabled s.runEffects(s.completeCompact(a, result, err, s.id, enabled))}
func (s *session) usageLocked(info *daemon.Usage) { generation, fetch := s.usage.add(info) if s.usage.window.phase == windowKnown { s.emitUsage(s.usage.take()) } if fetch { s.runEffects([]effect{fetchWindowEffect{generation: generation}}) }}
func (s *session) fetchContextWindow(generation int) { ctx, cancel := context.WithTimeout(s.ctx, 10*time.Second) defer cancel() window, err := s.client.ContextWindow(ctx) s.mu.Lock() defer s.mu.Unlock() if s.ctx.Err() != nil { return } var tokens *int if err == nil { tokens = window } s.emitUsage(s.usage.resolve(tokens, generation))}
func (s *session) emitUsage(pending []*daemon.Usage) { for _, info := range pending { s.update(usageOf(info, s.usage.window.tokens)) }}
func usageOf(info *daemon.Usage, window int) sdk.SessionUsageUpdate { update := sdk.SessionUsageUpdate{SessionUpdate: string(updateUsage), Size: int64(window), Used: int64(usedTokens(info))} if window > 0 && update.Used > update.Size { update.Used = update.Size } if info.Spent != nil && info.Spent.Usd >= 0 { currency := currencyUSD if info.Spent.Basis == daemon.BasisCounted { currency = currencyCredits } update.Cost = &sdk.Cost{Amount: info.Spent.Usd, Currency: currency} } return update}
func usedTokens(info *daemon.Usage) int { switch { case info.PromptTokens != nil && *info.PromptTokens >= 0: return *info.PromptTokens case info.TotalTokens != nil && *info.TotalTokens >= 0: return *info.TotalTokens case info.Size != nil && info.Size.Tokens >= 0: return info.Size.Tokens } return 0}
func (s *session) compacted(event daemon.StreamEvent) { if !s.compactionEnabled || !event.Replayed && event.Sequence == 0 { return } update := sdk.SessionCompactionUpdate{SessionUpdate: string(updateCompaction), CompactionId: sdk.CompactionId(s.compactionID(event)), Status: sdk.CompactionStatusCompleted} if summary := strings.TrimSpace(event.Summary); summary != "" { const maxSummary = 128 * 1024 if len(summary) > maxSummary { summary = boundedText(summary, maxSummary) + "\n[albedo: compaction summary truncated]" } update.Summary = sdk.PatchSet([]sdk.ContentBlock{sdk.TextBlock(summary)}) } s.update(update)}
func (s *session) runEffects(effects []effect) { for _, item := range effects { item.apply(s) } if s.observation == lost && len(s.held) > 0 { s.rejectHeld(s.failure) }}
func (s *session) compactionID(event daemon.StreamEvent) string { if event.Replayed { if event.EntryID != "" { return "acp-compaction-entry-" + event.EntryID } return "acp-compaction-position-" + strconv.FormatInt(event.Position, 10) } id := "acp-compaction-" if s.cursor.Generation != "" { id += s.cursor.Generation + "-" } return id + strconv.FormatInt(event.Sequence, 10)}
func (s *session) finishWithBarrier(item finishEffect) { wire := s.adapter.response(item.request.ID, item.result, item.failure) if s.v2 { a := item.activity if a.acknowledged { reason := "error" if result, ok := item.result.(sdk.PromptResponse); ok { reason = string(result.StopReason) } update := map[string]any{"sessionUpdate": "state_update", "state": "idle", "stopReason": reason} if reason == "error" { update["error"] = map[string]any{"code": -32603, "message": "The turn failed"} } wire = map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{"sessionId": s.id, "update": update}} } else { failure := item.failure if failure == nil { failure = problem(-32800, "cancelled", "Prompt cancelled before insertion") } wire = s.adapter.response(item.request.ID, nil, failure) for _, id := range a.retries { s.adapter.respond(id, nil, failure) } } } s.writeFinal(item.activity, wire)}
func (s *session) writeFinal(a *activity, wire map[string]any) { barrier, err := s.adapter.writer.enqueue(wire, true) if err != nil { s.adapter.cancel() return } go func() { select { case <-barrier: case <-s.ctx.Done(): return case <-s.adapter.writer.done: return } s.mu.Lock() defer s.mu.Unlock() s.runEffects(s.responseWritten(a)) s.nextHeld() if s.closeWait != nil { close(s.closeWait) s.closeWait = nil } }()}
func (s *session) submit(a *activity) { go func() { select { case <-s.ready: case <-s.ctx.Done(): return } s.mu.Lock() eligible := s.observation != lost && s.active == a && a.phase == activityPending && s.ctx.Err() == nil s.mu.Unlock() if !eligible { return } err := s.adapter.backend.Submit(s.ctx, a.handle) s.mu.Lock() defer s.mu.Unlock() s.runEffects(s.submitted(a, err)) }()}
func (s *session) observe(event daemon.StreamEvent) { if s.ctx.Err() != nil || s.observation == lost || event.Replayed { return } for _, id := range []string{event.MessageID, event.EntryID, event.ProgressCallID} { if len(id) > maxReconciliationIDBytes { s.fail("Output identity exceeds reconciliation limit") return } } defer s.limitReconciliation() if event.Type == daemon.EventReset { if s.reconcileReset(event.Snapshot) { s.refreshResources(allResources) return } s.fail("The session stream reset; resume required") return } if event.Sequence > 0 { s.cursor.Sequence = event.Sequence } if event.SnapshotOrigin { return } s.observedHistory = max(s.observedHistory, event.Position, event.Seq) if event.Type == daemon.EventInvalidate && event.Invalidation != nil { s.invalidateResources(*event.Invalidation) return } if event.Type == daemon.EventUsage { s.usageLocked(event.Usage) return } if event.Type == daemon.EventCompacted { s.compacted(event) return } if s.active != nil && s.active.phase == activityFinishing { return } if s.active != nil && s.active.handle != nil && event.OperationID == s.active.handle.ID() && event.Receipt != nil && event.Receipt.Admission == admissionAccepted { s.runEffects(s.admitted(s.active)) } if event.Type == daemon.EventTurnMembership { s.runEffects(s.turnMembership(event.TurnID, event.SubmissionIDs)) } if s.turn == "" || event.TurnID != "" && event.TurnID != s.turn { return } switch event.Type { case daemon.EventText, daemon.EventThinking: if event.EntryID != "" { s.durableMessage(event) return } s.live[event.MessageID] = appendPrefix(s.live[event.MessageID], event.Text) if len(s.withheld) > 0 { s.withheld[event.MessageID] = true return } kind := updateAgentChunk if event.Type == daemon.EventThinking { kind = updateThoughtChunk } s.chunk(kind, event.MessageID, event.Text) case daemon.EventMessage: s.durableMessage(event) case daemon.EventToolProgress: if event.Progress == nil { return } id := event.Progress.CallID switch s.tools[id] { case toolUnseen: s.announceTool(id, event.Progress.Name, event.Progress.Title) case toolAnnounced: s.update(progressedTool(id)) case toolCompleted: } case daemon.EventTool: if event.ProgressCallID == "" || !settled(event.ToolStatus) { s.fail("Missing durable tool outcome") return } s.finishTool(event) case daemon.EventError: reason := event.Code if reason == "" { reason = "turn_failed" } s.failure = problem(-32603, reason, event.Text) case daemon.EventRetry: s.failure = nil case daemon.EventCommitted: for _, id := range event.ReplacesLiveIDs { delete(s.live, id) delete(s.boundaries, id) delete(s.withheld, id) } if event.ReplacesAllLive { clear(s.live) clear(s.boundaries) clear(s.withheld) } case daemon.EventTurnCompleted: if s.v2 && s.active == nil && s.turn == event.TurnID && s.turn != "" { reason := "end_turn" switch event.Outcome { case daemon.OutcomeInterrupted: reason = "cancelled" case daemon.OutcomeFailed: reason = "error" } s.v2State("idle", reason) } s.runEffects(s.turnCompleted(event.TurnID, event.SubmissionIDs, event.Outcome)) s.nextHeld() }}
const admissionAccepted = "accepted"
// subscribeTimeout bounds how long session/load waits for its event stream to// be accepted. A stream that neither subscribes nor fails would otherwise leave// the request unanswered for as long as the editor stays connected.const subscribeTimeout = 30 * time.Second