@fungi.computer/computer 0.8.1 → 0.8.2

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/dist/index.js CHANGED
@@ -1135,9 +1135,9 @@ export class ComputerDO extends ContainerComputer {
1135
1135
  }
1136
1136
  if (start.port !== undefined && isComputerReservedPort(start.port))
1137
1137
  throw new ComputerPortReservedError(start.port);
1138
- const backend = this.requireHostedRoute().id;
1138
+ const backend = this.routeBackend(undefined, "exec").id;
1139
1139
  const workspace = await this.readyWorkspace();
1140
- const admission = this.#processes.admit(start, inputHash, accept);
1140
+ const admission = this.#processes.admit(backend, start, inputHash, accept);
1141
1141
  if (!admission.created) {
1142
1142
  if (executionContext !== undefined)
1143
1143
  await this.#renewExecution(admission.process.id);
@@ -1234,25 +1234,35 @@ export class ComputerDO extends ContainerComputer {
1234
1234
  .map((row) => observed.get(row.id) ?? row),
1235
1235
  });
1236
1236
  }
1237
+ async #processBackend(id) {
1238
+ const backend = this.#processes.backend(id);
1239
+ if (backend === undefined)
1240
+ return undefined;
1241
+ let route;
1242
+ try {
1243
+ route = this.routeBackend(backend, "exec");
1244
+ }
1245
+ catch (cause) {
1246
+ if (cause instanceof ComputerBackendUnavailableError)
1247
+ return undefined;
1248
+ throw cause;
1249
+ }
1250
+ return route.backend.kind === "local" ||
1251
+ (await this.containerRunning(route.backend))
1252
+ ? route
1253
+ : undefined;
1254
+ }
1237
1255
  async #observeOwnedProcess(row) {
1238
1256
  // A reservation or unresolved SDK preparation is not a native exit.
1239
1257
  if (this.#processStarts.has(row.id))
1240
1258
  return { ...row, state: "uncertain" };
1241
1259
  const execution = this.#executions.get(row.id);
1242
- let hosted = this.hostedRoute();
1243
- if (execution !== undefined) {
1244
- try {
1245
- hosted = this.hostedRoute(execution.backend);
1246
- if (hosted === undefined)
1247
- throw new ComputerBackendUnavailableError("no_backend");
1248
- }
1249
- catch {
1250
- await this.#closeExecution(row.id, "unknown");
1251
- return { ...row, state: "uncertain" };
1252
- }
1260
+ const hosted = await this.#processBackend(row.id);
1261
+ if (execution !== undefined && hosted === undefined) {
1262
+ await this.#closeExecution(row.id, "unknown");
1263
+ return { ...row, state: "uncertain" };
1253
1264
  }
1254
- if (hosted === undefined ||
1255
- !(await this.containerRunning(hosted.backing))) {
1265
+ if (hosted === undefined) {
1256
1266
  // Never wake a stopped backend. Absence before a known native handle
1257
1267
  // cannot settle a dispatch whose reply was lost.
1258
1268
  if (execution !== undefined && !execution.nativeOpened)
@@ -1337,22 +1347,15 @@ export class ComputerDO extends ContainerComputer {
1337
1347
  });
1338
1348
  }
1339
1349
  const execution = this.#executions.get(id);
1340
- let hosted = this.hostedRoute();
1341
- if (execution !== undefined) {
1342
- try {
1343
- hosted = this.hostedRoute(execution.backend);
1344
- if (hosted === undefined)
1345
- throw new ComputerBackendUnavailableError("no_backend");
1346
- }
1347
- catch {
1348
- await this.#closeExecution(id, "unknown");
1349
- return processKillOutputSchema.parse({
1350
- process: { ...row, state: "uncertain" },
1351
- signalled: false,
1352
- });
1353
- }
1350
+ const hosted = await this.#processBackend(row.id);
1351
+ if (execution !== undefined && hosted === undefined) {
1352
+ await this.#closeExecution(id, "unknown");
1353
+ return processKillOutputSchema.parse({
1354
+ process: { ...row, state: "uncertain" },
1355
+ signalled: false,
1356
+ });
1354
1357
  }
1355
- if (hosted === undefined || !(await this.containerRunning(hosted.backing)))
1358
+ if (hosted === undefined)
1356
1359
  return processKillOutputSchema.parse({
1357
1360
  process: execution !== undefined && !execution.nativeOpened
1358
1361
  ? { ...row, state: "uncertain" }
@@ -27,10 +27,12 @@ export declare class ComputerProcessInventory {
27
27
  /** A replay does not boot a backend or redispatch an exited command. */
28
28
  replay(input: z.output<typeof processStartInputSchema>, inputHash: string, accept: () => void): ComputerProcess | undefined;
29
29
  /** Reserve the process and only a newly created exposure in one transaction. */
30
- admit(input: z.output<typeof processStartInputSchema>, inputHash: string, accept: () => void): Readonly<{
30
+ admit(backend: string, input: z.output<typeof processStartInputSchema>, inputHash: string, accept: () => void): Readonly<{
31
31
  process: ComputerProcess;
32
32
  created: boolean;
33
33
  }>;
34
+ /** The backend selected at admission, retained across reconnects and restarts. */
35
+ backend(id: string): string | undefined;
34
36
  exited(id: ComputerProcessId, exitCode: number | null, sync?: ComputerProcessSync): ComputerProcess;
35
37
  /** Replace the retained output tail of one process, if its record is still kept. */
36
38
  setLog(id: ComputerProcessId, tail: ProcessLogSnapshot): void;
@@ -75,7 +75,7 @@ function toProcess(row) {
75
75
  * storage (confirmed in workerd; node:sqlite does not enforce the same
76
76
  * sandbox, so a unit test against it alone would hide this).
77
77
  */
78
- const processSchemaVersion = 3;
78
+ const processSchemaVersion = 4;
79
79
  /** At most this many bytes of output text are retained per process. */
80
80
  const maximumLogBytes = 64 * 1024;
81
81
  /**
@@ -139,7 +139,7 @@ export class ComputerProcessInventory {
139
139
  storage.sql.exec("DELETE FROM computer_schema_meta WHERE id = 0");
140
140
  storage.sql.exec("INSERT INTO computer_schema_meta (id, version) VALUES (0, ?)", processSchemaVersion);
141
141
  }
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, 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");
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
143
  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
144
  });
145
145
  }
@@ -165,7 +165,7 @@ export class ComputerProcessInventory {
165
165
  });
166
166
  }
167
167
  /** Reserve the process and only a newly created exposure in one transaction. */
168
- admit(input, inputHash, accept) {
168
+ admit(backend, input, inputHash, accept) {
169
169
  return this.#storage.transactionSync(() => {
170
170
  accept();
171
171
  const existing = this.#matching(input, inputHash);
@@ -195,12 +195,18 @@ export class ComputerProcessInventory {
195
195
  this.#storage.sql.exec("INSERT INTO computer_exposed_ports (port, process_id) VALUES (?, ?)", input.port, id);
196
196
  }
197
197
  }
198
- this.#storage.sql.exec("INSERT INTO computer_processes (id, request_scope, request_id, input_hash, command, cwd, port, state, started_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'running', ?)", id, input.requestScope, input.requestId, inputHash, input.command, input.cwd ?? null, input.port ?? null, now);
198
+ this.#storage.sql.exec("INSERT INTO computer_processes (id, request_scope, request_id, input_hash, backend, command, cwd, port, state, started_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'running', ?)", id, input.requestScope, input.requestId, inputHash, backend, input.command, input.cwd ?? null, input.port ?? null, now);
199
199
  // Each scope retains its own newest 64 rows, including running processes.
200
200
  this.#storage.sql.exec("DELETE FROM computer_processes WHERE request_scope = ? AND state = 'exited' AND id NOT IN (SELECT id FROM computer_processes WHERE request_scope = ? AND state = 'exited' ORDER BY started_at DESC, id DESC LIMIT MAX(0, ? - (SELECT COUNT(*) FROM computer_processes WHERE request_scope = ? AND state = 'running')))", input.requestScope, input.requestScope, maximumProcessRecords, input.requestScope);
201
201
  return { process: this.#require(id), created: true };
202
202
  });
203
203
  }
204
+ /** The backend selected at admission, retained across reconnects and restarts. */
205
+ backend(id) {
206
+ return this.#storage.sql
207
+ .exec("SELECT backend FROM computer_processes WHERE id = ?", id)
208
+ .toArray()[0]?.backend;
209
+ }
204
210
  exited(id, exitCode, sync = "unknown") {
205
211
  return this.#storage.transactionSync(() => {
206
212
  this.#storage.sql.exec("UPDATE computer_processes SET state = 'exited', exit_code = ?, sync = ?, ended_at = ? WHERE id = ? AND state = 'running'", exitCode, sync, this.#now(), id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -77,7 +77,7 @@
77
77
  "tsx": "4.23.12",
78
78
  "typescript": "5.9.3",
79
79
  "uuid": "11.1.1",
80
- "wrangler": "4.127.1"
80
+ "wrangler": "4.156.2"
81
81
  },
82
82
  "botanical": {
83
83
  "role": "runtime",