ultra-builder-pro-cli 0.20.0 → 0.22.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.
- package/.ultra-template/docs/research/README.md +26 -5
- package/.ultra-template/specs/discovery.md +1 -1
- package/.ultra-template/tasks/tasks.json +10 -3
- package/.ultra-template/templates/task-context.md +19 -0
- package/CHANGELOG.md +106 -0
- package/README.md +215 -41
- package/adapters/_shared/codex-assets.cjs +69 -11
- package/adapters/_shared/interaction-contract.cjs +36 -3
- package/adapters/_shared/runtime-assets.cjs +41 -1
- package/adapters/claude.js +7 -5
- package/adapters/codex.js +1 -1
- package/adapters/kimi.js +2 -8
- package/adapters/opencode.js +43 -30
- package/bin/orchestrator.js +64 -24
- package/docs/AGENT-CONTEXT.md +89 -49
- package/docs/ARCHITECTURE.md +112 -45
- package/docs/ARTIFACT-AUTHORITY.md +274 -0
- package/docs/COMMIT-HASH-BACKFILL.md +33 -90
- package/docs/DECISIONS.md +14 -6
- package/docs/LEGACY-CLI-CROSSWALK.md +16 -6
- package/docs/PLUGIN-ISOLATION-CONTRACT.md +6 -4
- package/docs/ROADMAP.md +20 -6
- package/docs/RUNTIME-COMPAT-MATRIX.md +29 -16
- package/docs/STATE-DB-ACCESS-POLICY.md +43 -13
- package/docs/WORKFLOW-LIFECYCLE.md +105 -50
- package/hooks/README.md +10 -8
- package/hooks/active_task_context.py +105 -18
- package/hooks/context_spine.py +16 -4
- package/hooks/health_check.py +5 -4
- package/hooks/pre_stop_check.py +5 -2
- package/hooks/runtime_paths.py +396 -0
- package/hooks/subagent_tracker.py +5 -2
- package/hooks/workflow_checkpoint.py +16 -3
- package/hooks/workflow_context.py +11 -3
- package/hooks/workflow_resume.py +7 -4
- package/mcp-server/breadcrumb.cjs +20 -2
- package/mcp-server/lib/archive-journal.cjs +1026 -111
- package/mcp-server/lib/archive-mutation-worker.py +365 -0
- package/mcp-server/lib/artifact-registry.cjs +1127 -0
- package/mcp-server/lib/baseline-workflow.cjs +69 -28
- package/mcp-server/lib/change-packet.cjs +818 -0
- package/mcp-server/lib/change-workflow.cjs +932 -176
- package/mcp-server/lib/context-paths.cjs +142 -0
- package/mcp-server/lib/context-spine.cjs +511 -45
- package/mcp-server/lib/decision-dialogue.cjs +345 -3
- package/mcp-server/lib/delivery-transaction.cjs +471 -0
- package/mcp-server/lib/doctor-backup-worker.cjs +328 -0
- package/mcp-server/lib/doctor.cjs +401 -11
- package/mcp-server/lib/git-bootstrap.cjs +458 -47
- package/mcp-server/lib/init-project.cjs +240 -95
- package/mcp-server/lib/plan-publication-worker.cjs +830 -0
- package/mcp-server/lib/plan-store.cjs +700 -5
- package/mcp-server/lib/project-breadcrumb.cjs +23 -4
- package/mcp-server/lib/projector.cjs +211 -42
- package/mcp-server/lib/runtime-paths.cjs +1687 -0
- package/mcp-server/lib/safe-project-file.cjs +385 -0
- package/mcp-server/lib/spec-learning.cjs +28 -12
- package/mcp-server/lib/state-authority.cjs +19 -3
- package/mcp-server/lib/state-db.cjs +263 -9
- package/mcp-server/lib/state-ops.cjs +254 -50
- package/mcp-server/lib/task-ledger.cjs +1342 -0
- package/mcp-server/lib/telemetry.cjs +12 -3
- package/mcp-server/lib/workflow-state.cjs +610 -58
- package/mcp-server/server.cjs +423 -42
- package/orchestrator/daemon.cjs +2 -10
- package/orchestrator/parallel-orchestrator.cjs +314 -50
- package/orchestrator/planner/plan-builder.cjs +1 -1
- package/orchestrator/recovery.cjs +125 -7
- package/orchestrator/session-close-journal-worker.cjs +5 -0
- package/orchestrator/session-close-journal.cjs +1014 -0
- package/orchestrator/session-runner.cjs +679 -69
- package/orchestrator/worktree-manager.cjs +186 -35
- package/package.json +2 -3
- package/skills/cc-collab/SKILL.md +4 -1
- package/skills/codex-collab/SKILL.md +5 -2
- package/skills/ultra-change/SKILL.md +34 -8
- package/skills/ultra-deliver/SKILL.md +26 -11
- package/skills/ultra-dev/SKILL.md +52 -10
- package/skills/ultra-doctor/SKILL.md +16 -3
- package/skills/ultra-init/SKILL.md +22 -11
- package/skills/ultra-plan/SKILL.md +58 -18
- package/skills/ultra-research/SKILL.md +37 -35
- package/skills/ultra-review/SKILL.md +14 -8
- package/skills/ultra-status/SKILL.md +18 -9
- package/skills/ultra-test/SKILL.md +11 -5
- package/skills/ultra-think/SKILL.md +6 -2
- package/skills/ultra-think/references/decision-dialogue.md +17 -6
- package/skills/ultra-verify/SKILL.md +4 -1
- package/spec/cli-protocol.md +21 -7
- package/spec/mcp-tools.yaml +629 -44
- package/spec/schemas/change-delta.v1.schema.json +153 -0
- package/spec/schemas/context-file.v4.5.schema.json +1 -1
- package/spec/schemas/documentation-reconciliation.v1.schema.json +106 -0
- package/spec/schemas/mcp-tools.schema.json +1 -1
- package/spec/schemas/state-db.sql +51 -14
- package/spec/schemas/task-ledger.v1.schema.json +217 -0
- package/spec/schemas/tasks.v4.5.schema.json +1 -1
- package/ultra-tools/cli.cjs +3 -2
- package/ultra-tools/commands/db.cjs +76 -25
- package/ultra-tools/commands/migrate.cjs +1000 -82
- package/ultra-tools/commands/session.cjs +34 -8
- package/ultra-tools/commands/status.cjs +32 -6
- package/ultra-tools/commands/system.cjs +129 -22
- package/.ultra-template/tasks/contexts/TEMPLATE.md +0 -19
- package/commands/learn.md +0 -10
- package/orchestrator/skill-miner.cjs +0 -133
- package/output-styles/coding-vibes.md +0 -67
- package/output-styles/structural-thinking.md +0 -61
- package/skills/learn/SKILL.md +0 -58
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const childProcess = require('node:child_process');
|
|
3
5
|
const fs = require('node:fs');
|
|
4
6
|
const path = require('node:path');
|
|
5
7
|
|
|
@@ -13,14 +15,24 @@ const changes = require('./change-workflow.cjs');
|
|
|
13
15
|
const archiveJournal = require('./archive-journal.cjs');
|
|
14
16
|
const workflows = require('./workflow-state.cjs');
|
|
15
17
|
const decisions = require('./decision-dialogue.cjs');
|
|
18
|
+
const runtimePaths = require('./runtime-paths.cjs');
|
|
19
|
+
const artifactRegistry = require('./artifact-registry.cjs');
|
|
20
|
+
const planStore = require('./plan-store.cjs');
|
|
21
|
+
const taskLedger = require('./task-ledger.cjs');
|
|
22
|
+
const closeJournal = require('../../orchestrator/session-close-journal.cjs');
|
|
16
23
|
|
|
17
24
|
const SPEC_CONSUMER = 'spec-staleness';
|
|
25
|
+
const BACKUP_WORKER_FILENAME = 'doctor-backup-worker.cjs';
|
|
26
|
+
const BACKUP_WORKER_TIMEOUT_MS = 120 * 1000;
|
|
27
|
+
const BACKUP_WORKER_METHOD = 'sqlite-serialize-pinned-fd-v1';
|
|
18
28
|
|
|
19
29
|
class DoctorError extends Error {
|
|
20
|
-
constructor(code, message) {
|
|
30
|
+
constructor(code, message, { cause, details } = {}) {
|
|
21
31
|
super(message);
|
|
22
32
|
this.name = 'DoctorError';
|
|
23
33
|
this.code = code;
|
|
34
|
+
if (cause) this.cause = cause;
|
|
35
|
+
if (details !== undefined) this.details = details;
|
|
24
36
|
}
|
|
25
37
|
}
|
|
26
38
|
|
|
@@ -28,6 +40,73 @@ function timestamp() {
|
|
|
28
40
|
return new Date().toISOString().replace(/[:.]/g, '-');
|
|
29
41
|
}
|
|
30
42
|
|
|
43
|
+
function inspectSessionCloses(db, {
|
|
44
|
+
rootDir = process.cwd(),
|
|
45
|
+
sessionsAvailable = true,
|
|
46
|
+
} = {}) {
|
|
47
|
+
let intents;
|
|
48
|
+
try {
|
|
49
|
+
intents = closeJournal.list(rootDir);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return {
|
|
52
|
+
status: 'fail',
|
|
53
|
+
pending: 1,
|
|
54
|
+
items: [{
|
|
55
|
+
code: error.code || 'SESSION_CLOSE_JOURNAL_INVALID',
|
|
56
|
+
reason: 'journal_invalid',
|
|
57
|
+
message: error.message,
|
|
58
|
+
}],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const items = intents.map((intent) => {
|
|
62
|
+
const session = sessionsAvailable ? ops.readSession(db, intent.sid) : null;
|
|
63
|
+
let reason = 'recovery_required';
|
|
64
|
+
if (!sessionsAvailable) reason = 'session_state_unavailable';
|
|
65
|
+
else if (!session) reason = 'session_missing';
|
|
66
|
+
else if (session.status === intent.requested_status) reason = 'terminal_reconciliation_required';
|
|
67
|
+
else if (['completed', 'crashed'].includes(session.status)) {
|
|
68
|
+
reason = `terminal_status_conflict:${session.status}`;
|
|
69
|
+
} else if (recovery._internal.isPidAlive(session.pid)) {
|
|
70
|
+
reason = 'worker_still_alive';
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
code: 'SESSION_CLOSE_PENDING',
|
|
74
|
+
sid: intent.sid,
|
|
75
|
+
task_id: intent.task_id,
|
|
76
|
+
requested_status: intent.requested_status,
|
|
77
|
+
phase: intent.phase,
|
|
78
|
+
reason,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
return {
|
|
82
|
+
status: items.length === 0 ? 'pass' : 'fail',
|
|
83
|
+
pending: items.length,
|
|
84
|
+
items,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function inspectTeamTaskLedger(db, rootDir) {
|
|
89
|
+
try {
|
|
90
|
+
const health = taskLedger.inspectTaskLedger(db, { rootDir });
|
|
91
|
+
const taskCount = db.prepare('SELECT COUNT(*) AS count FROM tasks').get().count;
|
|
92
|
+
let status = 'warning';
|
|
93
|
+
if (health.status === 'current') status = 'pass';
|
|
94
|
+
return {
|
|
95
|
+
...health,
|
|
96
|
+
condition: health.status,
|
|
97
|
+
status,
|
|
98
|
+
task_count: taskCount,
|
|
99
|
+
};
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return {
|
|
102
|
+
status: 'fail',
|
|
103
|
+
condition: 'invalid',
|
|
104
|
+
code: error.code || 'TASK_LEDGER_INVALID',
|
|
105
|
+
message: error.message,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
31
110
|
function inspectSystem(db, { rootDir = process.cwd() } = {}) {
|
|
32
111
|
const tables = new Set(tableNames(db));
|
|
33
112
|
const missing = REQUIRED_TABLES.filter((name) => !tables.has(name));
|
|
@@ -39,6 +118,10 @@ function inspectSystem(db, { rootDir = process.cwd() } = {}) {
|
|
|
39
118
|
const orphanSessions = missing.length === 0
|
|
40
119
|
? db.prepare("SELECT COUNT(*) AS count FROM sessions WHERE status = 'orphan'").get().count
|
|
41
120
|
: 0;
|
|
121
|
+
const sessionCloses = inspectSessionCloses(db, {
|
|
122
|
+
rootDir,
|
|
123
|
+
sessionsAvailable: missing.length === 0,
|
|
124
|
+
});
|
|
42
125
|
const eventCursor = missing.length === 0
|
|
43
126
|
? Number(db.prepare('SELECT COALESCE(MAX(id), 0) AS id FROM events').get().id)
|
|
44
127
|
: 0;
|
|
@@ -71,14 +154,27 @@ function inspectSystem(db, { rootDir = process.cwd() } = {}) {
|
|
|
71
154
|
checkpoint_ready: 0,
|
|
72
155
|
deferred_blocking: 0, stale_artifacts: [], current: null, current_thread_id: null,
|
|
73
156
|
};
|
|
157
|
+
const artifactHealth = missing.length === 0
|
|
158
|
+
? artifactRegistry.inspectArtifactHealth(db, { rootDir })
|
|
159
|
+
: { status: 'fail', registered: 0, issues: [], counts: {} };
|
|
160
|
+
const planPublications = missing.length === 0
|
|
161
|
+
? planStore.inspectPlanPublications(db, { rootDir })
|
|
162
|
+
: { status: 'fail', pending: 0, transactions: [], issues: [] };
|
|
163
|
+
const taskLedgerHealth = missing.length === 0
|
|
164
|
+
? inspectTeamTaskLedger(db, rootDir)
|
|
165
|
+
: { status: 'fail', condition: 'state_unavailable', code: 'STATE_DB_UNAVAILABLE' };
|
|
74
166
|
const baselineCheckStatus = baseline.status === 'pass'
|
|
75
167
|
? 'pass'
|
|
76
168
|
: (baseline.baseline?.status === 'ready' ? 'fail' : 'warning');
|
|
77
169
|
const degraded = integrity !== 'ok' || missing.length > 0 || incidents.length > 0
|
|
78
170
|
|| pending.length > 0 || running.length > 0 || failed.length > 0 || orphanSessions > 0 || activeMissing > 0
|
|
171
|
+
|| sessionCloses.status === 'fail'
|
|
79
172
|
|| archiveIntents.length > 0
|
|
80
173
|
|| eventCursor > projectedCursor || workflowHealth.status === 'fail'
|
|
81
174
|
|| decisionHealth.status === 'fail'
|
|
175
|
+
|| artifactHealth.status === 'fail'
|
|
176
|
+
|| planPublications.status === 'fail'
|
|
177
|
+
|| taskLedgerHealth.status === 'fail'
|
|
82
178
|
|| baselineCheckStatus === 'fail';
|
|
83
179
|
return {
|
|
84
180
|
status: degraded ? 'degraded' : 'healthy',
|
|
@@ -90,8 +186,16 @@ function inspectSystem(db, { rootDir = process.cwd() } = {}) {
|
|
|
90
186
|
pending: pending.length, running: running.length, failed: failed.length,
|
|
91
187
|
event_cursor: eventCursor, projected_cursor: projectedCursor,
|
|
92
188
|
},
|
|
93
|
-
sessions: {
|
|
189
|
+
sessions: {
|
|
190
|
+
status: orphanSessions === 0 && sessionCloses.status === 'pass' ? 'pass' : 'fail',
|
|
191
|
+
orphan: orphanSessions,
|
|
192
|
+
close_pending: sessionCloses.pending,
|
|
193
|
+
close_intents: sessionCloses.items,
|
|
194
|
+
},
|
|
94
195
|
change_artifacts: { status: activeMissing === 0 ? 'pass' : 'fail', missing: activeMissing },
|
|
196
|
+
artifacts: artifactHealth,
|
|
197
|
+
task_ledger: taskLedgerHealth,
|
|
198
|
+
plan_publications: planPublications,
|
|
95
199
|
archive_recovery: {
|
|
96
200
|
status: archiveIntents.length === 0 ? 'pass' : 'fail',
|
|
97
201
|
pending: archiveIntents.length, corrupt: corruptArchiveIntents,
|
|
@@ -133,24 +237,308 @@ function inspectSystem(db, { rootDir = process.cwd() } = {}) {
|
|
|
133
237
|
};
|
|
134
238
|
}
|
|
135
239
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
240
|
+
function statIdentity(stat) {
|
|
241
|
+
return { dev: String(stat.dev), ino: String(stat.ino) };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function sameIdentity(left, right) {
|
|
245
|
+
return String(left?.dev) === String(right?.dev)
|
|
246
|
+
&& String(left?.ino) === String(right?.ino);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function backupWorkerPath() {
|
|
250
|
+
const runtimeRoot = process.env.UBP_RUNTIME_ROOT
|
|
251
|
+
? path.resolve(process.env.UBP_RUNTIME_ROOT)
|
|
252
|
+
: null;
|
|
253
|
+
const candidate = runtimeRoot
|
|
254
|
+
? path.join(runtimeRoot, 'runtime', BACKUP_WORKER_FILENAME)
|
|
255
|
+
: path.join(__dirname, BACKUP_WORKER_FILENAME);
|
|
256
|
+
const stat = fs.lstatSync(candidate, { throwIfNoEntry: false });
|
|
257
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile()) {
|
|
258
|
+
throw new DoctorError(
|
|
259
|
+
'BACKUP_FAILED',
|
|
260
|
+
`Doctor backup worker is missing or unsafe: ${candidate}`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
return candidate;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function canonicalStateDb(db) {
|
|
267
|
+
const main = db.pragma('database_list').find((row) => row.name === 'main');
|
|
268
|
+
const sourcePath = path.resolve(String(main?.file || ''));
|
|
269
|
+
const stat = fs.lstatSync(sourcePath, {
|
|
270
|
+
bigint: true,
|
|
271
|
+
throwIfNoEntry: false,
|
|
272
|
+
});
|
|
273
|
+
if (!main?.file || !stat || stat.isSymbolicLink() || !stat.isFile()) {
|
|
274
|
+
throw new DoctorError(
|
|
275
|
+
'BACKUP_FAILED',
|
|
276
|
+
`canonical state database is missing or unsafe: ${sourcePath}`,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
return { sourcePath, sourceIdentity: statIdentity(stat) };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function decodeBackupWorker(result, expected) {
|
|
283
|
+
if (result.error) {
|
|
284
|
+
throw new DoctorError(
|
|
285
|
+
'BACKUP_FAILED',
|
|
286
|
+
`cannot start Doctor backup worker: ${result.error.message}`,
|
|
287
|
+
{ cause: result.error },
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
let payload;
|
|
291
|
+
try {
|
|
292
|
+
payload = JSON.parse(String(result.stdout || '').trim());
|
|
293
|
+
} catch (cause) {
|
|
294
|
+
throw new DoctorError(
|
|
295
|
+
'BACKUP_FAILED',
|
|
296
|
+
`Doctor backup worker returned invalid output: ${String(result.stderr || '').trim()}`,
|
|
297
|
+
{ cause },
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
if (!payload?.ok) {
|
|
301
|
+
throw new DoctorError(
|
|
302
|
+
'BACKUP_FAILED',
|
|
303
|
+
payload?.error?.message || 'Doctor backup worker failed',
|
|
304
|
+
{ details: payload?.error },
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const value = payload.result;
|
|
308
|
+
const valid = result.status === 0
|
|
309
|
+
&& value && typeof value === 'object' && !Array.isArray(value)
|
|
310
|
+
&& value.output_name === expected.outputName
|
|
311
|
+
&& value.integrity === 'ok'
|
|
312
|
+
&& value.method === BACKUP_WORKER_METHOD
|
|
313
|
+
&& Number.isSafeInteger(value.size) && value.size > 0
|
|
314
|
+
&& /^[0-9a-f]{64}$/.test(value.digest || '')
|
|
315
|
+
&& sameIdentity(value.directory_identity, expected.directoryIdentity)
|
|
316
|
+
&& sameIdentity(value.source_identity, expected.sourceIdentity)
|
|
317
|
+
&& value.backup_identity && typeof value.backup_identity === 'object';
|
|
318
|
+
if (!valid) {
|
|
319
|
+
throw new DoctorError(
|
|
320
|
+
'BACKUP_FAILED',
|
|
321
|
+
'Doctor backup worker returned an invalid success payload',
|
|
322
|
+
{ details: { status: result.status, payload } },
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
return value;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function verifyPublishedBackup(backupDir, backupPath, expected) {
|
|
329
|
+
const directory = fs.lstatSync(backupDir, {
|
|
330
|
+
bigint: true,
|
|
331
|
+
throwIfNoEntry: false,
|
|
332
|
+
});
|
|
333
|
+
if (!directory || directory.isSymbolicLink() || !directory.isDirectory()
|
|
334
|
+
|| !sameIdentity(statIdentity(directory), expected.directoryIdentity)) {
|
|
335
|
+
throw new DoctorError(
|
|
336
|
+
'BACKUP_FAILED',
|
|
337
|
+
'Doctor backup directory changed before backup publication could be verified',
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
if (!Number.isInteger(fs.constants.O_NOFOLLOW) || fs.constants.O_NOFOLLOW === 0) {
|
|
341
|
+
throw new DoctorError(
|
|
342
|
+
'BACKUP_FAILED',
|
|
343
|
+
'Doctor cannot verify backups without no-follow file support on this platform',
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
let backupFd;
|
|
347
|
+
try {
|
|
348
|
+
backupFd = fs.openSync(
|
|
349
|
+
backupPath,
|
|
350
|
+
fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW,
|
|
351
|
+
);
|
|
352
|
+
} catch (cause) {
|
|
353
|
+
throw new DoctorError(
|
|
354
|
+
'BACKUP_FAILED',
|
|
355
|
+
'Doctor backup publication is missing or unsafe',
|
|
356
|
+
{ cause },
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
let backup;
|
|
360
|
+
let digest;
|
|
361
|
+
try {
|
|
362
|
+
backup = fs.fstatSync(backupFd, { bigint: true });
|
|
363
|
+
if (!backup.isFile()
|
|
364
|
+
|| !sameIdentity(statIdentity(backup), expected.backupIdentity)) {
|
|
365
|
+
throw new DoctorError(
|
|
366
|
+
'BACKUP_FAILED',
|
|
367
|
+
'Doctor backup publication does not match the worker-pinned file',
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
digest = crypto.createHash('sha256').update(fs.readFileSync(backupFd)).digest('hex');
|
|
371
|
+
} finally {
|
|
372
|
+
fs.closeSync(backupFd);
|
|
373
|
+
}
|
|
374
|
+
if (digest !== expected.digest || Number(backup.size) !== expected.size) {
|
|
375
|
+
throw new DoctorError(
|
|
376
|
+
'BACKUP_FAILED',
|
|
377
|
+
'Doctor backup publication failed post-worker verification',
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function backupDatabase(db, rootDir, { beforeWorker = null } = {}) {
|
|
383
|
+
const backupDir = runtimePaths.pathsFor(rootDir).backupsDir;
|
|
384
|
+
const outputName = `state-${timestamp()}-${crypto.randomUUID()}.db`;
|
|
385
|
+
const backupPath = path.join(backupDir, outputName);
|
|
386
|
+
try {
|
|
387
|
+
runtimePaths.validateProjectLayout(rootDir, {
|
|
388
|
+
env: {},
|
|
389
|
+
validateRuntimeTree: true,
|
|
390
|
+
});
|
|
391
|
+
fs.mkdirSync(backupDir, { recursive: true, mode: 0o700 });
|
|
392
|
+
runtimePaths.assertManagedBackupDestination(rootDir, backupPath);
|
|
393
|
+
const backupDirectory = fs.lstatSync(backupDir, { bigint: true });
|
|
394
|
+
if (backupDirectory.isSymbolicLink() || !backupDirectory.isDirectory()) {
|
|
395
|
+
throw new DoctorError('BACKUP_FAILED', `backup root is unsafe: ${backupDir}`);
|
|
396
|
+
}
|
|
397
|
+
const directoryIdentity = statIdentity(backupDirectory);
|
|
398
|
+
const { sourcePath, sourceIdentity } = canonicalStateDb(db);
|
|
399
|
+
if (beforeWorker) {
|
|
400
|
+
beforeWorker({
|
|
401
|
+
backupDir,
|
|
402
|
+
backupPath,
|
|
403
|
+
directoryIdentity,
|
|
404
|
+
sourcePath,
|
|
405
|
+
sourceIdentity,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
const result = childProcess.spawnSync(
|
|
409
|
+
process.execPath,
|
|
410
|
+
[backupWorkerPath()],
|
|
411
|
+
{
|
|
412
|
+
cwd: backupDir,
|
|
413
|
+
input: JSON.stringify({
|
|
414
|
+
directory_identity: directoryIdentity,
|
|
415
|
+
source_path: sourcePath,
|
|
416
|
+
source_identity: sourceIdentity,
|
|
417
|
+
output_name: outputName,
|
|
418
|
+
}),
|
|
419
|
+
encoding: 'utf8',
|
|
420
|
+
maxBuffer: 1024 * 1024,
|
|
421
|
+
timeout: BACKUP_WORKER_TIMEOUT_MS,
|
|
422
|
+
windowsHide: true,
|
|
423
|
+
},
|
|
424
|
+
);
|
|
425
|
+
const published = decodeBackupWorker(result, {
|
|
426
|
+
outputName,
|
|
427
|
+
directoryIdentity,
|
|
428
|
+
sourceIdentity,
|
|
429
|
+
});
|
|
430
|
+
verifyPublishedBackup(backupDir, backupPath, {
|
|
431
|
+
directoryIdentity,
|
|
432
|
+
backupIdentity: published.backup_identity,
|
|
433
|
+
digest: published.digest,
|
|
434
|
+
size: published.size,
|
|
435
|
+
});
|
|
436
|
+
return backupPath;
|
|
437
|
+
} catch (error) {
|
|
438
|
+
if (error instanceof DoctorError) throw error;
|
|
439
|
+
throw new DoctorError(
|
|
440
|
+
'BACKUP_FAILED',
|
|
441
|
+
`state.db backup failed: ${error.message}`,
|
|
442
|
+
{ cause: error },
|
|
443
|
+
);
|
|
444
|
+
}
|
|
143
445
|
}
|
|
144
446
|
|
|
145
447
|
async function runDoctor(db, {
|
|
146
448
|
rootDir = process.cwd(), repair = false, project = projector.projectAll,
|
|
147
449
|
projectionStaleAfterMs = 5 * 60 * 1000,
|
|
450
|
+
_beforeBackupWorker = null,
|
|
148
451
|
} = {}) {
|
|
149
452
|
if (!repair) return { ...inspectSystem(db, { rootDir }), repair_performed: false };
|
|
150
|
-
const backupPath = await backupDatabase(db, rootDir
|
|
453
|
+
const backupPath = await backupDatabase(db, rootDir, {
|
|
454
|
+
beforeWorker: _beforeBackupWorker,
|
|
455
|
+
});
|
|
456
|
+
const recovered = recovery.recoverOnBoot(db, { repoRoot: rootDir });
|
|
457
|
+
if (recovered.close_pending.length > 0) {
|
|
458
|
+
const deferred = {
|
|
459
|
+
status: 'deferred',
|
|
460
|
+
code: 'SESSION_CLOSE_PENDING',
|
|
461
|
+
pending: recovered.close_pending,
|
|
462
|
+
};
|
|
463
|
+
return {
|
|
464
|
+
...inspectSystem(db, { rootDir }),
|
|
465
|
+
repair_performed: true,
|
|
466
|
+
backup_path: backupPath,
|
|
467
|
+
repair: {
|
|
468
|
+
close_intents: recovered,
|
|
469
|
+
recovered,
|
|
470
|
+
plan_publications: deferred,
|
|
471
|
+
archives: deferred,
|
|
472
|
+
workflows: deferred,
|
|
473
|
+
},
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
const planPublications = planStore.recoverPlanPublications(db, { rootDir });
|
|
477
|
+
if (planPublications.pending > 0 || planPublications.issues.length > 0) {
|
|
478
|
+
const blocked = planPublications.issues.length > 0;
|
|
479
|
+
const planGate = {
|
|
480
|
+
...planPublications,
|
|
481
|
+
status: blocked ? 'blocked' : 'deferred',
|
|
482
|
+
code: 'PLAN_RECOVERY_REQUIRED',
|
|
483
|
+
};
|
|
484
|
+
const deferred = {
|
|
485
|
+
status: 'deferred',
|
|
486
|
+
code: 'PLAN_RECOVERY_REQUIRED',
|
|
487
|
+
blocked_by: 'plan_publications',
|
|
488
|
+
};
|
|
489
|
+
return {
|
|
490
|
+
...inspectSystem(db, { rootDir }),
|
|
491
|
+
status: 'degraded',
|
|
492
|
+
repair_performed: true,
|
|
493
|
+
backup_path: backupPath,
|
|
494
|
+
repair: {
|
|
495
|
+
close_intents: recovered,
|
|
496
|
+
recovered,
|
|
497
|
+
plan_publications: planGate,
|
|
498
|
+
archives: deferred,
|
|
499
|
+
workflows: deferred,
|
|
500
|
+
spec_events: deferred,
|
|
501
|
+
interrupted: deferred,
|
|
502
|
+
requeued: deferred,
|
|
503
|
+
ensured: deferred,
|
|
504
|
+
projections: deferred,
|
|
505
|
+
},
|
|
506
|
+
};
|
|
507
|
+
}
|
|
151
508
|
const archives = changes.recoverInterruptedArchives(db, { rootDir });
|
|
509
|
+
const unresolvedArchives = archiveJournal.listArchiveIntents(rootDir);
|
|
510
|
+
if (archives.failed > 0 || unresolvedArchives.length > 0) {
|
|
511
|
+
const archiveGate = {
|
|
512
|
+
...archives,
|
|
513
|
+
pending: unresolvedArchives.length,
|
|
514
|
+
status: 'blocked',
|
|
515
|
+
code: 'ARCHIVE_RECOVERY_REQUIRED',
|
|
516
|
+
};
|
|
517
|
+
const deferred = {
|
|
518
|
+
status: 'deferred',
|
|
519
|
+
code: 'ARCHIVE_RECOVERY_REQUIRED',
|
|
520
|
+
blocked_by: 'archives',
|
|
521
|
+
};
|
|
522
|
+
return {
|
|
523
|
+
...inspectSystem(db, { rootDir }),
|
|
524
|
+
status: 'degraded',
|
|
525
|
+
repair_performed: true,
|
|
526
|
+
backup_path: backupPath,
|
|
527
|
+
repair: {
|
|
528
|
+
close_intents: recovered,
|
|
529
|
+
recovered,
|
|
530
|
+
plan_publications: planPublications,
|
|
531
|
+
archives: archiveGate,
|
|
532
|
+
workflows: deferred,
|
|
533
|
+
spec_events: deferred,
|
|
534
|
+
interrupted: deferred,
|
|
535
|
+
requeued: deferred,
|
|
536
|
+
ensured: deferred,
|
|
537
|
+
projections: deferred,
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
}
|
|
152
541
|
const workflowRecovery = workflows.recoverUntrackedChangeWorkflows(db, { rootDir });
|
|
153
|
-
const recovered = recovery.recoverOnBoot(db);
|
|
154
542
|
const cursor = runtime.readConsumerCursor(db, SPEC_CONSUMER);
|
|
155
543
|
const consumed = ops.consumeSpecChangedEvents(db, { since_id: cursor, limit: 500 });
|
|
156
544
|
runtime.writeConsumerCursor(db, SPEC_CONSUMER, consumed.next_since_id);
|
|
@@ -165,6 +553,8 @@ async function runDoctor(db, {
|
|
|
165
553
|
repair_performed: true,
|
|
166
554
|
backup_path: backupPath,
|
|
167
555
|
repair: {
|
|
556
|
+
close_intents: recovered,
|
|
557
|
+
plan_publications: planPublications,
|
|
168
558
|
archives, workflows: workflowRecovery, recovered, consumed,
|
|
169
559
|
interrupted, requeued, ensured, projections,
|
|
170
560
|
},
|