fce074726f
Follow-up to #23716. Moves ConfigPermission.Info from zod-first (with a preprocess hack) to Effect Schema canonical using Schema.StructWithRest + Schema.decodeTo, and deletes the now-unused ZodPreprocess plumbing. Core change: rule precedence in `Permission.fromConfig` now sorts top-level keys so wildcard permissions (e.g. `*`, `mcp_*`) come before specific ones (e.g. `bash`, `edit`). Combined with `findLast` in evaluate(), this gives the intuitive semantic 'specific tool rules override the `*` fallback' regardless of the user's JSON key order. This silently fixes the previously-broken case `{bash: "allow", "*": "deny"}` (which under the old semantics denied bash because `*` came last). Once rule precedence no longer depends on JSON insertion order, the `__originalKeys` + ZodPreprocess hack can go — StructWithRest's natural canonicalisation is fine because fromConfig sorts anyway. - src/config/permission.ts: rewrite. InputObject is StructWithRest with known permission keys (read/edit/bash/... as Rule, todowrite/webfetch/... as Action-only for type narrowing) + Record rest. Schema.decodeTo normalises the Action shorthand into { "*": action }. .zod is derived — walker already carries the decodeTo transform. - src/config/config.ts, src/config/agent.ts: reference ConfigPermission.Info directly instead of via Schema.Any + ZodOverride. The Effect decoder now applies the permission transform at load time. - src/permission/index.ts: fromConfig sorts wildcards-before-specifics at top level. Sub-pattern order inside a tool key is preserved (documented `*` first, specifics after). - src/util/effect-zod.ts: delete ZodPreprocess symbol, its walkUncached branch, and the TODO comment. Zero remaining consumers. - test/permission/next.test.ts: 6 new tests pinning the new semantics — order-independent precedence, wildcard-as-fallback, sub-pattern order preservation, canonical documented-example regression guard. - test/config/config.test.ts: updated the "preserves key order" test to reflect the new canonical output shape (declaration-order known fields, then input-order rest keys). Behavioural guarantees live in the new permission tests. - test/util/effect-zod.test.ts: delete the ZodPreprocess describe block (~115 lines of tests for the now-removed feature). SDK diff vs dev: - Removed `__originalKeys?: Array<string>` (internal leak). - Catchall cleaned up (no unrelated `Array<string>`). - Known-field types preserved (autocomplete + narrowing). - Only shape change: PermissionConfig union order swap (commutative). Safety audit: no config, test, or doc in the repo (including all 16 translations) exercises the pattern where specifics come before wildcards at the top level. The only configs whose behaviour changes are ones that were silently broken.
80 lines
3.3 KiB
TypeScript
80 lines
3.3 KiB
TypeScript
export * as ConfigPermission from "./permission"
|
|
import { Schema, SchemaGetter } from "effect"
|
|
import { zod } from "@/util/effect-zod"
|
|
import { withStatics } from "@/util/schema"
|
|
|
|
export const Action = Schema.Literals(["ask", "allow", "deny"])
|
|
.annotate({ identifier: "PermissionActionConfig" })
|
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
export type Action = Schema.Schema.Type<typeof Action>
|
|
|
|
export const Object = Schema.Record(Schema.String, Action)
|
|
.annotate({ identifier: "PermissionObjectConfig" })
|
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
export type Object = Schema.Schema.Type<typeof Object>
|
|
|
|
export const Rule = Schema.Union([Action, Object])
|
|
.annotate({ identifier: "PermissionRuleConfig" })
|
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
export type Rule = Schema.Schema.Type<typeof Rule>
|
|
|
|
// Known permission keys get explicit types — most are full Rule (either a
|
|
// single Action or a per-pattern object), but a handful of tools take no
|
|
// sub-target patterns and are Action-only. Unknown keys fall through the
|
|
// Record rest signature as Rule.
|
|
//
|
|
// StructWithRest canonicalises key order on decode (known first, then rest),
|
|
// which used to require the `__originalKeys` preprocess hack because
|
|
// `Permission.fromConfig` depended on the user's insertion order. That
|
|
// dependency is gone — `fromConfig` now sorts top-level keys so wildcard
|
|
// permissions come before specifics, making the final precedence
|
|
// order-independent.
|
|
const InputObject = Schema.StructWithRest(
|
|
Schema.Struct({
|
|
read: Schema.optional(Rule),
|
|
edit: Schema.optional(Rule),
|
|
glob: Schema.optional(Rule),
|
|
grep: Schema.optional(Rule),
|
|
list: Schema.optional(Rule),
|
|
bash: Schema.optional(Rule),
|
|
task: Schema.optional(Rule),
|
|
external_directory: Schema.optional(Rule),
|
|
todowrite: Schema.optional(Action),
|
|
question: Schema.optional(Action),
|
|
webfetch: Schema.optional(Action),
|
|
websearch: Schema.optional(Action),
|
|
codesearch: Schema.optional(Action),
|
|
lsp: Schema.optional(Rule),
|
|
doom_loop: Schema.optional(Action),
|
|
skill: Schema.optional(Rule),
|
|
}),
|
|
[Schema.Record(Schema.String, Rule)],
|
|
)
|
|
|
|
// Input the user writes in config: either a single Action (shorthand for "*")
|
|
// or an object of per-target rules.
|
|
const InputSchema = Schema.Union([Action, InputObject])
|
|
|
|
// Normalise the Action shorthand into `{ "*": action }`. Object inputs pass
|
|
// through untouched.
|
|
const normalizeInput = (input: Schema.Schema.Type<typeof InputSchema>): Schema.Schema.Type<typeof InputObject> =>
|
|
typeof input === "string" ? { "*": input } : input
|
|
|
|
export const Info = InputSchema.pipe(
|
|
Schema.decodeTo(InputObject, {
|
|
decode: SchemaGetter.transform(normalizeInput),
|
|
// Not perfectly invertible (we lose whether the user originally typed an
|
|
// Action shorthand), but the object form is always a valid representation
|
|
// of the same rules.
|
|
encode: SchemaGetter.passthrough({ strict: false }),
|
|
}),
|
|
)
|
|
.annotate({ identifier: "PermissionConfig" })
|
|
.pipe(
|
|
// Walker already emits the decodeTo transform into the derived zod (see
|
|
// `encoded()` in effect-zod.ts), so just expose that directly.
|
|
withStatics((s) => ({ zod: zod(s) })),
|
|
)
|
|
type _Info = Schema.Schema.Type<typeof InputObject>
|
|
export type Info = { -readonly [K in keyof _Info]: _Info[K] }
|