@aporthq/aport-agent-guardrails 1.0.8

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.
Files changed (237) hide show
  1. package/LICENSE +217 -0
  2. package/README.md +481 -0
  3. package/bin/agent-guardrails +133 -0
  4. package/bin/aport-create-passport.sh +444 -0
  5. package/bin/aport-cursor-hook.sh +90 -0
  6. package/bin/aport-guardrail-api.sh +108 -0
  7. package/bin/aport-guardrail-bash.sh +394 -0
  8. package/bin/aport-guardrail-v2.sh +5 -0
  9. package/bin/aport-guardrail.sh +5 -0
  10. package/bin/aport-resolve-paths.sh +71 -0
  11. package/bin/aport-status.sh +276 -0
  12. package/bin/frameworks/crewai.sh +49 -0
  13. package/bin/frameworks/cursor.sh +95 -0
  14. package/bin/frameworks/langchain.sh +48 -0
  15. package/bin/frameworks/n8n.sh +36 -0
  16. package/bin/frameworks/openclaw.sh +19 -0
  17. package/bin/lib/allowlist.sh +18 -0
  18. package/bin/lib/common.sh +28 -0
  19. package/bin/lib/config.sh +46 -0
  20. package/bin/lib/constants.sh +232 -0
  21. package/bin/lib/detect.sh +65 -0
  22. package/bin/lib/error.sh +269 -0
  23. package/bin/lib/passport.sh +19 -0
  24. package/bin/lib/templates/.gitkeep +1 -0
  25. package/bin/lib/templates/config.yaml +6 -0
  26. package/bin/lib/validation.sh +206 -0
  27. package/bin/openclaw +660 -0
  28. package/docs/ADDING_A_FRAMEWORK.md +87 -0
  29. package/docs/AGENTS.md.example +40 -0
  30. package/docs/CODE_REVIEW.md +192 -0
  31. package/docs/DEPLOYMENT_READINESS.md +81 -0
  32. package/docs/FAQ_SECURITY_SCANNERS.md +373 -0
  33. package/docs/FRAMEWORK_ROADMAP.md +41 -0
  34. package/docs/HOSTED_PASSPORT_SETUP.md +362 -0
  35. package/docs/IMPLEMENTING_YOUR_OWN_EVALUATOR.md +433 -0
  36. package/docs/OPENCLAW_COMPATIBILITY.md +73 -0
  37. package/docs/OPENCLAW_LOCAL_INTEGRATION.md +596 -0
  38. package/docs/OPENCLAW_TOOLS_AND_POLICIES.md +54 -0
  39. package/docs/QUICKSTART.md +470 -0
  40. package/docs/QUICKSTART_OPENCLAW_PLUGIN.md +470 -0
  41. package/docs/README.md +28 -0
  42. package/docs/RELEASE.md +87 -0
  43. package/docs/REPO_LAYOUT.md +47 -0
  44. package/docs/SKILLS_ECOSYSTEM_ANALYSIS_FEB17.md +1260 -0
  45. package/docs/TOOL_POLICY_MAPPING.md +46 -0
  46. package/docs/UPGRADE.md +46 -0
  47. package/docs/VERIFICATION_METHODS.md +97 -0
  48. package/docs/assets/README.md +8 -0
  49. package/docs/assets/porter.svg +54 -0
  50. package/docs/development/ERROR_CODES.md +616 -0
  51. package/docs/frameworks/GITHUB_ISSUE_PROPOSALS.md +1105 -0
  52. package/docs/frameworks/crewai.md +114 -0
  53. package/docs/frameworks/cursor.md +159 -0
  54. package/docs/frameworks/langchain.md +72 -0
  55. package/docs/frameworks/n8n.md +40 -0
  56. package/docs/frameworks/openclaw.md +40 -0
  57. package/docs/launch/ADD_APORT_AWESOME_LISTS_INSTRUCTIONS.md +146 -0
  58. package/docs/launch/ANNOUNCEMENT_GUIDE.md +266 -0
  59. package/docs/launch/AWESOME_REPOS.md +53 -0
  60. package/docs/launch/CURSOR_VSCODE_HOOKS_RESEARCH.md +77 -0
  61. package/docs/launch/DEMO_TERMINAL_OUTPUT.txt +48 -0
  62. package/docs/launch/DRY_AND_PLAN_CHECKLIST.md +47 -0
  63. package/docs/launch/EVIDENCE_README.md +61 -0
  64. package/docs/launch/EVIDENCE_TERMINAL_CAPTURE.txt +10 -0
  65. package/docs/launch/FRAMEWORK_SUPPORT_PLAN.md +1640 -0
  66. package/docs/launch/LAUNCH_READINESS_CHECKLIST.md +237 -0
  67. package/docs/launch/LAUNCH_STRATEGY_SUMMARY.md +464 -0
  68. package/docs/launch/OPENCLAW_FEEDBACK_AND_FIXES.md +85 -0
  69. package/docs/launch/POST_1_VALENTINE_IMPROVED.md +233 -0
  70. package/docs/launch/POST_2_GUARDRAIL_IMPROVED.md +369 -0
  71. package/docs/launch/PRE_LAUNCH_FIXES.md +766 -0
  72. package/docs/launch/QUICK_LAUNCH_CHECKLIST.md +400 -0
  73. package/docs/launch/READINESS_SUMMARY.md +262 -0
  74. package/docs/launch/README.md +68 -0
  75. package/docs/launch/USER_STORIES.md +327 -0
  76. package/docs/launch/scripts/add-aport-awesome-pr.sh +69 -0
  77. package/docs/operations/MONITORING.md +588 -0
  78. package/docs/reviews/2026-02-18-staff-review.md +268 -0
  79. package/extensions/openclaw-aport/README.md +415 -0
  80. package/extensions/openclaw-aport/index.js +625 -0
  81. package/extensions/openclaw-aport/openclaw-aport.js +7 -0
  82. package/extensions/openclaw-aport/openclaw.plugin.json +46 -0
  83. package/extensions/openclaw-aport/package.json +36 -0
  84. package/extensions/openclaw-aport/test.js +307 -0
  85. package/external/aport-policies/README.md +363 -0
  86. package/external/aport-policies/agent.session.create.v1/README.md +345 -0
  87. package/external/aport-policies/agent.session.create.v1/policy.json +162 -0
  88. package/external/aport-policies/agent.tool.register.v1/README.md +361 -0
  89. package/external/aport-policies/agent.tool.register.v1/policy.json +172 -0
  90. package/external/aport-policies/code.release.publish.v1/README.md +51 -0
  91. package/external/aport-policies/code.release.publish.v1/policy.json +121 -0
  92. package/external/aport-policies/code.repository.merge.v1/README.md +287 -0
  93. package/external/aport-policies/code.repository.merge.v1/express.example.js +332 -0
  94. package/external/aport-policies/code.repository.merge.v1/fastapi.example.py +370 -0
  95. package/external/aport-policies/code.repository.merge.v1/policy.json +162 -0
  96. package/external/aport-policies/data.export.create.v1/README.md +226 -0
  97. package/external/aport-policies/data.export.create.v1/express.example.js +172 -0
  98. package/external/aport-policies/data.export.create.v1/fastapi.example.py +165 -0
  99. package/external/aport-policies/data.export.create.v1/policy.json +133 -0
  100. package/external/aport-policies/data.report.ingest.v1/README.md +134 -0
  101. package/external/aport-policies/data.report.ingest.v1/express.example.js +105 -0
  102. package/external/aport-policies/data.report.ingest.v1/minimal-example.js +68 -0
  103. package/external/aport-policies/data.report.ingest.v1/policy.json +174 -0
  104. package/external/aport-policies/finance.crypto.trade.v1/README.md +146 -0
  105. package/external/aport-policies/finance.crypto.trade.v1/express.example.js +109 -0
  106. package/external/aport-policies/finance.crypto.trade.v1/minimal-example.js +65 -0
  107. package/external/aport-policies/finance.crypto.trade.v1/policy.json +176 -0
  108. package/external/aport-policies/finance.payment.charge.v1/README.md +326 -0
  109. package/external/aport-policies/finance.payment.charge.v1/express.example.js +250 -0
  110. package/external/aport-policies/finance.payment.charge.v1/fastapi.example.py +227 -0
  111. package/external/aport-policies/finance.payment.charge.v1/minimal-example.js +64 -0
  112. package/external/aport-policies/finance.payment.charge.v1/policy.json +224 -0
  113. package/external/aport-policies/finance.payment.charge.v1/tests/contexts.jsonl +12 -0
  114. package/external/aport-policies/finance.payment.charge.v1/tests/expected.jsonl +12 -0
  115. package/external/aport-policies/finance.payment.charge.v1/tests/passport.instance.json +42 -0
  116. package/external/aport-policies/finance.payment.charge.v1/tests/passport.template.json +40 -0
  117. package/external/aport-policies/finance.payment.charge.v1/tests/payments-charge-policy.test.js +817 -0
  118. package/external/aport-policies/finance.payment.charge.v1/tests/test_payments_charge_policy.py +486 -0
  119. package/external/aport-policies/finance.payment.payout.v1/README.md +78 -0
  120. package/external/aport-policies/finance.payment.payout.v1/policy.json +181 -0
  121. package/external/aport-policies/finance.payment.refund.v1/README.md +275 -0
  122. package/external/aport-policies/finance.payment.refund.v1/express.example.js +167 -0
  123. package/external/aport-policies/finance.payment.refund.v1/fastapi.example.py +136 -0
  124. package/external/aport-policies/finance.payment.refund.v1/minimal-example.js +183 -0
  125. package/external/aport-policies/finance.payment.refund.v1/policy.json +216 -0
  126. package/external/aport-policies/finance.payment.refund.v1/tests/refunds-policy.test.js +924 -0
  127. package/external/aport-policies/finance.payment.refund.v1/tests/test_refunds_policy.py +778 -0
  128. package/external/aport-policies/finance.transaction.execute.v1/README.md +309 -0
  129. package/external/aport-policies/finance.transaction.execute.v1/express.example.js +261 -0
  130. package/external/aport-policies/finance.transaction.execute.v1/fastapi.example.py +231 -0
  131. package/external/aport-policies/finance.transaction.execute.v1/minimal-example.js +78 -0
  132. package/external/aport-policies/finance.transaction.execute.v1/policy.json +189 -0
  133. package/external/aport-policies/finance.transaction.execute.v1/tests/contexts.jsonl +12 -0
  134. package/external/aport-policies/finance.transaction.execute.v1/tests/expected.jsonl +12 -0
  135. package/external/aport-policies/finance.transaction.execute.v1/tests/passport.instance.json +42 -0
  136. package/external/aport-policies/finance.transaction.execute.v1/tests/passport.template.json +42 -0
  137. package/external/aport-policies/finance.transaction.execute.v1/tests/test_transactions_policy.py +214 -0
  138. package/external/aport-policies/finance.transaction.execute.v1/tests/transactions-policy.test.js +306 -0
  139. package/external/aport-policies/governance.data.access.v1/README.md +292 -0
  140. package/external/aport-policies/governance.data.access.v1/express.example.js +321 -0
  141. package/external/aport-policies/governance.data.access.v1/fastapi.example.py +279 -0
  142. package/external/aport-policies/governance.data.access.v1/minimal-example.js +65 -0
  143. package/external/aport-policies/governance.data.access.v1/policy.json +208 -0
  144. package/external/aport-policies/governance.data.access.v1/tests/contexts.jsonl +12 -0
  145. package/external/aport-policies/governance.data.access.v1/tests/data-access-policy.test.js +308 -0
  146. package/external/aport-policies/governance.data.access.v1/tests/expected.jsonl +12 -0
  147. package/external/aport-policies/governance.data.access.v1/tests/passport.instance.json +56 -0
  148. package/external/aport-policies/governance.data.access.v1/tests/passport.template.json +56 -0
  149. package/external/aport-policies/governance.data.access.v1/tests/test_data_access_policy.py +214 -0
  150. package/external/aport-policies/legal.contract.review.v1/README.md +109 -0
  151. package/external/aport-policies/legal.contract.review.v1/policy.json +378 -0
  152. package/external/aport-policies/legal.contract.review.v1/tests/legal-contract-review-policy.test.js +609 -0
  153. package/external/aport-policies/legal.contract.review.v1/tests/passport.template.json +49 -0
  154. package/external/aport-policies/mcp.tool.execute.v1/README.md +301 -0
  155. package/external/aport-policies/mcp.tool.execute.v1/policy.json +141 -0
  156. package/external/aport-policies/messaging.message.send.v1/README.md +230 -0
  157. package/external/aport-policies/messaging.message.send.v1/express.example.js +183 -0
  158. package/external/aport-policies/messaging.message.send.v1/fastapi.example.py +193 -0
  159. package/external/aport-policies/messaging.message.send.v1/policy.json +144 -0
  160. package/external/aport-policies/policy-template.json +107 -0
  161. package/external/aport-policies/system.command.execute.v1/README.md +275 -0
  162. package/external/aport-policies/system.command.execute.v1/policy.json +146 -0
  163. package/external/aport-spec/CONTRIBUTING.md +273 -0
  164. package/external/aport-spec/LICENSE +21 -0
  165. package/external/aport-spec/README.md +168 -0
  166. package/external/aport-spec/conformance/README.md +294 -0
  167. package/external/aport-spec/conformance/cases/data.export.v1/contexts/allow_users.json +6 -0
  168. package/external/aport-spec/conformance/cases/data.export.v1/contexts/deny_pii.json +6 -0
  169. package/external/aport-spec/conformance/cases/data.export.v1/expected/allow_users.decision.json +19 -0
  170. package/external/aport-spec/conformance/cases/data.export.v1/expected/deny_pii.decision.json +19 -0
  171. package/external/aport-spec/conformance/cases/data.export.v1/passports/template.json +29 -0
  172. package/external/aport-spec/conformance/cases/payments.refunds.v1/contexts/allow_50usd.json +9 -0
  173. package/external/aport-spec/conformance/cases/payments.refunds.v1/contexts/deny_150usd.json +9 -0
  174. package/external/aport-spec/conformance/cases/payments.refunds.v1/contexts/deny_currency.json +9 -0
  175. package/external/aport-spec/conformance/cases/payments.refunds.v1/expected/allow_50usd.decision.json +19 -0
  176. package/external/aport-spec/conformance/cases/payments.refunds.v1/expected/deny_150usd.decision.json +19 -0
  177. package/external/aport-spec/conformance/cases/payments.refunds.v1/expected/deny_currency.decision.json +19 -0
  178. package/external/aport-spec/conformance/cases/payments.refunds.v1/passports/template.json +42 -0
  179. package/external/aport-spec/conformance/package.json +44 -0
  180. package/external/aport-spec/conformance/pnpm-lock.yaml +642 -0
  181. package/external/aport-spec/conformance/src/cases.ts +371 -0
  182. package/external/aport-spec/conformance/src/ed25519.ts +167 -0
  183. package/external/aport-spec/conformance/src/jcs.ts +85 -0
  184. package/external/aport-spec/conformance/src/runner.ts +533 -0
  185. package/external/aport-spec/conformance/src/validators.ts +185 -0
  186. package/external/aport-spec/conformance/test-runner.js +315 -0
  187. package/external/aport-spec/conformance/tsconfig.json +21 -0
  188. package/external/aport-spec/error-schema.json +192 -0
  189. package/external/aport-spec/index.json +12 -0
  190. package/external/aport-spec/integrations/clawmoat/README.md +12 -0
  191. package/external/aport-spec/integrations/shield/README.md +245 -0
  192. package/external/aport-spec/integrations/shield/adapters/index.js +116 -0
  193. package/external/aport-spec/integrations/shield/adapters/system-command-execute.js +133 -0
  194. package/external/aport-spec/integrations/shield/test/README.md +58 -0
  195. package/external/aport-spec/integrations/shield/test/shield.md +40 -0
  196. package/external/aport-spec/integrations/shield/test/test-shield-to-verify.js +274 -0
  197. package/external/aport-spec/metrics-schema.json +504 -0
  198. package/external/aport-spec/oap/CHANGELOG.md +54 -0
  199. package/external/aport-spec/oap/VERSION.md +40 -0
  200. package/external/aport-spec/oap/capability-registry.md +229 -0
  201. package/external/aport-spec/oap/conformance.md +257 -0
  202. package/external/aport-spec/oap/decision-schema.json +114 -0
  203. package/external/aport-spec/oap/examples/context.refund.usd.50.json +9 -0
  204. package/external/aport-spec/oap/examples/decision.allow.sample.json +20 -0
  205. package/external/aport-spec/oap/examples/decision.deny.sample.json +23 -0
  206. package/external/aport-spec/oap/examples/passport.instance.v1.json +50 -0
  207. package/external/aport-spec/oap/examples/passport.template.v1.json +71 -0
  208. package/external/aport-spec/oap/oap-spec.md +426 -0
  209. package/external/aport-spec/oap/passport-schema.json +396 -0
  210. package/external/aport-spec/oap/security.md +213 -0
  211. package/external/aport-spec/oap/vc/context-oap-v1.jsonld +137 -0
  212. package/external/aport-spec/oap/vc/examples/oap-decision-vc.json +37 -0
  213. package/external/aport-spec/oap/vc/examples/oap-passport-vc.json +68 -0
  214. package/external/aport-spec/oap/vc/tools/INTEGRATION.md +375 -0
  215. package/external/aport-spec/oap/vc/tools/README.md +278 -0
  216. package/external/aport-spec/oap/vc/tools/examples/decision-to-vc.js +66 -0
  217. package/external/aport-spec/oap/vc/tools/examples/passport-to-vc.js +83 -0
  218. package/external/aport-spec/oap/vc/tools/examples/vc-to-decision.js +77 -0
  219. package/external/aport-spec/oap/vc/tools/examples/vc-to-passport.js +94 -0
  220. package/external/aport-spec/oap/vc/tools/package.json +38 -0
  221. package/external/aport-spec/oap/vc/tools/pnpm-lock.yaml +472 -0
  222. package/external/aport-spec/oap/vc/tools/src/cli.ts +226 -0
  223. package/external/aport-spec/oap/vc/tools/src/crypto-utils.ts +427 -0
  224. package/external/aport-spec/oap/vc/tools/src/index.ts +653 -0
  225. package/external/aport-spec/oap/vc/tools/src/test.ts +148 -0
  226. package/external/aport-spec/oap/vc/tools/src/vp.ts +382 -0
  227. package/external/aport-spec/oap/vc/tools/test-simple.js +214 -0
  228. package/external/aport-spec/oap/vc/tools/tsconfig.json +19 -0
  229. package/external/aport-spec/oap/vc/vc-mapping.md +443 -0
  230. package/external/aport-spec/passport-schema.json +586 -0
  231. package/external/aport-spec/rate-limiting.md +136 -0
  232. package/external/aport-spec/transport-profile.md +325 -0
  233. package/external/aport-spec/webhook-spec.md +314 -0
  234. package/package.json +70 -0
  235. package/skills/aport-agent-guardrail/SKILL.md +314 -0
  236. package/src/evaluator.js +252 -0
  237. package/src/server/index.js +72 -0
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@aporthq/openclaw-aport",
3
+ "version": "1.0.0",
4
+ "description": "OpenClaw plugin for deterministic pre-action authorization via APort guardrails",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node test.js"
9
+ },
10
+ "keywords": [
11
+ "openclaw",
12
+ "openclaw-plugin",
13
+ "aport",
14
+ "security",
15
+ "guardrails",
16
+ "policy-enforcement"
17
+ ],
18
+ "author": "APort Technologies Inc.",
19
+ "license": "Apache-2.0",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/aporthq/aport-agent-guardrails.git",
23
+ "directory": "extensions/openclaw-aport"
24
+ },
25
+ "peerDependencies": {
26
+ "openclaw": ">=2026.2.0"
27
+ },
28
+ "engines": {
29
+ "node": ">=18.0.0"
30
+ },
31
+ "openclaw": {
32
+ "extensions": [
33
+ "openclaw-aport"
34
+ ]
35
+ }
36
+ }
@@ -0,0 +1,307 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Unit, integration, and performance tests for APort OpenClaw plugin.
4
+ * Run: node test.js
5
+ */
6
+
7
+ import { describe, it } from 'node:test';
8
+ import assert from 'node:assert';
9
+ import { createHash } from 'crypto';
10
+ import { spawn } from 'child_process';
11
+ import { mkdtemp, writeFile, readFile, rm, mkdir } from 'fs/promises';
12
+ import { join } from 'path';
13
+ import { tmpdir } from 'os';
14
+ import { fileURLToPath } from 'url';
15
+ import { dirname } from 'path';
16
+ import {
17
+ mapToolToPolicy,
18
+ canonicalize,
19
+ verifyDecisionIntegrity,
20
+ } from './index.js';
21
+
22
+ const __dirname = dirname(fileURLToPath(import.meta.url));
23
+ const REPO_ROOT = join(__dirname, '..', '..');
24
+ const GUARDRAIL_SCRIPT = join(REPO_ROOT, 'bin', 'aport-guardrail-bash.sh');
25
+
26
+ describe('canonicalize', () => {
27
+ it('sorts keys at top level', () => {
28
+ assert.strictEqual(
29
+ canonicalize({ b: 1, a: 2 }),
30
+ '{"a":2,"b":1}',
31
+ );
32
+ });
33
+
34
+ it('sorts keys recursively in nested objects', () => {
35
+ assert.strictEqual(
36
+ canonicalize({ o: { z: 1, y: 2 } }),
37
+ '{"o":{"y":2,"z":1}}',
38
+ );
39
+ });
40
+
41
+ it('sorts keys in array elements', () => {
42
+ assert.strictEqual(
43
+ canonicalize({ reasons: [{ message: 'm', code: 'c' }] }),
44
+ '{"reasons":[{"code":"c","message":"m"}]}',
45
+ );
46
+ });
47
+
48
+ it('handles primitives and null', () => {
49
+ assert.strictEqual(canonicalize(null), 'null');
50
+ assert.strictEqual(canonicalize(1), '1');
51
+ assert.strictEqual(canonicalize('x'), '"x"');
52
+ });
53
+ });
54
+
55
+ describe('verifyDecisionIntegrity', () => {
56
+ it('returns true when content_hash is missing (legacy decision)', () => {
57
+ assert.strictEqual(verifyDecisionIntegrity({ allow: false }), true);
58
+ assert.strictEqual(verifyDecisionIntegrity(null), true);
59
+ });
60
+
61
+ it('returns true when content_hash matches computed hash', () => {
62
+ const decision = {
63
+ allow: false,
64
+ decision_id: 'dec-1',
65
+ reasons: [{ code: 'oap.denied', message: 'test' }],
66
+ };
67
+ const canonical = canonicalize(decision);
68
+ const hash =
69
+ 'sha256:' + createHash('sha256').update(canonical, 'utf8').digest('hex');
70
+ assert.strictEqual(
71
+ verifyDecisionIntegrity({ ...decision, content_hash: hash }),
72
+ true,
73
+ );
74
+ });
75
+
76
+ it('returns false when content is tampered (hash does not match)', () => {
77
+ const decision = {
78
+ allow: false,
79
+ decision_id: 'dec-1',
80
+ reasons: [{ code: 'oap.denied', message: 'test' }],
81
+ content_hash: 'sha256:wrong',
82
+ };
83
+ assert.strictEqual(verifyDecisionIntegrity(decision), false);
84
+ });
85
+
86
+ it('returns false when a field is changed after hash was computed', () => {
87
+ const decision = {
88
+ allow: false,
89
+ decision_id: 'dec-1',
90
+ reasons: [{ code: 'oap.denied', message: 'original' }],
91
+ };
92
+ const canonical = canonicalize(decision);
93
+ const hash =
94
+ 'sha256:' + createHash('sha256').update(canonical, 'utf8').digest('hex');
95
+ const tampered = {
96
+ ...decision,
97
+ reasons: [{ code: 'oap.denied', message: 'tampered' }],
98
+ content_hash: hash,
99
+ };
100
+ assert.strictEqual(verifyDecisionIntegrity(tampered), false);
101
+ });
102
+ });
103
+
104
+ describe('mapToolToPolicy', () => {
105
+ it('maps exec and exec.* to system.command.execute.v1', () => {
106
+ assert.strictEqual(mapToolToPolicy('exec'), 'system.command.execute.v1');
107
+ assert.strictEqual(mapToolToPolicy('exec.run'), 'system.command.execute.v1');
108
+ assert.strictEqual(mapToolToPolicy('exec.shell'), 'system.command.execute.v1');
109
+ assert.strictEqual(mapToolToPolicy('EXEC.RUN'), 'system.command.execute.v1');
110
+ });
111
+
112
+ it('maps system.command.* and bash/shell to system.command.execute.v1', () => {
113
+ assert.strictEqual(mapToolToPolicy('system.command.run'), 'system.command.execute.v1');
114
+ assert.strictEqual(mapToolToPolicy('bash'), 'system.command.execute.v1');
115
+ assert.strictEqual(mapToolToPolicy('shell'), 'system.command.execute.v1');
116
+ assert.strictEqual(mapToolToPolicy('command'), 'system.command.execute.v1');
117
+ });
118
+
119
+ it('maps git tools to code.repository.merge.v1', () => {
120
+ assert.strictEqual(mapToolToPolicy('git.create_pr'), 'code.repository.merge.v1');
121
+ assert.strictEqual(mapToolToPolicy('git.merge'), 'code.repository.merge.v1');
122
+ assert.strictEqual(mapToolToPolicy('git.push'), 'code.repository.merge.v1');
123
+ assert.strictEqual(mapToolToPolicy('git.commit'), 'code.repository.merge.v1');
124
+ });
125
+
126
+ it('maps messaging tools to messaging.message.send.v1', () => {
127
+ assert.strictEqual(mapToolToPolicy('message.send'), 'messaging.message.send.v1');
128
+ assert.strictEqual(mapToolToPolicy('messaging.slack'), 'messaging.message.send.v1');
129
+ });
130
+
131
+ it('maps mcp.* to mcp.tool.execute.v1', () => {
132
+ assert.strictEqual(mapToolToPolicy('mcp.foo'), 'mcp.tool.execute.v1');
133
+ });
134
+
135
+ it('maps session/agent.session to agent.session.create.v1', () => {
136
+ assert.strictEqual(mapToolToPolicy('agent.session.create'), 'agent.session.create.v1');
137
+ assert.strictEqual(mapToolToPolicy('session.create'), 'agent.session.create.v1');
138
+ });
139
+
140
+ it('maps payment/finance to finance policies', () => {
141
+ assert.strictEqual(mapToolToPolicy('payment.refund'), 'finance.payment.refund.v1');
142
+ assert.strictEqual(mapToolToPolicy('payment.charge'), 'finance.payment.charge.v1');
143
+ });
144
+
145
+ it('maps data.export and database.* to data.export.create.v1', () => {
146
+ assert.strictEqual(mapToolToPolicy('data.export'), 'data.export.create.v1');
147
+ assert.strictEqual(mapToolToPolicy('database.write'), 'data.export.create.v1');
148
+ });
149
+
150
+ it('returns null for unmapped tools', () => {
151
+ assert.strictEqual(mapToolToPolicy('unknown.tool'), null);
152
+ assert.strictEqual(mapToolToPolicy('read_file'), null);
153
+ });
154
+ });
155
+
156
+ describe('performance', () => {
157
+ it('mapToolToPolicy: 5000 calls complete in under 100ms', () => {
158
+ const tools = [
159
+ 'exec.run',
160
+ 'git.create_pr',
161
+ 'message.send',
162
+ 'unknown.tool',
163
+ ];
164
+ const start = process.hrtime.bigint();
165
+ for (let i = 0; i < 5000; i++) {
166
+ mapToolToPolicy(tools[i % tools.length]);
167
+ }
168
+ const elapsed = Number(process.hrtime.bigint() - start) / 1e6;
169
+ assert.ok(elapsed < 100, `mapToolToPolicy 5k calls took ${elapsed.toFixed(2)}ms (expected < 100ms)`);
170
+ });
171
+
172
+ it('verifyDecisionIntegrity: 1000 valid checks in under 50ms', () => {
173
+ const decision = {
174
+ allow: false,
175
+ decision_id: 'dec-1',
176
+ reasons: [{ code: 'oap.denied', message: 'test' }],
177
+ };
178
+ const canonical = canonicalize(decision);
179
+ const hash =
180
+ 'sha256:' + createHash('sha256').update(canonical, 'utf8').digest('hex');
181
+ const withHash = { ...decision, content_hash: hash };
182
+ const start = process.hrtime.bigint();
183
+ for (let i = 0; i < 1000; i++) {
184
+ assert.strictEqual(verifyDecisionIntegrity(withHash), true);
185
+ }
186
+ const elapsed = Number(process.hrtime.bigint() - start) / 1e6;
187
+ assert.ok(elapsed < 50, `verifyDecisionIntegrity 1k calls took ${elapsed.toFixed(2)}ms (expected < 50ms)`);
188
+ });
189
+
190
+ it('canonicalize: 2000 objects in under 30ms', () => {
191
+ const obj = {
192
+ allow: false,
193
+ decision_id: 'dec-1',
194
+ reasons: [{ code: 'c', message: 'm' }],
195
+ policy_id: 'system.command.execute.v1',
196
+ };
197
+ const start = process.hrtime.bigint();
198
+ for (let i = 0; i < 2000; i++) {
199
+ canonicalize(obj);
200
+ }
201
+ const elapsed = Number(process.hrtime.bigint() - start) / 1e6;
202
+ assert.ok(elapsed < 30, `canonicalize 2k calls took ${elapsed.toFixed(2)}ms (expected < 30ms)`);
203
+ });
204
+ });
205
+
206
+ /**
207
+ * Integration: run real guardrail script when repo is available.
208
+ * Ensures decision file has content_hash and chain fields; non-core writes don't block.
209
+ */
210
+ describe('integration (guardrail script)', () => {
211
+ it('writes decision with content_hash and chain; audit is non-blocking', async () => {
212
+ const { existsSync } = await import('fs');
213
+ if (!existsSync(GUARDRAIL_SCRIPT)) {
214
+ console.log(' (skip: bin/aport-guardrail-bash.sh not found)');
215
+ return;
216
+ }
217
+ const tmp = await mkdtemp(join(tmpdir(), 'aport-plugin-test-'));
218
+ const passportPath = join(tmp, 'passport.json');
219
+ const decisionsDir = join(tmp, 'decisions');
220
+ const auditLog = join(tmp, 'audit.log');
221
+ await mkdir(decisionsDir, { recursive: true });
222
+ const minimalPassport = {
223
+ spec_version: 'oap/1.0',
224
+ passport_id: 'test-passport',
225
+ agent_id: 'test-passport',
226
+ owner_id: 'test-owner',
227
+ status: 'active',
228
+ capabilities: [{ id: 'system.command.execute' }],
229
+ limits: {
230
+ 'system.command.execute': {
231
+ allowed_commands: ['node'],
232
+ blocked_patterns: [],
233
+ },
234
+ },
235
+ };
236
+ await writeFile(passportPath, JSON.stringify(minimalPassport));
237
+
238
+ const runScript = (toolName, contextJson, decisionPath) =>
239
+ new Promise((resolve, reject) => {
240
+ const proc = spawn(GUARDRAIL_SCRIPT, [toolName, contextJson], {
241
+ env: {
242
+ ...process.env,
243
+ OPENCLAW_PASSPORT_FILE: passportPath,
244
+ OPENCLAW_DECISION_FILE: decisionPath,
245
+ OPENCLAW_AUDIT_LOG: auditLog,
246
+ },
247
+ cwd: REPO_ROOT,
248
+ });
249
+ let stderr = '';
250
+ proc.stderr.on('data', (d) => (stderr += d));
251
+ proc.on('close', (code) => resolve({ code, stderr }));
252
+ proc.on('error', reject);
253
+ });
254
+
255
+ const decisionPath1 = join(decisionsDir, 'run1.json');
256
+ const res1 = await runScript(
257
+ 'system.command.execute',
258
+ JSON.stringify({ command: 'node --version' }),
259
+ decisionPath1,
260
+ );
261
+ if (res1.code !== 0) {
262
+ console.log(' (skip: guardrail exited with code=' + res1.code + ', stderr=' + (res1.stderr || '').slice(0, 200) + ')');
263
+ await rm(tmp, { recursive: true, force: true }).catch(() => {});
264
+ return;
265
+ }
266
+ let dec1;
267
+ try {
268
+ dec1 = JSON.parse(await readFile(decisionPath1, 'utf8'));
269
+ } catch (e) {
270
+ console.log(' (skip: decision file missing or invalid JSON)');
271
+ await rm(tmp, { recursive: true, force: true }).catch(() => {});
272
+ return;
273
+ }
274
+ if (!dec1.content_hash) {
275
+ console.log(' (skip: decision has no content_hash - need guardrail with tamper-resistant writes)');
276
+ await rm(tmp, { recursive: true, force: true }).catch(() => {});
277
+ return;
278
+ }
279
+ if (dec1.allow !== true) {
280
+ console.log(' (skip: guardrail denied - passport/limits may not allow node)');
281
+ await rm(tmp, { recursive: true, force: true }).catch(() => {});
282
+ return;
283
+ }
284
+ assert.ok(verifyDecisionIntegrity(dec1), 'content_hash must verify');
285
+
286
+ const decisionPath2 = join(decisionsDir, 'run2.json');
287
+ const res2 = await runScript(
288
+ 'system.command.execute',
289
+ JSON.stringify({ command: 'node --version' }),
290
+ decisionPath2,
291
+ );
292
+ if (res2.code !== 0) {
293
+ console.log(' (skip: second run exited code=' + res2.code + ')');
294
+ await rm(tmp, { recursive: true, force: true }).catch(() => {});
295
+ return;
296
+ }
297
+ const dec2 = JSON.parse(await readFile(decisionPath2, 'utf8'));
298
+ assert.ok(dec2.content_hash);
299
+ assert.ok(
300
+ dec2.prev_decision_id != null || dec2.prev_content_hash != null,
301
+ 'second decision should chain',
302
+ );
303
+ assert.ok(verifyDecisionIntegrity(dec2), 'second decision must verify');
304
+
305
+ await rm(tmp, { recursive: true, force: true });
306
+ });
307
+ });