@fungi.computer/oyster 0.19.32 → 0.19.34

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.
@@ -81,16 +81,6 @@ export type Manager = "npm" | "pnpm";
81
81
  /** pnpm when it ran this command (`pnpm dlx`), otherwise npm. */
82
82
  export declare function detectManager(userAgent: string | undefined): "pnpm" | "npm";
83
83
  export declare function installTryDependencies(directory: string, manager: Manager, verbose: boolean, signal: AbortSignal): Promise<void>;
84
- /**
85
- * `wrangler dev` for one local Oyster. The devtools inspector takes a port the
86
- * kernel assigns (0): wrangler's default probes 9229 upward and binds it later,
87
- * so two Oysters (or any wrangler dev) starting together could both pick the
88
- * same port and one failed with "Address already in use".
89
- */
90
- export declare function wranglerDevArguments(input: {
91
- port: number;
92
- key: string;
93
- }): readonly string[];
94
84
  /** Provision the trial's separate encryption key through Wrangler's native secrets. */
95
85
  export declare function prepareTryModelKeyCustody(directory: string): Promise<void>;
96
86
  export declare function tryOyster(args: readonly string[], effects?: TryEffects, ui?: Cli, callerSignal?: AbortSignal): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  // `oyster try`: one command from nothing to a running local Oyster with a
2
2
  // Hello App. It scaffolds a small project (the standalone worker and wrangler
3
3
  // config this package ships, plus a package.json), installs it with the
4
- // caller's package manager, runs `wrangler dev` locally, installs the Hello
4
+ // caller's package manager, starts Wrangler’s local runtime, installs the Hello
5
5
  // App and opens the browser. Running it again in the same directory restarts
6
6
  // the server after refreshing the generated project files and dependencies.
7
7
  //
@@ -13,6 +13,7 @@
13
13
  // and --verbose streams them instead. Piped output or CI gets plain lines.
14
14
  import { spawn } from "node:child_process";
15
15
  import { startTrialComputer } from "./trial-computer.js";
16
+ import { createTrialOwner, prepareTrialPrivateState, prepareTrialSecretFile, } from "./try-custody.js";
16
17
  import { randomBytes } from "node:crypto";
17
18
  import { encode as encodeBase64Url } from "effect/encoding/Base64Url";
18
19
  import { addAbortListener } from "node:events";
@@ -58,6 +59,7 @@ Options:
58
59
  const localBuildPnpmVersion = "11.24.0";
59
60
  const requiredNodeMajor = 24;
60
61
  const ownerKeyFile = ".oyster-owner";
62
+ const trialWorkerFile = "node_modules/@fungi.computer/oyster/dist/standalone/try-worker.js";
61
63
  const logFile = join(".oyster", "wrangler.log");
62
64
  const installLogFile = join(".oyster", "install.log");
63
65
  const command = "npx @fungi.computer/oyster try";
@@ -229,12 +231,10 @@ async function scaffold(directory, own, spec, manager, signal) {
229
231
  const stage = await mkdtemp(join(dirname(directory), ".oyster-try-"));
230
232
  try {
231
233
  await refreshProject(stage, own, spec, manager, signal);
232
- await writeFile(join(stage, ".gitignore"), ".oyster/\n.wrangler/\nnode_modules/\n.oyster-owner\n");
234
+ await writeFile(join(stage, ".gitignore"), "node_modules/\n");
233
235
  const key = Array.from(randomBytes(24), (byte) => byte.toString(16).padStart(2, "0")).join("");
234
- await writeFile(join(stage, ownerKeyFile), key + "\n", {
235
- mode: 0o600,
236
- flag: "wx",
237
- });
236
+ await createTrialOwner(stage, key);
237
+ await prepareTrialPrivateState(stage);
238
238
  signal.throwIfAborted();
239
239
  await rename(stage, directory);
240
240
  return key;
@@ -414,35 +414,10 @@ async function waitUntilReady(url, child, timeoutMs, signal) {
414
414
  }
415
415
  return signal.aborted ? "exited" : "timeout";
416
416
  }
417
- /**
418
- * `wrangler dev` for one local Oyster. The devtools inspector takes a port the
419
- * kernel assigns (0): wrangler's default probes 9229 upward and binds it later,
420
- * so two Oysters (or any wrangler dev) starting together could both pick the
421
- * same port and one failed with "Address already in use".
422
- */
423
- export function wranglerDevArguments(input) {
424
- const url = `http://127.0.0.1:${String(input.port)}`;
425
- return [
426
- "dev",
427
- "--local",
428
- "--enable-containers=false",
429
- "--ip",
430
- "127.0.0.1",
431
- "--port",
432
- String(input.port),
433
- "--inspector-port",
434
- "0",
435
- "--var",
436
- `OYSTER_OWNER_KEY:${input.key}`,
437
- "--var",
438
- `OYSTER_ORIGIN:${url}`,
439
- "--var",
440
- "OYSTER_COMPUTER_BACKEND:local",
441
- ];
442
- }
443
417
  /** Provision the trial's separate encryption key through Wrangler's native secrets. */
444
418
  export async function prepareTryModelKeyCustody(directory) {
445
419
  try {
420
+ await prepareTrialPrivateState(directory);
446
421
  const native = await import("wrangler");
447
422
  const resolveKey = () => {
448
423
  const config = native.unstable_readConfig({
@@ -458,26 +433,13 @@ export async function prepareTryModelKeyCustody(directory) {
458
433
  const validate = async (key) => {
459
434
  await createCredentialSealer({ activeKeyId: "instance", keys: { instance: key } }, "fungi.oyster.credentials.v1").seal([], "Oyster credential custody check");
460
435
  };
461
- const secretFiles = [".dev.vars", ".env", ".env.local"];
462
- const ignored = "\n# Local Oyster secrets\n/.dev.vars\n/.env\n/.env.local\n";
463
- const ignorePath = join(directory, ".gitignore");
464
- const before = (await exists(ignorePath))
465
- ? await readFile(ignorePath, "utf8")
466
- : "";
467
- if (!before.includes(ignored))
468
- await appendFile(ignorePath, ignored);
469
- for (const name of secretFiles) {
470
- const path = join(directory, name);
471
- if (await exists(path))
472
- await chmod(path, 0o600);
473
- }
474
436
  const existing = resolveKey();
475
437
  if (existing !== undefined) {
476
438
  await validate(existing);
477
439
  return;
478
440
  }
479
441
  const key = encodeBase64Url(randomBytes(32));
480
- const path = join(directory, (await exists(join(directory, ".dev.vars"))) ? ".dev.vars" : ".env.local");
442
+ const path = await prepareTrialSecretFile(directory, (await exists(join(directory, ".dev.vars"))) ? ".dev.vars" : ".env.local");
481
443
  await appendFile(path, `\nOYSTER_CREDENTIAL_KEY=${key}\n`, { mode: 0o600 });
482
444
  await chmod(path, 0o600);
483
445
  const provisioned = resolveKey();
@@ -492,9 +454,9 @@ export async function prepareTryModelKeyCustody(directory) {
492
454
  function startWrangler(input) {
493
455
  return (async () => {
494
456
  input.signal.throwIfAborted();
495
- const wrangler = join(input.directory, "node_modules", "wrangler", "bin", "wrangler.js");
496
- if (!(await exists(wrangler)))
497
- throw new TryError(`wrangler is not installed in ${input.directory}.`, `Run \`npm install\` there, then run \`${retryCommand(input.directory, input.port)}\` again.`);
457
+ const workerHost = join(input.directory, trialWorkerFile);
458
+ if (!(await exists(workerHost)))
459
+ throw new TryError(`The local Oyster worker is not installed in ${input.directory}.`, `Run \`npm install\` there, then run \`${retryCommand(input.directory, input.port)}\` again.`);
498
460
  await prepareTryModelKeyCustody(input.directory);
499
461
  await mkdir(join(input.directory, ".oyster"), { recursive: true });
500
462
  const log = join(input.directory, logFile);
@@ -502,22 +464,29 @@ function startWrangler(input) {
502
464
  const logStream = createWriteStream(log);
503
465
  const logClosed = Promise.allSettled([finished(logStream)]);
504
466
  const url = `http://127.0.0.1:${String(input.port)}`;
505
- const child = spawn(process.execPath, [wrangler, ...wranglerDevArguments({ port: input.port, key: input.key })], {
467
+ const child = spawn(process.execPath, [workerHost], {
506
468
  cwd: input.directory,
507
469
  detached: process.platform !== "win32",
508
470
  signal: input.signal,
509
- stdio: ["ignore", "pipe", "pipe"],
471
+ stdio: ["pipe", "pipe", "pipe"],
510
472
  env: { ...process.env, WRANGLER_SEND_METRICS: "false" },
511
473
  });
512
474
  captureOutput(child, logStream, input.verbose);
513
475
  const startupError = firstWranglerError(child);
514
476
  const lifetime = new AbortController();
515
477
  const spawnError = new Promise((done) => {
516
- child.once("error", () => {
478
+ const failed = () => {
517
479
  done("spawn-error");
518
480
  lifetime.abort();
519
- });
481
+ };
482
+ child.once("error", failed);
483
+ child.stdin.once("error", failed);
520
484
  });
485
+ child.stdin.end(JSON.stringify({
486
+ directory: input.directory,
487
+ port: input.port,
488
+ key: input.key,
489
+ }));
521
490
  const exited = new Promise((done) => {
522
491
  child.on("close", (code) => {
523
492
  lifetime.abort();
@@ -711,6 +680,7 @@ export async function tryOyster(args, effects = realEffects, ui = createTryCli(a
711
680
  key = await task(`Creating ${basename(directory)}`, () => scaffold(directory, own, values.package, manager, signal));
712
681
  }
713
682
  else {
683
+ await prepareTrialPrivateState(directory);
714
684
  key = (await readFile(keyPath, "utf8")).trim();
715
685
  ui.info(`Restarting the Oyster in ${directory}`);
716
686
  const manager = (await exists(join(directory, "pnpm-workspace.yaml")))
@@ -720,7 +690,7 @@ export async function tryOyster(args, effects = realEffects, ui = createTryCli(a
720
690
  }
721
691
  if (fresh ||
722
692
  (await exists(join(directory, pendingInstall))) ||
723
- !(await exists(join(directory, "node_modules/wrangler/bin/wrangler.js")))) {
693
+ !(await exists(join(directory, trialWorkerFile)))) {
724
694
  const manager = (await exists(join(directory, "pnpm-workspace.yaml")))
725
695
  ? "pnpm"
726
696
  : "npm";
@@ -0,0 +1,6 @@
1
+ /** Fresh owner bytes never enter the project or its staging directory. */
2
+ export declare function createTrialOwner(directory: string, key: string): Promise<void>;
3
+ /** Native Wrangler filenames remain links into Fungi's existing sandbox mask. */
4
+ export declare function prepareTrialPrivateState(directory: string): Promise<void>;
5
+ /** Create a native secret file only when selected, preserving dotenv precedence. */
6
+ export declare function prepareTrialSecretFile(directory: string, name: ".dev.vars" | ".env.local"): Promise<string>;
@@ -0,0 +1,124 @@
1
+ import { constants, lstatSync } from "node:fs";
2
+ import { appendFile, readFile, copyFile, cp, mkdir, mkdtemp, realpath, rename, rm, symlink, writeFile, chmod, } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { dirname, join, sep } from "node:path";
5
+ const ownerFile = ".oyster-owner";
6
+ const privateBase = () => join(homedir(), ".config", "fungi", "oyster-trials");
7
+ async function newStore() {
8
+ const base = privateBase();
9
+ await mkdir(base, { recursive: true, mode: 0o700 });
10
+ return mkdtemp(join(base, "trial-"));
11
+ }
12
+ /** Atomically replace a regular project file with its protected-store link. */
13
+ async function linkFile(source, destination) {
14
+ const stage = await mkdtemp(join(dirname(source), ".oyster-link-"));
15
+ const temporary = join(stage, "link");
16
+ try {
17
+ await symlink(destination, temporary, "file");
18
+ await rename(temporary, source);
19
+ }
20
+ finally {
21
+ await rm(stage, { recursive: true, force: true });
22
+ }
23
+ }
24
+ /** Fresh owner bytes never enter the project or its staging directory. */
25
+ export async function createTrialOwner(directory, key) {
26
+ const store = await newStore();
27
+ try {
28
+ const target = join(store, ownerFile);
29
+ await writeFile(target, key + "\n", { mode: 0o600, flag: "wx" });
30
+ await symlink(target, join(directory, ownerFile), "file");
31
+ }
32
+ catch (cause) {
33
+ await rm(store, { recursive: true, force: true });
34
+ throw cause;
35
+ }
36
+ }
37
+ async function ownerStore(directory) {
38
+ const source = join(directory, ownerFile);
39
+ const entry = lstatSync(source);
40
+ if (entry.isSymbolicLink()) {
41
+ const target = await realpath(source);
42
+ const base = await realpath(privateBase());
43
+ if (!target.startsWith(base + sep))
44
+ throw new Error("Trial owner key is outside private Fungi storage");
45
+ return dirname(target);
46
+ }
47
+ if (!entry.isFile())
48
+ throw new Error("Trial owner key must be a file");
49
+ const store = await newStore();
50
+ const target = join(store, ownerFile);
51
+ // Copy before the atomic link replacement: interruption leaves the original
52
+ // owner key intact, including when the project and HOME use different disks.
53
+ await copyFile(source, target, constants.COPYFILE_EXCL);
54
+ await chmod(target, 0o600);
55
+ await linkFile(source, target);
56
+ return store;
57
+ }
58
+ async function protect(directory, store, name, kind) {
59
+ const source = join(directory, name);
60
+ const destination = join(store, name);
61
+ const entry = lstatSync(source, { throwIfNoEntry: false });
62
+ if (entry?.isSymbolicLink()) {
63
+ if ((await realpath(source)) !== (await realpath(destination)))
64
+ throw new Error("Trial private path points outside its store");
65
+ return;
66
+ }
67
+ if (entry !== undefined) {
68
+ if (lstatSync(destination, { throwIfNoEntry: false }) !== undefined)
69
+ throw new Error("Trial private storage conflicts with a project path");
70
+ if (kind === "file") {
71
+ if (!entry.isFile())
72
+ throw new Error("Trial secret must be a file");
73
+ await copyFile(source, destination, constants.COPYFILE_EXCL);
74
+ await chmod(destination, 0o600);
75
+ await linkFile(source, destination);
76
+ return;
77
+ }
78
+ if (!entry.isDirectory())
79
+ throw new Error("Trial state must be a directory");
80
+ try {
81
+ await rename(source, destination);
82
+ }
83
+ catch (cause) {
84
+ if (!(cause instanceof Error) ||
85
+ !("code" in cause) ||
86
+ cause.code !== "EXDEV")
87
+ throw cause;
88
+ await cp(source, destination, {
89
+ recursive: true,
90
+ force: false,
91
+ errorOnExist: true,
92
+ });
93
+ await rm(source, { recursive: true });
94
+ }
95
+ }
96
+ else if (kind === "directory") {
97
+ await mkdir(destination, { recursive: true, mode: 0o700 });
98
+ }
99
+ else if (lstatSync(destination, { throwIfNoEntry: false }) === undefined) {
100
+ await writeFile(destination, "", { mode: 0o600, flag: "wx" });
101
+ }
102
+ await symlink(destination, source, kind === "directory" ? "dir" : "file");
103
+ }
104
+ /** Native Wrangler filenames remain links into Fungi's existing sandbox mask. */
105
+ export async function prepareTrialPrivateState(directory) {
106
+ const store = await ownerStore(directory);
107
+ const ignored = "\n# Local Oyster secrets\n/.oyster\n/.wrangler\n/.oyster-owner\n/.dev.vars\n/.env\n/.env.local\n";
108
+ const ignorePath = join(directory, ".gitignore");
109
+ const before = lstatSync(ignorePath, { throwIfNoEntry: false }) === undefined
110
+ ? ""
111
+ : await readFile(ignorePath, "utf8");
112
+ if (!before.includes(ignored))
113
+ await appendFile(ignorePath, ignored);
114
+ for (const name of [".oyster", ".wrangler"])
115
+ await protect(directory, store, name, "directory");
116
+ for (const name of [".dev.vars", ".env", ".env.local"])
117
+ if (lstatSync(join(directory, name), { throwIfNoEntry: false }) !== undefined)
118
+ await protect(directory, store, name, "file");
119
+ }
120
+ /** Create a native secret file only when selected, preserving dotenv precedence. */
121
+ export async function prepareTrialSecretFile(directory, name) {
122
+ await protect(directory, await ownerStore(directory), name, "file");
123
+ return join(directory, name);
124
+ }
@@ -0,0 +1,7 @@
1
+ import { unstable_startWorker } from "wrangler";
2
+ /** Wrangler owns the local Worker; the trial owns its credentials and lifetime. */
3
+ export declare function trialWorkerConfiguration(input: {
4
+ directory: string;
5
+ port: number;
6
+ key: string;
7
+ }): Parameters<typeof unstable_startWorker>[0];
@@ -0,0 +1,66 @@
1
+ // Private child of `oyster try`: credentials arrive over stdin, never argv.
2
+ import { join } from "node:path";
3
+ import { unstable_startWorker } from "wrangler";
4
+ import * as z from "zod";
5
+ const inputSchema = z.strictObject({
6
+ directory: z.string().min(1),
7
+ port: z.number().int().min(1).max(65535),
8
+ key: z.string().min(1),
9
+ });
10
+ /** Wrangler owns the local Worker; the trial owns its credentials and lifetime. */
11
+ export function trialWorkerConfiguration(input) {
12
+ return {
13
+ config: join(input.directory, "wrangler.jsonc"),
14
+ sendMetrics: false,
15
+ bindings: {
16
+ OYSTER_OWNER_KEY: { type: "secret_text", value: input.key },
17
+ OYSTER_ORIGIN: {
18
+ type: "plain_text",
19
+ value: `http://127.0.0.1:${String(input.port)}`,
20
+ },
21
+ OYSTER_COMPUTER_BACKEND: { type: "plain_text", value: "local" },
22
+ },
23
+ dev: {
24
+ remote: false,
25
+ server: { hostname: "127.0.0.1", port: input.port },
26
+ inspector: false,
27
+ enableContainers: false,
28
+ },
29
+ };
30
+ }
31
+ async function main() {
32
+ const stopped = Promise.withResolvers();
33
+ const stop = () => {
34
+ stopped.resolve(undefined);
35
+ };
36
+ process.once("SIGTERM", stop);
37
+ process.once("SIGINT", stop);
38
+ try {
39
+ process.stdin.setEncoding("utf8");
40
+ let text = "";
41
+ for await (const chunk of process.stdin) {
42
+ text += String(chunk);
43
+ if (text.length > 64 * 1024)
44
+ throw new Error("Trial input too large");
45
+ }
46
+ const worker = await unstable_startWorker(trialWorkerConfiguration(inputSchema.parse(JSON.parse(text))));
47
+ try {
48
+ await worker.ready;
49
+ await stopped.promise;
50
+ }
51
+ finally {
52
+ await worker.dispose();
53
+ }
54
+ }
55
+ finally {
56
+ process.removeListener("SIGTERM", stop);
57
+ process.removeListener("SIGINT", stop);
58
+ }
59
+ }
60
+ if (import.meta.main) {
61
+ main().catch((cause) => {
62
+ // A provider or configuration error can contain credentials.
63
+ process.stderr.write("[ERROR] The local Oyster worker could not start.\n");
64
+ process.exitCode = cause instanceof z.ZodError ? 2 : 1;
65
+ });
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/oyster",
3
- "version": "0.19.32",
3
+ "version": "0.19.34",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Self-hosted Apps for people and AI agents, with a local trial and deployment to your Cloudflare account.",
6
6
  "homepage": "https://docs.fungi.computer/oyster",
@@ -344,7 +344,7 @@
344
344
  "@fungi.computer/burrow": "0.2.1",
345
345
  "@fungi.computer/cairn": "0.1.0",
346
346
  "@fungi.computer/capnweb": "0.12.0-fungi.0",
347
- "@fungi.computer/caps": "0.10.4",
347
+ "@fungi.computer/caps": "0.10.5",
348
348
  "@fungi.computer/carrier": "0.2.4",
349
349
  "@fungi.computer/computer": "0.8.2",
350
350
  "@fungi.computer/computer-contract": "0.1.0",
@@ -356,7 +356,7 @@
356
356
  "@fungi.computer/lantern": "0.4.2",
357
357
  "@fungi.computer/mycelium": "0.3.8",
358
358
  "@fungi.computer/nacre": "0.4.5",
359
- "@fungi.computer/parakeet": "0.11.9",
359
+ "@fungi.computer/parakeet": "0.11.10",
360
360
  "@fungi.computer/puffball": "0.5.5",
361
361
  "@fungi.computer/seam": "0.2.5",
362
362
  "@fungi.computer/shiitake": "0.12.4",