Self-evolving memory and knowledge accumulation system for AI agents. Acts as a persistent 'second brain' that automatically retrieves past experiences, capt...
Confirm: "mem-skill initialized with QMD memory engine. Collections created and embeddings generated."
IMPORTANT: Never silently create QMD collections without confirming scope and names with the user. If no --qmd-* flags were provided, every question above MUST be asked interactively.
For the default engine (no --mem-engine flag), create .mem-skill.config.json with:
When the user runs /mem-skill upgrade, migrate an existing mem-skill workspace to the latest version. This is safe to run multiple times — it only adds what's missing.
Procedure:
Check prerequisites:
Verify knowledge-base/_index.json and experience/_index.json exist. If not, tell the user to run /mem-skill init instead.
Read .mem-skill.config.json to determine current engine and version.
Create log.md if it doesn't exist:
```markdown
# mem-skill Activity Log
Chronological record of all mem-skill operations. Each entry is parseable with grep "^## \[" log.md.
## [YYYY-MM-DD] upgrade | Migrated from v to v1.2.0
```
If log.md already exists, just append the upgrade log entry.
Backfill Source: field on existing entries:
Scan all .md files in knowledge-base/ and experience/.
For each entry (identified by ## [Title]) that is missing Source:, insert Source: conversation after the Date: line.
Report: "Backfilled Source field on entries."
Backfill Related: field on existing entries:
For each entry missing Related:, insert an empty Related: line before Keywords:.
These will be populated naturally by future writes and by running /mem-skill lint.
Report: "Added Related field placeholder to entries."
Update .mem-skill.config.json version to 1.2.0.
QMD engine post-upgrade (if engine is qmd):
Run qmd update && qmd embed to re-index the backfilled entries.
Confirm:
> "Upgrade complete (v → v1.2.0):
> - Created log.md ✓ (or: log.md already existed)
> - Backfilled Source on entries ✓
> - Added Related placeholder to entries ✓
> - Updated config version ✓
>
> Run /mem-skill lint to discover cross-reference opportunities between your existing entries."
Manual Recording Command
When the user runs /mem-skill recordnow, immediately trigger the recording flow for the current conversation — even if Step 5 was not triggered automatically.
This is useful when:
Multiple tasks were completed in one session and the agent forgot to ask.
The user wants to record something that didn't trigger the satisfaction keywords.
The user remembers later that a solution was worth saving.
Procedure:
Review the full conversation history for completed tasks.
For each completed task, summarize it into a one-line essence.
Evaluate: "Will this save time next time?"
Present all recordable items to the user as a numbered list:
> "I found these completed tasks worth recording:
> 1. [summary of task 1] → knowledge-base
> 2. [summary of task 2] → knowledge-base
> 3. [summary of skill usage] → experience
>
> Which ones should I record? (all / 1,2 / none)"
On approval, write each selected item following the same write procedure as Step 5 (including compounding checks, cross-references, source fields, and QMD post-write sync if applicable).
Append to log.md: ## [YYYY-MM-DD] recordnow | Manually recorded entries
If no recordable tasks are found, respond: "I reviewed the conversation but didn't find any completed tasks worth recording. Is there something specific you'd like me to save?"
Health-Check Command
When the user runs /mem-skill lint, perform a comprehensive health-check of the knowledge base and experience store.
Checks performed:
Duplicate detection — Scan all entries for similar titles, overlapping keywords (>= 70% overlap), or near-identical content. Flag entries that should be merged.
Stale entries — Flag entries older than 6 months (based on Date: field) that have no Updated: timestamp. These may contain outdated practices.
Contradiction detection — Within the same category, look for entries that give conflicting advice on the same topic. Flag with both entries quoted.
Orphan detection:
Categories in _index.json with no corresponding .md file.
.md files in knowledge-base/ or experience/ with no _index.json entry.
Broken Related: links pointing to non-existent entries.
Missing cross-references — Entries in the same category or with overlapping keywords that have no Related: links between them.
Knowledge gaps — Categories with fewer than 2 entries. Suggest whether to merge into a broader category or to actively build out.
Index consistency — Verify totalEntries and per-category count values match the actual entry count in each .md file.
Output format:
mem-skill Health Check — YYYY-MM-DD
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✓ 24 entries across 5 categories
✓ 8 experience entries across 3 skills
⚠ STALE (2):
- knowledge-base/frontend-dev.md#"React Class Components" (2025-08-12, 8 months old)
- experience/skill-webpack.md#"Loader Order Fix" (2025-06-03, 10 months old)
⚠ DUPLICATE (1 pair):
- knowledge-base/workflow.md#"CI Pipeline Setup" ↔ knowledge-base/backend-dev.md#"CI/CD Best Practices"
→ Keywords overlap 75%. Consider merging.
⚠ MISSING CROSS-REFS (3):
- knowledge-base/frontend-dev.md#"Responsive Grid" ↔ knowledge-base/design-layout.md#"CSS Grid Patterns"
- ...
✓ No contradictions detected
✓ No orphans detected
✓ Index counts consistent
Summary: 6 issues found (0 critical, 6 warnings)
After lint:
Ask the user which issues to fix.
For merges: combine entries, update cross-references, update index counts.
For stale entries: ask whether to update, archive, or delete.
For missing cross-references: add Related: links.
Append to log.md: ## [YYYY-MM-DD] lint | Health-check completed: issues found
QMD engine: After any fixes that modify .md files, run qmd update && qmd embed.
Ingest Command
When the user runs /mem-skill ingest , process an external source into knowledge base entries.
QMD engine post-write: Run qmd update && qmd embed.
IMPORTANT: Ingest does NOT blindly copy source content. It extracts actionable knowledge that fits the recording criteria, re-formats it into the standard entry format, and integrates it with existing knowledge.
Core Loop (Mandatory Every Turn)
Execute these steps on every conversation turn. Do not display internal cache state to the user.
Step 0: In-Conversation Cache (Internal)
Maintain these variables silently within the conversation:
last_keywords — keywords from the previous turn
last_topic_fingerprint — top 3 keywords as a fingerprint
last_index_lastUpdated — timestamp of last index read
last_matched_categories — categories matched on last read
last_used_skills — non-mem-skill skills used this turn
missing_experience_skills — skills with no experience entry
loaded_experience_skills — skills whose experience has been loaded this session
Step 1: Extract Keywords (No File I/O)
Extract 3–8 core nouns/phrases from the user's current message.
Deduplicate and normalize casing.
Generate topic_fingerprint from the top 3 keywords.
Step 2: Detect Topic Switch (No File I/O)
A topic switch occurs when any of these conditions are met:
If the skill-id is already in loaded_experience_skills, skip (do not re-read or re-announce).
Otherwise:
Read experience/_index.json.
If a matching skill-id entry exists, load experience/skill-.md.
Add the skill-id to loaded_experience_skills.
Include in response: "Loaded experience: skill-.md"
Log (first read per session only): Append to log.md: ## [YYYY-MM-DD] read | Retrieved experience: skill-.md
If no entry exists, add to missing_experience_skills.
Engine-specific retrieval:
Default engine: Read experience/_index.json and match by skillId.
QMD engine: Read collection names from .mem-skill.config.json, then run qmd search "" -c --json -n 5 for keyword match, or qmd query "" -c --json -n 5 for deeper retrieval.
Step 4: Knowledge Base Read (Only on Topic Switch)
Execute only on the first turn of the conversation or when a topic switch is detected:
Default engine:
Read knowledge-base/_index.json.
Match current keywords against all category keywords arrays.
Load every matched category file (no priority ranking — load all matches).
If no category matches, follow the "Dynamic Category" flow (see below).
If any files were loaded, include in response: "Loaded knowledge: .md, .md"
Log (first read per session only): Append to log.md: ## [YYYY-MM-DD] read | Loaded knowledge: .md, .md
If no topic switch occurred, reuse last_matched_categories without re-reading.
QMD engine:
Read collection names from .mem-skill.config.json.
Run qmd query "" -c --json -n 10 --min-score 0.3.
Load top results as context.
Include in response: "Retrieved knowledge via QMD: "
Step 5: Proactive Recording (Most Important)
Trigger conditions:
The current task is clearly completed at high quality.
The user expresses satisfaction ("great", "perfect", "that works", etc.).
The agent synthesizes a particularly valuable answer (comparison, deep analysis, connection discovery) that would be useful beyond this conversation.
Recording procedure:
Summarize: Distill the solution into a one-line essence.
Evaluate value: "Will this save time next time?"
Check for existing related entries (compounding update):
Search knowledge-base/_index.json and relevant .md files for entries with overlapping keywords or similar topics.
QMD engine: Run qmd query "" -c --json -n 3 --min-score 0.5 to find related entries.
If a closely related entry exists, offer to update/merge instead of creating a duplicate:
> "I found an existing entry '[[#]]' that's related. Should I update it with the new information, or create a separate entry?"
Update = append new points to the existing entry, add Updated: YYYY-MM-DD below the original date, and add cross-references.
Separate = create a new entry with Related: links to the existing one (and update the existing entry's Related: too).
Ask permission: Always say something like:
> "We solved [problem description]. I'd like to record this experience so I can reference it next time. Is that okay?"
Write on approval:
Skill experience (if a non-mem-skill skill was used and the skill has no entry or has new techniques): Write to experience/skill-.md and update experience/_index.json.
General knowledge (if it's a reusable workflow, preference, or solution): Write to knowledge-base/.md and update knowledge-base/_index.json.
Include Source: field (default: conversation).
Include Related: cross-references to any related entries found in step 3.
Log the operation: Append to log.md:
For new entries: ## [YYYY-MM-DD] write | Recorded to : ""
For updates: ## [YYYY-MM-DD] update | Updated : "" (merged new info)
Filing valuable answers back:
If the agent produces a valuable synthesis (comparison table, analysis, architectural decision) during a query — not just task completion — proactively offer:
> "This [comparison/analysis/summary] seems valuable beyond this conversation. Want me to save it to the knowledge base?"
This ensures knowledge is captured from exploration and research, not just from building things.
QMD engine post-write:
After writing any .md file, run:
qmd update
qmd embed
Forced rule — always ask when experience is missing:
If a non-mem-skill skill was used this turn and that skill has no entry in experience/_index.json, you must ask at task completion:
> "We used this time, but there's no experience record yet. Can I record this session's approach for future reference?"
If Step 5 was not triggered (e.g., multi-task sessions where satisfaction signals were missed), the user can run /mem-skill recordnow at any time to manually trigger recording. See "Manual Recording Command" above.