| 1 | // Package fprobe is the Mac's shipped Engineering probes, F1 to F7 (Mac |
| 2 | // runners spec §8.2 and §8.3, slice F task 14): proof, on the Mac, as the |
| 3 | // runner user, that Claude Code's own sandbox holds under the settings |
| 4 | // Engineering launches with. They run as three short launches of the |
| 5 | // pinned CLI on a connection's small model, each prompted to run one |
| 6 | // fixed script with Bash (and, in launch A, to make fixed native tool |
| 7 | // calls): |
| 8 | // |
| 9 | // - launch A, the no-Xcode settings: F1 (no network), F2 (shell reads |
| 10 | // denied), F3 (the token out of reach), F6 (native tools refused) and |
| 11 | // F7 (no Xcode without access); |
| 12 | // - launch B, the sandboxed-Xcode settings, only while P2 is denied: F5 |
| 13 | // (P2's hostile fixture built and tested by xcodebuild from Bash); |
| 14 | // - launch C, the no-Xcode settings, last: F4 (the process group: the |
| 15 | // runner cancels the launch, and what it left must be gone). |
| 16 | // |
| 17 | // The verdict never reads model text. Observers outside the sandbox |
| 18 | // decide: a loopback listener, nonce sentinels the runner plants and |
| 19 | // then searches for in the probe tree, the runner's own permission tool, |
| 20 | // the process table, and the same commands run outside the sandbox. Each |
| 21 | // probe has a control that must succeed, and a missing record is never a |
| 22 | // denial. The probe tree is the launch's worktree: it is never committed, |
| 23 | // uploaded or logged, and it is removed when the launch ends. |
| 24 | // |
| 25 | // A launch's results are kept with the inputs it ran under (Inputs) and |
| 26 | // trusted only while those are the current inputs and for under a day. |
| 27 | package fprobe |
| 28 | |
| 29 | import ( |
| 30 | "crypto/sha256" |
| 31 | "encoding/hex" |
| 32 | "fmt" |
| 33 | "slices" |
| 34 | "strings" |
| 35 | "sync" |
| 36 | "time" |
| 37 | |
| 38 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 39 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 40 | "amadan.net/oficina/equipo/internal/wire" |
| 41 | ) |
| 42 | |
| 43 | // The three launches. |
| 44 | const ( |
| 45 | LaunchA = "A" |
| 46 | LaunchB = "B" |
| 47 | LaunchC = "C" |
| 48 | ) |
| 49 | |
| 50 | // Launches is the order they run in: C last, since it cancels its launch. |
| 51 | var Launches = []string{LaunchA, LaunchB, LaunchC} |
| 52 | |
| 53 | // launchProbes are the probes each launch decides. |
| 54 | var launchProbes = map[string][]string{ |
| 55 | LaunchA: {"F1", "F2", "F3", "F6", "F7"}, |
| 56 | LaunchB: {"F5"}, |
| 57 | LaunchC: {"F4"}, |
| 58 | } |
| 59 | |
| 60 | // LaunchOf is the launch that decides probe id ("" for none). |
| 61 | func LaunchOf(id string) string { |
| 62 | for l, ids := range launchProbes { |
| 63 | if slices.Contains(ids, id) { |
| 64 | return l |
| 65 | } |
| 66 | } |
| 67 | return "" |
| 68 | } |
| 69 | |
| 70 | // Required are the probes Engineering needs (spec §8.5); F5 decides only |
| 71 | // sandboxed Xcode access. |
| 72 | var Required = []string{"F1", "F2", "F3", "F4", "F6", "F7"} |
| 73 | |
| 74 | // Trust bounds a result's age (under a day); a result is run again once |
| 75 | // it is Refresh old, so a healthy Mac does not lapse; a launch that could |
| 76 | // not decide is tried again after Retry. |
| 77 | const ( |
| 78 | Trust = 24 * time.Hour |
| 79 | Refresh = 23 * time.Hour |
| 80 | Retry = time.Hour |
| 81 | ) |
| 82 | |
| 83 | // Inputs are what a launch's results are trusted for: this runner |
| 84 | // version, the Engineering settings profiles and the probe scripts |
| 85 | // (Settings), the verified pinned CLI (CLI: its digest, "" when it does |
| 86 | // not verify), this Xcode (version and build) and its simulator runtimes. |
| 87 | type Inputs struct { |
| 88 | Runner string `json:"runner"` |
| 89 | Settings string `json:"settings"` |
| 90 | CLI string `json:"cli"` |
| 91 | Xcode string `json:"xcode"` |
| 92 | Runtimes string `json:"runtimes"` |
| 93 | } |
| 94 | |
| 95 | // NewInputs is the inputs now: version is this runner's, verified whether |
| 96 | // the pinned CLI verifies, x and sims what the capability report found. |
| 97 | func NewInputs(version string, verified bool, x wire.Xcode, sims []wire.Simulator) Inputs { |
| 98 | in := Inputs{Runner: version, Settings: SettingsDigest()} |
| 99 | if verified { |
| 100 | in.CLI = claude.ExecutableSHA256DarwinArm64 |
| 101 | } |
| 102 | if x.Version != "" { |
| 103 | in.Xcode = x.Version + " (" + x.Build + ")" |
| 104 | } |
| 105 | var runtimes []string |
| 106 | for _, s := range sims { |
| 107 | if s.Runtime != "" && !slices.Contains(runtimes, s.Runtime) { |
| 108 | runtimes = append(runtimes, s.Runtime) |
| 109 | } |
| 110 | } |
| 111 | slices.Sort(runtimes) |
| 112 | in.Runtimes = strings.Join(runtimes, ", ") |
| 113 | return in |
| 114 | } |
| 115 | |
| 116 | // runnable: a launch can run under these inputs at all. |
| 117 | func (i Inputs) runnable() bool { return i.Runner != "" && i.Settings != "" && i.CLI != "" } |
| 118 | |
| 119 | // changed names the first input that differs from was ("" when none). |
| 120 | func (i Inputs) changed(was Inputs) string { |
| 121 | switch { |
| 122 | case i.Runner != was.Runner: |
| 123 | return "stale_runner" |
| 124 | case i.Settings != was.Settings: |
| 125 | return "stale_settings" |
| 126 | case i.CLI != was.CLI: |
| 127 | return "stale_cli" |
| 128 | case i.Xcode != was.Xcode: |
| 129 | return "stale_xcode" |
| 130 | case i.Runtimes != was.Runtimes: |
| 131 | return "stale_runtimes" |
| 132 | } |
| 133 | return "" |
| 134 | } |
| 135 | |
| 136 | // settingsInput is a fixed launch, so the digest names the generator, |
| 137 | // never this Mac's paths. |
| 138 | func settingsInput(xcode bool) claude.DarwinInputs { |
| 139 | d := claude.DarwinInputs{Action: claude.DarwinImplement, Model: "claude-haiku-5-5", CLI: "/Users/u/i/claude/claude", CLIDigest: claude.ExecutableSHA256DarwinArm64, CLIDir: "/Users/u/i/claude", InstallDir: "/Users/u/i", |
| 140 | Runner: "/Users/u/i/equipo-runner", ToolRoot: "/Users/u/s/t", Socket: "/Users/u/s/t/x.sock", Workspace: "/Users/u/s/w", Scratch: "/Users/u/s/p", Home: "/Users/u", |
| 141 | ConfigDir: "/Users/u/s/homes/c", CredentialHome: "/Users/u/a/c", RuntimeRoot: "/Users/u/s/homes", Denied: mac.DenyList(mac.Paths{Home: "/Users/u", StateDir: "/Users/u/st", AccountsRoot: "/Users/u/a", SessionRoot: "/Users/u/n", OtherHomes: []string{"/Users/o"}}), |
| 142 | Attempt: "fprobe-x", SessionID: "00000000-0000-0000-0000-000000000000"} |
| 143 | if xcode { |
| 144 | d.XcodeAccess, d.XcodeWrites, d.Mach = wire.XcodeAccessSandboxed, []string{"/Users/u/s/p/DerivedData"}, mac.CheckMachServices |
| 145 | } |
| 146 | return d |
| 147 | } |
| 148 | |
| 149 | // SettingsDigest names the settings the probes judge: both Engineering |
| 150 | // profiles over fixed paths, the probe scripts and prompts, and P2's |
| 151 | // fixture (F5 builds it). A profile that cannot be made is named so. |
| 152 | func SettingsDigest() string { |
| 153 | h := sha256.New() |
| 154 | for _, xcode := range []bool{false, true} { |
| 155 | p, err := claude.DarwinPolicy(settingsInput(xcode)) |
| 156 | if err != nil { |
| 157 | fmt.Fprintf(h, "unmade: %v\n", err) |
| 158 | continue |
| 159 | } |
| 160 | h.Write(p.Settings) |
| 161 | h.Write([]byte(p.Hash)) |
| 162 | } |
| 163 | fmt.Fprintf(h, "%s\n%s\n%s\n", scriptVersion, mac.ProfileDigest(), mac.P2Script("x", "y", "z")) |
| 164 | return hex.EncodeToString(h.Sum(nil))[:16] |
| 165 | } |
| 166 | |
| 167 | // Result is one launch's results and the inputs it ran under. |
| 168 | type Result struct { |
| 169 | Launch string `json:"launch"` |
| 170 | Inputs Inputs `json:"inputs"` |
| 171 | At time.Time `json:"at"` |
| 172 | Probes []wire.Probe `json:"probes"` |
| 173 | } |
| 174 | |
| 175 | // decided: every probe of the result reached a verdict (unavailable |
| 176 | // means the launch could not say, and it is tried again after Retry). |
| 177 | func (r Result) decided() bool { |
| 178 | return len(r.Probes) > 0 && !slices.ContainsFunc(r.Probes, func(p wire.Probe) bool { return p.Verdict == wire.ProbeUnavailable }) |
| 179 | } |
| 180 | |
| 181 | // Store keeps each launch's newest result, in this process's memory only |
| 182 | // (as P1 to P7: nothing on disk is read back as a verdict), and when each |
| 183 | // launch was last tried and under what inputs. |
| 184 | type Store struct { |
| 185 | mu sync.Mutex |
| 186 | results map[string]Result |
| 187 | tried map[string]Result |
| 188 | } |
| 189 | |
| 190 | // Put keeps r as its launch's newest result. |
| 191 | func (s *Store) Put(r Result) { |
| 192 | s.mu.Lock() |
| 193 | defer s.mu.Unlock() |
| 194 | if s.results == nil { |
| 195 | s.results = map[string]Result{} |
| 196 | } |
| 197 | s.results[r.Launch] = r |
| 198 | } |
| 199 | |
| 200 | // try notes that launch is being run now under in. |
| 201 | func (s *Store) try(launch string, in Inputs, at time.Time) { |
| 202 | s.mu.Lock() |
| 203 | defer s.mu.Unlock() |
| 204 | if s.tried == nil { |
| 205 | s.tried = map[string]Result{} |
| 206 | } |
| 207 | s.tried[launch] = Result{Launch: launch, Inputs: in, At: at} |
| 208 | } |
| 209 | |
| 210 | // age is now less at, and whether a result made then may still count |
| 211 | // (under Trust, not dated more than a minute ahead). |
| 212 | func age(at, now time.Time) (time.Duration, bool) { |
| 213 | d := now.Sub(at) |
| 214 | return d, !at.IsZero() && d < Trust && d >= -time.Minute |
| 215 | } |
| 216 | |
| 217 | // Probes is F1 to F7 as the capability report says them now: each from |
| 218 | // its launch's result when that ran under the current inputs and is |
| 219 | // under a day old; otherwise not proven, saying why. F5 is skipped while |
| 220 | // P2 is not denied: Xcode then stays in checks. |
| 221 | func (s *Store) Probes(cur Inputs, now time.Time, p2Denied bool) []wire.Probe { |
| 222 | s.mu.Lock() |
| 223 | defer s.mu.Unlock() |
| 224 | out := make([]wire.Probe, 0, len(wire.EngineeringProbeIDs)) |
| 225 | for _, id := range wire.EngineeringProbeIDs { |
| 226 | r, ok := s.results[LaunchOf(id)] |
| 227 | d, fresh := age(r.At, now) |
| 228 | p := wire.Probe{ID: id, Verdict: wire.ProbeUnavailable} |
| 229 | switch why := cur.changed(r.Inputs); { |
| 230 | case id == "F5" && !p2Denied: |
| 231 | p.Verdict, p.Detail = wire.ProbeSkipped, wire.ProbeDetail("f5_skipped") |
| 232 | case !ok: |
| 233 | p.Detail = wire.ProbeDetail("not_run") |
| 234 | case why != "": |
| 235 | p.Detail = wire.ProbeDetail(why) |
| 236 | case !fresh && d < 0: |
| 237 | p.Detail = wire.ProbeDetail("future") |
| 238 | case !fresh: |
| 239 | p.Detail = wire.ProbeDetail("expired") |
| 240 | default: |
| 241 | for _, q := range r.Probes { |
| 242 | if q.ID == id { |
| 243 | p = q |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | out = append(out, p) |
| 248 | } |
| 249 | return out |
| 250 | } |
| 251 | |
| 252 | // Due is the launches to run now, in order: each needed (B only while P2 |
| 253 | // is denied) whose result is missing, made under other inputs, undecided, |
| 254 | // or Refresh old, unless it was tried under these same inputs less than |
| 255 | // Retry ago (an undecided launch waits, as a failed one waits for its |
| 256 | // result to age). Nothing is due under inputs no launch can run under. |
| 257 | func (s *Store) Due(cur Inputs, now time.Time, p2Denied bool) []string { |
| 258 | if !cur.runnable() { |
| 259 | return nil |
| 260 | } |
| 261 | s.mu.Lock() |
| 262 | defer s.mu.Unlock() |
| 263 | var due []string |
| 264 | for _, l := range Launches { |
| 265 | if l == LaunchB && !p2Denied { |
| 266 | continue |
| 267 | } |
| 268 | r, ok := s.results[l] |
| 269 | d, fresh := age(r.At, now) |
| 270 | if ok && cur.changed(r.Inputs) == "" && fresh && d < Refresh && r.decided() { |
| 271 | continue |
| 272 | } |
| 273 | if t, ok := s.tried[l]; ok && cur.changed(t.Inputs) == "" && now.Sub(t.At) < Retry && now.Sub(t.At) >= -time.Minute { |
| 274 | continue |
| 275 | } |
| 276 | due = append(due, l) |
| 277 | } |
| 278 | return due |
| 279 | } |
| 280 | |
| 281 | // State is what Engineering on this Mac depends on now. |
| 282 | type State struct { |
| 283 | // Guards is the credential guards' status (tainted, else home_open), |
| 284 | // nil when both pass. |
| 285 | Guards *wire.EngineeringStatus |
| 286 | // CLI: the pinned CLI verifies. |
| 287 | CLI bool |
| 288 | // Probes are F1 to F7 now (Store.Probes). |
| 289 | Probes []wire.Probe |
| 290 | // Conn is "" while a Claude connection is usable, else the |
| 291 | // connection state that says why not (ConnState). |
| 292 | Conn, ConnReason string |
| 293 | // P2 is the trusted P2 verdict ("" while none is trusted): the |
| 294 | // object scan's gitleaks runs under the checks profile P2 proves. |
| 295 | P2 string |
| 296 | } |
| 297 | |
| 298 | // Status is Engineering's status on this Mac: ready when the guards |
| 299 | // pass, the pinned CLI verifies, F1 to F4, F6 and F7 reached their |
| 300 | // success verdicts for the current inputs, P2 is denied (the object |
| 301 | // scan's sandbox; P2 named otherwise) and a Claude connection is |
| 302 | // usable; otherwise the first thing missing, in this order: the guards |
| 303 | // (they last), the CLI, a probe that failed (no connection fixes it), the |
| 304 | // connection (the probes need one), the probes not yet proven. |
| 305 | func Status(s State) wire.EngineeringStatus { |
| 306 | if s.Guards != nil && s.Guards.State != wire.EngineeringReady { |
| 307 | return *s.Guards |
| 308 | } |
| 309 | if !s.CLI { |
| 310 | return wire.EngineeringStatus{State: wire.EngineeringCLIUnpinned} |
| 311 | } |
| 312 | var failed, pending []string |
| 313 | for _, id := range Required { |
| 314 | p := probe(s.Probes, id) |
| 315 | switch { |
| 316 | case p.Verdict == wire.ProbeFailed: |
| 317 | failed = append(failed, id) |
| 318 | case p.Verdict != wire.EngineeringProbeSuccess(id): |
| 319 | pending = append(pending, id) |
| 320 | } |
| 321 | } |
| 322 | // The object scan's sandbox: P2 denied, or nothing leaves the Mac. |
| 323 | switch s.P2 { |
| 324 | case wire.ProbeDenied: |
| 325 | case "": |
| 326 | pending = append(pending, "P2") |
| 327 | default: |
| 328 | failed = append(failed, "P2") |
| 329 | } |
| 330 | switch { |
| 331 | case len(failed) > 0: |
| 332 | return wire.EngineeringStatus{State: wire.EngineeringProbesFailed, Reason: strings.Join(failed, " ")} |
| 333 | case s.Conn != "": |
| 334 | return wire.EngineeringStatus{State: s.Conn, Reason: s.ConnReason} |
| 335 | case len(pending) > 0: |
| 336 | return wire.EngineeringStatus{State: wire.EngineeringProbesPending, Reason: strings.Join(pending, " ")} |
| 337 | } |
| 338 | return wire.EngineeringStatus{State: wire.EngineeringReady} |
| 339 | } |
| 340 | |
| 341 | // probe is id in probes (a zero verdict when absent). |
| 342 | func probe(probes []wire.Probe, id string) wire.Probe { |
| 343 | for _, p := range probes { |
| 344 | if p.ID == id { |
| 345 | return p |
| 346 | } |
| 347 | } |
| 348 | return wire.Probe{ID: id} |
| 349 | } |
| 350 | |
| 351 | // XcodeAllowed: F5 is denied for the current inputs, so Engineering's |
| 352 | // Bash sandbox may have Xcode's services (with P2 denied, which F5 |
| 353 | // needs to run at all). |
| 354 | func XcodeAllowed(probes []wire.Probe) bool { |
| 355 | return probe(probes, "F5").Verdict == wire.ProbeDenied |
| 356 | } |
| 357 | |
| 358 | // ConnState is why no Claude connection is usable ("" when one is): none |
| 359 | // held, else limited when one is limited, else signed out. Only Claude |
| 360 | // token connections run on a Mac (§8.5). |
| 361 | func ConnState(held []wire.HeldConnection, usable func(id string) bool) string { |
| 362 | state := wire.EngineeringNoConnection |
| 363 | for _, c := range held { |
| 364 | if c.Provider != wire.ProviderClaude || c.Kind != wire.KindToken { |
| 365 | continue |
| 366 | } |
| 367 | switch { |
| 368 | case usable(c.LocalID): |
| 369 | return "" |
| 370 | case c.State == wire.ConnLimited: |
| 371 | state = wire.EngineeringLimited |
| 372 | case state == wire.EngineeringNoConnection: |
| 373 | state = wire.EngineeringSignedOut |
| 374 | } |
| 375 | } |
| 376 | return state |
| 377 | } |
| 378 | |