-
Notifications
You must be signed in to change notification settings - Fork 2
feat: configurable workspaces #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jonathanlab
wants to merge
1
commit into
main
Choose a base branch
from
feat/configurable-workspaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,856
−1,533
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { type IpcMainInvokeEvent, ipcMain } from "electron"; | ||
| import { logger } from "./logger"; | ||
|
|
||
| type IpcHandler<T extends unknown[], R> = ( | ||
| event: IpcMainInvokeEvent, | ||
| ...args: T | ||
| ) => Promise<R> | R; | ||
|
|
||
| interface HandleOptions { | ||
| scope?: string; | ||
| rethrow?: boolean; | ||
| fallback?: unknown; | ||
| } | ||
|
|
||
| export function createIpcHandler(scope: string) { | ||
| const log = logger.scope(scope); | ||
|
|
||
| return function handle<T extends unknown[], R>( | ||
| channel: string, | ||
| handler: IpcHandler<T, R>, | ||
| options: HandleOptions = {}, | ||
| ): void { | ||
| const { rethrow = true, fallback } = options; | ||
|
|
||
| ipcMain.handle(channel, async (event: IpcMainInvokeEvent, ...args: T) => { | ||
| try { | ||
| return await handler(event, ...args); | ||
| } catch (error) { | ||
| log.error(`Failed to handle ${channel}:`, error); | ||
| if (rethrow) { | ||
| throw error; | ||
| } | ||
| return fallback as R; | ||
| } | ||
| }); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import type { WebContents } from "electron"; | ||
| import * as pty from "node-pty"; | ||
| import { logger } from "./logger"; | ||
|
|
||
| const log = logger.scope("shell"); | ||
|
|
||
| export interface ShellSession { | ||
| pty: pty.IPty; | ||
| webContents: WebContents; | ||
| exitPromise: Promise<{ exitCode: number }>; | ||
| command?: string; | ||
| } | ||
|
|
||
| function getDefaultShell(): string { | ||
| const platform = os.platform(); | ||
| if (platform === "win32") { | ||
| return process.env.COMSPEC || "cmd.exe"; | ||
| } | ||
| return process.env.SHELL || "/bin/bash"; | ||
| } | ||
|
|
||
| function buildShellEnv(): Record<string, string> { | ||
| const env = { ...process.env } as Record<string, string>; | ||
|
|
||
| if (os.platform() === "darwin" && !process.env.LC_ALL) { | ||
| const locale = process.env.LC_CTYPE || "en_US.UTF-8"; | ||
| env.LANG = locale; | ||
| env.LC_ALL = locale; | ||
| env.LC_MESSAGES = locale; | ||
| env.LC_NUMERIC = locale; | ||
| env.LC_COLLATE = locale; | ||
| env.LC_MONETARY = locale; | ||
| } | ||
|
|
||
| env.TERM_PROGRAM = "Array"; | ||
| env.COLORTERM = "truecolor"; | ||
| env.FORCE_COLOR = "3"; | ||
|
|
||
| return env; | ||
| } | ||
|
|
||
| export interface CreateSessionOptions { | ||
| sessionId: string; | ||
| webContents: WebContents; | ||
| cwd?: string; | ||
| initialCommand?: string; | ||
| } | ||
|
|
||
| class ShellManagerImpl { | ||
| private sessions = new Map<string, ShellSession>(); | ||
|
|
||
| createSession(options: CreateSessionOptions): ShellSession { | ||
| const { sessionId, webContents, cwd, initialCommand } = options; | ||
|
|
||
| const existing = this.sessions.get(sessionId); | ||
| if (existing) { | ||
| return existing; | ||
| } | ||
|
|
||
| const shell = getDefaultShell(); | ||
| const homeDir = os.homedir(); | ||
| let workingDir = cwd || homeDir; | ||
|
|
||
| if (!fs.existsSync(workingDir)) { | ||
| log.warn( | ||
| `Shell session ${sessionId}: cwd "${workingDir}" does not exist, falling back to home`, | ||
| ); | ||
| workingDir = homeDir; | ||
| } | ||
|
|
||
| log.info( | ||
| `Creating shell session ${sessionId}: shell=${shell}, cwd=${workingDir}`, | ||
| ); | ||
|
|
||
| const env = buildShellEnv(); | ||
| const ptyProcess = pty.spawn(shell, ["-l"], { | ||
| name: "xterm-256color", | ||
| cols: 80, | ||
| rows: 24, | ||
| cwd: workingDir, | ||
| env, | ||
| encoding: null, | ||
| }); | ||
|
|
||
| let resolveExit: (result: { exitCode: number }) => void; | ||
| const exitPromise = new Promise<{ exitCode: number }>((resolve) => { | ||
| resolveExit = resolve; | ||
| }); | ||
|
|
||
| ptyProcess.onData((data: string) => { | ||
| webContents.send(`shell:data:${sessionId}`, data); | ||
| }); | ||
|
|
||
| ptyProcess.onExit(({ exitCode }) => { | ||
| log.info(`Shell session ${sessionId} exited with code ${exitCode}`); | ||
| webContents.send(`shell:exit:${sessionId}`, { exitCode }); | ||
| this.sessions.delete(sessionId); | ||
| resolveExit({ exitCode }); | ||
| }); | ||
|
|
||
| if (initialCommand) { | ||
| setTimeout(() => { | ||
| ptyProcess.write(`${initialCommand}\n`); | ||
| }, 100); | ||
| } | ||
|
|
||
| const session: ShellSession = { | ||
| pty: ptyProcess, | ||
| webContents, | ||
| exitPromise, | ||
| command: initialCommand, | ||
| }; | ||
|
|
||
| this.sessions.set(sessionId, session); | ||
| return session; | ||
| } | ||
|
|
||
| getSession(sessionId: string): ShellSession | undefined { | ||
| return this.sessions.get(sessionId); | ||
| } | ||
|
|
||
| hasSession(sessionId: string): boolean { | ||
| return this.sessions.has(sessionId); | ||
| } | ||
|
|
||
| write(sessionId: string, data: string): void { | ||
| const session = this.sessions.get(sessionId); | ||
| if (!session) { | ||
| throw new Error(`Shell session ${sessionId} not found`); | ||
| } | ||
| session.pty.write(data); | ||
| } | ||
|
|
||
| resize(sessionId: string, cols: number, rows: number): void { | ||
| const session = this.sessions.get(sessionId); | ||
| if (!session) { | ||
| throw new Error(`Shell session ${sessionId} not found`); | ||
| } | ||
| session.pty.resize(cols, rows); | ||
| } | ||
|
|
||
| destroy(sessionId: string): void { | ||
| const session = this.sessions.get(sessionId); | ||
| if (!session) { | ||
| return; | ||
| } | ||
| session.pty.kill(); | ||
| this.sessions.delete(sessionId); | ||
| } | ||
|
|
||
| getProcess(sessionId: string): string | null { | ||
| const session = this.sessions.get(sessionId); | ||
| return session?.pty.process ?? null; | ||
| } | ||
|
|
||
| getSessionsByPrefix(prefix: string): string[] { | ||
| const result: string[] = []; | ||
| for (const sessionId of this.sessions.keys()) { | ||
| if (sessionId.startsWith(prefix)) { | ||
| result.push(sessionId); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| destroyByPrefix(prefix: string): void { | ||
| for (const sessionId of this.sessions.keys()) { | ||
| if (sessionId.startsWith(prefix)) { | ||
| this.destroy(sessionId); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const shellManager = new ShellManagerImpl(); | ||
Oops, something went wrong.
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.
Check failure
Code scanning / CodeQL
Insecure randomness High
Copilot Autofix
AI 3 days ago
To fix this issue, replace all uses of
Math.random()for selecting random elements in the worktree name generation logic (WorktreeManager.randomElement) with a cryptographically secure alternative. In Node.js, this means usingcrypto.randomIntor deriving randomness fromcrypto.randomBytes.Specifically, in
packages/agent/src/worktree-manager.ts, update therandomElementselection function to utilizecrypto.randomIntto generate a random index for the array, rather than using a value derived fromMath.random(). This update applies wherever random unique names are generated for worktrees.cryptomodule.randomElementmethod (not fully shown above, but implied from usage) to usecrypto.randomIntto select the random array index.Only the file
packages/agent/src/worktree-manager.tsrequires modification, as the taint originates here.