@ultrst-armada/krnl 1.0.0 → 1.0.2

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.
@@ -1,453 +1,491 @@
1
1
  // src/KrnlValidatorEngine.ts
2
- import * as fs from "fs";
3
- import * as path from "path";
4
- import { Project, SyntaxKind, Node } from "ts-morph";
5
- var KrnlValidatorEngine = class {
6
- project;
7
- projectRoot;
8
- maxFileLineLimit;
9
- maxMainLogicLimit;
10
- maxHelperLineLimit;
11
- constructor(optionsOrRoot) {
12
- const options = typeof optionsOrRoot === "string" ? { projectRoot: optionsOrRoot } : optionsOrRoot;
13
- this.projectRoot = path.resolve(options.projectRoot || process.cwd());
14
- this.maxFileLineLimit = options.maxFileLineLimit ?? 200;
15
- this.maxMainLogicLimit = options.maxMainLogicLimit ?? 20;
16
- this.maxHelperLineLimit = options.maxHelperLineLimit ?? 30;
17
- const tsConfigPath = path.join(this.projectRoot, "tsconfig.json");
18
- if (fs.existsSync(tsConfigPath)) {
19
- this.project = new Project({ tsConfigFilePath: tsConfigPath, skipAddingFilesFromTsConfig: true });
20
- } else {
21
- this.project = new Project({ compilerOptions: {
22
- target: 99
23
- /* ESNext */
24
- } });
25
- }
26
- }
27
- /**
28
- * Scans and validates all source files in the project.
29
- */
30
- validateAll() {
31
- const files = this.collectSourceFiles(this.projectRoot);
32
- const errors = [];
33
- const warnings = [];
34
- this.validateWorkspaceLayout(errors);
35
- for (const f of files) {
36
- const sf = this.project.addSourceFileAtPath(f);
37
- const fileReport = this.validateSourceFile(sf);
38
- errors.push(...fileReport.errors);
39
- warnings.push(...fileReport.warnings);
40
- }
41
- return {
42
- valid: errors.length === 0,
43
- totalFiles: files.length,
44
- errors,
45
- warnings
46
- };
2
+ import * as fs2 from "fs";
3
+ import * as path4 from "path";
4
+ import { Project } from "ts-morph";
5
+
6
+ // src/rules/fileLimits.ts
7
+ function checkFileLimits(sf, options) {
8
+ const errors = [];
9
+ const warnings = [];
10
+ const filePath = sf.getFilePath();
11
+ const sourceText = sf.getText();
12
+ const lines = sourceText.split("\n");
13
+ if (lines.length > options.maxFileLineLimit) {
14
+ errors.push({
15
+ ruleId: "krnl/file-line-limit",
16
+ filePath,
17
+ message: `File exceeds maximum length of ${options.maxFileLineLimit} lines (currently ${lines.length} lines). Decompose into sub-components, custom hooks, quad-units, or concept libraries.`,
18
+ severity: "error"
19
+ });
47
20
  }
48
- /**
49
- * Validates a single SourceFile.
50
- */
51
- validateSourceFile(sf) {
52
- const errors = [];
53
- const warnings = [];
54
- const filePath = sf.getFilePath();
55
- const sourceText = sf.getText();
56
- const baseName = path.basename(filePath);
57
- if (baseName.includes("_pb.") || baseName.endsWith(".d.ts")) {
58
- return { errors, warnings };
59
- }
60
- const lines = sourceText.split("\n");
61
- if (lines.length > this.maxFileLineLimit) {
21
+ for (const decl of sf.getImportDeclarations()) {
22
+ const moduleSpec = decl.getModuleSpecifierValue();
23
+ if (moduleSpec.startsWith(".") && !moduleSpec.endsWith(".js") && !moduleSpec.endsWith(".json") && !moduleSpec.endsWith(".css") && !moduleSpec.endsWith(".svg") && !moduleSpec.endsWith(".png")) {
62
24
  errors.push({
63
- ruleId: "krnl/file-line-limit",
25
+ ruleId: "krnl/import-suffix",
64
26
  filePath,
65
- message: `File exceeds maximum length of ${this.maxFileLineLimit} lines (currently ${lines.length} lines). Decompose into sub-components, custom hooks, quad-units, or concept libraries.`,
27
+ line: decl.getStartLineNumber(),
28
+ message: `Relative import '${moduleSpec}' must end with '.js' for NodeNext module resolution.`,
66
29
  severity: "error"
67
30
  });
68
31
  }
69
- for (const decl of sf.getImportDeclarations()) {
70
- const moduleSpec = decl.getModuleSpecifierValue();
71
- if (moduleSpec.startsWith(".") && !moduleSpec.endsWith(".js") && !moduleSpec.endsWith(".json") && !moduleSpec.endsWith(".css") && !moduleSpec.endsWith(".svg") && !moduleSpec.endsWith(".png")) {
72
- const line = decl.getStartLineNumber();
32
+ }
33
+ const isHelper = filePath.includes("helper_functions") || filePath.includes("/helpers/") || filePath.includes("\\helpers\\");
34
+ if (isHelper) {
35
+ for (const fn of sf.getFunctions()) {
36
+ const fnLines = fn.getText().split("\n").length;
37
+ if (fnLines > options.maxHelperLineLimit && !sourceText.includes("Concepts.")) {
73
38
  errors.push({
74
- ruleId: "krnl/import-suffix",
39
+ ruleId: "krnl/helper-line-limit",
75
40
  filePath,
76
- line,
77
- message: `Relative import '${moduleSpec}' must end with '.js' for NodeNext module resolution.`,
41
+ line: fn.getStartLineNumber(),
42
+ message: `Helper function '${fn.getName() || "anonymous"}' exceeds ${options.maxHelperLineLimit} lines (${fnLines} lines). Must delegate to a root libraries/{Domain}Concepts.ts library.`,
78
43
  severity: "error"
79
44
  });
80
45
  }
81
46
  }
82
- const isUiTopLevel = /^(App|main|index|_app|root)\.(tsx|jsx)$/i.test(baseName);
83
- const isBackendTopLevel = /^(server|app)\.(ts|js)$/i.test(baseName) && (!filePath.includes("/components/") && !filePath.includes("/entities/") && !filePath.includes("/libraries/"));
84
- if (isUiTopLevel) {
85
- const jsxElements = sf.getDescendantsOfKind(SyntaxKind.JsxOpeningElement);
86
- const jsxSelfClosing = sf.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement);
87
- const allJsxTags = [...jsxElements, ...jsxSelfClosing];
88
- for (const jsx of allJsxTags) {
89
- const tagName = jsx.getTagNameNode().getText();
90
- if (/^[a-z]/.test(tagName)) {
91
- errors.push({
92
- ruleId: "krnl/top-level-purity",
93
- filePath,
94
- line: jsx.getStartLineNumber(),
95
- message: `Top-level file '${baseName}' violates Top-Level Purity Law. Raw JSX/HTML element '<${tagName}>' is strictly forbidden. Top-level files must ONLY import and compose React components.`,
96
- severity: "error"
97
- });
98
- }
99
- }
100
- const calls2 = sf.getDescendantsOfKind(SyntaxKind.CallExpression);
101
- for (const call of calls2) {
102
- const exprText = call.getExpression().getText();
103
- if (["useState", "useEffect", "useRef", "useMemo", "useCallback", "useReducer", "useContext", "useLayoutEffect"].includes(exprText) || /^use[A-Z]/.test(exprText) && exprText !== "use") {
104
- errors.push({
105
- ruleId: "krnl/top-level-purity",
106
- filePath,
107
- line: call.getStartLineNumber(),
108
- message: `Top-level file '${baseName}' violates Top-Level Purity Law. React hook '${exprText}' is forbidden in top-level files. Encapsulate state and effects inside dedicated component files.`,
109
- severity: "error"
110
- });
111
- }
112
- }
47
+ }
48
+ const isMainLogic = filePath.endsWith("logic.ts") || filePath.endsWith("logic.js");
49
+ if (isMainLogic) {
50
+ const substantiveLines = lines.filter((l) => {
51
+ const trimmed = l.trim();
52
+ return trimmed.length > 0 && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*");
53
+ });
54
+ if (substantiveLines.length > options.maxMainLogicLimit && !sourceText.includes("helpers") && !sourceText.includes("Concepts") && !sourceText.includes("state")) {
55
+ errors.push({
56
+ ruleId: "krnl/main-delegation",
57
+ filePath,
58
+ message: `Main logic function has ${substantiveLines.length} substantive lines without delegating to Layer 1 helper functions or Layer 0 libraries.`,
59
+ severity: "error"
60
+ });
113
61
  }
114
- if (isBackendTopLevel) {
115
- const calls2 = sf.getDescendantsOfKind(SyntaxKind.CallExpression);
116
- for (const call of calls2) {
117
- const exprText = call.getExpression().getText();
118
- if (/^(app|router|server)\.(get|post|put|delete|patch|options|head)$/.test(exprText)) {
119
- errors.push({
120
- ruleId: "krnl/top-level-purity",
121
- filePath,
122
- line: call.getStartLineNumber(),
123
- message: `Backend entry file '${baseName}' violates Entry Purity Law. Inline route definition '${exprText}' is forbidden. Delegate to dedicated route/controller modules.`,
124
- severity: "error"
125
- });
126
- }
62
+ }
63
+ return { errors, warnings };
64
+ }
65
+
66
+ // src/rules/componentRules.ts
67
+ import * as path from "path";
68
+ import { SyntaxKind } from "ts-morph";
69
+ function checkComponentRules(sf) {
70
+ const errors = [];
71
+ const warnings = [];
72
+ const filePath = sf.getFilePath();
73
+ const baseName = path.basename(filePath);
74
+ if (filePath.endsWith(".tsx") || filePath.endsWith(".jsx")) {
75
+ const componentNames = [];
76
+ for (const fn of sf.getFunctions()) {
77
+ const name = fn.getName();
78
+ if (name && /^[A-Z]/.test(name)) {
79
+ const hasJsx = fn.getDescendantsOfKind(SyntaxKind.JsxElement).length > 0 || fn.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).length > 0 || fn.getDescendantsOfKind(SyntaxKind.JsxFragment).length > 0;
80
+ if (hasJsx) componentNames.push(name);
127
81
  }
128
82
  }
129
- if (filePath.endsWith(".tsx") || filePath.endsWith(".jsx")) {
130
- const componentNames = [];
131
- for (const fn of sf.getFunctions()) {
132
- const name = fn.getName();
133
- if (name && /^[A-Z]/.test(name)) {
134
- const hasJsx = fn.getDescendantsOfKind(SyntaxKind.JsxElement).length > 0 || fn.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).length > 0 || fn.getDescendantsOfKind(SyntaxKind.JsxFragment).length > 0;
135
- if (hasJsx) {
136
- componentNames.push(name);
137
- }
138
- }
139
- }
140
- for (const stmt of sf.getVariableStatements()) {
141
- for (const decl of stmt.getDeclarations()) {
142
- const name = decl.getName();
143
- if (/^[A-Z]/.test(name)) {
144
- const typeText = decl.getTypeNode()?.getText() || "";
145
- const isFcType = typeText.includes("FC") || typeText.includes("FunctionComponent") || typeText.includes("ComponentType");
146
- const init = decl.getInitializer();
147
- if (init) {
148
- const hasJsx = init.getDescendantsOfKind(SyntaxKind.JsxElement).length > 0 || init.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).length > 0 || init.getDescendantsOfKind(SyntaxKind.JsxFragment).length > 0;
149
- if (isFcType || hasJsx) {
150
- if (!componentNames.includes(name)) {
151
- componentNames.push(name);
152
- }
153
- }
83
+ for (const stmt of sf.getVariableStatements()) {
84
+ for (const decl of stmt.getDeclarations()) {
85
+ const name = decl.getName();
86
+ if (/^[A-Z]/.test(name)) {
87
+ const typeText = decl.getTypeNode()?.getText() || "";
88
+ const isFcType = typeText.includes("FC") || typeText.includes("FunctionComponent") || typeText.includes("ComponentType");
89
+ const init = decl.getInitializer();
90
+ if (init) {
91
+ const hasJsx = init.getDescendantsOfKind(SyntaxKind.JsxElement).length > 0 || init.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).length > 0 || init.getDescendantsOfKind(SyntaxKind.JsxFragment).length > 0;
92
+ if (isFcType || hasJsx) {
93
+ if (!componentNames.includes(name)) componentNames.push(name);
154
94
  }
155
95
  }
156
96
  }
157
97
  }
158
- for (const cls of sf.getClasses()) {
159
- const name = cls.getName();
160
- const ext = cls.getExtends()?.getText() || "";
161
- if (name && (ext.includes("Component") || ext.includes("PureComponent"))) {
162
- if (!componentNames.includes(name)) {
163
- componentNames.push(name);
164
- }
165
- }
98
+ }
99
+ for (const cls of sf.getClasses()) {
100
+ const name = cls.getName();
101
+ const ext = cls.getExtends()?.getText() || "";
102
+ if (name && (ext.includes("Component") || ext.includes("PureComponent"))) {
103
+ if (!componentNames.includes(name)) componentNames.push(name);
166
104
  }
167
- if (componentNames.length > 1) {
105
+ }
106
+ if (componentNames.length > 1) {
107
+ errors.push({
108
+ ruleId: "krnl/one-component-per-file",
109
+ filePath,
110
+ message: `File '${baseName}' defines multiple React components (${componentNames.join(
111
+ ", "
112
+ )}). Krnl strictly enforces ONE component per file. Decompose into sub-component files.`,
113
+ severity: "error"
114
+ });
115
+ }
116
+ }
117
+ const isHelper = filePath.includes("helper_functions") || filePath.includes("/helpers/") || filePath.includes("\\helpers\\");
118
+ if (isHelper) {
119
+ for (const decl of sf.getImportDeclarations()) {
120
+ const spec = decl.getModuleSpecifierValue();
121
+ if (["fs", "node:fs", "http", "node:http", "ws"].includes(spec)) {
168
122
  errors.push({
169
- ruleId: "krnl/one-component-per-file",
123
+ ruleId: "krnl/helper-purity",
170
124
  filePath,
171
- message: `File '${baseName}' defines multiple React components (${componentNames.join(", ")}). Krnl strictly enforces ONE component per file. Decompose into sub-component files.`,
125
+ line: decl.getStartLineNumber(),
126
+ message: `Helper functions (Layer 1) cannot directly import I/O modules ('${spec}'). Delegate to Layer 0 libraries or Entity context.`,
172
127
  severity: "error"
173
128
  });
174
129
  }
175
130
  }
176
- const isHelper = filePath.includes("helper_functions") || filePath.includes("/helpers/") || filePath.includes("\\helpers\\");
177
- const isMainLogic = filePath.endsWith("logic.ts") || filePath.endsWith("logic.js");
178
- const isRootConcept = filePath.includes("libraries/") || filePath.includes("Concepts.ts");
179
- if (isHelper) {
180
- for (const decl of sf.getImportDeclarations()) {
181
- const spec = decl.getModuleSpecifierValue();
182
- if (["fs", "node:fs", "http", "node:http", "ws"].includes(spec)) {
183
- errors.push({
184
- ruleId: "krnl/helper-purity",
185
- filePath,
186
- line: decl.getStartLineNumber(),
187
- message: `Helper functions (Layer 1) cannot directly import I/O modules ('${spec}'). Delegate to Layer 0 libraries or Entity context.`,
188
- severity: "error"
189
- });
190
- }
191
- }
192
- for (const fn of sf.getFunctions()) {
193
- const fnLines = fn.getText().split("\n").length;
194
- if (fnLines > this.maxHelperLineLimit && !sourceText.includes("Concepts.")) {
195
- errors.push({
196
- ruleId: "krnl/helper-line-limit",
197
- filePath,
198
- line: fn.getStartLineNumber(),
199
- message: `Helper function '${fn.getName() || "anonymous"}' exceeds ${this.maxHelperLineLimit} lines (${fnLines} lines). Must delegate to a root libraries/{Domain}Concepts.ts library.`,
200
- severity: "error"
201
- });
202
- }
203
- }
204
- }
205
- if (isMainLogic) {
206
- const substantiveLines = lines.filter((l) => {
207
- const trimmed = l.trim();
208
- return trimmed.length > 0 && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*");
131
+ }
132
+ return { errors, warnings };
133
+ }
134
+
135
+ // src/rules/entityRules.ts
136
+ import * as path2 from "path";
137
+ import { SyntaxKind as SyntaxKind2, Node } from "ts-morph";
138
+ function checkEntityRules(sf) {
139
+ const errors = [];
140
+ const warnings = [];
141
+ const filePath = sf.getFilePath();
142
+ const sourceText = sf.getText();
143
+ const baseName = path2.basename(filePath);
144
+ const calls = sf.getDescendantsOfKind(SyntaxKind2.CallExpression);
145
+ for (const call of calls) {
146
+ const exprText = call.getExpression().getText();
147
+ if (exprText === "setInterval" || exprText === "window.setInterval") {
148
+ errors.push({
149
+ ruleId: "krnl/no-polling",
150
+ filePath,
151
+ line: call.getStartLineNumber(),
152
+ message: `Prohibited use of 'setInterval'. Krnl applications must use reactive event streams (entity.emitEvent / entity.on).`,
153
+ severity: "error"
209
154
  });
210
- if (substantiveLines.length > this.maxMainLogicLimit && !sourceText.includes("helpers") && !sourceText.includes("Concepts") && !sourceText.includes("state")) {
155
+ }
156
+ }
157
+ if (sourceText.includes("main = async") || sourceText.includes("export const main = async")) {
158
+ const match = sourceText.match(/main\s*=\s*async\s*\([^,]+,\s*\{([^}]*)\}\s*\)/);
159
+ if (match) {
160
+ const destructured = match[1];
161
+ if (/\bsystem\b/.test(destructured) && !/\bentity\b/.test(destructured)) {
211
162
  errors.push({
212
- ruleId: "krnl/main-delegation",
163
+ ruleId: "krnl/context-destructuring",
213
164
  filePath,
214
- message: `Main logic function has ${substantiveLines.length} substantive lines without delegating to Layer 1 helper functions or Layer 0 libraries.`,
165
+ message: `Functionality context parameter destructures 'system'. Functionality passes { state, entity }. Always destructure 'entity'.`,
215
166
  severity: "error"
216
167
  });
217
168
  }
218
169
  }
219
- if (sourceText.includes("main = async") || sourceText.includes("export const main = async")) {
220
- const match = sourceText.match(/main\s*=\s*async\s*\([^,]+,\s*\{([^}]*)\}\s*\)/);
221
- if (match) {
222
- const destructured = match[1];
223
- if (/\bsystem\b/.test(destructured) && !/\bentity\b/.test(destructured)) {
224
- errors.push({
225
- ruleId: "krnl/context-destructuring",
226
- filePath,
227
- message: `Functionality context parameter destructures 'system'. Functionality passes { state, entity }. Always destructure 'entity'.`,
228
- severity: "error"
229
- });
230
- }
170
+ }
171
+ const isEntityClass = sf.getClasses().some((c) => {
172
+ const ext = c.getExtends();
173
+ return ext && (ext.getText().includes("Entity") || ext.getText().includes("krnlEntityBase"));
174
+ });
175
+ if (isEntityClass) {
176
+ for (const decl of sf.getImportDeclarations()) {
177
+ const spec = decl.getModuleSpecifierValue();
178
+ if (spec.includes("libraries/") || spec.includes("Concepts")) {
179
+ errors.push({
180
+ ruleId: "krnl/entity-no-libraries",
181
+ filePath,
182
+ line: decl.getStartLineNumber(),
183
+ message: `Entity class violates Library Calling Invariant. Entities must not call or import libraries directly ('${spec}'). Only functionalities in 'src/functionalities/' are permitted to call shared libraries.`,
184
+ severity: "error"
185
+ });
231
186
  }
232
187
  }
233
- const calls = sf.getDescendantsOfKind(SyntaxKind.CallExpression);
188
+ let loadAllLine = -1;
189
+ const addLocalLines = [];
234
190
  for (const call of calls) {
235
- const exprText = call.getExpression().getText();
236
- if (exprText === "setInterval" || exprText === "window.setInterval") {
237
- errors.push({
238
- ruleId: "krnl/no-polling",
239
- filePath,
191
+ const txt = call.getText();
192
+ if (txt.includes("MagneticLoader.loadAll") || txt.includes("KrnlMagneticLoader.loadAll")) {
193
+ loadAllLine = call.getStartLineNumber();
194
+ } else if (txt.startsWith("this.addLocal(")) {
195
+ const hasUiSpec = txt.includes("uiSpec");
196
+ const idMatch = txt.match(/id\s*:\s*["']([^"']+)["']/);
197
+ addLocalLines.push({
240
198
  line: call.getStartLineNumber(),
241
- message: `Prohibited use of 'setInterval'. Krnl applications must use reactive event streams (entity.emitEvent / entity.on).`,
242
- severity: "error"
199
+ hasUiSpec,
200
+ id: idMatch ? idMatch[1] : "unknown"
243
201
  });
244
202
  }
245
203
  }
246
- const isEntityClass = sf.getClasses().some((c) => {
247
- const ext = c.getExtends();
248
- return ext && (ext.getText().includes("Entity") || ext.getText().includes("krnlEntityBase"));
249
- });
250
- if (isEntityClass) {
251
- for (const decl of sf.getImportDeclarations()) {
252
- const spec = decl.getModuleSpecifierValue();
253
- if (spec.includes("libraries/") || spec.includes("Concepts")) {
204
+ if (loadAllLine !== -1) {
205
+ for (const al of addLocalLines) {
206
+ if (al.hasUiSpec && al.line > loadAllLine) {
254
207
  errors.push({
255
- ruleId: "krnl/entity-no-libraries",
208
+ ruleId: "krnl/boot-order",
256
209
  filePath,
257
- line: decl.getStartLineNumber(),
258
- message: `Entity class violates Library Calling Invariant. Entities must not call or import libraries directly ('${spec}'). Only functionalities in 'src/functionalities/' are permitted to call shared libraries.`,
210
+ line: al.line,
211
+ message: `addLocal('${al.id}') with uiSpec at line ${al.line} appears AFTER loadAll() at line ${loadAllLine}. All addLocal() with UI must precede loadAll().`,
259
212
  severity: "error"
260
213
  });
261
214
  }
262
215
  }
263
- let loadAllLine = -1;
264
- const addLocalLines = [];
265
- for (const call of calls) {
266
- const txt = call.getText();
267
- if (txt.includes("MagneticLoader.loadAll") || txt.includes("KrnlMagneticLoader.loadAll")) {
268
- loadAllLine = call.getStartLineNumber();
269
- } else if (txt.startsWith("this.addLocal(")) {
270
- const hasUiSpec = txt.includes("uiSpec");
271
- const idMatch = txt.match(/id\s*:\s*["']([^"']+)["']/);
272
- addLocalLines.push({
273
- line: call.getStartLineNumber(),
274
- hasUiSpec,
275
- id: idMatch ? idMatch[1] : "unknown"
216
+ }
217
+ for (const cls of sf.getClasses()) {
218
+ const ext = cls.getExtends();
219
+ if (ext && (ext.getText().includes("Entity") || ext.getText().includes("krnlEntityBase"))) {
220
+ const loops = [
221
+ ...cls.getDescendantsOfKind(SyntaxKind2.ForStatement),
222
+ ...cls.getDescendantsOfKind(SyntaxKind2.ForOfStatement),
223
+ ...cls.getDescendantsOfKind(SyntaxKind2.ForInStatement),
224
+ ...cls.getDescendantsOfKind(SyntaxKind2.WhileStatement),
225
+ ...cls.getDescendantsOfKind(SyntaxKind2.DoStatement)
226
+ ];
227
+ for (const loop of loops) {
228
+ errors.push({
229
+ ruleId: "krnl/entity-purity",
230
+ filePath,
231
+ line: loop.getStartLineNumber(),
232
+ message: `Entity '${cls.getName() || "Anonymous"}' violates Entity Purity Law. Loops and procedural algorithms are prohibited inside entities. Move logic to a functionality or a global library function.`,
233
+ severity: "error"
276
234
  });
277
235
  }
278
- }
279
- if (loadAllLine !== -1) {
280
- for (const al of addLocalLines) {
281
- if (al.hasUiSpec && al.line > loadAllLine) {
236
+ for (const method of cls.getMethods()) {
237
+ const methodName = method.getName();
238
+ if (!["boot", "init", "dispose"].includes(methodName)) {
282
239
  errors.push({
283
- ruleId: "krnl/boot-order",
240
+ ruleId: "krnl/entity-purity",
284
241
  filePath,
285
- line: al.line,
286
- message: `addLocal('${al.id}') with uiSpec at line ${al.line} appears AFTER loadAll() at line ${loadAllLine}. All addLocal() with UI must precede loadAll().`,
242
+ line: method.getStartLineNumber(),
243
+ message: `Entity '${cls.getName() || "Anonymous"}' defines custom method '${methodName}'. Entities are purely declarative compositors/orchestrators. Define capabilities as functionalities or global library functions.`,
287
244
  severity: "error"
288
245
  });
289
246
  }
290
247
  }
291
248
  }
292
- for (const cls of sf.getClasses()) {
293
- const ext = cls.getExtends();
294
- if (ext && (ext.getText().includes("Entity") || ext.getText().includes("krnlEntityBase"))) {
295
- const loops = [
296
- ...cls.getDescendantsOfKind(SyntaxKind.ForStatement),
297
- ...cls.getDescendantsOfKind(SyntaxKind.ForOfStatement),
298
- ...cls.getDescendantsOfKind(SyntaxKind.ForInStatement),
299
- ...cls.getDescendantsOfKind(SyntaxKind.WhileStatement),
300
- ...cls.getDescendantsOfKind(SyntaxKind.DoStatement)
301
- ];
302
- for (const loop of loops) {
249
+ }
250
+ for (const call of calls) {
251
+ const callText = call.getText();
252
+ if (callText.startsWith("this.orchestration(") || callText.startsWith("this.flow(")) {
253
+ let currentExpr = call;
254
+ const chainCalls = [];
255
+ while (currentExpr && Node.isCallExpression(currentExpr)) {
256
+ const propAccess = currentExpr.getExpression();
257
+ if (Node.isPropertyAccessExpression(propAccess)) {
258
+ const methodName = propAccess.getName();
259
+ const args = currentExpr.getArguments();
260
+ const argText = args.length > 0 ? args[0].getText() : void 0;
261
+ chainCalls.unshift({ name: methodName, node: currentExpr, argText });
262
+ currentExpr = propAccess.getExpression();
263
+ } else {
264
+ break;
265
+ }
266
+ }
267
+ let chainId = "orchestration";
268
+ for (const step of chainCalls) {
269
+ if (step.name === "orchestration" || step.name === "flow") {
270
+ chainId = step.argText ? step.argText.replace(/['"]/g, "") : "orchestration";
271
+ } else if (step.name === "thenFunction") {
303
272
  errors.push({
304
- ruleId: "krnl/entity-purity",
273
+ ruleId: "krnl/entity-no-direct-library-calls",
305
274
  filePath,
306
- line: loop.getStartLineNumber(),
307
- message: `Entity '${cls.getName() || "Anonymous"}' violates Entity Purity Law. Loops and procedural algorithms are prohibited inside entities. Move logic to a functionality or a global library function.`,
275
+ line: step.node.getStartLineNumber(),
276
+ message: `Orchestration chain '${chainId}' violates Entity Purity Law. Entities cannot chain direct library functions via thenFunction(). Entities must only orchestrate functionalities (thenFunctionality) and orchestrations from other entities (thenEntityOrchestration).`,
308
277
  severity: "error"
309
278
  });
310
279
  }
311
- for (const method of cls.getMethods()) {
312
- const methodName = method.getName();
313
- if (!["boot", "init", "dispose"].includes(methodName)) {
314
- errors.push({
315
- ruleId: "krnl/entity-purity",
316
- filePath,
317
- line: method.getStartLineNumber(),
318
- message: `Entity '${cls.getName() || "Anonymous"}' defines custom method '${methodName}'. Entities are purely declarative compositors/orchestrators. Define capabilities as functionalities or global library functions.`,
319
- severity: "error"
320
- });
321
- }
322
- }
323
280
  }
324
281
  }
325
- for (const call of calls) {
326
- const callText = call.getText();
327
- if (callText.startsWith("this.orchestration(") || callText.startsWith("this.flow(")) {
328
- let currentExpr = call;
329
- const chainCalls = [];
330
- while (currentExpr && Node.isCallExpression(currentExpr)) {
331
- const propAccess = currentExpr.getExpression();
332
- if (Node.isPropertyAccessExpression(propAccess)) {
333
- const methodName = propAccess.getName();
334
- const args = currentExpr.getArguments();
335
- const argText = args.length > 0 ? args[0].getText() : void 0;
336
- chainCalls.unshift({ name: methodName, node: currentExpr, argText });
337
- currentExpr = propAccess.getExpression();
338
- } else {
339
- break;
340
- }
341
- }
342
- let consecutiveFunctions = 0;
343
- let chainId = "orchestration";
344
- for (const step of chainCalls) {
345
- if (step.name === "orchestration" || step.name === "flow") {
346
- chainId = step.argText ? step.argText.replace(/['"]/g, "") : "orchestration";
347
- } else if (step.name === "thenFunctionality" || step.name === "then") {
348
- consecutiveFunctions = 0;
349
- } else if (step.name === "thenEntityOrchestration" || step.name === "thenEntity") {
350
- consecutiveFunctions = 0;
351
- } else if (step.name === "thenFunction") {
352
- errors.push({
353
- ruleId: "krnl/entity-no-direct-library-calls",
354
- filePath,
355
- line: step.node.getStartLineNumber(),
356
- message: `Orchestration chain '${chainId}' violates Entity Purity Law. Entities cannot chain direct library functions via thenFunction(). Entities must only orchestrate functionalities (thenFunctionality) and orchestrations from other entities (thenEntityOrchestration).`,
357
- severity: "error"
358
- });
359
- }
360
- }
361
- }
282
+ }
283
+ }
284
+ return { errors, warnings };
285
+ }
286
+
287
+ // src/rules/workspaceRules.ts
288
+ import * as fs from "fs";
289
+ import * as path3 from "path";
290
+ import { SyntaxKind as SyntaxKind3 } from "ts-morph";
291
+ function checkTopLevelPurity(sf) {
292
+ const errors = [];
293
+ const warnings = [];
294
+ const filePath = sf.getFilePath();
295
+ const baseName = path3.basename(filePath);
296
+ const isUiTopLevel = /^(App|main|index|_app|root)\.(tsx|jsx)$/i.test(baseName);
297
+ const isBackendTopLevel = /^(server|app)\.(ts|js)$/i.test(baseName) && !filePath.includes("/components/") && !filePath.includes("/entities/") && !filePath.includes("/libraries/");
298
+ if (isUiTopLevel) {
299
+ const jsxElements = sf.getDescendantsOfKind(SyntaxKind3.JsxOpeningElement);
300
+ const jsxSelfClosing = sf.getDescendantsOfKind(SyntaxKind3.JsxSelfClosingElement);
301
+ const allJsxTags = [...jsxElements, ...jsxSelfClosing];
302
+ for (const jsx of allJsxTags) {
303
+ const tagName = jsx.getTagNameNode().getText();
304
+ if (/^[a-z]/.test(tagName)) {
305
+ errors.push({
306
+ ruleId: "krnl/top-level-purity",
307
+ filePath,
308
+ line: jsx.getStartLineNumber(),
309
+ message: `Top-level file '${baseName}' violates Top-Level Purity Law. Raw JSX/HTML element '<${tagName}>' is strictly forbidden. Top-level files must ONLY import and compose React components.`,
310
+ severity: "error"
311
+ });
312
+ }
313
+ }
314
+ const calls = sf.getDescendantsOfKind(SyntaxKind3.CallExpression);
315
+ for (const call of calls) {
316
+ const exprText = call.getExpression().getText();
317
+ if ([
318
+ "useState",
319
+ "useEffect",
320
+ "useRef",
321
+ "useMemo",
322
+ "useCallback",
323
+ "useReducer",
324
+ "useContext",
325
+ "useLayoutEffect"
326
+ ].includes(exprText) || /^use[A-Z]/.test(exprText) && exprText !== "use") {
327
+ errors.push({
328
+ ruleId: "krnl/top-level-purity",
329
+ filePath,
330
+ line: call.getStartLineNumber(),
331
+ message: `Top-level file '${baseName}' violates Top-Level Purity Law. React hook '${exprText}' is forbidden in top-level files. Encapsulate state and effects inside dedicated component files.`,
332
+ severity: "error"
333
+ });
362
334
  }
363
335
  }
364
- return { errors, warnings };
365
336
  }
366
- /**
367
- * Validates workspace layout invariants for symmetric Krnl architecture.
368
- */
369
- validateWorkspaceLayout(errors) {
370
- const krnlDir = path.join(this.projectRoot, "krnl");
371
- const srcDir = path.join(this.projectRoot, "src");
372
- const targetDir = fs.existsSync(krnlDir) ? krnlDir : fs.existsSync(srcDir) ? srcDir : null;
373
- if (!targetDir) return;
374
- const backendDir = path.join(targetDir, "backend");
375
- const frontendDir = path.join(targetDir, "frontend");
376
- if (fs.existsSync(backendDir)) {
377
- const bLibs = path.join(backendDir, "libraries");
378
- if (fs.existsSync(bLibs)) {
379
- for (const entry of fs.readdirSync(bLibs, { withFileTypes: true })) {
380
- if (!entry.isDirectory()) continue;
381
- const libPath = path.join(bLibs, entry.name);
382
- const manifestPath = path.join(libPath, "manifest.json");
383
- if (!fs.existsSync(manifestPath)) {
384
- errors.push({
385
- ruleId: "krnl/concept-manifest-required",
386
- filePath: libPath,
387
- message: `Backend concept library "${entry.name}" is missing a manifest.json file.`,
388
- severity: "error"
389
- });
390
- }
337
+ if (isBackendTopLevel) {
338
+ const calls = sf.getDescendantsOfKind(SyntaxKind3.CallExpression);
339
+ for (const call of calls) {
340
+ const exprText = call.getExpression().getText();
341
+ if (/^(app|router|server)\.(get|post|put|delete|patch|options|head)$/.test(exprText)) {
342
+ errors.push({
343
+ ruleId: "krnl/top-level-purity",
344
+ filePath,
345
+ line: call.getStartLineNumber(),
346
+ message: `Backend entry file '${baseName}' violates Entry Purity Law. Inline route definition '${exprText}' is forbidden. Delegate to dedicated route/controller modules.`,
347
+ severity: "error"
348
+ });
349
+ }
350
+ }
351
+ }
352
+ return { errors, warnings };
353
+ }
354
+ function checkWorkspaceLayout(projectRoot, errors) {
355
+ const krnlDir = path3.join(projectRoot, "krnl");
356
+ const srcDir = path3.join(projectRoot, "src");
357
+ const targetDir = fs.existsSync(krnlDir) ? krnlDir : fs.existsSync(srcDir) ? srcDir : null;
358
+ if (!targetDir) return;
359
+ const backendDir = path3.join(targetDir, "backend");
360
+ const frontendDir = path3.join(targetDir, "frontend");
361
+ if (fs.existsSync(backendDir)) {
362
+ const bLibs = path3.join(backendDir, "libraries");
363
+ if (fs.existsSync(bLibs)) {
364
+ for (const entry of fs.readdirSync(bLibs, { withFileTypes: true })) {
365
+ if (!entry.isDirectory()) continue;
366
+ const libPath = path3.join(bLibs, entry.name);
367
+ if (!fs.existsSync(path3.join(libPath, "manifest.json"))) {
368
+ errors.push({
369
+ ruleId: "krnl/concept-manifest-required",
370
+ filePath: libPath,
371
+ message: `Backend concept library "${entry.name}" is missing a manifest.json file.`,
372
+ severity: "error"
373
+ });
391
374
  }
392
375
  }
393
- const bFuncs = path.join(backendDir, "functionalities");
394
- if (fs.existsSync(bFuncs)) {
395
- for (const entry of fs.readdirSync(bFuncs, { withFileTypes: true })) {
396
- if (!entry.isDirectory()) continue;
397
- const funcPath = path.join(bFuncs, entry.name);
398
- const manifestPath = path.join(funcPath, "manifest.json");
399
- if (!fs.existsSync(manifestPath)) {
400
- errors.push({
401
- ruleId: "krnl/functionality-manifest-required",
402
- filePath: funcPath,
403
- message: `Backend functionality "${entry.name}" is missing a manifest.json file.`,
404
- severity: "error"
405
- });
406
- }
376
+ }
377
+ const bFuncs = path3.join(backendDir, "functionalities");
378
+ if (fs.existsSync(bFuncs)) {
379
+ for (const entry of fs.readdirSync(bFuncs, { withFileTypes: true })) {
380
+ if (!entry.isDirectory()) continue;
381
+ const funcPath = path3.join(bFuncs, entry.name);
382
+ if (!fs.existsSync(path3.join(funcPath, "manifest.json"))) {
383
+ errors.push({
384
+ ruleId: "krnl/functionality-manifest-required",
385
+ filePath: funcPath,
386
+ message: `Backend functionality "${entry.name}" is missing a manifest.json file.`,
387
+ severity: "error"
388
+ });
407
389
  }
408
390
  }
409
391
  }
410
- if (fs.existsSync(frontendDir)) {
411
- const fLibs = path.join(frontendDir, "libraries");
412
- if (fs.existsSync(fLibs)) {
413
- for (const entry of fs.readdirSync(fLibs, { withFileTypes: true })) {
414
- if (!entry.isDirectory()) continue;
415
- const libPath = path.join(fLibs, entry.name);
416
- const manifestPath = path.join(libPath, "manifest.json");
417
- if (!fs.existsSync(manifestPath)) {
418
- errors.push({
419
- ruleId: "krnl/frontend-concept-manifest-required",
420
- filePath: libPath,
421
- message: `Frontend concept library "${entry.name}" is missing a manifest.json file.`,
422
- severity: "error"
423
- });
424
- }
392
+ }
393
+ if (fs.existsSync(frontendDir)) {
394
+ const fLibs = path3.join(frontendDir, "libraries");
395
+ if (fs.existsSync(fLibs)) {
396
+ for (const entry of fs.readdirSync(fLibs, { withFileTypes: true })) {
397
+ if (!entry.isDirectory()) continue;
398
+ const libPath = path3.join(fLibs, entry.name);
399
+ if (!fs.existsSync(path3.join(libPath, "manifest.json"))) {
400
+ errors.push({
401
+ ruleId: "krnl/frontend-concept-manifest-required",
402
+ filePath: libPath,
403
+ message: `Frontend concept library "${entry.name}" is missing a manifest.json file.`,
404
+ severity: "error"
405
+ });
425
406
  }
426
407
  }
427
- const fFuncs = path.join(frontendDir, "functionalities");
428
- if (fs.existsSync(fFuncs)) {
429
- for (const entry of fs.readdirSync(fFuncs, { withFileTypes: true })) {
430
- if (!entry.isDirectory()) continue;
431
- const funcPath = path.join(fFuncs, entry.name);
432
- const manifestPath = path.join(funcPath, "manifest.json");
433
- if (!fs.existsSync(manifestPath)) {
434
- errors.push({
435
- ruleId: "krnl/frontend-functionality-manifest-required",
436
- filePath: funcPath,
437
- message: `Frontend functionality "${entry.name}" is missing a manifest.json file.`,
438
- severity: "error"
439
- });
440
- }
408
+ }
409
+ const fFuncs = path3.join(frontendDir, "functionalities");
410
+ if (fs.existsSync(fFuncs)) {
411
+ for (const entry of fs.readdirSync(fFuncs, { withFileTypes: true })) {
412
+ if (!entry.isDirectory()) continue;
413
+ const funcPath = path3.join(fFuncs, entry.name);
414
+ if (!fs.existsSync(path3.join(funcPath, "manifest.json"))) {
415
+ errors.push({
416
+ ruleId: "krnl/frontend-functionality-manifest-required",
417
+ filePath: funcPath,
418
+ message: `Frontend functionality "${entry.name}" is missing a manifest.json file.`,
419
+ severity: "error"
420
+ });
441
421
  }
442
422
  }
443
423
  }
444
424
  }
425
+ }
426
+
427
+ // src/KrnlValidatorEngine.ts
428
+ var KrnlValidatorEngine = class {
429
+ project;
430
+ projectRoot;
431
+ limitOptions;
432
+ constructor(optionsOrRoot) {
433
+ const options = typeof optionsOrRoot === "string" ? { projectRoot: optionsOrRoot } : optionsOrRoot;
434
+ this.projectRoot = path4.resolve(options.projectRoot || process.cwd());
435
+ this.limitOptions = {
436
+ maxFileLineLimit: options.maxFileLineLimit ?? 200,
437
+ maxMainLogicLimit: options.maxMainLogicLimit ?? 20,
438
+ maxHelperLineLimit: options.maxHelperLineLimit ?? 30
439
+ };
440
+ const tsConfigPath = path4.join(this.projectRoot, "tsconfig.json");
441
+ if (fs2.existsSync(tsConfigPath)) {
442
+ this.project = new Project({ tsConfigFilePath: tsConfigPath, skipAddingFilesFromTsConfig: true });
443
+ } else {
444
+ this.project = new Project({ compilerOptions: {
445
+ target: 99
446
+ /* ESNext */
447
+ } });
448
+ }
449
+ }
450
+ validateAll() {
451
+ const files = this.collectSourceFiles(this.projectRoot);
452
+ const errors = [];
453
+ const warnings = [];
454
+ checkWorkspaceLayout(this.projectRoot, errors);
455
+ for (const f of files) {
456
+ const sf = this.project.addSourceFileAtPath(f);
457
+ const fileReport = this.validateSourceFile(sf);
458
+ errors.push(...fileReport.errors);
459
+ warnings.push(...fileReport.warnings);
460
+ }
461
+ return {
462
+ valid: errors.length === 0,
463
+ totalFiles: files.length,
464
+ errors,
465
+ warnings
466
+ };
467
+ }
468
+ validateSourceFile(sf) {
469
+ const filePath = sf.getFilePath();
470
+ const baseName = path4.basename(filePath);
471
+ if (baseName.includes("_pb.") || baseName.endsWith(".d.ts")) {
472
+ return { errors: [], warnings: [] };
473
+ }
474
+ const limits = checkFileLimits(sf, this.limitOptions);
475
+ const purity = checkTopLevelPurity(sf);
476
+ const components = checkComponentRules(sf);
477
+ const entity = checkEntityRules(sf);
478
+ return {
479
+ errors: [...limits.errors, ...purity.errors, ...components.errors, ...entity.errors],
480
+ warnings: [...limits.warnings, ...purity.warnings, ...components.warnings, ...entity.warnings]
481
+ };
482
+ }
445
483
  collectSourceFiles(dir, collected = []) {
446
- if (!fs.existsSync(dir)) return collected;
447
- const entries = fs.readdirSync(dir, { withFileTypes: true });
484
+ if (!fs2.existsSync(dir)) return collected;
485
+ const entries = fs2.readdirSync(dir, { withFileTypes: true });
448
486
  for (const entry of entries) {
449
487
  if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".git") continue;
450
- const full = path.join(dir, entry.name);
488
+ const full = path4.join(dir, entry.name);
451
489
  if (entry.isDirectory()) {
452
490
  this.collectSourceFiles(full, collected);
453
491
  } else if (entry.isFile() && (entry.name.endsWith(".ts") || entry.name.endsWith(".js") || entry.name.endsWith(".tsx") || entry.name.endsWith(".jsx"))) {