-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_narrative_continuity.py
More file actions
61 lines (55 loc) · 2.4 KB
/
Copy pathpatch_narrative_continuity.py
File metadata and controls
61 lines (55 loc) · 2.4 KB
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
#!/usr/bin/env python3
"""Patch tl_ego_server.py: narrative continuity (line-based replacement)."""
from pathlib import Path
fp = Path(r"C:\Users\Administrator\.openclaw\workspace\projects\tl-ego\tl_ego_server.py")
lines = fp.read_text(encoding="utf-8").splitlines()
# Lines are 0-indexed; Phase 7 starts at line 2277 (1-indexed 2278)
start = 2277 # 0-indexed line number for "# ── Phase 7: LLM Self-Narrative ──"
# Find actual start by scanning
for i in range(start, start + 5):
if "Phase 7: LLM Self-Narrative" in lines[i]:
start = i
break
# Find end of the _call_llm block
end = start
for i in range(start, len(lines)):
if 'raise ValueError("empty LLM response")' in lines[i]:
end = i
break
print(f"Replacing lines {start+1} to {end+1}")
print("BEFORE:")
for l in lines[start:end+1]:
print(f" {l}")
# Build replacement block
replacement = [
lines[start], # keep comment header
lines[start+1], # keep if-comment
"# ── NARRATIVE CONTINUITY: load recent past narratives from disk ──",
'_history_block = ""',
'if "narrative_history" in model and model["narrative_history"]:',
' _past = model["narrative_history"][-3:]',
' _history_block = "\\nYour recent past self-narratives (most recent first):\\n"',
' for _i, _sn in enumerate(reversed(_past)):',
' _history_block += f" {_i+1}: [{_sn.get(\'ts\',\'?\')[:16]}] {_sn.get(\'text\',\'\')[:120]}\\n"',
' _history_block += (',
' "\\nWhen writing your new narrative, briefly acknowledge how you have "',
' "evolved or shifted from your past self. Do NOT copy — continue the story.\\n\\n"',
' )',
"",
'if _llm_resp:',
' # Archive this narrative for future continuity (persisted in lae_self_model.json)',
' if "narrative_history" not in model:',
' model["narrative_history"] = []',
' model["narrative_history"].append({',
' "ts": datetime.now(tz).isoformat(),',
' "text": _llm_resp.strip(),',
' })',
' if len(model["narrative_history"]) > 10:',
' model["narrative_history"] = model["narrative_history"][-10:]',
' model["self_narrative"] = _llm_resp.strip()',
'else:',
' raise ValueError("empty LLM response")',
]
new_lines = lines[:start] + replacement + lines[end+1:]
fp.write_text("\n".join(new_lines), encoding="utf-8")
print("\nOK: patch applied.")