Files
magpie/internal/proc/disclaim.go
T
yetone 531b44a9a8 codex: the daemon magpie restarts asks macOS for folders in its own name, not magpie's (reformed9448 on Discord)
The first `codex --yolo` in a terminal had macOS say magpie wanted files
in the Desktop folder. magpie keeps Codex's managed app-server daemon
current (KeepCodexDaemonCurrent restarts it with `codex app-server daemon
restart` when no session is attached). On macOS a process is the TCC
"responsible process" of whoever started it, so the daemon that restart
leaves running answered to magpie. Every codex run in a terminal attaches
to that daemon, which works in the run's folder, so ~/Desktop was asked
for under magpie's name.

proc.CombinedOutputOwn runs a command through posix_spawn with
responsibility_spawnattrs_setdisclaim (looked up with dlsym, as it has no
header), so the command and what it leaves running answer for themselves.
Only stdio is passed on, stdin is /dev/null, signals are reset, and the
context's end kills it as CommandContext would. restartCodexDaemon uses it.
Elsewhere, and in a build without cgo (the CLI), it is cmd.CombinedOutput.

Run (darwin/arm64, Go 1.26.8, at f51ab771f + this):
- go test -count=1 ./internal/proc/: ok (TestCombinedOutputOwnAnswersForItself,
  TestCombinedOutputOwnRuns, TestCombinedOutputOwnContext)
- go test -count=1 ./internal/provider/: ok with
  -skip TestUnreadLoginsWriteErrorsKeepCredentials; that test crashes with
  "close of synctest channel from outside bubble" in filememo.Forget on
  main too, which is #3632 (open, a patch is under review there)
- gofmt -l clean; go vet for darwin, linux and windows on proc and provider;
  CGO_ENABLED=0 -tags nogui build, the GUI build, linux nogui and windows
  builds
Broken by hand:
- the disclaim call turned off: TestCombinedOutputOwnAnswersForItself red
  ("the sleep answers to 2201, this process to 2201")
- restartCodexDaemon back on cmd.CombinedOutput:
  TestRestartedCodexDaemonIsNotMagpies red ("the daemon answers to 2201,
  magpie to 2201")
Not tried: a real TCC prompt (the tests check the responsible pid macOS
reports, which is what the prompt names; it can't be shown in a sandbox).
The CLI build, without cgo, still starts the daemon as its own child; it
is the GUI that keeps the daemon current for most users.
2026-10-27 12:28:06 +08:00

31 lines
1.3 KiB
Go

package proc
import (
"context"
"os/exec"
)
// On macOS, a privacy prompt (Desktop, Documents, Downloads, a removable
// volume…) names the process responsible for the one asking, and a child
// is the responsibility of whoever started it, down through every process
// it starts in turn. So a long-lived process magpie starts for an agent —
// Codex's app-server daemon, which every codex run in a terminal attaches
// to and works in the folder it was run in — asks in magpie's name: the
// first `codex` in ~/Desktop had macOS say magpie wants the Desktop
// folder (reformed9448 on Discord). CombinedOutputOwn starts such a
// command as its own responsible process instead, as a terminal's would
// be asked about under the terminal's name rather than magpie's.
// CombinedOutputOwn runs cmd, as made by Command or CommandContext with
// ctx, to its end and returns its output, stdout and stderr together, as
// cmd.CombinedOutput does; stdin is the null device. On macOS (built with
// cgo) the command, and what it starts, answer for themselves to macOS's
// privacy prompts rather than to magpie. Elsewhere it is
// cmd.CombinedOutput.
func CombinedOutputOwn(ctx context.Context, cmd *exec.Cmd) ([]byte, error) {
if cmd.Err != nil {
return nil, cmd.Err
}
return combinedOutputOwn(ctx, cmd)
}