@cuzfrog/pi-module-gates 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,6 +25,7 @@ The extension intercepts agent `write`/`edit` operations and enforces these cont
25
25
  - **Readonly gate** — is the target file locked?
26
26
  **Fronzen gate** — is there any surface change to the target file?
27
27
  - **Export gate** — would the change introduce an export not in the `visible` list?
28
+ - **Module interface import gate** — external files can only import from the module not internal files, i.e. re-exports from `index.ts` or `mod.rs`. A child module may import from a parent module's internal files (not recommended but allowed). (Only Typescript/JavaScript and Rust are supported)
28
29
  - **Import gate** (not implemented yet) — would the change introduce an import violating visibility scope?
29
30
 
30
31
  - System prompt: [system-prompt.md](src/context/system-prompt.ts)
@@ -125,6 +126,7 @@ Add a `module-gates` entry to `.pi/settings.json`:
125
126
  | `moduleDescriptorFileName` | `MODULE.md` | File name used for module descriptors (case-insensitive) |
126
127
  | `moduleDescriptorReadonly` | `true` | When `true`, descriptor files are readonly.|
127
128
  | `sourceRoot` | `"src/"` | Directory to scan for descriptor files and enforce gates. Set to `""` to scan from project root. |
129
+ | `disableModuleInterfaceImportGate` | `false` | When `true`, imports will not be forced to be from module interface. |
128
130
 
129
131
  When no settings file exists or no `module-gates` key is present, defaults apply.
130
132
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuzfrog/pi-module-gates",
3
- "version": "0.12.0",
3
+ "version": "0.13.1",
4
4
  "description": "pi extension that controls the entropy of the codebase by enforcing code module boundaries.",
5
5
  "keywords": [
6
6
  "pi-package"
package/src/config.ts CHANGED
@@ -5,12 +5,14 @@ export type ModuleGateConfig = {
5
5
  moduleDescriptorFileName: string;
6
6
  moduleDescriptorReadonly: "file" | "frontmatter" | "off";
7
7
  sourceRoot: string;
8
+ disableModuleInterfaceImportGate: boolean;
8
9
  };
9
10
 
10
11
  const DEFAULTS: ModuleGateConfig = {
11
12
  moduleDescriptorFileName: "module.md",
12
13
  moduleDescriptorReadonly: "file",
13
14
  sourceRoot: "src/",
15
+ disableModuleInterfaceImportGate: false,
14
16
  };
15
17
 
16
18
  export function loadConfig(cwd: string): ModuleGateConfig {
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  frozen:
3
3
  - system-prompt.ts
4
+ - index.ts
4
5
  ---
5
6
 
@@ -0,0 +1 @@
1
+ export { buildSystemPromptHint } from "./system-prompt.ts";
@@ -3,5 +3,6 @@ frozen:
3
3
  - export-gate.ts
4
4
  - frozen-gate.ts
5
5
  - readonly-gate.ts
6
+ - index.ts
6
7
  ---
7
8
 
@@ -4,3 +4,5 @@ import "./java.ts";
4
4
  import "./go.ts";
5
5
  import "./kotlin.ts";
6
6
  import "./scala.ts";
7
+
8
+ export { registerChecker, getChecker, ExportChecker } from "./registry.ts";
@@ -19,7 +19,9 @@ function extractExports(src: string): Signature[] {
19
19
  ),
20
20
  ].map((m) => ({ name: m[1] }));
21
21
 
22
- for (const m of src.matchAll(/^export\s*\{\s*([^}]+)\s*\}\s*from/gm)) {
22
+ for (const m of src.matchAll(
23
+ /^export\s*(?:type\s+)?\{\s*([^}]+)\s*\}\s*from/gm,
24
+ )) {
23
25
  const inner = m[1];
24
26
  for (const entry of inner.split(",")) {
25
27
  const trimmed = entry.trim();
@@ -37,5 +39,15 @@ function extractExports(src: string): Signature[] {
37
39
  results.push({ name: m[1] });
38
40
  }
39
41
 
42
+ for (const m of src.matchAll(/^export\s*\*\s+from/gm)) {
43
+ results.push({ name: "*" });
44
+ }
45
+
46
+ for (const m of src.matchAll(
47
+ /^export\s+default\s+(?!function|class|const|let|var|type|interface|enum|abstract|async|declare)([a-zA-Z_]\w*)/gm,
48
+ )) {
49
+ results.push({ name: m[1] });
50
+ }
51
+
40
52
  return results;
41
53
  }
@@ -0,0 +1,4 @@
1
+ export { checkReadonly } from "./readonly-gate.ts";
2
+ export { checkFrozen } from "./frozen-gate.ts";
3
+ export { checkExports } from "./export-gate.ts";
4
+ export { checkModuleInterfaceImports } from "./module-interface-import-gate.ts";
@@ -0,0 +1,153 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { ModuleIndex } from "../types.ts";
4
+ import { findOwningModule } from "../utils.ts";
5
+
6
+ export type ImportCheckResult =
7
+ | { blocked: true; reason: string }
8
+ | { blocked: false };
9
+
10
+ export function checkModuleInterfaceImports(
11
+ filePath: string,
12
+ afterContent: string,
13
+ index: ModuleIndex,
14
+ cwd: string,
15
+ disabled: boolean,
16
+ sourceRoot: string,
17
+ ): ImportCheckResult {
18
+ if (disabled) return { blocked: false };
19
+
20
+ const absFile = path.resolve(cwd, filePath);
21
+ const fileDir = path.dirname(absFile);
22
+ const srcRoot = path.resolve(cwd, sourceRoot);
23
+ const violations: string[] = [];
24
+
25
+ for (const importPath of extractJsImportPaths(afterContent)) {
26
+ const resolved = resolveRelativeImport(importPath, fileDir);
27
+ if (!resolved) continue;
28
+ if (isInNodeModules(resolved, cwd)) continue;
29
+
30
+ const violation = checkViolation(resolved, fileDir, cwd, index);
31
+ if (violation) violations.push(violation);
32
+ }
33
+
34
+ for (const modulePath of extractRustUsePaths(afterContent)) {
35
+ const resolved = resolveRustCratePath(modulePath, srcRoot);
36
+ if (!resolved) continue;
37
+ if (isInNodeModules(resolved, cwd)) continue;
38
+
39
+ const violation = checkViolation(resolved, fileDir, cwd, index);
40
+ if (violation) violations.push(violation);
41
+ }
42
+
43
+ if (violations.length === 0) return { blocked: false };
44
+
45
+ return {
46
+ blocked: true,
47
+ reason: `Module interface import violations:\n${violations.map((v) => ` - ${v}`).join("\n")}`,
48
+ };
49
+ }
50
+
51
+ function checkViolation(
52
+ resolved: string,
53
+ fileDir: string,
54
+ cwd: string,
55
+ index: ModuleIndex,
56
+ ): string | undefined {
57
+ const targetModule = findOwningModule(resolved, index);
58
+ if (!targetModule) return undefined;
59
+
60
+ const targetDir = path.dirname(resolved);
61
+ if (targetDir === fileDir) return undefined;
62
+
63
+ if (isInterfaceFile(resolved)) return undefined;
64
+
65
+ const sourceModule = findOwningModule(path.join(fileDir, "dummy.ts"), index);
66
+ if (sourceModule && isDescendantOrSelf(sourceModule, targetModule)) return undefined;
67
+
68
+ const relTarget = path.relative(cwd, resolved);
69
+ const relModule = path.relative(cwd, targetModule);
70
+ return `Import from "${relTarget}" bypasses module interface of ${relModule}/`;
71
+ }
72
+
73
+ function extractJsImportPaths(content: string): string[] {
74
+ const results: string[] = [];
75
+
76
+ for (const m of content.matchAll(/^\s*import\s+[\s\S]*?\s+from\s+["']([^"']+)["']/gm)) {
77
+ results.push(m[1]);
78
+ }
79
+
80
+ for (const m of content.matchAll(/^\s*(?:const|let|var)\s+[\s\S]*?=\s*require\s*\(\s*["']([^"']+)["']\s*\)/gm)) {
81
+ results.push(m[1]);
82
+ }
83
+
84
+ return results;
85
+ }
86
+
87
+ function extractRustUsePaths(content: string): string[] {
88
+ const results: string[] = [];
89
+
90
+ for (const m of content.matchAll(/^\s*(?:pub\s+)?use\s+crate::(\w+(?:::\w+)*)::/gm)) {
91
+ const segments = m[1].split("::");
92
+ if (segments.length >= 2) {
93
+ results.push(segments.join("/"));
94
+ }
95
+ }
96
+
97
+ return results;
98
+ }
99
+
100
+ function resolveRelativeImport(importPath: string, fileDir: string): string | undefined {
101
+ if (!importPath.startsWith(".")) return undefined;
102
+
103
+ const resolved = path.resolve(fileDir, importPath);
104
+ const ext = path.extname(resolved);
105
+
106
+ if (ext) {
107
+ return fs.existsSync(resolved) ? resolved : undefined;
108
+ }
109
+
110
+ for (const tryExt of [".ts", ".tsx", ".js", ".jsx", ".rs"]) {
111
+ const candidate = resolved + tryExt;
112
+ if (fs.existsSync(candidate)) return candidate;
113
+ }
114
+
115
+ return undefined;
116
+ }
117
+
118
+ function resolveRustCratePath(modulePath: string, srcRoot: string): string | undefined {
119
+ const segments = modulePath.split("/");
120
+ const base = path.resolve(srcRoot, ...segments);
121
+
122
+ const asFile = base + ".rs";
123
+ if (fs.existsSync(asFile)) return asFile;
124
+
125
+ const asMod = path.join(base, "mod.rs");
126
+ if (fs.existsSync(asMod)) return asMod;
127
+
128
+ return undefined;
129
+ }
130
+
131
+ function isInNodeModules(resolvedPath: string, _cwd: string): boolean {
132
+ return resolvedPath.split(path.sep).includes("node_modules");
133
+ }
134
+
135
+ function isInterfaceFile(absPath: string): boolean {
136
+ const basename = path.basename(absPath);
137
+ const ext = path.extname(absPath);
138
+
139
+ if ([".ts", ".tsx", ".js", ".jsx"].includes(ext)) {
140
+ return ["index.ts", "index.tsx", "index.js", "index.jsx"].includes(basename);
141
+ }
142
+
143
+ if (ext === ".rs") {
144
+ return basename === "mod.rs";
145
+ }
146
+
147
+ return false;
148
+ }
149
+
150
+ function isDescendantOrSelf(child: string, ancestor: string): boolean {
151
+ if (child === ancestor) return true;
152
+ return child.startsWith(ancestor + path.sep);
153
+ }
@@ -3,6 +3,7 @@ frozen:
3
3
  - frontmatter-parser.ts
4
4
  - module-index-builder.ts
5
5
  - validation.ts
6
+ - index.ts
6
7
  ---
7
8
 
8
9
 
@@ -0,0 +1,4 @@
1
+ export { buildModuleIndex } from "./module-index-builder.ts";
2
+ export { validateVisibleEntries } from "./validation.ts";
3
+ export { parseVisibleEntry } from "./frontmatter-parser.ts";
4
+ export type { VisibleEntryRaw, ModuleFrontmatter } from "./frontmatter-parser.ts";
@@ -8,7 +8,7 @@ import type { ModuleGateConfig } from "../config.ts";
8
8
  type ModuleDescriptorReadonly = ModuleGateConfig["moduleDescriptorReadonly"];
9
9
  import type { Dirent } from "node:fs";
10
10
  import { validateVisibleEntries } from "./validation.ts";
11
- import { parseVisibleEntry, type VisibleEntryRaw, type ModuleFrontmatter } from "./frontmatter-parser.ts";
11
+ import { parseVisibleEntry, type ModuleFrontmatter } from "./frontmatter-parser.ts";
12
12
 
13
13
  type IndexContext = {
14
14
  cwd: string;
@@ -1,7 +1,7 @@
1
1
  import * as path from "node:path";
2
2
  import { readdir } from "node:fs/promises";
3
3
  import type { ModuleIndex } from "../types.ts";
4
- import { getChecker } from "../gates/checkers/registry.ts";
4
+ import { getChecker } from "../gates/checkers/index.ts";
5
5
  import { readFileSafe } from "../utils.ts";
6
6
  import type { Dirent } from "node:fs";
7
7
 
File without changes
package/src/index.ts CHANGED
@@ -8,12 +8,15 @@ import { isToolCallEventType, parseFrontmatter } from "@earendil-works/pi-coding
8
8
  import type { ModuleIndex } from "./types.ts";
9
9
  import { loadConfig } from "./config.ts";
10
10
  import type { ModuleGateConfig } from "./config.ts";
11
- import { buildModuleIndex } from "./graph/module-index-builder.ts";
11
+ import { buildModuleIndex } from "./graph/index.ts";
12
12
  import { findOwningModule, readFileSafe, applyEdits, isWithinSourceRoot } from "./utils.ts";
13
- import { checkReadonly } from "./gates/readonly-gate.ts";
14
- import { checkExports } from "./gates/export-gate.ts";
15
- import { checkFrozen } from "./gates/frozen-gate.ts";
16
- import { buildSystemPromptHint } from "./context/system-prompt.ts";
13
+ import {
14
+ checkReadonly,
15
+ checkExports,
16
+ checkFrozen,
17
+ checkModuleInterfaceImports,
18
+ } from "./gates/index.ts";
19
+ import { buildSystemPromptHint } from "./context/index.ts";
17
20
  import "./gates/checkers/index.ts";
18
21
 
19
22
  export default function (pi: ExtensionAPI): void {
@@ -101,6 +104,11 @@ function handleEdit(
101
104
  return { block: true, reason: formatDenial(filePath, exportResult.reason, absPath, index, cwd, config.moduleDescriptorFileName) };
102
105
  }
103
106
 
107
+ const importResult = checkModuleInterfaceImports(filePath, after, index, cwd, config.disableModuleInterfaceImportGate, config.sourceRoot);
108
+ if (importResult.blocked) {
109
+ return { block: true, reason: formatDenial(filePath, importResult.reason, absPath, index, cwd, config.moduleDescriptorFileName) };
110
+ }
111
+
104
112
  return undefined;
105
113
  }
106
114
 
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  export type Signature = {
2
3
  modifier?: string;
3
4
  name: string;