Spaces:
Runtime error

File size: 35,732 Bytes
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
"""
Node Executor β€” per-turn logic for a single workflow node.

Called by the orchestrator when session mode == WORKFLOW.  Decides between two paths:

  A. Slots complete β†’ fire auto_actions via execute_with_recovery β†’ evaluate edges
                     β†’ advance node or end workflow.
                     LLM receives only a confirmation directive (no business tools).

  B. Slots incomplete β†’ inject missing-slots directive and node-scoped tool list.
                        LLM collects the remaining variables conversationally.

Auto-actions fire through the existing execute_with_recovery + execute_tool pipeline,
keeping idempotency, retry, and timeout semantics intact.

Edge evaluation uses WorkflowNode.get_edge_target(fast_response_key) β€” the node graph
edges are the new replacement for the flat STEP_TRANSITIONS table.  The flat table
remains untouched and used by the legacy fintech workflow.
"""
from __future__ import annotations

import logging
import re
from dataclasses import dataclass, field
from typing import Callable, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from session.call_session import CallSession
    from workflow.node_schema import AgentProfile, WorkflowNode, WorkflowDefinition

logger = logging.getLogger(__name__)

_WORKFLOW_END_SENTINEL = "__end__"

# Map granular tool fast_response_keys to canonical edge conditions.
# Edge conditions in workflow definitions use short readable names (success,
# not_found, invalid_otp…). Tools return more specific keys (account_verified,
# otp_wrong…). This table normalizes them before edge lookup so every workflow
# definition can use the same canonical set without knowing tool internals.
_FAST_KEY_ALIASES: dict[str, str] = {
    # verify_account
    "account_verified":      "success",
    "account_not_found":     "not_found",
    # send_otp
    "otp_sent":              "success",
    # verify_otp
    "otp_verified":          "success",
    "otp_wrong":             "invalid_otp",
    "otp_expired":           "invalid_otp",
    "otp_max_attempts":      "otp_locked",
    # lookup_transaction / search_transactions
    "transaction_found":     "success",
    "transaction_not_found": "not_found",
    # initiate_refund
    "refund_initiated":           "success",
    "refund_ineligible":          "failure",
    "refund_already_initiated":   "already_refunded",
    "refund_already_completed":   "already_refunded",
    # raise_dispute
    "dispute_filed":              "success",
    "dispute_duplicate":          "already_filed",
    # report_fraud
    "fraud_reported":             "success",
    "fraud_already_open":         "success",
    "fraud_already_reported":     "success",
    # shared escalation triggers
    "fraud_review_required":      "escalate",
    "kyc_escalation_required":    "escalate",
    "verification_required":      "escalate",
    # unlock_account
    "account_unlocked":           "success",
    # escalate_to_human
    "escalating":                 "escalate",
    "already_escalated":          "escalate",
}


def _normalize_fast_key(key: str) -> str:
    """Return canonical edge condition for a given tool fast_response_key."""
    return _FAST_KEY_ALIASES.get(key, key)


# ── Slot type validators ───────────────────────────────────────────────────────
# Hard guards that prevent cross-slot contamination (OTP used as TXN, etc.).
# Keyed by slot name; value is compiled regex that the slot value must fully match.

_SLOT_VALIDATORS: dict[str, re.Pattern] = {
    "phone":  re.compile(r"^\+?\d{10,13}$"),
    "otp":    re.compile(r"^\d{4,8}$"),
    "txn_id": re.compile(r"^TXN-\d{4,}$", re.IGNORECASE),
}

# Human-readable prompts the agent gives when a slot fails validation
_SLOT_REPROMPT: dict[str, str] = {
    "phone":  "I didn't catch a valid mobile number β€” could you repeat your 10-digit registered number?",
    "otp":    "I didn't catch a valid verification code β€” could you read out the digits again?",
    "txn_id": "I didn't catch a valid transaction reference β€” could you give me the TXN number? It starts with TXN followed by digits.",
}


def _validate_slots(slots, node: "WorkflowNode") -> Optional[str]:
    """Validate slot values against their expected formats.

    Returns a corrective directive string if any required slot is malformed,
    or None if all slots pass.  On failure, the caller should clear the bad
    slot and return the directive without firing the auto_action.
    """
    # Map slot name to current value
    slot_values = {
        "phone":  slots.phone,
        "otp":    slots.otp,
        "txn_id": slots.txn_id,
    }
    for slot_name, pattern in _SLOT_VALIDATORS.items():
        value = slot_values.get(slot_name)
        if value is None:
            continue  # slot not set β€” skip (slots_complete already checked required slots)
        if not pattern.fullmatch(str(value)):
            logger.warning(
                "node_executor: slot_validator rejected %s=%r β€” clearing and re-prompting",
                slot_name, value,
            )
            # Clear the bad value so the extractor doesn't re-use it
            if slot_name == "phone":
                slots.clear_phone()
            elif slot_name == "otp":
                slots.clear_otp()
            elif slot_name == "txn_id":
                slots.clear_txn_id()
            return _SLOT_REPROMPT.get(slot_name, "Could you repeat that?")
    return None


@dataclass
class NodeAdvanceResult:
    directive:          str
    tools_for_llm:      list                 # empty list when auto-action already fired
    context_additions:  list[dict]           # action result messages injected into LLM ctx
    agent_profile:      Optional["AgentProfile"] = None
    workflow_done:      bool = False
    # When True, the caller should compress the verification exchange (phone + OTP turns)
    # in conversation history down to a single clean "[Identity confirmed: Name]" turn.
    compress_verification_history: bool = False


async def advance(
    text: str,
    session: "CallSession",
    all_tools: list,
    publish_event: Callable,
) -> NodeAdvanceResult:
    """Single entry point called by the orchestrator per user turn."""
    state = session.orchestrator_state

    wf_def  = _load_workflow(state.active_workflow_id)
    if wf_def is None:
        logger.error("node_executor: workflow %s not found; exiting workflow", state.active_workflow_id)
        state.exit_workflow()
        return NodeAdvanceResult(
            directive="I've encountered an internal issue. Let me help you in a moment.",
            tools_for_llm=[],
            context_additions=[],
            workflow_done=True,
        )

    node = wf_def.get_node(state.active_node_id)
    if node is None:
        logger.error(
            "node_executor: node %s not in workflow %s; exiting",
            state.active_node_id, state.active_workflow_id,
        )
        state.exit_workflow()
        return NodeAdvanceResult(
            directive="Something went wrong navigating the workflow.",
            tools_for_llm=[],
            context_additions=[],
            workflow_done=True,
        )

    # Branch / end nodes: pure routing, no LLM turn needed, but we still surface
    # the directive so the LLM can acknowledge in natural speech.
    if node.node_type == "end":
        state.exit_workflow()
        return NodeAdvanceResult(
            directive=node.directive or "We're all set.",
            tools_for_llm=[],
            context_additions=[],
            workflow_done=True,
        )

    slots = state.entity_slots

    # Pre-fill txn_id from customer profile when the node needs one but the entity
    # extractor hasn't captured one from speech yet.  Fires when the customer has
    # exactly ONE failed/refund-eligible transaction β€” avoids making them speak
    # "TXN-XXXX" explicitly when the account lookup already identified it.
    # Multiple candidates: returns None β†’ agent asks customer to clarify which one.
    if "txn_id" in node.variables and not slots.txn_id:
        inferred = _infer_txn_from_profile(session)
        if inferred:
            slots.txn_id = inferred
            logger.debug(
                "node_executor: pre-filled txn_id=%s from customer profile (single eligible txn)",
                inferred,
            )

    # LLM-action node advancement: if a prior LLM tool call stored a fast_response_key,
    # try to advance using it.  Only advance if the key maps to an EXISTING edge on the
    # current node β€” unmatched keys (e.g. query tools like get_refund_status) are
    # silently dropped and the node stays active so the LLM can continue the conversation.
    if state.pending_tool_result is not None:
        tool_name, raw_key = state.pending_tool_result
        state.pending_tool_result = None
        fast_key     = _normalize_fast_key(raw_key)
        # Use STRICT edge lookup (no fallback) β€” only advance if there's an exact match.
        # The catch-all "success" fallback in get_edge_target() is too aggressive here:
        # query tools (get_refund_status, search_transactions) return keys like
        # "refund_processing" that have no defined edge and should keep the node alive.
        next_node_id = _get_edge_exact(node, fast_key)
        logger.info(
            "node_executor: pending_tool_result tool=%s key=%s→%s next=%s",
            tool_name, raw_key, fast_key, next_node_id,
        )
        if next_node_id is not None:
            # Matched edge β€” advance to next node or end workflow
            if next_node_id == _WORKFLOW_END_SENTINEL:
                state.exit_workflow()
                return NodeAdvanceResult(
                    directive=_resolve_outcome_directive(node, fast_key),
                    tools_for_llm=[],
                    context_additions=[],
                    workflow_done=True,
                )
            state.advance_to_node(next_node_id)
            next_node = wf_def.get_node(next_node_id)
            if next_node and next_node.node_type == "collect":
                for slot_name in next_node.variables:
                    _clear_slot(slot_name, state.entity_slots)
            directive    = next_node.directive if next_node else _resolve_outcome_directive(node, fast_key)
            scoped_tools = (
                [t for t in all_tools if _tool_name(t) in next_node.allowed_tools]
                if next_node else []
            )
            return NodeAdvanceResult(
                directive=directive,
                tools_for_llm=scoped_tools,
                context_additions=[],
                agent_profile=next_node.agent_profile if next_node else None,
            )
        # No matching edge β€” query tool result; fall through so LLM stays at this node

    # Path A: all required slots filled AND auto_actions defined β†’ fire auto-actions
    # Nodes with no auto_actions are LLM-driven (Path B always) β€” do not auto-advance.
    if _slots_complete(node, slots) and node.auto_actions:
        return await _handle_auto_actions(node, wf_def, session, all_tools, publish_event)

    # Path B: waiting for slots β†’ provide directive + node-scoped tools
    state.node_attempts += 1
    if node.max_attempts and state.node_attempts > node.max_attempts:
        timeout_edge = node.on_timeout_edge or "timeout"
        next_node_id = node.get_edge_target(timeout_edge)
        if next_node_id and next_node_id != _WORKFLOW_END_SENTINEL:
            state.advance_to_node(next_node_id)
            return NodeAdvanceResult(
                directive="We weren't able to collect the required information. Let me assist you differently.",
                tools_for_llm=[],
                context_additions=[],
            )
        state.exit_workflow()
        return NodeAdvanceResult(
            directive="We weren't able to complete this step. Is there anything else I can help you with?",
            tools_for_llm=[],
            context_additions=[],
            workflow_done=True,
        )

    missing  = _missing_slot_names(node, slots)
    directive = node.directive
    if missing:
        directive = f"{directive} (still need: {', '.join(missing)})"

    scoped_tools = [t for t in all_tools if _tool_name(t) in node.allowed_tools]

    return NodeAdvanceResult(
        directive=directive,
        tools_for_llm=scoped_tools,
        context_additions=[],
        agent_profile=node.agent_profile,
    )


# ── Auto-action path ──────────────────────────────────────────────────────────

async def _handle_auto_actions(
    node: "WorkflowNode",
    wf_def: "WorkflowDefinition",
    session: "CallSession",
    all_tools: list,
    publish_event: Callable,
    _depth: int = 0,
) -> NodeAdvanceResult:
    from voice.tool_recovery import execute_with_recovery

    state   = session.orchestrator_state
    slots   = state.entity_slots
    context_additions: list[dict] = []
    last_fast_key = "success"
    _compress_verification = False
    _directive_template_vars: dict[str, str] = {}  # filled by action handlers, applied to directive

    # Validate slot formats before firing any auto_action.
    # Prevents cross-slot contamination (OTP digits used as TXN ID, etc.).
    _validation_error = _validate_slots(slots, node)
    if _validation_error:
        return NodeAdvanceResult(
            directive=_validation_error,
            tools_for_llm=[],
            context_additions=[],
        )

    for action_name in node.auto_actions:
        args = _build_action_args(action_name, slots, state.node_variables, session)
        logger.info(
            "node_executor: firing auto_action=%s node=%s",
            action_name, node.id,
        )

        tool_result = await execute_with_recovery(
            tool_name=action_name,
            args=args,
            call_id=session.call_id,
            turn_id=session.turn_counter,
            step_id=f"{node.id}/{action_name}",
            session=session,
        )

        raw_key    = tool_result.fast_response_key or ("success" if tool_result.success else "failure")
        fast_key   = _normalize_fast_key(raw_key)
        last_fast_key = fast_key
        if raw_key != fast_key:
            logger.debug(
                "node_executor: fast_key normalized %r β†’ %r (node=%s)",
                raw_key, fast_key, node.id,
            )

        # Publish tool_call event so browser UI reflects the action
        try:
            await publish_event({
                "type":   "tool_call",
                "tool":   action_name,
                "status": "done",
                "result": {"fast_response_key": fast_key},
            })
        except Exception:
            pass

        # Publish otp_sent event so the frontend can surface the OTP code to the customer
        if action_name == "send_otp" and fast_key == "success":
            otp_code = tool_result.data.get("otp")
            if otp_code:
                try:
                    await publish_event({"type": "otp_sent", "otp": otp_code})
                except Exception:
                    pass

        # Publish otp_verified so the frontend clears the displayed OTP code.
        # Also mark the session as verified so subsequent workflows in this call
        # skip the collect_phone β†’ send_otp β†’ verify_otp nodes entirely.
        if action_name == "verify_otp" and fast_key == "success":
            state.session_verified = True
            _compress_verification = True   # signal to caller to rewrite history
            logger.info(
                "node_executor: session_verified=True β€” identity confirmed for this call",
            )
            try:
                await publish_event({"type": "otp_verified"})
            except Exception:
                pass

        # After verify_account succeeds, inject the full customer profile into
        # LLM context so the agent uses the DB-stored name and data immediately β€”
        # never the name the customer stated β€” and never needs to re-query the DB.
        if action_name == "verify_account" and fast_key == "success":
            profile = getattr(session, "customer_profile", None) or {}
            if profile:
                context_additions.append({
                    "role": "developer",
                    "content": _build_customer_context_message(profile),
                })
        elif action_name == "send_otp" and fast_key == "success":
            # Phone number was just accepted and OTP dispatched.
            # Give one clear, prescriptive response β€” no numbered instructions that
            # the LLM might try to reconcile with the phone digits in chat history.
            context_additions.append({
                "role": "developer",
                "content": (
                    "[Account found β€” OTP sent]: Account located. "
                    "A 6-digit code was sent to their registered number. "
                    "Say exactly: 'I found your account β€” I've sent a 6-digit verification code "
                    "to your registered number. Please read it out when you receive it.' "
                    "Say nothing else. Do not reference the digits the customer just provided."
                ),
            })
        elif action_name == "initiate_refund":
            rfn = tool_result.data.get("rfn_number", "")
            if fast_key == "success" and rfn:
                _directive_template_vars["rfn_number"] = rfn
                context_additions.append({
                    "role": "developer",
                    "content": (
                        f"[REFERENCE CONFIRMED β€” READ VERBATIM]: {rfn}. "
                        f"Say exactly: 'Your refund reference is {rfn}.' "
                        f"Do not rephrase, abbreviate, or generate a different reference number. "
                        f"Add: 'It should arrive within 3 to 5 business days.'"
                    ),
                })
            elif fast_key == "already_refunded":
                existing = tool_result.data.get("refund_case_id", "")
                context_additions.append({
                    "role": "developer",
                    "content": (
                        "[Refund already exists"
                        + (f" β€” reference: {existing}" if existing else "")
                        + "]: A refund for this transaction is already in progress. "
                        "Tell the customer their refund is already being processed and "
                        "give them the existing reference number if available."
                    ),
                })
            elif fast_key in ("failure", "ineligible"):
                reason = tool_result.data.get("reason", "")
                context_additions.append({
                    "role": "developer",
                    "content": (
                        "[Refund not eligible"
                        + (f": {reason}" if reason else "")
                        + "]: This transaction cannot be refunded. "
                        "Briefly explain and offer to connect the customer with a specialist."
                    ),
                })
            elif tool_result.data:
                context_additions.append({
                    "role":    "developer",
                    "content": f"[auto_action:{action_name}] result_key={fast_key}",
                })
        elif action_name == "raise_dispute":
            dsp = tool_result.data.get("dsp_number", "")
            if fast_key == "dispute_filed" and dsp:
                _directive_template_vars["dsp_number"] = dsp
                context_additions.append({
                    "role": "developer",
                    "content": (
                        f"[REFERENCE CONFIRMED β€” READ VERBATIM]: {dsp}. "
                        f"Say exactly: 'Your dispute reference is {dsp}.' "
                        f"Do not rephrase, abbreviate, or generate a different reference number. "
                        f"Add: 'Our team will investigate within 3 to 5 business days.'"
                    ),
                })
            elif tool_result.data:
                context_additions.append({
                    "role":    "developer",
                    "content": f"[auto_action:{action_name}] result_key={fast_key}",
                })
        elif action_name == "report_fraud":
            fraud_num = tool_result.data.get("fraud_number", "")
            if fast_key == "fraud_case_opened" and fraud_num:
                _directive_template_vars["fraud_number"] = fraud_num
                context_additions.append({
                    "role": "developer",
                    "content": (
                        f"[REFERENCE CONFIRMED β€” READ VERBATIM]: {fraud_num}. "
                        f"Say exactly: 'Your fraud case reference is {fraud_num}.' "
                        f"Do not rephrase, abbreviate, or generate a different reference number. "
                        f"Add: 'Our fraud team will review this within 24 hours.'"
                    ),
                })
            elif tool_result.data:
                context_additions.append({
                    "role":    "developer",
                    "content": f"[auto_action:{action_name}] result_key={fast_key}",
                })
        elif tool_result.data:
            context_additions.append({
                "role":    "developer",
                "content": f"[auto_action:{action_name}] result_key={fast_key}",
            })

        # If the action indicates the call should escalate, honour it immediately
        if fast_key == "escalate":
            state.enter_escalation()
            return NodeAdvanceResult(
                directive="Let me connect you with a specialist right away.",
                tools_for_llm=[],
                context_additions=context_additions,
                workflow_done=False,
            )

    # Edge evaluation: which node do we move to?
    next_node_id = node.get_edge_target(last_fast_key)

    if next_node_id is None or next_node_id == _WORKFLOW_END_SENTINEL:
        # Workflow complete β€” prefer the node's own directive (it describes the outcome)
        # over the generic _resolve_outcome_directive fallback.
        state.exit_workflow()
        directive = node.directive or _resolve_outcome_directive(node, last_fast_key)
        # Substitute any [placeholder] tokens that were populated by action handlers
        # (e.g. [dsp_number], [rfn_number], [fraud_number]).  Without this the LLM
        # sees the literal placeholder and invents a value by pattern-matching against
        # known IDs in its context (e.g. TXN-3300 β†’ DSP-3300).
        for key, val in _directive_template_vars.items():
            directive = directive.replace(f"[{key}]", val)
        return NodeAdvanceResult(
            directive=directive,
            tools_for_llm=[],
            context_additions=context_additions,
            workflow_done=True,
        )

    # Advance to next node within the same workflow
    state.advance_to_node(next_node_id)
    next_node = wf_def.get_node(next_node_id)

    # When retrying a collect node (e.g. wrong phone / wrong OTP), clear the
    # slot the node collects so _slots_complete doesn't fire again immediately
    # with the bad value.
    if next_node and next_node.node_type == "collect":
        for slot_name in next_node.variables:
            _clear_slot(slot_name, state.entity_slots)
            logger.debug(
                "node_executor: cleared slot %r for retry at node %s",
                slot_name, next_node_id,
            )

    # Cascade: if the new node is also immediately auto-completable (action/inform node
    # with no required variables and has auto_actions), fire it in the SAME turn instead
    # of waiting for the next customer utterance.  This is what makes send_otp fire
    # immediately after verify_account rather than waiting 26 seconds for the customer
    # to ask "Have you sent the OTP?".  Depth guard prevents runaway chains.
    if (
        _depth < 3
        and next_node is not None
        and next_node.auto_actions
        and next_node.node_type not in ("collect", "end")
        and _slots_complete(next_node, state.entity_slots)
    ):
        logger.info(
            "node_executor: cascading into node=%s depth=%d",
            next_node_id, _depth + 1,
        )
        cascade = await _handle_auto_actions(
            next_node, wf_def, session, all_tools, publish_event, _depth + 1
        )
        cascade.context_additions = context_additions + cascade.context_additions
        if _compress_verification:
            cascade.compress_verification_history = True
        return cascade

    directive    = next_node.directive if next_node else _resolve_outcome_directive(node, last_fast_key)
    scoped_tools = (
        [t for t in all_tools if _tool_name(t) in next_node.allowed_tools]
        if next_node else []
    )
    return NodeAdvanceResult(
        directive=directive,
        tools_for_llm=scoped_tools,
        context_additions=context_additions,
        agent_profile=next_node.agent_profile if next_node else None,
        compress_verification_history=_compress_verification,
    )


# ── Helpers ───────────────────────────────────────────────────────────────────

def _get_edge_exact(node: "WorkflowNode", condition: str) -> Optional[str]:
    """Strict edge lookup β€” only returns a target if an EXACT condition match exists.

    Unlike WorkflowNode.get_edge_target(), this never falls back to the 'success'
    catch-all.  Used for LLM-action node advancement so that query tools whose
    fast_response_key has no declared edge keep the node alive instead of
    accidentally matching the generic success→__end__ edge.
    """
    for edge in node.edges:
        if edge.condition == condition:
            return edge.target_node_id
    return None


def _slots_complete(node: "WorkflowNode", slots) -> bool:
    for slot_name, var_def in node.variables.items():
        if not var_def.required:
            continue
        value = _get_slot_value(slot_name, slots)
        if not value:
            return False
        if var_def.min_length and len(str(value)) < var_def.min_length:
            return False
    return True


def _missing_slot_names(node: "WorkflowNode", slots) -> list[str]:
    missing = []
    for slot_name, var_def in node.variables.items():
        if not var_def.required:
            continue
        if not _get_slot_value(slot_name, slots):
            missing.append(slot_name)
    return missing


def _get_slot_value(slot_name: str, slots) -> Optional[str]:
    """Map canonical slot names to EntitySlots fields."""
    if slot_name == "phone":
        return slots.phone
    if slot_name == "otp":
        return slots.otp
    if slot_name in ("txn_id", "transaction_id"):
        return slots.txn_id
    return None


def _clear_slot(slot_name: str, slots) -> None:
    """Clear a slot so the entity extractor can re-fill it on the next utterance."""
    if slot_name == "phone":
        slots.phone = None
    elif slot_name == "otp":
        slots.otp = None
    elif slot_name in ("txn_id", "transaction_id"):
        slots.txn_id = None


def _build_action_args(
    action_name: str,
    slots,
    node_variables: dict,
    session: "CallSession",
) -> dict:
    """Construct the args dict for the tool call from accumulated entity slots."""
    base: dict = {}

    # Standard slot mapping
    if slots.phone:
        base["phone"] = slots.phone
    if slots.otp:
        base["otp_code"] = slots.otp
    if slots.txn_id:
        base["transaction_id"] = slots.txn_id

    # Session-level overrides
    if session.customer_id:
        base["customer_id"] = session.customer_id

    # Action-specific defaults (parameters the entity extractor doesn't collect)
    if action_name == "raise_dispute" and "reason" not in base:
        base["reason"] = node_variables.get("dispute_reason", "Customer disputed the charge")
    if action_name == "report_fraud" and "fraud_type" not in base:
        base["fraud_type"] = node_variables.get("fraud_type", "unauthorized_transaction")

    # Merge any additional node-level variables collected by LLM tools
    base.update(node_variables)

    return base


def _resolve_outcome_directive(node: "WorkflowNode", fast_key: str) -> str:
    if fast_key == "success":
        return (
            "The issue has been resolved. Wrap up warmly using the customer's first name "
            "(e.g. 'All sorted, Ayaan β€” is there anything else I can help you with today?'). "
            "If they say no / goodbye / thank you, wish them well and end naturally. "
            "Do NOT offer to connect them with a specialist or agent."
        )
    if fast_key == "failure":
        return "I wasn't able to complete that step. Let me know how else I can help."
    if fast_key == "not_found":
        return "I couldn't find the information needed. Can you double-check the details?"
    return "We've reached the end of this process. How else can I assist you?"


def _tool_name(tool) -> str:
    """Extract the tool name from whatever the LiveKit SDK tool object looks like."""
    try:
        return tool.info.name
    except AttributeError:
        try:
            return tool.__name__
        except AttributeError:
            return str(tool)


_PROBLEMATIC_TXN_STATUSES = frozenset({
    "failed", "kyc_hold", "compliance_hold", "flagged",
    "fraud_reported", "fraud_confirmed",
    "disputed", "chargeback_initiated",
    "refund_initiated", "refund_processing",
    "pending", "processing",
})

# Statuses that a customer would legitimately want to refund.
# Subset of _PROBLEMATIC_TXN_STATUSES β€” only statuses where a new refund can be raised.
_REFUND_ELIGIBLE_STATUSES = frozenset({"failed", "kyc_hold", "compliance_hold"})


def _infer_txn_from_profile(session: "CallSession") -> Optional[str]:
    """Return the TXN number if the customer profile has exactly ONE refund-eligible transaction.

    Used to auto-fill the txn_id slot so the customer doesn't need to say "TXN-XXXX"
    when the account lookup already identified the only problematic transaction.
    Returns None when zero or multiple candidates exist β€” agent must ask the customer.
    """
    profile = getattr(session, "customer_profile", None)
    if not profile:
        return None
    transactions = profile.get("transactions") or []
    candidates = [
        t for t in transactions
        if t.get("status") in _REFUND_ELIGIBLE_STATUSES and t.get("txn_number")
    ]
    if len(candidates) == 1:
        return candidates[0]["txn_number"]
    return None


def _build_customer_context_message(profile: dict) -> str:
    """Inject customer identity + account diagnostic block after verify_account.

    Includes problematic transaction statuses, holds, and fraud cases so the LLM
    can immediately address the customer's issue without an extra tool call.
    Specific records (amounts, merchants) are surfaced here; full details available
    on demand via search_transactions / get_refund_status / get_dispute_status.
    """
    name        = profile.get("name", "")
    first_name  = profile.get("first_name", name.split()[0] if name else "customer")
    acct_type   = profile.get("account_type", "standard")
    acct_status = profile.get("account_status", "active")
    kyc         = profile.get("kyc_status", "pending")

    transactions    = profile.get("transactions") or []
    open_refunds    = profile.get("open_refunds") or []
    open_disputes   = profile.get("open_disputes") or []
    account_holds   = profile.get("account_holds") or []
    fraud_cases     = profile.get("active_fraud_cases") or []

    lines = [
        f"MANDATORY NAME RULE: Call this customer '{first_name}' in EVERY response β€” no exceptions.",
        "[Account data β€” identity already confirmed; do NOT say 'you are verified' or 'verification complete']",
        f"Name on account: {name} (use first name '{first_name}' in all speech).",
        f"Account: {acct_type} | status: {acct_status} | KYC: {kyc}",
    ]

    # Surface account-level alerts first (locked/frozen/fraud hold)
    if acct_status in ("locked", "frozen", "suspended"):
        lock_reason = profile.get("account_locked_reason", "")
        lines.append(
            f"ACCOUNT IS {acct_status.upper()}"
            + (f": {lock_reason}" if lock_reason else "") + "."
        )

    if profile.get("fraud_hold_active"):
        lines.append("FRAUD HOLD IS ACTIVE on this account.")

    if kyc in ("rejected", "pending"):
        lines.append(f"KYC status is {kyc.upper()} β€” some transactions may be on hold pending verification.")

    # Active account-level holds
    if account_holds:
        for h in account_holds:
            reason = h.get("reason", "")
            lines.append(
                f"Account hold [{h.get('hold_type', 'unknown')}]"
                + (f": {reason}" if reason else "") + "."
            )

    # Problematic transactions (surface these explicitly so LLM can address without a tool call)
    problematic = [t for t in transactions if t.get("status") in _PROBLEMATIC_TXN_STATUSES]
    all_count   = len(transactions)
    prob_count  = len(problematic)

    if problematic:
        lines.append(f"Issue(s) detected β€” {prob_count} of {all_count} transaction(s) need attention:")
        for t in problematic[:5]:
            currency = t.get("currency", "INR")
            symbol   = "β‚Ή" if "INR" in currency else ("$" if "USD" in currency else f"{currency} ")
            amt_str  = f"{symbol}{t['amount']:,.0f}" if t.get("amount") is not None else ""
            lines.append(f"  {t['txn_number']} β€” {t.get('merchant', '?')} {amt_str} β€” {t['status']}")
    else:
        clean = all_count - prob_count
        lines.append(f"No problematic transactions ({clean} transaction(s) all clear).")

    # Refunds and disputes
    if open_refunds:
        lines.append(f"Open refund(s): {len(open_refunds)}")
        for r in open_refunds[:3]:
            lines.append(f"  {r.get('rfn_number')} β€” {r.get('merchant', '?')} β€” {r.get('status')}")

    if open_disputes:
        lines.append(f"Open dispute(s): {len(open_disputes)}")
        for d in open_disputes[:3]:
            lines.append(f"  {d.get('dsp_number')} β€” {d.get('merchant', '?')} β€” {d.get('status')}")

    # Fraud cases
    if fraud_cases:
        lines.append(f"Active fraud case(s): {len(fraud_cases)} β€” handle with care.")
        for fc in fraud_cases[:2]:
            lines.append(f"  {fc.get('fraud_number')} β€” {fc.get('fraud_type')} risk:{fc.get('risk_level')}")
    else:
        lines.append("No active fraud cases. Do not mention or invent any fraud reference.")

    lines.append("Use search_transactions, get_refund_status, or get_dispute_status to retrieve full records when needed.")
    return "\n".join(lines)


def _load_workflow(workflow_id: Optional[str]) -> Optional["WorkflowDefinition"]:
    if not workflow_id:
        return None
    try:
        from config_loader import get_active_workflows
        for wf in get_active_workflows():
            if wf.id == workflow_id:
                return wf
    except Exception:
        logger.exception("node_executor: failed to load workflow %s", workflow_id)
    return None