Files
openclaw/scripts/lib/live-docker-stage.sh
Peter SteinbergerandVincent Koc 1ea2640f54 refactor(state): consolidate wide rows, plugin index, workspace attestations, and shared auth singletons at schema v13 (#130466)
* refactor(state): make cron and subagent rows JSON-canonical

* refactor(state): make gateway origin device tokens canonical at v13

The lazy ensure predates the table joining the canonical schema; at the
v13 bump the schema owns creation, so the feature-local DDL, WeakSet
dedupe, and lazy-list entry retire. The legacy-file guard the ensure
carried stays at each call site.

* test: drop obsolete lazy-ensure coverage for origin device tokens

The table is canonical at v13; same-version lazy creation no longer
exists to protect. Origin CRUD, isolation, and rotation coverage remains
in the surviving cases.

* refactor(state): fold installed_plugin_index into config_machine_state

The singleton index row becomes one JSON value under
plugins.installedIndex with its rollback-fencing revision inside the
value; reads, CAS restore, and the lease-held write transactions use
direct Kysely on config_machine_state so the state_leases assertion
stays in-transaction. The v13 migration imports the row and drops the
table; the additive workspace_dir entry folds with it. Doctor guidance,
docker staging, and the e2e probes name the machine-state row.

* refactor(state): merge workspace_attestations into workspace_setup_state

One row per workspace now carries both setup milestones and the
attestation clock: nullable setup columns represent attestation-only
workspaces (replaceWorkspaceAttestation can precede any setup write) and
setupExists derives from a non-null version. The bootstrap-hash FK
repoints to the merged table; migration receipts keep the historical
workspace_attestations discriminator string. The v13 migration grows and
rebuilds the table, merges attestation rows (orphans without a path
alias drop — their hashes re-derive at the next bootstrap attestation),
and the consolidation kind is renamed state-consolidation-v13 to cover
the batch.

* test(state): cover the workspace merge and consolidation fallout

The v12-to-v13 regression seeds merged, attestation-only, and orphan
attestation workspaces; the 13-to-12 downgrade fixture recreates
workspace_attestations and installed_plugin_index from the folded data;
the fold-in migration gates the additive workspace_dir column for
pre-additive rows; the workspace merge now triggers on the setup table's
own shape so stable-era databases without an attestations table still
reshape; the consolidation applied-message covers the batch.

* refactor(state): fold shared auth profile singletons into config_machine_state

The shared-state auth_profile_stores/auth_profile_state rows (fixed key
'shared') become authProfiles.store/authProfiles.state machine-state
values; the agent-DB tables of the same names are untouched. Git-backup
redaction moves from table-drop to the authProfiles. secret prefix with
seeded-secret absence proof; migration receipts keep the historical
table-name discriminators; the shared-auth relocation and receipt
verification project the KV cells back to the receipt-era row shapes so
persisted digests stay byte-compatible. mcp_oauth_stores stays a table —
its multi-key fold is a named follow-up.

* test(state): finish shared-auth fold coverage and annotate boundary casts

Auth seeders and assertions across the e2e/scripts/secrets suites target
the authProfiles machine-state cells; the v12-to-v13 regression proves
payload-byte fidelity, non-shared-row drop, and insert-if-absent
precedence; the downgrade fixture recreates and repopulates both v12
tables. Boundary type assertions in the plugin-index store carry SAFETY
invariants per the ratchet.

* chore: shrink assertion-safety baseline for plugin-index store

* refactor(doctor): delete the dead onboarding-recommendations migration

Its input — the unscoped 'primary' onboarding row — existed only between
9a93a52a8a and 473962b7de, a two-day beta window; no shipped stable
can produce it and the runtime table folded away at v12. The audit
backup list keeps recognizing system-agent.jsonl artifacts because beta
installs that ran that import may still carry its backups.

* docs: sync the 13-to-12 downgrade example with the executable fixture

* style: format the synced downgrade example

* style: drop unused import and duplicate union constituent

* fix(state): keep orphan attestations across the v13 workspace merge

The merged workspace_setup_state required a workspace path, but legacy
orphan hashed-key attestations never recorded one. workspace_path is now
nullable (setup rows still enforce it via CHECK), the v13 migration and
the doctor file import keep orphans with a NULL path that heals on the
next live access, and the 13-to-12 downgrade keeps attestation-owned
hashes. Doctor test seeds move to the folded KV row.

* perf(state): retire unused cron indexes

* fix(state): preserve v13 migration recovery

* fix(state): preserve v12 lazy-table upgrade

* docs(state): document v13 auth relocation

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-08-27 15:26:14 +08:00

201 lines
5.8 KiB
Bash

#!/usr/bin/env bash
openclaw_live_stage_mounted_auth() {
if [ "${OPENCLAW_DOCKER_AUTH_PRESTAGED:-0}" = "1" ]; then
return 0
fi
local auth_path
local auth_dirs=()
local auth_files=()
IFS=',' read -r -a auth_dirs <<<"${OPENCLAW_DOCKER_AUTH_DIRS_RESOLVED:-}"
IFS=',' read -r -a auth_files <<<"${OPENCLAW_DOCKER_AUTH_FILES_RESOLVED:-}"
if ((${#auth_dirs[@]} > 0)); then
for auth_path in "${auth_dirs[@]}"; do
[ -n "$auth_path" ] || continue
if [ -d "/host-auth/$auth_path" ]; then
mkdir -p "$HOME/$auth_path"
cp -R "/host-auth/$auth_path/." "$HOME/$auth_path"
chmod -R u+rwX "$HOME/$auth_path" || true
fi
done
fi
if ((${#auth_files[@]} > 0)); then
for auth_path in "${auth_files[@]}"; do
[ -n "$auth_path" ] || continue
if [ -f "/host-auth-files/$auth_path" ]; then
mkdir -p "$(dirname "$HOME/$auth_path")"
cp "/host-auth-files/$auth_path" "$HOME/$auth_path"
chmod u+rw "$HOME/$auth_path" || true
fi
done
fi
}
openclaw_live_run_setup_command() {
local timeout_seconds="${1:?setup timeout seconds required}"
local label="${2:?setup label required}"
shift 2
local timeout_bin=""
if command -v timeout >/dev/null 2>&1; then
timeout_bin="timeout"
elif command -v gtimeout >/dev/null 2>&1; then
timeout_bin="gtimeout"
else
echo "timeout command not found; cannot bound ${label} after ${timeout_seconds}s" >&2
return 127
fi
if "$timeout_bin" --kill-after=1s 1s true >/dev/null 2>&1; then
"$timeout_bin" --kill-after=30s "${timeout_seconds}s" "$@"
else
"$timeout_bin" "${timeout_seconds}s" "$@"
fi
}
openclaw_live_stage_source_tree() {
local dest_dir="${1:?destination directory required}"
local stage_mode="${OPENCLAW_LIVE_DOCKER_SOURCE_STAGE_MODE:-copy}"
if [ "$stage_mode" = "symlink" ]; then
echo "OPENCLAW_LIVE_DOCKER_SOURCE_STAGE_MODE=symlink is disabled; using copy staging." >&2
fi
set +e
tar -C /src \
--warning=no-file-changed \
--ignore-failed-read \
--exclude=.git \
--exclude=.artifacts \
--exclude=node_modules \
--exclude=dist \
--exclude=ui/dist \
--exclude=ui/node_modules \
--exclude=.pnpm-store \
--exclude=.tmp \
--exclude=.tmp-precommit-venv \
--exclude=.worktrees \
--exclude=__openclaw_vitest__ \
--exclude=relay.sock \
--exclude='*.sock' \
--exclude='*/*.sock' \
--exclude='apps/*/.build' \
--exclude='apps/*/*.bun-build' \
--exclude='apps/*/.gradle' \
--exclude='apps/*/.kotlin' \
--exclude='apps/*/build' \
-cf - . | tar -C "$dest_dir" -xf -
local status=$?
set -e
if [ "$status" -gt 1 ]; then
return "$status"
fi
local scripts_dir="${OPENCLAW_LIVE_DOCKER_SCRIPTS_DIR:-/src/scripts}"
node "$scripts_dir/live-docker-stage-private-sdk-exports.mjs" "$dest_dir"
}
openclaw_live_link_runtime_tree() {
local dest_dir="${1:?destination directory required}"
if [ ! -e "$dest_dir/node_modules" ]; then
ln -s /app/node_modules "$dest_dir/node_modules"
fi
ln -s /app/dist "$dest_dir/dist"
if [ -d /app/dist-runtime/extensions ]; then
export OPENCLAW_BUNDLED_PLUGINS_DIR=/app/dist-runtime/extensions
elif [ -d /app/dist/extensions ]; then
export OPENCLAW_BUNDLED_PLUGINS_DIR=/app/dist/extensions
fi
}
openclaw_live_stage_node_modules() {
local dest_dir="${1:?destination directory required}"
local target_dir="$dest_dir/node_modules"
mkdir -p "$target_dir"
cp -aRs /app/node_modules/. "$target_dir"
rm -rf "$target_dir/.vite-temp"
mkdir -p "$target_dir/.vite-temp"
}
openclaw_live_scrub_staged_plugin_index() {
local dest_dir="${1:?destination directory required}"
local db_path="$dest_dir/state/openclaw.sqlite"
if [ ! -f "$db_path" ]; then
return 0
fi
node - "$db_path" <<'NODE'
const dbPath = process.argv[2];
let db;
try {
const { DatabaseSync } = await import("node:sqlite");
db = new DatabaseSync(dbPath);
try {
db.exec("PRAGMA secure_delete = ON;");
db.prepare("DELETE FROM config_machine_state WHERE state_key = ?").run("plugins.installedIndex");
db.exec("PRAGMA wal_checkpoint(TRUNCATE);");
db.exec("VACUUM;");
} catch (err) {
if (!String(err?.message ?? err).includes("no such table")) {
throw err;
}
}
} finally {
db?.close();
}
NODE
}
openclaw_live_stage_state_dir() {
local dest_dir="${1:?destination directory required}"
local source_dir="${HOME}/.openclaw"
mkdir -p "$dest_dir"
if [ -d "$source_dir" ]; then
# Sandbox workspaces can accumulate root-owned artifacts from prior Docker
# runs. Persisted plugin registry state contains host-absolute paths that
# are not portable into Linux containers. Live-test auth/config staging does
# not need the old JSON source or the SQLite plugins.installedIndex machine-state row.
set +e
tar -C "$source_dir" \
--warning=no-file-changed \
--ignore-failed-read \
--exclude=workspace \
--exclude=sandboxes \
--exclude=plugins/installs.json \
--exclude=plugins/installs.json.migrated \
--exclude=relay.sock \
--exclude='*.sock' \
--exclude='*/*.sock' \
-cf - . | tar -C "$dest_dir" -xf -
local status=$?
set -e
if [ "$status" -gt 1 ]; then
return "$status"
fi
chmod -R u+rwX "$dest_dir" || true
openclaw_live_scrub_staged_plugin_index "$dest_dir"
if [ -d "$source_dir/workspace" ] && [ ! -e "$dest_dir/workspace" ]; then
ln -s "$source_dir/workspace" "$dest_dir/workspace"
fi
fi
export OPENCLAW_STATE_DIR="$dest_dir"
export OPENCLAW_CONFIG_PATH="$dest_dir/openclaw.json"
}
openclaw_live_prepare_staged_config() {
if [ ! -f "${OPENCLAW_CONFIG_PATH:-}" ]; then
return 0
fi
local scripts_dir="${OPENCLAW_LIVE_DOCKER_SCRIPTS_DIR:-/src/scripts}"
(
cd /app
node --import tsx "$scripts_dir/live-docker-normalize-config.ts"
)
}