ultra-builder-pro-cli 0.25.1 → 0.26.1

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 (243) hide show
  1. package/.ultra-template/changes/abandoned/.gitkeep +0 -0
  2. package/.ultra-template/contexts/TEMPLATE.md +94 -0
  3. package/.ultra-template/decisions/.gitkeep +1 -0
  4. package/.ultra-template/evidence/.gitkeep +1 -0
  5. package/.ultra-template/north-star.md +46 -0
  6. package/.ultra-template/research/.gitkeep +1 -0
  7. package/.ultra-template/specs/discovery.md +2 -2
  8. package/.ultra-template/tasks.json +3 -0
  9. package/.ultra-template/test-report.json +27 -0
  10. package/CHANGELOG.md +107 -1
  11. package/README.md +217 -579
  12. package/adapters/_shared/delegate-profiles/kimi-read-only.md +15 -0
  13. package/adapters/_shared/delegate-profiles/kimi-write.md +17 -0
  14. package/adapters/_shared/file-ops.cjs +76 -2
  15. package/adapters/_shared/host-profile.cjs +97 -0
  16. package/adapters/_shared/plugin-core.cjs +204 -0
  17. package/adapters/_shared/runtime-assets.cjs +31 -96
  18. package/adapters/claude.js +92 -291
  19. package/adapters/codex.js +309 -842
  20. package/adapters/grok.js +143 -1469
  21. package/adapters/kimi.js +124 -1053
  22. package/adapters/opencode.js +230 -742
  23. package/bin/delegate-worker.cjs +328 -0
  24. package/bin/delegate.cjs +463 -0
  25. package/bin/install.js +38 -11
  26. package/docs/ARCHITECTURE.md +125 -216
  27. package/docs/ARTIFACT-AUTHORITY.md +90 -268
  28. package/docs/DECISIONS.md +77 -87
  29. package/docs/PHILOSOPHY.md +208 -0
  30. package/docs/PLUGIN-ISOLATION-CONTRACT.md +65 -39
  31. package/docs/RUNTIME-COMPAT-MATRIX.md +140 -155
  32. package/docs/SKILL-AUTHORING.md +94 -73
  33. package/docs/WORKFLOW-LIFECYCLE.md +97 -189
  34. package/hooks/README.md +19 -37
  35. package/hooks/_common.py +113 -0
  36. package/hooks/adapters/codex.py +9 -16
  37. package/hooks/adapters/grok.py +5 -5
  38. package/hooks/adapters/kimi.py +8 -14
  39. package/hooks/block_dangerous_commands.py +201 -0
  40. package/hooks/compact_context.py +65 -0
  41. package/hooks/mid_workflow_recall.py +25 -0
  42. package/hooks/post_edit_guard.py +105 -0
  43. package/hooks/session_context.py +30 -0
  44. package/package.json +17 -52
  45. package/skills/ultra-change/SKILL.md +115 -50
  46. package/skills/ultra-change/references/change-contract.md +68 -3
  47. package/skills/ultra-delegate/SKILL.md +89 -0
  48. package/skills/ultra-delegate/references/delegation-contract.md +59 -0
  49. package/skills/ultra-delegate/scripts/delegate_wait.py +86 -0
  50. package/skills/ultra-deliver/SKILL.md +98 -42
  51. package/skills/ultra-deliver/references/baseline-reconciliation.md +39 -20
  52. package/skills/ultra-dev/SKILL.md +115 -62
  53. package/skills/ultra-dev/references/debugging.md +22 -0
  54. package/skills/ultra-domain-modeling/SKILL.md +79 -0
  55. package/skills/ultra-grilling/SKILL.md +118 -0
  56. package/skills/ultra-grilling/references/reframing.md +72 -0
  57. package/skills/ultra-init/SKILL.md +102 -37
  58. package/skills/ultra-init/references/brownfield-adoption.md +33 -83
  59. package/skills/ultra-init/scripts/init_project.cjs +84 -0
  60. package/skills/ultra-plan/SKILL.md +101 -68
  61. package/skills/ultra-research/SKILL.md +68 -48
  62. package/skills/ultra-research/references/00-problem-validation.md +5 -5
  63. package/skills/ultra-research/references/01-opportunity-discovery.md +3 -3
  64. package/skills/ultra-research/references/02-market-assessment.md +5 -4
  65. package/skills/ultra-research/references/03-competitive-landscape.md +3 -3
  66. package/skills/ultra-research/references/04-product-strategy.md +3 -3
  67. package/skills/ultra-research/references/05-assumptions-validation.md +5 -5
  68. package/skills/ultra-research/references/10-user-personas.md +3 -2
  69. package/skills/ultra-research/references/11-user-scenarios.md +3 -3
  70. package/skills/ultra-research/references/20-user-stories.md +4 -3
  71. package/skills/ultra-research/references/21-features-scope.md +3 -2
  72. package/skills/ultra-research/references/22-success-metrics.md +3 -3
  73. package/skills/ultra-research/references/30-architecture-context.md +3 -3
  74. package/skills/ultra-research/references/31-solution-strategy.md +3 -3
  75. package/skills/ultra-research/references/32-building-blocks.md +3 -3
  76. package/skills/ultra-research/references/40-deployment.md +3 -3
  77. package/skills/ultra-research/references/41-quality-risks.md +3 -3
  78. package/skills/ultra-research/references/99-synthesis.md +7 -7
  79. package/skills/ultra-review/SKILL.md +84 -32
  80. package/skills/ultra-review/references/code.md +17 -0
  81. package/skills/ultra-review/references/comments.md +12 -0
  82. package/skills/ultra-review/references/design.md +12 -0
  83. package/skills/ultra-review/references/errors.md +13 -0
  84. package/skills/ultra-review/references/spec.md +13 -0
  85. package/skills/ultra-review/references/tests.md +12 -0
  86. package/skills/ultra-review/references/worker-packet.md +40 -0
  87. package/skills/ultra-review/scripts/review_wait.py +26 -15
  88. package/skills/ultra-status/SKILL.md +61 -36
  89. package/skills/ultra-tdd/SKILL.md +81 -0
  90. package/skills/ultra-tdd/references/templates/README.md +20 -0
  91. package/skills/ultra-tdd/references/templates/feature-flag-default-audit.sh +79 -0
  92. package/skills/ultra-tdd/references/templates/persistence-real.ts +78 -0
  93. package/skills/ultra-tdd/references/templates/testcontainer-postgres.py +78 -0
  94. package/skills/ultra-tdd/references/templates/testcontainer-postgres.ts +73 -0
  95. package/skills/ultra-tdd/references/templates/vertical-slice.ts +66 -0
  96. package/skills/ultra-tdd/references/test-execution.md +18 -0
  97. package/skills/ultra-test/SKILL.md +75 -30
  98. package/skills/ultra-test/references/export-syntax.md +18 -0
  99. package/skills/ultra-test/scripts/worktree_digest.cjs +54 -0
  100. package/skills/ultra-think/SKILL.md +49 -27
  101. package/skills/ultra-think/references/autonomy-boundary.md +48 -0
  102. package/skills/ultra-think/references/interaction-boundary.md +18 -33
  103. package/.ultra-template/docs/research/README.md +0 -55
  104. package/.ultra-template/reports/templates/delivery-report.json +0 -17
  105. package/.ultra-template/reports/templates/test-report.json +0 -18
  106. package/.ultra-template/tasks/tasks.json +0 -15
  107. package/.ultra-template/templates/task-context.md +0 -19
  108. package/adapters/_shared/codex-assets.cjs +0 -725
  109. package/adapters/_shared/codex-ultra-tools-entry.cjs +0 -11
  110. package/adapters/_shared/interaction-contract.cjs +0 -129
  111. package/adapters/_shared/path-rewrite.cjs +0 -79
  112. package/adapters/_shared/runtime-native.cjs +0 -132
  113. package/adapters/_shared/settings-merge.cjs +0 -76
  114. package/agents/code-reviewer.md +0 -42
  115. package/agents/debugger.md +0 -40
  116. package/agents/review-code.md +0 -49
  117. package/agents/review-comments.md +0 -38
  118. package/agents/review-coordinator.md +0 -56
  119. package/agents/review-design.md +0 -40
  120. package/agents/review-errors.md +0 -43
  121. package/agents/review-spec.md +0 -39
  122. package/agents/review-tests.md +0 -40
  123. package/agents/tdd-runner.md +0 -38
  124. package/bin/orchestrator.js +0 -383
  125. package/commands/ultra-change.md +0 -10
  126. package/commands/ultra-deliver.md +0 -10
  127. package/commands/ultra-dev.md +0 -10
  128. package/commands/ultra-doctor.md +0 -10
  129. package/commands/ultra-init.md +0 -10
  130. package/commands/ultra-plan.md +0 -10
  131. package/commands/ultra-research.md +0 -10
  132. package/commands/ultra-review.md +0 -10
  133. package/commands/ultra-status.md +0 -10
  134. package/commands/ultra-test.md +0 -10
  135. package/commands/ultra-think.md +0 -10
  136. package/docs/AGENT-CONTEXT.md +0 -191
  137. package/docs/COMMIT-HASH-BACKFILL.md +0 -51
  138. package/docs/LEGACY-CLI-CROSSWALK.md +0 -175
  139. package/docs/ROADMAP.md +0 -101
  140. package/docs/STATE-DB-ACCESS-POLICY.md +0 -161
  141. package/hooks/active_task_context.py +0 -152
  142. package/hooks/context_envelope.py +0 -100
  143. package/hooks/health_check.py +0 -196
  144. package/hooks/pre_stop_check.py +0 -66
  145. package/hooks/runtime_paths.py +0 -396
  146. package/hooks/subagent_tracker.py +0 -88
  147. package/hooks/workflow_checkpoint.py +0 -76
  148. package/hooks/workflow_context.py +0 -48
  149. package/hooks/workflow_resume.py +0 -84
  150. package/mcp-server/hook-context.cjs +0 -38
  151. package/mcp-server/hook-event.cjs +0 -34
  152. package/mcp-server/lib/archive-journal.cjs +0 -1119
  153. package/mcp-server/lib/archive-mutation-worker.py +0 -365
  154. package/mcp-server/lib/artifact-registry.cjs +0 -1232
  155. package/mcp-server/lib/baseline-workflow.cjs +0 -1193
  156. package/mcp-server/lib/canonical-json.cjs +0 -19
  157. package/mcp-server/lib/change-authority.cjs +0 -26
  158. package/mcp-server/lib/change-packet.cjs +0 -790
  159. package/mcp-server/lib/change-workflow.cjs +0 -65
  160. package/mcp-server/lib/context-envelope.cjs +0 -581
  161. package/mcp-server/lib/context-paths.cjs +0 -142
  162. package/mcp-server/lib/decision-records.cjs +0 -423
  163. package/mcp-server/lib/delivery-transaction.cjs +0 -471
  164. package/mcp-server/lib/doctor-backup-worker.cjs +0 -328
  165. package/mcp-server/lib/doctor.cjs +0 -627
  166. package/mcp-server/lib/git-bootstrap.cjs +0 -651
  167. package/mcp-server/lib/init-project.cjs +0 -1021
  168. package/mcp-server/lib/kernel-change-workflow.cjs +0 -2029
  169. package/mcp-server/lib/legacy-change-workflow.cjs +0 -3032
  170. package/mcp-server/lib/managed-file-write.cjs +0 -172
  171. package/mcp-server/lib/plan-checkpoint.cjs +0 -177
  172. package/mcp-server/lib/plan-publication-worker.cjs +0 -830
  173. package/mcp-server/lib/plan-store.cjs +0 -794
  174. package/mcp-server/lib/prd-parser.cjs +0 -83
  175. package/mcp-server/lib/pricing.cjs +0 -44
  176. package/mcp-server/lib/project-context.cjs +0 -178
  177. package/mcp-server/lib/projector.cjs +0 -386
  178. package/mcp-server/lib/provider-refs.cjs +0 -42
  179. package/mcp-server/lib/runtime-paths.cjs +0 -1714
  180. package/mcp-server/lib/runtime-state.cjs +0 -222
  181. package/mcp-server/lib/safe-project-file.cjs +0 -385
  182. package/mcp-server/lib/stage-checkpoints.cjs +0 -463
  183. package/mcp-server/lib/state-authority.cjs +0 -95
  184. package/mcp-server/lib/state-db.cjs +0 -1580
  185. package/mcp-server/lib/state-ops.cjs +0 -1428
  186. package/mcp-server/lib/task-contract.cjs +0 -404
  187. package/mcp-server/lib/task-expander.cjs +0 -105
  188. package/mcp-server/lib/task-ledger.cjs +0 -1964
  189. package/mcp-server/lib/telemetry.cjs +0 -85
  190. package/mcp-server/lib/topo.cjs +0 -131
  191. package/mcp-server/lib/ultra-facade.cjs +0 -2684
  192. package/mcp-server/lib/worker-packet.cjs +0 -358
  193. package/mcp-server/server.cjs +0 -437
  194. package/orchestrator/auto-merge.cjs +0 -128
  195. package/orchestrator/daemon.cjs +0 -264
  196. package/orchestrator/dispatch-rules.cjs +0 -78
  197. package/orchestrator/parallel-orchestrator.cjs +0 -903
  198. package/orchestrator/planner/plan-builder.cjs +0 -125
  199. package/orchestrator/recovery.cjs +0 -188
  200. package/orchestrator/session-close-journal-worker.cjs +0 -5
  201. package/orchestrator/session-close-journal.cjs +0 -1014
  202. package/orchestrator/session-runner.cjs +0 -1153
  203. package/orchestrator/worktree-manager.cjs +0 -282
  204. package/settings.json +0 -7
  205. package/skills/cc-collab/SKILL.md +0 -42
  206. package/skills/code-review-expert/SKILL.md +0 -43
  207. package/skills/code-review-expert/references/correctness-reliability.md +0 -30
  208. package/skills/code-review-expert/references/design-boundaries.md +0 -26
  209. package/skills/code-review-expert/references/removal-plan.md +0 -24
  210. package/skills/codex-collab/SKILL.md +0 -49
  211. package/skills/codex-collab/evals/evals.json +0 -35
  212. package/skills/integration-rules/SKILL.md +0 -32
  213. package/skills/security-rules/SKILL.md +0 -38
  214. package/skills/testing-rules/SKILL.md +0 -32
  215. package/skills/ultra-doctor/SKILL.md +0 -41
  216. package/skills/ultra-plan/references/semantic-preflight.md +0 -20
  217. package/skills/ultra-verify/SKILL.md +0 -46
  218. package/skills/ultra-verify/evals/evals.json +0 -46
  219. package/skills/ultra-verify/references/cross-verify-modes.md +0 -28
  220. package/skills/ultra-verify/references/evidence-status.md +0 -21
  221. package/skills/ultra-verify/scripts/verify_wait.py +0 -94
  222. package/spec/cli-protocol.md +0 -102
  223. package/spec/mcp-tools.yaml +0 -312
  224. package/spec/schemas/baseline-reconciliation.v1.schema.json +0 -73
  225. package/spec/schemas/change-delta.v1.schema.json +0 -153
  226. package/spec/schemas/command-manifest.schema.json +0 -29
  227. package/spec/schemas/context-file.v4.5.schema.json +0 -54
  228. package/spec/schemas/delivery-report.v1.schema.json +0 -66
  229. package/spec/schemas/documentation-reconciliation.v1.schema.json +0 -106
  230. package/spec/schemas/mcp-tools.schema.json +0 -136
  231. package/spec/schemas/skill-manifest.schema.json +0 -24
  232. package/spec/schemas/state-db.sql +0 -641
  233. package/spec/schemas/task-ledger.v2.schema.json +0 -296
  234. package/spec/schemas/tasks.v4.5.schema.json +0 -96
  235. package/spec/schemas/test-report.v1.schema.json +0 -225
  236. package/ultra-tools/cli.cjs +0 -129
  237. package/ultra-tools/commands/db.cjs +0 -226
  238. package/ultra-tools/commands/legacy-memory.cjs +0 -159
  239. package/ultra-tools/commands/migrate.cjs +0 -1386
  240. package/ultra-tools/commands/session.cjs +0 -213
  241. package/ultra-tools/commands/status.cjs +0 -559
  242. package/ultra-tools/commands/system.cjs +0 -498
  243. package/ultra-tools/commands/task.cjs +0 -105
@@ -1,651 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('node:fs');
4
- const path = require('node:path');
5
- const { spawnSync } = require('node:child_process');
6
-
7
- const GIT_MODES = new Set(['auto', 'initialize', 'skip']);
8
- // No trailing slash: isolated session worktrees expose only `.ultra/.runtime`
9
- // as a symlink, and Git's directory-only pattern does not ignore that symlink
10
- // reliably. Semantic and evidence artifacts elsewhere in `.ultra/` remain
11
- // trackable and travel with their task branch.
12
- const ULTRA_RUNTIME_IGNORE_LINE = '.ultra/.runtime';
13
- const ULTRA_LEGACY_STATE_IGNORE_LINES = Object.freeze([
14
- '.ultra/[s]tate.db',
15
- '.ultra/[s]tate.db-wal',
16
- '.ultra/[s]tate.db-shm',
17
- ]);
18
- const ULTRA_LEGACY_STATE_PROBES = Object.freeze([
19
- '.ultra/' + 'state.db',
20
- '.ultra/' + 'state.db-wal',
21
- '.ultra/' + 'state.db-shm',
22
- ]);
23
- const ULTRA_UNIGNORE_DIR_LINE = '!.ultra/';
24
- const ULTRA_UNIGNORE_CONTENT_LINE = '!.ultra/**';
25
- const LEGACY_BROAD_IGNORE_LINES = new Set(['.ultra', '.ultra/', '/.ultra', '/.ultra/']);
26
- const ULTRA_GIT_ARTIFACT_CLASSES = Object.freeze({
27
- runtime: Object.freeze({
28
- compatibility: ULTRA_LEGACY_STATE_PROBES,
29
- authority: Object.freeze([
30
- '.ultra/.runtime/state.db',
31
- '.ultra/.runtime/state.db-wal',
32
- '.ultra/.runtime/state.db-shm',
33
- ]),
34
- backups: Object.freeze([
35
- '.ultra/.runtime/backups/state-db-2026-07-28.db',
36
- '.ultra/.runtime/backups/legacy-state-2026-07-28/state.db',
37
- '.ultra/.runtime/backups/projection-v4.5-2026-07-28/tasks/tasks.json',
38
- '.ultra/.runtime/backups/backup-v4.4-2026-07-28/specs/product.md',
39
- ]),
40
- coordination: Object.freeze([
41
- '.ultra/.runtime/collab/review.json',
42
- '.ultra/.runtime/sessions/session/metadata.json',
43
- '.ultra/.runtime/worktrees/session/marker',
44
- '.ultra/.runtime/telemetry/events.jsonl',
45
- '.ultra/.runtime/debug/trace.jsonl',
46
- '.ultra/.runtime/checkpoint.json',
47
- '.ultra/.runtime/orchestrator/orchestrator.pid',
48
- '.ultra/.runtime/orchestrator/orchestrator.log',
49
- '.ultra/.runtime/recovery/worktrees/session/sentinel',
50
- ]),
51
- }),
52
- semantic: Object.freeze({
53
- baseline: Object.freeze([
54
- '.ultra/specs/discovery.md',
55
- '.ultra/specs/product.md',
56
- '.ultra/specs/architecture.md',
57
- '.ultra/specs/research-distillate.md',
58
- ]),
59
- planning: Object.freeze([
60
- '.ultra/plan.md',
61
- '.ultra/plan.json',
62
- '.ultra/tasks/tasks.json',
63
- '.ultra/tasks/plan.md',
64
- '.ultra/tasks/plan.json',
65
- ]),
66
- research: Object.freeze([
67
- '.ultra/docs/research/README.md',
68
- '.ultra/docs/research/workflow/step.md',
69
- ]),
70
- changes: Object.freeze([
71
- '.ultra/changes/active/example/intent.md',
72
- '.ultra/changes/active/example/plan.md',
73
- '.ultra/changes/active/example/delta/spec.md',
74
- '.ultra/changes/active/example/findings/review.md',
75
- '.ultra/changes/active/example/evidence/build.json',
76
- '.ultra/changes/active/example/reconciliation.json',
77
- '.ultra/changes/active/example/verification.md',
78
- '.ultra/changes/active/example/contexts/manifest.json',
79
- '.ultra/changes/archive/example/intent.md',
80
- '.ultra/changes/archive/example/plan.md',
81
- '.ultra/changes/archive/example/evidence/release.json',
82
- ]),
83
- reports: Object.freeze([
84
- '.ultra/reports/templates/test-report.json',
85
- '.ultra/reports/templates/delivery-report.json',
86
- '.ultra/reports/tests/workflow.json',
87
- '.ultra/reports/delivery/workflow.json',
88
- '.ultra/reports/test-report.json',
89
- '.ultra/reports/delivery-report.json',
90
- ]),
91
- evidence: Object.freeze([
92
- '.ultra/evidence/build.json',
93
- '.ultra/findings/review.md',
94
- '.ultra/reconciliation/change.md',
95
- '.ultra/archive/change/evidence.json',
96
- ]),
97
- }),
98
- });
99
- const ULTRA_RUNTIME_PROBES = Object.freeze(
100
- Object.values(ULTRA_GIT_ARTIFACT_CLASSES.runtime).flat(),
101
- );
102
- const ULTRA_SEMANTIC_PROBES = Object.freeze(
103
- Object.values(ULTRA_GIT_ARTIFACT_CLASSES.semantic).flat(),
104
- );
105
-
106
- class GitBootstrapError extends Error {
107
- constructor(code, message, { cause } = {}) {
108
- super(message);
109
- this.name = 'GitBootstrapError';
110
- this.code = code;
111
- if (cause) this.cause = cause;
112
- }
113
- }
114
-
115
- function runGit(rootDir, args, spawnGit = spawnSync) {
116
- return spawnGit('git', args, {
117
- cwd: rootDir,
118
- encoding: 'utf8',
119
- stdio: ['ignore', 'pipe', 'pipe'],
120
- });
121
- }
122
-
123
- function gitValue(rootDir, args, spawnGit = spawnSync, {
124
- absentStatuses = [],
125
- } = {}) {
126
- const result = runGit(rootDir, args, spawnGit);
127
- if (result?.error) {
128
- throw new GitBootstrapError(
129
- 'GIT_DISCOVERY_FAILED',
130
- `Git probe failed: ${result.error.message}`,
131
- { cause: result.error },
132
- );
133
- }
134
- if (result.status === 0) return String(result.stdout || '').trim();
135
- if (absentStatuses.includes(result.status)) return null;
136
- const detail = String(result.stderr || result.stdout || `exit ${result.status}`).trim();
137
- throw new GitBootstrapError(
138
- 'GIT_DISCOVERY_FAILED',
139
- `Git probe ${args.join(' ')} failed: ${detail}`,
140
- );
141
- }
142
-
143
- function hasGitMetadata(rootDir) {
144
- let current = path.resolve(rootDir);
145
- while (true) {
146
- if (fs.lstatSync(path.join(current, '.git'), { throwIfNoEntry: false })) return true;
147
- const parent = path.dirname(current);
148
- if (parent === current) return false;
149
- current = parent;
150
- }
151
- }
152
-
153
- function inspectGitRepository(rootDir, { spawnGit = spawnSync } = {}) {
154
- const metadataPresent = hasGitMetadata(rootDir);
155
- const result = runGit(rootDir, ['rev-parse', '--show-toplevel'], spawnGit);
156
- const repositoryRoot = result?.status === 0 ? String(result.stdout || '').trim() : '';
157
- if (!repositoryRoot) {
158
- const detail = String(
159
- result?.error?.message || result?.stderr || result?.stdout || 'unknown Git probe failure',
160
- ).trim();
161
- const explicitlyNotRepository = !metadataPresent
162
- && result?.status !== null
163
- && result?.status !== 0
164
- && /not a git repository/i.test(detail);
165
- if (!explicitlyNotRepository) {
166
- throw new GitBootstrapError(
167
- 'GIT_DISCOVERY_FAILED',
168
- `cannot classify repository state: ${detail}`,
169
- { cause: result?.error },
170
- );
171
- }
172
- return {
173
- present: false,
174
- repository_root: null,
175
- branch: null,
176
- head: null,
177
- };
178
- }
179
- return {
180
- present: true,
181
- repository_root: path.resolve(repositoryRoot),
182
- branch: gitValue(
183
- rootDir,
184
- ['symbolic-ref', '--quiet', '--short', 'HEAD'],
185
- spawnGit,
186
- { absentStatuses: [1] },
187
- )
188
- || gitValue(rootDir, ['branch', '--show-current'], spawnGit),
189
- head: gitValue(
190
- rootDir,
191
- ['rev-parse', '--verify', '--quiet', 'HEAD'],
192
- spawnGit,
193
- { absentStatuses: [1] },
194
- ),
195
- };
196
- }
197
-
198
- function initializeRepository(rootDir) {
199
- let result = runGit(rootDir, ['init', '-q', '-b', 'main']);
200
- if (result.status !== 0) {
201
- result = runGit(rootDir, ['init', '-q']);
202
- if (result.status === 0) {
203
- const branch = runGit(rootDir, ['symbolic-ref', 'HEAD', 'refs/heads/main']);
204
- if (branch.status !== 0) result = branch;
205
- }
206
- }
207
- if (result.status !== 0) {
208
- throw new GitBootstrapError(
209
- 'GIT_INIT_FAILED',
210
- `git init failed: ${(result.stderr || result.stdout || 'unknown error').trim()}`,
211
- );
212
- }
213
- }
214
-
215
- function checkIgnoreStates(rootDir, probes, spawnGit = spawnSync) {
216
- const unique = [...new Set(probes)];
217
- if (unique.length === 0) return new Map();
218
- const result = spawnGit(
219
- 'git',
220
- ['check-ignore', '--no-index', '--stdin', '-z'],
221
- {
222
- cwd: rootDir,
223
- encoding: 'utf8',
224
- input: `${unique.join('\0')}\0`,
225
- stdio: ['pipe', 'pipe', 'pipe'],
226
- },
227
- );
228
- if (result?.error || ![0, 1].includes(result?.status)) {
229
- const detail = String(
230
- result?.error?.message || result?.stderr || result?.stdout
231
- || `exit ${String(result?.status)}`,
232
- ).trim();
233
- throw new GitBootstrapError(
234
- 'GITIGNORE_PROBE_FAILED',
235
- `cannot classify Ultra artifacts: ${detail}`,
236
- { cause: result?.error },
237
- );
238
- }
239
- const ignored = new Set(
240
- String(result.stdout || '').split('\0').filter(Boolean),
241
- );
242
- for (const output of ignored) {
243
- if (!unique.includes(output)) {
244
- throw new GitBootstrapError(
245
- 'GITIGNORE_PROBE_FAILED',
246
- `Git returned an unexpected Ultra artifact path: ${output}`,
247
- );
248
- }
249
- }
250
- return new Map(unique.map((probe) => [
251
- probe,
252
- ignored.has(probe) ? 'ignored' : 'trackable',
253
- ]));
254
- }
255
-
256
- function checkIgnoreState(rootDir, probe) {
257
- return checkIgnoreStates(rootDir, [probe]).get(probe);
258
- }
259
-
260
- function scanManagedArtifacts(rootDir) {
261
- const ultraRoot = path.join(rootDir, '.ultra');
262
- const stat = fs.lstatSync(ultraRoot, { throwIfNoEntry: false });
263
- if (!stat || stat.isSymbolicLink() || !stat.isDirectory()) {
264
- return { runtime: [], semantic: [] };
265
- }
266
- const found = { runtime: [], semantic: [] };
267
- const pending = fs.readdirSync(ultraRoot)
268
- .map((entry) => path.join(ultraRoot, entry));
269
- while (pending.length > 0) {
270
- const candidate = pending.pop();
271
- const entry = fs.lstatSync(candidate, { throwIfNoEntry: false });
272
- if (!entry) continue;
273
- const relative = path.relative(rootDir, candidate).split(path.sep).join('/');
274
- const runtime = relative === '.ultra/.runtime'
275
- || relative.startsWith('.ultra/.runtime/')
276
- || ULTRA_LEGACY_STATE_PROBES.includes(relative);
277
- found[runtime ? 'runtime' : 'semantic'].push(relative);
278
- if (entry.isSymbolicLink() || !entry.isDirectory()) continue;
279
- const parts = relative.split('/');
280
- if (parts.length === 4
281
- && parts[0] === '.ultra'
282
- && parts[1] === '.runtime'
283
- && parts[2] === 'worktrees') {
284
- continue;
285
- }
286
- for (const child of fs.readdirSync(candidate)) {
287
- pending.push(path.join(candidate, child));
288
- }
289
- }
290
- return found;
291
- }
292
-
293
- function runtimeIsIgnored(rootDir) {
294
- const runtimeEntry = fs.lstatSync(
295
- path.join(rootDir, '.ultra', '.runtime'),
296
- { throwIfNoEntry: false },
297
- );
298
- if (runtimeEntry?.isSymbolicLink()) {
299
- // Git refuses to classify pathspecs beyond a symlink. For a task
300
- // worktree the link itself is the complete runtime boundary, so proving
301
- // the exact link ignored is both necessary and sufficient.
302
- return checkIgnoreState(rootDir, '.ultra/.runtime') === 'ignored';
303
- }
304
- const actual = scanManagedArtifacts(rootDir).runtime;
305
- const probes = [...new Set([...ULTRA_RUNTIME_PROBES, ...actual])];
306
- const states = checkIgnoreStates(rootDir, probes);
307
- return probes.every((probe) => states.get(probe) === 'ignored');
308
- }
309
-
310
- function semanticArtifactsAreTrackable(rootDir) {
311
- const actual = scanManagedArtifacts(rootDir).semantic;
312
- const probes = [...new Set([...ULTRA_SEMANTIC_PROBES, ...actual])];
313
- const states = checkIgnoreStates(rootDir, probes);
314
- return probes.every((probe) => states.get(probe) === 'trackable');
315
- }
316
-
317
- function rootRulesPreserveUltraBoundary(rootDir) {
318
- const file = path.join(rootDir, '.gitignore');
319
- const stat = fs.lstatSync(file, { throwIfNoEntry: false });
320
- if (!stat) return true;
321
- if (stat.isSymbolicLink() || !stat.isFile()) return false;
322
- const rules = fs.readFileSync(file, 'utf8')
323
- .split(/\r?\n/)
324
- .map((line) => line.trim())
325
- .filter((line) => line && !line.startsWith('#'));
326
- let lastUnsafe = -1;
327
- let unignoreDir = -1;
328
- let unignoreContent = -1;
329
- let runtimeIgnore = -1;
330
- const compatibilityIgnores = new Map(
331
- ULTRA_LEGACY_STATE_IGNORE_LINES.map((rule) => [rule, -1]),
332
- );
333
- rules.forEach((rule, index) => {
334
- const normalized = rule.replace(/^\/+/, '').replace(/\/+$/u, '');
335
- if (rule === ULTRA_UNIGNORE_DIR_LINE) unignoreDir = index;
336
- else if (rule === ULTRA_UNIGNORE_CONTENT_LINE) unignoreContent = index;
337
- else if (normalized === ULTRA_RUNTIME_IGNORE_LINE) runtimeIgnore = index;
338
- else if (compatibilityIgnores.has(normalized)) compatibilityIgnores.set(normalized, index);
339
- else if (normalized.includes('.ultra')) lastUnsafe = index;
340
- });
341
- if (lastUnsafe === -1) return true;
342
- return unignoreDir > lastUnsafe
343
- && unignoreContent > unignoreDir
344
- && runtimeIgnore > unignoreContent
345
- && [...compatibilityIgnores.values()].every((index) => index > unignoreContent);
346
- }
347
-
348
- function effectiveUltraStorageBoundary(rootDir) {
349
- return rootRulesPreserveUltraBoundary(rootDir)
350
- && runtimeIsIgnored(rootDir)
351
- && semanticArtifactsAreTrackable(rootDir);
352
- }
353
-
354
- function repositoryLocalRuntimePattern(rootDir, repository) {
355
- const repositoryRoot = fs.realpathSync(repository.repository_root);
356
- const projectRoot = fs.realpathSync(rootDir);
357
- const relative = path.relative(repositoryRoot, projectRoot);
358
- if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
359
- throw new GitBootstrapError(
360
- 'GIT_REPOSITORY_SCOPE_INVALID',
361
- `project root is outside its reported Git repository: ${rootDir}`,
362
- );
363
- }
364
- const components = relative ? relative.split(path.sep) : [];
365
- return `/${[...components, '.ultra', '.runtime'].join('/')}`;
366
- }
367
-
368
- function repositoryLocalCompatibilityPatterns(rootDir, repository) {
369
- const runtimePattern = repositoryLocalRuntimePattern(rootDir, repository);
370
- const prefix = runtimePattern.slice(0, -'.runtime'.length);
371
- return ULTRA_LEGACY_STATE_IGNORE_LINES.map((line) => (
372
- `${prefix}${line.slice('.ultra/'.length)}`
373
- ));
374
- }
375
-
376
- function ensureLocalRuntimeIgnored(rootDir, repository) {
377
- const result = runGit(rootDir, ['rev-parse', '--git-path', 'info/exclude']);
378
- if (result.status !== 0) {
379
- throw new GitBootstrapError(
380
- 'GITEXCLUDE_UNAVAILABLE',
381
- `cannot resolve repository-local exclude file: ${(result.stderr || result.stdout).trim()}`,
382
- );
383
- }
384
- const reported = result.stdout.trim();
385
- const file = path.isAbsolute(reported) ? reported : path.resolve(rootDir, reported);
386
- let stat = null;
387
- try { stat = fs.lstatSync(file); }
388
- catch (error) {
389
- if (error.code !== 'ENOENT') throw error;
390
- }
391
- if (stat && (stat.isSymbolicLink() || !stat.isFile())) {
392
- throw new GitBootstrapError(
393
- 'GITEXCLUDE_UNSAFE',
394
- `refusing to update non-regular repository-local exclude file: ${file}`,
395
- );
396
- }
397
- const existed = stat !== null;
398
- const before = existed ? fs.readFileSync(file, 'utf8') : null;
399
- if (runtimeIsIgnored(rootDir)) {
400
- return { changed: false, path: file, rollback: { existed, before } };
401
- }
402
- const managedPattern = repositoryLocalRuntimePattern(rootDir, repository);
403
- const compatibilityPatterns = repositoryLocalCompatibilityPatterns(rootDir, repository);
404
- const preserved = (before || '')
405
- .split(/\r?\n/)
406
- .filter((line) => line.trim() !== '# Ultra Builder Pro local runtime')
407
- .filter((line) => line.trim() !== ULTRA_RUNTIME_IGNORE_LINE)
408
- .filter((line) => line.trim() !== managedPattern)
409
- .filter((line) => !compatibilityPatterns.includes(line.trim()))
410
- .join('\n')
411
- .replace(/\n+$/u, '');
412
- const prefix = preserved ? `${preserved}\n` : '';
413
- const separator = prefix && !prefix.endsWith('\n\n') ? '\n' : '';
414
- fs.mkdirSync(path.dirname(file), { recursive: true });
415
- fs.writeFileSync(
416
- file,
417
- `${prefix}${separator}# Ultra Builder Pro local runtime\n`
418
- + `${managedPattern}\n${compatibilityPatterns.join('\n')}\n`,
419
- );
420
- return { changed: true, path: file, rollback: { existed, before } };
421
- }
422
-
423
- function rollbackFileMutation(mutation) {
424
- if (!mutation?.changed) return [];
425
- try {
426
- if (mutation.rollback.existed) {
427
- fs.writeFileSync(mutation.path, mutation.rollback.before);
428
- } else {
429
- fs.rmSync(mutation.path, { force: true });
430
- }
431
- return [];
432
- } catch (error) {
433
- return [`${mutation.path}: ${error.message}`];
434
- }
435
- }
436
-
437
- function ensureUltraRuntimeIgnored(rootDir) {
438
- const file = path.join(rootDir, '.gitignore');
439
- let stat = null;
440
- try { stat = fs.lstatSync(file); }
441
- catch (error) {
442
- if (error.code !== 'ENOENT') throw error;
443
- }
444
- const existed = stat !== null;
445
- if (stat) {
446
- if (stat.isSymbolicLink() || !stat.isFile()) {
447
- throw new GitBootstrapError(
448
- 'GITIGNORE_UNSAFE',
449
- `refusing to update non-regular root .gitignore: ${file}`,
450
- );
451
- }
452
- }
453
- const before = existed ? fs.readFileSync(file, 'utf8') : null;
454
- if (effectiveUltraStorageBoundary(rootDir)) {
455
- return {
456
- changed: false,
457
- path: file,
458
- rollback: { existed, before },
459
- };
460
- }
461
- const preserved = (before || '')
462
- .split(/\r?\n/)
463
- .filter((line) => !LEGACY_BROAD_IGNORE_LINES.has(line.trim()))
464
- .filter((line) => ![
465
- ULTRA_RUNTIME_IGNORE_LINE,
466
- `/${ULTRA_RUNTIME_IGNORE_LINE}`,
467
- `${ULTRA_RUNTIME_IGNORE_LINE}/`,
468
- `/${ULTRA_RUNTIME_IGNORE_LINE}/`,
469
- ULTRA_UNIGNORE_DIR_LINE,
470
- ULTRA_UNIGNORE_CONTENT_LINE,
471
- ...ULTRA_LEGACY_STATE_IGNORE_LINES,
472
- ].includes(line.trim()))
473
- .join('\n')
474
- .replace(/\n+$/u, '');
475
- const prefix = preserved ? `${preserved}\n` : '';
476
- const separator = prefix && !prefix.endsWith('\n\n') ? '\n' : '';
477
- const next = `${prefix}${separator}`
478
- + '# Ultra Builder Pro semantic artifacts are trackable; runtime state is local\n'
479
- + `${ULTRA_UNIGNORE_DIR_LINE}\n`
480
- + `${ULTRA_UNIGNORE_CONTENT_LINE}\n`
481
- + `${ULTRA_RUNTIME_IGNORE_LINE}\n`
482
- + `${ULTRA_LEGACY_STATE_IGNORE_LINES.join('\n')}\n`;
483
- fs.writeFileSync(file, next);
484
- return {
485
- changed: true,
486
- path: file,
487
- rollback: { existed, before },
488
- };
489
- }
490
-
491
- function assertUltraStorageBoundary(rootDir) {
492
- if (effectiveUltraStorageBoundary(rootDir)) return;
493
- throw new GitBootstrapError(
494
- 'GITIGNORE_INEFFECTIVE',
495
- 'Git rules must ignore .ultra/.runtime while keeping semantic .ultra artifacts trackable',
496
- );
497
- }
498
-
499
- function ensureExistingProjectStorageBoundary(rootDir) {
500
- const repository = inspectGitRepository(rootDir);
501
- if (!repository.present) {
502
- return {
503
- status: 'not_a_repository',
504
- repository,
505
- gitignore_changed: false,
506
- gitignore_path: null,
507
- };
508
- }
509
- let gitignore;
510
- let localExclude;
511
- try {
512
- if (effectiveUltraStorageBoundary(rootDir)) {
513
- return {
514
- status: 'ready',
515
- repository,
516
- gitignore_changed: false,
517
- gitignore_path: null,
518
- local_exclude_changed: false,
519
- local_exclude_path: null,
520
- };
521
- }
522
- if (semanticArtifactsAreTrackable(rootDir)) {
523
- localExclude = ensureLocalRuntimeIgnored(rootDir, repository);
524
- if (!effectiveUltraStorageBoundary(rootDir)) {
525
- rollbackFileMutation(localExclude);
526
- localExclude = null;
527
- gitignore = ensureUltraRuntimeIgnored(rootDir);
528
- }
529
- } else {
530
- gitignore = ensureUltraRuntimeIgnored(rootDir);
531
- }
532
- assertUltraStorageBoundary(rootDir);
533
- return {
534
- status: 'ready',
535
- repository,
536
- gitignore_changed: Boolean(gitignore?.changed),
537
- gitignore_path: gitignore?.path || null,
538
- local_exclude_changed: Boolean(localExclude?.changed),
539
- local_exclude_path: localExclude?.path || null,
540
- };
541
- } catch (error) {
542
- rollbackFileMutation(localExclude);
543
- rollbackGitBootstrap(rootDir, { created_repository: false, gitignore });
544
- throw error;
545
- }
546
- }
547
-
548
- function rollbackGitBootstrap(rootDir, internal) {
549
- const errors = [];
550
- if (!internal) return errors;
551
- const gitignore = internal.gitignore;
552
- if (gitignore?.changed) {
553
- try {
554
- if (gitignore.rollback.existed) {
555
- fs.writeFileSync(gitignore.path, gitignore.rollback.before);
556
- } else {
557
- fs.rmSync(gitignore.path, { force: true });
558
- }
559
- } catch (error) {
560
- errors.push(`gitignore restore failed: ${error.message}`);
561
- }
562
- }
563
- if (internal.created_repository) {
564
- try {
565
- fs.rmSync(path.join(rootDir, '.git'), { recursive: true, force: true });
566
- } catch (error) {
567
- errors.push(`git repository rollback failed: ${error.message}`);
568
- }
569
- }
570
- return errors;
571
- }
572
-
573
- function bootstrapGit(rootDir, { mode = 'auto' } = {}) {
574
- if (!GIT_MODES.has(mode)) {
575
- throw new GitBootstrapError(
576
- 'VALIDATION_ERROR',
577
- `git_mode must be one of: ${[...GIT_MODES].join(', ')}`,
578
- );
579
- }
580
- const before = inspectGitRepository(rootDir);
581
- if (!before.present && mode === 'skip') {
582
- return {
583
- result: {
584
- requested_mode: mode,
585
- status: 'skipped',
586
- repository_root: null,
587
- branch: null,
588
- head: null,
589
- gitignore_path: null,
590
- gitignore_changed: false,
591
- initial_commit_required: false,
592
- },
593
- internal: null,
594
- };
595
- }
596
-
597
- const internal = {
598
- created_repository: false,
599
- gitignore: null,
600
- };
601
- try {
602
- if (!before.present) {
603
- initializeRepository(rootDir);
604
- internal.created_repository = true;
605
- }
606
- internal.gitignore = ensureUltraRuntimeIgnored(rootDir);
607
- assertUltraStorageBoundary(rootDir);
608
- const after = inspectGitRepository(rootDir);
609
- if (!after.present) {
610
- throw new GitBootstrapError(
611
- 'GIT_INIT_FAILED',
612
- 'git initialization completed without a discoverable repository',
613
- );
614
- }
615
- return {
616
- result: {
617
- requested_mode: mode,
618
- status: internal.created_repository ? 'initialized' : 'existing',
619
- repository_root: after.repository_root,
620
- branch: after.branch,
621
- head: after.head,
622
- gitignore_path: path.relative(rootDir, internal.gitignore.path) || '.gitignore',
623
- gitignore_changed: internal.gitignore.changed,
624
- initial_commit_required: after.head === null,
625
- },
626
- internal,
627
- };
628
- } catch (error) {
629
- rollbackGitBootstrap(rootDir, internal);
630
- if (error instanceof GitBootstrapError) throw error;
631
- throw new GitBootstrapError(
632
- 'GIT_INIT_FAILED',
633
- `Git bootstrap failed: ${error.message}`,
634
- { cause: error },
635
- );
636
- }
637
- }
638
-
639
- module.exports = {
640
- GitBootstrapError,
641
- GIT_MODES,
642
- ULTRA_GIT_ARTIFACT_CLASSES,
643
- ULTRA_RUNTIME_PROBES,
644
- ULTRA_SEMANTIC_PROBES,
645
- bootstrapGit,
646
- assertUltraStorageBoundary,
647
- effectiveUltraStorageBoundary,
648
- ensureExistingProjectStorageBoundary,
649
- inspectGitRepository,
650
- rollbackGitBootstrap,
651
- };