@fungi.computer/computer 0.15.2 → 0.16.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.
@@ -78,17 +78,21 @@ export declare const computerGrantSchema: z.ZodObject<{
78
78
  }, z.core.$strict>;
79
79
  export type ComputerGrant = z.output<typeof computerGrantSchema>;
80
80
  /** State visible without knowing which application hosts the Computer. */
81
- export declare const computerStateSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
81
+ export declare const computerStateSchema: z.ZodUnion<readonly [z.ZodObject<{
82
82
  state: z.ZodEnum<{
83
83
  running: "running";
84
84
  sleeping: "sleeping";
85
85
  starting: "starting";
86
- start_failed: "start_failed";
87
86
  }>;
87
+ }, z.core.$strict>, z.ZodObject<{
88
+ state: z.ZodLiteral<"start_failed">;
89
+ }, z.core.$strict>, z.ZodObject<{
90
+ state: z.ZodLiteral<"start_failed">;
91
+ reason: z.ZodString;
88
92
  }, z.core.$strict>, z.ZodObject<{
89
93
  state: z.ZodLiteral<"queued">;
90
94
  position: z.ZodNumber;
91
- }, z.core.$strict>], "state">;
95
+ }, z.core.$strict>]>;
92
96
  export type ComputerRuntimeState = z.output<typeof computerStateSchema>;
93
97
  /** Hosted fallback order, with the same bound and identity law as the retained owner. */
94
98
  export declare const hostedBackingsSchema: z.ZodLazy<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
@@ -228,17 +232,21 @@ export declare const computerFactSchema: z.ZodLazy<z.ZodDiscriminatedUnion<[z.Zo
228
232
  }, z.core.$strict>, z.ZodObject<{
229
233
  kind: z.ZodLiteral<"lifecycle">;
230
234
  observedAt: z.ZodNumber;
231
- state: z.ZodDiscriminatedUnion<[z.ZodObject<{
235
+ state: z.ZodUnion<readonly [z.ZodObject<{
232
236
  state: z.ZodEnum<{
233
237
  running: "running";
234
238
  sleeping: "sleeping";
235
239
  starting: "starting";
236
- start_failed: "start_failed";
237
240
  }>;
241
+ }, z.core.$strict>, z.ZodObject<{
242
+ state: z.ZodLiteral<"start_failed">;
243
+ }, z.core.$strict>, z.ZodObject<{
244
+ state: z.ZodLiteral<"start_failed">;
245
+ reason: z.ZodString;
238
246
  }, z.core.$strict>, z.ZodObject<{
239
247
  state: z.ZodLiteral<"queued">;
240
248
  position: z.ZodNumber;
241
- }, z.core.$strict>], "state">;
249
+ }, z.core.$strict>]>;
242
250
  computerId: z.ZodString;
243
251
  provenance: z.ZodObject<{
244
252
  actor: z.ZodString;
@@ -33,10 +33,16 @@ export const computerGrantSchema = z.strictObject({
33
33
  provenance: computerProvenanceSchema,
34
34
  });
35
35
  /** State visible without knowing which application hosts the Computer. */
36
- export const computerStateSchema = z.discriminatedUnion("state", [
37
- // start_failed: a queued start ended without running; start again.
36
+ export const computerStateSchema = z.union([
37
+ z.strictObject({ state: z.enum(["running", "sleeping", "starting"]) }),
38
+ // An owed start ended without running; start again. `reason` says why
39
+ // where the reader asked for it or decodes extra keys: released strict
40
+ // readers, and Hubs from before it, see `start_failed` alone. Two shapes,
41
+ // not an optional key, keep the state plain JSON.
42
+ z.strictObject({ state: z.literal("start_failed") }),
38
43
  z.strictObject({
39
- state: z.enum(["running", "sleeping", "starting", "start_failed"]),
44
+ state: z.literal("start_failed"),
45
+ reason: z.string().min(1).max(1_024),
40
46
  }),
41
47
  z.strictObject({
42
48
  state: z.literal("queued"),
package/dist/index.d.ts CHANGED
@@ -342,7 +342,7 @@ export declare class ComputerDO extends ContainerComputer implements ComputerFil
342
342
  }
343
343
  export { WorkspaceProxy, WorkspaceServiceProxy };
344
344
  export { computerName, learnComputerName } from "./computer-name.js";
345
- export { OnBoxVmContainerHost, OnBoxVmTimeoutError, onBoxVmPlacement, onBoxVmIngress, type OnBoxVmEnv, type OnBoxVmOperation, type OnBoxVmState, } from "./on-box-vm.js";
345
+ export { OnBoxVmContainerHost, OnBoxVmTimeoutError, OnBoxVmUnavailableError, onBoxVmPlacement, onBoxVmIngress, type OnBoxVmEnv, type OnBoxVmOperation, type OnBoxVmState, } from "./on-box-vm.js";
346
346
  export { computerContainerSleepAfterMs } from "./container-policy.js";
347
347
  export { isVaultExecutionContext, vaultExecutionContext, withVaultExecution, type VaultGrantPort, type VaultHolder, type VaultReferenceAdmission, type VaultSecretGrant, type VaultSubject, } from "./vault-references.js";
348
348
  export * from "./computer-backends.js";
package/dist/index.js CHANGED
@@ -51,6 +51,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
53
  import { boundExecStream } from "./exec-output.js";
54
+ import { portPreviewFailure, portPreviewReply } from "./port-failure.js";
54
55
  export { createLocalBackendReplayStore } from "./local-backend-replay.js";
55
56
  import { computerInternetAccess } from "./internet-access.js";
56
57
  import { createComputerVaultReferences, isVaultExecutionContext, vaultExecutionHooks, } from "./vault-references.js";
@@ -897,13 +898,11 @@ export class ComputerDO extends ContainerComputer {
897
898
  const response = await this.withHostedResponse(hosted.backing, () => this.hostedContainerHost(hosted.backing)
898
899
  .port(port)
899
900
  .fetch(new Request(forwarded, { headers })));
900
- const safe = new Response(response.body, response);
901
- safe.headers.delete("set-cookie");
902
- return safe;
901
+ return await portPreviewReply(response, request, port);
903
902
  }
904
903
  catch (cause) {
905
904
  console.error("Computer runtime operation failed", cause);
906
- return Response.json({ error: "Computer port service is unavailable" }, { status: 502 });
905
+ return await portPreviewFailure("unreachable", request, port);
907
906
  }
908
907
  }
909
908
  async #withBinaryFile(operation) {
@@ -1747,7 +1746,7 @@ export class ComputerDO extends ContainerComputer {
1747
1746
  // entrypoints. The consuming Worker must re-export both from its main module.
1748
1747
  export { WorkspaceProxy, WorkspaceServiceProxy };
1749
1748
  export { computerName, learnComputerName } from "./computer-name.js";
1750
- export { OnBoxVmContainerHost, OnBoxVmTimeoutError, onBoxVmPlacement, onBoxVmIngress, } from "./on-box-vm.js";
1749
+ export { OnBoxVmContainerHost, OnBoxVmTimeoutError, OnBoxVmUnavailableError, onBoxVmPlacement, onBoxVmIngress, } from "./on-box-vm.js";
1751
1750
  export { computerContainerSleepAfterMs } from "./container-policy.js";
1752
1751
  export { isVaultExecutionContext, vaultExecutionContext, withVaultExecution, } from "./vault-references.js";
1753
1752
  export * from "./computer-backends.js";
@@ -40,6 +40,12 @@ export type OnBoxVmPlacement = Readonly<{
40
40
  }>;
41
41
  /** The registered host `hostId`, or `undefined` when this Worker has none. */
42
42
  export declare function onBoxVmPlacement(env: OnBoxVmEnv, hostId: string | undefined): OnBoxVmPlacement | undefined;
43
+ /** The host service refused an operation; `status` is its HTTP answer. */
44
+ export declare class OnBoxVmUnavailableError extends Error {
45
+ readonly operation: string;
46
+ readonly status: number;
47
+ constructor(operation: string, status: number, detail?: string);
48
+ }
43
49
  declare const vmStateSchema: z.ZodObject<{
44
50
  running: z.ZodBoolean;
45
51
  runtimeId: z.ZodNullable<z.ZodString>;
package/dist/on-box-vm.js CHANGED
@@ -39,9 +39,14 @@ export function onBoxVmPlacement(env, hostId) {
39
39
  const host = hostRegistry(env)[hostIdSchema.parse(hostId)];
40
40
  return host === undefined ? undefined : { hostId, origin: host.origin };
41
41
  }
42
- class OnBoxVmUnavailableError extends Error {
42
+ /** The host service refused an operation; `status` is its HTTP answer. */
43
+ export class OnBoxVmUnavailableError extends Error {
44
+ operation;
45
+ status;
43
46
  constructor(operation, status, detail = "") {
44
47
  super(`On-box Computer VM ${operation} failed with HTTP ${String(status)}${detail ? `: ${detail}` : ""}`);
48
+ this.operation = operation;
49
+ this.status = status;
45
50
  this.name = "OnBoxVmUnavailableError";
46
51
  }
47
52
  }
@@ -0,0 +1,21 @@
1
+ import * as Schema from "effect/Schema";
2
+ declare const hostRefusalSchema: Schema.Literals<readonly ["not_running", "not_listening", "unreachable"]>;
3
+ /**
4
+ * Why a Computer port preview has nothing to show. `not_running`: the
5
+ * Computer is asleep (a preview never wakes it). `not_listening`: nothing in
6
+ * the Computer answers on that port. `unreachable`: Fungi could not reach the
7
+ * Computer. `expired`: the preview link's five-minute ticket ran out.
8
+ */
9
+ export type PortPreviewFailure = Schema.Schema.Type<typeof hostRefusalSchema> | "unreachable" | "expired";
10
+ /**
11
+ * The one answer a failed preview gives: a styled page in plain words for a
12
+ * browser document, or `{ error: reason }` for a script's request. Raw
13
+ * upstream text never reaches the viewer.
14
+ */
15
+ export declare function portPreviewFailure(reason: PortPreviewFailure, request: Request, port?: number): Promise<Response>;
16
+ /**
17
+ * What a preview answers for a forwarded port reply: the VM host's own
18
+ * refusal in plain words, or the guest's reply as it is, minus cookies.
19
+ */
20
+ export declare function portPreviewReply(response: Response, request: Request, port: number): Promise<Response>;
21
+ export {};
@@ -0,0 +1,106 @@
1
+ import { html, raw } from "hono/html";
2
+ import * as Option from "effect/Option";
3
+ import * as Schema from "effect/Schema";
4
+ /**
5
+ * Names the VM host's own refusal of a port request (vmd's `refusePort` in
6
+ * tooling/celld-platform/computer-vm/vmd-proxy.mjs, which strips it from
7
+ * guest replies), so a preview can tell it from the guest's answer.
8
+ */
9
+ const portFailureHeader = "x-fungi-port-failure";
10
+ const hostRefusalSchema = Schema.Literals([
11
+ "not_running",
12
+ "not_listening",
13
+ "unreachable",
14
+ ]);
15
+ /**
16
+ * The VM host's typed refusal, or null when the reply is the guest's own.
17
+ * vmd pipes an upgrade's bytes raw, so on an upgrade request the header could
18
+ * be the guest's and is never read.
19
+ */
20
+ function hostPortRefusal(response, request) {
21
+ if (request.headers.has("upgrade"))
22
+ return null;
23
+ const named = Schema.decodeUnknownOption(hostRefusalSchema)(response.headers.get(portFailureHeader));
24
+ return Option.isSome(named) ? named.value : null;
25
+ }
26
+ const copy = {
27
+ not_running: () => ({
28
+ title: "This Computer is asleep",
29
+ detail: "Start it from Fungi, then reload this page to see the preview.",
30
+ }),
31
+ not_listening: (port) => ({
32
+ title: port === undefined
33
+ ? "Nothing is answering on this port"
34
+ : `Nothing is answering on port ${String(port)}`,
35
+ detail: "Start the server on this Computer, or check its port, then reload this page.",
36
+ }),
37
+ unreachable: () => ({
38
+ title: "Fungi couldn't reach this Computer",
39
+ detail: "Reload in a moment. If it keeps happening, report it.",
40
+ }),
41
+ expired: () => ({
42
+ title: "This preview link has expired",
43
+ detail: "Open the preview again from Fungi to get a fresh link.",
44
+ }),
45
+ };
46
+ const statuses = {
47
+ not_running: 503,
48
+ not_listening: 502,
49
+ unreachable: 502,
50
+ expired: 403,
51
+ };
52
+ // The preview origin is isolated and loads no Fungi asset, so the page's
53
+ // styling travels inline and follows the viewer's light or dark scheme.
54
+ const style = raw(`<style>
55
+ :root{color-scheme:light dark;--bg:#faf8f4;--card:#fff;--ink:#2a2622;--muted:#6f675e;--line:#e4ded4;--accent:#1f6f54;--accent-ink:#fff}
56
+ @media (prefers-color-scheme:dark){:root{--bg:#171512;--card:#211e1a;--ink:#f0ebe3;--muted:#a39a8e;--line:#3a352e;--accent:#58c29b;--accent-ink:#0f1a15}}
57
+ *{box-sizing:border-box}
58
+ body{margin:0;min-height:100vh;display:grid;place-items:center;background:var(--bg);color:var(--ink);font:16px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif;padding:1rem}
59
+ main{width:100%;max-width:28rem;background:var(--card);border:1px solid var(--line);border-radius:12px;padding:1.5rem}
60
+ h1{font-size:1.25rem;margin:0 0 .5rem}
61
+ p{margin:0 0 1rem;color:var(--muted)}
62
+ a{display:inline-block;font-weight:600;border-radius:8px;padding:.45rem 1rem;background:var(--accent);color:var(--accent-ink);text-decoration:none}
63
+ a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
64
+ </style>`);
65
+ /**
66
+ * The one answer a failed preview gives: a styled page in plain words for a
67
+ * browser document, or `{ error: reason }` for a script's request. Raw
68
+ * upstream text never reaches the viewer.
69
+ */
70
+ export async function portPreviewFailure(reason, request, port) {
71
+ const headers = { "Cache-Control": "no-store" };
72
+ const status = statuses[reason];
73
+ if (request.headers.get("sec-fetch-dest") !== "document")
74
+ return Response.json({ error: reason }, { status, headers });
75
+ const { title, detail } = copy[reason](port);
76
+ const page = await html `<!doctype html>
77
+ <html lang="en">
78
+ <meta charset="utf-8" />
79
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
80
+ <title>${title}</title>
81
+ ${style}
82
+ <main>
83
+ <h1>${title}</h1>
84
+ <p>${detail}</p>
85
+ ${reason === "expired" ? "" : html `<a href="">Reload</a>`}
86
+ </main>
87
+ </html>`;
88
+ return new Response(page, {
89
+ status,
90
+ headers: { ...headers, "Content-Type": "text/html; charset=utf-8" },
91
+ });
92
+ }
93
+ /**
94
+ * What a preview answers for a forwarded port reply: the VM host's own
95
+ * refusal in plain words, or the guest's reply as it is, minus cookies.
96
+ */
97
+ export async function portPreviewReply(response, request, port) {
98
+ const refused = hostPortRefusal(response, request);
99
+ if (refused !== null) {
100
+ await response.body?.cancel();
101
+ return await portPreviewFailure(refused, request, port);
102
+ }
103
+ const safe = new Response(response.body, response);
104
+ safe.headers.delete("set-cookie");
105
+ return safe;
106
+ }
package/dist/preview.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LONG, nanoid } from "@fungi.computer/cairn";
2
2
  import * as Schema from "effect/Schema";
3
3
  import * as Option from "effect/Option";
4
+ import { portPreviewFailure } from "./port-failure.js";
4
5
  const storedClaimsSchema = Schema.Struct({
5
6
  claims: Schema.String,
6
7
  expires_at: Schema.Number,
@@ -108,8 +109,9 @@ export class ComputerPortPreview {
108
109
  }
109
110
  async dispatchSigned(signature, request, forward) {
110
111
  const claims = await this.resolve(signature);
112
+ // An expired, unknown or altered ticket: the viewer needs a fresh link.
111
113
  if (claims === null)
112
- return new Response(null, { status: 403 });
114
+ return await portPreviewFailure("expired", request);
113
115
  return await forward(claims.selector, claims.port, request);
114
116
  }
115
117
  ensureTable() {
@@ -146,6 +148,6 @@ export async function computerPortPreviewRequest(request, origin, host) {
146
148
  return await host.forward(computerId, new Request(destination, forwarded));
147
149
  }
148
150
  catch {
149
- return new Response(null, { status: 503 });
151
+ return await portPreviewFailure("unreachable", request);
150
152
  }
151
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.15.2",
3
+ "version": "0.16.1",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -79,6 +79,7 @@
79
79
  "esbuild": "0.28.2",
80
80
  "eslint": "10.9.1",
81
81
  "oxlint": "1.78.0",
82
+ "playwright": "1.62.1",
82
83
  "tsx": "4.23.12",
83
84
  "typescript": "5.9.3",
84
85
  "uuid": "11.1.1",
@@ -125,8 +126,9 @@
125
126
  "test:nightly": "bash ../../tooling/proof-runner/run-proof.sh node test/egress-proof.mjs",
126
127
  "test:app-method-execution": "node test/app-method-execution-proof.mjs",
127
128
  "test:home-patch": "node test/home-instructions-patch-proof.mjs",
129
+ "test:preview-failure:browser": "node test/preview-failure.browser.mjs",
128
130
  "test:execution-lifecycle": "node test/execution-lifecycle-proof.mjs",
129
- "test:unit": "bash ../../tooling/proof-runner/hermetic-host.sh -- sh -c \"node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/egress-policy.test.mjs test/preview.test.mjs test/port-access.test.mjs test/process-wait.test.mjs test/fleet.test.mjs test/proof-worker.test.mjs test/proof-client.test.mjs test/native-readiness.test.mjs test/workspace-sync.test.mjs test/runtime-observer.test.mjs test/files-patch.test.mjs test/exec-output.test.mjs && node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/seam-signature.test.mjs test/local-backend-replay.test.mjs test/machine-link-reopen-replay.test.mjs test/computer-backends.test.mjs test/terminal-welcome.test.mjs test/on-box-vm.test.mjs && node test/terminal-protocol.test.mjs && node test/container-policy.test.mjs && node test/files-adapter-proof.mjs && node test/operation-record-proof.mjs && node test/process-inventory-proof.mjs\"",
131
+ "test:unit": "bash ../../tooling/proof-runner/hermetic-host.sh -- sh -c \"node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/egress-policy.test.mjs test/preview.test.mjs test/port-failure.test.mjs test/port-access.test.mjs test/process-wait.test.mjs test/fleet.test.mjs test/proof-worker.test.mjs test/proof-client.test.mjs test/native-readiness.test.mjs test/workspace-sync.test.mjs test/runtime-observer.test.mjs test/files-patch.test.mjs test/exec-output.test.mjs && node --test --test-concurrency=${BOTANICAL_TEST_WORKERS:-4} test/seam-signature.test.mjs test/local-backend-replay.test.mjs test/machine-link-reopen-replay.test.mjs test/computer-backends.test.mjs test/terminal-welcome.test.mjs test/on-box-vm.test.mjs && node test/terminal-protocol.test.mjs && node test/container-policy.test.mjs && node test/files-adapter-proof.mjs && node test/operation-record-proof.mjs && node test/process-inventory-proof.mjs\"",
130
132
  "test:fleet-native": "node test/fleet.native.mjs",
131
133
  "assemble:image": "node scripts/assemble-image.mjs"
132
134
  }