@denizokcu/haze 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -1
- package/README.md +66 -25
- package/SECURITY.md +22 -0
- package/bin/haze.js +102 -1
- package/dist/buildInfo.json +7 -0
- package/dist/cli/chat/TaskBar.d.ts +2 -1
- package/dist/cli/chat/TaskBar.js +7 -4
- package/dist/cli/chat/contextReport.js +2 -0
- package/dist/cli/chat/fileMentionSuggestions.js +3 -2
- package/dist/cli/chat/liveRegion.d.ts +25 -0
- package/dist/cli/chat/liveRegion.js +48 -0
- package/dist/cli/chat/messages.d.ts +33 -2
- package/dist/cli/chat/messages.js +216 -33
- package/dist/cli/chat/sessionLifecycle.d.ts +9 -0
- package/dist/cli/chat/sessionLifecycle.js +64 -14
- package/dist/cli/chat/sessionRecorder.js +75 -7
- package/dist/cli/chat/startupInfo.d.ts +0 -1
- package/dist/cli/chat/startupInfo.js +11 -15
- package/dist/cli/chat/wizardDispatch.js +111 -12
- package/dist/cli/commands/chat.d.ts +5 -0
- package/dist/cli/commands/chat.js +203 -101
- package/dist/cli/commands/commands.d.ts +2 -0
- package/dist/cli/commands/commands.js +10 -5
- package/dist/cli/commands/doctor.d.ts +19 -0
- package/dist/cli/commands/doctor.js +76 -0
- package/dist/cli/commands/formatters.js +2 -0
- package/dist/cli/commands/ndjsonSink.d.ts +23 -0
- package/dist/cli/commands/ndjsonSink.js +38 -0
- package/dist/cli/commands/providerWizard.d.ts +9 -2
- package/dist/cli/commands/providerWizard.js +37 -4
- package/dist/cli/commands/runCommand.d.ts +3 -0
- package/dist/cli/commands/runCommand.js +117 -11
- package/dist/cli/commands/streaming/abortCause.d.ts +24 -0
- package/dist/cli/commands/streaming/abortCause.js +16 -0
- package/dist/cli/commands/streaming/assistantText.js +9 -2
- package/dist/cli/commands/streaming/goalCheckpoint.d.ts +65 -0
- package/dist/cli/commands/streaming/goalCheckpoint.js +23 -0
- package/dist/cli/commands/streaming/goalSupervisor.d.ts +64 -0
- package/dist/cli/commands/streaming/goalSupervisor.js +127 -0
- package/dist/cli/commands/streaming/toolCallRecovery.d.ts +2 -0
- package/dist/cli/commands/streaming/toolCallRecovery.js +24 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.d.ts +29 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.js +50 -4
- package/dist/cli/commands/streaming/toolResultState.d.ts +6 -1
- package/dist/cli/commands/streaming/toolResultState.js +29 -7
- package/dist/cli/commands/streaming/turnOutcome.d.ts +17 -0
- package/dist/cli/commands/streaming/turnOutcome.js +27 -11
- package/dist/cli/commands/streaming.d.ts +70 -3
- package/dist/cli/commands/streaming.js +491 -77
- package/dist/cli/commands/wizardActions.d.ts +1 -0
- package/dist/cli/commands/wizardActions.js +1 -0
- package/dist/cli/commands/wizardPrompts.js +7 -1
- package/dist/cli/commands/wizardSuggestions.js +5 -3
- package/dist/cli/index.js +68 -22
- package/dist/cli/terminalTitle.js +1 -1
- package/dist/config/modelDiscovery.d.ts +51 -0
- package/dist/config/modelDiscovery.js +164 -1
- package/dist/config/privateStorage.js +28 -2
- package/dist/config/providerAuth.d.ts +14 -0
- package/dist/config/providerAuth.js +49 -0
- package/dist/config/providerPresets.d.ts +39 -7
- package/dist/config/providerPresets.js +371 -100
- package/dist/config/providers.js +2 -0
- package/dist/config/settings.d.ts +36 -0
- package/dist/config/settings.js +9 -0
- package/dist/core/agent/budgets.d.ts +10 -0
- package/dist/core/agent/budgets.js +10 -0
- package/dist/core/agent/compaction.d.ts +38 -0
- package/dist/core/agent/compaction.js +92 -17
- package/dist/core/agent/completionController.d.ts +230 -0
- package/dist/core/agent/completionController.js +287 -0
- package/dist/core/agent/contextBudget.d.ts +38 -0
- package/dist/core/agent/contextBudget.js +60 -7
- package/dist/core/agent/events.d.ts +74 -0
- package/dist/core/agent/reasoningPolicy.d.ts +31 -0
- package/dist/core/agent/reasoningPolicy.js +27 -0
- package/dist/core/agent/toolCapabilities.d.ts +27 -0
- package/dist/core/agent/toolCapabilities.js +58 -0
- package/dist/core/agent/toolExecutionBudget.d.ts +35 -0
- package/dist/core/agent/toolExecutionBudget.js +49 -0
- package/dist/core/agent/toolOutputStore.d.ts +8 -0
- package/dist/core/agent/toolOutputStore.js +10 -10
- package/dist/core/agent/toolResults.d.ts +12 -0
- package/dist/core/agent/toolResults.js +38 -0
- package/dist/core/agent/turnBudget.d.ts +35 -0
- package/dist/core/agent/turnBudget.js +27 -0
- package/dist/core/agent/turnPolicy.d.ts +11 -0
- package/dist/core/agent/turnPolicy.js +19 -0
- package/dist/core/agent/workState.d.ts +75 -0
- package/dist/core/agent/workState.js +120 -9
- package/dist/core/deadline.d.ts +33 -0
- package/dist/core/deadline.js +74 -0
- package/dist/core/goal/completionPolicy.d.ts +27 -0
- package/dist/core/goal/completionPolicy.js +37 -1
- package/dist/core/limits/byteBudgets.d.ts +2 -0
- package/dist/core/limits/byteBudgets.js +2 -0
- package/dist/core/limits/textBudgets.d.ts +2 -0
- package/dist/core/limits/textBudgets.js +2 -0
- package/dist/core/log/llmLog.js +7 -0
- package/dist/core/persistence/orderedFileWriter.d.ts +10 -0
- package/dist/core/persistence/orderedFileWriter.js +16 -2
- package/dist/core/process/backgroundRegistry.js +8 -7
- package/dist/core/process/runBoundedProcess.d.ts +8 -2
- package/dist/core/process/runBoundedProcess.js +15 -6
- package/dist/core/safety/missingExecutable.d.ts +42 -0
- package/dist/core/safety/missingExecutable.js +77 -0
- package/dist/core/session/sessionStore.d.ts +37 -0
- package/dist/core/session/sessionStore.js +215 -51
- package/dist/core/subagent/contracts.d.ts +4 -0
- package/dist/core/subagent/contracts.js +1 -0
- package/dist/core/subagent/subagentCoordinator.d.ts +4 -0
- package/dist/core/subagent/subagentCoordinator.js +32 -2
- package/dist/core/subagent/subagentRunner.js +17 -37
- package/dist/llm/client.d.ts +17 -1
- package/dist/llm/client.js +82 -14
- package/dist/llm/hazeTools.d.ts +554 -80
- package/dist/llm/hazeTools.js +103 -24
- package/dist/llm/lsp.d.ts +24 -4
- package/dist/llm/lsp.js +72 -11
- package/dist/llm/lspTools.d.ts +2366 -0
- package/dist/llm/lspTools.js +87 -79
- package/dist/llm/openaiCodex.d.ts +3 -0
- package/dist/llm/openaiCodex.js +47 -0
- package/dist/llm/openaiCodexOAuth.d.ts +54 -0
- package/dist/llm/openaiCodexOAuth.js +214 -0
- package/dist/llm/requestContext.d.ts +3 -0
- package/dist/llm/requestContext.js +7 -4
- package/dist/llm/systemPrompt.d.ts +11 -0
- package/dist/llm/systemPrompt.js +12 -6
- package/dist/llm/toolResultTypes.d.ts +8 -1
- package/dist/llm/tools/bashTool.d.ts +48 -16
- package/dist/llm/tools/bashTool.js +5 -0
- package/dist/llm/tools/editMatch.d.ts +14 -0
- package/dist/llm/tools/editMatch.js +72 -0
- package/dist/llm/tools/failures.d.ts +2 -0
- package/dist/llm/tools/failures.js +1 -0
- package/dist/llm/tools/fetchTool.d.ts +16 -0
- package/dist/llm/tools/fileToolShared.d.ts +4 -3
- package/dist/llm/tools/fileToolShared.js +16 -21
- package/dist/llm/tools/gitIgnore.d.ts +49 -0
- package/dist/llm/tools/gitIgnore.js +129 -0
- package/dist/llm/tools/readRecovery.d.ts +10 -0
- package/dist/llm/tools/readRecovery.js +135 -0
- package/dist/llm/tools/toolContext.d.ts +1 -1
- package/dist/llm/tools/toolContext.js +61 -32
- package/dist/llm/webFetch.d.ts +17 -0
- package/dist/llm/webFetch.js +64 -97
- package/dist/ui/codeHighlight.d.ts +4 -0
- package/dist/ui/codeHighlight.js +31 -0
- package/dist/ui/components/Header.d.ts +1 -1
- package/dist/ui/components/MarkdownText.d.ts +16 -2
- package/dist/ui/components/MarkdownText.js +244 -34
- package/dist/ui/components/TextInput.d.ts +8 -1
- package/dist/ui/components/TextInput.js +6 -2
- package/dist/ui/theme.d.ts +4 -3
- package/dist/ui/theme.js +5 -4
- package/dist/utils/buildInfo.d.ts +104 -0
- package/dist/utils/buildInfo.js +253 -0
- package/dist/utils/fs.d.ts +9 -0
- package/dist/utils/fs.js +87 -25
- package/dist/utils/openPath.d.ts +2 -2
- package/dist/utils/openPath.js +19 -6
- package/dist/utils/path.d.ts +2 -0
- package/dist/utils/path.js +5 -1
- package/dist/utils/utf8.d.ts +22 -0
- package/dist/utils/utf8.js +54 -0
- package/dist/utils/version.d.ts +9 -3
- package/dist/utils/version.js +28 -2
- package/package.json +24 -19
package/dist/llm/hazeTools.js
CHANGED
|
@@ -12,16 +12,30 @@ import { reductionMetrics } from '../core/toolOutput/reduction.js';
|
|
|
12
12
|
import { HazeToolError, structuredToolFailure } from './tools/failures.js';
|
|
13
13
|
import { compactGrepMatches, renderGrepMatches } from './tools/outputCap.js';
|
|
14
14
|
import { parseRipgrepJsonStream } from './tools/grepParse.js';
|
|
15
|
-
import { findEditRange, splitDiffLines, lineNumberAtOffset, replacementDiff } from './tools/editMatch.js';
|
|
15
|
+
import { boundedDiff, fileDiff, findEditRange, splitDiffLines, lineNumberAtOffset, renderToolDiff, replacementDiff } from './tools/editMatch.js';
|
|
16
16
|
import { runDedupedTool, discoverScopedContext, withScopedContext, hazeToolContextSchema } from './tools/toolContext.js';
|
|
17
17
|
import { prepareWorkspaceMutation, prepareWorkspaceRead, prepareWorkspaceWritePath } from './tools/workspaceFile.js';
|
|
18
18
|
import { fetchTool } from './tools/fetchTool.js';
|
|
19
19
|
import { bashTool } from './tools/bashTool.js';
|
|
20
20
|
import { processTool } from './tools/processTool.js';
|
|
21
|
-
import { DEFAULT_READ_LINES, INLINE_DIFF_LINE_LIMIT,
|
|
21
|
+
import { DEFAULT_READ_LINES, INLINE_DIFF_LINE_LIMIT, MAX_OUTPUT_CHARS, sourceOutlineEntries } from './tools/fileToolShared.js';
|
|
22
|
+
import { createIgnoreClassifier } from './tools/gitIgnore.js';
|
|
22
23
|
import { runRipgrepBounded } from './tools/grepRunner.js';
|
|
23
24
|
import { EXACT_MUTATION_BYTES } from '../core/limits/byteBudgets.js';
|
|
25
|
+
import { WRITE_FILE_CHUNK_BYTES } from '../core/agent/budgets.js';
|
|
24
26
|
import { readUtf8LinesPage, readUtf8Prefix } from '../core/io/boundedRead.js';
|
|
27
|
+
import { assertReadableTextFile, readFailureRecovery } from './tools/readRecovery.js';
|
|
28
|
+
function mutationDiffFields(filePath, fullDiff) {
|
|
29
|
+
const preview = boundedDiff(fullDiff, INLINE_DIFF_LINE_LIMIT);
|
|
30
|
+
const diffHandle = preview.truncated ? storeToolOutput(renderToolDiff(filePath, fullDiff)) : undefined;
|
|
31
|
+
return {
|
|
32
|
+
diffLineCount: fullDiff.length,
|
|
33
|
+
diff: preview.diff,
|
|
34
|
+
diffTruncated: preview.truncated,
|
|
35
|
+
diffOmittedLines: preview.omittedLines,
|
|
36
|
+
...(diffHandle ? { diffHandle } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
25
39
|
export const hazeTools = {
|
|
26
40
|
listFiles: tool({
|
|
27
41
|
description: 'List workspace files/directories with pagination. Prefer this to bash for discovery.',
|
|
@@ -39,13 +53,15 @@ export const hazeTools = {
|
|
|
39
53
|
const absolutePath = await prepareWorkspaceRead(dirPath, includeIgnored, context);
|
|
40
54
|
const entries = [];
|
|
41
55
|
let ignoredSkipped = 0;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
// One persistent classifier owns the whole listing so a frontier of
|
|
57
|
+
// candidates collapses to O(visited directories) Git subprocesses
|
|
58
|
+
// instead of one process per walked entry (RH-001). Page-2 cursor
|
|
59
|
+
// resumes do not re-check entries preceding the cursor.
|
|
60
|
+
const ignoreBatch = includeIgnored ? undefined : (relativePaths) => createIgnoreClassifier(workspaceRoot()).classify(relativePaths).then(ignored => {
|
|
61
|
+
ignoredSkipped += ignored.size;
|
|
62
|
+
return ignored;
|
|
63
|
+
});
|
|
64
|
+
const walked = await walkDir(absolutePath, { recursive, maxEntries: maxEntries + 1, cursor, ignoreBatch });
|
|
49
65
|
const page = walked.slice(0, maxEntries);
|
|
50
66
|
const hasMore = walked.length > maxEntries;
|
|
51
67
|
for (const entry of page) {
|
|
@@ -83,9 +99,13 @@ export const hazeTools = {
|
|
|
83
99
|
execute: async ({ path: filePath, offset, limit, mode, allowIgnored }, context) => runDedupedTool('readFile', { path: filePath, offset, limit, mode, allowIgnored }, context, async () => {
|
|
84
100
|
try {
|
|
85
101
|
const absolutePath = await prepareWorkspaceRead(filePath, allowIgnored, context);
|
|
102
|
+
await assertReadableTextFile(absolutePath, filePath);
|
|
86
103
|
const start = offset == null ? 0 : offset - 1;
|
|
87
104
|
const pageLimit = limit ?? DEFAULT_READ_LINES;
|
|
88
105
|
const page = await readUtf8LinesPage(absolutePath, start + 1, pageLimit);
|
|
106
|
+
if (offset != null && offset > page.totalLines) {
|
|
107
|
+
throw new HazeToolError(`offset ${offset} is beyond end of file (${page.totalLines} lines)`, 'invalid_line_range', { recoveryTool: 'readFile', recoveryInput: { path: filePath, offset: Math.max(1, page.totalLines) } });
|
|
108
|
+
}
|
|
89
109
|
const lines = page.lines;
|
|
90
110
|
const totalLines = page.totalLines;
|
|
91
111
|
const requestedEnd = Math.min(totalLines, start + pageLimit);
|
|
@@ -144,7 +164,8 @@ export const hazeTools = {
|
|
|
144
164
|
}, scopedContext);
|
|
145
165
|
}
|
|
146
166
|
catch (error) {
|
|
147
|
-
|
|
167
|
+
const recovery = await readFailureRecovery(filePath, error);
|
|
168
|
+
return structuredToolFailure('readFile', error, recovery.suggestedNextStep, filePath, { reasonCode: recovery.reasonCode, suggestedPaths: recovery.suggestedPaths });
|
|
148
169
|
}
|
|
149
170
|
}),
|
|
150
171
|
}),
|
|
@@ -265,10 +286,23 @@ export const hazeTools = {
|
|
|
265
286
|
lines.splice(startLine - 1, effectiveEndLine - startLine + 1, ...replacementLines);
|
|
266
287
|
}
|
|
267
288
|
const updated = lines.join('\n') + (hasTrailingNewline ? '\n' : '');
|
|
268
|
-
const
|
|
269
|
-
const
|
|
289
|
+
const replacement = replacementDiff(removedText, content, startLine, startLine, { before: beforeContext, after: afterContext });
|
|
290
|
+
const changed = updated !== original;
|
|
270
291
|
await fs.writeFile(absolutePath, updated, 'utf8');
|
|
271
|
-
return {
|
|
292
|
+
return {
|
|
293
|
+
ok: true,
|
|
294
|
+
path: filePath,
|
|
295
|
+
startLine,
|
|
296
|
+
endLine: effectiveEndLine,
|
|
297
|
+
requestedEndLine: endLine,
|
|
298
|
+
endLineClamped: effectiveEndLine !== endLine,
|
|
299
|
+
replacementLines: replacementLines.length,
|
|
300
|
+
appended: isAppend,
|
|
301
|
+
noChange: !changed,
|
|
302
|
+
addedLines: changed ? replacement.addedLines : 0,
|
|
303
|
+
removedLines: changed ? replacement.removedLines : 0,
|
|
304
|
+
...mutationDiffFields(filePath, changed ? replacement.diff : []),
|
|
305
|
+
};
|
|
272
306
|
}
|
|
273
307
|
catch (error) {
|
|
274
308
|
return structuredToolFailure('replaceLines', error, 'Read the file again for current line numbers, then retry replaceLines with a valid range.', filePath);
|
|
@@ -276,38 +310,74 @@ export const hazeTools = {
|
|
|
276
310
|
}),
|
|
277
311
|
}),
|
|
278
312
|
writeFile: tool({
|
|
279
|
-
description:
|
|
313
|
+
description: `Create, rewrite, or append a UTF-8 file in chunks of at most ${WRITE_FILE_CHUNK_BYTES} bytes. Existing files require explicit rewrite or append approval.`,
|
|
280
314
|
contextSchema: hazeToolContextSchema,
|
|
281
315
|
inputSchema: z.object({
|
|
282
316
|
path: z.string().describe('File path relative to the current workspace'),
|
|
283
|
-
content: z.string().describe(
|
|
284
|
-
overwriteExisting: z.boolean().default(false).describe('Approve
|
|
317
|
+
content: z.string().max(WRITE_FILE_CHUNK_BYTES).describe(`File content chunk, at most ${WRITE_FILE_CHUNK_BYTES} UTF-8 bytes`),
|
|
318
|
+
overwriteExisting: z.boolean().default(false).describe('Approve replacing an existing file with the first chunk of an intentional full rewrite'),
|
|
319
|
+
append: z.boolean().default(false).describe('Append this chunk to an existing file; use after creating or replacing the file with the first chunk'),
|
|
285
320
|
allowIgnored: z.boolean().default(false).describe('Write a .gitignored file only when needed'),
|
|
286
321
|
}),
|
|
287
|
-
execute: async ({ path: filePath, content, overwriteExisting, allowIgnored }, context) => runDedupedTool('writeFile', { path: filePath, content, overwriteExisting, allowIgnored }, context, async () => {
|
|
322
|
+
execute: async ({ path: filePath, content, overwriteExisting, append, allowIgnored }, context) => runDedupedTool('writeFile', { path: filePath, content, overwriteExisting, append, allowIgnored }, context, async () => {
|
|
288
323
|
try {
|
|
324
|
+
const contentBytes = Buffer.byteLength(content, 'utf8');
|
|
325
|
+
if (contentBytes > WRITE_FILE_CHUNK_BYTES) {
|
|
326
|
+
throw new HazeToolError(`Content is ${contentBytes} UTF-8 bytes; writeFile accepts at most ${WRITE_FILE_CHUNK_BYTES} bytes per call. Write the first chunk normally, then use append=true for later chunks.`, 'write_chunk_too_large');
|
|
327
|
+
}
|
|
328
|
+
if (append && overwriteExisting) {
|
|
329
|
+
throw new HazeToolError('append=true and overwriteExisting=true are mutually exclusive', 'conflicting_write_modes');
|
|
330
|
+
}
|
|
289
331
|
const { absolutePath, scopedStop, assertExistingInsideWorkspace, assertWritableInsideWorkspace } = await prepareWorkspaceWritePath('writeFile', filePath, allowIgnored, context);
|
|
290
332
|
if (scopedStop)
|
|
291
333
|
return scopedStop;
|
|
334
|
+
let targetExisted = false;
|
|
335
|
+
let previousContent = '';
|
|
336
|
+
let previousContentTruncated = false;
|
|
337
|
+
let previousContentOmittedBytes = 0;
|
|
292
338
|
try {
|
|
293
339
|
await fs.access(absolutePath);
|
|
340
|
+
targetExisted = true;
|
|
294
341
|
await assertExistingInsideWorkspace();
|
|
295
|
-
if (!overwriteExisting) {
|
|
296
|
-
throw new HazeToolError(`Refusing to overwrite existing file: ${filePath}. Use editFile/replaceLines for targeted edits, or set overwriteExisting=true for an intentional
|
|
342
|
+
if (!overwriteExisting && !append) {
|
|
343
|
+
throw new HazeToolError(`Refusing to overwrite existing file: ${filePath}. Use editFile/replaceLines for targeted edits, set append=true for a later chunk, or set overwriteExisting=true for an intentional rewrite.`, 'existing_file_requires_overwrite', { recoveryTool: 'readFile', recoveryInput: { path: filePath } });
|
|
297
344
|
}
|
|
345
|
+
const previous = await readUtf8Prefix(absolutePath, EXACT_MUTATION_BYTES);
|
|
346
|
+
previousContent = previous.content;
|
|
347
|
+
previousContentTruncated = previous.truncated;
|
|
348
|
+
previousContentOmittedBytes = Math.max(0, previous.totalBytes - Buffer.byteLength(previous.content, 'utf8'));
|
|
298
349
|
}
|
|
299
350
|
catch (error) {
|
|
300
351
|
const code = typeof error === 'object' && error != null && 'code' in error ? error.code : undefined;
|
|
301
352
|
if (code !== 'ENOENT')
|
|
302
353
|
throw error;
|
|
354
|
+
if (append)
|
|
355
|
+
throw new HazeToolError(`Cannot append to missing file: ${filePath}. Create its first chunk with append=false.`, 'append_target_missing');
|
|
303
356
|
await assertWritableInsideWorkspace();
|
|
304
357
|
}
|
|
305
358
|
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
|
|
306
|
-
|
|
307
|
-
|
|
359
|
+
const updated = append ? `${previousContent}${content}` : content;
|
|
360
|
+
const writeDiff = fileDiff(previousContent, updated);
|
|
361
|
+
if (append)
|
|
362
|
+
await fs.appendFile(absolutePath, content, 'utf8');
|
|
363
|
+
else
|
|
364
|
+
await fs.writeFile(absolutePath, content, 'utf8');
|
|
365
|
+
return {
|
|
366
|
+
ok: true,
|
|
367
|
+
path: filePath,
|
|
368
|
+
bytes: contentBytes,
|
|
369
|
+
overwritten: overwriteExisting,
|
|
370
|
+
appended: append,
|
|
371
|
+
noChange: targetExisted && !previousContentTruncated && previousContent === updated,
|
|
372
|
+
addedLines: writeDiff.addedLines,
|
|
373
|
+
removedLines: writeDiff.removedLines,
|
|
374
|
+
diffComplete: !previousContentTruncated,
|
|
375
|
+
...(previousContentTruncated ? { previousContentOmittedBytes } : {}),
|
|
376
|
+
...mutationDiffFields(filePath, writeDiff.diff),
|
|
377
|
+
};
|
|
308
378
|
}
|
|
309
379
|
catch (error) {
|
|
310
|
-
return structuredToolFailure('writeFile', error,
|
|
380
|
+
return structuredToolFailure('writeFile', error, `Keep each content chunk at or below ${WRITE_FILE_CHUNK_BYTES} UTF-8 bytes. Create or replace the first chunk, then continue with append=true.`, filePath);
|
|
311
381
|
}
|
|
312
382
|
}),
|
|
313
383
|
}),
|
|
@@ -369,9 +439,18 @@ export const hazeTools = {
|
|
|
369
439
|
removedLines += rangeDiff.removedLines;
|
|
370
440
|
lineDelta += rangeDiff.addedLines - rangeDiff.removedLines;
|
|
371
441
|
}
|
|
372
|
-
const
|
|
442
|
+
const changed = updated !== original;
|
|
373
443
|
await fs.writeFile(absolutePath, updated, 'utf8');
|
|
374
|
-
return {
|
|
444
|
+
return {
|
|
445
|
+
ok: true,
|
|
446
|
+
path: filePath,
|
|
447
|
+
edits: edits.length,
|
|
448
|
+
approximateMatches: ranges.filter(range => range.approximate).length,
|
|
449
|
+
noChange: !changed,
|
|
450
|
+
addedLines: changed ? addedLines : 0,
|
|
451
|
+
removedLines: changed ? removedLines : 0,
|
|
452
|
+
...mutationDiffFields(filePath, changed ? diff : []),
|
|
453
|
+
};
|
|
375
454
|
}
|
|
376
455
|
catch (error) {
|
|
377
456
|
return structuredToolFailure('editFile', error, 'Read the file again, then retry with exact current text or use replaceLines with the latest line numbers.', filePath);
|
package/dist/llm/lsp.d.ts
CHANGED
|
@@ -67,6 +67,8 @@ export declare class StdioLspClient {
|
|
|
67
67
|
static start(server: HazeLspServer): StdioLspClient;
|
|
68
68
|
private rejectAll;
|
|
69
69
|
private terminate;
|
|
70
|
+
/** Whether this client has begun (or completed) teardown and must not be reused. */
|
|
71
|
+
get terminated(): boolean;
|
|
70
72
|
private onData;
|
|
71
73
|
private send;
|
|
72
74
|
request(method: string, params?: Json, timeoutMs?: number): Promise<unknown>;
|
|
@@ -75,14 +77,32 @@ export declare class StdioLspClient {
|
|
|
75
77
|
openDocument(absolutePath: string): Promise<void>;
|
|
76
78
|
close(): Promise<void>;
|
|
77
79
|
}
|
|
78
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Turn-scoped pool of reused LSP clients (RH-009). Autonomous code navigation
|
|
82
|
+
* issues many symbols/definition/references calls per turn; reusing one
|
|
83
|
+
* initialized server and its opened documents avoids repeating expensive
|
|
84
|
+
* startup and indexing on every call. Clients are keyed by server name; a
|
|
85
|
+
* crashed/terminated client is evicted so the next call restarts it.
|
|
86
|
+
*/
|
|
87
|
+
export declare class LspPool {
|
|
88
|
+
private readonly clients;
|
|
89
|
+
private readonly openedDocuments;
|
|
90
|
+
private closed;
|
|
91
|
+
/** Get (or lazily start + initialize) the reusable client for a server. */
|
|
92
|
+
getClient(server: HazeLspServer): Promise<StdioLspClient>;
|
|
93
|
+
/** Open a document once per client; subsequent calls for the same URI are no-ops. */
|
|
94
|
+
ensureOpen(server: HazeLspServer, client: StdioLspClient, absolutePath: string): Promise<void>;
|
|
95
|
+
/** Bounded teardown of every pooled client. Safe to call once. */
|
|
96
|
+
close(): Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
export declare function lspDocumentSymbols(server: HazeLspServer, filePath: string, limit: number, pool?: LspPool): Promise<{
|
|
79
99
|
name: string;
|
|
80
100
|
kind?: number;
|
|
81
101
|
path: string;
|
|
82
102
|
range?: ReturnType<typeof asRange>;
|
|
83
103
|
selectionRange?: ReturnType<typeof asRange>;
|
|
84
104
|
}[]>;
|
|
85
|
-
export declare function lspDefinition(server: HazeLspServer, filePath: string, line: number, character: number, limit: number): Promise<{
|
|
105
|
+
export declare function lspDefinition(server: HazeLspServer, filePath: string, line: number, character: number, limit: number, pool?: LspPool): Promise<{
|
|
86
106
|
external?: boolean | undefined;
|
|
87
107
|
path: string;
|
|
88
108
|
range: {
|
|
@@ -96,7 +116,7 @@ export declare function lspDefinition(server: HazeLspServer, filePath: string, l
|
|
|
96
116
|
};
|
|
97
117
|
};
|
|
98
118
|
}[]>;
|
|
99
|
-
export declare function lspReferences(server: HazeLspServer, filePath: string, line: number, character: number, limit: number): Promise<{
|
|
119
|
+
export declare function lspReferences(server: HazeLspServer, filePath: string, line: number, character: number, limit: number, pool?: LspPool): Promise<{
|
|
100
120
|
external?: boolean | undefined;
|
|
101
121
|
path: string;
|
|
102
122
|
range: {
|
|
@@ -110,7 +130,7 @@ export declare function lspReferences(server: HazeLspServer, filePath: string, l
|
|
|
110
130
|
};
|
|
111
131
|
};
|
|
112
132
|
}[]>;
|
|
113
|
-
export declare function lspWorkspaceSymbols(server: HazeLspServer, query: string, limit: number): Promise<{
|
|
133
|
+
export declare function lspWorkspaceSymbols(server: HazeLspServer, query: string, limit: number, pool?: LspPool): Promise<{
|
|
114
134
|
external?: boolean | undefined;
|
|
115
135
|
path: string;
|
|
116
136
|
range: {
|
package/dist/llm/lsp.js
CHANGED
|
@@ -159,6 +159,10 @@ export class StdioLspClient {
|
|
|
159
159
|
this.child.stdout.destroy?.();
|
|
160
160
|
this.child.stderr.destroy?.();
|
|
161
161
|
}
|
|
162
|
+
/** Whether this client has begun (or completed) teardown and must not be reused. */
|
|
163
|
+
get terminated() {
|
|
164
|
+
return this.terminating;
|
|
165
|
+
}
|
|
162
166
|
onData(chunk) {
|
|
163
167
|
if (this.buffer.length + chunk.length > LSP_BUFFER_BYTES)
|
|
164
168
|
throw new LspError(`LSP receive buffer exceeds ${LSP_BUFFER_BYTES} bytes.`);
|
|
@@ -252,8 +256,60 @@ export class StdioLspClient {
|
|
|
252
256
|
}
|
|
253
257
|
}
|
|
254
258
|
}
|
|
255
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Turn-scoped pool of reused LSP clients (RH-009). Autonomous code navigation
|
|
261
|
+
* issues many symbols/definition/references calls per turn; reusing one
|
|
262
|
+
* initialized server and its opened documents avoids repeating expensive
|
|
263
|
+
* startup and indexing on every call. Clients are keyed by server name; a
|
|
264
|
+
* crashed/terminated client is evicted so the next call restarts it.
|
|
265
|
+
*/
|
|
266
|
+
export class LspPool {
|
|
267
|
+
clients = new Map();
|
|
268
|
+
openedDocuments = new Map();
|
|
269
|
+
closed = false;
|
|
270
|
+
/** Get (or lazily start + initialize) the reusable client for a server. */
|
|
271
|
+
async getClient(server) {
|
|
272
|
+
const existing = this.clients.get(server.name);
|
|
273
|
+
if (existing && !existing.terminated)
|
|
274
|
+
return existing;
|
|
275
|
+
const client = StdioLspClient.start(server);
|
|
276
|
+
await client.initialize();
|
|
277
|
+
if (this.closed) {
|
|
278
|
+
await client.close();
|
|
279
|
+
throw new LspError('LSP pool closed during initialization.');
|
|
280
|
+
}
|
|
281
|
+
this.clients.set(server.name, client);
|
|
282
|
+
this.openedDocuments.set(server.name, new Set());
|
|
283
|
+
return client;
|
|
284
|
+
}
|
|
285
|
+
/** Open a document once per client; subsequent calls for the same URI are no-ops. */
|
|
286
|
+
async ensureOpen(server, client, absolutePath) {
|
|
287
|
+
const uri = toUri(absolutePath);
|
|
288
|
+
const opened = this.openedDocuments.get(server.name);
|
|
289
|
+
if (opened && opened.has(uri))
|
|
290
|
+
return;
|
|
291
|
+
await client.openDocument(absolutePath);
|
|
292
|
+
opened?.add(uri);
|
|
293
|
+
}
|
|
294
|
+
/** Bounded teardown of every pooled client. Safe to call once. */
|
|
295
|
+
async close() {
|
|
296
|
+
if (this.closed)
|
|
297
|
+
return;
|
|
298
|
+
this.closed = true;
|
|
299
|
+
const clients = [...this.clients.values()];
|
|
300
|
+
this.clients.clear();
|
|
301
|
+
this.openedDocuments.clear();
|
|
302
|
+
await Promise.all(clients.map(client => client.close().catch(() => undefined)));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
async function withLspClient(server, filePath, pool, fn) {
|
|
256
306
|
const absolutePath = await prepareWorkspaceRead(filePath, false);
|
|
307
|
+
if (pool) {
|
|
308
|
+
const client = await pool.getClient(server);
|
|
309
|
+
await pool.ensureOpen(server, client, absolutePath);
|
|
310
|
+
return fn(client, absolutePath);
|
|
311
|
+
}
|
|
312
|
+
// Single-call fallback (no pool): full server lifecycle per invocation.
|
|
257
313
|
const client = StdioLspClient.start(server);
|
|
258
314
|
try {
|
|
259
315
|
await client.initialize();
|
|
@@ -264,31 +320,29 @@ async function withLsp(server, filePath, fn) {
|
|
|
264
320
|
await client.close();
|
|
265
321
|
}
|
|
266
322
|
}
|
|
267
|
-
export async function lspDocumentSymbols(server, filePath, limit) {
|
|
268
|
-
return await
|
|
323
|
+
export async function lspDocumentSymbols(server, filePath, limit, pool) {
|
|
324
|
+
return await withLspClient(server, filePath, pool, async (client, absolutePath) => {
|
|
269
325
|
const result = await client.request('textDocument/documentSymbol', { textDocument: { uri: toUri(absolutePath) } });
|
|
270
326
|
const symbols = Array.isArray(result) ? result : [];
|
|
271
327
|
return flattenSymbols(symbols, workspaceRelativePath(absolutePath), limit);
|
|
272
328
|
});
|
|
273
329
|
}
|
|
274
|
-
export async function lspDefinition(server, filePath, line, character, limit) {
|
|
275
|
-
return await
|
|
330
|
+
export async function lspDefinition(server, filePath, line, character, limit, pool) {
|
|
331
|
+
return await withLspClient(server, filePath, pool, async (client, absolutePath) => {
|
|
276
332
|
const result = await client.request('textDocument/definition', { textDocument: { uri: toUri(absolutePath) }, position: { line: line - 1, character: character - 1 } });
|
|
277
333
|
const values = Array.isArray(result) ? result : result ? [result] : [];
|
|
278
334
|
return (await Promise.all(values.map(locationToWorkspaceResult))).filter(result => result != null).slice(0, limit);
|
|
279
335
|
});
|
|
280
336
|
}
|
|
281
|
-
export async function lspReferences(server, filePath, line, character, limit) {
|
|
282
|
-
return await
|
|
337
|
+
export async function lspReferences(server, filePath, line, character, limit, pool) {
|
|
338
|
+
return await withLspClient(server, filePath, pool, async (client, absolutePath) => {
|
|
283
339
|
const result = await client.request('textDocument/references', { textDocument: { uri: toUri(absolutePath) }, position: { line: line - 1, character: character - 1 }, context: { includeDeclaration: true } });
|
|
284
340
|
const values = Array.isArray(result) ? result : [];
|
|
285
341
|
return (await Promise.all(values.map(locationToWorkspaceResult))).filter(result => result != null).slice(0, limit);
|
|
286
342
|
});
|
|
287
343
|
}
|
|
288
|
-
export async function lspWorkspaceSymbols(server, query, limit) {
|
|
289
|
-
const
|
|
290
|
-
try {
|
|
291
|
-
await client.initialize();
|
|
344
|
+
export async function lspWorkspaceSymbols(server, query, limit, pool) {
|
|
345
|
+
const run = async (client) => {
|
|
292
346
|
const result = await client.request('workspace/symbol', { query });
|
|
293
347
|
const values = Array.isArray(result) ? result : [];
|
|
294
348
|
const locations = await Promise.all(values.map(async (value) => {
|
|
@@ -300,6 +354,13 @@ export async function lspWorkspaceSymbols(server, query, limit) {
|
|
|
300
354
|
return [{ name: value.name, kind: typeof value.kind === 'number' ? value.kind : undefined, ...location }];
|
|
301
355
|
}));
|
|
302
356
|
return locations.flat().slice(0, limit);
|
|
357
|
+
};
|
|
358
|
+
if (pool)
|
|
359
|
+
return run(await pool.getClient(server));
|
|
360
|
+
const client = StdioLspClient.start(server);
|
|
361
|
+
try {
|
|
362
|
+
await client.initialize();
|
|
363
|
+
return await run(client);
|
|
303
364
|
}
|
|
304
365
|
finally {
|
|
305
366
|
await client.close();
|