@actahq/agent 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +125 -2
- package/package.json +40 -4
- package/src/agent.js +104 -0
- package/src/index.d.ts +79 -0
- package/src/index.js +22 -0
- package/src/ledger.js +75 -0
- package/src/passport.js +51 -0
- package/src/policy.js +59 -0
- package/src/receipt.js +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Acta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Acta
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Give every AI agent a verifiable passport and a tamper-evident, public action log.**
|
|
4
|
+
|
|
5
|
+
AI agents are already moving money, sending email, and shipping code on their own. Acta is the guardrail layer that sits at the chokepoint every agent action passes through: it checks the action against what the agent is *allowed* to do, blocks what's out of scope, and writes a signed, hash-chained receipt that anyone can audit.
|
|
6
|
+
|
|
7
|
+
> If an agent won't show its receipts, don't trust it.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @actahq/agent
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { Acta } from "@actahq/agent";
|
|
21
|
+
|
|
22
|
+
// 1. Register the agent — mints its public passport.
|
|
23
|
+
const agent = Acta.register({
|
|
24
|
+
name: "ShopPilot",
|
|
25
|
+
owner: "Northwind Commerce",
|
|
26
|
+
scopes: ["read:catalog", "write:cart", "charge:payments <= $500"],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 2. Every action is scope-checked and logged.
|
|
30
|
+
agent.act("charge:payments", { amount: 420 }); // -> allowed
|
|
31
|
+
agent.act("charge:payments", { amount: 900 }); // -> blocked (over limit)
|
|
32
|
+
agent.act("export:customers", {}); // -> blocked (not in scope)
|
|
33
|
+
|
|
34
|
+
// 3. Verify nobody edited the log.
|
|
35
|
+
agent.verify(); // -> { ok: true, reason: "intact" }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Run the full example: `npm run example`. Run the tests: `npm test`.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## How it works
|
|
43
|
+
|
|
44
|
+
Three moving parts:
|
|
45
|
+
|
|
46
|
+
1. **Passport** — a signed identity for the agent: who owns it and exactly what it may do. Each passport carries an ed25519 keypair; the public key is shared openly so the log can be verified by anyone.
|
|
47
|
+
2. **Policy** — deny-by-default scope enforcement. Actions outside the declared scopes are blocked at the gate, in real time, not explained away later.
|
|
48
|
+
3. **Receipts** — every action (allowed *or* blocked) becomes a receipt that embeds the hash of the previous one. Edit or remove any past receipt and every hash after it stops matching, so tampering is detectable.
|
|
49
|
+
|
|
50
|
+
### Scope grammar
|
|
51
|
+
|
|
52
|
+
| Scope | Grants |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `read:catalog` | exactly that action |
|
|
55
|
+
| `read:*` | any action beginning `read:` |
|
|
56
|
+
| `*` | any action |
|
|
57
|
+
| `charge:payments <= $500` | the action, with an upper limit on `params.amount` |
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Three ways to integrate
|
|
62
|
+
|
|
63
|
+
Acta meets your agent wherever it lives:
|
|
64
|
+
|
|
65
|
+
- **SDK** — `npm install @actahq/agent`, wrap your tool calls (this repo, `src/`).
|
|
66
|
+
- **MCP server** — drop it into any MCP host so the agent records actions through Acta (`mcp/`). See [`mcp/README.md`](./mcp/README.md).
|
|
67
|
+
- **Git** — clone and self-host the whole layer.
|
|
68
|
+
|
|
69
|
+
### Wrapping a tool
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
const charge = agent.wrap("charge:payments", async ({ amount, cart }) => {
|
|
73
|
+
return paymentProvider.charge(amount, cart); // only runs if in scope
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
await charge({ amount: 420, cart: "c_8831" }); // logged + allowed
|
|
77
|
+
await charge({ amount: 900 }); // throws ScopeError, logged as blocked
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## The public ledger
|
|
83
|
+
|
|
84
|
+
Point the ledger at your Acta service and every receipt is mirrored to the open ledger, where anyone can look up a passport and see the agent's full history:
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { Acta, LocalLedger } from "@actahq/agent";
|
|
88
|
+
|
|
89
|
+
const ledger = new LocalLedger({
|
|
90
|
+
file: "./ledger.jsonl", // local, append-only
|
|
91
|
+
endpoint: "https://acta.dev", // public ledger (mirror)
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const agent = Acta.register({ name, owner, scopes, ledger });
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The agent never blocks on the ledger — mirroring is fire-and-forget and offline-first.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Repo layout
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
src/ core SDK (@actahq/agent) — passport, policy, receipts, ledger
|
|
105
|
+
mcp/ MCP server (@actahq/mcp) — plug into any agent host
|
|
106
|
+
backend/ ledger API (@actahq/ledger) — ingest + public lookup + verify
|
|
107
|
+
examples/ runnable basic example
|
|
108
|
+
test/ SDK smoke test
|
|
109
|
+
docker-compose.yml Postgres + API, one command
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## The backend
|
|
113
|
+
|
|
114
|
+
The hosted ledger lives in [`backend/`](./backend). It runs with **no database** by default (in-memory) so you can `npm start` and see it immediately, and switches to **Postgres** via `docker compose up`. It verifies every receipt's hash, signature, and chain position before storing — forged, replayed, or out-of-order receipts are rejected. See [`backend/README.md`](./backend/README.md).
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
cd backend && npm install && npm start # http://localhost:4000
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Status
|
|
121
|
+
|
|
122
|
+
Early and open. The SDK, MCP server, and ledger backend all run today (the backend against in-memory or Postgres). Next up: per-account API keys, Stripe metering, Merkle checkpoints, and the hosted reputation feed. Contributions welcome.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT — see [LICENSE](./LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@actahq/agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Give every AI agent a verifiable passport and a tamper-evident, public action log. The guardrail layer for autonomous agents.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": ["src"],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node test/smoke.test.js",
|
|
14
|
+
"example": "node examples/basic.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"ai-agents",
|
|
18
|
+
"agent",
|
|
19
|
+
"audit-log",
|
|
20
|
+
"observability",
|
|
21
|
+
"guardrails",
|
|
22
|
+
"compliance",
|
|
23
|
+
"ai-safety",
|
|
24
|
+
"mcp"
|
|
25
|
+
],
|
|
26
|
+
"author": "Acta",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"homepage": "https://github.com/Actahq/acta#readme",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/Actahq/acta.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/Actahq/acta/issues"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/agent.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// agent.js — the object a developer actually uses.
|
|
2
|
+
//
|
|
3
|
+
// const agent = Acta.register({ name, owner, scopes });
|
|
4
|
+
// const { allowed, receipt } = await agent.act("charge:payments", { amount: 420 });
|
|
5
|
+
//
|
|
6
|
+
// Every call to act() is evaluated against the passport's scopes, recorded as a
|
|
7
|
+
// signed, hash-chained receipt (allowed OR blocked), and appended to the ledger.
|
|
8
|
+
|
|
9
|
+
import { mintPassport, publicKeyOf, publicView } from "./passport.js";
|
|
10
|
+
import { evaluate } from "./policy.js";
|
|
11
|
+
import { makeReceipt, verifyChain } from "./receipt.js";
|
|
12
|
+
import { LocalLedger } from "./ledger.js";
|
|
13
|
+
|
|
14
|
+
export class ScopeError extends Error {
|
|
15
|
+
constructor(reason, action) {
|
|
16
|
+
super(`Acta blocked "${action}": ${reason}`);
|
|
17
|
+
this.name = "ScopeError";
|
|
18
|
+
this.action = action;
|
|
19
|
+
this.reason = reason;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class Agent {
|
|
24
|
+
constructor({ passport, privateKey, ledger }) {
|
|
25
|
+
this.passport = passport;
|
|
26
|
+
this._privateKey = privateKey;
|
|
27
|
+
this.ledger = ledger || new LocalLedger();
|
|
28
|
+
this._seq = 0;
|
|
29
|
+
// If the ledger mirrors to a public endpoint, publish the passport first
|
|
30
|
+
// so receipts have somewhere to land.
|
|
31
|
+
if (this.ledger.endpoint && typeof this.ledger.publishPassport === "function") {
|
|
32
|
+
this.ledger.publishPassport(this.publicPassport);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Public, shareable identity (no private key). */
|
|
37
|
+
get publicPassport() {
|
|
38
|
+
return publicView(this.passport);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Record (and enforce) one action.
|
|
43
|
+
* @param {string} action e.g. "charge:payments"
|
|
44
|
+
* @param {object} [params] e.g. { amount: 420 }
|
|
45
|
+
* @returns {{allowed:boolean, reason:string, receipt:object}}
|
|
46
|
+
*/
|
|
47
|
+
act(action, params = {}) {
|
|
48
|
+
const decision = evaluate(this.passport.scopes, action, params);
|
|
49
|
+
const receipt = makeReceipt({
|
|
50
|
+
seq: this._seq++,
|
|
51
|
+
passportId: this.passport.id,
|
|
52
|
+
action,
|
|
53
|
+
params,
|
|
54
|
+
result: decision.allowed ? "allowed" : "blocked",
|
|
55
|
+
reason: decision.reason,
|
|
56
|
+
prevHash: this.ledger.lastHash,
|
|
57
|
+
privateKey: this._privateKey,
|
|
58
|
+
});
|
|
59
|
+
this.ledger.append(receipt);
|
|
60
|
+
return { allowed: decision.allowed, reason: decision.reason, receipt };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Run `fn` only if the action is in scope; otherwise throw ScopeError.
|
|
65
|
+
* The attempt is logged either way.
|
|
66
|
+
*/
|
|
67
|
+
async guard(action, params, fn) {
|
|
68
|
+
const { allowed, reason } = this.act(action, params);
|
|
69
|
+
if (!allowed) throw new ScopeError(reason, action);
|
|
70
|
+
return await fn();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Wrap a tool function so every call is logged and scope-checked.
|
|
75
|
+
* @returns {(params?:object)=>Promise<any>}
|
|
76
|
+
*/
|
|
77
|
+
wrap(action, fn) {
|
|
78
|
+
return (params = {}) => this.guard(action, params, () => fn(params));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Verify this agent's own chain is intact and signed by its passport. */
|
|
82
|
+
verify() {
|
|
83
|
+
return verifyChain(this.ledger.byPassport(this.passport.id), publicKeyOf(this.passport));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const Acta = {
|
|
88
|
+
/**
|
|
89
|
+
* Mint a passport and return a ready-to-use agent.
|
|
90
|
+
* @param {object} opts { name, owner, scopes, ledger? }
|
|
91
|
+
*/
|
|
92
|
+
register(opts) {
|
|
93
|
+
const { passport, privateKey } = mintPassport(opts);
|
|
94
|
+
return new Agent({ passport, privateKey, ledger: opts.ledger });
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
/** Verify any chain against a passport's public view. */
|
|
98
|
+
verifyChain(receipts, passport) {
|
|
99
|
+
return verifyChain(receipts, publicKeyOf(passport));
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
LocalLedger,
|
|
103
|
+
ScopeError,
|
|
104
|
+
};
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Type definitions for @actahq/agent
|
|
2
|
+
|
|
3
|
+
export interface Passport {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
owner: string;
|
|
7
|
+
scopes: string[];
|
|
8
|
+
publicKey: string;
|
|
9
|
+
createdAt: string;
|
|
10
|
+
version: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Receipt {
|
|
14
|
+
seq: number;
|
|
15
|
+
passportId: string;
|
|
16
|
+
action: string;
|
|
17
|
+
params: Record<string, unknown> | null;
|
|
18
|
+
result: "allowed" | "blocked";
|
|
19
|
+
reason: string;
|
|
20
|
+
prevHash: string;
|
|
21
|
+
ts: string;
|
|
22
|
+
hash: string;
|
|
23
|
+
sig: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ActResult {
|
|
27
|
+
allowed: boolean;
|
|
28
|
+
reason: string;
|
|
29
|
+
receipt: Receipt;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ChainResult {
|
|
33
|
+
ok: boolean;
|
|
34
|
+
brokenAt: number | null;
|
|
35
|
+
reason: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface LedgerOptions {
|
|
39
|
+
file?: string;
|
|
40
|
+
endpoint?: string;
|
|
41
|
+
onAppend?: (receipt: Receipt) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class LocalLedger {
|
|
45
|
+
constructor(opts?: LedgerOptions);
|
|
46
|
+
append(receipt: Receipt): Receipt;
|
|
47
|
+
all(): Receipt[];
|
|
48
|
+
byPassport(passportId: string): Receipt[];
|
|
49
|
+
readonly lastHash: string | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class ScopeError extends Error {
|
|
53
|
+
action: string;
|
|
54
|
+
reason: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class Agent {
|
|
58
|
+
passport: Passport;
|
|
59
|
+
ledger: LocalLedger;
|
|
60
|
+
readonly publicPassport: Omit<Passport, never>;
|
|
61
|
+
act(action: string, params?: Record<string, unknown>): ActResult;
|
|
62
|
+
guard<T>(action: string, params: Record<string, unknown>, fn: () => Promise<T> | T): Promise<T>;
|
|
63
|
+
wrap<T>(action: string, fn: (params?: Record<string, unknown>) => Promise<T> | T): (params?: Record<string, unknown>) => Promise<T>;
|
|
64
|
+
verify(): ChainResult;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface RegisterOptions {
|
|
68
|
+
name: string;
|
|
69
|
+
owner: string;
|
|
70
|
+
scopes: string[];
|
|
71
|
+
ledger?: LocalLedger;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const Acta: {
|
|
75
|
+
register(opts: RegisterOptions): Agent;
|
|
76
|
+
verifyChain(receipts: Receipt[], passport: Passport): ChainResult;
|
|
77
|
+
LocalLedger: typeof LocalLedger;
|
|
78
|
+
ScopeError: typeof ScopeError;
|
|
79
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// @actahq/agent — the guardrail layer for AI agents.
|
|
2
|
+
//
|
|
3
|
+
// Quickstart:
|
|
4
|
+
//
|
|
5
|
+
// import { Acta } from "@actahq/agent";
|
|
6
|
+
//
|
|
7
|
+
// const agent = Acta.register({
|
|
8
|
+
// name: "ShopPilot",
|
|
9
|
+
// owner: "Northwind Commerce",
|
|
10
|
+
// scopes: ["read:catalog", "write:cart", "charge:payments <= $500"],
|
|
11
|
+
// });
|
|
12
|
+
//
|
|
13
|
+
// await agent.act("charge:payments", { amount: 420 }); // -> allowed, receipt on the ledger
|
|
14
|
+
// await agent.act("charge:payments", { amount: 900 }); // -> blocked (exceeds limit)
|
|
15
|
+
//
|
|
16
|
+
// agent.verify(); // -> { ok: true } : the chain is intact and signed
|
|
17
|
+
|
|
18
|
+
export { Acta, Agent, ScopeError } from "./agent.js";
|
|
19
|
+
export { LocalLedger } from "./ledger.js";
|
|
20
|
+
export { verifyChain, hashReceipt } from "./receipt.js";
|
|
21
|
+
export { evaluate, parseScope } from "./policy.js";
|
|
22
|
+
export { mintPassport, publicKeyOf, publicView } from "./passport.js";
|
package/src/ledger.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// ledger.js — where receipts are stored.
|
|
2
|
+
//
|
|
3
|
+
// LocalLedger keeps the chain in memory and (optionally) mirrors every receipt
|
|
4
|
+
// to a sink: a file on disk, and/or a remote "public ledger" endpoint. The remote
|
|
5
|
+
// transport is a thin stub here — point it at your Acta service when it exists.
|
|
6
|
+
|
|
7
|
+
import { appendFileSync } from "node:fs";
|
|
8
|
+
|
|
9
|
+
export class LocalLedger {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} [opts]
|
|
12
|
+
* @param {string} [opts.file] Append each receipt as a JSON line to this path.
|
|
13
|
+
* @param {string} [opts.endpoint] POST each receipt here (the public ledger).
|
|
14
|
+
* @param {(receipt:object)=>void} [opts.onAppend] Custom sink.
|
|
15
|
+
*/
|
|
16
|
+
constructor(opts = {}) {
|
|
17
|
+
this.receipts = [];
|
|
18
|
+
this.file = opts.file || null;
|
|
19
|
+
this.endpoint = opts.endpoint || null;
|
|
20
|
+
this.apiKey = opts.apiKey || null;
|
|
21
|
+
this.onAppend = opts.onAppend || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#headers() {
|
|
25
|
+
const h = { "content-type": "application/json" };
|
|
26
|
+
if (this.apiKey) h["authorization"] = "Bearer " + this.apiKey;
|
|
27
|
+
return h;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Publish an agent's public passport to the ledger (needed before receipts). */
|
|
31
|
+
async publishPassport(passport) {
|
|
32
|
+
if (!this.endpoint || typeof fetch !== "function") return;
|
|
33
|
+
try {
|
|
34
|
+
await fetch(this.endpoint + "/v1/passports", {
|
|
35
|
+
method: "POST", headers: this.#headers(), body: JSON.stringify(passport),
|
|
36
|
+
});
|
|
37
|
+
} catch { /* offline-first */ }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
append(receipt) {
|
|
41
|
+
this.receipts.push(receipt);
|
|
42
|
+
if (this.file) {
|
|
43
|
+
try { appendFileSync(this.file, JSON.stringify(receipt) + "\n"); } catch { /* best effort */ }
|
|
44
|
+
}
|
|
45
|
+
if (this.onAppend) {
|
|
46
|
+
try { this.onAppend(receipt); } catch { /* best effort */ }
|
|
47
|
+
}
|
|
48
|
+
if (this.endpoint) this.#mirror(receipt);
|
|
49
|
+
return receipt;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async #mirror(receipt) {
|
|
53
|
+
// Fire-and-forget to the public ledger. No-op if fetch/endpoint unavailable.
|
|
54
|
+
try {
|
|
55
|
+
if (typeof fetch !== "function") return;
|
|
56
|
+
await fetch(this.endpoint + "/v1/receipts", {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: this.#headers(),
|
|
59
|
+
body: JSON.stringify(receipt),
|
|
60
|
+
});
|
|
61
|
+
} catch { /* offline-first: never block the agent on the ledger */ }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
all() {
|
|
65
|
+
return [...this.receipts];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
byPassport(passportId) {
|
|
69
|
+
return this.receipts.filter((r) => r.passportId === passportId);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get lastHash() {
|
|
73
|
+
return this.receipts.length ? this.receipts[this.receipts.length - 1].hash : null;
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/passport.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// passport.js — an agent's verifiable identity.
|
|
2
|
+
// A passport is a signed record of who owns an agent and what it is allowed to do.
|
|
3
|
+
// Each passport carries an ed25519 keypair; every receipt the agent writes is
|
|
4
|
+
// signed with the private key, so anyone with the public key can verify the log.
|
|
5
|
+
|
|
6
|
+
import { generateKeyPairSync, createPublicKey } from "node:crypto";
|
|
7
|
+
import { randomBytes } from "node:crypto";
|
|
8
|
+
|
|
9
|
+
/** Generate a short, human-readable passport id like "AC-7F3A9C". */
|
|
10
|
+
function mintId() {
|
|
11
|
+
return "AC-" + randomBytes(3).toString("hex").toUpperCase();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Mint a new passport (identity + keypair) for an agent.
|
|
16
|
+
* @param {object} opts
|
|
17
|
+
* @param {string} opts.name Human name of the agent, e.g. "ShopPilot".
|
|
18
|
+
* @param {string} opts.owner Who is accountable for it, e.g. "Northwind Commerce".
|
|
19
|
+
* @param {string[]} opts.scopes What it is allowed to do (see policy.js grammar).
|
|
20
|
+
* @returns {{passport: object, privateKey: import('node:crypto').KeyObject}}
|
|
21
|
+
*/
|
|
22
|
+
export function mintPassport({ name, owner, scopes = [] }) {
|
|
23
|
+
if (!name) throw new Error("passport requires a name");
|
|
24
|
+
if (!owner) throw new Error("passport requires an owner (who is accountable)");
|
|
25
|
+
|
|
26
|
+
const { publicKey, privateKey } = generateKeyPairSync("ed25519");
|
|
27
|
+
|
|
28
|
+
const passport = {
|
|
29
|
+
id: mintId(),
|
|
30
|
+
name,
|
|
31
|
+
owner,
|
|
32
|
+
scopes,
|
|
33
|
+
// public key is shared openly so the ledger can be verified by anyone
|
|
34
|
+
publicKey: publicKey.export({ type: "spki", format: "pem" }),
|
|
35
|
+
createdAt: new Date().toISOString(),
|
|
36
|
+
version: 1,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return { passport, privateKey };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Rebuild a KeyObject from a passport's PEM public key, for verification. */
|
|
43
|
+
export function publicKeyOf(passport) {
|
|
44
|
+
return createPublicKey(passport.publicKey);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The subset of a passport that is safe to publish on the open ledger. */
|
|
48
|
+
export function publicView(passport) {
|
|
49
|
+
const { id, name, owner, scopes, publicKey, createdAt, version } = passport;
|
|
50
|
+
return { id, name, owner, scopes, publicKey, createdAt, version };
|
|
51
|
+
}
|
package/src/policy.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// policy.js — scope grammar + enforcement.
|
|
2
|
+
//
|
|
3
|
+
// A scope is a string that grants an agent permission to take an action,
|
|
4
|
+
// optionally with a numeric limit. Supported forms:
|
|
5
|
+
//
|
|
6
|
+
// "read:catalog" exact action
|
|
7
|
+
// "read:*" any action beginning "read:"
|
|
8
|
+
// "*" any action at all
|
|
9
|
+
// "charge:payments <= $500" action with an upper limit on params.amount
|
|
10
|
+
// "charge:payments <= 500" same, currency sign optional
|
|
11
|
+
//
|
|
12
|
+
// Enforcement is deny-by-default: if no scope matches, the action is blocked.
|
|
13
|
+
|
|
14
|
+
/** Parse one scope string into a structured rule. */
|
|
15
|
+
export function parseScope(scope) {
|
|
16
|
+
const m = String(scope).match(/^\s*([^\s<]+)\s*(?:<=\s*\$?\s*([0-9]+(?:\.[0-9]+)?))?\s*$/);
|
|
17
|
+
if (!m) return { action: scope, max: null, raw: scope };
|
|
18
|
+
return { action: m[1], max: m[2] != null ? Number(m[2]) : null, raw: scope };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Does a rule's action pattern cover the requested action name? */
|
|
22
|
+
function actionMatches(pattern, action) {
|
|
23
|
+
if (pattern === "*") return true;
|
|
24
|
+
if (pattern.endsWith(":*")) return action.startsWith(pattern.slice(0, -1)); // "read:*" -> "read:"
|
|
25
|
+
return pattern === action;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Decide whether an action is permitted by a set of scopes.
|
|
30
|
+
* @param {string[]} scopes
|
|
31
|
+
* @param {string} action e.g. "charge:payments"
|
|
32
|
+
* @param {object} params e.g. { amount: 420 }
|
|
33
|
+
* @returns {{allowed: boolean, reason: string, scope: string|null}}
|
|
34
|
+
*/
|
|
35
|
+
export function evaluate(scopes, action, params = {}) {
|
|
36
|
+
const rules = scopes.map(parseScope);
|
|
37
|
+
const matching = rules.filter((r) => actionMatches(r.action, action));
|
|
38
|
+
|
|
39
|
+
if (matching.length === 0) {
|
|
40
|
+
return { allowed: false, reason: `no scope grants "${action}"`, scope: null };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// If any matching rule carries a limit, the amount must satisfy at least one.
|
|
44
|
+
const limited = matching.filter((r) => r.max != null);
|
|
45
|
+
if (limited.length > 0 && params.amount != null) {
|
|
46
|
+
const ok = limited.some((r) => Number(params.amount) <= r.max);
|
|
47
|
+
const unlimited = matching.some((r) => r.max == null);
|
|
48
|
+
if (!ok && !unlimited) {
|
|
49
|
+
const lowest = Math.min(...limited.map((r) => r.max));
|
|
50
|
+
return {
|
|
51
|
+
allowed: false,
|
|
52
|
+
reason: `amount ${params.amount} exceeds limit of ${lowest} for "${action}"`,
|
|
53
|
+
scope: limited[0].raw,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { allowed: true, reason: "within scope", scope: matching[0].raw };
|
|
59
|
+
}
|
package/src/receipt.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// receipt.js — tamper-evident receipts, hash-chained per agent.
|
|
2
|
+
//
|
|
3
|
+
// Every action an agent takes produces a receipt. Each receipt embeds the hash
|
|
4
|
+
// of the previous one (prevHash), forming a chain: if any past receipt is edited
|
|
5
|
+
// or removed, every hash after it stops matching, so tampering is detectable.
|
|
6
|
+
// Each receipt is also signed with the agent's passport key.
|
|
7
|
+
|
|
8
|
+
import { createHash, sign as edSign, verify as edVerify } from "node:crypto";
|
|
9
|
+
|
|
10
|
+
const GENESIS = "0".repeat(64);
|
|
11
|
+
|
|
12
|
+
/** Deterministic hash of a receipt's contents (everything except hash + sig). */
|
|
13
|
+
export function hashReceipt(core) {
|
|
14
|
+
const canonical = JSON.stringify([
|
|
15
|
+
core.seq,
|
|
16
|
+
core.passportId,
|
|
17
|
+
core.action,
|
|
18
|
+
core.params ?? null,
|
|
19
|
+
core.result,
|
|
20
|
+
core.reason ?? "",
|
|
21
|
+
core.prevHash,
|
|
22
|
+
core.ts,
|
|
23
|
+
]);
|
|
24
|
+
return createHash("sha256").update(canonical).digest("hex");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Build and sign a receipt.
|
|
29
|
+
* @param {object} args
|
|
30
|
+
* @param {number} args.seq
|
|
31
|
+
* @param {string} args.passportId
|
|
32
|
+
* @param {string} args.action
|
|
33
|
+
* @param {object} args.params
|
|
34
|
+
* @param {"allowed"|"blocked"} args.result
|
|
35
|
+
* @param {string} args.reason
|
|
36
|
+
* @param {string} args.prevHash
|
|
37
|
+
* @param {import('node:crypto').KeyObject} args.privateKey
|
|
38
|
+
*/
|
|
39
|
+
export function makeReceipt({ seq, passportId, action, params, result, reason, prevHash, privateKey }) {
|
|
40
|
+
const core = {
|
|
41
|
+
seq,
|
|
42
|
+
passportId,
|
|
43
|
+
action,
|
|
44
|
+
params: params ?? null,
|
|
45
|
+
result,
|
|
46
|
+
reason: reason ?? "",
|
|
47
|
+
prevHash: prevHash ?? GENESIS,
|
|
48
|
+
ts: new Date().toISOString(),
|
|
49
|
+
};
|
|
50
|
+
const hash = hashReceipt(core);
|
|
51
|
+
const sig = edSign(null, Buffer.from(hash, "hex"), privateKey).toString("base64");
|
|
52
|
+
return { ...core, hash, sig };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Verify a full chain of receipts against a passport's public key.
|
|
57
|
+
* Checks: hash integrity, chain linkage, and signatures.
|
|
58
|
+
* @returns {{ok: boolean, brokenAt: number|null, reason: string}}
|
|
59
|
+
*/
|
|
60
|
+
export function verifyChain(receipts, publicKey) {
|
|
61
|
+
let prev = GENESIS;
|
|
62
|
+
for (const r of receipts) {
|
|
63
|
+
const recomputed = hashReceipt(r);
|
|
64
|
+
if (recomputed !== r.hash) {
|
|
65
|
+
return { ok: false, brokenAt: r.seq, reason: "contents were altered (hash mismatch)" };
|
|
66
|
+
}
|
|
67
|
+
if (r.prevHash !== prev) {
|
|
68
|
+
return { ok: false, brokenAt: r.seq, reason: "chain broken (a receipt was removed or reordered)" };
|
|
69
|
+
}
|
|
70
|
+
const sigOk = edVerify(null, Buffer.from(r.hash, "hex"), publicKey, Buffer.from(r.sig, "base64"));
|
|
71
|
+
if (!sigOk) {
|
|
72
|
+
return { ok: false, brokenAt: r.seq, reason: "signature invalid (not signed by this passport)" };
|
|
73
|
+
}
|
|
74
|
+
prev = r.hash;
|
|
75
|
+
}
|
|
76
|
+
return { ok: true, brokenAt: null, reason: "intact" };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { GENESIS };
|