@@ -28,8 +28,18 @@ function findRepoRoot(startFilePath: string): string {
2828}
2929
3030async function main ( ) {
31- initInMemoryApplicationContext ( ) ;
32- startWatcher ( ) ;
31+ // timeout avoids ReferenceError: Cannot access 'RateLimiter' before initialization
32+ setTimeout ( ( ) => {
33+ initInMemoryApplicationContext ( ) ;
34+ startWatcher ( ) ;
35+ } , 100 ) ;
36+ }
37+
38+ // Check for the runWatcher flag so tests don't start the watcher
39+ // package.json script
40+ // "watch": " node --env-file=variables/local.env -r esbuild-register src/cli/watch.ts -- runWatcher",
41+ if ( process . argv . includes ( 'runWatcher' ) ) {
42+ main ( ) . catch ( console . error ) ;
3343}
3444
3545export function extractInstructionBlock ( fileContents : string ) : string | null {
@@ -112,28 +122,30 @@ export function startWatcher(): void {
112122 return ;
113123 }
114124
115- // Check rate limit
116- if ( ! rateLimiter . canProcess ( ) ) {
117- const remainingMs = rateLimiter . getRemainingTime ( ) ;
118- const remainingSecs = Math . ceil ( remainingMs / 1000 ) ;
119- console . warn ( '⚠️ Rate limit exceeded! Too many edits in 10 seconds. Check for edit loops. Disabling watcher and exiting.' ) ;
120- watcher . close ( ) ;
121- beep ( ) ;
122- beep ( ) ;
123- beep ( ) ;
124- beep ( ) ;
125- process . exit ( ) ;
126- }
127-
128- // Mark file as being processed
129- processingFiles . add ( filePath ) ;
130125 console . log ( `Checking ${ filePath } ` ) ;
131126 try {
132127 // const repoRoot = findRepoRoot(filePath);
133128 const fileContents = await fs . promises . readFile ( filePath , 'utf-8' ) ;
134129
135130 const instructions = extractInstructionBlock ( fileContents ) ;
136131 if ( ! instructions ) return ;
132+
133+ // Check rate limit
134+ if ( ! rateLimiter . canProcess ( ) ) {
135+ const remainingMs = rateLimiter . getRemainingTime ( ) ;
136+ const remainingSecs = Math . ceil ( remainingMs / 1000 ) ;
137+ console . warn ( '⚠️ Rate limit exceeded! Too many edits in 10 seconds. Check for edit loops. Disabling watcher and exiting.' ) ;
138+ watcher . close ( ) ;
139+ beep ( ) ;
140+ beep ( ) ;
141+ beep ( ) ;
142+ beep ( ) ;
143+ process . exit ( ) ;
144+ }
145+
146+ // Mark file as being processed
147+ processingFiles . add ( filePath ) ;
148+
137149 console . log ( `Extracted instructions: ${ instructions } ` ) ;
138150 beep ( ) ;
139151 let start = Date . now ( ) ;
@@ -226,7 +238,9 @@ async function generateCodeEdits(fileContents: string, instructions: string): Pr
226238 'Return only the edit snippet.' ,
227239 ] . join ( '\n' ) ;
228240
229- let edits = await cerebrasQwen3_Coder ( ) . generateText ( prompt , { temperature : 0.1 , topP : 0 , id : 'morph-edit' } ) ;
241+ const cerebrasCoder = cerebrasQwen3_Coder ( ) ;
242+ const llm = cerebrasCoder . isConfigured ( ) ? cerebrasCoder : llms ( ) . medium ;
243+ let edits = await llm . generateText ( prompt , { temperature : 0.1 , topP : 0 , id : 'morph-edit' } ) ;
230244
231245 // Strip code fences if the model accidentally added them
232246 const fenced = edits . match ( / ` ` ` (?: \w + ) ? \n ( [ \s \S ] * ?) ` ` ` / ) ;
0 commit comments