@sixb/agent 0.2.4 → 0.2.5
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/README.md +2 -2
- package/dist/agent-prompt.d.ts.map +1 -1
- package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
- package/dist/artifacts/publish-tool.d.ts.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10590 -8888
- package/dist/index.js.map +30 -26
- package/dist/run-agent-turn.d.ts +3 -14
- package/dist/run-agent-turn.d.ts.map +1 -1
- package/dist/run-environment.d.ts.map +1 -1
- package/dist/run-subagent.d.ts.map +1 -1
- package/dist/sandbox-api-context.d.ts.map +1 -1
- package/dist/search-script.d.ts +7 -0
- package/dist/search-script.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts.map +1 -1
- package/dist/tools/glob.d.ts +41 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/grep.d.ts +64 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts.map +1 -1
- package/dist/tools/sandbox-search.d.ts +26 -0
- package/dist/tools/sandbox-search.d.ts.map +1 -0
- package/dist/tools/search/command.d.ts +15 -0
- package/dist/tools/search/command.d.ts.map +1 -0
- package/dist/tools/search/gitignore.d.ts +28 -0
- package/dist/tools/search/gitignore.d.ts.map +1 -0
- package/dist/tools/search/glob.d.ts +15 -0
- package/dist/tools/search/glob.d.ts.map +1 -0
- package/dist/tools/search/main.d.ts +2 -0
- package/dist/tools/search/main.d.ts.map +1 -0
- package/dist/tools/search/match.d.ts +30 -0
- package/dist/tools/search/match.d.ts.map +1 -0
- package/dist/tools/search/pattern.d.ts +19 -0
- package/dist/tools/search/pattern.d.ts.map +1 -0
- package/dist/tools/search/read.d.ts +62 -0
- package/dist/tools/search/read.d.ts.map +1 -0
- package/dist/tools/search/request.d.ts +80 -0
- package/dist/tools/search/request.d.ts.map +1 -0
- package/dist/tools/search/search.d.ts +4 -0
- package/dist/tools/search/search.d.ts.map +1 -0
- package/dist/tools/search/walk.d.ts +43 -0
- package/dist/tools/search/walk.d.ts.map +1 -0
- package/dist/tools/tool-outputs.d.ts.map +1 -1
- package/dist/turn-inbox.d.ts +75 -0
- package/dist/turn-inbox.d.ts.map +1 -0
- package/package.json +6 -4
- package/src/agent-prompt.ts +1 -0
- package/src/agent-runtime/preflight.ts +1 -0
- package/src/artifacts/layout-check/check.cjs +176 -95
- package/src/artifacts/layout-check/layout-check.ts +118 -76
- package/src/artifacts/publish-tool.ts +48 -99
- package/src/generated/search.mjs +985 -0
- package/src/index.ts +0 -1
- package/src/run-agent-turn.ts +85 -129
- package/src/run-conversation-turn.ts +4 -4
- package/src/run-environment.ts +4 -0
- package/src/run-subagent.ts +1 -1
- package/src/sandbox-api-context.ts +3 -0
- package/src/search-script-assets.d.ts +5 -0
- package/src/search-script.ts +9 -0
- package/src/skills/built-in.ts +16 -0
- package/src/skills/sixb-artifacts/SKILL.md +7 -7
- package/src/skills/sixb-artifacts/canvas.md +3 -2
- package/src/skills/sixb-artifacts/document.md +92 -5
- package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
- package/src/skills/sixb-artifacts/queries.md +2 -1
- package/src/tools/glob.ts +124 -0
- package/src/tools/grep.ts +190 -0
- package/src/tools/model-spec.ts +4 -0
- package/src/tools/sandbox-search.ts +219 -0
- package/src/tools/search/command.ts +116 -0
- package/src/tools/search/gitignore.ts +101 -0
- package/src/tools/search/glob.ts +90 -0
- package/src/tools/search/main.ts +9 -0
- package/src/tools/search/match.ts +278 -0
- package/src/tools/search/pattern.ts +178 -0
- package/src/tools/search/read.ts +237 -0
- package/src/tools/search/request.ts +101 -0
- package/src/tools/search/search.ts +297 -0
- package/src/tools/search/walk.ts +145 -0
- package/src/tools/tool-outputs.ts +7 -2
- package/src/turn-inbox.ts +283 -0
- package/dist/artifacts/board-previews.d.ts +0 -41
- package/dist/artifacts/board-previews.d.ts.map +0 -1
- package/src/artifacts/board-previews.ts +0 -218
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { AgentToolPublicError, type CommandResult } from "@sixb/core"
|
|
2
|
+
import { AgentToolExecutionError } from "../errors"
|
|
3
|
+
import { type AgentSandboxHandle, contextPath } from "../sandbox-handle"
|
|
4
|
+
import { pathInput } from "./edit"
|
|
5
|
+
import { publicError } from "./file-writes"
|
|
6
|
+
import type { GlobSearchRequest, GrepSearchRequest, SearchErrorCode } from "./search/request"
|
|
7
|
+
|
|
8
|
+
/** How long a search walks before it returns what it found, marked incomplete. */
|
|
9
|
+
const SEARCH_DEADLINE_MS = 20_000
|
|
10
|
+
/** The walk's deadline, plus the time to start the script. */
|
|
11
|
+
const COMMAND_TIMEOUT_MS = 30_000
|
|
12
|
+
|
|
13
|
+
type SearchTool = "glob" | "grep"
|
|
14
|
+
|
|
15
|
+
type SandboxSearchRequest =
|
|
16
|
+
| Omit<GlobSearchRequest, "contextDirectory" | "timeoutMs">
|
|
17
|
+
| Omit<GrepSearchRequest, "contextDirectory" | "timeoutMs">
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Search with the run's `lib/search.mjs`, the same in every sandbox and on a computer. The script
|
|
21
|
+
* confines the search to the working directory and the context directory, as `read` does, and
|
|
22
|
+
* prints one JSON line. Returns that result, unchecked.
|
|
23
|
+
*
|
|
24
|
+
* A search that ran too long, or a script that could not search, is the model's to know: it can
|
|
25
|
+
* narrow the search, or fall back to bash.
|
|
26
|
+
*/
|
|
27
|
+
export async function runSandboxSearch(
|
|
28
|
+
handle: AgentSandboxHandle,
|
|
29
|
+
request: SandboxSearchRequest,
|
|
30
|
+
signal: AbortSignal
|
|
31
|
+
): Promise<unknown> {
|
|
32
|
+
const tool = request.tool
|
|
33
|
+
const body = JSON.stringify({
|
|
34
|
+
...request,
|
|
35
|
+
contextDirectory: contextPath(handle),
|
|
36
|
+
timeoutMs: SEARCH_DEADLINE_MS,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
let result: CommandResult
|
|
40
|
+
try {
|
|
41
|
+
result = await handle.sandbox.runCommand(
|
|
42
|
+
"bash",
|
|
43
|
+
["-c", RUN_SEARCH, `sixb-${tool}`, contextPath(handle, "lib", "search.mjs"), body],
|
|
44
|
+
{
|
|
45
|
+
cwd: handle.sandbox.workingDirectory,
|
|
46
|
+
env: handle.env ?? {},
|
|
47
|
+
timeout: COMMAND_TIMEOUT_MS,
|
|
48
|
+
signal,
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
signal.throwIfAborted()
|
|
53
|
+
throw new AgentToolExecutionError(tool, { cause: error })
|
|
54
|
+
}
|
|
55
|
+
signal.throwIfAborted()
|
|
56
|
+
|
|
57
|
+
const cause = new Error(result.stderr.trim() || `search.mjs exited with code ${result.exitCode}.`)
|
|
58
|
+
|
|
59
|
+
if (result.timedOut) {
|
|
60
|
+
throw new AgentToolPublicError(
|
|
61
|
+
`[SixbAgent] ${tool}: The search took too long: ${
|
|
62
|
+
tool === "grep"
|
|
63
|
+
? "narrow the path, simplify the pattern, or pass literal: true."
|
|
64
|
+
: "narrow the path or the pattern."
|
|
65
|
+
}`,
|
|
66
|
+
{ cause }
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (result.exitCode === 0) {
|
|
71
|
+
const output = lastJsonLine(result.stdout)
|
|
72
|
+
if (output !== undefined) return output
|
|
73
|
+
throw searchUnavailable(tool, "its output could not be read", cause)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const failure = scriptFailure(lastJsonLine(result.stderr))
|
|
77
|
+
if (result.exitCode === 2 && failure && isSearchErrorCode(failure.code)) {
|
|
78
|
+
throw searchError(tool, request.path ?? ".", failure.code, failure.message)
|
|
79
|
+
}
|
|
80
|
+
throw searchUnavailable(tool, unavailableReason(result.exitCode, failure), cause)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A search the run's script could not make: the model reads why, in words of ours rather than the
|
|
85
|
+
* sandbox's, and the way out. The script's own words stay in the cause.
|
|
86
|
+
*/
|
|
87
|
+
export function searchUnavailable(
|
|
88
|
+
tool: SearchTool,
|
|
89
|
+
reason: string,
|
|
90
|
+
cause: unknown
|
|
91
|
+
): AgentToolPublicError {
|
|
92
|
+
return new AgentToolPublicError(
|
|
93
|
+
`[SixbAgent] ${tool}: The search could not run in this sandbox (${reason}): use bash with find and grep instead.`,
|
|
94
|
+
{ cause }
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function unavailableReason(
|
|
99
|
+
exitCode: number,
|
|
100
|
+
failure: { readonly code: string; readonly message: string } | undefined
|
|
101
|
+
): string {
|
|
102
|
+
if (failure?.code === "runtime_unavailable") return "the search script needs Bun or Node.js"
|
|
103
|
+
if (failure?.code === "script_missing") return "the run has no search script"
|
|
104
|
+
return `the search script exited with code ${exitCode}`
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** An optional value left out: models send `"null"` or `"undefined"` for one. */
|
|
108
|
+
export function isLeftOut(value: unknown): boolean {
|
|
109
|
+
return (
|
|
110
|
+
value === undefined ||
|
|
111
|
+
value === null ||
|
|
112
|
+
value === "" ||
|
|
113
|
+
value === "null" ||
|
|
114
|
+
value === "undefined"
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function optionalPathInput(value: unknown, tool: SearchTool): string | undefined {
|
|
119
|
+
return isLeftOut(value) ? undefined : pathInput(value, tool)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const INCOMPLETE_SEARCH_FOOTER =
|
|
123
|
+
"(The search stopped after 20 seconds, before it looked everywhere: narrow the pattern or the path)"
|
|
124
|
+
|
|
125
|
+
function searchError(tool: SearchTool, path: string, code: SearchErrorCode, reason: string): Error {
|
|
126
|
+
switch (code) {
|
|
127
|
+
case "path_missing":
|
|
128
|
+
return publicError(tool, `'${path}' does not exist.`)
|
|
129
|
+
case "path_outside":
|
|
130
|
+
return publicError(
|
|
131
|
+
tool,
|
|
132
|
+
`'${path}' resolves outside the working directory and the run's context directory.`
|
|
133
|
+
)
|
|
134
|
+
case "path_is_file":
|
|
135
|
+
return publicError(tool, `'${path}' is a file: read it, or grep it.`)
|
|
136
|
+
case "path_not_searchable":
|
|
137
|
+
return publicError(tool, `'${path}' is not a file or a directory.`)
|
|
138
|
+
case "invalid_pattern":
|
|
139
|
+
return publicError(
|
|
140
|
+
tool,
|
|
141
|
+
tool === "grep"
|
|
142
|
+
? `Invalid pattern: ${reason}. Pass literal: true to search the text as written.`
|
|
143
|
+
: `Invalid pattern: ${reason}.`
|
|
144
|
+
)
|
|
145
|
+
case "invalid_include":
|
|
146
|
+
return publicError(tool, `Invalid include: ${reason}.`)
|
|
147
|
+
case "file_binary":
|
|
148
|
+
return publicError(tool, `'${path}' is binary, or not UTF-8 text: ${tool} searches text.`)
|
|
149
|
+
case "file_too_large":
|
|
150
|
+
return publicError(tool, `'${path}' is over 10 MiB: search it with bash.`)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const SEARCH_ERROR_CODES: readonly SearchErrorCode[] = [
|
|
155
|
+
"path_missing",
|
|
156
|
+
"path_outside",
|
|
157
|
+
"path_is_file",
|
|
158
|
+
"path_not_searchable",
|
|
159
|
+
"invalid_pattern",
|
|
160
|
+
"invalid_include",
|
|
161
|
+
"file_binary",
|
|
162
|
+
"file_too_large",
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
function isSearchErrorCode(value: unknown): value is SearchErrorCode {
|
|
166
|
+
return SEARCH_ERROR_CODES.some((code) => code === value)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The script's `{ error: { code, message } }`. */
|
|
170
|
+
function scriptFailure(
|
|
171
|
+
value: unknown
|
|
172
|
+
): { readonly code: string; readonly message: string } | undefined {
|
|
173
|
+
const error = isRecord(value) ? value.error : undefined
|
|
174
|
+
if (!isRecord(error) || typeof error.code !== "string" || typeof error.message !== "string") {
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
return { code: error.code, message: error.message }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The last line that parses as JSON: in the login-shell fallback, a profile may print before the
|
|
182
|
+
* script does.
|
|
183
|
+
*/
|
|
184
|
+
function lastJsonLine(text: string): unknown {
|
|
185
|
+
const lines = text.split("\n").filter((line) => line.trim() !== "")
|
|
186
|
+
const last = lines.at(-1)
|
|
187
|
+
if (last === undefined) return undefined
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
return JSON.parse(last)
|
|
191
|
+
} catch {
|
|
192
|
+
return undefined
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
197
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Runs the script at its absolute path on Bun, or else Node. Preflight found one of them in a
|
|
202
|
+
* login shell: when this shell's PATH has neither, the search runs in one, as `exec` so no logout
|
|
203
|
+
* script runs. A missing script, or a login shell with neither, prints an error as the script
|
|
204
|
+
* does.
|
|
205
|
+
*
|
|
206
|
+
* Arguments: the script's path, the request.
|
|
207
|
+
*/
|
|
208
|
+
const RUN_SEARCH = `set -u
|
|
209
|
+
if [ ! -r "$1" ]; then
|
|
210
|
+
echo '{"error":{"code":"script_missing","message":"The search script is missing."}}' >&2
|
|
211
|
+
exit 2
|
|
212
|
+
fi
|
|
213
|
+
command -v bun >/dev/null 2>&1 && exec bun "$1" "$2"
|
|
214
|
+
command -v node >/dev/null 2>&1 && exec node "$1" "$2"
|
|
215
|
+
exec "$BASH" -lc '
|
|
216
|
+
command -v bun >/dev/null 2>&1 && exec bun "$0" "$1"
|
|
217
|
+
command -v node >/dev/null 2>&1 && exec node "$0" "$1"
|
|
218
|
+
echo "{\\"error\\":{\\"code\\":\\"runtime_unavailable\\",\\"message\\":\\"The search needs Bun or Node.js.\\"}}" >&2
|
|
219
|
+
exit 2' "$1" "$2"`
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GlobSearchRequest,
|
|
3
|
+
type GrepSearchRequest,
|
|
4
|
+
SearchError,
|
|
5
|
+
type SearchRequest,
|
|
6
|
+
} from "./request"
|
|
7
|
+
import { globFiles, grepFiles } from "./search"
|
|
8
|
+
|
|
9
|
+
const USAGE = "Usage: search.mjs <request JSON>"
|
|
10
|
+
|
|
11
|
+
const KEYS: Readonly<Record<SearchRequest["tool"], readonly string[]>> = {
|
|
12
|
+
glob: ["tool", "pattern", "path", "contextDirectory", "timeoutMs"],
|
|
13
|
+
grep: ["tool", "pattern", "path", "include", "literal", "contextDirectory", "timeoutMs"],
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** What the process prints, and the code it exits with. */
|
|
17
|
+
export interface SearchCommandResult {
|
|
18
|
+
readonly exitCode: number
|
|
19
|
+
readonly stdout: string
|
|
20
|
+
readonly stderr: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `search.mjs '<request JSON>'`: the Agent's `glob` and `grep`, the same in every sandbox. The
|
|
25
|
+
* request is one JSON argument, so no shell quoting reaches a pattern; the result is one JSON
|
|
26
|
+
* line. A search that cannot run exits 2 with `{"error":{"code","message"}}` on stderr: a request
|
|
27
|
+
* it does not understand is `invalid_arguments`, and a search the request names wrongly has its
|
|
28
|
+
* own code ({@link SearchError}). Anything else exits 3, as `internal_error`.
|
|
29
|
+
*/
|
|
30
|
+
export async function runSearchCommand(
|
|
31
|
+
args: readonly string[],
|
|
32
|
+
workingDirectory: string
|
|
33
|
+
): Promise<SearchCommandResult> {
|
|
34
|
+
try {
|
|
35
|
+
const request = parseRequest(args)
|
|
36
|
+
const result =
|
|
37
|
+
request.tool === "glob"
|
|
38
|
+
? globFiles(request, workingDirectory)
|
|
39
|
+
: await grepFiles(request, workingDirectory)
|
|
40
|
+
|
|
41
|
+
return { exitCode: 0, stdout: `${JSON.stringify(result)}\n`, stderr: "" }
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error instanceof SearchError) return failure(2, error.code, error.message)
|
|
44
|
+
if (error instanceof RequestError) return failure(2, "invalid_arguments", error.message)
|
|
45
|
+
|
|
46
|
+
const message = error instanceof Error ? error.message : "The search failed unexpectedly."
|
|
47
|
+
return failure(3, "internal_error", message)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class RequestError extends Error {}
|
|
52
|
+
|
|
53
|
+
function failure(exitCode: number, code: string, message: string): SearchCommandResult {
|
|
54
|
+
return { exitCode, stdout: "", stderr: `${JSON.stringify({ error: { code, message } })}\n` }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function fail(message: string): never {
|
|
58
|
+
throw new RequestError(message)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function parseRequest(args: readonly string[]): SearchRequest {
|
|
62
|
+
if (args.length !== 1) fail(USAGE)
|
|
63
|
+
|
|
64
|
+
let value: unknown
|
|
65
|
+
try {
|
|
66
|
+
value = JSON.parse(args[0] as string)
|
|
67
|
+
} catch {
|
|
68
|
+
fail("The search request is not valid JSON.")
|
|
69
|
+
}
|
|
70
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
71
|
+
fail("The search request must be a JSON object.")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const input = value as Record<string, unknown>
|
|
75
|
+
const tool = input.tool
|
|
76
|
+
if (tool !== "glob" && tool !== "grep") fail("tool must be 'glob' or 'grep'.")
|
|
77
|
+
for (const key of Object.keys(input)) {
|
|
78
|
+
if (!KEYS[tool].includes(key)) fail(`The ${tool} request has an unknown property '${key}'.`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const pattern = nonEmptyString(input.pattern, "pattern")
|
|
82
|
+
const timeoutMs = input.timeoutMs
|
|
83
|
+
if (typeof timeoutMs !== "number" || !Number.isSafeInteger(timeoutMs) || timeoutMs < 0) {
|
|
84
|
+
fail("timeoutMs must be a non-negative integer.")
|
|
85
|
+
}
|
|
86
|
+
const path = optionalString(input.path, "path")
|
|
87
|
+
const contextDirectory = optionalString(input.contextDirectory, "contextDirectory")
|
|
88
|
+
const scope = {
|
|
89
|
+
timeoutMs,
|
|
90
|
+
...(path === undefined ? {} : { path }),
|
|
91
|
+
...(contextDirectory === undefined ? {} : { contextDirectory }),
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (tool === "glob") return { tool, pattern, ...scope } satisfies GlobSearchRequest
|
|
95
|
+
|
|
96
|
+
if (input.literal !== undefined && typeof input.literal !== "boolean") {
|
|
97
|
+
fail("literal must be true or false.")
|
|
98
|
+
}
|
|
99
|
+
const include = optionalString(input.include, "include")
|
|
100
|
+
return {
|
|
101
|
+
tool,
|
|
102
|
+
pattern,
|
|
103
|
+
...scope,
|
|
104
|
+
...(include === undefined ? {} : { include }),
|
|
105
|
+
...(input.literal === undefined ? {} : { literal: input.literal }),
|
|
106
|
+
} satisfies GrepSearchRequest
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function nonEmptyString(value: unknown, field: string): string {
|
|
110
|
+
if (typeof value !== "string" || value === "") fail(`${field} must be a non-empty string.`)
|
|
111
|
+
return value
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function optionalString(value: unknown, field: string): string | undefined {
|
|
115
|
+
return value === undefined ? undefined : nonEmptyString(value, field)
|
|
116
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { compilePattern, PatternError, pathPatternSource, segmentSource } from "./pattern"
|
|
2
|
+
|
|
3
|
+
/** One line of a `.gitignore`. */
|
|
4
|
+
export interface IgnoreRule {
|
|
5
|
+
/** `!pattern`: brings back what an earlier rule excluded. */
|
|
6
|
+
readonly negated: boolean
|
|
7
|
+
/** `pattern/`: matches folders only. */
|
|
8
|
+
readonly folderOnly: boolean
|
|
9
|
+
/** No `/` but a trailing one: matches a name at any depth below the file's folder. */
|
|
10
|
+
readonly anyDepth: boolean
|
|
11
|
+
readonly regex: RegExp
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A `.gitignore` and the folder it applies to, an absolute path. */
|
|
15
|
+
export interface IgnoreFile {
|
|
16
|
+
readonly folder: string
|
|
17
|
+
readonly rules: readonly IgnoreRule[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Parse a `.gitignore` as git reads it. A line that is not a valid pattern is skipped, as git
|
|
22
|
+
* skips it.
|
|
23
|
+
*/
|
|
24
|
+
export function parseGitignore(text: string): IgnoreRule[] {
|
|
25
|
+
const rules: IgnoreRule[] = []
|
|
26
|
+
|
|
27
|
+
// Git drops a UTF-8 byte order mark, which would otherwise join the first pattern.
|
|
28
|
+
const body = text.startsWith("\uFEFF") ? text.slice(1) : text
|
|
29
|
+
|
|
30
|
+
for (const rawLine of body.split("\n")) {
|
|
31
|
+
const rule = parseLine(rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine)
|
|
32
|
+
if (rule) rules.push(rule)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return rules
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseLine(rawLine: string): IgnoreRule | undefined {
|
|
39
|
+
// `\#` and `\!` stay escaped: the pattern compiler reads them as the characters themselves.
|
|
40
|
+
if (rawLine === "" || rawLine.startsWith("#")) return undefined
|
|
41
|
+
|
|
42
|
+
let line = withoutTrailingSpaces(rawLine)
|
|
43
|
+
const negated = line.startsWith("!")
|
|
44
|
+
if (negated) line = line.slice(1)
|
|
45
|
+
|
|
46
|
+
const folderOnly = line.endsWith("/")
|
|
47
|
+
line = line.replace(/\/+$/, "")
|
|
48
|
+
|
|
49
|
+
// A `/` at the start or in the middle anchors the pattern to the file's folder.
|
|
50
|
+
const anyDepth = !line.includes("/")
|
|
51
|
+
line = line.replace(/^\//, "")
|
|
52
|
+
if (!line) return undefined
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const regex = compilePattern(anyDepth ? segmentSource(line) : pathPatternSource(line))
|
|
56
|
+
return { negated, folderOnly, anyDepth, regex }
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof PatternError) return undefined
|
|
59
|
+
throw error
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Trailing spaces are dropped, unless a backslash escapes them. */
|
|
64
|
+
function withoutTrailingSpaces(line: string): string {
|
|
65
|
+
let end = line.length
|
|
66
|
+
|
|
67
|
+
while (end > 0 && line[end - 1] === " ") {
|
|
68
|
+
let backslashes = 0
|
|
69
|
+
while (line[end - 2 - backslashes] === "\\") backslashes += 1
|
|
70
|
+
if (backslashes % 2 === 1) break
|
|
71
|
+
end -= 1
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return line.slice(0, end)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Whether the `.gitignore` files that apply exclude an entry, given as an absolute path. Files
|
|
79
|
+
* come from the outermost folder in, and a later rule wins over an earlier one, so a deeper file
|
|
80
|
+
* overrides its parents. What lies inside an excluded folder is never asked about: the walk does
|
|
81
|
+
* not enter it, so a rule cannot bring it back, as in git.
|
|
82
|
+
*/
|
|
83
|
+
export function isIgnored(files: readonly IgnoreFile[], path: string, isFolder: boolean): boolean {
|
|
84
|
+
const name = path.slice(path.lastIndexOf("/") + 1)
|
|
85
|
+
let ignored = false
|
|
86
|
+
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const prefix = file.folder.endsWith("/") ? file.folder : `${file.folder}/`
|
|
89
|
+
if (!path.startsWith(prefix)) continue
|
|
90
|
+
const relative = path.slice(prefix.length)
|
|
91
|
+
|
|
92
|
+
for (const rule of file.rules) {
|
|
93
|
+
if (rule.folderOnly && !isFolder) continue
|
|
94
|
+
// Only a rule that would change the verdict so far needs testing.
|
|
95
|
+
if (rule.negated !== ignored) continue
|
|
96
|
+
if (rule.regex.test(rule.anyDepth ? name : relative)) ignored = !rule.negated
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return ignored
|
|
101
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compilePattern,
|
|
3
|
+
expandBraces,
|
|
4
|
+
PatternError,
|
|
5
|
+
pathPatternSource,
|
|
6
|
+
segmentSource,
|
|
7
|
+
} from "./pattern"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A `glob` pattern, matched against paths relative to where the search starts. A pattern without
|
|
11
|
+
* `/` matches a name at any depth (`*.md` finds every Markdown file), as in `.gitignore`; one with
|
|
12
|
+
* `/` matches from the start, with `**` for any number of folders.
|
|
13
|
+
*/
|
|
14
|
+
export interface GlobMatcher {
|
|
15
|
+
matches(relativePath: string): boolean
|
|
16
|
+
/** Whether a folder, relative to where the search starts, may hold a match. */
|
|
17
|
+
mayContain(relativeFolder: string): boolean
|
|
18
|
+
/** A segment starts with `.`: the pattern looks for hidden files. */
|
|
19
|
+
readonly namesHidden: boolean
|
|
20
|
+
readonly namesNodeModules: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Alternative {
|
|
24
|
+
/** Matched against the name alone, at any depth. */
|
|
25
|
+
readonly anyDepth: boolean
|
|
26
|
+
readonly regex: RegExp
|
|
27
|
+
readonly segments: readonly string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function compileGlob(pattern: string): GlobMatcher {
|
|
31
|
+
const alternatives = expandBraces(pattern).map(compileAlternative)
|
|
32
|
+
const segments = alternatives.flatMap((alternative) => alternative.segments)
|
|
33
|
+
const folderSegments = new Map<string, RegExp>()
|
|
34
|
+
|
|
35
|
+
// One folder segment against one pattern segment, compiled once for the whole walk.
|
|
36
|
+
const segmentMatches = (patternSegment: string, name: string): boolean => {
|
|
37
|
+
let regex = folderSegments.get(patternSegment)
|
|
38
|
+
if (!regex) {
|
|
39
|
+
regex = compilePattern(segmentSource(patternSegment))
|
|
40
|
+
folderSegments.set(patternSegment, regex)
|
|
41
|
+
}
|
|
42
|
+
return regex.test(name)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
matches(relativePath) {
|
|
47
|
+
const name = relativePath.slice(relativePath.lastIndexOf("/") + 1)
|
|
48
|
+
return alternatives.some((alternative) =>
|
|
49
|
+
alternative.regex.test(alternative.anyDepth ? name : relativePath)
|
|
50
|
+
)
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
mayContain(relativeFolder) {
|
|
54
|
+
const names = relativeFolder.split("/")
|
|
55
|
+
return alternatives.some((alternative) => {
|
|
56
|
+
if (alternative.anyDepth) return true
|
|
57
|
+
|
|
58
|
+
for (const [index, name] of names.entries()) {
|
|
59
|
+
const segment = alternative.segments[index]
|
|
60
|
+
if (segment === undefined) return false
|
|
61
|
+
if (segment === "**") return true
|
|
62
|
+
if (!segmentMatches(segment, name)) return false
|
|
63
|
+
}
|
|
64
|
+
// The folder matched as far as it goes: the pattern must still name what is inside.
|
|
65
|
+
return alternative.segments.length > names.length
|
|
66
|
+
})
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
namesHidden: segments.some((segment) => segment.startsWith(".")),
|
|
70
|
+
namesNodeModules: segments.some((segment) => segment.includes("node_modules")),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function compileAlternative(pattern: string): Alternative {
|
|
75
|
+
let relative = pattern
|
|
76
|
+
while (relative.startsWith("./")) relative = relative.slice(2)
|
|
77
|
+
|
|
78
|
+
// A leading `/` anchors the pattern where the search starts, as in `.gitignore`.
|
|
79
|
+
const anchored = relative.startsWith("/")
|
|
80
|
+
if (anchored) relative = relative.replace(/^\/+/, "")
|
|
81
|
+
if (!relative) throw new PatternError("it names no file")
|
|
82
|
+
|
|
83
|
+
const anyDepth = !anchored && !relative.includes("/")
|
|
84
|
+
const segments = relative.split("/")
|
|
85
|
+
return {
|
|
86
|
+
anyDepth,
|
|
87
|
+
regex: compilePattern(anyDepth ? segmentSource(relative) : pathPatternSource(relative)),
|
|
88
|
+
segments,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The engine of the Agent's `glob` and `grep`, as a run installs it at `lib/search.mjs` and runs
|
|
2
|
+
// it on Bun or Node: `bun run generate:search-script` bundles it into src/generated/search.mjs.
|
|
3
|
+
import { runSearchCommand } from "./command"
|
|
4
|
+
|
|
5
|
+
const result = await runSearchCommand(process.argv.slice(2), process.cwd())
|
|
6
|
+
|
|
7
|
+
process.stdout.write(result.stdout)
|
|
8
|
+
process.stderr.write(result.stderr)
|
|
9
|
+
process.exitCode = result.exitCode
|