Sync agent state from PC at 2026-05-15 12:08:01

This commit is contained in:
wangzhendong
2026-05-15 12:08:01 +08:00
parent 30b9e9da54
commit dd420a5030
2 changed files with 119 additions and 7 deletions

View File

@@ -2,12 +2,15 @@
set -euo pipefail
INTERVAL_SECONDS="${INTERVAL_SECONDS:-30}"
HEARTBEAT_EVERY_SECONDS="${HEARTBEAT_EVERY_SECONDS:-60}"
ONCE="${1:-}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
STATE_DIR="$REPO_ROOT/.sync-state"
ERROR_FILE="$STATE_DIR/sync-error-vps.md"
LOCAL_STATE_DIR="$REPO_ROOT/.sync-state"
PUBLIC_STATE_DIR="$REPO_ROOT/sync-state"
HEARTBEAT_FILE="$PUBLIC_STATE_DIR/heartbeat-vps.json"
ERROR_FILE="$PUBLIC_STATE_DIR/error-vps.md"
ALLOWED_PATHS=(
"AGENT_BOARD.md"
@@ -18,11 +21,13 @@ ALLOWED_PATHS=(
"rollback"
"tasks"
"sync"
"sync-state"
".gitattributes"
".gitignore"
)
ensure_state_dir() {
mkdir -p "$STATE_DIR"
mkdir -p "$LOCAL_STATE_DIR" "$PUBLIC_STATE_DIR"
}
write_sync_error() {
@@ -41,6 +46,51 @@ Manual action required. Do not continue automatic sync until the repository is c
EOF
}
repo_clean_status() {
if [[ -z "$(git status --porcelain)" ]]; then
echo "clean"
else
echo "dirty"
fi
}
heartbeat_due() {
if [[ "$ONCE" == "--once" || ! -f "$HEARTBEAT_FILE" ]]; then
return 0
fi
local now last age
now="$(date +%s)"
last="$(stat -c %Y "$HEARTBEAT_FILE")"
age=$((now - last))
[[ "$age" -ge "$HEARTBEAT_EVERY_SECONDS" ]]
}
write_heartbeat() {
ensure_state_dir
if ! heartbeat_due; then
return 0
fi
local head branch status timestamp
head="$(git rev-parse --short HEAD)"
branch="$(git branch --show-current)"
status="$(repo_clean_status)"
timestamp="$(date '+%Y-%m-%dT%H:%M:%S%z')"
cat > "$HEARTBEAT_FILE" <<EOF
{
"host": "vps",
"time": "$timestamp",
"branch": "$branch",
"head": "$head",
"status": "$status",
"intervalSeconds": $INTERVAL_SECONDS,
"heartbeatEverySeconds": $HEARTBEAT_EVERY_SECONDS
}
EOF
}
has_allowed_changes() {
local line path allowed
while IFS= read -r line; do
@@ -69,6 +119,8 @@ sync_once() {
return 20
fi
write_heartbeat
if ! has_allowed_changes; then
return 0
fi