@opencode/util 0.0.0-dev-21844 → 0.0.0-dev-21846

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.
@@ -1,4 +1,6 @@
1
1
  import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
2
+ /** Closing a child's scope escalates to SIGKILL after this, unless the command sets its own `forceKillAfter`. */
3
+ export declare const RELEASE_FORCE_KILL_AFTER = "3 seconds";
2
4
  export declare class KilledBySignal extends Error {
3
5
  readonly signal: NodeJS.Signals;
4
6
  constructor(signal: NodeJS.Signals);
@@ -10,6 +10,9 @@ import { PassThrough } from "node:stream";
10
10
  import launch from "cross-spawn";
11
11
  import { makeGlobalNode } from "./effect/app-node.js";
12
12
  import { filesystem, path } from "./effect/app-node-platform.js";
13
+ import { Teardown } from "./teardown.js";
14
+ /** Closing a child's scope escalates to SIGKILL after this, unless the command sets its own `forceKillAfter`. */
15
+ export const RELEASE_FORCE_KILL_AFTER = "3 seconds";
13
16
  const toError = (err) => (err instanceof globalThis.Error ? err : new globalThis.Error(String(err)));
14
17
  const toTag = (err) => {
15
18
  switch (err.code) {
@@ -229,6 +232,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
229
232
  const closed = Deferred.makeUnsafe();
230
233
  const exited = Deferred.makeUnsafe();
231
234
  const proc = launch(command.command, command.args, opts);
235
+ let settle = () => { };
232
236
  let end = false;
233
237
  let exit;
234
238
  proc.on("error", (err) => {
@@ -242,11 +246,13 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
242
246
  if (end)
243
247
  return;
244
248
  end = true;
249
+ settle();
245
250
  Deferred.doneUnsafe(exited, Exit.succeed(exit ?? args));
246
251
  Deferred.doneUnsafe(closed, Exit.succeed(exit ?? args));
247
252
  });
248
253
  proc.on("spawn", () => {
249
- resume(Effect.succeed([proc, closed, exited]));
254
+ settle = Teardown.track(`child ${proc.pid} running: ${[command.command, ...command.args].join(" ").slice(0, 200)}`);
255
+ resume(Effect.succeed([proc, closed, exited, () => settle()]));
250
256
  });
251
257
  return Effect.sync(() => {
252
258
  proc.kill("SIGTERM");
@@ -254,7 +260,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
254
260
  });
255
261
  const spawn = Effect.fnUntraced(function* (command, opts) {
256
262
  yield* Effect.logInfo("spawning process", { command: command.command, args: command.args, cwd: opts.cwd });
257
- const [proc, closed, exited] = yield* launchProcess(command, opts);
263
+ const [proc, closed, exited, settle] = yield* launchProcess(command, opts);
258
264
  const stopOutput = yield* Deferred.make();
259
265
  // Register before process release so the deadline remains active during termination.
260
266
  yield* Effect.forkScoped(Effect.gen(function* () {
@@ -269,7 +275,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
269
275
  discard(proc.stdout);
270
276
  discard(proc.stderr);
271
277
  }));
272
- return [proc, closed, exited, stopOutput];
278
+ return [proc, closed, exited, stopOutput, settle];
273
279
  });
274
280
  const discard = (readable) => {
275
281
  if (!readable || readable.destroyed)
@@ -360,8 +366,13 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
360
366
  return yield* Effect.ignore(killGroup(command, proc, command.options.killSignal ?? "SIGTERM"));
361
367
  }
362
368
  }
363
- yield* Effect.ignore(stop(command, proc, closed, stopOutput, command.options));
364
- }, (effect, [proc, , , stopOutput]) => effect.pipe(Effect.ensuring(Effect.gen(function* () {
369
+ yield* Effect.ignore(stop(command, proc, closed, stopOutput, {
370
+ killSignal: command.options.killSignal,
371
+ forceKillAfter: command.options.forceKillAfter ?? RELEASE_FORCE_KILL_AFTER,
372
+ }));
373
+ }, (effect, [proc, , , stopOutput, settle]) => effect.pipe(Effect.ensuring(Effect.gen(function* () {
374
+ // A detached descendant holding the pipes can keep "close" from ever firing.
375
+ settle();
365
376
  yield* Deferred.succeed(stopOutput, undefined);
366
377
  proc.stdout?.destroy();
367
378
  proc.stderr?.destroy();
@@ -1,4 +1,5 @@
1
- import { Brand, Context, Effect, Layer } from "effect";
1
+ import { Brand, Context, Effect, Layer, Scope } from "effect";
2
+ import { Teardown } from "../teardown.js";
2
3
  export * as LayerNode from "./layer-node.js";
3
4
  const GraphTypeId = Symbol("LayerNode.Graph");
4
5
  const VarianceTypeId = Symbol("LayerNode.Variance");
@@ -109,6 +110,7 @@ export function compile(root, options = {}) {
109
110
  if (definition.kind === "unbound")
110
111
  throw new Error(`Unbound layer node: ${definition.name}`);
111
112
  const node = {
113
+ name: definition.name,
112
114
  implementation: definition.implementation,
113
115
  dependencies: definition.dependencies.flatMap((dependency) => resolve(dependency, isShared)),
114
116
  shared: isShared,
@@ -141,13 +143,27 @@ export function compile(root, options = {}) {
141
143
  const dependencies = node.dependencies.map(build);
142
144
  // Acquisition uses the selected cache, while lazy LayerMaps inherit the enclosing shared cache.
143
145
  const implementation = Layer.suspend(() => node.implementation.pipe(Layer.provide([ambient, ...dependencies])));
144
- const layer = Layer.fromBuild((_, scope) => buildContext(implementation, node.shared ? memoMap : local, scope));
146
+ const layer = Layer.fromBuild((_, scope) => trackRelease(node.name, scope, () => buildContext(implementation, node.shared ? memoMap : local, scope)));
145
147
  layers.set(node, layer);
146
148
  return layer;
147
149
  };
148
150
  return buildContext(roots.map(build).reduce((result, layer) => layer.pipe(Layer.provideMerge(result)), Layer.empty), memoMap, scope);
149
151
  });
150
152
  }
153
+ // The node's scope releases sequentially in reverse order, so a finalizer added before the build runs last and one
154
+ // added after it runs first: together they bracket the release of everything this node acquired. The build must
155
+ // not start before the first finalizer is added; Layer builds fork their scopes as soon as they are called.
156
+ function trackRelease(name, scope, build) {
157
+ return Effect.gen(function* () {
158
+ let settle = () => { };
159
+ yield* Scope.addFinalizer(scope, Effect.sync(() => settle()));
160
+ const result = yield* build();
161
+ yield* Scope.addFinalizer(scope, Effect.sync(() => {
162
+ settle = Teardown.track(`layer ${name} releasing`);
163
+ }));
164
+ return result;
165
+ });
166
+ }
151
167
  class BuildResult extends Context.Service()("@opencode/LayerNode/BuildResult") {
152
168
  }
153
169
  function buildContext(layer, memoMap, scope) {
@@ -0,0 +1,8 @@
1
+ export * as Teardown from "./teardown.js";
2
+ /** Record pending work; call the returned function once it settles. */
3
+ export declare function track(label: string): () => void;
4
+ /** Pending work, oldest first, with how long each has been pending. */
5
+ export declare function pending(now?: number): {
6
+ label: string;
7
+ ms: number;
8
+ }[];
@@ -0,0 +1,16 @@
1
+ export * as Teardown from "./teardown.js";
2
+ // Work that shutdown may be waiting on: layers releasing their resources and child processes that have not
3
+ // closed. Read when a shutdown deadline expires, to name what kept the process alive.
4
+ const entries = new Set();
5
+ /** Record pending work; call the returned function once it settles. */
6
+ export function track(label) {
7
+ const entry = { label, since: Date.now() };
8
+ entries.add(entry);
9
+ return () => {
10
+ entries.delete(entry);
11
+ };
12
+ }
13
+ /** Pending work, oldest first, with how long each has been pending. */
14
+ export function pending(now = Date.now()) {
15
+ return Array.from(entries, (entry) => ({ label: entry.label, ms: now - entry.since }));
16
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/util",
4
- "version": "0.0.0-dev-21844",
4
+ "version": "0.0.0-dev-21846",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {