Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

ci: compile server for darwin on every backend PR and test darwin implementations daily (MUL-9177) (#10524)

Jiayuan Zhang00501e998967
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ffe8a96ebb..42d5911879 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -507,10 +507,47 @@ jobs:
working-directory: server
run: go tool govulncheck ./...
+ # Every other backend job builds for Linux, so a `_darwin.go` file or a
+ # `//go:build darwin` test that falls out of step with a shared signature
+ # stayed green until the release build compiled it. Cross-vetting from
+ # ubuntu type-checks the whole module, tests included, the way macOS builds
+ # it, without paying for a macOS runner. arm64 because that is what Apple
+ # silicon users run; cgo is off when cross-compiling, as in the release.
+ # Its own runner rather than a backend-tests step: a cold darwin vet adds
+ # ~1 GB to the build cache that job publishes (see MUL-7547), and here it
+ # runs in parallel with the long pole instead of extending it.
+ backend-darwin-vet:
+ needs: changes
+ if: ${{ needs.changes.outputs.backend == 'true' }}
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v6
+
+ - name: Setup Go
+ uses: actions/setup-go@v5
+ with:
+ go-version: "~1.27.4"
+ check-latest: true
+ cache: false
+
+ - name: Restore Go module cache
+ uses: actions/cache/restore@v6
+ with:
+ path: ~/go/pkg/mod
+ key: go-mod-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('server/go.sum') }}
+
+ - name: Vet darwin build
+ working-directory: server
+ env:
+ GOOS: darwin
+ GOARCH: arm64
+ run: go vet ./...
+
# Preserve the existing required-check name while allowing tests, generated
# SQL verification, and the vulnerability scan to run independently.
backend:
- needs: [changes, backend-tests, backend-agent-tests, sqlc-check, go-vulnerability-scan, macos-runtime, macos-processtree, windows-execenv, script-checks, installer]
+ needs: [changes, backend-tests, backend-agent-tests, backend-darwin-vet, sqlc-check, go-vulnerability-scan, macos-runtime, macos-processtree, windows-execenv, script-checks, installer]
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
@@ -518,7 +555,7 @@ jobs:
- name: Check backend job results
env:
NEEDS_JSON: ${{ toJSON(needs) }}
- JOB_SCOPES: '{"backend-tests":"backend","backend-agent-tests":"backend","sqlc-check":"sqlc","go-vulnerability-scan":"backend","macos-runtime":"full","macos-processtree":"processtree","windows-execenv":"runtime","script-checks":"scripts","installer":"installer"}'
+ JOB_SCOPES: '{"backend-tests":"backend","backend-agent-tests":"backend","backend-darwin-vet":"backend","sqlc-check":"sqlc","go-vulnerability-scan":"backend","macos-runtime":"full","macos-processtree":"processtree","windows-execenv":"runtime","script-checks":"scripts","installer":"installer"}'
run: node scripts/ci-scope.mjs gate
windows-execenv:
@@ -797,15 +834,17 @@ jobs:
# backend job still covers the untagged and Unix arms.
run: go test -race ./pkg/agent -v -run 'TestCursorBackground|TestCaptureCursorBackground' -count=1 -timeout=5m
- # The daily macOS runtime runner, and the only place cursor_background_process_darwin.go
- # is ever built: it signals a process group through XNU's per-process unique
- # identity so a recycled PID cannot redirect a kill, and the two tests pinning
- # that are `//go:build darwin`. Windows keeps its arm as a step in
- # windows-execenv, but macOS has no sibling job to join, and this covers one
- # integration's background shell rather than the shared launch path every
- # agent uses. So the daily full run carries it instead of charging every
- # backend merge for a macOS runner; `full` also means the stress counts below
- # are now the only finalization pass, replacing the single-count merge variant.
+ # The daily macOS runtime runner, and the only place the darwin-only code
+ # runs (backend-darwin-vet only type-checks it). It began with
+ # cursor_background_process_darwin.go, which signals a process group through
+ # XNU's per-process unique identity so a recycled PID cannot redirect a kill,
+ # and the two tests pinning that are `//go:build darwin`. Windows keeps its
+ # arm as a step in windows-execenv, but macOS has no sibling job to join,
+ # and this covers one integration's background shell rather than the shared
+ # launch path every agent uses. So the daily full run carries it instead of
+ # charging every backend merge for a macOS runner; `full` also means the
+ # stress counts below are now the only finalization pass, replacing the
+ # single-count merge variant.
macos-runtime:
needs: changes
if: ${{ needs.changes.outputs.full == 'true' }}
@@ -838,6 +877,23 @@ jobs:
working-directory: server
run: CGO_ENABLED=0 go test ./pkg/agent -run '^TestCaptureCursorBackground(KernelRejectsStaleIdentity|DetachedSession|LateDescendant)$' -count=1
+ # The ubuntu backend job passes these same tests through the Linux
+ # implementations; only a macOS host reaches pi_cleanup_darwin.go,
+ # localenv's lsof/ps/AppKit detection, and the LaunchAgent writer.
+ # localenv's focusApp needs a GUI login session and stays uncovered.
+ # Runs even when a Cursor step above failed, since it shares nothing with
+ # them. -race reuses the objects those steps already built.
+ # -v so a silent skip or a -run pattern that stops matching is visible.
+ - name: Test macOS Pi cleanup, local environments, and autostart
+ if: ${{ !cancelled() }}
+ shell: bash
+ working-directory: server
+ run: |
+ go test -race ./pkg/agent -v -run '^TestPiCleanup' -count=1 -timeout=10m
+ go test -race ./internal/daemon -v -run '^TestPiCleanup' -count=1 -timeout=10m
+ go test -race ./internal/daemon/localenv -v -count=1 -timeout=10m
+ go test -race ./cmd/multica -v -run 'Autostart|LaunchAgent' -count=1 -timeout=10m
+
# MUL-9162: Darwin's kill(-pgid) skips group members that are exiting or
# zombies and returns EPERM instead of ESRCH while the group drains, so
# processtree treats that EPERM as "nothing left to signal". The branch is
diff --git a/server/cmd/multica/cmd_daemon_autostart_darwin_test.go b/server/cmd/multica/cmd_daemon_autostart_darwin_test.go
new file mode 100644
index 0000000000..c0c0c3602a
--- /dev/null
+++ b/server/cmd/multica/cmd_daemon_autostart_darwin_test.go
@@ -0,0 +1,141 @@
+//go:build darwin
+
+package main
+
+import (
+ "errors"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// TestLaunchAgentRoundTrip exercises the real LaunchAgent file mechanism
+// under a throwaway HOME. It calls the platform functions directly (not the
+// writeAutostart seam) so other tests' stubs cannot leak in. PATH is emptied
+// so the best-effort `launchctl enable` cannot record an override for the
+// test label in the machine's real launchd database.
+func TestLaunchAgentRoundTrip(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ t.Setenv("PATH", t.TempDir())
+ agents := filepath.Join(home, "Library", "LaunchAgents")
+ stagingPath := filepath.Join(agents, "ai.multica.daemon.staging.plist")
+
+ spec := autostartSpec{
+ Exe: "/opt/homebrew/bin/multica",
+ Args: autostartArgs("staging"),
+ PathEnv: "/usr/bin:/opt/homebrew/bin",
+ }
+
+ // Nothing registered yet: status reports the location it would use.
+ read, err := platformReadAutostart("staging")
+ if err != nil {
+ t.Fatalf("platformReadAutostart before write = %v", err)
+ }
+ if read.Present || read.Enabled || read.Location != stagingPath {
+ t.Fatalf("read before write = %+v, want absent at %q", read, stagingPath)
+ }
+
+ // First registration creates LaunchAgents and reports a change.
+ state, changed, err := platformWriteAutostart("staging", spec)
+ if err != nil {
+ t.Fatalf("platformWriteAutostart = %v", err)
+ }
+ if !changed || !state.Present || !state.Enabled || !state.Managed {
+ t.Fatalf("first write = %+v changed=%v, want a managed, enabled entry", state, changed)
+ }
+ if state.Mechanism != autostartMechanismLaunchd || state.Location != stagingPath {
+ t.Errorf("first write mechanism/location = %q/%q, want %q/%q", state.Mechanism, state.Location, autostartMechanismLaunchd, stagingPath)
+ }
+ wantCommand := "/opt/homebrew/bin/multica daemon start --foreground --profile staging"
+ if state.Command != wantCommand {
+ t.Errorf("command = %q, want %q", state.Command, wantCommand)
+ }
+
+ // launchd silently ignores a plist it cannot parse, so validate what we
+ // wrote with the system's own parser.
+ if out, err := exec.Command("/usr/bin/plutil", "-lint", stagingPath).CombinedOutput(); err != nil {
+ t.Fatalf("plutil -lint rejected the LaunchAgent: %v\n%s", err, out)
+ }
+
+ // Refreshing with the same spec is a no-op — the property that keeps
+ // every `daemon start` after the first read-only.
+ if _, changed, err := platformWriteAutostart("staging", spec); err != nil || changed {
+ t.Fatalf("identical rewrite: changed=%v err=%v, want a no-op", changed, err)
+ }
+
+ // status reads back exactly what was stored.
+ read, err = platformReadAutostart("staging")
+ if err != nil {
+ t.Fatalf("platformReadAutostart = %v", err)
+ }
+ if !read.Present || !read.Enabled || !read.Managed || read.Command != wantCommand {
+ t.Fatalf("read = %+v, want a managed, enabled entry with command %q", read, wantCommand)
+ }
+
+ // A moved binary rewrites the file (the self-update heal path).
+ moved := spec
+ moved.Exe = "/usr/local/bin/multica"
+ if _, changed, err := platformWriteAutostart("staging", moved); err != nil || !changed {
+ t.Fatalf("path change: changed=%v err=%v, want a rewrite", changed, err)
+ }
+ if read, _ = platformReadAutostart("staging"); !strings.HasPrefix(read.Command, "/usr/local/bin/multica ") {
+ t.Errorf("command after move = %q, want the new executable", read.Command)
+ }
+
+ // A different profile owns a different file — registrations coexist.
+ if _, _, err := platformWriteAutostart("", autostartSpec{Exe: moved.Exe, Args: autostartArgs("")}); err != nil {
+ t.Fatalf("write default profile: %v", err)
+ }
+ if _, changed, err := platformRemoveAutostart("staging"); err != nil || !changed {
+ t.Fatalf("remove staging: changed=%v err=%v, want true", changed, err)
+ }
+ if _, err := os.Stat(stagingPath); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("staging plist after remove: stat err = %v, want not exist", err)
+ }
+ if read, err := platformReadAutostart(""); err != nil || !read.Enabled {
+ t.Fatalf("default profile after removing staging: enabled=%v err=%v, want still enabled", read.Enabled, err)
+ }
+
+ // Removal is idempotent: the second call finds nothing to remove.
+ if _, changed, err := platformRemoveAutostart("staging"); err != nil || changed {
+ t.Fatalf("second remove: changed=%v err=%v, want false/nil", changed, err)
+ }
+}
+
+// TestLaunchAgentLeavesForeignPlistAlone pins the second line of defense: a
+// plist at our path without the Multica marker is the user's own agent, and
+// refresh must neither overwrite it nor report it as ours.
+func TestLaunchAgentLeavesForeignPlistAlone(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ t.Setenv("PATH", t.TempDir())
+ path := filepath.Join(home, "Library", "LaunchAgents", "ai.multica.daemon.plist")
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ foreign := `<?xml version="1.0" encoding="UTF-8"?>
+<plist version="1.0"><dict><key>Label</key><string>ai.multica.daemon</string><key>ProgramArguments</key><array><string>/usr/local/bin/mine</string></array></dict></plist>
+`
+ if err := os.WriteFile(path, []byte(foreign), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ read, err := platformReadAutostart("")
+ if err != nil {
+ t.Fatalf("platformReadAutostart = %v", err)
+ }
+ if !read.Present || read.Managed || read.Command != "/usr/local/bin/mine" {
+ t.Fatalf("read = %+v, want a present, unmanaged entry reporting its own command", read)
+ }
+
+ _, changed, err := platformWriteAutostart("", autostartSpec{Exe: "/opt/homebrew/bin/multica", Args: autostartArgs("")})
+ if !errors.Is(err, errAutostartUnmanaged) || changed {
+ t.Fatalf("write over a foreign plist: changed=%v err=%v, want errAutostartUnmanaged", changed, err)
+ }
+ if got, _ := os.ReadFile(path); string(got) != foreign {
+ t.Fatalf("foreign plist was modified:\n%s", got)
+ }
+}
diff --git a/server/internal/daemon/processtree/run_unix_test.go b/server/internal/daemon/processtree/run_unix_test.go
index f0098da36f..e89e5c4cb6 100644
--- a/server/internal/daemon/processtree/run_unix_test.go
+++ b/server/internal/daemon/processtree/run_unix_test.go
@@ -26,7 +26,10 @@ func TestCombinedOutputKillsDescendantsHoldingOutput(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
started := time.Now()
- _, err := CombinedOutput(ctx, exec.Command(script, marker), 250*time.Millisecond)
+ // Exec'ing the freshly written script directly fails with ETXTBSY when a
+ // parallel test forks while the write descriptor is open (golang/go#22315);
+ // sh only reads it.
+ _, err := CombinedOutput(ctx, exec.Command("/bin/sh", script, marker), 250*time.Millisecond)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("CombinedOutput error = %v, want deadline exceeded", err)
}