@sixb/agent 0.2.3 → 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.
Files changed (149) hide show
  1. package/README.md +3 -3
  2. package/dist/agent-prompt.d.ts.map +1 -1
  3. package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
  4. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
  5. package/dist/artifacts/publish-tool.d.ts +26 -44
  6. package/dist/artifacts/publish-tool.d.ts.map +1 -1
  7. package/dist/ask-tool.d.ts +62 -0
  8. package/dist/ask-tool.d.ts.map +1 -0
  9. package/dist/attachments.d.ts +1 -1
  10. package/dist/attachments.d.ts.map +1 -1
  11. package/dist/background-commands.d.ts +3 -3
  12. package/dist/background-commands.d.ts.map +1 -1
  13. package/dist/background-notices.d.ts.map +1 -1
  14. package/dist/child-files.d.ts +1 -1
  15. package/dist/child-questions.d.ts +1 -1
  16. package/dist/index.d.ts +1 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +10457 -7971
  19. package/dist/index.js.map +46 -38
  20. package/dist/run-agent-turn.d.ts +3 -14
  21. package/dist/run-agent-turn.d.ts.map +1 -1
  22. package/dist/run-conversation-turn.d.ts.map +1 -1
  23. package/dist/run-environment.d.ts +4 -1
  24. package/dist/run-environment.d.ts.map +1 -1
  25. package/dist/run-subagent.d.ts +1 -1
  26. package/dist/run-subagent.d.ts.map +1 -1
  27. package/dist/sandbox-api-context.d.ts.map +1 -1
  28. package/dist/search-script.d.ts +7 -0
  29. package/dist/search-script.d.ts.map +1 -0
  30. package/dist/skills/built-in.d.ts.map +1 -1
  31. package/dist/subagent-tools.d.ts +20 -1
  32. package/dist/subagent-tools.d.ts.map +1 -1
  33. package/dist/tools/background.d.ts +50 -18
  34. package/dist/tools/background.d.ts.map +1 -1
  35. package/dist/tools/bash.d.ts +1 -0
  36. package/dist/tools/bash.d.ts.map +1 -1
  37. package/dist/tools/edit-text.d.ts +22 -0
  38. package/dist/tools/edit-text.d.ts.map +1 -0
  39. package/dist/tools/edit.d.ts +54 -0
  40. package/dist/tools/edit.d.ts.map +1 -0
  41. package/dist/tools/file-writes.d.ts +52 -0
  42. package/dist/tools/file-writes.d.ts.map +1 -0
  43. package/dist/tools/glob.d.ts +41 -0
  44. package/dist/tools/glob.d.ts.map +1 -0
  45. package/dist/tools/grep.d.ts +64 -0
  46. package/dist/tools/grep.d.ts.map +1 -0
  47. package/dist/tools/model-spec.d.ts.map +1 -1
  48. package/dist/tools/read.d.ts +16 -3
  49. package/dist/tools/read.d.ts.map +1 -1
  50. package/dist/tools/sandbox-search.d.ts +26 -0
  51. package/dist/tools/sandbox-search.d.ts.map +1 -0
  52. package/dist/tools/search/command.d.ts +15 -0
  53. package/dist/tools/search/command.d.ts.map +1 -0
  54. package/dist/tools/search/gitignore.d.ts +28 -0
  55. package/dist/tools/search/gitignore.d.ts.map +1 -0
  56. package/dist/tools/search/glob.d.ts +15 -0
  57. package/dist/tools/search/glob.d.ts.map +1 -0
  58. package/dist/tools/search/main.d.ts +2 -0
  59. package/dist/tools/search/main.d.ts.map +1 -0
  60. package/dist/tools/search/match.d.ts +30 -0
  61. package/dist/tools/search/match.d.ts.map +1 -0
  62. package/dist/tools/search/pattern.d.ts +19 -0
  63. package/dist/tools/search/pattern.d.ts.map +1 -0
  64. package/dist/tools/search/read.d.ts +62 -0
  65. package/dist/tools/search/read.d.ts.map +1 -0
  66. package/dist/tools/search/request.d.ts +80 -0
  67. package/dist/tools/search/request.d.ts.map +1 -0
  68. package/dist/tools/search/search.d.ts +4 -0
  69. package/dist/tools/search/search.d.ts.map +1 -0
  70. package/dist/tools/search/walk.d.ts +43 -0
  71. package/dist/tools/search/walk.d.ts.map +1 -0
  72. package/dist/tools/tool-outputs.d.ts.map +1 -1
  73. package/dist/tools/view-file.d.ts +20 -23
  74. package/dist/tools/view-file.d.ts.map +1 -1
  75. package/dist/tools/web-fetch/tool.d.ts +1 -0
  76. package/dist/tools/web-fetch/tool.d.ts.map +1 -1
  77. package/dist/tools/web-search.d.ts +1 -0
  78. package/dist/tools/web-search.d.ts.map +1 -1
  79. package/dist/tools/write.d.ts +36 -0
  80. package/dist/tools/write.d.ts.map +1 -0
  81. package/dist/turn-inbox.d.ts +75 -0
  82. package/dist/turn-inbox.d.ts.map +1 -0
  83. package/package.json +6 -4
  84. package/src/agent-prompt.ts +10 -9
  85. package/src/agent-runtime/preflight.ts +1 -0
  86. package/src/artifacts/layout-check/check.cjs +176 -95
  87. package/src/artifacts/layout-check/layout-check.ts +118 -76
  88. package/src/artifacts/publish-tool.ts +123 -192
  89. package/src/ask-tool.ts +223 -0
  90. package/src/attachments.ts +7 -6
  91. package/src/background-commands.ts +5 -5
  92. package/src/background-notices.ts +5 -12
  93. package/src/child-files.ts +1 -1
  94. package/src/child-questions.ts +1 -1
  95. package/src/context-compaction.ts +4 -1
  96. package/src/generated/search.mjs +985 -0
  97. package/src/index.ts +1 -2
  98. package/src/run-agent-turn.ts +99 -133
  99. package/src/run-child.ts +1 -1
  100. package/src/run-conversation-turn.ts +8 -5
  101. package/src/run-environment.ts +40 -21
  102. package/src/run-subagent.ts +4 -4
  103. package/src/sandbox-api-context.ts +3 -0
  104. package/src/search-script-assets.d.ts +5 -0
  105. package/src/search-script.ts +9 -0
  106. package/src/skills/built-in.ts +16 -0
  107. package/src/skills/sixb-artifacts/SKILL.md +13 -13
  108. package/src/skills/sixb-artifacts/canvas.md +5 -4
  109. package/src/skills/sixb-artifacts/document.md +92 -5
  110. package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
  111. package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
  112. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
  113. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
  114. package/src/skills/sixb-artifacts/page.md +2 -2
  115. package/src/skills/sixb-artifacts/queries.md +2 -1
  116. package/src/skills/sixb-artifacts/slides.md +2 -2
  117. package/src/subagent-tools.ts +115 -81
  118. package/src/tools/background.ts +227 -67
  119. package/src/tools/bash.ts +2 -1
  120. package/src/tools/edit-text.ts +241 -0
  121. package/src/tools/edit.ts +166 -0
  122. package/src/tools/file-writes.ts +270 -0
  123. package/src/tools/glob.ts +124 -0
  124. package/src/tools/grep.ts +190 -0
  125. package/src/tools/model-spec.ts +13 -13
  126. package/src/tools/read.ts +75 -9
  127. package/src/tools/sandbox-search.ts +219 -0
  128. package/src/tools/search/command.ts +116 -0
  129. package/src/tools/search/gitignore.ts +101 -0
  130. package/src/tools/search/glob.ts +90 -0
  131. package/src/tools/search/main.ts +9 -0
  132. package/src/tools/search/match.ts +278 -0
  133. package/src/tools/search/pattern.ts +178 -0
  134. package/src/tools/search/read.ts +237 -0
  135. package/src/tools/search/request.ts +101 -0
  136. package/src/tools/search/search.ts +297 -0
  137. package/src/tools/search/walk.ts +145 -0
  138. package/src/tools/tool-outputs.ts +9 -4
  139. package/src/tools/view-file.ts +78 -80
  140. package/src/tools/web-fetch/tool.ts +2 -1
  141. package/src/tools/web-search.ts +1 -0
  142. package/src/tools/write.ts +95 -0
  143. package/src/turn-inbox.ts +283 -0
  144. package/dist/artifacts/board-previews.d.ts +0 -41
  145. package/dist/artifacts/board-previews.d.ts.map +0 -1
  146. package/dist/ask-parent-tool.d.ts +0 -36
  147. package/dist/ask-parent-tool.d.ts.map +0 -1
  148. package/src/artifacts/board-previews.ts +0 -218
  149. package/src/ask-parent-tool.ts +0 -100
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Path patterns, as `glob` and `.gitignore` write them: `*` and `?` within a name, `[abc]`,
3
+ * `[!abc]` and `[^abc]`, `**` as a whole segment for any number of folders, `\` to escape.
4
+ * `{a,b}` belongs to `glob` alone and is expanded before compiling ({@link expandBraces}).
5
+ */
6
+
7
+ /** More alternatives than any real pattern names: past it, `{…}` is a mistake. */
8
+ const MAX_ALTERNATIVES = 256
9
+
10
+ export class PatternError extends Error {}
11
+
12
+ /** A pattern's regular expression source, for a whole path relative to where it applies. */
13
+ export function pathPatternSource(pattern: string): string {
14
+ const segments = pattern.split("/")
15
+ let source = ""
16
+
17
+ segments.forEach((segment, index) => {
18
+ const last = index === segments.length - 1
19
+
20
+ if (segment === "**") {
21
+ // `a/**` is everything inside a; `**/b` and `a/**/b` cross any number of folders.
22
+ source += last ? ".*" : "(?:[^/]*/)*"
23
+ return
24
+ }
25
+
26
+ source += segmentSource(segment)
27
+ if (!last) source += "/"
28
+ })
29
+
30
+ return source
31
+ }
32
+
33
+ /** One name's source: never crosses a `/`. */
34
+ export function segmentSource(segment: string): string {
35
+ let source = ""
36
+
37
+ for (let index = 0; index < segment.length; index += 1) {
38
+ const char = segment[index] as string
39
+
40
+ if (char === "\\") {
41
+ index += 1
42
+ source += escapeRegExp(segment[index] ?? "\\")
43
+ } else if (char === "*") {
44
+ while (segment[index + 1] === "*") index += 1
45
+ source += "[^/]*"
46
+ } else if (char === "?") {
47
+ source += "[^/]"
48
+ } else if (char === "[") {
49
+ const parsed = characterClass(segment, index)
50
+ if (parsed) {
51
+ source += parsed.source
52
+ index = parsed.end
53
+ } else {
54
+ source += "\\["
55
+ }
56
+ } else {
57
+ source += escapeRegExp(char)
58
+ }
59
+ }
60
+
61
+ return source
62
+ }
63
+
64
+ /** `[...]` from `start`, or nothing when it never closes and is a literal `[`. */
65
+ function characterClass(
66
+ segment: string,
67
+ start: number
68
+ ): { readonly source: string; readonly end: number } | undefined {
69
+ let index = start + 1
70
+ const negated = segment[index] === "!" || segment[index] === "^"
71
+ if (negated) index += 1
72
+
73
+ let body = ""
74
+ // A `]` right after the opening is part of the class.
75
+ if (segment[index] === "]") {
76
+ body += "\\]"
77
+ index += 1
78
+ }
79
+
80
+ for (; index < segment.length; index += 1) {
81
+ const char = segment[index] as string
82
+
83
+ if (char === "]") {
84
+ return { source: negated ? `[^/${body}]` : `[${body}]`, end: index }
85
+ }
86
+ if (char === "\\" && index + 1 < segment.length) {
87
+ index += 1
88
+ body += escapeClassChar(segment[index] as string)
89
+ } else {
90
+ body += char === "-" ? "-" : escapeClassChar(char)
91
+ }
92
+ }
93
+
94
+ return undefined
95
+ }
96
+
97
+ /**
98
+ * Expand `{a,b}` into its alternatives, nested ones included. A brace without a comma, or that
99
+ * never closes, is a literal brace.
100
+ */
101
+ export function expandBraces(pattern: string): string[] {
102
+ const expanded: string[] = []
103
+ expandInto(pattern, expanded)
104
+ return expanded
105
+ }
106
+
107
+ function expandInto(pattern: string, into: string[]): void {
108
+ for (let index = 0; index < pattern.length; index += 1) {
109
+ const char = pattern[index]
110
+
111
+ if (char === "\\") {
112
+ index += 1
113
+ continue
114
+ }
115
+ if (char !== "{") continue
116
+
117
+ const group = braceGroup(pattern, index)
118
+ if (!group) continue
119
+
120
+ const prefix = pattern.slice(0, index)
121
+ const suffix = pattern.slice(group.end + 1)
122
+ for (const option of group.options) expandInto(`${prefix}${option}${suffix}`, into)
123
+ return
124
+ }
125
+
126
+ if (into.length >= MAX_ALTERNATIVES) {
127
+ throw new PatternError(`it names more than ${MAX_ALTERNATIVES} alternatives with {…}`)
128
+ }
129
+ into.push(pattern)
130
+ }
131
+
132
+ /** The `{…}` opening at `start`: its options split on top-level commas, and its closing index. */
133
+ function braceGroup(
134
+ pattern: string,
135
+ start: number
136
+ ): { readonly options: string[]; readonly end: number } | undefined {
137
+ const options: string[] = []
138
+ let depth = 0
139
+ let optionStart = start + 1
140
+
141
+ for (let index = start + 1; index < pattern.length; index += 1) {
142
+ const char = pattern[index]
143
+
144
+ if (char === "\\") {
145
+ index += 1
146
+ } else if (char === "{") {
147
+ depth += 1
148
+ } else if (char === "}" && depth > 0) {
149
+ depth -= 1
150
+ } else if (char === "," && depth === 0) {
151
+ options.push(pattern.slice(optionStart, index))
152
+ optionStart = index + 1
153
+ } else if (char === "}") {
154
+ if (options.length === 0) return undefined
155
+ options.push(pattern.slice(optionStart, index))
156
+ return { options, end: index }
157
+ }
158
+ }
159
+
160
+ return undefined
161
+ }
162
+
163
+ /** Compile a regular expression source, or say why the pattern is not one. */
164
+ export function compilePattern(source: string, flags = ""): RegExp {
165
+ try {
166
+ return new RegExp(`^${source}$`, flags)
167
+ } catch (error) {
168
+ throw new PatternError(error instanceof Error ? error.message : String(error))
169
+ }
170
+ }
171
+
172
+ function escapeRegExp(text: string): string {
173
+ return text.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")
174
+ }
175
+
176
+ function escapeClassChar(char: string): string {
177
+ return /[\\\]^[-]/.test(char) ? `\\${char}` : char
178
+ }
@@ -0,0 +1,237 @@
1
+ import { Buffer } from "node:buffer"
2
+ import { type FileHandle, open } from "node:fs/promises"
3
+ import { SEARCH_LIMITS } from "./request"
4
+
5
+ export type FileText =
6
+ | { readonly text: string }
7
+ | { readonly skipped: "binary" | "too_large" | "unreadable" }
8
+
9
+ /** A read that knows what it will hold before it holds it. */
10
+ export interface PendingRead<R> {
11
+ /** The bytes the read holds once started, until its result is taken. */
12
+ readonly bytes: number
13
+ /** Never rejects. */
14
+ read(): Promise<R>
15
+ /** Lets go of a read never started. Never rejects. */
16
+ close(): Promise<void>
17
+ }
18
+
19
+ /**
20
+ * A file opened to be read: one open, and its size from that descriptor. Its text comes from
21
+ * {@link PendingRead.read}, unless it is too large or unreadable, which is known already.
22
+ */
23
+ export async function openText(path: string, strict: boolean): Promise<PendingRead<FileText>> {
24
+ let file: FileHandle | undefined
25
+
26
+ try {
27
+ file = await open(path, "r")
28
+ const { size } = await file.stat()
29
+
30
+ if (size > SEARCH_LIMITS.fileBytes) {
31
+ await closeQuietly(file)
32
+ return settled({ skipped: "too_large" })
33
+ }
34
+
35
+ const opened = file
36
+ return {
37
+ bytes: size,
38
+ read: () => readText(opened, size, strict),
39
+ close: () => closeQuietly(opened),
40
+ }
41
+ } catch {
42
+ if (file) await closeQuietly(file)
43
+ return settled({ skipped: "unreadable" })
44
+ }
45
+ }
46
+
47
+ function settled(text: FileText): PendingRead<FileText> {
48
+ return { bytes: 0, read: async () => text, close: async () => {} }
49
+ }
50
+
51
+ /**
52
+ * The text of a file opened at `size` bytes, read through its descriptor and closed. A file
53
+ * over {@link READ_AT_ONCE_BYTES} is read in two: its first {@link SEARCH_LIMITS.binaryProbeBytes},
54
+ * then the rest unless they show it is binary, so a large binary file is never read whole. A file
55
+ * found by the walk is read as UTF-8 leniently, so a mostly ASCII file in another encoding still
56
+ * matches; a file named as the path must be UTF-8 text. A leading byte order mark is dropped, so
57
+ * `^` finds the first line. Never rejects.
58
+ */
59
+ async function readText(file: FileHandle, size: number, strict: boolean): Promise<FileText> {
60
+ try {
61
+ const first = size <= READ_AT_ONCE_BYTES ? size : PROBE_BYTES
62
+ let bytes = await readBytes(file, Buffer.allocUnsafe(first), 0)
63
+ if (bytes.subarray(0, PROBE_BYTES).includes(0)) return { skipped: "binary" }
64
+
65
+ if (bytes.length === first && first < size) {
66
+ const whole = Buffer.allocUnsafe(size)
67
+ bytes.copy(whole)
68
+ bytes = await readBytes(file, whole, first)
69
+ }
70
+
71
+ let text: string
72
+ if (strict) {
73
+ try {
74
+ text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes)
75
+ } catch {
76
+ return { skipped: "binary" }
77
+ }
78
+ } else {
79
+ text = bytes.toString("utf8")
80
+ }
81
+
82
+ return { text: text.startsWith("\uFEFF") ? text.slice(1) : text }
83
+ } catch {
84
+ return { skipped: "unreadable" }
85
+ } finally {
86
+ await closeQuietly(file)
87
+ }
88
+ }
89
+
90
+ const PROBE_BYTES = SEARCH_LIMITS.binaryProbeBytes
91
+
92
+ /** Up to this size, a file is read in one call: most source files, and two calls cost more. */
93
+ const READ_AT_ONCE_BYTES = 64 * 1024
94
+
95
+ /**
96
+ * Fills `bytes` from `from` on, and returns what was read: the file as it was when opened, if it
97
+ * grows meanwhile, or less if it shrank.
98
+ */
99
+ async function readBytes(file: FileHandle, bytes: Buffer, from: number): Promise<Buffer> {
100
+ let filled = from
101
+
102
+ while (filled < bytes.length) {
103
+ const { bytesRead } = await file.read(bytes, filled, bytes.length - filled, filled)
104
+ if (bytesRead === 0) break
105
+ filled += bytesRead
106
+ }
107
+
108
+ return bytes.subarray(0, filled)
109
+ }
110
+
111
+ async function closeQuietly(file: FileHandle): Promise<void> {
112
+ await file.close().catch(() => {})
113
+ }
114
+
115
+ /**
116
+ * How far reads run ahead of the file searched. Reading is most of a search's time, and mostly
117
+ * waiting: reads in flight overlap that wait. At most {@link READ_AHEAD} files are open, and a read
118
+ * starts only while the reads started hold at most {@link READ_AHEAD_BYTES} of file with it, but
119
+ * for the next file in line, which always starts: one file of 10 MiB past the bound at most. A
120
+ * binary file's bytes are let go once its first bytes show it is one.
121
+ *
122
+ * Memory: the texts read ahead take up to twice their bytes once decoded (a string with one
123
+ * character beyond Latin-1 takes two bytes for each): 52 MiB at worst, plus the file searched and
124
+ * the copies its search makes. 16 MiB reads a source tree as fast as 32 did, and holds half as
125
+ * much over a folder of large files.
126
+ */
127
+ export const READ_AHEAD = 32
128
+ export const READ_AHEAD_BYTES = 16 * 1024 * 1024
129
+
130
+ interface Slot<T, R> {
131
+ readonly item: T
132
+ /** Its {@link PendingRead}, once opened. */
133
+ pending?: PendingRead<R>
134
+ /** Settles once {@link pending} is known. */
135
+ opening?: Promise<void>
136
+ /** The bytes it holds against {@link READ_AHEAD_BYTES}. */
137
+ holds: number
138
+ result?: Promise<R>
139
+ }
140
+
141
+ /**
142
+ * Files read ahead of the one searched, handed back in the order they come, so a search finds the
143
+ * same lines, in the same order, as reading one file at a time. Files are opened as they come,
144
+ * and read as soon as they are open and their bytes fit. A search that stops reads no further than
145
+ * {@link READ_AHEAD} files past where it stopped.
146
+ */
147
+ export class ReadAhead<T, R> {
148
+ readonly #slots: Slot<T, R>[] = []
149
+ #held = 0
150
+ #exhausted = false
151
+ #stopped = false
152
+
153
+ constructor(
154
+ private readonly items: Iterator<T>,
155
+ /** Never rejects. */
156
+ private readonly open: (item: T) => Promise<PendingRead<R>>,
157
+ /** What a read that holds nothing once done looks like, a binary file. */
158
+ private readonly holdsNothing: (result: R) => boolean = () => false
159
+ ) {}
160
+
161
+ async next(): Promise<{ readonly item: T; readonly read: R } | undefined> {
162
+ this.#fill()
163
+ const slot = this.#slots[0]
164
+ if (!slot) return undefined
165
+
166
+ await slot.opening
167
+ this.#start()
168
+ const read = await (slot.result as Promise<R>)
169
+
170
+ this.#slots.shift()
171
+ this.#held -= slot.holds
172
+ this.#fill()
173
+ this.#start()
174
+
175
+ return { item: slot.item, read }
176
+ }
177
+
178
+ /**
179
+ * Starts no other read, waits for those in flight and closes the files never read: no file
180
+ * stays open past the search.
181
+ */
182
+ async stop(): Promise<void> {
183
+ this.#exhausted = true
184
+ this.#stopped = true
185
+
186
+ await Promise.all(
187
+ this.#slots.splice(0).map(async (slot) => {
188
+ await slot.opening
189
+ if (slot.result) await slot.result
190
+ else await slot.pending?.close()
191
+ })
192
+ )
193
+ }
194
+
195
+ #fill(): void {
196
+ while (!this.#exhausted && this.#slots.length < READ_AHEAD) {
197
+ const step = this.items.next()
198
+ if (step.done) {
199
+ this.#exhausted = true
200
+ return
201
+ }
202
+
203
+ const slot: Slot<T, R> = { item: step.value, holds: 0 }
204
+ this.#slots.push(slot)
205
+ slot.opening = this.open(step.value).then((pending) => {
206
+ slot.pending = pending
207
+ this.#start()
208
+ })
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Starts the reads of the files opened, as long as their bytes fit. The first file in line always
214
+ * starts: the search waits on it, and no read behind it can make room.
215
+ */
216
+ #start(): void {
217
+ if (this.#stopped) return
218
+
219
+ for (let index = 0; index < this.#slots.length; index += 1) {
220
+ const slot = this.#slots[index] as Slot<T, R>
221
+ const pending = slot.pending
222
+ if (slot.result || !pending) continue
223
+ if (index > 0 && this.#held + pending.bytes > READ_AHEAD_BYTES) continue
224
+
225
+ slot.holds = pending.bytes
226
+ this.#held += slot.holds
227
+ slot.result = pending.read().then((result) => {
228
+ if (this.holdsNothing(result) && this.#slots.includes(slot)) {
229
+ this.#held -= slot.holds
230
+ slot.holds = 0
231
+ this.#start()
232
+ }
233
+ return result
234
+ })
235
+ }
236
+ }
237
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * What `search.mjs` takes and prints: the contract between the `glob` and `grep` tools and the
3
+ * engine the run installs.
4
+ */
5
+
6
+ /** How much a search returns, and which files `grep` reads. */
7
+ export const SEARCH_LIMITS = {
8
+ files: 100,
9
+ lines: 100,
10
+ linesPerFile: 20,
11
+ lineCharacters: 500,
12
+ /** How much of a cut line shows before its first match. */
13
+ matchLead: 40,
14
+ /**
15
+ * How much of a line `grep` matches. A backtracking regex can take seconds on one long line of a
16
+ * minified file; past this, a match is missed rather than the search stalled.
17
+ */
18
+ matchCharacters: 4_000,
19
+ /** What `grep`'s matches may add to its model text, in UTF-8 bytes, before it is truncated. */
20
+ textBytes: 24 * 1024,
21
+ fileBytes: 10 * 1024 * 1024,
22
+ /** A NUL byte in this many first bytes makes a file binary. */
23
+ binaryProbeBytes: 8 * 1024,
24
+ } as const
25
+
26
+ interface SearchScopeRequest {
27
+ /** Relative to the working directory, or absolute. Default: the working directory. */
28
+ readonly path?: string
29
+ /**
30
+ * The run's context directory: the second root a search may start from. A search that starts
31
+ * outside it never enters it.
32
+ */
33
+ readonly contextDirectory?: string
34
+ /** How long the walk may take before it stops with what it found. */
35
+ readonly timeoutMs: number
36
+ }
37
+
38
+ export interface GlobSearchRequest extends SearchScopeRequest {
39
+ readonly tool: "glob"
40
+ readonly pattern: string
41
+ }
42
+
43
+ export interface GrepSearchRequest extends SearchScopeRequest {
44
+ readonly tool: "grep"
45
+ /** A JavaScript regular expression, or the text itself with `literal`. */
46
+ readonly pattern: string
47
+ /** A `glob` pattern the files searched must match. */
48
+ readonly include?: string
49
+ readonly literal?: boolean
50
+ }
51
+
52
+ export type SearchRequest = GlobSearchRequest | GrepSearchRequest
53
+
54
+ /**
55
+ * Paths are relative to the working directory when they are under it, absolute otherwise.
56
+ * `incomplete` says the walk stopped at its deadline.
57
+ */
58
+ export interface GlobSearchResult {
59
+ readonly files: string[]
60
+ readonly truncated: boolean
61
+ readonly incomplete?: true
62
+ }
63
+
64
+ export interface GrepMatch {
65
+ readonly path: string
66
+ /** One-based. */
67
+ readonly line: number
68
+ readonly text: string
69
+ }
70
+
71
+ export interface GrepSearchResult {
72
+ readonly matches: GrepMatch[]
73
+ /** How many files the matches are in. */
74
+ readonly files: number
75
+ /** Matching lines counted past what a file shows, by path. */
76
+ readonly more?: Record<string, number>
77
+ /** Another file matches past the cap. */
78
+ readonly truncated: boolean
79
+ readonly incomplete?: true
80
+ }
81
+
82
+ export type SearchErrorCode =
83
+ | "path_missing"
84
+ | "path_outside"
85
+ | "path_is_file"
86
+ | "path_not_searchable"
87
+ | "invalid_pattern"
88
+ | "invalid_include"
89
+ | "file_binary"
90
+ | "file_too_large"
91
+
92
+ /** A search that cannot run as asked: the caller says why in its own words. */
93
+ export class SearchError extends Error {
94
+ constructor(
95
+ readonly code: SearchErrorCode,
96
+ message: string
97
+ ) {
98
+ super(message)
99
+ this.name = "SearchError"
100
+ }
101
+ }