File size: 5,259 Bytes
119e586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Gravity-to-Forth renderer.
// Takes a repo topology (from AHMAD-BOT S-expression output)
// and renders it as Forth source β€” the ancient meta-repo format.
// The output IS the inverted documentation. Executable. Sovereign.

export function renderRepoAsForth(repoName, clusters, gravity, gaps) {
  const lines = [];

  lines.push(`\\ INVERTED META-REPO β€” ${repoName}`);
  lines.push(`\\ Generated by AHMAD-BOT + Forth renderer`);
  lines.push(`\\ Gravity: ${gravity} | Gaps: ${gaps.length}`);
  lines.push(`\\ This file IS the repository's architectural truth.`);
  lines.push(`\\ Feed it to the Forth machine. It will reason about the repo.`);
  lines.push('');
  lines.push('( ── CONSTANTS ── )');
  lines.push(`variable repo-gravity`);
  lines.push(`${gravity} repo-gravity !`);
  lines.push('');

  lines.push('( ── CLUSTER WORDS ── )');
  for (const cluster of clusters) {
    const safeName = cluster.id.replace(/[^a-z0-9-]/gi, '-').toLowerCase();
    lines.push(`\\ cluster: ${cluster.id} | gravity: ${cluster.gravity} | status: ${cluster.status}`);
    lines.push(`: ${safeName} ( -- )`);
    lines.push(`  ${cluster.gravity} gravity`);

    if (cluster.status === 'alive') {
      lines.push(`  alive? IF`);
      lines.push(`    ." ${cluster.id} [alive] gravity=${cluster.gravity} " cr`);
      lines.push(`  THEN`);
    } else if (cluster.status === 'broken') {
      lines.push(`  broken? IF`);
      lines.push(`    ." ${cluster.id} [broken] needs repair " cr`);
      lines.push(`    "${cluster.id}" repair`);
      lines.push(`  THEN`);
    } else {
      lines.push(`  orphan? IF`);
      lines.push(`    ." ${cluster.id} [orphan] β€” grave marker " cr`);
      lines.push(`    "${cluster.id}" flag`);
      lines.push(`  THEN`);
    }
    lines.push(`;`);
    lines.push('');
  }

  lines.push('( ── GAP WORDS ── )');
  for (const gap of gaps) {
    const safeName = `gap-${gap.type}-${gap.path.replace(/[^a-z0-9]/gi, '-').toLowerCase()}`.slice(0, 40);
    lines.push(`: ${safeName} ( -- )`);
    lines.push(`  ." GAP [${gap.type}] @ ${gap.path} " cr`);
    lines.push(`  ." FIX: ${gap.fix} " cr`);
    if (['missing_wire', 'dead_page'].includes(gap.type)) {
      lines.push(`  "${gap.path}" flag`);
    } else {
      lines.push(`  "${gap.path}" repair`);
    }
    lines.push(`;`);
    lines.push('');
  }

  lines.push('( ── INVERSION SEQUENCE ── )');
  lines.push(`: run-inversion ( -- )`);
  lines.push(`  ." === INVERTING ${repoName} ===" cr`);
  lines.push(`  repo-gravity @`);
  lines.push(`  dup 0.6 > IF`);
  lines.push(`    ." repo is alive β€” inverting to sovereign format " cr`);
  lines.push(`    "${repoName}" invert-repo`);
  lines.push(`  ELSE`);
  lines.push(`    dup 0.3 > IF`);
  lines.push(`      ." repo is broken β€” repair before inversion " cr`);
  lines.push(`    ELSE`);
  lines.push(`      ." repo is orphan β€” grave marker set " cr`);
  lines.push(`    THEN`);
  lines.push(`  THEN`);
  lines.push(`  drop`);

  // Call all cluster words
  for (const cluster of clusters) {
    const safeName = cluster.id.replace(/[^a-z0-9-]/gi, '-').toLowerCase();
    lines.push(`  ${safeName}`);
  }

  // Call all gap words
  for (const gap of gaps) {
    const safeName = `gap-${gap.type}-${gap.path.replace(/[^a-z0-9]/gi, '-').toLowerCase()}`.slice(0, 40);
    lines.push(`  ${safeName}`);
  }

  lines.push(`  "${repoName}" seal`);
  lines.push(`;`);
  lines.push('');
  lines.push('( ── ENTRY POINT ── )');
  lines.push('run-inversion');

  return lines.join('\n');
}

// Render a full org crawl as a multi-repo Forth suite
export function renderOrgAsForth(org, repos) {
  const sections = [];

  sections.push(`\\ GRAVEYARD MAP β€” ${org}`);
  sections.push(`\\ ${repos.length} repos | rendered by AHMAD-BOT + Forth renderer`);
  sections.push(`\\ The graveyard in Forth. Every repo is a word.`);
  sections.push('');

  for (const repo of repos) {
    const name = repo.name.replace(/[^a-z0-9-]/gi, '-').toLowerCase();
    sections.push(`\\ ── ${repo.name} (gravity: ${repo.gravity}, status: ${repo.status}) ──`);
    sections.push(`: crawl-${name} ( -- )`);
    sections.push(`  ${repo.gravity} gravity`);
    sections.push(`  dup alive? IF`);
    sections.push(`    ." ${repo.name} alive " cr`);
    sections.push(`  ELSE dup broken? IF`);
    sections.push(`    ." ${repo.name} broken " cr`);
    sections.push(`    "${repo.name}" repair`);
    sections.push(`  ELSE`);
    sections.push(`    ." ${repo.name} orphan " cr`);
    sections.push(`    "${repo.name}" flag`);
    sections.push(`  THEN THEN`);
    sections.push(`  drop`);
    sections.push(`;`);
    sections.push('');
  }

  // Master crawl word
  sections.push(`: crawl-graveyard ( -- )`);
  sections.push(`  ." === ${org} GRAVEYARD CRAWL ===" cr`);
  for (const repo of repos) {
    const name = repo.name.replace(/[^a-z0-9-]/gi, '-').toLowerCase();
    sections.push(`  crawl-${name}`);
  }
  sections.push(`  ." === CRAWL COMPLETE ===" cr`);
  sections.push(`;`);
  sections.push('');
  sections.push('crawl-graveyard');

  return sections.join('\n');
}