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
1 change: 1 addition & 0 deletions src/agents/workers/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type RenameStats = {

const SKIP_SEGMENTS = new Set([
".git",
".claude",
"build",
".gradle",
".idea",
Expand Down
2 changes: 1 addition & 1 deletion src/agents/workers/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RenameStats = {
files_renamed: number;
};

const SKIP_RELATIVE_PATHS = ["/.git", "/DerivedData", "/.build", "/Pods", "/Carthage"];
const SKIP_RELATIVE_PATHS = ["/.git", "/.claude", "/DerivedData", "/.build", "/Pods", "/Carthage"];

export async function runIosWorker(domain: DomainSpec): Promise<WorkerResult> {
if (isStub("ios")) {
Expand Down
2 changes: 1 addition & 1 deletion src/agents/workers/rails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RenameStats = {
files_renamed: number;
};

const SKIP_RELATIVE_PATHS = ["/.git", "/node_modules", "/tmp", "/log", "/vendor/bundle"];
const SKIP_RELATIVE_PATHS = ["/.git", "/.claude", "/node_modules", "/tmp", "/log", "/vendor/bundle"];

export async function runRailsWorker(domain: DomainSpec): Promise<WorkerResult> {
if (isStub("rails")) {
Expand Down
104 changes: 102 additions & 2 deletions src/validation/layer1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { readdir, readFile, stat } from "node:fs/promises";
import { extname, join, relative, basename } from "node:path";

export type Layer1Input = {
projectDir: string;
forbiddenTokens: readonly string[];
Expand All @@ -15,7 +18,104 @@ export type Layer1Result = {
findings: Layer1Finding[];
};

const TEXT_EXTS = new Set([
".rb", ".erb", ".yml", ".yaml", ".json", ".md", ".gemspec", ".rake", ".ru",
".txt", ".sample", ".example", ".conf", ".html", ".css", ".scss", ".js", ".mjs",
".tt", ".lock",
".swift", ".plist", ".strings", ".xcconfig", ".entitlements", ".pbxproj",
".xcworkspacedata", ".modulemap",
".kt", ".kts", ".xml", ".gradle", ".pro", ".toml", ".properties", ".cfg",
]);

const TEXT_BASENAMES = new Set([
"Gemfile", "Gemfile.lock", "Rakefile", "Procfile", "Procfile.dev",
"config.ru", "Dockerfile",
"Podfile", "Podfile.lock", "Package.swift", "Cartfile", "Makefile",
"gradlew", "gradlew.bat", "gradle.properties", "local.properties",
]);

const SKIP_SEGMENTS = new Set([
".git", "node_modules", "tmp", "log", "vendor",
"DerivedData", "Pods", "Carthage", "xcuserdata", ".build",
"build", ".gradle", ".idea", ".kotlin", "captures",
]);

export async function runLayer1(input: Layer1Input): Promise<Layer1Result> {
void input;
throw new Error("runLayer1 not implemented: shell out to rg --json, collect matches, pass=true iff findings is empty");
if (input.forbiddenTokens.length === 0) {
return { pass: true, findings: [] };
}

const root = await realRoot(input.projectDir);
if (!root) {
return { pass: false, findings: [] };
}

const regex = buildRegex(input.forbiddenTokens);
const findings: Layer1Finding[] = [];

for await (const filePath of walk(root)) {
const content = await safeRead(filePath);
if (!content) continue;
const lines = content.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i]!;
regex.lastIndex = 0;
let m: RegExpExecArray | null;
while ((m = regex.exec(line)) !== null) {
findings.push({
token: m[0],
file: relative(root, filePath),
line: i + 1,
text: line,
});
}
}
}

return { pass: findings.length === 0, findings };
}

async function realRoot(dir: string): Promise<string | null> {
try {
const s = await stat(dir);
return s.isDirectory() ? dir : null;
} catch {
return null;
}
}

function buildRegex(tokens: readonly string[]): RegExp {
const escaped = tokens.map(escapeRegex).join("|");
return new RegExp(`(?:${escaped})`, "g");
}

function escapeRegex(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

async function* walk(dir: string): AsyncGenerator<string> {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (SKIP_SEGMENTS.has(entry.name)) continue;
const full = join(dir, entry.name);
if (entry.isDirectory()) {
yield* walk(full);
} else if (entry.isFile() && isTextFile(full)) {
yield full;
}
}
}

function isTextFile(path: string): boolean {
const base = basename(path);
if (TEXT_BASENAMES.has(base)) return true;
return TEXT_EXTS.has(extname(path));
}

async function safeRead(path: string): Promise<string | null> {
try {
return await readFile(path, "utf8");
} catch {
return null;
}
}
9 changes: 4 additions & 5 deletions tests/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ test("validation layers are exported as functions", () => {
assert.equal(typeof runLayer3, "function");
});

test("runLayer1 rejects until implemented", async () => {
await assert.rejects(
runLayer1({ projectDir: "/tmp", forbiddenTokens: [] }),
/not implemented/i,
);
test("runLayer1 returns pass when forbiddenTokens is empty", async () => {
const result = await runLayer1({ projectDir: "/tmp", forbiddenTokens: [] });
assert.equal(result.pass, true);
assert.deepEqual(result.findings, []);
});

test("runLayer2 returns a failed result for a non-Rails directory", async () => {
Expand Down
Loading