Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

12.5 KB
Raw Blame
import { homedir } from 'node:os';
import { mkdirSync } from 'node:fs';
import { isAbsolute, relative, resolve } from 'node:path';
import { CapabilityRegistry } from '../capabilities/registry.js';
import { isMcpToolName } from '../capabilities/mcp/naming.js';
import type { SkillLoader } from '../skills/loader.js';
import type { UserMemoryStore } from '../memory/user-memory.js';
import type { MercuryConfig } from '../utils/config.js';
import { logger } from '../utils/logger.js';
import type { BotManifest, BotPermissionsFile, BotPathScope } from './types.js';
import { BOT_DANGEROUS_TOOLS, BOT_SANDBOX_DIRNAME, BOT_SHARED_SANDBOX_DIRNAME } from './store.js';
import { isPluginToolName, pluginToolAllowed } from '../plugins/tool-bridge.js';

/**
 * Tools a bot must never see, regardless of manifest. These are the tools
 * that prompt the user (bots run unattended), mutate the global permission
 * manifest, delegate to sub-agents, or manage schedules:
 */
const ALWAYS_STRIPPED = new Set([
  'ask_user',            // bots never ask questions mid-run
  'approve_scope',       // would persist scopes into the GLOBAL permissions.yaml
  'approve_command',     // same global-state mutation hazard
  'update_plan',         // plan UI has no bot surface
  'delegate_task',       // bots do not spawn sub-agents in P0
  'list_agents',
  'stop_agent',
  'install_skill',       // no skill installs from an unattended bot
  'create_skill',        // self-made skills come from the owner's own sessions (ADR-027)
  'schedule_task',       // bot schedules are managed via bot.yaml, not tools
  'list_scheduled_tasks',
  'cancel_scheduled_task',
  'send_message',        // channel messaging belongs to the owning surfaces
  'send_file',
  'bot_send',            // replaced by the bot-scoped instance after filtering
  'dispatch_bot',        // the owner's way to wake a bot; a bot uses bot_send
]);

/**
 * Tool groups a bot gets only when its gate names them (an allow entry with the
 * group name, e.g. "spotify", or a tool's own name). Every tool schema is re-sent
 * on every step, and a writer bot has no use for 15 Spotify controls: off by
 * default, they cost nothing.
 */
const OPT_IN_GROUPS: Array<{ group: string; match: (name: string) => boolean }> = [
  { group: 'spotify', match: (n) => n.startsWith('spotify_') },
  { group: 'github', match: (n) => ['github_api', 'create_issue', 'list_issues', 'create_pr', 'review_pr'].includes(n) },
  { group: 'git', match: (n) => n.startsWith('git_') },
];

function optInGroup(name: string): string | undefined {
  return OPT_IN_GROUPS.find((g) => g.match(name))?.group;
}

export interface BotRegistryDeps {
  botId: string;
  manifest: BotManifest;
  botDir: string;
  /**
   * The bot's permissions.yaml — THE single source of truth for what this
   * bot may do: tool gate (tools), path scopes (paths), shell lists. The
   * persona is NOT a permission source (character only).
   */
  permissions: BotPermissionsFile;
  config: MercuryConfig;
  /** Per-bot memory store (P0-5); null until memory scoping is wired. */
  userMemory?: UserMemoryStore | null;
  /**
   * Skill access: when set, the bot gets list_skills + use_skill over the
   * global library AND its own skills dir (install_skill stays stripped).
   */
  skillLoader?: SkillLoader;
  /**
   * Sandbox areas granted implicitly (read/write/execute, no ask, no
   * persona declaration): deps.botDir/sandbox (private workspace) and the
   * fleet-shared folder next to the bots root. Unset = not granted.
   */
  sandbox?: { workspace: string; shared: string };
  /**
   * Ask the owner about something outside the bot's grants (a command, a file
   * outside its folders). Resolves "yes" | "always" | "no"; the runtime answers
   * "no" when nobody replies in time. Unset = never ask (fail closed).
   */
  ask?: (prompt: string) => Promise<string>;
}

/**
 * Build a bot's own CapabilityRegistry + filtered toolset.
 *
 * Isolation model (BOTS-ARCHITECTURE.md §2.5):
 * - own PermissionManager instance — no shared mutable cwd/channel swapping
 * - NO ask handler: every approval path fails closed (auto-deny)
 * - channel type 'bot' — never 'internal' (internal auto-approves)
 * - shell auto-approve list emptied; safe-read classifier still applies
 * - no persistent-manifest writes: interactive mutation tools are stripped
 */
export function createBotCapabilityRegistry(deps: BotRegistryDeps): CapabilityRegistry {
  const registry = new CapabilityRegistry(deps.skillLoader);
  const pm = registry.permissions;

  // Reshape the manifest in place (never call save() — that writes the
  // global ~/.mercury/permissions.yaml, and bots have no path to it).
  const manifest = pm.getManifest();
  // Malformed scope entries (missing/empty `scope` — e.g. a hand-edited
  // permissions.yaml typo) must never crash every turn: skip + warn.
  const fileGrants = (deps.permissions.paths ?? []).filter(isValidScopeEntry);
  const skipped = (deps.permissions.paths?.length ?? 0) - fileGrants.length;
  if (skipped > 0) {
    logger.warn({ botId: deps.botId, skipped }, 'Malformed path-scope entries in permissions.yaml (missing "scope") skipped');
  }
  const granted = fileGrants;
  // Implicit sandbox grants come LAST and are never user-configurable away:
  // the private workspace and the fleet-shared folder are the bot's built-in
  // work areas (read/write/execute, no ask, no declaration).
  if (deps.sandbox) {
    mkdirSync(deps.sandbox.workspace, { recursive: true });
    mkdirSync(deps.sandbox.shared, { recursive: true });
    granted.push(
      { scope: deps.sandbox.workspace, read: true, write: true, execute: true },
      { scope: deps.sandbox.shared, read: true, write: true, execute: true },
    );
  }
  manifest.capabilities.filesystem.scopes = buildBotScopes(granted, deps.botDir);
  // Shell execution: instead of emptying autoApproved (which made every
  // non-safe-read command deny, breaking browser/file tooling), bots declare
  // their own allow-list via permissions.yaml autoApproveCommands. It is
  // carried in a dedicated PermissionManager field (NOT the manifest list)
  // so an ambient global autoApproved entry can never silently elevate an
  // unattended context. Fail-closed stays intact: no ask handler,
  // needsApproval wins, the global blocked list wins, and a literal "*"
  // grant is dropped (allow-all stays interactive-only).
  const approvedCommands = (deps.permissions.autoApproveCommands ?? [])
    .map(c => c.trim())
    .filter(c => c.length > 0 && c !== '*');
  manifest.capabilities.shell.autoApproved = [];
  pm.setBotShellAllowList(approvedCommands);
  manifest.capabilities.shell.blocked = [
    ...new Set([...manifest.capabilities.shell.blocked, ...(deps.permissions.blockedCommands ?? [])]),
  ];

  // Fail-closed core: no ask handler, no allow-all, non-internal context.
  // Granted scopes apply without prompting; everything else denies.
  pm.setAutoApproveAll(false);
  pm.setFailClosed(true);
  pm.setCurrentContext('bot', deps.botId);
  if (deps.ask) pm.onAsk(deps.ask);

  // Relative paths and shell commands resolve inside the bot's workspace,
  // never the daemon's cwd (a bot with a wide grant wrote "sandbox/…" files
  // into whatever repo the daemon happened to be started from).
  const workspace = deps.sandbox?.workspace ?? deps.botDir;
  registry.setCwd(workspace);
  // "_shared/…" and "sandbox/…" (the names a bot's instructions use) always
  // mean its real folders, whatever its cwd. And a bot can only move its cwd
  // within its own folders: with a wide grant (e.g. "~"), a cwd inside a code
  // checkout turned every relative write into a write to that repo.
  const roots: Array<readonly [string, string]> = [['sandbox', workspace]];
  if (deps.sandbox) roots.push([BOT_SHARED_SANDBOX_DIRNAME, deps.sandbox.shared]);
  registry.setPathRoots(roots);
  const ownDirs = [workspace, deps.botDir, ...(deps.sandbox ? [deps.sandbox.shared] : [])].map((d) => resolve(d));
  registry.setCwdGuard((dir) => ownDirs.some((base) => isWithin(base, resolve(dir))));
  registry.registerAll();

  const tools = filterBotTools(registry.getTools(), deps.manifest, deps.permissions);
  logger.info(
    { botId: deps.botId, tools: Object.keys(tools).length, scopes: manifest.capabilities.filesystem.scopes.length },
    'Bot capability registry built (fail-closed, isolated)',
  );
  return registry;
}

/**
 * Tool-set filter. The gate comes from the bot's permissions.yaml
 * (`tools.allow/deny`) — the single source of truth. Legacy fallback: a
 * manifest that still carries an explicit tools block (pre-migration
 * bot.yaml) is honored; with NEITHER source configured, the fail-closed
 * dangerous-tool deny list applies (same default as manifest
 * normalization). Interactive/global-mutation tools are always stripped;
 * deny wins over allow; a non-empty allow restricts the toolset to exactly
 * that list.
 */
export function filterBotTools(all: Record<string, any>, manifest: BotManifest, permissions?: BotPermissionsFile): Record<string, any> {
  const explicitManifestTools = (manifest as any).tools !== undefined;
  const gate = permissions?.tools
    ?? (explicitManifestTools ? manifest.tools : undefined)
    ?? { deny: [...BOT_DANGEROUS_TOOLS] };
  const allow = gate.allow ?? [];
  // permissions.yaml wins over bot.yaml: every loaded manifest carries the
  // default dangerous-tool deny list (normalizeBotManifest), so a union would
  // make the permission tiers unable to grant anything.
  const deny = new Set(gate.deny ?? []);
  const out: Record<string, any> = {};
  for (const [name, tool] of Object.entries(all)) {
    if (ALWAYS_STRIPPED.has(name) || isMcpToolName(name) || name.startsWith('browser_')) continue;
    if (deny.has(name)) continue;
    // Plugin tools are opt-in for bots (fail-closed): only an explicit allow
    // entry ("<plugin>__<tool>" or "<plugin>__*") lets a bot see one. A
    // deny-only gate must never expose a tool installed after the bot was set up.
    if (isPluginToolName(name)) {
      if (!pluginToolAllowed(name, allow)) continue;
      out[name] = tool;
      continue;
    }
    const group = optInGroup(name);
    if (group && !allow.includes(group) && !allow.includes(name)) continue;
    if (allow.length > 0 && !allow.includes(name) && !(group && allow.includes(group))) continue;
    out[name] = tool;
  }
  return out;
}

/**
 * Bot path scopes from permissions.yaml + persona Access grants. 'self'
 * resolves to the bot's own profile dir; everything else resolves against
 * cwd or home (~). An unconfigured bot gets NO filesystem access
 * (fail-closed) — but the store always writes a default self scope at
 * creation.
 */
/** A scope entry is usable only with a non-empty string scope. */
function isValidScopeEntry(p: BotPathScope | undefined): boolean {
  return !!p && typeof p.scope === 'string' && p.scope.trim().length > 0;
}

/**
 * Union of path-scope grants keyed by resolved path: the same path granted
 * twice unions its modes. Used by the permission migration to fold legacy
 * persona Access grants into the permissions.yaml paths block.
 */
export function mergePathScopes(
  baseScopes: BotPathScope[] | undefined,
  extraScopes: BotPathScope[],
): BotPathScope[] {
  if (extraScopes.length === 0) return baseScopes ?? [];
  const merged = [...(baseScopes ?? [])];
  for (const grant of extraScopes) {
    const key = normalizeScopeKey(grant.scope);
    const existing = merged.find(p => normalizeScopeKey(p.scope) === key);
    if (existing) {
      existing.read = existing.read || grant.read;
      existing.write = existing.write || grant.write;
      existing.execute = existing.execute || grant.execute;
    } else {
      merged.push(grant);
    }
  }
  return merged;
}

/** Key for grant de-duplication: resolved absolute path ('self' resolved later). */
function normalizeScopeKey(scope: string): string {
  return scope === 'self' ? 'self' : resolve(scope.replace(/^~/, homedir())).toLowerCase();
}

function buildBotScopes(paths: BotPathScope[] | undefined, botDir: string): Array<{ path: string; read: boolean; write: boolean; execute?: boolean }> {
  const scopes: Array<{ path: string; read: boolean; write: boolean; execute?: boolean }> = [];
  for (const p of paths ?? []) {
    if (!isValidScopeEntry(p)) continue;
    const resolved = p.scope === 'self'
      ? resolve(botDir)
      : resolve(p.scope.replace(/^~/, homedir()));
    scopes.push({ path: resolved, read: p.read ?? false, write: p.write ?? false, execute: p.execute || undefined });
  }
  return scopes;
}

function isWithin(base: string, target: string): boolean {
  const rel = relative(base, target);
  return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
}