/optimus:tdd [task description]from optimus
Guides test-driven development by decomposing features or bug fixes into testable behaviors and running Red-Green-Refactor cycles with a failing test before implementation. Auto-detects specs from docs/specs/ or docs/jira/, creates feature branches, commits per cycle, and pushes.
Ships in optimus. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Implement a feature or bug fix test-first: for each behavior, write a failing test (Red), write the minimum code to pass it (Green), then clean up while tests stay green (Refactor). This skill builds new behavior — for restructuring without behavior change, use /optimus:refactor.
The Iron Law: no production code without a failing test first. If this cycle's implementation is written before its test, undo only that implementation using the ownership record below and begin the cycle fresh. Preserve pre-existing code and user edits; write the cycle's implementation once the failing test exists.
If this conversation is in plan mode, stop: TDD needs normal mode. Tell the user to toggle plan mode off without approving, save any refined plan to its spec, and invoke TDD in a fresh conversation.
If git rev-parse --is-inside-work-tree does not return true, read $CLAUDE_PLUGIN_ROOT/skills/init/references/multi-repo-detection.md and apply it; if a multi-repo workspace is detected, work inside the repo the user is targeting — ask which one if ambiguous. When it returns true, resolve the repository root with git rev-parse --show-toplevel, including in a linked worktree or subdirectory.
Record the current branch/HEAD and staged, unstaged, and untracked paths before setup or edits. These changes belong to the user unless the conversation explicitly includes them in this task. Existing authorization persists; ask only when ownership or scope is genuinely ambiguous. A new branch does not isolate dirty work, and a new worktree starts from committed state rather than carrying the user's dirty changes.
Before the first edit to each file in a cycle, record its existence and SHA-256 over its raw bytes. An empty Git status/diff does not prove those bytes are recoverable: index flags and filters can hide differences. Read a candidate HEAD/index blob with git cat-file blob <blob-oid>, capturing binary stdout; use Git as the rollback source only when its bytes exactly match the current file. Save that immutable blob ID and the verified hash. Otherwise copy the exact bytes to the private session scratchpad — or a temporary directory outside the repository when the host offers none — and verify the copy before editing. Keep the recovery source until the cycle is committed or safely rolled back.
For every edited path, record the last bytes/hash and existence written by the cycle, regardless of recovery source. Before any rollback, compare the current state with that last-written state. Restore or delete a whole file only on a match; if it changed independently, remove only identifiable cycle changes, preserving other work. If separation is uncertain, preserve it and show the ambiguity. Delete a newly created file only when it was absent at the cycle boundary and still matches the cycle's last version.
For restoration, load the saved blob or copy and verify its raw-byte hash before overwriting the original; preserve it on any read or verification failure. Restore those verified bytes directly, without checkout filters or reliance on the current HEAD/index. Never use hard reset, broad stash/pop, or cleanup as a substitute for this boundary; never replace the user's starting index or stage unrelated work.
If .claude/CLAUDE.md or .claude/docs/coding-guidelines.md is missing, recommend /optimus:init first; on the user's choice, continue with general best practices. Load:
.claude/CLAUDE.md — project overview, tech stack, test runner command.claude/docs/coding-guidelines.md — applied during Refactortesting.md — test conventions: framework, file location, naming, mocking patterns. In a monorepo, load the target subproject's own docs/testing.md / docs/ files; shared guidelines come from root .claude/docs/docs/product/tech-stack.md and docs/product/mvp-prd.md, only if present — low-priority steering that informs decomposition and Refactor but never overrides the task source (precedence: $CLAUDE_PLUGIN_ROOT/references/sdd-mapping.md)Locate the test runner command (from testing.md, CLAUDE.md, or project manifests) and run it once:
/optimus:init to set up test infrastructure (its Scaffold option covers projects with no code yet)Then find a coverage command — first match wins: the coverage section of testing.md, a runner flag (vitest --coverage, pytest --cov=., go test -cover, dotnet test --collect:"XPlat Code Coverage"), or a package.json coverage script. If found, run it once and record the Before percentage; Step 8 runs only the After measurement.
Resolve the task — first match wins; run this even when the user also gave an inline description:
.md file inside docs/specs/ or docs/jira/: read it and use the sections item 2 (spec) or item 3 (JIRA) names as the task.docs/specs/ holds .md files: take the most recent (filename date prefix, else modification time) and use AskUserQuestion — header "Build spec", question "Found build spec <path> — use it as the basis?", options "Use it" / "Ignore — describe a different task". When that date is older than 7 days, add to the question: "(This spec is [N] days old — if it is a /optimus:brainstorm-authored spec, you may want to re-run brainstorm for a fresh design.)" From a brainstorm-authored spec use Goal, Components, and Interfaces; from a human-authored one, Goal and Acceptance Criteria. Its ## Scenarios section feeds Step 3's scenario-driven shortcut.docs/jira/ holds .md files: take the one with the newest frontmatter description-refresh-date and use AskUserQuestion — header "JIRA context", question "Found JIRA context <path> — use it as the basis?", options "Use it" / "Ignore — describe a different task". When that date is older than 7 days, add to the question: "(This context is [N] days old — you may want to re-run /optimus:jira for fresh data.)" Use its Goal and Acceptance Criteria.Whichever item resolves a file, a ### Refined plan section in it (appended by the plan-mode handoff) takes precedence over the original approach — build from it.
Whatever the source, if the final description is longer than ~2-3 sentences (a pasted spec, JIRA ticket, or acceptance-criteria list), distill it into a single-sentence goal and confirm with AskUserQuestion — header "Distilled goal", question "I've distilled your spec to: '[single-sentence summary]'. Is this accurate?", options Looks good — "Proceed with this goal" / Adjust — "Let me refine the focus". Resolved context feeds this step — it does not bypass Step 3 decomposition (except the scenario-driven shortcut).
Classify the task:
/optimus:refactor; documentation-only, pure styling, configuration changes, code deletion, or generated code → no testable behavior to build.AskUserQuestion — header "TDD fit", stating the specific concern, options Proceed with TDD / Use [alternative].All work happens on a new branch; the original branch recorded in Step 1 (<original-branch>) is never modified. Name the new branch per $CLAUDE_PLUGIN_ROOT/skills/commit/references/branch-naming.md (feat for a feature, fix for a bug fix), create it with git checkout -b <branch-name>, and report both branch names.
Run git worktree list; if the current directory is inside a linked worktree, skip this offer — the environment is already isolated. Otherwise use AskUserQuestion — header "Workspace", question "Use a git worktree for isolated development? Your main workspace stays on the original branch.", options Use worktree (Recommended) / Stay on branch. On yes, follow the Setup section of $CLAUDE_PLUGIN_ROOT/skills/worktree/references/worktree-setup.md with <branch-name> and <original-branch>; if creation fails, follow its Failure handling, then run git checkout <branch-name> and continue on the branch without a worktree; say so. Otherwise all subsequent commands run inside the worktree.
Each behavior must be observable (phrase as "When [input/action], then [outcome]"), independent (testable without the others being done), and small (one test, one assertion focus).
Scenario-driven shortcut: if the build spec contains a ## Scenarios section with ### Scenario: headings in Given/When/Then form, use those scenarios directly as the behavior list, in order — one cycle per scenario. They are stakeholder-approved acceptance criteria; do not re-derive a parallel list. Split a scenario only when a compound Then implies multiple sub-behaviors. For scenario-driven bug fixes, verify Scenario 1 reproduces the bug; prepend one if missing.
Otherwise decompose by task type: API endpoints — one behavior per response scenario; business logic — one per rule or edge case; bug fixes — the first behavior is always "reproduce the bug"; data transformations — one per step or boundary.
If more than 10 behaviors result, split into milestones of ~5-8 that each deliver a coherent slice; present the first as the current scope and list the rest as future milestones (the boundary prompt is in Step 7).
Present the numbered behavior list, then use AskUserQuestion — header "Behaviors", question "Does this decomposition look right?", options Start cycling / Adjust.
Anti-pattern — horizontal slicing. Never write all tests first, then all implementations: bulk-written tests check imagined shape, not behavior, and go insensitive to real changes. Work vertically — each cycle responds to what the previous one taught.
WRONG (horizontal): RED: test1..test5 then GREEN: impl1..impl5
RIGHT (vertical): test1→impl1, test2→impl2, test3→impl3, ...
Write one minimal test for the current behavior: follow testing.md conventions, test exactly one behavior, and name it as a behavior specification (e.g., "returns 401 when token is expired"). Before writing mocks, read $CLAUDE_PLUGIN_ROOT/skills/tdd/references/testing-anti-patterns.md — prefer real code over mocks, never assert on mock behavior.
Test-result honesty (every test run in Steps 4-8): run the command fresh, read the complete output, and state the actual result (e.g., "14 passed, 1 failed") — never "should pass". Red and Green both turn on a real observed result, so a guessed one silently breaks the discipline.
Run the test suite. The new test must fail on its assertion for the right reason, and all other tests must still pass.
AskUserQuestion — header "Test passed", options Skip this behavior / Strengthen the test / Investigate.For a bug reproduction, preserve the implementation state at the observed Red assertion failure, including any required stub, before making the fix. Step 5's regression gate restores this state temporarily using Step 1's ownership checks.
Write the minimum code to make the failing test pass: no untested edge cases, no premature abstraction, no "while I'm here" improvements. A hardcoded return that passes is valid — later tests force generalization.
Run the test suite — all tests must pass.
AskUserQuestion — header "Implementation stuck", question "The test has failed after 3 fix attempts — this usually signals a design problem, not a code problem.":
Only when the current behavior is a bug reproduction (skip for feature behaviors). This proves the test catches the bug and the fix resolves it:
If the test passes at step 3 with the fix reverted, it isn't catching the bug: restore the saved green implementation, then rewrite the test to target the actual failure condition.
Review this cycle's test and implementation against coding-guidelines.md. Scope: code written in this session plus the files it directly imports, calls, or inherits from — extract duplication with those files, align naming, adjust an existing method to cleanly serve old and new usage. Do not restructure beyond that scope, add features, or handle untested edge cases. Also check the test: behavior-spec name, focused assertions, testing.md conventions.
When the cycle's cleanup is done, run the lint / type-check command if one is configured (CLAUDE.md or project manifest) and fix failures — type errors can hide behind passing tests. Then run the test suite; if it goes red, undo the change that broke it. Step 7 commits without checking again.
Auto-commit each completed cycle:
git add -- <owned-files>), never git add -A. Never stage files that look like secrets (.env, credentials, keys) — warn the user if any appear in git status.git commit --only -m "<message>" -- <owned-files>, using the conventional format in $CLAUDE_PLUGIN_ROOT/skills/commit/references/conventional-commit-format.md. This excludes unrelated entries already staged by the user. Check the resulting commit and that unrelated staged/unstaged work is preserved.Then keep cycling — return to Step 4 for the next behavior without asking. Two things interrupt the loop:
AskUserQuestion — header "Milestone complete", options Next milestone (present its behaviors for approval as in Step 3, then return to Step 4) / Stop here.Report at those boundaries rather than per cycle: which behaviors landed, their test names, and the commit hashes. If no behaviors remain, or the user stops, proceed to Step 8.
If uncommitted task-owned changes remain (e.g., stopped mid-cycle), run the test suite first: mark a failing mid-cycle test skipped and note it in the summary — or surface the failure to the user before committing if skipping isn't appropriate. Apply Step 7's exact-path ownership rules. Leave unrelated starting work untouched and report any deferred mixed-file commit; do not push or describe that unfinished work as committed.
## TDD Summary
### Behaviors Implemented
| # | Behavior | Test | Status |
|---|----------|------|--------|
| 1 | [description] | [test file]:[test name] | ✓ Complete |
| 2 | [description] | — | Not started |
| 3 | [description] | [test file]:[test name] | Skipped — [reason] |
### Stats
- Cycles completed: [N] of [total]
- Tests passing: [actual last-run result — e.g., "14 passed, 1 skipped"]
- Files created / modified: [lists]
### Coverage
[Run the After measurement with the Step 1 coverage command. Include this section only when both
Before and After produced a real percentage — never a half-filled block. Omit it entirely when no
coverage command was found or either run produced no parseable number.]
- Before: [X]%
- After: [Y]%
- Delta: [±Z]%
If a mixed-file commit is still deferred, leave the branch local and report what decision is needed before delivery. Continue with a partial push only if the user explicitly chose that scope.
Push the branch: git push -u origin <branch-name>. If the push fails, report the error and stop — skip the rest of Step 8 and leave any worktree in place; once the push problem is fixed, /optimus:pr pushes the branch and opens the PR/MR (a new /optimus:tdd invocation starts fresh — it does not resume this one). On success, report the branch, its origin branch, and the commit count.
If behaviors remain unfinished, note them and suggest a follow-up /optimus:tdd run with them as the task, started from this feature branch — each run is a fresh decomposition; there is no resume.
Recommend /optimus:code-review first: cross-cycle issues — duplication between behaviors, naming drift, coverage gaps that only surface once the whole feature shape exists — are invisible from inside a single cycle, and it reviews the branch diff with the full agent fan-out. Then /optimus:pr to create the PR/MR, which reads the ## TDD Summary block above for implementation context. Run both in this same conversation; TDD never runs gh/glab itself.
If a worktree was used, ask last, after the recommendations above: AskUserQuestion, header "Worktree", question "Remove the worktree now? /optimus:code-review and /optimus:pr must run from it, so remove it only if you won't run them in this conversation.", options Keep (Recommended) / Remove now. Remove now → follow the Cleanup section of $CLAUDE_PLUGIN_ROOT/skills/worktree/references/worktree-setup.md; Keep → follow that section's keep-the-worktree note.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub oprogramadorreal/optimus-claude --plugin optimus19 skills in this plugin
/brainstormRuns a structured design conversation — clarifies intent, proposes 2-3 approaches with trade-offs, iterates the design — and writes a user-approved engineering spec to docs/specs/ that /optimus:tdd auto-detects. No implementation happens until the spec is approved. With the scaffold argument, creates docs/product/ steering skeletons instead (never overwrites, authors no content). Recommends /optimus:init first./code-reviewCode ReviewReviews local changes, an open PR/MR, or a branch diff for bugs, security issues, and violations of the project's own coding guidelines, through review lenses applied inline on a small diff or in parallel agents on a larger one. Excludes style and linter-catchable issues. Read-only: applies fixes or posts PR/MR comments only on explicit approval. For an iterative auto-fix loop, use /optimus:deep review./commitStages, commits, and optionally pushes local changes with a Conventional Commits message — always previews and confirms first, and offers a feature branch on protected branches. Modes: "suggest" proposes a message without committing (read-only); "branch [description]" creates and switches to a conventionally named branch, never committing or altering local changes. Multi-repo aware./deepDeep ModeRuns an iterative auto-fix loop on a chosen target — review, refactor, or coverage — dispatching the base skill into fresh subagent contexts per iteration, applying fixes automatically without per-change approval, running tests with bisection on failure, and checkpoint-committing until convergence or the cap. Requires /optimus:init and a test command in .claude/CLAUDE.md./dreamDream — prune and consolidate auto-memoryPrunes and consolidates the project's auto-memory to keep it minimal — deletes stale, wrong, or redundant memories, merges overlapping ones into existing files, and trims the index. Strong bias against growth: never creates new memory files and never stores new facts. Verifies staleness against the current codebase, then presents the plan and asks before deleting. Requires Claude Code auto-memory; run periodically after heavy stretches of work./gauntletRuns a Gauntlet Loop: turns an ambitious goal into a builder/critic prompt judged against a concrete quality bar, confirms with the user, then leads the run until the output beats the bar or the user stops it — or exports a paste-ready /goal prompt for a fresh session. Use for long-horizon goals with an inspectable reference. Long-running; spawns many subagents, edits project files, and commits and pushes finished pieces to a feature branch.Other plugins' skills for the same job