Skip to content

Commit c95f234

Browse files
sahrizviclaude
andcommitted
fix(core): flat module shape, submatch bound, and text-arm offset validation
Fifth review round — Kilo and cubic on the extracted module. - The new module used `export namespace`, which packages/opencode/AGENTS.md explicitly prohibits, and its header cited that rule INVERTED: it claimed AGENTS.md forbids `export * as`, when `export * as` is the prescribed pattern and `export namespace` is what is forbidden (not standard ESM, blocks tree-shaking, breaks Node's native TS runner). Flattened to top-level exports with a self-reexport at the bottom; importers are unchanged. Header corrected. - Bound the submatch count on the legacy path, matching the core parser. Each submatch costs a rebase per endpoint and a rebase allocates a string up to the line length, so an unbounded array turned one in-ceiling record into O(count x line) work on the shipped `/find` route. - Validate `{text}`-arm offsets in both parsers. Nothing is rebased there, but the offset must still be addressable in the line it indexes: core's `NonNegativeInt` rejects negatives and fractions yet not values past the end, and legacy's `z.number()` rejects none of them. Raised by two reviewers independently; my earlier push-back was too narrow — I argued no claim is made on that arm, but returning a coordinate pair that indexes nothing is a claim. - Dropped a stale doc comment describing the previous, superseded layout. Tests: 23 core, 12 legacy. Marker balance verified across all five files. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent e21dc19 commit c95f234

4 files changed

Lines changed: 257 additions & 201 deletions

File tree

‎packages/core/src/ripgrep.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,17 @@ const normalizeMatch = (json: object): unknown => {
148148
// are still correct and useful, and this whole change exists to stop losing matches. Offsets on
149149
// the `{text}` arm are untouched — no rebasing happens there, so no claim is made.
150150
const isContinuationByte = (byte: number | undefined) => byte !== undefined && (byte & 0xc0) === 0x80
151+
const lineBytes = Buffer.byteLength(lines.text, "utf8")
151152
const rebase = (offset: unknown): number | undefined => {
152-
if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0 || offset > raw!.length) return undefined
153-
if (offset !== 0 && offset !== raw!.length && isContinuationByte(raw![offset])) return undefined
154-
return Buffer.byteLength(raw!.subarray(0, offset).toString("utf8"), "utf8")
153+
// `{text}` arm: nothing is rebased, but the offset must still be addressable in the line it
154+
// indexes, or the record carries a coordinate pair that points at nothing.
155+
if (!raw)
156+
return typeof offset === "number" && Number.isInteger(offset) && offset >= 0 && offset <= lineBytes
157+
? offset
158+
: undefined
159+
if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0 || offset > raw.length) return undefined
160+
if (offset !== 0 && offset !== raw.length && isContinuationByte(raw[offset])) return undefined
161+
return Buffer.byteLength(raw.subarray(0, offset).toString("utf8"), "utf8")
155162
}
156163
const submatches = readProp(data, "submatches")
157164
const normalized = {
@@ -171,7 +178,6 @@ const normalizeMatch = (json: object): unknown => {
171178
// matched text needs the same bound as the line or the retained-memory cap is defeated
172179
// by the submatches instead.
173180
const decoded = { ...submatch, match: { text: capLineText(match.text) } }
174-
if (!raw) return [decoded]
175181
const start = rebase(readProp(submatch, "start"))
176182
const end = rebase(readProp(submatch, "end"))
177183
// Endpoints are rebased independently, so ordering is checked explicitly: an inverted

‎packages/core/test/ripgrep.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ describe("Ripgrep", () => {
348348
expect(matches[0].submatches).toEqual([])
349349
})
350350

351+
// The `{text}` arm is not rebased, but an offset still has to be addressable in the line.
352+
stubTest("drops a text-arm submatch whose offset is past the end of the line", async () => {
353+
const matches = await Effect.runPromise(
354+
grepWithStubbedRecords([
355+
matchRecord("a.txt", {
356+
lines: { text: "needle\n" },
357+
submatches: [{ match: { text: "needle" }, start: 0, end: 999 }],
358+
}),
359+
]),
360+
)
361+
362+
expect(matches[0].submatches).toEqual([])
363+
})
364+
351365
// Endpoints are rebased independently, so an inverted range can survive both endpoint checks.
352366
stubTest("drops a submatch whose range is inverted", async () => {
353367
const matches = await Effect.runPromise(

0 commit comments

Comments
 (0)