Elffuss BraidHub

kikocis/continue public

main · 4.2 KiBRawPermalink
1import { logger } from "../util/logger.js";
2
3import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
4import {
5 loadPermissionsYaml,
6 yamlConfigToPolicies,
7} from "./permissionsYamlLoader.js";
8import { type ToolPermissionPolicy } from "./types.js";
9
10export interface PermissionSources {
11 /** Command line flags - highest precedence */
12 commandLineFlags?: {
13 allow?: string[];
14 ask?: string[];
15 exclude?: string[];
16 };
17 /** Config.yaml permissions - second precedence (not implemented yet) */
18 configPermissions?: ToolPermissionPolicy[];
19 /** ~/.continue/permissions.yaml - third precedence */
20 personalSettings?: boolean; // Whether to load from permissions.yaml
21 /** Default policies - lowest precedence */
22 useDefaults?: boolean;
23}
24
25/**
26 * Resolves permission policies from all sources according to precedence rules.
27 * This function implements the clean precedence logic from the specification:
28 *
29 * 1. Command line flags (highest)
30 * 2. Permissions in config.yaml (when implemented)
31 * 3. Permissions in ~/.continue/permissions.yaml
32 * 4. Default policies (lowest)
33 *
34 * Earlier sources completely override later ones on a per-tool basis.
35 */
36export function resolvePermissionPrecedence(
37 sources: PermissionSources,
38): ToolPermissionPolicy[] {
39 const layers: {
40 name: string;
41 policies: ToolPermissionPolicy[];
42 }[] = [];
43
44 // Layer 4: Default policies (lowest precedence)
45 if (sources.useDefaults !== false) {
46 layers.push({
47 name: "defaults",
48 policies: [...DEFAULT_TOOL_POLICIES],
49 });
50 }
51
52 // Layer 3: Personal settings from ~/.continue/permissions.yaml
53 if (sources.personalSettings !== false) {
54 const yamlConfig = loadPermissionsYaml();
55 if (yamlConfig) {
56 const yamlPolicies = yamlConfigToPolicies(yamlConfig);
57 if (yamlPolicies.length > 0) {
58 layers.push({
59 name: "personal-settings",
60 policies: yamlPolicies,
61 });
62 }
63 }
64 }
65
66 // Layer 2: Config permissions (when implemented)
67 if (sources.configPermissions) {
68 layers.push({
69 name: "config",
70 policies: sources.configPermissions,
71 });
72 }
73
74 // Layer 1: Command line flags (highest precedence)
75 if (sources.commandLineFlags) {
76 const cliPolicies = commandLineFlagsToPolicies(sources.commandLineFlags);
77 if (cliPolicies.length > 0) {
78 layers.push({
79 name: "cli-flags",
80 policies: cliPolicies,
81 });
82 }
83 }
84
85 // Combine layers with proper precedence
86 const combinedPolicies = combineLayersWithPrecedence(layers);
87
88 logger.debug("Resolved permission precedence", {
89 layers: layers.map((l) => ({ name: l.name, count: l.policies.length })),
90 totalPolicies: combinedPolicies.length,
91 });
92
93 return combinedPolicies;
94}
95
96/**
97 * Converts command line flags to policies
98 */
99function commandLineFlagsToPolicies(flags: {
100 allow?: string[];
101 ask?: string[];
102 exclude?: string[];
103}): ToolPermissionPolicy[] {
104 const policies: ToolPermissionPolicy[] = [];
105
106 // Order matters for CLI flags too
107 if (flags.exclude) {
108 for (const tool of flags.exclude) {
109 const normalizedName = tool;
110 policies.push({ tool: normalizedName, permission: "exclude" });
111 }
112 }
113
114 if (flags.ask) {
115 for (const tool of flags.ask) {
116 const normalizedName = tool;
117 policies.push({ tool: normalizedName, permission: "ask" });
118 }
119 }
120
121 if (flags.allow) {
122 for (const tool of flags.allow) {
123 const normalizedName = tool;
124 policies.push({ tool: normalizedName, permission: "allow" });
125 }
126 }
127
128 return policies;
129}
130
131/**
132 * Combines permission layers with proper precedence.
133 * Higher precedence layers come later in the array and are prepended to the result.
134 */
135function combineLayersWithPrecedence(
136 layers: Array<{ name: string; policies: ToolPermissionPolicy[] }>,
137): ToolPermissionPolicy[] {
138 // Start with empty array
139 let combined: ToolPermissionPolicy[] = [];
140
141 // Add layers in order (lowest to highest precedence)
142 // Since we're building from lowest to highest, and we want first-match-wins,
143 // we prepend higher precedence policies
144 for (const layer of layers) {
145 // Prepend this layer's policies (higher precedence at the front)
146 combined = [...layer.policies, ...combined];
147 }
148
149 return combined;
150}