netresearch/matrix-skill / skills/matrix-communication
matrix-communication
FlaggedUse when communicating via Matrix chat, notifying teams, or managing E2EE. Triggers on #room:server references, Matrix URLs, and chat requests.
scripts/_lib/daemon_client.py
View on GitHub# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Netresearch DTT GmbH
"""Client side of the watch daemon's socket.
Stdlib only.
`daemon_request` returning None is the signal to fall back to the direct path:
it means nothing is serving the socket. That is a different question from
whether the store lock is held, and only this one may decide the routing - a
short-lived direct send holds the lock too, so a command starting in that window
would conclude "daemon is running" and talk to a socket nobody answers.
An error *response* is not None. It is an answer, and returning None for it
would send the caller down the direct path as well, delivering the message
twice.
"""
import json
import os
import socket
from pathlib import Path
CONNECT_TIMEOUT = 2.0
def socket_path() -> Path:
"""Where the daemon listens.
The runtime directory when it is usable: it is cleared on reboot, which is
exactly the lifetime a socket should have. `XDG_RUNTIME_DIR` being set is
not the same as it existing - on WSL and in containers it is routinely
exported for a `/run/user/<uid>` nobody created - so the directory is
tested rather than trusted, and the fall-back is the data directory the
rest of the skill already uses.
"""
runtime = os.environ.get("XDG_RUNTIME_DIR")
if runtime and os.path.isdir(runtime) and os.access(runtime, os.W_OK):
base = Path(runtime)
else:
base = Path.home() / ".local" / "share"
return base / "matrix-skill" / "daemon.sock"
def daemon_request(payload: dict, timeout: float = 30.0):
"""One request, one response. None when no daemon is listening."""
path = socket_path()
if not path.exists():
return None
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(CONNECT_TIMEOUT)
try:
sock.connect(str(path))
except OSError:
# Stale socket from a crashed daemon, or none at all. Either way there
# is nobody to delegate to.
return None
try:
sock.settimeout(timeout)
sock.sendall(json.dumps(payload).encode("utf-8") + b"\n")
buffer = b""
while not buffer.endswith(b"\n"):
chunk = sock.recv(4096)
if not chunk:
break
buffer += chunk
if not buffer.strip():
return None
return json.loads(buffer)
except (OSError, ValueError):
return None
finally:
sock.close()
def running_daemon_pid(status) -> int | None:
"""The pid of the daemon that answered a status request, or None.
`matrix-watchd.py --stop` signals this pid rather than the one in the pid
file: a pid file outlives a crashed daemon, and its number may by then
belong to an unrelated process. Only a daemon that answers on the socket,
which sits in an owner-only directory, is stopped.
"""
if not status or not status.get("ok"):
return None
pid = status.get("pid")
if isinstance(pid, int) and not isinstance(pid, bool) and pid > 0:
return pid
return None
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |