attenu-guard 0.1.0

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 (112) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +202 -0
  3. package/README.md +200 -0
  4. package/SECURITY.md +34 -0
  5. package/bin/attenu-guard.js +4 -0
  6. package/dist/cjs/adapters/langgraph.d.ts +174 -0
  7. package/dist/cjs/adapters/langgraph.d.ts.map +1 -0
  8. package/dist/cjs/adapters/langgraph.js +242 -0
  9. package/dist/cjs/adapters/langgraph.js.map +1 -0
  10. package/dist/cjs/audit.d.ts +82 -0
  11. package/dist/cjs/audit.d.ts.map +1 -0
  12. package/dist/cjs/audit.js +195 -0
  13. package/dist/cjs/audit.js.map +1 -0
  14. package/dist/cjs/authority.d.ts +114 -0
  15. package/dist/cjs/authority.d.ts.map +1 -0
  16. package/dist/cjs/authority.js +238 -0
  17. package/dist/cjs/authority.js.map +1 -0
  18. package/dist/cjs/canonical.d.ts +91 -0
  19. package/dist/cjs/canonical.d.ts.map +1 -0
  20. package/dist/cjs/canonical.js +372 -0
  21. package/dist/cjs/canonical.js.map +1 -0
  22. package/dist/cjs/ceilings.d.ts +211 -0
  23. package/dist/cjs/ceilings.d.ts.map +1 -0
  24. package/dist/cjs/ceilings.js +476 -0
  25. package/dist/cjs/ceilings.js.map +1 -0
  26. package/dist/cjs/chain.d.ts +106 -0
  27. package/dist/cjs/chain.d.ts.map +1 -0
  28. package/dist/cjs/chain.js +0 -0
  29. package/dist/cjs/chain.js.map +1 -0
  30. package/dist/cjs/cli.d.ts +16 -0
  31. package/dist/cjs/cli.d.ts.map +1 -0
  32. package/dist/cjs/cli.js +100 -0
  33. package/dist/cjs/cli.js.map +1 -0
  34. package/dist/cjs/evidence.d.ts +163 -0
  35. package/dist/cjs/evidence.d.ts.map +1 -0
  36. package/dist/cjs/evidence.js +409 -0
  37. package/dist/cjs/evidence.js.map +1 -0
  38. package/dist/cjs/guard.d.ts +186 -0
  39. package/dist/cjs/guard.d.ts.map +1 -0
  40. package/dist/cjs/guard.js +432 -0
  41. package/dist/cjs/guard.js.map +1 -0
  42. package/dist/cjs/index.d.ts +52 -0
  43. package/dist/cjs/index.d.ts.map +1 -0
  44. package/dist/cjs/index.js +99 -0
  45. package/dist/cjs/index.js.map +1 -0
  46. package/dist/cjs/package.json +3 -0
  47. package/dist/cjs/reasons.d.ts +93 -0
  48. package/dist/cjs/reasons.d.ts.map +1 -0
  49. package/dist/cjs/reasons.js +139 -0
  50. package/dist/cjs/reasons.js.map +1 -0
  51. package/dist/cjs/strikes.d.ts +24 -0
  52. package/dist/cjs/strikes.d.ts.map +1 -0
  53. package/dist/cjs/strikes.js +30 -0
  54. package/dist/cjs/strikes.js.map +1 -0
  55. package/dist/cjs/wire.d.ts +83 -0
  56. package/dist/cjs/wire.d.ts.map +1 -0
  57. package/dist/cjs/wire.js +165 -0
  58. package/dist/cjs/wire.js.map +1 -0
  59. package/dist/esm/adapters/langgraph.d.ts +174 -0
  60. package/dist/esm/adapters/langgraph.d.ts.map +1 -0
  61. package/dist/esm/adapters/langgraph.js +199 -0
  62. package/dist/esm/adapters/langgraph.js.map +1 -0
  63. package/dist/esm/audit.d.ts +82 -0
  64. package/dist/esm/audit.d.ts.map +1 -0
  65. package/dist/esm/audit.js +189 -0
  66. package/dist/esm/audit.js.map +1 -0
  67. package/dist/esm/authority.d.ts +114 -0
  68. package/dist/esm/authority.d.ts.map +1 -0
  69. package/dist/esm/authority.js +233 -0
  70. package/dist/esm/authority.js.map +1 -0
  71. package/dist/esm/canonical.d.ts +91 -0
  72. package/dist/esm/canonical.d.ts.map +1 -0
  73. package/dist/esm/canonical.js +359 -0
  74. package/dist/esm/canonical.js.map +1 -0
  75. package/dist/esm/ceilings.d.ts +211 -0
  76. package/dist/esm/ceilings.d.ts.map +1 -0
  77. package/dist/esm/ceilings.js +460 -0
  78. package/dist/esm/ceilings.js.map +1 -0
  79. package/dist/esm/chain.d.ts +106 -0
  80. package/dist/esm/chain.d.ts.map +1 -0
  81. package/dist/esm/chain.js +0 -0
  82. package/dist/esm/chain.js.map +1 -0
  83. package/dist/esm/cli.d.ts +16 -0
  84. package/dist/esm/cli.d.ts.map +1 -0
  85. package/dist/esm/cli.js +97 -0
  86. package/dist/esm/cli.js.map +1 -0
  87. package/dist/esm/evidence.d.ts +163 -0
  88. package/dist/esm/evidence.d.ts.map +1 -0
  89. package/dist/esm/evidence.js +398 -0
  90. package/dist/esm/evidence.js.map +1 -0
  91. package/dist/esm/guard.d.ts +186 -0
  92. package/dist/esm/guard.d.ts.map +1 -0
  93. package/dist/esm/guard.js +427 -0
  94. package/dist/esm/guard.js.map +1 -0
  95. package/dist/esm/index.d.ts +52 -0
  96. package/dist/esm/index.d.ts.map +1 -0
  97. package/dist/esm/index.js +42 -0
  98. package/dist/esm/index.js.map +1 -0
  99. package/dist/esm/package.json +3 -0
  100. package/dist/esm/reasons.d.ts +93 -0
  101. package/dist/esm/reasons.d.ts.map +1 -0
  102. package/dist/esm/reasons.js +134 -0
  103. package/dist/esm/reasons.js.map +1 -0
  104. package/dist/esm/strikes.d.ts +24 -0
  105. package/dist/esm/strikes.d.ts.map +1 -0
  106. package/dist/esm/strikes.js +26 -0
  107. package/dist/esm/strikes.js.map +1 -0
  108. package/dist/esm/wire.d.ts +83 -0
  109. package/dist/esm/wire.d.ts.map +1 -0
  110. package/dist/esm/wire.js +159 -0
  111. package/dist/esm/wire.js.map +1 -0
  112. package/package.json +89 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,46 @@
1
+ # Changelog
2
+
3
+ All notable changes to this package are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this package
5
+ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.0] — 2026-08-28
8
+
9
+ First release: the TypeScript implementation of attenu-guard.
10
+
11
+ ### Added
12
+
13
+ - **Offline verifier.** `verifyBundle` checks integrity, monotonicity
14
+ (child ⊆ parent) and containment from an evidence bundle alone, with the
15
+ signed anchor verified when a key is supplied. HS256 and Ed25519 anchors are
16
+ both read. `AuditLog.verify` checks a raw `.jsonl` ledger's hash chain.
17
+ `delegationGraph` and `denials` fold a bundle into the reviewer's view.
18
+ - **Core enforcement.** `Authority` (scopes with `family.*` wildcards, typed
19
+ ceilings, TTL), `Guard.issue`, `delegate` (the meet: scopes intersect,
20
+ ceilings take the tighter bound, TTL the shorter; a request wider than the
21
+ parent comes back narrowed), `check` returning a `Decision`, `enforce`
22
+ throwing `AuthorityDenied`, `wouldAllow` and `wouldDelegate` as pure dry-runs,
23
+ chain depth and fanout ceilings, cascade `revoke` and principal-scoped
24
+ `revokeAgent`, strike policy, and a hash-chained audit log.
25
+ - **Ceilings:** `RowLimit`, `SpendCap`, `CallLimit` (optionally scoped, and
26
+ self-metering), `EgressRank`, `Allow`, `Deny`, `Prefix`, plus
27
+ `registerCeiling` for custom ones. An unrecognised constraint type denies
28
+ rather than going unenforced.
29
+ - **Evidence custody.** `exportBundle` with task redaction and a strict mode
30
+ that refuses to carry any field outside the published ledger allow-list.
31
+ - **LangGraph.js adapter** (`attenu-guard/adapters/langgraph`): `guardTool`,
32
+ `guardTools`, `guardNode`, `addGuardedNode`, `delegateTo`, `toolArgs`. The
33
+ adapter never imports the framework.
34
+ - **CLI:** `attenu-guard verify <file> [--hs256-key HEX | --pubkey HEX] [--kid KID]`,
35
+ matching the Python CLI's output lines and exit codes.
36
+ - **Cross-language fixtures.** `tools/gen_fixtures.py` generates
37
+ `test/fixtures/` from the Python package; the suite reproduces every vector,
38
+ and a further test has the Python CLI verify a ledger and bundle written here.
39
+
40
+ ### Notes
41
+
42
+ - Zero runtime dependencies. ESM and CommonJS builds, types included, Node 18+.
43
+ - Canonical JSON is byte-identical to Python's
44
+ `json.dumps(obj, sort_keys=True, separators=(",", ":"))`, including ASCII
45
+ escaping and number formatting. Documents that will be re-hashed are parsed
46
+ with number literals preserved.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Attenu
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # attenu-guard (TypeScript)
2
+
3
+ Permissions for multi-agent systems, enforced at the point of the call.
4
+
5
+ A sub-agent holds a subset of what its parent holds. Every tool call is checked
6
+ against the calling agent's own permissions, every handoff narrows them, and
7
+ every decision lands in a hash-chained log that anyone can verify offline —
8
+ without this library, its authors, or a network.
9
+
10
+ This is the TypeScript implementation. It writes the same ledger and the same
11
+ evidence bundle as the [Python library](https://github.com/attenu-io/attenu-guard),
12
+ and reads either one's output.
13
+
14
+ - Zero runtime dependencies — Node's own `crypto`, nothing else
15
+ - ESM and CommonJS builds, types included
16
+ - Node 20 and above (the LangGraph.js adapter depends on `langsmith`, which needs the global WebCrypto that Node 19 introduced; Node 18 is end-of-life)
17
+
18
+ ## Install
19
+
20
+ ```
21
+ npm install attenu-guard
22
+ ```
23
+
24
+ ## Verify a run you did not produce
25
+
26
+ You have a bundle exported from an agent run, and the public half of the key
27
+ that anchored it. You do not need an account, a network, or the system that
28
+ produced it.
29
+
30
+ ```
31
+ npx attenu-guard verify run.bundle.json --pubkey <hex>
32
+ ```
33
+
34
+ ```
35
+ integrity=True monotonicity=True containment=True anchor=verified nodes=3 actions_checked=2
36
+ OK
37
+ ```
38
+
39
+ Three things were checked from the file alone:
40
+
41
+ - **integrity** — every entry hashes to the next, and the head matches the signed
42
+ anchor. Nothing was inserted, removed, or rewritten afterwards.
43
+ - **monotonicity** — every delegated agent's permissions are a subset of its
44
+ parent's, all the way down.
45
+ - **containment** — every allowed action fell inside what the acting agent held
46
+ at the time.
47
+
48
+ Those are different findings. A broken chain says the record was edited. A
49
+ broken monotonicity check says the record is honest and the delegation was not —
50
+ which points at a different person. The same checks are available in code:
51
+
52
+ ```ts
53
+ import { parseBundle, verifyBundle, Ed25519Verifier } from "attenu-guard";
54
+ import { readFileSync } from "node:fs";
55
+
56
+ const bundle = parseBundle(readFileSync("run.bundle.json", "utf8"));
57
+ const report = verifyBundle(bundle, new Ed25519Verifier(Buffer.from(pubkeyHex, "hex")));
58
+
59
+ report.ok; // false if any check failed
60
+ report.checks; // { integrity, monotonicity, containment, anchor }
61
+ report.failures; // one line per finding
62
+ ```
63
+
64
+ Pass `null` instead of a key and the chain, subset and containment checks still
65
+ run; the anchor is reported as `not checked`, and `ok` then means *consistent*
66
+ rather than *verified* — a consistent full rewrite by someone holding the signing
67
+ key cannot be excluded without the key.
68
+
69
+ ## Guard a delegation chain
70
+
71
+ ```ts
72
+ import { Authority, Guard, RowLimit, EgressRank } from "attenu-guard";
73
+
74
+ const orchestrator = Guard.issue("orchestrator", new Authority({
75
+ scopes: ["crm.*", "mail.send"],
76
+ ceilings: [new RowLimit(100_000), new EgressRank("any")],
77
+ ttl: 3600,
78
+ }));
79
+
80
+ // The handoff is where permissions narrow. A request wider than the parent
81
+ // holds comes back narrowed — `fs.write` was never the orchestrator's to give.
82
+ const summarizer = orchestrator.delegate("summarizer", new Authority({
83
+ scopes: ["crm.read", "fs.write"],
84
+ ceilings: [new RowLimit(5_000), new EgressRank("none")],
85
+ ttl: 900,
86
+ }), "summarise the Q3 pipeline");
87
+
88
+ summarizer.authority.scopes; // Set { "crm.read" }
89
+ summarizer.isNarrowerThan(orchestrator); // true, by construction
90
+
91
+ const decision = summarizer.check("crm.read", { context: { rows: 4200 } });
92
+ if (!decision.allowed) console.log(decision.explain());
93
+
94
+ summarizer.enforce("crm.export", { context: { egress: "any" } }); // throws AuthorityDenied
95
+ ```
96
+
97
+ `check` returns a `Decision` and never throws — a denial is an outcome to reason
98
+ about, not a bug. `enforce` throws `AuthorityDenied` where a denial should stop
99
+ the caller. `wouldAllow` runs the same evaluation and writes nothing, so a
100
+ planner can ask without leaving a record of an attempt.
101
+
102
+ Beyond scopes, a chain has ceilings no single-agent permission model expresses:
103
+ row and spend caps, an ordered egress rank, per-scope call limits that meter
104
+ themselves, membership and prefix bounds, a TTL, and depth and fanout limits on
105
+ the tree. `revoke` cascades to a whole subtree; `revokeAgent` bans a principal
106
+ chain-wide, so a framework that hands off to it again cannot mint it fresh
107
+ permissions. A constraint type this build does not recognise denies rather than
108
+ going unenforced.
109
+
110
+ Export the evidence when the run is done:
111
+
112
+ ```ts
113
+ import { exportBundle, Ed25519Signer } from "attenu-guard";
114
+
115
+ const signer = Ed25519Signer.generate();
116
+ const bundle = exportBundle(orchestrator.auditLog(), signer);
117
+ // Publish the bundle; distribute signer.publicBytesRaw() out of band.
118
+ ```
119
+
120
+ ## LangGraph.js
121
+
122
+ ```ts
123
+ import { ToolNode } from "@langchain/langgraph/prebuilt";
124
+ import { delegateTo, guardTools, toolArgs } from "attenu-guard/adapters/langgraph";
125
+
126
+ const researcher = delegateTo(supervisor, {
127
+ agentId: "researcher",
128
+ request: new Authority({ scopes: ["crm.read"], ceilings: [new RowLimit(500)], ttl: 900 }),
129
+ task: "summarise the pipeline",
130
+ });
131
+
132
+ const node = new ToolNode(guardTools(researcher, [crmQuery, wireMoney], {
133
+ scopes: { crm_query: "crm.read", wire_money: "payments.send" },
134
+ contexts: { crm_query: (input) => ({ rows: toolArgs(input).limit }) },
135
+ onDenied: (decision) => `Denied by attenu-guard: ${decision.explain()}`,
136
+ }));
137
+ ```
138
+
139
+ `wire_money` is refused: the researcher does not hold `payments.send`, and the
140
+ supervisor could not have granted it either. The tool body never runs. With
141
+ `onDenied` the model sees the refusal and can re-plan; leave it out and the call
142
+ throws `AuthorityDenied` instead.
143
+
144
+ **Which hook this uses.** LangGraph.js exposes no public before-tool-call hook —
145
+ `ToolNode` resolves the tool itself and calls `tool.invoke(toolCall, runtime)`
146
+ inside a `protected runTool`. So the adapter takes the tool-wrapping form:
147
+ `guardTool` returns a stand-in whose `invoke` authorizes first and forwards
148
+ everything else — `name`, `description`, `schema` — to the original, which is
149
+ all `ToolNode` matches on. `guardNode` wraps any node callable the same way, and
150
+ `addGuardedNode` registers one on a graph. LangGraph is never imported by the
151
+ adapter; it is a devDependency here, used to test against the real `ToolNode`.
152
+
153
+ ## Cross-language interoperability
154
+
155
+ Same bundle format as the Python library, verified by cross-language fixtures.
156
+ `tools/gen_fixtures.py` generates `test/fixtures/` from the Python package —
157
+ canonical-form vectors, entry-hash vectors, `meet` and subsumption cases, a raw
158
+ ledger, and one bundle per tamper mode with the report Python produces for each.
159
+ The suite reproduces all of it, and a separate test writes a ledger and bundle
160
+ here and has the Python CLI verify them, so both directions are covered.
161
+
162
+ Getting this right needs more than `JSON.stringify`: Python sorts keys, escapes
163
+ every non-ASCII code point, and distinguishes `100` from `100.0`. A document
164
+ that will be re-hashed is therefore read with a parser that keeps each number's
165
+ original literal. See `src/canonical.ts`.
166
+
167
+ ## What it does not do
168
+
169
+ - It does not decide what permissions a task needs. You state them; this library
170
+ enforces that a child never exceeds its parent and records what happened.
171
+ - It does not sandbox anything. A tool called around the guard is not guarded.
172
+ - The in-process integrity seal catches bugs and casual mutation, not code
173
+ running in the same process. Tamper-evidence comes from the signed bundle.
174
+ - `HS256TestSigner` is for tests and local development. It is symmetric, so
175
+ anyone who can verify can also forge. Use `Ed25519Signer` in production.
176
+
177
+ ## Development
178
+
179
+ ```
180
+ npm install
181
+ npm run build
182
+ npm test
183
+ ```
184
+
185
+ `npm test` builds first. The Python-CLI interoperability test skips unless a
186
+ Python `attenu-guard` is reachable:
187
+
188
+ ```
189
+ ATTENU_GUARD_PY=path/to/venv/bin/attenu-guard npm test
190
+ ```
191
+
192
+ Regenerate the fixtures with `npm run fixtures` (needs the Python package and
193
+ `cryptography` importable).
194
+
195
+ Publishing runs on a `v*` tag through npm trusted publishing, which needs a
196
+ one-time trusted-publisher configuration on the npm side for this package.
197
+
198
+ ## Licence
199
+
200
+ Apache-2.0. Security reports: see [SECURITY.md](SECURITY.md).
package/SECURITY.md ADDED
@@ -0,0 +1,34 @@
1
+ # Security policy
2
+
3
+ ## Reporting a vulnerability
4
+
5
+ Report privately — please do not open a public issue.
6
+
7
+ - **Preferred:** a [private security advisory](https://github.com/attenu-io/attenu-guard-ts/security/advisories/new)
8
+ on this repository.
9
+ - **Email:** security@attenu.io
10
+
11
+ Please include the version, a description of the issue, and the smallest
12
+ reproduction you can manage. We aim to acknowledge within three working days
13
+ and to keep you informed while a fix is prepared. We will credit you in the
14
+ advisory unless you would rather we did not.
15
+
16
+ ## Supported versions
17
+
18
+ The most recent minor release receives security fixes.
19
+
20
+ ## Scope
21
+
22
+ In scope: anything that lets a delegated agent hold permissions its parent did
23
+ not hold, lets a denied action through, or lets a tampered ledger or evidence
24
+ bundle verify as intact.
25
+
26
+ Out of scope, because they are documented properties rather than defects:
27
+
28
+ - `HS256TestSigner` is symmetric and is not for production. Anyone able to
29
+ verify with it can also forge. Use `Ed25519Signer`.
30
+ - The in-process integrity seal catches bugs and casual mutation. Code running
31
+ in the same process can read the chain secret; tamper-evidence for a third
32
+ party comes from the signed evidence bundle.
33
+ - A tool invoked around the guard rather than through it is not guarded. This
34
+ library authorizes calls; it does not sandbox them.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ const { main } = require("../dist/cjs/cli.js");
4
+ process.exitCode = main(process.argv.slice(2));
@@ -0,0 +1,174 @@
1
+ /**
2
+ * adapters/langgraph.ts — a thin LangGraph.js integration.
3
+ *
4
+ * The adapter is built in two layers, the same shape as the Python one:
5
+ *
6
+ * 1. The AUTHORIZATION-WRAPPING LOGIC is plain TypeScript. It wraps an
7
+ * arbitrary callable so that every call is authorized through a `Guard`
8
+ * first, and it does not import, require or reference `@langchain/langgraph`
9
+ * at all. That is deliberate: a LangGraph node is, by LangGraph's own
10
+ * convention, just a callable — `(state) => partialState` — so wrapping
11
+ * "any callable" is enough to wrap a node or a plain tool function alike,
12
+ * and it keeps the logic testable with no framework installed.
13
+ *
14
+ * 2. The one spot that touches the package itself (`isLangGraphAvailable`)
15
+ * imports it lazily. `addGuardedNode` takes an already-constructed graph
16
+ * and only calls `.addNode(name, callable)` on it, so it needs no import
17
+ * either.
18
+ *
19
+ * ## Which hook this uses, and why
20
+ *
21
+ * The Python adapter wraps node callables. LangGraph.js has no public
22
+ * before-tool-call hook: `ToolNode` resolves the tool itself and invokes it at
23
+ * `dist/prebuilt/tool_node.js:207` (`const output = await tool.invoke(toolCall,
24
+ * runtime)`), inside a `protected runTool`. So this adapter takes the
25
+ * TOOL-WRAPPING form: `guardTool` returns a stand-in for a tool object whose
26
+ * `invoke` authorizes first and calls through only on an allow. The stand-in
27
+ * keeps the original `name`, `description` and `schema`, which is all `ToolNode`
28
+ * looks at when it matches a tool call to a tool, so a wrapped tool drops
29
+ * straight into `new ToolNode([...])` or `createReactAgent({tools})`.
30
+ *
31
+ * A denial throws `AuthorityDenied` — the same error `Guard.enforce` throws —
32
+ * BEFORE the tool body runs, so a poisoned instruction or a runaway plan never
33
+ * reaches the call it is not authorized to make. A graph can catch it around
34
+ * `graph.invoke(...)`, or route around it with LangGraph's own error handling;
35
+ * this adapter does not prescribe which. It guarantees only that the tool body
36
+ * never executes on a denial.
37
+ *
38
+ * ## Delegation
39
+ *
40
+ * Handing work to a sub-agent is the delegation moment. `delegateTo` mints the
41
+ * child `Guard` at the handoff — before the sub-agent's graph is invoked — so
42
+ * the sub-agent's tools are guarded by an authority that is provably a subset of
43
+ * the parent's.
44
+ *
45
+ * const researcher = delegateTo(supervisor, {
46
+ * agentId: "researcher",
47
+ * request: new Authority({ scopes: ["crm.read"], ceilings: [new RowLimit(500)], ttl: 900 }),
48
+ * task: "summarise the Q3 pipeline",
49
+ * });
50
+ * const tools = guardTools(researcher, [crmQuery], { scopes: { crm_query: "crm.read" } });
51
+ */
52
+ import { type Guard } from "../guard.js";
53
+ import type { Authority } from "../authority.js";
54
+ import type { Context } from "../ceilings.js";
55
+ import type { Decision } from "../reasons.js";
56
+ /** Options shared by every guarded wrapper. */
57
+ export interface GuardOptions {
58
+ /**
59
+ * Called with the exact arguments the wrapped callable received; returns the
60
+ * context for `guard.check`, e.g. `{rows: 5000, egress: "none"}`. Omitted
61
+ * means an empty context — fine for a scope-only check.
62
+ */
63
+ contextFn?: (...args: any[]) => Context;
64
+ /** The `tool` label recorded on the ledger. Defaults to the callable's name. */
65
+ tool?: string | null;
66
+ /** What the caller knows about why this scope would be absent. */
67
+ disposition?: string | null;
68
+ /** Marks the call as consuming a metered resource. */
69
+ metered?: boolean;
70
+ }
71
+ /** A callable node, with the guard and scope it was wrapped with attached. */
72
+ export type GuardedNode<F extends (...args: any[]) => any> = F & {
73
+ readonly guard: Guard;
74
+ readonly toolScope: string;
75
+ readonly unwrapped: F;
76
+ };
77
+ /**
78
+ * Wrap a callable so every call is authorized through `guard` first.
79
+ *
80
+ * On call: the context is `contextFn(...args)` if given, else `{}`; then
81
+ * `guard.check(toolScope, {context, tool})`. On a denial this throws
82
+ * `AuthorityDenied` and the wrapped callable is NEVER invoked. Otherwise it
83
+ * calls through with the original arguments and returns the result unchanged —
84
+ * including a promise, which is passed along untouched.
85
+ *
86
+ * Use the Guard for the SPECIFIC agent this callable belongs to, not the
87
+ * orchestrator's broader one, so a denial reflects that node's real, narrowed
88
+ * authority.
89
+ */
90
+ export declare function guardNode<F extends (...args: any[]) => any>(guard: Guard, toolScope: string, fn: F, options?: GuardOptions): GuardedNode<F>;
91
+ /** The minimum a LangGraph.js graph needs to expose for `addGuardedNode`. */
92
+ export interface GraphLike {
93
+ addNode(name: string, action: (...args: any[]) => any, ...rest: any[]): unknown;
94
+ }
95
+ /**
96
+ * Build the guarded wrapper and register it on a graph in one call.
97
+ *
98
+ * `graph` is structurally typed: it only needs `.addNode(name, callable)`,
99
+ * which is `StateGraph`'s real signature — so this helper needs no import of
100
+ * its own and stays testable against a plain stand-in graph.
101
+ */
102
+ export declare function addGuardedNode<F extends (...args: any[]) => any>(graph: GraphLike, name: string, guard: Guard, toolScope: string, fn: F, options?: GuardOptions): GuardedNode<F>;
103
+ /** The shape `ToolNode` requires of a tool: a name and an `invoke`. */
104
+ export interface ToolLike {
105
+ name: string;
106
+ invoke(input: any, config?: any): any;
107
+ [key: string]: any;
108
+ }
109
+ export interface GuardToolOptions extends GuardOptions {
110
+ /** The scope this tool needs. Defaults to the tool's own name. */
111
+ scope?: string;
112
+ /**
113
+ * Called when the guard denies the call, instead of throwing. Return the
114
+ * value the tool should yield — e.g. a `ToolMessage` explaining the refusal,
115
+ * so the model can see the denial and re-plan rather than the run aborting.
116
+ */
117
+ onDenied?: (decision: Decision, input: any) => any;
118
+ }
119
+ /**
120
+ * Return a stand-in for `tool` whose `invoke` authorizes through `guard` before
121
+ * the tool body runs. Everything else — `name`, `description`, `schema`, any
122
+ * LangChain internals — is forwarded to the original, so `ToolNode` matches and
123
+ * runs it exactly as it would the unwrapped tool.
124
+ *
125
+ * The context function receives the raw invoke arguments. `ToolNode` passes a
126
+ * tool call object, so the arguments the model proposed are at `input.args`;
127
+ * a direct `tool.invoke({...})` passes them at the top level. `toolArgs` below
128
+ * reads either shape.
129
+ */
130
+ export declare function guardTool<T extends ToolLike>(guard: Guard, tool: T, options?: GuardToolOptions): T;
131
+ /** Marker properties, so a caller can tell a guarded tool from a bare one. */
132
+ export declare const ATTENU_GUARD = "__attenuGuard";
133
+ export declare const ATTENU_SCOPE = "__attenuScope";
134
+ export interface GuardToolsOptions extends GuardOptions {
135
+ /** Tool name -> scope. A tool absent from the map uses its own name. */
136
+ scopes?: Record<string, string>;
137
+ /** Tool name -> context function, overriding the shared `contextFn`. */
138
+ contexts?: Record<string, (input: any, config?: any) => Context>;
139
+ onDenied?: (decision: Decision, input: any) => any;
140
+ }
141
+ /**
142
+ * Guard a whole tool list in one call — what you hand to `new ToolNode([...])`
143
+ * or `createReactAgent({tools})`.
144
+ */
145
+ export declare function guardTools<T extends ToolLike>(guard: Guard, tools: readonly T[], options?: GuardToolsOptions): T[];
146
+ /**
147
+ * The arguments a tool call carries, whichever shape `invoke` was handed.
148
+ * `ToolNode` passes `{name, args, id, type}`; a direct call passes the args
149
+ * themselves. Useful inside a `contextFn`.
150
+ */
151
+ export declare function toolArgs(input: any): Record<string, any>;
152
+ export interface DelegateOptions {
153
+ agentId: string;
154
+ request: Authority;
155
+ task: string;
156
+ }
157
+ /**
158
+ * Mint the child Guard for a sub-agent handoff. Handing work to a sub-agent IS
159
+ * the delegation moment: call this before the sub-agent's graph is invoked, so
160
+ * its tools are guarded by an authority that is provably a subset of the
161
+ * parent's. A request wider than the parent holds comes back narrowed.
162
+ *
163
+ * Throws `AuthorityError` if the handoff is structurally refused — the parent
164
+ * is revoked or expired, the agent is banned, or the chain's depth or fanout
165
+ * ceiling would be exceeded.
166
+ */
167
+ export declare function delegateTo(parent: Guard, options: DelegateOptions): Guard;
168
+ /**
169
+ * True iff `@langchain/langgraph` is importable here. The import is lazy and
170
+ * happens only when this is called, so merely importing this adapter — or using
171
+ * `guardNode` / `guardTool` — never requires LangGraph to be installed.
172
+ */
173
+ export declare function isLangGraphAvailable(): Promise<boolean>;
174
+ //# sourceMappingURL=langgraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"langgraph.d.ts","sourceRoot":"","sources":["../../../src/adapters/langgraph.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,OAAO,EAAmB,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,+CAA+C;AAC/C,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,SAAS,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC;IACxC,gFAAgF;IAChF,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,kEAAkE;IAClE,WAAW,CAAC,EAAE,MAAM,GAAG,IAA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