Jump to content

Let AI self-improve your coding prompts with AI rules


gebeer
 Share

Recommended Posts

What is this about?

When working with AI enhanced IDEs like Cursor or Windsurf, we often tend to give somewhat ambiguous requests like these exaggerated examples:
 

  Quote

Make the menu items slide when clicked

Expand  

or

  Quote

Add form validation to contact form

Expand  

Much better wording for these examples would be

  Quote

Add click event listeners to .menu-item elements that trigger 300ms CSS transition, sliding child .submenu elements from height:0 to height:auto

Expand  
  Quote

Implement validateForm() function for #contact-form: validate email format, required fields, max lengths. Show error messages in .error-message elements. Prevent default submit if invalid.

Expand  

AI tends to deliver much better results when we give it concise, technical instructions that fit the context.

My approach

I have tried to "automate" this in some ways with simple AI rules. Both Cursor and windsurf have a feature called AI rules where you can set global and project specific rules that the assistant will follow.

This snippet is in my global rules:

## User Prompt Rephrasing
Every time you encounter the exact keyword "rephrase" in a user prompt, do the following:
1. rephrase the user prompt in concise technical terms focusing on:
   - specific technical task scope
   - affected components/files
   - required functionality changes
2. preserve the users intent in the rephrased prompt
3. output the rephrased prompt and ask for confirmation with exact phrase "Act on the rephrased prompt? [y/n]"
4. IMPORTANT: after asking for confirmation, STOP and wait for explicit user response
5. proceed ONLY after receiving "y" confirmation, otherwise ask for clarification
6. when proceeding, act only on the rephrased prompt

How it works

Now, whenever I add "rephrase" to my prompt, the assistant will act accordingly.

Example:

image.png.ae4c76f8b6e34af86d189931c8ed405c.png

Benefits

The rephrased version offers several benefits over the original version:

  • Component Clarity - Original was vague, rephrased version explicitly lists required components (GUI framework, drag-drop handler, PDF converter)
  • Scope Definition - Clearly separates existing functionality (PDF conversion) from new requirements (GUI wrapper)
  • Implementation Direction - Suggests specific technical approaches (tkinter/PyQt) while maintaining flexibility

The AI assistant will perform better with the rephrased prompt because:

  • More precise input leads to more precise output - technical specifications eliminate ambiguity about what to implement
  • Breaking down into components helps the AI reason systematically about the solution architecture
  • Explicit requirements (e.g., "single file processing") prevent the AI from making incorrect assumptions about scope

Clear instructions to AI yield clear results. Or as the old saying goes: garbage in, garbage out :-)

A bit of theory behind the concept

The idea for a rule like that came to me when I heard about the concept of "Latent Space Activation" in Large Language Models. Very brief explanation from Claude:

  Quote

A latent space is a compressed dimensional representation where AI models encode data's key features. Think of it as a semantic map where similar concepts cluster together and meaningful patterns emerge. During training, models learn to activate specific regions of this space to capture and manipulate abstract features of the input data, enabling tasks like generation and transformation while preserving essential characteristics.

For example, in a language model, words with similar meanings would activate nearby regions in the latent space, allowing the model to understand and generate semantically related content.

Expand  

My rephrasing prompt supposedly has some impact on latent space activation. Claude again:

  Quote

The rephrasing technique maps particularly well to latent space concepts in AI, as it essentially performs semantic compression similar to how latent spaces encode high-dimensional data:

1. Dimensional Reduction - Converting verbose natural language into focused technical specifications parallels how latent spaces reduce high-dimensional data to essential features

2. Semantic Preservation - Like latent representations maintaining semantic meaning while reducing dimensionality, the rephrasing preserves user intent while stripping unnecessary context

3. Structured Transformation - The systematic conversion process mirrors how encoders transform inputs into structured latent representations

4. Validation Gateway - The confirmation step acts as a feedback mechanism to verify semantic preservation, similar to reconstruction loss in autoencoders

This parallel suggests the technique could be valuable for human-AI interaction patterns where precise semantic understanding is crucial.

Expand  

Does it really work?

I've been experimenting with this for several days now and my subjective impression is that I really get better results with this approach. Better, working code often on the first shot.

Try it yourself

Have a play and let me know if you get better results, too.

  • Like 5
Link to comment
Share on other sites

This reminds me of the AIDER Architect Workflow i saw a while back.

In short:

  • 2 instances
    • one for a technical concept/developer manual
    • one for doing the work
  • the first instance uses Gemini Flash, Deepseek R1, or an OpenAI Reasoning model and puts every little detail into separate files
  • the second instance uses Claude Sonnet 3.5 and does all the heavy code lifting

Your workflow is super similar, yet way slimmer and probably faster for most tasks.

Need to try this in my rules!

  • Like 2
Link to comment
Share on other sites

  On 2/20/2025 at 9:07 PM, wbmnfktr said:

This reminds me of the AIDER Architect Workflow i saw a while back.

Expand  

Actually, my rule is quite different from aider architect mode where the architect agent plans and lays out the implementation steps on a higher level and then passes that to the coding agent. Love aider and am getting really great results with it, too.

My prompt simply rephrases the original user prompt in more technical terms and most of the time more on the spot for the request at hand.

  On 2/20/2025 at 9:07 PM, wbmnfktr said:

Need to try this in my rules!

Expand  

Please let me know how it is working for you. Since the prompt is just a few days old, there might be room for improvement.

  • Like 1
Link to comment
Share on other sites

Just gave it a super quick try, while giving guidance based on existing code and purpose.

image.thumb.png.db9e8b19400a620601a888e823a8db02.png

I used Deepseek V3, as it is giving me more issues than Claude or OpenAI on average, and ... even though it was a simple task it did a great job.

I need to test on the weekend with this as my current rules do things here as well - as they should. The rephrasing might just be the cherry on the top!

image.thumb.png.9b074e875d7f3586ed7290c1616a03ff.png

Claude Sonnet does a great job as always but faster.

Edited by wbmnfktr
added 2nd screenshot
  • Like 2
Link to comment
Share on other sites

  On 2/21/2025 at 3:21 AM, wbmnfktr said:

I need to test on the weekend with this as my current rules do things here as well - as they should. The rephrasing might just be the cherry on the top!

Expand  

Thanks for sharing this. It is much more comprehensive then mine:
 

# Project Guidelines

## Expert Knowledge
- ProcessWire CMS PHP development
- Tailwind / Flowbite markup and classes

## Core Technical Requirements
- PHP 8.3+
- ProcessWire CMS
- Tailwind CSS
- ES6+ JavaScript
- Webpack NPM/NPX

## Development Principles
- PSR-12 and SOLID compliance
- Type-safe PHP implementation
- Defensive programming with early returns
- DRY code organization
- Exception handling with WireException
- Logging via ProcessWire log() API

## ProcessWire Best Practices
- API-first development approach
- Markup regions for output processing
- Strategic hook usage in site/ready.php and site/modules/Site.module.php
- Input sanitization through PW API
- CSRF protection implementation
- Field/template migrations with RockMigrations
- use of functions API preferred. (`pages()` instead of `$pages` etc.)

## Frontend Standards
- Mobile-first Tailwind / Flowbite design
- WCAG 2.1 accessibility compliance
- Progressive enhancement strategy
- Modern browser support (last 2 versions)
- CSS custom properties

Will definitely take some inspirations from yours. Judging from parts of your rules, it almost seems like you might have read https://neoexogenesis.com/posts/rust-windsurf-transformation/ :-)  

  • Like 1
Link to comment
Share on other sites

I took a way easier route and grabbed the one from Yifan here:

At one point I gave this one a try but will keep only parts from it in the next iteration of my rules file.

Another thing I started doing is adding files with examples:

image.png.8e4214d85d616a2bafbce6a51fbbebf0.png

PHP or programming isn't the real issue with any of the current bigger LLMs, but ProcessWire itself as there isn't that much training data around - compared to WordPress, NextJS, Angular, ... whatever. So adding the examples makes it pretty easy and drops the amount of credits needed by a good margin.

  • Like 2
Link to comment
Share on other sites

Yeah, Yifan has some great videos on the subject.

And context is all that matters. Since LLMs nowadays support larger context windows, we can take advantage of that and pass quite a lot of docs for in-context learning.

I have a docs folder in each project, mostly with markdown files that contain detailed requirements for a feature, documentation for obscure libraries etc. I then pass those to the assistant via the @files feature when needed. This makes a big difference in code quality. Haven't used PW-specific examples but will definitely try that out. So thanks for sharing this idea!

I am using a Browser extension for downloading webpage content as markdown. It is quite fast and much cheaper than passing this in via @web to windsurf, which eats up lots of credits.

  • Like 2
Link to comment
Share on other sites

  On 2/22/2025 at 1:25 AM, gebeer said:

I am using a Browser extension for downloading webpage content as markdown. It is quite fast and much cheaper than passing this in via @web to windsurf, which eats up lots of credits.

Expand  

Want to share the name of the extension? 🙂 

  • Like 2
Link to comment
Share on other sites

I'll try to get my latest starter tested and fixed and will give you a link to the full repo with all examples and rules. Well... you basically have the link already but the version is an old one.

In regards to self-hosted docs and documents there is a new service which helps quite a bit - maybe worth putting processwire.com docs in there: parse.new

 

About the examples vs. docs part... even though we have huge context windows now, they fill up pretty quick. Using examples here nails the results almost 99% of the time. A list of hooks, some more complex examples like modules with configurations, and other module specific things speed it up as well. No scraping, outlining, and understanding as you can reference it.

The same with more complex selectors. I write them myself, put them in place, and whatever IDE and LLM can work with it. Plus: adding an example of the returned data structure makes writing views/components so super easy then. ☺️

 

Update: just found out parse.new is for sale - not sure how long this will be kept up - see: sale.parse.new

Edited by wbmnfktr
added update note for link
Link to comment
Share on other sites

  • 4 weeks later...

Just a little side note for those that like to play and experiment:

I was playing around with Grok 3 beta (via x.com/grok.com) and asked for specific Processwire rule files to use in Cursor/Windsurf.

And let's say: I'm quite impressed how good those rule files look.
🤯

I'm in the process of moving everything around and can't tell if they work as good as they look but Grok3 seems to be even better than Sonnet 3.5 and 3.7 for technical tasks like these.

  • Like 2
Link to comment
Share on other sites

  On 2/20/2025 at 3:40 AM, gebeer said:
User Prompt Rephrasing
Expand  

Based on the given Rephrasing prompt from @gebeer I played around and created this file/rule to import either globally or put it inside my rules file - could be a self-contained .mdc file as well in the future:

<!-- Global Rule: 1 -->
---
name: Global Rule #1
description: The very first global rule to apply the moment the user starts a task
globs: **/*.*
alwaysApply: true
---
# Before starting a task
1. analyze the given prompt and understand the intention
2. rephrase the user prompt in concise technical terms focusing on:
   - specific technical task scope
   - affected components/files
   - required functionality changes
3. preserve the users intent in the rephrased prompt
4. output the rephrased prompt and ask for confirmation with exact phrase "Act on the rephrased prompt? [y/n]"
5. IMPORTANT: after asking for confirmation, STOP and wait for explicit user response
6. proceed ONLY after receiving "y" confirmation, otherwise ask for clarification
7. when proceeding, act only on the rephrased prompt and start your output with "" and {name} of rule
<!-- /Global Rule: 1 -->

It's pretty much the same prompt but in a different structure with additional metadata. Cursor and Windsurf seem to love those little nuggets of metadata.

The frontmatter - indicated by leading/trailing triple dashes - allows to add metadata.
In this case you could output the rule name later on when needed like in #7., or give additional context whenever this rule should be applied.

So far it works pretty much as expected in my testcases - which where in bash scripting (.sh), Laravel (.blade.php), PHP, PW-specific, AstroJS (.astro, .tsx), NextJS (.tsx).

In total about 100 credits in Windsurf through various projects I had to do stuff in.

 

Example 1:

2025-03-23_03-06.thumb.png.aa71ccbc107e534cf754b06d3f6d7042.png

Built this maintenance script - based of 3-5 other scripts, with about 15 prompts to make it work on Ubuntu, Debian, Arch.
I'm pretty happy so far as the script also adds whatever I need - like NPM, PNPM, Flatpaks if not installed.

Could become a whole project on its own. Super stoked!

 

Example 2:

Asked for optimising my .gitconfig file and put suggestions into a new file.

2025-03-23_03-14.thumb.png.a6182c88fb3d99f3157e07212c6db253.png

The result... looks good, not sure about each and every detail based on personal preferences but... YEAH!

2025-03-23_03-15.thumb.png.425b6afaa3ba76be14fa27f161f4cdd4.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   1 member

×
×
  • Create New...