File size: 8,072 Bytes
5850885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Single logging entrypoint: JSONL to ``outputs/logs/sql_drift_env.log``.

The stdlib :mod:`logging` must not be used elsewhere in this package;
use :func:`get_module_logger` for diagnostic messages
(:meth:`~logging.Logger.info` / :meth:`~logging.Logger.warning` / …) and
:func:`log_interaction` for structured agent/env events.
"""

from __future__ import annotations

import functools
import json
import logging
import os
from collections.abc import Callable
from datetime import datetime
from logging.handlers import RotatingFileHandler
from pathlib import Path
from typing import Any
from zoneinfo import ZoneInfo

_REPO_ROOT = Path(__file__).resolve().parent.parent
LOG_DIR = _REPO_ROOT / "outputs" / "logs"
LOG_FILE = LOG_DIR / "sql_drift_env.log"
INTERACTION_LOGGER_NAME = "sql_drift_env.interactions"
APP_LOGGER_ROOT = "sql_drift_env.app"
IST = ZoneInfo("Asia/Kolkata")
TIMESTAMP_FORMAT = "%d-%b-%Y %I:%M:%S %p"

# Rotating log limits — override via environment variables.
_LOG_MAX_BYTES: int = int(os.environ.get("SQL_DRIFT_LOG_MAX_BYTES", str(50 * 1024 * 1024)))
_LOG_BACKUP_COUNT: int = int(os.environ.get("SQL_DRIFT_LOG_BACKUP_COUNT", "5"))

# When SQL_DRIFT_LOG_PROMPTS is not set (default), llm_prompt and llm_response
# are suppressed from the interaction log to prevent unbounded disk growth and
# accidental prompt/credential leakage in production.
_LOG_PROMPTS: bool = os.environ.get("SQL_DRIFT_LOG_PROMPTS", "").strip().lower() in (
    "1",
    "true",
    "yes",
)

_app_logging_configured: bool = False


def get_interaction_jsonl_logger() -> logging.Logger:
    """Return the rotating-file logger used to append JSONL lines, handler registered once."""
    LOG_DIR.mkdir(parents=True, exist_ok=True)
    lg = logging.getLogger(INTERACTION_LOGGER_NAME)
    lg.setLevel(logging.INFO)
    lg.propagate = False

    log_path = str(LOG_FILE)
    for handler in lg.handlers:
        if isinstance(handler, logging.FileHandler) and handler.baseFilename == log_path:
            return lg

    handler = RotatingFileHandler(
        LOG_FILE,
        maxBytes=_LOG_MAX_BYTES,
        backupCount=_LOG_BACKUP_COUNT,
        encoding="utf-8",
    )
    handler.setFormatter(logging.Formatter("%(message)s"))
    lg.addHandler(handler)
    return lg


interaction_jsonl_logger = get_interaction_jsonl_logger()


def log_interaction(
    *,
    event_type: str,
    agent_id: str | None = None,
    action_taken: Any = None,
    observation_returned: Any = None,
    reward: Any = None,
    done: Any = None,
    llm_prompt: Any = None,
    llm_response: Any = None,
    error: str | None = None,
) -> None:
    """Write one structured interaction event to the central log file.

    LLM prompts and responses are omitted unless the environment variable
    ``SQL_DRIFT_LOG_PROMPTS=1`` is set, preventing unbounded disk growth and
    accidental leakage of prompt content in production deployments.
    """
    entry = {
        "timestamp": datetime.now(IST).strftime(TIMESTAMP_FORMAT),
        "event_type": event_type,
        "agent_id": agent_id,
        "action_taken": _to_jsonable(action_taken),
        "observation_returned": _to_jsonable(observation_returned),
        "reward": _to_jsonable(reward),
        "done": _to_jsonable(done),
        "llm_prompt": _to_jsonable(llm_prompt) if _LOG_PROMPTS else None,
        "llm_response": _to_jsonable(llm_response) if _LOG_PROMPTS else None,
    }
    if error is not None:
        entry["error"] = error
    interaction_jsonl_logger.info(json.dumps(entry, ensure_ascii=False, separators=(",", ":")))


class _AppJsonlLogHandler(logging.Handler):
    """Routes :class:`logging.LogRecord` instances into :func:`log_interaction`."""

    def emit(self, record: logging.LogRecord) -> None:
        try:
            log_interaction(
                event_type="app",
                action_taken={
                    "level": record.levelname,
                    "source": record.name,
                    "message": record.getMessage(),
                },
            )
        except Exception:
            self.handleError(record)


def _ensure_app_logging() -> None:
    global _app_logging_configured
    if _app_logging_configured:
        return
    _app_logging_configured = True
    parent = logging.getLogger(APP_LOGGER_ROOT)
    parent.setLevel(logging.DEBUG)
    # Allow records to continue to ancestors (e.g. root, for pytest caplog).
    parent.propagate = True
    if not any(type(h) is _AppJsonlLogHandler for h in parent.handlers):
        parent.addHandler(_AppJsonlLogHandler(level=logging.DEBUG))


def get_module_logger(qualname: str) -> logging.Logger:
    """Diagnostic logger for a module; records go to the central JSONL file."""
    _ensure_app_logging()
    return logging.getLogger(f"{APP_LOGGER_ROOT}.{qualname}")


def get_logger() -> logging.Logger:
    """Same as :func:`get_interaction_jsonl_logger`."""
    return get_interaction_jsonl_logger()


logger = interaction_jsonl_logger


def log_env_reset(func: Callable[..., Any]) -> Callable[..., Any]:
    """Decorator for OpenEnv ``reset()`` methods."""

    @functools.wraps(func)
    def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
        action = {
            "seed": args[0] if args else kwargs.get("seed"),
            "episode_id": args[1] if len(args) > 1 else kwargs.get("episode_id"),
            **kwargs,
        }
        try:
            observation = func(self, *args, **kwargs)
        except Exception as exc:
            episode_id = action.get("episode_id")
            log_interaction(
                event_type="reset",
                agent_id=str(episode_id) if episode_id is not None else None,
                action_taken=action,
                error=repr(exc),
            )
            raise
        log_interaction(
            event_type="reset",
            agent_id=_agent_id_from_env(self),
            action_taken=action,
            observation_returned=observation,
            reward=getattr(observation, "reward", None),
            done=getattr(observation, "done", None),
        )
        return observation

    return wrapper


def log_env_step(func: Callable[..., Any]) -> Callable[..., Any]:
    """Decorator for OpenEnv ``step()`` methods."""

    @functools.wraps(func)
    def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
        action = args[0] if args else kwargs.get("action")
        try:
            observation = func(self, *args, **kwargs)
        except Exception as exc:
            log_interaction(
                event_type="step",
                agent_id=_agent_id_from_env(self),
                action_taken=action,
                error=repr(exc),
            )
            raise
        log_interaction(
            event_type="step",
            agent_id=_agent_id_from_env(self),
            action_taken=action,
            observation_returned=observation,
            reward=getattr(observation, "reward", None),
            done=getattr(observation, "done", None),
        )
        return observation

    return wrapper


def _agent_id_from_env(env: Any) -> str | None:
    runtime = getattr(env, "_runtime", None)
    episode_id = getattr(runtime, "episode_id", None)
    return str(episode_id) if episode_id is not None else None


def _to_jsonable(value: Any) -> Any:
    if value is None or isinstance(value, str | int | float | bool):
        return value
    if isinstance(value, dict):
        return {str(k): _to_jsonable(v) for k, v in value.items()}
    if isinstance(value, list | tuple | set):
        return [_to_jsonable(v) for v in value]
    model_dump = getattr(value, "model_dump", None)
    if callable(model_dump):
        return model_dump(mode="json")
    return str(value)


__all__ = [
    "APP_LOGGER_ROOT",
    "LOG_FILE",
    "get_interaction_jsonl_logger",
    "get_logger",
    "get_module_logger",
    "interaction_jsonl_logger",
    "logger",
    "log_env_reset",
    "log_env_step",
    "log_interaction",
]  # _LOG_PROMPTS / _LOG_MAX_BYTES / _LOG_BACKUP_COUNT are intentionally private