@topogram/cli 0.3.64 → 0.3.66

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 (278) hide show
  1. package/package.json +1 -1
  2. package/src/adoption/plan/index.js +716 -0
  3. package/src/adoption/plan.js +12 -703
  4. package/src/adoption/reporting.js +1 -1
  5. package/src/agent-brief.js +7 -21
  6. package/src/agent-ops/query-builders/auth.js +375 -0
  7. package/src/agent-ops/query-builders/change-risk/change-plan.js +123 -0
  8. package/src/agent-ops/query-builders/change-risk/import-plan.js +49 -0
  9. package/src/agent-ops/query-builders/change-risk/maintained.js +286 -0
  10. package/src/agent-ops/query-builders/change-risk/review-packets.js +123 -0
  11. package/src/agent-ops/query-builders/change-risk/risk.js +189 -0
  12. package/src/agent-ops/query-builders/change-risk.js +25 -0
  13. package/src/agent-ops/query-builders/common.js +149 -0
  14. package/src/agent-ops/query-builders/maintained-risk.js +539 -0
  15. package/src/agent-ops/query-builders/maintained-shared.js +120 -0
  16. package/src/agent-ops/query-builders/multi-agent.js +547 -0
  17. package/src/agent-ops/query-builders/projection-impacts.js +514 -0
  18. package/src/agent-ops/query-builders/work-packets.js +417 -0
  19. package/src/agent-ops/query-builders/workflow-context-shared.js +300 -0
  20. package/src/agent-ops/query-builders/workflow-context.js +398 -0
  21. package/src/agent-ops/query-builders/workflow-presets-core.js +677 -0
  22. package/src/agent-ops/query-builders/workflow-presets.js +341 -0
  23. package/src/agent-ops/query-builders.d.ts +26 -26
  24. package/src/agent-ops/query-builders.js +42 -5021
  25. package/src/archive/jsonl.js +2 -2
  26. package/src/archive/resolver-bridge.js +1 -1
  27. package/src/archive/unarchive.js +2 -1
  28. package/src/catalog/constants.js +10 -0
  29. package/src/catalog/copy.js +65 -0
  30. package/src/catalog/diagnostics.js +15 -0
  31. package/src/catalog/entries.js +42 -0
  32. package/src/catalog/files.js +67 -0
  33. package/src/catalog/provenance.js +123 -0
  34. package/src/catalog/source.js +150 -0
  35. package/src/catalog/validation.js +252 -0
  36. package/src/catalog.d.ts +2 -0
  37. package/src/catalog.js +18 -746
  38. package/src/cli/command-parsers/project.js +3 -0
  39. package/src/cli/command-parsers/shared.js +1 -1
  40. package/src/cli/commands/agent.js +2 -2
  41. package/src/cli/commands/catalog/check.js +31 -0
  42. package/src/cli/commands/catalog/copy.js +59 -0
  43. package/src/cli/commands/catalog/doctor.js +248 -0
  44. package/src/cli/commands/catalog/help.js +21 -0
  45. package/src/cli/commands/catalog/list.js +52 -0
  46. package/src/cli/commands/catalog/runner.js +92 -0
  47. package/src/cli/commands/catalog/shared.js +17 -0
  48. package/src/cli/commands/catalog/show.js +134 -0
  49. package/src/cli/commands/catalog.js +30 -615
  50. package/src/cli/commands/check.js +3 -3
  51. package/src/cli/commands/doctor.js +2 -9
  52. package/src/cli/commands/generator-policy/package-info.js +162 -0
  53. package/src/cli/commands/generator-policy/payloads.js +372 -0
  54. package/src/cli/commands/generator-policy/printers.js +159 -0
  55. package/src/cli/commands/generator-policy/runner.js +81 -0
  56. package/src/cli/commands/generator-policy/shared.js +39 -0
  57. package/src/cli/commands/generator-policy.js +15 -783
  58. package/src/cli/commands/import/adopt.js +170 -0
  59. package/src/cli/commands/import/check.js +91 -0
  60. package/src/cli/commands/import/diff.js +84 -0
  61. package/src/cli/commands/import/help.js +47 -0
  62. package/src/cli/commands/import/paths.js +269 -0
  63. package/src/cli/commands/import/plan.js +292 -0
  64. package/src/cli/commands/import/refresh.js +471 -0
  65. package/src/cli/commands/import/status-history.js +196 -0
  66. package/src/cli/commands/import/workspace.js +233 -0
  67. package/src/cli/commands/import.js +33 -1732
  68. package/src/cli/commands/migrate.js +153 -0
  69. package/src/cli/commands/package/constants.js +17 -0
  70. package/src/cli/commands/package/doctor.js +240 -0
  71. package/src/cli/commands/package/help.js +27 -0
  72. package/src/cli/commands/package/lockfile.js +135 -0
  73. package/src/cli/commands/package/npm.js +97 -0
  74. package/src/cli/commands/package/reporting.js +35 -0
  75. package/src/cli/commands/package/runner.js +33 -0
  76. package/src/cli/commands/package/shared.js +9 -0
  77. package/src/cli/commands/package/update-cli.js +252 -0
  78. package/src/cli/commands/package/versions.js +35 -0
  79. package/src/cli/commands/package.js +29 -813
  80. package/src/cli/commands/query/change-plan.js +68 -0
  81. package/src/cli/commands/query/definitions.js +202 -0
  82. package/src/cli/commands/query/import-adopt.js +121 -0
  83. package/src/cli/commands/query/runner/artifacts.js +102 -0
  84. package/src/cli/commands/query/runner/boundaries.js +211 -0
  85. package/src/cli/commands/query/runner/change.js +182 -0
  86. package/src/cli/commands/query/runner/import-adopt.js +111 -0
  87. package/src/cli/commands/query/runner/index.js +31 -0
  88. package/src/cli/commands/query/runner/output.js +12 -0
  89. package/src/cli/commands/query/runner/workflow.js +241 -0
  90. package/src/cli/commands/query/runner.js +3 -0
  91. package/src/cli/commands/query/workflow-context.js +5 -0
  92. package/src/cli/commands/query/workspace.js +270 -0
  93. package/src/cli/commands/query.js +9 -1300
  94. package/src/cli/commands/source.js +3 -12
  95. package/src/cli/commands/template/baseline.js +100 -0
  96. package/src/cli/commands/template/check.js +467 -0
  97. package/src/cli/commands/template/constants.js +8 -0
  98. package/src/cli/commands/template/diagnostics.js +26 -0
  99. package/src/cli/commands/template/help.js +28 -0
  100. package/src/cli/commands/template/lifecycle.js +404 -0
  101. package/src/cli/commands/template/list-show.js +287 -0
  102. package/src/cli/commands/template/policy.js +422 -0
  103. package/src/cli/commands/template/shared.js +127 -0
  104. package/src/cli/commands/template/updates.js +352 -0
  105. package/src/cli/commands/template-runner.js +6 -6
  106. package/src/cli/commands/template.js +41 -2143
  107. package/src/cli/commands/trust.js +1 -1
  108. package/src/cli/commands/workflow.js +6 -1
  109. package/src/cli/dispatcher.js +6 -1
  110. package/src/cli/help.js +15 -14
  111. package/src/cli/migration-guidance.js +1 -1
  112. package/src/cli/output-safety.js +2 -1
  113. package/src/cli/path-normalization.js +3 -13
  114. package/src/generator/api/contracts.js +497 -0
  115. package/src/generator/api/metadata.js +221 -0
  116. package/src/generator/api/openapi.js +559 -0
  117. package/src/generator/api/schema.js +124 -0
  118. package/src/generator/api/types.d.ts +98 -0
  119. package/src/generator/api.js +3 -1195
  120. package/src/generator/context/domain-page.js +1 -1
  121. package/src/generator/context/shared/domain-sdlc.js +282 -0
  122. package/src/generator/context/shared/maintained-boundary.js +665 -0
  123. package/src/generator/context/shared/metrics.js +85 -0
  124. package/src/generator/context/shared/primitives.js +64 -0
  125. package/src/generator/context/shared/relationships.js +453 -0
  126. package/src/generator/context/shared/summaries.js +263 -0
  127. package/src/generator/context/shared/types.d.ts +207 -0
  128. package/src/generator/context/shared.d.ts +42 -0
  129. package/src/generator/context/shared.js +80 -1390
  130. package/src/generator/context/slice/core.js +397 -0
  131. package/src/generator/context/slice/sdlc.js +417 -0
  132. package/src/generator/context/slice/ui-packets.js +183 -0
  133. package/src/generator/context/slice.js +2 -859
  134. package/src/generator/context/task-mode.js +2 -2
  135. package/src/generator/registry/index.js +507 -0
  136. package/src/generator/registry.js +18 -504
  137. package/src/generator/runtime/environment/index.js +666 -0
  138. package/src/generator/runtime/environment.js +4 -666
  139. package/src/generator/runtime/runtime-check/index.js +554 -0
  140. package/src/generator/runtime/runtime-check.js +4 -554
  141. package/src/generator/runtime/shared/index.js +572 -0
  142. package/src/generator/runtime/shared.js +19 -570
  143. package/src/generator/sdlc/doc-page.js +1 -1
  144. package/src/generator/shared.d.ts +2 -0
  145. package/src/generator/surfaces/databases/lifecycle-shared.js +1 -1
  146. package/src/generator/surfaces/native/swiftui-templates/README.generated.md +1 -1
  147. package/src/generator/surfaces/shared.d.ts +3 -0
  148. package/src/generator/widget-conformance/behavior-report.js +258 -0
  149. package/src/generator/widget-conformance/checks.js +371 -0
  150. package/src/generator/widget-conformance/projection-context.js +200 -0
  151. package/src/generator/widget-conformance/report.js +166 -0
  152. package/src/generator/widget-conformance/types.d.ts +121 -0
  153. package/src/generator/widget-conformance.js +3 -824
  154. package/src/import/core/context.d.ts +3 -0
  155. package/src/import/core/context.js +5 -7
  156. package/src/import/core/contracts.d.ts +1 -0
  157. package/src/import/core/registry.d.ts +4 -0
  158. package/src/import/core/runner/candidates.js +337 -0
  159. package/src/import/core/runner/options.js +22 -0
  160. package/src/import/core/runner/reports.js +51 -0
  161. package/src/import/core/runner/run.js +79 -0
  162. package/src/import/core/runner/tracks.js +150 -0
  163. package/src/import/core/runner/ui-drafts.js +393 -0
  164. package/src/import/core/runner.js +3 -698
  165. package/src/import/core/shared/api-routes.js +221 -0
  166. package/src/import/core/shared/candidates.js +97 -0
  167. package/src/import/core/shared/files.js +177 -0
  168. package/src/import/core/shared/next-app.js +389 -0
  169. package/src/import/core/shared/types.d.ts +51 -0
  170. package/src/import/core/shared/ui-routes.js +230 -0
  171. package/src/import/core/shared.js +60 -861
  172. package/src/new-project/constants.js +128 -0
  173. package/src/new-project/create.js +90 -0
  174. package/src/new-project/json.js +28 -0
  175. package/src/new-project/metadata.js +96 -0
  176. package/src/new-project/package-spec.js +161 -0
  177. package/src/new-project/project-files.js +351 -0
  178. package/src/new-project/template-policy.js +269 -0
  179. package/src/new-project/template-resolution.js +370 -0
  180. package/src/new-project/template-snapshots.js +442 -0
  181. package/src/new-project/template-updates.js +512 -0
  182. package/src/new-project/types.d.ts +83 -0
  183. package/src/new-project.js +6 -2277
  184. package/src/parser.d.ts +87 -1
  185. package/src/parser.js +118 -0
  186. package/src/policy/review-boundaries.d.ts +15 -0
  187. package/src/project-config/index.js +591 -0
  188. package/src/project-config.js +19 -561
  189. package/src/resolver/enrich/acceptance-criterion.js +2 -0
  190. package/src/resolver/enrich/bug.js +2 -0
  191. package/src/resolver/enrich/pitch.js +2 -0
  192. package/src/resolver/enrich/requirement.js +2 -0
  193. package/src/resolver/enrich/task.js +2 -0
  194. package/src/resolver/index.js +19 -2089
  195. package/src/resolver/normalize.js +384 -1
  196. package/src/resolver/plans.js +168 -0
  197. package/src/resolver/projections-api.js +494 -0
  198. package/src/resolver/projections-db.js +133 -0
  199. package/src/resolver/projections-ui.js +317 -0
  200. package/src/resolver/shapes.js +251 -0
  201. package/src/resolver/shared.js +278 -0
  202. package/src/resolver/widgets.js +132 -0
  203. package/src/sdlc/adopt.js +6 -5
  204. package/src/sdlc/paths.js +3 -5
  205. package/src/sdlc/scaffold.js +2 -1
  206. package/src/template-trust/constants.js +62 -0
  207. package/src/template-trust/content.js +258 -0
  208. package/src/template-trust/diff.js +92 -0
  209. package/src/template-trust/policy.js +61 -0
  210. package/src/template-trust/record.js +90 -0
  211. package/src/template-trust/status.js +182 -0
  212. package/src/template-trust.js +24 -687
  213. package/src/text-helpers.d.ts +1 -0
  214. package/src/topogram-types.d.ts +69 -0
  215. package/src/validator/common.js +488 -0
  216. package/src/validator/data-model.js +237 -0
  217. package/src/validator/docs.js +167 -0
  218. package/src/validator/expressions.js +146 -1
  219. package/src/validator/index.d.ts +23 -0
  220. package/src/validator/index.js +32 -3585
  221. package/src/validator/kinds.d.ts +41 -0
  222. package/src/validator/kinds.js +2 -0
  223. package/src/validator/model-helpers.js +46 -0
  224. package/src/validator/per-kind/acceptance-criterion.js +5 -0
  225. package/src/validator/per-kind/bug.js +6 -0
  226. package/src/validator/per-kind/domain.js +15 -2
  227. package/src/validator/per-kind/pitch.js +7 -0
  228. package/src/validator/per-kind/requirement.js +5 -0
  229. package/src/validator/per-kind/task.js +7 -0
  230. package/src/validator/per-kind/widget.js +14 -0
  231. package/src/validator/projections/api-http-async.js +410 -0
  232. package/src/validator/projections/api-http-authz.js +88 -0
  233. package/src/validator/projections/api-http-core.js +205 -0
  234. package/src/validator/projections/api-http-policies.js +339 -0
  235. package/src/validator/projections/api-http-responses.js +233 -0
  236. package/src/validator/projections/api-http.js +44 -0
  237. package/src/validator/projections/db.js +353 -0
  238. package/src/validator/projections/generator-defaults.js +45 -0
  239. package/src/validator/projections/helpers.js +87 -0
  240. package/src/validator/projections/ui-helpers.js +214 -0
  241. package/src/validator/projections/ui-navigation.js +344 -0
  242. package/src/validator/projections/ui-structure.js +364 -0
  243. package/src/validator/projections/ui-widgets.js +493 -0
  244. package/src/validator/projections/ui.js +46 -0
  245. package/src/validator/registry.js +48 -1
  246. package/src/validator/utils.d.ts +20 -0
  247. package/src/validator/utils.js +115 -12
  248. package/src/widget-behavior.d.ts +1 -0
  249. package/src/workflows/import-app/api/collect.js +221 -0
  250. package/src/workflows/import-app/api/openapi.js +257 -0
  251. package/src/workflows/import-app/api/routes.js +327 -0
  252. package/src/workflows/import-app/api/sources.js +22 -0
  253. package/src/workflows/import-app/api.js +2 -797
  254. package/src/workflows/reconcile/adoption-plan/build.js +212 -0
  255. package/src/workflows/reconcile/adoption-plan/dependencies.js +75 -0
  256. package/src/workflows/reconcile/adoption-plan/outputs.js +153 -0
  257. package/src/workflows/reconcile/adoption-plan/paths.js +58 -0
  258. package/src/workflows/reconcile/adoption-plan/projection-patches.js +177 -0
  259. package/src/workflows/reconcile/adoption-plan/reasons.js +107 -0
  260. package/src/workflows/reconcile/adoption-plan.js +30 -740
  261. package/src/workflows/reconcile/auth/closures.js +115 -0
  262. package/src/workflows/reconcile/auth/formatters.js +142 -0
  263. package/src/workflows/reconcile/auth/inference.js +330 -0
  264. package/src/workflows/reconcile/auth/roles.js +122 -0
  265. package/src/workflows/reconcile/auth.js +35 -690
  266. package/src/workflows/reconcile/bundle-core/index.js +600 -0
  267. package/src/workflows/reconcile/bundle-core.js +12 -598
  268. package/src/workflows/reconcile/candidate-model.js +18 -2
  269. package/src/workflows/reconcile/canonical-surface.js +1 -1
  270. package/src/workflows/reconcile/impacts/adoption-plan.js +196 -0
  271. package/src/workflows/reconcile/impacts/indexes.js +105 -0
  272. package/src/workflows/reconcile/impacts/patches.js +252 -0
  273. package/src/workflows/reconcile/impacts/reports.js +80 -0
  274. package/src/workflows/reconcile/impacts.js +14 -623
  275. package/src/workflows/reconcile/renderers.js +41 -6
  276. package/src/workflows/shared.js +5 -11
  277. package/src/workspace-docs.d.ts +29 -0
  278. package/src/workspace-paths.js +328 -0
@@ -0,0 +1,212 @@
1
+ // @ts-check
2
+
3
+ import { adoptionItemKey } from "../../../adoption/plan.js";
4
+ import { confidenceRank } from "../../docs.js";
5
+ import {
6
+ adoptionStatusForStep,
7
+ blockingDependenciesForProjectionImpacts,
8
+ blockingDependenciesForUiImpacts,
9
+ blockingDependenciesForWorkflowImpacts,
10
+ projectionImpactsForAdoptionItem
11
+ } from "./dependencies.js";
12
+ import {
13
+ canonicalDisplayPathForItem,
14
+ canonicalRelativePathForItem,
15
+ candidateSourcePathForItem
16
+ } from "./paths.js";
17
+ import {
18
+ reasonForAdoptionItem,
19
+ recommendationForAdoptionItem
20
+ } from "./reasons.js";
21
+
22
+ /** @param {CandidateBundle[]} bundles @returns {any} */
23
+ export function buildAdoptionPlan(bundles) {
24
+ /** @type {any[]} */
25
+ const items = [];
26
+ for (const bundle of bundles) {
27
+ for (const step of bundle.adoptionPlan || []) {
28
+ const itemKind =
29
+ step.action === "merge_bundle_into_existing_entity" ? "bundle" :
30
+ step.action === "apply_projection_permission_patch" ? "projection_permission_patch" :
31
+ step.action === "apply_projection_auth_patch" ? "projection_auth_patch" :
32
+ step.action === "apply_projection_ownership_patch" ? "projection_ownership_patch" :
33
+ step.action.includes("doc") ? "doc" :
34
+ step.action.includes("decision") ? "decision" :
35
+ step.action.includes("verification") ? "verification" :
36
+ step.action.includes("widget") ? "widget" :
37
+ step.action.includes("ui_") ? "ui" :
38
+ step.action.includes("actor") ? "actor" :
39
+ step.action.includes("role") ? "role" :
40
+ step.action.includes("enum") ? "enum" :
41
+ step.action.includes("shape") ? "shape" :
42
+ step.action.includes("capability") ? "capability" :
43
+ step.action.includes("entity") ? "entity" :
44
+ "bundle";
45
+ if (itemKind === "bundle") {
46
+ continue;
47
+ }
48
+ const projectionImpacts = projectionImpactsForAdoptionItem(bundle, step);
49
+ const directProjectionBlockingDependencies =
50
+ (step.action === "apply_projection_permission_patch" || step.action === "apply_projection_auth_patch" || step.action === "apply_projection_ownership_patch") && step.target
51
+ ? blockingDependenciesForProjectionImpacts([
52
+ {
53
+ projection_id: step.target,
54
+ kind: step.projection_kind || "api",
55
+ projection_type: null,
56
+ reason: `Projection ${step.target} auth rules need explicit review before promotion.`
57
+ }
58
+ ])
59
+ : [];
60
+ const uiImpacts = step.action.includes("ui_") ? bundle.uiImpacts || [] : [];
61
+ const workflowImpacts = step.action.includes("workflow") ? bundle.workflowImpacts || [] : [];
62
+ const blockingDependencies = [
63
+ ...blockingDependenciesForProjectionImpacts(projectionImpacts),
64
+ ...directProjectionBlockingDependencies,
65
+ ...blockingDependenciesForUiImpacts(uiImpacts),
66
+ ...blockingDependenciesForWorkflowImpacts(workflowImpacts)
67
+ ];
68
+ items.push({
69
+ bundle: bundle.slug,
70
+ item: step.item,
71
+ kind: itemKind,
72
+ track:
73
+ step.action.includes("workflow") ? "workflows" :
74
+ step.action.includes("verification") ? "verification" :
75
+ step.action.includes("ui_") ? "ui" :
76
+ step.action === "apply_projection_permission_patch" ? "projection" :
77
+ step.action === "apply_projection_auth_patch" ? "projection" :
78
+ step.action === "apply_projection_ownership_patch" ? "projection" :
79
+ step.action.includes("doc") ? "docs" :
80
+ itemKind,
81
+ suggested_action: step.action,
82
+ target: step.target || null,
83
+ confidence: step.confidence || null,
84
+ inference_summary: step.inference_summary || null,
85
+ source_kind: step.source_kind || null,
86
+ widget_id: step.widget_id || null,
87
+ event_name: step.event_name || null,
88
+ related_docs: step.related_docs || [],
89
+ related_capabilities: step.related_capabilities || [],
90
+ related_shapes: step.related_shapes || [],
91
+ permission: step.permission || null,
92
+ claim: step.claim || null,
93
+ claim_value: Object.prototype.hasOwnProperty.call(step, "claim_value") ? step.claim_value : null,
94
+ ownership: step.ownership || null,
95
+ ownership_field: step.ownership_field || null,
96
+ projection_surface: step.projection_surface || null,
97
+ status: adoptionStatusForStep(step, projectionImpacts, uiImpacts, workflowImpacts),
98
+ source_path: step.source_path || candidateSourcePathForItem(bundle, itemKind, step.item),
99
+ canonical_path:
100
+ step.action === "skip_duplicate_shape"
101
+ ? (step.target ? canonicalDisplayPathForItem("shape", step.target) : null)
102
+ : (step.canonical_rel_path ? `topo/${step.canonical_rel_path}` : canonicalDisplayPathForItem(itemKind, step.item)),
103
+ canonical_rel_path: step.canonical_rel_path || canonicalRelativePathForItem(itemKind, step.item),
104
+ reason: reasonForAdoptionItem(step),
105
+ recommendation: recommendationForAdoptionItem(step),
106
+ projection_impacts: projectionImpacts,
107
+ ui_impacts: uiImpacts,
108
+ workflow_impacts: workflowImpacts,
109
+ blocking_dependencies: blockingDependencies
110
+ });
111
+ }
112
+ for (const patch of bundle.projectionPatches || []) {
113
+ items.push({
114
+ bundle: bundle.slug,
115
+ item: `projection_patch:${patch.projection_id}`,
116
+ kind: "projection_patch",
117
+ track: "projection",
118
+ suggested_action: "review_projection_patch",
119
+ target: patch.projection_id,
120
+ status: "needs_projection_review",
121
+ source_path: `candidates/reconcile/model/bundles/${bundle.slug}/${patch.patch_rel_path}`,
122
+ canonical_path: null,
123
+ canonical_rel_path: null,
124
+ reason: patch.reason || `Projection ${patch.projection_id} needs additive review.`,
125
+ projection_impacts: [
126
+ {
127
+ projection_id: patch.projection_id,
128
+ kind: patch.kind,
129
+ projection_type: patch.projection_type,
130
+ missing_capabilities: patch.missing_realizes || []
131
+ }
132
+ ],
133
+ ui_impacts: (patch.missing_screens || []).length > 0 ? [
134
+ {
135
+ projection_id: patch.projection_id,
136
+ kind: patch.kind,
137
+ projection_type: patch.projection_type,
138
+ missing_screens: patch.missing_screens || []
139
+ }
140
+ ] : [],
141
+ workflow_impacts: [],
142
+ blocking_dependencies: blockingDependenciesForProjectionImpacts([
143
+ {
144
+ projection_id: patch.projection_id,
145
+ kind: patch.kind,
146
+ projection_type: patch.projection_type,
147
+ reason: patch.reason || `Projection ${patch.projection_id} needs additive review.`
148
+ }
149
+ ])
150
+ });
151
+ }
152
+ for (const patch of bundle.docLinkSuggestions || []) {
153
+ items.push({
154
+ bundle: bundle.slug,
155
+ item: `doc_link_patch:${patch.doc_id}`,
156
+ kind: "doc_link_patch",
157
+ track: "docs",
158
+ suggested_action: "apply_doc_link_patch",
159
+ target: patch.doc_id,
160
+ status: "pending",
161
+ source_path: `candidates/reconcile/model/bundles/${bundle.slug}/${patch.patch_rel_path}`,
162
+ canonical_path: patch.canonical_rel_path ? `topo/${patch.canonical_rel_path}` : null,
163
+ canonical_rel_path: patch.canonical_rel_path || null,
164
+ reason: `Apply suggested related actor/role links to \`${patch.doc_id}\`.`,
165
+ recommendation: recommendationForAdoptionItem({ action: "apply_doc_link_patch", target: patch.doc_id }),
166
+ related_docs: [patch.doc_id],
167
+ related_actors: patch.add_related_actors || [],
168
+ related_roles: patch.add_related_roles || [],
169
+ related_capabilities: patch.add_related_capabilities || [],
170
+ related_rules: patch.add_related_rules || [],
171
+ related_workflows: patch.add_related_workflows || [],
172
+ projection_impacts: [],
173
+ ui_impacts: [],
174
+ workflow_impacts: [],
175
+ blocking_dependencies: []
176
+ });
177
+ }
178
+ for (const patch of bundle.docMetadataPatches || []) {
179
+ items.push({
180
+ bundle: bundle.slug,
181
+ item: `doc_metadata_patch:${patch.doc_id}`,
182
+ kind: "doc_metadata_patch",
183
+ track: "docs",
184
+ suggested_action: "apply_doc_metadata_patch",
185
+ target: patch.doc_id,
186
+ status: "pending",
187
+ confidence: patch.imported_confidence || "low",
188
+ source_path: `candidates/reconcile/model/bundles/${bundle.slug}/${patch.patch_rel_path}`,
189
+ canonical_path: patch.canonical_rel_path ? `topo/${patch.canonical_rel_path}` : null,
190
+ canonical_rel_path: patch.canonical_rel_path || null,
191
+ reason: `Apply suggested safe metadata updates to \`${patch.doc_id}\`.`,
192
+ recommendation: recommendationForAdoptionItem({ action: "apply_doc_metadata_patch", target: patch.doc_id }),
193
+ related_docs: [patch.doc_id],
194
+ summary: patch.summary || null,
195
+ success_outcome: patch.success_outcome || null,
196
+ actors: patch.actors || [],
197
+ projection_impacts: [],
198
+ ui_impacts: [],
199
+ workflow_impacts: [],
200
+ blocking_dependencies: []
201
+ });
202
+ }
203
+ }
204
+ return items.sort((/** @type {any} */ a, /** @type {any} */ b) =>
205
+ a.bundle.localeCompare(b.bundle) ||
206
+ confidenceRank(b.confidence || "low") - confidenceRank(a.confidence || "low") ||
207
+ a.kind.localeCompare(b.kind) ||
208
+ a.item.localeCompare(b.item)
209
+ );
210
+ }
211
+
212
+ export const ADOPT_SELECTORS = new Set(["from-plan", "actors", "roles", "enums", "shapes", "entities", "capabilities", "widgets", "docs", "journeys", "workflows", "verification", "ui"]);
@@ -0,0 +1,75 @@
1
+ // @ts-check
2
+
3
+ /** @param {WorkflowRecord} step @param {any[]} projectionImpacts @param {any[]} uiImpacts @param {any[]} workflowImpacts @returns {any} */
4
+ export function adoptionStatusForStep(step, projectionImpacts = [], uiImpacts = [], workflowImpacts = []) {
5
+ if (step.action === "skip_duplicate_shape") {
6
+ return "skipped";
7
+ }
8
+ if (step.action === "apply_projection_permission_patch") {
9
+ return "needs_projection_review";
10
+ }
11
+ if (step.action === "apply_projection_auth_patch") {
12
+ return "needs_projection_review";
13
+ }
14
+ if (step.action === "apply_projection_ownership_patch") {
15
+ return "needs_projection_review";
16
+ }
17
+ if (step.action.includes("capability") && projectionImpacts.length > 0) {
18
+ return "needs_projection_review";
19
+ }
20
+ if (step.action.includes("ui_") && uiImpacts.length > 0) {
21
+ return "needs_ui_review";
22
+ }
23
+ if (step.action.includes("workflow") && workflowImpacts.length > 0) {
24
+ return "needs_workflow_review";
25
+ }
26
+ return "pending";
27
+ }
28
+
29
+ /** @param {CandidateBundle} bundle @param {WorkflowRecord} step @returns {any} */
30
+ export function projectionImpactsForAdoptionItem(bundle, step) {
31
+ if (!step.action.includes("capability")) {
32
+ return [];
33
+ }
34
+ return (bundle.projectionImpacts || [])
35
+ .filter((/** @type {any} */ impact) => (impact.missing_capabilities || []).includes(step.item))
36
+ .map((/** @type {any} */ impact) => ({
37
+ projection_id: impact.projection_id,
38
+ kind: impact.kind,
39
+ projection_type: impact.projection_type,
40
+ reason: impact.reason
41
+ }));
42
+ }
43
+
44
+ /** @param {any[]} projectionImpacts @returns {any} */
45
+ export function blockingDependenciesForProjectionImpacts(projectionImpacts) {
46
+ return projectionImpacts.map((/** @type {any} */ impact) => ({
47
+ type: "projection_review",
48
+ id: `projection_review:${impact.projection_id}`,
49
+ projection_id: impact.projection_id,
50
+ kind: impact.kind,
51
+ projection_type: impact.projection_type,
52
+ reason: impact.reason
53
+ }));
54
+ }
55
+
56
+ /** @param {any[]} uiImpacts @returns {any} */
57
+ export function blockingDependenciesForUiImpacts(uiImpacts) {
58
+ return uiImpacts.map((/** @type {any} */ impact) => ({
59
+ type: "ui_review",
60
+ id: `ui_review:${impact.projection_id}`,
61
+ projection_id: impact.projection_id,
62
+ kind: impact.kind,
63
+ projection_type: impact.projection_type,
64
+ reason: impact.reason
65
+ }));
66
+ }
67
+
68
+ /** @param {any[]} workflowImpacts @returns {any} */
69
+ export function blockingDependenciesForWorkflowImpacts(workflowImpacts) {
70
+ return workflowImpacts.map((/** @type {any} */ impact) => ({
71
+ type: "workflow_review",
72
+ id: impact.review_group_id,
73
+ reason: impact.reason
74
+ }));
75
+ }
@@ -0,0 +1,153 @@
1
+ // @ts-check
2
+
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+
6
+ import { adoptionItemKey } from "../../../adoption/plan.js";
7
+ import {
8
+ applyDocLinkPatchToMarkdown as applyDocLinkPatchToMarkdownReconcile,
9
+ applyDocMetadataPatchToMarkdown as applyDocMetadataPatchToMarkdownReconcile
10
+ } from "../../../reconcile/docs.js";
11
+ import { readJsonIfExists, readTextIfExists } from "../../shared.js";
12
+ import { canonicalRelativePathForItem } from "./paths.js";
13
+ import { applyProjectionAuthPatchToTopogram } from "./projection-patches.js";
14
+
15
+ /** @param {WorkspacePaths} paths @returns {any} */
16
+ export function readAdoptionPlan(paths) {
17
+ return readJsonIfExists(path.join(paths.topogramRoot, "candidates", "reconcile", "adoption-plan.json"));
18
+ }
19
+
20
+ /** @param {WorkspacePaths} paths @param {any[]} candidateFiles @param {any[]} planItems @param {any[]} selectedItems @param {WorkflowOptions} options @returns {any} */
21
+ export function buildCanonicalAdoptionOutputs(paths, candidateFiles, planItems, selectedItems, options = {}) {
22
+ const refreshAdopted = Boolean(options.refreshAdopted);
23
+ /** @type {WorkflowFiles} */
24
+ /** @type {WorkflowFiles} */
25
+ const files = {};
26
+ /** @type {any[]} */
27
+ const refreshedFiles = [];
28
+ const selectedSet = new Set(selectedItems);
29
+ const itemMap = new Map(planItems.map((/** @type {any} */ item) => [adoptionItemKey(item), item]));
30
+ const capabilityBundleSet = new Set(
31
+ [...selectedSet]
32
+ .map((/** @type {any} */ key) => itemMap.get(key))
33
+ .filter((/** @type {any} */ item) => item?.kind === "capability")
34
+ .map((/** @type {any} */ item) => item.bundle)
35
+ );
36
+ const widgetShapeIdSet = new Set(
37
+ [...selectedSet]
38
+ .map((/** @type {any} */ key) => itemMap.get(key))
39
+ .filter((/** @type {any} */ item) => item?.kind === "widget")
40
+ .flatMap((/** @type {any} */ item) => item.related_shapes || [])
41
+ );
42
+ for (const item of planItems) {
43
+ if (
44
+ item.kind === "shape" &&
45
+ item.status !== "skipped" &&
46
+ (capabilityBundleSet.has(item.bundle) || widgetShapeIdSet.has(item.item))
47
+ ) {
48
+ selectedSet.add(adoptionItemKey(item));
49
+ }
50
+ }
51
+ for (const item of planItems) {
52
+ if (!selectedSet.has(adoptionItemKey(item))) {
53
+ continue;
54
+ }
55
+ if (item.suggested_action === "skip_duplicate_shape") {
56
+ continue;
57
+ }
58
+ const relativeCanonicalPath = item.canonical_rel_path || canonicalRelativePathForItem(item.kind, item.item);
59
+ if (!relativeCanonicalPath) {
60
+ continue;
61
+ }
62
+ const canonicalPath = path.join(paths.topogramRoot, relativeCanonicalPath);
63
+ if (item.suggested_action === "apply_doc_link_patch") {
64
+ const baseContents = files[relativeCanonicalPath] || (fs.existsSync(canonicalPath) ? fs.readFileSync(canonicalPath, "utf8") : null);
65
+ if (!baseContents) {
66
+ continue;
67
+ }
68
+ const updatedContents = applyDocLinkPatchToMarkdownReconcile(baseContents, item);
69
+ if (!updatedContents || updatedContents === baseContents) {
70
+ continue;
71
+ }
72
+ files[relativeCanonicalPath] = updatedContents;
73
+ refreshedFiles.push(relativeCanonicalPath.replaceAll(path.sep, "/"));
74
+ continue;
75
+ }
76
+ if (item.suggested_action === "apply_doc_metadata_patch") {
77
+ const baseContents = files[relativeCanonicalPath] || (fs.existsSync(canonicalPath) ? fs.readFileSync(canonicalPath, "utf8") : null);
78
+ if (!baseContents) {
79
+ continue;
80
+ }
81
+ const updatedContents = applyDocMetadataPatchToMarkdownReconcile(baseContents, item);
82
+ if (!updatedContents || updatedContents === baseContents) {
83
+ continue;
84
+ }
85
+ files[relativeCanonicalPath] = updatedContents;
86
+ refreshedFiles.push(relativeCanonicalPath.replaceAll(path.sep, "/"));
87
+ continue;
88
+ }
89
+ if (item.suggested_action === "apply_projection_permission_patch" || item.suggested_action === "apply_projection_auth_patch" || item.suggested_action === "apply_projection_ownership_patch") {
90
+ const baseContents = files[relativeCanonicalPath] || (fs.existsSync(canonicalPath) ? fs.readFileSync(canonicalPath, "utf8") : null);
91
+ if (!baseContents) {
92
+ continue;
93
+ }
94
+ const updatedContents = applyProjectionAuthPatchToTopogram(baseContents, item);
95
+ if (!updatedContents || updatedContents === baseContents) {
96
+ continue;
97
+ }
98
+ files[relativeCanonicalPath] = updatedContents;
99
+ refreshedFiles.push(relativeCanonicalPath.replaceAll(path.sep, "/"));
100
+ continue;
101
+ }
102
+ if (fs.existsSync(canonicalPath)) {
103
+ if (!refreshAdopted) {
104
+ continue;
105
+ }
106
+ const existingContents = fs.readFileSync(canonicalPath, "utf8");
107
+ const machineManagedImported =
108
+ existingContents.startsWith("# imported ") ||
109
+ /\bsource_of_truth:\s*imported\b/.test(existingContents);
110
+ if (!machineManagedImported) {
111
+ continue;
112
+ }
113
+ }
114
+ const candidateContents =
115
+ candidateFiles[item.source_path] ||
116
+ (item.source_path ? readTextIfExists(path.join(paths.topogramRoot, item.source_path)) : null);
117
+ if (!candidateContents) {
118
+ continue;
119
+ }
120
+ files[relativeCanonicalPath] = candidateContents;
121
+ if (fs.existsSync(canonicalPath)) {
122
+ refreshedFiles.push(relativeCanonicalPath.replaceAll(path.sep, "/"));
123
+ }
124
+ }
125
+ return { files, refreshedFiles: [...new Set(refreshedFiles)].sort() };
126
+ }
127
+
128
+ /** @param {any[]} planItems @param {any[]} selectedItems @param {any[]} writtenCanonicalFiles @param {string} selector @returns {any} */
129
+ export function buildPromotedCanonicalItems(planItems, selectedItems, writtenCanonicalFiles, selector) {
130
+ const itemMap = new Map((planItems || []).map((/** @type {any} */ item) => [adoptionItemKey(item), item]));
131
+ const writtenSet = new Set((writtenCanonicalFiles || []).map((/** @type {any} */ item) => String(item).replaceAll(path.sep, "/")));
132
+ return [...new Set(selectedItems || [])]
133
+ .map((/** @type {any} */ key) => itemMap.get(key))
134
+ .filter(Boolean)
135
+ .filter((/** @type {any} */ item) => item.canonical_rel_path && writtenSet.has(String(item.canonical_rel_path).replaceAll(path.sep, "/")))
136
+ .map((/** @type {any} */ item) => ({
137
+ selector: selector || null,
138
+ bundle: item.bundle,
139
+ item: item.item,
140
+ kind: item.kind,
141
+ track: item.track || null,
142
+ source_path: item.source_path || null,
143
+ canonical_rel_path: String(item.canonical_rel_path).replaceAll(path.sep, "/"),
144
+ canonical_path: item.canonical_path || `topo/${String(item.canonical_rel_path).replaceAll(path.sep, "/")}`,
145
+ suggested_action: item.suggested_action || null
146
+ }))
147
+ .sort((/** @type {any} */ a, /** @type {any} */ b) =>
148
+ (a.bundle || "").localeCompare(b.bundle || "") ||
149
+ (a.track || "").localeCompare(b.track || "") ||
150
+ (a.kind || "").localeCompare(b.kind || "") ||
151
+ (a.item || "").localeCompare(b.item || "")
152
+ );
153
+ }
@@ -0,0 +1,58 @@
1
+ // @ts-check
2
+
3
+ import { dashedTopogramId } from "../ids.js";
4
+
5
+ /** @param {string} kind @param {string} item @returns {any} */
6
+ export function canonicalRelativePathForItem(kind, item) {
7
+ switch (kind) {
8
+ case "actor":
9
+ return `actors/${dashedTopogramId(item)}.tg`;
10
+ case "role":
11
+ return `roles/${dashedTopogramId(item)}.tg`;
12
+ case "enum":
13
+ return `enums/enum-${dashedTopogramId(String(item || "").replace(/^enum_/, ""))}.tg`;
14
+ case "entity":
15
+ return `entities/${dashedTopogramId(item)}.tg`;
16
+ case "shape":
17
+ return `shapes/${dashedTopogramId(item)}.tg`;
18
+ case "capability":
19
+ return `capabilities/${dashedTopogramId(item)}.tg`;
20
+ case "widget":
21
+ return `widgets/${dashedTopogramId(item)}.tg`;
22
+ case "verification":
23
+ return `verifications/${dashedTopogramId(item)}.tg`;
24
+ default:
25
+ return null;
26
+ }
27
+ }
28
+
29
+ /** @param {string} kind @param {string} item @returns {any} */
30
+ export function canonicalDisplayPathForItem(kind, item) {
31
+ const relativePath = canonicalRelativePathForItem(kind, item);
32
+ return relativePath ? `topo/${relativePath}` : null;
33
+ }
34
+
35
+ /** @param {CandidateBundle} bundle @param {string} kind @param {WorkflowRecord} item @returns {any} */
36
+ export function candidateSourcePathForItem(bundle, kind, item) {
37
+ const base = `candidates/reconcile/model/bundles/${bundle.slug}`;
38
+ switch (kind) {
39
+ case "actor":
40
+ return `${base}/actors/${item}.tg`;
41
+ case "role":
42
+ return `${base}/roles/${item}.tg`;
43
+ case "enum":
44
+ return `${base}/enums/${item}.tg`;
45
+ case "entity":
46
+ return `${base}/entities/${item}.tg`;
47
+ case "shape":
48
+ return `${base}/shapes/${item}.tg`;
49
+ case "capability":
50
+ return `${base}/capabilities/${item}.tg`;
51
+ case "widget":
52
+ return `${base}/widgets/${item}.tg`;
53
+ case "verification":
54
+ return `${base}/verifications/${item}.tg`;
55
+ default:
56
+ return `${base}/README.md`;
57
+ }
58
+ }
@@ -0,0 +1,177 @@
1
+ // @ts-check
2
+
3
+ import { ensureTrailingNewline } from "../../../text-helpers.js";
4
+
5
+ /** @param {any[]} lines @param {string} blockName @returns {any} */
6
+ export function ensureProjectionBlock(lines, blockName) {
7
+ const startIndex = lines.findIndex((/** @type {any} */ line) => new RegExp(`^\\s*${blockName}\\s*\\{\\s*$`).test(line));
8
+ if (startIndex !== -1) {
9
+ let endIndex = -1;
10
+ for (let index = startIndex + 1; index < lines.length; index += 1) {
11
+ if (/^\s*\}\s*$/.test(lines[index])) {
12
+ endIndex = index;
13
+ break;
14
+ }
15
+ }
16
+ if (endIndex !== -1) {
17
+ return { lines, startIndex, endIndex };
18
+ }
19
+ }
20
+ const insertBeforeStatus = lines.findIndex((/** @type {any} */ line) => /^\s*status\s+\w+/.test(line));
21
+ const insertIndex = insertBeforeStatus === -1 ? lines.length : insertBeforeStatus;
22
+ const blockLines = ["", ` ${blockName} {`, " }"];
23
+ lines.splice(insertIndex, 0, ...blockLines);
24
+ return {
25
+ lines,
26
+ startIndex: insertIndex + 1,
27
+ endIndex: insertIndex + 2
28
+ };
29
+ }
30
+
31
+ /** @param {any[]} lines @param {any[]} capabilityIds @returns {any} */
32
+ export function ensureProjectionRealizes(lines, capabilityIds) {
33
+ const startIndex = lines.findIndex((/** @type {any} */ line) => /^\s*realizes\s*\[/.test(line));
34
+ if (startIndex === -1) {
35
+ return { changed: false, lines };
36
+ }
37
+ let endIndex = startIndex;
38
+ while (endIndex < lines.length && !/\]/.test(lines[endIndex])) {
39
+ endIndex += 1;
40
+ }
41
+ if (endIndex >= lines.length) {
42
+ return { changed: false, lines };
43
+ }
44
+ /** @type {any[]} */
45
+ const existingItems = [];
46
+ for (let index = startIndex; index <= endIndex; index += 1) {
47
+ const text = lines[index]
48
+ .replace(/^\s*realizes\s*\[/, "")
49
+ .replace(/\]\s*$/, "")
50
+ .trim();
51
+ if (!text) {
52
+ continue;
53
+ }
54
+ for (const item of text.split(",").map((/** @type {any} */ entry) => entry.trim()).filter(Boolean)) {
55
+ existingItems.push(item);
56
+ }
57
+ }
58
+ const merged = [...new Set([...existingItems, ...(capabilityIds || [])])];
59
+ if (merged.length === existingItems.length) {
60
+ return { changed: false, lines };
61
+ }
62
+ const replacement = [" realizes [", ...merged.map((/** @type {any} */ item, /** @type {any} */ index) => ` ${item}${index === merged.length - 1 ? "" : ","}`), " ]"];
63
+ lines.splice(startIndex, endIndex - startIndex + 1, ...replacement);
64
+ return { changed: true, lines };
65
+ }
66
+
67
+ /** @param {string} baseContents @param {WorkflowRecord} item @returns {any} */
68
+ export function applyProjectionAuthPatchToTopogram(baseContents, item) {
69
+ const lines = String(baseContents || "").replace(/\r\n/g, "\n").split("\n");
70
+ const capabilities = [...new Set(item.related_capabilities || [])];
71
+ let changed = false;
72
+
73
+ const realizesResult = ensureProjectionRealizes(lines, capabilities);
74
+ changed = changed || realizesResult.changed;
75
+
76
+ if (item.projection_surface === "authorization") {
77
+ const block = ensureProjectionBlock(lines, "authorization");
78
+ for (const capabilityId of capabilities) {
79
+ const lineIndex = lines.findIndex((/** @type {any} */ line, /** @type {any} */ index) =>
80
+ index > block.startIndex &&
81
+ index < block.endIndex &&
82
+ new RegExp(`^\\s*${capabilityId}(\\s|$)`).test(line)
83
+ );
84
+ if (item.suggested_action === "apply_projection_ownership_patch") {
85
+ const ownershipFragment = `ownership ${item.ownership}${item.ownership_field ? ` ownership_field ${item.ownership_field}` : ""}`;
86
+ if (lineIndex !== -1) {
87
+ if (!/\bownership\s+/.test(lines[lineIndex])) {
88
+ lines[lineIndex] = `${lines[lineIndex].trimEnd()} ${ownershipFragment}`;
89
+ changed = true;
90
+ }
91
+ continue;
92
+ }
93
+ lines.splice(block.endIndex, 0, ` ${capabilityId} ${ownershipFragment}`);
94
+ block.endIndex += 1;
95
+ changed = true;
96
+ continue;
97
+ }
98
+
99
+ if (item.suggested_action === "apply_projection_permission_patch") {
100
+ const permissionFragment = `permission ${item.permission}`;
101
+ if (lineIndex !== -1) {
102
+ if (!/\bpermission\s+/.test(lines[lineIndex])) {
103
+ lines[lineIndex] = `${lines[lineIndex].trimEnd()} ${permissionFragment}`;
104
+ changed = true;
105
+ }
106
+ continue;
107
+ }
108
+ lines.splice(block.endIndex, 0, ` ${capabilityId} ${permissionFragment}`);
109
+ block.endIndex += 1;
110
+ changed = true;
111
+ continue;
112
+ }
113
+
114
+ const claimFragment = `claim ${item.claim}${item.claim_value != null ? ` claim_value ${item.claim_value}` : ""}`;
115
+ if (lineIndex !== -1) {
116
+ if (!/\bclaim\s+/.test(lines[lineIndex])) {
117
+ lines[lineIndex] = `${lines[lineIndex].trimEnd()} ${claimFragment}`;
118
+ changed = true;
119
+ }
120
+ continue;
121
+ }
122
+ lines.splice(block.endIndex, 0, ` ${capabilityId} ${claimFragment}`);
123
+ block.endIndex += 1;
124
+ changed = true;
125
+ }
126
+ }
127
+
128
+ if (item.projection_surface === "visibility_rules") {
129
+ const block = ensureProjectionBlock(lines, "visibility_rules");
130
+ for (const capabilityId of capabilities) {
131
+ if (item.suggested_action === "apply_projection_permission_patch") {
132
+ const hasExistingPermissionRule = lines.some((/** @type {any} */ line, /** @type {any} */ index) =>
133
+ index > block.startIndex &&
134
+ index < block.endIndex &&
135
+ new RegExp(`^\\s*action\\s+${capabilityId}\\s+visible_if\\s+permission\\s+${item.permission}(\\s|$)`).test(line)
136
+ );
137
+ if (hasExistingPermissionRule) {
138
+ continue;
139
+ }
140
+ const hasAnyVisibilityRule = lines.some((/** @type {any} */ line, /** @type {any} */ index) =>
141
+ index > block.startIndex &&
142
+ index < block.endIndex &&
143
+ new RegExp(`^\\s*action\\s+${capabilityId}\\s+visible_if\\s+`).test(line)
144
+ );
145
+ if (hasAnyVisibilityRule) {
146
+ continue;
147
+ }
148
+ lines.splice(block.endIndex, 0, ` action ${capabilityId} visible_if permission ${item.permission}`);
149
+ block.endIndex += 1;
150
+ changed = true;
151
+ continue;
152
+ }
153
+
154
+ const hasExistingClaimRule = lines.some((/** @type {any} */ line, /** @type {any} */ index) =>
155
+ index > block.startIndex &&
156
+ index < block.endIndex &&
157
+ new RegExp(`^\\s*action\\s+${capabilityId}\\s+visible_if\\s+claim\\s+${item.claim}(\\s|$)`).test(line)
158
+ );
159
+ if (hasExistingClaimRule) {
160
+ continue;
161
+ }
162
+ const hasAnyVisibilityRule = lines.some((/** @type {any} */ line, /** @type {any} */ index) =>
163
+ index > block.startIndex &&
164
+ index < block.endIndex &&
165
+ new RegExp(`^\\s*action\\s+${capabilityId}\\s+visible_if\\s+`).test(line)
166
+ );
167
+ if (hasAnyVisibilityRule) {
168
+ continue;
169
+ }
170
+ lines.splice(block.endIndex, 0, ` action ${capabilityId} visible_if claim ${item.claim}${item.claim_value != null ? ` claim_value ${item.claim_value}` : ""}`);
171
+ block.endIndex += 1;
172
+ changed = true;
173
+ }
174
+ }
175
+
176
+ return changed ? ensureTrailingNewline(lines.join("\n")) : baseContents;
177
+ }