Skip to content

feat(keys): run shell commands from shortcuts with spawn-sh - #298

Merged
qeude merged 3 commits into
mainfrom
feat/spawn-command
Nov 15, 2026
Merged

qeude merged 3 commits into
mainfrom
feat/spawn-command

Conversation

@qeude

@qeude qeude commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

A [keys] binding now runs a shell command line, like Niri's spawn-sh, so users can script their own commands on top of the defi CLI. Asked for on Reddit to port a Niri "swap columns between two monitors" script.

[keys]
"mod-backspace" = "spawn-sh ~/.config/defi/swap.sh"
 Daemon.handle(rawCommand)
   parseCommand            # "spawn-sh <rest of line>" -> .spawnShell(String)
   …toggleOverview / openSettings
+  .spawnShell -> /bin/sh -c <line>, cwd $HOME, not awaited
+                 PATH = <folder of defi>:/opt/homebrew/bin:/usr/local/bin:<inherited>
   overview layout routing / reducer   # reducer ignores .spawnShell
  • Shortcuts only: the socket refuses spawn-sh, because any local process can reach it and the shell inherits Defi's Accessibility and Screen Recording grants (raised by Macroscope).
  • Config-file only by design: no dedicated Settings entry. Settings' existing "Custom command…" field keeps such a binding as written.
  • Cheatsheet shows it as "Run ".
  • CONFIGURATION.md: command row plus a short Scripts section, including that scripts share Defi's Accessibility and Screen Recording permissions.

Evidence

Real key press, in the defi-e2e VM (staged build of 871027a, scratch config "ctrl-alt-home" = "spawn-sh …", HID key event):

$ defi spawn-sh 'touch /tmp/defi-ipc-proof.txt'
spawn-sh runs only from a shortcut in config.toml      # exit 1, file never created
--- ctrl-alt-home pressed (captured=1 processed=1); the script wrote:
/Users/admin/defi-spawn/Defi.app/Contents/MacOS:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
/Users/admin/defi-spawn/Defi.app/Contents/MacOS/defi
{"focusedMonitorID":1,"monitors":[{"acti

Earlier, before the socket refusal, proved with make verify, parser/PATH tests, and the installed build (commit 69628b2, same designated requirement, one defi-daemon):

$ defi spawn-sh "pwd; echo \$PATH | cut -d: -f1-3; command -v defi; defi list-workspaces --json"   # output to a file
/Users/quentin
/Users/quentin/Applications/Defi.app/Contents/MacOS:/opt/homebrew/bin:/usr/local/bin
/Users/quentin/Applications/Defi.app/Contents/MacOS/defi
{"focusedMonitorID":1,"monitors":[…
$ defi spawn-sh            -> missingArgument("command"), exit 1
overview=open; defi spawn-sh … -> ran while the Overview was open
no child or zombie process left under defi-daemon
  • Before: spawn-sh → unknownCommand("spawn-sh").
    After: ModelTests parses " spawn-sh defi focus-column left | tee \"a b\" " to .spawnShell("defi focus-column left | tee \"a b\""); SpawnShellTests checks the PATH order and deduplication.

SpawnShellTests.socketClientsCannotSpawnShells covers the refusal. Not covered: the two-display lane (nothing display-specific here).

Merge Danger

Door: two-way

New opt-in command; nothing runs unless a user binds it.

Blast Radius: small

Commands run with Defi's TCC grants, documented, and only from the user's own shortcuts. Config with spawn-sh fails to load on older builds with an unknown-command error.

Reviewed with review-loop: 1 round with claude-opus-5-5 (medium). Verdict: approved

Note

Add spawn-sh command to run shell scripts from shortcuts

  • Shortcuts with spawn-sh <command line> launch the script via /bin/sh -c in the home directory, without waiting for it to finish. Parsed in CommandParser.swift and launched by the new spawnShell helper in DaemonCommands.swift
  • The child process PATH puts the Defi executable directory first, then Homebrew locations, then deduplicated inherited entries, built by the new spawnSearchPath helper
  • IPC socket clients cannot run spawn-sh; the new refusedOverIPC check in DaemonCommands.swift returns a failure response before dispatch
  • Reducer treats .spawnShell as a no-op for layout and native-fullscreen classification; the cheatsheet shows a "Run" label
  • Risk: spawn-sh via IPC now returns a failure response instead of dispatching — check the early refusedOverIPC check in the IPC event handler

Macroscope summarized 871027a.

A spawn-sh binding runs the rest of its line with /bin/sh -c, as in Niri,
so users can script their own commands with the defi CLI. The PATH gains the
folder holding defi and the Homebrew prefixes, since launchd starts the daemon
with a bare one.
@github-actions github-actions Bot added the macroscope-review Request a Macroscope review label Nov 15, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Approvability

Verdict: Approved at 871027a

Macroscope's review found this PR approvable — Adds explicitly configured shell-command shortcuts, with documented permission inheritance and no default binding. IPC requests are blocked, and head-revision testing demonstrates both shortcut execution and socket refusal.

You can add or adjust custom eligibility rules. Learn more.

Any local process can reach Defi's socket, and a spawned shell inherits
Defi's Accessibility and Screen Recording grants, so only shortcuts may run
spawn-sh.
@qeude

qeude commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Fixed in 871027a: the socket now refuses spawn-sh, so only shortcuts in the user's own config.toml can run it, and CONFIGURATION.md says why. A real key press in the defi-e2e VM still spawns the script, while defi spawn-sh … over IPC exits 1 and spawns nothing.

@qeude
qeude merged commit de0c007 into main Nov 15, 2026
4 checks passed
@qeude
qeude deleted the feat/spawn-command branch November 15, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

macroscope-review Request a Macroscope review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant