@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.
- package/package.json +1 -1
- package/src/agentLoop.js +116 -26
- package/src/agentLoop.ts +121 -26
- package/src/contextManager.js +2 -0
- package/src/decideTools.js +9 -0
- package/src/decideTools.ts +9 -0
- package/src/decisionPolicy.js +119 -1
- package/src/decisionPolicy.ts +129 -1
- package/src/historyPruner.js +16 -1
- package/src/historyPruner.ts +13 -1
- package/src/sandbox.js +33 -7
- package/src/skills/skillResourceSafety.js +70 -0
- package/src/skills/skillResourceSafety.ts +77 -0
- package/src/smartToolsUse.js +28 -11
- package/src/smartToolsUse.ts +34 -11
- package/src/taskShape.js +28 -13
- package/src/taskShape.ts +31 -12
- package/src/tools/codeSearcher.js +349 -210
- package/src/tools/codeSearcher.ts +408 -0
- package/src/tools/codebaseSearch.js +182 -68
- package/src/tools/codebaseSearch.ts +221 -0
- package/src/tools/deferredMcpSearch.js +94 -40
- package/src/tools/deferredMcpSearch.ts +89 -30
- package/src/tools/fsFind.js +180 -115
- package/src/tools/fsFind.ts +228 -0
- package/src/tools/fsList.js +149 -69
- package/src/tools/fsList.ts +224 -0
- package/src/tools/fsSearch.js +192 -189
- package/src/tools/fsSearch.ts +241 -0
- package/src/tools/readNativeSkill.js +34 -42
- package/src/tools/readNativeSkill.ts +38 -36
- package/src/tools/searchRelevance.js +70 -0
- package/src/tools/searchRelevance.ts +72 -0
- package/src/tools/toolSelfRepair.js +56 -20
- package/src/tools/toolSelfRepair.ts +58 -19
- package/src/tools/walkerSafety.js +445 -0
- package/src/tools/walkerSafety.ts +516 -0
- package/src/tui/composerGeometry.js +45 -0
- package/src/tui/composerGeometry.ts +80 -0
- package/src/tui/homeView.js +10 -6
- package/src/tui/homeView.ts +10 -6
- package/src/tui/lineEditor.js +8 -0
- package/src/tui/prompt.js +9 -4
- package/src/userSkills.js +183 -203
- package/src/userSkills.ts +301 -0
- package/src/userSkills.d.ts +0 -26
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { causyneHome } from './utils/paths.js';
|
|
4
|
+
import {
|
|
5
|
+
assertSkillFileSafe,
|
|
6
|
+
assertSkillTreeSafe,
|
|
7
|
+
writeSkillFileSafely
|
|
8
|
+
} from './skills/skillResourceSafety.js';
|
|
9
|
+
|
|
10
|
+
export interface UserSkill {
|
|
11
|
+
domain: string;
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
content: string;
|
|
16
|
+
source: 'user';
|
|
17
|
+
path: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Bounds. Every one of these protects the context window, which is the resource
|
|
21
|
+
// a skill library actually spends.
|
|
22
|
+
export const MAX_USER_SKILLS = 200;
|
|
23
|
+
export const MAX_SKILL_BYTES = 32 * 1024;
|
|
24
|
+
export const MAX_DESCRIPTION = 400;
|
|
25
|
+
|
|
26
|
+
// A skill id and a domain are path segments. Lowercase, digits and hyphens is
|
|
27
|
+
// the whole alphabet — which makes traversal, drive letters, device names,
|
|
28
|
+
// leading dots and spaces unrepresentable rather than merely filtered.
|
|
29
|
+
const SEGMENT = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
30
|
+
|
|
31
|
+
// Windows reserves these as device names. A directory called `con` or `aux`
|
|
32
|
+
// cannot be created there, and `nul` silently discards what is written to it —
|
|
33
|
+
// so a skill named after one would work on the author's Mac and fail, or vanish,
|
|
34
|
+
// for a Windows user. Vision section 3: never assume the developer's own OS.
|
|
35
|
+
const RESERVED = new Set<string>([
|
|
36
|
+
'con', 'prn', 'aux', 'nul',
|
|
37
|
+
...Array.from({ length: 9 }, (_, index) => `com${index + 1}`),
|
|
38
|
+
...Array.from({ length: 9 }, (_, index) => `lpt${index + 1}`)
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
export function userSkillsRoot(): string {
|
|
42
|
+
return path.join(causyneHome(), 'skills');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Is this a usable path segment? Returns a reason when it is not. */
|
|
46
|
+
export function validateSegment(value: unknown, what = 'name'): string | null {
|
|
47
|
+
const text = String(value == null ? '' : value);
|
|
48
|
+
if (!text) return `${what} is required`;
|
|
49
|
+
if (!SEGMENT.test(text)) {
|
|
50
|
+
return `${what} must be lowercase letters, digits and hyphens, starting with a letter or digit `
|
|
51
|
+
+ `(got ${JSON.stringify(text.slice(0, 40))})`;
|
|
52
|
+
}
|
|
53
|
+
if (RESERVED.has(text)) {
|
|
54
|
+
return `${what} ${JSON.stringify(text)} is a reserved device name on Windows — `
|
|
55
|
+
+ 'a skill by that name would fail there. Pick another.';
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read `domain` and `name` out of a `domain/name` id.
|
|
62
|
+
*
|
|
63
|
+
* Rejects anything with a different shape rather than guessing, because a
|
|
64
|
+
* guessed split is how a traversal segment reaches a join().
|
|
65
|
+
*/
|
|
66
|
+
export function parseSkillId(id: unknown):
|
|
67
|
+
| { domain: string; name: string; error?: never }
|
|
68
|
+
| { domain?: never; name?: never; error: string } {
|
|
69
|
+
const parts = String(id == null ? '' : id).split('/');
|
|
70
|
+
if (parts.length !== 2) {
|
|
71
|
+
return { error: 'skill id must be "domain/name", for example team/release-checklist' };
|
|
72
|
+
}
|
|
73
|
+
const [domain = '', name = ''] = parts;
|
|
74
|
+
const domainError = validateSegment(domain, 'domain');
|
|
75
|
+
if (domainError) return { error: domainError };
|
|
76
|
+
const nameError = validateSegment(name, 'skill name');
|
|
77
|
+
if (nameError) return { error: nameError };
|
|
78
|
+
return { domain, name };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Strip control characters and collapse a description onto one line. */
|
|
82
|
+
function cleanDescription(value: unknown): string {
|
|
83
|
+
return String(value == null ? '' : value)
|
|
84
|
+
// eslint-disable-next-line no-control-regex
|
|
85
|
+
.replace(/[\x00-\x1f\x7f]/g, ' ')
|
|
86
|
+
.replace(/\s+/g, ' ')
|
|
87
|
+
.trim()
|
|
88
|
+
.slice(0, MAX_DESCRIPTION);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Remove control bytes from a body while keeping newlines and tabs. */
|
|
92
|
+
function cleanBody(value: unknown): string {
|
|
93
|
+
// eslint-disable-next-line no-control-regex
|
|
94
|
+
return String(value == null ? '' : value).replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseFrontmatter(content: string): { name?: string; description?: string } {
|
|
98
|
+
const match = content.match(/^---\n([\s\S]*?)\n---/);
|
|
99
|
+
if (!match) return {};
|
|
100
|
+
const block = match[1] ?? '';
|
|
101
|
+
const name = block.match(/^name:\s*(.+)$/m);
|
|
102
|
+
const description = block.match(/^description:\s*(.+)$/m);
|
|
103
|
+
return {
|
|
104
|
+
name: name?.[1] ? name[1].trim() : '',
|
|
105
|
+
description: description?.[1] ? description[1].trim() : ''
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Every user skill on disk, validated.
|
|
111
|
+
*
|
|
112
|
+
* Never throws: a broken file is skipped and reported, because one bad skill
|
|
113
|
+
* must not stop a session from starting. Returns { skills, problems }.
|
|
114
|
+
*/
|
|
115
|
+
export function loadUserSkills(root = userSkillsRoot()): { skills: UserSkill[]; problems: string[] } {
|
|
116
|
+
const skills: UserSkill[] = [];
|
|
117
|
+
const problems: string[] = [];
|
|
118
|
+
|
|
119
|
+
let domains: string[];
|
|
120
|
+
try {
|
|
121
|
+
if (!fs.existsSync(root)) return { skills, problems };
|
|
122
|
+
domains = fs.readdirSync(root, { withFileTypes: true })
|
|
123
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith('.'))
|
|
124
|
+
.map((entry) => entry.name);
|
|
125
|
+
} catch (e: unknown) {
|
|
126
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
127
|
+
return { skills, problems: [`could not read ${root}: ${msg}`] };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const domain of domains) {
|
|
131
|
+
if (validateSegment(domain, 'domain')) {
|
|
132
|
+
problems.push(`skipped domain ${JSON.stringify(domain)}: not a valid name`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
let names: string[];
|
|
136
|
+
try {
|
|
137
|
+
names = fs.readdirSync(path.join(root, domain), { withFileTypes: true })
|
|
138
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith('.'))
|
|
139
|
+
.map((entry) => entry.name);
|
|
140
|
+
} catch { continue; }
|
|
141
|
+
|
|
142
|
+
for (const name of names) {
|
|
143
|
+
if (skills.length >= MAX_USER_SKILLS) {
|
|
144
|
+
problems.push(`stopped at ${MAX_USER_SKILLS} skills — the rest were not loaded`);
|
|
145
|
+
return { skills, problems };
|
|
146
|
+
}
|
|
147
|
+
if (validateSegment(name, 'skill name')) {
|
|
148
|
+
problems.push(`skipped ${domain}/${name}: not a valid name`);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const file = path.join(root, domain, name, 'SKILL.md');
|
|
153
|
+
let content: string;
|
|
154
|
+
try {
|
|
155
|
+
assertSkillTreeSafe(root, domain, name);
|
|
156
|
+
// Refuse a symlink outright: it is the one way a file inside this
|
|
157
|
+
// directory can point at something outside it.
|
|
158
|
+
const stat = fs.lstatSync(file);
|
|
159
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
160
|
+
problems.push(`skipped ${domain}/${name}: not a regular file`);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (stat.size > MAX_SKILL_BYTES) {
|
|
164
|
+
problems.push(`skipped ${domain}/${name}: ${(stat.size / 1024).toFixed(0)} KB exceeds the `
|
|
165
|
+
+ `${MAX_SKILL_BYTES / 1024} KB limit — a skill this large would crowd out the task`);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
content = fs.readFileSync(file, 'utf8');
|
|
169
|
+
} catch { continue; }
|
|
170
|
+
|
|
171
|
+
const meta = parseFrontmatter(content);
|
|
172
|
+
const description = cleanDescription(meta.description);
|
|
173
|
+
if (!description) {
|
|
174
|
+
problems.push(`skipped ${domain}/${name}: no description in the frontmatter — `
|
|
175
|
+
+ 'the description is how the agent decides when the skill applies');
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
skills.push({
|
|
180
|
+
domain,
|
|
181
|
+
id: name,
|
|
182
|
+
name: meta.name || name,
|
|
183
|
+
description,
|
|
184
|
+
content: cleanBody(content),
|
|
185
|
+
source: 'user',
|
|
186
|
+
path: file
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { skills, problems };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The starting point a new skill is written from. */
|
|
194
|
+
export function skillTemplate(_domain: string, name: string, description = ''): string {
|
|
195
|
+
const title = name.split('-').map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
|
|
196
|
+
return `---
|
|
197
|
+
name: ${name}
|
|
198
|
+
description: ${description || 'TODO — one line: what this does, and WHEN to reach for it.'}
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
# ${title}
|
|
202
|
+
|
|
203
|
+
## Workflow
|
|
204
|
+
|
|
205
|
+
1. First step.
|
|
206
|
+
- What to do, and the detail that is easy to get wrong.
|
|
207
|
+
- **Done when:** something you can actually check.
|
|
208
|
+
|
|
209
|
+
2. Second step.
|
|
210
|
+
- **Done when:** something you can actually check.
|
|
211
|
+
|
|
212
|
+
## Verification
|
|
213
|
+
|
|
214
|
+
- How to confirm the whole thing worked.
|
|
215
|
+
`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Write a user skill to disk.
|
|
220
|
+
*
|
|
221
|
+
* Refuses to overwrite unless asked, so neither a user nor an agent can silently
|
|
222
|
+
* replace a skill someone spent time on.
|
|
223
|
+
*/
|
|
224
|
+
export function writeUserSkill(
|
|
225
|
+
{ domain, name, content, overwrite = false }: {
|
|
226
|
+
domain: string;
|
|
227
|
+
name: string;
|
|
228
|
+
content: string;
|
|
229
|
+
overwrite?: boolean;
|
|
230
|
+
},
|
|
231
|
+
root = userSkillsRoot()
|
|
232
|
+
): { ok: boolean; path?: string; error?: string } {
|
|
233
|
+
const domainError = validateSegment(domain, 'domain');
|
|
234
|
+
if (domainError) return { ok: false, error: domainError };
|
|
235
|
+
const nameError = validateSegment(name, 'skill name');
|
|
236
|
+
if (nameError) return { ok: false, error: nameError };
|
|
237
|
+
|
|
238
|
+
const body = cleanBody(content);
|
|
239
|
+
if (Buffer.byteLength(body, 'utf8') > MAX_SKILL_BYTES) {
|
|
240
|
+
return { ok: false, error: `skill exceeds the ${MAX_SKILL_BYTES / 1024} KB limit` };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const meta = parseFrontmatter(body);
|
|
244
|
+
if (!cleanDescription(meta.description)) {
|
|
245
|
+
return {
|
|
246
|
+
ok: false,
|
|
247
|
+
error: "the frontmatter needs a `description:` line — it is what the agent reads to decide whether the skill applies, so a skill without one can never be selected"
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const directory = path.join(root, domain, name);
|
|
252
|
+
const file = path.join(directory, 'SKILL.md');
|
|
253
|
+
|
|
254
|
+
// Belt and braces: the resolved path must still be inside the root after every
|
|
255
|
+
// segment has been joined. Validation above already makes this unreachable.
|
|
256
|
+
const resolvedRoot = path.resolve(root);
|
|
257
|
+
const resolvedFile = path.resolve(file);
|
|
258
|
+
if (resolvedFile !== resolvedRoot && !resolvedFile.startsWith(resolvedRoot + path.sep)) {
|
|
259
|
+
return { ok: false, error: 'refusing to write outside the skills directory' };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!overwrite && fs.existsSync(file)) {
|
|
263
|
+
return { ok: false, error: `${domain}/${name} already exists — pass overwrite to replace it` };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
// Causyne Skills Safety v1: lexical path checks do not stop an existing
|
|
268
|
+
// symlinked parent directory or SKILL.md symlink from redirecting writes.
|
|
269
|
+
const writtenPath = writeSkillFileSafely(root, domain, name, 'SKILL.md', body, overwrite);
|
|
270
|
+
return { ok: true, path: writtenPath };
|
|
271
|
+
} catch (e: unknown) {
|
|
272
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
273
|
+
return { ok: false, error: `could not write ${file}: ${msg}` };
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Remove a user skill. Compiled skills are not removable — they are the build. */
|
|
278
|
+
export function deleteUserSkill(
|
|
279
|
+
domain: string,
|
|
280
|
+
name: string,
|
|
281
|
+
root = userSkillsRoot()
|
|
282
|
+
): { ok: boolean; path?: string; error?: string } {
|
|
283
|
+
const domainError = validateSegment(domain, 'domain');
|
|
284
|
+
if (domainError) return { ok: false, error: domainError };
|
|
285
|
+
const nameError = validateSegment(name, 'skill name');
|
|
286
|
+
if (nameError) return { ok: false, error: nameError };
|
|
287
|
+
|
|
288
|
+
const directory = path.join(root, domain, name);
|
|
289
|
+
if (!fs.existsSync(path.join(directory, 'SKILL.md'))) {
|
|
290
|
+
return { ok: false, error: `${domain}/${name} is not a user skill` };
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
assertSkillTreeSafe(root, domain, name);
|
|
294
|
+
assertSkillFileSafe(path.join(directory, 'SKILL.md'), false);
|
|
295
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
296
|
+
} catch (e: unknown) {
|
|
297
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
298
|
+
return { ok: false, error: `could not remove ${directory}: ${msg}` };
|
|
299
|
+
}
|
|
300
|
+
return { ok: true, path: directory };
|
|
301
|
+
}
|
package/src/userSkills.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/** Types for the existing JavaScript user skill store during the TS migration. */
|
|
2
|
-
export interface UserSkill {
|
|
3
|
-
domain: string;
|
|
4
|
-
id: string;
|
|
5
|
-
name: string;
|
|
6
|
-
description: string;
|
|
7
|
-
content: string;
|
|
8
|
-
source: 'user';
|
|
9
|
-
path: string;
|
|
10
|
-
}
|
|
11
|
-
export const MAX_USER_SKILLS: number;
|
|
12
|
-
export const MAX_SKILL_BYTES: number;
|
|
13
|
-
export const MAX_DESCRIPTION: number;
|
|
14
|
-
export function userSkillsRoot(): string;
|
|
15
|
-
export function validateSegment(value: unknown, what?: string): string | null;
|
|
16
|
-
export function parseSkillId(id: unknown):
|
|
17
|
-
| { domain: string; name: string; error?: never }
|
|
18
|
-
| { domain?: never; name?: never; error: string };
|
|
19
|
-
export function loadUserSkills(root?: string): { skills: UserSkill[]; problems: string[] };
|
|
20
|
-
export function skillTemplate(domain: string, name: string, description?: string): string;
|
|
21
|
-
export function writeUserSkill(input: {
|
|
22
|
-
domain: string; name: string; content: string; overwrite?: boolean;
|
|
23
|
-
}, root?: string): { ok: boolean; path?: string; error?: string };
|
|
24
|
-
export function deleteUserSkill(domain: string, name: string, root?: string): {
|
|
25
|
-
ok: boolean; path?: string; error?: string;
|
|
26
|
-
};
|