arkgate 2.6.1 → 2.8.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 (55) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +8 -3
  3. package/bin/ark-check.mjs +19 -993
  4. package/bin/ark-layer-match.mjs +148 -171
  5. package/bin/ark-shared.mjs +9 -159
  6. package/bin/lib/agent-gates.mjs +48 -228
  7. package/bin/lib/architecture-scan.mjs +299 -0
  8. package/bin/lib/ast-scan.mjs +427 -0
  9. package/bin/lib/baseline-key.mjs +23 -0
  10. package/bin/lib/codex-home.mjs +320 -0
  11. package/bin/lib/config-warnings.mjs +228 -0
  12. package/bin/lib/doctor-plan.mjs +2 -0
  13. package/bin/lib/graph-cycles.mjs +56 -0
  14. package/bin/lib/remediation.mjs +182 -0
  15. package/bin/lib/scan-files.mjs +69 -0
  16. package/bin/lib/ts-resolve.mjs +216 -0
  17. package/bin/lib/violations.mjs +3 -9
  18. package/dist/eslint/index.cjs +21 -3
  19. package/dist/eslint/index.cjs.map +1 -1
  20. package/dist/eslint/index.d.cts +5 -3
  21. package/dist/eslint/index.d.ts +5 -3
  22. package/dist/eslint/index.js +21 -3
  23. package/dist/eslint/index.js.map +1 -1
  24. package/dist/index.cjs +1 -1
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +3 -3
  27. package/dist/index.d.ts +3 -3
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/nestjs/index.cjs +1 -1
  31. package/dist/nestjs/index.cjs.map +1 -1
  32. package/dist/nestjs/index.d.cts +1 -1
  33. package/dist/nestjs/index.d.ts +1 -1
  34. package/dist/nestjs/index.js +1 -1
  35. package/dist/nestjs/index.js.map +1 -1
  36. package/dist/runtime/index.cjs +3080 -0
  37. package/dist/runtime/index.cjs.map +1 -0
  38. package/dist/runtime/index.d.cts +2 -0
  39. package/dist/runtime/index.d.ts +2 -0
  40. package/dist/runtime/index.js +2998 -0
  41. package/dist/runtime/index.js.map +1 -0
  42. package/dist/{types-DpdVN7Lm.d.cts → types-CP3KkwZt.d.cts} +1 -1
  43. package/dist/{types-DpdVN7Lm.d.ts → types-CP3KkwZt.d.ts} +1 -1
  44. package/docs/agent-guide.md +5 -2
  45. package/docs/ai-gates.md +41 -7
  46. package/docs/brownfield-adoption.md +7 -0
  47. package/docs/demos/03-copilot-autopilot.md +3 -2
  48. package/docs/enthusiast/reference-commands.md +2 -2
  49. package/docs/migrate-from-ark-runtime-kernel.md +4 -2
  50. package/docs/package-surface.md +72 -0
  51. package/docs/production-hardening.md +3 -0
  52. package/package.json +12 -1
  53. package/server.json +2 -2
  54. package/templates/skills/ark-explain.md +3 -2
  55. package/templates/skills/ark-loop.md +2 -1
@@ -3,7 +3,6 @@
3
3
  */
4
4
  import { createRequire } from 'node:module';
5
5
  import { spawnSync } from 'node:child_process';
6
- import crypto from 'node:crypto';
7
6
  import fs from 'node:fs';
8
7
  import os from 'node:os';
9
8
  import path from 'node:path';
@@ -23,6 +22,40 @@ import {
23
22
  createElevenLayerConfig,
24
23
  applyFrameworkLayoutOverlays
25
24
  } from '../ark-shared.mjs';
25
+ import {
26
+ assessCodexHomeMcp,
27
+ codexArkBlockHasPreferredBin,
28
+ codexArkBlockNeedsRewrite,
29
+ codexConfigPath,
30
+ codexPrimaryTable,
31
+ codexProjectSlug,
32
+ codexPromptsDir,
33
+ codexScopedTableForRoot,
34
+ extractCodexArkRootFromToml,
35
+ extractCodexRootFromBlock,
36
+ isTempOrUpgradeRoot,
37
+ listCodexArkServerTables,
38
+ upsertCodexMcpTable,
39
+ wireCodexMcp,
40
+ } from './codex-home.mjs';
41
+
42
+ // Re-export Codex home API for existing consumers (ark-check, tests).
43
+ export {
44
+ assessCodexHomeMcp,
45
+ codexArkBlockHasPreferredBin,
46
+ codexArkBlockNeedsRewrite,
47
+ codexConfigPath,
48
+ codexPrimaryTable,
49
+ codexProjectSlug,
50
+ codexPromptsDir,
51
+ codexScopedTableForRoot,
52
+ extractCodexArkRootFromToml,
53
+ extractCodexRootFromBlock,
54
+ isTempOrUpgradeRoot,
55
+ listCodexArkServerTables,
56
+ upsertCodexMcpTable,
57
+ wireCodexMcp,
58
+ };
26
59
 
27
60
  /** Package root (parent of bin/). All modules live under bin/lib/. */
28
61
  const __packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
@@ -762,136 +795,6 @@ export function skillTemplateNames() {
762
795
  // skills this version ships, surface it here so agents and CI actually notice.
763
796
  // Advisory only — never affects the exit code. Copilot has no reliable directory
764
797
  // signal, so it is not auto-detected (explicit --tools only), matching resolveTools.
765
- // Where Codex loads slash-command prompts from. Codex reads $CODEX_HOME/prompts
766
- // (defaulting to ~/.codex/prompts), NOT the repo — so home copies of the /ark-*
767
- // skills drift out of date when a repo refresh only touches in-repo tool dirs.
768
- export function codexPromptsDir() {
769
- const base = process.env.CODEX_HOME || path.join(os.homedir(), '.codex');
770
- return path.join(base, 'prompts');
771
- }
772
-
773
- // Where Codex reads its MCP server registrations. Unlike Claude (.claude/settings.json)
774
- // and Cursor (.cursor/mcp.json), Codex loads MCP servers only from $CODEX_HOME/config.toml
775
- // (~/.codex/config.toml) — never from .mcp.json — so wiring Codex means editing the user's
776
- // home config, not a repo file.
777
- export function codexConfigPath() {
778
- const base = process.env.CODEX_HOME || path.join(os.homedir(), '.codex');
779
- return path.join(base, 'config.toml');
780
- }
781
-
782
- // Merge the [mcp_servers.ark] table into Codex's config.toml so `ark://manifest` and the
783
- // AI write gate are live from the first edit — the piece that was previously only shipped as
784
- // a copy-me sample in docs/ark-codex-config.toml. Idempotent: an existing ark table is left
785
- // untouched unless `force` replaces it; other content in the file is preserved. Returns a
786
- // status for the install summary. The table match runs from the [mcp_servers.ark] header to
787
- // the line before the next top-level table header (a line starting with `[`) or EOF.
788
- //
789
- // Unlike .mcp.json / .cursor/mcp.json (loaded relative to the project), config.toml is a
790
- // GLOBAL file — Codex launches it without the project as cwd — so `--root .` would resolve
791
- // against the wrong directory. The paths must be absolute, and TOML string values need the
792
- // backslashes/quotes escaped (matters on Windows and for repo paths containing quotes).
793
- export function wireCodexMcp(root, force) {
794
- const file = codexConfigPath();
795
- const esc = (s) => s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
796
- const absRoot = path.resolve(root);
797
- const absConfig = path.join(absRoot, 'ark.config.json');
798
- // Preferred product bin; absolute --root so Codex (cwd ≠ project) resolves correctly.
799
- const preferredBin = 'arkgate-mcp';
800
- const { command, args } = execCommandParts(root, preferredBin, [
801
- '--root',
802
- esc(absRoot),
803
- '--config',
804
- esc(absConfig),
805
- ]);
806
- const argsToml = args.map((value) => `"${value}"`).join(', ');
807
- const makeBlock = (table) =>
808
- `[mcp_servers.${table}]
809
- command = "${command}"
810
- args = [${argsToml}]`;
811
- let existing = '';
812
- try {
813
- if (fs.existsSync(file)) existing = fs.readFileSync(file, 'utf8');
814
- } catch (error) {
815
- return { status: 'failed', file, message: error.message };
816
- }
817
- const tableRe = /(^|\n)\[mcp_servers\.ark\][^\n]*\n(?:(?!\[)[^\n]*\n?)*/;
818
- const hasTable = tableRe.test(existing);
819
- const existingRoot = hasTable ? extractCodexArkRootFromToml(existing) : null;
820
- let differentProject = false;
821
- try {
822
- differentProject = Boolean(
823
- existingRoot && path.resolve(existingRoot) !== absRoot
824
- );
825
- } catch {
826
- differentProject = Boolean(existingRoot);
827
- }
828
- // Fail-closed: rewrite temp/upgrade roots and dual/wrong bins even without --force.
829
- const mustRewrite = hasTable && codexArkBlockNeedsRewrite(existing, absRoot);
830
-
831
- // Multi-project: another project's [mcp_servers.ark] is present. Without --force,
832
- // add a project-scoped table so we do not steal the primary binding.
833
- if (hasTable && differentProject && !force && !mustRewrite) {
834
- const slug =
835
- path
836
- .basename(absRoot)
837
- .replace(/[^a-zA-Z0-9_-]/g, '_')
838
- .slice(0, 48) || 'project';
839
- const table = `ark_${slug}`;
840
- const multiRe = new RegExp(
841
- `(^|\\n)\\[mcp_servers\\.${table.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\][^\\n]*\\n(?:(?!\\[)[^\\n]*\\n?)*`
842
- );
843
- const block = makeBlock(table);
844
- let next;
845
- if (multiRe.test(existing)) {
846
- next = existing.replace(multiRe, (match) => `${match.startsWith('\n') ? '\n' : ''}${block}\n`);
847
- } else {
848
- const sep =
849
- existing.length === 0
850
- ? ''
851
- : existing.endsWith('\n\n')
852
- ? ''
853
- : existing.endsWith('\n')
854
- ? '\n'
855
- : '\n\n';
856
- next = `${existing}${sep}${block}\n`;
857
- }
858
- try {
859
- fs.mkdirSync(path.dirname(file), { recursive: true });
860
- fs.writeFileSync(file, next);
861
- } catch (error) {
862
- return { status: 'failed', file, message: error.message };
863
- }
864
- return { status: 'written-multi', file, table, primaryUnchanged: true };
865
- }
866
-
867
- if (hasTable && !force && !mustRewrite) {
868
- return { status: 'skipped', file };
869
- }
870
- const block = makeBlock('ark');
871
- let next;
872
- if (hasTable) {
873
- next = existing.replace(tableRe, (match) => `${match.startsWith('\n') ? '\n' : ''}${block}\n`);
874
- } else {
875
- const sep = existing.length === 0 ? '' : existing.endsWith('\n\n') ? '' : existing.endsWith('\n') ? '\n' : '\n\n';
876
- next = `${existing}${sep}${block}\n`;
877
- }
878
- try {
879
- fs.mkdirSync(path.dirname(file), { recursive: true });
880
- fs.writeFileSync(file, next);
881
- } catch (error) {
882
- return { status: 'failed', file, message: error.message };
883
- }
884
- return {
885
- status: hasTable ? 'updated' : 'written',
886
- file,
887
- ...(mustRewrite && !force ? { reason: 'temp-or-stale-root' } : {}),
888
- };
889
- }
890
-
891
- // Detects stale/missing /ark-* skills in the Codex home prompts dir. Only nags
892
- // when at least one ark-* prompt already lives there (evidence Codex was set up
893
- // for this user) — never introduces Codex to someone who doesn't use it. Same
894
- // guards as detectSkillGaps (adopted repo, not the Ark source tree).
895
798
  export function detectCodexHomeGap(root) {
896
799
  if (!fs.existsSync(path.join(root, 'AGENTS.md'))) return null;
897
800
  if (fs.existsSync(path.join(root, 'templates', 'skills'))) return null;
@@ -1027,73 +930,6 @@ const CORE_LAYER_NAMES = new Set([
1027
930
  'PersistenceAdapters',
1028
931
  ]);
1029
932
 
1030
- /** Temp / upgrade sandbox roots must never remain as Codex MCP --root. */
1031
- export function isTempOrUpgradeRoot(p) {
1032
- if (!p || typeof p !== 'string') return false;
1033
- const n = p.replace(/\\/g, '/');
1034
- return (
1035
- /\/var\/folders\//i.test(n) ||
1036
- /\/tmp\//i.test(n) ||
1037
- /\/Temp\//i.test(n) ||
1038
- /ark-upgrade/i.test(n) ||
1039
- /\/T\/(?:ark-|grok-)/i.test(n) ||
1040
- /[\\/]AppData[\\/]Local[\\/]Temp[\\/]/i.test(n)
1041
- );
1042
- }
1043
-
1044
- /** Extract --root value from Codex [mcp_servers.ark] args array text. */
1045
- export function extractCodexArkRootFromToml(tomlText) {
1046
- if (!tomlText || typeof tomlText !== 'string') return null;
1047
- const start = tomlText.search(/(^|\n)\[mcp_servers\.ark\]/);
1048
- if (start < 0) return null;
1049
- const rest = tomlText.slice(start);
1050
- const endMatch = rest.slice(1).search(/\n\[/);
1051
- const block = endMatch >= 0 ? rest.slice(0, endMatch + 1) : rest;
1052
- // args = ["arkgate-mcp", "--root", "/abs/path", ...]
1053
- const rootIdx = block.search(/"--root"\s*,\s*"/);
1054
- if (rootIdx < 0) {
1055
- // alternate: --root as adjacent string after any bin
1056
- const m = block.match(/"--root"\s*,\s*"([^"]+)"/);
1057
- return m ? m[1] : null;
1058
- }
1059
- const m = block.slice(rootIdx).match(/"--root"\s*,\s*"([^"]+)"/);
1060
- return m ? m[1] : null;
1061
- }
1062
-
1063
- export function codexArkBlockHasPreferredBin(tomlText) {
1064
- if (!tomlText) return false;
1065
- const start = tomlText.search(/(^|\n)\[mcp_servers\.ark\]/);
1066
- if (start < 0) return false;
1067
- const rest = tomlText.slice(start);
1068
- const endMatch = rest.slice(1).search(/\n\[/);
1069
- const block = endMatch >= 0 ? rest.slice(0, endMatch + 1) : rest;
1070
- const bins = [...block.matchAll(/"(arkgate-mcp|ark-mcp)"/g)].map((m) => m[1]);
1071
- if (bins.length > 1) return false;
1072
- return bins.length === 1 && bins[0] === PREFERRED_MCP_BIN;
1073
- }
1074
-
1075
- /**
1076
- * True when the primary [mcp_servers.ark] block is broken (temp root / dual bin)
1077
- * and should be rewritten fail-closed. Different permanent project roots are NOT
1078
- * "broken" — multi-project wiring uses a secondary table instead.
1079
- */
1080
- export function codexArkBlockNeedsRewrite(tomlText, absRoot) {
1081
- if (!tomlText || !tomlText.includes('[mcp_servers.ark]')) return true;
1082
- const rootArg = extractCodexArkRootFromToml(tomlText);
1083
- if (!rootArg || isTempOrUpgradeRoot(rootArg)) return true;
1084
- // Permanent different project: multi-project path handles this (do not steal primary).
1085
- try {
1086
- if (path.resolve(rootArg) !== path.resolve(absRoot)) {
1087
- if (!isTempOrUpgradeRoot(rootArg)) return false;
1088
- return true;
1089
- }
1090
- } catch {
1091
- return true;
1092
- }
1093
- if (!codexArkBlockHasPreferredBin(tomlText)) return true;
1094
- return false;
1095
- }
1096
-
1097
933
  /**
1098
934
  * Production deploy path quality (universal — any consumer repo).
1099
935
  * Detects when the production build host runs ESLint / typecheck as part of
@@ -1383,7 +1219,7 @@ export function collectAdoptionGaps(root, config, coverage) {
1383
1219
  }
1384
1220
  }
1385
1221
 
1386
- // --- Codex home MCP (temp path / wrong root / dual bin) ---
1222
+ // --- Codex home MCP (temp path / wrong root / multi-project) ---
1387
1223
  let codexHome = null;
1388
1224
  if (adopted && !isProducer) {
1389
1225
  const codexFile = codexConfigPath();
@@ -1394,39 +1230,23 @@ export function collectAdoptionGaps(root, config, coverage) {
1394
1230
  toml = '';
1395
1231
  }
1396
1232
  if (toml.includes('[mcp_servers.ark]')) {
1397
- const rootArg = extractCodexArkRootFromToml(toml);
1398
- const absRoot = path.resolve(root);
1399
- const temp = isTempOrUpgradeRoot(rootArg);
1400
- let wrongRoot = false;
1401
- try {
1402
- wrongRoot = rootArg ? path.resolve(rootArg) !== absRoot : true;
1403
- } catch {
1404
- wrongRoot = true;
1405
- }
1406
- const preferredBin = codexArkBlockHasPreferredBin(toml);
1407
- const needsRewrite = codexArkBlockNeedsRewrite(toml, absRoot);
1233
+ const assessed = assessCodexHomeMcp(toml, root);
1408
1234
  codexHome = {
1409
1235
  file: codexFile,
1410
- root: rootArg,
1411
- tempPath: temp,
1412
- wrongRoot,
1413
- preferredBin,
1414
- needsRewrite,
1236
+ root: assessed.root,
1237
+ tempPath: assessed.tempPath,
1238
+ wrongRoot: assessed.wrongRoot,
1239
+ preferredBin: assessed.preferredBin,
1240
+ needsRewrite: assessed.needsRewrite,
1241
+ multiProject: assessed.multiProject,
1242
+ scopedTable: assessed.scopedTable,
1415
1243
  };
1416
- if (needsRewrite) {
1244
+ if (assessed.gap) {
1417
1245
  gaps.push({
1418
- id: 'codex-home-mcp',
1419
- severity: temp || wrongRoot ? 'warn' : 'info',
1420
- message: temp
1421
- ? `Codex home MCP --root points at a temp/upgrade path (${rootArg})`
1422
- : wrongRoot
1423
- ? `Codex home MCP --root is not this project (${rootArg || 'missing'} ≠ ${absRoot})`
1424
- : `Codex home MCP should use a single ${PREFERRED_MCP_BIN} bin with absolute project paths`,
1425
- fix: arkCommand(
1426
- root,
1427
- 'ark-check',
1428
- '--install-agent-gates --codex-home --force'
1429
- ),
1246
+ id: assessed.gap.id,
1247
+ severity: assessed.gap.severity,
1248
+ message: assessed.gap.message,
1249
+ fix: arkCommand(root, 'ark-check', assessed.gap.fixArgs),
1430
1250
  });
1431
1251
  }
1432
1252
  }
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Architecture check pipeline: content scan → import graph → layer edges → cycles.
3
+ * Extracted from ark-check entry (R3). Entry remains orchestration + presentation.
4
+ */
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import {
8
+ collectForbiddenGlobalUses,
9
+ layerForFile,
10
+ looksLikeIntent,
11
+ } from '../ark-shared.mjs';
12
+ import {
13
+ isTypeOnlyModuleReference,
14
+ isArkPublishCandidate,
15
+ isPublishCall,
16
+ lineOf,
17
+ moduleSpecifierFromCall,
18
+ namedModuleBindings,
19
+ objectHasProperty,
20
+ publishHasSource,
21
+ publishSourceLiteral,
22
+ sourceFileExportsOnlyTypes,
23
+ sourceFileHasTopLevelSideEffects,
24
+ stringLiteralText,
25
+ textOfModuleSpecifier,
26
+ typeOnlyExportNames,
27
+ } from './ast-scan.mjs';
28
+ import {
29
+ intentLayersFromManifest,
30
+ layerForIntent,
31
+ isBlocked,
32
+ collectConfigWarnings,
33
+ } from './config-warnings.mjs';
34
+ import { detectCycles } from './graph-cycles.mjs';
35
+ import { normalize } from './scan-files.mjs';
36
+ import {
37
+ createCompilerOptionsLookup,
38
+ createModuleResolutionHost,
39
+ loadScanCache,
40
+ resolveImport,
41
+ saveScanCache,
42
+ scanCacheKey,
43
+ } from './ts-resolve.mjs';
44
+
45
+ /**
46
+ * Parse one governed source file into content violations + module edges.
47
+ */
48
+ export function scanSourceFile(ts, root, config, rules, manifestIntentLayers, file, sourceLayer) {
49
+ const source = fs.readFileSync(file, 'utf8');
50
+ const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true);
51
+ const violations = [];
52
+ const edges = [];
53
+
54
+ const layerConfig = config.layers.find((layer) => layer.name === sourceLayer);
55
+ const forbiddenGlobals = Array.isArray(layerConfig?.forbiddenGlobals)
56
+ ? layerConfig.forbiddenGlobals.filter((entry) => typeof entry === 'string')
57
+ : [];
58
+ for (const use of collectForbiddenGlobalUses(ts, sourceFile, forbiddenGlobals)) {
59
+ violations.push({
60
+ ruleId: 'FORBIDDEN_GLOBAL',
61
+ file: normalize(path.relative(root, file)),
62
+ line: lineOf(sourceFile, use.node.getStart(sourceFile)),
63
+ fromLayer: sourceLayer,
64
+ target: use.name,
65
+ message: `${sourceLayer} must not use the ambient global "${use.name}".`,
66
+ });
67
+ }
68
+
69
+ const checkModuleEdge = (specifier, node, kind, typeOnly = false) => {
70
+ const namedBindings = namedModuleBindings(ts, node);
71
+ edges.push({
72
+ specifier,
73
+ line: lineOf(sourceFile, node.getStart(sourceFile)),
74
+ kind,
75
+ typeOnly,
76
+ ...(namedBindings ? { namedBindings } : {}),
77
+ });
78
+ };
79
+
80
+ const visit = (node) => {
81
+ if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
82
+ const specifier = textOfModuleSpecifier(node);
83
+ if (specifier) {
84
+ checkModuleEdge(
85
+ specifier,
86
+ node,
87
+ ts.isImportDeclaration(node) ? 'import' : 'export',
88
+ isTypeOnlyModuleReference(ts, node)
89
+ );
90
+ }
91
+ }
92
+
93
+ if (ts.isCallExpression(node)) {
94
+ const moduleCall = moduleSpecifierFromCall(ts, node);
95
+ if (moduleCall) {
96
+ checkModuleEdge(moduleCall.value, node, moduleCall.kind);
97
+ }
98
+
99
+ if (isPublishCall(ts, node)) {
100
+ const firstArg = node.arguments[0];
101
+ const rawIntent = stringLiteralText(ts, firstArg);
102
+ if (
103
+ (rawIntent && looksLikeIntent(rawIntent)) ||
104
+ objectHasProperty(ts, firstArg, 'intent')
105
+ ) {
106
+ violations.push({
107
+ ruleId: 'RAW_EVENT_PUBLISH',
108
+ file: normalize(path.relative(root, file)),
109
+ line: lineOf(sourceFile, node.getStart(sourceFile)),
110
+ message:
111
+ 'Publish through a registered intent creator; raw event objects or intent strings bypass Ark contracts and tooling.',
112
+ });
113
+ }
114
+
115
+ if (isArkPublishCandidate(ts, node) && !publishHasSource(ts, node)) {
116
+ violations.push({
117
+ ruleId: 'PUBLISH_MISSING_SOURCE',
118
+ file: normalize(path.relative(root, file)),
119
+ line: lineOf(sourceFile, node.getStart(sourceFile)),
120
+ fromLayer: sourceLayer,
121
+ message: 'Strict Ark publish calls must include metadata.source.',
122
+ });
123
+ }
124
+
125
+ const sourceIntent = publishSourceLiteral(ts, node);
126
+ if (sourceIntent && looksLikeIntent(sourceIntent)) {
127
+ const sourceIntentLayer = layerForIntent(
128
+ sourceIntent,
129
+ config.layers,
130
+ manifestIntentLayers
131
+ );
132
+ if (sourceIntentLayer && sourceIntentLayer !== sourceLayer) {
133
+ violations.push({
134
+ ruleId: 'PUBLISH_SOURCE_LAYER_MISMATCH',
135
+ file: normalize(path.relative(root, file)),
136
+ line: lineOf(sourceFile, node.getStart(sourceFile)),
137
+ fromLayer: sourceLayer,
138
+ toLayer: sourceIntentLayer,
139
+ target: sourceIntent,
140
+ message:
141
+ `Publish source "${sourceIntent}" resolves to ${sourceIntentLayer}, but the publishing file is classified as ${sourceLayer}.`,
142
+ });
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ if (ts.isStringLiteralLike(node) && looksLikeIntent(node.text)) {
149
+ const targetLayer = layerForIntent(node.text, config.layers, manifestIntentLayers);
150
+ const rule = targetLayer ? isBlocked(rules, sourceLayer, targetLayer) : undefined;
151
+ if (rule) {
152
+ violations.push({
153
+ ruleId: 'LAYER_INTENT_REFERENCE_VIOLATION',
154
+ file: normalize(path.relative(root, file)),
155
+ line: lineOf(sourceFile, node.getStart(sourceFile)),
156
+ fromLayer: sourceLayer,
157
+ toLayer: targetLayer,
158
+ target: node.text,
159
+ message:
160
+ rule.message ??
161
+ `${sourceLayer} must not reference ${targetLayer} intent ${node.text}.`,
162
+ });
163
+ }
164
+ }
165
+
166
+ ts.forEachChild(node, visit);
167
+ };
168
+ visit(sourceFile);
169
+ return {
170
+ contentViolations: violations,
171
+ edges,
172
+ exportsOnlyTypes: sourceFileExportsOnlyTypes(ts, sourceFile),
173
+ typeOnlyExportNames: typeOnlyExportNames(ts, sourceFile),
174
+ hasTopLevelSideEffects: sourceFileHasTopLevelSideEffects(ts, sourceFile),
175
+ };
176
+ }
177
+
178
+ /**
179
+ * Full architecture scan for governed files.
180
+ * @returns {{ violations: object[], warnings: object[] }}
181
+ */
182
+ export function runArchitectureScan({ root, config, manifest, rules, files, ts, args }) {
183
+ const manifestIntentLayers = intentLayersFromManifest(manifest);
184
+ const compilerOptionsFor = createCompilerOptionsLookup(ts, root, args.tsconfig);
185
+ const moduleHost = createModuleResolutionHost(ts);
186
+
187
+ const violations = [];
188
+ const warnings = collectConfigWarnings(root, config, files, rules, manifest);
189
+ const cacheKey = args.noCache ? undefined : scanCacheKey(root, args);
190
+ const cachedFiles = cacheKey ? loadScanCache(root, cacheKey) : undefined;
191
+ const nextCacheFiles = {};
192
+
193
+ const importGraph = new Map();
194
+ const scanned = [];
195
+ for (const file of files) {
196
+ const sourceLayer = layerForFile(root, file, config.layers);
197
+ if (!sourceLayer) continue;
198
+ const relFile = normalize(path.relative(root, file));
199
+ if (!importGraph.has(relFile)) importGraph.set(relFile, new Set());
200
+ const stat = fs.statSync(file);
201
+ const fileKey = `${stat.mtimeMs}:${stat.size}`;
202
+ const cached = cachedFiles?.[relFile];
203
+ const entry =
204
+ cached && cached.fileKey === fileKey
205
+ ? cached
206
+ : {
207
+ fileKey,
208
+ ...scanSourceFile(
209
+ ts,
210
+ root,
211
+ config,
212
+ rules,
213
+ manifestIntentLayers,
214
+ file,
215
+ sourceLayer
216
+ ),
217
+ };
218
+ nextCacheFiles[relFile] = entry;
219
+ scanned.push({ file, sourceLayer, relFile, entry });
220
+ }
221
+
222
+ for (const { file, sourceLayer, relFile, entry } of scanned) {
223
+ violations.push(...entry.contentViolations);
224
+ for (const edge of entry.edges) {
225
+ const target = resolveImport(
226
+ ts,
227
+ edge.specifier,
228
+ file,
229
+ compilerOptionsFor(file),
230
+ moduleHost,
231
+ root
232
+ );
233
+ const targetLayer = target ? layerForFile(root, target, config.layers) : undefined;
234
+ if (target && targetLayer) {
235
+ const relTarget = normalize(path.relative(root, target));
236
+ if (relTarget !== relFile && !edge.typeOnly) {
237
+ importGraph.get(relFile).add(relTarget);
238
+ }
239
+ }
240
+ const rule = targetLayer ? isBlocked(rules, sourceLayer, targetLayer) : undefined;
241
+ if (rule) {
242
+ const relTarget = normalize(path.relative(root, target));
243
+ const targetCached = nextCacheFiles[relTarget];
244
+ const staticEdge = edge.kind === 'import' || edge.kind === 'export';
245
+ const targetTypeOnlyExports =
246
+ staticEdge && Boolean(targetCached?.exportsOnlyTypes) && !edge.typeOnly;
247
+ const sourcePureTypeModule = Boolean(entry.exportsOnlyTypes);
248
+ // R6: every named binding is a type-only export of the target (mixed modules OK).
249
+ // Conservative: no dual-space value names, no top-level side effects on target
250
+ // (import type would skip evaluation), no default/namespace/side-effect/export*.
251
+ const targetTypeNames = new Set(targetCached?.typeOnlyExportNames || []);
252
+ const named = edge.namedBindings;
253
+ const namedBindingsTypeOnly =
254
+ staticEdge &&
255
+ Array.isArray(named) &&
256
+ named.length > 0 &&
257
+ targetTypeNames.size > 0 &&
258
+ !targetCached?.hasTopLevelSideEffects &&
259
+ named.every((n) => targetTypeNames.has(n));
260
+ violations.push({
261
+ ruleId: 'LAYER_IMPORT_VIOLATION',
262
+ file: relFile,
263
+ line: edge.line,
264
+ fromLayer: sourceLayer,
265
+ toLayer: targetLayer,
266
+ target: relTarget,
267
+ ...(edge.typeOnly ? { typeOnly: true } : {}),
268
+ ...(targetTypeOnlyExports ? { targetTypeOnlyExports: true } : {}),
269
+ ...(sourcePureTypeModule ? { sourcePureTypeModule: true } : {}),
270
+ ...(namedBindingsTypeOnly ? { namedBindingsTypeOnly: true } : {}),
271
+ ...(edge.kind ? { edgeKind: edge.kind } : {}),
272
+ message: rule.message ?? `${sourceLayer} must not ${edge.kind} ${targetLayer}.`,
273
+ });
274
+ }
275
+ }
276
+ }
277
+
278
+ if (cacheKey) saveScanCache(root, cacheKey, nextCacheFiles);
279
+
280
+ const cyclePolicy = String(config.cyclePolicy || 'strict').toLowerCase();
281
+ if (cyclePolicy !== 'off') {
282
+ const cycles = detectCycles(importGraph);
283
+ if (cyclePolicy === 'soft' || cyclePolicy === 'framework-soft') {
284
+ for (const c of cycles) {
285
+ warnings.push({
286
+ ruleId: 'CIRCULAR_DEPENDENCY',
287
+ message: `${c.message} (soft cycle policy — advisory only; set cyclePolicy: "strict" to fail the check)`,
288
+ file: c.file,
289
+ target: c.target,
290
+ failsStrict: false,
291
+ });
292
+ }
293
+ } else {
294
+ violations.push(...cycles);
295
+ }
296
+ }
297
+
298
+ return { violations, warnings };
299
+ }