@fungi.computer/computer 0.16.6 → 0.17.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.
@@ -0,0 +1,38 @@
1
+ import { computerServiceSchema, type computerServiceDeclarationSchema } from "@fungi.computer/computer-contract";
2
+ import { z } from "zod";
3
+ import { type ExposedPortStorage } from "./port-ownership.js";
4
+ /** A declared service as stored, the shape the contract names. */
5
+ export type ComputerService = z.output<typeof computerServiceSchema>;
6
+ /** What a person or Agent declares: name, command and optional cwd, port and env. */
7
+ export type ComputerServiceDeclaration = z.output<typeof computerServiceDeclarationSchema>;
8
+ /** The JSON `fungi-services apply` reads on stdin to render the supervisor's directories. */
9
+ export type ApplyService = Pick<ComputerService, "name" | "command" | "cwd" | "env" | "enabled">;
10
+ export type ComputerServicesApply = Readonly<{
11
+ services: readonly Readonly<ApplyService>[];
12
+ }>;
13
+ /**
14
+ * The Computer's declared services: the durable rules, never the live state.
15
+ * Whether one is up is read from the supervisor on demand. A service's port is
16
+ * admitted through the same table as a process's, so a port has one owner.
17
+ */
18
+ export declare class ComputerServiceInventory {
19
+ #private;
20
+ constructor(storage: ExposedPortStorage, now: () => number);
21
+ /** Declare or replace one service; it is enabled, so the next apply starts it. */
22
+ declare(input: ComputerServiceDeclaration, declaredBy: string): ComputerService;
23
+ /** One service, or `undefined` when none is declared by that name. */
24
+ get(name: string): ComputerService | undefined;
25
+ /** Every declared service by name. */
26
+ list(): readonly ComputerService[];
27
+ /** Start or stop declaring a service as running; a stopped one stays declared. */
28
+ setEnabled(name: string, enabled: boolean): ComputerService;
29
+ /** Remove a declaration and the port it owned. */
30
+ remove(name: string): void;
31
+ /**
32
+ * Re-admit every declared service port. The process inventory rebuilds the
33
+ * exposure table when its own schema changes; the declarations outlive that.
34
+ */
35
+ ensurePorts(): void;
36
+ /** What `fungi-services apply` renders: every declaration and whether it runs. */
37
+ applyPayload(): ComputerServicesApply;
38
+ }
@@ -0,0 +1,123 @@
1
+ import { ComputerServiceLimitError, ComputerServiceNotFoundError, computerServiceSchema, maximumComputerServices, } from "@fungi.computer/computer-contract";
2
+ import { z } from "zod";
3
+ import { claimPort, createExposedPorts, releasePortsOwnedBy, } from "./port-ownership.js";
4
+ const envColumn = z.record(z.string(), z.string());
5
+ function toService(row) {
6
+ const env = envColumn.parse(JSON.parse(row.env));
7
+ const service = {
8
+ name: row.name,
9
+ command: row.command,
10
+ enabled: row.enabled === 1,
11
+ declaredBy: row.declared_by,
12
+ declaredAt: row.declared_at,
13
+ revision: row.revision,
14
+ };
15
+ if (row.cwd !== null)
16
+ service.cwd = row.cwd;
17
+ if (row.port !== null)
18
+ service.port = row.port;
19
+ if (Object.keys(env).length > 0)
20
+ service.env = env;
21
+ return computerServiceSchema.parse(service);
22
+ }
23
+ const ownerOf = (name) => `service:${name}`;
24
+ /**
25
+ * The Computer's declared services: the durable rules, never the live state.
26
+ * Whether one is up is read from the supervisor on demand. A service's port is
27
+ * admitted through the same table as a process's, so a port has one owner.
28
+ */
29
+ export class ComputerServiceInventory {
30
+ #storage;
31
+ #now;
32
+ constructor(storage, now) {
33
+ this.#storage = storage;
34
+ this.#now = now;
35
+ storage.transactionSync(() => {
36
+ createExposedPorts(storage);
37
+ storage.sql.exec("CREATE TABLE IF NOT EXISTS computer_services (name TEXT PRIMARY KEY, command TEXT NOT NULL, cwd TEXT, port INTEGER, env TEXT NOT NULL DEFAULT '{}', enabled INTEGER NOT NULL CHECK (enabled IN (0,1)), declared_by TEXT NOT NULL, declared_at INTEGER NOT NULL, revision INTEGER NOT NULL CHECK (revision >= 1)) STRICT");
38
+ });
39
+ }
40
+ #row(name) {
41
+ return this.#storage.sql
42
+ .exec("SELECT * FROM computer_services WHERE name = ?", name)
43
+ .toArray()[0];
44
+ }
45
+ /** Declare or replace one service; it is enabled, so the next apply starts it. */
46
+ declare(input, declaredBy) {
47
+ return this.#storage.transactionSync(() => {
48
+ const existing = this.#row(input.name);
49
+ if (existing === undefined) {
50
+ const count = this.#storage.sql
51
+ .exec("SELECT COUNT(*) AS count FROM computer_services")
52
+ .toArray()[0]?.count ?? maximumComputerServices;
53
+ if (count >= maximumComputerServices)
54
+ throw new ComputerServiceLimitError();
55
+ }
56
+ // Only a port that moves is released: keeping it must not reset its
57
+ // visibility or share links.
58
+ if (existing?.port != null && existing.port !== (input.port ?? null))
59
+ releasePortsOwnedBy(this.#storage, ownerOf(input.name));
60
+ if (input.port !== undefined)
61
+ claimPort(this.#storage, input.port, ownerOf(input.name));
62
+ this.#storage.sql.exec("INSERT INTO computer_services (name, command, cwd, port, env, enabled, declared_by, declared_at, revision) VALUES (?, ?, ?, ?, ?, 1, ?, ?, 1) ON CONFLICT(name) DO UPDATE SET command = excluded.command, cwd = excluded.cwd, port = excluded.port, env = excluded.env, enabled = 1, declared_by = excluded.declared_by, declared_at = excluded.declared_at, revision = revision + 1", input.name, input.command, input.cwd ?? null, input.port ?? null, JSON.stringify(input.env ?? {}), declaredBy, this.#now());
63
+ return this.#require(input.name);
64
+ });
65
+ }
66
+ #require(name) {
67
+ const row = this.#row(name);
68
+ if (row === undefined)
69
+ throw new ComputerServiceNotFoundError(name);
70
+ return toService(row);
71
+ }
72
+ /** One service, or `undefined` when none is declared by that name. */
73
+ get(name) {
74
+ const row = this.#row(name);
75
+ return row === undefined ? undefined : toService(row);
76
+ }
77
+ /** Every declared service by name. */
78
+ list() {
79
+ return this.#storage.sql
80
+ .exec("SELECT * FROM computer_services ORDER BY name")
81
+ .toArray()
82
+ .map(toService);
83
+ }
84
+ /** Start or stop declaring a service as running; a stopped one stays declared. */
85
+ setEnabled(name, enabled) {
86
+ return this.#storage.transactionSync(() => {
87
+ this.#require(name);
88
+ this.#storage.sql.exec("UPDATE computer_services SET enabled = ? WHERE name = ?", enabled ? 1 : 0, name);
89
+ return this.#require(name);
90
+ });
91
+ }
92
+ /** Remove a declaration and the port it owned. */
93
+ remove(name) {
94
+ this.#storage.transactionSync(() => {
95
+ this.#require(name);
96
+ releasePortsOwnedBy(this.#storage, ownerOf(name));
97
+ this.#storage.sql.exec("DELETE FROM computer_services WHERE name = ?", name);
98
+ });
99
+ }
100
+ /**
101
+ * Re-admit every declared service port. The process inventory rebuilds the
102
+ * exposure table when its own schema changes; the declarations outlive that.
103
+ */
104
+ ensurePorts() {
105
+ this.#storage.transactionSync(() => {
106
+ for (const service of this.list())
107
+ if (service.port !== undefined)
108
+ claimPort(this.#storage, service.port, ownerOf(service.name));
109
+ });
110
+ }
111
+ /** What `fungi-services apply` renders: every declaration and whether it runs. */
112
+ applyPayload() {
113
+ const services = this.list().map(({ name, command, cwd, env, enabled }) => {
114
+ const entry = { name, command, enabled };
115
+ if (cwd !== undefined)
116
+ entry.cwd = cwd;
117
+ if (env !== undefined)
118
+ entry.env = env;
119
+ return entry;
120
+ });
121
+ return { services };
122
+ }
123
+ }
@@ -36084,7 +36084,7 @@ function snapshotBuildEnvironment(sourceSnapshotPath) {
36084
36084
  };
36085
36085
  return env;
36086
36086
  }
36087
- function rebuildViteSnapshot(sourceSnapshotPath, recipe, options) {
36087
+ function rebuildViteSnapshot(sourceSnapshotPath, recipe, options, shared) {
36088
36088
  if (recipe.kind === "source-html")
36089
36089
  return rebuildNpmSnapshot(sourceSnapshotPath, recipe, options);
36090
36090
  return gen2(function* () {
@@ -36110,15 +36110,23 @@ function rebuildViteSnapshot(sourceSnapshotPath, recipe, options) {
36110
36110
  )
36111
36111
  );
36112
36112
  const installStarted = performance.now();
36113
- yield* run2([
36114
- ...productionWorkspaceSelectors(html),
36115
- ...frozenSnapshotInstallArguments(
36116
- options.packageStoreDirectory,
36117
- options.packageCacheDirectory
36118
- )
36119
- ]);
36113
+ if (shared === null)
36114
+ yield* run2([
36115
+ ...productionWorkspaceSelectors(html),
36116
+ ...frozenSnapshotInstallArguments(
36117
+ options.packageStoreDirectory,
36118
+ options.packageCacheDirectory
36119
+ )
36120
+ ]);
36121
+ else if (shared.snapshotPath !== sourceSnapshotPath || !productionWorkspaceSelectors(html).every(
36122
+ (selector) => shared.selectors.has(selector)
36123
+ ))
36124
+ return yield* fail5(
36125
+ new Error("The shared install did not hydrate this App's workspace")
36126
+ );
36120
36127
  const buildStarted = performance.now();
36121
- options.onPhase?.("install", buildStarted - installStarted);
36128
+ if (shared === null)
36129
+ options.onPhase?.("install", buildStarted - installStarted);
36122
36130
  const appDirectory = join5(sourceSnapshotPath, html.directory);
36123
36131
  const dependencies = join5(appDirectory, ".publisher-dependencies.mjs");
36124
36132
  const program = join5(appDirectory, ".publisher-build.mjs");
@@ -36132,8 +36140,11 @@ function rebuildViteSnapshot(sourceSnapshotPath, recipe, options) {
36132
36140
  catch: (error2) => error2
36133
36141
  })
36134
36142
  );
36143
+ const steps = html.steps.filter(
36144
+ (step) => step.kind !== "typescript" || !shared?.compiled.has(step.config)
36145
+ );
36135
36146
  const groups = [];
36136
- for (const step of html.steps) {
36147
+ for (const step of steps) {
36137
36148
  const last = groups.at(-1);
36138
36149
  if (step.kind === "typescript" && last?.every((member) => member.kind === "typescript"))
36139
36150
  last.push(step);
@@ -36166,6 +36177,9 @@ function rebuildViteSnapshot(sourceSnapshotPath, recipe, options) {
36166
36177
  ],
36167
36178
  appDirectory
36168
36179
  );
36180
+ for (const compiled of group)
36181
+ if (compiled.kind === "typescript")
36182
+ shared?.compiled.add(compiled.config);
36169
36183
  }
36170
36184
  yield* run2(
36171
36185
  [
@@ -36460,6 +36474,9 @@ function localSnapshotBuildOptions(source, recipe, emission) {
36460
36474
  return options;
36461
36475
  }
36462
36476
  function createLocalBuildExecutor(options = {}) {
36477
+ return createWorkspaceBuildExecutor(options, null);
36478
+ }
36479
+ function createWorkspaceBuildExecutor(options, shared) {
36463
36480
  const emissions = /* @__PURE__ */ new WeakMap();
36464
36481
  const compiler2 = {
36465
36482
  build: (source, recipe) => {
@@ -36526,7 +36543,7 @@ function createLocalBuildExecutor(options = {}) {
36526
36543
  const ui = yield* recipe.kind === "bundled-html" ? tryPromise2({
36527
36544
  try: () => readBundledHtml(source.snapshotPath, recipe),
36528
36545
  catch: () => new Error("Captured HTML bundle failed validation")
36529
- }) : rebuildViteSnapshot(source.snapshotPath, recipe, options);
36546
+ }) : rebuildViteSnapshot(source.snapshotPath, recipe, options, shared);
36530
36547
  const code = yield* compiler2.build(source, {
36531
36548
  bundle: true,
36532
36549
  entry,
@@ -36552,7 +36569,7 @@ function createLocalBuildExecutor(options = {}) {
36552
36569
  };
36553
36570
  });
36554
36571
  }
36555
- return recipe.format !== "esm" ? rebuildViteSnapshot(source.snapshotPath, recipe, options) : acquireUseRelease2(
36572
+ return recipe.format !== "esm" ? rebuildViteSnapshot(source.snapshotPath, recipe, options, shared) : acquireUseRelease2(
36556
36573
  tryPromise2({
36557
36574
  try: () => {
36558
36575
  const emission = emissions.get(source);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.16.6",
3
+ "version": "0.17.0",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",
@@ -50,6 +50,10 @@
50
50
  "./port-access": {
51
51
  "types": "./dist/port-access.d.ts",
52
52
  "import": "./dist/port-access.js"
53
+ },
54
+ "./services": {
55
+ "types": "./dist/services.d.ts",
56
+ "import": "./dist/services.js"
53
57
  }
54
58
  },
55
59
  "files": [
@@ -62,12 +66,12 @@
62
66
  "dependencies": {
63
67
  "@fungi.computer/cairn": "0.1.0",
64
68
  "@fungi.computer/capnweb": "0.12.0-fungi.0",
65
- "@fungi.computer/cloudflare-computer": "0.2.1-fungi.2",
69
+ "@fungi.computer/cloudflare-computer": "0.2.1-fungi.3",
66
70
  "@fungi.computer/cordyceps": "0.4.3",
67
- "@fungi.computer/knapsack": "0.4.23",
68
- "@fungi.computer/seam": "0.2.6",
69
- "@fungi.computer/watchdog": "0.1.4",
70
- "@fungi.computer/computer-contract": "0.5.11",
71
+ "@fungi.computer/knapsack": "0.4.24",
72
+ "@fungi.computer/seam": "0.2.7",
73
+ "@fungi.computer/watchdog": "0.1.5",
74
+ "@fungi.computer/computer-contract": "0.6.0",
71
75
  "effect": "4.0.2",
72
76
  "just-bash": "3.4.2",
73
77
  "zod": "4.4.3",
@@ -119,7 +123,7 @@
119
123
  "build:container": "bash ../../tooling/proof-runner/run-proof.sh docker build --load --platform linux/amd64 --provenance=false .",
120
124
  "build:native": "fungi-apply-patch-build --out native/bin",
121
125
  "clean": "node --input-type=module --eval \"import { rm } from 'node:fs/promises'; await rm('dist', { recursive: true, force: true });\"",
122
- "test": "pnpm run test:unit && pnpm run test:fleet-native && node test/files-rpc.native.mjs && node test/shell-groups.native.mjs && node test/port-proof.mjs && node test/terminal-container-proof.mjs && node test/ssh-container-proof.mjs && node test/fuse-startup-proof.mjs && node test/container-resource-proof.mjs && node test/runtime-proof.mjs && node test/process-schema-proof.mjs && node test/process-proof.mjs && node test/terminal-proof.mjs && pnpm run test:app-method-execution && pnpm run test:execution-lifecycle && pnpm run test:home-patch",
126
+ "test": "pnpm run test:unit && pnpm run test:fleet-native && node test/files-rpc.native.mjs && node test/shell-groups.native.mjs && node test/port-proof.mjs && node test/services-proof.mjs && node test/services-wake-proof.mjs && node test/terminal-container-proof.mjs && node test/ssh-container-proof.mjs && node test/fuse-startup-proof.mjs && node test/container-resource-proof.mjs && node test/runtime-proof.mjs && node test/process-schema-proof.mjs && node test/process-proof.mjs && node test/terminal-proof.mjs && pnpm run test:app-method-execution && pnpm run test:execution-lifecycle && pnpm run test:home-patch",
123
127
  "format:check": "node ../../tooling/repository/scripts/format-package-check.mjs",
124
128
  "lint:anti-slop": "node ../../tooling/repository/scripts/lint-package-anti-slop.mjs",
125
129
  "test:merge": "pnpm run test",
@@ -129,7 +133,8 @@
129
133
  "test:ssh-container": "node test/ssh-container-proof.mjs",
130
134
  "test:preview-failure:browser": "node test/preview-failure.browser.mjs",
131
135
  "test:execution-lifecycle": "node test/execution-lifecycle-proof.mjs",
132
- "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\"",
136
+ "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 test/tools-install.test.mjs test/services.test.mjs test/services-apply.test.mjs test/services-tools.test.mjs test/services-render.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\"",
137
+ "test:services": "node test/services-proof.mjs && node test/services-wake-proof.mjs",
133
138
  "test:fleet-native": "node test/fleet.native.mjs",
134
139
  "assemble:image": "node scripts/assemble-image.mjs"
135
140
  }