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