@mandujs/core 0.9.45 → 0.10.0

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.
Files changed (41) hide show
  1. package/package.json +1 -1
  2. package/src/brain/doctor/config-analyzer.ts +498 -0
  3. package/src/brain/doctor/index.ts +10 -0
  4. package/src/change/snapshot.ts +46 -1
  5. package/src/change/types.ts +13 -0
  6. package/src/config/index.ts +8 -2
  7. package/src/config/mcp-ref.ts +348 -0
  8. package/src/config/mcp-status.ts +348 -0
  9. package/src/config/metadata.test.ts +308 -0
  10. package/src/config/metadata.ts +293 -0
  11. package/src/config/symbols.ts +144 -0
  12. package/src/contract/index.ts +26 -25
  13. package/src/contract/protection.ts +364 -0
  14. package/src/error/domains.ts +265 -0
  15. package/src/error/index.ts +25 -13
  16. package/src/filling/filling.ts +88 -6
  17. package/src/guard/analyzer.ts +7 -2
  18. package/src/guard/config-guard.ts +281 -0
  19. package/src/guard/decision-memory.test.ts +293 -0
  20. package/src/guard/decision-memory.ts +532 -0
  21. package/src/guard/healing.test.ts +259 -0
  22. package/src/guard/healing.ts +874 -0
  23. package/src/guard/index.ts +119 -0
  24. package/src/guard/negotiation.test.ts +282 -0
  25. package/src/guard/negotiation.ts +975 -0
  26. package/src/guard/semantic-slots.test.ts +379 -0
  27. package/src/guard/semantic-slots.ts +796 -0
  28. package/src/index.ts +2 -0
  29. package/src/lockfile/generate.ts +259 -0
  30. package/src/lockfile/index.ts +186 -0
  31. package/src/lockfile/lockfile.test.ts +410 -0
  32. package/src/lockfile/types.ts +184 -0
  33. package/src/lockfile/validate.ts +308 -0
  34. package/src/runtime/security.ts +155 -0
  35. package/src/runtime/server.ts +320 -258
  36. package/src/utils/differ.test.ts +342 -0
  37. package/src/utils/differ.ts +482 -0
  38. package/src/utils/hasher.test.ts +326 -0
  39. package/src/utils/hasher.ts +319 -0
  40. package/src/utils/index.ts +29 -0
  41. package/src/utils/safe-io.ts +188 -0
@@ -0,0 +1,265 @@
1
+ /**
2
+ * 도메인별 에러 클래스
3
+ *
4
+ * ManduError 인터페이스 기반의 실제 Error 클래스들.
5
+ * try-catch에서 instanceof 체크 가능.
6
+ */
7
+
8
+ import { ErrorCode, ERROR_MESSAGES, ERROR_SUMMARIES } from "./types";
9
+ import type { ManduError, RouteContext, ErrorType } from "./types";
10
+
11
+ /**
12
+ * Mandu 에러 베이스 클래스
13
+ */
14
+ export abstract class ManduBaseError extends Error implements ManduError {
15
+ abstract readonly errorType: ErrorType;
16
+ readonly code: ErrorCode | string;
17
+ readonly httpStatus?: number;
18
+ readonly summary: string;
19
+ readonly fix: { file: string; suggestion: string; line?: number };
20
+ readonly route?: RouteContext;
21
+ readonly timestamp: string;
22
+
23
+ constructor(
24
+ code: ErrorCode | string,
25
+ message: string,
26
+ fix: { file: string; suggestion: string; line?: number },
27
+ options?: {
28
+ httpStatus?: number;
29
+ route?: RouteContext;
30
+ cause?: unknown;
31
+ }
32
+ ) {
33
+ super(message, { cause: options?.cause });
34
+ this.name = this.constructor.name;
35
+ this.code = code;
36
+ this.httpStatus = options?.httpStatus;
37
+ this.summary =
38
+ typeof code === "string" && code in ERROR_SUMMARIES
39
+ ? ERROR_SUMMARIES[code as ErrorCode]
40
+ : message;
41
+ this.fix = fix;
42
+ this.route = options?.route;
43
+ this.timestamp = new Date().toISOString();
44
+ }
45
+
46
+ /**
47
+ * ManduError 인터페이스로 변환
48
+ */
49
+ toManduError(): ManduError {
50
+ return {
51
+ errorType: this.errorType,
52
+ code: this.code,
53
+ httpStatus: this.httpStatus,
54
+ message: this.message,
55
+ summary: this.summary,
56
+ fix: this.fix,
57
+ route: this.route,
58
+ timestamp: this.timestamp,
59
+ };
60
+ }
61
+ }
62
+
63
+ // ============================================================
64
+ // 파일 시스템 에러
65
+ // ============================================================
66
+
67
+ /**
68
+ * 파일 읽기/쓰기 에러
69
+ */
70
+ export class FileError extends ManduBaseError {
71
+ readonly errorType = "LOGIC_ERROR" as const;
72
+ readonly filePath: string;
73
+ readonly operation: "read" | "write" | "access" | "stat";
74
+
75
+ constructor(
76
+ filePath: string,
77
+ operation: "read" | "write" | "access" | "stat",
78
+ cause?: unknown
79
+ ) {
80
+ const message = `파일 ${operation} 실패: ${filePath}`;
81
+ super(
82
+ ErrorCode.SLOT_IMPORT_ERROR,
83
+ message,
84
+ {
85
+ file: filePath,
86
+ suggestion: `파일이 존재하고 읽기 권한이 있는지 확인하세요`,
87
+ },
88
+ { cause }
89
+ );
90
+ this.filePath = filePath;
91
+ this.operation = operation;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * 디렉토리 읽기 에러
97
+ */
98
+ export class DirectoryError extends ManduBaseError {
99
+ readonly errorType = "LOGIC_ERROR" as const;
100
+ readonly dirPath: string;
101
+
102
+ constructor(dirPath: string, cause?: unknown) {
103
+ super(
104
+ ErrorCode.SLOT_NOT_FOUND,
105
+ `디렉토리 읽기 실패: ${dirPath}`,
106
+ {
107
+ file: dirPath,
108
+ suggestion: `디렉토리가 존재하고 접근 가능한지 확인하세요`,
109
+ },
110
+ { cause }
111
+ );
112
+ this.dirPath = dirPath;
113
+ }
114
+ }
115
+
116
+ // ============================================================
117
+ // Guard 에러
118
+ // ============================================================
119
+
120
+ /**
121
+ * Guard 아키텍처 검사 에러
122
+ */
123
+ export class GuardError extends ManduBaseError {
124
+ readonly errorType = "LOGIC_ERROR" as const;
125
+ readonly ruleId: string;
126
+
127
+ constructor(
128
+ ruleId: string,
129
+ message: string,
130
+ file: string,
131
+ options?: {
132
+ line?: number;
133
+ suggestion?: string;
134
+ cause?: unknown;
135
+ }
136
+ ) {
137
+ super(
138
+ ErrorCode.SLOT_VALIDATION_ERROR,
139
+ message,
140
+ {
141
+ file,
142
+ suggestion: options?.suggestion || "아키텍처 규칙을 확인하세요",
143
+ line: options?.line,
144
+ },
145
+ { cause: options?.cause }
146
+ );
147
+ this.ruleId = ruleId;
148
+ }
149
+ }
150
+
151
+ // ============================================================
152
+ // Router 에러
153
+ // ============================================================
154
+
155
+ /**
156
+ * 라우터 에러
157
+ */
158
+ export class RouterError extends ManduBaseError {
159
+ readonly errorType = "FRAMEWORK_BUG" as const;
160
+
161
+ constructor(
162
+ message: string,
163
+ file: string,
164
+ options?: {
165
+ route?: RouteContext;
166
+ cause?: unknown;
167
+ }
168
+ ) {
169
+ super(
170
+ ErrorCode.FRAMEWORK_ROUTER_ERROR,
171
+ message,
172
+ {
173
+ file,
174
+ suggestion: "라우트 설정을 확인하세요",
175
+ },
176
+ { httpStatus: 500, ...options }
177
+ );
178
+ }
179
+ }
180
+
181
+ // ============================================================
182
+ // SSR 에러
183
+ // ============================================================
184
+
185
+ /**
186
+ * SSR 렌더링 에러
187
+ */
188
+ export class SSRError extends ManduBaseError {
189
+ readonly errorType = "FRAMEWORK_BUG" as const;
190
+
191
+ constructor(
192
+ message: string,
193
+ route: RouteContext,
194
+ cause?: unknown
195
+ ) {
196
+ super(
197
+ ErrorCode.FRAMEWORK_SSR_ERROR,
198
+ message,
199
+ {
200
+ file: `app/${route.id}/page.tsx`,
201
+ suggestion: "페이지 컴포넌트에서 렌더링 오류가 발생했습니다",
202
+ },
203
+ { httpStatus: 500, route, cause }
204
+ );
205
+ }
206
+ }
207
+
208
+ // ============================================================
209
+ // Contract 에러
210
+ // ============================================================
211
+
212
+ /**
213
+ * API 계약 위반 에러
214
+ */
215
+ export class ContractError extends ManduBaseError {
216
+ readonly errorType = "LOGIC_ERROR" as const;
217
+
218
+ constructor(
219
+ message: string,
220
+ contractFile: string,
221
+ options?: {
222
+ route?: RouteContext;
223
+ cause?: unknown;
224
+ }
225
+ ) {
226
+ super(
227
+ ErrorCode.SLOT_VALIDATION_ERROR,
228
+ message,
229
+ {
230
+ file: contractFile,
231
+ suggestion: "API 계약과 실제 구현이 일치하는지 확인하세요",
232
+ },
233
+ { httpStatus: 400, ...options }
234
+ );
235
+ }
236
+ }
237
+
238
+ // ============================================================
239
+ // Security 에러
240
+ // ============================================================
241
+
242
+ /**
243
+ * 보안 관련 에러
244
+ */
245
+ export class SecurityError extends ManduBaseError {
246
+ readonly errorType = "LOGIC_ERROR" as const;
247
+ readonly securityType: "path_traversal" | "injection" | "unauthorized" | "import_violation";
248
+
249
+ constructor(
250
+ securityType: "path_traversal" | "injection" | "unauthorized" | "import_violation",
251
+ message: string,
252
+ file?: string
253
+ ) {
254
+ super(
255
+ ErrorCode.SLOT_HANDLER_ERROR,
256
+ message,
257
+ {
258
+ file: file || "unknown",
259
+ suggestion: "보안 정책을 위반하는 요청입니다",
260
+ },
261
+ { httpStatus: 403 }
262
+ );
263
+ this.securityType = securityType;
264
+ }
265
+ }
@@ -2,6 +2,18 @@
2
2
  export type { ManduError, RouteContext, FixTarget, DebugInfo, ErrorType } from "./types";
3
3
  export { ErrorCode, ERROR_MESSAGES, ERROR_SUMMARIES } from "./types";
4
4
 
5
+ // Domain Error Classes
6
+ export {
7
+ ManduBaseError,
8
+ FileError,
9
+ DirectoryError,
10
+ GuardError,
11
+ RouterError,
12
+ SSRError,
13
+ ContractError,
14
+ SecurityError,
15
+ } from "./domains";
16
+
5
17
  // Stack Analyzer
6
18
  export { StackTraceAnalyzer, type StackFrame } from "./stack-analyzer";
7
19
 
@@ -14,16 +26,16 @@ export {
14
26
  } from "./classifier";
15
27
 
16
28
  // Formatter
17
- export {
18
- formatErrorResponse,
19
- formatErrorForConsole,
20
- createNotFoundResponse,
21
- createHandlerNotFoundResponse,
22
- createPageLoadErrorResponse,
23
- createSSRErrorResponse,
24
- type FormatOptions,
25
- } from "./formatter";
26
-
27
- // Result helpers
28
- export type { Result } from "./result";
29
- export { ok, err, statusFromError, errorToResponse } from "./result";
29
+ export {
30
+ formatErrorResponse,
31
+ formatErrorForConsole,
32
+ createNotFoundResponse,
33
+ createHandlerNotFoundResponse,
34
+ createPageLoadErrorResponse,
35
+ createSSRErrorResponse,
36
+ type FormatOptions,
37
+ } from "./formatter";
38
+
39
+ // Result helpers
40
+ export type { Result } from "./result";
41
+ export { ok, err, statusFromError, errorToResponse } from "./result";
@@ -3,11 +3,11 @@
3
3
  * 체이닝 API로 비즈니스 로직 정의
4
4
  */
5
5
 
6
- import { ManduContext, ValidationError } from "./context";
7
- import { AuthenticationError, AuthorizationError } from "./auth";
8
- import { ErrorClassifier, formatErrorResponse, ErrorCode } from "../error";
9
- import { TIMEOUTS } from "../constants";
10
- import { createContract, type ContractDefinition, type ContractInstance } from "../contract";
6
+ import { ManduContext, ValidationError } from "./context";
7
+ import { AuthenticationError, AuthorizationError } from "./auth";
8
+ import { ErrorClassifier, formatErrorResponse, ErrorCode } from "../error";
9
+ import { TIMEOUTS } from "../constants";
10
+ import { createContract, type ContractDefinition, type ContractInstance } from "../contract";
11
11
  import {
12
12
  type Middleware as RuntimeMiddleware,
13
13
  type MiddlewareEntry,
@@ -26,6 +26,7 @@ import {
26
26
  executeLifecycle,
27
27
  type ExecuteOptions,
28
28
  } from "../runtime/lifecycle";
29
+ import type { SlotMetadata, SlotConstraints } from "../guard/semantic-slots";
29
30
 
30
31
  /** Handler function type */
31
32
  export type Handler = (ctx: ManduContext) => Response | Promise<Response>;
@@ -60,6 +61,8 @@ interface FillingConfig<TLoaderData = unknown> {
60
61
  loader?: Loader<TLoaderData>;
61
62
  lifecycle: LifecycleStore;
62
63
  middleware: MiddlewareEntry[];
64
+ /** Semantic slot metadata */
65
+ semantic: SlotMetadata;
63
66
  }
64
67
 
65
68
  export class ManduFilling<TLoaderData = unknown> {
@@ -67,8 +70,87 @@ export class ManduFilling<TLoaderData = unknown> {
67
70
  handlers: new Map(),
68
71
  lifecycle: createLifecycleStore(),
69
72
  middleware: [],
73
+ semantic: {},
70
74
  };
71
75
 
76
+ /**
77
+ * Semantic Slot: 슬롯의 목적 정의
78
+ * AI가 이 슬롯의 역할을 이해하고 적절한 구현을 하도록 안내
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * Mandu.filling()
83
+ * .purpose("사용자 목록 조회 API")
84
+ * .get(async (ctx) => { ... });
85
+ * ```
86
+ */
87
+ purpose(purposeText: string): this {
88
+ this.config.semantic.purpose = purposeText;
89
+ return this;
90
+ }
91
+
92
+ /**
93
+ * Semantic Slot: 상세 설명 추가
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * Mandu.filling()
98
+ * .purpose("사용자 목록 조회 API")
99
+ * .description("페이지네이션된 사용자 목록 반환. 관리자 전용.")
100
+ * .get(async (ctx) => { ... });
101
+ * ```
102
+ */
103
+ description(descText: string): this {
104
+ this.config.semantic.description = descText;
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * Semantic Slot: 제약 조건 정의
110
+ * AI가 이 범위 내에서만 구현하도록 제한
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * Mandu.filling()
115
+ * .purpose("사용자 목록 조회 API")
116
+ * .constraints({
117
+ * maxLines: 50,
118
+ * maxCyclomaticComplexity: 10,
119
+ * requiredPatterns: ["input-validation", "error-handling"],
120
+ * forbiddenPatterns: ["direct-db-write"],
121
+ * allowedImports: ["server/domain/user/*", "shared/utils/*"],
122
+ * })
123
+ * .get(async (ctx) => { ... });
124
+ * ```
125
+ */
126
+ constraints(constraintsConfig: SlotConstraints): this {
127
+ this.config.semantic.constraints = constraintsConfig;
128
+ return this;
129
+ }
130
+
131
+ /**
132
+ * Semantic Slot: 태그 추가 (검색 및 분류용)
133
+ */
134
+ tags(...tagList: string[]): this {
135
+ this.config.semantic.tags = tagList;
136
+ return this;
137
+ }
138
+
139
+ /**
140
+ * Semantic Slot: 소유자/담당자 지정
141
+ */
142
+ owner(ownerName: string): this {
143
+ this.config.semantic.owner = ownerName;
144
+ return this;
145
+ }
146
+
147
+ /**
148
+ * 슬롯 메타데이터 가져오기
149
+ */
150
+ getSemanticMetadata(): SlotMetadata {
151
+ return { ...this.config.semantic };
152
+ }
153
+
72
154
  loader(loaderFn: Loader<TLoaderData>): this {
73
155
  this.config.loader = loaderFn;
74
156
  return this;
@@ -81,7 +163,7 @@ export class ManduFilling<TLoaderData = unknown> {
81
163
  if (!this.config.loader) {
82
164
  return undefined;
83
165
  }
84
- const { timeout = TIMEOUTS.LOADER_DEFAULT, fallback } = options;
166
+ const { timeout = TIMEOUTS.LOADER_DEFAULT, fallback } = options;
85
167
  try {
86
168
  const loaderPromise = Promise.resolve(this.config.loader(ctx));
87
169
  const timeoutPromise = new Promise<never>((_, reject) => {
@@ -4,7 +4,7 @@
4
4
  * 파일 분석 및 Import 추출
5
5
  */
6
6
 
7
- import { readFile } from "fs/promises";
7
+ import { safeReadFile } from "../utils/safe-io";
8
8
  import { dirname, isAbsolute, relative, resolve } from "path";
9
9
  import { minimatch } from "minimatch";
10
10
  import type {
@@ -263,7 +263,12 @@ export async function analyzeFile(
263
263
  layers: LayerDefinition[],
264
264
  rootDir: string
265
265
  ): Promise<FileAnalysis> {
266
- const content = await readFile(filePath, "utf-8");
266
+ const result = await safeReadFile(filePath);
267
+ if (!result.ok) {
268
+ throw new Error(`파일 분석 실패: ${filePath} - ${result.error.message}`);
269
+ }
270
+
271
+ const content = result.value;
267
272
  const imports = extractImports(content);
268
273
  const layer = resolveFileLayer(filePath, layers, rootDir);
269
274
  const slice = layer ? extractSlice(filePath, layer) : undefined;