@sixb/agent 0.2.4 → 0.2.6

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.
Files changed (117) hide show
  1. package/README.md +2 -2
  2. package/dist/agent-prompt.d.ts +13 -3
  3. package/dist/agent-prompt.d.ts.map +1 -1
  4. package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
  5. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
  6. package/dist/artifacts/publish-tool.d.ts.map +1 -1
  7. package/dist/background-commands.d.ts +2 -2
  8. package/dist/context-compaction.d.ts.map +1 -1
  9. package/dist/conversation-messages.d.ts +1 -1
  10. package/dist/conversation-messages.d.ts.map +1 -1
  11. package/dist/index.d.ts +0 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +11082 -9281
  14. package/dist/index.js.map +37 -33
  15. package/dist/login-shell.d.ts +18 -2
  16. package/dist/login-shell.d.ts.map +1 -1
  17. package/dist/model-adapters.d.ts +5 -2
  18. package/dist/model-adapters.d.ts.map +1 -1
  19. package/dist/model-lines.d.ts +1 -0
  20. package/dist/model-lines.d.ts.map +1 -1
  21. package/dist/run-agent-turn.d.ts +3 -14
  22. package/dist/run-agent-turn.d.ts.map +1 -1
  23. package/dist/run-conversation-turn.d.ts.map +1 -1
  24. package/dist/run-environment.d.ts.map +1 -1
  25. package/dist/run-sandbox.d.ts +11 -1
  26. package/dist/run-sandbox.d.ts.map +1 -1
  27. package/dist/run-subagent.d.ts.map +1 -1
  28. package/dist/sandbox-api-context.d.ts.map +1 -1
  29. package/dist/search-script.d.ts +7 -0
  30. package/dist/search-script.d.ts.map +1 -0
  31. package/dist/skills/built-in.d.ts.map +1 -1
  32. package/dist/subagent-tools.d.ts +11 -1
  33. package/dist/subagent-tools.d.ts.map +1 -1
  34. package/dist/tools/bash.d.ts +1 -1
  35. package/dist/tools/bash.d.ts.map +1 -1
  36. package/dist/tools/glob.d.ts +41 -0
  37. package/dist/tools/glob.d.ts.map +1 -0
  38. package/dist/tools/grep.d.ts +64 -0
  39. package/dist/tools/grep.d.ts.map +1 -0
  40. package/dist/tools/model-spec.d.ts.map +1 -1
  41. package/dist/tools/sandbox-search.d.ts +26 -0
  42. package/dist/tools/sandbox-search.d.ts.map +1 -0
  43. package/dist/tools/search/command.d.ts +15 -0
  44. package/dist/tools/search/command.d.ts.map +1 -0
  45. package/dist/tools/search/gitignore.d.ts +28 -0
  46. package/dist/tools/search/gitignore.d.ts.map +1 -0
  47. package/dist/tools/search/glob.d.ts +15 -0
  48. package/dist/tools/search/glob.d.ts.map +1 -0
  49. package/dist/tools/search/main.d.ts +2 -0
  50. package/dist/tools/search/main.d.ts.map +1 -0
  51. package/dist/tools/search/match.d.ts +30 -0
  52. package/dist/tools/search/match.d.ts.map +1 -0
  53. package/dist/tools/search/pattern.d.ts +19 -0
  54. package/dist/tools/search/pattern.d.ts.map +1 -0
  55. package/dist/tools/search/read.d.ts +62 -0
  56. package/dist/tools/search/read.d.ts.map +1 -0
  57. package/dist/tools/search/request.d.ts +80 -0
  58. package/dist/tools/search/request.d.ts.map +1 -0
  59. package/dist/tools/search/search.d.ts +4 -0
  60. package/dist/tools/search/search.d.ts.map +1 -0
  61. package/dist/tools/search/walk.d.ts +43 -0
  62. package/dist/tools/search/walk.d.ts.map +1 -0
  63. package/dist/tools/tool-outputs.d.ts.map +1 -1
  64. package/dist/turn-inbox.d.ts +75 -0
  65. package/dist/turn-inbox.d.ts.map +1 -0
  66. package/package.json +7 -5
  67. package/src/agent-prompt.ts +63 -28
  68. package/src/agent-runtime/preflight.ts +1 -0
  69. package/src/artifacts/layout-check/check.cjs +176 -95
  70. package/src/artifacts/layout-check/layout-check.ts +118 -76
  71. package/src/artifacts/publish-tool.ts +48 -99
  72. package/src/background-commands.ts +8 -6
  73. package/src/context-compaction.ts +1 -0
  74. package/src/conversation-messages.ts +11 -4
  75. package/src/generated/search.mjs +985 -0
  76. package/src/index.ts +0 -1
  77. package/src/login-shell.ts +30 -3
  78. package/src/model-adapters.ts +23 -4
  79. package/src/model-lines.ts +1 -1
  80. package/src/run-agent-turn.ts +85 -129
  81. package/src/run-conversation-turn.ts +15 -6
  82. package/src/run-environment.ts +42 -11
  83. package/src/run-sandbox.ts +60 -1
  84. package/src/run-subagent.ts +9 -2
  85. package/src/sandbox-api-context.ts +10 -1
  86. package/src/search-script-assets.d.ts +5 -0
  87. package/src/search-script.ts +9 -0
  88. package/src/skills/built-in.ts +16 -0
  89. package/src/skills/sixb-artifacts/SKILL.md +7 -7
  90. package/src/skills/sixb-artifacts/canvas.md +3 -2
  91. package/src/skills/sixb-artifacts/document.md +92 -5
  92. package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
  93. package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
  94. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
  95. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
  96. package/src/skills/sixb-artifacts/queries.md +2 -1
  97. package/src/subagent-tools.ts +23 -5
  98. package/src/tools/bash.ts +9 -10
  99. package/src/tools/glob.ts +124 -0
  100. package/src/tools/grep.ts +190 -0
  101. package/src/tools/model-spec.ts +4 -0
  102. package/src/tools/sandbox-search.ts +219 -0
  103. package/src/tools/search/command.ts +116 -0
  104. package/src/tools/search/gitignore.ts +101 -0
  105. package/src/tools/search/glob.ts +90 -0
  106. package/src/tools/search/main.ts +9 -0
  107. package/src/tools/search/match.ts +278 -0
  108. package/src/tools/search/pattern.ts +178 -0
  109. package/src/tools/search/read.ts +237 -0
  110. package/src/tools/search/request.ts +101 -0
  111. package/src/tools/search/search.ts +297 -0
  112. package/src/tools/search/walk.ts +145 -0
  113. package/src/tools/tool-outputs.ts +7 -2
  114. package/src/turn-inbox.ts +283 -0
  115. package/dist/artifacts/board-previews.d.ts +0 -41
  116. package/dist/artifacts/board-previews.d.ts.map +0 -1
  117. package/src/artifacts/board-previews.ts +0 -218
@@ -0,0 +1,190 @@
1
+ import type { JsonObject, JsonValue, ModelTool } from "@sixb/core/models"
2
+ import { agentToolErrorText } from "../model-adapters"
3
+ import type { AgentSandboxHandle } from "../sandbox-handle"
4
+ import { objectInput } from "./edit"
5
+ import { publicError } from "./file-writes"
6
+ import {
7
+ INCOMPLETE_SEARCH_FOOTER,
8
+ isLeftOut,
9
+ isRecord,
10
+ optionalPathInput,
11
+ runSandboxSearch,
12
+ searchUnavailable,
13
+ } from "./sandbox-search"
14
+ import type { GrepMatch, GrepSearchResult } from "./search/request"
15
+
16
+ export const GREP_TOOL_SPEC = {
17
+ name: "grep",
18
+ strict: true,
19
+ description:
20
+ "Search file contents with a JavaScript regular expression, or with literal text. Case is ignored unless the pattern has an uppercase letter. Searches hidden files, and skips .git, node_modules, what .gitignore excludes, binary files and files over 10 MiB. Matches the first 4,000 characters of a line. Returns matching lines grouped by file, at most 100 in all and 20 per file, with line numbers that read takes as its offset. Prefer this over bash to search text.",
21
+ inputSchema: {
22
+ type: "object",
23
+ properties: {
24
+ pattern: {
25
+ type: "string",
26
+ description: "Regular expression, or the text itself with literal.",
27
+ },
28
+ path: {
29
+ type: "string",
30
+ description:
31
+ "File or folder to search: relative to the working directory, or an absolute path in the run's context directory. Default: the working directory.",
32
+ },
33
+ include: {
34
+ type: "string",
35
+ description: "Glob pattern the files must match, such as *.ts or *.{ts,tsx}.",
36
+ },
37
+ literal: {
38
+ type: "boolean",
39
+ description: "Search the pattern as written, not as a regular expression. Default: false.",
40
+ },
41
+ },
42
+ required: ["pattern"],
43
+ additionalProperties: false,
44
+ } satisfies JsonObject,
45
+ } as const
46
+
47
+ export interface GrepToolInput {
48
+ readonly pattern: string
49
+ readonly path?: string
50
+ readonly include?: string
51
+ readonly literal?: boolean
52
+ }
53
+
54
+ export interface GrepToolOutput {
55
+ readonly pattern: string
56
+ /** As the call named it; `.` for the working directory. */
57
+ readonly path: string
58
+ /** Paths relative to the working directory when under it, absolute otherwise. */
59
+ readonly matches: readonly GrepMatch[]
60
+ /** How many files the matches are in. */
61
+ readonly files: number
62
+ /** Matching lines past the 20 a file shows, by path. */
63
+ readonly more?: Readonly<Record<string, number>>
64
+ /** Another file matches past the 100 lines shown. */
65
+ readonly truncated: boolean
66
+ /** The search stopped at its deadline. */
67
+ readonly incomplete?: true
68
+ }
69
+
70
+ export function createGrepTool(
71
+ resolveSandbox: () => Promise<AgentSandboxHandle>
72
+ ): ModelTool<GrepToolInput> {
73
+ return {
74
+ ...GREP_TOOL_SPEC,
75
+ parseInput: parseGrepInput,
76
+ async execute(input, { signal }): Promise<JsonValue> {
77
+ const handle = await resolveSandbox()
78
+ const result = await runSandboxSearch(handle, { tool: "grep", ...input }, signal)
79
+ if (!isGrepResult(result)) {
80
+ throw searchUnavailable(
81
+ "grep",
82
+ "its output could not be read",
83
+ new Error("Invalid sandbox output.")
84
+ )
85
+ }
86
+
87
+ return {
88
+ pattern: input.pattern,
89
+ path: input.path ?? ".",
90
+ matches: result.matches.map(({ path, line, text }) => ({ path, line, text })),
91
+ files: result.files,
92
+ ...(result.more ? { more: result.more } : {}),
93
+ truncated: result.truncated,
94
+ ...(result.incomplete ? { incomplete: true } : {}),
95
+ } satisfies GrepToolOutput
96
+ },
97
+ toModelOutput(output) {
98
+ return { type: "text", value: grepModelText(output as unknown as GrepToolOutput) }
99
+ },
100
+ errorText: agentToolErrorText,
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Matches grouped by file, each line with its number, then how many more the file holds:
106
+ *
107
+ * ```
108
+ * src/config.ts
109
+ * 12: const retries = 3
110
+ * … 7 more in this file
111
+ * ```
112
+ */
113
+ export function grepModelText(output: GrepToolOutput): string {
114
+ const groups: string[] = []
115
+ let group: string[] = []
116
+ let path: string | undefined
117
+
118
+ const close = () => {
119
+ if (path === undefined) return
120
+ const more = output.more?.[path]
121
+ if (more) group.push(` … ${more} more in this file`)
122
+ groups.push(group.join("\n"))
123
+ }
124
+
125
+ for (const match of output.matches) {
126
+ if (match.path !== path) {
127
+ close()
128
+ path = match.path
129
+ group = [match.path]
130
+ }
131
+ group.push(` ${match.line}: ${match.text}`)
132
+ }
133
+ close()
134
+
135
+ const footers = [
136
+ ...(output.truncated
137
+ ? [
138
+ `(${output.matches.length} matching lines shown, more match: narrow the pattern, the path or include)`,
139
+ ]
140
+ : []),
141
+ ...(output.incomplete ? [INCOMPLETE_SEARCH_FOOTER] : []),
142
+ ]
143
+ return [groups.length > 0 ? groups.join("\n\n") : "No matches", ...footers].join("\n\n")
144
+ }
145
+
146
+ function parseGrepInput(value: unknown): GrepToolInput {
147
+ const input = objectInput(value, "grep", ["pattern", "path", "include", "literal"])
148
+ if (typeof input.pattern !== "string" || input.pattern === "") {
149
+ throw publicError("grep", "pattern must be a non-empty pattern.")
150
+ }
151
+ if (input.literal !== undefined && input.literal !== null && typeof input.literal !== "boolean") {
152
+ throw publicError("grep", "literal must be true or false.")
153
+ }
154
+
155
+ const path = optionalPathInput(input.path, "grep")
156
+ const include = isLeftOut(input.include) ? undefined : input.include
157
+ if (include !== undefined && typeof include !== "string") {
158
+ throw publicError("grep", "include must be a glob pattern.")
159
+ }
160
+
161
+ return {
162
+ pattern: input.pattern,
163
+ ...(path === undefined ? {} : { path }),
164
+ ...(include === undefined ? {} : { include }),
165
+ ...(input.literal === true ? { literal: true } : {}),
166
+ }
167
+ }
168
+
169
+ function isGrepResult(value: unknown): value is GrepSearchResult {
170
+ return (
171
+ isRecord(value) &&
172
+ Array.isArray(value.matches) &&
173
+ value.matches.every(isGrepMatch) &&
174
+ typeof value.files === "number" &&
175
+ (value.more === undefined ||
176
+ (isRecord(value.more) &&
177
+ Object.values(value.more).every((count) => typeof count === "number"))) &&
178
+ typeof value.truncated === "boolean" &&
179
+ (value.incomplete === undefined || value.incomplete === true)
180
+ )
181
+ }
182
+
183
+ function isGrepMatch(value: unknown): value is GrepMatch {
184
+ return (
185
+ isRecord(value) &&
186
+ typeof value.path === "string" &&
187
+ typeof value.line === "number" &&
188
+ typeof value.text === "string"
189
+ )
190
+ }
@@ -4,6 +4,8 @@ import { OPEN_ARTIFACT_TOOL_SPEC, PUBLISH_ARTIFACT_TOOL_SPEC } from "../artifact
4
4
  import { STOP_TOOL_SPEC, WAIT_TOOL_SPEC } from "./background"
5
5
  import { BASH_TOOL_SPEC } from "./bash"
6
6
  import { EDIT_TOOL_SPEC } from "./edit"
7
+ import { GLOB_TOOL_SPEC } from "./glob"
8
+ import { GREP_TOOL_SPEC } from "./grep"
7
9
  import { READ_TOOL_SPEC } from "./read"
8
10
  import { WEB_FETCH_TOOL_SPEC } from "./web-fetch/tool"
9
11
  import { WEB_SEARCH_TOOL_SPEC } from "./web-search"
@@ -28,6 +30,8 @@ export function agentModelToolSpecs(
28
30
  READ_TOOL_SPEC,
29
31
  EDIT_TOOL_SPEC,
30
32
  WRITE_TOOL_SPEC,
33
+ GLOB_TOOL_SPEC,
34
+ GREP_TOOL_SPEC,
31
35
  BASH_TOOL_SPEC,
32
36
  WAIT_TOOL_SPEC,
33
37
  STOP_TOOL_SPEC,
@@ -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
+ }