@nowcrew/daemon 0.6.103 → 0.6.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/package.json +1 -1
  2. package/dist/codex-home-launch.js +0 -7
  3. package/dist/codex-home-mode-controller.js +0 -68
  4. package/dist/daemon-migration-controller.js +0 -210
  5. package/dist/daemon-migration-wiring.js +0 -22
  6. package/dist/project-skills/capability.js +0 -109
  7. package/dist/project-skills/controller-convergence.js +0 -57
  8. package/dist/project-skills/projection-set-switch.js +0 -419
  9. package/dist/project-skills/projection-state-domain.js +0 -170
  10. package/dist/project-skills/projection-state-store.js +0 -842
  11. package/dist/project-skills/projection-state-transaction.js +0 -322
  12. package/dist/project-skills/projection-state.js +0 -3
  13. package/dist/project-skills/runtime-root-bootstrap.js +0 -47
  14. package/dist/project-skills/runtime-root-domain.js +0 -268
  15. package/dist/project-skills/runtime-root-gc.js +0 -293
  16. package/dist/project-skills/runtime-root-lease-artifact.js +0 -46
  17. package/dist/project-skills/runtime-root-leases.js +0 -487
  18. package/dist/project-skills/runtime-root-source-identity.js +0 -60
  19. package/dist/project-skills/runtime-root-startup.js +0 -49
  20. package/dist/project-skills/runtime-root-state-artifact-domain.js +0 -143
  21. package/dist/project-skills/runtime-root-state-index.js +0 -356
  22. package/dist/project-skills/runtime-root-store.js +0 -722
  23. package/dist/project-skills/serve-capability.js +0 -28
  24. package/dist/project-skills/serve-startup.js +0 -37
  25. package/dist/runtimes/codex-home-migration-cli.js +0 -26
  26. package/dist/runtimes/codex-home-migration.js +0 -112
  27. package/dist/runtimes/codex-home.js +0 -236
  28. package/dist/storage-control.js +0 -212
  29. package/dist/storage-copy.js +0 -628
  30. package/dist/storage-credential-guard.js +0 -21
  31. package/dist/storage-doctor.js +0 -228
  32. package/dist/storage-manifest.js +0 -1027
  33. package/dist/storage-migration-controller.js +0 -441
  34. package/dist/storage-migration-manifest.js +0 -105
  35. package/dist/storage-migration-policy.js +0 -100
  36. package/dist/storage-runtime-controller.js +0 -361
  37. package/dist/storage-runtime-readiness.js +0 -206
  38. package/dist/storage-safe-fs.js +0 -302
  39. package/dist/storage-status-delivery.js +0 -224
  40. package/dist/storage-status-replay.js +0 -140
  41. package/dist/storage-target-confirmation.js +0 -129
  42. package/dist/storage-worker-command.js +0 -37
  43. package/dist/storage-worker-executor.js +0 -431
  44. package/dist/storage-worker-handoff.js +0 -151
  45. package/dist/storage-worker-operation.js +0 -288
  46. package/dist/storage-worker-status.js +0 -259
@@ -1,361 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { dirname, join } from "node:path";
3
- import { z } from "zod";
4
- import { buildServiceSpec } from "./computer-service.js";
5
- import { buildManagedServiceRecord } from "./managed-service-lifecycle.js";
6
- import { createStorageControlController } from "./storage-control.js";
7
- import { runStorageDoctor } from "./storage-doctor.js";
8
- import { storageHelloIdentity } from "./storage-identity.js";
9
- import { storageLayout, validateStorageEnvironment } from "./storage-layout.js";
10
- import { StorageMigrationFailure } from "./storage-migration-controller.js";
11
- import { launchStorageMigrationWorker, writeStorageWorkerHandoff } from "./storage-worker-handoff.js";
12
- import { writeStorageWorkerOperation } from "./storage-worker-operation.js";
13
- import { latestSourceStorageMigrationReceipt, monitorStorageMigrationStatus, storageMigrationStatusReplay, } from "./storage-status-replay.js";
14
- const UUID = z.string().uuid();
15
- function fingerprint(value) {
16
- return `sha256:${createHash("sha256").update(value).digest("hex")}`;
17
- }
18
- function deterministicUuid(value) {
19
- const chars = createHash("sha256").update(value).digest("hex").slice(0, 32).split("");
20
- chars[12] = "4";
21
- chars[16] = ((Number.parseInt(chars[16], 16) & 0x3) | 0x8).toString(16);
22
- const hex = chars.join("");
23
- return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
24
- }
25
- function storageId(input, workspaceId) {
26
- if (input.ownership.workspaceId !== workspaceId || input.ownership.environment !== input.environment) {
27
- throw new Error("Storage ownership claim does not match the migration identity");
28
- }
29
- return `storage_${input.ownership.claimFingerprint.slice("sha256:".length)}`;
30
- }
31
- function installationBase(input, workspaceId, migrationId, createdAt) {
32
- const layout = storageLayout({
33
- nowworkHome: input.nowworkHome,
34
- environment: input.environment,
35
- workspaceId,
36
- runtimeBase: input.local.runtimeBase,
37
- platform: input.platform,
38
- });
39
- return {
40
- version: 1,
41
- installationId: deterministicUuid(`installation\0${input.serverUrl}\0${input.profile}\0${workspaceId}\0${input.nowworkHome}`),
42
- profile: input.profile,
43
- serverUrl: input.serverUrl,
44
- environment: input.environment,
45
- workspaceId,
46
- storageId: storageId(input, workspaceId),
47
- nowworkHome: input.nowworkHome,
48
- runtimeRoot: layout.runtimeRoot,
49
- credentialRef: `profile:${input.profile}`,
50
- ...(migrationId === undefined ? {} : { storageMigrationId: migrationId }),
51
- createdAt,
52
- updatedAt: createdAt,
53
- };
54
- }
55
- function targetRecord(input, workspaceId, createdAt) {
56
- const layout = storageLayout({
57
- nowworkHome: input.nowworkHome,
58
- environment: input.environment,
59
- workspaceId,
60
- runtimeBase: input.local.runtimeBase,
61
- platform: input.platform,
62
- });
63
- const spec = buildServiceSpec({
64
- platform: input.platform,
65
- profile: input.profile,
66
- userHome: input.userHome,
67
- uid: input.uid,
68
- nodePath: input.sourceRecord.nodePath,
69
- entryPath: input.sourceRecord.entryPath,
70
- profileHome: input.local.configRoot,
71
- ...(input.serviceEnvironment === undefined ? {} : { environment: input.serviceEnvironment }),
72
- });
73
- return buildManagedServiceRecord({
74
- spec,
75
- daemonHome: input.local.configRoot,
76
- agentsRoot: layout.agentItemsRoot,
77
- nodePath: input.sourceRecord.nodePath,
78
- entryPath: input.sourceRecord.entryPath,
79
- packageRoot: input.sourceRecord.packageRoot,
80
- npmPrefix: input.sourceRecord.npmPrefix,
81
- generation: () => deterministicUuid(`service\0${spec.id}\0${workspaceId}`),
82
- now: () => new Date(createdAt),
83
- });
84
- }
85
- export function createStorageMigrationRuntimeController(rawInput, injected = {}) {
86
- const input = {
87
- ...rawInput,
88
- profile: z.string().regex(/^[a-z0-9][a-z0-9_-]{0,47}$/u).parse(rawInput.profile),
89
- serverUrl: z.string().url().parse(rawInput.serverUrl),
90
- environment: validateStorageEnvironment(rawInput.environment),
91
- };
92
- if (input.sourceRecord.profile !== input.profile || input.sourceRecord.platform !== input.platform) {
93
- throw new Error("Storage runtime source service identity does not match profile");
94
- }
95
- const dependencies = {
96
- assertSourceIdentity: async () => undefined,
97
- runDoctor: (doctorInput) => runStorageDoctor(doctorInput),
98
- writeOperation: writeStorageWorkerOperation,
99
- writeHandoff: writeStorageWorkerHandoff,
100
- launchWorker: launchStorageMigrationWorker,
101
- monitorWorker: monitorStorageMigrationStatus,
102
- now: () => new Date(),
103
- ...injected,
104
- };
105
- let workspaceId = null;
106
- let machineId = null;
107
- let identity;
108
- let gate = null;
109
- let sendStatus = null;
110
- let control = null;
111
- const pendingControlMessages = [];
112
- let drainingPendingControlMessages = false;
113
- const drainPendingControlMessages = () => {
114
- if (control === null || drainingPendingControlMessages || pendingControlMessages.length === 0)
115
- return;
116
- drainingPendingControlMessages = true;
117
- const pending = pendingControlMessages.splice(0);
118
- void (async () => {
119
- for (const message of pending)
120
- await control?.handle(message);
121
- })().catch(() => undefined).finally(() => {
122
- drainingPendingControlMessages = false;
123
- drainPendingControlMessages();
124
- });
125
- };
126
- const doctorInput = async (migrationId) => {
127
- if (workspaceId === null || gate === null)
128
- throw new Error("Storage migration runtime is not attached");
129
- let profileIdentityExact = true;
130
- await dependencies.assertSourceIdentity().catch(() => { profileIdentityExact = false; });
131
- const target = storageLayout({
132
- nowworkHome: input.nowworkHome,
133
- environment: input.environment,
134
- workspaceId,
135
- runtimeBase: input.local.runtimeBase,
136
- platform: input.platform,
137
- });
138
- return {
139
- migrationId,
140
- generatedAt: dependencies.now().toISOString(),
141
- environment: input.environment,
142
- workspaceId,
143
- profile: input.profile,
144
- targetWorkspaceRoot: target.workspaceRoot,
145
- sources: [
146
- { kind: "daemon-home", root: input.sourceRecord.daemonHome, logicalPrefix: "daemon" },
147
- { kind: "agents-root", root: input.sourceRecord.agentsRoot, logicalPrefix: "agent-items" },
148
- ],
149
- policy: input.policy,
150
- manifestStoreRoot: input.local.stateRoot,
151
- state: {
152
- profileIdentityExact,
153
- duplicateWorkspaceMappings: input.duplicateWorkspaceMappings ?? 0,
154
- activeAgentExecutions: gate.isBusy() ? 1 : 0,
155
- activeScheduledRuns: 0,
156
- },
157
- freeSpaceMultiplier: 1.2,
158
- };
159
- };
160
- const buildOperation = (doctor, freshInput) => {
161
- if (workspaceId === null)
162
- throw new Error("Storage migration workspace identity is missing");
163
- const createdAt = freshInput.generatedAt;
164
- const target = storageLayout({
165
- nowworkHome: input.nowworkHome,
166
- environment: input.environment,
167
- workspaceId,
168
- runtimeBase: input.local.runtimeBase,
169
- platform: input.platform,
170
- });
171
- const { freeSpaceMultiplier, ...doctorBase } = freshInput;
172
- const operationDoctorInput = {
173
- ...doctorBase,
174
- sources: freshInput.sources.map((source) => ({ ...source })),
175
- policy: {
176
- exclusions: freshInput.policy.exclusions.map((exclusion) => ({ ...exclusion })),
177
- transformations: freshInput.policy.transformations.map((transformation) => ({ ...transformation })),
178
- },
179
- ...(freeSpaceMultiplier === undefined ? {} : { freeSpaceMultiplier }),
180
- };
181
- return {
182
- version: 1,
183
- migrationId: doctor.manifest.migrationId,
184
- createdAt,
185
- environment: input.environment,
186
- workspaceId,
187
- profile: input.profile,
188
- expectedManifestDigest: doctor.manifestDigest,
189
- doctorInput: operationDoctorInput,
190
- migration: {
191
- sourceRoots: {
192
- "daemon-home": input.sourceRecord.daemonHome,
193
- "agents-root": input.sourceRecord.agentsRoot,
194
- },
195
- stagingWorkspaceRoot: join(dirname(target.workspaceRoot), `.nowwork-staging-${doctor.manifest.migrationId}`),
196
- targetWorkspaceRoot: target.workspaceRoot,
197
- backupsRoot: input.local.backupsRoot,
198
- backupArtifacts: input.backupArtifacts.map((artifact) => ({ ...artifact })),
199
- ownership: {
200
- ownerEpoch: input.ownership.ownerEpoch,
201
- ownershipClaimFingerprint: input.ownership.claimFingerprint,
202
- },
203
- acceptedWarnings: [...input.policy.acceptedWarnings],
204
- },
205
- service: {
206
- platform: input.platform,
207
- userHome: input.userHome,
208
- ...(input.uid === undefined ? {} : { uid: input.uid }),
209
- ...(input.serviceEnvironment === undefined ? {} : { serviceEnvironment: input.serviceEnvironment }),
210
- sourceWasRunning: true,
211
- sourceRecord: input.sourceRecord,
212
- targetRecord: targetRecord(input, workspaceId, createdAt),
213
- },
214
- installation: {
215
- configRoot: input.local.configRoot,
216
- target: {
217
- ...installationBase(input, workspaceId, doctor.manifest.migrationId, createdAt),
218
- storageMigrationId: doctor.manifest.migrationId,
219
- },
220
- },
221
- };
222
- };
223
- const ensureControl = () => {
224
- if (control !== null || workspaceId === null || gate === null || sendStatus === null || identity === undefined)
225
- return;
226
- const sourceStorageFingerprint = fingerprint(JSON.stringify({
227
- profile: input.profile,
228
- daemonHome: input.sourceRecord.daemonHome,
229
- agentsRoot: input.sourceRecord.agentsRoot,
230
- serviceId: input.sourceRecord.serviceId,
231
- }));
232
- control = createStorageControlController({
233
- environment: input.environment,
234
- workspaceId,
235
- profile: input.profile,
236
- sourceStorageFingerprint,
237
- targetStorageFingerprint: identity.storageIdFingerprint,
238
- ownership: {
239
- ownerEpoch: input.ownership.ownerEpoch,
240
- ownershipClaimFingerprint: input.ownership.claimFingerprint,
241
- },
242
- runDoctor: async (migrationId) => dependencies.runDoctor(await doctorInput(migrationId)),
243
- runMigration: async (doctor) => {
244
- await gate.closeAndFlush();
245
- let workerLaunched = false;
246
- try {
247
- const freshInput = await doctorInput(doctor.manifest.migrationId);
248
- const freshDoctor = await dependencies.runDoctor(freshInput);
249
- if (!freshDoctor.applyAllowed || freshDoctor.manifestDigest !== doctor.manifestDigest) {
250
- throw new StorageMigrationFailure("source_changed_after_preflight", "source data or doctor gates changed after server preflight");
251
- }
252
- const operation = buildOperation(freshDoctor, freshInput);
253
- const written = await dependencies.writeOperation(operation, { stateRoot: input.local.stateRoot });
254
- const handoffPath = await dependencies.writeHandoff({
255
- version: 1,
256
- migrationId: operation.migrationId,
257
- profile: operation.profile,
258
- operationPath: written.path,
259
- operationSha256: written.sha256,
260
- }, { stateRoot: input.local.stateRoot });
261
- await dependencies.launchWorker({
262
- entryPath: input.sourceRecord.entryPath,
263
- handoffPath,
264
- migrationId: operation.migrationId,
265
- platform: input.platform,
266
- userHome: input.userHome,
267
- ...(input.uid === undefined ? {} : { uid: input.uid }),
268
- });
269
- workerLaunched = true;
270
- const outcome = await dependencies.monitorWorker({
271
- stateRoot: input.local.stateRoot,
272
- backupsRoot: input.local.backupsRoot,
273
- migrationId: operation.migrationId,
274
- environment: operation.environment,
275
- workspaceId: operation.workspaceId,
276
- profile: operation.profile,
277
- ...(operation.migration.ownership === undefined ? {} : operation.migration.ownership),
278
- }, sendStatus);
279
- if (outcome.reopenAdmission)
280
- gate.reopen();
281
- }
282
- catch (error) {
283
- if (!workerLaunched) {
284
- gate.reopen();
285
- const failure = error instanceof Error ? error : new Error(String(error));
286
- throw Object.assign(new Error(failure.message, { cause: failure }), {
287
- code: error.code,
288
- reopenAdmission: true,
289
- });
290
- }
291
- throw error;
292
- }
293
- },
294
- sendStatus,
295
- });
296
- };
297
- const bindServerIdentity = (server) => {
298
- const nextWorkspaceId = UUID.parse(server.workspaceId).toLowerCase();
299
- const nextMachineId = UUID.parse(server.machineId).toLowerCase();
300
- if (workspaceId !== null && workspaceId !== nextWorkspaceId) {
301
- throw new Error("Storage migration server workspace identity changed");
302
- }
303
- if (machineId !== null && machineId !== nextMachineId) {
304
- throw new Error("Storage migration server machine identity changed");
305
- }
306
- if (input.ownership.workspaceId !== nextWorkspaceId
307
- || input.ownership.machineId !== nextMachineId
308
- || input.ownership.environment !== input.environment) {
309
- throw new Error("Storage ownership claim does not match authenticated server identity");
310
- }
311
- const createdAt = dependencies.now().toISOString();
312
- const installation = installationBase(input, nextWorkspaceId, undefined, createdAt);
313
- const layout = storageLayout({
314
- nowworkHome: input.nowworkHome,
315
- environment: input.environment,
316
- workspaceId: nextWorkspaceId,
317
- runtimeBase: input.local.runtimeBase,
318
- platform: input.platform,
319
- });
320
- const nextIdentity = storageHelloIdentity({ installation, layout, ownership: input.ownership });
321
- workspaceId = nextWorkspaceId;
322
- machineId = nextMachineId;
323
- identity = nextIdentity;
324
- ensureControl();
325
- drainPendingControlMessages();
326
- };
327
- const attach = (runtime) => {
328
- gate = runtime.gate;
329
- sendStatus = runtime.sendStatus;
330
- ensureControl();
331
- drainPendingControlMessages();
332
- };
333
- const replayStatus = async (send) => {
334
- const receipt = await latestSourceStorageMigrationReceipt(input.local.backupsRoot, {
335
- environment: input.environment,
336
- profile: input.profile,
337
- });
338
- if (receipt === null)
339
- return;
340
- for (const frame of storageMigrationStatusReplay(receipt))
341
- send(frame);
342
- };
343
- return Object.freeze({
344
- statusOutboxPath: join(input.local.stateRoot, "storage-status-outbox.json"),
345
- identity: () => identity,
346
- bindServerIdentity,
347
- attach,
348
- replayStatus,
349
- controller: Object.freeze({
350
- handle: (raw) => {
351
- if (control !== null && !drainingPendingControlMessages)
352
- return control.handle(raw);
353
- if (pendingControlMessages.length >= 16)
354
- return Promise.resolve(false);
355
- pendingControlMessages.push(raw);
356
- drainPendingControlMessages();
357
- return Promise.resolve(true);
358
- },
359
- }),
360
- });
361
- }
@@ -1,206 +0,0 @@
1
- import { realpathSync } from "node:fs";
2
- import { isAbsolute, resolve } from "node:path";
3
- import { z } from "zod";
4
- import { buildServiceSpec } from "./computer-service.js";
5
- import { profilePath } from "./computer-profile.js";
6
- import { assertManagedServiceIdentity } from "./managed-service-lifecycle.js";
7
- import { managedServiceRegistryPath, readManagedServiceRegistry, } from "./managed-service-registry.js";
8
- import { installationsPath, resolveLocalStorageRoots } from "./storage-config.js";
9
- import { readStorageMigrationPolicy } from "./storage-migration-policy.js";
10
- import { createStorageMigrationRuntimeController, } from "./storage-runtime-controller.js";
11
- import { resolveEnvTag, resolveNowworkHome } from "./storage-layout.js";
12
- import { readStorageOwnershipClaim } from "./storage-ownership.js";
13
- import { resolveDaemonLocalStateRoot } from "./daemon-local-home.js";
14
- const notReady = (reason) => Object.freeze({ ready: false, reason });
15
- function canonicalPath(value) {
16
- const absolute = resolve(value);
17
- try {
18
- return realpathSync.native(absolute);
19
- }
20
- catch (error) {
21
- const code = error.code;
22
- if (code === "ENOTDIR")
23
- return absolute;
24
- if (code !== "ENOENT")
25
- throw error;
26
- }
27
- const missing = [];
28
- let ancestor = absolute;
29
- for (;;) {
30
- const parent = resolve(ancestor, "..");
31
- if (parent === ancestor)
32
- return absolute;
33
- missing.unshift(ancestor.slice(parent.length + (parent.endsWith("/") ? 0 : 1)));
34
- ancestor = parent;
35
- try {
36
- return resolve(realpathSync.native(ancestor), ...missing);
37
- }
38
- catch (error) {
39
- const code = error.code;
40
- if (code === "ENOTDIR")
41
- return absolute;
42
- if (code !== "ENOENT")
43
- throw error;
44
- }
45
- }
46
- }
47
- const samePath = (left, right) => canonicalPath(left) === canonicalPath(right);
48
- function environmentMatchesServer(serverUrl, environment) {
49
- const hostname = new URL(serverUrl).hostname.toLowerCase();
50
- if (hostname === "nowwork-dev.nowcoder.com")
51
- return environment === "dev";
52
- if (hostname === "nowwork-in.nowcoder.com")
53
- return environment === "production";
54
- return true;
55
- }
56
- function findSourceRecord(records, input) {
57
- const matches = records.filter((record) => record.profile === input.profile
58
- && samePath(record.daemonHome, input.currentDaemonHome)
59
- && samePath(record.agentsRoot, input.currentAgentsRoot));
60
- return matches.length === 1 ? matches[0] : null;
61
- }
62
- export async function probeStorageMigrationRuntime(rawInput, injected = {}) {
63
- if (rawInput.platform !== "darwin" && rawInput.platform !== "linux") {
64
- return notReady("platform_not_supported");
65
- }
66
- if (rawInput.platform === "darwin" && rawInput.uid === undefined) {
67
- return notReady("platform_identity_missing");
68
- }
69
- const configuredNowworkHome = rawInput.environment.NOWWORK_HOME;
70
- if (configuredNowworkHome === undefined)
71
- return notReady("nowwork_home_missing");
72
- const input = {
73
- ...rawInput,
74
- profile: z.string().regex(/^[a-z0-9][a-z0-9_-]{0,47}$/u).parse(rawInput.profile),
75
- serverUrl: z.string().url().parse(rawInput.serverUrl),
76
- platform: rawInput.platform,
77
- };
78
- const localHome = resolveDaemonLocalStateRoot({
79
- userHome: rawInput.userHome,
80
- platform: rawInput.platform,
81
- explicit: rawInput.environment.NOWWORK_LOCAL_HOME,
82
- mode: rawInput.environment.NOWWORK_LOCAL_HOME_MODE ?? rawInput.environment.NOWWORK_STORAGE_MODE,
83
- });
84
- let nowworkHome;
85
- let environment;
86
- try {
87
- const home = resolveNowworkHome({
88
- environment: configuredNowworkHome,
89
- userHome: input.userHome,
90
- platform: input.platform,
91
- });
92
- if (home.mode !== "standard")
93
- return notReady("nowwork_home_missing");
94
- nowworkHome = home.root;
95
- environment = resolveEnvTag({
96
- ...(input.environment.NOWWORK_ENV_TAG === undefined
97
- ? {}
98
- : { environment: input.environment.NOWWORK_ENV_TAG }),
99
- }).environment;
100
- }
101
- catch {
102
- return notReady("storage_environment_invalid");
103
- }
104
- if (!environmentMatchesServer(input.serverUrl, environment)) {
105
- return notReady("environment_server_mismatch");
106
- }
107
- const policyPath = input.environment.NOWWORK_STORAGE_POLICY_PATH;
108
- if (policyPath === undefined)
109
- return notReady("migration_policy_missing");
110
- if (!isAbsolute(policyPath) || /[\0\r\n]/u.test(policyPath))
111
- return notReady("migration_policy_invalid");
112
- const dependencies = {
113
- readManagedServices: readManagedServiceRegistry,
114
- assertManagedServiceIdentity,
115
- readPolicy: readStorageMigrationPolicy,
116
- createController: createStorageMigrationRuntimeController,
117
- readOwnership: readStorageOwnershipClaim,
118
- ...injected,
119
- };
120
- let sourceRecord;
121
- try {
122
- const records = await dependencies.readManagedServices({ userHome: input.userHome, localHome });
123
- const found = findSourceRecord(records, input);
124
- if (found === null)
125
- return notReady("source_service_record_missing");
126
- sourceRecord = found;
127
- }
128
- catch {
129
- return notReady("source_service_registry_invalid");
130
- }
131
- const sourceSpec = buildServiceSpec({
132
- platform: input.platform,
133
- profile: input.profile,
134
- userHome: input.userHome,
135
- uid: input.uid,
136
- nodePath: sourceRecord.nodePath,
137
- entryPath: sourceRecord.entryPath,
138
- profileHome: input.currentDaemonHome,
139
- environment: input.environment,
140
- });
141
- const assertSourceIdentity = () => dependencies.assertManagedServiceIdentity({
142
- spec: sourceSpec,
143
- record: sourceRecord,
144
- registry: { userHome: input.userHome, localHome },
145
- });
146
- try {
147
- await assertSourceIdentity();
148
- }
149
- catch {
150
- return notReady("source_service_identity_drift");
151
- }
152
- let policy;
153
- try {
154
- policy = await dependencies.readPolicy(policyPath, {
155
- profile: input.profile,
156
- environment,
157
- nowworkHome,
158
- sourceDaemonHome: sourceRecord.daemonHome,
159
- sourceAgentsRoot: sourceRecord.agentsRoot,
160
- });
161
- }
162
- catch {
163
- return notReady("migration_policy_invalid");
164
- }
165
- let ownership;
166
- try {
167
- ownership = await dependencies.readOwnership(nowworkHome);
168
- }
169
- catch (error) {
170
- return notReady(error.code === "ENOENT"
171
- ? "storage_ownership_claim_missing"
172
- : "storage_ownership_claim_invalid");
173
- }
174
- if (ownership.environment !== environment)
175
- return notReady("storage_ownership_claim_invalid");
176
- const local = resolveLocalStorageRoots({
177
- platform: input.platform,
178
- userHome: input.userHome,
179
- environment: input.environment,
180
- });
181
- const runtime = dependencies.createController({
182
- profile: input.profile,
183
- serverUrl: input.serverUrl,
184
- environment,
185
- nowworkHome,
186
- userHome: input.userHome,
187
- platform: input.platform,
188
- ...(input.uid === undefined ? {} : { uid: input.uid }),
189
- sourceRecord,
190
- local,
191
- policy,
192
- ownership,
193
- serviceEnvironment: {
194
- NOWWORK_HOME: nowworkHome,
195
- NOWWORK_ENV_TAG: environment,
196
- NOWWORK_STORAGE_POLICY_PATH: policyPath,
197
- },
198
- backupArtifacts: [
199
- { name: "profile.json", path: profilePath(input.profile, input.currentDaemonHome) },
200
- { name: "service-descriptor", path: sourceRecord.descriptorPath },
201
- { name: "managed-service-registry.json", path: managedServiceRegistryPath(input.userHome, localHome) },
202
- { name: "installations.json", path: installationsPath(local.configRoot) },
203
- ],
204
- }, { assertSourceIdentity });
205
- return Object.freeze({ ready: true, runtime });
206
- }