From second-brain
Run a "dream" — background consolidation of the knowledge base. Mines session transcripts for missed insights, deduplicates wiki entries, builds cross-links, and stages all changes for review before applying. Equivalent to Anthropic's managed-agents Dreams API, running locally. Supports inline (default) and background (--background) execution modes.
How this skill is triggered — by the user, by Claude, or both
Slash command
/second-brain:dream [--background] [instructions text][--background] [instructions text]This skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
Fully automated. Detect state and act — no prompts, no menus.
Fully automated. Detect state and act — no prompts, no menus.
On invocation, check current state via dream_list and act accordingly:
--background flag present: set BACKGROUND=truedream_create with:
instructions: from args or emptytranscript_filter: default (all transcripts, max 50)run_in_background: true, passing the dream_id. Report that dream is running in background and user will be notified on completion. Stop here.Set dream status to running via:
TMPFILE=$(mktemp)
jq '.status = "running" | .started_at = "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"' \
~/.second-brain/dreams/{dream_id}/status.json > "$TMPFILE" && mv "$TMPFILE" ~/.second-brain/dreams/{dream_id}/status.json
Read all transcript files from ~/.second-brain/dreams/{dream_id}/transcripts/.
For each transcript, scan the metadata header (session_id, slug, date, tool_count) first. Then read the full content.
Identify:
Collect findings as a structured list of proposed wiki actions:
create — new page for a previously uncaptured entity/concept/learningupdate — add content to an existing pagerelate — add cross-links between existing pagesWork exclusively on the staging wiki at ~/.second-brain/dreams/{dream_id}/staging/wiki/.
2a. AUDIT — Read all pages. Fix:
[[wiki-links]] → fix or create stubcreated: fieldNode-shape note (0.24.48): you do NOT need to hand-canonicalize frontmatter FORMAT (β
related:/tags:, missing required fields).dream_acceptruns the maintainer's exact shaper (knowledge_validateautofix, viasb_validate_wiki) over the staging dir BEFORE merging onto live, and the post-merge reindex re-projects relations. Focus your AUDIT on CONTENT (dead links, dupes, empties) — the deterministic format pass is automatic and identical to the maintainer's, so dream and maintainer output converge to the same canonical shape.
2b. DEDUPLICATE — Find pages with overlapping titles/content:
[[wiki-links]] and related: references to point to surviving page## History entry noting the merge2c. RELATE — relationships live in the bi-temporal edge log (~/knowledge/graph/edges.jsonl)
and are projected onto related: + the ## Dependencies block at reindex. The dream does
NOT curate edges — graph/edges.jsonl is not snapshotted into staging, so do not hand-edit
related: or the <!-- graph:begin --> block here (it is overwritten at projection).
knowledge_relate calls for the user / knowledge-maintainer to apply live.~/knowledge/graph/conflicts.jsonl (live, read-only) and echo the folded
open-conflict count into the dream report ("N open graph conflicts — resolve via the
maintainer"). Write nothing to graph/ — the knowledge-maintainer owns the drain.2d. ENRICH — Category-specific BODY quality. Do NOT hand-edit related: — it is
projected from the edge log; surface relationship gaps as suggested knowledge_relate calls
in the report (per 2c), not frontmatter edits (the next reindex overwrites them).
SB_DREAM_AI_BLOCKS=off — this step only COUNTS blockless pages and reports; off ⇒ omit the count. No wiki write to gate, so advisory, not machine-enforced.) — scan staging for structured
pages (learnings/decisions/entities/issues/concepts/security) lacking an <!-- ai:begin -->
block and count them. Do NOT author blocks in staging — block authoring stays a single
path through the live knowledge-maintainer (it grounds the block in the page's current
prose; the dream would re-derive from prose it is still rewriting). Surface the count in the
dream report: "N structured pages have no ai-block — run /second-brain:maintain to backfill."2e. SUMMARIZE — whole-corpus theme pages (skip if SB_DREAM_SUMMARIZE=off — machine-enforced:
graph-cluster.sh returns [] when off, so the per-cluster write loop below has zero iterations
and no theme page is authored). Cluster the staging wiki's link graph and write one summary page
per cluster, so a fresh session can be handed themes, not just nearest slugs. Clustering is
deterministic and staging-local (reads related: + body [[links]], never the live edge log):
CLUST=$(bash "$CLAUDE_PLUGIN_ROOT/scripts/graph-cluster.sh" \
--knowledge-dir "$HOME/.second-brain/dreams/{dream_id}/staging")
CLUST is JSON [{id, members, member_hash}] for clusters ≥ SB_SUMMARIZE_MIN_CLUSTER
(default 4), capped at SB_SUMMARIZE_MAX_PAGES (default 8). Theme pages are slugged
theme-<id> — the cluster id is the smallest member slug, which is usually an EXISTING
page (the cluster's anchor), so the theme- prefix prevents a duplicate_slug collision with it.
For each cluster:
staging/wiki/themes/theme-<id>.md exists with a matching member_hash, skip it
(membership and member content unchanged — no LLM call).staging/wiki/themes/theme-<id>.md with frontmatter type: themes,
generated: true, related: [[member]]… (the member slugs), member_hash: <hash>,
created/updated, and an LLM summary INSIDE the markers
<!-- theme:begin (generated — do not hand-edit) --> … <!-- theme:end --> describing
what the cluster is about and how its members relate. Author only the marked region.Theme pages are derived/regenerable (FORGET protects the themes category) and are staged
like any other page — reviewed at dream_accept; the embedding cache + live index populate
on accept, not at dream time.
Project MOCs (since 0.23.0) are the deterministic sibling: reindex projects one
wiki/projects/<key>.md per project: facet (≥3 members) into the two-tier index. The dream
does NOT assign project: on the live path (that is the knowledge-maintainer's job) — if
mining surfaces a clear ungrouped project, surface it as a suggestion in the report
("group X, Y, Z under project <key>"), the same surface-only pattern as relationships.
projects/ + themes/ are excluded from clustering input so MOCs never become hubs.
2f. REINDEX — Call knowledge_reindex MCP tool (pointed at staging dir is not possible via MCP, so manually update staging/wiki/index.md if needed). Run AFTER SUMMARIZE so new theme + project MOC pages are catalogued in the two-tier index.
2g. FORGET — bound cold-tier wiki growth (skip entirely if SB_WIKI_FORGET=off).
Scores the LIVE wiki read-only (it copies to a temp to probe; never mutates live —
and real page ages matter, since staging mtimes are all fresh from the dream snapshot),
selecting low-value, old, unlinked, recall-safe pages, and writes a manifest. Archiving
happens only on accept (Review phase) — the FORGET phase writes nothing to the wiki.
MAN=~/.second-brain/dreams/{dream_id}/forget-manifest.tsv
if [ "${SB_WIKI_FORGET:-on}" != "off" ]; then
CAND=$(bash "$CLAUDE_PLUGIN_ROOT/scripts/wiki-forget-candidates.sh"); rc=$?
if [ "$rc" -eq 2 ]; then
echo "FORGET: recall guard unavailable — skipping forgetting this dream (fail-safe)."
else
: > "$MAN"
while IFS=$'\t' read -r slug path; do
[ -n "$slug" ] || continue
printf '%s\t%s\n' "$slug" "$(basename "$(dirname "$path")")" >> "$MAN"
done <<< "$CAND"
n=$(grep -c . "$MAN" 2>/dev/null || echo 0)
echo "FORGET: staged $n page(s) for archive."
fi
fi
The selector scores offline signals (access/recency/connectivity/category — no
embeddings), takes score < SB_FORGET_FLOOR (default 0.15), unprotected, capped at
SB_FORGET_MAX_PER_DREAM (default 5), and drops any page its live recall-probe shows is
the UNIQUE answer to its topic. If $n > 0, add a ## Proposed archives (N) section to
diff.md listing the staged slugs so they're reviewed alongside the consolidation.
Generate the diff:
bash "$CLAUDE_PLUGIN_ROOT/scripts/dream-diff.sh" {dream_id}
Update status to completed:
TMPFILE=$(mktemp)
jq '.status = "completed" | .ended_at = "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"' \
~/.second-brain/dreams/{dream_id}/status.json > "$TMPFILE" && mv "$TMPFILE" ~/.second-brain/dreams/{dream_id}/status.json
Proceed to Review phase.
~/.second-brain/dreams/{dream_id}/diff.mddream_accept to apply the consolidation to the live wiki.~/.second-brain/dreams/{dream_id}/forget-manifest.tsv exists,
archive each listed LIVE page (reversible move, never delete — the user's accept
IS the confirmation). The manifest was built BEFORE consolidation, so re-validate
each slug against the POST-accept wiki and skip any page the dream just enriched
(now linked / higher-scoring — the enrichment race):
MAN=~/.second-brain/dreams/{dream_id}/forget-manifest.tsv
ARC=~/.second-brain/wiki-archive; LOG=~/.second-brain/wiki-archive-log.jsonl
KD="${CLAUDE_PLUGIN_OPTION_KNOWLEDGE_DIR:-$HOME/knowledge}"; KD="${KD/#\~/$HOME}"
mkdir -p "$ARC"
# still-forgettable in the post-consolidation live wiki (re-score guard)
STILL=$(bash "$CLAUDE_PLUGIN_ROOT/scripts/wiki-forget-score.sh" \
| awk -F'\t' -v fl="${SB_FORGET_FLOOR:-0.15}" '($1+0)<fl && $5==""{print $2}')
while IFS=$'\t' read -r slug cat; do
[ -n "$slug" ] || continue
printf '%s\n' "$STILL" | grep -qxF "$slug" \
|| { echo "FORGET: keeping '$slug' — no longer low-value after consolidation"; continue; }
src="$KD/wiki/$cat/$slug.md"; [ -f "$src" ] || continue
mkdir -p "$ARC/$cat"; mv "$src" "$ARC/$cat/$slug.md"
printf '{"event":"archived","slug":"%s","category":"%s","date":"%s"}\n' \
"$slug" "$cat" "$(date -u +%FT%TZ)" >> "$LOG"
done < "$MAN"
rm -f "$MAN"
Then call knowledge_reindex so archived pages leave the search index.bash "$CLAUDE_PLUGIN_ROOT/scripts/wiki-restore.sh" <slug>).If the diff shows 0 total changes AND no archive manifest, call dream_discard instead
and report that the wiki was already well-consolidated. On any discard, also
rm -f ~/.second-brain/dreams/{dream_id}/forget-manifest.tsv (archive nothing).
[[lowercase-kebab-case]]Guides creation, editing, and verification of skills for AI coding agents using test-driven development with subagent scenarios. Use when authoring or debugging skills.
npx claudepluginhub cain-ish/claude-code-plugin --plugin cost-router