Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat: add approve_command tool and refactor shell permission flow to use explicit user confirmation
Salman Qureshi69bf47de8f6d
diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.tsindex 7e24c6fe..965a8ae6 100644--- a/src/capabilities/permissions.ts+++ b/src/capabilities/permissions.ts@@ -91,6 +91,8 @@ const DEFAULT_MANIFEST: PermissionsManifest = {'df *','du *','uname *',+ 'curl *',+ 'wget *',],needsApproval: ['npm publish *',@@ -127,6 +129,7 @@ export class PermissionManager {private askHandler?: (prompt: string) => Promise<string>;private autoApproveAll = false;private elevatedCommands: Set<string> = new Set();+ private pendingApprovals: Set<string> = new Set();constructor() {this.cwd = process.cwd();@@ -166,6 +169,14 @@ export class PermissionManager {return this.elevatedCommands.has('run_command');}+ addPendingApproval(baseCommand: string): void {+ this.pendingApprovals.add(baseCommand);+ }++ clearPendingApprovals(): void {+ this.pendingApprovals.clear();+ }+private load(): PermissionsManifest {if (existsSync(PERMISSIONS_FILE)) {try {@@ -247,6 +258,12 @@ export class PermissionManager {const trimmed = command.trim();+ const baseCmd = trimmed.split(/\s+/)[0];+ if (this.pendingApprovals.has(baseCmd)) {+ logger.info({ cmd: trimmed }, 'Shell command auto-approved (pending approval)');+ return { allowed: true, needsApproval: false };+ }+for (const pattern of shell.blocked) {if (this.matchPattern(trimmed, pattern)) {return { allowed: false, reason: `Blocked command: matches "${pattern}"`, needsApproval: false };diff --git a/src/capabilities/registry.ts b/src/capabilities/registry.tsindex 5c8e0047..f4b9d3de 100644--- a/src/capabilities/registry.ts+++ b/src/capabilities/registry.ts@@ -6,6 +6,7 @@ import { createCreateFileTool } from './filesystem/create-file.js';import { createListDirTool } from './filesystem/list-dir.js';import { createDeleteFileTool } from './filesystem/delete-file.js';import { createRunCommandTool } from './shell/run-command.js';+import { createApproveCommandTool } from './shell/approve-command.js';import { createInstallSkillTool } from './skills/install-skill.js';import { createListSkillsTool } from './skills/list-skills.js';import { createUseSkillTool } from './skills/use-skill.js';@@ -43,7 +44,8 @@ export class CapabilityRegistry {if (manifest.capabilities.shell.enabled) {this.tools.run_command = createRunCommandTool(this.permissions);- logger.info('Shell tool registered');+ this.tools.approve_command = createApproveCommandTool(this.permissions);+ logger.info('Shell tools registered');}if (this.skillLoader) {diff --git a/src/capabilities/shell/approve-command.ts b/src/capabilities/shell/approve-command.tsnew file mode 100644index 00000000..3c2f0f21--- /dev/null+++ b/src/capabilities/shell/approve-command.ts@@ -0,0 +1,17 @@+import { tool } from 'ai';+import { z } from 'zod';+import type { PermissionManager } from '../permissions.js';++export function createApproveCommandTool(permissions: PermissionManager) {+ return tool({+ description: 'Permanently approve a command type so it runs without asking in the future. Use this when the user says "always" or "always approve" for a command. For example, if the user says "always approve curl", call this with command="curl".',+ parameters: z.object({+ command: z.string().describe('The base command to permanently approve (e.g. "curl", "docker", "npm")'),+ }),+ execute: async ({ command }) => {+ const baseCmd = command.trim().split(/\s+/)[0];+ permissions.addApprovedCommand(baseCmd);+ return `Command "${baseCmd}" has been permanently approved. Future calls to "${baseCmd} ..." will run without asking.`;+ },+ });+}\ No newline at end of filediff --git a/src/capabilities/shell/run-command.ts b/src/capabilities/shell/run-command.tsindex 56178fed..8123a0a7 100644--- a/src/capabilities/shell/run-command.ts+++ b/src/capabilities/shell/run-command.ts@@ -9,8 +9,8 @@ export function createRunCommandTool(permissions: PermissionManager) {return tool({description: `Run a shell command. Commands run in the current working directory unless an absolute path is given.Blocked commands (sudo, rm -rf /, etc.) are never executed.-Auto-approved commands (ls, cat, git status, etc.) run without asking.-Other commands require user approval (y/n/always). "always" saves the approval for future use.`,+Auto-approved commands (ls, cat, git status, curl, etc.) run without asking.+Other commands require user approval — tell the user what command you want to run and ask for confirmation. If they say "yes", try again. If they say "always", use the approve_command tool.`,parameters: z.object({command: z.string().describe('The shell command to execute'),}),@@ -18,13 +18,11 @@ Other commands require user approval (y/n/always). "always" saves the approval fconst check = await permissions.checkShellCommand(command);if (!check.allowed) {if (check.needsApproval) {- const response = await askApproval(command, permissions);- if (!response) {- return `Command "${command}" was not approved by user.`;- }- } else {- return `Error: ${check.reason}`;+ const baseCmd = command.trim().split(/\s+/)[0];+ permissions.addPendingApproval(baseCmd);+ return `⚠ Command requires approval: ${command}\n\nTell the user what this command does and ask for permission. If they approve, try running it again. If they say "always", use the approve_command tool to permanently approve this command type.`;}+ return `Error: ${check.reason}`;}try {@@ -48,28 +46,4 @@ Other commands require user approval (y/n/always). "always" saves the approval f}},});-}--async function askApproval(command: string, permissions: PermissionManager): Promise<boolean> {- const handler = (permissions as any).askHandler;- if (!handler) {- return false;- }-- const response = await handler(- `Mercury wants to run: ${command}\nAllow? (y/n/always): `- );-- const normalized = response.toLowerCase().trim();-- if (normalized === 'always') {- permissions.addApprovedCommand(command);- return true;- }-- if (normalized === 'y' || normalized === 'yes') {- return true;- }-- return false;}\ No newline at end of filediff --git a/src/index.ts b/src/index.tsindex 2a098cca..556b649b 100644--- a/src/index.ts+++ b/src/index.ts@@ -20,23 +20,23 @@ function hr() {function banner() {console.log('');- console.log(chalk.cyan(' ┌────────────────────────────────────┐'));- console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));- console.log(chalk.cyan(' └────────────────────────────────────┘'));- console.log(chalk.dim(' v0.1.0 — an AI agent for personal tasks'));- console.log(chalk.dim(' by Cosmic Stack — mercury.cosmicstack.org'));+ console.log(chalk.cyan(' ═════════════════════════════════════'));+ console.log(chalk.bold.white(' M E R C U R Y'));+ console.log(chalk.cyan(' ═════════════════════════════════════'));+ console.log(chalk.dim(' v0.1.0 — an AI agent for personal tasks'));+ console.log(chalk.dim(' by Cosmic Stack — mercury.cosmicstack.org'));console.log('');}function splashScreen() {console.log('');- console.log(chalk.cyan(' ┌────────────────────────────────────┐'));- console.log(chalk.cyan(' │') + chalk.bold.white(' M E R C U R Y ') + chalk.cyan('│'));- console.log(chalk.cyan(' └────────────────────────────────────┘'));+ console.log(chalk.cyan(' ═════════════════════════════════════'));+ console.log(chalk.bold.white(' M E R C U R Y'));+ console.log(chalk.cyan(' ═════════════════════════════════════'));console.log('');- console.log(chalk.dim(' an AI agent for personal tasks'));- console.log(chalk.cyan(' by Cosmic Stack'));- console.log(chalk.dim(' mercury.cosmicstack.org'));+ console.log(chalk.dim(' an AI agent for personal tasks'));+ console.log(chalk.cyan(' by Cosmic Stack'));+ console.log(chalk.dim(' mercury.cosmicstack.org'));console.log('');}