name: tcl-optimise description: "Apply LSP optimiser suggestions to a Tcl file and explain why each optimisation is safe and beneficial. Covers constant folding, propagation, dead code elimination, strength reduction, and expression canonicalisation. Use when optimising Tcl code, improving .tcl file performance, refactoring Tcl scripts for efficiency, or applying language server optimisation suggestions." allowed-tools: Bash, Read, Edit
Tcl Optimise
Apply LSP optimiser suggestions to Tcl files with safety explanations for each transformation.
Steps
- Read the domain knowledge from
ai/prompts/tcl_system.md - Read the Tcl file to optimise
- Run the optimiser:
uv run --no-dev python ai/claude/tcl_ai.py optimize $FILE - If the tool fails (e.g. file not found or parse error), report the error clearly and suggest fixes
- If no optimisations found, report the code is already well-optimised
- If the tool outputs an "Optimized Source" section, apply it to the file using the Edit tool
- For each optimisation applied, explain in 1-2 sentences:
- Why it is safe (preserves behaviour)
- What benefit it provides
- Validate the optimised file to confirm no regressions:
uv run --no-dev python ai/claude/tcl_ai.py diagnostics $FILE - If validation finds new issues, revert the problematic optimisation and explain why
Optimisation codes reference
See docs/generated/optimisation_codes.md for the full auto-generated table of O100+ codes.
Key categories:
- Readability (O111, O114, O115, O117, O120, O128): Idiomatic rewrites (incr, eq/ne, bracing)
- Constant folding/propagation (O100, O101, O102, O103, O105, O110, O113, O116, O118, O129): Inline and simplify known values
- Pattern recognition (O104, O119, O130): Fold string chains, pack consecutive sets
- Dead code (O107, O108, O109, O112, O124, O126): Remove unreachable or unused code and stores
- Code motion (O106, O125, O127): Hoist, sink, and inline assignments
- Recursion (O121, O122, O123): Tail-call rewriting and accumulator hints
Optimiser profiles
- off: All optimisations disabled
- readability (editor default): O111, O114, O115, O117, O120, O128
- standard: readability + constant folding + pattern recognition
- full (CLI/AI default): All 28 passes, single pass
- aggressive: All passes, multi-pass to fixpoint
Grouped optimisations
The optimiser automatically groups causally-linked passes. When constant propagation/folding makes a variable definition dead, the resulting dead store elimination is grouped with the propagation as one logical optimisation. The tool output shows these as a single item with sub-entries. When explaining grouped optimisations, treat them as one transformation.
$ARGUMENTS