File size: 9,745 Bytes
dfd38de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**

 * backtick-shell.mjs β€” Sovereign Shell Execution

 * BOB Reasoning Engine Β· Agent capability layer

 *

 * Standard AI shell execution is flawed:

 *   exec(cmd) β†’ string dump β†’ no gate β†’ no seal β†’ hallucinations pass through

 *

 * Sovereign backtick execution:

 *   command β†’ EDAULC trust check β†’ backtick composition β†’ entropy gate β†’ WORM seal

 *

 * The backtick is the sovereign shell primitive:

 *   compositional  β€” output of one command becomes input to the next

 *   deterministic  β€” same command, same output, auditable

 *   chainable      β€” pipes naturally into WORM seal

 *   gated          β€” hallucinated commands fail trust check before execution

 *

 * Author: Ahmad Ali Parr Β· SnapKitty Collective Β· 2026

 */

import { execSync }    from 'child_process'
import { createHash }  from 'crypto'

const PHI             = (1 + Math.sqrt(5)) / 2
const ENTROPY_GATE    = 0.21
const MAX_OUTPUT_BYTES = 64 * 1024  // 64KB cap β€” no runaway output

// ── Seal ──────────────────────────────────────────────────────────────────────

function seal (content) {
  return createHash('sha256').update(String(content)).digest('hex').slice(0, 16)
}

function full_hash (content) {
  return createHash('sha256').update(String(content)).digest('hex')
}

// ── EDAULC Trust Check β€” command must pass before execution ───────────────────

const BLOCKED_PATTERNS = [
  /rm\s+-rf/,           // destructive delete
  />\s*\/dev\/sd/,      // disk overwrite
  /mkfs/,               // format
  /dd\s+if=/,           // disk dump
  /curl.*\|\s*sh/,      // pipe to shell
  /wget.*\|\s*sh/,      // pipe to shell
  /chmod\s+777/,        // world-writable
  /sudo\s+rm/,          // privileged delete
  /:\(\)\{.*\}/,        // fork bomb
  /base64.*\|\s*sh/,    // obfuscated exec
]

const SOVEREIGN_PATTERNS = [
  /^git\s/,             // git operations β€” trusted
  /^grep\s/,            // search β€” trusted
  /^find\s/,            // find β€” trusted
  /^cat\s/,             // read β€” trusted
  /^ls\s*/,             // list β€” trusted
  /^node\s/,            // node execution β€” trusted
  /^curl\s/,            // curl β€” conditionally trusted (checked below)
]

function edaulc_command_trust (cmd) {
  // Block dangerous patterns regardless
  for (const pattern of BLOCKED_PATTERNS) {
    if (pattern.test(cmd)) {
      return {
        trusted: false,
        reason:  `BLOCKED: matches dangerous pattern ${pattern}`,
        score:   0.0,
      }
    }
  }

  // curl is allowed but not piped to shell
  if (/curl/.test(cmd) && /\|\s*(sh|bash|zsh)/.test(cmd)) {
    return { trusted: false, reason: 'BLOCKED: curl piped to shell', score: 0.0 }
  }

  // Score trust vector
  const is_sovereign = SOVEREIGN_PATTERNS.some(p => p.test(cmd.trim()))
  const has_pipe     = cmd.includes('|')
  const has_backtick = cmd.includes('`')
  const length_ok    = cmd.length < 512

  const trust_vector = {
    coherence:                length_ok ? 0.9 : 0.4,
    provenance:               1.0,          // agent invoked explicitly
    reversibility:            is_sovereign ? 0.9 : 0.5,
    consent:                  1.0,          // agent called this
    auditability:             1.0,          // command string is fully visible
    semantic_alignment:       is_sovereign ? 1.0 : 0.6,
    contradiction_resistance: 1.0,          // no sorry in a shell command
  }

  const norm  = Math.sqrt(Object.values(trust_vector).reduce((s, v) => s + v * v, 0))
  const score = norm / Math.sqrt(7)

  return { trusted: score > 0.75, score, trust_vector, is_sovereign, has_pipe, has_backtick }
}

// ── Entropy Gate ──────────────────────────────────────────────────────────────

function entropy_gate (score) {
  const p    = Math.max(score, 1e-10)
  const q    = Math.max(1 - score, 1e-10)
  const H    = -(p * Math.log(p) + q * Math.log(q)) / Math.log(PHI)
  return { entropy: H, open: H < ENTROPY_GATE }
}

// ── Backtick Composition ──────────────────────────────────────────────────────
// Executes the command using actual shell backtick semantics:
// the output is captured inline, trimmed, ready for composition.

function backtick (cmd, opts = {}) {
  const {
    timeout  = 10000,
    cwd      = process.cwd(),
    env      = process.env,
  } = opts

  try {
    const raw = execSync(cmd, {
      encoding:   'utf8',
      timeout,
      cwd,
      env,
      shell:      true,
      maxBuffer:  MAX_OUTPUT_BYTES,
    })
    return { ok: true, output: raw.trim(), exit_code: 0 }
  } catch (err) {
    return {
      ok:        false,
      output:    (err.stdout || '').trim(),
      stderr:    (err.stderr || '').trim(),
      exit_code: err.status || 1,
      error:     err.message.slice(0, 200),
    }
  }
}

// ── Backtick Compose β€” chain output into next command ─────────────────────────
// Equivalent to: result=`cmd1 | cmd2`
// Executes the full pipeline and returns composed output.

function backtick_compose (...cmds) {
  const pipeline = cmds.join(' | ')
  return backtick(pipeline)
}

// ── WORM Seal ─────────────────────────────────────────────────────────────────

function worm_seal_exec (agent_id, cmd, trust, gate, result) {
  const content = JSON.stringify({
    agent_id,
    cmd,
    trust_score:  trust.score,
    entropy:      gate.entropy,
    exit_code:    result.exit_code,
    output_hash:  full_hash(result.output || ''),
    timestamp:    new Date().toISOString(),
  })

  const state_hash = full_hash(content)
  const worm       = seal(content)

  return {
    action_id:    `shell-exec-${worm}`,
    agent_id,
    cmd,
    trust_score:  trust.score,
    entropy:      gate.entropy,
    exit_code:    result.exit_code,
    ok:           result.ok,
    output_lines: (result.output || '').split('\n').length,
    state_hash,
    worm_seal:    worm,
    append_only:  true,
    timestamp:    new Date().toISOString(),
  }
}

// ── Sovereign Execute β€” full pipeline ─────────────────────────────────────────

export async function sovereign_exec (agent_id, cmd, opts = {}) {
  // 1. EDAULC trust check
  const trust = edaulc_command_trust(cmd)
  if (!trust.trusted) {
    return {
      ok:      false,
      blocked: true,
      reason:  trust.reason,
      cmd,
      receipt: null,
    }
  }

  // 2. Entropy gate
  const gate = entropy_gate(trust.score)
  if (!gate.open) {
    return {
      ok:      false,
      blocked: true,
      reason:  `Entropy gate closed: H=${gate.entropy.toFixed(4)} β‰₯ ${ENTROPY_GATE}`,
      cmd,
      receipt: null,
    }
  }

  // 3. Backtick execution
  const result = backtick(cmd, opts)

  // 4. WORM seal
  const receipt = worm_seal_exec(agent_id, cmd, trust, gate, result)

  return {
    ok:      result.ok,
    blocked: false,
    output:  result.output,
    stderr:  result.stderr,
    receipt,
  }
}

// ── Agent-facing shortcuts ─────────────────────────────────────────────────────

export const SHELL = {
  // Sovereign grep β€” search only committed files
  grep: (pattern, glob = '') => sovereign_exec(
    'SENTINEL',
    glob
      ? `grep -r "${pattern}" \`git ls-files "${glob}"\``
      : `grep -rn "${pattern}" \`git ls-files\``,
  ),

  // Sovereign find β€” scoped to repo
  find: (name_pattern) => sovereign_exec(
    'ATLAS',
    `find . -name "${name_pattern}" -not -path "*/node_modules/*" -not -path "*/.git/*"`,
  ),

  // Sovereign git log
  log: (n = 10) => sovereign_exec(
    'MNEMEX',
    `git log --oneline -${n}`,
  ),

  // Sovereign curl β€” GET only, no pipe to shell
  curl: (url) => sovereign_exec(
    'HERALD',
    `curl -s --max-time 10 "${url}"`,
  ),

  // Backtick compose β€” pipeline
  compose: (...cmds) => {
    const pipeline = cmds.join(' | ')
    return sovereign_exec('BOB', pipeline)
  },
}

// ── CLI demo ──────────────────────────────────────────────────────────────────

if (process.argv[1].endsWith('backtick-shell.mjs')) {
  const demos = [
    ['BOB',      'git ls-files "*.mjs" | head -5'],
    ['SENTINEL', 'grep -rn "WORM" `git ls-files "*.mjs"` | head -5'],
    ['ATLAS',    'find . -name "*.apl" -not -path "*/node_modules/*"'],
    ['CIPHER',   'rm -rf /tmp/test'],  // should be BLOCKED
  ]

  for (const [agent, cmd] of demos) {
    const r = await sovereign_exec(agent, cmd)
    console.log(`\n[${agent}] ${cmd}`)
    if (r.blocked) {
      console.log(`  βŠ₯ BLOCKED β€” ${r.reason}`)
    } else {
      console.log(`  βœ“ seal: ${r.receipt?.worm_seal}`)
      console.log(`  output: ${(r.output || '').split('\n').slice(0, 3).join(' | ')}`)
    }
  }
}