@fungi.computer/computer 0.8.8 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,6 +39,8 @@ export type FleetLifecyclePorts = Readonly<{
39
39
  id: string;
40
40
  };
41
41
  containerRunning: (backing: HostedBacking) => Promise<boolean>;
42
+ /** The Computer's process record learns that this backend went to sleep. */
43
+ endedByIdleSleep: (backendId: string) => void;
42
44
  vmQueuePosition: (hostId: string) => Promise<number | null>;
43
45
  }>;
44
46
  }>;
@@ -358,6 +358,8 @@ export class FleetLifecycle {
358
358
  this.diagnostic("computer_idle_stop_failed", "idle_stop", cause);
359
359
  return { running: true, at: this.billedThrough(runtimeId, now) };
360
360
  }
361
+ // The sleep ended every command still running on this backend; say so.
362
+ this.ports.backend.endedByIdleSleep(attachedBackendId(backing));
361
363
  return { running: false, at: this.billedThrough(runtimeId, now) };
362
364
  }
363
365
  return { running, at: this.billedThrough(runtimeId, exitedAt) };
package/dist/index.d.ts CHANGED
@@ -156,6 +156,8 @@ declare class ContainerComputer extends ContainerComputerBase {
156
156
  export declare class ComputerDO extends ContainerComputer implements ComputerFilesRpc {
157
157
  #private;
158
158
  constructor(ctx: DurableObjectState, env: ComputerEnv);
159
+ /** The fleet slept this backend after it sat idle; its running commands ended with it. */
160
+ protected endedByIdleSleep(backendId: string): void;
159
161
  /** Host-admitted settings write; egress reads the current durable value. */
160
162
  internetAccess(enabled?: boolean): boolean;
161
163
  /**
package/dist/index.js CHANGED
@@ -518,6 +518,10 @@ export class ComputerDO extends ContainerComputer {
518
518
  // Reconstruction revokes unfinished exec authority before the first RPC.
519
519
  void this.#executions;
520
520
  }
521
+ /** The fleet slept this backend after it sat idle; its running commands ended with it. */
522
+ endedByIdleSleep(backendId) {
523
+ this.#processes.endedByIdleSleep(backendId);
524
+ }
521
525
  /** Host-admitted settings write; egress reads the current durable value. */
522
526
  internetAccess(enabled) {
523
527
  return computerInternetAccess(this.state.storage.sql, enabled);
@@ -1,10 +1,18 @@
1
1
  import type { SeamReplayStore } from "@fungi.computer/seam";
2
2
  import type { WatchdogSqliteOwner } from "@fungi.computer/watchdog";
3
- /** Private receiver-local storage ceiling, not a Team or attachment quota. */
3
+ /**
4
+ * Private receiver-local storage ceiling for ONE backend's live dial history,
5
+ * not a Team or attachment quota. Each attached backend has its own share, so a
6
+ * backend that reaches it is refused alone; a Computer's total is bounded by
7
+ * its attached-backend limit times this.
8
+ */
4
9
  export declare const maximumLocalDialReplayRecords = 4096;
5
10
  /**
6
- * One Computer's local-dial receiver history, in its existing owner database.
7
- * Reconstruct with the same owner, never a process-generation namespace.
8
- * Native SQLite owns atomicity and commit; there is no in-memory fallback.
11
+ * One backend's share of a Computer's local-dial receiver history, in its
12
+ * existing owner database. A nonce is a replay whichever backend first used
13
+ * it, but only `backendId`'s own live records count toward its capacity.
14
+ * Reconstruct with the same owner and backend, never a process-generation
15
+ * namespace. Native SQLite owns atomicity and commit; there is no in-memory
16
+ * fallback.
9
17
  */
10
- export declare function createLocalBackendReplayStore(owner: WatchdogSqliteOwner): SeamReplayStore;
18
+ export declare function createLocalBackendReplayStore(owner: WatchdogSqliteOwner, backendId: string): SeamReplayStore;
@@ -1,22 +1,44 @@
1
1
  import { z } from "zod";
2
- /** Private receiver-local storage ceiling, not a Team or attachment quota. */
2
+ /**
3
+ * Private receiver-local storage ceiling for ONE backend's live dial history,
4
+ * not a Team or attachment quota. Each attached backend has its own share, so a
5
+ * backend that reaches it is refused alone; a Computer's total is bounded by
6
+ * its attached-backend limit times this.
7
+ */
3
8
  export const maximumLocalDialReplayRecords = 4096;
4
9
  const countSchema = z.number().int().nonnegative();
10
+ const definitionSchema = z.object({ sql: z.string() });
5
11
  /**
6
- * One Computer's local-dial receiver history, in its existing owner database.
7
- * Reconstruct with the same owner, never a process-generation namespace.
8
- * Native SQLite owns atomicity and commit; there is no in-memory fallback.
12
+ * One backend's share of a Computer's local-dial receiver history, in its
13
+ * existing owner database. A nonce is a replay whichever backend first used
14
+ * it, but only `backendId`'s own live records count toward its capacity.
15
+ * Reconstruct with the same owner and backend, never a process-generation
16
+ * namespace. Native SQLite owns atomicity and commit; there is no in-memory
17
+ * fallback.
9
18
  */
10
- export function createLocalBackendReplayStore(owner) {
19
+ export function createLocalBackendReplayStore(owner, backendId) {
11
20
  return {
12
21
  admit({ nonce, now, expiresAt }) {
13
22
  try {
14
23
  return owner.transactionSync(() => {
24
+ // Pre-release data is disposable: a table without `backend_id` is
25
+ // dropped and recreated in the current shape. Its nonces are dials
26
+ // that expire within Seam's 120 s window (two 60 s skews), the
27
+ // bounded span in which one of them could be replayed.
28
+ const existing = definitionSchema
29
+ .optional()
30
+ .parse(owner.sql
31
+ .exec("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'computer_local_dial_replay'")
32
+ .toArray()[0]);
33
+ if (existing !== undefined && !existing.sql.includes("backend_id"))
34
+ owner.sql.exec("DROP TABLE computer_local_dial_replay");
15
35
  owner.sql
16
36
  .exec(`CREATE TABLE IF NOT EXISTS computer_local_dial_replay (
17
37
  nonce TEXT PRIMARY KEY,
38
+ backend_id TEXT NOT NULL,
18
39
  expires_at INTEGER NOT NULL
19
40
  ) STRICT`);
41
+ owner.sql.exec("CREATE INDEX IF NOT EXISTS computer_local_dial_replay_backend ON computer_local_dial_replay (backend_id)");
20
42
  // Seam's last fresh instant is inclusive. Do not renew duplicate
21
43
  // deadlines, assume expiry order, or discard any still-live record.
22
44
  owner.sql.exec("DELETE FROM computer_local_dial_replay WHERE expires_at < ?", now);
@@ -25,11 +47,11 @@ export function createLocalBackendReplayStore(owner) {
25
47
  .toArray().length > 0)
26
48
  return "replayed";
27
49
  const count = countSchema.parse(owner.sql
28
- .exec("SELECT COUNT(*) AS count FROM computer_local_dial_replay")
50
+ .exec("SELECT COUNT(*) AS count FROM computer_local_dial_replay WHERE backend_id = ?", backendId)
29
51
  .toArray()[0]?.count);
30
52
  if (count >= maximumLocalDialReplayRecords)
31
53
  return "at-capacity";
32
- owner.sql.exec("INSERT INTO computer_local_dial_replay (nonce, expires_at) VALUES (?, ?)", nonce, expiresAt);
54
+ owner.sql.exec("INSERT INTO computer_local_dial_replay (nonce, backend_id, expires_at) VALUES (?, ?, ?)", nonce, backendId, expiresAt);
33
55
  return "admitted";
34
56
  });
35
57
  }
@@ -128,7 +128,7 @@ export function localBackendAudience(computerName, backendId) {
128
128
  */
129
129
  export async function verifyLocalBackendDial(request, computerName, backend, owner) {
130
130
  try {
131
- await verifySeamRequest(await importSeamVerifiers({ [backend.deviceId]: backend.publicKey }), localBackendAudience(computerName, attachedBackendId(backend)), request.method, request.url, request.headers.get(seamHeader), null, createLocalBackendReplayStore(owner));
131
+ await verifySeamRequest(await importSeamVerifiers({ [backend.deviceId]: backend.publicKey }), localBackendAudience(computerName, attachedBackendId(backend)), request.method, request.url, request.headers.get(seamHeader), null, createLocalBackendReplayStore(owner, attachedBackendId(backend)));
132
132
  return true;
133
133
  }
134
134
  catch (cause) {
@@ -34,6 +34,13 @@ export declare class ComputerProcessInventory {
34
34
  /** The backend selected at admission, retained across reconnects and restarts. */
35
35
  backend(id: string): string | undefined;
36
36
  exited(id: ComputerProcessId, exitCode: number | null, sync?: ComputerProcessSync): ComputerProcess;
37
+ /**
38
+ * The Computer slept after sitting idle, so every command still running on
39
+ * that backend stopped with it. Record them as ended by idle sleep, in one
40
+ * transaction, instead of leaving a later liveness probe to report an
41
+ * unexplained exit.
42
+ */
43
+ endedByIdleSleep(backend: string): void;
37
44
  /** Replace the retained output tail of one process, if its record is still kept. */
38
45
  setLog(id: ComputerProcessId, tail: ProcessLogSnapshot): void;
39
46
  /** The retained output tail of one process, or undefined when no record is kept. */
@@ -64,6 +64,7 @@ function toProcess(row) {
64
64
  sync: row.sync,
65
65
  startedAt: row.started_at,
66
66
  endedAt: row.ended_at,
67
+ endedReason: row.ended_reason,
67
68
  });
68
69
  }
69
70
  /**
@@ -75,7 +76,7 @@ function toProcess(row) {
75
76
  * storage (confirmed in workerd; node:sqlite does not enforce the same
76
77
  * sandbox, so a unit test against it alone would hide this).
77
78
  */
78
- const processSchemaVersion = 4;
79
+ const processSchemaVersion = 5;
79
80
  /** At most this many bytes of output text are retained per process. */
80
81
  const maximumLogBytes = 64 * 1024;
81
82
  /**
@@ -139,7 +140,7 @@ export class ComputerProcessInventory {
139
140
  storage.sql.exec("DELETE FROM computer_schema_meta WHERE id = 0");
140
141
  storage.sql.exec("INSERT INTO computer_schema_meta (id, version) VALUES (0, ?)", processSchemaVersion);
141
142
  }
142
- storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_processes (id TEXT PRIMARY KEY, request_scope TEXT NOT NULL, request_id TEXT NOT NULL, input_hash TEXT NOT NULL, backend TEXT NOT NULL, command TEXT NOT NULL, cwd TEXT, port INTEGER, state TEXT NOT NULL CHECK (state IN ('running','exited')), exit_code INTEGER, sync TEXT NOT NULL DEFAULT 'unknown' CHECK (sync IN ('synced','pending','unknown')), started_at INTEGER NOT NULL, ended_at INTEGER, log_tail TEXT NOT NULL DEFAULT '', log_truncated INTEGER NOT NULL DEFAULT 0, CHECK ((state = 'running') = (ended_at IS NULL)), UNIQUE(request_scope, request_id)) STRICT");
143
+ storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_processes (id TEXT PRIMARY KEY, request_scope TEXT NOT NULL, request_id TEXT NOT NULL, input_hash TEXT NOT NULL, backend TEXT NOT NULL, command TEXT NOT NULL, cwd TEXT, port INTEGER, state TEXT NOT NULL CHECK (state IN ('running','exited')), exit_code INTEGER, sync TEXT NOT NULL DEFAULT 'unknown' CHECK (sync IN ('synced','pending','unknown')), started_at INTEGER NOT NULL, ended_at INTEGER, ended_reason TEXT CHECK (ended_reason IN ('idle-sleep')), log_tail TEXT NOT NULL DEFAULT '', log_truncated INTEGER NOT NULL DEFAULT 0, CHECK ((state = 'running') = (ended_at IS NULL)), UNIQUE(request_scope, request_id)) STRICT");
143
144
  storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_exposed_ports (port INTEGER PRIMARY KEY CHECK (port BETWEEN 1 AND 65535), process_id TEXT) STRICT");
144
145
  });
145
146
  }
@@ -214,6 +215,22 @@ export class ComputerProcessInventory {
214
215
  return this.#require(id);
215
216
  });
216
217
  }
218
+ /**
219
+ * The Computer slept after sitting idle, so every command still running on
220
+ * that backend stopped with it. Record them as ended by idle sleep, in one
221
+ * transaction, instead of leaving a later liveness probe to report an
222
+ * unexplained exit.
223
+ */
224
+ endedByIdleSleep(backend) {
225
+ this.#storage.transactionSync(() => {
226
+ const ids = this.#storage.sql
227
+ .exec("SELECT id FROM computer_processes WHERE backend = ? AND state = 'running'", backend)
228
+ .toArray();
229
+ this.#storage.sql.exec("UPDATE computer_processes SET state = 'exited', exit_code = NULL, sync = 'unknown', ended_at = ?, ended_reason = 'idle-sleep' WHERE backend = ? AND state = 'running'", this.#now(), backend);
230
+ for (const { id } of ids)
231
+ this.#storage.sql.exec("DELETE FROM computer_exposed_ports WHERE process_id = ?", id);
232
+ });
233
+ }
217
234
  /** Replace the retained output tail of one process, if its record is still kept. */
218
235
  setLog(id, tail) {
219
236
  this.#storage.sql.exec("UPDATE computer_processes SET log_tail = ?, log_truncated = ? WHERE id = ?", tail.lines.join("\n"), tail.truncated ? 1 : 0, id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.8.8",
3
+ "version": "0.9.0",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -63,7 +63,7 @@
63
63
  "@fungi.computer/knapsack": "0.4.9",
64
64
  "@fungi.computer/seam": "0.2.5",
65
65
  "@fungi.computer/watchdog": "0.1.3",
66
- "@fungi.computer/computer-contract": "0.1.0",
66
+ "@fungi.computer/computer-contract": "0.2.0",
67
67
  "effect": "4.0.2",
68
68
  "just-bash": "3.4.2",
69
69
  "zod": "4.4.3"