A UserPromptSubmit hook the sidecar installs in every account's settings.json: the host asks the hub, an owner's worker its outbox, a guest's computer gets none. On a prompt about past conversations it says where they live and how to read them on this machine; each conversation the prompt names (id, prefix after conv, viewer link) comes with its card. Silent elsewhere and in a guest's run; never blocks a prompt. Why: on the workers 3 of 15 'check past conv' prompts reached the hub in Nov 2026 (the rest grepped local transcripts or ssh-found an 01a1… id). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
"""conv_ref_hook: the conv-ref prompt hook in every account's settings.json —
|
|
the host's asks the hub, an owner worker's its outbox, a guest's computer
|
|
none; rewritten when the command changes, never next to a second copy."""
|
|
|
|
import json
|
|
import os
|
|
import pathlib
|
|
|
|
import conv_ref_hook as ch
|
|
|
|
TOKEN = pathlib.Path("/state/outbox-token")
|
|
|
|
|
|
def _groups(path):
|
|
return json.loads(path.read_text())["hooks"].get(ch.EVENT, [])
|
|
|
|
|
|
def _mine(path):
|
|
return [h for g in _groups(path) for h in g["hooks"] if ch.MARK in h["command"]]
|
|
|
|
|
|
def test_the_commands():
|
|
host = ch.command(worker=False, guest=False, python="/usr/bin/python3")
|
|
assert host.startswith("/usr/bin/python3 ") and host.split(" #")[0].endswith("cli/conv_ref.py")
|
|
assert "--outbox" not in host
|
|
w = ch.command(worker=True, guest=False, outbox_url="http://100.1.2.3:8790", token_file=TOKEN,
|
|
python="/v/bin/python")
|
|
assert "--outbox http://100.1.2.3:8790 --token-file /state/outbox-token" in w
|
|
assert ch.command(worker=True, guest=True) is None
|
|
|
|
|
|
def test_added_once_next_to_the_users_own_hooks(tmp_path):
|
|
p = tmp_path / "settings.json"
|
|
p.write_text(json.dumps({"hooks": {ch.EVENT: [{"hooks": [{"type": "command",
|
|
"command": "agent-probe hook"}]}]}}))
|
|
os.chmod(p, 0o600)
|
|
cmd = ch.command(worker=False, guest=False, python="python3")
|
|
assert ch.ensure(p, cmd) is True
|
|
assert [g["hooks"][0]["command"] for g in _groups(p)] == ["agent-probe hook", cmd]
|
|
assert _mine(p)[0]["timeout"] == ch.TIMEOUT_S
|
|
assert os.stat(p).st_mode & 0o777 == 0o600
|
|
assert ch.ensure(p, cmd) is False
|
|
|
|
|
|
def test_a_changed_command_replaces_the_old_one(tmp_path):
|
|
p = tmp_path / "settings.json"
|
|
old = ch.command(worker=True, guest=False, outbox_url="http://a:1", token_file=TOKEN, python="python3")
|
|
new = ch.command(worker=True, guest=False, outbox_url="http://b:2", token_file=TOKEN, python="python3")
|
|
ch.ensure(p, old)
|
|
assert ch.ensure(p, new) is True
|
|
assert [h["command"] for h in _mine(p)] == [new]
|
|
|
|
|
|
def test_removed_on_a_guests_computer_or_when_disabled(tmp_path):
|
|
p = tmp_path / "settings.json"
|
|
ch.ensure(p, ch.command(worker=False, guest=False, python="python3"))
|
|
assert ch.ensure(p, None) is True
|
|
assert _mine(p) == [] and ch.EVENT not in json.loads(p.read_text())["hooks"]
|
|
assert ch.ensure(p, None) is False
|
|
assert ch.ensure(tmp_path / "missing.json", None) is False
|
|
assert not (tmp_path / "missing.json").exists()
|