feat(api): Integrate aggressive logging for all hook invocations

This commit is contained in:
2026-02-23 12:23:23 -05:00
parent 0d09007dc1
commit ef29902963
2 changed files with 26 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import json
import threading import threading
from http.server import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPServer, BaseHTTPRequestHandler
import logging import logging
import session_logger
class HookServerInstance(HTTPServer): class HookServerInstance(HTTPServer):
def __init__(self, server_address, RequestHandlerClass, app): def __init__(self, server_address, RequestHandlerClass, app):
@@ -11,6 +12,7 @@ class HookServerInstance(HTTPServer):
class HookHandler(BaseHTTPRequestHandler): class HookHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
app = self.server.app app = self.server.app
session_logger.log_api_hook("GET", self.path, "")
if self.path == '/status': if self.path == '/status':
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', 'application/json') self.send_header('Content-Type', 'application/json')
@@ -34,9 +36,11 @@ class HookHandler(BaseHTTPRequestHandler):
app = self.server.app app = self.server.app
content_length = int(self.headers.get('Content-Length', 0)) content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length) body = self.rfile.read(content_length)
body_str = body.decode('utf-8') if body else ""
session_logger.log_api_hook("POST", self.path, body_str)
try: try:
data = json.loads(body.decode('utf-8')) if body else {} data = json.loads(body_str) if body_str else {}
if self.path == '/api/project': if self.path == '/api/project':
app.project = data.get('project', app.project) app.project = data.get('project', app.project)
self.send_response(200) self.send_response(200)

View File

@@ -40,6 +40,7 @@ _seq_lock = threading.Lock()
_comms_fh = None # file handle: logs/comms_<ts>.log _comms_fh = None # file handle: logs/comms_<ts>.log
_tool_fh = None # file handle: logs/toolcalls_<ts>.log _tool_fh = None # file handle: logs/toolcalls_<ts>.log
_api_fh = None # file handle: logs/apihooks_<ts>.log
def _now_ts() -> str: def _now_ts() -> str:
@@ -52,7 +53,7 @@ def open_session():
opens the two log files for this session. Idempotent - a second call is opens the two log files for this session. Idempotent - a second call is
ignored. ignored.
""" """
global _ts, _comms_fh, _tool_fh, _seq global _ts, _comms_fh, _tool_fh, _api_fh, _seq
if _comms_fh is not None: if _comms_fh is not None:
return # already open return # already open
@@ -65,6 +66,7 @@ def open_session():
_comms_fh = open(_LOG_DIR / f"comms_{_ts}.log", "w", encoding="utf-8", buffering=1) _comms_fh = open(_LOG_DIR / f"comms_{_ts}.log", "w", encoding="utf-8", buffering=1)
_tool_fh = open(_LOG_DIR / f"toolcalls_{_ts}.log", "w", encoding="utf-8", buffering=1) _tool_fh = open(_LOG_DIR / f"toolcalls_{_ts}.log", "w", encoding="utf-8", buffering=1)
_api_fh = open(_LOG_DIR / f"apihooks_{_ts}.log", "w", encoding="utf-8", buffering=1)
_tool_fh.write(f"# Tool-call log — session {_ts}\n\n") _tool_fh.write(f"# Tool-call log — session {_ts}\n\n")
_tool_fh.flush() _tool_fh.flush()
@@ -72,13 +74,30 @@ def open_session():
def close_session(): def close_session():
"""Flush and close both log files. Called on clean exit (optional).""" """Flush and close both log files. Called on clean exit (optional)."""
global _comms_fh, _tool_fh global _comms_fh, _tool_fh, _api_fh
if _comms_fh: if _comms_fh:
_comms_fh.close() _comms_fh.close()
_comms_fh = None _comms_fh = None
if _tool_fh: if _tool_fh:
_tool_fh.close() _tool_fh.close()
_tool_fh = None _tool_fh = None
if _api_fh:
_api_fh.close()
_api_fh = None
def log_api_hook(method: str, path: str, payload: str):
"""
Log an API hook invocation.
"""
if _api_fh is None:
return
ts_entry = datetime.datetime.now().strftime("%H:%M:%S")
try:
_api_fh.write(f"[{ts_entry}] {method} {path} - Payload: {payload}\n")
_api_fh.flush()
except Exception:
pass
def log_comms(entry: dict): def log_comms(entry: dict):