Repository navigation
fix(lint): reduce cognitive complexity in buildClaudeCommand and launchTmuxSpawn #595
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,55 +210,30 @@ function appendNativeTeamFlags( | |
| if (nt.permissionMode) parts.push('--permission-mode', escapeShellArg(nt.permissionMode)); | ||
| } | ||
|
|
||
| export function buildClaudeCommand(params: SpawnParams): LaunchCommand { | ||
| preflightCheck('claude'); | ||
|
|
||
| const parts: string[] = ['claude', '--dangerously-skip-permissions']; | ||
| const env: Record<string, string> = {}; | ||
|
|
||
| // Always set GENIE_AGENT_NAME and GENIE_TEAM, even for non-native spawns | ||
| if (params.role) { | ||
| env.GENIE_AGENT_NAME = params.role; | ||
| } | ||
| if (params.team) { | ||
| env.GENIE_TEAM = params.team; | ||
| } | ||
|
|
||
| if (params.nativeTeam?.enabled) { | ||
| appendNativeTeamFlags(parts, env, params.nativeTeam, params); | ||
| } | ||
|
|
||
| if (params.resume) { | ||
| parts.push('--resume', escapeShellArg(params.resume)); | ||
| } else if (params.sessionId) { | ||
| parts.push('--session-id', escapeShellArg(params.sessionId)); | ||
| } | ||
|
|
||
| if (params.role) parts.push('--agent', escapeShellArg(params.role)); | ||
|
|
||
| if (params.model) parts.push('--model', escapeShellArg(params.model)); | ||
|
|
||
| /** | ||
| * Resolve system prompt flags for the Claude command. | ||
| * | ||
| * When both an inline systemPrompt and a systemPromptFile exist, merges | ||
| * them into a single temp file. Also merges any --append-system-prompt-file | ||
| * found in extraArgs (consuming it so it isn't duplicated later). | ||
| */ | ||
| function appendSystemPromptFlags(parts: string[], params: SpawnParams): void { | ||
| if (params.systemPrompt) { | ||
| // Write built-in prompt to temp file — avoids shell escaping of complex content | ||
| const { mkdirSync, writeFileSync, readFileSync } = require('node:fs'); | ||
| const { join } = require('node:path'); | ||
| const dir = '/tmp/genie-prompts'; | ||
| mkdirSync(dir, { recursive: true }); | ||
| const ts = Date.now().toString(36); | ||
| const promptFile = join(dir, `${params.role || 'agent'}-${ts}.md`); | ||
|
|
||
| // If there is also a systemPromptFile (user agent), merge both | ||
| let content = params.systemPrompt; | ||
| if (params.systemPromptFile) { | ||
| content = `${readFileSync(params.systemPromptFile, 'utf-8')}\n\n${content}`; | ||
| } | ||
|
|
||
| // If extraArgs has --append-system-prompt-file, merge that too | ||
| if (params.extraArgs) { | ||
| const fileIdx = params.extraArgs.indexOf('--append-system-prompt-file'); | ||
| if (fileIdx !== -1 && params.extraArgs[fileIdx + 1]) { | ||
| content = `${content}\n\n${readFileSync(params.extraArgs[fileIdx + 1], 'utf-8')}`; | ||
| // Remove the extra arg since we merged it | ||
| params.extraArgs.splice(fileIdx, 2); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function mutates the |
||
| } | ||
| } | ||
|
|
@@ -270,12 +245,36 @@ export function buildClaudeCommand(params: SpawnParams): LaunchCommand { | |
| const flag = params.promptMode === 'system' ? '--system-prompt-file' : '--append-system-prompt-file'; | ||
| parts.push(flag, escapeShellArg(params.systemPromptFile)); | ||
| } | ||
| } | ||
|
|
||
| export function buildClaudeCommand(params: SpawnParams): LaunchCommand { | ||
| preflightCheck('claude'); | ||
|
|
||
| const parts: string[] = ['claude', '--dangerously-skip-permissions']; | ||
| const env: Record<string, string> = {}; | ||
|
|
||
| if (params.role) env.GENIE_AGENT_NAME = params.role; | ||
| if (params.team) env.GENIE_TEAM = params.team; | ||
|
|
||
| if (params.nativeTeam?.enabled) { | ||
| appendNativeTeamFlags(parts, env, params.nativeTeam, params); | ||
| } | ||
|
|
||
| if (params.resume) { | ||
| parts.push('--resume', escapeShellArg(params.resume)); | ||
| } else if (params.sessionId) { | ||
| parts.push('--session-id', escapeShellArg(params.sessionId)); | ||
| } | ||
|
|
||
| if (params.role) parts.push('--agent', escapeShellArg(params.role)); | ||
| if (params.model) parts.push('--model', escapeShellArg(params.model)); | ||
|
|
||
| appendSystemPromptFlags(parts, params); | ||
|
|
||
| if (params.extraArgs) { | ||
| for (const arg of params.extraArgs) parts.push(escapeShellArg(arg)); | ||
| } | ||
|
|
||
| // Positional [prompt] arg must be last — becomes the first user message | ||
| if (params.initialPrompt) { | ||
| parts.push(escapeShellArg(params.initialPrompt)); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function creates temporary files in
/tmp/genie-promptsbut doesn't seem to have a mechanism to clean them up. Over time, this could lead to a large number of files accumulating in the/tmpdirectory.Consider implementing a cleanup strategy. This could be done, for example, by registering a cleanup function on process exit, or by using a library like
tmpwhich can manage temporary files and directories automatically.