| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 11 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // The Machines screens (Paul, 2026-11-11), in Settings beside |
| 16 | // Connections and drawn like it: one compact row per machine with its |
| 17 | // name, its state in words with a dot, its role and its platform; the |
| 18 | // rest behind a click (the row is a <details>, so it opens without |
| 19 | // scripts). The person's own machines show; other people's (only an |
| 20 | // owner sees them) are folded with a count. Revoke and Remove now sit |
| 21 | // inside the details as quiet buttons, each behind a confirming second |
| 22 | // click. Add a machine is its own screen: the install line, the code, |
| 23 | // and the person's new machines until they connect, following the |
| 24 | // person's event stream. Only presentation lives here: who sees which |
| 25 | // machine is runnersFor's, approval is the enrolment flow's. |
| 26 | |
| 27 | // Machine states, as data-s on the row's state. |
| 28 | const ( |
| 29 | machineOnline = "online" |
| 30 | machineOffline = "offline" |
| 31 | machineNever = "never" |
| 32 | machineRemoving = "removing" |
| 33 | machineRevoked = "revoked" |
| 34 | ) |
| 35 | |
| 36 | // machineNewFor is how long a machine stays on the Add page after it |
| 37 | // was approved, so the person sees it come online. |
| 38 | const machineNewFor = 30 * time.Minute |
| 39 | |
| 40 | // machinesView is the Machines list and the Add a machine page. |
| 41 | type machinesView struct { |
| 42 | Adding bool |
| 43 | // Mine are the person's machines; Others everyone else's an owner |
| 44 | // sees, folded. |
| 45 | Mine, Others []machineRow |
| 46 | // New are the person's machines on the Add page: never connected, |
| 47 | // or approved in the last half hour. |
| 48 | New []machineRow |
| 49 | // Install is the install line, "" when this release has none. |
| 50 | Install string |
| 51 | // Previews says which of the person's machines hosts previews, "" when |
| 52 | // none is paired (the section then says how to pair one). |
| 53 | Previews string |
| 54 | } |
| 55 | |
| 56 | // machineRow is one machine. |
| 57 | type machineRow struct { |
| 58 | ID, Name, Person string |
| 59 | // State is one of the machine states; Words says it, Seen when an |
| 60 | // offline machine was last seen. |
| 61 | State, Words, Seen string |
| 62 | // Role and Platform are the row's facts in words; Mark is the icon |
| 63 | // in its tile. |
| 64 | Role, Platform, Mark string |
| 65 | // The details. |
| 66 | Raw, Version, Protocol, Tags, Enrolled, EnrolledBy, Fingerprint string |
| 67 | Report *setup.RunnerReport |
| 68 | Tainted, Removing, Revoked bool |
| 69 | // HoldsClaude: the instance holds this Mac as having a model |
| 70 | // credential (D1: its checks run only in its sandbox). |
| 71 | HoldsClaude bool |
| 72 | Revision int64 |
| 73 | // Record is the runner itself, for its supervised history. |
| 74 | Record Runner |
| 75 | } |
| 76 | |
| 77 | func machinesPage(records []machineRecord, me string, now time.Time, adding bool, install string) machinesView { |
| 78 | v := machinesView{Adding: adding, Install: install} |
| 79 | for _, r := range records { |
| 80 | row := machineRowOf(r, now) |
| 81 | switch { |
| 82 | case r.Person != me: |
| 83 | v.Others = append(v.Others, row) |
| 84 | default: |
| 85 | v.Mine = append(v.Mine, row) |
| 86 | fresh := r.Facts.EnrolledAt != nil && now.Sub(*r.Facts.EnrolledAt) < machineNewFor |
| 87 | if adding && !row.Revoked && (row.State == machineNever || fresh) { |
| 88 | v.New = append(v.New, row) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | v.Previews = previewsLine(records, me, now) |
| 93 | for _, rows := range [][]machineRow{v.Mine, v.Others, v.New} { |
| 94 | slices.SortStableFunc(rows, func(a, b machineRow) int { |
| 95 | if c := strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)); c != 0 { |
| 96 | return c |
| 97 | } |
| 98 | return strings.Compare(a.ID, b.ID) |
| 99 | }) |
| 100 | } |
| 101 | return v |
| 102 | } |
| 103 | |
| 104 | func machineRowOf(r machineRecord, now time.Time) machineRow { |
| 105 | f := r.Facts |
| 106 | row := machineRow{ID: r.ID, Name: r.Label, Person: r.Person, Fingerprint: r.MachineKeyHash, Report: r.Report, Tainted: r.TaintedAt != nil, HoldsClaude: r.CredentialHeldAt != nil, Removing: r.RemovingAt != nil, Revoked: r.RevokedAt != nil, Revision: r.Revision, Record: r.Runner, Mark: "monitor"} |
| 107 | if f.Label != "" { |
| 108 | row.Name = f.Label |
| 109 | } |
| 110 | switch { |
| 111 | case row.Revoked: |
| 112 | row.State = machineRevoked |
| 113 | case row.Removing: |
| 114 | row.State = machineRemoving |
| 115 | case !f.Session: |
| 116 | row.State = machineNever |
| 117 | case f.Connected && now.Sub(f.SeenAt) < chat.RunnerOnline: |
| 118 | row.State = machineOnline |
| 119 | default: |
| 120 | row.State = machineOffline |
| 121 | row.Seen = fmt.Sprintf(copyText("machines.last_seen"), ago(now, f.SeenAt)) |
| 122 | } |
| 123 | row.Words = copyText("machines.state." + row.State) |
| 124 | row.Role = copyText("machines.role.primary") |
| 125 | if r.Role == setup.RoleWorker { |
| 126 | row.Role = copyText("machines.role.worker") |
| 127 | } |
| 128 | if r.Report != nil { |
| 129 | row.Raw = r.Report.Platform |
| 130 | os, _, _ := strings.Cut(r.Report.Platform, "/") |
| 131 | row.Platform = copyText("machines.platform." + os) |
| 132 | if row.Platform == "" { |
| 133 | row.Platform = os |
| 134 | } |
| 135 | switch os { |
| 136 | case "darwin": |
| 137 | row.Mark = "laptop" |
| 138 | case "linux": |
| 139 | row.Mark = "server" |
| 140 | } |
| 141 | } |
| 142 | row.Version = f.Versions["runner"] |
| 143 | if f.Session { |
| 144 | row.Protocol = strconv.Itoa(f.Protocol) |
| 145 | } |
| 146 | row.Tags = strings.Join(f.Tags, ", ") |
| 147 | if f.EnrolledAt != nil { |
| 148 | row.Enrolled = f.EnrolledAt.UTC().Format(copyText("runner.requested_format")) |
| 149 | row.EnrolledBy = f.EnrolledBy |
| 150 | } |
| 151 | return row |
| 152 | } |
| 153 | |
| 154 | // previewsLine names the person's machine that hosts previews: an online |
| 155 | // one first, and CARLOS devbox before Carloku temporary sites; "" when no |
| 156 | // machine of theirs is paired with a preview host. |
| 157 | func previewsLine(records []machineRecord, me string, now time.Time) string { |
| 158 | best, bestRank := "", 0 |
| 159 | for _, r := range records { |
| 160 | h := r.Facts.PreviewHost |
| 161 | if r.Person != me || h == nil || r.RevokedAt != nil { |
| 162 | continue |
| 163 | } |
| 164 | rank := 1 |
| 165 | if h.Kind == wire.PreviewHostDevbox { |
| 166 | rank += 2 |
| 167 | } |
| 168 | if r.Facts.Connected && now.Sub(r.Facts.SeenAt) < chat.RunnerOnline { |
| 169 | rank += 4 |
| 170 | } |
| 171 | if rank <= bestRank { |
| 172 | continue |
| 173 | } |
| 174 | name := r.Label |
| 175 | if r.Facts.Label != "" { |
| 176 | name = r.Facts.Label |
| 177 | } |
| 178 | key := "machines.previews_carloku" |
| 179 | if h.Kind == wire.PreviewHostDevbox { |
| 180 | key = "machines.previews_devbox" |
| 181 | } |
| 182 | best, bestRank = fmt.Sprintf(copyText(key), name), rank |
| 183 | } |
| 184 | return best |
| 185 | } |
| 186 | |