@agenticprimitives/contracts 1.0.0-alpha.45 → 1.0.0-alpha.47

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 (82) hide show
  1. package/AUDIT.md +60 -26
  2. package/deployments-base-sepolia.json +2 -0
  3. package/deployments-faithchain.json +2 -0
  4. package/deployments-hopechain.json +2 -0
  5. package/dist/abi/AgentAccount.json +175 -0
  6. package/dist/abi/AgentAccountFactory.json +47 -0
  7. package/dist/abi/AgentNameAttributeResolver.json +24 -0
  8. package/dist/abi/AgentNameRegistry.json +369 -0
  9. package/dist/abi/AgentNameUniversalResolver.json +34 -0
  10. package/dist/abi/AgentProfileResolver.json +48 -0
  11. package/dist/abi/AgentRegistryBase.json +158 -0
  12. package/dist/abi/AgentRelationship.json +212 -0
  13. package/dist/abi/AgreementRegistry.json +10 -0
  14. package/dist/abi/AttestationRegistry.json +49 -0
  15. package/dist/abi/AttributeStorage.json +24 -0
  16. package/dist/abi/CustodyPolicy.json +184 -0
  17. package/dist/abi/DelegationManager.json +96 -0
  18. package/dist/abi/DigestBindingEnforcer.json +103 -1
  19. package/dist/abi/EstateProjectionRegistry.json +137 -24
  20. package/dist/abi/GeoFeatureRegistry.json +42 -0
  21. package/dist/abi/IAgentAccountFactoryView.json +13 -0
  22. package/dist/abi/IAgentAccountGovernanceTimelockView.json +15 -0
  23. package/dist/abi/ICustodyPolicyFactoryView.json +13 -0
  24. package/dist/abi/ICustodyPolicyPasskeyView.json +50 -0
  25. package/dist/abi/IRedemptionContext.json +15 -0
  26. package/dist/abi/ITreasurySpendPolicyAccountView.json +44 -0
  27. package/dist/abi/IUpgradeVoucherView.json +26 -0
  28. package/dist/abi/OntologyTermRegistry.json +57 -0
  29. package/dist/abi/PaymentEnforcer.json +10 -0
  30. package/dist/abi/PermissionlessSubregistry.json +165 -0
  31. package/dist/abi/PricedSubregistry.json +57 -0
  32. package/dist/abi/RelationshipTypeRegistry.json +57 -0
  33. package/dist/abi/ShapeRegistry.json +57 -0
  34. package/dist/abi/SkillDefinitionRegistry.json +42 -0
  35. package/dist/abi/TreasurySpendPolicy.json +106 -1
  36. package/dist/abi/UpgradeVoucher.json +176 -0
  37. package/dist/abi/index.d.ts +6 -0
  38. package/dist/abi/index.js +6 -0
  39. package/dist/deployments/base-sepolia.d.ts +2 -0
  40. package/dist/deployments/base-sepolia.js +4 -0
  41. package/dist/deployments/faithchain.d.ts +2 -0
  42. package/dist/deployments/faithchain.js +4 -0
  43. package/dist/deployments/hopechain.d.ts +2 -0
  44. package/dist/deployments/hopechain.js +4 -0
  45. package/package.json +1 -1
  46. package/src/AgentAccount.sol +531 -92
  47. package/src/AgentAccountFactory.sol +28 -2
  48. package/src/IAgentAccount.sol +14 -0
  49. package/src/SmartAgentPaymaster.sol +30 -3
  50. package/src/UpgradeVoucher.sol +74 -0
  51. package/src/agency/DelegationManager.sol +177 -52
  52. package/src/agency/ICaveatEnforcer.sol +10 -0
  53. package/src/agency/IDelegationManager.sol +6 -5
  54. package/src/agreement/AgreementRegistry.sol +61 -17
  55. package/src/attestation/AttestationRegistry.sol +115 -22
  56. package/src/custody/CustodyPolicy.sol +393 -47
  57. package/src/custody/TreasurySpendPolicy.sol +192 -29
  58. package/src/enforcers/AllowedMethodsEnforcer.sol +6 -1
  59. package/src/enforcers/AllowedTargetsEnforcer.sol +6 -1
  60. package/src/enforcers/DigestBindingEnforcer.sol +91 -19
  61. package/src/enforcers/PaymentEnforcer.sol +32 -1
  62. package/src/enforcers/QuorumEnforcer.sol +1 -1
  63. package/src/enforcers/ValueEnforcer.sol +6 -1
  64. package/src/geo/GeoFeatureRegistry.sol +25 -1
  65. package/src/governance/AgenticGovernance.sol +16 -0
  66. package/src/identity/AgentProfileResolver.sol +11 -0
  67. package/src/naming/AgentNameAttributeResolver.sol +17 -2
  68. package/src/naming/AgentNamePredicates.sol +1 -1
  69. package/src/naming/AgentNameRegistry.sol +205 -10
  70. package/src/naming/AgentNameUniversalResolver.sol +26 -0
  71. package/src/naming/PermissionlessSubregistry.sol +69 -23
  72. package/src/naming/PricedSubregistry.sol +28 -0
  73. package/src/ontology/AttributeStorage.sol +93 -80
  74. package/src/ontology/OntologyTermRegistry.sol +28 -0
  75. package/src/ontology/ShapeRegistry.sol +28 -0
  76. package/src/payments/PaymentEscrow.sol +23 -15
  77. package/src/payments/PaymentReceiptRegistry.sol +11 -3
  78. package/src/registry/AgentRegistryBase.sol +39 -0
  79. package/src/registry/EstateProjectionRegistry.sol +94 -40
  80. package/src/relationships/AgentRelationship.sol +112 -7
  81. package/src/relationships/RelationshipTypeRegistry.sol +28 -0
  82. package/src/skills/SkillDefinitionRegistry.sol +25 -1
@@ -75,6 +75,8 @@ contract AgentRegistryBase {
75
75
  // ─── Storage ────────────────────────────────────────────────────────
76
76
 
77
77
  mapping(bytes32 => Registry) private _registries; // registryId => Registry
78
+ /// @notice REG-10 — registryId => the successor the controller named, until it accepts.
79
+ mapping(bytes32 => address) public pendingControllerOf;
78
80
  mapping(bytes32 => Entry) private _entries; // entryKey => Entry
79
81
  mapping(bytes32 => mapping(bytes32 => bytes32)) private _claims; // entryKey => claimSlotId => claimHash
80
82
 
@@ -92,12 +94,17 @@ contract AgentRegistryBase {
92
94
  error AlreadyRevoked();
93
95
  error InvalidExpiry();
94
96
  error MembershipDenied(); // RB-02 fail-closed admission denial
97
+ error NotSuspended(); // REG-10: reinstateEntry on an entry that is not Suspended
98
+ error NotPendingController(); // REG-10: acceptController by anyone but the named successor
95
99
 
96
100
  // ─── Events ─────────────────────────────────────────────────────────
97
101
 
98
102
  // Registry provisioning (not part of the registry-kit lifecycle ABI).
99
103
  event RegistryCreated(bytes32 indexed registryId, address indexed controller, address policy);
100
104
  event RegistryPolicyUpdated(bytes32 indexed registryId, address policy);
105
+ event RegistryControllerTransferStarted(bytes32 indexed registryId, address indexed controller, address indexed pendingController);
106
+ event RegistryControllerTransferred(bytes32 indexed registryId, address indexed previousController, address indexed controller);
107
+ event RegistryEntryReinstated(bytes32 indexed registryId, bytes32 indexed entryId, bytes32 reasonHash);
101
108
 
102
109
  // Entry lifecycle — MUST match registry-kit `AGENT_REGISTRY_BASE_ABI` + spec 279 §4.1 EXACTLY
103
110
  // (the SDK encodes calls against / decodes logs from these signatures).
@@ -133,6 +140,27 @@ contract AgentRegistryBase {
133
140
  emit RegistryCreated(registryId, msg.sender, policy);
134
141
  }
135
142
 
143
+ /// @notice REG-10 — name a successor controller (zero cancels). Two steps: the controller keeps the
144
+ /// role until the successor accepts, so a registry created by a bootstrap key can be handed to
145
+ /// a multisig, and a compromised controller rotated out, without risking a dead address.
146
+ function transferController(bytes32 registryId, address newController) external {
147
+ Registry storage r = _registries[registryId];
148
+ if (!r.exists) revert RegistryNotFound();
149
+ if (msg.sender != r.controller) revert NotController();
150
+ pendingControllerOf[registryId] = newController;
151
+ emit RegistryControllerTransferStarted(registryId, msg.sender, newController);
152
+ }
153
+
154
+ /// @notice REG-10 — take the controller role. Only the successor the controller named.
155
+ function acceptController(bytes32 registryId) external {
156
+ Registry storage r = _registries[registryId];
157
+ if (!r.exists) revert RegistryNotFound();
158
+ if (msg.sender != pendingControllerOf[registryId]) revert NotPendingController();
159
+ emit RegistryControllerTransferred(registryId, r.controller, msg.sender);
160
+ r.controller = msg.sender;
161
+ pendingControllerOf[registryId] = address(0);
162
+ }
163
+
136
164
  /// @notice Swap the pluggable membership hook. Controller only.
137
165
  function setRegistryPolicy(bytes32 registryId, address policy) external {
138
166
  Registry storage r = _registries[registryId];
@@ -238,6 +266,17 @@ contract AgentRegistryBase {
238
266
  emit RegistryEntrySuspended(registryId, entryId, reasonHash);
239
267
  }
240
268
 
269
+ /// @notice REG-10 — lift a suspension (Suspended → Active). Same authority as `suspendEntry`. Before
270
+ /// this, the only exit from Suspended was `revokeEntry`, which is terminal and burns the id.
271
+ function reinstateEntry(bytes32 registryId, bytes32 entryId, bytes32 reasonHash) external {
272
+ Entry storage e = _entries[_entryKey(registryId, entryId)];
273
+ if (e.status == EntryStatus.None) revert EntryNotFound();
274
+ _requireAdmin(registryId, entryId); // RB-03
275
+ if (e.status != EntryStatus.Suspended) revert NotSuspended();
276
+ e.status = EntryStatus.Active;
277
+ emit RegistryEntryReinstated(registryId, entryId, reasonHash);
278
+ }
279
+
241
280
  /// @notice Revoke an entry (terminal). The subject SA may leave; the controller / policy-admin may eject.
242
281
  function revokeEntry(bytes32 registryId, bytes32 entryId, bytes32 reasonHash) external {
243
282
  Entry storage e = _entries[_entryKey(registryId, entryId)];
@@ -11,11 +11,33 @@ pragma solidity ^0.8.28;
11
11
  * verifier in another estate that a grant is live or an agent is a member in that estate as of that
12
12
  * sequence, and nothing else (the estate-projection SDK package).
13
13
  *
14
- * WHO MAY WRITE. An estate REGISTERS once: its id — keccak256("eip155:<chainId>:<governance>"), the
15
- * one fact public ground learns about it — bound to the SIGNER its governance controls here (an EOA
16
- * or an account on this chain). First registration wins; the signer rotates only by the current
17
- * signer. `setRoots` is the signer's, with a sequence strictly above the last: a replayed or reordered
18
- * projection is refused. No admin, no pause, no upgrade: public ground has no operator either.
14
+ * WHO MAY WRITE (generation 4, spec 445 / REG-2). ANYONE may anchor roots for an estate id — under
15
+ * THEIR OWN address. A row is keyed by `(estateId, signer)`, the pattern `ReceiptAnchorRegistry`
16
+ * adopted in spec 408 §1.5: WHO anchored is the evidence, never a gate. There is no registration to
17
+ * win and therefore nothing to squat: `estateId = keccak256("eip155:<chainId>:<governance>")` is
18
+ * predictable, and a stranger anchoring under it writes a row no verifier reads.
19
+ *
20
+ * WHICH ROW A VERIFIER READS. The estate says who its projection signer is, and the verifier reads
21
+ * exactly that row — `roots(estateId, signer)`:
22
+ * - the estate's agent declares it on the ESTATE'S OWN chain (`atl:projectionSigner`, spec 439
23
+ * F2.5). This is the only proof available across chains: public ground cannot call ERC-1271 on,
24
+ * or read the storage of, a governance account that lives on another chain.
25
+ * - when the estate's governance account lives on THIS chain, it can also say so here:
26
+ * `declareSigner(signer)` is callable only by the governance address the id is derived from
27
+ * (`estateIdOf(block.chainid, msg.sender)`), and `declaredSignerOf(estateId)` is then a proof
28
+ * this chain checked itself.
29
+ * A verifier that knows neither has no row to read; it never picks one by being first.
30
+ *
31
+ * ROTATION. The current signer retires its row with `rotateSigner(estateId, to)` — the old key can
32
+ * anchor nothing more, and the row names its successor and the sequence it stopped at, so a
33
+ * verifier can require the successor's sequence to continue above it. A LOST key needs no
34
+ * ceremony here at all: the estate re-declares a new signer (on its chain, or `declareSigner`) and
35
+ * that signer starts its own row. No admin, no pause, no upgrade, no recovery governor: public
36
+ * ground has no operator, and with rows keyed by signer it does not need one.
37
+ *
38
+ * `setRoots` takes a sequence strictly above the row's last (a replayed or reordered projection is
39
+ * refused) and at most `MAX_SEQUENCE_STEP` above it (a mistaken `type(uint64).max` cannot brick
40
+ * the row).
19
41
  *
20
42
  * WHAT THIS IS NOT: authority. A root proves what the estate vouches for at a moment; whether the
21
43
  * receiving estate lets an agent ACT is its own mandate, per step (ADR-0056 / ADR-0057).
@@ -29,60 +51,92 @@ contract EstateProjectionRegistry {
29
51
  uint64 anchoredAt;
30
52
  }
31
53
 
32
- mapping(bytes32 estateId => address signer) public signerOf;
33
- mapping(bytes32 estateId => Roots) private _roots;
54
+ /// @notice The furthest one `setRoots` may advance a row's sequence. Sequences are small counters (one
55
+ /// per revocation / membership change); this bounds an operator mistake, not honest use.
56
+ uint64 public constant MAX_SEQUENCE_STEP = 1 << 32;
57
+
58
+ /// estateId => signer => the roots that signer anchored. A stranger's row for the same estate id is a
59
+ /// different row: it can neither pre-empt the estate's nor be mistaken for it.
60
+ mapping(bytes32 estateId => mapping(address signer => Roots)) private _roots;
61
+ /// estateId => signer => the successor it rotated to (non-zero ⇒ the row is retired).
62
+ mapping(bytes32 estateId => mapping(address signer => address)) public successorOf;
63
+ /// estateId => the signer declared by the estate's governance account ON THIS CHAIN (zero when the
64
+ /// governance lives elsewhere or has not declared).
65
+ mapping(bytes32 estateId => address signer) public declaredSignerOf;
34
66
 
35
67
  event EstateRegistered(bytes32 indexed estateId, address indexed signer);
36
68
  event EstateSignerRotated(bytes32 indexed estateId, address indexed from, address indexed to);
37
- event RootsAnchored(bytes32 indexed estateId, uint64 indexed sequence, bytes32 liveGrantRoot, bytes32 membershipRoot, bytes32 charterHash);
69
+ event EstateSignerDeclared(bytes32 indexed estateId, address indexed governance, address indexed signer);
70
+ event RootsAnchored(bytes32 indexed estateId, address indexed signer, uint64 indexed sequence, bytes32 liveGrantRoot, bytes32 membershipRoot, bytes32 charterHash);
38
71
 
39
- error AlreadyRegistered(bytes32 estateId);
40
- error NotRegistered(bytes32 estateId);
41
- error NotTheSigner(bytes32 estateId, address caller);
72
+ error NotRegistered(bytes32 estateId, address signer);
73
+ error SignerRetired(bytes32 estateId, address signer, address successor);
42
74
  error SequenceNotAbove(uint64 last, uint64 given);
75
+ error SequenceStepTooLarge(uint64 last, uint64 given);
43
76
  error ZeroAddress();
44
77
 
45
- /// @notice Bind an estate id to the signer its governance controls on this chain. First come, once.
46
- function register(bytes32 estateId, address signer) external {
47
- if (signer == address(0)) revert ZeroAddress();
48
- if (signerOf[estateId] != address(0)) revert AlreadyRegistered(estateId);
49
- signerOf[estateId] = signer;
50
- emit EstateRegistered(estateId, signer);
51
- }
52
-
53
- function rotateSigner(bytes32 estateId, address to) external {
54
- if (to == address(0)) revert ZeroAddress();
55
- address cur = signerOf[estateId];
56
- if (cur == address(0)) revert NotRegistered(estateId);
57
- if (msg.sender != cur) revert NotTheSigner(estateId, msg.sender);
58
- signerOf[estateId] = to;
59
- emit EstateSignerRotated(estateId, cur, to);
60
- }
61
-
62
- /// @notice Anchor the estate's roots under `sequence` (strictly above the last). Refreshed on every revocation
63
- /// and membership change; a verifier's freshness policy reads `anchoredAt`.
78
+ /// @notice Anchor the caller's roots for `estateId` under `sequence` (strictly above the caller's last,
79
+ /// by at most `MAX_SEQUENCE_STEP`). The first anchor opens the caller's row. Refreshed on every
80
+ /// revocation and membership change; a verifier's freshness policy reads `anchoredAt`.
64
81
  function setRoots(bytes32 estateId, bytes32 liveGrantRoot, bytes32 membershipRoot, bytes32 charterHash, uint64 sequence) external {
65
- address cur = signerOf[estateId];
66
- if (cur == address(0)) revert NotRegistered(estateId);
67
- if (msg.sender != cur) revert NotTheSigner(estateId, msg.sender);
68
- Roots storage r = _roots[estateId];
69
- if (sequence <= r.sequence) revert SequenceNotAbove(r.sequence, sequence);
82
+ address successor = successorOf[estateId][msg.sender];
83
+ if (successor != address(0)) revert SignerRetired(estateId, msg.sender, successor);
84
+ Roots storage r = _roots[estateId][msg.sender];
85
+ uint64 last = r.sequence;
86
+ if (sequence <= last) revert SequenceNotAbove(last, sequence);
87
+ if (sequence - last > MAX_SEQUENCE_STEP) revert SequenceStepTooLarge(last, sequence);
88
+ // slither-disable-next-line incorrect-equality
89
+ if (r.anchoredAt == 0) emit EstateRegistered(estateId, msg.sender); // first-anchor sentinel
70
90
  r.liveGrantRoot = liveGrantRoot;
71
91
  r.membershipRoot = membershipRoot;
72
92
  r.charterHash = charterHash;
73
93
  r.sequence = sequence;
74
94
  r.anchoredAt = uint64(block.timestamp);
75
- emit RootsAnchored(estateId, sequence, liveGrantRoot, membershipRoot, charterHash);
95
+ emit RootsAnchored(estateId, msg.sender, sequence, liveGrantRoot, membershipRoot, charterHash);
96
+ }
97
+
98
+ /// @notice Retire the caller's row for `estateId` and name its successor. The caller must have anchored
99
+ /// at least once; afterwards it can anchor nothing under this estate id. This writes NOTHING
100
+ /// into the successor's row (a stranger rotating "to" the estate's real signer changes nothing
101
+ /// for it) — the successor anchors for itself, and the estate's declaration is what makes a
102
+ /// verifier read it.
103
+ function rotateSigner(bytes32 estateId, address to) external {
104
+ if (to == address(0) || to == msg.sender) revert ZeroAddress();
105
+ // `anchoredAt == 0` is the "this signer never anchored" sentinel (storage default), not arithmetic.
106
+ // slither-disable-next-line incorrect-equality
107
+ if (_roots[estateId][msg.sender].anchoredAt == 0) revert NotRegistered(estateId, msg.sender);
108
+ address successor = successorOf[estateId][msg.sender];
109
+ if (successor != address(0)) revert SignerRetired(estateId, msg.sender, successor);
110
+ successorOf[estateId][msg.sender] = to;
111
+ emit EstateSignerRotated(estateId, msg.sender, to);
76
112
  }
77
113
 
78
- /// @notice The one read a verifier makes: the roots as anchored, with the sequence and when.
79
- function roots(bytes32 estateId) external view returns (bytes32 liveGrantRoot, bytes32 membershipRoot, bytes32 charterHash, uint64 sequence, uint64 anchoredAt) {
80
- Roots storage r = _roots[estateId];
114
+ /// @notice Same-chain proof: the governance account an estate id is derived from names its projection
115
+ /// signer. Only meaningful for an estate whose governance lives on this chain — the id is
116
+ /// computed from `msg.sender` and `block.chainid`, so nobody can declare for another estate.
117
+ /// Callable again to re-declare (rotation, or a lost key); zero clears the declaration.
118
+ function declareSigner(address signer) external returns (bytes32 estateId) {
119
+ estateId = estateIdOf(block.chainid, msg.sender);
120
+ declaredSignerOf[estateId] = signer;
121
+ emit EstateSignerDeclared(estateId, msg.sender, signer);
122
+ }
123
+
124
+ /// @notice The one read a verifier makes: the roots `signer` anchored for `estateId`, with the sequence
125
+ /// and when. Zeros when that signer anchored nothing. The verifier names the signer from the
126
+ /// estate's own declaration and decides what absence means.
127
+ function roots(bytes32 estateId, address signer) external view returns (bytes32 liveGrantRoot, bytes32 membershipRoot, bytes32 charterHash, uint64 sequence, uint64 anchoredAt) {
128
+ Roots storage r = _roots[estateId][signer];
81
129
  return (r.liveGrantRoot, r.membershipRoot, r.charterHash, r.sequence, r.anchoredAt);
82
130
  }
83
131
 
132
+ /// @notice Has `signer` retired its row for `estateId`? A retired row keeps its last roots readable
133
+ /// (history), but a verifier MUST NOT accept a presentation against it.
134
+ function isRetired(bytes32 estateId, address signer) external view returns (bool) {
135
+ return successorOf[estateId][signer] != address(0);
136
+ }
137
+
84
138
  /// @notice keccak256("eip155:<chainId>:<governance>") — the same derivation the SDK's `estateIdOf` uses.
85
- function estateIdOf(uint256 chainId, address governance) external pure returns (bytes32) {
139
+ function estateIdOf(uint256 chainId, address governance) public pure returns (bytes32) {
86
140
  return keccak256(abi.encodePacked("eip155:", _utoa(chainId), ":", _lowerHex(governance)));
87
141
  }
88
142
 
@@ -97,6 +97,25 @@ contract AgentRelationship {
97
97
  error RoleNotFound();
98
98
  error NotAuthorized();
99
99
  error InvalidTransition();
100
+ /// @notice REG-9 — more than `MAX_ROLES` roles on one edge.
101
+ error TooManyRoles();
102
+ /// @notice REG-9 — the caller already proposed this role; the COUNTERPARTY must call `addRole` to accept it.
103
+ error RoleAwaitingCounterparty();
104
+
105
+ /// @notice REG-9 — a role proposed on a confirmed edge; it joins the edge when the counterparty also
106
+ /// calls `addRole(edgeId, role)`.
107
+ event RoleProposed(bytes32 indexed edgeId, bytes32 indexed role, address indexed proposer);
108
+ event RoleProposalWithdrawn(bytes32 indexed edgeId, bytes32 indexed role, address indexed proposer);
109
+ /// @notice REG-9 — a REVOKED triple was proposed again (a new life for the same edge id).
110
+ event EdgeReproposed(bytes32 indexed edgeId, uint32 generation);
111
+
112
+ /// @notice REG-9 — the role bag is bounded (a re-proposal clears it in one loop).
113
+ uint256 public constant MAX_ROLES = 32;
114
+
115
+ /// @dev edgeId → how many times the triple has been re-proposed after a revocation.
116
+ mapping(bytes32 => uint32) private _generation;
117
+ /// @dev edgeId → generation → role → who proposed it on a confirmed edge (zero = no open proposal).
118
+ mapping(bytes32 => mapping(uint32 => mapping(bytes32 => address))) private _roleProposedBy;
100
119
 
101
120
  // ─── Edge ID ────────────────────────────────────────────────────
102
121
 
@@ -138,7 +157,23 @@ contract AgentRelationship {
138
157
  if (subject == object_) revert InvalidEdge();
139
158
  if (msg.sender != subject) revert NotAuthorized();
140
159
  edgeId = computeEdgeId(subject, object_, relationshipType);
141
- if (_edges[edgeId].createdAt != 0) revert EdgeAlreadyExists();
160
+ bool reproposal = false;
161
+ if (_edges[edgeId].createdAt != 0) {
162
+ // REG-9: REVOKED was terminal FOR EVER, so two agents who ended a relationship could never
163
+ // state it again. A revoked triple may be proposed anew by its subject; it starts over at
164
+ // PROPOSED with an empty role bag, and the object must confirm again. Any other status is live.
165
+ if (_edges[edgeId].status != EdgeStatus.REVOKED) revert EdgeAlreadyExists();
166
+ reproposal = true;
167
+ bytes32[] storage old = _roles[edgeId];
168
+ for (uint256 i = old.length; i > 0; i--) {
169
+ _hasRole[edgeId][old[i - 1]] = false;
170
+ old.pop();
171
+ }
172
+ uint32 g = _generation[edgeId] + 1;
173
+ _generation[edgeId] = g; // open role proposals of the previous life are unreachable
174
+ emit EdgeReproposed(edgeId, g);
175
+ }
176
+ if (initialRoles.length > MAX_ROLES) revert TooManyRoles();
142
177
 
143
178
  _edges[edgeId] = Edge({
144
179
  edgeId: edgeId,
@@ -153,11 +188,14 @@ contract AgentRelationship {
153
188
  metadataHash: metadataHash
154
189
  });
155
190
 
156
- _edgesBySubject[subject].push(edgeId);
157
- _edgesByObject[object_].push(edgeId);
158
- _byTriple[subject][object_][relationshipType] = edgeId;
191
+ if (!reproposal) {
192
+ _edgesBySubject[subject].push(edgeId);
193
+ _edgesByObject[object_].push(edgeId);
194
+ _byTriple[subject][object_][relationshipType] = edgeId;
195
+ }
159
196
 
160
197
  for (uint256 i = 0; i < initialRoles.length; i++) {
198
+ if (_hasRole[edgeId][initialRoles[i]]) revert RoleAlreadyExists();
161
199
  _addRole(edgeId, initialRoles[i]);
162
200
  emit RoleAdded(edgeId, initialRoles[i], msg.sender);
163
201
  }
@@ -214,35 +252,78 @@ contract AgentRelationship {
214
252
  // ─── Roles ──────────────────────────────────────────────────────
215
253
 
216
254
  /**
217
- * @notice Add a role to an existing edge. Either side may add
218
- * roles to the edge's role bag.
255
+ * @notice Add a role to an edge (spec 445 / REG-9).
256
+ *
257
+ * - PROPOSED: the proposal is the subject's, so only the SUBJECT adds — the object's levers are
258
+ * confirm and revoke, and what it confirms is what the subject wrote.
259
+ * - CONFIRMED / ACTIVE: the edge says "both agreed", so a new role needs BOTH. The first party's
260
+ * call records a proposal (`RoleProposed`); the role joins the edge when the counterparty makes
261
+ * the same call. Previously either party could add `TREASURER` to an edge the other had only
262
+ * confirmed as `MEMBER`, and the edge still read as confirmed.
263
+ * - REVOKED: nothing changes on a revoked edge.
219
264
  */
220
265
  function addRole(bytes32 edgeId, bytes32 role) external {
221
266
  Edge storage e = _edges[edgeId];
222
267
  if (e.createdAt == 0) revert EdgeNotFound();
223
268
  if (msg.sender != e.subject && msg.sender != e.object_) revert NotAuthorized();
269
+ if (e.status == EdgeStatus.REVOKED) revert InvalidTransition();
224
270
  if (_hasRole[edgeId][role]) revert RoleAlreadyExists();
271
+ if (e.status == EdgeStatus.PROPOSED) {
272
+ if (msg.sender != e.subject) revert NotAuthorized();
273
+ } else {
274
+ uint32 g = _generation[edgeId];
275
+ address proposer = _roleProposedBy[edgeId][g][role];
276
+ if (proposer == address(0)) {
277
+ _roleProposedBy[edgeId][g][role] = msg.sender;
278
+ emit RoleProposed(edgeId, role, msg.sender);
279
+ return;
280
+ }
281
+ if (proposer == msg.sender) revert RoleAwaitingCounterparty();
282
+ delete _roleProposedBy[edgeId][g][role];
283
+ }
284
+ if (_roles[edgeId].length >= MAX_ROLES) revert TooManyRoles();
225
285
  _addRole(edgeId, role);
226
286
  e.updatedAt = uint64(block.timestamp);
227
287
  emit RoleAdded(edgeId, role, msg.sender);
228
288
  }
229
289
 
290
+ /**
291
+ * @notice Remove a role — a reduction, so either party may do it alone on a live edge (either may
292
+ * revoke the whole edge alone). Also withdraws the caller's own open role proposal.
293
+ */
230
294
  function removeRole(bytes32 edgeId, bytes32 role) external {
231
295
  Edge storage e = _edges[edgeId];
232
296
  if (e.createdAt == 0) revert EdgeNotFound();
233
297
  if (msg.sender != e.subject && msg.sender != e.object_) revert NotAuthorized();
234
- if (!_hasRole[edgeId][role]) revert RoleNotFound();
298
+ if (e.status == EdgeStatus.REVOKED) revert InvalidTransition();
299
+ if (!_hasRole[edgeId][role]) {
300
+ uint32 g = _generation[edgeId];
301
+ if (_roleProposedBy[edgeId][g][role] != msg.sender) revert RoleNotFound();
302
+ delete _roleProposedBy[edgeId][g][role];
303
+ emit RoleProposalWithdrawn(edgeId, role, msg.sender);
304
+ return;
305
+ }
235
306
  _removeRole(edgeId, role);
236
307
  e.updatedAt = uint64(block.timestamp);
237
308
  emit RoleRemoved(edgeId, role, msg.sender);
238
309
  }
239
310
 
311
+ /// @notice Who proposed `role` on this (confirmed) edge and is waiting for the counterparty; zero if nobody.
312
+ function roleProposedBy(bytes32 edgeId, bytes32 role) external view returns (address) {
313
+ return _roleProposedBy[edgeId][_generation[edgeId]][role];
314
+ }
315
+
240
316
  // ─── Metadata ───────────────────────────────────────────────────
241
317
 
318
+ /// @dev While PROPOSED the metadata is the subject's proposal; afterwards either party may update it
319
+ /// (the event names the updater — metadata is descriptive, roles are what an edge asserts).
320
+ /// Nothing changes on a REVOKED edge.
242
321
  function setMetadata(bytes32 edgeId, string calldata metadataURI, bytes32 metadataHash) external {
243
322
  Edge storage e = _edges[edgeId];
244
323
  if (e.createdAt == 0) revert EdgeNotFound();
245
324
  if (msg.sender != e.subject && msg.sender != e.object_) revert NotAuthorized();
325
+ if (e.status == EdgeStatus.REVOKED) revert InvalidTransition();
326
+ if (e.status == EdgeStatus.PROPOSED && msg.sender != e.subject) revert NotAuthorized();
246
327
  e.metadataURI = metadataURI;
247
328
  e.metadataHash = metadataHash;
248
329
  e.updatedAt = uint64(block.timestamp);
@@ -268,6 +349,30 @@ contract AgentRelationship {
268
349
  function getEdgesByObject(address object_) external view returns (bytes32[] memory) {
269
350
  return _edgesByObject[object_];
270
351
  }
352
+ function edgeCountBySubject(address subject) external view returns (uint256) {
353
+ return _edgesBySubject[subject].length;
354
+ }
355
+ function edgeCountByObject(address object_) external view returns (uint256) {
356
+ return _edgesByObject[object_].length;
357
+ }
358
+ /// @notice REG-12 — a bounded window of a subject's edges. `getEdgesBySubject` is unbounded.
359
+ function getEdgesBySubjectPage(address subject, uint256 offset, uint256 limit) external view returns (bytes32[] memory) {
360
+ return _page(_edgesBySubject[subject], offset, limit);
361
+ }
362
+ /// @notice REG-12 — a bounded window of the edges naming `object_`. `getEdgesByObject` is unbounded and
363
+ /// grows by one for every UNSOLICITED proposal naming the object (anyone can extend it), so a
364
+ /// reader of a popular agent uses this.
365
+ function getEdgesByObjectPage(address object_, uint256 offset, uint256 limit) external view returns (bytes32[] memory) {
366
+ return _page(_edgesByObject[object_], offset, limit);
367
+ }
368
+ function _page(bytes32[] storage all, uint256 offset, uint256 limit) private view returns (bytes32[] memory page) {
369
+ uint256 n = all.length;
370
+ if (offset >= n) return new bytes32[](0);
371
+ uint256 end = offset + limit;
372
+ if (end > n) end = n;
373
+ page = new bytes32[](end - offset);
374
+ for (uint256 i = offset; i < end; i++) page[i - offset] = all[i];
375
+ }
271
376
  function getEdgeByTriple(address subject, address object_, bytes32 relationshipType) external view returns (bytes32) {
272
377
  return _byTriple[subject][object_][relationshipType];
273
378
  }
@@ -60,6 +60,34 @@ contract RelationshipTypeRegistry {
60
60
  if (newGovernor == address(0)) revert ZeroGovernor();
61
61
  emit GovernorTransferred(governor, newGovernor);
62
62
  governor = newGovernor;
63
+ pendingGovernor = address(0); // a proposal made by the previous governor does not survive it
64
+ }
65
+
66
+ // ─── Two-step governor hand-over (spec 445 / REG-11) ────────────
67
+ //
68
+ // `transferGovernor` is one step: a wrong address is permanent. It stays for the scripted bootstrap
69
+ // hand-over, where the deploy script asserts the end state in the same run (spec 445 §2.1). Every
70
+ // LATER rotation should use this pair — the successor proves it can act before the role moves.
71
+
72
+ /// @notice The successor named by `proposeGovernor`, until it accepts. Zero otherwise.
73
+ address public pendingGovernor;
74
+
75
+ event GovernorProposed(address indexed governor, address indexed pendingGovernor);
76
+
77
+ error NotPendingGovernor();
78
+
79
+ /// @notice Name a successor (zero cancels). The governor keeps the role until the successor accepts.
80
+ function proposeGovernor(address newGovernor) external onlyGovernor {
81
+ pendingGovernor = newGovernor;
82
+ emit GovernorProposed(governor, newGovernor);
83
+ }
84
+
85
+ /// @notice Take the governor role. Only the proposed successor.
86
+ function acceptGovernor() external {
87
+ if (msg.sender != pendingGovernor || msg.sender == address(0)) revert NotPendingGovernor();
88
+ emit GovernorTransferred(governor, msg.sender);
89
+ governor = msg.sender;
90
+ pendingGovernor = address(0);
63
91
  }
64
92
 
65
93
  // ─── Registration ───────────────────────────────────────────────
@@ -52,6 +52,8 @@ contract SkillDefinitionRegistry {
52
52
  error SkillNotFound();
53
53
  error EmptyMetadata();
54
54
  error EmptyId();
55
+ /// @notice REG-15 — `setValidity` may only end a version's window earlier.
56
+ error ValidityNotNarrowed();
55
57
 
56
58
  // ─── Storage ────────────────────────────────────────────────────────
57
59
  struct SkillRecord {
@@ -158,7 +160,12 @@ contract SkillDefinitionRegistry {
158
160
  SkillRecord storage r = _records[skillId][version];
159
161
  if (r.version == 0) revert SkillNotFound();
160
162
  if (msg.sender != r.stewardAccount) revert NotSteward();
161
- r.validAfter = validAfter;
163
+ // REG-15: a published version's window can be ENDED, never rewritten. `validAfter` is fixed at
164
+ // publish; `validUntil` may be set (0 → a time) or moved earlier, not extended or cleared — a
165
+ // credential that pinned this version must not find its window widened after the fact. To extend,
166
+ // publish a new version.
167
+ if (validAfter != r.validAfter) revert ValidityNotNarrowed();
168
+ if (validUntil == 0 || (r.validUntil != 0 && validUntil > r.validUntil)) revert ValidityNotNarrowed();
162
169
  r.validUntil = validUntil;
163
170
  emit SkillValidityChanged(skillId, version, validAfter, validUntil);
164
171
  }
@@ -166,6 +173,8 @@ contract SkillDefinitionRegistry {
166
173
  // ─── Views ──────────────────────────────────────────────────────────
167
174
  function getSkill(bytes32 skillId, uint64 version) external view returns (SkillRecord memory) {
168
175
  SkillRecord memory r = _records[skillId][version];
176
+ // Zero-default existence check (versions are 1-based) — benign.
177
+ // slither-disable-next-line incorrect-equality
169
178
  if (r.version == 0) revert SkillNotFound();
170
179
  return r;
171
180
  }
@@ -184,4 +193,19 @@ contract SkillDefinitionRegistry {
184
193
  function allSkills() external view returns (bytes32[] memory) {
185
194
  return _allSkills;
186
195
  }
196
+
197
+ function skillCount() external view returns (uint256) {
198
+ return _allSkills.length;
199
+ }
200
+
201
+ /// @notice REG-12 — a bounded window of the id index: `limit` ids from `offset` (fewer at the end).
202
+ /// `allSkills()` is unbounded (anyone may publish a new id) and is kept for small deployments.
203
+ function allSkillsPage(uint256 offset, uint256 limit) external view returns (bytes32[] memory page) {
204
+ uint256 n = _allSkills.length;
205
+ if (offset >= n) return new bytes32[](0);
206
+ uint256 end = offset + limit;
207
+ if (end > n) end = n;
208
+ page = new bytes32[](end - offset);
209
+ for (uint256 i = offset; i < end; i++) page[i - offset] = _allSkills[i];
210
+ }
187
211
  }