@amaster.ai/pi-security

中文文档
0.1.37发布于 2026/11/09Apache-2.0TypeScriptES Module近 30 天下载 3821

Pi extension for resource-aware security policy engine and tool authorization

原始使用文档
文档目录10 节
  1. Pi extension
  2. Built-in profiles
  3. Sandbox modes
  4. Approval modes
  5. Custom profiles
  6. JSON config examples
  7. Profile schema
  8. Rule schema
  9. Commands
  10. Trust model

Pi Security

pi-security preview

@amaster.ai/pi-security provides a sandbox-aware security policy engine and a Pi extension entry point.

The core engine classifies tool calls into resources (files, shell commands, network access), assesses risk, and applies profile rules to decide whether a call is allowed, denied, or requires human approval. Profiles are built from two orthogonal axes — sandbox (what tools are exposed) and approval (when to ask the user) — inspired by Codex.

Pi extension

Install the extension entry point @amaster.ai/pi-security/extension and load the pi-security settings key through Pi SettingsManager:

{
  "pi-security": {
    "enabled": true,
    "profile": "default",
    "approvals": {
      "allowSessionGrants": true
    }
  }
}

The extension listens to Pi tool_call and user_bash events. It does not register an LLM-callable tool that can bypass policy. Human approvals are requested through Pi UI primitives when UI is available; non-interactive contexts fail closed when a rule requires approval.

Built-in profiles

Profiles are derived from a sandbox × approval matrix. Pick one as a starting point and override either axis in your own profiles.

ProfileSandboxApprovalUse case
read-onlyread-onlyneverRead-only inspection, no modifications
defaultworkspace-writeon-requestWorkspace edits with approval for writes (recommended)
autoworkspace-writeon-failureAutonomous edits, only ask when risk is high
full-accessfull-accessneverTrusted automation, no gating (use with care)

Sandbox modes

ModeAllowed toolsDenied tools
read-onlyread, ls, find, grepwrite, edit, bash
workspace-writeread, ls, find, grep, write, edit, bash—
full-access* (all tools)—

Tool names align with the pi-coding-agent built-in tools: bash, read, write, edit, ls, find, grep.

Approval modes

ModeBehavior
neverNo approval prompts (deny rules still apply, e.g. for secrets).
on-failureAsk only on high/critical risk operations.
on-requestAsk on workspace writes/deletes (in addition to high-risk ops).
untrustedAsk on workspace writes plus high-risk ops; default decision is also ask.

Baseline secret protection (denying .ssh/, .env, *.pem, *.key, etc.) is always enabled, regardless of profile.

Custom profiles

Profiles can be defined in three places. Higher-priority sources override lower-priority ones with the same name:

  1. Trusted project: <cwd>/.pi/policy/<name>.json (highest priority when project trust is accepted)
  2. Agent: <agentDir>/policy/<name>.json
  3. User: ~/.pi/agent/policy/<name>.json
  4. Settings: under pi-security.security.profiles in pi.json

Each JSON file defines a single profile; the filename (without .json) is the profile name. Project policies are ignored when project trust is declined or unavailable.

JSON config examples

Ready-to-copy samples live under examples/ and are exercised by the test suite:

  • examples/reviewer.json — a project-level file policy. For a trusted project, drop it into <project>/.pi/policy/reviewer.json. Starts from read-only, opens to workspace-write, asks on bash, denies external network.
  • examples/auto-review.settings.json — a pi.json snippet that defines a custom auto-review profile inline. Extends default and asks before package-manager installs (npm/pnpm/yarn/pip install|add) via argsRegex.

Profile schema

type SecurityProfileConfig = {
  extends?: string;                   // built-in profile name or another custom profile
  sandbox?: 'read-only' | 'workspace-write' | 'full-access';
  approval?: 'never' | 'on-failure' | 'on-request' | 'untrusted';
  rules?: SecurityRule[];             // appended after parent rules
  defaultDecision?: SecurityDecision; // overrides parent default for unmatched tools
};

Rule schema

Rules are evaluated in priority order (descending); the first match wins.

type SecurityRule = {
  id: string;
  priority?: number;                  // default 300 for user rules
  tools?: string[];                   // tool name patterns ('*', 'mcp_*', 'bash')
  sources?: ToolSource[];             // 'builtin' | 'mcp' | 'runtime'
  triggers?: Array<'user' | 'agent' | 'scheduler'>;
  senderTrusts?: Array<'owner' | 'trusted' | 'untrusted'>;
  args?: Record<string, string>;      // exact/glob match against args
  argsRegex?: Record<string, string>; // regex match against args
  resources?: ('file' | 'shell' | 'network')[];
  operations?: ('read' | 'write' | 'execute' | 'delete' | 'connect' | 'search')[];
  scopes?: ('workspace' | 'home' | 'system' | 'external' | 'unknown')[];
  sensitivity?: ('normal' | 'source' | 'config' | 'secret' | 'credential')[];
  risk?: ('low' | 'medium' | 'high' | 'critical')[];
  decision: { kind: 'allow' } | { kind: 'deny'; reason: string } | { kind: 'ask'; reason: string; prompt?: string };
};

Commands

  • /pi-security-status shows the active profile, audit count, and in-session grants.
  • /pi-security-audit [limit] shows recent authorization decisions without raw sensitive output.
  • /pi-security-reset clears in-session approval grants.

Trust model

This module is an execution gate, not a filesystem sandbox by itself. Deny and approval decisions are enforceable because they run before Pi tool execution; sandbox enforcement still depends on the runtime that executes the tool.

Sensitive paths such as SSH keys, .env, credentials, tokens, private keys, and secrets/ directories are denied by baseline policy across all profiles. Audit entries include tool names, resource summaries, risk level, and decision metadata, but they should not include plaintext secrets.

The extension refreshes inline settings and file policies asynchronously for each authorization decision, rechecks trust after awaited reads, and clears session grants when effective configuration or trust changes. Policy changes, deletion, and project trust changes apply to the next decision; authorization profiles are not held in a TTL cache. Policy loading accepts at most 128 JSON files, 128 KiB per file, 2 MiB combined, and 1,024 directory entries across layers. Settings files are limited to 1 MiB each. Limits, malformed configuration, and read errors block authorization with a visible error instead of dropping policy.

Configuration refresh has one 5-second deadline covering queue waits, settings, policies, and trust-triggered reloads, and accepts caller cancellation. Concurrent callers with the same authority scope admitted before a read starts share that fresh read, each with independent cancellation and deadline. Callers arriving after the read starts require a new refresh. Native filesystem operations that cannot be stopped remain tracked until they settle; subsequent reads wait within their own budget, preventing repeated stalled I/O without caching authorization snapshots. Timeout or cancellation blocks execution, removes waiting listeners, and prevents late reads from publishing configuration or starting further I/O.

版本与发布记录

@amaster.ai/pi-security 共发布 131 个版本,首次发布于 2026/05/18;过去 12 个月发布了 131 个版本。

版本发布日期距今
0.1.37latest2026/11/09今天
0.1.2-beta.91beta2026/11/072 天前
0.1.2-beta.902026/11/062 天前
0.1.2-beta.892026/11/053 天前
0.1.362026/11/054 天前
0.1.2-beta.882026/11/054 天前

关于 @amaster.ai/pi-security 的常见问题

@amaster.ai/pi-security 是什么?

Pi extension for resource-aware security policy engine and tool authorization

如何安装 @amaster.ai/pi-security?

在项目目录运行 npm install @amaster.ai/pi-security,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@earendil-works/pi-coding-agent(>=0.79.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@amaster.ai/pi-security 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@amaster.ai/pi-security 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@amaster.ai/pi-security 可以商用吗?

@amaster.ai/pi-security 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@amaster.ai/pi-security 的下载量有多少?

根据 npm 官方下载统计,@amaster.ai/pi-security 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,821 次,日均约 127 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@amaster.ai/pi-security 还在维护吗?

最新版本 0.1.37 发布于 2026/11/09(今天);至今共发布 131 个版本,其中过去 12 个月发布了 131 个;首次发布于 2026/05/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。