@fungi.computer/oyster 0.13.0 → 0.13.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.
@@ -181,7 +181,8 @@ export async function createDefinitionEmission(source, recipe) {
181
181
  throw new Error("The App declaration is derived from a checked backend build");
182
182
  const file = join(source.snapshotPath, ".fungi-built-definition.mjs");
183
183
  await writeFile(file, declarationCode, { flag: "wx", mode: 0o600 });
184
- const result = await promisify(execFile)(process.execPath, [
184
+ signal.throwIfAborted();
185
+ const evaluation = promisify(execFile)(process.execPath, [
185
186
  "--input-type=module",
186
187
  "--eval",
187
188
  evaluateDefinition,
@@ -192,9 +193,22 @@ export async function createDefinitionEmission(source, recipe) {
192
193
  maxBuffer: 1024 * 1024,
193
194
  timeout: 30_000,
194
195
  signal,
195
- }).catch((cause) => {
196
- throw evaluationFailure(cause);
197
196
  });
197
+ const closed = new Promise((resolve) => {
198
+ evaluation.child.once("close", () => {
199
+ resolve();
200
+ });
201
+ });
202
+ let result;
203
+ try {
204
+ result = await evaluation.catch((cause) => {
205
+ signal.throwIfAborted();
206
+ throw evaluationFailure(cause);
207
+ });
208
+ }
209
+ finally {
210
+ await closed;
211
+ }
198
212
  return Schema.decodeUnknownSync(Schema.fromJsonString(appDeclarationSchema), { onExcessProperty: "error" })(result.stdout);
199
213
  },
200
214
  };
@@ -1,9 +1,15 @@
1
+ import { Buffer } from "node:buffer";
1
2
  import { type DefinitionEmission } from "./definition-emission.js";
2
3
  import { type AssetBuildPolicy } from "@fungi.computer/puffball/assets";
3
4
  import { parseSha256Digest, type AppId, type SourceRevision } from "@fungi.computer/puffball";
4
5
  import * as Effect from "effect/Effect";
5
6
  import * as esbuild from "esbuild";
6
7
  import { type AppPublisher, type ArtifactWriter, type BuildExecutor, type CapturedSource, type PinnedRecipe, type ReleaseCreator, type SourceCapture } from "./effect.js";
8
+ export declare class GitCommandError extends Error {
9
+ readonly exitCode: number | null;
10
+ constructor(message: string, exitCode: number | null);
11
+ }
12
+ export declare function runGit(args: readonly string[], cwd: string, signal: AbortSignal, environment?: NodeJS.ProcessEnv): Promise<Buffer>;
7
13
  /** Local proving adapter; accepted source policy is intentionally fixed to main. */
8
14
  export declare function createLocalSourceCapture(repositoryPath: string): SourceCapture;
9
15
  /** Capture saved App files for local authoring without modifying the customer's
@@ -1,12 +1,7 @@
1
+ import { CleanupError, stopProcessGroup } from "./process-group.js";
2
+ import { addAbortListener } from "node:events";
1
3
  import { Buffer } from "node:buffer";
2
4
  import { appBackendPackageMainFields, createDefinitionEmission, } from "./definition-emission.js";
3
- function logEvent(operation, cause) {
4
- console.error(JSON.stringify({
5
- source: "app-publisher",
6
- operation,
7
- failureKind: cause instanceof Error ? "Error" : "NonErrorThrown",
8
- }));
9
- }
10
5
  import { frozenSnapshotInstallArguments, packageScriptArguments, } from "./snapshot-package-store.js";
11
6
  import { isInsideSnapshot } from "./snapshot-programs/snapshot-path.js";
12
7
  import { writeImmutableArtifact, } from "./artifact-writer.js";
@@ -16,12 +11,11 @@ import { appBuildRecipe, readBuildManifest, resolveWorkspaceBuild, workspaceProj
16
11
  import { readBundledHtml } from "./bundled-html.js";
17
12
  import { createHash, randomBytes } from "node:crypto";
18
13
  import { spawn } from "node:child_process";
19
- import { createReadStream, mkdtempSync, realpathSync } from "node:fs";
14
+ import { createReadStream, mkdtempSync, realpathSync, rmSync } from "node:fs";
20
15
  import { access, cp, lstat, mkdir, readdir, readFile, rm, writeFile, } from "node:fs/promises";
21
16
  import { tmpdir } from "node:os";
22
17
  import { basename, dirname, isAbsolute, join, relative, resolve, sep, } from "node:path";
23
18
  import { pipeline } from "node:stream/promises";
24
- import { setTimeout as delay } from "node:timers/promises";
25
19
  import { parseArtifactRef, parseSha256Digest, parseSourceRevision, } from "@fungi.computer/puffball";
26
20
  import { appIdSchema } from "@fungi.computer/puffball/effect";
27
21
  import * as Effect from "effect/Effect";
@@ -50,15 +44,27 @@ async function sha256File(path, signal) {
50
44
  await pipeline(createReadStream(path), hash, { signal });
51
45
  return parseSha256Digest(`sha256:${hash.digest("hex")}`);
52
46
  }
53
- function runGit(args, cwd, signal, environment = repositoryEnvironment()) {
54
- return new Promise((resolve, reject) => {
55
- const child = spawn("git", [...args], {
56
- cwd,
57
- env: environment,
58
- shell: false,
59
- signal,
60
- stdio: ["ignore", "pipe", "pipe"],
61
- });
47
+ export class GitCommandError extends Error {
48
+ exitCode;
49
+ constructor(message, exitCode) {
50
+ super(message);
51
+ this.exitCode = exitCode;
52
+ }
53
+ }
54
+ export async function runGit(args, cwd, signal, environment = repositoryEnvironment()) {
55
+ signal.throwIfAborted();
56
+ const child = spawn("git", [...args], {
57
+ cwd,
58
+ env: environment,
59
+ shell: false,
60
+ detached: process.platform !== "win32",
61
+ signal,
62
+ stdio: ["ignore", "pipe", "pipe"],
63
+ });
64
+ let stopping;
65
+ const stop = () => (stopping ??= stopProcessGroup(child));
66
+ let listener;
67
+ const completion = new Promise((resolve, reject) => {
62
68
  const stdout = [];
63
69
  const stderr = [];
64
70
  let spawnError;
@@ -74,11 +80,21 @@ function runGit(args, cwd, signal, environment = repositoryEnvironment()) {
74
80
  }
75
81
  const detail = Buffer.concat(stderr).toString("utf8").trim();
76
82
  reject(spawnError ??
77
- new Error(detail.length > 0
83
+ new GitCommandError(detail.length > 0
78
84
  ? `git exited unsuccessfully: ${detail}`
79
- : `git exited unsuccessfully (${String(code)} ${String(terminationSignal)})`));
85
+ : `git exited unsuccessfully (${String(code)} ${String(terminationSignal)})`, code));
86
+ });
87
+ listener = addAbortListener(signal, () => {
88
+ void stop().catch(reject);
80
89
  });
81
90
  });
91
+ try {
92
+ return await completion;
93
+ }
94
+ finally {
95
+ listener?.[Symbol.dispose]();
96
+ await stop();
97
+ }
82
98
  }
83
99
  function extractTar(archivePath, destination, signal) {
84
100
  return new Promise((resolve, reject) => {
@@ -172,18 +188,47 @@ async function captureCommit(workdir, repositoryPath, sourceRevision, signal) {
172
188
  }
173
189
  function scopedSourceCapture(capture, failureMessage) {
174
190
  return {
175
- capture: () => Effect.acquireRelease(Effect.sync(() =>
176
- // Canonical, so every path derived from the scratch matches the
177
- // realpaths pnpm reports (macOS tmpdir is a symlink).
178
- realpathSync(mkdtempSync(join(tmpdir(), "app-publisher-")))), (workdir) => Effect.tryPromise({
179
- try: () => rm(workdir, { recursive: true, force: true }),
180
- catch: () => new Error("Local source cleanup failed"),
181
- }).pipe(Effect.catch((cause) => {
182
- logEvent("services/app-publisher/src/local.ts:289", cause);
183
- return Effect.void;
184
- }))).pipe(Effect.flatMap((workdir) => Effect.tryPromise({
185
- try: (signal) => capture(workdir, signal),
186
- catch: () => new Error(failureMessage),
191
+ capture: () => Effect.acquireRelease(Effect.sync(() => {
192
+ const scratch = mkdtempSync(join(tmpdir(), "app-publisher-"));
193
+ try {
194
+ // Canonical paths also work when macOS tmpdir is a symlink.
195
+ return realpathSync(scratch);
196
+ }
197
+ catch (cause) {
198
+ try {
199
+ rmSync(scratch, { recursive: true, force: true });
200
+ }
201
+ catch (cleanupCause) {
202
+ throw new CleanupError("Source snapshot cleanup failed", {
203
+ cause: cleanupCause,
204
+ });
205
+ }
206
+ throw cause;
207
+ }
208
+ }), (workdir) => Effect.promise(async () => {
209
+ try {
210
+ await rm(workdir, { recursive: true, force: true });
211
+ }
212
+ catch (cause) {
213
+ throw new CleanupError("Source snapshot cleanup failed", {
214
+ cause,
215
+ });
216
+ }
217
+ })).pipe(Effect.flatMap((workdir) => Effect.callback((resume, signal) => {
218
+ const pending = capture(workdir, signal);
219
+ pending.then((source) => {
220
+ resume(Effect.succeed(source));
221
+ }, (cause) => {
222
+ resume(cause instanceof CleanupError
223
+ ? Effect.die(cause)
224
+ : Effect.fail(new Error(failureMessage)));
225
+ });
226
+ // Callback interruption aborts signal, then awaits this cleanup.
227
+ // Git/tar must close before the enclosing scope removes scratch.
228
+ return Effect.promise(() => pending.then(() => undefined, (cause) => {
229
+ if (cause instanceof CleanupError)
230
+ throw cause;
231
+ }));
187
232
  }))),
188
233
  };
189
234
  }
@@ -535,46 +580,6 @@ export function createFirstPartyAppSourceDistribution(options) {
535
580
  });
536
581
  });
537
582
  }
538
- function htmlGroupExists(child) {
539
- if (child.pid === undefined)
540
- return false;
541
- try {
542
- process.kill(-child.pid, 0);
543
- return true;
544
- }
545
- catch (error) {
546
- if (error instanceof Error && "code" in error && error.code === "ESRCH")
547
- return false;
548
- throw error;
549
- }
550
- }
551
- // Narrow adaptation of Caps' workbench-process process-group teardown. The
552
- // Effect finalizer waits for cleanup before the source snapshot can be removed.
553
- async function stopHtmlGroup(child) {
554
- if (!htmlGroupExists(child))
555
- return;
556
- const deadline = Date.now() + 5_000;
557
- for (const [signal, until] of [
558
- ["SIGTERM", deadline - 1_000],
559
- ["SIGKILL", deadline],
560
- ]) {
561
- try {
562
- if (child.pid !== undefined)
563
- process.kill(-child.pid, signal);
564
- }
565
- catch (error) {
566
- if (!(error instanceof Error &&
567
- "code" in error &&
568
- error.code === "ESRCH"))
569
- throw error;
570
- }
571
- while (htmlGroupExists(child) && Date.now() < until)
572
- await delay(25);
573
- if (!htmlGroupExists(child))
574
- return;
575
- }
576
- throw new Error("HTML process group survived its five-second cleanup bound");
577
- }
578
583
  function runHtmlCommand(args, cwd, env, command = "pnpm", onOutput) {
579
584
  return Effect.acquireUseRelease(Effect.sync(() => {
580
585
  if (process.platform === "win32")
@@ -609,7 +614,7 @@ function runHtmlCommand(args, cwd, env, command = "pnpm", onOutput) {
609
614
  });
610
615
  });
611
616
  return { child, completion };
612
- }), ({ completion }) => Effect.tryPromise({ try: () => completion, catch: (error) => error }), ({ child }) => Effect.promise(() => stopHtmlGroup(child)));
617
+ }), ({ completion }) => Effect.tryPromise({ try: () => completion, catch: (error) => error }), ({ child }) => Effect.promise(() => stopProcessGroup(child)));
613
618
  }
614
619
  /** Load the bundled snapshot programs only when a snapshot build writes them,
615
620
  * so source-level importers of this module do not need the package build. */
@@ -663,14 +668,14 @@ function rebuildViteSnapshot(sourceSnapshotPath, recipe, options) {
663
668
  const appDirectory = join(sourceSnapshotPath, html.directory);
664
669
  const dependencies = join(appDirectory, ".publisher-dependencies.mjs");
665
670
  const program = join(appDirectory, ".publisher-build.mjs");
666
- yield* Effect.tryPromise({
671
+ yield* Effect.uninterruptible(Effect.tryPromise({
667
672
  try: async () => {
668
673
  const { dependencyBuildProgram, htmlBuildProgram } = await snapshotPrograms();
669
674
  await writeFile(dependencies, dependencyBuildProgram, { flag: "wx" });
670
675
  await writeFile(program, htmlBuildProgram, { flag: "wx" });
671
676
  },
672
677
  catch: (error) => error,
673
- });
678
+ }));
674
679
  // Keep the derived order and every diagnostic/emit check. Consecutive
675
680
  // projects share one warmed compiler; stylesheet steps stay separate.
676
681
  const groups = [];
@@ -841,19 +846,19 @@ function rebuildNpmSnapshot(sourceSnapshotPath, recipe, options) {
841
846
  const globalConfig = join(home, "global.npmrc");
842
847
  env.npm_config_userconfig = userConfig;
843
848
  env.npm_config_globalconfig = globalConfig;
844
- yield* Effect.tryPromise(async () => {
849
+ yield* Effect.uninterruptible(Effect.tryPromise(async () => {
845
850
  await mkdir(home, { recursive: true });
846
851
  await writeFile(userConfig, "", { flag: "wx" });
847
852
  await writeFile(globalConfig, "", { flag: "wx" });
848
- });
853
+ }));
849
854
  if ((yield* runHtmlCommand(["--version"], sourceSnapshotPath, env, "npm", options.onOutput)).trim() !== recipe.toolchain.npm)
850
855
  return yield* Effect.fail(new Error("Captured npm version differs from the recipe"));
851
856
  yield* runHtmlCommand(["ci", "--include=dev", "--ignore-scripts", "--no-audit", "--no-fund"], sourceSnapshotPath, env, "npm", options.onOutput);
852
857
  const program = join(sourceSnapshotPath, ".publisher-build.mjs");
853
- yield* Effect.tryPromise(async () => {
858
+ yield* Effect.uninterruptible(Effect.tryPromise(async () => {
854
859
  const { htmlBuildProgram } = await snapshotPrograms();
855
860
  await writeFile(program, htmlBuildProgram, { flag: "wx" });
856
- });
861
+ }));
857
862
  yield* runHtmlCommand([
858
863
  program,
859
864
  sourceSnapshotPath,
@@ -1045,13 +1050,16 @@ export function createLocalBuildExecutor(options = {}) {
1045
1050
  catch: (error) => error instanceof Error
1046
1051
  ? error
1047
1052
  : new Error("Local snapshot build failed"),
1048
- }), ({ context }) => Effect.tryPromise({
1049
- try: () => context.dispose(),
1050
- catch: () => new Error("Local build cleanup failed"),
1051
- }).pipe(Effect.catch((cause) => {
1052
- logEvent("services/app-publisher/src/local.ts:1262", cause);
1053
- return Effect.void;
1054
- })));
1053
+ }), ({ context }) => Effect.promise(async () => {
1054
+ try {
1055
+ await context.dispose();
1056
+ }
1057
+ catch (cause) {
1058
+ throw new CleanupError("Local compiler cleanup failed", {
1059
+ cause,
1060
+ });
1061
+ }
1062
+ }));
1055
1063
  },
1056
1064
  };
1057
1065
  const declarations = new WeakMap();
@@ -1067,11 +1075,16 @@ export function createLocalBuildExecutor(options = {}) {
1067
1075
  });
1068
1076
  emissions.set(source, emission);
1069
1077
  const output = yield* compiler.build(source, recipe);
1070
- const declaration = yield* Effect.tryPromise({
1071
- try: (signal) => emission.declare(signal),
1072
- catch: (cause) => cause instanceof Error
1073
- ? cause
1074
- : new Error("App declaration emission failed", { cause }),
1078
+ const declaration = yield* Effect.callback((resume, signal) => {
1079
+ const pending = emission.declare(signal);
1080
+ pending.then((value) => {
1081
+ resume(Effect.succeed(value));
1082
+ }, (cause) => {
1083
+ resume(Effect.fail(cause instanceof Error
1084
+ ? cause
1085
+ : new Error("App declaration emission failed", { cause })));
1086
+ });
1087
+ return Effect.promise(() => pending.then(() => undefined, () => undefined));
1075
1088
  });
1076
1089
  declarations.set(source, declaration);
1077
1090
  return output;
@@ -0,0 +1,6 @@
1
+ import type { ChildProcess } from "node:child_process";
2
+ export declare class CleanupError extends Error {
3
+ }
4
+ /** Inspect retained outcomes only at the owning cleanup barrier. */
5
+ export declare function awaitCleanup(completion: Promise<readonly PromiseSettledResult<void>[]>): Promise<void>;
6
+ export declare function stopProcessGroup(child: ChildProcess): Promise<void>;
@@ -0,0 +1,92 @@
1
+ import { setTimeout as delay } from "node:timers/promises";
2
+ export class CleanupError extends Error {
3
+ }
4
+ /** Inspect retained outcomes only at the owning cleanup barrier. */
5
+ export async function awaitCleanup(completion) {
6
+ for (const outcome of await completion)
7
+ if (outcome.status === "rejected")
8
+ throw outcome.reason;
9
+ }
10
+ function processGroupExists(child) {
11
+ if (child.pid === undefined)
12
+ return false;
13
+ try {
14
+ process.kill(-child.pid, 0);
15
+ return true;
16
+ }
17
+ catch (error) {
18
+ if (error instanceof Error && "code" in error && error.code === "ESRCH")
19
+ return false;
20
+ throw error;
21
+ }
22
+ }
23
+ // Narrow adaptation of Caps' workbench-process process-group teardown. The
24
+ // Effect finalizer waits for cleanup before the source snapshot can be removed.
25
+ export async function stopProcessGroup(child) {
26
+ try {
27
+ if (process.platform === "win32")
28
+ await stopChild(child);
29
+ else
30
+ await stopGroup(child);
31
+ }
32
+ catch (cause) {
33
+ throw new CleanupError("Owned process cleanup did not complete.", {
34
+ cause,
35
+ });
36
+ }
37
+ }
38
+ async function stopGroup(child) {
39
+ if (!processGroupExists(child))
40
+ return;
41
+ const deadline = Date.now() + 5_000;
42
+ for (const [signal, until] of [
43
+ ["SIGTERM", deadline - 1_000],
44
+ ["SIGKILL", deadline],
45
+ ]) {
46
+ try {
47
+ if (child.pid !== undefined)
48
+ process.kill(-child.pid, signal);
49
+ }
50
+ catch (error) {
51
+ if (!(error instanceof Error &&
52
+ "code" in error &&
53
+ error.code === "ESRCH"))
54
+ throw error;
55
+ }
56
+ while (processGroupExists(child) && Date.now() < until)
57
+ await delay(25);
58
+ if (!processGroupExists(child))
59
+ return;
60
+ }
61
+ throw new Error("Owned process group survived its five-second cleanup bound");
62
+ }
63
+ // Preserve the CLI's direct-child Windows lifetime. The local build owner and
64
+ // the process-group qualification remain POSIX; this is not a Windows tree kill.
65
+ async function stopChild(child) {
66
+ if (child.pid === undefined ||
67
+ child.exitCode !== null ||
68
+ child.signalCode !== null)
69
+ return;
70
+ let closed;
71
+ let force;
72
+ let deadline;
73
+ try {
74
+ await new Promise((resolve, reject) => {
75
+ closed = resolve;
76
+ child.once("close", closed);
77
+ child.kill("SIGTERM");
78
+ force = setTimeout(() => {
79
+ child.kill("SIGKILL");
80
+ }, 4_000);
81
+ deadline = setTimeout(() => {
82
+ reject(new Error("Owned child survived its five-second cleanup bound"));
83
+ }, 5_000);
84
+ });
85
+ }
86
+ finally {
87
+ clearTimeout(force);
88
+ clearTimeout(deadline);
89
+ if (closed !== undefined)
90
+ child.off("close", closed);
91
+ }
92
+ }
package/dist/build.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./build/effect.js";
2
- export * from "./build/local-build.js";
2
+ export { createLocalSourceCapture, createLocalDirectorySourceCapture, createArtifactsSourceCapture, createLocalCommitSourceCapture, createFirstPartyAppSourceDistribution, localSnapshotBuildOptions, createLocalBuildExecutor, createLocalArtifactWriter, createLocalAppPublisher, type FirstPartyAppSourceDistribution, type LocalBuildExecutorOptions, type LocalAppPublisherOptions, } from "./build/local-build.js";
3
3
  export { createDefinitionEmission, type AppBackendOutput, type DefinitionEmission, } from "./build/definition-emission.js";
4
4
  export { readBundledHtml } from "./build/bundled-html.js";
5
5
  export { bundledHtmlRecipe, boundedBundlePolicy } from "./build/html-recipe.js";
package/dist/build.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /** Node-only App source capture, compilation and artifact writing. */
2
2
  import { dependencyBuildProgram as bundledDependencyBuildProgram } from "#snapshot-program-scripts";
3
3
  export * from "./build/effect.js";
4
- export * from "./build/local-build.js";
4
+ export { createLocalSourceCapture, createLocalDirectorySourceCapture, createArtifactsSourceCapture, createLocalCommitSourceCapture, createFirstPartyAppSourceDistribution, localSnapshotBuildOptions, createLocalBuildExecutor, createLocalArtifactWriter, createLocalAppPublisher, } from "./build/local-build.js";
5
5
  export { createDefinitionEmission, } from "./build/definition-emission.js";
6
6
  export { readBundledHtml } from "./build/bundled-html.js";
7
7
  export { bundledHtmlRecipe, boundedBundlePolicy } from "./build/html-recipe.js";