@neurcode-ai/governance-runtime 0.1.3 → 0.1.5

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 (49) hide show
  1. package/dist/admission-provenance.d.ts +111 -0
  2. package/dist/admission-provenance.d.ts.map +1 -0
  3. package/dist/admission-provenance.js +735 -0
  4. package/dist/admission-provenance.js.map +1 -0
  5. package/dist/agent-guard-posture.d.ts +40 -0
  6. package/dist/agent-guard-posture.d.ts.map +1 -0
  7. package/dist/agent-guard-posture.js +117 -0
  8. package/dist/agent-guard-posture.js.map +1 -0
  9. package/dist/agent-invocation-observability.d.ts +47 -0
  10. package/dist/agent-invocation-observability.d.ts.map +1 -0
  11. package/dist/agent-invocation-observability.js +229 -0
  12. package/dist/agent-invocation-observability.js.map +1 -0
  13. package/dist/agent-plan.d.ts +119 -0
  14. package/dist/agent-plan.d.ts.map +1 -0
  15. package/dist/agent-plan.js +590 -0
  16. package/dist/agent-plan.js.map +1 -0
  17. package/dist/agent-runtime-adapter.d.ts +69 -0
  18. package/dist/agent-runtime-adapter.d.ts.map +1 -0
  19. package/dist/agent-runtime-adapter.js +274 -0
  20. package/dist/agent-runtime-adapter.js.map +1 -0
  21. package/dist/ai-change-record.d.ts +185 -0
  22. package/dist/ai-change-record.d.ts.map +1 -0
  23. package/dist/ai-change-record.js +580 -0
  24. package/dist/ai-change-record.js.map +1 -0
  25. package/dist/architecture-graph.d.ts +153 -0
  26. package/dist/architecture-graph.d.ts.map +1 -0
  27. package/dist/architecture-graph.js +646 -0
  28. package/dist/architecture-graph.js.map +1 -0
  29. package/dist/architecture-obligations.d.ts +161 -0
  30. package/dist/architecture-obligations.d.ts.map +1 -0
  31. package/dist/architecture-obligations.js +553 -0
  32. package/dist/architecture-obligations.js.map +1 -0
  33. package/dist/index.d.ts +10 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +104 -1
  36. package/dist/index.js.map +1 -1
  37. package/dist/profile.d.ts +159 -0
  38. package/dist/profile.d.ts.map +1 -0
  39. package/dist/profile.js +611 -0
  40. package/dist/profile.js.map +1 -0
  41. package/dist/session.d.ts +428 -0
  42. package/dist/session.d.ts.map +1 -0
  43. package/dist/session.js +2206 -0
  44. package/dist/session.js.map +1 -0
  45. package/package.json +13 -2
  46. package/src/constraints.ts +0 -828
  47. package/src/index.test.ts +0 -502
  48. package/src/index.ts +0 -463
  49. package/tsconfig.json +0 -19
@@ -1,828 +0,0 @@
1
- export type DeterministicConstraintSource = 'intent' | 'policy';
2
-
3
- export interface DeterministicConstraintRule {
4
- id: string;
5
- source: DeterministicConstraintSource;
6
- statement: string;
7
- displayName: string;
8
- pattern: RegExp;
9
- matchToken: string;
10
- provenance?: DeterministicConstraintProvenance;
11
- pathIncludePatterns?: string[];
12
- pathExcludePatterns?: string[];
13
- pathIncludes?: RegExp[];
14
- pathExcludes?: RegExp[];
15
- minMatchesPerFile?: number;
16
- maxMatchesPerFile?: number;
17
- evaluationMode?: 'added_lines' | 'full_file' | 'signature_delta';
18
- evaluationScope?: 'file' | 'repo';
19
- }
20
-
21
- export interface DeterministicConstraintProvenance {
22
- why: string;
23
- evidence: string[];
24
- contributingGraphPaths: string[];
25
- trustBoundaries: string[];
26
- }
27
-
28
- export interface DeterministicConstraintCompilation {
29
- rules: DeterministicConstraintRule[];
30
- unmatchedStatements: string[];
31
- }
32
-
33
- export interface DeterministicConstraintCompilationInput {
34
- intentConstraints?: string;
35
- policyRules?: string[];
36
- }
37
-
38
- interface ConstraintTemplate {
39
- id: string;
40
- displayName: string;
41
- triggerTokens: string[];
42
- pattern: RegExp;
43
- matchToken: string;
44
- }
45
-
46
- interface PathScopeMatch {
47
- includePatterns: string[];
48
- excludePatterns: string[];
49
- }
50
-
51
- const PROHIBITIVE_PATTERN = /\b(no|do not|don't|without|avoid|ban|disallow|never|must not)\b/i;
52
-
53
- const CONSTRAINT_TEMPLATES: ConstraintTemplate[] = [
54
- {
55
- id: 'no_useeffect',
56
- displayName: 'No useEffect',
57
- triggerTokens: ['useeffect'],
58
- pattern: /\buseEffect\s*\(/i,
59
- matchToken: 'useeffect',
60
- },
61
- {
62
- id: 'no_console_log',
63
- displayName: 'No console.log',
64
- triggerTokens: ['console.log', 'console log'],
65
- pattern: /\bconsole\.log\s*\(/i,
66
- matchToken: 'console.log',
67
- },
68
- {
69
- id: 'no_debugger',
70
- displayName: 'No debugger statements',
71
- triggerTokens: ['debugger'],
72
- pattern: /\bdebugger\b/i,
73
- matchToken: 'debugger',
74
- },
75
- {
76
- id: 'no_eval',
77
- displayName: 'No eval usage',
78
- triggerTokens: ['eval'],
79
- pattern: /\beval\s*\(/i,
80
- matchToken: 'eval',
81
- },
82
- {
83
- id: 'no_process_env',
84
- displayName: 'No process.env access',
85
- triggerTokens: ['process.env', 'process env'],
86
- pattern: /\bprocess\.env\b/i,
87
- matchToken: 'process.env',
88
- },
89
- {
90
- id: 'no_any_type',
91
- displayName: 'No any type',
92
- triggerTokens: [' any', 'type any', ': any', '<any>'],
93
- pattern: /(:\s*any\b|<\s*any\s*>|\bArray<any>\b|\bPromise<any>\b)/i,
94
- matchToken: 'any',
95
- },
96
- {
97
- id: 'no_todo_fixme',
98
- displayName: 'No TODO/FIXME markers',
99
- triggerTokens: ['todo', 'fixme'],
100
- pattern: /\b(TODO|FIXME)\b/i,
101
- matchToken: 'todo/fixme',
102
- },
103
- {
104
- id: 'no_network_calls',
105
- displayName: 'No network calls',
106
- triggerTokens: ['network call', 'network calls', 'http call', 'api call', 'external call'],
107
- pattern: /\b(fetch\s*\(|axios\.[a-z]+\s*\(|axios\s*\(|XMLHttpRequest\b|http\.request\s*\(|https\.request\s*\(|got\s*\(|superagent\s*\()/i,
108
- matchToken: 'network-call',
109
- },
110
- {
111
- id: 'no_child_process',
112
- displayName: 'No child_process execution',
113
- triggerTokens: ['child_process', 'shell command', 'exec(', 'spawn('],
114
- pattern: /\b(child_process|exec\s*\(|execFile\s*\(|spawn\s*\(|fork\s*\()/i,
115
- matchToken: 'child_process',
116
- },
117
- {
118
- id: 'no_innerhtml',
119
- displayName: 'No innerHTML / dangerouslySetInnerHTML',
120
- triggerTokens: ['innerhtml', 'dangerouslysetinnerhtml', 'dom injection'],
121
- pattern: /\b(innerHTML|dangerouslySetInnerHTML)\b/i,
122
- matchToken: 'innerHTML',
123
- },
124
- ];
125
-
126
- function splitStatements(raw: string): string[] {
127
- return raw
128
- .split(/[\n;]+/)
129
- .map((part) => part.trim())
130
- .filter(Boolean);
131
- }
132
-
133
- function normalizeStatement(statement: string): string {
134
- return statement
135
- .replace(/\s+/g, ' ')
136
- .trim()
137
- .toLowerCase();
138
- }
139
-
140
- function normalizePathScopeToken(rawValue: string): string {
141
- return rawValue
142
- .trim()
143
- .replace(/^[`'"]+|[`'"]+$/g, '')
144
- .replace(/\\/g, '/')
145
- .replace(/^\.\//, '')
146
- .replace(/\/{2,}/g, '/');
147
- }
148
-
149
- function looksLikePathScope(token: string): boolean {
150
- if (!token) return false;
151
- if (/\s/.test(token)) return false;
152
- if (token.includes('/')) return true;
153
- if (token.includes('*')) return true;
154
- return /\.[a-z0-9]{1,8}$/i.test(token);
155
- }
156
-
157
- function globLikeToRegex(pattern: string): RegExp {
158
- const escaped = pattern
159
- .split('*')
160
- .map((segment) => segment.replace(/[.+^${}()|[\]\\]/g, '\\$&'))
161
- .join('.*');
162
- return new RegExp(`^${escaped}$`, 'i');
163
- }
164
-
165
- function compilePathPatterns(patterns: string[]): RegExp[] {
166
- return patterns
167
- .map((pattern) => {
168
- try {
169
- return globLikeToRegex(pattern);
170
- } catch {
171
- return null;
172
- }
173
- })
174
- .filter((item): item is RegExp => item instanceof RegExp);
175
- }
176
-
177
- function parsePathScopes(statement: string): PathScopeMatch {
178
- const includePatterns = new Set<string>();
179
- const excludePatterns = new Set<string>();
180
-
181
- const includeRegex = /\b(?:in|within|under|inside)\s+[`'"]?([A-Za-z0-9_./*?-]+)[`'"]?/gi;
182
- const excludeRegex = /\b(?:except|excluding|but\s+not\s+in|not\s+in)\s+[`'"]?([A-Za-z0-9_./*?-]+)[`'"]?/gi;
183
-
184
- for (const match of statement.matchAll(includeRegex)) {
185
- const candidate = normalizePathScopeToken(match[1] || '');
186
- if (looksLikePathScope(candidate)) {
187
- includePatterns.add(candidate);
188
- }
189
- }
190
- for (const match of statement.matchAll(excludeRegex)) {
191
- const candidate = normalizePathScopeToken(match[1] || '');
192
- if (looksLikePathScope(candidate)) {
193
- excludePatterns.add(candidate);
194
- }
195
- }
196
-
197
- return {
198
- includePatterns: [...includePatterns],
199
- excludePatterns: [...excludePatterns],
200
- };
201
- }
202
-
203
- function uniqueSorted(values: string[]): string[] {
204
- return [...new Set(values.filter(Boolean))].sort((left, right) => left.localeCompare(right));
205
- }
206
-
207
- function buildProvenance(input: {
208
- why: string;
209
- evidence: string[];
210
- trustBoundaries: string[];
211
- pathScopes: PathScopeMatch;
212
- }): DeterministicConstraintProvenance {
213
- const contributingGraphPaths = input.pathScopes.includePatterns.length > 0
214
- ? uniqueSorted(input.pathScopes.includePatterns)
215
- : ['<repo-scope>'];
216
- return {
217
- why: input.why,
218
- evidence: uniqueSorted(input.evidence),
219
- contributingGraphPaths,
220
- trustBoundaries: uniqueSorted(input.trustBoundaries),
221
- };
222
- }
223
-
224
- function applyPathScopes(
225
- rule: Omit<DeterministicConstraintRule, 'pathIncludePatterns' | 'pathExcludePatterns' | 'pathIncludes' | 'pathExcludes'>,
226
- pathScopes: PathScopeMatch
227
- ): DeterministicConstraintRule {
228
- const includePatterns = [...pathScopes.includePatterns];
229
- const excludePatterns = [...pathScopes.excludePatterns];
230
-
231
- return {
232
- ...rule,
233
- ...(includePatterns.length > 0
234
- ? {
235
- pathIncludePatterns: includePatterns,
236
- pathIncludes: compilePathPatterns(includePatterns),
237
- }
238
- : {}),
239
- ...(excludePatterns.length > 0
240
- ? {
241
- pathExcludePatterns: excludePatterns,
242
- pathExcludes: compilePathPatterns(excludePatterns),
243
- }
244
- : {}),
245
- };
246
- }
247
-
248
- function escapeRegex(value: string): string {
249
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
250
- }
251
-
252
- function parseInvocationLimitRule(
253
- statement: string,
254
- source: DeterministicConstraintSource,
255
- pathScopes: PathScopeMatch
256
- ): DeterministicConstraintRule | null {
257
- const normalized = normalizeStatement(statement);
258
-
259
- const fnPatterns: Array<{
260
- regex: RegExp;
261
- fnIndex: number;
262
- countIndex: number;
263
- comparator: 'max' | 'min' | 'exact';
264
- }> = [
265
- {
266
- regex: /\b([a-z_$][a-z0-9_$]*)\s+(?:function\s+)?(?:should\s+be\s+)?(?:invoked|called)\s+(?:only\s+)?(\d+)\s+times?\b/i,
267
- fnIndex: 1,
268
- countIndex: 2,
269
- comparator: 'max',
270
- },
271
- {
272
- regex: /\b([a-z_$][a-z0-9_$]*)\s+(?:function\s+)?(?:should\s+be\s+)?(?:invoked|called)\s+(?:at\s+most|no\s+more\s+than|maximum)\s+(\d+)\s+times?\b/i,
273
- fnIndex: 1,
274
- countIndex: 2,
275
- comparator: 'max',
276
- },
277
- {
278
- regex: /\b([a-z_$][a-z0-9_$]*)\s+(?:function\s+)?(?:should\s+be\s+)?(?:invoked|called)\s+(?:at\s+least|no\s+less\s+than|minimum)\s+(\d+)\s+times?\b/i,
279
- fnIndex: 1,
280
- countIndex: 2,
281
- comparator: 'min',
282
- },
283
- {
284
- regex: /\b([a-z_$][a-z0-9_$]*)\s+(?:function\s+)?(?:should\s+be\s+)?(?:invoked|called)\s+(?:exactly)\s+(\d+)\s+times?\b/i,
285
- fnIndex: 1,
286
- countIndex: 2,
287
- comparator: 'exact',
288
- },
289
- {
290
- regex: /\bonly\s+(\d+)\s+calls?\s+to\s+([a-z_$][a-z0-9_$]*)\b/i,
291
- fnIndex: 2,
292
- countIndex: 1,
293
- comparator: 'max',
294
- },
295
- {
296
- regex: /\bat\s+most\s+(\d+)\s+calls?\s+to\s+([a-z_$][a-z0-9_$]*)\b/i,
297
- fnIndex: 2,
298
- countIndex: 1,
299
- comparator: 'max',
300
- },
301
- {
302
- regex: /\bat\s+least\s+(\d+)\s+calls?\s+to\s+([a-z_$][a-z0-9_$]*)\b/i,
303
- fnIndex: 2,
304
- countIndex: 1,
305
- comparator: 'min',
306
- },
307
- {
308
- regex: /\bexactly\s+(\d+)\s+calls?\s+to\s+([a-z_$][a-z0-9_$]*)\b/i,
309
- fnIndex: 2,
310
- countIndex: 1,
311
- comparator: 'exact',
312
- },
313
- ];
314
-
315
- let fnName: string | null = null;
316
- let rawLimit: string | null = null;
317
- let comparator: 'max' | 'min' | 'exact' = 'max';
318
-
319
- for (const pattern of fnPatterns) {
320
- const match = normalized.match(pattern.regex);
321
- if (!match) {
322
- continue;
323
- }
324
- fnName = match[pattern.fnIndex] || null;
325
- rawLimit = match[pattern.countIndex] || null;
326
- comparator = pattern.comparator;
327
- break;
328
- }
329
-
330
- if (!fnName || !rawLimit) {
331
- return null;
332
- }
333
- const limit = Number(rawLimit);
334
- if (!Number.isFinite(limit) || limit < 0) {
335
- return null;
336
- }
337
-
338
- const repoScopeHint = /\b(across\s+(?:the\s+)?(?:repo|repository|codebase)|globally|in\s+the\s+entire\s+repo)\b/i.test(
339
- normalized
340
- );
341
- const displaySuffix =
342
- comparator === 'exact'
343
- ? `exactly ${limit}`
344
- : comparator === 'min'
345
- ? `at least ${limit}`
346
- : `at most ${limit}`;
347
- const minMatches = comparator === 'min' || comparator === 'exact' ? limit : undefined;
348
- const maxMatches = comparator === 'max' || comparator === 'exact' ? limit : undefined;
349
-
350
- return applyPathScopes(
351
- {
352
- id: `${source}:${comparator}_invocations_${fnName.toLowerCase()}${repoScopeHint ? '_repo' : ''}`,
353
- source,
354
- statement,
355
- displayName: `${fnName}() invocation limit (${displaySuffix}${repoScopeHint ? ', repo-wide' : ''})`,
356
- pattern: new RegExp(`(?<!function\\s)\\b${escapeRegex(fnName)}\\s*\\(`, 'i'),
357
- matchToken: `${fnName}(`,
358
- ...(typeof minMatches === 'number' ? { minMatchesPerFile: minMatches } : {}),
359
- ...(typeof maxMatches === 'number' ? { maxMatchesPerFile: maxMatches } : {}),
360
- evaluationMode: 'full_file',
361
- evaluationScope: repoScopeHint ? 'repo' : 'file',
362
- provenance: buildProvenance({
363
- why: 'Statement imposes deterministic invocation cardinality limits.',
364
- evidence: [fnName, rawLimit, comparator, repoScopeHint ? 'repo-scope' : 'file-scope'],
365
- trustBoundaries: repoScopeHint ? ['cross-module'] : ['local-module'],
366
- pathScopes,
367
- }),
368
- },
369
- pathScopes
370
- );
371
- }
372
-
373
- const EXPORTED_SIGNATURE_PATTERN =
374
- /\bexport\s+(?:async\s+)?function\s+[A-Za-z_$][A-Za-z0-9_$]*\s*\(|\bexport\s+const\s+[A-Za-z_$][A-Za-z0-9_$]*\s*=\s*(?:async\s*)?\([^)]*\)\s*=>|\bexport\s+(?:interface|type)\s+[A-Za-z_$][A-Za-z0-9_$]*/i;
375
-
376
- function parseSignatureDriftRule(
377
- statement: string,
378
- source: DeterministicConstraintSource,
379
- pathScopes: PathScopeMatch
380
- ): DeterministicConstraintRule | null {
381
- const normalized = normalizeStatement(statement);
382
- const mentionsSignature =
383
- /\b(signature|contract|public api|api surface|exported api)\b/i.test(normalized);
384
- const mentionsChange =
385
- /\b(change|changed|modify|modified|drift|mutation|alter)\b/i.test(normalized);
386
- const prohibitive =
387
- PROHIBITIVE_PATTERN.test(normalized)
388
- || /\bkeep\b/i.test(normalized)
389
- || /\bpreserve\b/i.test(normalized);
390
-
391
- if (!mentionsSignature || !mentionsChange || !prohibitive) {
392
- return null;
393
- }
394
-
395
- return applyPathScopes(
396
- {
397
- id: `${source}:no_api_signature_drift`,
398
- source,
399
- statement,
400
- displayName: 'No exported API signature drift',
401
- pattern: EXPORTED_SIGNATURE_PATTERN,
402
- matchToken: 'api-signature-drift',
403
- evaluationMode: 'signature_delta',
404
- evaluationScope: 'file',
405
- provenance: buildProvenance({
406
- why: 'Statement protects external API signature stability.',
407
- evidence: ['signature', 'public api', 'change/modify'],
408
- trustBoundaries: ['public-api', 'external-consumer'],
409
- pathScopes,
410
- }),
411
- },
412
- pathScopes
413
- );
414
- }
415
-
416
- function parseBackwardCompatibilityRule(
417
- statement: string,
418
- source: DeterministicConstraintSource,
419
- pathScopes: PathScopeMatch
420
- ): DeterministicConstraintRule | null {
421
- const normalized = normalizeStatement(statement);
422
- const mentionsCompatibility =
423
- /\b(backward compatibility|backwards compatibility|breaking change|non-breaking|existing consumers?)\b/i.test(
424
- normalized
425
- );
426
-
427
- if (!mentionsCompatibility) {
428
- return null;
429
- }
430
-
431
- return applyPathScopes(
432
- {
433
- id: `${source}:backward_compatibility`,
434
- source,
435
- statement,
436
- displayName: 'Backward compatibility guard (public contract drift)',
437
- pattern: EXPORTED_SIGNATURE_PATTERN,
438
- matchToken: 'backward-compatibility',
439
- evaluationMode: 'signature_delta',
440
- evaluationScope: 'file',
441
- provenance: buildProvenance({
442
- why: 'Statement requires non-breaking compatibility guarantees for existing consumers.',
443
- evidence: ['backward compatibility', 'breaking change', 'existing consumers'],
444
- trustBoundaries: ['public-api', 'external-consumer'],
445
- pathScopes,
446
- }),
447
- },
448
- pathScopes
449
- );
450
- }
451
-
452
- function parseAsyncOrderingRule(
453
- statement: string,
454
- source: DeterministicConstraintSource,
455
- pathScopes: PathScopeMatch
456
- ): DeterministicConstraintRule | null {
457
- const normalized = normalizeStatement(statement);
458
- const mentionsOrdering =
459
- /\b(async ordering|message ordering|out of order|preserve order|ordered workflow|ordering guarantees?|fifo)\b/i.test(
460
- normalized
461
- );
462
- const mentionsParallelRisk =
463
- /\b(parallel|promise\.all|allsettled|race|fan-?out|parallelize)\b/i.test(normalized);
464
-
465
- if (!mentionsOrdering && !mentionsParallelRisk) {
466
- return null;
467
- }
468
-
469
- return applyPathScopes(
470
- {
471
- id: `${source}:async_ordering`,
472
- source,
473
- statement,
474
- displayName: 'Async ordering guard (parallelization risk)',
475
- pattern: /\bPromise\.(?:all|allSettled|race|any)\s*\(|\bp-?map\s*\(|\bparallel(?:ize|Map)?\s*\(/i,
476
- matchToken: 'async-ordering',
477
- evaluationMode: 'added_lines',
478
- evaluationScope: 'file',
479
- provenance: buildProvenance({
480
- why: 'Statement flags async ordering sensitivity and blocks risky parallel fan-out patterns.',
481
- evidence: ['ordering', 'out of order', 'parallel execution'],
482
- trustBoundaries: ['async-workflow', 'downstream-capacity'],
483
- pathScopes,
484
- }),
485
- },
486
- pathScopes
487
- );
488
- }
489
-
490
- function parseEventSchemaConsistencyRule(
491
- statement: string,
492
- source: DeterministicConstraintSource,
493
- pathScopes: PathScopeMatch
494
- ): DeterministicConstraintRule | null {
495
- const normalized = normalizeStatement(statement);
496
- const mentionsEventSchema =
497
- /\b(event schema|event payload|event contract|subscriber|downstream event|schema evolution|required fields?)\b/i.test(
498
- normalized
499
- );
500
-
501
- if (!mentionsEventSchema) {
502
- return null;
503
- }
504
-
505
- return applyPathScopes(
506
- {
507
- id: `${source}:event_schema_consistency`,
508
- source,
509
- statement,
510
- displayName: 'Event/schema consistency guard',
511
- pattern:
512
- /\b(?:interface|type)\s+[A-Za-z_$][A-Za-z0-9_$]*(?:Event|Payload|Message|Envelope)\b|\bevent[A-Za-z0-9_$]*\s*:\s*|\bschemaVersion\b/i,
513
- matchToken: 'event-schema-drift',
514
- evaluationMode: 'signature_delta',
515
- evaluationScope: 'file',
516
- provenance: buildProvenance({
517
- why: 'Statement requires event contract continuity for downstream consumers.',
518
- evidence: ['event schema', 'subscriber', 'required fields'],
519
- trustBoundaries: ['event-bus', 'external-subscriber'],
520
- pathScopes,
521
- }),
522
- },
523
- pathScopes
524
- );
525
- }
526
-
527
- function parseMultiTenantIsolationRule(
528
- statement: string,
529
- source: DeterministicConstraintSource,
530
- pathScopes: PathScopeMatch
531
- ): DeterministicConstraintRule | null {
532
- const normalized = normalizeStatement(statement);
533
- const mentionsTenantIsolation =
534
- /\b(multi-tenant|tenant isolation|cross-tenant|tenant boundaries?|tenant_id|tenant guard)\b/i.test(normalized);
535
-
536
- if (!mentionsTenantIsolation) {
537
- return null;
538
- }
539
-
540
- return applyPathScopes(
541
- {
542
- id: `${source}:multi_tenant_isolation`,
543
- source,
544
- statement,
545
- displayName: 'Multi-tenant isolation guard',
546
- pattern:
547
- /\b(?:bypassTenant(?:Guard|Scope)?|ignoreTenant(?:Scope)?|crossTenant|allTenants|tenantScope\s*:\s*false|setTenantContext\s*\(\s*null\s*\)|withoutTenantScope)\b/i,
548
- matchToken: 'tenant-isolation',
549
- evaluationMode: 'full_file',
550
- evaluationScope: 'file',
551
- provenance: buildProvenance({
552
- why: 'Statement enforces tenant boundary safety and isolation constraints.',
553
- evidence: ['multi-tenant', 'tenant_id', 'cross-tenant'],
554
- trustBoundaries: ['tenant-boundary', 'data-access-layer'],
555
- pathScopes,
556
- }),
557
- },
558
- pathScopes
559
- );
560
- }
561
-
562
- function parseCacheInvariantRule(
563
- statement: string,
564
- source: DeterministicConstraintSource,
565
- pathScopes: PathScopeMatch
566
- ): DeterministicConstraintRule | null {
567
- const normalized = normalizeStatement(statement);
568
- const mentionsCache = /\b(cache invalidation|cache invariant|cache keys?|cache consistency|evict cache|clear(?:ing)? (?:shared )?cache|shared cache|invalidate .*cache)\b/i.test(
569
- normalized
570
- );
571
-
572
- if (!mentionsCache) {
573
- return null;
574
- }
575
-
576
- return applyPathScopes(
577
- {
578
- id: `${source}:cache_invariant`,
579
- source,
580
- statement,
581
- displayName: 'Cache invariant guard (global invalidation risk)',
582
- pattern:
583
- /\b(?:cache\.(?:clear|reset)\s*\(\s*\)|invalidateAll\s*\(|flushAll\s*\(|redis\.flush(?:all|db)\s*\(|\bFLUSH(?:ALL|DB)\b)\b/i,
584
- matchToken: 'cache-invariant',
585
- evaluationMode: 'added_lines',
586
- evaluationScope: 'file',
587
- provenance: buildProvenance({
588
- why: 'Statement marks cache behavior as operationally sensitive and blocks global flush patterns.',
589
- evidence: ['cache invalidation', 'cache key', 'clear cache'],
590
- trustBoundaries: ['cache-layer', 'operational-safety'],
591
- pathScopes,
592
- }),
593
- },
594
- pathScopes
595
- );
596
- }
597
-
598
- function parseIdempotencyRule(
599
- statement: string,
600
- source: DeterministicConstraintSource,
601
- pathScopes: PathScopeMatch
602
- ): DeterministicConstraintRule | null {
603
- const normalized = normalizeStatement(statement);
604
- const mentionsIdempotency =
605
- /\b(idempotency|idempotent|retryable write|retries|exactly once|at-least-once)\b/i.test(normalized);
606
-
607
- if (!mentionsIdempotency) {
608
- return null;
609
- }
610
-
611
- return applyPathScopes(
612
- {
613
- id: `${source}:idempotency`,
614
- source,
615
- statement,
616
- displayName: 'Idempotency expectation guard',
617
- pattern: /\b(?:idempotency[-_ ]key|x-idempotency-key|idempotencyKey|dedupe(?:Key|Id)?)\b/i,
618
- matchToken: 'idempotency-key',
619
- minMatchesPerFile: 1,
620
- evaluationMode: 'full_file',
621
- evaluationScope: 'repo',
622
- provenance: buildProvenance({
623
- why: 'Statement requires deterministic retry safety via idempotency markers.',
624
- evidence: ['idempotency', 'retryable write', 'exactly once'],
625
- trustBoundaries: ['api-edge', 'write-path'],
626
- pathScopes,
627
- }),
628
- },
629
- pathScopes
630
- );
631
- }
632
-
633
- function parseMigrationSafetyRule(
634
- statement: string,
635
- source: DeterministicConstraintSource,
636
- pathScopes: PathScopeMatch
637
- ): DeterministicConstraintRule | null {
638
- const normalized = normalizeStatement(statement);
639
- const mentionsMigration =
640
- /\b(migration safety|schema migration|destructive migration|drop column|drop table|truncate table|backfill safety)\b/i.test(
641
- normalized
642
- );
643
-
644
- if (!mentionsMigration) {
645
- return null;
646
- }
647
-
648
- return applyPathScopes(
649
- {
650
- id: `${source}:migration_safety`,
651
- source,
652
- statement,
653
- displayName: 'Migration safety guard (destructive operation risk)',
654
- pattern:
655
- /\b(?:DROP\s+COLUMN|DROP\s+TABLE|TRUNCATE\s+TABLE|ALTER\s+TABLE\s+[A-Za-z0-9_`".]+\s+DROP\s+COLUMN|DELETE\s+FROM\s+[A-Za-z0-9_`".]+\s*;)\b/i,
656
- matchToken: 'destructive-migration',
657
- evaluationMode: 'added_lines',
658
- evaluationScope: 'file',
659
- provenance: buildProvenance({
660
- why: 'Statement requires non-destructive migration behavior for production safety.',
661
- evidence: ['migration safety', 'drop column', 'truncate table'],
662
- trustBoundaries: ['data-store', 'migration-pipeline'],
663
- pathScopes,
664
- }),
665
- },
666
- pathScopes
667
- );
668
- }
669
-
670
- function statementMatchesTemplate(normalizedStatement: string, template: ConstraintTemplate): boolean {
671
- return template.triggerTokens.some((token) => normalizedStatement.includes(token));
672
- }
673
-
674
- function createRule(
675
- template: ConstraintTemplate,
676
- source: DeterministicConstraintSource,
677
- statement: string,
678
- pathScopes: PathScopeMatch
679
- ): DeterministicConstraintRule {
680
- return applyPathScopes(
681
- {
682
- id: `${source}:${template.id}`,
683
- source,
684
- statement,
685
- displayName: template.displayName,
686
- pattern: template.pattern,
687
- matchToken: template.matchToken,
688
- provenance: buildProvenance({
689
- why: 'Statement matched deterministic constraint template.',
690
- evidence: template.triggerTokens,
691
- trustBoundaries: ['code-hygiene'],
692
- pathScopes,
693
- }),
694
- },
695
- pathScopes
696
- );
697
- }
698
-
699
- function compileStatements(
700
- statements: string[],
701
- source: DeterministicConstraintSource
702
- ): DeterministicConstraintCompilation {
703
- const rules: DeterministicConstraintRule[] = [];
704
- const unmatchedStatements: string[] = [];
705
-
706
- for (const rawStatement of statements) {
707
- const normalized = normalizeStatement(rawStatement);
708
- if (!normalized) {
709
- continue;
710
- }
711
- const pathScopes = parsePathScopes(rawStatement);
712
- const invocationLimitRule = parseInvocationLimitRule(rawStatement, source, pathScopes);
713
- if (invocationLimitRule) {
714
- rules.push(invocationLimitRule);
715
- continue;
716
- }
717
-
718
- const signatureDriftRule = parseSignatureDriftRule(rawStatement, source, pathScopes);
719
- if (signatureDriftRule) {
720
- rules.push(signatureDriftRule);
721
- continue;
722
- }
723
-
724
- const backwardCompatibilityRule = parseBackwardCompatibilityRule(rawStatement, source, pathScopes);
725
- if (backwardCompatibilityRule) {
726
- rules.push(backwardCompatibilityRule);
727
- continue;
728
- }
729
-
730
- const asyncOrderingRule = parseAsyncOrderingRule(rawStatement, source, pathScopes);
731
- if (asyncOrderingRule) {
732
- rules.push(asyncOrderingRule);
733
- continue;
734
- }
735
-
736
- const eventSchemaRule = parseEventSchemaConsistencyRule(rawStatement, source, pathScopes);
737
- if (eventSchemaRule) {
738
- rules.push(eventSchemaRule);
739
- continue;
740
- }
741
-
742
- const multiTenantRule = parseMultiTenantIsolationRule(rawStatement, source, pathScopes);
743
- if (multiTenantRule) {
744
- rules.push(multiTenantRule);
745
- continue;
746
- }
747
-
748
- const cacheInvariantRule = parseCacheInvariantRule(rawStatement, source, pathScopes);
749
- if (cacheInvariantRule) {
750
- rules.push(cacheInvariantRule);
751
- continue;
752
- }
753
-
754
- const idempotencyRule = parseIdempotencyRule(rawStatement, source, pathScopes);
755
- if (idempotencyRule) {
756
- rules.push(idempotencyRule);
757
- continue;
758
- }
759
-
760
- const migrationSafetyRule = parseMigrationSafetyRule(rawStatement, source, pathScopes);
761
- if (migrationSafetyRule) {
762
- rules.push(migrationSafetyRule);
763
- continue;
764
- }
765
-
766
- const requiresProhibitiveLanguage = source === 'intent';
767
- if (requiresProhibitiveLanguage && !PROHIBITIVE_PATTERN.test(normalized)) {
768
- continue;
769
- }
770
-
771
- const matches = CONSTRAINT_TEMPLATES.filter((template) => statementMatchesTemplate(normalized, template));
772
- if (matches.length === 0) {
773
- unmatchedStatements.push(rawStatement);
774
- continue;
775
- }
776
-
777
- for (const match of matches) {
778
- rules.push(createRule(match, source, rawStatement, pathScopes));
779
- }
780
- }
781
-
782
- return {
783
- rules,
784
- unmatchedStatements,
785
- };
786
- }
787
-
788
- function dedupeRules(rules: DeterministicConstraintRule[]): DeterministicConstraintRule[] {
789
- const seen = new Set<string>();
790
- const deduped: DeterministicConstraintRule[] = [];
791
-
792
- for (const rule of rules) {
793
- const key = [
794
- rule.id,
795
- rule.statement.toLowerCase(),
796
- rule.pathIncludePatterns?.join('|') || '',
797
- rule.pathExcludePatterns?.join('|') || '',
798
- typeof rule.minMatchesPerFile === 'number' ? String(rule.minMatchesPerFile) : '',
799
- typeof rule.maxMatchesPerFile === 'number' ? String(rule.maxMatchesPerFile) : '',
800
- rule.evaluationMode || '',
801
- rule.evaluationScope || '',
802
- ].join('::');
803
- if (seen.has(key)) {
804
- continue;
805
- }
806
- seen.add(key);
807
- deduped.push(rule);
808
- }
809
-
810
- return deduped;
811
- }
812
-
813
- export function compileDeterministicConstraints(
814
- input: DeterministicConstraintCompilationInput
815
- ): DeterministicConstraintCompilation {
816
- const intentStatements = splitStatements(input.intentConstraints || '');
817
- const policyStatements = (input.policyRules || [])
818
- .map((rule) => String(rule || '').trim())
819
- .filter(Boolean);
820
-
821
- const compiledIntent = compileStatements(intentStatements, 'intent');
822
- const compiledPolicy = compileStatements(policyStatements, 'policy');
823
-
824
- return {
825
- rules: dedupeRules([...compiledIntent.rules, ...compiledPolicy.rules]),
826
- unmatchedStatements: [...compiledIntent.unmatchedStatements, ...compiledPolicy.unmatchedStatements],
827
- };
828
- }