File size: 8,750 Bytes
05667d9
 
 
ee6be2a
 
05667d9
ee6be2a
6f1c0d6
7a19fe4
 
6f1c0d6
05667d9
7a19fe4
5c78eb5
05667d9
188bc44
 
 
 
 
 
 
 
 
 
 
 
 
 
6f1c0d6
59ac471
6f1c0d6
 
 
59ac471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cba4f0
 
 
feb92c0
 
6cba4f0
 
feb92c0
 
6cba4f0
feb92c0
 
 
6cba4f0
feb92c0
6cba4f0
 
feb92c0
80e6c3d
6cba4f0
 
 
 
 
 
 
feb92c0
6cba4f0
feb92c0
6cba4f0
 
 
 
 
 
feb92c0
6cba4f0
 
 
 
01d281b
7a19fe4
 
519b3e9
7a19fe4
5c78eb5
7a19fe4
 
 
05667d9
5c78eb5
a673681
 
 
 
9c5ee69
6cba4f0
 
 
 
 
 
 
 
 
 
 
35eeda3
3af5b14
519b3e9
3af5b14
 
6cba4f0
 
 
 
 
 
 
 
b283eff
 
6cba4f0
 
 
 
 
 
59ac471
3d09fff
 
 
 
 
 
 
 
5dc4736
 
 
 
 
05830ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee6be2a
a03d338
 
8ad17b4
ee6be2a
 
05667d9
 
 
 
 
 
ee6be2a
05667d9
 
 
 
 
 
 
 
 
 
 
ee6be2a
 
 
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
#!/bin/bash
set -e

echo "🚀 Starting REA + Codeman Mission Control Space..."
echo "================================================="

# 1. Directories:
#    - /data (S3 bucket): workspace (user files only), /data/apps/ (isolated application data)
#    - /tmp (ephemeral local disk): logs, caches, scratch files
#    - /home/appuser (container overlayfs): SQLite databases (hermes-web-ui.db, state.db)
mkdir -p /data/{workspace,services,apps/vscode,apps/hermes,apps/ekko,apps/opencode,apps/dsh}
mkdir -p /tmp/{logs,.cache,code-server-cache}
mkdir -p /home/appuser/{.hermes,.hermes-web-ui}
chmod -R 755 /data/services /data/apps

# Sync service scripts into /data/services and remove stale legacy services
for s in /data/services/*.sh; do
    if [ ! -f "/app/services/$(basename "$s")" ]; then
        echo "🗑️ Removing stale service $(basename "$s")..."
        rm -f "$s"
    fi
done

for f in /app/services/*.sh /app/services/*.py; do
    target="/data/services/$(basename "$f")"
    cp "$f" "$target"
    chmod +x "$target"
done

# Ensure opencode global configuration persists under /data/apps/opencode
mkdir -p /root/.config
ln -sfn /data/apps/opencode /root/.config/opencode 2>/dev/null || true
if [ ! -f /data/apps/opencode/opencode.json ]; then
    cat << 'OPEOF' > /data/apps/opencode/opencode.json
{
  "mcp": {
    "rea": {
      "type": "local",
      "command": [
        "/usr/local/bin/rea",
        "mcp"
      ],
      "enabled": true
    }
  }
}
OPEOF
fi

# Migrate Ekko/Hermes-Web-UI SQLite state out of the bucket. Older revisions ran the
# gateway with cwd=/data/workspace while NODE_ENV!=production, which made hermes-web-ui
# write to $CWD/packages/server/data/hermes-web-ui.db (S3 FUSE — never allowed there).
# Move it into /data/apps/ekko/hermes-web-ui.db for persistent backup and restore into
# /home/appuser/.hermes-web-ui. Drop the bucket-side workspace/packages entirely.
WS_EKKO_DB="/data/workspace/packages/server/data/hermes-web-ui.db"
LOCAL_EKKO_DB="/home/appuser/.hermes-web-ui/hermes-web-ui.db"
BACKUP_EKKO_DB="/data/apps/ekko/hermes-web-ui.db"
mkdir -p /home/appuser/.hermes-web-ui /data/apps/ekko
if [ -f "$WS_EKKO_DB" ]; then
    if [ ! -f "$BACKUP_EKKO_DB" ] || [ "$WS_EKKO_DB" -nt "$BACKUP_EKKO_DB" ]; then
        cp -f "$WS_EKKO_DB" "$BACKUP_EKKO_DB" 2>/dev/null || true
    fi
    if [ ! -f "$LOCAL_EKKO_DB" ] || [ "$WS_EKKO_DB" -nt "$LOCAL_EKKO_DB" ]; then
        cp -f "$WS_EKKO_DB" "$LOCAL_EKKO_DB" 2>/dev/null || true
    fi
fi
rm -rf /data/workspace/packages 2>/dev/null || true

# Clean clutter out of user /data/workspace (user files only — no app state, no SQLite)
rm -rf /data/workspace/.claude /data/workspace/.vscode /data/workspace/.opencode \
       /data/workspace/config.yaml /data/workspace/hook-secret \
       /data/workspace/session-lifecycle.jsonl /data/workspace/state.json* \
       /data/workspace/subagent-window-states.json 2>/dev/null || true

# Migrate legacy /data/config/* into the structured /data/apps/* layout (older revisions
# wrote app config here). Copy any missing files, remove the legacy directory completely.
if [ -d /data/config ]; then
    echo "📦 Migrating legacy /data/config to /data/apps..."
    for legacy in /data/config/*/; do
        [ -d "$legacy" ] || continue
        name="$(basename "$legacy")"
        mkdir -p "/data/apps/$name"
        cp -rn "$legacy". "/data/apps/$name/" 2>/dev/null || true
    done
    rm -rf /data/config 2>/dev/null || true
fi

# Legacy code-server user-data location; current user-data is /data/apps/vscode
rm -rf /data/code-server 2>/dev/null || true

# Clean legacy logs/tmp and active SQLite DBs from FUSE bucket to prevent deadlocks
rm -rf /data/logs /data/tmp /data/.cache /data/openclaw /data/script/codeman /data/.hermes /data/.hermes-web-ui /data/.ekko-studio /data/.ekko 2>/dev/null || true

# Hermes & Ekko Studio run purely on local fast container storage (/home/appuser)
mkdir -p /home/appuser/{.hermes,.hermes-web-ui} /data/apps/{hermes,ekko}
ln -sfn /home/appuser/.hermes /root/.hermes 2>/dev/null || true
ln -sfn /home/appuser/.hermes-web-ui /root/.hermes-web-ui 2>/dev/null || true
ln -sfn /home/appuser/.hermes-web-ui /root/.ekko 2>/dev/null || true

# Restore persistent database, skills, and configs from /data/apps/ backup
if [ -f /app/services/apps_sync.py ]; then
    python3 /app/services/apps_sync.py restore || true
    nohup python3 /app/services/apps_sync.py > /tmp/logs/apps-sync.log 2>&1 &
fi

# code-server must always open BLANK: drop any restored "last opened folder" memory so
# /code/ never deep-links to ?folder=/data/workspace. Runs after apps_sync restore.
rm -rf /data/apps/vscode/User/workspaceStorage /data/apps/vscode/User/History /data/apps/vscode/Backups 2>/dev/null || true
mkdir -p /data/apps/vscode/User
cat > /data/apps/vscode/User/settings.json << 'SETEOF'
{
  "workbench.startupEditor": "none",
  "window.restoreWindows": "none"
}
SETEOF

# Deploy clean opencode-router with assets and public UI into /data/script/opencode-router
mkdir -p /data/script/opencode-router
cp -rn /opt/opencode-router/* /data/script/opencode-router/ 2>/dev/null || true
chmod +x /data/script/opencode-router/opencode-router 2>/dev/null || true

# Ship the already-fixed index.html (relative asset + API paths) instead of rewriting the
# HTML with sed at boot. See config/opencode-router/index.html — that file is the source
# of truth, so /opencode/ never hijacks the root /assets or /api namespaces.
mkdir -p /data/script/opencode-router/public
if [ -f /app/config/opencode-router/index.html ]; then
    cp -f /app/config/opencode-router/index.html /data/script/opencode-router/public/index.html
else
    echo "⚠️ /app/config/opencode-router/index.html missing; keeping router's bundled index.html"
fi

# Redirect code-server caches & logs to /tmp so the S3 bucket never accumulates garbage
mkdir -p /data/apps/vscode
rm -rf /data/apps/vscode/CachedData /data/apps/vscode/CachedProfilesData /data/apps/vscode/CachedExtensionVSIXs /data/apps/vscode/logs 2>/dev/null || true
ln -sfn /tmp/logs /data/apps/vscode/logs 2>/dev/null || true
ln -sfn /tmp/code-server-cache /data/apps/vscode/CachedData 2>/dev/null || true
ln -sfn /tmp/code-server-cache/ProfilesData /data/apps/vscode/CachedProfilesData 2>/dev/null || true

# Deploy additional skills bundle if present (excluding browser-act)
if [ -f /app/skills-bundle/hermes-missing-skills.tar.gz ]; then
    mkdir -p /home/appuser/.hermes/skills /data/apps/hermes/skills
    tar -xzf /app/skills-bundle/hermes-missing-skills.tar.gz -C /home/appuser/.hermes/skills/ 2>/dev/null || true
    tar -xzf /app/skills-bundle/hermes-missing-skills.tar.gz -C /data/apps/hermes/skills/ 2>/dev/null || true
    rm -rf /home/appuser/.hermes/skills/browser-act /data/apps/hermes/skills/browser-act 2>/dev/null || true
fi

# Generate shared API_SERVER_KEY if not already set, so hermes gateway and web-ui talk cleanly
if [ -z "$API_SERVER_KEY" ]; then
    export API_SERVER_KEY="$(python3 -c "import secrets; print(secrets.token_hex(32))")"
    echo "API_SERVER_KEY=$API_SERVER_KEY" >> /tmp/.env
fi
if [ -f /usr/local/lib/hermes-agent/install-stamp.json ]; then
    python3 -c '
import json
try:
    p = "/usr/local/lib/hermes-agent/install-stamp.json"
    d = json.load(open(p))
    if d.get("baseVersion") == "unknown":
        d["baseVersion"] = "0.21.0"
        d["displayVersion"] = "0.21.0"
        json.dump(d, open(p, "w"), indent=2)
except Exception:
    pass
' 2>/dev/null || true
fi

# 2. Persistent Caddyfile on /data (user-editable):
# Always sync from template to apply latest routing rules and remove stale directives
cp /app/config/Caddyfile.example /data/Caddyfile
caddy fmt --overwrite /data/Caddyfile 2>/dev/null || true

# 3. Enforce Browser Lockdown
rm -rf /root/.cache/ms-playwright /root/.cache/puppeteer /tmp/.cache/ms-playwright 2>/dev/null || true
mkdir -p /root/.cache
ln -sfn /dev/null /root/.cache/ms-playwright 2>/dev/null || true
ln -sfn /dev/null /root/.cache/puppeteer 2>/dev/null || true
echo "Browser lockdown verified: zero browser packages permitted." > /tmp/logs/browser-block.log

# 4. Launch Persistent Background Services
shopt -s nullglob
scripts=(/data/services/*.sh)
echo "🧩 Launching ${#scripts[@]} background service(s)..." > /tmp/logs/services.log
for s in "${scripts[@]}"; do
    name="$(basename "$s")"
    chmod +x "$s" 2>/dev/null || true
    echo "   ▶️  $name" >> /tmp/logs/services.log
    setsid bash "$s" >> /tmp/logs/services.log 2>&1 < /dev/null &
    disown 2>/dev/null || true
done

# 5. Run Caddy in Foreground using persistent /data/Caddyfile
echo "🌐 Starting Caddy edge proxy on :7860 using /data/Caddyfile..."
exec caddy run --config /data/Caddyfile --adapter caddyfile