aisnitch 0.2.3 → 0.2.4

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.
@@ -3253,8 +3253,16 @@ var EventBus = class {
3253
3253
  },
3254
3254
  "Published event"
3255
3255
  );
3256
- this.emitter.emit("event", parsedEvent.data);
3257
- this.emitter.emit(`event:${parsedEvent.data.type}`, parsedEvent.data);
3256
+ try {
3257
+ this.emitter.emit("event", parsedEvent.data);
3258
+ } catch (error) {
3259
+ logger.warn({ error, eventType: parsedEvent.data.type }, "\u{1F4D6} Error in EventBus global subscriber");
3260
+ }
3261
+ try {
3262
+ this.emitter.emit(`event:${parsedEvent.data.type}`, parsedEvent.data);
3263
+ } catch (error) {
3264
+ logger.warn({ error, eventType: parsedEvent.data.type }, "\u{1F4D6} Error in EventBus typed subscriber");
3265
+ }
3258
3266
  return true;
3259
3267
  }
3260
3268
  /**
@@ -3520,6 +3528,7 @@ var WSServer = class {
3520
3528
  });
3521
3529
  socket.on("error", (error) => {
3522
3530
  logger.warn({ error }, "WebSocket consumer error");
3531
+ this.consumers.delete(socket);
3523
3532
  });
3524
3533
  const welcomeMessage = {
3525
3534
  type: "welcome",
@@ -3674,10 +3683,17 @@ var HTTPReceiver = class {
3674
3683
  }
3675
3684
  async handleRequest(request, response, options) {
3676
3685
  this.requestCount += 1;
3677
- const requestUrl = new URL(
3678
- request.url ?? "/",
3679
- `http://${this.host}:${this.port ?? options.port}`
3680
- );
3686
+ let requestUrl;
3687
+ try {
3688
+ requestUrl = new URL(
3689
+ request.url ?? "/",
3690
+ `http://${this.host}:${this.port ?? options.port}`
3691
+ );
3692
+ } catch {
3693
+ this.invalidRequestCount += 1;
3694
+ this.sendJson(response, 400, { error: "malformed request url" });
3695
+ return;
3696
+ }
3681
3697
  if (request.method === "GET" && requestUrl.pathname === "/health") {
3682
3698
  this.sendJson(response, 200, options.getHealthSnapshot());
3683
3699
  return;
@@ -4018,15 +4034,24 @@ var BaseAdapter = class {
4018
4034
  cwd: data.cwd ?? context.cwd
4019
4035
  }
4020
4036
  });
4021
- const published = await this.publishEventImplementation(event, {
4022
- cwd: context.cwd,
4023
- env: context.env,
4024
- hookPayload: context.hookPayload,
4025
- pid: context.pid,
4026
- sessionId,
4027
- source: context.source,
4028
- transcriptPath: context.transcriptPath
4029
- });
4037
+ let published;
4038
+ try {
4039
+ published = await this.publishEventImplementation(event, {
4040
+ cwd: context.cwd,
4041
+ env: context.env,
4042
+ hookPayload: context.hookPayload,
4043
+ pid: context.pid,
4044
+ sessionId,
4045
+ source: context.source,
4046
+ transcriptPath: context.transcriptPath
4047
+ });
4048
+ } catch (error) {
4049
+ logger.error(
4050
+ { error, eventType: type, adapter: this.name, sessionId },
4051
+ "\u{1F4D6} Failed to publish event \u2014 swallowing to prevent daemon crash"
4052
+ );
4053
+ published = false;
4054
+ }
4030
4055
  if (published) {
4031
4056
  this.eventsEmitted += 1;
4032
4057
  }
@@ -9154,22 +9179,40 @@ var AdapterRegistry = class {
9154
9179
  }
9155
9180
  /**
9156
9181
  * Starts every adapter enabled in the current AISnitch config.
9182
+ * 📖 Each adapter is started independently — one failure does not prevent
9183
+ * the others from starting.
9157
9184
  */
9158
9185
  async startAll(config) {
9159
9186
  for (const adapter of this.list()) {
9160
9187
  if (config.adapters[adapter.name]?.enabled !== true) {
9161
9188
  continue;
9162
9189
  }
9163
- await adapter.start();
9190
+ try {
9191
+ await adapter.start();
9192
+ } catch (error) {
9193
+ logger.error(
9194
+ { error, adapter: adapter.name },
9195
+ `\u{1F4D6} Failed to start adapter "${adapter.name}" \u2014 skipping`
9196
+ );
9197
+ }
9164
9198
  }
9165
9199
  }
9166
9200
  /**
9167
9201
  * Stops every adapter in reverse registration order.
9202
+ * 📖 Each adapter is stopped independently — one failure does not prevent
9203
+ * the others from being stopped.
9168
9204
  */
9169
9205
  async stopAll() {
9170
9206
  const adapters = this.list().reverse();
9171
9207
  for (const adapter of adapters) {
9172
- await adapter.stop();
9208
+ try {
9209
+ await adapter.stop();
9210
+ } catch (error) {
9211
+ logger.warn(
9212
+ { error, adapter: adapter.name },
9213
+ `\u{1F4D6} Error stopping adapter "${adapter.name}" \u2014 continuing`
9214
+ );
9215
+ }
9173
9216
  }
9174
9217
  }
9175
9218
  };
@@ -9274,26 +9317,32 @@ var Pipeline = class {
9274
9317
  await adapter.handleHook(payload);
9275
9318
  });
9276
9319
  }
9320
+ try {
9321
+ this.wsPort = await this.wsServer.start({
9322
+ port: resolvedWsPort,
9323
+ eventBus: this.eventBus,
9324
+ activeTools
9325
+ });
9326
+ this.httpPort = await this.httpReceiver.start({
9327
+ port: resolvedHttpPort,
9328
+ onHook: async (tool, payload) => {
9329
+ await this.handleHook(tool, payload);
9330
+ },
9331
+ getHealthSnapshot: () => this.getHealthSnapshot()
9332
+ });
9333
+ this.socketPath = await this.udsServer.start({
9334
+ socketPath,
9335
+ onEvent: async (event) => {
9336
+ await this.publishEvent(event);
9337
+ }
9338
+ });
9339
+ await this.adapterRegistry.startAll(config);
9340
+ } catch (error) {
9341
+ logger.error({ error }, "\u{1F4D6} Pipeline start failed \u2014 rolling back already-started components");
9342
+ await this.rollbackPartialStart();
9343
+ throw error;
9344
+ }
9277
9345
  this.startedAt = Date.now();
9278
- this.wsPort = await this.wsServer.start({
9279
- port: resolvedWsPort,
9280
- eventBus: this.eventBus,
9281
- activeTools
9282
- });
9283
- this.httpPort = await this.httpReceiver.start({
9284
- port: resolvedHttpPort,
9285
- onHook: async (tool, payload) => {
9286
- await this.handleHook(tool, payload);
9287
- },
9288
- getHealthSnapshot: () => this.getHealthSnapshot()
9289
- });
9290
- this.socketPath = await this.udsServer.start({
9291
- socketPath,
9292
- onEvent: async (event) => {
9293
- await this.publishEvent(event);
9294
- }
9295
- });
9296
- await this.adapterRegistry.startAll(config);
9297
9346
  logger.info(this.getStatus(), "Core pipeline started");
9298
9347
  return this.getStatus();
9299
9348
  }
@@ -9301,10 +9350,25 @@ var Pipeline = class {
9301
9350
  * Stops every pipeline component in reverse dependency order.
9302
9351
  */
9303
9352
  async stop() {
9304
- await this.adapterRegistry?.stopAll();
9305
- await this.httpReceiver.stop();
9306
- await this.udsServer.stop();
9307
- await this.wsServer.stop();
9353
+ const stopSafely = async (label, fn) => {
9354
+ try {
9355
+ await fn();
9356
+ } catch (error) {
9357
+ logger.warn({ error }, `\u{1F4D6} Error while stopping ${label} \u2014 continuing shutdown`);
9358
+ }
9359
+ };
9360
+ await stopSafely("adapter registry", async () => {
9361
+ await this.adapterRegistry?.stopAll();
9362
+ });
9363
+ await stopSafely("HTTP receiver", async () => {
9364
+ await this.httpReceiver.stop();
9365
+ });
9366
+ await stopSafely("UDS server", async () => {
9367
+ await this.udsServer.stop();
9368
+ });
9369
+ await stopSafely("WebSocket server", async () => {
9370
+ await this.wsServer.stop();
9371
+ });
9308
9372
  this.eventBus.unsubscribeAll();
9309
9373
  this.adapterRegistry = null;
9310
9374
  this.enabledTools.clear();
@@ -9321,11 +9385,50 @@ var Pipeline = class {
9321
9385
  registerHookHandler(tool, handler) {
9322
9386
  this.hookHandlers.set(tool, handler);
9323
9387
  }
9388
+ /**
9389
+ * 📖 Rolls back any components that were successfully started before a
9390
+ * failure occurred, preventing orphaned servers or leaking resources.
9391
+ */
9392
+ async rollbackPartialStart() {
9393
+ const stopSafe = async (label, fn) => {
9394
+ try {
9395
+ await fn();
9396
+ } catch (error) {
9397
+ logger.warn({ error }, `\u{1F4D6} Error rolling back ${label}`);
9398
+ }
9399
+ };
9400
+ await stopSafe("adapter registry", async () => {
9401
+ await this.adapterRegistry?.stopAll();
9402
+ });
9403
+ await stopSafe("UDS server", async () => {
9404
+ await this.udsServer.stop();
9405
+ });
9406
+ await stopSafe("HTTP receiver", async () => {
9407
+ await this.httpReceiver.stop();
9408
+ });
9409
+ await stopSafe("WebSocket server", async () => {
9410
+ await this.wsServer.stop();
9411
+ });
9412
+ this.adapterRegistry = null;
9413
+ this.enabledTools.clear();
9414
+ this.hookHandlers.clear();
9415
+ }
9324
9416
  /**
9325
9417
  * Publishes an event after best-effort context enrichment.
9418
+ * 📖 If enrichment fails, the original event is published un-enriched
9419
+ * rather than being dropped entirely.
9326
9420
  */
9327
9421
  async publishEvent(event, context = {}) {
9328
- const enrichedEvent = await this.contextDetector.enrich(event, context);
9422
+ let enrichedEvent;
9423
+ try {
9424
+ enrichedEvent = await this.contextDetector.enrich(event, context);
9425
+ } catch (error) {
9426
+ logger.warn(
9427
+ { error, eventId: event.id },
9428
+ "\u{1F4D6} Context enrichment failed \u2014 publishing un-enriched event"
9429
+ );
9430
+ enrichedEvent = event;
9431
+ }
9329
9432
  return this.eventBus.publish(enrichedEvent);
9330
9433
  }
9331
9434
  /**
@@ -9361,6 +9464,16 @@ var Pipeline = class {
9361
9464
  };
9362
9465
  }
9363
9466
  async handleHook(tool, payload) {
9467
+ try {
9468
+ await this.handleHookInner(tool, payload);
9469
+ } catch (error) {
9470
+ logger.error(
9471
+ { error, tool },
9472
+ "\u{1F4D6} Unhandled error in hook handler \u2014 swallowing to prevent daemon crash"
9473
+ );
9474
+ }
9475
+ }
9476
+ async handleHookInner(tool, payload) {
9364
9477
  if (!this.enabledTools.has(tool)) {
9365
9478
  logger.debug({ tool }, "Ignoring hook for disabled tool");
9366
9479
  return;
@@ -10536,18 +10649,22 @@ function parseSocketPayload(data) {
10536
10649
  return parsedEvent.success ? parsedEvent.data : null;
10537
10650
  }
10538
10651
  function parseUnknownPayload(data) {
10539
- if (typeof data === "string") {
10540
- return JSON.parse(data);
10541
- }
10542
- if (Array.isArray(data)) {
10543
- return JSON.parse(Buffer.concat(data).toString("utf8"));
10544
- }
10545
- if (data instanceof ArrayBuffer) {
10546
- return JSON.parse(
10547
- Buffer.from(new Uint8Array(data)).toString("utf8")
10548
- );
10652
+ try {
10653
+ if (typeof data === "string") {
10654
+ return JSON.parse(data);
10655
+ }
10656
+ if (Array.isArray(data)) {
10657
+ return JSON.parse(Buffer.concat(data).toString("utf8"));
10658
+ }
10659
+ if (data instanceof ArrayBuffer) {
10660
+ return JSON.parse(
10661
+ Buffer.from(new Uint8Array(data)).toString("utf8")
10662
+ );
10663
+ }
10664
+ return JSON.parse(Buffer.from(data).toString("utf8"));
10665
+ } catch {
10666
+ return null;
10549
10667
  }
10550
- return JSON.parse(Buffer.from(data).toString("utf8"));
10551
10668
  }
10552
10669
 
10553
10670
  // src/tui/hooks/useKeyBinds.ts
@@ -11275,16 +11392,20 @@ function ManagedDaemonApp({
11275
11392
  }
11276
11393
  function parseSocketPayload2(data) {
11277
11394
  let parsedPayload;
11278
- if (typeof data === "string") {
11279
- parsedPayload = JSON.parse(data);
11280
- } else if (Array.isArray(data)) {
11281
- parsedPayload = JSON.parse(Buffer.concat(data).toString("utf8"));
11282
- } else if (data instanceof ArrayBuffer) {
11283
- parsedPayload = JSON.parse(
11284
- Buffer.from(new Uint8Array(data)).toString("utf8")
11285
- );
11286
- } else {
11287
- parsedPayload = JSON.parse(Buffer.from(data).toString("utf8"));
11395
+ try {
11396
+ if (typeof data === "string") {
11397
+ parsedPayload = JSON.parse(data);
11398
+ } else if (Array.isArray(data)) {
11399
+ parsedPayload = JSON.parse(Buffer.concat(data).toString("utf8"));
11400
+ } else if (data instanceof ArrayBuffer) {
11401
+ parsedPayload = JSON.parse(
11402
+ Buffer.from(new Uint8Array(data)).toString("utf8")
11403
+ );
11404
+ } else {
11405
+ parsedPayload = JSON.parse(Buffer.from(data).toString("utf8"));
11406
+ }
11407
+ } catch {
11408
+ return null;
11288
11409
  }
11289
11410
  if (typeof parsedPayload === "object" && parsedPayload !== null && "type" in parsedPayload && parsedPayload.type === "welcome") {
11290
11411
  return null;
@@ -11931,16 +12052,20 @@ function hexToRgb(hexColor) {
11931
12052
  ];
11932
12053
  }
11933
12054
  function parseSocketMessage(data) {
11934
- if (typeof data === "string") {
11935
- return JSON.parse(data);
11936
- }
11937
- if (Array.isArray(data)) {
11938
- return JSON.parse(Buffer.concat(data).toString("utf8"));
11939
- }
11940
- if (data instanceof ArrayBuffer) {
11941
- return JSON.parse(Buffer.from(new Uint8Array(data)).toString("utf8"));
12055
+ try {
12056
+ if (typeof data === "string") {
12057
+ return JSON.parse(data);
12058
+ }
12059
+ if (Array.isArray(data)) {
12060
+ return JSON.parse(Buffer.concat(data).toString("utf8"));
12061
+ }
12062
+ if (data instanceof ArrayBuffer) {
12063
+ return JSON.parse(Buffer.from(new Uint8Array(data)).toString("utf8"));
12064
+ }
12065
+ return JSON.parse(Buffer.from(data).toString("utf8"));
12066
+ } catch {
12067
+ return null;
11942
12068
  }
11943
- return JSON.parse(Buffer.from(data).toString("utf8"));
11944
12069
  }
11945
12070
  function isWelcomeMessage(payload) {
11946
12071
  if (!isRecord10(payload)) {
@@ -12288,6 +12413,20 @@ function createCliRuntime(dependencies = {}) {
12288
12413
  process.once("SIGINT", () => {
12289
12414
  void shutdown("SIGINT");
12290
12415
  });
12416
+ process.once("uncaughtException", (error) => {
12417
+ output.stderr(
12418
+ `AISnitch crashed: ${error instanceof Error ? error.message : "unknown exception"}
12419
+ `
12420
+ );
12421
+ void shutdown("uncaughtException", 1);
12422
+ });
12423
+ process.once("unhandledRejection", (reason) => {
12424
+ output.stderr(
12425
+ `AISnitch rejected a promise: ${reason instanceof Error ? reason.message : "unknown rejection"}
12426
+ `
12427
+ );
12428
+ void shutdown("unhandledRejection", 1);
12429
+ });
12291
12430
  await renderForegroundTui({
12292
12431
  configuredAdapters: getEnabledAdapters(config),
12293
12432
  eventBus: pipeline.getEventBus(),