Mastermind environments

From monoes/monomind · View interactive version

Mastermind environments is an AI agent skill from the GitHub repository monoes/monomind. Deprecated: bookkeeping only — not enforced by the Org Runtime. Mastermind environments: Records execution environments (local, SSH, sandbox) in a side file; agents run where the runtime runs them.

How to use this skill

Skill file

Mastermind Environments

This skill is invoked directly via /mastermind-environments.

Bookkeeping only — not enforced by the Org Runtime. It writes .monomind/orgs/<org>-environments.json, which no runtime code reads: creating an environment or changing the default does not change where agents run.

On first use in a session, print this one line before doing anything else: WARNING: mastermind-environments is a ledger only - nothing is enforced by the Org Runtime.


Inputs


Security Constraints


Step 0 — Brain Load (standalone only)

If caller is not "command", load brain context following mastermind-protocol/SKILL.md Brain Load Procedure with namespace: ops.


Step 1 — Load Environments File

orgFile=".monomind/orgs/${org_name}.json"
[ ! -f "$orgFile" ] && { echo "ERROR: Org '${org_name}' not found."; exit 1; }

envFile=".monomind/orgs/${org_name}-environments.json"
[ ! -f "$envFile" ] && cat > "$envFile" <<'EOF'
{"environments":[],"default_env":null}
EOF

Step 2 — Execute Action

list (default)

echo "ENVIRONMENTS — org: $org_name"
echo "────────────────────────────────────────────────────────"
printf "%-20s %-10s %-28s %-8s %s\n" "ID" "KIND" "HOST / PATH" "DEFAULT" "SECRET REF"
echo "────────────────────────────────────────────────────────"

defaultEnv=$(jq -r '.default_env // "none"' "$envFile")
count=$(jq '.environments | length' "$envFile")

if [ "$count" -eq 0 ]; then
  echo "  No environments. Use --action create to add one."
else
  jq -r --arg def "$defaultEnv" '(.environments // [])[] |
    [
      .id,
      (.kind // "local"),
      (if .kind == "ssh" then ((.user // "?") + "@" + (.host // "?") + ":" + ((.port // 22) | tostring)) else (.work_dir // "(local)") end),
      (if .id == $def then "yes" else "-" end),
      (.secret_ref // "-")
    ] | @tsv' "$envFile" | while IFS=$'\t' read -r id kind loc def sref; do
    printf "%-20s %-10s %-28s %-8s %s\n" "$id" "$kind" "$loc" "$def" "$sref"
  done
fi

echo ""
echo "Total: $count environment(s)  |  Default: $defaultEnv"

create

[ -z "$kind" ] && { echo "ERROR: --kind required (local|ssh|sandbox)."; exit 1; }
[ -z "$name" ] && { echo "ERROR: --name required."; exit 1; }
case "$kind" in local|ssh|sandbox) : ;; *)
  echo "ERROR: --kind must be local, ssh, or sandbox"; exit 1 ;;
esac
if [ "$kind" = "ssh" ]; then
  [ -z "$host" ] && { echo "ERROR: --host required for ssh kind."; exit 1; }
  [ -z "$user" ] && { echo "ERROR: --user required for ssh kind."; exit 1; }
  [ -z "$secret_ref" ] && { echo "WARNING: No --secret-ref supplied. SSH key must be pre-configured on the agent."; }
fi

envId=$(echo "${name}" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-' | sed 's/^-//;s/-$//')
ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)

tmp="${envFile}.tmp"
jq --arg id "$envId" \
   --arg n "$name" \
   --arg kind "$kind" \
   --arg host "${host:-}" \
   --argjson port "${port:-22}" \
   --arg user "${user:-}" \
   --arg sref "${secret_ref:-}" \
   --arg wdir "${work_dir:-/tmp/monomind}" \
   --arg ts "$ts" \
  '.environments = [(.environments // [])[] | select(.id != $id)] +
   [{"id":$id,"name":$n,"kind":$kind,
     "host":(if $host != "" then $host else null end),
     "port":(if $host != "" then $port else null end),
     "user":(if $user != "" then $user else null end),
     "secret_ref":(if $sref != "" then $sref else null end),
     "work_dir":$wdir,
     "createdAt":$ts,"lastProbe":null,"probeStatus":null}]' \
  "$envFile" > "$tmp" && mv "$tmp" "$envFile"

echo "Created environment: $envId (kind: $kind)"
[ "$kind" = "ssh" ] && echo "  SSH: ${user}@${host}:${port:-22}  key ref: ${secret_ref:-(none)}"
echo "Run --action probe --env-id $envId to verify connectivity."

edit

[ -z "$env_id" ] && { echo "ERROR: --env-id required."; exit 1; }
exists=$(jq --arg id "$env_id" '[(.environments // [])[] | select(.id == $id)] | length' "$envFile")
[ "$exists" -eq 0 ] && { echo "ERROR: Environment '$env_id' not found."; exit 1; }

tmp="${envFile}.tmp"
jq --arg id "$env_id" \
   --arg host "${host:-}" \
   --argjson port "${port:-0}" \
   --arg user "${user:-}" \
   --arg sref "${secret_ref:-}" \
   --arg wdir "${work_dir:-}" \
  '.environments = [(.environments // [])[] | if .id == $id then
     . *
     (if $host != "" then {"host":$host} else {} end) *
     (if $port > 0 then {"port":$port} else {} end) *
     (if $user != "" then {"user":$user} else {} end) *
     (if $sref != "" then {"secret_ref":$sref} else {} end) *
     (if $wdir != "" then {"work_dir":$wdir} else {} end)
   else . end]' \
  "$envFile" > "$tmp" && mv "$tmp" "$envFile"

echo "Updated environment: $env_id"

delete

[ -z "$env_id" ] && { echo "ERROR: --env-id required."; exit 1; }
tmp="${envFile}.tmp"
jq --arg id "$env_id" \
  '.environments = [(.environments // [])[] | select(.id != $id)] |
   if .default_env == $id then .default_env = null else . end' \
  "$envFile" > "$tmp" && mv "$tmp" "$envFile"
echo "Deleted environment: $env_id"

probe

[ -z "$env_id" ] && { echo "ERROR: --env-id required."; exit 1; }
envData=$(jq -r --arg id "$env_id" '(.environments // [])[] | select(.id == $id)' "$envFile")
[ -z "$envData" ] && { echo "ERROR: Environment '$env_id' not found."; exit 1; }

kind=$(echo "$envData" | jq -r '.kind')
ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)

case "$kind" in
  local)
    if [ -d "$(echo "$envData" | jq -r '.work_dir // "/tmp/monomind"')" ]; then
      status="ok"
    else
      status="unreachable"
    fi
    ;;
  ssh)
    sshHost=$(echo "$envData" | jq -r '.host')
    sshUser=$(echo "$envData" | jq -r '.user')
    sshPort=$(echo "$envData" | jq -r '.port // 22')
    sref=$(echo "$envData" | jq -r '.secret_ref // ""')
    keyPath=""
    [ -n "$sref" ] && keyPath=$(cat ".monomind/orgs/.secrets/${org_name}/${sref}" 2>/dev/null | jq -r '.path // ""' 2>/dev/null || echo "")
    
    if [ -n "$keyPath" ] && [ -f "$keyPath" ]; then
      result=$(ssh -i "$keyPath" -p "$sshPort" -o ConnectTimeout=5 -o StrictHostKeyChecking=no \
        "${sshUser}@${sshHost}" "echo ok" 2>&1)
    else
      result=$(ssh -p "$sshPort" -o ConnectTimeout=5 -o StrictHostKeyChecking=no \
        "${sshUser}@${sshHost}" "echo ok" 2>&1)
    fi
    [ "$result" = "ok" ] && status="ok" || status="unreachable"
    ;;
  sandbox)
    status="unknown"
    ;;
esac

tmp="${envFile}.tmp"
jq --arg id "$env_id" --arg st "$status" --arg ts "$ts" \
  '.environments = [(.environments // [])[] | if .id == $id then .probeStatus = $st | .lastProbe = $ts else . end]' \
  "$envFile" > "$tmp" && mv "$tmp" "$envFile"

echo "Probe [$env_id]: $status  (checked at $ts)"

set-default

[ -z "$env_id" ] && { echo "ERROR: --env-id required."; exit 1; }
exists=$(jq --arg id "$env_id" '[(.environments // [])[] | select(.id == $id)] | length' "$envFile")
[ "$exists" -eq 0 ] && { echo "ERROR: Environment '$env_id' not found."; exit 1; }

tmp="${envFile}.tmp"
jq --arg id "$env_id" '.default_env = $id' "$envFile" > "$tmp" && mv "$tmp" "$envFile"
echo "Default environment → $env_id"

Step 3 — Return Output

domain: ops
status: complete
action: <action>
org: <org_name>
environments_count: <N>
default_env: <id or null>

Step 4 — Brain Write (standalone only)

If caller is not "command", follow mastermind-protocol/SKILL.md Brain Write Procedure for domain ops.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.