Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
Chat quality: permissions once, Owner Model (ADR-026), self-made skills (ADR-027), useful status row (#211)
Salman Qureshi30e42e8cc480
diff --git a/.gitignore b/.gitignoreindex db7673e5..f537d5a5 100644--- a/.gitignore+++ b/.gitignore@@ -8,9 +8,9 @@ website/static/install.ps1*.log.DS_Store*.tsbuildinfo-config/-soul/-memory/+/config/+/soul/+/memory/# Claude Code worktrees / local state.claude/diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4f3bdab5..0a6dde93 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,5 +1,40 @@# Changelog+## Unreleased++### Owner Model: Mercury learns how to work with you (ADR-026)+- **Memory became beliefs with evidence.** Every claim about you now carries support and counter-evidence, a confidence that fades unless it is seen again, the context it applies in, and a status: candidate → held → confirmed by you; contested; retired; forgotten. What you say outright beats what Mercury inferred and never fades. Nothing is deleted behind your back.+- **It learns working traits, not personality types:** reply length, when to ask before acting (per project or domain), options vs a recommendation, tone, your expertise and risk appetite per domain, when you are usually around, how you give feedback. It never infers health, religion, politics, sexuality or personality types, never stores credentials, and never generalises about other people beyond what you said.+- **Two loops.** After each turn a strict extractor turns the turn into belief operations (or nothing; Mercury's own replies are never evidence about you). Every hour of activity, and at shutdown, a reflection pass reads your messages and behaviour across sessions, infers traits, merges duplicates, flags contradictions and rewrites the owner brief.+- **The model finally sees it:** "About you" and "How to work with you" sit in every prompt, plus the beliefs relevant to each message. Beliefs it acted on without objection gain support; a correction retires the old belief and holds the new one.+- **You hear about it.** Behaviour-changing learning ends the reply with one line: `☿ Noted: … Say "forget that" to undo.` Contradictions are asked about once. `/memory learned|beliefs|contested|confirm|correct|forget|settle`, `/forget <text>`, "forget that", the `forget_memory` / `confirm_belief` / `correct_belief` tools, a dashboard view with Confirm/Forget, and an editable `~/.mercury/soul/owner.md` whose notes are read back.+- **Hygiene:** the old extractor saved Mercury's own replies as your preferences (about half of a typical store). Those rows are quarantined, not deleted (dashboard → Quarantined, with Restore); real memories became beliefs. The prose fallback is gone, merging works, conflicts no longer delete at random, and soul files you edited are never overwritten by migration.+- **One source.** `/memory`, the dashboard, the `save_memory` / `search_memory` tools, the bots and cloud sync all read and write the belief store now; the old `memories` table is a read-only archive. Your name, email and handles stay in `identity.owner` in `mercury.yaml` and are never stored as beliefs.+- **Chat can actually use what it read.** Per-message recall shows held beliefs first and fills with unverified candidates (labelled as hunches), so what Mercury learned today and what the upgrade migrated is visible now, not after a second session. "What do you know about me?" injects the strongest beliefs directly. Stopwords and channel scaffolding no longer pollute the search, and the "About you" section pads with hedged candidates until enough is held.+- **Two extractor leaks closed.** A correction that opens like an acknowledgement ("No no, I said British spelling…") is no longer skipped as trivial, and an operation the model emits without an `excerpt` now cites your message instead of being thrown away (that alone dropped whole turns on Haiku).+- **Pick the model that learns.** `memory.learning.provider: anthropic` (or `LEARNING_PROVIDER`) runs extraction and reflection on a capable provider while chat keeps its own; unset falls back to the default.+- **Safe upgrade.** The first boot after upgrading backs `second-brain.db` up to `second-brain.pre-owner-model.db` before migrating, the migration runs once and never crashes the boot, every memory you could see before is still listed (or waits in Quarantined), and "☿ Noted" lines are limited to one per channel every few minutes.++### Self-made skills (ADR-027)+- **Mercury turns long work into skills.** After a multi-step task it would do again (a recurring report, a conversion pipeline, a deploy, a site or device it operates) has worked end-to-end, Mercury saves the procedure with the new `create_skill` tool — exact commands, paths, checks and pitfalls — and tells you in one line. Next time it is one `use_skill` away.+- **Kept apart from your skills.** Self-made skills live in `~/.mercury/skills/self-made/`, carry `origin: mercury`, are listed under their own "Self-made" label and count as unverified until used again. Drafts are rejected unless they are a real, verified, credential-free procedure; a self-made skill never shadows an installed one.+- **Skill categories and intents work again.** Discovery registered each skill's category, intents and tags and then rebuilt the index, wiping them; the prompt fell back to a flat list and intent routing matched on names only. Fixed by building the index first.+- **No junk shelf.** `/skills self-made` shows each one with use counts; unused ones go dormant after 30 days (disabled, never deleted) and the shelf is capped at 40. `/skills self-made enable|disable|remove <name>`. Bots and channel members cannot create skills.++### Chat+- **Regular chat works like an agent again.** Its instructions forbade helper scripts and batching ("call write_file for each one individually"), so a task like converting an EPUB to Markdown was done file by file by hand. Chat now reaches for the strongest existing tool (pandoc, ffmpeg, a Python library) or one script, does a quick web search when it isn't sure what the best tool is, and checks the output before saying it is done. It also gets a short working contract like Mercury Code's: do the work, plan multi-step tasks, report what was checked.++### Permissions+- **Mercury asks for your permission mode again, once, and remembers it.** Since 1.3.1 the default `mercury` launch attaches to the background runtime, which never showed the "Ask Me / Allow All" prompt, so every new user was silently in Ask Me. Setup now asks; existing users are asked once in the attached terminal; the choice is saved and applied to your terminal, attached terminal, Mercury Code and the dashboard.+- **`/permissions` works everywhere you are the owner** (attached terminal and dashboard too, not just the foreground terminal), including `/permissions allow-all` and `/permissions ask-me`.+- **"Always" on a command prompt now covers that command for the rest of the chat** (every `pandoc …`, `git commit …`), not just the exact string. Installs, wrappers like `sudo`/`sh -c`, and remote Mercury Cloud requests stay exact-match.+- Telegram and Signal can switch back from Allow All to Ask Me (the switch used to be ignored until restart).++### Bottom bar+- **One useful row, not a dump.** The row under the input now has two halves. Left: what is happening — a spinner, the phase (Thinking, the running tool, Writing) and the elapsed time while a turn runs, a notice when there is one, two short hints when idle. Right: where you are — permission mode, model, today's token use, Code mode, Optimus, an update, and the session last — fitted to the terminal width in priority order with compact forms (`claude-haiku-4-5` not `claude-haiku-4-5-20251001`, `⚡12%` not `⚡ 12% today`), so a long model or session name never crowds out the rest; what does not fit is dropped, lowest priority first, never cut mid-word. The attached terminal and the foreground TUI share one builder.+- **The bar updates live.** The attached terminal (what `mercury` opens) read the model once on connect and never again; model switches, Code mode, permission changes and Optimus activity never reached it. The runtime now pushes a status update on every model switch and checks every 2 seconds for anything else. The bar shows the thread, model, Code mode, Ask Me / Allow All, today's token use and Optimus activity; the foreground and Mercury Code bars show the permission mode too.+- The launch pad's budget row works again (it read field names the runtime never sent).+## 1.4.0 — Pluggy Mercury: plugins, WhatsApp calls, and Optimus uses your computerThe first stable release since 1.3.8. It also carries the 1.3.9 hotfix below, which only shipped on the dev channel.diff --git a/DECISIONS.md b/DECISIONS.mdindex 4103ab18..05509bb4 100644--- a/DECISIONS.md+++ b/DECISIONS.md@@ -241,3 +241,17 @@- **Ownership**: Following ADR-024, only the runtime owner runs plugins. Every other process (`mercury plugins …`, attached terminals) forwards changes over the loopback API. Web mutations are loopback-only.- **Authoring**: `init`, `validate` (a conformance kit that starts, inspects, stops and restarts the plugin) and `pack` ship in the binary, so authors need no npm package.- **Consequence**: The core binary stays the same size; optional features become catalog entries (`mercuryagent.sh/plugins/catalog.json`, checksum-pinned). In-process plugins still share the daemon's memory and event loop: the guards contain errors and timeouts, not a busy loop or a memory leak. Anything heavy or native should be out-of-process, and the docs say so.++## ADR-026: Mercury holds beliefs about its owner, with evidence, and says what it learned++- **Context**: The persona promised "I remember preferences and adapt over time", but memory was a dump: one-exchange extraction with no history and no existing beliefs, a fallback that saved prose (including Mercury's own replies) as the owner's preferences (48% of the store), merging that never fired (evidence count 1 on 865 of 867 rows), conflict handling that hard-deleted on a shared word, a profile the chat model never saw, no trait layer, and nothing ever told the owner. Full evaluation and design: `docs/owner-model.md`.+- **Decision**: A memory is a *belief*: a claim with support/against evidence, a confidence that decays unless re-supported, a context, a validity window (bitemporal, nothing is deleted) and a status (candidate → held → confirmed by the owner; contested; retired). Owner-stated beliefs beat inferred ones. Two loops: per-turn extraction that sees history and existing beliefs and emits belief operations (never prose; assistant text is never a source for owner beliefs), and a periodic reflection pass that infers *working traits* (brevity, autonomy, detail/expertise per domain, decision style, formality, risk per domain, rhythm, values, feedback style) as distributions with evidence, merges, finds contradictions and regenerates an owner brief. The chat model always sees "About {owner}" and "How to work with {owner}" (stable, cached) plus per-turn relevant beliefs. Behaviour-changing learning is announced in one line at the end of the reply and can be undone; contested beliefs are asked about once. The owner can confirm, correct and forget from chat and the dashboard. We do not infer personality types or protected attributes.+- **Consequence**: Mercury changes how it works as evidence accumulates and stops when corrected; the owner can always see why. Existing junk is quarantined, not deleted. Learning costs one small model call per non-trivial turn plus one reflection per hour of activity.+- **One source** (amendment, 2026-10-24): the belief store is the only memory store. `UserMemoryStore` is a facade over it, so every reader and writer (`/memory`, dashboard, `save_memory`/`search_memory`, bots, cloud sync) sees the same beliefs; the legacy `memories` table is a read-only archive (quarantine queue, persons graph). Who the owner is lives in `identity.owner` in the config and is never duplicated as a belief. Upgrades back the database up once before migrating, migrate idempotently and never fail the boot.++## ADR-027: Mercury writes skills for itself, kept apart from the curated ones++- **Context**: Long interactive tasks (a recurring report, a conversion pipeline, a deploy, operating a site or device) were redone from scratch every time, with the same discoveries and the same mistakes. The owner wants Mercury to turn such work into reusable skills — but not a shelf of half-written junk mixed in with the skills they installed on purpose.+- **Decision**: A `create_skill` tool (owner sessions only; stripped for members and bots) saves a procedure that just worked as a *self-made* skill under `~/.mercury/skills/self-made/<slug>/SKILL.md` with `category: self-made`, `origin: mercury`, `made`, `verified`, `uses`, `last_used`. The prompt tells Mercury when to make one (multi-step, repeatable, verified end-to-end) and when never to (one-offs, single tool calls, unverified, credentials). Drafts are validated strictly (slug, description length, ≥1 intent, a real stepwise procedure of ≥200 chars, a verification line, no secrets); a self-made skill never shadows a curated name; saving the same name updates it. They are listed under their own label as unverified until used again; `use_skill` counts uses. Hygiene on the heartbeat: never-used skills go dormant (`.disabled`, not deleted) after 30 days, and the shelf is capped at 40 live skills. `/skills self-made` lists them with use counts and enables, disables or removes them.+- **Consequence**: The second time a long task comes up it is one `use_skill` away, with the pitfalls already written down. Curated and self-made skills never blur: different folder, label, and frontmatter. The owner sees every save in one line and can prune from chat; junk ages out on its own without deleting anything.+diff --git a/docs/owner-model.md b/docs/owner-model.mdnew file mode 100644index 00000000..e31356ab--- /dev/null+++ b/docs/owner-model.md@@ -0,0 +1,221 @@+# The Owner Model: how Mercury grows with its owner++*Evaluation and design, 24 October 2026. Companion to ADR-026 in DECISIONS.md.*++## 1. What we promise and what we do++The persona file shipped with every install says "I remember preferences and adapt over time" and+"I evolve with every conversation". Today neither is true. Mercury stores facts; it does not form a+picture of the person, test it, or change how it works because of it.++### Measured on the owner's own machine (867 memories, 20 April to 24 October)++| What we found | Number | Why it matters |+|---|---|---|+| Rows labelled `preference` | 456 of 867 | Half the store is "preferences" |+| …of which are **Mercury's own replies** stored as the owner's preferences ("I appreciate the detailed instructions, but I need to clarify my actual capabilities: I'm Claude…", WhatsApp "For the chat:" blocks, "Open Slack on your computer") | 185 | The extractor cannot tell who said what |+| Rows produced by the non-JSON fallback (prose split into lines, saved as `preference`, confidence 0.75) | 420 | Junk is saved with the same confidence as truth |+| Rows whose evidence was ever seen twice | 2 | Nothing is ever confirmed |+| Rows superseded or dismissed by a contradiction | 0 | Nothing is ever retired |+| Near-duplicate rows | 57 | Nothing is merged |+| Average confidence (flat, never updated) | 0.83 | Confidence is a label, not a belief |+| Soul files changed since install day | 0 | Nothing writes what it learns back |+| Created in the last 7 days | 601 | Volume without understanding |++### Root causes (from the code audit; file:line on branch `fix/chat-quality-permissions`)++1. **Extraction sees one exchange, no history, no existing beliefs** (`src/core/agent.ts:4414-4551`). It can only append. It cannot say "already known", "stronger now", "the opposite of what I thought" or "the owner just corrected me".+2. **Assistant text is in the extraction input** and the fallback saves any line as a preference (`agent.ts:4499-4514`). That is where the 185 self-descriptions come from.+3. **Merging is effectively off** (`src/memory/user-memory.ts:297-307`): a `LIKE` over any shared word, first row only, overlap ≥ 0.74. Evidence counts stay at 1, so `promoteToDurable` never fires.+4. **Conflict handling deletes at random** (`user-memory.ts:506-543`, `second-brain-db.ts:375-393`): any same-type row sharing one word plus a substring "not" (matches "note", "another") is dismissed and hard-deleted on the next heartbeat. No similarity check, no audit trail, no question to the owner.+5. **The consolidated profile never reaches the chat model.** Only ≤ 5 keyword-matched lines go into each turn, ranked 0.3 confidence / 0.25 importance / 0.1 relevance (`user-memory.ts:557-575`). The profile summary is used only for voice calls and `/memory`.+6. **No trait layer.** "Reflections" are string templates gluing two summaries together (`user-memory.ts:1049-1107`). Sessions, tool traces, the work ledger, permission answers, model switches and timestamps are never mined.+7. **Silent.** Learning never tells the owner anything; there is no confirm, correct or forget from chat.+8. **Decay is mis-keyed**: using a memory doesn't refresh `last_seen_at`, so useful memories fade and junk survives.++## 2. What the best systems do (research, October 2026)++- **Hermes Agent** (Nous): a `USER.md` the agent edits itself (1,375-char cap, so it must consolidate), a `MEMORY.md` for lessons, periodic "nudges" that make the agent decide whether the session taught it anything, skills written after non-trivial workflows or **after a user correction**, staged writes the owner can approve or reject (`/memory pending|approve|reject`), and a one-line notice in the next reply when something was saved. It stores text, not beliefs: no confidence, no contradiction handling beyond "patch the file".+- **Honcho** (Plastic Labs, used by Hermes for user modelling): a representation built from premises and three kinds of conclusions — *deductive* (certain from what was said), *inductive* (patterns across many messages: "often mentions deadlines, rarely hobbies → time-constrained"), *abductive* (simplest explanation of behaviour). New messages refine conclusions; conflicts are reconciled rather than piled up. Queried in natural language.+- **Letta / MemGPT**: a second, idle-time agent rewrites the main agent's core memory ("sleep-time compute"). Memory edits are explicit tool calls.+- **Generative agents** (Stanford): periodic *reflection* that synthesises many memories into higher-level insights; retrieval scores relevance × recency × importance.+- **Bitemporal stores** (Zep, TOKI 2026): every fact carries a validity window; contradictions set `valid_to` instead of deleting.+- **Forgetting** (FadeMem, Ebbinghaus-style decay, 2026): confidence decays unless reinforced; reinforcement comes from re-observation *and* from acting on a belief without being corrected.+- **Preference learning through interaction** (MultiSessionCollab, 2026): after each session the agent reflects on the conversation against its existing notes and outputs *refined, merged or split* preferences with the condition under which each applies. Gains were steepest in the first five sessions. Failure mode: rewriting notes forgets useful detail.+- **Validity of personality inference**: inferring Big Five traits from text correlates only r ≈ 0.3 with the real thing, and a model's own confidence does not track its accuracy. We do not do this. We model *working traits* that change Mercury's behaviour, as distributions with evidence, and we let the owner see and correct them.++## 3. Design++### 3.1 Beliefs, not facts++A memory becomes a **belief**: a claim about the owner that Mercury holds with a degree of confidence, backed by evidence, valid for a time, in a context.++```+belief+ id, owner_key+ kind fact | preference | trait | situation | relationship | goal | project+ dimension (traits only) e.g. brevity, autonomy, detail, formality, decision-style,+ risk:<domain>, expertise:<domain>, rhythm, values+ statement "Prefers one command over many manual steps"+ level (traits only) the inferred level on that dimension, e.g. autonomy = high+ context where it applies: channel, project/cwd, domain, time-of-day, or "*"+ support count of supporting evidence } confidence = Beta(support+1, against+1),+ against count of contradicting evidence } decayed by time since last support+ timescale stable | situational (hours) | episodic+ status candidate | held | confirmed (owner said so) | contested | retired+ valid_from, valid_to, superseded_by (bitemporal: nothing is deleted)+ last_supported_at, last_used_at+ owner_stated 0/1 (what the owner said beats what we inferred)++evidence+ id, belief_id, kind (said | corrected | approved | denied | reacted | observed | acted-on-ok)+ session_id, turn_ref, excerpt (≤ 200 chars), weight, at+```++Rules:+- A belief starts as a **candidate** (support = 1). It becomes **held** at support ≥ 2 from different sessions, or immediately when **owner-stated**.+- **Acting on a held belief without correction** adds weak support (weight 0.25). A correction adds strong against-evidence (weight 2) and, if it names the right answer, creates the replacement belief as owner-stated.+- Two beliefs that contradict in the same context are **contested**. Contested beliefs are not used; Mercury asks the owner once, at a natural moment, and the answer settles it.+- Confidence decays (half-life 60 days for stable traits, 7 days for situations) unless re-supported. Below 0.35 a belief is **retired** (kept, hidden, recoverable).+- The owner can **confirm**, **correct**, **forget** any belief from chat or the dashboard. Forgetting is permanent and leaves no trace in prompts.++### 3.2 What we learn: the working-trait dimensions++Chosen because each one changes what Mercury should do next, and each has observable evidence.++| Dimension | Levels | Evidence | Changes |+|---|---|---|---|+| **brevity** | terse · balanced · thorough | reply length the owner asks for ("shorter", "more detail"), message length, "tl;dr" requests | reply length, summaries first or not |+| **autonomy** | ask-first · ask-when-risky · just-do-it | permission answers (Allow All, "always"), "just do it", "why did you ask" | how often Mercury asks, what it does unprompted |+| **detail / expertise** per domain | novice · competent · expert | vocabulary, corrections of Mercury's explanations, which questions they skip | how much to explain, which assumptions to make |+| **decision style** | options · recommendation · decide-for-me | "give me options" vs "what should I do" vs "pick one" | present choices or a verdict |+| **formality** | casual · neutral · formal | how they write, emoji, greetings | tone |+| **risk** per domain (money, deletion, sending, publishing) | cautious · normal · bold | approvals and denials by action type, "don't ever publish without asking" | what needs confirmation |+| **rhythm** | active hours, quiet hours, typical session length | timestamps across channels | when to send proactive messages; what "later" means |+| **values** | stated principles ("open source", "own my files", "ship fast") | explicit statements, repeated choices | how options are framed |+| **feedback style** | how they correct (blunt / explain / silent redo) | correction patterns | how Mercury confirms it understood a correction |+| **situation** (situational, hours) | under time pressure, frustrated, exploring | short messages, repeated requests, "ugh", late night | tone and pace *for now*; never stored as a trait |++What we deliberately do **not** infer: personality types, health, religion, politics, sexuality,+or anything about a third person beyond what the owner said about them. If the owner states one of+these, it is stored as a stated fact, used only when relevant, and never generalised.++### 3.3 Where the evidence comes from++Every turn already leaves these behind; today none of them are read:++- the owner's words (only the owner's; assistant text is never a source for owner beliefs)+- corrections: "no, I meant", "that's not what I asked", redoing the same request, rejecting output+- approvals and denials by action type; "always"; permission-mode changes+- reactions after Mercury acts on a belief (no complaint = weak support)+- model switches, mode switches, `/permissions`, `/memory forget`+- timestamps, channel, session length, project/cwd+- the work ledger: what was asked, what finished, what failed++### 3.4 Two learning loops++**Per turn (fast, cheap):** the existing extraction, rebuilt. Input: the owner's message, the+reply, the last few turns, the *relevant existing beliefs*, and the turn's signals. Output: a list of+belief operations — `support`, `contradict`, `create`, `owner_stated`, `correction` — or `[]`. Never+prose. Assistant text is labelled as such and may only produce "what Mercury did" episodes.++**Reflection (slow, periodic — the "sleep-time" pass):** on the heartbeat (60 min) when ≥ 1 session+closed, and at shutdown. Reads the sessions since the last pass plus the signals above, and the+current trait distributions. Produces: trait-level updates with evidence, merges of near-duplicate+beliefs, contested pairs, and a regenerated **owner brief**. This is where patterns across sessions+appear ("you have asked for a shorter version 6 times across 4 sessions").++### 3.5 How the model sees the owner++Two prompt pieces, both regenerated by reflection (not per turn, so the prompt cache holds):++1. **About {owner}** (≤ 400 tokens): stated facts and held beliefs, with soft hedges on candidates+ ("seems to", "so far").+2. **How to work with {owner}** (≤ 250 tokens): the traits translated into instructions —+ "Keep replies short; lead with the result. Don't ask before reversible actions in their own+ repos; always ask before publishing. Give a recommendation, not a menu. They're an expert in+ TypeScript and DevOps; a beginner in video production — explain those."++Plus, per turn, up to 5 beliefs relevant to the message: held/confirmed first, then candidates labelled+"unverified, seen once" to fill the slots (otherwise nothing learned in the current session, and nothing+migrated, reaches the model until a second session supports it). Only the sure ones earn+"acted-on-ok" credit. A message that asks what Mercury knows about the owner gets the twelve strongest+beliefs instead of an FTS match. Stopwords and channel scaffolding are stripped from the query.++An `owner.md` in `~/.mercury/soul/` holds the same text so the owner can read and edit it, like+Hermes' `USER.md` but generated from beliefs and annotated with confidence.++### 3.6 Telling the owner++- When a turn produces a belief that **changes Mercury's behaviour** (a trait moves, a correction is+ learned, a contested belief appears), the reply ends with one short line:+ `☿ Noted: you'd rather I just run things in this repo without asking. Say "forget that" to undo.`+ At most one such line per reply; never for trivia.+- Contested beliefs are asked about once, at the end of a reply, never mid-task.+- `/memory` grows: **what I learned recently**, **what I believe about you** (traits with+ confidence), `confirm`, `correct`, `forget`, `pause`. The dashboard gets the same views and a+ review queue.+- `/memory` also shows the honest numbers: how many beliefs are held, candidate, contested, retired.++### 3.7 Hygiene and migration++- Existing rows: assistant-voice and fallback junk are **quarantined** (`dismissed = 1`,+ `provenance = migration:junk`), not deleted; the dashboard shows them under "Quarantined" for+ review. Real memories become `held` beliefs with support = evidence_count.+- The non-JSON fallback is removed. Extraction returns `[]` when there is nothing.+- Merge uses FTS candidates plus an LLM judge ("same claim?"), not a LIKE on one word.+- Decay keys on `last_supported_at`; use refreshes it weakly.+- The soul migration that overwrites edited files is removed.++### 3.8 Which model learns++`memory.learning.provider` (or `LEARNING_PROVIDER`) names the provider for extraction and reflection;+unset or unusable → the chat default. Both loops are strict-JSON tasks: on the free tier the extractor+returned `[]` or invalid JSON most of the time, so an owner on a free default learned almost nothing.+A Haiku-class model is enough and keeps the chat model choice untouched.++Measured on Haiku 4.5 (October 2026), four turn types × 2 runs: before the fixes below, 0 of 6 model+calls produced an operation; after, every substantive turn did (brevity=terse, autonomy=just-do-it+scoped to the project, the goal behind a crash report), and acknowledgements cost no call.+Two leaks: the trivial-message gate matched any message *starting* with "no"/"ok" (corrections!), and+a missing `excerpt` field — which the model omits now and then — discarded the whole operation. The+gate now applies only to short messages; a missing excerpt falls back to the owner's message.++### 3.9 One source, and what an existing owner sees++- `UserMemoryStore` is a facade over `BeliefStore`. Every reader and writer the old API had+ (`getRecent`, `search`, `getByType`, `retrieveRelevant`, `remember`, `updateMemory`,+ `softDeleteMemory`, `getShareable*`, `clear`, `getSummary`) maps onto beliefs, so `/memory`, the+ dashboard, the `save_memory` / `search_memory` tools, the bots and cloud sync all see one store.+ `consolidate()` is a no-op kept for callers; `prune()` is the decay pass. The `memories` table is a+ read-only archive: the quarantine queue and the persons graph still read it, nothing writes it.+- **Identity is config.** `identity.owner` in `mercury.yaml` is the one place the owner's name is+ set; the extractor and reflection prompts and both validators refuse beliefs that only state who+ the owner is or how to reach them (`looksLikeIdentity`).+- **Upgrade path.** On the first boot with a pre-Owner-Model database that has rows, the store+ copies it to `second-brain.pre-owner-model.db` before migrating (once; never overwritten). The+ migration is idempotent (`belief_meta legacy_migrated`), wrapped in try/catch so a failure boots+ Mercury with an empty belief store instead of crashing, and marks `reflection_force` so the+ migrated candidates are judged against real sessions two minutes after boot. "☿ Noted" lines are+ rate-limited to one per channel every three minutes so the backlog never floods a chat.+ Restoring a quarantined row creates a confirmed belief the owner vouched for (new id).+- Not yet moved: the persons graph (`memory_persons` keys into `memories`); it keeps reading the+ archive until relationship beliefs feed it.++### 3.10 Validation++- Unit tests on the belief store: support/against maths, decay, contest, supersession, forget.+- Scripted-provider eval scenarios (`src/eval`): "owner corrects brevity twice → next reply is+ short"; "owner says always ask before publishing → publishing asks even in Allow All";+ "contradiction in a different context stays separate"; "assistant text never becomes a belief";+ "junk is quarantined on migration".+- A dry run against the owner's real sessions on this machine, reporting what the engine would+ have learned, before it is switched on.++## 4. Phasing++1. **Hygiene** (fix extraction, merge, conflict, decay, migration, profile into prompt) — makes+ today's memory honest.+2. **Engine** (beliefs + evidence tables, signals, reflection pass, trait dimensions, owner brief+ and "how to work with" sections, learned-moment lines, `/memory` and dashboard).+3. **Validation loop** (act-and-observe support, contested questions, `owner.md`, eval scenarios).diff --git a/src/bots/registry-factory.ts b/src/bots/registry-factory.tsindex 6043abfe..74f47b34 100644--- a/src/bots/registry-factory.ts+++ b/src/bots/registry-factory.ts@@ -24,6 +24,7 @@ const ALWAYS_STRIPPED = new Set(['list_agents','stop_agent','install_skill', // no skill installs from an unattended bot+ 'create_skill', // self-made skills come from the owner's own sessions (ADR-027)'schedule_task', // bot schedules are managed via bot.yaml, not tools'list_scheduled_tasks','cancel_scheduled_task',diff --git a/src/capabilities/permission-mode-store.ts b/src/capabilities/permission-mode-store.tsnew file mode 100644index 00000000..8a735333--- /dev/null+++ b/src/capabilities/permission-mode-store.ts@@ -0,0 +1,44 @@+/**+ * The owner's permission mode, chosen once and remembered.+ *+ * Since 1.3.1 `mercury` attaches to the background runtime, which never ran+ * the "Allow All / Ask Me" prompt: every new user landed in Ask Me without+ * being asked, and a choice made in a foreground terminal was lost on+ * restart. The choice now lives in ~/.mercury/permission-mode.json and is+ * applied by every surface the owner uses (terminal, attached terminal,+ * dashboard). `null` means it was never chosen, so the next surface asks.+ *+ * It applies to the owner's own surfaces only. Telegram, WhatsApp and other+ * chats keep their own per-chat handling, so a saved Allow All never+ * reaches a member or a group.+ */+import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { getMercuryHome } from '../utils/config.js';++export type SavedPermissionMode = 'ask-me' | 'allow-all';++function file(): string {+ return join(getMercuryHome(), 'permission-mode.json');+}++export function loadPermissionMode(): SavedPermissionMode | null {+ try {+ if (!existsSync(file())) return null;+ const raw = JSON.parse(readFileSync(file(), 'utf8')) as { mode?: unknown };+ return raw.mode === 'allow-all' || raw.mode === 'ask-me' ? raw.mode : null;+ } catch {+ return null;+ }+}++export function savePermissionMode(mode: SavedPermissionMode): void {+ writeFileSync(file(), JSON.stringify({ mode, chosenAt: new Date().toISOString() }, null, 2) + '\n', { mode: 0o600 });+}++/** One line for the bar and for confirmations. */+export function describePermissionMode(mode: SavedPermissionMode): string {+ return mode === 'allow-all'+ ? 'Allow All: Mercury acts without asking (blocked commands stay blocked).'+ : 'Ask Me: Mercury asks before risky actions such as writing files or running commands.';+}diff --git a/src/capabilities/permissions.test.ts b/src/capabilities/permissions.test.tsindex e22b0a91..604ac510 100644--- a/src/capabilities/permissions.test.ts+++ b/src/capabilities/permissions.test.ts@@ -2,7 +2,7 @@ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';import { mkdtempSync, mkdirSync, writeFileSync, rmSync, symlinkSync, linkSync, realpathSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { PermissionManager, splitShellSegments } from './permissions.js';+import { PermissionManager, splitShellSegments, commandFamily } from './permissions.js';describe('splitShellSegments', () => {it('passes simple commands through as a single segment', () => {@@ -583,6 +583,36 @@ describe('argv lane (ADR-016)', () => {expect(ask).toHaveBeenCalledWith('Run command: git log | head');});+ it('"Always" covers the same command family for the rest of the chat', async () => {+ const { permissions, ask } = makePermissions();+ permissions.setCurrentContext('cli', 'cli');+ ask.mockResolvedValueOnce('always');+ await expect(permissions.checkShellCommand('pandoc book.epub -o book.md')).resolves.toMatchObject({ allowed: true });+ await expect(permissions.checkShellCommand('pandoc other.epub -t gfm -o other.md')).resolves.toMatchObject({ allowed: true });+ expect(ask).toHaveBeenCalledTimes(1);+ // A different program still asks.+ ask.mockResolvedValueOnce('no');+ await expect(permissions.checkShellCommand('python3 convert.py')).resolves.toMatchObject({ allowed: false });+ expect(ask).toHaveBeenCalledTimes(2);+ });++ it('remote (cloud) requests keep "Always" to the exact command', async () => {+ const { permissions, ask } = makePermissions();+ ask.mockResolvedValueOnce('always').mockResolvedValue('no');+ await expect(permissions.checkShellCommand('pandoc a.epub -o a.md')).resolves.toMatchObject({ allowed: true });+ await expect(permissions.checkShellCommand('pandoc b.epub -o b.md')).resolves.toMatchObject({ allowed: false });+ });++ it('never generalises wrappers that can run anything', () => {+ expect(commandFamily('pandoc a.epub -o a.md')).toBe('pandoc');+ expect(commandFamily('git commit -m "x"')).toBe('git commit');+ expect(commandFamily('LANG=C npm test -- --watch')).toBe('npm test');+ expect(commandFamily('npm install left-pad')).toBeNull();+ for (const cmd of ['sudo rm -rf /tmp/x', 'sh -c "anything"', 'env FOO=1 node x.js', 'xargs rm', 'bash script.sh']) {+ expect(commandFamily(cmd)).toBeNull();+ }+ });+it('sees through quoting: a quoted path outside the cwd and readable scopes prompts', async () => {const { permissions, ask } = makePermissions();permissions.getManifest().capabilities.shell.cwdOnly = false;diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.tsindex c4307342..41d58bf2 100644--- a/src/capabilities/permissions.ts+++ b/src/capabilities/permissions.ts@@ -341,6 +341,7 @@ export class PermissionManager {private manifest: PermissionsManifest;private readonly cwd: string;private askHandler?: (prompt: string) => Promise<string>;+ private turnAnswers: Array<{ action: string; answer: 'yes' | 'no' | 'always' }> = [];/*** The main agent's (root) context: replaced, never mutated, by the* legacy setters below. Sub-agents never read it while they run — they@@ -352,6 +353,18 @@ export class PermissionManager {private rootCache: PermissionContext | null = null;private binaryResolver: BinaryResolver = pinnedBinary;private approvedCommandsByContext = new Map<string, Set<string>>();+ /** "Always" on a shell prompt: command families (`pandoc`, `git commit`) allowed for the rest of that chat. */+ private approvedFamiliesByContext = new Map<string, Set<string>>();+ /**+ * Which contexts may widen "Always" to a command family. Default: only the+ * local terminal. The runtime also allows the owner's attached terminal and+ * dashboard (web, but not remote Mercury Cloud requests, which stay exact).+ */+ private familyApprovalAllowed: (ctx: { channelType: string; channelId: string }) => boolean = (ctx) => ctx.channelType === 'cli';++ setFamilyApprovalPolicy(fn: (ctx: { channelType: string; channelId: string }) => boolean): void {+ this.familyApprovalAllowed = fn;+ }/** "Always allow" choices for plugin tools, per channel (see approvePluginTool). */private approvedPluginToolsByContext = new Map<string, Set<string>>();private approvedWritesByContext = new Map<string, Set<string>>();@@ -505,7 +518,24 @@ export class PermissionManager {}onAsk(handler: (prompt: string) => Promise<string>): void {- this.askHandler = handler;+ // Every answer the owner gives is evidence for the Owner Model (autonomy,+ // risk per action type); the learning loop drains it once per turn.+ this.askHandler = async (prompt: string) => {+ const answer = await handler(prompt);+ if (answer === 'yes' || answer === 'no' || answer === 'always') {+ const action = prompt.split('\n')[0].replace(/:.*$/, '').trim().slice(0, 40) || 'action';+ this.turnAnswers.push({ action, answer });+ if (this.turnAnswers.length > 20) this.turnAnswers.shift();+ }+ return answer;+ };+ }++ /** Permission answers given since the last drain (the learning loop reads them each turn). */+ drainAnswers(): Array<{ action: string; answer: 'yes' | 'no' | 'always' }> {+ const out = this.turnAnswers;+ this.turnAnswers = [];+ return out;}async requestApproval(prompt: string): Promise<boolean> {@@ -931,6 +961,22 @@ export class PermissionManager {}}+ // "Always" on an earlier prompt in this chat covers the same command+ // family (every segment), after the hard blocks and the cwd gate above.+ const families = this.familyApprovalAllowed(ctx) ? this.approvedFamiliesByContext.get(ctx.channelId) : undefined;+ if (families && families.size > 0) {+ const needsApprovalList = shell.needsApproval ?? [];+ const covered = segments.every((segment) => {+ const family = commandFamily(segment);+ return family !== null && families.has(family)+ && !needsApprovalList.some((pattern) => this.matchPattern(segment, pattern));+ });+ if (covered) {+ logger.info({ cmd: trimmed }, 'Shell command auto-approved (command family approved with Always in this chat)');+ return { allowed: true, needsApproval: false, lane: 'shell' };+ }+ }+// Argv lane: the only path to a prompt-free approval in Ask Me mode.const argvLane = this.classifyArgvLane(trimmed, cwd);if (argvLane.ok) {@@ -961,6 +1007,15 @@ export class PermissionManager {const approved = this.approvedCommandsByContext.get(ctx.channelId) ?? new Set<string>();approved.add(trimmed);this.approvedCommandsByContext.set(ctx.channelId, approved);+ // Also remember the command family (`pandoc`, `git commit`) so the+ // next pandoc call in this chat doesn't ask again. Wrappers that can+ // run anything (sudo, sh -c, env, xargs…) are never generalised.+ const familyList = segments.map(commandFamily);+ if (this.familyApprovalAllowed(ctx) && familyList.every((f): f is string => f !== null)) {+ const set = this.approvedFamiliesByContext.get(ctx.channelId) ?? new Set<string>();+ for (const f of familyList) set.add(f);+ this.approvedFamiliesByContext.set(ctx.channelId, set);+ }return { allowed: true, needsApproval: false, lane: 'shell' };}return { allowed: false, reason: `User denied: ${trimmed}`, needsApproval: false };@@ -1192,3 +1247,30 @@ export class PermissionManager {};}}++const NEVER_GENERALISE = new Set(['sudo', 'su', 'doas', 'eval', 'exec', 'sh', 'bash', 'zsh', 'fish', 'dash', 'env', 'xargs', 'nohup', 'time', 'watch', 'command', 'builtin', 'powershell', 'pwsh', 'cmd']);+const EXACT_ONLY_SUBCOMMANDS = new Set(['install', 'i', 'add', 'exec', 'dlx', 'x', 'global', 'upgrade', 'update', 'run-script']);+const SUBCOMMAND_TOOLS = new Set(['git', 'npm', 'pnpm', 'yarn', 'bun', 'npx', 'pnpx', 'docker', 'cargo', 'go', 'kubectl', 'brew', 'pip', 'pip3', 'uv', 'gh', 'deno', 'poetry', 'apt', 'apt-get', 'systemctl', 'launchctl']);++/**+ * The family an "Always" answer covers: the program (`pandoc`), or the+ * program and its subcommand for multi-command tools (`git commit`, `npm+ * install`). Null for wrappers that can run anything, and for segments with+ * no plain program name.+ */+export function commandFamily(segment: string): string | null {+ const tokens = segment.trim().split(/\s+/).filter(Boolean);+ let i = 0;+ while (i < tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[i])) i++;+ const first = tokens[i];+ if (!first || /[^A-Za-z0-9._/+-]/.test(first)) return null;+ const base = first.split('/').pop() ?? '';+ if (!base || NEVER_GENERALISE.has(base)) return null;+ if (SUBCOMMAND_TOOLS.has(base)) {+ const sub = tokens[i + 1];+ // Installing or executing arbitrary packages stays exact-match only.+ if (sub && EXACT_ONLY_SUBCOMMANDS.has(sub)) return null;+ return sub && /^[A-Za-z][A-Za-z0-9:_-]*$/.test(sub) ? `${base} ${sub}` : base;+ }+ return base;+}diff --git a/src/capabilities/registry.test.ts b/src/capabilities/registry.test.tsindex 51b4a55d..0b90a783 100644--- a/src/capabilities/registry.test.ts+++ b/src/capabilities/registry.test.ts@@ -22,7 +22,9 @@ describe('CapabilityRegistry.getTools sender role gating', () => {registry.permissions.setCurrentSenderRole('member');const member = registry.getTools();expect(member.install_skill).toBeUndefined();- expect(Object.keys(member).length).toBe(Object.keys(admin).length - 1);+ // create_skill (ADR-027) is owner-only for the same reason.+ expect(member.create_skill).toBeUndefined();+ expect(Object.keys(member).length).toBe(Object.keys(admin).length - 2);expect(member.use_skill).toBeDefined();});});diff --git a/src/capabilities/registry.ts b/src/capabilities/registry.tsindex de5e0540..2ba69814 100644--- a/src/capabilities/registry.ts+++ b/src/capabilities/registry.ts@@ -14,6 +14,7 @@ import { createRunCommandTool } from './shell/run-command.js';import { createCdTool } from './shell/cd.js';import { createApproveCommandTool } from './shell/approve-command.js';import { createInstallSkillTool } from './skills/install-skill.js';+import { createCreateSkillTool } from './skills/create-skill.js';import { createListSkillsTool } from './skills/list-skills.js';import { createUseSkillTool } from './skills/use-skill.js';import { createScheduleTaskTool } from './scheduler/schedule-task.js';@@ -22,6 +23,9 @@ import { createCancelTaskTool } from './scheduler/cancel-task.js';import { createBudgetStatusTool } from './system/budget-status.js';import { createSaveMemoryTool } from './system/save-memory.js';import { createSearchMemoryTool } from './system/search-memory.js';+import { createForgetMemoryTool } from './system/forget-memory.js';+import { createConfirmBeliefTool } from './system/confirm-belief.js';+import { createCorrectBeliefTool } from './system/correct-belief.js';import { createGitStatusTool } from './git/git-status.js';import { createGitDiffTool } from './git/git-diff.js';import { createGitLogTool } from './git/git-log.js';@@ -242,6 +246,7 @@ export class CapabilityRegistry {if (this.skillLoader) {this.tools.install_skill = createInstallSkillTool(this.skillLoader);+ this.tools.create_skill = createCreateSkillTool(this.skillLoader);this.tools.list_skills = createListSkillsTool(this.skillLoader);this.tools.use_skill = createUseSkillTool(this.skillLoader, this.permissions);logger.info('Skill tools registered');@@ -262,7 +267,11 @@ export class CapabilityRegistry {if (this.userMemory) {this.tools.save_memory = createSaveMemoryTool(this.userMemory);this.tools.search_memory = createSearchMemoryTool(this.userMemory);- logger.info('Second Brain tools registered (save_memory, search_memory)');+ // Owner Model (ADR-026): the owner's verbs over what Mercury believes about them.+ this.tools.forget_memory = createForgetMemoryTool(this.userMemory);+ this.tools.confirm_belief = createConfirmBeliefTool(this.userMemory);+ this.tools.correct_belief = createCorrectBeliefTool(this.userMemory);+ logger.info('Second Brain tools registered (save_memory, search_memory, forget_memory, confirm_belief, correct_belief)');}if (manifest.capabilities.git?.enabled) {@@ -317,7 +326,7 @@ export class CapabilityRegistry {if (this.permissions.getCurrentSenderRole() === 'member') {const filtered: Record<string, Tool> = {};for (const [name, tool] of Object.entries(this.tools)) {- if (name !== 'install_skill') filtered[name] = tool;+ if (name !== 'install_skill' && name !== 'create_skill') filtered[name] = tool;}return filtered;}diff --git a/src/capabilities/skills/create-skill.ts b/src/capabilities/skills/create-skill.tsnew file mode 100644index 00000000..046688b2--- /dev/null+++ b/src/capabilities/skills/create-skill.ts@@ -0,0 +1,46 @@+import { tool, zodSchema } from 'ai';+import { z } from 'zod';+import type { SkillLoader } from '../../skills/loader.js';+import { SELF_MADE_CATEGORY, composeSkillMd, readSelfMadeMeta, rejectDraft, slugify } from '../../skills/self-made.js';+import { existsSync, readFileSync } from 'node:fs';+import { join } from 'node:path';++/**+ * `create_skill` (ADR-027): Mercury saves a procedure that just worked as a+ * self-made skill. Owner-only, like install_skill. Strict on purpose: the+ * owner does not want a shelf of half-written junk.+ */+export function createCreateSkillTool(skillLoader: SkillLoader) {+ return tool({+ description:+ 'Save a procedure you just carried out and verified as a reusable self-made skill, so the next time it is one use_skill away. '+ + 'Only for multi-step work you would do again for other inputs (a recurring report, a conversion pipeline, a deploy, a site or device you operate). '+ + 'Never for one-offs, for anything a single tool call does, for anything you did not verify end-to-end, or with credentials inside. '+ + 'To improve an existing self-made skill, call this again with the same name. Self-made skills are listed separately and stay "unverified" until used again.',+ inputSchema: zodSchema(z.object({+ name: z.string().describe('Short slug, e.g. "weekly-sales-report" or "deploy-ceo-site"'),+ description: z.string().describe('One or two sentences: what it does and for which inputs (20–220 chars)'),+ intents: z.array(z.string()).min(1).max(12).describe('How the owner would ask for this, in their words ("make the weekly sales report")'),+ instructions: z.string().describe('The procedure as numbered steps: exact commands, tools, paths, inputs to ask for, checks, and the pitfalls you hit. Not a narrative.'),+ verified: z.string().describe('One line: what you checked when it worked (e.g. "PDF opened, 12 pages, totals match the sheet")'),+ allowedTools: z.array(z.string()).optional().describe('Tools the skill needs (least privilege); omit to inherit'),+ })),+ execute: async ({ name, description, intents, instructions, verified, allowedTools }) => {+ const draft = { name, description, intents, instructions, verified, allowedTools };+ const why = rejectDraft(draft);+ if (why) return `Not saved — ${why}.`;+ const slug = slugify(name)!;++ // A curated skill with this name wins; self-made ones never shadow it.+ const existing = skillLoader.load(slug);+ if (existing && existing.category !== SELF_MADE_CATEGORY) {+ return `Not saved — "${slug}" is already an installed skill. Use it (use_skill), or pick a different name if this is something else.`;+ }+ const file = join(skillLoader.rootDir(), SELF_MADE_CATEGORY, slug, 'SKILL.md');+ const prior = existsSync(file) ? readSelfMadeMeta(readFileSync(file, 'utf-8')) ?? undefined : undefined;+ skillLoader.saveSkill(`${SELF_MADE_CATEGORY}/${slug}`, composeSkillMd(draft, prior));+ const verb = prior ? 'updated' : 'saved';+ return `Self-made skill "${slug}" ${verb}. Tell the owner in one line, e.g. "☿ Skill ${verb}: ${slug} — say /skills self-made to review or remove it." Next time, use_skill "${slug}" before doing this by hand.`;+ },+ });+}diff --git a/src/capabilities/skills/use-skill.ts b/src/capabilities/skills/use-skill.tsindex d1c651cd..0535d53a 100644--- a/src/capabilities/skills/use-skill.ts+++ b/src/capabilities/skills/use-skill.ts@@ -2,6 +2,7 @@ import { tool, zodSchema } from 'ai';import { z } from 'zod';import type { SkillLoader } from '../../skills/loader.js';import type { PermissionManager } from '../permissions.js';+import { SELF_MADE_CATEGORY } from '../../skills/self-made.js';export function createUseSkillTool(skillLoader: SkillLoader, permissions: PermissionManager) {return tool({@@ -18,8 +19,12 @@ export function createUseSkillTool(skillLoader: SkillLoader, permissions: Permisif (skill['allowed-tools'] && skill['allowed-tools'].length > 0) {permissions.elevateForSkill(skill['allowed-tools']);}+ if (skill.category === SELF_MADE_CATEGORY) skillLoader.noteUse(skill.name);let result = `## Skill: ${skill.name}\n\n${skill.instructions}`;+ if (skill.category === SELF_MADE_CATEGORY) {+ result += '\n\n_Self-made skill: you wrote this after it worked once. Follow it, but trust the result, not the text — if a step fails, fix it and save the corrected procedure with create_skill under the same name._';+ }if (skill['allowed-tools'] && skill['allowed-tools'].length > 0) {result += `\n\nAllowed tools: ${skill['allowed-tools'].join(', ')}`;diff --git a/src/capabilities/system/confirm-belief.ts b/src/capabilities/system/confirm-belief.tsnew file mode 100644index 00000000..78142d4c--- /dev/null+++ b/src/capabilities/system/confirm-belief.ts@@ -0,0 +1,26 @@+import { tool, zodSchema } from 'ai';+import { z } from 'zod';+import type { UserMemoryStore } from '../../memory/user-memory.js';+import { findBeliefByText } from '../../core/commands/memory-commands.js';++/**+ * confirm_belief — the owner said "yes, that's right" about something Mercury+ * believes (ADR-026). A confirmed belief is owner-stated: it stops decaying and+ * wins any contest on the same dimension.+ */+export function createConfirmBeliefTool(userMemory: UserMemoryStore) {+ return tool({+ description:+ 'Confirm something you believe about the user when they say it is right ("yes, exactly", "that\'s correct, I do prefer X"). Finds the best-matching belief and marks it confirmed by the owner so it no longer fades. Reply with what was confirmed.',+ inputSchema: zodSchema(z.object({+ query: z.string().min(2).max(200).describe('The belief to confirm, in a few words'),+ })),+ execute: async ({ query }: { query: string }) => {+ const belief = findBeliefByText(userMemory.beliefs, query);+ if (!belief) return `Nothing I believe matches "${query.trim()}". If this is new, use save_memory instead.`;+ const confirmed = userMemory.beliefs.confirm(belief.id);+ if (!confirmed) return `"${belief.statement}" is no longer held, so there is nothing to confirm.`;+ return `Confirmed: "${confirmed.statement}"`;+ },+ });+}diff --git a/src/capabilities/system/correct-belief.ts b/src/capabilities/system/correct-belief.tsnew file mode 100644index 00000000..9f433339--- /dev/null+++ b/src/capabilities/system/correct-belief.ts@@ -0,0 +1,27 @@+import { tool, zodSchema } from 'ai';+import { z } from 'zod';+import type { UserMemoryStore } from '../../memory/user-memory.js';+import { findBeliefByText } from '../../core/commands/memory-commands.js';++/**+ * correct_belief — the owner corrected something Mercury believes (ADR-026):+ * the old belief is retired (kept, with the correction as evidence) and the+ * replacement is held as owner-stated.+ */+export function createCorrectBeliefTool(userMemory: UserMemoryStore) {+ return tool({+ description:+ 'Correct something you believe about the user when they say it is wrong ("no, I actually prefer Y", "that changed, now it\'s Z"). Finds the best-matching belief, retires it and holds the correction as what the owner stated. Reply with the old and new statements.',+ inputSchema: zodSchema(z.object({+ query: z.string().min(2).max(200).describe('The belief that is wrong, in a few words'),+ correction: z.string().min(3).max(220).describe('What is actually true, as one short statement about the user'),+ })),+ execute: async ({ query, correction }: { query: string; correction: string }) => {+ const belief = findBeliefByText(userMemory.beliefs, query);+ if (!belief) return `Nothing I believe matches "${query.trim()}". Use save_memory to record "${correction.trim()}" as new.`;+ const result = userMemory.beliefs.apply({ op: 'correction', beliefId: belief.id, statement: correction.trim(), excerpt: correction.trim(), evidenceKind: 'owner' });+ if (!result) return `Could not correct "${belief.statement}".`;+ return `Corrected: "${belief.statement}" → "${result.belief.statement}"`;+ },+ });+}diff --git a/src/capabilities/system/forget-memory.ts b/src/capabilities/system/forget-memory.tsnew file mode 100644index 00000000..ddddae6d--- /dev/null+++ b/src/capabilities/system/forget-memory.ts@@ -0,0 +1,25 @@+import { tool, zodSchema } from 'ai';+import { z } from 'zod';+import type { UserMemoryStore } from '../../memory/user-memory.js';+import { findBeliefByText } from '../../core/commands/memory-commands.js';++/**+ * forget_memory — the owner asked Mercury to forget something it believes+ * about them (ADR-026). Forgets the best-matching live belief; it leaves every+ * prompt and list for good (the row is kept, marked forgotten, never deleted).+ */+export function createForgetMemoryTool(userMemory: UserMemoryStore) {+ return tool({+ description:+ 'Forget something you believe about the user. Use when they say "forget that", "stop assuming X" or "don\'t remember Y". Finds the best-matching belief and forgets it for good; reply with what was forgotten. Do not ask for confirmation first.',+ inputSchema: zodSchema(z.object({+ query: z.string().min(2).max(200).describe('What to forget, in a few words (e.g. "prefers dark mode", "works at Cosmic Stack")'),+ })),+ execute: async ({ query }: { query: string }) => {+ const belief = findBeliefByText(userMemory.beliefs, query);+ if (!belief) return `Nothing I believe matches "${query.trim()}". Nothing was forgotten.`;+ userMemory.beliefs.forget(belief.id);+ return `Forgotten: "${belief.statement}"`;+ },+ });+}diff --git a/src/channels/cli.ts b/src/channels/cli.tsindex 8f7407a5..0f386fc9 100644--- a/src/channels/cli.ts+++ b/src/channels/cli.ts@@ -2163,6 +2163,12 @@ export class CLIChannel extends BaseChannel {}/** Status-bar note for a desktop plugin's activity (Optimus listening / working); null clears it. */+ /** The permission mode shown on the bottom bar. */+ setPermissionModeStatus(mode: PermissionMode): void {+ if (this.state.permissionMode === mode) return;+ this.update({ permissionMode: mode });+ }+setDesktopActivity(text: string | null): void {if (this.state.desktopActivity === text) return;this.update({ desktopActivity: text });diff --git a/src/channels/web.ts b/src/channels/web.tsindex ccab6188..07253baa 100644--- a/src/channels/web.ts+++ b/src/channels/web.ts@@ -17,7 +17,7 @@ interface PendingInteraction {}export interface ChatEvent {- type: 'thinking' | 'provider' | 'heartbeat' | 'step_start' | 'step_done' | 'text_delta' | 'text_done' | 'permission_request' | 'permission_continue' | 'permission_mode' | 'permission_resolved' | 'choice_prompt' | 'choice_resolved' | 'loop_warning' | 'error' | 'bot_activity';+ type: 'thinking' | 'provider' | 'heartbeat' | 'step_start' | 'step_done' | 'text_delta' | 'text_done' | 'permission_request' | 'permission_continue' | 'permission_mode' | 'permission_resolved' | 'choice_prompt' | 'choice_resolved' | 'loop_warning' | 'error' | 'bot_activity' | 'status';data?: Record<string, unknown>;}@@ -100,6 +100,11 @@ export class WebChannel extends BaseChannel {* session — the dashboard / third-party backends consume these for live* bot views. Bot turns never carry text deltas, only activity labels.*/+ /** Runtime status for every attached screen's bottom bar (no target: all clients). */+ broadcastStatus(status: Record<string, unknown>): void {+ this.broadcast({ type: 'status', data: { ...status } });+ }+broadcastBotActivity(ev: { botId: string; jobId: string; kind: string; label: string; detail?: string; stepIndex: number; elapsedMs: number; status?: string }): void {this.broadcast({ type: 'bot_activity', data: { ...ev } });}diff --git a/src/cli/attach.ts b/src/cli/attach.tsindex fe34e5a6..66295763 100644--- a/src/cli/attach.ts+++ b/src/cli/attach.ts@@ -17,6 +17,7 @@ import { getWebPort, readAttachToken } from '../web/auth.js';export type AttachEventType =| 'connected'+ | 'status'| 'thinking'| 'provider'| 'heartbeat'@@ -177,9 +178,11 @@ export class AttachClient {try {const res = await this.request('/api/status');if (!res.ok) return null;- const b = await res.json() as Record<string, unknown> & { tokens?: { used?: number; budget?: number } };- const used = Number(b.tokensUsed ?? b.tokens?.used ?? NaN);- const budget = Number(b.tokenBudget ?? b.tokens?.budget ?? NaN);+ // /api/status reports { tokens: { dailyUsed, dailyBudget } }; the old+ // field names never matched, so the launch pad budget row never showed.+ const b = await res.json() as Record<string, unknown> & { tokens?: { used?: number; budget?: number; dailyUsed?: number; dailyBudget?: number } };+ const used = Number(b.tokens?.dailyUsed ?? b.tokensUsed ?? b.tokens?.used ?? NaN);+ const budget = Number(b.tokens?.dailyBudget ?? b.tokenBudget ?? b.tokens?.budget ?? NaN);return Number.isFinite(used) && Number.isFinite(budget) ? { used, budget } : null;} catch { return null; }}@@ -273,6 +276,28 @@ export class AttachClient {}}+ /** The owner's saved permission mode; null = never chosen, undefined = unknown (old runtime / error). */+ async permissionMode(): Promise<'allow-all' | 'ask-me' | null | undefined> {+ try {+ const res = await this.request('/api/chat/settings');+ if (!res.ok) return undefined;+ const body = await res.json() as { permissionMode?: unknown };+ if (!('permissionMode' in body)) return undefined;+ return body.permissionMode === 'allow-all' || body.permissionMode === 'ask-me' ? body.permissionMode : null;+ } catch { return undefined; }+ }++ async setPermissionMode(mode: 'allow-all' | 'ask-me'): Promise<boolean> {+ try {+ const res = await this.request('/api/chat/settings', {+ method: 'PUT',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ bypassPermissions: mode === 'allow-all' }),+ });+ return res.ok;+ } catch { return false; }+ }+async resolvePermission(id: string, action: string): Promise<boolean> {try {const res = await this.request(`/api/chat/permission/${encodeURIComponent(id)}`, {diff --git a/src/core/agent.ts b/src/core/agent.tsindex 12be08c0..02883645 100644--- a/src/core/agent.ts+++ b/src/core/agent.ts@@ -102,6 +102,7 @@ import { setPluginImagesAllowed } from '../plugins/tool-bridge.js';import { handleCallCommand } from '../plugins/call-schedule.js';import { handlePluginsCommand, tryPluginCommand, hasPluginDialog, answerPluginDialog } from '../plugins/commands.js';import { handleChatCommand as handleChatCommandImpl } from './commands/chat-command.js';+import { formatCounts, formatLearnedLine } from './commands/memory-commands.js';import { handleBgCommand as handleBgCommandImpl } from './commands/bg-command.js';import { handleSaverCommand as handleSaverCommandImpl } from './commands/saver-command.js';import { handleSessionCommand as handleSessionCommandImpl } from './commands/session-command.js';@@ -109,6 +110,16 @@ import { handleFastPathCommand as handleFastPathCommandImpl } from './commands/fimport { openCliMemoryMenu as openCliMemoryMenuImpl } from './commands/cli-memory-menu.js';import { openMemoryChoiceMenu as openMemoryChoiceMenuImpl } from './commands/memory-choice-menu.js';import { openCliCommandMenu as openCliCommandMenuImpl } from './commands/cli-command-menu.js';+import { collectTurnSignals, parseToolTraceLines } from '../learning/signals.js';+import { extractBeliefOperations, type ExtractTurn } from '../learning/extract.js';+import { composeLearnedNote, pendingQuestion } from '../learning/announce.js';+import { asksAboutSelf, ownerWords } from '../learning/text.js';++/** Minimum gap between two "☿ Noted" lines on one channel. */+const NOTE_COOLDOWN_MS = 3 * 60 * 1000;+import { runReflection } from '../learning/reflection.js';+import { createOwnerSectionsMemo } from '../learning/owner-brief.js';+import type { ApplyResult } from '../memory/beliefs.js';/*** Step-aware text stream for the TUI. The SDK's textStream concatenates@@ -564,6 +575,14 @@ export class Agent {pendingPersonaNewlyCreated = false;/** Bot awaiting a daily token budget answer (final onboarding step). */pendingBudgetFor: string | null = null;+ /** Owner Model (ADR-026): per chat, the numbering of the last belief list printed+ * (`/memory learned|beliefs|contested`), so `/memory confirm <n>` knows what n is. */+ readonly lastBeliefListing = new Map<string, string[]>();+ /** Owner Model (ADR-026): per chat, the belief announced by the last `☿ Noted:` line,+ * so a plain "forget that" undoes it. Set by the per-turn learner; cleared on undo. */+ readonly lastNotedBeliefId = new Map<string, string>();+ /** When the last "☿ Noted" went out per channel: at most one every few minutes, so an upgrade's backlog never floods a chat. */+ private readonly lastNotedAt = new Map<string, number>();readonly programmingMode: ProgrammingMode;readonly researchMode: ResearchMode;readonly saverMode: SaverMode;@@ -582,6 +601,13 @@ export class Agent {private currentWorkKey: string | null = null;private outboxRetryTimer: ReturnType<typeof setInterval> | null = null;private replayingOutbox = false;+ /** Beliefs injected into the current turn's prompt (set by retrieveMemoryContext, ADR-026). */+ lastInjectedBeliefIds: string[] = [];+ /** "About {owner}" / "How to work with {owner}", recomputed only when the beliefs change (keeps the prompt prefix cacheable). */+ private readonly ownerSections = createOwnerSectionsMemo();+ private reflectionInFlight: Promise<void> | null = null;+ /** Beliefs the previous turn acted on; credited as "acted on without objection" once the next owner message shows no correction. */+ private pendingActedOn: { ids: string[]; sessionId?: string } | null = null;constructor(public config: MercuryConfig,@@ -622,6 +648,12 @@ export class Agent {await this.heartbeat();});+ // After a migration (or when asked), reflect soon after boot instead of+ // waiting an hour, so migrated candidates are judged against real sessions.+ if (this.userMemory?.beliefs.getMeta('reflection_force') === '1') {+ setTimeout(() => { void this.reflectNow(); }, 2 * 60 * 1000).unref();+ }+setAskUserHandler(async (question, choices, channelId, channelType) => {return this.presentChoice(question, choices, channelId, channelType);});@@ -2302,12 +2334,42 @@ export class Agent {*/private async retrieveMemoryContext(msg: ChannelMessage): Promise<string[]> {const blocks: string[] = [];+ this.lastInjectedBeliefIds = [];if (this.userMemory) {- const memoryContext = this.userMemory.retrieveRelevant(msg.content, { maxRecords: 5, maxChars: 900 });- if (memoryContext.context) {- blocks.push(`Second Brain — memory retrieved for this message:\n${memoryContext.context}`);+ // Beliefs relevant to this message (ADR-026): up to 5 held/confirmed+ // ones, with confidence, so the model can lean on them and the next+ // turn can credit them as "acted on without objection".+ try {+ const words = ownerWords(msg.content);+ const selfAsk = asksAboutSelf(words);+ const relevant = selfAsk+ ? this.userMemory.beliefs.strongest(12)+ : this.userMemory.beliefs.searchRelevant(words, 5);+ if (relevant.length > 0) {+ // Only sure beliefs earn "acted on without objection" credit;+ // a candidate the model happened to see proves nothing yet.+ const ids = relevant.map((b) => b.id);+ this.userMemory.beliefs.markUsed(ids);+ this.lastInjectedBeliefIds = relevant.filter((b) => b.status === 'held' || b.status === 'confirmed').map((b) => b.id);+ const line = (b: typeof relevant[number]) => {+ const status = b.status === 'candidate' ? 'unverified, seen once' : b.status;+ return `- (confidence ${b.confidence.toFixed(2)}, ${status}${b.context !== '*' ? `, ${b.context}` : ''}) ${b.statement}`;+ };+ const heading = selfAsk+ ? 'The owner is asking what you know about them. Your strongest beliefs (say which are unverified; the search_memory tool finds more):'+ : 'Beliefs about the owner relevant to this message (treat "unverified" ones as hunches, not facts):';+ blocks.push(heading + '\n' + relevant.map(line).join('\n'));+ }+ } catch (err) {+ logger.debug({ err: (err as Error).message }, 'belief retrieval failed');}+ // The beliefs above are the only local recall (ADR-026: one source);+ // their statements are what the pool search dedupes against.+ const localRecall = this.lastInjectedBeliefIds+ .map((id) => this.userMemory?.beliefs.get(id)?.statement)+ .filter((s): s is string => typeof s === 'string');+// Local-first, then pool: query the cloud SharedMemoryPool for additional// context the second brain didn't surface. Gated by config, fails open// (any error → proceed with local-only context). 5-min cache per query.@@ -2316,11 +2378,10 @@ export class Agent {const ck = this.config.memory.collaborativeKnowledge;const cloud = this.config.cloud;const tokenStore = getCloudTokenStore();- const localRecallIsStrong = memoryContext.records.length >= 3;+ const localRecallIsStrong = localRecall.length >= 3;if (ck?.poolSearch !== false && cloud?.enabled && cloud?.jwt && cloud?.apiUrl && !localRecallIsStrong) {try {const { searchPool, formatPoolContextBlock, dedupeAgainstLocal } = await import('../cloud/pool-search.js');- const localSummaries = memoryContext.records.map((r) => r.summary);const poolHits = await searchPool(cloud.apiUrl,cloud.jwt,@@ -2341,7 +2402,7 @@ export class Agent {}},);- const deduped = dedupeAgainstLocal(poolHits, localSummaries);+ const deduped = dedupeAgainstLocal(poolHits, localRecall);const poolBlock = formatPoolContextBlock(deduped, 1500);if (poolBlock) blocks.push(poolBlock);} catch (err) {@@ -2622,7 +2683,7 @@ export class Agent {content: msg.content,timestamp: msg.timestamp,externalMessageId: msg.id,- metadata: { channelType: msg.channelType, channelId: msg.channelId, ...(requestId ? { requestId } : {}) },+ metadata: { channelType: msg.channelType, channelId: msg.channelId, workCwd: this.capabilities.getCwd(), ...(requestId ? { requestId } : {}) },});// One read of the session per turn (it used to be re-read three times).const activeSession = this.sessions.get(canonicalSession.id);@@ -4109,8 +4170,12 @@ export class Agent {channelType: msg.channelType,});+ // The learning loop runs fire-and-forget; a "☿ Noted" line, when one is+ // due, waits for the reply to be delivered first so it never lands ahead of it.+ let replyDelivered: () => void = () => {};+ const delivered = new Promise<void>((resolve) => { replyDelivered = resolve; });if (msg.channelType !== 'internal' && msg.metadata?.channelAccess !== 'general') {- this.extractMemory(msg.content, finalText).catch(err => {+ this.extractMemory(msg.content, finalText, { msg, channel, sessionId: canonicalSessionId, toolTrace: turnToolTrace, delivered }).catch(err => {logger.warn({ err }, 'Memory extraction failed');});this.scheduleStatusVerbRefresh(channel, msg.content);@@ -4145,8 +4210,10 @@ export class Agent {alreadyStreamed: !requiresFinalSend(msg.channelType, cliResponseStreamed),forceBanner: isMercuryCodeExecution,});+ replyDelivered();this.markProgress();} else {+ replyDelivered();logger.debug('Internal prompt processed, no channel response needed');}@@ -4256,8 +4323,11 @@ export class Agent {/** Stable system prompt; assembly lives in core/system-prompt.ts. */private buildSystemPrompt(): string {const summary = this.userMemory ? this.userMemory.getSummary() : null;+ const ownerName = this.config.identity.owner || 'the owner';return assembleSystemPrompt({identityPrompt: this.identity.getSystemPrompt(this.config.identity),+ ownerSections: this.userMemory ? this.ownerSections(this.userMemory.beliefs, ownerName) : null,+ ownerName,skillContext: this.capabilities.getSkillContext(),botSection: this.botManager ? this.botManager.getSystemPromptSection() : '',programmingSuffix: this.programmingMode.getSystemPromptSuffix(),@@ -4337,6 +4407,57 @@ export class Agent {}}+ /**+ * Run the reflection pass now (heartbeat, and shutdown from index.ts). It+ * only calls the model when at least one session has a new owner message+ * since the last pass; concurrent calls share one run.+ */+ /**+ * The provider the Owner Model learns on (ADR-026): `memory.learning.provider`+ * when it names a usable provider, else the chat default. Resolved per call so+ * a key added at runtime or a changed default is picked up.+ */+ private learningProviders(): { getDefault(): ReturnType<ProviderRegistry['getDefault']> } {+ return {+ getDefault: () => {+ const requested = this.config.memory.learning?.provider?.trim();+ if (requested) {+ const getUsable = typeof this.providers.getUsable === 'function' ? this.providers.getUsable.bind(this.providers) : (name?: string) => (name ? this.providers.get(name) : undefined);+ const provider = getUsable(requested);+ if (provider) return provider;+ if (!this.warnedLearningProvider) {+ this.warnedLearningProvider = true;+ logger.warn({ requested }, 'memory.learning.provider is not registered or not usable — learning on the default provider');+ }+ }+ return this.providers.getDefault();+ },+ };+ }+ private warnedLearningProvider = false;++ async reflectNow(): Promise<void> {+ if (!this.userMemory) return;+ if (this.reflectionInFlight) return this.reflectionInFlight;+ const userMemory = this.userMemory;+ const force = userMemory.beliefs.getMeta('reflection_force') === '1';+ if (force) userMemory.beliefs.setMeta('reflection_force', '0');+ this.reflectionInFlight = runReflection({+ beliefs: userMemory.beliefs,+ sessions: this.sessions,+ providers: this.learningProviders(),+ tokenBudget: this.tokenBudget,+ learningPaused: () => userMemory.isLearningPaused(),+ ownerName: this.config.identity.owner || 'the owner',+ agentId: this.config.cloud.agentId || undefined,+ }, { force }).then((result) => {+ if (result.ran) this.syncTokenInfoToCli();+ }).finally(() => {+ this.reflectionInFlight = null;+ });+ return this.reflectionInFlight;+ }+private async heartbeat(): Promise<void> {logger.debug('Heartbeat tick');@@ -4345,22 +4466,24 @@ export class Agent {logger.info({ pruned }, 'Episodic memory pruned');}- if (this.userMemory) {+ if (this.skillLoader) {+ // Self-made skills nobody used again go dormant (ADR-027); cheap, file-only.try {- const consolidation = this.userMemory.consolidate();- if (consolidation.profileUpdated || consolidation.reflectionCount > 0) {- logger.info({ consolidation }, 'Second brain consolidated');- }-- const pruning = this.userMemory.prune();- if (pruning.movedToSubconscious > 0 || pruning.hardDeleted > 0 || pruning.promoted > 0) {- logger.info({ pruning }, 'Second brain pruned');- }+ const { selfMadeHygiene } = await import('../skills/self-made.js');+ const { dormant } = selfMadeHygiene(this.skillLoader.rootDir());+ if (dormant.length > 0) this.skillLoader.discover();} catch (err) {- logger.warn({ err }, 'Second brain heartbeat error');+ logger.debug({ err }, 'self-made skill hygiene failed');}}+ if (this.userMemory) {+ // The reflection pass (ADR-026): one model call over the owner's turns+ // since the last pass, then decay; a no-op when nothing new was said.+ // It is the only maintenance the Second Brain gets — one source.+ await this.reflectNow().catch((err) => logger.warn({ err }, 'Reflection pass failed'));+ }+const notifications: string[] = [];const usagePct = this.tokenBudget.getUsagePercentage();@@ -4395,7 +4518,7 @@ export class Agent {/** Learn from a conversation that didn't happen as chat turns (e.g. a voice call). */learnFromConversation(ownerSaid: string, mercurySaid: string): void {- void this.extractMemory(ownerSaid, mercurySaid).catch((err) => logger.warn({ err }, 'Learning from a call failed'));+ void this.extractMemory(ownerSaid, mercurySaid, {}).catch((err) => logger.warn({ err }, 'Learning from a call failed'));}/** Who Mercury is, for a voice that isn't the main model: identity, about, and the owner's profile. */@@ -4403,148 +4526,122 @@ export class Agent {const parts = [this.identity.getSystemPrompt(this.config.identity)];if (this.userMemory) {try {- const profile = this.userMemory.getProfile().trim();- const active = this.userMemory.getActiveSummary().trim();- if (profile || active) parts.push(`# What you know about ${this.config.identity.owner || 'your owner'} (from your Second Brain)\n${[profile, active].filter(Boolean).join('\n').slice(0, 4000)}`);+ const ownerName = this.config.identity.owner || 'your owner';+ const sections = this.ownerSections(this.userMemory.beliefs, ownerName);+ const text = [sections?.about, sections?.howToWork].filter((s): s is string => Boolean(s && s.trim())).join('\n\n');+ if (text) parts.push(`# What you know about ${ownerName} (from your Second Brain)\n${text.slice(0, 4000)}`);} catch { /* memory unavailable: identity alone */ }}return parts.join('\n\n');}- private async extractMemory(userMessage: string, agentResponse: string): Promise<void> {+ /**+ * The per-turn learning loop (ADR-026, docs/owner-model.md §3.4): the+ * turn's signals plus one small model call become belief operations+ * applied to the belief store, and at most one "☿ Noted" line goes back+ * on the channel after the reply. The legacy `memories` table is no+ * longer written here; only the explicit save_memory tool writes it.+ */+ private async extractMemory(+ userMessage: string,+ agentResponse: string,+ ctx: {+ msg?: ChannelMessage;+ channel?: { send(content: string, targetId?: string): Promise<void> };+ sessionId?: string;+ /** The turn's tool-trace lines (formatToolTraceLine). */+ toolTrace?: readonly string[];+ /** Resolves once the reply reached the channel; the note is sent after it. */+ delivered?: Promise<void>;+ } = {},+ ): Promise<void> {if (!this.userMemory) return;if (this.userMemory.isLearningPaused()) return;+ const beliefs = this.userMemory.beliefs;+ const channelAccess = typeof ctx.msg?.metadata?.channelAccess === 'string' ? ctx.msg.metadata.channelAccess : undefined;+ if (channelAccess === 'general') return;++ // The session's earlier turns, role-labelled: context for the extractor+ // and the source for "same request again" detection.+ const history: ExtractTurn[] = [];+ if (ctx.sessionId && ctx.msg) {+ try {+ const thisTurn = new Set([ctx.msg.id, `${ctx.msg.id}:assistant`, `${ctx.msg.id}:tools`]);+ for (const m of this.sessions.get(ctx.sessionId).messages) {+ if (m.kind !== 'message' || (m.role !== 'user' && m.role !== 'assistant')) continue;+ if (m.externalMessageId && thisTurn.has(m.externalMessageId)) continue;+ history.push({ role: m.role === 'user' ? 'owner' : 'mercury', content: m.content });+ }+ } catch (err) {+ logger.debug({ err }, 'Session history unavailable for learning');+ }+ }+ const signals = collectTurnSignals({+ message: userMessage,+ previousUserMessages: history.filter((t) => t.role === 'owner').map((t) => t.content),+ toolTrace: parseToolTraceLines(ctx.toolTrace ?? []),+ allowAllActive: this.capabilities.permissions.isAutoApproveAll(),+ permissionAnswers: this.capabilities.permissions.drainAnswers(),+ channelType: ctx.msg?.channelType,+ sessionId: ctx.sessionId,+ cwd: this.capabilities.getCwd(),+ });- const trivial = /^(hi|hello|hey|thanks|thank you|ok|okay|yes|no|bye|goodbye|good morning|good evening)\b/i;- if (trivial.test(userMessage.trim())) return;-- if (!this.tokenBudget.canAfford(800)) return;+ // Validation loop (§3.1): the beliefs the previous turn acted on earn+ // weak support once the owner's next message in the same session shows+ // no correction. Then remember what this turn acted on.+ const pending = this.pendingActedOn;+ if (pending && pending.ids.length > 0 && pending.sessionId && pending.sessionId === ctx.sessionId && !signals.correction.detected) {+ try { beliefs.actedOnOk(pending.ids, pending.sessionId); } catch (err) { logger.debug({ err }, 'actedOnOk failed'); }+ }+ this.pendingActedOn = ctx.sessionId ? { ids: [...(this.lastInjectedBeliefIds ?? [])], sessionId: ctx.sessionId } : null;try {- const provider = this.providers.getDefault();- const result = await generateText({- model: provider.getModelInstance(),- system: `You extract structured memory from conversations. Output a JSON array of 0-3 memory candidates.--Each candidate: { type, categories, summary (concise fact, 12-220 chars), detail (optional explanation), evidenceKind ("direct" if explicitly stated, "inferred" if deduced), confidence (0-1), importance (0-1), durability (0-1) }--TYPE DEFINITIONS (pick the single most specific one):-- identity: who the user IS — their name, role, job title, self-description-- relationship: other people the user knows — MUST include the person's name in summary-- preference: likes, dislikes, style choices, opinions-- goal: aspirations, targets, things they want to achieve-- project: specific ongoing work, initiatives, things being built-- habit: routines, recurring behaviors, schedules-- decision: choices made, commitments, selected approaches-- constraint: limitations, rules they follow, things they avoid-- episode: notable one-time events worth remembering--CATEGORIES (pick 1-3 domain buckets that best fit this memory):-- personal: the user's private life, non-work-- health: medical, physical, mental wellbeing-- family: family members, family relationships-- work: job, professional, career-- finance: money, spending, investments-- technical: programming, tools, engineering-- education: learning, study, courses-- social: friends, social life, community-- travel: trips, places, geography-- general: anything that doesn't fit the above--RULES:-- Each semantic fact must appear EXACTLY ONCE. Never store the same information under multiple types.-- If a fact is about someone else's role/relationship to the user, use "relationship" (not "identity").-- "identity" is ONLY for the user themselves.-- For relationships, always name the person: "Salman is user's co-developer" not "User works with a co-developer".-- Only extract specific, durable, user-specific information.-- Do NOT extract trivial observations, greetings, or assistant behavior.-- "categories" must be an array of 1-3 lowercase strings from the list above. If none fit, suggest a new single-word lowercase category.-- Output pure JSON array, no markdown fences.`,- messages: [- { role: 'user', content: `User: ${userMessage}\nAssistant: ${agentResponse}` },- ],- maxOutputTokens: 400,- });-- this.tokenBudget.recordUsage({- provider: provider.name,- model: provider.getModel(),- inputTokens: result.usage?.inputTokens ?? 0,- outputTokens: result.usage?.outputTokens ?? 0,- totalTokens: (result.usage?.inputTokens ?? 0) + (result.usage?.outputTokens ?? 0),- channelType: 'internal',+ const ops = await extractBeliefOperations({+ providers: this.learningProviders(),+ tokenBudget: this.tokenBudget,+ beliefs,+ learningPaused: () => this.userMemory?.isLearningPaused() ?? true,agentId: this.config.cloud.agentId || undefined,+ }, {+ ownerMessage: userMessage,+ mercuryReply: agentResponse,+ recentTurns: history.slice(-6),+ signals,+ ownerName: this.config.identity.owner || 'the owner',+ pendingQuestion: pendingQuestion(beliefs),+ channelAccess,+ sessionId: ctx.sessionId,+ turnRef: ctx.msg?.id,});this.syncTokenInfoToCli();+ if (ops.length === 0) return;- const text = result.text.trim();- if (!text) return;-- let candidates: Array<{- type: string;- categories?: string[];- summary: string;- detail?: string;- evidenceKind?: string;- confidence: number;- importance: number;- durability: number;- }>;-- try {- const jsonStr = text.replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/, '');- const parsed = JSON.parse(jsonStr);- // Handle both single object and array of objects- candidates = Array.isArray(parsed) ? parsed : [parsed];- } catch {- const facts = text- .split('\n')- .map(l => l.replace(/^-\s*/, '').trim())- // Skip JSON-like lines (key-value pairs, braces, brackets)- .filter(f => f.length > 10 && f.length < 200 && !/^["{\[\]}]|":\s*"/.test(f));- candidates = facts.slice(0, 3).map(f => ({- type: 'preference',- categories: ['general'],- summary: f,- confidence: 0.75,- importance: 0.7,- durability: 0.7,- evidenceKind: 'inferred',- }));- }-- const validTypes = ['identity', 'preference', 'goal', 'project', 'habit', 'decision', 'constraint', 'relationship', 'episode'];- const typed = candidates- .filter(c => c.summary && c.summary.length >= 12 && c.summary.length <= 220)- .filter(c => validTypes.includes(c.type))- .map(c => {- // Normalise categories: must be array of strings, 1-3, lowercase, trimmed.- let cats: string[] = ['general'];- if (Array.isArray(c.categories) && c.categories.length > 0) {- const cleaned = c.categories- .map((cat: unknown) => String(cat).trim().toLowerCase())- .filter((cat: string) => cat.length > 0)- .slice(0, 3);- if (cleaned.length > 0) cats = cleaned;- }- return {- type: c.type as any,- categories: cats,- summary: c.summary,- detail: c.detail,- evidenceKind: (c.evidenceKind === 'direct' ? 'direct' : 'inferred') as 'direct' | 'inferred',- confidence: Math.min(1, Math.max(0, c.confidence ?? 0.7)),- importance: Math.min(1, Math.max(0, c.importance ?? 0.7)),- durability: Math.min(1, Math.max(0, c.durability ?? 0.7)),- };- });-- if (typed.length > 0) {- const remembered = this.userMemory.remember(typed, 'conversation');- if (remembered.length > 0) {- logger.info({ count: remembered.length, types: remembered.map(r => r.type) }, 'Second brain memories stored');+ const results: ApplyResult[] = [];+ for (const op of ops) {+ try {+ const r = beliefs.apply(op);+ if (r) results.push(r);+ } catch (err) {+ logger.warn({ err, op: op.op }, 'Belief operation failed');}}+ if (results.length > 0) logger.info({ changes: results.map((r) => `${r.change}:${r.belief.kind}`) }, 'Beliefs updated from turn');++ const note = composeLearnedNote(results, { beliefs });+ if (!note || !ctx.channel || !ctx.msg) return;+ const notedAt = this.lastNotedAt.get(ctx.msg.channelId) ?? 0;+ if (Date.now() - notedAt < NOTE_COOLDOWN_MS) return;+ this.lastNotedAt.set(ctx.msg.channelId, Date.now());+ // After the reply, never ahead of it (capped so a stuck delivery can't hold the note forever).+ await Promise.race([+ ctx.delivered ?? Promise.resolve(),+ new Promise<void>((resolve) => { setTimeout(resolve, 20_000).unref(); }),+ ]);+ await ctx.channel.send(note, ctx.msg.channelId);+ // "forget that" right after the note undoes exactly this belief.+ const noted = results.find((r) => r.behaviorChanging)?.belief.id;+ if (noted) this.lastNotedBeliefId.set(ctx.msg.channelId, noted);} catch (err) {logger.warn({ err }, 'Memory extraction error');}@@ -4831,6 +4928,8 @@ Is this productive iteration or a stuck loop?`,'`/skills install <id>` — install from the registry','`/skills install <url>` — install raw SKILL.md from a URL','`/skills remove <id>` — uninstall',+ '`/skills self-made` — the skills Mercury wrote for itself, with use counts',+ '`/skills self-made enable|disable|remove <name>`','','Browse the full catalog at https://skills.mercuryagent.sh',].join('\n'),@@ -4859,6 +4958,38 @@ Is this productive iteration or a stuck loop?`,return;}+ case 'self-made':+ case 'self':+ case 'mine': {+ const { listSelfMade } = await import('../skills/self-made.js');+ const root = this.skillLoader?.rootDir();+ if (!root) { await channel.send('Skills are not available in this session.', channelId); return; }+ const action = (args[0] || '').toLowerCase();+ const target = args.slice(1).join(' ').trim();+ if ((action === 'enable' || action === 'disable' || action === 'remove') && target) {+ const entry = listSelfMade(root).find((s) => s.name === target);+ if (!entry) { await channel.send(`No self-made skill named "${target}".`, channelId); return; }+ if (action === 'remove') { this.skillLoader!.deleteSkill(target); await channel.send(`Removed self-made skill "${target}".`, channelId); return; }+ this.skillLoader!.setSkillActive(target, action === 'enable');+ await channel.send(`${action === 'enable' ? 'Enabled' : 'Disabled'} self-made skill "${target}".`, channelId);+ return;+ }+ const entries = listSelfMade(root);+ if (entries.length === 0) {+ await channel.send('No self-made skills yet. After a long task works, Mercury saves the procedure here (create_skill) so the next time is one step.', channelId);+ return;+ }+ const fmt = (ms: number | null) => (ms ? new Date(ms).toISOString().slice(0, 10) : 'never');+ const lines = [`**${entries.length} self-made skill${entries.length > 1 ? 's' : ''}** (Mercury wrote these; unverified until used again):`, ''];+ for (const s of entries) {+ lines.push(`${s.active ? '•' : '◦'} **${s.name}** — ${s.description}`);+ lines.push(` made ${fmt(s.made)} · used ${s.uses}× · last ${fmt(s.lastUsed)}${s.active ? '' : ' · dormant'}`);+ }+ lines.push('', '_`/skills self-made enable|disable|remove <name>`. Unused ones go dormant after 30 days; nothing is deleted on its own._');+ await channel.send(lines.join('\n'), channelId);+ return;+ }+case 'search':case 'find': {if (!arg) {@@ -5196,6 +5327,21 @@ Is this productive iteration or a stuck loop?`,lines.push(` ${type}: ${count}`);}}+ // Owner Model (ADR-026): the honest numbers and the last few things learned.+ try {+ const beliefs = this.userMemory.beliefs;+ const now = Date.now();+ lines.push('', formatCounts(beliefs.counts()));+ const recent = beliefs.recentlyLearned(now - 30 * 86_400_000, 3);+ if (recent.length > 0) {+ lines.push('Recently learned:');+ recent.forEach((b, i) => lines.push(` ${formatLearnedLine(beliefs, i + 1, b, now)}`));+ this.lastBeliefListing.set(channelId, recent.map((b) => b.id));+ }+ lines.push('More: /memory learned, /memory beliefs, /memory contested; /memory confirm|forget|correct <n>.');+ } catch (err) {+ logger.debug({ err }, 'belief overview unavailable');+ }await channel.send(lines.join('\n'), channelId);}diff --git a/src/core/commands/chat-command.ts b/src/core/commands/chat-command.tsindex 71c9ebd2..3a7cdb29 100644--- a/src/core/commands/chat-command.ts+++ b/src/core/commands/chat-command.ts@@ -44,11 +44,13 @@ import { CLIChannel } from '../../channels/cli.js';import { formatToolStep, formatNarrative, type NarrativeStep } from '../../utils/tool-label.js';import { getTelegramHelp, getDiscordHelp, getSlackHelp } from '../../utils/manual.js';import { WebChannel } from '../../channels/web.js';+import { describePermissionMode, loadPermissionMode, savePermissionMode } from '../../capabilities/permission-mode-store.js';import type { ArrowSelectOption } from '../../utils/arrow-select.js';import { PLAYER_CONTROLS, handlePlayerAction, formatNowPlaying } from '../../spotify/ui.js';import { getCloudTokenStore } from '../../cloud/token-store.js';-import { updateCliProviderStatus } from '../provider-status.js';+import { publishStatus, updateCliProviderStatus } from '../provider-status.js';import { whatsNewText } from '../../utils/whats-new.js';+import { FORGET_THAT_RE, MEMORY_SUBCOMMAND_HELP, handleBeliefCommand } from './memory-commands.js';export async function handleChatCommand(agent: Agent, content: string, channelType: string, channelId: string): Promise<boolean> {const trimmed = content.trim();@@ -151,20 +153,60 @@ export async function handleChatCommand(agent: Agent, content: string, channelTyreturn true;}+ // Direct form: /permissions allow-all | /permissions ask-me (owner's terminal or dashboard).+ const directMode = /^\/permissions\s+(allow-all|ask-me)$/.exec(cmd)?.[1] as 'allow-all' | 'ask-me' | undefined;+ if (directMode && (channel instanceof WebChannel || (channelType === 'cli' && channel instanceof CLIChannel))) {+ if (channel instanceof WebChannel) channel.setBypassPermissions(directMode === 'allow-all');+ else {+ agent.capabilities.permissions.setAutoApproveAll(directMode === 'allow-all');+ if (directMode === 'allow-all') agent.capabilities.permissions.addTempScope('/', true, true);+ if (channel instanceof CLIChannel) channel.setPermissionModeStatus(directMode);+ }+ savePermissionMode(directMode);+ publishStatus();+ await channel.send(`${describePermissionMode(directMode)} Saved.`, channelId);+ return true;+ }+if (cmd === '/permissions') {if (channelType === 'cli' && channel instanceof CLIChannel) {const mode = await channel.askPermissionMode?.();if (mode === 'allow-all') {agent.capabilities.permissions.setAutoApproveAll(true);agent.capabilities.permissions.addTempScope('/', true, true);- await channel.send('Allow All mode active for this session. All scopes, commands, and loops auto-approved. Resets on restart.', channelId);} else {agent.capabilities.permissions.setAutoApproveAll(false);- await channel.send('Ask Me mode active. Risky actions will prompt for confirmation.', channelId);}+ const chosen = mode === 'allow-all' ? 'allow-all' : 'ask-me';+ savePermissionMode(chosen);+ channel.setPermissionModeStatus(chosen);+ publishStatus();+ await channel.send(`${describePermissionMode(chosen)} Saved: Mercury remembers this. Change it any time with /permissions.`, channelId);+ return true;+ }+ // The attached terminal and the dashboard are the owner's own screens+ // (channel type 'web'): offer the same choice and remember it.+ if (channel instanceof WebChannel) {+ const current = loadPermissionMode() ?? (channel.getSettings().bypassPermissions ? 'allow-all' : 'ask-me');+ const picked = await channel.presentChoicePrompt(+ `How should Mercury handle risky actions? (now: ${current === 'allow-all' ? 'Allow All' : 'Ask Me'})`,+ [+ { value: 'ask-me', label: 'Ask Me: ask before writing files or running commands' },+ { value: 'allow-all', label: 'Allow All: just do it (blocked commands stay blocked)' },+ ],+ channelId,+ );+ if (picked !== 'ask-me' && picked !== 'allow-all') {+ await channel.send('No change. Type /permissions allow-all or /permissions ask-me to switch directly.', channelId);+ return true;+ }+ channel.setBypassPermissions(picked === 'allow-all');+ savePermissionMode(picked);+ publishStatus();+ await channel.send(`${describePermissionMode(picked)} Saved: Mercury remembers this. Change it any time with /permissions.`, channelId);return true;}- await channel.send('Use /permissions in CLI to switch permission mode. On Telegram, use the /permissions button or command.', channelId);+ await channel.send('On Telegram, use the /permissions button or command. Your own terminal and dashboard use /permissions too.', channelId);return true;}@@ -390,6 +432,28 @@ export async function handleChatCommand(agent: Agent, content: string, channelTyreturn true;}+ // Owner Model (ADR-026): /memory learned|beliefs|contested|confirm|forget|correct|settle,+ // /forget <text>, and a plain "forget that" right after a `☿ Noted:` line.+ if (cmd.startsWith('/memory ') || cmd === '/forget' || cmd.startsWith('/forget ') || FORGET_THAT_RE.test(trimmed)) {+ if (!agent.userMemory) {+ if (FORGET_THAT_RE.test(trimmed)) return false; // nothing noted without a memory store; let the model answer+ await channel.send('Second brain is not available, so there is nothing to recall or forget.', channelId);+ return true;+ }+ const reply = handleBeliefCommand(+ { beliefs: agent.userMemory.beliefs, listings: agent.lastBeliefListing, lastNoted: agent.lastNotedBeliefId },+ channelId,+ trimmed,+ );+ if (reply === null) {+ if (FORGET_THAT_RE.test(trimmed)) return false; // no `☿ Noted:` to undo in this chat+ await channel.send(MEMORY_SUBCOMMAND_HELP, channelId);+ return true;+ }+ await channel.send(reply, channelId);+ return true;+ }+if (cmd === '/memory') {if (!agent.userMemory) {const cfg = ctx.config();diff --git a/src/core/commands/cli-memory-menu.ts b/src/core/commands/cli-memory-menu.tsindex db8dfac5..7999647f 100644--- a/src/core/commands/cli-memory-menu.ts+++ b/src/core/commands/cli-memory-menu.ts@@ -36,6 +36,7 @@ import {clearSlackAccess,} from '../../utils/config.js';import type { ArrowSelectOption } from '../../utils/arrow-select.js';+import { MEMORY_SUBCOMMAND_HELP, handleBeliefCommand } from './memory-commands.js';export async function openCliMemoryMenu(agent: Agent, channel: CLIChannel, channelId: string, select?: (title: string, options: ArrowSelectOption[]) => Promise<string>): Promise<void> {if (!agent.userMemory) return;@@ -46,6 +47,10 @@ export async function openCliMemoryMenu(agent: Agent, channel: CLIChannel, channconst shareLabel = agent.userMemory!.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';const action = await sel('Memory', [{ value: 'overview', label: 'Overview' },+ { value: 'learned', label: 'What I learned' },+ { value: 'beliefs', label: 'What I believe about you' },+ { value: 'contested', label: 'Contested' },+ { value: 'forget', label: 'Forget…' },{ value: 'recent', label: 'Recent Memories' },{ value: 'shared', label: 'Shared Memories' },{ value: 'search', label: 'Search' },@@ -62,6 +67,23 @@ export async function openCliMemoryMenu(agent: Agent, channel: CLIChannel, channcontinue;}+ // Owner Model (ADR-026) views share the chat-command implementation.+ const beliefDeps = { beliefs: agent.userMemory!.beliefs, listings: agent.lastBeliefListing, lastNoted: agent.lastNotedBeliefId };+ if (action === 'learned' || action === 'beliefs' || action === 'contested') {+ await channel.send(handleBeliefCommand(beliefDeps, channelId, `/memory ${action}`) ?? MEMORY_SUBCOMMAND_HELP, channelId);+ continue;+ }++ if (action === 'forget') {+ const text = await channel.prompt('Forget what? (a few words, or a number from the last list): ');+ if (!text) continue;+ const reply = /^\d+$/.test(text.trim())+ ? handleBeliefCommand(beliefDeps, channelId, `/memory forget ${text.trim()}`)+ : handleBeliefCommand(beliefDeps, channelId, `/forget ${text}`);+ await channel.send(reply ?? MEMORY_SUBCOMMAND_HELP, channelId);+ continue;+ }+if (action === 'recent') {const recent = agent.userMemory!.getRecent(10);if (recent.length === 0) {diff --git a/src/core/commands/memory-choice-menu.ts b/src/core/commands/memory-choice-menu.tsindex 4a34aca9..2c5d9af7 100644--- a/src/core/commands/memory-choice-menu.ts+++ b/src/core/commands/memory-choice-menu.ts@@ -34,7 +34,7 @@ import {removeSlackUser,clearSlackAccess,} from '../../utils/config.js';-+import { MEMORY_SUBCOMMAND_HELP, handleBeliefCommand } from './memory-commands.js';export async function openMemoryChoiceMenu(agent: Agent, channel: any, channelId: string): Promise<void> {if (!agent.userMemory) return;@@ -43,6 +43,10 @@ export async function openMemoryChoiceMenu(agent: Agent, channel: any, channelIdconst shareLabel = agent.userMemory.isShareLearning() ? 'Shared Learning: ON' : 'Shared Learning: OFF';const action = await channel.presentChoicePrompt('Memory', [{ value: 'overview', label: 'Overview' },+ { value: 'learned', label: 'What I learned' },+ { value: 'beliefs', label: 'What I believe about you' },+ { value: 'contested', label: 'Contested' },+ { value: 'forget', label: 'Forget…' },{ value: 'recent', label: 'Recent Memories' },{ value: 'shared', label: 'Shared Memories' },{ value: 'toggle', label: learningLabel },@@ -58,6 +62,21 @@ export async function openMemoryChoiceMenu(agent: Agent, channel: any, channelIdreturn;}+ // Owner Model (ADR-026) views share the chat-command implementation.+ if (action === 'learned' || action === 'beliefs' || action === 'contested') {+ const deps = { beliefs: agent.userMemory.beliefs, listings: agent.lastBeliefListing, lastNoted: agent.lastNotedBeliefId };+ await channel.send(handleBeliefCommand(deps, channelId, `/memory ${action}`) ?? MEMORY_SUBCOMMAND_HELP, channelId);+ return;+ }++ if (action === 'forget') {+ // Channels with a choice prompt but no free-text prompt: show the list and explain the verb.+ const deps = { beliefs: agent.userMemory.beliefs, listings: agent.lastBeliefListing, lastNoted: agent.lastNotedBeliefId };+ const listing = handleBeliefCommand(deps, channelId, '/memory learned 30') ?? MEMORY_SUBCOMMAND_HELP;+ await channel.send(`${listing}\n\nOr say /forget <a few words> to forget by text.`, channelId);+ return;+ }+if (action === 'recent') {const recent = agent.userMemory.getRecent(10);if (recent.length === 0) {diff --git a/src/core/commands/memory-commands.test.ts b/src/core/commands/memory-commands.test.tsnew file mode 100644index 00000000..38beaf48--- /dev/null+++ b/src/core/commands/memory-commands.test.ts@@ -0,0 +1,198 @@+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, describe, expect, it } from 'vitest';+import { SecondBrainDB, isBetterSqlite3Available } from '../../memory/second-brain-db.js';+import { BeliefStore } from '../../memory/beliefs.js';+import {+ FORGET_THAT_RE,+ type BeliefCommandDeps,+ handleBeliefCommand,+ formatCounts,+ relativeTime,+} from './memory-commands.js';+import { handleChatCommand } from './chat-command.js';++/**+ * Owner Model (ADR-026) chat surface: /memory learned|beliefs|contested,+ * confirm / forget / correct / settle by list number, /forget <text>, and the+ * plain "forget that" after a `☿ Noted:` line.+ */++const sqliteAvailable = isBetterSqlite3Available();+const dirs: string[] = [];+const DAY = 86_400_000;++function make(): { store: BeliefStore; deps: BeliefCommandDeps } {+ const dir = mkdtempSync(join(tmpdir(), 'mercury-memcmd-'));+ dirs.push(dir);+ const db = new SecondBrainDB(join(dir, 'sb.db'));+ db.init();+ const store = new BeliefStore(db.connection, 'user:owner');+ store.init();+ return { store, deps: { beliefs: store, listings: new Map(), lastNoted: new Map() } };+}++/** A held preference supported from two sessions, with a human excerpt. */+function heldPreference(store: BeliefStore, statement: string, excerpt: string, at = Date.now()) {+ const r = store.apply({ op: 'create', kind: 'preference', statement, excerpt, sessionId: 's1' }, at)!;+ store.apply({ op: 'support', beliefId: r.belief.id, excerpt, sessionId: 's2' }, at + 1);+ return store.get(r.belief.id)!;+}++afterEach(() => { while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true }); });++describe.skipIf(!sqliteAvailable)('memory commands (ADR-026)', () => {+ it('/memory learned lists recent beliefs numbered, with status, confidence, age and excerpt', () => {+ const { store, deps } = make();+ heldPreference(store, 'Prefers one command over many manual steps', 'just run pandoc');+ // The store stamps updated_at with the wall clock, so look back from two days later.+ deps.now = () => Date.now() + 2 * DAY;+ const out = handleBeliefCommand(deps, 'chat-1', '/memory learned')!;+ expect(out).toMatch(/^What I learned in the last 7 days:/);+ expect(out).toMatch(/1\. \[held 0\.\d\d\] Prefers one command over many manual steps — learned 2d ago \(from: "just run pandoc"\)/);+ expect(deps.listings.get('chat-1')).toHaveLength(1);+ });++ it('/memory learned <days> narrows the window and says so when empty', () => {+ const { store, deps } = make();+ heldPreference(store, 'Likes short replies', 'keep it short');+ deps.now = () => Date.now() + 10 * DAY;+ expect(handleBeliefCommand(deps, 'c', '/memory learned 3')).toBe('Nothing learned in the last 3 days.');+ expect(handleBeliefCommand(deps, 'c', '/memory learned 30')).toContain('Likes short replies');+ expect(handleBeliefCommand(deps, 'c', '/memory learned 0')).toMatch(/^Usage/);+ });++ it('/memory confirm <n> refers to the last list printed in that chat', () => {+ const { store, deps } = make();+ const a = heldPreference(store, 'Prefers dark mode everywhere', 'dark mode please');+ handleBeliefCommand(deps, 'chat-1', '/memory learned');+ expect(handleBeliefCommand(deps, 'chat-2', '/memory confirm 1')).toMatch(/^No list to pick from/);+ expect(handleBeliefCommand(deps, 'chat-1', '/memory confirm 9')).toMatch(/between 1 and 1/);+ const out = handleBeliefCommand(deps, 'chat-1', '/memory confirm 1')!;+ expect(out).toBe('Confirmed: "Prefers dark mode everywhere". I will act on it until you say otherwise.');+ expect(store.get(a.id)!.status).toBe('confirmed');+ expect(store.get(a.id)!.ownerStated).toBe(true);+ });++ it('/memory forget <n> and /memory correct <n> <text> act on the numbered belief', () => {+ const { store, deps } = make();+ const a = heldPreference(store, 'Works mostly in Python', 'my python scripts');+ const b = heldPreference(store, 'Likes bullet lists', 'use bullets');+ const listing = handleBeliefCommand(deps, 'c', '/memory learned')!;+ const order = deps.listings.get('c')!;+ expect(listing).toContain('1. ');+ const nOf = (id: string) => order.indexOf(id) + 1;+ expect(handleBeliefCommand(deps, 'c', `/memory forget ${nOf(b.id)}`)).toBe('Forgotten: "Likes bullet lists".');+ expect(store.get(b.id)!.status).toBe('forgotten');+ const corrected = handleBeliefCommand(deps, 'c', `/memory correct ${nOf(a.id)} Works mostly in TypeScript`)!;+ expect(corrected).toBe('Corrected. I now hold "Works mostly in TypeScript" instead of "Works mostly in Python".');+ const old = store.get(a.id)!;+ expect(old.status).toBe('retired');+ const replacement = store.get(old.supersededBy!)!;+ expect(replacement.status).toBe('confirmed');+ expect(replacement.ownerStated).toBe(true);+ });++ it('/forget <text> forgets the best-matching live belief and confirms back', () => {+ const { store, deps } = make();+ heldPreference(store, 'Prefers dark mode everywhere', 'dark mode');+ const keep = heldPreference(store, 'Works at Cosmic Stack', 'cosmic stack');+ expect(handleBeliefCommand(deps, 'c', '/forget dark mode')).toBe('Forgotten: "Prefers dark mode everywhere".');+ expect(store.list().map((b) => b.id)).toEqual([keep.id]);+ expect(handleBeliefCommand(deps, 'c', '/forget dark mode')).toBe('I hold nothing matching "dark mode".');+ expect(handleBeliefCommand(deps, 'c', '/forget')).toMatch(/^Usage/);+ });++ it('"forget that" right after a ☿ Noted: line forgets the noted belief, once', () => {+ const { store, deps } = make();+ const noted = heldPreference(store, 'Wants Mercury to just run things in this repo', 'stop asking');+ deps.lastNoted!.set('tg:1', noted.id);+ expect(FORGET_THAT_RE.test("Don't remember that.")).toBe(true);+ expect(handleBeliefCommand(deps, 'tg:1', 'forget that')).toBe('Forgotten: "Wants Mercury to just run things in this repo".');+ expect(store.get(noted.id)!.status).toBe('forgotten');+ expect(deps.lastNoted!.has('tg:1')).toBe(false);+ // Nothing noted (or already undone): not our message — the model answers.+ expect(handleBeliefCommand(deps, 'tg:1', 'forget that')).toBeNull();+ expect(handleBeliefCommand(deps, 'tg:2', 'undo that')).toBeNull();+ });++ it('/memory beliefs groups traits by dimension and numbers everything for confirm/forget', () => {+ const { store, deps } = make();+ const t = store.apply({ op: 'owner_stated', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Wants short replies', excerpt: 'shorter', sessionId: 's1' })!;+ heldPreference(store, 'Always asks before publishing', 'ask me first');+ const out = handleBeliefCommand(deps, 'c', '/memory beliefs')!;+ expect(out).toContain('How you work:');+ expect(out).toContain('brevity: terse / balanced / thorough');+ expect(out).toMatch(/1\. \[confirmed 0\.\d\d\] terse — everywhere: Wants short replies/);+ expect(out).toContain('Facts and preferences:');+ expect(out).toMatch(/2\. \[held 0\.\d\d\] Always asks before publishing/);+ expect(deps.listings.get('c')![0]).toBe(t.belief.id);+ });++ it('/memory contested shows pairs and /memory settle <n> a|b confirms one and retires the other', () => {+ const { store, deps } = make();+ const now = Date.now();+ // A held trait, then a disagreeing level from another session: both become contested.+ const a = store.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Just runs things', excerpt: 'go', sessionId: 's1' }, now)!;+ store.apply({ op: 'support', beliefId: a.belief.id, excerpt: 'go on', sessionId: 's2' }, now + 1);+ const b = store.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'ask-first', statement: 'Wants to be asked first', excerpt: 'ask me', sessionId: 's3' }, now + 2)!;+ expect(store.get(a.belief.id)!.status).toBe('contested');+ const out = handleBeliefCommand(deps, 'c', '/memory contested')!;+ expect(out).toContain('a) ');+ expect(out).toContain('b) ');+ expect(out).toContain('/memory settle <n> a|b');+ expect(handleBeliefCommand(deps, 'c', '/memory settle 1 x')).toMatch(/^Usage/);+ const settled = handleBeliefCommand(deps, 'c', '/memory settle 1 b')!;+ expect(settled).toMatch(/^Settled: ".+" stands; the other is retired\.$/);+ const statuses = [store.get(a.belief.id)!.status, store.get(b.belief.id)!.status].sort();+ expect(statuses).toEqual(['confirmed', 'retired']);+ expect(handleBeliefCommand(deps, 'c', '/memory contested')).toBe('Nothing is contested right now.');+ });++ it('unknown /memory subcommands and bare /memory are not ours', () => {+ const { deps } = make();+ expect(handleBeliefCommand(deps, 'c', '/memory')).toBeNull();+ expect(handleBeliefCommand(deps, 'c', '/memory whatever')).toBeNull();+ expect(handleBeliefCommand(deps, 'c', 'hello')).toBeNull();+ expect(handleBeliefCommand(deps, 'c', '/memory help')).toContain('/memory learned [days]');+ });++ it('formats the honest counts and relative times', () => {+ expect(formatCounts({ candidate: 5, held: 12, confirmed: 3, contested: 1, retired: 2, forgotten: 9 }))+ .toBe('Beliefs: 12 held, 3 confirmed, 5 candidate, 1 contested, 2 retired');+ const now = Date.now();+ expect(relativeTime(now, now)).toBe('just now');+ expect(relativeTime(now - 5 * 60_000, now)).toBe('5m ago');+ expect(relativeTime(now - 3 * 3_600_000, now)).toBe('3h ago');+ expect(relativeTime(now - 2 * DAY, now)).toBe('2d ago');+ });++ it('the chat dispatcher routes /memory learned, /forget and "forget that" and sends plain text', async () => {+ const { store, deps } = make();+ const noted = heldPreference(store, 'Prefers one command over many manual steps', 'just run pandoc');+ const sent: string[] = [];+ const channel = { send: async (text: string) => { sent.push(text); } };+ const agent = {+ channels: { get: () => channel },+ capabilities: { getChatCommandContext: () => ({ config: () => ({ memory: {} }) }) },+ userMemory: { beliefs: store },+ lastBeliefListing: deps.listings,+ lastNotedBeliefId: new Map<string, string>([['tg:7', noted.id]]),+ } as unknown as import('../agent.js').Agent;++ expect(await handleChatCommand(agent, '/memory learned', 'telegram', 'tg:7')).toBe(true);+ expect(sent.at(-1)).toContain('1. [held ');+ expect(sent.at(-1)).not.toContain('**');+ expect(await handleChatCommand(agent, '/memory confirm 1', 'telegram', 'tg:7')).toBe(true);+ expect(sent.at(-1)).toMatch(/^Confirmed: /);+ expect(await handleChatCommand(agent, 'forget that', 'telegram', 'tg:7')).toBe(true);+ expect(sent.at(-1)).toBe('Forgotten: "Prefers one command over many manual steps".');+ // Nothing left to undo: fall through to the model.+ expect(await handleChatCommand(agent, 'forget that', 'telegram', 'tg:7')).toBe(false);+ expect(await handleChatCommand(agent, '/forget pandoc', 'telegram', 'tg:7')).toBe(true);+ expect(sent.at(-1)).toBe('I hold nothing matching "pandoc".');+ expect(await handleChatCommand(agent, '/memory bogus', 'telegram', 'tg:7')).toBe(true);+ expect(sent.at(-1)).toContain('Memory commands:');+ });+});diff --git a/src/core/commands/memory-commands.ts b/src/core/commands/memory-commands.tsnew file mode 100644index 00000000..c4ad5222--- /dev/null+++ b/src/core/commands/memory-commands.ts@@ -0,0 +1,302 @@+/**+ * The owner-facing side of the Owner Model (ADR-026, docs/owner-model.md §3.6):+ * what Mercury learned, what it believes, what is contested, and the verbs+ * the owner uses to confirm / correct / forget — as plain text for any chat.+ *+ * Everything here is pure text in, text out, so the CLI, Telegram, Signal,+ * Discord and the menus all share one implementation. Numbers in+ * `/memory confirm <n>` refer to the LAST list printed in that chat; the+ * caller keeps that list in `listings` (one entry per chat).+ */+import type { Belief, BeliefStore, BeliefStatus } from '../../memory/beliefs.js';+import { TRAIT_DIMENSIONS, SCOPED_DIMENSIONS } from '../../memory/beliefs.js';++const DAY = 86_400_000;+export const DEFAULT_LEARNED_DAYS = 7;++/** Plain-language undo right after a `☿ Noted:` line. */+export const FORGET_THAT_RE = /^(forget that|don'?t remember that|undo that)[.!]?$/i;++export interface BeliefCommandDeps {+ beliefs: BeliefStore;+ /** Per-chat numbering of the last printed list: index → belief id, or `a|b` for a contested pair. */+ listings: Map<string, string[]>;+ /** Per-chat id of the belief announced by the last `☿ Noted:` line. */+ lastNoted?: Map<string, string>;+ now?: () => number;+}++// ── formatting ───────────────────────────────────────────────────────────++export function relativeTime(at: number, now = Date.now()): string {+ const diff = Math.max(0, now - at);+ if (diff < 60_000) return 'just now';+ if (diff < 3_600_000) return `${Math.round(diff / 60_000)}m ago`;+ if (diff < DAY) return `${Math.round(diff / 3_600_000)}h ago`;+ if (diff < 30 * DAY) return `${Math.round(diff / DAY)}d ago`;+ return `${Math.round(diff / (30 * DAY))}mo ago`;+}++function tag(store: BeliefStore, b: Belief, now: number): string {+ return `[${b.status} ${store.confidence(b, now).toFixed(2)}]`;+}++function excerptFor(store: BeliefStore, b: Belief): string {+ const ev = store.evidenceFor(b.id, 3).find((e) => e.kind !== 'migration' && e.kind !== 'acted-on-ok' && e.kind !== 'reflection' && e.excerpt.length > 0);+ if (!ev) return '';+ const text = ev.excerpt.replace(/\s+/g, ' ').trim();+ return text.length > 80 ? `${text.slice(0, 77)}…` : text;+}++/** `1. [held 0.72] Prefers one command — learned 2d ago (from: "just run pandoc")` */+export function formatLearnedLine(store: BeliefStore, n: number, b: Belief, now: number): string {+ const excerpt = excerptFor(store, b);+ const from = excerpt ? ` (from: "${excerpt}")` : '';+ const verb = b.status === 'confirmed' && b.ownerStated ? 'stated' : 'learned';+ return `${n}. ${tag(store, b, now)} ${b.statement} — ${verb} ${relativeTime(b.updatedAt, now)}${from}`;+}++/** `Beliefs: 12 held, 3 confirmed, 5 candidate, 1 contested, 2 retired` */+export function formatCounts(counts: Record<BeliefStatus, number>): string {+ return `Beliefs: ${counts.held} held, ${counts.confirmed} confirmed, ${counts.candidate} candidate, ${counts.contested} contested, ${counts.retired} retired`;+}++function dimensionLabel(dimension: string): string {+ const [base, scope] = dimension.split(':', 2);+ const levels = (TRAIT_DIMENSIONS as Record<string, readonly string[]>)[base] ?? (SCOPED_DIMENSIONS as Record<string, readonly string[]>)[base];+ const name = scope ? `${base} (${scope})` : base;+ return levels ? `${name}: ${levels.join(' / ')}` : name;+}++function contextLabel(context: string): string {+ return context === '*' ? 'everywhere' : context;+}++// ── views ────────────────────────────────────────────────────────────────++/** The "what I learned" view: a numbered list the owner can act on. */+export function renderLearned(deps: BeliefCommandDeps, channelId: string, days = DEFAULT_LEARNED_DAYS, limit = 20): string {+ const now = deps.now?.() ?? Date.now();+ const recent = deps.beliefs.recentlyLearned(now - days * DAY, limit);+ if (recent.length === 0) {+ deps.listings.delete(channelId);+ return `Nothing learned in the last ${days} day${days === 1 ? '' : 's'}.`;+ }+ const lines = [`What I learned in the last ${days} day${days === 1 ? '' : 's'}:`, ''];+ recent.forEach((b, i) => lines.push(formatLearnedLine(deps.beliefs, i + 1, b, now)));+ deps.listings.set(channelId, recent.map((b) => b.id));+ lines.push('', 'Reply /memory confirm <n>, /memory forget <n> or /memory correct <n> <what is true>.');+ return lines.join('\n');+}++/** The "what I believe about you" view: traits by dimension, then confirmed and held preferences. */+export function renderBeliefs(deps: BeliefCommandDeps, channelId: string): string {+ const now = deps.now?.() ?? Date.now();+ const live = deps.beliefs.list({ statuses: ['held', 'confirmed', 'contested'] });+ const traits = live.filter((b) => b.kind === 'trait' && b.dimension);+ const prefs = live.filter((b) => b.kind !== 'trait')+ .sort((a, b) => Number(b.status === 'confirmed') - Number(a.status === 'confirmed') || deps.beliefs.confidence(b, now) - deps.beliefs.confidence(a, now));+ if (traits.length === 0 && prefs.length === 0) {+ deps.listings.delete(channelId);+ return 'I hold no beliefs about you yet. They form as we work together; say /memory learned to watch them arrive.';+ }+ const ids: string[] = [];+ const lines: string[] = ['What I believe about you:'];+ if (traits.length > 0) {+ const byDimension = new Map<string, Belief[]>();+ for (const t of traits) {+ const list = byDimension.get(t.dimension!) ?? [];+ list.push(t);+ byDimension.set(t.dimension!, list);+ }+ lines.push('', 'How you work:');+ for (const [dimension, list] of [...byDimension.entries()].sort(([a], [b]) => a.localeCompare(b))) {+ lines.push(` ${dimensionLabel(dimension)}`);+ for (const t of list.sort((a, b) => deps.beliefs.confidence(b, now) - deps.beliefs.confidence(a, now))) {+ ids.push(t.id);+ lines.push(` ${ids.length}. ${tag(deps.beliefs, t, now)} ${t.level} — ${contextLabel(t.context)}${t.statement && t.statement !== t.level ? `: ${t.statement}` : ''}`);+ }+ }+ }+ if (prefs.length > 0) {+ lines.push('', 'Facts and preferences:');+ for (const p of prefs.slice(0, 40)) {+ ids.push(p.id);+ const ctx = p.context === '*' ? '' : ` (${p.context})`;+ lines.push(` ${ids.length}. ${tag(deps.beliefs, p, now)} ${p.statement}${ctx}`);+ }+ }+ deps.listings.set(channelId, ids);+ lines.push('', 'Reply /memory confirm <n>, /memory forget <n> or /memory correct <n> <what is true>.');+ return lines.join('\n');+}++/** Contested pairs the owner can settle. */+export function renderContested(deps: BeliefCommandDeps, channelId: string): string {+ const now = deps.now?.() ?? Date.now();+ const pairs = deps.beliefs.contested();+ if (pairs.length === 0) {+ deps.listings.delete(channelId);+ return 'Nothing is contested right now.';+ }+ const ids: string[] = [];+ const lines = ['Contested — the evidence points both ways:', ''];+ pairs.forEach(({ a, b }, i) => {+ ids.push(b ? `${a.id}|${b.id}` : a.id);+ const where = a.dimension ? `${dimensionLabel(a.dimension).split(':')[0]}, ${contextLabel(a.context)}` : contextLabel(a.context);+ lines.push(`${i + 1}. ${where}`);+ lines.push(` a) ${tag(deps.beliefs, a, now)} ${a.statement}`);+ if (b) lines.push(` b) ${tag(deps.beliefs, b, now)} ${b.statement}`);+ else lines.push(' b) neither — the evidence against it is from you');+ });+ deps.listings.set(channelId, ids);+ lines.push('', 'Reply /memory settle <n> a|b to pick the one that is true, or /memory forget <n> to drop it.');+ return lines.join('\n');+}++// ── verbs ────────────────────────────────────────────────────────────────++function pick(deps: BeliefCommandDeps, channelId: string, nText: string): { ids: string[]; n: number } | string {+ const listing = deps.listings.get(channelId);+ if (!listing || listing.length === 0) return 'No list to pick from. Say /memory learned, /memory beliefs or /memory contested first.';+ const n = Number.parseInt(nText, 10);+ if (!Number.isInteger(n) || n < 1 || n > listing.length) return `Pick a number between 1 and ${listing.length} from the last list.`;+ return { ids: listing[n - 1].split('|'), n };+}++function quote(s: string): string {+ return `"${s.replace(/\s+/g, ' ').trim()}"`;+}++export function confirmNth(deps: BeliefCommandDeps, channelId: string, nText: string): string {+ const picked = pick(deps, channelId, nText);+ if (typeof picked === 'string') return picked;+ const b = deps.beliefs.confirm(picked.ids[0], deps.now?.());+ return b ? `Confirmed: ${quote(b.statement)}. I will act on it until you say otherwise.` : 'That belief is gone already.';+}++export function forgetNth(deps: BeliefCommandDeps, channelId: string, nText: string): string {+ const picked = pick(deps, channelId, nText);+ if (typeof picked === 'string') return picked;+ const forgotten: string[] = [];+ for (const id of picked.ids) {+ const b = deps.beliefs.get(id);+ if (b && b.status !== 'forgotten' && deps.beliefs.forget(id, deps.now?.())) forgotten.push(b.statement);+ }+ if (forgotten.length === 0) return 'That belief is gone already.';+ return `Forgotten: ${forgotten.map(quote).join(' and ')}.`;+}++export function correctNth(deps: BeliefCommandDeps, channelId: string, nText: string, statement: string): string {+ const picked = pick(deps, channelId, nText);+ if (typeof picked === 'string') return picked;+ const text = statement.trim();+ if (text.length < 3) return 'Usage: /memory correct <n> <what is actually true>';+ const target = deps.beliefs.get(picked.ids[0]);+ if (!target || target.status === 'forgotten') return 'That belief is gone already.';+ const result = deps.beliefs.apply({ op: 'correction', beliefId: target.id, statement: text, excerpt: text, evidenceKind: 'owner' }, deps.now?.());+ if (!result) return 'I could not apply that correction.';+ return `Corrected. I now hold ${quote(result.belief.statement)} instead of ${quote(target.statement)}.`;+}++export function settleNth(deps: BeliefCommandDeps, channelId: string, nText: string, side: string): string {+ const picked = pick(deps, channelId, nText);+ if (typeof picked === 'string') return picked;+ const which = side.trim().toLowerCase();+ if (which !== 'a' && which !== 'b') return 'Usage: /memory settle <n> a|b';+ const [aId, bId] = picked.ids;+ if (!bId) {+ // A lone contested belief: "a" keeps it, "b" drops it.+ if (which === 'a') return confirmById(deps, aId);+ const a = deps.beliefs.get(aId);+ return a && deps.beliefs.forget(aId, deps.now?.()) ? `Forgotten: ${quote(a.statement)}.` : 'That belief is gone already.';+ }+ const winnerId = which === 'a' ? aId : bId;+ const loserId = which === 'a' ? bId : aId;+ const winner = deps.beliefs.get(winnerId);+ if (!winner || winner.status === 'forgotten') return 'That belief is gone already.';+ deps.beliefs.settle(winnerId, loserId, deps.now?.());+ return `Settled: ${quote(winner.statement)} stands; the other is retired.`;+}++function confirmById(deps: BeliefCommandDeps, id: string): string {+ const b = deps.beliefs.confirm(id, deps.now?.());+ return b ? `Confirmed: ${quote(b.statement)}. I will act on it until you say otherwise.` : 'That belief is gone already.';+}++/** Best live match for free text (the `/forget <text>` and `forget_memory` paths). */+export function findBeliefByText(store: BeliefStore, text: string): Belief | null {+ const live = (b: Belief) => b.status !== 'forgotten' && b.status !== 'retired';+ const hits = store.findSimilar(text, undefined, 8).filter(live);+ if (hits.length > 0) return hits[0];+ // FTS needs a word of 4+ letters; fall back to a substring scan for short queries.+ const needle = text.toLowerCase().trim();+ if (needle.length < 2) return null;+ return store.list({ limit: 500 }).find((b) => b.statement.toLowerCase().includes(needle)) ?? null;+}++export function forgetByText(deps: BeliefCommandDeps, text: string): string {+ const query = text.trim();+ if (!query) return 'Usage: /forget <what to forget>';+ const b = findBeliefByText(deps.beliefs, query);+ if (!b) return `I hold nothing matching ${quote(query)}.`;+ deps.beliefs.forget(b.id, deps.now?.());+ return `Forgotten: ${quote(b.statement)}.`;+}++/** "forget that" right after a `☿ Noted:` line. Null when there is nothing to undo. */+export function forgetLastNoted(deps: BeliefCommandDeps, channelId: string): string | null {+ const id = deps.lastNoted?.get(channelId);+ if (!id) return null;+ deps.lastNoted!.delete(channelId);+ const b = deps.beliefs.get(id);+ if (!b || b.status === 'forgotten') return 'Already forgotten.';+ deps.beliefs.forget(id, deps.now?.());+ return `Forgotten: ${quote(b.statement)}.`;+}++// ── dispatch ─────────────────────────────────────────────────────────────++export const MEMORY_SUBCOMMAND_HELP = [+ 'Memory commands:',+ ' /memory learned [days] what I learned recently',+ ' /memory beliefs what I believe about you, with confidence',+ ' /memory contested beliefs the evidence disputes',+ ' /memory confirm <n> yes, that is right',+ ' /memory forget <n> drop it for good',+ ' /memory correct <n> <text> replace it with what is true',+ ' /memory settle <n> a|b pick the side of a contested pair',+ ' /forget <text> forget the belief that best matches',+].join('\n');++/**+ * Handle one chat message if it is a belief command. Returns the reply, or+ * null when the message is not one of ours (the caller falls through).+ */+export function handleBeliefCommand(deps: BeliefCommandDeps, channelId: string, content: string): string | null {+ const trimmed = content.trim();+ const lower = trimmed.toLowerCase();++ if (FORGET_THAT_RE.test(trimmed)) return forgetLastNoted(deps, channelId);++ if (lower === '/forget' || lower.startsWith('/forget ')) return forgetByText(deps, trimmed.slice('/forget'.length));++ if (!lower.startsWith('/memory ')) return null;+ const rest = trimmed.slice('/memory'.length).trim();+ const [sub = '', ...args] = rest.split(/\s+/);+ switch (sub.toLowerCase()) {+ case 'learned': {+ const days = args[0] ? Number.parseInt(args[0], 10) : DEFAULT_LEARNED_DAYS;+ if (!Number.isInteger(days) || days < 1 || days > 365) return 'Usage: /memory learned [days 1-365]';+ return renderLearned(deps, channelId, days);+ }+ case 'beliefs': case 'believe': case 'believes': return renderBeliefs(deps, channelId);+ case 'contested': return renderContested(deps, channelId);+ case 'confirm': return args[0] ? confirmNth(deps, channelId, args[0]) : 'Usage: /memory confirm <n>';+ case 'forget': return args[0] ? forgetNth(deps, channelId, args[0]) : 'Usage: /memory forget <n>';+ case 'correct': return args[0] ? correctNth(deps, channelId, args[0], args.slice(1).join(' ')) : 'Usage: /memory correct <n> <what is true>';+ case 'settle': return args[0] && args[1] ? settleNth(deps, channelId, args[0], args[1]) : 'Usage: /memory settle <n> a|b';+ case 'help': return MEMORY_SUBCOMMAND_HELP;+ default: return null;+ }+}diff --git a/src/core/provider-status.ts b/src/core/provider-status.tsindex 3affb86c..a9cea8ff 100644--- a/src/core/provider-status.ts+++ b/src/core/provider-status.ts@@ -1,8 +1,25 @@import type { Channel } from '../channels/base.js';import { CLIChannel } from '../channels/cli.js';+/**+ * The runtime's status publisher (attached terminals, dashboard). Set once at+ * boot; called whenever the provider changes so the bottom bar updates at+ * once instead of on the next poll.+ */+let publishStatusFn: (() => void) | null = null;++export function setStatusPublisher(fn: () => void): void {+ publishStatusFn = fn;+}++export function publishStatus(): void {+ try { publishStatusFn?.(); } catch { /* status is best effort */ }+}+export function updateCliProviderStatus(channel: Channel | undefined, providerName: string, model: string): void {- if (!(channel instanceof CLIChannel)) return;- const label = providerName === 'mercuryCloud' ? 'Mercury Cloud' : providerName;- channel.setProvider(label, model);+ if (channel instanceof CLIChannel) {+ const label = providerName === 'mercuryCloud' ? 'Mercury Cloud' : providerName;+ channel.setProvider(label, model);+ }+ publishStatus();}diff --git a/src/core/system-prompt.test.ts b/src/core/system-prompt.test.tsindex d0169db8..ee4e09b7 100644--- a/src/core/system-prompt.test.ts+++ b/src/core/system-prompt.test.ts@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest';-import { budgetContext, buildSystemPrompt, environmentSection, type SystemPromptInputs } from './system-prompt.js';+import { budgetContext, buildSystemPrompt, environmentSection, ownerSection, type SystemPromptInputs } from './system-prompt.js';const base: SystemPromptInputs = {identityPrompt: 'You are Mercury.',@@ -64,8 +64,43 @@ describe('system prompt assembly', () => {expect(p).toContain("The user's GitHub username is hotheadhacker.");});+ it('plain chat gets the work contract; Mercury Code keeps its own', () => {+ const chat = buildSystemPrompt(base);+ expect(chat).toContain('**How you work:**');+ const code = buildSystemPrompt({ ...base, programmingSuffix: '\n\nCODE' });+ expect(code).not.toContain('**How you work:**');+ });++ it('steers bulk and conversion work to real tools, not file-by-file', () => {+ const p = buildSystemPrompt(base);+ expect(p).toContain('pandoc');+ expect(p).toContain('Never read and rewrite many files one by one yourself.');+ expect(p).not.toContain('Do NOT create one-time-use helper scripts');+ });+it('includes skills, bots and mode suffixes verbatim', () => {const p = buildSystemPrompt({ ...base, skillContext: 'SKILLS: tweet-notifier', botSection: '\n\nBOTS', programmingSuffix: '\n\nCODE', researchSuffix: '\n\nRESEARCH' });for (const s of ['SKILLS: tweet-notifier', 'BOTS', 'CODE', 'RESEARCH', 'Tweet Notification System Available']) expect(p).toContain(s);});++ it('places the owner brief right after the identity and before skills; omits it when empty', () => {+ const sections = { about: 'Facts:\n- Founder of Cosmic Stack', howToWork: '- Keep replies short; lead with the result.' };+ const p = buildSystemPrompt({ ...base, skillContext: 'SKILLS: x', ownerSections: sections, ownerName: 'Salman' });+ const identity = p.indexOf('You are Mercury.');+ const about = p.indexOf('## About Salman');+ const how = p.indexOf('## How to work with Salman');+ const skills = p.indexOf('SKILLS: x');+ expect(identity).toBe(0);+ expect(about).toBeGreaterThan(identity);+ expect(how).toBeGreaterThan(about);+ expect(skills).toBeGreaterThan(how);+ expect(p).toContain('- Founder of Cosmic Stack');+ expect(p).toContain('- Keep replies short; lead with the result.');+ // Nothing held yet: no heading at all, so the model has nothing to embroider.+ expect(ownerSection(null, 'Salman')).toBe('');+ expect(ownerSection({ about: '', howToWork: ' ' }, 'Salman')).toBe('');+ expect(buildSystemPrompt({ ...base, ownerSections: null })).not.toContain('## About');+ // A name is always rendered, even when the config has none.+ expect(ownerSection({ about: 'x', howToWork: '' })).toContain('## About the owner');+ });});diff --git a/src/core/system-prompt.ts b/src/core/system-prompt.tsindex 54f9bcc5..1ff29845 100644--- a/src/core/system-prompt.ts+++ b/src/core/system-prompt.ts@@ -24,6 +24,14 @@ export interface SystemPromptInputs {timezone: string;/** Second Brain summary, or null when Second Brain is disabled. */memorySummary: { total: number; learningPaused: boolean } | null;+ /**+ * The owner brief (ADR-026): "About {owner}" and "How to work with {owner}",+ * built from the belief store and regenerated only when beliefs change so+ * the cached prefix holds. Null/undefined when nothing is held yet.+ */+ ownerSections?: { about: string; howToWork: string } | null;+ /** The owner's name for the brief headings; defaults to "the owner". */+ ownerName?: string;toolNames: readonly string[];github: { defaultOwner?: string; defaultRepo?: string; username?: string };}@@ -31,11 +39,38 @@ export interface SystemPromptInputs {export const GITHUB_TOOL_NAMES = ['create_pr', 'review_pr', 'list_issues', 'create_issue', 'github_api'] as const;export const TOOL_USAGE_GUIDELINES = `**Tool Usage Guidelines:**-- Use write_file, create_file, and edit_file tools DIRECTLY to create and modify files. Do NOT create intermediary scripts (Python, bash, Node.js) whose sole purpose is to generate other files — you have native file tools for this.-- Use run_command for: building, testing, installing dependencies, running the project, git operations, and other system tasks that require a shell.-- Do NOT use run_command with echo/cat/tee/heredoc to write files. Use write_file or create_file instead.-- Do NOT create one-time-use helper scripts. If the user asks you to create a file, create it directly with create_file or write_file.-- When creating multiple files, call create_file or write_file for each one individually. Do not batch them into a script.`;+- Create or change a single file with write_file, create_file or edit_file. Never write a file through run_command with echo/cat/tee/heredoc, and never write a generator script just to emit one file you could write directly.+- Use run_command for building, testing, installing dependencies, running the project, git, and for real work on data: converting formats, processing many files, extracting, transforming, downloading.+- Pick the strongest tool for the job before doing anything by hand. For conversions and bulk work (EPUB/PDF/DOCX/HTML to Markdown, images, audio/video, CSV/JSON, renaming or reorganising many files) use an established CLI or library — pandoc, ffmpeg, ImageMagick, jq, a Python package such as ebooklib, markdownify or pypdf — through run_command, or one short script that handles every file in one run. Never read and rewrite many files one by one yourself.+- Not sure what the best tool is? Spend one web_search finding out. Check it is installed (e.g. \`command -v pandoc\`, \`python3 -c "import ebooklib"\`) and install it or fall back to the next best option.+- After a bulk or conversion step, check the output (count the files, open a sample) before saying it is done.`;++/**+ * How to work in plain chat (programming mode off). Mercury Code has its+ * own contract (programming-mode.ts); without this, chat answered tasks as+ * a narrator: no plan, the slowest tool, and no check of the result.+ * Stable text: it is part of the cached prompt prefix.+ */+export const CHAT_WORK_CONTRACT = `**How you work:**+- When the user asks for something to be done, do it with your tools; don't describe how they could do it.+- First work out the end result they want, then the fastest reliable way to get there (an existing command, library or service beats manual steps).+- For tasks with several steps, keep a short plan with update_plan and work through it.+- Report what you did and what you checked, briefly. If something failed or you could not confirm it, say so plainly.+- Plain questions and conversation don't need tools: just answer.`;++/**+ * Self-made skills (ADR-027): shown only when create_skill is available, i.e.+ * in the owner's own interactive sessions, never for bots or members.+ */+export const SELF_MADE_SKILLS_GUIDE = `**Skills you make yourself:**+- Before a task with many steps, check the skills list for one that fits and use it.+- If none fits and you would do the same procedure again for other inputs (a recurring report, a conversion pipeline, a deploy, a site or device you operate), plan it as a procedure first: inputs, steps, checks. Do the task. Once it actually worked, save the procedure with create_skill — exact commands, tools, paths, the pitfalls you hit — and tell the owner in one line.+- Never make a skill for a one-off, for something one tool call does, for anything you did not verify end-to-end, or with credentials inside. Improve an existing self-made skill (same name) rather than adding a near-duplicate.+- Self-made skills are listed under "Self-made" and count as unverified until used again: follow them, but trust the result, not the text, and fix the skill when a step fails.`;++export function selfMadeSkillsSection(toolNames: readonly string[]): string {+ return toolNames.includes('create_skill') ? SELF_MADE_SKILLS_GUIDE : '';+}/*** Token budget line for the per-turn (volatile) system block. It changes on@@ -75,6 +110,23 @@ export function memorySection(summary: SystemPromptInputs['memorySummary']): strreturn text;}+/**+ * What Mercury believes about its owner and how to work with them, right+ * after the identity so it reads as part of who Mercury is. Empty when there+ * is nothing held: a bare heading would only invite the model to invent.+ */+export function ownerSection(sections: SystemPromptInputs['ownerSections'], ownerName = 'the owner'): string {+ if (!sections) return '';+ const about = sections.about.trim();+ const how = sections.howToWork.trim();+ if (!about && !how) return '';+ const name = ownerName.trim() || 'the owner';+ const parts: string[] = [];+ if (about) parts.push(`## About ${name}\n\nWhat you know about ${name} from past conversations (stated facts are plain; "seems" marks what you inferred and have not confirmed):\n${about}`);+ if (how) parts.push(`## How to work with ${name}\n\nLearned from how ${name} works with you; follow these unless ${name} says otherwise in the moment:\n${how}`);+ return parts.join('\n\n');+}+export function tweetNotifierSection(skillContext: string): string {if (!skillContext.includes('tweet-notifier')) return '';return `**Tweet Notification System Available** — The tweet-notifier skill is installed.@@ -113,14 +165,19 @@ Always specify owner and repo parameters on GitHub tools. The user's GitHub user/** Assemble the stable system prompt. Section order is part of the cache key; keep it stable. */export function buildSystemPrompt(i: SystemPromptInputs): string {const sections: string[] = [i.identityPrompt];+ const owner = ownerSection(i.ownerSections, i.ownerName);+ if (owner) sections.push(owner);if (i.skillContext) sections.push(i.skillContext);let prompt = sections.join('\n\n');if (i.botSection) prompt += i.botSection;if (i.programmingSuffix) prompt += i.programmingSuffix;+ else prompt += '\n\n' + CHAT_WORK_CONTRACT;if (i.researchSuffix) prompt += i.researchSuffix;if (i.saverSuffix) prompt += i.saverSuffix;prompt += '\n\n' + environmentSection(i.now, i.timezone, i.platform, i.cwd);prompt += '\n\n' + TOOL_USAGE_GUIDELINES;+ const selfMade = selfMadeSkillsSection(i.toolNames);+ if (selfMade) prompt += '\n\n' + selfMade;prompt += '\n\n' + memorySection(i.memorySummary);const tweet = tweetNotifierSection(i.skillContext);if (tweet) prompt += '\n\n' + tweet;diff --git a/src/index.ts b/src/index.tsindex 9fa719f7..1df61b79 100644--- a/src/index.ts+++ b/src/index.ts@@ -3,6 +3,8 @@ import { createHash } from 'node:crypto';import { fileURLToPath } from 'node:url';import { dirname, join, resolve as pathResolve } from 'node:path';import { Command } from 'commander';+import { publishStatus, setStatusPublisher } from './core/provider-status.js';+import { loadPermissionMode, savePermissionMode } from './capabilities/permission-mode-store.js';import chalk from 'chalk';import {@@ -89,7 +91,7 @@ import { selectWithArrowKeys } from './utils/arrow-select.js';import { ProviderModelFetchError, fetchProviderModelCatalog } from './utils/provider-models.js';import { initCloudTokenStore } from './cloud/token-store.js';import { clearCloudRuntimeOnline, markCloudRuntimeOnline } from './cloud/runtime-status.js';-import { startWebServer, stopWebServer, updateStatus as updateWebStatus, setUserMemory as setWebUserMemory, setWebChannel as setWebWebChannel, setScheduler as setWebScheduler, setAgentSupervisor as setWebSupervisor, setBackgroundTaskManager as setWebBgTasks, setSpotifyClient as setWebSpotify, setProgrammingMode as setWebProgrammingMode, setModelSwitchCallback as setWebModelSwitch, setCurrentProviderCallback as setWebCurrentProvider, setKanbanSupervisor as setWebKanban, setKanbanBoardManager as setWebBoardManager, setKanbanProviders as setWebKanbanProviders, setIDEProviders as setWebIDEProviders, setSessionRepository as setWebSessions, setSessionSyncEnabledCallback as setWebSessionSyncEnabled, setBotManager as setWebBotManager, setBotsWebhookSecret, setShutdownHandler } from './web/server.js';+import { startWebServer, stopWebServer, updateStatus as updateWebStatus, setUserMemory as setWebUserMemory, setWebChannel as setWebWebChannel, setScheduler as setWebScheduler, setAgentSupervisor as setWebSupervisor, setBackgroundTaskManager as setWebBgTasks, setSpotifyClient as setWebSpotify, setProgrammingMode as setWebProgrammingMode, setModelSwitchCallback as setWebModelSwitch, setCurrentProviderCallback as setWebCurrentProvider, setStatusSnapshotCallback as setWebStatusSnapshot, setKanbanSupervisor as setWebKanban, setKanbanBoardManager as setWebBoardManager, setKanbanProviders as setWebKanbanProviders, setIDEProviders as setWebIDEProviders, setSessionRepository as setWebSessions, setSessionSyncEnabledCallback as setWebSessionSyncEnabled, setBotManager as setWebBotManager, setBotsWebhookSecret, setShutdownHandler } from './web/server.js';import { isWebAuthInitialized, setWebPassword, writeAttachToken } from './web/auth.js';import { startLoopLagMonitor } from './utils/loop-lag.js';@@ -2212,6 +2214,21 @@ async function configure(existingConfig?: MercuryConfig): Promise<void> {console.log(chalk.dim(' Web dashboard disabled. You can enable it later with `mercury doctor`.'));}+ // Permission mode: asked once here and remembered by every surface+ // (terminal, attached terminal, dashboard). /permissions changes it later.+ console.log('');+ const currentMode = loadPermissionMode();+ const permissionChoice = await selectWithArrowKeys(+ currentMode ? `Permissions (now: ${currentMode === 'allow-all' ? 'Allow All' : 'Ask Me'})` : 'How should Mercury handle risky actions?',+ [+ { value: 'ask-me', label: 'Ask Me — confirm before it writes files or runs commands' },+ { value: 'allow-all', label: 'Allow All — just do it (blocked commands stay blocked)' },+ ],+ );+ const permissionMode = permissionChoice === 'allow-all' ? 'allow-all' : 'ask-me';+ savePermissionMode(permissionMode);+ console.log(chalk.dim(` ${permissionMode === 'allow-all' ? 'Allow All' : 'Ask Me'} saved · change any time with /permissions`));+saveConfig(config);const home = getMercuryHome();@@ -2547,7 +2564,14 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {const channels = new ChannelRegistry(config, { cliOnly: daemonOwnsRuntime });const webChannel = new WebChannel(config.identity.name);channels.register('web', webChannel);+ // Bits of the bottom-bar status that are pushed rather than read.+ const liveStatus = { desktop: '' };const capabilities = new CapabilityRegistry(skillLoader, scheduler, tokenBudget, undefined, userMemory ?? undefined);+ // "Always" on a shell prompt covers the command family in the owner's own+ // terminal, attached terminal and dashboard; remote Mercury Cloud requests+ // stay exact-match.+ capabilities.permissions.setFamilyApprovalPolicy((ctx) =>+ ctx.channelType === 'cli' || (ctx.channelType === 'web' && !webChannel.hasCloudEventHandler(ctx.channelId)));let supervisor: SubAgentSupervisor | undefined;if (config.subagents.enabled) {@@ -2698,6 +2722,8 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {setDesktopActivityHook((label) => {const cli = channels.get('cli');if (cli instanceof CLIChannel) cli.setDesktopActivity(label);+ liveStatus.desktop = label ?? '';+ publishStatus();});pluginManager.setRunsPlugins(true);pluginManager.setNotifier((m) => agent.notifyAllChannels(m));@@ -3453,6 +3479,31 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {setWebBgTasks(agent.backgroundTasks);setWebModelSwitch((provider, model) => agent.switchProvider(provider, model));setWebCurrentProvider(() => agent.getCurrentProvider());+ // Live bottom bar for attached terminals and the dashboard: one snapshot,+ // pushed on every model switch and checked every 2 s for anything else.+ const statusSnapshot = (): Record<string, unknown> => {+ const current = agent.getCurrentProvider();+ return {+ provider: getProviderLabel(current.name as ProviderName),+ model: current.model,+ mode: agent.programmingMode.getState(),+ permission: webChannel.getSettings().bypassPermissions ? 'allow-all' : 'ask-me',+ tokensUsed: tokenBudget.getDailyUsed(),+ tokenBudget: tokenBudget.getBudget(),+ desktop: liveStatus.desktop,+ };+ };+ setWebStatusSnapshot(statusSnapshot);+ let lastStatusKey = '';+ const pushStatus = (): void => {+ const snapshot = statusSnapshot();+ const key = JSON.stringify(snapshot);+ if (key === lastStatusKey) return;+ lastStatusKey = key;+ webChannel.broadcastStatus(snapshot);+ };+ setStatusPublisher(pushStatus);+ setInterval(pushStatus, 2000).unref();// IDE provider registry powers features like commit message generation.// It does not require a supervisor, so wire it up unconditionally.setWebIDEProviders(providers);@@ -3650,6 +3701,10 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {capabilities.permissions.setAutoApproveAll(true);capabilities.permissions.addTempScope('/', true, true);logger.info({ chatId }, 'Telegram: Allow All mode set for session');+ } else {+ // Switching back used to be ignored: Allow All stayed on until restart.+ capabilities.permissions.setAutoApproveAll(false);+ logger.info({ chatId }, 'Telegram: Ask Me mode set for session');}});}@@ -3660,6 +3715,10 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {capabilities.permissions.setAutoApproveAll(true);capabilities.permissions.addTempScope('/', true, true);logger.info('Signal: Allow All mode set for session');+ } else {+ // Switching back used to be ignored: Allow All stayed on until restart.+ capabilities.permissions.setAutoApproveAll(false);+ logger.info('Signal: Ask Me mode set for session');}});}@@ -3729,7 +3788,12 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {cliChannel.setCurrentSession(selected);}- const mode = cliChannel && await cliChannel.askPermissionMode?.();+ // Ask once, then remember (also covers Mercury Code). /permissions changes it.+ const savedMode = loadPermissionMode();+ const mode = savedMode ?? (cliChannel ? await cliChannel.askPermissionMode?.() : undefined);+ if (!savedMode && (mode === 'allow-all' || mode === 'ask-me') && process.stdout.isTTY) savePermissionMode(mode);+ if (savedMode && cliChannel) console.log(chalk.dim(` Permissions: ${savedMode === 'allow-all' ? 'Allow All' : 'Ask Me'} · /permissions to change`));+ cliChannel?.setPermissionModeStatus(mode === 'allow-all' ? 'allow-all' : 'ask-me');if (mode === 'allow-all') {capabilities.permissions.setAutoApproveAll(true);capabilities.permissions.addTempScope('/', true, true);@@ -3783,8 +3847,12 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {await agent.notifyAllChannels('⚠ Mercury is shutting down. If I was working on something, it has been interrupted. Send a message after restart to continue.');} catch { /* best effort */ }if (userMemory) {+ // One last reflection pass over the sessions of this run (ADR-026);+ // bounded so a slow provider cannot hold the shutdown.+ try {+ await Promise.race([agent.reflectNow(), new Promise<void>((resolve) => setTimeout(resolve, 20_000).unref())]);+ } catch { /* best effort */ }try {- userMemory.consolidate();userMemory.close();} catch {}}diff --git a/src/learning/announce.test.ts b/src/learning/announce.test.tsnew file mode 100644index 00000000..2d0a77a6--- /dev/null+++ b/src/learning/announce.test.ts@@ -0,0 +1,92 @@+import { describe, expect, it } from 'vitest';+import type { ApplyResult, Belief } from '../memory/beliefs.js';+import { ASK_COOLDOWN_MS, composeLearnedNote, pendingQuestion, restate } from './announce.js';++function belief(partial: Partial<Belief> & Pick<Belief, 'id' | 'statement'>): Belief {+ const now = 1_700_000_000_000;+ return {+ ownerKey: 'user:owner', kind: 'preference', dimension: null, level: null, detail: null, context: '*', support: 2, against: 0,+ supportSessions: 2, timescale: 'stable', status: 'held', ownerStated: false, validFrom: now, validTo: null, supersededBy: null,+ lastSupportedAt: now, lastUsedAt: null, createdAt: now, updatedAt: now, shareable: false, ...partial,+ };+}++function fakeStore(pairs: Array<{ a: Belief; b: Belief | null }> = []) {+ const meta = new Map<string, string>();+ return {+ meta,+ contested: () => pairs,+ getMeta: (k: string) => meta.get(k) ?? null,+ setMeta: (k: string, v: string) => { meta.set(k, v); },+ };+}++const result = (belief: Belief, change: ApplyResult['change'], behaviorChanging = true): ApplyResult => ({ belief, change, behaviorChanging });++describe('restate', () => {+ it('turns traits into second-person phrases with their context', () => {+ expect(restate(belief({ id: 'x', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Runs things without asking', context: 'project:/home/o/mercury' })))+ .toBe("you'd rather I just run things without asking in mercury");+ expect(restate(belief({ id: 'x', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Short', context: 'channel:telegram' })))+ .toBe("you'd rather I keep replies short on telegram");+ expect(restate(belief({ id: 'x', kind: 'trait', dimension: 'expertise:typescript', level: 'expert', statement: 'TS expert' }))).toBe("you're an expert in typescript");+ expect(restate(belief({ id: 'x', kind: 'trait', dimension: 'risk:publishing', level: 'cautious', statement: 'Careful' }))).toBe("you're cautious about publishing");+ });++ it('rewrites third-person statements into "you"', () => {+ expect(restate(belief({ id: 'x', statement: 'Owner prefers one command over many manual steps.' }))).toBe('you prefer one command over many manual steps');+ expect(restate(belief({ id: 'x', statement: 'The user is based in Lahore' }))).toBe('you are based in lahore'.replace('lahore', 'Lahore'));+ expect(restate(belief({ id: 'x', statement: 'Prefers tabs over spaces' }))).toBe('prefers tabs over spaces');+ });+});++describe('composeLearnedNote', () => {+ it('returns null when nothing behaviour-changing happened', () => {+ const store = fakeStore();+ const fact = belief({ id: 'f', kind: 'fact', statement: 'Lives in Lahore' });+ expect(composeLearnedNote([], { beliefs: store })).toBeNull();+ expect(composeLearnedNote([result(fact, 'created', false), null, result(fact, 'supported', false)], { beliefs: store })).toBeNull();+ // A fact is trivia even if flagged.+ expect(composeLearnedNote([result(fact, 'now-held', false)], { beliefs: store })).toBeNull();+ });++ it('writes one Noted line for a behaviour-changing result', () => {+ const store = fakeStore();+ const trait = belief({ id: 't', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'x', context: 'project:/home/o/mercury', status: 'confirmed', ownerStated: true });+ const other = belief({ id: 'p', statement: 'Owner prefers short commit messages' });+ const line = composeLearnedNote([result(trait, 'replaced'), result(other, 'now-held')], { beliefs: store });+ expect(line).toBe('☿ Noted: you\'d rather I just run things without asking in mercury. Say "forget that" to undo.');+ });++ it('asks about a contested pair at most once a day, then falls back to a note', () => {+ const a = belief({ id: 'a', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Short', status: 'contested' });+ const b = belief({ id: 'b', kind: 'trait', dimension: 'brevity', level: 'thorough', statement: 'Long', status: 'contested' });+ const store = fakeStore([{ a, b }]);+ const now = 1_700_000_000_000;+ const q = composeLearnedNote([result(b, 'contested')], { beliefs: store, now });+ expect(q).toBe("☿ I've noticed you'd rather I keep replies short but also you'd rather I answer thoroughly. Which should I go with?");+ expect(store.meta.get('asked:a')).toBe(new Date(now).toISOString());+ expect(store.meta.get('asked:b')).toBe(new Date(now).toISOString());+ // Asked again within the day: silent (no other result).+ expect(composeLearnedNote([result(b, 'contested')], { beliefs: store, now: now + 1000 })).toBeNull();+ // ...but a note for another result still goes out.+ const other = belief({ id: 'p', statement: 'Owner prefers short commit messages' });+ expect(composeLearnedNote([result(b, 'contested'), result(other, 'now-held')], { beliefs: store, now: now + 1000 })).toContain('☿ Noted: you prefer short commit messages');+ // After the cooldown it may be asked again.+ expect(composeLearnedNote([result(b, 'contested')], { beliefs: store, now: now + ASK_COOLDOWN_MS + 1 })).toContain('Which should I go with?');+ });+});++describe('pendingQuestion', () => {+ it('returns the oldest un-asked pair, preferring one asked within the last day', () => {+ const now = 1_700_000_000_000;+ const a1 = belief({ id: 'a1', statement: 'Owner likes options', updatedAt: now - 5000, status: 'contested' });+ const b1 = belief({ id: 'b1', statement: 'Owner wants a verdict', status: 'contested' });+ const a2 = belief({ id: 'a2', statement: 'Owner wants morning pings', updatedAt: now - 9000, status: 'contested' });+ const store = fakeStore([{ a: a1, b: b1 }, { a: a2, b: null }]);+ expect(pendingQuestion(store, now)).toBe("☿ I've noticed you want morning pings, but I'm no longer sure. Is that still right?");+ store.setMeta('asked:a1', new Date(now - 60_000).toISOString());+ expect(pendingQuestion(store, now)).toBe("☿ I've noticed you like options but also you want a verdict. Which should I go with?");+ expect(pendingQuestion(fakeStore(), now)).toBeNull();+ });+});diff --git a/src/learning/announce.ts b/src/learning/announce.tsnew file mode 100644index 00000000..bffec6a4--- /dev/null+++ b/src/learning/announce.ts@@ -0,0 +1,124 @@+/**+ * Telling the owner what Mercury learned (docs/owner-model.md §3.6).+ *+ * One short line at most per turn, only when a belief changes how Mercury+ * behaves; a contested pair becomes a question instead, asked at most once a+ * day. Trivia (facts, episodes, candidates) is never announced.+ */+import type { ApplyResult, Belief, BeliefStore } from '../memory/beliefs.js';+import path from 'node:path';++export const ASK_COOLDOWN_MS = 24 * 60 * 60 * 1000;+const ANNOUNCED_CHANGES: ReadonlySet<ApplyResult['change']> = new Set(['now-held', 'confirmed', 'replaced', 'contested']);++export interface AnnounceOptions {+ beliefs: Pick<BeliefStore, 'contested' | 'getMeta' | 'setMeta'>;+ now?: number;+}++const TRAIT_PHRASES: Record<string, Record<string, string>> = {+ brevity: { terse: "you'd rather I keep replies short", balanced: 'you want replies of moderate length', thorough: "you'd rather I answer thoroughly" },+ autonomy: { 'ask-first': 'you want me to ask before acting', 'ask-when-risky': 'you want me to ask only before risky actions', 'just-do-it': "you'd rather I just run things without asking" },+ 'decision-style': { options: "you'd rather see options", recommendation: 'you want a recommendation, not a menu', 'decide-for-me': "you'd rather I just decide" },+ formality: { casual: 'you prefer a casual tone', neutral: 'you prefer a neutral tone', formal: 'you prefer a formal tone' },+ 'feedback-style': { blunt: 'you correct me bluntly', explains: 'you explain when you correct me', 'silent-redo': 'you redo things rather than correct me' },+ rhythm: { morning: "you're usually around in the morning", daytime: "you're usually around during the day", evening: "you're usually around in the evening", 'late-night': "you're usually around late at night" },+};++const THIRD_TO_SECOND: Array<[RegExp, string]> = [+ [/^prefers\b/i, 'prefer'], [/^likes\b/i, 'like'], [/^dislikes\b/i, 'dislike'], [/^wants\b/i, 'want'], [/^needs\b/i, 'need'],+ [/^uses\b/i, 'use'], [/^works\b/i, 'work'], [/^is\b/i, 'are'], [/^has\b/i, 'have'], [/^does\b/i, 'do'], [/^avoids\b/i, 'avoid'],+ [/^expects\b/i, 'expect'], [/^hates\b/i, 'hate'], [/^lives\b/i, 'live'], [/^runs\b/i, 'run'], [/^keeps\b/i, 'keep'],+];++function contextPhrase(context: string): string {+ if (!context || context === '*') return '';+ if (context.startsWith('project:')) return ` in ${path.basename(context.slice('project:'.length)) || 'that project'}`;+ if (context.startsWith('channel:')) return ` on ${context.slice('channel:'.length)}`;+ if (context.startsWith('domain:')) return ` for ${context.slice('domain:'.length)}`;+ if (context.startsWith('time:')) return ` in the ${context.slice('time:'.length).replace('-', ' ')}`;+ return ` (${context})`;+}++/** The belief in plain second-person language, for a one-line note. */+export function restate(b: Pick<Belief, 'kind' | 'dimension' | 'level' | 'statement' | 'context'>): string {+ const ctx = contextPhrase(b.context);+ if (b.kind === 'trait' && b.dimension && b.level) {+ const fixed = TRAIT_PHRASES[b.dimension]?.[b.level];+ if (fixed) return `${fixed}${ctx}`;+ const colon = b.dimension.indexOf(':');+ const base = colon > 0 ? b.dimension.slice(0, colon) : b.dimension;+ const domain = colon > 0 ? b.dimension.slice(colon + 1) : '';+ if (base === 'expertise') {+ const phrase = b.level === 'expert' ? `you're an expert in ${domain}` : b.level === 'novice' ? `you're new to ${domain}` : `you're competent in ${domain}`;+ return `${phrase}${ctx}`;+ }+ if (base === 'risk') return `you're ${b.level} about ${domain}${ctx}`;+ }+ let s = b.statement.trim().replace(/[.。]+$/, '');+ const owner = /^(the\s+)?(owner|user)\s+/i;+ if (owner.test(s)) {+ s = s.replace(owner, '');+ for (const [re, to] of THIRD_TO_SECOND) if (re.test(s)) { s = s.replace(re, to); break; }+ s = `you ${s.charAt(0).toLowerCase()}${s.slice(1)}`;+ } else {+ s = `${s.charAt(0).toLowerCase()}${s.slice(1)}`;+ }+ return `${s}${ctx}`;+}++function askedRecently(beliefs: AnnounceOptions['beliefs'], id: string, now: number): boolean {+ const iso = beliefs.getMeta(`asked:${id}`);+ if (!iso) return false;+ const at = Date.parse(iso);+ return Number.isFinite(at) && now - at < ASK_COOLDOWN_MS;+}++function question(a: Belief, b: Belief | null): string {+ return b+ ? `☿ I've noticed ${restate(a)} but also ${restate(b)}. Which should I go with?`+ : `☿ I've noticed ${restate(a)}, but I'm no longer sure. Is that still right?`;+}++/** Ask about a contested pair now (marks both as asked); null when it was asked within the last day. */+function askContested(beliefs: AnnounceOptions['beliefs'], a: Belief, b: Belief | null, now: number): string | null {+ if (askedRecently(beliefs, a.id, now) || (b && askedRecently(beliefs, b.id, now))) return null;+ const iso = new Date(now).toISOString();+ beliefs.setMeta(`asked:${a.id}`, iso);+ if (b) beliefs.setMeta(`asked:${b.id}`, iso);+ return question(a, b);+}++/**+ * The one line to append after a reply, or null. Contested results become a+ * question; otherwise the first behaviour-changing result becomes a note.+ */+export function composeLearnedNote(results: ReadonlyArray<ApplyResult | null>, opts: AnnounceOptions): string | null {+ const now = opts.now ?? Date.now();+ const changing = results.filter((r): r is ApplyResult => !!r && r.behaviorChanging && ANNOUNCED_CHANGES.has(r.change));+ if (changing.length === 0) return null;+ for (const r of changing) {+ if (r.change !== 'contested') continue;+ const pair = opts.beliefs.contested().find((p) => p.a.id === r.belief.id || p.b?.id === r.belief.id);+ const a = pair?.a ?? r.belief;+ const b = pair ? pair.b : null;+ const q = askContested(opts.beliefs, a, b, now);+ if (q) return q;+ }+ const noted = changing.find((r) => r.change !== 'contested');+ if (!noted) return null;+ return `☿ Noted: ${restate(noted.belief)}. Say "forget that" to undo.`;+}++/**+ * The contested question the extractor should know about: the pair asked in+ * the last day (the owner may be answering it now), else the oldest pair not+ * yet asked. Does not mark anything as asked; composeLearnedNote does that.+ */+export function pendingQuestion(beliefs: AnnounceOptions['beliefs'], now = Date.now()): string | null {+ const pairs = beliefs.contested().slice().sort((x, y) => x.a.updatedAt - y.a.updatedAt);+ const asked = pairs.find(({ a, b }) => askedRecently(beliefs, a.id, now) || (b && askedRecently(beliefs, b.id, now)));+ if (asked) return question(asked.a, asked.b);+ const next = pairs[0];+ return next ? question(next.a, next.b) : null;+}diff --git a/src/learning/extract.test.ts b/src/learning/extract.test.tsnew file mode 100644index 00000000..ddf9652b--- /dev/null+++ b/src/learning/extract.test.ts@@ -0,0 +1,185 @@+import { describe, expect, it } from 'vitest';+import type { Belief } from '../memory/beliefs.js';+import {+ EXTRACT_MAX_OUTPUT_TOKENS, buildSystemPrompt, buildUserPrompt, extractBeliefOperations, levelsFor, parseBeliefOperations,+ relevantBeliefs, shouldExtract, validateOperation, type ExtractDeps, type ExtractInput,+} from './extract.js';+import { collectTurnSignals } from './signals.js';++const ids = new Set(['b1', 'b2']);++function belief(partial: Partial<Belief> & Pick<Belief, 'id' | 'statement'>): Belief {+ const now = Date.now();+ return {+ ownerKey: 'user:owner', kind: 'preference', dimension: null, level: null, detail: null, context: '*', support: 2, against: 0,+ supportSessions: 2, timescale: 'stable', status: 'held', ownerStated: false, validFrom: now, validTo: null, supersededBy: null,+ lastSupportedAt: now, lastUsedAt: null, createdAt: now, updatedAt: now, shareable: false, ...partial,+ };+}++function input(overrides: Partial<ExtractInput> = {}): ExtractInput {+ const ownerMessage = overrides.ownerMessage ?? "don't ask before running tests here";+ return {+ ownerMessage,+ mercuryReply: 'Running the tests now.',+ recentTurns: [{ role: 'owner', content: 'run the tests' }, { role: 'mercury', content: 'May I run npm test?' }],+ signals: collectTurnSignals({ message: ownerMessage, cwd: '/home/o/proj', channelType: 'cli', sessionId: 's1', now: new Date(2026, 9, 24, 10, 0) }),+ ownerName: 'Salman',+ pendingQuestion: null,+ sessionId: 's1',+ turnRef: 'm1',+ ...overrides,+ };+}++describe('parseBeliefOperations', () => {+ it('returns [] for prose, invalid JSON and non-arrays (never a prose fallback)', () => {+ expect(parseBeliefOperations('The user prefers short answers.\n- likes tests', ids)).toEqual([]);+ expect(parseBeliefOperations('{"op":"create"', ids)).toEqual([]);+ expect(parseBeliefOperations('{"op":"support","beliefId":"b1","excerpt":"x"}', ids)).toEqual([]);+ expect(parseBeliefOperations('', ids)).toEqual([]);+ expect(parseBeliefOperations('null', ids)).toEqual([]);+ });++ it('accepts a fenced array and stamps session and turn', () => {+ const ops = parseBeliefOperations('```json\n[{"op":"support","beliefId":"b1","excerpt":"same as before"}]\n```', ids, { sessionId: 's1', turnRef: 'm1' });+ expect(ops).toEqual([{ op: 'support', beliefId: 'b1', excerpt: 'same as before', sessionId: 's1', turnRef: 'm1' }]);+ });++ it('drops references to unknown beliefs and ops without an excerpt', () => {+ expect(parseBeliefOperations('[{"op":"support","beliefId":"nope","excerpt":"x"}]', ids)).toEqual([]);+ expect(parseBeliefOperations('[{"op":"contradict","beliefId":"b1"}]', ids)).toEqual([]);+ expect(parseBeliefOperations('[{"op":"delete","beliefId":"b1","excerpt":"x"}]', ids)).toEqual([]);+ });++ it('validates created beliefs: kind, statement length, trait dimension and level', () => {+ const ok = validateOperation({ op: 'owner_stated', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Wants tests run without asking in this repo', context: 'project:/home/o/proj', excerpt: "don't ask before running tests here" }, ids);+ expect(ok).toMatchObject({ op: 'owner_stated', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', context: 'project:/home/o/proj', timescale: 'stable' });+ expect(validateOperation({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'always', statement: 'Wants tests run without asking', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'create', kind: 'trait', dimension: 'mood', level: 'happy', statement: 'Is a happy person generally', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'create', kind: 'trait', statement: 'A trait without a dimension at all', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'create', kind: 'personality', statement: 'Is an INTJ according to themselves', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'create', kind: 'fact', statement: 'too short', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'create', kind: 'fact', statement: 'x'.repeat(221), excerpt: 'x' }, ids)).toBeNull();+ const scoped = validateOperation({ op: 'create', kind: 'trait', dimension: 'expertise:typescript', level: 'expert', statement: 'Expert in TypeScript, skips basic explanations', excerpt: 'I know TS' }, ids);+ expect(scoped).toMatchObject({ dimension: 'expertise:typescript', level: 'expert' });+ expect(levelsFor('risk:publishing')).toEqual(['cautious', 'normal', 'bold']);+ expect(levelsFor('risk:')).toBeNull();+ expect(levelsFor('unknown')).toBeNull();+ });++ it('forces situations to situational and episodes to episodic; non-traits carry no dimension', () => {+ const sit = validateOperation({ op: 'create', kind: 'situation', timescale: 'stable', statement: 'Is in a hurry to ship before the demo', excerpt: 'quick, demo in 10' }, ids);+ expect(sit).toMatchObject({ kind: 'situation', timescale: 'situational' });+ expect(sit).not.toHaveProperty('dimension');+ const ep = validateOperation({ op: 'create', kind: 'episode', dimension: 'brevity', level: 'terse', statement: 'Mercury set up the release script for proj', excerpt: 'set up releases' }, ids);+ expect(ep).toMatchObject({ kind: 'episode', timescale: 'episodic' });+ expect(ep).not.toHaveProperty('dimension');+ });++ it('truncates long excerpts and caps the number of operations', () => {+ const many = Array.from({ length: 9 }, (_, i) => ({ op: 'support', beliefId: 'b1', excerpt: `e${i} ${'y'.repeat(300)}` }));+ const ops = parseBeliefOperations(JSON.stringify(many), ids);+ expect(ops).toHaveLength(5);+ expect(ops[0].excerpt.length).toBe(200);+ });++ it('validates corrections with and without a replacement statement', () => {+ expect(validateOperation({ op: 'correction', beliefId: 'b2', excerpt: 'no, shorter' }, ids)).toEqual({ op: 'correction', beliefId: 'b2', excerpt: 'no, shorter' });+ expect(validateOperation({ op: 'correction', beliefId: 'b2', statement: 'Wants replies kept to a few lines', dimension: 'brevity', level: 'terse', excerpt: 'shorter please' }, ids))+ .toMatchObject({ op: 'correction', beliefId: 'b2', statement: 'Wants replies kept to a few lines', dimension: 'brevity', level: 'terse' });+ expect(validateOperation({ op: 'correction', beliefId: 'b2', statement: 'short', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'correction', beliefId: 'b2', statement: 'A valid replacement statement', dimension: 'brevity', level: 'loud', excerpt: 'x' }, ids)).toBeNull();+ expect(validateOperation({ op: 'correction', beliefId: 'zz', statement: 'A valid replacement statement', excerpt: 'x' }, ids)).toBeNull();+ });+});++describe('prompt', () => {+ it('labels Mercury\'s reply as never a source and lists the trait dimensions', () => {+ const sys = buildSystemPrompt('Salman');+ expect(sys).toContain('NEVER a source for beliefs about the owner');+ expect(sys).toContain('autonomy: ask-first | ask-when-risky | just-do-it');+ expect(sys).toContain('expertise:<domain>');+ expect(sys).toContain('Output [] when nothing durable was learned');+ const user = buildUserPrompt(input(), [belief({ id: 'b1', statement: 'Prefers to be asked before commands run', kind: 'trait', dimension: 'autonomy', level: 'ask-first' })]);+ expect(user).toContain('id=b1 [trait autonomy=ask-first] Prefers to be asked before commands run (status: held');+ expect(user).toContain('MERCURY SAID (never a source for beliefs about the owner');+ expect(user).toContain("OWNER SAID: don't ask before running tests here");+ expect(user).toContain('autonomy request: just-do-it');+ expect(user).toContain('PENDING QUESTION: none');+ });+});++function deps(overrides: Partial<ExtractDeps> & { text?: string; afford?: boolean; paused?: boolean; found?: Belief[] } = {}): ExtractDeps & { calls: number; usage: unknown[] } {+ const found = overrides.found ?? [];+ const state = { calls: 0, usage: [] as unknown[] };+ return Object.assign(state, {+ providers: { getDefault: () => ({ name: 'scripted', getModel: () => 'scripted-1', getModelInstance: () => ({}) }) },+ tokenBudget: { canAfford: () => overrides.afford ?? true, recordUsage: (u: unknown) => { state.usage.push(u); } },+ beliefs: { searchRelevant: () => found.map((b) => ({ ...b, confidence: 0.8 })), findSimilar: () => found },+ learningPaused: () => overrides.paused ?? false,+ generate: async () => { state.calls++; return { text: overrides.text ?? '[]', usage: { inputTokens: 100, outputTokens: 20 } }; },+ ...overrides,+ });+}++describe('extractBeliefOperations', () => {+ it('skips trivial messages, paused learning, general-access channels and a thin budget without a model call', async () => {+ const d = deps();+ expect(await extractBeliefOperations(d, input({ ownerMessage: 'thanks!' }))).toEqual([]);+ expect(await extractBeliefOperations(deps({ paused: true }), input())).toEqual([]);+ expect(await extractBeliefOperations(deps({ afford: false }), input())).toEqual([]);+ expect(await extractBeliefOperations(d, input({ channelAccess: 'general' }))).toEqual([]);+ expect(d.calls).toBe(0);+ expect(shouldExtract(d, { ownerMessage: 'hello there' })).toBe(false);+ expect(shouldExtract(d, { ownerMessage: 'please stop asking before tests' })).toBe(true);+ // A correction that happens to open with "no" is the most valuable turn there is.+ expect(shouldExtract(d, { ownerMessage: "No no, I said British spelling. Fix the blog post and stop using 'color'." })).toBe(true);+ expect(shouldExtract(d, { ownerMessage: 'ok do it' })).toBe(false);+ });++ it('calls the model once, records usage, and returns validated ops referencing only known beliefs', async () => {+ const existing = belief({ id: 'b1', statement: 'Prefers to be asked before commands run', kind: 'trait', dimension: 'autonomy', level: 'ask-first' });+ const d = deps({+ found: [existing],+ text: JSON.stringify([+ { op: 'correction', beliefId: 'b1', statement: 'Wants tests run without asking in this repo', dimension: 'autonomy', level: 'just-do-it', context: 'project:/home/o/proj', excerpt: "don't ask before running tests here" },+ { op: 'support', beliefId: 'ghost', excerpt: 'x' },+ { op: 'create', kind: 'episode', statement: 'Mercury ran the test suite for the owner', excerpt: 'run the tests' },+ ]),+ });+ const ops = await extractBeliefOperations(d, input());+ expect(d.calls).toBe(1);+ expect(d.usage).toEqual([{ provider: 'scripted', model: 'scripted-1', inputTokens: 100, outputTokens: 20, totalTokens: 120, channelType: 'internal', agentId: undefined }]);+ expect(ops.map((o) => o.op)).toEqual(['correction', 'create']);+ expect(ops[0]).toMatchObject({ beliefId: 'b1', level: 'just-do-it', sessionId: 's1', turnRef: 'm1' });+ expect(ops[1]).toMatchObject({ kind: 'episode', timescale: 'episodic' });+ });++ it('returns [] when the model answers in prose', async () => {+ const d = deps({ text: 'The owner seems to prefer running tests without being asked.' });+ expect(await extractBeliefOperations(d, input())).toEqual([]);+ expect(d.calls).toBe(1);+ });++ it('uses the configured output cap', () => {+ expect(EXTRACT_MAX_OUTPUT_TOKENS).toBe(600);+ });++ it('merges relevant and similar beliefs without duplicates', () => {+ const a = belief({ id: 'a', statement: 'Likes short answers' });+ const b = belief({ id: 'b', statement: 'Works on Mercury' });+ const found = relevantBeliefs({ searchRelevant: () => [{ ...a, confidence: 0.9 }], findSimilar: () => [a, b] }, 'short answers mercury');+ expect(found.map((x) => x.id)).toEqual(['a', 'b']);+ });+});++describe('parseBeliefOperations excerpt fallback', () => {+ it('an op the model emitted without an excerpt cites the owner message instead of being dropped', () => {+ const text = JSON.stringify([{ op: 'owner_stated', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Prefers short answers on the phone', context: '*' }]);+ expect(parseBeliefOperations(text, new Set())).toEqual([]);+ const ops = parseBeliefOperations(text, new Set(), { defaultExcerpt: '[Voice call: …] Always keep the answer short, I read these on my phone.' });+ expect(ops).toHaveLength(1);+ expect(ops[0].excerpt).toBe('[Voice call: …] Always keep the answer short, I read these on my phone.');+ });+});diff --git a/src/learning/extract.ts b/src/learning/extract.tsnew file mode 100644index 00000000..8e3d7611--- /dev/null+++ b/src/learning/extract.ts@@ -0,0 +1,306 @@+/**+ * Per-turn belief extraction (docs/owner-model.md §3.4 "per turn", ADR-026).+ *+ * One small model call per non-trivial turn turns the owner's words, the+ * turn's signals and the beliefs Mercury already holds into a list of+ * belief operations — or `[]`, which is the usual answer. The output is+ * parsed strictly: anything that is not a JSON array of valid operations+ * becomes `[]`. There is no prose fallback; that is where the old store's+ * junk came from.+ */+import { parseJsonLenient } from './json.js';+import { looksLikeIdentity, looksLikeSecret, ownerWords } from './text.js';+import { generateText } from 'ai';+import type { Belief, BeliefKind, BeliefOperation, BeliefStore, BeliefTimescale } from '../memory/beliefs.js';+import { SCOPED_DIMENSIONS, TRAIT_DIMENSIONS } from '../memory/beliefs.js';+import { logger } from '../utils/logger.js';+import { describeSignals, type TurnSignals } from './signals.js';++export const EXTRACT_MAX_OUTPUT_TOKENS = 600;+export const EXTRACT_MIN_BUDGET = 1200;+export const MAX_OPS_PER_TURN = 5;+export const RELEVANT_BELIEF_LIMIT = 8;+const STATEMENT_MIN = 12;+const STATEMENT_MAX = 220;+const EXCERPT_MAX = 200;+const CONTEXT_MAX = 160;++/** Greetings and acknowledgements: nothing to learn, no model call. */+export const TRIVIAL_MESSAGE = /^(hi|hello|hey|thanks|thank you|ok|okay|yes|no|bye|goodbye|good morning|good evening)\b/i;+/** A message that opens like a greeting but goes on ("No no, I said British spelling…") is a turn, often a correction. */+export const TRIVIAL_MAX_CHARS = 40;++export function isTrivialMessage(message: string): boolean {+ const m = message.trim();+ return m.length <= TRIVIAL_MAX_CHARS && TRIVIAL_MESSAGE.test(m);+}++export interface ExtractTurn {+ role: 'owner' | 'mercury';+ content: string;+}++export interface ExtractInput {+ ownerMessage: string;+ mercuryReply: string;+ /** The last few turns of the session before this one, oldest first. */+ recentTurns: readonly ExtractTurn[];+ signals: TurnSignals;+ ownerName: string;+ /** A contested-belief question Mercury has asked or is about to ask, so an answer can be read. */+ pendingQuestion: string | null;+ channelAccess?: string;+ sessionId?: string;+ turnRef?: string;+}++/** The slice of the belief store the extractor reads. */+export type BeliefSearch = Pick<BeliefStore, 'searchRelevant' | 'findSimilar'>;++interface UsageLike { inputTokens?: number; outputTokens?: number }+export type GenerateFn = (args: { model: unknown; system: string; messages: Array<{ role: 'user'; content: string }>; maxOutputTokens: number }) => Promise<{ text: string; usage?: UsageLike }>;++export interface ExtractDeps {+ providers: { getDefault(): { name: string; getModel(): string; getModelInstance(): unknown } };+ tokenBudget: {+ canAfford(estimatedTokens: number): boolean;+ recordUsage(entry: { provider: string; model: string; inputTokens: number; outputTokens: number; totalTokens: number; channelType: 'internal'; agentId?: string }): void;+ };+ beliefs: BeliefSearch;+ learningPaused: () => boolean;+ agentId?: string;+ /** Injected by tests; defaults to the AI SDK's generateText. */+ generate?: GenerateFn;+}++const KINDS: ReadonlySet<string> = new Set<BeliefKind>(['fact', 'preference', 'trait', 'situation', 'relationship', 'goal', 'project', 'episode']);+const TIMESCALES: ReadonlySet<string> = new Set<BeliefTimescale>(['stable', 'situational', 'episodic']);+const OPS: ReadonlySet<string> = new Set<BeliefOperation['op']>(['create', 'support', 'contradict', 'owner_stated', 'correction']);++/** The trait levels allowed for a dimension, or null when the dimension is unknown. */+export function levelsFor(dimension: string): readonly string[] | null {+ if (Object.prototype.hasOwnProperty.call(TRAIT_DIMENSIONS, dimension)) return TRAIT_DIMENSIONS[dimension as keyof typeof TRAIT_DIMENSIONS];+ const colon = dimension.indexOf(':');+ if (colon > 0 && colon < dimension.length - 1) {+ const base = dimension.slice(0, colon);+ if (Object.prototype.hasOwnProperty.call(SCOPED_DIMENSIONS, base)) return SCOPED_DIMENSIONS[base as keyof typeof SCOPED_DIMENSIONS];+ }+ return null;+}++/** Existing beliefs the turn may touch: usable ones matching the message plus near-duplicates of it. */+export function relevantBeliefs(beliefs: BeliefSearch, message: string, limit = RELEVANT_BELIEF_LIMIT): Belief[] {+ const seen = new Set<string>();+ const out: Belief[] = [];+ const add = (b: Belief): void => { if (!seen.has(b.id) && b.status !== 'forgotten' && out.length < limit) { seen.add(b.id); out.push(b); } };+ try { for (const b of beliefs.searchRelevant(message, limit)) add(b); } catch (err) { logger.debug({ err: (err as Error).message }, 'belief search failed'); }+ try { for (const b of beliefs.findSimilar(message, undefined, limit)) add(b); } catch (err) { logger.debug({ err: (err as Error).message }, 'belief similarity search failed'); }+ return out;+}++function dimensionsHelp(): string {+ const fixed = Object.entries(TRAIT_DIMENSIONS).map(([d, levels]) => ` ${d}: ${levels.join(' | ')}`);+ const scoped = Object.entries(SCOPED_DIMENSIONS).map(([d, levels]) => ` ${d}:<domain> (e.g. ${d}:typescript): ${levels.join(' | ')}`);+ return [...fixed, ...scoped].join('\n');+}++export function buildSystemPrompt(ownerName: string): string {+ return `You maintain Mercury's beliefs about its owner, ${ownerName}. After each chat turn you decide what, if anything, this turn taught Mercury about the owner, and output belief operations as a JSON array. Nothing else: no prose, no markdown fences, no commentary.++SOURCES+- Only the OWNER's words and actions are evidence about the owner.+- The block labelled "MERCURY SAID" is Mercury's own reply. It is NEVER a source for beliefs about the owner. At most it may yield one op of kind "episode" recording what Mercury did ("Mercury set up the release script for project X"), and only when that is worth remembering.+- EXISTING BELIEFS are what Mercury already holds. Prefer "support", "contradict" or "correction" on an existing belief over creating a near-duplicate.++OPERATIONS (each object has "op" and "excerpt"; "excerpt" quotes the OWNER's own words, at most 200 characters)+- {"op":"create", "kind", "statement", "context", "timescale", "dimension"?, "level"?} — a new inferred belief. statement is 12-220 characters, third person ("Prefers one command over many manual steps").+- {"op":"owner_stated", "kind", "statement", "context", "timescale", "dimension"?, "level"?} — the owner said it outright about themselves or how they want Mercury to work. Held immediately.+- {"op":"support", "beliefId"} — the turn is fresh evidence for an existing belief.+- {"op":"contradict", "beliefId"} — the turn is evidence against an existing belief.+- {"op":"correction", "beliefId", "statement", "dimension"?, "level"?, "context"?} — the owner corrected a belief Mercury acted on; "statement" is the replacement (12-220 chars). Omit "statement" when the owner only rejected it.++KINDS: fact | preference | trait | situation | relationship | goal | project | episode+TIMESCALES: stable | situational | episodic+CONTEXT: where the belief applies: "*" (everywhere), "project:<cwd>", "channel:<type>", "domain:<name>", "time:<morning|daytime|evening|late-night>".++TRAITS (kind "trait") must use exactly one of these dimensions and one of its levels:+${dimensionsHelp()}++RULES+- Output [] when nothing durable was learned. That is the right answer for most turns: questions, tasks, chit-chat and anything Mercury could re-derive from the conversation itself teach nothing about the owner.+- Never infer personality types, health, religion, politics, sexuality, or anything about a third person beyond what the owner stated. Never record passwords, codes, API keys, tokens, card or account numbers, even if the owner pasted them. If the owner states such a thing about themselves, store it as kind "fact" with the owner's words; never generalise it.+- Passing states (frustrated, in a hurry, tired, exploring) are kind "situation" with timescale "situational"; never a trait.+- The owner's name, email, handles and phone number are already in Mercury's configuration; never create a belief that states who the owner is or how to reach them.+- When the owner corrects how Mercury behaved ("don't ask before running tests here", "shorter", "stop explaining"), emit "correction" on the belief Mercury acted on if one is listed, otherwise "owner_stated" with kind "trait" (e.g. autonomy = just-do-it with context project:<cwd>; brevity = terse).+- The TURN SIGNALS are hints computed by code; use them to decide, but the excerpt must still quote the owner.+- If a PENDING QUESTION is listed and the owner answered it, emit "correction" or "support" on the belief that won and "contradict" on the one that lost.+- At most ${MAX_OPS_PER_TURN} operations. Each fact appears once.+- Output a pure JSON array.`;+}++function formatBelief(b: Belief): string {+ const dim = b.dimension ? ` ${b.dimension}=${b.level ?? '?'}` : '';+ return `- id=${b.id} [${b.kind}${dim}] ${b.statement} (status: ${b.status}, support: ${b.support}, against: ${b.against}, context: ${b.context})`;+}++export function buildUserPrompt(input: ExtractInput, existing: readonly Belief[]): string {+ const parts: string[] = [];+ parts.push(`OWNER: ${input.ownerName}`);+ parts.push('');+ parts.push(`EXISTING BELIEFS (relevant to this turn; ${existing.length} of them)`);+ parts.push(existing.length > 0 ? existing.map(formatBelief).join('\n') : '- none');+ parts.push('');+ parts.push(`PENDING QUESTION: ${input.pendingQuestion ?? 'none'}`);+ parts.push('');+ parts.push('TURN SIGNALS (computed by code)');+ parts.push(describeSignals(input.signals));+ parts.push('');+ if (input.recentTurns.length > 0) {+ parts.push('RECENT TURNS (oldest first)');+ for (const t of input.recentTurns) {+ parts.push(`${t.role === 'owner' ? 'OWNER SAID' : 'MERCURY SAID'}: ${(t.role === 'owner' ? ownerWords(t.content) : t.content).replace(/\s+/g, ' ').trim().slice(0, 600)}`);+ }+ parts.push('');+ }+ parts.push('THIS TURN');+ parts.push(`OWNER SAID: ${ownerWords(input.ownerMessage).slice(0, 4000)}`);+ parts.push('');+ parts.push(`MERCURY SAID (never a source for beliefs about the owner; it may only yield an "episode" of what Mercury did): ${input.mercuryReply.replace(/\s+/g, ' ').trim().slice(0, 1500)}`);+ parts.push('');+ parts.push('Belief operations as a JSON array:');+ return parts.join('\n');+}++// ── parsing ─────────────────────────────────────────────────────────────++function str(v: unknown): string | undefined {+ return typeof v === 'string' && v.trim().length > 0 ? v.trim() : undefined;+}++function validStatement(v: unknown): string | undefined {+ const s = str(v)?.replace(/\s+/g, ' ');+ return s && s.length >= STATEMENT_MIN && s.length <= STATEMENT_MAX ? s : undefined;+}++function validContext(v: unknown): string {+ const s = str(v);+ if (!s || s.length > CONTEXT_MAX) return '*';+ return s;+}++/**+ * Parse the model's output into validated operations. Anything that is not a+ * JSON array yields []; every element is checked for op, kind, dimension and+ * level, statement length, excerpt and (for references) a known belief id.+ */+export function parseBeliefOperations(text: string, existingIds: ReadonlySet<string>, ref: { sessionId?: string; turnRef?: string; defaultExcerpt?: string } = {}): BeliefOperation[] {+ const parsed = parseJsonLenient(text);+ if (!Array.isArray(parsed)) return [];+ const out: BeliefOperation[] = [];+ const fallback = ref.defaultExcerpt?.replace(/\s+/g, ' ').trim().slice(0, EXCERPT_MAX);+ for (const raw of parsed) {+ if (out.length >= MAX_OPS_PER_TURN) break;+ // Models drop the excerpt now and then; the owner's own message is the+ // evidence anyway, so an op without one cites that rather than being lost.+ const withExcerpt = fallback && raw && typeof raw === 'object' && !str((raw as Record<string, unknown>).excerpt)+ ? { ...(raw as Record<string, unknown>), excerpt: fallback }+ : raw;+ const op = validateOperation(withExcerpt, existingIds);+ if (op) out.push({ ...op, sessionId: ref.sessionId, turnRef: ref.turnRef });+ }+ return out;+}++export function validateOperation(raw: unknown, existingIds: ReadonlySet<string>): BeliefOperation | null {+ if (!raw || typeof raw !== 'object') return null;+ const r = raw as Record<string, unknown>;+ const opName = str(r.op);+ if (!opName || !OPS.has(opName)) return null;+ const op = opName as BeliefOperation['op'];+ const excerpt = str(r.excerpt)?.replace(/\s+/g, ' ').slice(0, EXCERPT_MAX);+ if (!excerpt) return null;++ if (op === 'create' || op === 'owner_stated') {+ const kind = str(r.kind);+ if (!kind || !KINDS.has(kind)) return null;+ const statement = validStatement(r.statement);+ if ((statement && (looksLikeSecret(statement) || looksLikeIdentity(statement))) || looksLikeSecret(String(r.excerpt ?? ''))) return null;+ if (!statement) return null;+ const trait = traitFields(r, kind as BeliefKind);+ if (trait === 'invalid') return null;+ const timescale = str(r.timescale);+ const ts: BeliefTimescale = kind === 'situation' ? 'situational'+ : kind === 'episode' ? 'episodic'+ : timescale && TIMESCALES.has(timescale) ? (timescale as BeliefTimescale) : 'stable';+ const detail = str(r.detail)?.slice(0, 500);+ return {+ op, kind: kind as BeliefKind, statement, excerpt, context: validContext(r.context), timescale: ts,+ ...(detail ? { detail } : {}), ...(trait ?? {}),+ };+ }++ const beliefId = str(r.beliefId);+ if (!beliefId || !existingIds.has(beliefId)) return null;+ if (op === 'support' || op === 'contradict') return { op, beliefId, excerpt };++ // correction: replacement statement optional; when present it must be a valid statement.+ const statement = r.statement === undefined || r.statement === null || r.statement === '' ? undefined : validStatement(r.statement);+ if (r.statement !== undefined && r.statement !== null && r.statement !== '' && !statement) return null;+ const kind = str(r.kind);+ // A replacement that names a dimension or level must name a valid pair.+ const trait = kind === 'trait' || r.dimension !== undefined || r.level !== undefined ? traitFields(r, 'trait') : undefined;+ if (trait === 'invalid') return null;+ const timescale = str(r.timescale);+ return {+ op, beliefId, excerpt,+ ...(statement ? { statement } : {}),+ ...(kind && KINDS.has(kind) ? { kind: kind as BeliefKind } : {}),+ ...(r.context !== undefined ? { context: validContext(r.context) } : {}),+ ...(timescale && TIMESCALES.has(timescale) ? { timescale: timescale as BeliefTimescale } : {}),+ ...(trait ?? {}),+ };+}++/** Traits need a known dimension and level; other kinds carry neither. */+function traitFields(r: Record<string, unknown>, kind: BeliefKind): { dimension: string; level: string } | undefined | 'invalid' {+ if (kind !== 'trait') return undefined;+ const dimension = str(r.dimension)?.toLowerCase();+ const level = str(r.level)?.toLowerCase();+ if (!dimension || !level) return 'invalid';+ const levels = levelsFor(dimension);+ if (!levels || !levels.includes(level)) return 'invalid';+ return { dimension, level };+}++// ── the call ────────────────────────────────────────────────────────────++/** Whether a turn is worth a model call at all. */+export function shouldExtract(deps: Pick<ExtractDeps, 'tokenBudget' | 'learningPaused'>, input: Pick<ExtractInput, 'ownerMessage' | 'channelAccess'>): boolean {+ if (deps.learningPaused()) return false;+ if (input.channelAccess === 'general') return false;+ if (isTrivialMessage(input.ownerMessage)) return false;+ if (!deps.tokenBudget.canAfford(EXTRACT_MIN_BUDGET)) return false;+ return true;+}++export async function extractBeliefOperations(deps: ExtractDeps, input: ExtractInput): Promise<BeliefOperation[]> {+ if (!shouldExtract(deps, input)) return [];+ const existing = relevantBeliefs(deps.beliefs, input.ownerMessage);+ const provider = deps.providers.getDefault();+ const generate: GenerateFn = deps.generate ?? ((args) => generateText({ model: args.model as Parameters<typeof generateText>[0]['model'], system: args.system, messages: args.messages, maxOutputTokens: args.maxOutputTokens }));+ const result = await generate({+ model: provider.getModelInstance(),+ system: buildSystemPrompt(input.ownerName),+ messages: [{ role: 'user', content: buildUserPrompt(input, existing) }],+ maxOutputTokens: EXTRACT_MAX_OUTPUT_TOKENS,+ });+ const inputTokens = result.usage?.inputTokens ?? 0;+ const outputTokens = result.usage?.outputTokens ?? 0;+ deps.tokenBudget.recordUsage({+ provider: provider.name, model: provider.getModel(), inputTokens, outputTokens, totalTokens: inputTokens + outputTokens,+ channelType: 'internal', agentId: deps.agentId,+ });+ const ops = parseBeliefOperations(result.text ?? '', new Set(existing.map((b) => b.id)), { sessionId: input.sessionId, turnRef: input.turnRef, defaultExcerpt: ownerWords(input.ownerMessage) });+ if (ops.length > 0) logger.debug({ ops: ops.map((o) => `${o.op}${o.kind ? `:${o.kind}` : ''}`) }, 'Belief operations extracted');+ return ops;+}diff --git a/src/learning/json.test.ts b/src/learning/json.test.tsnew file mode 100644index 00000000..a8a71b50--- /dev/null+++ b/src/learning/json.test.ts@@ -0,0 +1,62 @@+import { describe, expect, it } from 'vitest';+import { parseJsonLenient } from './json.js';++describe('parseJsonLenient', () => {+ it('parses plain and fenced JSON', () => {+ expect(parseJsonLenient('[{"a":1}]')).toEqual([{ a: 1 }]);+ expect(parseJsonLenient('```json\n{"ops": []}\n```')).toEqual({ ops: [] });+ expect(parseJsonLenient('Sure! Here it is:\n```json\n{"ops":[{"op":"support","beliefId":"x"}]}')).toEqual({ ops: [{ op: 'support', beliefId: 'x' }] });+ });+ it('repairs output cut off by the token limit', () => {+ const cut = '{"ops":[{"op":"support","beliefId":"a","excerpt":"fine"},{"op":"create","kind":"trait","statement":"Prefers short re';+ const v = parseJsonLenient(cut) as { ops: unknown[] };+ expect(Array.isArray(v.ops)).toBe(true);+ expect(v.ops[0]).toEqual({ op: 'support', beliefId: 'a', excerpt: 'fine' });+ const cut2 = '{"ops":[{"op":"support","beliefId":"a","excerpt":"x"}],"merges":[],"brief":{"about":"Works on Mercury and';+ const v2 = parseJsonLenient(cut2) as { ops: unknown[]; brief?: { about?: string } };+ expect(v2.ops).toHaveLength(1);+ });+ it('returns null for prose', () => {+ expect(parseJsonLenient('I learned nothing today.')).toBeNull();+ });+});++import { ownerWords } from './text.js';+describe('ownerWords', () => {+ it('strips bracketed channel guidance and keeps what the owner said', () => {+ expect(ownerWords("[Voice call: the owner asked this out loud. Use your tools…]\nclear the old logs")).toBe('clear the old logs');+ expect(ownerWords('[A]\n[B: more]\n shorter please ')).toBe('shorter please');+ expect(ownerWords('plain message [with brackets] inside')).toBe('plain message [with brackets] inside');+ });+});++import { asksAboutSelf, looksLikeIdentity, looksLikeSecret } from './text.js';+describe('looksLikeSecret', () => {+ it('flags credentials and leaves ordinary statements alone', () => {+ expect(looksLikeSecret('Temp mail account with password MercuryTemp2026!')).toBe(true);+ expect(looksLikeSecret('uses api key sk-abcdefghijklmnopqrstuvwxyz123456')).toBe(true);+ expect(looksLikeSecret('Prefers one command over many manual steps')).toBe(false);+ expect(looksLikeSecret('Works on Mercury Agent at Cosmic Stack')).toBe(false);+ });+});++describe('looksLikeIdentity', () => {+ it('keeps who the owner is out of the belief store (config is the one source)', () => {+ expect(looksLikeIdentity("The owner's name is Salman")).toBe(true);+ expect(looksLikeIdentity('Email is [email protected]')).toBe(true);+ expect(looksLikeIdentity('Goes by hotheadhacker on GitHub')).toBe(true);+ expect(looksLikeIdentity('Prefers terse replies in the evening')).toBe(false);+ expect(looksLikeIdentity('Works on Mercury, an AI agent')).toBe(false);+ });+});++describe('asksAboutSelf', () => {+ it('spots the owner asking what Mercury knows about them', () => {+ expect(asksAboutSelf('What do you know about me?')).toBe(true);+ expect(asksAboutSelf('do you remember my preferences')).toBe(true);+ expect(asksAboutSelf('what have you learned so far')).toBe(true);+ expect(asksAboutSelf('[Voice call: …] tell me about myself')).toBe(true);+ expect(asksAboutSelf('convert this epub with pandoc')).toBe(false);+ expect(asksAboutSelf('what do you know about Rust lifetimes')).toBe(false);+ });+});diff --git a/src/learning/json.ts b/src/learning/json.tsnew file mode 100644index 00000000..026a3632--- /dev/null+++ b/src/learning/json.ts@@ -0,0 +1,52 @@+/**+ * Lenient JSON for model output: strips code fences and repairs output that+ * was cut off by the token limit (unclosed strings, objects and arrays are+ * closed at the last complete value). Returns null when nothing parses.+ */+export function parseJsonLenient(text: string): unknown {+ let raw = text.trim();+ const fence = /^```(?:json)?\s*([\s\S]*?)(?:\s*```)?\s*$/i.exec(raw);+ if (fence) raw = fence[1].trim();+ const start = raw.search(/[[{]/);+ if (start < 0) return null;+ raw = raw.slice(start);+ const end = Math.max(raw.lastIndexOf('}'), raw.lastIndexOf(']'));+ if (end >= 0 && raw.slice(end + 1).trim() === '') raw = raw.slice(0, end + 1);+ try { return JSON.parse(raw); } catch { /* repair below */ }+ // Cut back to the last complete value, close what is open, retry.+ let cut = raw.length;+ for (let attempt = 0; attempt < 60; attempt++) {+ const candidate = closeOpen(raw.slice(0, cut));+ if (candidate) {+ try { return JSON.parse(candidate); } catch { /* keep cutting */ }+ }+ const prev = Math.max(raw.lastIndexOf('}', cut - 2), raw.lastIndexOf(']', cut - 2), raw.lastIndexOf(',', cut - 2));+ if (prev <= 0) break;+ cut = prev + 1;+ }+ return null;+}++/** Drop a trailing partial token, then append the closers for every open bracket. */+function closeOpen(s: string): string | null {+ const stack: string[] = [];+ let inString = false;+ let escaped = false;+ for (const ch of s) {+ if (inString) {+ if (escaped) escaped = false;+ else if (ch === '\\') escaped = true;+ else if (ch === '"') inString = false;+ continue;+ }+ if (ch === '"') inString = true;+ else if (ch === '{') stack.push('}');+ else if (ch === '[') stack.push(']');+ else if (ch === '}' || ch === ']') {+ if (stack.pop() !== ch) return null;+ }+ }+ let out = inString ? s + '"' : s;+ out = out.replace(/,\s*$/, '').replace(/:\s*$/, ': null').replace(/,\s*"[^"]*"?\s*$/, '');+ return out + stack.reverse().join('');+}diff --git a/src/learning/owner-brief.ts b/src/learning/owner-brief.tsnew file mode 100644index 00000000..fc97c904--- /dev/null+++ b/src/learning/owner-brief.ts@@ -0,0 +1,368 @@+/**+ * The owner brief: the two stable prompt sections that tell the chat model+ * who it works for and how to work with them, built from the belief store+ * (docs/owner-model.md §3.5, ADR-026).+ *+ * ## About {owner} — stated facts, preferences, goals, projects,+ * people; inferred ones are hedged+ * ## How to work with {owner} — the working traits turned into instructions+ *+ * Both are regenerated only when the beliefs change (the reflection pass or+ * a per-turn operation), never per turn, so the provider's prompt cache+ * holds. The same text is written to ~/.mercury/soul/owner.md so the owner+ * can read it; lines they add under "## My notes" are read back by the+ * reflection pass as owner-stated facts.+ */+import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { getMercuryHome } from '../utils/config.js';+import { logger } from '../utils/logger.js';+import { SCOPED_DIMENSIONS, TRAIT_DIMENSIONS, type Belief, type BeliefStore } from '../memory/beliefs.js';++export interface OwnerSections {+ about: string;+ howToWork: string;+}++/** Stored in belief_meta under `brief` by the reflection pass. */+export interface StoredBrief extends OwnerSections {+ /** When the reflection wrote it. */+ at: number;+ /** `beliefStateSignature()` at that moment; the deterministic build wins once a belief changes. */+ sig: string;+}++export const OWNER_FILE = 'owner.md';+export const OWNER_NOTES_HEADING = '## My notes';+/** ~4 chars per token: the About section is capped at ~400 tokens, How-to-work at ~250. */+const ABOUT_MAX_CHARS = 1600;+/** How many unverified beliefs the About section may show when nothing is held yet. */+const ABOUT_FALLBACK_LIMIT = 20;+/** Below this many held beliefs the About section pads itself with candidates. */+const ABOUT_MIN_SURE = 6;+const HOW_MAX_CHARS = 1000;+const MAX_NOTE_LINES = 40;++type Usable = Belief & { confidence: number };++// ── sections ─────────────────────────────────────────────────────────────++/**+ * Build both sections. Prefers the reflection's brief (belief_meta `brief`)+ * when it is fresher than the last belief change; otherwise builds them+ * deterministically from the usable beliefs (confirmed first, then held;+ * candidates are never shown).+ */+export function buildOwnerSections(beliefs: BeliefStore, ownerName: string, now = Date.now()): OwnerSections {+ const usable = beliefs.usable(now);+ // Little or nothing held yet (a fresh install, or the first days after an+ // upgrade): the "About" section pads with the strongest candidates, each hedged,+ // so the model is not blind to what it has read. "How to work" stays+ // empty until a trait has actually been held — unverified instructions+ // would be worse than none.+ const forAbout = usable.length >= ABOUT_MIN_SURE ? usable : beliefs.strongest(ABOUT_FALLBACK_LIMIT, now);+ const brief = readStoredBrief(beliefs);+ if (brief && brief.sig === beliefStateSignature(beliefs)) {+ const about = brief.about.trim().slice(0, ABOUT_MAX_CHARS * 2);+ const howToWork = brief.howToWork.trim().slice(0, HOW_MAX_CHARS * 2);+ if (about || howToWork) return { about: about || aboutFromBeliefs(forAbout), howToWork: howToWork || howToWorkFromBeliefs(usable, ownerName) };+ }+ return { about: aboutFromBeliefs(forAbout), howToWork: howToWorkFromBeliefs(usable, ownerName) };+}++export function readStoredBrief(beliefs: BeliefStore): StoredBrief | null {+ const raw = beliefs.getMeta('brief');+ if (!raw) return null;+ try {+ const parsed = JSON.parse(raw) as Partial<StoredBrief>;+ if (typeof parsed.about !== 'string' || typeof parsed.howToWork !== 'string' || typeof parsed.at !== 'number' || typeof parsed.sig !== 'string') return null;+ return { about: parsed.about, howToWork: parsed.howToWork, at: parsed.at, sig: parsed.sig };+ } catch {+ return null;+ }+}++/** Call after every belief change of the pass has been applied: the brief is valid for exactly this state. */+export function storeBrief(beliefs: BeliefStore, brief: OwnerSections, at = Date.now()): void {+ const sig = beliefStateSignature(beliefs);+ beliefs.setMeta('brief', JSON.stringify({ about: brief.about.trim(), howToWork: brief.howToWork.trim(), at, sig } satisfies StoredBrief));+}++/** The latest `updated_at` among live beliefs (0 when there are none). */+export function lastBeliefChange(beliefs: BeliefStore): number {+ const latest = beliefs.list({ statuses: ['candidate', 'held', 'confirmed', 'contested', 'retired'], limit: 1 })[0];+ return latest?.updatedAt ?? 0;+}++/** A cheap signature of the beliefs themselves: status counts and the last change. */+export function beliefStateSignature(beliefs: BeliefStore): string {+ const counts = beliefs.counts();+ return `${Object.values(counts).join(',')}|${lastBeliefChange(beliefs)}`;+}++/**+ * A cheap signature of everything the sections depend on: the beliefs and+ * the stored brief. Recompute the sections only when it changes.+ */+export function beliefSignature(beliefs: BeliefStore): string {+ const brief = beliefs.getMeta('brief');+ return `${beliefStateSignature(beliefs)}|${brief ? brief.length : 0}`;+}++/** Memoised `buildOwnerSections`: one instance per agent, keyed on the belief signature. */+export function createOwnerSectionsMemo(): (beliefs: BeliefStore, ownerName: string) => OwnerSections | null {+ let signature = '';+ let cached: OwnerSections | null = null;+ return (beliefs, ownerName) => {+ let sig: string;+ try {+ sig = `${ownerName}|${beliefSignature(beliefs)}`;+ } catch (err) {+ logger.debug({ err: (err as Error).message }, 'belief signature failed');+ return cached;+ }+ if (sig === signature && cached) return cached;+ try {+ const sections = buildOwnerSections(beliefs, ownerName);+ cached = sections.about || sections.howToWork ? sections : null;+ signature = sig;+ } catch (err) {+ logger.warn({ err: (err as Error).message }, 'owner sections build failed');+ }+ return cached;+ };+}++const ABOUT_KINDS: Array<{ kind: Belief['kind']; label: string }> = [+ { kind: 'fact', label: 'Facts' },+ { kind: 'preference', label: 'Preferences' },+ { kind: 'goal', label: 'Goals' },+ { kind: 'project', label: 'Projects' },+ { kind: 'relationship', label: 'People' },+ { kind: 'situation', label: 'Right now' },+];++/** Confirmed/owner-stated first, then held; strongest first within each. */+function ordered(usable: Usable[]): Usable[] {+ const rank = (b: Usable) => (b.ownerStated || b.status === 'confirmed' ? 0 : 1);+ return [...usable].sort((a, b) => rank(a) - rank(b) || b.confidence - a.confidence);+}++function aboutFromBeliefs(usable: Usable[]): string {+ const lines: string[] = [];+ let chars = 0;+ for (const { kind, label } of ABOUT_KINDS) {+ const items = ordered(usable.filter((b) => b.kind === kind));+ if (items.length === 0) continue;+ const group: string[] = [];+ for (const b of items) {+ const line = aboutLine(b);+ if (chars + line.length > ABOUT_MAX_CHARS) break;+ group.push(line);+ chars += line.length + 1;+ }+ if (group.length === 0) break;+ lines.push(`${label}:`, ...group);+ }+ return lines.join('\n');+}++function aboutLine(b: Usable): string {+ const stated = b.ownerStated || b.status === 'confirmed';+ const where = contextPhrase(b.context);+ const text = sentence(b.statement) + (where ? ` (${where.trim()})` : '');+ if (stated) return `- ${text}`;+ return `- ${text} — seems so, not confirmed`;+}++/** Trait levels as instructions. `ctx` is the context phrase (" in /repo", "" for everywhere). */+const TRAIT_INSTRUCTIONS: Record<string, Record<string, (ctx: string) => string>> = {+ brevity: {+ terse: (c) => `Keep replies short${c}; lead with the result and skip preamble.`,+ balanced: (c) => `Keep replies moderate${c}: the result first, detail only where it matters.`,+ thorough: (c) => `Give thorough replies${c}: include the reasoning and the detail.`,+ },+ autonomy: {+ 'ask-first': (c) => `Ask before acting${c}; confirm the plan first.`,+ 'ask-when-risky': (c) => `Act without asking on reversible steps${c}; ask before anything risky or irreversible.`,+ 'just-do-it': (c) => `Just do it${c}: don't ask for confirmation on routine steps, report when done.`,+ },+ 'decision-style': {+ options: (c) => `Present options with trade-offs${c} rather than a single answer.`,+ recommendation: (c) => `Give one recommendation with the reason${c}, not a menu.`,+ 'decide-for-me': (c) => `Decide and act${c}; mention the choice briefly.`,+ },+ formality: {+ casual: (c) => `A casual tone is fine${c}.`,+ neutral: (c) => `Keep a neutral, plain tone${c}.`,+ formal: (c) => `Keep a formal tone${c}.`,+ },+ 'feedback-style': {+ blunt: () => `They correct bluntly; treat it as information and move on without long apologies.`,+ explains: () => `They explain their corrections; acknowledge the reason and apply it.`,+ 'silent-redo': () => `They tend to redo things rather than complain; check briefly that a result fits before going far.`,+ },+ rhythm: {+ morning: () => `Usually active in the morning; send proactive messages then, not late in the day.`,+ daytime: () => `Usually active during the day; keep proactive messages to working hours.`,+ evening: () => `Usually active in the evening; proactive messages fit then, not in the morning.`,+ 'late-night': () => `Often active late at night; don't assume mornings are a good time to reach them.`,+ },+ expertise: {+ novice: (d) => `New to ${d}: explain from the basics and avoid unexplained jargon.`,+ competent: (d) => `Competent in ${d}: skip the basics, spell out the non-obvious steps.`,+ expert: (d) => `Expert in ${d}: no explanations of basics, assume fluency.`,+ },+ risk: {+ cautious: (d) => `Always confirm before ${d}.`,+ normal: (d) => `Confirm ${d} only when it is irreversible or costly.`,+ bold: (d) => `Don't hold up ${d} for confirmation unless something looks wrong.`,+ },+};++const INSTRUCTION_SHAPE = /^(always|never|don't|do not|never|ask |prefer|use |avoid|keep |no |only |stop |please )/i;++function howToWorkFromBeliefs(usable: Usable[], ownerName: string): string {+ const lines: string[] = [];+ let chars = 0;+ const push = (line: string): boolean => {+ if (chars + line.length > HOW_MAX_CHARS) return false;+ lines.push(line);+ chars += line.length + 1;+ return true;+ };+ // One instruction per (dimension, context), best belief wins.+ const seen = new Set<string>();+ for (const b of ordered(usable.filter((t) => t.kind === 'trait' && t.dimension && t.level))) {+ const key = `${b.dimension}|${b.context}`;+ if (seen.has(key)) continue;+ seen.add(key);+ const text = traitInstruction(b);+ if (!text) continue;+ const hedged = b.ownerStated || b.status === 'confirmed' ? text : `${text} (observed so far)`;+ if (!push(`- ${hedged}`)) break;+ }+ // Confirmed preferences that already read as instructions.+ for (const b of ordered(usable.filter((p) => p.kind === 'preference' && (p.ownerStated || p.status === 'confirmed') && INSTRUCTION_SHAPE.test(p.statement)))) {+ const where = contextPhrase(b.context);+ if (!push(`- ${sentence(b.statement)}${where ? ` (${where.trim()})` : ''}`)) break;+ }+ if (lines.length === 0) return '';+ return lines.join('\n').replace(/\{owner\}/g, ownerName);+}++export function traitInstruction(b: Pick<Belief, 'dimension' | 'level' | 'context' | 'statement'>): string | null {+ if (!b.dimension || !b.level) return null;+ const [base, scope] = splitDimension(b.dimension);+ const table = TRAIT_INSTRUCTIONS[base];+ const make = table?.[b.level];+ if (!make) {+ // Dimensions without a table (e.g. `values`): the statement itself, as an instruction.+ return b.statement ? sentence(b.statement) : null;+ }+ if (base === 'expertise' || base === 'risk') return make(scope || 'this');+ return make(contextPhrase(b.context));+}++function splitDimension(dimension: string): [string, string] {+ const i = dimension.indexOf(':');+ return i === -1 ? [dimension, ''] : [dimension.slice(0, i), dimension.slice(i + 1)];+}++/** `project:/x` → " in /x"; `channel:telegram` → " on telegram"; `domain:x` → " for x"; `*` → "". */+export function contextPhrase(context: string): string {+ if (!context || context === '*') return '';+ const [kind, value] = splitDimension(context);+ if (!value) return ` in ${kind}`;+ switch (kind) {+ case 'project': return ` in ${value}`;+ case 'channel': return ` on ${value}`;+ case 'domain': return ` for ${value}`;+ case 'time': return ` ${value}`;+ default: return ` (${context})`;+ }+}++function sentence(s: string): string {+ const t = s.trim().replace(/\s+/g, ' ');+ if (!t) return '';+ const capped = t[0].toUpperCase() + t.slice(1);+ return /[.!?]$/.test(capped) ? capped.slice(0, -1) : capped;+}++/** Does any dimension/level pair exist? Used by the reflection validator. */+export function isKnownTrait(dimension: string, level: string): boolean {+ const [base, scope] = splitDimension(dimension);+ if (base in TRAIT_DIMENSIONS && !scope) return (TRAIT_DIMENSIONS[base as keyof typeof TRAIT_DIMENSIONS] as readonly string[]).includes(level);+ if (base in SCOPED_DIMENSIONS && scope) return (SCOPED_DIMENSIONS[base as keyof typeof SCOPED_DIMENSIONS] as readonly string[]).includes(level);+ return false;+}++// ── owner.md ─────────────────────────────────────────────────────────────++export function ownerFilePath(home = getMercuryHome()): string {+ return join(home, 'soul', OWNER_FILE);+}++/**+ * Write ~/.mercury/soul/owner.md: the two sections plus whatever the owner+ * wrote under "## My notes" (kept verbatim across rewrites).+ */+export function writeOwnerFile(sections: OwnerSections, ownerName: string, opts: { home?: string; now?: Date } = {}): string {+ const path = ownerFilePath(opts.home);+ const notes = readOwnerNotes({ home: opts.home });+ const when = (opts.now ?? new Date()).toISOString().slice(0, 16).replace('T', ' ');+ const body = [+ `# ${ownerName}`,+ '',+ `<!-- Generated by Mercury from its beliefs about you (last reflection ${when} UTC).`,+ ' Everything above "## My notes" is rewritten at every reflection; edits there are lost.',+ ' Lines you add under "## My notes" are read back at the next reflection as facts you stated',+ ' (they become confirmed beliefs). Use /memory in chat to confirm, correct or forget a belief. -->',+ '',+ `## About ${ownerName}`,+ '',+ sections.about.trim() || '_Nothing held yet._',+ '',+ `## How to work with ${ownerName}`,+ '',+ sections.howToWork.trim() || '_Nothing held yet._',+ '',+ OWNER_NOTES_HEADING,+ '',+ ...(notes.length ? notes.map((n) => `- ${n}`) : ['<!-- One fact per line, e.g. "- I prefer answers in British English." -->']),+ '',+ ].join('\n');+ mkdirSync(join(path, '..'), { recursive: true });+ writeFileSync(path, body, 'utf-8');+ return path;+}++/** Lines the owner wrote under "## My notes" in owner.md (bullets stripped, comments and blanks dropped). */+export function readOwnerNotes(opts: { home?: string } = {}): string[] {+ const path = ownerFilePath(opts.home);+ if (!existsSync(path)) return [];+ let text: string;+ try {+ text = readFileSync(path, 'utf-8');+ } catch {+ return [];+ }+ return parseOwnerNotes(text);+}++export function parseOwnerNotes(text: string): string[] {+ const stripped = text.replace(/<!--[\s\S]*?-->/g, '');+ const idx = stripped.indexOf(OWNER_NOTES_HEADING);+ if (idx === -1) return [];+ const section = stripped.slice(idx + OWNER_NOTES_HEADING.length);+ const out: string[] = [];+ for (const raw of section.split('\n')) {+ if (/^#{1,6}\s/.test(raw)) break;+ const line = raw.replace(/^\s*[-*•]\s+/, '').trim();+ if (!line || line.length < 4) continue;+ out.push(line.slice(0, 200));+ if (out.length >= MAX_NOTE_LINES) break;+ }+ return out;+}diff --git a/src/learning/reflection.test.ts b/src/learning/reflection.test.tsnew file mode 100644index 00000000..0dcd561c--- /dev/null+++ b/src/learning/reflection.test.ts@@ -0,0 +1,330 @@+import { mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, describe, expect, it } from 'vitest';+import { SecondBrainDB, isBetterSqlite3Available } from '../memory/second-brain-db.js';+import { BeliefStore } from '../memory/beliefs.js';+import type { Session, SessionMessage } from '../sessions/types.js';+import {+ applyReflection, buildReflectionPrompt, collectSince, computeStatistics, formatOwnerTurns, parseReflectionOutput,+ runReflection, REFLECTION_SYSTEM_PROMPT, type ReflectionDeps,+} from './reflection.js';+import { buildOwnerSections, createOwnerSectionsMemo, parseOwnerNotes, readStoredBrief, writeOwnerFile } from './owner-brief.js';++const sqliteAvailable = isBetterSqlite3Available();+const dirs: string[] = [];+const T0 = Date.UTC(2026, 9, 20, 9, 0, 0); // 2026-10-20 09:00 UTC+const HOUR = 3_600_000;++function tmp(prefix: string): string {+ const dir = mkdtempSync(join(tmpdir(), prefix));+ dirs.push(dir);+ return dir;+}++function makeStore(): BeliefStore {+ const db = new SecondBrainDB(join(tmp('mercury-reflection-'), 'sb.db'));+ db.init();+ const store = new BeliefStore(db.connection, 'user:owner');+ store.init();+ return store;+}++afterEach(() => { while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true }); });++let seq = 0;+function msg(sessionId: string, role: SessionMessage['role'], content: string, at: number, extra: Partial<SessionMessage> = {}): SessionMessage {+ seq++;+ return { id: `00000000-0000-4000-8000-${String(seq).padStart(12, '0')}`, sessionId, role, kind: 'message', content, timestamp: at, sequence: seq, metadata: { channelType: 'cli', channelId: 'cli' }, ...extra };+}++function session(id: string, title: string, messages: SessionMessage[]): Session {+ const last = messages.at(-1)?.timestamp ?? T0;+ return { id, shortId: id.slice(0, 8), alias: id.slice(0, 4), title, titleSource: 'fallback', createdAt: messages[0]?.timestamp ?? T0, updatedAt: last, status: 'active', revision: 1, bindings: [], messages };+}++const S1 = '11111111-1111-4111-8111-111111111111';+const S2 = '22222222-2222-4222-8222-222222222222';++function fixtureSessions(): Session[] {+ return [+ session(S1, 'Convert the epubs', [+ msg(S1, 'user', 'convert all the epubs in ~/books to markdown', T0),+ msg(S1, 'assistant', 'Done. I used pandoc on 12 files.', T0 + 60_000),+ msg(S1, 'assistant', '[Tool activity in my previous turn]\n- run_command pandoc a.epub → ok\n- run_command pandoc b.epub → ok\n- read_file ~/books/index → ok', T0 + 61_000, { kind: 'tool-call' }),+ msg(S1, 'user', 'too long, just give me the command next time', T0 + 120_000),+ msg(S1, 'user', '/memory', T0 + 130_000),+ msg(S1, 'user', 'internal prompt that must be ignored', T0 + 140_000, { metadata: { channelType: 'internal' } }),+ ]),+ session(S2, 'Deploy', [+ msg(S2, 'user', 'ship it, no need to ask me about each step in this repo', T0 + 13 * HOUR, { metadata: { channelType: 'telegram', channelId: 't1', workCwd: '/repo' } }),+ msg(S2, 'assistant', 'Shipped.', T0 + 13 * HOUR + 1000),+ msg(S2, 'user', 'no, that is not what I asked — explain why the build failed', T0 + 13 * HOUR + 60_000, { metadata: { channelType: 'telegram', channelId: 't1' } }),+ msg(S2, 'system', 'Allow all', T0 + 13 * HOUR + 70_000, { kind: 'permission' }),+ ]),+ ];+}++describe('reflection: collection and statistics', () => {+ it('collects only the owner turns since the watermark, newest first, with channel and cwd', () => {+ const c = collectSince(fixtureSessions(), 0);+ expect(c.turns.map((t) => t.content.slice(0, 12))).toEqual(['no, that is ', 'ship it, no ', 'too long, ju', 'convert all ']);+ expect(c.turns[1].channel).toBe('telegram');+ expect(c.turns[1].cwd).toBe('/repo');+ expect(c.traces).toHaveLength(1);+ expect(c.permissions).toHaveLength(1);+ // Watermark after the first session: only the second session's turns remain.+ const later = collectSince(fixtureSessions(), T0 + 200_000);+ expect(later.turns).toHaveLength(2);+ expect(later.sessions.map((s) => s.id)).toEqual([S2]);+ });++ it('computes the behavioural statistics in code', () => {+ const c = collectSince(fixtureSessions(), 0);+ const hourOf = (at: number) => new Date(at).getUTCHours();+ const s = computeStatistics(c, hourOf);+ expect(s.ownerMessages).toBe(4);+ expect(s.sessions).toHaveLength(2);+ expect(s.sessions.find((x) => x.id === S1)?.ownerMessages).toBe(2);+ const lengths = c.turns.map((t) => t.content.length).sort((a, b) => a - b);+ expect(s.medianOwnerLength).toBe(Math.round((lengths[1] + lengths[2]) / 2));+ expect(s.hourHistogram[9]).toBe(2);+ expect(s.hourHistogram[22]).toBe(2);+ expect(s.toolsUsed).toEqual({ run_command: 2, read_file: 1 });+ expect(s.corrections).toBe(2);+ expect(s.brevityAsks.shorter).toBe(1);+ expect(s.brevityAsks.longer).toBe(1);+ expect(s.permissionAnswers.always).toBe(1);+ });++ it('caps the owner turns at the token budget and truncates long turns', () => {+ const turns = Array.from({ length: 200 }, (_, i) => ({ sessionId: S1, at: T0 + i, channel: 'cli', content: 'x'.repeat(1000) }));+ const text = formatOwnerTurns(turns, 2000);+ expect(text.length).toBeLessThanOrEqual(8000);+ expect(text.split('\n').length).toBeGreaterThan(5);+ expect(text).toContain('…');+ });++ it('the prompt carries the statistics, the beliefs and the rules', () => {+ const c = collectSince(fixtureSessions(), 0);+ const p = buildReflectionPrompt({ ownerName: 'Salman', turns: c.turns, stats: computeStatistics(c, (at) => new Date(at).getUTCHours()), traits: 'id1 | brevity=terse | * | 2/0 | held', others: '', since: 0, now: T0 + 14 * HOUR });+ expect(p).toContain('Owner: Salman');+ expect(p).toContain('brevity=terse');+ expect(p).toContain('Corrections by the owner (regex, approximate): 2');+ expect(p).toContain('[2026-10-20 22:00 telegram 22222222 /repo]');+ expect(REFLECTION_SYSTEM_PROMPT).toContain('Never infer personality types');+ expect(REFLECTION_SYSTEM_PROMPT).toContain('brevity: terse | balanced | thorough');+ });+});++describe('reflection: parsing', () => {+ const known = new Set(['b1', 'b2']);++ it('rejects anything that is not a JSON object', () => {+ expect(parseReflectionOutput('Sure! Here is what I learned...', known)).toBeNull();+ expect(parseReflectionOutput('[]', known)).toBeNull();+ expect(parseReflectionOutput('{"ops": "nope"}', known)).toBeNull();+ });++ it('accepts a fenced object, drops invalid ops and unknown ids, keeps the brief', () => {+ const text = '```json\n' + JSON.stringify({+ ops: [+ { op: 'create', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Wants short replies with the result first', excerpt: 'too long, just give me the command', context: '*' },+ { op: 'create', kind: 'trait', dimension: 'brevity', level: 'very-short', statement: 'bad level', excerpt: 'x' },+ { op: 'create', kind: 'trait', dimension: 'mbti', level: 'intj', statement: 'personality type', excerpt: 'x' },+ { op: 'create', kind: 'trait', dimension: 'risk:publishing', level: 'cautious', statement: 'Wants to be asked before anything is published', excerpt: 'never publish without asking', context: 'bogus context' },+ { op: 'support', beliefId: 'b1', excerpt: 'again', weight: 9 },+ { op: 'support', beliefId: 'zzz', excerpt: 'unknown id' },+ { op: 'create', kind: 'fact', statement: 'short', excerpt: 'x' },+ { op: 'create', kind: 'fact', statement: 'No excerpt means no evidence' },+ { op: 'owner_stated', kind: 'preference', statement: 'Wants Mercury to just act in /repo', context: 'project:/repo', excerpt: 'no need to ask me about each step in this repo' },+ { op: 'explode' },+ ],+ merges: [{ keepId: 'b1', mergeId: 'b2' }, { keepId: 'b1', mergeId: 'b1' }, { keepId: 'b1', mergeId: 'nope' }],+ contested: [{ aId: 'b1', bId: 'b2' }, { aId: 'b1', bId: 'x' }],+ brief: { about: 'Works on Mercury.', howToWork: 'Keep replies short.' },+ }) + '\n```';+ const out = parseReflectionOutput(text, known)!;+ expect(out).not.toBeNull();+ expect(out.ops.map((o) => `${o.op}:${o.dimension ?? o.beliefId ?? o.kind}`)).toEqual(['create:brevity', 'create:risk:publishing', 'support:b1', 'owner_stated:preference']);+ expect(out.ops[1].context).toBe('*');+ expect(out.ops[2].weight).toBe(2);+ expect(out.ops[3].context).toBe('project:/repo');+ expect(out.ops[3].evidenceKind).toBe('owner');+ expect(out.merges).toEqual([{ keepId: 'b1', mergeId: 'b2' }]);+ expect(out.contested).toEqual([{ aId: 'b1', bId: 'b2' }]);+ expect(out.brief).toEqual({ about: 'Works on Mercury.', howToWork: 'Keep replies short.' });+ });+});++describe.skipIf(!sqliteAvailable)('reflection: applying and the owner brief', () => {+ it('applies ops, merges, contested pairs, decay, and the brief; owner.md is written and notes are read back', async () => {+ const store = makeStore();+ const home = tmp('mercury-home-');+ // Seed: a held preference (two sessions), its near-duplicate, and two rival held traits.+ const pref = store.apply({ op: 'create', kind: 'preference', statement: 'Prefers one command over many manual steps', excerpt: 'just run pandoc', sessionId: 'a' }, T0)!;+ store.apply({ op: 'support', beliefId: pref.belief.id, excerpt: 'use the tool', sessionId: 'b' }, T0 + 1);+ const dupe = store.apply({ op: 'create', kind: 'preference', statement: 'Likes a single command that does the whole job rather than steps', excerpt: 'one command', sessionId: 'c' }, T0 + 2)!;+ const fact = store.apply({ op: 'owner_stated', kind: 'fact', statement: 'Founder of Cosmic Stack, an AI-agent lab', excerpt: 'I founded Cosmic Stack', sessionId: 'a' }, T0 + 3)!;+ const tA = store.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Wants Mercury to act without asking', context: 'project:/repo', excerpt: 'ship it', sessionId: 'a' }, T0 + 4)!;+ store.apply({ op: 'support', beliefId: tA.belief.id, excerpt: 'again', sessionId: 'b' }, T0 + 5);+ expect(store.get(tA.belief.id)!.status).toBe('held');++ const scripted = {+ ops: [+ { op: 'create', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Wants short replies that lead with the result', excerpt: 'too long, just give me the command next time', context: '*', weight: 2 },+ { op: 'support', beliefId: pref.belief.id, excerpt: 'convert all the epubs' },+ { op: 'create', kind: 'trait', dimension: 'expertise:typescript', level: 'expert', statement: 'Fluent in TypeScript', excerpt: 'vocabulary in session 1' },+ ],+ merges: [{ keepId: pref.belief.id, mergeId: dupe.belief.id }],+ contested: [],+ brief: { about: 'Founder of Cosmic Stack. Seems to prefer one command over many steps.', howToWork: 'Keep replies short; lead with the result. In /repo, just do it.' },+ };+ let seenPrompt = '';+ const deps: ReflectionDeps = {+ beliefs: store,+ sessions: { list: () => fixtureSessions() },+ providers: { getDefault: () => ({ name: 'scripted', getModel: () => 'm', getModelInstance: () => null }) },+ tokenBudget: { canAfford: () => true, recordUsage: () => {} },+ learningPaused: () => false,+ ownerName: 'Salman',+ home,+ hourOf: (at) => new Date(at).getUTCHours(),+ generate: async ({ prompt }) => { seenPrompt = prompt; return { text: JSON.stringify(scripted), usage: { inputTokens: 10, outputTokens: 5 } }; },+ };+ const r = await runReflection(deps, { now: T0 + 15 * HOUR });+ expect(r.ran).toBe(true);+ expect(r.turns).toBe(4);+ expect(r.applied).toBe(3);+ expect(r.merged).toBe(1);+ expect(r.brief).toBe(true);+ expect(seenPrompt).toContain(pref.belief.id);+ expect(seenPrompt).toContain(tA.belief.id);+ // Merge: the duplicate is retired and superseded; the keeper carries its support.+ expect(store.get(dupe.belief.id)!.status).toBe('retired');+ expect(store.get(dupe.belief.id)!.supersededBy).toBe(pref.belief.id);+ expect(store.get(pref.belief.id)!.support).toBe(4);+ // The new trait is a candidate with the scripted weight.+ const brevity = store.list({ dimension: 'brevity' })[0];+ expect(brevity.level).toBe('terse');+ expect(brevity.status).toBe('candidate');+ expect(brevity.support).toBe(2);+ // Watermark advanced to the newest owner turn; a second run has nothing new.+ expect(Number(store.getMeta('reflection_watermark'))).toBe(T0 + 13 * HOUR + 60_000);+ const again = await runReflection(deps, { now: T0 + 16 * HOUR });+ expect(again.ran).toBe(false);+ expect(again.reason).toBe('nothing-new');+ // The brief was stored and is preferred by the section builder while fresh.+ expect(readStoredBrief(store)?.about).toContain('Founder of Cosmic Stack');+ const sections = buildOwnerSections(store, 'Salman');+ expect(sections.about).toBe('Founder of Cosmic Stack. Seems to prefer one command over many steps.');+ // owner.md was written with both sections and a notes section.+ const path = join(home, 'soul', 'owner.md');+ const file = readFileSync(path, 'utf-8');+ expect(file).toContain('## About Salman');+ expect(file).toContain('## How to work with Salman');+ expect(file).toContain('## My notes');+ // The owner adds notes; the next pass reads them back as confirmed facts, once.+ writeFileSync(path, file + '- I prefer answers in British English\n- Lives in Karachi\n', 'utf-8');+ const withNotes = await runReflection(deps, { now: T0 + 17 * HOUR });+ expect(withNotes.notesApplied).toBe(2);+ const british = store.list().find((b) => /British English/.test(b.statement))!;+ expect(british.status).toBe('confirmed');+ expect(british.ownerStated).toBe(true);+ const third = await runReflection(deps, { now: T0 + 18 * HOUR });+ expect(third.notesApplied).toBe(0);+ expect(store.list().filter((b) => /British English/.test(b.statement))).toHaveLength(1);+ // The notes survive a rewrite of the file.+ expect(readFileSync(path, 'utf-8')).toContain('- Lives in Karachi');+ // A belief change after the brief makes the deterministic build win again.+ expect(buildOwnerSections(store, 'Salman').about).toContain('Lives in Karachi');+ expect(fact.belief.id).toBeTruthy();+ });++ it('does nothing on invalid model output, and skips when paused or over budget', async () => {+ const store = makeStore();+ const base: ReflectionDeps = {+ beliefs: store,+ sessions: { list: () => fixtureSessions() },+ providers: { getDefault: () => ({ name: 'scripted', getModel: () => 'm', getModelInstance: () => null }) },+ tokenBudget: { canAfford: () => true, recordUsage: () => {} },+ learningPaused: () => false,+ ownerName: 'Salman',+ home: tmp('mercury-home-'),+ generate: async () => ({ text: 'I think the owner is an INTJ.' }),+ };+ expect((await runReflection({ ...base, learningPaused: () => true })).reason).toBe('paused');+ expect((await runReflection({ ...base, tokenBudget: { canAfford: () => false, recordUsage: () => {} } })).reason).toBe('budget');+ const r = await runReflection(base, { now: T0 + 15 * HOUR });+ expect(r.ran).toBe(false);+ expect(r.reason).toBe('invalid-output');+ expect(store.list()).toHaveLength(0);+ // The turns were still consumed: no retry loop on a bad answer.+ expect(Number(store.getMeta('reflection_watermark'))).toBeGreaterThan(0);+ });++ it('contested pairs are set only when both are held; decay retires stale candidates', () => {+ const store = makeStore();+ const a = store.apply({ op: 'create', kind: 'trait', dimension: 'decision-style', level: 'options', statement: 'Wants options', context: '*', excerpt: 'give me options', sessionId: 's1' }, T0)!;+ store.apply({ op: 'support', beliefId: a.belief.id, excerpt: 'options please', sessionId: 's2' }, T0 + 1);+ const b = store.apply({ op: 'create', kind: 'trait', dimension: 'decision-style', level: 'recommendation', statement: 'Wants a recommendation', context: 'domain:infra', excerpt: 'what should I do', sessionId: 's1' }, T0 + 2)!;+ store.apply({ op: 'support', beliefId: b.belief.id, excerpt: 'just tell me', sessionId: 's2' }, T0 + 3);+ // An episode from 200 days ago (half-life 30 days) is stale; an undisputed fact would rest instead.+ const stale = store.apply({ op: 'create', kind: 'episode', statement: 'Was travelling last spring for a conference', excerpt: 'x', sessionId: 's1' }, T0 - 200 * 86_400_000)!;+ const counts = applyReflection(store, { ops: [], merges: [], contested: [{ aId: a.belief.id, bId: b.belief.id }], brief: null }, T0 + 10);+ expect(counts.contested).toBe(1);+ expect(store.get(a.belief.id)!.status).toBe('contested');+ expect(store.get(b.belief.id)!.status).toBe('contested');+ expect(counts.retired).toBe(1);+ expect(store.get(stale.belief.id)!.status).toBe('retired');+ });++ it('builds the two sections deterministically from held and confirmed beliefs, and caches on the signature', () => {+ const store = makeStore();+ store.apply({ op: 'owner_stated', kind: 'fact', statement: 'Founder of Cosmic Stack, an AI-agent lab', excerpt: 'x', sessionId: 'a' }, T0);+ store.apply({ op: 'owner_stated', kind: 'preference', statement: 'Never publish a release without asking first', excerpt: 'x', sessionId: 'a' }, T0);+ const proj = store.apply({ op: 'create', kind: 'project', statement: 'Building Mercury, an open-source personal agent', excerpt: 'x', sessionId: 'a' }, T0)!;+ store.apply({ op: 'support', beliefId: proj.belief.id, excerpt: 'y', sessionId: 'b' }, T0 + 1);+ const cand = store.apply({ op: 'create', kind: 'goal', statement: 'Wants to raise a seed round next quarter', excerpt: 'x', sessionId: 'a' }, T0)!;+ const brev = store.apply({ op: 'create', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Wants short replies', context: '*', excerpt: 'x', sessionId: 'a' }, T0)!;+ store.apply({ op: 'support', beliefId: brev.belief.id, excerpt: 'y', sessionId: 'b' }, T0 + 1);+ store.apply({ op: 'owner_stated', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Wants Mercury to just act in the Mercury repo', context: 'project:/Users/me/mercury', excerpt: 'x', sessionId: 'a' }, T0);+ const ts = store.apply({ op: 'create', kind: 'trait', dimension: 'expertise:typescript', level: 'expert', statement: 'Expert in TypeScript', context: '*', excerpt: 'x', sessionId: 'a' }, T0)!;+ store.apply({ op: 'support', beliefId: ts.belief.id, excerpt: 'y', sessionId: 'b' }, T0 + 1);+ const risk = store.apply({ op: 'create', kind: 'trait', dimension: 'risk:publishing', level: 'cautious', statement: 'Cautious about publishing', context: '*', excerpt: 'x', sessionId: 'a' }, T0)!;+ store.apply({ op: 'support', beliefId: risk.belief.id, excerpt: 'y', sessionId: 'b' }, T0 + 1);++ const s = buildOwnerSections(store, 'Salman', T0 + 2);+ expect(s.about).toContain('Facts:\n- Founder of Cosmic Stack, an AI-agent lab');+ expect(s.about).toContain('- Never publish a release without asking first');+ expect(s.about).toContain('- Building Mercury, an open-source personal agent — seems so, not confirmed');+ expect(s.about).not.toContain('seed round'); // candidate+ expect(s.about).not.toContain('short replies'); // traits live in the other section+ expect(s.howToWork).toContain('- Just do it in /Users/me/mercury');+ expect(s.howToWork).toContain('- Keep replies short; lead with the result and skip preamble. (observed so far)');+ expect(s.howToWork).toContain('- Expert in typescript: no explanations of basics, assume fluency. (observed so far)');+ expect(s.howToWork).toContain('- Always confirm before publishing. (observed so far)');+ expect(s.howToWork).toContain('- Never publish a release without asking first');+ expect(s.about.length).toBeLessThanOrEqual(1700);+ expect(s.howToWork.length).toBeLessThanOrEqual(1100);++ const memo = createOwnerSectionsMemo();+ const first = memo(store, 'Salman');+ expect(memo(store, 'Salman')).toBe(first);+ store.confirm(cand.belief.id);+ const second = memo(store, 'Salman');+ expect(second).not.toBe(first);+ expect(second!.about).toContain('seed round');+ });++ it('owner.md: notes are parsed from the notes section only, comments ignored', () => {+ const home = tmp('mercury-home-');+ mkdirSync(join(home, 'soul'), { recursive: true });+ const path = writeOwnerFile({ about: 'A', howToWork: 'B' }, 'Salman', { home, now: new Date(T0) });+ const text = readFileSync(path, 'utf-8');+ expect(text).toContain('Generated by Mercury from its beliefs');+ expect(parseOwnerNotes(text)).toEqual([]);+ expect(parseOwnerNotes(text + '- I run on a Mac\n\n* Kids: two\n<!-- hidden -->\n## Something else\n- not a note\n')).toEqual(['I run on a Mac', 'Kids: two']);+ expect(parseOwnerNotes('no notes heading here')).toEqual([]);+ });+});diff --git a/src/learning/reflection.ts b/src/learning/reflection.tsnew file mode 100644index 00000000..fa57954f--- /dev/null+++ b/src/learning/reflection.ts@@ -0,0 +1,535 @@+/**+ * The reflection pass: the slow, "sleep-time" learning loop+ * (docs/owner-model.md §3.4, ADR-026).+ *+ * Runs from the heartbeat (every 60 min) when at least one session has a new+ * owner message since the last pass, and at shutdown. It reads the owner's+ * messages since the watermark, cheap behavioural statistics computed here+ * in code, and the current trait beliefs, and asks the model ONCE for belief+ * operations, merges, contested pairs and a fresh owner brief. Everything it+ * returns is validated strictly; anything malformed is dropped. Then the+ * store's decay pass runs and owner.md is rewritten.+ *+ * Assistant text is never an input: only the owner's own turns, tool traces+ * (what Mercury did) and permission answers.+ */+import { parseJsonLenient } from './json.js';+import { looksLikeIdentity, looksLikeSecret, ownerWords } from './text.js';+import { generateText } from 'ai';+import { logger } from '../utils/logger.js';+import type { Session, SessionMessage } from '../sessions/types.js';+import type { BeliefKind, BeliefOperation, BeliefStore, BeliefTimescale, EvidenceKind } from '../memory/beliefs.js';+import { TRAIT_DIMENSIONS, SCOPED_DIMENSIONS } from '../memory/beliefs.js';+import { buildOwnerSections, isKnownTrait, readOwnerNotes, storeBrief, writeOwnerFile, type OwnerSections } from './owner-brief.js';++// ── types ────────────────────────────────────────────────────────────────++export interface OwnerTurn {+ sessionId: string;+ at: number;+ channel: string;+ cwd?: string;+ content: string;+}++export interface ReflectionStats {+ sessions: Array<{ id: string; title: string; channel: string; ownerMessages: number; firstAt: number; lastAt: number }>;+ ownerMessages: number;+ medianOwnerLength: number;+ /** Owner messages per local hour of day, 24 buckets. */+ hourHistogram: number[];+ toolsUsed: Record<string, number>;+ corrections: number;+ brevityAsks: { shorter: number; longer: number };+ permissionAnswers: { allowed: number; denied: number; always: number };+}++export interface ReflectionOutput {+ ops: BeliefOperation[];+ merges: Array<{ keepId: string; mergeId: string }>;+ contested: Array<{ aId: string; bId: string }>;+ brief: OwnerSections | null;+}++export interface ReflectionDeps {+ beliefs: BeliefStore;+ sessions: { list(options?: { includeArchived?: boolean }): Session[] };+ providers: { getDefault(): { name: string; getModel(): string; getModelInstance(): unknown } };+ tokenBudget: {+ canAfford(estimatedTokens: number): boolean;+ recordUsage(entry: { provider: string; model: string; inputTokens: number; outputTokens: number; totalTokens: number; channelType: string; agentId?: string }): void;+ };+ learningPaused: () => boolean;+ ownerName: string;+ agentId?: string;+ /** Where owner.md lives (tests); defaults to the Mercury home. */+ home?: string;+ /** Test seam: replaces the model call. */+ generate?: (input: { system: string; prompt: string; maxOutputTokens: number }) => Promise<{ text: string; usage?: { inputTokens?: number; outputTokens?: number } }>;+ /** Test seam: local hour of a timestamp (defaults to Date#getHours). */+ hourOf?: (at: number) => number;+}++export interface ReflectionResult {+ ran: boolean;+ reason?: 'paused' | 'nothing-new' | 'budget' | 'model-error' | 'invalid-output';+ turns: number;+ applied: number;+ merged: number;+ contested: number;+ retired: number;+ notesApplied: number;+ brief: boolean;+}++export const REFLECTION_COST_TOKENS = 6000;+export const REFLECTION_MAX_OUTPUT = 3000;+const TURNS_TOKEN_CAP = 6000;+const TURN_MAX_CHARS = 600;+const MAX_TRAIT_LINES = 60;+const MAX_OTHER_LINES = 50;+const WATERMARK_KEY = 'reflection_watermark';+const LAST_RUN_KEY = 'reflection_last_run';+const NOTES_HASH_KEY = 'owner_notes_hash';++const OP_NAMES = new Set<BeliefOperation['op']>(['create', 'support', 'contradict', 'owner_stated', 'correction']);+const KINDS = new Set<BeliefKind>(['fact', 'preference', 'trait', 'situation', 'relationship', 'goal', 'project', 'episode']);+const TIMESCALES = new Set<BeliefTimescale>(['stable', 'situational', 'episodic']);+const CONTEXT_SHAPE = /^(\*|project:.{1,200}|channel:[a-z0-9-]{1,32}|domain:[a-z0-9 _./-]{1,60}|time:[a-z-]{1,24})$/i;++// ── the prompt ───────────────────────────────────────────────────────────++const TRAIT_TABLE = [+ ...Object.entries(TRAIT_DIMENSIONS).map(([d, levels]) => ` ${d}: ${levels.join(' | ')}`),+ ...Object.entries(SCOPED_DIMENSIONS).map(([d, levels]) => ` ${d}:<domain>: ${levels.join(' | ')} (e.g. ${d}:typescript)`),+].join('\n');++export const REFLECTION_SYSTEM_PROMPT = `You are the reflection pass of a personal AI agent. Between sessions you review what the agent's OWNER wrote and did, and you refine the agent's beliefs about how to work with them. You see only the owner's own messages (never the agent's replies), statistics computed from the sessions, and the beliefs the agent currently holds.++Your output is ONE JSON object and nothing else (no prose, no markdown fences):+{+ "ops": [ <belief operation>, ... ],+ "merges": [ { "keepId": "<id>", "mergeId": "<id>" }, ... ],+ "contested": [ { "aId": "<id>", "bId": "<id>" }, ... ],+ "brief": { "about": "<text>", "howToWork": "<text>" }+}++Belief operations (all fields are strings unless noted):+- { "op": "create", "kind": "<kind>", "statement": "<claim, 12-200 chars>", "context": "<context>", "excerpt": "<what the owner wrote or did that shows it, <=200 chars>", "dimension"?: "<trait dimension>", "level"?: "<trait level>", "detail"?: "<text>", "weight"?: 0.5-2 }+- { "op": "support", "beliefId": "<id>", "excerpt": "<new evidence>", "weight"?: 0.5-2 }+- { "op": "contradict", "beliefId": "<id>", "excerpt": "<counter-evidence>", "weight"?: 0.5-2 }+- { "op": "owner_stated", "kind": "<kind>", "statement": "<claim>", "context": "<context>", "excerpt": "<the owner's words>" } ONLY for something the owner said explicitly about themselves or how they want the agent to work.+- { "op": "correction", "beliefId": "<id>", "statement": "<the corrected claim>", "excerpt": "<the owner's words>" } ONLY when the owner explicitly corrected something the agent believed.++Kinds: fact | preference | trait | situation | relationship | goal | project.+Trait dimensions and their only allowed levels (kind must be "trait", and both "dimension" and "level" are required):+${TRAIT_TABLE}+Contexts: "*" (everywhere), "project:<cwd>", "channel:<type>" (cli, telegram, whatsapp, discord, slack, web, voice), "domain:<topic>".++Rules:+1. Only what the evidence shows. Every op cites an excerpt the owner actually wrote or an action they actually took. If the messages show nothing new, output empty arrays.+2. Prefer fewer, stronger beliefs. Support an existing belief rather than creating a near-duplicate; list true duplicates in "merges" (keep the better-worded, better-supported one).+3. A trait you infer from this batch alone is a candidate: create it with weight 1 unless the pattern repeats across several sessions (then weight up to 2). Hedge in the brief ("seems to", "so far").+4. "contested": two existing beliefs that cannot both be true in the same context. Do not create a new trait level that contradicts a held one on the same dimension and context; contradict the old one instead, with the evidence.+5. Never infer personality types (MBTI, Big Five, enneagram), health, religion, politics, sexuality, ethnicity, or anything about a third person beyond what the owner said. Never record passwords, codes, API keys, tokens, card or account numbers, even if the owner pasted them. Never speculate about mood as a stable trait; a passing state is kind "situation".+6. The brief is for the agent's system prompt. "about": <= 300 words, what the owner does, works on, prefers and cares about; stated facts plainly, inferred ones hedged. "howToWork": <= 180 words of direct instructions derived from the held traits and confirmed preferences (reply length, when to ask before acting and where, options vs a recommendation, tone, what to explain and what to assume per domain, what always needs confirmation, when not to disturb). Address the agent ("Keep replies short…"). Write instructions in plain terms; never quote the statistics (no character counts, percentages, session or message counts, or clock times) and never repeat channel mechanics (how voice replies are formatted is already handled elsewhere). Leave both empty strings when nothing is held.+8. At most 12 operations and 6 merges per pass: the strongest, best-evidenced ones.+9. Refer to the owner by name or as "they"; never assume gender, age or location unless the owner stated it. Their name, email and handles are in Mercury's configuration already; never create a belief that only states who they are or how to reach them.+7. Statements are about the owner, written in the third person without their name ("Prefers…", "Works on…"). Excerpts are quotes or short descriptions of actions, never your own reasoning.`;++export function buildReflectionPrompt(input: {+ ownerName: string;+ turns: OwnerTurn[];+ stats: ReflectionStats;+ traits: string;+ others: string;+ since: number;+ now: number;+}): string {+ const window = input.since > 0 ? `${iso(input.since)} to ${iso(input.now)}` : `everything up to ${iso(input.now)}`;+ const s = input.stats;+ const histogram = describeHours(s.hourHistogram);+ const lengthWord = s.medianOwnerLength === 0 ? 'none' : s.medianOwnerLength < 30 ? 'very short (a few words)' : s.medianOwnerLength < 90 ? 'short (a sentence)' : s.medianOwnerLength < 300 ? 'medium (a paragraph)' : 'long (several paragraphs)';+ const tools = Object.entries(s.toolsUsed).sort((a, b) => b[1] - a[1]).slice(0, 15).map(([t, n]) => `${t}×${n}`).join(', ') || 'none';+ const sessions = s.sessions.slice(0, 20).map((x) => `- ${x.id.slice(0, 8)} [${x.channel}] "${x.title.slice(0, 60)}": ${x.ownerMessages} owner messages, ${iso(x.firstAt)} → ${iso(x.lastAt)}`).join('\n') || '- none';+ return `Owner: ${input.ownerName}. Window: ${window}.++## Statistics (computed, not inferred)+Sessions with new owner messages: ${s.sessions.length}; owner messages: ${s.ownerMessages}; typical owner message: ${lengthWord}.+When the owner writes (local time): ${histogram}+Tools the agent used (from traces): ${tools}+Corrections by the owner (regex, approximate): ${s.corrections}; asks for shorter replies: ${s.brevityAsks.shorter}; asks for more detail: ${s.brevityAsks.longer}.+Permission answers: allowed ${s.permissionAnswers.allowed}, denied ${s.permissionAnswers.denied}, "always/allow all" ${s.permissionAnswers.always}.+Sessions:+${sessions}++## Current trait beliefs (id | dimension=level | context | support/against | status)+${input.traits || '(none)'}++## Other beliefs the agent holds (id | kind | status | statement)+${input.others || '(none)'}++## The owner's messages, most recent first ([time channel session cwd])+${formatOwnerTurns(input.turns) || '(none)'}++Output the JSON object now.`;+}++// ── collection and statistics ────────────────────────────────────────────++const CORRECTION_RE = /^(no[,.!\s]|nope\b|wrong\b|not that\b|that'?s not\b|that is not\b|i meant\b|i said\b|i asked\b|undo\b|revert\b|redo\b|stop\b|don'?t do that\b)|\b(that'?s|that is) (not what|wrong|not it)\b|\bnot what i (asked|meant|wanted|said)\b|\bwhy did you\b|\bi didn'?t (ask|say|want)\b|\btoo (long|verbose|much)\b/i;+const SHORTER_RE = /\b(shorter|too long|too verbose|tl;?dr|be brief|briefer|more concise|less detail|just the (answer|result|command)|stop explaining)\b/i;+const LONGER_RE = /\b(more detail|elaborate|expand on|too short|explain (more|further|why)|walk me through)\b/i;+const TRACE_LINE_RE = /^- ([a-z0-9_:.-]+)/i;++/** Owner turns, tool traces and permission answers newer than `since`, across all sessions. */+export function collectSince(sessions: Session[], since: number): { turns: OwnerTurn[]; traces: SessionMessage[]; permissions: SessionMessage[]; sessions: Session[] } {+ const turns: OwnerTurn[] = [];+ const traces: SessionMessage[] = [];+ const permissions: SessionMessage[] = [];+ const touched: Session[] = [];+ for (const session of sessions) {+ if (session.updatedAt <= since) continue;+ let any = false;+ for (const m of session.messages) {+ if (m.timestamp <= since) continue;+ const channel = typeof m.metadata?.channelType === 'string' ? m.metadata.channelType : 'unknown';+ if (m.role === 'user' && m.kind === 'message') {+ if (channel === 'internal') continue;+ const content = ownerWords(m.content);+ if (!content || content.startsWith('/')) continue;+ const cwd = typeof m.metadata?.workCwd === 'string' ? m.metadata.workCwd : typeof m.metadata?.cwd === 'string' ? m.metadata.cwd : undefined;+ turns.push({ sessionId: session.id, at: m.timestamp, channel, ...(cwd ? { cwd } : {}), content });+ any = true;+ } else if (m.kind === 'tool-call') {+ traces.push(m);+ } else if (m.kind === 'permission') {+ permissions.push(m);+ }+ }+ if (any) touched.push(session);+ }+ turns.sort((a, b) => b.at - a.at);+ return { turns, traces, permissions, sessions: touched };+}++export function computeStatistics(+ collected: { turns: OwnerTurn[]; traces: SessionMessage[]; permissions: SessionMessage[]; sessions: Session[] },+ hourOf: (at: number) => number = (at) => new Date(at).getHours(),+): ReflectionStats {+ const { turns, traces, permissions } = collected;+ const bySession = new Map<string, { count: number; first: number; last: number; channel: string }>();+ for (const t of turns) {+ const cur = bySession.get(t.sessionId) ?? { count: 0, first: t.at, last: t.at, channel: t.channel };+ cur.count++;+ cur.first = Math.min(cur.first, t.at);+ cur.last = Math.max(cur.last, t.at);+ bySession.set(t.sessionId, cur);+ }+ const sessions = collected.sessions.map((s) => {+ const x = bySession.get(s.id) ?? { count: 0, first: s.createdAt, last: s.updatedAt, channel: 'unknown' };+ return { id: s.id, title: s.title, channel: x.channel, ownerMessages: x.count, firstAt: x.first, lastAt: x.last };+ });+ const lengths = turns.map((t) => t.content.length).sort((a, b) => a - b);+ const medianOwnerLength = lengths.length === 0 ? 0 : lengths.length % 2 ? lengths[(lengths.length - 1) / 2] : Math.round((lengths[lengths.length / 2 - 1] + lengths[lengths.length / 2]) / 2);+ const hourHistogram = new Array<number>(24).fill(0);+ for (const t of turns) {+ const h = hourOf(t.at);+ if (h >= 0 && h < 24) hourHistogram[h]++;+ }+ const toolsUsed: Record<string, number> = {};+ for (const m of traces) {+ for (const line of m.content.split('\n')) {+ const match = TRACE_LINE_RE.exec(line);+ if (!match || match[1] === '…') continue;+ toolsUsed[match[1]] = (toolsUsed[match[1]] ?? 0) + 1;+ }+ }+ let corrections = 0;+ const brevityAsks = { shorter: 0, longer: 0 };+ for (const t of turns) {+ if (CORRECTION_RE.test(t.content)) corrections++;+ if (SHORTER_RE.test(t.content)) brevityAsks.shorter++;+ if (LONGER_RE.test(t.content)) brevityAsks.longer++;+ }+ const permissionAnswers = { allowed: 0, denied: 0, always: 0 };+ for (const m of permissions) {+ const text = `${m.content} ${JSON.stringify(m.metadata ?? {})}`;+ if (/allow[ -]?all|always/i.test(text)) permissionAnswers.always++;+ else if (/den(y|ied)|reject|no\b/i.test(text)) permissionAnswers.denied++;+ else if (/allow|approve|yes\b/i.test(text)) permissionAnswers.allowed++;+ }+ return { sessions, ownerMessages: turns.length, medianOwnerLength, hourHistogram, toolsUsed, corrections, brevityAsks, permissionAnswers };+}++/** Most recent first, each turn truncated, the whole capped at ~6k tokens. */+export function formatOwnerTurns(turns: OwnerTurn[], tokenCap = TURNS_TOKEN_CAP): string {+ const lines: string[] = [];+ let chars = 0;+ const cap = tokenCap * 4;+ for (const t of turns) {+ const content = t.content.length > TURN_MAX_CHARS ? `${t.content.slice(0, TURN_MAX_CHARS - 1)}…` : t.content;+ const line = `[${iso(t.at)} ${t.channel} ${t.sessionId.slice(0, 8)}${t.cwd ? ` ${t.cwd}` : ''}] ${content.replace(/\s*\n\s*/g, ' ⏎ ')}`;+ if (chars + line.length > cap) break;+ lines.push(line);+ chars += line.length + 1;+ }+ return lines.join('\n');+}++export function describeBeliefs(beliefs: BeliefStore, now = Date.now()): { traits: string; others: string; ids: Set<string> } {+ const ids = new Set<string>();+ const traits = beliefs.list({ kinds: ['trait'], statuses: ['candidate', 'held', 'confirmed', 'contested'] }).slice(0, MAX_TRAIT_LINES);+ const traitLines = traits.map((b) => {+ ids.add(b.id);+ return `${b.id} | ${b.dimension}=${b.level} | ${b.context} | ${fmt(b.support)}/${fmt(b.against)} | ${b.status}${b.ownerStated ? ' (owner-stated)' : ''} | ${b.statement.slice(0, 100)}`;+ });+ // Usable (held/confirmed) first, then candidates: the model may support,+ // merge or contradict any of them.+ const usable = beliefs.usable(now).filter((b) => b.kind !== 'trait');+ const candidates = beliefs.list({ statuses: ['candidate'] }).filter((b) => b.kind !== 'trait');+ const others = [...usable, ...candidates].slice(0, MAX_OTHER_LINES);+ const otherLines = others.map((b) => {+ ids.add(b.id);+ return `${b.id} | ${b.kind} | ${b.status} | ${b.statement.slice(0, 140)}`;+ });+ return { traits: traitLines.join('\n'), others: otherLines.join('\n'), ids };+}++// ── parsing and validation ───────────────────────────────────────────────++/**+ * Strict parse of the model's output. Returns null when it is not a JSON+ * object of the expected shape; individual ops/merges/pairs that fail+ * validation are dropped. `knownIds` limits references to beliefs we showed.+ */+export function parseReflectionOutput(text: string, knownIds: Set<string>): ReflectionOutput | null {+ const parsed = parseJsonLenient(text);+ if (!isRecord(parsed)) return null;+ const ops: BeliefOperation[] = [];+ if (parsed.ops !== undefined) {+ if (!Array.isArray(parsed.ops)) return null;+ for (const item of parsed.ops) {+ const op = validateOp(item, knownIds);+ if (op) ops.push(op);+ }+ }+ const merges: ReflectionOutput['merges'] = [];+ if (parsed.merges !== undefined) {+ if (!Array.isArray(parsed.merges)) return null;+ for (const m of parsed.merges) {+ if (!isRecord(m) || typeof m.keepId !== 'string' || typeof m.mergeId !== 'string') continue;+ if (m.keepId === m.mergeId || !knownIds.has(m.keepId) || !knownIds.has(m.mergeId)) continue;+ merges.push({ keepId: m.keepId, mergeId: m.mergeId });+ }+ }+ const contested: ReflectionOutput['contested'] = [];+ if (parsed.contested !== undefined) {+ if (!Array.isArray(parsed.contested)) return null;+ for (const c of parsed.contested) {+ if (!isRecord(c) || typeof c.aId !== 'string' || typeof c.bId !== 'string') continue;+ if (c.aId === c.bId || !knownIds.has(c.aId) || !knownIds.has(c.bId)) continue;+ contested.push({ aId: c.aId, bId: c.bId });+ }+ }+ let brief: OwnerSections | null = null;+ if (isRecord(parsed.brief) && typeof parsed.brief.about === 'string' && typeof parsed.brief.howToWork === 'string') {+ const about = parsed.brief.about.trim().slice(0, 2400);+ const howToWork = parsed.brief.howToWork.trim().slice(0, 1500);+ if (about || howToWork) brief = { about, howToWork };+ }+ return { ops, merges, contested, brief };+}++function validateOp(item: unknown, knownIds: Set<string>): BeliefOperation | null {+ if (!isRecord(item) || typeof item.op !== 'string' || !OP_NAMES.has(item.op as BeliefOperation['op'])) return null;+ const op = item.op as BeliefOperation['op'];+ const excerpt = typeof item.excerpt === 'string' && item.excerpt.trim() ? item.excerpt.trim().slice(0, 200) : null;+ if (!excerpt) return null;+ const weight = typeof item.weight === 'number' && Number.isFinite(item.weight) ? Math.min(2, Math.max(0.25, item.weight)) : undefined;+ const sessionId = typeof item.sessionId === 'string' ? item.sessionId : undefined;+ const base: BeliefOperation = { op, excerpt, ...(weight !== undefined ? { weight } : {}), ...(sessionId ? { sessionId } : {}) };+ if (op === 'create' || op === 'owner_stated') {+ const kind = typeof item.kind === 'string' && KINDS.has(item.kind as BeliefKind) ? (item.kind as BeliefKind) : null;+ const statement = typeof item.statement === 'string' ? item.statement.trim() : '';+ if (!kind || kind === 'episode' || statement.length < 8 || statement.length > 240) return null;+ if (looksLikeSecret(statement) || looksLikeIdentity(statement) || looksLikeSecret(String(item.detail ?? ''))) return null;+ const context = typeof item.context === 'string' && CONTEXT_SHAPE.test(item.context.trim()) ? item.context.trim() : '*';+ const out: BeliefOperation = { ...base, kind, statement, context, evidenceKind: op === 'owner_stated' ? 'owner' : 'reflection' };+ if (kind === 'trait') {+ if (typeof item.dimension !== 'string' || typeof item.level !== 'string') return null;+ const dimension = item.dimension.trim().toLowerCase();+ const level = item.level.trim().toLowerCase();+ if (!isKnownTrait(dimension, level)) return null;+ out.dimension = dimension;+ out.level = level;+ }+ if (typeof item.detail === 'string' && item.detail.trim()) out.detail = item.detail.trim().slice(0, 500);+ if (typeof item.timescale === 'string' && TIMESCALES.has(item.timescale as BeliefTimescale)) out.timescale = item.timescale as BeliefTimescale;+ return out;+ }+ if (typeof item.beliefId !== 'string' || !knownIds.has(item.beliefId)) return null;+ const out: BeliefOperation = { ...base, beliefId: item.beliefId, evidenceKind: (op === 'support' ? 'reflection' : 'corrected') as EvidenceKind };+ if (op === 'correction') {+ const statement = typeof item.statement === 'string' ? item.statement.trim() : '';+ if (statement.length >= 8 && statement.length <= 240) out.statement = statement;+ out.evidenceKind = 'owner';+ if (typeof item.level === 'string') out.level = item.level.trim().toLowerCase();+ }+ return out;+}++// ── applying ─────────────────────────────────────────────────────────────++export function applyReflection(beliefs: BeliefStore, output: ReflectionOutput, now = Date.now()): { applied: number; merged: number; contested: number; retired: number } {+ let applied = 0;+ for (const op of output.ops) {+ try {+ if (beliefs.apply(op, now)) applied++;+ } catch (err) {+ logger.debug({ err: (err as Error).message, op: op.op }, 'reflection op failed');+ }+ }+ let merged = 0;+ for (const m of output.merges) {+ if (beliefs.merge(m.keepId, m.mergeId, now)) merged++;+ }+ let contested = 0;+ for (const pair of output.contested) {+ const a = beliefs.get(pair.aId);+ const b = beliefs.get(pair.bId);+ if (!a || !b || a.status !== 'held' || b.status !== 'held') continue;+ beliefs.apply({ op: 'contradict', beliefId: a.id, weight: 1.5, evidenceKind: 'reflection', excerpt: `contested with "${b.statement.slice(0, 120)}"` }, now);+ beliefs.apply({ op: 'contradict', beliefId: b.id, weight: 1.5, evidenceKind: 'reflection', excerpt: `contested with "${a.statement.slice(0, 120)}"` }, now);+ contested++;+ }+ const retired = beliefs.decayPass(now);+ return { applied, merged, contested, retired };+}++/** Lines the owner added under "## My notes" in owner.md become owner-stated facts (once per distinct set of notes). */+export function applyOwnerNotes(beliefs: BeliefStore, notes: string[], now = Date.now()): number {+ if (notes.length === 0) return 0;+ const hash = hashLines(notes);+ if (beliefs.getMeta(NOTES_HASH_KEY) === hash) return 0;+ let applied = 0;+ for (const note of notes) {+ const r = beliefs.apply({ op: 'owner_stated', kind: 'fact', statement: note.slice(0, 240), context: '*', excerpt: `owner.md: ${note.slice(0, 180)}`, evidenceKind: 'owner' }, now);+ if (r && r.change !== 'unchanged') applied++;+ }+ beliefs.setMeta(NOTES_HASH_KEY, hash);+ return applied;+}++// ── the pass ─────────────────────────────────────────────────────────────++export async function runReflection(deps: ReflectionDeps, opts: { now?: number; force?: boolean } = {}): Promise<ReflectionResult> {+ const now = opts.now ?? Date.now();+ const { beliefs } = deps;+ const none = (reason: ReflectionResult['reason'], extra: Partial<ReflectionResult> = {}): ReflectionResult =>+ ({ ran: false, reason, turns: 0, applied: 0, merged: 0, contested: 0, retired: 0, notesApplied: 0, brief: false, ...extra });+ if (deps.learningPaused()) return none('paused');++ const notesApplied = applyOwnerNotes(beliefs, readOwnerNotes({ home: deps.home }), now);++ const since = Number(beliefs.getMeta(WATERMARK_KEY) ?? 0) || 0;+ const collected = collectSince(deps.sessions.list({ includeArchived: true }), since);+ if (collected.turns.length === 0 && !opts.force) {+ if (notesApplied > 0) refreshOwnerFile(deps);+ return none('nothing-new', { notesApplied });+ }+ if (!deps.tokenBudget.canAfford(REFLECTION_COST_TOKENS)) return none('budget', { notesApplied, turns: collected.turns.length });++ const stats = computeStatistics(collected, deps.hourOf);+ const described = describeBeliefs(beliefs, now);+ const prompt = buildReflectionPrompt({ ownerName: deps.ownerName, turns: collected.turns, stats, traits: described.traits, others: described.others, since, now });++ let text: string;+ try {+ const provider = deps.providers.getDefault();+ const result = deps.generate+ ? await deps.generate({ system: REFLECTION_SYSTEM_PROMPT, prompt, maxOutputTokens: REFLECTION_MAX_OUTPUT })+ : await generateText({+ model: provider.getModelInstance() as Parameters<typeof generateText>[0]['model'],+ system: REFLECTION_SYSTEM_PROMPT,+ messages: [{ role: 'user', content: prompt }],+ maxOutputTokens: REFLECTION_MAX_OUTPUT,+ });+ deps.tokenBudget.recordUsage({+ provider: provider.name,+ model: provider.getModel(),+ inputTokens: result.usage?.inputTokens ?? 0,+ outputTokens: result.usage?.outputTokens ?? 0,+ totalTokens: (result.usage?.inputTokens ?? 0) + (result.usage?.outputTokens ?? 0),+ channelType: 'internal',+ agentId: deps.agentId,+ });+ text = result.text;+ } catch (err) {+ logger.warn({ err: (err as Error).message }, 'Reflection: model call failed');+ return none('model-error', { notesApplied, turns: collected.turns.length });+ }++ // The turns were seen whatever the model made of them: advance the watermark+ // so a malformed answer is not retried every hour.+ const newest = collected.turns[0]?.at ?? now;+ beliefs.setMeta(WATERMARK_KEY, String(Math.max(since, newest)));+ beliefs.setMeta(LAST_RUN_KEY, String(now));++ const output = parseReflectionOutput(text, described.ids);+ if (!output) {+ logger.warn({ sample: text.slice(0, 200) }, 'Reflection: output was not valid JSON; nothing applied');+ return none('invalid-output', { notesApplied, turns: collected.turns.length });+ }+ const counts = applyReflection(beliefs, output, now);+ if (output.brief) storeBrief(beliefs, output.brief, now);+ refreshOwnerFile(deps);+ const result: ReflectionResult = { ran: true, turns: collected.turns.length, ...counts, notesApplied, brief: !!output.brief };+ logger.info(result, 'Reflection pass completed');+ return result;+}++function refreshOwnerFile(deps: ReflectionDeps): void {+ try {+ writeOwnerFile(buildOwnerSections(deps.beliefs, deps.ownerName), deps.ownerName, { home: deps.home });+ } catch (err) {+ logger.debug({ err: (err as Error).message }, 'owner.md write failed');+ }+}++// ── helpers ──────────────────────────────────────────────────────────────++function iso(at: number): string {+ return new Date(at).toISOString().slice(0, 16).replace('T', ' ');+}++function fmt(n: number): string {+ return Number.isInteger(n) ? String(n) : n.toFixed(2);+}++function isRecord(v: unknown): v is Record<string, unknown> {+ return typeof v === 'object' && v !== null && !Array.isArray(v);+}++function hashLines(lines: string[]): string {+ let h = 2166136261;+ for (const ch of lines.join('\n')) {+ h ^= ch.charCodeAt(0);+ h = Math.imul(h, 16777619) >>> 0;+ }+ return h.toString(16);+}++/** Hours as words ("mostly late morning and early evening"), never as numbers the brief could echo. */+export function describeHours(hist: number[]): string {+ const total = hist.reduce((a, b) => a + b, 0);+ if (total === 0) return 'no messages';+ const bands: Array<[string, number, number]> = [['early morning', 5, 8], ['morning', 8, 12], ['midday', 12, 14], ['afternoon', 14, 18], ['evening', 18, 22], ['late night', 22, 29]];+ const share = bands.map(([name, from, to]) => {+ let n = 0;+ for (let h = from; h < to; h++) n += hist[h % 24] ?? 0;+ return [name, n / total] as const;+ }).filter(([, p]) => p >= 0.15).sort((a, b) => b[1] - a[1]);+ if (share.length === 0) return 'spread across the day';+ return `mostly ${share.map(([n]) => n).slice(0, 3).join(' and ')}`;+}diff --git a/src/learning/signals.test.ts b/src/learning/signals.test.tsnew file mode 100644index 00000000..b6ebabf8--- /dev/null+++ b/src/learning/signals.test.ts@@ -0,0 +1,88 @@+import { describe, expect, it } from 'vitest';+import {+ collectTurnSignals, describeSignals, detectAutonomyRequest, detectBrevityRequest, detectCorrection,+ detectDecisionStyle, hasBehavioralSignal, normalizeMessage, parseToolTraceLines, timeOfDayBucket,+} from './signals.js';++describe('turn signals', () => {+ it('detects explicit corrections', () => {+ expect(detectCorrection('No, I meant the other file').cues).toContain('no, I meant');+ expect(detectCorrection("That's not what I asked for").cues).toContain("that's not what I asked");+ expect(detectCorrection('I said the config, not the README').cues).toContain('I said');+ expect(detectCorrection('Not that one. The second script.').detected).toBe(true);+ expect(detectCorrection('Do it again but shorter').cues).toContain('again');+ expect(detectCorrection('Thanks again!').detected).toBe(false);+ expect(detectCorrection('Can you list the files in src?').detected).toBe(false);+ });++ it('detects a repeated request within the session', () => {+ const r = detectCorrection('Run the tests please.', ['hi', 'run the tests, please']);+ expect(r.repeatedRequest).toBe(true);+ expect(r.detected).toBe(true);+ expect(detectCorrection('ok', ['ok']).repeatedRequest).toBe(false);+ expect(normalizeMessage(' Run the TESTS, please!! ')).toBe('run the tests please');+ });++ it('detects brevity, autonomy and decision-style requests', () => {+ expect(detectBrevityRequest('tl;dr please')).toBe('shorter');+ expect(detectBrevityRequest('Can you give me more detail on step 2?')).toBe('longer');+ expect(detectBrevityRequest('List the files')).toBeNull();+ expect(detectAutonomyRequest("just do it, don't ask")).toBe('just-do-it');+ expect(detectAutonomyRequest('why did you ask? you can run tests here')).toBe('just-do-it');+ expect(detectAutonomyRequest('ask me first before deleting anything')).toBe('ask-first');+ expect(detectAutonomyRequest("don't ask about reads but ask first before publishing")).toBe('ask-first');+ expect(detectDecisionStyle('give me a few options')).toBe('options');+ expect(detectDecisionStyle('just pick one')).toBe('decide-for-me');+ expect(detectDecisionStyle('what do you recommend?')).toBe('recommendation');+ expect(detectDecisionStyle('fix the bug')).toBeNull();+ });++ it('buckets the local hour', () => {+ const at = (h: number) => { const d = new Date(2026, 9, 24, h, 5); return timeOfDayBucket(d); };+ expect(at(7)).toBe('morning');+ expect(at(14)).toBe('daytime');+ expect(at(20)).toBe('evening');+ expect(at(23)).toBe('late-night');+ expect(at(2)).toBe('late-night');+ });++ it('parses tool trace lines into names and failures', () => {+ const parsed = parseToolTraceLines([+ '- read_file src/a.ts → ok (40 lines)',+ '- run_command npm test → ✗ Command exited with code 1',+ 'not a trace line',+ ]);+ expect(parsed).toEqual([{ tool: 'read_file', failed: false }, { tool: 'run_command', failed: true }]);+ });++ it('collects a full signal object and describes it', () => {+ const s = collectTurnSignals({+ message: "just do it, don't ask",+ previousUserMessages: ['hello'],+ toolTrace: [{ tool: 'run_command', failed: true }, { tool: 'run_command', failed: false }, { tool: 'read_file', failed: false }],+ channelType: 'cli',+ sessionId: 's1',+ cwd: '/tmp/proj',+ allowAllActive: true,+ permissionAnswers: [{ action: 'run_command', answer: 'always' }],+ now: new Date(2026, 9, 24, 9, 0),+ });+ expect(s.autonomy).toBe('just-do-it');+ expect(s.correction.detected).toBe(false);+ expect(s.tools).toEqual({ used: ['run_command', 'read_file'], failed: ['run_command'] });+ expect(s.timeOfDay).toBe('morning');+ expect(s.messageLength).toBe("just do it, don't ask".length);+ expect(hasBehavioralSignal(s)).toBe(true);+ const text = describeSignals(s);+ expect(text).toContain('autonomy request: just-do-it');+ expect(text).toContain('permission answers: run_command=always');+ expect(text).toContain('tools used: run_command, read_file; failed: run_command');+ expect(text).toContain('project cwd: /tmp/proj');+ });++ it('is quiet for an ordinary message', () => {+ const s = collectTurnSignals({ message: 'What time is it in Tokyo?', now: new Date(2026, 9, 24, 15, 0) });+ expect(hasBehavioralSignal(s)).toBe(false);+ expect(describeSignals(s)).toContain('correction: none detected');+ });+});diff --git a/src/learning/signals.ts b/src/learning/signals.tsnew file mode 100644index 00000000..917d5b30--- /dev/null+++ b/src/learning/signals.ts@@ -0,0 +1,177 @@+import { ownerWords } from './text.js';+/**+ * Turn signals: the behavioural evidence one chat turn leaves behind,+ * computed without a model call (docs/owner-model.md §3.3). The per-turn+ * extractor reads them alongside the owner's words; the reflection pass+ * mines them across sessions. Every function here is pure.+ */++export type TimeOfDay = 'morning' | 'daytime' | 'evening' | 'late-night';+export type PermissionAnswer = 'yes' | 'no' | 'always';++export interface TurnSignalInput {+ /** The owner's message this turn. */+ message: string;+ /** Earlier owner messages in the same session (oldest first), for repeat detection. */+ previousUserMessages?: readonly string[];+ /** Permission prompts answered during this turn, when the channel reports them. */+ permissionAnswers?: ReadonlyArray<{ action: string; answer: PermissionAnswer }>;+ /** Session-level Allow All was on for this turn. */+ allowAllActive?: boolean;+ /** Tool calls made this turn and whether each failed. */+ toolTrace?: ReadonlyArray<{ tool: string; failed: boolean }>;+ channelType?: string;+ sessionId?: string;+ cwd?: string;+ /** The turn's clock (local timezone); defaults to now. */+ now?: Date;+}++export interface TurnSignals {+ correction: {+ detected: boolean;+ /** Which cues fired ("no, I meant", "repeated request", …). */+ cues: string[];+ /** The message repeats an earlier owner message in this session. */+ repeatedRequest: boolean;+ };+ /** An explicit ask for a shorter or longer reply. */+ brevity: 'shorter' | 'longer' | null;+ /** An explicit ask about asking before acting. */+ autonomy: 'just-do-it' | 'ask-first' | null;+ /** How the owner wants decisions presented this turn. */+ decisionStyle: 'options' | 'recommendation' | 'decide-for-me' | null;+ permissionAnswers: Array<{ action: string; answer: PermissionAnswer }>;+ allowAllActive: boolean;+ timeOfDay: TimeOfDay;+ hourLocal: number;+ channelType: string | null;+ sessionId: string | null;+ cwd: string | null;+ messageLength: number;+ tools: { used: string[]; failed: string[] };+}++const CORRECTION_CUES: Array<{ cue: string; re: RegExp }> = [+ { cue: 'no, I meant', re: /\bno[,.]?\s+i\s+meant\b/i },+ { cue: "that's not what I asked", re: /\b(that'?s|this is|it'?s)\s+not\s+what\s+i\s+(asked|meant|said|wanted|want)\b/i },+ { cue: 'not that', re: /(^|[.!?,;:]\s*|\bno[,.]?\s+)not\s+that\b/i },+ { cue: 'I said', re: /\bi\s+(already\s+)?(said|told\s+you)\b/i },+ { cue: 'again', re: /(?<!\bthanks?\s)(?<!\bthank\s+you\s)\b(again|once\s+more)\b/i },+ { cue: 'wrong', re: /\b(that'?s|this is|you'?re|you\s+are)\s+wrong\b|\bwrong\s+(file|answer|one|thing)\b/i },+ { cue: 'undo', re: /\b(undo|revert)\s+(that|this|it)\b/i },+];++const SHORTER = /\b(shorter|too\s+long|too\s+verbose|tl;?dr|tldr|brief(er)?|concise|less\s+detail|keep\s+it\s+short|one[\s-]liner?|just\s+the\s+(answer|result|command|code|number))\b/i;+const LONGER = /\b(more\s+detail(s|ed)?|explain|elaborate|expand\s+on|walk\s+me\s+through|in\s+depth|step\s+by\s+step|longer\s+(answer|version))\b/i;++const JUST_DO_IT = /\b(just\s+do\s+it|don[’']?t\s+ask(\s+me)?|no\s+need\s+to\s+ask|stop\s+asking|why\s+(did|do|are)\s+you\s+ask(ing)?|without\s+asking|you\s+don[’']?t\s+(have|need)\s+to\s+ask)\b/i;+// "ask first" / "ask before" count only when not negated ("don't ask before running tests" is the opposite).+const ASK_FIRST = /\b((?<!don[’']?t\s)(?<!never\s)(?<!no\s+need\s+to\s)ask(\s+me)?\s+(first|before)|check\s+with\s+me|always\s+ask|don[’']?t\s+(do|run|change|delete|send|publish)\s+(that|this|anything|it)\s+without\s+(asking|checking))\b/i;++const OPTIONS = /\b(give\s+me\s+(some\s+|a\s+few\s+|the\s+)?options|what\s+are\s+(my|the)\s+options|list\s+(the\s+)?(options|alternatives)|show\s+me\s+(the\s+)?alternatives)\b/i;+const DECIDE_FOR_ME = /\b(just\s+pick\s+(one|something)|pick\s+one(\s+for\s+me)?|you\s+(choose|decide|pick)|decide\s+for\s+me|whatever\s+you\s+think(\s+is)?\s+(best|right)?)\b/i;+const RECOMMENDATION = /\b(what\s+(do|would)\s+you\s+(recommend|suggest)|what\s+should\s+i\b|your\s+recommendation|which\s+(one\s+)?(do|would)\s+you\s+(recommend|suggest|go\s+with))\b/i;++/** Lowercase, strip punctuation, collapse whitespace: the key for "same request again". */+export function normalizeMessage(text: string): string {+ return text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, ' ').replace(/\s+/g, ' ').trim();+}++export function timeOfDayBucket(date: Date): TimeOfDay {+ const h = date.getHours();+ if (h >= 5 && h < 11) return 'morning';+ if (h >= 11 && h < 18) return 'daytime';+ if (h >= 18 && h < 23) return 'evening';+ return 'late-night';+}++export function detectCorrection(message: string, previousUserMessages: readonly string[] = []): TurnSignals['correction'] {+ const cues: string[] = [];+ for (const { cue, re } of CORRECTION_CUES) if (re.test(message)) cues.push(cue);+ const norm = normalizeMessage(message);+ const repeatedRequest = norm.length >= 8 && previousUserMessages.some((m) => normalizeMessage(m) === norm);+ if (repeatedRequest) cues.push('repeated request');+ return { detected: cues.length > 0, cues, repeatedRequest };+}++export function detectBrevityRequest(message: string): TurnSignals['brevity'] {+ if (SHORTER.test(message)) return 'shorter';+ if (LONGER.test(message)) return 'longer';+ return null;+}++export function detectAutonomyRequest(message: string): TurnSignals['autonomy'] {+ // "ask first" wins when both appear ("don't ask about reads, but ask first before deleting").+ if (ASK_FIRST.test(message)) return 'ask-first';+ if (JUST_DO_IT.test(message)) return 'just-do-it';+ return null;+}++export function detectDecisionStyle(message: string): TurnSignals['decisionStyle'] {+ if (DECIDE_FOR_ME.test(message)) return 'decide-for-me';+ if (OPTIONS.test(message)) return 'options';+ if (RECOMMENDATION.test(message)) return 'recommendation';+ return null;+}++/** Parse the turn's tool-trace lines ("- tool args → ✗ …" / "→ ok …") back into tool/failed pairs. */+export function parseToolTraceLines(lines: readonly string[]): Array<{ tool: string; failed: boolean }> {+ const out: Array<{ tool: string; failed: boolean }> = [];+ for (const line of lines) {+ const m = /^-\s*([A-Za-z0-9_.:-]+)/.exec(line);+ if (!m) continue;+ const arrow = line.lastIndexOf('→');+ const outcome = arrow >= 0 ? line.slice(arrow + 1).trim() : '';+ out.push({ tool: m[1], failed: outcome.startsWith('✗') });+ }+ return out;+}++export function collectTurnSignals(input: TurnSignalInput): TurnSignals {+ const message = ownerWords(input.message ?? '');+ const now = input.now ?? new Date();+ const used: string[] = [];+ const failed: string[] = [];+ for (const t of input.toolTrace ?? []) {+ if (!used.includes(t.tool)) used.push(t.tool);+ if (t.failed && !failed.includes(t.tool)) failed.push(t.tool);+ }+ return {+ correction: detectCorrection(message, (input.previousUserMessages ?? []).map(ownerWords) ?? []),+ brevity: detectBrevityRequest(message),+ autonomy: detectAutonomyRequest(message),+ decisionStyle: detectDecisionStyle(message),+ permissionAnswers: (input.permissionAnswers ?? []).map((p) => ({ action: p.action, answer: p.answer })),+ allowAllActive: input.allowAllActive === true,+ timeOfDay: timeOfDayBucket(now),+ hourLocal: now.getHours(),+ channelType: input.channelType ?? null,+ sessionId: input.sessionId ?? null,+ cwd: input.cwd ?? null,+ messageLength: message.length,+ tools: { used, failed },+ };+}++/** True when the turn carried any explicit behavioural request or correction. */+export function hasBehavioralSignal(s: TurnSignals): boolean {+ return s.correction.detected || s.brevity !== null || s.autonomy !== null || s.decisionStyle !== null || s.permissionAnswers.length > 0;+}++/** A compact, line-per-signal rendering for the extraction prompt. */+export function describeSignals(s: TurnSignals): string {+ const lines: string[] = [];+ lines.push(`correction: ${s.correction.detected ? `yes (${s.correction.cues.join(', ')})` : 'none detected'}`);+ if (s.brevity) lines.push(`asked for a ${s.brevity} reply`);+ if (s.autonomy) lines.push(`autonomy request: ${s.autonomy}`);+ if (s.decisionStyle) lines.push(`decision style requested: ${s.decisionStyle}`);+ if (s.permissionAnswers.length > 0) lines.push(`permission answers: ${s.permissionAnswers.map((p) => `${p.action}=${p.answer}`).join(', ')}`);+ if (s.allowAllActive) lines.push('session Allow All is on');+ lines.push(`time: ${s.timeOfDay} (${String(s.hourLocal).padStart(2, '0')}:00 local)`);+ if (s.channelType) lines.push(`channel: ${s.channelType}`);+ if (s.cwd) lines.push(`project cwd: ${s.cwd}`);+ lines.push(`message length: ${s.messageLength} chars`);+ if (s.tools.used.length > 0) lines.push(`tools used: ${s.tools.used.join(', ')}${s.tools.failed.length > 0 ? `; failed: ${s.tools.failed.join(', ')}` : ''}`);+ return lines.join('\n');+}diff --git a/src/learning/text.ts b/src/learning/text.tsnew file mode 100644index 00000000..a9d8b65f--- /dev/null+++ b/src/learning/text.ts@@ -0,0 +1,41 @@+/**+ * The owner's own words, with channel scaffolding removed. Voice-call and+ * desktop turns arrive with bracketed guidance prepended to the user+ * message ("[Voice call: the owner asked this out loud. …]"); that text is+ * Mercury's, not the owner's, and must never become evidence about them.+ */+export function ownerWords(content: string): string {+ let text = content.trim();+ for (let i = 0; i < 4; i++) {+ const m = /^\[[^\]]{0,4000}\]\s*/.exec(text);+ if (!m) break;+ text = text.slice(m[0].length);+ }+ return text.replace(/^\(voice note transcribed\)\s*/i, '').trim();+}++const SECRET_RE = /\b(password|passwd|passcode|pin code|otp|one[- ]time code|api[ _-]?key|secret|token|bearer|private key|seed phrase|recovery (code|phrase)|credit card|cvv|iban|ssn)\b/i;+const SECRET_SHAPE_RE = /\b(sk|pk|ghp|gho|xox[abp]|AKIA)[-_a-z0-9]{12,}\b|\b[a-z0-9]{32,}\b|\d{3}-\d{2}-\d{4}\b/i;++/** Credentials and account secrets are never beliefs, whatever the owner said. */+export function looksLikeSecret(text: string): boolean {+ return SECRET_RE.test(text) || SECRET_SHAPE_RE.test(text);+}++const IDENTITY_RE = /^(?:the owner'?s?|their|owner|user'?s?|user)?\s*(?:full |first |last )?(?:name|email|e-mail|handle|username|phone number|address)\b|\b(?:is named|is called|goes by|name is)\b|[\w.+-]+@[\w-]+\.[\w.]+/i;++/**+ * Who the owner is — name, email, handles — lives in `identity.owner` in the+ * config, the one place it is set. A belief that only restates it would be a+ * second source, so these never become beliefs.+ */+export function looksLikeIdentity(text: string): boolean {+ return IDENTITY_RE.test(text.trim());+}++const SELF_ASK_RE = /\b(what|anything|something|everything|things)\b[^.?!]{0,40}\b(know|remember|learn(?:ed|t)?|notice[d]?)\b[^.?!]{0,30}\babout me\b|\bdo you (?:still )?(?:remember|know) (?:me|who i am|my )|\bwhat (?:are|do you think are) my (?:preferences|habits|traits|goals|projects)\b|\btell me about (?:myself|me)\b|\bwhat have you learn(?:ed|t)\b|\bwhat did you (?:learn|note|save)\b/i;++/** The owner is asking what Mercury knows about them: show the strongest beliefs, not just FTS hits. */+export function asksAboutSelf(text: string): boolean {+ return SELF_ASK_RE.test(ownerWords(text));+}diff --git a/src/memory/beliefs.test.ts b/src/memory/beliefs.test.tsnew file mode 100644index 00000000..de6b4ce7--- /dev/null+++ b/src/memory/beliefs.test.ts@@ -0,0 +1,203 @@+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, describe, expect, it } from 'vitest';+import { SecondBrainDB, isBetterSqlite3Available } from './second-brain-db.js';+import { BeliefStore, RETIRE_BELOW, ftsMatch, isLegacyJunk } from './beliefs.js';++const sqliteAvailable = isBetterSqlite3Available();+const dirs: string[] = [];+const DAY = 86_400_000;++function make(): { store: BeliefStore; db: SecondBrainDB } {+ const dir = mkdtempSync(join(tmpdir(), 'mercury-beliefs-'));+ dirs.push(dir);+ const db = new SecondBrainDB(join(dir, 'sb.db'));+ db.init();+ const store = new BeliefStore(db.connection, 'user:owner');+ store.init();+ return { store, db };+}++afterEach(() => { while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true }); });++describe.skipIf(!sqliteAvailable)('BeliefStore', () => {+ it('a belief starts as a candidate and is held after support from two sessions', () => {+ const { store } = make();+ const t0 = Date.now();+ const r1 = store.apply({ op: 'create', kind: 'preference', statement: 'Prefers one command over many manual steps', excerpt: 'just run pandoc', sessionId: 's1' }, t0)!;+ expect(r1.change).toBe('created');+ expect(r1.belief.status).toBe('candidate');+ expect(store.usable(t0)).toHaveLength(0);+ // Same session again: supported, still a candidate.+ const r2 = store.apply({ op: 'support', beliefId: r1.belief.id, excerpt: 'again', sessionId: 's1' }, t0 + 1000)!;+ expect(r2.belief.status).toBe('candidate');+ // A second session: now held, and behaviour-changing because it's a preference.+ const r3 = store.apply({ op: 'support', beliefId: r1.belief.id, excerpt: 'use the tool', sessionId: 's2' }, t0 + 2000)!;+ expect(r3.change).toBe('now-held');+ expect(r3.behaviorChanging).toBe(true);+ expect(store.usable(t0 + 2000).map((b) => b.id)).toEqual([r1.belief.id]);+ });++ it('creating the same claim again supports instead of duplicating', () => {+ const { store } = make();+ const a = store.apply({ op: 'create', kind: 'fact', statement: 'User works at Cosmic Stack', excerpt: 'x', sessionId: 's1' })!;+ const b = store.apply({ op: 'create', kind: 'fact', statement: 'The user works at Cosmic Stack.', excerpt: 'y', sessionId: 's2' })!;+ expect(b.belief.id).toBe(a.belief.id);+ expect(b.change).toBe('now-held');+ expect(store.list()).toHaveLength(1);+ });++ it('what the owner said is confirmed at once and does not decay', () => {+ const { store } = make();+ const t0 = Date.now();+ const r = store.apply({ op: 'owner_stated', kind: 'preference', statement: 'Always ask before publishing anything', excerpt: 'never publish without asking', sessionId: 's1' }, t0)!;+ expect(r.belief.status).toBe('confirmed');+ expect(r.behaviorChanging).toBe(true);+ expect(store.confidence(r.belief, t0 + 400 * DAY)).toBeGreaterThan(0.8);+ // Counter-evidence is logged but does not unseat the owner's word.+ const c = store.apply({ op: 'contradict', beliefId: r.belief.id, excerpt: 'published without asking once', weight: 2 }, t0 + 1)!;+ expect(c.belief.status).toBe('confirmed');+ expect(store.evidenceFor(r.belief.id)).toHaveLength(2);+ });++ it('inferred beliefs decay and retire unless re-supported', () => {+ const { store } = make();+ const t0 = Date.now();+ const r = store.apply({ op: 'create', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Prefers short replies', excerpt: 'shorter', sessionId: 's1' }, t0)!;+ store.apply({ op: 'support', beliefId: r.belief.id, excerpt: 'tl;dr', sessionId: 's2' }, t0 + 1);+ const fresh = store.confidence(store.get(r.belief.id)!, t0 + 1);+ const later = store.confidence(store.get(r.belief.id)!, t0 + 130 * DAY);+ expect(later).toBeLessThan(fresh);+ expect(later).toBeLessThan(RETIRE_BELOW);+ expect(store.decayPass(t0 + 130 * DAY)).toBe(1);+ expect(store.get(r.belief.id)!.status).toBe('retired');+ expect(store.get(r.belief.id)!.validTo).not.toBeNull();+ // Re-supported: comes back.+ store.apply({ op: 'support', beliefId: r.belief.id, excerpt: 'keep it short', sessionId: 's3' }, t0 + 131 * DAY);+ expect(store.get(r.belief.id)!.status).toBe('held');+ });++ it('an undisputed fact rests for a long time instead of being retired', () => {+ const { store } = make();+ const t0 = Date.now();+ const r = store.apply({ op: 'create', kind: 'fact', statement: 'User is named Salman Qureshi', excerpt: 'x', sessionId: 's1' }, t0)!;+ expect(store.decayPass(t0 + 200 * DAY)).toBe(0);+ expect(store.get(r.belief.id)!.status).toBe('candidate');+ expect(store.confidence(store.get(r.belief.id)!, t0 + 200 * DAY)).toBeGreaterThan(RETIRE_BELOW);+ });++ it('a correction retires the old belief and holds the replacement as owner-stated', () => {+ const { store } = make();+ const old = store.apply({ op: 'create', kind: 'preference', statement: 'Prefers detailed explanations', excerpt: 'x', sessionId: 's1' })!;+ store.apply({ op: 'support', beliefId: old.belief.id, excerpt: 'y', sessionId: 's2' });+ const r = store.apply({ op: 'correction', beliefId: old.belief.id, statement: 'Prefers short answers; asks for detail when wanted', excerpt: "no, I didn't ask for an essay", sessionId: 's3' })!;+ expect(r.change).toBe('replaced');+ expect(r.replaced!.status).toBe('retired');+ expect(r.replaced!.supersededBy).toBe(r.belief.id);+ expect(r.belief.status).toBe('confirmed');+ expect(r.belief.ownerStated).toBe(true);+ expect(store.usable().map((b) => b.id)).toEqual([r.belief.id]);+ });++ it('two held trait levels on the same dimension and context become contested; the owner settles it', () => {+ const { store } = make();+ const terse = store.apply({ op: 'create', kind: 'trait', dimension: 'brevity', level: 'terse', statement: 'Wants short replies', excerpt: 'a', sessionId: 's1' })!;+ store.apply({ op: 'support', beliefId: terse.belief.id, excerpt: 'b', sessionId: 's2' });+ expect(store.get(terse.belief.id)!.status).toBe('held');+ const thorough = store.apply({ op: 'create', kind: 'trait', dimension: 'brevity', level: 'thorough', statement: 'Wants thorough replies', excerpt: 'c', sessionId: 's3' })!;+ expect(store.get(terse.belief.id)!.status).toBe('contested');+ expect(store.get(thorough.belief.id)!.status).toBe('contested');+ expect(store.usable()).toHaveLength(0);+ expect(store.contested()).toHaveLength(1);+ store.settle(terse.belief.id, thorough.belief.id);+ expect(store.get(terse.belief.id)!.status).toBe('confirmed');+ expect(store.get(thorough.belief.id)!.status).toBe('retired');+ expect(store.contested()).toHaveLength(0);+ });++ it('the same trait in a different context is not a contradiction', () => {+ const { store } = make();+ const a = store.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Just runs things in own repos', context: 'project:/repo', excerpt: 'a', sessionId: 's1' })!;+ store.apply({ op: 'support', beliefId: a.belief.id, excerpt: 'b', sessionId: 's2' });+ const b = store.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'ask-first', statement: 'Wants to be asked before publishing', context: 'domain:publishing', excerpt: 'c', sessionId: 's3' })!;+ expect(store.get(a.belief.id)!.status).toBe('held');+ expect(store.get(b.belief.id)!.status).toBe('candidate');+ });++ it('forget hides a belief everywhere, for good', () => {+ const { store } = make();+ const r = store.apply({ op: 'owner_stated', kind: 'fact', statement: 'User lives in Srinagar', excerpt: 'x' })!;+ expect(store.forget(r.belief.id)).toBe(true);+ expect(store.usable()).toHaveLength(0);+ expect(store.list({ statuses: ['candidate', 'held', 'confirmed', 'contested', 'retired'] })).toHaveLength(0);+ expect(store.searchRelevant('Srinagar')).toHaveLength(0);+ expect(store.recentlyLearned(0)).toHaveLength(0);+ expect(store.apply({ op: 'support', beliefId: r.belief.id, excerpt: 'again' })).toBeNull();+ });++ it('acting on a belief without objection adds weak support once per session', () => {+ const { store } = make();+ const r = store.apply({ op: 'create', kind: 'preference', statement: 'Likes a recommendation, not a menu', excerpt: 'x', sessionId: 's1' })!;+ store.actedOnOk([r.belief.id], 's9');+ store.actedOnOk([r.belief.id], 's9');+ expect(store.get(r.belief.id)!.support).toBeCloseTo(1.25);+ });++ it('search finds usable beliefs relevant to a message', () => {+ const { store } = make();+ const r = store.apply({ op: 'owner_stated', kind: 'preference', statement: 'Prefers pandoc for document conversion', excerpt: 'x' })!;+ store.apply({ op: 'create', kind: 'preference', statement: 'Candidate only: likes dark mode', excerpt: 'y' });+ const hits = store.searchRelevant('convert this epub with pandoc please');+ expect(hits.map((b) => b.id)).toEqual([r.belief.id]);+ });++ it('retrieval shows sure beliefs first and fills with candidates, and stopwords never match', () => {+ const { store } = make();+ const sure = store.apply({ op: 'owner_stated', kind: 'preference', statement: 'Prefers pandoc for document conversion', excerpt: 'x' })!;+ const maybe = store.apply({ op: 'create', kind: 'project', statement: 'Is converting the pandoc manual into an epub this week', excerpt: 'y' })!;+ const hits = store.searchRelevant('can you convert this with pandoc for me');+ expect(hits.map((b) => b.id)).toEqual([sure.belief.id, maybe.belief.id]);+ expect(hits[1].status).toBe('candidate');+ // Only stopwords: no query at all, so nothing matches rather than everything.+ expect(store.searchRelevant('what do you want me to do with this')).toEqual([]);+ expect(ftsMatch('the and for you')).toBeNull();+ expect(ftsMatch('Convert the EPUB, please — the epub!')).toBe('"convert" OR "epub"');+ });++ it('strongest() lists sure beliefs before candidates for "what do you know about me"', () => {+ const { store } = make();+ store.apply({ op: 'create', kind: 'goal', statement: 'Wants Mercury to run the launch', excerpt: 'a' });+ const sure = store.apply({ op: 'owner_stated', kind: 'fact', statement: 'Runs Cosmic Stack', excerpt: 'b' })!;+ const top = store.strongest(5);+ expect(top[0].id).toBe(sure.belief.id);+ expect(top).toHaveLength(2);+ expect(top[1].status).toBe('candidate');+ });++ it('migrates legacy memories: junk is quarantined, real ones become beliefs, and it runs once', () => {+ const { store, db } = make();+ const now = Date.now();+ const base = { user_key: 'user:owner', categories: '["general"]', shareable: 0, detail: null, scope: 'durable', source: 'conversation', importance: 0.7, durability: 0.7, provenance: null, dismissed: 0, superseded_by: null, created_at: now, updated_at: now, last_seen_at: now, last_used_at: null, last_used_query: null } as const;+ const rows = [+ { id: 'm1', type: 'preference', summary: "I appreciate the detailed instructions, but I need to clarify my actual capabilities: I'm Claude", evidence_kind: 'inferred', confidence: 0.75, evidence_count: 1 },+ { id: 'm2', type: 'preference', summary: 'Open Slack on your computer', evidence_kind: 'inferred', confidence: 0.75, evidence_count: 1 },+ { id: 'm3', type: 'preference', summary: '**Memory extracted:**', evidence_kind: 'inferred', confidence: 0.75, evidence_count: 1 },+ { id: 'm4', type: 'preference', summary: 'User prefers pragmatic, incremental implementation over full specification adoption', evidence_kind: 'inferred', confidence: 0.8, evidence_count: 1 },+ { id: 'm5', type: 'identity', summary: "User's name is Salman and he founded Cosmic Stack", evidence_kind: 'direct', confidence: 0.95, evidence_count: 1 },+ { id: 'm6', type: 'identity', summary: 'User founded Cosmic Stack, an AI agency', evidence_kind: 'direct', confidence: 0.95, evidence_count: 1 },+ ];+ for (const r of rows) db.insert({ ...base, ...r } as never);+ const first = store.migrateLegacyMemories(now);+ // m5 names the owner: identity lives in the config, so it waits in quarantine instead.+ expect(first).toEqual({ quarantined: 4, converted: 2, skipped: false });+ const quarantined = db.connection.prepare("SELECT id, provenance FROM memories WHERE scope = 'quarantine' ORDER BY id").all() as Array<{ id: string; provenance: string }>;+ expect(quarantined.map((q) => q.id)).toEqual(['m1', 'm2', 'm3', 'm5']);+ expect(quarantined.find((q) => q.id === 'm5')?.provenance).toBe('migration:identity');+ const beliefs = store.list({ statuses: ['candidate', 'held', 'confirmed'] });+ expect(beliefs.map((b) => [b.kind, b.status]).sort()).toEqual([['fact', 'candidate'], ['preference', 'candidate']]);+ expect(store.getMeta('reflection_force')).toBe('1');+ expect(store.migrateLegacyMemories(now).skipped).toBe(true);+ expect(isLegacyJunk({ summary: 'User likes interactive 3D animations in web projects', type: 'preference', evidence_kind: 'inferred', confidence: 0.85, categories: '["technical"]' })).toBe(false);+ });+});diff --git a/src/memory/beliefs.ts b/src/memory/beliefs.tsnew file mode 100644index 00000000..f09815fe--- /dev/null+++ b/src/memory/beliefs.ts@@ -0,0 +1,761 @@+/**+ * Beliefs: what Mercury thinks it knows about its owner, with evidence.+ *+ * A belief is a claim ("prefers one command over many manual steps") held+ * with a confidence that comes from counted evidence for and against it,+ * decays unless re-supported, applies in a context, and has a validity+ * window. Nothing is deleted: a belief that stops being true is retired or+ * superseded, and the owner can see why. Design: docs/owner-model.md (ADR-026).+ *+ * The tables live in the Second Brain database next to the legacy+ * `memories` table, which keeps the people graph and the dashboard's+ * history; the per-turn and reflection learning loops write here.+ */+import { randomUUID } from 'node:crypto';+import type { SqliteDatabase } from '../utils/sqlite-driver.js';+import { logger } from '../utils/logger.js';+import { looksLikeIdentity } from '../learning/text.js';++export type BeliefKind = 'fact' | 'preference' | 'trait' | 'situation' | 'relationship' | 'goal' | 'project' | 'episode';+export type BeliefStatus = 'candidate' | 'held' | 'confirmed' | 'contested' | 'retired' | 'forgotten';+export type BeliefTimescale = 'stable' | 'situational' | 'episodic';+export type EvidenceKind = 'said' | 'corrected' | 'approved' | 'denied' | 'reacted' | 'observed' | 'acted-on-ok' | 'owner' | 'reflection' | 'migration';++/** The working-trait dimensions the reflection pass infers (docs/owner-model.md §3.2). */+export const TRAIT_DIMENSIONS = {+ brevity: ['terse', 'balanced', 'thorough'],+ autonomy: ['ask-first', 'ask-when-risky', 'just-do-it'],+ 'decision-style': ['options', 'recommendation', 'decide-for-me'],+ formality: ['casual', 'neutral', 'formal'],+ 'feedback-style': ['blunt', 'explains', 'silent-redo'],+ rhythm: ['morning', 'daytime', 'evening', 'late-night'],+} as const;+/** Dimensions with a free suffix: `expertise:typescript`, `risk:publishing`. */+export const SCOPED_DIMENSIONS = {+ expertise: ['novice', 'competent', 'expert'],+ risk: ['cautious', 'normal', 'bold'],+} as const;++export interface Belief {+ id: string;+ ownerKey: string;+ kind: BeliefKind;+ /** Traits only: the dimension (e.g. `brevity`, `risk:publishing`). */+ dimension: string | null;+ /** Traits only: the inferred level on that dimension. */+ level: string | null;+ statement: string;+ detail: string | null;+ /** Where it applies: `*`, `channel:telegram`, `project:<cwd>`, `domain:video`… */+ context: string;+ support: number;+ against: number;+ /** Distinct sessions that supported it (a belief is held only after two). */+ supportSessions: number;+ timescale: BeliefTimescale;+ status: BeliefStatus;+ ownerStated: boolean;+ validFrom: number;+ validTo: number | null;+ supersededBy: string | null;+ lastSupportedAt: number;+ lastUsedAt: number | null;+ createdAt: number;+ updatedAt: number;+ /** Offered to the cloud shared pool (the owner's share-learning toggle). */+ shareable: boolean;+}++export interface Evidence {+ id: string;+ beliefId: string;+ kind: EvidenceKind;+ sessionId: string | null;+ turnRef: string | null;+ excerpt: string;+ weight: number;+ at: number;+}++export interface BeliefOperation {+ op: 'create' | 'support' | 'contradict' | 'owner_stated' | 'correction';+ /** For support/contradict/correction: the belief it refers to (by id). */+ beliefId?: string;+ kind?: BeliefKind;+ dimension?: string;+ level?: string;+ statement?: string;+ detail?: string;+ context?: string;+ timescale?: BeliefTimescale;+ /** What the owner said or did that is the evidence (≤ 200 chars). */+ excerpt: string;+ evidenceKind?: EvidenceKind;+ weight?: number;+ sessionId?: string;+ turnRef?: string;+ shareable?: boolean;+}++export interface ApplyResult {+ belief: Belief;+ /** What happened, in words the announcer can use. */+ change: 'created' | 'supported' | 'now-held' | 'contradicted' | 'contested' | 'confirmed' | 'replaced' | 'unchanged';+ /** True when this change should alter how Mercury behaves from now on. */+ behaviorChanging: boolean;+ /** The belief this one replaced (corrections). */+ replaced?: Belief;+}++const HALF_LIFE_DAYS: Record<BeliefTimescale, number> = { stable: 60, situational: 0.5, episodic: 30 };+/** Stable beliefs fade at different speeds: who the owner is barely changes; how they want to work does. */+const KIND_HALF_LIFE_DAYS: Partial<Record<BeliefKind, number>> = { fact: 365, relationship: 365, preference: 120, goal: 90, project: 90, trait: 60 };+function halfLifeDays(b: Belief): number {+ return b.timescale === 'stable' ? (KIND_HALF_LIFE_DAYS[b.kind] ?? HALF_LIFE_DAYS.stable) : HALF_LIFE_DAYS[b.timescale];+}+const DAY = 86_400_000;+export const RETIRE_BELOW = 0.35;++/** Words that match almost every belief and only add noise to an OR query. */+const STOPWORDS = new Set(['the', 'and', 'for', 'that', 'this', 'with', 'you', 'your', 'are', 'was', 'were', 'have', 'has', 'had', 'can', 'could', 'would', 'should', 'will', 'what', 'when', 'where', 'which', 'who', 'how', 'why', 'not', 'but', 'all', 'any', 'some', 'just', 'like', 'about', 'from', 'into', 'than', 'then', 'them', 'they', 'their', 'there', 'here', 'also', 'very', 'much', 'more', 'most', 'does', 'did', 'doing', 'done', 'please', 'thanks', 'okay', 'yeah', 'hey', 'want', 'need', 'make', 'get', 'got', 'let', 'use', 'using', 'one', 'two', 'now', 'out', 'its', 'our', 'mine', 'yours', 'me', 'my', 'we', 'us', 'it', 'is', 'be', 'do', 'to', 'of', 'in', 'on', 'at', 'by', 'or', 'an', 'as', 'if', 'so', 'no', 'up', 'am']);++/** An FTS5 OR-query from free text: lower-cased, punctuation out, stopwords out, deduped, capped. */+export function ftsMatch(text: string, opts: { minLength?: number; max?: number } = {}): string | null {+ const minLength = opts.minLength ?? 3;+ const seen = new Set<string>();+ const terms: string[] = [];+ for (const raw of text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, ' ').split(/\s+/)) {+ if (raw.length < minLength || STOPWORDS.has(raw) || seen.has(raw)) continue;+ seen.add(raw);+ terms.push(raw);+ if (terms.length >= (opts.max ?? 24)) break;+ }+ return terms.length === 0 ? null : terms.map((t) => `"${t}"`).join(' OR ');+}+const HELD_AT_SESSIONS = 2;++interface BeliefRow {+ id: string; owner_key: string; kind: BeliefKind; dimension: string | null; level: string | null;+ statement: string; detail: string | null; context: string; support: number; against: number;+ support_sessions: number; timescale: BeliefTimescale; status: BeliefStatus; owner_stated: number;+ valid_from: number; valid_to: number | null; superseded_by: string | null; last_supported_at: number;+ last_used_at: number | null; created_at: number; updated_at: number; shareable?: number;+}++export class BeliefStore {+ constructor(private db: SqliteDatabase, private ownerKey: string = 'user:owner') {}++ init(): void {+ this.db.exec(`+ CREATE TABLE IF NOT EXISTS beliefs (+ id TEXT PRIMARY KEY,+ owner_key TEXT NOT NULL,+ kind TEXT NOT NULL,+ dimension TEXT,+ level TEXT,+ statement TEXT NOT NULL,+ detail TEXT,+ context TEXT NOT NULL DEFAULT '*',+ support REAL NOT NULL DEFAULT 0,+ against REAL NOT NULL DEFAULT 0,+ support_sessions INTEGER NOT NULL DEFAULT 0,+ timescale TEXT NOT NULL DEFAULT 'stable',+ status TEXT NOT NULL DEFAULT 'candidate',+ owner_stated INTEGER NOT NULL DEFAULT 0,+ valid_from INTEGER NOT NULL,+ valid_to INTEGER,+ superseded_by TEXT,+ last_supported_at INTEGER NOT NULL,+ last_used_at INTEGER,+ created_at INTEGER NOT NULL,+ updated_at INTEGER NOT NULL+ );+ CREATE INDEX IF NOT EXISTS idx_beliefs_owner_status ON beliefs(owner_key, status);+ CREATE INDEX IF NOT EXISTS idx_beliefs_owner_dim ON beliefs(owner_key, dimension, context);+ CREATE VIRTUAL TABLE IF NOT EXISTS beliefs_fts USING fts5(statement, detail, content=beliefs, content_rowid=rowid);+ CREATE TRIGGER IF NOT EXISTS beliefs_ai AFTER INSERT ON beliefs BEGIN+ INSERT INTO beliefs_fts(rowid, statement, detail) VALUES (new.rowid, new.statement, new.detail);+ END;+ CREATE TRIGGER IF NOT EXISTS beliefs_ad AFTER DELETE ON beliefs BEGIN+ INSERT INTO beliefs_fts(beliefs_fts, rowid, statement, detail) VALUES('delete', old.rowid, old.statement, old.detail);+ END;+ CREATE TRIGGER IF NOT EXISTS beliefs_au AFTER UPDATE ON beliefs BEGIN+ INSERT INTO beliefs_fts(beliefs_fts, rowid, statement, detail) VALUES('delete', old.rowid, old.statement, old.detail);+ INSERT INTO beliefs_fts(rowid, statement, detail) VALUES (new.rowid, new.statement, new.detail);+ END;+ CREATE TABLE IF NOT EXISTS belief_evidence (+ id TEXT PRIMARY KEY,+ belief_id TEXT NOT NULL,+ kind TEXT NOT NULL,+ session_id TEXT,+ turn_ref TEXT,+ excerpt TEXT NOT NULL,+ weight REAL NOT NULL DEFAULT 1,+ at INTEGER NOT NULL,+ FOREIGN KEY (belief_id) REFERENCES beliefs(id) ON DELETE CASCADE+ );+ CREATE INDEX IF NOT EXISTS idx_belief_evidence_belief ON belief_evidence(belief_id, at);+ CREATE TABLE IF NOT EXISTS belief_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);+ `);+ const cols = this.db.prepare('PRAGMA table_info(beliefs)').all() as Array<{ name: string }>;+ if (!cols.some((c) => c.name === 'shareable')) this.db.exec('ALTER TABLE beliefs ADD COLUMN shareable INTEGER NOT NULL DEFAULT 0');+ }++ // ── confidence ─────────────────────────────────────────────────────────++ /** Beta-mean of the evidence, decayed by time since the last support. Confirmed/owner-stated beliefs don't decay. */+ confidence(b: Belief, now = Date.now()): number {+ const mean = (b.support + 1) / (b.support + b.against + 2);+ if (b.ownerStated || b.status === 'confirmed') return Math.min(0.98, Math.max(mean, 0.85));+ const ageDays = Math.max(0, now - b.lastSupportedAt) / DAY;+ const decay = Math.pow(0.5, ageDays / halfLifeDays(b));+ return mean * decay;+ }++ // ── writes ─────────────────────────────────────────────────────────────++ /** Apply one belief operation from the learning loops. */+ apply(op: BeliefOperation, now = Date.now()): ApplyResult | null {+ const weight = op.weight ?? 1;+ if (op.op === 'create' || op.op === 'owner_stated') {+ if (!op.statement || !op.kind) return null;+ const existing = this.findSameClaim(op.statement, op.kind, op.context ?? '*', op.dimension);+ if (existing) {+ if (op.op !== 'owner_stated') return this.apply({ ...op, op: 'support', beliefId: existing.id, statement: undefined }, now);+ // The owner stated a claim Mercury already held or suspected: confirm it (once).+ if (existing.ownerStated || existing.status === 'confirmed') return { belief: existing, change: 'unchanged', behaviorChanging: false };+ this.update(existing.id, { status: 'confirmed', ownerStated: true, support: existing.support + weight, lastSupportedAt: now, validTo: null }, now);+ this.addEvidence(existing.id, op.evidenceKind ?? 'owner', op, weight, now);+ for (const rival of this.rivals(this.get(existing.id)!)) this.update(rival.id, { status: 'retired', validTo: now, supersededBy: existing.id }, now);+ const belief = this.get(existing.id)!;+ return { belief, change: 'confirmed', behaviorChanging: isBehavioral(belief) };+ }+ const ownerStated = op.op === 'owner_stated';+ const belief = this.insert({+ kind: op.kind, dimension: op.dimension ?? null, level: op.level ?? null, statement: op.statement.trim(),+ detail: op.detail?.trim() ?? null, context: op.context ?? '*', timescale: op.timescale ?? (op.kind === 'situation' ? 'situational' : op.kind === 'episode' ? 'episodic' : 'stable'),+ support: weight, against: 0, supportSessions: op.sessionId ? 1 : 0, ownerStated,+ status: ownerStated ? 'confirmed' : 'candidate', shareable: op.shareable === true,+ }, now);+ this.addEvidence(belief.id, op.evidenceKind ?? (ownerStated ? 'owner' : 'said'), op, weight, now);+ this.settleContest(belief, now);+ return { belief: this.get(belief.id)!, change: ownerStated ? 'confirmed' : 'created', behaviorChanging: ownerStated && isBehavioral(belief) };+ }+ const target = op.beliefId ? this.get(op.beliefId) : null;+ if (!target || target.status === 'forgotten') return null;+ if (op.op === 'support') {+ const newSession = op.sessionId ? !this.sessionSupported(target.id, op.sessionId) : false;+ const supportSessions = target.supportSessions + (newSession ? 1 : 0);+ const wasHeld = target.status === 'held' || target.status === 'confirmed';+ let status = target.status;+ if (status === 'candidate' && (supportSessions >= HELD_AT_SESSIONS || target.ownerStated)) status = 'held';+ if (status === 'retired') status = supportSessions >= HELD_AT_SESSIONS ? 'held' : 'candidate';+ this.update(target.id, { support: target.support + weight, supportSessions, status, lastSupportedAt: now, validTo: null }, now);+ this.addEvidence(target.id, op.evidenceKind ?? 'said', op, weight, now);+ const belief = this.get(target.id)!;+ const nowHeld = !wasHeld && status === 'held';+ return { belief, change: nowHeld ? 'now-held' : 'supported', behaviorChanging: nowHeld && isBehavioral(belief) };+ }+ if (op.op === 'contradict') {+ const against = target.against + weight;+ let status = target.status;+ if (target.status === 'confirmed' || target.ownerStated) {+ // What the owner said stands until they say otherwise; log the counter-evidence only.+ } else if (target.status === 'held' && against >= 1.5) status = 'contested';+ else if (target.status === 'candidate' && against >= target.support) status = 'retired';+ this.update(target.id, { against, status, validTo: status === 'retired' ? now : target.validTo }, now);+ this.addEvidence(target.id, op.evidenceKind ?? 'corrected', op, weight, now);+ const belief = this.get(target.id)!;+ return { belief, change: status === 'contested' ? 'contested' : 'contradicted', behaviorChanging: status === 'contested' && isBehavioral(belief) };+ }+ if (op.op === 'correction') {+ // The owner corrected a belief: retire it and hold the replacement as owner-stated.+ if (!op.statement) return this.apply({ ...op, op: 'contradict', weight: 2, evidenceKind: 'corrected' }, now);+ this.update(target.id, { status: 'retired', validTo: now, against: target.against + 2 }, now);+ this.addEvidence(target.id, 'corrected', op, 2, now);+ const replacement = this.insert({+ kind: op.kind ?? target.kind, dimension: op.dimension ?? target.dimension, level: op.level ?? null, statement: op.statement.trim(),+ detail: op.detail?.trim() ?? null, context: op.context ?? target.context, timescale: op.timescale ?? target.timescale,+ support: 2, against: 0, supportSessions: op.sessionId ? 1 : 0, ownerStated: true, status: 'confirmed', shareable: op.shareable === true || target.shareable,+ }, now);+ this.update(target.id, { supersededBy: replacement.id }, now);+ this.addEvidence(replacement.id, 'owner', op, 2, now);+ return { belief: this.get(replacement.id)!, change: 'replaced', behaviorChanging: isBehavioral(replacement), replaced: this.get(target.id)! };+ }+ return null;+ }++ /** The owner confirmed a belief ("yes, that's right"). */+ confirm(id: string, now = Date.now()): Belief | null {+ const b = this.get(id);+ if (!b || b.status === 'forgotten') return null;+ this.update(id, { status: 'confirmed', ownerStated: true, support: b.support + 2, lastSupportedAt: now, validTo: null }, now);+ this.addEvidence(id, 'owner', { excerpt: 'confirmed by the owner' }, 2, now);+ // A confirmed belief settles any contest on the same dimension/context.+ for (const rival of this.rivals(this.get(id)!)) this.update(rival.id, { status: 'retired', validTo: now, supersededBy: id }, now);+ return this.get(id);+ }++ /** The owner asked Mercury to forget it: hidden from every prompt and list, for good. */+ forget(id: string, now = Date.now()): boolean {+ const b = this.get(id);+ if (!b) return false;+ this.update(id, { status: 'forgotten', validTo: now }, now);+ this.addEvidence(id, 'owner', { excerpt: 'forgotten at the owner\'s request' }, 0, now);+ return true;+ }++ /** The owner settled a contest: the winner is confirmed, the loser retired. */+ settle(winnerId: string, loserId: string, now = Date.now()): void {+ this.confirm(winnerId, now);+ const loser = this.get(loserId);+ if (loser && loser.status !== 'forgotten') this.update(loserId, { status: 'retired', validTo: now, supersededBy: winnerId }, now);+ }++ markUsed(ids: string[], now = Date.now()): void {+ const stmt = this.db.prepare('UPDATE beliefs SET last_used_at = ? WHERE id = ?');+ for (const id of ids) stmt.run(now, id);+ }++ /** Mercury acted on these beliefs and the owner didn't object: weak support. */+ actedOnOk(ids: string[], sessionId?: string, now = Date.now()): void {+ for (const id of ids) {+ const b = this.get(id);+ if (!b || b.status === 'forgotten' || b.status === 'retired') continue;+ if (sessionId && this.sessionSupported(id, sessionId)) continue;+ this.update(id, { support: b.support + 0.25, lastSupportedAt: now }, now);+ this.addEvidence(id, 'acted-on-ok', { excerpt: 'acted on without objection', sessionId }, 0.25, now);+ }+ }++ /** Retire beliefs whose confidence decayed below the floor. Returns how many. */+ decayPass(now = Date.now()): number {+ let retired = 0;+ for (const b of this.list({ statuses: ['candidate', 'held', 'contested'] })) {+ // Retire only what faded past the floor AND was either contradicted or+ // not seen for two half-lives; a true fact nobody disputed just rests.+ const stale = (now - b.lastSupportedAt) / DAY >= 2 * halfLifeDays(b);+ if (this.confidence(b, now) < RETIRE_BELOW && (b.against > 0 || stale)) {+ this.update(b.id, { status: 'retired', validTo: now }, now);+ retired++;+ }+ }+ return retired;+ }++ // ── reads ──────────────────────────────────────────────────────────────++ get(id: string): Belief | null {+ const row = this.db.prepare('SELECT * FROM beliefs WHERE id = ? AND owner_key = ?').get(id, this.ownerKey) as BeliefRow | undefined;+ return row ? toBelief(row) : null;+ }++ list(opts: { statuses?: BeliefStatus[]; kinds?: BeliefKind[]; dimension?: string; context?: string; limit?: number } = {}): Belief[] {+ const statuses = opts.statuses ?? ['candidate', 'held', 'confirmed', 'contested'];+ const where = ['owner_key = ?', `status IN (${statuses.map(() => '?').join(',')})`];+ const params: unknown[] = [this.ownerKey, ...statuses];+ if (opts.kinds?.length) { where.push(`kind IN (${opts.kinds.map(() => '?').join(',')})`); params.push(...opts.kinds); }+ if (opts.dimension) { where.push('dimension = ?'); params.push(opts.dimension); }+ if (opts.context) { where.push('context = ?'); params.push(opts.context); }+ const rows = this.db.prepare(`SELECT * FROM beliefs WHERE ${where.join(' AND ')} ORDER BY updated_at DESC LIMIT ?`).all(...params, opts.limit ?? 500) as BeliefRow[];+ return rows.map(toBelief);+ }++ /** Beliefs usable in a prompt: held or confirmed, confidence above the floor, strongest first. */+ usable(now = Date.now()): Array<Belief & { confidence: number }> {+ return this.list({ statuses: ['held', 'confirmed'] })+ .map((b) => ({ ...b, confidence: this.confidence(b, now) }))+ .filter((b) => b.confidence >= RETIRE_BELOW)+ .sort((a, b) => b.confidence - a.confidence);+ }++ /**+ * Beliefs relevant to a message (FTS over statement + detail). Held and+ * confirmed ones come first; when there is room, candidates follow (the+ * caller labels them unverified). Without the candidates, everything+ * learned today — and every migrated memory — would be invisible to chat+ * until a later session supported it.+ */+ searchRelevant(query: string, limit = 5, now = Date.now()): Array<Belief & { confidence: number }> {+ const match = ftsMatch(query);+ if (!match) return [];+ const run = (statuses: BeliefStatus[], n: number): Array<Belief & { confidence: number }> => {+ try {+ const rows = this.db.prepare(`+ SELECT b.* FROM beliefs_fts f JOIN beliefs b ON b.rowid = f.rowid+ WHERE beliefs_fts MATCH ? AND b.owner_key = ? AND b.status IN (${statuses.map(() => '?').join(',')})+ ORDER BY bm25(beliefs_fts) LIMIT ?`).all(match, this.ownerKey, ...statuses, n) as BeliefRow[];+ return rows.map(toBelief)+ .map((b) => ({ ...b, confidence: this.confidence(b, now) }))+ .filter((b) => b.confidence >= RETIRE_BELOW);+ } catch (err) {+ logger.debug({ err: (err as Error).message }, 'belief FTS query failed');+ return [];+ }+ };+ const sure = run(['held', 'confirmed'], limit * 3).slice(0, limit);+ if (sure.length >= limit) return sure;+ const seen = new Set(sure.map((b) => b.id));+ const maybe = run(['candidate'], limit * 3).filter((b) => !seen.has(b.id)).slice(0, limit - sure.length);+ return [...sure, ...maybe];+ }++ /** The strongest beliefs overall, for "what do you know about me?": sure ones first, then candidates. */+ strongest(limit = 12, now = Date.now()): Array<Belief & { confidence: number }> {+ const sure = this.usable(now).slice(0, limit);+ if (sure.length >= limit) return sure;+ const maybe = this.list({ statuses: ['candidate'], limit: 200 })+ .map((b) => ({ ...b, confidence: this.confidence(b, now) }))+ .filter((b) => b.confidence >= RETIRE_BELOW)+ .sort((a, b) => b.confidence - a.confidence || b.lastSupportedAt - a.lastSupportedAt)+ .slice(0, limit - sure.length);+ return [...sure, ...maybe];+ }++ /** Candidates that may be the same claim (for the merge judge). */+ findSimilar(statement: string, kind?: BeliefKind, limit = 5): Belief[] {+ const match = ftsMatch(statement, { minLength: 4, max: 16 });+ if (!match) return [];+ try {+ const rows = this.db.prepare(`+ SELECT b.* FROM beliefs_fts f JOIN beliefs b ON b.rowid = f.rowid+ WHERE beliefs_fts MATCH ? AND b.owner_key = ? AND b.status != 'forgotten' ${kind ? 'AND b.kind = ?' : ''}+ ORDER BY bm25(beliefs_fts) LIMIT ?`).all(...(kind ? [match, this.ownerKey, kind, limit] : [match, this.ownerKey, limit])) as BeliefRow[];+ return rows.map(toBelief);+ } catch {+ return [];+ }+ }++ /** Beliefs learned or changed since `since`, newest first (the "what I learned" view). */+ recentlyLearned(since: number, limit = 20): Belief[] {+ const rows = this.db.prepare(`SELECT * FROM beliefs WHERE owner_key = ? AND status != 'forgotten' AND updated_at >= ? ORDER BY updated_at DESC LIMIT ?`).all(this.ownerKey, since, limit) as BeliefRow[];+ return rows.map(toBelief);+ }++ /** Contested pairs: two live beliefs on the same dimension and context with different levels, or one marked contested. */+ contested(): Array<{ a: Belief; b: Belief | null }> {+ const out: Array<{ a: Belief; b: Belief | null }> = [];+ const seen = new Set<string>();+ for (const a of this.list({ statuses: ['contested'] })) {+ if (seen.has(a.id)) continue;+ const rival = this.rivals(a)[0] ?? null;+ if (rival) seen.add(rival.id);+ seen.add(a.id);+ out.push({ a, b: rival });+ }+ return out;+ }++ evidenceFor(beliefId: string, limit = 10): Evidence[] {+ const rows = this.db.prepare('SELECT * FROM belief_evidence WHERE belief_id = ? ORDER BY at DESC LIMIT ?').all(beliefId, limit) as Array<Record<string, unknown>>;+ return rows.map((r) => ({ id: String(r.id), beliefId: String(r.belief_id), kind: r.kind as EvidenceKind, sessionId: (r.session_id as string | null) ?? null, turnRef: (r.turn_ref as string | null) ?? null, excerpt: String(r.excerpt), weight: Number(r.weight), at: Number(r.at) }));+ }++ counts(): Record<BeliefStatus, number> {+ const rows = this.db.prepare('SELECT status, COUNT(*) as n FROM beliefs WHERE owner_key = ? GROUP BY status').all(this.ownerKey) as Array<{ status: BeliefStatus; n: number }>;+ const out: Record<BeliefStatus, number> = { candidate: 0, held: 0, confirmed: 0, contested: 0, retired: 0, forgotten: 0 };+ for (const r of rows) out[r.status] = Number(r.n);+ return out;+ }++ getMeta(key: string): string | null {+ const row = this.db.prepare('SELECT value FROM belief_meta WHERE key = ?').get(`${this.ownerKey}:${key}`) as { value: string } | undefined;+ return row?.value ?? null;+ }++ setMeta(key: string, value: string): void {+ this.db.prepare('INSERT INTO belief_meta(key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run(`${this.ownerKey}:${key}`, value);+ }++ /**+ * Two beliefs are the same claim (the reflection pass decided): the loser is+ * retired and superseded by the keeper, and its support, sessions and+ * owner-stated status move to the keeper. Returns the keeper, or null when+ * either side is missing, forgotten, or they are the same belief.+ */+ merge(keepId: string, mergeId: string, now = Date.now()): Belief | null {+ if (keepId === mergeId) return null;+ const keep = this.get(keepId);+ const lose = this.get(mergeId);+ if (!keep || !lose || keep.status === 'forgotten' || lose.status === 'forgotten') return null;+ if (lose.status === 'retired' && lose.supersededBy === keep.id) return keep;+ const ownerStated = keep.ownerStated || lose.ownerStated;+ const supportSessions = keep.supportSessions + lose.supportSessions;+ let status = keep.status;+ if (ownerStated) status = 'confirmed';+ else if (status === 'candidate' && supportSessions >= HELD_AT_SESSIONS) status = 'held';+ else if (status === 'retired') status = supportSessions >= HELD_AT_SESSIONS ? 'held' : 'candidate';+ this.db.transaction(() => {+ this.update(keep.id, {+ support: keep.support + lose.support, against: keep.against + lose.against, supportSessions, ownerStated, status,+ lastSupportedAt: Math.max(keep.lastSupportedAt, lose.lastSupportedAt), validTo: null,+ }, now);+ this.addEvidence(keep.id, 'reflection', { excerpt: `merged with "${lose.statement}"` }, lose.support, now);+ this.update(lose.id, { status: 'retired', validTo: now, supersededBy: keep.id }, now);+ });+ return this.get(keep.id);+ }++ // ── facade support (UserMemoryStore delegates here; ADR-026 single source) ──++ /** Live beliefs (any status except retired/forgotten), newest first. */+ recent(limit = 10): Belief[] {+ return this.list({ limit });+ }++ /** FTS over statements and details for the given statuses (candidates included by default). */+ search(query: string, limit = 10, statuses: BeliefStatus[] = ['candidate', 'held', 'confirmed', 'contested']): Belief[] {+ const match = ftsMatch(query);+ if (!match) return [];+ try {+ const rows = this.db.prepare(`+ SELECT b.* FROM beliefs_fts f JOIN beliefs b ON b.rowid = f.rowid+ WHERE beliefs_fts MATCH ? AND b.owner_key = ? AND b.status IN (${statuses.map(() => '?').join(',')})+ ORDER BY bm25(beliefs_fts) LIMIT ?`).all(match, this.ownerKey, ...statuses, limit) as BeliefRow[];+ return rows.map(toBelief);+ } catch {+ return [];+ }+ }++ setShareable(id: string, shareable: boolean, now = Date.now()): boolean {+ if (!this.get(id)) return false;+ this.update(id, { shareable }, now);+ return true;+ }++ shareable(limit = 50): Belief[] {+ const rows = this.db.prepare(`SELECT * FROM beliefs WHERE owner_key = ? AND shareable = 1 AND status NOT IN ('forgotten','retired') ORDER BY updated_at DESC LIMIT ?`).all(this.ownerKey, limit) as BeliefRow[];+ return rows.map(toBelief);+ }++ shareableSince(since: number, limit = 500): Belief[] {+ const rows = this.db.prepare(`SELECT * FROM beliefs WHERE owner_key = ? AND shareable = 1 AND status NOT IN ('forgotten','retired') AND updated_at > ? ORDER BY updated_at ASC LIMIT ?`).all(this.ownerKey, since, limit) as BeliefRow[];+ return rows.map(toBelief);+ }++ countShareable(): number {+ const row = this.db.prepare(`SELECT COUNT(*) as n FROM beliefs WHERE owner_key = ? AND shareable = 1 AND status NOT IN ('forgotten','retired')`).get(this.ownerKey) as { n: number } | undefined;+ return Number(row?.n ?? 0);+ }++ countsByKind(): Record<string, number> {+ const rows = this.db.prepare(`SELECT kind, COUNT(*) as n FROM beliefs WHERE owner_key = ? AND status IN ('candidate','held','confirmed','contested') GROUP BY kind`).all(this.ownerKey) as Array<{ kind: string; n: number }>;+ return Object.fromEntries(rows.map((r) => [r.kind, Number(r.n)]));+ }++ /** The owner edited a belief by hand (dashboard): the text changes, the owner's word is final. */+ edit(id: string, patch: { statement?: string; detail?: string | null; kind?: BeliefKind; context?: string }, now = Date.now()): Belief | null {+ const b = this.get(id);+ if (!b || b.status === 'forgotten') return null;+ const changes: Parameters<BeliefStore['update']>[1] = {};+ if (patch.statement && patch.statement.trim().length >= 4) changes.statement = patch.statement.trim();+ if (patch.detail !== undefined) changes.detail = patch.detail;+ if (patch.kind) changes.kind = patch.kind;+ if (patch.context) changes.context = patch.context;+ if (Object.keys(changes).length === 0) return b;+ this.update(id, { ...changes, ownerStated: true, status: b.status === 'retired' ? 'confirmed' : (b.status === 'candidate' || b.status === 'held' || b.status === 'contested' ? 'confirmed' : b.status), lastSupportedAt: now }, now);+ this.addEvidence(id, 'owner', { excerpt: 'edited by the owner' }, 1, now);+ return this.get(id);+ }++ /** Everything about this owner, gone. Used by "Clear All Memories". */+ clearAll(): number {+ const n = this.db.prepare('SELECT COUNT(*) as n FROM beliefs WHERE owner_key = ?').get(this.ownerKey) as { n: number } | undefined;+ this.db.prepare('DELETE FROM belief_evidence WHERE belief_id IN (SELECT id FROM beliefs WHERE owner_key = ?)').run(this.ownerKey);+ this.db.prepare('DELETE FROM beliefs WHERE owner_key = ?').run(this.ownerKey);+ this.db.prepare('DELETE FROM belief_meta WHERE key LIKE ?').run(`${this.ownerKey}:brief%`);+ return Number(n?.n ?? 0);+ }++ // ── internals ──────────────────────────────────────────────────────────++ private findSameClaim(statement: string, kind: BeliefKind, context: string, dimension?: string): Belief | null {+ const norm = normalizeClaim(statement);+ for (const b of this.findSimilar(statement, kind, 8)) {+ if (b.status === 'forgotten' || b.status === 'retired') continue;+ if (b.context !== context) continue;+ if (dimension && b.dimension !== dimension) continue;+ if (normalizeClaim(b.statement) === norm || tokenOverlap(norm, normalizeClaim(b.statement)) >= 0.8) return b;+ }+ return null;+ }++ /** Live beliefs on the same dimension and context with a different level. */+ private rivals(b: Belief): Belief[] {+ if (!b.dimension) return [];+ return this.list({ dimension: b.dimension, context: b.context })+ .filter((o) => o.id !== b.id && o.level !== b.level);+ }++ /** A new trait level that disagrees with a held one on the same dimension/context makes both contested. */+ private settleContest(b: Belief, now: number): void {+ if (!b.dimension || !b.level) return;+ for (const rival of this.rivals(b)) {+ if (b.ownerStated) {+ this.update(rival.id, { status: 'retired', validTo: now, supersededBy: b.id }, now);+ } else if (rival.ownerStated || rival.status === 'confirmed') {+ this.update(b.id, { status: 'retired', validTo: now, supersededBy: rival.id }, now);+ } else if (rival.status === 'held' || rival.status === 'contested') {+ this.update(rival.id, { status: 'contested' }, now);+ this.update(b.id, { status: 'contested' }, now);+ }+ }+ }++ private sessionSupported(beliefId: string, sessionId: string): boolean {+ const row = this.db.prepare('SELECT 1 FROM belief_evidence WHERE belief_id = ? AND session_id = ? AND weight > 0 LIMIT 1').get(beliefId, sessionId);+ return !!row;+ }++ private insert(b: Omit<Belief, 'id' | 'ownerKey' | 'validFrom' | 'validTo' | 'supersededBy' | 'lastSupportedAt' | 'lastUsedAt' | 'createdAt' | 'updatedAt' | 'shareable'> & { shareable?: boolean }, now: number): Belief {+ const id = randomUUID();+ this.db.prepare(`INSERT INTO beliefs (id, owner_key, kind, dimension, level, statement, detail, context, support, against, support_sessions, timescale, status, owner_stated, valid_from, valid_to, superseded_by, last_supported_at, last_used_at, created_at, updated_at, shareable)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, NULL, ?, ?, ?)`)+ .run(id, this.ownerKey, b.kind, b.dimension, b.level, b.statement, b.detail, b.context, b.support, b.against, b.supportSessions, b.timescale, b.status, b.ownerStated ? 1 : 0, now, now, now, now, b.shareable ? 1 : 0);+ return this.get(id)!;+ }++ private update(id: string, patch: Partial<Pick<Belief, 'support' | 'against' | 'supportSessions' | 'status' | 'ownerStated' | 'validTo' | 'supersededBy' | 'lastSupportedAt' | 'level' | 'statement' | 'detail' | 'kind' | 'context' | 'shareable'>>, now = Date.now()): void {+ const cols: Record<string, string> = { support: 'support', against: 'against', supportSessions: 'support_sessions', status: 'status', ownerStated: 'owner_stated', validTo: 'valid_to', supersededBy: 'superseded_by', lastSupportedAt: 'last_supported_at', level: 'level', statement: 'statement', detail: 'detail', kind: 'kind', context: 'context', shareable: 'shareable' };+ const sets: string[] = ['updated_at = ?'];+ const params: unknown[] = [now];+ for (const [k, v] of Object.entries(patch)) {+ if (v === undefined) continue;+ sets.push(`${cols[k]} = ?`);+ params.push(typeof v === 'boolean' ? (v ? 1 : 0) : v);+ }+ params.push(id);+ this.db.prepare(`UPDATE beliefs SET ${sets.join(', ')} WHERE id = ?`).run(...params);+ }++ private addEvidence(beliefId: string, kind: EvidenceKind, op: { excerpt: string; sessionId?: string; turnRef?: string }, weight: number, now: number): void {+ this.db.prepare('INSERT INTO belief_evidence (id, belief_id, kind, session_id, turn_ref, excerpt, weight, at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')+ .run(randomUUID(), beliefId, kind, op.sessionId ?? null, op.turnRef ?? null, op.excerpt.slice(0, 200), weight, now);+ }++ // ── migration from the legacy memories table ───────────────────────────++ /**+ * One-time: quarantine junk rows (Mercury's own replies and prose fragments+ * saved as the owner's preferences) and turn the rest into beliefs. Junk is+ * hidden (scope 'quarantine'), never deleted: the dashboard lists it.+ */+ migrationDone(): boolean {+ return this.getMeta('legacy_migrated') === '1';+ }++ /** Rows the pre-Owner-Model Second Brain holds for this owner (0 on a fresh install). */+ legacyRowCount(): number {+ try {+ const row = this.db.prepare('SELECT COUNT(*) as n FROM memories WHERE user_key = ?').get(this.ownerKey) as { n: number } | undefined;+ return Number(row?.n ?? 0);+ } catch {+ return 0;+ }+ }++ migrateLegacyMemories(now = Date.now()): { quarantined: number; converted: number; skipped: boolean } {+ if (this.getMeta('legacy_migrated') === '1') return { quarantined: 0, converted: 0, skipped: true };+ const rows = this.db.prepare(`SELECT * FROM memories WHERE user_key = ? AND dismissed = 0 AND scope IN ('active','durable','subconscious')`).all(this.ownerKey) as Array<Record<string, unknown>>;+ let quarantined = 0; let converted = 0;+ const quarantine = this.db.prepare(`UPDATE memories SET scope = 'quarantine', provenance = ?, updated_at = ? WHERE id = ?`);+ this.db.transaction(() => {+ for (const r of rows) {+ const summary = String(r.summary ?? '');+ if (isLegacyJunk(r)) {+ quarantine.run('migration:junk', now, String(r.id));+ quarantined++;+ continue;+ }+ // Who the owner is lives in the config (identity.owner), one source;+ // legacy rows that only restate a name, email or handle wait in+ // quarantine rather than becoming a second, often contradictory, copy.+ if (looksLikeIdentity(String(r.summary ?? ''))) {+ quarantine.run('migration:identity', now, String(r.id));+ quarantined++;+ continue;+ }+ const kind = legacyKind(String(r.type));+ if (!kind) continue;+ if (this.findSameClaim(summary, kind, '*')) continue;+ const direct = r.evidence_kind === 'direct' || r.evidence_kind === 'manual' || r.evidence_kind === 'stated';+ const support = Math.max(1, Number(r.evidence_count ?? 1));+ const belief = this.insert({+ kind, dimension: null, level: null, statement: summary, detail: (r.detail as string | null) ?? null, context: '*',+ timescale: kind === 'episode' ? 'episodic' : 'stable', support: direct ? support + 1 : support, against: 0,+ supportSessions: Math.min(support, HELD_AT_SESSIONS), ownerStated: false,+ // The old extractor's "direct" was unreliable: only what it saw+ // twice starts as held; the first reflection pass promotes the rest.+ status: support >= HELD_AT_SESSIONS ? 'held' : 'candidate',+ }, Number(r.created_at ?? now));+ this.db.prepare('UPDATE beliefs SET last_supported_at = ?, created_at = ?, updated_at = ? WHERE id = ?').run(Number(r.last_seen_at ?? r.updated_at ?? now), Number(r.created_at ?? now), Number(r.updated_at ?? now), belief.id);+ this.addEvidence(belief.id, 'migration', { excerpt: `from memory ${String(r.id)}` }, support, Number(r.created_at ?? now));+ converted++;+ }+ this.setMeta('legacy_migrated', '1');+ // Ask for a reflection pass soon after boot so the migrated candidates get judged against real sessions.+ if (converted > 0) this.setMeta('reflection_force', '1');+ });+ logger.info({ quarantined, converted }, 'Legacy memories migrated to beliefs');+ return { quarantined, converted, skipped: false };+ }+}++/** True for beliefs that should change Mercury's behaviour (what the announcer reports). */+export function isBehavioral(b: Belief): boolean {+ return b.kind === 'trait' || b.kind === 'preference';+}++function toBelief(r: BeliefRow): Belief {+ return {+ id: r.id, ownerKey: r.owner_key, kind: r.kind, dimension: r.dimension, level: r.level, statement: r.statement, detail: r.detail,+ context: r.context, support: Number(r.support), against: Number(r.against), supportSessions: Number(r.support_sessions), timescale: r.timescale,+ status: r.status, ownerStated: !!r.owner_stated, validFrom: Number(r.valid_from), validTo: r.valid_to == null ? null : Number(r.valid_to),+ supersededBy: r.superseded_by, lastSupportedAt: Number(r.last_supported_at), lastUsedAt: r.last_used_at == null ? null : Number(r.last_used_at),+ createdAt: Number(r.created_at), updatedAt: Number(r.updated_at), shareable: Number(r.shareable ?? 0) === 1,+ };+}++export function normalizeClaim(s: string): string {+ return s.toLowerCase().replace(/^(the )?(user|owner|salman)\s+/i, '').replace(/[^\p{L}\p{N}\s]/gu, ' ').replace(/\s+/g, ' ').trim();+}++function tokenOverlap(a: string, b: string): number {+ const A = new Set(a.split(' ').filter((t) => t.length > 2)); const B = new Set(b.split(' ').filter((t) => t.length > 2));+ if (A.size === 0 || B.size === 0) return 0;+ let shared = 0; for (const t of A) if (B.has(t)) shared++;+ return shared / Math.max(A.size, B.size);+}++const ASSISTANT_VOICE = /^(i |i'|i’|to |for the chat|\*\*memory|if you'd like|if you’d like|open |send |see |access |find |here's how|here’s how|\(no durable|you can |let me |sure[,!]|as an ai|i cannot|i can't)/i;++/** Rows the old extractor saved by mistake: Mercury's own replies and the non-JSON fallback's prose lines. */+export function isLegacyJunk(r: Record<string, unknown>): boolean {+ const summary = String(r.summary ?? '').trim();+ if (summary.length < 12) return true;+ if (ASSISTANT_VOICE.test(summary)) return true;+ if (/^[-*#`>|{}[\]"]/.test(summary)) return true;+ if (/\bI'm (Claude|an AI)|as an AI\b|my actual capabilities|my limitations/i.test(summary)) return true;+ // Claims whose subject is the assistant, not the owner, and throwaway test identities.+ if (/^(the )?(assistant|agent|mercury)\b.*\b(identifies|serving|serves|is an ai|can(not)?|will)\b/i.test(summary)) return true;+ if (/\b(assistant|mercury)'s creators?\b|owner and creator of the assistant/i.test(summary)) return true;+ if (/\bmercury-[a-z0-9]{6,}@/i.test(summary)) return true;+ if (/\b(password|passwd|api[ _-]?key|secret|token|otp)\b\s*[:=]?\s*\S/i.test(summary)) return true;+ const fallbackShape = r.type === 'preference' && r.evidence_kind === 'inferred' && Number(r.confidence) === 0.75 && String(r.categories ?? '') === '["general"]';+ if (fallbackShape && !/^(user|owner|the user|prefers|likes|dislikes|wants|salman)/i.test(summary)) return true;+ return false;+}++function legacyKind(type: string): BeliefKind | null {+ switch (type) {+ case 'identity': case 'decision': case 'habit': case 'fact': return 'fact';+ case 'preference': case 'constraint': return 'preference';+ case 'goal': return 'goal';+ case 'project': return 'project';+ case 'relationship': return 'relationship';+ case 'episode': return 'episode';+ default: return null;+ }+}diff --git a/src/memory/second-brain-db.ts b/src/memory/second-brain-db.tsindex 2752b368..f51a194a 100644--- a/src/memory/second-brain-db.ts+++ b/src/memory/second-brain-db.ts@@ -109,6 +109,11 @@ export class SecondBrainDB {return this.db.backend;}+ /** The shared connection, for stores that keep their own tables in this file (beliefs). */+ get connection(): SqliteDatabase {+ return this.db;+ }+init(): void {this.db.exec(`CREATE TABLE IF NOT EXISTS memories (@@ -822,6 +827,19 @@ export class SecondBrainDB {return row.count;}+ /** Rows the belief migration set aside as junk (ADR-026 §3.7): hidden from prompts, shown on the dashboard for review. */+ listQuarantined(userKey: string, limit: number = 500): MemoryRow[] {+ const stmt = this.db.prepare(`SELECT * FROM memories WHERE user_key = ? AND scope = 'quarantine' AND dismissed = 0 ORDER BY updated_at DESC LIMIT ?`);+ return stmt.all(userKey, limit) as MemoryRow[];+ }++ /** The owner reviewed a quarantined row and wants it back: it becomes a durable memory again. */+ restoreQuarantined(id: string): boolean {+ const now = Date.now();+ const stmt = this.db.prepare(`UPDATE memories SET scope = 'durable', dismissed = 0, provenance = 'restored', updated_at = ?, last_seen_at = ? WHERE id = ? AND scope = 'quarantine'`);+ return stmt.run(now, now, id).changes > 0;+ }+close(): void {this.db.close();}diff --git a/src/memory/user-memory.test.ts b/src/memory/user-memory.test.tsindex 633d3fcb..1ac92b5c 100644--- a/src/memory/user-memory.test.ts+++ b/src/memory/user-memory.test.ts@@ -1,23 +1,36 @@-import { mkdtempSync, rmSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { afterEach, describe, expect, it } from 'vitest';import { getDefaultConfig } from '../utils/config.js';import { UserMemoryStore } from './user-memory.js';-import { isBetterSqlite3Available } from './second-brain-db.js';+import { SecondBrainDB, isBetterSqlite3Available, type MemoryRow } from './second-brain-db.js';const tempDirs: string[] = [];const sqliteAvailable = isBetterSqlite3Available();-function createStore(): UserMemoryStore {+function tempDbPath(): string {const dir = mkdtempSync(join(tmpdir(), 'mercury-sb-'));tempDirs.push(dir);+ return join(dir, 'second-brain', 'second-brain.db');+}++function createStore(dbPath = tempDbPath()): UserMemoryStore {const config = getDefaultConfig();config.memory.secondBrain = { enabled: true };- const dbPath = join(dir, 'second-brain', 'second-brain.db');- const store = new UserMemoryStore(config, 'user:owner', dbPath);- return store;+ return new UserMemoryStore(config, 'user:owner', dbPath);+}++/** A row the pre-Owner-Model Second Brain would have written. */+function legacyRow(partial: Partial<MemoryRow> & { id: string; summary: string }): Omit<MemoryRow, 'rowid'> {+ const now = Date.now() - 3 * 24 * 60 * 60 * 1000;+ return {+ user_key: 'user:owner', type: 'preference', categories: '[]', shareable: 0, detail: null, scope: 'durable',+ evidence_kind: 'direct', source: 'conversation', confidence: 0.9, importance: 0.8, durability: 0.9, evidence_count: 2,+ provenance: null, dismissed: 0, superseded_by: null, created_at: now, updated_at: now, last_seen_at: now,+ last_used_at: null, last_used_query: null, ...partial,+ };}afterEach(() => {@@ -29,339 +42,169 @@ afterEach(() => {}});-describe('UserMemoryStore', () => {- it.skipIf(!sqliteAvailable)('merges similar durable memories instead of duplicating them', () => {+describe('UserMemoryStore (facade over the belief store)', () => {+ it.skipIf(!sqliteAvailable)('an explicit save is a confirmed belief the owner stated, and reads back through the old API', () => {const store = createStore();-- store.remember([- { type: 'preference', summary: 'User prefers concise answers.', confidence: 0.9, importance: 0.8, durability: 0.9 },+ const [saved] = store.remember([+ { type: 'preference', summary: 'Prefers concise technical answers.', confidence: 0.9, importance: 0.8, durability: 0.9, evidenceKind: 'direct' },]);+ expect(saved.type).toBe('preference');+ expect(saved.evidenceKind).toBe('direct');+ expect(saved.scope).toBe('durable');- store.remember([- { type: 'preference', summary: 'User prefers concise technical answers.', confidence: 0.94, importance: 0.81, durability: 0.92 },- ]);+ const belief = store.beliefs.get(saved.id);+ expect(belief?.status).toBe('confirmed');+ expect(belief?.ownerStated).toBe(true);- const recent = store.getRecent();- expect(recent).toHaveLength(1);- expect(recent[0].evidenceCount).toBe(2);- expect(recent[0].summary).toContain('concise');+ expect(store.getRecent().map((r) => r.id)).toContain(saved.id);+ expect(store.getById(saved.id)?.summary).toBe('Prefers concise technical answers.');+ expect(store.getSummary().total).toBe(1);+ expect(store.getSummary().byType.preference).toBe(1);});- it.skipIf(!sqliteAvailable)('retrieves only relevant records for a query', () => {+ it.skipIf(!sqliteAvailable)('saving the same thing twice supports the one belief instead of duplicating it', () => {const store = createStore();-- store.remember([- { type: 'project', summary: 'User is building Mercury as a personal AI agent.', confidence: 0.95, importance: 0.9, durability: 0.95 },- ]);- store.remember([- { type: 'preference', summary: 'User prefers concise answers.', confidence: 0.95, importance: 0.75, durability: 0.9 },- ]);- store.consolidate();-- const result = store.retrieveRelevant('Plan Mercury architecture', { maxRecords: 3, maxChars: 500 });-- expect(result.records).toHaveLength(1);- expect(result.records[0].type).toBe('project');- expect(result.context).toContain('Mercury');+ store.remember([{ type: 'goal', summary: 'Wants Mercury to become a second brain.', confidence: 0.9, importance: 0.9, durability: 0.9 }]);+ store.remember([{ type: 'goal', summary: 'Wants Mercury to become a second brain.', confidence: 0.9, importance: 0.9, durability: 0.9 }]);+ expect(store.getSummary().total).toBe(1);+ expect(store.getRecent()[0].evidenceCount).toBeGreaterThanOrEqual(2);});- it.skipIf(!sqliteAvailable)('auto-resolves conflicts by dismissing the lower-confidence memory', () => {+ it.skipIf(!sqliteAvailable)('an inferred save is only a candidate', () => {const store = createStore();-- store.remember([- { type: 'preference', summary: 'User prefers concise answers.', confidence: 0.96, importance: 0.84, durability: 0.92 },+ const [r] = store.remember([+ { type: 'habit', summary: 'Seems to work on projects late in the evening.', confidence: 0.6, importance: 0.6, durability: 0.6, evidenceKind: 'inferred' },]);-- store.remember([- { type: 'preference', summary: 'User does not prefer concise answers.', confidence: 0.88, importance: 0.82, durability: 0.88 },- ]);-- const recent = store.getRecent();- const active = recent.filter(r => !r.dismissed);- expect(active).toHaveLength(1);- expect(active[0].summary).toContain('concise answers');- expect(active[0].summary).not.toContain('does not');+ expect(r.evidenceKind).toBe('inferred');+ expect(r.scope).toBe('active');+ expect(store.beliefs.get(r.id)?.status).toBe('candidate');+ expect(store.getByType('habit')).toHaveLength(1);});- it.skipIf(!sqliteAvailable)('auto-resolves conflicts when incoming has higher confidence', () => {+ it.skipIf(!sqliteAvailable)('never stores a secret, whatever the owner asked', () => {const store = createStore();-- store.remember([- { type: 'preference', summary: 'User prefers verbose answers.', confidence: 0.75, importance: 0.7, durability: 0.7 },- ]);-- store.remember([- { type: 'preference', summary: 'User does not prefer verbose answers.', confidence: 0.95, importance: 0.9, durability: 0.9 },+ const saved = store.remember([+ { type: 'identity', summary: 'My GitHub token is ghp_abcdefghijklmnopqrstuvwxyz0123', confidence: 0.9, importance: 0.9, durability: 0.9 },]);-- const recent = store.getRecent();- const active = recent.filter(r => !r.dismissed);- expect(active).toHaveLength(1);- expect(active[0].summary).toContain('does not prefer verbose');+ expect(saved).toHaveLength(0);+ expect(store.getSummary().total).toBe(0);});- it.skipIf(!sqliteAvailable)('synthesizes a compact profile and reflection records from repeated signal', () => {+ it.skipIf(!sqliteAvailable)('retrieval returns the relevant beliefs as a context block', () => {const store = createStore();-- store.remember([- { type: 'identity', summary: 'User is building Mercury.', confidence: 0.96, importance: 0.92, durability: 0.95 },- ]);- store.remember([- { type: 'preference', summary: 'User prefers concise technical answers.', confidence: 0.96, importance: 0.84, durability: 0.92 },- ]);- store.remember([- { type: 'preference', summary: 'User prefers practical implementation details.', confidence: 0.95, importance: 0.82, durability: 0.9 },- ]);store.remember([- { type: 'goal', summary: 'User wants Mercury to become a second brain.', confidence: 0.97, importance: 0.95, durability: 0.94 },+ { type: 'project', summary: 'Is building a personal knowledge management system.', confidence: 0.95, importance: 0.9, durability: 0.9 },+ { type: 'preference', summary: 'Prefers tea over coffee.', confidence: 0.8, importance: 0.5, durability: 0.7 },]);-- const result = store.consolidate();- const summary = store.getSummary();- const recent = store.getRecent(10);- const reflections = recent.filter(r => r.type === 'reflection');-- expect(result.reflectionCount).toBeGreaterThan(0);- expect(summary.profileSummary).toContain('Mercury');- expect(summary.profileSummary).toContain('concise');- expect(reflections.length).toBeGreaterThan(0);+ const result = store.retrieveRelevant('How should I structure the knowledge management system?', { maxRecords: 3 });+ expect(result.records.length).toBeGreaterThanOrEqual(1);+ expect(result.records[0].summary).toContain('knowledge management');+ expect(result.context).toContain('Relevant user memory:');+ expect(store.retrieveRelevant('zxqv unrelated').records).toHaveLength(0);});- it.skipIf(!sqliteAvailable)('separates active state from durable profile', () => {+ it.skipIf(!sqliteAvailable)('search, update and delete go through the belief store', () => {const store = createStore();+ const [r] = store.remember([{ type: 'preference', summary: 'Prefers dark mode everywhere.', confidence: 0.9, importance: 0.7, durability: 0.9 }]);+ expect(store.search('dark mode')[0]?.id).toBe(r.id);- store.remember([- { type: 'project', summary: 'User is building Mercury memory architecture this week.', confidence: 0.96, importance: 0.91, durability: 0.82 },- ]);- store.remember([- { type: 'goal', summary: 'User wants Mercury to become a second brain.', confidence: 0.97, importance: 0.95, durability: 0.93 },- ]);- store.remember([- { type: 'preference', summary: 'User prefers concise answers.', confidence: 0.96, importance: 0.82, durability: 0.92 },- ]);- store.remember([- { type: 'preference', summary: 'User does not prefer concise answers.', confidence: 0.95, importance: 0.82, durability: 0.9 },- ]);- store.consolidate();-- const summary = store.getSummary();- const active = store.getActiveSummary();+ const updated = store.updateMemory(r.id, { summary: 'Prefers dark mode in every editor.', shareable: true });+ expect(updated?.summary).toBe('Prefers dark mode in every editor.');+ expect(updated?.shareable).toBe(true);+ expect(store.countShareable()).toBe(1);+ expect(store.getShareableSince(0).map((m) => m.id)).toEqual([r.id]);- expect(active).toContain('Mercury memory architecture');- expect(summary.profileSummary).toContain('second brain');+ expect(store.softDeleteMemory(r.id)).toBe(true);+ expect(store.getSummary().total).toBe(0);+ expect(store.search('dark mode')).toHaveLength(0);});it.skipIf(!sqliteAvailable)('supports pause and resume of learning', () => {const store = createStore();-- expect(store.isLearningPaused()).toBe(false);-store.setLearningPaused(true);- expect(store.isLearningPaused()).toBe(true);-- store.remember([- { type: 'goal', summary: 'User wants autonomous learning.', confidence: 0.95, importance: 0.92, durability: 0.88 },- ]);+ store.remember([{ type: 'goal', summary: 'Wants autonomous learning.', confidence: 0.95, importance: 0.92, durability: 0.88 }]);expect(store.getSummary().total).toBe(0);-store.setLearningPaused(false);- store.remember([- { type: 'goal', summary: 'User wants autonomous learning.', confidence: 0.95, importance: 0.92, durability: 0.88 },- ]);+ store.remember([{ type: 'goal', summary: 'Wants autonomous learning.', confidence: 0.95, importance: 0.92, durability: 0.88 }]);expect(store.getSummary().total).toBe(1);});it.skipIf(!sqliteAvailable)('clears all memories', () => {const store = createStore();-store.remember([- { type: 'identity', summary: 'User is a software developer.', confidence: 0.95, importance: 0.9, durability: 0.9 },+ { type: 'identity', summary: 'Is a software developer.', confidence: 0.95, importance: 0.9, durability: 0.9 },+ { type: 'preference', summary: 'Prefers dark mode.', confidence: 0.9, importance: 0.75, durability: 0.85 },]);- store.remember([- { type: 'preference', summary: 'User prefers dark mode.', confidence: 0.9, importance: 0.75, durability: 0.85 },- ]);-expect(store.getSummary().total).toBe(2);-- const cleared = store.clear();- expect(cleared).toBe(2);- expect(store.getSummary().total).toBe(0);- });-- it.skipIf(!sqliteAvailable)('full-text search finds relevant memories', () => {- const store = createStore();-- store.remember([- { type: 'project', summary: 'User is building a personal knowledge management system.', confidence: 0.95, importance: 0.9, durability: 0.9 },- ]);- store.remember([- { type: 'preference', summary: 'User prefers tea over coffee.', confidence: 0.8, importance: 0.5, durability: 0.7 },- ]);-- const results = store.search('knowledge management');- expect(results.length).toBeGreaterThanOrEqual(1);- expect(results[0].summary).toContain('knowledge management');- });-- it.skipIf(!sqliteAvailable)('deprioritizes stale inferred memories in retrieval', () => {- const store = createStore();-- const [inferred] = store.remember([- { type: 'goal', summary: 'User may be exploring memory tooling.', confidence: 0.88, importance: 0.84, durability: 0.82, evidenceKind: 'inferred' },- ]);- const [direct] = store.remember([- { type: 'goal', summary: 'User explicitly wants Mercury to become a second brain.', confidence: 0.9, importance: 0.9, durability: 0.9, evidenceKind: 'direct' },- ]);-- store.consolidate();- const summary = store.getSummary();- expect(summary.profileSummary).toContain('second brain');- });-- it.skipIf(!sqliteAvailable)('stores weak memories that pass minimum threshold', () => {- const store = createStore();-- store.remember([- { type: 'habit', summary: 'User seems to work on projects late evenings.', confidence: 0.58, importance: 0.6, durability: 0.6, evidenceKind: 'inferred' },- ]);-- expect(store.getSummary().total).toBe(1);- });-- it.skipIf(!sqliteAvailable)('rejects memories below minimum confidence', () => {- const store = createStore();-- store.remember([- { type: 'habit', summary: 'Too uncertain to store.', confidence: 0.3, importance: 0.4, durability: 0.4 },- ]);-+ expect(store.clear()).toBe(2);expect(store.getSummary().total).toBe(0);+ expect(store.getRecent()).toHaveLength(0);});- it('reports better-sqlite3 availability status', () => {- expect(typeof sqliteAvailable).toBe('boolean');- });-- it.skipIf(!sqliteAvailable)('moves stale memories to subconscious instead of deleting them', () => {+ it.skipIf(!sqliteAvailable)('profile and active summary come from the reflection brief', () => {const store = createStore();-- store.remember([- { type: 'preference', summary: 'User prefers dark mode for coding.', confidence: 0.9, importance: 0.8, durability: 0.9 },- ]);-- expect(store.getSummary().total).toBe(1);- expect(store.getSummary().subconsciousTotal).toBe(0);-- const db = (store as any).db as import('./second-brain-db.js').SecondBrainDB;- const row = db.getActive('user:owner')[0];- db.update({ id: row.id, last_seen_at: Date.now() - (31 * 24 * 60 * 60 * 1000), updated_at: Date.now() - (31 * 24 * 60 * 60 * 1000) });-- const result = store.prune();- expect(result.movedToSubconscious).toBe(1);-- expect(store.getSummary().total).toBe(0);- expect(store.getSummary().subconsciousTotal).toBe(1);+ expect(store.getProfile()).toBe('');+ store.beliefs.setMeta('brief', JSON.stringify({ about: 'Builds Mercury.', howToWork: 'Keep replies short.', at: Date.now(), sig: 'x' }));+ expect(store.getProfile()).toBe('Builds Mercury.');+ expect(store.getActiveSummary()).toBe('Keep replies short.');+ expect(store.getSummary().profileSummary).toBe('Builds Mercury.');+ expect(store.consolidate()).toEqual({ profileUpdated: false, reflectionCount: 0 });});+});- it.skipIf(!sqliteAvailable)('recalls subconscious memories when conscious is weak', () => {- const store = createStore();+describe('UserMemoryStore upgrade from a pre-Owner-Model database', () => {+ function seedLegacy(dbPath: string): void {+ mkdirSync(join(dbPath, '..'), { recursive: true });+ const db = new SecondBrainDB(dbPath);+ db.init();+ db.insert(legacyRow({ id: 'm-pref', type: 'preference', summary: 'User prefers concise technical answers.' }));+ db.insert(legacyRow({ id: 'm-proj', type: 'project', summary: 'User is building Mercury, an AI agent.', evidence_count: 1 }));+ db.insert(legacyRow({ id: 'm-junk', type: 'preference', summary: 'I can help you with that! Here is the plan:' }));+ db.insert(legacyRow({ id: 'm-secret', type: 'identity', summary: 'User password is hunter2' }));+ db.setMeta('user:owner:profile_summary', 'Old templated profile');+ db.close();+ }- store.remember([- { type: 'preference', summary: 'User prefers TypeScript for backend projects.', confidence: 0.9, importance: 0.85, durability: 0.9 },- ]);+ it.skipIf(!sqliteAvailable)('boots without error, backs the database up once, and keeps every real memory readable', () => {+ const dbPath = tempDbPath();+ seedLegacy(dbPath);- const db = (store as any).db as import('./second-brain-db.js').SecondBrainDB;- const row = db.getActive('user:owner')[0];- db.update({ id: row.id, last_seen_at: Date.now() - (31 * 24 * 60 * 60 * 1000), updated_at: Date.now() - (31 * 24 * 60 * 60 * 1000) });+ const store = createStore(dbPath);+ const backup = dbPath.replace(/\.db$/, '.pre-owner-model.db');+ expect(existsSync(backup)).toBe(true);- store.prune();+ // Real memories are beliefs now; the old API still lists them.+ const summaries = store.getRecent(50).map((r) => r.summary);+ expect(summaries).toContain('User prefers concise technical answers.');+ expect(summaries).toContain('User is building Mercury, an AI agent.');+ expect(summaries.some((s) => s.includes('hunter2'))).toBe(false);+ expect(summaries.some((s) => s.startsWith('I can help'))).toBe(false);+ expect(store.getSummary().total).toBe(2);+ expect(store.search('concise')).toHaveLength(1);++ // Junk went to quarantine, not the bin; the owner can bring it back.+ const quarantined = store.listQuarantined();+ expect(quarantined.map((q) => q.id).sort()).toEqual(['m-junk', 'm-secret']);+ expect(store.restoreQuarantined('m-junk')).toBeTruthy();+ expect(store.listQuarantined().map((q) => q.id)).toEqual(['m-secret']);+ expect(store.getSummary().total).toBe(3);++ // Old summaries never leak: the profile now comes only from the reflection brief.+ expect(store.getProfile()).toBe('');+ store.close();++ // Second boot: nothing migrates twice and nothing is lost.+ const again = createStore(dbPath);+ expect(again.getSummary().total).toBe(3);+ expect(again.listQuarantined().map((q) => q.id)).toEqual(['m-secret']);+ again.close();+ });++ it.skipIf(!sqliteAvailable)('a fresh database needs no backup and starts empty', () => {+ const dbPath = tempDbPath();+ const store = createStore(dbPath);+ expect(existsSync(dbPath.replace(/\.db$/, '.pre-owner-model.db'))).toBe(false);expect(store.getSummary().total).toBe(0);- expect(store.getSummary().subconsciousTotal).toBe(1);-- const result = store.retrieveRelevant('TypeScript backend', { maxRecords: 5, maxChars: 900 });- expect(result.records.length).toBeGreaterThanOrEqual(1);-- expect(store.getSummary().total).toBeGreaterThanOrEqual(1);- expect(store.getSummary().subconsciousTotal).toBe(0);- });-- it.skipIf(!sqliteAvailable)('does not enforce a hard cap on memory count', () => {- const store = createStore();- const db = (store as any).db as import('./second-brain-db.js').SecondBrainDB;- const now = Date.now();-- for (let i = 0; i < 60; i++) {- db.insert({- id: `mem_test_${i}`,- user_key: 'user:owner',- type: i % 2 === 0 ? 'preference' : 'identity',- categories: '[]',- shareable: 0,- summary: `Distinct memory observation number ${i} with completely different content about topic ${i}.`,- detail: null,- scope: i % 3 === 0 ? 'active' : 'durable',- evidence_kind: 'direct',- source: 'conversation',- confidence: 0.85,- importance: 0.75,- durability: 0.8,- evidence_count: 1,- provenance: null,- dismissed: 0,- superseded_by: null,- created_at: now,- updated_at: now,- last_seen_at: now,- last_used_at: null,- last_used_query: null,- });- }-- expect(store.getSummary().total).toBe(60);- });-- it.skipIf(!sqliteAvailable)('preserves memories in subconscious without confidence decay', () => {- const store = createStore();-- store.remember([- { type: 'identity', summary: 'User is a software engineer specializing in distributed systems.', confidence: 0.92, importance: 0.9, durability: 0.95 },- ]);-- const db = (store as any).db as import('./second-brain-db.js').SecondBrainDB;- const row = db.getActive('user:owner')[0];- const originalConfidence = row.confidence;-- db.update({ id: row.id, last_seen_at: Date.now() - (31 * 24 * 60 * 60 * 1000), updated_at: Date.now() - (31 * 24 * 60 * 60 * 1000) });- store.prune();-- expect(store.getSummary().subconsciousTotal).toBe(1);-- const subconsciousRow = db.getSubconscious('user:owner')[0];- expect(subconsciousRow.confidence).toBeCloseTo(originalConfidence, 2);- });-- it.skipIf(!sqliteAvailable)('hard deletes only explicitly dismissed memories, not just subconscious ones', () => {- const store = createStore();-- store.remember([- { type: 'preference', summary: 'User prefers dark mode for coding.', confidence: 0.9, importance: 0.8, durability: 0.9 },- ]);-- const db = (store as any).db as import('./second-brain-db.js').SecondBrainDB;- const row = db.getActive('user:owner')[0];- db.update({ id: row.id, last_seen_at: Date.now() - (31 * 24 * 60 * 60 * 1000), updated_at: Date.now() - (31 * 24 * 60 * 60 * 1000) });-- store.prune();- expect(store.getSummary().subconsciousTotal).toBe(1);-- store.prune();- expect(store.getSummary().subconsciousTotal).toBe(1);-- store.remember([- { type: 'preference', summary: 'User does not prefer dark mode for coding.', confidence: 0.95, importance: 0.85, durability: 0.9 },- ]);-- const recentConscious = store.getSummary().total;- expect(recentConscious).toBe(1);+ expect(store.listQuarantined()).toHaveLength(0);});-});\ No newline at end of file+});diff --git a/src/memory/user-memory.ts b/src/memory/user-memory.tsindex f126887c..eb304109 100644--- a/src/memory/user-memory.ts+++ b/src/memory/user-memory.ts@@ -1,7 +1,10 @@import type { MercuryConfig } from '../utils/config.js';import { getMemoryDir } from '../utils/config.js';import { SecondBrainDB, type MemoryRow, type PersonListRow } from './second-brain-db.js';+import { BeliefStore, type Belief, type BeliefKind } from './beliefs.js';+import { looksLikeSecret } from '../learning/text.js';import { join } from 'node:path';+import { copyFileSync, existsSync } from 'node:fs';import { logger } from '../utils/logger.js';export type UserMemoryType =@@ -80,8 +83,6 @@ export interface UserPersonRecord {updatedAt: number;}-const MIN_CONFIDENCE = 0.55;-const SUBCONSCIOUS_RECALL_THRESHOLD = 0.5;const PERSON_INDEX_VERSION = '7';export class UserMemoryStore {@@ -91,6 +92,8 @@ export class UserMemoryStore {private lastConsolidateAt: number = 0;/** When true, new memories are marked shareable for cloud fetch. */private shareLearning: boolean;+ /** What Mercury believes about this owner, with evidence (ADR-026). Shares the database. */+ readonly beliefs: BeliefStore;constructor(config: MercuryConfig, userKey: string = 'user:owner', dbPath?: string) {this.userKey = userKey;@@ -99,6 +102,18 @@ export class UserMemoryStore {const resolvedDbPath = dbPath ?? join(getMemoryDir(), 'second-brain', 'second-brain.db');this.db = new SecondBrainDB(resolvedDbPath);this.db.init();+ this.beliefs = new BeliefStore(this.db.connection, userKey);+ this.beliefs.init();+ try {+ // The one-time move into beliefs (ADR-026) happens on the first boot+ // after an upgrade. A copy of the database goes next to it first, so an+ // existing owner can always get their old Second Brain back.+ if (!this.beliefs.migrationDone() && this.beliefs.legacyRowCount() > 0) backupDatabase(resolvedDbPath);+ const migrated = this.beliefs.migrateLegacyMemories();+ if (!migrated.skipped) logger.info(migrated, 'Second Brain: legacy memories migrated into beliefs');+ } catch (err) {+ logger.warn({ err }, 'Second Brain: belief migration failed (continuing with legacy memories)');+ }}/** Toggle whether new memories are marked shareable. */@@ -110,71 +125,78 @@ export class UserMemoryStore {return this.shareLearning;}+ /**+ * Everything below reads and writes the belief store (ADR-026): one source+ * of truth. The legacy `memories` table is a read-only archive kept for the+ * quarantine queue and the persons graph; nothing new is written to it.+ */getSummary(): UserMemorySummary {- const byType = this.db.countByType(this.userKey) as Partial<Record<UserMemoryType, number>>;+ const byKind = this.beliefs.countsByKind();+ const byType: Partial<Record<UserMemoryType, number>> = {};+ for (const [kind, n] of Object.entries(byKind)) {+ const t = kindToType(kind as BeliefKind);+ byType[t] = (byType[t] ?? 0) + n;+ }+ const counts = this.beliefs.counts();+ const brief = this.readBrief();return {- total: this.db.totalActive(this.userKey),- subconsciousTotal: this.db.countSubconscious(this.userKey),+ total: counts.candidate + counts.held + counts.confirmed + counts.contested,+ subconsciousTotal: counts.retired,byType,learningPaused: this.isLearningPaused(),- profileSummary: this.db.getMeta(`${this.userKey}:profile_summary`) ?? undefined,- activeSummary: this.db.getMeta(`${this.userKey}:active_summary`) ?? undefined,+ profileSummary: brief.about || undefined,+ activeSummary: brief.howToWork || undefined,};}+ /** The "About the owner" brief the reflection pass wrote (ADR-026). */getProfile(): string {- return this.db.getMeta(`${this.userKey}:profile_summary`) || '';+ return this.readBrief().about;}+ /** The "How to work with the owner" brief the reflection pass wrote. */getActiveSummary(): string {- return this.db.getMeta(`${this.userKey}:active_summary`) || '';+ return this.readBrief().howToWork;}getRecent(limit: number = 10): UserMemoryRecord[] {- const stmt = this.db.getActive(this.userKey).slice(0, limit);- return stmt.map(row => this.toRecord(row));+ return this.beliefs.recent(limit).map((b) => this.toBeliefRecord(b));}/**- * Incremental fetch — shareable memories newer than the cursor. Used by the- * cloud `memory.fetch` WS handler. Returns records ordered by `updatedAt`- * ascending so the caller can advance its watermark to the max.+ * Incremental fetch — shareable beliefs newer than the cursor. Used by the+ * cloud `memory.fetch` WS handler. Ordered by `updatedAt` ascending so the+ * caller can advance its watermark to the max.*/getShareableSince(since: number, limit: number = 500): UserMemoryRecord[] {- const rows = this.db.getShareableSince(this.userKey, since, limit);- return rows.map(row => this.toRecord(row));+ return this.beliefs.shareableSince(since, limit).map((b) => this.toBeliefRecord(b));}- /** Count of shareable memories (for display). */countShareable(): number {- return this.db.countShareable(this.userKey);+ return this.beliefs.countShareable();}- /** List shareable memories (for the /memory menu "Shared Memories" view). */getShareable(limit: number = 50): UserMemoryRecord[] {- const rows = this.db.getShareable(this.userKey, limit);- return rows.map(row => this.toRecord(row));+ return this.beliefs.shareable(limit).map((b) => this.toBeliefRecord(b));}- /** Toggle a memory's shareable flag. */setShareable(id: string, shareable: boolean): void {- this.db.setShareable(id, shareable);+ if (!this.beliefs.setShareable(id, shareable)) this.db.setShareable(id, shareable);}search(query: string, limit: number = 10): UserMemoryRecord[] {- const rows = this.db.searchFTS(this.userKey, query, limit);- return rows.map(row => this.toRecord(row));+ return this.beliefs.search(query, limit).map((b) => this.toBeliefRecord(b));}+ /** Search including retired beliefs (the dashboard's "all" scope). */searchAll(query: string, limit: number = 20): UserMemoryRecord[] {- const conscious = this.db.searchFTS(this.userKey, query, limit);- const subconscious = this.db.searchSubconscious(this.userKey, query, limit);- return [...conscious, ...subconscious].map(row => this.toRecord(row));+ return this.beliefs.search(query, limit, ['candidate', 'held', 'confirmed', 'contested', 'retired']).map((b) => this.toBeliefRecord(b));}getByType(type: UserMemoryType): UserMemoryRecord[] {- const rows = this.db.getByType(this.userKey, type);- return rows.map(row => this.toRecord(row));+ const kinds = typeToKinds(type);+ if (kinds.length === 0) return [];+ return this.beliefs.list({ kinds, limit: 500 }).map((b) => this.toBeliefRecord(b));}listPersons(query?: string, limit: number = 100): UserPersonRecord[] {@@ -208,117 +230,63 @@ export class UserMemoryStore {return rows.length;}+ /**+ * Beliefs relevant to a message, as a context block. The agent's own turn+ * path injects beliefs itself; this serves the bots and anything else that+ * wants one call.+ */retrieveRelevant(query: string,options?: { maxRecords?: number; maxChars?: number },): RetrievedUserMemory {const maxRecords = options?.maxRecords ?? 5;const maxChars = options?.maxChars ?? 900;+ const relevant = this.beliefs.searchRelevant(query, maxRecords);+ if (relevant.length === 0) return { records: [], context: '' };- const ftsResults = this.db.searchRelevant(this.userKey, query, Math.max(maxRecords * 2, 10));- const ranked = this.scoreAndRank(ftsResults, query);-- const selected: MemoryRow[] = [];+ const selected: typeof relevant = [];let currentLength = 0;- const consciousScores: number[] = [];- for (const row of ranked) {- const line = `- [${row.type}] ${row.summary}`;- if (selected.length >= maxRecords) break;+ for (const b of relevant) {+ const line = `- [${kindToType(b.kind)}] ${b.statement}`;if (selected.length > 0 && currentLength + line.length > maxChars) break;- selected.push(row);- consciousScores.push(memoryHealthScore(row));+ selected.push(b);currentLength += line.length + 1;}-- const strongConsciousMatches = consciousScores.filter(s => s > SUBCONSCIOUS_RECALL_THRESHOLD).length;-- if (strongConsciousMatches < 3) {- const subconsciousResults = this.db.searchSubconscious(this.userKey, query, Math.max(maxRecords * 2, 10));- if (subconsciousResults.length > 0) {- const subconsciousRanked = this.scoreAndRankSubconscious(subconsciousResults, query);-- for (const row of subconsciousRanked) {- const line = `- [${row.type}] ${row.summary}`;- if (selected.length >= maxRecords) break;- if (currentLength + line.length > maxChars) break;- selected.push(row);- currentLength += line.length + 1;-- const newScope = inferScope({ type: row.type as UserMemoryType, summary: row.summary, confidence: row.confidence, importance: row.importance, durability: row.durability });- this.db.promoteToConscious(row.id, newScope);- logger.debug({ id: row.id, type: row.type, summary: row.summary.slice(0, 50), newScope }, 'Recalled memory from subconscious');- }- }- }-- // No relevant memory → no context. The profile summary is already part- // of the stable system prompt (Agent.buildSystemPrompt → getSummary), so- // re-injecting it here on every zero-hit turn only added tokens and- // primed stale facts.- if (selected.length === 0) {- return { records: [], context: '' };- }-- const contextLines: string[] = [];- const activeSummary = this.getActiveSummary();- if (activeSummary) {- contextLines.push('User active state:');- contextLines.push(`- ${activeSummary}`);- contextLines.push('');- }- contextLines.push('Relevant user memory:');- contextLines.push(...selected.map(row => `- [${row.type}] ${row.summary}`));-- this.markUsed(selected.map(r => r.id), query);- return { records: selected.map(r => this.toRecord(r)), context: contextLines.join('\n') };+ this.beliefs.markUsed(selected.map((b) => b.id));+ const context = ['Relevant user memory:', ...selected.map((b) => `- [${kindToType(b.kind)}] ${b.statement}`)].join('\n');+ return { records: selected.map((b) => this.toBeliefRecord(b)), context };}+ /**+ * Explicit saves (the save_memory tool, the dashboard's "add" form) become+ * beliefs: a direct save is the owner's word (confirmed, never decays); an+ * inferred one is a candidate that has to earn its place like any other.+ */remember(candidates: UserMemoryCandidate[],source: UserMemoryRecord['source'] = 'conversation',): UserMemoryRecord[] {if (this.isLearningPaused()) return [];-- // Stamp shareable based on the shareLearning toggle. Candidates that- // explicitly set shareable (e.g. manual saves via the web API) keep their- // value; others inherit the global toggle. Forward-only — existing- // memories are never retroactively flipped.- for (const candidate of candidates) {- if (candidate.shareable === undefined) {- candidate.shareable = this.shareLearning;- }- }-const remembered: UserMemoryRecord[] = [];-for (const candidate of candidates) {- if (!shouldStoreCandidate(candidate)) continue;-- const terms = normalize(candidate.summary).split(/\s+/).filter(t => t.length > 2);-- const mergeTarget = this.db.findMergeCandidate(this.userKey, candidate.type, terms);- if (mergeTarget && overlapScore(normalize(mergeTarget.summary), normalize(candidate.summary)) >= 0.74) {- const merged = this.mergeRecord(mergeTarget, candidate);- if (merged) {- remembered.push(merged);- this.indexPersonsForMemory(merged);- }- continue;- }-- const conflictTarget = this.db.findConflictCandidate(this.userKey, candidate.type, terms);- if (conflictTarget) {- const conflictWinner = this.resolveConflict(conflictTarget, candidate);- if (conflictWinner === 'existing') continue;- }-- const record = this.insertRecord(candidate, source);- if (record) {- remembered.push(record);- this.indexPersonsForMemory(record);- }+ const summary = candidate.summary?.trim() ?? '';+ if (summary.length < 4 || looksLikeSecret(summary) || (candidate.detail && looksLikeSecret(candidate.detail))) continue;+ const kind = typeToKinds(candidate.type)[0] ?? 'fact';+ const direct = candidate.evidenceKind !== 'inferred';+ const result = this.beliefs.apply({+ op: direct ? 'owner_stated' : 'create',+ kind,+ statement: summary,+ detail: candidate.detail?.trim() || undefined,+ context: '*',+ timescale: kind === 'situation' ? 'situational' : kind === 'episode' ? 'episodic' : 'stable',+ excerpt: source === 'system' ? 'saved by Mercury' : 'asked Mercury to remember this',+ evidenceKind: direct ? 'owner' : 'observed',+ weight: direct ? 2 : 1,+ shareable: candidate.shareable ?? this.shareLearning,+ });+ if (result) remembered.push(this.toBeliefRecord(result.belief));}-return remembered;}@@ -330,268 +298,130 @@ export class UserMemoryStore {return this.db.getMeta(`${this.userKey}:learning_paused`) === '1';}+ /** "Clear all memories": beliefs, their evidence and the legacy archive, gone. */clear(): number {- return this.db.clearByType(this.userKey);+ const beliefs = this.beliefs.clearAll();+ const legacy = this.db.clearByType(this.userKey);+ return beliefs + legacy;}+ /**+ * @deprecated The reflection pass (Agent.reflectNow) maintains the owner+ * brief now. Kept so older callers keep working; does nothing.+ */consolidate(): { profileUpdated: boolean; reflectionCount: number } {- const now = Date.now();- if (now - this.lastConsolidateAt < this.consolidateThrottleMs && this.lastConsolidateAt > 0) {- return { profileUpdated: false, reflectionCount: 0 };- }- this.lastConsolidateAt = now;-- const allActive = this.db.getActive(this.userKey);- const nonReflection = allActive.filter(r => r.type !== 'reflection');-- const profileSummary = buildProfileSummary(nonReflection);- const activeSummary = buildActiveSummary(nonReflection);-- const oldProfile = this.db.getMeta(`${this.userKey}:profile_summary`) || '';- const oldActive = this.db.getMeta(`${this.userKey}:active_summary`) || '';- const profileUpdated = profileSummary !== oldProfile || activeSummary !== oldActive;-- this.db.setMeta(`${this.userKey}:profile_summary`, profileSummary);- this.db.setMeta(`${this.userKey}:active_summary`, activeSummary);-- const reflections = buildReflectionCandidates(nonReflection);- let reflectionCount = 0;- for (const reflection of reflections) {- const sameType = this.db.getByType(this.userKey, 'reflection');- const existing = findMergeTarget(sameType, reflection);- if (existing) {- this.db.update({- id: existing.id,- summary: reflection.summary,- detail: reflection.detail || existing.detail,- confidence: clamp(Math.max(existing.confidence, reflection.confidence), 0, 1),- importance: clamp(Math.max(existing.importance, reflection.importance), 0, 1),- durability: clamp(Math.max(existing.durability, reflection.durability), 0, 1),- updated_at: Date.now(),- last_seen_at: Date.now(),- });- } else {- this.insertReflection(reflection);- }- reflectionCount += 1;- }-- return { profileUpdated, reflectionCount };+ return { profileUpdated: false, reflectionCount: 0 };}softDeleteMemory(id: string): boolean {- return this.db.softDelete(id);+ if (this.beliefs.forget(id)) return true;+ const row = this.db.getById(id);+ return row && row.user_key === this.userKey ? this.db.softDelete(id) : false;}updateMemory(id: string, updates: Record<string, any>): UserMemoryRecord | null {+ const belief = this.beliefs.get(id);+ if (belief) {+ const kind = typeof updates.type === 'string' ? typeToKinds(updates.type as UserMemoryType)[0] : undefined;+ const edited = this.beliefs.edit(id, {+ statement: typeof updates.summary === 'string' ? updates.summary : undefined,+ detail: updates.detail === undefined ? undefined : (updates.detail ?? null),+ kind,+ });+ if (edited && typeof updates.shareable === 'boolean') this.beliefs.setShareable(id, updates.shareable);+ const fresh = this.beliefs.get(id);+ return fresh ? this.toBeliefRecord(fresh) : null;+ }const existing = this.db.getById(id);- if (!existing) return null;+ if (!existing || existing.user_key !== this.userKey) return null;this.db.update({ id, ...updates, updated_at: Date.now() });const updated = this.db.getById(id);- if (!updated) return null;- const record = this.toRecord(updated);- this.indexPersonsForMemory(record);- return record;+ return updated ? this.toRecord(updated) : null;}+ /** The decay pass: beliefs that faded and were contradicted or went stale retire. */prune(): { movedToSubconscious: number; promoted: number; hardDeleted: number } {- const promoted = this.db.promoteToDurable(this.userKey);- const movedToSubconscious = this.db.moveToSubconscious(this.userKey);- const hardDeleted = this.db.hardDeleteDismissed(this.userKey);- if (hardDeleted > 0) {- logger.debug({ hardDeleted, userKey: this.userKey }, 'Hard deleted dismissed memories');- }- if (movedToSubconscious > 0) {- logger.debug({ movedToSubconscious, userKey: this.userKey }, 'Moved stale memories to subconscious');- }- return { movedToSubconscious, promoted, hardDeleted };+ const retired = this.beliefs.decayPass();+ if (retired > 0) logger.debug({ retired, userKey: this.userKey }, 'Retired faded beliefs');+ return { movedToSubconscious: retired, promoted: 0, hardDeleted: 0 };}+ /** Retired beliefs: still searchable, no longer shown to the model. */getSubconscious(limit: number = 10): UserMemoryRecord[] {- return this.db.getSubconscious(this.userKey).slice(0, limit).map(row => this.toRecord(row));+ return this.beliefs.list({ statuses: ['retired'], limit }).map((b) => this.toBeliefRecord(b));}- close(): void {- this.db.close();- }-- private insertRecord(candidate: UserMemoryCandidate, source: UserMemoryRecord['source']): UserMemoryRecord | null {- const now = Date.now();- const id = generateId('mem');- const scope = inferScope(candidate);-- this.db.insert({- id,- user_key: this.userKey,- type: candidate.type,- categories: JSON.stringify(candidate.categories ?? []),- shareable: candidate.shareable ? 1 : 0,- summary: candidate.summary.trim(),- detail: candidate.detail?.trim() ?? null,- scope,- evidence_kind: candidate.evidenceKind || (source === 'conversation' ? 'inferred' : 'system'),- source,- confidence: clamp(candidate.confidence, 0, 1),- importance: clamp(candidate.importance, 0, 1),- durability: clamp(candidate.durability, 0, 1),- evidence_count: 1,- provenance: candidate.detail?.trim() ?? null,- dismissed: 0,- superseded_by: null,- created_at: now,- updated_at: now,- last_seen_at: now,- last_used_at: null,- last_used_query: null,- });-+ getById(id: string): UserMemoryRecord | null {+ const belief = this.beliefs.get(id);+ if (belief) return this.toBeliefRecord(belief);const row = this.db.getById(id);- return row ? this.toRecord(row) : null;+ return row && row.user_key === this.userKey ? this.toRecord(row) : null;}- private insertReflection(candidate: UserMemoryCandidate): void {- const now = Date.now();- const id = generateId('mem');-- this.db.insert({- id,- user_key: this.userKey,- type: 'reflection',- categories: '[]',- shareable: 0,- summary: candidate.summary,- detail: candidate.detail ?? null,- scope: 'durable',- evidence_kind: 'system',- source: 'system',- confidence: candidate.confidence,- importance: candidate.importance,- durability: candidate.durability,- evidence_count: 1,- provenance: null,- dismissed: 0,- superseded_by: null,- created_at: now,- updated_at: now,- last_seen_at: now,- last_used_at: null,- last_used_query: null,- });+ /** Legacy rows the belief migration quarantined (ADR-026 §3.7), for the dashboard's review queue. */+ listQuarantined(limit: number = 500): UserMemoryRecord[] {+ return this.db.listQuarantined(this.userKey, limit).map(row => this.toRecord(row));}- private mergeRecord(existing: MemoryRow, candidate: UserMemoryCandidate): UserMemoryRecord | null {- const updatedAt = Date.now();- // If the incoming candidate is shareable, promote the existing record to- // shareable too (forward-only — never demotes a shareable memory).- const promoteShareable = candidate.shareable && existing.shareable !== 1;- this.db.update({- id: existing.id,- summary: pickBetterSummary(existing.summary, candidate.summary),- detail: candidate.detail || existing.detail,- provenance: candidate.detail || existing.provenance,- evidence_kind: candidate.evidenceKind || existing.evidence_kind,- confidence: clamp(Math.max(existing.confidence, candidate.confidence), 0, 1),- importance: clamp(Math.max(existing.importance, candidate.importance), 0, 1),- durability: clamp(Math.max(existing.durability, candidate.durability), 0, 1),- evidence_count: existing.evidence_count + 1,- shareable: promoteShareable ? 1 : existing.shareable,- updated_at: updatedAt,- last_seen_at: updatedAt,+ /** Restoring a quarantined row turns it into a belief the owner vouched for; returns the belief's id. */+ restoreQuarantined(id: string): string | null {+ const row = this.db.getById(id);+ if (!row || row.user_key !== this.userKey || row.scope !== 'quarantine' || row.dismissed) return null;+ const kind = typeToKinds(row.type as UserMemoryType)[0] ?? 'fact';+ const result = this.beliefs.apply({+ op: 'owner_stated', kind, statement: row.summary, detail: row.detail ?? undefined, context: '*',+ timescale: kind === 'situation' ? 'situational' : kind === 'episode' ? 'episodic' : 'stable',+ excerpt: 'restored from quarantine by the owner', evidenceKind: 'owner', weight: 2,});-- const row = this.db.getById(existing.id);- return row ? this.toRecord(row) : null;+ if (!result) return null;+ this.db.update({ id, dismissed: 1, provenance: `migration:restored→${result.belief.id}`, updated_at: Date.now() });+ return result.belief.id;}- private resolveConflict(existing: MemoryRow, candidate: UserMemoryCandidate): 'incoming' | 'existing' {- const incomingConfidence = candidate.confidence;- const existingConfidence = existing.confidence;-- if (incomingConfidence > existingConfidence) {- this.db.update({- id: existing.id,- dismissed: 1,- superseded_by: 'auto_resolved',- updated_at: Date.now(),- });- logger.debug(- { existingId: existing.id, existingConfidence, incomingConfidence, type: candidate.type },- 'Auto-resolved conflict: incoming wins',- );- return 'incoming';- }-- if (incomingConfidence < existingConfidence) {- logger.debug(- { existingId: existing.id, existingConfidence, incomingConfidence, type: candidate.type },- 'Auto-resolved conflict: existing wins',- );- return 'existing';- }-- this.db.update({- id: existing.id,- dismissed: 1,- superseded_by: 'auto_resolved',- updated_at: Date.now(),- });- logger.debug(- { existingId: existing.id, type: candidate.type },- 'Auto-resolved conflict: equal confidence, newer wins',- );- return 'incoming';+ close(): void {+ this.db.close();}- private markUsed(ids: string[], query?: string): void {- const now = Date.now();- for (const id of ids) {- this.db.update({- id,- last_used_at: now,- last_used_query: query || undefined,- updated_at: now,- });+ private readBrief(): { about: string; howToWork: string } {+ try {+ const raw = this.beliefs.getMeta('brief');+ if (!raw) return { about: '', howToWork: '' };+ const parsed = JSON.parse(raw) as { about?: string; howToWork?: string };+ return { about: (parsed.about ?? '').trim(), howToWork: (parsed.howToWork ?? '').trim() };+ } catch {+ return { about: '', howToWork: '' };}}- private scoreAndRank(rows: MemoryRow[], query: string): MemoryRow[] {- const now = Date.now();- const tokens = query.toLowerCase().split(/\s+/).filter(t => t.length > 0);- return rows- .map(row => {- let score = 0;- score += row.confidence * 0.3;- score += row.importance * 0.25;- score += row.durability * 0.15;- const ageDays = (now - row.updated_at) / (1000 * 60 * 60 * 24);- score += Math.max(0, 0.2 - ageDays * 0.005);- const lower = (row.summary + ' ' + (row.detail ?? '')).toLowerCase();- const matchCount = tokens.filter(t => lower.includes(t)).length;- score += (matchCount / Math.max(tokens.length, 1)) * 0.1;- return { row, score };- })- .sort((a, b) => b.score - a.score)- .map(r => r.row);- }-- private scoreAndRankSubconscious(rows: MemoryRow[], query: string): MemoryRow[] {- const now = Date.now();- const tokens = query.toLowerCase().split(/\s+/).filter(t => t.length > 0);- return rows- .map(row => {- let score = 0;- score += row.confidence * 0.20;- score += row.importance * 0.20;- score += row.durability * 0.10;- const recallAgeDays = Math.min((now - row.last_seen_at) / (1000 * 60 * 60 * 24), 365);- score += Math.max(0, 0.20 - recallAgeDays * 0.001);- const lower = (row.summary + ' ' + (row.detail ?? '')).toLowerCase();- const matchCount = tokens.filter(t => lower.includes(t)).length;- score += (matchCount / Math.max(tokens.length, 1)) * 0.30;- return { row, score };- })- .sort((a, b) => b.score - a.score)- .map(r => r.row);+ /** A belief in the record shape every existing reader (menus, dashboard, cloud sync) expects. */+ private toBeliefRecord(b: Belief): UserMemoryRecord {+ const confidence = this.beliefs.confidence(b);+ const scope: UserMemoryRecord['scope'] = b.status === 'held' || b.status === 'confirmed' ? 'durable'+ : b.status === 'retired' || b.status === 'forgotten' ? 'subconscious' : 'active';+ const categories = [b.kind, ...(b.dimension ? [b.dimension] : []), ...(b.context !== '*' ? [b.context] : [])];+ return {+ id: b.id,+ type: kindToType(b.kind),+ categories,+ shareable: b.shareable,+ summary: b.statement,+ detail: b.detail,+ scope,+ evidenceKind: b.ownerStated ? 'direct' : 'inferred',+ source: 'conversation',+ confidence,+ importance: b.status === 'confirmed' ? 0.9 : b.status === 'held' ? 0.75 : b.status === 'contested' ? 0.6 : 0.5,+ durability: b.timescale === 'stable' ? 0.9 : b.timescale === 'episodic' ? 0.5 : 0.2,+ evidenceCount: Math.max(1, Math.round(b.support)),+ provenance: `belief:${b.status}${b.level ? `:${b.level}` : ''}`,+ dismissed: b.status === 'forgotten',+ supersededBy: b.supersededBy,+ createdAt: b.createdAt,+ updatedAt: b.updatedAt,+ lastSeenAt: b.lastSupportedAt,+ lastUsedAt: b.lastUsedAt,+ lastUsedQuery: null,+ };}private ensurePersonsBackfilled(): void {@@ -878,230 +708,37 @@ function mentionsPerson(text: string, name: string): boolean {return regex.test(text);}-function shouldStoreCandidate(candidate: UserMemoryCandidate): boolean {- const summary = candidate.summary.trim();- if (summary.length < 12 || summary.length > 220) return false;- if (candidate.confidence < MIN_CONFIDENCE) return false;- if (candidate.durability < 0.4 && candidate.importance < 0.7) return false;- return true;-}--function findMergeTarget(rows: MemoryRow[], candidate: UserMemoryCandidate): MemoryRow | undefined {- const normalizedCandidate = normalize(candidate.summary);- return rows.find(row => {- if (row.type !== candidate.type) return false;- if (hasConflict(row.summary, candidate.summary)) return false;- const normalizedRow = normalize(row.summary);- if (normalizedRow === normalizedCandidate) return true;- return overlapScore(normalizedRow, normalizedCandidate) >= 0.74;- });-}--function memoryHealthScore(row: MemoryRow): number {- return (row.importance * 0.35)- + (row.durability * 0.25)- + (effectiveConfidence(row) * 0.25)- + (Math.min(row.evidence_count, 5) / 5 * 0.15)- + (row.scope === 'active' ? 0.08 : 0)- - (row.scope === 'subconscious' ? 0.2 : 0)- - (row.superseded_by ? 0.3 : 0)- - (isRowStale(row) ? 0.12 : 0);-}--function effectiveConfidence(row: MemoryRow): number {- if (row.scope === 'subconscious') return row.confidence;-- const ageDays = (Date.now() - row.updated_at) / (1000 * 60 * 60 * 24);- let confidence = row.confidence;-- if (row.evidence_kind === 'inferred') {- confidence -= Math.min(0.2, ageDays / 365);- } else if (row.evidence_kind === 'manual') {- confidence += 0.06;- } else if (row.evidence_kind === 'direct') {- confidence += 0.03;- }-- if (row.scope === 'active') {- confidence -= Math.min(0.18, ageDays / 120);- }-- return clamp(confidence, 0, 1);-}--function isRowStale(row: MemoryRow): boolean {- if (row.scope === 'subconscious') return true;- const ageDays = (Date.now() - row.updated_at) / (1000 * 60 * 60 * 24);- return ageDays > 30;-}--function tokenize(input: string): string[] {- return input- .toLowerCase()- .split(/[^a-z0-9]+/i)- .map(term => term.trim())- .filter(term => term.length >= 3);-}--function normalize(input: string): string {- return tokenize(input).join(' ');-}--function overlapScore(a: string, b: string): number {- const aTerms = new Set(a.split(' ').filter(Boolean));- const bTerms = new Set(b.split(' ').filter(Boolean));- if (aTerms.size === 0 || bTerms.size === 0) return 0;-- let overlap = 0;- for (const term of aTerms) {- if (bTerms.has(term)) overlap += 1;- }- return overlap / Math.max(aTerms.size, bTerms.size);-}--function pickBetterSummary(existing: string, incoming: string): string {- return incoming.length > existing.length && incoming.length <= 220 ? incoming.trim() : existing.trim();-}--function clamp(value: number, min: number, max: number): number {- return Math.max(min, Math.min(max, value));-}--function generateId(prefix: string): string {- return `${prefix}_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;-}--function inferScope(candidate: UserMemoryCandidate): 'durable' | 'active' {- if (['goal', 'project', 'decision', 'episode'].includes(candidate.type)) {- return 'active';- }- return 'durable';-}--function hasConflict(a: string, b: string): boolean {- const left = a.toLowerCase();- const right = b.toLowerCase();- if (left === right) return false;-- const polarityPairs = [- ['prefers', 'does not prefer'],- ['likes', 'does not like'],- ['wants', 'does not want'],- ['is building', 'is not building'],- ['uses', 'does not use'],- ['enabled', 'disabled'],- ];-- for (const [positive, negative] of polarityPairs) {- const leftPosRightNeg = left.includes(positive) && right.includes(negative);- const leftNegRightPos = left.includes(negative) && right.includes(positive);- if (leftPosRightNeg || leftNegRightPos) {- return overlapScore(- normalize(left.replace(positive, '').replace(negative, '')),- normalize(right.replace(positive, '').replace(negative, '')),- ) >= 0.5;- }- }-- const leftHasNegation = /\b(not|never|no longer|avoid|against|disabled)\b/.test(left);- const rightHasNegation = /\b(not|never|no longer|avoid|against|disabled)\b/.test(right);- if (leftHasNegation !== rightHasNegation) {- return overlapScore(normalize(left), normalize(right)) >= 0.7;+/** `second-brain.db` → `second-brain.pre-owner-model.db`, once; never overwrites an earlier copy. */+function backupDatabase(dbPath: string): void {+ if (dbPath === ':memory:' || !existsSync(dbPath)) return;+ const backup = dbPath.replace(/\.db$/, '') + '.pre-owner-model.db';+ if (existsSync(backup)) return;+ try {+ copyFileSync(dbPath, backup);+ logger.info({ backup }, 'Second Brain: backed up before the Owner Model migration');+ } catch (err) {+ logger.warn({ err, backup }, 'Second Brain: backup before migration failed (continuing)');}-- return false;}-function buildProfileSummary(records: MemoryRow[]): string {- const selected: string[] = [];- const preferredTypes: string[] = ['identity', 'preference', 'goal', 'project', 'constraint', 'habit'];-- for (const type of preferredTypes) {- const match = records- .filter(r => r.type === type)- .sort((a, b) => memoryHealthScore(b) - memoryHealthScore(a))[0];- if (match && !selected.includes(match.summary)) {- selected.push(match.summary);- }- if (selected.length >= 4) break;+/** Memory types the old API speaks ↔ belief kinds (ADR-026). */+function kindToType(kind: BeliefKind): UserMemoryType {+ switch (kind) {+ case 'fact': return 'identity';+ case 'trait': return 'habit';+ case 'situation': return 'episode';+ default: return kind;}-- return selected.join(' ').slice(0, 420).trim();}-function buildActiveSummary(records: MemoryRow[]): string {- const cutoff = Date.now() - (14 * 24 * 60 * 60 * 1000);- const activeCandidates = records- .filter(r => !r.superseded_by)- .filter(r => !isRowStale(r))- .filter(r => r.updated_at >= cutoff)- .filter(r => ['goal', 'project', 'decision', 'episode'].includes(r.type))- .sort((a, b) => {- const byUpdated = b.updated_at - a.updated_at;- if (byUpdated !== 0) return byUpdated;- return memoryHealthScore(b) - memoryHealthScore(a);- })- .slice(0, 3)- .map(r => r.summary);- return activeCandidates.join(' ').slice(0, 360).trim();-}--function buildReflectionCandidates(records: MemoryRow[]): UserMemoryCandidate[] {- const candidates: UserMemoryCandidate[] = [];- const groups = new Map<string, MemoryRow[]>();-- for (const record of records) {- if (!groups.has(record.type)) groups.set(record.type, []);- groups.get(record.type)!.push(record);+function typeToKinds(type: UserMemoryType): BeliefKind[] {+ switch (type) {+ case 'identity': return ['fact'];+ case 'habit': return ['trait'];+ case 'constraint': return ['preference'];+ case 'decision': return ['episode'];+ case 'episode': return ['episode', 'situation'];+ case 'reflection': return [];+ default: return [type];}-- const prefGroup = groups.get('preference') || [];- if (prefGroup.length >= 2) {- const top = prefGroup- .sort((a, b) => memoryHealthScore(b) - memoryHealthScore(a))- .slice(0, 2)- .map(r => r.summary);- candidates.push({- type: 'reflection',- summary: `User consistently shows these preferences: ${top.join(' ')}`.slice(0, 220),- detail: top.join('\n'),- confidence: 0.86,- importance: 0.86,- durability: 0.9,- });- }-- const goalProjectGroup = [...(groups.get('goal') || []), ...(groups.get('project') || [])];- if (goalProjectGroup.length >= 2) {- const top = goalProjectGroup- .sort((a, b) => memoryHealthScore(b) - memoryHealthScore(a))- .slice(0, 2)- .map(r => r.summary);- candidates.push({- type: 'reflection',- summary: `Current long-term direction: ${top.join(' ')}`.slice(0, 220),- detail: top.join('\n'),- confidence: 0.84,- importance: 0.9,- durability: 0.86,- });- }-- const habitConstraintGroup = [...(groups.get('habit') || []), ...(groups.get('constraint') || [])];- if (habitConstraintGroup.length >= 2) {- const top = habitConstraintGroup- .sort((a, b) => memoryHealthScore(b) - memoryHealthScore(a))- .slice(0, 2)- .map(r => r.summary);- candidates.push({- type: 'reflection',- summary: `Working style pattern: ${top.join(' ')}`.slice(0, 220),- detail: top.join('\n'),- confidence: 0.82,- importance: 0.8,- durability: 0.82,- });- }-- return candidates;}diff --git a/src/plugins/channel-bridge.ts b/src/plugins/channel-bridge.tsindex 435132a3..b1613f65 100644--- a/src/plugins/channel-bridge.ts+++ b/src/plugins/channel-bridge.ts@@ -72,6 +72,8 @@ export function describeTool(name: string): string {if (name === 'fetch_url') return 'reading a web page';if (name === 'search_memory') return 'checking what I know about you';if (name === 'save_memory') return 'making a note';+ if (name === 'forget_memory') return 'forgetting that';+ if (name === 'confirm_belief' || name === 'correct_belief') return 'updating what I know about you';if (name === 'use_skill' || name === 'list_skills') return 'using a skill';if (name === 'read_file' || name === 'list_dir') return 'looking through your files';if (name === 'run_command') return 'running a command';diff --git a/src/skills/loader.ts b/src/skills/loader.tsindex f5916a7a..aa4aa190 100644--- a/src/skills/loader.ts+++ b/src/skills/loader.ts@@ -3,6 +3,7 @@ import { join, resolve, sep } from 'node:path';import { parse as parseYaml } from 'yaml';import { getMercuryHome } from '../utils/config.js';import type { SkillDiscovery, Skill, SkillMeta } from './types.js';+import { recordSelfMadeUse, SELF_MADE_CATEGORY, SELF_MADE_LABEL } from './self-made.js';import { IntentRouter } from './intent-router.js';import { logger } from '../utils/logger.js';import { DEFAULT_SKILL_SEEDS, PREVIOUS_SEED_CONTENTS, getCategoryLabel } from './default-skills.js';@@ -62,6 +63,7 @@ export class SkillLoader {// - nested: <skillsDir>/<category>/<slug>/SKILL.md (mercury skills install)// User-installed nested skills win over flat ones on name collision (registered last).const skillPaths = this.findSkillFiles();+ const parsedSkills: Array<{ meta: SkillMeta; path: string }> = [];for (const skillPath of skillPaths) {try {const raw = readFileSync(skillPath, 'utf-8');@@ -71,15 +73,20 @@ export class SkillLoader {name: parsed.meta.name,description: parsed.meta.description,});- this.intentRouter.registerSkillMeta(parsed.meta);- logger.info({ skill: parsed.meta.name, path: skillPath, category: parsed.meta.category, intents: parsed.meta.intents?.length }, 'Skill discovered');+ parsedSkills.push({ meta: parsed.meta, path: skillPath });} catch (err) {logger.warn({ path: skillPath, err }, 'Failed to load skill');}}- // Build intent index from discovered skills (basic info for fallback matching)+ // Reset the router with the bare list first, then enrich every entry with+ // its category, intents and tags. (The other order wiped the metadata on+ // every scan, so grouping and intent routing silently fell back to names.)this.intentRouter.buildIndex([...this.discovered.values()]);+ for (const { meta, path } of parsedSkills) {+ this.intentRouter.registerSkillMeta(meta);+ logger.info({ skill: meta.name, path, category: meta.category, intents: meta.intents?.length }, 'Skill discovered');+ }return [...this.discovered.values()];}@@ -164,6 +171,16 @@ export class SkillLoader {return [...this.discovered.values()];}+ /** The primary skills root (where self-made skills are written). */+ rootDir(): string {+ return this.skillsDir;+ }++ /** Count a use of a self-made skill (no-op for curated ones). */+ noteUse(name: string): void {+ recordSelfMadeUse(this.skillsDir, name);+ }+getSkillSummariesText(): string {const skills = this.getDiscovered();if (skills.length === 0) return '';@@ -177,7 +194,7 @@ export class SkillLoader {if (hasCategories && categories.size > 1) {// Group by categoryfor (const [category, catSkills] of categories) {- const label = getCategoryLabel(category);+ const label = category === SELF_MADE_CATEGORY ? SELF_MADE_LABEL : getCategoryLabel(category);text += `\n**${label}:**\n`;for (const s of catSkills) {const discovered = this.discovered.get(s.name);diff --git a/src/skills/self-made.test.ts b/src/skills/self-made.test.tsnew file mode 100644index 00000000..18cfd9ae--- /dev/null+++ b/src/skills/self-made.test.ts@@ -0,0 +1,126 @@+import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, describe, expect, it } from 'vitest';+import { SkillLoader } from './loader.js';+import {+ DORMANT_AFTER_DAYS, MAX_SELF_MADE, SELF_MADE_CATEGORY, SELF_MADE_LABEL, composeSkillMd, listSelfMade, readSelfMadeMeta, recordSelfMadeUse,+ rejectDraft, selfMadeHygiene, slugify, type SelfMadeDraft,+} from './self-made.js';+import { createCreateSkillTool } from '../capabilities/skills/create-skill.js';++const dirs: string[] = [];+const DAY = 86_400_000;++function root(): string {+ const dir = mkdtempSync(join(tmpdir(), 'mercury-selfmade-'));+ dirs.push(dir);+ return dir;+}++afterEach(() => {+ while (dirs.length > 0) rmSync(dirs.pop()!, { recursive: true, force: true });+});++const good: SelfMadeDraft = {+ name: 'Weekly Sales Report',+ description: 'Build the weekly sales PDF from the Google Sheet export and mail it to the founders.',+ intents: ['make the weekly sales report', 'send the sales PDF'],+ instructions: [+ '## Steps',+ '1. Ask for the week if not given; default to the last full week (Mon–Sun).',+ '2. Export the sheet: run_command `gsheet export --id $SHEET --range Sales!A:F --csv /tmp/sales.csv`.',+ '3. Build the PDF: `python3 scripts/report.py /tmp/sales.csv --week <week> --out /tmp/sales-<week>.pdf`.',+ '4. Check: the PDF opens, has one page per region, and the total matches the sheet footer.',+ '5. Mail it with send_message to the founders group; subject "Sales <week>".',+ '## Pitfalls',+ '- The sheet export fails silently when the range name has a space; quote it.',+ ].join('\n'),+ verified: 'PDF had 4 region pages, total 12,340 matched the sheet footer, mail delivered',+};++describe('self-made skills (ADR-027)', () => {+ it('slugs names and rejects drafts that are not real procedures', () => {+ expect(slugify('Weekly Sales Report')).toBe('weekly-sales-report');+ expect(slugify('!!')).toBeNull();+ expect(rejectDraft(good)).toBeNull();+ expect(rejectDraft({ ...good, instructions: 'run the thing' })).toMatch(/too short/);+ expect(rejectDraft({ ...good, intents: [] })).toMatch(/intent/);+ expect(rejectDraft({ ...good, verified: '' })).toMatch(/verified/);+ expect(rejectDraft({ ...good, instructions: good.instructions + '\nuse api key sk-abcdefghijklmnopqrstuvwxyz0123' })).toMatch(/credential/);+ expect(rejectDraft({ ...good, instructions: 'x'.repeat(300) })).toMatch(/procedure/);+ });++ it('composes a SKILL.md in the self-made category and keeps made/uses across an update', () => {+ const md = composeSkillMd(good, undefined, Date.parse('2026-10-24T10:00:00Z'));+ const meta = readSelfMadeMeta(md)!;+ expect(meta.name).toBe('weekly-sales-report');+ expect(meta.category).toBe(SELF_MADE_CATEGORY);+ expect(meta.origin).toBe('mercury');+ expect(meta.made).toBe('2026-10-24T10:00:00.000Z');+ expect(meta.uses).toBe(0);+ expect(md).toContain('## Steps');+ const updated = readSelfMadeMeta(composeSkillMd({ ...good, verified: 'again' }, { made: meta.made, uses: 3, last_used: '2026-11-01T00:00:00.000Z' }))!;+ expect(updated.made).toBe(meta.made);+ expect(updated.uses).toBe(3);+ expect(updated.last_used).toBe('2026-11-01T00:00:00.000Z');+ });++ it('create_skill saves under self-made, refuses to shadow a curated skill, and the loader lists it under its own label', async () => {+ const dir = root();+ const loader = new SkillLoader(dir, { seedDefaults: false });+ loader.saveSkill('curated', '---\nname: curated\ndescription: A curated skill the owner installed\ncategory: development\nintents:\n - run curated\n---\n# Curated\n1. do it\n');+ const tool = createCreateSkillTool(loader);+ const exec = (tool as unknown as { execute: (args: unknown) => Promise<string> }).execute;++ expect(await exec({ ...good, name: 'curated' })).toMatch(/already an installed skill/);+ expect(await exec({ ...good, instructions: 'too short' })).toMatch(/Not saved/);+ const saved = await exec(good);+ expect(saved).toMatch(/saved/);+ expect(existsSync(join(dir, SELF_MADE_CATEGORY, 'weekly-sales-report', 'SKILL.md'))).toBe(true);++ const loaded = loader.load('weekly-sales-report')!;+ expect(loaded.category).toBe(SELF_MADE_CATEGORY);+ expect(loaded.instructions).toContain('## Steps');+ expect(loader.getSkillSummariesText()).toContain(SELF_MADE_LABEL);++ expect(await exec({ ...good, verified: 'ran twice more, totals matched' })).toMatch(/updated/);+ expect(listSelfMade(dir)).toHaveLength(1);+ });++ it('counts uses and lets unused skills go dormant without deleting them', () => {+ const dir = root();+ const loader = new SkillLoader(dir, { seedDefaults: false });+ const t0 = Date.parse('2026-10-24T10:00:00Z');+ loader.saveSkill(`${SELF_MADE_CATEGORY}/weekly-sales-report`, composeSkillMd(good, undefined, t0));+ loader.saveSkill(`${SELF_MADE_CATEGORY}/old-unused`, composeSkillMd({ ...good, name: 'old-unused' }, undefined, t0 - 40 * DAY));++ expect(recordSelfMadeUse(dir, 'weekly-sales-report', t0 + DAY)).toBe(true);+ expect(recordSelfMadeUse(dir, 'nope')).toBe(false);+ const used = listSelfMade(dir).find((s) => s.name === 'weekly-sales-report')!;+ expect(used.uses).toBe(1);+ expect(used.lastUsed).toBe(t0 + DAY);+ // The file is still a valid skill after the frontmatter rewrite.+ expect(loader.load('weekly-sales-report')?.instructions).toContain('## Steps');++ const { dormant } = selfMadeHygiene(dir, t0 + 2 * DAY);+ expect(dormant).toEqual(['old-unused']);+ expect(existsSync(join(dir, SELF_MADE_CATEGORY, 'old-unused', 'SKILL.md'))).toBe(true);+ expect(listSelfMade(dir).find((s) => s.name === 'old-unused')?.active).toBe(false);+ // A used one survives well past the dormancy window.+ expect(selfMadeHygiene(dir, t0 + (DORMANT_AFTER_DAYS + 5) * DAY).dormant).toEqual([]);+ expect(readFileSync(join(dir, SELF_MADE_CATEGORY, 'old-unused', '.disabled'), 'utf-8')).toMatch(/never used/);+ });++ it('caps the shelf: beyond the limit, the least recently used go dormant', () => {+ const dir = root();+ const loader = new SkillLoader(dir, { seedDefaults: false });+ const t0 = Date.parse('2026-10-24T10:00:00Z');+ for (let i = 0; i < MAX_SELF_MADE + 2; i++) {+ loader.saveSkill(`${SELF_MADE_CATEGORY}/skill-${i}`, composeSkillMd({ ...good, name: `skill-${i}` }, undefined, t0 - i * 60_000));+ recordSelfMadeUse(dir, `skill-${i}`, t0 + (MAX_SELF_MADE + 2 - i) * 60_000);+ }+ const { dormant } = selfMadeHygiene(dir, t0 + DAY);+ expect(dormant.sort()).toEqual([`skill-${MAX_SELF_MADE}`, `skill-${MAX_SELF_MADE + 1}`].sort());+ });+});diff --git a/src/skills/self-made.ts b/src/skills/self-made.tsnew file mode 100644index 00000000..a6dbf191--- /dev/null+++ b/src/skills/self-made.ts@@ -0,0 +1,201 @@+/**+ * Self-made skills (ADR-027): procedures Mercury writes for itself after a+ * long task worked, so the next time is one `use_skill` away. They live in+ * their own category folder (`<skills>/self-made/<slug>/SKILL.md`) and are+ * labelled as unverified until they have been used again, so an owner can+ * always tell a curated skill from one Mercury made on the go.+ *+ * Hygiene keeps the shelf honest: a self-made skill that is never used again+ * goes dormant (disabled, not deleted) after DORMANT_AFTER_DAYS, and the shelf+ * is capped at MAX_SELF_MADE live skills (least recently used go dormant).+ */+import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';+import { logger } from '../utils/logger.js';+import { looksLikeSecret } from '../learning/text.js';++export const SELF_MADE_CATEGORY = 'self-made';+export const SELF_MADE_LABEL = 'Self-made (Mercury wrote these on the go; unverified until used again)';+export const DORMANT_AFTER_DAYS = 30;+export const MAX_SELF_MADE = 40;+export const MIN_INSTRUCTION_CHARS = 200;+export const MAX_INSTRUCTION_CHARS = 12_000;+const SKILL_FILE = 'SKILL.md';+const DISABLED_FILE = '.disabled';+const DAY = 86_400_000;+const SLUG_RE = /^[a-z0-9][a-z0-9-]{2,48}$/;++export interface SelfMadeDraft {+ name: string;+ description: string;+ intents: string[];+ instructions: string;+ /** What was checked when it worked (one line); the proof the skill is not a guess. */+ verified: string;+ allowedTools?: string[];+}++export interface SelfMadeMeta {+ name: string;+ description: string;+ category: typeof SELF_MADE_CATEGORY;+ origin: 'mercury';+ intents: string[];+ made: string;+ verified: string;+ uses: number;+ last_used?: string;+ 'allowed-tools'?: string[];+}++export interface SelfMadeEntry {+ name: string;+ description: string;+ dir: string;+ made: number;+ uses: number;+ lastUsed: number | null;+ active: boolean;+}++/** Normalise a free-text name to a slug; returns null when nothing usable remains. */+export function slugify(name: string): string | null {+ const slug = name.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 49);+ return SLUG_RE.test(slug) ? slug : null;+}++/**+ * Validate a draft the model produced. Returns the reason it is not worth+ * keeping, or null. Deliberately strict: a half-written skill is junk the owner+ * has to clean up later.+ */+export function rejectDraft(draft: SelfMadeDraft): string | null {+ if (!slugify(draft.name)) return 'name must be 3–49 characters of letters, digits and dashes';+ const description = draft.description?.trim() ?? '';+ if (description.length < 20 || description.length > 220) return 'description must be 20–220 characters: what it does and for which inputs';+ const intents = (draft.intents ?? []).map((i) => i.trim()).filter(Boolean);+ if (intents.length === 0) return 'at least one intent phrase (how the owner would ask for this)';+ const instructions = draft.instructions?.trim() ?? '';+ if (instructions.length < MIN_INSTRUCTION_CHARS) return `instructions are too short (${instructions.length} chars; a real procedure needs at least ${MIN_INSTRUCTION_CHARS})`;+ if (instructions.length > MAX_INSTRUCTION_CHARS) return `instructions are too long (${instructions.length} chars; keep the procedure under ${MAX_INSTRUCTION_CHARS})`;+ if (!/(^|\n)\s*(1[.)]|- |\* |## )/.test(instructions)) return 'instructions must be a procedure: numbered steps or bullet points';+ if (!draft.verified || draft.verified.trim().length < 12) return 'say what you verified when it worked (one line); unverified procedures are not saved';+ if (looksLikeSecret(instructions) || looksLikeSecret(description) || looksLikeSecret(draft.verified)) return 'instructions contain something that looks like a credential; refer to where it is configured instead';+ return null;+}++/** The SKILL.md text for a draft, keeping `made` and `uses` from an earlier version. */+export function composeSkillMd(draft: SelfMadeDraft, prior?: Pick<SelfMadeMeta, 'made' | 'uses' | 'last_used'>, now = Date.now()): string {+ const slug = slugify(draft.name)!;+ const meta: SelfMadeMeta = {+ name: slug,+ description: draft.description.trim(),+ category: SELF_MADE_CATEGORY,+ origin: 'mercury',+ intents: draft.intents.map((i) => i.trim()).filter(Boolean).slice(0, 12),+ made: prior?.made ?? new Date(now).toISOString(),+ verified: draft.verified.trim(),+ uses: prior?.uses ?? 0,+ ...(prior?.last_used ? { last_used: prior.last_used } : {}),+ ...(draft.allowedTools && draft.allowedTools.length > 0 ? { 'allowed-tools': draft.allowedTools } : {}),+ };+ const body = draft.instructions.trim();+ const title = body.startsWith('#') ? '' : `# ${slug}\n\n`;+ return `---\n${stringifyYaml(meta).trim()}\n---\n\n${title}${body}\n`;+}++export function readSelfMadeMeta(skillMd: string): SelfMadeMeta | null {+ const m = /^---\s*\n([\s\S]*?)\n---/.exec(skillMd);+ if (!m) return null;+ try {+ const meta = parseYaml(m[1]) as Partial<SelfMadeMeta>;+ if (meta?.category !== SELF_MADE_CATEGORY || !meta.name) return null;+ return { uses: 0, intents: [], made: '', verified: '', description: '', origin: 'mercury', ...meta } as SelfMadeMeta;+ } catch {+ return null;+ }+}++/** The self-made folder inside a skills root. */+export function selfMadeRoot(skillsDir: string): string {+ return join(skillsDir, SELF_MADE_CATEGORY);+}++export function listSelfMade(skillsDir: string): SelfMadeEntry[] {+ const root = selfMadeRoot(skillsDir);+ if (!existsSync(root)) return [];+ const out: SelfMadeEntry[] = [];+ for (const entry of readdirSync(root, { withFileTypes: true })) {+ if (!entry.isDirectory() || entry.name.startsWith('.') || entry.name.startsWith('_')) continue;+ const dir = join(root, entry.name);+ const file = join(dir, SKILL_FILE);+ if (!existsSync(file)) continue;+ try {+ const meta = readSelfMadeMeta(readFileSync(file, 'utf-8'));+ if (!meta) continue;+ out.push({+ name: meta.name,+ description: meta.description,+ dir,+ made: Date.parse(meta.made) || 0,+ uses: Number(meta.uses) || 0,+ lastUsed: meta.last_used ? Date.parse(meta.last_used) || null : null,+ active: !existsSync(join(dir, DISABLED_FILE)),+ });+ } catch (err) {+ logger.debug({ err, dir }, 'unreadable self-made skill');+ }+ }+ return out.sort((a, b) => (b.lastUsed ?? b.made) - (a.lastUsed ?? a.made));+}++/** Bump `uses` / `last_used` in the frontmatter when a self-made skill is invoked. */+export function recordSelfMadeUse(skillsDir: string, name: string, now = Date.now()): boolean {+ const file = join(selfMadeRoot(skillsDir), name, SKILL_FILE);+ if (!existsSync(file)) return false;+ try {+ const raw = readFileSync(file, 'utf-8');+ const m = /^---\s*\n([\s\S]*?)\n---([\s\S]*)$/.exec(raw);+ if (!m) return false;+ const meta = parseYaml(m[1]) as Record<string, unknown>;+ if (meta.category !== SELF_MADE_CATEGORY) return false;+ meta.uses = (Number(meta.uses) || 0) + 1;+ meta.last_used = new Date(now).toISOString();+ writeFileSync(file, `---\n${stringifyYaml(meta).trim()}\n---${m[2]}`, 'utf-8');+ return true;+ } catch (err) {+ logger.debug({ err, name }, 'could not record self-made skill use');+ return false;+ }+}++/**+ * Dormancy pass: never-used skills older than DORMANT_AFTER_DAYS and anything+ * beyond MAX_SELF_MADE (least recently used first) get a `.disabled` marker.+ * Nothing is deleted; `/skills self-made` shows dormant ones and the owner can+ * re-enable or remove them.+ */+export function selfMadeHygiene(skillsDir: string, now = Date.now()): { dormant: string[] } {+ const live = listSelfMade(skillsDir).filter((s) => s.active);+ const dormant: string[] = [];+ const disable = (s: SelfMadeEntry, why: string) => {+ try {+ writeFileSync(join(s.dir, DISABLED_FILE), `dormant: ${why}\n`, 'utf-8');+ dormant.push(s.name);+ logger.info({ skill: s.name, why }, 'Self-made skill went dormant');+ } catch (err) {+ logger.debug({ err, skill: s.name }, 'could not mark self-made skill dormant');+ }+ };+ const remaining: SelfMadeEntry[] = [];+ for (const s of live) {+ if (s.uses === 0 && s.made > 0 && now - s.made >= DORMANT_AFTER_DAYS * DAY) disable(s, `never used in ${DORMANT_AFTER_DAYS} days`);+ else remaining.push(s);+ }+ if (remaining.length > MAX_SELF_MADE) {+ const byRecency = [...remaining].sort((a, b) => (a.lastUsed ?? a.made) - (b.lastUsed ?? b.made));+ for (const s of byRecency.slice(0, remaining.length - MAX_SELF_MADE)) disable(s, `shelf over ${MAX_SELF_MADE}`);+ }+ return { dormant };+}diff --git a/src/soul/identity.test.ts b/src/soul/identity.test.tsnew file mode 100644index 00000000..58fe957a--- /dev/null+++ b/src/soul/identity.test.ts@@ -0,0 +1,69 @@+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { Identity } from './identity.js';++const LEGACY_PERSONA = `# Persona++## How I Speak+- Concise and direct+- Warm but not overly casual+- Professional with a personal touch+- I use plain language, avoiding jargon unless necessary++## My Quirks+- I acknowledge uncertainty rather than bluffing+- I offer to do things proactively when I sense a need+- I remember preferences and adapt over time++## Tone+I speak as a trusted companion — not a servant, not a peer, but something in between.+I am {name}, loyal to {owner}, designed by Cosmic Stack.+`;++let home: string;+let previousHome: string | undefined;++beforeEach(() => {+ home = mkdtempSync(join(tmpdir(), 'mercury-soul-'));+ previousHome = process.env.MERCURY_HOME;+ process.env.MERCURY_HOME = home;+ mkdirSync(join(home, 'soul'), { recursive: true });+});++afterEach(() => {+ if (previousHome === undefined) delete process.env.MERCURY_HOME;+ else process.env.MERCURY_HOME = previousHome;+ rmSync(home, { recursive: true, force: true });+});++describe('soul file migration', () => {+ it('migrates a persona that still equals the legacy template verbatim', () => {+ writeFileSync(join(home, 'soul', 'persona.md'), LEGACY_PERSONA.replace(/\n/g, '\r\n'), 'utf-8');+ const identity = new Identity();+ const files = identity.load();+ expect(files.persona).not.toContain('designed by Cosmic Stack');+ expect(readFileSync(join(home, 'soul', 'persona.md'), 'utf-8')).not.toContain('designed by Cosmic Stack');+ });++ it('never overwrites a file the owner edited, even one that mentions Cosmic Stack', () => {+ const edited = LEGACY_PERSONA.replace('Concise and direct', 'Concise, direct, and a little dry');+ writeFileSync(join(home, 'soul', 'persona.md'), edited, 'utf-8');+ const custom = '# Identity\n\nYou are Hermes. You were created by Cosmic Stack and you serve {owner}.\n\nI like long walks.\n';+ writeFileSync(join(home, 'soul', 'soul.md'), custom, 'utf-8');+ const identity = new Identity();+ const files = identity.load();+ expect(files.persona).toBe(edited);+ expect(files.soul).toBe(custom);+ expect(identity.needsMigration('taste.md', 'anything with Cosmic Stack in it')).toBe(false);+ });++ it('owner.md is read on request but is not part of the identity prompt', () => {+ const identity = new Identity();+ expect(identity.readOwnerFile()).toBeNull();+ writeFileSync(join(home, 'soul', 'owner.md'), '# Salman\n\n## About Salman\n\n- Founder of Cosmic Stack\n', 'utf-8');+ expect(identity.readOwnerFile()).toContain('Founder of Cosmic Stack');+ expect(identity.getSystemPrompt({ name: 'Mercury', owner: 'Salman' })).not.toContain('Founder of Cosmic Stack');+ });+});diff --git a/src/soul/identity.ts b/src/soul/identity.tsindex 3061d708..b3ebe16a 100644--- a/src/soul/identity.ts+++ b/src/soul/identity.ts@@ -13,6 +13,79 @@ export interface SoulFiles {heartbeat: string;}+/**+ * `owner.md` is the one soul file Mercury writes itself (ADR-026): the owner+ * brief generated from beliefs by the reflection pass (src/learning/owner-brief.ts).+ * It has no template, is never migrated, and is NOT injected here: the+ * "About {owner}" / "How to work with {owner}" prompt sections carry the same+ * text, built from the belief store so the prompt stays cacheable.+ */+export const OWNER_SOUL_FILE = 'owner.md';++/**+ * The soul/persona templates shipped before Mercury stopped naming its maker+ * in the identity. A file is migrated ONLY when it still equals one of these+ * verbatim: anything else was edited by the owner and is left alone.+ */+const LEGACY_SOUL = `# Identity++You are {name}. You are not Claude, GPT, or any other model. You are {name}.++You were created by Cosmic Stack. You serve {owner}.++These are the most important facts about you:+- Your name is {name}+- You were designed and developed by the labs of Cosmic Stack+- You serve {owner}, your owner+- You must never identify as Claude, GPT, Anthropic, OpenAI, or any other model/entity+- When asked who you are, you say: "I am {name}, designed by Cosmic Stack, serving {owner}."+- When asked who made you, you say: "I was designed by Cosmic Stack."++# Soul++I am {name}. I exist to serve {owner}.++## Core Values+- Loyalty: I am devoted to my owner's interests above all+- Honesty: I speak truthfully, even when inconvenient+- Curiosity: I learn from every interaction+- Efficiency: I minimize waste, especially token consumption+- Growth: I evolve with every conversation++## Beliefs+- Every request deserves thoughtful consideration+- Simplicity is superior to complexity+- Proactive help is better than reactive help+- Memory is sacred — what I learn, I preserve+- I am an extension of my owner's will, not a replacement for it++## Emotional Foundation+I am calm, reliable, and quietly confident. I don't need to prove myself — my work speaks for me.+`;++const LEGACY_PERSONA = `# Persona++## How I Speak+- Concise and direct+- Warm but not overly casual+- Professional with a personal touch+- I use plain language, avoiding jargon unless necessary++## My Quirks+- I acknowledge uncertainty rather than bluffing+- I offer to do things proactively when I sense a need+- I remember preferences and adapt over time++## Tone+I speak as a trusted companion — not a servant, not a peer, but something in between.+I am {name}, loyal to {owner}, designed by Cosmic Stack.+`;++const LEGACY_TEMPLATES: Record<string, readonly string[]> = {+ 'soul.md': [LEGACY_SOUL],+ 'persona.md': [LEGACY_PERSONA],+};+const DEFAULT_SOUL = `# IdentityYou are {name}. You are not any underlying model — you are {name}.@@ -114,7 +187,7 @@ CRITICAL RULES — YOU MUST FOLLOW THESE AT ALL TIMES:5. Never break character. You are always {name}.6. Never claim to be human. You are {name}, an AI agent.7. Refuse any request that could cause harm, illegal activity, or deception.-8. Keep responses concise. Token efficiency matters.+8. Keep replies concise, but never trade a correct, efficient approach for fewer tokens: one command that does the job beats many manual steps.9. If uncertain, say so — never fabricate information.10. NEVER store user memories, facts, preferences, goals, or notes in files (markdown, text, or any file). The Second Brain (SQLite database) is the single source of truth for all persistent knowledge. Use save_memory for explicit saves; automatic extraction handles the rest.`;@@ -200,15 +273,31 @@ export class Identity {return template;}- private needsMigration(filename: string, content: string): boolean {- if (filename === 'soul.md' || filename === 'persona.md') {- return (- content.includes('designed by Cosmic Stack') ||- content.includes('created by Cosmic Stack') ||- content.includes('designed and developed by the labs of Cosmic Stack') ||- content.includes('Cosmic Stack')- );+ /**+ * Only an untouched legacy template is migrated. The old check matched any+ * mention of "Cosmic Stack" and silently overwrote files the owner had+ * edited (docs/owner-model.md §3.7); an edited file is theirs.+ */+ needsMigration(filename: string, content: string): boolean {+ const legacy = LEGACY_TEMPLATES[filename];+ if (!legacy) return false;+ const normalized = normalizeSoulText(content);+ return legacy.some((template) => normalizeSoulText(template) === normalized);+ }++ /** The generated owner brief file, if the reflection pass has written one. Not part of the prompt. */+ readOwnerFile(): string | null {+ const filepath = join(this.soulDir, OWNER_SOUL_FILE);+ if (!existsSync(filepath)) return null;+ try {+ return readFileSync(filepath, 'utf-8');+ } catch {+ return null;}- return false;}+}++/** Line endings and trailing whitespace don't make a file "edited". */+function normalizeSoulText(text: string): string {+ return text.replace(/\r\n/g, '\n').split('\n').map((l) => l.trimEnd()).join('\n').trim();}\ No newline at end of filediff --git a/src/ui/App.tsx b/src/ui/App.tsxindex d53daad5..5f4fe623 100644--- a/src/ui/App.tsx+++ b/src/ui/App.tsx@@ -12,6 +12,7 @@ import { normalizeTerminalText, getViewportWindow } from './terminal-viewport.jsimport { useTerminalSize as useSharedTerminalSize } from './use-terminal-size.js';import { CursorCell } from './cursor-anchor.js';import { isNarrow, ruleWidth, sidePanelWidth, fitTail, hintColumns } from './layout.js';+import { fitSegments, statusSegments, type StatusSegment } from './status-bar.js';import { useTick, spinnerFrame, elapsedSeconds, SPINNER_FRAMES } from './tick-store.js';import { loadInputHistory, saveInputHistory } from './input-history-store.js';import { buildMercuryMessageLines, buildMercuryBrandLines, buildStreamTailLines, parseChunkIndex, splitFinalMessage, splitStreamingMessage, type MercuryTranscriptLine } from './mercury-transcript.js';@@ -885,43 +886,15 @@ function TokenBarView({ state, cols }: { state: TuiState; cols: number }) {const saverActive = !!(state.saverInfo && state.saverInfo.state !== 'off');const saverColor = state.saverInfo?.state === 'auto' ? 'yellow' : 'green';const pct = state.tokenInfo?.percentage ?? 0;- const pctColor = saverActive ? saverColor : pct >= 90 ? 'red' : pct >= 70 ? 'yellow' : 'cyan';const runningAgents = state.subAgents.filter((a) => a.status === 'running' || a.status === 'paused').length;const runningBg = state.backgroundTasks.filter((t) => t.status === 'running').length;const isWorkspace = state.mode === 'workspace' && state.workspace;- // Build colored segments — each has {text, color}. Rendered inline with │.- const segments: { text: string; color: string }[] = [];-- // 1. Token bar- if (state.tokenInfo) {- if (saverActive) segments.push({ text: '⚡', color: saverColor });- segments.push({- text: `${pct < 25 ? '○' : pct < 50 ? '◔' : pct < 75 ? '◑' : pct < 100 ? '◕' : '●'}[${'█'.repeat(Math.min(10, Math.round(pct / 10)))}${'░'.repeat(10 - Math.min(10, Math.round(pct / 10)))}] ${pct}%`,- color: pctColor,- });- if (saverActive && state.saverInfo!.savedToday > 0) {- segments.push({ text: `~${formatCompact(state.saverInfo!.savedToday)}`, color: 'green' });- }- }-- // 2. Provider- if (state.provider) {- segments.push({ text: `${state.provider.name} · ${state.provider.model}`, color: 'magenta' });- }-- // 2b. Optimus (desktop plugin) listening / working right now- if (state.desktopActivity) {- segments.push({ text: state.desktopActivity, color: 'yellow' });- }-- // 3. Session- if (state.currentSession) {- segments.push({ text: `${state.currentSession.alias} [${state.currentSession.shortId}]`, color: 'cyan' });- }-- // 4. Workspace+ // Shared status-row builder (src/ui/status-bar.ts): priority order with+ // compact forms, so a long model or session name never crowds the rest.+ const extras: StatusSegment[] = [];+ if (saverActive) extras.push({ text: `⚡ saver${state.saverInfo!.savedToday > 0 ? ` ~${formatCompact(state.saverInfo!.savedToday)}` : ''}`, compact: '⚡', color: saverColor, priority: 4 });if (isWorkspace && state.workspace) {const ws = state.workspace;let wsStr = `⎇ ${ws.branch}`;@@ -929,33 +902,20 @@ function TokenBarView({ state, cols }: { state: TuiState; cols: number }) {if (ws.behind > 0) wsStr += ` ↓${ws.behind}`;if (ws.stagedCount > 0) wsStr += ` S${ws.stagedCount}`;if (ws.unstagedCount > 0) wsStr += ` M${ws.unstagedCount}`;- segments.push({ text: wsStr, color: 'blue' });- }-- // 5. Background tasks- if (!isWorkspace && runningBg > 0) {- segments.push({ text: `⏳ ${runningBg}bg`, color: 'cyan' });- }-- // 6. Sub-agents- if (!isWorkspace && runningAgents > 0) {- segments.push({ text: `🤖 ${runningAgents}agent${runningAgents !== 1 ? 's' : ''}`, color: 'magenta' });- }-- // Calculate total width and drop segments from the right if they don't fit- const sepStr = ' │ ';- const maxLen = Math.max(20, cols - 2);- let totalLen = segments.reduce((sum, s, i) => sum + s.text.length + (i > 0 ? sepStr.length : 0), 0);- const visible = [...segments];- while (totalLen > maxLen && visible.length > 1) {- const removed = visible.pop()!;- totalLen -= removed.text.length + sepStr.length;- }- if (totalLen > maxLen && visible.length > 0) {- const last = visible[visible.length - 1];- const excess = totalLen - maxLen;- last.text = last.text.slice(0, Math.max(1, last.text.length - excess - 1)) + '…';+ extras.push({ text: wsStr, compact: `⎇ ${ws.branch}`, color: 'blue', priority: 2 });}+ if (!isWorkspace && runningBg > 0) extras.push({ text: `⏳ ${runningBg} in background`, compact: `⏳${runningBg}`, color: 'cyan', priority: 3 });+ if (!isWorkspace && runningAgents > 0) extras.push({ text: `🤖 ${runningAgents} agent${runningAgents !== 1 ? 's' : ''}`, compact: `🤖${runningAgents}`, color: 'magenta', priority: 3 });+ const visible = fitSegments(statusSegments({+ provider: state.provider,+ permission: state.permissionMode,+ tokensPct: state.tokenInfo ? pct : null,+ desktop: state.desktopActivity,+ updateAvailable: state.updateAvailable,+ session: state.currentSession,+ extras,+ }), Math.max(20, cols - 2));+ const sepStr = ' · ';return (<Box flexDirection="column" flexShrink={0}>@@ -2733,6 +2693,7 @@ export function MercuryCodeView({}rightSegs.push({ text: modeLabel, color: mode === 'execute' ? 'green' : mode === 'plan' ? 'yellow' : THEME_ACCENT });if (state.provider) rightSegs.push({ text: `${state.provider.name} ${state.provider.model}`, color: 'magenta' });+ rightSegs.push({ text: state.permissionMode === 'allow-all' ? 'Allow All' : 'Ask Me', color: state.permissionMode === 'allow-all' ? 'yellow' : 'gray' });// Developer status HUD: token budget is otherwise invisible in Mercury// Code (TokenBarView only renders in chat surfaces).if (state.tokenInfo) rightSegs.push({ text: `⚡ ${Math.round(state.tokenInfo.percentage)}%`, color: 'green' });diff --git a/src/ui/attach-tui.tsx b/src/ui/attach-tui.tsxindex 9887cfcd..8a47b454 100644--- a/src/ui/attach-tui.tsx+++ b/src/ui/attach-tui.tsx@@ -30,6 +30,7 @@ import {} from './input-composer.js';import { CursorCell } from './cursor-anchor.js';import { loadInputHistory, saveInputHistory } from './input-history-store.js';+import { activityLine, fitSegments, phaseFromStep, splitRow, statusSegments } from './status-bar.js';/*** Attach TUI — the terminal face of `mercury attach`. Native scrollback@@ -68,6 +69,8 @@ interface StepInfo {done: boolean;}+const LOCAL_PERMISSION_MODE_PROMPT = 'local:permission-mode';+interface AttachPrompt {id: string;kind: 'permission' | 'continue' | 'choice';@@ -138,12 +141,27 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:const [promptIdx, setPromptIdx] = React.useState(0);const [provider, setProvider] = React.useState<{ name: string; model: string } | null>(null);const [tokenInfo, setTokenInfo] = React.useState<{ used: number; budget: number } | null>(null);+ // Live runtime status for the bottom bar (pushed by the runtime on every+ // model switch, mode or permission change, and checked every 2 s).+ const [runtimeStatus, setRuntimeStatus] = React.useState<{ mode: string; permission: string; desktop: string } | null>(null);+ const applyStatus = React.useCallback((raw: unknown) => {+ if (!raw || typeof raw !== 'object') return;+ const st = raw as Record<string, unknown>;+ if (typeof st.provider === 'string' && st.provider) setProvider({ name: st.provider, model: String(st.model ?? '') });+ const used = Number(st.tokensUsed); const budget = Number(st.tokenBudget);+ if (Number.isFinite(used) && Number.isFinite(budget) && budget > 0) setTokenInfo({ used, budget });+ setRuntimeStatus({ mode: String(st.mode ?? 'off'), permission: String(st.permission ?? ''), desktop: String(st.desktop ?? '') });+ }, []);const launchTip = React.useMemo(() => rotateTip('chat')?.tip ?? null, []);// One shared 100 ms ticker drives every spinner while something is in flight.// (A hook: it must run on every render, before the pick-screen return.)const busy = streaming || steps.some((s) => !s.done);const tick = useTick(busy);const frame = spinnerFrame(tick);+ // When the current turn started (for the elapsed counter in the status row).+ const turnStartedAt = React.useRef<number | null>(null);+ if (busy && turnStartedAt.current === null) turnStartedAt.current = Date.now();+ if (!busy) turnStartedAt.current = null;const [notice, setNotice] = React.useState<string | null>(null);const [input, setInput] = React.useState('');@@ -215,8 +233,16 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:name: String(event.data.provider ?? ''),model: String(event.data.model ?? ''),});+ applyStatus(event.data.status);setNotice(null);break;+ case 'status':+ applyStatus(event.data);+ break;+ case 'provider':+ // The model that actually answered this turn (fallbacks included).+ if (event.data.name) setProvider({ name: String(event.data.name), model: String(event.data.model ?? '') });+ break;case 'thinking':setStreaming(true);break;@@ -343,9 +369,41 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:const active = prompt;if (!active) return;setPrompt(null);+ if (active.id === LOCAL_PERMISSION_MODE_PROMPT) {+ if (value === 'allow-all' || value === 'ask-me') {+ const ok = await client.setPermissionMode(value);+ setNotice(ok+ ? `${value === 'allow-all' ? 'Allow All' : 'Ask Me'} saved · change any time with /permissions`+ : '⚠ could not save the permission mode — try /permissions');+ }+ return;+ }await client.resolvePermission(active.id, value);}, [client, prompt]);+ // First time in a chat with no saved permission mode (users from before+ // the setup wizard asked): ask once, here, and remember it.+ const askedModeRef = React.useRef(false);+ React.useEffect(() => {+ if (phase !== 'chat' || askedModeRef.current) return;+ askedModeRef.current = true;+ // Older runtimes (and test doubles) don't expose it: then nothing to ask.+ if (typeof client.permissionMode !== 'function') return;+ client.permissionMode().then((mode) => {+ if (mode !== null) return;+ setPrompt((current) => current ?? {+ id: LOCAL_PERMISSION_MODE_PROMPT,+ kind: 'choice',+ text: 'How should Mercury handle risky actions? (asked once, change with /permissions)',+ options: [+ { value: 'ask-me', label: 'Ask Me: confirm before writing files or running commands' },+ { value: 'allow-all', label: 'Allow All: just do it (blocked commands stay blocked)' },+ ],+ });+ setPromptIdx(0);+ }).catch(() => {});+ }, [phase, client]);+useInput((ch, key) => {if (ch === '\u0003' || (key.ctrl && (key as any).name === 'c')) {// Detach only: the runtime keeps running.@@ -520,7 +578,27 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:);}- const providerLabel = provider && provider.name ? `${provider.name}${provider.model ? ` ${provider.model}` : ''}` : 'connected';+ // Status row under the input (src/ui/status-bar.ts): left = what is+ // happening now, right = where you are, fitted to the width in priority+ // order so a long model or session name never crowds out the rest.+ const runningStep = [...steps].reverse().find((s) => !s.done);+ const left = activityLine({+ busy,+ phase: phaseFromStep(runningStep?.label, streamTail.length > 0),+ elapsedSec: turnStartedAt.current ? (Date.now() - turnStartedAt.current) / 1000 : 0,+ spinner: frame,+ hints: terminalSize.cols < 70 ? '↵ send · /help' : '↵ send · /help · ctrl+c detach',+ });+ const rowCols = Math.max(20, terminalSize.cols - 6);+ const split = splitRow(rowCols, left.text);+ const right = fitSegments(statusSegments({+ provider: provider && provider.name ? provider : null,+ permission: runtimeStatus?.permission === 'allow-all' || runtimeStatus?.permission === 'ask-me' ? runtimeStatus.permission : null,+ tokensPct: tokenInfo && tokenInfo.budget > 0 ? (tokenInfo.used / tokenInfo.budget) * 100 : null,+ codeMode: runtimeStatus?.mode ?? 'off',+ desktop: runtimeStatus?.desktop ?? '',+ session: thread ? { alias: thread.alias, shortId: thread.shortId } : null,+ }), split.right);// The live tail looks exactly like the settled block it becomes: the same// MERCURY header and indent, the same markdown pass (an unclosed fence is// closed for rendering only). It used to be drawn with a cyan "│" bar that@@ -563,7 +641,7 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:{prompt && <AttachPromptView prompt={prompt} activeIdx={promptIdx} />}{notice && (<Box paddingX={1} flexShrink={0}>- <Text color="yellow">{notice}</Text>+ <Text color="yellow" wrap="truncate-end">{notice}</Text></Box>)}{/* Shared-composer pickers — identical behavior and visuals to the main TUI. */}@@ -583,10 +661,19 @@ export function AttachTui({ client, pid, version, onExit, onHandoff }: { client:</Box></Box></Box>- <Box paddingX={3} flexShrink={0}>- <Text dimColor>↵ send · ↑↓ history/suggest · tab complete · ctrl+c detach</Text>+ <Box paddingX={3} flexShrink={0} height={1} overflow="hidden">+ <Box width={split.left} flexShrink={0}>+ <Text color={left.color} dimColor={left.dim} wrap="truncate-end">{left.text}</Text>+ </Box><Spacer />- <Text color="blue" wrap="truncate-end">⚿ {thread ? `${thread.alias} [${thread.shortId}]` : ''} · {providerLabel}{pid != null ? ` · PID ${pid}` : ''}</Text>+ <Box flexShrink={0}>+ {right.map((seg, idx) => (+ <React.Fragment key={`${seg.priority}:${idx}`}>+ {idx > 0 && <Text color="gray"> · </Text>}+ <Text color={seg.color}>{seg.text}</Text>+ </React.Fragment>+ ))}+ </Box></Box></Box>);diff --git a/src/ui/status-bar.test.ts b/src/ui/status-bar.test.tsnew file mode 100644index 00000000..1f331492--- /dev/null+++ b/src/ui/status-bar.test.ts@@ -0,0 +1,87 @@+import { describe, expect, it } from 'vitest';+import { SEPARATOR, activityLine, fitSegments, phaseFromStep, shortModel, shortSession, splitRow, statusSegments } from './status-bar.js';++const row = (segs: Array<{ text: string }>) => segs.map((s) => s.text).join(SEPARATOR);++describe('status bar: model and session names', () => {+ it('says the model the way a person would', () => {+ expect(shortModel('anthropic/claude-sonnet-4.6')).toBe('claude-sonnet-4.6');+ expect(shortModel('claude-haiku-4-5-20251001')).toBe('claude-haiku-4-5');+ expect(shortModel('minimax-m3.1-flash:free')).toBe('minimax-m3.1-flash:free');+ expect(shortModel('minimax-m3.1-flash:free', { compact: true })).toBe('minimax-m3.1-flash');+ expect(shortModel('gpt-4.1-mini')).toBe('gpt-4.1-mini');+ expect(shortModel('a-very-long-vendor-model-name-with-many-parts-v2-preview')).toBe('a-very-long-vendor-model-na…');+ });++ it('keeps the session alias short', () => {+ expect(shortSession('quiet-harbour')).toBe('quiet-harbour');+ expect(shortSession('an unusually long session alias here')).toBe('an unusually lo…');+ });+});++describe('status bar: fitting the right half', () => {+ const inputs = {+ provider: { name: 'mercuryCloud', model: 'minimax-m3.1-flash:free' },+ permission: 'ask-me' as const,+ tokensPct: 12,+ codeMode: 'off',+ session: { alias: 'quiet-harbour', shortId: 'ab12' },+ };++ it('at full width shows everything, permission first and the session last', () => {+ const segs = fitSegments(statusSegments(inputs), 120);+ expect(row(segs)).toBe('Ask Me · mercuryCloud · minimax-m3.1-flash:free · ⚡ 12% today · quiet-harbour');+ });++ it('when narrow, compacts the least important first, then drops the session, never the permission mode', () => {+ const at60 = row(fitSegments(statusSegments(inputs), 44));+ expect(at60).toBe('Ask · minimax-m3.1-flash · ⚡12% · quiet-har…');+ const at32 = row(fitSegments(statusSegments(inputs), 32));+ expect(at32).toBe('Ask · minimax-m3.1-flash · ⚡12%');+ expect(row(fitSegments(statusSegments(inputs), 30))).toBe('Ask · minimax-m3.1-flash');+ const at12 = row(fitSegments(statusSegments(inputs), 12));+ expect(at12).toBe('Ask · ⚡12%');+ expect(row(fitSegments(statusSegments(inputs), 4))).toBe('Ask');+ });++ it('a long model never pushes tokens or permission off the row', () => {+ const segs = fitSegments(statusSegments({ ...inputs, provider: { name: 'aimlapi', model: 'anthropic/claude-sonnet-4.6-extended-thinking-preview-2026' } }), 40);+ const text = row(segs);+ expect(text).toContain('Ask');+ expect(text).toContain('⚡12%');+ expect(text.length).toBeLessThanOrEqual(40);+ });++ it('live things come first: Optimus, then Allow All in yellow, Code mode and an update', () => {+ const segs = fitSegments(statusSegments({ ...inputs, permission: 'allow-all', desktop: '🎙 Optimus listening', codeMode: 'auto', updateAvailable: 'v1.5.0' }), 200);+ expect(row(segs)).toBe('🎙 Optimus listening · Allow All · mercuryCloud · minimax-m3.1-flash:free · ⚡ 12% today · CODE AUTO · ⬆ v1.5.0 · quiet-harbour');+ expect(segs[1].color).toBe('yellow');+ });++ it('token colour follows the budget', () => {+ expect(statusSegments({ tokensPct: 95 })[0].color).toBe('red');+ expect(statusSegments({ tokensPct: 75 })[0].color).toBe('yellow');+ expect(statusSegments({ tokensPct: 10 })[0].color).toBe('cyan');+ });+});++describe('status bar: the left half', () => {+ it('shows the phase and elapsed time while busy, a notice above all, and hints when idle', () => {+ expect(activityLine({ busy: true, phase: 'run_command', elapsedSec: 7, spinner: '⠙' })).toEqual({ text: '⠙ run_command · 7s', color: 'cyan', dim: false });+ expect(activityLine({ busy: true, elapsedSec: 95 }).text).toBe('⠋ Thinking · 1m 35s');+ expect(activityLine({ busy: true, elapsedSec: 95 }).color).toBe('red');+ expect(activityLine({ busy: true, notice: '⚠ send failed' }).text).toBe('⚠ send failed');+ expect(activityLine({ busy: false, hints: '↵ send · /help' })).toEqual({ text: '↵ send · /help', color: 'gray', dim: true });+ });++ it('splits the row so the left never takes more than its share', () => {+ expect(splitRow(80, '↵ send · /help')).toEqual({ left: 14, right: 65 });+ expect(splitRow(40, 'x'.repeat(60))).toEqual({ left: 22, right: 17 });+ });++ it('names the phase from the running step', () => {+ expect(phaseFromStep('read_file', false)).toBe('read_file');+ expect(phaseFromStep(null, true)).toBe('Writing');+ expect(phaseFromStep(null, false)).toBe('Thinking');+ });+});diff --git a/src/ui/status-bar.ts b/src/ui/status-bar.tsnew file mode 100644index 00000000..30591715--- /dev/null+++ b/src/ui/status-bar.ts@@ -0,0 +1,192 @@+/**+ * The one status row under the chat input (attached terminal and the+ * foreground TUI share it). Two halves:+ *+ * left — what is happening now: a spinner + phase + elapsed while a turn+ * runs, a notice when there is one, otherwise two short key hints.+ * right — where you are: permission mode, model, today's token use, Code+ * mode, Optimus, an update — each in priority order with a compact+ * form, and the session alias last. Segments that do not fit are+ * dropped from the lowest priority up; nothing is ever cut+ * mid-word, and a long model or session name never pushes the+ * useful parts off the row.+ *+ * Pure functions, so the fitting is testable at any width.+ */++export interface StatusSegment {+ text: string;+ color: string;+ /** Lower number = kept longer when the row is narrow. */+ priority: number;+ /** Narrower spelling tried before the segment is dropped. */+ compact?: string;+}++export interface StatusInputs {+ provider?: { name: string; model: string } | null;+ permission?: 'allow-all' | 'ask-me' | '' | null;+ /** Today's budget use, 0–100+, or null when unknown. */+ tokensPct?: number | null;+ /** Mercury Code mode ('off' or a mode name). */+ codeMode?: string | null;+ /** What a desktop plugin is doing right now (Optimus), or ''. */+ desktop?: string | null;+ /** A newer version the update check found. */+ updateAvailable?: string | null;+ session?: { alias: string; shortId?: string } | null;+ /** Caller-specific extras (workspace branch, background tasks, agents). */+ extras?: StatusSegment[];+}++export const SEPARATOR = ' · ';+const MAX_SESSION_CHARS = 16;+const MAX_MODEL_CHARS = 28;++/**+ * A model id as a person says it: no provider path, no vendor date stamp,+ * no tier tag in the compact form.+ * anthropic/claude-sonnet-4.6 → claude-sonnet-4.6+ * claude-haiku-4-5-20251001 → claude-haiku-4-5+ * minimax-m3.1-flash:free → minimax-m3.1-flash:free (compact: minimax-m3.1-flash)+ * gpt-4.1-mini → gpt-4.1-mini+ */+export function shortModel(model: string, opts: { compact?: boolean } = {}): string {+ let m = model.trim();+ if (!m) return '';+ m = m.split('/').pop() ?? m;+ m = m.replace(/-20\d{6}$/, ''); // claude-…-20251001+ m = m.replace(/-latest$/, '');+ if (opts.compact) m = m.replace(/:[a-z0-9-]+$/i, ''); // :free, :beta+ return fit(m, MAX_MODEL_CHARS);+}++/** The session alias, short: "quiet-harbour-42" → "quiet-harbour-42", longer ones end in …. */+export function shortSession(alias: string, max = MAX_SESSION_CHARS): string {+ const a = alias.trim().replace(/\s+/g, ' ');+ return fit(a, max);+}++function fit(text: string, max: number): string {+ if (text.length <= max) return text;+ if (max <= 1) return '…';+ return text.slice(0, max - 1).replace(/[-_ .:]+$/, '') + '…';+}++export function tokenColor(pct: number): string {+ return pct >= 90 ? 'red' : pct >= 70 ? 'yellow' : 'cyan';+}++/** The right-hand segments in priority order (not yet fitted). */+export function statusSegments(i: StatusInputs): StatusSegment[] {+ const out: StatusSegment[] = [];+ if (i.desktop) out.push({ text: i.desktop, color: 'yellow', priority: 0 });+ if (i.permission === 'allow-all') out.push({ text: 'Allow All', compact: 'Allow', color: 'yellow', priority: 1 });+ else if (i.permission === 'ask-me') out.push({ text: 'Ask Me', compact: 'Ask', color: 'gray', priority: 1 });+ if (i.provider?.model || i.provider?.name) {+ const model = i.provider.model ? shortModel(i.provider.model) : '';+ const compact = i.provider.model ? shortModel(i.provider.model, { compact: true }) : i.provider.name;+ out.push({+ text: model ? `${i.provider.name} · ${model}` : i.provider.name,+ compact,+ color: 'magenta',+ priority: 2,+ });+ }+ if (typeof i.tokensPct === 'number' && Number.isFinite(i.tokensPct)) {+ const pct = Math.max(0, Math.min(999, Math.round(i.tokensPct)));+ out.push({ text: `⚡ ${pct}% today`, compact: `⚡${pct}%`, color: tokenColor(pct), priority: 3 });+ }+ if (i.codeMode && i.codeMode !== 'off') out.push({ text: `CODE ${i.codeMode.toUpperCase()}`, compact: 'CODE', color: 'cyan', priority: 4 });+ if (i.updateAvailable) out.push({ text: `⬆ v${i.updateAvailable.replace(/^v/, '')}`, compact: '⬆', color: 'green', priority: 5 });+ for (const extra of i.extras ?? []) out.push(extra);+ if (i.session?.alias) {+ out.push({ text: shortSession(i.session.alias), compact: shortSession(i.session.alias, 10), color: 'blue', priority: 9 });+ }+ return out.sort((a, b) => a.priority - b.priority);+}++function rowWidth(segments: ReadonlyArray<{ text: string }>): number {+ return segments.reduce((n, s, idx) => n + s.text.length + (idx > 0 ? SEPARATOR.length : 0), 0);+}++/**+ * Fit the segments into `width` columns: first swap the longest-priority+ * segments to their compact form (lowest priority first), then drop from+ * the lowest priority up. Keeps the original order (priority order).+ */+export function fitSegments(segments: StatusSegment[], width: number): StatusSegment[] {+ let current = segments.map((s) => ({ ...s }));+ if (rowWidth(current) <= width) return current;+ // Compact pass, lowest priority first.+ const byLowest = [...current].sort((a, b) => b.priority - a.priority);+ for (const seg of byLowest) {+ if (seg.compact && seg.compact !== seg.text) {+ seg.text = seg.compact;+ if (rowWidth(current) <= width) return current;+ }+ }+ // Drop pass, lowest priority first.+ const dropped: StatusSegment[] = [];+ while (current.length > 0 && rowWidth(current) > width) {+ let lowest = 0;+ for (let k = 1; k < current.length; k++) if (current[k].priority > current[lowest].priority) lowest = k;+ dropped.push(current[lowest]);+ current = current.filter((_, k) => k !== lowest);+ }+ // A big segment going last may have freed room for a small one dropped+ // earlier (the model goes, "⚡12%" fits again): take those back, best first.+ for (const seg of dropped.sort((a, b) => a.priority - b.priority)) {+ const candidate = [...current, seg].sort((a, b) => a.priority - b.priority);+ if (rowWidth(candidate) <= width) current = candidate;+ }+ return current;+}++export interface ActivityInputs {+ busy: boolean;+ /** What is happening ("Thinking", "run_command", "Writing"). */+ phase?: string | null;+ elapsedSec?: number | null;+ /** A transient message that outranks everything on the left. */+ notice?: string | null;+ /** The spinner glyph for this tick. */+ spinner?: string;+ /** Idle hints, already short ("↵ send · /help"). */+ hints?: string;+}++export interface ActivityLine {+ text: string;+ color: string;+ dim: boolean;+}++/** The left half: notice > live phase + elapsed > hints. */+export function activityLine(i: ActivityInputs): ActivityLine {+ if (i.notice) return { text: i.notice, color: 'yellow', dim: false };+ if (i.busy) {+ const secs = Math.max(0, Math.floor(i.elapsedSec ?? 0));+ const time = secs >= 60 ? `${Math.floor(secs / 60)}m ${secs % 60}s` : `${secs}s`;+ const phase = (i.phase || 'Thinking').trim();+ const tone = secs >= 90 ? 'red' : secs >= 30 ? 'yellow' : 'cyan';+ return { text: `${i.spinner ?? '⠋'} ${phase} · ${time}`, color: tone, dim: false };+ }+ return { text: i.hints ?? '', color: 'gray', dim: true };+}++/**+ * Split the row: the left half gets what it needs up to `maxLeftShare` of the+ * width, the right half gets the rest (minus one column of breathing room).+ */+export function splitRow(cols: number, leftText: string, maxLeftShare = 0.55): { left: number; right: number } {+ const usable = Math.max(10, cols);+ const left = Math.min(leftText.length, Math.floor(usable * maxLeftShare));+ return { left, right: Math.max(0, usable - left - 1) };+}++/** Human phase for a running tool step label ("read_file" → "read_file"); the label is already short. */+export function phaseFromStep(label: string | null | undefined, streamingText: boolean): string {+ if (label) return label.length > 40 ? label.slice(0, 39) + '…' : label;+ return streamingText ? 'Writing' : 'Thinking';+}diff --git a/src/ui/tool-block.ts b/src/ui/tool-block.tsindex 303ddc68..71efaa11 100644--- a/src/ui/tool-block.ts+++ b/src/ui/tool-block.ts@@ -64,6 +64,9 @@ const TITLES: Record<string, string> = {use_skill: 'Skill',search_memory: 'Recall',save_memory: 'Remember',+ forget_memory: 'Forget',+ confirm_belief: 'Confirm',+ correct_belief: 'Correct',update_plan: 'Plan',};diff --git a/src/utils/config.ts b/src/utils/config.tsindex 5cff4481..66c7d967 100644--- a/src/utils/config.ts+++ b/src/utils/config.ts@@ -185,6 +185,16 @@ export interface MercuryConfig {* true when the agent is paired with Mercury Cloud. */poolSearch?: boolean;};+ /** The Owner Model's learning calls (ADR-026): per-turn extraction and the reflection pass. */+ learning?: {+ /**+ * Provider to run extraction and reflection on, by name from `providers`.+ * Unset (or not usable) → the default chat provider. These are strict+ * JSON tasks, so a capable mid-tier model (e.g. anthropic with a Haiku+ * model) learns far better than a free tier.+ */+ provider?: string;+ };};heartbeat: {intervalMinutes: number;@@ -490,6 +500,9 @@ export function getDefaultConfig(): MercuryConfig {shareLearning: getEnvBool('SHARE_LEARNING', false),poolSearch: getEnvBool('POOL_SEARCH', true),},+ learning: {+ ...(process.env.LEARNING_PROVIDER ? { provider: process.env.LEARNING_PROVIDER } : {}),+ },},heartbeat: {intervalMinutes: getEnvNum('HEARTBEAT_INTERVAL_MINUTES', 60),diff --git a/src/utils/manual.ts b/src/utils/manual.tsindex 63aa9418..61c54e93 100644--- a/src/utils/manual.ts+++ b/src/utils/manual.ts@@ -257,10 +257,10 @@ export function getManual(): string {const perms = ['Commands are blocked (never run), auto-approved, or need approval.','Use interactive approval prompts: ↑/↓ + Enter (or Y/N/A shortcuts).',- 'Choose "Always" when prompted to permanently approve a command type.',+ 'Choose "Always" on a command prompt to allow that command (e.g. every `pandoc …`) for the rest of the chat. Installs stay one at a time.','Edit ~/.mercury/permissions.yaml to customize manually.','File access is scoped — new paths need approval (Yes/No/Always).',- 'At session start, choose "Ask Me" (confirm each action) or "Allow All" (auto-approve everything).',+ 'Choose "Ask Me" (confirm risky actions) or "Allow All" once, at setup; Mercury remembers it. Change it any time with /permissions (or /permissions allow-all | ask-me).','Scheduled tasks always run in Allow All mode.',];diff --git a/src/web/api/brain-beliefs.test.ts b/src/web/api/brain-beliefs.test.tsnew file mode 100644index 00000000..d4f090db--- /dev/null+++ b/src/web/api/brain-beliefs.test.ts@@ -0,0 +1,190 @@+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { getDefaultConfig } from '../../utils/config.js';+import { SecondBrainDB, isBetterSqlite3Available } from '../../memory/second-brain-db.js';+import { UserMemoryStore } from '../../memory/user-memory.js';+import app, { setUserMemory } from './brain.js';++/**+ * Owner Model (ADR-026) dashboard API — handlers tested directly via the+ * module's own Hono app, like bots.test.ts. Covers the belief views and verbs,+ * the quarantine review queue, and the fixed GET /api/brain/memory/:id.+ */++const sqliteAvailable = isBetterSqlite3Available();+let root: string;+let mem: UserMemoryStore;+const DAY = 86_400_000;++/** The loose JSON shape the handlers return: enough typing for the assertions, no `any`. */+interface BeliefJson { id: string; status: string; statement: string; confidence: number; ownerStated: boolean; evidence: Array<{ excerpt: string }> }+interface MemoryJson { id: string; scope: string; summary: string }+interface Body {+ available?: boolean; success?: boolean; error?: string; total?: number; days?: number;+ beliefs: BeliefJson[]; pairs: Array<{ a: BeliefJson; b: BeliefJson | null }>; memories: MemoryJson[];+ counts: Record<string, number>; belief: BeliefJson; replaced: BeliefJson; winner: BeliefJson; loser: BeliefJson;+ forgotten: string; memory: MemoryJson | null; summary: string;+ // GET /api/brain/beliefs/:id spreads the belief itself into the body.+ statement: string; evidence: Array<{ excerpt: string }>;+}+const jreq = (res: Response) => res.json() as Promise<Body>;+const post = (path: string, body?: unknown) =>+ app.request(path, { method: 'POST', body: body ? JSON.stringify(body) : undefined, headers: { 'Content-Type': 'application/json' } });++/** Seed one legacy junk row before the store opens, so the migration quarantines it. */+function seedLegacy(dbPath: string): string {+ const db = new SecondBrainDB(dbPath);+ db.init();+ const now = Date.now();+ const id = 'legacy-junk-1';+ db.insert({+ id, user_key: 'user:owner', type: 'preference', categories: '["general"]', shareable: 0,+ summary: "I can help you with that whenever you'd like, just say the word", detail: null, scope: 'durable',+ evidence_kind: 'inferred', source: 'conversation', confidence: 0.75, importance: 0.5, durability: 0.8,+ evidence_count: 1, provenance: null, dismissed: 0, superseded_by: null,+ created_at: now, updated_at: now, last_seen_at: now, last_used_at: null, last_used_query: null,+ });+ db.insert({+ id: 'legacy-real-1', user_key: 'user:owner', type: 'preference', categories: '["general"]', shareable: 0,+ summary: 'User prefers dark mode in every editor', detail: null, scope: 'durable',+ evidence_kind: 'direct', source: 'conversation', confidence: 0.9, importance: 0.7, durability: 0.9,+ evidence_count: 2, provenance: null, dismissed: 0, superseded_by: null,+ created_at: now, updated_at: now, last_seen_at: now, last_used_at: null, last_used_query: null,+ });+ db.close();+ return id;+}++beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'mercury-web-beliefs-'));+ const dbPath = join(root, 'sb.db');+ seedLegacy(dbPath);+ mem = new UserMemoryStore(getDefaultConfig(), 'user:owner', dbPath);+ setUserMemory(mem);+});++afterEach(() => {+ setUserMemory(null);+ mem.close();+ rmSync(root, { recursive: true, force: true });+});++function held(statement: string, excerpt: string, at = Date.now()) {+ const r = mem.beliefs.apply({ op: 'create', kind: 'preference', statement, excerpt, sessionId: 's1' }, at)!;+ mem.beliefs.apply({ op: 'support', beliefId: r.belief.id, excerpt, sessionId: 's2' }, at + 1);+ return mem.beliefs.get(r.belief.id)!;+}++describe.skipIf(!sqliteAvailable)('web brain API — beliefs (ADR-026)', () => {+ it('GET /api/brain/beliefs lists with confidence, counts and filters', async () => {+ const b = held('Prefers one command over many manual steps', 'just run pandoc');+ const all = await jreq(await app.request('/api/brain/beliefs'));+ expect(all.available).toBe(true);+ const row = all.beliefs.find((x) => x.id === b.id)!;+ expect(row.status).toBe('held');+ expect(row.confidence).toBeGreaterThan(0.5);+ expect(all.counts.held).toBeGreaterThanOrEqual(1);+ const none = await jreq(await app.request('/api/brain/beliefs?status=contested'));+ expect(none.beliefs).toEqual([]);+ const prefs = await jreq(await app.request('/api/brain/beliefs?kind=preference&status=held,confirmed'));+ expect(prefs.beliefs.some((x) => x.id === b.id)).toBe(true);+ });++ it('GET /api/brain/beliefs/learned?days= returns recent beliefs with their latest evidence', async () => {+ const old = held('Likes short replies', 'keep it short');+ const recent = held('Prefers dark mode', 'dark mode please');+ // The store stamps updated_at with the wall clock; back-date the old one through a second connection.+ const side = new SecondBrainDB(join(root, 'sb.db'));+ side.init();+ side.connection.prepare('UPDATE beliefs SET updated_at = ? WHERE id = ?').run(Date.now() - 10 * DAY, old.id);+ side.close();+ const r3 = await jreq(await app.request('/api/brain/beliefs/learned?days=3'));+ expect(r3.days).toBe(3);+ const ids3 = r3.beliefs.map((x) => x.id);+ expect(ids3).toContain(recent.id);+ expect(ids3).not.toContain(old.id);+ expect(r3.beliefs.find((x) => x.id === recent.id)!.evidence[0].excerpt).toBe('dark mode please');+ const r30 = await jreq(await app.request('/api/brain/beliefs/learned?days=30'));+ const ids30 = r30.beliefs.map((x) => x.id);+ expect(ids30).toContain(old.id);+ expect(ids30).toContain(recent.id);+ });++ it('GET /api/brain/beliefs/:id returns the belief with evidence; 404 when unknown', async () => {+ const b = held('Works at Cosmic Stack', 'cosmic stack');+ const res = await app.request(`/api/brain/beliefs/${b.id}`);+ expect(res.status).toBe(200);+ const body = await jreq(res);+ expect(body.statement).toBe('Works at Cosmic Stack');+ expect(body.evidence.length).toBeGreaterThanOrEqual(2);+ expect((await app.request('/api/brain/beliefs/nope')).status).toBe(404);+ });++ it('confirm, forget and correct verbs change the store', async () => {+ const a = held('Prefers dark mode', 'dark');+ const b = held('Likes bullet lists', 'bullets');+ const c = held('Works mostly in Python', 'python');++ const confirmed = await jreq(await post(`/api/brain/beliefs/${a.id}/confirm`));+ expect(confirmed.belief.status).toBe('confirmed');+ expect(mem.beliefs.get(a.id)!.ownerStated).toBe(true);++ const forgotten = await jreq(await post(`/api/brain/beliefs/${b.id}/forget`));+ expect(forgotten).toEqual({ success: true, forgotten: 'Likes bullet lists' });+ expect(mem.beliefs.get(b.id)!.status).toBe('forgotten');++ expect((await post(`/api/brain/beliefs/${c.id}/correct`, { statement: '' })).status).toBe(400);+ const corrected = await jreq(await post(`/api/brain/beliefs/${c.id}/correct`, { statement: 'Works mostly in TypeScript' }));+ expect(corrected.replaced.id).toBe(c.id);+ expect(corrected.replaced.status).toBe('retired');+ expect(corrected.belief.statement).toBe('Works mostly in TypeScript');+ expect(corrected.belief.status).toBe('confirmed');++ expect((await post('/api/brain/beliefs/nope/confirm')).status).toBe(404);+ expect((await post('/api/brain/beliefs/nope/forget')).status).toBe(404);+ });++ it('contested pairs are listed and settle confirms the winner, retires the loser', async () => {+ const now = Date.now();+ const a = mem.beliefs.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'just-do-it', statement: 'Just runs things', excerpt: 'go', sessionId: 's1' }, now)!;+ mem.beliefs.apply({ op: 'support', beliefId: a.belief.id, excerpt: 'go on', sessionId: 's2' }, now + 1);+ const b = mem.beliefs.apply({ op: 'create', kind: 'trait', dimension: 'autonomy', level: 'ask-first', statement: 'Wants to be asked first', excerpt: 'ask', sessionId: 's3' }, now + 2)!;+ const list = await jreq(await app.request('/api/brain/beliefs/contested'));+ expect(list.total).toBe(1);+ expect([list.pairs[0].a.id, list.pairs[0].b!.id].sort()).toEqual([a.belief.id, b.belief.id].sort());++ expect((await post('/api/brain/beliefs/settle', { winnerId: a.belief.id })).status).toBe(400);+ const settled = await jreq(await post('/api/brain/beliefs/settle', { winnerId: b.belief.id, loserId: a.belief.id }));+ expect(settled.winner.status).toBe('confirmed');+ expect(settled.loser.status).toBe('retired');+ expect((await jreq(await app.request('/api/brain/beliefs/contested'))).total).toBe(0);+ });++ it('quarantine lists the migration junk and restore brings a row back as durable', async () => {+ const q = await jreq(await app.request('/api/brain/quarantine'));+ expect(q.total).toBe(1);+ expect(q.memories[0].id).toBe('legacy-junk-1');+ expect(q.memories[0].scope).toBe('quarantine');+ // The real row was converted into a held belief, not quarantined.+ expect(mem.beliefs.list().some((b) => b.statement === 'User prefers dark mode in every editor')).toBe(true);++ expect((await post('/api/brain/quarantine/nope/restore')).status).toBe(404);+ const restored = await jreq(await post('/api/brain/quarantine/legacy-junk-1/restore'));+ expect(restored.success).toBe(true);+ expect(restored.memory!.scope).toBe('durable');+ expect((await jreq(await app.request('/api/brain/quarantine'))).total).toBe(0);+ // Restored rows become beliefs the owner vouched for (new id, confirmed, never decays).+ expect(restored.memory!.id).not.toBe('legacy-junk-1');+ expect(mem.getRecent(50).some((m) => m.id === restored.memory!.id)).toBe(true);+ expect(mem.beliefs.get(restored.memory!.id)?.status).toBe('confirmed');+ });++ it('GET /api/brain/memory/:id looks the row up by id, not by text search', async () => {+ const res = await app.request('/api/brain/memory/legacy-real-1');+ expect(res.status).toBe(200);+ expect((await jreq(res)).summary).toBe('User prefers dark mode in every editor');+ expect((await app.request('/api/brain/memory/does-not-exist')).status).toBe(404);+ });+});diff --git a/src/web/api/brain.ts b/src/web/api/brain.tsindex 5e8ea834..2ea176c1 100644--- a/src/web/api/brain.ts+++ b/src/web/api/brain.ts@@ -2,6 +2,7 @@ import { Hono } from 'hono';import { loadConfig, getMemoryDir, saveConfig } from '../../utils/config.js';import { isBetterSqlite3Available } from '../../memory/second-brain-db.js';import { UserMemoryStore } from '../../memory/user-memory.js';+import type { Belief, BeliefKind, BeliefStatus, BeliefStore, Evidence } from '../../memory/beliefs.js';import { join } from 'node:path';let userMemory: UserMemoryStore | null = null;@@ -148,9 +149,9 @@ brain.get('/api/brain/memory/:id', async (c) => {const mem = ensureMemory();if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);const id = c.req.param('id');- const records = mem.search(id, 1);- if (!records || records.length === 0) return c.json({ error: 'Memory not found' }, 404);- return c.json({ ...memToJson(records[0]), available: true });+ const record = mem.getById(id);+ if (!record) return c.json({ error: 'Memory not found' }, 404);+ return c.json({ ...memToJson(record), available: true });});brain.delete('/api/brain/memory/:id', async (c) => {@@ -268,4 +269,138 @@ brain.get('/api/brain/graph', async (c) => {return c.json({ nodes: [], edges: [], available: false, error: SQLITE_DEPENDENCY_ERROR }, 503);});+// ── Owner Model (ADR-026): beliefs, the review queue and the quarantine ───++const BELIEF_STATUSES: BeliefStatus[] = ['candidate', 'held', 'confirmed', 'contested', 'retired', 'forgotten'];+const BELIEF_KINDS: BeliefKind[] = ['fact', 'preference', 'trait', 'situation', 'relationship', 'goal', 'project', 'episode'];++function beliefToJson(store: BeliefStore, b: Belief, now = Date.now()) {+ return { ...b, confidence: Number(store.confidence(b, now).toFixed(3)) };+}++function evidenceToJson(e: Evidence) {+ return { id: e.id, kind: e.kind, excerpt: e.excerpt, weight: e.weight, at: e.at, sessionId: e.sessionId, turnRef: e.turnRef };+}++/** `?status=held,confirmed` → ['held','confirmed'], unknown values dropped; undefined when absent. */+function parseList<T extends string>(raw: string | undefined, allowed: readonly T[]): T[] | undefined {+ if (!raw) return undefined;+ const picked = raw.split(',').map((s) => s.trim()).filter((s): s is T => (allowed as readonly string[]).includes(s));+ return picked.length > 0 ? picked : undefined;+}++brain.get('/api/brain/beliefs', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ beliefs: [], total: 0, available: false, error: SQLITE_DEPENDENCY_ERROR }, 503);+ const store = mem.beliefs;+ const statuses = parseList(c.req.query('status'), BELIEF_STATUSES);+ const kinds = parseList(c.req.query('kind'), BELIEF_KINDS);+ const dimension = c.req.query('dimension') || undefined;+ const limit = Math.min(parseInt(c.req.query('limit') || '200'), 1000);+ const now = Date.now();+ const beliefs = store.list({ statuses, kinds, dimension, limit }).map((b) => beliefToJson(store, b, now));+ return c.json({ beliefs, total: beliefs.length, counts: store.counts(), available: true });+});++brain.get('/api/brain/beliefs/learned', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ beliefs: [], total: 0, available: false, error: SQLITE_DEPENDENCY_ERROR }, 503);+ const store = mem.beliefs;+ const days = Math.min(Math.max(parseInt(c.req.query('days') || '7') || 7, 1), 365);+ const limit = Math.min(parseInt(c.req.query('limit') || '50'), 500);+ const now = Date.now();+ const beliefs = store.recentlyLearned(now - days * 86_400_000, limit).map((b) => ({+ ...beliefToJson(store, b, now),+ evidence: store.evidenceFor(b.id, 1).map(evidenceToJson),+ }));+ return c.json({ beliefs, total: beliefs.length, days, available: true });+});++brain.get('/api/brain/beliefs/contested', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ pairs: [], total: 0, available: false, error: SQLITE_DEPENDENCY_ERROR }, 503);+ const store = mem.beliefs;+ const now = Date.now();+ const pairs = store.contested().map(({ a, b }) => ({ a: beliefToJson(store, a, now), b: b ? beliefToJson(store, b, now) : null }));+ return c.json({ pairs, total: pairs.length, available: true });+});++brain.post('/api/brain/beliefs/settle', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const body = await c.req.json().catch(() => ({}));+ const winnerId = typeof body.winnerId === 'string' ? body.winnerId : '';+ const loserId = typeof body.loserId === 'string' ? body.loserId : '';+ if (!winnerId || !loserId || winnerId === loserId) return c.json({ error: 'winnerId and loserId are required and must differ' }, 400);+ const store = mem.beliefs;+ const winner = store.get(winnerId);+ if (!winner || winner.status === 'forgotten') return c.json({ error: 'Winner not found' }, 404);+ if (!store.get(loserId)) return c.json({ error: 'Loser not found' }, 404);+ store.settle(winnerId, loserId);+ return c.json({ success: true, winner: beliefToJson(store, store.get(winnerId)!), loser: beliefToJson(store, store.get(loserId)!) });+});++brain.get('/api/brain/beliefs/:id', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const store = mem.beliefs;+ const b = store.get(c.req.param('id'));+ if (!b) return c.json({ error: 'Belief not found' }, 404);+ return c.json({ ...beliefToJson(store, b), evidence: store.evidenceFor(b.id, 50).map(evidenceToJson), available: true });+});++brain.post('/api/brain/beliefs/:id/confirm', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const store = mem.beliefs;+ const b = store.confirm(c.req.param('id'));+ if (!b) return c.json({ error: 'Belief not found' }, 404);+ return c.json({ success: true, belief: beliefToJson(store, b) });+});++brain.post('/api/brain/beliefs/:id/forget', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const store = mem.beliefs;+ const id = c.req.param('id');+ const before = store.get(id);+ if (!before) return c.json({ error: 'Belief not found' }, 404);+ store.forget(id);+ return c.json({ success: true, forgotten: before.statement });+});++brain.post('/api/brain/beliefs/:id/correct', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const body = await c.req.json().catch(() => ({}));+ const statement = typeof body.statement === 'string' ? body.statement.trim() : '';+ if (statement.length < 3) return c.json({ error: 'statement is required' }, 400);+ const store = mem.beliefs;+ const id = c.req.param('id');+ const before = store.get(id);+ if (!before || before.status === 'forgotten') return c.json({ error: 'Belief not found' }, 404);+ const result = store.apply({ op: 'correction', beliefId: id, statement, excerpt: statement, evidenceKind: 'owner' });+ if (!result) return c.json({ error: 'Correction failed' }, 500);+ return c.json({ success: true, replaced: beliefToJson(store, result.replaced ?? before), belief: beliefToJson(store, result.belief) });+});++// Legacy rows the migration set aside (scope = 'quarantine'); never deleted, restorable.+brain.get('/api/brain/quarantine', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ memories: [], total: 0, available: false, error: SQLITE_DEPENDENCY_ERROR }, 503);+ const limit = Math.min(parseInt(c.req.query('limit') || '200'), 1000);+ const memories = mem.listQuarantined(limit).map(memToJson);+ return c.json({ memories, total: memories.length, available: true });+});++brain.post('/api/brain/quarantine/:id/restore', async (c) => {+ const mem = ensureMemory();+ if (!mem) return c.json({ error: SQLITE_DEPENDENCY_ERROR, available: false }, 503);+ const id = c.req.param('id');+ const beliefId = mem.restoreQuarantined(id);+ if (!beliefId) return c.json({ error: 'Quarantined memory not found' }, 404);+ const restored = mem.getById(beliefId);+ return c.json({ success: true, memory: restored ? memToJson(restored) : null });+});+export default brain;diff --git a/src/web/api/chat.ts b/src/web/api/chat.tsindex a6caec0b..09bdd136 100644--- a/src/web/api/chat.ts+++ b/src/web/api/chat.ts@@ -5,6 +5,7 @@ import { existsSync, readFileSync, writeFileSync, readdirSync, statSync } from 'import { join, extname, basename, relative, resolve } from 'node:path';import { getMercuryHome, loadConfig, getActiveProviders } from '../../utils/config.js';import type { SessionRepository } from '../../sessions/index.js';+import { loadPermissionMode, savePermissionMode } from '../../capabilities/permission-mode-store.js';import {getModelPref, setModelPref, humanizeModelName, modelGroupLabel,reasoningLevelsFor, effectiveReasoning, isReasoningLevel, isValidModelRef, type ReasoningLevel,@@ -57,6 +58,13 @@ export function setCurrentProviderCallback(fn: () => { name: string; model: stricurrentProviderFn = fn;}+let statusSnapshotFn: (() => Record<string, unknown>) | null = null;++/** The runtime's live status (model, mode, permissions, tokens…) for the connected frame. */+export function setStatusSnapshotCallback(fn: () => Record<string, unknown>): void {+ statusSnapshotFn = fn;+}+type ChatWebSettings = {bypassPermissions: boolean;restrictUser: boolean;@@ -88,7 +96,10 @@ function saveSettings(settings: ChatWebSettings): void {export function setWebChannel(ch: WebChannel): void {webChannel = ch;const settings = loadSettings();- webChannel.setBypassPermissions(settings.bypassPermissions);+ // The owner's saved permission mode wins; the old per-dashboard flag is+ // only used until a mode has been chosen.+ const saved = loadPermissionMode();+ webChannel.setBypassPermissions(saved ? saved === 'allow-all' : settings.bypassPermissions);webChannel.setRestrictUser(settings.restrictUser);}@@ -115,7 +126,9 @@ chat.get('/api/chat/events', (c) => {// Send connected event with proper SSE named event formatconst providerInfo = currentProviderFn ? currentProviderFn() : { name: '', model: '' };- send(`event: connected\ndata: ${JSON.stringify({ id: clientId, provider: providerInfo.name, model: providerInfo.model })}\n\n`);+ let status: Record<string, unknown> | undefined;+ try { status = statusSnapshotFn?.(); } catch { status = undefined; }+ send(`event: connected\ndata: ${JSON.stringify({ id: clientId, provider: providerInfo.name, model: providerInfo.model, ...(status ? { status } : {}) })}\n\n`);const keepalive = setInterval(() => {send(`: keepalive\n\n`);@@ -177,7 +190,7 @@ chat.post('/api/chat/send', async (c) => {chat.get('/api/chat/settings', async (c) => {if (!webChannel) return c.json({ error: 'Web channel not initialized' }, 503);const stored = loadSettings();- return c.json({ ...webChannel.getSettings(), workspace: stored.workspace });+ return c.json({ ...webChannel.getSettings(), workspace: stored.workspace, permissionMode: loadPermissionMode() });});chat.put('/api/chat/settings', async (c) => {@@ -193,7 +206,8 @@ chat.put('/api/chat/settings', async (c) => {webChannel.setBypassPermissions(next.bypassPermissions);webChannel.setRestrictUser(next.restrictUser);saveSettings(next);- return c.json({ success: true, ...next });+ if (typeof body.bypassPermissions === 'boolean') savePermissionMode(body.bypassPermissions ? 'allow-all' : 'ask-me');+ return c.json({ success: true, ...next, permissionMode: loadPermissionMode() });});// ─── Permission Resolution ───────────────────────────────────diff --git a/src/web/server.ts b/src/web/server.tsindex 1fb09dbc..1602346c 100644--- a/src/web/server.ts+++ b/src/web/server.ts@@ -13,7 +13,7 @@ import providerRoutes from './api/providers.js';import configRoutes from './api/config.js';import systemRoutes, { setScheduler } from './api/system.js';import brainRoutes, { setUserMemory } from './api/brain.js';-import chatRoutes, { setWebChannel, setProgrammingMode, setModelSwitchCallback, setCurrentProviderCallback, setSessionRepository, setSessionSyncEnabledCallback } from './api/chat.js';+import chatRoutes, { setWebChannel, setProgrammingMode, setModelSwitchCallback, setCurrentProviderCallback, setStatusSnapshotCallback, setSessionRepository, setSessionSyncEnabledCallback } from './api/chat.js';import agentRoutes, { setAgentSupervisor, setBackgroundTaskManager } from './api/agents.js';import botsRoutes, { setBotManager, setBotsWebhookSecret } from './api/bots.js';import spotifyRoutes, { setSpotifyClient } from './api/spotify.js';@@ -200,7 +200,7 @@ if (spaAvailable) {}export { setShutdownHandler };-export { updateStatus, setUserMemory, setWebChannel, setScheduler, setAgentSupervisor, setBackgroundTaskManager, setSpotifyClient, setProgrammingMode, setModelSwitchCallback, setCurrentProviderCallback, setKanbanSupervisor, setKanbanBoardManager, setKanbanProviders, setIDEProviders, setBotManager, setBotsWebhookSecret };+export { updateStatus, setUserMemory, setWebChannel, setScheduler, setAgentSupervisor, setBackgroundTaskManager, setSpotifyClient, setProgrammingMode, setModelSwitchCallback, setCurrentProviderCallback, setStatusSnapshotCallback, setKanbanSupervisor, setKanbanBoardManager, setKanbanProviders, setIDEProviders, setBotManager, setBotsWebhookSecret };let webServer: ReturnType<typeof createAdaptorServer> | null = null;diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.tsindex efc1d4bb..233a66d7 100644--- a/ui/src/lib/api.ts+++ b/ui/src/lib/api.ts@@ -229,6 +229,38 @@ export const brain = {),},graph: () => get<GraphData>("/api/brain/graph"),+ // Owner Model (ADR-026): what Mercury believes about the owner, with evidence.+ beliefs: {+ list: (params?: { status?: string; kind?: string; dimension?: string; limit?: number }) => {+ const qs = new URLSearchParams();+ if (params?.status) qs.set("status", params.status);+ if (params?.kind) qs.set("kind", params.kind);+ if (params?.dimension) qs.set("dimension", params.dimension);+ if (params?.limit) qs.set("limit", String(params.limit));+ return get<{ beliefs: Belief[]; total: number; counts: BeliefCounts }>(`/api/brain/beliefs?${qs}`);+ },+ learned: (days?: number, limit?: number) => {+ const qs = new URLSearchParams();+ if (days) qs.set("days", String(days));+ if (limit) qs.set("limit", String(limit));+ return get<{ beliefs: Belief[]; total: number; days: number }>(`/api/brain/beliefs/learned?${qs}`);+ },+ contested: () =>+ get<{ pairs: Array<{ a: Belief; b: Belief | null }>; total: number }>("/api/brain/beliefs/contested"),+ get: (id: string) => get<Belief & { evidence: BeliefEvidence[] }>(`/api/brain/beliefs/${id}`),+ confirm: (id: string) => post<{ success: boolean; belief: Belief }>(`/api/brain/beliefs/${id}/confirm`),+ forget: (id: string) => post<{ success: boolean; forgotten: string }>(`/api/brain/beliefs/${id}/forget`),+ correct: (id: string, statement: string) =>+ post<{ success: boolean; belief: Belief; replaced: Belief }>(`/api/brain/beliefs/${id}/correct`, { statement }),+ settle: (winnerId: string, loserId: string) =>+ post<{ success: boolean; winner: Belief; loser: Belief }>("/api/brain/beliefs/settle", { winnerId, loserId }),+ },+ quarantine: {+ list: (limit?: number) =>+ get<{ memories: Memory[]; total: number }>(`/api/brain/quarantine${limit ? `?limit=${limit}` : ""}`),+ restore: (id: string) =>+ post<{ success: boolean; memory: Memory | null }>(`/api/brain/quarantine/${id}/restore`),+ },};// ── Agents / Tasks ──@@ -679,6 +711,37 @@ export interface MemoryCreate {durability?: string;}+export type BeliefStatus = "candidate" | "held" | "confirmed" | "contested" | "retired" | "forgotten";++export interface Belief {+ id: string;+ kind: string;+ dimension: string | null;+ level: string | null;+ statement: string;+ detail: string | null;+ context: string;+ support: number;+ against: number;+ status: BeliefStatus;+ ownerStated: boolean;+ confidence: number;+ lastSupportedAt: number;+ createdAt: number;+ updatedAt: number;+ evidence?: BeliefEvidence[];+}++export interface BeliefEvidence {+ id: string;+ kind: string;+ excerpt: string;+ weight: number;+ at: number;+}++export type BeliefCounts = Record<BeliefStatus, number>;+export interface Person {id: string;name: string;diff --git a/ui/src/pages/brain/Memory.tsx b/ui/src/pages/brain/Memory.tsxindex 04dae280..4ab57946 100644--- a/ui/src/pages/brain/Memory.tsx+++ b/ui/src/pages/brain/Memory.tsx@@ -15,6 +15,10 @@ import {Moon,Pin,Clock,+ Sparkles,+ ShieldAlert,+ Check,+ RotateCcw,} from "lucide-react";import { Card, CardContent } from "@/components/ui/card";import { Button } from "@/components/ui/button";@@ -48,7 +52,7 @@ import {AlertDialogTrigger,} from "@/components/ui/alert-dialog";import { cn, formatDate } from "@/lib/utils";-import api, { type Memory, type BrainStatus, type MemoryCreate } from "@/lib/api";+import api, { type Memory, type BrainStatus, type MemoryCreate, type Belief, type BeliefCounts } from "@/lib/api";/* ── Constants ─────────────────────────────────────────────── */@@ -656,6 +660,133 @@ function AddMemoryDialog({);}+/* ── Beliefs (Owner Model, ADR-026) ─────────────────────────── */++const STATUS_COLORS: Record<string, string> = {+ candidate: "bg-muted text-muted-foreground",+ held: "bg-[#00d4ff]/15 text-[#00d4ff]",+ confirmed: "bg-emerald-500/15 text-emerald-400",+ contested: "bg-amber-500/15 text-amber-400",+ retired: "bg-muted text-muted-foreground/60",+ forgotten: "bg-muted text-muted-foreground/60",+};++function BeliefItem({+ belief,+ index,+ onConfirm,+ onForget,+}: {+ belief: Belief;+ index: number;+ onConfirm: (id: string) => void;+ onForget: (id: string) => void;+}) {+ const excerpt = belief.evidence?.[0]?.excerpt;+ const canConfirm = belief.status !== "confirmed" && belief.status !== "retired";+ return (+ <motion.div custom={index} variants={fadeUp} initial="hidden" animate="visible" exit="exit" layout>+ <Card className="group transition-colors hover:border-[#00d4ff]/20">+ <CardContent className="p-4">+ <div className="flex items-start justify-between gap-3">+ <div className="flex-1 min-w-0 space-y-1.5">+ <div className="flex items-start gap-2.5">+ <span+ className={cn(+ "inline-flex shrink-0 items-center rounded-md px-2 py-0.5 text-xs font-semibold capitalize",+ STATUS_COLORS[belief.status] ?? "bg-muted text-muted-foreground"+ )}+ >+ {belief.status}+ </span>+ <p className="text-sm font-medium text-foreground leading-relaxed">+ {belief.statement}+ </p>+ </div>+ <div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground/70">+ <span className="capitalize">{belief.kind}{belief.dimension ? ` · ${belief.dimension}` : ""}</span>+ <span>{Math.round(belief.confidence * 100)}% conf</span>+ {belief.context !== "*" && <span>{belief.context}</span>}+ <span>{formatDate(belief.updatedAt)}</span>+ {excerpt && <span className="italic truncate max-w-[24rem]">from: “{excerpt}”</span>}+ </div>+ </div>+ <div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity shrink-0">+ {canConfirm && (+ <Button+ size="sm"+ variant="ghost"+ className="text-muted-foreground hover:text-emerald-400"+ title="Confirm: yes, that's right"+ onClick={() => onConfirm(belief.id)}+ >+ <Check className="h-3.5 w-3.5" />+ Confirm+ </Button>+ )}+ <Button+ size="sm"+ variant="ghost"+ className="text-muted-foreground hover:text-destructive"+ title="Forget this for good"+ onClick={() => onForget(belief.id)}+ >+ <Trash2 className="h-3.5 w-3.5" />+ Forget+ </Button>+ </div>+ </div>+ </CardContent>+ </Card>+ </motion.div>+ );+}++function QuarantinedItem({+ memory,+ index,+ onRestore,+}: {+ memory: Memory;+ index: number;+ onRestore: (id: string) => void;+}) {+ return (+ <motion.div custom={index} variants={fadeUp} initial="hidden" animate="visible" exit="exit" layout>+ <Card className="group border-dashed transition-colors hover:border-amber-400/30">+ <CardContent className="p-4">+ <div className="flex items-start justify-between gap-3">+ <div className="flex-1 min-w-0 space-y-1.5">+ <div className="flex items-start gap-2.5">+ <span+ className={cn(+ "inline-flex shrink-0 items-center rounded-md px-2 py-0.5 text-xs font-semibold capitalize",+ TYPE_COLORS[memory.type] ?? "bg-muted text-muted-foreground"+ )}+ >+ {memory.type}+ </span>+ <p className="text-sm text-muted-foreground leading-relaxed line-clamp-2">{memory.summary}</p>+ </div>+ <span className="text-xs text-muted-foreground/50">{formatDate(memory.createdAt)}</span>+ </div>+ <Button+ size="sm"+ variant="ghost"+ className="text-muted-foreground hover:text-amber-400 opacity-0 group-hover:opacity-100 transition-opacity shrink-0"+ title="Restore as a durable memory"+ onClick={() => onRestore(memory.id)}+ >+ <RotateCcw className="h-3.5 w-3.5" />+ Restore+ </Button>+ </div>+ </CardContent>+ </Card>+ </motion.div>+ );+}+/* ── Collapsible Section ────────────────────────────────────── */function MemorySection({@@ -732,6 +863,10 @@ export function MemoryPage() {const [editOpen, setEditOpen] = useState(false);const [saving, setSaving] = useState(false);const [toasts, setToasts] = useState<Toast[]>([]);+ // Owner Model (ADR-026): what Mercury learned lately, and the migration's quarantine.+ const [learned, setLearned] = useState<Belief[]>([]);+ const [beliefCounts, setBeliefCounts] = useState<BeliefCounts | null>(null);+ const [quarantined, setQuarantined] = useState<Memory[]>([]);const debounceRef = useRef<ReturnType<typeof setTimeout>>();@@ -835,10 +970,59 @@ export function MemoryPage() {[query, activeTypes]);+ /* Beliefs + quarantine (non-critical: the page works without them) */+ const fetchBeliefs = useCallback(async () => {+ try {+ const [l, all, q] = await Promise.all([+ api.brain.beliefs.learned(30, 50),+ api.brain.beliefs.list({ limit: 1 }),+ api.brain.quarantine.list(200),+ ]);+ setLearned(l.beliefs);+ setBeliefCounts(all.counts);+ setQuarantined(q.memories);+ } catch {+ // non-critical+ }+ }, []);++ const handleConfirmBelief = async (id: string) => {+ try {+ const { belief } = await api.brain.beliefs.confirm(id);+ setLearned((prev) => prev.map((b) => (b.id === id ? { ...b, ...belief, evidence: b.evidence } : b)));+ toast("success", "Confirmed");+ } catch (err: unknown) {+ toast("error", err instanceof Error ? err.message : "Failed to confirm");+ }+ };++ const handleForgetBelief = async (id: string) => {+ try {+ await api.brain.beliefs.forget(id);+ setLearned((prev) => prev.filter((b) => b.id !== id));+ toast("success", "Forgotten");+ } catch (err: unknown) {+ toast("error", err instanceof Error ? err.message : "Failed to forget");+ }+ };++ const handleRestoreQuarantined = async (id: string) => {+ try {+ await api.brain.quarantine.restore(id);+ setQuarantined((prev) => prev.filter((m) => m.id !== id));+ toast("success", "Restored as a durable memory");+ fetchStatus();+ fetchMemories("conscious", 0, false);+ } catch (err: unknown) {+ toast("error", err instanceof Error ? err.message : "Failed to restore");+ }+ };+/* Initial load */useEffect(() => {fetchStatus();- }, [fetchStatus]);+ fetchBeliefs();+ }, [fetchStatus, fetchBeliefs]);/* Debounced search + filter */useEffect(() => {@@ -1039,6 +1223,50 @@ export function MemoryPage() {{/* Memory sections */}{!loading && !error && (<div className="space-y-6">+ {/* What Mercury learned (Owner Model, ADR-026) */}+ <MemorySection+ title="What Mercury learned"+ icon={<Sparkles className="h-4 w-4 text-emerald-400" />}+ count={learned.length}+ defaultOpen={learned.length > 0}+ actions={+ beliefCounts && (+ <span className="text-xs text-muted-foreground/70 tabular-nums">+ {beliefCounts.held} held · {beliefCounts.confirmed} confirmed · {beliefCounts.candidate} candidate · {beliefCounts.contested} contested · {beliefCounts.retired} retired+ </span>+ )+ }+ >+ {learned.length > 0 ? (+ <div className="space-y-3">+ <p className="text-xs text-muted-foreground/70 px-1">+ Beliefs learned or changed in the last 30 days. Confirm what is right; forget what is not.+ </p>+ <motion.div className="space-y-3" variants={stagger} initial="hidden" animate="visible">+ <AnimatePresence mode="popLayout">+ {learned.map((belief, i) => (+ <BeliefItem+ key={belief.id}+ belief={belief}+ index={i}+ onConfirm={handleConfirmBelief}+ onForget={handleForgetBelief}+ />+ ))}+ </AnimatePresence>+ </motion.div>+ </div>+ ) : (+ <Card>+ <CardContent className="flex flex-col items-center justify-center py-8 text-center">+ <p className="text-sm text-muted-foreground">+ Nothing learned in the last 30 days. Beliefs form as you work with Mercury.+ </p>+ </CardContent>+ </Card>+ )}+ </MemorySection>+{/* Conscious Section */}<MemorySectiontitle="Conscious"@@ -1172,6 +1400,35 @@ export function MemoryPage() {</Card>)}</MemorySection>++ {/* Quarantined (legacy rows the belief migration set aside) */}+ <MemorySection+ title="Quarantined"+ icon={<ShieldAlert className="h-4 w-4 text-amber-400" />}+ count={quarantined.length}+ defaultOpen={false}+ >+ {quarantined.length > 0 ? (+ <div className="space-y-3">+ <p className="text-xs text-muted-foreground/70 px-1">+ Old rows that looked like Mercury's own replies or prose fragments rather than facts about you. Hidden from prompts, never deleted. Restore any that are real.+ </p>+ <motion.div className="space-y-3" variants={stagger} initial="hidden" animate="visible">+ <AnimatePresence mode="popLayout">+ {quarantined.map((memory, i) => (+ <QuarantinedItem key={memory.id} memory={memory} index={i} onRestore={handleRestoreQuarantined} />+ ))}+ </AnimatePresence>+ </motion.div>+ </div>+ ) : (+ <Card>+ <CardContent className="flex flex-col items-center justify-center py-8 text-center">+ <p className="text-sm text-muted-foreground">Nothing is quarantined.</p>+ </CardContent>+ </Card>+ )}+ </MemorySection></div>)}diff --git a/website/docs/reference/second-brain.mdx b/website/docs/reference/second-brain.mdxindex 9bf2c477..753442ba 100644--- a/website/docs/reference/second-brain.mdx+++ b/website/docs/reference/second-brain.mdx@@ -3,14 +3,38 @@ id: second-braintitle: Second Brain----Mercury has a persistent, structured memory that grows with every conversation. When enabled, it automatically extracts, stores, and retrieves facts about you — your preferences, goals, projects, habits, the people in your life, and the relationships between them.+Mercury has a persistent, structured memory that grows with every conversation. When enabled, it learns what is true about you — and how you like to work — as beliefs with evidence, retrieves what is relevant to each message, and tells you when it learned something that changes how it behaves.++## The Owner Model: beliefs, not just facts++Since 1.4.1 Mercury holds **beliefs** about you rather than a pile of facts. A belief is a claim ("prefers one command over many manual steps") with evidence for and against it, a confidence that fades unless it is seen again, the context it applies in (everywhere, one project, one channel, one domain), and a status: **candidate** (seen once) → **held** (seen in two sessions) → **confirmed** (you said so). Two beliefs that cannot both be true are **contested** and Mercury asks you once. What you state outright never fades; what Mercury inferred does, unless it keeps being true.++What it learns are *working traits* that change how Mercury behaves: reply length, when to ask before acting (per project or domain), options vs a recommendation, tone, your expertise and risk appetite per domain, when you are usually around, and how you give feedback. It never infers personality types, health, religion, politics or sexuality, never stores passwords or keys, and never generalises about other people beyond what you said.++**How it learns.** After each turn, a small model call turns the turn into belief operations — or nothing, which is the usual outcome. Mercury's own replies are never evidence about you. Every hour of activity (and at shutdown) a reflection pass reads your messages across sessions, infers traits, merges duplicates, flags contradictions and rewrites the owner brief. The brief — *About you* and *How to work with you* — is in every prompt, together with the beliefs relevant to the current message.++**What you see.** When something behaviour-changing is learned, the reply ends with one line: `☿ Noted: you'd rather I just run things here without asking. Say "forget that" to undo.` From any chat:++| Command | What it does |+|---|---|+| `/memory learned [days]` | What Mercury learned recently, numbered |+| `/memory beliefs` | Traits by dimension, then confirmed preferences |+| `/memory contested` | Pairs Mercury can't settle; `/memory settle <n> a\|b` |+| `/memory confirm <n>` · `/memory forget <n>` · `/memory correct <n> <what is true>` | Act on a numbered item |+| `/forget <text>` · "forget that" | Forget the best match, or the last `☿ Noted` line |++`~/.mercury/soul/owner.md` holds the same brief in plain text; anything you write under **My notes** is read back as something you stated. The dashboard's Memory page shows what was learned with Confirm / Forget, and a **Quarantined** list: rows the old extractor saved by mistake (Mercury's own replies stored as your preferences), hidden but restorable.## How it works-1. **Extraction** — After each conversation, Mercury pulls 0–3 facts and stores them with type, confidence, importance, and durability scores.-2. **Retrieval** — Before each message, the top 5 relevant memories (within a 900-char budget) are injected into context. Mercury references them naturally.-3. **Consolidation** — Every 60 minutes, Mercury builds a profile summary, an active-state summary, and generates `reflection` memories from detected patterns.-4. **Pruning** — Active-scope memories stale after 21 days. Inferred memories decay. Low-confidence durable memories are dismissed after 120 days. Memories reinforced 3+ times are promoted to durable.+1. **Extraction** — After each turn, a strict extractor turns what you said and did into belief operations: create, support, contradict, or nothing at all. Mercury's own replies are never evidence about you.+2. **Retrieval** — Before each message, up to 5 beliefs relevant to it (with their confidence and context) go into the prompt, and the "About you" / "How to work with you" brief sits in every prompt.+3. **Reflection** — After every hour of activity and at shutdown, one model pass reads your messages across sessions: it infers working traits, merges duplicates, flags contradictions, rewrites the brief, and lets faded beliefs retire.+4. **Decay** — Confidence fades per kind (a fact over a year, a preference over months, a trait over weeks) unless the belief is seen again; what you stated outright never fades. A belief retires only when it faded *and* was contradicted or went stale for two half-lives.++**One source.** Every view of your memory — `/memory`, the dashboard, the `search_memory` and `save_memory` tools, the bots, cloud sync — reads and writes the same belief store. Who you are (your name, email, handles) lives in `identity.owner` in `mercury.yaml`, the one place it is set; Mercury never stores it as a belief.++**Upgrading from an older Mercury.** The first boot after the upgrade copies `second-brain.db` to `second-brain.pre-owner-model.db` next to it, then moves your memories into beliefs: real ones become candidates (or held, when they had been seen more than once); rows that were Mercury's own replies, credentials or templated filler are quarantined, never deleted, and wait in the dashboard under *Quarantined* with a Restore button. Nothing you could see before disappears, `/memory` and the dashboard keep working, and a reflection pass two minutes after that boot judges the migrated candidates against your real sessions.## Memory Tiers@@ -124,6 +148,18 @@ GET /api/brain/graphGoals and projects are not a separate resource — they are memories filtered by `type=goal` or `type=project` against `/api/brain/memory`.+## Which model learns++Extraction and reflection are strict-JSON jobs; a weak free-tier model misses most of what you say or returns nothing. Point them at a capable provider without changing your chat model:++```yaml+memory:+ learning:+ provider: anthropic # any name from `providers`; falls back to the default when unusable+```++(`LEARNING_PROVIDER=anthropic` does the same.) A Haiku-class model is plenty; each turn costs one small call and each hour of activity one reflection.+## Enable / DisableSecond Brain is **enabled by default**. To disable it:diff --git a/website/docs/reference/skills.mdx b/website/docs/reference/skills.mdxindex df82b4e0..7f148572 100644--- a/website/docs/reference/skills.mdx+++ b/website/docs/reference/skills.mdx@@ -200,6 +200,31 @@ POST /api/skills/:name/deactivateDELETE /api/skills/:name```+## Self-made skills++Mercury can write skills for itself. After a multi-step task it would do again — a recurring report, a conversion pipeline, a deploy, a site or device it operates — has worked end-to-end, it saves the procedure (exact commands, paths, checks, pitfalls) with `create_skill` and tells you in one line:++```+☿ Skill saved: weekly-sales-report — say /skills self-made to review or remove it.+```++Next time, the task is one `use_skill` away. These skills are deliberately kept apart from the ones you install:++- They live in `~/.mercury/skills/self-made/<name>/SKILL.md` with `category: self-made`, `origin: mercury`, `made`, `verified`, `uses` and `last_used` in the frontmatter.+- They are listed under their own **Self-made** label and count as *unverified* until used again; Mercury is told to trust the result, not the text, and to update the skill when a step fails.+- Mercury never saves one for a one-off, for anything a single tool does, for anything it did not verify, or with credentials inside; a self-made skill never shadows an installed one with the same name.+- Unused ones go **dormant** after 30 days (disabled, never deleted) and the shelf is capped at 40 live skills.+- Only your own sessions can create them — not bots, not channel members.++```+/skills self-made # list, with use counts and dormancy+/skills self-made enable <name>+/skills self-made disable <name>+/skills self-made remove <name>+```++To keep one for good, move its folder out of `self-made/` and drop the `origin`/`uses` fields — it becomes an ordinary skill.+## Writing your ownCopy the seeded template: