@ontrails/warden 1.0.0-beta.2 → 1.0.0-beta.22

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 (249) hide show
  1. package/CHANGELOG.md +508 -6
  2. package/README.md +77 -26
  3. package/bin/warden.ts +50 -0
  4. package/package.json +27 -5
  5. package/src/adapter-check.ts +136 -0
  6. package/src/ast.ts +28 -0
  7. package/src/cli.ts +1374 -103
  8. package/src/command.ts +953 -0
  9. package/src/config.ts +184 -0
  10. package/src/draft.ts +22 -0
  11. package/src/drift.ts +106 -22
  12. package/src/fix.ts +120 -0
  13. package/src/formatters.ts +79 -9
  14. package/src/guide.ts +245 -0
  15. package/src/index.ts +206 -14
  16. package/src/project-context.ts +163 -0
  17. package/src/resolve.ts +530 -0
  18. package/src/rules/activation-orphan.ts +97 -0
  19. package/src/rules/ast.ts +3176 -85
  20. package/src/rules/circular-refs.ts +154 -0
  21. package/src/rules/composes-declarations.ts +704 -0
  22. package/src/rules/context-no-surface-types.ts +68 -8
  23. package/src/rules/contour-exists.ts +251 -0
  24. package/src/rules/contour-ids.ts +15 -0
  25. package/src/rules/dead-internal-trail.ts +154 -0
  26. package/src/rules/draft-file-marking.ts +160 -0
  27. package/src/rules/draft-visible-debt.ts +87 -0
  28. package/src/rules/error-mapping-completeness.ts +288 -0
  29. package/src/rules/example-valid.ts +401 -0
  30. package/src/rules/fires-declarations.ts +758 -0
  31. package/src/rules/implementation-returns-result.ts +1265 -95
  32. package/src/rules/incomplete-accessor-for-standard-op.ts +272 -0
  33. package/src/rules/incomplete-crud.ts +580 -0
  34. package/src/rules/index.ts +219 -18
  35. package/src/rules/intent-propagation.ts +127 -0
  36. package/src/rules/layer-field-name-drift.ts +96 -0
  37. package/src/rules/metadata.ts +654 -0
  38. package/src/rules/missing-reconcile.ts +98 -0
  39. package/src/rules/missing-visibility.ts +110 -0
  40. package/src/rules/no-destructured-compose.ts +192 -0
  41. package/src/rules/no-dev-permit-in-source.ts +99 -0
  42. package/src/rules/no-direct-implementation-call.ts +7 -7
  43. package/src/rules/no-legacy-layer-imports.ts +211 -0
  44. package/src/rules/no-native-error-result.ts +111 -0
  45. package/src/rules/no-redundant-result-error-wrap.ts +331 -0
  46. package/src/rules/no-retired-cross-vocabulary.ts +194 -0
  47. package/src/rules/no-sync-result-assumption.ts +1134 -99
  48. package/src/rules/no-throw-in-detour-recover.ts +225 -0
  49. package/src/rules/no-throw-in-implementation.ts +10 -9
  50. package/src/rules/no-top-level-surface.ts +389 -0
  51. package/src/rules/on-references-exist.ts +194 -0
  52. package/src/rules/orphaned-signal.ts +150 -0
  53. package/src/rules/owner-projection-parity.ts +146 -0
  54. package/src/rules/permit-governance.ts +25 -0
  55. package/src/rules/public-export-example-coverage.ts +553 -0
  56. package/src/rules/public-internal-deep-imports.ts +517 -0
  57. package/src/rules/public-output-schema.ts +29 -0
  58. package/src/rules/public-union-output-discriminants.ts +150 -0
  59. package/src/rules/read-intent-fires.ts +187 -0
  60. package/src/rules/reference-exists.ts +98 -0
  61. package/src/rules/registry-names.ts +145 -0
  62. package/src/rules/resolved-import-boundary.ts +146 -0
  63. package/src/rules/resource-declarations.ts +704 -0
  64. package/src/rules/resource-exists.ts +179 -0
  65. package/src/rules/resource-id-grammar.ts +65 -0
  66. package/src/rules/resource-mock-coverage.ts +115 -0
  67. package/src/rules/scan.ts +38 -25
  68. package/src/rules/scheduled-destroy-intent.ts +44 -0
  69. package/src/rules/signal-graph-coaching.ts +191 -0
  70. package/src/rules/specs.ts +9 -5
  71. package/src/rules/static-resource-accessor-preference.ts +657 -0
  72. package/src/rules/surface-facet-coherence.ts +370 -0
  73. package/src/rules/trail-versioning-source.ts +1094 -0
  74. package/src/rules/trail-versioning-topo.ts +172 -0
  75. package/src/rules/types.ts +270 -6
  76. package/src/rules/unmaterialized-activation-source.ts +84 -0
  77. package/src/rules/unreachable-detour-shadowing.ts +344 -0
  78. package/src/rules/valid-describe-refs.ts +160 -32
  79. package/src/rules/valid-detour-contract.ts +78 -0
  80. package/src/rules/warden-export-symmetry.ts +533 -0
  81. package/src/rules/warden-rules-use-ast.ts +996 -0
  82. package/src/rules/webhook-route-collision.ts +243 -0
  83. package/src/trails/activation-orphan.trail.ts +84 -0
  84. package/src/trails/circular-refs.trail.ts +29 -0
  85. package/src/trails/composes-declarations.trail.ts +22 -0
  86. package/src/trails/context-no-surface-types.trail.ts +21 -0
  87. package/src/trails/contour-exists.trail.ts +21 -0
  88. package/src/trails/dead-internal-trail.trail.ts +26 -0
  89. package/src/trails/deprecation-without-guidance.trail.ts +21 -0
  90. package/src/trails/draft-file-marking.trail.ts +16 -0
  91. package/src/trails/draft-visible-debt.trail.ts +16 -0
  92. package/src/trails/error-mapping-completeness.trail.ts +29 -0
  93. package/src/trails/example-valid.trail.ts +25 -0
  94. package/src/trails/fires-declarations.trail.ts +23 -0
  95. package/src/trails/fork-without-preserved-blaze.trail.ts +31 -0
  96. package/src/trails/implementation-returns-result.trail.ts +20 -0
  97. package/src/trails/incomplete-accessor-for-standard-op.trail.ts +76 -0
  98. package/src/trails/incomplete-crud.trail.ts +39 -0
  99. package/src/trails/index.ts +78 -0
  100. package/src/trails/intent-propagation.trail.ts +30 -0
  101. package/src/trails/layer-field-name-drift.trail.ts +39 -0
  102. package/src/trails/marker-schema-unsupported.trail.ts +23 -0
  103. package/src/trails/missing-reconcile.trail.ts +33 -0
  104. package/src/trails/missing-visibility.trail.ts +22 -0
  105. package/src/trails/no-destructured-compose.trail.ts +44 -0
  106. package/src/trails/no-dev-permit-in-source.trail.ts +16 -0
  107. package/src/trails/no-direct-implementation-call.trail.ts +16 -0
  108. package/src/trails/no-legacy-layer-imports.trail.ts +41 -0
  109. package/src/trails/no-native-error-result.trail.ts +18 -0
  110. package/src/trails/no-redundant-result-error-wrap.trail.ts +55 -0
  111. package/src/trails/no-retired-cross-vocabulary.trail.ts +42 -0
  112. package/src/trails/no-sync-result-assumption.trail.ts +19 -0
  113. package/src/trails/no-throw-in-detour-recover.trail.ts +24 -0
  114. package/src/trails/no-throw-in-implementation.trail.ts +20 -0
  115. package/src/trails/no-top-level-surface.trail.ts +43 -0
  116. package/src/trails/on-references-exist.trail.ts +21 -0
  117. package/src/trails/orphaned-signal.trail.ts +36 -0
  118. package/src/trails/owner-projection-parity.trail.ts +26 -0
  119. package/src/trails/pending-force.trail.ts +21 -0
  120. package/src/trails/permit-governance.trail.ts +51 -0
  121. package/src/trails/prefer-schema-inference.trail.ts +21 -0
  122. package/src/trails/public-export-example-coverage.trail.ts +16 -0
  123. package/src/trails/public-internal-deep-imports.trail.ts +94 -0
  124. package/src/trails/public-output-schema.trail.ts +55 -0
  125. package/src/trails/public-union-output-discriminants.trail.ts +33 -0
  126. package/src/trails/read-intent-fires.trail.ts +20 -0
  127. package/src/trails/reference-exists.trail.ts +25 -0
  128. package/src/trails/resolved-import-boundary.trail.ts +109 -0
  129. package/src/trails/resource-declarations.trail.ts +25 -0
  130. package/src/trails/resource-exists.trail.ts +27 -0
  131. package/src/trails/resource-id-grammar.trail.ts +39 -0
  132. package/src/trails/resource-mock-coverage.trail.ts +40 -0
  133. package/src/trails/run.ts +162 -0
  134. package/src/trails/scheduled-destroy-intent.trail.ts +56 -0
  135. package/src/trails/schema.ts +194 -0
  136. package/src/trails/signal-graph-coaching.trail.ts +77 -0
  137. package/src/trails/static-resource-accessor-preference.trail.ts +25 -0
  138. package/src/trails/surface-facet-coherence.trail.ts +25 -0
  139. package/src/trails/topo.ts +6 -0
  140. package/src/trails/unmaterialized-activation-source.trail.ts +72 -0
  141. package/src/trails/unreachable-detour-shadowing.trail.ts +45 -0
  142. package/src/trails/valid-describe-refs.trail.ts +18 -0
  143. package/src/trails/valid-detour-contract.trail.ts +71 -0
  144. package/src/trails/version-gap.trail.ts +35 -0
  145. package/src/trails/version-pinned-compose.trail.ts +23 -0
  146. package/src/trails/version-without-examples.trail.ts +38 -0
  147. package/src/trails/warden-export-symmetry.trail.ts +16 -0
  148. package/src/trails/warden-rules-use-ast.trail.ts +45 -0
  149. package/src/trails/webhook-route-collision.trail.ts +50 -0
  150. package/src/trails/wrap-rule.ts +213 -0
  151. package/src/workspaces.ts +238 -0
  152. package/.turbo/turbo-build.log +0 -1
  153. package/.turbo/turbo-lint.log +0 -3
  154. package/.turbo/turbo-typecheck.log +0 -1
  155. package/dist/cli.d.ts +0 -46
  156. package/dist/cli.d.ts.map +0 -1
  157. package/dist/cli.js +0 -221
  158. package/dist/cli.js.map +0 -1
  159. package/dist/drift.d.ts +0 -26
  160. package/dist/drift.d.ts.map +0 -1
  161. package/dist/drift.js +0 -27
  162. package/dist/drift.js.map +0 -1
  163. package/dist/formatters.d.ts +0 -29
  164. package/dist/formatters.d.ts.map +0 -1
  165. package/dist/formatters.js +0 -87
  166. package/dist/formatters.js.map +0 -1
  167. package/dist/index.d.ts +0 -26
  168. package/dist/index.d.ts.map +0 -1
  169. package/dist/index.js +0 -26
  170. package/dist/index.js.map +0 -1
  171. package/dist/rules/ast.d.ts +0 -41
  172. package/dist/rules/ast.d.ts.map +0 -1
  173. package/dist/rules/ast.js +0 -163
  174. package/dist/rules/ast.js.map +0 -1
  175. package/dist/rules/context-no-surface-types.d.ts +0 -12
  176. package/dist/rules/context-no-surface-types.d.ts.map +0 -1
  177. package/dist/rules/context-no-surface-types.js +0 -96
  178. package/dist/rules/context-no-surface-types.js.map +0 -1
  179. package/dist/rules/implementation-returns-result.d.ts +0 -13
  180. package/dist/rules/implementation-returns-result.d.ts.map +0 -1
  181. package/dist/rules/implementation-returns-result.js +0 -231
  182. package/dist/rules/implementation-returns-result.js.map +0 -1
  183. package/dist/rules/index.d.ts +0 -22
  184. package/dist/rules/index.d.ts.map +0 -1
  185. package/dist/rules/index.js +0 -41
  186. package/dist/rules/index.js.map +0 -1
  187. package/dist/rules/no-direct-impl-in-route.d.ts +0 -12
  188. package/dist/rules/no-direct-impl-in-route.d.ts.map +0 -1
  189. package/dist/rules/no-direct-impl-in-route.js +0 -46
  190. package/dist/rules/no-direct-impl-in-route.js.map +0 -1
  191. package/dist/rules/no-direct-implementation-call.d.ts +0 -12
  192. package/dist/rules/no-direct-implementation-call.d.ts.map +0 -1
  193. package/dist/rules/no-direct-implementation-call.js +0 -39
  194. package/dist/rules/no-direct-implementation-call.js.map +0 -1
  195. package/dist/rules/no-sync-result-assumption.d.ts +0 -6
  196. package/dist/rules/no-sync-result-assumption.d.ts.map +0 -1
  197. package/dist/rules/no-sync-result-assumption.js +0 -98
  198. package/dist/rules/no-sync-result-assumption.js.map +0 -1
  199. package/dist/rules/no-throw-in-detour-target.d.ts +0 -12
  200. package/dist/rules/no-throw-in-detour-target.d.ts.map +0 -1
  201. package/dist/rules/no-throw-in-detour-target.js +0 -87
  202. package/dist/rules/no-throw-in-detour-target.js.map +0 -1
  203. package/dist/rules/no-throw-in-implementation.d.ts +0 -9
  204. package/dist/rules/no-throw-in-implementation.d.ts.map +0 -1
  205. package/dist/rules/no-throw-in-implementation.js +0 -34
  206. package/dist/rules/no-throw-in-implementation.js.map +0 -1
  207. package/dist/rules/prefer-schema-inference.d.ts +0 -7
  208. package/dist/rules/prefer-schema-inference.d.ts.map +0 -1
  209. package/dist/rules/prefer-schema-inference.js +0 -86
  210. package/dist/rules/prefer-schema-inference.js.map +0 -1
  211. package/dist/rules/scan.d.ts +0 -8
  212. package/dist/rules/scan.d.ts.map +0 -1
  213. package/dist/rules/scan.js +0 -32
  214. package/dist/rules/scan.js.map +0 -1
  215. package/dist/rules/specs.d.ts +0 -29
  216. package/dist/rules/specs.d.ts.map +0 -1
  217. package/dist/rules/specs.js +0 -192
  218. package/dist/rules/specs.js.map +0 -1
  219. package/dist/rules/structure.d.ts +0 -13
  220. package/dist/rules/structure.d.ts.map +0 -1
  221. package/dist/rules/structure.js +0 -142
  222. package/dist/rules/structure.js.map +0 -1
  223. package/dist/rules/types.d.ts +0 -52
  224. package/dist/rules/types.d.ts.map +0 -1
  225. package/dist/rules/types.js +0 -2
  226. package/dist/rules/types.js.map +0 -1
  227. package/dist/rules/valid-describe-refs.d.ts +0 -7
  228. package/dist/rules/valid-describe-refs.d.ts.map +0 -1
  229. package/dist/rules/valid-describe-refs.js +0 -51
  230. package/dist/rules/valid-describe-refs.js.map +0 -1
  231. package/dist/rules/valid-detour-refs.d.ts +0 -6
  232. package/dist/rules/valid-detour-refs.d.ts.map +0 -1
  233. package/dist/rules/valid-detour-refs.js +0 -116
  234. package/dist/rules/valid-detour-refs.js.map +0 -1
  235. package/src/__tests__/cli.test.ts +0 -198
  236. package/src/__tests__/drift.test.ts +0 -74
  237. package/src/__tests__/formatters.test.ts +0 -157
  238. package/src/__tests__/implementation-returns-result.test.ts +0 -75
  239. package/src/__tests__/no-direct-implementation-call.test.ts +0 -83
  240. package/src/__tests__/no-sync-result-assumption.test.ts +0 -85
  241. package/src/__tests__/no-throw-in-detour-target.test.ts +0 -78
  242. package/src/__tests__/prefer-schema-inference.test.ts +0 -84
  243. package/src/__tests__/rules.test.ts +0 -188
  244. package/src/__tests__/valid-describe-refs.test.ts +0 -60
  245. package/src/rules/no-direct-impl-in-route.ts +0 -77
  246. package/src/rules/no-throw-in-detour-target.ts +0 -150
  247. package/src/rules/valid-detour-refs.ts +0 -187
  248. package/tsconfig.json +0 -9
  249. package/tsconfig.tsbuildinfo +0 -1
package/src/cli.ts CHANGED
@@ -5,38 +5,172 @@
5
5
  * and returns a structured report.
6
6
  */
7
7
 
8
- import { resolve } from 'node:path';
8
+ import { isAbsolute, relative, resolve } from 'node:path';
9
9
 
10
10
  import type { Topo } from '@ontrails/core';
11
+ import { deriveTopoGraph } from '@ontrails/topographer';
12
+ import type { TopoGraph } from '@ontrails/topographer';
13
+ import { getContourReferences } from '@ontrails/core';
11
14
 
15
+ import type {
16
+ EffectiveWardenConfig,
17
+ WardenConfigInput,
18
+ WardenConfigLayer,
19
+ WardenDepth,
20
+ WardenFailOn,
21
+ WardenFormat,
22
+ WardenLockMode,
23
+ } from './config.js';
24
+ import { runWardenAdapterChecks } from './adapter-check.js';
25
+ import { resolveWardenConfig } from './config.js';
26
+ import { isDraftMarkedFile } from './draft.js';
27
+ import { applySafeFixesToSource, hasSafeFixEdits } from './fix.js';
12
28
  import type { DriftResult } from './drift.js';
13
29
  import { checkDrift } from './drift.js';
14
30
  import {
15
- findConfigProperty,
31
+ collectProjectDocumentationImportResolutions,
32
+ collectProjectImportResolutions,
33
+ collectPublicWorkspaces,
34
+ } from './project-context.js';
35
+ import {
36
+ collectContourDefinitionIds,
37
+ collectContourReferenceTargetsByName,
38
+ collectCrudTableIds as collectCrudTableIdsFromAst,
39
+ collectComposeTargetTrailIds,
40
+ collectOnTargetSignalIds as collectOnTargetSignalIdsFromAst,
41
+ collectReconcileTableIds as collectReconcileTableIdsFromAst,
42
+ collectResourceDefinitionIds,
43
+ collectSignalDefinitionIds,
44
+ collectTrailIntentsById,
16
45
  findTrailDefinitions,
17
46
  parse,
18
- walk,
19
47
  } from './rules/ast.js';
20
- import { wardenRules } from './rules/index.js';
48
+ import { collectFileCrudCoverage } from './rules/incomplete-crud.js';
49
+ import { wardenRules, wardenTopoRules } from './rules/index.js';
50
+ import { getWardenRuleMetadata } from './rules/metadata.js';
51
+ import {
52
+ isWardenDevPermitTestScanTarget,
53
+ isWardenSourceScanTarget,
54
+ } from './rules/scan.js';
21
55
  import type {
22
56
  ProjectAwareWardenRule,
23
57
  ProjectContext,
58
+ TopoAwareWardenRule,
24
59
  WardenDiagnostic,
60
+ WardenGuidanceLink,
25
61
  WardenRule,
62
+ WardenRuleTier,
26
63
  } from './rules/types.js';
64
+ import type { WardenImportResolution } from './resolve.js';
65
+
66
+ /**
67
+ * Resolved topo input for Warden runs that govern multiple apps.
68
+ */
69
+ export interface WardenTopoTarget {
70
+ /** Optional precomputed topo graph, including graph-only audit annotations. */
71
+ readonly graph?: TopoGraph | undefined;
72
+ /** Stable app/topo label used to tag topo-aware diagnostics. */
73
+ readonly name?: string | undefined;
74
+ /** Resolved topo module to inspect. */
75
+ readonly topo: Topo;
76
+ }
27
77
 
28
78
  /**
29
- * Options for the warden CLI runner.
79
+ * Options for the shared Warden runner.
30
80
  */
31
- export interface WardenOptions {
81
+ export interface WardenRunOptions {
32
82
  /** Root directory to scan for TypeScript files. Defaults to cwd. */
33
83
  readonly rootDir?: string | undefined;
84
+ /** Warden config section from `trails.config.ts`, if already loaded. */
85
+ readonly config?: WardenConfigInput | undefined;
86
+ /** CLI/config-layer app names carried through shared resolution. */
87
+ readonly apps?: readonly string[] | undefined;
88
+ /** Include shared adapter authoring checks as Warden diagnostics. */
89
+ readonly adapterCheck?: boolean | undefined;
90
+ /** Cumulative analysis depth for the final M1 surfaces. */
91
+ readonly depth?: WardenDepth | undefined;
92
+ /** Draft-state handling mode for final M1 surfaces. */
93
+ readonly drafts?: EffectiveWardenConfig['drafts'] | undefined;
94
+ /** Failure threshold used to compute `report.passed`. */
95
+ readonly failOn?: WardenFailOn | undefined;
96
+ /**
97
+ * Apply safe source fixes among the run's diagnostics, writing changed files.
98
+ *
99
+ * Only `safety: 'safe'` fixes with concrete edits are applied; review-required,
100
+ * edit-less, and topo diagnostics stay reported but unapplied.
101
+ */
102
+ readonly fix?: boolean | undefined;
103
+ /** Output format requested by the caller. */
104
+ readonly format?: WardenFormat | undefined;
105
+ /** Lockfile mode requested by the caller. */
106
+ readonly lock?: WardenLockMode | undefined;
107
+ /** Suppress lockfile mutation for CI/pre-push callers. */
108
+ readonly noLockMutation?: boolean | undefined;
109
+ /** Environment layer for config resolution. Pass `process.env` at process boundaries. */
110
+ readonly env?: Record<string, string | undefined> | undefined;
34
111
  /** Only run lint rules, skip drift detection */
35
112
  readonly lintOnly?: boolean | undefined;
36
113
  /** Only run drift detection, skip lint rules */
37
114
  readonly driftOnly?: boolean | undefined;
38
- /** App topology for drift detection. When provided, enables real surface lock comparison. */
115
+ /**
116
+ * Run a single Warden tier. Defaults to all lint tiers plus drift.
117
+ *
118
+ * Selecting a non-drift tier skips drift detection; selecting `drift` skips
119
+ * lint rule dispatch. `lintOnly` and `driftOnly` remain compatibility shims.
120
+ */
121
+ readonly tier?: WardenRuleTier | undefined;
122
+ /**
123
+ * App topology for drift detection. When provided, enables real topology
124
+ * drift comparison and unlocks the topo-aware rule dispatch path.
125
+ *
126
+ * @remarks
127
+ * Topo-aware rules (both built-in `wardenTopoRules` and `extraTopoRules`)
128
+ * only fire when a `Topo` is supplied. Runs without a topo silently skip
129
+ * topo-aware dispatch — callers that depend on a topo-aware rule firing
130
+ * must pass `topo` explicitly.
131
+ */
39
132
  readonly topo?: Topo | undefined;
133
+ /**
134
+ * Multiple resolved topos to govern in one invocation.
135
+ *
136
+ * Source/project rules run once; topo-aware rules run once per target.
137
+ */
138
+ readonly topos?: readonly WardenTopoTarget[] | undefined;
139
+ /**
140
+ * Extra topo-aware rules to run in addition to the built-in registry.
141
+ *
142
+ * Primarily a test hook — production callers should register rules via
143
+ * `wardenTopoRules` in `rules/index.ts`. These rules are only invoked
144
+ * when `topo` is also supplied (see `topo` remarks).
145
+ */
146
+ readonly extraTopoRules?: readonly TopoAwareWardenRule[] | undefined;
147
+ /**
148
+ * Extra source rules to run in addition to the built-in registry.
149
+ *
150
+ * Primarily a test hook — production callers should register durable rules
151
+ * via `wardenRules` in `rules/index.ts`.
152
+ */
153
+ readonly extraSourceRules?: readonly WardenRule[] | undefined;
154
+ }
155
+
156
+ /** Backwards-compatible name for older consumers. */
157
+ export type WardenOptions = WardenRunOptions;
158
+
159
+ /**
160
+ * Aggregate outcome of a `--fix` pass over a run's diagnostics.
161
+ */
162
+ export interface WardenFixSummary {
163
+ /** Diagnostics whose safe fix was applied to source. */
164
+ readonly applied: number;
165
+ /** Source files rewritten with patched content. */
166
+ readonly filesChanged: number;
167
+ /** Diagnostics carrying fix metadata left unapplied (review or edit-less). */
168
+ readonly skipped: number;
169
+ }
170
+
171
+ export interface WardenFixApplication extends WardenFixSummary {
172
+ /** Diagnostics removed from the final report because their safe fix applied. */
173
+ readonly appliedDiagnostics: readonly WardenDiagnostic[];
40
174
  }
41
175
 
42
176
  /**
@@ -53,33 +187,122 @@ export interface WardenReport {
53
187
  readonly drift: DriftResult | null;
54
188
  /** Whether the warden run passed (no errors, no drift) */
55
189
  readonly passed: boolean;
190
+ /** Effective shared config consumed by this run. */
191
+ readonly effectiveConfig?: EffectiveWardenConfig | undefined;
192
+ /** Resolved topo/app labels governed by this run. */
193
+ readonly topoNames?: readonly string[] | undefined;
194
+ /** Safe-fix application summary, present only when a `--fix` pass ran. */
195
+ readonly fixes?: WardenFixSummary | undefined;
56
196
  }
57
197
 
58
198
  /**
59
- * Collect all .ts files under a directory, excluding node_modules, dist, and .git.
199
+ * Collect Warden scan targets under a directory, excluding generated and test
200
+ * surfaces that should not contribute most committed-source diagnostics.
60
201
  */
61
- const isSourceFile = (match: string): boolean =>
62
- !match.endsWith('.d.ts') &&
63
- !match.startsWith('node_modules/') &&
64
- !match.startsWith('dist/') &&
65
- !match.startsWith('.git/') &&
66
- !match.includes('__tests__/') &&
67
- !match.includes('__test__/') &&
68
- !match.endsWith('.test.ts') &&
69
- !match.endsWith('.spec.ts');
70
-
71
- const collectTsFiles = (dir: string): readonly string[] => {
202
+ const collectFilesMatching = (
203
+ dir: string,
204
+ pattern: string,
205
+ dot = false
206
+ ): readonly string[] => {
207
+ const glob = new Bun.Glob(pattern);
208
+ let matches: IterableIterator<string>;
209
+ try {
210
+ matches = glob.scanSync({ cwd: dir, dot, onlyFiles: true });
211
+ } catch {
212
+ return [];
213
+ }
214
+
215
+ const files: string[] = [];
216
+ for (const match of matches) {
217
+ if (isWardenSourceScanTarget(match)) {
218
+ files.push(`${dir}/${match}`);
219
+ }
220
+ }
221
+ return files;
222
+ };
223
+
224
+ const collectTsFiles = (dir: string): readonly string[] =>
225
+ collectFilesMatching(dir, '**/*.ts');
226
+
227
+ const draftModeIncludesFile = (
228
+ filePath: string,
229
+ drafts: EffectiveWardenConfig['drafts']
230
+ ): boolean => {
231
+ const isDraftFile = isDraftMarkedFile(filePath);
232
+ if (drafts === 'exclude') {
233
+ return !isDraftFile;
234
+ }
235
+ if (drafts === 'only') {
236
+ return isDraftFile;
237
+ }
238
+ return true;
239
+ };
240
+
241
+ const filterSourceFilesByDraftMode = (
242
+ sourceFiles: readonly SourceFile[],
243
+ drafts: EffectiveWardenConfig['drafts']
244
+ ): readonly SourceFile[] =>
245
+ drafts === 'include'
246
+ ? sourceFiles
247
+ : sourceFiles.filter((sourceFile) =>
248
+ draftModeIncludesFile(sourceFile.filePath, drafts)
249
+ );
250
+
251
+ const collectDevPermitTestFiles = (dir: string): readonly string[] => {
72
252
  const glob = new Bun.Glob('**/*.ts');
73
253
  let matches: IterableIterator<string>;
74
254
  try {
75
- matches = glob.scanSync({ cwd: dir, dot: false, onlyFiles: true });
255
+ matches = glob.scanSync({ cwd: dir, onlyFiles: true });
256
+ } catch {
257
+ return [];
258
+ }
259
+
260
+ const files: string[] = [];
261
+ for (const match of matches) {
262
+ if (isWardenDevPermitTestScanTarget(match)) {
263
+ files.push(`${dir}/${match}`);
264
+ }
265
+ }
266
+ return files;
267
+ };
268
+
269
+ const collectTextScanFiles = (dir: string): readonly string[] => [
270
+ ...collectFilesMatching(dir, '**/*.sh', true),
271
+ ...collectFilesMatching(dir, '**/*.bash', true),
272
+ ...collectFilesMatching(dir, '**/*.zsh', true),
273
+ ...collectFilesMatching(dir, '**/*.yml', true),
274
+ ...collectFilesMatching(dir, '**/*.yaml', true),
275
+ ...collectFilesMatching(dir, '**/package.json', true),
276
+ ];
277
+
278
+ const isDocumentationScanTarget = (match: string): boolean => {
279
+ if (match === 'README.md') {
280
+ return true;
281
+ }
282
+ if (/^(?:packages|adapters|apps)\/[^/]+\/README\.md$/.test(match)) {
283
+ return true;
284
+ }
285
+ return (
286
+ match.startsWith('docs/') &&
287
+ !match.startsWith('docs/adr/') &&
288
+ !match.startsWith('docs/migration/') &&
289
+ !match.startsWith('docs/releases/') &&
290
+ match.endsWith('.md')
291
+ );
292
+ };
293
+
294
+ const collectDocumentationFiles = (dir: string): readonly string[] => {
295
+ const glob = new Bun.Glob('**/*.md');
296
+ let matches: IterableIterator<string>;
297
+ try {
298
+ matches = glob.scanSync({ cwd: dir, onlyFiles: true });
76
299
  } catch {
77
300
  return [];
78
301
  }
79
302
 
80
303
  const files: string[] = [];
81
304
  for (const match of matches) {
82
- if (isSourceFile(match)) {
305
+ if (isWardenSourceScanTarget(match) && isDocumentationScanTarget(match)) {
83
306
  files.push(`${dir}/${match}`);
84
307
  }
85
308
  }
@@ -88,9 +311,131 @@ const collectTsFiles = (dir: string): readonly string[] => {
88
311
 
89
312
  interface SourceFile {
90
313
  readonly filePath: string;
314
+ readonly kind: 'documentation' | 'text' | 'typescript';
91
315
  readonly sourceCode: string;
92
316
  }
93
317
 
318
+ interface MutableProjectContext {
319
+ contourReferencesByName: Map<string, Set<string>>;
320
+ crudTableIds: Set<string>;
321
+ composeTargetTrailIds: Set<string>;
322
+ crudCoverageByEntity: Map<string, Set<string>>;
323
+ knownContourIds: Set<string>;
324
+ knownResourceIds: Set<string>;
325
+ knownSignalIds: Set<string>;
326
+ knownTrailIds: Set<string>;
327
+ importResolutionsByFile: Map<string, readonly WardenImportResolution[]>;
328
+ documentedImportResolutionsByFile: Map<
329
+ string,
330
+ readonly WardenImportResolution[]
331
+ >;
332
+ onTargetSignalIds: Set<string>;
333
+ publicWorkspaces: ReturnType<typeof collectPublicWorkspaces>;
334
+ reconcileTableIds: Set<string>;
335
+ trailIntentsById: Map<string, 'destroy' | 'read' | 'write'>;
336
+ }
337
+
338
+ const createMutableProjectContext = (): MutableProjectContext => ({
339
+ composeTargetTrailIds: new Set<string>(),
340
+ contourReferencesByName: new Map<string, Set<string>>(),
341
+ crudCoverageByEntity: new Map<string, Set<string>>(),
342
+ crudTableIds: new Set<string>(),
343
+ documentedImportResolutionsByFile: new Map<
344
+ string,
345
+ readonly WardenImportResolution[]
346
+ >(),
347
+ importResolutionsByFile: new Map<string, readonly WardenImportResolution[]>(),
348
+ knownContourIds: new Set<string>(),
349
+ knownResourceIds: new Set<string>(),
350
+ knownSignalIds: new Set<string>(),
351
+ knownTrailIds: new Set<string>(),
352
+ onTargetSignalIds: new Set<string>(),
353
+ publicWorkspaces: new Map(),
354
+ reconcileTableIds: new Set<string>(),
355
+ trailIntentsById: new Map<string, 'destroy' | 'read' | 'write'>(),
356
+ });
357
+
358
+ const addContourReferenceTargets = (
359
+ context: MutableProjectContext,
360
+ contourName: string,
361
+ targets: readonly string[]
362
+ ): void => {
363
+ const existing = context.contourReferencesByName.get(contourName);
364
+ if (existing) {
365
+ for (const target of targets) {
366
+ existing.add(target);
367
+ }
368
+ return;
369
+ }
370
+
371
+ context.contourReferencesByName.set(contourName, new Set(targets));
372
+ };
373
+
374
+ const toProjectContext = (context: MutableProjectContext): ProjectContext => ({
375
+ ...(context.contourReferencesByName.size > 0
376
+ ? {
377
+ contourReferencesByName: new Map(
378
+ [...context.contourReferencesByName.entries()].map(
379
+ ([name, targets]) => [name, [...targets]]
380
+ )
381
+ ),
382
+ }
383
+ : {}),
384
+ ...(context.crudTableIds.size > 0
385
+ ? { crudTableIds: context.crudTableIds }
386
+ : {}),
387
+ ...(context.crudCoverageByEntity.size > 0
388
+ ? {
389
+ crudCoverageByEntity: new Map(
390
+ [...context.crudCoverageByEntity.entries()].map(
391
+ ([entityId, operations]) => [
392
+ entityId,
393
+ new Set(operations) as ReadonlySet<string>,
394
+ ]
395
+ )
396
+ ),
397
+ }
398
+ : {}),
399
+ composeTargetTrailIds: context.composeTargetTrailIds,
400
+ knownContourIds: context.knownContourIds,
401
+ knownResourceIds: context.knownResourceIds,
402
+ knownSignalIds: context.knownSignalIds,
403
+ knownTrailIds: context.knownTrailIds,
404
+ ...(context.importResolutionsByFile.size > 0
405
+ ? { importResolutionsByFile: context.importResolutionsByFile }
406
+ : {}),
407
+ ...(context.documentedImportResolutionsByFile.size > 0
408
+ ? {
409
+ documentedImportResolutionsByFile:
410
+ context.documentedImportResolutionsByFile,
411
+ }
412
+ : {}),
413
+ ...(context.onTargetSignalIds.size > 0
414
+ ? { onTargetSignalIds: context.onTargetSignalIds }
415
+ : {}),
416
+ ...(context.publicWorkspaces.size > 0
417
+ ? { publicWorkspaces: context.publicWorkspaces }
418
+ : {}),
419
+ ...(context.reconcileTableIds.size > 0
420
+ ? { reconcileTableIds: context.reconcileTableIds }
421
+ : {}),
422
+ trailIntentsById: context.trailIntentsById,
423
+ });
424
+
425
+ const collectKnownContourIds = (
426
+ sourceCode: string,
427
+ filePath: string,
428
+ knownContourIds: Set<string>
429
+ ): void => {
430
+ const ast = parse(filePath, sourceCode);
431
+ if (!ast) {
432
+ return;
433
+ }
434
+ for (const id of collectContourDefinitionIds(ast)) {
435
+ knownContourIds.add(id);
436
+ }
437
+ };
438
+
94
439
  const collectKnownTrailIds = (
95
440
  sourceCode: string,
96
441
  filePath: string,
@@ -105,30 +450,122 @@ const collectKnownTrailIds = (
105
450
  }
106
451
  };
107
452
 
108
- const collectDetourTargetTrailIds = (
453
+ const collectComposedTrailIds = (
109
454
  sourceCode: string,
110
455
  filePath: string,
111
- detourTargetTrailIds: Set<string>
456
+ composeTargetTrailIds: Set<string>
112
457
  ): void => {
113
458
  const ast = parse(filePath, sourceCode);
114
459
  if (!ast) {
115
460
  return;
116
461
  }
117
- for (const def of findTrailDefinitions(ast)) {
118
- const detoursProp = findConfigProperty(def.config, 'detours');
119
- if (!detoursProp) {
120
- continue;
462
+ for (const id of collectComposeTargetTrailIds(ast, sourceCode)) {
463
+ composeTargetTrailIds.add(id);
464
+ }
465
+ };
466
+
467
+ const collectKnownResourceIds = (
468
+ sourceCode: string,
469
+ filePath: string,
470
+ knownResourceIds: Set<string>
471
+ ): void => {
472
+ const ast = parse(filePath, sourceCode);
473
+ if (!ast) {
474
+ return;
475
+ }
476
+ for (const id of collectResourceDefinitionIds(ast)) {
477
+ knownResourceIds.add(id);
478
+ }
479
+ };
480
+
481
+ const collectKnownSignalIds = (
482
+ sourceCode: string,
483
+ filePath: string,
484
+ knownSignalIds: Set<string>
485
+ ): void => {
486
+ const ast = parse(filePath, sourceCode);
487
+ if (!ast) {
488
+ return;
489
+ }
490
+ for (const id of collectSignalDefinitionIds(ast)) {
491
+ knownSignalIds.add(id);
492
+ }
493
+ };
494
+
495
+ const collectTrailIntents = (
496
+ sourceCode: string,
497
+ filePath: string,
498
+ trailIntentsById: Map<string, 'destroy' | 'read' | 'write'>
499
+ ): void => {
500
+ const ast = parse(filePath, sourceCode);
501
+ if (!ast) {
502
+ return;
503
+ }
504
+ for (const [id, intent] of collectTrailIntentsById(ast)) {
505
+ trailIntentsById.set(id, intent);
506
+ }
507
+ };
508
+
509
+ const collectCrudTableIds = (
510
+ sourceCode: string,
511
+ filePath: string,
512
+ crudTableIds: Set<string>
513
+ ): void => {
514
+ const ast = parse(filePath, sourceCode);
515
+ if (!ast) {
516
+ return;
517
+ }
518
+ for (const id of collectCrudTableIdsFromAst(ast)) {
519
+ crudTableIds.add(id);
520
+ }
521
+ };
522
+
523
+ const collectOnTargetSignalIds = (
524
+ sourceCode: string,
525
+ filePath: string,
526
+ onTargetSignalIds: Set<string>
527
+ ): void => {
528
+ const ast = parse(filePath, sourceCode);
529
+ if (!ast) {
530
+ return;
531
+ }
532
+ for (const id of collectOnTargetSignalIdsFromAst(ast, sourceCode)) {
533
+ onTargetSignalIds.add(id);
534
+ }
535
+ };
536
+
537
+ const collectCrudCoverageByEntity = (
538
+ sourceCode: string,
539
+ filePath: string,
540
+ coverageByEntity: Map<string, Set<string>>
541
+ ): void => {
542
+ const ast = parse(filePath, sourceCode);
543
+ if (!ast) {
544
+ return;
545
+ }
546
+ for (const [entityId, operations] of collectFileCrudCoverage(
547
+ ast,
548
+ sourceCode
549
+ )) {
550
+ const bucket = coverageByEntity.get(entityId) ?? new Set<string>();
551
+ for (const operation of operations) {
552
+ bucket.add(operation);
121
553
  }
122
- // Walk the detours value for string literals that look like trail IDs
123
- walk(detoursProp, (node) => {
124
- if (node.type !== 'Literal') {
125
- return;
126
- }
127
- const val = (node as unknown as { value?: string }).value;
128
- if (val && val.includes('.')) {
129
- detourTargetTrailIds.add(val);
130
- }
131
- });
554
+ coverageByEntity.set(entityId, bucket);
555
+ }
556
+ };
557
+
558
+ const collectReconcileTableIds = (
559
+ sourceCode: string,
560
+ filePath: string,
561
+ reconcileTableIds: Set<string>
562
+ ): void => {
563
+ const ast = parse(filePath, sourceCode);
564
+ if (!ast) {
565
+ return;
566
+ }
567
+ for (const id of collectReconcileTableIdsFromAst(ast)) {
568
+ reconcileTableIds.add(id);
132
569
  }
133
570
  };
134
571
 
@@ -141,6 +578,7 @@ const loadSourceFiles = async (
141
578
  try {
142
579
  sourceFiles.push({
143
580
  filePath,
581
+ kind: 'typescript',
144
582
  sourceCode: await Bun.file(filePath).text(),
145
583
  });
146
584
  } catch {
@@ -148,78 +586,461 @@ const loadSourceFiles = async (
148
586
  }
149
587
  }
150
588
 
151
- return sourceFiles;
152
- };
589
+ for (const filePath of collectTextScanFiles(rootDir)) {
590
+ try {
591
+ sourceFiles.push({
592
+ filePath,
593
+ kind: 'text',
594
+ sourceCode: await Bun.file(filePath).text(),
595
+ });
596
+ } catch {
597
+ continue;
598
+ }
599
+ }
153
600
 
154
- const buildProjectContextFromTopo = (appTopo: Topo): ProjectContext => {
155
- const knownTrailIds = new Set<string>([
156
- ...appTopo.trails.keys(),
157
- ...appTopo.hikes.keys(),
158
- ]);
601
+ for (const filePath of collectDocumentationFiles(rootDir)) {
602
+ try {
603
+ sourceFiles.push({
604
+ filePath,
605
+ kind: 'documentation',
606
+ sourceCode: await Bun.file(filePath).text(),
607
+ });
608
+ } catch {
609
+ continue;
610
+ }
611
+ }
159
612
 
160
- const detourTargetTrailIds = new Set<string>();
161
- for (const t of appTopo.trails.values()) {
162
- const detours = (t as unknown as Record<string, unknown>)['detours'] as
163
- | Readonly<Record<string, readonly string[]>>
164
- | undefined;
165
- if (!detours) {
613
+ for (const filePath of collectDevPermitTestFiles(rootDir)) {
614
+ try {
615
+ sourceFiles.push({
616
+ filePath,
617
+ kind: 'text',
618
+ sourceCode: await Bun.file(filePath).text(),
619
+ });
620
+ } catch {
166
621
  continue;
167
622
  }
168
- for (const targets of Object.values(detours)) {
169
- for (const id of targets) {
170
- detourTargetTrailIds.add(id);
171
- }
623
+ }
624
+
625
+ return sourceFiles;
626
+ };
627
+
628
+ const collectTopoKnownIds = (
629
+ appTopo: Topo,
630
+ context: MutableProjectContext
631
+ ): void => {
632
+ for (const name of appTopo.contours.keys()) {
633
+ context.knownContourIds.add(name);
634
+ }
635
+
636
+ for (const id of appTopo.trails.keys()) {
637
+ context.knownTrailIds.add(id);
638
+ }
639
+
640
+ for (const id of appTopo.resources.keys()) {
641
+ context.knownResourceIds.add(id);
642
+ }
643
+
644
+ for (const id of appTopo.signals.keys()) {
645
+ context.knownSignalIds.add(id);
646
+ }
647
+ };
648
+
649
+ const collectTopoComposesAndIntents = (
650
+ appTopo: Topo,
651
+ context: MutableProjectContext
652
+ ): void => {
653
+ for (const trail of appTopo.trails.values()) {
654
+ context.trailIntentsById.set(trail.id, trail.intent);
655
+ for (const composedTrailId of trail.composes) {
656
+ context.composeTargetTrailIds.add(composedTrailId);
172
657
  }
173
658
  }
659
+ };
660
+
661
+ const collectTopoContourReferences = (
662
+ appTopo: Topo,
663
+ context: MutableProjectContext
664
+ ): void => {
665
+ for (const contour of appTopo.listContours()) {
666
+ addContourReferenceTargets(
667
+ context,
668
+ contour.name,
669
+ getContourReferences(contour).map((reference) => reference.contour)
670
+ );
671
+ }
672
+ };
673
+
674
+ const collectTopoTrailContext = (
675
+ appTopo: Topo,
676
+ context: MutableProjectContext
677
+ ): void => {
678
+ collectTopoKnownIds(appTopo, context);
679
+ collectTopoComposesAndIntents(appTopo, context);
680
+ collectTopoContourReferences(appTopo, context);
681
+ };
682
+
683
+ const collectFileKnownIds = (
684
+ sourceFile: SourceFile,
685
+ context: MutableProjectContext
686
+ ): void => {
687
+ collectKnownContourIds(
688
+ sourceFile.sourceCode,
689
+ sourceFile.filePath,
690
+ context.knownContourIds
691
+ );
692
+ collectKnownTrailIds(
693
+ sourceFile.sourceCode,
694
+ sourceFile.filePath,
695
+ context.knownTrailIds
696
+ );
697
+ collectKnownResourceIds(
698
+ sourceFile.sourceCode,
699
+ sourceFile.filePath,
700
+ context.knownResourceIds
701
+ );
702
+ collectKnownSignalIds(
703
+ sourceFile.sourceCode,
704
+ sourceFile.filePath,
705
+ context.knownSignalIds
706
+ );
707
+ };
708
+
709
+ const collectFileTrailRelationships = (
710
+ sourceFile: SourceFile,
711
+ context: MutableProjectContext
712
+ ): void => {
713
+ collectComposedTrailIds(
714
+ sourceFile.sourceCode,
715
+ sourceFile.filePath,
716
+ context.composeTargetTrailIds
717
+ );
718
+ collectTrailIntents(
719
+ sourceFile.sourceCode,
720
+ sourceFile.filePath,
721
+ context.trailIntentsById
722
+ );
723
+ };
724
+
725
+ const collectFileSupplementalProjectContext = (
726
+ sourceFile: SourceFile,
727
+ context: MutableProjectContext
728
+ ): void => {
729
+ collectCrudTableIds(
730
+ sourceFile.sourceCode,
731
+ sourceFile.filePath,
732
+ context.crudTableIds
733
+ );
734
+ collectOnTargetSignalIds(
735
+ sourceFile.sourceCode,
736
+ sourceFile.filePath,
737
+ context.onTargetSignalIds
738
+ );
739
+ collectReconcileTableIds(
740
+ sourceFile.sourceCode,
741
+ sourceFile.filePath,
742
+ context.reconcileTableIds
743
+ );
744
+ collectCrudCoverageByEntity(
745
+ sourceFile.sourceCode,
746
+ sourceFile.filePath,
747
+ context.crudCoverageByEntity
748
+ );
749
+ };
750
+
751
+ const collectFileProjectContext = (
752
+ sourceFile: SourceFile,
753
+ context: MutableProjectContext
754
+ ): void => {
755
+ collectFileKnownIds(sourceFile, context);
756
+ collectFileTrailRelationships(sourceFile, context);
757
+ collectFileSupplementalProjectContext(sourceFile, context);
758
+ };
759
+
760
+ const collectFileContourReferences = (
761
+ sourceFile: SourceFile,
762
+ context: MutableProjectContext
763
+ ): void => {
764
+ const ast = parse(sourceFile.filePath, sourceFile.sourceCode);
765
+ if (!ast) {
766
+ return;
767
+ }
768
+
769
+ const referencesByName = collectContourReferenceTargetsByName(
770
+ ast,
771
+ context.knownContourIds
772
+ );
773
+ for (const [contourName, targets] of referencesByName) {
774
+ addContourReferenceTargets(context, contourName, targets);
775
+ }
776
+ };
174
777
 
175
- return { detourTargetTrailIds, knownTrailIds };
778
+ const collectFileImportResolutions = (
779
+ rootDir: string,
780
+ sourceFiles: readonly SourceFile[],
781
+ context: MutableProjectContext
782
+ ): void => {
783
+ const resolutionsByFile = collectProjectImportResolutions({
784
+ rootDir,
785
+ sourceFiles,
786
+ });
787
+ for (const [filePath, resolutions] of resolutionsByFile) {
788
+ context.importResolutionsByFile.set(filePath, resolutions);
789
+ }
790
+ };
791
+
792
+ const collectFileDocumentedImportResolutions = (
793
+ rootDir: string,
794
+ sourceFiles: readonly SourceFile[],
795
+ context: MutableProjectContext
796
+ ): void => {
797
+ const resolutionsByFile = collectProjectDocumentationImportResolutions({
798
+ rootDir,
799
+ sourceFiles,
800
+ });
801
+ for (const [filePath, resolutions] of resolutionsByFile) {
802
+ context.documentedImportResolutionsByFile.set(filePath, resolutions);
803
+ }
176
804
  };
177
805
 
178
- const buildProjectContextFromFiles = (
179
- sourceFiles: readonly SourceFile[]
806
+ const buildProjectContext = (
807
+ sourceFiles: readonly SourceFile[],
808
+ rootDir: string,
809
+ appTopos: readonly Topo[] = []
180
810
  ): ProjectContext => {
181
- const knownTrailIds = new Set<string>();
182
- const detourTargetTrailIds = new Set<string>();
811
+ const context = createMutableProjectContext();
812
+ const typeScriptSourceFiles = sourceFiles.filter(
813
+ (sourceFile) => sourceFile.kind === 'typescript'
814
+ );
815
+ const documentationSourceFiles = sourceFiles.filter(
816
+ (sourceFile) => sourceFile.kind === 'documentation'
817
+ );
818
+ context.publicWorkspaces = collectPublicWorkspaces(rootDir);
183
819
 
184
- for (const sourceFile of sourceFiles) {
185
- collectKnownTrailIds(
186
- sourceFile.sourceCode,
187
- sourceFile.filePath,
188
- knownTrailIds
189
- );
190
- collectDetourTargetTrailIds(
191
- sourceFile.sourceCode,
192
- sourceFile.filePath,
193
- detourTargetTrailIds
194
- );
820
+ if (appTopos.length > 0) {
821
+ for (const appTopo of appTopos) {
822
+ collectTopoTrailContext(appTopo, context);
823
+ }
824
+ for (const sourceFile of typeScriptSourceFiles) {
825
+ collectFileSupplementalProjectContext(sourceFile, context);
826
+ }
827
+ } else {
828
+ for (const sourceFile of typeScriptSourceFiles) {
829
+ collectFileProjectContext(sourceFile, context);
830
+ }
195
831
  }
196
832
 
197
- return {
198
- detourTargetTrailIds,
199
- knownTrailIds,
200
- };
833
+ for (const sourceFile of typeScriptSourceFiles) {
834
+ collectFileContourReferences(sourceFile, context);
835
+ }
836
+ collectFileImportResolutions(rootDir, typeScriptSourceFiles, context);
837
+ collectFileDocumentedImportResolutions(
838
+ rootDir,
839
+ documentationSourceFiles,
840
+ context
841
+ );
842
+
843
+ return toProjectContext(context);
201
844
  };
202
845
 
203
846
  const isProjectAwareRule = (rule: WardenRule): rule is ProjectAwareWardenRule =>
204
847
  'checkWithContext' in rule;
205
848
 
849
+ const createOptionsDiagnostic = (message: string): WardenDiagnostic => ({
850
+ filePath: '<warden-options>',
851
+ line: 1,
852
+ message,
853
+ rule: 'warden-options',
854
+ severity: 'error',
855
+ });
856
+
857
+ interface WardenRuleSelector {
858
+ readonly depth?: WardenDepth | undefined;
859
+ readonly tier?: WardenRuleTier | undefined;
860
+ }
861
+
862
+ const depthIncludesTier = (
863
+ depth: WardenDepth,
864
+ tier: WardenRuleTier
865
+ ): boolean => {
866
+ switch (depth) {
867
+ case 'source': {
868
+ return tier === 'source-static';
869
+ }
870
+ case 'project': {
871
+ return tier === 'source-static' || tier === 'project-static';
872
+ }
873
+ case 'topo': {
874
+ return (
875
+ tier === 'source-static' ||
876
+ tier === 'project-static' ||
877
+ tier === 'topo-aware'
878
+ );
879
+ }
880
+ case 'all': {
881
+ return true;
882
+ }
883
+ default: {
884
+ return false;
885
+ }
886
+ }
887
+ };
888
+
889
+ const ruleMatchesTier = (
890
+ metadata: ReturnType<typeof getWardenRuleMetadata>,
891
+ tier: WardenRuleTier | undefined
892
+ ): boolean => {
893
+ if (!tier) {
894
+ return true;
895
+ }
896
+
897
+ if (!metadata) {
898
+ return false;
899
+ }
900
+
901
+ return tier === 'advisory'
902
+ ? metadata.scope === 'advisory'
903
+ : metadata.tier === tier;
904
+ };
905
+
906
+ const ruleMatchesDepth = (
907
+ metadata: ReturnType<typeof getWardenRuleMetadata>,
908
+ depth: WardenDepth | undefined
909
+ ): boolean => {
910
+ if (!depth) {
911
+ return true;
912
+ }
913
+
914
+ if (!metadata) {
915
+ return false;
916
+ }
917
+
918
+ if (metadata.scope === 'advisory') {
919
+ return depth === 'all';
920
+ }
921
+
922
+ return depthIncludesTier(depth, metadata.tier);
923
+ };
924
+
925
+ const isSelectedRule = (
926
+ rule: WardenRule | TopoAwareWardenRule,
927
+ selector: WardenRuleSelector
928
+ ): boolean => {
929
+ const metadata = getWardenRuleMetadata(rule);
930
+ return selector.tier
931
+ ? ruleMatchesTier(metadata, selector.tier)
932
+ : ruleMatchesDepth(metadata, selector.depth);
933
+ };
934
+
935
+ const isSelectedTopoRule = (
936
+ rule: TopoAwareWardenRule,
937
+ selector: WardenRuleSelector
938
+ ): boolean => {
939
+ const metadata = getWardenRuleMetadata(rule);
940
+ if (selector.tier) {
941
+ return metadata
942
+ ? ruleMatchesTier(metadata, selector.tier)
943
+ : selector.tier === 'topo-aware';
944
+ }
945
+
946
+ return metadata ? ruleMatchesDepth(metadata, selector.depth) : true;
947
+ };
948
+
949
+ const withDiagnosticGuidance = (
950
+ diagnostic: WardenDiagnostic
951
+ ): WardenDiagnostic => {
952
+ if (diagnostic.guidance !== undefined) {
953
+ return diagnostic;
954
+ }
955
+
956
+ const guidance = getWardenRuleMetadata(diagnostic.rule)?.guidance;
957
+ return guidance === undefined ? diagnostic : { ...diagnostic, guidance };
958
+ };
959
+
960
+ const topoRuleFailureDiagnostic = (
961
+ rule: TopoAwareWardenRule,
962
+ error: unknown
963
+ ): WardenDiagnostic => {
964
+ const cause = error instanceof Error ? error : new Error(String(error));
965
+ return {
966
+ filePath: '<topo>',
967
+ line: 1,
968
+ message: `Topo-aware rule "${rule.name}" threw: ${cause.message}`,
969
+ rule: rule.name,
970
+ severity: 'error',
971
+ };
972
+ };
973
+
206
974
  /**
207
- * Lint all files against all warden rules.
975
+ * Run all registered topo-aware rules against the resolved topo.
976
+ *
977
+ * Topo-aware rules fire exactly once per run (not per file) because they
978
+ * inspect the compiled trail graph, not source text.
208
979
  */
209
- const lintFiles = async (
210
- rootDir: string,
211
- appTopo?: Topo | undefined
212
- ): Promise<WardenDiagnostic[]> => {
213
- const allDiagnostics: WardenDiagnostic[] = [];
214
- const sourceFiles = await loadSourceFiles(rootDir);
215
- const context = appTopo
216
- ? buildProjectContextFromTopo(appTopo)
217
- : buildProjectContextFromFiles(sourceFiles);
980
+ const lintTopo = async (
981
+ appTopo: Topo,
982
+ graph: TopoGraph | undefined,
983
+ extraTopoRules: readonly TopoAwareWardenRule[],
984
+ selector: WardenRuleSelector
985
+ ): Promise<readonly WardenDiagnostic[]> => {
986
+ const diagnostics: WardenDiagnostic[] = [];
987
+ const rules: readonly TopoAwareWardenRule[] = [
988
+ ...wardenTopoRules.values(),
989
+ ...extraTopoRules,
990
+ ].filter((rule) => isSelectedTopoRule(rule, selector));
991
+ let contextGraph: TopoGraph;
992
+ try {
993
+ contextGraph = graph ?? deriveTopoGraph(appTopo);
994
+ } catch (error) {
995
+ for (const rule of rules) {
996
+ diagnostics.push(topoRuleFailureDiagnostic(rule, error));
997
+ }
998
+ return diagnostics;
999
+ }
1000
+
1001
+ for (const rule of rules) {
1002
+ try {
1003
+ diagnostics.push(
1004
+ ...(await rule.checkTopo(appTopo, { graph: contextGraph }))
1005
+ );
1006
+ } catch (error) {
1007
+ diagnostics.push(topoRuleFailureDiagnostic(rule, error));
1008
+ }
1009
+ }
1010
+ return diagnostics;
1011
+ };
218
1012
 
1013
+ const lintSourceFiles = (
1014
+ sourceFiles: readonly SourceFile[],
1015
+ context: ProjectContext,
1016
+ extraSourceRules: readonly WardenRule[],
1017
+ selector: WardenRuleSelector
1018
+ ): readonly WardenDiagnostic[] => {
1019
+ const diagnostics: WardenDiagnostic[] = [];
1020
+ const rules = [...wardenRules.values(), ...extraSourceRules];
219
1021
  for (const sourceFile of sourceFiles) {
220
- for (const rule of wardenRules.values()) {
1022
+ for (const rule of rules) {
1023
+ if (
1024
+ sourceFile.kind === 'text' &&
1025
+ rule.name !== 'no-dev-permit-in-source' &&
1026
+ rule.name !== 'public-internal-deep-imports'
1027
+ ) {
1028
+ continue;
1029
+ }
1030
+
1031
+ if (
1032
+ sourceFile.kind === 'documentation' &&
1033
+ rule.name !== 'public-internal-deep-imports'
1034
+ ) {
1035
+ continue;
1036
+ }
1037
+
1038
+ if (!isSelectedRule(rule, selector)) {
1039
+ continue;
1040
+ }
1041
+
221
1042
  if (isProjectAwareRule(rule)) {
222
- allDiagnostics.push(
1043
+ diagnostics.push(
223
1044
  ...rule.checkWithContext(
224
1045
  sourceFile.sourceCode,
225
1046
  sourceFile.filePath,
@@ -228,44 +1049,467 @@ const lintFiles = async (
228
1049
  );
229
1050
  continue;
230
1051
  }
231
-
232
- allDiagnostics.push(
1052
+ diagnostics.push(
233
1053
  ...rule.check(sourceFile.sourceCode, sourceFile.filePath)
234
1054
  );
235
1055
  }
236
1056
  }
1057
+ return diagnostics;
1058
+ };
1059
+
1060
+ const tagTopoDiagnostic = (
1061
+ diagnostic: WardenDiagnostic,
1062
+ topoName: string | undefined
1063
+ ): WardenDiagnostic =>
1064
+ topoName === undefined ? diagnostic : { ...diagnostic, topoName };
1065
+
1066
+ const lintTopoTargets = async (
1067
+ topoTargets: readonly WardenTopoTarget[],
1068
+ extraTopoRules: readonly TopoAwareWardenRule[],
1069
+ selector: WardenRuleSelector,
1070
+ tagDiagnostics: boolean
1071
+ ): Promise<readonly WardenDiagnostic[]> => {
1072
+ const diagnostics: WardenDiagnostic[] = [];
1073
+
1074
+ for (const target of topoTargets) {
1075
+ const topoDiagnostics = await lintTopo(
1076
+ target.topo,
1077
+ target.graph,
1078
+ extraTopoRules,
1079
+ selector
1080
+ );
1081
+ const topoName = target.name ?? target.topo.name;
1082
+ diagnostics.push(
1083
+ ...(tagDiagnostics
1084
+ ? topoDiagnostics.map((diagnostic) =>
1085
+ tagTopoDiagnostic(diagnostic, topoName)
1086
+ )
1087
+ : topoDiagnostics)
1088
+ );
1089
+ }
1090
+
1091
+ return diagnostics;
1092
+ };
1093
+
1094
+ const selectorIncludesTopoRules = (selector: WardenRuleSelector): boolean => {
1095
+ if (selector.tier) {
1096
+ return selector.tier === 'advisory';
1097
+ }
1098
+
1099
+ return !selector.depth || depthIncludesTier(selector.depth, 'topo-aware');
1100
+ };
1101
+
1102
+ /**
1103
+ * Lint all files against all warden rules.
1104
+ */
1105
+ interface WardenLintResult {
1106
+ readonly diagnostics: readonly WardenDiagnostic[];
1107
+ readonly sourceFiles: readonly SourceFile[];
1108
+ }
1109
+
1110
+ const lintFiles = async (
1111
+ rootDir: string,
1112
+ drafts: EffectiveWardenConfig['drafts'],
1113
+ topoTargets: readonly WardenTopoTarget[],
1114
+ extraTopoRules: readonly TopoAwareWardenRule[],
1115
+ extraSourceRules: readonly WardenRule[],
1116
+ selector: WardenRuleSelector
1117
+ ): Promise<WardenLintResult> => {
1118
+ if (selector.tier === 'topo-aware') {
1119
+ return {
1120
+ diagnostics: [
1121
+ ...(await lintTopoTargets(topoTargets, extraTopoRules, selector, true)),
1122
+ ],
1123
+ sourceFiles: [],
1124
+ };
1125
+ }
1126
+
1127
+ const sourceFiles = filterSourceFilesByDraftMode(
1128
+ await loadSourceFiles(rootDir),
1129
+ drafts
1130
+ );
1131
+ const context = buildProjectContext(
1132
+ sourceFiles,
1133
+ rootDir,
1134
+ topoTargets.map((target) => target.topo)
1135
+ );
1136
+ const allDiagnostics: WardenDiagnostic[] = [
1137
+ ...lintSourceFiles(sourceFiles, context, extraSourceRules, selector),
1138
+ ];
1139
+
1140
+ if (
1141
+ topoTargets.length > 0 &&
1142
+ (selector.tier === undefined || selector.tier === 'advisory') &&
1143
+ selectorIncludesTopoRules(selector)
1144
+ ) {
1145
+ allDiagnostics.push(
1146
+ ...(await lintTopoTargets(
1147
+ topoTargets,
1148
+ extraTopoRules,
1149
+ selector,
1150
+ topoTargets.length > 1
1151
+ ))
1152
+ );
1153
+ }
1154
+
1155
+ return { diagnostics: allDiagnostics, sourceFiles };
1156
+ };
1157
+
1158
+ const topoTargetsFromOptions = (
1159
+ options: WardenRunOptions
1160
+ ): readonly WardenTopoTarget[] => {
1161
+ if (options.topos !== undefined && options.topos.length > 0) {
1162
+ return options.topos;
1163
+ }
1164
+
1165
+ return options.topo ? [{ name: options.topo.name, topo: options.topo }] : [];
1166
+ };
1167
+
1168
+ const aggregateDriftHash = (
1169
+ topoTargets: readonly WardenTopoTarget[],
1170
+ driftResults: readonly DriftResult[]
1171
+ ): string => {
1172
+ const currentHashes = new Set(
1173
+ driftResults.map((result) => result.currentHash)
1174
+ );
1175
+ const [onlyHash] = currentHashes;
1176
+ if (currentHashes.size === 1 && onlyHash !== undefined) {
1177
+ return onlyHash;
1178
+ }
1179
+
1180
+ const payload = driftResults
1181
+ .map((result, index) => {
1182
+ const target = topoTargets[index];
1183
+ return {
1184
+ currentHash: result.currentHash,
1185
+ topoName: target?.name ?? target?.topo.name ?? `topo-${String(index)}`,
1186
+ };
1187
+ })
1188
+ .toSorted((left, right) => left.topoName.localeCompare(right.topoName));
1189
+ const hasher = new Bun.CryptoHasher('sha256');
1190
+ hasher.update(JSON.stringify(payload));
1191
+ return hasher.digest('hex');
1192
+ };
1193
+
1194
+ const describeTopoDriftHash = (
1195
+ topoTargets: readonly WardenTopoTarget[],
1196
+ driftResults: readonly DriftResult[]
1197
+ ): string =>
1198
+ driftResults
1199
+ .map((result, index) => {
1200
+ const target = topoTargets[index];
1201
+ const topoName =
1202
+ target?.name ?? target?.topo.name ?? `topo-${String(index)}`;
1203
+ return `${topoName}=${result.committedHash ?? '<none>'}`;
1204
+ })
1205
+ .join(', ');
1206
+
1207
+ const checkDriftForTopoTargets = async (
1208
+ rootDir: string,
1209
+ topoTargets: readonly WardenTopoTarget[]
1210
+ ): Promise<DriftResult> => {
1211
+ if (topoTargets.length <= 1) {
1212
+ return checkDrift(rootDir, topoTargets[0]?.topo);
1213
+ }
1214
+
1215
+ const driftResults = await Promise.all(
1216
+ topoTargets.map((target) => checkDrift(rootDir, target.topo))
1217
+ );
1218
+ const committedHashes = new Set(
1219
+ driftResults.map((result) => result.committedHash)
1220
+ );
1221
+ if (committedHashes.size > 1) {
1222
+ return {
1223
+ blockedReason: `multi-topo drift expected one committed trails.lock hash but found conflicting hashes: ${describeTopoDriftHash(topoTargets, driftResults)}`,
1224
+ committedHash: null,
1225
+ currentHash: 'blocked',
1226
+ stale: true,
1227
+ };
1228
+ }
1229
+ const committedHash = driftResults[0]?.committedHash ?? null;
1230
+ const blockedReasons = driftResults.flatMap((result, index) => {
1231
+ if (result.blockedReason === undefined) {
1232
+ return [];
1233
+ }
1234
+ const target = topoTargets[index];
1235
+ const topoName =
1236
+ target?.name ?? target?.topo.name ?? `topo-${String(index)}`;
1237
+ return [`${topoName}: ${result.blockedReason}`];
1238
+ });
1239
+
1240
+ if (blockedReasons.length > 0) {
1241
+ return {
1242
+ blockedReason: blockedReasons.join('; '),
1243
+ committedHash,
1244
+ currentHash: 'blocked',
1245
+ stale: true,
1246
+ };
1247
+ }
1248
+
1249
+ const currentHash = aggregateDriftHash(topoTargets, driftResults);
1250
+ return {
1251
+ committedHash,
1252
+ currentHash,
1253
+ stale: committedHash !== null && committedHash !== currentHash,
1254
+ };
1255
+ };
1256
+
1257
+ const shouldRunLint = (options: WardenRunOptions): boolean =>
1258
+ options.tier ? options.tier !== 'drift' : !options.driftOnly;
1259
+
1260
+ const adapterDiagnosticsForRun = (
1261
+ rootDir: string,
1262
+ options: WardenRunOptions
1263
+ ): readonly WardenDiagnostic[] =>
1264
+ options.adapterCheck ? runWardenAdapterChecks(rootDir) : [];
1265
+
1266
+ const shouldRunDrift = (
1267
+ options: WardenRunOptions,
1268
+ effectiveConfig: EffectiveWardenConfig
1269
+ ): boolean => {
1270
+ if (effectiveConfig.lock === 'skip') {
1271
+ return false;
1272
+ }
1273
+
1274
+ if (options.tier) {
1275
+ return options.tier === 'drift';
1276
+ }
1277
+
1278
+ if (options.lintOnly) {
1279
+ return false;
1280
+ }
1281
+
1282
+ return options.driftOnly || effectiveConfig.depth === 'all';
1283
+ };
1284
+
1285
+ const reportPassed = ({
1286
+ drift,
1287
+ errorCount,
1288
+ failOn,
1289
+ warnCount,
1290
+ }: {
1291
+ readonly drift: DriftResult | null;
1292
+ readonly errorCount: number;
1293
+ readonly failOn: WardenFailOn;
1294
+ readonly warnCount: number;
1295
+ }): boolean =>
1296
+ errorCount === 0 &&
1297
+ (failOn === 'error' || warnCount === 0) &&
1298
+ !(drift?.stale ?? false) &&
1299
+ drift?.blockedReason === undefined;
1300
+
1301
+ const buildCliConfigLayer = (options: WardenRunOptions): WardenConfigLayer => ({
1302
+ ...(options.apps ? { apps: [...options.apps] } : {}),
1303
+ ...(options.depth ? { depth: options.depth } : {}),
1304
+ ...(options.drafts ? { drafts: options.drafts } : {}),
1305
+ ...(options.failOn ? { failOn: options.failOn } : {}),
1306
+ ...(options.format ? { format: options.format } : {}),
1307
+ ...(options.lock ? { lock: options.lock } : {}),
1308
+ ...(options.noLockMutation === undefined
1309
+ ? {}
1310
+ : { noLockMutation: options.noLockMutation }),
1311
+ });
1312
+
1313
+ const fixSummary = (application: WardenFixApplication): WardenFixSummary => ({
1314
+ applied: application.applied,
1315
+ filesChanged: application.filesChanged,
1316
+ skipped: application.skipped,
1317
+ });
1318
+
1319
+ const filterAppliedFixDiagnostics = (
1320
+ diagnostics: readonly WardenDiagnostic[],
1321
+ appliedDiagnostics: readonly WardenDiagnostic[]
1322
+ ): readonly WardenDiagnostic[] => {
1323
+ if (appliedDiagnostics.length === 0) {
1324
+ return diagnostics;
1325
+ }
1326
+ const applied = new Set(appliedDiagnostics);
1327
+ return diagnostics.filter((diagnostic) => !applied.has(diagnostic));
1328
+ };
1329
+
1330
+ const blockedDriftAfterSourceFixes = (): DriftResult => ({
1331
+ blockedReason:
1332
+ 'Source fixes were applied; rerun Warden to refresh drift evidence.',
1333
+ committedHash: null,
1334
+ currentHash: 'blocked',
1335
+ stale: true,
1336
+ });
1337
+
1338
+ /**
1339
+ * Apply every safe source fix among a run's diagnostics, writing patched files.
1340
+ *
1341
+ * Diagnostics with a safe, edit-bearing fix are grouped by file; each file is
1342
+ * re-read so the rule's recorded offsets stay valid, patched via
1343
+ * {@link applySafeFixesToSource}, and written back only when its source
1344
+ * actually changed. Diagnostics that carry fix metadata but are not safe with
1345
+ * edits (review-required or edit-less) are counted as skipped and left reported
1346
+ * for a human or downstream regrade to resolve. Diagnostics without fix
1347
+ * metadata — including topo diagnostics, which carry no source span — are
1348
+ * neither applied nor counted in `skipped`.
1349
+ */
1350
+ export const applySafeFixesToFiles = async (
1351
+ diagnostics: readonly WardenDiagnostic[],
1352
+ options: {
1353
+ readonly allowedFilePaths?: ReadonlySet<string> | readonly string[];
1354
+ readonly rootDir: string;
1355
+ }
1356
+ ): Promise<WardenFixApplication> => {
1357
+ const rootDir = resolve(options.rootDir);
1358
+ const allowedFilePaths =
1359
+ options.allowedFilePaths === undefined
1360
+ ? undefined
1361
+ : new Set(
1362
+ [...options.allowedFilePaths].map((filePath) => resolve(filePath))
1363
+ );
1364
+ const fixableByFile = new Map<string, WardenDiagnostic[]>();
1365
+ let skipped = 0;
1366
+ for (const diagnostic of diagnostics) {
1367
+ if (diagnostic.fix === undefined) {
1368
+ continue;
1369
+ }
1370
+ if (hasSafeFixEdits(diagnostic)) {
1371
+ const filePath = resolve(diagnostic.filePath);
1372
+ const rootRelativePath = relative(rootDir, filePath);
1373
+ const insideRoot =
1374
+ rootRelativePath.length === 0 ||
1375
+ (!rootRelativePath.startsWith('..') && !isAbsolute(rootRelativePath));
1376
+ if (
1377
+ !insideRoot ||
1378
+ (allowedFilePaths !== undefined && !allowedFilePaths.has(filePath))
1379
+ ) {
1380
+ skipped += 1;
1381
+ continue;
1382
+ }
1383
+ const bucket = fixableByFile.get(filePath) ?? [];
1384
+ bucket.push(diagnostic);
1385
+ fixableByFile.set(filePath, bucket);
1386
+ } else {
1387
+ skipped += 1;
1388
+ }
1389
+ }
237
1390
 
238
- return allDiagnostics;
1391
+ let applied = 0;
1392
+ const appliedDiagnostics: WardenDiagnostic[] = [];
1393
+ let filesChanged = 0;
1394
+ for (const [filePath, group] of fixableByFile) {
1395
+ const source = await Bun.file(filePath).text();
1396
+ const result = applySafeFixesToSource(source, group);
1397
+ applied += result.applied.length;
1398
+ appliedDiagnostics.push(...result.applied);
1399
+ if (result.changed) {
1400
+ await Bun.write(filePath, result.patched);
1401
+ filesChanged += 1;
1402
+ }
1403
+ }
1404
+
1405
+ return { applied, appliedDiagnostics, filesChanged, skipped };
239
1406
  };
240
1407
 
241
1408
  /**
242
1409
  * Run all warden checks and return a structured report.
243
1410
  */
244
1411
  export const runWarden = async (
245
- options: WardenOptions = {}
1412
+ options: WardenRunOptions = {}
246
1413
  ): Promise<WardenReport> => {
247
1414
  const rootDir = resolve(options.rootDir ?? process.cwd());
248
- const allDiagnostics = options.driftOnly
249
- ? []
250
- : await lintFiles(rootDir, options.topo);
251
- const drift = options.lintOnly
252
- ? null
253
- : await checkDrift(rootDir, options.topo);
254
-
255
- const errorCount = allDiagnostics.filter(
1415
+ const { diagnostics: configDiagnostics, effectiveConfig } =
1416
+ resolveWardenConfig({
1417
+ cli: buildCliConfigLayer(options),
1418
+ config: options.config,
1419
+ env: options.env,
1420
+ });
1421
+ const optionDiagnostics =
1422
+ !options.tier && options.lintOnly && options.driftOnly
1423
+ ? [
1424
+ createOptionsDiagnostic(
1425
+ 'lintOnly and driftOnly cannot both be true. Use tier to select a single Warden mode.'
1426
+ ),
1427
+ ]
1428
+ : [];
1429
+ const topoTargets = topoTargetsFromOptions(options);
1430
+ const selector = {
1431
+ depth: options.tier ? undefined : effectiveConfig.depth,
1432
+ tier: options.tier,
1433
+ } satisfies WardenRuleSelector;
1434
+ const runLint = shouldRunLint(options);
1435
+ const runDrift = shouldRunDrift(options, effectiveConfig);
1436
+ const lintResult = runLint
1437
+ ? await lintFiles(
1438
+ rootDir,
1439
+ effectiveConfig.drafts,
1440
+ topoTargets,
1441
+ options.extraTopoRules ?? [],
1442
+ options.extraSourceRules ?? [],
1443
+ selector
1444
+ )
1445
+ : { diagnostics: [], sourceFiles: [] };
1446
+ const adapterDiagnostics = adapterDiagnosticsForRun(rootDir, options);
1447
+
1448
+ const rawDiagnostics = [
1449
+ ...configDiagnostics,
1450
+ ...optionDiagnostics,
1451
+ ...lintResult.diagnostics,
1452
+ ...adapterDiagnostics,
1453
+ ];
1454
+ const allDiagnostics = rawDiagnostics.map(withDiagnosticGuidance);
1455
+ const fixApplication = options.fix
1456
+ ? await applySafeFixesToFiles(allDiagnostics, {
1457
+ allowedFilePaths: lintResult.sourceFiles.map(
1458
+ (sourceFile) => sourceFile.filePath
1459
+ ),
1460
+ rootDir,
1461
+ })
1462
+ : undefined;
1463
+ const reportDiagnostics = filterAppliedFixDiagnostics(
1464
+ allDiagnostics,
1465
+ fixApplication?.appliedDiagnostics ?? []
1466
+ );
1467
+ let drift: DriftResult | null = null;
1468
+ if (runDrift) {
1469
+ drift =
1470
+ fixApplication !== undefined && fixApplication.filesChanged > 0
1471
+ ? blockedDriftAfterSourceFixes()
1472
+ : await checkDriftForTopoTargets(rootDir, topoTargets);
1473
+ }
1474
+
1475
+ const errorCount = reportDiagnostics.filter(
256
1476
  (d) => d.severity === 'error'
257
1477
  ).length;
258
- const warnCount = allDiagnostics.filter((d) => d.severity === 'warn').length;
1478
+ const warnCount = reportDiagnostics.filter(
1479
+ (d) => d.severity === 'warn'
1480
+ ).length;
1481
+ const topoNames =
1482
+ topoTargets.length > 0
1483
+ ? topoTargets.map((target) => target.name ?? target.topo.name)
1484
+ : undefined;
259
1485
 
260
1486
  return {
261
- diagnostics: allDiagnostics,
1487
+ diagnostics: reportDiagnostics,
262
1488
  drift,
1489
+ effectiveConfig,
263
1490
  errorCount,
264
- passed: errorCount === 0 && !(drift?.stale ?? false),
1491
+ ...(fixApplication === undefined
1492
+ ? {}
1493
+ : { fixes: fixSummary(fixApplication) }),
1494
+ passed: reportPassed({
1495
+ drift,
1496
+ errorCount,
1497
+ failOn: effectiveConfig.failOn,
1498
+ warnCount,
1499
+ }),
1500
+ ...(topoNames === undefined ? {} : { topoNames }),
265
1501
  warnCount,
266
1502
  };
267
1503
  };
268
1504
 
1505
+ const formatPlainGuidanceLink = (link: WardenGuidanceLink): string => {
1506
+ const target = link.path ?? link.url;
1507
+ if (target === undefined || target === link.label) {
1508
+ return link.label;
1509
+ }
1510
+ return `${link.label} (${target})`;
1511
+ };
1512
+
269
1513
  /**
270
1514
  * Format the lint section of the report.
271
1515
  */
@@ -283,6 +1527,25 @@ const formatLintSection = (report: WardenReport): string[] => {
283
1527
  lines.push(
284
1528
  ` ${d.filePath}:${String(d.line)} [${prefix}] ${d.rule} ${d.message}`
285
1529
  );
1530
+ if (d.guidance !== undefined) {
1531
+ lines.push(` Next: ${d.guidance.summary}`);
1532
+ for (const [index, step] of (d.guidance.steps ?? []).entries()) {
1533
+ lines.push(` ${String(index + 1)}. ${step}`);
1534
+ }
1535
+ if (d.guidance.commands !== undefined) {
1536
+ lines.push(
1537
+ ` Commands: ${d.guidance.commands.map((cmd) => `\`${cmd}\``).join(', ')}`
1538
+ );
1539
+ }
1540
+ if (d.guidance.docs !== undefined) {
1541
+ lines.push(
1542
+ ` Docs: ${d.guidance.docs.map(formatPlainGuidanceLink).join(', ')}`
1543
+ );
1544
+ }
1545
+ if (d.guidance.relatedRules !== undefined) {
1546
+ lines.push(` Related: ${d.guidance.relatedRules.join(', ')}`);
1547
+ }
1548
+ }
286
1549
  }
287
1550
 
288
1551
  return lines;
@@ -295,8 +1558,11 @@ const formatDriftSection = (drift: DriftResult | null): string[] => {
295
1558
  if (drift === null) {
296
1559
  return [];
297
1560
  }
1561
+ if (drift.blockedReason !== undefined) {
1562
+ return [`Drift: blocked (${drift.blockedReason})`, ''];
1563
+ }
298
1564
  const label = drift.stale
299
- ? 'Drift: surface.lock is stale (regenerate with `trails survey generate`)'
1565
+ ? 'Drift: trails.lock is stale (regenerate with `trails compile`)'
300
1566
  : 'Drift: clean';
301
1567
  return [label, ''];
302
1568
  };
@@ -312,7 +1578,12 @@ const formatResultLine = (report: WardenReport): string => {
312
1578
  if (report.errorCount > 0) {
313
1579
  parts.push(`${report.errorCount} errors`);
314
1580
  }
315
- if (report.drift?.stale) {
1581
+ if (report.warnCount > 0 && report.effectiveConfig?.failOn === 'warning') {
1582
+ parts.push(`${report.warnCount} warnings`);
1583
+ }
1584
+ if (report.drift?.blockedReason !== undefined) {
1585
+ parts.push('established exports blocked');
1586
+ } else if (report.drift?.stale) {
316
1587
  parts.push('drift detected');
317
1588
  }
318
1589
  return `Result: FAIL (${parts.join(', ')})`;