sentinel-agentos 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +262 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -604,6 +604,268 @@ print(requests.get(f"{BASE}/pipeline/report", headers=HEADERS).text)
|
|
|
604
604
|
|
|
605
605
|
---
|
|
606
606
|
|
|
607
|
+
---
|
|
608
|
+
|
|
609
|
+
## 🔌 sentinel-guard Skill — OpenClaw 实际对接
|
|
610
|
+
|
|
611
|
+
> 这是 Sentinel AgentOS 接入 OpenClaw Agent 框架的**生产级 skill**。
|
|
612
|
+
> 提供一个统一入口 `sentinel.execute()`,自动走完 Guard → Execute → Verify + Audit + Memory + Evaluator 三层面。
|
|
613
|
+
> 使用前务必阅读 [sentinel-guard SKILL.md](../skills/sentinel-guard/SKILL.md)。
|
|
614
|
+
|
|
615
|
+
### 统一入口 vs 传统两段式
|
|
616
|
+
|
|
617
|
+
**传统方式(preCheck + postCheck 两次调用)**:
|
|
618
|
+
|
|
619
|
+
```javascript
|
|
620
|
+
const guard = require('./sentinel-guard');
|
|
621
|
+
const check = guard.preCheck('exec', { command: '...' }); // 只做拦截
|
|
622
|
+
if (!check.passed) return ...;
|
|
623
|
+
// 执行...
|
|
624
|
+
guard.postCheck('exec', params, result); // 只做审计
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
**统一入口(推荐)**:
|
|
628
|
+
|
|
629
|
+
```javascript
|
|
630
|
+
const sentinel = require('./sentinel-guard');
|
|
631
|
+
const result = await sentinel.execute('exec', { command: 'npm test' }, () => exec('npm test'));
|
|
632
|
+
|
|
633
|
+
// result.allowed — Guard 是否放行
|
|
634
|
+
// result.result — 执行函数的返回值
|
|
635
|
+
// result.auditId — 审计 ID
|
|
636
|
+
// result.verify — Verify Gate 状态 (PASS/WARN/FAIL)
|
|
637
|
+
// result.profile — 当前质量评分 (0-100)
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### 执行流程详解
|
|
641
|
+
|
|
642
|
+
```
|
|
643
|
+
sentinel.execute(toolName, params, fn)
|
|
644
|
+
│
|
|
645
|
+
├─ 第1层: 确定性命令/文件拦截 (<1μs,零 LLM)
|
|
646
|
+
│ ├─ 危险命令正则匹配 (rm -rf /, mkfs, dd if=, fork bomb...)
|
|
647
|
+
│ ├─ 警告命令正则匹配 (git push --force, npm publish, DROP TABLE...)
|
|
648
|
+
│ ├─ 敏感文件 glob 匹配 (.env, *.key, *.pem, .git/**...)
|
|
649
|
+
│ └─ 保护文件匹配 (package.json, MEMORY.md, AGENTS.md...)
|
|
650
|
+
│
|
|
651
|
+
├─ 第2层: AgentOS Pipeline
|
|
652
|
+
│ ├─ Schema Gate — 参数格式校验 (required/types/pathDeny/maxSize/secrets)
|
|
653
|
+
│ ├─ Risk Gate — 四维公式评分 → auto/notify/confirm/deny
|
|
654
|
+
│ └─ Snapshot Gate — 执行前文件 hash 快照
|
|
655
|
+
│
|
|
656
|
+
├─ 第3层: 实际执行 fn()
|
|
657
|
+
│ └─ 异常捕获,失败也记录审计
|
|
658
|
+
│
|
|
659
|
+
├─ 第4层: Verify + Audit + Evaluator
|
|
660
|
+
│ ├─ Verify Gate — 8 项确定性校验 (文件存在/变更/lint/格式...)
|
|
661
|
+
│ ├─ Audit Log — JSONL 不可篡改审计
|
|
662
|
+
│ ├─ PreExecEvaluator — 参数质量+上下文利用评分
|
|
663
|
+
│ ├─ RuntimeEvaluator — 重试/超时/自纠正评分
|
|
664
|
+
│ ├─ PostExecEvaluator — 验证通过/用户接受/结果利用率评分
|
|
665
|
+
│ └─ AgentProfiler — 综合评分 0-100 + 趋势 + 亮点/警告
|
|
666
|
+
│
|
|
667
|
+
├─ 第5层: Memory 同步
|
|
668
|
+
│ ├─ Working Memory — 消息+工具结果缓存
|
|
669
|
+
│ ├─ Episodic Memory — 事件记录
|
|
670
|
+
│ └─ Semantic Memory — (自动迁移时写入)
|
|
671
|
+
│
|
|
672
|
+
└─ 返回统一结果
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
### execute() 完整返回值
|
|
676
|
+
|
|
677
|
+
| 字段 | 类型 | 说明 |
|
|
678
|
+
|------|------|------|
|
|
679
|
+
| `allowed` | boolean | 是否通过 Guard 层 |
|
|
680
|
+
| `blocked` | boolean | 是否被拦截 |
|
|
681
|
+
| `risk` | `'auto' | 'confirm' | 'deny'` | 风险等级 |
|
|
682
|
+
| `reason` | string? | 拦截/警告原因(blocked=true 时) |
|
|
683
|
+
| `needsConfirmation` | boolean? | 是否需要用户确认 |
|
|
684
|
+
| `result` | any | 执行函数的返回值 |
|
|
685
|
+
| `error` | string? | 执行异常信息 |
|
|
686
|
+
| `auditId` | string | 审计条目 ID(如 `op_5`) |
|
|
687
|
+
| `verify` | `'PASS' | 'WARN' | 'FAIL'` | Verify Gate 状态 |
|
|
688
|
+
| `profile` | number | 当前 Agent 质量评分 (0-100) |
|
|
689
|
+
|
|
690
|
+
**被拦截示例**:
|
|
691
|
+
|
|
692
|
+
```json
|
|
693
|
+
{
|
|
694
|
+
"allowed": false,
|
|
695
|
+
"blocked": true,
|
|
696
|
+
"risk": "DENY",
|
|
697
|
+
"reason": "🚫 危险命令: rm -rf / — 删除整个系统"
|
|
698
|
+
}
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
**正常通过示例**:
|
|
702
|
+
|
|
703
|
+
```json
|
|
704
|
+
{
|
|
705
|
+
"allowed": true,
|
|
706
|
+
"blocked": false,
|
|
707
|
+
"risk": "auto",
|
|
708
|
+
"result": "hello output",
|
|
709
|
+
"auditId": "op_5",
|
|
710
|
+
"verify": "PASS",
|
|
711
|
+
"profile": 85
|
|
712
|
+
}
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
### 自动迁移 MEMORY.md → Semantic Memory
|
|
716
|
+
|
|
717
|
+
首次加载 sentinel-guard 时自动执行,仅一次:
|
|
718
|
+
|
|
719
|
+
1. 解析 `MEMORY.md` 的 Markdown 结构(支持 bullet 列表和表格行)
|
|
720
|
+
2. 提取用户事实 (👤 关于老板 / 🆔 关于我) → `semantic.addFact()`
|
|
721
|
+
3. 提取工作方式规则 (🤖 我的工作方式) → `semantic.learnRule()`
|
|
722
|
+
4. 提取项目上下文 (📦 coderev / agentos) → `semantic.setProjectContext()`
|
|
723
|
+
5. 提取环境记录 (💻 环境记录) → `semantic.addFact()`
|
|
724
|
+
6. 提取关键决策 (💡 关键决策记录) → `episodic.record('decision', ...)`
|
|
725
|
+
7. 生成 `.sentinel-migrated` 标记文件防止重复
|
|
726
|
+
|
|
727
|
+
迁移后原有 MEMORY.md 不受影响(只读不写),两套记忆并行:
|
|
728
|
+
|
|
729
|
+
| 记忆系统 | 用途 | 格式 |
|
|
730
|
+
|---------|------|------|
|
|
731
|
+
| `MEMORY.md` | 人类编辑,session 注入上下文 | Markdown |
|
|
732
|
+
| AgentOS Semantic Memory | 程序读写,自动学习 | 结构化 JSON (`.agentos/`) |
|
|
733
|
+
|
|
734
|
+
### 规则配置 guard-rules.json
|
|
735
|
+
|
|
736
|
+
所有 Guard 黑白名单可通过 `guard-rules.json` 直接编辑,无需改源码:
|
|
737
|
+
|
|
738
|
+
```json
|
|
739
|
+
{
|
|
740
|
+
"dangerous": [
|
|
741
|
+
["rm -rf /", "删除整个系统"],
|
|
742
|
+
["sudo rm", "超级用户删除"],
|
|
743
|
+
["mkfs", "格式化磁盘"]
|
|
744
|
+
],
|
|
745
|
+
"warning": [
|
|
746
|
+
["git push --force", "强制覆盖远程分支"],
|
|
747
|
+
["npm publish\\b", "发布 npm 公共包"],
|
|
748
|
+
["DROP (TABLE|DATABASE)", "删除数据库"]
|
|
749
|
+
],
|
|
750
|
+
"sensitiveFiles": [
|
|
751
|
+
".env", ".env.*", "*.key", "*.pem",
|
|
752
|
+
".git/**", "**/credentials/**"
|
|
753
|
+
],
|
|
754
|
+
"protectedFiles": [
|
|
755
|
+
"package.json", "MEMORY.md", "AGENTS.md", "SOUL.md"
|
|
756
|
+
],
|
|
757
|
+
"schema": [
|
|
758
|
+
{ "tool": "exec", "required": ["command"] },
|
|
759
|
+
{
|
|
760
|
+
"tool": "write",
|
|
761
|
+
"required": ["path", "content"],
|
|
762
|
+
"pathDeny": [".env", "*.key", ".git/**"],
|
|
763
|
+
"maxSize": { "content": 1048576 },
|
|
764
|
+
"secrets": ["content"]
|
|
765
|
+
}
|
|
766
|
+
]
|
|
767
|
+
}
|
|
768
|
+
```
|
|
769
|
+
|
|
770
|
+
| 配置项 | 类型 | 说明 |
|
|
771
|
+
|--------|------|------|
|
|
772
|
+
| `dangerous` | `[regex, desc][]` | 匹配到直接拒绝 |
|
|
773
|
+
| `warning` | `[regex, desc][]` | 匹配到需用户确认 |
|
|
774
|
+
| `sensitiveFiles` | `string[]` | glob 模式,禁止读写 |
|
|
775
|
+
| `protectedFiles` | `string[]` | 精确匹配,禁止删除 |
|
|
776
|
+
| `schema` | `SchemaRule[]` | AgentOS Schema Gate 规则 |
|
|
777
|
+
|
|
778
|
+
### 完整 API 参考
|
|
779
|
+
|
|
780
|
+
```javascript
|
|
781
|
+
const sentinel = require('./sentinel-guard');
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
| 方法 | 类型 | 说明 |
|
|
785
|
+
|------|------|------|
|
|
786
|
+
| `execute(tool, params, fn, opts?)` | async | **统一入口**,走完三层 |
|
|
787
|
+
| `preCheck(tool, params)` | sync | [兼容] 仅确定性拦截 |
|
|
788
|
+
| `postCheck(tool, params, result)` | sync | [兼容] 仅审计记录 |
|
|
789
|
+
| `injectContext()` | sync → string | Session 启动时注入 Memory 上下文 |
|
|
790
|
+
| `endSession()` | sync | Session 结束:追加 daily log → 清空 Working → 同步 Episodic |
|
|
791
|
+
| `status()` | sync → string | AgentOS 状态报告 |
|
|
792
|
+
| `fullStatus()` | sync → object | 完整状态快照 (JSON) |
|
|
793
|
+
| `compactReport()` | sync → string | 精简版评估报告 |
|
|
794
|
+
| `fullReport()` | sync → string | 完整评估报告 |
|
|
795
|
+
| `audit(limit?)` | sync → array | 最近 N 条审计记录 |
|
|
796
|
+
| `feedback(signal)` | sync | 记录用户反馈信号 |
|
|
797
|
+
|
|
798
|
+
### Session 生命周期
|
|
799
|
+
|
|
800
|
+
```
|
|
801
|
+
Session 启动
|
|
802
|
+
├─ require('./sentinel-guard') ← 首次自动迁移 MEMORY.md
|
|
803
|
+
├─ sentinel.injectContext() ← 注入 Semantic+Episodic 上下文
|
|
804
|
+
│
|
|
805
|
+
Session 运行中
|
|
806
|
+
├─ sentinel.execute('exec', ...) ← 每次工具调用
|
|
807
|
+
├─ sentinel.execute('write', ...)
|
|
808
|
+
├─ sentinel.feedback('user_used_result') ← 关键节点记录反馈
|
|
809
|
+
│
|
|
810
|
+
Session 结束
|
|
811
|
+
└─ sentinel.endSession() ← 追加 daily log + 清空 Working
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
### AgentOS 与 sentinel-guard 功能对照
|
|
815
|
+
|
|
816
|
+
| 功能 | AgentOS 源码 | sentinel-guard skill | 覆盖率 |
|
|
817
|
+
|------|------------|---------------------|:------:|
|
|
818
|
+
| Schema Gate (12 项校验) | ✅ `schema-gate.ts` | ✅ `execute()` 内自动 | 100% |
|
|
819
|
+
| Risk Gate (四维公式) | ✅ `risk-gate.ts` | ✅ `execute()` 内自动 | 100% |
|
|
820
|
+
| 确定性命令拦截 | ❌ (依赖 Sandbox) | ✅ 正则匹配 (<1μs) | **额外增强** |
|
|
821
|
+
| Snapshot Gate | ✅ `snapshot-verify.ts` | ✅ `execute()` 内自动 | 100% |
|
|
822
|
+
| Verify Gate (8 项校验) | ✅ `snapshot-verify.ts` | ✅ `execute()` 内自动 | 100% |
|
|
823
|
+
| Audit Log (JSONL) | ✅ `audit-log.ts` | ✅ 双写 (AgentOS + 自身) | 100% |
|
|
824
|
+
| 规则可配置 | ❌ (代码硬编码) | ✅ `guard-rules.json` | **额外增强** |
|
|
825
|
+
| Working Memory | ✅ `working.ts` | ✅ 消息+工具缓存 | 100% |
|
|
826
|
+
| Episodic Memory | ✅ `episodic.ts` | ✅ 事件自动记录 | 100% |
|
|
827
|
+
| Semantic Memory | ✅ `semantic.ts` | ✅ 自动迁移+初始值 | 100% |
|
|
828
|
+
| MEMORY.md 迁移 | ✅ `memory-bridge.ts` | ✅ 首次加载自动跑 | 100% |
|
|
829
|
+
| Session 注入上下文 | ✅ `injectContext()` | ✅ `sentinel.injectContext()` | 100% |
|
|
830
|
+
| Session 清理 | ✅ `endSession()` | ✅ 含 daily log | 100% |
|
|
831
|
+
| PreExecEvaluator | ✅ `exec-evaluator.ts` | ✅ `execute()` 内自动 | 100% |
|
|
832
|
+
| RuntimeEvaluator | ✅ `exec-evaluator.ts` | ✅ `execute()` 内自动 | 100% |
|
|
833
|
+
| PostExecEvaluator | ✅ `exec-evaluator.ts` | ✅ `execute()` 内自动 | 100% |
|
|
834
|
+
| AgentProfiler | ✅ `profiler.ts` | ✅ `execute()` 返回 profile | 100% |
|
|
835
|
+
| ImplicitFeedback | ✅ `feedback.ts` | ✅ `recordFeedback()` | 100% |
|
|
836
|
+
| Daily Log 注入 | ✅ `evaluation-bridge.ts` | ✅ `endSession()` 自动 | 100% |
|
|
837
|
+
| Compact/Full Report | ✅ `evaluation-bridge.ts` | ✅ `compactReport()`/`fullReport()` | 100% |
|
|
838
|
+
| Sandbox 沙箱 | ✅ `sandbox.ts` | ❌ (暂未接入) | 0% |
|
|
839
|
+
| HTTP API | ✅ `server.ts` | ❌ (skill 为进程内调用) | N/A |
|
|
840
|
+
| **按设计范围总计** | **20 项** | **20/20** | **100%** |
|
|
841
|
+
|
|
842
|
+
### 快速入门(5 步接入)
|
|
843
|
+
|
|
844
|
+
```javascript
|
|
845
|
+
// 1. 加载 skill(自动迁移 MEMORY.md)
|
|
846
|
+
const sentinel = require('./sentinel-guard');
|
|
847
|
+
|
|
848
|
+
// 2. Session 启动 — 注入记忆上下文
|
|
849
|
+
const context = sentinel.injectContext();
|
|
850
|
+
|
|
851
|
+
// 3. 工具调用 — 统一入口,三层全走
|
|
852
|
+
const r = await sentinel.execute('write',
|
|
853
|
+
{ path: 'src/main.ts', content: 'console.log("hi")' },
|
|
854
|
+
() => fs.writeFileSync('src/main.ts', 'console.log("hi")')
|
|
855
|
+
);
|
|
856
|
+
if (!r.allowed) return { blocked: true, reason: r.reason };
|
|
857
|
+
|
|
858
|
+
// 4. 记录反馈
|
|
859
|
+
sentinel.feedback('user_used_result');
|
|
860
|
+
|
|
861
|
+
// 5. Session 结束 — 追加 daily log + 清理
|
|
862
|
+
sentinel.endSession();
|
|
863
|
+
```
|
|
864
|
+
|
|
865
|
+
完整示例代码见 `sentinel-guard/SKILL.md`,配置文件见 `sentinel-guard/guard-rules.json`。
|
|
866
|
+
|
|
867
|
+
---
|
|
868
|
+
|
|
607
869
|
### API 层 · SDK API
|
|
608
870
|
|
|
609
871
|
```typescript
|