@braedonsaunders/appkit-desk 0.10.0 → 0.11.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/README.md CHANGED
@@ -27,6 +27,18 @@ tiers already cover.
27
27
 
28
28
  Size the concurrency cap against screen-open desks, not headless ones.
29
29
 
30
+ Explicit `suspend` accepts optional sidecar hooks: `beforePark` runs after guest
31
+ trim and sync, and `onPaused` runs after the Cloud Hypervisor VM pauses and
32
+ before its memory snapshot. The caller must undo partial preparation on failure,
33
+ bound hook duration, and provide durable recovery across crashes. A hook failure
34
+ is reported to `onPausedError` and ordinary memory parking continues. Backends
35
+ without the paused hook report it unavailable and park normally.
36
+ `onParked(snapshotDir)` runs only after a successful memory snapshot, CPU identity
37
+ write, and file/directory sync, and only if preparation and paused hooks reported
38
+ no failure. Use that positive completion signal to publish a sidecar; `suspend`
39
+ returning or a snapshot directory existing alone does not prove a successful park.
40
+ A publication callback failure is reported without discarding the memory snapshot.
41
+
30
42
  ## Booting a desk
31
43
 
32
44
  ```ts
@@ -292,6 +304,30 @@ DESK_CHECK_TMPDIR=/data/tmp pnpm --filter @braedonsaunders/appkit-desk exec tsx
292
304
 
293
305
  Set `DESK_CHECK_COW=1` to exercise copy-on-write snapshot restore and `DESK_CHECK_BALLOON=1` to check the guest virtio-balloon driver. Set `DESK_CHECK_KERNEL_CMDLINE` for images that require a custom root device. The check creates and removes a disposable VM, overlay, snapshot, TAP and firewall rules. It verifies resident pause and resume, and a guest HTTP stream before and after memory restore; it does not verify the proxy or cross-CPU migration. Choose addresses that do not overlap existing routes. Set `DESK_CHECK_VIRTIOFSD` to a `virtiofsd` binary to also check a read-only share: the guest mounts it, a write fails, and a file held open across the park reads back after the restore.
294
306
 
307
+ The check uses a unique desk ID and private runtime directory. Set `DESK_CHECK_CID`
308
+ to a free guest CID when testing alongside other hosts. Put `DESK_CHECK_TMPDIR` on
309
+ the same reflink-capable filesystem as the base image to avoid a full disk copy.
310
+ Run inside a disposable network namespace when testing on a machine with an active
311
+ host. `DESK_CHECK_VMM` selects a test binary without changing the system VMM.
312
+
313
+ With the [pinned incremental build](vmm/README.md), set `DESK_CHECK_INCREMENTAL=1`
314
+ and `DESK_CHECK_COW=1` on XFS or Btrfs. This retains a hard link to the original
315
+ memory file, verifies its digest after another park, checks shared filesystem
316
+ extents, and confirms the guest HTTP process survives a second restore. It uses
317
+ `filefrag` from e2fsprogs. A failed VMM stop leaves the test files in place for
318
+ recovery instead of deleting backing files from a live process. The check does
319
+ not measure latency or memory savings.
320
+
321
+ `memoryRestoreMode: 'ondemand'` selects Cloud Hypervisor's userfaultfd page-on-access
322
+ restore instead of its default eager copy. It requires Linux 6.6 or newer and
323
+ permission to open `/dev/userfaultfd` or use the corresponding syscall. It rejects
324
+ virtio-fs shares and does not silently cold boot when restore is unavailable. The
325
+ source snapshot is durably retired before boot and retained until the VMM stops;
326
+ an interrupted restore leaves a durable fence requiring operator recovery. The
327
+ mode can be selected per host or per desk and is separate from copy-on-write
328
+ restore: it does not share CoW pages or enable incremental snapshot writes.
329
+ Run the disposable check with `DESK_CHECK_RESTORE_MODE=ondemand` to exercise it.
330
+
295
331
  ## Host directory shares
296
332
 
297
333
  A desk can mount host directories over virtio-fs. Pass `shares` to
@@ -431,3 +467,84 @@ and its path immutable and available for as long as a desk or its snapshot uses
431
467
  AppKit hashes each image on start and records the attachment list with the snapshot;
432
468
  restore refuses a changed list before resuming. Virtualization.framework rejects
433
469
  attachments. Device support and filesystem mount options depend on the guest kernel.
470
+
471
+ Cloud Hypervisor backends accept `inheritedFds`, a list of open descriptors to
472
+ retain in the VMM and each share daemon, mapped to consecutive child descriptors
473
+ starting at 3. Keep each descriptor open until the backend has stopped its
474
+ children. For example, a host can retain an advisory lock in those processes
475
+ so that an orphaned VMM continues to fence offline disk operations. The caller
476
+ is responsible for acquiring and validating the lock and for ensuring the
477
+ executables do not close the inherited descriptors. Run
478
+ `python3 packages/desk/scripts/check-inherited-lock.py` from the repository to
479
+ check descriptor inheritance and release with disposable processes.
480
+ ### Snapshot memory pins
481
+
482
+ `pinTemplateMemory(source, destination, config)` creates an exclusive child
483
+ snapshot directory with a hard link to the source `memory-ranges` inode and
484
+ private `state.json` and `config.json` files. `assertPinnedTemplateMemory`
485
+ checks the publication marker, source inode, and read-only mode before use.
486
+ Source and destination must be on the same filesystem. The caller must keep
487
+ the memory bytes unchanged for the lifetime of every child and prevent all
488
+ writers, including processes with permission to change the file mode. Removing
489
+ the source pathname does not invalidate an existing hard link.
490
+
491
+ These filesystem operations alone do not establish that a snapshot is safe to
492
+ clone. A caller must independently validate its contents and compatibility,
493
+ prepare independent disk and device identities, fence the guest until it has
494
+ been reinitialized, and retain each memory pin while its VM can use it. A
495
+ partially published destination is left in place after an interruption and
496
+ must not be restored. Remove it only after verifying no VM is using it.
497
+
498
+ `planTemplateConfig` checks a snapshot config for a restricted Cloud Hypervisor
499
+ copy-on-write configuration and produces a child-specific copy with distinct
500
+ disk, TAP, MAC and vsock endpoints. It does not create a disk or establish
501
+ that any of those identities are globally unique. Cloud Hypervisor restores
502
+ virtio-net configuration from saved device state, so the guest must also change
503
+ its live MAC under an external network fence before traffic is allowed.
504
+ Read-only attachments retain their paths; the caller must verify their contents.
505
+
506
+ `strictMemoryRestore` refuses a missing or mismatched saved CPU identity and
507
+ refuses a cold-boot fallback when a restore fails. Before restore it writes a
508
+ durable `.restore-pending` marker beside the snapshot. The marker stays until a
509
+ later memory park has completed and its snapshot files are synced. If the host
510
+ restarts with the marker present, wake fails and requires operator recovery;
511
+ the disk and retired source are not automatically deleted. This is a lifecycle
512
+ fence, not a template registration or guest identity reset.
513
+
514
+ `registerDeskTemplate` records the hashes of a stopped, read-only template disk,
515
+ snapshot files, kernel, initramfs and read-only attachments together with its VMM
516
+ and CPU identities. The caller must supply actual VMM and CPU identities and
517
+ prepare the source in isolation without user data or credentials. Hashing cannot
518
+ establish that provenance. The source and its memory inode must be protected
519
+ against all writers for the lifetime of its descendants.
520
+
521
+ `forkDeskTemplate` checks the registration and actual file bytes, reserves a new
522
+ private directory, copies the template disk into an independent writable file,
523
+ and pins the same snapshot memory inode in the child directory for its lifetime,
524
+ including while parked. Its returned disk and snapshot are
525
+ prepared artifacts, not a running VM. A caller must verify `.prepared`, establish
526
+ an external network and workload fence before restore, reinitialize the guest's
527
+ identity and entropy, and only then allow traffic or guest workloads. An
528
+ interrupted preparation leaves a directory without `.prepared`; it must not be
529
+ restored. Retain and inspect it before cleanup.
530
+
531
+ A caller can pass `template` in `DeskStartOptions` on a Cloud Hypervisor host
532
+ with memory parking enabled. This opts that desk into strict copy-on-write restore
533
+ without changing the restore mode of other desks. Its `holdFence` callback establishes the external network fence before
534
+ preparation or restore, including when preparation was interrupted. Its `prepare`
535
+ callback receives the assigned launch plan and creates the disk and snapshot. AppKit verifies their device and resource identities against that
536
+ plan. After restore, `initialize` runs before the desk handle is exposed;
537
+ only after it succeeds and the initialization marker is durable does
538
+ `releaseFence` run. If initialization fails, AppKit stops the guest, preserves
539
+ the interruption marker and does not release the fence. The caller is
540
+ responsible for ensuring the source has no running user workloads at capture
541
+ and for resetting guest entropy and identity in `initialize`.
542
+
543
+ A caller may supply `vsockCid` on a start to use a stable guest CID. AppKit
544
+ rejects collisions among CIDs allocated by the same host instance; the caller
545
+ must also keep that allocation unique across host instances and restarts.
546
+
547
+ A descendant may select strict memory handling and copy-on-write restore per
548
+ start without requesting template initialization again. Explicit `shutdown`
549
+ stops that known VM, discards its memory and clears its interruption marker;
550
+ this permits an intentional cold restart of its existing disk.
package/backend.d.ts CHANGED
@@ -53,6 +53,7 @@ export type DeskConnectionChange = {
53
53
  };
54
54
  /** A booted microVM as the host sees it: one desk, one guest-agent channel. */
55
55
  export interface DeskMachine {
56
+ supportsPausedParkHook?: true;
56
57
  readonly deskId: string;
57
58
  /**
58
59
  * Send one request to the in-guest agent and await its correlated result.
@@ -118,7 +119,13 @@ export interface DeskMachine {
118
119
  * parked without a prior `sync` is consistent only *with that snapshot* —
119
120
  * which matters the moment the snapshot is discarded.
120
121
  */
121
- park?(snapshotDir: string): Promise<void>;
122
+ park?(snapshotDir: string, hooks?: DeskParkHooks): Promise<void>;
123
+ }
124
+ export interface DeskParkHooks {
125
+ /** Called while the VM is paused, before its memory snapshot is written. */
126
+ onPaused?: () => Promise<void>;
127
+ /** Reports a sidecar failure; ordinary memory parking continues. */
128
+ onPausedError?: (error: unknown) => void;
122
129
  }
123
130
  export interface DeskBootHooks {
124
131
  /**
@@ -151,8 +158,13 @@ export interface DeskBackend {
151
158
  }
152
159
  export type DeskProcessLauncher = (command: string, args: readonly string[], options: {
153
160
  identity?: DeskLauncherIdentity;
161
+ inheritedFds?: readonly number[];
154
162
  }) => ChildProcess;
155
163
  export interface CloudHypervisorBackendOptions {
164
+ /** VMM API transport, injectable for tests. */
165
+ vmmApi?: VmmApiCall;
166
+ /** Descriptors retained by the VMM and share daemons, mapped consecutively from fd 3. */
167
+ inheritedFds?: readonly number[];
156
168
  /** Spawns the VMM and image tooling; injectable for tests. */
157
169
  launcher?: DeskProcessLauncher;
158
170
  /** Opens the host side of the vsock Unix socket; injectable for tests. */
@@ -191,9 +203,13 @@ export interface CloudHypervisorBackendOptions {
191
203
  serviceVsock?: boolean;
192
204
  /** Whether a share daemon's socket exists yet; injectable for tests. */
193
205
  pathExists?: (path: string) => boolean;
194
- /** Removes a stale share socket before its daemon starts; injectable for tests. */
206
+ /** Removes a stale share or vsock socket before the process that binds it starts; injectable for tests. */
195
207
  removeFile?: (path: string) => Promise<void>;
208
+ /** Whether a process listens on a Unix socket (a running VMM on its vsock socket); injectable for tests. */
209
+ socketListening?: (path: string) => Promise<boolean>;
196
210
  }
211
+ /** Whether something accepts connections on the Unix socket at `path`: false when nothing is there or nothing listens. */
212
+ export declare function unixSocketListening(path: string): Promise<boolean>;
197
213
  export declare function createCloudHypervisorBackend(options?: CloudHypervisorBackendOptions): DeskBackend;
198
214
  /**
199
215
  * Cloud Hypervisor's control plane: HTTP/1.1 over the `--api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I,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACrD;AAED,0HAA0H;AAC1H,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAalE;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,6BAAkC,GAC1C,WAAW,CAsNb;AASD;;;;;;;;;GASG;AACH,MAAM,MAAM,UAAU,GAAG,CACvB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,KACnB,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,CAAA;AAE3B,yFAAyF;AACzF,wBAAsB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAWpH;AAqED,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,EAAE,WAEpC,CAAA"}
package/backend.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { randomUUID } from 'node:crypto';
2
+ import { assertPrivateMemoryMapping } from './template-mapping.js';
2
3
  import { existsSync } from 'node:fs';
3
4
  import { rm } from 'node:fs/promises';
4
5
  import { request as httpRequest } from 'node:http';
6
+ import { createConnection } from 'node:net';
5
7
  import { GUEST_AGENT_VSOCK_PORT } from './protocol.js';
6
8
  import { DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_RETRY_DELAY_MS, DEFAULT_HANDSHAKE_TIMEOUT_MS, DEFAULT_RECONNECT_WINDOW_MS, DEFAULT_RECONNECT_RETRY_DELAY_MS, DEFAULT_KILL_GRACE_MS, RECONNECT_BACKOFF_CEILING, connectWithRetry, createGuestMachine, attemptHandshake, defaultLauncher, defaultConnect, drainOutput, watchExit, terminate, runToCompletion, errorMessage, assertShareManifest, writeShareManifest, } from './machine.js';
7
9
  import { DeskError, } from './plan.js';
@@ -19,7 +21,25 @@ import { DeskError, } from './plan.js';
19
21
  export class DeskRequestFateUnknownError extends DeskError {
20
22
  name = 'DeskRequestFateUnknownError';
21
23
  }
24
+ /** Whether something accepts connections on the Unix socket at `path`: false when nothing is there or nothing listens. */
25
+ export function unixSocketListening(path) {
26
+ return new Promise((resolvePromise, rejectPromise) => {
27
+ const socket = createConnection(path);
28
+ socket.once('connect', () => {
29
+ socket.destroy();
30
+ resolvePromise(true);
31
+ });
32
+ socket.once('error', (error) => {
33
+ socket.destroy();
34
+ if (error.code === 'ECONNREFUSED' || error.code === 'ENOENT')
35
+ resolvePromise(false);
36
+ else
37
+ rejectPromise(error);
38
+ });
39
+ });
40
+ }
22
41
  export function createCloudHypervisorBackend(options = {}) {
42
+ const api = options.vmmApi ?? vmmApi;
23
43
  const launcher = options.launcher ?? defaultLauncher;
24
44
  const connect = options.connect ?? defaultConnect;
25
45
  const platform = options.platform ?? process.platform;
@@ -35,6 +55,7 @@ export function createCloudHypervisorBackend(options = {}) {
35
55
  const guestAgentPort = options.guestAgentPort ?? GUEST_AGENT_VSOCK_PORT;
36
56
  const pathExists = options.pathExists ?? existsSync;
37
57
  const removeFile = options.removeFile ?? ((path) => rm(path, { force: true }));
58
+ const socketListening = options.socketListening ?? unixSocketListening;
38
59
  /**
39
60
  * Start one `virtiofsd` per share and wait until every socket exists.
40
61
  *
@@ -53,7 +74,7 @@ export function createCloudHypervisorBackend(options = {}) {
53
74
  try {
54
75
  for (const share of plan.shares) {
55
76
  await removeFile(share.socketPath);
56
- const child = launcher(share.daemon.command, share.daemon.args, { identity: plan.launcherIdentity });
77
+ const child = launcher(share.daemon.command, share.daemon.args, { identity: plan.launcherIdentity, inheritedFds: options.inheritedFds });
57
78
  daemons.push({ tag: share.tag, child, exit: watchExit(child), log: drainOutput(child) });
58
79
  }
59
80
  const deadline = now() + connectTimeoutMs;
@@ -109,6 +130,13 @@ export function createCloudHypervisorBackend(options = {}) {
109
130
  throw new DeskError(`Desk ${plan.deskId} could not be restored from ${plan.snapshot.dir}: ${errorMessage(error)}`);
110
131
  }
111
132
  }
133
+ // Cloud Hypervisor binds the vsock's host socket and fails with "Address in use" when a
134
+ // VMM that did not exit cleanly left it behind. A socket nothing listens on is that
135
+ // leftover and is removed; one a VMM still serves is this desk running already.
136
+ if (await socketListening(plan.vsock.socketPath)) {
137
+ throw new DeskError(`Desk ${plan.deskId} is already running: a VMM is listening on ${plan.vsock.socketPath}.`);
138
+ }
139
+ await removeFile(plan.vsock.socketPath);
112
140
  let shares;
113
141
  try {
114
142
  shares = await startShareDaemons(plan);
@@ -116,7 +144,7 @@ export function createCloudHypervisorBackend(options = {}) {
116
144
  catch (error) {
117
145
  throw new DeskError(`Desk ${plan.deskId} could not start its shares: ${errorMessage(error)}`);
118
146
  }
119
- const child = launcher(plan.vmm.command, plan.vmm.args, { identity: plan.launcherIdentity });
147
+ const child = launcher(plan.vmm.command, plan.vmm.args, { identity: plan.launcherIdentity, inheritedFds: options.inheritedFds });
120
148
  const log = drainOutput(child);
121
149
  const exit = watchExit(child);
122
150
  shares.supervise(child, exit);
@@ -136,9 +164,11 @@ export function createCloudHypervisorBackend(options = {}) {
136
164
  // guest still cannot write, then start it.
137
165
  if (plan.snapshot.restoring) {
138
166
  try {
139
- await waitForVmm(vmmApi, plan.api.socketPath, { now, exited: exit, timeoutMs: connectTimeoutMs });
167
+ await waitForVmm(api, plan.api.socketPath, { now, exited: exit, timeoutMs: connectTimeoutMs });
168
+ if (plan.snapshot.requirePrivateMemoryMapping)
169
+ await assertPrivateMemoryMapping(child.pid, plan.snapshot.dir);
140
170
  await hooks?.onRestored?.();
141
- await vmmApi(plan.api.socketPath, '/api/v1/vm.resume');
171
+ await api(plan.api.socketPath, '/api/v1/vm.resume');
142
172
  }
143
173
  catch (error) {
144
174
  await stop();
@@ -198,12 +228,22 @@ export function createCloudHypervisorBackend(options = {}) {
198
228
  return attemptHandshake(connect, plan.vsock.socketPath, port, handshakeTimeoutMs);
199
229
  } } : {}),
200
230
  stop,
201
- pause: () => setVmmState(vmmApi, plan.api.socketPath, 'Paused'),
202
- resume: () => setVmmState(vmmApi, plan.api.socketPath, 'Running'),
203
- park: async (snapshotDir) => {
231
+ pause: () => setVmmState(api, plan.api.socketPath, 'Paused'),
232
+ resume: () => setVmmState(api, plan.api.socketPath, 'Running'),
233
+ supportsPausedParkHook: true,
234
+ park: async (snapshotDir, hooks) => {
204
235
  try {
205
- await vmmApi(plan.api.socketPath, '/api/v1/vm.pause');
206
- await vmmApi(plan.api.socketPath, '/api/v1/vm.snapshot', { destination_url: `file://${snapshotDir}` });
236
+ await api(plan.api.socketPath, '/api/v1/vm.pause');
237
+ try {
238
+ await hooks?.onPaused?.();
239
+ }
240
+ catch (error) {
241
+ try {
242
+ hooks?.onPausedError?.(error);
243
+ }
244
+ catch { /* preserve memory */ }
245
+ }
246
+ await api(plan.api.socketPath, '/api/v1/vm.snapshot', { destination_url: `file://${snapshotDir}` });
207
247
  await writeShareManifest(snapshotDir, plan.shares);
208
248
  }
209
249
  finally {
package/backend.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AACrC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,WAAW,CAAA;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,EACL,0BAA0B,EAAE,0BAA0B,EAAE,8BAA8B,EACtF,4BAA4B,EAAE,2BAA2B,EAAE,gCAAgC,EAC3F,qBAAqB,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,kBAAkB,E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CAAC,UAAU,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAA;IACnE,CAAC;AACH,CAAC;AAED,2EAA2E;AAC3E,MAAM,CAAC,MAAM,sBAAsB,GAAgB;IACjD,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,4BAA4B,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;CACxE,CAAA","sourcesContent":["import { randomUUID } from 'node:crypto'\nimport { existsSync } from 'node:fs'\nimport { rm } from 'node:fs/promises'\nimport { request as httpRequest } from 'node:http'\nimport { GUEST_AGENT_VSOCK_PORT } from './protocol'\nimport {\n DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_RETRY_DELAY_MS,\n DEFAULT_HANDSHAKE_TIMEOUT_MS, DEFAULT_RECONNECT_WINDOW_MS, DEFAULT_RECONNECT_RETRY_DELAY_MS,\n DEFAULT_KILL_GRACE_MS, RECONNECT_BACKOFF_CEILING, connectWithRetry, createGuestMachine, attemptHandshake,\n defaultLauncher, defaultConnect, drainOutput, watchExit, terminate, runToCompletion, errorMessage,\n assertShareManifest, writeShareManifest, type ExitWatcher,\n} from './machine'\n/**\n * The backend port.\n *\n * `DeskBackend` is the seam that keeps the package testable without a\n * hypervisor: the host asks a backend to boot a `DeskLaunchPlan` and gets a\n * `DeskMachine` — a request/response channel to the in-guest agent plus an\n * event subscription. CI and non-KVM developer machines inject an in-memory\n * fake; production uses `cloudHypervisorBackend` on Linux or\n * `createVirtualizationBackend` on an Apple silicon Mac, whose process glue is\n * deliberately thin because everything that can be pure lives in the plan\n * builder, the protocol module, and the shared guest channel.\n */\nimport type { ChildProcess } from 'node:child_process'\nimport type { Duplex } from 'node:stream'\nimport {\n DeskError, type CloudHypervisorLaunchPlan, type DeskHypervisor, type DeskLaunchPlan, type DeskLauncherIdentity,\n} from './plan'\nimport type { GuestCommand, GuestEventMessage } from './protocol'\n\n/**\n * A request that was in flight when the guest connection dropped.\n *\n * Distinct from a plain `DeskError` because the caller's next move is\n * different: this request may ALREADY HAVE RUN. The frame reached the guest,\n * and whether the guest executed it before the channel went away is not\n * knowable from the host. An `exec` may have sent mail; a `click` may have\n * landed. So the transport refuses to guess — it names the ambiguity and lets\n * the caller decide whether the operation is safe to repeat. Anything that\n * rejects with a plain `DeskError` instead never reached the guest at all.\n */\nexport class DeskRequestFateUnknownError extends DeskError {\n override readonly name: string = 'DeskRequestFateUnknownError'\n}\n\n/**\n * What happened to the transport underneath a machine.\n *\n * `reconnected` means the host lost the guest channel mid-lease and got it\n * back; the desk is usable again. It is NOT a promise that the guest is\n * unchanged — see the note on `onConnectionChange`. `lost` means it will not\n * come back on this VM and the desk needs a fresh boot.\n */\nexport type DeskConnectionChange =\n | {\n state: 'reconnected'\n deskId: string\n /** How the previous connection went away. */\n reason: string\n /** Wall-clock time the desk spent with no channel to its guest. */\n downtimeMs: number\n /** Reconnects this machine has survived, including this one. */\n reconnects: number\n }\n | {\n state: 'lost'\n deskId: string\n /** Why it is gone for good: the VMM exited, or the window ran out. */\n reason: string\n }\n\n/** A booted microVM as the host sees it: one desk, one guest-agent channel. */\nexport interface DeskMachine {\n readonly deskId: string\n /**\n * Send one request to the in-guest agent and await its correlated result.\n *\n * Rejects with `DeskError` when the guest reports an error, the request\n * times out, or the connection is gone for good — in that last case the\n * request never left the host, so repeating it is safe.\n *\n * A connection lost mid-lease is re-established rather than fatal: a call\n * that arrives while that is happening WAITS for it instead of failing.\n * A call that was already in flight when the channel dropped rejects with\n * `DeskRequestFateUnknownError`, because the host cannot tell whether the\n * guest ran it.\n */\n request(command: GuestCommand, options?: { timeoutMs?: number }): Promise<unknown>\n /**\n * Subscribe to unsolicited guest events (job exits, frames, focus changes).\n *\n * Subscriptions belong to the machine, not to the socket under it, so they\n * survive a reconnect. What does NOT survive is the guest's own capture\n * state: a guest agent that restarted has no screen, no frame encoder and\n * no video encoder, and a subscriber will simply stop receiving. Watch\n * `onConnectionChange` to notice.\n */\n subscribe(listener: (event: GuestEventMessage) => void): () => void\n /**\n * Watch the transport under this machine.\n *\n * Optional: a backend whose channel cannot drop has nothing to report. The\n * default backend reports every reconnect, because a reconnect that leaves\n * no trace turns a guest agent restarting every few minutes into a desk that\n * misbehaves for reasons nobody can find.\n */\n onConnectionChange?(listener: (change: DeskConnectionChange) => void): () => void\n /**\n * Open a TCP stream from the host to `port` on the guest.\n *\n * Optional: a backend whose guest has a routable address (Cloud Hypervisor's\n * TAP) leaves this out and the host dials the guest address directly. A\n * backend whose guest sits behind a userspace NAT (Virtualization.framework)\n * provides it, because there is no address on the Mac to dial. Rejects with\n * `DeskError` when the guest refuses or does not answer.\n */\n connect?(port: number): Promise<Duplex>\n /** Optional resident pause and resume. Callers must serialize these with lifecycle operations. */\n pause?(): Promise<void>\n resume?(): Promise<void>\n /** Stop the VM. Idempotent; resolves once the VMM process has exited. */\n shutdown(): Promise<void>\n /**\n * Park the VM with its memory, so resuming continues the process tree.\n *\n * Optional: a backend with no snapshot facility simply does not offer it, and\n * the host falls back to `shutdown()`. Pauses the guest, writes a snapshot to\n * `snapshotDir`, then stops the VMM — and resolves only once all three have\n * happened, because a caller that believed a snapshot exists when it does not\n * would later refuse to boot the desk at all.\n *\n * The caller is responsible for quiescing the guest filesystem first. A paused\n * guest still holds dirty pages that exist only in the snapshot, so a disk\n * parked without a prior `sync` is consistent only *with that snapshot* —\n * which matters the moment the snapshot is discarded.\n */\n park?(snapshotDir: string): Promise<void>\n}\n\nexport interface DeskBootHooks {\n /**\n * Called on a restore, after the snapshot has been read back and before the\n * guest is allowed to run.\n *\n * The snapshot must no longer be available for replay by this point. A memory image\n * is valid only against the disk it was taken with, so once the guest resumes\n * and writes, any surviving snapshot describes a past that no longer matches —\n * and replaying it later would corrupt the desk. Restoring paused and\n * invalidating here is what makes that impossible rather than unlikely.\n *\n * Throwing aborts the restore: the VM is stopped and the caller is free to\n * cold boot, which is safe because a paused guest has written nothing.\n */\n onRestored?: () => Promise<void>\n}\n\nexport interface DeskBackend {\n /**\n * Which hypervisor this backend boots, and so which plan the host builds\n * for it. Absent means Cloud Hypervisor.\n */\n readonly hypervisor?: DeskHypervisor\n /**\n * The binary the backend launches — the VMM or the helper — for readiness\n * reporting. Optional; `verifyDeskHost` falls back to its own option.\n */\n readonly vmmPath?: string\n boot(plan: DeskLaunchPlan, hooks?: DeskBootHooks): Promise<DeskMachine>\n}\n\nexport type DeskProcessLauncher = (\n command: string,\n args: readonly string[],\n options: { identity?: DeskLauncherIdentity },\n) => ChildProcess\n\nexport interface CloudHypervisorBackendOptions {\n /** Spawns the VMM and image tooling; injectable for tests. */\n launcher?: DeskProcessLauncher\n /** Opens the host side of the vsock Unix socket; injectable for tests. */\n connect?: (socketPath: string) => Duplex\n platform?: NodeJS.Platform\n /**\n * Clock behind every connect and reconnect deadline. Injectable for the same\n * reason `DeskHostOptions.now` is: a test drives a window to its end by\n * moving the clock, rather than sleeping for it and hoping the machine it\n * runs on is fast enough.\n */\n now?: () => number\n idFactory?: () => string\n requestTimeoutMs?: number\n connectTimeoutMs?: number\n connectRetryDelayMs?: number\n /** Bound on a single handshake attempt; see attemptHandshake. */\n handshakeTimeoutMs?: number\n /**\n * How long a machine keeps trying to get its guest channel back after an\n * unexpected drop before declaring the desk lost. Bounded on purpose: the\n * VMM is still alive so a reconnect is cheap and usually fast, but retrying\n * forever against a guest that is never coming back is worse than failing.\n */\n reconnectWindowMs?: number\n /**\n * First delay between reconnect attempts. It doubles toward a ceiling, so\n * this is the fast first poll rather than the steady rate. Separate from\n * `connectRetryDelayMs`, which is sized for a guest that is still cold\n * booting; here the guest was up a moment ago.\n */\n reconnectRetryDelayMs?: number\n killGraceMs?: number\n guestAgentPort?: number\n /** Use guest-provided vsock service bridges for DeskHandle.connect. Opt in only when the image provides them. */\n serviceVsock?: boolean\n /** Whether a share daemon's socket exists yet; injectable for tests. */\n pathExists?: (path: string) => boolean\n /** Removes a stale share socket before its daemon starts; injectable for tests. */\n removeFile?: (path: string) => Promise<void>\n}\n\nexport function createCloudHypervisorBackend(\n options: CloudHypervisorBackendOptions = {},\n): DeskBackend {\n const launcher = options.launcher ?? defaultLauncher\n const connect = options.connect ?? defaultConnect\n const platform = options.platform ?? process.platform\n const now = options.now ?? Date.now\n const idFactory = options.idFactory ?? randomUUID\n const requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS\n const connectTimeoutMs = options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS\n const connectRetryDelayMs = options.connectRetryDelayMs ?? DEFAULT_CONNECT_RETRY_DELAY_MS\n const handshakeTimeoutMs = options.handshakeTimeoutMs ?? DEFAULT_HANDSHAKE_TIMEOUT_MS\n const reconnectWindowMs = options.reconnectWindowMs ?? DEFAULT_RECONNECT_WINDOW_MS\n const reconnectRetryDelayMs = options.reconnectRetryDelayMs ?? DEFAULT_RECONNECT_RETRY_DELAY_MS\n const killGraceMs = options.killGraceMs ?? DEFAULT_KILL_GRACE_MS\n const guestAgentPort = options.guestAgentPort ?? GUEST_AGENT_VSOCK_PORT\n const pathExists = options.pathExists ?? existsSync\n const removeFile = options.removeFile ?? ((path: string) => rm(path, { force: true }))\n\n /**\n * Start one `virtiofsd` per share and wait until every socket exists.\n *\n * The VMM connects to these at boot and at restore alike, so they come up\n * first. A stale socket is removed beforehand, or its presence would read as\n * a daemon that is listening. Existence is the readiness signal rather than\n * a probe connection, because `virtiofsd` serves exactly one connection and\n * exits when it closes; the moment between bind and listen is covered by\n * Cloud Hypervisor retrying a refused vhost-user connect.\n */\n async function startShareDaemons(plan: CloudHypervisorLaunchPlan): Promise<ShareDaemons> {\n const daemons: { tag: string; child: ChildProcess; exit: ExitWatcher; log: { tail: () => string } }[] = []\n const stop = async () => {\n await Promise.all(daemons.map((daemon) => terminate(daemon.child, daemon.exit, killGraceMs)))\n }\n try {\n for (const share of plan.shares) {\n await removeFile(share.socketPath)\n const child = launcher(share.daemon.command, share.daemon.args, { identity: plan.launcherIdentity })\n daemons.push({ tag: share.tag, child, exit: watchExit(child), log: drainOutput(child) })\n }\n const deadline = now() + connectTimeoutMs\n for (;;) {\n const exited = daemons.find((daemon) => daemon.exit.done)\n if (exited) {\n const said = exited.log.tail()\n throw new DeskError(`virtiofsd for share ${exited.tag} exited before it listened${said === '' ? '.' : `: ${said}`}`)\n }\n const waiting = plan.shares.filter((share) => !pathExists(share.socketPath))\n if (waiting.length === 0) break\n if (now() >= deadline) {\n throw new DeskError(\n `virtiofsd did not create ${waiting.map((share) => share.socketPath).join(', ')} in ${connectTimeoutMs}ms.`,\n )\n }\n // A held timer: the wait is bounded by the deadline, and nothing else may be keeping\n // the process alive while a daemon starts.\n await new Promise<void>((resolvePromise) => setTimeout(resolvePromise, connectRetryDelayMs))\n }\n } catch (error) {\n await stop()\n throw error\n }\n return {\n stop,\n // The VMM cannot serve a share whose daemon is gone; neither can it be\n // handed a new one mid-run. So a daemon that dies stops the VMM, and the\n // desk is lost loudly instead of hanging every guest access to the share.\n supervise: (vmm, vmmExit) => {\n for (const daemon of daemons) {\n void daemon.exit.promise.then(() => terminate(vmm, vmmExit, killGraceMs))\n }\n },\n }\n }\n\n async function boot(plan: DeskLaunchPlan, hooks?: DeskBootHooks): Promise<DeskMachine> {\n if (plan.hypervisor !== 'cloud-hypervisor') throw new DeskError('Cloud Hypervisor requires a cloud-hypervisor plan.')\n if (platform !== 'linux') {\n throw new DeskError(`The AppKit desk requires Linux; received ${platform}.`)\n }\n if (plan.overlay.create) {\n await runToCompletion(\n launcher(plan.overlay.create.command, plan.overlay.create.args, {\n identity: plan.launcherIdentity,\n }),\n `overlay creation for desk ${plan.deskId}`,\n )\n }\n if (plan.snapshot.restoring) {\n try {\n await assertShareManifest(plan.snapshot.dir, plan.shares)\n } catch (error) {\n throw new DeskError(`Desk ${plan.deskId} could not be restored from ${plan.snapshot.dir}: ${errorMessage(error)}`)\n }\n }\n\n let shares: ShareDaemons\n try {\n shares = await startShareDaemons(plan)\n } catch (error) {\n throw new DeskError(`Desk ${plan.deskId} could not start its shares: ${errorMessage(error)}`)\n }\n const child = launcher(plan.vmm.command, plan.vmm.args, { identity: plan.launcherIdentity })\n const log = drainOutput(child)\n const exit = watchExit(child)\n shares.supervise(child, exit)\n // The VMM first: a daemon outliving it is harmless, while a daemon stopped\n // under a running VMM is a share failing in the guest.\n const stop = async () => {\n try {\n await terminate(child, exit, killGraceMs)\n } finally {\n await shares.stop()\n }\n }\n\n // A restored guest comes back PAUSED (see the `resume=false` reasoning in\n // the plan). Three things have to happen in this order and nowhere else:\n // prove the restore finished, let the caller invalidate replay while the\n // guest still cannot write, then start it.\n if (plan.snapshot.restoring) {\n try {\n await waitForVmm(vmmApi, plan.api.socketPath, { now, exited: exit, timeoutMs: connectTimeoutMs })\n await hooks?.onRestored?.()\n await vmmApi(plan.api.socketPath, '/api/v1/vm.resume')\n } catch (error) {\n await stop()\n const said = log.tail()\n const detail = said === '' ? '' : ` The VMM said: ${said}`\n throw new DeskError(\n `Desk ${plan.deskId} could not be restored from ${plan.snapshot.dir}: ${errorMessage(error)}${detail}`,\n )\n }\n }\n\n let stream: Duplex\n try {\n stream = await connectWithRetry({\n connect,\n now,\n socketPath: plan.vsock.socketPath,\n port: guestAgentPort,\n timeoutMs: connectTimeoutMs,\n retryDelayMs: connectRetryDelayMs,\n attemptTimeoutMs: handshakeTimeoutMs,\n exited: exit,\n })\n } catch (error) {\n await stop()\n const said = log.tail()\n const detail = said === '' ? '' : ` The VMM said: ${said}`\n throw error instanceof DeskError\n ? new DeskError(`${error.message}${detail}`)\n : new DeskError(\n `Could not reach the guest agent for desk ${plan.deskId}: ${errorMessage(error)}${detail}`,\n )\n }\n return createGuestMachine({\n deskId: plan.deskId,\n exit,\n stream,\n // The same path the first connection took, deliberately: a reconnect\n // that skipped confirmGuest would adopt the very hollow handshake the\n // boot path learned not to trust.\n reconnect: (abandoned) =>\n connectWithRetry({\n connect,\n now,\n socketPath: plan.vsock.socketPath,\n port: guestAgentPort,\n timeoutMs: reconnectWindowMs,\n retryDelayMs: reconnectRetryDelayMs,\n maxRetryDelayMs: reconnectRetryDelayMs * RECONNECT_BACKOFF_CEILING,\n attemptTimeoutMs: handshakeTimeoutMs,\n exited: exit,\n exitedMessage: `the VMM for desk ${plan.deskId} exited.`,\n abandoned,\n }),\n reconnectWindowMs,\n now,\n idFactory,\n requestTimeoutMs,\n ...(options.serviceVsock ? { connect: (port: number) => {\n if (!Number.isInteger(port) || port < 1 || port > 65535) throw new DeskError('Guest port must be between 1 and 65535.')\n return attemptHandshake(connect, plan.vsock.socketPath, port, handshakeTimeoutMs)\n } } : {}),\n stop,\n pause: () => setVmmState(vmmApi, plan.api.socketPath, 'Paused'),\n resume: () => setVmmState(vmmApi, plan.api.socketPath, 'Running'),\n park: async (snapshotDir) => {\n try {\n await vmmApi(plan.api.socketPath, '/api/v1/vm.pause')\n await vmmApi(plan.api.socketPath, '/api/v1/vm.snapshot', { destination_url: `file://${snapshotDir}` })\n await writeShareManifest(snapshotDir, plan.shares)\n } finally {\n await stop()\n }\n },\n })\n }\n\n return { boot }\n}\n\ninterface ShareDaemons {\n /** Stop every daemon; resolves once all have exited. */\n stop: () => Promise<void>\n /** Stop the VMM if any daemon exits while it runs. */\n supervise: (vmm: ChildProcess, vmmExit: ExitWatcher) => void\n}\n\n/**\n * Cloud Hypervisor's control plane: HTTP/1.1 over the `--api-socket` Unix\n * socket. The plan has always created that socket; until now nothing spoke to\n * it, because stopping a VM needed nothing more than killing the process.\n *\n * Pausing and snapshotting do need it, and `ch-remote` — the tool that usually\n * does this — is not in the runner image. Writing the two requests by hand is a\n * smaller change than adding a binary to the image, and keeps the whole\n * lifecycle inside one process that already owns the socket path.\n */\nexport type VmmApiCall = (\n socketPath: string,\n path: string,\n body?: Record<string, unknown>,\n method?: 'PUT' | 'GET',\n) => Promise<void | string>\n\n/** Reconcile an ambiguous command failure against the VMM before releasing the fence. */\nexport async function setVmmState(call: VmmApiCall, socketPath: string, desired: 'Paused' | 'Running'): Promise<void> {\n try {\n await call(socketPath, desired === 'Paused' ? '/api/v1/vm.pause' : '/api/v1/vm.resume')\n } catch (error) {\n try {\n const response = await call(socketPath, '/api/v1/vm.info', undefined, 'GET')\n const info: unknown = typeof response === 'string' ? JSON.parse(response) : null\n if (info && typeof info === 'object' && 'state' in info && info.state === desired) return\n } catch { /* State is unknown: preserve the original failure and the request fence. */ }\n throw error\n }\n}\n\nconst vmmApi: VmmApiCall = async (socketPath, path, body, method = 'PUT') => {\n const payload = body === undefined ? undefined : Buffer.from(JSON.stringify(body), 'utf8')\n return await new Promise<string>((resolve, reject) => {\n const request = httpRequest(\n {\n socketPath,\n path,\n method,\n headers: {\n ...(payload ? { 'content-type': 'application/json', 'content-length': String(payload.length) } : {}),\n },\n },\n (response) => {\n // CH answers 204 with no body on success. Drain regardless: an unread\n // response keeps the socket open and the next call queues behind it.\n const chunks: Buffer[] = []\n response.on('data', (chunk: Buffer) => chunks.push(chunk))\n response.on('end', () => {\n const status = response.statusCode ?? 0\n if (status >= 200 && status < 300) {\n resolve(Buffer.concat(chunks).toString('utf8'))\n return\n }\n const said = Buffer.concat(chunks).toString('utf8').trim().slice(0, 400)\n reject(new DeskError(`${path} returned ${status}${said === '' ? '' : `: ${said}`}`))\n })\n },\n )\n request.on('error', (error) => reject(new DeskError(`${path} failed: ${errorMessage(error)}`)))\n if (payload) request.write(payload)\n request.end()\n })\n}\n\n/**\n * Wait until the VMM answers its control socket.\n *\n * On a restore this is also the only honest signal that the snapshot was read\n * back: Cloud Hypervisor creates the API socket early but does not serve\n * `vm.info` until it holds a VM, so a successful call means the restore\n * completed rather than merely that the process started. A VMM that exits\n * instead — a corrupt snapshot, a config the host can no longer satisfy — stops\n * the wait immediately rather than burning the whole timeout.\n */\nasync function waitForVmm(\n call: VmmApiCall,\n socketPath: string,\n options: { now: () => number; exited: ExitWatcher; timeoutMs: number; retryDelayMs?: number },\n): Promise<void> {\n const deadline = options.now() + options.timeoutMs\n const retryDelayMs = options.retryDelayMs ?? 50\n let lastFailure = 'it never answered'\n for (;;) {\n if (options.exited.done) throw new DeskError(`the VMM exited before it served its control socket`)\n try {\n await call(socketPath, '/api/v1/vm.info', undefined, 'GET')\n return\n } catch (error) {\n lastFailure = errorMessage(error)\n }\n if (options.now() >= deadline) {\n throw new DeskError(`the VMM did not answer ${socketPath} in ${options.timeoutMs}ms: ${lastFailure}`)\n }\n await new Promise((resolve) => setTimeout(resolve, retryDelayMs))\n }\n}\n\n/** The default backend: real Cloud Hypervisor over a real vsock socket. */\nexport const cloudHypervisorBackend: DeskBackend = {\n boot: (plan, hooks) => createCloudHypervisorBackend().boot(plan, hooks),\n}\n"]}
1
+ {"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AACrC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,WAAW,CAAA;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAC3C,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,EACL,0BAA0B,EAAE,0BAA0B,EAAE,8BAA8B,EACtF,4BAA4B,EAAE,2BAA2B,EAAE,gCAAgC,EAC3F,qBAAqB,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,gBAAgB,EACxG,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,YAAY,EACjG,mBAAmB,EAAE,kBAAkB,GACxC,MAAM,WAAW,CAAA;AAelB,OAAO,EACL,SAAS,GACV,MAAM,QAAQ,CAAA;AAGf;;;;;;;;;;GAUG;AACH,MAA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AAC,0BAA0B,UAAU,OAAO,OAAO,CAAC,SAAS,OAAO,WAAW,EAAE,CAAC,CAAA;QACvG,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAA;IACnE,CAAC;AACH,CAAC;AAED,2EAA2E;AAC3E,MAAM,CAAC,MAAM,sBAAsB,GAAgB;IACjD,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,4BAA4B,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;CACxE,CAAA","sourcesContent":["import { randomUUID } from 'node:crypto'\nimport { assertPrivateMemoryMapping } from './template-mapping'\nimport { existsSync } from 'node:fs'\nimport { rm } from 'node:fs/promises'\nimport { request as httpRequest } from 'node:http'\nimport { createConnection } from 'node:net'\nimport { GUEST_AGENT_VSOCK_PORT } from './protocol'\nimport {\n DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_RETRY_DELAY_MS,\n DEFAULT_HANDSHAKE_TIMEOUT_MS, DEFAULT_RECONNECT_WINDOW_MS, DEFAULT_RECONNECT_RETRY_DELAY_MS,\n DEFAULT_KILL_GRACE_MS, RECONNECT_BACKOFF_CEILING, connectWithRetry, createGuestMachine, attemptHandshake,\n defaultLauncher, defaultConnect, drainOutput, watchExit, terminate, runToCompletion, errorMessage,\n assertShareManifest, writeShareManifest, type ExitWatcher,\n} from './machine'\n/**\n * The backend port.\n *\n * `DeskBackend` is the seam that keeps the package testable without a\n * hypervisor: the host asks a backend to boot a `DeskLaunchPlan` and gets a\n * `DeskMachine` — a request/response channel to the in-guest agent plus an\n * event subscription. CI and non-KVM developer machines inject an in-memory\n * fake; production uses `cloudHypervisorBackend` on Linux or\n * `createVirtualizationBackend` on an Apple silicon Mac, whose process glue is\n * deliberately thin because everything that can be pure lives in the plan\n * builder, the protocol module, and the shared guest channel.\n */\nimport type { ChildProcess } from 'node:child_process'\nimport type { Duplex } from 'node:stream'\nimport {\n DeskError, type CloudHypervisorLaunchPlan, type DeskHypervisor, type DeskLaunchPlan, type DeskLauncherIdentity,\n} from './plan'\nimport type { GuestCommand, GuestEventMessage } from './protocol'\n\n/**\n * A request that was in flight when the guest connection dropped.\n *\n * Distinct from a plain `DeskError` because the caller's next move is\n * different: this request may ALREADY HAVE RUN. The frame reached the guest,\n * and whether the guest executed it before the channel went away is not\n * knowable from the host. An `exec` may have sent mail; a `click` may have\n * landed. So the transport refuses to guess — it names the ambiguity and lets\n * the caller decide whether the operation is safe to repeat. Anything that\n * rejects with a plain `DeskError` instead never reached the guest at all.\n */\nexport class DeskRequestFateUnknownError extends DeskError {\n override readonly name: string = 'DeskRequestFateUnknownError'\n}\n\n/**\n * What happened to the transport underneath a machine.\n *\n * `reconnected` means the host lost the guest channel mid-lease and got it\n * back; the desk is usable again. It is NOT a promise that the guest is\n * unchanged — see the note on `onConnectionChange`. `lost` means it will not\n * come back on this VM and the desk needs a fresh boot.\n */\nexport type DeskConnectionChange =\n | {\n state: 'reconnected'\n deskId: string\n /** How the previous connection went away. */\n reason: string\n /** Wall-clock time the desk spent with no channel to its guest. */\n downtimeMs: number\n /** Reconnects this machine has survived, including this one. */\n reconnects: number\n }\n | {\n state: 'lost'\n deskId: string\n /** Why it is gone for good: the VMM exited, or the window ran out. */\n reason: string\n }\n\n/** A booted microVM as the host sees it: one desk, one guest-agent channel. */\nexport interface DeskMachine {\n supportsPausedParkHook?: true\n readonly deskId: string\n /**\n * Send one request to the in-guest agent and await its correlated result.\n *\n * Rejects with `DeskError` when the guest reports an error, the request\n * times out, or the connection is gone for good — in that last case the\n * request never left the host, so repeating it is safe.\n *\n * A connection lost mid-lease is re-established rather than fatal: a call\n * that arrives while that is happening WAITS for it instead of failing.\n * A call that was already in flight when the channel dropped rejects with\n * `DeskRequestFateUnknownError`, because the host cannot tell whether the\n * guest ran it.\n */\n request(command: GuestCommand, options?: { timeoutMs?: number }): Promise<unknown>\n /**\n * Subscribe to unsolicited guest events (job exits, frames, focus changes).\n *\n * Subscriptions belong to the machine, not to the socket under it, so they\n * survive a reconnect. What does NOT survive is the guest's own capture\n * state: a guest agent that restarted has no screen, no frame encoder and\n * no video encoder, and a subscriber will simply stop receiving. Watch\n * `onConnectionChange` to notice.\n */\n subscribe(listener: (event: GuestEventMessage) => void): () => void\n /**\n * Watch the transport under this machine.\n *\n * Optional: a backend whose channel cannot drop has nothing to report. The\n * default backend reports every reconnect, because a reconnect that leaves\n * no trace turns a guest agent restarting every few minutes into a desk that\n * misbehaves for reasons nobody can find.\n */\n onConnectionChange?(listener: (change: DeskConnectionChange) => void): () => void\n /**\n * Open a TCP stream from the host to `port` on the guest.\n *\n * Optional: a backend whose guest has a routable address (Cloud Hypervisor's\n * TAP) leaves this out and the host dials the guest address directly. A\n * backend whose guest sits behind a userspace NAT (Virtualization.framework)\n * provides it, because there is no address on the Mac to dial. Rejects with\n * `DeskError` when the guest refuses or does not answer.\n */\n connect?(port: number): Promise<Duplex>\n /** Optional resident pause and resume. Callers must serialize these with lifecycle operations. */\n pause?(): Promise<void>\n resume?(): Promise<void>\n /** Stop the VM. Idempotent; resolves once the VMM process has exited. */\n shutdown(): Promise<void>\n /**\n * Park the VM with its memory, so resuming continues the process tree.\n *\n * Optional: a backend with no snapshot facility simply does not offer it, and\n * the host falls back to `shutdown()`. Pauses the guest, writes a snapshot to\n * `snapshotDir`, then stops the VMM — and resolves only once all three have\n * happened, because a caller that believed a snapshot exists when it does not\n * would later refuse to boot the desk at all.\n *\n * The caller is responsible for quiescing the guest filesystem first. A paused\n * guest still holds dirty pages that exist only in the snapshot, so a disk\n * parked without a prior `sync` is consistent only *with that snapshot* —\n * which matters the moment the snapshot is discarded.\n */\n park?(snapshotDir: string, hooks?: DeskParkHooks): Promise<void>\n}\n\nexport interface DeskParkHooks {\n /** Called while the VM is paused, before its memory snapshot is written. */\n onPaused?: () => Promise<void>\n /** Reports a sidecar failure; ordinary memory parking continues. */\n onPausedError?: (error: unknown) => void\n}\n\nexport interface DeskBootHooks {\n /**\n * Called on a restore, after the snapshot has been read back and before the\n * guest is allowed to run.\n *\n * The snapshot must no longer be available for replay by this point. A memory image\n * is valid only against the disk it was taken with, so once the guest resumes\n * and writes, any surviving snapshot describes a past that no longer matches —\n * and replaying it later would corrupt the desk. Restoring paused and\n * invalidating here is what makes that impossible rather than unlikely.\n *\n * Throwing aborts the restore: the VM is stopped and the caller is free to\n * cold boot, which is safe because a paused guest has written nothing.\n */\n onRestored?: () => Promise<void>\n}\n\nexport interface DeskBackend {\n /**\n * Which hypervisor this backend boots, and so which plan the host builds\n * for it. Absent means Cloud Hypervisor.\n */\n readonly hypervisor?: DeskHypervisor\n /**\n * The binary the backend launches — the VMM or the helper — for readiness\n * reporting. Optional; `verifyDeskHost` falls back to its own option.\n */\n readonly vmmPath?: string\n boot(plan: DeskLaunchPlan, hooks?: DeskBootHooks): Promise<DeskMachine>\n}\n\nexport type DeskProcessLauncher = (\n command: string,\n args: readonly string[],\n options: { identity?: DeskLauncherIdentity; inheritedFds?: readonly number[] },\n) => ChildProcess\n\nexport interface CloudHypervisorBackendOptions {\n /** VMM API transport, injectable for tests. */\n vmmApi?: VmmApiCall\n /** Descriptors retained by the VMM and share daemons, mapped consecutively from fd 3. */\n inheritedFds?: readonly number[]\n /** Spawns the VMM and image tooling; injectable for tests. */\n launcher?: DeskProcessLauncher\n /** Opens the host side of the vsock Unix socket; injectable for tests. */\n connect?: (socketPath: string) => Duplex\n platform?: NodeJS.Platform\n /**\n * Clock behind every connect and reconnect deadline. Injectable for the same\n * reason `DeskHostOptions.now` is: a test drives a window to its end by\n * moving the clock, rather than sleeping for it and hoping the machine it\n * runs on is fast enough.\n */\n now?: () => number\n idFactory?: () => string\n requestTimeoutMs?: number\n connectTimeoutMs?: number\n connectRetryDelayMs?: number\n /** Bound on a single handshake attempt; see attemptHandshake. */\n handshakeTimeoutMs?: number\n /**\n * How long a machine keeps trying to get its guest channel back after an\n * unexpected drop before declaring the desk lost. Bounded on purpose: the\n * VMM is still alive so a reconnect is cheap and usually fast, but retrying\n * forever against a guest that is never coming back is worse than failing.\n */\n reconnectWindowMs?: number\n /**\n * First delay between reconnect attempts. It doubles toward a ceiling, so\n * this is the fast first poll rather than the steady rate. Separate from\n * `connectRetryDelayMs`, which is sized for a guest that is still cold\n * booting; here the guest was up a moment ago.\n */\n reconnectRetryDelayMs?: number\n killGraceMs?: number\n guestAgentPort?: number\n /** Use guest-provided vsock service bridges for DeskHandle.connect. Opt in only when the image provides them. */\n serviceVsock?: boolean\n /** Whether a share daemon's socket exists yet; injectable for tests. */\n pathExists?: (path: string) => boolean\n /** Removes a stale share or vsock socket before the process that binds it starts; injectable for tests. */\n removeFile?: (path: string) => Promise<void>\n /** Whether a process listens on a Unix socket (a running VMM on its vsock socket); injectable for tests. */\n socketListening?: (path: string) => Promise<boolean>\n}\n\n/** Whether something accepts connections on the Unix socket at `path`: false when nothing is there or nothing listens. */\nexport function unixSocketListening(path: string): Promise<boolean> {\n return new Promise((resolvePromise, rejectPromise) => {\n const socket = createConnection(path)\n socket.once('connect', () => {\n socket.destroy()\n resolvePromise(true)\n })\n socket.once('error', (error: NodeJS.ErrnoException) => {\n socket.destroy()\n if (error.code === 'ECONNREFUSED' || error.code === 'ENOENT') resolvePromise(false)\n else rejectPromise(error)\n })\n })\n}\n\nexport function createCloudHypervisorBackend(\n options: CloudHypervisorBackendOptions = {},\n): DeskBackend {\n const api = options.vmmApi ?? vmmApi\n const launcher = options.launcher ?? defaultLauncher\n const connect = options.connect ?? defaultConnect\n const platform = options.platform ?? process.platform\n const now = options.now ?? Date.now\n const idFactory = options.idFactory ?? randomUUID\n const requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS\n const connectTimeoutMs = options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS\n const connectRetryDelayMs = options.connectRetryDelayMs ?? DEFAULT_CONNECT_RETRY_DELAY_MS\n const handshakeTimeoutMs = options.handshakeTimeoutMs ?? DEFAULT_HANDSHAKE_TIMEOUT_MS\n const reconnectWindowMs = options.reconnectWindowMs ?? DEFAULT_RECONNECT_WINDOW_MS\n const reconnectRetryDelayMs = options.reconnectRetryDelayMs ?? DEFAULT_RECONNECT_RETRY_DELAY_MS\n const killGraceMs = options.killGraceMs ?? DEFAULT_KILL_GRACE_MS\n const guestAgentPort = options.guestAgentPort ?? GUEST_AGENT_VSOCK_PORT\n const pathExists = options.pathExists ?? existsSync\n const removeFile = options.removeFile ?? ((path: string) => rm(path, { force: true }))\n const socketListening = options.socketListening ?? unixSocketListening\n\n /**\n * Start one `virtiofsd` per share and wait until every socket exists.\n *\n * The VMM connects to these at boot and at restore alike, so they come up\n * first. A stale socket is removed beforehand, or its presence would read as\n * a daemon that is listening. Existence is the readiness signal rather than\n * a probe connection, because `virtiofsd` serves exactly one connection and\n * exits when it closes; the moment between bind and listen is covered by\n * Cloud Hypervisor retrying a refused vhost-user connect.\n */\n async function startShareDaemons(plan: CloudHypervisorLaunchPlan): Promise<ShareDaemons> {\n const daemons: { tag: string; child: ChildProcess; exit: ExitWatcher; log: { tail: () => string } }[] = []\n const stop = async () => {\n await Promise.all(daemons.map((daemon) => terminate(daemon.child, daemon.exit, killGraceMs)))\n }\n try {\n for (const share of plan.shares) {\n await removeFile(share.socketPath)\n const child = launcher(share.daemon.command, share.daemon.args, { identity: plan.launcherIdentity, inheritedFds: options.inheritedFds })\n daemons.push({ tag: share.tag, child, exit: watchExit(child), log: drainOutput(child) })\n }\n const deadline = now() + connectTimeoutMs\n for (;;) {\n const exited = daemons.find((daemon) => daemon.exit.done)\n if (exited) {\n const said = exited.log.tail()\n throw new DeskError(`virtiofsd for share ${exited.tag} exited before it listened${said === '' ? '.' : `: ${said}`}`)\n }\n const waiting = plan.shares.filter((share) => !pathExists(share.socketPath))\n if (waiting.length === 0) break\n if (now() >= deadline) {\n throw new DeskError(\n `virtiofsd did not create ${waiting.map((share) => share.socketPath).join(', ')} in ${connectTimeoutMs}ms.`,\n )\n }\n // A held timer: the wait is bounded by the deadline, and nothing else may be keeping\n // the process alive while a daemon starts.\n await new Promise<void>((resolvePromise) => setTimeout(resolvePromise, connectRetryDelayMs))\n }\n } catch (error) {\n await stop()\n throw error\n }\n return {\n stop,\n // The VMM cannot serve a share whose daemon is gone; neither can it be\n // handed a new one mid-run. So a daemon that dies stops the VMM, and the\n // desk is lost loudly instead of hanging every guest access to the share.\n supervise: (vmm, vmmExit) => {\n for (const daemon of daemons) {\n void daemon.exit.promise.then(() => terminate(vmm, vmmExit, killGraceMs))\n }\n },\n }\n }\n\n async function boot(plan: DeskLaunchPlan, hooks?: DeskBootHooks): Promise<DeskMachine> {\n if (plan.hypervisor !== 'cloud-hypervisor') throw new DeskError('Cloud Hypervisor requires a cloud-hypervisor plan.')\n if (platform !== 'linux') {\n throw new DeskError(`The AppKit desk requires Linux; received ${platform}.`)\n }\n if (plan.overlay.create) {\n await runToCompletion(\n launcher(plan.overlay.create.command, plan.overlay.create.args, {\n identity: plan.launcherIdentity,\n }),\n `overlay creation for desk ${plan.deskId}`,\n )\n }\n if (plan.snapshot.restoring) {\n try {\n await assertShareManifest(plan.snapshot.dir, plan.shares)\n } catch (error) {\n throw new DeskError(`Desk ${plan.deskId} could not be restored from ${plan.snapshot.dir}: ${errorMessage(error)}`)\n }\n }\n\n // Cloud Hypervisor binds the vsock's host socket and fails with \"Address in use\" when a\n // VMM that did not exit cleanly left it behind. A socket nothing listens on is that\n // leftover and is removed; one a VMM still serves is this desk running already.\n if (await socketListening(plan.vsock.socketPath)) {\n throw new DeskError(`Desk ${plan.deskId} is already running: a VMM is listening on ${plan.vsock.socketPath}.`)\n }\n await removeFile(plan.vsock.socketPath)\n\n let shares: ShareDaemons\n try {\n shares = await startShareDaemons(plan)\n } catch (error) {\n throw new DeskError(`Desk ${plan.deskId} could not start its shares: ${errorMessage(error)}`)\n }\n const child = launcher(plan.vmm.command, plan.vmm.args, { identity: plan.launcherIdentity, inheritedFds: options.inheritedFds })\n const log = drainOutput(child)\n const exit = watchExit(child)\n shares.supervise(child, exit)\n // The VMM first: a daemon outliving it is harmless, while a daemon stopped\n // under a running VMM is a share failing in the guest.\n const stop = async () => {\n try {\n await terminate(child, exit, killGraceMs)\n } finally {\n await shares.stop()\n }\n }\n\n // A restored guest comes back PAUSED (see the `resume=false` reasoning in\n // the plan). Three things have to happen in this order and nowhere else:\n // prove the restore finished, let the caller invalidate replay while the\n // guest still cannot write, then start it.\n if (plan.snapshot.restoring) {\n try {\n await waitForVmm(api, plan.api.socketPath, { now, exited: exit, timeoutMs: connectTimeoutMs })\n if (plan.snapshot.requirePrivateMemoryMapping) await assertPrivateMemoryMapping(child.pid, plan.snapshot.dir)\n await hooks?.onRestored?.()\n await api(plan.api.socketPath, '/api/v1/vm.resume')\n } catch (error) {\n await stop()\n const said = log.tail()\n const detail = said === '' ? '' : ` The VMM said: ${said}`\n throw new DeskError(\n `Desk ${plan.deskId} could not be restored from ${plan.snapshot.dir}: ${errorMessage(error)}${detail}`,\n )\n }\n }\n\n let stream: Duplex\n try {\n stream = await connectWithRetry({\n connect,\n now,\n socketPath: plan.vsock.socketPath,\n port: guestAgentPort,\n timeoutMs: connectTimeoutMs,\n retryDelayMs: connectRetryDelayMs,\n attemptTimeoutMs: handshakeTimeoutMs,\n exited: exit,\n })\n } catch (error) {\n await stop()\n const said = log.tail()\n const detail = said === '' ? '' : ` The VMM said: ${said}`\n throw error instanceof DeskError\n ? new DeskError(`${error.message}${detail}`)\n : new DeskError(\n `Could not reach the guest agent for desk ${plan.deskId}: ${errorMessage(error)}${detail}`,\n )\n }\n return createGuestMachine({\n deskId: plan.deskId,\n exit,\n stream,\n // The same path the first connection took, deliberately: a reconnect\n // that skipped confirmGuest would adopt the very hollow handshake the\n // boot path learned not to trust.\n reconnect: (abandoned) =>\n connectWithRetry({\n connect,\n now,\n socketPath: plan.vsock.socketPath,\n port: guestAgentPort,\n timeoutMs: reconnectWindowMs,\n retryDelayMs: reconnectRetryDelayMs,\n maxRetryDelayMs: reconnectRetryDelayMs * RECONNECT_BACKOFF_CEILING,\n attemptTimeoutMs: handshakeTimeoutMs,\n exited: exit,\n exitedMessage: `the VMM for desk ${plan.deskId} exited.`,\n abandoned,\n }),\n reconnectWindowMs,\n now,\n idFactory,\n requestTimeoutMs,\n ...(options.serviceVsock ? { connect: (port: number) => {\n if (!Number.isInteger(port) || port < 1 || port > 65535) throw new DeskError('Guest port must be between 1 and 65535.')\n return attemptHandshake(connect, plan.vsock.socketPath, port, handshakeTimeoutMs)\n } } : {}),\n stop,\n pause: () => setVmmState(api, plan.api.socketPath, 'Paused'),\n resume: () => setVmmState(api, plan.api.socketPath, 'Running'),\n supportsPausedParkHook: true,\n park: async (snapshotDir, hooks) => {\n try {\n await api(plan.api.socketPath, '/api/v1/vm.pause')\n try { await hooks?.onPaused?.() }\n catch (error) { try { hooks?.onPausedError?.(error) } catch { /* preserve memory */ } }\n await api(plan.api.socketPath, '/api/v1/vm.snapshot', { destination_url: `file://${snapshotDir}` })\n await writeShareManifest(snapshotDir, plan.shares)\n } finally {\n await stop()\n }\n },\n })\n }\n\n return { boot }\n}\n\ninterface ShareDaemons {\n /** Stop every daemon; resolves once all have exited. */\n stop: () => Promise<void>\n /** Stop the VMM if any daemon exits while it runs. */\n supervise: (vmm: ChildProcess, vmmExit: ExitWatcher) => void\n}\n\n/**\n * Cloud Hypervisor's control plane: HTTP/1.1 over the `--api-socket` Unix\n * socket. The plan has always created that socket; until now nothing spoke to\n * it, because stopping a VM needed nothing more than killing the process.\n *\n * Pausing and snapshotting do need it, and `ch-remote` — the tool that usually\n * does this — is not in the runner image. Writing the two requests by hand is a\n * smaller change than adding a binary to the image, and keeps the whole\n * lifecycle inside one process that already owns the socket path.\n */\nexport type VmmApiCall = (\n socketPath: string,\n path: string,\n body?: Record<string, unknown>,\n method?: 'PUT' | 'GET',\n) => Promise<void | string>\n\n/** Reconcile an ambiguous command failure against the VMM before releasing the fence. */\nexport async function setVmmState(call: VmmApiCall, socketPath: string, desired: 'Paused' | 'Running'): Promise<void> {\n try {\n await call(socketPath, desired === 'Paused' ? '/api/v1/vm.pause' : '/api/v1/vm.resume')\n } catch (error) {\n try {\n const response = await call(socketPath, '/api/v1/vm.info', undefined, 'GET')\n const info: unknown = typeof response === 'string' ? JSON.parse(response) : null\n if (info && typeof info === 'object' && 'state' in info && info.state === desired) return\n } catch { /* State is unknown: preserve the original failure and the request fence. */ }\n throw error\n }\n}\n\nconst vmmApi: VmmApiCall = async (socketPath, path, body, method = 'PUT') => {\n const payload = body === undefined ? undefined : Buffer.from(JSON.stringify(body), 'utf8')\n return await new Promise<string>((resolve, reject) => {\n const request = httpRequest(\n {\n socketPath,\n path,\n method,\n headers: {\n ...(payload ? { 'content-type': 'application/json', 'content-length': String(payload.length) } : {}),\n },\n },\n (response) => {\n // CH answers 204 with no body on success. Drain regardless: an unread\n // response keeps the socket open and the next call queues behind it.\n const chunks: Buffer[] = []\n response.on('data', (chunk: Buffer) => chunks.push(chunk))\n response.on('end', () => {\n const status = response.statusCode ?? 0\n if (status >= 200 && status < 300) {\n resolve(Buffer.concat(chunks).toString('utf8'))\n return\n }\n const said = Buffer.concat(chunks).toString('utf8').trim().slice(0, 400)\n reject(new DeskError(`${path} returned ${status}${said === '' ? '' : `: ${said}`}`))\n })\n },\n )\n request.on('error', (error) => reject(new DeskError(`${path} failed: ${errorMessage(error)}`)))\n if (payload) request.write(payload)\n request.end()\n })\n}\n\n/**\n * Wait until the VMM answers its control socket.\n *\n * On a restore this is also the only honest signal that the snapshot was read\n * back: Cloud Hypervisor creates the API socket early but does not serve\n * `vm.info` until it holds a VM, so a successful call means the restore\n * completed rather than merely that the process started. A VMM that exits\n * instead — a corrupt snapshot, a config the host can no longer satisfy — stops\n * the wait immediately rather than burning the whole timeout.\n */\nasync function waitForVmm(\n call: VmmApiCall,\n socketPath: string,\n options: { now: () => number; exited: ExitWatcher; timeoutMs: number; retryDelayMs?: number },\n): Promise<void> {\n const deadline = options.now() + options.timeoutMs\n const retryDelayMs = options.retryDelayMs ?? 50\n let lastFailure = 'it never answered'\n for (;;) {\n if (options.exited.done) throw new DeskError(`the VMM exited before it served its control socket`)\n try {\n await call(socketPath, '/api/v1/vm.info', undefined, 'GET')\n return\n } catch (error) {\n lastFailure = errorMessage(error)\n }\n if (options.now() >= deadline) {\n throw new DeskError(`the VMM did not answer ${socketPath} in ${options.timeoutMs}ms: ${lastFailure}`)\n }\n await new Promise((resolve) => setTimeout(resolve, retryDelayMs))\n }\n}\n\n/** The default backend: real Cloud Hypervisor over a real vsock socket. */\nexport const cloudHypervisorBackend: DeskBackend = {\n boot: (plan, hooks) => createCloudHypervisorBackend().boot(plan, hooks),\n}\n"]}
package/host.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import type { Duplex } from 'node:stream';
3
- import { type DeskLauncherIdentity, type DeskGuestNetworkOptions, type DeskShare, type DeskReadOnlyAttachment, type DeskVirtiofsdSandbox } from './plan.js';
4
- import { type DeskBackend } from './backend.js';
3
+ import { buildDeskLaunchPlan, type DeskLauncherIdentity, type DeskGuestNetworkOptions, type DeskShare, type DeskReadOnlyAttachment, type DeskVirtiofsdSandbox } from './plan.js';
4
+ import { type DeskBackend, type DeskMachine, type DeskParkHooks } from './backend.js';
5
5
  import type { A11yNode, DeskFrameFormat, DeskHandoverScope, DeskPoint, DeskPointerButton, DeskPorts, DeskVideoChunkKind, WindowInfo } from './events.js';
6
+ /** Capability marker for the fenced template lifecycle and mapping proof. */
7
+ export declare const DESK_TEMPLATE_VERSION = 3;
6
8
  export declare const DEFAULT_CAPACITY = 4;
7
9
  export declare const DEFAULT_IDLE_SUSPEND_MS: number;
8
10
  export declare const DEFAULT_LEASE_MS: number;
@@ -77,8 +79,10 @@ export interface DeskHostOptions {
77
79
  * occurrence is indistinguishable, from inside the guest, from a failing host.
78
80
  */
79
81
  parkWithMemory?: boolean;
80
- /** Opt in to Cloud Hypervisor copy-on-write snapshot restore. */
81
- memoryRestoreMode?: 'copyonwrite';
82
+ /** Select file-backed CoW or strict userfaultfd on-demand restore on Linux. */
83
+ memoryRestoreMode?: 'copyonwrite' | 'ondemand';
84
+ /** Refuse fallback or replay after a memory restore is interrupted. */
85
+ strictMemoryRestore?: boolean;
82
86
  /** Atomically retire a parked snapshot and sync its parent directory. */
83
87
  retireSnapshot?: (from: string, to: string) => Promise<void>;
84
88
  /** Identifies the host CPU for conservative snapshot portability checks. */
@@ -129,6 +133,18 @@ export interface DeskHostOptions {
129
133
  }
130
134
  export interface DeskStartOptions {
131
135
  deskId: string;
136
+ /** Prepare and initialize a sterile template under an external network fence. */
137
+ template?: {
138
+ /** Install the external fence even when a prior preparation was interrupted. */
139
+ holdFence: () => Promise<void>;
140
+ prepare: (plan: ReturnType<typeof buildDeskLaunchPlan>) => Promise<void>;
141
+ /** Revalidate a prepared child before any VMM maps it. */
142
+ verifyPrepared: (plan: ReturnType<typeof buildDeskLaunchPlan>) => Promise<void>;
143
+ /** Revalidate the registered image identities for a parked descendant. */
144
+ verifyResume: (plan: ReturnType<typeof buildDeskLaunchPlan>) => Promise<void>;
145
+ initialize: (machine: DeskMachine, plan: ReturnType<typeof buildDeskLaunchPlan>) => Promise<void>;
146
+ releaseFence: () => Promise<void>;
147
+ };
132
148
  /** Absolute path of the golden base image this desk boots from. */
133
149
  baseImage: string;
134
150
  /** Optional kernel and initramfs for this desk; retain them with its disk. */
@@ -141,6 +157,12 @@ export interface DeskStartOptions {
141
157
  freePageReporting?: boolean;
142
158
  vcpus?: number;
143
159
  leaseMs?: number;
160
+ /** Preserve memory state on failures or interruption for this desk. */
161
+ strictMemoryRestore?: boolean;
162
+ /** Select file-backed CoW or strict userfaultfd on-demand restore for this desk. */
163
+ memoryRestoreMode?: 'copyonwrite' | 'ondemand';
164
+ /** Stable guest CID; callers must allocate it uniquely across host restarts. */
165
+ vsockCid?: number;
144
166
  network?: {
145
167
  tapDevice?: string;
146
168
  macAddress?: string;
@@ -379,6 +401,12 @@ export interface DeskHostStats {
379
401
  reason: 'signature_mismatch' | 'incompatible_cpuid';
380
402
  } | null;
381
403
  }
404
+ export interface DeskSuspendHooks extends DeskParkHooks {
405
+ /** Undo partial preparation before rejecting; crash recovery belongs to the caller. */
406
+ beforePark?: (machine: DeskMachine) => Promise<void>;
407
+ /** Called only after a successful, durable memory park without a sidecar error. */
408
+ onParked?: (snapshotDir: string) => Promise<void>;
409
+ }
382
410
  export interface DeskHost {
383
411
  start(options: DeskStartOptions): Promise<DeskHandle>;
384
412
  /** True when this resident desk supports in-memory pause. */
@@ -387,7 +415,7 @@ export interface DeskHost {
387
415
  pause(deskId: string): Promise<void>;
388
416
  /** Resume a paused resident desk or restore a suspended desk. */
389
417
  resume(deskId: string): Promise<DeskHandle>;
390
- suspend(deskId: string): Promise<void>;
418
+ suspend(deskId: string, hooks?: DeskSuspendHooks): Promise<void>;
391
419
  /**
392
420
  * Stop a desk WITHOUT its memory, keeping its overlay: the next `start` or
393
421
  * `resume` cold boots from disk. This is a restart, not a destroy — the disk