import { n as matchesAnyGlobPattern, t as compileGlobPatterns } from "./glob-pattern-DFVWJ-hh.js";
import { a as expandToolGroups, c as normalizeToolPolicyName, l as readToolAllowlistIntersection } from "./tool-policy-shared-BAaV7zK-.js";
//#region src/agents/tool-policy-match.ts
/**
* Runtime matcher for sandbox tool policies. Deny patterns always win, then
* an empty allow list means "allow everything not denied".
*/
/** Snapshot one synchronous filtering operation; execution checks must prepare current policy. */
function createToolPolicyMatcher(policy, writeAllowsApplyPatch = true) {
	if (!policy) return () => true;
	const deny = compileGlobPatterns({
		raw: expandToolGroups(policy.deny ?? []),
		normalize: normalizeToolPolicyName
	});
	const allow = compileGlobPatterns({
		raw: expandToolGroups(policy.allow ?? []),
		normalize: normalizeToolPolicyName
	});
	return (name) => {
		const normalized = normalizeToolPolicyName(name);
		if (matchesAnyGlobPattern(normalized, deny)) return false;
		if (allow.length === 0) return true;
		if (matchesAnyGlobPattern(normalized, allow)) return true;
		if (writeAllowsApplyPatch && normalized === "apply_patch" && matchesAnyGlobPattern("write", allow)) return true;
		return false;
	};
}
/** Return whether one tool name is allowed by a single sandbox policy. */
function isToolAllowedByPolicyName(name, policy) {
	if (!policy) return true;
	return createToolPolicyMatcher(policy)(name);
}
/** Runtime caps deny empty lists and preserve every independently merged restriction. */
function createRuntimeToolMatcher(toolsAllow, writeAllowsApplyPatch = true) {
	const matchers = (toolsAllow === void 0 ? [] : readToolAllowlistIntersection(toolsAllow) ?? [toolsAllow]).map((allow) => allow.length > 0 ? createToolPolicyMatcher({ allow }, writeAllowsApplyPatch) : () => false);
	return (name) => matchers.every((matches) => matches(name));
}
function isRuntimeToolAllowed(name, toolsAllow) {
	return toolsAllow === void 0 || (readToolAllowlistIntersection(toolsAllow) ?? [toolsAllow]).every((allow) => allow.length > 0 && isToolAllowedByPolicyName(name, { allow }));
}
/** Filter runtime tools by policy without rebuilding its patterns for each tool. */
function filterToolsByPolicy(tools, policy) {
	if (!policy) return tools;
	if (tools.length === 0) return [];
	const matches = createToolPolicyMatcher(policy);
	return tools.filter((tool) => matches(tool.name));
}
/** Return whether one tool name is allowed by every active sandbox policy. */
function isToolAllowedByPolicies(name, policies) {
	return policies.every((policy) => isToolAllowedByPolicyName(name, policy));
}
//#endregion
export { isToolAllowedByPolicies as a, isRuntimeToolAllowed as i, createToolPolicyMatcher as n, isToolAllowedByPolicyName as o, filterToolsByPolicy as r, createRuntimeToolMatcher as t };
