File size: 5,886 Bytes
824c85b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from datetime import datetime
import hashlib
import os

class MemoryEngine:
    def __init__(self,
                 memory_path="SegmentMemory.csv",
                 short_term_path="ShortTermMemory.csv",
                 history_path="SegmentHistory.csv",
                 conflict_path="SegmentConflictMap.csv"):
        self.memory_path = memory_path
        self.short_term_path = short_term_path
        self.history_path = history_path
        self.conflict_path = conflict_path

        self.memory_df = self._load_csv(memory_path)
        self.short_term_df = self._load_csv(short_term_path)
        self.history_df = self._load_csv(history_path)
        self.conflict_df = self._load_csv(conflict_path)

    def _load_csv(self, path):
        return pd.read_csv(path) if os.path.exists(path) else pd.DataFrame()

    def _generate_segment_id(self):
        existing_ids = self.memory_df['SegmentID'].astype(int) if not self.memory_df.empty else []
        return str(max(existing_ids) + 1 if len(existing_ids) > 0 else 1).zfill(4)

    def _hash_text(self, text):
        return hashlib.sha256(text.encode()).hexdigest()[:12]

    def insert_segment(self, raw_text, concepts, terms, structure, datapoints, comparisons, applications, agent_id="system"):
        segment_id = self._generate_segment_id()
        hash_val = self._hash_text(raw_text)

        # Check for duplicates
        if hash_val in self.memory_df.get("HashChecksum", []):
            print("Duplicate detected. Skipping.")
            return

        # Trust & entropy logic
        trust_score = 0.75 if "error" not in raw_text.lower() else -0.4
        entropy = 0.5
        relevance = 0.6

        # Conflict detection
        conflict_rows = self.memory_df[self.memory_df["RawText"].str.contains(raw_text.split(" ")[0], case=False, na=False)]
        if not conflict_rows.empty:
            for _, row in conflict_rows.iterrows():
                self._add_conflict(row["SegmentID"], segment_id, agent_id)
            return

        # Accept segment
        new_row = {
            "SegmentID": segment_id,
            "RawText": raw_text,
            "Concepts": concepts,
            "Terms": terms,
            "Structure": structure,
            "DataPoints": datapoints,
            "Comparisons": comparisons,
            "Applications": applications,
            "Links": "",
            "Entropy": entropy,
            "TrustScore": trust_score,
            "RelevanceScore": relevance,
            "RecallCount": 0,
            "LastUsed": "",
            "ConflictsWith": "",
            "Verified": trust_score > 0.5,
            "HashChecksum": hash_val
        }

        self.memory_df = pd.concat([self.memory_df, pd.DataFrame([new_row])], ignore_index=True)
        self.memory_df.to_csv(self.memory_path, index=False)

        self._log_history(agent_id, segment_id, "insert", trust_score, trust_score, relevance, relevance, entropy, entropy, "manual_insert", False)
        self._add_to_short_term(agent_id, segment_id, raw_text, relevance, trust_score)

        print(f"Segment {segment_id} inserted.")

    def _log_history(self, agent_id, seg_id, action, c_before, c_after, r_before, r_after, e_before, e_after, context, conflict):
        log_entry = {
            "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "AgentID": agent_id,
            "SegmentID": seg_id,
            "ActionType": action,
            "ConfidenceBefore": c_before,
            "ConfidenceAfter": c_after,
            "RelevanceBefore": r_before,
            "RelevanceAfter": r_after,
            "EntropyBefore": e_before,
            "EntropyAfter": e_after,
            "UsedInContext": context,
            "ConflictTriggered": conflict,
            "Notes": ""
        }
        self.history_df = pd.concat([self.history_df, pd.DataFrame([log_entry])], ignore_index=True)
        self.history_df.to_csv(self.history_path, index=False)

    def _add_conflict(self, seg_a, seg_b, agent_id):
        entry = {
            "SegmentA": seg_a,
            "SegmentB": seg_b,
            "ConflictType": "Concept Overlap",
            "ConflictScore": 0.8,
            "ResolutionStrategy": "Manual Review",
            "WinningSegment": "",
            "LastChecked": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "ResolutionStatus": "Pending Review",
            "AgentTrigger": agent_id,
            "Notes": "Auto conflict detection"
        }
        self.conflict_df = pd.concat([self.conflict_df, pd.DataFrame([entry])], ignore_index=True)
        self.conflict_df.to_csv(self.conflict_path, index=False)
        print(f"Conflict detected between {seg_a} and {seg_b}. Routed to conflict map.")

    def _add_to_short_term(self, agent_id, seg_id, raw_text, relevance, confidence):
        row = {
            "AgentID": agent_id,
            "SegmentID": seg_id,
            "RawText": raw_text,
            "Priority": relevance,
            "ActivationTime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "ExpiresAt": "",
            "DecayRate": 0.03,
            "RecencyScore": 1.0,
            "FocusLock": False,
            "UsageContext": "manual_insert",
            "ConfidenceScore": confidence,
            "LastInteraction": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }
        self.short_term_df = pd.concat([self.short_term_df, pd.DataFrame([row])], ignore_index=True)
        self.short_term_df.to_csv(self.short_term_path, index=False)

# Example usage
if __name__ == "__main__":
    brain = MemoryEngine()
    brain.insert_segment(
        "Hydrogen is the first element on the periodic table.",
        "hydrogen, atomic number 1",
        "hydrogen, element, periodic",
        "Chemistry > Elements",
        "Atomic number: 1",
        "Compared with helium",
        "Used in gas balloons"
    )