// `exec` and `process` tool definitions — Phase 5a. // // Two tools, both with target='local'| pattern: // // - exec : single-shot command. sync (default) or background. // - process : interact with running background jobs. action enum: // list, poll, log, write, kill. // // The model is steered (via tool descriptions) toward file_write for // remote file writing and file_read for paging through big captured // outputs — exec is for COMMANDS, not for moving file contents. import { z } from 'zod'; import { logger } from '../../server/logger.ts'; import type { ToolDefinition } from '../types.ts'; import { resolveVisibleResourceFresh } from '../resources/visibility.ts'; import { auditPrivilegedExec, evaluateExecPolicy } from './allowlist.ts'; import { checkBlacklist } from './blacklist.ts'; import { releaseRemoteSlot, tryAcquireExecSlot } from './concurrency.ts'; import { clearLivePid, clearLiveStdin, completeJob, failJob, getLivePid, getLiveStdin, listJobsForAgent, markJobKilled, probeLocalJob, readJobLog, readMeta, JOB_ID_RE, type JobMeta, } from './job-store.ts'; import { skillEnvScopeForCommand, skillEnvStripHint } from '../../skills/env-scope.ts'; import { SOMORA_INTERNAL_ENV_SUMMARY } from './internal-env.ts'; import { killLocalJob, localExecBackground, localExecSync } from './local.ts'; import { fitExecOutput } from './fit-output.ts'; import { recordExecTimeout, repeatTimeoutNotice } from './timeout-repeats.ts'; import { saveToolOutput } from '../tool-output.ts'; import { pinnedWorkdir } from '../../server/session-workdir.ts'; /** * When a stream was shortened for the result (or capped at capture), * keep the captured text on disk and tell the model where. The captured * text is the full output up to the 256 KB capture cap. */ async function saveShortenedStreams( agent: string, r: { stdout: string; stderr: string; truncated: boolean }, shortened: boolean, ): Promise<{ files: { stdout_file?: string; stderr_file?: string }; hint?: string }> { if (!shortened && !r.truncated) return { files: {} }; const files: { stdout_file?: string; stderr_file?: string } = {}; if (r.stdout.length > 0) { const f = await saveToolOutput(agent, 'exec-stdout', r.stdout); if (f) files.stdout_file = f; } if (r.stderr.length > 0) { const f = await saveToolOutput(agent, 'exec-stderr', r.stderr); if (f) files.stderr_file = f; } if (!files.stdout_file && !files.stderr_file) return { files }; const parts: string[] = []; if (files.stdout_file) parts.push(`stdout (${r.stdout.length} chars) → ${files.stdout_file}`); if (files.stderr_file) parts.push(`stderr (${r.stderr.length} chars) → ${files.stderr_file}`); const capNote = r.truncated ? ' (capture stopped at 256 KB per stream)' : ''; return { files, hint: `Full captured output saved${capNote}: ${parts.join('; ')}. ` + `Read a part with file_read offset/limit or search in it with file_search instead of re-running the command.`, }; } import { killRemoteJob, pollRemoteJob, remoteExecBackground, remoteExecSync, tailRemoteJobLog, } from './remote.ts'; // ───────────────────────────────────────────────────────────────────── // exec // ───────────────────────────────────────────────────────────────────── /** Field texts shared by the zod schema and the JSON schema, so every * engine reads the same (src/tools/schema-parity.test.mts). */ const EXEC_FIELD = { env: 'Extra environment variables to merge into the command shell. Local target only in v1.', background: 'true = fire-and-forget. Returns a job_id immediately; output streams to disk; ' + 'use process tool to poll/kill/read. Only supported on target="local" in v1.', timeout_ms: 'Sync only. Kill after this. Default 60s, max 1h. Ignored when background:true. ' + 'stdin is closed, so a command waiting for input ends at once. ' + 'When you intentionally run a long-blocking command, set this above the expected ' + 'duration — e.g. for `sleep N` use `timeout_ms: (N+5)*1000`. The 60s default exists ' + 'so a runaway command can\'t lock a tool slot for an hour.', pty: 'Allocate a pseudo-terminal so TUI tools and tools that check isatty() ' + '(vim, htop, claude, codex, color/cursor handling, progress bars) work correctly. ' + 'Works on both target=local (via node-pty) and target= (via ssh2 native ' + 'pty allocation). When pty is on, stdout and stderr merge into one stream ' + '(= same behavior as a real terminal). Background mode ignores this flag — ' + 'TUI tools running in the background nobody can see; use tmux for that.', description: 'Short human-readable label for this command. Surfaces in process_list and logs.', }; const ExecInput = z .object({ command: z .string() .min(1) .describe('Shell command to run. Interpreted by the target machine\'s default shell (bash/zsh/dash).'), target: z .string() .min(1) .default('local') .describe( '"local" (default) = the somora server\'s machine. Otherwise a configured ' + 'resource name from resource_list. Resources must be set up in config.yaml — exec ' + 'cannot dial ad-hoc SSH targets.', ), cwd: z .string() .min(1) .optional() .describe( 'Working directory on the target machine. Absolute path, "~/..." or relative ' + '(both resolved against the target user\'s home). Must be an existing directory — ' + 'exec fails with a cwd error otherwise.', ), env: z .record(z.string(), z.string()) .optional() .describe(EXEC_FIELD.env), background: z .boolean() .default(false) .describe(EXEC_FIELD.background), timeout_ms: z .number() .int() .min(100) .max(3_600_000) .default(60_000) .describe(EXEC_FIELD.timeout_ms), pty: z .boolean() .default(false) .describe(EXEC_FIELD.pty), description: z .string() .min(1) .max(200) .optional() .describe(EXEC_FIELD.description), inherit_agent_env: z .boolean() .default(false) .describe( 'local target only. When false (default), the shell gets a curated env: somora-internal ' + `vars (${SOMORA_INTERNAL_ENV_SUMMARY}) are stripped before spawn, so a \`claude\` / ` + '`codex` invocation behaves like in the user\'s normal terminal and a project\'s own ' + 'tsx/next/dotenv see their own config, not somora\'s. Set true only when you ' + 'intentionally want somora\'s isolated claude tree and runtime vars visible to the ' + 'spawned process.', ), }) .strict(); interface ExecSyncResult { ok: boolean; background: false; target: string; exit_code: number | null; stdout: string; stderr: string; truncated: boolean; ms: number; hint?: string; /** Killed by its timeout_ms. */ timed_out?: boolean; /** From the second timeout of the same command in this session and * folder: how often it has timed out (timeout-repeats.ts). */ timeouts_in_session?: number; } interface ExecBackgroundResult { ok: true; background: true; job_id: string; pid: number; target: string; hint: string; } interface ExecBlockedResult { ok: false; background: false; blocked: true; reason: string; pattern: string; } interface ExecCapDeniedResult { ok: false; background: true; cap_denied: true; reason: string; per_agent_cap: number; global_cap: number; } type ExecResult = | ExecSyncResult | ExecBackgroundResult | ExecBlockedResult | ExecCapDeniedResult; /** Who runs the command: skill scripts call back into somora with it, * and `somora server restart` / `somora update` use it to schedule the * restart for the end of this turn instead of cutting it. In the MCP * child the launcher has set both already; the in-process engine has * them only here. */ function callerIdentityEnv(ctx: { agent: string; session?: string }): Record { return { SOMORA_AGENT: ctx.agent, ...(ctx.session ? { SOMORA_SESSION: ctx.session } : {}) }; } export const exec: ToolDefinition, ExecResult> = { name: 'exec', toolset: 'exec', description: 'Run a shell command on the somora server (target:"local") or on a configured SSH resource ' + '(target: from resource_list). Default: synchronous, returns stdout/stderr/exit. ' + 'Set background:true for long-running tasks (build, test-suite, dev server) — returns a ' + 'job_id immediately, output streams to disk, interact via the process tool ' + '(list/poll/log/write/kill). Background jobs are fully detached: they keep running across ' + 'turns and somora/MCP restarts until they finish or you kill them. Note: process write ' + '(stdin) only works while the somora process that spawned the job is alive. ' + '\n\n' + 'Restarting somora itself: run `somora server restart` (or `somora update`) — from here it does ' + 'not restart at once but when your turn has ended, and you are woken in this session when the ' + 'server is back; end your turn after it, do not wait or poll. NEVER restart it with ' + '`systemctl restart somora` / `launchctl` directly: that kills the turn you are in. ' + '\n\n' + 'IMPORTANT — to reach a remote host, ALWAYS use target: (from resource_list). ' + 'NEVER shell out to raw `ssh user@host …` from target:"local" — that bypasses the resource ' + 'config, hits hosts without keys, and produces "Permission denied (publickey,password)" errors. ' + 'If a host you need is not in resource_list, ask the user to add it to config.yaml first. ' + '\n\n' + 'IMPORTANT — non-empty stderr does NOT mean failure. Many tools (ssh, git, build systems) write ' + 'progress / warnings to stderr while still exiting 0. Treat the call as failed ONLY when ' + '`ok:false` OR `exit_code != 0`. Do not retry just because stderr has content — read it for ' + 'context but trust the exit status. ' + '\n\n' + 'IMPORTANT — for writing FILES on a remote target, ALWAYS use file_write with target:. ' + 'NEVER use exec with heredoc/echo to push file content over SSH — shell escaping breaks for ' + 'large or complex content. file_write goes through SFTP, binary-clean, no escape pain. ' + '\n\n' + 'Remote targets have NO assumed binaries beyond POSIX core (sh, ls, cat, cp, mv, find, …). ' + 'jq, python, node, rg etc. may be missing — probe first with `which ` or `command -v` ' + 'before relying on them, or pipe a JSON post-processor through stdin on the local side. ' + 'To leave something running on a remote target from a sync call, detach all three streams: ' + '`nohup cmd /tmp/cmd.log 2>&1 &` — a bare `cmd &` keeps the call open, and it returns ' + 'a few seconds after your command exits with a note; or use background:true. ' + '\n\n' + 'Output: a sync result carries about 60 000 chars of stdout+stderr. Longer output comes back as ' + 'the first and last part of each stream with the cut marked in the text and truncated:true — ' + 'never guess the omitted middle; redirect to a file and file_read it, or narrow the command. ' + 'Capture cap: 256 KB per stream, also for process_log tail reads. If your command ' + 'produces more output, write it to a file on the target (e.g. ./out.log) and use file_read ' + 'with offset/limit to page through. ' + '\n\n' + 'Hard-blocked: rm -rf / and other destructive system ops, sudo/doas, fork bombs, system ' + 'shutdown, chmod world-writable on /etc /usr /bin /sbin /boot /root /var /opt, reading ' + 'private SSH keys, curl|sh / wget|sh untrusted-remote-exec patterns. Match → blocked:true ' + 'in result with reason; the model should adapt the command rather than retry. ' + '\n\n' + 'Per-resource override: a configured SSH resource may have an `allowBlocked` list in ' + 'config.yaml that whitelists specific commands (e.g. `sudo ~/bin/update.sh`, `systemctl ' + 'reboot`) despite the global block. Check resource_list — entries with allowBlockedCount>0 ' + 'have privileged overrides. local target is never overridable.', inputSchema: ExecInput, jsonSchema: { type: 'object', properties: { command: { type: 'string', description: 'Shell command.' }, target: { type: 'string', description: '"local" or resource name.', default: 'local' }, cwd: { type: 'string', description: 'Working directory. Absolute, "~/..." or relative-to-target-home; must exist. Default: the folder of the project pinned to this session (when it has a workdir), else the server process cwd.', }, env: { type: 'object', additionalProperties: { type: 'string' }, description: EXEC_FIELD.env }, background: { type: 'boolean', default: false, description: EXEC_FIELD.background }, timeout_ms: { type: 'integer', minimum: 100, maximum: 3_600_000, default: 60_000, description: EXEC_FIELD.timeout_ms }, pty: { type: 'boolean', default: false, description: EXEC_FIELD.pty }, description: { type: 'string', maxLength: 200, description: EXEC_FIELD.description }, inherit_agent_env: { type: 'boolean', default: false, description: `local only. Default false: somora-internal env (${SOMORA_INTERNAL_ENV_SUMMARY}) is stripped ` + 'so a project\'s own tsx/next/dotenv and a nested claude/codex behave like in a normal terminal. ' + 'true = inherit everything.', }, }, required: ['command'], additionalProperties: false, }, // Engine-level race: sync exec gets the caller's timeout_ms +2s // buffer (OpenClaw pattern). Background returns instantly so the // 30s default is plenty. defaultTimeoutMs: 30_000, timeoutFromInput: (input) => input.background ? undefined : (input.timeout_ms ?? 60_000) + 2_000, maxTimeoutMs: 3_600_000 + 2_000, async handler(input, ctx): Promise { // Hard-blacklist runs FIRST — never even reach a target if the // command is in the deny list. const block = checkBlacklist(input.command); if (block.matched) { // Per-resource allowlist override: if the target is a configured // SSH resource, evaluate the command against its `allowBlocked` // list segment-by-segment. `local` target NEVER gets an override // (the host where somora runs is not a dedicated-agent-workstation) // — we pass an empty entry list so evaluateExecPolicy always // blocks there. See src/tools/exec/allowlist.ts for the algorithm // (each blacklisted sub-command needs its own override; hard-blocks // like `rm -rf /etc` and cross-pipe `curl|sh` can't be overridden). let entries: ReadonlyArray = []; if (input.target !== 'local') { const resource = await resolveVisibleResourceFresh(ctx.agent, input.target); if (resource && resource.type === 'ssh') { entries = resource.allowBlocked; } } const policy = evaluateExecPolicy(input.command, entries); if (policy.allowed) { logger.info({ msg: 'exec.privileged_allowed', agent: ctx.agent, target: input.target, matched_entries: policy.overrides ?? [], blacklist_reason: block.reason, command_head: input.command.slice(0, 120), }); // Audit-trail JSONL — fire-and-forget. We deliberately log // the AUTH event (matched + about-to-execute), not the // completion: the rest of somora's exec logging captures // exit codes, and one line per privileged-allow is the // simplest after-the-fact review surface. void auditPrivilegedExec({ ts: Date.now(), agent: ctx.agent, session: ctx.session ?? 'unknown', resource: input.target, command_head: input.command.slice(0, 200), matched_entry: (policy.overrides ?? []).join(', '), blacklist_reason: block.reason, blacklist_pattern: block.pattern, }); } else { logger.warn({ msg: 'exec.blocked', agent: ctx.agent, target: input.target, reason: policy.reason ?? block.reason, pattern: policy.pattern ?? block.pattern, command_head: input.command.slice(0, 120), }); return { ok: false, background: false, blocked: true, reason: policy.reason ?? block.reason, pattern: policy.pattern ?? block.pattern, ...(policy.segment ? { blocked_segment: policy.segment } : {}), ...(policy.hint ? { hint: policy.hint } : {}), // Surface the resource's allowBlocked entries (if any) so the // agent can see why none of them cleared this command instead // of having to reverse-engineer the matcher (2026-07-27 // feedback). Entries match a segment when it equals the entry // or starts with `entry + ' '`, and command substitution // ($(…)/backticks) always blocks. ...(entries.length > 0 ? { allow_blocked_entries: [...entries] } : {}), }; } } if (input.background) { // Concurrency cap — protects the host from runaway-loop spawn. // Counted across both local AND remote BG jobs since the load // matters regardless of which machine the work lands on. Slot is // released in job-store completion paths. // // Per-agent count comes from DISK (post-liveness-probe), not // only the in-memory counter: detached jobs survive somora/MCP // restarts, so a fresh process would otherwise under-count and // over-spawn. The probe also clears stale 'running' states so a // dead job can never wedge the cap. const diskJobs = await listJobsForAgent(ctx.agent); const diskRunning = ( await Promise.all( diskJobs .filter((j) => j.state === 'running') .map((j) => probeLocalJob(ctx.agent, j)), ) ).filter((j) => j.state === 'running').length; const slot = tryAcquireExecSlot(ctx.agent, diskRunning); if (!slot.ok) { logger.warn({ msg: 'exec.background.cap_denied', agent: ctx.agent, target: input.target, per_agent_now: slot.perAgentNow, global_now: slot.globalNow, reason: slot.reason, }); return { ok: false, background: true, cap_denied: true, reason: slot.reason, per_agent_cap: slot.perAgentCap, global_cap: slot.globalCap, }; } try { if (input.target === 'local') { const bgSkillScope = await skillEnvScopeForCommand(input.command, ctx.config); const result = await localExecBackground({ agent: ctx.agent, ...(ctx.session ? { session: ctx.session } : {}), command: input.command, ...(input.cwd ? { cwd: input.cwd } : {}), env: { ...callerIdentityEnv(ctx), ...(input.env ?? {}) }, ...(input.description ? { description: input.description } : {}), releaseSlot: slot.release, stripSomoraInternalEnv: !input.inherit_agent_env, ...(bgSkillScope ? { skillEnvScope: bgSkillScope } : {}), }); return { ok: true, background: true, job_id: result.job_id, pid: result.pid, target: 'local', hint: `Job started in the background. Use process({action:"poll", job_id:"${result.job_id}"}) ` + `to check status, process({action:"log", job_id:"${result.job_id}"}) for output, ` + `process({action:"kill", job_id:"${result.job_id}"}) to terminate.`, }; } // Remote target — nohup-pattern, slot is released when the // job is detected complete via process({action:"poll"}) or // explicitly killed. process tool calls completeJob/failJob/ // markJobKilled which can in turn signal release; for v1 we // accept that the slot remains held until either a poll // resolves the job or it's killed. The cap protects against // floods, not against long-running jobs. const remote = await remoteExecBackground({ agent: ctx.agent, target: input.target, command: input.command, ...(input.cwd ? { cwd: input.cwd } : {}), ...(input.description ? { description: input.description } : {}), releaseSlot: slot.release, }); return { ok: true, background: true, job_id: remote.job_id, pid: remote.remote_pid, target: input.target, hint: `Remote job started on ${input.target} (pid=${remote.remote_pid}). Output streams to ` + `${remote.remote_out_file} / ${remote.remote_err_file} on the remote host. Use ` + `process({action:"poll", job_id:"${remote.job_id}"}) to check status, ` + `process({action:"log", job_id:"${remote.job_id}"}) for tail, ` + `process({action:"kill", job_id:"${remote.job_id}"}) to terminate. ` + `Note: ${remote.remote_out_file} stays on the remote host even after the job ends — ` + `clean up via exec({command:"rm /tmp/somora-exec-${remote.job_id}.*", target:"${input.target}"}) when done.`, }; } catch (err) { // Spawn failed — release the slot so the cap doesn't leak. slot.release(); throw err; } } // ── sync path ── if (input.target === 'local') { // Skill-scoped env: strip all skill-declared secrets, re-inject // only those whose skill's bins this command invokes. Logged when // it actually matched — forensics for "why does/doesn't my skill // command see its env var". const skillScope = await skillEnvScopeForCommand(input.command, ctx.config); if (skillScope && skillScope.matchedSkills.length > 0) { logger.info({ msg: 'exec.skill_env_injected', agent: ctx.agent, skills: skillScope.matchedSkills, vars: Object.keys(skillScope.injectEnv), }); } // Default cwd: the folder of the project pinned to this session, // when it has one (docs/projects.md `workdir`); else the server's. const localCwd = input.cwd ?? (await pinnedWorkdir(ctx.agent, ctx.session)) ?? undefined; const r = await localExecSync({ command: input.command, ...(localCwd ? { cwd: localCwd } : {}), env: { ...callerIdentityEnv(ctx), ...(input.env ?? {}) }, timeoutMs: input.timeout_ms ?? 60_000, pty: input.pty, stripSomoraInternalEnv: !input.inherit_agent_env, ...(skillScope ? { skillEnvScope: skillScope } : {}), ...(ctx.signal ? { signal: ctx.signal } : {}), }); // A stripped skill var named in a failed command's output is // almost always an indirect bin call — say so, or the agent reads // the bin's "set VAR" message as a broken bootstrap. const stripHint = skillEnvStripHint(skillScope, r.exit_code, `${r.stdout}\n${r.stderr}`); if (stripHint) { logger.info({ msg: 'exec.skill_env_stripped_hint', agent: ctx.agent, vars: skillScope?.stripVars.filter((v) => !(v in (skillScope?.injectEnv ?? {}))), }); } let repeat: { count: number; totalMs: number } | null = null; if (r.timed_out) { repeat = await recordExecTimeout({ agent: ctx.agent, session: ctx.session, cwd: localCwd, command: input.command, ms: r.ms }); logger[repeat.count >= 2 ? 'warn' : 'info']({ msg: repeat.count >= 2 ? 'exec.timeout_repeat' : 'exec.timeout', agent: ctx.agent, session: ctx.session, cwd: localCwd, command: input.command.slice(0, 200), timeout_ms: input.timeout_ms ?? 60_000, quiet_ms: r.quiet_ms, count: repeat.count, total_ms: repeat.totalMs, }); } const repeatNotice = repeat && repeat.count >= 2 ? repeatTimeoutNotice(repeat.count, repeat.totalMs) : ''; const fitted = fitExecOutput(r.stdout, r.stderr); const saved = await saveShortenedStreams(ctx.agent, r, fitted.shortened); const hint = [repeatNotice, stripHint, fitted.hint, saved.hint].filter(Boolean).join(' '); return { ok: r.exit_code === 0, background: false, target: 'local', exit_code: r.exit_code, stdout: fitted.stdout, stderr: fitted.stderr, // One flag for every way the text can be incomplete: the 256 KB // capture cap AND the result budget (fit-output.ts). truncated: r.truncated || fitted.shortened, ms: r.ms, ...(r.timed_out ? { timed_out: true } : {}), ...(repeat && repeat.count >= 2 ? { timeouts_in_session: repeat.count } : {}), ...saved.files, ...(hint ? { hint } : {}), }; } // Remote sync — env not threaded yet (ssh2 exec doesn't take it // as easily as local; would need shell-quoting). Surface a hint // if the model tries to set env on remote. if (input.env) { logger.info({ msg: 'exec.remote.env_ignored', target: input.target, keys: Object.keys(input.env), hint: 'env-on-remote requires shell-quoted prefix; v1 ignores. Use cwd or cd in command.', }); } const rr = await remoteExecSync({ agent: ctx.agent, target: input.target, command: input.command, ...(input.cwd ? { cwd: input.cwd } : {}), timeoutMs: input.timeout_ms ?? 60_000, ...(input.pty ? { pty: true } : {}), }); const rfitted = fitExecOutput(rr.stdout, rr.stderr); const rsaved = await saveShortenedStreams(ctx.agent, rr, rfitted.shortened); const rhint = [rfitted.hint, rsaved.hint].filter(Boolean).join(' '); return { ok: rr.exit_code === 0, background: false, target: input.target, exit_code: rr.exit_code, stdout: rfitted.stdout, stderr: rfitted.stderr, truncated: rr.truncated || rfitted.shortened, ms: rr.ms, ...rsaved.files, ...(rhint ? { hint: rhint } : {}), }; }, }; // ───────────────────────────────────────────────────────────────────── // process — action enum for interacting with background exec jobs // ───────────────────────────────────────────────────────────────────── const PROCESS_FIELD = { action: 'list (no other fields) | poll (job_id) | log (job_id, tail_lines?) | write (job_id, text) | kill (job_id, signal?)', job_id: 'Required for poll/log/write/kill. From the job_id returned by exec({background:true}).', text: 'write only — text to send to the job\'s stdin. Append \\n yourself if needed.', tail_lines: 'log only — how many lines from the END of stdout/stderr each. Default 50.', signal: 'kill only — signal to send. Default SIGTERM (graceful).', }; const ProcessInput = z .object({ action: z .enum(['list', 'poll', 'log', 'write', 'kill']) .describe(PROCESS_FIELD.action), job_id: z .string() .regex(JOB_ID_RE, 'a job id as returned by exec background:true (job__<4 chars>)') .optional() .describe(PROCESS_FIELD.job_id), text: z .string() .optional() .describe(PROCESS_FIELD.text), tail_lines: z .number() .int() .min(1) .max(2000) .default(50) .describe(PROCESS_FIELD.tail_lines), signal: z .enum(['SIGTERM', 'SIGKILL', 'SIGINT', 'SIGHUP']) .default('SIGTERM') .describe(PROCESS_FIELD.signal), }) .strict(); interface ProcessListResult { action: 'list'; count: number; jobs: Array<{ job_id: string; target: string; state: JobMeta['state']; started_at: number; ended_at?: number; exit_code?: number | null; description?: string; command_head: string; ms?: number; }>; } interface ProcessPollResult { action: 'poll'; job_id: string; state: JobMeta['state']; exit_code: number | null | undefined; pid: number | null; ms: number; error?: string; } interface ProcessLogResult { action: 'log'; job_id: string; stdout_tail: string; stderr_tail: string; total_stdout_bytes: number; total_stderr_bytes: number; truncated: boolean; hint?: string; } interface ProcessWriteResult { action: 'write'; job_id: string; ok: boolean; bytes_written: number; error?: string; } interface ProcessKillResult { action: 'kill'; job_id: string; ok: boolean; /** null when no signal was sent (job already ended). */ signal_sent: string | null; was_running: boolean; note?: string; } type ProcessResult = | ProcessListResult | ProcessPollResult | ProcessLogResult | ProcessWriteResult | ProcessKillResult; function tailLines(text: string, n: number): string { if (!text) return ''; const lines = text.split('\n'); if (lines.length <= n) return text; return lines.slice(-n).join('\n'); } export const processTool: ToolDefinition, ProcessResult> = { name: 'process', toolset: 'exec', description: 'Interact with background exec jobs. action="list" shows your agent\'s known jobs (newest ' + 'first). action="poll" returns the current state of one job. action="log" returns the tail ' + 'of stdout+stderr. action="write" pipes text into the job\'s stdin (local only). action="kill" ' + 'sends a signal. Use exec({background:true}) to spawn jobs first; the job_id from that result ' + 'feeds back into the actions here.', inputSchema: ProcessInput, jsonSchema: { type: 'object', properties: { action: { type: 'string', enum: ['list', 'poll', 'log', 'write', 'kill'], description: PROCESS_FIELD.action }, job_id: { type: 'string', pattern: '^job_\\d+_[a-z0-9]{4}$', description: PROCESS_FIELD.job_id }, text: { type: 'string', description: PROCESS_FIELD.text }, tail_lines: { type: 'integer', minimum: 1, maximum: 2000, default: 50, description: PROCESS_FIELD.tail_lines }, signal: { type: 'string', enum: ['SIGTERM', 'SIGKILL', 'SIGINT', 'SIGHUP'], default: 'SIGTERM', description: PROCESS_FIELD.signal, }, }, required: ['action'], additionalProperties: false, }, async handler(input, ctx): Promise { if (input.action === 'list') { const listed = await listJobsForAgent(ctx.agent); // Resolve stale 'running' states for local jobs before rendering // — a job spawned by a previous somora/MCP process has no exit // listener in THIS process, so meta alone can lie (2026-07-27 // report). probeLocalJob is a no-op for terminal/remote jobs. const jobs = await Promise.all(listed.map((j) => probeLocalJob(ctx.agent, j))); return { action: 'list', count: jobs.length, jobs: jobs.map((j) => ({ job_id: j.job_id, target: j.target, state: j.state, started_at: j.started_at, ...(j.ended_at !== undefined ? { ended_at: j.ended_at } : {}), ...(j.exit_code !== undefined ? { exit_code: j.exit_code } : {}), ...(j.description ? { description: j.description } : {}), command_head: j.command.slice(0, 120), ...(j.ended_at !== undefined ? { ms: j.ended_at - j.started_at } : {}), })), }; } // poll/log/write/kill all need job_id if (!input.job_id) { throw new Error(`process: action='${input.action}' requires job_id`); } const meta = await readMeta(ctx.agent, input.job_id); if (!meta) { throw new Error(`process: job '${input.job_id}' not found for agent '${ctx.agent}'`); } if (input.action === 'poll') { // For LOCAL running jobs: probe PID + exit file. The exit // listener lives in whichever process spawned the job — after a // somora/MCP restart nobody is watching, and trusting meta.state // reported 'running' for long-dead PIDs (2026-07-27 report). if (meta.target === 'local' && meta.state === 'running') { const fresh = await probeLocalJob(ctx.agent, meta); return { action: 'poll', job_id: input.job_id, state: fresh.state, exit_code: fresh.exit_code, pid: fresh.pid, ms: (fresh.ended_at ?? Date.now()) - fresh.started_at, ...(fresh.error ? { error: fresh.error } : {}), }; } // For remote running jobs, do an active probe — kill -0 + // exit-file check on the remote host. Updates meta.state to // done/failed when the remote process has finished. if (meta.target !== 'local' && meta.state === 'running' && meta.pid != null) { try { const probe = await pollRemoteJob(ctx.agent, meta.target, meta.pid, input.job_id); if (!probe.alive) { if (probe.exit_code === null) { await failJob(ctx.agent, input.job_id, 'remote process gone without exit-file'); } else { await completeJob(ctx.agent, input.job_id, probe.exit_code); } // Free the concurrency slot — remote jobs have no // local close-handler equivalent. releaseRemoteSlot(input.job_id); // Re-read meta to surface updated state in this same call. const fresh = await readMeta(ctx.agent, input.job_id); if (fresh) { return { action: 'poll', job_id: input.job_id, state: fresh.state, exit_code: fresh.exit_code, pid: fresh.pid, ms: (fresh.ended_at ?? Date.now()) - fresh.started_at, ...(fresh.error ? { error: fresh.error } : {}), }; } } } catch (err) { logger.warn({ msg: 'exec.process.remote_poll_failed', job_id: input.job_id, target: meta.target, err: (err as Error).message, }); } } return { action: 'poll', job_id: input.job_id, state: meta.state, exit_code: meta.exit_code, pid: meta.pid, ms: (meta.ended_at ?? Date.now()) - meta.started_at, ...(meta.error ? { error: meta.error } : {}), }; } if (input.action === 'log') { const lines = input.tail_lines ?? 50; // Remote: tail the files on the remote host via short ssh exec. if (meta.target !== 'local') { const remote = await tailRemoteJobLog(ctx.agent, meta.target, input.job_id); return { action: 'log', job_id: input.job_id, stdout_tail: tailLines(remote.stdout, lines), stderr_tail: tailLines(remote.stderr, lines), total_stdout_bytes: remote.stdout.length, total_stderr_bytes: remote.stderr.length, truncated: remote.truncated, ...(remote.truncated ? { hint: `Tail capped at 256 KB per stream. Full logs live on the remote host at ` + `/tmp/somora-exec-${input.job_id}.{out,err} on ${meta.target} — ` + `use file_read with target:"${meta.target}" + offset/limit to page through.`, } : {}), }; } // Local const log = await readJobLog(ctx.agent, input.job_id); if (!log) { throw new Error(`process: log not available for job '${input.job_id}'`); } const stdoutTail = tailLines(log.stdout_tail, lines); const stderrTail = tailLines(log.stderr_tail, lines); return { action: 'log', job_id: input.job_id, stdout_tail: stdoutTail, stderr_tail: stderrTail, total_stdout_bytes: log.total_stdout_bytes, total_stderr_bytes: log.total_stderr_bytes, truncated: log.truncated, ...(log.truncated ? { hint: 'Tail capped at 256 KB per stream. Full output is on disk at ' + `~/.somora/agents/${ctx.agent}/exec-jobs/${input.job_id}/{stdout,stderr}.log — ` + 'use file_read with offset/limit to page through.', } : {}), }; } if (input.action === 'write') { if (input.text === undefined) { throw new Error('process: action="write" requires text'); } if (meta.state !== 'running') { return { action: 'write', job_id: input.job_id, ok: false, bytes_written: 0, error: `job is ${meta.state}, not running — can't write to its stdin`, }; } const stdin = getLiveStdin(input.job_id); if (!stdin) { return { action: 'write', job_id: input.job_id, ok: false, bytes_written: 0, error: 'no live stdin handle for this job — likely orphaned across restart, ' + 'or job is remote (remote stdin not supported in v1)', }; } const buf = Buffer.from(input.text, 'utf8'); const ok = stdin.write(buf); return { action: 'write', job_id: input.job_id, ok, bytes_written: buf.length, }; } // kill if (meta.state !== 'running') { // Honest no-op: nothing was signalled. Note for jobs from // pre-group-kill somora versions: their workload may have // survived a "successful" kill (only the wrapper sh died) — // point the agent at a manual path instead of dead-ending. return { action: 'kill', job_id: input.job_id, ok: true, signal_sent: null, was_running: false, note: `job is already '${meta.state}' — no signal sent. If a stray workload from this job ` + `is still alive (jobs killed before 2026-07-21 only stopped the wrapper shell), find and ` + `kill it manually via exec (pgrep -f '').`, }; } const sig = (input.signal ?? 'SIGTERM') as NodeJS.Signals; if (meta.target === 'local') { const pid = getLivePid(input.job_id) ?? meta.pid; if (!pid) { throw new Error('process: no pid recorded for job — cannot kill'); } const r = killLocalJob(pid, sig); if (r.delivered) { await markJobKilled(ctx.agent, input.job_id, sig); clearLivePid(input.job_id); clearLiveStdin(input.job_id); } return { action: 'kill', job_id: input.job_id, ok: r.delivered, signal_sent: sig, was_running: r.delivered, }; } // Remote kill via short ssh exec. We strip the SIG- prefix that // Node uses (`SIGTERM`) since `kill -SIGTERM` is non-portable on // some shells; the bare name (`TERM`) works everywhere. if (meta.pid == null) { throw new Error('process: no pid recorded for remote job — cannot kill'); } const remoteSig = sig.replace(/^SIG/, ''); const r = await killRemoteJob(ctx.agent, meta.target, meta.pid, remoteSig); if (r.delivered) { await markJobKilled(ctx.agent, input.job_id, sig); releaseRemoteSlot(input.job_id); } return { action: 'kill', job_id: input.job_id, ok: r.delivered, signal_sent: sig, was_running: r.delivered, }; }, }; export function execTools(): ToolDefinition[] { return [exec, processTool] as ToolDefinition[]; }