@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
package/src/decisionPolicy.ts
CHANGED
|
@@ -165,8 +165,136 @@ export function assessDecisionRequirement(prompt: string): DecisionRequirement {
|
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
+
function extractPreviousCueWord(beforeRaw: string): string {
|
|
169
|
+
let end = beforeRaw.length;
|
|
170
|
+
while (end > 0) {
|
|
171
|
+
const c = beforeRaw[end - 1]!;
|
|
172
|
+
if (c === ':' || c === '=' || /\s/.test(c)) end--;
|
|
173
|
+
else break;
|
|
174
|
+
}
|
|
175
|
+
let start = end;
|
|
176
|
+
while (start > 0) {
|
|
177
|
+
const c = beforeRaw[start - 1]!;
|
|
178
|
+
if (/\s/.test(c) || c === '(') break;
|
|
179
|
+
start--;
|
|
180
|
+
}
|
|
181
|
+
return beforeRaw.slice(start, end).toLowerCase();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function stripTokenEdgePunctuation(token: string): string {
|
|
185
|
+
const leading = new Set(['`', '"', "'", '(']);
|
|
186
|
+
const trailing = new Set(['`', '"', "'", ',', '.', ';', ':', '!', '?', ')']);
|
|
187
|
+
let start = 0;
|
|
188
|
+
let end = token.length;
|
|
189
|
+
while (start < end && leading.has(token[start]!)) start++;
|
|
190
|
+
while (end > start && trailing.has(token[end - 1]!)) end--;
|
|
191
|
+
return token.slice(start, end);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function redactDelimitedLiteralAfterKeyword(text: string): string {
|
|
195
|
+
const cues = new Set(['skill', 'skill_id', 'fs_read_skill', 'fs_read_skil', 'read_skill', 'matching', 'pattern', 'query']);
|
|
196
|
+
let out = '';
|
|
197
|
+
let i = 0;
|
|
198
|
+
while (i < text.length) {
|
|
199
|
+
const ch = text[i]!;
|
|
200
|
+
if (ch === '`' || ch === '"') {
|
|
201
|
+
const lastWord = extractPreviousCueWord(out);
|
|
202
|
+
const close = text.indexOf(ch, i + 1);
|
|
203
|
+
if (close !== -1 && cues.has(lastWord)) {
|
|
204
|
+
out += '<lookup_literal>';
|
|
205
|
+
i = close + 1;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (ch === "'" && (i === 0 || /[\s:=(\[]/.test(text[i - 1]!))) {
|
|
210
|
+
const lastWord = extractPreviousCueWord(out);
|
|
211
|
+
const close = text.indexOf("'", i + 1);
|
|
212
|
+
if (close !== -1 && cues.has(lastWord)) {
|
|
213
|
+
out += '<lookup_literal>';
|
|
214
|
+
i = close + 1;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
out += ch;
|
|
219
|
+
i++;
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const KNOWN_FILE_EXT_RE =
|
|
225
|
+
/\.(?:ts|tsx|js|jsx|mjs|cjs|py|go|rs|java|kt|kts|swift|c|cc|cpp|h|hpp|json|yaml|yml|toml|xml|html|htm|css|scss|md|txt|sql|sh|bash|gradle|properties|apk|aab|dex|so|dylib|dll|exe|wasm|asar|png|jpe?g|webp|pdf|blend\d*|psd|aep|prproj)$/i;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Redact inline skill identifiers and tool_search query/regex literals in O(n) time
|
|
229
|
+
* so intent classifiers never mistake skill names (e.g. `mobile-development/android-build-optimization`)
|
|
230
|
+
* or search patterns (e.g. `android/.*gradle`) for software engineering/build commands.
|
|
231
|
+
*/
|
|
232
|
+
export function redactSkillAndQueryLiterals(raw: string): string {
|
|
233
|
+
const input = String(raw ?? '');
|
|
234
|
+
if (!input.trim()) return input;
|
|
235
|
+
const hasSkillOrSearchContext =
|
|
236
|
+
/\b(?:skills?|fs_read_skill|fs_read_skil|read_skill|tool_search|tool\.search|tool_deferred_mcp_search|capabilities\s+matching)\b/i.test(input);
|
|
237
|
+
if (!hasSkillOrSearchContext) return input;
|
|
238
|
+
|
|
239
|
+
const afterQuoted = redactDelimitedLiteralAfterKeyword(input);
|
|
240
|
+
return afterQuoted.replace(/\S+/g, (token) => {
|
|
241
|
+
const core = stripTokenEdgePunctuation(token);
|
|
242
|
+
if (!core || core === '<lookup_literal>') return token;
|
|
243
|
+
const isFileLikePath = /^(?:\.{0,2}\/|~\/|https?:\/\/)/i.test(core) || KNOWN_FILE_EXT_RE.test(core);
|
|
244
|
+
if (isFileLikePath) return token;
|
|
245
|
+
const isSlashSkillOrPattern = core.includes('/') && /[a-z0-9]/i.test(core);
|
|
246
|
+
const hasRegexMeta = /[*+?^$[\]\\]/.test(core) && /[a-z0-9]/i.test(core);
|
|
247
|
+
if (isSlashSkillOrPattern || hasRegexMeta) {
|
|
248
|
+
return '<lookup_literal>';
|
|
249
|
+
}
|
|
250
|
+
return token;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const NON_LOOKUP_ACTION_RE =
|
|
255
|
+
/\b(?:create|write|edit|modify|update|delete|remove|rename|move|build|make|implement|refactor|fix|debug|patch|install|deploy|scaffold|generate|compile|execute|decompile|disassemble|launch|apply)\b/;
|
|
256
|
+
|
|
257
|
+
export function isPureSkillOrCapabilityLookup(raw: string): boolean {
|
|
258
|
+
const text = String(raw ?? '').trim();
|
|
259
|
+
if (!text) return false;
|
|
260
|
+
const cleaned = redactSkillAndQueryLiterals(text).toLowerCase().trim();
|
|
261
|
+
if (!cleaned) return false;
|
|
262
|
+
|
|
263
|
+
if (
|
|
264
|
+
NON_LOOKUP_ACTION_RE.test(cleaned) ||
|
|
265
|
+
/\b(?:run|execute)\s+(?:tests?|commands?|scripts?|code)\b/.test(cleaned) ||
|
|
266
|
+
KNOWN_FILE_EXT_RE.test(cleaned)
|
|
267
|
+
) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const isSkillReadRequest =
|
|
272
|
+
/\b(?:fs_read_skill|fs_read_skil|read_skill|read_skil|skill_read)\b/i.test(text) ||
|
|
273
|
+
/\b(?:read|load|open|show|get|inspect|fetch|view)\s+(?:the\s+|this\s+|a\s+|compiled\s+|native\s+|user\s+)?skills?\b/i.test(text) ||
|
|
274
|
+
/\bskill(?:_id|\s+id)\s*[:=]/i.test(text) ||
|
|
275
|
+
/^\/skills?\b/i.test(text);
|
|
276
|
+
|
|
277
|
+
const isSkillInventoryQuestion =
|
|
278
|
+
/\b(?:what|which|list|show|available|browse)\s+(?:all\s+|the\s+|any\s+)?skills?\b/i.test(text) ||
|
|
279
|
+
/\bwhat\s+skills?\s+(?:do\s+you\s+have|are\s+available)\b/i.test(text) ||
|
|
280
|
+
/\bskills?\s+(?:available|catalog|inventory|list)\b/i.test(text);
|
|
281
|
+
|
|
282
|
+
const isToolSearchLookup =
|
|
283
|
+
/\b(?:tool_search|tool\.search|tool_deferred_mcp_search)\b/i.test(text) ||
|
|
284
|
+
(/\b(?:search|discover|find|lookup|list|show)\b/i.test(text) &&
|
|
285
|
+
/\bcapabilit(?:y|ies)\b|\bcapability\s+packs?\b/i.test(text));
|
|
286
|
+
|
|
287
|
+
return isSkillReadRequest || isSkillInventoryQuestion || isToolSearchLookup;
|
|
288
|
+
}
|
|
289
|
+
|
|
168
290
|
export function requiresImplementationPlan(prompt: string, archetype = '', confidence = ''): boolean {
|
|
169
|
-
|
|
291
|
+
if (archetype === 'skill_lookup' || isPureSkillOrCapabilityLookup(prompt)) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
const text = redactSkillAndQueryLiterals(String(prompt ?? '')).toLowerCase();
|
|
295
|
+
if (archetype === 'research' && !/\b(?:implement|build|create|migrate|rewrite|refactor|integrate|write|edit|fix|modify|delete|remove|update)\b/.test(text)) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
170
298
|
// A classifier can label any "create" request build_app. A single plain
|
|
171
299
|
// text/data-file operation needs no checklist or rejected mutation round trip.
|
|
172
300
|
const namedFiles = new Set([...text.matchAll(/\b[\w./-]+\.(?:txt|md|csv|json)\b/g)].map(match => match[0]));
|
package/src/historyPruner.js
CHANGED
|
@@ -59,7 +59,16 @@ export function pruneExecutedToolArguments(messages, options = {}) {
|
|
|
59
59
|
* not make a 60 KB result safe to resend in full on every model call.
|
|
60
60
|
*/
|
|
61
61
|
export function shortenOversizedToolObservations(messages, options = {}) {
|
|
62
|
-
const
|
|
62
|
+
const explicitThreshold = options.threshold !== undefined ? Math.max(1, options.threshold) : undefined;
|
|
63
|
+
const thresholdFor = (toolName) => {
|
|
64
|
+
if (explicitThreshold !== undefined)
|
|
65
|
+
return explicitThreshold;
|
|
66
|
+
if (toolName === 'fs_read_skill')
|
|
67
|
+
return 16000;
|
|
68
|
+
if (toolName === 'tool_search')
|
|
69
|
+
return 8000;
|
|
70
|
+
return 2500;
|
|
71
|
+
};
|
|
63
72
|
let pruned = 0;
|
|
64
73
|
let reclaimedChars = 0;
|
|
65
74
|
let spillFailures = 0;
|
|
@@ -68,8 +77,14 @@ export function shortenOversizedToolObservations(messages, options = {}) {
|
|
|
68
77
|
if (!message || message.role !== 'tool' || typeof message.content !== 'string')
|
|
69
78
|
continue;
|
|
70
79
|
const original = message.content;
|
|
80
|
+
const threshold = thresholdFor(message.name);
|
|
71
81
|
if (original.length <= threshold || original.startsWith(MARKER))
|
|
72
82
|
continue;
|
|
83
|
+
// Do not truncate an observation a second time when ToolOutputStore has
|
|
84
|
+
// already persisted the complete payload and added its recovery notice.
|
|
85
|
+
if (message.spillPath && original.includes('Causyne ToolOutputStore:') && original.length <= Math.max(threshold + 1000, 4000)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
73
88
|
let spillPath = message.spillPath;
|
|
74
89
|
if (!spillPath) {
|
|
75
90
|
const spill = spillToolResult(original, {
|
package/src/historyPruner.ts
CHANGED
|
@@ -70,7 +70,13 @@ export function shortenOversizedToolObservations(
|
|
|
70
70
|
messages: ToolObservation[],
|
|
71
71
|
options: { threshold?: number; taskId?: string } = {}
|
|
72
72
|
): ObservationPruneResult {
|
|
73
|
-
const
|
|
73
|
+
const explicitThreshold = options.threshold !== undefined ? Math.max(1, options.threshold) : undefined;
|
|
74
|
+
const thresholdFor = (toolName?: string): number => {
|
|
75
|
+
if (explicitThreshold !== undefined) return explicitThreshold;
|
|
76
|
+
if (toolName === 'fs_read_skill') return 16000;
|
|
77
|
+
if (toolName === 'tool_search') return 8000;
|
|
78
|
+
return 2500;
|
|
79
|
+
};
|
|
74
80
|
let pruned = 0;
|
|
75
81
|
let reclaimedChars = 0;
|
|
76
82
|
let spillFailures = 0;
|
|
@@ -78,7 +84,13 @@ export function shortenOversizedToolObservations(
|
|
|
78
84
|
const message = messages[index];
|
|
79
85
|
if (!message || message.role !== 'tool' || typeof message.content !== 'string') continue;
|
|
80
86
|
const original = message.content;
|
|
87
|
+
const threshold = thresholdFor(message.name);
|
|
81
88
|
if (original.length <= threshold || original.startsWith(MARKER)) continue;
|
|
89
|
+
// Do not truncate an observation a second time when ToolOutputStore has
|
|
90
|
+
// already persisted the complete payload and added its recovery notice.
|
|
91
|
+
if (message.spillPath && original.includes('Causyne ToolOutputStore:') && original.length <= Math.max(threshold + 1000, 4000)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
82
94
|
let spillPath = message.spillPath;
|
|
83
95
|
if (!spillPath) {
|
|
84
96
|
const spill = spillToolResult(original, {
|
package/src/sandbox.js
CHANGED
|
@@ -221,7 +221,8 @@ export class Sandbox {
|
|
|
221
221
|
|
|
222
222
|
validate(toolName, args) {
|
|
223
223
|
// 1. Tool availability check
|
|
224
|
-
|
|
224
|
+
const canonicalToolName = String(toolName || '').replace(/\./g, '_');
|
|
225
|
+
if (this.deniedTools.has(canonicalToolName)) {
|
|
225
226
|
return { allowed: false, reason: `Tool ${toolName} is denied by local policy`, risk: 'forbidden' };
|
|
226
227
|
}
|
|
227
228
|
if (!this.allowedTools.includes('*') && !this.allowedTools.includes(toolName)) {
|
|
@@ -233,10 +234,25 @@ export class Sandbox {
|
|
|
233
234
|
// file_path, the Blender/Godot automators use filepath and directory, the
|
|
234
235
|
// media tools use outputPath. Checking only `path` let all of those write
|
|
235
236
|
// outside the workspace unchallenged.
|
|
236
|
-
|
|
237
|
+
// fs_read_skill parameters (`skill_id`, `file_path`) identify bundled or
|
|
238
|
+
// user-skill resources bounded strictly by skillResourceSafety.ts rather
|
|
239
|
+
// than workspace filesystem paths.
|
|
240
|
+
const safeArgs = args && typeof args === 'object' ? args : {};
|
|
237
241
|
const pathsToCheck = [];
|
|
238
|
-
|
|
239
|
-
|
|
242
|
+
if (canonicalToolName !== 'fs_read_skill') {
|
|
243
|
+
const PATH_ARGUMENTS = [
|
|
244
|
+
'path', 'searchPath', 'cwd', 'source', 'destination',
|
|
245
|
+
'file_path', 'filePath', 'filepath', 'directory',
|
|
246
|
+
'targetFile', 'outputFile', 'outputPath'
|
|
247
|
+
];
|
|
248
|
+
for (const field of PATH_ARGUMENTS) {
|
|
249
|
+
if (safeArgs[field]) pathsToCheck.push(safeArgs[field]);
|
|
250
|
+
}
|
|
251
|
+
if (Array.isArray(safeArgs.target_directories)) {
|
|
252
|
+
for (const dir of safeArgs.target_directories) {
|
|
253
|
+
if (typeof dir === 'string' && dir.trim()) pathsToCheck.push(dir);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
240
256
|
}
|
|
241
257
|
|
|
242
258
|
for (const p of pathsToCheck) {
|
|
@@ -376,13 +392,23 @@ export class Sandbox {
|
|
|
376
392
|
|
|
377
393
|
isPathAllowed(targetPath) {
|
|
378
394
|
if (typeof targetPath !== 'string' || targetPath.length === 0) return false;
|
|
379
|
-
const expand = (value) =>
|
|
380
|
-
|
|
395
|
+
const expand = (value) => {
|
|
396
|
+
if (value === '~') return os.homedir();
|
|
397
|
+
if (value.startsWith('~/') || value.startsWith('~\\')) {
|
|
398
|
+
return path.join(os.homedir(), value.slice(2));
|
|
399
|
+
}
|
|
400
|
+
return value;
|
|
401
|
+
};
|
|
381
402
|
const workspaces = Array.isArray(this.workspacePaths)
|
|
382
403
|
? this.workspacePaths.filter(item => typeof item === 'string').map(expand) : [];
|
|
404
|
+
const baseDir = workspaces[0] ? path.resolve(workspaces[0]) : process.cwd();
|
|
405
|
+
const expandedTarget = expand(targetPath);
|
|
406
|
+
const resolvedTarget = path.isAbsolute(expandedTarget)
|
|
407
|
+
? expandedTarget
|
|
408
|
+
: path.resolve(baseDir, expandedTarget);
|
|
383
409
|
// Resolve physical ancestors, including symlinks, before checking the scope.
|
|
384
410
|
// Missing destination files remain possible inside a valid workspace.
|
|
385
|
-
return isPhysicalPathAllowed(
|
|
411
|
+
return isPhysicalPathAllowed(resolvedTarget, workspaces, this.grantAllAccess === true);
|
|
386
412
|
}
|
|
387
413
|
|
|
388
414
|
isInternalUrl(urlString, { allowLoopback = false } = {}) {
|
|
@@ -39,6 +39,75 @@ function inspectDirectory(directory, allowMissing) {
|
|
|
39
39
|
}
|
|
40
40
|
return true;
|
|
41
41
|
}
|
|
42
|
+
export function assertSkillFileSafe(filePath, allowMissing = false) {
|
|
43
|
+
let stat;
|
|
44
|
+
try {
|
|
45
|
+
stat = fs.lstatSync(filePath);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (allowMissing && error?.code === 'ENOENT')
|
|
49
|
+
return;
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
53
|
+
throw new Error(`Skill file must be a regular file: ${filePath}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function samePhysicalDirectory(a, b) {
|
|
57
|
+
return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Write a regular skill file safely across both POSIX (O_NOFOLLOW) and Windows
|
|
61
|
+
* (no O_NOFOLLOW): reject symlinks before opening, and never truncate until
|
|
62
|
+
* lstatSync, fstatSync, and realpathSync confirm the descriptor is not a symlink
|
|
63
|
+
* and resolves inside the skill directory.
|
|
64
|
+
*/
|
|
65
|
+
export function writeSkillFileSafely(root, domain, name, fileName, body, overwrite, useNoFollow = typeof fs.constants.O_NOFOLLOW === 'number' && fs.constants.O_NOFOLLOW !== 0) {
|
|
66
|
+
const directory = assertSkillTreeSafe(root, domain, name, true);
|
|
67
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
68
|
+
assertSkillTreeSafe(root, domain, name);
|
|
69
|
+
const file = path.join(directory, fileName);
|
|
70
|
+
assertSkillFileSafe(file, true);
|
|
71
|
+
const nofollow = useNoFollow && typeof fs.constants.O_NOFOLLOW === 'number'
|
|
72
|
+
? fs.constants.O_NOFOLLOW
|
|
73
|
+
: 0;
|
|
74
|
+
if (nofollow !== 0) {
|
|
75
|
+
const flags = fs.constants.O_WRONLY | fs.constants.O_CREAT | nofollow
|
|
76
|
+
| (overwrite ? fs.constants.O_TRUNC : fs.constants.O_EXCL);
|
|
77
|
+
const fd = fs.openSync(file, flags, 0o600);
|
|
78
|
+
try {
|
|
79
|
+
if (!fs.fstatSync(fd).isFile())
|
|
80
|
+
throw new Error(`${fileName} must be a regular file`);
|
|
81
|
+
fs.writeFileSync(fd, body, 'utf8');
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
fs.closeSync(fd);
|
|
85
|
+
}
|
|
86
|
+
return file;
|
|
87
|
+
}
|
|
88
|
+
const flags = fs.constants.O_WRONLY | fs.constants.O_CREAT
|
|
89
|
+
| (overwrite ? 0 : fs.constants.O_EXCL);
|
|
90
|
+
const fd = fs.openSync(file, flags, 0o600);
|
|
91
|
+
try {
|
|
92
|
+
assertSkillTreeSafe(root, domain, name);
|
|
93
|
+
assertSkillFileSafe(file, false);
|
|
94
|
+
const realDir = fs.realpathSync(directory);
|
|
95
|
+
const realFile = fs.realpathSync(file);
|
|
96
|
+
if (!samePhysicalDirectory(path.dirname(realFile), realDir)) {
|
|
97
|
+
throw new Error(`${fileName} resolves outside its skill directory`);
|
|
98
|
+
}
|
|
99
|
+
if (!fs.fstatSync(fd).isFile())
|
|
100
|
+
throw new Error(`${fileName} must be a regular file`);
|
|
101
|
+
if (overwrite) {
|
|
102
|
+
fs.ftruncateSync(fd, 0);
|
|
103
|
+
}
|
|
104
|
+
fs.writeFileSync(fd, body, 'utf8');
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
fs.closeSync(fd);
|
|
108
|
+
}
|
|
109
|
+
return file;
|
|
110
|
+
}
|
|
42
111
|
/**
|
|
43
112
|
* Check each directory segment. In particular, do not let a skill author
|
|
44
113
|
* place a symlink at ~/.causyne/skills/<domain> or <name> and redirect writes
|
|
@@ -95,6 +164,7 @@ export function readSkillTextResource(root, domain, name, relativePath) {
|
|
|
95
164
|
inspectDirectory(cursor, false);
|
|
96
165
|
}
|
|
97
166
|
const target = path.join(cursor, components[components.length - 1]);
|
|
167
|
+
assertSkillFileSafe(target, false);
|
|
98
168
|
const physicalRoot = fs.realpathSync(skillDir);
|
|
99
169
|
const physicalFile = fs.realpathSync(target);
|
|
100
170
|
if (!physicalFile.startsWith(`${physicalRoot}${path.sep}`)) {
|
|
@@ -41,6 +41,82 @@ function inspectDirectory(directory: string, allowMissing: boolean): boolean {
|
|
|
41
41
|
return true;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
export function assertSkillFileSafe(filePath: string, allowMissing = false): void {
|
|
45
|
+
let stat: fs.Stats;
|
|
46
|
+
try {
|
|
47
|
+
stat = fs.lstatSync(filePath);
|
|
48
|
+
} catch (error: unknown) {
|
|
49
|
+
if (allowMissing && (error as NodeJS.ErrnoException)?.code === 'ENOENT') return;
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
53
|
+
throw new Error(`Skill file must be a regular file: ${filePath}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function samePhysicalDirectory(a: string, b: string): boolean {
|
|
58
|
+
return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Write a regular skill file safely across both POSIX (O_NOFOLLOW) and Windows
|
|
63
|
+
* (no O_NOFOLLOW): reject symlinks before opening, and never truncate until
|
|
64
|
+
* lstatSync, fstatSync, and realpathSync confirm the descriptor is not a symlink
|
|
65
|
+
* and resolves inside the skill directory.
|
|
66
|
+
*/
|
|
67
|
+
export function writeSkillFileSafely(
|
|
68
|
+
root: string,
|
|
69
|
+
domain: string,
|
|
70
|
+
name: string,
|
|
71
|
+
fileName: string,
|
|
72
|
+
body: string,
|
|
73
|
+
overwrite: boolean,
|
|
74
|
+
useNoFollow = typeof fs.constants.O_NOFOLLOW === 'number' && fs.constants.O_NOFOLLOW !== 0
|
|
75
|
+
): string {
|
|
76
|
+
const directory = assertSkillTreeSafe(root, domain, name, true);
|
|
77
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
78
|
+
assertSkillTreeSafe(root, domain, name);
|
|
79
|
+
const file = path.join(directory, fileName);
|
|
80
|
+
assertSkillFileSafe(file, true);
|
|
81
|
+
|
|
82
|
+
const nofollow = useNoFollow && typeof fs.constants.O_NOFOLLOW === 'number'
|
|
83
|
+
? fs.constants.O_NOFOLLOW
|
|
84
|
+
: 0;
|
|
85
|
+
if (nofollow !== 0) {
|
|
86
|
+
const flags = fs.constants.O_WRONLY | fs.constants.O_CREAT | nofollow
|
|
87
|
+
| (overwrite ? fs.constants.O_TRUNC : fs.constants.O_EXCL);
|
|
88
|
+
const fd = fs.openSync(file, flags, 0o600);
|
|
89
|
+
try {
|
|
90
|
+
if (!fs.fstatSync(fd).isFile()) throw new Error(`${fileName} must be a regular file`);
|
|
91
|
+
fs.writeFileSync(fd, body, 'utf8');
|
|
92
|
+
} finally {
|
|
93
|
+
fs.closeSync(fd);
|
|
94
|
+
}
|
|
95
|
+
return file;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const flags = fs.constants.O_WRONLY | fs.constants.O_CREAT
|
|
99
|
+
| (overwrite ? 0 : fs.constants.O_EXCL);
|
|
100
|
+
const fd = fs.openSync(file, flags, 0o600);
|
|
101
|
+
try {
|
|
102
|
+
assertSkillTreeSafe(root, domain, name);
|
|
103
|
+
assertSkillFileSafe(file, false);
|
|
104
|
+
const realDir = fs.realpathSync(directory);
|
|
105
|
+
const realFile = fs.realpathSync(file);
|
|
106
|
+
if (!samePhysicalDirectory(path.dirname(realFile), realDir)) {
|
|
107
|
+
throw new Error(`${fileName} resolves outside its skill directory`);
|
|
108
|
+
}
|
|
109
|
+
if (!fs.fstatSync(fd).isFile()) throw new Error(`${fileName} must be a regular file`);
|
|
110
|
+
if (overwrite) {
|
|
111
|
+
fs.ftruncateSync(fd, 0);
|
|
112
|
+
}
|
|
113
|
+
fs.writeFileSync(fd, body, 'utf8');
|
|
114
|
+
} finally {
|
|
115
|
+
fs.closeSync(fd);
|
|
116
|
+
}
|
|
117
|
+
return file;
|
|
118
|
+
}
|
|
119
|
+
|
|
44
120
|
/**
|
|
45
121
|
* Check each directory segment. In particular, do not let a skill author
|
|
46
122
|
* place a symlink at ~/.causyne/skills/<domain> or <name> and redirect writes
|
|
@@ -104,6 +180,7 @@ export function readSkillTextResource(
|
|
|
104
180
|
inspectDirectory(cursor, false);
|
|
105
181
|
}
|
|
106
182
|
const target = path.join(cursor, components[components.length - 1]!);
|
|
183
|
+
assertSkillFileSafe(target, false);
|
|
107
184
|
const physicalRoot = fs.realpathSync(skillDir);
|
|
108
185
|
const physicalFile = fs.realpathSync(target);
|
|
109
186
|
if (!physicalFile.startsWith(`${physicalRoot}${path.sep}`)) {
|
package/src/smartToolsUse.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* Authorship: Strict TypeScript (Causyne Agent).
|
|
9
9
|
*/
|
|
10
10
|
import { PACK_PLAYBOOKS, isWebIntent } from './toolPacks.js';
|
|
11
|
-
import { isMutatingToolCall } from './decisionPolicy.js';
|
|
11
|
+
import { isMutatingToolCall, isPureSkillOrCapabilityLookup, redactSkillAndQueryLiterals } from './decisionPolicy.js';
|
|
12
12
|
const INSPECTION_TOOLS = new Set([
|
|
13
13
|
'fs_read', 'fs_list', 'fs_find', 'fs_search', 'grep_search', 'code_searcher',
|
|
14
14
|
'codebase_search', 'web_search', 'web_fetch', 'browser_snapshot',
|
|
@@ -147,8 +147,25 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
147
147
|
if (!text) {
|
|
148
148
|
return { toolScope: null, archetype: 'empty', suppressedPacks: [] };
|
|
149
149
|
}
|
|
150
|
+
// 0. Pure skill inspection, skill catalog inquiry, or tool_search capability lookup
|
|
151
|
+
if (isPureSkillOrCapabilityLookup(text)) {
|
|
152
|
+
const tools = new Set([
|
|
153
|
+
'fs_read_skill',
|
|
154
|
+
'tool_search',
|
|
155
|
+
'fs_read',
|
|
156
|
+
'fs_list',
|
|
157
|
+
'update_plan'
|
|
158
|
+
]);
|
|
159
|
+
return {
|
|
160
|
+
toolScope: tools,
|
|
161
|
+
archetype: 'skill_lookup',
|
|
162
|
+
suppressedPacks: ['media', 'game', 'web', 'system', 'advanced', 'reverse_engineering'],
|
|
163
|
+
reason: 'Skill reading or capability lookup uses direct skill access and bounded tool_search; suppresses creative, web, game, system, and binary packs.'
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const effectiveText = redactSkillAndQueryLiterals(text);
|
|
150
167
|
// 1. Reverse engineering & binary analysis intent
|
|
151
|
-
if (EXPLICIT_REVERSE_ENGINEERING_PATTERN.test(
|
|
168
|
+
if (EXPLICIT_REVERSE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
152
169
|
const tools = new Set([
|
|
153
170
|
'reverse_engineering',
|
|
154
171
|
'fs_read',
|
|
@@ -169,7 +186,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
169
186
|
};
|
|
170
187
|
}
|
|
171
188
|
// 2. Desktop personalization (e.g. wallpaper / desktop background)
|
|
172
|
-
if (DESKTOP_PERSONALIZATION_PATTERN.test(
|
|
189
|
+
if (DESKTOP_PERSONALIZATION_PATTERN.test(effectiveText)) {
|
|
173
190
|
const tools = new Set([
|
|
174
191
|
'sysRun',
|
|
175
192
|
'web_search',
|
|
@@ -188,7 +205,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
188
205
|
};
|
|
189
206
|
}
|
|
190
207
|
// 2. Pure web research (searching/fetching online information without web app construction)
|
|
191
|
-
if (PURE_WEB_SEARCH_PATTERN.test(
|
|
208
|
+
if (PURE_WEB_SEARCH_PATTERN.test(effectiveText) && !WEB_CONSTRUCTION_EXCLUSION.test(effectiveText)) {
|
|
192
209
|
const tools = new Set([
|
|
193
210
|
'web_search',
|
|
194
211
|
'web_fetch',
|
|
@@ -206,7 +223,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
206
223
|
};
|
|
207
224
|
}
|
|
208
225
|
// 3. System process or archive administration
|
|
209
|
-
if (SYSTEM_ADMIN_PATTERN.test(
|
|
226
|
+
if (SYSTEM_ADMIN_PATTERN.test(effectiveText)) {
|
|
210
227
|
const tools = new Set([
|
|
211
228
|
'sysRun',
|
|
212
229
|
'proc_term',
|
|
@@ -225,7 +242,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
225
242
|
};
|
|
226
243
|
}
|
|
227
244
|
// 4. Simple local file manipulation
|
|
228
|
-
if (SIMPLE_FILE_OP_PATTERN.test(
|
|
245
|
+
if (SIMPLE_FILE_OP_PATTERN.test(effectiveText) && !EXPLICIT_WEB_ENGINEERING_PATTERN.test(effectiveText) && !EXPLICIT_GAME_DEV_PATTERN.test(effectiveText) && !EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
229
246
|
const tools = new Set([
|
|
230
247
|
'fs_read',
|
|
231
248
|
'fs_write',
|
|
@@ -244,7 +261,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
244
261
|
};
|
|
245
262
|
}
|
|
246
263
|
// 5. Web engineering intent (websites, web applications, landing pages, Three.js web scenes)
|
|
247
|
-
if (EXPLICIT_WEB_ENGINEERING_PATTERN.test(
|
|
264
|
+
if (EXPLICIT_WEB_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
248
265
|
const tools = new Set([
|
|
249
266
|
'fs_write',
|
|
250
267
|
'fs_edit',
|
|
@@ -265,7 +282,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
265
282
|
};
|
|
266
283
|
}
|
|
267
284
|
// 6. Media / 3D generation & creative audio intent
|
|
268
|
-
if (EXPLICIT_MEDIA_GENERATION_PATTERN.test(
|
|
285
|
+
if (EXPLICIT_MEDIA_GENERATION_PATTERN.test(effectiveText)) {
|
|
269
286
|
const tools = new Set([
|
|
270
287
|
'blender_act',
|
|
271
288
|
'creative_apps',
|
|
@@ -285,7 +302,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
285
302
|
};
|
|
286
303
|
}
|
|
287
304
|
// 7. Game development and interactive simulation intent (simulators, 3D worlds, games)
|
|
288
|
-
if (EXPLICIT_GAME_DEV_PATTERN.test(
|
|
305
|
+
if (EXPLICIT_GAME_DEV_PATTERN.test(effectiveText)) {
|
|
289
306
|
const tools = new Set([
|
|
290
307
|
'creative_apps',
|
|
291
308
|
'fs_write',
|
|
@@ -307,7 +324,7 @@ export function determineToolScope(prompt, _taskShape) {
|
|
|
307
324
|
};
|
|
308
325
|
}
|
|
309
326
|
// 8. Software and systems engineering intent (backend, APIs, refactoring, algorithms, testing, architecture, CLI tools)
|
|
310
|
-
if (EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(
|
|
327
|
+
if (EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
311
328
|
const tools = new Set([
|
|
312
329
|
'fs_write',
|
|
313
330
|
'fs_edit',
|
|
@@ -356,7 +373,7 @@ export function filterSuppressedPacks(candidatePacks, prompt) {
|
|
|
356
373
|
export function resolveLazyPlaybooks(unlockedPacks, observedTools = [], prompt = '') {
|
|
357
374
|
const packs = new Set(unlockedPacks);
|
|
358
375
|
const tools = new Set(observedTools);
|
|
359
|
-
const text = prompt || '';
|
|
376
|
+
const text = redactSkillAndQueryLiterals(prompt || '');
|
|
360
377
|
const activePlaybooks = [];
|
|
361
378
|
const microAnnouncements = [];
|
|
362
379
|
const activeDomains = [];
|
package/src/smartToolsUse.ts
CHANGED
|
@@ -9,7 +9,11 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { PACK_PLAYBOOKS, isWebIntent } from './toolPacks.js';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
isMutatingToolCall,
|
|
14
|
+
isPureSkillOrCapabilityLookup,
|
|
15
|
+
redactSkillAndQueryLiterals
|
|
16
|
+
} from './decisionPolicy.js';
|
|
13
17
|
import type { ToolDecision } from './decideTools.js';
|
|
14
18
|
|
|
15
19
|
export type ExecutionPhase = 'inspection' | 'synthesis' | 'execution' | 'verification';
|
|
@@ -202,8 +206,27 @@ export function determineToolScope(
|
|
|
202
206
|
return { toolScope: null, archetype: 'empty', suppressedPacks: [] };
|
|
203
207
|
}
|
|
204
208
|
|
|
209
|
+
// 0. Pure skill inspection, skill catalog inquiry, or tool_search capability lookup
|
|
210
|
+
if (isPureSkillOrCapabilityLookup(text)) {
|
|
211
|
+
const tools = new Set([
|
|
212
|
+
'fs_read_skill',
|
|
213
|
+
'tool_search',
|
|
214
|
+
'fs_read',
|
|
215
|
+
'fs_list',
|
|
216
|
+
'update_plan'
|
|
217
|
+
]);
|
|
218
|
+
return {
|
|
219
|
+
toolScope: tools,
|
|
220
|
+
archetype: 'skill_lookup',
|
|
221
|
+
suppressedPacks: ['media', 'game', 'web', 'system', 'advanced', 'reverse_engineering'],
|
|
222
|
+
reason: 'Skill reading or capability lookup uses direct skill access and bounded tool_search; suppresses creative, web, game, system, and binary packs.'
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const effectiveText = redactSkillAndQueryLiterals(text);
|
|
227
|
+
|
|
205
228
|
// 1. Reverse engineering & binary analysis intent
|
|
206
|
-
if (EXPLICIT_REVERSE_ENGINEERING_PATTERN.test(
|
|
229
|
+
if (EXPLICIT_REVERSE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
207
230
|
const tools = new Set([
|
|
208
231
|
'reverse_engineering',
|
|
209
232
|
'fs_read',
|
|
@@ -225,7 +248,7 @@ export function determineToolScope(
|
|
|
225
248
|
}
|
|
226
249
|
|
|
227
250
|
// 2. Desktop personalization (e.g. wallpaper / desktop background)
|
|
228
|
-
if (DESKTOP_PERSONALIZATION_PATTERN.test(
|
|
251
|
+
if (DESKTOP_PERSONALIZATION_PATTERN.test(effectiveText)) {
|
|
229
252
|
const tools = new Set([
|
|
230
253
|
'sysRun',
|
|
231
254
|
'web_search',
|
|
@@ -245,7 +268,7 @@ export function determineToolScope(
|
|
|
245
268
|
}
|
|
246
269
|
|
|
247
270
|
// 2. Pure web research (searching/fetching online information without web app construction)
|
|
248
|
-
if (PURE_WEB_SEARCH_PATTERN.test(
|
|
271
|
+
if (PURE_WEB_SEARCH_PATTERN.test(effectiveText) && !WEB_CONSTRUCTION_EXCLUSION.test(effectiveText)) {
|
|
249
272
|
const tools = new Set([
|
|
250
273
|
'web_search',
|
|
251
274
|
'web_fetch',
|
|
@@ -264,7 +287,7 @@ export function determineToolScope(
|
|
|
264
287
|
}
|
|
265
288
|
|
|
266
289
|
// 3. System process or archive administration
|
|
267
|
-
if (SYSTEM_ADMIN_PATTERN.test(
|
|
290
|
+
if (SYSTEM_ADMIN_PATTERN.test(effectiveText)) {
|
|
268
291
|
const tools = new Set([
|
|
269
292
|
'sysRun',
|
|
270
293
|
'proc_term',
|
|
@@ -284,7 +307,7 @@ export function determineToolScope(
|
|
|
284
307
|
}
|
|
285
308
|
|
|
286
309
|
// 4. Simple local file manipulation
|
|
287
|
-
if (SIMPLE_FILE_OP_PATTERN.test(
|
|
310
|
+
if (SIMPLE_FILE_OP_PATTERN.test(effectiveText) && !EXPLICIT_WEB_ENGINEERING_PATTERN.test(effectiveText) && !EXPLICIT_GAME_DEV_PATTERN.test(effectiveText) && !EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
288
311
|
const tools = new Set([
|
|
289
312
|
'fs_read',
|
|
290
313
|
'fs_write',
|
|
@@ -304,7 +327,7 @@ export function determineToolScope(
|
|
|
304
327
|
}
|
|
305
328
|
|
|
306
329
|
// 5. Web engineering intent (websites, web applications, landing pages, Three.js web scenes)
|
|
307
|
-
if (EXPLICIT_WEB_ENGINEERING_PATTERN.test(
|
|
330
|
+
if (EXPLICIT_WEB_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
308
331
|
const tools = new Set([
|
|
309
332
|
'fs_write',
|
|
310
333
|
'fs_edit',
|
|
@@ -326,7 +349,7 @@ export function determineToolScope(
|
|
|
326
349
|
}
|
|
327
350
|
|
|
328
351
|
// 6. Media / 3D generation & creative audio intent
|
|
329
|
-
if (EXPLICIT_MEDIA_GENERATION_PATTERN.test(
|
|
352
|
+
if (EXPLICIT_MEDIA_GENERATION_PATTERN.test(effectiveText)) {
|
|
330
353
|
const tools = new Set([
|
|
331
354
|
'blender_act',
|
|
332
355
|
'creative_apps',
|
|
@@ -347,7 +370,7 @@ export function determineToolScope(
|
|
|
347
370
|
}
|
|
348
371
|
|
|
349
372
|
// 7. Game development and interactive simulation intent (simulators, 3D worlds, games)
|
|
350
|
-
if (EXPLICIT_GAME_DEV_PATTERN.test(
|
|
373
|
+
if (EXPLICIT_GAME_DEV_PATTERN.test(effectiveText)) {
|
|
351
374
|
const tools = new Set([
|
|
352
375
|
'creative_apps',
|
|
353
376
|
'fs_write',
|
|
@@ -370,7 +393,7 @@ export function determineToolScope(
|
|
|
370
393
|
}
|
|
371
394
|
|
|
372
395
|
// 8. Software and systems engineering intent (backend, APIs, refactoring, algorithms, testing, architecture, CLI tools)
|
|
373
|
-
if (EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(
|
|
396
|
+
if (EXPLICIT_SOFTWARE_ENGINEERING_PATTERN.test(effectiveText)) {
|
|
374
397
|
const tools = new Set([
|
|
375
398
|
'fs_write',
|
|
376
399
|
'fs_edit',
|
|
@@ -431,7 +454,7 @@ export function resolveLazyPlaybooks(
|
|
|
431
454
|
): LazyPlaybookResult {
|
|
432
455
|
const packs = new Set(unlockedPacks);
|
|
433
456
|
const tools = new Set(observedTools);
|
|
434
|
-
const text = prompt || '';
|
|
457
|
+
const text = redactSkillAndQueryLiterals(prompt || '');
|
|
435
458
|
|
|
436
459
|
const activePlaybooks: string[] = [];
|
|
437
460
|
const microAnnouncements: string[] = [];
|