Switch to prefixed IDs and tell agents which Mac a message came from

This commit is contained in:
Ian Gulliver
2026-10-20 23:15:19 -07:00
parent f1296ba13d
commit 59bf17189d
42 changed files with 770 additions and 145 deletions
+17 -7
View File
@@ -2,7 +2,6 @@ package clients
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
@@ -16,6 +15,7 @@ import (
"github.com/coder/websocket"
"g.fc.run/ai/byre/internal/api"
"g.fc.run/ai/byre/internal/ids"
)
const (
@@ -181,6 +181,16 @@ func (c *conn) ping(ctx context.Context) {
}
}
func (r *Registry) Host(id string) string {
r.mu.Lock()
defer r.mu.Unlock()
e := r.clients[id]
if e == nil {
return ""
}
return e.Host
}
func (r *Registry) List() []Client {
r.mu.Lock()
defer r.mu.Unlock()
@@ -197,7 +207,7 @@ func (r *Registry) List() []Client {
func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest) (api.ClientReply, error) {
r.mu.Lock()
matches := []*conn{}
ids := []string{}
matched := []string{}
connected := []string{}
for _, e := range r.clients {
if e.conn == nil {
@@ -206,7 +216,7 @@ func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest)
connected = append(connected, fmt.Sprintf("%s (%s)", e.Host, e.ID))
if e.ID == name || e.Host == name {
matches = append(matches, e.conn)
ids = append(ids, e.ID)
matched = append(matched, e.ID)
}
}
r.mu.Unlock()
@@ -215,12 +225,12 @@ func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest)
return api.ClientReply{}, fmt.Errorf("no client named %q is connected; connected: %s", name, strings.Join(connected, ", "))
}
if len(matches) > 1 {
slices.Sort(ids)
return api.ClientReply{}, fmt.Errorf("%d clients on %s are connected; name one by its id: %s", len(matches), name, strings.Join(ids, ", "))
slices.Sort(matched)
return api.ClientReply{}, fmt.Errorf("%d clients on %s are connected; name one by its id: %s", len(matches), name, strings.Join(matched, ", "))
}
c := matches[0]
req.Id = rand.Text()
req.Id = ids.New(ids.ClientRequest)
ch := make(chan api.ClientReply, 1)
c.mu.Lock()
c.pending[req.Id] = ch
@@ -254,7 +264,7 @@ func (r *Registry) Call(ctx context.Context, name string, req api.ClientRequest)
}
func (r *Registry) AddTransfer(t Transfer) string {
id := rand.Text()
id := ids.New(ids.Transfer)
r.mu.Lock()
defer r.mu.Unlock()
r.transfers[id] = t