@framers/agentos-ext-code-safety

中文文档
0.2.3发布于 2026/10/21Apache-2.0TypeScriptES Module零依赖近 30 天下载 2万

Static analysis guardrail for detecting dangerous code patterns in AgentOS

原始使用文档
文档目录8 节
  1. What it does
  2. Install
  3. Quickstart
  4. Public API
  5. Examples
  6. Lazy loading and optional install
  7. Tool calls
  8. License

@framers/agentos-ext-code-safety

Static-analysis guardrail for @framers/agentos. Scans code that agents emit (in chat, in forged tools, in generated artifacts) for dangerous patterns before the code is shown to the user or executed in any sandbox.

What it does

Extracts code fences from agent output, parses them, and runs each block through a configurable rule set. Default rules cover:

  • Filesystem destruction (rm -rf /, recursive deletes)
  • Process control (fork bombs, unrestricted exec)
  • Network exfiltration to non-allowlisted hosts
  • Hard-coded secrets (API keys, tokens, private keys)
  • Eval-style dynamic code execution
  • Known-bad patterns from the OWASP LLM Top 10

Rules are pluggable; bring your own and the engine merges them with defaults.

Install

npm install @framers/agentos-ext-code-safety

Peer dependency: @framers/agentos.

Quickstart

import { AgentOS } from '@framers/agentos';
import { createCodeSafetyGuardrail } from '@framers/agentos-ext-code-safety';

const agentos = new AgentOS();
await agentos.initialize({
  extensionManifest: {
    packs: [
      {
        factory: () =>
          createCodeSafetyGuardrail({
            languages: ['typescript', 'javascript', 'python', 'shell'],
            extraRules: [],
          }),
        enabled: true,
      },
    ],
  },
});

Public API

  • CodeFenceExtractor — pulls fenced code blocks from prose
  • CodeSafetyScanner — runs fence content through rules
  • CodeSafetyGuardrail — guardrail wiring the scanner for the AgentOS contract
  • DefaultRules — the built-in rule pack
  • createCodeSafetyGuardrail(options?) — factory returning an ExtensionPack
  • createExtensionPack(context) — auto-discoverable factory used by AgentOS extension auto-pickup

See src/types.ts for CodeSafetyPackOptions.

Examples

  • test/ — fixtures across the supported languages plus rule-authoring patterns

Lazy loading and optional install

This package is an optional dependency of @framers/agentos-extensions-registry. The registry ships catalog metadata; createCuratedManifest() calls import.meta.resolve() per entry and silently skips anything not installed. npm install @framers/agentos-ext-code-safety is the gate.

Unlike the other guardrail packs, code-safety is regex-only. There is no model to lazy-load. The default rule set ships in-process and adds zero cold-start cost. Custom rules merge in at activation through extraRules.

The guardrail registers with config.evaluateStreamingChunks = true and runs in Phase 2 of the two-phase dispatcher (parallel classifiers). Dangerous fence patterns (filesystem destruction, secrets, eval) return BLOCK; lower-severity matches return FLAG. Worst-action aggregation across all Phase 2 classifiers picks the strictest decision.

Tool calls

The guardrail also sets config.evaluateToolCalls, so the AgentOS output dispatcher (@framers/agentos 0.13.67 and later) hands it each tool-call chunk of a guarded stream. It scans the code argument of the tools it watches: command of shell_execute (as Bash), query of run_sql (as SQL), and content of write_file, create_file and edit_file. The codeExecutingTools and codeArgumentMapping options change that list. A critical or high finding blocks the call: the stream ends with the guardrail's verdict in place of the chunk, so the stream never hands the host the call to run. A call the runtime runs itself (executionMode: 'internal') does not wait for the output guardrails, so a block ends the stream without stopping it. An earlier AgentOS does not read the flag and passes tool calls unscanned.

For the full DI model and the end-to-end walkthrough that places this pack in the dispatcher, see How extensions stay optional and lazy and the auto-loading guide.

License

Apache 2.0 — see the repo root LICENSE.

版本与发布记录

@framers/agentos-ext-code-safety 共发布 5 个版本,首次发布于 2026/03/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.2.3latest2026/10/2126 天前
0.2.22026/10/121 个月前
0.2.12026/05/185 个月前
0.2.02026/03/257 个月前
0.1.02026/03/227 个月前

关于 @framers/agentos-ext-code-safety 的常见问题

@framers/agentos-ext-code-safety 是什么?

Static analysis guardrail for detecting dangerous code patterns in AgentOS

如何安装 @framers/agentos-ext-code-safety?

在项目目录运行 npm install @framers/agentos-ext-code-safety,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@framers/agentos(>=0.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@framers/agentos-ext-code-safety 支持 TypeScript 吗?

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

@framers/agentos-ext-code-safety 支持 ESM 还是 CommonJS?

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

@framers/agentos-ext-code-safety 可以商用吗?

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

@framers/agentos-ext-code-safety 的下载量有多少?

根据 npm 官方下载统计,@framers/agentos-ext-code-safety 在 2026/10/16 – 2026/11/14 这 30 天内被下载 19,863 次,日均约 662 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@framers/agentos-ext-code-safety 还在维护吗?

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