File size: 9,999 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { existsSync, readFileSync, statSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { dirname, join, normalize, relative, resolve } from "node:path";
import { globSync } from "node:fs";
import { errorLine, outputLine } from "./io.mjs";

const root = process.cwd();
const rules = [
	{
		label: "attachment modules must stay provider-neutral",
		files: "src/attachments/**/*.{ts,tsx}",
		disallowed: ["../"],
		allowed: ["../shared"],
	},
	{
		label: "shared modules must stay leaf-level",
		files: "src/shared/**/*.ts",
		disallowed: [
			"../gemini/",
			"../http/",
			"../promptcompat/",
			"../toolcall/",
			"../toolstream/",
			"./gemini/",
			"./http/",
			"./promptcompat/",
			"./toolcall/",
			"./toolstream/",
		],
	},
	{
		label: "gemini client must not depend on prompt compatibility",
		files: "src/gemini/client/**/*.ts",
		disallowed: ["../../promptcompat/", "../promptcompat/"],
	},
	{
		label: "prompt compatibility must not depend on HTTP adapters",
		files: "src/promptcompat/**/*.ts",
		disallowed: ["../http/", "../../http/"],
	},
	{
		label: "completion modules must not depend on HTTP adapters",
		files: "src/completion/**/*.ts",
		disallowed: ["../http/", "../../http/"],
	},
	{
		label:
			"completion modules must depend on provider ports instead of Gemini implementation packages",
		files: "src/completion/**/*.ts",
		disallowed: ["../gemini/", "../../gemini/"],
	},
	{
		label:
			"completion modules must import prompt compatibility owner modules instead of the compatibility barrel",
		files: "src/completion/**/*.ts",
		disallowed: [
			"../promptcompat",
			"../promptcompat/",
			"../../promptcompat",
			"../../promptcompat/",
		],
		allowed: [
			"../promptcompat/file-refs",
			"../promptcompat/google",
			"../promptcompat/history",
			"../promptcompat/messages",
			"../promptcompat/prompt-build",
			"../promptcompat/responses-input",
			"../../promptcompat/file-refs",
			"../../promptcompat/google",
			"../../promptcompat/history",
			"../../promptcompat/messages",
			"../../promptcompat/prompt-build",
			"../../promptcompat/responses-input",
		],
	},
	{
		label: "HTTP adapters must not call Gemini client directly",
		files: "src/http/**/*.ts",
		disallowed: [
			"../gemini/client",
			"../gemini/client/",
			"../../gemini/client",
			"../../gemini/client/",
		],
	},
	{
		label:
			"Gemini implementation must only depend on completion ports through its provider adapter",
		files: "src/gemini/**/*.ts",
		disallowed: [
			"../completion",
			"../completion/",
			"../../completion",
			"../../completion/",
		],
		allowed: ["../completion/ports", "../../completion/ports"],
	},
	{
		label:
			"HTTP protocol adapters must import core helpers directly instead of the HTTP barrel",
		files: "src/http/{openai,google}/**/*.ts",
		disallowed: ["../index", "../index.ts", "../../http", "../../http/"],
	},
	{
		label: "HTTP adapters must not consume tool stream state directly",
		files: "src/http/**/*.ts",
		disallowed: [
			"../toolstream",
			"../toolstream/",
			"../../toolstream",
			"../../toolstream/",
		],
	},
	{
		label: "HTTP adapter barrels must not re-export lower-layer internals",
		files: "src/http/**/index.ts",
		disallowed: [
			"../../completion",
			"../../completion/",
			"../../promptcompat",
			"../../promptcompat/",
			"../../toolcall",
			"../../toolcall/",
			"../../gemini",
			"../../gemini/",
		],
	},
	{
		label: "prompt compatibility must not perform Gemini uploads",
		files: "src/promptcompat/**/*.ts",
		disallowed: [
			"../gemini/uploads",
			"../gemini/uploads/",
			"../../gemini/uploads",
			"../../gemini/uploads/",
		],
	},
	{
		label:
			"prompt compatibility internals must not depend on completion modules",
		files: "src/promptcompat/**/*.ts",
		disallowed: [
			"../completion",
			"../completion/",
			"../../completion",
			"../../completion/",
		],
	},
	{
		label: "google HTTP adapter must not depend on openai HTTP adapter",
		files: "src/http/google/**/*.ts",
		disallowed: ["../openai/", "../../http/openai/"],
	},
	{
		label: "openai HTTP adapter must not depend on google HTTP adapter",
		files: "src/http/openai/**/*.ts",
		disallowed: ["../google/", "../../http/google/"],
	},
	{
		label: "toolcall must not depend on prompt compatibility",
		files: "src/toolcall/**/*.ts",
		disallowed: ["../promptcompat/", "../../promptcompat/"],
	},
	{
		label: "toolcall must not depend on HTTP adapters or stream state",
		files: "src/toolcall/**/*.ts",
		disallowed: [
			"../http/",
			"../../http/",
			"../toolstream/",
			"../../toolstream/",
		],
	},
	{
		label: "toolcall must not depend on Gemini upload modules",
		files: "src/toolcall/**/*.ts",
		disallowed: [
			"../gemini/uploads",
			"../gemini/uploads/",
			"../../gemini/uploads",
			"../../gemini/uploads/",
		],
	},
	{
		label:
			"implementation modules must import toolcall owner modules instead of the compatibility barrel",
		files: "src/{completion,promptcompat,toolstream,http}/**/*.ts",
		disallowedExact: [
			"../toolcall",
			"../toolcall/index",
			"../toolcall/index.ts",
			"../../toolcall",
			"../../toolcall/index",
			"../../toolcall/index.ts",
		],
	},
	{
		label:
			"implementation modules must import attachment media owner modules instead of the shared media compatibility shim",
		files:
			"src/{completion,promptcompat,toolcall,toolstream,http,gemini}/**/*.ts",
		disallowed: ["../shared/media", "../../shared/media"],
	},
];

const importPattern =
	/(?:import|export)\s+(?:[^"']*?\s+from\s+)?["']([^"']+)["']/g;
const violations = [];

for (const rule of rules) {
	for (const file of globSync(rule.files, { cwd: root })) {
		if (rule.exceptFiles?.includes(normalizeSourcePath(file))) continue;
		const text = await readFile(resolve(root, file), "utf8");
		for (const match of text.matchAll(importPattern)) {
			const specifier = match[1];
			if (
				rule.allowed?.some(
					(prefix) =>
						specifier === prefix || specifier.startsWith(`${prefix}/`),
				)
			)
				continue;
			if (rule.disallowedExact?.includes(specifier)) {
				violations.push(
					`${normalizeSourcePath(relative(root, resolve(root, file)))}: ${specifier} (${rule.label})`,
				);
				continue;
			}
			if (
				rule.disallowed?.some(
					(prefix) => specifier === prefix || specifier.startsWith(prefix),
				)
			) {
				violations.push(
					`${normalizeSourcePath(relative(root, resolve(root, file)))}: ${specifier} (${rule.label})`,
				);
			}
		}
	}
}

const sourceFiles = globSync("src/**/*.{ts,tsx}", { cwd: root }).map(
	normalizeSourcePath,
);
const sourceFileSet = new Set(sourceFiles);
const importGraph = new Map();

for (const file of sourceFiles) {
	const text = await readFile(resolve(root, file), "utf8");
	const deps = [];
	for (const match of text.matchAll(importPattern)) {
		const specifier = match[1];
		if (!specifier.startsWith(".")) continue;
		const dep = resolveSourceImport(file, specifier);
		if (dep && sourceFileSet.has(dep)) deps.push(dep);
	}
	importGraph.set(file, deps);
}

for (const cycle of findImportCycles(importGraph)) {
	violations.push(
		`${cycle.join(" -> ")} (source modules must not form import cycles)`,
	);
}

const packageGraph = buildPackageGraph(sourceFiles, sourceFileSet);
for (const cycle of findImportCycles(packageGraph)) {
	violations.push(
		`${cycle.join(" -> ")} (source directories must not form dependency cycles)`,
	);
}

if (violations.length) {
	errorLine("Architecture check failed:");
	for (const violation of violations) errorLine(`- ${violation}`);
	process.exit(1);
}

outputLine("Architecture check passed");

function resolveSourceImport(fromFile, specifier) {
	const base = normalizeSourcePath(join(dirname(fromFile), specifier));
	const candidates = [
		base,
		`${base}.ts`,
		`${base}.tsx`,
		normalizeSourcePath(join(base, "index.ts")),
		normalizeSourcePath(join(base, "index.tsx")),
	];
	for (const candidate of candidates) {
		const absolute = resolve(root, candidate);
		if (existsSync(absolute) && statSync(absolute).isFile())
			return normalizeSourcePath(candidate);
	}
	return null;
}

function findImportCycles(graph) {
	const cycles = [];
	const seen = new Set();
	const active = new Set();
	const stack = [];
	const cycleKeys = new Set();

	function visit(node) {
		seen.add(node);
		active.add(node);
		stack.push(node);
		for (const dep of graph.get(node) || []) {
			if (!graph.has(dep)) continue;
			if (!seen.has(dep)) {
				visit(dep);
				continue;
			}
			if (!active.has(dep)) continue;
			const start = stack.indexOf(dep);
			const cycle = stack.slice(start).concat(dep);
			const key = cycle.slice(0, -1).sort().join("|");
			if (!cycleKeys.has(key)) {
				cycleKeys.add(key);
				cycles.push(cycle);
			}
		}
		stack.pop();
		active.delete(node);
	}

	for (const node of graph.keys()) {
		if (!seen.has(node)) visit(node);
	}
	return cycles;
}

function buildPackageGraph(files, fileSet) {
	const graph = new Map();
	const packageOwners = new Set(
		files.map(sourcePackage).filter((owner) => owner && owner !== "generated"),
	);
	for (const owner of packageOwners) graph.set(owner, []);

	for (const file of files) {
		const fromOwner = sourcePackage(file);
		if (!packageOwners.has(fromOwner)) continue;
		const text = readSourceFileSync(file);
		for (const match of text.matchAll(importPattern)) {
			const specifier = match[1];
			if (!specifier.startsWith(".")) continue;
			const dep = resolveSourceImport(file, specifier);
			if (!dep || !fileSet.has(dep)) continue;
			const toOwner = sourcePackage(dep);
			if (!packageOwners.has(toOwner) || toOwner === fromOwner) continue;
			const deps = graph.get(fromOwner);
			if (deps && !deps.includes(toOwner)) deps.push(toOwner);
		}
	}

	return graph;
}

function sourcePackage(file) {
	const match = /^src\/([^/]+)\//.exec(file);
	return match?.[1] ? match[1] : "";
}

function readSourceFileSync(file) {
	return readFileSync(resolve(root, file), "utf8");
}

function normalizeSourcePath(file) {
	return normalize(file).replaceAll("\\", "/");
}