Restore followup/DM reliability fixes wiped by 19:43Z tree-clean

Re-applies three workstreams lost when 793d3d7 committed over uncommitted
edits, reconciled against the parallel track's committed dm.py changes:
- followup-sweeper.py: backfill thread_uuid after successful nudge sends;
  record final_nudge_target=main on final-nudge routing (C1/C2)
- response-harvester.py: resolve followups on main-chat replies when
  final_nudge_target=main (C3); harvest ALL [RESULT] markers per message
- dm.py: pre-send placement gate (fail closed when post-nav URL lacks the
  target thread UUID; skips main) — purely additive over 793d3d7+f268d3d
- sidechat_manager.py: wait_for_chat_list() settle-poll for list population
  race (sidebar button renders before titles load)
- new: bin/tests/test_followup_fixes.py (25 tests), bin/placement-audit.py,
  bin/dm-log-taxonomy.py, bin/session-probe.py,
  docs/SIDECHAT-RELIABILITY.md, docs/UUID-ROTATION.md

Verified: 25/25 tests pass, py_compile clean, sweeper/harvester dry-runs clean.
Known limitation: gate catches wrong-placement, not wrong-mapping (false
autoprovision adopting the parked thread needs a creation check).
This commit is contained in:
operator-main
2026-10-04 20:06:58 +00:00
parent b5c5e2ff4c
commit ad9dbca7eb
10 changed files with 1984 additions and 51 deletions
Executable → Regular
+52
View File
@@ -14,6 +14,7 @@ Usage:
import argparse
import json
import os
import re
import subprocess
import sys
import time
@@ -26,6 +27,7 @@ BIN_DIR = NETVM_ROOT / "bin"
JOBS_DIR = NETVM_ROOT / "jobs"
FOLLOWUPS_FILE = NETVM_ROOT / "followups.json"
JOB_LOG = NETVM_ROOT / "job-log.jsonl"
DM_LOG = NETVM_ROOT / "dm-log.jsonl"
DM_PY = BIN_DIR / "dm.py"
DISPATCH_PY = BIN_DIR / "job-dispatch.py"
@@ -101,6 +103,46 @@ def send_dm(sender, recipient, target, text):
except Exception as e:
return False, str(e)
_DM_ID_RE = re.compile(r"\bDM ([0-9a-f]{8})\b")
_THREAD_UUID_RE = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
re.IGNORECASE,
)
def _resolve_nudge_thread_uuid(nudge_output):
"""Parse the DM id from dm.py's stdout, scan the tail (last 5000 lines)
of dm-log.jsonl for that id's sidechat_autoprovisioned event, falling
back to the sent event's tags.thread. Returns the UUID or None."""
m = _DM_ID_RE.search(nudge_output or "")
if not m:
return None
nudge_id = m.group(1)
fallback = None
try:
with open(DM_LOG, "r", encoding="utf-8") as f:
lines = f.readlines()
except FileNotFoundError:
return None
for line in lines[-5000:]:
line = line.strip()
if not line:
continue
try:
e = json.loads(line)
except Exception:
continue
if e.get("id") != nudge_id:
continue
if e.get("type") == "sidechat_autoprovisioned":
uuid = e.get("thread_uuid") or ""
if _THREAD_UUID_RE.fullmatch(uuid):
return uuid
elif e.get("type") == "sent":
cand = ((e.get("tags") or {}).get("thread")) or ""
if _THREAD_UUID_RE.fullmatch(cand):
fallback = cand
return fallback
def sweep_cycle(dry_run=False):
followups = load_followups()
@@ -181,6 +223,15 @@ def sweep_cycle(dry_run=False):
timeout_s = rec.get("timeout_s", 1800)
step_s = max(300, timeout_s // (nudges_allowed + 1))
rec["deadline"] = (now + timedelta(seconds=step_s)).isoformat()
# C1: backfill the thread the nudge actually landed in.
landed_uuid = _resolve_nudge_thread_uuid(out)
if landed_uuid and rec.get("thread_uuid") != landed_uuid:
rec["thread_uuid"] = landed_uuid
print(f"Sweeper: backfilled thread_uuid={landed_uuid} "
f"for followup {dm_id}", file=sys.stderr)
# C2: record final-nudge routing for the harvester.
if is_final:
rec["final_nudge_target"] = "main"
modified = True
append_job_log({
"ts": utcnow_str(),
@@ -189,6 +240,7 @@ def sweep_cycle(dry_run=False):
"nudge_num": nudge_num,
"recipient": recipient,
"target": delivery_target,
"thread_uuid": rec.get("thread_uuid"),
})
else:
print(f"Sweeper WARNING: nudge send failed: {out}", file=sys.stderr)