@causyne/agent 1.20.91 → 1.20.94

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 (46) hide show
  1. package/package.json +1 -1
  2. package/src/agentLoop.js +116 -26
  3. package/src/agentLoop.ts +121 -26
  4. package/src/contextManager.js +2 -0
  5. package/src/decideTools.js +9 -0
  6. package/src/decideTools.ts +9 -0
  7. package/src/decisionPolicy.js +119 -1
  8. package/src/decisionPolicy.ts +129 -1
  9. package/src/historyPruner.js +16 -1
  10. package/src/historyPruner.ts +13 -1
  11. package/src/sandbox.js +33 -7
  12. package/src/skills/skillResourceSafety.js +70 -0
  13. package/src/skills/skillResourceSafety.ts +77 -0
  14. package/src/smartToolsUse.js +28 -11
  15. package/src/smartToolsUse.ts +34 -11
  16. package/src/taskShape.js +28 -13
  17. package/src/taskShape.ts +31 -12
  18. package/src/tools/codeSearcher.js +349 -210
  19. package/src/tools/codeSearcher.ts +408 -0
  20. package/src/tools/codebaseSearch.js +182 -68
  21. package/src/tools/codebaseSearch.ts +221 -0
  22. package/src/tools/deferredMcpSearch.js +94 -40
  23. package/src/tools/deferredMcpSearch.ts +89 -30
  24. package/src/tools/fsFind.js +180 -115
  25. package/src/tools/fsFind.ts +228 -0
  26. package/src/tools/fsList.js +149 -69
  27. package/src/tools/fsList.ts +224 -0
  28. package/src/tools/fsSearch.js +192 -189
  29. package/src/tools/fsSearch.ts +241 -0
  30. package/src/tools/readNativeSkill.js +34 -42
  31. package/src/tools/readNativeSkill.ts +38 -36
  32. package/src/tools/searchRelevance.js +70 -0
  33. package/src/tools/searchRelevance.ts +72 -0
  34. package/src/tools/toolSelfRepair.js +56 -20
  35. package/src/tools/toolSelfRepair.ts +58 -19
  36. package/src/tools/walkerSafety.js +445 -0
  37. package/src/tools/walkerSafety.ts +516 -0
  38. package/src/tui/composerGeometry.js +45 -0
  39. package/src/tui/composerGeometry.ts +80 -0
  40. package/src/tui/homeView.js +10 -6
  41. package/src/tui/homeView.ts +10 -6
  42. package/src/tui/lineEditor.js +8 -0
  43. package/src/tui/prompt.js +9 -4
  44. package/src/userSkills.js +183 -203
  45. package/src/userSkills.ts +301 -0
  46. package/src/userSkills.d.ts +0 -26
@@ -1,77 +1,157 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import os from 'os';
4
-
5
- // Phase 2 AGI Upgrade: Tree-Map Algorithm with Depth Limiting
6
- async function generateTree(dirPath, currentDepth, maxDepth, prefix = '') {
7
- if (currentDepth > maxDepth) return '';
8
-
9
- let treeStr = '';
10
- try {
11
- const items = await fs.readdir(dirPath, { withFileTypes: true });
12
-
13
- // Sort directories first
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { checkSearchScope, createTraversalGuard, isSafeSymlinkTarget, resolveSearchRoot, shouldExcludeChild } from './walkerSafety.js';
4
+ const DEFAULT_LIST_DEPTH = 2;
5
+ const MAX_LIST_DEPTH = 8;
6
+ const MAX_TREE_ENTRIES = 500;
7
+ async function generateTree(dirPath, rootReal, currentDepth, maxDepth, seen, guard, state, prefix, context) {
8
+ if (currentDepth > maxDepth || state.truncated || guard.checkStopped())
9
+ return '';
10
+ let dirReal;
11
+ try {
12
+ dirReal = await fs.realpath(dirPath);
13
+ }
14
+ catch {
15
+ return `${prefix}└── [Access Denied]\n`;
16
+ }
17
+ if (!isSafeSymlinkTarget(dirReal, rootReal, context))
18
+ return '';
19
+ if (seen.has(dirReal))
20
+ return `${prefix}└── [Symlink Cycle Skipped]\n`;
21
+ if (!guard.enterDir(currentDepth)) {
22
+ state.truncated = true;
23
+ return '';
24
+ }
25
+ seen.add(dirReal);
26
+ let rawItems;
27
+ try {
28
+ rawItems = await fs.readdir(dirPath, { withFileTypes: true });
29
+ }
30
+ catch {
31
+ return `${prefix}└── [Access Denied]\n`;
32
+ }
33
+ const items = rawItems.filter((item) => !shouldExcludeChild(dirReal, item.name));
14
34
  items.sort((a, b) => {
15
- if (a.isDirectory() === b.isDirectory()) return a.name.localeCompare(b.name);
16
- return a.isDirectory() ? -1 : 1;
35
+ const aDir = a.isDirectory();
36
+ const bDir = b.isDirectory();
37
+ if (aDir === bDir)
38
+ return a.name.localeCompare(b.name);
39
+ return aDir ? -1 : 1;
17
40
  });
18
-
41
+ let treeStr = '';
19
42
  for (let i = 0; i < items.length; i++) {
20
- const item = items[i];
21
- // AGI Optimization: Aggressively ignore massive hidden folders
22
- if (item.name === 'node_modules' || item.name === '.git' || item.name === '.gradle') {
23
- continue;
24
- }
25
-
26
- const isLast = i === items.length - 1;
27
- const marker = isLast ? '└── ' : '├── ';
28
- const newPrefix = prefix + (isLast ? ' ' : '│ ');
29
-
30
- let metadata = '';
31
- if (!item.isDirectory()) {
32
- try {
33
- const stats = await fs.stat(path.join(dirPath, item.name));
34
- const sizeKb = Math.round(stats.size / 1024);
35
- metadata = ` (${sizeKb}KB)`;
36
- } catch (e) {}
37
- }
38
-
39
- treeStr += `${prefix}${marker}${item.name}${item.isDirectory() ? '/' : ''}${metadata}\n`;
40
-
41
- if (item.isDirectory()) {
42
- treeStr += await generateTree(path.join(dirPath, item.name), currentDepth + 1, maxDepth, newPrefix);
43
- }
43
+ if (guard.checkStopped() || state.entryCount >= MAX_TREE_ENTRIES) {
44
+ state.truncated = true;
45
+ break;
46
+ }
47
+ const item = items[i];
48
+ const isLast = i === items.length - 1;
49
+ const marker = isLast ? '└── ' : '├── ';
50
+ const newPrefix = prefix + (isLast ? ' ' : '│ ');
51
+ const fullPath = path.join(dirPath, item.name);
52
+ let isDir = item.isDirectory();
53
+ let metadata = '';
54
+ if (item.isSymbolicLink()) {
55
+ try {
56
+ const targetReal = await fs.realpath(fullPath);
57
+ if (!isSafeSymlinkTarget(targetReal, rootReal, context)) {
58
+ treeStr += `${prefix}${marker}${item.name} [symlink outside scope skipped]\n`;
59
+ state.entryCount++;
60
+ continue;
61
+ }
62
+ const stat = await fs.stat(targetReal);
63
+ isDir = stat.isDirectory();
64
+ if (!isDir) {
65
+ metadata = ` (${Math.round(stat.size / 1024)}KB)`;
66
+ }
67
+ }
68
+ catch {
69
+ treeStr += `${prefix}${marker}${item.name} [broken symlink]\n`;
70
+ state.entryCount++;
71
+ continue;
72
+ }
73
+ }
74
+ else if (!isDir) {
75
+ try {
76
+ const stat = await fs.stat(fullPath);
77
+ metadata = ` (${Math.round(stat.size / 1024)}KB)`;
78
+ }
79
+ catch {
80
+ // Ignore stat error on transient files.
81
+ }
82
+ }
83
+ treeStr += `${prefix}${marker}${item.name}${isDir ? '/' : ''}${metadata}\n`;
84
+ state.entryCount++;
85
+ if (isDir && currentDepth < maxDepth) {
86
+ treeStr += await generateTree(fullPath, rootReal, currentDepth + 1, maxDepth, seen, guard, state, newPrefix, context);
87
+ }
44
88
  }
45
- } catch (e) {
46
- treeStr += `${prefix}└── [Access Denied]\n`;
47
- }
48
- return treeStr;
89
+ return treeStr;
49
90
  }
50
-
51
91
  export const fsList = {
52
- schema: {
53
- name: 'fs.list',
54
- description: 'List directory contents as an interactive Tree-Map. Gives spatial awareness of a project layout.',
55
- parameters: {
56
- type: 'object',
57
- properties: {
58
- path: { type: 'string', description: 'Directory to list' },
59
- depth: { type: 'number', description: 'Recursion depth (default 2)' }
60
- },
61
- required: ['path']
62
- }
63
- },
64
- async execute(args) {
65
- try {
66
- const target = args.path.replace(/^~\//, os.homedir() + '/');
67
- const maxDepth = args.depth || 2;
68
-
69
- const tree = await generateTree(target, 1, maxDepth);
70
- const header = `${target}\n`;
71
-
72
- return { success: true, output: header + (tree || ' (Empty or access denied)') };
73
- } catch (e) {
74
- return { success: false, error: e.message };
92
+ schema: {
93
+ name: 'fs.list',
94
+ description: 'List directory contents as an interactive Tree-Map. Enforces depth bounds, entry caps, and workspace/symlink safety.',
95
+ parameters: {
96
+ type: 'object',
97
+ properties: {
98
+ path: { type: 'string', description: 'Directory to list. Defaults to workspace root.' },
99
+ depth: {
100
+ type: 'number',
101
+ description: `Recursion depth (default ${DEFAULT_LIST_DEPTH}, max ${MAX_LIST_DEPTH})`
102
+ }
103
+ },
104
+ required: ['path']
105
+ }
106
+ },
107
+ async execute(args = {}, context) {
108
+ try {
109
+ const target = resolveSearchRoot(args?.path, context);
110
+ const scope = checkSearchScope(target, context);
111
+ if (!scope.allowed || !scope.realPath) {
112
+ return { success: false, error: scope.error || `Directory path not allowed: ${target}` };
113
+ }
114
+ let stat;
115
+ try {
116
+ stat = await fs.stat(scope.realPath);
117
+ }
118
+ catch {
119
+ return { success: false, error: `Directory does not exist: ${target}` };
120
+ }
121
+ if (!stat.isDirectory()) {
122
+ return { success: false, error: `Path is not a directory: ${target}` };
123
+ }
124
+ const rawDepth = typeof args?.depth === 'number' && Number.isFinite(args.depth)
125
+ ? Math.floor(args.depth)
126
+ : DEFAULT_LIST_DEPTH;
127
+ const maxDepth = Math.max(1, Math.min(rawDepth, MAX_LIST_DEPTH));
128
+ const guard = createTraversalGuard({
129
+ timeoutMs: 10_000,
130
+ maxDirs: 2_000,
131
+ maxDepth,
132
+ signal: context?.signal
133
+ });
134
+ const state = { entryCount: 0, truncated: false };
135
+ const tree = await generateTree(scope.realPath, scope.realPath, 1, maxDepth, new Set(), guard, state, '', context);
136
+ if (guard.stopReason === 'aborted') {
137
+ return { success: false, aborted: true, error: 'Directory listing aborted.' };
138
+ }
139
+ const notices = [];
140
+ if (state.entryCount >= MAX_TREE_ENTRIES) {
141
+ notices.push(`truncated at ${MAX_TREE_ENTRIES} entries`);
142
+ }
143
+ if (guard.stopReason) {
144
+ notices.push(guard.stopNotice());
145
+ }
146
+ const footer = notices.length > 0 ? `\n[${notices.join('; ')}]` : '';
147
+ const header = `${target}\n`;
148
+ return {
149
+ success: true,
150
+ output: header + (tree || ' (Empty or access denied)') + footer
151
+ };
152
+ }
153
+ catch (error) {
154
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
155
+ }
75
156
  }
76
- }
77
157
  };
@@ -0,0 +1,224 @@
1
+ import type { Dirent } from 'node:fs';
2
+ import fs from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import {
5
+ checkSearchScope,
6
+ createTraversalGuard,
7
+ isSafeSymlinkTarget,
8
+ resolveSearchRoot,
9
+ shouldExcludeChild,
10
+ type SearchExecutionContext,
11
+ type TraversalGuard
12
+ } from './walkerSafety.js';
13
+
14
+ const DEFAULT_LIST_DEPTH = 2;
15
+ const MAX_LIST_DEPTH = 8;
16
+ const MAX_TREE_ENTRIES = 500;
17
+
18
+ export interface FsListArgs {
19
+ path?: string | undefined;
20
+ depth?: number | undefined;
21
+ }
22
+
23
+ export interface FsListResult {
24
+ success: boolean;
25
+ output?: string;
26
+ error?: string;
27
+ aborted?: boolean;
28
+ }
29
+
30
+ interface TreeState {
31
+ entryCount: number;
32
+ truncated: boolean;
33
+ }
34
+
35
+ async function generateTree(
36
+ dirPath: string,
37
+ rootReal: string,
38
+ currentDepth: number,
39
+ maxDepth: number,
40
+ seen: Set<string>,
41
+ guard: TraversalGuard,
42
+ state: TreeState,
43
+ prefix: string,
44
+ context?: SearchExecutionContext
45
+ ): Promise<string> {
46
+ if (currentDepth > maxDepth || state.truncated || guard.checkStopped()) return '';
47
+
48
+ let dirReal: string;
49
+ try {
50
+ dirReal = await fs.realpath(dirPath);
51
+ } catch {
52
+ return `${prefix}└── [Access Denied]\n`;
53
+ }
54
+ if (!isSafeSymlinkTarget(dirReal, rootReal, context)) return '';
55
+ if (seen.has(dirReal)) return `${prefix}└── [Symlink Cycle Skipped]\n`;
56
+ if (!guard.enterDir(currentDepth)) {
57
+ state.truncated = true;
58
+ return '';
59
+ }
60
+ seen.add(dirReal);
61
+
62
+ let rawItems: Dirent[];
63
+ try {
64
+ rawItems = await fs.readdir(dirPath, { withFileTypes: true });
65
+ } catch {
66
+ return `${prefix}└── [Access Denied]\n`;
67
+ }
68
+
69
+ const items = rawItems.filter((item) => !shouldExcludeChild(dirReal, item.name));
70
+ items.sort((a, b) => {
71
+ const aDir = a.isDirectory();
72
+ const bDir = b.isDirectory();
73
+ if (aDir === bDir) return a.name.localeCompare(b.name);
74
+ return aDir ? -1 : 1;
75
+ });
76
+
77
+ let treeStr = '';
78
+ for (let i = 0; i < items.length; i++) {
79
+ if (guard.checkStopped() || state.entryCount >= MAX_TREE_ENTRIES) {
80
+ state.truncated = true;
81
+ break;
82
+ }
83
+
84
+ const item = items[i]!;
85
+ const isLast = i === items.length - 1;
86
+ const marker = isLast ? '└── ' : '├── ';
87
+ const newPrefix = prefix + (isLast ? ' ' : '│ ');
88
+ const fullPath = path.join(dirPath, item.name);
89
+
90
+ let isDir = item.isDirectory();
91
+ let metadata = '';
92
+ if (item.isSymbolicLink()) {
93
+ try {
94
+ const targetReal = await fs.realpath(fullPath);
95
+ if (!isSafeSymlinkTarget(targetReal, rootReal, context)) {
96
+ treeStr += `${prefix}${marker}${item.name} [symlink outside scope skipped]\n`;
97
+ state.entryCount++;
98
+ continue;
99
+ }
100
+ const stat = await fs.stat(targetReal);
101
+ isDir = stat.isDirectory();
102
+ if (!isDir) {
103
+ metadata = ` (${Math.round(stat.size / 1024)}KB)`;
104
+ }
105
+ } catch {
106
+ treeStr += `${prefix}${marker}${item.name} [broken symlink]\n`;
107
+ state.entryCount++;
108
+ continue;
109
+ }
110
+ } else if (!isDir) {
111
+ try {
112
+ const stat = await fs.stat(fullPath);
113
+ metadata = ` (${Math.round(stat.size / 1024)}KB)`;
114
+ } catch {
115
+ // Ignore stat error on transient files.
116
+ }
117
+ }
118
+
119
+ treeStr += `${prefix}${marker}${item.name}${isDir ? '/' : ''}${metadata}\n`;
120
+ state.entryCount++;
121
+
122
+ if (isDir && currentDepth < maxDepth) {
123
+ treeStr += await generateTree(
124
+ fullPath,
125
+ rootReal,
126
+ currentDepth + 1,
127
+ maxDepth,
128
+ seen,
129
+ guard,
130
+ state,
131
+ newPrefix,
132
+ context
133
+ );
134
+ }
135
+ }
136
+
137
+ return treeStr;
138
+ }
139
+
140
+ export const fsList = {
141
+ schema: {
142
+ name: 'fs.list',
143
+ description:
144
+ 'List directory contents as an interactive Tree-Map. Enforces depth bounds, entry caps, and workspace/symlink safety.',
145
+ parameters: {
146
+ type: 'object',
147
+ properties: {
148
+ path: { type: 'string', description: 'Directory to list. Defaults to workspace root.' },
149
+ depth: {
150
+ type: 'number',
151
+ description: `Recursion depth (default ${DEFAULT_LIST_DEPTH}, max ${MAX_LIST_DEPTH})`
152
+ }
153
+ },
154
+ required: ['path']
155
+ }
156
+ },
157
+
158
+ async execute(
159
+ args: FsListArgs = {},
160
+ context?: SearchExecutionContext
161
+ ): Promise<FsListResult> {
162
+ try {
163
+ const target = resolveSearchRoot(args?.path, context);
164
+ const scope = checkSearchScope(target, context);
165
+ if (!scope.allowed || !scope.realPath) {
166
+ return { success: false, error: scope.error || `Directory path not allowed: ${target}` };
167
+ }
168
+
169
+ let stat;
170
+ try {
171
+ stat = await fs.stat(scope.realPath);
172
+ } catch {
173
+ return { success: false, error: `Directory does not exist: ${target}` };
174
+ }
175
+ if (!stat.isDirectory()) {
176
+ return { success: false, error: `Path is not a directory: ${target}` };
177
+ }
178
+
179
+ const rawDepth = typeof args?.depth === 'number' && Number.isFinite(args.depth)
180
+ ? Math.floor(args.depth)
181
+ : DEFAULT_LIST_DEPTH;
182
+ const maxDepth = Math.max(1, Math.min(rawDepth, MAX_LIST_DEPTH));
183
+ const guard = createTraversalGuard({
184
+ timeoutMs: 10_000,
185
+ maxDirs: 2_000,
186
+ maxDepth,
187
+ signal: context?.signal
188
+ });
189
+ const state: TreeState = { entryCount: 0, truncated: false };
190
+ const tree = await generateTree(
191
+ scope.realPath,
192
+ scope.realPath,
193
+ 1,
194
+ maxDepth,
195
+ new Set(),
196
+ guard,
197
+ state,
198
+ '',
199
+ context
200
+ );
201
+
202
+ if (guard.stopReason === 'aborted') {
203
+ return { success: false, aborted: true, error: 'Directory listing aborted.' };
204
+ }
205
+
206
+ const notices: string[] = [];
207
+ if (state.entryCount >= MAX_TREE_ENTRIES) {
208
+ notices.push(`truncated at ${MAX_TREE_ENTRIES} entries`);
209
+ }
210
+ if (guard.stopReason) {
211
+ notices.push(guard.stopNotice());
212
+ }
213
+ const footer = notices.length > 0 ? `\n[${notices.join('; ')}]` : '';
214
+ const header = `${target}\n`;
215
+
216
+ return {
217
+ success: true,
218
+ output: header + (tree || ' (Empty or access denied)') + footer
219
+ };
220
+ } catch (error: unknown) {
221
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
222
+ }
223
+ }
224
+ };