@sema-agent/core
Version:
Stateless, task-oriented AI agent core
279 lines (278 loc) • 24.2 kB
TypeScript
/**
* design/127 — **参数级权限规则 DSL**(追平批 γ)。声明式规则字符串 → {@link ToolPolicy}
* 编译器:`"Tool"`(裸)/ `"Tool(param:pattern)"`(参数级,`*` 通配全串锚定)。CC ground truth
* 行号级:解析 `Lh` :55214 · 通配 `ZMr` :55196 · 参数级 matcher `gnn` :595123 · 评估序 `G1e` :595272。
*
* 架构边界(design/127 §0,重判过的「core 不长 rule interpreter」):这是纯函数式、opt-in、零
* run-loop 介入的「声明式数据→ToolPolicy」工厂(`createCoarseCommandNamePolicy` 同形态先例)。
* core 不读设置文件、不管 source 层级合并 —— Settings-loader 把各层合并成**单一规则表**后编译
* **一个** policy。
*
* ## Deny and ask only
* A policy fold says `deny` / `ask`. An allow rule is refused (`config.invalid_permission_rules`, one `unsupported.allow`
* issue per rule): "this does not need asking" lives on the allow layer (`TaskSpec.allowedTools`, `RunnerDeps.allowedTools`).
*
* ## The CONTENT-FORM command lane (the shell tool's own grammar)
*
* A `Bash(<command>)` / `Bash(<prefix>:*)` / `Bash(<body> *)` rule is not a `param:pattern` rule — its
* content is a COMMAND SPELLING in the persisted-rule model's grammar (`permission-rule-model.ts`), and
* this compiler now compiles it AS SUCH for the two tightening behaviors: the text goes through the ONE
* validator (`parseRuleText(text, behavior)` — the tightening floor, so `Bash(node:*)` as a deny is a
* legitimately wide tightening) and the call is judged with the ONE tightening reach
* (`programRunReachOf`: the whole command line or any program run in a connector chain — upstream's
* per-subcommand deny, the same instrument the org partition and the personal store's deny/ask rows use;
* a call the lexer cannot read against a rule is a fail-closed ask, never silence).
* The rule is written in the command word (`Bash`) and reaches every call on the catalog's SHELL FACE — `Bash`, and
* `Monitor`, which runs its `command` through the same execution seam — under any name the call answers to; a command
* spelling on another shell-face name (`Monitor(touch:*)`) is refused (`invalid.command_rule`, naming the `Bash(…)` form):
* upstream reads no such rule. A bare `Bash` rule is a tool-name rule and reaches the tool of that name only.
* It sits in the field-level tier of the evaluation order — right after the bare deny (resp. ask) — which
* is upstream's placement (`bs()` whole-tool deny → `AN(…,"deny")` field-level deny → `ph()` whole-tool
* ask → `AN(…,"ask")` field-level ask).
*
* The PATH lane is the same shape for every tool the catalog declares a path target for: a
* `Edit(~/.sema/settings.json)` / `Write(src/**)` / `Read(//etc/**)` deny or ask compiles through the same
* validator (the model's path grammar — the `//abs-dir/**` directory form and upstream's pattern form:
* `//` absolute, `~/` home, `/` project root, otherwise working-directory relative; `*` within a segment,
* `**` across) and is judged on the call's PROTECTIVE path target (design/388: the object face's declaration, the
* catalog's for the name as the floor), every view of it (`callTargetViewsOf`), by the one judgment every path rule
* meets (`judgePathRows`). A pattern's ANCHORS are the rule's own fact, taken once where the policy is installed: the
* declared `pathBases` word by word, else the leg the Runner installs the policy on (`policyAnchors`) — never the
* call's live working directory. A declared base that is not a usable absolute path is refused at compile time
* (`unsupported.path_base`); a policy called outside a Runner leg with no declared base reads such a rule
* `unreadable` (the fail-closed ask).
* A `Read(...)` / `Edit(...)` deny or ask reaches the calls on its behavior's FACES (`PATH_RULE_FACES`), not only
* its own tool: a `Read` ask asks for a Grep / Glob rooted in its path, judged on that call's own target.
*
* The NAME lane is the same shape for the tools of `NAME_RULE_FACES` (the `Skill` tool): a `Skill(<name>)` / `Skill(<prefix>:*)`
* deny or ask — the reference's own spelling — compiles through the same validator (the model's name grammar) and is judged on the
* one string the call names (`nameRuleSubjectOf`) with `nameRuleReach`, the predicate the persisted rule store uses. The parameter
* spelling `Skill(skill:<name>)` those tools used to take is retired: refused loudly, its sentence naming the CC form. A KEYED
* value tool (`Artifact(action:<word>)`) takes the same lane for content keyed by its own parameter (`nameGrammarReads`); its
* other arguments stay on the generic `param:pattern` lane. The delegation tool's `Agent(<subagent_type>)` takes it too, its
* value and the call's resolved against the agent types the call's face carries (`nameRuleReach`), its other arguments on the
* generic lane. A call of a closed-set tool that names no value as the tool reads it is refused by the tool's deny or ask rule
* (`nameValueAbsentReason`, the persisted lane's reading).
*
* The HOST lane is the same shape for a tool name the catalog declares a host target for (the URL its call reaches —
* `WebFetch`'s `url`): a `WebFetch(domain:<host>)` / `WebFetch(domain:*.<domain>)` deny or ask — the reference's own spelling —
* compiles through the model's domain grammar and is judged on the host the call reaches (`callHostTargetOf`, the protective
* reading: the call face's slot, the catalog's for the name as the floor) with `domainRuleAdmits`, the reader a person's stored
* rows and the entry lists use. Every lane is one dispatch over the rule's name and content (`ruleLaneOf`), so the static
* compile and a leg's roster compile read every rule the same way. A call whose host cannot be read is refused by a host deny
* rule and is the fail-closed ask under a host ask rule.
*
* 🔴 与 CC 的有意差(design/127 r2):
* - CC 参数级 generic 道无 allow lane(gnn 只有 deny/ask)—— 我们同样,且整个 policy 无 allow 臂。
* - CC 对尾缀垃圾静默降级为裸名(Lh `n !== len-1`)—— `parsePermissionRule` 保持 CC parity,但
* **工厂 validator strict**:降级即 invalid(fail-fast 世界观,写错的规则不能静默变宽)。
* - 已知主字段参数规则(如 `Bash(command:…)`)默认 **reject**:通配是 trim 后**整串 lexical**
* 匹配,不是 shell AST / 前缀 lane —— `Bash(command:git push*)` 匹配不了 `cd /x && git push`,
* 「以为挡住了」比没挡更危险。`primaryFieldGeneric:"allow-lexical"` 显式解锁。
*/
import { type NamedToolPolicy, type ToolPolicy } from "./tool-policy.js";
import { formatPermissionRule, parsePermissionRule, type ParsedPermissionRule } from "@sema-agent/primitives";
import { type PathRuleBases } from "./permission-rule-model.js";
import type { RuleSource } from "./permission-rule-provider.js";
export { parsePermissionRule, formatPermissionRule, type ParsedPermissionRule };
export interface PermissionRule {
/** CC 语法:`"Tool"` 或 `"Tool(param:pattern)"`。`Tool()`/`Tool(*)` 归一为裸规则;括号可 `\(`/`\)` 转义。 */
rule: string;
behavior: "deny" | "ask";
/** Provenance:进 deny/ask 的 message 供审计(e.g. `"policySettings"`)。层级
* 优先级/tighten-only 语义在 Settings-loader,不在这里。 */
source?: string;
/**
* The layer that wrote the rule, in the engine's rule-source words ({@link RuleSource}: `user`, `project`, `org`, `session`).
* Absent = the host's own rule. It decides one thing here: a rule a PERSON's settings layer wrote (`user` or `project`) whose
* text the compiler cannot read under its defaults is left out of the policy and told to that person in the run that installs it
* ({@link skipsUnreadableRule}, {@link ToolPolicy.skippedRules}), instead of refusing the whole set. Every other unreadable rule
* follows {@link PermissionRulePolicyOptions.onInvalidRule}. A value outside the four words, or a read of it that throws, is no
* layer — the strict side, which never leaves a rule out; a refusal an outside word causes names it.
*/
layer?: RuleSource;
}
export interface PermissionRuleIssue {
rule: string;
/** `invalid.*` = 语法/语义错;`unsupported.*` = CC 合法但 v1 不支持的 lane(独立
* 分类,绝不静默 no-op —— `Bash(npm run:*)` 被当 generic 参数规则接受后永不匹配是最危险的失败形态)。 */
code: "invalid.empty_tool" | "invalid.paren"
/** A rule the rule syntax reads no rule in (`ruleSpellingOf`) whose name is the reason: blanks around the rule, a
* character outside a tool name's spelling (a variation selector, a filler, a zero-width space), a blank or an invisible
* character before the `(`, a leading `-`. Refused — the lenient reading would deny a tool under another spelling, or none.
* (A `*` in a tool name is no such spelling: a deny or ask rule reads it as a glob over the whole name, `*` alone as every
* tool.) */
| "invalid.tool_name" | "invalid.param_split" | "invalid.primary_field" | "invalid.cap_exceeded"
/** An ALLOW rule (any spelling): a policy fold says deny / ask only — "this does not need asking" is the allow
* layer's (`TaskSpec.allowedTools`, `RunnerDeps.allowedTools`). One issue per allow rule. */
| "unsupported.allow"
/** A `behavior` outside the closed set `deny | ask` (a JS caller's typo, an undefined member, a boxed string): refused —
* a rule that matched nothing because its verb was misspelled would be a deny that silently never fires. */
| "invalid.behavior"
/** A content-form deny/ask rule the ONE rule validator refuses — the shell's command grammar (a redirection, a substitution,
* a mid-pattern star, control characters…), a path tool's path grammar, a name tool's name grammar (the retired parameter
* spelling `Skill(skill:<name>)`, an empty name…) — the message carries the validator's own code and sentence. */
| "invalid.command_rule"
/** An `allowedTools` value (the request's `TaskSpec.allowedTools`, the deployment's `RunnerDeps.allowedTools`) that is
* not a list of strings: one issue for a non-list, one per element that is not a string (`rule` is empty — there is
* no rule text to name). */
| "invalid.entry_type"
/** design/388 B15 — the closed rule-lint set over a tool's RULE FACE (derived scalar vocabulary; a
* declaration only narrows it): the named parameter is not one the tool carries; it is carried but
* is an object/array (a rule on it can never match); the tool is unknown to the resolver a leg
* supplied (absent resolver ⇒ the catalog alone judges and an unknown tool stays permissive). */
| "unsupported.param_unknown" | "unsupported.param_non_scalar" | "unsupported.tool_unknown"
/** A deny/ask name rule whose value the fold lands on SEVERAL of the values the leg's mounted tool declares
* (`Agent(codereview)` on a leg offering `code-review` and `code_review`): which one it names is not determinable, and a rule
* is never resolved to one of several — refused when the leg's `ruleFaces` carry the values (without them the rule reaches
* every such value at the gate). */
| "invalid.name_ambiguous"
/** A path-form deny/ask whose spelling needs a base this policy DECLARES as something other than an absolute path
* of its tree's family (`/x` reads `pathBases.root`; a relative pattern `pathBases.cwd`, else `root`; `~/` `home`)
* — a bad declared value, refused rather than compiled into a rule that reaches nothing. An undeclared base is no
* refusal: the rule is anchored where the policy is installed. */
| "unsupported.path_base" | "unsupported.mcp_paren"
/** A parenthesised rule whose NAME is a covering spelling in a non-MCP protocol namespace
* (`a2a__<peer>__*(x:y)`, `a2a__<peer>(x:y)`). The param DSL is keyed by EXACT tool name, and a
* covering spelling is not one — no mounted tool can ever bear it, so the rule compiled clean and
* matched nothing. Additive member; the MCP spelling keeps its own {@link
* PermissionRuleIssue.code} `unsupported.mcp_paren` (that refusal is wider — it covers exact MCP
* names too, for CC parity — and fires first, so no MCP rule's reported code changes). */
| "unsupported.covering_paren"
/** HRD-PRM-7: a param-level rule naming a parameter the target tool does not carry. Additive member —
* the lane that used to compile silently and never match (see {@link FILE_TOOL_PARAMS}). */
| "unsupported.unknown_param";
/** What a person can do about the rule — never a construction option of the library: those ride on {@link libraryOption}. */
message: string;
/**
* The engine-side library coordinate this refusal relates to — a name a host program or a spec author holds and a person
* writing rules does not: a construction option of the compile call (a key of {@link PermissionRulePolicyOptions}, a
* `pathBases` / `caps` entry spelled `pathBases.<word>` / `caps.<key>`), the allow-list seat of a request or of a deployment
* (`TaskSpec.allowedTools` / `RunnerDeps.allowedTools`), or the MCP server spec's per-tool declaration (`toolAxes`). Present
* where the engine offers it as the way out of the refusal, or names it as the declaration the refusal rests on (a rule set
* over its cap, a declared base that is no path, the list that is no list) — not wherever some other value of an option would
* happen to change the answer (a resolver that declared more parameters would also make a misspelled one compile; that is not
* a way out). The sentence never names it, so a host that exposes the coordinate renders it and any other host shows the
* sentence alone. Minted in one place ({@link ruleIssue}).
*/
libraryOption?: {
name: "primaryFieldGeneric" | "defaultAction" | "ruleFaces" | `pathBases.${keyof PathRuleBases}` | `caps.${keyof PermissionRuleCaps}` | "TaskSpec.allowedTools" | "RunnerDeps.allowedTools" | "toolAxes";
/** The value that opts in, when the way out is one value (`primaryFieldGeneric` ⇒ `"allow-lexical"`); absent when the option
* takes the host's own choice (a cap's size, a base's path, the answer for calls no rule reaches). */
value?: string;
};
}
/** THE mint of a {@link PermissionRuleIssue}: every issue the compiler and the allow-list screen report is made here, so the
* optional {@link PermissionRuleIssue.libraryOption} is written at one place and an absent one is absent, never `undefined`. */
export declare function ruleIssue(rule: string, code: PermissionRuleIssue["code"], message: string, libraryOption?: PermissionRuleIssue["libraryOption"]): PermissionRuleIssue;
export interface PermissionRuleCaps {
maxRules: number;
maxRuleLength: number;
maxPatternLength: number;
maxStars: number;
/** Scalar arg values longer than this are NOT matched textually — the rule **fail-closes** to its
* lane (a deny rule targeting an oversized value denies; an ask rule asks). 租户规则 × 模型巨串
* 的热路径成本上界。 */
maxScalarValueChars: number;
}
/** The caps a rule set is held to unless the caller names its own — `maxRules` is the ONE bound on how many rules a set
* carries, read by the compiler here and by the org layer's list judgment (which refuses a longer list before it is copied). */
export declare const DEFAULT_CAPS: Readonly<PermissionRuleCaps>;
export interface PermissionRulePolicyOptions {
/** Invalid/unsupported rules: `"throw"`(default — 部署作者写错要立刻知道)aggregates ALL issues
* into one error; `"skip"` drops them (pair with {@link validatePermissionRules} for a report, and/or
* supply {@link onIssue}). Exception(实现审 M3):`invalid.cap_exceeded` on the WHOLE rule set
* (maxRules) throws EVEN under `"skip"` — skipping it would compile an EMPTY table and fail-open.
* Not asked of a rule a person's settings layer wrote whose text is unreadable ({@link skipsUnreadableRule}): that rule is left
* out whatever this says, rides the policy ({@link ToolPolicy.skippedRules}) and is told to that person by the run. */
onInvalidRule?: "throw" | "skip";
/** Called once per dropped rule — under `"skip"`, and for a person's rule {@link skipsUnreadableRule} leaves out (audit surface —
* silent drops are how a deployment discovers, months later, that its deny never existed). Default under `"skip"`: one
* aggregated `config.permission_rules_skipped` notice on the process's default seat, else one process line. */
onIssue?: (issue: PermissionRuleIssue) => void;
/** What the policy returns when NO rule matches. Default `"allow"` = 本 policy 无意见(组合语义;不是同意)。
* default-closed 部署用 `"ask"`/`"deny"` —— 必须在这 INSIDE the policy(链尾 ask policy 会改写整条链的答案)。 */
defaultAction?: "allow" | "ask" | "deny";
/** 已知主字段参数规则(`Bash(command:…)` 等)的处置。默认 `"reject"`(整串 lexical ≠ 前缀/AST,
* 假安全感);`"allow-lexical"` 显式接受并按 generic 语义匹配。 */
primaryFieldGeneric?: "reject" | "allow-lexical";
caps?: Partial<PermissionRuleCaps>;
/**
* design/388 B15 — the leg's RULE-FACE resolver (typically `ruleFacesFromRoster(prepared.toolRoster)`): with it
* every tool on the leg is judged — MCP and caller tools included — and a rule naming a tool the leg does not
* mount is `unsupported.tool_unknown`. Without it the static catalog alone judges; a tool the catalog does not
* carry stays permissive (a rule for it compiles unjudged).
*/
ruleFaces?: (toolName: string) => RuleFaceView | undefined;
/**
* The ANCHORS of this rule set's path patterns ({@link PathRuleBases}), word by word: `root` for `/…`, `cwd` for a
* relative pattern (the declared root stands in when absent — the word's own meaning), `home` for `~/…` (an
* execution environment's home — never the engine process's own; #644). A word declared here anchors the rules
* wherever the policy runs; a word left out is anchored where the Runner installs the policy — the root and the
* environment's home of the leg it enters the delegation chain on — and stays there for every child and every `cd`.
* A declared value that is not an absolute path of its tree's family is refused at compile time
* (`unsupported.path_base`, naming the base — {@link ruleBasesNeeded} is the same classification).
* A SETTINGS LAYER's rules are one rule set, the reference's reading of `/x` (the root of the layer the rule came from):
* `root` = the layer's root — the task root for a project or local layer, the configuration directory for a user layer —
* and `cwd` = the task root, since a relative pattern reads at the working directory whatever its layer (left out, it
* stands in at `root`). Rules of several layers merged into one set read every `/x` at that one set's root.
* These are the RULES' anchors, never the call's: a call's target is read from the call (its target reading, else its
* own `root` / `cwd`); only a host calling the policy directly with a request that carries neither has its target
* resolved against `root` / `cwd` here. A target nothing locates is unreadable: every path rule able to reach the
* call answers it with the fail-closed ask (`ruleUnreadable`).
*/
pathBases?: PathRuleBases;
}
import { isCoveringRuleName, ruleNameCovers, wildcardMatch } from "./namespaced-rule-name.js";
export { isCoveringRuleName, ruleNameCovers, wildcardMatch };
/**
* design/388 A-6/7/8/9 (D-3): the per-tool RULE FACE — the parameters a `Tool(param:pattern)` rule may name
* (`params`, the SCALAR top-level keys ∪ the path target's aliases), the ones the generic lexical matcher must
* not be pointed at (`primaryParams`) and the carried-but-non-scalar keys a rule can never match (`nonScalar`). Resolved
* through {@link PermissionRulePolicyOptions.ruleFaces} when a leg supplies its roster, else from the static catalog — the
* three hand-kept tables (`PRIMARY_FIELDS` / `FILE_TOOLS` / `FILE_TOOL_PARAMS`) this replaced are gone. Which lane a rule
* takes is not the face's to say: it is the rule's name's grammar (`ruleLaneOf`), the same with or without a resolver.
*/
export interface RuleFaceView {
primaryParams: readonly string[];
/** The full scalar vocabulary. A face that states none declares its `primaryParams` only: a rule naming any other
* parameter is refused (`unsupported.param_unknown`) — a face never leaves a parameter name unjudged. */
params?: readonly string[];
nonScalar?: readonly string[];
/** The values the mounted tool takes for the argument its name rules are read against (`ToolRuleFace.nameValues`): with them a
* name rule whose value the fold lands on several of them is refused at compile (`invalid.name_ambiguous`). */
nameValues?: readonly string[];
}
/** The static catalog's answer for a wire name (undefined ⇒ the catalog does not carry it). */
export declare function catalogRuleFaceView(toolName: string): RuleFaceView | undefined;
/** Exported for the lockstep guard only (the scalar vocabulary of a catalog tool) — not part of the rule DSL. */
export declare const fileToolParamVocabulary: (canonicalTool: string) => ReadonlySet<string> | undefined;
/**
* THE answer to "is this unreadable rule left out (and told to the person who wrote it) rather than refusing the set?" — one rule: the
* rule's `layer` is a person's settings layer (`user` / `project`), its `behavior` is `deny` or `ask` (the list it sits on is the
* host's choice), and its TEXT alone is refused under the engine's defaults (no rule faces, the default caps, no declared bases). A rule
* that reads under those defaults and is refused only because of what the host handed the compiler (`ruleFaces`, `caps`, `pathBases`)
* is the host's to fix, never the person's: it refuses the set. The compiler reads this answer, and a host that judges a rule before
* compiling it (a request's pre-check) reads it too, so the two never answer differently.
*/
export declare function skipsUnreadableRule(issue: Pick<PermissionRuleIssue, "rule">, rule: {
readonly behavior?: unknown;
readonly layer?: unknown;
}): boolean;
/** One rule a compiled policy left out because {@link skipsUnreadableRule} answered true for it — what {@link ToolPolicy.skippedRules}
* carries and the person's notice names: the rule as written, why (the issue's code and sentence), the list it sat on, its layer. */
export interface SkippedPermissionRule {
readonly rule: string;
readonly code: PermissionRuleIssue["code"];
readonly message: string;
readonly behavior: "deny" | "ask";
readonly layer: "user" | "project";
}
/** The rules every one of `policies` left out ({@link ToolPolicy.skippedRules}; a composed policy carries its members'), in order —
* only a person's rows, as plain copies: the one reading every reader of the member takes. The member and each element are read once
* by index (never through the carried value's own methods); a read that throws names nothing from that policy. */
export declare function toolPolicySkippedRules(...policies: ReadonlyArray<ToolPolicy | undefined>): readonly SkippedPermissionRule[];
/** Dry-run 校验/迁移报告:不 throw,返回全部 issues(含 `unsupported.*` 分类,
* 供从 CC settings 迁移的部署逐条对照)。 */
export declare function validatePermissionRules(rules: PermissionRule[], opts?: Pick<PermissionRulePolicyOptions, "primaryFieldGeneric" | "caps" | "ruleFaces" | "pathBases">): PermissionRuleIssue[];
export declare function createPermissionRulePolicy(rules: PermissionRule[], opts?: PermissionRulePolicyOptions): NamedToolPolicy;