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
@@ -7,7 +7,7 @@ export const SOURCE = 'optimizer-convergence';
7
7
 
8
8
  export const GUIDANCE = [
9
9
  "Work toward the user's completion criteria with as few model round trips as practical.",
10
- 'Batch independent reads, searches, and checks in a single response when their inputs are known (or use read_batch for multi-file reads); preserve dependencies and exclusive tool barriers.',
10
+ 'Batch independent reads, searches, and checks in a single response when their inputs are known; use find_and_read when searching code and inspecting surrounding lines together. Preserve dependencies and exclusive tool barriers.',
11
11
  'Command chaining: Run heavy tasks (npm test, npm run build, vitest, npm publish) standalone to prevent Windows deadlocks; chain lightweight commands via && (git add ... && git commit ... && git push) to eliminate round trips.',
12
12
  'Before emitting a large payload or calculation, produce deliverables or reusable scripts to disk using native write and execute by file path. NEVER embed complete files, calculations, or large scripts in shell arguments (node -e, python -c, pwsh -Command).',
13
13
  'For localized revisions on existing files, reuse context already in the conversation and prefer native edit (or edit_batch for multi-file/multi-location revisions) with an unambiguous match over rewriting the entire file.',
@@ -15,7 +15,7 @@ export const GUIDANCE = [
15
15
  'This efficiency guidance NEVER overrides the user, correctness, permission checks, required verification, or cancellation.'
16
16
  ].join('\n');
17
17
 
18
- export const REMINDER = 'Accelerate execution: batch remaining actions and conclude if criteria are met; NEVER omit verification.';
18
+ export const REMINDER = 'Batch remaining actions; conclude only if criteria are met; NEVER omit verification.';
19
19
 
20
20
  export const ConvergenceConfig = z.object({
21
21
  enabled: z.boolean().default(true),
@@ -146,7 +146,7 @@ export function applyConvergence(ctx, raw = {}) {
146
146
  source: {
147
147
  kind: SOURCE,
148
148
  form: 'notice',
149
- summary: 'Accelerate execution and conclude when criteria are met.'
149
+ summary: 'Batch remaining actions; conclude only if criteria are met.'
150
150
  }
151
151
  });
152
152
  return { ...decision, messages: [...decision.messages, reminder] };
@@ -22,6 +22,8 @@ export interface CustomPromptOptions {
22
22
  hotReload?: boolean;
23
23
  interpolate?: boolean;
24
24
  injectEnvironmentFacts?: boolean;
25
+ includeCliFacts?: boolean;
26
+ extraEnvironmentFacts?: string[];
25
27
  }
26
28
 
27
29
  export interface PythonEnvInfo {
@@ -33,7 +35,11 @@ export declare const CustomPromptConfig: z<CustomPromptOptions>;
33
35
 
34
36
  export declare function detectPythonEnv(spawnSyncFn?: any): PythonEnvInfo | null;
35
37
  export declare function resetPythonCacheForTesting(): void;
36
- export declare function formatEnvironmentFacts(processRef?: any, pythonDetector?: () => PythonEnvInfo | null): string;
38
+ export declare function formatEnvironmentFacts(
39
+ processRef?: { version?: string; platform?: string; arch?: string },
40
+ pythonDetector?: () => PythonEnvInfo | null,
41
+ options?: { includeCliFacts?: boolean; extraFacts?: string[] }
42
+ ): string;
37
43
 
38
44
  export declare function resolveDshHome(env?: NodeJS.ProcessEnv): string;
39
45
  export declare function resolveGlobalPath(filePath?: string, env?: NodeJS.ProcessEnv): string;
@@ -21,9 +21,10 @@ export const CustomPromptConfig = z.object({
21
21
  mergeStrategy: z.union(['concat', 'workspace-override', 'global-only', 'workspace-only']).default('concat'),
22
22
  hotReload: z.boolean().default(true),
23
23
  interpolate: z.boolean().default(false),
24
- injectEnvironmentFacts: z.boolean().default(true)
24
+ injectEnvironmentFacts: z.boolean().default(true),
25
+ includeCliFacts: z.boolean().default(true),
26
+ extraEnvironmentFacts: z.array(z.string()).default([])
25
27
  });
26
-
27
28
  let cachedPython = undefined;
28
29
 
29
30
  export function resetPythonCacheForTesting() {
@@ -54,21 +55,34 @@ export function detectPythonEnv(spawnSyncFn = spawnSync) {
54
55
  /**
55
56
  * Generate compact host environment facts to eliminate exploratory probing turns (pwd, ls, node -v).
56
57
  */
57
- export function formatEnvironmentFacts(processRef = process, pythonDetector = detectPythonEnv) {
58
+ export function formatEnvironmentFacts(processRef = process, pythonDetector = detectPythonEnv, options = {}) {
58
59
  const version = processRef?.version || 'unknown';
59
60
  const platform = processRef?.platform || 'unknown';
60
61
  const arch = processRef?.arch || 'unknown';
61
62
  const py = typeof pythonDetector === 'function' ? pythonDetector() : null;
62
63
  const pyInfo = py ? `, ${py.version} (${py.bin})` : ' (Python not detected; use Node.js for ad-hoc scripts)';
63
- return [
64
+ const includeCliFacts = options?.includeCliFacts !== false;
65
+ const lines = [
64
66
  '## Host Environment & Tooling Facts',
65
- `- Runtime: Node.js ${version}${pyInfo} on ${platform} (${arch}).`,
66
- '- Standalone CLIs: playwright-cli, gh, ast-grep, and opencli are standalone binaries via bash; NEVER require/import as node modules.',
67
- '- Browser automation: playwright-cli blocks "file:" URLs (serve via loopback HTTP); \'screenshot\' takes selector target ONLY (no output filename; auto-saved to .playwright-cli/). NEVER probe via \'--help\' or \'--version\'.',
67
+ `- Runtime: Node.js ${version}${pyInfo} on ${platform} (${arch}).`
68
+ ];
69
+ if (includeCliFacts) {
70
+ lines.push(
71
+ '- Standalone CLIs: playwright-cli, gh, ast-grep, and opencli are standalone binaries via bash; NEVER require/import as node modules.',
72
+ '- Browser automation: playwright-cli blocks "file:" URLs (serve via loopback HTTP); \'screenshot\' takes selector target ONLY (no output filename; auto-saved to .playwright-cli/). NEVER probe via \'--help\' or \'--version\'.'
73
+ );
74
+ }
75
+ lines.push(
68
76
  '- Zero startup probing: workspace context and host facts are already hydrated. NEVER run exploratory probing (pwd, ls, node -v, which).',
69
- '- Persistent evaluation: `eval_python` and `eval_node` maintain stateful in-memory kernels across turns without disk pollution (math, re, json, time, collections, itertools pre-imported); prioritize for intermediate math, kinematic solving, and data filtering.',
77
+ '- Persistent evaluation: `eval_python` and `eval_node` maintain stateful in-memory kernels across turns; the session working directory is synchronized before execution and exposed as CWD. Python pre-imports math, re, json, time, collections, itertools, pathlib, difflib, csv; use for calculations, data filtering, and read-only project analysis with compact summaries, subject to file access rules.',
70
78
  '- Ad-hoc scripts & cleanup: MUST write ad-hoc Node/Python scripts to disk via native write first and execute by file path; remove temporary artifacts directly via bash (rm -rf).'
71
- ].join('\n');
79
+ );
80
+ if (Array.isArray(options?.extraFacts)) {
81
+ for (const fact of options.extraFacts) {
82
+ if (typeof fact === 'string' && fact.trim()) lines.push(fact);
83
+ }
84
+ }
85
+ return lines.join('\n');
72
86
  }
73
87
 
74
88
  /**
@@ -194,15 +208,32 @@ export class CustomPromptManager {
194
208
  try {
195
209
  const dir = path.dirname(targetPath);
196
210
  const filename = path.basename(targetPath);
211
+ const isWin = process.platform === 'win32';
212
+ const matchesName = (a, b) => (isWin ? a.toLowerCase() === b.toLowerCase() : a === b);
213
+
197
214
  if (!fs.existsSync(dir)) {
198
- // If directory doesn't exist yet, try to create parent or skip until created
215
+ // Watch nearest existing parent directory (e.g. workspace root before .dsh/ exists)
216
+ const parentDir = path.dirname(dir);
217
+ const subDirName = path.basename(dir);
218
+ if (!fs.existsSync(parentDir) || this.watchers.has(dir)) return;
219
+ const parentWatcher = fs.watch(parentDir, { persistent: false }, (_eventType, triggerName) => {
220
+ if (!triggerName || matchesName(triggerName, subDirName)) {
221
+ if (fs.existsSync(dir)) {
222
+ try { parentWatcher.close(); } catch {}
223
+ this.watchers.delete(dir);
224
+ this.watchPath(targetPath, onChange);
225
+ onChange();
226
+ }
227
+ }
228
+ });
229
+ parentWatcher.on('error', () => {});
230
+ this.watchers.set(dir, parentWatcher);
199
231
  return;
200
232
  }
201
233
 
202
234
  const watcher = fs.watch(dir, { persistent: false }, (_eventType, triggerName) => {
203
- if (!triggerName || triggerName === filename) {
204
- const existingTimer = this.debounceTimers.get(targetPath);
205
- if (existingTimer) clearTimeout(existingTimer);
235
+ if (!triggerName || matchesName(triggerName, filename)) {
236
+ clearTimeout(this.debounceTimers.get(targetPath));
206
237
  this.debounceTimers.set(
207
238
  targetPath,
208
239
  setTimeout(() => {
@@ -301,7 +332,7 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
301
332
  if (!customText) return assembly;
302
333
 
303
334
  if (config.placement === 'replace-all') {
304
- // Retain optimizer's own sections (e.g. convergence guidance) while stripping all official verbose sections
335
+ // Retain optimizer's own sections (e.g. convergence guidance) while stripping all other sections
305
336
  const optimizerSections = assembly.sections.filter(
306
337
  s => typeof s.name === 'string' && s.name.startsWith('optimizer:') && s.name !== config.sectionName && s.name !== ENVIRONMENT_FACTS_SECTION
307
338
  );
@@ -315,7 +346,10 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
315
346
  if (config.injectEnvironmentFacts) {
316
347
  sections.push({
317
348
  name: ENVIRONMENT_FACTS_SECTION,
318
- text: formatEnvironmentFacts(),
349
+ text: formatEnvironmentFacts(process, detectPythonEnv, {
350
+ includeCliFacts: config.includeCliFacts,
351
+ extraFacts: config.extraEnvironmentFacts
352
+ }),
319
353
  interpolate: false
320
354
  });
321
355
  }
@@ -343,13 +377,14 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
343
377
  if (config.injectEnvironmentFacts && !sections.some(s => s.name === ENVIRONMENT_FACTS_SECTION)) {
344
378
  sections.push({
345
379
  name: ENVIRONMENT_FACTS_SECTION,
346
- text: formatEnvironmentFacts(),
380
+ text: formatEnvironmentFacts(process, detectPythonEnv, {
381
+ includeCliFacts: config.includeCliFacts,
382
+ extraFacts: config.extraEnvironmentFacts
383
+ }),
347
384
  interpolate: false
348
385
  });
349
386
  }
350
387
  return { ...assembly, sections };
351
-
352
- return assembly;
353
388
  });
354
389
 
355
390
  if (typeof ctx.effect === 'function') {
@@ -0,0 +1,59 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type z from '@deepseek-ai/schemastery';
3
+
4
+ export interface EditRecoveryOptions {
5
+ enabled?: boolean;
6
+ contextLines?: number;
7
+ maxSnippetLines?: number;
8
+ maxInspectBytes?: number;
9
+ maxHintBytes?: number;
10
+ maxMultiMatchLocations?: number;
11
+ }
12
+
13
+ export interface EditRecoveryStats {
14
+ inspected: number;
15
+ hintsAttached: number;
16
+ skipped: number;
17
+ }
18
+
19
+ export interface EditMatchLocation {
20
+ startLine: number;
21
+ endLine: number;
22
+ }
23
+
24
+ export type EditMatchResult =
25
+ | { kind: 'multiple-matches'; count: number; locations: EditMatchLocation[]; totalLines: number }
26
+ | { kind: 'whitespace-mismatch'; startLine: number; endLine: number; score: number; notes: string[]; totalLines: number }
27
+ | { kind: 'fuzzy-match'; startLine: number; endLine: number; score: number; notes: string[]; totalLines: number }
28
+ | { kind: 'no-close-match'; totalLines: number };
29
+
30
+ export interface EditRecoveryService {
31
+ getStats(session: unknown): EditRecoveryStats;
32
+ }
33
+
34
+ export declare const EditRecoveryConfig: z<EditRecoveryOptions>;
35
+ export declare function isEditMatchFailure(errorMessage: string, errorCode?: string): boolean;
36
+ export declare function findBestEditMatch(
37
+ content: string,
38
+ oldString: string,
39
+ options?: { maxMultiMatchLocations?: number }
40
+ ): EditMatchResult;
41
+ export declare function buildEditFailureHint(
42
+ content: string,
43
+ oldString: string,
44
+ options?: {
45
+ filePath?: string;
46
+ contextLines?: number;
47
+ maxSnippetLines?: number;
48
+ maxHintBytes?: number;
49
+ maxMultiMatchLocations?: number;
50
+ }
51
+ ): string;
52
+ export declare function applyEditRecovery(ctx: Context, rawConfig?: EditRecoveryOptions): EditRecoveryService | null;
53
+
54
+ export declare const name: string;
55
+ export declare const inject: string[];
56
+ export declare const Config: z<EditRecoveryOptions>;
57
+ export declare function apply(ctx: Context, config?: EditRecoveryOptions): void;
58
+ declare const plugin: { name: typeof name; inject: typeof inject; Config: typeof Config; apply: typeof apply };
59
+ export default plugin;