agentleFS
jame581jame581/LogseqBrain/skills/brain-doctor/SKILL.md

brain-doctor

Skill18 starsChanged 2 months agoMITView on GitHub

Lint and repair the Claude Brain Logseq graph — find and fix format violations that create phantom pages or broken macros. Triggers: "brain doctor", "lint brain", "check brain health", "clean up brain", "fix brain", "graph hygiene", "why are there empty pages", "find broken pages", "backfill digests", "rebuild digest for <project>". Don't fire for loads (use brain-load), saves (use brain-save), status/analytics (use brain-status), or first-time setup (use brain-init).

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Put text arguments in single quotes.

    line 23
  2. Very specific:

    Run brain info once, and use its graph: path for the backup and for every Edit this skill makes.

    line 28
  3. Distinctive:

    If the trigger is ambiguous, run Report first and offer to fix.

    line 35
  4. Distinctive:

    Confirm before any write: Ask which classes to fix if the user hasn't said.

    line 56

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Brain Doctor

Scan the whole graph for the format mistakes that silently corrupt a Logseq graph. Report them and, on confirmation and after a backup, repair them. This is a maintenance tool: run it on demand, not in the save/load cycle. Detection runs through the helper (below). The catalog of meaning, tiers and remediation is skills/_shared/hygiene-rules.md.

Running the helper

  • Bash: sh "<this skill's base directory>/../_shared/bin/brain" <command> …
  • PowerShell-only Windows: & "$(Split-Path (Split-Path (Get-Command git).Source))\bin\bash.exe" "<base>/../_shared/bin/brain" <command> …
  • Neither works: stop and say "logseq-brain needs Git for Windows (Git Bash) — https://git-scm.com/download/win".
  • In Cowork, always pass --graph <connected folder>. Elsewhere pass --graph only when the graph is not configured. It may go anywhere on the line. On exit 2 with graph not resolved, follow skills/_shared/path-resolution.md, then pass --graph.
  • Put text arguments in single quotes; a ' inside becomes '\''. In double quotes the shell runs backticks and expands $.
  • Exit 0 clean, 1 findings (act on them), 2 error (act on its reason; don't retry blindly). Quote every coverage: line. Never recompute a figure it printed. Its graph: line is the folder for your own Read, Edit and Write calls.

Prerequisites

Run brain info once, and use its graph: path for the backup and for every Edit this skill makes. If it exits 2, follow skills/_shared/path-resolution.md, then pass --graph to every call.

Modes

  • Report (default: "lint brain" / "check brain health" / "why are there empty pages"): detect and summarize, change nothing.
  • Fix ("fix brain" / "clean up brain" / "yes, fix them" after a report): repair after a backup, with confirmation.

If the trigger is ambiguous, run Report first and offer to fix.

Process

  1. Scan in one call: brain lint --all. It prints every mechanical rule, plus the digest rules for every digest-bearing page, as file:line rule tier detail, followed by a summary: line. Then run the three judgment rules the helper does not: description-link (from the phantom-target procedure under broken-link), duplicate-entry and structural-integrity, per the catalog.
  2. Report, grouped by the catalog's auto-fixable field:

    • Auto-fixable: code-in-braces, bare-hash-tag, unnamespaced-link, file-link, relative-link, malformed-property (page-top only), and stale-map / map-label. The digest ones are fixed with brain digest <page> --apply.
    • Needs your call: description-link, broken-link, new-property-key, duplicate-entry, structural-integrity, jira-markup, missing-digest, stale-digest, oversized-digest, nonconvergent-map, duplicate-map — the helper refuses to write on either (see the catalog), so the fix is a human edit, not a bulk pass.

    The headline total counts every bucket. Example:

    Brain health: 16 issues across 8 pages
    Auto-fixable (5):   3 bare #tags (incl. C#-parity), 1 {{code}}, 1 stale Map
    Needs your call (7): 4 links to missing task pages, 2 one-off property keys, 1 unfenced Jira draft
    Digests (4):        4 pages with no digest — say "backfill digests"

    Empty pages that lint no longer explains: Logseq never deletes a page once it has parsed one. Pages created by text that has since been fixed survive until the graph is re-indexed in Logseq (All graphs → Re-index). A clean brain lint plus leftover empty pages means "re-index", not "fix more". If the graph is clean, say so, run brain activity "ran brain-doctor (clean)", and stop.

  3. Confirm before any write. Ask which classes to fix if the user hasn't said.
  4. Back up first. The graph is usually not git-tracked, so there is no undo. Copy pages/, journals/ and logseq/ to a timestamped folder (e.g. <graph>/.brain-doctor-backup-<yyyy-MM-dd-HHmm>/, or the host scratchpad) and say where. If the backup fails, stop.
  5. Repair.
    • Mechanical classes: Edit each hit per the catalog's remediation. For high-volume classes a single scripted pass is acceptable (this is one-time maintenance, and a backup exists), but it must mask fenced blocks and inline code exactly as the catalog says.
    • stale-map / map-label: brain digest <page> --apply per page.
    • Needs-your-call findings: walk them with the user and apply only what they approve. Never auto-write broken-link, duplicate-entry or structural-integrity changes.
  6. Verify.
    • brain lint <every file you changed>: expect zero for the fixed classes.
    • Per-file backtick parity (the catalog's "After repair — verify").
    • Then brain digest <page> --apply on every digest-bearing page whose bytes changed. This is a Remap: digest-updated:: and the prose stay untouched. Skip a page with no ## Digest section — that's a missing-digest finding, not a Remap, and --apply exits 2 on it; leave it to the guided digest backfill below.
  7. brain activity "ran brain-doctor · fixed <N> issues" and report the result.

Guided digest backfill

Triggered by "backfill digests" or accepted from a missing-digest report. The shape is: report, cost, one confirmation, then work.

  1. Report what is missing: the missing-digest rows from brain lint --all. These are pages with no ## Digest, and pages whose Digest has no Map line (e.g. Tasks/CRMGM-2037). List each with its size from brain sections, largest first; the biggest pages pay back a digest soonest.
  2. State the cost before spending it. For each page, brain sections <page> gives the sections its Rebuild branch will read (skills/_shared/digest.md). Project-shaped pages read Overview, Current Plan, the Decisions headlines and the Session Log tail. Task-shaped pages read their largest one or two sections. Cap each at 8 KB and sum. Quote the sum: "14 pages, ~340 KB on disk — the bounded reads come to roughly 90 KB. Proceed?"
  3. Offer a scope: project pages by default; "all" includes task pages.
  4. Back up first, unconditionally.
  5. Build each digest per the Rebuild procedure in skills/_shared/digest.md: bounded reads, then prose Edits, then brain digest <page> --apply. Work one page at a time, so an interruption leaves a consistent graph.
  6. Report pages built, skipped and bytes read. Then brain activity "backfilled N digests".

Never build a digest without confirmation. It is a write, and it costs tokens.

Important Notes

  • Report before repair, backup before write, confirm before both.
  • A link to a missing page is not always a bug. Tickets that don't have a page yet are legitimate forward references. Surface them, but leave them unless the user says otherwise.
  • Logseq may be running. Recommend closing it (or pausing Sync) during a bulk fix.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.