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.
- package/.ultra-template/contexts/TEMPLATE.md +87 -0
- package/.ultra-template/decisions/.gitkeep +1 -0
- package/.ultra-template/evidence/.gitkeep +1 -0
- package/.ultra-template/north-star.md +46 -0
- package/.ultra-template/research/.gitkeep +1 -0
- package/.ultra-template/specs/discovery.md +2 -2
- package/.ultra-template/tasks.json +3 -0
- package/.ultra-template/test-report.json +27 -0
- package/CHANGELOG.md +49 -0
- package/README.md +217 -579
- package/adapters/_shared/delegate-profiles/kimi-read-only.md +15 -0
- package/adapters/_shared/delegate-profiles/kimi-write.md +17 -0
- package/adapters/_shared/file-ops.cjs +76 -2
- package/adapters/_shared/host-profile.cjs +97 -0
- package/adapters/_shared/plugin-core.cjs +204 -0
- package/adapters/_shared/runtime-assets.cjs +31 -96
- package/adapters/claude.js +92 -291
- package/adapters/codex.js +309 -842
- package/adapters/grok.js +143 -1469
- package/adapters/kimi.js +124 -1053
- package/adapters/opencode.js +230 -742
- package/bin/delegate-worker.cjs +323 -0
- package/bin/delegate.cjs +456 -0
- package/bin/install.js +38 -11
- package/docs/ARCHITECTURE.md +122 -216
- package/docs/ARTIFACT-AUTHORITY.md +90 -268
- package/docs/DECISIONS.md +75 -87
- package/docs/PHILOSOPHY.md +177 -0
- package/docs/PLUGIN-ISOLATION-CONTRACT.md +62 -39
- package/docs/RUNTIME-COMPAT-MATRIX.md +99 -156
- package/docs/SKILL-AUTHORING.md +94 -73
- package/docs/WORKFLOW-LIFECYCLE.md +93 -189
- package/hooks/README.md +17 -37
- package/hooks/_common.py +113 -0
- package/hooks/adapters/codex.py +9 -16
- package/hooks/adapters/grok.py +5 -5
- package/hooks/adapters/kimi.py +8 -14
- package/hooks/block_dangerous_commands.py +95 -0
- package/hooks/compact_context.py +65 -0
- package/hooks/mid_workflow_recall.py +25 -0
- package/hooks/post_edit_guard.py +105 -0
- package/hooks/session_context.py +30 -0
- package/package.json +17 -52
- package/skills/ultra-change/SKILL.md +97 -50
- package/skills/ultra-change/references/change-contract.md +40 -1
- package/skills/ultra-delegate/SKILL.md +82 -0
- package/skills/ultra-delegate/references/delegation-contract.md +51 -0
- package/skills/ultra-delegate/scripts/delegate_wait.py +86 -0
- package/skills/ultra-deliver/SKILL.md +61 -42
- package/skills/ultra-deliver/references/baseline-reconciliation.md +39 -20
- package/skills/ultra-dev/SKILL.md +96 -62
- package/skills/ultra-dev/references/debugging.md +22 -0
- package/skills/ultra-domain-modeling/SKILL.md +79 -0
- package/skills/ultra-grilling/SKILL.md +79 -0
- package/skills/ultra-init/SKILL.md +94 -37
- package/skills/ultra-init/references/brownfield-adoption.md +33 -83
- package/skills/ultra-init/scripts/init_project.cjs +84 -0
- package/skills/ultra-plan/SKILL.md +88 -68
- package/skills/ultra-research/SKILL.md +62 -48
- package/skills/ultra-research/references/00-problem-validation.md +5 -5
- package/skills/ultra-research/references/01-opportunity-discovery.md +3 -3
- package/skills/ultra-research/references/02-market-assessment.md +5 -4
- package/skills/ultra-research/references/03-competitive-landscape.md +3 -3
- package/skills/ultra-research/references/04-product-strategy.md +3 -3
- package/skills/ultra-research/references/05-assumptions-validation.md +5 -5
- package/skills/ultra-research/references/10-user-personas.md +3 -2
- package/skills/ultra-research/references/11-user-scenarios.md +3 -3
- package/skills/ultra-research/references/20-user-stories.md +4 -3
- package/skills/ultra-research/references/21-features-scope.md +3 -2
- package/skills/ultra-research/references/22-success-metrics.md +3 -3
- package/skills/ultra-research/references/30-architecture-context.md +3 -3
- package/skills/ultra-research/references/31-solution-strategy.md +3 -3
- package/skills/ultra-research/references/32-building-blocks.md +3 -3
- package/skills/ultra-research/references/40-deployment.md +3 -3
- package/skills/ultra-research/references/41-quality-risks.md +3 -3
- package/skills/ultra-research/references/99-synthesis.md +7 -7
- package/skills/ultra-review/SKILL.md +59 -32
- package/skills/ultra-review/references/code.md +17 -0
- package/skills/ultra-review/references/comments.md +12 -0
- package/skills/ultra-review/references/design.md +12 -0
- package/skills/ultra-review/references/errors.md +13 -0
- package/skills/ultra-review/references/spec.md +13 -0
- package/skills/ultra-review/references/tests.md +12 -0
- package/skills/ultra-review/references/worker-packet.md +40 -0
- package/skills/ultra-review/scripts/review_wait.py +26 -15
- package/skills/ultra-status/SKILL.md +61 -36
- package/skills/ultra-tdd/SKILL.md +81 -0
- package/skills/ultra-tdd/references/templates/README.md +20 -0
- package/skills/ultra-tdd/references/templates/feature-flag-default-audit.sh +79 -0
- package/skills/ultra-tdd/references/templates/persistence-real.ts +78 -0
- package/skills/ultra-tdd/references/templates/testcontainer-postgres.py +78 -0
- package/skills/ultra-tdd/references/templates/testcontainer-postgres.ts +73 -0
- package/skills/ultra-tdd/references/templates/vertical-slice.ts +66 -0
- package/skills/ultra-tdd/references/test-execution.md +18 -0
- package/skills/ultra-test/SKILL.md +70 -30
- package/skills/ultra-test/references/export-syntax.md +18 -0
- package/skills/ultra-test/scripts/worktree_digest.cjs +54 -0
- package/skills/ultra-think/SKILL.md +49 -27
- package/skills/ultra-think/references/autonomy-boundary.md +48 -0
- package/skills/ultra-think/references/interaction-boundary.md +18 -33
- package/.ultra-template/docs/research/README.md +0 -55
- package/.ultra-template/reports/templates/delivery-report.json +0 -17
- package/.ultra-template/reports/templates/test-report.json +0 -18
- package/.ultra-template/tasks/tasks.json +0 -15
- package/.ultra-template/templates/task-context.md +0 -19
- package/adapters/_shared/codex-assets.cjs +0 -725
- package/adapters/_shared/codex-ultra-tools-entry.cjs +0 -11
- package/adapters/_shared/interaction-contract.cjs +0 -129
- package/adapters/_shared/path-rewrite.cjs +0 -79
- package/adapters/_shared/runtime-native.cjs +0 -132
- package/adapters/_shared/settings-merge.cjs +0 -76
- package/agents/code-reviewer.md +0 -42
- package/agents/debugger.md +0 -40
- package/agents/review-code.md +0 -49
- package/agents/review-comments.md +0 -38
- package/agents/review-coordinator.md +0 -56
- package/agents/review-design.md +0 -40
- package/agents/review-errors.md +0 -43
- package/agents/review-spec.md +0 -39
- package/agents/review-tests.md +0 -40
- package/agents/tdd-runner.md +0 -38
- package/bin/orchestrator.js +0 -383
- package/commands/ultra-change.md +0 -10
- package/commands/ultra-deliver.md +0 -10
- package/commands/ultra-dev.md +0 -10
- package/commands/ultra-doctor.md +0 -10
- package/commands/ultra-init.md +0 -10
- package/commands/ultra-plan.md +0 -10
- package/commands/ultra-research.md +0 -10
- package/commands/ultra-review.md +0 -10
- package/commands/ultra-status.md +0 -10
- package/commands/ultra-test.md +0 -10
- package/commands/ultra-think.md +0 -10
- package/docs/AGENT-CONTEXT.md +0 -191
- package/docs/COMMIT-HASH-BACKFILL.md +0 -51
- package/docs/LEGACY-CLI-CROSSWALK.md +0 -175
- package/docs/ROADMAP.md +0 -101
- package/docs/STATE-DB-ACCESS-POLICY.md +0 -161
- package/hooks/active_task_context.py +0 -152
- package/hooks/context_envelope.py +0 -100
- package/hooks/health_check.py +0 -196
- package/hooks/pre_stop_check.py +0 -66
- package/hooks/runtime_paths.py +0 -396
- package/hooks/subagent_tracker.py +0 -88
- package/hooks/workflow_checkpoint.py +0 -76
- package/hooks/workflow_context.py +0 -48
- package/hooks/workflow_resume.py +0 -84
- package/mcp-server/hook-context.cjs +0 -38
- package/mcp-server/hook-event.cjs +0 -34
- package/mcp-server/lib/archive-journal.cjs +0 -1119
- package/mcp-server/lib/archive-mutation-worker.py +0 -365
- package/mcp-server/lib/artifact-registry.cjs +0 -1232
- package/mcp-server/lib/baseline-workflow.cjs +0 -1193
- package/mcp-server/lib/canonical-json.cjs +0 -19
- package/mcp-server/lib/change-authority.cjs +0 -26
- package/mcp-server/lib/change-packet.cjs +0 -790
- package/mcp-server/lib/change-workflow.cjs +0 -65
- package/mcp-server/lib/context-envelope.cjs +0 -581
- package/mcp-server/lib/context-paths.cjs +0 -142
- package/mcp-server/lib/decision-records.cjs +0 -423
- package/mcp-server/lib/delivery-transaction.cjs +0 -471
- package/mcp-server/lib/doctor-backup-worker.cjs +0 -328
- package/mcp-server/lib/doctor.cjs +0 -627
- package/mcp-server/lib/git-bootstrap.cjs +0 -651
- package/mcp-server/lib/init-project.cjs +0 -1021
- package/mcp-server/lib/kernel-change-workflow.cjs +0 -2029
- package/mcp-server/lib/legacy-change-workflow.cjs +0 -3032
- package/mcp-server/lib/managed-file-write.cjs +0 -172
- package/mcp-server/lib/plan-checkpoint.cjs +0 -177
- package/mcp-server/lib/plan-publication-worker.cjs +0 -830
- package/mcp-server/lib/plan-store.cjs +0 -794
- package/mcp-server/lib/prd-parser.cjs +0 -83
- package/mcp-server/lib/pricing.cjs +0 -44
- package/mcp-server/lib/project-context.cjs +0 -178
- package/mcp-server/lib/projector.cjs +0 -386
- package/mcp-server/lib/provider-refs.cjs +0 -42
- package/mcp-server/lib/runtime-paths.cjs +0 -1687
- package/mcp-server/lib/runtime-state.cjs +0 -222
- package/mcp-server/lib/safe-project-file.cjs +0 -385
- package/mcp-server/lib/stage-checkpoints.cjs +0 -463
- package/mcp-server/lib/state-authority.cjs +0 -95
- package/mcp-server/lib/state-db.cjs +0 -1580
- package/mcp-server/lib/state-ops.cjs +0 -1428
- package/mcp-server/lib/task-contract.cjs +0 -404
- package/mcp-server/lib/task-expander.cjs +0 -105
- package/mcp-server/lib/task-ledger.cjs +0 -1964
- package/mcp-server/lib/telemetry.cjs +0 -85
- package/mcp-server/lib/topo.cjs +0 -131
- package/mcp-server/lib/ultra-facade.cjs +0 -2684
- package/mcp-server/lib/worker-packet.cjs +0 -358
- package/mcp-server/server.cjs +0 -437
- package/orchestrator/auto-merge.cjs +0 -128
- package/orchestrator/daemon.cjs +0 -264
- package/orchestrator/dispatch-rules.cjs +0 -78
- package/orchestrator/parallel-orchestrator.cjs +0 -903
- package/orchestrator/planner/plan-builder.cjs +0 -125
- package/orchestrator/recovery.cjs +0 -188
- package/orchestrator/session-close-journal-worker.cjs +0 -5
- package/orchestrator/session-close-journal.cjs +0 -1014
- package/orchestrator/session-runner.cjs +0 -1153
- package/orchestrator/worktree-manager.cjs +0 -282
- package/settings.json +0 -7
- package/skills/cc-collab/SKILL.md +0 -42
- package/skills/code-review-expert/SKILL.md +0 -43
- package/skills/code-review-expert/references/correctness-reliability.md +0 -30
- package/skills/code-review-expert/references/design-boundaries.md +0 -26
- package/skills/code-review-expert/references/removal-plan.md +0 -24
- package/skills/codex-collab/SKILL.md +0 -49
- package/skills/codex-collab/evals/evals.json +0 -35
- package/skills/integration-rules/SKILL.md +0 -32
- package/skills/security-rules/SKILL.md +0 -38
- package/skills/testing-rules/SKILL.md +0 -32
- package/skills/ultra-doctor/SKILL.md +0 -41
- package/skills/ultra-plan/references/semantic-preflight.md +0 -20
- package/skills/ultra-verify/SKILL.md +0 -46
- package/skills/ultra-verify/evals/evals.json +0 -46
- package/skills/ultra-verify/references/cross-verify-modes.md +0 -28
- package/skills/ultra-verify/references/evidence-status.md +0 -21
- package/skills/ultra-verify/scripts/verify_wait.py +0 -94
- package/spec/cli-protocol.md +0 -102
- package/spec/mcp-tools.yaml +0 -312
- package/spec/schemas/baseline-reconciliation.v1.schema.json +0 -73
- package/spec/schemas/change-delta.v1.schema.json +0 -153
- package/spec/schemas/command-manifest.schema.json +0 -29
- package/spec/schemas/context-file.v4.5.schema.json +0 -54
- package/spec/schemas/delivery-report.v1.schema.json +0 -66
- package/spec/schemas/documentation-reconciliation.v1.schema.json +0 -106
- package/spec/schemas/mcp-tools.schema.json +0 -136
- package/spec/schemas/skill-manifest.schema.json +0 -24
- package/spec/schemas/state-db.sql +0 -641
- package/spec/schemas/task-ledger.v2.schema.json +0 -296
- package/spec/schemas/tasks.v4.5.schema.json +0 -96
- package/spec/schemas/test-report.v1.schema.json +0 -225
- package/ultra-tools/cli.cjs +0 -129
- package/ultra-tools/commands/db.cjs +0 -226
- package/ultra-tools/commands/legacy-memory.cjs +0 -159
- package/ultra-tools/commands/migrate.cjs +0 -1386
- package/ultra-tools/commands/session.cjs +0 -213
- package/ultra-tools/commands/status.cjs +0 -559
- package/ultra-tools/commands/system.cjs +0 -498
- package/ultra-tools/commands/task.cjs +0 -105
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const crypto = require('node:crypto');
|
|
4
|
-
const fs = require('node:fs');
|
|
5
|
-
const path = require('node:path');
|
|
6
|
-
const Database = require('better-sqlite3');
|
|
7
|
-
|
|
8
|
-
const MAX_INPUT_BYTES = 64 * 1024;
|
|
9
|
-
const OUTPUT_PATTERN = /^state-[A-Za-z0-9-]+-[0-9a-f-]{36}\.db$/i;
|
|
10
|
-
const BACKUP_METHOD = 'sqlite-serialize-pinned-fd-v1';
|
|
11
|
-
const SQLITE_HEADER = Buffer.from('SQLite format 3\0', 'ascii');
|
|
12
|
-
|
|
13
|
-
class BackupWorkerError extends Error {
|
|
14
|
-
constructor(code, message, details = undefined) {
|
|
15
|
-
super(message);
|
|
16
|
-
this.code = code;
|
|
17
|
-
if (details !== undefined) this.details = details;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function identity(stat) {
|
|
22
|
-
return { dev: String(stat.dev), ino: String(stat.ino) };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function sameIdentity(left, right) {
|
|
26
|
-
return String(left?.dev) === String(right?.dev)
|
|
27
|
-
&& String(left?.ino) === String(right?.ino);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function lstatRegular(file, code, label) {
|
|
31
|
-
const stat = fs.lstatSync(file, {
|
|
32
|
-
bigint: true,
|
|
33
|
-
throwIfNoEntry: false,
|
|
34
|
-
});
|
|
35
|
-
if (!stat || stat.isSymbolicLink() || !stat.isFile()) {
|
|
36
|
-
throw new BackupWorkerError(code, `${label} must be a regular file: ${file}`);
|
|
37
|
-
}
|
|
38
|
-
return stat;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function validatePayload(payload) {
|
|
42
|
-
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
43
|
-
throw new BackupWorkerError(
|
|
44
|
-
'BACKUP_WORKER_PROTOCOL_INVALID',
|
|
45
|
-
'backup worker payload must be an object',
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
const sourcePath = payload.source_path;
|
|
49
|
-
const outputName = payload.output_name;
|
|
50
|
-
if (typeof sourcePath !== 'string' || !path.isAbsolute(sourcePath)) {
|
|
51
|
-
throw new BackupWorkerError(
|
|
52
|
-
'BACKUP_WORKER_PROTOCOL_INVALID',
|
|
53
|
-
'source_path must be absolute',
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
if (typeof outputName !== 'string' || !OUTPUT_PATTERN.test(outputName)
|
|
57
|
-
|| path.basename(outputName) !== outputName) {
|
|
58
|
-
throw new BackupWorkerError(
|
|
59
|
-
'BACKUP_WORKER_PROTOCOL_INVALID',
|
|
60
|
-
'output_name is invalid',
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
if (!payload.directory_identity || !payload.source_identity) {
|
|
64
|
-
throw new BackupWorkerError(
|
|
65
|
-
'BACKUP_WORKER_PROTOCOL_INVALID',
|
|
66
|
-
'directory and source identities are required',
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
return {
|
|
70
|
-
sourcePath,
|
|
71
|
-
outputName,
|
|
72
|
-
directoryIdentity: payload.directory_identity,
|
|
73
|
-
sourceIdentity: payload.source_identity,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function assertCurrentDirectory(expected) {
|
|
78
|
-
const stat = fs.statSync('.', { bigint: true });
|
|
79
|
-
if (!stat.isDirectory() || !sameIdentity(identity(stat), expected)) {
|
|
80
|
-
throw new BackupWorkerError(
|
|
81
|
-
'BACKUP_DIRECTORY_CHANGED',
|
|
82
|
-
'backup worker cwd no longer matches the verified backup directory',
|
|
83
|
-
{ expected, actual: identity(stat) },
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function fsyncCurrentDirectory() {
|
|
89
|
-
const fd = fs.openSync('.', fs.constants.O_RDONLY);
|
|
90
|
-
try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function cleanupOwnedTemp(file, expectedIdentity) {
|
|
94
|
-
const stat = fs.lstatSync(file, {
|
|
95
|
-
bigint: true,
|
|
96
|
-
throwIfNoEntry: false,
|
|
97
|
-
});
|
|
98
|
-
if (!stat || stat.isSymbolicLink() || !stat.isFile()) return;
|
|
99
|
-
if (expectedIdentity && !sameIdentity(identity(stat), expectedIdentity)) return;
|
|
100
|
-
fs.unlinkSync(file);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function noFollowFlag() {
|
|
104
|
-
if (!Number.isInteger(fs.constants.O_NOFOLLOW) || fs.constants.O_NOFOLLOW === 0) {
|
|
105
|
-
throw new BackupWorkerError(
|
|
106
|
-
'BACKUP_CAPABILITY_UNAVAILABLE',
|
|
107
|
-
'this platform cannot pin backup files without following symbolic links',
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
return fs.constants.O_NOFOLLOW;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function sqliteSnapshot(source) {
|
|
114
|
-
if (typeof source.serialize !== 'function') {
|
|
115
|
-
throw new BackupWorkerError(
|
|
116
|
-
'BACKUP_CAPABILITY_UNAVAILABLE',
|
|
117
|
-
'the SQLite runtime does not expose a serialized snapshot capability',
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
let snapshot;
|
|
121
|
-
try {
|
|
122
|
-
snapshot = source.serialize();
|
|
123
|
-
} catch (cause) {
|
|
124
|
-
throw new BackupWorkerError(
|
|
125
|
-
'BACKUP_SNAPSHOT_FAILED',
|
|
126
|
-
`SQLite could not create a coherent serialized snapshot: ${cause.message}`,
|
|
127
|
-
{ cause: { code: cause.code, message: cause.message } },
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
if (!Buffer.isBuffer(snapshot) || snapshot.length < 100
|
|
131
|
-
|| !snapshot.subarray(0, SQLITE_HEADER.length).equals(SQLITE_HEADER)) {
|
|
132
|
-
throw new BackupWorkerError(
|
|
133
|
-
'BACKUP_SNAPSHOT_FAILED',
|
|
134
|
-
'SQLite returned an invalid serialized snapshot',
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
return snapshot;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function verifySnapshot(snapshot) {
|
|
141
|
-
const verificationBytes = Buffer.from(snapshot);
|
|
142
|
-
// An in-memory SQLite database cannot retain WAL journal mode. The serialized
|
|
143
|
-
// pages already contain the connection's coherent view, so only the verifier
|
|
144
|
-
// copy uses rollback-journal header flags. Published bytes remain unchanged.
|
|
145
|
-
verificationBytes[18] = 1;
|
|
146
|
-
verificationBytes[19] = 1;
|
|
147
|
-
let verification;
|
|
148
|
-
try {
|
|
149
|
-
verification = new Database(verificationBytes);
|
|
150
|
-
verification.pragma('query_only = ON');
|
|
151
|
-
const integrity = verification.pragma('quick_check', { simple: true });
|
|
152
|
-
const userVersion = Number(verification.pragma('user_version', { simple: true }));
|
|
153
|
-
if (integrity !== 'ok') {
|
|
154
|
-
throw new BackupWorkerError(
|
|
155
|
-
'BACKUP_VERIFICATION_FAILED',
|
|
156
|
-
`SQLite backup quick_check failed: ${String(integrity)}`,
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
return { integrity, userVersion };
|
|
160
|
-
} catch (cause) {
|
|
161
|
-
if (cause instanceof BackupWorkerError) throw cause;
|
|
162
|
-
throw new BackupWorkerError(
|
|
163
|
-
'BACKUP_VERIFICATION_FAILED',
|
|
164
|
-
`SQLite could not verify the serialized snapshot: ${cause.message}`,
|
|
165
|
-
{ cause: { code: cause.code, message: cause.message } },
|
|
166
|
-
);
|
|
167
|
-
} finally {
|
|
168
|
-
if (verification) verification.close();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async function execute(payload, { beforeSnapshotWrite = null } = {}) {
|
|
173
|
-
const {
|
|
174
|
-
sourcePath,
|
|
175
|
-
outputName,
|
|
176
|
-
directoryIdentity,
|
|
177
|
-
sourceIdentity,
|
|
178
|
-
} = validatePayload(payload);
|
|
179
|
-
assertCurrentDirectory(directoryIdentity);
|
|
180
|
-
const sourceStat = lstatRegular(
|
|
181
|
-
sourcePath,
|
|
182
|
-
'BACKUP_SOURCE_CHANGED',
|
|
183
|
-
'canonical state database',
|
|
184
|
-
);
|
|
185
|
-
if (!sameIdentity(identity(sourceStat), sourceIdentity)) {
|
|
186
|
-
throw new BackupWorkerError(
|
|
187
|
-
'BACKUP_SOURCE_CHANGED',
|
|
188
|
-
'canonical state database identity changed before backup',
|
|
189
|
-
{ expected: sourceIdentity, actual: identity(sourceStat) },
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const tempName = `.${outputName}.${crypto.randomUUID()}.tmp`;
|
|
194
|
-
let tempFd = fs.openSync(
|
|
195
|
-
tempName,
|
|
196
|
-
fs.constants.O_RDWR
|
|
197
|
-
| fs.constants.O_CREAT
|
|
198
|
-
| fs.constants.O_EXCL
|
|
199
|
-
| noFollowFlag(),
|
|
200
|
-
0o600,
|
|
201
|
-
);
|
|
202
|
-
const tempIdentity = identity(fs.fstatSync(tempFd, { bigint: true }));
|
|
203
|
-
let source;
|
|
204
|
-
try {
|
|
205
|
-
source = new Database(sourcePath, {
|
|
206
|
-
readonly: true,
|
|
207
|
-
fileMustExist: true,
|
|
208
|
-
});
|
|
209
|
-
const openedSource = lstatRegular(
|
|
210
|
-
sourcePath,
|
|
211
|
-
'BACKUP_SOURCE_CHANGED',
|
|
212
|
-
'canonical state database',
|
|
213
|
-
);
|
|
214
|
-
if (!sameIdentity(identity(openedSource), sourceIdentity)) {
|
|
215
|
-
throw new BackupWorkerError(
|
|
216
|
-
'BACKUP_SOURCE_CHANGED',
|
|
217
|
-
'canonical state database identity changed while opening backup source',
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
source.pragma('query_only = ON');
|
|
221
|
-
const snapshot = sqliteSnapshot(source);
|
|
222
|
-
source.close();
|
|
223
|
-
source = null;
|
|
224
|
-
const { userVersion } = verifySnapshot(snapshot);
|
|
225
|
-
|
|
226
|
-
if (beforeSnapshotWrite) beforeSnapshotWrite({ tempName, tempIdentity });
|
|
227
|
-
fs.ftruncateSync(tempFd, 0);
|
|
228
|
-
fs.writeFileSync(tempFd, snapshot);
|
|
229
|
-
fs.fsyncSync(tempFd);
|
|
230
|
-
const written = fs.fstatSync(tempFd, { bigint: true });
|
|
231
|
-
if (!sameIdentity(identity(written), tempIdentity)
|
|
232
|
-
|| Number(written.size) !== snapshot.length) {
|
|
233
|
-
throw new BackupWorkerError(
|
|
234
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
235
|
-
'backup staging file identity or size changed during snapshot write',
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const completedTemp = lstatRegular(
|
|
240
|
-
tempName,
|
|
241
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
242
|
-
'backup staging file',
|
|
243
|
-
);
|
|
244
|
-
if (!sameIdentity(identity(completedTemp), tempIdentity)) {
|
|
245
|
-
throw new BackupWorkerError(
|
|
246
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
247
|
-
'backup staging file identity changed during backup',
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
if (Number(completedTemp.size) !== snapshot.length) {
|
|
251
|
-
throw new BackupWorkerError(
|
|
252
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
253
|
-
'backup staging file size changed before publication',
|
|
254
|
-
);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
fs.linkSync(tempName, outputName);
|
|
258
|
-
const published = lstatRegular(
|
|
259
|
-
outputName,
|
|
260
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
261
|
-
'published backup',
|
|
262
|
-
);
|
|
263
|
-
if (!sameIdentity(identity(published), tempIdentity)
|
|
264
|
-
|| Number(published.size) !== snapshot.length) {
|
|
265
|
-
throw new BackupWorkerError(
|
|
266
|
-
'BACKUP_OUTPUT_UNSAFE',
|
|
267
|
-
'published backup does not match the pinned staging file',
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
fs.unlinkSync(tempName);
|
|
271
|
-
fsyncCurrentDirectory();
|
|
272
|
-
fs.closeSync(tempFd);
|
|
273
|
-
tempFd = null;
|
|
274
|
-
return {
|
|
275
|
-
output_name: outputName,
|
|
276
|
-
integrity: 'ok',
|
|
277
|
-
user_version: userVersion,
|
|
278
|
-
size: Number(published.size),
|
|
279
|
-
digest: crypto.createHash('sha256').update(snapshot).digest('hex'),
|
|
280
|
-
method: BACKUP_METHOD,
|
|
281
|
-
directory_identity: directoryIdentity,
|
|
282
|
-
source_identity: sourceIdentity,
|
|
283
|
-
backup_identity: identity(published),
|
|
284
|
-
};
|
|
285
|
-
} catch (error) {
|
|
286
|
-
if (source) {
|
|
287
|
-
try { source.close(); } catch {}
|
|
288
|
-
}
|
|
289
|
-
if (tempFd !== null) {
|
|
290
|
-
try { fs.closeSync(tempFd); } catch {}
|
|
291
|
-
}
|
|
292
|
-
cleanupOwnedTemp(tempName, tempIdentity);
|
|
293
|
-
cleanupOwnedTemp(outputName, tempIdentity);
|
|
294
|
-
throw error;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
async function main() {
|
|
299
|
-
try {
|
|
300
|
-
const input = fs.readFileSync(0, 'utf8');
|
|
301
|
-
if (Buffer.byteLength(input) > MAX_INPUT_BYTES) {
|
|
302
|
-
throw new BackupWorkerError(
|
|
303
|
-
'BACKUP_WORKER_PROTOCOL_INVALID',
|
|
304
|
-
'backup worker payload is too large',
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
const result = await execute(JSON.parse(input));
|
|
308
|
-
process.stdout.write(`${JSON.stringify({ ok: true, result })}\n`);
|
|
309
|
-
} catch (error) {
|
|
310
|
-
process.stdout.write(`${JSON.stringify({
|
|
311
|
-
ok: false,
|
|
312
|
-
error: {
|
|
313
|
-
code: error.code || 'BACKUP_WORKER_FAILED',
|
|
314
|
-
message: error.message,
|
|
315
|
-
details: error.details,
|
|
316
|
-
},
|
|
317
|
-
})}\n`);
|
|
318
|
-
process.exitCode = 1;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
if (require.main === module) main();
|
|
323
|
-
|
|
324
|
-
module.exports = {
|
|
325
|
-
BACKUP_METHOD,
|
|
326
|
-
BackupWorkerError,
|
|
327
|
-
execute,
|
|
328
|
-
};
|