@nowcrew/daemon 0.6.88-beta.15 → 0.6.88-beta.16
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/README.md +15 -0
- package/dist/daemon-update-eligibility.js +1 -1
- package/dist/local-executor.js +4 -2
- package/dist/machine-info.js +16 -4
- package/dist/main.js +4 -69
- package/dist/project-skills/execution-adapter.js +3 -2
- package/dist/project-skills/runtime-launch.js +14 -0
- package/dist/provider-env.js +3 -4
- package/dist/runtimes/deepseek-harness-profile.js +92 -0
- package/dist/runtimes/deepseek-harness-runner.js +224 -0
- package/dist/runtimes/deepseek-harness.js +61 -10
- package/dist/runtimes/kimi-acp-runner.js +5 -5
- package/dist/serve.js +14 -62
- package/dist/storage-identity.js +0 -3
- package/dist/storage-serve-adapter.js +6 -72
- package/dist/supervised-runtime.js +17 -2
- package/package.json +1 -1
- package/dist/daemon-migration-controller.js +0 -211
- package/dist/daemon-migration-wiring.js +0 -22
- package/dist/storage-control.js +0 -212
- package/dist/storage-copy.js +0 -628
- package/dist/storage-credential-guard.js +0 -21
- package/dist/storage-doctor.js +0 -228
- package/dist/storage-manifest.js +0 -1027
- package/dist/storage-migration-controller.js +0 -440
- package/dist/storage-migration-manifest.js +0 -105
- package/dist/storage-migration-policy.js +0 -100
- package/dist/storage-runtime-controller.js +0 -381
- package/dist/storage-runtime-readiness.js +0 -240
- package/dist/storage-safe-fs.js +0 -302
- package/dist/storage-status-delivery.js +0 -224
- package/dist/storage-status-replay.js +0 -140
- package/dist/storage-target-confirmation.js +0 -129
- package/dist/storage-worker-command.js +0 -37
- package/dist/storage-worker-executor.js +0 -449
- package/dist/storage-worker-handoff.js +0 -151
- package/dist/storage-worker-operation.js +0 -294
- package/dist/storage-worker-status.js +0 -259
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { isAbsolute, join } from "node:path";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
import { atomicPrivateWrite } from "./atomic-private-write.js";
|
|
5
|
-
import { readStorageMigrationReceipt } from "./storage-migration-controller.js";
|
|
6
|
-
const FingerprintSchema = z.string().regex(/^sha256:[0-9a-f]{64}$/u);
|
|
7
|
-
const IdentitySchema = z.object({
|
|
8
|
-
type: z.literal("daemon:storage-migration-confirmed"),
|
|
9
|
-
migrationId: z.string().uuid(),
|
|
10
|
-
environment: z.string().regex(/^[a-z0-9][a-z0-9_-]{0,31}$/u),
|
|
11
|
-
workspaceId: z.string().uuid(),
|
|
12
|
-
profile: z.string().regex(/^[a-z0-9][a-z0-9_-]{0,47}$/u),
|
|
13
|
-
manifestDigest: z.string().regex(/^[0-9a-f]{64}$/u),
|
|
14
|
-
targetStorageFingerprint: FingerprintSchema,
|
|
15
|
-
}).strict();
|
|
16
|
-
const AcknowledgementSchema = IdentitySchema.omit({ type: true }).extend({
|
|
17
|
-
version: z.literal(1),
|
|
18
|
-
confirmedAt: z.string().datetime({ offset: true }),
|
|
19
|
-
}).strict();
|
|
20
|
-
const delay = (milliseconds) => new Promise((resolvePromise) => {
|
|
21
|
-
setTimeout(resolvePromise, milliseconds);
|
|
22
|
-
});
|
|
23
|
-
export function storageMigrationAcknowledgementPath(backupsRoot, migrationId) {
|
|
24
|
-
if (!isAbsolute(backupsRoot))
|
|
25
|
-
throw new Error("storage migration backups root must be absolute");
|
|
26
|
-
return join(backupsRoot, z.string().uuid().parse(migrationId), "hello-confirmed.json");
|
|
27
|
-
}
|
|
28
|
-
export async function waitForStorageMigrationAcknowledgement(input) {
|
|
29
|
-
const expected = IdentitySchema.omit({ type: true }).parse({
|
|
30
|
-
migrationId: input.migrationId,
|
|
31
|
-
environment: input.environment,
|
|
32
|
-
workspaceId: input.workspaceId,
|
|
33
|
-
profile: input.profile,
|
|
34
|
-
manifestDigest: input.manifestDigest,
|
|
35
|
-
targetStorageFingerprint: input.targetStorageFingerprint,
|
|
36
|
-
});
|
|
37
|
-
const timeoutMs = z.number().int().min(10).max(600_000).parse(input.timeoutMs ?? 120_000);
|
|
38
|
-
const pollIntervalMs = z.number().int().min(1).max(5_000).parse(input.pollIntervalMs ?? 250);
|
|
39
|
-
const path = storageMigrationAcknowledgementPath(input.backupsRoot, expected.migrationId);
|
|
40
|
-
const deadline = Date.now() + timeoutMs;
|
|
41
|
-
while (Date.now() <= deadline) {
|
|
42
|
-
try {
|
|
43
|
-
const acknowledgement = AcknowledgementSchema.parse(JSON.parse(await readFile(path, "utf8")));
|
|
44
|
-
const { version: _version, confirmedAt: _confirmedAt, ...identity } = acknowledgement;
|
|
45
|
-
if (JSON.stringify(identity) !== JSON.stringify(expected)) {
|
|
46
|
-
throw new Error("storage migration acknowledgement identity mismatch");
|
|
47
|
-
}
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
if (error.code !== "ENOENT")
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
await delay(pollIntervalMs);
|
|
55
|
-
}
|
|
56
|
-
throw new Error("storage migration hello confirmation timeout");
|
|
57
|
-
}
|
|
58
|
-
export function createStorageTargetConfirmationHandler(input) {
|
|
59
|
-
const profile = z.string().regex(/^[a-z0-9][a-z0-9_-]{0,47}$/u).parse(input.profile);
|
|
60
|
-
const completionTimeoutMs = z.number().int().min(10).max(600_000).parse(input.completionTimeoutMs ?? 120_000);
|
|
61
|
-
const pollIntervalMs = z.number().int().min(1).max(5_000).parse(input.pollIntervalMs ?? 250);
|
|
62
|
-
if (input.identity.storageMigrationId === undefined) {
|
|
63
|
-
throw new Error("storage target confirmation requires a migration identity");
|
|
64
|
-
}
|
|
65
|
-
let active = null;
|
|
66
|
-
const handle = async (raw, send) => {
|
|
67
|
-
const rawType = typeof raw === "object" && raw !== null && "type" in raw ? raw.type : undefined;
|
|
68
|
-
if (rawType !== "daemon:storage-migration-confirmed")
|
|
69
|
-
return false;
|
|
70
|
-
const confirmation = IdentitySchema.parse(raw);
|
|
71
|
-
if (confirmation.migrationId !== input.identity.storageMigrationId
|
|
72
|
-
|| confirmation.environment !== input.identity.storageEnvironment
|
|
73
|
-
|| confirmation.workspaceId !== input.identity.storageWorkspaceId
|
|
74
|
-
|| confirmation.profile !== profile
|
|
75
|
-
|| confirmation.targetStorageFingerprint !== input.identity.storageIdFingerprint) {
|
|
76
|
-
throw new Error("storage migration confirmation identity mismatch");
|
|
77
|
-
}
|
|
78
|
-
const key = JSON.stringify(confirmation);
|
|
79
|
-
if (active !== null) {
|
|
80
|
-
if (active.key !== key)
|
|
81
|
-
throw new Error("storage migration confirmation changed while processing");
|
|
82
|
-
return active.promise;
|
|
83
|
-
}
|
|
84
|
-
const processConfirmation = async () => {
|
|
85
|
-
const receipt = await readStorageMigrationReceipt(input.backupsRoot, confirmation.migrationId);
|
|
86
|
-
if (receipt === null || receipt.status !== "service_switched"
|
|
87
|
-
|| receipt.environment !== confirmation.environment
|
|
88
|
-
|| receipt.workspaceId !== confirmation.workspaceId
|
|
89
|
-
|| receipt.profile !== confirmation.profile
|
|
90
|
-
|| receipt.manifestDigest !== confirmation.manifestDigest) {
|
|
91
|
-
throw new Error("storage migration confirmation receipt identity mismatch");
|
|
92
|
-
}
|
|
93
|
-
const { type: _type, ...confirmationIdentity } = confirmation;
|
|
94
|
-
await atomicPrivateWrite(storageMigrationAcknowledgementPath(input.backupsRoot, confirmation.migrationId), `${JSON.stringify(AcknowledgementSchema.parse({
|
|
95
|
-
version: 1,
|
|
96
|
-
...confirmationIdentity,
|
|
97
|
-
confirmedAt: new Date().toISOString(),
|
|
98
|
-
}), null, 2)}\n`);
|
|
99
|
-
const deadline = Date.now() + completionTimeoutMs;
|
|
100
|
-
while (Date.now() <= deadline) {
|
|
101
|
-
const current = await readStorageMigrationReceipt(input.backupsRoot, confirmation.migrationId);
|
|
102
|
-
if (current?.status === "completed") {
|
|
103
|
-
send({
|
|
104
|
-
type: "daemon:storage-migration-status",
|
|
105
|
-
migrationId: confirmation.migrationId,
|
|
106
|
-
status: "completed",
|
|
107
|
-
environment: confirmation.environment,
|
|
108
|
-
workspaceId: confirmation.workspaceId,
|
|
109
|
-
profile: confirmation.profile,
|
|
110
|
-
});
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
if (current !== null && ["failed", "rolled_back", "rollback_failed"].includes(current.status)) {
|
|
114
|
-
throw new Error(`storage migration ended before completion: ${current.status}`);
|
|
115
|
-
}
|
|
116
|
-
await delay(pollIntervalMs);
|
|
117
|
-
}
|
|
118
|
-
throw new Error("storage migration worker completion timeout");
|
|
119
|
-
};
|
|
120
|
-
const promise = processConfirmation();
|
|
121
|
-
active = { key, promise };
|
|
122
|
-
void promise.catch(() => {
|
|
123
|
-
if (active?.key === key)
|
|
124
|
-
active = null;
|
|
125
|
-
});
|
|
126
|
-
return promise;
|
|
127
|
-
};
|
|
128
|
-
return Object.freeze({ handle });
|
|
129
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { posix, win32 } from "node:path";
|
|
2
|
-
import { executeStorageMigrationWorker, recoverStorageMigrationWorker, } from "./storage-worker-executor.js";
|
|
3
|
-
import { completeStorageMigrationWorkerServiceForHandoff } from "./storage-worker-handoff.js";
|
|
4
|
-
import { startStorageWorkerHeartbeat, writeStorageWorkerFailureStatus, } from "./storage-worker-status.js";
|
|
5
|
-
export async function runStorageWorkerCommand(args, execute = executeStorageMigrationWorker, writeFailure = writeStorageWorkerFailureStatus, startHeartbeat = startStorageWorkerHeartbeat, completeService = completeStorageMigrationWorkerServiceForHandoff, recover = recoverStorageMigrationWorker) {
|
|
6
|
-
const command = args[0];
|
|
7
|
-
if (command !== "storage-migration-worker" && command !== "storage-migration-recover")
|
|
8
|
-
return null;
|
|
9
|
-
const path = args[1] === "--job" ? args[2] : undefined;
|
|
10
|
-
if (args.length !== 3 || path === undefined || path.length === 0
|
|
11
|
-
|| /[\0\r\n]/u.test(path) || (!posix.isAbsolute(path) && !win32.isAbsolute(path))) {
|
|
12
|
-
throw new Error("Invalid storage migration worker command");
|
|
13
|
-
}
|
|
14
|
-
const stopHeartbeat = await startHeartbeat(path);
|
|
15
|
-
const assertHeartbeatHealthy = stopHeartbeat.assertHealthy;
|
|
16
|
-
try {
|
|
17
|
-
if (command === "storage-migration-recover") {
|
|
18
|
-
await (assertHeartbeatHealthy === undefined
|
|
19
|
-
? recover(path)
|
|
20
|
-
: recover(path, { assertHeartbeatHealthy }));
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
await (assertHeartbeatHealthy === undefined
|
|
24
|
-
? execute(path)
|
|
25
|
-
: execute(path, { assertHeartbeatHealthy }));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
await writeFailure(path, error).catch(() => undefined);
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
finally {
|
|
33
|
-
await stopHeartbeat().catch(() => undefined);
|
|
34
|
-
await completeService(path);
|
|
35
|
-
}
|
|
36
|
-
return 0;
|
|
37
|
-
}
|
|
@@ -1,449 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { realpathSync } from "node:fs";
|
|
3
|
-
import { buildServiceSpec, captureExactService, serviceAction, systemCommandRunner, } from "./computer-service.js";
|
|
4
|
-
import { listProfiles, loadProfile, removeProfile, saveProfile, } from "./computer-profile.js";
|
|
5
|
-
import { assertManagedServiceIdentity, installManagedService, uninstallManagedService, uninstallUnregisteredManagedService, } from "./managed-service-lifecycle.js";
|
|
6
|
-
import { readManagedServiceRegistry, normalizeManagedServiceRecord, serviceDescriptorSha256, } from "./managed-service-registry.js";
|
|
7
|
-
import { readInstallations, writeInstallations, } from "./storage-config.js";
|
|
8
|
-
import { runStorageDoctor } from "./storage-doctor.js";
|
|
9
|
-
import { readStorageMigrationReceipt, recoverStorageMigration, runStorageMigration, StorageMigrationFailure, } from "./storage-migration-controller.js";
|
|
10
|
-
import { materializeStorageManifest, publishStagedStorage, reconcileStorageManifest, verifyStagedStorage, } from "./storage-copy.js";
|
|
11
|
-
import { readStorageWorkerHandoff } from "./storage-worker-handoff.js";
|
|
12
|
-
import { readStorageWorkerOperation } from "./storage-worker-operation.js";
|
|
13
|
-
import { storageIdFingerprint } from "./storage-identity.js";
|
|
14
|
-
import { waitForStorageMigrationAcknowledgement } from "./storage-target-confirmation.js";
|
|
15
|
-
import { resolveDaemonLocalStateRoot } from "./daemon-local-home.js";
|
|
16
|
-
const defaultServiceDependencies = () => ({
|
|
17
|
-
assertManagedServiceIdentity,
|
|
18
|
-
serviceAction,
|
|
19
|
-
uninstallManagedService,
|
|
20
|
-
uninstallUnregisteredManagedService,
|
|
21
|
-
installManagedService,
|
|
22
|
-
captureExactService,
|
|
23
|
-
readManagedServices: readManagedServiceRegistry,
|
|
24
|
-
listProfiles,
|
|
25
|
-
loadProfile,
|
|
26
|
-
saveProfile,
|
|
27
|
-
removeProfile,
|
|
28
|
-
readInstallations,
|
|
29
|
-
writeInstallations,
|
|
30
|
-
});
|
|
31
|
-
function serviceSpec(operation, record, profileHome, layout = "target") {
|
|
32
|
-
return buildServiceSpec({
|
|
33
|
-
platform: operation.service.platform,
|
|
34
|
-
profile: operation.profile,
|
|
35
|
-
userHome: realpathSync.native(operation.service.userHome),
|
|
36
|
-
uid: operation.service.uid,
|
|
37
|
-
nodePath: record.nodePath,
|
|
38
|
-
entryPath: record.entryPath,
|
|
39
|
-
profileHome,
|
|
40
|
-
...(layout === "source" || operation.service.serviceEnvironment === undefined
|
|
41
|
-
? {}
|
|
42
|
-
: { environment: operation.service.serviceEnvironment }),
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
function assertRecordMatchesSpec(record, spec) {
|
|
46
|
-
const normalizedDescriptorPath = spec.descriptorPath === null
|
|
47
|
-
? null
|
|
48
|
-
: normalizeManagedServiceRecord({ ...record, descriptorPath: spec.descriptorPath }).descriptorPath;
|
|
49
|
-
const mismatches = [
|
|
50
|
-
spec.descriptor === null ? "descriptor" : null,
|
|
51
|
-
spec.descriptorPath === null ? "descriptorPath" : null,
|
|
52
|
-
record.platform !== spec.platform ? "platform" : null,
|
|
53
|
-
record.serviceId !== spec.id ? "serviceId" : null,
|
|
54
|
-
record.profile !== spec.profile ? "profile" : null,
|
|
55
|
-
record.descriptorPath !== normalizedDescriptorPath ? "descriptorPath" : null,
|
|
56
|
-
spec.descriptor !== null && record.descriptorSha256 !== serviceDescriptorSha256(spec.descriptor)
|
|
57
|
-
? "descriptorSha256"
|
|
58
|
-
: null,
|
|
59
|
-
].filter((value) => value !== null);
|
|
60
|
-
if (mismatches.length > 0) {
|
|
61
|
-
throw new Error(`Managed service record does not match operation service '${record.serviceId}': ${mismatches.join(", ")}`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function sameRecord(left, right) {
|
|
65
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
66
|
-
}
|
|
67
|
-
function sameInstallation(left, right) {
|
|
68
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
69
|
-
}
|
|
70
|
-
function sameProfile(left, right) {
|
|
71
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
72
|
-
}
|
|
73
|
-
function canonicalServerUrl(value) {
|
|
74
|
-
return value.replace(/\/+$/u, "");
|
|
75
|
-
}
|
|
76
|
-
export function createStorageWorkerServiceLifecycle(operation, injected = {}) {
|
|
77
|
-
const dependencies = { ...defaultServiceDependencies(), ...injected };
|
|
78
|
-
const sourceLocalHome = resolveDaemonLocalStateRoot({
|
|
79
|
-
userHome: operation.service.userHome,
|
|
80
|
-
platform: operation.service.platform,
|
|
81
|
-
profile: operation.service.sourceRecord.profile,
|
|
82
|
-
daemonHome: operation.service.sourceRecord.daemonHome,
|
|
83
|
-
});
|
|
84
|
-
const targetLocalHome = resolveDaemonLocalStateRoot({
|
|
85
|
-
userHome: operation.service.userHome,
|
|
86
|
-
platform: operation.service.platform,
|
|
87
|
-
profile: operation.service.targetRecord.profile,
|
|
88
|
-
daemonHome: operation.service.targetRecord.daemonHome,
|
|
89
|
-
...(operation.service.serviceEnvironment?.NOWWORK_LOCAL_HOME === undefined
|
|
90
|
-
? {} : { explicit: operation.service.serviceEnvironment.NOWWORK_LOCAL_HOME }),
|
|
91
|
-
...(operation.service.serviceEnvironment?.NOWWORK_LOCAL_HOME_MODE === undefined
|
|
92
|
-
? {} : { mode: operation.service.serviceEnvironment.NOWWORK_LOCAL_HOME_MODE }),
|
|
93
|
-
});
|
|
94
|
-
const sourceRegistry = { userHome: operation.service.userHome, localHome: sourceLocalHome };
|
|
95
|
-
const targetRegistry = { userHome: operation.service.userHome, localHome: targetLocalHome };
|
|
96
|
-
const sourceRecord = operation.service.sourceRecord;
|
|
97
|
-
const targetRecord = operation.service.targetRecord;
|
|
98
|
-
const sourceSpec = serviceSpec(operation, sourceRecord, operation.migration.sourceRoots["daemon-home"]);
|
|
99
|
-
const targetSpec = serviceSpec(operation, targetRecord, operation.installation.configRoot);
|
|
100
|
-
assertRecordMatchesSpec(sourceRecord, sourceSpec);
|
|
101
|
-
assertRecordMatchesSpec(targetRecord, targetSpec);
|
|
102
|
-
let sourceProfile = null;
|
|
103
|
-
let savedTargetProfile = null;
|
|
104
|
-
let baselineInstallations = null;
|
|
105
|
-
let targetProfileCreated = false;
|
|
106
|
-
let targetInstallationWritten = false;
|
|
107
|
-
const assertSourceIdentity = () => dependencies.assertManagedServiceIdentity({
|
|
108
|
-
spec: sourceSpec,
|
|
109
|
-
record: sourceRecord,
|
|
110
|
-
registry: sourceRegistry,
|
|
111
|
-
});
|
|
112
|
-
const readTargetInstallations = () => dependencies.readInstallations({
|
|
113
|
-
configRoot: operation.installation.configRoot,
|
|
114
|
-
});
|
|
115
|
-
const assertTargetMetadataUnchanged = async () => {
|
|
116
|
-
const profiles = await dependencies.listProfiles(operation.installation.configRoot);
|
|
117
|
-
if (profiles.includes(operation.profile))
|
|
118
|
-
throw new Error(`Target profile '${operation.profile}' already exists`);
|
|
119
|
-
const current = await readTargetInstallations();
|
|
120
|
-
if (baselineInstallations !== null && JSON.stringify(current) !== JSON.stringify(baselineInstallations)) {
|
|
121
|
-
throw new Error("Target installation registry changed after worker preflight");
|
|
122
|
-
}
|
|
123
|
-
if (current.some((record) => record.profile === operation.profile
|
|
124
|
-
|| record.installationId === operation.installation.target.installationId)) {
|
|
125
|
-
throw new Error("Target installation identity already exists");
|
|
126
|
-
}
|
|
127
|
-
return current;
|
|
128
|
-
};
|
|
129
|
-
const preflight = async () => {
|
|
130
|
-
await assertSourceIdentity();
|
|
131
|
-
const source = await dependencies.captureExactService(sourceSpec, systemCommandRunner);
|
|
132
|
-
if (!source.installed || !source.exact || source.running !== operation.service.sourceWasRunning) {
|
|
133
|
-
throw new Error("Source service state does not match worker operation");
|
|
134
|
-
}
|
|
135
|
-
const target = await dependencies.captureExactService(targetSpec, systemCommandRunner);
|
|
136
|
-
if (target.installed)
|
|
137
|
-
throw new Error(`Target service '${targetSpec.id}' already exists`);
|
|
138
|
-
sourceProfile = await dependencies.loadProfile(operation.profile, sourceRecord.daemonHome);
|
|
139
|
-
if (sourceProfile.name !== operation.profile
|
|
140
|
-
|| canonicalServerUrl(sourceProfile.serverUrl) !== canonicalServerUrl(operation.installation.target.serverUrl)) {
|
|
141
|
-
throw new Error("Source profile does not match target installation identity");
|
|
142
|
-
}
|
|
143
|
-
baselineInstallations = await assertTargetMetadataUnchanged();
|
|
144
|
-
};
|
|
145
|
-
const stopSourceService = async () => {
|
|
146
|
-
await dependencies.serviceAction(sourceSpec, "stop", systemCommandRunner);
|
|
147
|
-
const stopped = await dependencies.captureExactService(sourceSpec, systemCommandRunner);
|
|
148
|
-
if (!stopped.installed || !stopped.exact || stopped.running) {
|
|
149
|
-
throw new Error(`Source service '${sourceSpec.id}' did not stop exactly`);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
const installTargetService = async () => {
|
|
153
|
-
if (sourceProfile === null || baselineInstallations === null)
|
|
154
|
-
throw new Error("Worker service preflight is required");
|
|
155
|
-
const installations = await assertTargetMetadataUnchanged();
|
|
156
|
-
await assertSourceIdentity();
|
|
157
|
-
await dependencies.uninstallManagedService({
|
|
158
|
-
spec: sourceSpec,
|
|
159
|
-
record: sourceRecord,
|
|
160
|
-
runner: systemCommandRunner,
|
|
161
|
-
registry: sourceRegistry,
|
|
162
|
-
});
|
|
163
|
-
const { version: _version, ...profile } = sourceProfile;
|
|
164
|
-
savedTargetProfile = await dependencies.saveProfile({
|
|
165
|
-
...profile,
|
|
166
|
-
agentsRoot: targetRecord.agentsRoot,
|
|
167
|
-
}, operation.installation.configRoot);
|
|
168
|
-
targetProfileCreated = true;
|
|
169
|
-
await dependencies.writeInstallations([...installations, operation.installation.target].sort((left, right) => left.profile.localeCompare(right.profile)), { configRoot: operation.installation.configRoot });
|
|
170
|
-
targetInstallationWritten = true;
|
|
171
|
-
await dependencies.installManagedService({
|
|
172
|
-
spec: targetSpec,
|
|
173
|
-
record: targetRecord,
|
|
174
|
-
runner: systemCommandRunner,
|
|
175
|
-
registry: targetRegistry,
|
|
176
|
-
});
|
|
177
|
-
const target = await dependencies.captureExactService(targetSpec, systemCommandRunner);
|
|
178
|
-
if (!target.installed || !target.exact || target.running) {
|
|
179
|
-
throw new Error(`Target service '${targetSpec.id}' was not installed exactly in stopped state`);
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
const startTargetService = async () => {
|
|
183
|
-
await dependencies.serviceAction(targetSpec, "start", systemCommandRunner);
|
|
184
|
-
const target = await dependencies.captureExactService(targetSpec, systemCommandRunner);
|
|
185
|
-
if (!target.installed || !target.exact || !target.running) {
|
|
186
|
-
throw new Error(`Target service '${targetSpec.id}' did not start exactly`);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
const removeTargetService = async () => {
|
|
190
|
-
const records = await dependencies.readManagedServices(targetRegistry);
|
|
191
|
-
const registered = records.find((record) => record.serviceId === targetRecord.serviceId
|
|
192
|
-
|| (record.profile === targetRecord.profile && record.daemonHome === targetRecord.daemonHome));
|
|
193
|
-
if (registered !== undefined && !sameRecord(registered, targetRecord)) {
|
|
194
|
-
throw new Error(`Target service '${targetSpec.id}' identity changed before rollback`);
|
|
195
|
-
}
|
|
196
|
-
if (registered !== undefined) {
|
|
197
|
-
await dependencies.uninstallManagedService({
|
|
198
|
-
spec: targetSpec,
|
|
199
|
-
record: targetRecord,
|
|
200
|
-
runner: systemCommandRunner,
|
|
201
|
-
registry: targetRegistry,
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
const target = await dependencies.captureExactService(targetSpec, systemCommandRunner);
|
|
206
|
-
if (target.installed && !target.exact)
|
|
207
|
-
throw new Error(`Target service '${targetSpec.id}' changed before rollback`);
|
|
208
|
-
if (target.installed) {
|
|
209
|
-
await dependencies.uninstallUnregisteredManagedService({
|
|
210
|
-
spec: targetSpec,
|
|
211
|
-
daemonHome: targetRecord.daemonHome,
|
|
212
|
-
npmPrefix: targetRecord.npmPrefix,
|
|
213
|
-
runner: systemCommandRunner,
|
|
214
|
-
registry: targetRegistry,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
const installations = await readTargetInstallations();
|
|
219
|
-
const targetInstallation = installations.find((record) => record.profile === operation.profile
|
|
220
|
-
|| record.installationId === operation.installation.target.installationId);
|
|
221
|
-
if (targetInstallation !== undefined) {
|
|
222
|
-
if (!sameInstallation(targetInstallation, operation.installation.target)) {
|
|
223
|
-
throw new Error("Target installation identity changed before rollback");
|
|
224
|
-
}
|
|
225
|
-
await dependencies.writeInstallations(installations.filter((record) => !sameInstallation(record, operation.installation.target)), { configRoot: operation.installation.configRoot });
|
|
226
|
-
targetInstallationWritten = false;
|
|
227
|
-
}
|
|
228
|
-
const profiles = await dependencies.listProfiles(operation.installation.configRoot);
|
|
229
|
-
if (profiles.includes(operation.profile)) {
|
|
230
|
-
const current = await dependencies.loadProfile(operation.profile, operation.installation.configRoot);
|
|
231
|
-
const original = sourceProfile
|
|
232
|
-
?? await dependencies.loadProfile(operation.profile, sourceRecord.daemonHome);
|
|
233
|
-
const { version: _version, ...profile } = original;
|
|
234
|
-
const expected = savedTargetProfile ?? { version: 1, ...profile, agentsRoot: targetRecord.agentsRoot };
|
|
235
|
-
if (!sameProfile(current, expected)) {
|
|
236
|
-
throw new Error("Target profile identity changed before rollback");
|
|
237
|
-
}
|
|
238
|
-
await dependencies.removeProfile(operation.profile, operation.installation.configRoot);
|
|
239
|
-
targetProfileCreated = false;
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
const restoreSourceService = async () => {
|
|
243
|
-
const current = await dependencies.captureExactService(sourceSpec, systemCommandRunner);
|
|
244
|
-
if (current.installed && !current.exact) {
|
|
245
|
-
throw new Error(`Source service '${sourceSpec.id}' changed before rollback`);
|
|
246
|
-
}
|
|
247
|
-
if (!current.installed) {
|
|
248
|
-
await dependencies.installManagedService({
|
|
249
|
-
spec: sourceSpec,
|
|
250
|
-
record: sourceRecord,
|
|
251
|
-
runner: systemCommandRunner,
|
|
252
|
-
registry: sourceRegistry,
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
const afterInstall = current.installed
|
|
256
|
-
? current
|
|
257
|
-
: await dependencies.captureExactService(sourceSpec, systemCommandRunner);
|
|
258
|
-
if (operation.service.sourceWasRunning && !afterInstall.running) {
|
|
259
|
-
await dependencies.serviceAction(sourceSpec, "start", systemCommandRunner);
|
|
260
|
-
}
|
|
261
|
-
else if (!operation.service.sourceWasRunning && afterInstall.running) {
|
|
262
|
-
await dependencies.serviceAction(sourceSpec, "stop", systemCommandRunner);
|
|
263
|
-
}
|
|
264
|
-
const restored = await dependencies.captureExactService(sourceSpec, systemCommandRunner);
|
|
265
|
-
if (!restored.installed || !restored.exact || restored.running !== operation.service.sourceWasRunning) {
|
|
266
|
-
throw new Error(`Source service '${sourceSpec.id}' was not restored exactly`);
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
return Object.freeze({
|
|
270
|
-
preflight,
|
|
271
|
-
assertServiceIdentity: assertSourceIdentity,
|
|
272
|
-
isBusy: () => false,
|
|
273
|
-
quiesceExecutions: async () => undefined,
|
|
274
|
-
waitForIdle: async () => undefined,
|
|
275
|
-
flushLocalState: async () => undefined,
|
|
276
|
-
stopSourceService,
|
|
277
|
-
installTargetService,
|
|
278
|
-
startTargetService,
|
|
279
|
-
removeTargetService,
|
|
280
|
-
restoreSourceService,
|
|
281
|
-
confirmHello: (receipt) => waitForStorageMigrationAcknowledgement({
|
|
282
|
-
backupsRoot: operation.migration.backupsRoot,
|
|
283
|
-
migrationId: operation.migrationId,
|
|
284
|
-
environment: operation.environment,
|
|
285
|
-
workspaceId: operation.workspaceId,
|
|
286
|
-
profile: operation.profile,
|
|
287
|
-
manifestDigest: receipt.manifestDigest,
|
|
288
|
-
targetStorageFingerprint: storageIdFingerprint(operation.installation.target.storageId),
|
|
289
|
-
}),
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
function validateFreshDoctor(operation, doctor) {
|
|
293
|
-
if (!doctor.applyAllowed
|
|
294
|
-
|| doctor.manifestDigest !== operation.expectedManifestDigest
|
|
295
|
-
|| doctor.manifest.migrationId !== operation.migrationId
|
|
296
|
-
|| doctor.environment !== operation.environment
|
|
297
|
-
|| doctor.workspaceId !== operation.workspaceId
|
|
298
|
-
|| doctor.profile !== operation.profile
|
|
299
|
-
|| doctor.manifest.targetWorkspaceRoot !== operation.migration.targetWorkspaceRoot) {
|
|
300
|
-
throw new StorageMigrationFailure("worker_doctor_mismatch", "fresh worker doctor digest or identity does not match the approved operation");
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
function workerDoctorInput(operation, generatedAt = operation.doctorInput.generatedAt) {
|
|
304
|
-
const { freeSpaceMultiplier, manifestStoreRoot, ...input } = operation.doctorInput;
|
|
305
|
-
return {
|
|
306
|
-
...input,
|
|
307
|
-
generatedAt,
|
|
308
|
-
...(manifestStoreRoot === undefined ? {} : { manifestStoreRoot }),
|
|
309
|
-
...(freeSpaceMultiplier === undefined ? {} : { freeSpaceMultiplier }),
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
function workerSourceRoots(operation) {
|
|
313
|
-
return Object.fromEntries(Object.entries(operation.migration.sourceRoots)
|
|
314
|
-
.filter((entry) => entry[1] !== undefined));
|
|
315
|
-
}
|
|
316
|
-
export async function executeStorageMigrationWorker(handoffPath, injected = {}) {
|
|
317
|
-
const dependencies = {
|
|
318
|
-
runDoctor: (input) => runStorageDoctor(input),
|
|
319
|
-
runMigration: runStorageMigration,
|
|
320
|
-
recoverMigration: recoverStorageMigration,
|
|
321
|
-
readReceipt: readStorageMigrationReceipt,
|
|
322
|
-
createServiceLifecycle: (operation) => createStorageWorkerServiceLifecycle(operation),
|
|
323
|
-
now: () => new Date(),
|
|
324
|
-
...injected,
|
|
325
|
-
};
|
|
326
|
-
const handoff = await readStorageWorkerHandoff(handoffPath);
|
|
327
|
-
const operation = await readStorageWorkerOperation(handoff.operationPath, handoff.operationSha256);
|
|
328
|
-
const assertHeartbeatHealthy = dependencies.assertHeartbeatHealthy ?? (() => undefined);
|
|
329
|
-
if (handoff.migrationId !== operation.migrationId || handoff.profile !== operation.profile) {
|
|
330
|
-
throw new StorageMigrationFailure("worker_handoff_mismatch", "worker handoff does not match operation identity");
|
|
331
|
-
}
|
|
332
|
-
const lifecycle = dependencies.createServiceLifecycle(operation);
|
|
333
|
-
const existing = await dependencies.readReceipt(operation.migration.backupsRoot, operation.migrationId);
|
|
334
|
-
if (existing !== null) {
|
|
335
|
-
if (existing.environment !== operation.environment || existing.workspaceId !== operation.workspaceId
|
|
336
|
-
|| existing.profile !== operation.profile) {
|
|
337
|
-
throw new StorageMigrationFailure("worker_receipt_mismatch", "worker receipt does not match operation identity");
|
|
338
|
-
}
|
|
339
|
-
if (existing.status === "completed" || existing.status === "rolled_back")
|
|
340
|
-
return existing;
|
|
341
|
-
return dependencies.recoverMigration({
|
|
342
|
-
backupsRoot: operation.migration.backupsRoot,
|
|
343
|
-
migrationId: operation.migrationId,
|
|
344
|
-
}, { ...lifecycle, now: dependencies.now });
|
|
345
|
-
}
|
|
346
|
-
assertHeartbeatHealthy();
|
|
347
|
-
await lifecycle.preflight();
|
|
348
|
-
assertHeartbeatHealthy();
|
|
349
|
-
const doctor = await dependencies.runDoctor(workerDoctorInput(operation));
|
|
350
|
-
assertHeartbeatHealthy();
|
|
351
|
-
validateFreshDoctor(operation, doctor);
|
|
352
|
-
const guardedMaterialize = async (input, hooks = {}) => materializeStorageManifest(input, {
|
|
353
|
-
...hooks,
|
|
354
|
-
afterSourceLstat: async (path) => {
|
|
355
|
-
await hooks.afterSourceLstat?.(path);
|
|
356
|
-
assertHeartbeatHealthy();
|
|
357
|
-
},
|
|
358
|
-
afterTargetParentOpen: async (path) => {
|
|
359
|
-
await hooks.afterTargetParentOpen?.(path);
|
|
360
|
-
assertHeartbeatHealthy();
|
|
361
|
-
},
|
|
362
|
-
});
|
|
363
|
-
const guardedVerify = async (input, hooks = {}) => verifyStagedStorage(input, {
|
|
364
|
-
...hooks,
|
|
365
|
-
afterTargetLstat: async (path) => {
|
|
366
|
-
await hooks.afterTargetLstat?.(path);
|
|
367
|
-
assertHeartbeatHealthy();
|
|
368
|
-
},
|
|
369
|
-
});
|
|
370
|
-
const guardedReconcile = async (input) => {
|
|
371
|
-
assertHeartbeatHealthy();
|
|
372
|
-
const result = await reconcileStorageManifest(input);
|
|
373
|
-
assertHeartbeatHealthy();
|
|
374
|
-
return result;
|
|
375
|
-
};
|
|
376
|
-
const guardedPublish = async (input, deps = {}) => {
|
|
377
|
-
assertHeartbeatHealthy();
|
|
378
|
-
await publishStagedStorage(input, deps);
|
|
379
|
-
assertHeartbeatHealthy();
|
|
380
|
-
};
|
|
381
|
-
const receipt = await dependencies.runMigration({
|
|
382
|
-
doctor,
|
|
383
|
-
sourceRoots: workerSourceRoots(operation),
|
|
384
|
-
stagingWorkspaceRoot: operation.migration.stagingWorkspaceRoot,
|
|
385
|
-
targetWorkspaceRoot: operation.migration.targetWorkspaceRoot,
|
|
386
|
-
backupsRoot: operation.migration.backupsRoot,
|
|
387
|
-
backupArtifacts: operation.migration.backupArtifacts,
|
|
388
|
-
...(operation.migration.ownership === undefined ? {} : { ownership: operation.migration.ownership }),
|
|
389
|
-
acceptedWarnings: operation.migration.acceptedWarnings,
|
|
390
|
-
onStatus: () => assertHeartbeatHealthy(),
|
|
391
|
-
assertHealthy: assertHeartbeatHealthy,
|
|
392
|
-
}, {
|
|
393
|
-
...lifecycle,
|
|
394
|
-
now: dependencies.now,
|
|
395
|
-
materialize: guardedMaterialize,
|
|
396
|
-
reconcile: guardedReconcile,
|
|
397
|
-
verify: guardedVerify,
|
|
398
|
-
publish: guardedPublish,
|
|
399
|
-
refreshDoctor: async () => {
|
|
400
|
-
const refreshed = await dependencies.runDoctor(workerDoctorInput(operation, dependencies.now().toISOString()));
|
|
401
|
-
if (refreshed.manifest.migrationId !== operation.migrationId
|
|
402
|
-
|| refreshed.environment !== operation.environment
|
|
403
|
-
|| refreshed.workspaceId !== operation.workspaceId
|
|
404
|
-
|| refreshed.profile !== operation.profile) {
|
|
405
|
-
throw new StorageMigrationFailure("final_doctor_failed", "final worker doctor identity changed");
|
|
406
|
-
}
|
|
407
|
-
return refreshed;
|
|
408
|
-
},
|
|
409
|
-
});
|
|
410
|
-
assertHeartbeatHealthy();
|
|
411
|
-
return receipt;
|
|
412
|
-
}
|
|
413
|
-
export async function recoverStorageMigrationWorker(handoffPath, injected = {}) {
|
|
414
|
-
const dependencies = {
|
|
415
|
-
runDoctor: (input) => runStorageDoctor(input),
|
|
416
|
-
runMigration: runStorageMigration,
|
|
417
|
-
recoverMigration: recoverStorageMigration,
|
|
418
|
-
readReceipt: readStorageMigrationReceipt,
|
|
419
|
-
createServiceLifecycle: (operation) => createStorageWorkerServiceLifecycle(operation),
|
|
420
|
-
now: () => new Date(),
|
|
421
|
-
...injected,
|
|
422
|
-
};
|
|
423
|
-
const handoff = await readStorageWorkerHandoff(handoffPath);
|
|
424
|
-
const operation = await readStorageWorkerOperation(handoff.operationPath, handoff.operationSha256);
|
|
425
|
-
const assertHeartbeatHealthy = dependencies.assertHeartbeatHealthy ?? (() => undefined);
|
|
426
|
-
assertHeartbeatHealthy();
|
|
427
|
-
if (handoff.migrationId !== operation.migrationId || handoff.profile !== operation.profile) {
|
|
428
|
-
throw new StorageMigrationFailure("worker_handoff_mismatch", "worker handoff does not match operation identity");
|
|
429
|
-
}
|
|
430
|
-
const receipt = await dependencies.readReceipt(operation.migration.backupsRoot, operation.migrationId);
|
|
431
|
-
if (receipt === null) {
|
|
432
|
-
throw new StorageMigrationFailure("migration_receipt_missing", "explicit rollback requires an existing migration receipt");
|
|
433
|
-
}
|
|
434
|
-
if (receipt.environment !== operation.environment || receipt.workspaceId !== operation.workspaceId
|
|
435
|
-
|| receipt.profile !== operation.profile) {
|
|
436
|
-
throw new StorageMigrationFailure("worker_receipt_mismatch", "worker receipt does not match operation identity");
|
|
437
|
-
}
|
|
438
|
-
if (receipt.status === "completed" || receipt.status === "rolled_back")
|
|
439
|
-
return receipt;
|
|
440
|
-
const recovered = await dependencies.recoverMigration({
|
|
441
|
-
backupsRoot: operation.migration.backupsRoot,
|
|
442
|
-
migrationId: operation.migrationId,
|
|
443
|
-
}, { ...dependencies.createServiceLifecycle(operation), now: dependencies.now });
|
|
444
|
-
assertHeartbeatHealthy();
|
|
445
|
-
return recovered;
|
|
446
|
-
}
|
|
447
|
-
export function storageWorkerOperationDigest(operation) {
|
|
448
|
-
return createHash("sha256").update(`${JSON.stringify(operation, null, 2)}\n`).digest("hex");
|
|
449
|
-
}
|