UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

56 lines (55 loc) • 5.04 kB
/** * The name matchers of the rule grammar and of a run's tool-name lists: the linear `*` glob and the covering forms of a tool name — * a glob over the whole name (`Re*d`, `mcp__*`, the lone `*`; read by a list that restricts) and the protocol-namespace peer * (`<ns>__<peer>`). A leaf module — the allow layer's rule model and the run's list reader both import it, so neither needs the * other's dependency tree. */ import { type ProtocolNamespace } from "@sema-agent/primitives"; import type { RuleBehavior } from "./permission-rule-model.js"; /** * `*` glob match (design/127 M6: NOT a RegExp — patterns may come from tenant-supplied Settings layers; the same semantics as * CC's anchored `^…$`+dotAll construction: every non-`*` char is literal, `*` spans anything incl. newlines). The literal * before the first star and the one after the last are checked at the two ends, and each literal between two stars is put at * its earliest place after the one before it (the earliest placement leaves the most room, so no later one need be tried): * there is no backtracking, so no arrangement of stars multiplies the work and there is no ReDoS surface. The cost is at most * the product of the two lengths, in the engine's native comparisons: a literal after the last star is refused at the end in * one pass over its own length (`*` + k `a` + `b` against 2k `a`s costs k, not k²), and the shape that does reach the product * is a long literal between two stars that matches over most of its length at every place — `*` + k `a` + `b` + k `a` + `*` * against n `a`s is about k × n comparisons (49 ms at k = 1 000, n = 65 536). */ export declare function wildcardMatch(pattern: string, value: string): boolean; /** * True ⇔ this rule name reaches a SET of tool names rather than one exact name — the shapes {@link ruleNameCovers} answers and an * exact-name table cannot hold — on a list of `behavior`. Every lane that keys entries by exact tool name asks it to know which * entries it must NOT key that way. Two shapes: * · a `*` in the name — on a list that RESTRICTS (`deny`, `ask`; {@link readsNameGlob}) anywhere in it (`Re*d`, `mcp__*`, `*`); * on a list that GRANTS only in the tool segment after a literal `<ns>__<peer>__` (`mcp__srv__*`, `mcp__srv__get_*`): any other * `*` names no scope an allow widens ({@link grantWildcardRefusalOf}), so it is no covering name there — the readers refuse it; * · a protocol peer with no tool segment (`<ns>__<peer>`): every tool of that peer, in every list. * `behavior` = the behavior of the list the name is written in — required: a list's `*` has no reading without it, so a value * that is no behavior word (an array index handed by `filter`, say) is refused loudly, never read as either list. */ export declare function isCoveringRuleName(name: string, behavior: RuleBehavior): boolean; /** * The part of {@link isCoveringRuleName} that is a fact about the NAME alone: the name reaches a SET of tool names whatever list it * is written in (a `*` it reads, or a bare protocol peer). A stored rule names ONE tool, so its grammar asks only this — a tool name * that a restricting list also reads as another server's spelling ({@link peerReadingsOf}) is still one tool's name there. */ export declare function namesASetOfTools(name: string, behavior: RuleBehavior): boolean; /** * Does a covering rule name ({@link isCoveringRuleName}) cover `toolName`? A name with a `*` is a glob over the WHOLE name * ({@link wildcardMatch} — the reference's `p` matcher): `mcp__srv__get_*` reaches `mcp__srv__get_issue`, `mcp__*` every MCP tool, * `*` every tool. A protocol peer (`<ns>__<peer>`) reaches every tool minted under that peer, in the SAME namespace, the peer * compared EXACTLY (CC `s.serverName === a.serverName`): a rule spells the peer the way the mint does. A literal name is an exact * tool name, not a covering one — the exact tables answer it. This matcher does not know whose list a name was written in: a caller * asks {@link isCoveringRuleName} with the list's behavior first and matches only what it admits. */ export declare function ruleNameCovers(ruleName: string, toolName: string): boolean; /** * How far a covering rule name reaches into the peer `peer` of namespace `ns`: `"all"` — every tool of it; `"some"` — some tools of * it (a glob over that peer's tool segment); `"none"` — none of it, as far as a list that dials peers needs to know. A glob reaches * EVERY tool of the peer exactly when it ends in `*` and matches the peer's own prefix (`<ns>__<peer>__`): a prefix of every name * minted under the peer is then matched by the glob up to its last `*`, which takes the rest; a glob that does not end in `*` leaves * a tool name it does not match. The one reading of a name against a peer that the tool-name lists take. */ export declare function ruleNameReachIntoPeer(ruleName: string, ns: ProtocolNamespace, peer: string, readsOtherServers?: boolean): "all" | "some" | "none";