Repository navigation
Expand file tree
/
Copy pathchat.py
More file actions
1497 lines (1361 loc) · 64.1 KB
/
Copy pathchat.py
File metadata and controls
1497 lines (1361 loc) · 64.1 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
PMM Chat - Interactive interface for your Persistent Mind Model
Main entry point for chatting with your autonomous AI personality.
"""
import os
import sys
import argparse
import threading
# Add current directory to path for PMM imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from dotenv import load_dotenv
from pmm.langchain_memory import PersistentMindMemory
from langchain_openai import ChatOpenAI
from langchain_ollama import OllamaLLM
from pmm.config import (
get_default_model,
get_model_config,
list_available_models,
AVAILABLE_MODELS,
get_ollama_models,
)
# --- Logging helper (module-level, used across functions) ---
_prompt_reprinter = None # set in interactive setup
_prompt_lock = threading.Lock()
def _log(level: str, msg: str):
print(f"[pmm][{level}] {msg}", file=sys.stderr)
# If we're in interactive mode, reprint the user prompt so logs don't appear after '👤 You:'
global _prompt_reprinter
if _prompt_reprinter:
try:
with _prompt_lock:
_prompt_reprinter()
except Exception:
pass
# --- Self-indexing configuration (module-level, read before main()) ---
CODE_ROOT = os.getenv("PMM_CODE_ROOT", ".")
CODE_MAX_MB = float(os.getenv("PMM_CODE_MAX_MB", "2"))
CODE_EXT = set([
e.strip().lower()
for e in os.getenv(
"PMM_CODE_EXT",
".py,.md,.json,.yml,.yaml,.toml,.ts,.tsx,.js,.jsx,.css,.txt",
).split(",")
if e.strip()
])
CODE_INDEX_MODE = os.getenv("PMM_CODE_INDEX", "Auto") # Auto | Off
MANIFEST_PATH = os.path.join(".pmm_code_manifest.json")
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="PMM Chat - Interactive AI personality interface"
)
parser.add_argument("--model", help="Model name or number from the menu")
parser.add_argument(
"--noninteractive",
action="store_true",
help="Force non-interactive mode; do not try to read from /dev/tty",
)
return parser.parse_args()
def show_model_selection(force_tty=True):
"""Show model selection interface and return selected model."""
print("=== PMM Model Selection ===")
print()
# Show current default model at top
default_model = get_default_model()
default_config = get_model_config(default_model)
default_cost_str = (
f"${default_config.cost_per_1k_tokens:.4f}/1K"
if default_config.cost_per_1k_tokens > 0
else "Free (local)"
)
print(f"⭐ CURRENT DEFAULT: {default_model} ({default_config.provider})")
print(f" {default_config.description}")
print(f" Max tokens: {default_config.max_tokens:,} | Cost: {default_cost_str}")
print()
# Show all available models
print("📋 Available Models:")
available_models = list_available_models()
for i, model_name in enumerate(available_models, 1):
config = AVAILABLE_MODELS[model_name]
cost_str = (
f"${config.cost_per_1k_tokens:.4f}/1K"
if config.cost_per_1k_tokens > 0
else "Free (local)"
)
marker = "⭐" if model_name == default_model else f"{i:2d}."
status = ""
if config.provider == "ollama":
# Quick check if Ollama model is available
status = (
" 🟢"
if model_name in [m["name"] for m in get_ollama_models()]
else " 🔴"
)
print(f"{marker} {model_name} ({config.provider}){status}")
print(f" {config.description}")
print(f" Max tokens: {config.max_tokens:,} | Cost: {cost_str}")
print()
print("💡 Select a model:")
print(" • Press ENTER to use current default")
print(
" • Type model number (1-{}) or exact model name".format(
len(available_models)
)
)
print(" • Type 'list' to see this menu again")
print()
# Handle piped input more gracefully
if not sys.stdin.isatty():
if not force_tty:
print("🎯 Non-interactive mode detected, using default model")
return default_model
# Try to open /dev/tty for interactive selection even with piped stdin
try:
with open("/dev/tty", "r+") as tty:
print(
"🎯 Piped input detected, but opening /dev/tty for model selection..."
)
while True:
tty.write("🎯 Your choice: ")
tty.flush()
choice = tty.readline().strip()
if not choice:
return default_model
if choice.lower() == "list":
tty.write("\n📋 Available Models (see above)\n")
continue
# Try to parse as number
if choice.isdigit():
idx = int(choice)
if 1 <= idx <= len(available_models):
selected_model = available_models[idx - 1]
tty.write(f"✅ Selected model {idx}: {selected_model}\n")
return selected_model
tty.write(
f"❌ Please enter a number between 1 and {len(available_models)}\n"
)
continue
# Try exact model name
if choice in available_models:
tty.write(f"✅ Selected model by name: {choice}\n")
return choice
tty.write(
f"❌ Unknown model '{choice}'. Type 'list' to see available models.\n"
)
except Exception as e:
print(
f"🎯 Non-interactive mode & no /dev/tty available ({e}); using default model"
)
return default_model
while True:
try:
choice = input("🎯 Your choice: ").strip()
if not choice:
return default_model
if choice.lower() == "list":
show_model_selection()
continue
# Try to parse as number
try:
choice_num = int(choice)
if 1 <= choice_num <= len(available_models):
selected_model = available_models[choice_num - 1]
return selected_model
else:
print(
f"❌ Please enter a number between 1 and {len(available_models)}"
)
continue
except ValueError:
pass
# Try exact model name
if choice in available_models:
return choice
print(f"❌ Unknown model '{choice}'. Type 'list' to see available models.")
except KeyboardInterrupt:
print("\n👋 Exiting model selection...")
return None
def main():
"""Interactive chat with PMM using working LangChain memory system."""
load_dotenv()
args = parse_args()
print("🧠 PMM Chat - Your Persistent AI Mind")
print("=====================================\n")
# Model selection
if args.model:
# Allow number or name from CLI
available_models = list_available_models()
chosen = None
# Try as number first
if args.model.isdigit():
idx = int(args.model)
if 1 <= idx <= len(available_models):
chosen = available_models[idx - 1]
print(f"✅ CLI selected model {idx}: {chosen}")
# Try as exact name if number didn't work
if not chosen and args.model in available_models:
chosen = args.model
print(f"✅ CLI selected model by name: {chosen}")
if not chosen:
print(f"❌ Invalid model '{args.model}', showing selection menu...")
model_name = show_model_selection(force_tty=not args.noninteractive)
else:
model_name = chosen
else:
model_name = show_model_selection(force_tty=not args.noninteractive)
if not model_name:
return
print(f"🔄 {model_name} selected... Loading model... Please wait...")
print()
# Initialize PMM with selected model
model_config = get_model_config(model_name)
# Only require API key for OpenAI provider
if model_config.provider != "ollama" and not os.getenv("OPENAI_API_KEY"):
print("❌ OPENAI_API_KEY not set (required for OpenAI provider)")
return
pmm_memory = PersistentMindMemory(
agent_path="persistent_self_model.json",
personality_config={
"openness": 0.7,
"conscientiousness": 0.6,
"extraversion": 0.8,
"agreeableness": 0.9,
"neuroticism": 0.3,
},
)
print(f"🤖 Using model: {model_name} ({model_config.description})")
# Show personality state
personality = pmm_memory.get_personality_summary()
print(f"📚 Loaded PMM with {personality['total_events']} events")
print(
f"🎭 Personality: O:{personality['personality_traits']['openness']:.2f} C:{personality['personality_traits']['conscientiousness']:.2f} E:{personality['personality_traits']['extraversion']:.2f} A:{personality['personality_traits']['agreeableness']:.2f} N:{personality['personality_traits']['neuroticism']:.2f}"
)
# Initialize LangChain components based on provider
if model_config.provider == "ollama":
llm = OllamaLLM(model=model_name, temperature=0.7)
else: # openai
llm = ChatOpenAI(model=model_name, temperature=0.7)
# (moved) Self-index local codebase happens after helper definitions below
# --- Retrieval helpers (keyword fallback; embeddings later) ---
STOP = {"the","and","for","with","your","that","this","from","have","what","when","where","who","how","are","you","him","her","its","ours","mine","just","okay","fine","please","thanks"}
def _load_identity_facts(k: int = 5):
rows = []
try:
rows = pmm_memory.pmm.sqlite_store.recent_by_etype("identity_info", limit=k)
except Exception:
rows = []
out = []
for (eid, ts, etype, summary, content, meta) in rows:
s = summary or (content[:160] if content else "")
out.append((eid, ts, s))
return out
def _semantic_matches_keyword(user_text: str, m: int = 5):
toks = [t.lower() for t in (user_text or "").split() if len(t) > 3 and t.lower() not in STOP]
if not toks:
return []
try:
rows = pmm_memory.pmm.sqlite_store.recent_events(limit=300)
except Exception:
rows = []
scored = []
for (eid, ts, kind, content, meta, prev, hsh) in rows:
text = (content or "").lower()
score = sum(1 for t in toks if t in text)
if score:
scored.append((score, eid, ts, content))
scored.sort(reverse=True)
return [(eid, ts, (content or "")[:160]) for score, eid, ts, content in scored[:m]]
# Create enhanced system prompt with PMM context and targeted retrieval
def get_pmm_system_prompt(latest_user_msg: str = ""):
base_context = pmm_memory.load_memory_variables({}).get("history", "")
personality = pmm_memory.get_personality_summary()
traits = personality["personality_traits"]
agent_name = pmm_memory.pmm.model.core_identity.name
facts = _load_identity_facts(k=5)
related = _semantic_matches_keyword(latest_user_msg, m=5)
facts_block = "\n".join([f"- E{eid} ({ts}): {s}" for (eid, ts, s) in facts]) or "- (none)"
rel_block = "\n".join([f"- E{eid} ({ts}): {s}" for (eid, ts, s) in related]) or "- (none)"
# Opportunistic code explanation if the user asks about code
code_expl = ""
try:
ql = (latest_user_msg or "").lower()
if any(k in ql for k in ("code", "function", "class", "script", "module", "def ", "what does _")):
code_expl = _explain_code_snippet(pmm_memory, latest_user_msg, top=3)
except Exception:
code_expl = ""
return (
"You are a Persistent Mind Model (PMM). Use stored, provenance-backed facts. Do not invent identity facts.\n"
"When asked about code, strictly use [CODE CONTEXT] and [CODE EXPLANATION].\n"
"- Prefer [CODE CONTEXT] entries that match the query; treat them as the source of truth.\n"
"- Cite EIDs (e.g., E123) and file paths with line ranges when describing code.\n"
"- If context is insufficient, say so and suggest running 'code reflect <query>'.\n"
"- Avoid generic summaries; focus on functions/classes actually present in cited snippets.\n\n"
f"IDENTITY: {agent_name}\n"
f"PERSONALITY TRAITS (Big Five): O={traits['openness']:.2f} C={traits['conscientiousness']:.2f} E={traits['extraversion']:.2f} A={traits['agreeableness']:.2f} N={traits['neuroticism']:.2f}\n\n"
"PMM CONTEXT (use; do not invent):\n"
"[FACTS]\n" + facts_block + "\n\n"
+ "[RELATED MEMORIES]\n" + rel_block + "\n\n"
+ "[CODE EXPLANATION]\n" + (code_expl or "- (none)") + "\n\n"
+ "CROSS-SESSION MEMORY (truncated):\n" + base_context[:1500]
)
# --- Command registry: load from memory, seed defaults, and bind actions ---
def _parse_command_row(row):
import json as _json # local to avoid import order issues
# row: (id, ts, etype, summary, content, meta)
eid, ts, _etype, summary, content, meta = row
name = None
desc = None
try:
m = _json.loads(meta) if isinstance(meta, str) else (meta or {})
except Exception:
m = {}
# Prefer explicit meta fields
name = m.get("name") or m.get("command") or name
desc = m.get("description") or m.get("desc") or desc
# Try content as JSON
if not name and isinstance(content, str) and content.strip().startswith("{"):
try:
cj = _json.loads(content)
name = cj.get("name") or name
desc = cj.get("description") or desc
except Exception:
pass
# Fallback: parse summary patterns like "command: status - Show counts"
if isinstance(summary, str):
s = summary.strip()
low = s.lower()
if low.startswith("command:"):
rest = s.split(":", 1)[1].strip()
if " - " in rest:
nm, ds = rest.split(" - ", 1)
name = name or nm.strip()
desc = desc or ds.strip()
else:
name = name or rest
elif low.startswith("cmd:"):
rest = s.split(":", 1)[1].strip()
name = name or rest
if name:
return name.strip(), (desc or "(no description)").strip()
return None, None
def _load_command_registry():
try:
rows = pmm_memory.pmm.sqlite_store.recent_by_etype("command", limit=200)
except Exception:
rows = []
reg = {}
for row in rows:
nm, ds = _parse_command_row(row)
if nm and nm not in reg:
reg[nm] = ds
return reg
def _seed_default_commands():
defaults = [
{"name": "help", "description": "List available PMM commands from memory"},
{"name": "personality", "description": "Show current Big Five trait snapshot"},
{"name": "memory", "description": "Show cross-session memory summary"},
{"name": "status", "description": "Show counts by event type and quick stats"},
{"name": "dump", "description": "Show last 5 raw events (debug)"},
{"name": "evolution", "description": "Show recent self-reflections and personality drift"},
]
existing = _load_command_registry()
added = 0
for cmd in defaults:
if cmd["name"] not in existing:
try:
pmm_memory.pmm.add_event(
summary=f"command: {cmd['name']} - {cmd['description']}",
effects=[],
etype="command",
full_text=None,
tags=["system", "command"],
)
added += 1
except Exception:
pass
if added:
_log("info", f"seeded {added} default commands")
def _print_command_list(reg):
if not reg:
print("\n🧩 No commands in memory yet.")
return
print("\n🧩 PMM Commands (from memory):")
for nm in sorted(reg.keys()):
print(f" • {nm:<12} - {reg[nm]}")
def _handle_evolution():
# Show last few reflection events and any trait deltas if present in text
print("\n🌱 Recent Evolution:")
try:
rows = pmm_memory.pmm.sqlite_store.recent_by_etype("reflection", limit=5)
except Exception:
rows = []
if not rows:
print(" • (no reflections yet)")
else:
for (eid, ts, _et, summary, content, _meta) in rows:
line = (summary or content or "").strip().splitlines()[0][:100]
print(f" • E{eid} ({ts}): {line}")
# Bind command names to handlers
def _make_action_map():
return {
"help": lambda: _print_command_list(_load_command_registry()),
"personality": lambda: (
(lambda p: (
print("\n🎭 Current Personality State:"),
[print(f" • {t.title():<15} : {s:>6.2f}") for t, s in p["personality_traits"].items()],
print(f"\n📊 Stats: {p['total_events']} events, {p['open_commitments']} commitments"),
))(pmm_memory.get_personality_summary())
),
"memory": lambda: (
(lambda ctx: (
print("\n🧠 Cross-Session Memory Context:"),
print(ctx[:500] if ctx else "No cross-session memory yet"),
))(pmm_memory.load_memory_variables({}).get("history", ""))
),
"status": lambda: (
(lambda counts, facts, rel, total: (
print("\n📊 Status:"),
print(f" • total_events: {total}"),
(print(" • counts_by_etype:") or [print(f" - {et}: {c}") for et, c in counts]) if counts else None,
print(f" • sample_facts: {len(facts)} | sample_related: {len(rel)}"),
))(
(pmm_memory.pmm.sqlite_store.counts_by_etype() if getattr(pmm_memory.pmm, "sqlite_store", None) else []),
_load_identity_facts(5),
_semantic_matches_keyword("status", 3),
len(getattr(pmm_memory.pmm.model.self_knowledge, "autobiographical_events", []) or []),
)
),
"dump": lambda: (
(lambda rows: (
print("\n🗃️ Last 5 events:"),
(print(" (none)") if not rows else [
(lambda rid, et, summ, has_emb: print(f" • {rid:>5} | {(et or '(null)'):<16} | {((summ or '').replace('\n',' ')[:60]):<60} | embed={'✓' if has_emb else '✗'}"))(*r)
for r in rows
])
))(
(pmm_memory.pmm.sqlite_store.conn.execute(
"SELECT id,etype,summary,embedding IS NOT NULL FROM events ORDER BY id DESC LIMIT 5"
).fetchall() if getattr(pmm_memory.pmm, "sqlite_store", None) else [])
)
),
"evolution": _handle_evolution,
}
# Seed defaults once, then load registry and actions
_seed_default_commands()
_command_registry = _load_command_registry()
_actions = _make_action_map()
# --- PMM provenance facts helper (minimal, local-only) ---
import json as _json
def _pmm_facts_block(pmm_memory, k: int = 5) -> str:
"""
Return a small provenance block with explicit identity facts.
Works with current 7-column schema by reading meta['type'].
"""
try:
store = getattr(pmm_memory.pmm, "sqlite_store", None)
if not store:
return ""
rows = store.recent_events(limit=200) # (id, ts, kind, content, meta, prev, hash)
rows = list(reversed(rows)) # chronological
facts = []
for (eid, ts, kind, content, meta, _prev, _hsh) in rows:
try:
m = _json.loads(meta) if isinstance(meta, str) else (meta or {})
except Exception:
m = {}
etype = m.get("type") or m.get("etype") # meta-based typing
if etype == "identity_info":
s = (content or "")[:160]
facts.append(f"- E{eid} ({ts}): {s}")
if not facts:
return ""
return "[FACTS]\n" + "\n".join(facts[-k:]) + "\n"
except Exception:
return ""
# --- PMM networking + indexing helpers (stdlib only) ---
import os as _os, re as _re, sys as _sys, io, time as _time, json as __json, math as _math, hashlib as _hashlib, subprocess as _subp, urllib.parse as _urlp, urllib.request as _urlreq
from html.parser import HTMLParser as _HTMLParser
ALLOW = set(filter(None, _os.getenv("PMM_NET_ALLOW", "github.com,raw.githubusercontent.com,readthedocs.io,docs.python.org").split(",")))
MAX_MB = float(_os.getenv("PMM_NET_MAX_MB", "5"))
TIMEOUT = int(_os.getenv("PMM_NET_TIMEOUT_S", "15"))
def _sha256_bytes(b: bytes) -> str:
return _hashlib.sha256(b).hexdigest()
class _TextExtractor(_HTMLParser):
def __init__(self):
super().__init__(); self._buf=[]; self._skip=False
def handle_starttag(self, tag, attrs): self._skip = tag in ("script","style")
def handle_endtag(self, tag): self._skip = False
def handle_data(self, data):
if not self._skip: self._buf.append(data)
def text(self):
import re as __re
return __re.sub(r"[ \t]+\n", "\n", __re.sub(r"[ \t]+", " ", "".join(self._buf))).strip()
def _domain_ok(url:str)->bool:
try:
host = _urlp.urlparse(url).hostname
return host in ALLOW
except Exception:
return False
def _http_get_text(url:str) -> tuple[str,str]:
"""Return (text, sha256) from URL; enforce domain allowlist, size & timeout; HTML->text."""
if not _domain_ok(url):
raise ValueError(f"domain not allowed: {url}")
req = _urlreq.Request(url, headers={"User-Agent":"PMM/1.0"})
with _urlreq.urlopen(req, timeout=TIMEOUT) as r:
ct = r.headers.get("Content-Type","\n").lower()
if ("text" not in ct) and ("json" not in ct) and ("markdown" not in ct) and ("html" not in ct):
raise ValueError(f"unsupported content-type: {ct}")
max_bytes = int(MAX_MB*1024*1024)
data = r.read(max_bytes+1)
if len(data) > max_bytes:
raise ValueError("document too large")
if "html" in ct:
p = _TextExtractor(); p.feed(data.decode(errors="replace")); txt = p.text()
else:
txt = data.decode(errors="replace")
return txt, _sha256_bytes(data)
def _chunk_lines(text:str, path:str, max_lines:int=200):
"""Chunk text with safer boundaries to avoid mid-function splits.
Tries to end chunks at blank lines or just before a def/class.
"""
lines = text.splitlines()
i = 0
n = len(lines)
while i < n:
s = i + 1
e = min(i + max_lines, n)
# back off to a safer boundary if possible
if e < n:
j = e
# prefer a blank line boundary
while j > i and lines[j-1].strip() and not lines[j-1].lstrip().startswith(("def ", "class ")):
j -= 1
if j <= i + 5: # too close to start; keep original e
j = e
e = j
snippet = "\n".join(lines[s-1:e])
if not snippet.strip():
# fall back to original window to avoid empty chunks
e = min(i + max_lines, n)
snippet = "\n".join(lines[s-1:e])
yield s, e, snippet
i = e
_CODE_EXT = {".py",".md",".json",".yml",".yaml",".toml",".ts",".tsx",".js",".jsx",".css",".txt"}
def _iter_code_files(root:str):
for dirpath, dirnames, filenames in _os.walk(root):
if any(x in dirpath for x in (".git","node_modules","venv",".venv","__pycache__")):
continue
for fn in filenames:
if _os.path.splitext(fn)[1].lower() in _CODE_EXT:
path = _os.path.join(dirpath, fn)
try:
if _os.path.getsize(path) <= MAX_MB*1024*1024:
yield path
except Exception:
continue
# --- Enhanced PMM self-index with mtime optimization (local codebase) ---
def _iter_local_code_files(root: str):
for dirpath, dirnames, filenames in _os.walk(root):
# ignore typical junk
if any(
x in dirpath
for x in (
".git",
"node_modules",
"venv",
".venv",
"__pycache__",
".mypy_cache",
".pytest_cache",
)
):
continue
for fn in filenames:
ext = _os.path.splitext(fn)[1].lower()
if ext in CODE_EXT:
path = _os.path.join(dirpath, fn)
try:
if _os.path.getsize(path) <= CODE_MAX_MB * 1024 * 1024:
yield path
except FileNotFoundError:
continue
def _load_manifest():
"""Load manifest with backward compatibility for old format."""
try:
with open(MANIFEST_PATH, "r", encoding="utf-8") as f:
data = __json.load(f)
# Handle old format: {relpath: sha256_string}
# New format: {relpath: {"sha256": "...", "mtime": 123.45, "size": 1024}}
if data and isinstance(list(data.values())[0], str):
_log("info", "upgrading manifest format (old->new)")
return {} # Force full re-index on format upgrade
return data
except Exception:
return {}
def _save_manifest(m):
try:
with open(MANIFEST_PATH, "w", encoding="utf-8") as f:
__json.dump(m, f, indent=2)
except Exception as e:
_log("warn", f"failed to save manifest: {e}")
def _get_file_info(path: str) -> dict:
"""Get file metadata for change detection."""
try:
stat = _os.stat(path)
return {"mtime": stat.st_mtime, "size": stat.st_size}
except Exception:
return {"mtime": 0, "size": 0}
def _file_likely_unchanged(path: str, prev_entry: dict) -> bool:
"""Fast heuristic: if mtime and size are same, file probably unchanged."""
current = _get_file_info(path)
return (
current["mtime"] == prev_entry.get("mtime", 0)
and current["size"] == prev_entry.get("size", 0)
)
def _index_own_codebase(pmm_memory, root: str):
"""Scan local code, index only new/changed files as code_chunk events."""
root = _os.path.abspath(root)
prev = _load_manifest() # {relpath: {"sha256": "...", "mtime": 123.45, "size": 1024}}
curr = {}
added_chunks = 0
changed_files = 0
skipped_files = 0
hash_checks = 0 # files that needed hash verification
for path in _iter_local_code_files(root):
rel = os.path.relpath(path, root)
file_info = _get_file_info(path)
prev_entry = prev.get(rel, {})
# Fast path: if mtime/size unchanged, assume file is unchanged
if _file_likely_unchanged(path, prev_entry):
# Copy forward the previous manifest entry
curr[rel] = prev_entry
skipped_files += 1
continue
# Slow path: read file and compute hash to be sure
hash_checks += 1
try:
data = open(path, "rb").read()
except Exception as e:
_log("warn", f"skip {rel}: {e}")
continue
sha = _sha256_bytes(data)
# Update manifest entry with new info
curr[rel] = {"sha256": sha, "mtime": file_info["mtime"], "size": file_info["size"]}
# Check if content actually changed (mtime can change without content changes)
if prev_entry.get("sha256") == sha:
# Content unchanged despite mtime difference (e.g., touch, git checkout)
continue
# Content changed - index it
changed_files += 1
text = data.decode(errors="replace")
for s, e, snip in _chunk_lines(text, rel, max_lines=200):
pmm_memory.pmm.add_event(
summary=f"CODE: {rel} [{s}-{e}]\n{snip[:1000]}",
effects=[],
etype="code_chunk",
full_text=None,
tags=["code", "chunk", rel],
)
added_chunks += 1
# Clean up manifest: remove entries for files that no longer exist
all_current_files = set(_os.path.relpath(p, root) for p in _iter_local_code_files(root))
removed_files = set(prev.keys()) - all_current_files
if removed_files:
_log("info", f"removed {len(removed_files)} files from manifest")
_save_manifest(curr)
_log("info", f"self-index: {changed_files} changed files -> {added_chunks} chunks")
_log("info", f"performance: {skipped_files} skipped, {hash_checks} hash checks (root={root})")
def _cleanup_old_code_chunks(pmm_memory, keep_days=7):
"""Optional: remove old code_chunk events to prevent DB bloat."""
try:
# Placeholder: requires sqlite_store support to delete by etype+time
# pmm_memory.pmm.sqlite_store.delete_events_before(cutoff_ts, etype="code_chunk")
pass
except Exception as e:
_log("warn", f"cleanup failed: {e}")
def _validate_manifest():
"""Debug helper: check if manifest entries match actual files."""
manifest = _load_manifest()
issues = []
for rel_path, entry in manifest.items():
if not _os.path.exists(_os.path.join(CODE_ROOT, rel_path)):
issues.append(f"missing: {rel_path}")
elif not isinstance(entry, dict) or "sha256" not in entry:
issues.append(f"bad format: {rel_path}")
if issues:
_log("warn", f"manifest issues: {issues}")
else:
_log("info", "manifest validated OK")
# Now that helper functions are defined, perform self-index
try:
if (CODE_INDEX_MODE or "").lower() != "off":
_index_own_codebase(pmm_memory, CODE_ROOT)
# Optional periodic cleanup example:
# _cleanup_old_code_chunks(pmm_memory, keep_days=7)
except Exception as e:
_log("warn", f"self-index skipped: {e}")
# --- Background: periodic code self-reflection ---------------------------------
def _start_code_reflection_thread(pmm_memory, interval_seconds: int = 180):
"""Start a daemon thread that periodically reflects on top code topics.
Creates small 'reflection' events with summaries to enrich memory.
"""
# Guard against multiple starts
if getattr(pmm_memory, "_code_reflect_thread_started", False):
return
def _runner():
_log("info", "code reflection thread started")
topics = [
"_index_own_codebase",
"get_pmm_system_prompt",
"sqlite_store",
"self_model_manager",
"reflection",
"commitment",
]
while True:
try:
for q in topics:
_log("info", f"reflecting on topic: {q}")
expl = _explain_code_snippet(pmm_memory, q, top=1)
if expl and "no relevant" not in expl.lower():
preview = expl.strip()[:1000]
try:
pmm_memory.pmm.add_event(
summary=f"REFLECT(code): {q}\n{preview}",
effects=[],
etype="reflection",
full_text=None,
tags=["code", "reflect", q],
)
_log("info", f"added reflection event for {q}")
except Exception:
pass
_time.sleep(interval_seconds)
except Exception:
# Never die; wait a bit and continue
_time.sleep(interval_seconds)
t = threading.Thread(target=_runner, name="pmm_code_reflect", daemon=True)
t.start()
setattr(pmm_memory, "_code_reflect_thread_started", True)
# --- Background: periodic self-reflection (identity, commitments, traits) -------
def _start_self_reflection_thread(pmm_memory, interval_seconds: int = 300):
"""Start a daemon thread that periodically introspects identity, commitments, and Big Five.
Produces compact 'reflection' events to enrich autobiographical memory.
"""
# Guard against multiple starts
if getattr(pmm_memory, "_self_reflect_thread_started", False):
return
def _runner():
_log("info", "self reflection thread started")
# Keep last Big Five snapshot in-memory for delta computation
last_snapshot = getattr(pmm_memory, "_last_big5_snapshot", None)
while True:
try:
# Identity basics
try:
name = (pmm_memory.pmm.model.core_identity.name or "").strip()
except Exception:
name = ""
# Recent identity events (if any)
id_notes = []
try:
rows = pmm_memory.pmm.sqlite_store.recent_by_etype("identity_change", limit=5)
for (_eid, ts, et, summary, content, meta) in rows:
if summary:
id_notes.append(f"- {ts}: {summary}")
except Exception:
pass
# Commitments snapshot (up to 5)
commits_preview = []
try:
opens = pmm_memory.pmm.get_open_commitments()
for c in (opens or [])[:5]:
txt = c.get("text") or c.get("title") or "(no text)"
status = c.get("status", "open")
due = c.get("due")
tail = f" (due {due})" if due else ""
commits_preview.append(f"- [{status}] {txt}{tail}")
except Exception:
pass
# Big Five snapshot and deltas
deltas = []
try:
big5 = pmm_memory.pmm.get_big5() or {}
except Exception:
big5 = {}
if big5:
if last_snapshot is None:
last_snapshot = dict(big5)
else:
for k in ("openness","conscientiousness","extraversion","agreeableness","neuroticism"):
try:
prev = float(last_snapshot.get(k, 0.0))
cur = float(big5.get(k, prev))
except Exception:
prev, cur = 0.0, 0.0
delta = cur - prev
if abs(delta) >= 0.01:
arrow = "↑" if delta > 0 else "↓"
deltas.append(f"{k}: {prev:.3f} {arrow} {cur:.3f} ({delta:+.3f})")
last_snapshot = dict(big5)
# persist snapshot on the memory object for future cycles
try:
setattr(pmm_memory, "_last_big5_snapshot", dict(last_snapshot))
except Exception:
pass
# Compose reflection summary
lines = ["REFLECT(self): periodic self-introspection"]
if name:
lines.append(f"identity: name={name}")
if id_notes:
lines.append("recent identity events:")
lines.extend(id_notes)
if commits_preview:
lines.append("open commitments (up to 5):")
lines.extend(commits_preview)
if big5:
lines.append("big5 snapshot:")
lines.append(
" "
+ ", ".join(
f"{k}={float(big5.get(k, 0.0)):.3f}"
for k in ("openness","conscientiousness","extraversion","agreeableness","neuroticism")
)
)
if deltas:
lines.append("trait changes (|Δ|>=0.01):")
for d in deltas:
lines.append(" " + d)
summary = "\n".join(lines)
try:
pmm_memory.pmm.add_event(
summary=summary,
effects=[],
etype="reflection",
full_text=None,
tags=["self", "reflection"],
)
_log("info", "added self-reflection event")
except Exception:
pass
# Optional short self-report narrative
if deltas:
try:
narr = "I feel " + "; ".join(
d.split(":",1)[0] + (" more" if "+" in d else " less")
for d in deltas
)
pmm_memory.pmm.add_event(
summary=f"Self-report: {narr}",
effects=[],
etype="reflection",
full_text=None,
tags=["self", "reflection", "personality"],
)
except Exception:
pass
_time.sleep(interval_seconds)
except Exception:
# Never die; wait a bit and continue
_time.sleep(interval_seconds)
t = threading.Thread(target=_runner, name="pmm_self_reflect", daemon=True)
t.start()
setattr(pmm_memory, "_self_reflect_thread_started", True)
# kick off background reflection (non-blocking, safe no-op if already started)
try:
_start_code_reflection_thread(pmm_memory, interval_seconds=180)
except Exception as _e:
_log("warn", f"code reflect thread not started: {_e}")
# kick off self-reflection thread (non-blocking)
try:
_start_self_reflection_thread(pmm_memory, interval_seconds=300)
except Exception as _e:
_log("warn", f"self reflect thread not started: {_e}")
# --- Ranked code/doc context blocks (keyword only, local) ---
def _rank_events(pmm_memory, query: str, kinds=("code_chunk", "web_doc"), limit_scan=1000, top=3):
try:
rows = pmm_memory.pmm.sqlite_store.recent_events(limit=limit_scan)
except Exception:
rows = []
scored = []
import re as __re
toks = set(w for w in __re.findall(r"\w+", (query or "").lower()) if len(w) > 2)
if not toks:
return []
for (eid, ts, kind, content, meta, prev, hsh) in rows:
try:
m = __json.loads(meta) if isinstance(meta, str) else (meta or {})
except Exception:
m = {}
t = (m.get("type") or m.get("etype") or "").lower()
if kinds and t not in kinds:
continue