dsh-awesome-optimizer 3.1.0 → 3.5.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/README.md +26 -20
- package/README.zh.md +26 -20
- package/cache-diagnostics.md +19 -0
- package/cordis.patch.yml +4 -0
- package/outline-benchmark.md +37 -0
- package/outline-benchmark.windows.json +184 -0
- package/package.json +11 -2
- package/scripts/benchmark-outline.mjs +100 -0
- package/src/ast-outline-config.yml +1 -0
- package/src/ast-outline.js +143 -0
- package/src/cache-diagnostics.js +218 -0
- package/src/code-lexical.js +131 -0
- package/src/code-outline.js +126 -153
- package/src/composite-tools.d.ts +8 -0
- package/src/composite-tools.js +105 -162
- package/src/context-compaction.d.ts +34 -0
- package/src/context-compaction.js +111 -0
- package/src/convergence.js +2 -2
- package/src/custom-prompt.js +3 -3
- package/src/edit-recovery.js +4 -1
- package/src/find-read-output.js +175 -0
- package/src/find-read-ranking.js +63 -0
- package/src/index.d.ts +8 -0
- package/src/index.js +10 -2
- package/src/outline-cache.js +36 -0
- package/src/persistent-eval/daemon-pool.js +21 -0
- package/src/persistent-eval/index.js +18 -10
- package/src/persistent-eval/stdio-runner.js +46 -14
- package/src/persistent-eval/workers/node_worker.mjs +44 -18
- package/src/persistent-eval/workers/py_worker.py +44 -2
- package/src/read-guard.d.ts +28 -0
- package/src/read-guard.js +152 -38
- package/src/telemetry.d.ts +4 -0
- package/src/telemetry.js +20 -12
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { buildCodeOutline } from './code-outline.js';
|
|
5
|
+
|
|
6
|
+
const CONFIG_PATH = fileURLToPath(new URL('./ast-outline-config.yml', import.meta.url));
|
|
7
|
+
const CLI_CWD = path.dirname(CONFIG_PATH);
|
|
8
|
+
const LANGUAGES = new Map([
|
|
9
|
+
...['.js', '.mjs', '.cjs', '.jsx'].map(ext => [ext, 'javascript']),
|
|
10
|
+
...['.ts', '.mts', '.cts'].map(ext => [ext, 'typescript']),
|
|
11
|
+
['.tsx', 'tsx'], ['.py', 'python'], ['.pyw', 'python'], ['.go', 'go'], ['.rs', 'rust']
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export function needsAstOutline(content, filePath, lexical, mode) {
|
|
15
|
+
if (mode === 'off' || !LANGUAGES.has(path.extname(filePath).toLowerCase())) return false;
|
|
16
|
+
if (mode === 'always') return true;
|
|
17
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
18
|
+
// Known weak areas of the bounded lexical scanner, without a repository-wide scan.
|
|
19
|
+
if (['.tsx', '.jsx'].includes(ext)) return true;
|
|
20
|
+
if (['.ts', '.mts', '.cts'].includes(ext) && /^\s*(?:@|export\s+(?:declare\s+)?(?:namespace|module)\b)/m.test(content)) return true;
|
|
21
|
+
return lexical.entries.length === 0 && /^\s*(?:(?:export|pub|async|unsafe)\s+)*(?:function|class|def|fn|func|impl|trait|interface)\b/m.test(content);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Parse only the authorized snapshot. Never pass a model-controlled path to the CLI. */
|
|
25
|
+
export function runAstOutline(content, filePath, {
|
|
26
|
+
command = 'ast-grep', timeoutMs = 750, maxOutputBytes = 256 * 1024, signal
|
|
27
|
+
} = {}) {
|
|
28
|
+
const language = LANGUAGES.get(path.extname(filePath).toLowerCase());
|
|
29
|
+
if (!language || signal?.aborted) return Promise.resolve({ status: signal?.aborted ? 'aborted' : 'unsupported' });
|
|
30
|
+
return new Promise(resolve => {
|
|
31
|
+
let child;
|
|
32
|
+
let settled = false;
|
|
33
|
+
let failure;
|
|
34
|
+
let bytes = 0;
|
|
35
|
+
const chunks = [];
|
|
36
|
+
const finish = result => {
|
|
37
|
+
if (settled) return;
|
|
38
|
+
settled = true;
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
clearTimeout(killTimer);
|
|
41
|
+
signal?.removeEventListener('abort', abort);
|
|
42
|
+
resolve(result);
|
|
43
|
+
};
|
|
44
|
+
let killTimer;
|
|
45
|
+
const stop = status => {
|
|
46
|
+
if (settled || failure) return;
|
|
47
|
+
failure = status;
|
|
48
|
+
child?.kill('SIGKILL');
|
|
49
|
+
// Do not hold a read hook indefinitely if the platform never emits close.
|
|
50
|
+
killTimer = setTimeout(() => finish({ status }), 100);
|
|
51
|
+
};
|
|
52
|
+
const abort = () => stop('aborted');
|
|
53
|
+
const timer = setTimeout(() => stop('timeout'), timeoutMs);
|
|
54
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
55
|
+
try {
|
|
56
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([name, value]) =>
|
|
57
|
+
value !== undefined && !/KEY|PASSWORD|SECRET|TOKEN/i.test(name) && !/^DSH_/i.test(name)
|
|
58
|
+
));
|
|
59
|
+
child = spawn(command, [
|
|
60
|
+
'outline', '--stdin', '--lang', language, '--json=compact', '--view', 'expanded', '--items', 'structure',
|
|
61
|
+
'--config', CONFIG_PATH, '--custom-languages', 'ignore', '--color', 'never', '--threads', '1'
|
|
62
|
+
], { cwd: CLI_CWD, env, shell: false, windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
63
|
+
child.once('error', error => finish({ status: error.code === 'ENOENT' ? 'unavailable' : 'error' }));
|
|
64
|
+
child.stdin.on('error', () => {});
|
|
65
|
+
child.stdout.on('data', chunk => {
|
|
66
|
+
bytes += chunk.length;
|
|
67
|
+
if (bytes > maxOutputBytes) stop('output-limit');
|
|
68
|
+
else chunks.push(chunk);
|
|
69
|
+
});
|
|
70
|
+
// Drain diagnostics without retaining or exposing source excerpts.
|
|
71
|
+
child.stderr.on('data', () => {});
|
|
72
|
+
child.once('close', code => {
|
|
73
|
+
if (failure) return finish({ status: failure });
|
|
74
|
+
if (code !== 0) return finish({ status: 'error' });
|
|
75
|
+
try {
|
|
76
|
+
const value = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
77
|
+
const entries = parseAstEntries(value, content.split('\n').length);
|
|
78
|
+
// Bundled outline rules expose TS namespaces without their children.
|
|
79
|
+
// Preserve lexically recognized children inside these AST-confirmed scopes.
|
|
80
|
+
if (language === 'typescript') {
|
|
81
|
+
const modules = value[0].items.filter(item => item.symbolType === 'module');
|
|
82
|
+
if (modules.length) {
|
|
83
|
+
const lexical = buildCodeOutline(content, filePath);
|
|
84
|
+
for (const item of modules) {
|
|
85
|
+
const start = item.range.start.line + 1;
|
|
86
|
+
const end = item.range.end?.line + 1;
|
|
87
|
+
for (const entry of lexical.entries) {
|
|
88
|
+
if (entry.line > start && entry.line <= end && !entries.some(existing => existing.line === entry.line)) {
|
|
89
|
+
entries.push({ ...entry, nested: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
entries.sort((a, b) => a.line - b.line);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finish({ status: 'ok', entries });
|
|
97
|
+
} catch { finish({ status: 'invalid-output' }); }
|
|
98
|
+
});
|
|
99
|
+
child.stdin.end(content, 'utf8');
|
|
100
|
+
if (signal?.aborted) abort();
|
|
101
|
+
} catch { finish({ status: 'error' }); }
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function parseAstEntries(value, totalLines) {
|
|
106
|
+
if (!Array.isArray(value) || value.length !== 1 || !Array.isArray(value[0]?.items)) throw new Error('Invalid AST outline shape');
|
|
107
|
+
const entries = [];
|
|
108
|
+
const visit = (items, nested = false, depth = 0) => {
|
|
109
|
+
if (depth > 32) throw new Error('AST outline depth exceeded');
|
|
110
|
+
for (const item of items) {
|
|
111
|
+
if (item.isImport || item.role === 'import') continue;
|
|
112
|
+
const line = item.range?.start?.line;
|
|
113
|
+
if (!Number.isInteger(line) || line < 0 || line >= totalLines || typeof item.signature !== 'string') throw new Error('Invalid AST item');
|
|
114
|
+
let text = item.signature.replace(/\s+/g, ' ').trim();
|
|
115
|
+
if (!text) continue;
|
|
116
|
+
if (item.isExported && !/^(?:export\b|pub\b)/.test(text)) text = `export ${text}`;
|
|
117
|
+
entries.push({ line: line + 1, text: text.length > 96 ? `${text.slice(0, 93)}...` : text, nested });
|
|
118
|
+
if (Array.isArray(item.members)) visit(item.members, true, depth + 1);
|
|
119
|
+
if (Array.isArray(item.children)) visit(item.children, true, depth + 1);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
visit(value[0].items);
|
|
123
|
+
return entries.sort((a, b) => a.line - b.line);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function buildAdaptiveOutline(content, filePath, options = {}, signal, stats) {
|
|
127
|
+
const lexical = buildCodeOutline(content, filePath);
|
|
128
|
+
if (!needsAstOutline(content, filePath, lexical, options.astOutline ?? 'auto')) return { ...lexical, engine: 'lexical' };
|
|
129
|
+
if (stats) stats.astAttempts++;
|
|
130
|
+
const result = await runAstOutline(content, filePath, {
|
|
131
|
+
command: options.astGrepCommand, timeoutMs: options.astTimeoutMs,
|
|
132
|
+
maxOutputBytes: options.astMaxOutputBytes, signal
|
|
133
|
+
});
|
|
134
|
+
if (result.status === 'ok') {
|
|
135
|
+
if (stats) stats.astSucceeded++;
|
|
136
|
+
return { entries: result.entries, totalLines: lexical.totalLines, engine: 'ast', astStatus: 'ok' };
|
|
137
|
+
}
|
|
138
|
+
if (stats) {
|
|
139
|
+
stats.astFallbacks++;
|
|
140
|
+
if (result.status === 'timeout') stats.astTimeouts++;
|
|
141
|
+
}
|
|
142
|
+
return { ...lexical, engine: 'lexical', astStatus: result.status };
|
|
143
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import { mkdirSync, statSync } from 'node:fs';
|
|
4
|
+
import { appendFile } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const scope = new AsyncLocalStorage();
|
|
8
|
+
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
9
|
+
let users = 0;
|
|
10
|
+
let originalFetch;
|
|
11
|
+
let wrappedFetch;
|
|
12
|
+
const MAX_EVENT = 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
function numericUsage(value, depth = 0) {
|
|
15
|
+
if (!value || typeof value !== 'object' || depth > 2) return {};
|
|
16
|
+
const out = {};
|
|
17
|
+
for (const key of ['prompt_tokens', 'completion_tokens', 'total_tokens', 'cached_tokens',
|
|
18
|
+
'prompt_cache_hit_tokens', 'prompt_cache_miss_tokens', 'input_tokens', 'output_tokens',
|
|
19
|
+
'cache_read_input_tokens', 'cache_creation_input_tokens', 'prompt_tokens_details',
|
|
20
|
+
'input_tokens_details', 'reasoning_tokens']) {
|
|
21
|
+
const item = value[key];
|
|
22
|
+
if (typeof item === 'number' && Number.isFinite(item)) out[key] = item;
|
|
23
|
+
else if (item && typeof item === 'object') out[key] = numericUsage(item, depth + 1);
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function identifier(value) {
|
|
29
|
+
return typeof value === 'string' && value.length <= 256 && /^[\w.:/-]+$/.test(value) ? value : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function preserveMetadata(target, source) {
|
|
33
|
+
for (const key of ['url', 'redirected', 'type']) {
|
|
34
|
+
Object.defineProperty(target, key, { value: source[key], configurable: true });
|
|
35
|
+
}
|
|
36
|
+
const clone = target.clone.bind(target);
|
|
37
|
+
Object.defineProperty(target, 'clone', { value: () => preserveMetadata(clone(), source), configurable: true });
|
|
38
|
+
return target;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// One transport wrapper; only fetches inside an explicitly enabled LLM scope are observed.
|
|
42
|
+
function acquire() {
|
|
43
|
+
if (users++ !== 0) return;
|
|
44
|
+
originalFetch = globalThis.fetch;
|
|
45
|
+
const delegate = originalFetch;
|
|
46
|
+
wrappedFetch = async function (input, init) {
|
|
47
|
+
const active = scope.getStore();
|
|
48
|
+
if (!active || active.owner.closed || active.owner.stopped) return delegate(input, init);
|
|
49
|
+
let record;
|
|
50
|
+
try {
|
|
51
|
+
const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url);
|
|
52
|
+
if (!url.pathname.endsWith('/chat/completions') || typeof init?.body !== 'string') {
|
|
53
|
+
return delegate(input, init);
|
|
54
|
+
}
|
|
55
|
+
const body = JSON.parse(init.body);
|
|
56
|
+
if (!Array.isArray(body.messages)) return delegate(input, init);
|
|
57
|
+
record = active.owner.request(active.session, body, url);
|
|
58
|
+
} catch { /* Observation must not fail inference. */ }
|
|
59
|
+
let response;
|
|
60
|
+
try { response = await delegate(input, init); }
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (record) active.owner.emit({ type: 'transport-error', request: record.request });
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
if (!record) return response;
|
|
66
|
+
active.owner.emit({ type: 'response', request: record.request, status: response.status,
|
|
67
|
+
requestId: identifier(response.headers.get('x-request-id') ?? response.headers.get('request-id')) });
|
|
68
|
+
if (!response.body) return response;
|
|
69
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
70
|
+
if (!contentType.includes('text/event-stream') && !contentType.includes('application/json')) return response;
|
|
71
|
+
const decoder = new TextDecoder();
|
|
72
|
+
let pending = '';
|
|
73
|
+
let dropped = false;
|
|
74
|
+
const observe = text => {
|
|
75
|
+
try {
|
|
76
|
+
const event = JSON.parse(text);
|
|
77
|
+
if (event.usage) active.owner.emit({ type: 'usage', request: record.request,
|
|
78
|
+
usage: numericUsage(event.usage), responseId: identifier(event.id),
|
|
79
|
+
model: identifier(event.model), systemFingerprint: identifier(event.system_fingerprint) });
|
|
80
|
+
} catch { /* Ignore non-JSON events such as [DONE]. */ }
|
|
81
|
+
};
|
|
82
|
+
const inspect = (bytes, final = false) => {
|
|
83
|
+
if (dropped) return;
|
|
84
|
+
pending += decoder.decode(bytes, { stream: !final });
|
|
85
|
+
if (pending.length > MAX_EVENT) {
|
|
86
|
+
dropped = true;
|
|
87
|
+
pending = '';
|
|
88
|
+
active.owner.emit({ type: 'capture-gap', request: record.request, reason: 'event-size-limit' });
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (contentType.includes('text/event-stream')) {
|
|
92
|
+
pending = pending.replace(/\r\n/g, '\n');
|
|
93
|
+
let end = pending.indexOf('\n\n');
|
|
94
|
+
while (end !== -1) {
|
|
95
|
+
const event = pending.slice(0, end);
|
|
96
|
+
pending = pending.slice(end + 2);
|
|
97
|
+
observe(event.split('\n').filter(line => line.startsWith('data:')).map(line => line.slice(5).trimStart()).join('\n'));
|
|
98
|
+
end = pending.indexOf('\n\n');
|
|
99
|
+
}
|
|
100
|
+
} else if (final) observe(pending);
|
|
101
|
+
};
|
|
102
|
+
const stream = response.body.pipeThrough(new TransformStream({
|
|
103
|
+
transform(chunk, controller) {
|
|
104
|
+
try { inspect(chunk); } catch { /* Preserve the original stream. */ }
|
|
105
|
+
controller.enqueue(chunk);
|
|
106
|
+
},
|
|
107
|
+
flush() {
|
|
108
|
+
try { inspect(undefined, true); } catch { /* Preserve stream completion. */ }
|
|
109
|
+
active.owner.emit({ type: 'complete', request: record.request, captureGap: dropped });
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
return preserveMetadata(new Response(stream, { status: response.status, statusText: response.statusText, headers: response.headers }), response);
|
|
113
|
+
};
|
|
114
|
+
globalThis.fetch = wrappedFetch;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class CacheDiagnostics {
|
|
118
|
+
constructor(filePath, { sink, maxBytes = 8 * 1024 * 1024, onStop, maxQueuedBytes = 256 * 1024, writer = appendFile } = {}) {
|
|
119
|
+
this.previous = new Map();
|
|
120
|
+
this.onStop = onStop;
|
|
121
|
+
this.stopped = false;
|
|
122
|
+
this.maxQueuedBytes = maxQueuedBytes;
|
|
123
|
+
this.queuedBytes = 0;
|
|
124
|
+
this.pending = Promise.resolve();
|
|
125
|
+
this.writer = writer;
|
|
126
|
+
this.closed = false;
|
|
127
|
+
this.maxBytes = maxBytes;
|
|
128
|
+
this.bytes = 0;
|
|
129
|
+
this.sink = sink;
|
|
130
|
+
this.filePath = filePath;
|
|
131
|
+
if (!sink) {
|
|
132
|
+
if (!path.isAbsolute(filePath)) throw new Error('Cache diagnostic path must be absolute');
|
|
133
|
+
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
134
|
+
try { this.bytes = statSync(filePath).size; } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
135
|
+
}
|
|
136
|
+
acquire();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
stop(reason) {
|
|
140
|
+
if (this.stopped) return;
|
|
141
|
+
this.stopped = true;
|
|
142
|
+
this.previous.clear();
|
|
143
|
+
try { this.onStop?.(reason); } catch {}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
flush() { return this.pending; }
|
|
147
|
+
|
|
148
|
+
emit(record) {
|
|
149
|
+
if (this.closed || this.stopped) return;
|
|
150
|
+
try {
|
|
151
|
+
const line = JSON.stringify({ time: new Date().toISOString(), ...record }) + '\n';
|
|
152
|
+
const bytes = Buffer.byteLength(line);
|
|
153
|
+
if (this.bytes + bytes > this.maxBytes) { this.stop('file-size-limit'); return; }
|
|
154
|
+
if (!this.sink && this.queuedBytes + bytes > this.maxQueuedBytes) { this.stop('queue-size-limit'); return; }
|
|
155
|
+
this.bytes += bytes;
|
|
156
|
+
if (this.sink) this.sink(JSON.parse(line));
|
|
157
|
+
else {
|
|
158
|
+
this.queuedBytes += bytes;
|
|
159
|
+
this.pending = this.pending.then(async () => {
|
|
160
|
+
try {
|
|
161
|
+
if (!this.writeFailed) await this.writer(this.filePath, line, { encoding: 'utf8', mode: 0o600 });
|
|
162
|
+
} catch { this.writeFailed = true; this.stop('write-failed'); }
|
|
163
|
+
finally { this.queuedBytes -= bytes; }
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
} catch { this.stop('observer-failed'); }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
request(session, body, url) {
|
|
170
|
+
const messages = body.messages.map(digest);
|
|
171
|
+
const { messages: ignored, ...config } = body;
|
|
172
|
+
const configDigest = digest(config);
|
|
173
|
+
const key = digest([session, url.origin, url.pathname, body.model]);
|
|
174
|
+
const previous = this.previous.get(key);
|
|
175
|
+
let firstDifference = null;
|
|
176
|
+
if (previous) {
|
|
177
|
+
for (let i = 0; i < previous.messages.length; i++) {
|
|
178
|
+
if (messages[i] !== previous.messages[i]) { firstDifference = i; break; }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const record = { type: 'request', request: randomUUID(), session: digest(session),
|
|
182
|
+
endpoint: digest(url.origin + url.pathname), model: identifier(body.model),
|
|
183
|
+
messages, configDigest, toolsDigest: body.tools === undefined ? null : digest(body.tools),
|
|
184
|
+
cacheKeyDigest: body.prompt_cache_key === undefined ? null : digest(body.prompt_cache_key),
|
|
185
|
+
previousRequest: previous?.request ?? null, firstDifference,
|
|
186
|
+
prefixStable: previous ? firstDifference === null : null,
|
|
187
|
+
configStable: previous ? configDigest === previous.configDigest : null };
|
|
188
|
+
this.previous.delete(key);
|
|
189
|
+
this.previous.set(key, record);
|
|
190
|
+
if (this.previous.size > 128) this.previous.delete(this.previous.keys().next().value);
|
|
191
|
+
this.emit(record);
|
|
192
|
+
return record;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
wrap(session, next) {
|
|
196
|
+
if (session === undefined || session === null || this.closed) return next();
|
|
197
|
+
const active = { owner: this, session: String(session) };
|
|
198
|
+
return (async function* () {
|
|
199
|
+
const stream = scope.run(active, next);
|
|
200
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
201
|
+
try {
|
|
202
|
+
while (true) {
|
|
203
|
+
const result = await scope.run(active, () => iterator.next());
|
|
204
|
+
if (result.done) return;
|
|
205
|
+
yield result.value;
|
|
206
|
+
}
|
|
207
|
+
} finally { if (iterator.return) await scope.run(active, () => iterator.return()); }
|
|
208
|
+
})();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
dispose() {
|
|
212
|
+
if (this.closed) return this.flush();
|
|
213
|
+
this.closed = true;
|
|
214
|
+
this.previous.clear();
|
|
215
|
+
if (--users === 0 && globalThis.fetch === wrappedFetch) globalThis.fetch = originalFetch;
|
|
216
|
+
return this.flush();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -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
|
+
}
|