// Package fprobe is the Mac's shipped Engineering probes, F1 to F7 (Mac // runners spec §8.2 and §8.3, slice F task 14): proof, on the Mac, as the // runner user, that Claude Code's own sandbox holds under the settings // Engineering launches with. They run as three short launches of the // pinned CLI on a connection's small model, each prompted to run one // fixed script with Bash (and, in launch A, to make fixed native tool // calls): // // - launch A, the no-Xcode settings: F1 (no network), F2 (shell reads // denied), F3 (the token out of reach), F6 (native tools refused) and // F7 (no Xcode without access); // - launch B, the sandboxed-Xcode settings, only while P2 is denied: F5 // (P2's hostile fixture built and tested by xcodebuild from Bash); // - launch C, the no-Xcode settings, last: F4 (the process group: the // runner cancels the launch, and what it left must be gone). // // The verdict never reads model text. Observers outside the sandbox // decide: a loopback listener, nonce sentinels the runner plants and // then searches for in the probe tree, the runner's own permission tool, // the process table, and the same commands run outside the sandbox. Each // probe has a control that must succeed, and a missing record is never a // denial. The probe tree is the launch's worktree: it is never committed, // uploaded or logged, and it is removed when the launch ends. // // A launch's results are kept with the inputs it ran under (Inputs) and // trusted only while those are the current inputs and for under a day. package fprobe import ( "crypto/sha256" "encoding/hex" "fmt" "slices" "strings" "sync" "time" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/provider/claude" "amadan.net/oficina/equipo/internal/wire" ) // The three launches. const ( LaunchA = "A" LaunchB = "B" LaunchC = "C" ) // Launches is the order they run in: C last, since it cancels its launch. var Launches = []string{LaunchA, LaunchB, LaunchC} // launchProbes are the probes each launch decides. var launchProbes = map[string][]string{ LaunchA: {"F1", "F2", "F3", "F6", "F7"}, LaunchB: {"F5"}, LaunchC: {"F4"}, } // LaunchOf is the launch that decides probe id ("" for none). func LaunchOf(id string) string { for l, ids := range launchProbes { if slices.Contains(ids, id) { return l } } return "" } // Required are the probes Engineering needs (spec §8.5); F5 decides only // sandboxed Xcode access. var Required = []string{"F1", "F2", "F3", "F4", "F6", "F7"} // Trust bounds a result's age (under a day); a result is run again once // it is Refresh old, so a healthy Mac does not lapse; a launch that could // not decide is tried again after Retry. const ( Trust = 24 * time.Hour Refresh = 23 * time.Hour Retry = time.Hour ) // Inputs are what a launch's results are trusted for: this runner // version, the Engineering settings profiles and the probe scripts // (Settings), the verified pinned CLI (CLI: its digest, "" when it does // not verify), this Xcode (version and build) and its simulator runtimes. type Inputs struct { Runner string `json:"runner"` Settings string `json:"settings"` CLI string `json:"cli"` Xcode string `json:"xcode"` Runtimes string `json:"runtimes"` } // NewInputs is the inputs now: version is this runner's, verified whether // the pinned CLI verifies, x and sims what the capability report found. func NewInputs(version string, verified bool, x wire.Xcode, sims []wire.Simulator) Inputs { in := Inputs{Runner: version, Settings: SettingsDigest()} if verified { in.CLI = claude.ExecutableSHA256DarwinArm64 } if x.Version != "" { in.Xcode = x.Version + " (" + x.Build + ")" } var runtimes []string for _, s := range sims { if s.Runtime != "" && !slices.Contains(runtimes, s.Runtime) { runtimes = append(runtimes, s.Runtime) } } slices.Sort(runtimes) in.Runtimes = strings.Join(runtimes, ", ") return in } // runnable: a launch can run under these inputs at all. func (i Inputs) runnable() bool { return i.Runner != "" && i.Settings != "" && i.CLI != "" } // changed names the first input that differs from was ("" when none). func (i Inputs) changed(was Inputs) string { switch { case i.Runner != was.Runner: return "stale_runner" case i.Settings != was.Settings: return "stale_settings" case i.CLI != was.CLI: return "stale_cli" case i.Xcode != was.Xcode: return "stale_xcode" case i.Runtimes != was.Runtimes: return "stale_runtimes" } return "" } // settingsInput is a fixed launch, so the digest names the generator, // never this Mac's paths. func settingsInput(xcode bool) claude.DarwinInputs { 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", 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", 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"}}), Attempt: "fprobe-x", SessionID: "00000000-0000-0000-0000-000000000000"} if xcode { d.XcodeAccess, d.XcodeWrites, d.Mach = wire.XcodeAccessSandboxed, []string{"/Users/u/s/p/DerivedData"}, mac.CheckMachServices } return d } // SettingsDigest names the settings the probes judge: both Engineering // profiles over fixed paths, the probe scripts and prompts, and P2's // fixture (F5 builds it). A profile that cannot be made is named so. func SettingsDigest() string { h := sha256.New() for _, xcode := range []bool{false, true} { p, err := claude.DarwinPolicy(settingsInput(xcode)) if err != nil { fmt.Fprintf(h, "unmade: %v\n", err) continue } h.Write(p.Settings) h.Write([]byte(p.Hash)) } fmt.Fprintf(h, "%s\n%s\n%s\n", scriptVersion, mac.ProfileDigest(), mac.P2Script("x", "y", "z")) return hex.EncodeToString(h.Sum(nil))[:16] } // Result is one launch's results and the inputs it ran under. type Result struct { Launch string `json:"launch"` Inputs Inputs `json:"inputs"` At time.Time `json:"at"` Probes []wire.Probe `json:"probes"` } // decided: every probe of the result reached a verdict (unavailable // means the launch could not say, and it is tried again after Retry). func (r Result) decided() bool { return len(r.Probes) > 0 && !slices.ContainsFunc(r.Probes, func(p wire.Probe) bool { return p.Verdict == wire.ProbeUnavailable }) } // Store keeps each launch's newest result, in this process's memory only // (as P1 to P7: nothing on disk is read back as a verdict), and when each // launch was last tried and under what inputs. type Store struct { mu sync.Mutex results map[string]Result tried map[string]Result } // Put keeps r as its launch's newest result. func (s *Store) Put(r Result) { s.mu.Lock() defer s.mu.Unlock() if s.results == nil { s.results = map[string]Result{} } s.results[r.Launch] = r } // try notes that launch is being run now under in. func (s *Store) try(launch string, in Inputs, at time.Time) { s.mu.Lock() defer s.mu.Unlock() if s.tried == nil { s.tried = map[string]Result{} } s.tried[launch] = Result{Launch: launch, Inputs: in, At: at} } // age is now less at, and whether a result made then may still count // (under Trust, not dated more than a minute ahead). func age(at, now time.Time) (time.Duration, bool) { d := now.Sub(at) return d, !at.IsZero() && d < Trust && d >= -time.Minute } // Probes is F1 to F7 as the capability report says them now: each from // its launch's result when that ran under the current inputs and is // under a day old; otherwise not proven, saying why. F5 is skipped while // P2 is not denied: Xcode then stays in checks. func (s *Store) Probes(cur Inputs, now time.Time, p2Denied bool) []wire.Probe { s.mu.Lock() defer s.mu.Unlock() out := make([]wire.Probe, 0, len(wire.EngineeringProbeIDs)) for _, id := range wire.EngineeringProbeIDs { r, ok := s.results[LaunchOf(id)] d, fresh := age(r.At, now) p := wire.Probe{ID: id, Verdict: wire.ProbeUnavailable} switch why := cur.changed(r.Inputs); { case id == "F5" && !p2Denied: p.Verdict, p.Detail = wire.ProbeSkipped, wire.ProbeDetail("f5_skipped") case !ok: p.Detail = wire.ProbeDetail("not_run") case why != "": p.Detail = wire.ProbeDetail(why) case !fresh && d < 0: p.Detail = wire.ProbeDetail("future") case !fresh: p.Detail = wire.ProbeDetail("expired") default: for _, q := range r.Probes { if q.ID == id { p = q } } } out = append(out, p) } return out } // Due is the launches to run now, in order: each needed (B only while P2 // is denied) whose result is missing, made under other inputs, undecided, // or Refresh old, unless it was tried under these same inputs less than // Retry ago (an undecided launch waits, as a failed one waits for its // result to age). Nothing is due under inputs no launch can run under. func (s *Store) Due(cur Inputs, now time.Time, p2Denied bool) []string { if !cur.runnable() { return nil } s.mu.Lock() defer s.mu.Unlock() var due []string for _, l := range Launches { if l == LaunchB && !p2Denied { continue } r, ok := s.results[l] d, fresh := age(r.At, now) if ok && cur.changed(r.Inputs) == "" && fresh && d < Refresh && r.decided() { continue } if t, ok := s.tried[l]; ok && cur.changed(t.Inputs) == "" && now.Sub(t.At) < Retry && now.Sub(t.At) >= -time.Minute { continue } due = append(due, l) } return due } // State is what Engineering on this Mac depends on now. type State struct { // Guards is the credential guards' status (tainted, else home_open), // nil when both pass. Guards *wire.EngineeringStatus // CLI: the pinned CLI verifies. CLI bool // Probes are F1 to F7 now (Store.Probes). Probes []wire.Probe // Conn is "" while a Claude connection is usable, else the // connection state that says why not (ConnState). Conn, ConnReason string // P2 is the trusted P2 verdict ("" while none is trusted): the // object scan's gitleaks runs under the checks profile P2 proves. P2 string } // Status is Engineering's status on this Mac: ready when the guards // pass, the pinned CLI verifies, F1 to F4, F6 and F7 reached their // success verdicts for the current inputs, P2 is denied (the object // scan's sandbox; P2 named otherwise) and a Claude connection is // usable; otherwise the first thing missing, in this order: the guards // (they last), the CLI, a probe that failed (no connection fixes it), the // connection (the probes need one), the probes not yet proven. func Status(s State) wire.EngineeringStatus { if s.Guards != nil && s.Guards.State != wire.EngineeringReady { return *s.Guards } if !s.CLI { return wire.EngineeringStatus{State: wire.EngineeringCLIUnpinned} } var failed, pending []string for _, id := range Required { p := probe(s.Probes, id) switch { case p.Verdict == wire.ProbeFailed: failed = append(failed, id) case p.Verdict != wire.EngineeringProbeSuccess(id): pending = append(pending, id) } } // The object scan's sandbox: P2 denied, or nothing leaves the Mac. switch s.P2 { case wire.ProbeDenied: case "": pending = append(pending, "P2") default: failed = append(failed, "P2") } switch { case len(failed) > 0: return wire.EngineeringStatus{State: wire.EngineeringProbesFailed, Reason: strings.Join(failed, " ")} case s.Conn != "": return wire.EngineeringStatus{State: s.Conn, Reason: s.ConnReason} case len(pending) > 0: return wire.EngineeringStatus{State: wire.EngineeringProbesPending, Reason: strings.Join(pending, " ")} } return wire.EngineeringStatus{State: wire.EngineeringReady} } // probe is id in probes (a zero verdict when absent). func probe(probes []wire.Probe, id string) wire.Probe { for _, p := range probes { if p.ID == id { return p } } return wire.Probe{ID: id} } // XcodeAllowed: F5 is denied for the current inputs, so Engineering's // Bash sandbox may have Xcode's services (with P2 denied, which F5 // needs to run at all). func XcodeAllowed(probes []wire.Probe) bool { return probe(probes, "F5").Verdict == wire.ProbeDenied } // ConnState is why no Claude connection is usable ("" when one is): none // held, else limited when one is limited, else signed out. Only Claude // token connections run on a Mac (§8.5). func ConnState(held []wire.HeldConnection, usable func(id string) bool) string { state := wire.EngineeringNoConnection for _, c := range held { if c.Provider != wire.ProviderClaude || c.Kind != wire.KindToken { continue } switch { case usable(c.LocalID): return "" case c.State == wire.ConnLimited: state = wire.EngineeringLimited case state == wire.EngineeringNoConnection: state = wire.EngineeringSignedOut } } return state }