b-macker/NAAb

GitHub: b-macker/NAAb

一个内置 AI 代码治理机制的多语言编程语言,通过 govern.json 配置在执行前拦截 AI 生成的幻觉 API、存根代码和安全漏洞。

Stars: 1 | Forks: 0

# NAAb — 内置 AI 治理的编程语言 [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/0cb1b7ad87140745.svg)](https://github.com/b-macker/NAAb/actions/workflows/ci.yml) [![Sanitizers](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/687ae6ad2e140746.svg)](https://github.com/b-macker/NAAb/actions/workflows/sanitizers.yml) [![CodeQL](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/c5e412653e140747.svg)](https://github.com/b-macker/NAAb/actions/workflows/codeql.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md) [![Discussions](https://img.shields.io/badge/Discussions-enabled-blue.svg)](https://github.com/b-macker/NAAb/discussions) **在语言层面治理 AI 生成的代码。** NAAb 是一种多语言编程语言,它将代码质量、安全性和正确性作为运行时约束强制执行——而非仅仅是建议。只需一个 `govern.json` 文件即可在一个文件中控制 12 种语言的执行行为。 ``` // govern.json — catches AI hallucinations before they execute { "version": "3.0", "mode": "enforce", "code_quality": { "no_hallucinated_apis": { "level": "hard" }, "no_oversimplification": { "level": "hard" }, "no_incomplete_logic": { "level": "soft" } } } ``` ``` $ naab-lang app.naab Error: Hallucinated API in python block: ".push()" is not valid Python [HARD] Rule: code_quality.no_hallucinated_apis Help: .push() is JavaScript — in Python, use .append() ✗ Wrong: my_list.push(item) ✓ Right: my_list.append(item) ``` ## 问题所在:AI 代码漂移 AI 模型生成的代码看起来正确,但实际上并非如此。每次会话都是全新的——对你的安全规则、命名约定或架构决策毫无记忆: - **幻觉 API** — Python 中的 `.push()`,JavaScript 中的 `print()`,使用 `json.stringify()` 而非 `json.dumps()` - **以“完整”名义发布的存根** — `def validate(): return True`,仅有 `pass` 或 `NotImplementedError` 的函数 - **被绕过的安全模式** — 硬编码密钥、SQL 注入、`except: pass` 吞掉错误 - **语言误用** — Python 用于繁重计算,JavaScript 用于文件操作,Shell 用于复杂逻辑 提示词只是建议。**`govern.json` 才是强制执行。** NAAb 在执行前会根据你的策略检查每一个多语言块——在这里无法被绕过。 ## 概览 | 能力 | 详情 | |---|---| | **治理引擎** | 50+ 项检查,3 级强制执行(hard / soft / advisory),`govern.json` 配置 | | **多语言执行** | 单文件支持 12 种语言 — Python, JavaScript, Rust, C++, Go, C#, Ruby, PHP, Shell, Nim, Zig, Julia | | **智能错误信息** | 通过 Levenshtein 距离提供“你是否指的是?”建议,附带示例的详细修复方案 | | **标准库** | 13 个模块 — array, string, math, json, http, file, time, debug, env, csv, regex, crypto, bolo | | **语言特性** | 模式匹配, async/await, lambdas, 闭包, 管道操作符, if 表达式 | | **CI/CD 集成** | SARIF (GitHub Code Scanning), JUnit XML (Jenkins/GitLab), JSON 报告 | | **项目上下文** | 自动读取 CLAUDE.md, .editorconfig, .eslintrc, package.json 以补充治理规则 | | **开发者工具** | REPL, LLM 友好语法(关键字别名,可选分号),204 条错误信息 | ## 快速开始 ``` # Clone 和 build git clone --recursive https://github.com/b-macker/NAAb.git cd NAAb mkdir build && cd build cmake .. -G Ninja ninja naab-lang -j$(nproc) # Run 一个 file ./naab-lang hello.naab ``` ### Hello World ``` main { let name = "World" print("Hello, " + name + "!") } ``` ## 治理引擎 NAAb 的治理引擎是其独特之处。在项目根目录下放置一个 `govern.json`,每个多语言代码块在执行前都会被检查。 ### 它能捕捉什么 | 类别 | 示例 | 模式 | |---|---|---| | **幻觉 API** | Python 中的 `.push()`,JS 中的 `print()`,JS 中的 `len()` | 86+ 种跨语言模式 | | **过度简化** | `def validate(): return True`,仅含 `pass` 的函数体,恒等函数 | 35+ 种存根模式 | | **不完整逻辑** | `except: pass`,裸 raise,`"something went wrong"` 消息 | 40+ 种模式 | | **安全性** | SQL 注入,路径遍历,Shell 注入,硬编码密钥 | 基于熵的检测 | | **代码质量** | TODO/FIXME,调试残留,Mock 数据,硬编码 URL/IP | 死代码检测 | | **PII 泄露** | SSN 模式,信用卡号,字符串中的 API Key | 正则 + 熵 | ### 三种强制执行级别 - **HARD** — 阻止执行。代码不会运行。不可覆盖。 - **SOFT** — 阻止执行。除非传递 `--governance-override`,否则代码不会运行。 - **ADVISORY** — 警告并继续。记录在审计追踪中。 ### govern.json 示例 ``` { "version": "3.0", "mode": "enforce", "languages": { "allowed": ["python", "javascript", "go"], "blocked": ["php"] }, "code_quality": { "no_secrets": { "level": "hard" }, "no_sql_injection": { "level": "hard" }, "no_oversimplification": { "level": "hard" }, "no_incomplete_logic": { "level": "soft" }, "no_hallucinated_apis": { "level": "soft" } }, "restrictions": { "no_eval": { "level": "hard" }, "no_shell_injection": { "level": "hard" } }, "limits": { "max_lines_per_block": 200, "timeout_seconds": 30 } } ``` ### 项目上下文感知 NAAb 可以读取你现有的项目配置文件并补充治理规则——而不会覆盖 `govern.json`: - **Layer 1:** LLM 指令文件 — `CLAUDE.md`, `.cursorrules`, `AGENTS.md` - **Layer 2:** Linter 配置 — `.eslintrc`, `.flake8`, `pyproject.toml` - **Layer 3:** 项目清单 — `package.json`, `Cargo.toml`, `go.mod` 每一层都是可选择启用且可切换的。 ### 自定义规则 定义你自己的基于正则表达式的治理规则: ``` { "custom_rules": [ { "name": "no_print_debugging", "pattern": "console\\.log|print\\(.*debug", "message": "Remove debug print statements before committing", "level": "soft" } ] } ``` ### CI/CD 集成 ``` # GitHub Code Scanning (SARIF) naab-lang app.naab --governance-report sarif > results.sarif # Jenkins / GitLab CI (JUnit XML) naab-lang app.naab --governance-report junit > results.xml # Custom tooling (JSON) naab-lang app.naab --governance-report json > results.json ``` ## 多语言执行 在每种语言最擅长的领域使用它——Python 用于数据科学,Rust 用于性能,JavaScript 用于 Web,Go 用于并发,Shell 用于文件操作。变量在语言之间自动流转。无需 FFI,无需序列化,无需微服务。 **支持的语言:** Python · JavaScript · Rust · C++ · Go · C# · Ruby · PHP · Shell · Nim · Zig · Julia ``` main { let numbers = [10, 20, 30, 40, 50] // Python: statistical analysis let stats = <> // JavaScript: format as HTML let html = <${data}
`; >> print(stats) print(html) } ``` ### 关键多语言特性 - **变量绑定** — 使用 `< JSON` 头从任何语言返回结构化数据 - **错误映射** — 多语言错误映射回 NAAb 源码位置并流入 try/catch - **块头感知** — Go 自动获得 `package main`,PHP 自动获得 ` "OK" 404 => "Not Found" 500 => "Server Error" _ => "Unknown" } print(message) // "Not Found" } ``` ### Async/Await ``` main { async fn fetch_data() { return "data loaded" } async fn process() { return "processed" } let data = await fetch_data() let result = await process() print(data + " -> " + result) } ``` ### Lambdas 与闭包 ``` main { let multiplier = 3 let scale = fn(x) { return x * multiplier } print(scale(10)) // 30 // Closures capture their environment function make_counter() { let count = 0 return fn() { count = count + 1 return count } } let counter = make_counter() print(counter()) // 1 print(counter()) // 2 } ``` ### 管道操作符 ``` main { let result = "hello world" |> string.upper() |> string.split(" ") print(result) // ["HELLO", "WORLD"] } ``` ### If 表达式 ``` main { let score = 85 let grade = if score >= 90 { "A" } else if score >= 80 { "B" } else { "C" } print(grade) // "B" } ``` ### 错误处理 ``` main { try { let result = <> } catch (e) { print("Caught from Python: " + e) } } ``` ### 更多语言特性 - 变量,常量,函数 - 结构体,枚举,类 - 带有导入/导出的模块系统 - For 循环,While 循环,break/continue - 支持点表示法的字典和数组 ## 标准库 包含 13 个模块,提供 204 条错误信息、“你是否指的是?”建议以及详细文档。 ``` main { // JSON let data = json.parse('{"name": "NAAb", "version": "0.2.0"}') print(data["name"]) // HTTP let response = http.get("https://api.github.com/repos/b-macker/NAAb") print(json.parse(response)["stargazers_count"]) // File I/O file.write("output.txt", "Hello from NAAb!") // Math print(math.sqrt(144)) // 12 // String operations let words = string.split("hello world", " ") print(string.upper(words[0])) // "HELLO" } ``` | 模块 | 函数 | |---|---| | `array` | push, pop, map, filter, reduce, sort, slice, find, reverse, length | | `string` | split, join, upper, lower, trim, replace, reverse, contains, starts_with, char_at | | `math` | sqrt, pow, abs, floor, ceil, round, sin, cos, tan, random, pi, e | | `json` | parse, stringify, query, validate | | `http` | get, post, put, delete (支持 headers 和 body) | | `file` | read, write, append, exists, delete, list | | `time` | now, sleep, format, parse, duration | | `debug` | inspect, type, trace, watch, snapshot, diff, assert | | `env` | get, set_var, list | | `csv` | read, write, parse | | `regex` | match, search, replace, split, groups | | `crypto` | hash, hmac, random_bytes, uuid | | `bolo` | scan, report (治理集成) | ## 开发者体验 ### 智能错误信息 NAAb 不仅告诉你哪里错了——它还会告诉你如何修复: ``` Error: Unknown function "array.pussh" Did you mean: array.push ? Help: - array.push(arr, value) adds an element to the end of an array Example: ✗ Wrong: array.pussh(my_list, 42) ✓ Right: array.push(my_list, 42) ``` ### 常见错误检测 NAAb 检测约 35 种开发者(以及 AI)使用错误语言习语的模式: - `array.append()` → “那是 Python。在 NAAb 中,请使用 `array.push()`” - `console.log()` → “那是 JavaScript。在 NAAb 中,请使用 `print()`” - `str.upper()` → “请使用 `string.upper(str)` 或 `str.upper()`(点表示法)” ### LLM 友好语法 NAAb 接受多种关键字风格,因此 AI 生成的代码无需手动编辑即可运行: - `function` / `func` / `fn` — 均有效 - `let` / `var` / `const` — 均有效 - 分号 — 可选(接受但不强制要求) - `return` — 在单表达式函数中可选 ## NAAb 生态系统 使用 NAAb 构建的三个工具——代码治理、性能优化和数据安全: | 项目 | 用途 | 核心特性 | |---------|---------|--------------| | **[NAAb BOLO](https://github.com/b-macker/naab-bolo)** | 代码治理与验证 | 50+ 项检查,SARIF 报告,AI 漂移检测 | | **[NAAb Pivot](https://github.com/b-macker/naab-pivot)** | 代码演进与优化 | 3-60x 提速,经证实的正确性,8 种语言 | | **[NAAb Passage](https://github.com/b-macker/naab-passage)** | 数据网关与 PII 保护 | 零泄露,主权架构,HIPAA/GDPR | ### NAAb BOLO — 代码治理与验证 **[NAAb BOLO](https://github.com/b-macker/naab-bolo)** ("Be On the Lookout") 捕捉 AI 生成代码中过度简化的存根、幻觉 API 和不完整的逻辑。 ``` # Scan 治理违规 naab-lang bolo.naab scan ./src --profile enterprise # 生成 CI 的 SARIF 报告 naab-lang bolo.naab report ./src --format sarif --output results.sarif # AI 治理验证 naab-lang bolo.naab ai-check ./ml-models ``` **50+ 项检查 · 4 种语言 · 64 个回归测试** → [开始使用](https://github.com/b-macker/naab-bolo) ### NAAb Pivot — 代码演进与优化 **[NAAb Pivot](https://github.com/b-macker/naab-pivot)** 将慢速代码重写为更快的语言,并提供数学正确性证明。 ``` # 分析 hotspots (Python → Rust candidates) naab-lang pivot.naab analyze app.py # Rewrite 并提供 proof naab-lang pivot.naab rewrite app.py:expensive_loop --target rust --prove # Result: 快 45 倍, 语义一致 ``` **3-60x 提速 · 8 种源语言 · 经证实正确** → [开始使用](https://github.com/b-macker/naab-pivot) ### NAAb Passage — 数据网关与 PII 保护 **[NAAb Passage](https://github.com/b-macker/naab-passage)** 通过主权架构确保零 PII 泄露到 LLM、API 或不受信任的系统。 ``` # 启动 secure gateway naab-lang main.naab # 所有 requests 已验证, PII 已拦截 curl -X POST http://localhost:8091/ -d '{"prompt": "SSN: 123-45-6789"}' # → {"error": "POLICY_VIOLATION"} ``` **零泄露 · 自合成 · HIPAA/GDPR 合规** → [开始使用](https://github.com/b-macker/naab-passage) ## 架构 ``` Source Code (.naab) | Lexer ──> Tokens | Parser ──> AST (recursive descent) | Governance Engine ──> Policy checks (govern.json) | Interpreter (visitor pattern) | ├── Native execution (NAAb code) ├── Python executor (C API) ├── JavaScript executor (QuickJS) ├── Go/Rust/C++/C#/Nim/Zig/Julia executors (compile & run) ├── Ruby/PHP executors (interpreted) └── Shell executor (subprocess) ``` - **73,000+** 行 C++17 - **185** 个测试文件,**325** 个 Mono 测试断言 - **13** 个标准库模块,包含 **204** 条错误信息 - 基于 Abseil, fmt, spdlog, nlohmann/json, QuickJS 构建 ## 贡献 欢迎贡献代码!请参阅 [CONTRIBUTING.md](CONTRIBUTING.md) 了解构建说明和指南。 ### 贡献领域 - 性能优化 - 新的标准库模块 - 文档和教程 - IDE 集成 (Vim, Emacs, IntelliJ) - 包管理器实现 ## 许可证 MIT License - 详情见 [LICENSE](LICENSE)。 **Brandon Mackert** - [@b-macker](https://github.com/b-macker) _NAAb — Polyglot without the trip._
标签:AI代码检测, Bash脚本, DevSecOps, DNS 反向解析, Go语言, LNA, MIT许可, Polyglot, Redis利用, 上游代理, 云安全监控, 人工智能治理, 代码安全, 代码纠错, 多语言支持, 威胁情报, 安全测试框架, 安全策略, 开发者工具, 提示词设计, 漏洞枚举, 程序破解, 编程语言, 编译器, 运行时约束, 静态分析