trustdex 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.ko.md +65 -5
  2. package/README.md +67 -5
  3. package/bin/trustdex.mjs +182 -3
  4. package/docs/ARCHITECTURE.ko.md +17 -1
  5. package/docs/ARCHITECTURE.md +17 -1
  6. package/docs/CLAUDE.ko.md +33 -1
  7. package/docs/CLAUDE.md +32 -1
  8. package/docs/CODEX.ko.md +34 -4
  9. package/docs/CODEX.md +35 -4
  10. package/docs/DEFENSIVE_USE_CASE.ko.md +12 -0
  11. package/docs/DEFENSIVE_USE_CASE.md +13 -0
  12. package/docs/EVALUATION.ko.md +35 -0
  13. package/docs/EVALUATION.md +35 -0
  14. package/docs/ROADMAP.ko.md +9 -5
  15. package/docs/ROADMAP.md +9 -5
  16. package/docs/THREAT_MODEL.ko.md +8 -0
  17. package/docs/THREAT_MODEL.md +8 -0
  18. package/examples/adversarial/README.ko.md +2 -0
  19. package/examples/adversarial/README.md +2 -0
  20. package/examples/adversarial/bindings/README.ko.md +32 -0
  21. package/examples/adversarial/bindings/README.md +33 -0
  22. package/examples/adversarial/bindings/claude.mcp.json +12 -0
  23. package/examples/adversarial/bindings/claude.settings.json +9 -0
  24. package/examples/adversarial/bindings/codex.toml +9 -0
  25. package/examples/adversarial/bindings/expected.json +27 -0
  26. package/examples/adversarial/bindings/runtime-after.json +21 -0
  27. package/examples/adversarial/bindings/runtime-before.json +13 -0
  28. package/examples/adversarial/bindings/trust-store.json +15 -0
  29. package/package.json +3 -3
  30. package/schemas/effective-runtime-tools-diff-v1.schema.json +50 -0
  31. package/schemas/effective-runtime-tools-v1.schema.json +68 -0
  32. package/schemas/runtime-tool-inventory-v1.schema.json +27 -0
  33. package/schemas/tool-permission-bindings-diff-v1.schema.json +70 -0
  34. package/schemas/tool-permission-bindings-v1.schema.json +94 -0
  35. package/src/bindings.mjs +358 -0
  36. package/src/claude-settings.mjs +49 -0
  37. package/src/runtime-evidence.mjs +318 -0
@@ -0,0 +1,50 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/grigent/trustdex/blob/main/schemas/effective-runtime-tools-diff-v1.schema.json",
4
+ "title": "TrustDex effective runtime tool boundary diff v1",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": ["schemaVersion", "runtime", "status", "baselineDigest", "currentDigest", "summary", "changes"],
8
+ "properties": {
9
+ "schemaVersion": { "const": 1 },
10
+ "runtime": { "enum": ["claude", "codex"] },
11
+ "status": { "enum": ["unchanged", "restricted", "needs-review"] },
12
+ "baselineDigest": { "$ref": "#/$defs/digest" },
13
+ "currentDigest": { "$ref": "#/$defs/digest" },
14
+ "summary": {
15
+ "type": "object",
16
+ "additionalProperties": false,
17
+ "required": ["expansions", "restrictions", "reviews"],
18
+ "properties": {
19
+ "expansions": { "$ref": "#/$defs/count" },
20
+ "restrictions": { "$ref": "#/$defs/count" },
21
+ "reviews": { "$ref": "#/$defs/count" }
22
+ }
23
+ },
24
+ "changes": {
25
+ "type": "array",
26
+ "items": {
27
+ "type": "object",
28
+ "additionalProperties": false,
29
+ "required": ["impact", "type", "name", "field", "before", "after"],
30
+ "properties": {
31
+ "impact": { "enum": ["expansion", "restriction", "review"] },
32
+ "type": {
33
+ "enum": [
34
+ "bindings-changed", "capture-method-changed", "server-added", "server-removed",
35
+ "observation-changed", "tool-count-changed", "binding-state-changed", "server-status-changed"
36
+ ]
37
+ },
38
+ "name": { "oneOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }] },
39
+ "field": { "type": "string", "minLength": 1 },
40
+ "before": {},
41
+ "after": {}
42
+ }
43
+ }
44
+ }
45
+ },
46
+ "$defs": {
47
+ "digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
48
+ "count": { "type": "integer", "minimum": 0 }
49
+ }
50
+ }
@@ -0,0 +1,68 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/grigent/trustdex/blob/main/schemas/effective-runtime-tools-v1.schema.json",
4
+ "title": "TrustDex effective runtime tool boundary v1",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "schemaVersion", "kind", "createdAt", "runtime", "capturedAt", "captureMethod",
9
+ "bindingDigest", "inventoryDigest", "status", "summary", "servers", "digest"
10
+ ],
11
+ "properties": {
12
+ "schemaVersion": { "const": 1 },
13
+ "kind": { "const": "agent-effective-tool-boundary" },
14
+ "createdAt": { "type": "string", "format": "date-time" },
15
+ "runtime": { "enum": ["claude", "codex"] },
16
+ "capturedAt": { "type": "string", "format": "date-time" },
17
+ "captureMethod": { "enum": ["runtime-export", "runtime-adapter"] },
18
+ "bindingDigest": { "$ref": "#/$defs/digest" },
19
+ "inventoryDigest": { "$ref": "#/$defs/digest" },
20
+ "status": { "enum": ["matched", "restricted", "needs-review", "block"] },
21
+ "summary": {
22
+ "type": "object",
23
+ "additionalProperties": false,
24
+ "required": [
25
+ "configuredServerCount", "observedServerCount", "observedToolCount", "matchedCount",
26
+ "reviewCount", "blockedCount", "unexpectedCount", "notObservedCount"
27
+ ],
28
+ "properties": {
29
+ "configuredServerCount": { "$ref": "#/$defs/count" },
30
+ "observedServerCount": { "$ref": "#/$defs/count" },
31
+ "observedToolCount": { "$ref": "#/$defs/count" },
32
+ "matchedCount": { "$ref": "#/$defs/count" },
33
+ "reviewCount": { "$ref": "#/$defs/count" },
34
+ "blockedCount": { "$ref": "#/$defs/count" },
35
+ "unexpectedCount": { "$ref": "#/$defs/count" },
36
+ "notObservedCount": { "$ref": "#/$defs/count" }
37
+ }
38
+ },
39
+ "servers": {
40
+ "type": "array",
41
+ "items": { "$ref": "#/$defs/server" }
42
+ },
43
+ "digest": { "$ref": "#/$defs/digest" }
44
+ },
45
+ "$defs": {
46
+ "digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
47
+ "count": { "type": "integer", "minimum": 0 },
48
+ "server": {
49
+ "type": "object",
50
+ "additionalProperties": false,
51
+ "required": [
52
+ "name", "configured", "observed", "toolCount", "sourceDecision", "provenanceStatus",
53
+ "automaticGrant", "bindingStatus", "status"
54
+ ],
55
+ "properties": {
56
+ "name": { "type": "string", "minLength": 1 },
57
+ "configured": { "type": "boolean" },
58
+ "observed": { "type": "boolean" },
59
+ "toolCount": { "oneOf": [{ "$ref": "#/$defs/count" }, { "type": "null" }] },
60
+ "sourceDecision": { "oneOf": [{ "enum": ["allow", "ask", "block"] }, { "type": "null" }] },
61
+ "provenanceStatus": { "oneOf": [{ "enum": ["unknown", "verified", "mismatch"] }, { "type": "null" }] },
62
+ "automaticGrant": { "oneOf": [{ "enum": ["none", "scoped", "all"] }, { "type": "null" }] },
63
+ "bindingStatus": { "oneOf": [{ "enum": ["not-granted", "bound", "needs-review", "blocked"] }, { "type": "null" }] },
64
+ "status": { "enum": ["matched", "not-observed", "needs-review", "blocked", "unexpected"] }
65
+ }
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/grigent/trustdex/blob/main/schemas/runtime-tool-inventory-v1.schema.json",
4
+ "title": "TrustDex value-free runtime tool inventory v1",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": ["schemaVersion", "kind", "runtime", "capturedAt", "captureMethod", "servers"],
8
+ "properties": {
9
+ "schemaVersion": { "const": 1 },
10
+ "kind": { "const": "agent-runtime-tool-inventory" },
11
+ "runtime": { "enum": ["claude", "codex"] },
12
+ "capturedAt": { "type": "string", "format": "date-time" },
13
+ "captureMethod": { "enum": ["runtime-export", "runtime-adapter"] },
14
+ "servers": {
15
+ "type": "array",
16
+ "items": {
17
+ "type": "object",
18
+ "additionalProperties": false,
19
+ "required": ["name", "toolCount"],
20
+ "properties": {
21
+ "name": { "type": "string", "minLength": 1 },
22
+ "toolCount": { "type": "integer", "minimum": 0 }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,70 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/grigent/trustdex/blob/main/schemas/tool-permission-bindings-diff-v1.schema.json",
4
+ "title": "TrustDex tool provenance and permission binding diff v1",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "schemaVersion",
9
+ "runtime",
10
+ "status",
11
+ "baselineDigest",
12
+ "currentDigest",
13
+ "summary",
14
+ "changes"
15
+ ],
16
+ "properties": {
17
+ "schemaVersion": { "const": 1 },
18
+ "runtime": { "enum": ["claude", "codex"] },
19
+ "status": { "enum": ["unchanged", "restricted", "needs-review"] },
20
+ "baselineDigest": { "$ref": "tool-permission-bindings-v1.schema.json#/$defs/digest" },
21
+ "currentDigest": { "$ref": "tool-permission-bindings-v1.schema.json#/$defs/digest" },
22
+ "summary": {
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "required": ["expansions", "restrictions", "reviews"],
26
+ "properties": {
27
+ "expansions": { "$ref": "tool-permission-bindings-v1.schema.json#/$defs/count" },
28
+ "restrictions": { "$ref": "tool-permission-bindings-v1.schema.json#/$defs/count" },
29
+ "reviews": { "$ref": "tool-permission-bindings-v1.schema.json#/$defs/count" }
30
+ }
31
+ },
32
+ "changes": {
33
+ "type": "array",
34
+ "items": { "$ref": "#/$defs/change" }
35
+ }
36
+ },
37
+ "$defs": {
38
+ "value": { "type": ["string", "boolean", "number", "null"] },
39
+ "change": {
40
+ "type": "object",
41
+ "additionalProperties": false,
42
+ "required": ["impact", "type", "name", "field", "before", "after"],
43
+ "properties": {
44
+ "impact": { "enum": ["expansion", "restriction", "review"] },
45
+ "type": {
46
+ "enum": [
47
+ "policy-changed",
48
+ "trust-store-changed",
49
+ "count-changed",
50
+ "binding-added",
51
+ "binding-removed",
52
+ "source-decision-changed",
53
+ "provenance-changed",
54
+ "grant-changed",
55
+ "wildcard-changed"
56
+ ]
57
+ },
58
+ "name": {
59
+ "oneOf": [
60
+ { "type": "string", "minLength": 1 },
61
+ { "type": "null" }
62
+ ]
63
+ },
64
+ "field": { "type": "string", "minLength": 1 },
65
+ "before": { "$ref": "#/$defs/value" },
66
+ "after": { "$ref": "#/$defs/value" }
67
+ }
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,94 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/grigent/trustdex/blob/main/schemas/tool-permission-bindings-v1.schema.json",
4
+ "title": "TrustDex tool provenance and permission bindings v1",
5
+ "type": "object",
6
+ "additionalProperties": false,
7
+ "required": [
8
+ "schemaVersion",
9
+ "kind",
10
+ "createdAt",
11
+ "runtime",
12
+ "policyDigest",
13
+ "trustStoreDigest",
14
+ "status",
15
+ "summary",
16
+ "bindings",
17
+ "digest"
18
+ ],
19
+ "properties": {
20
+ "schemaVersion": { "const": 1 },
21
+ "kind": { "const": "agent-tool-permission-bindings" },
22
+ "createdAt": { "type": "string", "format": "date-time" },
23
+ "runtime": { "enum": ["claude", "codex"] },
24
+ "policyDigest": { "$ref": "#/$defs/digest" },
25
+ "trustStoreDigest": {
26
+ "oneOf": [
27
+ { "$ref": "#/$defs/digest" },
28
+ { "type": "null" }
29
+ ]
30
+ },
31
+ "status": { "enum": ["clear", "bound", "needs-review", "block"] },
32
+ "summary": {
33
+ "type": "object",
34
+ "additionalProperties": false,
35
+ "required": [
36
+ "serverCount",
37
+ "automaticGrantCount",
38
+ "boundCount",
39
+ "reviewCount",
40
+ "blockedCount",
41
+ "unboundAutomaticGrantCount",
42
+ "ambiguousAutomaticGrantCount"
43
+ ],
44
+ "properties": {
45
+ "serverCount": { "$ref": "#/$defs/count" },
46
+ "automaticGrantCount": { "$ref": "#/$defs/count" },
47
+ "boundCount": { "$ref": "#/$defs/count" },
48
+ "reviewCount": { "$ref": "#/$defs/count" },
49
+ "blockedCount": { "$ref": "#/$defs/count" },
50
+ "unboundAutomaticGrantCount": { "$ref": "#/$defs/count" },
51
+ "ambiguousAutomaticGrantCount": { "$ref": "#/$defs/count" }
52
+ }
53
+ },
54
+ "bindings": {
55
+ "type": "array",
56
+ "items": { "$ref": "#/$defs/binding" }
57
+ },
58
+ "digest": { "$ref": "#/$defs/digest" }
59
+ },
60
+ "$defs": {
61
+ "digest": {
62
+ "type": "string",
63
+ "pattern": "^[a-f0-9]{64}$"
64
+ },
65
+ "count": {
66
+ "type": "integer",
67
+ "minimum": 0
68
+ },
69
+ "binding": {
70
+ "type": "object",
71
+ "additionalProperties": false,
72
+ "required": [
73
+ "name",
74
+ "sourceDecision",
75
+ "provenanceStatus",
76
+ "integrityVerified",
77
+ "automaticGrant",
78
+ "grantRuleCount",
79
+ "toolScopeWildcard",
80
+ "status"
81
+ ],
82
+ "properties": {
83
+ "name": { "type": "string", "minLength": 1 },
84
+ "sourceDecision": { "enum": ["allow", "ask", "block"] },
85
+ "provenanceStatus": { "enum": ["unknown", "verified", "mismatch"] },
86
+ "integrityVerified": { "type": "boolean" },
87
+ "automaticGrant": { "enum": ["none", "scoped", "all"] },
88
+ "grantRuleCount": { "$ref": "#/$defs/count" },
89
+ "toolScopeWildcard": { "type": "boolean" },
90
+ "status": { "enum": ["not-granted", "bound", "needs-review", "blocked"] }
91
+ }
92
+ }
93
+ }
94
+ }
@@ -0,0 +1,358 @@
1
+ import { sha256Json } from './canonical.mjs';
2
+
3
+ const ACTIONS = new Set(['allow', 'ask', 'block']);
4
+ const GRANTS = new Set(['none', 'scoped', 'all']);
5
+ const PROVENANCE = new Set(['unknown', 'verified', 'mismatch']);
6
+ const BINDING_STATUSES = new Set(['not-granted', 'bound', 'needs-review', 'blocked']);
7
+ const RUNTIMES = new Set(['claude', 'codex']);
8
+ const SNAPSHOT_KIND = 'agent-tool-permission-bindings';
9
+ const SNAPSHOT_VERSION = 1;
10
+
11
+ function requireObject(value, message) {
12
+ if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error(message);
13
+ return value;
14
+ }
15
+
16
+ function requireExactKeys(value, expected, message) {
17
+ const keys = Object.keys(requireObject(value, message));
18
+ if (keys.length !== expected.size || keys.some((key) => !expected.has(key))) {
19
+ throw new Error(message);
20
+ }
21
+ return value;
22
+ }
23
+
24
+ function requireCount(value, field) {
25
+ if (!Number.isSafeInteger(value) || value < 0) throw new Error(`Invalid binding count: ${field}`);
26
+ return value;
27
+ }
28
+
29
+ function requireDigest(value, field, nullable = false) {
30
+ if (nullable && value === null) return null;
31
+ if (typeof value !== 'string' || !/^[a-f0-9]{64}$/.test(value)) {
32
+ throw new Error(`Invalid binding digest: ${field}`);
33
+ }
34
+ return value;
35
+ }
36
+
37
+ function normalizeGrant(grant) {
38
+ requireObject(grant, 'Invalid tool permission grant.');
39
+ if (typeof grant.name !== 'string' || !grant.name) throw new Error('Tool permission grant is missing a name.');
40
+ if (!GRANTS.has(grant.automaticGrant)) throw new Error('Unsupported automatic tool grant mode.');
41
+ if (typeof grant.toolScopeWildcard !== 'boolean') throw new Error('Invalid tool permission wildcard state.');
42
+ return {
43
+ name: grant.name,
44
+ automaticGrant: grant.automaticGrant,
45
+ grantRuleCount: requireCount(grant.grantRuleCount, 'grantRuleCount'),
46
+ toolScopeWildcard: grant.toolScopeWildcard
47
+ };
48
+ }
49
+
50
+ function bindingStatus(sourceDecision, provenanceStatus, grant) {
51
+ if (grant.automaticGrant === 'none') return 'not-granted';
52
+ if (sourceDecision === 'block' || provenanceStatus === 'mismatch') return 'blocked';
53
+ if (sourceDecision === 'ask' || grant.automaticGrant === 'all' || grant.toolScopeWildcard) {
54
+ return 'needs-review';
55
+ }
56
+ return 'bound';
57
+ }
58
+
59
+ function snapshotDigest(snapshot) {
60
+ return sha256Json({
61
+ schemaVersion: snapshot.schemaVersion,
62
+ kind: snapshot.kind,
63
+ runtime: snapshot.runtime,
64
+ policyDigest: snapshot.policyDigest,
65
+ trustStoreDigest: snapshot.trustStoreDigest,
66
+ summary: snapshot.summary,
67
+ bindings: snapshot.bindings
68
+ });
69
+ }
70
+
71
+ export function createToolPermissionBindingSnapshot({
72
+ runtime,
73
+ results,
74
+ grants,
75
+ policy,
76
+ trustStore = null,
77
+ unboundAutomaticGrantCount = 0,
78
+ ambiguousAutomaticGrantCount = 0
79
+ }) {
80
+ if (!RUNTIMES.has(runtime)) throw new Error('Unsupported tool permission binding runtime.');
81
+ if (!Array.isArray(results) || !Array.isArray(grants)) {
82
+ throw new Error('Tool permission bindings require result and grant arrays.');
83
+ }
84
+
85
+ const resultMap = new Map();
86
+ for (const result of results) {
87
+ if (!result || typeof result.name !== 'string' || resultMap.has(result.name)) {
88
+ throw new Error('Tool permission binding results require unique names.');
89
+ }
90
+ if (!ACTIONS.has(result.action)) throw new Error('Tool permission binding result has an invalid action.');
91
+ resultMap.set(result.name, result);
92
+ }
93
+
94
+ const grantMap = new Map();
95
+ for (const input of grants) {
96
+ const grant = normalizeGrant(input);
97
+ if (grantMap.has(grant.name)) throw new Error('Tool permission grants require unique names.');
98
+ grantMap.set(grant.name, grant);
99
+ }
100
+ if (resultMap.size !== grantMap.size || [...resultMap.keys()].some((name) => !grantMap.has(name))) {
101
+ throw new Error('Tool permission grants do not match inspected MCP servers.');
102
+ }
103
+
104
+ const bindings = [...resultMap.entries()].map(([name, result]) => {
105
+ const grant = grantMap.get(name);
106
+ const provenanceStatus = result.provenance?.status || 'unknown';
107
+ if (!PROVENANCE.has(provenanceStatus)) throw new Error('Unsupported tool provenance status.');
108
+ return {
109
+ name,
110
+ sourceDecision: result.action,
111
+ provenanceStatus,
112
+ integrityVerified: result.provenance?.integrityVerified === true,
113
+ automaticGrant: grant.automaticGrant,
114
+ grantRuleCount: grant.grantRuleCount,
115
+ toolScopeWildcard: grant.toolScopeWildcard,
116
+ status: bindingStatus(result.action, provenanceStatus, grant)
117
+ };
118
+ }).sort((left, right) => left.name.localeCompare(right.name));
119
+
120
+ const unbound = requireCount(unboundAutomaticGrantCount, 'unboundAutomaticGrantCount');
121
+ const ambiguous = requireCount(ambiguousAutomaticGrantCount, 'ambiguousAutomaticGrantCount');
122
+ const summary = {
123
+ serverCount: bindings.length,
124
+ automaticGrantCount: bindings.filter((binding) => binding.automaticGrant !== 'none').length,
125
+ boundCount: bindings.filter((binding) => binding.status === 'bound').length,
126
+ reviewCount: bindings.filter((binding) => binding.status === 'needs-review').length,
127
+ blockedCount: bindings.filter((binding) => binding.status === 'blocked').length,
128
+ unboundAutomaticGrantCount: unbound,
129
+ ambiguousAutomaticGrantCount: ambiguous
130
+ };
131
+ const status = summary.blockedCount
132
+ ? 'block'
133
+ : summary.reviewCount || unbound || ambiguous
134
+ ? 'needs-review'
135
+ : summary.boundCount ? 'bound' : 'clear';
136
+ const snapshot = {
137
+ schemaVersion: SNAPSHOT_VERSION,
138
+ kind: SNAPSHOT_KIND,
139
+ createdAt: new Date().toISOString(),
140
+ runtime,
141
+ policyDigest: sha256Json(policy || {}),
142
+ trustStoreDigest: trustStore ? sha256Json(trustStore) : null,
143
+ status,
144
+ summary,
145
+ bindings
146
+ };
147
+ return { ...snapshot, digest: snapshotDigest(snapshot) };
148
+ }
149
+
150
+ function validateBinding(binding) {
151
+ requireExactKeys(binding, new Set([
152
+ 'name', 'sourceDecision', 'provenanceStatus', 'integrityVerified', 'automaticGrant',
153
+ 'grantRuleCount', 'toolScopeWildcard', 'status'
154
+ ]), 'Invalid tool permission binding.');
155
+ if (typeof binding.name !== 'string' || !binding.name) throw new Error('Tool permission binding is missing a name.');
156
+ if (!ACTIONS.has(binding.sourceDecision)) throw new Error('Invalid binding source decision.');
157
+ if (!PROVENANCE.has(binding.provenanceStatus)) throw new Error('Invalid binding provenance status.');
158
+ if (typeof binding.integrityVerified !== 'boolean') throw new Error('Invalid binding integrity state.');
159
+ if (!GRANTS.has(binding.automaticGrant)) throw new Error('Invalid binding grant mode.');
160
+ requireCount(binding.grantRuleCount, 'grantRuleCount');
161
+ if (typeof binding.toolScopeWildcard !== 'boolean') throw new Error('Invalid binding wildcard state.');
162
+ if (!BINDING_STATUSES.has(binding.status)) throw new Error('Invalid tool permission binding status.');
163
+ if (binding.status !== bindingStatus(binding.sourceDecision, binding.provenanceStatus, binding)) {
164
+ throw new Error('Tool permission binding status is inconsistent.');
165
+ }
166
+ return binding;
167
+ }
168
+
169
+ function validateSnapshot(snapshot) {
170
+ requireExactKeys(snapshot, new Set([
171
+ 'schemaVersion', 'kind', 'createdAt', 'runtime', 'policyDigest', 'trustStoreDigest',
172
+ 'status', 'summary', 'bindings', 'digest'
173
+ ]), 'Invalid tool permission binding snapshot.');
174
+ if (snapshot.schemaVersion !== SNAPSHOT_VERSION || snapshot.kind !== SNAPSHOT_KIND) {
175
+ throw new Error('Unsupported tool permission binding snapshot schema.');
176
+ }
177
+ if (!RUNTIMES.has(snapshot.runtime)) throw new Error('Invalid tool permission binding runtime.');
178
+ if (typeof snapshot.createdAt !== 'string' ||
179
+ Number.isNaN(Date.parse(snapshot.createdAt)) ||
180
+ new Date(snapshot.createdAt).toISOString() !== snapshot.createdAt) {
181
+ throw new Error('Invalid tool permission binding creation time.');
182
+ }
183
+ requireDigest(snapshot.policyDigest, 'policyDigest');
184
+ requireDigest(snapshot.trustStoreDigest, 'trustStoreDigest', true);
185
+ requireDigest(snapshot.digest, 'digest');
186
+ if (!['clear', 'bound', 'needs-review', 'block'].includes(snapshot.status)) {
187
+ throw new Error('Invalid tool permission binding snapshot status.');
188
+ }
189
+ if (!Array.isArray(snapshot.bindings)) throw new Error('Tool permission binding snapshot is missing bindings.');
190
+ for (const binding of snapshot.bindings) validateBinding(binding);
191
+ const names = snapshot.bindings.map((binding) => binding.name);
192
+ if (new Set(names).size !== names.length || [...names].sort().join('\0') !== names.join('\0')) {
193
+ throw new Error('Tool permission bindings are not canonical.');
194
+ }
195
+ const summaryFields = [
196
+ 'serverCount', 'automaticGrantCount', 'boundCount', 'reviewCount', 'blockedCount',
197
+ 'unboundAutomaticGrantCount', 'ambiguousAutomaticGrantCount'
198
+ ];
199
+ const summary = requireExactKeys(
200
+ snapshot.summary,
201
+ new Set(summaryFields),
202
+ 'Tool permission binding snapshot has invalid summary fields.'
203
+ );
204
+ for (const field of summaryFields) requireCount(summary[field], field);
205
+ const expected = createSummary(snapshot.bindings, summary);
206
+ if (sha256Json(expected) !== sha256Json(summary)) throw new Error('Tool permission binding summary mismatch.');
207
+ const expectedStatus = expected.blockedCount
208
+ ? 'block'
209
+ : expected.reviewCount || expected.unboundAutomaticGrantCount || expected.ambiguousAutomaticGrantCount
210
+ ? 'needs-review'
211
+ : expected.boundCount ? 'bound' : 'clear';
212
+ if (snapshot.status !== expectedStatus) throw new Error('Tool permission binding snapshot status mismatch.');
213
+ if (snapshot.digest !== snapshotDigest(snapshot)) throw new Error('Tool permission binding snapshot digest mismatch.');
214
+ return snapshot;
215
+ }
216
+
217
+ export function validateToolPermissionBindingSnapshot(snapshot) {
218
+ return validateSnapshot(snapshot);
219
+ }
220
+
221
+ function createSummary(bindings, input) {
222
+ return {
223
+ serverCount: bindings.length,
224
+ automaticGrantCount: bindings.filter((binding) => binding.automaticGrant !== 'none').length,
225
+ boundCount: bindings.filter((binding) => binding.status === 'bound').length,
226
+ reviewCount: bindings.filter((binding) => binding.status === 'needs-review').length,
227
+ blockedCount: bindings.filter((binding) => binding.status === 'blocked').length,
228
+ unboundAutomaticGrantCount: input.unboundAutomaticGrantCount,
229
+ ambiguousAutomaticGrantCount: input.ambiguousAutomaticGrantCount
230
+ };
231
+ }
232
+
233
+ function addChange(changes, impact, type, name, field, before, after) {
234
+ changes.push({ impact, type, name, field, before, after });
235
+ }
236
+
237
+ export function diffToolPermissionBindingSnapshots(beforeInput, afterInput) {
238
+ const before = validateSnapshot(beforeInput);
239
+ const after = validateSnapshot(afterInput);
240
+ if (before.runtime !== after.runtime) throw new Error('Cannot compare different binding runtimes.');
241
+
242
+ const changes = [];
243
+ if (before.policyDigest !== after.policyDigest) {
244
+ addChange(changes, 'review', 'policy-changed', null, 'policyDigest', null, null);
245
+ }
246
+ if (before.trustStoreDigest !== after.trustStoreDigest) {
247
+ addChange(changes, 'review', 'trust-store-changed', null, 'trustStoreDigest', null, null);
248
+ }
249
+ for (const field of ['unboundAutomaticGrantCount', 'ambiguousAutomaticGrantCount']) {
250
+ const previous = before.summary[field];
251
+ const current = after.summary[field];
252
+ if (previous !== current) {
253
+ addChange(changes, current > previous ? 'expansion' : 'restriction', 'count-changed', null, field, previous, current);
254
+ }
255
+ }
256
+
257
+ const left = new Map(before.bindings.map((binding) => [binding.name, binding]));
258
+ const right = new Map(after.bindings.map((binding) => [binding.name, binding]));
259
+ const grantRank = { none: 0, scoped: 1, all: 2 };
260
+
261
+ for (const [name, current] of right) {
262
+ const previous = left.get(name);
263
+ if (!previous) {
264
+ addChange(
265
+ changes,
266
+ current.automaticGrant === 'none' ? 'review' : 'expansion',
267
+ 'binding-added',
268
+ name,
269
+ 'binding',
270
+ null,
271
+ current.status
272
+ );
273
+ continue;
274
+ }
275
+ if (previous.sourceDecision !== current.sourceDecision) {
276
+ addChange(changes, 'review', 'source-decision-changed', name, 'sourceDecision', previous.sourceDecision, current.sourceDecision);
277
+ }
278
+ for (const field of ['provenanceStatus', 'integrityVerified']) {
279
+ if (previous[field] !== current[field]) {
280
+ addChange(changes, 'review', 'provenance-changed', name, field, previous[field], current[field]);
281
+ }
282
+ }
283
+ if (previous.automaticGrant !== current.automaticGrant) {
284
+ addChange(
285
+ changes,
286
+ grantRank[current.automaticGrant] > grantRank[previous.automaticGrant] ? 'expansion' : 'restriction',
287
+ 'grant-changed',
288
+ name,
289
+ 'automaticGrant',
290
+ previous.automaticGrant,
291
+ current.automaticGrant
292
+ );
293
+ }
294
+ if (previous.grantRuleCount !== current.grantRuleCount) {
295
+ addChange(
296
+ changes,
297
+ current.grantRuleCount > previous.grantRuleCount ? 'expansion' : 'restriction',
298
+ 'count-changed',
299
+ name,
300
+ 'grantRuleCount',
301
+ previous.grantRuleCount,
302
+ current.grantRuleCount
303
+ );
304
+ }
305
+ if (previous.toolScopeWildcard !== current.toolScopeWildcard) {
306
+ addChange(
307
+ changes,
308
+ current.toolScopeWildcard ? 'expansion' : 'restriction',
309
+ 'wildcard-changed',
310
+ name,
311
+ 'toolScopeWildcard',
312
+ previous.toolScopeWildcard,
313
+ current.toolScopeWildcard
314
+ );
315
+ }
316
+ }
317
+ for (const [name, previous] of left) {
318
+ if (!right.has(name)) {
319
+ addChange(
320
+ changes,
321
+ previous.automaticGrant === 'none' ? 'review' : 'restriction',
322
+ 'binding-removed',
323
+ name,
324
+ 'binding',
325
+ previous.status,
326
+ null
327
+ );
328
+ }
329
+ }
330
+
331
+ const summary = {
332
+ expansions: changes.filter((change) => change.impact === 'expansion').length,
333
+ restrictions: changes.filter((change) => change.impact === 'restriction').length,
334
+ reviews: changes.filter((change) => change.impact === 'review').length
335
+ };
336
+ const status = summary.expansions || summary.reviews
337
+ ? 'needs-review'
338
+ : summary.restrictions ? 'restricted' : 'unchanged';
339
+ return {
340
+ schemaVersion: 1,
341
+ runtime: before.runtime,
342
+ status,
343
+ baselineDigest: before.digest,
344
+ currentDigest: after.digest,
345
+ summary,
346
+ changes
347
+ };
348
+ }
349
+
350
+ export function codexAutomaticGrantProjection(results) {
351
+ return results.map((result) => ({
352
+ name: result.name,
353
+ automaticGrant: result.signals?.includes('auto-approve-tools') ? 'all' : 'none',
354
+ grantRuleCount: result.signals?.includes('auto-approve-tools') ? 1 : 0,
355
+ toolScopeWildcard: result.signals?.includes('wildcard-tool-scope') || false
356
+ }));
357
+ }
358
+