From 72d2afb13b290eb0ecc73dd768456d8dc98357a1 Mon Sep 17 00:00:00 2001 From: yuzhou <219110351+ZYZ666-RGB@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:49:55 +0000 Subject: [PATCH] fix(terminal): reject terminal_execute on non-active sessions terminal_close only marked a session closed. terminal_execute still ran the command and wrote the status back to active. Refuse any session whose status is not active before the command is spawned. Addresses #4730 Co-authored-by: yuzhou <1991039819@qq.com> --- .../terminal-execute-closed-session.test.ts | 121 ++++++++++++++++++ .../cli/src/mcp-tools/terminal-tools.ts | 10 ++ 2 files changed, 131 insertions(+) create mode 100644 v3/@claude-flow/cli/__tests__/terminal-execute-closed-session.test.ts diff --git a/v3/@claude-flow/cli/__tests__/terminal-execute-closed-session.test.ts b/v3/@claude-flow/cli/__tests__/terminal-execute-closed-session.test.ts new file mode 100644 index 0000000000..346057b7d1 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/terminal-execute-closed-session.test.ts @@ -0,0 +1,121 @@ +/** + * #4730: after terminal_create + terminal_close, terminal_execute still ran + * the command and reported success, then set the session back to "active". + * A session whose status is not "active" must error and must not run. + */ +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { terminalTools } from '../src/mcp-tools/terminal-tools.js'; + +const createTool = terminalTools.find(t => t.name === 'terminal_create')!; +const closeTool = terminalTools.find(t => t.name === 'terminal_close')!; +const executeTool = terminalTools.find(t => t.name === 'terminal_execute')!; + +type ToolResult = { + success: boolean; + error?: string; + sessionId?: string; + output?: string; +}; + +interface StoredSession { + id: string; + status: string; + history: unknown[]; +} + +function readStore(dir: string): { sessions: Record } { + return JSON.parse(readFileSync(join(dir, '.claude-flow', 'terminals', 'store.json'), 'utf-8')); +} + +describe('terminal_execute on a non-active session (#4730)', () => { + let dir: string; + let prevCwd: string | undefined; + let prevEncrypt: string | undefined; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'ruflo-terminal-execute-closed-')); + prevCwd = process.env.CLAUDE_FLOW_CWD; + prevEncrypt = process.env.CLAUDE_FLOW_ENCRYPT_AT_REST; + process.env.CLAUDE_FLOW_CWD = dir; + delete process.env.CLAUDE_FLOW_ENCRYPT_AT_REST; + }); + + afterEach(() => { + if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD; + else process.env.CLAUDE_FLOW_CWD = prevCwd; + if (prevEncrypt === undefined) delete process.env.CLAUDE_FLOW_ENCRYPT_AT_REST; + else process.env.CLAUDE_FLOW_ENCRYPT_AT_REST = prevEncrypt; + rmSync(dir, { recursive: true, force: true }); + }); + + it('errors after terminal_close and does not run the command', async () => { + const created = await createTool.handler({ name: 'closed-session' }) as ToolResult; + expect(created.success).toBe(true); + const sessionId = created.sessionId!; + + const closed = await closeTool.handler({ sessionId }) as ToolResult; + expect(closed.success).toBe(true); + + const marker = join(dir, 'ran.txt'); + const result = await executeTool.handler({ + sessionId, + command: `echo still-running > "${marker}"`, + }) as ToolResult; + + expect(result.success).toBe(false); + expect(result.error).toBe('Session is not active (closed)'); + expect(result.output).toBeUndefined(); + expect(existsSync(marker)).toBe(false); + + const store = readStore(dir); + expect(store.sessions[sessionId].status).toBe('closed'); + expect(store.sessions[sessionId].history).toEqual([]); + expect(Object.keys(store.sessions)).toEqual([sessionId]); + }); + + it('rejects an idle session the same way', async () => { + const created = await createTool.handler({ name: 'idle-session' }) as ToolResult; + const sessionId = created.sessionId!; + const store = readStore(dir); + store.sessions[sessionId].status = 'idle'; + writeFileSync(join(dir, '.claude-flow', 'terminals', 'store.json'), JSON.stringify(store)); + + const marker = join(dir, 'ran.txt'); + const result = await executeTool.handler({ + sessionId, + command: `echo still-running > "${marker}"`, + }) as ToolResult; + + expect(result.success).toBe(false); + expect(result.error).toBe('Session is not active (idle)'); + expect(existsSync(marker)).toBe(false); + expect(readStore(dir).sessions[sessionId].status).toBe('idle'); + }); + + it('still executes in an active session', async () => { + const created = await createTool.handler({ name: 'live-session' }) as ToolResult; + const result = await executeTool.handler({ + sessionId: created.sessionId, + command: 'echo still-running', + }) as ToolResult; + + expect(result.success).toBe(true); + expect(result.sessionId).toBe(created.sessionId); + expect(result.output?.trim()).toBe('still-running'); + expect(readStore(dir).sessions[created.sessionId!].status).toBe('active'); + }); + + it('still creates a default session when no sessionId is given and none is active', async () => { + const created = await createTool.handler({}) as ToolResult; + const closed = await closeTool.handler({ sessionId: created.sessionId }) as ToolResult; + expect(closed.success).toBe(true); + + const result = await executeTool.handler({ command: 'echo hi' }) as ToolResult; + expect(result.success).toBe(true); + expect(result.sessionId).not.toBe(created.sessionId); + expect(result.output?.trim()).toBe('hi'); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/terminal-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/terminal-tools.ts index c997e87feb..d667589aa2 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/terminal-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/terminal-tools.ts @@ -173,6 +173,16 @@ export const terminalTools: MCPTool[] = [ // Find or create default session let session = sessionId ? store.sessions[sessionId] : Object.values(store.sessions).find(s => s.status === 'active'); + // #4730: terminal_close only flips status to "closed". Executing anyway + // ran the command and then wrote status back to "active", so a closed + // session kept accepting commands. Reject anything that is not active + // before the process is spawned and before that status write. When no + // sessionId was given, the lookup above already selected an active + // session (or we create one below). + if (session && session.status !== 'active') { + return { success: false, error: `Session is not active (${session.status})` }; + } + if (!session) { // Create default session const id = `term-${Date.now()}-${randomBytes(4).toString('hex')}`;