First-PR scope from #1. Single-process Python daemon that relays between Claude Code instances and chat-Claude (Anthropic API). Components: * relay.config — .env + config.yaml loader. Auto-generates ntfy topic on first run and persists it back to .env. * relay.state — atomic file I/O via tempfile + rename, advisory flock at state/.lock to enforce single-instance. * relay.conversation — append-only history with summarization. Triggers a summarize call when total chars exceed HISTORY_CHAR_CAP (default 400k); replaces history with the summary plus the most recent 10 turns. * relay.anthropic_client — SDK wrapper. Marks the system prompt cacheable (5-min ephemeral cache); concatenates text blocks; estimates per-call cost from the Anthropic price table with cache-write/read accounted for. * relay.queue — JSON envelope intake; oldest-by-mtime; malformed envelopes moved to queue/.rejected/. * relay.dispatch — one-input-at-a-time per session (dispatch/<session_id>/input.txt). Won't overwrite a pending dispatch; queues internally and waits for CC to delete. * relay.ntfy — best-effort POST to https://ntfy.sh/<topic>; failures logged but never block the main loop. * relay.daemon — main loop. Polls jc_input.txt (priority) then queue/. Detects [NEEDS-JC] in the first 200 chars of any response and pauses dispatch until JC writes jc_input.txt. JC override supports @session-N: prefix for direct dispatch without an API call. * relay.__main__ — CLI: relay run / relay status / relay topic. Tests: 57 unit tests pass (config, state, conversation, queue, dispatch, anthropic_client, ntfy, full daemon loop with a fake client). One real-API smoke test marked real_api, opt-in via pytest -m real_api; skips cleanly on credit-balance errors. Out of scope for this PR (deferred to follow-ups): Flask status endpoint, multi-session config in production, exponential backoff, systemd unit, cost-tracking aggregation. Closes #1. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from relay.queue import ack, list_pending, stuck_age_seconds, take_oldest
|
|
|
|
|
|
def _drop(queue_dir: Path, name: str, payload: dict) -> Path:
|
|
queue_dir.mkdir(parents=True, exist_ok=True)
|
|
path = queue_dir / name
|
|
path.write_text(json.dumps(payload))
|
|
return path
|
|
|
|
|
|
def test_take_oldest_returns_none_on_empty_queue(tmp_path: Path) -> None:
|
|
assert take_oldest(tmp_path / "queue") is None
|
|
|
|
|
|
def test_take_oldest_returns_oldest_by_mtime(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
older = _drop(queue_dir, "a.json", {"session_id": "s1", "timestamp": "t1", "content": "first"})
|
|
time.sleep(0.01)
|
|
newer = _drop(queue_dir, "b.json", {"session_id": "s2", "timestamp": "t2", "content": "second"})
|
|
entry = take_oldest(queue_dir)
|
|
assert entry is not None
|
|
assert entry.path == older
|
|
assert entry.session_id == "s1"
|
|
assert entry.content == "first"
|
|
assert newer.exists()
|
|
|
|
|
|
def test_invalid_json_is_rejected(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
queue_dir.mkdir()
|
|
bad = queue_dir / "bad.json"
|
|
bad.write_text("not json")
|
|
assert take_oldest(queue_dir) is None
|
|
assert not bad.exists()
|
|
assert (queue_dir / ".rejected" / "bad.json").exists()
|
|
|
|
|
|
def test_envelope_missing_keys_is_rejected(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
_drop(queue_dir, "x.json", {"session_id": "s1"}) # no timestamp / content
|
|
assert take_oldest(queue_dir) is None
|
|
assert (queue_dir / ".rejected" / "x.json").exists()
|
|
|
|
|
|
def test_envelope_blank_content_is_rejected(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
_drop(queue_dir, "x.json", {"session_id": "s1", "timestamp": "t", "content": " "})
|
|
assert take_oldest(queue_dir) is None
|
|
|
|
|
|
def test_ack_deletes_file(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
_drop(queue_dir, "a.json", {"session_id": "s1", "timestamp": "t", "content": "x"})
|
|
entry = take_oldest(queue_dir)
|
|
assert entry is not None
|
|
ack(entry)
|
|
assert not entry.path.exists()
|
|
|
|
|
|
def test_list_pending_skips_rejected_dir(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
queue_dir.mkdir()
|
|
rejected = queue_dir / ".rejected"
|
|
rejected.mkdir()
|
|
(rejected / "old.json").write_text("{}")
|
|
a = _drop(queue_dir, "a.json", {"session_id": "s", "timestamp": "t", "content": "x"})
|
|
pending = list_pending(queue_dir)
|
|
assert pending == [a]
|
|
|
|
|
|
def test_stuck_age_returns_zero_on_empty_queue(tmp_path: Path) -> None:
|
|
assert stuck_age_seconds(tmp_path / "queue") == 0
|
|
|
|
|
|
def test_stuck_age_reports_oldest_age(tmp_path: Path) -> None:
|
|
queue_dir = tmp_path / "queue"
|
|
path = _drop(queue_dir, "a.json", {"session_id": "s", "timestamp": "t", "content": "x"})
|
|
# Backdate the file
|
|
old_mtime = time.time() - 60
|
|
import os
|
|
|
|
os.utime(path, (old_mtime, old_mtime))
|
|
age = stuck_age_seconds(queue_dir)
|
|
assert age >= 59
|