@fungi.computer/computer 0.8.4 → 0.8.6

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.
@@ -0,0 +1,21 @@
1
+ import type { WatchdogSqliteOwner } from "@fungi.computer/watchdog";
2
+ /** Record the name a name-addressed wake carries. */
3
+ export declare function recordComputerName(sql: WatchdogSqliteOwner["sql"], name: string): void;
4
+ /**
5
+ * The wake's own name, else the recorded one. Read-only: it never recreates
6
+ * the record after an erasure. Undefined only when no name-addressed wake has
7
+ * reached this Computer.
8
+ */
9
+ export declare function computerName(sql: WatchdogSqliteOwner["sql"], wokenAs: string | undefined): string | undefined;
10
+ /**
11
+ * Learn the name of a Computer no named wake has reached, from names its host
12
+ * already knows. A candidate is accepted only when the Computer's own
13
+ * namespace derives this object's id from it; anything else is refused and
14
+ * never stored. Call it only where recording is wanted, never after an
15
+ * erasure that must stay erased.
16
+ */
17
+ export declare function learnComputerName<Id extends {
18
+ equals(other: Id): boolean;
19
+ }>(sql: WatchdogSqliteOwner["sql"], id: Id, namespace: Readonly<{
20
+ idFromName(name: string): Id;
21
+ }> | undefined, candidates: readonly string[]): string | undefined;
@@ -0,0 +1,55 @@
1
+ import { z } from "zod";
2
+ /*
3
+ * A Computer's durable name, recorded by each name-addressed wake.
4
+ *
5
+ * `ctx.id.name` belongs to the wake that constructed the live instance, not to
6
+ * the object. The doors that address a Computer by object id (the container's
7
+ * WorkspaceProxy reconnect and egress, the service proxy, VM ingress) and
8
+ * alarms wake it with no name, and every later name-addressed call reaches
9
+ * that same nameless instance. A redeploy restarts the object while its
10
+ * container reconnects, so a nameless instance reads the name from storage.
11
+ */
12
+ /** Record the name a name-addressed wake carries. */
13
+ export function recordComputerName(sql, name) {
14
+ sql.exec("CREATE TABLE IF NOT EXISTS computer_name (singleton INTEGER PRIMARY KEY CHECK (singleton=1), name TEXT NOT NULL) STRICT");
15
+ sql.exec("INSERT INTO computer_name (singleton,name) VALUES (1,?) ON CONFLICT (singleton) DO NOTHING", name);
16
+ // An object id is derived from its name, so a second name is corruption.
17
+ if (storedComputerName(sql) !== name)
18
+ throw new Error("Computer storage records a different name");
19
+ }
20
+ /**
21
+ * The wake's own name, else the recorded one. Read-only: it never recreates
22
+ * the record after an erasure. Undefined only when no name-addressed wake has
23
+ * reached this Computer.
24
+ */
25
+ export function computerName(sql, wokenAs) {
26
+ return wokenAs ?? storedComputerName(sql);
27
+ }
28
+ /**
29
+ * Learn the name of a Computer no named wake has reached, from names its host
30
+ * already knows. A candidate is accepted only when the Computer's own
31
+ * namespace derives this object's id from it; anything else is refused and
32
+ * never stored. Call it only where recording is wanted, never after an
33
+ * erasure that must stay erased.
34
+ */
35
+ export function learnComputerName(sql, id, namespace, candidates) {
36
+ const known = storedComputerName(sql);
37
+ if (known !== undefined || namespace === undefined)
38
+ return known;
39
+ const name = candidates.find((candidate) => id.equals(namespace.idFromName(candidate)));
40
+ if (name === undefined)
41
+ return undefined;
42
+ recordComputerName(sql, name);
43
+ return name;
44
+ }
45
+ function storedComputerName(sql) {
46
+ const recorded = sql
47
+ .exec("SELECT 1 FROM sqlite_master WHERE type='table' AND name='computer_name'")
48
+ .toArray().length > 0;
49
+ if (!recorded)
50
+ return undefined;
51
+ const row = sql
52
+ .exec("SELECT name FROM computer_name WHERE singleton=1")
53
+ .toArray()[0];
54
+ return row === undefined ? undefined : z.string().parse(row.name);
55
+ }
package/dist/index.d.ts CHANGED
@@ -52,6 +52,10 @@ export type ComputerEnv = OnBoxVmEnv & Readonly<{
52
52
  }) => Fetcher;
53
53
  /** Platform kill switch for container-backed Computer operations. */
54
54
  HOSTED_COMPUTERS?: string;
55
+ /** This Computer's own namespace; derives and verifies its object id. */
56
+ COMPUTER?: Readonly<{
57
+ idFromName(name: string): DurableObjectId;
58
+ }>;
55
59
  LOADER?: NonNullable<WorkerShellBackendOptions["loader"]>;
56
60
  }>;
57
61
  /** A command could not be placed on an attached backend. */
@@ -78,6 +82,14 @@ declare class ContainerComputer extends ContainerComputerBase {
78
82
  /** Parked dial-ins from attached machines; survives Workspace rebuilds. */
79
83
  readonly localConnections: LocalBackendConnections;
80
84
  constructor(ctx: DurableObjectState, env: ComputerEnv);
85
+ /**
86
+ * This Computer's name, from storage: an instance first woken by object id
87
+ * has no `ctx.id.name` for its whole life. Undefined only when no
88
+ * name-addressed wake has reached this Computer yet.
89
+ */
90
+ protected computerName(): string | undefined;
91
+ /** Names this host knows this Computer may have; each is verified, not trusted. */
92
+ protected nameCandidates(): readonly string[];
81
93
  getWorkspaceContainer(): import("@fungi.computer/cloudflare-computer/backends/container").WorkspaceContainerAPI;
82
94
  /**
83
95
  * This Computer's attached backends, in attach order. The standalone
@@ -302,6 +314,7 @@ export declare class ComputerDO extends ContainerComputer implements ComputerFil
302
314
  protected replaceFileIfUnchangedAfterAcceptance(input: ComputerFileReplacement, accept: () => void): Promise<ComputerFileReplacementResult>;
303
315
  }
304
316
  export { WorkspaceProxy, WorkspaceServiceProxy };
317
+ export { computerName } from "./computer-name.js";
305
318
  export { OnBoxVmContainerHost, OnBoxVmTimeoutError, onBoxVmPlacement, onBoxVmIngress, type OnBoxVmEnv, type OnBoxVmOperation, type OnBoxVmState, } from "./on-box-vm.js";
306
319
  export { computerContainerSleepAfterMs } from "./container-policy.js";
307
320
  export * from "./computer-backends.js";
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
53
53
  import { boundExecStream } from "./exec-output.js";
54
54
  export { createLocalBackendReplayStore } from "./local-backend-replay.js";
55
55
  import { computerInternetAccess } from "./internet-access.js";
56
+ import { computerName, learnComputerName, recordComputerName, } from "./computer-name.js";
56
57
  import { getWorkspace, SQLiteWorkspaceProvider, Workspace, WorkspaceProxy, WorkspaceServiceProxy, } from "@fungi.computer/cloudflare-computer";
57
58
  import { WorkerShellBackend, } from "@fungi.computer/cloudflare-computer/backends/worker-shell";
58
59
  import jqCommands from "@fungi.computer/cloudflare-computer/shell/jq";
@@ -235,11 +236,30 @@ class ContainerComputer extends ContainerComputerBase {
235
236
  #pullPending = new PullPendingTracker();
236
237
  constructor(ctx, env) {
237
238
  super(ctx, env);
239
+ if (ctx.id.name !== undefined)
240
+ recordComputerName(ctx.storage.sql, ctx.id.name);
238
241
  if (ctx.container === undefined ||
239
242
  hostedComputerContainersPaused(env.HOSTED_COMPUTERS))
240
243
  return;
241
244
  ctx.waitUntil(configureComputerContainer(ctx.container, computerKeepAwake(ctx.storage.sql)));
242
245
  }
246
+ /**
247
+ * This Computer's name, from storage: an instance first woken by object id
248
+ * has no `ctx.id.name` for its whole life. Undefined only when no
249
+ * name-addressed wake has reached this Computer yet.
250
+ */
251
+ computerName() {
252
+ const known = computerName(this.state.storage.sql, this.state.id.name);
253
+ if (known !== undefined)
254
+ return known;
255
+ // An object that predates the record learns its name from the names its
256
+ // host already knows, verified against this object's id.
257
+ return learnComputerName(this.state.storage.sql, this.state.id, this.bindings.COMPUTER, this.nameCandidates());
258
+ }
259
+ /** Names this host knows this Computer may have; each is verified, not trusted. */
260
+ nameCandidates() {
261
+ return [];
262
+ }
243
263
  getWorkspaceContainer() {
244
264
  assertComputerContainersEnabled(this.bindings.HOSTED_COMPUTERS);
245
265
  return super.getWorkspaceContainer();
@@ -280,7 +300,7 @@ class ContainerComputer extends ContainerComputerBase {
280
300
  const backend = this.attachedBackends().find((candidate) => candidate.kind === "local" &&
281
301
  attachedBackendId(candidate) === backendId);
282
302
  if (backend === undefined ||
283
- computerName !== this.state.id.name ||
303
+ computerName !== this.computerName() ||
284
304
  !(await verifyLocalBackendDial(request, computerName, backend, this.state.storage)))
285
305
  return new Response(null, { status: 404 });
286
306
  // A verified plain request lets the machine tell attached from detached.
@@ -1535,7 +1555,7 @@ export class ComputerDO extends ContainerComputer {
1535
1555
  /** Host-owned display labels only; never credentials or provider URLs. */
1536
1556
  terminalDisplayEnvironment() {
1537
1557
  return Promise.resolve({
1538
- FUNGI_COMPUTER_NAME: this.state.id.name ?? "Computer",
1558
+ FUNGI_COMPUTER_NAME: this.computerName() ?? "Computer",
1539
1559
  });
1540
1560
  }
1541
1561
  /** Atomically replace one bounded file only when its raw contents still match. */
@@ -1567,6 +1587,7 @@ export class ComputerDO extends ContainerComputer {
1567
1587
  // Runtime backends reach the authoritative Workspace through these loopback
1568
1588
  // entrypoints. The consuming Worker must re-export both from its main module.
1569
1589
  export { WorkspaceProxy, WorkspaceServiceProxy };
1590
+ export { computerName } from "./computer-name.js";
1570
1591
  export { OnBoxVmContainerHost, OnBoxVmTimeoutError, onBoxVmPlacement, onBoxVmIngress, } from "./on-box-vm.js";
1571
1592
  export { computerContainerSleepAfterMs } from "./container-policy.js";
1572
1593
  export * from "./computer-backends.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",