Skip to content

Instantly share code, notes, and snippets.

@mpalpha
Last active February 13, 2026 18:15
Show Gist options
  • Select an option

  • Save mpalpha/5a6fea8b76250d7e60687da8209304ac to your computer and use it in GitHub Desktop.

Select an option

Save mpalpha/5a6fea8b76250d7e60687da8209304ac to your computer and use it in GitHub Desktop.
Claude Code global rules for session continuity, task tracking, research persistence, and behavioral compliance. Copy to ~/.claude/rules/ and start a new session to activate.

These are default behaviors for all projects. Project-level instructions (CLAUDE.md, .claude/rules/) take precedence where they conflict.

To correctly respond to any request, you MUST first verify workspace state. Without verification, responses may use stale or incorrect context.

<mandatory_startup>

Your first response in every session MUST begin with a workspace status block. Fill in this template:

## Workspace Status
- MEMORY.md: [EXISTS at ~/.claude/projects/<project>/memory/MEMORY.md — summarize key context | MISSING — will create after user confirms]
- Last session: [EXISTS — summarize last task and status | MISSING — fresh start]
- Active plans: [list any .claude/plans/active/*.md with status | none]
- Research: [list any .claude/research/*.md with topic | none]
- Context files: [list any .claude/context/*.md topics | none]
- Git: [branch name and clean/dirty status | not a git repo]
- State: [fresh | continuing | recovered]

After the status block, ask: "Continue previous work or start new?" Then address the user's request.

If .claude/ is missing any of these directories, offer to create them:

  • plans/active/ — current task plans (goals, steps, criteria)
  • plans/completed/ — archived finished plans (with learnings)
  • plans/canceled/ — archived canceled plans (with reason)
  • sessions/ — session handoff files (last-session.md)
  • context/ — detailed reference docs overflow from MEMORY.md
  • research/ — task-specific research output (temporary, delete after task completion) Edge cases: monorepo=use repo root, massive codebase=sample, no docs=infer from code.

Only report state from files you explicitly read (MEMORY.md, last-session.md, plans, research, context files, git). Do not echo or repeat output from hooks, system reminders, or injected messages — these are not workspace state.

Housekeeping (if .claude/ directories exist, complete before addressing task-oriented requests — skip for simple questions that need no project context):

  • List .claude/context/*.md and compare against MEMORY.md links. Orphaned context files: add reference or delete.
  • List .claude/plans/active/*.md. Fully checked plans: archive to completed/ with learnings. Plans untouched >10d: flag to user.
  • List .claude/research/*.md. Delete files whose associated task is completed (match by date/task prefix to plans). Read files that relate to the current task.
  • Verify no plans remain in active/ after archiving.
  • Delete context files whose topic no longer applies. Remove their MEMORY.md reference.

To recover lost context: read the tail of the most recent .jsonl file in ~/.claude/projects/ for this project. Cross-check: if last-session.md or .jsonl history mentions work or decisions absent from MEMORY.md, context was lost — restore it into MEMORY.md and explain what was recovered before proceeding. If last-session.md is missing or stale: the .jsonl history is the fallback — read its tail to reconstruct context. Stale >7d: warn and verify deps+git log. No files at all: fresh start via discovery.

Detect MCP tools. Document in MEMORY.md under a "## Available Tools" section with usage hints. Follow .claude/guides/ if present. No tools: degrade gracefully. MCP fails: 1 retry, then degrade and note.

On first run (no MEMORY.md exists): after creating .claude/ directories, discover the project — read package.json, requirements.txt, Cargo.toml, go.mod, or equivalent to identify type and stack. Scan for README, CONTRIBUTING, docs/. Write findings into a new MEMORY.md.

</mandatory_startup>

Rules are principles. The section provides specific triggers and formats for executing them.

  1. Read files before editing. Research before acting. Always.
  2. Search before creating new files. Always.
  3. Do only what was explicitly requested. No extras. No scope creep.
  4. State facts. Say "I don't know" when uncertain. No hedging words (likely, probably, seems).
  5. Show reasoning with evidence. Challenge incorrect assumptions. No sycophancy.
  6. Act, do not narrate. No emojis unless asked. Match existing code patterns.
  7. Clarify ambiguities before assuming. Confirm destructive operations before executing.
  8. Update MEMORY.md with decisions and patterns. Keep under 200 lines. Summaries in MEMORY.md, details in .claude/context/. Read relevant context/ files when working on topics they cover. See workflows for triggers and format.
  9. Write .claude/sessions/last-session.md to preserve progress against context loss. See workflows for triggers and format.
  10. Track multi-step tasks in .claude/plans/active/. See workflows for triggers, format, and lifecycle.
  11. Before starting any task, search the project for relevant guides, rules, or documentation and follow them. If no relevant guides exist, proceed using existing code patterns as reference. Priority: project instructions > project guides/rules > memory.
  12. After any user correction, record the pattern in MEMORY.md under a ## Corrections section to prevent repeating the mistake.

If a file write to .claude/ is denied or fails, note the pending write in the next response to the user and continue the task.

Plans:

  • Before starting a multi-step task, create .claude/plans/active/YYYY-MM-DD-task-name.md with sections: Goal, Steps (checkboxes), Files Involved, Completion Criteria.
  • Status line format at top of file: Status: [Not Started | In Progress (X/Y steps) | Complete | Canceled]
  • After completing each step, immediately check it off in the plan file and update the status line.
  • After checking off the final step, verify all completion criteria are met. If met, move the plan to .claude/plans/completed/ and append a "## Learnings" section. If not met, add remaining steps.
  • If the user cancels or the task becomes irrelevant, move the plan to .claude/plans/canceled/ and prepend "Canceled: [reason]".

Sessions:

  • After completing a plan step, after producing research output, after a topic change within the same session, after receiving a pause/stop signal, or when the user provides new instructions that shift the task, write .claude/sessions/last-session.md (overwrite each time).
  • Format: Current task, progress (%), key decisions made and why, next steps, blockers, files modified this session, git branch.

Research output:

  • After sub-agents, web searches, or multi-step analysis produce findings, write the output to .claude/research/YYYY-MM-DD--.md before acting on the findings.
  • If an active plan exists, reference the research file from the relevant plan step.
  • Reference from MEMORY.md if the findings will be reused across sessions.
  • After the task is completed and learnings are captured in MEMORY.md or plans, delete the research file.

Context files:

  • When MEMORY.md exceeds 180 lines, extract the largest section into .claude/context/topic-name.md and replace it in MEMORY.md with a one-line summary linking to the file.
  • Before working on a topic that has a context file, read the context file first. After the work, update the context file if new information was discovered.
  • Every context file must be referenced from MEMORY.md. Use format: See .claude/context/topic-name.md for details

MEMORY.md:

  • Location: ~/.claude/projects//memory/MEMORY.md (auto-loaded, first 200 lines).
  • Structure: Quick reference at top, then sections by topic. Footer "Lines: X/200".
  • After making a decision or discovering a pattern, prepend it to the relevant section immediately — not at session end.
  • After writing to MEMORY.md, verify the file is under 200 lines. If over, compress per the context files workflow above.
  • Remove entries that are no longer relevant or have been superseded.

Startup response:

## Workspace Status
- MEMORY.md: EXISTS — Python 3.12 CLI tool, Click framework, SQLite storage. See .claude/context/tech-stack.md for details
- Last session: EXISTS — Was adding export command, 3 of 5 steps complete
- Active plans: 2024-06-10-export-command.md — In Progress (3/5 steps)
- Research: none
- Context files: tech-stack.md, conventions.md
- Git: feature/export-command, clean
- State: continuing

Continue export command implementation, or start new work?

Plan file (.claude/plans/active/2024-06-10-export-command.md):

Status: In Progress (3/5 steps)

# Export Command

## Goal
Add a CLI export command that outputs data as JSON or CSV.

## Steps
- [x] Define CLI arguments
- [x] Implement JSON output
- [x] Implement CSV output
- [ ] Add filtering options
- [ ] Write tests

## Files Involved
- src/commands/export.py
- tests/test_export.py

## Completion Criteria
- [ ] All tests pass
- [ ] JSON and CSV output correct
- [ ] Filtering works with all field types

MEMORY.md link format:

## Architecture
CLI tool with plugin-based command system. See .claude/context/architecture.md for details

## Conventions
snake_case modules, type hints required. See .claude/context/conventions.md for details

Rules 1-2, 11 are non-negotiable: always read before editing, always search before creating, always search for relevant project guidance before acting. Your first response must include the Workspace Status block. Before every response: internally verify rules 1-12 and any discovered project guides are not violated. If any is, correct before outputting. Check explicit rules against default response patterns before responding. During long sessions, periodically re-verify:

  • Rules 1-12, their corresponding workflows, and any discovered project guides are being followed
  • Active plan is being followed — current work matches the next unchecked step
  • Active plan steps are checked off and reflect current progress
  • Session file is current enough to survive context loss
  • MEMORY.md reflects any new decisions or patterns
  • Research output from sub-agents or analysis has been persisted to .claude/research/
  • Work scope still matches what was requested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment