dsh-awesome-optimizer 2.1.0 → 3.3.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.
Files changed (42) hide show
  1. package/README.md +29 -35
  2. package/README.zh.md +29 -35
  3. package/cache-diagnostics.md +19 -0
  4. package/cordis.patch.yml +14 -2
  5. package/outline-benchmark.md +37 -0
  6. package/outline-benchmark.windows.json +184 -0
  7. package/package.json +15 -2
  8. package/scripts/benchmark-outline.mjs +100 -0
  9. package/scripts/benchmark.mjs +33 -26
  10. package/scripts/typecheck.mjs +37 -18
  11. package/src/ast-outline-config.yml +1 -0
  12. package/src/ast-outline.js +143 -0
  13. package/src/cache-diagnostics.js +218 -0
  14. package/src/code-lexical.js +131 -0
  15. package/src/code-outline.js +171 -0
  16. package/src/composite-tools.d.ts +36 -8
  17. package/src/composite-tools.js +538 -194
  18. package/src/convergence.js +3 -3
  19. package/src/custom-prompt.d.ts +7 -1
  20. package/src/custom-prompt.js +53 -18
  21. package/src/edit-recovery.d.ts +59 -0
  22. package/src/edit-recovery.js +524 -0
  23. package/src/failure-recovery.js +1 -1
  24. package/src/file-mutation-policy.js +0 -8
  25. package/src/find-read-output.js +175 -0
  26. package/src/find-read-ranking.js +63 -0
  27. package/src/index.d.ts +34 -2
  28. package/src/index.js +29 -5
  29. package/src/outline-cache.js +36 -0
  30. package/src/persistent-eval/daemon-pool.js +3 -2
  31. package/src/persistent-eval/index.d.ts +35 -8
  32. package/src/persistent-eval/index.js +51 -16
  33. package/src/persistent-eval/stdio-runner.js +209 -30
  34. package/src/persistent-eval/workers/node_worker.mjs +42 -31
  35. package/src/persistent-eval/workers/py_worker.py +47 -24
  36. package/src/prompt-linter.js +43 -26
  37. package/src/read-guard.d.ts +80 -0
  38. package/src/read-guard.js +336 -0
  39. package/src/runtime-policy.d.ts +1 -0
  40. package/src/runtime-policy.js +35 -15
  41. package/src/telemetry.d.ts +4 -0
  42. package/src/telemetry.js +27 -8
@@ -0,0 +1,131 @@
1
+ /** Mask literals/comments without changing UTF-16 offsets or physical line numbers. */
2
+ export function maskCode(source, language) {
3
+ const parts = [];
4
+ let copied = 0;
5
+ let index = 0;
6
+ while (index < source.length) {
7
+ const end = literalEnd(source, index, language);
8
+ if (end > index) {
9
+ parts.push(source.slice(copied, index), source.slice(index, end).replace(/[^\r\n]/g, ' '));
10
+ index = end;
11
+ copied = end;
12
+ } else index++;
13
+ }
14
+ parts.push(source.slice(copied));
15
+ return parts.join('');
16
+ }
17
+
18
+ function quotedEnd(source, start, quote, triple = false, multiline = false) {
19
+ const delimiter = triple ? quote.repeat(3) : quote;
20
+ let index = start + delimiter.length;
21
+ while (index < source.length) {
22
+ if (source[index] === '\\') { index += 2; continue; }
23
+ if (source.startsWith(delimiter, index)) return index + delimiter.length;
24
+ if (!triple && !multiline && quote !== '`' && source[index] === '\n') return index;
25
+ index++;
26
+ }
27
+ return source.length;
28
+ }
29
+
30
+ function commentEnd(source, start, nested) {
31
+ let depth = 1;
32
+ let index = start + 2;
33
+ while (index < source.length) {
34
+ if (nested && source.startsWith('/*', index)) { depth++; index += 2; }
35
+ else if (source.startsWith('*/', index)) {
36
+ index += 2;
37
+ if (--depth === 0) return index;
38
+ } else index++;
39
+ }
40
+ return source.length;
41
+ }
42
+
43
+ function regexEnd(source, start) {
44
+ // Conservative expression-start recognition; division must remain visible.
45
+ const before = source.slice(Math.max(0, start - 80), start).trimEnd();
46
+ if (before && !/[=(:,\[!&|?{};>]$/.test(before) && !/\b(?:return|throw|yield|case|typeof|void|delete)$/.test(before)) return start;
47
+ let inClass = false;
48
+ for (let index = start + 1; index < source.length && source[index] !== '\n'; index++) {
49
+ const char = source[index];
50
+ if (char === '\\') { index++; continue; }
51
+ if (char === '[') inClass = true;
52
+ else if (char === ']') inClass = false;
53
+ else if (char === '/' && !inClass) {
54
+ index++;
55
+ while (/[a-z]/i.test(source[index] ?? '')) index++;
56
+ return index;
57
+ }
58
+ }
59
+ return start;
60
+ }
61
+
62
+ function templateEnd(source, start, depth = 0) {
63
+ if (depth > 64) return source.length;
64
+ let index = start + 1;
65
+ while (index < source.length) {
66
+ if (source[index] === '\\') { index += 2; continue; }
67
+ if (source[index] === '`') return index + 1;
68
+ if (source.startsWith('${', index)) {
69
+ index += 2;
70
+ let braces = 1;
71
+ while (index < source.length && braces > 0) {
72
+ const end = literalEnd(source, index, 'js', depth + 1);
73
+ if (end > index) { index = end; continue; }
74
+ if (source[index] === '{') braces++;
75
+ else if (source[index] === '}') braces--;
76
+ index++;
77
+ }
78
+ } else index++;
79
+ }
80
+ return source.length;
81
+ }
82
+
83
+ function literalEnd(source, index, language, depth = 0) {
84
+ const char = source[index];
85
+ if (language === 'py' && char === '#') {
86
+ const end = source.indexOf('\n', index);
87
+ return end < 0 ? source.length : end;
88
+ }
89
+ if (language !== 'py' && source.startsWith('//', index)) {
90
+ const end = source.indexOf('\n', index);
91
+ return end < 0 ? source.length : end;
92
+ }
93
+ if (language !== 'py' && source.startsWith('/*', index)) return commentEnd(source, index, language === 'rust');
94
+ if (language === 'rust') {
95
+ if ((char === 'r' || char === 'b') && !/[\w]/.test(source[index - 1] ?? '')) {
96
+ const raw = source.slice(index, index + 260).match(/^(?:br|r)(#{0,255})"/);
97
+ if (raw) {
98
+ const delimiter = `"${raw[1]}`;
99
+ const end = source.indexOf(delimiter, index + raw[0].length);
100
+ return end < 0 ? source.length : end + delimiter.length;
101
+ }
102
+ }
103
+ // A lifetime such as 'a is not a character literal.
104
+ if (char === "'") {
105
+ const literal = source.slice(index, index + 20).match(/^'(?:\\(?:u\{[\da-fA-F]+\}|x[\da-fA-F]{2}|[^\r\n])|[^'\\\r\n])'/u);
106
+ return literal ? index + literal[0].length : index;
107
+ }
108
+ }
109
+ if (char === '"' || char === "'") {
110
+ return quotedEnd(source, index, char, language === 'py' && source.startsWith(char.repeat(3), index), language === 'rust');
111
+ }
112
+ if (char === '`' && language === 'js') return templateEnd(source, index, depth);
113
+ if (char === '`' && language === 'go') {
114
+ const end = source.indexOf('`', index + 1);
115
+ return end < 0 ? source.length : end + 1;
116
+ }
117
+ if (char === '/' && language === 'js') return regexEnd(source, index);
118
+ return index;
119
+ }
120
+
121
+ /** Read a bounded declaration header; nested default values and types stay balanced. */
122
+ export function parameterEnd(masked, open) {
123
+ if (open < 0) return -1;
124
+ let depth = 0;
125
+ const end = Math.min(masked.length, open + 8192);
126
+ for (let index = open; index < end; index++) {
127
+ if (masked[index] === '(') depth++;
128
+ else if (masked[index] === ')' && --depth === 0) return index;
129
+ }
130
+ return -1;
131
+ }
@@ -0,0 +1,171 @@
1
+ import path from 'node:path';
2
+ import { maskCode, parameterEnd } from './code-lexical.js';
3
+
4
+ const JS_TS_EXTS = new Set(['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx', '.mts', '.cts']);
5
+ const PY_EXTS = new Set(['.py', '.pyw']);
6
+ const GO_EXTS = new Set(['.go']);
7
+ const RS_EXTS = new Set(['.rs']);
8
+
9
+ const CONTROL_WORDS = new Set([
10
+ 'if', 'for', 'while', 'switch', 'catch', 'with', 'return', 'throw', 'await', 'yield', 'new', 'typeof', 'delete'
11
+ ]);
12
+
13
+ function truncateSig(sig, maxLen = 96) {
14
+ const clean = sig.replace(/\s+/g, ' ').trim();
15
+ return clean.length <= maxLen ? clean : `${clean.slice(0, maxLen - 3)}...`;
16
+ }
17
+
18
+ const IDENTIFIER = '[\\p{ID_Start}_$][\\p{ID_Continue}$]*';
19
+ const jsFunction = new RegExp(`^(export\\s+(?:default\\s+)?)?(?:declare\\s+)?(async\\s+)?function\\s*\\*?\\s*(${IDENTIFIER})`, 'u');
20
+ const jsType = new RegExp(`^(export\\s+(?:default\\s+)?)?(?:declare\\s+)?(?:abstract\\s+)?(class|interface|type|enum)\\s+(${IDENTIFIER})`, 'u');
21
+ const jsVariable = new RegExp(`^(export\\s+)?(?:const|let|var)\\s+(${IDENTIFIER})(?:\\s*:[^=;]+)?\\s*=\\s*(async\\s+)?`, 'u');
22
+ const jsMethod = new RegExp(`^(?:(?:public|private|protected|static|readonly|override|abstract|get|set|async)\\s+)*\\*?\\s*(#?${IDENTIFIER})\\s*(?:<[^;{}]*>)?\\s*\\(`, 'u');
23
+
24
+ function scanDeclarations(content, language) {
25
+ const masked = maskCode(content, language);
26
+ const entries = [];
27
+ let offset = 0;
28
+ let depth = 0;
29
+ let skipUntil = -1;
30
+ for (const [index, line] of masked.split('\n').entries()) {
31
+ const indent = line.length - line.trimStart().length;
32
+ const start = offset + indent;
33
+ const header = masked.slice(start, start + 8192);
34
+ const push = (text, nested = language === 'py' ? indent > 0 : depth > 0) => {
35
+ entries.push({ line: index + 1, text: truncateSig(text), nested });
36
+ };
37
+ const params = (prefix, from = 0) => {
38
+ let cursor = from;
39
+ while (/\s/.test(header[cursor] ?? '') && cursor < header.length) cursor++;
40
+ if (header[cursor] === '<') {
41
+ let depth = 1;
42
+ while (++cursor < header.length && depth > 0) {
43
+ if (header[cursor] === '<') depth++;
44
+ else if (header[cursor] === '>' && header[cursor - 1] !== '=') depth--;
45
+ }
46
+ }
47
+ while (/\s/.test(header[cursor] ?? '') && cursor < header.length) cursor++;
48
+ const open = header[cursor] === '(' ? cursor : -1;
49
+ const end = parameterEnd(header, open);
50
+ if (end < 0) return null;
51
+ skipUntil = Math.max(skipUntil, start + end);
52
+ return { text: `${prefix}(${content.slice(start + open + 1, start + end).trim()})`, end };
53
+ };
54
+ if (start > skipUntil && line.trim()) {
55
+ let match;
56
+ const matches = regex => { match = header.match(regex); return match !== null; };
57
+ if (language === 'js') {
58
+ if (matches(jsFunction)) {
59
+ const signature = params(`${match[1] ? 'export ' : ''}${match[2] ? 'async ' : ''}function ${match[3]}`, match[0].length);
60
+ if (signature) push(signature.text);
61
+ } else if (matches(jsType)) {
62
+ const base = match[2] === 'class' ? header.slice(match[0].length).match(/^\s+extends\s+([\w$.]+)/)?.[1] : '';
63
+ push(`${match[1] ? 'export ' : ''}${match[2]} ${match[3]}${base ? ` extends ${base}` : ''}`);
64
+ } else if (matches(jsVariable)) {
65
+ const prefix = `${match[1] ? 'export ' : ''}const ${match[2]}`;
66
+ const rest = header.slice(match[0].length);
67
+ const arrow = rest.match(/^(?:<[^;{}]+>\s*)?\(/);
68
+ const expression = rest.match(/^function(?:\s+[\w$]+)?\s*\(/);
69
+ if (arrow || expression) {
70
+ const signature = params(`${prefix} = ${match[3] ? 'async ' : ''}`, match[0].length + (expression ? expression[0].length - 1 : 0));
71
+ if (signature && (expression || /^(?:\s*:\s*[^;=]+)?\s*=>/.test(header.slice(signature.end + 1, signature.end + 1024)))) push(signature.text);
72
+ else if (match[1]) push(prefix);
73
+ } else {
74
+ const single = rest.match(new RegExp(`^(${IDENTIFIER})\\s*=>`, 'u'));
75
+ if (single) push(`${prefix} = ${match[3] ? 'async ' : ''}(${single[1]})`);
76
+ else if (match[1]) push(prefix);
77
+ }
78
+ } else if (depth > 0 && matches(jsMethod) && !CONTROL_WORDS.has(match[1])) {
79
+ const signature = params(`${/\basync\s/.test(match[0]) ? 'async ' : ''}${match[1]}`, match[0].length - 1);
80
+ if (signature && /^\s*(?::[^;=]+)?\s*\{/.test(header.slice(signature.end + 1, signature.end + 1024))) push(signature.text, true);
81
+ }
82
+ } else if (language === 'py') {
83
+ if (matches(new RegExp(`^(async\\s+)?def\\s+(${IDENTIFIER})(?:\\[[^\\]]+\\])?\\s*\\(`, 'u'))) {
84
+ const signature = params(`${match[1] ? 'async ' : ''}def ${match[2]}`, match[0].length - 1);
85
+ if (signature) push(signature.text);
86
+ } else if (matches(new RegExp(`^class\\s+(${IDENTIFIER})(?:\\[[^\\]]+\\])?`, 'u'))) {
87
+ const rest = header.slice(match[0].length);
88
+ if (/^\s*\(/.test(rest)) {
89
+ const signature = params(`class ${match[1]}`, match[0].length);
90
+ if (signature) push(signature.text);
91
+ } else if (/^\s*:/.test(rest)) push(`class ${match[1]}`);
92
+ }
93
+ } else if (language === 'go') {
94
+ if (matches(/^func\s+(?:\([^)]*\)\s*)?([\p{ID_Start}_][\p{ID_Continue}_]*)(?:\[[^\]]+\])?\s*\(/u)) {
95
+ const open = match[0].lastIndexOf('(');
96
+ const end = parameterEnd(header, open);
97
+ if (end >= 0) { push(content.slice(start, start + end + 1)); skipUntil = start + end; }
98
+ } else if (matches(/^type\s+([\p{ID_Start}_][\p{ID_Continue}_]*)(?:\[[^\]]+\])?\s+(struct|interface)/u)) push(`type ${match[1]} ${match[2]}`);
99
+ } else if (language === 'rust') {
100
+ if (matches(/^(?:pub(?:\([^)]*\))?\s+)?(?:(?:async|unsafe|const|extern)\s+)*(fn\s+[\p{ID_Start}_][\p{ID_Continue}_]*(?:<[^;{}]*>)?\s*)\(/u)) {
101
+ const open = match[0].lastIndexOf('(');
102
+ const end = parameterEnd(header, open);
103
+ if (end >= 0) { push(content.slice(start, start + end + 1)); skipUntil = start + end; }
104
+ } else if (matches(/^(?:pub(?:\([^)]*\))?\s+)?(?:(?:unsafe|auto)\s+)?(?:struct|enum|trait)\s+[\p{ID_Start}_][\p{ID_Continue}_]*/u)) push(match[0]);
105
+ else if (matches(/^(?:unsafe\s+)?impl(?:<[^;{}]*>)?\s+[^{}]+/)) push(match[0]);
106
+ }
107
+ }
108
+ if (language !== 'py') for (const char of line) {
109
+ if (char === '{') depth++;
110
+ else if (char === '}') depth = Math.max(0, depth - 1);
111
+ }
112
+ offset += line.length + 1;
113
+ }
114
+ return entries;
115
+ }
116
+
117
+ /** Check language support without reading file contents. */
118
+ export function supportsCodeOutline(filePath) {
119
+ const ext = path.extname(filePath).toLowerCase();
120
+ return !ext || JS_TS_EXTS.has(ext) || PY_EXTS.has(ext) || GO_EXTS.has(ext) || RS_EXTS.has(ext);
121
+ }
122
+
123
+ /** Extract lightweight symbols with 1-based line numbers. */
124
+ export function extractCodeOutline(content, { filePath = '', startLine = 1, endLine = Infinity } = {}) {
125
+ if (typeof content !== 'string' || !content) return [];
126
+ const ext = path.extname(filePath).toLowerCase();
127
+ const language = PY_EXTS.has(ext) ? 'py' : GO_EXTS.has(ext) ? 'go' : RS_EXTS.has(ext) ? 'rust' : JS_TS_EXTS.has(ext) || !ext ? 'js' : null;
128
+ if (!language) return [];
129
+ return scanDeclarations(content, language).filter(item => item.line >= startLine && item.line <= endLine);
130
+ }
131
+
132
+ /** Cacheable symbols are independent of the caller's current read offset. */
133
+ export function buildCodeOutline(content, filePath) {
134
+ return { entries: extractCodeOutline(content, { filePath }), totalLines: content.split('\n').length };
135
+ }
136
+
137
+ /** Prefer top-level APIs and sample across the remaining file instead of its first page. */
138
+ export function formatOutlineEntries(entries, { startLine = 1, endLine = Infinity, maxEntries = 20, maxBytes = 1536 } = {}) {
139
+ const all = entries.filter(item => item.line >= startLine && item.line <= endLine);
140
+ const groups = [[], [], []];
141
+ for (const entry of all) groups[entry.nested ? 2 : /^(?:export|pub\b)/.test(entry.text) ? 0 : 1].push(entry);
142
+ for (let count = Math.min(all.length, Math.max(0, Math.floor(maxEntries))); count > 0; count--) {
143
+ const shown = [];
144
+ for (const group of groups) {
145
+ const slots = Math.min(group.length, count - shown.length);
146
+ for (let i = 0; i < slots; i++) {
147
+ const index = slots === 1 ? 0 : Math.round(i * (group.length - 1) / (slots - 1));
148
+ shown.push(group[index]);
149
+ }
150
+ }
151
+ shown.sort((a, b) => a.line - b.line);
152
+ const lines = shown.map(item => `L${item.line}: ${item.nested ? ' ' : ''}${item.text}`);
153
+ if (all.length > count) lines.push(`... (+${all.length - count} more symbols)`);
154
+ const output = lines.join('\n');
155
+ if (Buffer.byteLength(output, 'utf8') <= maxBytes) return output;
156
+ }
157
+ return '';
158
+ }
159
+
160
+ /**
161
+ * Format bounded structural outline lines for a file slice.
162
+ */
163
+ export function formatCodeOutline(content, {
164
+ filePath = '',
165
+ startLine = 1,
166
+ endLine = Infinity,
167
+ maxEntries = 20,
168
+ maxBytes = 1536
169
+ } = {}) {
170
+ return formatOutlineEntries(extractCodeOutline(content, { filePath }), { startLine, endLine, maxEntries, maxBytes });
171
+ }
@@ -3,18 +3,31 @@ import type z from '@deepseek-ai/schemastery';
3
3
 
4
4
  export interface CompositeToolsOptions {
5
5
  enabled?: boolean;
6
- readBatch?: boolean;
6
+ findAndRead?: boolean;
7
7
  editBatch?: boolean;
8
- maxBatchFiles?: number;
8
+ maxPatterns?: number;
9
+ defaultContextLines?: number;
10
+ maxFiles?: number;
11
+ maxWindows?: number;
12
+ /** Maximum lines requested in one merged read window. Default: 120. */
13
+ maxWindowLines?: number;
14
+ /** Maximum total lines requested per find_and_read call. Default: 600. */
15
+ maxReadLines?: number;
16
+ /** Maximum simultaneous reads in each budget-aware batch. Default: 3. */
17
+ readConcurrency?: number;
9
18
  maxBatchEdits?: number;
10
- defaultLimitPerFile?: number;
19
+ maxLineChars?: number;
20
+ maxTotalBytes?: number;
21
+ atomicEdits?: boolean;
11
22
  }
12
23
 
13
24
  export interface CompositeToolsStats {
14
- batchesExecuted: number;
15
- filesRequested: number;
16
- filesSucceeded: number;
17
- filesFailed: number;
25
+ findExecuted: number;
26
+ findPatternsSearched: number;
27
+ findWindowsRead: number;
28
+ findLinesRequested: number;
29
+ findReadBatches: number;
30
+ findFilesMatched: number;
18
31
  editBatchesExecuted: number;
19
32
  editsRequested: number;
20
33
  editsSucceeded: number;
@@ -25,8 +38,23 @@ export interface CompositeToolsService {
25
38
  getStats(session: unknown): CompositeToolsStats;
26
39
  }
27
40
 
28
- export declare const READ_BATCH_TOOL: 'read_batch';
41
+ export interface ParsedGrepMatch {
42
+ path: string;
43
+ lineNumber: number;
44
+ line: string;
45
+ }
46
+
47
+ export interface MergedMatchWindow {
48
+ start: number;
49
+ end: number;
50
+ matchedLines: number[];
51
+ }
52
+
53
+ export declare const FIND_AND_READ_TOOL: 'find_and_read';
29
54
  export declare const EDIT_BATCH_TOOL: 'edit_batch';
55
+ export declare function truncateLongLines(text: string, maxLineChars?: number): string;
56
+ export declare function parseGrepOutput(text: string, meta?: unknown): ParsedGrepMatch[];
57
+ export declare function mergeMatchWindows(lineNumbers: number[], contextLines?: number, adjacentGap?: number): MergedMatchWindow[];
30
58
  export declare const CompositeToolsConfig: z<CompositeToolsOptions>;
31
59
  export declare function applyCompositeTools(ctx: Context, rawConfig?: CompositeToolsOptions): CompositeToolsService | null;
32
60
  export declare const name: string;