omni-notify-mcp 1.3.331 → 1.3.332

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.
Files changed (2) hide show
  1. package/dist/notify.js +69 -49
  2. package/package.json +1 -1
package/dist/notify.js CHANGED
@@ -12,7 +12,7 @@
12
12
  */
13
13
  import { randomUUID, createHash } from "crypto";
14
14
  import { createServer as createNetServer } from "net";
15
- import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync, unlinkSync, renameSync, watch } from "fs";
15
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync, unlinkSync, renameSync, rmSync, watch } from "fs";
16
16
  import { homedir, networkInterfaces, hostname, tmpdir } from "os";
17
17
  import { join, basename, dirname } from "path";
18
18
  import { fileURLToPath } from "url";
@@ -695,48 +695,76 @@ function markDelivered(entry, tag) {
695
695
  else
696
696
  inboxDeliveredTo.set(entryKey(entry), new Set([deliveredKey(tag)]));
697
697
  }
698
- const INBOX_QUEUE_PATH = join(CONFIG_DIR, "inbox-queue.json");
699
- /** the shared queue exactly as this process last wrote it — its own write, seen on the folder's watch, is not another host's news. */
700
- let queueWritten = "";
701
- // GATE: an entry older than this is dropped when the queue file is rewritten — it is a message no host ever collected, and keeping it forever grows the file without bound.
698
+ // The shared queue is every host's own files in one folder. A write NEVER replaces a file another process may be reading — on Windows a rename
699
+ // over a file another process has open is refused (EPERM) — so each write is a NEW file `inbox-queue.<pid>.<stamp>.json` that only its own
700
+ // process ever creates, and the process then removes its own older ones. Every reader merges all the files, so no process can erase what
701
+ // another recorded, and a removed generation holds only what its successor also holds. `inbox-queue.json` is the name the build before wrote.
702
+ const INBOX_QUEUE_FILE = /^inbox-queue(\.\d+\.\d+)?\.json$/;
703
+ const STAGED_QUEUE_FILE = /^inbox-queue\.\d+\.\d+\.json\.tmp$/;
704
+ const OWN_QUEUE_PREFIX = `inbox-queue.${process.pid}.`;
705
+ /** orders this process's writes inside one millisecond. */
706
+ let queueWrites = 0;
707
+ // GATE: an entry older than this is dropped when the queue is read or rewritten, and a queue file nobody wrote for this long is deleted — it is a message no host ever collected, and keeping it forever grows the folder without bound.
702
708
  const INBOX_ENTRY_TTL_MS = 24 * 60 * 60 * 1000;
703
- // The queue file is the SHARED state: every host runs its own process against
704
- // it, so a write reads the file back first and merges, or one host's write
705
- // would silently delete the messages another host's poller just recorded.
709
+ function absorbQueued(into, entry, deliveredTo) {
710
+ const key = entryKey(entry);
711
+ into.set(key, { ...entry, deliveredTo: [...new Set([...(into.get(key)?.deliveredTo ?? []), ...deliveredTo])] });
712
+ }
713
+ /** True when the file's entries are now in `into` or the file is gone (a generation its writer replaced holds nothing its successor lacks); false when it exists and could not be read. */
714
+ function absorbQueueFile(into, name) {
715
+ const path = join(CONFIG_DIR, name);
716
+ try {
717
+ for (const { deliveredTo, ...entry } of JSON.parse(readFileSync(path, "utf8"))) {
718
+ if (Date.now() - Date.parse(entry.ts) < INBOX_ENTRY_TTL_MS)
719
+ absorbQueued(into, entry, deliveredTo);
720
+ }
721
+ return true;
722
+ }
723
+ catch {
724
+ if (!existsSync(path))
725
+ return true;
726
+ log("·", "inbox", `the queue file ${path} could not be read — its entries are skipped and the file is kept`);
727
+ return false;
728
+ }
729
+ }
730
+ function queueFileNames() {
731
+ return existsSync(CONFIG_DIR) ? readdirSync(CONFIG_DIR).filter((f) => INBOX_QUEUE_FILE.test(f)) : [];
732
+ }
733
+ /** Every host's entry still inside its lifetime, the `deliveredTo` marks of all queue files united. */
734
+ function readSharedQueue() {
735
+ const merged = new Map();
736
+ for (const name of queueFileNames())
737
+ absorbQueueFile(merged, name);
738
+ return [...merged.values()];
739
+ }
706
740
  function persistInboxQueue(arrivals = []) {
707
741
  mkdirSync(CONFIG_DIR, { recursive: true });
708
742
  const merged = new Map();
709
- const absorb = (entry, deliveredTo) => {
710
- const key = entryKey(entry);
711
- const prev = merged.get(key);
712
- merged.set(key, { ...entry, deliveredTo: [...new Set([...(prev?.deliveredTo ?? []), ...deliveredTo])] });
713
- };
714
- if (existsSync(INBOX_QUEUE_PATH)) {
715
- try {
716
- for (const { deliveredTo, ...entry } of JSON.parse(readFileSync(INBOX_QUEUE_PATH, "utf8"))) {
717
- if (Date.now() - Date.parse(entry.ts) < INBOX_ENTRY_TTL_MS)
718
- absorb(entry, deliveredTo);
719
- }
720
- }
721
- catch {
722
- log("·", "inbox", `the shared queue at ${INBOX_QUEUE_PATH} could not be read back — this process's own view replaces it`);
723
- }
724
- }
743
+ // an earlier file of this process that could not be read is kept: it may hold what nothing else does
744
+ const earlier = queueFileNames().filter((f) => f.startsWith(OWN_QUEUE_PREFIX)).filter((f) => absorbQueueFile(merged, f));
725
745
  for (const entry of inboxQueue)
726
- absorb(entry, [...(inboxDeliveredTo.get(entryKey(entry)) ?? [])]);
746
+ absorbQueued(merged, entry, [...(inboxDeliveredTo.get(entryKey(entry)) ?? [])]);
727
747
  for (const entry of arrivals)
728
- absorb(entry, []);
729
- const staged = `${INBOX_QUEUE_PATH}.${process.pid}.tmp`;
730
- queueWritten = JSON.stringify([...merged.values()]);
731
- writeFileSync(staged, queueWritten);
732
- renameSync(staged, INBOX_QUEUE_PATH);
748
+ absorbQueued(merged, entry, []);
749
+ const name = `${OWN_QUEUE_PREFIX}${Date.now()}${String(queueWrites++).padStart(4, "0")}.json`;
750
+ writeFileSync(join(CONFIG_DIR, `${name}.tmp`), JSON.stringify([...merged.values()]));
751
+ renameSync(join(CONFIG_DIR, `${name}.tmp`), join(CONFIG_DIR, name));
752
+ for (const old of earlier)
753
+ rmSync(join(CONFIG_DIR, old), { force: true });
754
+ // a file nobody wrote for the entry lifetime holds only entries already past it (an entry is never newer than its file's last write); a staging file a dead process left is the same
755
+ const staged = existsSync(CONFIG_DIR) ? readdirSync(CONFIG_DIR).filter((f) => STAGED_QUEUE_FILE.test(f)) : [];
756
+ for (const other of [...queueFileNames().filter((f) => !f.startsWith(OWN_QUEUE_PREFIX)), ...staged]) {
757
+ const stat = statSync(join(CONFIG_DIR, other), { throwIfNoEntry: false }); // another process may have removed it first
758
+ if (stat !== undefined && Date.now() - stat.mtimeMs >= INBOX_ENTRY_TTL_MS)
759
+ rmSync(join(CONFIG_DIR, other), { force: true });
760
+ }
733
761
  }
734
762
  function restoreInboxQueue() {
735
- if (!existsSync(INBOX_QUEUE_PATH))
763
+ const saved = readSharedQueue();
764
+ if (saved.length === 0)
736
765
  return;
737
- const saved = JSON.parse(readFileSync(INBOX_QUEUE_PATH, "utf8"));
738
766
  const taken = takeSharedEntriesForSelf(saved);
739
- log("·", "inbox", `restored ${taken.length} undelivered inbox entr${taken.length === 1 ? "y" : "ies"} of ${saved.length} in ${INBOX_QUEUE_PATH}`);
767
+ log("·", "inbox", `restored ${taken.length} undelivered inbox entr${taken.length === 1 ? "y" : "ies"} of ${saved.length} in ${CONFIG_DIR}`);
740
768
  }
741
769
  // Entries in the shared queue addressed to THIS host that no process of it has taken yet. Each is
742
770
  // recorded as taken (in memory and in the shared file, so it is never taken twice) and handed over.
@@ -2460,7 +2488,7 @@ let dropWatched = false;
2460
2488
  * platform could not name or a watch that failed. a watch that fails is ARMED AGAIN, never left dead: with the
2461
2489
  * beat gone this watch is the only trigger the writes it covers have, so a folder that is still there must keep one.
2462
2490
  */
2463
- function watchNames(dir, names, onChange) {
2491
+ function watchNames(dir, watched, onChange) {
2464
2492
  const arm = () => {
2465
2493
  if (!existsSync(dir)) {
2466
2494
  onChange(undefined);
@@ -2471,7 +2499,7 @@ function watchNames(dir, names, onChange) {
2471
2499
  // NULL-OK: field name is OPTIONAL and therefore it is ok to check for NULL
2472
2500
  if (name === null)
2473
2501
  onChange(undefined);
2474
- else if (names.includes(String(name)))
2502
+ else if (watched(String(name)))
2475
2503
  onChange(String(name));
2476
2504
  });
2477
2505
  folder.on("error", () => {
@@ -2659,16 +2687,7 @@ async function stepAside(polling, host) {
2659
2687
  // layer and to the attached client, and record it in the shared file so no host
2660
2688
  // delivers the same message twice.
2661
2689
  function syncInboxFromDisk(host) {
2662
- if (!existsSync(INBOX_QUEUE_PATH))
2663
- return;
2664
- let saved;
2665
- try {
2666
- saved = JSON.parse(readFileSync(INBOX_QUEUE_PATH, "utf8"));
2667
- }
2668
- catch {
2669
- return;
2670
- }
2671
- for (const entry of takeSharedEntriesForSelf(saved)) {
2690
+ for (const entry of takeSharedEntriesForSelf(readSharedQueue())) {
2672
2691
  host.emit(entry).catch((err) =>
2673
2692
  // OWNER: the message is already in this host's inbox and in the shared queue, so a refused push costs the push alone — it is reported once here and the caller's next poll or wait_for_inbox still hands the message over.
2674
2693
  log("·", "inbox", `channel push failed: ${err instanceof Error ? err.message : String(err)}`, host.clientId));
@@ -2701,7 +2720,7 @@ export function startNotifyServer(host) {
2701
2720
  if (door === undefined || supersededSaid || yielding)
2702
2721
  tryOwnGateway(polling, host);
2703
2722
  }, OWNERSHIP_RETRY_MS);
2704
- watchNames(dirname(MANIFEST_PATH), [basename(MANIFEST_PATH)], () => tryOwnGateway(polling, host));
2723
+ watchNames(dirname(MANIFEST_PATH), (name) => name === basename(MANIFEST_PATH), () => tryOwnGateway(polling, host));
2705
2724
  // another host's queued message and a dropped file are taken in on the write that brings them, never on a clock.
2706
2725
  mkdirSync(CONFIG_DIR, { recursive: true });
2707
2726
  const watchDrops = () => {
@@ -2712,12 +2731,13 @@ export function startNotifyServer(host) {
2712
2731
  // The drop folder was taken away: its watch is gone, and the folder's return is seen on the config folder's own watch.
2713
2732
  drops.on("error", () => { dropWatched = false; });
2714
2733
  };
2715
- watchNames(CONFIG_DIR, [basename(INBOX_QUEUE_PATH), basename(INBOX_DROP_IN_DIR)], (name) => {
2716
- if (name !== basename(INBOX_QUEUE_PATH)) {
2734
+ // this process's own queue file is its own write, never another host's news; a name that is neither a queue file nor the drop folder never reaches here.
2735
+ watchNames(CONFIG_DIR, (name) => INBOX_QUEUE_FILE.test(name) || name === basename(INBOX_DROP_IN_DIR), (name) => {
2736
+ if (name === undefined || name === basename(INBOX_DROP_IN_DIR)) {
2717
2737
  watchDrops();
2718
2738
  ingestDropFolder(host);
2719
2739
  }
2720
- if (name !== basename(INBOX_DROP_IN_DIR) && !(existsSync(INBOX_QUEUE_PATH) && readFileSync(INBOX_QUEUE_PATH, "utf8") === queueWritten))
2740
+ if (name !== basename(INBOX_DROP_IN_DIR) && !name?.startsWith(OWN_QUEUE_PREFIX))
2721
2741
  syncInboxFromDisk(host);
2722
2742
  });
2723
2743
  watchDrops();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "omni-notify-mcp",
3
- "version": "1.3.331",
3
+ "version": "1.3.332",
4
4
  "description": "Reach your AI agents from anywhere — and let them reach you. One stdio process serves the whole tool surface to Claude Code, GitHub Copilot Chat and Codex: desktop, Telegram, Slack, ntfy, Discord, Teams and SMS are configured in one file, and agents notify you when long work finishes or a decision is needed, ask you a question and wait for your answer, and receive and reply to the messages you send them — with Do Not Disturb and idle detection deciding what actually gets through.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",