Skip to content

Commit ce91d29

Browse files
authored
Merge pull request #43 from ColdBox/copilot/fix-claude-file-overwrite-issue
Preserve user content in agent config files on `coldbox ai refresh`
2 parents 3be68af + 27f53d2 commit ce91d29

File tree

3 files changed

+139
-64
lines changed

3 files changed

+139
-64
lines changed

models/AgentRegistry.cfc

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ component singleton {
2828
"gemini" : "GEMINI.md",
2929
"opencode" : "AGENTS.md"
3030
}
31-
AGENT_OPTIONS = [
31+
// Demarcation markers that wrap the ColdBox CLI-managed section
32+
MANAGED_SECTION_START = "<!-- COLDBOX-CLI:START -->"
33+
MANAGED_SECTION_END = "<!-- COLDBOX-CLI:END -->"
34+
AGENT_OPTIONS = [
3235
{
3336
display : "Claude (Anthropic) - Recommended for general development",
3437
value : "claude"
@@ -126,6 +129,61 @@ component singleton {
126129
// Private Helpers
127130
// ========================================
128131

132+
/**
133+
* Merges newly generated managed content with any user-authored content from an existing file.
134+
*
135+
* The managed section is delimited by COLDBOX-CLI:START and COLDBOX-CLI:END HTML comment
136+
* markers. On refresh, only the content between those markers is replaced; everything after
137+
* the end marker (i.e. the user's custom documentation) is preserved unchanged.
138+
*
139+
* Behavior:
140+
* - File does not exist → return newContent as-is (first-time write).
141+
* - File exists but has no end marker → return newContent as-is (old format, no user section to preserve).
142+
* - File exists with end marker → replace managed section, keep user section intact.
143+
*
144+
* @filePath Absolute path to the existing agent config file (may not exist yet).
145+
* @newContent Freshly generated content that includes both START and END markers.
146+
*
147+
* @return Combined content with updated managed section and preserved user section.
148+
*/
149+
private string function mergeUserContent(
150+
required string filePath,
151+
required string newContent
152+
){
153+
var endMarker = static.MANAGED_SECTION_END
154+
155+
// Nothing to preserve — first-time write
156+
if ( !fileExists( filePath ) ) {
157+
return newContent
158+
}
159+
160+
var existingContent = fileRead( filePath )
161+
162+
// Find the end marker in the existing file
163+
var endPos = findNoCase( endMarker, existingContent )
164+
165+
// Old-format file (no markers) — write fresh content, no user section to preserve
166+
if ( !endPos ) {
167+
return newContent
168+
}
169+
170+
// Extract user content: everything that comes after the end marker
171+
var userStartPos = endPos + len( endMarker )
172+
var userContent = mid( existingContent, userStartPos, len( existingContent ) - userStartPos + 1 )
173+
174+
// Find the end marker position in the newly generated content
175+
var newEndPos = findNoCase( endMarker, newContent )
176+
if ( !newEndPos ) {
177+
// New template has no end marker — return new content plus preserved user section
178+
return newContent & userContent
179+
}
180+
181+
// Slice off the managed portion of the new content (up to and including the end marker)
182+
var managedContent = left( newContent, newEndPos + len( endMarker ) - 1 )
183+
184+
return managedContent & userContent
185+
}
186+
129187
/**
130188
* Configure a single agent
131189
*
@@ -156,13 +214,14 @@ component singleton {
156214
// For Claude, write the full content to AGENTS.md and make CLAUDE.md point to it
157215
if ( arguments.agent == "claude" ) {
158216
var agentsFilePath = getDirectoryFromPath( configPath ) & "AGENTS.md"
159-
fileWrite( agentsFilePath, content )
217+
var mergedContent = mergeUserContent( agentsFilePath, content )
218+
fileWrite( agentsFilePath, mergedContent )
160219
fileWrite( configPath, "@AGENTS.md" )
161220
return
162221
}
163222

164-
// Write agent config file
165-
fileWrite( configPath, content )
223+
// Write agent config file, preserving any user-authored content outside the managed section
224+
fileWrite( configPath, mergeUserContent( configPath, content ) )
166225
}
167226

168227
/**

templates/ai/agents/agent-flat-instructions.md

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<!-- COLDBOX-CLI:START -->
2+
<!-- ⚡ This section is managed by ColdBox CLI and will be refreshed on `coldbox ai refresh`. -->
3+
<!-- ⚠️ Do NOT edit content between COLDBOX-CLI:START and COLDBOX-CLI:END markers — changes will be overwritten. -->
4+
15
# |PROJECT_NAME| - AI Agent Instructions
26

37
This is a ColdBox HMVC application using the **flat template structure** where all application code lives in the webroot. Compatible with Adobe ColdFusion 2018+, Lucee 5.x+, and BoxLang 1.0+.
@@ -88,34 +92,6 @@ box testbox run
8892
/?fwreinit=true
8993
```
9094

91-
## Custom Application Details
92-
93-
<!-- Add project-specific information below -->
94-
95-
### Business Domain
96-
97-
<!-- Describe what this application does -->
98-
99-
### Key Services/Models
100-
101-
<!-- List important services and their responsibilities -->
102-
103-
### Authentication/Security
104-
105-
<!-- Describe authentication approach if applicable -->
106-
107-
### API Endpoints
108-
109-
<!-- Document REST API routes if applicable -->
110-
111-
### Deployment
112-
113-
<!-- Document deployment process -->
114-
115-
### Third-Party Integrations
116-
117-
<!-- List external services, APIs, or integrations -->
118-
11995
## AI Integration
12096

12197
This project includes AI-powered development assistance with guidelines, skills, and MCP documentation servers.
@@ -195,3 +171,35 @@ This project has access to the following Model Context Protocol (MCP) documentat
195171
- ColdBox Docs: https://coldbox.ortusbooks.com
196172
- TestBox: https://testbox.ortusbooks.com
197173
- WireBox: https://wirebox.ortusbooks.com
174+
175+
<!-- COLDBOX-CLI:END -->
176+
177+
<!-- ℹ️ YOUR PROJECT DOCUMENTATION — Add your custom details below. ColdBox CLI will NOT overwrite this section. -->
178+
179+
## Custom Application Details
180+
181+
<!-- Add project-specific information below -->
182+
183+
### Business Domain
184+
185+
<!-- Describe what this application does -->
186+
187+
### Key Services/Models
188+
189+
<!-- List important services and their responsibilities -->
190+
191+
### Authentication/Security
192+
193+
<!-- Describe authentication approach if applicable -->
194+
195+
### API Endpoints
196+
197+
<!-- Document REST API routes if applicable -->
198+
199+
### Deployment
200+
201+
<!-- Document deployment process -->
202+
203+
### Third-Party Integrations
204+
205+
<!-- List external services, APIs, or integrations -->

templates/ai/agents/agent-modern-instructions.md

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<!-- COLDBOX-CLI:START -->
2+
<!-- ⚡ This section is managed by ColdBox CLI and will be refreshed on `coldbox ai refresh`. -->
3+
<!-- ⚠️ Do NOT edit content between COLDBOX-CLI:START and COLDBOX-CLI:END markers — changes will be overwritten. -->
4+
15
# |PROJECT_NAME| - AI Agent Instructions
26

37
This is a ColdBox HMVC application using the **modern template structure** with application code separated from the public webroot. Compatible with Adobe ColdFusion 2018+, Lucee 5.x+, and BoxLang 1.0+.
@@ -125,38 +129,6 @@ docker-compose logs -f
125129
- **ORM:** |ORM_ENABLED| - Object-Relational Mapping via CBORM or Quick
126130
- **Migrations:** |MIGRATIONS_ENABLED| - Database version control with CommandBox Migrations
127131

128-
## Custom Application Details
129-
130-
<!-- Add project-specific information below -->
131-
132-
### Business Domain
133-
134-
<!-- Describe what this application does -->
135-
136-
### Key Services/Models
137-
138-
<!-- List important services and their responsibilities -->
139-
140-
### Authentication/Security
141-
142-
<!-- Describe authentication approach if applicable -->
143-
144-
### API Endpoints
145-
146-
<!-- Document REST API routes if applicable -->
147-
148-
### Database
149-
150-
<!-- Document database setup, migrations, seeders if applicable -->
151-
152-
### Deployment
153-
154-
<!-- Document deployment process -->
155-
156-
### Third-Party Integrations
157-
158-
<!-- List external services, APIs, or integrations -->
159-
160132
## AI Integration
161133

162134
This project includes AI-powered development assistance with guidelines, skills, and MCP documentation servers.
@@ -238,3 +210,39 @@ This project has access to the following Model Context Protocol (MCP) documentat
238210
- ColdBox Docs: https://coldbox.ortusbooks.com
239211
- TestBox: https://testbox.ortusbooks.com
240212
- WireBox: https://wirebox.ortusbooks.com
213+
214+
<!-- COLDBOX-CLI:END -->
215+
216+
<!-- ℹ️ YOUR PROJECT DOCUMENTATION — Add your custom details below. ColdBox CLI will NOT overwrite this section. -->
217+
218+
## Custom Application Details
219+
220+
<!-- Add project-specific information below -->
221+
222+
### Business Domain
223+
224+
<!-- Describe what this application does -->
225+
226+
### Key Services/Models
227+
228+
<!-- List important services and their responsibilities -->
229+
230+
### Authentication/Security
231+
232+
<!-- Describe authentication approach if applicable -->
233+
234+
### API Endpoints
235+
236+
<!-- Document REST API routes if applicable -->
237+
238+
### Database
239+
240+
<!-- Document database setup, migrations, seeders if applicable -->
241+
242+
### Deployment
243+
244+
<!-- Document deployment process -->
245+
246+
### Third-Party Integrations
247+
248+
<!-- List external services, APIs, or integrations -->

0 commit comments

Comments
 (0)