@jalaxy33/pi-permission 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/schema.json ADDED
@@ -0,0 +1,348 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "title": "pi-permission configuration",
4
+ "description": "Config files read by pi-permission: ~/.pi/agent/permission.json, <project>/.pi/permission.json, and the shipped rules/index.json. A ruleset file uses the smaller shape in $defs/ruleSet.",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "properties": {
8
+ "$schema": {
9
+ "type": "string"
10
+ },
11
+ "description": {
12
+ "type": "string"
13
+ },
14
+ "rulesets": {
15
+ "description": "Named rulesets: inline rules or a path to a ruleset file. Merged by id; later files win.",
16
+ "type": "object",
17
+ "additionalProperties": {
18
+ "$ref": "#/$defs/rulesetRef"
19
+ }
20
+ },
21
+ "presets": {
22
+ "description": "Permission levels. Only the shipped index defines them.",
23
+ "type": "object",
24
+ "additionalProperties": {
25
+ "$ref": "#/$defs/preset"
26
+ }
27
+ },
28
+ "preset": {
29
+ "$ref": "#/$defs/presetRef"
30
+ },
31
+ "include": {
32
+ "description": "Extra rulesets. The last file that declares this list replaces it.",
33
+ "type": "array",
34
+ "items": {
35
+ "$ref": "#/$defs/includeEntry"
36
+ }
37
+ },
38
+ "defaultPermission": {
39
+ "$ref": "#/$defs/defaultPermission"
40
+ },
41
+ "enabled": {
42
+ "type": "object",
43
+ "additionalProperties": false,
44
+ "properties": {
45
+ "commands": {
46
+ "type": "boolean"
47
+ },
48
+ "paths": {
49
+ "type": "boolean"
50
+ }
51
+ }
52
+ },
53
+ "commands": {
54
+ "$ref": "#/$defs/commands"
55
+ },
56
+ "paths": {
57
+ "$ref": "#/$defs/pathRules"
58
+ },
59
+ "outsideCwd": {
60
+ "$ref": "#/$defs/outsideCwd"
61
+ }
62
+ },
63
+ "$defs": {
64
+ "commands": {
65
+ "type": "object",
66
+ "additionalProperties": false,
67
+ "properties": {
68
+ "deny": {
69
+ "type": "array",
70
+ "items": {
71
+ "$ref": "#/$defs/commandRule"
72
+ }
73
+ },
74
+ "ask": {
75
+ "type": "array",
76
+ "items": {
77
+ "$ref": "#/$defs/commandRule"
78
+ }
79
+ },
80
+ "allow": {
81
+ "type": "array",
82
+ "items": {
83
+ "$ref": "#/$defs/commandRule"
84
+ }
85
+ }
86
+ }
87
+ },
88
+ "commandRule": {
89
+ "type": "object",
90
+ "required": [
91
+ "id",
92
+ "program"
93
+ ],
94
+ "additionalProperties": false,
95
+ "properties": {
96
+ "id": {
97
+ "type": "string",
98
+ "minLength": 1
99
+ },
100
+ "program": {
101
+ "description": "Program name(s); `*` wildcard allowed (e.g. mkfs.*).",
102
+ "oneOf": [
103
+ {
104
+ "type": "string"
105
+ },
106
+ {
107
+ "type": "array",
108
+ "items": {
109
+ "type": "string"
110
+ },
111
+ "minItems": 1
112
+ }
113
+ ]
114
+ },
115
+ "args": {
116
+ "description": "Matches when any of these appears as a positional argument.",
117
+ "type": "array",
118
+ "items": {
119
+ "type": "string"
120
+ }
121
+ },
122
+ "subcommand": {
123
+ "description": "Program subcommand: matches the first positional, after global options that take a value (e.g. `git -C dir push`).",
124
+ "type": "array",
125
+ "items": {
126
+ "type": "string"
127
+ }
128
+ },
129
+ "anyFlag": {
130
+ "type": "array",
131
+ "items": {
132
+ "type": "string"
133
+ }
134
+ },
135
+ "allFlags": {
136
+ "type": "array",
137
+ "items": {
138
+ "type": "string"
139
+ }
140
+ },
141
+ "unlessFlag": {
142
+ "type": "array",
143
+ "items": {
144
+ "type": "string"
145
+ }
146
+ },
147
+ "argvRegex": {
148
+ "description": "Regex over the joined argv of one simple command.",
149
+ "type": "string"
150
+ },
151
+ "reason": {
152
+ "type": "string"
153
+ },
154
+ "terminate": {
155
+ "type": "boolean"
156
+ }
157
+ }
158
+ },
159
+ "pathRules": {
160
+ "type": "array",
161
+ "items": {
162
+ "$ref": "#/$defs/pathRule"
163
+ }
164
+ },
165
+ "pathRule": {
166
+ "type": "object",
167
+ "required": [
168
+ "id",
169
+ "patterns"
170
+ ],
171
+ "additionalProperties": false,
172
+ "properties": {
173
+ "id": {
174
+ "type": "string",
175
+ "minLength": 1
176
+ },
177
+ "patterns": {
178
+ "description": "Globs. No slash: basename anywhere. `~` or `/`: absolute. Otherwise relative to the project.",
179
+ "type": "array",
180
+ "items": {
181
+ "type": "string"
182
+ },
183
+ "minItems": 1
184
+ },
185
+ "except": {
186
+ "type": "array",
187
+ "items": {
188
+ "type": "string"
189
+ }
190
+ },
191
+ "action": {
192
+ "description": "What a match does. Default deny. deny beats allow and ask; allow beats ask.",
193
+ "enum": [
194
+ "allow",
195
+ "ask",
196
+ "deny"
197
+ ]
198
+ },
199
+ "ops": {
200
+ "type": "array",
201
+ "items": {
202
+ "enum": [
203
+ "read",
204
+ "write",
205
+ "delete"
206
+ ]
207
+ }
208
+ },
209
+ "onlyIfExists": {
210
+ "description": "Only protect paths that already exist. Default false.",
211
+ "type": "boolean"
212
+ },
213
+ "message": {
214
+ "type": "string"
215
+ }
216
+ }
217
+ },
218
+ "ruleSet": {
219
+ "type": "object",
220
+ "additionalProperties": false,
221
+ "properties": {
222
+ "$schema": {
223
+ "type": "string"
224
+ },
225
+ "description": {
226
+ "type": "string"
227
+ },
228
+ "commands": {
229
+ "$ref": "#/$defs/commands"
230
+ },
231
+ "paths": {
232
+ "$ref": "#/$defs/pathRules"
233
+ }
234
+ }
235
+ },
236
+ "rulesetRef": {
237
+ "oneOf": [
238
+ {
239
+ "type": "string",
240
+ "minLength": 1
241
+ },
242
+ {
243
+ "$ref": "#/$defs/ruleSet"
244
+ }
245
+ ]
246
+ },
247
+ "preset": {
248
+ "type": "object",
249
+ "required": [
250
+ "level",
251
+ "include"
252
+ ],
253
+ "additionalProperties": false,
254
+ "properties": {
255
+ "description": {
256
+ "type": "string"
257
+ },
258
+ "level": {
259
+ "type": "integer",
260
+ "minimum": 1
261
+ },
262
+ "include": {
263
+ "description": "Ruleset ids, flat. A preset never includes another preset.",
264
+ "type": "array",
265
+ "items": {
266
+ "type": "string",
267
+ "minLength": 1
268
+ },
269
+ "minItems": 1
270
+ }
271
+ }
272
+ },
273
+ "includeEntry": {
274
+ "oneOf": [
275
+ {
276
+ "type": "string",
277
+ "minLength": 1
278
+ },
279
+ {
280
+ "$ref": "#/$defs/includeEntryObject"
281
+ }
282
+ ]
283
+ },
284
+ "presetRef": {
285
+ "$ref": "#/$defs/includeEntry"
286
+ },
287
+ "defaultPermission": {
288
+ "type": "object",
289
+ "additionalProperties": false,
290
+ "properties": {
291
+ "commands": {
292
+ "enum": [
293
+ "allow",
294
+ "ask",
295
+ "deny"
296
+ ]
297
+ },
298
+ "paths": {
299
+ "enum": [
300
+ "allow",
301
+ "ask",
302
+ "deny"
303
+ ]
304
+ }
305
+ }
306
+ },
307
+ "outsideCwd": {
308
+ "type": "object",
309
+ "additionalProperties": false,
310
+ "properties": {
311
+ "mode": {
312
+ "enum": [
313
+ "allow",
314
+ "ask",
315
+ "block"
316
+ ]
317
+ },
318
+ "allowPaths": {
319
+ "description": "Extra globs that stay allowed outside the workspace.",
320
+ "type": "array",
321
+ "items": {
322
+ "type": "string"
323
+ }
324
+ }
325
+ }
326
+ },
327
+ "includeEntryObject": {
328
+ "type": "object",
329
+ "required": [
330
+ "id"
331
+ ],
332
+ "additionalProperties": false,
333
+ "properties": {
334
+ "id": {
335
+ "type": "string",
336
+ "minLength": 1
337
+ },
338
+ "remove": {
339
+ "description": "Rule ids to drop from the referenced ruleset.",
340
+ "type": "array",
341
+ "items": {
342
+ "type": "string"
343
+ }
344
+ }
345
+ }
346
+ }
347
+ }
348
+ }
package/src/argv.ts ADDED
@@ -0,0 +1,67 @@
1
+ /** Turns the argv of one simple command into the pieces rules match on. */
2
+
3
+ export interface Args {
4
+ program: string;
5
+ positional: string[];
6
+ flags: Set<string>;
7
+ joined: string;
8
+ }
9
+
10
+ const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
11
+
12
+ /** Options that consume the next token, so a subcommand keeps its position. */
13
+ const GLOBAL_VALUE_FLAGS: Record<string, Set<string>> = {
14
+ git: new Set(["-C", "-c", "--git-dir", "--work-tree", "--namespace", "--exec-path", "--super-prefix"]),
15
+ };
16
+
17
+ function programOf(argv: string[]): { program: string; index: number } | undefined {
18
+ for (let i = 0; i < argv.length; i++) {
19
+ const token = argv[i] as string;
20
+ if (ASSIGNMENT.test(token)) continue;
21
+ return { program: token.split("/").pop() as string, index: i };
22
+ }
23
+ return undefined;
24
+ }
25
+
26
+ export function parseArgs(argv: string[]): Args | undefined {
27
+ const head = programOf(argv);
28
+ if (!head) return undefined;
29
+
30
+ const positional: string[] = [];
31
+ const flags = new Set<string>();
32
+ const valueFlags = GLOBAL_VALUE_FLAGS[head.program];
33
+ let afterDoubleDash = false;
34
+
35
+ for (let i = head.index + 1; i < argv.length; i++) {
36
+ const token = argv[i] as string;
37
+ if (afterDoubleDash) {
38
+ positional.push(token);
39
+ continue;
40
+ }
41
+ if (token === "--") {
42
+ afterDoubleDash = true;
43
+ continue;
44
+ }
45
+ if (valueFlags?.has(token) && i + 1 < argv.length) {
46
+ i += 1;
47
+ continue;
48
+ }
49
+ if (token.startsWith("--")) {
50
+ flags.add(token);
51
+ flags.add(token.slice(2));
52
+ continue;
53
+ }
54
+ if (/^-[A-Za-z0-9]+$/.test(token)) {
55
+ flags.add(token);
56
+ for (const letter of token.slice(1)) flags.add(`-${letter}`);
57
+ continue;
58
+ }
59
+ if (token.startsWith("-")) {
60
+ flags.add(token);
61
+ continue;
62
+ }
63
+ positional.push(token);
64
+ }
65
+
66
+ return { program: head.program, positional, flags, joined: argv.join(" ") };
67
+ }
package/src/config.ts ADDED
@@ -0,0 +1,201 @@
1
+ import { homedir } from "node:os";
2
+ import { dirname, join } from "node:path";
3
+ import { compileOutside } from "./outside";
4
+ import { compilePathRules } from "./paths";
5
+ import { compileRules } from "./match";
6
+ import { isObject } from "./validate";
7
+ import {
8
+ collectOwn,
9
+ collectSelected,
10
+ defaultConfigPath,
11
+ loadPresets,
12
+ loadRuleSetRef,
13
+ mergeRules,
14
+ readJson,
15
+ resolveSelection,
16
+ type LoadedRuleSet,
17
+ } from "./rulesets";
18
+ import type {
19
+ Action,
20
+ ConfigFile,
21
+ IncludeRef,
22
+ PresetRef,
23
+ ResolvedConfig,
24
+ RuleSet,
25
+ } from "./types";
26
+
27
+ export function globalConfigPath(): string {
28
+ const home = process.env.PI_PERMISSION_HOME ?? homedir();
29
+ return join(home, ".pi", "agent", "permission.json");
30
+ }
31
+
32
+ export function projectConfigPath(cwd: string): string {
33
+ return join(cwd, ".pi", "permission.json");
34
+ }
35
+
36
+ /** Test and tooling hook: point the loader at explicit files instead of the real home. */
37
+ export interface LoadConfigOptions {
38
+ globalPath?: string;
39
+ projectPath?: string;
40
+ }
41
+
42
+ const ACTIONS: Action[] = ["allow", "ask", "deny"];
43
+
44
+ function isAction(value: unknown): value is Action {
45
+ return typeof value === "string" && (ACTIONS as string[]).includes(value);
46
+ }
47
+
48
+ function shapeError(file: ConfigFile, path: string): string | undefined {
49
+ if (file.rulesets !== undefined && !isObject(file.rulesets)) return `${path}: "rulesets" must be an object`;
50
+ if (file.include !== undefined && !Array.isArray(file.include)) return `${path}: "include" must be an array`;
51
+ if (file.commands !== undefined && !isObject(file.commands)) return `${path}: "commands" must be an object`;
52
+ if (file.paths !== undefined && !Array.isArray(file.paths)) return `${path}: "paths" must be an array`;
53
+ if (
54
+ file.preset !== undefined &&
55
+ typeof file.preset !== "string" &&
56
+ !(isObject(file.preset) && typeof file.preset.id === "string")
57
+ ) {
58
+ return `${path}: "preset" must be a name or an { id, remove } object`;
59
+ }
60
+ return undefined;
61
+ }
62
+
63
+ function pickOutside(scopes: ConfigFile[]) {
64
+ const merged: { mode?: "allow" | "ask" | "block"; allowPaths?: string[] } = {};
65
+ for (const scope of scopes) {
66
+ if (scope.outsideCwd?.mode) merged.mode = scope.outsideCwd.mode;
67
+ if (scope.outsideCwd?.allowPaths) merged.allowPaths = scope.outsideCwd.allowPaths;
68
+ }
69
+ return merged.mode || merged.allowPaths ? merged : undefined;
70
+ }
71
+
72
+ function emptyConfig(fatal: string, warnings: string[]): ResolvedConfig {
73
+ return {
74
+ enabled: { commands: true, paths: false },
75
+ commands: { deny: [], ask: [], allow: [] },
76
+ paths: [],
77
+ outside: compileOutside(undefined),
78
+ defaultPermission: { commands: "allow", paths: "allow" },
79
+ rulesets: [],
80
+ sources: [],
81
+ fatal,
82
+ warnings,
83
+ };
84
+ }
85
+
86
+ export function loadConfig(
87
+ cwd: string,
88
+ trusted: boolean,
89
+ options: LoadConfigOptions = {},
90
+ ): ResolvedConfig {
91
+ const warnings: string[] = [];
92
+ const sources: string[] = [];
93
+
94
+ const builtinPath = defaultConfigPath();
95
+ const builtin = readJson(builtinPath, warnings, "config");
96
+ if (!builtin) return emptyConfig(`pi-permission: cannot read ${builtinPath}`, warnings);
97
+ sources.push(builtinPath);
98
+
99
+ const scopes: Array<{ path: string; baseDir: string; file: ConfigFile }> = [
100
+ { path: builtinPath, baseDir: dirname(builtinPath), file: builtin },
101
+ ];
102
+
103
+ const scopePaths = [options.globalPath ?? globalConfigPath()];
104
+ if (trusted) scopePaths.push(options.projectPath ?? projectConfigPath(cwd));
105
+ for (const path of scopePaths) {
106
+ const file = readJson(path, warnings, "config");
107
+ if (!file) continue;
108
+ sources.push(path);
109
+ scopes.push({ path, baseDir: dirname(path), file });
110
+ }
111
+
112
+ let fatal: string | undefined;
113
+ const rulesets = new Map<string, LoadedRuleSet>();
114
+ for (const scope of scopes) {
115
+ const shape = shapeError(scope.file, scope.path);
116
+ if (shape) {
117
+ fatal ??= `pi-permission: ${shape}`;
118
+ continue;
119
+ }
120
+ for (const [id, ref] of Object.entries(scope.file.rulesets ?? {})) {
121
+ const previous = rulesets.get(id);
122
+ if (previous) {
123
+ warnings.push(`${scope.path}: ruleset "${id}" redefines ${previous.source ?? "an earlier definition"}`);
124
+ }
125
+ rulesets.set(id, loadRuleSetRef(id, ref as RuleSet | string, scope.baseDir, warnings));
126
+ }
127
+ }
128
+
129
+ const presets = loadPresets(builtin, builtinPath, warnings);
130
+ for (const scope of scopes.slice(1)) {
131
+ if (scope.file.presets) warnings.push(`${scope.path}: presets are shipped only, ignored`);
132
+ }
133
+
134
+ let presetRef: PresetRef | undefined;
135
+ let includeList: IncludeRef[] = [];
136
+ for (const scope of scopes) {
137
+ if (shapeError(scope.file, scope.path)) continue;
138
+ if (scope.file.preset !== undefined) presetRef = scope.file.preset;
139
+ if (scope.file.include !== undefined) includeList = scope.file.include;
140
+ }
141
+
142
+ const selection = resolveSelection(presetRef, includeList, rulesets, presets);
143
+ warnings.push(...selection.warnings);
144
+
145
+ const selected = collectSelected(selection, rulesets);
146
+ warnings.push(...selected.warnings);
147
+
148
+ const own = collectOwn(scopes);
149
+ warnings.push(...own.warnings);
150
+
151
+ const merged = mergeRules(selected, own);
152
+ warnings.push(...merged.warnings);
153
+
154
+ const compile = (rules: typeof merged.commands.deny) => {
155
+ const result = compileRules(rules);
156
+ warnings.push(...result.warnings);
157
+ return result.rules;
158
+ };
159
+
160
+ const compiledPaths = compilePathRules(merged.paths);
161
+ warnings.push(...compiledPaths.warnings);
162
+
163
+ const defaults: { commands: Action; paths: Action } = { commands: "allow", paths: "allow" };
164
+ for (const scope of scopes) {
165
+ const value = scope.file.defaultPermission;
166
+ if (!value) continue;
167
+ if (value.commands !== undefined) {
168
+ if (isAction(value.commands)) defaults.commands = value.commands;
169
+ else warnings.push(`${scope.path}: defaultPermission.commands must be allow, ask or deny`);
170
+ }
171
+ if (value.paths !== undefined) {
172
+ if (isAction(value.paths)) defaults.paths = value.paths;
173
+ else warnings.push(`${scope.path}: defaultPermission.paths must be allow, ask or deny`);
174
+ }
175
+ }
176
+
177
+ const enabled = { commands: true, paths: true };
178
+ for (const scope of scopes) {
179
+ if (scope.file.enabled?.commands !== undefined) enabled.commands = scope.file.enabled.commands;
180
+ if (scope.file.enabled?.paths !== undefined) enabled.paths = scope.file.enabled.paths;
181
+ }
182
+
183
+ fatal ??= selection.fatal ?? selected.fatal ?? own.fatal;
184
+
185
+ return {
186
+ enabled,
187
+ commands: {
188
+ deny: compile(merged.commands.deny),
189
+ ask: compile(merged.commands.ask),
190
+ allow: compile(merged.commands.allow),
191
+ },
192
+ paths: compiledPaths.rules,
193
+ outside: compileOutside(pickOutside(scopes.map((scope) => scope.file))),
194
+ defaultPermission: defaults,
195
+ preset: selection.preset,
196
+ rulesets: selected.active,
197
+ sources,
198
+ fatal,
199
+ warnings,
200
+ };
201
+ }
package/src/decide.ts ADDED
@@ -0,0 +1,90 @@
1
+ import { parseArgs, type Args } from "./argv";
2
+ import { matchRule } from "./match";
3
+ import { MAX_NESTING, scriptAfterC } from "./ops";
4
+ import { tokenize } from "./tokenize";
5
+ import type { Action, CompiledRule, GateRules, Verdict } from "./types";
6
+
7
+ const RANK: Record<Action, number> = { allow: 0, ask: 1, deny: 2 };
8
+
9
+ function verdict(action: Action, matched: boolean): Verdict {
10
+ return { action, reasons: [], grants: [], terminate: false, matched };
11
+ }
12
+
13
+ /** A session grant covers one rule with the same flags, not every command it matches. */
14
+ function grantKey(rule: CompiledRule, args: Args): string {
15
+ return `rule:${rule.id}:${[...args.flags].filter(canonicalFlag).sort().join(",")}`;
16
+ }
17
+
18
+ /** `-rf` and `-fr` are the same flags; keep only the letters for grant keys. */
19
+ function canonicalFlag(flag: string): boolean {
20
+ return flag.startsWith("-") && !/^-[A-Za-z0-9]{2,}$/.test(flag);
21
+ }
22
+
23
+ /** Fallback grants key on the program: approving one unmatched command grants that program. */
24
+ function fallbackGrant(args: Args): string {
25
+ return `default:${args.program}:${[...args.flags].filter(canonicalFlag).sort().join(",")}`;
26
+ }
27
+
28
+ function decideOne(argv: string[], rules: GateRules, fallback: Action): Verdict {
29
+ const args = parseArgs(argv);
30
+ // A line of assignments has no program; there is nothing to ask about.
31
+ if (!args) return verdict("allow", true);
32
+
33
+ // `cd` is structural: later commands are judged in the directory it reaches,
34
+ // so it stays allowed even in a deny-by-default setup.
35
+ if (args.program === "cd" || args.program === "pushd") return verdict("allow", true);
36
+
37
+ const deny = rules.deny.filter((rule) => matchRule(rule, args));
38
+ if (deny.length > 0) {
39
+ const result = verdict("deny", true);
40
+ result.reasons = deny.map((rule) => rule.reason ?? rule.id);
41
+ result.terminate = deny.some((rule) => rule.terminate === true);
42
+ return result;
43
+ }
44
+
45
+ if (rules.allow.some((rule) => matchRule(rule, args))) return verdict("allow", true);
46
+
47
+ const ask = rules.ask.filter((rule) => matchRule(rule, args));
48
+ if (ask.length > 0) {
49
+ const result = verdict("ask", true);
50
+ result.reasons = ask.map((rule) => rule.reason ?? rule.id);
51
+ result.grants = ask.map((rule) => grantKey(rule, args));
52
+ return result;
53
+ }
54
+
55
+ if (fallback === "allow") return verdict("allow", false);
56
+
57
+ const result = verdict(fallback, false);
58
+ result.reasons = [`no command rule allows ${args.program}`];
59
+ if (fallback === "ask") result.grants = [fallbackGrant(args)];
60
+ return result;
61
+ }
62
+
63
+ function mergeInto(target: Verdict, source: Verdict): void {
64
+ for (const reason of source.reasons) {
65
+ if (!target.reasons.includes(reason)) target.reasons.push(reason);
66
+ }
67
+ for (const grant of source.grants) {
68
+ if (!target.grants.includes(grant)) target.grants.push(grant);
69
+ }
70
+ if (RANK[source.action] > RANK[target.action]) target.action = source.action;
71
+ if (source.terminate) target.terminate = true;
72
+ if (source.matched) target.matched = true;
73
+ }
74
+
75
+ /**
76
+ * Strictest verdict across every simple command, including `sh -c` scripts.
77
+ * `fallback` is `defaultPermission.commands`: what an unmatched program does.
78
+ */
79
+ export function decide(command: string, rules: GateRules, fallback: Action = "allow", depth = 0): Verdict {
80
+ const merged: Verdict = { action: "allow", reasons: [], grants: [], terminate: false, matched: false };
81
+
82
+ for (const simple of tokenize(command)) {
83
+ mergeInto(merged, decideOne(simple.argv, rules, fallback));
84
+
85
+ const script = scriptAfterC(simple.argv);
86
+ if (script && depth < MAX_NESTING) mergeInto(merged, decide(script, rules, fallback, depth + 1));
87
+ }
88
+
89
+ return merged;
90
+ }