@mostra/runtime 0.6.0 → 0.6.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.
package/dist/bin.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { a as uninstallService, i as serviceStatus, n as startInstance, o as checkPlatform, r as installService, s as resolveHomePath, t as runtimeVersion } from "./instance-Da8UG_Qd.js";
2
+ import { a as uninstallService, i as serviceStatus, n as startInstance, o as checkPlatform, r as installService, s as resolveHomePath, t as runtimeVersion } from "./instance-BfGOToHo.js";
3
3
  import { s as realPath } from "./files-FygSJIzK.js";
4
4
  import { mkdirSync } from "node:fs";
5
5
  import { fileURLToPath } from "node:url";
@@ -92,7 +92,7 @@ function handleSignals(instance) {
92
92
  event: "uncaught",
93
93
  message: error.message
94
94
  })}\n`);
95
- instance.stop("uncaught exception", EXIT.failure);
95
+ instance.stop("uncaught exception", EXIT.failure, createDiagnostic("MOSTRA_HOST_INTERNAL_ERROR", { message: (error instanceof Error ? error.stack ?? `${error.name}: ${error.message}` : String(error)).slice(0, 8192) }));
96
96
  setTimeout(() => process.exit(EXIT.failure), 15e3).unref();
97
97
  });
98
98
  }
package/dist/bin.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bin.js","names":[],"sources":["../src/bin.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { mkdirSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\n/**\n * `mostra-runtime`, the runtime bundle's entry point (D85).\n *\n * mostra-runtime host [--home <dir>] the host protocol over stdio (`mostra host`)\n * mostra-runtime daemon [--home <dir>] [--always-on]\n * the home's daemon on its socket (`mostra daemon start`);\n * it exits after 30 idle minutes unless `--always-on`\n * mostra-runtime service install|uninstall|status [--home <dir>] [--no-start]\n * the home's login service (`mostra daemon install`, D62):\n * one JSON line with the service's file and state\n *\n * Both take `--bind <address>` to serve viewers on an address other than 127.0.0.1, and\n * `--url-host <name>`, the host name links use, which a wildcard address needs (D99). Neither is\n * remembered: every wider start is explicit.\n *\n * `daemon` prints one JSON line on stdout: `{\"status\":\"started\",…}` when it took the home,\n * `{\"status\":\"attached\",…}` when another daemon already owns it (then it exits 0), or\n * `{\"status\":\"failed\",\"diagnostic\":…}` (exit 3). `host` speaks only the protocol on stdout; when it\n * can't start, it answers the host's first request with the diagnostic and exits 3. Logs go to\n * `<home>/logs/supervisor.log`; warnings and errors are mirrored to stderr.\n */\nimport { parseArgs } from \"node:util\";\nimport {\n createDiagnostic,\n type Diagnostic,\n encodeLine,\n LineSplitter,\n RpcError,\n RpcPeer,\n} from \"@mostra/protocol\";\nimport { checkPlatform, resolveHomePath } from \"./home/index.ts\";\nimport { type Instance, runtimeVersion, startInstance } from \"./instance/index.ts\";\nimport { realPath } from \"./platform/index.ts\";\nimport { installService, serviceStatus, uninstallService } from \"./service/index.ts\";\n\n/** Exit codes, as INTERFACES §9 defines them for the CLI. */\nconst EXIT = { ok: 0, failure: 1, usage: 2, environment: 3 } as const;\n\nconst USAGE = [\n \"usage: mostra-runtime <host|daemon> [--home <dir>] [--bind <address> [--url-host <name>]] [--always-on]\",\n \" mostra-runtime service <install|uninstall|status> [--home <dir>] [--no-start]\",\n].join(\"\\n\");\n\n// A closed stdout or stderr (a starter that read the ready line and left) must not crash the\n// supervisor.\nprocess.stdout.on(\"error\", () => {});\nprocess.stderr.on(\"error\", () => {});\n\nconst mirror = (line: string): void => {\n process.stderr.write(line);\n};\n\nfunction diagnosticOf(error: unknown): Diagnostic {\n if (error instanceof RpcError) return error.diagnostic;\n return createDiagnostic(\"MOSTRA_HOST_INTERNAL_ERROR\", {\n message: `mostra-runtime failed unexpectedly: ${error instanceof Error ? error.message : String(error)}`,\n });\n}\n\n/**\n * In host mode, a supervisor that can't start still speaks the protocol: it answers the host's\n * requests with the diagnostic, then exits once the first is answered or stdin ends.\n */\nfunction refuseOverStdio(diagnostic: Diagnostic, exitCode: number): void {\n mirror(`${JSON.stringify({ level: \"error\", diagnostic })}\\n`);\n const splitter = new LineSplitter();\n const exit = (): void => {\n process.stdout.write(\"\", () => process.exit(exitCode));\n };\n const peer = new RpcPeer({\n send: (message) => {\n process.stdout.write(encodeLine(message));\n },\n onRequest: () => {\n setImmediate(exit);\n throw new RpcError(diagnostic);\n },\n });\n process.stdin.setEncoding(\"utf8\");\n process.stdin.on(\"data\", (chunk: string) => {\n try {\n for (const line of splitter.push(chunk)) peer.receiveLine(line);\n } catch {\n exit();\n }\n });\n process.stdin.on(\"end\", exit);\n process.stdin.on(\"error\", exit);\n setTimeout(exit, 30_000).unref();\n}\n\nfunction handleSignals(instance: Instance): void {\n // SIGTERM and SIGINT on POSIX; on Windows Ctrl+C and Ctrl+Break reach a console process as\n // SIGINT and SIGBREAK, and everything else stops it over the protocol (`mostra/shutdown`, D87).\n for (const signal of [\"SIGTERM\", \"SIGINT\", \"SIGBREAK\"] as const) {\n process.on(signal, () => void instance.stop(signal, EXIT.ok));\n }\n process.on(\"SIGPIPE\", () => {});\n // Last resort: children never outlive an exiting supervisor (FAILURE_MODEL §2).\n process.on(\"exit\", () => instance.processes.killAll());\n process.on(\"uncaughtException\", (error) => {\n mirror(`${JSON.stringify({ level: \"error\", event: \"uncaught\", message: error.message })}\\n`);\n void instance.stop(\"uncaught exception\", EXIT.failure);\n setTimeout(() => process.exit(EXIT.failure), 15_000).unref();\n });\n}\n\nasync function main(argv: readonly string[]): Promise<number> {\n let parsed: ReturnType<typeof parse>;\n try {\n parsed = parse(argv);\n } catch (error) {\n process.stderr.write(`${(error as Error).message}\\n${USAGE}\\n`);\n return EXIT.usage;\n }\n const { values, positionals } = parsed;\n if (values.version) {\n process.stdout.write(`${runtimeVersion()}\\n`);\n return EXIT.ok;\n }\n const [mode, ...rest] = positionals;\n if (mode === \"service\" && !values.help)\n return await service(rest, values.home, values[\"no-start\"] === true);\n if (values.help || (mode !== \"host\" && mode !== \"daemon\") || rest.length > 0) {\n (values.help ? process.stdout : process.stderr).write(`${USAGE}\\n`);\n return values.help ? EXIT.ok : EXIT.usage;\n }\n\n try {\n const started = await startInstance({\n mode,\n home: values.home,\n bind: values.bind,\n urlHost: values[\"url-host\"],\n alwaysOn: values[\"always-on\"] === true,\n mirror,\n ...(mode === \"host\" ? { stdio: { input: process.stdin, output: process.stdout } } : {}),\n });\n if (started.kind === \"attached\") {\n process.stdout.write(\n `${JSON.stringify({\n status: \"attached\",\n pid: started.record.pid,\n home: started.result.instance.home,\n socket: started.record.socket,\n revision: started.result.revision,\n })}\\n`,\n );\n return EXIT.ok;\n }\n const { instance } = started;\n handleSignals(instance);\n if (mode === \"daemon\") {\n process.stdout.write(\n `${JSON.stringify({\n status: \"started\",\n pid: instance.info.pid,\n home: instance.info.home,\n socket: instance.socketPath,\n reaped: instance.reaped.filter((child) => child.signalled.length > 0).length,\n })}\\n`,\n );\n }\n const stop = await instance.stopped;\n return stop.exitCode;\n } catch (error) {\n const diagnostic = diagnosticOf(error);\n const exitCode = error instanceof RpcError ? EXIT.environment : EXIT.failure;\n if (mode === \"host\") {\n refuseOverStdio(diagnostic, exitCode);\n return new Promise<number>(() => {});\n }\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic })}\\n`);\n mirror(`${JSON.stringify({ level: \"error\", diagnostic })}\\n`);\n return exitCode;\n }\n}\n\n/** `mostra-runtime service …`: the home's login service, as one JSON line (D62, RT-08). */\nasync function service(\n args: readonly string[],\n home: string | undefined,\n noStart: boolean,\n): Promise<number> {\n const [action, ...extra] = args;\n if ((action !== \"install\" && action !== \"uninstall\" && action !== \"status\") || extra.length > 0) {\n process.stderr.write(`${USAGE}\\n`);\n return EXIT.usage;\n }\n const gate = checkPlatform();\n if (gate) {\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic: gate })}\\n`);\n return EXIT.environment;\n }\n try {\n const resolved = resolveHomePath({ home, env: process.env });\n mkdirSync(resolved, { recursive: true, mode: 0o700 });\n const spec = {\n // As the daemon spells its home, so the service's name and hash are the daemon's (D82).\n home: realPath(resolved),\n node: process.execPath,\n entry: fileURLToPath(import.meta.url),\n };\n // Who asks, should a Windows uninstall stop the daemon over its pipe.\n const client = { name: \"mostra-runtime-service\", version: runtimeVersion() };\n const status =\n action === \"install\"\n ? await installService(spec, { noStart, client })\n : action === \"uninstall\"\n ? await uninstallService(spec, { noStart, client })\n : serviceStatus(spec);\n process.stdout.write(`${JSON.stringify({ status: \"ok\", action, service: status })}\\n`);\n return EXIT.ok;\n } catch (error) {\n const diagnostic = diagnosticOf(error);\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic })}\\n`);\n return EXIT.environment;\n }\n}\n\nfunction parse(argv: readonly string[]) {\n return parseArgs({\n args: [...argv],\n allowPositionals: true,\n options: {\n home: { type: \"string\" },\n bind: { type: \"string\" },\n \"url-host\": { type: \"string\" },\n \"always-on\": { type: \"boolean\" },\n \"no-start\": { type: \"boolean\" },\n help: { type: \"boolean\", short: \"h\" },\n version: { type: \"boolean\" },\n },\n });\n}\n\nconst code = await main(process.argv.slice(2));\n// Let stdout drain before exiting, so the status line is never cut off.\nprocess.stdout.write(\"\", () => process.exit(code));\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAM,OAAO;CAAE,IAAI;CAAG,SAAS;CAAG,OAAO;CAAG,aAAa;AAAE;AAE3D,MAAM,QAAQ,CACZ,2GACA,sFACF,CAAC,CAAC,KAAK,IAAI;AAIX,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AACnC,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AAEnC,MAAM,UAAU,SAAuB;CACrC,QAAQ,OAAO,MAAM,IAAI;AAC3B;AAEA,SAAS,aAAa,OAA4B;CAChD,IAAI,iBAAiB,UAAU,OAAO,MAAM;CAC5C,OAAO,iBAAiB,8BAA8B,EACpD,SAAS,uCAAuC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,IACvG,CAAC;AACH;;;;;AAMA,SAAS,gBAAgB,YAAwB,UAAw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,IAC9C,WAAW,cACT,MAAM,iBAAiB,MAAM;GAAE;GAAS;EAAO,CAAC,IAChD,cAAc,IAAI;EAC1B,QAAQ,OAAO,MAAM,GAAG,KAAK,UAAU;GAAE,QAAQ;GAAM;GAAQ,SAAS;EAAO,CAAC,EAAE,GAAG;EACrF,OAAO,KAAK;CACd,SAAS,OAAO;EACd,MAAM,aAAa,aAAa,KAAK;EACrC,QAAQ,OAAO,MAAM,GAAG,KAAK,UAAU;GAAE,QAAQ;GAAU;EAAW,CAAC,EAAE,GAAG;EAC5E,OAAO,KAAK;CACd;AACF;AAEA,SAAS,MAAM,MAAyB;CACtC,OAAO,UAAU;EACf,MAAM,CAAC,GAAG,IAAI;EACd,kBAAkB;EAClB,SAAS;GACP,MAAM,EAAE,MAAM,SAAS;GACvB,MAAM,EAAE,MAAM,SAAS;GACvB,YAAY,EAAE,MAAM,SAAS;GAC7B,aAAa,EAAE,MAAM,UAAU;GAC/B,YAAY,EAAE,MAAM,UAAU;GAC9B,MAAM;IAAE,MAAM;IAAW,OAAO;GAAI;GACpC,SAAS,EAAE,MAAM,UAAU;EAC7B;CACF,CAAC;AACH;AAEA,MAAM,OAAO,MAAM,KAAK,QAAQ,KAAK,MAAM,CAAC,CAAC;AAE7C,QAAQ,OAAO,MAAM,UAAU,QAAQ,KAAK,IAAI,CAAC"}
1
+ {"version":3,"file":"bin.js","names":[],"sources":["../src/bin.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { mkdirSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\n/**\n * `mostra-runtime`, the runtime bundle's entry point (D85).\n *\n * mostra-runtime host [--home <dir>] the host protocol over stdio (`mostra host`)\n * mostra-runtime daemon [--home <dir>] [--always-on]\n * the home's daemon on its socket (`mostra daemon start`);\n * it exits after 30 idle minutes unless `--always-on`\n * mostra-runtime service install|uninstall|status [--home <dir>] [--no-start]\n * the home's login service (`mostra daemon install`, D62):\n * one JSON line with the service's file and state\n *\n * Both take `--bind <address>` to serve viewers on an address other than 127.0.0.1, and\n * `--url-host <name>`, the host name links use, which a wildcard address needs (D99). Neither is\n * remembered: every wider start is explicit.\n *\n * `daemon` prints one JSON line on stdout: `{\"status\":\"started\",…}` when it took the home,\n * `{\"status\":\"attached\",…}` when another daemon already owns it (then it exits 0), or\n * `{\"status\":\"failed\",\"diagnostic\":…}` (exit 3). `host` speaks only the protocol on stdout; when it\n * can't start, it answers the host's first request with the diagnostic and exits 3. Logs go to\n * `<home>/logs/supervisor.log`; warnings and errors are mirrored to stderr.\n */\nimport { parseArgs } from \"node:util\";\nimport {\n createDiagnostic,\n type Diagnostic,\n encodeLine,\n LineSplitter,\n RpcError,\n RpcPeer,\n} from \"@mostra/protocol\";\nimport { checkPlatform, resolveHomePath } from \"./home/index.ts\";\nimport { type Instance, runtimeVersion, startInstance } from \"./instance/index.ts\";\nimport { realPath } from \"./platform/index.ts\";\nimport { installService, serviceStatus, uninstallService } from \"./service/index.ts\";\n\n/** Exit codes, as INTERFACES §9 defines them for the CLI. */\nconst EXIT = { ok: 0, failure: 1, usage: 2, environment: 3 } as const;\n\nconst USAGE = [\n \"usage: mostra-runtime <host|daemon> [--home <dir>] [--bind <address> [--url-host <name>]] [--always-on]\",\n \" mostra-runtime service <install|uninstall|status> [--home <dir>] [--no-start]\",\n].join(\"\\n\");\n\n// A closed stdout or stderr (a starter that read the ready line and left) must not crash the\n// supervisor.\nprocess.stdout.on(\"error\", () => {});\nprocess.stderr.on(\"error\", () => {});\n\nconst mirror = (line: string): void => {\n process.stderr.write(line);\n};\n\nfunction diagnosticOf(error: unknown): Diagnostic {\n if (error instanceof RpcError) return error.diagnostic;\n return createDiagnostic(\"MOSTRA_HOST_INTERNAL_ERROR\", {\n message: `mostra-runtime failed unexpectedly: ${error instanceof Error ? error.message : String(error)}`,\n });\n}\n\n/**\n * In host mode, a supervisor that can't start still speaks the protocol: it answers the host's\n * requests with the diagnostic, then exits once the first is answered or stdin ends.\n */\nfunction refuseOverStdio(diagnostic: Diagnostic, exitCode: number): void {\n mirror(`${JSON.stringify({ level: \"error\", diagnostic })}\\n`);\n const splitter = new LineSplitter();\n const exit = (): void => {\n process.stdout.write(\"\", () => process.exit(exitCode));\n };\n const peer = new RpcPeer({\n send: (message) => {\n process.stdout.write(encodeLine(message));\n },\n onRequest: () => {\n setImmediate(exit);\n throw new RpcError(diagnostic);\n },\n });\n process.stdin.setEncoding(\"utf8\");\n process.stdin.on(\"data\", (chunk: string) => {\n try {\n for (const line of splitter.push(chunk)) peer.receiveLine(line);\n } catch {\n exit();\n }\n });\n process.stdin.on(\"end\", exit);\n process.stdin.on(\"error\", exit);\n setTimeout(exit, 30_000).unref();\n}\n\nfunction handleSignals(instance: Instance): void {\n // SIGTERM and SIGINT on POSIX; on Windows Ctrl+C and Ctrl+Break reach a console process as\n // SIGINT and SIGBREAK, and everything else stops it over the protocol (`mostra/shutdown`, D87).\n for (const signal of [\"SIGTERM\", \"SIGINT\", \"SIGBREAK\"] as const) {\n process.on(signal, () => void instance.stop(signal, EXIT.ok));\n }\n process.on(\"SIGPIPE\", () => {});\n // Last resort: children never outlive an exiting supervisor (FAILURE_MODEL §2).\n process.on(\"exit\", () => instance.processes.killAll());\n process.on(\"uncaughtException\", (error) => {\n mirror(`${JSON.stringify({ level: \"error\", event: \"uncaught\", message: error.message })}\\n`);\n // The home's log keeps what failed and where, so a failure nobody watched stderr for still\n // names its cause (#35: an instance that ended with only \"uncaught exception\" in its log).\n void instance.stop(\n \"uncaught exception\",\n EXIT.failure,\n createDiagnostic(\"MOSTRA_HOST_INTERNAL_ERROR\", {\n message: (error instanceof Error\n ? (error.stack ?? `${error.name}: ${error.message}`)\n : String(error)\n ).slice(0, 8192),\n }),\n );\n setTimeout(() => process.exit(EXIT.failure), 15_000).unref();\n });\n}\n\nasync function main(argv: readonly string[]): Promise<number> {\n let parsed: ReturnType<typeof parse>;\n try {\n parsed = parse(argv);\n } catch (error) {\n process.stderr.write(`${(error as Error).message}\\n${USAGE}\\n`);\n return EXIT.usage;\n }\n const { values, positionals } = parsed;\n if (values.version) {\n process.stdout.write(`${runtimeVersion()}\\n`);\n return EXIT.ok;\n }\n const [mode, ...rest] = positionals;\n if (mode === \"service\" && !values.help)\n return await service(rest, values.home, values[\"no-start\"] === true);\n if (values.help || (mode !== \"host\" && mode !== \"daemon\") || rest.length > 0) {\n (values.help ? process.stdout : process.stderr).write(`${USAGE}\\n`);\n return values.help ? EXIT.ok : EXIT.usage;\n }\n\n try {\n const started = await startInstance({\n mode,\n home: values.home,\n bind: values.bind,\n urlHost: values[\"url-host\"],\n alwaysOn: values[\"always-on\"] === true,\n mirror,\n ...(mode === \"host\" ? { stdio: { input: process.stdin, output: process.stdout } } : {}),\n });\n if (started.kind === \"attached\") {\n process.stdout.write(\n `${JSON.stringify({\n status: \"attached\",\n pid: started.record.pid,\n home: started.result.instance.home,\n socket: started.record.socket,\n revision: started.result.revision,\n })}\\n`,\n );\n return EXIT.ok;\n }\n const { instance } = started;\n handleSignals(instance);\n if (mode === \"daemon\") {\n process.stdout.write(\n `${JSON.stringify({\n status: \"started\",\n pid: instance.info.pid,\n home: instance.info.home,\n socket: instance.socketPath,\n reaped: instance.reaped.filter((child) => child.signalled.length > 0).length,\n })}\\n`,\n );\n }\n const stop = await instance.stopped;\n return stop.exitCode;\n } catch (error) {\n const diagnostic = diagnosticOf(error);\n const exitCode = error instanceof RpcError ? EXIT.environment : EXIT.failure;\n if (mode === \"host\") {\n refuseOverStdio(diagnostic, exitCode);\n return new Promise<number>(() => {});\n }\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic })}\\n`);\n mirror(`${JSON.stringify({ level: \"error\", diagnostic })}\\n`);\n return exitCode;\n }\n}\n\n/** `mostra-runtime service …`: the home's login service, as one JSON line (D62, RT-08). */\nasync function service(\n args: readonly string[],\n home: string | undefined,\n noStart: boolean,\n): Promise<number> {\n const [action, ...extra] = args;\n if ((action !== \"install\" && action !== \"uninstall\" && action !== \"status\") || extra.length > 0) {\n process.stderr.write(`${USAGE}\\n`);\n return EXIT.usage;\n }\n const gate = checkPlatform();\n if (gate) {\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic: gate })}\\n`);\n return EXIT.environment;\n }\n try {\n const resolved = resolveHomePath({ home, env: process.env });\n mkdirSync(resolved, { recursive: true, mode: 0o700 });\n const spec = {\n // As the daemon spells its home, so the service's name and hash are the daemon's (D82).\n home: realPath(resolved),\n node: process.execPath,\n entry: fileURLToPath(import.meta.url),\n };\n // Who asks, should a Windows uninstall stop the daemon over its pipe.\n const client = { name: \"mostra-runtime-service\", version: runtimeVersion() };\n const status =\n action === \"install\"\n ? await installService(spec, { noStart, client })\n : action === \"uninstall\"\n ? await uninstallService(spec, { noStart, client })\n : serviceStatus(spec);\n process.stdout.write(`${JSON.stringify({ status: \"ok\", action, service: status })}\\n`);\n return EXIT.ok;\n } catch (error) {\n const diagnostic = diagnosticOf(error);\n process.stdout.write(`${JSON.stringify({ status: \"failed\", diagnostic })}\\n`);\n return EXIT.environment;\n }\n}\n\nfunction parse(argv: readonly string[]) {\n return parseArgs({\n args: [...argv],\n allowPositionals: true,\n options: {\n home: { type: \"string\" },\n bind: { type: \"string\" },\n \"url-host\": { type: \"string\" },\n \"always-on\": { type: \"boolean\" },\n \"no-start\": { type: \"boolean\" },\n help: { type: \"boolean\", short: \"h\" },\n version: { type: \"boolean\" },\n },\n });\n}\n\nconst code = await main(process.argv.slice(2));\n// Let stdout drain before exiting, so the status line is never cut off.\nprocess.stdout.write(\"\", () => process.exit(code));\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAM,OAAO;CAAE,IAAI;CAAG,SAAS;CAAG,OAAO;CAAG,aAAa;AAAE;AAE3D,MAAM,QAAQ,CACZ,2GACA,sFACF,CAAC,CAAC,KAAK,IAAI;AAIX,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AACnC,QAAQ,OAAO,GAAG,eAAe,CAAC,CAAC;AAEnC,MAAM,UAAU,SAA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.js CHANGED
@@ -1,3 +1,3 @@
1
- import { n as startInstance, t as runtimeVersion } from "./instance-Da8UG_Qd.js";
1
+ import { n as startInstance, t as runtimeVersion } from "./instance-BfGOToHo.js";
2
2
  import { t as connectToSupervisor } from "./child-Chiavhdx.js";
3
3
  export { connectToSupervisor, runtimeVersion, startInstance };
@@ -14462,14 +14462,24 @@ function createDev(options) {
14462
14462
  };
14463
14463
  /** The connection that started the session closed while it started: nothing may outlive it. */
14464
14464
  const abandoned = () => new RpcError(createDiagnostic("MOSTRA_HOST_CONNECTION_CLOSED", { message: "The connection that started this dev session closed while it started." }));
14465
- connection.signal.addEventListener("abort", () => void stop(), { once: true });
14465
+ let starting = true;
14466
+ connection.signal.addEventListener("abort", () => {
14467
+ if (starting) closed = true;
14468
+ else stop().catch((error) => log.warn("dev.stop_failed", {
14469
+ sessionId,
14470
+ error
14471
+ }));
14472
+ }, { once: true });
14466
14473
  try {
14467
14474
  if (connection.signal.aborted) throw abandoned();
14468
14475
  await sync();
14476
+ if (closed) throw abandoned();
14469
14477
  const described = await describe();
14478
+ if (closed) throw abandoned();
14470
14479
  manifestKey = JSON.stringify(described.manifest);
14471
14480
  nodeModules = await resolve(described.manifest.id);
14472
14481
  artifactId = described.artifactId;
14482
+ if (closed) throw abandoned();
14473
14483
  child = await startChild(described);
14474
14484
  if (closed) throw abandoned();
14475
14485
  const upstream = () => child ? {
@@ -14494,11 +14504,13 @@ function createDev(options) {
14494
14504
  ...params.port !== void 0 ? { shellPort: params.port } : {}
14495
14505
  });
14496
14506
  if (closed) throw abandoned();
14507
+ starting = false;
14497
14508
  sandbox.onSubmission((results) => notify("submission", { results }));
14498
14509
  } catch (error) {
14499
14510
  closed = true;
14500
14511
  sessions.delete(sessionId);
14501
14512
  await release();
14513
+ if (connection.signal.aborted) log.info("dev.stopped", { sessionId });
14502
14514
  throw error;
14503
14515
  }
14504
14516
  try {
@@ -16515,7 +16527,8 @@ async function assemble(home, options) {
16515
16527
  stopping ??= (async () => {
16516
16528
  log.info("instance.stopping", {
16517
16529
  reason,
16518
- exitCode
16530
+ exitCode,
16531
+ ...diagnostic ? { diagnostic } : {}
16519
16532
  });
16520
16533
  idle?.stop();
16521
16534
  stopWatchingPublishes();
@@ -16677,4 +16690,4 @@ function sourceDirectory(path) {
16677
16690
  //#endregion
16678
16691
  export { uninstallService as a, serviceStatus as i, startInstance as n, checkPlatform as o, installService as r, resolveHomePath as s, runtimeVersion as t };
16679
16692
 
16680
- //# sourceMappingURL=instance-Da8UG_Qd.js.map
16693
+ //# sourceMappingURL=instance-BfGOToHo.js.map