@lushly-dev/violet-afd 0.0.0-stage → 0.3.0-preview.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/commands/analyze/compare.d.ts +34 -0
- package/dist/commands/analyze/compare.d.ts.map +1 -0
- package/dist/commands/analyze/compare.js +180 -0
- package/dist/commands/analyze/compare.js.map +1 -0
- package/dist/commands/analyze/compliance.d.ts +41 -0
- package/dist/commands/analyze/compliance.d.ts.map +1 -0
- package/dist/commands/analyze/compliance.js +166 -0
- package/dist/commands/analyze/compliance.js.map +1 -0
- package/dist/commands/analyze/deviations.d.ts +30 -0
- package/dist/commands/analyze/deviations.d.ts.map +1 -0
- package/dist/commands/analyze/deviations.js +201 -0
- package/dist/commands/analyze/deviations.js.map +1 -0
- package/dist/commands/analyze/health.d.ts +211 -0
- package/dist/commands/analyze/health.d.ts.map +1 -0
- package/dist/commands/analyze/health.js +697 -0
- package/dist/commands/analyze/health.js.map +1 -0
- package/dist/commands/analyze/index.d.ts +15 -0
- package/dist/commands/analyze/index.d.ts.map +1 -0
- package/dist/commands/analyze/index.js +31 -0
- package/dist/commands/analyze/index.js.map +1 -0
- package/dist/commands/analyze/snapshot.d.ts +64 -0
- package/dist/commands/analyze/snapshot.d.ts.map +1 -0
- package/dist/commands/analyze/snapshot.js +126 -0
- package/dist/commands/analyze/snapshot.js.map +1 -0
- package/dist/commands/analyze/trends.d.ts +82 -0
- package/dist/commands/analyze/trends.d.ts.map +1 -0
- package/dist/commands/analyze/trends.js +312 -0
- package/dist/commands/analyze/trends.js.map +1 -0
- package/dist/commands/analyze/violations.d.ts +84 -0
- package/dist/commands/analyze/violations.d.ts.map +1 -0
- package/dist/commands/analyze/violations.js +158 -0
- package/dist/commands/analyze/violations.js.map +1 -0
- package/dist/commands/apikeys/check.d.ts +39 -0
- package/dist/commands/apikeys/check.d.ts.map +1 -0
- package/dist/commands/apikeys/check.js +61 -0
- package/dist/commands/apikeys/check.js.map +1 -0
- package/dist/commands/apikeys/create.d.ts +36 -0
- package/dist/commands/apikeys/create.d.ts.map +1 -0
- package/dist/commands/apikeys/create.js +116 -0
- package/dist/commands/apikeys/create.js.map +1 -0
- package/dist/commands/apikeys/index.d.ts +15 -0
- package/dist/commands/apikeys/index.d.ts.map +1 -0
- package/dist/commands/apikeys/index.js +21 -0
- package/dist/commands/apikeys/index.js.map +1 -0
- package/dist/commands/apikeys/keys.d.ts +79 -0
- package/dist/commands/apikeys/keys.d.ts.map +1 -0
- package/dist/commands/apikeys/keys.js +239 -0
- package/dist/commands/apikeys/keys.js.map +1 -0
- package/dist/commands/audit/index.d.ts +11 -0
- package/dist/commands/audit/index.d.ts.map +1 -0
- package/dist/commands/audit/index.js +15 -0
- package/dist/commands/audit/index.js.map +1 -0
- package/dist/commands/audit/list.d.ts +101 -0
- package/dist/commands/audit/list.d.ts.map +1 -0
- package/dist/commands/audit/list.js +230 -0
- package/dist/commands/audit/list.js.map +1 -0
- package/dist/commands/changes/impact.d.ts +477 -0
- package/dist/commands/changes/impact.d.ts.map +1 -0
- package/dist/commands/changes/impact.js +541 -0
- package/dist/commands/changes/impact.js.map +1 -0
- package/dist/commands/changes/index.d.ts +11 -0
- package/dist/commands/changes/index.d.ts.map +1 -0
- package/dist/commands/changes/index.js +15 -0
- package/dist/commands/changes/index.js.map +1 -0
- package/dist/commands/constraints/index.d.ts +11 -0
- package/dist/commands/constraints/index.d.ts.map +1 -0
- package/dist/commands/constraints/index.js +19 -0
- package/dist/commands/constraints/index.js.map +1 -0
- package/dist/commands/constraints/list.d.ts +52 -0
- package/dist/commands/constraints/list.d.ts.map +1 -0
- package/dist/commands/constraints/list.js +103 -0
- package/dist/commands/constraints/list.js.map +1 -0
- package/dist/commands/constraints/set.d.ts +50 -0
- package/dist/commands/constraints/set.d.ts.map +1 -0
- package/dist/commands/constraints/set.js +137 -0
- package/dist/commands/constraints/set.js.map +1 -0
- package/dist/commands/constraints/validate.d.ts +38 -0
- package/dist/commands/constraints/validate.d.ts.map +1 -0
- package/dist/commands/constraints/validate.js +164 -0
- package/dist/commands/constraints/validate.js.map +1 -0
- package/dist/commands/docs/generator.d.ts +77 -0
- package/dist/commands/docs/generator.d.ts.map +1 -0
- package/dist/commands/docs/generator.js +256 -0
- package/dist/commands/docs/generator.js.map +1 -0
- package/dist/commands/docs/index.d.ts +6 -0
- package/dist/commands/docs/index.d.ts.map +1 -0
- package/dist/commands/docs/index.js +6 -0
- package/dist/commands/docs/index.js.map +1 -0
- package/dist/commands/export/export.d.ts +64 -0
- package/dist/commands/export/export.d.ts.map +1 -0
- package/dist/commands/export/export.js +193 -0
- package/dist/commands/export/export.js.map +1 -0
- package/dist/commands/export/index.d.ts +9 -0
- package/dist/commands/export/index.d.ts.map +1 -0
- package/dist/commands/export/index.js +13 -0
- package/dist/commands/export/index.js.map +1 -0
- package/dist/commands/import/flatten.d.ts +30 -0
- package/dist/commands/import/flatten.d.ts.map +1 -0
- package/dist/commands/import/flatten.js +121 -0
- package/dist/commands/import/flatten.js.map +1 -0
- package/dist/commands/import/index.d.ts +11 -0
- package/dist/commands/import/index.d.ts.map +1 -0
- package/dist/commands/import/index.js +19 -0
- package/dist/commands/import/index.js.map +1 -0
- package/dist/commands/import/json.d.ts +40 -0
- package/dist/commands/import/json.d.ts.map +1 -0
- package/dist/commands/import/json.js +209 -0
- package/dist/commands/import/json.js.map +1 -0
- package/dist/commands/import/tree.d.ts +39 -0
- package/dist/commands/import/tree.d.ts.map +1 -0
- package/dist/commands/import/tree.js +247 -0
- package/dist/commands/import/tree.js.map +1 -0
- package/dist/commands/import/validate.d.ts +53 -0
- package/dist/commands/import/validate.d.ts.map +1 -0
- package/dist/commands/import/validate.js +201 -0
- package/dist/commands/import/validate.js.map +1 -0
- package/dist/commands/index.d.ts +21 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +21 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/json-schema.d.ts +21 -0
- package/dist/commands/json-schema.d.ts.map +1 -0
- package/dist/commands/json-schema.js +41 -0
- package/dist/commands/json-schema.js.map +1 -0
- package/dist/commands/node/create.d.ts +80 -0
- package/dist/commands/node/create.d.ts.map +1 -0
- package/dist/commands/node/create.js +174 -0
- package/dist/commands/node/create.js.map +1 -0
- package/dist/commands/node/delete.d.ts +28 -0
- package/dist/commands/node/delete.d.ts.map +1 -0
- package/dist/commands/node/delete.js +134 -0
- package/dist/commands/node/delete.js.map +1 -0
- package/dist/commands/node/deprecation.d.ts +23 -0
- package/dist/commands/node/deprecation.d.ts.map +1 -0
- package/dist/commands/node/deprecation.js +59 -0
- package/dist/commands/node/deprecation.js.map +1 -0
- package/dist/commands/node/get.d.ts +79 -0
- package/dist/commands/node/get.d.ts.map +1 -0
- package/dist/commands/node/get.js +106 -0
- package/dist/commands/node/get.js.map +1 -0
- package/dist/commands/node/graph.d.ts +138 -0
- package/dist/commands/node/graph.d.ts.map +1 -0
- package/dist/commands/node/graph.js +285 -0
- package/dist/commands/node/graph.js.map +1 -0
- package/dist/commands/node/index.d.ts +17 -0
- package/dist/commands/node/index.d.ts.map +1 -0
- package/dist/commands/node/index.js +31 -0
- package/dist/commands/node/index.js.map +1 -0
- package/dist/commands/node/list.d.ts +74 -0
- package/dist/commands/node/list.d.ts.map +1 -0
- package/dist/commands/node/list.js +130 -0
- package/dist/commands/node/list.js.map +1 -0
- package/dist/commands/node/operations.d.ts +35 -0
- package/dist/commands/node/operations.d.ts.map +1 -0
- package/dist/commands/node/operations.js +66 -0
- package/dist/commands/node/operations.js.map +1 -0
- package/dist/commands/node/scope.d.ts +14 -0
- package/dist/commands/node/scope.d.ts.map +1 -0
- package/dist/commands/node/scope.js +49 -0
- package/dist/commands/node/scope.js.map +1 -0
- package/dist/commands/node/update.d.ts +84 -0
- package/dist/commands/node/update.d.ts.map +1 -0
- package/dist/commands/node/update.js +229 -0
- package/dist/commands/node/update.js.map +1 -0
- package/dist/commands/registry.d.ts +114 -0
- package/dist/commands/registry.d.ts.map +1 -0
- package/dist/commands/registry.js +141 -0
- package/dist/commands/registry.js.map +1 -0
- package/dist/commands/specimen/get.d.ts +46 -0
- package/dist/commands/specimen/get.d.ts.map +1 -0
- package/dist/commands/specimen/get.js +121 -0
- package/dist/commands/specimen/get.js.map +1 -0
- package/dist/commands/specimen/index.d.ts +10 -0
- package/dist/commands/specimen/index.d.ts.map +1 -0
- package/dist/commands/specimen/index.js +16 -0
- package/dist/commands/specimen/index.js.map +1 -0
- package/dist/commands/specimen/set.d.ts +31 -0
- package/dist/commands/specimen/set.d.ts.map +1 -0
- package/dist/commands/specimen/set.js +188 -0
- package/dist/commands/specimen/set.js.map +1 -0
- package/dist/commands/specimen/shared.d.ts +16 -0
- package/dist/commands/specimen/shared.d.ts.map +1 -0
- package/dist/commands/specimen/shared.js +27 -0
- package/dist/commands/specimen/shared.js.map +1 -0
- package/dist/commands/system/doctor.d.ts +48 -0
- package/dist/commands/system/doctor.d.ts.map +1 -0
- package/dist/commands/system/doctor.js +224 -0
- package/dist/commands/system/doctor.js.map +1 -0
- package/dist/commands/system/index.d.ts +12 -0
- package/dist/commands/system/index.d.ts.map +1 -0
- package/dist/commands/system/index.js +17 -0
- package/dist/commands/system/index.js.map +1 -0
- package/dist/commands/system/validate.d.ts +47 -0
- package/dist/commands/system/validate.d.ts.map +1 -0
- package/dist/commands/system/validate.js +245 -0
- package/dist/commands/system/validate.js.map +1 -0
- package/dist/commands/token/add.d.ts +94 -0
- package/dist/commands/token/add.d.ts.map +1 -0
- package/dist/commands/token/add.js +162 -0
- package/dist/commands/token/add.js.map +1 -0
- package/dist/commands/token/catalog.d.ts +69 -0
- package/dist/commands/token/catalog.d.ts.map +1 -0
- package/dist/commands/token/catalog.js +175 -0
- package/dist/commands/token/catalog.js.map +1 -0
- package/dist/commands/token/compare.d.ts +140 -0
- package/dist/commands/token/compare.d.ts.map +1 -0
- package/dist/commands/token/compare.js +207 -0
- package/dist/commands/token/compare.js.map +1 -0
- package/dist/commands/token/copy.d.ts +31 -0
- package/dist/commands/token/copy.d.ts.map +1 -0
- package/dist/commands/token/copy.js +179 -0
- package/dist/commands/token/copy.js.map +1 -0
- package/dist/commands/token/definitions.d.ts +29 -0
- package/dist/commands/token/definitions.d.ts.map +1 -0
- package/dist/commands/token/definitions.js +63 -0
- package/dist/commands/token/definitions.js.map +1 -0
- package/dist/commands/token/index.d.ts +22 -0
- package/dist/commands/token/index.d.ts.map +1 -0
- package/dist/commands/token/index.js +44 -0
- package/dist/commands/token/index.js.map +1 -0
- package/dist/commands/token/lineage.d.ts +43 -0
- package/dist/commands/token/lineage.d.ts.map +1 -0
- package/dist/commands/token/lineage.js +187 -0
- package/dist/commands/token/lineage.js.map +1 -0
- package/dist/commands/token/matrix.d.ts +249 -0
- package/dist/commands/token/matrix.d.ts.map +1 -0
- package/dist/commands/token/matrix.js +658 -0
- package/dist/commands/token/matrix.js.map +1 -0
- package/dist/commands/token/override.d.ts +51 -0
- package/dist/commands/token/override.d.ts.map +1 -0
- package/dist/commands/token/override.js +158 -0
- package/dist/commands/token/override.js.map +1 -0
- package/dist/commands/token/previous-value.d.ts +23 -0
- package/dist/commands/token/previous-value.d.ts.map +1 -0
- package/dist/commands/token/previous-value.js +26 -0
- package/dist/commands/token/previous-value.js.map +1 -0
- package/dist/commands/token/reset.d.ts +29 -0
- package/dist/commands/token/reset.d.ts.map +1 -0
- package/dist/commands/token/reset.js +143 -0
- package/dist/commands/token/reset.js.map +1 -0
- package/dist/commands/token/resolve.d.ts +43 -0
- package/dist/commands/token/resolve.d.ts.map +1 -0
- package/dist/commands/token/resolve.js +136 -0
- package/dist/commands/token/resolve.js.map +1 -0
- package/dist/commands/token/subtract.d.ts +27 -0
- package/dist/commands/token/subtract.d.ts.map +1 -0
- package/dist/commands/token/subtract.js +129 -0
- package/dist/commands/token/subtract.js.map +1 -0
- package/dist/commands/webhooks/delete.d.ts +31 -0
- package/dist/commands/webhooks/delete.d.ts.map +1 -0
- package/dist/commands/webhooks/delete.js +94 -0
- package/dist/commands/webhooks/delete.js.map +1 -0
- package/dist/commands/webhooks/get.d.ts +50 -0
- package/dist/commands/webhooks/get.d.ts.map +1 -0
- package/dist/commands/webhooks/get.js +122 -0
- package/dist/commands/webhooks/get.js.map +1 -0
- package/dist/commands/webhooks/index.d.ts +24 -0
- package/dist/commands/webhooks/index.d.ts.map +1 -0
- package/dist/commands/webhooks/index.js +41 -0
- package/dist/commands/webhooks/index.js.map +1 -0
- package/dist/commands/webhooks/list.d.ts +72 -0
- package/dist/commands/webhooks/list.d.ts.map +1 -0
- package/dist/commands/webhooks/list.js +116 -0
- package/dist/commands/webhooks/list.js.map +1 -0
- package/dist/commands/webhooks/logs.d.ts +65 -0
- package/dist/commands/webhooks/logs.d.ts.map +1 -0
- package/dist/commands/webhooks/logs.js +138 -0
- package/dist/commands/webhooks/logs.js.map +1 -0
- package/dist/commands/webhooks/redact.d.ts +18 -0
- package/dist/commands/webhooks/redact.d.ts.map +1 -0
- package/dist/commands/webhooks/redact.js +24 -0
- package/dist/commands/webhooks/redact.js.map +1 -0
- package/dist/commands/webhooks/register.d.ts +78 -0
- package/dist/commands/webhooks/register.d.ts.map +1 -0
- package/dist/commands/webhooks/register.js +175 -0
- package/dist/commands/webhooks/register.js.map +1 -0
- package/dist/commands/webhooks/test.d.ts +32 -0
- package/dist/commands/webhooks/test.d.ts.map +1 -0
- package/dist/commands/webhooks/test.js +101 -0
- package/dist/commands/webhooks/test.js.map +1 -0
- package/dist/commands/webhooks/update.d.ts +77 -0
- package/dist/commands/webhooks/update.d.ts.map +1 -0
- package/dist/commands/webhooks/update.js +198 -0
- package/dist/commands/webhooks/update.js.map +1 -0
- package/dist/css.d.ts +3 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/css.js.map +1 -0
- package/dist/demo/index.d.ts +8 -0
- package/dist/demo/index.d.ts.map +1 -0
- package/dist/demo/index.js +8 -0
- package/dist/demo/index.js.map +1 -0
- package/dist/demo/realistic-system.d.ts +54 -0
- package/dist/demo/realistic-system.d.ts.map +1 -0
- package/dist/demo/realistic-system.js +919 -0
- package/dist/demo/realistic-system.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/types/command-result.d.ts +10 -0
- package/dist/types/command-result.d.ts.map +1 -0
- package/dist/types/command-result.js +11 -0
- package/dist/types/command-result.js.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +10 -0
- package/dist/types/index.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/dist/webhooks/dispatcher.d.ts +147 -0
- package/dist/webhooks/dispatcher.d.ts.map +1 -0
- package/dist/webhooks/dispatcher.js +420 -0
- package/dist/webhooks/dispatcher.js.map +1 -0
- package/dist/webhooks/index.d.ts +9 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +9 -0
- package/dist/webhooks/index.js.map +1 -0
- package/dist/webhooks/signer.d.ts +119 -0
- package/dist/webhooks/signer.d.ts.map +1 -0
- package/dist/webhooks/signer.js +149 -0
- package/dist/webhooks/signer.js.map +1 -0
- package/dist/webhooks/url-policy.d.ts +73 -0
- package/dist/webhooks/url-policy.d.ts.map +1 -0
- package/dist/webhooks/url-policy.js +250 -0
- package/dist/webhooks/url-policy.js.map +1 -0
- package/dist/worker.d.ts +11 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +31 -0
- package/dist/worker.js.map +1 -0
- package/package.json +84 -3
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import { InheritanceResolutionError, resetTarget, resolveNodeTokenMap, } from '@lushly-dev/violet-core/engine';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { ErrorCodes, failure, success } from '../../types/command-result.js';
|
|
4
|
+
import { daysUntil } from '../node/deprecation.js';
|
|
5
|
+
import { selectSubtree } from '../node/scope.js';
|
|
6
|
+
import { operationTarget } from '../token/definitions.js';
|
|
7
|
+
import { nodeViolations } from './violations.js';
|
|
8
|
+
/**
|
|
9
|
+
* Input schema for analyze.health command
|
|
10
|
+
*/
|
|
11
|
+
const InputSchema = z.object({
|
|
12
|
+
rootId: z.string().min(1).optional(), // Subtree to report; defaults to every tree
|
|
13
|
+
/**
|
|
14
|
+
* What drift is measured against: 'parent' (what the node inherits, the
|
|
15
|
+
* default), 'root' (the top of the node's tree), or any node id (a chosen
|
|
16
|
+
* reference, e.g. a product every theme should match). The keywords win
|
|
17
|
+
* over a node with the same id.
|
|
18
|
+
*/
|
|
19
|
+
baseline: z.string().min(1).optional().default('parent'),
|
|
20
|
+
sort: z.enum(['tree', 'drift', 'violations', 'customizations']).optional().default('tree'),
|
|
21
|
+
includeViolations: z.boolean().optional().default(true), // Run every node's constraints (slowest part)
|
|
22
|
+
});
|
|
23
|
+
const SeverityCountsSchema = z.object({
|
|
24
|
+
total: z.number(),
|
|
25
|
+
error: z.number(),
|
|
26
|
+
warning: z.number(),
|
|
27
|
+
info: z.number(),
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* A risk worth a look:
|
|
31
|
+
* - redundant-override: an override equal to the value the node already inherits
|
|
32
|
+
* - sibling-borrow: a copy from a node outside this node's own line (not an
|
|
33
|
+
* ancestor or descendant), so a change there silently changes this node
|
|
34
|
+
* - deprecated-with-children: a deprecated node that still has children
|
|
35
|
+
* - inert-reset: a stored reset that does nothing, because it is on a root or
|
|
36
|
+
* its target is not an ancestor of the node (see resetTarget)
|
|
37
|
+
* - sunset-without-replacement: a node with a sunset date but no replacement
|
|
38
|
+
* - replacement-deprecated: a node whose replacement is itself deprecated, or
|
|
39
|
+
* no longer exists
|
|
40
|
+
* - past-sunset: a node still in the tree after its sunset date
|
|
41
|
+
*/
|
|
42
|
+
const HealthFlagSchema = z.object({
|
|
43
|
+
kind: z.enum([
|
|
44
|
+
'redundant-override',
|
|
45
|
+
'sibling-borrow',
|
|
46
|
+
'deprecated-with-children',
|
|
47
|
+
'inert-reset',
|
|
48
|
+
'sunset-without-replacement',
|
|
49
|
+
'replacement-deprecated',
|
|
50
|
+
'past-sunset',
|
|
51
|
+
]),
|
|
52
|
+
message: z.string(),
|
|
53
|
+
tokenId: z.string().optional(),
|
|
54
|
+
value: z.any().optional(),
|
|
55
|
+
/** Redundant overrides: the node the equal value is inherited from */
|
|
56
|
+
inheritedFrom: z.string().optional(),
|
|
57
|
+
/** Sibling borrows: the copy source */
|
|
58
|
+
fromNode: z.string().optional(),
|
|
59
|
+
/** Inert resets: the stored target, when there is one */
|
|
60
|
+
toAncestor: z.string().optional(),
|
|
61
|
+
/** Deprecated nodes: their children */
|
|
62
|
+
children: z.array(z.string()).optional(),
|
|
63
|
+
/** Deprecation plans: the replacement and the sunset date */
|
|
64
|
+
replacedBy: z.string().optional(),
|
|
65
|
+
sunsetAt: z.string().optional(),
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* A deprecated node's plan and what still depends on it: when it goes away,
|
|
69
|
+
* what replaces it, and its usage (descendants and borrowers in the report)
|
|
70
|
+
*/
|
|
71
|
+
const DeprecationSchema = z.object({
|
|
72
|
+
/** `YYYY-MM-DD`, or null when no date is set */
|
|
73
|
+
sunsetAt: z.string().nullable(),
|
|
74
|
+
/** Days from today (UTC) to the sunset date; negative once passed */
|
|
75
|
+
daysLeft: z.number().nullable(),
|
|
76
|
+
replacedBy: z.string().nullable(),
|
|
77
|
+
/** The replacement's status, or null when there is none or it no longer exists */
|
|
78
|
+
replacementStatus: z.string().nullable(),
|
|
79
|
+
usage: z.object({
|
|
80
|
+
/** Nodes under this one */
|
|
81
|
+
descendants: z.number(),
|
|
82
|
+
/** Nodes that copy tokens from this one */
|
|
83
|
+
borrowers: z.number(),
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
const HealthNodeSchema = z.object({
|
|
87
|
+
id: z.string(),
|
|
88
|
+
name: z.string(),
|
|
89
|
+
type: z.string(),
|
|
90
|
+
status: z.string(),
|
|
91
|
+
parentId: z.string().nullable(),
|
|
92
|
+
depth: z.number(),
|
|
93
|
+
owner: z.string().optional(),
|
|
94
|
+
/** For a deprecated node, or one with a sunset date or replacement */
|
|
95
|
+
deprecation: DeprecationSchema.optional(),
|
|
96
|
+
/** Tokens the node resolves */
|
|
97
|
+
tokens: z.number(),
|
|
98
|
+
/** The node's own effective changes: the last operation per token */
|
|
99
|
+
customizations: z.object({
|
|
100
|
+
total: z.number(),
|
|
101
|
+
add: z.number(),
|
|
102
|
+
override: z.number(),
|
|
103
|
+
subtract: z.number(),
|
|
104
|
+
reset: z.number(),
|
|
105
|
+
copy: z.number(),
|
|
106
|
+
byCategory: z.record(z.string(), z.number()),
|
|
107
|
+
}),
|
|
108
|
+
/** Resolved tokens that differ from the baseline node's */
|
|
109
|
+
drift: z.object({
|
|
110
|
+
/** Node compared against; null when there is none (a root, with baseline 'parent') */
|
|
111
|
+
baseline: z.string().nullable(),
|
|
112
|
+
/** Tokens in either node */
|
|
113
|
+
compared: z.number(),
|
|
114
|
+
/** Different value in both */
|
|
115
|
+
changed: z.number(),
|
|
116
|
+
/** Only here */
|
|
117
|
+
added: z.number(),
|
|
118
|
+
/** Only in the baseline */
|
|
119
|
+
removed: z.number(),
|
|
120
|
+
/** (changed + added + removed) / compared, 0–1 */
|
|
121
|
+
ratio: z.number(),
|
|
122
|
+
}),
|
|
123
|
+
/** Constraint violations by severity; omitted when not checked */
|
|
124
|
+
violations: SeverityCountsSchema.optional(),
|
|
125
|
+
coupling: z.object({
|
|
126
|
+
/** Mixed-in nodes */
|
|
127
|
+
includes: z.array(z.string()),
|
|
128
|
+
/** Nodes this one copies tokens from */
|
|
129
|
+
borrows: z.array(z.object({
|
|
130
|
+
fromNode: z.string(),
|
|
131
|
+
relation: z.enum(['ancestor', 'descendant', 'sibling']),
|
|
132
|
+
tokens: z.array(z.string()),
|
|
133
|
+
})),
|
|
134
|
+
/** Nodes in the report that copy tokens from this one */
|
|
135
|
+
borrowedBy: z.array(z.string()),
|
|
136
|
+
}),
|
|
137
|
+
flags: z.array(HealthFlagSchema),
|
|
138
|
+
/** This node plus its descendants in the report */
|
|
139
|
+
subtree: z.object({
|
|
140
|
+
nodes: z.number(),
|
|
141
|
+
customizations: z.number(),
|
|
142
|
+
violations: z.number(),
|
|
143
|
+
maxDrift: z.number(),
|
|
144
|
+
}),
|
|
145
|
+
/** Why the node could not be resolved (e.g. a copy/include cycle) */
|
|
146
|
+
error: z.string().optional(),
|
|
147
|
+
});
|
|
148
|
+
/**
|
|
149
|
+
* Output schema for analyze.health command
|
|
150
|
+
*/
|
|
151
|
+
const OutputSchema = z.object({
|
|
152
|
+
baseline: z.string(),
|
|
153
|
+
sort: z.string(),
|
|
154
|
+
nodes: z.array(HealthNodeSchema),
|
|
155
|
+
summary: z.object({
|
|
156
|
+
nodes: z.number(),
|
|
157
|
+
customizations: z.number(),
|
|
158
|
+
violations: SeverityCountsSchema.optional(),
|
|
159
|
+
flags: z.object({
|
|
160
|
+
redundantOverrides: z.number(),
|
|
161
|
+
siblingBorrows: z.number(),
|
|
162
|
+
deprecatedWithChildren: z.number(),
|
|
163
|
+
inertResets: z.number(),
|
|
164
|
+
sunsetWithoutReplacement: z.number(),
|
|
165
|
+
replacementDeprecated: z.number(),
|
|
166
|
+
pastSunset: z.number(),
|
|
167
|
+
}),
|
|
168
|
+
/** Nodes with a sunset date, soonest first */
|
|
169
|
+
sunsets: z.array(z.object({
|
|
170
|
+
id: z.string(),
|
|
171
|
+
sunsetAt: z.string(),
|
|
172
|
+
daysLeft: z.number(),
|
|
173
|
+
replacedBy: z.string().nullable(),
|
|
174
|
+
descendants: z.number(),
|
|
175
|
+
borrowers: z.number(),
|
|
176
|
+
})),
|
|
177
|
+
/** Nodes with at least one flag */
|
|
178
|
+
flagged: z.number(),
|
|
179
|
+
/** Nodes and violations per owner; `owner` is null for unowned nodes */
|
|
180
|
+
owners: z.array(z.object({
|
|
181
|
+
owner: z.string().nullable(),
|
|
182
|
+
nodes: z.number(),
|
|
183
|
+
customizations: z.number(),
|
|
184
|
+
violations: z.number(),
|
|
185
|
+
})),
|
|
186
|
+
/** Nodes that could not be resolved */
|
|
187
|
+
unresolved: z.array(z.string()),
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
/** Compare values with references followed, as the matrix does */
|
|
191
|
+
function valueKey(token) {
|
|
192
|
+
return JSON.stringify(token.finalValue ?? token.value);
|
|
193
|
+
}
|
|
194
|
+
function round(ratio) {
|
|
195
|
+
return Math.round(ratio * 10_000) / 10_000;
|
|
196
|
+
}
|
|
197
|
+
/** Tokens that differ between a node and its baseline */
|
|
198
|
+
export function compareTokenMaps(tokens, baseline) {
|
|
199
|
+
let changed = 0;
|
|
200
|
+
let added = 0;
|
|
201
|
+
for (const [id, token] of tokens) {
|
|
202
|
+
const other = baseline.get(id);
|
|
203
|
+
if (!other)
|
|
204
|
+
added += 1;
|
|
205
|
+
else if (valueKey(other) !== valueKey(token))
|
|
206
|
+
changed += 1;
|
|
207
|
+
}
|
|
208
|
+
let removed = 0;
|
|
209
|
+
for (const id of baseline.keys()) {
|
|
210
|
+
if (!tokens.has(id))
|
|
211
|
+
removed += 1;
|
|
212
|
+
}
|
|
213
|
+
const compared = tokens.size + removed;
|
|
214
|
+
const differing = changed + added + removed;
|
|
215
|
+
return {
|
|
216
|
+
compared,
|
|
217
|
+
changed,
|
|
218
|
+
added,
|
|
219
|
+
removed,
|
|
220
|
+
ratio: compared > 0 ? round(differing / compared) : 0,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/** The last operation per token on a node: what it currently changes */
|
|
224
|
+
function effectiveOperations(ops) {
|
|
225
|
+
const latest = new Map();
|
|
226
|
+
for (const op of [...ops].sort((a, b) => a.sequence - b.sequence)) {
|
|
227
|
+
const target = operationTarget(op);
|
|
228
|
+
latest.delete(target);
|
|
229
|
+
latest.set(target, op);
|
|
230
|
+
}
|
|
231
|
+
return [...latest.values()];
|
|
232
|
+
}
|
|
233
|
+
function parseValue(raw) {
|
|
234
|
+
if (raw === null)
|
|
235
|
+
return undefined;
|
|
236
|
+
try {
|
|
237
|
+
return JSON.parse(raw);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return raw;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function formatValue(value) {
|
|
244
|
+
return typeof value === 'string' ? value : JSON.stringify(value);
|
|
245
|
+
}
|
|
246
|
+
function createResolver(db) {
|
|
247
|
+
const resolved = new Map();
|
|
248
|
+
return async (nodeId) => {
|
|
249
|
+
let entry = resolved.get(nodeId);
|
|
250
|
+
if (entry === undefined) {
|
|
251
|
+
try {
|
|
252
|
+
entry = await resolveNodeTokenMap(db, nodeId);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
if (!(error instanceof InheritanceResolutionError))
|
|
256
|
+
throw error;
|
|
257
|
+
entry = error.message;
|
|
258
|
+
}
|
|
259
|
+
resolved.set(nodeId, entry);
|
|
260
|
+
}
|
|
261
|
+
return entry;
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function baselineFor(run, node, chain) {
|
|
265
|
+
if (run.baseline === 'parent')
|
|
266
|
+
return node.parentId;
|
|
267
|
+
if (run.baseline === 'root')
|
|
268
|
+
return chain[0] ?? node.id;
|
|
269
|
+
return run.baseline;
|
|
270
|
+
}
|
|
271
|
+
/** Where a copy source sits relative to the copying node */
|
|
272
|
+
async function relationTo(run, nodeId, ancestors, sourceId) {
|
|
273
|
+
if (ancestors.has(sourceId))
|
|
274
|
+
return 'ancestor';
|
|
275
|
+
const sourceChain = await run.context.db.getAncestryChain(sourceId);
|
|
276
|
+
return sourceChain.includes(nodeId) ? 'descendant' : 'sibling';
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* A flag when an override sets the value the node already inherits. Only when
|
|
280
|
+
* the override is what the node resolves: a mix-in may win over it.
|
|
281
|
+
*/
|
|
282
|
+
function redundantOverride(nodeId, op, own, from) {
|
|
283
|
+
if (op.type !== 'override' || !own || !from || own.resolvedAt !== nodeId)
|
|
284
|
+
return null;
|
|
285
|
+
if (valueKey(own) !== valueKey(from))
|
|
286
|
+
return null;
|
|
287
|
+
const value = parseValue(op.value);
|
|
288
|
+
return {
|
|
289
|
+
kind: 'redundant-override',
|
|
290
|
+
message: `Overrides ${own.id} with ${formatValue(value)}, the value it already inherits from ${from.resolvedAt}`,
|
|
291
|
+
tokenId: own.id,
|
|
292
|
+
value,
|
|
293
|
+
inheritedFrom: from.resolvedAt,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Flags for every stored reset that lands nowhere, effective or not: a reset on
|
|
298
|
+
* a root, or to a node that is not an ancestor. It is stored but does nothing.
|
|
299
|
+
*/
|
|
300
|
+
function inertResets(node, chain, operations) {
|
|
301
|
+
return operations
|
|
302
|
+
.filter((op) => op.type === 'reset' && !resetTarget(op, chain))
|
|
303
|
+
.map((op) => ({
|
|
304
|
+
kind: 'inert-reset',
|
|
305
|
+
message: `Resets ${op.tokenId}, but ${op.toAncestor
|
|
306
|
+
? `'${op.toAncestor}' is not an ancestor of ${node.id}`
|
|
307
|
+
: `${node.id} is a root, so there is no ancestor to reset to`}: the reset does nothing`,
|
|
308
|
+
tokenId: op.tokenId,
|
|
309
|
+
...(op.toAncestor ? { toAncestor: op.toAncestor } : {}),
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Count a node's effective operations by type and category, and flag
|
|
314
|
+
* redundant overrides and copies from outside its own line.
|
|
315
|
+
*/
|
|
316
|
+
async function scanOperations(run, node, chain, tokens, entry) {
|
|
317
|
+
const parent = node.parentId ? await run.resolve(node.parentId) : undefined;
|
|
318
|
+
const inherited = typeof parent === 'string' ? undefined : parent;
|
|
319
|
+
const ancestors = new Set(chain);
|
|
320
|
+
const borrows = new Map();
|
|
321
|
+
const { customizations } = entry;
|
|
322
|
+
const operations = await run.context.db.getOperations(node.id);
|
|
323
|
+
entry.flags.push(...inertResets(node, chain, operations));
|
|
324
|
+
for (const op of effectiveOperations(operations)) {
|
|
325
|
+
const target = operationTarget(op);
|
|
326
|
+
const own = tokens.get(target);
|
|
327
|
+
const from = inherited?.get(target);
|
|
328
|
+
const category = (own ?? from)?.metadata.category ?? 'custom';
|
|
329
|
+
customizations[op.type] += 1;
|
|
330
|
+
customizations.total += 1;
|
|
331
|
+
customizations.byCategory[category] = (customizations.byCategory[category] ?? 0) + 1;
|
|
332
|
+
const redundant = redundantOverride(node.id, op, own, from);
|
|
333
|
+
if (redundant)
|
|
334
|
+
entry.flags.push(redundant);
|
|
335
|
+
if (op.type !== 'copy' || !op.fromNode)
|
|
336
|
+
continue;
|
|
337
|
+
let borrow = borrows.get(op.fromNode);
|
|
338
|
+
if (!borrow) {
|
|
339
|
+
const relation = await relationTo(run, node.id, ancestors, op.fromNode);
|
|
340
|
+
borrow = { fromNode: op.fromNode, relation, tokens: [] };
|
|
341
|
+
borrows.set(op.fromNode, borrow);
|
|
342
|
+
}
|
|
343
|
+
borrow.tokens.push(target);
|
|
344
|
+
if (borrow.relation === 'sibling') {
|
|
345
|
+
entry.flags.push({
|
|
346
|
+
kind: 'sibling-borrow',
|
|
347
|
+
message: `Borrows ${target} from sibling ${op.fromNode}: a change there silently changes this node`,
|
|
348
|
+
tokenId: target,
|
|
349
|
+
fromNode: op.fromNode,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
entry.coupling.borrows = [...borrows.values()];
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* A deprecated node's sunset date and replacement, with flags for a date and no
|
|
357
|
+
* replacement and for a replacement that is deprecated or gone. Usage and the
|
|
358
|
+
* past-sunset flag need the whole report, so rollUp fills them in.
|
|
359
|
+
*/
|
|
360
|
+
async function planDeprecation(run, node, entry) {
|
|
361
|
+
const sunsetAt = node.sunsetAt ?? null;
|
|
362
|
+
const replacedBy = node.replacedBy ?? null;
|
|
363
|
+
if (node.status !== 'deprecated' && !sunsetAt && !replacedBy)
|
|
364
|
+
return;
|
|
365
|
+
const replacement = replacedBy ? await run.context.db.getNode(replacedBy) : null;
|
|
366
|
+
const available = replacement && !replacement.deletedAt ? replacement : null;
|
|
367
|
+
entry.deprecation = {
|
|
368
|
+
sunsetAt,
|
|
369
|
+
daysLeft: sunsetAt ? daysUntil(sunsetAt, run.now) : null,
|
|
370
|
+
replacedBy,
|
|
371
|
+
replacementStatus: available?.status ?? null,
|
|
372
|
+
usage: { descendants: 0, borrowers: 0 },
|
|
373
|
+
};
|
|
374
|
+
if (sunsetAt && !replacedBy) {
|
|
375
|
+
entry.flags.push({
|
|
376
|
+
kind: 'sunset-without-replacement',
|
|
377
|
+
message: `Goes away on ${sunsetAt}, but no node replaces it`,
|
|
378
|
+
sunsetAt,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
if (replacedBy && available?.status !== 'active' && available?.status !== 'experimental') {
|
|
382
|
+
entry.flags.push({
|
|
383
|
+
kind: 'replacement-deprecated',
|
|
384
|
+
message: available
|
|
385
|
+
? `Replaced by ${replacedBy}, which is itself deprecated`
|
|
386
|
+
: `Replaced by ${replacedBy}, which no longer exists`,
|
|
387
|
+
replacedBy,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
async function describeNode(run, node) {
|
|
392
|
+
const chain = await run.context.db.getAncestryChain(node.id);
|
|
393
|
+
const baselineId = baselineFor(run, node, chain);
|
|
394
|
+
const children = run.childrenOf.get(node.id) ?? [];
|
|
395
|
+
const entry = {
|
|
396
|
+
id: node.id,
|
|
397
|
+
name: node.name,
|
|
398
|
+
type: node.type,
|
|
399
|
+
status: node.status,
|
|
400
|
+
parentId: node.parentId,
|
|
401
|
+
depth: chain.length,
|
|
402
|
+
...(node.owner ? { owner: node.owner } : {}),
|
|
403
|
+
tokens: 0,
|
|
404
|
+
customizations: {
|
|
405
|
+
total: 0,
|
|
406
|
+
add: 0,
|
|
407
|
+
override: 0,
|
|
408
|
+
subtract: 0,
|
|
409
|
+
reset: 0,
|
|
410
|
+
copy: 0,
|
|
411
|
+
byCategory: {},
|
|
412
|
+
},
|
|
413
|
+
drift: { baseline: baselineId, compared: 0, changed: 0, added: 0, removed: 0, ratio: 0 },
|
|
414
|
+
coupling: { includes: node.includes ?? [], borrows: [], borrowedBy: [] },
|
|
415
|
+
flags: [],
|
|
416
|
+
subtree: { nodes: 1, customizations: 0, violations: 0, maxDrift: 0 },
|
|
417
|
+
};
|
|
418
|
+
if (node.status === 'deprecated' && children.length > 0) {
|
|
419
|
+
entry.flags.push({
|
|
420
|
+
kind: 'deprecated-with-children',
|
|
421
|
+
message: `Deprecated but still has ${children.length} child node(s): ${children.join(', ')}`,
|
|
422
|
+
children,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
await planDeprecation(run, node, entry);
|
|
426
|
+
const tokens = await run.resolve(node.id);
|
|
427
|
+
if (typeof tokens === 'string') {
|
|
428
|
+
entry.error = tokens;
|
|
429
|
+
return entry;
|
|
430
|
+
}
|
|
431
|
+
entry.tokens = tokens.size;
|
|
432
|
+
const baseline = baselineId ? await run.resolve(baselineId) : undefined;
|
|
433
|
+
if (baseline && typeof baseline !== 'string') {
|
|
434
|
+
entry.drift = { baseline: baselineId, ...compareTokenMaps(tokens, baseline) };
|
|
435
|
+
}
|
|
436
|
+
await scanOperations(run, node, chain, tokens, entry);
|
|
437
|
+
if (run.includeViolations) {
|
|
438
|
+
const found = await nodeViolations(run.context, node, undefined);
|
|
439
|
+
if (found) {
|
|
440
|
+
const count = (severity) => found.filter((violation) => violation.severity === severity).length;
|
|
441
|
+
entry.violations = {
|
|
442
|
+
total: found.length,
|
|
443
|
+
error: count('error'),
|
|
444
|
+
warning: count('warning'),
|
|
445
|
+
info: count('info'),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return entry;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Fill in who borrows from each node and each node's subtree totals. Expects
|
|
453
|
+
* depth-first order, so every child follows its parent.
|
|
454
|
+
*/
|
|
455
|
+
function rollUp(nodes) {
|
|
456
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
457
|
+
for (const node of nodes) {
|
|
458
|
+
for (const borrow of node.coupling.borrows) {
|
|
459
|
+
byId.get(borrow.fromNode)?.coupling.borrowedBy.push(node.id);
|
|
460
|
+
}
|
|
461
|
+
node.subtree = {
|
|
462
|
+
nodes: 1,
|
|
463
|
+
customizations: node.customizations.total,
|
|
464
|
+
violations: node.violations?.total ?? 0,
|
|
465
|
+
maxDrift: node.drift.ratio,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
for (const node of [...nodes].reverse()) {
|
|
469
|
+
const parent = node.parentId ? byId.get(node.parentId) : undefined;
|
|
470
|
+
if (!parent)
|
|
471
|
+
continue;
|
|
472
|
+
parent.subtree.nodes += node.subtree.nodes;
|
|
473
|
+
parent.subtree.customizations += node.subtree.customizations;
|
|
474
|
+
parent.subtree.violations += node.subtree.violations;
|
|
475
|
+
parent.subtree.maxDrift = Math.max(parent.subtree.maxDrift, node.subtree.maxDrift);
|
|
476
|
+
}
|
|
477
|
+
for (const node of nodes) {
|
|
478
|
+
const plan = node.deprecation;
|
|
479
|
+
if (!plan)
|
|
480
|
+
continue;
|
|
481
|
+
plan.usage = {
|
|
482
|
+
descendants: node.subtree.nodes - 1,
|
|
483
|
+
borrowers: node.coupling.borrowedBy.length,
|
|
484
|
+
};
|
|
485
|
+
if (plan.sunsetAt && plan.daysLeft !== null && plan.daysLeft < 0) {
|
|
486
|
+
const users = [
|
|
487
|
+
plan.usage.descendants > 0 ? `${plan.usage.descendants} descendant(s)` : '',
|
|
488
|
+
plan.usage.borrowers > 0 ? `${plan.usage.borrowers} borrower(s)` : '',
|
|
489
|
+
].filter(Boolean);
|
|
490
|
+
node.flags.push({
|
|
491
|
+
kind: 'past-sunset',
|
|
492
|
+
message: `Was due to go away on ${plan.sunsetAt} (${-plan.daysLeft} day(s) ago)${users.length > 0 ? ` and still has ${users.join(' and ')}` : ''}`,
|
|
493
|
+
sunsetAt: plan.sunsetAt,
|
|
494
|
+
...(plan.replacedBy ? { replacedBy: plan.replacedBy } : {}),
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
const SORT_KEYS = {
|
|
500
|
+
drift: (node) => [node.drift.ratio],
|
|
501
|
+
violations: (node) => [
|
|
502
|
+
node.violations?.error ?? 0,
|
|
503
|
+
node.violations?.warning ?? 0,
|
|
504
|
+
node.violations?.info ?? 0,
|
|
505
|
+
],
|
|
506
|
+
customizations: (node) => [node.customizations.total],
|
|
507
|
+
};
|
|
508
|
+
/** Highest first; ties keep tree order */
|
|
509
|
+
function sortNodes(nodes, sort) {
|
|
510
|
+
if (sort === 'tree')
|
|
511
|
+
return nodes;
|
|
512
|
+
const key = SORT_KEYS[sort];
|
|
513
|
+
const order = new Map(nodes.map((node, index) => [node.id, index]));
|
|
514
|
+
return [...nodes].sort((a, b) => {
|
|
515
|
+
const left = key(a);
|
|
516
|
+
const right = key(b);
|
|
517
|
+
for (let i = 0; i < left.length; i++) {
|
|
518
|
+
if (left[i] !== right[i])
|
|
519
|
+
return right[i] - left[i];
|
|
520
|
+
}
|
|
521
|
+
return (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0);
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function summarize(nodes, includeViolations) {
|
|
525
|
+
const flags = nodes.flatMap((node) => node.flags);
|
|
526
|
+
const countFlags = (kind) => flags.filter((flag) => flag.kind === kind).length;
|
|
527
|
+
const owners = new Map();
|
|
528
|
+
for (const node of nodes) {
|
|
529
|
+
const owner = node.owner ?? null;
|
|
530
|
+
const entry = owners.get(owner) ?? { owner, nodes: 0, customizations: 0, violations: 0 };
|
|
531
|
+
entry.nodes += 1;
|
|
532
|
+
entry.customizations += node.customizations.total;
|
|
533
|
+
entry.violations += node.violations?.total ?? 0;
|
|
534
|
+
owners.set(owner, entry);
|
|
535
|
+
}
|
|
536
|
+
const sum = (pick) => nodes.reduce((total, node) => total + pick(node), 0);
|
|
537
|
+
return {
|
|
538
|
+
nodes: nodes.length,
|
|
539
|
+
customizations: sum((node) => node.customizations.total),
|
|
540
|
+
...(includeViolations
|
|
541
|
+
? {
|
|
542
|
+
violations: {
|
|
543
|
+
total: sum((node) => node.violations?.total ?? 0),
|
|
544
|
+
error: sum((node) => node.violations?.error ?? 0),
|
|
545
|
+
warning: sum((node) => node.violations?.warning ?? 0),
|
|
546
|
+
info: sum((node) => node.violations?.info ?? 0),
|
|
547
|
+
},
|
|
548
|
+
}
|
|
549
|
+
: {}),
|
|
550
|
+
flags: {
|
|
551
|
+
redundantOverrides: countFlags('redundant-override'),
|
|
552
|
+
siblingBorrows: countFlags('sibling-borrow'),
|
|
553
|
+
deprecatedWithChildren: countFlags('deprecated-with-children'),
|
|
554
|
+
inertResets: countFlags('inert-reset'),
|
|
555
|
+
sunsetWithoutReplacement: countFlags('sunset-without-replacement'),
|
|
556
|
+
replacementDeprecated: countFlags('replacement-deprecated'),
|
|
557
|
+
pastSunset: countFlags('past-sunset'),
|
|
558
|
+
},
|
|
559
|
+
sunsets: nodes
|
|
560
|
+
.flatMap((node) => {
|
|
561
|
+
const plan = node.deprecation;
|
|
562
|
+
if (!plan?.sunsetAt || plan.daysLeft === null)
|
|
563
|
+
return [];
|
|
564
|
+
return [
|
|
565
|
+
{
|
|
566
|
+
id: node.id,
|
|
567
|
+
sunsetAt: plan.sunsetAt,
|
|
568
|
+
daysLeft: plan.daysLeft,
|
|
569
|
+
replacedBy: plan.replacedBy,
|
|
570
|
+
descendants: plan.usage.descendants,
|
|
571
|
+
borrowers: plan.usage.borrowers,
|
|
572
|
+
},
|
|
573
|
+
];
|
|
574
|
+
})
|
|
575
|
+
.sort((a, b) => a.sunsetAt.localeCompare(b.sunsetAt) || a.id.localeCompare(b.id)),
|
|
576
|
+
flagged: nodes.filter((node) => node.flags.length > 0).length,
|
|
577
|
+
// Unowned nodes last
|
|
578
|
+
owners: [...owners.values()].sort((a, b) => a.owner === null ? 1 : b.owner === null ? -1 : a.owner.localeCompare(b.owner)),
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function describeRun(output) {
|
|
582
|
+
const { summary } = output;
|
|
583
|
+
const top = output.nodes.reduce((best, node) => (!best || node.drift.ratio > best.drift.ratio ? node : best), undefined);
|
|
584
|
+
const flags = Object.values(summary.flags).reduce((total, n) => total + n, 0);
|
|
585
|
+
return [
|
|
586
|
+
`${summary.nodes} node(s) against baseline '${output.baseline}'`,
|
|
587
|
+
top && top.drift.ratio > 0
|
|
588
|
+
? `most drift: ${top.id} (${Math.round(top.drift.ratio * 100)}%)`
|
|
589
|
+
: null,
|
|
590
|
+
summary.violations ? `${summary.violations.total} violation(s)` : null,
|
|
591
|
+
`${flags} flag(s) on ${summary.flagged} node(s)`,
|
|
592
|
+
summary.sunsets.length > 0
|
|
593
|
+
? `next sunset: ${summary.sunsets[0].id} on ${summary.sunsets[0].sunsetAt}`
|
|
594
|
+
: null,
|
|
595
|
+
summary.unresolved.length > 0
|
|
596
|
+
? `${summary.unresolved.length} node(s) could not be resolved`
|
|
597
|
+
: null,
|
|
598
|
+
]
|
|
599
|
+
.filter(Boolean)
|
|
600
|
+
.join('; ');
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* analyze.health command
|
|
604
|
+
*
|
|
605
|
+
* One report for "which products drift most, where do violations cluster, who
|
|
606
|
+
* owns what, and which links are risky": per node, its own customizations by
|
|
607
|
+
* category, drift against a baseline, violations by severity, owner, status and
|
|
608
|
+
* coupling (mix-ins and copies), with flags for redundant overrides, sibling
|
|
609
|
+
* borrows and deprecated nodes that still have children, and totals for each
|
|
610
|
+
* node's subtree. Deprecated nodes also report their plan (sunset date and
|
|
611
|
+
* replacement) and usage, with flags for a date without a replacement, a
|
|
612
|
+
* replacement that is deprecated or gone, and a node kept past its sunset.
|
|
613
|
+
*/
|
|
614
|
+
export const analyzeHealthCommand = {
|
|
615
|
+
name: 'analyze.health',
|
|
616
|
+
description: 'Health report per node: customizations by category, drift against a baseline, violations, owner, status, deprecation plans, coupling and risk flags',
|
|
617
|
+
category: 'analyze',
|
|
618
|
+
inputSchema: InputSchema,
|
|
619
|
+
outputSchema: OutputSchema,
|
|
620
|
+
async handler(rawInput, context) {
|
|
621
|
+
const { db } = context;
|
|
622
|
+
// Parse input to apply defaults
|
|
623
|
+
const input = InputSchema.parse(rawInput);
|
|
624
|
+
const scope = await selectSubtree(db, input.rootId);
|
|
625
|
+
if (!scope) {
|
|
626
|
+
return failure({
|
|
627
|
+
code: ErrorCodes.NODE_NOT_FOUND,
|
|
628
|
+
message: `Node '${input.rootId}' not found`,
|
|
629
|
+
suggestion: `Use 'node.list' to see available nodes`,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
const resolve = createResolver(db);
|
|
633
|
+
if (input.baseline !== 'parent' && input.baseline !== 'root') {
|
|
634
|
+
const node = await db.getNode(input.baseline);
|
|
635
|
+
if (!node || node.deletedAt) {
|
|
636
|
+
return failure({
|
|
637
|
+
code: ErrorCodes.NODE_NOT_FOUND,
|
|
638
|
+
message: `Baseline node '${input.baseline}' not found`,
|
|
639
|
+
suggestion: `Use 'parent', 'root', or a node id from 'node.list'`,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
const baseline = await resolve(input.baseline);
|
|
643
|
+
if (typeof baseline === 'string') {
|
|
644
|
+
return failure({
|
|
645
|
+
code: ErrorCodes.CIRCULAR_REFERENCE,
|
|
646
|
+
message: `Baseline node '${input.baseline}' could not be resolved: ${baseline}`,
|
|
647
|
+
suggestion: 'Fix the copy/include cycle, or choose another baseline',
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const scopeIds = new Set(scope.map((node) => node.id));
|
|
652
|
+
const childrenOf = new Map();
|
|
653
|
+
for (const node of scope) {
|
|
654
|
+
if (node.parentId && scopeIds.has(node.parentId)) {
|
|
655
|
+
childrenOf.set(node.parentId, [...(childrenOf.get(node.parentId) ?? []), node.id]);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
const run = {
|
|
659
|
+
context,
|
|
660
|
+
resolve,
|
|
661
|
+
baseline: input.baseline,
|
|
662
|
+
includeViolations: input.includeViolations,
|
|
663
|
+
childrenOf,
|
|
664
|
+
now: Date.now(),
|
|
665
|
+
};
|
|
666
|
+
const nodes = [];
|
|
667
|
+
for (const node of scope) {
|
|
668
|
+
nodes.push(await describeNode(run, node));
|
|
669
|
+
}
|
|
670
|
+
rollUp(nodes);
|
|
671
|
+
const output = {
|
|
672
|
+
baseline: input.baseline,
|
|
673
|
+
sort: input.sort,
|
|
674
|
+
nodes: sortNodes(nodes, input.sort),
|
|
675
|
+
summary: {
|
|
676
|
+
...summarize(nodes, input.includeViolations),
|
|
677
|
+
unresolved: nodes.filter((node) => node.error).map((node) => node.id),
|
|
678
|
+
},
|
|
679
|
+
};
|
|
680
|
+
return success(output, { reasoning: describeRun(output) });
|
|
681
|
+
},
|
|
682
|
+
examples: [
|
|
683
|
+
{
|
|
684
|
+
description: 'Health of every node, drift against each parent',
|
|
685
|
+
input: {},
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
description: 'The office subtree ranked by drift from the root',
|
|
689
|
+
input: { rootId: 'office', baseline: 'root', sort: 'drift' },
|
|
690
|
+
},
|
|
691
|
+
{
|
|
692
|
+
description: 'How far every node strays from a reference product, without constraint checks',
|
|
693
|
+
input: { baseline: 'office', includeViolations: false },
|
|
694
|
+
},
|
|
695
|
+
],
|
|
696
|
+
};
|
|
697
|
+
//# sourceMappingURL=health.js.map
|