Compare commits
1 Commits
burn/252-1
...
burn/255-1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc9d7705b6 |
@@ -1,128 +0,0 @@
|
||||
# Codebase Cleanup Report — 8 Subagents
|
||||
|
||||
**Date:** 2026-04-14
|
||||
**Target:** `~/repos/timmy/hermes-agent`
|
||||
**Scope:** Deduplication, type safety, dead code, circular deps, error handling, legacy code, AI slop
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| # | Task | Status | Impact |
|
||||
|---|------|--------|--------|
|
||||
| 1 | Deduplicate & consolidate | ✅ Committed | 6 files, shared helpers created |
|
||||
| 2 | Type consolidation | ✅ Complete | No duplicates found (types are clean) |
|
||||
| 3 | Dead code removal | ⚠️ Found but not persisted | 18 files with unused imports identified |
|
||||
| 4 | Circular dependencies | ⚠️ Found but not persisted | 11 cycles in tool_call_parsers, fix designed |
|
||||
| 5 | Weak types | ⚠️ Found but not persisted | 211 `Any` found, 9 should be replaced |
|
||||
| 6 | Error handling | ⚠️ Found but not persisted | 891 broad catches found, 178 should be tightened |
|
||||
| 7 | Legacy code | ⚠️ Found but not persisted | 71 lines of dead legacy identified |
|
||||
| 8 | AI slop cleanup | ⚠️ Found but not persisted | Most comments are legitimate, 7 lines of slop |
|
||||
|
||||
**Pushed to Gitea:** `burn/252-1776117800` — dedup commit pushed.
|
||||
|
||||
---
|
||||
|
||||
## What Was Committed
|
||||
|
||||
### Subagent 1: Deduplication — PUSHED ✅
|
||||
|
||||
Consolidated duplicate utility functions across platform adapters:
|
||||
|
||||
**Before:** `_coerce_list()`, `_coerce_bool()`, `_coerce_int()`, `_entry_matches()`, `_is_dm_allowed()`, `_is_group_allowed()` each duplicated in 2-5 files.
|
||||
|
||||
**After:** Single implementations in `gateway/platforms/helpers.py`, imported by all adapters.
|
||||
|
||||
**Files modified:**
|
||||
- `gateway/platforms/helpers.py` (+117 lines — new shared utilities)
|
||||
- `gateway/platforms/qqbot.py` (removed duplicates, imports from helpers)
|
||||
- `gateway/platforms/wecom.py` (removed duplicates, imports from helpers)
|
||||
- `gateway/platforms/weixin.py` (removed duplicates, delegates to helpers)
|
||||
- `gateway/platforms/feishu.py` (removed duplicates, imports from helpers)
|
||||
- `hermes_cli/uninstall.py` (removed duplicate `get_project_root`, imports from config)
|
||||
|
||||
**Tests:** qqbot 65 passed, wecom 32 passed, feishu 106 passed, gateway 1007 passed.
|
||||
|
||||
---
|
||||
|
||||
## What Was Found (Not Yet Applied)
|
||||
|
||||
The subagents ran in sandbox environments. Their analysis is accurate but the file changes didn't persist. Here's what they found — ready for manual application:
|
||||
|
||||
### Subagent 3: Dead Code — 18 files with unused imports
|
||||
|
||||
```
|
||||
mini_swe_runner.py: import sys, time, uuid, Path, Optional, Literal
|
||||
trajectory_compressor.py: Optional, Callable
|
||||
tools/qwen_crisis.py: Path, List, Optional
|
||||
environments/tool_call_parsers/kimi_k2_parser.py: uuid, Optional
|
||||
environments/tool_call_parsers/mistral_parser.py: uuid, Optional
|
||||
(+ 13 more files)
|
||||
```
|
||||
|
||||
**Action:** Run `pyflakes hermes-agent/ | grep 'imported but unused'` and remove.
|
||||
|
||||
### Subagent 4: Circular Dependencies — 11 cycles in tool_call_parsers
|
||||
|
||||
The `__init__.py` imports all sub-parsers, each sub-parsers imports back from `__init__.py`.
|
||||
|
||||
**Fix:** Create `environments/tool_call_parsers/_base.py` with `ToolCallParser`, `register_parser`, etc. Update `__init__.py` to re-export. Update all 11 sub-parsers to import from `_base`.
|
||||
|
||||
**Action:** Apply the fix described above.
|
||||
|
||||
### Subagent 5: Weak Types — 211 `Any` usages
|
||||
|
||||
9 should be replaced:
|
||||
- `gateway/stream_consumer.py`: `adapter: Any` → `BasePlatformAdapter`
|
||||
- `gateway/config.py`: `_coerce_bool(value: Any)` → `object`
|
||||
- `gateway/platforms/wecom.py`: `_parse_json(raw: Any)` → `str | bytes`
|
||||
- `agent/insights.py`: `provider: str = None` → `Optional[str] = None`
|
||||
- (+ 5 more)
|
||||
|
||||
**Action:** Replace the 9 identified weak types. Keep legitimate `Any` for JSON serialization.
|
||||
|
||||
### Subagent 6: Error Handling — 891 broad catches
|
||||
|
||||
178 should be tightened from `except Exception:` to specific types:
|
||||
- Config reads → `(KeyError, TypeError, ValueError, OSError, ImportError)`
|
||||
- Import fallbacks → `(ImportError, AttributeError)`
|
||||
- JSON/serialization → `(AttributeError, TypeError, ValueError)`
|
||||
- Network/HTTP → `(ConnectionError, TimeoutError, OSError)`
|
||||
- Filesystem → `(OSError, IOError)`
|
||||
|
||||
**Action:** Apply specific exception types to the 178 identified catches.
|
||||
|
||||
### Subagent 7: Legacy Code — 71 lines to remove
|
||||
|
||||
- `model_tools.py`: `_LEGACY_TOOLSET_MAP` (11 old toolset names)
|
||||
- `gateway/platforms/matrix.py`: pre-SQLite crypto store cleanup
|
||||
- Related tests
|
||||
|
||||
**Action:** Remove the legacy map and its tests.
|
||||
|
||||
### Subagent 8: AI Slop — 7 lines
|
||||
|
||||
- 4 test files: stale tombstone comments and commented-out code
|
||||
|
||||
**Action:** Remove the 7 identified lines.
|
||||
|
||||
---
|
||||
|
||||
## Recommended Next Steps
|
||||
|
||||
1. **Immediate:** Run `pyflakes` on the codebase and remove unused imports (10 min)
|
||||
2. **Quick win:** Apply the `_base.py` fix for circular imports (30 min)
|
||||
3. **Medium effort:** Replace the 9 weak types (20 min)
|
||||
4. **Larger effort:** Tighten the 178 error catches (2-3 hours)
|
||||
5. **Cleanup:** Remove legacy code and AI slop (15 min)
|
||||
|
||||
**Total estimated effort:** 4-5 hours of manual work to apply all findings.
|
||||
|
||||
---
|
||||
|
||||
## Risk Assessment
|
||||
|
||||
- All identified changes are safe (tests pass, no functional changes)
|
||||
- Error handling changes are the riskiest — need to verify specific exceptions don't break edge cases
|
||||
- Circular dependency fix is the highest value — breaks a real architectural problem
|
||||
- Dead code removal is the lowest risk — just removing unused imports
|
||||
@@ -37,31 +37,6 @@ from agent.memory_provider import MemoryProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# Correction detection patterns
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
_CORRECTION_PATTERNS = [
|
||||
re.compile(r'\b(?:no|wrong|incorrect|that\'s not right|that is not right)\b', re.IGNORECASE),
|
||||
re.compile(r'\b(?:actually|nope|not quite|that\'s wrong|that is wrong)\b', re.IGNORECASE),
|
||||
re.compile(r'\b(?:that\'s not|that is not|that was not|that\'s not what)\b', re.IGNORECASE),
|
||||
re.compile(r'\bi said|i told you|what i meant|what i said\b', re.IGNORECASE),
|
||||
re.compile(r'\bcorrection[:\s]|fix that|revise|undo\b', re.IGNORECASE),
|
||||
]
|
||||
|
||||
|
||||
def _detect_correction(user_content: str) -> bool:
|
||||
"""Detect if the user message is a correction of the previous assistant response."""
|
||||
if not user_content or len(user_content) < 3:
|
||||
return False
|
||||
# Must be short-ish to be a correction (not a new topic)
|
||||
if len(user_content) > 200:
|
||||
return False
|
||||
for pattern in _CORRECTION_PATTERNS:
|
||||
if pattern.search(user_content):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Context fencing helpers
|
||||
@@ -236,74 +211,6 @@ class MemoryManager:
|
||||
provider.name, e,
|
||||
)
|
||||
|
||||
def auto_calibrate_feedback(
|
||||
self,
|
||||
current_user_message: str,
|
||||
*,
|
||||
prev_assistant_response: str = "",
|
||||
session_id: str = "",
|
||||
) -> None:
|
||||
"""Auto-calibrate fact trust based on interaction outcome.
|
||||
|
||||
Called after sync_all(). If the user's current message is a correction
|
||||
of the previous assistant response, marks prefetched facts as unhelpful.
|
||||
If no correction detected, marks them as helpful.
|
||||
|
||||
This creates a passive feedback loop: facts that contribute to correct
|
||||
responses gain trust, facts that lead to corrections lose trust.
|
||||
"""
|
||||
is_correction = _detect_correction(current_user_message)
|
||||
|
||||
for provider in self._providers:
|
||||
try:
|
||||
fact_ids = provider.get_prefetched_fact_ids()
|
||||
except Exception:
|
||||
continue
|
||||
if not fact_ids:
|
||||
continue
|
||||
|
||||
for fact_id in fact_ids:
|
||||
try:
|
||||
provider.handle_tool_call(
|
||||
"fact_feedback",
|
||||
{
|
||||
"action": "unhelpful" if is_correction else "helpful",
|
||||
"fact_id": fact_id,
|
||||
},
|
||||
)
|
||||
logger.debug(
|
||||
"Auto-calibrate fact %d: %s (provider=%s)",
|
||||
fact_id,
|
||||
"unhelpful" if is_correction else "helpful",
|
||||
provider.name,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(
|
||||
"Auto-calibrate fact %d failed (provider=%s): %s",
|
||||
fact_id, provider.name, e,
|
||||
)
|
||||
|
||||
def get_pruning_candidates(self, threshold: float = 0.15) -> List[Dict[str, Any]]:
|
||||
"""Return facts below the trust threshold that are candidates for pruning.
|
||||
|
||||
This is a read-only query — no facts are deleted. The caller decides
|
||||
whether to remove them (e.g. during on_session_end or periodic hygiene).
|
||||
"""
|
||||
candidates = []
|
||||
for provider in self._providers:
|
||||
try:
|
||||
result = provider.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0, "limit": 100},
|
||||
)
|
||||
data = json.loads(result)
|
||||
for fact in data.get("facts", []):
|
||||
if fact.get("trust_score", 0.5) < threshold:
|
||||
candidates.append(fact)
|
||||
except Exception:
|
||||
continue
|
||||
return candidates
|
||||
|
||||
# -- Tools ---------------------------------------------------------------
|
||||
|
||||
def get_all_tool_schemas(self) -> List[Dict[str, Any]]:
|
||||
|
||||
@@ -220,15 +220,6 @@ class MemoryProvider(ABC):
|
||||
should all have ``env_var`` set and this method stays no-op).
|
||||
"""
|
||||
|
||||
def get_prefetched_fact_ids(self) -> List[int]:
|
||||
"""Return fact IDs recalled by the last prefetch() call.
|
||||
|
||||
Override this to enable automatic trust calibration: facts used in
|
||||
successful interactions gain trust, facts that lead to corrections
|
||||
lose trust. Default returns empty list (no auto-calibration).
|
||||
"""
|
||||
return []
|
||||
|
||||
def on_memory_write(self, action: str, target: str, content: str) -> None:
|
||||
"""Called when the built-in memory tool writes an entry.
|
||||
|
||||
|
||||
9
deploy/synapse/.gitignore
vendored
Normal file
9
deploy/synapse/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Secrets — never commit
|
||||
.env
|
||||
synapse-credentials.env
|
||||
|
||||
# Backups
|
||||
backups/
|
||||
|
||||
# Generated config backups
|
||||
homeserver.yaml.bak
|
||||
82
deploy/synapse/docker-compose.yml
Normal file
82
deploy/synapse/docker-compose.yml
Normal file
@@ -0,0 +1,82 @@
|
||||
# Synapse Homeserver — Docker Compose Stack
|
||||
# Matrix Phase 1: Deploy Synapse on Ezra VPS
|
||||
#
|
||||
# Usage:
|
||||
# cd deploy/synapse
|
||||
# ./setup.sh # first-time deploy (generates config + keys)
|
||||
# docker compose up -d # start
|
||||
# docker compose logs -f # follow logs
|
||||
# docker compose down # stop
|
||||
#
|
||||
# Secrets:
|
||||
# Never commit .env to version control.
|
||||
# setup.sh generates secrets automatically.
|
||||
|
||||
services:
|
||||
synapse-db:
|
||||
image: postgres:16-alpine
|
||||
container_name: synapse-db
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- synapse_db:/var/lib/postgresql/data
|
||||
environment:
|
||||
POSTGRES_USER: synapse
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
|
||||
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --lc-collate=C --lc-ctype=C"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U synapse"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
networks:
|
||||
- synapse_net
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "20m"
|
||||
max-file: "3"
|
||||
|
||||
synapse:
|
||||
image: matrixdotorg/synapse:latest
|
||||
container_name: synapse
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
synapse-db:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- synapse_data:/data
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
SYNAPSE_CONFIG_PATH: /data/homeserver.yaml
|
||||
ports:
|
||||
- "127.0.0.1:8008:8008" # Client-server API (localhost only)
|
||||
- "8448:8448" # Federation (public)
|
||||
networks:
|
||||
- synapse_net
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fSs", "http://localhost:8008/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "50m"
|
||||
max-file: "5"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "2.0"
|
||||
memory: 2G
|
||||
reservations:
|
||||
memory: 512M
|
||||
|
||||
volumes:
|
||||
synapse_data:
|
||||
synapse_db:
|
||||
|
||||
networks:
|
||||
synapse_net:
|
||||
driver: bridge
|
||||
101
deploy/synapse/homeserver.yaml
Normal file
101
deploy/synapse/homeserver.yaml
Normal file
@@ -0,0 +1,101 @@
|
||||
# Synapse Homeserver Configuration
|
||||
# Generated by setup.sh — edit with care.
|
||||
#
|
||||
# Docs: https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html
|
||||
|
||||
# Server name — your Matrix domain (e.g. matrix.example.com)
|
||||
server_name: "SERVER_NAME_PLACEHOLDER"
|
||||
|
||||
# Signing key — generated by setup.sh
|
||||
signing_key_path: "/data/signing.key"
|
||||
|
||||
# Trusted key servers (empty = trust only ourselves for our own keys)
|
||||
trusted_key_servers: []
|
||||
|
||||
# Report stats to matrix.org (no for sovereignty)
|
||||
report_stats: false
|
||||
|
||||
# Listeners
|
||||
listeners:
|
||||
- port: 8008
|
||||
tls: false
|
||||
type: http
|
||||
x_forwarded: true
|
||||
resources:
|
||||
- names: [client, federation]
|
||||
compress: false
|
||||
|
||||
# Database — PostgreSQL
|
||||
database:
|
||||
name: psycopg2
|
||||
args:
|
||||
user: synapse
|
||||
password: "${POSTGRES_PASSWORD}"
|
||||
database: synapse
|
||||
host: synapse-db
|
||||
cp_min: 5
|
||||
cp_max: 10
|
||||
|
||||
# Media store
|
||||
media_store_path: "/data/media_store"
|
||||
|
||||
# Upload limits
|
||||
max_upload_size: "50M"
|
||||
|
||||
# URL previews (disable to reduce attack surface)
|
||||
url_preview_enabled: false
|
||||
|
||||
# Enable room list publishing
|
||||
enable_room_list_search: true
|
||||
|
||||
# Turn off public registration by default (create users via admin API)
|
||||
enable_registration: false
|
||||
enable_registration_without_verification: false
|
||||
|
||||
# Rate limiting
|
||||
rc_message:
|
||||
per_second: 0.2
|
||||
burst_count: 10
|
||||
|
||||
rc_registration:
|
||||
per_second: 0.1
|
||||
burst_count: 3
|
||||
|
||||
rc_login:
|
||||
address:
|
||||
per_second: 0.05
|
||||
burst_count: 2
|
||||
account:
|
||||
per_second: 0.05
|
||||
burst_count: 2
|
||||
failed_attempts:
|
||||
per_second: 0.15
|
||||
burst_count: 3
|
||||
|
||||
# Retention — keep messages for 90 days by default
|
||||
retention:
|
||||
enabled: true
|
||||
default_policy:
|
||||
min_lifetime: 1d
|
||||
max_lifetime: 90d
|
||||
|
||||
# Logging
|
||||
log_config: "/data/log.config"
|
||||
|
||||
# Metrics (optional — enable if running Prometheus)
|
||||
enable_metrics: false
|
||||
|
||||
# Presence
|
||||
use_presence: true
|
||||
|
||||
# Federation
|
||||
federation_verify_certificates: true
|
||||
federation_sender_instances: 1
|
||||
|
||||
# Appservice config directory
|
||||
app_service_config_files: []
|
||||
|
||||
# Experimental features
|
||||
experimental_features:
|
||||
# MSC3440: Threading support
|
||||
msc3440_enabled: true
|
||||
33
deploy/synapse/log.config
Normal file
33
deploy/synapse/log.config
Normal file
@@ -0,0 +1,33 @@
|
||||
# Synapse logging configuration
|
||||
# https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html#log_config
|
||||
|
||||
version: 1
|
||||
|
||||
formatters:
|
||||
precise:
|
||||
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
|
||||
|
||||
handlers:
|
||||
console:
|
||||
class: logging.StreamHandler
|
||||
formatter: precise
|
||||
level: INFO
|
||||
stream: ext://sys.stdout
|
||||
|
||||
file:
|
||||
class: logging.handlers.RotatingFileHandler
|
||||
formatter: precise
|
||||
filename: /data/homeserver.log
|
||||
maxBytes: 104857600 # 100MB
|
||||
backupCount: 3
|
||||
level: INFO
|
||||
|
||||
loggers:
|
||||
synapse.storage.SQL:
|
||||
level: WARNING
|
||||
synapse.http.client:
|
||||
level: INFO
|
||||
|
||||
root:
|
||||
level: INFO
|
||||
handlers: [console, file]
|
||||
131
deploy/synapse/manage.sh
Executable file
131
deploy/synapse/manage.sh
Executable file
@@ -0,0 +1,131 @@
|
||||
#!/usr/bin/env bash
|
||||
# Synapse Homeserver — Management Utilities
|
||||
# Usage: ./manage.sh <command>
|
||||
#
|
||||
# Commands:
|
||||
# status Show container status and health
|
||||
# restart Restart Synapse (preserves data)
|
||||
# logs Tail Synapse logs
|
||||
# create-user <username> <password> [admin]
|
||||
# backup Create timestamped backup of data volumes
|
||||
# update Pull latest Synapse image and recreate
|
||||
# teardown Stop and remove everything (DESTRUCTIVE)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
CYAN='\033[0;36m'
|
||||
NC='\033[0m'
|
||||
|
||||
info() { echo -e "${GREEN}[MANAGE]${NC} $*"; }
|
||||
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
|
||||
COMMAND="${1:-help}"
|
||||
|
||||
case "$COMMAND" in
|
||||
status)
|
||||
info "Container status:"
|
||||
docker compose ps
|
||||
echo ""
|
||||
info "Synapse health:"
|
||||
curl -sfS http://127.0.0.1:8008/health && echo "" || echo "Not responding"
|
||||
echo ""
|
||||
info "Disk usage:"
|
||||
docker system df -v 2>/dev/null | grep -E "synapse|VOLUME" || true
|
||||
;;
|
||||
|
||||
restart)
|
||||
info "Restarting Synapse..."
|
||||
docker compose restart synapse
|
||||
info "Waiting for health check..."
|
||||
sleep 5
|
||||
curl -sfS http://127.0.0.1:8008/health && echo "" && info "Synapse is healthy" || warn "Not responding yet"
|
||||
;;
|
||||
|
||||
logs)
|
||||
shift
|
||||
LINES="${1:-100}"
|
||||
info "Tailing Synapse logs (last $LINES lines)..."
|
||||
docker compose logs -f --tail="$LINES" synapse
|
||||
;;
|
||||
|
||||
create-user)
|
||||
USERNAME="${2:?Usage: manage.sh create-user <username> <password> [admin]}"
|
||||
PASSWORD="${3:?Usage: manage.sh create-user <username> <password> [admin]}"
|
||||
IS_ADMIN="${4:-false}"
|
||||
info "Creating user @$USERNAME..."
|
||||
ADMIN_FLAG=""
|
||||
if [ "$IS_ADMIN" = "admin" ] || [ "$IS_ADMIN" = "true" ]; then
|
||||
ADMIN_FLAG="--admin"
|
||||
fi
|
||||
docker compose exec -T synapse register_new_matrix_user \
|
||||
http://localhost:8008 \
|
||||
-c /data/homeserver.yaml \
|
||||
-u "$USERNAME" \
|
||||
-p "$PASSWORD" \
|
||||
$ADMIN_FLAG \
|
||||
--no-extra-prompt
|
||||
;;
|
||||
|
||||
backup)
|
||||
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
||||
BACKUP_DIR="./backups/${TIMESTAMP}"
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
info "Backing up PostgreSQL..."
|
||||
docker compose exec -T synapse-db pg_dump -U synapse > "${BACKUP_DIR}/synapse_db.sql"
|
||||
info "Backing up Synapse data volume..."
|
||||
docker run --rm \
|
||||
-v synapse_data:/source:ro \
|
||||
-v "$(pwd)/${BACKUP_DIR}:/backup" \
|
||||
alpine tar czf /backup/synapse_data.tar.gz -C /source .
|
||||
info "Backup complete: $BACKUP_DIR"
|
||||
ls -lh "$BACKUP_DIR"
|
||||
;;
|
||||
|
||||
update)
|
||||
info "Pulling latest Synapse image..."
|
||||
docker compose pull synapse
|
||||
info "Recreating containers..."
|
||||
docker compose up -d --force-recreate synapse
|
||||
info "Waiting for health..."
|
||||
sleep 10
|
||||
curl -sfS http://127.0.0.1:8008/health && echo "" && info "Updated and healthy" || warn "Check logs"
|
||||
;;
|
||||
|
||||
teardown)
|
||||
echo -e "${RED}WARNING: This will stop and remove all Synapse containers and volumes.${NC}"
|
||||
echo -e "${RED}ALL DATA WILL BE LOST. This cannot be undone.${NC}"
|
||||
echo ""
|
||||
read -p "Type 'yes-delete-everything' to confirm: " CONFIRM
|
||||
if [ "$CONFIRM" = "yes-delete-everything" ]; then
|
||||
info "Stopping containers..."
|
||||
docker compose down -v
|
||||
info "Removing volumes..."
|
||||
docker volume rm synapse_data synapse_db 2>/dev/null || true
|
||||
info "Teardown complete."
|
||||
else
|
||||
info "Aborted."
|
||||
fi
|
||||
;;
|
||||
|
||||
help|*)
|
||||
echo "Synapse Homeserver Management"
|
||||
echo ""
|
||||
echo "Usage: ./manage.sh <command>"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " status Show container status and health"
|
||||
echo " restart Restart Synapse"
|
||||
echo " logs [lines] Tail Synapse logs (default: 100)"
|
||||
echo " create-user <u> <p> [admin] Create a new Matrix user"
|
||||
echo " backup Backup database + data volume"
|
||||
echo " update Pull latest image and recreate"
|
||||
echo " teardown Stop and remove everything (DESTRUCTIVE)"
|
||||
;;
|
||||
esac
|
||||
211
deploy/synapse/setup.sh
Executable file
211
deploy/synapse/setup.sh
Executable file
@@ -0,0 +1,211 @@
|
||||
#!/usr/bin/env bash
|
||||
# Synapse Homeserver — One-Shot Setup Script
|
||||
# Matrix Phase 1: Deploy Synapse on Ezra VPS
|
||||
#
|
||||
# Usage:
|
||||
# ./setup.sh <server_name> [admin_user] [admin_password]
|
||||
#
|
||||
# Example:
|
||||
# ./setup.sh matrix.timmy-time.xyz hermes-bot 'secure-pass-123'
|
||||
#
|
||||
# What it does:
|
||||
# 1. Generates .env with secrets
|
||||
# 2. Prepares homeserver.yaml with correct server name
|
||||
# 3. Generates signing key
|
||||
# 4. Starts Synapse + PostgreSQL via Docker Compose
|
||||
# 5. Waits for Synapse to be healthy
|
||||
# 6. Registers admin user + bot account
|
||||
# 7. Outputs Matrix credentials for hermes-agent
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
# --- Colors ---
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
CYAN='\033[0;36m'
|
||||
NC='\033[0m'
|
||||
|
||||
info() { echo -e "${GREEN}[SETUP]${NC} $*"; }
|
||||
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
||||
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
|
||||
# --- Args ---
|
||||
SERVER_NAME="${1:?Usage: $0 <server_name> [admin_user] [admin_password]}"
|
||||
ADMIN_USER="${2:-timmy-admin}"
|
||||
ADMIN_PASS="${3:-$(openssl rand -hex 16)}"
|
||||
BOT_USER="${4:-hermes-bot}"
|
||||
BOT_PASS="${5:-$(openssl rand -hex 16)}"
|
||||
|
||||
echo -e "${CYAN}"
|
||||
echo "╔══════════════════════════════════════════════════╗"
|
||||
echo "║ Synapse Homeserver — Matrix Phase 1 Deploy ║"
|
||||
echo "╚══════════════════════════════════════════════════╝"
|
||||
echo -e "${NC}"
|
||||
info "Server name: $SERVER_NAME"
|
||||
info "Admin user: @$ADMIN_USER:$SERVER_NAME"
|
||||
info "Bot user: @$BOT_USER:$SERVER_NAME"
|
||||
echo ""
|
||||
|
||||
# --- Preflight ---
|
||||
info "Preflight checks..."
|
||||
command -v docker >/dev/null 2>&1 || error "docker not found. Install Docker first."
|
||||
command -v docker compose version >/dev/null 2>&1 || error "docker compose not found. Install Docker Compose plugin."
|
||||
info "Docker: $(docker --version | head -1)"
|
||||
info "Compose: $(docker compose version | head -1)"
|
||||
|
||||
# --- Generate .env ---
|
||||
info "Generating .env..."
|
||||
POSTGRES_PASSWORD=$(openssl rand -hex 24)
|
||||
REGISTRATION_SECRET=$(openssl rand -hex 16)
|
||||
|
||||
cat > .env <<EOF
|
||||
# Synapse deployment — generated $(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
# DO NOT COMMIT THIS FILE
|
||||
|
||||
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
|
||||
SYNAPSE_SERVER_NAME=${SERVER_NAME}
|
||||
SYNAPSE_REPORT_STATS=no
|
||||
REGISTRATION_SECRET=${REGISTRATION_SECRET}
|
||||
EOF
|
||||
chmod 600 .env
|
||||
info ".env written with secure permissions"
|
||||
|
||||
# --- Prepare homeserver.yaml ---
|
||||
info "Preparing homeserver.yaml..."
|
||||
sed -i.bak "s/SERVER_NAME_PLACEHOLDER/${SERVER_NAME}/g" homeserver.yaml
|
||||
rm -f homeserver.yaml.bak
|
||||
info "Server name set to: $SERVER_NAME"
|
||||
|
||||
# --- Generate signing key ---
|
||||
info "Generating signing key..."
|
||||
# Synapse will generate its own key on first run if missing
|
||||
# But we pre-create the data volume structure
|
||||
docker volume create synapse_data >/dev/null 2>&1 || true
|
||||
docker volume create synapse_db >/dev/null 2>&1 || true
|
||||
|
||||
# --- Start the stack ---
|
||||
info "Starting Synapse + PostgreSQL..."
|
||||
docker compose up -d
|
||||
|
||||
# --- Wait for Synapse to be healthy ---
|
||||
info "Waiting for Synapse to start (up to 120s)..."
|
||||
MAX_WAIT=120
|
||||
ELAPSED=0
|
||||
while [ $ELAPSED -lt $MAX_WAIT ]; do
|
||||
if curl -sfS http://127.0.0.1:8008/health >/dev/null 2>&1; then
|
||||
info "Synapse is healthy!"
|
||||
break
|
||||
fi
|
||||
sleep 3
|
||||
ELAPSED=$((ELAPSED + 3))
|
||||
if [ $((ELAPSED % 15)) -eq 0 ]; then
|
||||
info "Still waiting... (${ELAPSED}s)"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $ELAPSED -ge $MAX_WAIT ]; then
|
||||
warn "Synapse did not respond within ${MAX_WAIT}s. Check logs:"
|
||||
echo " docker compose logs synapse"
|
||||
error "Aborting registration."
|
||||
fi
|
||||
|
||||
# --- Register admin user ---
|
||||
info "Registering admin user @$ADMIN_USER:$SERVER_NAME..."
|
||||
docker compose exec -T synapse register_new_matrix_user \
|
||||
http://localhost:8008 \
|
||||
-c /data/homeserver.yaml \
|
||||
-u "$ADMIN_USER" \
|
||||
-p "$ADMIN_PASS" \
|
||||
--admin \
|
||||
--no-extra-prompt 2>&1 || {
|
||||
# User might already exist if re-running
|
||||
warn "Admin user registration returned non-zero (may already exist)"
|
||||
}
|
||||
|
||||
# --- Register bot user ---
|
||||
info "Registering bot user @$BOT_USER:$SERVER_NAME..."
|
||||
docker compose exec -T synapse register_new_matrix_user \
|
||||
http://localhost:8008 \
|
||||
-c /data/homeserver.yaml \
|
||||
-u "$BOT_USER" \
|
||||
-p "$BOT_PASS" \
|
||||
--no-admin \
|
||||
--no-extra-prompt 2>&1 || {
|
||||
warn "Bot user registration returned non-zero (may already exist)"
|
||||
}
|
||||
|
||||
# --- Get bot access token ---
|
||||
info "Acquiring bot access token..."
|
||||
BOT_TOKEN_RESPONSE=$(curl -sfS -X POST "http://127.0.0.1:8008/_matrix/client/v3/login" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{
|
||||
\"type\": \"m.login.password\",
|
||||
\"identifier\": {
|
||||
\"type\": \"m.id.user\",
|
||||
\"user\": \"${BOT_USER}\"
|
||||
},
|
||||
\"password\": \"${BOT_PASS}\",
|
||||
\"device_name\": \"Hermes Agent\"
|
||||
}")
|
||||
|
||||
BOT_ACCESS_TOKEN=$(echo "$BOT_TOKEN_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['access_token'])" 2>/dev/null || echo "FAILED_TO_EXTRACT")
|
||||
BOT_DEVICE_ID=$(echo "$BOT_TOKEN_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['device_id'])" 2>/dev/null || echo "UNKNOWN")
|
||||
|
||||
if [ "$BOT_ACCESS_TOKEN" = "FAILED_TO_EXTRACT" ]; then
|
||||
warn "Could not extract bot access token automatically."
|
||||
warn "Login manually: curl -X POST http://127.0.0.1:8008/_matrix/client/v3/login ..."
|
||||
fi
|
||||
|
||||
# --- Write credentials file ---
|
||||
CREDENTIALS_FILE="synapse-credentials.env"
|
||||
cat > "$CREDENTIALS_FILE" <<EOF
|
||||
# Synapse Credentials — generated $(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
# Add these to hermes-agent's ~/.hermes/.env
|
||||
|
||||
# Matrix integration
|
||||
MATRIX_HOMESERVER=http://${SERVER_NAME}:8008
|
||||
MATRIX_ACCESS_TOKEN=${BOT_ACCESS_TOKEN}
|
||||
MATRIX_USER_ID=@${BOT_USER}:${SERVER_NAME}
|
||||
MATRIX_DEVICE_ID=${BOT_DEVICE_ID}
|
||||
MATRIX_ENCRYPTION=true
|
||||
|
||||
# Admin credentials (for user management)
|
||||
SYNAPSE_ADMIN_USER=@${ADMIN_USER}:${SERVER_NAME}
|
||||
SYNAPSE_ADMIN_PASSWORD=${ADMIN_PASS}
|
||||
|
||||
# Bot credentials
|
||||
SYNAPSE_BOT_USER=@${BOT_USER}:${SERVER_NAME}
|
||||
SYNAPSE_BOT_PASSWORD=${BOT_PASS}
|
||||
EOF
|
||||
chmod 600 "$CREDENTIALS_FILE"
|
||||
info "Credentials written to: $CREDENTIALS_FILE"
|
||||
|
||||
# --- Summary ---
|
||||
echo ""
|
||||
echo -e "${GREEN}╔══════════════════════════════════════════════════╗${NC}"
|
||||
echo -e "${GREEN}║ Synapse Deployed Successfully! ║${NC}"
|
||||
echo -e "${GREEN}╚══════════════════════════════════════════════════╝${NC}"
|
||||
echo ""
|
||||
echo -e " Server: ${CYAN}https://${SERVER_NAME}${NC}"
|
||||
echo -e " Client API: ${CYAN}http://127.0.0.1:8008${NC}"
|
||||
echo -e " Federation: ${CYAN}https://${SERVER_NAME}:8448${NC}"
|
||||
echo ""
|
||||
echo -e " Admin: ${YELLOW}@${ADMIN_USER}:${SERVER_NAME}${NC}"
|
||||
echo -e " Bot: ${YELLOW}@${BOT_USER}:${SERVER_NAME}${NC}"
|
||||
echo -e " Bot Token: ${YELLOW}${BOT_ACCESS_TOKEN:0:20}...${NC}"
|
||||
echo ""
|
||||
echo -e " Credentials: ${CYAN}${SCRIPT_DIR}/${CREDENTIALS_FILE}${NC}"
|
||||
echo ""
|
||||
echo -e "${GREEN}Next steps:${NC}"
|
||||
echo " 1. Point DNS: ${SERVER_NAME} → $(curl -s ifconfig.me 2>/dev/null || echo '<VPS_IP>')"
|
||||
echo " 2. Set up TLS: nginx/certbot reverse proxy for :8008 and :8448"
|
||||
echo " 3. Copy credentials to hermes-agent: cp ${CREDENTIALS_FILE} ~/.hermes/.env"
|
||||
echo " 4. Start hermes: hermes gateway --platform matrix"
|
||||
echo ""
|
||||
echo " Manage: docker compose logs -f | docker compose restart | docker compose down"
|
||||
echo " Users: docker compose exec synapse register_new_matrix_user http://localhost:8008 -c /data/homeserver.yaml -u <user> -p <pass>"
|
||||
echo ""
|
||||
251
docs/synapse-deployment.md
Normal file
251
docs/synapse-deployment.md
Normal file
@@ -0,0 +1,251 @@
|
||||
# Synapse Homeserver Deployment Guide
|
||||
|
||||
## Matrix Phase 1: Deploy Synapse on Ezra VPS
|
||||
|
||||
Part of [Epic #269: Matrix Integration — Sovereign Messaging for Timmy](https://forge.alexanderwhitestone.com/Timmy_Foundation/hermes-agent/issues/269).
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────┐
|
||||
│ Ezra VPS (143.198.27.163) │
|
||||
│ │
|
||||
│ ┌──────────┐ ┌─────────────────────────┐ │
|
||||
│ │ Nginx │────▶│ Synapse (Docker) │ │
|
||||
│ │ :443→8008│ │ Client API: localhost:8008│ │
|
||||
│ │ :8448→8448│ │ Federation: 0.0.0.0:8448│ │
|
||||
│ └──────────┘ └──────────┬──────────────┘ │
|
||||
│ │ │
|
||||
│ ┌────────▼──────────┐ │
|
||||
│ │ PostgreSQL 16 │ │
|
||||
│ │ (Docker volume) │ │
|
||||
│ └───────────────────┘ │
|
||||
│ │
|
||||
│ ┌──────────────────────────────────────────┐ │
|
||||
│ │ hermes-agent (gateway) │ │
|
||||
│ │ MATRIX_HOMESERVER=http://localhost:8008 │ │
|
||||
│ └──────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Docker + Docker Compose plugin on Ezra VPS
|
||||
- SSH access: `ssh root@143.198.27.163`
|
||||
- DNS A record pointing to the VPS IP
|
||||
- (Recommended) Nginx + Certbot for TLS termination
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# SSH into Ezra
|
||||
ssh root@143.198.27.163
|
||||
|
||||
# Clone hermes-agent (if not present)
|
||||
cd /root
|
||||
git clone https://forge.alexanderwhitestone.com/Timmy_Foundation/hermes-agent.git
|
||||
cd hermes-agent/deploy/synapse
|
||||
|
||||
# Deploy Synapse
|
||||
chmod +x setup.sh
|
||||
./setup.sh matrix.timmy-time.xyz
|
||||
|
||||
# This will:
|
||||
# 1. Generate .env with database password
|
||||
# 2. Prepare homeserver.yaml
|
||||
# 3. Start Synapse + PostgreSQL via Docker Compose
|
||||
# 4. Wait for health
|
||||
# 5. Register admin + bot accounts
|
||||
# 6. Acquire bot access token
|
||||
# 7. Write synapse-credentials.env
|
||||
```
|
||||
|
||||
## Step-by-Step
|
||||
|
||||
### 1. DNS Configuration
|
||||
|
||||
Point your Matrix domain to Ezra's IP:
|
||||
|
||||
```
|
||||
Type Name Value
|
||||
A matrix 143.198.27.163
|
||||
```
|
||||
|
||||
Federation uses SRV records for port discovery, but direct `:8448` works without them.
|
||||
|
||||
### 2. Deploy Synapse
|
||||
|
||||
```bash
|
||||
cd /root/hermes-agent/deploy/synapse
|
||||
./setup.sh matrix.timmy-time.xyz hermes-bot 'your-secure-password'
|
||||
```
|
||||
|
||||
Arguments:
|
||||
| Arg | Default | Description |
|
||||
|-----|---------|-------------|
|
||||
| `server_name` | (required) | Matrix domain (e.g., `matrix.timmy-time.xyz`) |
|
||||
| `admin_user` | `timmy-admin` | Admin account username |
|
||||
| `admin_password` | (random) | Admin account password |
|
||||
| `bot_user` | `hermes-bot` | Bot account username |
|
||||
| `bot_password` | (random) | Bot account password |
|
||||
|
||||
### 3. TLS Termination (Nginx)
|
||||
|
||||
Install Nginx + Certbot:
|
||||
|
||||
```bash
|
||||
apt install -y nginx certbot python3-certbot-nginx
|
||||
|
||||
# Client-server API
|
||||
cat > /etc/nginx/sites-available/matrix <<'EOF'
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name matrix.timmy-time.xyz;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/matrix.timmy-time.xyz/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/matrix.timmy-time.xyz/privkey.pem;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8008;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
client_max_body_size 50M;
|
||||
}
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8448 ssl http2;
|
||||
server_name matrix.timmy-time.xyz;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/matrix.timmy-time.xyz/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/matrix.timmy-time.xyz/privkey.pem;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8008;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
ln -sf /etc/nginx/sites-available/matrix /etc/nginx/sites-enabled/
|
||||
nginx -t && systemctl reload nginx
|
||||
|
||||
# Get cert
|
||||
certbot --nginx -d matrix.timmy-time.xyz
|
||||
```
|
||||
|
||||
### 4. Wire Hermes Agent
|
||||
|
||||
Copy the generated credentials to hermes-agent's environment:
|
||||
|
||||
```bash
|
||||
# From synapse-credentials.env, add to ~/.hermes/.env:
|
||||
MATRIX_HOMESERVER=https://matrix.timmy-time.xyz
|
||||
MATRIX_ACCESS_TOKEN=<from synapse-credentials.env>
|
||||
MATRIX_USER_ID=@hermes-bot:matrix.timmy-time.xyz
|
||||
MATRIX_DEVICE_ID=<from synapse-credentials.env>
|
||||
MATRIX_ENCRYPTION=true
|
||||
```
|
||||
|
||||
Then start the gateway:
|
||||
|
||||
```bash
|
||||
hermes gateway --platform matrix
|
||||
```
|
||||
|
||||
### 5. Verify
|
||||
|
||||
```bash
|
||||
# Check Synapse health
|
||||
curl -s https://matrix.timmy-time.xyz/_matrix/client/versions
|
||||
|
||||
# Check federation
|
||||
curl -s https://matrix.timmy-time.xyz:8448/_matrix/federation/v1/version
|
||||
|
||||
# Check bot is connected
|
||||
# (should appear online in Element or any Matrix client)
|
||||
```
|
||||
|
||||
## Management
|
||||
|
||||
Use the management script for day-to-day operations:
|
||||
|
||||
```bash
|
||||
cd /root/hermes-agent/deploy/synapse
|
||||
|
||||
./manage.sh status # container health
|
||||
./manage.sh logs # tail logs
|
||||
./manage.sh restart # restart Synapse
|
||||
./manage.sh backup # backup DB + data
|
||||
./manage.sh update # pull latest image
|
||||
./manage.sh create-user alice 'password123'
|
||||
./manage.sh create-user admin 'secret' admin
|
||||
```
|
||||
|
||||
## Backups
|
||||
|
||||
```bash
|
||||
./manage.sh backup
|
||||
# Creates: backups/YYYYMMDD_HHMMSS/
|
||||
# ├── synapse_db.sql (PostgreSQL dump)
|
||||
# └── synapse_data.tar.gz (media store + keys)
|
||||
```
|
||||
|
||||
Automate with cron:
|
||||
|
||||
```bash
|
||||
# Daily backup at 3 AM
|
||||
0 3 * * * cd /root/hermes-agent/deploy/synapse && ./manage.sh backup >> /var/log/synapse-backup.log 2>&1
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Synapse won't start
|
||||
```bash
|
||||
docker compose logs synapse
|
||||
# Common: PostgreSQL not ready. Wait for healthcheck.
|
||||
```
|
||||
|
||||
### Bot can't connect
|
||||
```bash
|
||||
# Verify token is valid
|
||||
curl -H "Authorization: Bearer $MATRIX_ACCESS_TOKEN" \
|
||||
https://matrix.timmy-time.xyz/_matrix/client/v3/account/whoami
|
||||
```
|
||||
|
||||
### Federation not working
|
||||
```bash
|
||||
# Check port 8448 is open
|
||||
ss -tlnp | grep 8448
|
||||
# Check firewall
|
||||
ufw status
|
||||
```
|
||||
|
||||
### High memory usage
|
||||
```bash
|
||||
# Check resource limits in docker-compose.yml
|
||||
docker stats synapse
|
||||
# Tune in homeserver.yaml: event_cache_size, caches
|
||||
```
|
||||
|
||||
## Security Notes
|
||||
|
||||
- Registration is disabled by default (`enable_registration: false`)
|
||||
- Rate limiting is enforced on login, registration, and messages
|
||||
- Federation certificate verification is enabled
|
||||
- `.env` and `synapse-credentials.env` are `chmod 600`
|
||||
- Client API binds to `127.0.0.1` only (use Nginx for public access)
|
||||
- Consider: firewall rules, fail2ban, regular backups
|
||||
|
||||
## References
|
||||
|
||||
- [Synapse Documentation](https://matrix-org.github.io/synapse/latest/)
|
||||
- [Matrix Spec](https://spec.matrix.org/)
|
||||
- [Epic #269: Matrix Integration](https://forge.alexanderwhitestone.com/Timmy_Foundation/hermes-agent/issues/269)
|
||||
- [Issue #272: Deploy Synapse on Ezra](https://forge.alexanderwhitestone.com/Timmy_Foundation/hermes-agent/issues/272)
|
||||
- [Hermes Matrix Setup Guide](docs/matrix-setup.md)
|
||||
@@ -119,7 +119,6 @@ class HolographicMemoryProvider(MemoryProvider):
|
||||
self._store = None
|
||||
self._retriever = None
|
||||
self._min_trust = float(self._config.get("min_trust_threshold", 0.3))
|
||||
self._last_prefetch_ids: List[int] = []
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@@ -206,14 +205,11 @@ class HolographicMemoryProvider(MemoryProvider):
|
||||
|
||||
def prefetch(self, query: str, *, session_id: str = "") -> str:
|
||||
if not self._retriever or not query:
|
||||
self._last_prefetch_ids = []
|
||||
return ""
|
||||
try:
|
||||
results = self._retriever.search(query, min_trust=self._min_trust, limit=5)
|
||||
if not results:
|
||||
self._last_prefetch_ids = []
|
||||
return ""
|
||||
self._last_prefetch_ids = [r["fact_id"] for r in results if "fact_id" in r]
|
||||
lines = []
|
||||
for r in results:
|
||||
trust = r.get("trust_score", r.get("trust", 0))
|
||||
@@ -221,12 +217,8 @@ class HolographicMemoryProvider(MemoryProvider):
|
||||
return "## Holographic Memory\n" + "\n".join(lines)
|
||||
except Exception as e:
|
||||
logger.debug("Holographic prefetch failed: %s", e)
|
||||
self._last_prefetch_ids = []
|
||||
return ""
|
||||
|
||||
def get_prefetched_fact_ids(self) -> List[int]:
|
||||
return list(self._last_prefetch_ids)
|
||||
|
||||
def sync_turn(self, user_content: str, assistant_content: str, *, session_id: str = "") -> None:
|
||||
# Holographic memory stores explicit facts via tools, not auto-sync.
|
||||
# The on_session_end hook handles auto-extraction if configured.
|
||||
|
||||
35
run_agent.py
35
run_agent.py
@@ -7324,14 +7324,6 @@ class AIAgent:
|
||||
try:
|
||||
_query = original_user_message if isinstance(original_user_message, str) else ""
|
||||
_ext_prefetch_cache = self._memory_manager.prefetch_all(_query) or ""
|
||||
# Auto-calibrate fact trust: detect if user is correcting
|
||||
# the previous turn's response. Runs after prefetch so the
|
||||
# current turn's facts are fresh, and before the tool loop
|
||||
# so any trust changes affect fact retrieval immediately.
|
||||
self._memory_manager.auto_calibrate_feedback(
|
||||
_query,
|
||||
session_id=getattr(self, 'session_id', ''),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -9035,30 +9027,11 @@ class AIAgent:
|
||||
approx_tokens=self.context_compressor.last_prompt_tokens,
|
||||
task_id=effective_task_id,
|
||||
)
|
||||
# Compression created a new session — clear history so
|
||||
# _flush_messages_to_session_db writes compressed messages
|
||||
# to the new session (see preflight compression comment).
|
||||
conversation_history = None
|
||||
|
||||
# Hard overflow guard (#296): if voluntary compression
|
||||
# didn't fire but context exceeds 85% of the MODEL's limit
|
||||
# (not the configured threshold), force compression.
|
||||
# Catches: silent compression failures, context growing too
|
||||
# fast between checks, threshold misconfiguration.
|
||||
elif self.compression_enabled and _compressor.context_length > 0:
|
||||
_model_usage = _real_tokens / _compressor.context_length
|
||||
if _model_usage >= 0.85:
|
||||
logger.warning(
|
||||
"Hard context overflow guard: %.1f%% of model context "
|
||||
"(%s tokens of %s), forcing compression",
|
||||
_model_usage * 100,
|
||||
f"{_real_tokens:,}",
|
||||
f"{_compressor.context_length:,}",
|
||||
)
|
||||
messages, active_system_prompt = self._compress_context(
|
||||
messages, system_message,
|
||||
approx_tokens=self.context_compressor.last_prompt_tokens,
|
||||
task_id=effective_task_id,
|
||||
)
|
||||
conversation_history = None
|
||||
|
||||
|
||||
# Save session log incrementally (so progress is visible even if interrupted)
|
||||
self._session_messages = messages
|
||||
self._save_session_log(messages)
|
||||
|
||||
@@ -1,284 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Benchmark local Ollama models against the 50 tok/s UX threshold.
|
||||
|
||||
Usage:
|
||||
python3 scripts/benchmark_local_models.py [--models MODEL1,MODEL2] [--prompt PROMPT] [--rounds N]
|
||||
python3 scripts/benchmark_local_models.py --all # test all pulled models
|
||||
python3 scripts/benchmark_local_models.py --json # JSON output for CI
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
from dataclasses import dataclass, asdict
|
||||
from typing import Optional
|
||||
|
||||
OLLAMA_BASE = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
|
||||
THRESHOLD_TOK_S = 50.0
|
||||
|
||||
BENCHMARK_PROMPT = (
|
||||
"Explain the difference between TCP and UDP protocols. "
|
||||
"Cover reliability, ordering, speed, and use cases. "
|
||||
"Be thorough but concise. Write at least 300 words."
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BenchmarkResult:
|
||||
model: str
|
||||
size_gb: float
|
||||
prompt_tokens: int
|
||||
eval_tokens: int
|
||||
eval_duration_s: float
|
||||
tokens_per_second: float
|
||||
total_duration_s: float
|
||||
rounds: int
|
||||
avg_tok_s: float
|
||||
meets_threshold: bool
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
def get_models() -> list[dict]:
|
||||
"""List all pulled Ollama models."""
|
||||
url = f"{OLLAMA_BASE}/api/tags"
|
||||
try:
|
||||
req = urllib.request.Request(url)
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
data = json.loads(resp.read())
|
||||
return data.get("models", [])
|
||||
except Exception as e:
|
||||
print(f"Error connecting to Ollama at {OLLAMA_BASE}: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def benchmark_model(model: str, prompt: str, num_predict: int = 512) -> dict:
|
||||
"""Run a single benchmark generation, return timing stats."""
|
||||
url = f"{OLLAMA_BASE}/api/generate"
|
||||
payload = json.dumps({
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
"options": {
|
||||
"num_predict": num_predict,
|
||||
"temperature": 0.1, # low temp for consistent output
|
||||
},
|
||||
}).encode()
|
||||
|
||||
req = urllib.request.Request(url, data=payload, method="POST")
|
||||
req.add_header("Content-Type", "application/json")
|
||||
|
||||
start = time.monotonic()
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||||
data = json.loads(resp.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
body = e.read().decode() if e.fp else str(e)
|
||||
raise RuntimeError(f"HTTP {e.code}: {body[:200]}")
|
||||
except Exception as e:
|
||||
raise RuntimeError(str(e))
|
||||
elapsed = time.monotonic() - start
|
||||
|
||||
prompt_tokens = data.get("prompt_eval_count", 0)
|
||||
eval_tokens = data.get("eval_count", 0)
|
||||
eval_duration_ns = data.get("eval_duration", 0)
|
||||
total_duration_ns = data.get("total_duration", 0)
|
||||
|
||||
eval_duration_s = eval_duration_ns / 1e9 if eval_duration_ns else elapsed
|
||||
total_duration_s = total_duration_ns / 1e9 if total_duration_ns else elapsed
|
||||
tok_s = eval_tokens / eval_duration_s if eval_duration_s > 0 else 0.0
|
||||
|
||||
return {
|
||||
"prompt_tokens": prompt_tokens,
|
||||
"eval_tokens": eval_tokens,
|
||||
"eval_duration_s": round(eval_duration_s, 2),
|
||||
"total_duration_s": round(total_duration_s, 2),
|
||||
"tokens_per_second": round(tok_s, 1),
|
||||
}
|
||||
|
||||
|
||||
def run_benchmark(
|
||||
model_name: str,
|
||||
model_size: float,
|
||||
prompt: str,
|
||||
rounds: int,
|
||||
num_predict: int,
|
||||
threshold: float = 50.0,
|
||||
) -> BenchmarkResult:
|
||||
"""Run multiple rounds and compute average."""
|
||||
results = []
|
||||
errors = []
|
||||
|
||||
for i in range(rounds):
|
||||
try:
|
||||
r = benchmark_model(model_name, prompt, num_predict)
|
||||
results.append(r)
|
||||
print(f" Round {i+1}/{rounds}: {r['tokens_per_second']} tok/s "
|
||||
f"({r['eval_tokens']} tokens in {r['eval_duration_s']}s)")
|
||||
except Exception as e:
|
||||
errors.append(str(e))
|
||||
print(f" Round {i+1}/{rounds}: ERROR - {e}")
|
||||
|
||||
if not results:
|
||||
return BenchmarkResult(
|
||||
model=model_name,
|
||||
size_gb=model_size,
|
||||
prompt_tokens=0, eval_tokens=0,
|
||||
eval_duration_s=0, tokens_per_second=0,
|
||||
total_duration_s=0, rounds=rounds,
|
||||
avg_tok_s=0, meets_threshold=False,
|
||||
error="; ".join(errors),
|
||||
)
|
||||
|
||||
avg_tok_s = sum(r["tokens_per_second"] for r in results) / len(results)
|
||||
avg_tok_s = round(avg_tok_s, 1)
|
||||
|
||||
return BenchmarkResult(
|
||||
model=model_name,
|
||||
size_gb=model_size,
|
||||
prompt_tokens=sum(r["prompt_tokens"] for r in results) // len(results),
|
||||
eval_tokens=sum(r["eval_tokens"] for r in results) // len(results),
|
||||
eval_duration_s=round(sum(r["eval_duration_s"] for r in results) / len(results), 2),
|
||||
tokens_per_second=avg_tok_s,
|
||||
total_duration_s=round(sum(r["total_duration_s"] for r in results) / len(results), 2),
|
||||
rounds=len(results),
|
||||
avg_tok_s=avg_tok_s,
|
||||
meets_threshold=avg_tok_s >= threshold,
|
||||
)
|
||||
|
||||
|
||||
def format_report(results: list[BenchmarkResult], threshold: float = 50.0) -> str:
|
||||
"""Format a human-readable benchmark report."""
|
||||
lines = []
|
||||
lines.append("")
|
||||
lines.append("=" * 72)
|
||||
lines.append(f" LOCAL MODEL BENCHMARK — {threshold:.0f} tok/s UX Threshold")
|
||||
lines.append("=" * 72)
|
||||
lines.append("")
|
||||
|
||||
# Summary table
|
||||
header = f"{'Model':<25} {'Size':>6} {'tok/s':>8} {'Threshold':>10} {'Status':>8}"
|
||||
lines.append(header)
|
||||
lines.append("-" * 72)
|
||||
|
||||
passed = 0
|
||||
failed = 0
|
||||
errors = 0
|
||||
|
||||
for r in sorted(results, key=lambda x: x.avg_tok_s, reverse=True):
|
||||
size_str = f"{r.size_gb:.1f}GB"
|
||||
tok_s_str = f"{r.avg_tok_s:.1f}"
|
||||
|
||||
if r.error:
|
||||
status = "ERROR"
|
||||
errors += 1
|
||||
elif r.meets_threshold:
|
||||
status = "PASS"
|
||||
passed += 1
|
||||
else:
|
||||
status = "FAIL"
|
||||
failed += 1
|
||||
|
||||
marker = ">" if r.meets_threshold else "X" if r.error else "!"
|
||||
thresh_str = f">= {threshold:.0f}"
|
||||
lines.append(f" {marker} {r.model:<23} {size_str:>6} {tok_s_str:>8} {thresh_str:>10} {status:>8}")
|
||||
|
||||
lines.append("-" * 72)
|
||||
lines.append(f" Passed: {passed} | Failed: {failed} | Errors: {errors} | Total: {len(results)}")
|
||||
lines.append("")
|
||||
|
||||
# Detail section for failures
|
||||
failures = [r for r in results if not r.meets_threshold and not r.error]
|
||||
if failures:
|
||||
lines.append(" FAILED MODELS (below threshold):")
|
||||
for r in sorted(failures, key=lambda x: x.avg_tok_s):
|
||||
gap = threshold - r.avg_tok_s
|
||||
lines.append(f" - {r.model}: {r.avg_tok_s:.1f} tok/s "
|
||||
f"({gap:.1f} tok/s short, {r.eval_tokens} avg tokens/round)")
|
||||
lines.append("")
|
||||
|
||||
error_list = [r for r in results if r.error]
|
||||
if error_list:
|
||||
lines.append(" ERRORS:")
|
||||
for r in error_list:
|
||||
lines.append(f" - {r.model}: {r.error}")
|
||||
lines.append("")
|
||||
|
||||
# Hardware info
|
||||
import platform
|
||||
lines.append(f" Host: {platform.node()} | {platform.system()} {platform.release()}")
|
||||
lines.append(f" Ollama: {OLLAMA_BASE}")
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Benchmark local Ollama models vs 50 tok/s threshold")
|
||||
parser.add_argument("--models", help="Comma-separated model names (default: all)")
|
||||
parser.add_argument("--prompt", default=BENCHMARK_PROMPT, help="Benchmark prompt")
|
||||
parser.add_argument("--rounds", type=int, default=3, help="Rounds per model (default: 3)")
|
||||
parser.add_argument("--tokens", type=int, default=512, help="Max tokens to generate (default: 512)")
|
||||
parser.add_argument("--json", action="store_true", help="JSON output for CI")
|
||||
parser.add_argument("--all", action="store_true", help="Test all pulled models")
|
||||
parser.add_argument("--threshold", type=float, default=THRESHOLD_TOK_S, help="tok/s threshold")
|
||||
args = parser.parse_args()
|
||||
threshold = args.threshold
|
||||
|
||||
# Get model list
|
||||
available = get_models()
|
||||
if not available:
|
||||
print("No models found. Pull a model first: ollama pull <model>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if args.models:
|
||||
names = [m.strip() for m in args.models.split(",")]
|
||||
models = [m for m in available if m["name"] in names]
|
||||
missing = set(names) - set(m["name"] for m in models)
|
||||
if missing:
|
||||
print(f"Models not found: {', '.join(missing)}", file=sys.stderr)
|
||||
print(f"Available: {', '.join(m['name'] for m in available)}", file=sys.stderr)
|
||||
else:
|
||||
models = available
|
||||
|
||||
print(f"Benchmarking {len(models)} model(s) against {threshold} tok/s threshold")
|
||||
print(f"Ollama: {OLLAMA_BASE} | Rounds: {args.rounds} | Max tokens: {args.tokens}")
|
||||
print()
|
||||
|
||||
results = []
|
||||
for m in models:
|
||||
name = m["name"]
|
||||
size_gb = m.get("size", 0) / (1024**3)
|
||||
print(f" {name} ({size_gb:.1f}GB):")
|
||||
|
||||
result = run_benchmark(name, size_gb, args.prompt, args.rounds, args.tokens, threshold)
|
||||
results.append(result)
|
||||
|
||||
# Output
|
||||
report = format_report(results, threshold)
|
||||
if args.json:
|
||||
output = {
|
||||
"threshold_tok_s": threshold,
|
||||
"ollama_base": OLLAMA_BASE,
|
||||
"rounds": args.rounds,
|
||||
"results": [asdict(r) for r in results],
|
||||
"passed": sum(1 for r in results if r.meets_threshold),
|
||||
"failed": sum(1 for r in results if not r.meets_threshold and not r.error),
|
||||
"errors": sum(1 for r in results if r.error),
|
||||
}
|
||||
print(json.dumps(output, indent=2))
|
||||
else:
|
||||
print(report)
|
||||
|
||||
# Exit code: 0 if all pass, 1 if any fail/error
|
||||
if any(not r.meets_threshold or r.error for r in results):
|
||||
sys.exit(1)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,252 +0,0 @@
|
||||
"""Tests for automatic fact trust calibration (Issue #252)."""
|
||||
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from agent.memory_manager import MemoryManager, _detect_correction
|
||||
from plugins.memory.holographic import HolographicMemoryProvider
|
||||
|
||||
|
||||
def _make_holographic_provider(db_path=":memory:"):
|
||||
"""Create a holographic provider backed by an in-memory SQLite DB."""
|
||||
provider = HolographicMemoryProvider(config={
|
||||
"db_path": db_path,
|
||||
"default_trust": 0.5,
|
||||
"min_trust_threshold": 0.3,
|
||||
"hrr_dim": 64, # small for speed
|
||||
})
|
||||
provider.initialize(session_id="test")
|
||||
return provider
|
||||
|
||||
|
||||
class TestDetectCorrection:
|
||||
"""Correction detection pattern matching."""
|
||||
|
||||
@pytest.mark.parametrize("msg", [
|
||||
"No, that's wrong",
|
||||
"Actually, it's Python 3.12",
|
||||
"That's not right",
|
||||
"I said the config is in YAML",
|
||||
"Correction: the port is 8080",
|
||||
"Nope, wrong file",
|
||||
"Not quite what I meant",
|
||||
"Undo that last change",
|
||||
"that is not correct",
|
||||
"what i meant was different",
|
||||
])
|
||||
def test_correction_detected(self, msg):
|
||||
assert _detect_correction(msg) is True
|
||||
|
||||
@pytest.mark.parametrize("msg", [
|
||||
"",
|
||||
"Hello",
|
||||
"What's the weather today?",
|
||||
"I need you to build a new feature. " * 10,
|
||||
"yes that's correct",
|
||||
])
|
||||
def test_not_a_correction(self, msg):
|
||||
assert _detect_correction(msg) is False
|
||||
|
||||
|
||||
class TestAutoCalibrateFeedback:
|
||||
"""Auto-calibration integration."""
|
||||
|
||||
def test_correction_marks_unhelpful(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
# Store a fact
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "The project uses Flask framework"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
|
||||
# Simulate: this fact was prefetched
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
# User corrects: "No, it uses FastAPI"
|
||||
manager.auto_calibrate_feedback("No, it uses FastAPI")
|
||||
|
||||
# Check trust dropped
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] < 0.5 # dropped from default 0.5
|
||||
assert target["trust_score"] == pytest.approx(0.4, abs=0.01) # 0.5 - 0.1
|
||||
|
||||
def test_successful_interaction_gains_trust(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
# Store a fact
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "The project uses Django framework"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
|
||||
# Simulate: this fact was prefetched
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
# User says something normal (not a correction)
|
||||
manager.auto_calibrate_feedback("What version of Django?")
|
||||
|
||||
# Check trust increased
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] > 0.5 # rose from default 0.5
|
||||
assert target["trust_score"] == pytest.approx(0.55, abs=0.01) # 0.5 + 0.05
|
||||
|
||||
def test_no_prefetch_no_calibration(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
# Store a fact
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "The database is PostgreSQL"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
|
||||
# No prefetched facts
|
||||
provider._last_prefetch_ids = []
|
||||
|
||||
# Calibrate — should be no-op
|
||||
manager.auto_calibrate_feedback("No, it's MySQL")
|
||||
|
||||
# Trust should be unchanged
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] == 0.5 # unchanged
|
||||
|
||||
def test_multiple_corrections_drives_trust_low(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
# Store a fact
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "The server runs on port 3000"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
# Simulate 5 corrections
|
||||
for _ in range(5):
|
||||
manager.auto_calibrate_feedback("Wrong, it's port 8080")
|
||||
|
||||
# Trust should be much lower
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] < 0.2 # 0.5 - 5*0.1 = 0.0 (clamped)
|
||||
|
||||
def test_trust_floor_at_zero(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "Test fact for floor"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
# 10 corrections should clamp at 0.0, not go negative
|
||||
for _ in range(10):
|
||||
manager.auto_calibrate_feedback("Wrong!")
|
||||
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] == 0.0
|
||||
|
||||
def test_trust_ceiling_at_one(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "Test fact for ceiling"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
# 20 successful interactions should cap at 1.0
|
||||
for _ in range(20):
|
||||
manager.auto_calibrate_feedback("Thanks, what else?")
|
||||
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "list", "min_trust": 0.0},
|
||||
)
|
||||
facts = json.loads(result)["facts"]
|
||||
target = next(f for f in facts if f["fact_id"] == fact_id)
|
||||
assert target["trust_score"] == 1.0
|
||||
|
||||
def test_get_pruning_candidates(self):
|
||||
provider = _make_holographic_provider()
|
||||
manager = MemoryManager()
|
||||
manager.add_provider(provider)
|
||||
|
||||
# Add a fact and drive its trust below threshold via corrections
|
||||
result = manager.handle_tool_call(
|
||||
"fact_store",
|
||||
{"action": "add", "content": "Bad fact to be pruned"},
|
||||
)
|
||||
fact_id = json.loads(result)["fact_id"]
|
||||
provider._last_prefetch_ids = [fact_id]
|
||||
|
||||
for _ in range(5):
|
||||
manager.auto_calibrate_feedback("Wrong!")
|
||||
|
||||
# Get pruning candidates
|
||||
candidates = manager.get_pruning_candidates(threshold=0.15)
|
||||
assert any(c["fact_id"] == fact_id for c in candidates)
|
||||
|
||||
def test_prefetch_tracks_fact_ids(self):
|
||||
"""Verify prefetch populates _last_prefetch_ids."""
|
||||
provider = _make_holographic_provider()
|
||||
|
||||
# Add facts
|
||||
provider.handle_tool_call("fact_store", {
|
||||
"action": "add",
|
||||
"content": "Alexander uses Python for development",
|
||||
})
|
||||
provider.handle_tool_call("fact_store", {
|
||||
"action": "add",
|
||||
"content": "Alexander prefers dark mode editors",
|
||||
})
|
||||
|
||||
# Prefetch should find them and track IDs
|
||||
result = provider.prefetch("Alexander")
|
||||
assert "Holographic Memory" in result
|
||||
assert len(provider._last_prefetch_ids) > 0
|
||||
|
||||
# Empty query clears IDs
|
||||
provider.prefetch("")
|
||||
assert provider._last_prefetch_ids == []
|
||||
@@ -1,107 +0,0 @@
|
||||
"""Tests for hard context overflow guard (#296)."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
class TestHardOverflowGuard:
|
||||
"""Verify the 85% hard overflow guard catches context overflow."""
|
||||
|
||||
def test_model_usage_calculation(self):
|
||||
"""Verify model usage = real_tokens / context_length."""
|
||||
real_tokens = 85_000
|
||||
context_length = 100_000
|
||||
usage = real_tokens / context_length
|
||||
assert usage == 0.85
|
||||
|
||||
def test_85_percent_is_threshold(self):
|
||||
"""85% of model context should trigger the hard guard."""
|
||||
context_length = 100_000
|
||||
# At 85% exactly
|
||||
assert (85_000 / context_length) >= 0.85
|
||||
# At 84.9% — should NOT trigger
|
||||
assert (84_900 / context_length) < 0.85
|
||||
|
||||
def test_hard_guard_only_when_voluntary_skipped(self):
|
||||
"""Hard guard should use elif — not fire when voluntary compression fires."""
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
# Find the hard guard code in run_conversation
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
# It should be an elif, not a separate if
|
||||
# The elif ensures it only fires when voluntary compression didn't
|
||||
assert "elif" in src.split("Hard overflow guard")[0].split("should_compress")[-1]
|
||||
|
||||
def test_hard_guard_checks_85_percent(self):
|
||||
"""Hard guard threshold should be 0.85 (85%)."""
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
# Find the line with the threshold
|
||||
for line in src.split('\n'):
|
||||
if 'model_usage >= 0.85' in line or 'model_usage >= 0.85' in line:
|
||||
assert True
|
||||
return
|
||||
# Alternative: check for >= 0.85 anywhere near the hard guard
|
||||
assert "0.85" in src.split("Hard overflow guard")[1].split("Save session log")[0]
|
||||
|
||||
def test_hard_guard_logs_warning(self):
|
||||
"""Hard guard should log a warning when triggered."""
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
guard_section = src.split("Hard overflow guard")[1].split("Save session log")[0]
|
||||
assert "logger.warning" in guard_section
|
||||
assert "forcing compression" in guard_section
|
||||
|
||||
def test_context_length_zero_skips(self):
|
||||
"""Guard should skip when context_length is 0 (unknown model)."""
|
||||
context_length = 0
|
||||
# The guard checks context_length > 0 before computing usage
|
||||
assert context_length > 0 is False
|
||||
|
||||
def test_usage_scenarios(self):
|
||||
"""Test various usage levels against the 85% threshold."""
|
||||
context_length = 128_000
|
||||
scenarios = [
|
||||
(50_000, False, "39% — well under"),
|
||||
(80_000, False, "62% — under"),
|
||||
(100_000, False, "78% — under but close"),
|
||||
(108_800, True, "85% — exactly at threshold"),
|
||||
(110_000, True, "86% — just over"),
|
||||
(120_000, True, "94% — dangerously high"),
|
||||
(128_000, True, "100% — at limit"),
|
||||
]
|
||||
for tokens, should_trigger, desc in scenarios:
|
||||
usage = tokens / context_length
|
||||
triggers = usage >= 0.85
|
||||
assert triggers == should_trigger, f"{desc}: usage={usage:.1%}, expected trigger={should_trigger}, got={triggers}"
|
||||
|
||||
|
||||
class TestHardGuardIntegration:
|
||||
"""Test that the hard guard is present in the right location."""
|
||||
|
||||
def test_guard_is_in_run_conversation(self):
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
assert "Hard overflow guard" in src
|
||||
|
||||
def test_guard_uses_elif_chain(self):
|
||||
"""Verify the elif structure: voluntary → hard guard → else."""
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
# Find the section
|
||||
section = src.split("should_compress(_real_tokens)")[1].split("Save session log")[0]
|
||||
# Should contain elif for the hard guard
|
||||
assert "elif" in section
|
||||
assert "_model_usage" in section
|
||||
|
||||
def test_compression_disabled_skips_hard_guard(self):
|
||||
"""If compression is disabled, hard guard should also be skipped."""
|
||||
import inspect
|
||||
from run_agent import AIAgent
|
||||
src = inspect.getsource(AIAgent.run_conversation)
|
||||
section = src.split("Hard overflow guard")[1].split("Save session log")[0]
|
||||
assert "self.compression_enabled" in section
|
||||
Reference in New Issue
Block a user