Repository navigation
Expand file tree
/
Copy pathbus.go
More file actions
170 lines (159 loc) · 6.09 KB
/
Copy pathbus.go
File metadata and controls
170 lines (159 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
// Party-line is the message bus Claude sessions use to talk to each other.
// It is multi-tenant: a machine enrolls once with its owner's sign-in (the same
// token `work login` holds) and gets its own node token, so a person's laptop
// and box share a bus with each other and with nobody else.
func partyLineURL() string {
if u := os.Getenv("WORK_PARTY_LINE_URL"); u != "" {
return strings.TrimRight(u, "/")
}
return "https://party-line.jumpstartlab.com"
}
// partyLineDir is where the party-line client looks for a machine's token.
func partyLineDir() string {
dir := os.Getenv("XDG_CONFIG_HOME")
if dir == "" {
home, _ := os.UserHomeDir()
dir = filepath.Join(home, ".config")
}
return filepath.Join(dir, "party-line")
}
// What party-line's enrollment error codes mean to the person running this.
var enrollErrors = map[string]string{
"missing_token": "sign in first with `work login <your email>`",
"invalid_token": "your sign-in isn't valid here; run `work login <your email>` again",
"expired": "your sign-in expired; run `work login <your email>` again",
"wrong_audience": "this sign-in isn't for party-line; update work and sign in again",
"wrong_issuer": "this sign-in isn't for party-line; update work and sign in again",
"wrong_client": "this sign-in isn't for party-line; update work and sign in again",
"not_entitled": "your account isn't on party-line yet; ask Jumpstart Lab",
"email_not_verified": "your email isn't verified; ask Jumpstart Lab",
"archived": "your party-line account is archived; ask Jumpstart Lab",
"env_provisioned": "this machine's party-line token is managed for you; nothing to do",
"machine_taken": "another machine already uses that name; pick another: `work bus enroll <name>`",
"invalid_machine": "that machine name isn't allowed; use lowercase letters, digits and dashes",
"enrollment_not_configured": "party-line isn't open for new machines yet; try again later",
}
var machineName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}$`)
// defaultMachine is this computer's short host name, lowercased.
func defaultMachine() string {
h, _ := os.Hostname()
h = strings.ToLower(strings.SplitN(h, ".", 2)[0])
return strings.Trim(regexp.MustCompile(`[^a-z0-9-]+`).ReplaceAllString(h, "-"), "-")
}
// busEnroll enrolls this machine on party-line as the signed-in person and
// writes its node token. Running it again rotates the token.
func busEnroll(machine string) error {
if machine == "" {
machine = defaultMachine()
}
if !machineName.MatchString(machine) {
return fmt.Errorf("%q is not a usable machine name (lowercase letters, digits and dashes)", machine)
}
tok, err := accessToken()
if err != nil {
return err
}
body, _ := json.Marshal(map[string]string{"machine": machine})
req, _ := http.NewRequest("POST", partyLineURL()+"/api/v1/enrollments", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+tok)
req.Header.Set("Content-Type", "application/json")
resp, err := (&http.Client{Timeout: 30 * time.Second}).Do(req)
if err != nil {
return fmt.Errorf("party-line unreachable: %w", err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
var out struct {
OK bool `json:"ok"`
Machine string `json:"machine"`
Token string `json:"token"`
Error string `json:"error"`
Tenant struct {
Slug string `json:"slug"`
} `json:"tenant"`
}
_ = json.Unmarshal(raw, &out)
if resp.StatusCode/100 != 2 || out.Token == "" {
msg := out.Error
if plain, ok := enrollErrors[msg]; ok {
msg = plain + " (" + msg + ")"
}
if msg == "" {
msg = strings.TrimSpace(string(raw))
if len(msg) > 200 {
msg = msg[:200]
}
}
return fmt.Errorf("party-line refused enrollment (HTTP %d): %s", resp.StatusCode, msg)
}
if out.Machine != "" {
machine = out.Machine
}
dir := partyLineDir()
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
path := filepath.Join(dir, machine+".env")
env := fmt.Sprintf("PARTY_LINE_URL=%s\nNODE_TOKEN=%s\nNODE_MACHINE=%s\n", partyLineURL(), out.Token, machine)
if err := os.WriteFile(path, []byte(env), 0o600); err != nil {
return err
}
if err := os.Chmod(path, 0o600); err != nil { // WriteFile keeps an existing file's mode
return err
}
tenant := ""
if out.Tenant.Slug != "" {
tenant = " (tenant " + out.Tenant.Slug + ")"
}
fmt.Printf("Enrolled %s on party-line%s; token in %s.\n", machine, tenant, path)
if err := addPartyLineMCP(out.Token); err != nil {
fmt.Fprintf(os.Stderr, "Claude Code not set up for party-line (%v).\nTo do it by hand: %s\n", err,
strings.Join(append([]string{"claude"}, mcpAddArgs("<token from "+path+">")...), " "))
}
return nil
}
// mcpAddArgs registers party-line's HTTP MCP (the tenant-scoped "phone"
// toolset: send, check_inbox, read_thread, roll_call, lines) for every project.
func mcpAddArgs(token string) []string {
return []string{"mcp", "add", "--scope", "user", "--transport", "http", "party-line",
partyLineURL() + "/mcp", "--header", "Authorization: Bearer " + token}
}
// addPartyLineMCP points Claude Code at party-line with this machine's token,
// replacing an earlier entry (a re-enroll rotates the token).
var addPartyLineMCP = func(token string) error {
claude, err := exec.LookPath("claude")
if err != nil {
return errors.New("claude is not installed here")
}
_ = exec.Command(claude, "mcp", "remove", "--scope", "user", "party-line").Run()
if out, err := exec.Command(claude, mcpAddArgs(token)...).CombinedOutput(); err != nil {
return fmt.Errorf("claude mcp add: %v: %s", err, strings.TrimSpace(string(out)))
}
fmt.Println("Claude Code now has party-line (restart open Claude sessions to see it).")
return nil
}
func bus(args []string) error {
if len(args) == 0 || args[0] != "enroll" {
return errors.New("usage: work bus enroll [machine]")
}
machine := ""
if len(args) > 1 {
machine = args[1]
}
return busEnroll(machine)
}