omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / cli / src / stack.ts
| 1 | import { execFileSync, spawnSync } from 'node:child_process'; |
| 2 | import chalk from 'chalk'; |
| 3 | import { SYMBOL } from './brand.js'; |
| 4 | import { probeInkling } from './inkling.js'; |
| 5 | |
| 6 | export interface StackOptions { |
| 7 | sparkHost: string; |
| 8 | endpoint: string; |
| 9 | apiKey?: string; |
| 10 | } |
| 11 | |
| 12 | function getPortFromEndpoint(endpoint: string): number { |
| 13 | try { |
| 14 | const u = new URL(endpoint); |
| 15 | return u.port ? Number(u.port) : 8080; |
| 16 | } catch { |
| 17 | return 8080; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | const BUILD_REMOTE_SCRIPT = `set -eu |
| 22 | mkdir -p ~/.local/opt/inkling2 |
| 23 | cd ~/canon/omega-mv |
| 24 | ( echo om2 |
| 25 | cat gpu/nvidia/native/nvrm/context.om \\ |
| 26 | gpu/nvidia/native/blackwell/vector.om \\ |
| 27 | gpu/nvidia/native/blackwell/launch.om \\ |
| 28 | gpu/nvidia/native/blackwell/batch.om \\ |
| 29 | om/encoder/bw_enc_sm121.om \\ |
| 30 | om/gpu/inkling2/emit_fast.om \\ |
| 31 | om/gpu/inkling2/emit_batch.om \\ |
| 32 | om/arboris_online.om \\ |
| 33 | om/gpu/inkling2/arboris/prefetch.om \\ |
| 34 | om/gpu/inkling2/decoder/safetensors.om \\ |
| 35 | om/gpu/inkling2/decoder/tokenizer.om \\ |
| 36 | om/gpu/inkling2/decoder/decode.om \\ |
| 37 | om/gpu/inkling2/multistream/batcher.om \\ |
| 38 | om/gpu/inkling2/decoder/prefill_b8.om \\ |
| 39 | om/gpu/inkling2/multistream/serve_live.om |
| 40 | ) > /tmp/inkling2-serve-build.om |
| 41 | seed/out/omc < /tmp/inkling2-serve-build.om > ~/.local/opt/inkling2/inkling2-serve.new |
| 42 | chmod +x ~/.local/opt/inkling2/inkling2-serve.new |
| 43 | mv ~/.local/opt/inkling2/inkling2-serve.new ~/.local/opt/inkling2/inkling2-serve |
| 44 | rm -f /tmp/inkling2-serve-build.om |
| 45 | sha256sum ~/.local/opt/inkling2/inkling2-serve |
| 46 | ls -lh ~/.local/opt/inkling2/inkling2-serve |
| 47 | `; |
| 48 | |
| 49 | export function rebuildSparkInkling(sparkHost: string): { sha256: string; output: string } { |
| 50 | console.log(chalk.cyan(`${SYMBOL} Rebuilding pure seeded-Omega inkling2-serve (with Arboris + SM121 speedups) on ${sparkHost}…`)); |
| 51 | const out = execFileSync('ssh', ['-o', 'ConnectTimeout=8', sparkHost, BUILD_REMOTE_SCRIPT], { |
| 52 | encoding: 'utf8', |
| 53 | timeout: 60_000, |
| 54 | }); |
| 55 | const sha = out.trim().split(/\s+/)[0] || ''; |
| 56 | return { sha256: sha, output: out.trim() }; |
| 57 | } |
| 58 | |
| 59 | export function ensureTunnel(sparkHost: string, localPort: number, remotePort = 8080): void { |
| 60 | const spec = `127.0.0.1:${localPort}:127.0.0.1:${remotePort}`; |
| 61 | // Check if already listening on localPort |
| 62 | const check = spawnSync('lsof', [`-iTCP:${localPort}`, '-sTCP:LISTEN', '-n', '-P'], { |
| 63 | encoding: 'utf8', |
| 64 | }); |
| 65 | if (check.status === 0 && (check.stdout || '').trim().length > 0) { |
| 66 | return; |
| 67 | } |
| 68 | console.log(chalk.gray(`${SYMBOL} Opening SSH tunnel ${spec} → ${sparkHost}…`)); |
| 69 | execFileSync( |
| 70 | 'ssh', |
| 71 | [ |
| 72 | '-f', |
| 73 | '-N', |
| 74 | '-o', 'ControlMaster=no', |
| 75 | '-o', 'ControlPath=none', |
| 76 | '-o', 'BatchMode=yes', |
| 77 | '-o', 'ExitOnForwardFailure=yes', |
| 78 | '-o', 'ServerAliveInterval=15', |
| 79 | '-o', 'ServerAliveCountMax=3', |
| 80 | '-o', 'ConnectTimeout=8', |
| 81 | '-L', spec, |
| 82 | sparkHost, |
| 83 | ], |
| 84 | { stdio: 'ignore', timeout: 15_000 } |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | export async function stackUp(opts: StackOptions & { rebuild?: boolean }): Promise<boolean> { |
| 89 | const port = getPortFromEndpoint(opts.endpoint); |
| 90 | |
| 91 | // Fast path: already online |
| 92 | const initial = await probeInkling(opts.endpoint, opts.apiKey); |
| 93 | if (initial.online && !opts.rebuild) { |
| 94 | console.log( |
| 95 | chalk.green(`${SYMBOL} Inkling-2-Mini is already ONLINE`) + |
| 96 | chalk.gray(` (${initial.endpoint} · ${initial.engine} · Arboris ACTIVE)`) |
| 97 | ); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // Ensure binary exists on Spark (or rebuild if requested) |
| 102 | if (opts.rebuild) { |
| 103 | const built = rebuildSparkInkling(opts.sparkHost); |
| 104 | console.log(chalk.gray(` sha256: ${built.sha256}`)); |
| 105 | } else { |
| 106 | const hasBin = spawnSync( |
| 107 | 'ssh', |
| 108 | ['-o', 'ConnectTimeout=8', opts.sparkHost, 'test -x ~/.local/opt/inkling2/inkling2-serve'], |
| 109 | { stdio: 'ignore', timeout: 10_000 } |
| 110 | ); |
| 111 | if (hasBin.status !== 0) { |
| 112 | const built = rebuildSparkInkling(opts.sparkHost); |
| 113 | console.log(chalk.gray(` sha256: ${built.sha256}`)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Check if inkling2-serve is already running on Spark |
| 118 | const startCmd = `set -eu |
| 119 | if [ -f /tmp/inkling2-serve.pid ] && kill -0 $(cat /tmp/inkling2-serve.pid) 2>/dev/null; then |
| 120 | if [ "${opts.rebuild ? '1' : '0'}" = "1" ]; then |
| 121 | kill $(cat /tmp/inkling2-serve.pid) 2>/dev/null || true |
| 122 | sleep 4 |
| 123 | else |
| 124 | echo "ALREADY_RUNNING=$(cat /tmp/inkling2-serve.pid)" |
| 125 | exit 0 |
| 126 | fi |
| 127 | fi |
| 128 | setsid -f ~/.local/opt/inkling2/inkling2-serve </dev/null >/tmp/inkling2-serve.log 2>&1 |
| 129 | sleep 1 |
| 130 | PID=$(pgrep -n -x inkling2-serve || true) |
| 131 | echo "$PID" > /tmp/inkling2-serve.pid |
| 132 | echo "STARTED=$PID" |
| 133 | `; |
| 134 | |
| 135 | console.log(chalk.cyan(`${SYMBOL} Starting pure-Omega inkling2-serve (GB10 zero-CUDA + Arboris) on ${opts.sparkHost}…`)); |
| 136 | const startOut = execFileSync('ssh', ['-o', 'ConnectTimeout=8', opts.sparkHost, startCmd], { |
| 137 | encoding: 'utf8', |
| 138 | timeout: 20_000, |
| 139 | }).trim(); |
| 140 | console.log(chalk.gray(` ${startOut}`)); |
| 141 | |
| 142 | ensureTunnel(opts.sparkHost, port, 8080); |
| 143 | |
| 144 | // Wait for Inkling-2-Mini 18.3 GB checkpoint to finish streaming into GB10 UVM |
| 145 | process.stdout.write(chalk.gray(`${SYMBOL} Waiting for Inkling-2-Mini checkpoint load on GB10…`)); |
| 146 | const deadline = Date.now() + 90_000; |
| 147 | while (Date.now() < deadline) { |
| 148 | const h = await probeInkling(opts.endpoint, opts.apiKey); |
| 149 | if (h.online) { |
| 150 | process.stdout.write('\n'); |
| 151 | console.log( |
| 152 | chalk.green(`${SYMBOL} ONLINE: ${h.model || 'inkling-2-mini'}`) + |
| 153 | chalk.gray( |
| 154 | ` · ${h.engine} · ${h.slotsActive ?? 0}/${h.slotsMax ?? 8} KV slots` + |
| 155 | (h.arborisTransitions !== undefined ? ` · Arboris ACTIVE` : '') |
| 156 | ) |
| 157 | ); |
| 158 | return true; |
| 159 | } |
| 160 | process.stdout.write('.'); |
| 161 | await new Promise((r) => setTimeout(r, 2000)); |
| 162 | } |
| 163 | process.stdout.write('\n'); |
| 164 | console.error(chalk.red(`${SYMBOL} Timed out waiting for Inkling-2-Mini at ${opts.endpoint}. Check ssh ${opts.sparkHost} 'cat /tmp/inkling2-serve.log'`)); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | export function stackDown(opts: StackOptions): void { |
| 169 | const port = getPortFromEndpoint(opts.endpoint); |
| 170 | console.log(chalk.cyan(`${SYMBOL} Stopping inkling2-serve on ${opts.sparkHost}…`)); |
| 171 | spawnSync( |
| 172 | 'ssh', |
| 173 | [ |
| 174 | '-o', 'ConnectTimeout=8', |
| 175 | opts.sparkHost, |
| 176 | 'if [ -f /tmp/inkling2-serve.pid ]; then kill $(cat /tmp/inkling2-serve.pid) 2>/dev/null || true; rm -f /tmp/inkling2-serve.pid; fi; pkill -x inkling2-serve 2>/dev/null || true', |
| 177 | ], |
| 178 | { stdio: 'ignore', timeout: 15_000 } |
| 179 | ); |
| 180 | console.log(chalk.cyan(`${SYMBOL} Closing local SSH tunnel on port ${port}…`)); |
| 181 | spawnSync('pkill', ['-f', `127.0.0.1:${port}:127.0.0.1:8080 ${opts.sparkHost}`], { stdio: 'ignore' }); |
| 182 | console.log(chalk.green(`${SYMBOL} Stack stopped cleanly (Spark GPU idle).`)); |
| 183 | } |