ruvector-memopt 0.5.0

Intelligent cross-platform memory optimizer with neural learning capabilities for smart optimization decisions
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# GCP DevBox CLI - PowerShell Version
# Codespace-style Development Environments for Windows

param(
    [Parameter(Position=0)]
    [string]$Command = "help",

    [Alias("r")]
    [string]$Repo,

    [Alias("m")]
    [string]$Machine,

    [Alias("z")]
    [string]$Zone = "us-central1-a",

    [Alias("p")]
    [string]$Project,

    [Alias("g")]
    [string]$Gpu,

    [Alias("t")]
    [string]$TailscaleKey,

    [Alias("d")]
    [string]$DiskSize,

    [Alias("i")]
    [switch]$Interactive,

    [Parameter(ValueFromRemainingArguments=$true)]
    [string[]]$RemainingArgs
)

$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ConfigFile = Join-Path $ScriptDir "repos.json"
$SetupScript = Join-Path $ScriptDir "setup-vm.sh"

# ============================================================================
# Helper Functions
# ============================================================================
function Write-Info { param($Message) Write-Host "[INFO] $Message" -ForegroundColor Blue }
function Write-Success { param($Message) Write-Host "[OK] $Message" -ForegroundColor Green }
function Write-Warn { param($Message) Write-Host "[WARN] $Message" -ForegroundColor Yellow }
function Write-Error { param($Message) Write-Host "[ERROR] $Message" -ForegroundColor Red }

function Test-GCloud {
    if (-not (Get-Command gcloud -ErrorAction SilentlyContinue)) {
        Write-Error "gcloud CLI not found. Install from https://cloud.google.com/sdk/docs/install"
        exit 1
    }
}

function Get-Config {
    param($Key)
    $config = Get-Content $ConfigFile | ConvertFrom-Json
    $parts = $Key -split '\.'
    $value = $config
    foreach ($part in $parts) {
        $value = $value.$part
    }
    return $value
}

function Get-RepoConfig {
    param($RepoName)
    $config = Get-Content $ConfigFile | ConvertFrom-Json
    return $config.repos.$RepoName
}

# ============================================================================
# List available repos
# ============================================================================
function Show-RepoList {
    $config = Get-Content $ConfigFile | ConvertFrom-Json

    Write-Host ""
    Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
    Write-Host "║              Available Development Environments                ║" -ForegroundColor Cyan
    Write-Host "╠════════════════════════════════════════════════════════════════╣" -ForegroundColor Cyan

    $repos = $config.repos.PSObject.Properties | Select-Object -ExpandProperty Name
    $i = 1

    foreach ($repo in $repos) {
        $repoConfig = $config.repos.$repo
        $desc = $repoConfig.description
        $machine = $repoConfig.machine_type
        $gpu = if ($repoConfig.gpu) { $repoConfig.gpu } else { "none" }

        Write-Host ("║ " + "{0,2}" -f $i + ". " + "{0,-15}" -f $repo + " " + "{0,-30}" -f $desc + "║") -ForegroundColor Cyan
        Write-Host ("║     Machine: " + "{0,-12}" -f $machine + " GPU: " + "{0,-20}" -f $gpu + "║") -ForegroundColor Cyan
        $i++
    }

    Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
    Write-Host ""

    return $repos
}

# ============================================================================
# Interactive repo selection
# ============================================================================
function Select-Repo {
    $repos = Show-RepoList
    $repoArray = @($repos)
    $count = $repoArray.Count

    $selection = Read-Host "Select environment (1-$count)"
    $index = [int]$selection - 1

    if ($index -lt 0 -or $index -ge $count) {
        Write-Error "Invalid selection"
        exit 1
    }

    $selectedRepo = $repoArray[$index]

    if ($selectedRepo -eq "custom") {
        $customUrl = Read-Host "Enter custom repository URL"
        return @{ Name = "custom"; Url = $customUrl }
    }

    $config = Get-Content $ConfigFile | ConvertFrom-Json
    return @{ Name = $selectedRepo; Url = $config.repos.$selectedRepo.url }
}

# ============================================================================
# Create DevBox
# ============================================================================
function New-DevBox {
    Test-GCloud

    $repoName = $Repo
    $repoUrl = ""

    # Interactive mode
    if ($Interactive -or -not $repoName) {
        $selection = Select-Repo
        $repoName = $selection.Name
        $repoUrl = $selection.Url
    }

    # Get repo config
    $repoConfig = Get-RepoConfig $repoName
    if (-not $repoUrl) { $repoUrl = $repoConfig.url }
    if (-not $Machine) { $Machine = $repoConfig.machine_type }
    if (-not $DiskSize) { $DiskSize = $repoConfig.disk_size }
    if (-not $Gpu) { $Gpu = $repoConfig.gpu }
    $postClone = ($repoConfig.post_clone -join "`n")

    # Get project
    if (-not $Project) {
        $Project = gcloud config get-value project 2>$null
    }

    # Get defaults
    $imageFamily = Get-Config "defaults.image_family"
    $imageProject = Get-Config "defaults.image_project"

    # Tailscale key from env
    if (-not $TailscaleKey) {
        $TailscaleKey = $env:TAILSCALE_AUTHKEY
    }

    # Generate instance name
    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
    $instanceName = "devbox-$repoName-$timestamp"

    Write-Host ""
    Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
    Write-Host "║                    Creating DevBox Instance                    ║" -ForegroundColor Cyan
    Write-Host "╠════════════════════════════════════════════════════════════════╣" -ForegroundColor Cyan
    Write-Host "║ Instance:  $instanceName" -ForegroundColor Cyan
    Write-Host "║ Project:   $Project" -ForegroundColor Yellow
    Write-Host "║ Zone:      $Zone" -ForegroundColor Yellow
    Write-Host "║ Machine:   $Machine" -ForegroundColor Yellow
    Write-Host "║ Disk:      ${DiskSize}GB" -ForegroundColor Yellow
    Write-Host "║ GPU:       $(if ($Gpu) { $Gpu } else { 'none' })" -ForegroundColor Yellow
    Write-Host "║ Repo:      $repoUrl" -ForegroundColor Yellow
    Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
    Write-Host ""

    $confirm = Read-Host "Create this DevBox? (y/N)"
    if ($confirm -notmatch "^[Yy]$") {
        Write-Warn "Cancelled"
        return
    }

    # Build metadata
    $metadata = @(
        "REPO_URL=$repoUrl",
        "REPO_NAME=$repoName",
        "POST_CLONE_COMMANDS=$postClone",
        "GITHUB_TOKEN=$env:GITHUB_TOKEN",
        "TAILSCALE_AUTHKEY=$TailscaleKey"
    ) -join ","

    # Build gcloud command
    $gcloudArgs = @(
        "compute", "instances", "create", $instanceName,
        "--project=$Project",
        "--zone=$Zone",
        "--machine-type=$Machine",
        "--image-family=$imageFamily",
        "--image-project=$imageProject",
        "--boot-disk-size=${DiskSize}GB",
        "--boot-disk-type=pd-ssd",
        "--tags=devbox,http-server,https-server",
        "--metadata-from-file=startup-script=$SetupScript",
        "--metadata=$metadata",
        "--scopes=cloud-platform"
    )

    # Add GPU if specified
    if ($Gpu -and $Gpu -ne "null") {
        $gcloudArgs += "--accelerator=type=$Gpu,count=1"
        $gcloudArgs += "--maintenance-policy=TERMINATE"
    }

    Write-Info "Creating instance..."
    & gcloud @gcloudArgs

    Write-Success "Instance created: $instanceName"

    Write-Info "Waiting for setup to complete (this may take 3-5 minutes)..."
    Start-Sleep -Seconds 30

    # Get external IP
    $externalIp = gcloud compute instances describe $instanceName `
        --zone=$Zone `
        --format="get(networkInterfaces[0].accessConfigs[0].natIP)"

    Write-Host ""
    Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Green
    Write-Host "║                     DevBox Ready!                              ║" -ForegroundColor Green
    Write-Host "╠════════════════════════════════════════════════════════════════╣" -ForegroundColor Green
    Write-Host "║ SSH:       gcloud compute ssh $instanceName --zone=$Zone" -ForegroundColor Cyan
    Write-Host "║ Direct:    ssh devbox@$externalIp" -ForegroundColor Cyan
    Write-Host "║" -ForegroundColor Green
    Write-Host "║ VS Code:   code --remote ssh-remote+devbox@$externalIp /home/devbox/workspace" -ForegroundColor Cyan
    Write-Host "║" -ForegroundColor Green
    Write-Host "║ Tailscale: Run 'devbox-status' on VM for Tailscale IP" -ForegroundColor Yellow
    Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Green
}

# ============================================================================
# Status
# ============================================================================
function Get-DevBoxStatus {
    Test-GCloud

    Write-Host ""
    Write-Host "Running DevBox Instances:" -ForegroundColor Cyan
    Write-Host ""

    gcloud compute instances list --filter="tags.items=devbox" `
        --format="table(name,zone,machineType.basename(),status,networkInterfaces[0].accessConfigs[0].natIP:label=EXTERNAL_IP)"
}

# ============================================================================
# Connect
# ============================================================================
function Connect-DevBox {
    Test-GCloud

    $instanceName = $RemainingArgs[0]
    $connectZone = if ($RemainingArgs[1]) { $RemainingArgs[1] } else { $Zone }

    if (-not $instanceName) {
        Write-Host "Select a DevBox to connect:"
        gcloud compute instances list --filter="tags.items=devbox AND status=RUNNING" `
            --format="table[no-heading](name,zone)"

        $instanceName = Read-Host "Instance name"
        $connectZone = Read-Host "Zone"
    }

    Write-Info "Connecting to $instanceName..."
    gcloud compute ssh $instanceName --zone=$connectZone -- -A
}

# ============================================================================
# Stop
# ============================================================================
function Stop-DevBox {
    $instanceName = $RemainingArgs[0]
    $stopZone = if ($RemainingArgs[1]) { $RemainingArgs[1] } else { $Zone }

    if (-not $instanceName) {
        Write-Error "Usage: devbox stop <instance-name> [zone]"
        return
    }

    Write-Info "Stopping $instanceName..."
    gcloud compute instances stop $instanceName --zone=$stopZone
    Write-Success "Instance stopped"
}

# ============================================================================
# Start
# ============================================================================
function Start-DevBox {
    $instanceName = $RemainingArgs[0]
    $startZone = if ($RemainingArgs[1]) { $RemainingArgs[1] } else { $Zone }

    if (-not $instanceName) {
        Write-Error "Usage: devbox start <instance-name> [zone]"
        return
    }

    Write-Info "Starting $instanceName..."
    gcloud compute instances start $instanceName --zone=$startZone
    Write-Success "Instance started"
}

# ============================================================================
# Delete
# ============================================================================
function Remove-DevBox {
    $instanceName = $RemainingArgs[0]
    $deleteZone = if ($RemainingArgs[1]) { $RemainingArgs[1] } else { $Zone }

    if (-not $instanceName) {
        Write-Error "Usage: devbox delete <instance-name> [zone]"
        return
    }

    $confirm = Read-Host "Delete $instanceName? This cannot be undone. (y/N)"
    if ($confirm -notmatch "^[Yy]$") {
        Write-Warn "Cancelled"
        return
    }

    Write-Info "Deleting $instanceName..."
    gcloud compute instances delete $instanceName --zone=$deleteZone --quiet
    Write-Success "Instance deleted"
}

# ============================================================================
# SSH Config
# ============================================================================
function Get-SshConfig {
    Test-GCloud

    Write-Host ""
    Write-Host "# Add to ~/.ssh/config"
    Write-Host ""

    $instances = gcloud compute instances list --filter="tags.items=devbox AND status=RUNNING" `
        --format="csv[no-heading](name,zone,networkInterfaces[0].accessConfigs[0].natIP)"

    foreach ($line in $instances -split "`n") {
        if ($line) {
            $parts = $line -split ","
            $name = $parts[0]
            $instZone = $parts[1]
            $ip = $parts[2]

            Write-Host "Host $name"
            Write-Host "    HostName $ip"
            Write-Host "    User devbox"
            Write-Host "    ForwardAgent yes"
            Write-Host ""
        }
    }
}

# ============================================================================
# Help
# ============================================================================
function Show-Help {
    Write-Host ""
    Write-Host "GCP DevBox - Codespace-style Development Environments" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "Usage: .\devbox.ps1 <command> [options]"
    Write-Host ""
    Write-Host "Commands:"
    Write-Host "  list              List available repo templates"
    Write-Host "  create            Create a new DevBox instance"
    Write-Host "  status            Show running DevBox instances"
    Write-Host "  connect <name>    SSH into a DevBox"
    Write-Host "  start <name>      Start a stopped DevBox"
    Write-Host "  stop <name>       Stop a running DevBox"
    Write-Host "  delete <name>     Delete a DevBox"
    Write-Host "  ssh-config        Generate SSH config for all DevBoxes"
    Write-Host ""
    Write-Host "Create Options:"
    Write-Host "  -r, -Repo <name>         Repository template name"
    Write-Host "  -m, -Machine <type>      Machine type (e.g., e2-standard-4)"
    Write-Host "  -z, -Zone <zone>         GCP zone (default: us-central1-a)"
    Write-Host "  -p, -Project <id>        GCP project ID"
    Write-Host "  -g, -Gpu <type>          GPU type (e.g., nvidia-tesla-t4)"
    Write-Host "  -t, -TailscaleKey        Tailscale auth key"
    Write-Host "  -d, -DiskSize <size>     Disk size in GB"
    Write-Host "  -i, -Interactive         Interactive repo selection"
    Write-Host ""
    Write-Host "Examples:"
    Write-Host "  .\devbox.ps1 create -i                    # Interactive mode"
    Write-Host "  .\devbox.ps1 create -r claude-flow        # Create claude-flow DevBox"
    Write-Host "  .\devbox.ps1 create -r ml-workspace -g nvidia-tesla-t4"
    Write-Host "  .\devbox.ps1 connect devbox-claude-flow-20240115"
    Write-Host ""
    Write-Host "Environment Variables:"
    Write-Host "  TAILSCALE_AUTHKEY    Tailscale auth key for auto-connect"
    Write-Host "  GITHUB_TOKEN         GitHub token for private repos"
    Write-Host ""
}

# ============================================================================
# Main
# ============================================================================
switch ($Command.ToLower()) {
    "list" { Show-RepoList | Out-Null }
    "create" { New-DevBox }
    "status" { Get-DevBoxStatus }
    "connect" { Connect-DevBox }
    "start" { Start-DevBox }
    "stop" { Stop-DevBox }
    "delete" { Remove-DevBox }
    "ssh-config" { Get-SshConfig }
    "help" { Show-Help }
    default { Write-Error "Unknown command: $Command"; Show-Help }
}