Skip to content

Commit 7c996b4

Browse files
donjor-agent[bot]donjor
authored andcommitted
feat(terminal): open a plain shell alongside managed zmux (#1677)
Fork-Domain: zmux-estate Fork-Tier: qol Co-authored-by: donjor <[email protected]>
1 parent 49cad64 commit 7c996b4

8 files changed

Lines changed: 208 additions & 29 deletions

File tree

‎apps/server/src/terminal/Manager.fork.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ it.layer(
107107
});
108108
}),
109109
);
110+
it.effect("keeps a requested plain shell through reattach, restart, and checkout follow", () =>
111+
Effect.gen(function* () {
112+
const processRunner = resolvedZmuxProcessRunner();
113+
const { manager, ptyAdapter, baseDir, join } = yield* createManager(5, {
114+
shellResolver: () => "/bin/bash",
115+
terminalSessionMode: "zmux",
116+
subprocessInspector: () =>
117+
Effect.succeed({ hasRunningSubprocess: false, childCommand: null, processIds: [] }),
118+
}).pipe(Effect.provideService(ProcessRunner.ProcessRunner, processRunner.service));
119+
const input = openInput({ attachmentId: "plain-viewer", plainShellFork: true });
120+
yield* manager.open(input);
121+
const release = yield* manager.attachStream(input, () => Effect.void);
122+
yield* Effect.addFinalizer(() => Effect.sync(release));
123+
yield* manager.restart({ ...input, cols: 100, rows: 24 });
124+
const destination = join(baseDir, "destination");
125+
yield* (yield* FileSystem.FileSystem).makeDirectory(destination);
126+
yield* manager.open({ ...input, cwd: destination, worktreePath: destination });
127+
expect(processRunner.inputs).toEqual([]);
128+
expect(ptyAdapter.spawnInputs).toHaveLength(3);
129+
expect(ptyAdapter.spawnInputs.every((spawn) => spawn.shell === "/bin/bash")).toBe(true);
130+
expect(ptyAdapter.spawnInputs[2]?.cwd).toBe(destination);
131+
yield* manager.open(
132+
openInput({ attachmentId: "managed-viewer", worktreePath: process.cwd() }),
133+
);
134+
expect(processRunner.inputs.length).toBeGreaterThan(0);
135+
expect(ptyAdapter.spawnInputs[3]?.shell).toBe("zmux");
136+
}),
137+
);
110138
it.effect("uses fresh activity when deciding whether an unmanaged terminal can retarget", () =>
111139
Effect.gen(function* () {
112140
let running = false;

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ interface TerminalSessionState {
411411
managedAttachment: ManagedAttachmentLifecycle;
412412
managedSuspendFiber: Fiber.Fiber<void, never> | null;
413413
runtimeEnv: Record<string, string> | null;
414+
plainShellFork: boolean; // fork-hook: zmux-estate/plain-shell-state
414415
}
415416

416417
interface PersistHistoryRequest {
@@ -2771,6 +2772,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
27712772
session: TerminalSessionState,
27722773
input: TerminalStartInput,
27732774
): Effect.fn.Return<PreparedManagedRetarget | null, TerminalError> {
2775+
if (session.plainShellFork) return null; // fork-hook: zmux-estate/plain-shell-retarget
27742776
if ((yield* effectiveTerminalSessionMode) !== "zmux") return null; // fork-hook: zmux-estate/settle-hook-shell-retarget-mode
27752777

27762778
const worktreePath = input.worktreePath ?? null;
@@ -3152,7 +3154,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
31523154
}
31533155
}
31543156
}
3155-
const mode = yield* effectiveTerminalSessionMode; // fork-hook: zmux-estate/settle-hook-shell-open-mode
3157+
const mode = session.plainShellFork ? "shell" : yield* effectiveTerminalSessionMode; // fork-hook: zmux-estate/settle-hook-shell-open-mode
31563158
let shellCandidates = plainShellCandidates;
31573159
let spawnEnv = terminalEnv;
31583160
let managedTarget: string | null = null;
@@ -4001,6 +4003,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
40014003
},
40024004
managedSuspendFiber: null,
40034005
runtimeEnv: current.runtimeEnv,
4006+
plainShellFork: false, // fork-hook: zmux-estate/plain-shell-retained
40044007
};
40054008
const sessions = new Map(state.sessions);
40064009
sessions.set(key, session);
@@ -4064,6 +4067,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
40644067
managedAttachment: INITIAL_MANAGED_ATTACHMENT_LIFECYCLE,
40654068
managedSuspendFiber: null,
40664069
runtimeEnv: normalizedRuntimeEnv(input.env),
4070+
plainShellFork: input.plainShellFork === true, // fork-hook: zmux-estate/plain-shell-open-state
40674071
};
40684072

40694073
const createdSession = session;
@@ -4697,6 +4701,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
46974701
managedAttachment: INITIAL_MANAGED_ATTACHMENT_LIFECYCLE,
46984702
managedSuspendFiber: null,
46994703
runtimeEnv: normalizedRuntimeEnv(input.env),
4704+
plainShellFork: input.plainShellFork === true, // fork-hook: zmux-estate/plain-shell-restart-state
47004705
};
47014706
const createdSession = session;
47024707
yield* modifyManagerState((state) => {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Plus } from "lucide-react";
2+
import { Button } from "./ui/button";
3+
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "./ui/menu";
4+
5+
export function TerminalNewMenu({
6+
disabled,
7+
label,
8+
onNewTerminal,
9+
}: {
10+
disabled: boolean;
11+
label: string;
12+
onNewTerminal: (plainShell?: boolean) => void;
13+
}) {
14+
return (
15+
<Menu>
16+
<MenuTrigger
17+
render={<Button variant="ghost" size="icon-xs" />}
18+
disabled={disabled}
19+
aria-label={label}
20+
>
21+
<Plus className="size-3.25" />
22+
</MenuTrigger>
23+
<MenuPopup align="end">
24+
<MenuItem onClick={() => onNewTerminal()}>{label}</MenuItem>
25+
<MenuItem onClick={() => onNewTerminal(true)}>New plain shell</MenuItem>
26+
</MenuPopup>
27+
</Menu>
28+
);
29+
}

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
type TerminalSessionState,
1212
} from "@t3tools/client-runtime/state/terminal";
1313
import {
14-
Plus,
1514
Pin,
1615
Square,
1716
SquareSplitHorizontal,
@@ -49,6 +48,8 @@ import {
4948
} from "react";
5049
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
5150
import { Button } from "~/components/ui/button";
51+
import { TerminalNewMenu } from "./TerminalNewMenu.fork"; // fork-hook: zmux-estate/plain-shell-menu-import
52+
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; // fork-hook: zmux-estate/plain-shell-choice-import
5253
import { PanelTabCloseButton } from "~/components/ui/panel-tab-close-button";
5354
import { stackedThreadToast, toastManager } from "~/components/ui/toast";
5455
import { readTextFromClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
@@ -619,6 +620,11 @@ function TerminalSessionViewport({
619620
isTerminalUrl(text) || readEnvironmentScope(environmentId, AuthOrchestrationOperateScope),
620621
);
621622
const attachmentId = useMemo(() => terminalAttachmentId(terminalId), [terminalId]);
623+
const plainShellFork = useTerminalUiStateStore(
624+
(state) =>
625+
selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef)
626+
.plainShellByTerminalId?.[terminalId] === true,
627+
); // fork-hook: zmux-estate/plain-shell-choice
622628
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
623629
const openInPreferredEditor = useOpenInPreferredEditor(
624630
environmentId,
@@ -666,6 +672,7 @@ function TerminalSessionViewport({
666672
cwd,
667673
...(worktreePath !== undefined ? { worktreePath } : {}),
668674
...(runtimeEnv ? { env: runtimeEnv } : {}),
675+
...(plainShellFork ? { plainShellFork } : {}), // fork-hook: zmux-estate/plain-shell-retarget-input
669676
},
670677
}),
671678
);
@@ -706,6 +713,7 @@ function TerminalSessionViewport({
706713
cwd,
707714
...(worktreePath !== undefined ? { worktreePath } : {}),
708715
...(runtimeEnv ? { env: runtimeEnv } : {}),
716+
...(plainShellFork ? { plainShellFork } : {}), // fork-hook: zmux-estate/plain-shell-attach-input
709717
...(providerInstanceId ? { providerInstanceId } : {}),
710718
});
711719
// Attaching starts with the viewport, while the WASM surface is still loading;
@@ -1313,7 +1321,7 @@ interface ThreadTerminalDrawerProps {
13131321
activeTerminalGroupId: string;
13141322
onSplitTerminal: () => void;
13151323
onSplitTerminalVertical: () => void;
1316-
onNewTerminal: () => void;
1324+
onNewTerminal: (plainShell?: boolean) => void; // fork-hook: zmux-estate/plain-shell-drawer-action
13171325
splitShortcutLabel?: string | undefined;
13181326
splitVerticalShortcutLabel?: string | undefined;
13191327
newShortcutLabel?: string | undefined;
@@ -1814,9 +1822,9 @@ export default function ThreadTerminalDrawer({
18141822
if (!canOperateTerminal || hasReachedSplitLimit) return;
18151823
onSplitTerminalVertical();
18161824
};
1817-
const onNewTerminalAction = () => {
1825+
const onNewTerminalAction = (plainShell = false) => { // fork-hook: zmux-estate/plain-shell-create-action
18181826
if (!canOperateTerminal) return;
1819-
onNewTerminal();
1827+
onNewTerminal(plainShell);
18201828
};
18211829
const confirmCloseTerminal = (terminalId: string) => {
18221830
if (!canOperateTerminal) return;
@@ -1949,11 +1957,21 @@ export default function ThreadTerminalDrawer({
19491957
<Button
19501958
size="xs"
19511959
variant="outline"
1952-
onClick={onNewTerminalAction}
1960+
onClick={() => onNewTerminalAction()} // fork-hook: zmux-estate/plain-shell-empty-default
19531961
disabled={!canOperateTerminal}
19541962
>
19551963
{newTerminalActionLabel}
19561964
</Button>
1965+
{/* fork-hook: zmux-estate/plain-shell-empty-menu */}
1966+
<Button
1967+
size="xs"
1968+
variant="outline"
1969+
disabled={!canOperateTerminal}
1970+
onClick={() => onNewTerminalAction(true)}
1971+
>
1972+
New plain shell
1973+
</Button>
1974+
{/* fork-hook-end */}
19571975
</div>
19581976
</aside>
19591977
);
@@ -2023,14 +2041,13 @@ export default function ThreadTerminalDrawer({
20232041
onModeChange={onCheckoutModeChange}
20242042
/>
20252043
<div className="h-4 w-px bg-border/80" />
2026-
<TerminalActionButton
2044+
{/* fork-hook: zmux-estate/plain-shell-floating-menu */}
2045+
<TerminalNewMenu
20272046
disabled={!canOperateTerminal}
2028-
className="p-1 text-foreground/90 transition-colors hover:bg-accent"
2029-
onClick={onNewTerminalAction}
2047+
onNewTerminal={onNewTerminalAction}
20302048
label={newTerminalActionLabel}
2031-
>
2032-
<Plus className="size-3.25" />
2033-
</TerminalActionButton>
2049+
/>
2050+
{/* fork-hook-end */}
20342051
<div className="h-4 w-px bg-border/80" />
20352052
<TerminalActionButton
20362053
disabled={!canOperateTerminal}
@@ -2106,14 +2123,13 @@ export default function ThreadTerminalDrawer({
21062123
disabled={checkoutModeChangeDisabled}
21072124
onModeChange={onCheckoutModeChange}
21082125
/>
2109-
<TerminalActionButton
2126+
{/* fork-hook: zmux-estate/plain-shell-sidebar-menu */}
2127+
<TerminalNewMenu
21102128
disabled={!canOperateTerminal}
2111-
className="inline-flex h-full items-center border-l border-border/70 px-1 text-foreground/90 transition-colors hover:bg-accent/70"
2112-
onClick={onNewTerminalAction}
2129+
onNewTerminal={onNewTerminalAction}
21132130
label={newTerminalActionLabel}
2114-
>
2115-
<Plus className="size-3.25" />
2116-
</TerminalActionButton>
2131+
/>
2132+
{/* fork-hook-end */}
21172133
<TerminalActionButton
21182134
disabled={!canOperateTerminal}
21192135
className="inline-flex h-full items-center border-l border-border/70 px-1 text-foreground/90 transition-colors hover:bg-accent/70"

‎apps/web/src/terminalUiStateStore.fork.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,56 @@ describe("terminal checkout mode", () => {
3333
).toEqual({});
3434
});
3535
});
36+
37+
const thread = scopeThreadRef("environment-a" as never, ThreadId.make("thread-plain"));
38+
const read = () =>
39+
selectThreadTerminalUiState(
40+
useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
41+
thread,
42+
);
43+
44+
beforeEach(() => {
45+
useTerminalUiStateStore.persist.clearStorage();
46+
useTerminalUiStateStore.setState({
47+
terminalUiStateByThreadKey: {},
48+
suppressedTerminalIdsByThreadKey: {},
49+
});
50+
});
51+
52+
it("retains plain-shell choice through layout reconciliation and drops closed choices", () => {
53+
const store = useTerminalUiStateStore.getState();
54+
store.newTerminal(thread, "plain", true);
55+
store.newTerminal(thread, "other-plain", true);
56+
store.newTerminal(thread, "managed");
57+
store.reconcileTerminalIds(thread, ["managed", "plain", "other-plain"]);
58+
expect(read().plainShellByTerminalId).toEqual({ plain: true, "other-plain": true });
59+
store.setTerminalOpen(thread, false);
60+
store.setTerminalOpen(thread, true);
61+
expect(read().plainShellByTerminalId).toEqual({ plain: true, "other-plain": true });
62+
store.closeTerminal(thread, "plain");
63+
expect(read().plainShellByTerminalId).toEqual({ "other-plain": true });
64+
store.newTerminal(thread, "plain");
65+
expect(read().plainShellByTerminalId).toEqual({ "other-plain": true });
66+
store.closeTerminal(thread, "other-plain");
67+
expect(read().plainShellByTerminalId ?? {}).toEqual({});
68+
});
69+
70+
it("persists a panel shell choice without adding a drawer terminal or opening it", async () => {
71+
const store = useTerminalUiStateStore.getState();
72+
store.newTerminal(thread, "drawer", true);
73+
store.setTerminalOpen(thread, false);
74+
store.setTerminalPlainShellFork(thread, "panel");
75+
expect(read().terminalIds).toEqual(["drawer"]);
76+
expect(read().terminalOpen).toBe(false);
77+
store.closeTerminal(thread, "drawer");
78+
expect(read().plainShellByTerminalId).toEqual({ panel: true });
79+
const { storage, name } = useTerminalUiStateStore.persist.getOptions();
80+
const saved = await storage!.getItem(name!);
81+
useTerminalUiStateStore.setState({ terminalUiStateByThreadKey: {} });
82+
await storage!.setItem(name!, saved!);
83+
await useTerminalUiStateStore.persist.rehydrate();
84+
expect(read().plainShellByTerminalId).toEqual({ panel: true });
85+
expect(read().terminalIds).toEqual([]);
86+
store.closeTerminal(thread, "panel");
87+
expect(read().plainShellByTerminalId ?? {}).toEqual({});
88+
});

0 commit comments

Comments
 (0)