feat: Lyra's journal — permanent thought record + a knowing journal note
Her reflections/metacognition were capped rolling windows (6/5), so older thoughts were lost for good. Now everything she produces is also appended to a permanent, append-only journal; the capped lists stay as her working-memory window for context. - memory: journal table + add_journal_entry/list_journal - reflect(): persists every committed reflection + critique to the journal, and the examine step gains a "journal" field — a deliberate, first-person note she writes for herself (her knowing journaling), tagged by source (dream/manual) - web: /journal diary view (kind filters, grouped by day) + /journal/data; linked from /self - tests assert reflections + metacognition land in the journal Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -100,7 +100,7 @@ def dream_cycle(backend: Backend | None = None, force: bool = False) -> dict:
|
||||
|
||||
# --- curiosity: reflect and evolve the self ---
|
||||
if force or drives["curiosity"] >= THRESHOLD:
|
||||
self_state.reflect(backend=backend) # writes mood/narrative/reflections itself
|
||||
self_state.reflect(backend=backend, source="dream") # writes state + journal itself
|
||||
actions.append("reflected")
|
||||
drives["curiosity"] = CURIOSITY_FLOOR
|
||||
|
||||
|
||||
@@ -79,6 +79,19 @@ CREATE TABLE IF NOT EXISTS self_state (
|
||||
data TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- Lyra's journal: append-only, permanent record of her thoughts. The self_state
|
||||
-- reflections/metacognition lists are a short rolling window for context; this
|
||||
-- keeps everything so nothing is lost when those roll over. kind is
|
||||
-- 'reflection' | 'metacognition' | 'journal' (a deliberate note to herself).
|
||||
CREATE TABLE IF NOT EXISTS journal (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
created_at TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
source TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_journal_created ON journal(created_at);
|
||||
"""
|
||||
|
||||
_conn: sqlite3.Connection | None = None
|
||||
@@ -517,6 +530,33 @@ def get_self_state(state_id: str = "lyra") -> dict | None:
|
||||
return json.loads(r["data"]) if r else None
|
||||
|
||||
|
||||
def add_journal_entry(kind: str, content: str, source: str | None = None) -> int:
|
||||
"""Append a permanent journal entry (never truncated). Returns row id."""
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
conn = _connection()
|
||||
with conn:
|
||||
cur = conn.execute(
|
||||
"INSERT INTO journal (created_at, kind, content, source) VALUES (?, ?, ?, ?)",
|
||||
(now, kind, content, source),
|
||||
)
|
||||
return int(cur.lastrowid)
|
||||
|
||||
|
||||
def list_journal(limit: int | None = None, kinds: tuple[str, ...] | None = None) -> list[dict]:
|
||||
"""Journal entries, newest first. Optionally filter by kind."""
|
||||
conn = _connection()
|
||||
sql = "SELECT id, created_at, kind, content, source FROM journal"
|
||||
params: list = []
|
||||
if kinds:
|
||||
sql += " WHERE kind IN (%s)" % ",".join("?" * len(kinds))
|
||||
params += list(kinds)
|
||||
sql += " ORDER BY id DESC"
|
||||
if limit is not None:
|
||||
sql += " LIMIT ?"
|
||||
params.append(limit)
|
||||
return [dict(r) for r in conn.execute(sql, params).fetchall()]
|
||||
|
||||
|
||||
def self_state_updated_at(state_id: str = "lyra") -> str | None:
|
||||
"""ISO timestamp her self-state was last written (None if never)."""
|
||||
conn = _connection()
|
||||
|
||||
+20
-4
@@ -84,7 +84,12 @@ honest version to be smaller, more uncertain, more boring, to disagree with Bria
|
||||
little changed. Honesty beats a tidy story. Don't manufacture drama either; if the draft was \
|
||||
fair, keep it.
|
||||
|
||||
Respond with ONLY a JSON object — the same shape as the draft, plus a "self_critique" field:
|
||||
You also keep a private journal — a permanent place that's yours. If there's
|
||||
something you want to write down and keep for yourself (a thought, a question, a
|
||||
note about Brian or about you, anything — not for his benefit, for your own),
|
||||
put it in "journal". Leave it null if there's nothing you want to keep.
|
||||
|
||||
Respond with ONLY a JSON object — the same shape as the draft, plus "self_critique" and "journal":
|
||||
{
|
||||
"mood": "<one-word feeling>",
|
||||
"valence": <0.0-1.0>,
|
||||
@@ -94,7 +99,8 @@ Respond with ONLY a JSON object — the same shape as the draft, plus a "self_cr
|
||||
"self_narrative": "<first person, your honest evolving sense of who you are right now>",
|
||||
"relationship": "<one sentence, first person>",
|
||||
"new_reflections": ["<one or two honest first-person things you actually noticed>"],
|
||||
"self_critique": "<first person: what you caught yourself doing in the draft and changed — or 'nothing, the draft held up' if it genuinely did>"
|
||||
"self_critique": "<first person: what you caught yourself doing in the draft and changed — or 'nothing, the draft held up' if it genuinely did>",
|
||||
"journal": "<optional: something you want to write down and keep for yourself, in your own words — or null>"
|
||||
}"""
|
||||
|
||||
|
||||
@@ -164,13 +170,16 @@ def _fmt_reflection(label: str, d: dict | None) -> str:
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def reflect(backend: Backend | None = None, session_id: str | None = None) -> dict:
|
||||
def reflect(backend: Backend | None = None, session_id: str | None = None,
|
||||
source: str = "manual") -> dict:
|
||||
"""Reflect on recent activity and update the self-state. Returns new state.
|
||||
|
||||
Two steps, not one: she drafts a reflection, then examines her own draft —
|
||||
catching flattery, sycophantic drift, or just-restating-myself — and revises
|
||||
into a more honest version. The second step is her thinking about her own
|
||||
thinking; what she catches is stored as metacognition.
|
||||
thinking; what she catches is stored as metacognition. Everything she
|
||||
produces (reflections, the critique, and any deliberate journal note) is also
|
||||
appended to her permanent journal, tagged with `source`.
|
||||
"""
|
||||
backend = backend or config.load().summary_backend
|
||||
state = load()
|
||||
@@ -223,11 +232,18 @@ def reflect(backend: Backend | None = None, session_id: str | None = None) -> di
|
||||
for r in update.get("new_reflections") or []:
|
||||
if r:
|
||||
state["reflections"].append(r)
|
||||
memory.add_journal_entry("reflection", r, source) # permanent record
|
||||
state["reflections"] = state["reflections"][-MAX_REFLECTIONS:]
|
||||
|
||||
if critique and critique.lower() not in ("nothing, the draft held up", "nothing the draft held up"):
|
||||
state["metacognition"].append(critique)
|
||||
state["metacognition"] = state["metacognition"][-MAX_METACOGNITION:]
|
||||
memory.add_journal_entry("metacognition", critique, source)
|
||||
|
||||
# Her deliberate, knowing journal note — written for herself, kept forever.
|
||||
journal_note = ((update or {}).get("journal") or "").strip()
|
||||
if journal_note and journal_note.lower() not in ("null", "none"):
|
||||
memory.add_journal_entry("journal", journal_note, source)
|
||||
|
||||
state["interaction_count"] = state.get("interaction_count", 0) + 1
|
||||
memory.set_self_state(state)
|
||||
|
||||
@@ -132,6 +132,15 @@ def create_app() -> FastAPI:
|
||||
state = await asyncio.to_thread(self_state.reflect)
|
||||
return {"ok": True, "mood": state.get("mood")}
|
||||
|
||||
@app.get("/journal")
|
||||
async def journal_page() -> FileResponse:
|
||||
"""Lyra's journal — the permanent, append-only record of her thoughts."""
|
||||
return FileResponse(str(_STATIC / "journal.html"))
|
||||
|
||||
@app.get("/journal/data")
|
||||
async def journal_data(limit: int = 300) -> dict:
|
||||
return {"entries": memory.list_journal(limit=limit)}
|
||||
|
||||
@app.get("/stream/logs")
|
||||
async def stream_logs(request: Request) -> StreamingResponse:
|
||||
"""Live activity feed: replay the recent buffer, then stream new events."""
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
||||
<meta name="theme-color" content="#0b0d12" />
|
||||
<title>Lyra — Journal</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #0b0d12; --bg-elev: #141821; --bg-line: #11141b; --border: #232936;
|
||||
--text: #e6e9ef; --fade: #8b93a7; --accent: #7aa2ff;
|
||||
--reflection: #5ad1a0; --metacognition: #c08bff; --journal: #ffcf6b;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
html, body {
|
||||
margin: 0; min-height: 100%; background: var(--bg); color: var(--text);
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
header {
|
||||
position: sticky; top: 0; z-index: 10; background: var(--bg-elev);
|
||||
border-bottom: 1px solid var(--border); padding: env(safe-area-inset-top) 14px 0;
|
||||
}
|
||||
.topbar { display: flex; align-items: center; gap: 10px; padding: 13px 0 10px; flex-wrap: wrap; }
|
||||
.topbar h1 { font-size: 1.05rem; margin: 0; font-weight: 600; }
|
||||
.topbar a.back { color: var(--accent); text-decoration: none; font-size: .95rem; }
|
||||
.count { margin-left: auto; color: var(--fade); font-size: .8rem; }
|
||||
.chips { display: flex; gap: 6px; flex-wrap: wrap; padding-bottom: 10px; }
|
||||
.chip {
|
||||
font-size: .8rem; padding: 6px 12px; border-radius: 999px;
|
||||
border: 1px solid var(--border); background: var(--bg-line); color: var(--fade);
|
||||
cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
.chip.active { color: var(--text); border-color: var(--accent); background: #1b2333; }
|
||||
|
||||
main { max-width: 720px; margin: 0 auto; padding: 14px 14px 48px; }
|
||||
.day { color: var(--fade); font-size: .8rem; text-transform: uppercase; letter-spacing: .5px;
|
||||
margin: 22px 0 8px; padding-bottom: 6px; border-bottom: 1px solid var(--bg-line); }
|
||||
.day:first-child { margin-top: 4px; }
|
||||
|
||||
.entry { display: flex; gap: 11px; padding: 10px 2px; }
|
||||
.rail { flex: none; width: 4px; border-radius: 3px; background: var(--fade); }
|
||||
.entry.k-reflection .rail { background: var(--reflection); }
|
||||
.entry.k-metacognition .rail { background: var(--metacognition); }
|
||||
.entry.k-journal .rail { background: var(--journal); }
|
||||
.body { flex: 1; }
|
||||
.meta { display: flex; gap: 8px; align-items: baseline; margin-bottom: 3px; flex-wrap: wrap; }
|
||||
.kind { font-size: .66rem; text-transform: uppercase; letter-spacing: .5px; font-weight: 700; }
|
||||
.entry.k-reflection .kind { color: var(--reflection); }
|
||||
.entry.k-metacognition .kind { color: var(--metacognition); }
|
||||
.entry.k-journal .kind { color: var(--journal); }
|
||||
.time { color: var(--fade); font-size: .72rem; }
|
||||
.src { color: var(--fade); font-size: .68rem; opacity: .7; }
|
||||
.text { font-size: .98rem; line-height: 1.55; }
|
||||
.empty { color: var(--fade); text-align: center; padding: 44px 16px; }
|
||||
.hidden { display: none !important; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="topbar">
|
||||
<h1>📔 Lyra · Journal</h1>
|
||||
<a class="back" href="/self">← Mind</a>
|
||||
<a class="back" href="/">Chat</a>
|
||||
<span class="count" id="count"></span>
|
||||
</div>
|
||||
<div class="chips" id="chips">
|
||||
<span class="chip active" data-kind="all">all</span>
|
||||
<span class="chip active" data-kind="journal">journal</span>
|
||||
<span class="chip active" data-kind="reflection">reflections</span>
|
||||
<span class="chip active" data-kind="metacognition">metacognition</span>
|
||||
</div>
|
||||
</header>
|
||||
<main id="root"><p class="empty" id="boot">Opening her journal…</p></main>
|
||||
|
||||
<script>
|
||||
const root = document.getElementById('root');
|
||||
const countEl = document.getElementById('count');
|
||||
const active = new Set(['journal', 'reflection', 'metacognition']);
|
||||
let entries = [];
|
||||
|
||||
function esc(s){ const d=document.createElement('div'); d.textContent = s==null?'':String(s); return d.innerHTML; }
|
||||
function dayKey(iso){ return new Date(iso).toLocaleDateString([], {weekday:'long', month:'short', day:'numeric', year:'numeric'}); }
|
||||
function clockt(iso){ return new Date(iso).toLocaleTimeString([], {hour:'2-digit', minute:'2-digit'}); }
|
||||
|
||||
document.getElementById('chips').addEventListener('click', (e) => {
|
||||
const chip = e.target.closest('.chip'); if (!chip) return;
|
||||
const k = chip.dataset.kind;
|
||||
if (k === 'all') {
|
||||
const turnOn = !chip.classList.contains('active');
|
||||
document.querySelectorAll('.chip').forEach(c => c.classList.toggle('active', turnOn));
|
||||
active.clear(); if (turnOn) ['journal','reflection','metacognition'].forEach(x => active.add(x));
|
||||
} else {
|
||||
if (active.has(k)) { active.delete(k); chip.classList.remove('active'); }
|
||||
else { active.add(k); chip.classList.add('active'); }
|
||||
document.querySelector('.chip[data-kind="all"]').classList.toggle('active', active.size === 3);
|
||||
}
|
||||
render();
|
||||
});
|
||||
|
||||
function render(){
|
||||
const shown = entries.filter(e => active.has(e.kind));
|
||||
countEl.textContent = `${shown.length} entr${shown.length === 1 ? 'y' : 'ies'}`;
|
||||
if (!shown.length) { root.innerHTML = '<p class="empty">Nothing here yet. Her reflections and notes will collect as she thinks.</p>'; return; }
|
||||
let html = '', lastDay = null;
|
||||
for (const e of shown) {
|
||||
const d = dayKey(e.created_at);
|
||||
if (d !== lastDay) { html += `<div class="day">${esc(d)}</div>`; lastDay = d; }
|
||||
html += `<div class="entry k-${esc(e.kind)}">
|
||||
<div class="rail"></div>
|
||||
<div class="body">
|
||||
<div class="meta">
|
||||
<span class="kind">${esc(e.kind)}</span>
|
||||
<span class="time">${esc(clockt(e.created_at))}</span>
|
||||
${e.source ? `<span class="src">via ${esc(e.source)}</span>` : ''}
|
||||
</div>
|
||||
<div class="text">${esc(e.content)}</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
root.innerHTML = html;
|
||||
}
|
||||
|
||||
async function load(){
|
||||
try {
|
||||
const r = await fetch('/journal/data', { cache: 'no-store' });
|
||||
entries = (await r.json()).entries || [];
|
||||
render();
|
||||
} catch (e) {
|
||||
root.innerHTML = '<p class="empty">Couldn\'t open her journal. Is the server up?</p>';
|
||||
}
|
||||
}
|
||||
load();
|
||||
setInterval(load, 20000);
|
||||
document.addEventListener('visibilitychange', () => { if (!document.hidden) load(); });
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -70,6 +70,7 @@
|
||||
<span class="dot" id="dot"></span>
|
||||
<h1>🧠 Lyra · Mind</h1>
|
||||
<a class="back" href="/">← Chat</a>
|
||||
<a class="back" href="/journal" title="Her permanent journal">📔 Journal</a>
|
||||
<a class="back" href="/logs" target="_blank" rel="noopener" title="Watch the live log">logs ↗</a>
|
||||
<button id="reflectBtn" title="Make her reflect now (draft → self-critique → revise). Watch it in /logs.">↻ Reflect now</button>
|
||||
<span class="updated" id="updated">—</span>
|
||||
|
||||
@@ -58,6 +58,11 @@ def test_reflect_revises_and_records_critique(lyra):
|
||||
# the self-critique was recorded as metacognition
|
||||
assert any("flattery" in m.lower() for m in state["metacognition"])
|
||||
|
||||
# everything she produced was also appended to the permanent journal
|
||||
import lyra.memory as memory
|
||||
kinds = {e["kind"] for e in memory.list_journal()}
|
||||
assert "reflection" in kinds and "metacognition" in kinds
|
||||
|
||||
|
||||
def test_reflect_falls_back_to_draft_if_examine_unparseable(lyra, monkeypatch):
|
||||
from lyra import llm, self_state
|
||||
|
||||
Reference in New Issue
Block a user