Skip to content

Commit 8f6bcae

Browse files
Update watcher
1 parent 9d1be7e commit 8f6bcae

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"review": " node --env-file=variables/local.env -r esbuild-register src/cli/review.ts",
3838
"tokens": " node --env-file=variables/local.env -r esbuild-register src/cli/tokens.ts",
3939
"util": " node --env-file=variables/local.env -r esbuild-register src/cli/util.ts",
40-
"watch": " node --env-file=variables/local.env -r esbuild-register src/cli/watch.ts",
40+
"watch": " node --env-file=variables/local.env -r esbuild-register src/cli/watch.ts -- runWatcher",
4141
"build": " tsgo --project ./tsconfig.native.json",
4242
"build:tsc": "tsc",
4343
"initTiktokenizer": "node --env-file=variables/local.env -r esbuild-register src/initTiktokenizer.ts",

src/cli/watch.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ function findRepoRoot(startFilePath: string): string {
2828
}
2929

3030
async 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

3545
export 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

Comments
 (0)