File size: 10,618 Bytes
e70c280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Compact prior-state scratchpad for Architect batch extract.

Replaces replaying raw prior unit text with a token-capped summary of
entities, open event hubs, dates, and recent predicate/span pointers.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Optional, Sequence

from src.core.saving.ingest_cost import count_source_tokens

DEFAULT_SCRATCHPAD_TOKEN_CAP = 500
DEFAULT_PRIOR_UNIT_WINDOW = 4
_SPAN_POINTER_CHARS = 72
_DESC_CHARS = 60
_MAX_ENTITIES = 40
_MAX_HUBS = 16
_MAX_PREDICATES = 24
_MAX_DATES = 12


@dataclass
class ScratchpadEntity:
    uuid: str
    name: str
    type: str
    happened_at: Optional[str] = None


@dataclass
class ScratchpadEventHub:
    uuid: str
    name: str
    happened_at: Optional[str] = None
    description: Optional[str] = None
    actors: list[str] = field(default_factory=list)
    objects: list[str] = field(default_factory=list)


@dataclass
class ScratchpadPredicate:
    predicate: str
    tail: str
    tip: str
    span_pointer: Optional[str] = None


@dataclass
class ArchitectScratchpad:
    entities: list[ScratchpadEntity] = field(default_factory=list)
    event_hubs: list[ScratchpadEventHub] = field(default_factory=list)
    recent_predicates: list[ScratchpadPredicate] = field(default_factory=list)
    dates: list[str] = field(default_factory=list)
    raw_span_fetches: int = 0


def _attr(obj: Any, key: str, default: Any = None) -> Any:
    if obj is None:
        return default
    if isinstance(obj, dict):
        return obj.get(key, default)
    return getattr(obj, key, default)


def _truncate(text: Optional[str], limit: int) -> str:
    value = (text or "").strip()
    if len(value) <= limit:
        return value
    return value[: max(0, limit - 1)].rstrip() + "…"


def _is_event(typ: Optional[str]) -> bool:
    return (typ or "").strip().lower() == "event"


def _entity_from_any(obj: Any) -> Optional[ScratchpadEntity]:
    name = (_attr(obj, "name") or "").strip()
    typ = (_attr(obj, "type") or "").strip()
    if not name or not typ:
        return None
    if name.lower() == typ.lower():
        return None
    uuid_ = str(_attr(obj, "uuid") or "").strip()
    happened = _attr(obj, "happened_at")
    return ScratchpadEntity(
        uuid=uuid_,
        name=name,
        type=typ,
        happened_at=(str(happened).strip() if happened else None) or None,
    )


def _span_pointer_from_rel(rel: Any) -> Optional[str]:
    props = _attr(rel, "properties") or {}
    if not isinstance(props, dict):
        props = {}
    span = props.get("source_span") or _attr(rel, "source_span") or ""
    span = str(span).strip()
    if not span:
        return None
    return _truncate(span, _SPAN_POINTER_CHARS)


def build_scratchpad(
    prior_entities: Sequence[Any],
    prior_relationships: Sequence[Any] = (),
    *,
    max_entities: int = _MAX_ENTITIES,
    max_hubs: int = _MAX_HUBS,
    max_predicates: int = _MAX_PREDICATES,
) -> ArchitectScratchpad:
    """Build a compact prior-state scratchpad from Scout entities + Architect rels."""
    entities: list[ScratchpadEntity] = []
    seen_keys: set[tuple[str, str]] = set()
    seen_uuids: set[str] = set()
    dates: list[str] = []
    seen_dates: set[str] = set()

    for raw in prior_entities:
        ent = _entity_from_any(raw)
        if ent is None:
            continue
        key = (ent.name.lower(), ent.type.lower())
        if ent.uuid and ent.uuid in seen_uuids:
            continue
        if key in seen_keys:
            continue
        seen_keys.add(key)
        if ent.uuid:
            seen_uuids.add(ent.uuid)
        entities.append(ent)
        if ent.happened_at and ent.happened_at not in seen_dates:
            seen_dates.add(ent.happened_at)
            dates.append(ent.happened_at)

    entities = entities[-max_entities:]

    hubs_by_key: dict[str, ScratchpadEventHub] = {}
    predicates: list[ScratchpadPredicate] = []

    for rel in prior_relationships:
        tail = _entity_from_any(_attr(rel, "tail"))
        tip = _entity_from_any(_attr(rel, "tip"))
        pred = (_attr(rel, "name") or "").strip()
        if not pred or tail is None or tip is None:
            continue

        for endpoint in (tail, tip):
            if endpoint.happened_at and endpoint.happened_at not in seen_dates:
                seen_dates.add(endpoint.happened_at)
                dates.append(endpoint.happened_at)
            props = _attr(_attr(rel, "tail" if endpoint is tail else "tip"), "properties")
            if isinstance(props, dict):
                ha = props.get("happened_at")
                if ha and str(ha) not in seen_dates:
                    seen_dates.add(str(ha))
                    dates.append(str(ha))

        span_ptr = _span_pointer_from_rel(rel)
        predicates.append(
            ScratchpadPredicate(
                predicate=pred,
                tail=tail.name,
                tip=tip.name,
                span_pointer=span_ptr,
            )
        )

        for endpoint, other, role in (
            (tip, tail, "actor"),
            (tail, tip, "object"),
        ):
            if not _is_event(endpoint.type):
                continue
            hub_key = endpoint.uuid or f"{endpoint.name.lower()}|{endpoint.type.lower()}"
            hub = hubs_by_key.get(hub_key)
            if hub is None:
                desc = _attr(
                    _attr(rel, "tip" if endpoint is tip else "tail"),
                    "description",
                )
                hub = ScratchpadEventHub(
                    uuid=endpoint.uuid,
                    name=endpoint.name,
                    happened_at=endpoint.happened_at,
                    description=_truncate(desc, _DESC_CHARS) or None,
                )
                hubs_by_key[hub_key] = hub
            if role == "actor":
                if other.name not in hub.actors:
                    hub.actors.append(other.name)
            else:
                if other.name not in hub.objects:
                    hub.objects.append(other.name)

    predicates = predicates[-max_predicates:]
    hubs = list(hubs_by_key.values())[-max_hubs:]
    dates = dates[-_MAX_DATES:]

    return ArchitectScratchpad(
        entities=entities,
        event_hubs=hubs,
        recent_predicates=predicates,
        dates=dates,
    )


def serialize_scratchpad(
    pad: ArchitectScratchpad,
    *,
    token_cap: int = DEFAULT_SCRATCHPAD_TOKEN_CAP,
) -> tuple[str, int]:
    """Serialize scratchpad under a reference-token cap. Returns (text, tokens)."""
    cap = max(32, int(token_cap or DEFAULT_SCRATCHPAD_TOKEN_CAP))
    sections: list[str] = []

    if pad.entities:
        lines = ["Entities:"]
        for ent in pad.entities:
            bits = [ent.name, ent.type]
            if ent.uuid:
                bits.append(ent.uuid[:8])
            if ent.happened_at:
                bits.append(f"@{ent.happened_at}")
            lines.append("- " + " | ".join(bits))
        sections.append("\n".join(lines))

    if pad.event_hubs:
        lines = ["Open event hubs:"]
        for hub in pad.event_hubs:
            head = hub.name
            if hub.happened_at:
                head += f" @{hub.happened_at}"
            detail_parts = []
            if hub.actors:
                detail_parts.append("actors=" + ", ".join(hub.actors[:4]))
            if hub.objects:
                detail_parts.append("objects=" + ", ".join(hub.objects[:4]))
            if hub.description:
                detail_parts.append(hub.description)
            line = f"- {head}"
            if detail_parts:
                line += " (" + "; ".join(detail_parts) + ")"
            lines.append(line)
        sections.append("\n".join(lines))

    if pad.dates:
        sections.append("Dates: " + ", ".join(pad.dates))

    if pad.recent_predicates:
        lines = ["Recent predicates:"]
        for pred in pad.recent_predicates:
            line = f"- {pred.tail} --{pred.predicate}--> {pred.tip}"
            if pred.span_pointer:
                line += f' [{pred.span_pointer}]'
            lines.append(line)
        sections.append("\n".join(lines))

    if not sections:
        return "", 0

    # Drop trailing sections until under cap (prefer keep entities + hubs).
    while sections:
        text = "\n".join(sections)
        tokens, _, _ = count_source_tokens(text)
        if tokens <= cap:
            return text, tokens
        if len(sections) == 1:
            # Hard-trim the last remaining section by lines.
            lines = sections[0].split("\n")
            while len(lines) > 1:
                lines.pop()
                text = "\n".join(lines)
                tokens, _, _ = count_source_tokens(text)
                if tokens <= cap:
                    return text, tokens
            return "", 0
        sections.pop()

    return "", 0


def fetch_prior_span(
    prior_chunks: Sequence[str],
    needle: str,
    *,
    window_chars: int = 200,
) -> Optional[str]:
    """On-demand: locate `needle` in prior unit text and return a small window."""
    query = (needle or "").strip()
    if not query or not prior_chunks:
        return None
    window = max(40, int(window_chars or 200))
    for chunk in reversed(list(prior_chunks)):
        text = chunk or ""
        idx = text.find(query)
        if idx < 0:
            idx = text.lower().find(query.lower())
        if idx < 0:
            continue
        start = max(0, idx - window // 4)
        end = min(len(text), idx + len(query) + window // 2)
        return text[start:end].strip()
    return None


def format_unit_with_scratchpad(current_chunk: str, scratchpad_text: str) -> str:
    """Compose Architect unit prompt text: scratchpad preamble + current unit."""
    if not (scratchpad_text or "").strip():
        return current_chunk
    return (
        f"[Prior scratchpad]\n{scratchpad_text.strip()}\n\n"
        f"[Current unit]\n{current_chunk}"
    )


def resolve_prior_context_mode(
    configured: str | None,
    architect_mode: str,
) -> str:
    """
    Resolve prior-context strategy.

    auto (default): scratchpad for batch/schema, raw for tooler.
    """
    value = (configured or "auto").strip().lower()
    if value in ("scratchpad", "raw"):
        return value
    if value in ("auto", ""):
        if (architect_mode or "").strip().lower() in ("batch", "schema"):
            return "scratchpad"
        return "raw"
    raise ValueError(
        f"Invalid INGEST_ARCHITECT_PRIOR_CONTEXT: {configured!r}. "
        "Expected 'auto', 'scratchpad', or 'raw'."
    )