noguard 1.2.0 → 1.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.
- package/bin/noguard.mjs +264 -245
- package/lib/agent.mjs +102 -51
- package/lib/api.mjs +81 -76
- package/lib/auth.mjs +77 -77
- package/lib/chat.mjs +514 -515
- package/lib/config.mjs +57 -57
- package/lib/context.mjs +115 -115
- package/lib/files.mjs +168 -168
- package/lib/persona-eval.mjs +305 -305
- package/lib/render.mjs +400 -398
- package/lib/shell.mjs +84 -84
- package/package.json +23 -23
package/lib/files.mjs
CHANGED
|
@@ -1,168 +1,168 @@
|
|
|
1
|
-
// Listing and matching project files: git's view when there is one, ripgrep's, or a plain walk.
|
|
2
|
-
|
|
3
|
-
import { spawn, spawnSync } from 'node:child_process'
|
|
4
|
-
import { existsSync, lstatSync, readdirSync, readlinkSync, realpathSync, statSync } from 'node:fs'
|
|
5
|
-
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
6
|
-
|
|
7
|
-
const SKIP_DIRS = new Set(['.git', 'node_modules', '.venv', 'venv', '__pycache__', '.next', '.cache', '.turbo', 'target', '.idea', '.vs'])
|
|
8
|
-
const MAX_FILES = 200_000
|
|
9
|
-
|
|
10
|
-
let rgPath
|
|
11
|
-
export function ripgrep() {
|
|
12
|
-
if (rgPath === undefined) {
|
|
13
|
-
const found = spawnSync('rg', ['--version'], { stdio: 'ignore', windowsHide: true })
|
|
14
|
-
rgPath = found.status === 0 ? 'rg' : null
|
|
15
|
-
}
|
|
16
|
-
return rgPath
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function toPosix(p) {
|
|
20
|
-
return p.split(sep).join('/')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Resolves a path the model gave, including Git Bash style /c/Users/... paths on Windows. */
|
|
24
|
-
export function resolvePath(cwd, p) {
|
|
25
|
-
let path = String(p ?? '').trim()
|
|
26
|
-
if (process.platform === 'win32') {
|
|
27
|
-
const msys = path.match(/^\/([a-zA-Z])(\/.*)?$/)
|
|
28
|
-
if (msys) path = `${msys[1].toUpperCase()}:${msys[2] ?? '/'}`
|
|
29
|
-
}
|
|
30
|
-
if (path.startsWith('~/') || path === '~') path = join(process.env.HOME ?? process.env.USERPROFILE ?? '', path.slice(1))
|
|
31
|
-
return resolve(cwd, path || '.')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isInside(root, path) {
|
|
35
|
-
const rel = relative(root, path)
|
|
36
|
-
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The real path with symlinks resolved. For a path that doesn't exist yet, its nearest existing parent's real
|
|
41
|
-
* path plus the rest; a dangling symlink on the way is followed to where it points. null if it can't be resolved.
|
|
42
|
-
*/
|
|
43
|
-
export function realPath(path) {
|
|
44
|
-
const rest = []
|
|
45
|
-
let current = path
|
|
46
|
-
for (let step = 0; step < 256; step++) {
|
|
47
|
-
try { return join(realpathSync.native(current), ...rest) } catch { /* missing, or a link to something missing */ }
|
|
48
|
-
let target = null
|
|
49
|
-
try { if (lstatSync(current).isSymbolicLink()) target = readlinkSync(current) } catch { /* nothing here */ }
|
|
50
|
-
if (target !== null) {
|
|
51
|
-
// ".." after a linked folder means its real parent, which resolving by string gets wrong: fail closed.
|
|
52
|
-
if (target.split(/[\\/]+/).includes('..')) return null
|
|
53
|
-
current = resolve(dirname(current), target)
|
|
54
|
-
continue
|
|
55
|
-
}
|
|
56
|
-
const up = dirname(current)
|
|
57
|
-
if (up === current) return join(current, ...rest)
|
|
58
|
-
rest.unshift(basename(current))
|
|
59
|
-
current = up
|
|
60
|
-
}
|
|
61
|
-
return null
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function isInsideReal(root, path) {
|
|
65
|
-
const realRoot = realPath(root), real = realPath(path)
|
|
66
|
-
return realRoot !== null && real !== null && isInside(realRoot, real)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function displayPath(cwd, path) {
|
|
70
|
-
return isInside(cwd, path) ? (toPosix(relative(cwd, path)) || '.') : path
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function run(cmd, args, cwd, signal) {
|
|
74
|
-
return new Promise(resolvePromise => {
|
|
75
|
-
const child = spawn(cmd, args, { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] })
|
|
76
|
-
const chunks = []
|
|
77
|
-
let size = 0
|
|
78
|
-
child.stdout.on('data', chunk => {
|
|
79
|
-
size += chunk.length
|
|
80
|
-
if (size < 64 * 1024 * 1024) chunks.push(chunk)
|
|
81
|
-
})
|
|
82
|
-
const onAbort = () => child.kill()
|
|
83
|
-
signal?.addEventListener('abort', onAbort, { once: true })
|
|
84
|
-
child.on('error', () => resolvePromise(null))
|
|
85
|
-
child.on('close', code => {
|
|
86
|
-
signal?.removeEventListener('abort', onAbort)
|
|
87
|
-
resolvePromise(code === 0 ? Buffer.concat(chunks).toString('utf8') : null)
|
|
88
|
-
})
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Files under `root`, relative to it with forward slashes, respecting .gitignore when git or rg can. */
|
|
93
|
-
export async function listFiles(root, signal) {
|
|
94
|
-
if (existsSync(join(root, '.git')) || spawnSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd: root, stdio: 'ignore', windowsHide: true }).status === 0) {
|
|
95
|
-
const listed = await run('git', ['-c', 'core.quotepath=off', 'ls-files', '-z', '--cached', '--others', '--exclude-standard'], root, signal)
|
|
96
|
-
if (listed !== null) return listed.split('\0').filter(Boolean).slice(0, MAX_FILES)
|
|
97
|
-
}
|
|
98
|
-
if (ripgrep()) {
|
|
99
|
-
const listed = await run(ripgrep(), ['--files', '--hidden', '-g', '!.git'], root, signal)
|
|
100
|
-
if (listed !== null) return listed.split(/\r?\n/).filter(Boolean).map(toPosix).slice(0, MAX_FILES)
|
|
101
|
-
}
|
|
102
|
-
const files = []
|
|
103
|
-
const walk = (dir, prefix) => {
|
|
104
|
-
if (files.length >= MAX_FILES || signal?.aborted) return
|
|
105
|
-
let entries
|
|
106
|
-
try { entries = readdirSync(dir, { withFileTypes: true }) } catch { return }
|
|
107
|
-
for (const entry of entries) {
|
|
108
|
-
if (entry.isDirectory()) {
|
|
109
|
-
if (!SKIP_DIRS.has(entry.name)) walk(join(dir, entry.name), `${prefix}${entry.name}/`)
|
|
110
|
-
} else if (entry.isFile()) {
|
|
111
|
-
files.push(`${prefix}${entry.name}`)
|
|
112
|
-
if (files.length >= MAX_FILES) return
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
walk(root, '')
|
|
117
|
-
return files
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** A glob as a RegExp over forward-slash paths: **, *, ?, {a,b} and [...] classes. */
|
|
121
|
-
export function globToRegExp(glob) {
|
|
122
|
-
let i = 0
|
|
123
|
-
const parse = (stopAtComma) => {
|
|
124
|
-
let re = ''
|
|
125
|
-
while (i < glob.length) {
|
|
126
|
-
const ch = glob[i]
|
|
127
|
-
if (stopAtComma && (ch === ',' || ch === '}')) return re
|
|
128
|
-
i++
|
|
129
|
-
if (ch === '*') {
|
|
130
|
-
if (glob[i] === '*') {
|
|
131
|
-
i++
|
|
132
|
-
if (glob[i] === '/') { i++; re += '(?:.*/)?' } else re += '.*'
|
|
133
|
-
} else re += '[^/]*'
|
|
134
|
-
} else if (ch === '?') re += '[^/]'
|
|
135
|
-
else if (ch === '{') {
|
|
136
|
-
const options = []
|
|
137
|
-
for (;;) {
|
|
138
|
-
options.push(parse(true))
|
|
139
|
-
if (glob[i] === ',') { i++; continue }
|
|
140
|
-
if (glob[i] === '}') i++
|
|
141
|
-
break
|
|
142
|
-
}
|
|
143
|
-
re += `(?:${options.join('|')})`
|
|
144
|
-
} else if (ch === '[') {
|
|
145
|
-
const end = glob.indexOf(']', i)
|
|
146
|
-
if (end === -1) re += '\\['
|
|
147
|
-
else {
|
|
148
|
-
let body = glob.slice(i, end).replace(/\\/g, '\\\\')
|
|
149
|
-
if (body.startsWith('!')) body = `^${body.slice(1)}`
|
|
150
|
-
re += `[${body}]`
|
|
151
|
-
i = end + 1
|
|
152
|
-
}
|
|
153
|
-
} else re += ch.replace(/[.+^$()|\\]/g, '\\$&')
|
|
154
|
-
}
|
|
155
|
-
return re
|
|
156
|
-
}
|
|
157
|
-
return new RegExp(`^${parse(false)}$`, process.platform === 'win32' ? 'i' : '')
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function isBinary(buffer) {
|
|
161
|
-
const n = Math.min(buffer.length, 8000)
|
|
162
|
-
for (let i = 0; i < n; i++) if (buffer[i] === 0) return true
|
|
163
|
-
return false
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function mtime(path) {
|
|
167
|
-
try { return statSync(path).mtimeMs } catch { return 0 }
|
|
168
|
-
}
|
|
1
|
+
// Listing and matching project files: git's view when there is one, ripgrep's, or a plain walk.
|
|
2
|
+
|
|
3
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
4
|
+
import { existsSync, lstatSync, readdirSync, readlinkSync, realpathSync, statSync } from 'node:fs'
|
|
5
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
6
|
+
|
|
7
|
+
const SKIP_DIRS = new Set(['.git', 'node_modules', '.venv', 'venv', '__pycache__', '.next', '.cache', '.turbo', 'target', '.idea', '.vs'])
|
|
8
|
+
const MAX_FILES = 200_000
|
|
9
|
+
|
|
10
|
+
let rgPath
|
|
11
|
+
export function ripgrep() {
|
|
12
|
+
if (rgPath === undefined) {
|
|
13
|
+
const found = spawnSync('rg', ['--version'], { stdio: 'ignore', windowsHide: true })
|
|
14
|
+
rgPath = found.status === 0 ? 'rg' : null
|
|
15
|
+
}
|
|
16
|
+
return rgPath
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function toPosix(p) {
|
|
20
|
+
return p.split(sep).join('/')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Resolves a path the model gave, including Git Bash style /c/Users/... paths on Windows. */
|
|
24
|
+
export function resolvePath(cwd, p) {
|
|
25
|
+
let path = String(p ?? '').trim()
|
|
26
|
+
if (process.platform === 'win32') {
|
|
27
|
+
const msys = path.match(/^\/([a-zA-Z])(\/.*)?$/)
|
|
28
|
+
if (msys) path = `${msys[1].toUpperCase()}:${msys[2] ?? '/'}`
|
|
29
|
+
}
|
|
30
|
+
if (path.startsWith('~/') || path === '~') path = join(process.env.HOME ?? process.env.USERPROFILE ?? '', path.slice(1))
|
|
31
|
+
return resolve(cwd, path || '.')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isInside(root, path) {
|
|
35
|
+
const rel = relative(root, path)
|
|
36
|
+
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The real path with symlinks resolved. For a path that doesn't exist yet, its nearest existing parent's real
|
|
41
|
+
* path plus the rest; a dangling symlink on the way is followed to where it points. null if it can't be resolved.
|
|
42
|
+
*/
|
|
43
|
+
export function realPath(path) {
|
|
44
|
+
const rest = []
|
|
45
|
+
let current = path
|
|
46
|
+
for (let step = 0; step < 256; step++) {
|
|
47
|
+
try { return join(realpathSync.native(current), ...rest) } catch { /* missing, or a link to something missing */ }
|
|
48
|
+
let target = null
|
|
49
|
+
try { if (lstatSync(current).isSymbolicLink()) target = readlinkSync(current) } catch { /* nothing here */ }
|
|
50
|
+
if (target !== null) {
|
|
51
|
+
// ".." after a linked folder means its real parent, which resolving by string gets wrong: fail closed.
|
|
52
|
+
if (target.split(/[\\/]+/).includes('..')) return null
|
|
53
|
+
current = resolve(dirname(current), target)
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
const up = dirname(current)
|
|
57
|
+
if (up === current) return join(current, ...rest)
|
|
58
|
+
rest.unshift(basename(current))
|
|
59
|
+
current = up
|
|
60
|
+
}
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function isInsideReal(root, path) {
|
|
65
|
+
const realRoot = realPath(root), real = realPath(path)
|
|
66
|
+
return realRoot !== null && real !== null && isInside(realRoot, real)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function displayPath(cwd, path) {
|
|
70
|
+
return isInside(cwd, path) ? (toPosix(relative(cwd, path)) || '.') : path
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function run(cmd, args, cwd, signal) {
|
|
74
|
+
return new Promise(resolvePromise => {
|
|
75
|
+
const child = spawn(cmd, args, { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] })
|
|
76
|
+
const chunks = []
|
|
77
|
+
let size = 0
|
|
78
|
+
child.stdout.on('data', chunk => {
|
|
79
|
+
size += chunk.length
|
|
80
|
+
if (size < 64 * 1024 * 1024) chunks.push(chunk)
|
|
81
|
+
})
|
|
82
|
+
const onAbort = () => child.kill()
|
|
83
|
+
signal?.addEventListener('abort', onAbort, { once: true })
|
|
84
|
+
child.on('error', () => resolvePromise(null))
|
|
85
|
+
child.on('close', code => {
|
|
86
|
+
signal?.removeEventListener('abort', onAbort)
|
|
87
|
+
resolvePromise(code === 0 ? Buffer.concat(chunks).toString('utf8') : null)
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Files under `root`, relative to it with forward slashes, respecting .gitignore when git or rg can. */
|
|
93
|
+
export async function listFiles(root, signal) {
|
|
94
|
+
if (existsSync(join(root, '.git')) || spawnSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd: root, stdio: 'ignore', windowsHide: true }).status === 0) {
|
|
95
|
+
const listed = await run('git', ['-c', 'core.quotepath=off', 'ls-files', '-z', '--cached', '--others', '--exclude-standard'], root, signal)
|
|
96
|
+
if (listed !== null) return listed.split('\0').filter(Boolean).slice(0, MAX_FILES)
|
|
97
|
+
}
|
|
98
|
+
if (ripgrep()) {
|
|
99
|
+
const listed = await run(ripgrep(), ['--files', '--hidden', '-g', '!.git'], root, signal)
|
|
100
|
+
if (listed !== null) return listed.split(/\r?\n/).filter(Boolean).map(toPosix).slice(0, MAX_FILES)
|
|
101
|
+
}
|
|
102
|
+
const files = []
|
|
103
|
+
const walk = (dir, prefix) => {
|
|
104
|
+
if (files.length >= MAX_FILES || signal?.aborted) return
|
|
105
|
+
let entries
|
|
106
|
+
try { entries = readdirSync(dir, { withFileTypes: true }) } catch { return }
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
if (entry.isDirectory()) {
|
|
109
|
+
if (!SKIP_DIRS.has(entry.name)) walk(join(dir, entry.name), `${prefix}${entry.name}/`)
|
|
110
|
+
} else if (entry.isFile()) {
|
|
111
|
+
files.push(`${prefix}${entry.name}`)
|
|
112
|
+
if (files.length >= MAX_FILES) return
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
walk(root, '')
|
|
117
|
+
return files
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A glob as a RegExp over forward-slash paths: **, *, ?, {a,b} and [...] classes. */
|
|
121
|
+
export function globToRegExp(glob) {
|
|
122
|
+
let i = 0
|
|
123
|
+
const parse = (stopAtComma) => {
|
|
124
|
+
let re = ''
|
|
125
|
+
while (i < glob.length) {
|
|
126
|
+
const ch = glob[i]
|
|
127
|
+
if (stopAtComma && (ch === ',' || ch === '}')) return re
|
|
128
|
+
i++
|
|
129
|
+
if (ch === '*') {
|
|
130
|
+
if (glob[i] === '*') {
|
|
131
|
+
i++
|
|
132
|
+
if (glob[i] === '/') { i++; re += '(?:.*/)?' } else re += '.*'
|
|
133
|
+
} else re += '[^/]*'
|
|
134
|
+
} else if (ch === '?') re += '[^/]'
|
|
135
|
+
else if (ch === '{') {
|
|
136
|
+
const options = []
|
|
137
|
+
for (;;) {
|
|
138
|
+
options.push(parse(true))
|
|
139
|
+
if (glob[i] === ',') { i++; continue }
|
|
140
|
+
if (glob[i] === '}') i++
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
re += `(?:${options.join('|')})`
|
|
144
|
+
} else if (ch === '[') {
|
|
145
|
+
const end = glob.indexOf(']', i)
|
|
146
|
+
if (end === -1) re += '\\['
|
|
147
|
+
else {
|
|
148
|
+
let body = glob.slice(i, end).replace(/\\/g, '\\\\')
|
|
149
|
+
if (body.startsWith('!')) body = `^${body.slice(1)}`
|
|
150
|
+
re += `[${body}]`
|
|
151
|
+
i = end + 1
|
|
152
|
+
}
|
|
153
|
+
} else re += ch.replace(/[.+^$()|\\]/g, '\\$&')
|
|
154
|
+
}
|
|
155
|
+
return re
|
|
156
|
+
}
|
|
157
|
+
return new RegExp(`^${parse(false)}$`, process.platform === 'win32' ? 'i' : '')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function isBinary(buffer) {
|
|
161
|
+
const n = Math.min(buffer.length, 8000)
|
|
162
|
+
for (let i = 0; i < n; i++) if (buffer[i] === 0) return true
|
|
163
|
+
return false
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function mtime(path) {
|
|
167
|
+
try { return statSync(path).mtimeMs } catch { return 0 }
|
|
168
|
+
}
|