Skip to content

Commit 688565f

Browse files
authored
docs(plain-terminal): cut the context down to current behaviour and invariants (#1355) (#1391)
Trim plain-terminal.md context (#1355); review findings fixed.
1 parent 9df983e commit 688565f

2 files changed

Lines changed: 86 additions & 101 deletions

File tree

‎.ai/contexts/plain-terminal.md‎

Lines changed: 85 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,143 +7,128 @@ covers:
77
# Context: plain-terminal
88

99
**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)
1614
runs the real program, which Switchboard does not track as a session it
1715
launched.
1816

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.
2220

2321
## Key files
2422

2523
| File | Role |
2624
|---|---|
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. |
3230

3331
## Where the generated files live
3432

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-
4533
```
46-
shell-init/
34+
<data dir>/shell-init/ next to switchboard.db
4735
bashrc
4836
zsh/.zshenv zsh/.zprofile zsh/.zshrc zsh/.zlogin
4937
```
5038

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.
5553

5654
## Mechanism per shell
5755

5856
| Shell | How the shim gets in | Typed? |
5957
|---|---|---|
6058
| bash (Linux, macOS) | `bash --rcfile <shell-init>/bashrc -i` | no |
6159
| zsh (Linux, macOS) | `ZDOTDIR=<shell-init>/zsh`, args unchanged (`-l -i`) | no |
62-
| 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 |
6361
| 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 |
6563

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.
6968

7069
### bash: `--rcfile`, and the login startup it replaces
7170

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).
9988

10089
### zsh: a generated `ZDOTDIR`
10190

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:
10594

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
10796
`~/.zshenv` written as `ZDOTDIR=${ZDOTDIR:-$HOME/.config/zsh}` still takes
10897
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`.
122110

123111
Limits:
124112

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`.
135121

136122
### Everything else: the typed fallback
137123

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.

‎docs/decisions/0003-trigger-delivery-per-tab.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ composer/readiness/result gates. Do not add descriptor fallback to these tabs.
167167
Today's public contract includes plain terminals (`docs/automation.md:196`).
168168
The POSIX/fish shims block `claude` with a hint and return 1
169169
(`src/main/terminals/plain-terminal-shell.js:10`, `src/main/terminals/plain-terminal-shell.js:14`); no shim runs in
170-
PowerShell/cmd (`.ai/contexts/plain-terminal.md:52`). The synthetic nested-CLI scenario remains
170+
PowerShell/cmd (`.ai/contexts/plain-terminal.md` ("Mechanism per shell")). The synthetic nested-CLI scenario remains
171171
required, for a bypassed shim or an unshimmed shell. Unattached remote socket
172172
delivery retains its
173173
existing route and contract (`trigger-watcher.js:1294`,

0 commit comments

Comments
 (0)