File size: 11,590 Bytes
0423b99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Session orchestrator (Section 7 & 11): turn cycle + crash-safe persistence.

Every state-changing action flushes to disk, so a crash or quit never loses the
ledger or progress. Holds the deterministic engine pieces and routes the
in-session verbs (talk / look / confront / notes / accuse).
"""

from __future__ import annotations

import uuid
from dataclasses import dataclass, field
from datetime import UTC, datetime

from ..config import Config
from ..llm.client import LLMClient
from ..llm.prompts import PromptRegistry
from ..llm.usage import UsageLedger
from ..models import (
    AccusationResult,
    CharacterCard,
    SessionState,
    SessionStatus,
    TranscriptEvent,
)
from ..worldio import World, load_world
from .accuse import score_accusation
from .character import CharacterPipeline, TurnOutcome
from .clues import ClueGraph
from .confront import ConfrontResult, verify_confrontation
from .environment import EnvironmentChat, WorldAnswer, WorldDelta
from .ledger import LedgerStore


def _new_session_id(world_id: str) -> str:
    stamp = datetime.now(UTC).strftime("%Y%m%d-%H%M%S")
    return f"{world_id}-{stamp}-{uuid.uuid4().hex[:6]}"


@dataclass
class Notes:
    """The player's case file (typed view returned by ``Session.notes``)."""

    discovered_clues: list[dict[str, str]] = field(default_factory=list)
    cracked: list[str] = field(default_factory=list)
    ledgers: dict[str, list[dict[str, str]]] = field(default_factory=dict)


class Session:
    def __init__(
        self,
        *,
        config: Config,
        world: World,
        state: SessionState,
        prompts: PromptRegistry,
        base_client: LLMClient,
    ) -> None:
        self.config = config
        self.world = world
        self.state = state
        self.prompts = prompts
        self.dir = config.runtime_dir / state.session_id

        usage = UsageLedger(self.dir / "usage.jsonl")
        self.client = base_client.bind(
            world_id=world.meta.world_id, session_id=state.session_id, usage=usage,
        )

        self.clue_graph = ClueGraph(world.clues)
        self.ledger = LedgerStore(self.dir / "ledgers")
        self.delta = WorldDelta(self.dir / "world_delta.json")
        self.transcript_path = self.dir / "transcript.jsonl"

        self.pipeline = CharacterPipeline(
            world=world,
            client=self.client,
            prompts=prompts,
            ledger=self.ledger,
            clue_graph=self.clue_graph,
            regenerate_retries=config.engine.regenerate_retries,
        )
        self.env = EnvironmentChat(
            world=world, client=self.client, prompts=prompts,
            clue_graph=self.clue_graph, delta=self.delta,
        )
        self._confront_counts: dict[str, int] = {}

    # -- construction -------------------------------------------------------

    @classmethod
    def start(
        cls, config: Config, world_id: str, prompts: PromptRegistry,
        base_client: LLMClient,
    ) -> Session:
        world = load_world(config.worlds_dir / world_id)
        state = SessionState(session_id=_new_session_id(world_id), world_id=world_id)
        sess = cls(config=config, world=world, state=state,
                   prompts=prompts, base_client=base_client)
        sess.dir.mkdir(parents=True, exist_ok=True)
        sess._flush_state()
        sess._append_transcript(TranscriptEvent(
            turn=0, kind="system", text=f"Session started for world {world_id}.",
        ))
        return sess

    @classmethod
    def resume(
        cls, config: Config, session_id: str, prompts: PromptRegistry,
        base_client: LLMClient,
    ) -> Session:
        sdir = config.runtime_dir / session_id
        state = SessionState.model_validate_json(
            (sdir / "session.json").read_text("utf-8")
        )
        world = load_world(config.worlds_dir / state.world_id)
        sess = cls(config=config, world=world, state=state,
                   prompts=prompts, base_client=base_client)
        return sess

    # -- persistence --------------------------------------------------------

    def _flush_state(self) -> None:
        (self.dir / "session.json").write_text(
            self.state.model_dump_json(indent=2), "utf-8"
        )

    def _append_transcript(self, event: TranscriptEvent) -> None:
        with self.transcript_path.open("a", encoding="utf-8") as fh:
            fh.write(event.model_dump_json() + "\n")

    def transcript(self) -> list[TranscriptEvent]:
        if not self.transcript_path.exists():
            return []
        return [
            TranscriptEvent.model_validate_json(line)
            for line in self.transcript_path.read_text("utf-8").splitlines()
            if line.strip()
        ]

    def _tail_for(self, actor: str, n: int = 8) -> list[TranscriptEvent]:
        rel = [
            e for e in self.transcript()
            if e.kind in ("player", "character", "crack")
            and (e.actor == actor or e.meta.get("target") == actor)
        ]
        return rel[-n:]

    def _next_turn(self) -> int:
        self.state.turn += 1
        return self.state.turn

    # -- verbs --------------------------------------------------------------

    def _resolve_character(self, character: str) -> CharacterCard:
        card = self.world.character(character)
        if card is not None:
            return card
        matches = self.world.candidates(character)
        if len(matches) > 1:
            names = ", ".join(c.name for c in matches)
            raise KeyError(f"{character} matches {names} — be more specific")
        raise KeyError(f"no such character: {character}")

    def talk(self, character: str, message: str) -> TurnOutcome:
        card = self._resolve_character(character)
        turn = self._next_turn()
        self._append_transcript(TranscriptEvent(
            turn=turn, kind="player", actor="player", text=message,
            meta={"target": card.name},
        ))
        outcome = self.pipeline.run_turn(
            card=card,
            player_message=message,
            turn=turn,
            transcript_tail=self._tail_for(card.name),
            discovered=set(self.state.discovered_clues),
            confront_count=self._confront_counts.get(card.name, 0),
            already_cracked=card.name in self.state.cracked,
        )
        kind = "crack" if outcome.cracked else "character"
        self._append_transcript(TranscriptEvent(
            turn=turn, kind=kind, actor=card.name, text=outcome.text,
            meta={"behavior": outcome.crack_behavior, "tell": outcome.tell},
        ))
        if outcome.cracked and card.name not in self.state.cracked:
            self.state.cracked.append(card.name)
        # Testimony-sourced clues surface when the character speaks to them.
        outcome.discovered_clues = self._discover_from_testimony(
            card.name, message, outcome.text
        )
        self._flush_state()
        return outcome

    def _discover_from_testimony(
        self, character: str, question: str, reply: str
    ) -> list[str]:
        """Mark unlocked, testimony-sourced clues discovered when the character
        actually speaks to their substance. The engine owns *whether* a clue is
        unlocked (gating); a cheap extractor-tier check judges, robustly, whether
        the witness genuinely confirmed the fact (names/times may be reworded)."""
        slug = character.lower().replace(" ", "_").replace(".", "")
        source_tag = f"{slug}_testimony"
        discovered = set(self.state.discovered_clues)
        candidates = [
            {"id": node.id, "reveals": node.reveals}
            for node in self.world.clues
            if node.id not in discovered
            and self.clue_graph.is_unlocked(node.id, discovered)
            and any(
                source_tag in s.lower().replace(" ", "_").replace(".", "")
                for s in node.sources
            )
        ]
        if not candidates:
            return []
        confirmed = self.pipeline.extractor.confirmed_testimony(
            question=question, reply=reply, candidates=candidates,
        )
        newly: list[str] = []
        for cid in confirmed:
            if cid not in self.state.discovered_clues:
                self.state.discovered_clues.append(cid)
                newly.append(cid)
        return newly

    def look(self, query: str, location: str | None = None) -> WorldAnswer:
        turn = self._next_turn()
        self._append_transcript(TranscriptEvent(
            turn=turn, kind="player", actor="player",
            text=f"[look @ {location or 'scene'}] {query}",
        ))
        answer = self.env.ask(
            query=query, location=location, discovered=set(self.state.discovered_clues),
        )
        if answer.discovered_clue and answer.discovered_clue not in self.state.discovered_clues:
            self.state.discovered_clues.append(answer.discovered_clue)
        self._append_transcript(TranscriptEvent(
            turn=turn, kind="world", actor="world", text=answer.text,
            meta={"source": answer.source, "clue": answer.discovered_clue},
        ))
        self._flush_state()
        return answer

    def confront(self, character: str, claim_a: str, claim_b: str) -> ConfrontResult:
        card = self._resolve_character(character)
        result = verify_confrontation(self.ledger, card.name, claim_a, claim_b)
        turn = self._next_turn()
        if result.verified:
            self._confront_counts[card.name] = self._confront_counts.get(card.name, 0) + 1
        self._append_transcript(TranscriptEvent(
            turn=turn, kind="confront", actor=card.name, text=result.reason,
            meta={"verified": result.verified, "a": claim_a, "b": claim_b},
        ))
        self._flush_state()
        return result

    def notes(self) -> Notes:
        """The player's case file: committed claims + discovered clues."""
        ledgers: dict[str, list[dict[str, str]]] = {}
        for name in self.world.characters:
            entries = self.ledger.get(name).entries
            if not entries:
                continue
            ledgers[name] = [
                {
                    "claim_id": c.claim_id,
                    "topic": c.topic,
                    "proposition": c.proposition,
                }
                for e in entries for c in e.claims
            ]
        clues = [
            {"id": cid, "reveals": self.clue_graph.nodes[cid].reveals}
            for cid in self.state.discovered_clues
            if cid in self.clue_graph.nodes
        ]
        return Notes(
            discovered_clues=clues,
            cracked=list(self.state.cracked),
            ledgers=ledgers,
        )

    def accuse(
        self, culprit: str, means: str, motive: str, opportunity: str
    ) -> AccusationResult:
        result = score_accusation(
            world=self.world,
            clue_graph=self.clue_graph,
            discovered=set(self.state.discovered_clues),
            culprit=culprit, means=means, motive=motive, opportunity=opportunity,
        )
        turn = self._next_turn()
        self.state.accusation = result
        self.state.status = (
            SessionStatus.solved if result.grade == "solved" else SessionStatus.closed
        )
        self._append_transcript(TranscriptEvent(
            turn=turn, kind="accuse", actor="player",
            text=f"Accused {culprit}. Grade: {result.grade}",
            meta={"grade": result.grade},
        ))
        self._flush_state()
        return result