You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 688565f
Browse filesBrowse the repository at this point in the historyBrowse files
**Purpose**: a plain terminal (sidebar "+" → Terminal, and the panel shell of
10
-
[panel-terminal](panel-terminal.md)) is the user's own shell, with one
11
-
addition: a `claude` function that prints *"To start a Claude session, use the
12
-
+ button in the sidebar."* and returns 1, so that typing `claude` there does not
13
-
start a session Switchboard does not track.
14
-
15
-
Only `claude` is shimmed. Typing `codex` (or another CLI) in a plain terminal
10
+
[panel-terminal](panel-terminal.md)) is the user's own shell plus a `claude`
11
+
function that prints *"To start a Claude session, use the + button in the
12
+
sidebar."* and returns 1, so typing `claude` does not start a session
13
+
Switchboard does not track. Only `claude` is shimmed: `codex` (or another CLI)
16
14
runs the real program, which Switchboard does not track as a session it
17
15
launched.
18
16
19
-
The shim must be defined without being typed into the shell. A line written
20
-
into the PTY is echoed after the prompt until something clears it, and the
21
-
interactive shell records it in its history like any command the user typed.
17
+
The shim must be defined without being typed into the shell: a typed line is
18
+
echoed after the prompt until something clears it, and the interactive shell
19
+
records it in its history like any command the user typed.
22
20
23
21
## Key files
24
22
25
23
| File | Role |
26
24
|---|---|
27
-
|`src/main/terminals/plain-terminal-shell.js`|`plainTerminalLaunch()`: args, env and the fallback line per shell. `ensureInitFiles()`: writes the generated startup files. The files' content. |
28
-
|`main.js` (`open-terminal`, `isPlainTerminal` branch) |Builds the base env (`cleanPtyEnv` plus the terminal variables), asks `plainTerminalLaunch()`, spawns what it returns, types`launch.typed`300 ms after the spawn when it is not null. |
29
-
|`src/main/terminals/shell-profiles.js`|`shellArgs()`: the args every other spawn uses. With no command: `-l -i` for bash-like shells (bash, zsh, `sh`), fish and nushell; WSL gets`-- bash -l -i`. With a command (e.g. `headlessClaudeLaunch`): fish and nushell get `-l -c <cmd>`. |
30
-
|`src/main/shared/clean-env.js`| The base env: drops the Claude Code session markers inherited from a parent session and, when one was found, its`GIT_EDITOR=true` residue (see [ipc-bridge](ipc-bridge.md), "Clean child environment"). |
31
-
|`test/plain-terminal-shell.test.js`| Args and env per shell, the generated files run by real bash and zsh, the `main.js` wiring. |
25
+
|`src/main/terminals/plain-terminal-shell.js`|`plainTerminalLaunch()`: args, env and fallback line per shell. `ensureInitFiles()`: writes the generated startup files (content defined here). |
26
+
|`main.js` (`open-terminal`, `isPlainTerminal` branch) |Env = `cleanPtyEnv` plus the terminal variables and `CLAUDECODE=1`; spawns what `plainTerminalLaunch()` returns; when`launch.typed`is not null, schedules the typed fallback below. |
27
+
|`src/main/terminals/shell-profiles.js`|`shellArgs()`, the args every other spawn uses. No command: `-l -i` for bash, zsh, `sh`, fish, nushell; WSL `-- bash -l -i`. With a command (e.g. `headlessClaudeLaunch`): fish and nushell get `-l -c <cmd>`. |
28
+
|`src/main/shared/clean-env.js`| The base env: drops inherited Claude Code session markers and, when one was found, their`GIT_EDITOR=true` residue ([ipc-bridge](ipc-bridge.md), "Clean child environment"). |
29
+
|`test/plain-terminal-shell.test.js`| Args and env per shell, the generated files run by real bash and zsh, the `main.js` wiring. Real-shell tests skip on Windows, and elsewhere when the shell is not on `PATH`; `SWITCHBOARD_TEST_BASH` / `SWITCHBOARD_TEST_ZSH` name one off Windows. |
32
30
33
31
## Where the generated files live
34
32
35
-
`<data dir>/shell-init/` — the directory of `switchboard.db`, so
36
-
`SWITCHBOARD_DATA_DIR` isolates it with everything else. `ensureInitFiles()`
37
-
writes them on the first plain terminal of a process, so a new version's
38
-
content replaces the old one at the next launch. Each later plain terminal only
39
-
checks that the files still exist, and writes them again if any is missing: a
40
-
deleted `--rcfile` would otherwise start bash silently with none of the user's
41
-
startup files and a bare `PATH`, and zsh with none of theirs. Nothing under
42
-
`$HOME` is written or changed. When the write fails, bash and zsh get the typed
43
-
fallback below and a warning is logged.
44
-
45
33
```
46
-
shell-init/
34
+
<data dir>/shell-init/ next to switchboard.db
47
35
bashrc
48
36
zsh/.zshenv zsh/.zprofile zsh/.zshrc zsh/.zlogin
49
37
```
50
38
51
-
`zsh/` can also hold a `.zcompdump` that zsh writes itself: Ubuntu's
52
-
`/etc/zsh/zshrc` runs `compinit` while `ZDOTDIR` still points at the generated
53
-
directory, and `compinit` keeps its dump in `$ZDOTDIR`. The `compinit` of the
54
-
next plain zsh terminal reuses it; `ensureInitFiles()` neither writes nor removes it.
39
+
`SWITCHBOARD_DATA_DIR` therefore isolates them. `ensureInitFiles()` writes them
40
+
on a process's first plain terminal, so a new version's content lands at the
41
+
next launch; later terminals only check that they exist and rewrite all if any
42
+
is missing — a deleted `--rcfile` would start bash silently with none of the
43
+
user's startup files and a bare `PATH`, and zsh with none of theirs. No user
44
+
startup file is written or changed; `shell-init/` itself is under the data
45
+
dir: `~/.switchboard` for a packaged launch, `~/.switchboard-dev` unpackaged,
46
+
or `SWITCHBOARD_DATA_DIR` when set. On a write failure a warning is logged,
47
+
bash and zsh get the typed fallback, and the next terminal retries.
48
+
49
+
zsh may add its own `zsh/.zcompdump` (Ubuntu's `/etc/zsh/zshrc` runs
50
+
`compinit` while `ZDOTDIR` points there, and `compinit` keeps its dump in
51
+
`$ZDOTDIR`); the next plain zsh reuses it, and `ensureInitFiles()` neither
52
+
writes nor removes it.
55
53
56
54
## Mechanism per shell
57
55
58
56
| Shell | How the shim gets in | Typed? |
59
57
|---|---|---|
60
58
| bash (Linux, macOS) |`bash --rcfile <shell-init>/bashrc -i`| no |
| fish |`--init-command 'function claude; …; end'` appended to `-l -i`| no |
60
+
| fish (any platform) |`--init-command 'function claude; …; end'` appended to its `shellArgs()` (`-l -i`; none for a Windows `fish.exe`, which `shellArgs()` does not recognise)| no |
63
61
| PowerShell, cmd, nushell | none: the shim is POSIX syntax | no |
64
-
| sh, dash, ksh, other POSIX shells; WSL; bash on Windows (Git Bash, MSYS2) | the line is typed, **led by a space**| yes |
62
+
|every other shell (sh, dash, ksh, tcsh, csh); WSL; on Windows every shell not above (Git Bash, MSYS2, zsh) | the line is typed, **led by a space**| yes |
65
63
66
-
No shell is given `ENV` or `BASH_ENV`. Both name a *file* to source; neither
67
-
can carry code. `BASH_ENV` is read by every non-interactive bash, so it would
68
-
also reach every script the user runs from that terminal.
64
+
The shim is never injected by setting `ENV` or `BASH_ENV` (values inherited
65
+
from Switchboard's environment pass through unchanged): both name a *file*, not
66
+
code, and every non-interactive bash reads `BASH_ENV`, so it would reach every
67
+
script run from the terminal.
69
68
70
69
### bash: `--rcfile`, and the login startup it replaces
71
70
72
-
bash ignores `--rcfile` in a login shell: `bash --login --rcfile f -i` reads
73
-
`/etc/profile` and the first of `~/.bash_profile`, `~/.bash_login`,
74
-
`~/.profile`, and never reads `f`. The plain terminal is therefore started as a
75
-
non-login interactive shell whose rcfile performs the login startup itself, in
76
-
the order bash documents for a login shell:
77
-
78
-
1.`/etc/profile`, if it exists;
79
-
2. the first that exists of `~/.bash_profile`, `~/.bash_login`, `~/.profile`;
80
-
3. the shim, then `export -f claude` so child bash processes inherit it.
81
-
82
-
The user gets the files a login shell would have read, and only those. A home
83
-
with a `~/.bashrc` but none of the three login files does not get `~/.bashrc`
84
-
read, exactly as under the previous `bash -l -i`; on Debian and Ubuntu the
85
-
default `~/.profile` sources `~/.bashrc`.
86
-
87
-
Differences from a real login shell, all consequences of the shell not being
88
-
one:
89
-
90
-
-`shopt -q login_shell` is false, and `logout` refuses (use `exit`). A user
91
-
`~/.bashrc` that branches on `login_shell` takes its non-login branch (not
92
-
verified on a real configuration).
93
-
-`~/.bash_logout` does not run on exit.
94
-
- A bash built with `SYS_BASHRC` (Debian, Ubuntu) reads `/etc/bash.bashrc`
95
-
before the rcfile, and `/etc/profile` sources it again: it runs twice.
96
-
- On Fedora and RHEL, `/etc/bashrc` sources `/etc/profile.d/*.sh` again when
97
-
`! shopt -q login_shell`, so `/etc/profile.d` probably runs twice there (not
98
-
verified).
71
+
bash ignores `--rcfile` in a login shell, so the terminal is a non-login
72
+
interactive shell whose rcfile does the login startup in bash's documented
73
+
order: `/etc/profile` if it exists; the first existing of `~/.bash_profile`,
74
+
`~/.bash_login`, `~/.profile`; then the shim and `export -f claude`, so child
75
+
bash processes inherit it. The user gets what a login shell reads, and only
76
+
that: `~/.bashrc` is read only if one of those sources it (Debian and Ubuntu's
77
+
default `~/.profile` does).
78
+
79
+
Consequences of not being a login shell:
80
+
81
+
-`shopt -q login_shell` is false and `logout` refuses (use `exit`); a user
82
+
`~/.bashrc` branching on it takes the non-login branch (not verified).
83
+
-`~/.bash_logout` does not run.
84
+
- With `SYS_BASHRC` (Debian, Ubuntu), `/etc/bash.bashrc` runs twice: before
85
+
the rcfile and from `/etc/profile`.
86
+
- On Fedora and RHEL, `/etc/bashrc` re-sources `/etc/profile.d/*.sh` when
87
+
`! shopt -q login_shell`, so it probably runs twice (not verified).
99
88
100
89
### zsh: a generated `ZDOTDIR`
101
90
102
-
zsh reads `$ZDOTDIR/.zshenv`, then for a login shell `.zprofile`, for an
103
-
interactive one `.zshrc`, then for a login shell `.zlogin`; each lookup uses the
104
-
value `ZDOTDIR` has *at that moment*. Each generated file:
91
+
zsh reads `.zshenv`, then `.zprofile` (login), `.zshrc` (interactive),
92
+
`.zlogin` (login), each from `ZDOTDIR`*as it is at that moment*. Each
93
+
generated file:
105
94
106
-
1. gives `ZDOTDIR` back its user value (unset when the user had none, so a
95
+
1. gives `ZDOTDIR` back its user value, unset when there is none (so a
107
96
`~/.zshenv` written as `ZDOTDIR=${ZDOTDIR:-$HOME/.config/zsh}` still takes
108
97
its own default);
109
-
2. sources the user's file of the same name from `${ZDOTDIR:-$HOME}`, at top
110
-
level (never inside a function, where the user's `typeset` would become
111
-
local);
112
-
3. records the user value again, since the user's `.zshenv` is where
113
-
`ZDOTDIR` is conventionally set;
114
-
4. points `ZDOTDIR` back at the generated directory, so zsh reads the next
115
-
generated file.
116
-
117
-
`.zshrc` defines the shim after the user's `.zshrc`. `.zlogin`, the last file a
118
-
login shell reads, restores the user's `ZDOTDIR` for good and unsets its own `_sb_*` variables, so child shells, `.zlogout` and
119
-
anything the user runs see their own configuration. A `ZDOTDIR` present in the
120
-
environment Switchboard was started with is handed over as
121
-
`SWITCHBOARD_USER_ZDOTDIR` and unset by `.zshenv`.
98
+
2. sources the user's file of the same name from `${ZDOTDIR:-$HOME}` at top
99
+
level (inside a function the user's `typeset` would become local);
100
+
3. records the user value again (the user's `.zshenv` conventionally sets it);
101
+
4. except `.zlogin`, points `ZDOTDIR` back at the generated directory, so zsh
102
+
reads the next generated file.
103
+
104
+
`.zshrc` defines the shim after the user's `.zshrc`. `.zlogin`, read last,
105
+
restores the user's `ZDOTDIR` for good and unsets its `_sb_*` variables, so
106
+
child shells, `.zlogout` and programs see the user's configuration. A
107
+
`ZDOTDIR` in Switchboard's environment is handed over as
108
+
`SWITCHBOARD_USER_ZDOTDIR` (an inherited one is dropped first) and unset by
109
+
`.zshenv`.
122
110
123
111
Limits:
124
112
125
-
- zsh is always started as a login shell (`-l -i`). A non-login zsh would never
126
-
read `.zlogin`, and `ZDOTDIR` would stay on the generated directory.
127
-
128
-
- A `/etc/zshenv` (or `/etc/zsh/zshenv`) that assigns `ZDOTDIR`
129
-
unconditionally runs before the generated `.zshenv`; zsh then never reads the
130
-
generated files, and the terminal has no shim (nothing is typed either).
131
-
- A user `.zshenv` that sets `NO_RCS` stops zsh before `.zshrc`: no shim, and
132
-
`ZDOTDIR` stays on the generated directory for that shell.
133
-
- zsh has no `export -f`: a child bash started from a zsh plain terminal runs
134
-
the real `claude`, as before.
113
+
- zsh is always started as a login shell (`-l -i`): a non-login zsh never
114
+
reads `.zlogin`, and `ZDOTDIR` would stay on the generated directory.
115
+
- A `/etc/zshenv` (or `/etc/zsh/zshenv`) assigning `ZDOTDIR` unconditionally
116
+
runs before the generated `.zshenv`, so zsh never reads the generated files:
117
+
no shim, nothing typed either.
118
+
- A user `.zshenv` setting `NO_RCS` stops zsh before `.zshrc`: no shim, and
119
+
`ZDOTDIR` stays on the generated directory.
120
+
- zsh has no `export -f`: a child bash runs the real `claude`.
135
121
136
122
### Everything else: the typed fallback
137
123
138
-
The line is typed 300 ms after the spawn, as
139
-
` claude() { … }; export -f claude 2>/dev/null; clear` with a leading space.
140
-
bash with `HISTCONTROL` containing `ignorespace` or `ignoreboth` (Ubuntu's
141
-
default, WSL Ubuntu included) and zsh with `HIST_IGNORE_SPACE` leave a line
142
-
that starts with a space out of the history. Without those settings it is
143
-
recorded. The line is still drawn until `clear` runs.
144
-
145
-
Windows bash gets the fallback rather than `--rcfile` because the generated
146
-
file's path is a Windows path, and how Git Bash and MSYS2 resolve it in
147
-
`--rcfile` has not been measured; a path bash cannot open would leave the user
148
-
with none of their startup files. WSL gets it because the file lives on the
149
-
Windows side of a `wsl.exe` boundary.
124
+
` claude() { … }; export -f claude 2>/dev/null; clear`, leading space
125
+
included, is sent in one write attempt scheduled 300 ms after the spawn:
126
+
skipped if the PTY is disposed by then, write errors ignored. bash with
127
+
`HISTCONTROL` containing `ignorespace` or `ignoreboth` (Ubuntu's default, WSL
128
+
Ubuntu included) and zsh with `HIST_IGNORE_SPACE` leave a space-led line out of the history; without
129
+
those settings it is recorded. The line is drawn until `clear` runs.
130
+
131
+
Windows bash is not given `--rcfile` because the generated file's path is a
132
+
Windows path, how Git Bash and MSYS2 resolve it there is unmeasured, and an
133
+
unopenable rcfile would leave the user with none of their startup files. WSL
134
+
is not, because the file lives on the Windows side of the `wsl.exe` boundary.
0 commit comments