Sync 2026-04-14 08:03

This commit is contained in:
2026-04-14 08:03:42 +09:00
parent e365377625
commit eeed2f14d5
3 changed files with 89 additions and 86 deletions

Binary file not shown.

BIN
state_merged.db Normal file

Binary file not shown.

163
sync.sh
View File

@@ -8,24 +8,32 @@ cd "$SYNC_DIR"
echo "[$(date '+%H:%M:%S')] Sync from $HOSTNAME..." echo "[$(date '+%H:%M:%S')] Sync from $HOSTNAME..."
# ── Step 1: Export local state.db → state_<hostname>.db ────────────────── # ── Step 1: Export local state.db (via temp dir to avoid lock) ────────────
python3 << PYEOF python3 << PYEOF
import sqlite3, os, shutil import sqlite3, os, shutil, tempfile
local_db = os.path.join(os.environ['HERMES_HOME'], 'state.db') local_db = os.path.join(os.environ['HERMES_HOME'], 'state.db')
export_db = os.path.join(os.environ['SYNC_DIR'], f"state_{os.environ['HOSTNAME']}.db") export_db = os.path.join(os.environ['SYNC_DIR'], f"state_{os.environ['HOSTNAME']}.db")
tmpdir = tempfile.mkdtemp(prefix='hs_exp_')
# Checkpoint WAL
try: try:
conn = sqlite3.connect(local_db) conn = sqlite3.connect(local_db)
conn.execute('PRAGMA wal_checkpoint(TRUNCATE)') conn.execute('PRAGMA wal_checkpoint(TRUNCATE)')
conn.execute('PRAGMA optimize') conn.execute('PRAGMA optimize')
conn.close() conn.close()
except Exception as e:
print(f'Checkpoint: {e}', file=__import__('sys').stderr)
shutil.copy2(local_db, export_db) tmp_db = os.path.join(tmpdir, 'db')
print(f'Exported: {export_db}') shutil.copy2(local_db, tmp_db)
test = sqlite3.connect(tmp_db)
r = test.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
m = test.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
test.close()
shutil.copy2(tmp_db, export_db)
print(f'Exported: {r}s/{m}m')
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF PYEOF
# ── Step 2: Git stage ──────────────────────────────────────────────────── # ── Step 2: Git stage ────────────────────────────────────────────────────
@@ -65,42 +73,26 @@ fi
# ── Step 4: Merge all state_*.db → state_merged.db ────────────────────── # ── Step 4: Merge all state_*.db → state_merged.db ──────────────────────
python3 << 'PYEOF' python3 << 'PYEOF'
import sqlite3, os, glob, shutil import sqlite3, os, glob, shutil, tempfile
sync_dir = os.environ['SYNC_DIR'] sync_dir = os.environ['SYNC_DIR']
hermes_home = os.environ['HERMES_HOME']
merged_path = os.path.join(sync_dir, 'state_merged.db') merged_path = os.path.join(sync_dir, 'state_merged.db')
db_files = sorted(glob.glob(os.path.join(sync_dir, 'state_*.db'))) db_files = sorted(glob.glob(os.path.join(sync_dir, 'state_*.db')))
db_files = [f for f in db_files if not f.endswith('_merged.db')] db_files = [f for f in db_files if not f.endswith('_merged.db')]
print(f'DBs to merge: {[os.path.basename(f) for f in db_files]}') print(f'Merging {len(db_files)} DBs')
if os.path.exists(merged_path): tmpdir = tempfile.mkdtemp(prefix='hs_merge_')
os.remove(merged_path) tmp_merged = os.path.join(tmpdir, 'merged.db')
def get_schema(conn, table): try:
cols = conn.execute(f'PRAGMA table_info("{table}")').fetchall() # Create merged DB with proper schema
return [c[1] for c in cols] # column names conn = sqlite3.connect(tmp_merged)
conn.execute('PRAGMA journal_mode=DELETE')
conn.execute('PRAGMA locking_mode=NORMAL')
conn.execute('PRAGMA synchronous=FULL')
def copy_table(src_conn, dst_conn, table): conn.execute('''
cols = get_schema(src_conn, table) CREATE TABLE sessions (
placeholders = ','.join(['?'] * len(cols))
col_names = ','.join(f'"{c}"' for c in cols)
rows = src_conn.execute(f'SELECT {col_names} FROM "{table}"').fetchall()
for row in rows:
dst_conn.execute(f'INSERT OR REPLACE INTO "{table}" ({col_names}) VALUES ({placeholders})', row)
return len(rows)
# Open merged DB and rebuild from scratch
conn_merged = sqlite3.connect(merged_path)
conn_merged.execute('PRAGMA journal_mode=WAL')
conn_merged.execute('PRAGMA synchronous=NORMAL')
# We need at least one source to initialize schema
if not db_files:
print('No state DBs found, creating empty merged DB')
conn_merged.execute('''
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY, source TEXT NOT NULL, user_id TEXT, id TEXT PRIMARY KEY, source TEXT NOT NULL, user_id TEXT,
model TEXT, model_config TEXT, system_prompt TEXT, model TEXT, model_config TEXT, system_prompt TEXT,
parent_session_id TEXT, started_at REAL, ended_at REAL, parent_session_id TEXT, started_at REAL, ended_at REAL,
@@ -112,8 +104,9 @@ if not db_files:
estimated_cost_usd REAL, actual_cost_usd REAL, estimated_cost_usd REAL, actual_cost_usd REAL,
cost_status TEXT, cost_source TEXT, pricing_version TEXT, title TEXT cost_status TEXT, cost_source TEXT, pricing_version TEXT, title TEXT
)''') )''')
conn_merged.execute('''
CREATE TABLE IF NOT EXISTS messages ( conn.execute('''
CREATE TABLE messages (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
session_id TEXT NOT NULL, role TEXT NOT NULL, content TEXT, session_id TEXT NOT NULL, role TEXT NOT NULL, content TEXT,
tool_call_id TEXT, tool_calls TEXT, tool_name TEXT, tool_call_id TEXT, tool_calls TEXT, tool_name TEXT,
@@ -121,40 +114,50 @@ if not db_files:
finish_reason TEXT, reasoning TEXT, reasoning_details TEXT, finish_reason TEXT, reasoning TEXT, reasoning_details TEXT,
codex_reasoning_items TEXT codex_reasoning_items TEXT
)''') )''')
conn_merged.commit()
else:
first_db = db_files[0]
src_conn = sqlite3.connect(first_db)
src_conn.execute('PRAGMA wal_checkpoint(TRUNCATE)')
for table in ['sessions', 'messages']: conn.execute('CREATE INDEX idx_msg_session ON messages(session_id)')
cols = get_schema(src_conn, table) conn.execute('CREATE INDEX idx_msg_ts ON messages(timestamp)')
col_defs = []
for c in cols:
if c == 'id' and table == 'messages':
col_defs.append('id INTEGER PRIMARY KEY')
elif c == 'id':
col_defs.append(f'{c} TEXT PRIMARY KEY')
else:
col_defs.append(f'{c} TEXT')
conn_merged.execute(f'DROP TABLE IF EXISTS "{table}"') for db_file in db_files:
conn_merged.execute(f'CREATE TABLE "{table}" ({", ".join(col_defs)})') name = os.path.basename(db_file)
# Copy to temp file first (avoids WAL lock issues with open connections)
tmp_copy = os.path.join(tmpdir, name)
shutil.copy2(db_file, tmp_copy)
# Also copy WAL if exists
if os.path.exists(db_file + '-wal'):
shutil.copy2(db_file + '-wal', tmp_copy + '-wal')
placeholders = ','.join(['?'] * len(cols)) src = sqlite3.connect(tmp_copy)
col_names = ','.join(f'"{c}"' for c in cols)
rows = src_conn.execute(f'SELECT {col_names} FROM "{table}"').fetchall()
for row in rows:
conn_merged.execute(f'INSERT OR REPLACE INTO "{table}" ({col_names}) VALUES ({placeholders})', row)
print(f' {os.path.basename(first_db)}.{table}: {len(rows)} rows')
src_conn.close() s_cnt = src.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
m_cnt = src.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
print(f' {name}: {s_cnt}s/{m_cnt}m')
conn_merged.execute('PRAGMA wal_checkpoint(TRUNCATE)') sess_rows = src.execute('SELECT * FROM sessions').fetchall()
conn_merged.commit() sess_cols = len(src.execute('PRAGMA table_info(sessions)').fetchall())
conn_merged.close() for row in sess_rows:
conn.execute(f'INSERT OR REPLACE INTO sessions VALUES ({",".join(["?"]*sess_cols)})', row)
print(f'Merged: {merged_path} ({os.path.getsize(merged_path)/1024:.0f} KB)') msg_rows = src.execute('SELECT * FROM messages').fetchall()
msg_cols = len(src.execute('PRAGMA table_info(messages)').fetchall())
for row in msg_rows:
conn.execute(f'INSERT OR IGNORE INTO messages VALUES ({",".join(["?"]*msg_cols)})', row)
src.close()
os.remove(tmp_copy)
if os.path.exists(tmp_copy + '-wal'):
os.remove(tmp_copy + '-wal')
conn.commit()
conn.close()
if os.path.exists(merged_path):
os.remove(merged_path)
shutil.copy2(tmp_merged, merged_path)
print(f'Merged: {os.path.getsize(merged_path)/1024:.0f} KB')
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF PYEOF
# ── Step 5: Push ───────────────────────────────────────────────────────── # ── Step 5: Push ─────────────────────────────────────────────────────────
@@ -171,36 +174,36 @@ fi
# ── Step 6: Restore merged state to local hermes ───────────────────────── # ── Step 6: Restore merged state to local hermes ─────────────────────────
python3 << 'PYEOF' python3 << 'PYEOF'
import sqlite3, os, shutil import sqlite3, os, shutil, tempfile
sync_dir = os.environ['SYNC_DIR']
hermes_home = os.environ['HERMES_HOME'] hermes_home = os.environ['HERMES_HOME']
merged_path = os.path.join(sync_dir, 'state_merged.db') merged_path = os.path.join(os.environ['SYNC_DIR'], 'state_merged.db')
local_db = os.path.join(hermes_home, 'state.db') local_db = os.path.join(hermes_home, 'state.db')
if not os.path.exists(merged_path): if not os.path.exists(merged_path):
print('No merged DB, skipping restore') print('No merged DB, skipping restore')
else: else:
# Checkpoint + close local tmpdir = tempfile.mkdtemp(prefix='hs_rest_')
try: try:
conn = sqlite3.connect(local_db) conn = sqlite3.connect(local_db)
conn.execute('PRAGMA wal_checkpoint(TRUNCATE)') conn.execute('PRAGMA wal_checkpoint(TRUNCATE)')
conn.execute('PRAGMA optimize') conn.execute('PRAGMA optimize')
conn.close() conn.close()
except Exception as e:
print(f'Local checkpoint: {e}')
# Backup tmp_db = os.path.join(tmpdir, 'db')
shutil.copy2(local_db, local_db + '.pre_sync_bak') shutil.copy2(merged_path, tmp_db)
# Replace with merged test = sqlite3.connect(tmp_db)
shutil.copy2(merged_path, local_db) r = test.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]
print(f'Restored merged state to {local_db}') m = test.execute('SELECT COUNT(*) FROM messages').fetchone()[0]
test.close()
# Remove backup after successful restore shutil.copy2(local_db, local_db + '.bak')
bak = local_db + '.pre_sync_bak' shutil.copy2(tmp_db, local_db)
if os.path.exists(bak): os.remove(local_db + '.bak')
os.remove(bak) print(f'Restored: {r}s/{m}m')
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
PYEOF PYEOF
# ── Step 7: Sync memories + skills (additive) ──────────────────────────── # ── Step 7: Sync memories + skills (additive) ────────────────────────────