diff --git a/src/adapters/telegram/commands/menu.ts b/src/adapters/telegram/commands/menu.ts index f92e1bc..fcb282e 100644 --- a/src/adapters/telegram/commands/menu.ts +++ b/src/adapters/telegram/commands/menu.ts @@ -1,8 +1,6 @@ import type { Context } from "grammy"; import { InlineKeyboard } from "grammy"; -import type { OpenACPCore } from "../../../core/index.js"; import type { AgentCommand } from "../../../core/index.js"; -import { escapeHtml } from "../formatting.js"; import type { CommandsAssistantContext } from "../types.js"; export function buildMenuKeyboard(): InlineKeyboard { @@ -30,21 +28,6 @@ export async function handleMenu(ctx: Context): Promise { }); } -export async function handleAgents(ctx: Context, core: OpenACPCore): Promise { - const agents = core.agentManager.getAvailableAgents(); - const defaultAgent = core.configManager.get().defaultAgent; - const lines = agents.map( - (a) => - `• ${escapeHtml(a.name)}${a.name === defaultAgent ? " (default)" : ""}\n` + - ` ${escapeHtml(a.command)} ${a.args.map((arg) => escapeHtml(arg)).join(" ")}`, - ); - const text = - lines.length > 0 - ? `Available Agents:\n\n${lines.join("\n")}` - : `Available Agents:\n\nNo agents configured.`; - await ctx.reply(text, { parse_mode: "HTML" }); -} - export async function handleHelp(ctx: Context): Promise { await ctx.reply( `📖 OpenACP Help\n\n` + diff --git a/src/core/__tests__/permission-gate.test.ts b/src/core/__tests__/permission-gate.test.ts index c95639a..942e132 100644 --- a/src/core/__tests__/permission-gate.test.ts +++ b/src/core/__tests__/permission-gate.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from 'vitest' +import { describe, it, expect, vi } from 'vitest' import { PermissionGate } from '../permission-gate.js' import type { PermissionRequest } from '../types.js' @@ -74,4 +74,36 @@ describe('PermissionGate', () => { const result = await p2 expect(result).toBe('deny') }) + + it('rejects with timeout when no response received', async () => { + vi.useFakeTimers() + const gate = new PermissionGate(5000) // 5s timeout + const promise = gate.setPending(mockRequest) + + expect(gate.isPending).toBe(true) + + // Advance past timeout + vi.advanceTimersByTime(5000) + + await expect(promise).rejects.toThrow('Permission request timed out') + expect(gate.isPending).toBe(false) + + vi.useRealTimers() + }) + + it('clears timeout when resolved before expiry', async () => { + vi.useFakeTimers() + const gate = new PermissionGate(5000) + const promise = gate.setPending(mockRequest) + + gate.resolve('allow') + const result = await promise + expect(result).toBe('allow') + + // Advancing past timeout should not cause issues + vi.advanceTimersByTime(10000) + expect(gate.isPending).toBe(false) + + vi.useRealTimers() + }) }) diff --git a/src/core/agent-instance.ts b/src/core/agent-instance.ts index 68da950..cdd1c10 100644 --- a/src/core/agent-instance.ts +++ b/src/core/agent-instance.ts @@ -1,4 +1,4 @@ -import { spawn, execSync, type ChildProcess } from "node:child_process"; +import { spawn, execFileSync, type ChildProcess } from "node:child_process"; import { Transform } from "node:stream"; import fs from "node:fs"; import path from "node:path"; @@ -78,7 +78,7 @@ function resolveAgentCommand(cmd: string): { command: string; args: string[] } { // 3. Try resolving from PATH using which try { - const fullPath = execSync(`which ${cmd}`, { encoding: "utf-8" }).trim(); + const fullPath = execFileSync("which", [cmd], { encoding: "utf-8" }).trim(); if (fullPath) { const content = fs.readFileSync(fullPath, "utf-8"); if (content.startsWith("#!/usr/bin/env node")) { diff --git a/src/core/permission-gate.ts b/src/core/permission-gate.ts index 7518e64..cf8935b 100644 --- a/src/core/permission-gate.ts +++ b/src/core/permission-gate.ts @@ -1,5 +1,7 @@ import type { PermissionRequest } from "./types.js"; +const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes + /** * Encapsulates pending permission state with a typed Promise API. */ @@ -8,20 +10,32 @@ export class PermissionGate { private resolveFn?: (optionId: string) => void; private rejectFn?: (reason: Error) => void; private settled = false; + private timeoutTimer?: ReturnType; + private timeoutMs: number; + + constructor(timeoutMs?: number) { + this.timeoutMs = timeoutMs ?? DEFAULT_TIMEOUT_MS; + } setPending(request: PermissionRequest): Promise { this.request = request; this.settled = false; + this.clearTimeout(); return new Promise((resolve, reject) => { this.resolveFn = resolve; this.rejectFn = reject; + + this.timeoutTimer = setTimeout(() => { + this.reject("Permission request timed out (no response received)"); + }, this.timeoutMs); }); } resolve(optionId: string): void { if (this.settled || !this.resolveFn) return; this.settled = true; + this.clearTimeout(); this.resolveFn(optionId); this.cleanup(); } @@ -29,6 +43,7 @@ export class PermissionGate { reject(reason?: string): void { if (this.settled || !this.rejectFn) return; this.settled = true; + this.clearTimeout(); this.rejectFn(new Error(reason ?? "Permission rejected")); this.cleanup(); } @@ -46,6 +61,13 @@ export class PermissionGate { return this.request?.id; } + private clearTimeout(): void { + if (this.timeoutTimer) { + clearTimeout(this.timeoutTimer); + this.timeoutTimer = undefined; + } + } + private cleanup(): void { this.request = undefined; this.resolveFn = undefined;