@hasna/todos 0.15.46 → 0.15.47

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/index.js CHANGED
@@ -6194,8 +6194,9 @@ var init_event_hooks = __esm(() => {
6194
6194
  VALID_TARGETS = new Set(["stdout", "file", "socket", "script"]);
6195
6195
  });
6196
6196
 
6197
- // node_modules/.bun/@hasna+events@0.1.11/node_modules/@hasna/events/dist/index.js
6197
+ // node_modules/.bun/@hasna+events@0.1.16/node_modules/@hasna/events/dist/index.js
6198
6198
  import { chmod, mkdir, readFile, rename, writeFile } from "fs/promises";
6199
+ import { Buffer as Buffer2 } from "buffer";
6199
6200
  import { existsSync as existsSync5 } from "fs";
6200
6201
  import { homedir as homedir2 } from "os";
6201
6202
  import { join as join4 } from "path";
@@ -6304,11 +6305,13 @@ function getEventsDataDir(override) {
6304
6305
 
6305
6306
  class JsonEventsStore {
6306
6307
  dataDir;
6308
+ runtime;
6307
6309
  channelsPath;
6308
6310
  eventsPath;
6309
6311
  deliveriesPath;
6310
6312
  constructor(dataDir = getEventsDataDir()) {
6311
6313
  this.dataDir = dataDir;
6314
+ this.runtime = localJsonRuntime(dataDir);
6312
6315
  this.channelsPath = join4(dataDir, "channels.json");
6313
6316
  this.eventsPath = join4(dataDir, "events.json");
6314
6317
  this.deliveriesPath = join4(dataDir, "deliveries.json");
@@ -6356,13 +6359,58 @@ class JsonEventsStore {
6356
6359
  await this.writeJson(this.eventsPath, events);
6357
6360
  return event;
6358
6361
  }
6359
- async listEvents() {
6362
+ async appendEventOnce(event, options = {}) {
6360
6363
  await this.init();
6361
- return this.readJson(this.eventsPath, []);
6364
+ const events = await this.readJson(this.eventsPath, []);
6365
+ const dedupe = options.dedupe !== false;
6366
+ if (dedupe) {
6367
+ const existing = findEventByIdentity(events, { id: event.id, dedupeKey: event.dedupeKey });
6368
+ if (existing) {
6369
+ return {
6370
+ event: existing,
6371
+ stored: false,
6372
+ deduped: true,
6373
+ identity: { id: existing.id, dedupeKey: existing.dedupeKey }
6374
+ };
6375
+ }
6376
+ }
6377
+ events.push(event);
6378
+ await this.writeJson(this.eventsPath, events);
6379
+ return {
6380
+ event,
6381
+ stored: true,
6382
+ deduped: false,
6383
+ identity: { id: event.id, dedupeKey: event.dedupeKey }
6384
+ };
6385
+ }
6386
+ async listEvents(options = {}) {
6387
+ await this.init();
6388
+ const events = await this.readJson(this.eventsPath, []);
6389
+ return queryEvents(events, options);
6390
+ }
6391
+ async listEventsPage(options = {}) {
6392
+ await this.init();
6393
+ const events = await this.readJson(this.eventsPath, []);
6394
+ const queried = queryEvents(events, {
6395
+ eventId: options.eventId,
6396
+ source: options.source,
6397
+ type: options.type
6398
+ });
6399
+ const offset = decodeLocalJsonEventCursor(options.cursor, options);
6400
+ const limit = normalizeEventPageLimit(options.limit);
6401
+ const pageEvents = queried.slice(offset, offset + limit);
6402
+ const nextOffset = offset + pageEvents.length;
6403
+ const hasMore = nextOffset < queried.length;
6404
+ return {
6405
+ events: pageEvents,
6406
+ cursor: options.cursor,
6407
+ nextCursor: hasMore ? encodeLocalJsonEventCursor(nextOffset, options) : undefined,
6408
+ hasMore
6409
+ };
6362
6410
  }
6363
6411
  async findEventByIdentity(identity) {
6364
6412
  const events = await this.listEvents();
6365
- return events.find((event) => identity.id !== undefined && event.id === identity.id || identity.dedupeKey !== undefined && event.dedupeKey === identity.dedupeKey);
6413
+ return findEventByIdentity(events, identity);
6366
6414
  }
6367
6415
  async appendDelivery(result) {
6368
6416
  await this.init();
@@ -6413,6 +6461,83 @@ class JsonEventsStore {
6413
6461
  });
6414
6462
  }
6415
6463
  }
6464
+ function localJsonRuntime(dataDir = getEventsDataDir()) {
6465
+ return {
6466
+ mode: "local-files",
6467
+ name: "json-events-store",
6468
+ remote: false,
6469
+ localFiles: true,
6470
+ localSqlite: false,
6471
+ postgres: false,
6472
+ s3: false,
6473
+ aws: false,
6474
+ durable: true,
6475
+ idempotency: "best-effort-local",
6476
+ replayCursors: true,
6477
+ description: `Local JSON files in ${dataDir}; no SQLite, Postgres, S3, or AWS runtime is configured by this store.`
6478
+ };
6479
+ }
6480
+ function encodeLocalJsonEventCursor(offset, options = {}) {
6481
+ if (!Number.isInteger(offset) || offset < 0)
6482
+ throw new Error(`Invalid event cursor offset: ${offset}`);
6483
+ const payload = {
6484
+ offset,
6485
+ eventId: options.eventId,
6486
+ source: options.source,
6487
+ type: options.type
6488
+ };
6489
+ return `${LOCAL_JSON_EVENT_CURSOR_PREFIX}${Buffer2.from(JSON.stringify(payload), "utf-8").toString("base64url")}`;
6490
+ }
6491
+ function decodeLocalJsonEventCursor(cursor, options = {}) {
6492
+ if (!cursor)
6493
+ return 0;
6494
+ if (!cursor.startsWith(LOCAL_JSON_EVENT_CURSOR_PREFIX))
6495
+ throw new Error(`Invalid local JSON event cursor: ${cursor}`);
6496
+ const rawPayload = cursor.slice(LOCAL_JSON_EVENT_CURSOR_PREFIX.length);
6497
+ let payload;
6498
+ try {
6499
+ payload = JSON.parse(Buffer2.from(rawPayload, "base64url").toString("utf-8"));
6500
+ } catch {
6501
+ throw new Error(`Invalid local JSON event cursor: ${cursor}`);
6502
+ }
6503
+ const offset = payload.offset;
6504
+ if (!Number.isInteger(offset) || offset < 0)
6505
+ throw new Error(`Invalid local JSON event cursor: ${cursor}`);
6506
+ assertCursorFilter("eventId", payload.eventId, options.eventId);
6507
+ assertCursorFilter("source", payload.source, options.source);
6508
+ assertCursorFilter("type", payload.type, options.type);
6509
+ return offset;
6510
+ }
6511
+ function normalizeEventPageLimit(limit) {
6512
+ if (limit === undefined)
6513
+ return DEFAULT_EVENT_PAGE_LIMIT;
6514
+ if (!Number.isInteger(limit) || limit < 1)
6515
+ throw new Error(`Event page limit must be a positive integer, got ${limit}`);
6516
+ return Math.min(limit, MAX_EVENT_PAGE_LIMIT);
6517
+ }
6518
+ function queryEvents(events, options) {
6519
+ let rows = events;
6520
+ if (options.eventId)
6521
+ rows = rows.filter((event) => event.id === options.eventId);
6522
+ if (options.source)
6523
+ rows = rows.filter((event) => event.source === options.source);
6524
+ if (options.type)
6525
+ rows = rows.filter((event) => event.type === options.type);
6526
+ if (options.cursor) {
6527
+ const offset = decodeLocalJsonEventCursor(options.cursor, options);
6528
+ rows = rows.slice(offset);
6529
+ }
6530
+ if (options.limit !== undefined)
6531
+ rows = rows.slice(0, normalizeEventPageLimit(options.limit));
6532
+ return rows;
6533
+ }
6534
+ function assertCursorFilter(name, cursorValue, optionValue) {
6535
+ if (cursorValue !== optionValue)
6536
+ throw new Error(`Local JSON event cursor ${name} filter mismatch`);
6537
+ }
6538
+ function findEventByIdentity(events, identity) {
6539
+ return events.find((event) => identity.id !== undefined && event.id === identity.id || identity.dedupeKey !== undefined && event.dedupeKey === identity.dedupeKey);
6540
+ }
6416
6541
  function buildSignatureBase(timestamp2, body) {
6417
6542
  return `${timestamp2}.${body}`;
6418
6543
  }
@@ -6426,21 +6551,27 @@ function now2() {
6426
6551
  function truncate(value, max = 4096) {
6427
6552
  return value.length > max ? `${value.slice(0, max)}...` : value;
6428
6553
  }
6429
- function buildWebhookRequest(event, channel) {
6554
+ function buildWebhookRequest(event, channel, options = {}) {
6430
6555
  if (!channel.webhook)
6431
6556
  throw new Error(`Channel ${channel.id} has no webhook config`);
6557
+ for (const name of Object.keys(channel.webhook.headers ?? {})) {
6558
+ if (/^x-hasna-/i.test(name)) {
6559
+ throw new Error(`Webhook header ${name} is reserved for signed delivery metadata`);
6560
+ }
6561
+ }
6432
6562
  const body = JSON.stringify(event);
6433
- const timestamp2 = event.time;
6563
+ const timestamp2 = options.timestamp ?? new Date().toISOString();
6434
6564
  const headers = {
6435
6565
  "Content-Type": "application/json",
6436
6566
  "User-Agent": "@hasna/events",
6437
6567
  "X-Hasna-Event-Id": event.id,
6438
6568
  "X-Hasna-Event-Type": event.type,
6439
- "X-Hasna-Timestamp": timestamp2,
6440
- ...channel.webhook.headers
6569
+ ...channel.webhook.headers,
6570
+ "X-Hasna-Timestamp": timestamp2
6441
6571
  };
6442
- if (channel.webhook.secret) {
6443
- headers["X-Hasna-Signature"] = signPayload(channel.webhook.secret, timestamp2, body);
6572
+ const secret = options.secret ?? channel.webhook.secret;
6573
+ if (secret) {
6574
+ headers["X-Hasna-Signature"] = signPayload(secret, timestamp2, body);
6444
6575
  }
6445
6576
  return { body, headers };
6446
6577
  }
@@ -6448,7 +6579,21 @@ async function dispatchWebhook(event, channel, options = {}) {
6448
6579
  if (!channel.webhook)
6449
6580
  throw new Error(`Channel ${channel.id} has no webhook config`);
6450
6581
  const startedAt = now2();
6451
- const { body, headers } = buildWebhookRequest(event, channel);
6582
+ let secret = channel.webhook.secret;
6583
+ if (channel.webhook.secretRef) {
6584
+ if (!options.secretResolver) {
6585
+ return failedAttempt(startedAt, "Webhook secret reference has no runtime resolver");
6586
+ }
6587
+ try {
6588
+ secret = await options.secretResolver(channel.webhook.secretRef);
6589
+ } catch {
6590
+ return failedAttempt(startedAt, "Webhook secret reference could not be resolved");
6591
+ }
6592
+ if (!secret)
6593
+ return failedAttempt(startedAt, "Webhook secret reference could not be resolved");
6594
+ }
6595
+ const timestamp2 = (options.now?.() ?? new Date).toISOString();
6596
+ const { body, headers } = buildWebhookRequest(event, channel, { secret, timestamp: timestamp2 });
6452
6597
  const controller = new AbortController;
6453
6598
  const timeout = setTimeout(() => controller.abort(), channel.webhook.timeoutMs ?? 15000);
6454
6599
  try {
@@ -6480,6 +6625,15 @@ async function dispatchWebhook(event, channel, options = {}) {
6480
6625
  clearTimeout(timeout);
6481
6626
  }
6482
6627
  }
6628
+ function failedAttempt(startedAt, error) {
6629
+ return {
6630
+ attempt: 1,
6631
+ status: "failed",
6632
+ startedAt,
6633
+ completedAt: now2(),
6634
+ error
6635
+ };
6636
+ }
6483
6637
  async function dispatchCommand(event, channel) {
6484
6638
  if (!channel.command)
6485
6639
  throw new Error(`Channel ${channel.id} has no command config`);
@@ -6568,6 +6722,76 @@ function createDeliveryResult(event, channel, attempts) {
6568
6722
  completedAt: attempts.at(-1)?.completedAt ?? now2()
6569
6723
  };
6570
6724
  }
6725
+
6726
+ class EventTypeCatalog {
6727
+ definitions = new Map;
6728
+ register(definition) {
6729
+ this.definitions.set(definition.type, definition);
6730
+ return this;
6731
+ }
6732
+ unregister(type) {
6733
+ return this.definitions.delete(type);
6734
+ }
6735
+ has(type) {
6736
+ return this.definitions.has(type);
6737
+ }
6738
+ get(type) {
6739
+ return this.definitions.get(type);
6740
+ }
6741
+ list() {
6742
+ return [...this.definitions.values()];
6743
+ }
6744
+ validateEvent(event) {
6745
+ const definition = this.definitions.get(event.type);
6746
+ if (!definition)
6747
+ return { ok: true };
6748
+ return definition.validate(event.data, event);
6749
+ }
6750
+ assertEventValid(event) {
6751
+ const result = this.validateEvent(event);
6752
+ if (!result.ok) {
6753
+ throw new EventValidationError(event.type, result.issues);
6754
+ }
6755
+ }
6756
+ }
6757
+ function redactPaths(event, paths, replacement = "[REDACTED]") {
6758
+ if (paths.length === 0)
6759
+ return event;
6760
+ const copy = structuredClone(event);
6761
+ for (const path of paths) {
6762
+ setPath(copy, path, replacement);
6763
+ }
6764
+ return copy;
6765
+ }
6766
+ function redactSensitiveKeys(event, replacement = "[REDACTED]") {
6767
+ return redactValue2(event, replacement);
6768
+ }
6769
+ function shouldRedactKey(key) {
6770
+ return /secret|token|password|api[_-]?key|authorization/i.test(key);
6771
+ }
6772
+ function redactValue2(value, replacement) {
6773
+ if (Array.isArray(value))
6774
+ return value.map((item) => redactValue2(item, replacement));
6775
+ if (!value || typeof value !== "object")
6776
+ return value;
6777
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
6778
+ key,
6779
+ shouldRedactKey(key) ? replacement : redactValue2(item, replacement)
6780
+ ]));
6781
+ }
6782
+ function setPath(input, path, replacement) {
6783
+ const parts = path.split(".");
6784
+ let cursor = input;
6785
+ for (const part of parts.slice(0, -1)) {
6786
+ const next = cursor[part];
6787
+ if (!next || typeof next !== "object")
6788
+ return;
6789
+ cursor = next;
6790
+ }
6791
+ const last = parts.at(-1);
6792
+ if (last && last in cursor)
6793
+ cursor[last] = replacement;
6794
+ }
6571
6795
  function createEvent(input) {
6572
6796
  return {
6573
6797
  id: input.id ?? randomUUID22(),
@@ -6588,10 +6812,18 @@ class EventsClient {
6588
6812
  store;
6589
6813
  redactors;
6590
6814
  transportOptions;
6815
+ catalog;
6816
+ validateCatalogTypes;
6591
6817
  constructor(options = {}) {
6592
6818
  this.store = options.store ?? new JsonEventsStore(options.dataDir);
6593
6819
  this.redactors = options.redactors ?? [];
6594
- this.transportOptions = { fetchImpl: options.fetchImpl };
6820
+ this.transportOptions = {
6821
+ fetchImpl: options.fetchImpl,
6822
+ secretResolver: options.secretResolver,
6823
+ now: options.now
6824
+ };
6825
+ this.catalog = options.catalog ?? defaultEventTypeCatalog;
6826
+ this.validateCatalogTypes = options.validateCatalogTypes ?? false;
6595
6827
  }
6596
6828
  async addChannel(input) {
6597
6829
  const timestamp2 = new Date().toISOString();
@@ -6609,18 +6841,40 @@ class EventsClient {
6609
6841
  }
6610
6842
  async emit(input, options = {}) {
6611
6843
  const event = options.redactSensitiveData === false ? createEvent(input) : redactSensitiveKeys(createEvent(input));
6612
- if (options.dedupe !== false) {
6613
- const existing = await this.store.findEventByIdentity({ id: input.id, dedupeKey: event.dedupeKey });
6614
- if (existing) {
6615
- return { event: existing, deliveries: [], deduped: true };
6616
- }
6617
- }
6618
- await this.store.appendEvent(event);
6619
- const deliveries = options.deliver === false ? [] : await this.deliver(event);
6620
- return { event, deliveries, deduped: false };
6621
- }
6622
- async listEvents() {
6623
- return this.store.listEvents();
6844
+ if (options.validate ?? this.validateCatalogTypes) {
6845
+ this.catalog.assertEventValid(event);
6846
+ }
6847
+ const append = await this.appendEvent(event, { dedupe: options.dedupe !== false });
6848
+ if (append.deduped) {
6849
+ return { event: append.event, deliveries: [], deduped: true };
6850
+ }
6851
+ const deliveries = options.deliver === false ? [] : await this.deliver(append.event);
6852
+ return { event: append.event, deliveries, deduped: false };
6853
+ }
6854
+ async listEvents(options = {}) {
6855
+ if (Object.keys(options).length === 0)
6856
+ return this.store.listEvents();
6857
+ return queryClientEvents(await this.store.listEvents(), options);
6858
+ }
6859
+ async listEventsPage(options = {}) {
6860
+ if (this.store.listEventsPage)
6861
+ return this.store.listEventsPage(options);
6862
+ const events = queryClientEvents(await this.store.listEvents(), {
6863
+ eventId: options.eventId,
6864
+ source: options.source,
6865
+ type: options.type
6866
+ });
6867
+ const offset = decodeLocalJsonEventCursor(options.cursor, options);
6868
+ const limit = normalizeEventPageLimit(options.limit);
6869
+ const pageEvents = events.slice(offset, offset + limit);
6870
+ const nextOffset = offset + pageEvents.length;
6871
+ const hasMore = nextOffset < events.length;
6872
+ return {
6873
+ events: pageEvents,
6874
+ cursor: options.cursor,
6875
+ nextCursor: hasMore ? encodeLocalJsonEventCursor(nextOffset, options) : undefined,
6876
+ hasMore
6877
+ };
6624
6878
  }
6625
6879
  async listDeliveries() {
6626
6880
  return this.store.listDeliveries();
@@ -6688,22 +6942,37 @@ class EventsClient {
6688
6942
  return result;
6689
6943
  }
6690
6944
  async replay(options = {}) {
6691
- const events = (await this.store.listEvents()).filter((event) => {
6692
- if (options.eventId && event.id !== options.eventId)
6693
- return false;
6694
- if (options.source && event.source !== options.source)
6695
- return false;
6696
- if (options.type && event.type !== options.type)
6697
- return false;
6698
- return true;
6699
- });
6945
+ const page = options.cursor || options.limit !== undefined ? await this.listEventsPage(options) : { events: await this.listEvents(options), hasMore: false };
6700
6946
  if (options.dryRun)
6701
- return { events, deliveries: [] };
6947
+ return { events: page.events, deliveries: [], cursor: page.cursor, nextCursor: page.nextCursor, hasMore: page.hasMore };
6702
6948
  const deliveries = [];
6703
- for (const event of events) {
6949
+ for (const event of page.events) {
6704
6950
  deliveries.push(...await this.deliver(event));
6705
6951
  }
6706
- return { events, deliveries };
6952
+ return { events: page.events, deliveries, cursor: page.cursor, nextCursor: page.nextCursor, hasMore: page.hasMore };
6953
+ }
6954
+ async appendEvent(event, options) {
6955
+ if (this.store.appendEventOnce) {
6956
+ return this.store.appendEventOnce(event, { dedupe: options.dedupe });
6957
+ }
6958
+ if (options.dedupe) {
6959
+ const existing = await this.store.findEventByIdentity({ id: event.id, dedupeKey: event.dedupeKey });
6960
+ if (existing) {
6961
+ return {
6962
+ event: existing,
6963
+ stored: false,
6964
+ deduped: true,
6965
+ identity: { id: existing.id, dedupeKey: existing.dedupeKey }
6966
+ };
6967
+ }
6968
+ }
6969
+ const stored = await this.store.appendEvent(event);
6970
+ return {
6971
+ event: stored,
6972
+ stored: true,
6973
+ deduped: false,
6974
+ identity: { id: stored.id, dedupeKey: stored.dedupeKey }
6975
+ };
6707
6976
  }
6708
6977
  async applyRedaction(event, channel) {
6709
6978
  let next = redactPaths(event, channel.redact?.paths ?? [], channel.redact?.replacement ?? "[REDACTED]");
@@ -6730,43 +6999,19 @@ class EventsClient {
6730
6999
  return createDeliveryResult(event, channel, attempts);
6731
7000
  }
6732
7001
  }
6733
- function redactPaths(event, paths, replacement = "[REDACTED]") {
6734
- if (paths.length === 0)
6735
- return event;
6736
- const copy = structuredClone(event);
6737
- for (const path of paths) {
6738
- setPath(copy, path, replacement);
6739
- }
6740
- return copy;
6741
- }
6742
- function redactSensitiveKeys(event, replacement = "[REDACTED]") {
6743
- return redactValue2(event, replacement);
6744
- }
6745
- function shouldRedactKey(key) {
6746
- return /secret|token|password|api[_-]?key|authorization/i.test(key);
6747
- }
6748
- function redactValue2(value, replacement) {
6749
- if (Array.isArray(value))
6750
- return value.map((item) => redactValue2(item, replacement));
6751
- if (!value || typeof value !== "object")
6752
- return value;
6753
- return Object.fromEntries(Object.entries(value).map(([key, item]) => [
6754
- key,
6755
- shouldRedactKey(key) ? replacement : redactValue2(item, replacement)
6756
- ]));
6757
- }
6758
- function setPath(input, path, replacement) {
6759
- const parts = path.split(".");
6760
- let cursor = input;
6761
- for (const part of parts.slice(0, -1)) {
6762
- const next = cursor[part];
6763
- if (!next || typeof next !== "object")
6764
- return;
6765
- cursor = next;
6766
- }
6767
- const last = parts.at(-1);
6768
- if (last && last in cursor)
6769
- cursor[last] = replacement;
7002
+ function queryClientEvents(events, options) {
7003
+ let rows = events;
7004
+ if (options.eventId)
7005
+ rows = rows.filter((event) => event.id === options.eventId);
7006
+ if (options.source)
7007
+ rows = rows.filter((event) => event.source === options.source);
7008
+ if (options.type)
7009
+ rows = rows.filter((event) => event.type === options.type);
7010
+ if (options.cursor)
7011
+ rows = rows.slice(decodeLocalJsonEventCursor(options.cursor, options));
7012
+ if (options.limit !== undefined)
7013
+ rows = rows.slice(0, normalizeEventPageLimit(options.limit));
7014
+ return rows;
6770
7015
  }
6771
7016
  function normalizeTime(value) {
6772
7017
  if (!value)
@@ -6780,9 +7025,22 @@ function normalizeRetryPolicy(policy) {
6780
7025
  multiplier: Math.max(1, policy?.multiplier ?? 2)
6781
7026
  };
6782
7027
  }
6783
- var HASNA_EVENTS_DIR_ENV = "HASNA_EVENTS_DIR", HASNA_EVENTS_HOME_ENV = "HASNA_EVENTS_HOME", DEFAULT_SIGNATURE_TOLERANCE_MS;
7028
+ var HASNA_EVENTS_DIR_ENV = "HASNA_EVENTS_DIR", HASNA_EVENTS_HOME_ENV = "HASNA_EVENTS_HOME", LOCAL_JSON_EVENT_CURSOR_PREFIX = "local-json-v1:", DEFAULT_EVENT_PAGE_LIMIT = 100, MAX_EVENT_PAGE_LIMIT = 1000, DEFAULT_SIGNATURE_TOLERANCE_MS, EventValidationError, defaultEventTypeCatalog, APP_EVENT_V1_MAX_DATA_BYTES;
6784
7029
  var init_dist = __esm(() => {
6785
7030
  DEFAULT_SIGNATURE_TOLERANCE_MS = 5 * 60 * 1000;
7031
+ EventValidationError = class EventValidationError extends Error {
7032
+ eventType;
7033
+ issues;
7034
+ constructor(eventType, issues) {
7035
+ const detail = issues.map((issue) => `${issue.path || "<root>"}: ${issue.message}`).join("; ");
7036
+ super(`Event validation failed for type "${eventType}": ${detail}`);
7037
+ this.name = "EventValidationError";
7038
+ this.eventType = eventType;
7039
+ this.issues = issues;
7040
+ }
7041
+ };
7042
+ defaultEventTypeCatalog = new EventTypeCatalog;
7043
+ APP_EVENT_V1_MAX_DATA_BYTES = 32 * 1024;
6786
7044
  });
6787
7045
 
6788
7046
  // src/db/task-lists.ts
@@ -7886,8 +8144,12 @@ async function deliverWebhook(wh, event, body, attempt, db) {
7886
8144
  const sig = await crypto.subtle.sign("HMAC", key, encoder.encode(body));
7887
8145
  headers["X-Webhook-Signature"] = Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
7888
8146
  }
7889
- const resp = await fetch(wh.url, { method: "POST", headers, body });
8147
+ const resp = await fetch(wh.url, { method: "POST", headers, body, redirect: "manual" });
7890
8148
  const respText = await resp.text().catch(() => "");
8149
+ if (resp.status >= 300 && resp.status < 400) {
8150
+ logDelivery(db, wh.id, event, body, resp.status, "Blocked: webhook URL returned a 3xx redirect; redirects are never followed (SSRF prevention)", attempt);
8151
+ return;
8152
+ }
7891
8153
  logDelivery(db, wh.id, event, body, resp.status, respText.slice(0, 1000), attempt);
7892
8154
  if (resp.status >= 400 && attempt < MAX_RETRY_ATTEMPTS) {
7893
8155
  const delay = RETRY_BASE_DELAY_MS * Math.pow(2, attempt - 1);
@@ -12922,7 +13184,7 @@ var init_dispatches = __esm(() => {
12922
13184
  // package.json
12923
13185
  var package_default = {
12924
13186
  name: "@hasna/todos",
12925
- version: "0.15.46",
13187
+ version: "0.15.47",
12926
13188
  description: "Universal task management for AI coding agents - CLI + MCP server + interactive TUI",
12927
13189
  type: "module",
12928
13190
  main: "dist/index.js",
@@ -13047,11 +13309,12 @@ var package_default = {
13047
13309
  commander: "^13.1.0",
13048
13310
  ink: "^5.2.0",
13049
13311
  react: "^18.3.1",
13050
- zod: "^3.24.2"
13312
+ zod: "3.25.76"
13051
13313
  },
13052
13314
  overrides: {
13053
13315
  ajv: "8.20.0",
13054
- "fast-uri": "3.1.2"
13316
+ "fast-uri": "3.1.2",
13317
+ zod: "3.25.76"
13055
13318
  },
13056
13319
  devDependencies: {
13057
13320
  "@types/bun": "^1.2.4",
@@ -27983,9 +28246,11 @@ class PostgresJsonRecordStore {
27983
28246
  async ensureSchema() {
27984
28247
  if (!this.schemaReady) {
27985
28248
  this.schemaReady = (async () => {
27986
- for (const sql of postgresTodosSyncSchemaSql(this.tableName, this.cursorTableName)) {
27987
- await this.options.client.query(sql);
27988
- }
28249
+ await retryOnTransientPostgresError(async () => {
28250
+ for (const sql of postgresTodosSyncSchemaSql(this.tableName, this.cursorTableName)) {
28251
+ await this.options.client.query(sql);
28252
+ }
28253
+ });
27989
28254
  })().catch((error) => {
27990
28255
  this.schemaReady = null;
27991
28256
  throw error;
@@ -28399,13 +28664,14 @@ class PostgresJsonRecordStore {
28399
28664
  throw new Error(divergentAuditHistoryReplayError(value.id));
28400
28665
  }
28401
28666
  async withTaskParentIntegrityTransaction(fn) {
28402
- if (typeof this.options.client.transaction !== "function") {
28667
+ const transaction = this.options.client.transaction;
28668
+ if (typeof transaction !== "function") {
28403
28669
  throw new Error("TASK_PARENT_ATOMICITY_UNAVAILABLE: PostgreSQL parent writes and task deletion require transaction(callback)");
28404
28670
  }
28405
- return this.options.client.transaction(async (client) => {
28671
+ return retryOnTransientPostgresError(() => transaction(async (client) => {
28406
28672
  await client.query("/* todos:task-parent-integrity-lock */ SELECT pg_advisory_xact_lock(hashtextextended($1 || ':task-parent-integrity', 0))", [this.service]);
28407
28673
  return fn(client);
28408
- });
28674
+ }));
28409
28675
  }
28410
28676
  async upsertTaskWithPlanMembershipGuard(value, guardedPlanIds, explicitProject, context = {}, parentGuard, queryClient) {
28411
28677
  const planIds = [...new Set(guardedPlanIds.filter(Boolean))].sort();
@@ -30291,6 +30557,41 @@ function compareClock(left, right) {
30291
30557
  function numberValue3(value) {
30292
30558
  return typeof value === "number" && Number.isSafeInteger(value) ? value : null;
30293
30559
  }
30560
+ var TRANSIENT_POSTGRES_SQLSTATES = new Set(["55P03", "40P01", "40001"]);
30561
+ var TRANSIENT_POSTGRES_MESSAGE_MARKERS = [
30562
+ "canceling statement due to lock timeout",
30563
+ "deadlock detected",
30564
+ "could not serialize access"
30565
+ ];
30566
+ function isTransientPostgresError(error) {
30567
+ if (typeof error !== "object" || error === null)
30568
+ return false;
30569
+ const candidate = error;
30570
+ const states = [candidate.code, candidate.errno, candidate.sqlState, candidate.sqlstate];
30571
+ if (typeof candidate.cause === "object" && candidate.cause !== null) {
30572
+ const cause = candidate.cause;
30573
+ states.push(cause.code, cause.errno, cause.sqlState, cause.sqlstate);
30574
+ }
30575
+ if (states.some((state) => typeof state === "string" && TRANSIENT_POSTGRES_SQLSTATES.has(state))) {
30576
+ return true;
30577
+ }
30578
+ const message = typeof candidate.message === "string" ? candidate.message : candidate.cause?.message;
30579
+ return typeof message === "string" && TRANSIENT_POSTGRES_MESSAGE_MARKERS.some((marker) => message.includes(marker));
30580
+ }
30581
+ async function retryOnTransientPostgresError(fn, attempts = 2, delayMs = 150) {
30582
+ let lastError;
30583
+ for (let attempt = 1;attempt <= attempts; attempt += 1) {
30584
+ try {
30585
+ return await fn();
30586
+ } catch (error) {
30587
+ lastError = error;
30588
+ if (!isTransientPostgresError(error) || attempt === attempts)
30589
+ throw error;
30590
+ await new Promise((resolve10) => setTimeout(resolve10, delayMs * attempt));
30591
+ }
30592
+ }
30593
+ throw lastError;
30594
+ }
30294
30595
  function isPostgresUniqueViolation(error) {
30295
30596
  if (typeof error !== "object" || error === null)
30296
30597
  return false;