mirror of
https://github.com/qwibitai/nanoclaw.git
synced 2026-07-09 18:57:08 +08:00
3b8240a91b
install_packages and add_mcp_server already did the right thing on approve
(install auto-rebuilt+killed, add_mcp_server just killed), so request_rebuild
was redundant plumbing agents sometimes called after an install — wasting an
admin approval round-trip. Delete it end-to-end:
- container/agent-runner/src/mcp-tools/self-mod.ts: remove requestRebuild
tool + registration; update install_packages description.
- src/modules/self-mod/{request,apply,index}.ts: drop handleRequestRebuild
+ applyRequestRebuild + registrations; rewrite the rebuild-failed notify
to point admins at retrying install_packages instead.
- src/modules/{approvals,self-mod}/{agent,project}.md and skill/self-
customize/SKILL.md: scrub agent-facing references; clarify that
add_mcp_server needs no rebuild (bun runs TS directly).
- docs/{module-contract,architecture-diagram,checklist,db-central,shared-
source,v1-vs-v2/*}.md, CLAUDE.md, pending-approvals migration comment,
approvals/index.ts docstring, REFACTOR.md: trailing references.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
92 lines
3.6 KiB
TypeScript
92 lines
3.6 KiB
TypeScript
/**
|
|
* Delivery-action handlers for agent-initiated self-modification requests.
|
|
*
|
|
* Two actions the container can write into messages_out (via the self-mod
|
|
* MCP tools): install_packages, add_mcp_server. Each one validates input
|
|
* and queues an approval request. The admin's approval triggers the
|
|
* matching approval handler in ./apply.ts, which also performs the
|
|
* required follow-up (rebuild+restart for install_packages, restart-only
|
|
* for add_mcp_server).
|
|
*
|
|
* Host-side sanitization for install_packages is defense-in-depth — the MCP
|
|
* tool validates first. Both layers matter: the DB row carries the payload
|
|
* verbatim through to shell exec on apply.
|
|
*/
|
|
import { getAgentGroup } from '../../db/agent-groups.js';
|
|
import { log } from '../../log.js';
|
|
import type { Session } from '../../types.js';
|
|
import { notifyAgent, requestApproval } from '../approvals/index.js';
|
|
|
|
export async function handleInstallPackages(content: Record<string, unknown>, session: Session): Promise<void> {
|
|
const agentGroup = getAgentGroup(session.agent_group_id);
|
|
if (!agentGroup) {
|
|
notifyAgent(session, 'install_packages failed: agent group not found.');
|
|
return;
|
|
}
|
|
|
|
const apt = (content.apt as string[]) || [];
|
|
const npm = (content.npm as string[]) || [];
|
|
const reason = (content.reason as string) || '';
|
|
|
|
const APT_RE = /^[a-z0-9][a-z0-9._+-]*$/;
|
|
const NPM_RE = /^(@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/;
|
|
const MAX_PACKAGES = 20;
|
|
if (apt.length + npm.length === 0) {
|
|
notifyAgent(session, 'install_packages failed: at least one apt or npm package is required.');
|
|
return;
|
|
}
|
|
if (apt.length + npm.length > MAX_PACKAGES) {
|
|
notifyAgent(session, `install_packages failed: max ${MAX_PACKAGES} packages per request.`);
|
|
return;
|
|
}
|
|
const invalidApt = apt.find((p) => !APT_RE.test(p));
|
|
if (invalidApt) {
|
|
notifyAgent(session, `install_packages failed: invalid apt package name "${invalidApt}".`);
|
|
log.warn('install_packages: invalid apt package rejected', { pkg: invalidApt });
|
|
return;
|
|
}
|
|
const invalidNpm = npm.find((p) => !NPM_RE.test(p));
|
|
if (invalidNpm) {
|
|
notifyAgent(session, `install_packages failed: invalid npm package name "${invalidNpm}".`);
|
|
log.warn('install_packages: invalid npm package rejected', { pkg: invalidNpm });
|
|
return;
|
|
}
|
|
|
|
const packageList = [...apt.map((p) => `apt: ${p}`), ...npm.map((p) => `npm: ${p}`)].join(', ');
|
|
await requestApproval({
|
|
session,
|
|
agentName: agentGroup.name,
|
|
action: 'install_packages',
|
|
payload: { apt, npm, reason },
|
|
title: 'Install Packages Request',
|
|
question: `Agent "${agentGroup.name}" is attempting to install a package + rebuild container:\n${packageList}${reason ? `\nReason: ${reason}` : ''}`,
|
|
});
|
|
}
|
|
|
|
export async function handleAddMcpServer(content: Record<string, unknown>, session: Session): Promise<void> {
|
|
const agentGroup = getAgentGroup(session.agent_group_id);
|
|
if (!agentGroup) {
|
|
notifyAgent(session, 'add_mcp_server failed: agent group not found.');
|
|
return;
|
|
}
|
|
const serverName = content.name as string;
|
|
const command = content.command as string;
|
|
if (!serverName || !command) {
|
|
notifyAgent(session, 'add_mcp_server failed: name and command are required.');
|
|
return;
|
|
}
|
|
await requestApproval({
|
|
session,
|
|
agentName: agentGroup.name,
|
|
action: 'add_mcp_server',
|
|
payload: {
|
|
name: serverName,
|
|
command,
|
|
args: (content.args as string[]) || [],
|
|
env: (content.env as Record<string, string>) || {},
|
|
},
|
|
title: 'Add MCP Request',
|
|
question: `Agent "${agentGroup.name}" is attempting to add a new MCP server:\n${serverName} (${command})`,
|
|
});
|
|
}
|