File size: 5,846 Bytes
d2a5f5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Cryptographic Episodic Ledger — Vitalis FSI

High-performance, append-only, thread-safe, HMAC-signed memory ledger.
Uses line-delimited JSON (JSONL) to achieve O(1) append efficiency.
"""

from __future__ import annotations

import hashlib
import hmac
import json
import os
import secrets
import time
import fcntl
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional

from src.cognition._constants import BASE_DIR, logger
from src.cognition.serialization import VitalisEncoder

LEDGER_DIR = BASE_DIR / "ledger"
LEDGER_DIR.mkdir(parents=True, exist_ok=True)

KEY_FILE = LEDGER_DIR / ".vitalis_hmac.key"
if not KEY_FILE.exists():
    KEY_FILE.write_bytes(secrets.token_bytes(32))
HMAC_SECRET = KEY_FILE.read_bytes()

@dataclass(slots=True)
class LedgerBlock:
    index: int
    timestamp: float
    task_id: str
    outcome_metrics: str
    previous_hash: str
    block_hash: str = field(init=False)
    signature: str = field(init=False)

    def __post_init__(self) -> None:
        self.block_hash = self._calc_hash()
        self.signature = self._sign_hash()

    def _calc_hash(self) -> str:
        data = f"{self.index}|{self.timestamp:.6f}|{self.task_id}|{self.outcome_metrics}|{self.previous_hash}"
        return hashlib.sha256(data.encode("utf-8")).hexdigest()

    def _sign_hash(self) -> str:
        return hmac.new(HMAC_SECRET, self.block_hash.encode("utf-8"), hashlib.sha256).hexdigest()

    def is_valid(self) -> bool:
        if self.block_hash != self._calc_hash():
            return False
        expected = hmac.new(HMAC_SECRET, self.block_hash.encode("utf-8"), hashlib.sha256).hexdigest()
        return hmac.compare_digest(self.signature, expected)

class QuantumResistantLedger:
    """Streamlined append-only ledger processing system."""
    
    def __init__(self, ledger_file: str = "primary_chain.jsonl"):
        self.chain_path: Path = LEDGER_DIR / ledger_file
        self.chain: List[LedgerBlock] = []
        self._load_chain()

    def _load_chain(self) -> None:
        if not self.chain_path.exists():
            self._create_genesis_block()
            return

        with open(self.chain_path, "r", encoding="utf-8") as f:
            fcntl.flock(f.fileno(), fcntl.LOCK_SH)  # Shared read lock
            try:
                for line in f:
                    line = line.strip()
                    if not line:
                        continue
                    block_dict = json.loads(line)
                    block = LedgerBlock(
                        index=block_dict["index"],
                        timestamp=block_dict["timestamp"],
                        task_id=block_dict["task_id"],
                        outcome_metrics=block_dict["outcome_metrics"],
                        previous_hash=block_dict["previous_hash"],
                    )
                    block.block_hash = block_dict["block_hash"]
                    block.signature = block_dict.get("signature", "")
                    self.chain.append(block)
            finally:
                fcntl.flock(f.fileno(), fcntl.LOCK_UN)

        logger.debug("Ledger loaded – %d blocks parsed.", len(self.chain))

    def _create_genesis_block(self) -> None:
        genesis = LedgerBlock(
            index=0,
            timestamp=time.time(),
            task_id="GENESIS_0000",
            outcome_metrics="INITIALIZATION",
            previous_hash="0" * 64,
        )
        self.chain.append(genesis)
        
        # Initial write requires file generation
        with open(self.chain_path, "w", encoding="utf-8") as f:
            fcntl.flock(f.fileno(), fcntl.LOCK_EX)
            try:
                json_str = json.dumps(genesis, cls=VitalisEncoder, ensure_ascii=False)
                f.write(json_str + "\n")
            finally:
                fcntl.flock(f.fileno(), fcntl.LOCK_UN)
        logger.info("Genesis block synchronized successfully.")

    def append_record(self, task_id: str, outcome_metrics: str) -> LedgerBlock:
        """Appends a single ledger line in O(1) time without rewriting historical lines."""
        last = self.chain[-1]
        new_block = LedgerBlock(
            index=last.index + 1,
            timestamp=time.time(),
            task_id=task_id,
            outcome_metrics=outcome_metrics,
            previous_hash=last.block_hash,
        )
        
        json_str = json.dumps(new_block, cls=VitalisEncoder, ensure_ascii=False)
        
        # Open in append mode 'a' - will NOT truncate file before lock execution
        with open(self.chain_path, "a", encoding="utf-8") as f:
            fcntl.flock(f.fileno(), fcntl.LOCK_EX)
            try:
                f.write(json_str + "\n")
                f.flush()
                try:
                    os.fsync(f.fileno())
                except Exception:
                    pass
            finally:
                fcntl.flock(f.fileno(), fcntl.LOCK_UN)
                
        self.chain.append(new_block)
        logger.info("Ledger appended – block %d (task %s)", new_block.index, task_id)
        return new_block

    def verify_integrity(self) -> bool:
        """Validates chronological continuity and hardware hmac signatures."""
        for i in range(1, len(self.chain)):
            cur = self.chain[i]
            prev = self.chain[i - 1]

            if cur.previous_hash != prev.block_hash:
                logger.error("Ledger structural broken at block %d", i)
                return False
            if not cur.is_valid():
                logger.error("Signature invalid at block %d. Modification detected.", i)
                return False
        logger.info("Ledger integrity verified – %d blocks validated.", len(self.chain))
        return True

    def latest_block(self) -> LedgerBlock:
        return self.chain[-1]