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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
HITL pending run storage.

Supports persistent DB-backed storage (Supabase/SQLite provider) with in-memory fallback.
"""

from __future__ import annotations

import json
import logging
import re
from datetime import UTC, datetime, timedelta
from typing import Any
from uuid import UUID

from ..models.db import DbFilter, DbQueryRequest
from .db_adapters import build_adapter
from .db_registry import ProviderConfig, get_provider_registry, normalize_provider_type
from .db_service import execute_db_async
from .hitl_serializer import deserialize_requirements, serialize_requirements

logger = logging.getLogger(__name__)


def _utc_now() -> datetime:
    return datetime.now(UTC)


def _utc_now_iso() -> str:
    return _utc_now().replace(microsecond=0).isoformat()


def _is_missing_pending_form_table_error(error: Any) -> bool:
    text = str(error or "").lower()
    return "pending_form_runs" in text and (
        "pgrst205" in text
        or "pgrst204" in text
        or "could not find the table" in text
        or "could not find the" in text
        or "not found" in text
        or "does not exist" in text
    )


def _to_uuid_or_none(value: Any) -> str | None:
    if value is None:
        return None
    text = str(value).strip()
    if not text:
        return None
    try:
        return str(UUID(text))
    except Exception:
        return None


def _normalize_stored_messages(raw_messages: Any) -> tuple[Any, Any]:
    """
    Backward/adapter compatibility for pending_form_runs.messages.

    messages may come back as:
    - list (legacy history-only)
    - dict {"history": [...], "run_output": {...}} (current)
    - JSON string for either of the above (provider/adapter dependent)
    """
    parsed = raw_messages
    if isinstance(parsed, str):
        text = parsed.strip()
        if text:
            try:
                parsed = json.loads(text)
            except Exception:
                parsed = raw_messages

    saved_messages = parsed
    saved_run_output = None
    if isinstance(parsed, dict):
        if "history" in parsed:
            saved_messages = parsed.get("history")
        if "run_output" in parsed:
            saved_run_output = parsed.get("run_output")
    return saved_messages, saved_run_output


def _extract_missing_column_from_error(error: Any) -> str | None:
    text = str(error or "")
    # Supabase/PostgREST PGRST204 example:
    # "Could not find the 'agent_model' column of 'pending_form_runs' in the schema cache"
    match = re.search(r"Could not find the '([^']+)' column", text, re.IGNORECASE)
    if match:
        return match.group(1)
    return None


class InMemoryHITLStorage:
    """In-memory storage for HITL pending runs."""

    def __init__(self) -> None:
        self._store: dict[str, dict[str, Any]] = {}

    async def save_pending_run(
        self,
        run_id: str,
        requirements: list[Any],
        conversation_id: str | None = None,
        user_id: str | None = None,
        agent_model: str | None = None,
        ttl_minutes: int = 30,
        messages: list[dict[str, Any]] | None = None,
        run_output: dict[str, Any] | None = None,
    ) -> dict[str, Any] | None:
        requirements_data = serialize_requirements(requirements)
        expires_at = (_utc_now() + timedelta(minutes=ttl_minutes)).isoformat()
        stored_messages: Any = messages
        if run_output is not None:
            stored_messages = {
                "history": messages,
                "run_output": run_output,
            }
        record = {
            "run_id": run_id,
            "requirements_data": requirements_data,
            "expires_at": expires_at,
            "status": "pending",
            "conversation_id": conversation_id,
            "user_id": user_id,
            "agent_model": agent_model,
            "messages": stored_messages,
            "created_at": _utc_now_iso(),
        }
        self._store[run_id] = record
        logger.info("[HITL] Stored pending run in memory: %s", run_id)
        return record

    async def get_pending_run(self, run_id: str) -> dict[str, Any] | None:
        record = self._store.get(run_id)
        if not record:
            logger.warning("[HITL] Pending run %s not found in memory", run_id)
            return None
        requirements_data = record.get("requirements_data") or []
        requirements = deserialize_requirements(requirements_data)
        saved_messages, saved_run_output = _normalize_stored_messages(record.get("messages"))

        # If provider record exists but lacks rich continuation payload
        # (e.g., older schema or messages column dropped during compatibility retry),
        # hydrate from global in-memory shadow copy written at pause time.
        if saved_run_output is None or saved_messages is None:
            shadow_pending = await _ensure_global_memory_storage().get_pending_run(run_id)
            if isinstance(shadow_pending, dict):
                if saved_messages is None:
                    saved_messages = shadow_pending.get("messages")
                if saved_run_output is None:
                    saved_run_output = shadow_pending.get("run_output")
        return {
            "requirements": requirements,
            "messages": saved_messages,
            "run_output": saved_run_output,
            "record": record,
        }

    async def delete_pending_run(self, run_id: str) -> bool:
        if run_id in self._store:
            self._store.pop(run_id, None)
            return True
        return False

    async def mark_as_submitted(self, run_id: str) -> bool:
        record = self._store.get(run_id)
        if not record:
            return False
        record["status"] = "submitted"
        record["submitted_at"] = _utc_now_iso()
        self._store[run_id] = record
        return True

    async def cleanup_expired_runs(self) -> int:
        # Expiration cleanup disabled by design:
        # keep pending HITL runs until submit completes or conversation is removed.
        return 0


class DbHITLStorage:
    """Database-backed HITL storage via provider adapter (supabase/sqlite)."""

    TABLE_NAME = "pending_form_runs"

    def __init__(self, provider: ProviderConfig) -> None:
        self.provider = provider
        self.adapter = build_adapter(provider)
        self._memory_fallback = InMemoryHITLStorage()
        self._use_memory_fallback = False

    async def save_pending_run(
        self,
        run_id: str,
        requirements: list[Any],
        conversation_id: str | None = None,
        user_id: str | None = None,
        agent_model: str | None = None,
        ttl_minutes: int = 30,
        messages: list[dict[str, Any]] | None = None,
        run_output: dict[str, Any] | None = None,
    ) -> dict[str, Any] | None:
        # Global shadow copy: protects against provider-id mismatch between
        # pause and submit when provider-backed storage fell back to memory.
        await _ensure_global_memory_storage().save_pending_run(
            run_id=run_id,
            requirements=requirements,
            conversation_id=conversation_id,
            user_id=user_id,
            agent_model=agent_model,
            ttl_minutes=ttl_minutes,
            messages=messages,
            run_output=run_output,
        )

        if self._use_memory_fallback:
            return await self._memory_fallback.save_pending_run(
                run_id=run_id,
                requirements=requirements,
                conversation_id=conversation_id,
                user_id=user_id,
                agent_model=agent_model,
                ttl_minutes=ttl_minutes,
                messages=messages,
                run_output=run_output,
            )

        requirements_data = serialize_requirements(requirements)
        now = _utc_now()
        expires_at = (now + timedelta(minutes=ttl_minutes)).replace(microsecond=0).isoformat()
        normalized_conversation_id = conversation_id
        normalized_user_id = user_id
        if self.provider.type == "supabase":
            # Supabase schema often defines these columns as UUID.
            normalized_conversation_id = _to_uuid_or_none(conversation_id)
            normalized_user_id = _to_uuid_or_none(user_id)
        payload: dict[str, Any] = {
            "run_id": run_id,
            "requirements_data": requirements_data,
            "expires_at": expires_at,
            "status": "pending",
            "conversation_id": normalized_conversation_id,
            "user_id": normalized_user_id,
            "agent_model": agent_model,
            "submitted_at": None,
        }
        if messages is not None or run_output is not None:
            payload["messages"] = {
                "history": messages,
                "run_output": run_output,
            }

        req = DbQueryRequest(
            providerId=self.provider.id,
            action="upsert",
            table=self.TABLE_NAME,
            values=payload,
            onConflict=["run_id"],
        )
        result = await execute_db_async(self.adapter, req)
        if result.error:
            # Retry with progressively reduced payload for older schemas.
            # Drop explicitly missing columns reported by provider error.
            for _ in range(4):
                if not result.error:
                    break
                missing_col = _extract_missing_column_from_error(result.error)
                if missing_col and missing_col in payload:
                    payload.pop(missing_col, None)
                elif "messages" in payload:
                    # Older deployments may still miss messages json column.
                    payload.pop("messages", None)
                else:
                    break

                req = DbQueryRequest(
                    providerId=self.provider.id,
                    action="upsert",
                    table=self.TABLE_NAME,
                    values=payload,
                    onConflict=["run_id"],
                )
                result = await execute_db_async(self.adapter, req)

            if result.error:
                error_text = str(result.error or "").lower()
                if _is_missing_pending_form_table_error(result.error) or "42p10" in error_text:
                    self._use_memory_fallback = True
                    logger.warning(
                        "[HITL] Table pending_form_runs missing/incompatible in provider=%s (error=%s); switched to in-memory fallback",
                        self.provider.id,
                        error_text,
                    )
                    return await self._memory_fallback.save_pending_run(
                        run_id=run_id,
                        requirements=requirements,
                        conversation_id=conversation_id,
                        user_id=user_id,
                        agent_model=agent_model,
                        ttl_minutes=ttl_minutes,
                        messages=messages,
                        run_output=run_output,
                    )
                logger.error(
                    "[HITL] Failed to persist pending run %s in provider=%s: %s",
                    run_id,
                    self.provider.id,
                    result.error,
                )
                return None
        logger.info("[HITL] Stored pending run in provider=%s: %s", self.provider.id, run_id)
        return payload

    async def get_pending_run(self, run_id: str) -> dict[str, Any] | None:
        if self._use_memory_fallback:
            local_pending = await self._memory_fallback.get_pending_run(run_id)
            if local_pending:
                return local_pending
            return await _ensure_global_memory_storage().get_pending_run(run_id)

        req = DbQueryRequest(
            providerId=self.provider.id,
            action="select",
            table=self.TABLE_NAME,
            filters=[DbFilter(op="eq", column="run_id", value=run_id)],
            limit=1,
            maybeSingle=True,
        )
        result = await execute_db_async(self.adapter, req)
        if result.error:
            if _is_missing_pending_form_table_error(result.error):
                self._use_memory_fallback = True
                logger.warning(
                    "[HITL] Table pending_form_runs missing in provider=%s; switched to in-memory fallback",
                    self.provider.id,
                )
                return await self._memory_fallback.get_pending_run(run_id)
            logger.error(
                "[HITL] Failed to load pending run %s from provider=%s: %s",
                run_id,
                self.provider.id,
                result.error,
            )
            return None
        record = result.data if isinstance(result.data, dict) else None
        if not record:
            logger.warning("[HITL] Pending run %s not found in provider=%s", run_id, self.provider.id)
            return await _ensure_global_memory_storage().get_pending_run(run_id)

        requirements_data = record.get("requirements_data") or []
        requirements = deserialize_requirements(requirements_data)
        saved_messages, saved_run_output = _normalize_stored_messages(record.get("messages"))
        return {
            "requirements": requirements,
            "messages": saved_messages,
            "run_output": saved_run_output,
            "record": record,
        }

    async def delete_pending_run(self, run_id: str) -> bool:
        global_deleted = await _ensure_global_memory_storage().delete_pending_run(run_id)

        if self._use_memory_fallback:
            local_deleted = await self._memory_fallback.delete_pending_run(run_id)
            return local_deleted or global_deleted

        req = DbQueryRequest(
            providerId=self.provider.id,
            action="delete",
            table=self.TABLE_NAME,
            filters=[DbFilter(op="eq", column="run_id", value=run_id)],
        )
        result = await execute_db_async(self.adapter, req)
        if result.error:
            if _is_missing_pending_form_table_error(result.error):
                self._use_memory_fallback = True
                logger.warning(
                    "[HITL] Table pending_form_runs missing in provider=%s; switched to in-memory fallback",
                    self.provider.id,
                )
                local_deleted = await self._memory_fallback.delete_pending_run(run_id)
                return local_deleted or global_deleted
            logger.error(
                "[HITL] Failed to delete pending run %s in provider=%s: %s",
                run_id,
                self.provider.id,
                result.error,
            )
            return global_deleted
        return True

    async def mark_as_submitted(self, run_id: str) -> bool:
        await _ensure_global_memory_storage().mark_as_submitted(run_id)

        if self._use_memory_fallback:
            return await self._memory_fallback.mark_as_submitted(run_id)

        req = DbQueryRequest(
            providerId=self.provider.id,
            action="update",
            table=self.TABLE_NAME,
            payload={"status": "submitted", "submitted_at": _utc_now_iso()},
            filters=[DbFilter(op="eq", column="run_id", value=run_id)],
        )
        result = await execute_db_async(self.adapter, req)
        if result.error:
            # Backward compatibility: some deployments do not have submitted_at.
            missing_col = _extract_missing_column_from_error(result.error)
            if missing_col == "submitted_at":
                req = DbQueryRequest(
                    providerId=self.provider.id,
                    action="update",
                    table=self.TABLE_NAME,
                    payload={"status": "submitted"},
                    filters=[DbFilter(op="eq", column="run_id", value=run_id)],
                )
                result = await execute_db_async(self.adapter, req)

        if result.error:
            if _is_missing_pending_form_table_error(result.error):
                self._use_memory_fallback = True
                logger.warning(
                    "[HITL] Table pending_form_runs missing in provider=%s; switched to in-memory fallback",
                    self.provider.id,
                )
                return await self._memory_fallback.mark_as_submitted(run_id)
            logger.error(
                "[HITL] Failed to mark submitted run %s in provider=%s: %s",
                run_id,
                self.provider.id,
                result.error,
            )
            return False
        return True

    async def cleanup_expired_runs(self) -> int:
        if self._use_memory_fallback:
            return await self._memory_fallback.cleanup_expired_runs()
        # Expiration cleanup disabled by design:
        # keep pending HITL runs until submit completes or conversation is removed.
        return 0


_memory_storage: InMemoryHITLStorage | None = None
_provider_storages: dict[str, DbHITLStorage] = {}


def _ensure_global_memory_storage() -> InMemoryHITLStorage:
    global _memory_storage
    if _memory_storage is None:
        _memory_storage = InMemoryHITLStorage()
    return _memory_storage


def _resolve_provider(provider_id_or_type: str | None) -> ProviderConfig | None:
    registry = get_provider_registry()
    providers = registry.list()
    if not providers:
        return None

    active = providers[0]
    if provider_id_or_type:
        raw = str(provider_id_or_type).strip()
        if raw:
            by_id = registry.get(raw)
            if by_id:
                return by_id
            provider_type = normalize_provider_type(raw)
            if provider_type == active.type:
                return active
            return None
    return active


def get_hitl_storage(provider_id_or_type: str | None = None) -> DbHITLStorage | InMemoryHITLStorage:
    """
    Return HITL storage based on provider.

    Priority:
    1) Explicit provider id/type matching the active database
    2) Active configured provider
    3) In-memory fallback
    """
    provider = _resolve_provider(provider_id_or_type)
    if provider is None:
        logger.warning("[HITL] No DB provider configured; using in-memory storage")
        return _ensure_global_memory_storage()

    storage = _provider_storages.get(provider.id)
    if storage is None:
        try:
            storage = DbHITLStorage(provider)
            _provider_storages[provider.id] = storage
            logger.info("[HITL] Using provider-backed storage: %s (%s)", provider.id, provider.type)
        except Exception as exc:
            logger.error(
                "[HITL] Failed to initialize provider storage (%s): %s; fallback to memory",
                provider.id,
                exc,
            )
            return _ensure_global_memory_storage()
    return storage