File size: 3,737 Bytes
4b1aa2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Incremental evidence and Needle token counts for one append-only document."""
from dataclasses import dataclass
import time

import numpy as np

from policy import SegmentMemo, needle_count, validate_count


@dataclass(frozen=True)
class StreamState:
    tokens: int
    suffix: str
    halo: bytes
    stable_facts: np.ndarray
    explanation_text: str


class DecisionStream:
    def __init__(self, engine, explain=False):
        if engine.info["variant"] == "scan":
            raise ValueError("Streaming requires a decision head that accepts fact bits")
        self.engine = engine
        self.explain = explain
        self.memo = SegmentMemo()
        self.halo_size = max(map(len, engine.policy.patterns)) - 1
        self.state = self.empty_state()

    def empty_state(self):
        facts = np.zeros(len(self.engine.policy.literals), dtype=np.float32)
        return StreamState(0, "", b"", facts, "")

    def _initial_halo(self, text):
        if not self.halo_size:
            return b""
        width = 256
        while width < len(text):
            begin = text.find(" ", len(text) - width)
            if begin >= 0:
                encoded = self.engine.policy.project(text[begin:])
                if len(encoded) >= self.halo_size:
                    return encoded[-self.halo_size:]
            width *= 2
        return self.engine.policy.project(text)[-self.halo_size:]

    def append(self, fragment, reset=False):
        if not isinstance(fragment, str) or type(reset) is not bool:
            raise ValueError("Append must be a string and reset must be a Boolean")
        current = self.empty_state() if reset else self.state
        next_state = None
        policy = self.engine.policy

        def prepare(delta):
            nonlocal next_state
            pending = current.suffix + delta
            split = pending.rfind(" ")
            committed = pending[:split] if split > 0 else ""
            suffix = pending[split:] if split > 0 else pending

            # The pinned Needle model cannot merge across an ASCII space. Keep
            # that space with the uncommitted segment so its prefix is exact.
            stable_tokens = current.tokens + needle_count(self.engine.tokenizer, committed, memo=self.memo)
            tokens = validate_count(stable_tokens + needle_count(self.engine.tokenizer, suffix, memo=self.memo))
            counted = time.perf_counter()

            if not current.halo and len(committed) >= 4096:
                stable_facts = np.maximum(current.stable_facts, policy.text_atoms(committed))
                halo = self._initial_halo(committed)
            else:
                text = current.halo + policy.project(committed)
                stable_facts = np.maximum(current.stable_facts, policy.atoms(text))
                halo = text[-self.halo_size:] if self.halo_size else b""
            tail_facts = policy.atoms(halo + policy.project(suffix)) if halo else policy.text_atoms(suffix)
            facts = np.maximum(stable_facts, tail_facts)
            explanation_text = current.explanation_text + delta if self.explain else ""
            next_state = StreamState(stable_tokens, suffix, halo, stable_facts, explanation_text)
            return tokens, facts, counted

        prepared = self.engine.prepare_decision(fragment, prepare=prepare)
        result = self.engine.decide_prepared([prepared])[0]
        if self.explain:
            result["explanation"] = policy.explain(next_state.explanation_text, prepared.atoms)
        # A rejected append or a failed inference leaves the previous stream intact.
        self.state = next_state
        return result