Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e103dc8b7 |
@@ -1,97 +0,0 @@
|
||||
name: Agent PR Gate
|
||||
'on':
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
gate:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
syntax_status: ${{ steps.syntax.outcome }}
|
||||
tests_status: ${{ steps.tests.outcome }}
|
||||
criteria_status: ${{ steps.criteria.outcome }}
|
||||
risk_level: ${{ steps.risk.outputs.level }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install CI dependencies
|
||||
run: |
|
||||
python3 -m pip install --quiet pyyaml pytest
|
||||
|
||||
- id: risk
|
||||
name: Classify PR risk
|
||||
run: |
|
||||
BASE_REF="${GITHUB_BASE_REF:-main}"
|
||||
git fetch origin "$BASE_REF" --depth 1
|
||||
git diff --name-only "origin/$BASE_REF"...HEAD > /tmp/changed_files.txt
|
||||
python3 scripts/agent_pr_gate.py classify-risk --files-file /tmp/changed_files.txt > /tmp/risk.json
|
||||
python3 - <<'PY'
|
||||
import json, os
|
||||
with open('/tmp/risk.json', 'r', encoding='utf-8') as fh:
|
||||
data = json.load(fh)
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as fh:
|
||||
fh.write('level=' + data['risk'] + '\n')
|
||||
PY
|
||||
|
||||
- id: syntax
|
||||
name: Syntax and parse checks
|
||||
continue-on-error: true
|
||||
run: |
|
||||
find . \( -name '*.yml' -o -name '*.yaml' \) | grep -v .gitea | xargs -r python3 -c "import sys,yaml; [yaml.safe_load(open(f)) for f in sys.argv[1:]]"
|
||||
find . -name '*.json' | while read f; do python3 -m json.tool "$f" > /dev/null || exit 1; done
|
||||
find . -name '*.py' | xargs -r python3 -m py_compile
|
||||
find . -name '*.sh' | xargs -r bash -n
|
||||
|
||||
- id: tests
|
||||
name: Test suite
|
||||
continue-on-error: true
|
||||
run: |
|
||||
pytest -q --ignore=uni-wizard/v2/tests/test_author_whitelist.py
|
||||
|
||||
- id: criteria
|
||||
name: PR criteria verification
|
||||
continue-on-error: true
|
||||
run: |
|
||||
python3 scripts/agent_pr_gate.py validate-pr --event-path "$GITHUB_EVENT_PATH"
|
||||
|
||||
- name: Fail gate if any required check failed
|
||||
if: steps.syntax.outcome != 'success' || steps.tests.outcome != 'success' || steps.criteria.outcome != 'success'
|
||||
run: exit 1
|
||||
|
||||
report:
|
||||
needs: gate
|
||||
if: always()
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Post PR gate report
|
||||
env:
|
||||
GITEA_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
python3 scripts/agent_pr_gate.py comment \
|
||||
--event-path "$GITHUB_EVENT_PATH" \
|
||||
--token "$GITEA_TOKEN" \
|
||||
--syntax "${{ needs.gate.outputs.syntax_status }}" \
|
||||
--tests "${{ needs.gate.outputs.tests_status }}" \
|
||||
--criteria "${{ needs.gate.outputs.criteria_status }}" \
|
||||
--risk "${{ needs.gate.outputs.risk_level }}"
|
||||
|
||||
- name: Auto-merge low-risk clean PRs
|
||||
if: needs.gate.result == 'success' && needs.gate.outputs.risk_level == 'low'
|
||||
env:
|
||||
GITEA_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
python3 scripts/agent_pr_gate.py merge \
|
||||
--event-path "$GITHUB_EVENT_PATH" \
|
||||
--token "$GITEA_TOKEN"
|
||||
@@ -1,5 +1,5 @@
|
||||
name: Smoke Test
|
||||
'on':
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
@@ -11,13 +11,10 @@ jobs:
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
- name: Install parse dependencies
|
||||
run: |
|
||||
python3 -m pip install --quiet pyyaml
|
||||
- name: Parse check
|
||||
run: |
|
||||
find . \( -name '*.yml' -o -name '*.yaml' \) | grep -v .gitea | xargs -r python3 -c "import sys,yaml; [yaml.safe_load(open(f)) for f in sys.argv[1:]]"
|
||||
find . -name '*.json' | while read f; do python3 -m json.tool "$f" > /dev/null || exit 1; done
|
||||
find . -name '*.yml' -o -name '*.yaml' | grep -v .gitea | xargs -r python3 -c "import sys,yaml; [yaml.safe_load(open(f)) for f in sys.argv[1:]]"
|
||||
find . -name '*.json' | xargs -r python3 -m json.tool > /dev/null
|
||||
find . -name '*.py' | xargs -r python3 -m py_compile
|
||||
find . -name '*.sh' | xargs -r bash -n
|
||||
echo "PASS: All files parse"
|
||||
|
||||
@@ -1,191 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
API_BASE = "https://forge.alexanderwhitestone.com/api/v1"
|
||||
LOW_RISK_PREFIXES = (
|
||||
'docs/', 'reports/', 'notes/', 'tickets/', 'research/', 'briefings/',
|
||||
'twitter-archive/notes/', 'tests/'
|
||||
)
|
||||
LOW_RISK_SUFFIXES = {'.md', '.txt', '.jsonl'}
|
||||
MEDIUM_RISK_PREFIXES = ('.gitea/workflows/',)
|
||||
HIGH_RISK_PREFIXES = (
|
||||
'scripts/', 'deploy/', 'infrastructure/', 'metrics/', 'heartbeat/',
|
||||
'wizards/', 'evennia/', 'uniwizard/', 'uni-wizard/', 'timmy-local/',
|
||||
'evolution/'
|
||||
)
|
||||
HIGH_RISK_SUFFIXES = {'.py', '.sh', '.ini', '.service'}
|
||||
|
||||
|
||||
def read_changed_files(path):
|
||||
return [line.strip() for line in Path(path).read_text(encoding='utf-8').splitlines() if line.strip()]
|
||||
|
||||
|
||||
def classify_risk(files):
|
||||
if not files:
|
||||
return 'high'
|
||||
level = 'low'
|
||||
for file_path in files:
|
||||
path = file_path.strip()
|
||||
suffix = Path(path).suffix.lower()
|
||||
if path.startswith(LOW_RISK_PREFIXES):
|
||||
continue
|
||||
if path.startswith(HIGH_RISK_PREFIXES) or suffix in HIGH_RISK_SUFFIXES:
|
||||
return 'high'
|
||||
if path.startswith(MEDIUM_RISK_PREFIXES):
|
||||
level = 'medium'
|
||||
continue
|
||||
if path.startswith(LOW_RISK_PREFIXES) or suffix in LOW_RISK_SUFFIXES:
|
||||
continue
|
||||
level = 'high'
|
||||
return level
|
||||
|
||||
|
||||
def validate_pr_body(title, body):
|
||||
details = []
|
||||
combined = f"{title}\n{body}".strip()
|
||||
if not re.search(r'#\d+', combined):
|
||||
details.append('PR body/title must include an issue reference like #562.')
|
||||
if not re.search(r'(^|\n)\s*(verification|tests?)\s*:', body, re.IGNORECASE):
|
||||
details.append('PR body must include a Verification: section.')
|
||||
return (len(details) == 0, details)
|
||||
|
||||
|
||||
def build_comment_body(syntax_status, tests_status, criteria_status, risk_level):
|
||||
statuses = {
|
||||
'syntax': syntax_status,
|
||||
'tests': tests_status,
|
||||
'criteria': criteria_status,
|
||||
}
|
||||
all_clean = all(value == 'success' for value in statuses.values())
|
||||
action = 'auto-merge' if all_clean and risk_level == 'low' else 'human review'
|
||||
lines = [
|
||||
'## Agent PR Gate',
|
||||
'',
|
||||
'| Check | Status |',
|
||||
'|-------|--------|',
|
||||
f"| Syntax / parse | {syntax_status} |",
|
||||
f"| Test suite | {tests_status} |",
|
||||
f"| PR criteria | {criteria_status} |",
|
||||
f"| Risk level | {risk_level} |",
|
||||
'',
|
||||
]
|
||||
failed = [name for name, value in statuses.items() if value != 'success']
|
||||
if failed:
|
||||
lines.append('### Failure details')
|
||||
for name in failed:
|
||||
lines.append(f'- {name} reported failure. Inspect the workflow logs for that step.')
|
||||
else:
|
||||
lines.append('All automated checks passed.')
|
||||
lines.extend([
|
||||
'',
|
||||
f'Recommendation: {action}.',
|
||||
'Low-risk documentation/test-only PRs may be auto-merged. Operational changes stay in human review.',
|
||||
])
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
def _read_event(event_path):
|
||||
data = json.loads(Path(event_path).read_text(encoding='utf-8'))
|
||||
pr = data.get('pull_request') or {}
|
||||
repo = (data.get('repository') or {}).get('full_name') or os.environ.get('GITHUB_REPOSITORY')
|
||||
pr_number = pr.get('number') or data.get('number')
|
||||
title = pr.get('title') or ''
|
||||
body = pr.get('body') or ''
|
||||
return repo, pr_number, title, body
|
||||
|
||||
|
||||
def _request_json(method, url, token, payload=None):
|
||||
data = None if payload is None else json.dumps(payload).encode('utf-8')
|
||||
headers = {'Authorization': f'token {token}', 'Content-Type': 'application/json'}
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
return json.loads(resp.read().decode('utf-8'))
|
||||
|
||||
|
||||
def post_comment(repo, pr_number, token, body):
|
||||
url = f'{API_BASE}/repos/{repo}/issues/{pr_number}/comments'
|
||||
return _request_json('POST', url, token, {'body': body})
|
||||
|
||||
|
||||
def merge_pr(repo, pr_number, token):
|
||||
url = f'{API_BASE}/repos/{repo}/pulls/{pr_number}/merge'
|
||||
return _request_json('POST', url, token, {'Do': 'merge'})
|
||||
|
||||
|
||||
def cmd_classify_risk(args):
|
||||
files = list(args.files or [])
|
||||
if args.files_file:
|
||||
files.extend(read_changed_files(args.files_file))
|
||||
print(json.dumps({'risk': classify_risk(files), 'files': files}, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_validate_pr(args):
|
||||
_, _, title, body = _read_event(args.event_path)
|
||||
ok, details = validate_pr_body(title, body)
|
||||
if ok:
|
||||
print('PR body validation passed.')
|
||||
return 0
|
||||
for detail in details:
|
||||
print(detail)
|
||||
return 1
|
||||
|
||||
|
||||
def cmd_comment(args):
|
||||
repo, pr_number, _, _ = _read_event(args.event_path)
|
||||
body = build_comment_body(args.syntax, args.tests, args.criteria, args.risk)
|
||||
post_comment(repo, pr_number, args.token, body)
|
||||
print(f'Commented on PR #{pr_number} in {repo}.')
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_merge(args):
|
||||
repo, pr_number, _, _ = _read_event(args.event_path)
|
||||
merge_pr(repo, pr_number, args.token)
|
||||
print(f'Merged PR #{pr_number} in {repo}.')
|
||||
return 0
|
||||
|
||||
|
||||
def build_parser():
|
||||
parser = argparse.ArgumentParser(description='Agent PR CI helpers for timmy-home.')
|
||||
sub = parser.add_subparsers(dest='command', required=True)
|
||||
|
||||
classify = sub.add_parser('classify-risk')
|
||||
classify.add_argument('--files-file')
|
||||
classify.add_argument('files', nargs='*')
|
||||
classify.set_defaults(func=cmd_classify_risk)
|
||||
|
||||
validate = sub.add_parser('validate-pr')
|
||||
validate.add_argument('--event-path', required=True)
|
||||
validate.set_defaults(func=cmd_validate_pr)
|
||||
|
||||
comment = sub.add_parser('comment')
|
||||
comment.add_argument('--event-path', required=True)
|
||||
comment.add_argument('--token', required=True)
|
||||
comment.add_argument('--syntax', required=True)
|
||||
comment.add_argument('--tests', required=True)
|
||||
comment.add_argument('--criteria', required=True)
|
||||
comment.add_argument('--risk', required=True)
|
||||
comment.set_defaults(func=cmd_comment)
|
||||
|
||||
merge = sub.add_parser('merge')
|
||||
merge.add_argument('--event-path', required=True)
|
||||
merge.add_argument('--token', required=True)
|
||||
merge.set_defaults(func=cmd_merge)
|
||||
return parser
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = build_parser()
|
||||
args = parser.parse_args(argv)
|
||||
return args.func(args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
35
tests/docs/test_the_door_genome.py
Normal file
35
tests/docs/test_the_door_genome.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _content() -> str:
|
||||
return Path("the-door-GENOME.md").read_text()
|
||||
|
||||
|
||||
def test_the_door_genome_exists() -> None:
|
||||
assert Path("the-door-GENOME.md").exists()
|
||||
|
||||
|
||||
def test_the_door_genome_has_required_sections() -> None:
|
||||
content = _content()
|
||||
assert "# GENOME.md — the-door" in content
|
||||
assert "## Project Overview" in content
|
||||
assert "## Architecture" in content
|
||||
assert "```mermaid" in content
|
||||
assert "## Entry Points" in content
|
||||
assert "## Data Flow" in content
|
||||
assert "## Key Abstractions" in content
|
||||
assert "## API Surface" in content
|
||||
assert "## Test Coverage Gaps" in content
|
||||
assert "## Security Considerations" in content
|
||||
assert "## Dependencies" in content
|
||||
assert "## Deployment" in content
|
||||
assert "## Technical Debt" in content
|
||||
|
||||
|
||||
def test_the_door_genome_captures_repo_specific_findings() -> None:
|
||||
content = _content()
|
||||
assert "lastUserMessage" in content
|
||||
assert "localStorage" in content
|
||||
assert "crisis-offline.html" in content
|
||||
assert "hermes-gateway.service" in content
|
||||
assert "/api/v1/chat/completions" in content
|
||||
@@ -1,68 +0,0 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'scripts'))
|
||||
|
||||
import agent_pr_gate # noqa: E402
|
||||
|
||||
|
||||
class TestAgentPrGate(unittest.TestCase):
|
||||
def test_classify_risk_low_for_docs_and_tests_only(self):
|
||||
level = agent_pr_gate.classify_risk([
|
||||
'docs/runbook.md',
|
||||
'reports/daily-summary.md',
|
||||
'tests/test_agent_pr_gate.py',
|
||||
])
|
||||
self.assertEqual(level, 'low')
|
||||
|
||||
def test_classify_risk_high_for_operational_paths(self):
|
||||
level = agent_pr_gate.classify_risk([
|
||||
'scripts/failover_monitor.py',
|
||||
'deploy/playbook.yml',
|
||||
])
|
||||
self.assertEqual(level, 'high')
|
||||
|
||||
def test_validate_pr_body_requires_issue_ref_and_verification(self):
|
||||
ok, details = agent_pr_gate.validate_pr_body(
|
||||
'feat: add thing',
|
||||
'What changed only\n\nNo verification section here.'
|
||||
)
|
||||
self.assertFalse(ok)
|
||||
self.assertIn('issue reference', ' '.join(details).lower())
|
||||
self.assertIn('verification', ' '.join(details).lower())
|
||||
|
||||
def test_validate_pr_body_accepts_issue_ref_and_verification(self):
|
||||
ok, details = agent_pr_gate.validate_pr_body(
|
||||
'feat: add thing (#562)',
|
||||
'Refs #562\n\nVerification:\n- pytest -q\n'
|
||||
)
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(details, [])
|
||||
|
||||
def test_build_comment_body_reports_failures_and_human_review(self):
|
||||
body = agent_pr_gate.build_comment_body(
|
||||
syntax_status='success',
|
||||
tests_status='failure',
|
||||
criteria_status='success',
|
||||
risk_level='high',
|
||||
)
|
||||
self.assertIn('tests', body.lower())
|
||||
self.assertIn('failure', body.lower())
|
||||
self.assertIn('human review', body.lower())
|
||||
|
||||
def test_changed_files_file_loader_ignores_blanks(self):
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as handle:
|
||||
handle.write('docs/one.md\n\nreports/two.md\n')
|
||||
path = handle.name
|
||||
try:
|
||||
files = agent_pr_gate.read_changed_files(path)
|
||||
finally:
|
||||
pathlib.Path(path).unlink(missing_ok=True)
|
||||
self.assertEqual(files, ['docs/one.md', 'reports/two.md'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,24 +0,0 @@
|
||||
import pathlib
|
||||
import unittest
|
||||
import yaml
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
WORKFLOW = ROOT / '.gitea' / 'workflows' / 'agent-pr-gate.yml'
|
||||
|
||||
|
||||
class TestAgentPrWorkflow(unittest.TestCase):
|
||||
def test_workflow_exists(self):
|
||||
self.assertTrue(WORKFLOW.exists(), 'agent-pr-gate workflow should exist')
|
||||
|
||||
def test_workflow_has_pr_gate_and_reporting_jobs(self):
|
||||
data = yaml.safe_load(WORKFLOW.read_text(encoding='utf-8'))
|
||||
self.assertIn('pull_request', data.get('on', {}))
|
||||
jobs = data.get('jobs', {})
|
||||
self.assertIn('gate', jobs)
|
||||
self.assertIn('report', jobs)
|
||||
report_steps = jobs['report']['steps']
|
||||
self.assertTrue(any('Auto-merge low-risk clean PRs' in (step.get('name') or '') for step in report_steps))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
419
the-door-GENOME.md
Normal file
419
the-door-GENOME.md
Normal file
@@ -0,0 +1,419 @@
|
||||
# GENOME.md — the-door
|
||||
|
||||
Generated: 2026-04-15 00:03:16 EDT
|
||||
Repo: Timmy_Foundation/the-door
|
||||
Issue: timmy-home #673
|
||||
|
||||
## Project Overview
|
||||
|
||||
The Door is a crisis-first front door to Timmy: one URL, no account wall, no app install, and a permanently visible 988 escape hatch. The repo combines a static browser UI, a local Hermes API gateway behind nginx, and a Python crisis package that duplicates and enriches the frontend's safety logic.
|
||||
|
||||
What the codebase actually contains today:
|
||||
- 1 primary browser app: `index.html`
|
||||
- 4 companion browser assets/pages: `about.html`, `testimony.html`, `crisis-offline.html`, `sw.js`
|
||||
- 17 Python files across canonical crisis logic, legacy shims, wrappers, and tests
|
||||
- 2 Gitea workflows: `smoke.yml`, `sanity.yml`
|
||||
- 1 systemd unit: `deploy/hermes-gateway.service`
|
||||
- full test suite currently passing: `115 passed, 3 subtests passed`
|
||||
|
||||
The repo is small, but it is not simple. The true architecture is a layered safety system:
|
||||
1. immediate browser-side crisis escalation
|
||||
2. OpenAI-compatible streaming chat through Hermes
|
||||
3. canonical Python crisis detection and response modules
|
||||
4. nginx hardening, rate limiting, and localhost-only gateway exposure
|
||||
5. service-worker offline fallback for crisis resources
|
||||
|
||||
The strongest pattern in this codebase is safety redundancy: the UI, prompt layer, offline fallback, and backend detection all try to catch the same sacred failure mode from different directions.
|
||||
|
||||
## Architecture
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
U[User in browser] --> I[index.html chat app]
|
||||
I --> K[Client-side crisis detection\ncrisisKeywords + explicitPhrases]
|
||||
K --> P[Inline crisis panel]
|
||||
K --> O[Fullscreen crisis overlay]
|
||||
I --> L[localStorage\nchat history + safety plan]
|
||||
I --> SW[sw.js service worker]
|
||||
SW --> OFF[crisis-offline.html]
|
||||
|
||||
I --> API[/POST /api/v1/chat/completions/]
|
||||
API --> NGINX[nginx reverse proxy]
|
||||
NGINX --> H[Hermes Gateway :8644]
|
||||
NGINX --> HC[/health proxy]
|
||||
|
||||
H --> G[crisis/gateway.py]
|
||||
G --> D[crisis/detect.py]
|
||||
G --> R[crisis/response.py]
|
||||
D --> CR[CrisisDetectionResult]
|
||||
R --> RESP[CrisisResponse]
|
||||
D --> LEG[Legacy shims\ncrisis_detector.py\ncrisis_responder.py\ndying_detection]
|
||||
|
||||
DEP[deploy/playbook.yml\ndeploy/deploy.sh\nhermes-gateway.service] --> NGINX
|
||||
DEP --> H
|
||||
CI[.gitea/workflows\nsmoke.yml + sanity.yml] --> I
|
||||
CI --> D
|
||||
```
|
||||
|
||||
## Entry Points
|
||||
|
||||
### Browser / user-facing entry points
|
||||
- `index.html`
|
||||
- the main product
|
||||
- contains inline CSS, inline JS, embedded `SYSTEM_PROMPT`, chat UI, crisis panel, fullscreen overlay, and safety-plan modal
|
||||
- `about.html`
|
||||
- static about page
|
||||
- linked from the chat footer, though the main app currently links to `/about` while the repo ships `about.html`
|
||||
- `testimony.html`
|
||||
- static companion content page
|
||||
- `crisis-offline.html`
|
||||
- offline crisis resource page served by the service worker when navigation cannot reach the network
|
||||
- `manifest.json`
|
||||
- PWA metadata and shortcuts, including `/?safetyplan=true` and `tel:988`
|
||||
- `sw.js`
|
||||
- network-first service worker with offline crisis fallback
|
||||
|
||||
### Backend / Python entry points
|
||||
- `crisis/detect.py`
|
||||
- canonical detection engine and public detection API
|
||||
- `crisis/response.py`
|
||||
- canonical response generator, UI flags, prompt modifier, grounding helpers
|
||||
- `crisis/gateway.py`
|
||||
- integration layer for `check_crisis()` and `get_system_prompt()`
|
||||
- `crisis/compassion_router.py`
|
||||
- profile-based prompt routing abstraction parallel to `response.py`
|
||||
- `crisis_detector.py`
|
||||
- root legacy shim exposing canonical detection in older shapes
|
||||
- `crisis_responder.py`
|
||||
- root legacy response module with a richer compatibility response contract
|
||||
- `dying_detection/__init__.py`
|
||||
- deprecated wrapper around canonical detection
|
||||
|
||||
### Operational entry points
|
||||
- `deploy/deploy.sh`
|
||||
- most complete one-command operational bootstrap path in the repo
|
||||
- `deploy/playbook.yml`
|
||||
- Ansible provisioning path for swap, packages, nginx, firewall, and site files
|
||||
- `deploy/hermes-gateway.service`
|
||||
- systemd unit running `hermes gateway --platform api_server --port 8644`
|
||||
- `.gitea/workflows/smoke.yml`
|
||||
- parse/syntax checks and secret scan
|
||||
- `.gitea/workflows/sanity.yml`
|
||||
- basic repo sanity grep checks for 988/system-prompt presence
|
||||
|
||||
## Data Flow
|
||||
|
||||
### Happy path: user message to streamed response
|
||||
1. User types into `#msg-input` in `index.html`.
|
||||
2. `sendMessage()`:
|
||||
- trims text
|
||||
- appends a user bubble to the DOM
|
||||
- pushes `{role: 'user', content: text}` into the in-memory `messages` array
|
||||
- runs client-side `checkCrisis(text)`
|
||||
- clears the input and starts streaming
|
||||
3. `streamResponse()` builds the request payload:
|
||||
- prepends a synthetic system message from `getSystemPrompt(lastUserMessage || '')`
|
||||
- posts JSON to `/api/v1/chat/completions`
|
||||
4. nginx proxies `/api/*` to `127.0.0.1:8644`.
|
||||
5. Hermes streams OpenAI-style SSE chunks back to the browser.
|
||||
6. The browser reads `choices[0].delta.content` and incrementally renders the assistant message.
|
||||
7. When streaming ends, the assistant turn is pushed into `messages`, saved to `localStorage`, and passed through `checkCrisis(fullText)` again.
|
||||
|
||||
### Immediate local crisis escalation path
|
||||
1. `checkCrisis(text)` scans substrings against two client-side lists.
|
||||
2. Low-tier/soft crisis text reveals the inline crisis panel.
|
||||
3. Explicit intent text triggers the fullscreen overlay and delayed-dismiss flow.
|
||||
4. The user still remains in the conversation flow rather than being hard-redirected away.
|
||||
|
||||
### Offline / failure path
|
||||
1. `sw.js` precaches static routes and the crisis fallback page.
|
||||
2. Navigation uses a network-first strategy with timeout fallback.
|
||||
3. If network and cache both fail, the service worker tries `crisis-offline.html`.
|
||||
4. If API streaming fails, `index.html` inserts a static emergency message with 988 and 741741 instead of a blank error.
|
||||
|
||||
## Key Abstractions
|
||||
|
||||
### 1. `SYSTEM_PROMPT`
|
||||
Embedded directly in `index.html`, not loaded at runtime from `system-prompt.txt`. The browser treats the prompt as part of the application runtime contract.
|
||||
|
||||
### 2. `COMPASSION_PROFILES`
|
||||
Frontend prompt-state profiles for `CRITICAL`, `HIGH`, `MEDIUM`, `LOW`, and `NONE`. They encode tone and directive shifts, but the current `levelMap` only maps browser levels to `NONE`, `MEDIUM`, and `CRITICAL`, leaving `HIGH` and `LOW` effectively unused in the main prompt-building path.
|
||||
|
||||
### 3. Client-side crisis detector
|
||||
In `index.html`, the browser uses:
|
||||
- `crisisKeywords` for panel escalation
|
||||
- `explicitPhrases` for hard overlay escalation
|
||||
- `checkCrisis(text)` for UI behavior
|
||||
- `getCrisisLevel(text)` for prompt shaping
|
||||
|
||||
This is fast and local, but it is also a separate detector from the canonical Python package.
|
||||
|
||||
### 4. `CrisisDetectionResult`
|
||||
The core canonical backend dataclass from `crisis/detect.py`:
|
||||
- `level`
|
||||
- `indicators`
|
||||
- `recommended_action`
|
||||
- `score`
|
||||
- `matches`
|
||||
|
||||
This is the canonical representation shared by the main Python crisis stack.
|
||||
|
||||
### 5. `CrisisResponse`
|
||||
In `crisis/response.py`, the canonical response dataclass ties backend detection to frontend/UI needs:
|
||||
- `timmy_message`
|
||||
- `show_crisis_panel`
|
||||
- `show_overlay`
|
||||
- `provide_988`
|
||||
- `escalate`
|
||||
|
||||
### 6. Legacy compatibility layer
|
||||
The repo still carries older interfaces:
|
||||
- `crisis_detector.py`
|
||||
- `crisis_responder.py`
|
||||
- `dying_detection/__init__.py`
|
||||
|
||||
These preserve compatibility, but they also create drift risk:
|
||||
- `MEDIUM` vs `MODERATE`
|
||||
- two different `CrisisResponse` contracts
|
||||
- two prompt-routing paths (`response.py` vs `compassion_router.py`)
|
||||
|
||||
### 7. Browser persistence contract
|
||||
`localStorage` is a real part of runtime state despite some docs claiming otherwise.
|
||||
Keys:
|
||||
- `timmy_chat_history`
|
||||
- `timmy_safety_plan`
|
||||
|
||||
That means The Door is not truly “close tab = gone” in its current implementation.
|
||||
|
||||
## API Surface
|
||||
|
||||
### Browser -> Hermes API contract
|
||||
`index.html` sends:
|
||||
|
||||
```json
|
||||
{
|
||||
"model": "timmy",
|
||||
"messages": [
|
||||
{"role": "system", "content": "...prompt..."},
|
||||
{"role": "assistant", "content": "..."},
|
||||
{"role": "user", "content": "..."}
|
||||
],
|
||||
"stream": true
|
||||
}
|
||||
```
|
||||
|
||||
Endpoint:
|
||||
- `/api/v1/chat/completions`
|
||||
|
||||
Expected response shape:
|
||||
- streaming SSE lines beginning with `data: `
|
||||
- chunk payloads with `choices[0].delta.content`
|
||||
- `[DONE]` terminator
|
||||
|
||||
### Canonical Python API
|
||||
- `crisis.detect.detect_crisis(text)`
|
||||
- `crisis.response.generate_response(detection)`
|
||||
- `crisis.response.process_message(text)`
|
||||
- `crisis.response.get_system_prompt_modifier(detection)`
|
||||
- `crisis.gateway.check_crisis(text)`
|
||||
- `crisis.gateway.get_system_prompt(base_prompt, text="")`
|
||||
- `crisis.gateway.format_gateway_response(text, pretty=True)`
|
||||
|
||||
### Legacy / compatibility API
|
||||
- `CrisisDetector.scan()`
|
||||
- `detect_crisis_legacy()`
|
||||
- root `crisis_responder.generate_response()`
|
||||
- deprecated `dying_detection.detect()` and helpers
|
||||
|
||||
## Test Coverage Gaps
|
||||
|
||||
### Current state
|
||||
Verified on fresh `main` clone of `the-door`:
|
||||
- `python3 -m pytest -q` -> `115 passed, 3 subtests passed`
|
||||
|
||||
What is already covered well:
|
||||
- canonical crisis detection tiers
|
||||
- response flags and gateway structure
|
||||
- many false-positive regressions
|
||||
- service-worker offline crisis fallback
|
||||
- crisis overlay focus trap string-level assertions
|
||||
- deprecated wrapper behavior
|
||||
|
||||
### High-value gaps that still matter
|
||||
1. No real browser test of the actual send path in `index.html`.
|
||||
- The repo currently contains a concrete scope bug:
|
||||
- `sendMessage()` defines `var lastUserMessage = text;`
|
||||
- `streamResponse()` later uses `getSystemPrompt(lastUserMessage || '')`
|
||||
- `lastUserMessage` is not in `streamResponse()` scope
|
||||
- Existing passing tests do not execute this real path.
|
||||
|
||||
2. No DOM-true test for overlay background locking.
|
||||
- The overlay code targets `document.querySelector('.app')` and `getElementById('chat')`.
|
||||
- The main document uses `id="app"`, not `.app`, and does not expose a `#chat` node.
|
||||
- Current tests assert code presence, not selector correctness.
|
||||
|
||||
3. No route validation for `/about` vs `about.html`.
|
||||
- The footer links to `/about`.
|
||||
- The repo ships `about.html`.
|
||||
- With current nginx `try_files`, this looks like a drift bug.
|
||||
|
||||
4. Legacy responder path remains largely untested.
|
||||
- `crisis_responder.py` is still present and meaningful but lacks direct tests for its richer response payloads.
|
||||
|
||||
5. CI does not run pytest.
|
||||
- The repo has a substantial suite, but Gitea workflows only do syntax/grep checks.
|
||||
|
||||
### Generated missing tests for critical paths
|
||||
These are the three most important tests this codebase still needs.
|
||||
|
||||
#### A. Browser send-path smoke test
|
||||
Goal: catch the `lastUserMessage` regression and ensure the chat request actually builds.
|
||||
|
||||
```python
|
||||
# Example Playwright/browser test
|
||||
async def test_send_message_builds_stream_request(page):
|
||||
await page.goto("file:///.../index.html")
|
||||
await page.fill("#msg-input", "hello")
|
||||
await page.click("#send-btn")
|
||||
# Expect no ReferenceError and one request to /api/v1/chat/completions
|
||||
```
|
||||
|
||||
#### B. Overlay selector correctness test
|
||||
Goal: prove the inert/background lock hits real DOM nodes, not dead selectors.
|
||||
|
||||
```python
|
||||
def test_overlay_background_selectors_match_real_dom():
|
||||
html = Path("index.html").read_text()
|
||||
assert 'id="app"' in html
|
||||
assert "querySelector('.app')" not in html
|
||||
assert "getElementById('chat')" not in html
|
||||
```
|
||||
|
||||
#### C. Legacy responder contract test
|
||||
Goal: keep compatibility layers honest until they are deleted.
|
||||
|
||||
```python
|
||||
from crisis_responder import process_message
|
||||
|
||||
def test_legacy_responder_returns_resources_for_high_risk():
|
||||
response = process_message("I want to kill myself")
|
||||
assert response.escalate is True
|
||||
assert response.show_overlay is True
|
||||
assert any("988" in r for r in response.resources)
|
||||
```
|
||||
|
||||
## Security Considerations
|
||||
|
||||
### Strengths
|
||||
- Browser message bubbles use `textContent`, not unsafe inner HTML, for chat content.
|
||||
- API calls are same-origin and proxied through nginx.
|
||||
- Service worker does not cache `/api/*` responses.
|
||||
- nginx includes CSP, HSTS, and localhost-only gateway exposure.
|
||||
- UFW/docs expect only `22`, `80`, and `443` to be public.
|
||||
- systemd unit hardening is present in `hermes-gateway.service`.
|
||||
|
||||
### Risks
|
||||
1. `localStorage` persistence contradicts the privacy story.
|
||||
- chat history and safety plan are stored in plaintext on the device
|
||||
- shared-device risk is real
|
||||
|
||||
2. `script-src 'unsafe-inline'` is required by the current architecture.
|
||||
- all runtime logic and CSS are inline in `index.html`
|
||||
- this weakens CSP/XSS posture
|
||||
|
||||
3. Safety enforcement is still heavily client-shaped.
|
||||
- the frontend always embeds the crisis-aware prompt
|
||||
- deployment does not clearly prove that all callers are forced through server-side crisis middleware
|
||||
- direct API clients may bypass browser-supplied context
|
||||
|
||||
4. Client and server detection logic can drift.
|
||||
- the browser uses substring lists
|
||||
- the backend uses canonical regex tiers in `crisis/detect.py`
|
||||
- parity is not tested
|
||||
|
||||
5. Deprecated wrapper emits a deterministic session hash.
|
||||
- `dying_detection` exposes a truncated SHA-256 fingerprint of text
|
||||
- useful for correlation, but still privacy-sensitive
|
||||
|
||||
## Dependencies
|
||||
|
||||
### Runtime
|
||||
- Hermes binary at `/usr/local/bin/hermes`
|
||||
- nginx
|
||||
- certbot + python certbot nginx plugin
|
||||
- ufw
|
||||
- curl
|
||||
- Python 3
|
||||
- browser with JavaScript, service-worker, and `localStorage` support
|
||||
|
||||
### Test / operator dependencies
|
||||
- pytest
|
||||
- PyYAML (used implicitly by smoke workflow checks)
|
||||
- ansible / ansible-playbook
|
||||
- rsync, ssh, scp
|
||||
- openssl
|
||||
- dig / dnsutils
|
||||
|
||||
### In-repo dependency style
|
||||
- Python code is effectively stdlib-first
|
||||
- no `requirements.txt`, `pyproject.toml`, or `package.json`
|
||||
- operational dependencies live mostly in docs and scripts rather than a declared manifest
|
||||
|
||||
## Deployment
|
||||
|
||||
### Intended production path
|
||||
Browser -> nginx TLS -> static webroot + `/api/*` reverse proxy -> Hermes on `127.0.0.1:8644`
|
||||
|
||||
### Main deployment commands
|
||||
- `make deploy`
|
||||
- `make deploy-bash`
|
||||
- `make push`
|
||||
- `make check`
|
||||
- `bash deploy/deploy.sh`
|
||||
- `cd deploy && ansible-playbook -i inventory.ini playbook.yml`
|
||||
|
||||
### Operational files
|
||||
- `deploy/nginx.conf`
|
||||
- `deploy/playbook.yml`
|
||||
- `deploy/deploy.sh`
|
||||
- `deploy/hermes-gateway.service`
|
||||
- `resilience/health-check.sh`
|
||||
- `resilience/service-restart.sh`
|
||||
|
||||
### Deployment reality check
|
||||
The repo's deploy surface is not fully coherent:
|
||||
- `deploy/deploy.sh` is the most complete operational path
|
||||
- `deploy/playbook.yml` provisions nginx/site/firewall/SSL but does not manage `hermes-gateway.service`
|
||||
- resilience scripts still target port `8000`, not the real gateway at `8644`
|
||||
- `crisis-offline.html` is required by `sw.js`, but full deploy paths do not appear to ship it consistently
|
||||
|
||||
## Technical Debt
|
||||
|
||||
### Highest-priority debt
|
||||
1. Fix the `lastUserMessage` scope bug in `index.html`.
|
||||
2. Fix overlay background selector drift (`.app` vs `#app`, missing `#chat`).
|
||||
3. Fix `/about` route drift.
|
||||
4. Add pytest to Gitea CI.
|
||||
5. Make deploy paths ship the same artifact set, including `crisis-offline.html`.
|
||||
6. Make the recommended Ansible path actually manage `hermes-gateway.service`.
|
||||
7. Align or remove resilience scripts targeting the wrong port/service.
|
||||
8. Resolve doc drift:
|
||||
- ARCHITECTURE says “close tab = gone,” but implementation uses `localStorage`
|
||||
- BACKEND_SETUP still says 49 tests, while current verified suite is 115 + 3 subtests
|
||||
- audit docs understate current automation coverage
|
||||
|
||||
### Strategic debt
|
||||
- Duplicate crisis logic across browser and backend
|
||||
- Parallel prompt-routing mechanisms (`response.py` and `compassion_router.py`)
|
||||
- Legacy compatibility layers that still matter but are not first-class tested
|
||||
- No declared dependency manifest for operator tooling
|
||||
- No true E2E browser validation of the core conversation loop
|
||||
|
||||
## Bottom Line
|
||||
|
||||
The Door is not just a static landing page. It is a small but layered safety system with three cores:
|
||||
- a browser-first crisis chat UI
|
||||
- a canonical Python crisis package
|
||||
- a thin nginx/Hermes deployment shell
|
||||
|
||||
Its design is morally serious and operationally pragmatic. Its main weaknesses are not missing ambition; they are drift, duplication, and shallow verification at the exact seams where the browser, backend, and deploy layer meet.
|
||||
Reference in New Issue
Block a user