import { type PendingToolCall, type PermissionDecision } from '@moxxy/sdk'; import { z } from 'zod/v3'; /** * Flat shape used by the policy file and the engine's mutator API. * Different from SDK's `PermissionRule` (which uses a nested `pattern`). * The engine has its own type because the persisted format is intentionally * simple and stable. */ export interface PolicyRule { readonly name: string; /** * Map of tool-input field name → regex source string. Each value is compiled * with `new RegExp(value)` and tested with `.test(candidate)`, which is an * UNANCHORED (substring / partial) match by design: the pattern matches if it * occurs anywhere in the field's string form, not only when it spans the whole * value. This is a deliberate, stable contract — existing user permission * files rely on it, so it is never silently anchored. * * Consequence: an allow rule like `{ Read: { path: 'config' } }` matches * `/etc/passwd#config` and `~/.ssh/config-backup` too. A rule that wants a * FULL match must supply its own anchors, e.g. `'^/safe/dir/.*$'`. (Note this * differs from {@link nameMatches}, which anchors its glob with `^...$` * because a glob is a whole-name convention, not an author-supplied regex.) */ readonly inputMatches?: Record; /** * Field name to path PREFIX. Anchored and path-aware, unlike * {@link inputMatches}: `{ path: '/srv/app' }` matches `/srv/app/x` but not * `/srv/apple` and not `/tmp/srv/app`. * * Exists because the unanchored regex above is a trap for whoever writes an * organisation's policy. The obvious `{ Read: { path: '/etc' } }` reads like * "under /etc" and actually means "contains /etc anywhere", which as a DENY * rule over-blocks and as an ALLOW rule grants far more than intended. Paths * are normalised before comparison, so `..` cannot walk out of the prefix. */ readonly inputPathPrefix?: Record; /** * Field name to a glob, anchored whole-value. `*` matches within a path * segment, `**` across segments, `?` one character. */ readonly inputGlob?: Record; /** * Field name to the exact value it must have; `null` stands for a field the * call leaves out. What "Always allow" saves for a tool that narrows its * grant to the same request (`PermissionRule.sessionScope`). */ readonly inputEquals?: Readonly>; readonly reason?: string; } export declare const permissionPolicySchema: z.ZodObject<{ allow: z.ZodDefault>; inputPathPrefix: z.ZodOptional>; inputGlob: z.ZodOptional>; inputEquals: z.ZodOptional>; reason: z.ZodOptional; }, "strip", z.ZodTypeAny, { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }, { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }>, "many">>; deny: z.ZodDefault>; inputPathPrefix: z.ZodOptional>; inputGlob: z.ZodOptional>; inputEquals: z.ZodOptional>; reason: z.ZodOptional; }, "strip", z.ZodTypeAny, { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }, { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }>, "many">>; }, "strip", z.ZodTypeAny, { allow: { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }[]; deny: { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }[]; }, { allow?: { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }[] | undefined; deny?: { name: string; reason?: string | undefined; inputMatches?: Record | undefined; inputPathPrefix?: Record | undefined; inputGlob?: Record | undefined; inputEquals?: Record | undefined; }[] | undefined; }>; export type PermissionPolicy = z.infer; export declare class PermissionEngine { private policy; private policyPath; private mutex; constructor(policy?: PermissionPolicy, policyPath?: string | null); static load(policyPath: string): Promise; /** * Rules from CONFIG, which the mutators never touch and `save()` never * writes. When they come from the system scope (with the path in `locked:`) * they are the operator's policy, and the point is that a user cannot get rid * of them: not by editing `~/.moxxy/permissions.json`, not by answering * "allow always", not by deleting the file. */ private immutable; /** * Install the config-supplied layer. Separate from `policy` because the two * have different owners and different lifetimes: this one is re-derived from * config on every boot, the other is a user file the agent writes to. */ setImmutableRules(rules: PermissionPolicy): void; /** The config-supplied layer currently in force. */ getImmutableRules(): PermissionPolicy; /** * Decision order, and each step is load-bearing: * 1. immutable deny: the operator's policy wins over everything. * 2. file deny: the user's own standing denials. * 3. immutable allow: an operator-granted exception. * 4. file allow: "allow always" answers. * * Deny always precedes allow within a layer, and the immutable layer always * precedes the file. Checking the file first would let an "allow always" * answer, which the agent itself can provoke, silently defeat a rule the * operator pushed. */ check(call: PendingToolCall): PermissionDecision | null; addAllow(rule: PolicyRule): Promise; addDeny(rule: PolicyRule): Promise; /** Remove every rule (allow + deny) whose name matches exactly. Returns the count removed. */ removeByName(name: string): Promise; clear(): Promise; private persist; get policySnapshot(): PermissionPolicy; } //# sourceMappingURL=engine.d.ts.map