Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "kensai",
"description": "Multi-agent code review pipeline with adversarial falsification, orchestrated by a stateful MCP server",
"source": "./claude/kensai",
"version": "0.1.5",
"version": "0.1.7",
"license": "MIT",
"keywords": [
"code-review",
Expand Down
2 changes: 1 addition & 1 deletion claude/kensai/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kensai",
"version": "0.1.5",
"version": "0.1.7",
"description": "Multi-agent code review pipeline with adversarial falsification, orchestrated by a stateful MCP server",
"author": {
"name": "xobotyi",
Expand Down
2 changes: 1 addition & 1 deletion claude/kensai/mcp/review/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gaijin/kensai-review-mcp",
"version": "0.1.5",
"version": "0.1.7",
"description": "Stateful MCP server for the kensai multi-agent code review pipeline",
"license": "MIT",
"author": {
Expand Down
71 changes: 49 additions & 22 deletions claude/kensai/mcp/review/src/repofs/pathindex/pathindex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dirname, join } from "node:path";

import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";

import { PathIndex, validateGlobs } from "./pathindex.ts";
import { PathIndex, countFileLines, validateGlobs } from "./pathindex.ts";
import type { IndexEntryDir, IndexEntryFile, IndexEntrySymlink } from "./pathindex.ts";

describe("PathIndex", () => {
Expand Down Expand Up @@ -321,32 +321,21 @@ describe("PathIndex.new", () => {
expect(ix.get("nonexistent")).toBeUndefined();
});

it("counts lines in text files", async () => {
it("index is stat-only — lineCount and maxLineLen are zero", async () => {
await writeFile(join(tempDir, "three.txt"), "one\ntwo\nthree\n");
await writeFile(join(tempDir, "no-trailing.txt"), "one\ntwo");
const ix = await PathIndex.new(tempDir);
const three = ix.get("three.txt") as IndexEntryFile;
const noTrailing = ix.get("no-trailing.txt") as IndexEntryFile;
expect(three.lineCount).toBe(3);
expect(three.isBinary).toBe(false);
expect(noTrailing.lineCount).toBe(2);
expect(noTrailing.isBinary).toBe(false);
});

it("tracks max line length", async () => {
await writeFile(join(tempDir, "varied.txt"), "short\na longer line here\nhi\n");
const ix = await PathIndex.new(tempDir);
const entry = ix.get("varied.txt") as IndexEntryFile;
expect(entry.maxLineLen).toBe(Buffer.byteLength("a longer line here"));
const entry = ix.get("three.txt") as IndexEntryFile;
expect(entry.lineCount).toBe(0);
expect(entry.maxLineLen).toBe(0);
expect(entry.isBinary).toBe(false);
});

it("detects binary files", async () => {
await writeFile(join(tempDir, "binary.bin"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0x0a, 0x1a]));
it("detects binary by extension", async () => {
await writeFile(join(tempDir, "image.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47]));
await writeFile(join(tempDir, "code.ts"), "const x = 1;\n");
const ix = await PathIndex.new(tempDir);
const entry = ix.get("binary.bin") as IndexEntryFile;
expect(entry.isBinary).toBe(true);
expect(entry.lineCount).toBe(0);
expect(entry.maxLineLen).toBe(0);
expect((ix.get("image.png") as IndexEntryFile).isBinary).toBe(true);
expect((ix.get("code.ts") as IndexEntryFile).isBinary).toBe(false);
});

it("handles empty files", async () => {
Expand Down Expand Up @@ -401,3 +390,41 @@ describe("PathIndex.new", () => {
expect(root.children.map((c) => c.name).sort()).toEqual(["README.md", "lib", "main.go", "src"]);
});
});

describe("countFileLines", () => {
let tempDir: string;

beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "countlines-"));
});

afterEach(async () => {
await rm(tempDir, { recursive: true, maxRetries: 3 });
});

it("counts lines in text file", async () => {
await writeFile(join(tempDir, "three.txt"), "one\ntwo\nthree\n");
const result = await countFileLines(join(tempDir, "three.txt"));
expect(result.lineCount).toBe(3);
expect(result.isBinary).toBe(false);
});

it("tracks max line length", async () => {
await writeFile(join(tempDir, "varied.txt"), "short\na longer line here\nhi\n");
const result = await countFileLines(join(tempDir, "varied.txt"));
expect(result.maxLineLen).toBe(Buffer.byteLength("a longer line here"));
});

it("detects binary content", async () => {
await writeFile(join(tempDir, "bin"), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0x0a]));
const result = await countFileLines(join(tempDir, "bin"));
expect(result.isBinary).toBe(true);
expect(result.lineCount).toBe(0);
});

it("handles file without trailing newline", async () => {
await writeFile(join(tempDir, "no-nl.txt"), "one\ntwo");
const result = await countFileLines(join(tempDir, "no-nl.txt"));
expect(result.lineCount).toBe(2);
});
});
119 changes: 34 additions & 85 deletions claude/kensai/mcp/review/src/repofs/pathindex/pathindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import picomatch from "picomatch";
import { ErrBinaryContent, LineIter } from "../lineiter/lineiter.ts";

/** Directory names skipped during filesystem walk. Never descended into. */
const WALK_EXCLUDE_DIRS: ReadonlySet<string> = new Set([".git"]);
export const WALK_EXCLUDE_DIRS: ReadonlySet<string> = new Set([".git"]);

/** Extensions treated as binary without opening the file. Stat-only — no line counting, no fd consumed. */
const BINARY_EXTENSIONS: ReadonlySet<string> = new Set([
Expand Down Expand Up @@ -152,7 +152,7 @@ export type IndexEntryFile = {
lineCount: number;
/** Byte length of the longest line (excluding `\n`). 0 for binary/empty files. */
maxLineLen: number;
/** True when the file's leading bytes contain a null byte. */
/** Extension-based hint. Content-based detection (null byte probe) runs at read/scan time. */
isBinary: boolean;
};

Expand Down Expand Up @@ -192,7 +192,6 @@ export class PathIndex {
readonly root: IndexEntryDir = { type: "dir", name: "", children: [] };
readonly paths: string[] = [];
readonly #entries = new Map<string, IndexEntry>();
readonly #fdPool = new Pool(256);

private constructor(rootPath: string) {
this.absRoot = nativePath.resolve(rootPath);
Expand Down Expand Up @@ -332,41 +331,21 @@ export class PathIndex {
continue;
}

const ext = path.extname(dirent.name).toLowerCase();

if (BINARY_EXTENSIONS.has(ext)) {
work.push(
fs.stat(absPath).then((s) => {
const entry: IndexEntryFile = {
type: "file",
name: dirent.name,
size: s.size,
lineCount: 0,
maxLineLen: 0,
isBinary: true,
};
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}),
);
} else {
work.push(
Promise.all([fs.stat(absPath), this.#countLines(absPath)]).then(([s, lc]) => {
const entry: IndexEntryFile = {
type: "file",
name: dirent.name,
size: s.size,
lineCount: lc.lineCount,
maxLineLen: lc.maxLineLen,
isBinary: lc.isBinary,
};
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}),
);
}
work.push(
fs.stat(absPath).then((s) => {
const entry: IndexEntryFile = {
type: "file",
name: dirent.name,
size: s.size,
lineCount: 0,
maxLineLen: 0,
isBinary: BINARY_EXTENSIONS.has(path.extname(dirent.name).toLowerCase()),
};
Comment thread
xobotyi marked this conversation as resolved.
dirEntry.children.push(entry);
this.paths.push(relPath);
this.#entries.set(relPath, entry);
}),
);
}

await Promise.all(work);
Expand Down Expand Up @@ -395,57 +374,27 @@ export class PathIndex {
return undefined;
}
}

/** Body-less line scan via {@link LineIter}. Binary files are detected by the null byte probe on the first chunk. */
async #countLines(absPath: string): Promise<{ lineCount: number; maxLineLen: number; isBinary: boolean }> {
await this.#fdPool.acquire();
const stream = createReadStream(absPath);
try {
const iter = await LineIter.new(stream, { lineCap: 0 });
let maxLineLen = 0;
while (await iter.next()) {
if (iter.len() > maxLineLen) maxLineLen = iter.len();
}
return { lineCount: iter.num(), maxLineLen, isBinary: false };
} catch (err) {
if (err instanceof ErrBinaryContent) {
return { lineCount: 0, maxLineLen: 0, isBinary: true };
}
throw err;
} finally {
stream.destroy();
this.#fdPool.release();
}
}
}

/** Bounds concurrent async operations. {@link acquire} blocks when the limit is reached. */
class Pool {
readonly #limit: number;
#active = 0;
#queue: (() => void)[] = [];

constructor(limit: number) {
this.#limit = limit;
}

/** Take a slot. Returns a promise that resolves when a slot is available. */
acquire(): Promise<void> | void {
if (this.#active < this.#limit) {
this.#active++;
return;
/** Count lines, detect binary, and measure max line length for a single file. */
export async function countFileLines(
absPath: string,
): Promise<{ lineCount: number; maxLineLen: number; isBinary: boolean }> {
const stream = createReadStream(absPath);
try {
const iter = await LineIter.new(stream, { lineCap: 0 });
let maxLineLen = 0;
while (await iter.next()) {
if (iter.len() > maxLineLen) maxLineLen = iter.len();
}
return new Promise<void>((resolve) => this.#queue.push(resolve));
}

/** Return a slot. Wakes the next waiter if any. */
release(): void {
const next = this.#queue.shift();
if (next) {
next();
} else {
this.#active--;
return { lineCount: iter.num(), maxLineLen, isBinary: false };
} catch (err) {
if (err instanceof ErrBinaryContent) {
return { lineCount: 0, maxLineLen: 0, isBinary: true };
}
throw err;
} finally {
stream.destroy();
}
}

Expand Down
25 changes: 16 additions & 9 deletions claude/kensai/mcp/review/src/repofs/repofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path";

import { Repo } from "./git/git.ts";
import type { FilterOptions } from "./pathindex/pathindex.ts";
import { PathIndex } from "./pathindex/pathindex.ts";
import { PathIndex, WALK_EXCLUDE_DIRS } from "./pathindex/pathindex.ts";
import type { GrepOptions, GrepResult } from "./rg/rg.ts";
import { grep } from "./rg/rg.ts";

Expand Down Expand Up @@ -44,7 +44,6 @@ export class RepoFs {
}

const [index, git] = await Promise.all([PathIndex.new(absRoot, signal), Repo.open(absRoot, signal)]);

return new RepoFs(absRoot, index, git);
}

Expand Down Expand Up @@ -81,22 +80,30 @@ export class RepoFs {
return this.#index.dir(this.resolve(p)) !== undefined;
}

/** Resolves path, validates file entry in index, returns raw bytes. */
/** Resolves path, validates via index or filesystem, returns raw bytes. Falls through to direct read for files not in the index. */
async readFile(p: string): Promise<Buffer> {
const rel = this.resolve(p);
const entry = this.#index.get(rel);

if (!entry) {
throw new RepoFsError(`file not found: ${rel}`);
if (entry && entry.type !== "file") {
throw new RepoFsError(`${rel}: not a regular file (${entry.type})`);
}

if (entry.type !== "file") {
throw new RepoFsError(`${rel}: not a regular file (${entry.type})`);
const absPath = path.resolve(this.#root, rel);

if (!entry) {
const firstSegment = rel.split("/")[0];
if (firstSegment && WALK_EXCLUDE_DIRS.has(firstSegment)) {
throw new RepoFsError(`file not found: ${rel}`);
}
const stat = await fsp.stat(absPath).catch(() => null);
if (!stat?.isFile()) {
throw new RepoFsError(`file not found: ${rel}`);
}
}
Comment thread
xobotyi marked this conversation as resolved.

this.#pendingReadPaths.push(rel);

return fsp.readFile(path.resolve(this.#root, rel));
return fsp.readFile(absPath);
}

/** Fuzzy search via {@link PathIndex.fuzzySearch}. */
Expand Down
Loading
Loading