opencastle 0.38.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/README.md +81 -1
  2. package/bin/cli.mjs +20 -3
  3. package/dist/cli/adapters/antigravity.d.ts +1 -1
  4. package/dist/cli/adapters/antigravity.d.ts.map +1 -1
  5. package/dist/cli/adapters/claude-code.d.ts +1 -1
  6. package/dist/cli/adapters/claude-code.d.ts.map +1 -1
  7. package/dist/cli/adapters/codex.d.ts +1 -1
  8. package/dist/cli/adapters/codex.d.ts.map +1 -1
  9. package/dist/cli/adapters/cursor.d.ts +1 -1
  10. package/dist/cli/adapters/cursor.d.ts.map +1 -1
  11. package/dist/cli/adapters/opencode.d.ts +1 -1
  12. package/dist/cli/adapters/opencode.d.ts.map +1 -1
  13. package/dist/cli/adapters/rules-dir-base.d.ts +3 -2
  14. package/dist/cli/adapters/rules-dir-base.d.ts.map +1 -1
  15. package/dist/cli/adapters/rules-dir-base.js +13 -25
  16. package/dist/cli/adapters/rules-dir-base.js.map +1 -1
  17. package/dist/cli/adapters/single-file-base.d.ts.map +1 -1
  18. package/dist/cli/adapters/single-file-base.js +30 -43
  19. package/dist/cli/adapters/single-file-base.js.map +1 -1
  20. package/dist/cli/adapters/vscode.d.ts +3 -2
  21. package/dist/cli/adapters/vscode.d.ts.map +1 -1
  22. package/dist/cli/adapters/vscode.js +19 -71
  23. package/dist/cli/adapters/vscode.js.map +1 -1
  24. package/dist/cli/adapters/windsurf.d.ts +1 -1
  25. package/dist/cli/adapters/windsurf.d.ts.map +1 -1
  26. package/dist/cli/baseline.d.ts +3 -0
  27. package/dist/cli/baseline.d.ts.map +1 -0
  28. package/dist/cli/baseline.js +280 -0
  29. package/dist/cli/baseline.js.map +1 -0
  30. package/dist/cli/ci.d.ts +18 -0
  31. package/dist/cli/ci.d.ts.map +1 -0
  32. package/dist/cli/ci.js +279 -0
  33. package/dist/cli/ci.js.map +1 -0
  34. package/dist/cli/dispatcher.test.js +2 -2
  35. package/dist/cli/dispatcher.test.js.map +1 -1
  36. package/dist/cli/docs-accuracy.test.js +5 -0
  37. package/dist/cli/docs-accuracy.test.js.map +1 -1
  38. package/dist/cli/doctor.d.ts +3 -3
  39. package/dist/cli/doctor.d.ts.map +1 -1
  40. package/dist/cli/doctor.js +66 -9
  41. package/dist/cli/doctor.js.map +1 -1
  42. package/dist/cli/explain.d.ts +3 -0
  43. package/dist/cli/explain.d.ts.map +1 -0
  44. package/dist/cli/explain.js +210 -0
  45. package/dist/cli/explain.js.map +1 -0
  46. package/dist/cli/fleet.d.ts +30 -0
  47. package/dist/cli/fleet.d.ts.map +1 -0
  48. package/dist/cli/fleet.js +149 -0
  49. package/dist/cli/fleet.js.map +1 -0
  50. package/dist/cli/github-report.d.ts +49 -0
  51. package/dist/cli/github-report.d.ts.map +1 -0
  52. package/dist/cli/github-report.js +161 -0
  53. package/dist/cli/github-report.js.map +1 -0
  54. package/dist/cli/github-report.test.d.ts +2 -0
  55. package/dist/cli/github-report.test.d.ts.map +1 -0
  56. package/dist/cli/github-report.test.js +146 -0
  57. package/dist/cli/github-report.test.js.map +1 -0
  58. package/dist/cli/init.d.ts.map +1 -1
  59. package/dist/cli/init.js +135 -94
  60. package/dist/cli/init.js.map +1 -1
  61. package/dist/cli/init.test.js +26 -25
  62. package/dist/cli/init.test.js.map +1 -1
  63. package/dist/cli/layers.d.ts +152 -0
  64. package/dist/cli/layers.d.ts.map +1 -0
  65. package/dist/cli/layers.js +932 -0
  66. package/dist/cli/layers.js.map +1 -0
  67. package/dist/cli/layers.test.d.ts +2 -0
  68. package/dist/cli/layers.test.d.ts.map +1 -0
  69. package/dist/cli/layers.test.js +483 -0
  70. package/dist/cli/layers.test.js.map +1 -0
  71. package/dist/cli/lock.d.ts +130 -0
  72. package/dist/cli/lock.d.ts.map +1 -0
  73. package/dist/cli/lock.js +320 -0
  74. package/dist/cli/lock.js.map +1 -0
  75. package/dist/cli/mcp-audit.d.ts +164 -0
  76. package/dist/cli/mcp-audit.d.ts.map +1 -0
  77. package/dist/cli/mcp-audit.js +504 -0
  78. package/dist/cli/mcp-audit.js.map +1 -0
  79. package/dist/cli/mcp-audit.test.d.ts +2 -0
  80. package/dist/cli/mcp-audit.test.d.ts.map +1 -0
  81. package/dist/cli/mcp-audit.test.js +215 -0
  82. package/dist/cli/mcp-audit.test.js.map +1 -0
  83. package/dist/cli/mcp-upgrade.test.d.ts +2 -0
  84. package/dist/cli/mcp-upgrade.test.d.ts.map +1 -0
  85. package/dist/cli/mcp-upgrade.test.js +118 -0
  86. package/dist/cli/mcp-upgrade.test.js.map +1 -0
  87. package/dist/cli/mcp.d.ts +74 -5
  88. package/dist/cli/mcp.d.ts.map +1 -1
  89. package/dist/cli/mcp.js +340 -32
  90. package/dist/cli/mcp.js.map +1 -1
  91. package/dist/cli/policy.d.ts +58 -0
  92. package/dist/cli/policy.d.ts.map +1 -0
  93. package/dist/cli/policy.js +194 -0
  94. package/dist/cli/policy.js.map +1 -0
  95. package/dist/cli/publish-workflow.test.js +55 -1
  96. package/dist/cli/publish-workflow.test.js.map +1 -1
  97. package/dist/cli/remove.d.ts.map +1 -1
  98. package/dist/cli/remove.js +3 -2
  99. package/dist/cli/remove.js.map +1 -1
  100. package/dist/cli/review.d.ts +25 -0
  101. package/dist/cli/review.d.ts.map +1 -0
  102. package/dist/cli/review.js +437 -0
  103. package/dist/cli/review.js.map +1 -0
  104. package/dist/cli/status.d.ts +2 -0
  105. package/dist/cli/status.d.ts.map +1 -1
  106. package/dist/cli/status.js +25 -4
  107. package/dist/cli/status.js.map +1 -1
  108. package/dist/cli/status.test.js +5 -3
  109. package/dist/cli/status.test.js.map +1 -1
  110. package/dist/cli/sync-check.d.ts +16 -1
  111. package/dist/cli/sync-check.d.ts.map +1 -1
  112. package/dist/cli/sync-check.js +219 -13
  113. package/dist/cli/sync-check.js.map +1 -1
  114. package/dist/cli/sync-check.test.js +14 -8
  115. package/dist/cli/sync-check.test.js.map +1 -1
  116. package/dist/cli/sync.d.ts.map +1 -1
  117. package/dist/cli/sync.js +2 -0
  118. package/dist/cli/sync.js.map +1 -1
  119. package/dist/cli/team-ci.test.d.ts +2 -0
  120. package/dist/cli/team-ci.test.d.ts.map +1 -0
  121. package/dist/cli/team-ci.test.js +169 -0
  122. package/dist/cli/team-ci.test.js.map +1 -0
  123. package/dist/cli/team-commands.test.d.ts +2 -0
  124. package/dist/cli/team-commands.test.d.ts.map +1 -0
  125. package/dist/cli/team-commands.test.js +233 -0
  126. package/dist/cli/team-commands.test.js.map +1 -0
  127. package/dist/cli/team-config.d.ts +88 -0
  128. package/dist/cli/team-config.d.ts.map +1 -0
  129. package/dist/cli/team-config.js +261 -0
  130. package/dist/cli/team-config.js.map +1 -0
  131. package/dist/cli/team-health.d.ts +49 -0
  132. package/dist/cli/team-health.d.ts.map +1 -0
  133. package/dist/cli/team-health.js +205 -0
  134. package/dist/cli/team-health.js.map +1 -0
  135. package/dist/cli/team-sources.test.d.ts +2 -0
  136. package/dist/cli/team-sources.test.d.ts.map +1 -0
  137. package/dist/cli/team-sources.test.js +542 -0
  138. package/dist/cli/team-sources.test.js.map +1 -0
  139. package/dist/cli/types.d.ts +16 -2
  140. package/dist/cli/types.d.ts.map +1 -1
  141. package/dist/cli/types.js.map +1 -1
  142. package/dist/cli/update.d.ts.map +1 -1
  143. package/dist/cli/update.js +204 -44
  144. package/dist/cli/update.js.map +1 -1
  145. package/dist/cli/version-range.d.ts +24 -0
  146. package/dist/cli/version-range.d.ts.map +1 -0
  147. package/dist/cli/version-range.js +157 -0
  148. package/dist/cli/version-range.js.map +1 -0
  149. package/dist/cli/website-plugins.test.d.ts +2 -0
  150. package/dist/cli/website-plugins.test.d.ts.map +1 -0
  151. package/dist/cli/website-plugins.test.js +34 -0
  152. package/dist/cli/website-plugins.test.js.map +1 -0
  153. package/dist/orchestrator/plugins/chrome-devtools/config.d.ts.map +1 -1
  154. package/dist/orchestrator/plugins/chrome-devtools/config.js +11 -1
  155. package/dist/orchestrator/plugins/chrome-devtools/config.js.map +1 -1
  156. package/dist/orchestrator/plugins/contentful/config.d.ts.map +1 -1
  157. package/dist/orchestrator/plugins/contentful/config.js +11 -1
  158. package/dist/orchestrator/plugins/contentful/config.js.map +1 -1
  159. package/dist/orchestrator/plugins/convex/config.d.ts.map +1 -1
  160. package/dist/orchestrator/plugins/convex/config.js +11 -1
  161. package/dist/orchestrator/plugins/convex/config.js.map +1 -1
  162. package/dist/orchestrator/plugins/coolify/config.d.ts.map +1 -1
  163. package/dist/orchestrator/plugins/coolify/config.js +14 -1
  164. package/dist/orchestrator/plugins/coolify/config.js.map +1 -1
  165. package/dist/orchestrator/plugins/figma/config.d.ts.map +1 -1
  166. package/dist/orchestrator/plugins/figma/config.js +24 -13
  167. package/dist/orchestrator/plugins/figma/config.js.map +1 -1
  168. package/dist/orchestrator/plugins/linear/config.d.ts.map +1 -1
  169. package/dist/orchestrator/plugins/linear/config.js +27 -17
  170. package/dist/orchestrator/plugins/linear/config.js.map +1 -1
  171. package/dist/orchestrator/plugins/netlify/config.d.ts.map +1 -1
  172. package/dist/orchestrator/plugins/netlify/config.js +23 -9
  173. package/dist/orchestrator/plugins/netlify/config.js.map +1 -1
  174. package/dist/orchestrator/plugins/nextjs/config.d.ts.map +1 -1
  175. package/dist/orchestrator/plugins/nextjs/config.js +11 -1
  176. package/dist/orchestrator/plugins/nextjs/config.js.map +1 -1
  177. package/dist/orchestrator/plugins/nx/config.d.ts.map +1 -1
  178. package/dist/orchestrator/plugins/nx/config.js +11 -1
  179. package/dist/orchestrator/plugins/nx/config.js.map +1 -1
  180. package/dist/orchestrator/plugins/pins.test.d.ts +2 -0
  181. package/dist/orchestrator/plugins/pins.test.d.ts.map +1 -0
  182. package/dist/orchestrator/plugins/pins.test.js +78 -0
  183. package/dist/orchestrator/plugins/pins.test.js.map +1 -0
  184. package/dist/orchestrator/plugins/playwright/config.d.ts.map +1 -1
  185. package/dist/orchestrator/plugins/playwright/config.js +11 -1
  186. package/dist/orchestrator/plugins/playwright/config.js.map +1 -1
  187. package/dist/orchestrator/plugins/prisma/config.d.ts.map +1 -1
  188. package/dist/orchestrator/plugins/prisma/config.js +17 -2
  189. package/dist/orchestrator/plugins/prisma/config.js.map +1 -1
  190. package/dist/orchestrator/plugins/resend/config.d.ts.map +1 -1
  191. package/dist/orchestrator/plugins/resend/config.js +17 -1
  192. package/dist/orchestrator/plugins/resend/config.js.map +1 -1
  193. package/dist/orchestrator/plugins/sentry/config.d.ts.map +1 -1
  194. package/dist/orchestrator/plugins/sentry/config.js +13 -1
  195. package/dist/orchestrator/plugins/sentry/config.js.map +1 -1
  196. package/dist/orchestrator/plugins/slack/config.d.ts.map +1 -1
  197. package/dist/orchestrator/plugins/slack/config.js +20 -1
  198. package/dist/orchestrator/plugins/slack/config.js.map +1 -1
  199. package/dist/orchestrator/plugins/strapi/config.d.ts.map +1 -1
  200. package/dist/orchestrator/plugins/strapi/config.js +11 -1
  201. package/dist/orchestrator/plugins/strapi/config.js.map +1 -1
  202. package/dist/orchestrator/plugins/trello/config.d.ts.map +1 -1
  203. package/dist/orchestrator/plugins/trello/config.js +21 -1
  204. package/dist/orchestrator/plugins/trello/config.js.map +1 -1
  205. package/dist/orchestrator/plugins/types.d.ts +22 -0
  206. package/dist/orchestrator/plugins/types.d.ts.map +1 -1
  207. package/package.json +3 -1
  208. package/src/cli/adapters/rules-dir-base.ts +23 -44
  209. package/src/cli/adapters/single-file-base.ts +43 -46
  210. package/src/cli/adapters/vscode.ts +28 -71
  211. package/src/cli/baseline.ts +289 -0
  212. package/src/cli/ci.ts +289 -0
  213. package/src/cli/dispatcher.test.ts +2 -2
  214. package/src/cli/docs-accuracy.test.ts +5 -0
  215. package/src/cli/doctor.ts +92 -6
  216. package/src/cli/explain.ts +210 -0
  217. package/src/cli/fleet.ts +176 -0
  218. package/src/cli/github-report.test.ts +158 -0
  219. package/src/cli/github-report.ts +179 -0
  220. package/src/cli/init.test.ts +27 -27
  221. package/src/cli/init.ts +135 -93
  222. package/src/cli/layers.test.ts +525 -0
  223. package/src/cli/layers.ts +1089 -0
  224. package/src/cli/lock.ts +395 -0
  225. package/src/cli/mcp-audit.test.ts +259 -0
  226. package/src/cli/mcp-audit.ts +619 -0
  227. package/src/cli/mcp-upgrade.test.ts +135 -0
  228. package/src/cli/mcp.ts +373 -34
  229. package/src/cli/policy.ts +207 -0
  230. package/src/cli/publish-workflow.test.ts +60 -1
  231. package/src/cli/remove.ts +8 -2
  232. package/src/cli/review.ts +457 -0
  233. package/src/cli/status.test.ts +5 -3
  234. package/src/cli/status.ts +29 -4
  235. package/src/cli/sync-check.test.ts +16 -8
  236. package/src/cli/sync-check.ts +245 -14
  237. package/src/cli/sync.ts +2 -0
  238. package/src/cli/team-ci.test.ts +195 -0
  239. package/src/cli/team-commands.test.ts +253 -0
  240. package/src/cli/team-config.ts +289 -0
  241. package/src/cli/team-health.ts +234 -0
  242. package/src/cli/team-sources.test.ts +578 -0
  243. package/src/cli/types.ts +19 -1
  244. package/src/cli/update.ts +217 -44
  245. package/src/cli/version-range.ts +150 -0
  246. package/src/cli/website-plugins.test.ts +41 -0
  247. package/src/dashboard/node_modules/.vite/deps/_metadata.json +6 -6
  248. package/src/orchestrator/plugins/chrome-devtools/config.ts +11 -1
  249. package/src/orchestrator/plugins/contentful/config.ts +11 -1
  250. package/src/orchestrator/plugins/convex/config.ts +11 -1
  251. package/src/orchestrator/plugins/coolify/config.ts +14 -1
  252. package/src/orchestrator/plugins/figma/SKILL.md +18 -14
  253. package/src/orchestrator/plugins/figma/config.ts +24 -13
  254. package/src/orchestrator/plugins/linear/SKILL.md +6 -14
  255. package/src/orchestrator/plugins/linear/config.ts +27 -17
  256. package/src/orchestrator/plugins/netlify/config.ts +23 -9
  257. package/src/orchestrator/plugins/nextjs/config.ts +11 -1
  258. package/src/orchestrator/plugins/nx/config.ts +11 -1
  259. package/src/orchestrator/plugins/pins.test.ts +80 -0
  260. package/src/orchestrator/plugins/playwright/config.ts +11 -1
  261. package/src/orchestrator/plugins/prisma/config.ts +17 -2
  262. package/src/orchestrator/plugins/resend/config.ts +17 -1
  263. package/src/orchestrator/plugins/sentry/config.ts +13 -1
  264. package/src/orchestrator/plugins/slack/config.ts +20 -1
  265. package/src/orchestrator/plugins/strapi/config.ts +11 -1
  266. package/src/orchestrator/plugins/trello/config.ts +21 -1
  267. package/src/orchestrator/plugins/types.ts +24 -0
@@ -0,0 +1,234 @@
1
+ import { existsSync, readFileSync, statSync } from 'node:fs'
2
+ import { join, resolve } from 'node:path'
3
+ import { expectedTeamEntries } from './mcp.js'
4
+ import { resolveStack } from './stack-config.js'
5
+ import {
6
+ resolveSources,
7
+ materialize,
8
+ mcpPlan,
9
+ hasErrors,
10
+ usesTeamSources,
11
+ filesUnder,
12
+ type ResolvedSources,
13
+ type TeamMcpPlan,
14
+ } from './layers.js'
15
+ import { contentReport, priorTeam, tokensOf } from './lock.js'
16
+ import { parseVersion, compareVersions } from './version-range.js'
17
+ import type { TeamAuditContext } from './mcp-audit.js'
18
+ import type { IdeChoice, Manifest } from './types.js'
19
+
20
+ /**
21
+ * `doctor`'s questions about the team's own sources: do they resolve, how much
22
+ * does every assistant load before it reads the task, and do the instructions
23
+ * still describe this repository.
24
+ */
25
+
26
+ export interface HealthResult {
27
+ ok: boolean
28
+ label: string
29
+ detail?: string
30
+ warning?: boolean
31
+ fix?: string
32
+ }
33
+
34
+ export interface TeamState {
35
+ resolved: ResolvedSources
36
+ plan: TeamMcpPlan
37
+ }
38
+
39
+ /** Resolve the project's sources the way `sync` does. */
40
+ export function teamStateFor(pkgRoot: string, projectRoot: string, manifest: Manifest): TeamState {
41
+ const stack = resolveStack(manifest)
42
+ const resolved = resolveSources({ pkgRoot, projectRoot, stack, repoInfo: manifest.repoInfo })
43
+ return { resolved, plan: mcpPlan(resolved, ...priorTeam(projectRoot)) }
44
+ }
45
+
46
+ /** What the MCP audit needs to hold one target's config to the team's decisions. */
47
+ export function teamAuditContext(state: TeamState, ide: IdeChoice): TeamAuditContext {
48
+ return {
49
+ expected: expectedTeamEntries(state.plan, ide),
50
+ definedIn: Object.fromEntries([...state.resolved.servers.values()].map((s) => [s.key, s.where])),
51
+ retired: state.plan.retired,
52
+ blocked: state.resolved.blocked,
53
+ policy: state.resolved.policy,
54
+ }
55
+ }
56
+
57
+ const SOURCES = 'Team sources'
58
+
59
+ export function checkTeamSources(state: TeamState): HealthResult {
60
+ const { resolved } = state
61
+ const errors = resolved.issues.filter((i) => i.level === 'error')
62
+ const warnings = resolved.issues.filter((i) => i.level === 'warning')
63
+ if (errors.length > 0) {
64
+ const first = errors[0]
65
+ return {
66
+ ok: false,
67
+ label: SOURCES,
68
+ detail: errors.map((e) => `${e.where}: ${e.message}`).join('; '),
69
+ fix: first.fix ? `${first.fix}${errors.length > 1 ? ` (and ${errors.length - 1} more above)` : ''}` : 'fix the problems above; sync refuses to compile until then',
70
+ }
71
+ }
72
+ const baselines = resolved.layers.filter((l) => l.kind === 'baseline')
73
+ const own = [...resolved.items.values()].filter((i) => i.layer !== 'opencastle')
74
+ const counts = (['skills', 'agents', 'instructions', 'prompts', 'workflows'] as const)
75
+ .map((k) => [k, own.filter((i) => i.kind === k).length] as const)
76
+ .filter(([, n]) => n > 0)
77
+ .map(([k, n]) => `${n} ${n === 1 ? k.replace(/s$/, '') : k}`)
78
+ const detail = usesTeamSources(resolved)
79
+ ? [
80
+ baselines.length > 0 ? `extends ${baselines.map((b) => `${b.id}${b.version ? `@${b.version}` : ''}`).join(', ')}` : '',
81
+ counts.length > 0 ? `team content: ${counts.join(', ')}` : '',
82
+ resolved.servers.size > 0 ? `${resolved.servers.size} team MCP server(s)` : '',
83
+ resolved.policy.allowLists.length > 0 ? 'MCP allowlist in force' : '',
84
+ ]
85
+ .filter(Boolean)
86
+ .join('; ')
87
+ : `OpenCastle's own content only — add .opencastle/config.json to extend a baseline or add your own`
88
+ if (warnings.length > 0) {
89
+ return {
90
+ ok: true,
91
+ warning: true,
92
+ label: SOURCES,
93
+ detail: `${detail}; ${warnings.map((w) => `${w.where}: ${w.message}`).join('; ')}`,
94
+ fix: warnings.map((w) => w.fix).filter(Boolean).join('; ') || undefined,
95
+ }
96
+ }
97
+ return { ok: true, label: SOURCES, detail }
98
+ }
99
+
100
+ const CONTEXT = 'Always-loaded context'
101
+
102
+ function k(tokens: number): string {
103
+ return tokens >= 1000 ? `${(tokens / 1000).toFixed(1)}k` : String(tokens)
104
+ }
105
+
106
+ /**
107
+ * What every assistant reads before the task: the instructions, and the index
108
+ * of skills and agents that tells it what else exists. Context is finite and
109
+ * attention over it is not uniform, so a team that keeps adding "always" rules
110
+ * pays for each one on every request. Reported always; a warning only past the
111
+ * budget a layer set, with the biggest contributors named — the usual fix is to
112
+ * move detail out of an instruction into a skill that loads on demand.
113
+ */
114
+ export function checkContextBudget(pkgRoot: string, state: TeamState): HealthResult {
115
+ if (hasErrors(state.resolved)) return { ok: true, label: CONTEXT, detail: 'not measured — the team sources have errors (above)' }
116
+ const source = materialize(state.resolved, pkgRoot)
117
+ try {
118
+ const report = contentReport(source)
119
+ const total = tokensOf(report.instructionChars + report.indexChars)
120
+ const detail = `~${k(total)} tokens (instructions ~${k(tokensOf(report.instructionChars))}, skill and agent index ~${k(tokensOf(report.indexChars))})`
121
+ const budget = state.resolved.policy.contextBudget
122
+ if (!budget || total <= budget.tokens) {
123
+ return { ok: true, label: CONTEXT, detail: budget ? `${detail}; budget ${k(budget.tokens)} (${budget.by})` : detail }
124
+ }
125
+ const top = [...report.cost]
126
+ .sort((a, b) => b.chars - a.chars)
127
+ .slice(0, 3)
128
+ .map((c) => `${c.ref} ~${k(tokensOf(c.chars))}${c.from === 'opencastle' ? '' : ` (${c.from})`}`)
129
+ return {
130
+ ok: true,
131
+ warning: true,
132
+ label: CONTEXT,
133
+ detail: `${detail} — over the ${k(budget.tokens)} budget ${budget.by} set; largest: ${top.join(', ')}`,
134
+ fix: 'move detail out of always-loaded instructions into a skill, which loads only when a task needs it; or exclude what this repository does not use',
135
+ }
136
+ } finally {
137
+ source.dispose()
138
+ }
139
+ }
140
+
141
+ const REFERENCES = 'Team instructions match this repository'
142
+
143
+ /** Scripts named in instructions, as `npm run <name>` and friends. */
144
+ const SCRIPT_RUN = /\b(?:npm|pnpm|yarn|bun)\s+run\s+([A-Za-z0-9:_.][A-Za-z0-9:_.-]*)/g
145
+ /** A repository path written in backticks. */
146
+ const PATH_SPAN = /`([A-Za-z0-9_.@-][A-Za-z0-9_.@/-]*\/[A-Za-z0-9_.@/-]*[A-Za-z0-9_-])`/g
147
+
148
+ /**
149
+ * Instructions rot quietly. A renamed script or a moved directory leaves an
150
+ * instruction telling every agent to run a command that fails or read a path
151
+ * that is gone — and an agent follows it. Checked only in the team's own
152
+ * layers: OpenCastle's content is generic, and a baseline's paths belong to no
153
+ * repository in particular, though the scripts a baseline names must exist in
154
+ * every repository that extends it.
155
+ */
156
+ export function checkReferences(state: TeamState, projectRoot: string): HealthResult {
157
+ let scripts: Record<string, string> | null = null
158
+ try {
159
+ scripts = (JSON.parse(readFileSync(join(projectRoot, 'package.json'), 'utf8')) as { scripts?: Record<string, string> }).scripts ?? {}
160
+ } catch {
161
+ scripts = null
162
+ }
163
+ const dead: string[] = []
164
+ for (const item of state.resolved.items.values()) {
165
+ if (item.layer === 'opencastle') continue
166
+ const files = statSync(item.path).isDirectory()
167
+ ? filesUnder(item.path).filter((f) => f.endsWith('.md')).map((f) => join(item.path, f))
168
+ : [item.path]
169
+ for (const file of files) {
170
+ let text: string
171
+ try {
172
+ text = readFileSync(file, 'utf8')
173
+ } catch {
174
+ continue
175
+ }
176
+ const ref = `${item.kind}/${item.name}`
177
+ if (scripts) {
178
+ for (const m of text.matchAll(SCRIPT_RUN)) {
179
+ if (!(m[1] in scripts)) dead.push(`${ref} runs "${m[0]}", which package.json does not define`)
180
+ }
181
+ }
182
+ if (item.layer !== 'project') continue
183
+ for (const m of text.matchAll(PATH_SPAN)) {
184
+ const path = m[1].replace(/:\d+(?::\d+)?$/, '').replace(/\/$/, '')
185
+ if (path.includes('://') || path.startsWith('@')) continue
186
+ const first = path.split('/')[0]
187
+ // Only when the path starts in a directory this repository has: a
188
+ // generic example like `src/foo` in a repo with no `src/` is prose, not
189
+ // a reference.
190
+ const top = resolve(projectRoot, first)
191
+ if (!existsSync(top) || !statSync(top).isDirectory()) continue
192
+ if (!existsSync(resolve(projectRoot, path))) dead.push(`${ref} names \`${path}\`, which does not exist`)
193
+ }
194
+ }
195
+ }
196
+ const unique = [...new Set(dead)]
197
+ if (unique.length === 0) return { ok: true, label: REFERENCES, detail: 'every script and path the team content names exists' }
198
+ return {
199
+ ok: true,
200
+ warning: true,
201
+ label: REFERENCES,
202
+ detail: unique.slice(0, 5).join('; ') + (unique.length > 5 ? `; and ${unique.length - 5} more` : ''),
203
+ fix: 'update the instruction (in .opencastle/, or in the baseline that ships it), or restore what it names',
204
+ }
205
+ }
206
+
207
+ const VERSION = 'OpenCastle version'
208
+
209
+ /**
210
+ * A teammate on an older OpenCastle than the one that compiled the project
211
+ * would, on `sync`, replace newer output with older — and the next person
212
+ * would put it back. Named here; `sync` refuses it.
213
+ */
214
+ export function checkVersionSkew(cliVersion: string, manifest: Manifest): HealthResult {
215
+ const mine = parseVersion(cliVersion)
216
+ const theirs = parseVersion(manifest.version)
217
+ if (!mine || !theirs) return { ok: true, label: VERSION, detail: `running ${cliVersion}` }
218
+ const cmp = compareVersions(mine, theirs)
219
+ if (cmp < 0) {
220
+ // A failure, not a warning: `sync` refuses to run, and `sync --check`
221
+ // compares nothing, until the project's version is the one running.
222
+ return {
223
+ ok: false,
224
+ label: VERSION,
225
+ detail: `running ${cliVersion}, older than the ${manifest.version} that last compiled this project`,
226
+ fix: `use the project's version — add opencastle@${manifest.version} to devDependencies and run npx opencastle, so everyone runs the same one`,
227
+ }
228
+ }
229
+ return {
230
+ ok: true,
231
+ label: VERSION,
232
+ detail: cmp === 0 ? `${cliVersion}, the version that compiled this project` : `running ${cliVersion}; the project was compiled by ${manifest.version} — sync upgrades it`,
233
+ }
234
+ }