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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
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
"""tick_graph — per-agent LangGraph pipeline and standalone CLI debug tool.

Defines the perceive -> retrieve_memories -> react -> day_planner ->
write_back_memory subgraph and a day-planning CLI.

Architecture: the graph the engine was designed around; the production
WorldEngine currently implements its own phase loop and calls brain.py
directly, so this module serves as the reference single-agent pipeline.
Design: kept as the canonical per-agent graph for experiments and
debugging; its memory-stream integration is intentionally pluggable.
"""

from __future__ import annotations

import sys
from pathlib import Path

BACKEND_ROOT = Path(__file__).resolve().parents[2]
if str(BACKEND_ROOT) not in sys.path:
    sys.path.append(str(BACKEND_ROOT))


from datetime import date, timedelta
from enum import Enum
from typing import Any, Callable, Dict, List, Optional, Tuple, TypedDict

from langgraph.graph import StateGraph, START, END
from pydantic import BaseModel

from src.core.log import get_logger
from src.core.snapshot import WorldSnapshot
from src.core.world_state import CurrentAction
from src.core.perceive import perceive, Observation
from src.agents.react import decide_reaction, ReactionDecision

logger = get_logger(__name__)


# --------------------------------------------------------------------------- #
# Sim-tick <-> wallclock helpers
# --------------------------------------------------------------------------- #

MINUTES_PER_DAY = 24 * 60


def tick_to_wallclock(tick: int) -> Tuple[int, str]:
    """(sim day index, 'HH:MM') for a given absolute tick."""
    day_index, minute_of_day = divmod(tick, MINUTES_PER_DAY)
    hh, mm = divmod(minute_of_day, 60)
    return day_index, f"{hh:02d}:{mm:02d}"


def hhmm_to_minute(hhmm: str) -> int:
    hh, mm = hhmm.split(":")
    return int(hh) * 60 + int(mm)


# --------------------------------------------------------------------------- #
# Day-planner integration contract
# --------------------------------------------------------------------------- #


class DayPlannerRequest(BaseModel):
    agent_id: str
    persona: Dict[str, Any]
    relevant_memories: List[str]
    yesterday_summary: Optional[str]
    day_index: int
    current_hhmm: str
    interrupt_context: Optional[str] = None


class DayPlanEntry(BaseModel):
    action: str
    start: str
    end: str
    parent_activity: Optional[str] = None
    location_id: Optional[str] = None
    sub_area: Optional[str] = None


DayPlannerFn = Callable[[DayPlannerRequest], List[DayPlanEntry]]


def build_default_day_planner_fn(sim_start_date: Optional[date] = None) -> DayPlannerFn:
    epoch = sim_start_date or date.today()

    def _adapter(request: DayPlannerRequest) -> List[DayPlanEntry]:
        from src.agents.day_planner import run as day_planner_run

        calendar_date = epoch + timedelta(days=request.day_index)
        current_time_str = f"{calendar_date.isoformat()} {request.current_hhmm}"

        memories = list(request.relevant_memories)
        if request.interrupt_context:
            memories.append(f"IMPORTANT -- just happened, account for this: {request.interrupt_context}")

        class _AgentProxy:
            persona = request.persona
            relevant_memories = memories
            yesterday_summary = request.yesterday_summary

        result = day_planner_run(_AgentProxy(), {"current_time": current_time_str, "places": None})
        raw_plan = result.get("day_plan", [])

        entries = [DayPlanEntry.model_validate(item) for item in raw_plan]
        logger.info(
            "day_planner.run() produced %d entries for agent '%s', day %d%s",
            len(entries), request.agent_id, request.day_index,
            " (interrupt-triggered replan)" if request.interrupt_context else "",
        )
        return entries

    return _adapter


def _find_slot(plan: List[DayPlanEntry], hhmm: str) -> Optional[DayPlanEntry]:
    minute = hhmm_to_minute(hhmm)
    for entry in plan:
        if hhmm_to_minute(entry.start) <= minute < hhmm_to_minute(entry.end):
            return entry
    return None


# --------------------------------------------------------------------------- #
# Per-agent, per-sim-day plan cache
# --------------------------------------------------------------------------- #


class DayPlanStoreProtocol:
    def get(self, agent_id: str, day_index: int) -> Optional[List[DayPlanEntry]]: ...
    def set(self, agent_id: str, day_index: int, plan: List[DayPlanEntry]) -> None: ...
    def invalidate(self, agent_id: str, day_index: int) -> None: ...


class InMemoryDayPlanStore(DayPlanStoreProtocol):
    def __init__(self) -> None:
        self._plans: Dict[Tuple[str, int], List[DayPlanEntry]] = {}

    def get(self, agent_id: str, day_index: int) -> Optional[List[DayPlanEntry]]:
        return self._plans.get((agent_id, day_index))

    def set(self, agent_id: str, day_index: int, plan: List[DayPlanEntry]) -> None:
        self._plans[(agent_id, day_index)] = plan

    def invalidate(self, agent_id: str, day_index: int) -> None:
        self._plans.pop((agent_id, day_index), None)


# --------------------------------------------------------------------------- #
# Memory stream integration contract
# --------------------------------------------------------------------------- #


class MemoryStreamProtocol:
    def add_memory(self, agent_id: str, content: str, importance: Optional[int] = None) -> None: ...
    def retrieve_memories(self, agent_id: str, query: str, k: int = 5) -> List[str]: ...


class _NullMemoryStream:
    def add_memory(self, agent_id: str, content: str, importance: Optional[int] = None) -> None:
        logger.debug("[NullMemoryStream] would store for '%s': %s", agent_id, content)

    def retrieve_memories(self, agent_id: str, query: str, k: int = 5) -> List[str]:
        return []


# --------------------------------------------------------------------------- #
# Tick outcome / result
# --------------------------------------------------------------------------- #


class TickOutcome(str, Enum):
    KEEP_CURRENT = "keep_current"
    NEW_ACTION = "new_action"


class TickResult(BaseModel):
    agent_id: str
    tick: int
    outcome: TickOutcome
    action: Optional[CurrentAction] = None
    reaction: Optional[ReactionDecision] = None


# --------------------------------------------------------------------------- #
# Internal graph state
# --------------------------------------------------------------------------- #


class TickState(TypedDict, total=False):
    agent_id: str
    tick: int
    persona: Dict[str, Any]
    yesterday_summary: Optional[str]
    world_snapshot: WorldSnapshot
    current_action: Optional[CurrentAction]
    observations: List[Observation]
    memories: List[str]
    reaction: ReactionDecision
    result: TickResult


def build_initial_state(
    agent_id: str,
    persona: Dict[str, Any],
    yesterday_summary: Optional[str],
    world_snapshot: WorldSnapshot,
) -> TickState:
    current_action = world_snapshot.get_agent(agent_id).current_action
    return TickState(
        agent_id=agent_id,
        tick=world_snapshot.tick,
        persona=persona,
        yesterday_summary=yesterday_summary,
        world_snapshot=world_snapshot,
        current_action=current_action,
    )


# --------------------------------------------------------------------------- #
# Graph builder
# --------------------------------------------------------------------------- #


def build_tick_graph(
    memory_stream: Optional[MemoryStreamProtocol] = None,
    day_planner_fn: Optional[DayPlannerFn] = None,
    day_plan_store: Optional[DayPlanStoreProtocol] = None,
    sim_start_date: Optional[date] = None,
):
    memory = memory_stream or _NullMemoryStream()
    if memory_stream is None:
        logger.warning(
            "build_tick_graph() called with no memory_stream -- using a "
            "no-op stand-in. retrieve_memories() will always return []. "
            "Pass the real MemoryStream in once it's ready."
        )

    day_plans = day_plan_store or InMemoryDayPlanStore()
    plan_fn = day_planner_fn or build_default_day_planner_fn(sim_start_date)

    def perceive_node(state: TickState) -> Dict[str, Any]:
        observations = perceive(state["world_snapshot"], state["agent_id"])
        return {"observations": observations}

    def retrieve_memories_node(state: TickState) -> Dict[str, Any]:
        agent_id = state["agent_id"]
        observations = state.get("observations", [])
        if observations:
            query = "; ".join(o.description for o in observations)
        else:
            action = state.get("current_action")
            query = action.description if action else "what should I do next"
        memories = memory.retrieve_memories(agent_id, query=query, k=5)
        return {"memories": memories}

    def react_node(state: TickState) -> Dict[str, Any]:
        reaction = decide_reaction(
            agent_id=state["agent_id"],
            persona=state["persona"],
            current_action=state.get("current_action"),
            observations=state.get("observations", []),
            memories=state.get("memories", []),
            tick=state["tick"],
        )
        return {"reaction": reaction}

    def route_after_react(state: TickState) -> str:
        return "day_planner" if state["reaction"].should_replan else "keep_current"

    def day_planner_node(state: TickState) -> Dict[str, Any]:
        agent_id = state["agent_id"]
        tick = state["tick"]
        day_index, hhmm = tick_to_wallclock(tick)

        current_action = state.get("current_action")
        interrupted = current_action is not None and not current_action.is_finished(tick)

        cached_plan = day_plans.get(agent_id, day_index)

        if cached_plan is None or interrupted:
            interrupt_context = None
            if interrupted:
                obs_text = "; ".join(o.description for o in state.get("observations", []))
                interrupt_context = (
                    f"Was in the middle of '{current_action.description}' when this "
                    f"happened: {obs_text or '(unspecified event)'}."
                )
            request = DayPlannerRequest(
                agent_id=agent_id,
                persona=state["persona"],
                relevant_memories=state.get("memories", []),
                yesterday_summary=state.get("yesterday_summary"),
                day_index=day_index,
                current_hhmm=hhmm,
                interrupt_context=interrupt_context,
            )
            cached_plan = plan_fn(request)
            day_plans.set(agent_id, day_index, cached_plan)

        slot = _find_slot(cached_plan, hhmm)
        if slot is None:
            logger.warning(
                "No day-plan slot covers %s on day %d for agent '%s'; "
                "using a placeholder action and invalidating the cached plan.",
                hhmm, day_index, agent_id,
            )
            day_plans.invalidate(agent_id, day_index)
            slot = DayPlanEntry(action="idle (no plan slot found)", start=hhmm, end=hhmm)
            duration = 10
        else:
            duration = hhmm_to_minute(slot.end) - hhmm_to_minute(hhmm)
            duration = max(duration, 1)

        action = CurrentAction(
            description=slot.action,
            start_tick=tick,
            end_tick=tick + duration,
            target_location_id=slot.location_id,
        )
        result = TickResult(
            agent_id=agent_id,
            tick=tick,
            outcome=TickOutcome.NEW_ACTION,
            action=action,
            reaction=state.get("reaction"),
        )
        return {"result": result}

    def keep_current_node(state: TickState) -> Dict[str, Any]:
        result = TickResult(
            agent_id=state["agent_id"],
            tick=state["tick"],
            outcome=TickOutcome.KEEP_CURRENT,
            action=state.get("current_action"),
            reaction=state.get("reaction"),
        )
        return {"result": result}

    def write_back_memory_node(state: TickState) -> Dict[str, Any]:
        result: TickResult = state["result"]
        if result.outcome == TickOutcome.NEW_ACTION and result.action is not None:
            memory.add_memory(
                agent_id=state["agent_id"],
                content=f"Decided to: {result.action.description}",
                importance=None,
            )
        return {}

    graph = StateGraph(TickState)
    graph.add_node("perceive", perceive_node)
    graph.add_node("retrieve_memories", retrieve_memories_node)
    graph.add_node("react", react_node)
    graph.add_node("day_planner", day_planner_node)
    graph.add_node("keep_current", keep_current_node)
    graph.add_node("write_back_memory", write_back_memory_node)

    graph.add_edge(START, "perceive")
    graph.add_edge("perceive", "retrieve_memories")
    graph.add_edge("retrieve_memories", "react")
    graph.add_conditional_edges(
        "react",
        route_after_react,
        {"day_planner": "day_planner", "keep_current": "keep_current"},
    )
    graph.add_edge("day_planner", "write_back_memory")
    graph.add_edge("keep_current", "write_back_memory")
    graph.add_edge("write_back_memory", END)

    compiled = graph.compile()
    logger.info("Tick graph compiled.")
    return compiled


async def run_tick(
    tick_graph,
    agent_id: str,
    persona: Dict[str, Any],
    yesterday_summary: Optional[str],
    world_snapshot: WorldSnapshot,
) -> TickResult:
    initial_state = build_initial_state(agent_id, persona, yesterday_summary, world_snapshot)
    final_state = await tick_graph.ainvoke(initial_state)
    return final_state["result"]


# --------------------------------------------------------------------------- #
# Standalone day-plan CLI (debug tool)
# --------------------------------------------------------------------------- #

if __name__ == "__main__":
    import argparse
    import asyncio
    import json
    import time
    from types import SimpleNamespace

    from src.core.log import setup_logging
    setup_logging(run_id="agent_cli", console=False)
    from src.config import PERSONALITIES_DIR

    parser = argparse.ArgumentParser(description="Agent debug tool — day-plan or self-test")
    parser.add_argument(
        "persona", nargs="?",
        help="Persona name (e.g. parv) or path to a persona JSON file",
    )
    parser.add_argument(
        "--self-test", action="store_true",
        help="Run tick graph sanity check with a fake day planner (no Gemini)",
    )
    parser.add_argument(
        "--current-time", default="2026-07-03 06:00",
        help="Simulation time for the day planner (default: 2026-07-03 06:00)",
    )
    args = parser.parse_args()

    # --self-test mode: tick graph sanity check
    if args.self_test:
        from src.core.world_state import WorldState, Position
        from src.core.snapshot import take_snapshot

        def fake_day_planner(request: DayPlannerRequest) -> List[DayPlanEntry]:
            return [
                DayPlanEntry(action="sleeping", start="00:00", end="06:00"),
                DayPlanEntry(action="testing the tick graph", start="06:00", end="24:00"),
            ]

        async def _run_self_test() -> None:
            world = WorldState()
            world.register_agent("test_agent", Position(x=0, y=0, location_id="start"))
            world.advance_tick(minutes=6 * 60)
            snap = take_snapshot(world)
            graph = build_tick_graph(day_planner_fn=fake_day_planner)
            result = await run_tick(
                graph, "test_agent", persona={"name": "Test"}, yesterday_summary=None, world_snapshot=snap
            )
            assert result.outcome == TickOutcome.NEW_ACTION
            assert result.action is not None
            assert result.action.description == "testing the tick graph"
            print("Self-test passed:", result)

        asyncio.run(_run_self_test())
        sys.exit(0)

    # Day-plan mode: generate a full day plan for one persona
    if args.persona is None:
        parser.print_help()
        sys.exit(1)

    # Resolve persona path (same logic as Single_agent.py)
    candidate = Path(args.persona)
    if candidate.exists():
        persona_path = candidate
    elif candidate.suffix == ".json":
        matches = sorted(PERSONALITIES_DIR.glob(f"**/{candidate.name}"))
        persona_path = matches[0] if matches else candidate
    else:
        matches = sorted(PERSONALITIES_DIR.glob(f"**/{args.persona}/{args.persona}.json"))
        if not matches:
            matches = sorted(PERSONALITIES_DIR.glob(f"**/{args.persona}.json"))
        if not matches:
            available = sorted({p.parent.name for p in PERSONALITIES_DIR.glob("**/*.json")})
            raise FileNotFoundError(
                f"Could not find persona '{args.persona}'. "
                f"Available: {', '.join(available)}"
            )
        persona_path = matches[0]

    persona_data = json.loads(persona_path.read_text())
    persona_name = persona_data.get("Name", persona_path.stem)
    current_time = args.current_time

    t0 = time.perf_counter()
    print(f'Day planner — running "{persona_name}" at {current_time}\n')

    # Retrieve memories from Short_term
    from src.agents.Short_term import date_from_simulation_time, get_yesterday_summary, get_relevant_memories

    sim_date = date_from_simulation_time(current_time)
    yesterday_summary = get_yesterday_summary(persona_name, sim_date)
    traits = persona_data.get("Traits", persona_data.get("traits", []))
    query = " ".join(traits) if traits else "daily life"
    memories = get_relevant_memories(persona_name, sim_date, query, k=5)

    if yesterday_summary:
        print(f"  Loaded yesterday's summary")
    if memories:
        print(f"  Loaded {len(memories)} relevant memories")

    # Generate day plan via day_planner.run()
    from src.agents.day_planner import run as day_planner_run

    agent = SimpleNamespace(
        persona=persona_data,
        relevant_memories=memories,
        yesterday_summary=yesterday_summary,
    )

    try:
        result = day_planner_run(agent, {
            "current_time": current_time,
            "places": None,
            "persona_name": persona_name,
        })
        plan = result.get("day_plan", [])
        error = result.get("error")

        elapsed = time.perf_counter() - t0

        print(f"\n{'='*60}")
        if error:
            print(f"  Failed: {error}  |  Elapsed: {elapsed:.1f}s")
        else:
            print(f"  Completed — {len(plan)} actions  |  Elapsed: {elapsed:.1f}s")
        print(f"{'='*60}")

        if not error and plan:
            print(f'\n  Generated plan for "{persona_name}":')
            print(f'  {"Action":25s} {"Start":7s} {"End":7s} {"Location":25s} {"Area":20s}')
            print(f'  {"-"*25} {"-"*7} {"-"*7} {"-"*25} {"-"*20}')
            for a in plan:
                loc = (a.get("location_id") or "")[:25]
                area = (a.get("sub_area") or "")[:20]
                print(f'  {a.get("action", ""):25s} {a.get("start", ""):7s} {a.get("end", ""):7s} {loc:25s} {area:20s}')

    except Exception as exc:
        elapsed = time.perf_counter() - t0
        print(f"\n  Failed after {elapsed:.1f}s: {exc}")