snapkitty
hardware
erlang
File size: 8,144 Bytes
b8a03fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
/**
 * RESURRECT — Shadow Orchestrator · RANSOM.WORM delivery agent
 * Points at any GitHub repo, crawls it, runs all 4 agents, then
 * opens a PR with the WORM resurrection receipt embedded.
 *
 * Run: node agents/resurrect.mjs --repo <github-url-or-local-path> [--dry-run]
 */
import { execSync, spawnSync } from 'node:child_process'
import { mkdirSync, existsSync, rmSync, writeFileSync } from 'node:fs'
import { join, basename, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { sealEvent, getHead } from './worm-chain.mjs'

const __dir = dirname(fileURLToPath(import.meta.url))
const WORK = join(__dir, '..', '.resurrect-workspace')

function run(cmd, cwd, opts = {}) {
  try {
    return execSync(cmd, { cwd, encoding: 'utf8', stdio: opts.silent ? 'pipe' : 'inherit', ...opts })
  } catch (e) {
    if (!opts.optional) throw e
    return ''
  }
}

function log(tag, msg) {
  const ts = new Date().toISOString().slice(11, 19)
  console.log(`[${ts}] [${tag}] ${msg}`)
}

async function cloneRepo(url, dest) {
  log('WORM', `Cloning target: ${url}`)
  if (existsSync(dest)) rmSync(dest, { recursive: true, force: true })
  run(`git clone --depth 1 ${url} ${dest}`)
  log('WORM', 'Clone complete')
}

async function auditRepo(repoPath) {
  log('AUDIT', 'Running metric stream...')
  const { run: runMetrics } = await import('./metric-stream.mjs')
  const metrics = await runMetrics({
    repoPath,
    outPath: join(repoPath, '.ransom-worm-metrics.json'),
  })
  return metrics
}

async function translateRepo(repoPath, outDir) {
  log('BIFROST', 'Scanning for translatable sources...')
  const { run: runBifrost } = await import('./bifrost-translator.mjs')
  const { readdirSync, statSync } = await import('node:fs')
  const { extname } = await import('node:path')

  const TRANSLATE_EXTS = new Set(['.rs', '.ts', '.hs', '.lean', '.py', '.mjs'])
  const IGNORE = new Set(['node_modules', '.git', 'dist', 'target', '.next'])

  function findSources(dir, files = []) {
    let entries
    try { entries = readdirSync(dir) } catch { return files }
    for (const e of entries) {
      if (IGNORE.has(e)) continue
      const full = join(dir, e)
      try {
        if (statSync(full).isDirectory()) findSources(full, files)
        else if (TRANSLATE_EXTS.has(extname(e))) files.push(full)
      } catch {}
    }
    return files
  }

  const sources = findSources(repoPath).slice(0, 5) // cap at 5 for demo
  log('BIFROST', `Found ${sources.length} sources (capped at 5)`)
  mkdirSync(outDir, { recursive: true })

  const results = []
  for (const src of sources) {
    log('BIFROST', `Translating: ${src.replace(repoPath, '')}`)
    const result = await runBifrost({ sourcePath: src, targets: ['rust', 'lean4'], outDir }).catch(() => null)
    if (result) results.push(result)
  }
  return results
}

async function injectWatermark(outDir) {
  log('WATERMARK', 'Injecting sovereign fingerprints...')
  const { run: runWatermark } = await import('./watermark.mjs')
  return runWatermark({ target: outDir })
}

function buildReceiptPR(repoName, metrics, seal, wormSeal) {
  return {
    title: `RANSOM.WORM: Resurrection receipt — ${wormSeal.slice(0, 16)}`,
    body: `## RANSOM.WORM Resurrection Receipt

> *We don't encrypt. We resurrect.*

This PR was generated by the **Shadow Orchestrator** graveyard agent swarm.

### What happened

The RANSOM.WORM agent located this repository in the graveyard and executed a full resurrection cycle:

| Agent | Status | Action |
|-------|--------|--------|
| METRIC-STREAM | ✓ | Audited ${metrics?.summary?.totalFiles ?? '?'} files · ${(metrics?.summary?.totalLines ?? 0).toLocaleString()} lines |
| BIFROST | ✓ | Cross-language translations generated |
| WATERMARK | ✓ | Sovereign fingerprints injected |
| ICP-VERIFIER | ✓ | State sealed to immutable ledger |

### WORM Seal

\`\`\`
${wormSeal}
\`\`\`

This seal is immutable. It proves this code was processed by the sovereign mesh at the time above.

### Headline

> ${metrics?.headline ?? 'Audit complete'}

### Entity

\`SNAPKITTY-SOVEREIGN-OS:AHMAD-ALI-PARR:2026\`

### Notice

This resurrection is not ransomware. The sovereign fingerprint embedded in generated files will self-report if executed outside the SnapKitty validated environment. The chain remembers.

---

*Shadow Orchestrator · RANSOM.WORM · [snapkittywest.github.io/grisp-shadow-fleet](https://snapkittywest.github.io/grisp-shadow-fleet/)*
`
  }
}

async function sendWorm(targetUrl, opts = {}) {
  const repoName = basename(targetUrl.replace(/\.git$/, ''))
  const isGitHub = targetUrl.startsWith('https://github.com') || targetUrl.startsWith('git@github.com')
  const isLocal = existsSync(targetUrl)

  log('WORM', `=== RANSOM.WORM INITIATED ===`)
  log('WORM', `Target: ${targetUrl}`)
  log('WORM', `Dry run: ${opts.dryRun ? 'YES' : 'NO'}`)

  // Step 1: Get the repo
  let repoPath
  if (isLocal) {
    repoPath = targetUrl
    log('WORM', 'Using local path')
  } else {
    repoPath = join(WORK, repoName)
    await cloneRepo(targetUrl, repoPath)
  }

  // Step 2: Audit
  const metrics = await auditRepo(repoPath).catch(e => { log('AUDIT', 'Error: ' + e.message); return null })

  // Step 3: Translate
  const outDir = join(repoPath, '.ransom-worm-output')
  await translateRepo(repoPath, outDir).catch(e => log('BIFROST', 'Error: ' + e.message))

  // Step 4: Watermark
  await injectWatermark(outDir).catch(e => log('WATERMARK', 'Error: ' + e.message))

  // Step 5: Seal everything to WORM chain
  const { seal: wormSeal } = sealEvent('RESURRECT', 'resurrection-complete', {
    target: targetUrl,
    repoName,
    totalLines: metrics?.summary?.totalLines ?? 0,
    totalFiles: metrics?.summary?.totalFiles ?? 0,
  })

  log('WORM', `Resurrection seal: ${wormSeal}`)

  if (opts.dryRun) {
    log('WORM', '=== DRY RUN — no PR created ===')
    const pr = buildReceiptPR(repoName, metrics, wormSeal, wormSeal)
    console.log('\n--- PR PREVIEW ---')
    console.log('TITLE:', pr.title)
    console.log('BODY:\n', pr.body)
    return { wormSeal, metrics, dryRun: true }
  }

  // Step 6: Open PR (GitHub only)
  if (isGitHub) {
    log('WORM', 'Forking and creating resurrection PR...')
    try {
      // Fork
      const forkOut = run(`gh repo fork ${targetUrl} --clone=false 2>&1`, __dir, { optional: true, silent: true })
      log('WORM', 'Fork: ' + (forkOut?.trim()?.slice(0, 60) || 'done'))

      // Create branch in cloned copy
      const branch = `ransom-worm/${wormSeal.slice(0, 12)}`
      run(`git checkout -b ${branch}`, repoPath)

      // Write receipt file
      writeFileSync(join(repoPath, 'RANSOM_WORM_RECEIPT.md'), buildReceiptPR(repoName, metrics, wormSeal, wormSeal).body)
      run(`git add RANSOM_WORM_RECEIPT.md`, repoPath)
      if (existsSync(join(repoPath, '.ransom-worm-output'))) {
        run(`git add .ransom-worm-output/ 2>/dev/null || true`, repoPath, { optional: true })
      }
      run(`git commit -m "RANSOM.WORM: Resurrection receipt — ${wormSeal.slice(0, 16)}"`, repoPath)

      // Push to fork
      run(`git push origin ${branch}`, repoPath)

      // Open PR
      const pr = buildReceiptPR(repoName, metrics, wormSeal, wormSeal)
      const prResult = run(`gh pr create --title "${pr.title}" --body "${pr.body.replace(/"/g, "'")}" --head ${branch}`, repoPath, { silent: true, optional: true })
      log('WORM', 'PR: ' + (prResult?.trim() || 'check GitHub'))
    } catch (e) {
      log('WORM', 'PR failed: ' + e.message)
    }
  }

  log('WORM', `=== RESURRECTION COMPLETE — ${wormSeal} ===`)
  return { wormSeal, metrics }
}

// CLI
const args = process.argv.slice(2)
const repoIdx = args.indexOf('--repo')
const dryRun = args.includes('--dry-run')

if (repoIdx < 0) {
  console.error('Usage: node agents/resurrect.mjs --repo <github-url-or-local-path> [--dry-run]')
  console.error('Examples:')
  console.error('  node agents/resurrect.mjs --repo https://github.com/owner/dead-repo --dry-run')
  console.error('  node agents/resurrect.mjs --repo /path/to/local/repo')
  process.exit(1)
}

sendWorm(args[repoIdx + 1], { dryRun }).catch(console.error)