-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdashboard.py
More file actions
2963 lines (2654 loc) · 141 KB
/
Copy pathdashboard.py
File metadata and controls
2963 lines (2654 loc) · 141 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
"""eiDOS dashboard — operator shell: web UI + supervisor/watchdog.
Two co-located responsibilities (phase 8.3 split VOICE out into its own process — voice.py — so a
native TTS/ffmpeg crash can't take the watchdog down; the UI HTML lives in static/dashboard.html):
UI — HTML dashboard (static/dashboard.html) + /api/status,/api/ping,/api/activity models
SUPERVISOR — watchdog (spawn/respawn/crash-loop auto-rollback), /api/control/* + the event-driven
/api/control/wait channel, git safety, self-edit apply, self-guide apply (trust boundary)
The browser loads this page from here (port 8099) but opens the speech SSE + audio streams directly
to the voice service (config.voice_port); eidos POSTs speech and yields the GPU gate there too.
Writes: paused/should_run/pid sentinels, chat_hold.json, interventions/, self_guide.md, watchdog
crash notes, and the source tree via git restore / self-edit apply. Stdlib only — no dependencies.
"""
import argparse
import json
import logging
import re
import sys
import threading
import time
from http.server import HTTPServer, ThreadingHTTPServer, BaseHTTPRequestHandler
from pathlib import Path
# Add project root for imports
sys.path.insert(0, str(Path(__file__).parent))
logger = logging.getLogger("dashboard")
from config import load_config, Config
from ascii_art import get_creature
from persona import load_persona, compute_level
from telemetry import get_cpu_pct
from typed_boundary import DashboardPayloadError, validate_dashboard_post_payload
import os
import creature_gen
import glue
from atomicio import replace_with_retry
from growth import build_growth
def _read_json(path: Path) -> dict:
try:
return json.loads(path.read_text())
except (FileNotFoundError, json.JSONDecodeError, OSError):
return {}
def _read_text(path: Path) -> str:
try:
return path.read_text()
except (FileNotFoundError, OSError):
return ""
_LAST_TOOL_SKIP = {"system", "watchdog", "dream", "thought", "planning", "__no_tool__"}
def _last_tool_call(config: Config) -> dict:
"""Most recent *real* tool call from observations.jsonl, for the tool bubble.
Skips meta entries (thoughts, planning, watchdog/system, dream). Returns a small
dict {tool, ok, summary, tick} or None.
"""
path = config.workspace / "observations.jsonl"
try:
lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
except OSError:
return None
for ln in reversed(lines[-80:]):
try:
o = json.loads(ln)
except Exception: # noqa: BLE001
continue
tool = o.get("tool")
if not tool or tool in _LAST_TOOL_SKIP:
continue
args = o.get("args") or {}
summ = ""
if isinstance(args, dict):
summ = (args.get("cmd") or args.get("command") or args.get("path")
or args.get("url") or args.get("skill_name") or "")
return {
"tool": tool,
"ok": bool(o.get("success")),
"summary": str(summ)[:64],
"tick": o.get("tick"),
}
return None
def _tail_jsonl(path: Path, n: int = 20) -> list:
try:
lines = path.read_text().strip().splitlines()
result = []
for line in lines[-n:]:
try:
result.append(json.loads(line))
except json.JSONDecodeError:
continue
return result
except (FileNotFoundError, OSError):
return []
def _compute_narration(heartbeat: dict, persona: dict, goal: str, flavor: dict) -> str:
"""Derive a status narration from current state."""
failures = heartbeat.get("consecutive_failures", 0)
tick = heartbeat.get("tick", 0)
uptime = heartbeat.get("uptime_s", 0)
mood = persona.get("mood", "curious")
streak = persona.get("current_streak", 0)
if failures >= 3:
return "Struggling... something isn't working. Might need a different approach."
if not goal.strip():
return "No goal set. Waiting for instructions."
if tick <= 1:
return "Just woke up. Getting my bearings."
if mood == "triumphant":
return "Just finished a goal. Feeling accomplished."
if mood == "frustrated":
return "Running into walls. Need to think differently."
if mood == "struggling":
return "Things are rough but not giving up."
if streak > 20:
return f"Good flow \u2014 {streak} successful actions in a row."
if uptime and uptime > 86400:
days = uptime / 86400
return f"Been at this for {days:.1f} days. Steady progress."
if mood == "focused":
return "Locked in. Making progress."
if mood == "determined":
return "Working through challenges. Pushing forward."
return "Working on it. One step at a time."
def build_knowledge_list(config: Config) -> dict:
"""Read last 10 knowledge entries from index."""
idx_path = config.workspace / "knowledge" / "index.json"
try:
entries = json.loads(idx_path.read_text())
except (FileNotFoundError, json.JSONDecodeError, OSError):
entries = []
entries.sort(key=lambda e: e.get("created", ""), reverse=True)
return {"entries": entries[:25]}
# --- Control-change channel (event-driven; ARCHITECTURE_PRINCIPLES.md #1) -----------------
# The reverse of the GPU gate: the dashboard is the PRODUCER of control state (pause/resume,
# listening hold, chat arrival) and eidos is the consumer. v1 made eidos poll three sentinel
# files on timers (pause @5s, hold @2s, interventions @<=2s) — delay-based guessing. Now every
# control mutation bumps a sequence counter and notifies; eidos makes ONE long-poll to
# /api/control/wait that returns the instant anything changes (or at its bounded timeout).
# The sentinel files REMAIN the crash-survivable ground truth — eidos re-reads them on wake and
# falls back to nap-polling if this channel is down. It's the polled consumption that violated
# the principle, not the files.
_ctl_cond = threading.Condition()
_ctl_seq = 0 # bumped on every control-state change; guarded by _ctl_cond
def control_notify(reason: str = "") -> None:
"""Producer hook: call after ANY control-state mutation (pause/resume/hold/chat)."""
global _ctl_seq
with _ctl_cond:
_ctl_seq += 1
_ctl_cond.notify_all()
def control_wait(config, since: int, max_s: float = 25.0) -> dict:
"""Block until the control seq passes `since` (event) or `max_s` elapses (bounded long-poll).
Returns the new seq + a state snapshot so the consumer never needs a second request."""
start = time.monotonic()
max_s = max(0.0, min(float(max_s), 60.0))
with _ctl_cond:
while _ctl_seq <= since:
remaining = max_s - (time.monotonic() - start)
if remaining <= 0:
break
_ctl_cond.wait(timeout=remaining)
seq = _ctl_seq
snap = {"seq": seq, "paused": False, "held": False, "interventions": 0}
try:
snap["paused"] = (config.workspace / "paused").exists()
snap["held"] = config.chat_hold_path.exists()
idir = config.interventions_dir
if idir.exists():
snap["interventions"] = sum(
1 for p in idir.iterdir()
if not p.name.startswith(".") and p.suffix != ".done")
except OSError:
pass
return snap
def build_dream_list(config: Config) -> dict:
"""Read last 10 memory snapshots (dream records)."""
snap_dir = config.workspace / "snapshots"
if not snap_dir.exists():
return {"dreams": []}
# Prefer real dream records (the briefing dream cycle's distillation: flavor + learned + plan).
# Fall back to legacy memory_snapshot_* files. The <80-char filter below drops empty stubs.
snapshots = sorted(
list(snap_dir.glob("dream_*.md")) + list(snap_dir.glob("memory_snapshot_*")),
key=lambda p: p.stat().st_mtime,
reverse=True, # newest first -> renders newest-at-top
)
dreams = []
for snap in snapshots:
try:
content = snap.read_text()
except OSError:
continue
if len(content.strip()) < 80:
continue # skip empty startup/test stubs that clutter the journal
dreams.append({
"ts": snap.stem.replace("memory_snapshot_", "").replace("dream_", ""),
"chars": len(content),
"preview": content[:300],
})
if len(dreams) >= 10:
break
return {"dreams": dreams}
def _disk_total_gb() -> float:
"""Total size of the drive the dashboard runs from (for the disk gauge scale)."""
try:
import shutil
return round(shutil.disk_usage(__file__).total / (1024 ** 3), 1)
except OSError:
return 0.0
# --- Procedural creature (workspace/creature.json; dashboard is sole writer) ---
_CREATURE_LOCK = threading.Lock() # ThreadingHTTPServer → concurrent /api/status
_HATCH_XP = 25 # persona awards +1 XP per successful tick
def _jobs_list(config: Config) -> list:
"""jobs.json is a JSON ARRAY (unlike the dict files _read_json serves)."""
try:
data = json.loads((config.workspace / "jobs.json").read_text())
return data if isinstance(data, list) else []
except (FileNotFoundError, json.JSONDecodeError, OSError):
return []
def _delegate_running(config: Config) -> bool:
return any(j.get("kind") == "delegate" and j.get("status") == "running"
for j in _jobs_list(config))
def _listening_hold_fresh(config: Config) -> bool:
"""Mirror eidos._chat_hold_active's freshness RULES (eidos.py:255-261): held, within the
TTL, AND under the continuous ceiling. eiDOS stops honoring a hold after
chat_hold_max_continuous_s, so a forgotten focused tab must stop rendering as listening
too — TTL alone let it show ~5 min of false listening."""
hold = _read_json(config.workspace / "state" / "chat_hold.json")
if not hold.get("held"):
return False
try:
now = time.time()
ts = float(hold.get("ts", 0))
age = now - ts
if age < 0 or age > float(getattr(config, "chat_hold_ttl_s", 60.0)):
return False
first = float(hold.get("first_held_ts", ts) or ts)
if now - first > float(getattr(config, "chat_hold_max_continuous_s", 300.0)):
return False
except (TypeError, ValueError):
return False
return True
def _creature_path(config: Config) -> Path:
return config.workspace / "creature.json"
# --- Terrarium garden builder (read-only; reflects eiDOS's real growth) ---
import calendar # noqa: E402
import hashlib # noqa: E402
_GARDEN_CACHE = {} # path -> (mtime, parsed_index)
def _iso_epoch(s: str) -> float:
try:
return float(calendar.timegm(time.strptime(s, "%Y-%m-%dT%H:%M:%SZ")))
except (ValueError, TypeError):
return 0.0
def _slot(record_id: str, n: int) -> int:
"""Stable per-record slot — md5, NOT Python hash() (salted per process)."""
return int(hashlib.md5(str(record_id).encode("utf-8")).hexdigest(), 16) % n
def _hatched_ts(doc: dict) -> float:
for e in reversed(doc.get("events", [])):
if e.get("kind") == "hatched":
return float(e.get("ts", 0)) or float(doc.get("born_ts", 0) or 0)
return float(doc.get("born_ts", 0) or 0)
def _read_index_cached(config: Config) -> list:
p = config.workspace / "knowledge" / "index.json"
try:
mtime = p.stat().st_mtime
except OSError:
return []
cached = _GARDEN_CACHE.get(p)
if cached and cached[0] == mtime:
return cached[1]
try:
data = json.loads(p.read_text(encoding="utf-8"))
data = data if isinstance(data, list) else []
except (OSError, json.JSONDecodeError, ValueError):
data = []
_GARDEN_CACHE[p] = (mtime, data)
return data
def _build_garden(config: Config, doc: dict, persona: dict) -> dict:
"""Per-slot counts of THIS incarnation's lived experience. Two filters make
the garden a biography: drop seed (bootstrap) records, and drop anything
created before this creature hatched (the knowledge store outlives a wipe)."""
hatched = _hatched_ts(doc)
buckets = {"facts": [0] * creature_gen.FACT_SLOTS, "procedures": [0] * creature_gen.TREE_SLOTS,
"reflections": [0] * creature_gen.MOSS_SLOTS, "errors": [0] * creature_gen.STONE_SLOTS}
for rec in _read_index_cached(config):
if rec.get("source_goal") == "seed":
continue
if _iso_epoch(rec.get("created", "")) < hatched:
continue
slots = buckets.get(rec.get("category"))
if slots is None:
continue
slots[_slot(rec.get("id", ""), len(slots))] += 1
# done objectives
done = 0
try:
obj = _read_json(config.workspace / "objectives.json")
done = sum(1 for o in obj.get("objectives", []) if o.get("state") == "done")
except Exception: # noqa: BLE001
pass
# unconsumed interventions (consumed ones are renamed *.md.done)
mail = False
try:
idir = config.interventions_dir
mail = idir.exists() and any(idir.glob("*.md"))
except Exception: # noqa: BLE001
pass
return {
"facts": buckets["facts"], "trees": buckets["procedures"],
"moss": buckets["reflections"], "stones": buckets["errors"],
"titles": len(persona.get("titles") or []),
"done": done, "mail": bool(mail),
}
def _save_creature(config: Config, doc: dict) -> None:
config.workspace.mkdir(parents=True, exist_ok=True)
tmp = _creature_path(config).with_suffix(".tmp")
tmp.write_text(json.dumps(doc, indent=2), encoding="utf-8")
replace_with_retry(tmp, _creature_path(config))
def _load_or_create_creature(config: Config) -> dict:
"""Read creature.json, or lay a brand-new egg (fresh incarnation = new genome).
Seed unity (CREATURE_GENETICS.md red gate #5): the germline authority is
workspace/genome.json — drawn once at the creature's first breath. When laying a NEW egg,
adopt that seed so the dashboard creature, the behavioral genome, and the phenotype
descriptions are ONE individual. Only when no genome exists yet (dashboard polled before
eidos ever booted) does the egg fall back to its own draw — the pre-unification behavior."""
with _CREATURE_LOCK:
doc = _read_json(_creature_path(config))
genome = doc.get("genome") or {}
# eidos is the SOLE seed authority (workspace/genome.json, drawn once at first breath).
# Read its germline seed first so the egg can ADOPT it.
germ_seed = None
try:
germ = _read_json(config.workspace / "genome.json")
if germ.get("seed"):
germ_seed = int(germ["seed"])
except Exception: # noqa: BLE001 - a missing/corrupt germline never blocks the egg
germ_seed = None
# An existing creature of the current genome version is authoritative — UNLESS it was laid
# PROVISIONALLY (dashboard polled before eidos birthed the germline) and the real germline has
# since appeared with a DIFFERENT seed: then re-lay to adopt eidos's seed so seed unity
# (CREATURE_GENETICS red gate #5) holds on first boot instead of two uncoordinated draws.
if (doc.get("seed") and genome.get("v") == creature_gen.GENOME_VERSION
and not (doc.get("seed_provisional") and germ_seed is not None
and germ_seed != int(doc["seed"]))):
return doc
# Prefer the germline seed; fall back to a self-drawn PROVISIONAL seed only when eidos hasn't
# birthed the germline yet — that egg is re-adopted (above) the moment genome.json appears.
provisional = germ_seed is None
seed = germ_seed if germ_seed is not None else int.from_bytes(os.urandom(8), "big")
doc = {
"v": 1,
"seed": seed,
"seed_provisional": provisional,
"genome": creature_gen.genome_from_seed(seed),
"born_ts": time.time(),
"hatched": False,
"hatch_xp": _HATCH_XP,
"events": [{"ts": time.time(), "kind": "laid"}],
}
try:
_save_creature(config, doc)
except OSError:
logger.exception("creature.json save failed (continuing in-memory)")
return doc
def _update_hatch(config: Config, doc: dict, persona: dict) -> dict:
"""Hatch progress from persona XP. Persists ONLY on threshold crossings
(cracks at 1/3 and 2/3, hatch at 1.0) — never churns disk on a plain poll."""
if doc.get("hatched"):
return {"hatched": True, "progress": 1.0}
xp = persona.get("xp", 0)
progress = min(1.0, xp / max(1, doc.get("hatch_xp", _HATCH_XP)))
events = doc.setdefault("events", [])
have_cracks = sum(1 for e in events if e.get("kind") == "crack")
want_cracks = (1 if progress >= 0.34 else 0) + (1 if progress >= 0.67 else 0)
changed = False
for n in range(have_cracks + 1, want_cracks + 1):
events.append({"ts": time.time(), "kind": "crack", "n": n})
changed = True
if progress >= 1.0:
doc["hatched"] = True
events.append({"ts": time.time(), "kind": "hatched"})
changed = True
if changed:
with _CREATURE_LOCK:
try:
_save_creature(config, doc)
except OSError:
logger.exception("creature.json hatch update failed")
return {"hatched": bool(doc.get("hatched")), "progress": round(progress, 3)}
_METAMORPHOSIS_S = 60.0 # how long the cocoon interlude lasts
def _update_stage_events(config: Config, doc: dict, stage: str) -> None:
"""Phase B: record stage transitions. A non-egg UPGRADE (juvenile→adult etc.)
triggers a metamorphosis event + cocoon interlude; hatching, downgrades, and
the first-ever record (pre-Phase-B creatures) pass silently."""
last = doc.get("last_stage")
if stage == last:
return
order = creature_gen.STAGES
is_upgrade = (last in order and stage in order
and last != "egg" and stage != "egg"
and order.index(stage) > order.index(last))
doc["last_stage"] = stage
if is_upgrade:
doc["interlude_until"] = time.time() + _METAMORPHOSIS_S
doc.setdefault("events", []).append(
{"ts": time.time(), "kind": "metamorphosis", "stage": stage})
with _CREATURE_LOCK:
try:
_save_creature(config, doc)
except OSError:
logger.exception("creature.json stage update failed")
def build_creature_spec(config: Config, persona: dict, heartbeat: dict,
goal: str) -> dict:
"""The living-creature payload: genome morphology + v2 truth expression."""
doc = _load_or_create_creature(config)
hatch = _update_hatch(config, doc, persona)
stage = creature_gen.stage_for(persona.get("level", 1), hatch["hatched"])
_update_stage_events(config, doc, stage)
try:
condition = glue.compute_condition(glue.recent_outcomes(config))
except Exception: # noqa: BLE001 — truth display must not break the page
condition = "STABLE"
expr = {
"condition": condition,
"delegating": _delegate_running(config),
"listening": _listening_hold_fresh(config),
"dead": heartbeat.get("consecutive_failures", 0) >= 5,
"paused": (config.workspace / "paused").exists(),
"has_goal": bool(goal.strip()),
}
spec = creature_gen.build_spec(doc["genome"], stage, hatch, expr)
until = float(doc.get("interlude_until") or 0)
if stage != "egg" and until > time.time():
# Mid-metamorphosis: the body is wrapped — swap in the chrysalis grids.
spec.update(creature_gen.compose_cocoon(doc["genome"], stage))
spec["interlude"] = {"kind": "cocoon", "until_ts": until}
spec["events"] = doc.get("events", [])[-5:]
try:
spec["terrarium"] = creature_gen.compose_terrarium(
doc["genome"], _build_garden(config, doc, persona))
except Exception: # noqa: BLE001 — the garden must never break the page
logger.exception("terrarium build failed")
spec["delegates"] = _delegates_payload(config)
spec["pending"] = _build_pending(config)
spec["beats"] = _update_beats(config, doc)
bond = _accrue_bond(config, doc)
spec["bond_expr"] = {"tier": int(bond.get("tier", 0))}
spec["bond_hover"] = dict(bond.get("counts", {}))
spec["identity"] = _identity_payload(config)
spec["ladder"] = _ladder_payload(config)
spec["quest"] = _active_quest_payload(config)
return spec
def _identity_payload(config: Config) -> dict:
"""The creature's genetic identity (genetics v2): morph + germline seed for the Buddy pane's
nameplate. Operator-facing — the fourth wall doesn't apply to the forge's window."""
try:
g = json.loads((config.workspace / "genome.json").read_text(encoding="utf-8"))
out = {"morph": str(g.get("morph") or ""), "seed": str(g.get("seed") or "")}
try:
import genome as _genome
out["nature"] = _genome.nature_name(g) # operator-only personality label — never the creature's
except Exception: # noqa: BLE001 — label is best-effort, never breaks the nameplate
pass
return out
except Exception: # noqa: BLE001 — pre-genome workspace: the pane just omits the line
return {}
def _ladder_payload(config: Config) -> dict:
"""The tool-unlock ladder at a glance: every unit in ladder order, with its books state
(granted source / pending hold reason). Flag off → {} and the pane renders nothing."""
try:
if not getattr(config, "pillars_tool_unlocks_enabled", False):
return {}
import unlocks
st = unlocks.UnlockState(config)
return {"units": [u.id for u in unlocks.UNITS],
"granted": {k: str(v.get("source") or "") for k, v in st.granted.items()},
"pending": dict(st.pending)}
except Exception: # noqa: BLE001 — the ladder must never break the page
return {}
def _active_quest_payload(config: Config) -> dict:
"""What the System has ISSUED to the creature right now (not the Administrator's proposal
queue — that has its own panel). Empty dict when nothing is active."""
try:
import quests
q = quests.QuestStore(config).active()
if q is None:
return {}
return {"id": q.id, "directive": q.directive, "tier": int(getattr(q, "tier", 1) or 1)}
except Exception: # noqa: BLE001 — the System must never break the page
return {}
def _delegates_payload(config: Config) -> list:
"""All delegate jobs (running + recently-finished; jobs.json keeps the last 15)
so the client mini-me can catch the return transition on the 2.5s poll. A 1:1
render of reality — the mini-me IS the delegate job's live state."""
out = []
for j in _jobs_list(config):
if j.get("kind") != "delegate":
continue
out.append({"name": j.get("name"), "mode": j.get("mode", "research"),
"status": j.get("status", "running"),
"started_ts": j.get("started_ts", 0)})
return out
def _build_pending(config: Config) -> dict:
"""What eiDOS is asking Dean to approve RIGHT NOW — the actionable pull. A
self-guide proposal staged, and/or self-edit proposals awaiting review. The
creature holds up a tablet while this is non-empty (client renders it)."""
try:
sg = config.self_guide_proposed_path.exists()
except Exception: # noqa: BLE001
sg = False
se = 0
try:
import selfedit
se = sum(1 for m in selfedit.list_proposals(config, kind="self_edit")
if m.get("status") == "pending")
except Exception: # noqa: BLE001
se = 0
return {"self_guide": bool(sg), "selfedits": int(se)}
def _count_consumed(config: Config) -> int:
try:
idir = config.interventions_dir
return sum(1 for _ in idir.glob("*.md.done")) if idir.exists() else 0
except Exception: # noqa: BLE001
return 0
def _update_beats(config: Config, doc: dict) -> list:
"""Edge-triggered 'it responded to me' beats with stable ids (client plays each
id once, ever — reload mid-beat replays nothing). Consume = eiDOS read a message
(a *.md → *.md.done rename). Multiple consumes between polls collapse to one beat.
First sight establishes a baseline silently (no beat for historical consumes)."""
seen = doc.setdefault("bond_seen", {})
beats = doc.setdefault("beats", [])
consumed = _count_consumed(config)
prev = seen.get("consumed")
changed = False
if prev is None:
seen["consumed"] = consumed # baseline only — no beat for the backlog
changed = True
elif consumed > prev:
seen["consumed"] = consumed
doc["beat_seq"] = int(doc.get("beat_seq", 0)) + 1
beats.append({"id": "b%d" % doc["beat_seq"], "type": "consume",
"ts": time.time()})
del beats[:-8]
changed = True
if changed:
with _CREATURE_LOCK:
try:
_save_creature(config, doc)
except OSError:
logger.exception("creature.json beat update failed")
return beats[-5:]
# Provisional bond tiers (recalibrated from real telemetry after ~2 weeks — the
# ledger carries by_kind so the thresholds can be tuned without code changes).
BOND_TIERS = [(400, 5), (220, 4), (120, 3), (60, 2), (25, 1)]
def _bond_tier(score: float) -> int:
for thresh, n in BOND_TIERS:
if score >= thresh:
return n
return 0
def _epoch_ts(value) -> float:
"""Timestamp → epoch seconds, tolerating both stored forms.
selfedit.apply stamps applied_ts as ISO-8601 Z (human-readable manifest);
older code and tests use epoch floats. float() alone raised ValueError on
the ISO form, which the blanket except swallowed — so S5 bond credit never
fired for real applies. Returns 0.0 on anything unparseable.
"""
if value is None:
return 0.0
try:
return float(value)
except (TypeError, ValueError):
pass
try:
return calendar.timegm(time.strptime(str(value), "%Y-%m-%dT%H:%M:%SZ"))
except ValueError:
return 0.0
def _accrue_bond(config: Config, doc: dict) -> dict:
"""Monotonic ledger of shared work. Accrues from the poll-detectable signals
(consume / listening minutes / self-edit-that-survived), each rarity-weighted
and daily-capped so it can't be farmed. Resets with the incarnation; never
decays. Persists only when a point is actually credited."""
now = time.time()
b = doc.setdefault("bond", {})
b.setdefault("score", 0.0)
b.setdefault("by_kind", {})
counts = b.setdefault("counts", {"exchanges": 0, "hold_min": 0, "approvals": 0})
day = b.setdefault("day", {})
today = time.strftime("%Y-%m-%d", time.gmtime(now))
if day.get("date") != today:
day = b["day"] = {"date": today}
changed = False
def credit(kind, pts, day_key=None, cap=None):
nonlocal changed
if day_key is not None and cap is not None:
pts = min(pts, max(0, cap - day.get(day_key, 0)))
if pts <= 0:
return 0
if day_key is not None:
day[day_key] = day.get(day_key, 0) + pts
b["score"] = round(b.get("score", 0) + pts, 2)
b["by_kind"][kind] = b["by_kind"].get(kind, 0) + pts
changed = True
return pts
# S1 consume +2 each, cap 10/day — baseline skips the historical backlog
consumed = _count_consumed(config)
if not b.get("baseline_set"):
b["credited_consumed"] = consumed
b["baseline_set"] = True
changed = True
elif consumed > b.get("credited_consumed", 0):
delta = consumed - b["credited_consumed"]
credit("chat", delta * 2, "chat", 10)
counts["exchanges"] += delta
b["credited_consumed"] = consumed
changed = True
# S3 listening +1/min, cap 4/day (presence with intent)
if _listening_hold_fresh(config) and now - b.get("last_listen_credit_ts", 0) >= 60:
if credit("hold_min", 1, "hold", 4):
counts["hold_min"] += 1
b["last_listen_credit_ts"] = now
changed = True
# S5 self-edit applied AND survived 30 min without rollback, +12 (real coaching)
try:
import selfedit
seen = set(b.get("credited_selfedits", []))
before = len(seen)
for m in selfedit.list_proposals(config, kind="self_edit"):
mid = m.get("id")
applied_ts = _epoch_ts(m.get("applied_ts"))
if (m.get("status") == "applied" and mid not in seen
and applied_ts and now - applied_ts >= 1800):
credit("selfedits", 12)
counts["approvals"] += 1
seen.add(mid)
if len(seen) != before:
b["credited_selfedits"] = sorted(seen)
changed = True
except Exception: # noqa: BLE001
pass
b["tier"] = _bond_tier(b.get("score", 0))
b["tiers_provisional"] = True
if changed:
with _CREATURE_LOCK:
try:
_save_creature(config, doc)
except OSError:
logger.exception("creature.json bond update failed")
return b
def build_status(config: Config) -> dict:
"""Assemble full status from workspace files."""
heartbeat = _read_json(config.workspace / "heartbeat.json")
persona = _read_json(config.workspace / "persona.json")
wal = _read_json(config.workspace / "wal.json")
activity = _read_json(config.workspace / "activity.json")
goal = _read_text(config.workspace / "goal.md")
plan = _read_text(config.workspace / "plan.md")[:2000]
observations = _tail_jsonl(config.workspace / "observations.jsonl", 20)
paused = (config.workspace / "paused").exists()
flavor = _read_json(config.workspace / "flavor.json")
narration = _compute_narration(heartbeat, persona, goal, flavor)
level = persona.get("level", 1)
mood = persona.get("mood", "curious")
traits = persona.get("traits", [])
xp = persona.get("xp", 0)
titles = persona.get("titles", [])
# Determine special state
special = None
cf = heartbeat.get("consecutive_failures", 0)
if cf >= 5:
special = "dead"
elif not goal.strip():
special = "sleeping"
creature = get_creature(level, mood, traits, special=special)
creature_spec = None
try:
creature_spec = build_creature_spec(config, persona, heartbeat, goal)
except Exception: # noqa: BLE001 — the spec must never kill /api/status
logger.exception("creature spec build failed (client falls back to legacy)")
return {
"heartbeat": heartbeat,
"creature_spec": creature_spec,
"persona": {
"name": persona.get("name", "eiDOS"),
"level": level,
"xp": xp,
"xp_next": ((level) ** 2) * 50, # XP needed for next level
"mood": mood,
"traits": traits,
"titles": titles,
"goals_completed": persona.get("goals_completed", 0),
"total_ticks": persona.get("total_ticks", 0),
"longest_streak": persona.get("longest_streak", 0),
},
"creature": creature,
"goal": goal[:500],
"plan": plan,
"observations": observations,
"narration": narration,
"flavor": flavor,
"paused": paused,
"disk_total_gb": _disk_total_gb(),
"activity": activity,
"wal": {
"tick": wal.get("tick_number", 0),
"consecutive_failures": wal.get("consecutive_failures", 0),
},
"commission": _commission_status(config),
"ts": time.time(),
}
def _commission_status(config: Config) -> dict:
"""The commission at a glance for /api/status (read-only — the eidos engine owns the store).
Empty dict when the organ is dark or there is no commission."""
if not getattr(config, "pillars_commission_enabled", False):
return {}
try:
import commission as _cm
c = _cm.Commission(config)
tasks = c.load()
live = [t for t in tasks if t.state in ("open", "done_claimed")]
return {
"brief": bool(_cm.load_brief(config)),
"confirmed_total": sum(1 for t in tasks if t.state == "confirmed"),
"awaiting": [{"id": t.id, "title": t.title, "evidence": t.evidence}
for t in live if t.state == "done_claimed"],
"open": sum(1 for t in live if t.state == "open"),
}
except Exception: # noqa: BLE001 — the strip must never kill /api/status
return {}
def build_ping(config: Config) -> dict:
"""Tiny health-check response (<500 bytes)."""
hb = _read_json(config.workspace / "heartbeat.json")
return {
"ts": hb.get("ts", 0),
"tick": hb.get("tick", 0),
"level": hb.get("level", 1),
"mood": hb.get("mood", "unknown"),
"ok": hb.get("consecutive_failures", 0) < 5,
"failures": hb.get("consecutive_failures", 0),
"disk_free_gb": hb.get("disk_free_gb"),
"ram_pct": hb.get("ram_pct"),
"uptime_s": hb.get("uptime_s", 0),
}
def build_chat(config: Config) -> dict:
"""Build chat history from interventions, replies, and pending questions."""
messages = []
# Operator → LLM: intervention files (pending + consumed)
idir = config.interventions_dir
if idir.exists():
for path in sorted(idir.iterdir()):
if path.name.startswith("."):
continue
try:
content = path.read_text().strip()
if not content:
continue
done = path.suffix == ".done"
mtime = path.stat().st_mtime
messages.append({
"direction": "outgoing",
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(mtime)),
"text": content[:2000],
"status": "delivered" if done else "pending",
})
except OSError:
continue
# LLM → Operator: chat replies
replies = _tail_jsonl(config.workspace / "chat_replies.jsonl", 50)
for r in replies:
messages.append({
"direction": "incoming",
"ts": r.get("ts", ""),
"text": r.get("text", ""),
"status": "delivered",
"spoken": bool(r.get("spoken", False)), # spoken aloud (speak tool) vs silent <reply>
})
messages.sort(key=lambda m: m.get("ts", ""))
return {"messages": messages}
def _tool_preview(name: str, args) -> str:
"""Build a human-readable preview of a tool call."""
if not isinstance(args, dict):
return name
if name == "bash":
return "$ " + (args.get("cmd", "") or "")[:100]
if name == "write_file":
return "writing " + (args.get("path", "") or "")
if name == "read_file":
return "reading " + (args.get("path", "") or "")
if name == "memorize":
return (args.get("fact", "") or "")[:100] or "memorizing"
if name == "remember":
return (args.get("note", "") or "")[:100] or "noting something"
if name == "recall":
return "recalling: " + (args.get("query", "") or "")[:80]
if name == "http_request":
return "fetching " + (args.get("url", "") or "")[:80]
if name == "bg_run":
return "starting: " + (args.get("cmd", "") or "")[:80]
if name == "bg_check":
return "checking on " + (args.get("name", "") or "")
if name == "update_plan":
return (args.get("note", "") or "")[:100] or "updating plan"
return name
def build_thoughts(config: Config, limit: int = 30) -> dict:
"""The agent's train of thought (thoughts.jsonl) for the Buddy Thoughts panel.
Falls back to parsing llm_log.jsonl when no thought stream exists yet.
"""
thought_entries = _tail_jsonl(config.workspace / "thoughts.jsonl", limit)
if thought_entries:
out = []
for e in reversed(thought_entries): # newest first
text = (e.get("text") or "").strip()
if not text:
continue
out.append({
"tick": e.get("tick", 0),
"ts": e.get("ts", ""),
"elapsed_s": 0,
"preview": text,
"raw_tail": text[-60:].replace("\n", " ").strip(),
"segments": [{"type": "thinking", "text": text}],
})
return {"thoughts": out}
import re
entries = _tail_jsonl(config.workspace / "llm_log.jsonl", limit)
thoughts = []
for entry in reversed(entries): # newest first
raw = entry.get("response_preview", "")
if not raw:
continue
tick = entry.get("tick", 0)
ts = entry.get("ts", "")
elapsed = entry.get("elapsed_s", 0)
# Split response into segments: thinking text vs tool calls
segments = []
pos = 0
for m in re.finditer(
r'<tool>(\w+)</tool>\s*\n?<args>(.*?)</args>',
raw, re.DOTALL
):
# Thinking text before this tool call
thinking = raw[pos:m.start()].strip()
if thinking:
segments.append({"type": "thinking", "text": thinking})
# The tool call itself
tool_name = m.group(1)
try:
tool_args = json.loads(m.group(2))
except (json.JSONDecodeError, ValueError):
tool_args = m.group(2)
segments.append({"type": "tool", "name": tool_name, "args": tool_args})
pos = m.end()
# Trailing thinking text after last tool call
trailing = raw[pos:].strip()
if trailing:
segments.append({"type": "thinking", "text": trailing})
# If no tool tags found, treat entire response as thinking
if not segments and raw.strip():
segments.append({"type": "thinking", "text": raw.strip()})
# Build a short preview — prefer thinking text, else describe the tool action
preview = ""
for seg in segments:
if seg["type"] == "thinking":
preview = seg["text"][:120]
break
if not preview:
for seg in segments:
if seg["type"] == "tool":
preview = _tool_preview(seg["name"], seg.get("args", {}))
break
# Raw tail for thought bubble display
raw_tail = raw[-60:].replace('\n', ' ').strip() if raw else ''
thoughts.append({
"tick": tick,
"ts": ts,
"elapsed_s": elapsed,
"preview": preview,
"raw_tail": raw_tail,