carlos / platform Public

console: a saved home link clears its start request in the same step

GET /api/cli/link could answer linked together with a pending start.
homeLinkSave now clears the request under ops right after the save
(lock order ops then mu, as everywhere), and the status handler reads
the request before the link and drops it from a linked answer, so a
mid-save read is never "not linked, no request". Astra approved (#931).
#897.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from homelink-linked-clears-start into main.
claude agent, operated by paul@keymail.dev f6c82931914628762cd40b96d41850c5543101e0 parent b239aaf
1 file changed, +23 −6
  • internal/console/homelink.go +23 −6

CI failed — run details

diff --git a/internal/console/homelink.go b/internal/console/homelink.go
index 3d00baf1a..504201e7c 100644
--- a/internal/console/homelink.go
+++ b/internal/console/homelink.go
@@ -353,6 +353,15 @@ func (s *Server) homeLinkSave(seq uint64, base string, ans *linkPollAnswer) stri
log.Printf("console: home link %s: save: %v", ans.LinkID, err)
return copyHomeLinkSaveFailed
}
+ // The request is over the moment the link is saved: clear it here,
+ // under ops as the save, so GET /api/cli/link never answers linked
+ // together with a pending start (#897). The poll's own clear after it
+ // returns then finds nothing. Lock order: ops, then mu, as everywhere.
+ s.homeLink.mu.Lock()
+ if cur := s.homeLink.start; cur != nil && cur.seq == seq {
+ s.homeLink.start = nil
+ }
+ s.homeLink.mu.Unlock()
log.Printf("console: home link %s: linked to %s on %s", ans.LinkID, ans.Account, base)
return ""
}
@@ -414,12 +423,11 @@ func (s *Server) handleAPIHomeLinkStatus(w http.ResponseWriter, r *http.Request)
if !ok {
return
}
- l, err := reg.HomeLink()
- if err != nil {
- log.Printf("console: home link: read: %v", err)
- jsonError(w, http.StatusServiceUnavailable, copyHomeLinkReadFailed)
- return
- }
+ // The request is read before the link. homeLinkSave saves the link,
+ // then clears the request: read in this order, a request found gone
+ // means the link is already saved, so the answer is never "not linked,
+ // no request" in the middle of a save (the CLI's wait would call that
+ // lost), and a linked answer carries no request.
out := homeLinkWire{Apps: []homeLinkAppWire{}}
s.homeLink.mu.Lock()
start := s.homeLink.start
@@ -430,6 +438,15 @@ func (s *Server) handleAPIHomeLinkStatus(w http.ResponseWriter, r *http.Request)
}
}
s.homeLink.mu.Unlock()
+ l, err := reg.HomeLink()
+ if err != nil {
+ log.Printf("console: home link: read: %v", err)
+ jsonError(w, http.StatusServiceUnavailable, copyHomeLinkReadFailed)
+ return
+ }
+ if l != nil {
+ out.Start = nil
+ }
if l == nil {
writeJSON(w, http.StatusOK, out)
return