john-wilmes/multi-model-analyzer

GitHub: john-wilmes/multi-model-analyzer

面向大型 TypeScript/Node.js 代码库的静态分析工具,无需 LLM 即可快速提取依赖图、检测结构问题和故障风险,输出 SARIF 格式诊断报告。

Stars: 0 | Forks: 0

[![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/aa2eb3f19a015025.svg)](https://github.com/john-wilmes/multi-model-analyzer/actions/workflows/ci.yml) # Multi-Model Analyzer (mma) 将 `mma` 指向你的 TypeScript 仓库。获取包含结构性问题、故障风险和死代码的健康报告——只需几秒钟,且无需 LLM。 ``` $ mma index -c repos.json && mma practices Practices Report — Grade: F (0/100) — 1 repo(s) Category Scorecard: Category Health Errors Warnings Notes Total ------------ ------ ------ -------- ----- ----- structural ★☆☆☆☆ 0 64 420 484 fault ★★☆☆☆ 0 15 0 15 blast-radius ★★★★★ 0 0 10 10 Top Findings: Rule Category Level Count Score ---------------------------------- ---------- ------- ----- ----- structural/unstable-dependency structural warning 64 115 fault/unhandled-error-path fault warning 15 95 structural/dead-export structural note 186 75 structural/pain-zone-module structural note 222 75 ``` 该输出是真实的——[TypeORM](https://github.com/typeorm/typeorm)(3,371 个模块,61k 个调用图边)在笔记本电脑上仅用 26 秒完成索引。 ## 它能发现什么 | 类别 | 内容 | 示例 | |----------|------|---------| | **结构 (Structural)** | 不稳定的依赖、死导出、痛点模块 | “模块 A(稳定)依赖于模块 B(不稳定)——依赖方向倒置” | | **故障** | 未处理的错误路径、静默 catch 块、缺少 re-throw | “`handler` 中的 Catch 块没有日志或 re-throw” | | **影响范围** | 更改会广泛扩散的高 PageRank 模块 | “此模块的公共 API 影响 40% 的导入图” | 所有发现均符合 SARIF v2.1.0,且仅包含逻辑位置——没有任何源代码会离开你的设备。 ## 快速开始 ``` # Clone 并安装 git clone https://github.com/john-wilmes/multi-model-analyzer.git cd multi-model-analyzer && npm install && npm run build # 创建一个指向你的 repo 的 config cat > mma.config.json << 'EOF' { "mirrorDir": "./data/mirrors", "dbPath": "./data/mma.db", "repos": [{ "name": "my-service", "url": "https://github.com/org/my-service.git", "branch": "main", "localPath": "./data/mirrors/my-service.git" }] } EOF # Index 并分析 npx mma index -v npx mma practices ``` ## 命令 ``` mma index Index repositories (clone, parse, analyze) mma practices Health report with prioritized findings and grades mma query Natural language queries ("what calls auth?", "dependencies of scheduler") mma report Anonymized field trial report (JSON, markdown, SARIF) mma export Export SQLite DB (anonymized by default, --raw for baseline sharing) mma import Import a raw baseline export into local DB mma merge Combine multiple anonymized export DBs mma validate Statistical validation of SARIF findings quality mma affected Blast radius for a rev range mma serve MCP server for IDE integration (stdio) ``` ## 示例:优先级实践报告 `practices` 命令将发现划分为行动层级: - **立即修复** -- 表明存在活动风险的警告和错误 - **计划处理** -- 值得在下一个周期解决的备注 - **监控** -- 随时间推移需要跟踪的低优先级项 每个发现都包含一个具体的行动: ``` { "ruleId": "structural/unstable-dependency", "count": 64, "level": "warning", "interpretation": "A stable module depends on an unstable one, inverting the expected dependency direction.", "action": "Introduce an abstraction layer or inversion-of-control boundary to isolate the unstable module." } ``` 输出格式:`--format table`(默认)、`json`、`markdown`。 ## 示例:匿名化 SARIF 在外部分享结果时,使用 `--salt` 对标识符进行脱敏: ``` { "ruleId": "fault/unhandled-error-path", "level": "warning", "message": { "text": "Catch block in [REDACTED:b1861bbf]#handler has no logging or re-throw" }, "locations": [{ "logicalLocations": [{ "name": "[REDACTED:ad0b9153]", "kind": "module", "properties": { "repo": "[REDACTED:527d4d8a]" } }] }] } ``` 没有源代码,没有文件路径,没有服务名称——只有结构性发现。 ## 工作原理 索引重,查询轻。所有分析在索引时运行;查询即查找和图遍历。 ``` Repos --> Ingestion --> Parsing --> Structural Analysis --> Heuristic Analysis | Summarization (tiers 1-4) --> Storage | Config Model / Fault Model / Functional Model | SARIF Diagnostics ``` **解析** 使用 [tree-sitter](https://tree-sitter.github.io/tree-sitter/) (WASM) 进行快速纯语法解析,可选使用 [ts-morph](https://ts-morph.com/) 进行类型解析符号。 **摘要** 有 4 个层级——前 3 个免费且本地: | 层级 | 来源 | 成本 | 示例 | |------|--------|------|---------| | 1 | AST 模板 | 免费 | “接受 (patientId: string),返回 Promise” | | 2 | 命名启发式 | 免费 | “获取患者的预约” | | 3 | 通过 Ollama 运行 qwen2.5-coder:1.5b | 免费(本地) | “查询预约表,映射结果,处理分页” | | 4 | Claude Sonnet API | API tokens | “Scheduler 服务跨提供商日历管理预约预订” | ## 架构 基于 npm workspaces 的 Monorepo: | 包 | 用途 | |---------|---------| | `packages/core` | 共享类型,SARIF schema | | `packages/ingestion` | Git clone/fetch,变更检测,文件分类 | | `packages/parsing` | AST 解析 (tree-sitter WASM + ts-morph) | | `packages/structural` | 调用图,依赖图,控制流图 | | `packages/heuristics` | 服务推断,模式检测,功能标志,日志挖掘 | | `packages/summarization` | 4 层描述生成 | | `packages/storage` | 图数据库,搜索 (FTS5/BM25),KV 存储 | | `packages/models/*` | 配置模型,故障模型,功能模型 | | `packages/diagnostics` | SARIF 发射,脱敏,聚合 | | `packages/query` | 自然语言查询路由 | | `packages/mcp` | 用于 IDE 集成的 MCP server | | `apps/cli` | CLI 入口点 | ## 先决条件 - Node.js 22+ - macOS 或 Linux 可选: - [Ollama](https://ollama.com/) 用于第 3 层摘要(免费,本地) - Anthropic API key 用于第 4 层摘要 ## 数据处理 - 仓库作为裸镜像克隆(无工作树检出) - 所有分析均在本地进行——内存或 SQLite - 输出仅使用逻辑位置(无源代码片段) - 内置脱敏在共享前对所有标识符进行哈希处理 - 无遥测 ## 基线共享 共享已索引的基线,以便同事(或其代理)跳过完整的重新索引——仅重新处理更改的文件。 ### 设置(一次性,由索引者执行) ``` # Export raw baseline(包含 incremental indexing 所需的所有内部 key) mma export --raw -o baseline.db ``` 通过共享驱动器、S3、artifact store 等共享 `baseline.db`。 ### 用法(由同事执行) **选项 A:配置驱动(推荐)** 将 `baselinePath` 添加到你的 `mma.config.json`: ``` { "baselinePath": "baseline.db", // relative to config file, or absolute "mirrorDir": "./data/mirrors", "repos": [...] } ``` 然后只需运行: ``` mma index -c mma.config.json -v ``` 在全新的数据库上,基线会在索引前自动导入。在后续运行中会被跳过。 **选项 B:CLI 标志** ``` mma index -c mma.config.json --baseline baseline.db ``` `--baseline` 标志会覆盖配置中的 `baselinePath`。 **选项 C:手动导入** ``` mma import baseline.db --db my.db mma index -c mma.config.json --db my.db ``` ### 工作原理 增量引擎根据当前 HEAD 检查存储的提交哈希(`commit:`)。如果仓库的哈希匹配,则完全跳过。如果不同,则仅重新处理更改的文件。基线植入这些哈希以及缓存的符号和 pipeline 状态,因此第一次增量运行仅处理增量。 ### 对于 AI 代理 如果你是使用 MMA 的 AI 编码代理: 1. 检查 `mma.config.json` 中是否存在 `baselinePath`——如果存在,`mma index` 会自动处理所有事情 2. 如果你有基线文件但没有配置字段,请使用 `--baseline path/to/baseline.db` 3. 导入在全新 DB 上是幂等的,在已填充的 DB 上是无操作的——如果不确定,始终包含 `--baseline` 是安全的 4. 基线导入期间的错误是非致命的:索引会回退到完整处理并发出警告 ## 发现参考 有关所有 SARIF 规则 ID、严重级别和指标,请参阅 [docs/findings-guide.md](docs/findings-guide.md)。 ## 贡献 有关开发设置和指南,请参阅 [CONTRIBUTING.md](CONTRIBUTING.md)。 ## 开发 ``` npm run build # TypeScript compilation npm run type-check # Type checking without emit npm run test # Run all tests npm run lint # ESLint ``` ## 许可证 [Apache 2.0](LICENSE)
标签:AI风险缓解, DevSecOps, GNU通用公共许可证, MITM代理, Node.js, PageRank, SARIF, SAST, SSH爆破, TypeScript, 上游代理, 云安全监控, 代码合规, 代码安全, 依赖图, 复杂度分析, 威胁情报, 安全插件, 开发者工具, 技术债务, 故障预测, 无LLM, 架构分析, 死代码检测, 漏洞枚举, 盲注攻击, 自动化攻击, 静态分析