agentleFS
Kastalien-ResearchKastalien-Research/thoughtbox/.agents/skills/thoughtbox-refactor/SKILL.md

thoughtbox:refactor

Skill64 starsChanged 4 months agoMITView on GitHub

Friction-gated refactoring using the Theseus protocol. Prevents scope creep and refactoring fugue state by enforcing file scope boundaries, visa-based expansion, checkpoint audits, and a brittleness counter. Use when refactoring code, restructuring modules, renaming across files, or any task where "structure changes but behavior must stay the same". Triggers on "refactor", "restructure", "rename across", "extract module", "move code", or when a change touches 3+ files without adding features.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 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.

  • 0

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If tests fail, B increments.

    line 67
  2. Distinctive:

    If you discover you need to touch a file outside your declared scope, request a visa:

    line 71

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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

Theseus Refactoring Protocol

Refactoring has a failure mode: scope creep. You start renaming one function and end up restructuring three modules. Theseus prevents this by locking your file scope upfront, requiring explicit justification to expand it, and tracking how often your changes break tests.

Core Mechanics

  • Scope: declared file list at init — you can only touch these files
  • Visa: explicit request to touch an out-of-scope file (requires justification)
  • Checkpoint: submit each logical change for audit before proceeding
  • B-counter: tracks test failures — high B means your refactor is causing breakage
  • Terminal state: completed, abandoned, or rolled back

Workflow

Phase 1: Declare Scope

Identify every file you expect to touch. Be specific — this is your contract.

// thoughtbox_execute
async () => {
  return await tb.theseus({
    operation: "init",
    scope: ["src/auth/handler.ts", "src/auth/types.ts", "src/auth/middleware.ts"],
    description: "Extract token validation into standalone module"
  });
}

The session starts with B=0 (no test failures yet).

Phase 2: Work Within Scope

Make changes to declared files. After each logical unit of work:

  1. Checkpoint the change:
async () => {
  return await tb.theseus({
    operation: "checkpoint",
    diffHash: "abc123",  // git diff hash or commit SHA
    commitMessage: "refactor(auth): extract TokenValidator class",
    approved: true,
    feedback: "Clean extraction, no behavior change"
  });
}
  1. Run tests and record outcome:
async () => {
  return await tb.theseus({
    operation: "outcome",
    testsPassed: true,
    details: "All 47 auth tests pass"
  });
}

If tests fail, B increments. If B reaches 3, stop and reassess — your refactor is causing too much breakage.

Phase 3: Expand Scope (If Needed)

If you discover you need to touch a file outside your declared scope, request a visa:

async () => {
  return await tb.theseus({
    operation: "visa",
    filePath: "src/routes/login.ts",
    justification: "Login route imports TokenValidator directly — need to update import path",
    antiPatternAcknowledged: true
  });
}

Setting antiPatternAcknowledged: true is required — it forces you to recognize that scope expansion is a code smell. If you're requesting many visas, your initial scope was wrong.

Phase 4: Check Status

Monitor your session state anytime:

async () => {
  return await tb.theseus({ operation: "status" });
}
// Returns: B counter, scope list, visa count, audit count

Phase 5: Complete

async () => {
  return await tb.theseus({
    operation: "complete",
    terminalState: "completed",
    summary: "Extracted TokenValidator into src/auth/token-validator.ts. 3 files changed, 1 visa (login route import). B=0."
  });
}

Terminal states: "completed", "abandoned", "rolled_back"

Discipline Rules

SignalAction
B = 0 after all checkpointsRefactor is clean — proceed to complete
B = 1-2Fix the failing tests before continuing
B >= 3Stop. Your approach is causing too much breakage. Consider abandoning or reducing scope.
Visa count > 2Reassess your scope declaration — it was too narrow
Visa count > 5Your "refactor" is probably a rewrite. Stop and re-scope.

When NOT to Use Theseus

  • Adding new features (use regular development workflow)
  • Bug fixes (use thoughtbox:debug / Ulysses instead)
  • One-file changes (overkill — just make the change)
  • Exploratory prototyping (scope locking kills exploration)

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.