CLI: Add persistent shell mode to CLI with workspace management #3803
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Persistent Shell Mode Implementation
Context
Implemented persistent shell mode for the Kilocode CLI, allowing users to:
Implementation
Architecture Overview
Core Components:
ShellSession Service (
cli/src/services/shell/session.ts)child_process.spawn__KILO_DONE__:exitCode:cwd) to reliably detect command completion and track directory changesShell State Management (
cli/src/state/atoms/shell.ts)shellSessionAtom: Holds the persistent ShellSession instancecurrentShellDirectoryAtom: Tracks the shell's current working directoryinitializeShellSessionAtom: Action atom to initialize the shell session when entering shell modedisposeShellSessionAtom: Action atom to clean up the shell session on exitapplyShellWorkspaceAtom: Action atom to propagate shell's CWD back to the CLI workspaceexecuteShellCommandAtom: Refactored to use persistent session instead ofchild_process.execWorkspace Propagation (
cli/src/cli.ts)handleWorkspaceChange(): Method that handles workspace changes from shell modeUI Integration (
cli/src/ui/UI.tsx)workspacePathAtomfor changesonWorkspaceChangecallback when the shell's directory differs from the CLI's workspaceKey Design Decisions
__KILO_DONE__:exitCode:cwd) to reliably detect completionspawn()rather thanexec()to maintain a long-lived shell, preserving environment and working directory between commandsprocess.cwd()and the workspace atom simultaneouslyanytypesTradeoffs
as unknown astype casting to access private telemetry fields (pragmatic solution given singleton pattern)How to Test
Start Kilo Code CLI:
cd /your/project kilocodeEnter Shell Mode:
Shift+!or type!when the input is emptyTest Directory Navigation:
cd /tmpand press Enterpwdand verify you're in/tmpcd /varand press Enterpwdagain—should show/varTest Environment Variables:
export MY_VAR=helloand press Enterecho $MY_VARand verify it printshelloExit Shell Mode and Verify Workspace Update:
Shift+!again to exit shell mode📁 Workspace changed to: /var\nReinitializing AI context.../varworkspaceTest Command History:
Test Error Handling:
false)cd /nonexistentGet in Touch
If you have questions about this implementation, feel free to respond to this PR.