Skip to content

Open-source, local-first AI agent for real work.

7.3 KB
Raw Blame
import type { RuntimeToolDefinition } from "@vetta/runtime-core/kernel";
import type { SandboxPermissionRequest } from "@vetta/runtime-core/sandbox";
import type { CodingToolRegistration } from "@vetta/runtime-tools";
import type { CodingAgentSandboxToolSet } from "../../composition/contracts/session-execution-environment.js";
import type { CodingAgentSandboxAuthorizationPort } from "./authorization-contract.js";
import {
	type CodingAgentSandboxPermissionContext,
	confirmSandboxPermission,
	isSensitiveSandboxRequest,
} from "./permission-policy.js";

export interface SandboxRuntimeToolOptions {
	readonly cwd: string;
	readonly authorization: CodingAgentSandboxAuthorizationPort;
	readonly toolSet: CodingAgentSandboxToolSet;
	/** Resolves the current session id at execute-time. Required for session-scoped grant cache. */
	readonly getSessionId?: () => string | undefined;
}

export function createSandboxToolRegistrations(options: SandboxRuntimeToolOptions): readonly CodingToolRegistration[] {
	return [
		withTool(options.toolSet.read, wrapWorkspaceGuard(options.toolSet.read.tool, options)),
		withTool(options.toolSet.write, wrapWorkspaceGuard(options.toolSet.write.tool, options)),
		withTool(options.toolSet.edit, wrapWorkspaceGuard(options.toolSet.edit.tool, options)),
		withTool(options.toolSet.command, wrapShellPermissionGuard(options.toolSet.command.tool, options)),
	];
}

function withTool<TInput extends object>(
	registration: CodingToolRegistration<TInput>,
	tool: RuntimeToolDefinition<TInput>,
): CodingToolRegistration<TInput> {
	return { ...registration, tool };
}

function extractPathFromParams(params: unknown): string | undefined {
	if (!params || typeof params !== "object" || !("path" in params)) return undefined;
	const pathValue = (params as { path?: unknown }).path;
	return typeof pathValue === "string" ? pathValue : undefined;
}

function extractCommandFromParams(params: unknown): string | undefined {
	if (!params || typeof params !== "object" || !("command" in params)) return undefined;
	const commandValue = (params as { command?: unknown }).command;
	return typeof commandValue === "string" ? commandValue : undefined;
}

function createPermissionContext(
	authorization: CodingAgentSandboxAuthorizationPort,
): CodingAgentSandboxPermissionContext {
	return { authorization };
}

export function wrapWorkspaceGuard<TInput extends object>(
	tool: RuntimeToolDefinition<TInput>,
	options: SandboxRuntimeToolOptions,
): RuntimeToolDefinition<TInput> {
	return {
		...tool,
		async execute(request) {
			const sessionId = options.getSessionId?.() ?? request.sessionId;
			const requestedPath = extractPathFromParams(request.input);
			if (requestedPath) {
				const access = await options.toolSet.hostServices.resolveWorkspacePathAccess(requestedPath, options.cwd);
				options.toolSet.hostServices.assertPathNotDenied(access.targetBoundary, tool.name);
				options.toolSet.hostServices.assertPathNotDenied(access.targetPath, tool.name);
				if (
					!access.allowed &&
					!(await isSessionReadableTarget(
						tool.name,
						sessionId,
						access.targetBoundary,
						options.toolSet.hostServices,
					))
				) {
					const capability: SandboxPermissionRequest["capability"] =
						tool.name === "read" ? "file.read" : "file.write";
					const permissionRequest: SandboxPermissionRequest = {
						capability,
						toolName: tool.name,
						target: requestedPath,
						resolvedTarget: access.targetBoundary,
						grantRoot: access.targetBoundary,
						reason: `${tool.name} target is outside the workspace sandbox`,
					};
					const cached = sessionId
						? options.toolSet.hostServices.findSessionGrant(sessionId, permissionRequest)
						: undefined;
					if (!cached) {
						const decision = await confirmSandboxPermission(
							createPermissionContext(options.authorization),
							sessionId,
							permissionRequest,
							options.toolSet.hostServices.isDeniedPath,
							request.signal,
						);
						if (decision === "deny") {
							const currentAccess = await options.toolSet.hostServices.resolveWorkspacePathAccess(
								requestedPath,
								options.cwd,
							);
							if (currentAccess.allowed) return tool.execute(request);
							throw new Error(
								`Access denied by sandbox: "${requestedPath}" is outside workspace root for tool "${tool.name}".` +
									`\nworkspace=${currentAccess.workspaceRoot}` +
									`\nresolved=${currentAccess.targetBoundary}`,
							);
						} else if (
							decision === "allow_session" &&
							sessionId &&
							!isSensitiveSandboxRequest(permissionRequest, options.toolSet.hostServices.isDeniedPath)
						) {
							options.toolSet.hostServices.addSessionGrant(sessionId, permissionRequest);
						}
					}
				}
			}
			return tool.execute(request);
		},
	};
}

/** 只有 read 享受会话级额外可读根目录;write/edit 仍逐次确认。 */
async function isSessionReadableTarget(
	toolName: string,
	sessionId: string | undefined,
	targetBoundary: string,
	hostServices: CodingAgentSandboxToolSet["hostServices"],
): Promise<boolean> {
	if (toolName !== "read" || !sessionId || !hostServices.findSessionReadRoot) return false;
	try {
		return (await hostServices.findSessionReadRoot(sessionId, targetBoundary)) !== undefined;
	} catch {
		return false;
	}
}

export function wrapShellPermissionGuard<TInput extends object>(
	tool: RuntimeToolDefinition<TInput>,
	options: SandboxRuntimeToolOptions,
): RuntimeToolDefinition<TInput> {
	return {
		...tool,
		async execute(request) {
			const command = extractCommandFromParams(request.input);
			const permissionRequests = command
				? options.toolSet.hostServices.collectShellWritePermissionRequests(command, options.cwd)
				: [];
			const sessionId = options.getSessionId?.() ?? request.sessionId;
			const allowWriteRoots: string[] = [];
			for (const permissionRequest of permissionRequests) {
				const cached = sessionId
					? options.toolSet.hostServices.findSessionGrant(sessionId, permissionRequest)
					: undefined;
				if (cached) {
					if (permissionRequest.grantRoot) allowWriteRoots.push(permissionRequest.grantRoot);
					continue;
				}
				const decision = await confirmSandboxPermission(
					createPermissionContext(options.authorization),
					sessionId,
					permissionRequest,
					options.toolSet.hostServices.isDeniedPath,
					request.signal,
				);
				if (decision === "deny") {
					throw new Error(
						`Access denied by sandbox: shell command requires write permission outside workspace.` +
							`\ntarget=${permissionRequest.target}` +
							`\nresolved=${permissionRequest.resolvedTarget}` +
							(permissionRequest.grantRoot ? `\ngrantRoot=${permissionRequest.grantRoot}` : ""),
					);
				}
				if (
					decision === "allow_session" &&
					sessionId &&
					!isSensitiveSandboxRequest(permissionRequest, options.toolSet.hostServices.isDeniedPath)
				) {
					options.toolSet.hostServices.addSessionGrant(sessionId, permissionRequest);
				}
				if (permissionRequest.grantRoot) allowWriteRoots.push(permissionRequest.grantRoot);
			}

			if (allowWriteRoots.length === 0) return tool.execute(request);
			const uniqueAllowWriteRoots = Array.from(new Set(allowWriteRoots));
			return options.toolSet.hostServices.runWithShellGrant(
				options.cwd,
				{
					allowReadRoots: uniqueAllowWriteRoots,
					allowWriteRoots: uniqueAllowWriteRoots,
				},
				() => tool.execute(request),
			);
		},
	};
}