@phren/agent 0.3.35 → 0.3.36
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.
|
@@ -1,24 +1,33 @@
|
|
|
1
|
+
// A command position: the start of the line or of a chained, piped or
|
|
2
|
+
// substituted command, after any wrapper such as sudo or xargs. Matching a
|
|
3
|
+
// word only here keeps `grep -rn setsid src` and `npx mkfs-tool` runnable.
|
|
4
|
+
const CMD = String.raw `(?:^|[\n;&|({\`]|\$\()\s*(?:(?:sudo|exec|command|nice|time|env|xargs)\s+(?:-\S+\s+)*)*`;
|
|
5
|
+
// The end of a word as the shell splits it.
|
|
6
|
+
const END = String.raw `(?=$|[\s;&|)])`;
|
|
7
|
+
// rm with a recursive flag anywhere in the same command (-r, -rf, -fR, --recursive).
|
|
8
|
+
const RM_RECURSIVE = String.raw `\brm\b(?=[^;&|]*\s-(?:[a-zA-Z]*[rR]|-recursive))[^;&|]*?\s["']?`;
|
|
9
|
+
function blocked(source, reason, commandOnly = true) {
|
|
10
|
+
return { pattern: new RegExp(source, "i"), reason, severity: "block", commandOnly };
|
|
11
|
+
}
|
|
1
12
|
const DANGEROUS_PATTERNS = [
|
|
2
|
-
// Block: destructive/irreversible
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
{
|
|
7
|
-
{
|
|
8
|
-
{
|
|
9
|
-
{
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
{
|
|
13
|
-
{ pattern: /\bsetsid\b/i, reason: "Detached process may outlive session", severity: "block" },
|
|
13
|
+
// Block: destructive/irreversible. rm targets are matched in the raw
|
|
14
|
+
// command so quoting (`rm -rf "/"`) does not hide them; only `/`, `/*` and a
|
|
15
|
+
// top-level directory such as `/etc` count, not a path under one.
|
|
16
|
+
blocked(String.raw `${RM_RECURSIVE}\/\*?["']?${END}`, "Recursive delete of root filesystem", false),
|
|
17
|
+
blocked(String.raw `${RM_RECURSIVE}\/[^\/\s"';&|*]+\/?\*?["']?${END}`, "Recursive delete of top-level directory", false),
|
|
18
|
+
blocked(String.raw `${CMD}(?:curl|wget)\b.*\|\s*(?:sudo\s+)?(?:ba|z|da)?sh${END}`, "Piping remote script to shell"),
|
|
19
|
+
blocked(String.raw `${CMD}mkfs(?:\.\w+)?${END}`, "Filesystem format command"),
|
|
20
|
+
blocked(String.raw `${CMD}dd\b.*\bof=\/dev\/(?!null\b|zero\b|std(?:out|err)\b|fd\/)`, "Direct device write with dd"),
|
|
21
|
+
blocked(String.raw `>\s*\/dev\/[sh]d[a-z]`, "Direct write to block device"),
|
|
22
|
+
blocked(String.raw `:\(\)\s*\{\s*:\|:&\s*\};:`, "Fork bomb", false),
|
|
23
|
+
blocked(String.raw `${CMD}(?:nohup|disown|setsid)${END}`, "Detached process may outlive session"),
|
|
14
24
|
// Block: Windows-specific destructive commands
|
|
15
|
-
{
|
|
16
|
-
{
|
|
17
|
-
{
|
|
18
|
-
{
|
|
19
|
-
{
|
|
20
|
-
{
|
|
21
|
-
{ pattern: /\bcmd\b.*\/c.*\bdel\s+\/[sq]/i, reason: "Recursive or quiet delete via cmd", severity: "block" },
|
|
25
|
+
blocked(String.raw `${CMD}format\s+[a-z]:`, "Disk format command"),
|
|
26
|
+
blocked(String.raw `${CMD}del\s+\/[sq]`, "Recursive or quiet delete"),
|
|
27
|
+
blocked(String.raw `${CMD}(?:rd|rmdir)\s+\/s`, "Recursive directory removal"),
|
|
28
|
+
blocked(String.raw `${CMD}reg\s+delete\b`, "Registry deletion"),
|
|
29
|
+
blocked(String.raw `${CMD}(?:powershell|pwsh)\b.*\s-enc`, "Encoded PowerShell command (obfuscation)"),
|
|
30
|
+
blocked(String.raw `${CMD}cmd\b.*\/c.*\bdel\s+\/[sq]`, "Recursive or quiet delete via cmd"),
|
|
22
31
|
// Warn: potentially dangerous
|
|
23
32
|
{ pattern: /\beval\b/i, reason: "Dynamic code execution via eval", severity: "warn" },
|
|
24
33
|
{ pattern: /\$\(.*\)/, reason: "Command substitution", severity: "warn" },
|
|
@@ -48,12 +57,21 @@ const KEY_PATTERNS = [
|
|
|
48
57
|
/** Suffix patterns that also match connection strings and auth configs. */
|
|
49
58
|
const SECRET_SUFFIX_PATTERNS = ["_URI", "_DSN"];
|
|
50
59
|
const SECRET_SUFFIXES = ["_SECRET", "_TOKEN", "_PASSWORD", "_KEY"];
|
|
60
|
+
/**
|
|
61
|
+
* The command with quoted arguments blanked, so a word that is only text
|
|
62
|
+
* (`git commit -m 'drop nohup'`) is not read as a command. Quoted text after
|
|
63
|
+
* `-c` or `eval` is itself run, so it stays, fenced as its own command line.
|
|
64
|
+
*/
|
|
65
|
+
function unquoted(command) {
|
|
66
|
+
return command.replace(/(-c\s+|\beval\s+)?('[^']*'|"(?:[^"\\]|\\.)*")/g, (_m, run, quoted) => run ? `${run};${quoted.slice(1, -1)};` : "''");
|
|
67
|
+
}
|
|
51
68
|
/**
|
|
52
69
|
* Check a shell command for dangerous patterns.
|
|
53
70
|
*/
|
|
54
71
|
export function checkShellSafety(command) {
|
|
72
|
+
const commands = unquoted(command);
|
|
55
73
|
for (const dp of DANGEROUS_PATTERNS) {
|
|
56
|
-
if (dp.pattern.test(command)) {
|
|
74
|
+
if (dp.pattern.test(dp.commandOnly ? commands : command)) {
|
|
57
75
|
return { safe: false, reason: dp.reason, severity: dp.severity };
|
|
58
76
|
}
|
|
59
77
|
}
|
|
@@ -47,14 +47,15 @@ export function similarity(a, b) {
|
|
|
47
47
|
return 1;
|
|
48
48
|
if (a.length < 2 || b.length < 2)
|
|
49
49
|
return 0;
|
|
50
|
+
// A bigram as one number: two UTF-16 code units.
|
|
50
51
|
const grams = new Map();
|
|
51
52
|
for (let i = 0; i < a.length - 1; i++) {
|
|
52
|
-
const g = a.
|
|
53
|
+
const g = a.charCodeAt(i) * 65536 + a.charCodeAt(i + 1);
|
|
53
54
|
grams.set(g, (grams.get(g) ?? 0) + 1);
|
|
54
55
|
}
|
|
55
56
|
let hits = 0;
|
|
56
57
|
for (let i = 0; i < b.length - 1; i++) {
|
|
57
|
-
const g = b.
|
|
58
|
+
const g = b.charCodeAt(i) * 65536 + b.charCodeAt(i + 1);
|
|
58
59
|
const n = grams.get(g) ?? 0;
|
|
59
60
|
if (n > 0) {
|
|
60
61
|
hits++;
|
|
@@ -63,6 +64,7 @@ export function similarity(a, b) {
|
|
|
63
64
|
}
|
|
64
65
|
return (2 * hits) / (a.length - 1 + (b.length - 1));
|
|
65
66
|
}
|
|
67
|
+
const REGION_BUDGET = 100_000;
|
|
66
68
|
/**
|
|
67
69
|
* The file region most similar to `needleLines`, for failure messages.
|
|
68
70
|
* Bounded so a large file cannot make a failed edit slow.
|
|
@@ -76,12 +78,24 @@ export function closestRegion(fileLines, needleLines) {
|
|
|
76
78
|
if (firstReal === -1)
|
|
77
79
|
return null;
|
|
78
80
|
const last = Math.max(0, fileLines.length - n);
|
|
79
|
-
|
|
81
|
+
// Score at most REGION_BUDGET line pairs. On a big file, rank the windows by how
|
|
82
|
+
// close their anchor line is and score the closest first.
|
|
83
|
+
let starts;
|
|
84
|
+
if ((last + 1) * n <= REGION_BUDGET) {
|
|
85
|
+
starts = Array.from({ length: last + 1 }, (_, i) => i);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const anchored = [];
|
|
89
|
+
for (let start = 0; start <= last; start++) {
|
|
90
|
+
const score = similarity(fileLines[start + firstReal]?.trim() ?? "", needle[firstReal]);
|
|
91
|
+
if (score >= 0.5)
|
|
92
|
+
anchored.push({ start, score });
|
|
93
|
+
}
|
|
94
|
+
anchored.sort((a, b) => b.score - a.score || a.start - b.start);
|
|
95
|
+
starts = anchored.slice(0, Math.floor(REGION_BUDGET / n)).map((a) => a.start);
|
|
96
|
+
}
|
|
80
97
|
let best = null;
|
|
81
|
-
for (
|
|
82
|
-
// On big files only score windows whose anchor line is already close.
|
|
83
|
-
if (!exhaustive && similarity(fileLines[start + firstReal]?.trim() ?? "", needle[firstReal]) < 0.5)
|
|
84
|
-
continue;
|
|
98
|
+
for (const start of starts) {
|
|
85
99
|
let total = 0;
|
|
86
100
|
let counted = 0;
|
|
87
101
|
for (let j = 0; j < n; j++) {
|
|
@@ -93,7 +107,7 @@ export function closestRegion(fileLines, needleLines) {
|
|
|
93
107
|
counted++;
|
|
94
108
|
}
|
|
95
109
|
const score = counted > 0 ? total / counted : 0;
|
|
96
|
-
if (!best || score > best.score)
|
|
110
|
+
if (!best || score > best.score || (score === best.score && start < best.start))
|
|
97
111
|
best = { start, score };
|
|
98
112
|
}
|
|
99
113
|
return best;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phren/agent",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.36",
|
|
4
4
|
"description": "phren's coding agent: starts every session knowing your project's memory, tasks and decisions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@modelcontextprotocol/sdk": "^1.32.0",
|
|
18
|
-
"@phren/cli": "0.3.
|
|
18
|
+
"@phren/cli": "0.3.36",
|
|
19
19
|
"ajv": "^8.20.0",
|
|
20
20
|
"ink": "^8.0.0",
|
|
21
21
|
"react": "^19.3.0"
|