File size: 3,657 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { execFileSync } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import { listRuntimeSourceFiles } from "./repo-scan.js";

export type RuntimeSourceGuardrailFile = {
  relativePath: string;
  source: string;
};

const DEFAULT_GUARDRAIL_SKIP_PATTERNS = [
  /\.test\.tsx?$/,
  /\.test-helpers\.tsx?$/,
  /\.test-utils\.tsx?$/,
  /\.test-harness\.tsx?$/,
  /\.suite\.tsx?$/,
  /\.e2e\.tsx?$/,
  /\.d\.ts$/,
  /[\\/](?:__tests__|tests|test-utils)[\\/]/,
  /[\\/][^\\/]*test-helpers(?:\.[^\\/]+)?\.ts$/,
  /[\\/][^\\/]*test-utils(?:\.[^\\/]+)?\.ts$/,
  /[\\/][^\\/]*test-harness(?:\.[^\\/]+)?\.ts$/,
];

const runtimeSourceGuardrailCache = new Map<string, Promise<RuntimeSourceGuardrailFile[]>>();
const trackedRuntimeSourceListCache = new Map<string, string[]>();
const FILE_READ_CONCURRENCY = 24;

export function shouldSkipGuardrailRuntimeSource(relativePath: string): boolean {
  return DEFAULT_GUARDRAIL_SKIP_PATTERNS.some((pattern) => pattern.test(relativePath));
}

async function readRuntimeSourceFiles(
  repoRoot: string,
  absolutePaths: string[],
): Promise<RuntimeSourceGuardrailFile[]> {
  const output: Array<RuntimeSourceGuardrailFile | undefined> = Array.from({
    length: absolutePaths.length,
  });
  let nextIndex = 0;

  const worker = async () => {
    for (;;) {
      const index = nextIndex;
      nextIndex += 1;
      if (index >= absolutePaths.length) {
        return;
      }
      const absolutePath = absolutePaths[index];
      if (!absolutePath) {
        continue;
      }
      const source = await fs.readFile(absolutePath, "utf8");
      output[index] = {
        relativePath: path.relative(repoRoot, absolutePath),
        source,
      };
    }
  };

  const workers = Array.from(
    { length: Math.min(FILE_READ_CONCURRENCY, Math.max(1, absolutePaths.length)) },
    () => worker(),
  );
  await Promise.all(workers);
  return output.filter((entry): entry is RuntimeSourceGuardrailFile => entry !== undefined);
}

function tryListTrackedRuntimeSourceFiles(repoRoot: string): string[] | null {
  const cached = trackedRuntimeSourceListCache.get(repoRoot);
  if (cached) {
    return cached.slice();
  }

  try {
    const stdout = execFileSync("git", ["-C", repoRoot, "ls-files", "--", "src", "extensions"], {
      encoding: "utf8",
      stdio: ["ignore", "pipe", "ignore"],
    });
    const files = stdout
      .split(/\r?\n/u)
      .filter(Boolean)
      .filter((relativePath) => relativePath.endsWith(".ts") || relativePath.endsWith(".tsx"))
      .filter((relativePath) => !shouldSkipGuardrailRuntimeSource(relativePath))
      .map((relativePath) => path.join(repoRoot, relativePath));
    trackedRuntimeSourceListCache.set(repoRoot, files);
    return files.slice();
  } catch {
    return null;
  }
}

export async function loadRuntimeSourceFilesForGuardrails(
  repoRoot: string,
): Promise<RuntimeSourceGuardrailFile[]> {
  let pending = runtimeSourceGuardrailCache.get(repoRoot);
  if (!pending) {
    pending = (async () => {
      const trackedFiles = tryListTrackedRuntimeSourceFiles(repoRoot);
      const sourceFiles =
        trackedFiles ??
        (
          await listRuntimeSourceFiles(repoRoot, {
            roots: ["src", "extensions"],
            extensions: [".ts", ".tsx"],
          })
        ).filter((absolutePath) => {
          const relativePath = path.relative(repoRoot, absolutePath);
          return !shouldSkipGuardrailRuntimeSource(relativePath);
        });
      return await readRuntimeSourceFiles(repoRoot, sourceFiles);
    })();
    runtimeSourceGuardrailCache.set(repoRoot, pending);
  }
  return await pending;
}