← hudzik.info

Dominik Hudzik · Updated · Version 1.1

How I work with AI: a Sway desktop built for Claude Code and OpenCode

This is how I work with AI coding agents every day: one project per workspace, an agent in a terminal on each, and a desktop that tells me which agent is waiting for me. Below are the config and helper scripts I have built up on a laptop (Debian 13, Sway 1.10, one external monitor). Everything is plain bash or Python plus swaymsg and jq. Config and scripts are linked at the bottom. Take what is useful, tell me what you would do differently.

What it is built around

Claude Code is my main tool and the whole setup is built around it; OpenCode (with free models) runs on one workspace next to it. Each workspace is one project folder with Claude Code running inside Neovim (the claudecode.nvim plugin), in a foot terminal. In the shared scripts that launch line is plain nvim; swap in whatever you run. Browsers have fixed jobs:

Because of that, a session is a set of projects, each usually a terminal next to a browser, not a set of apps.

Screens

Made in a clean nested Sway with the config from the repository, so no client data is on them.

Workspace 1: three windows side by side, the fourth opens below (tile-rule). Workspace names come from the project folder.
Workspace 1: three windows side by side, the fourth opens below (tile-rule). Workspace names come from the project folder.
The bar: "2* crm" means an agent on workspace 2 waits for my answer.
The bar: "2* crm" means an agent on workspace 2 waits for my answer.
Alt+Shift+Tab: every window in fuzzel, workspace number first.
Alt+Shift+Tab: every window in fuzzel, workspace number first.

The workflow

I keep one folder per workspace and want my layout back after a login, and I want to see at a glance which agent needs me. These do that:

  1. Mod+1 ... 0 (or the numpad) switches workspace. If the workspace is empty, ws-open starts the apps saved for it (terminal in the right folder, browser, editor), in the saved columns and widths.
  2. Mod+Ctrl+s (sway-save) saves what is open now: folder, app kind, column and row, width in percent. It saves only the workspaces that have windows right now, so closing the ones you do not want before saving is how you pick a session. Ctrl+Alt+F4 saves and powers off.
  3. Login runs ws-open all from sway-startup, so the saved session comes back.
  4. Mod+Ctrl+n (ws-new) lists every workspace number you ever used, with the folder last used and the date (a * marks the open ones). Pick a number, confirm or change the folder, and it opens workspace "N: folder" with an editor in that folder. The history file is never pruned, so a workspace you used months ago is still on the list. Mod+Shift+n (ws-rename) renames the current workspace after the folder open in the focused window.
  5. Mod+Ctrl+z freezes a workspace (ws-freeze): SIGSTOP to the process trees behind the windows on the focused workspace, SIGCONT on the next press. Frozen windows are dimmed. Terminals and processes that also own windows on other workspaces are skipped. RAM stays used but the CPU does not. Good for a heavy browser on a laptop with 8 GB. Read the warning below before you use it.
  6. A * shows which agent waits for me. A workspace is named "3: myproject" while the agent on it works and "3* myproject" when it has stopped and waits for my input. With several agents running I see on the bar where I am needed, without clicking through. The workspace I am looking at never gets a star. How it works:
    • agent-wait on|off [PID] keeps a small state file per agent while it waits and sends Sway a tick.
    • Claude Code: hooks call it: Stop and Notification = waiting, UserPromptSubmit, PostToolUse and SessionEnd = working. cc-wait-hooks-add adds the hooks to ~/.claude/settings.json (backup and diff first).
    • OpenCode: its plugins get no session events in the version I use (2.4), so op-wait-watch, a small systemd user service, reads the event stream of the local OpenCode service (/api/event): execution started = working, succeeded / failed / interrupted or a permission question = waiting. It finds the terminal by the session's folder. I have tested it with opencode run only, not yet with a live TUI session.
    • tile-rule reads the state files and renames the workspaces.
  7. tile-rule decides where a new window goes: the first three windows on a workspace sit side by side, the fourth and later open below. It replaced autotiling, which kept splitting in directions I did not want. It also renames each workspace to "N: folder" after the folder of the session running in it (set SESSION at the top of the script to the process you run, claude by default).

Other things I use every day: Mod+x swaps the focused window with the next one, Mod+Shift+space floats a window at 70% x 70% centred (and back), Mod+e flips horizontal/vertical for the window you are in, Mod+Ctrl+minus picks a window from the scratchpad in fuzzel.

Keys

Mod is the Super key. I override some Sway defaults: Mod+b (was splith) and Mod+v (was splitv) are launchers, Mod+e runs sway-flip instead of layout toggle split, Mod+Shift+space runs sway-float instead of floating toggle, Mod+q closes the focused window and Mod+Shift+q closes the whole workspace, Mod+Shift+c checks the config before it reloads.

Windows

Layout

Workspaces

Switching and launching

System

Scripts

Script What it does
sway-save saves open windows (folder, kind, column, row, width %) to workspaces.map, and every folder ever used to workspaces.history
ws-open starts the saved apps for workspace N (or all, or l to list), only if N is empty
ws-new fuzzel list of every workspace number ever used, then opens it with an editor in a folder
ws-rename renames the focused workspace to "N: folder" from the focused window
sway-startup login: restores the saved session
ws-freeze freezes / wakes the apps behind the focused workspace, terminals skipped
tile-rule three windows side by side, the fourth and later below; names workspaces after their folder, * while an agent waits
agent-wait marks an agent as waiting or working (called by hooks and by op-wait-watch)
cc-wait-hooks-add adds the Claude Code hooks that call agent-wait
op-wait-watch OpenCode: reads the local service's event stream, calls agent-wait (systemd user unit in config/)
qb-here opens a URL in the qutebrowser of the workspace the calling terminal sits on
sway-mru most-recently-used window switch (daemon follows focus events)
sway-windows fuzzel window picker with workspace number first
sp-pick fuzzel picker for scratchpad windows
sway-launcher app launcher ordered by last start, --recent for a curated list
sway-swap, sway-flip, sway-float swap two windows, flip split direction, toggle a centred 70% float
sway-reload validates the config, reloads only if it is valid
sway-poweroff saves the session, then powers off even if the save fails
clip-sync keeps the primary selection and the clipboard the same
sway-outputs external monitor present: laptop panel off; none: on (follows hotplug)
ws-edge, bat-min, wallpaper-daily first/last workspace, battery minutes for waybar, one wallpaper per day from a folder

Script samples

Three short scripts from the repository, comment headers cut. The rest are in bin/.

agent-wait

Marks a Claude Code or OpenCode session as waiting or working. The hooks call it; it walks up the process tree to find the agent and tells tile-rule to put the star on the workspace.

dir=${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/agent-wait
mode=${1:-}
pid=${2:-}
[ -t 0 ] || cat >/dev/null 2>&1   # drain the hook's JSON on stdin
if [ -z "$pid" ]; then
  p=$PPID
  while [ -n "$p" ] && [ "$p" -gt 1 ]; do
    comm=$(cat "/proc/$p/comm" 2>/dev/null) || break
    case $comm in claude|opencode|.opencode) pid=$p; break ;; esac
    p=$(awk '{print $4}' "/proc/$p/stat" 2>/dev/null) || break
  done
fi
[ -n "$pid" ] || exit 0
mkdir -p "$dir" 2>/dev/null
case $mode in
  on) : > "$dir/$pid" ;;
  off) rm -f "$dir/$pid" ;;
  *) exit 0 ;;
esac
# drop files of agents that are gone
for f in "$dir"/*; do
  [ -e "$f" ] || continue
  [ -d "/proc/${f##*/}" ] || rm -f "$f"
done
swaymsg -t send_tick agent-wait >/dev/null 2>&1
exit 0

cc-wait-hooks-add

Adds the Claude Code hooks that call agent-wait, with a backup and a diff of settings.json first.

set -eu
s=$HOME/.claude/settings.json
d=$(date +%F-%H%M%S)
cp -p "$s" "$s.bak-$d"
tmp=$(mktemp)
jq --arg aw "$HOME/.local/bin/agent-wait" '
  def add(ev; mode):
    .hooks[ev] = ((.hooks[ev] // []) | map(select((.hooks[0].command // "") | startswith($aw) | not))
      + [{"hooks": [{"type": "command", "command": ($aw + " " + mode), "timeout": 5}]}]);
  add("Stop"; "on") | add("Notification"; "on")
  | add("UserPromptSubmit"; "off") | add("PostToolUse"; "off") | add("SessionEnd"; "off")' "$s" > "$tmp"
jq empty "$tmp"
diff -u "$s.bak-$d" "$tmp" > "$s.diff-$d" || true
cat "$tmp" > "$s"
rm -f "$tmp"
echo "backup: $s.bak-$d"
echo "diff:   $s.diff-$d"
jq -c '.hooks | keys' "$s"

sway-reload

Checks the config with sway -C and reloads only if it is valid, so a typo never leaves a broken desktop.

cfg=${1:-}
if [ -n "$cfg" ]; then out=$(sway -C -c "$cfg" 2>&1); else out=$(sway -C 2>&1); fi
rc=$?
if [ "$rc" -ne 0 ]; then
  notify-send -u critical "sway-reload" "config invalid, NOT reloaded: $(head -c 300 <<<"$out")" 2>/dev/null
  echo "sway-reload: invalid (rc=$rc): $out" >&2
  exit 1
fi
[ "${DRYRUN:-0}" = 1 ] && { echo "sway-reload: valid (dry run)"; exit 0; }
swaymsg reload >/dev/null && notify-send "sway-reload" "reloaded" 2>/dev/null

Known weak spots (an outside review found these, I have not fixed them all)

Gotchas I hit

Files

Config and scripts are one folder: config/sway-config, config/waybar-config, config/waybar-style.css, config/foot.ini, config/fuzzel.ini, config/mako-config, and bin/ with the scripts above. Theme is Catppuccin Mocha. Repository: https://github.com/hudzik/AI-setup (folder sway/)

Dependencies (Debian package names where I know them): sway, swayidle, swaylock, swaynag (comes with sway), foot, fuzzel, waybar, mako-notifier, jq, python3, psmisc (pstree), procps (pgrep), upower, grim, slurp, wl-clipboard, cliphist, wlsunset, brightnessctl, playerctl, wireplumber (wpctl), network-manager (nmcli, nmtui), libnotify-bin (notify-send), yad, python3-i3ipc (for tile-rule), qutebrowser, thunar, firefox, neovim, policykit-1-gnome, gnome-keyring.

The config has no personal data in it, but before you use it: delete the output lines for ports you do not have (swaymsg -t get_outputs), set your keyboard layout and put your own coordinates in the wlsunset line (the ones there are a placeholder). The polkit path is the Debian one. The scripts are tested on my machine only; read them before you bind them to keys.

Questions or your own setup

Questions about this setup, or do you use AI agents in the terminal in your own way? I would like to hear how. Write to me through the contact form.