@agenticprimitives/chain-state 0.0.0-alpha.1
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 +53 -0
- package/dist/circuit-breaker.d.ts +14 -0
- package/dist/circuit-breaker.d.ts.map +1 -0
- package/dist/circuit-breaker.js +36 -0
- package/dist/circuit-breaker.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/reader.d.ts +3 -0
- package/dist/reader.d.ts.map +1 -0
- package/dist/reader.js +158 -0
- package/dist/reader.js.map +1 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/package.json +51 -0
- package/spec.md +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agentic Trust Labs
|
|
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
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @agenticprimitives/chain-state
|
|
2
|
+
|
|
3
|
+
The chain-read **authority port** — resilient, evidence-bearing, fail-closed.
|
|
4
|
+
|
|
5
|
+
An authorization decision rests on a handful of chain reads (is this delegation revoked? does this Smart
|
|
6
|
+
Agent's signature validate? is this session delegation accepted?). Those reads are part of the security
|
|
7
|
+
decision, so they cannot run on a single flaky RPC with no evidence and no freshness contract. This
|
|
8
|
+
package wraps multiple **injected** providers with per-provider circuit breakers, timeouts, divergence
|
|
9
|
+
detection, and a strict revocation-freshness rule — and every answer carries the block it was decided
|
|
10
|
+
against.
|
|
11
|
+
|
|
12
|
+
See [spec 289](../../specs/289-chain-state-authority-port.md) +
|
|
13
|
+
[ADR-0042](../../docs/architecture/decisions/0042-chain-state-is-the-chain-read-authority-port.md).
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createChainAuthorityReader, type ChainProvider } from '@agenticprimitives/chain-state';
|
|
19
|
+
|
|
20
|
+
// Providers are injected — an app wires a viem-backed adapter (readContract only, ADR-0012).
|
|
21
|
+
const providers: ChainProvider[] = [makeViemProvider('alchemy', rpcA), makeViemProvider('infura', rpcB)];
|
|
22
|
+
|
|
23
|
+
const reader = createChainAuthorityReader({
|
|
24
|
+
chainId: 8453,
|
|
25
|
+
providers,
|
|
26
|
+
revocationFreshnessMs: 12_000, // a cached "not revoked" is good for 12s, then re-read
|
|
27
|
+
onDivergence: (e) => metrics.increment('chain_state.divergence', { method: e.method }),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const { revoked, evidence } = await reader.isDelegationRevoked(delegationHash);
|
|
31
|
+
// evidence = { chainId, blockNumber, blockHash, observedAt, source } — log it with the allow/deny.
|
|
32
|
+
|
|
33
|
+
// Critical-risk reads require all providers to agree, else fail closed:
|
|
34
|
+
const sig = await reader.verifySmartAgentSignature({ signer, digest, signature }, 'critical');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## The freshness invariant (ADR-0013 applied to time)
|
|
38
|
+
|
|
39
|
+
- A **known-revoked** grant is cached forever (revocation is monotonic) — subsequent calls short-circuit.
|
|
40
|
+
- A cached **not revoked** answer is authoritative only within `revocationFreshnessMs`; past the window
|
|
41
|
+
it is re-read. A failed re-read fails **closed** (treated as revoked) — a stale cache is never the final
|
|
42
|
+
revocation authority.
|
|
43
|
+
|
|
44
|
+
## What it is not
|
|
45
|
+
|
|
46
|
+
- Not a chain library — `ChainProvider` is injected; the viem adapter lives in the consumer.
|
|
47
|
+
- Not the verify orchestration — `delegation`/`mcp-runtime` call this port; `chain-state` never imports
|
|
48
|
+
them (no back-edge).
|
|
49
|
+
- Not an indexer — this is the per-decision read port, not a CQRS read model.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class CircuitBreaker {
|
|
2
|
+
private readonly threshold;
|
|
3
|
+
private readonly cooldownMs;
|
|
4
|
+
private readonly now;
|
|
5
|
+
private failures;
|
|
6
|
+
private openedAt;
|
|
7
|
+
constructor(threshold: number, cooldownMs: number, now: () => number);
|
|
8
|
+
/** True if the breaker currently permits a call (closed or half-open). */
|
|
9
|
+
allow(): boolean;
|
|
10
|
+
recordSuccess(): void;
|
|
11
|
+
recordFailure(): void;
|
|
12
|
+
get isOpen(): boolean;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=circuit-breaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circuit-breaker.d.ts","sourceRoot":"","sources":["../src/circuit-breaker.ts"],"names":[],"mappings":"AAIA,qBAAa,cAAc;IAKvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,GAAG;IANtB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAAuB;gBAGpB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,GAAG,EAAE,MAAM,MAAM;IAGpC,0EAA0E;IAC1E,KAAK,IAAI,OAAO;IAMhB,aAAa,IAAI,IAAI;IAKrB,aAAa,IAAI,IAAI;IAKrB,IAAI,MAAM,IAAI,OAAO,CAEpB;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// A minimal per-provider circuit breaker: opens after N consecutive failures,
|
|
2
|
+
// half-opens after a cooldown, closes on the next success. Keeps a flaky provider
|
|
3
|
+
// from being retried on every authority read.
|
|
4
|
+
export class CircuitBreaker {
|
|
5
|
+
threshold;
|
|
6
|
+
cooldownMs;
|
|
7
|
+
now;
|
|
8
|
+
failures = 0;
|
|
9
|
+
openedAt = null;
|
|
10
|
+
constructor(threshold, cooldownMs, now) {
|
|
11
|
+
this.threshold = threshold;
|
|
12
|
+
this.cooldownMs = cooldownMs;
|
|
13
|
+
this.now = now;
|
|
14
|
+
}
|
|
15
|
+
/** True if the breaker currently permits a call (closed or half-open). */
|
|
16
|
+
allow() {
|
|
17
|
+
if (this.openedAt === null)
|
|
18
|
+
return true;
|
|
19
|
+
if (this.now() - this.openedAt >= this.cooldownMs)
|
|
20
|
+
return true; // half-open: allow one probe
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
recordSuccess() {
|
|
24
|
+
this.failures = 0;
|
|
25
|
+
this.openedAt = null;
|
|
26
|
+
}
|
|
27
|
+
recordFailure() {
|
|
28
|
+
this.failures += 1;
|
|
29
|
+
if (this.failures >= this.threshold)
|
|
30
|
+
this.openedAt = this.now();
|
|
31
|
+
}
|
|
32
|
+
get isOpen() {
|
|
33
|
+
return this.openedAt !== null && this.now() - this.openedAt < this.cooldownMs;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=circuit-breaker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circuit-breaker.js","sourceRoot":"","sources":["../src/circuit-breaker.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,kFAAkF;AAClF,8CAA8C;AAE9C,MAAM,OAAO,cAAc;IAKN;IACA;IACA;IANX,QAAQ,GAAG,CAAC,CAAC;IACb,QAAQ,GAAkB,IAAI,CAAC;IAEvC,YACmB,SAAiB,EACjB,UAAkB,EAClB,GAAiB;QAFjB,cAAS,GAAT,SAAS,CAAQ;QACjB,eAAU,GAAV,UAAU,CAAQ;QAClB,QAAG,GAAH,GAAG,CAAc;IACjC,CAAC;IAEJ,0EAA0E;IAC1E,KAAK;QACH,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;QACxC,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,UAAU;YAAE,OAAO,IAAI,CAAC,CAAC,6BAA6B;QAC7F,OAAO,KAAK,CAAC;IACf,CAAC;IAED,aAAa;QACX,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;IAED,aAAa;QACX,IAAI,CAAC,QAAQ,IAAI,CAAC,CAAC;QACnB,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,SAAS;YAAE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAClE,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC;IAChF,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { PACKAGE_NAME, PACKAGE_STATUS, SPEC_REF } from './types.js';
|
|
2
|
+
export type { Hex, Address, RiskLevel, ChainAuthorityEvidence, SignatureInput, SignatureObservation, RevocationResult, SignatureResult, AcceptanceResult, ChainAuthorityReader, ProviderObservation, ChainProvider, DivergenceEvent, ChainAuthorityReaderConfig, } from './types.js';
|
|
3
|
+
export { createChainAuthorityReader } from './reader.js';
|
|
4
|
+
export { CircuitBreaker } from './circuit-breaker.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACpE,YAAY,EACV,GAAG,EACH,OAAO,EACP,SAAS,EACT,sBAAsB,EACtB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,aAAa,EACb,eAAe,EACf,0BAA0B,GAC3B,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @agenticprimitives/chain-state — public API.
|
|
2
|
+
// See ../../specs/289-chain-state-authority-port.md.
|
|
3
|
+
export { PACKAGE_NAME, PACKAGE_STATUS, SPEC_REF } from './types.js';
|
|
4
|
+
export { createChainAuthorityReader } from './reader.js';
|
|
5
|
+
export { CircuitBreaker } from './circuit-breaker.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,qDAAqD;AAErD,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAkBpE,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/reader.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../src/reader.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAGV,oBAAoB,EACpB,0BAA0B,EAO3B,MAAM,YAAY,CAAC;AAIpB,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,0BAA0B,GAAG,oBAAoB,CAmKnG"}
|
package/dist/reader.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// The resilient ChainAuthorityReader: multi-provider with per-provider circuit
|
|
2
|
+
// breakers + timeouts, divergence → fail-closed, and the asymmetric revocation
|
|
3
|
+
// freshness invariant (spec 289 §3–§4). Pure over INJECTED providers.
|
|
4
|
+
import { CircuitBreaker } from './circuit-breaker.js';
|
|
5
|
+
const ZERO_HASH = `0x${'00'.repeat(32)}`;
|
|
6
|
+
export function createChainAuthorityReader(config) {
|
|
7
|
+
if (config.providers.length === 0)
|
|
8
|
+
throw new Error('chain-state: at least one provider is required');
|
|
9
|
+
const now = config.now ?? (() => Date.now());
|
|
10
|
+
const timeoutMs = config.timeoutMs ?? 4000;
|
|
11
|
+
const freshnessMs = config.revocationFreshnessMs ?? 12_000;
|
|
12
|
+
const breakers = new Map();
|
|
13
|
+
const breaker = (source) => {
|
|
14
|
+
let b = breakers.get(source);
|
|
15
|
+
if (!b) {
|
|
16
|
+
b = new CircuitBreaker(config.breakerThreshold ?? 3, config.breakerCooldownMs ?? 30_000, now);
|
|
17
|
+
breakers.set(source, b);
|
|
18
|
+
}
|
|
19
|
+
return b;
|
|
20
|
+
};
|
|
21
|
+
// Revocation caches: monotonic `revoked` (cache forever) + bounded-freshness
|
|
22
|
+
// `not revoked` (re-read past the window) — ADR-0013 applied to time.
|
|
23
|
+
const revokedCache = new Map();
|
|
24
|
+
const notRevokedCache = new Map();
|
|
25
|
+
const failClosed = (reason) => ({
|
|
26
|
+
chainId: config.chainId,
|
|
27
|
+
blockNumber: 0n,
|
|
28
|
+
blockHash: ZERO_HASH,
|
|
29
|
+
observedAt: now(),
|
|
30
|
+
source: `fail-closed:${reason}`,
|
|
31
|
+
});
|
|
32
|
+
const withTimeout = async (p) => {
|
|
33
|
+
let timer;
|
|
34
|
+
try {
|
|
35
|
+
return await Promise.race([
|
|
36
|
+
p,
|
|
37
|
+
new Promise((_, reject) => {
|
|
38
|
+
timer = setTimeout(() => reject(new Error('chain-state: provider timeout')), timeoutMs);
|
|
39
|
+
}),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
if (timer)
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Query providers per the risk policy and resolve to a single (value, evidence).
|
|
49
|
+
* `conservative` is both the fail-closed value AND the value returned on divergence
|
|
50
|
+
* — it is the restrictive answer (revoked=true / valid=false / accepted=false).
|
|
51
|
+
* `ok=false` means no confirming read happened (fail-closed / divergence / quorum).
|
|
52
|
+
*/
|
|
53
|
+
const resolve = async (args) => {
|
|
54
|
+
const observations = [];
|
|
55
|
+
for (const p of config.providers) {
|
|
56
|
+
if (!breaker(p.source).allow())
|
|
57
|
+
continue;
|
|
58
|
+
try {
|
|
59
|
+
const obs = await withTimeout(args.read(p));
|
|
60
|
+
breaker(p.source).recordSuccess();
|
|
61
|
+
observations.push({ source: p.source, obs });
|
|
62
|
+
if (args.risk === 'low')
|
|
63
|
+
break; // first healthy answer suffices
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
breaker(p.source).recordFailure();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (observations.length === 0) {
|
|
70
|
+
return { value: args.conservative, ok: false, evidence: failClosed('no-provider') };
|
|
71
|
+
}
|
|
72
|
+
if (args.risk === 'critical') {
|
|
73
|
+
const quorum = config.criticalQuorum ?? config.providers.length;
|
|
74
|
+
const eq = args.equals ?? ((a, b) => a === b);
|
|
75
|
+
const values = observations.map((o) => o.obs.value);
|
|
76
|
+
const allAgree = values.every((v) => eq(v, values[0]));
|
|
77
|
+
if (!allAgree) {
|
|
78
|
+
config.onDivergence?.({
|
|
79
|
+
method: args.method,
|
|
80
|
+
key: args.key,
|
|
81
|
+
values: observations.map((o) => ({
|
|
82
|
+
source: o.source,
|
|
83
|
+
value: o.obs.value,
|
|
84
|
+
})),
|
|
85
|
+
});
|
|
86
|
+
return { value: args.conservative, ok: false, evidence: failClosed('divergence') };
|
|
87
|
+
}
|
|
88
|
+
if (observations.length < quorum) {
|
|
89
|
+
return { value: args.conservative, ok: false, evidence: failClosed('quorum') };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const chosen = observations[0];
|
|
93
|
+
return {
|
|
94
|
+
value: chosen.obs.value,
|
|
95
|
+
ok: true,
|
|
96
|
+
evidence: {
|
|
97
|
+
chainId: config.chainId,
|
|
98
|
+
blockNumber: chosen.obs.blockNumber,
|
|
99
|
+
blockHash: chosen.obs.blockHash,
|
|
100
|
+
observedAt: now(),
|
|
101
|
+
source: chosen.source,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
async isDelegationRevoked(hash, risk = 'low') {
|
|
107
|
+
const cachedRevoked = revokedCache.get(hash);
|
|
108
|
+
if (cachedRevoked)
|
|
109
|
+
return { revoked: true, evidence: cachedRevoked }; // monotonic — cache forever
|
|
110
|
+
const fresh = notRevokedCache.get(hash);
|
|
111
|
+
if (fresh && now() - fresh.observedAt < freshnessMs) {
|
|
112
|
+
return { revoked: false, evidence: fresh.evidence };
|
|
113
|
+
}
|
|
114
|
+
const r = await resolve({
|
|
115
|
+
method: 'isDelegationRevoked',
|
|
116
|
+
key: hash,
|
|
117
|
+
conservative: true, // fail-closed = treat as revoked
|
|
118
|
+
read: (p) => p.readRevoked(hash),
|
|
119
|
+
risk,
|
|
120
|
+
});
|
|
121
|
+
if (r.ok) {
|
|
122
|
+
if (r.value) {
|
|
123
|
+
revokedCache.set(hash, r.evidence);
|
|
124
|
+
return { revoked: true, evidence: r.evidence };
|
|
125
|
+
}
|
|
126
|
+
notRevokedCache.set(hash, { evidence: r.evidence, observedAt: r.evidence.observedAt });
|
|
127
|
+
return { revoked: false, evidence: r.evidence };
|
|
128
|
+
}
|
|
129
|
+
// Could not confirm → fail closed; never cache a fail-closed "not revoked".
|
|
130
|
+
return { revoked: true, evidence: r.evidence };
|
|
131
|
+
},
|
|
132
|
+
async verifySmartAgentSignature(input, risk = 'low') {
|
|
133
|
+
const r = await resolve({
|
|
134
|
+
method: 'verifySmartAgentSignature',
|
|
135
|
+
// fail-closed = invalid AND treated-as-undeployed (the restrictive answer
|
|
136
|
+
// for the consumer's requireDeployed policy).
|
|
137
|
+
conservative: { valid: false, deployed: false },
|
|
138
|
+
key: `${input.signer}:${input.digest}`,
|
|
139
|
+
read: (p) => p.readSignatureValid(input),
|
|
140
|
+
// Both facts must agree for a `critical` quorum.
|
|
141
|
+
equals: (a, b) => a.valid === b.valid && a.deployed === b.deployed,
|
|
142
|
+
risk,
|
|
143
|
+
});
|
|
144
|
+
return { valid: r.value.valid, deployed: r.value.deployed, evidence: r.evidence };
|
|
145
|
+
},
|
|
146
|
+
async isSessionDelegationAccepted(principal, hash, risk = 'low') {
|
|
147
|
+
const r = await resolve({
|
|
148
|
+
method: 'isSessionDelegationAccepted',
|
|
149
|
+
key: `${principal}:${hash}`,
|
|
150
|
+
conservative: false, // fail-closed = not accepted
|
|
151
|
+
read: (p) => p.readSessionAccepted(principal, hash),
|
|
152
|
+
risk,
|
|
153
|
+
});
|
|
154
|
+
return { accepted: r.value, evidence: r.evidence };
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reader.js","sourceRoot":"","sources":["../src/reader.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+EAA+E;AAC/E,sEAAsE;AAEtE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AActD,MAAM,SAAS,GAAQ,KAAK,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;AAE9C,MAAM,UAAU,0BAA0B,CAAC,MAAkC;IAC3E,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACrG,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC7C,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC;IAC3C,MAAM,WAAW,GAAG,MAAM,CAAC,qBAAqB,IAAI,MAAM,CAAC;IAC3D,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA0B,CAAC;IACnD,MAAM,OAAO,GAAG,CAAC,MAAc,EAAkB,EAAE;QACjD,IAAI,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC7B,IAAI,CAAC,CAAC,EAAE,CAAC;YACP,CAAC,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,gBAAgB,IAAI,CAAC,EAAE,MAAM,CAAC,iBAAiB,IAAI,MAAM,EAAE,GAAG,CAAC,CAAC;YAC9F,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QAC1B,CAA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
2
|
+
export declare const PACKAGE_STATUS: "w1-port";
|
|
3
|
+
export declare const SPEC_REF: "specs/289-chain-state-authority-port.md";
|
|
4
|
+
export type Hex = `0x${string}`;
|
|
5
|
+
export type Address = `0x${string}`;
|
|
6
|
+
/** Risk axis selecting how strict the read policy is (spec 289 §3). */
|
|
7
|
+
export type RiskLevel = 'low' | 'critical';
|
|
8
|
+
/** What a read was decided against — threaded into the audit trail. */
|
|
9
|
+
export interface ChainAuthorityEvidence {
|
|
10
|
+
chainId: number;
|
|
11
|
+
blockNumber: bigint;
|
|
12
|
+
blockHash: Hex;
|
|
13
|
+
/** epoch ms the observation was made. */
|
|
14
|
+
observedAt: number;
|
|
15
|
+
/** provider id, or `fail-closed:<reason>` when no confirming read was possible. */
|
|
16
|
+
source: string;
|
|
17
|
+
}
|
|
18
|
+
export interface SignatureInput {
|
|
19
|
+
signer: Address;
|
|
20
|
+
digest: Hex;
|
|
21
|
+
signature: Hex;
|
|
22
|
+
}
|
|
23
|
+
export interface RevocationResult {
|
|
24
|
+
revoked: boolean;
|
|
25
|
+
evidence: ChainAuthorityEvidence;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* What a single signature read observed (spec 289 §5 W3). `valid` is the
|
|
29
|
+
* authority answer (ERC-1271 / ERC-6492 / ECDSA via the provider's chosen
|
|
30
|
+
* surface). `deployed` is whether the signer SA had bytecode at the observed
|
|
31
|
+
* block — a COUNTERFACTUAL (ERC-6492) signature can be `valid: true` while
|
|
32
|
+
* `deployed: false`. The port surfaces both as FACTS; the consumer applies the
|
|
33
|
+
* `requireDeployed` policy (delegation/token.ts) — the port never decides it.
|
|
34
|
+
*/
|
|
35
|
+
export interface SignatureObservation {
|
|
36
|
+
valid: boolean;
|
|
37
|
+
deployed: boolean;
|
|
38
|
+
}
|
|
39
|
+
export interface SignatureResult {
|
|
40
|
+
valid: boolean;
|
|
41
|
+
/** Signer SA had deployed bytecode at the observed block. Fail-closed /
|
|
42
|
+
* divergence / quorum-miss reads report `false` (the restrictive answer). */
|
|
43
|
+
deployed: boolean;
|
|
44
|
+
evidence: ChainAuthorityEvidence;
|
|
45
|
+
}
|
|
46
|
+
export interface AcceptanceResult {
|
|
47
|
+
accepted: boolean;
|
|
48
|
+
evidence: ChainAuthorityEvidence;
|
|
49
|
+
}
|
|
50
|
+
/** The authority read port consumed by delegation/mcp-runtime verify paths. */
|
|
51
|
+
export interface ChainAuthorityReader {
|
|
52
|
+
isDelegationRevoked(hash: Hex, risk?: RiskLevel): Promise<RevocationResult>;
|
|
53
|
+
verifySmartAgentSignature(input: SignatureInput, risk?: RiskLevel): Promise<SignatureResult>;
|
|
54
|
+
isSessionDelegationAccepted(principal: Address, hash: Hex, risk?: RiskLevel): Promise<AcceptanceResult>;
|
|
55
|
+
}
|
|
56
|
+
/** A single observation from one provider, pinned to the block it read at. */
|
|
57
|
+
export interface ProviderObservation<T> {
|
|
58
|
+
value: T;
|
|
59
|
+
blockNumber: bigint;
|
|
60
|
+
blockHash: Hex;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* An injected chain provider. Each method does ONE `readContract`-equivalent
|
|
64
|
+
* `eth_call` and reports the block it read at. Implementations are app-supplied
|
|
65
|
+
* (e.g. a viem-backed adapter); the core never imports a chain library.
|
|
66
|
+
*/
|
|
67
|
+
export interface ChainProvider {
|
|
68
|
+
/** Stable id (goes into evidence.source + circuit-breaker keying). */
|
|
69
|
+
readonly source: string;
|
|
70
|
+
readRevoked(hash: Hex): Promise<ProviderObservation<boolean>>;
|
|
71
|
+
/** ONE observation reporting BOTH whether the signature is valid AND whether
|
|
72
|
+
* the signer SA is deployed, pinned to the block both were read at. */
|
|
73
|
+
readSignatureValid(input: SignatureInput): Promise<ProviderObservation<SignatureObservation>>;
|
|
74
|
+
readSessionAccepted(principal: Address, hash: Hex): Promise<ProviderObservation<boolean>>;
|
|
75
|
+
}
|
|
76
|
+
/** Emitted when providers disagree on an authority answer (spec 289 §3). */
|
|
77
|
+
export interface DivergenceEvent {
|
|
78
|
+
method: 'isDelegationRevoked' | 'verifySmartAgentSignature' | 'isSessionDelegationAccepted';
|
|
79
|
+
key: string;
|
|
80
|
+
values: Array<{
|
|
81
|
+
source: string;
|
|
82
|
+
value: boolean | SignatureObservation;
|
|
83
|
+
}>;
|
|
84
|
+
}
|
|
85
|
+
export interface ChainAuthorityReaderConfig {
|
|
86
|
+
chainId: number;
|
|
87
|
+
providers: ChainProvider[];
|
|
88
|
+
/** Per-call timeout (ms) for a single provider read. Default 4000. */
|
|
89
|
+
timeoutMs?: number;
|
|
90
|
+
/** Consecutive failures before a provider's breaker opens. Default 3. */
|
|
91
|
+
breakerThreshold?: number;
|
|
92
|
+
/** Cooldown (ms) before an open breaker half-opens. Default 30000. */
|
|
93
|
+
breakerCooldownMs?: number;
|
|
94
|
+
/** Bounded freshness for a cached `not revoked` answer (ms). Default 12000. */
|
|
95
|
+
revocationFreshnessMs?: number;
|
|
96
|
+
/** Providers that must AGREE for a `critical` read. Default = all providers. */
|
|
97
|
+
criticalQuorum?: number;
|
|
98
|
+
/** Observation clock (tests inject). Default Date.now. */
|
|
99
|
+
now?: () => number;
|
|
100
|
+
/** Divergence metric hook (fail-soft telemetry). */
|
|
101
|
+
onDivergence?: (e: DivergenceEvent) => void;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,YAAY,EAAG,gCAAyC,CAAC;AACtE,eAAO,MAAM,cAAc,EAAG,SAAkB,CAAC;AACjD,eAAO,MAAM,QAAQ,EAAG,yCAAkD,CAAC;AAE3E,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAChC,MAAM,MAAM,OAAO,GAAG,KAAK,MAAM,EAAE,CAAC;AAEpC,uEAAuE;AACvE,MAAM,MAAM,SAAS,GAAG,KAAK,GA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;IACpD,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,eAAe,KAAK,IAAI,CAAC;CAC7C"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// chain-state — the chain-read authority port (spec 289).
|
|
2
|
+
//
|
|
3
|
+
// Authorization-gating chain reads (isDelegationRevoked, verifySmartAgentSignature,
|
|
4
|
+
// isSessionDelegationAccepted) are part of the security decision, not convenience
|
|
5
|
+
// lookups: they must be resilient (one flaky provider must not weaken the answer),
|
|
6
|
+
// carry EVIDENCE (which block/hash/when/provider), and have an explicit freshness
|
|
7
|
+
// contract. This module is the PORT + a resilient reader over INJECTED providers
|
|
8
|
+
// (no viem in core — pure, testable; an app wires the viem adapter).
|
|
9
|
+
// `readContract`-equivalent calls only — never chain log scans (ADR-0012).
|
|
10
|
+
export const PACKAGE_NAME = '@agenticprimitives/chain-state';
|
|
11
|
+
export const PACKAGE_STATUS = 'w1-port';
|
|
12
|
+
export const SPEC_REF = 'specs/289-chain-state-authority-port.md';
|
|
13
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,EAAE;AACF,oFAAoF;AACpF,kFAAkF;AAClF,mFAAmF;AACnF,kFAAkF;AAClF,iFAAiF;AACjF,qEAAqE;AACrE,2EAA2E;AAE3E,MAAM,CAAC,MAAM,YAAY,GAAG,gCAAyC,CAAC;AACtE,MAAM,CAAC,MAAM,cAAc,GAAG,SAAkB,CAAC;AACjD,MAAM,CAAC,MAAM,QAAQ,GAAG,yCAAkD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agenticprimitives/chain-state",
|
|
3
|
+
"version": "0.0.0-alpha.1",
|
|
4
|
+
"description": "The chain-read authority port: resilient, evidence-bearing, fail-closed reads (isDelegationRevoked, verifySmartAgentSignature, isSessionDelegationAccepted) over multiple injected providers with circuit breakers, divergence detection, and bounded revocation freshness.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/agentictrustlabs/agenticprimitives.git",
|
|
9
|
+
"directory": "packages/chain-state"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/agentictrustlabs/agenticprimitives/tree/master/packages/chain-state",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/agentictrustlabs/agenticprimitives/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"LICENSE",
|
|
26
|
+
"dist",
|
|
27
|
+
"spec.md",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"vitest": "^4.1.8"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"agentic",
|
|
38
|
+
"primitives",
|
|
39
|
+
"chain",
|
|
40
|
+
"evidence",
|
|
41
|
+
"revocation"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.build.json",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"test:unit": "vitest run test/unit --passWithNoTests",
|
|
48
|
+
"test:watch": "vitest",
|
|
49
|
+
"clean": "rm -rf dist"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/spec.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @agenticprimitives/chain-state — spec
|
|
2
|
+
|
|
3
|
+
The full design lives in
|
|
4
|
+
[`../../specs/289-chain-state-authority-port.md`](../../specs/289-chain-state-authority-port.md), backed by
|
|
5
|
+
[ADR-0042](../../docs/architecture/decisions/0042-chain-state-is-the-chain-read-authority-port.md).
|
|
6
|
+
|
|
7
|
+
This package is the single **chain-read authority port**. Authorization-gating chain reads —
|
|
8
|
+
`isDelegationRevoked`, `verifySmartAgentSignature` (ERC-1271/6492/ECDSA), `isSessionDelegationAccepted` —
|
|
9
|
+
are part of the security decision, so they run through one resilient, evidence-bearing, fail-closed
|
|
10
|
+
surface instead of an ad-hoc single RPC client. Multiple providers with per-provider circuit breakers +
|
|
11
|
+
timeouts; divergence between providers resolves fail-closed and emits a metric; every answer carries
|
|
12
|
+
`ChainAuthorityEvidence` (block number + hash + provider + timestamp). `readContract`-equivalent `eth_call`
|
|
13
|
+
only — never `eth_getLogs` ([ADR-0012](../../docs/architecture/decisions/0012-no-eth-getlogs-in-product-read-paths.md)).
|
|
14
|
+
|
|
15
|
+
The **revocation freshness invariant** ([ADR-0013](../../docs/architecture/decisions/0013-no-silent-fallbacks.md)
|
|
16
|
+
applied to time): a known-revoked grant is cached forever (monotonic); a cached `not revoked` is
|
|
17
|
+
authoritative only within a bounded window, then re-read; a failed re-read fails closed.
|
|
18
|
+
|
|
19
|
+
It does NOT own the chain library (providers are injected; the viem adapter lives in the consumer), the
|
|
20
|
+
verify orchestration (`delegation`/`mcp-runtime` call this port — the dep edge is `types ← chain-state ←
|
|
21
|
+
delegation`, and chain-state never imports delegation), or an indexer/read model (external infra,
|
|
22
|
+
[ADR-0037](../../docs/architecture/decisions/0037-primitives-pure-repo-external-integration-and-ux-layers.md)).
|
|
23
|
+
|
|
24
|
+
Status: `w1-port` — the port + resilient reader (multi-provider, circuit breakers, timeout, divergence →
|
|
25
|
+
fail-closed, low/critical policy, revocation freshness). Later waves: the viem-backed `ChainProvider`
|
|
26
|
+
adapter, reorg confirmation-depth policy, and the `delegation/token.ts` + `mcp-runtime` rewire (spec 289
|
|
27
|
+
§5) that routes the four existing read sites + spec 287's ERC-1271 path through this port.
|