ultra-builder-pro-cli 0.25.0 → 0.26.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 (241) hide show
  1. package/.ultra-template/contexts/TEMPLATE.md +87 -0
  2. package/.ultra-template/decisions/.gitkeep +1 -0
  3. package/.ultra-template/evidence/.gitkeep +1 -0
  4. package/.ultra-template/north-star.md +46 -0
  5. package/.ultra-template/research/.gitkeep +1 -0
  6. package/.ultra-template/specs/discovery.md +2 -2
  7. package/.ultra-template/tasks.json +3 -0
  8. package/.ultra-template/test-report.json +27 -0
  9. package/CHANGELOG.md +49 -0
  10. package/README.md +217 -579
  11. package/adapters/_shared/delegate-profiles/kimi-read-only.md +15 -0
  12. package/adapters/_shared/delegate-profiles/kimi-write.md +17 -0
  13. package/adapters/_shared/file-ops.cjs +76 -2
  14. package/adapters/_shared/host-profile.cjs +97 -0
  15. package/adapters/_shared/plugin-core.cjs +204 -0
  16. package/adapters/_shared/runtime-assets.cjs +31 -96
  17. package/adapters/claude.js +92 -291
  18. package/adapters/codex.js +309 -842
  19. package/adapters/grok.js +143 -1469
  20. package/adapters/kimi.js +124 -1053
  21. package/adapters/opencode.js +230 -742
  22. package/bin/delegate-worker.cjs +323 -0
  23. package/bin/delegate.cjs +456 -0
  24. package/bin/install.js +38 -11
  25. package/docs/ARCHITECTURE.md +122 -216
  26. package/docs/ARTIFACT-AUTHORITY.md +90 -268
  27. package/docs/DECISIONS.md +75 -87
  28. package/docs/PHILOSOPHY.md +177 -0
  29. package/docs/PLUGIN-ISOLATION-CONTRACT.md +62 -39
  30. package/docs/RUNTIME-COMPAT-MATRIX.md +99 -156
  31. package/docs/SKILL-AUTHORING.md +94 -73
  32. package/docs/WORKFLOW-LIFECYCLE.md +93 -189
  33. package/hooks/README.md +17 -37
  34. package/hooks/_common.py +113 -0
  35. package/hooks/adapters/codex.py +9 -16
  36. package/hooks/adapters/grok.py +5 -5
  37. package/hooks/adapters/kimi.py +8 -14
  38. package/hooks/block_dangerous_commands.py +95 -0
  39. package/hooks/compact_context.py +65 -0
  40. package/hooks/mid_workflow_recall.py +25 -0
  41. package/hooks/post_edit_guard.py +105 -0
  42. package/hooks/session_context.py +30 -0
  43. package/package.json +17 -52
  44. package/skills/ultra-change/SKILL.md +97 -50
  45. package/skills/ultra-change/references/change-contract.md +40 -1
  46. package/skills/ultra-delegate/SKILL.md +82 -0
  47. package/skills/ultra-delegate/references/delegation-contract.md +51 -0
  48. package/skills/ultra-delegate/scripts/delegate_wait.py +86 -0
  49. package/skills/ultra-deliver/SKILL.md +61 -42
  50. package/skills/ultra-deliver/references/baseline-reconciliation.md +39 -20
  51. package/skills/ultra-dev/SKILL.md +96 -62
  52. package/skills/ultra-dev/references/debugging.md +22 -0
  53. package/skills/ultra-domain-modeling/SKILL.md +79 -0
  54. package/skills/ultra-grilling/SKILL.md +79 -0
  55. package/skills/ultra-init/SKILL.md +94 -37
  56. package/skills/ultra-init/references/brownfield-adoption.md +33 -83
  57. package/skills/ultra-init/scripts/init_project.cjs +84 -0
  58. package/skills/ultra-plan/SKILL.md +88 -68
  59. package/skills/ultra-research/SKILL.md +62 -48
  60. package/skills/ultra-research/references/00-problem-validation.md +5 -5
  61. package/skills/ultra-research/references/01-opportunity-discovery.md +3 -3
  62. package/skills/ultra-research/references/02-market-assessment.md +5 -4
  63. package/skills/ultra-research/references/03-competitive-landscape.md +3 -3
  64. package/skills/ultra-research/references/04-product-strategy.md +3 -3
  65. package/skills/ultra-research/references/05-assumptions-validation.md +5 -5
  66. package/skills/ultra-research/references/10-user-personas.md +3 -2
  67. package/skills/ultra-research/references/11-user-scenarios.md +3 -3
  68. package/skills/ultra-research/references/20-user-stories.md +4 -3
  69. package/skills/ultra-research/references/21-features-scope.md +3 -2
  70. package/skills/ultra-research/references/22-success-metrics.md +3 -3
  71. package/skills/ultra-research/references/30-architecture-context.md +3 -3
  72. package/skills/ultra-research/references/31-solution-strategy.md +3 -3
  73. package/skills/ultra-research/references/32-building-blocks.md +3 -3
  74. package/skills/ultra-research/references/40-deployment.md +3 -3
  75. package/skills/ultra-research/references/41-quality-risks.md +3 -3
  76. package/skills/ultra-research/references/99-synthesis.md +7 -7
  77. package/skills/ultra-review/SKILL.md +59 -32
  78. package/skills/ultra-review/references/code.md +17 -0
  79. package/skills/ultra-review/references/comments.md +12 -0
  80. package/skills/ultra-review/references/design.md +12 -0
  81. package/skills/ultra-review/references/errors.md +13 -0
  82. package/skills/ultra-review/references/spec.md +13 -0
  83. package/skills/ultra-review/references/tests.md +12 -0
  84. package/skills/ultra-review/references/worker-packet.md +40 -0
  85. package/skills/ultra-review/scripts/review_wait.py +26 -15
  86. package/skills/ultra-status/SKILL.md +61 -36
  87. package/skills/ultra-tdd/SKILL.md +81 -0
  88. package/skills/ultra-tdd/references/templates/README.md +20 -0
  89. package/skills/ultra-tdd/references/templates/feature-flag-default-audit.sh +79 -0
  90. package/skills/ultra-tdd/references/templates/persistence-real.ts +78 -0
  91. package/skills/ultra-tdd/references/templates/testcontainer-postgres.py +78 -0
  92. package/skills/ultra-tdd/references/templates/testcontainer-postgres.ts +73 -0
  93. package/skills/ultra-tdd/references/templates/vertical-slice.ts +66 -0
  94. package/skills/ultra-tdd/references/test-execution.md +18 -0
  95. package/skills/ultra-test/SKILL.md +70 -30
  96. package/skills/ultra-test/references/export-syntax.md +18 -0
  97. package/skills/ultra-test/scripts/worktree_digest.cjs +54 -0
  98. package/skills/ultra-think/SKILL.md +49 -27
  99. package/skills/ultra-think/references/autonomy-boundary.md +48 -0
  100. package/skills/ultra-think/references/interaction-boundary.md +18 -33
  101. package/.ultra-template/docs/research/README.md +0 -55
  102. package/.ultra-template/reports/templates/delivery-report.json +0 -17
  103. package/.ultra-template/reports/templates/test-report.json +0 -18
  104. package/.ultra-template/tasks/tasks.json +0 -15
  105. package/.ultra-template/templates/task-context.md +0 -19
  106. package/adapters/_shared/codex-assets.cjs +0 -725
  107. package/adapters/_shared/codex-ultra-tools-entry.cjs +0 -11
  108. package/adapters/_shared/interaction-contract.cjs +0 -129
  109. package/adapters/_shared/path-rewrite.cjs +0 -79
  110. package/adapters/_shared/runtime-native.cjs +0 -132
  111. package/adapters/_shared/settings-merge.cjs +0 -76
  112. package/agents/code-reviewer.md +0 -42
  113. package/agents/debugger.md +0 -40
  114. package/agents/review-code.md +0 -49
  115. package/agents/review-comments.md +0 -38
  116. package/agents/review-coordinator.md +0 -56
  117. package/agents/review-design.md +0 -40
  118. package/agents/review-errors.md +0 -43
  119. package/agents/review-spec.md +0 -39
  120. package/agents/review-tests.md +0 -40
  121. package/agents/tdd-runner.md +0 -38
  122. package/bin/orchestrator.js +0 -383
  123. package/commands/ultra-change.md +0 -10
  124. package/commands/ultra-deliver.md +0 -10
  125. package/commands/ultra-dev.md +0 -10
  126. package/commands/ultra-doctor.md +0 -10
  127. package/commands/ultra-init.md +0 -10
  128. package/commands/ultra-plan.md +0 -10
  129. package/commands/ultra-research.md +0 -10
  130. package/commands/ultra-review.md +0 -10
  131. package/commands/ultra-status.md +0 -10
  132. package/commands/ultra-test.md +0 -10
  133. package/commands/ultra-think.md +0 -10
  134. package/docs/AGENT-CONTEXT.md +0 -191
  135. package/docs/COMMIT-HASH-BACKFILL.md +0 -51
  136. package/docs/LEGACY-CLI-CROSSWALK.md +0 -175
  137. package/docs/ROADMAP.md +0 -101
  138. package/docs/STATE-DB-ACCESS-POLICY.md +0 -161
  139. package/hooks/active_task_context.py +0 -152
  140. package/hooks/context_envelope.py +0 -100
  141. package/hooks/health_check.py +0 -196
  142. package/hooks/pre_stop_check.py +0 -66
  143. package/hooks/runtime_paths.py +0 -396
  144. package/hooks/subagent_tracker.py +0 -88
  145. package/hooks/workflow_checkpoint.py +0 -76
  146. package/hooks/workflow_context.py +0 -48
  147. package/hooks/workflow_resume.py +0 -84
  148. package/mcp-server/hook-context.cjs +0 -38
  149. package/mcp-server/hook-event.cjs +0 -34
  150. package/mcp-server/lib/archive-journal.cjs +0 -1119
  151. package/mcp-server/lib/archive-mutation-worker.py +0 -365
  152. package/mcp-server/lib/artifact-registry.cjs +0 -1232
  153. package/mcp-server/lib/baseline-workflow.cjs +0 -1193
  154. package/mcp-server/lib/canonical-json.cjs +0 -19
  155. package/mcp-server/lib/change-authority.cjs +0 -26
  156. package/mcp-server/lib/change-packet.cjs +0 -790
  157. package/mcp-server/lib/change-workflow.cjs +0 -65
  158. package/mcp-server/lib/context-envelope.cjs +0 -581
  159. package/mcp-server/lib/context-paths.cjs +0 -142
  160. package/mcp-server/lib/decision-records.cjs +0 -423
  161. package/mcp-server/lib/delivery-transaction.cjs +0 -471
  162. package/mcp-server/lib/doctor-backup-worker.cjs +0 -328
  163. package/mcp-server/lib/doctor.cjs +0 -627
  164. package/mcp-server/lib/git-bootstrap.cjs +0 -651
  165. package/mcp-server/lib/init-project.cjs +0 -1021
  166. package/mcp-server/lib/kernel-change-workflow.cjs +0 -2029
  167. package/mcp-server/lib/legacy-change-workflow.cjs +0 -3032
  168. package/mcp-server/lib/managed-file-write.cjs +0 -172
  169. package/mcp-server/lib/plan-checkpoint.cjs +0 -177
  170. package/mcp-server/lib/plan-publication-worker.cjs +0 -830
  171. package/mcp-server/lib/plan-store.cjs +0 -794
  172. package/mcp-server/lib/prd-parser.cjs +0 -83
  173. package/mcp-server/lib/pricing.cjs +0 -44
  174. package/mcp-server/lib/project-context.cjs +0 -178
  175. package/mcp-server/lib/projector.cjs +0 -386
  176. package/mcp-server/lib/provider-refs.cjs +0 -42
  177. package/mcp-server/lib/runtime-paths.cjs +0 -1687
  178. package/mcp-server/lib/runtime-state.cjs +0 -222
  179. package/mcp-server/lib/safe-project-file.cjs +0 -385
  180. package/mcp-server/lib/stage-checkpoints.cjs +0 -463
  181. package/mcp-server/lib/state-authority.cjs +0 -95
  182. package/mcp-server/lib/state-db.cjs +0 -1580
  183. package/mcp-server/lib/state-ops.cjs +0 -1428
  184. package/mcp-server/lib/task-contract.cjs +0 -404
  185. package/mcp-server/lib/task-expander.cjs +0 -105
  186. package/mcp-server/lib/task-ledger.cjs +0 -1964
  187. package/mcp-server/lib/telemetry.cjs +0 -85
  188. package/mcp-server/lib/topo.cjs +0 -131
  189. package/mcp-server/lib/ultra-facade.cjs +0 -2684
  190. package/mcp-server/lib/worker-packet.cjs +0 -358
  191. package/mcp-server/server.cjs +0 -437
  192. package/orchestrator/auto-merge.cjs +0 -128
  193. package/orchestrator/daemon.cjs +0 -264
  194. package/orchestrator/dispatch-rules.cjs +0 -78
  195. package/orchestrator/parallel-orchestrator.cjs +0 -903
  196. package/orchestrator/planner/plan-builder.cjs +0 -125
  197. package/orchestrator/recovery.cjs +0 -188
  198. package/orchestrator/session-close-journal-worker.cjs +0 -5
  199. package/orchestrator/session-close-journal.cjs +0 -1014
  200. package/orchestrator/session-runner.cjs +0 -1153
  201. package/orchestrator/worktree-manager.cjs +0 -282
  202. package/settings.json +0 -7
  203. package/skills/cc-collab/SKILL.md +0 -42
  204. package/skills/code-review-expert/SKILL.md +0 -43
  205. package/skills/code-review-expert/references/correctness-reliability.md +0 -30
  206. package/skills/code-review-expert/references/design-boundaries.md +0 -26
  207. package/skills/code-review-expert/references/removal-plan.md +0 -24
  208. package/skills/codex-collab/SKILL.md +0 -49
  209. package/skills/codex-collab/evals/evals.json +0 -35
  210. package/skills/integration-rules/SKILL.md +0 -32
  211. package/skills/security-rules/SKILL.md +0 -38
  212. package/skills/testing-rules/SKILL.md +0 -32
  213. package/skills/ultra-doctor/SKILL.md +0 -41
  214. package/skills/ultra-plan/references/semantic-preflight.md +0 -20
  215. package/skills/ultra-verify/SKILL.md +0 -46
  216. package/skills/ultra-verify/evals/evals.json +0 -46
  217. package/skills/ultra-verify/references/cross-verify-modes.md +0 -28
  218. package/skills/ultra-verify/references/evidence-status.md +0 -21
  219. package/skills/ultra-verify/scripts/verify_wait.py +0 -94
  220. package/spec/cli-protocol.md +0 -102
  221. package/spec/mcp-tools.yaml +0 -312
  222. package/spec/schemas/baseline-reconciliation.v1.schema.json +0 -73
  223. package/spec/schemas/change-delta.v1.schema.json +0 -153
  224. package/spec/schemas/command-manifest.schema.json +0 -29
  225. package/spec/schemas/context-file.v4.5.schema.json +0 -54
  226. package/spec/schemas/delivery-report.v1.schema.json +0 -66
  227. package/spec/schemas/documentation-reconciliation.v1.schema.json +0 -106
  228. package/spec/schemas/mcp-tools.schema.json +0 -136
  229. package/spec/schemas/skill-manifest.schema.json +0 -24
  230. package/spec/schemas/state-db.sql +0 -641
  231. package/spec/schemas/task-ledger.v2.schema.json +0 -296
  232. package/spec/schemas/tasks.v4.5.schema.json +0 -96
  233. package/spec/schemas/test-report.v1.schema.json +0 -225
  234. package/ultra-tools/cli.cjs +0 -129
  235. package/ultra-tools/commands/db.cjs +0 -226
  236. package/ultra-tools/commands/legacy-memory.cjs +0 -159
  237. package/ultra-tools/commands/migrate.cjs +0 -1386
  238. package/ultra-tools/commands/session.cjs +0 -213
  239. package/ultra-tools/commands/status.cjs +0 -559
  240. package/ultra-tools/commands/system.cjs +0 -498
  241. package/ultra-tools/commands/task.cjs +0 -105
@@ -1,65 +0,0 @@
1
- 'use strict';
2
-
3
- const baselines = require('./baseline-workflow.cjs');
4
- const kernel = require('./kernel-change-workflow.cjs');
5
-
6
- function createChange(db, input, options = {}) {
7
- return kernel.createChange(db, input, options);
8
- }
9
-
10
- function updateChange(db, id, patch = {}, options = {}) {
11
- return kernel.updateChange(db, id, patch, options);
12
- }
13
-
14
- function supersedeChange(db, input = {}, options = {}) {
15
- return kernel.supersedeChange(db, input, options);
16
- }
17
-
18
- function assertTaskCreationAllowed(db, input = {}, {
19
- rootDir = process.cwd(),
20
- } = {}) {
21
- const change = input.change_id ? kernel.readChange(db, input.change_id) : null;
22
- if (input.change_id && !change) {
23
- throw new kernel.ChangeWorkflowError(
24
- 'CHANGE_NOT_FOUND',
25
- `change ${input.change_id} not found`,
26
- );
27
- }
28
- if (change && !['active', 'blocked'].includes(change.status)) {
29
- throw new kernel.ChangeWorkflowError(
30
- 'CHANGE_NOT_MUTABLE',
31
- `change ${change.id} is ${change.status}`,
32
- );
33
- }
34
- const health = baselines.inspectBaseline(db, { rootDir });
35
- return {
36
- allowed: true,
37
- diagnostics: health.status === 'pass'
38
- ? []
39
- : (health.blockers || []).map((code) => ({
40
- code,
41
- severity: 'needs_attention',
42
- })),
43
- };
44
- }
45
-
46
- function archiveChange(db, input, options = {}) {
47
- return kernel.archiveChange(db, input, options);
48
- }
49
-
50
- module.exports = {
51
- ChangeWorkflowError: kernel.ChangeWorkflowError,
52
- createChange,
53
- readChange: kernel.readChange,
54
- listChanges: kernel.listChanges,
55
- updateChange,
56
- supersedeChange,
57
- recordDelta: kernel.recordDelta,
58
- recordDocumentationReconciliation: kernel.recordDocumentationReconciliation,
59
- archiveChange,
60
- normalizeDocsImpact: kernel.normalizeDocsImpact,
61
- normalizeProviderRefs: kernel.normalizeProviderRefs,
62
- normalizeBaselineBypass: kernel.normalizeBaselineBypass,
63
- assertTaskCreationAllowed,
64
- recoverInterruptedArchives: kernel.recoverInterruptedArchives,
65
- };
@@ -1,581 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('node:path');
4
-
5
- const { version: PACKAGE_VERSION } = require('../../package.json');
6
- const artifactRegistry = require('./artifact-registry.cjs');
7
- const baselines = require('./baseline-workflow.cjs');
8
- const canonical = require('./canonical-json.cjs');
9
- const changes = require('./change-workflow.cjs');
10
- const decisions = require('./decision-records.cjs');
11
- const {
12
- restoreManagedFile,
13
- snapshotManagedFile,
14
- writeManagedJson,
15
- } = require('./managed-file-write.cjs');
16
- const ops = require('./state-ops.cjs');
17
- const { readStableProjectFile } = require('./safe-project-file.cjs');
18
- const checkpoints = require('./stage-checkpoints.cjs');
19
- const taskLedger = require('./task-ledger.cjs');
20
-
21
- const SUMMARY_LIMIT = 16 * 1024;
22
- const FULL_LIMIT = 64 * 1024;
23
-
24
- class ContextEnvelopeError extends Error {
25
- constructor(code, message, details) {
26
- super(message);
27
- this.name = 'ContextEnvelopeError';
28
- this.code = code;
29
- if (details !== undefined) this.details = details;
30
- }
31
- }
32
-
33
- function parseJson(value, fallback) {
34
- try { return value == null ? fallback : JSON.parse(value); }
35
- catch { return fallback; }
36
- }
37
-
38
- function text(value, max) {
39
- if (typeof value !== 'string') return value;
40
- if (value.length <= max) return value;
41
- return `${value.slice(0, Math.max(0, max - 32))}…[truncated ${value.length - max + 32} chars]`;
42
- }
43
-
44
- function compactValue(value, {
45
- depth = 0,
46
- maxDepth = 5,
47
- textLimit = 2000,
48
- arrayLimit = 50,
49
- } = {}) {
50
- if (typeof value === 'string') return text(value, textLimit);
51
- if (value === null || value === undefined || typeof value !== 'object') return value;
52
- if (depth >= maxDepth) return Array.isArray(value) ? `[${value.length} items]` : '[object]';
53
- if (Array.isArray(value)) {
54
- return value.slice(0, arrayLimit).map((item) => compactValue(item, {
55
- depth: depth + 1, maxDepth, textLimit, arrayLimit,
56
- }));
57
- }
58
- return Object.fromEntries(Object.entries(value).map(([key, child]) => [
59
- key,
60
- compactValue(child, { depth: depth + 1, maxDepth, textLimit, arrayLimit }),
61
- ]));
62
- }
63
-
64
- function selectedChange(db, changeId) {
65
- if (!changeId) return null;
66
- return changes.readChange(db, changeId);
67
- }
68
-
69
- function selectedTask(db, taskId) {
70
- if (!taskId) return null;
71
- return ops.readTask(db, taskId);
72
- }
73
-
74
- function resolvedScope(db, input = {}) {
75
- const requested = input.scope || {};
76
- const task = selectedTask(db, requested.task_id);
77
- if (requested.task_id && !task) {
78
- throw new ContextEnvelopeError(
79
- 'CONTEXT_SCOPE_NOT_FOUND',
80
- `Context task scope does not exist: ${requested.task_id}`,
81
- { scope_type: 'task', scope_id: requested.task_id },
82
- );
83
- }
84
- const changeId = requested.change_id || task?.change_id || null;
85
- const change = selectedChange(db, changeId);
86
- if (requested.change_id && !change) {
87
- throw new ContextEnvelopeError(
88
- 'CONTEXT_SCOPE_NOT_FOUND',
89
- `Context Change scope does not exist: ${requested.change_id}`,
90
- { scope_type: 'change', scope_id: requested.change_id },
91
- );
92
- }
93
- if (task && requested.change_id && task.change_id !== requested.change_id) {
94
- throw new ContextEnvelopeError(
95
- 'CONTEXT_SCOPE_MISMATCH',
96
- `Context task ${task.id} belongs to Change ${task.change_id || '(none)'}, not ${requested.change_id}`,
97
- {
98
- task_id: task.id,
99
- task_change_id: task.change_id || null,
100
- requested_change_id: requested.change_id,
101
- },
102
- );
103
- }
104
- const baseline = baselines.readBaseline(db);
105
- if (task) return { type: 'task', id: task.id, baseline, change, task };
106
- if (change) return { type: 'change', id: change.id, baseline, change, task: null };
107
- if (!requested.task_id && !requested.change_id) {
108
- const activeChangeRow = db.prepare(
109
- `SELECT id FROM changes
110
- WHERE status IN ('active', 'blocked', 'ready')
111
- ORDER BY
112
- CASE status WHEN 'active' THEN 0 WHEN 'blocked' THEN 1 ELSE 2 END,
113
- updated_at DESC,
114
- rowid DESC
115
- LIMIT 1`,
116
- ).get();
117
- if (activeChangeRow) {
118
- const activeChange = selectedChange(db, activeChangeRow.id);
119
- const activeTaskRow = db.prepare(
120
- `SELECT id FROM tasks
121
- WHERE change_id = ? AND status IN ('in_progress', 'blocked', 'pending')
122
- ORDER BY
123
- CASE status WHEN 'in_progress' THEN 0 WHEN 'blocked' THEN 1 ELSE 2 END,
124
- updated_at DESC,
125
- rowid DESC
126
- LIMIT 1`,
127
- ).get(activeChangeRow.id);
128
- const activeTask = activeTaskRow ? selectedTask(db, activeTaskRow.id) : null;
129
- if (activeTask) {
130
- return {
131
- type: 'task',
132
- id: activeTask.id,
133
- baseline,
134
- change: activeChange,
135
- task: activeTask,
136
- };
137
- }
138
- return {
139
- type: 'change',
140
- id: activeChange.id,
141
- baseline,
142
- change: activeChange,
143
- task: null,
144
- };
145
- }
146
- }
147
- if (baseline) return { type: 'baseline', id: baseline.id, baseline, change: null, task: null };
148
- return { type: 'project', id: 'project', baseline: null, change: null, task: null };
149
- }
150
-
151
- function acceptedDecisionRows(db, scope, rootDir) {
152
- const rows = [];
153
- if (scope.baseline) {
154
- rows.push(...decisions.listAcceptedDecisions(
155
- db,
156
- { baseline_id: scope.baseline.id },
157
- { limit: 200, rootDir, validateFiles: true },
158
- ));
159
- }
160
- if (scope.change) {
161
- rows.push(...decisions.listAcceptedDecisions(
162
- db,
163
- { change_id: scope.change.id },
164
- { limit: 200, rootDir, validateFiles: true },
165
- ));
166
- }
167
- return rows;
168
- }
169
-
170
- function checkpointRows(db, scope) {
171
- const rows = [];
172
- if (scope.change) {
173
- rows.push(...checkpoints.listCheckpoints(
174
- db,
175
- { scope: { change_id: scope.change.id }, limit: 100 },
176
- ).filter((item) => item.status !== 'superseded'));
177
- }
178
- if (scope.task) {
179
- rows.push(...checkpoints.listCheckpoints(
180
- db,
181
- { scope: { task_id: scope.task.id }, limit: 100 },
182
- ).filter((item) => item.status !== 'superseded'));
183
- }
184
- if (!scope.change && scope.baseline) {
185
- rows.push(...checkpoints.listCheckpoints(
186
- db,
187
- { scope: { baseline_id: scope.baseline.id }, limit: 100 },
188
- ).filter((item) => item.status !== 'superseded'));
189
- }
190
- return rows;
191
- }
192
-
193
- function artifactRows(db, scope) {
194
- const clauses = [
195
- "status <> 'archived'",
196
- "kind NOT IN ('context_envelope', 'worker_packet')",
197
- ];
198
- const params = [];
199
- if (scope.task) {
200
- clauses.push('(task_id = ? OR change_id = ?)');
201
- params.push(scope.task.id, scope.change?.id || '');
202
- } else if (scope.change) {
203
- clauses.push('change_id = ?');
204
- params.push(scope.change.id);
205
- } else if (scope.baseline) {
206
- clauses.push("(owner_type = 'baseline' AND owner_id = ?)");
207
- params.push(scope.baseline.id);
208
- } else {
209
- clauses.push("owner_type = 'project'");
210
- }
211
- return db.prepare(
212
- `SELECT id, owner_type, owner_id, kind, path, digest, status, provenance_json
213
- FROM artifacts WHERE ${clauses.join(' AND ')}
214
- ORDER BY updated_at DESC, id ASC LIMIT 500`,
215
- ).all(...params).map((row) => ({
216
- id: row.id,
217
- owner_type: row.owner_type,
218
- owner_id: row.owner_id,
219
- kind: row.kind,
220
- ref: row.path,
221
- digest: row.digest,
222
- status: row.status,
223
- provenance: parseJson(row.provenance_json, {}),
224
- }));
225
- }
226
-
227
- function executionState(db, scope) {
228
- if (!scope.task) return { session: null };
229
- const session = db.prepare(
230
- `SELECT sid, task_id, runtime, worktree_path, artifact_dir, status,
231
- lease_expires_at, heartbeat_at, started_at
232
- FROM sessions WHERE task_id = ?
233
- ORDER BY started_at DESC, rowid DESC LIMIT 1`,
234
- ).get(scope.task.id) || null;
235
- const packet = db.prepare(
236
- `SELECT id, role, context_envelope_id, context_digest, task_digest,
237
- decision_digest, packet_digest, packet_path, output_path, created_at
238
- FROM worker_packets WHERE scope_type = 'task' AND scope_id = ?
239
- AND status = 'assigned'
240
- ORDER BY created_at DESC, rowid DESC LIMIT 1`,
241
- ).get(scope.task.id) || null;
242
- return { session, worker_packet: packet };
243
- }
244
-
245
- function ledgerHealth(db, rootDir) {
246
- try {
247
- const result = taskLedger.inspectTaskLedger(db, { rootDir });
248
- return {
249
- status: result.status || 'available',
250
- generation: result.generation ?? null,
251
- state_digest: result.state_digest || null,
252
- path: taskLedger.LEDGER_RELATIVE_PATH,
253
- };
254
- } catch (error) {
255
- return {
256
- status: 'migration_required',
257
- generation: null,
258
- state_digest: null,
259
- path: taskLedger.LEDGER_RELATIVE_PATH,
260
- migration: {
261
- required: true,
262
- code: error.code || 'TASK_LEDGER_INVALID',
263
- action: 'ultra.sync migrate',
264
- },
265
- };
266
- }
267
- }
268
-
269
- function authorityPayload(db, input, options) {
270
- const rootDir = path.resolve(options.rootDir || process.cwd());
271
- const stage = input.stage || 'project';
272
- const scope = resolvedScope(db, input);
273
- const baselineHealth = scope.baseline
274
- ? baselines.inspectBaseline(db, { rootDir })
275
- : { blockers: [], warnings: [] };
276
- const checkpointList = checkpointRows(db, scope);
277
- const diagnostics = {
278
- warnings: (baselineHealth.warnings || []).map((code) => ({ code, severity: 'warning' })),
279
- needs_attention: checkpointList.flatMap((item) => (
280
- item.diagnostics.filter((diagnostic) => diagnostic.severity === 'needs_attention')
281
- )),
282
- hard_conflicts: checkpointList.flatMap((item) => (
283
- item.diagnostics.filter((diagnostic) => diagnostic.severity === 'hard_conflict')
284
- )),
285
- };
286
- if (scope.task) {
287
- diagnostics.warnings.push(...ops.taskContractBlockers(scope.task).map((code) => ({
288
- code,
289
- severity: 'warning',
290
- message: 'Task semantics are incomplete; the active Skill and model own the decision to proceed.',
291
- })));
292
- }
293
- if (scope.change) {
294
- const advisory = [
295
- [!String(scope.change.contract?.outcome || '').trim(), 'CHANGE_OUTCOME_UNRESOLVED'],
296
- [!Array.isArray(scope.change.contract?.acceptance)
297
- || scope.change.contract.acceptance.length === 0, 'CHANGE_ACCEPTANCE_UNRESOLVED'],
298
- [!scope.change.contract?.recovery, 'CHANGE_RECOVERY_UNRESOLVED'],
299
- [!String(scope.change.classification?.rationale || '').trim(), 'CHANGE_CLASSIFICATION_UNRESOLVED'],
300
- [scope.change.research_disposition?.status === 'unresolved',
301
- 'CHANGE_RESEARCH_DISPOSITION_UNRESOLVED'],
302
- ];
303
- diagnostics.warnings.push(...advisory
304
- .filter(([missing]) => missing)
305
- .map(([, code]) => ({
306
- code,
307
- severity: 'warning',
308
- message: 'Change semantics remain mutable; the active Skill and model own completion.',
309
- })));
310
- }
311
- for (const code of baselineHealth.blockers || []) {
312
- diagnostics.needs_attention.push({ code, severity: 'needs_attention' });
313
- }
314
- const ledger = ledgerHealth(db, rootDir);
315
- if (ledger.migration?.required) {
316
- diagnostics.needs_attention.push({
317
- code: ledger.migration.code,
318
- severity: 'needs_attention',
319
- action: ledger.migration.action,
320
- });
321
- }
322
- return {
323
- schema_version: '1.0',
324
- project: {
325
- root: '.',
326
- runtime: options.runtime || 'unknown',
327
- plugin_version: PACKAGE_VERSION,
328
- health: diagnostics.hard_conflicts.length > 0 ? 'conflict' : 'available',
329
- },
330
- git: scope.baseline ? {
331
- head: baselineHealth.current_repository_revision || scope.baseline.repository_revision || null,
332
- branch: scope.baseline.repository_branch || null,
333
- scoped_worktree_digest: scope.baseline.worktree_digest || null,
334
- drift: {
335
- metadata_only: (baselineHealth.warnings || []).includes('BASELINE_METADATA_ONLY_DRIFT'),
336
- source_or_spec: (baselineHealth.blockers || []).some((code) => (
337
- String(code).includes('SOURCE') || String(code).includes('SPEC')
338
- )),
339
- },
340
- } : null,
341
- baseline: scope.baseline,
342
- change: scope.change,
343
- task: scope.task,
344
- decisions: acceptedDecisionRows(db, scope, rootDir),
345
- checkpoints: checkpointList,
346
- evidence: [
347
- ...(scope.baseline?.evidence || []),
348
- ...artifactRows(db, scope),
349
- ],
350
- execution: {
351
- stage,
352
- ...executionState(db, scope),
353
- },
354
- team_checkpoint: ledger,
355
- diagnostics,
356
- };
357
- }
358
-
359
- function presentation(authority, detail) {
360
- const summary = detail !== 'full';
361
- return compactValue(authority, {
362
- maxDepth: summary ? 4 : 7,
363
- textLimit: summary ? 500 : 4000,
364
- arrayLimit: summary ? 12 : 100,
365
- });
366
- }
367
-
368
- function fitEnvelope(authority, detail, digest) {
369
- const limit = detail === 'full' ? FULL_LIMIT : SUMMARY_LIMIT;
370
- let envelope = presentation(authority, detail);
371
- let output = { schema_version: '1.0', digest, detail, envelope };
372
- if (Buffer.byteLength(JSON.stringify(output)) <= limit) return output;
373
- envelope = {
374
- ...envelope,
375
- evidence: (envelope.evidence || []).slice(0, detail === 'full' ? 30 : 6),
376
- decisions: (envelope.decisions || []).slice(0, detail === 'full' ? 30 : 8),
377
- checkpoints: (envelope.checkpoints || []).slice(0, detail === 'full' ? 20 : 8),
378
- cursors: {
379
- evidence: authority.evidence.length > (detail === 'full' ? 30 : 6)
380
- ? { available: authority.evidence.length, action: 'ultra.context full with narrower scope' }
381
- : null,
382
- decisions: authority.decisions.length > (detail === 'full' ? 30 : 8)
383
- ? { available: authority.decisions.length, action: 'ultra.context full with narrower scope' }
384
- : null,
385
- },
386
- };
387
- output = { schema_version: '1.0', digest, detail, envelope };
388
- if (Buffer.byteLength(JSON.stringify(output)) <= limit) return output;
389
- envelope = compactValue(envelope, {
390
- maxDepth: 4,
391
- textLimit: detail === 'full' ? 1000 : 240,
392
- arrayLimit: detail === 'full' ? 20 : 6,
393
- });
394
- output = { schema_version: '1.0', digest, detail, envelope };
395
- if (Buffer.byteLength(JSON.stringify(output)) > limit) {
396
- const error = new Error(`context envelope exceeds ${limit} byte limit after compaction`);
397
- error.code = 'CONTEXT_ENVELOPE_LIMIT_EXCEEDED';
398
- throw error;
399
- }
400
- return output;
401
- }
402
-
403
- function buildEnvelope(db, input = {}, options = {}) {
404
- const authority = authorityPayload(db, input, options);
405
- const digest = canonical.digest(authority);
406
- const output = fitEnvelope(authority, input.detail || 'summary', digest);
407
- output.bytes = Buffer.byteLength(JSON.stringify(output));
408
- return output;
409
- }
410
-
411
- function contextArtifactPath(scope, id) {
412
- if (scope.task) return `.ultra/.runtime/projections/contexts/${id}.json`;
413
- if (scope.change) return `${scope.change.artifact_root}/contexts/${id}.json`;
414
- return `.ultra/.runtime/projections/contexts/${id}.json`;
415
- }
416
-
417
- function readEnvelope(db, id, { rootDir = process.cwd() } = {}) {
418
- const row = db.prepare('SELECT * FROM context_envelopes WHERE id = ?').get(id);
419
- if (!row) {
420
- throw new ContextEnvelopeError(
421
- 'CONTEXT_ENVELOPE_NOT_FOUND',
422
- `Context Envelope not found: ${id}`,
423
- );
424
- }
425
- const read = readStableProjectFile(rootDir, row.artifact_path, { encoding: 'utf8' });
426
- if (!row.file_digest || read.digest !== row.file_digest) {
427
- throw new ContextEnvelopeError(
428
- 'CONTEXT_ENVELOPE_FILE_DRIFT',
429
- `Context Envelope bytes no longer match ${id}`,
430
- {
431
- path: row.artifact_path,
432
- expected: row.file_digest || null,
433
- actual: read.digest,
434
- },
435
- );
436
- }
437
- let document;
438
- try {
439
- document = JSON.parse(read.text);
440
- } catch (cause) {
441
- throw new ContextEnvelopeError(
442
- 'CONTEXT_ENVELOPE_INVALID',
443
- `Context Envelope is not valid JSON: ${row.artifact_path}`,
444
- { cause: cause.message },
445
- );
446
- }
447
- const storedPayload = parseJson(row.payload_json, null);
448
- if (!storedPayload
449
- || document.schema_version !== '1.0'
450
- || document.detail !== 'full'
451
- || document.digest !== row.digest
452
- || canonical.canonicalJson(document.envelope)
453
- !== canonical.canonicalJson(storedPayload)) {
454
- throw new ContextEnvelopeError(
455
- 'CONTEXT_ENVELOPE_DIGEST_MISMATCH',
456
- `Context Envelope authority does not match its database record: ${id}`,
457
- { path: row.artifact_path },
458
- );
459
- }
460
- return {
461
- id: row.id,
462
- stage: row.stage,
463
- scope_type: row.scope_type,
464
- scope_id: row.scope_id,
465
- digest: row.digest,
466
- file_digest: row.file_digest,
467
- artifact_path: row.artifact_path,
468
- document,
469
- };
470
- }
471
-
472
- function persistEnvelope(db, input = {}, options = {}) {
473
- const full = buildEnvelope(db, { ...input, detail: 'full' }, options);
474
- const existing = db.prepare(
475
- 'SELECT * FROM context_envelopes WHERE digest = ?',
476
- ).get(full.digest);
477
- if (existing) {
478
- readEnvelope(db, existing.id, options);
479
- return {
480
- id: existing.id,
481
- digest: existing.digest,
482
- artifact_path: existing.artifact_path,
483
- stage: existing.stage,
484
- scope_type: existing.scope_type,
485
- scope_id: existing.scope_id,
486
- file_digest: existing.file_digest,
487
- };
488
- }
489
- const scope = resolvedScope(db, input);
490
- const id = `context-${full.digest.slice(0, 24)}`;
491
- const artifactPath = contextArtifactPath(scope, id);
492
- const rootDir = options.rootDir || process.cwd();
493
- const snapshot = snapshotManagedFile(rootDir, artifactPath);
494
- const published = writeManagedJson(rootDir, artifactPath, full);
495
- try {
496
- ops.tx(db, () => {
497
- db.prepare(
498
- `INSERT INTO context_envelopes
499
- (id, stage, scope_type, scope_id, digest, file_digest, payload_json, artifact_path)
500
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
501
- ).run(
502
- id,
503
- input.stage || 'project',
504
- scope.type,
505
- scope.id,
506
- full.digest,
507
- published.digest,
508
- JSON.stringify(full.envelope),
509
- artifactPath,
510
- );
511
- if (!artifactPath.startsWith('.ultra/.runtime/')) {
512
- artifactRegistry.recordArtifactInTx(db, {
513
- id: `artifact-${id}`,
514
- owner_type: scope.change ? 'change' : (scope.baseline ? 'baseline' : 'project'),
515
- owner_id: scope.change?.id || scope.baseline?.id || 'project',
516
- kind: 'context_envelope',
517
- path: artifactPath,
518
- content_digest: published.digest,
519
- source_refs: [
520
- ...(scope.change
521
- ? [{ type: 'change', id: scope.change.id, relation: 'compiled_from' }]
522
- : []),
523
- ...(scope.task
524
- ? [{ type: 'task', id: scope.task.id, relation: 'compiled_from_task_contract' }]
525
- : []),
526
- ...(scope.baseline && !scope.change
527
- ? [{ type: 'baseline', id: scope.baseline.id, relation: 'compiled_from' }]
528
- : []),
529
- ],
530
- consumer_refs: [{
531
- type: 'external',
532
- id: 'ultra-context-consumer',
533
- relation: 'consumed_by',
534
- }],
535
- provenance: {
536
- writer: 'context-envelope',
537
- envelope_digest: full.digest,
538
- stage: input.stage || 'project',
539
- },
540
- metadata: {
541
- envelope_id: id,
542
- semantic_digest: full.digest,
543
- },
544
- }, { rootDir });
545
- }
546
- });
547
- } catch (error) {
548
- try {
549
- restoreManagedFile(rootDir, snapshot);
550
- } catch (rollbackError) {
551
- throw new ContextEnvelopeError(
552
- 'STATE_CORRUPT',
553
- 'Context Envelope publication failed and its managed file could not be restored',
554
- {
555
- cause: error.code || error.message,
556
- rollback: rollbackError.code || rollbackError.message,
557
- path: artifactPath,
558
- },
559
- );
560
- }
561
- throw error;
562
- }
563
- return {
564
- id,
565
- digest: full.digest,
566
- artifact_path: artifactPath,
567
- stage: input.stage || 'project',
568
- scope_type: scope.type,
569
- scope_id: scope.id,
570
- file_digest: published.digest,
571
- };
572
- }
573
-
574
- module.exports = {
575
- ContextEnvelopeError,
576
- SUMMARY_LIMIT,
577
- FULL_LIMIT,
578
- buildEnvelope,
579
- persistEnvelope,
580
- readEnvelope,
581
- };