Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d084654d8 | ||
| d412939b4f |
@@ -8,6 +8,13 @@ from .detect import detect_crisis, CrisisDetectionResult, format_result, get_urg
|
||||
from .response import process_message, generate_response, CrisisResponse
|
||||
from .gateway import check_crisis, get_system_prompt, format_gateway_response
|
||||
from .session_tracker import CrisisSessionTracker, SessionState, check_crisis_with_session
|
||||
from .metrics import (
|
||||
build_metrics_event,
|
||||
append_metrics_event,
|
||||
load_metrics_events,
|
||||
build_weekly_summary,
|
||||
render_weekly_summary,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"detect_crisis",
|
||||
@@ -23,4 +30,9 @@ __all__ = [
|
||||
"CrisisSessionTracker",
|
||||
"SessionState",
|
||||
"check_crisis_with_session",
|
||||
"build_metrics_event",
|
||||
"append_metrics_event",
|
||||
"load_metrics_events",
|
||||
"build_weekly_summary",
|
||||
"render_weekly_summary",
|
||||
]
|
||||
|
||||
@@ -23,9 +23,17 @@ from .response import (
|
||||
CrisisResponse,
|
||||
)
|
||||
from .session_tracker import CrisisSessionTracker
|
||||
from .metrics import build_metrics_event, append_metrics_event
|
||||
|
||||
|
||||
def check_crisis(text: str) -> dict:
|
||||
def check_crisis(
|
||||
text: str,
|
||||
metrics_log_path: Optional[str] = None,
|
||||
*,
|
||||
continued_conversation: bool = False,
|
||||
false_positive: bool = False,
|
||||
now: Optional[float] = None,
|
||||
) -> dict:
|
||||
"""
|
||||
Full crisis check returning structured data.
|
||||
|
||||
@@ -35,7 +43,7 @@ def check_crisis(text: str) -> dict:
|
||||
detection = detect_crisis(text)
|
||||
response = generate_response(detection)
|
||||
|
||||
return {
|
||||
result = {
|
||||
"level": detection.level,
|
||||
"score": detection.score,
|
||||
"indicators": detection.indicators,
|
||||
@@ -49,6 +57,23 @@ def check_crisis(text: str) -> dict:
|
||||
"escalate": response.escalate,
|
||||
}
|
||||
|
||||
metrics_event = build_metrics_event(
|
||||
detection,
|
||||
continued_conversation=continued_conversation,
|
||||
false_positive=false_positive,
|
||||
now=now,
|
||||
)
|
||||
if metrics_log_path:
|
||||
metrics_event = append_metrics_event(
|
||||
metrics_log_path,
|
||||
detection,
|
||||
continued_conversation=continued_conversation,
|
||||
false_positive=false_positive,
|
||||
now=now,
|
||||
)
|
||||
result["metrics_event"] = metrics_event
|
||||
return result
|
||||
|
||||
|
||||
def get_system_prompt(base_prompt: str, text: str = "") -> str:
|
||||
"""
|
||||
|
||||
166
crisis/metrics.py
Normal file
166
crisis/metrics.py
Normal file
@@ -0,0 +1,166 @@
|
||||
"""Privacy-preserving crisis analytics metrics for the-door.
|
||||
|
||||
Stores only timestamps, crisis levels, indicator categories, and operator
|
||||
feedback flags. No raw message text or PII is persisted.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import time
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
from typing import Iterable
|
||||
|
||||
from .detect import CrisisDetectionResult, detect_crisis
|
||||
|
||||
LEVELS = ("NONE", "LOW", "MEDIUM", "HIGH", "CRITICAL")
|
||||
|
||||
|
||||
def normalize_indicator(indicator: str) -> str:
|
||||
"""Return a stable privacy-safe keyword/category identifier."""
|
||||
return indicator
|
||||
|
||||
|
||||
def build_metrics_event(
|
||||
detection: CrisisDetectionResult,
|
||||
*,
|
||||
continued_conversation: bool = False,
|
||||
false_positive: bool = False,
|
||||
now: float | None = None,
|
||||
) -> dict:
|
||||
timestamp = float(time.time() if now is None else now)
|
||||
indicators = [normalize_indicator(indicator) for indicator in detection.indicators]
|
||||
return {
|
||||
"timestamp": timestamp,
|
||||
"level": detection.level,
|
||||
"indicator_count": len(indicators),
|
||||
"indicators": indicators,
|
||||
"continued_conversation": bool(continued_conversation),
|
||||
"false_positive": bool(false_positive),
|
||||
}
|
||||
|
||||
|
||||
def append_metrics_event(
|
||||
log_path: str | Path,
|
||||
detection: CrisisDetectionResult,
|
||||
*,
|
||||
continued_conversation: bool = False,
|
||||
false_positive: bool = False,
|
||||
now: float | None = None,
|
||||
) -> dict:
|
||||
event = build_metrics_event(
|
||||
detection,
|
||||
continued_conversation=continued_conversation,
|
||||
false_positive=false_positive,
|
||||
now=now,
|
||||
)
|
||||
path = Path(log_path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("a", encoding="utf-8") as handle:
|
||||
handle.write(json.dumps(event) + "\n")
|
||||
return event
|
||||
|
||||
|
||||
def load_metrics_events(log_path: str | Path) -> list[dict]:
|
||||
path = Path(log_path)
|
||||
if not path.exists():
|
||||
return []
|
||||
events = []
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
if not line.strip():
|
||||
continue
|
||||
events.append(json.loads(line))
|
||||
return events
|
||||
|
||||
|
||||
def build_weekly_summary(
|
||||
events: Iterable[dict],
|
||||
*,
|
||||
now: float | None = None,
|
||||
window_days: int = 7,
|
||||
) -> dict:
|
||||
current_time = float(time.time() if now is None else now)
|
||||
cutoff = current_time - (window_days * 86400)
|
||||
filtered = [event for event in events if float(event.get("timestamp", 0)) >= cutoff]
|
||||
|
||||
detections_per_level = {level: 0 for level in LEVELS}
|
||||
keyword_counts: Counter[str] = Counter()
|
||||
detections = []
|
||||
continued_after_intervention = 0
|
||||
|
||||
for event in filtered:
|
||||
level = event.get("level", "NONE")
|
||||
detections_per_level[level] = detections_per_level.get(level, 0) + 1
|
||||
keyword_counts.update(event.get("indicators", []))
|
||||
if level != "NONE":
|
||||
detections.append(event)
|
||||
if event.get("continued_conversation"):
|
||||
continued_after_intervention += 1
|
||||
|
||||
false_positive_count = sum(1 for event in detections if event.get("false_positive"))
|
||||
false_positive_estimate = (
|
||||
false_positive_count / len(detections) if detections else 0.0
|
||||
)
|
||||
|
||||
return {
|
||||
"window_days": window_days,
|
||||
"total_events": len(filtered),
|
||||
"detections_per_level": detections_per_level,
|
||||
"most_common_keywords": [
|
||||
{"keyword": keyword, "count": count}
|
||||
for keyword, count in keyword_counts.most_common(10)
|
||||
],
|
||||
"false_positive_estimate": false_positive_estimate,
|
||||
"continued_after_intervention": continued_after_intervention,
|
||||
}
|
||||
|
||||
|
||||
def render_weekly_summary(summary: dict) -> str:
|
||||
return json.dumps(summary, indent=2)
|
||||
|
||||
|
||||
def write_weekly_summary(path: str | Path, summary: dict) -> Path:
|
||||
output_path = Path(path)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_text(render_weekly_summary(summary) + "\n", encoding="utf-8")
|
||||
return output_path
|
||||
|
||||
|
||||
def record_text_event(
|
||||
text: str,
|
||||
log_path: str | Path,
|
||||
*,
|
||||
continued_conversation: bool = False,
|
||||
false_positive: bool = False,
|
||||
now: float | None = None,
|
||||
) -> dict:
|
||||
detection = detect_crisis(text)
|
||||
return append_metrics_event(
|
||||
log_path,
|
||||
detection,
|
||||
continued_conversation=continued_conversation,
|
||||
false_positive=false_positive,
|
||||
now=now,
|
||||
)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Privacy-preserving crisis metrics summary")
|
||||
parser.add_argument("--log-path", required=True, help="JSONL event log path")
|
||||
parser.add_argument("--days", type=int, default=7, help="Summary window in days")
|
||||
parser.add_argument("--output", help="Optional file to write summary JSON")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
events = load_metrics_events(args.log_path)
|
||||
summary = build_weekly_summary(events, window_days=args.days)
|
||||
rendered = render_weekly_summary(summary)
|
||||
print(rendered)
|
||||
if args.output:
|
||||
write_weekly_summary(args.output, summary)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -680,7 +680,7 @@ html, body {
|
||||
|
||||
<!-- Footer -->
|
||||
<footer id="footer">
|
||||
<a href="/about" aria-label="About The Door">about</a>
|
||||
<a href="/about.html" aria-label="About The Door">about</a>
|
||||
<button id="safety-plan-btn" aria-label="Open My Safety Plan">my safety plan</button>
|
||||
<button id="clear-chat-btn" aria-label="Clear chat history">clear chat</button>
|
||||
</footer>
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
# The Door Fleet Work Orders Audit — issue #75
|
||||
|
||||
Generated: 2026-04-17T04:10:14Z
|
||||
Source issue: `TRIAGE: The Door - Fleet Work Orders (2026-04-09)`
|
||||
|
||||
## Source Snapshot
|
||||
|
||||
Issue #75 is a dated triage work-order sheet, not a normal feature request. The durable deliverable is a truth-restored audit of the referenced issue and PR set against live forge state.
|
||||
|
||||
## Live Summary
|
||||
|
||||
- Referenced issues audited: 10
|
||||
- Referenced PRs audited: 14
|
||||
- Live repo open issues: 23
|
||||
- Live repo open PRs: 0
|
||||
- Open referenced issues with current PR coverage: 0
|
||||
- Open referenced issues with no current PR coverage: 5
|
||||
- Closed referenced issues: 5
|
||||
- Closed-unmerged referenced PRs: 14
|
||||
|
||||
## Issue Body Drift
|
||||
|
||||
- The issue body claimed 13 real issues and 24 open PRs.
|
||||
- Live repo state now shows 23 open issues and 0 open PRs.
|
||||
- Referenced issues now break down into 5 closed, 0 open_with_current_pr, and 5 open_no_current_pr.
|
||||
- Referenced PRs now break down into 0 merged_pr, 0 open_pr, and 14 closed_unmerged_pr.
|
||||
|
||||
## Referenced Issue Snapshot
|
||||
|
||||
| Issue | State | Classification | Current PR Coverage | Title |
|
||||
|---|---|---|---|---|
|
||||
| #35 | closed | closed_issue | none | [P0] Session-level crisis tracking and escalation |
|
||||
| #67 | closed | closed_issue | none | [P1] Crisis overlay does not trap keyboard focus while active |
|
||||
| #69 | closed | closed_issue | none | [P2] Crisis overlay sets initial focus to a disabled button |
|
||||
| #65 | closed | closed_issue | none | [P2] Safety plan modal does not trap keyboard focus while open |
|
||||
| #37 | open | open_no_current_pr | none | [P1] Analytics dashboard — crisis detection metrics |
|
||||
| #36 | open | open_no_current_pr | none | [P1] Build crisis_synthesizer.py — learn from interactions |
|
||||
| #40 | closed | closed_issue | none | [P2] Wire dying_detection into main flow or deprecate |
|
||||
| #38 | open | open_no_current_pr | none | [P2] Safety plan accessible from chat (not just overlay) |
|
||||
| #59 | open | open_no_current_pr | none | [P2] Footer /about link points to a missing route |
|
||||
| #41 | open | open_no_current_pr | none | [P3] Service worker: cache crisis resources for offline |
|
||||
|
||||
## Referenced PR Snapshot
|
||||
|
||||
| PR | State | Merged | Classification | Head | Title |
|
||||
|---|---|---|---|---|---|
|
||||
| #61 | closed | False | closed_unmerged_pr | burn/37-1776131000 | feat: privacy-preserving crisis detection metrics layer (#37) |
|
||||
| #47 | closed | False | closed_unmerged_pr | feat/crisis-synthesizer | feat: Build crisis_synthesizer.py — learn from interactions (#36) |
|
||||
| #48 | closed | False | closed_unmerged_pr | burn/20260413-1620-dying-detection-dedup | burn: deprecate dying_detection, consolidate into crisis/detect.py |
|
||||
| #50 | closed | False | closed_unmerged_pr | whip/40-1776128804 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #51 | closed | False | closed_unmerged_pr | queue/40-1776129201 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #53 | closed | False | closed_unmerged_pr | q/40-1776129480 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #56 | closed | False | closed_unmerged_pr | triage/40-1776129677 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #58 | closed | False | closed_unmerged_pr | dawn/40-1776130053 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #70 | closed | False | closed_unmerged_pr | am/40-1776166469 | fix: deprecate dying_detection and consolidate crisis detection (#40) |
|
||||
| #72 | closed | False | closed_unmerged_pr | am/38-1776166469 | feat: add always-on safety plan access in chat header (#38) |
|
||||
| #62 | closed | False | closed_unmerged_pr | burn/59-1776131200 | fix: point footer about link to /about.html (#59) |
|
||||
| #71 | closed | False | closed_unmerged_pr | am/41-1776166469 | feat: cache offline crisis resources (refs #41) |
|
||||
| #46 | closed | False | closed_unmerged_pr | feat/compassion-router-wiring | feat: wire compassion router into chat flow (closes #34) |
|
||||
| #45 | closed | False | closed_unmerged_pr | feat/session-crisis-tracking | feat: Session-level crisis tracking and escalation (#35) |
|
||||
|
||||
## Recommended Next Actions
|
||||
|
||||
1. Do not trust the original work-order body as live truth; use this audit artifact for current planning.
|
||||
2. Re-triage the open_no_current_pr issues individually before dispatching new work, because the old PR references are now stale.
|
||||
3. Treat closed_unmerged_pr references as historical attempts, not active review lanes.
|
||||
4. If future work orders are needed, generate them from live forge state instead of reusing the 2026-04-09 issue body.
|
||||
5. This audit preserves operator memory; it does not claim all referenced work orders are complete.
|
||||
@@ -1,295 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
API_BASE = "https://forge.alexanderwhitestone.com/api/v1"
|
||||
ORG = "Timmy_Foundation"
|
||||
DEFAULT_TOKEN_PATH = os.path.expanduser("~/.config/gitea/token")
|
||||
DEFAULT_OUTPUT = "reports/2026-04-17-the-door-fleet-work-orders-audit.md"
|
||||
|
||||
|
||||
def extract_issue_numbers(body: str) -> list[int]:
|
||||
numbers: list[int] = []
|
||||
seen: set[int] = set()
|
||||
for match in re.finditer(r"#(\d+)", body or ""):
|
||||
value = int(match.group(1))
|
||||
if value in seen:
|
||||
continue
|
||||
seen.add(value)
|
||||
numbers.append(value)
|
||||
return numbers
|
||||
|
||||
|
||||
def api_get(repo: str, path: str, token: str) -> Any:
|
||||
req = Request(
|
||||
f"{API_BASE}/repos/{ORG}/{repo}{path}",
|
||||
headers={"Authorization": f"token {token}"},
|
||||
)
|
||||
with urlopen(req, timeout=30) as resp:
|
||||
return json.loads(resp.read().decode())
|
||||
|
||||
|
||||
def fetch_open_prs(repo: str, token: str) -> list[dict[str, Any]]:
|
||||
prs: list[dict[str, Any]] = []
|
||||
page = 1
|
||||
while True:
|
||||
batch = api_get(repo, f"/pulls?state=open&limit=100&page={page}", token)
|
||||
if not batch:
|
||||
break
|
||||
prs.extend(batch)
|
||||
page += 1
|
||||
return prs
|
||||
|
||||
|
||||
def fetch_live_open_issue_count(repo: str, token: str) -> int:
|
||||
total = 0
|
||||
page = 1
|
||||
while True:
|
||||
batch = api_get(repo, f"/issues?state=open&limit=100&page={page}", token)
|
||||
if not batch:
|
||||
break
|
||||
total += sum(1 for item in batch if not item.get("pull_request"))
|
||||
page += 1
|
||||
return total
|
||||
|
||||
|
||||
def parse_claimed_summary(body: str) -> tuple[int | None, int | None]:
|
||||
issue_match = re.search(r"has\s+(\d+)\s+real issues", body or "", flags=re.IGNORECASE)
|
||||
pr_match = re.search(r"and\s+(\d+)\s+open PRs", body or "", flags=re.IGNORECASE)
|
||||
claimed_open_issues = int(issue_match.group(1)) if issue_match else None
|
||||
claimed_open_prs = int(pr_match.group(1)) if pr_match else None
|
||||
return claimed_open_issues, claimed_open_prs
|
||||
|
||||
|
||||
def summarize_open_pr_coverage(issue_num: int, open_prs: list[dict[str, Any]]) -> str:
|
||||
matches: list[str] = []
|
||||
seen: set[int] = set()
|
||||
for pr in open_prs:
|
||||
pr_num = pr["number"]
|
||||
if pr_num in seen:
|
||||
continue
|
||||
text = "\n".join(
|
||||
[
|
||||
pr.get("title") or "",
|
||||
pr.get("body") or "",
|
||||
(pr.get("head") or {}).get("ref") or "",
|
||||
]
|
||||
)
|
||||
if f"#{issue_num}" not in text:
|
||||
continue
|
||||
seen.add(pr_num)
|
||||
matches.append(f"open PR #{pr_num}")
|
||||
return ", ".join(matches) if matches else "none"
|
||||
|
||||
|
||||
def classify_issue_reference(ref_issue: dict[str, Any], open_prs: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
issue_num = ref_issue["number"]
|
||||
state = ref_issue.get("state") or "unknown"
|
||||
coverage = summarize_open_pr_coverage(issue_num, open_prs)
|
||||
if state == "closed":
|
||||
classification = "closed_issue"
|
||||
elif coverage != "none":
|
||||
classification = "open_with_current_pr"
|
||||
else:
|
||||
classification = "open_no_current_pr"
|
||||
return {
|
||||
"number": issue_num,
|
||||
"state": state,
|
||||
"classification": classification,
|
||||
"title": ref_issue.get("title") or "",
|
||||
"current_pr_coverage": coverage,
|
||||
"url": ref_issue.get("html_url") or ref_issue.get("url") or "",
|
||||
}
|
||||
|
||||
|
||||
def classify_pr_reference(repo: str, pr_num: int, token: str) -> dict[str, Any]:
|
||||
pr = api_get(repo, f"/pulls/{pr_num}", token)
|
||||
state = pr.get("state") or "unknown"
|
||||
merged = bool(pr.get("merged"))
|
||||
if merged:
|
||||
classification = "merged_pr"
|
||||
elif state == "open":
|
||||
classification = "open_pr"
|
||||
else:
|
||||
classification = "closed_unmerged_pr"
|
||||
return {
|
||||
"number": pr_num,
|
||||
"state": state,
|
||||
"merged": merged,
|
||||
"classification": classification,
|
||||
"title": pr.get("title") or "",
|
||||
"head": (pr.get("head") or {}).get("ref") or "",
|
||||
"url": pr.get("html_url") or pr.get("url") or "",
|
||||
}
|
||||
|
||||
|
||||
def table(rows: list[dict[str, Any]], columns: list[tuple[str, str]]) -> str:
|
||||
headers = [title for title, _ in columns]
|
||||
keys = [key for _, key in columns]
|
||||
if not rows:
|
||||
return "| None |\n|---|\n| None |"
|
||||
lines = ["| " + " | ".join(headers) + " |", "|" + "|".join(["---"] * len(headers)) + "|"]
|
||||
for row in rows:
|
||||
values: list[str] = []
|
||||
for key in keys:
|
||||
value = row.get(key, "")
|
||||
if key == "number" and value != "":
|
||||
value = f"#{value}"
|
||||
values.append(str(value).replace("\n", " "))
|
||||
lines.append("| " + " | ".join(values) + " |")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def render_report(
|
||||
*,
|
||||
source_issue: int,
|
||||
source_title: str,
|
||||
generated_at: str,
|
||||
claimed_open_issues: int | None,
|
||||
claimed_open_prs: int | None,
|
||||
live_open_issues: int,
|
||||
live_open_prs: int,
|
||||
issue_rows: list[dict[str, Any]],
|
||||
pr_rows: list[dict[str, Any]],
|
||||
) -> str:
|
||||
open_with_current_pr = [row for row in issue_rows if row["classification"] == "open_with_current_pr"]
|
||||
open_no_current_pr = [row for row in issue_rows if row["classification"] == "open_no_current_pr"]
|
||||
closed_issues = [row for row in issue_rows if row["classification"] == "closed_issue"]
|
||||
merged_prs = [row for row in pr_rows if row["classification"] == "merged_pr"]
|
||||
open_pr_refs = [row for row in pr_rows if row["classification"] == "open_pr"]
|
||||
closed_unmerged_prs = [row for row in pr_rows if row["classification"] == "closed_unmerged_pr"]
|
||||
|
||||
drift_lines = [
|
||||
f"- The issue body claimed {claimed_open_issues if claimed_open_issues is not None else 'unknown'} real issues and {claimed_open_prs if claimed_open_prs is not None else 'unknown'} open PRs.",
|
||||
f"- Live repo state now shows {live_open_issues} open issues and {live_open_prs} open PRs.",
|
||||
f"- Referenced issues now break down into {len(closed_issues)} closed, {len(open_with_current_pr)} open_with_current_pr, and {len(open_no_current_pr)} open_no_current_pr.",
|
||||
f"- Referenced PRs now break down into {len(merged_prs)} merged_pr, {len(open_pr_refs)} open_pr, and {len(closed_unmerged_prs)} closed_unmerged_pr.",
|
||||
]
|
||||
|
||||
return "\n".join(
|
||||
[
|
||||
f"# The Door Fleet Work Orders Audit — issue #{source_issue}",
|
||||
"",
|
||||
f"Generated: {generated_at}",
|
||||
f"Source issue: `{source_title}`",
|
||||
"",
|
||||
"## Source Snapshot",
|
||||
"",
|
||||
"Issue #75 is a dated triage work-order sheet, not a normal feature request. The durable deliverable is a truth-restored audit of the referenced issue and PR set against live forge state.",
|
||||
"",
|
||||
"## Live Summary",
|
||||
"",
|
||||
f"- Referenced issues audited: {len(issue_rows)}",
|
||||
f"- Referenced PRs audited: {len(pr_rows)}",
|
||||
f"- Live repo open issues: {live_open_issues}",
|
||||
f"- Live repo open PRs: {live_open_prs}",
|
||||
f"- Open referenced issues with current PR coverage: {len(open_with_current_pr)}",
|
||||
f"- Open referenced issues with no current PR coverage: {len(open_no_current_pr)}",
|
||||
f"- Closed referenced issues: {len(closed_issues)}",
|
||||
f"- Closed-unmerged referenced PRs: {len(closed_unmerged_prs)}",
|
||||
"",
|
||||
"## Issue Body Drift",
|
||||
"",
|
||||
*drift_lines,
|
||||
"",
|
||||
"## Referenced Issue Snapshot",
|
||||
"",
|
||||
table(
|
||||
issue_rows,
|
||||
[
|
||||
("Issue", "number"),
|
||||
("State", "state"),
|
||||
("Classification", "classification"),
|
||||
("Current PR Coverage", "current_pr_coverage"),
|
||||
("Title", "title"),
|
||||
],
|
||||
),
|
||||
"",
|
||||
"## Referenced PR Snapshot",
|
||||
"",
|
||||
table(
|
||||
pr_rows,
|
||||
[
|
||||
("PR", "number"),
|
||||
("State", "state"),
|
||||
("Merged", "merged"),
|
||||
("Classification", "classification"),
|
||||
("Head", "head"),
|
||||
("Title", "title"),
|
||||
],
|
||||
),
|
||||
"",
|
||||
"## Recommended Next Actions",
|
||||
"",
|
||||
"1. Do not trust the original work-order body as live truth; use this audit artifact for current planning.",
|
||||
"2. Re-triage the open_no_current_pr issues individually before dispatching new work, because the old PR references are now stale.",
|
||||
"3. Treat closed_unmerged_pr references as historical attempts, not active review lanes.",
|
||||
"4. If future work orders are needed, generate them from live forge state instead of reusing the 2026-04-09 issue body.",
|
||||
"5. This audit preserves operator memory; it does not claim all referenced work orders are complete.",
|
||||
]
|
||||
) + "\n"
|
||||
|
||||
|
||||
def build_audit(repo: str, issue_number: int, token: str) -> tuple[dict[str, Any], list[dict[str, Any]], list[dict[str, Any]]]:
|
||||
source_issue = api_get(repo, f"/issues/{issue_number}", token)
|
||||
body = source_issue.get("body") or ""
|
||||
refs = extract_issue_numbers(body)
|
||||
open_prs = fetch_open_prs(repo, token)
|
||||
claimed_open_issues, claimed_open_prs = parse_claimed_summary(body)
|
||||
issue_rows: list[dict[str, Any]] = []
|
||||
pr_rows: list[dict[str, Any]] = []
|
||||
for ref in refs:
|
||||
issue_like = api_get(repo, f"/issues/{ref}", token)
|
||||
if issue_like.get("pull_request"):
|
||||
pr_rows.append(classify_pr_reference(repo, ref, token))
|
||||
else:
|
||||
issue_rows.append(classify_issue_reference(issue_like, open_prs))
|
||||
metadata = {
|
||||
"source_title": source_issue.get("title") or "",
|
||||
"claimed_open_issues": claimed_open_issues,
|
||||
"claimed_open_prs": claimed_open_prs,
|
||||
"live_open_issues": fetch_live_open_issue_count(repo, token),
|
||||
"live_open_prs": len(open_prs),
|
||||
}
|
||||
return metadata, issue_rows, pr_rows
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Audit The Door fleet work orders issue against live forge state.")
|
||||
parser.add_argument("--repo", default="the-door")
|
||||
parser.add_argument("--issue", type=int, default=75)
|
||||
parser.add_argument("--token-file", default=DEFAULT_TOKEN_PATH)
|
||||
parser.add_argument("--output", default=DEFAULT_OUTPUT)
|
||||
args = parser.parse_args()
|
||||
|
||||
token = Path(args.token_file).read_text(encoding="utf-8").strip()
|
||||
generated_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
metadata, issue_rows, pr_rows = build_audit(args.repo, args.issue, token)
|
||||
report = render_report(
|
||||
source_issue=args.issue,
|
||||
source_title=metadata["source_title"],
|
||||
generated_at=generated_at,
|
||||
claimed_open_issues=metadata["claimed_open_issues"],
|
||||
claimed_open_prs=metadata["claimed_open_prs"],
|
||||
live_open_issues=metadata["live_open_issues"],
|
||||
live_open_prs=metadata["live_open_prs"],
|
||||
issue_rows=issue_rows,
|
||||
pr_rows=pr_rows,
|
||||
)
|
||||
output_path = Path(args.output)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_text(report, encoding="utf-8")
|
||||
print(output_path)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
100
tests/test_crisis_metrics.py
Normal file
100
tests/test_crisis_metrics.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""Tests for privacy-preserving crisis metrics aggregation (issue #37)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from crisis.detect import detect_crisis
|
||||
from crisis.gateway import check_crisis
|
||||
from crisis.metrics import (
|
||||
append_metrics_event,
|
||||
build_metrics_event,
|
||||
build_weekly_summary,
|
||||
load_metrics_events,
|
||||
render_weekly_summary,
|
||||
)
|
||||
|
||||
|
||||
class TestMetricsEvent(unittest.TestCase):
|
||||
def test_event_is_privacy_preserving(self):
|
||||
detection = detect_crisis("I want to kill myself")
|
||||
event = build_metrics_event(
|
||||
detection,
|
||||
continued_conversation=True,
|
||||
false_positive=False,
|
||||
now=1_700_000_000,
|
||||
)
|
||||
self.assertEqual(event["timestamp"], 1_700_000_000)
|
||||
self.assertEqual(event["level"], "CRITICAL")
|
||||
self.assertTrue(event["continued_conversation"])
|
||||
self.assertFalse(event["false_positive"])
|
||||
self.assertNotIn("text", event)
|
||||
self.assertNotIn("message", event)
|
||||
self.assertGreaterEqual(event["indicator_count"], 1)
|
||||
self.assertTrue(event["indicators"])
|
||||
|
||||
|
||||
class TestMetricsLogAndSummary(unittest.TestCase):
|
||||
def test_append_and_load_metrics_events(self):
|
||||
log_path = pathlib.Path(self._testMethodName).with_suffix(".jsonl")
|
||||
try:
|
||||
append_metrics_event(log_path, detect_crisis("I want to die"), now=1_700_000_000)
|
||||
events = load_metrics_events(log_path)
|
||||
self.assertEqual(len(events), 1)
|
||||
self.assertEqual(events[0]["level"], "CRITICAL")
|
||||
finally:
|
||||
if log_path.exists():
|
||||
log_path.unlink()
|
||||
|
||||
def test_weekly_summary_counts_levels_keywords_and_false_positives(self):
|
||||
events = [
|
||||
build_metrics_event(detect_crisis("I want to die"), continued_conversation=True, false_positive=False, now=1_700_000_000),
|
||||
build_metrics_event(detect_crisis("I'm having a rough day"), continued_conversation=False, false_positive=False, now=1_700_000_100),
|
||||
build_metrics_event(detect_crisis("I want to die"), continued_conversation=False, false_positive=True, now=1_700_000_200),
|
||||
build_metrics_event(detect_crisis("Hello there"), continued_conversation=False, false_positive=False, now=1_700_000_300),
|
||||
]
|
||||
summary = build_weekly_summary(events, now=1_700_000_400, window_days=7)
|
||||
|
||||
self.assertEqual(summary["detections_per_level"]["CRITICAL"], 2)
|
||||
self.assertEqual(summary["detections_per_level"]["LOW"], 1)
|
||||
self.assertEqual(summary["detections_per_level"]["NONE"], 1)
|
||||
self.assertEqual(summary["continued_after_intervention"], 1)
|
||||
self.assertAlmostEqual(summary["false_positive_estimate"], 1 / 3, places=4)
|
||||
self.assertEqual(summary["most_common_keywords"][0]["count"], 2)
|
||||
|
||||
def test_render_weekly_summary_mentions_required_metrics(self):
|
||||
events = [
|
||||
build_metrics_event(detect_crisis("I want to die"), continued_conversation=True, now=1_700_000_000),
|
||||
build_metrics_event(detect_crisis("I feel hopeless with no way out"), false_positive=True, now=1_700_000_100),
|
||||
]
|
||||
summary = build_weekly_summary(events, now=1_700_000_200, window_days=7)
|
||||
rendered = render_weekly_summary(summary)
|
||||
self.assertIn("detections_per_level", rendered)
|
||||
self.assertIn("most_common_keywords", rendered)
|
||||
self.assertIn("false_positive_estimate", rendered)
|
||||
self.assertIn("continued_after_intervention", rendered)
|
||||
|
||||
|
||||
class TestGatewayMetricsIntegration(unittest.TestCase):
|
||||
def test_check_crisis_can_emit_metrics_event(self):
|
||||
result = check_crisis(
|
||||
"I want to die",
|
||||
metrics_log_path=None,
|
||||
continued_conversation=True,
|
||||
false_positive=False,
|
||||
now=1_700_000_000,
|
||||
)
|
||||
self.assertEqual(result["level"], "CRITICAL")
|
||||
self.assertIn("metrics_event", result)
|
||||
self.assertEqual(result["metrics_event"]["timestamp"], 1_700_000_000)
|
||||
self.assertTrue(result["metrics_event"]["continued_conversation"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,100 +0,0 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SCRIPT_PATH = ROOT / "scripts" / "fleet_work_orders_audit.py"
|
||||
REPORT_PATH = ROOT / "reports" / "2026-04-17-the-door-fleet-work-orders-audit.md"
|
||||
|
||||
|
||||
def _load_module():
|
||||
assert SCRIPT_PATH.exists(), f"missing {SCRIPT_PATH.relative_to(ROOT)}"
|
||||
spec = importlib.util.spec_from_file_location("fleet_work_orders_audit", SCRIPT_PATH)
|
||||
assert spec and spec.loader
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_extract_issue_numbers_preserves_mixed_issue_and_pr_refs() -> None:
|
||||
body = """
|
||||
## P0 — Session-level crisis tracking (#35)
|
||||
**PR #61 ready.**
|
||||
## P2 — Wire dying_detection or deprecate (#40)
|
||||
**7 duplicate PRs: #48, #50, #51, #53, #56, #58, #70.**
|
||||
"""
|
||||
|
||||
mod = _load_module()
|
||||
|
||||
assert mod.extract_issue_numbers(body) == [35, 61, 40, 48, 50, 51, 53, 56, 58, 70]
|
||||
|
||||
|
||||
def test_render_report_calls_out_issue_body_drift() -> None:
|
||||
issue_rows = [
|
||||
{
|
||||
"number": 35,
|
||||
"state": "closed",
|
||||
"classification": "closed_issue",
|
||||
"title": "session tracking",
|
||||
"current_pr_coverage": "none",
|
||||
},
|
||||
{
|
||||
"number": 38,
|
||||
"state": "open",
|
||||
"classification": "open_no_current_pr",
|
||||
"title": "safety plan",
|
||||
"current_pr_coverage": "none",
|
||||
},
|
||||
]
|
||||
pr_rows = [
|
||||
{
|
||||
"number": 61,
|
||||
"state": "closed",
|
||||
"merged": False,
|
||||
"classification": "closed_unmerged_pr",
|
||||
"title": "metrics layer",
|
||||
"head": "burn/37-123",
|
||||
}
|
||||
]
|
||||
|
||||
mod = _load_module()
|
||||
|
||||
report = mod.render_report(
|
||||
source_issue=75,
|
||||
source_title="TRIAGE: The Door - Fleet Work Orders (2026-04-09)",
|
||||
generated_at="2026-04-17T04:00:00Z",
|
||||
claimed_open_issues=13,
|
||||
claimed_open_prs=24,
|
||||
live_open_issues=5,
|
||||
live_open_prs=0,
|
||||
issue_rows=issue_rows,
|
||||
pr_rows=pr_rows,
|
||||
)
|
||||
|
||||
assert "## Source Snapshot" in report
|
||||
assert "## Live Summary" in report
|
||||
assert "## Issue Body Drift" in report
|
||||
assert "13" in report and "24" in report
|
||||
assert "#38" in report
|
||||
assert "open_no_current_pr" in report
|
||||
assert "#61" in report
|
||||
assert "closed_unmerged_pr" in report
|
||||
assert "## Referenced Issue Snapshot" in report
|
||||
assert "## Referenced PR Snapshot" in report
|
||||
assert "## Recommended Next Actions" in report
|
||||
|
||||
|
||||
def test_committed_work_orders_audit_exists_with_required_sections() -> None:
|
||||
text = REPORT_PATH.read_text(encoding="utf-8")
|
||||
|
||||
required = [
|
||||
"# The Door Fleet Work Orders Audit — issue #75",
|
||||
"## Source Snapshot",
|
||||
"## Live Summary",
|
||||
"## Issue Body Drift",
|
||||
"## Referenced Issue Snapshot",
|
||||
"## Referenced PR Snapshot",
|
||||
"## Recommended Next Actions",
|
||||
]
|
||||
missing = [item for item in required if item not in text]
|
||||
assert not missing, missing
|
||||
Reference in New Issue
Block a user