@ryuenn3123/agentic-senior-core 2.5.13 → 2.5.15

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.
@@ -17,6 +17,8 @@ Use these checklists:
17
17
  4. Treat scope-style findings as advisory unless they hide factual errors, contract mismatches, or non-negotiable violations.
18
18
  5. Enforce documentation hard blockers on changed boundaries: public surface changes, API contract changes, and database structure changes must include synchronized documentation updates.
19
19
  6. Enforce context-triggered strict audits: review requests, PR-intent workflows, and major feature completion must run strict security and performance audits; small edits stay lightweight unless strict mode is explicitly forced.
20
+ 7. Enforce cross-session consistency guardian: session handoff must include active architecture contract summary, drift detection must warn before direction changes, and direction changes require explicit user confirmation.
21
+ 8. Enforce explain-on-demand state visibility: default responses must avoid unnecessary state-file internals, state internals are exposed only on explicit request, and diagnostic mode must explain relevant state decisions when needed.
20
22
 
21
23
  For EVERY violation found:
22
24
  - State the exact file and line
@@ -38,12 +38,22 @@ Release recommendation:
38
38
  - [ ] Breakpoint transitions preserve hierarchy, spacing rhythm, and action clarity.
39
39
  - [ ] Navigation and key CTA remain explicit across viewport sizes.
40
40
 
41
- ## 6. UX Narrative and Conversion Clarity
41
+ ## 6. Language and Content Consistency
42
+ - [ ] Content language is consistent across headline, body, CTA, and system messages for the same screen flow.
43
+ - [ ] Mixed-language output appears only when requested by user or product requirement.
44
+ - [ ] Terminology stays stable for repeated actions and labels.
45
+
46
+ ## 7. Text Contrast and Collision Safety
47
+ - [ ] Text-to-background contrast is checked for every semantic token pair used in UI.
48
+ - [ ] No text color clashes with gradients, images, or accent surfaces.
49
+ - [ ] Primary and secondary text remain readable in all supported breakpoints.
50
+
51
+ ## 8. UX Narrative and Conversion Clarity
42
52
  - [ ] First viewport communicates value proposition and primary action immediately.
43
53
  - [ ] Error, empty, and loading states provide clear next actions.
44
54
  - [ ] User journey avoids dead ends and hidden critical actions.
45
55
 
46
- ## 7. Template Diversity and Originality
56
+ ## 9. Template Diversity and Originality
47
57
  - [ ] Output is not a copy of a generic starter template or repeated AI layout pattern.
48
58
  - [ ] Layout composition shows intentional variation in structure and hierarchy.
49
59
  - [ ] Visual intent, interaction quality, and conversion clarity are all explicitly reviewed together.
@@ -6,6 +6,7 @@ Run this checklist before claiming frontend work is production-ready.
6
6
  - [ ] Typography scale is consistent and tokenized.
7
7
  - [ ] Color usage follows design tokens and avoids ad-hoc values.
8
8
  - [ ] Spacing and layout rhythm is coherent across pages.
9
+ - [ ] Language and terminology stay consistent across headline, body, and CTA for the same flow.
9
10
 
10
11
  ## 2. Responsiveness
11
12
  - [ ] Core pages are usable at mobile, tablet, and desktop breakpoints.
@@ -114,3 +114,13 @@ VERDICT: PASS / FAIL (X/Y items passed)
114
114
  - [ ] Strict audit mode activates automatically on review and PR-intent workflows
115
115
  - [ ] Small edits avoid heavy checks by default unless strict mode is explicitly requested
116
116
  - [ ] User can always force strict audit mode manually
117
+
118
+ ### 12. Rules as Guardian (Cross-Session Consistency)
119
+ - [ ] Session handoff includes active architecture contract summary
120
+ - [ ] Drift detection warns before direction changes
121
+ - [ ] Direction changes require explicit user confirmation
122
+
123
+ ### 13. Invisible State Management (Explain-on-Demand)
124
+ - [ ] Default responses avoid unnecessary state-file internals
125
+ - [ ] State internals are exposed only on explicit request
126
+ - [ ] Diagnostic mode can explain relevant state decisions when needed
@@ -13,6 +13,25 @@ These principles are mandatory for backend and shared core modules.
13
13
 
14
14
  If a short and a clear implementation are functionally equivalent, choose the clear implementation.
15
15
 
16
+ ## Rules as Guardian (Cross-Session Consistency)
17
+
18
+ These guardrails are mandatory to preserve architecture direction across sessions.
19
+
20
+ - Session handoff must include active architecture contract summary.
21
+ - Contract summary must include declared stack, blueprint, profile, and active core patterns.
22
+ - Detect drift before changing declared stack or core patterns.
23
+ - Direction changes require explicit user confirmation before applying changes.
24
+ - When confirmation is provided, record the rationale in session notes or PR context.
25
+
26
+ ## Invisible State Management with Explain-on-Demand
27
+
28
+ State internals must stay invisible by default.
29
+
30
+ - Default responses must avoid unnecessary state-file internals.
31
+ - State internals are exposed only on explicit user request.
32
+ - Diagnostic mode explains relevant state decisions when needed.
33
+ - Keep default explanations concise and outcome-first; show raw state details only in diagnostic mode.
34
+
16
35
  ## The Core Principle
17
36
 
18
37
  **Every layer has ONE job. Layer leaks are bugs — not "pragmatic shortcuts."**
@@ -16,6 +16,13 @@ Mandatory behavior when triggered:
16
16
  - score and review generated UI work against visual intent, interaction quality, and conversion clarity
17
17
  - reject template-only repetitive outputs and force a distinct layout direction
18
18
 
19
+ ## UI Consistency Guardrails (Mandatory)
20
+
21
+ - Content language must stay consistent per screen and flow unless user requests multilingual output.
22
+ - Text color must remain contrast-safe against its background; no color collisions.
23
+ - Layout must avoid overlap, clipped text, and misaligned key actions across breakpoints.
24
+ - Keep spacing and positioning token-driven so repeated outputs stay stable.
25
+
19
26
  ## 1. File Structure (Feature-Driven Design)
20
27
  Organize your application by feature domain, not by file type.
21
28
  - **BANNED:** Monolithic directories like `/components` (with 500 files), `/hooks`, `/api`.
@@ -1,5 +1,5 @@
1
1
  {
2
- "generatedAt": "2026-04-17T11:50:07.714Z",
2
+ "generatedAt": "2026-04-17T13:21:48.914Z",
3
3
  "reportName": "memory-continuity-benchmark",
4
4
  "schemaVersion": "1.0.0",
5
5
  "passed": true,
package/.cursorrules CHANGED
@@ -1,6 +1,6 @@
1
1
  # AGENTIC-SENIOR-CORE DYNAMIC GOVERNANCE RULESET
2
2
 
3
- Generated by Agentic-Senior-Core CLI v2.5.13
3
+ Generated by Agentic-Senior-Core CLI v2.5.15
4
4
  Timestamp: 2026-04-15T00:14:51.184Z
5
5
  Selected profile: beginner
6
6
  Selected policy file: .agent-context/policies/llm-judge-threshold.json
package/.windsurfrules CHANGED
@@ -1,6 +1,6 @@
1
1
  # AGENTIC-SENIOR-CORE DYNAMIC GOVERNANCE RULESET
2
2
 
3
- Generated by Agentic-Senior-Core CLI v2.5.13
3
+ Generated by Agentic-Senior-Core CLI v2.5.15
4
4
  Timestamp: 2026-04-15T00:14:51.184Z
5
5
  Selected profile: beginner
6
6
  Selected policy file: .agent-context/policies/llm-judge-threshold.json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryuenn3123/agentic-senior-core",
3
- "version": "2.5.13",
3
+ "version": "2.5.15",
4
4
  "type": "module",
5
5
  "description": "Force your AI Agent to code like a Staff Engineer, not a Junior.",
6
6
  "bin": {
@@ -45,6 +45,8 @@
45
45
  "audit:frontend-usability": "node ./scripts/frontend-usability-audit.mjs",
46
46
  "audit:documentation-boundary": "node ./scripts/documentation-boundary-audit.mjs",
47
47
  "audit:context-triggered": "node ./scripts/context-triggered-audit.mjs",
48
+ "audit:rules-guardian": "node ./scripts/rules-guardian-audit.mjs",
49
+ "audit:explain-on-demand": "node ./scripts/explain-on-demand-audit.mjs",
48
50
  "gate:release": "node ./scripts/release-gate.mjs && node ./scripts/forbidden-content-check.mjs",
49
51
  "prepublishOnly": "npm run gate:release",
50
52
  "sbom:generate": "node ./scripts/generate-sbom.mjs",
@@ -0,0 +1,426 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * explain-on-demand-audit.mjs
5
+ *
6
+ * Enforces invisible state management defaults and explicit diagnostic
7
+ * visibility rules for state internals.
8
+ */
9
+
10
+ import { existsSync, readFileSync } from 'node:fs';
11
+ import { execFileSync } from 'node:child_process';
12
+ import { dirname, resolve } from 'node:path';
13
+ import { fileURLToPath } from 'node:url';
14
+
15
+ const __filename = fileURLToPath(import.meta.url);
16
+ const __dirname = dirname(__filename);
17
+ const REPOSITORY_ROOT = resolve(__dirname, '..');
18
+
19
+ const ONBOARDING_REPORT_PATH = '.agent-context/state/onboarding-report.json';
20
+ const ARCHITECTURE_RULE_PATH = '.agent-context/rules/architecture.md';
21
+ const PR_CHECKLIST_PATH = '.agent-context/review-checklists/pr-checklist.md';
22
+ const REVIEW_PROMPT_PATH = '.agent-context/prompts/review-code.md';
23
+
24
+ const DEFAULT_MODE = 'default';
25
+ const SUPPORTED_MODES = new Set([DEFAULT_MODE, 'diagnostic']);
26
+ const DEFAULT_WORKFLOW = 'standard';
27
+
28
+ const REQUIRED_ARCHITECTURE_RULE_SNIPPETS = [
29
+ '## Invisible State Management with Explain-on-Demand',
30
+ 'Default responses must avoid unnecessary state-file internals.',
31
+ 'State internals are exposed only on explicit user request.',
32
+ 'Diagnostic mode explains relevant state decisions when needed.',
33
+ ];
34
+
35
+ const REQUIRED_PR_CHECKLIST_SNIPPETS = [
36
+ 'Default responses avoid unnecessary state-file internals',
37
+ 'State internals are exposed only on explicit request',
38
+ 'Diagnostic mode can explain relevant state decisions when needed',
39
+ ];
40
+
41
+ const REQUIRED_REVIEW_PROMPT_SNIPPETS = [
42
+ 'Enforce explain-on-demand state visibility: default responses must avoid unnecessary state-file internals, state internals are exposed only on explicit request, and diagnostic mode must explain relevant state decisions when needed.',
43
+ ];
44
+
45
+ const INTERNAL_STATE_SIGNAL_PATTERNS = [
46
+ /\.agent-context\/state\//i,
47
+ /\bautoDetection\b/i,
48
+ /\brankedCandidates\b/i,
49
+ /\bconfidenceGap\b/i,
50
+ /\bschemaVersion\b/i,
51
+ ];
52
+
53
+ function pushResult(results, isPassed, checkName, details) {
54
+ results.push({
55
+ checkName,
56
+ passed: isPassed,
57
+ details,
58
+ });
59
+ }
60
+
61
+ function normalizeFilePath(filePath) {
62
+ return filePath.replace(/\\/g, '/').replace(/^\.\//, '');
63
+ }
64
+
65
+ function parseGitFileList(rawOutput) {
66
+ if (typeof rawOutput !== 'string' || rawOutput.trim().length === 0) {
67
+ return [];
68
+ }
69
+
70
+ return rawOutput
71
+ .split(/\r?\n/)
72
+ .map((filePath) => filePath.trim())
73
+ .filter((filePath) => filePath.length > 0)
74
+ .map(normalizeFilePath);
75
+ }
76
+
77
+ function runGitFileQuery(commandArguments) {
78
+ try {
79
+ const rawOutput = execFileSync('git', commandArguments, {
80
+ cwd: REPOSITORY_ROOT,
81
+ encoding: 'utf8',
82
+ maxBuffer: 1024 * 1024,
83
+ });
84
+
85
+ return parseGitFileList(rawOutput);
86
+ } catch {
87
+ return [];
88
+ }
89
+ }
90
+
91
+ function uniqueSorted(filePaths) {
92
+ return Array.from(new Set(filePaths)).sort((leftPath, rightPath) => leftPath.localeCompare(rightPath));
93
+ }
94
+
95
+ function collectChangedFiles() {
96
+ const workingTreeFiles = runGitFileQuery(['diff', '--name-only']);
97
+ const stagedFiles = runGitFileQuery(['diff', '--name-only', '--cached']);
98
+ const workingScopeFiles = uniqueSorted([...workingTreeFiles, ...stagedFiles]);
99
+
100
+ if (workingScopeFiles.length > 0) {
101
+ return {
102
+ source: 'working-tree-and-index',
103
+ files: workingScopeFiles,
104
+ };
105
+ }
106
+
107
+ const latestCommitRangeFiles = runGitFileQuery(['diff', '--name-only', 'HEAD~1..HEAD']);
108
+ if (latestCommitRangeFiles.length > 0) {
109
+ return {
110
+ source: 'latest-commit-range',
111
+ files: uniqueSorted(latestCommitRangeFiles),
112
+ };
113
+ }
114
+
115
+ const headCommitFiles = runGitFileQuery(['show', '--pretty=format:', '--name-only', 'HEAD']);
116
+ if (headCommitFiles.length > 0) {
117
+ return {
118
+ source: 'head-commit',
119
+ files: uniqueSorted(headCommitFiles),
120
+ };
121
+ }
122
+
123
+ return {
124
+ source: 'none',
125
+ files: [],
126
+ };
127
+ }
128
+
129
+ function readText(relativeFilePath) {
130
+ const absolutePath = resolve(REPOSITORY_ROOT, relativeFilePath);
131
+ if (!existsSync(absolutePath)) {
132
+ return '';
133
+ }
134
+
135
+ return readFileSync(absolutePath, 'utf8');
136
+ }
137
+
138
+ function parseOnboardingReport(onboardingReportContent) {
139
+ if (typeof onboardingReportContent !== 'string' || onboardingReportContent.trim().length === 0) {
140
+ return null;
141
+ }
142
+
143
+ try {
144
+ return JSON.parse(onboardingReportContent);
145
+ } catch {
146
+ return null;
147
+ }
148
+ }
149
+
150
+ function parseCliArguments(argumentList) {
151
+ let mode = DEFAULT_MODE;
152
+ let workflow = DEFAULT_WORKFLOW;
153
+ let explicitStateRequest = false;
154
+
155
+ for (let argumentIndex = 0; argumentIndex < argumentList.length; argumentIndex += 1) {
156
+ const argumentValue = argumentList[argumentIndex];
157
+
158
+ if (argumentValue === '--state-debug') {
159
+ explicitStateRequest = true;
160
+ continue;
161
+ }
162
+
163
+ if (argumentValue === '--mode') {
164
+ const nextArgumentValue = argumentList[argumentIndex + 1];
165
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
166
+ mode = nextArgumentValue;
167
+ argumentIndex += 1;
168
+ }
169
+ continue;
170
+ }
171
+
172
+ if (argumentValue.startsWith('--mode=')) {
173
+ mode = argumentValue.slice('--mode='.length);
174
+ continue;
175
+ }
176
+
177
+ if (argumentValue === '--workflow') {
178
+ const nextArgumentValue = argumentList[argumentIndex + 1];
179
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
180
+ workflow = nextArgumentValue;
181
+ argumentIndex += 1;
182
+ }
183
+ continue;
184
+ }
185
+
186
+ if (argumentValue.startsWith('--workflow=')) {
187
+ workflow = argumentValue.slice('--workflow='.length);
188
+ }
189
+ }
190
+
191
+ const normalizedMode = String(mode || '').trim().toLowerCase();
192
+
193
+ return {
194
+ mode: SUPPORTED_MODES.has(normalizedMode) ? normalizedMode : DEFAULT_MODE,
195
+ workflow: String(workflow || '').trim().toLowerCase() || DEFAULT_WORKFLOW,
196
+ explicitStateRequest,
197
+ };
198
+ }
199
+
200
+ function assertSnippetCoverage(sourceLabel, sourcePath, requiredSnippets, failures, results) {
201
+ const sourceContent = readText(sourcePath);
202
+
203
+ if (!sourceContent) {
204
+ failures.push(`Missing ${sourceLabel} source: ${sourcePath}`);
205
+ pushResult(results, false, `${sourceLabel}-source-exists`, `Missing ${sourcePath}`);
206
+ return;
207
+ }
208
+
209
+ pushResult(results, true, `${sourceLabel}-source-exists`, `${sourcePath} is present`);
210
+
211
+ const missingSnippets = requiredSnippets.filter((requiredSnippet) => !sourceContent.includes(requiredSnippet));
212
+
213
+ if (missingSnippets.length > 0) {
214
+ failures.push(`Missing ${sourceLabel} snippets: ${missingSnippets.join(', ')}`);
215
+ pushResult(
216
+ results,
217
+ false,
218
+ `${sourceLabel}-source-coverage`,
219
+ `Missing snippets in ${sourcePath}: ${missingSnippets.join(', ')}`
220
+ );
221
+ return;
222
+ }
223
+
224
+ pushResult(results, true, `${sourceLabel}-source-coverage`, `${sourceLabel} snippets are complete`);
225
+ }
226
+
227
+ function buildDefaultResponseSummary(onboardingReport) {
228
+ const selectedStack = String(onboardingReport?.selectedStack || 'unknown-stack').trim() || 'unknown-stack';
229
+ const selectedBlueprint = String(onboardingReport?.selectedBlueprint || 'unknown-blueprint').trim() || 'unknown-blueprint';
230
+ const selectedProfile = String(onboardingReport?.selectedProfile || 'unknown-profile').trim() || 'unknown-profile';
231
+
232
+ return `Active setup summary: stack=${selectedStack}, blueprint=${selectedBlueprint}, profile=${selectedProfile}.`;
233
+ }
234
+
235
+ function detectInternalSignals(textValue) {
236
+ const normalizedText = String(textValue || '');
237
+ return INTERNAL_STATE_SIGNAL_PATTERNS.some((signalPattern) => signalPattern.test(normalizedText));
238
+ }
239
+
240
+ function buildDiagnosticDecisionSummaries(onboardingReport) {
241
+ const explanations = [];
242
+
243
+ const selectedStack = String(onboardingReport?.selectedStack || '').trim();
244
+ if (selectedStack) {
245
+ explanations.push(`Stack decision: selectedStack=${selectedStack}.`);
246
+ }
247
+
248
+ const selectedBlueprint = String(onboardingReport?.selectedBlueprint || '').trim();
249
+ if (selectedBlueprint) {
250
+ explanations.push(`Blueprint decision: selectedBlueprint=${selectedBlueprint}.`);
251
+ }
252
+
253
+ const selectedProfile = String(onboardingReport?.selectedProfile || '').trim();
254
+ if (selectedProfile) {
255
+ explanations.push(`Profile decision: selectedProfile=${selectedProfile}.`);
256
+ }
257
+
258
+ const detectionReasoning = String(onboardingReport?.autoDetection?.detectionReasoning || '').trim();
259
+ if (detectionReasoning) {
260
+ explanations.push(`Detection reasoning: ${detectionReasoning}`);
261
+ }
262
+
263
+ if (typeof onboardingReport?.ciGuardrailsEnabled === 'boolean') {
264
+ explanations.push(`CI guardrails decision: ciGuardrailsEnabled=${String(onboardingReport.ciGuardrailsEnabled)}.`);
265
+ }
266
+
267
+ return explanations;
268
+ }
269
+
270
+ function runAudit() {
271
+ const parsedArguments = parseCliArguments(process.argv.slice(2));
272
+ const changedScope = collectChangedFiles();
273
+ const changedFiles = changedScope.files;
274
+ const results = [];
275
+ const failures = [];
276
+ const warnings = [];
277
+
278
+ pushResult(results, true, 'context-workflow', `workflow=${parsedArguments.workflow}`);
279
+ pushResult(results, true, 'explain-mode', `mode=${parsedArguments.mode}`);
280
+ pushResult(
281
+ results,
282
+ true,
283
+ 'state-debug-explicit-request',
284
+ parsedArguments.explicitStateRequest
285
+ ? 'Explicit state diagnostic request received'
286
+ : 'No explicit state diagnostic request provided'
287
+ );
288
+
289
+ assertSnippetCoverage(
290
+ 'explain-on-demand-architecture-rule',
291
+ ARCHITECTURE_RULE_PATH,
292
+ REQUIRED_ARCHITECTURE_RULE_SNIPPETS,
293
+ failures,
294
+ results
295
+ );
296
+
297
+ assertSnippetCoverage(
298
+ 'explain-on-demand-pr-checklist',
299
+ PR_CHECKLIST_PATH,
300
+ REQUIRED_PR_CHECKLIST_SNIPPETS,
301
+ failures,
302
+ results
303
+ );
304
+
305
+ assertSnippetCoverage(
306
+ 'explain-on-demand-review-prompt',
307
+ REVIEW_PROMPT_PATH,
308
+ REQUIRED_REVIEW_PROMPT_SNIPPETS,
309
+ failures,
310
+ results
311
+ );
312
+
313
+ const onboardingReportContent = readText(ONBOARDING_REPORT_PATH);
314
+ const onboardingReport = parseOnboardingReport(onboardingReportContent);
315
+
316
+ if (!onboardingReportContent) {
317
+ failures.push(`Missing state source: ${ONBOARDING_REPORT_PATH}`);
318
+ pushResult(results, false, 'state-source', `Missing ${ONBOARDING_REPORT_PATH}`);
319
+ } else if (!onboardingReport) {
320
+ failures.push(`Invalid state source JSON: ${ONBOARDING_REPORT_PATH}`);
321
+ pushResult(results, false, 'state-source', `Cannot parse ${ONBOARDING_REPORT_PATH}`);
322
+ } else {
323
+ pushResult(results, true, 'state-source', `${ONBOARDING_REPORT_PATH} is present and valid`);
324
+ }
325
+
326
+ const defaultResponseSummary = buildDefaultResponseSummary(onboardingReport);
327
+ const defaultModeExposesStateInternals = detectInternalSignals(defaultResponseSummary);
328
+
329
+ if (defaultModeExposesStateInternals) {
330
+ failures.push('Default response exposes state internals');
331
+ pushResult(
332
+ results,
333
+ false,
334
+ 'default-response-invisible-state',
335
+ 'Default response leaks internal state details'
336
+ );
337
+ } else {
338
+ pushResult(
339
+ results,
340
+ true,
341
+ 'default-response-invisible-state',
342
+ 'Default response avoids unnecessary state-file internals'
343
+ );
344
+ }
345
+
346
+ const diagnosticDecisionSummaries = buildDiagnosticDecisionSummaries(onboardingReport);
347
+ const canExplainStateDecisions = diagnosticDecisionSummaries.length > 0;
348
+
349
+ if (parsedArguments.mode === 'diagnostic' && !parsedArguments.explicitStateRequest) {
350
+ failures.push('Diagnostic mode requested without explicit state request');
351
+ pushResult(
352
+ results,
353
+ false,
354
+ 'diagnostic-explicit-request-gate',
355
+ 'Diagnostic mode requires explicit request. Re-run with --state-debug when user asks for state-level details.'
356
+ );
357
+ } else {
358
+ pushResult(
359
+ results,
360
+ true,
361
+ 'diagnostic-explicit-request-gate',
362
+ parsedArguments.mode === 'diagnostic'
363
+ ? 'Diagnostic mode is explicitly requested and permitted'
364
+ : 'Diagnostic mode not requested; default hidden-state behavior applies'
365
+ );
366
+ }
367
+
368
+ if (!canExplainStateDecisions) {
369
+ failures.push('Diagnostic mode cannot explain relevant state decisions');
370
+ pushResult(
371
+ results,
372
+ false,
373
+ 'diagnostic-explain-state-decisions',
374
+ 'No state decision explanations available'
375
+ );
376
+ } else {
377
+ pushResult(
378
+ results,
379
+ true,
380
+ 'diagnostic-explain-state-decisions',
381
+ `Diagnostic mode can explain ${diagnosticDecisionSummaries.length} state decision points`
382
+ );
383
+ }
384
+
385
+ if (parsedArguments.mode === 'default' && parsedArguments.explicitStateRequest) {
386
+ warnings.push('Explicit state request was provided in default mode; internal state details remain hidden unless diagnostic mode is selected.');
387
+ }
388
+
389
+ const reportPayload = {
390
+ generatedAt: new Date().toISOString(),
391
+ auditName: 'explain-on-demand-audit',
392
+ workflow: parsedArguments.workflow,
393
+ mode: parsedArguments.mode,
394
+ source: changedScope.source,
395
+ changedFileCount: changedFiles.length,
396
+ changedFiles,
397
+ responsePolicy: {
398
+ defaultModeExposesStateInternals,
399
+ diagnosticRequiresExplicitRequest: true,
400
+ explicitStateRequestReceived: parsedArguments.explicitStateRequest,
401
+ },
402
+ defaultResponse: {
403
+ summary: defaultResponseSummary,
404
+ containsStateInternals: defaultModeExposesStateInternals,
405
+ },
406
+ diagnosticMode: {
407
+ requested: parsedArguments.mode === 'diagnostic',
408
+ allowed: parsedArguments.mode !== 'diagnostic' || parsedArguments.explicitStateRequest,
409
+ canExplainStateDecisions,
410
+ stateDecisionExplanations: parsedArguments.mode === 'diagnostic' && parsedArguments.explicitStateRequest
411
+ ? diagnosticDecisionSummaries
412
+ : [],
413
+ availableDecisionCount: diagnosticDecisionSummaries.length,
414
+ },
415
+ passed: failures.length === 0,
416
+ failureCount: failures.length,
417
+ failures,
418
+ warnings,
419
+ results,
420
+ };
421
+
422
+ console.log(JSON.stringify(reportPayload, null, 2));
423
+ process.exit(reportPayload.passed ? 0 : 1);
424
+ }
425
+
426
+ runAudit();
@@ -44,6 +44,8 @@ const REQUIRED_EXCELLENCE_RUBRIC_SNIPPETS = [
44
44
  'Typography Quality',
45
45
  'Color System Diversity and Contrast',
46
46
  'Interaction Choreography',
47
+ 'Language and Content Consistency',
48
+ 'Text Contrast and Collision Safety',
47
49
  'UX Narrative and Conversion Clarity',
48
50
  'Template Diversity and Originality',
49
51
  'Low-Diversity Template Output Policy',
@@ -55,6 +57,9 @@ const REQUIRED_FRONTEND_RULE_SNIPPETS = [
55
57
  'UI scope trigger signals',
56
58
  'visual intent, interaction quality, and conversion clarity',
57
59
  'template-only repetitive outputs',
60
+ 'UI Consistency Guardrails (Mandatory)',
61
+ 'Content language must stay consistent per screen and flow unless user requests multilingual output.',
62
+ 'Text color must remain contrast-safe against its background; no color collisions.',
58
63
  ];
59
64
 
60
65
  function assertFileExists(relativeFilePath, failures) {
@@ -32,6 +32,8 @@ const FRONTEND_EXCELLENCE_RUBRIC_PATH = '.agent-context/review-checklists/fronte
32
32
  const FRONTEND_AUDIT_SCRIPT_PATH = 'scripts/frontend-usability-audit.mjs';
33
33
  const DOCUMENTATION_BOUNDARY_AUDIT_SCRIPT_PATH = 'scripts/documentation-boundary-audit.mjs';
34
34
  const CONTEXT_TRIGGERED_AUDIT_SCRIPT_PATH = 'scripts/context-triggered-audit.mjs';
35
+ const RULES_GUARDIAN_AUDIT_SCRIPT_PATH = 'scripts/rules-guardian-audit.mjs';
36
+ const EXPLAIN_ON_DEMAND_AUDIT_SCRIPT_PATH = 'scripts/explain-on-demand-audit.mjs';
35
37
  const BACKEND_ARCHITECTURE_RULE_PATH = '.agent-context/rules/architecture.md';
36
38
  const BACKEND_REVIEW_CHECKLIST_PATH = '.agent-context/review-checklists/pr-checklist.md';
37
39
  const REFACTOR_PROMPT_PATH = '.agent-context/prompts/refactor.md';
@@ -62,6 +64,8 @@ const REQUIRED_FRONTEND_EXCELLENCE_RUBRIC_SNIPPETS = [
62
64
  'Typography Quality',
63
65
  'Color System Diversity and Contrast',
64
66
  'Interaction Choreography',
67
+ 'Language and Content Consistency',
68
+ 'Text Contrast and Collision Safety',
65
69
  'UX Narrative and Conversion Clarity',
66
70
  'Template Diversity and Originality',
67
71
  'Low-Diversity Template Output Policy',
@@ -431,6 +435,176 @@ function runReleaseGate() {
431
435
  }
432
436
  }
433
437
 
438
+ const rulesGuardianAuditExecution = runMachineReadableScript(
439
+ RULES_GUARDIAN_AUDIT_SCRIPT_PATH,
440
+ ['--workflow', 'pr-preparation']
441
+ );
442
+ if (!rulesGuardianAuditExecution.report) {
443
+ const failureDetails = rulesGuardianAuditExecution.executionErrorMessage
444
+ ? `Rules guardian audit execution failed before producing a machine-readable report: ${rulesGuardianAuditExecution.executionErrorMessage}`
445
+ : 'Rules guardian audit did not produce machine-readable JSON output';
446
+ pushResult(results, false, 'rules-guardian-audit', failureDetails);
447
+ } else {
448
+ diagnostics.rulesGuardianAudit = rulesGuardianAuditExecution.report;
449
+ pushResult(
450
+ results,
451
+ true,
452
+ 'rules-guardian-audit',
453
+ `rules-guardian-audit executed (passed=${rulesGuardianAuditExecution.report.passed}, driftDetected=${rulesGuardianAuditExecution.report?.driftDetection?.driftDetected}, failures=${rulesGuardianAuditExecution.report.failureCount})`
454
+ );
455
+
456
+ const sessionHandoffSummary = rulesGuardianAuditExecution.report?.sessionHandoff?.contractSummary;
457
+ const sessionHandoffIncluded = rulesGuardianAuditExecution.report?.sessionHandoff?.included === true
458
+ && typeof sessionHandoffSummary === 'string'
459
+ && sessionHandoffSummary.trim().length > 0;
460
+
461
+ if (sessionHandoffIncluded) {
462
+ pushResult(
463
+ results,
464
+ true,
465
+ 'rules-guardian-session-handoff',
466
+ 'Session handoff includes active architecture contract summary'
467
+ );
468
+ } else {
469
+ pushResult(
470
+ results,
471
+ false,
472
+ 'rules-guardian-session-handoff',
473
+ 'Rules guardian report is missing session handoff architecture contract summary'
474
+ );
475
+ }
476
+
477
+ const requiresExplicitConfirmation = rulesGuardianAuditExecution.report?.confirmationPolicy?.requiresExplicitUserConfirmation === true;
478
+
479
+ if (requiresExplicitConfirmation) {
480
+ pushResult(
481
+ results,
482
+ true,
483
+ 'rules-guardian-confirmation-policy',
484
+ 'Direction change policy requires explicit user confirmation'
485
+ );
486
+ } else {
487
+ pushResult(
488
+ results,
489
+ false,
490
+ 'rules-guardian-confirmation-policy',
491
+ 'Rules guardian report does not enforce explicit user confirmation policy'
492
+ );
493
+ }
494
+
495
+ if (rulesGuardianAuditExecution.report.passed === true) {
496
+ pushResult(
497
+ results,
498
+ true,
499
+ 'rules-guardian-drift-confirmation',
500
+ 'Rules guardian drift detection and confirmation checks passed'
501
+ );
502
+ } else {
503
+ const failedAuditDetails = Array.isArray(rulesGuardianAuditExecution.report.failures)
504
+ ? rulesGuardianAuditExecution.report.failures.join('; ')
505
+ : 'Unknown rules guardian audit failures';
506
+ pushResult(
507
+ results,
508
+ false,
509
+ 'rules-guardian-drift-confirmation',
510
+ `Rules guardian audit failed: ${failedAuditDetails}`
511
+ );
512
+ }
513
+ }
514
+
515
+ const explainOnDemandAuditExecution = runMachineReadableScript(
516
+ EXPLAIN_ON_DEMAND_AUDIT_SCRIPT_PATH,
517
+ ['--mode', 'default', '--workflow', 'pr-preparation']
518
+ );
519
+ if (!explainOnDemandAuditExecution.report) {
520
+ const failureDetails = explainOnDemandAuditExecution.executionErrorMessage
521
+ ? `Explain-on-demand audit execution failed before producing a machine-readable report: ${explainOnDemandAuditExecution.executionErrorMessage}`
522
+ : 'Explain-on-demand audit did not produce machine-readable JSON output';
523
+ pushResult(results, false, 'explain-on-demand-audit', failureDetails);
524
+ } else {
525
+ diagnostics.explainOnDemandAudit = explainOnDemandAuditExecution.report;
526
+ pushResult(
527
+ results,
528
+ true,
529
+ 'explain-on-demand-audit',
530
+ `explain-on-demand-audit executed (passed=${explainOnDemandAuditExecution.report.passed}, mode=${explainOnDemandAuditExecution.report.mode}, failures=${explainOnDemandAuditExecution.report.failureCount})`
531
+ );
532
+
533
+ const defaultHiddenStatePolicyPassed = explainOnDemandAuditExecution.report?.responsePolicy?.defaultModeExposesStateInternals === false
534
+ && explainOnDemandAuditExecution.report?.defaultResponse?.containsStateInternals === false;
535
+
536
+ if (defaultHiddenStatePolicyPassed) {
537
+ pushResult(
538
+ results,
539
+ true,
540
+ 'explain-on-demand-default-hidden-state',
541
+ 'Default response mode hides unnecessary state-file internals'
542
+ );
543
+ } else {
544
+ pushResult(
545
+ results,
546
+ false,
547
+ 'explain-on-demand-default-hidden-state',
548
+ 'Default response mode exposes state internals or visibility flags are inconsistent'
549
+ );
550
+ }
551
+
552
+ const diagnosticExplicitRequestPolicyPassed = explainOnDemandAuditExecution.report?.responsePolicy?.diagnosticRequiresExplicitRequest === true;
553
+
554
+ if (diagnosticExplicitRequestPolicyPassed) {
555
+ pushResult(
556
+ results,
557
+ true,
558
+ 'explain-on-demand-explicit-request-gate',
559
+ 'State internals are gated behind explicit diagnostic request'
560
+ );
561
+ } else {
562
+ pushResult(
563
+ results,
564
+ false,
565
+ 'explain-on-demand-explicit-request-gate',
566
+ 'Explain-on-demand policy does not require explicit diagnostic request'
567
+ );
568
+ }
569
+
570
+ const diagnosticExplainabilityPassed = explainOnDemandAuditExecution.report?.diagnosticMode?.canExplainStateDecisions === true;
571
+
572
+ if (diagnosticExplainabilityPassed) {
573
+ pushResult(
574
+ results,
575
+ true,
576
+ 'explain-on-demand-diagnostic-explainability',
577
+ 'Diagnostic mode can explain relevant state decisions when requested'
578
+ );
579
+ } else {
580
+ pushResult(
581
+ results,
582
+ false,
583
+ 'explain-on-demand-diagnostic-explainability',
584
+ 'Explain-on-demand audit cannot provide diagnostic state decision explanations'
585
+ );
586
+ }
587
+
588
+ if (explainOnDemandAuditExecution.report.passed === true) {
589
+ pushResult(
590
+ results,
591
+ true,
592
+ 'explain-on-demand-hard-rule',
593
+ 'Explain-on-demand hard-rule passed'
594
+ );
595
+ } else {
596
+ const failedAuditDetails = Array.isArray(explainOnDemandAuditExecution.report.failures)
597
+ ? explainOnDemandAuditExecution.report.failures.join('; ')
598
+ : 'Unknown explain-on-demand audit failures';
599
+ pushResult(
600
+ results,
601
+ false,
602
+ 'explain-on-demand-hard-rule',
603
+ `Explain-on-demand audit failed: ${failedAuditDetails}`
604
+ );
605
+ }
606
+ }
607
+
434
608
  const frontendParityChecklistContent = readText(FRONTEND_PARITY_CHECKLIST_PATH);
435
609
  if (!frontendParityChecklistContent) {
436
610
  pushResult(results, false, 'frontend-parity-checklist-exists', `Missing ${FRONTEND_PARITY_CHECKLIST_PATH}`);
@@ -0,0 +1,576 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * rules-guardian-audit.mjs
5
+ *
6
+ * Cross-session consistency audit for architecture direction.
7
+ * Ensures session handoff contains an active architecture contract summary,
8
+ * detects direction drift, and requires explicit user confirmation before
9
+ * direction changes are applied.
10
+ */
11
+
12
+ import { existsSync, readFileSync } from 'node:fs';
13
+ import { execFileSync } from 'node:child_process';
14
+ import { dirname, resolve } from 'node:path';
15
+ import { fileURLToPath } from 'node:url';
16
+
17
+ const __filename = fileURLToPath(import.meta.url);
18
+ const __dirname = dirname(__filename);
19
+ const REPOSITORY_ROOT = resolve(__dirname, '..');
20
+
21
+ const ONBOARDING_REPORT_PATH = '.agent-context/state/onboarding-report.json';
22
+ const ARCHITECTURE_RULE_PATH = '.agent-context/rules/architecture.md';
23
+ const PR_CHECKLIST_PATH = '.agent-context/review-checklists/pr-checklist.md';
24
+ const REVIEW_PROMPT_PATH = '.agent-context/prompts/review-code.md';
25
+
26
+ const DEFAULT_WORKFLOW = 'standard';
27
+ const SUPPORTED_WORKFLOWS = new Set([
28
+ 'auto',
29
+ 'standard',
30
+ 'review-request',
31
+ 'pr-preparation',
32
+ 'session-handoff',
33
+ 'direction-change',
34
+ ]);
35
+
36
+ const CORE_PATTERN_SIGNALS = [
37
+ {
38
+ pattern: 'layer-separation',
39
+ snippet: 'Every layer has ONE job.',
40
+ },
41
+ {
42
+ pattern: 'modular-monolith-default',
43
+ snippet: 'Default Architecture: Modular Monolith',
44
+ },
45
+ {
46
+ pattern: 'feature-based-grouping',
47
+ snippet: 'Project Structure: Feature-Based Grouping',
48
+ },
49
+ {
50
+ pattern: 'rules-as-guardian-cross-session',
51
+ snippet: 'Rules as Guardian (Cross-Session Consistency)',
52
+ },
53
+ ];
54
+
55
+ const REQUIRED_ARCHITECTURE_RULE_SNIPPETS = [
56
+ '## Rules as Guardian (Cross-Session Consistency)',
57
+ 'Session handoff must include active architecture contract summary.',
58
+ 'Detect drift before changing declared stack or core patterns.',
59
+ 'Direction changes require explicit user confirmation before applying changes.',
60
+ ];
61
+
62
+ const REQUIRED_PR_CHECKLIST_SNIPPETS = [
63
+ 'Session handoff includes active architecture contract summary',
64
+ 'Drift detection warns before direction changes',
65
+ 'Direction changes require explicit user confirmation',
66
+ ];
67
+
68
+ const REQUIRED_REVIEW_PROMPT_SNIPPETS = [
69
+ 'Enforce cross-session consistency guardian: session handoff must include active architecture contract summary, drift detection must warn before direction changes, and direction changes require explicit user confirmation.',
70
+ ];
71
+
72
+ function pushResult(results, isPassed, checkName, details) {
73
+ results.push({
74
+ checkName,
75
+ passed: isPassed,
76
+ details,
77
+ });
78
+ }
79
+
80
+ function normalizeFilePath(filePath) {
81
+ return filePath.replace(/\\/g, '/').replace(/^\.\//, '');
82
+ }
83
+
84
+ function parseGitFileList(rawOutput) {
85
+ if (typeof rawOutput !== 'string' || rawOutput.trim().length === 0) {
86
+ return [];
87
+ }
88
+
89
+ return rawOutput
90
+ .split(/\r?\n/)
91
+ .map((filePath) => filePath.trim())
92
+ .filter((filePath) => filePath.length > 0)
93
+ .map(normalizeFilePath);
94
+ }
95
+
96
+ function runGitFileQuery(commandArguments) {
97
+ try {
98
+ const rawOutput = execFileSync('git', commandArguments, {
99
+ cwd: REPOSITORY_ROOT,
100
+ encoding: 'utf8',
101
+ maxBuffer: 1024 * 1024,
102
+ });
103
+
104
+ return parseGitFileList(rawOutput);
105
+ } catch {
106
+ return [];
107
+ }
108
+ }
109
+
110
+ function runGitRawQuery(commandArguments) {
111
+ try {
112
+ return execFileSync('git', commandArguments, {
113
+ cwd: REPOSITORY_ROOT,
114
+ encoding: 'utf8',
115
+ maxBuffer: 1024 * 1024,
116
+ });
117
+ } catch {
118
+ return '';
119
+ }
120
+ }
121
+
122
+ function uniqueSorted(filePaths) {
123
+ return Array.from(new Set(filePaths)).sort((leftPath, rightPath) => leftPath.localeCompare(rightPath));
124
+ }
125
+
126
+ function collectChangedFiles() {
127
+ const workingTreeFiles = runGitFileQuery(['diff', '--name-only']);
128
+ const stagedFiles = runGitFileQuery(['diff', '--name-only', '--cached']);
129
+ const workingScopeFiles = uniqueSorted([...workingTreeFiles, ...stagedFiles]);
130
+
131
+ if (workingScopeFiles.length > 0) {
132
+ return {
133
+ source: 'working-tree-and-index',
134
+ files: workingScopeFiles,
135
+ };
136
+ }
137
+
138
+ const latestCommitRangeFiles = runGitFileQuery(['diff', '--name-only', 'HEAD~1..HEAD']);
139
+ if (latestCommitRangeFiles.length > 0) {
140
+ return {
141
+ source: 'latest-commit-range',
142
+ files: uniqueSorted(latestCommitRangeFiles),
143
+ };
144
+ }
145
+
146
+ const headCommitFiles = runGitFileQuery(['show', '--pretty=format:', '--name-only', 'HEAD']);
147
+ if (headCommitFiles.length > 0) {
148
+ return {
149
+ source: 'head-commit',
150
+ files: uniqueSorted(headCommitFiles),
151
+ };
152
+ }
153
+
154
+ return {
155
+ source: 'none',
156
+ files: [],
157
+ };
158
+ }
159
+
160
+ function readText(relativeFilePath) {
161
+ const absolutePath = resolve(REPOSITORY_ROOT, relativeFilePath);
162
+ if (!existsSync(absolutePath)) {
163
+ return '';
164
+ }
165
+
166
+ return readFileSync(absolutePath, 'utf8');
167
+ }
168
+
169
+ function readPreviousRevisionText(relativeFilePath) {
170
+ const rawOutput = runGitRawQuery(['show', `HEAD~1:${relativeFilePath}`]);
171
+ return typeof rawOutput === 'string' && rawOutput.length > 0
172
+ ? rawOutput
173
+ : '';
174
+ }
175
+
176
+ function parseOnboardingReport(onboardingReportContent) {
177
+ if (typeof onboardingReportContent !== 'string' || onboardingReportContent.trim().length === 0) {
178
+ return null;
179
+ }
180
+
181
+ try {
182
+ return JSON.parse(onboardingReportContent);
183
+ } catch {
184
+ return null;
185
+ }
186
+ }
187
+
188
+ function normalizeCorePatterns(corePatterns) {
189
+ if (!Array.isArray(corePatterns)) {
190
+ return [];
191
+ }
192
+
193
+ return Array.from(new Set(corePatterns
194
+ .map((patternValue) => String(patternValue || '').trim().toLowerCase())
195
+ .filter((patternValue) => patternValue.length > 0))).sort((leftValue, rightValue) => (
196
+ leftValue.localeCompare(rightValue)
197
+ ));
198
+ }
199
+
200
+ function detectCorePatterns(architectureRuleContent) {
201
+ return CORE_PATTERN_SIGNALS
202
+ .filter((signalEntry) => architectureRuleContent.includes(signalEntry.snippet))
203
+ .map((signalEntry) => signalEntry.pattern);
204
+ }
205
+
206
+ function parseCliArguments(argumentList) {
207
+ let workflow = DEFAULT_WORKFLOW;
208
+ let confirmDirectionChange = false;
209
+ let proposedStack = '';
210
+ let proposedBlueprint = '';
211
+ let proposedCorePatterns = [];
212
+
213
+ for (let argumentIndex = 0; argumentIndex < argumentList.length; argumentIndex += 1) {
214
+ const argumentValue = argumentList[argumentIndex];
215
+
216
+ if (argumentValue === '--confirm-direction-change') {
217
+ confirmDirectionChange = true;
218
+ continue;
219
+ }
220
+
221
+ if (argumentValue === '--workflow') {
222
+ const nextArgumentValue = argumentList[argumentIndex + 1];
223
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
224
+ workflow = nextArgumentValue;
225
+ argumentIndex += 1;
226
+ }
227
+ continue;
228
+ }
229
+
230
+ if (argumentValue.startsWith('--workflow=')) {
231
+ workflow = argumentValue.slice('--workflow='.length);
232
+ continue;
233
+ }
234
+
235
+ if (argumentValue === '--proposed-stack') {
236
+ const nextArgumentValue = argumentList[argumentIndex + 1];
237
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
238
+ proposedStack = nextArgumentValue;
239
+ argumentIndex += 1;
240
+ }
241
+ continue;
242
+ }
243
+
244
+ if (argumentValue.startsWith('--proposed-stack=')) {
245
+ proposedStack = argumentValue.slice('--proposed-stack='.length);
246
+ continue;
247
+ }
248
+
249
+ if (argumentValue === '--proposed-blueprint') {
250
+ const nextArgumentValue = argumentList[argumentIndex + 1];
251
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
252
+ proposedBlueprint = nextArgumentValue;
253
+ argumentIndex += 1;
254
+ }
255
+ continue;
256
+ }
257
+
258
+ if (argumentValue.startsWith('--proposed-blueprint=')) {
259
+ proposedBlueprint = argumentValue.slice('--proposed-blueprint='.length);
260
+ continue;
261
+ }
262
+
263
+ if (argumentValue === '--proposed-core-patterns') {
264
+ const nextArgumentValue = argumentList[argumentIndex + 1];
265
+ if (nextArgumentValue && !nextArgumentValue.startsWith('--')) {
266
+ proposedCorePatterns = nextArgumentValue.split(',');
267
+ argumentIndex += 1;
268
+ }
269
+ continue;
270
+ }
271
+
272
+ if (argumentValue.startsWith('--proposed-core-patterns=')) {
273
+ proposedCorePatterns = argumentValue.slice('--proposed-core-patterns='.length).split(',');
274
+ }
275
+ }
276
+
277
+ const normalizedWorkflow = String(workflow).trim().toLowerCase() || DEFAULT_WORKFLOW;
278
+
279
+ return {
280
+ workflow: SUPPORTED_WORKFLOWS.has(normalizedWorkflow) ? normalizedWorkflow : DEFAULT_WORKFLOW,
281
+ confirmDirectionChange,
282
+ proposedStack: String(proposedStack || '').trim(),
283
+ proposedBlueprint: String(proposedBlueprint || '').trim(),
284
+ proposedCorePatterns: normalizeCorePatterns(proposedCorePatterns),
285
+ };
286
+ }
287
+
288
+ function parseBooleanFromEnvironment(rawEnvironmentValue) {
289
+ const normalizedEnvironmentValue = String(rawEnvironmentValue || '').trim().toLowerCase();
290
+ return normalizedEnvironmentValue === '1'
291
+ || normalizedEnvironmentValue === 'true'
292
+ || normalizedEnvironmentValue === 'yes'
293
+ || normalizedEnvironmentValue === 'y';
294
+ }
295
+
296
+ function buildArchitectureContract(onboardingReport, corePatterns) {
297
+ return {
298
+ stack: String(onboardingReport?.selectedStack || 'unknown').trim() || 'unknown',
299
+ blueprint: String(onboardingReport?.selectedBlueprint || 'unknown').trim() || 'unknown',
300
+ profile: String(onboardingReport?.selectedProfile || 'unknown').trim() || 'unknown',
301
+ corePatterns: normalizeCorePatterns(corePatterns),
302
+ };
303
+ }
304
+
305
+ function toComparableContractValue(value) {
306
+ if (Array.isArray(value)) {
307
+ return normalizeCorePatterns(value).join(',');
308
+ }
309
+
310
+ return String(value || '').trim();
311
+ }
312
+
313
+ function detectContractDrift(baseContract, targetContract, driftSource) {
314
+ const driftItems = [];
315
+ const fieldNames = ['stack', 'blueprint', 'profile', 'corePatterns'];
316
+
317
+ for (const fieldName of fieldNames) {
318
+ const baseValue = toComparableContractValue(baseContract?.[fieldName]);
319
+ const targetValue = toComparableContractValue(targetContract?.[fieldName]);
320
+
321
+ if (baseValue !== targetValue) {
322
+ driftItems.push({
323
+ field: fieldName,
324
+ from: baseValue,
325
+ to: targetValue,
326
+ source: driftSource,
327
+ });
328
+ }
329
+ }
330
+
331
+ return driftItems;
332
+ }
333
+
334
+ function buildSessionHandoffSummary(architectureContract) {
335
+ const corePatternsSummary = architectureContract.corePatterns.length > 0
336
+ ? architectureContract.corePatterns.join(', ')
337
+ : 'none';
338
+
339
+ return `Architecture contract summary: stack=${architectureContract.stack}, blueprint=${architectureContract.blueprint}, profile=${architectureContract.profile}, corePatterns=${corePatternsSummary}.`;
340
+ }
341
+
342
+ function assertSnippetCoverage(sourceLabel, sourcePath, requiredSnippets, failures, results) {
343
+ const sourceContent = readText(sourcePath);
344
+
345
+ if (!sourceContent) {
346
+ failures.push(`Missing ${sourceLabel} source: ${sourcePath}`);
347
+ pushResult(results, false, `${sourceLabel}-source-exists`, `Missing ${sourcePath}`);
348
+ return;
349
+ }
350
+
351
+ pushResult(results, true, `${sourceLabel}-source-exists`, `${sourcePath} is present`);
352
+
353
+ const missingSnippets = requiredSnippets.filter((requiredSnippet) => !sourceContent.includes(requiredSnippet));
354
+
355
+ if (missingSnippets.length > 0) {
356
+ failures.push(`Missing ${sourceLabel} snippets: ${missingSnippets.join(', ')}`);
357
+ pushResult(
358
+ results,
359
+ false,
360
+ `${sourceLabel}-source-coverage`,
361
+ `Missing snippets in ${sourcePath}: ${missingSnippets.join(', ')}`
362
+ );
363
+ return;
364
+ }
365
+
366
+ pushResult(results, true, `${sourceLabel}-source-coverage`, `${sourceLabel} snippets are complete`);
367
+ }
368
+
369
+ function runAudit() {
370
+ const parsedArguments = parseCliArguments(process.argv.slice(2));
371
+ const changedScope = collectChangedFiles();
372
+ const changedFiles = changedScope.files;
373
+ const results = [];
374
+ const failures = [];
375
+ const warnings = [];
376
+
377
+ const envConfirmationFlag = parseBooleanFromEnvironment(process.env.RULES_GUARDIAN_CONFIRM_DIRECTION_CHANGE);
378
+ const confirmationProvided = parsedArguments.confirmDirectionChange || envConfirmationFlag;
379
+ const confirmationSource = parsedArguments.confirmDirectionChange
380
+ ? 'cli-flag'
381
+ : (envConfirmationFlag ? 'environment-variable' : 'none');
382
+
383
+ pushResult(results, true, 'context-workflow', `workflow=${parsedArguments.workflow}`);
384
+ pushResult(
385
+ results,
386
+ true,
387
+ 'direction-change-confirmation-flag',
388
+ confirmationProvided
389
+ ? `Explicit direction-change confirmation provided via ${confirmationSource}`
390
+ : 'Explicit direction-change confirmation not provided'
391
+ );
392
+
393
+ assertSnippetCoverage(
394
+ 'rules-guardian-architecture-rule',
395
+ ARCHITECTURE_RULE_PATH,
396
+ REQUIRED_ARCHITECTURE_RULE_SNIPPETS,
397
+ failures,
398
+ results
399
+ );
400
+
401
+ assertSnippetCoverage(
402
+ 'rules-guardian-pr-checklist',
403
+ PR_CHECKLIST_PATH,
404
+ REQUIRED_PR_CHECKLIST_SNIPPETS,
405
+ failures,
406
+ results
407
+ );
408
+
409
+ assertSnippetCoverage(
410
+ 'rules-guardian-review-prompt',
411
+ REVIEW_PROMPT_PATH,
412
+ REQUIRED_REVIEW_PROMPT_SNIPPETS,
413
+ failures,
414
+ results
415
+ );
416
+
417
+ const onboardingReportContent = readText(ONBOARDING_REPORT_PATH);
418
+ const onboardingReport = parseOnboardingReport(onboardingReportContent);
419
+
420
+ if (!onboardingReportContent) {
421
+ failures.push(`Missing architecture contract source: ${ONBOARDING_REPORT_PATH}`);
422
+ pushResult(results, false, 'architecture-contract-source', `Missing ${ONBOARDING_REPORT_PATH}`);
423
+ } else if (!onboardingReport) {
424
+ failures.push(`Cannot parse architecture contract source: ${ONBOARDING_REPORT_PATH}`);
425
+ pushResult(results, false, 'architecture-contract-source', `Invalid JSON in ${ONBOARDING_REPORT_PATH}`);
426
+ } else {
427
+ pushResult(results, true, 'architecture-contract-source', `${ONBOARDING_REPORT_PATH} is present and valid`);
428
+ }
429
+
430
+ const architectureRuleContent = readText(ARCHITECTURE_RULE_PATH);
431
+ const activeCorePatterns = detectCorePatterns(architectureRuleContent);
432
+
433
+ if (activeCorePatterns.length === 0) {
434
+ failures.push('Cannot resolve active core patterns from architecture rule snippets');
435
+ pushResult(
436
+ results,
437
+ false,
438
+ 'architecture-core-patterns',
439
+ `No core pattern signals detected in ${ARCHITECTURE_RULE_PATH}`
440
+ );
441
+ } else {
442
+ pushResult(
443
+ results,
444
+ true,
445
+ 'architecture-core-patterns',
446
+ `Resolved ${activeCorePatterns.length} core pattern signals: ${activeCorePatterns.join(', ')}`
447
+ );
448
+ }
449
+
450
+ const activeContract = buildArchitectureContract(onboardingReport, activeCorePatterns);
451
+ const sessionHandoffSummary = buildSessionHandoffSummary(activeContract);
452
+ const sessionHandoffIncluded = sessionHandoffSummary.trim().length > 0;
453
+
454
+ if (sessionHandoffIncluded) {
455
+ pushResult(results, true, 'session-handoff-contract-summary', sessionHandoffSummary);
456
+ } else {
457
+ failures.push('Session handoff summary is missing');
458
+ pushResult(results, false, 'session-handoff-contract-summary', 'Session handoff summary is empty');
459
+ }
460
+
461
+ const previousOnboardingReport = parseOnboardingReport(readPreviousRevisionText(ONBOARDING_REPORT_PATH));
462
+ const previousArchitectureRuleContent = readPreviousRevisionText(ARCHITECTURE_RULE_PATH) || architectureRuleContent;
463
+ const previousCorePatterns = detectCorePatterns(previousArchitectureRuleContent);
464
+
465
+ const previousContract = buildArchitectureContract(
466
+ previousOnboardingReport || onboardingReport,
467
+ previousCorePatterns.length > 0 ? previousCorePatterns : activeCorePatterns
468
+ );
469
+
470
+ const proposedContract = {
471
+ stack: parsedArguments.proposedStack || activeContract.stack,
472
+ blueprint: parsedArguments.proposedBlueprint || activeContract.blueprint,
473
+ profile: activeContract.profile,
474
+ corePatterns: parsedArguments.proposedCorePatterns.length > 0
475
+ ? parsedArguments.proposedCorePatterns
476
+ : activeContract.corePatterns,
477
+ };
478
+
479
+ const persistedContractDrift = detectContractDrift(
480
+ previousContract,
481
+ activeContract,
482
+ 'persisted-change-since-previous-session'
483
+ );
484
+ const proposedContractDrift = detectContractDrift(
485
+ activeContract,
486
+ proposedContract,
487
+ 'proposed-direction-change'
488
+ );
489
+
490
+ const driftItems = [...persistedContractDrift, ...proposedContractDrift];
491
+ const persistedDriftDetected = persistedContractDrift.length > 0;
492
+ const proposedDirectionChangeDetected = proposedContractDrift.length > 0;
493
+ const driftDetected = driftItems.length > 0;
494
+
495
+ if (driftDetected) {
496
+ const driftSummary = driftItems
497
+ .map((driftItem) => `${driftItem.field}: ${driftItem.from} -> ${driftItem.to} (${driftItem.source})`)
498
+ .join('; ');
499
+ warnings.push(`Direction drift detected: ${driftSummary}`);
500
+ pushResult(results, true, 'direction-drift-detection', `Drift detected: ${driftSummary}`);
501
+ } else {
502
+ pushResult(results, true, 'direction-drift-detection', 'No direction drift detected');
503
+ }
504
+
505
+ if (proposedDirectionChangeDetected && !confirmationProvided) {
506
+ failures.push('Direction change detected without explicit user confirmation');
507
+ pushResult(
508
+ results,
509
+ false,
510
+ 'direction-change-explicit-confirmation',
511
+ 'Direction change detected. Re-run with --confirm-direction-change (or set RULES_GUARDIAN_CONFIRM_DIRECTION_CHANGE=true) after explicit user approval.'
512
+ );
513
+ } else if (proposedDirectionChangeDetected && confirmationProvided) {
514
+ pushResult(
515
+ results,
516
+ true,
517
+ 'direction-change-explicit-confirmation',
518
+ `Direction change confirmed explicitly via ${confirmationSource}`
519
+ );
520
+ } else if (persistedDriftDetected) {
521
+ pushResult(
522
+ results,
523
+ true,
524
+ 'direction-change-explicit-confirmation',
525
+ 'Persisted drift detected from previous session; explicit confirmation is required only for new proposed direction changes'
526
+ );
527
+ } else {
528
+ pushResult(
529
+ results,
530
+ true,
531
+ 'direction-change-explicit-confirmation',
532
+ 'No direction change detected; explicit confirmation not required'
533
+ );
534
+ }
535
+
536
+ const reportPayload = {
537
+ generatedAt: new Date().toISOString(),
538
+ auditName: 'rules-guardian-audit',
539
+ workflow: parsedArguments.workflow,
540
+ source: changedScope.source,
541
+ changedFileCount: changedFiles.length,
542
+ changedFiles,
543
+ confirmationPolicy: {
544
+ requiresExplicitUserConfirmation: true,
545
+ requiredForProposedDirectionChange: true,
546
+ confirmationProvided,
547
+ confirmationSource,
548
+ },
549
+ sessionHandoff: {
550
+ included: sessionHandoffIncluded,
551
+ contractSummary: sessionHandoffSummary,
552
+ activeArchitectureContract: activeContract,
553
+ previousArchitectureContract: previousContract,
554
+ proposedArchitectureContract: proposedContract,
555
+ },
556
+ driftDetection: {
557
+ driftDetected,
558
+ persistedDriftDetected,
559
+ proposedDirectionChangeDetected,
560
+ driftItemCount: driftItems.length,
561
+ persistedDriftItemCount: persistedContractDrift.length,
562
+ proposedDriftItemCount: proposedContractDrift.length,
563
+ driftItems,
564
+ },
565
+ passed: failures.length === 0,
566
+ failureCount: failures.length,
567
+ failures,
568
+ warnings,
569
+ results,
570
+ };
571
+
572
+ console.log(JSON.stringify(reportPayload, null, 2));
573
+ process.exit(reportPayload.passed ? 0 : 1);
574
+ }
575
+
576
+ runAudit();
@@ -173,6 +173,8 @@ async function validateRequiredFiles() {
173
173
  'scripts/frontend-usability-audit.mjs',
174
174
  'scripts/documentation-boundary-audit.mjs',
175
175
  'scripts/context-triggered-audit.mjs',
176
+ 'scripts/rules-guardian-audit.mjs',
177
+ 'scripts/explain-on-demand-audit.mjs',
176
178
  'scripts/release-gate.mjs',
177
179
  'scripts/generate-sbom.mjs',
178
180
  'scripts/init-project.sh',