Sync agent state from PC at 2026-05-15 16:32:18

This commit is contained in:
wangzhendong
2026-05-15 16:32:18 +08:00
parent 8e12d018dd
commit c090ee8359
11 changed files with 71 additions and 86 deletions

View File

@@ -13,7 +13,6 @@ $HeartbeatFile = Join-Path $PublicStateDir "heartbeat-pc.json"
$ErrorFile = Join-Path $PublicStateDir "error-pc.md"
$AllowedPaths = @(
"AGENT_BOARD.md",
"USER_STATUS.md",
"README.md",
".cursor/rules",
"handoff",
@@ -67,6 +66,26 @@ function Invoke-Git {
}
}
function Remove-StaleSyncError {
if (Test-Path $ErrorFile) {
Remove-Item $ErrorFile -Force
}
}
function Push-WithRetry {
& git push
if ($LASTEXITCODE -eq 0) {
return
}
# Another agent may have pushed between our pull and push. Rebase once and retry.
Invoke-Git @("pull", "--rebase", "--autostash")
if (Test-ConflictMarkers) {
throw "merge or rebase conflicts detected after push retry"
}
Invoke-Git @("push")
}
function Get-AllowedChanges {
$changes = & git -C $RepoRoot status --porcelain
if ($LASTEXITCODE -ne 0) {
@@ -148,6 +167,7 @@ function Sync-Once {
throw "merge or rebase conflicts detected"
}
Remove-StaleSyncError
Write-Heartbeat
$allowedChanges = @(Get-AllowedChanges)
@@ -171,15 +191,12 @@ function Sync-Once {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Invoke-Git @("commit", "-m", "Sync agent state from PC at $timestamp")
Invoke-Git @("push")
Push-WithRetry
}
do {
try {
Sync-Once
if (Test-Path $ErrorFile) {
Remove-Item $ErrorFile -Force
}
}
catch {
Write-SyncError $_.Exception.Message

View File

@@ -14,7 +14,6 @@ ERROR_FILE="$PUBLIC_STATE_DIR/error-vps.md"
ALLOWED_PATHS=(
"AGENT_BOARD.md"
"USER_STATUS.md"
"README.md"
".cursor/rules"
"handoff"
@@ -110,6 +109,23 @@ has_conflicts() {
[[ -n "$(git diff --name-only --diff-filter=U)" ]]
}
remove_stale_sync_error() {
rm -f "$ERROR_FILE"
}
push_with_retry() {
if git push; then
return 0
fi
# Another agent may have pushed between our pull and push. Rebase once and retry.
git pull --rebase --autostash
if has_conflicts; then
return 20
fi
git push
}
sync_once() {
cd "$REPO_ROOT"
ensure_state_dir
@@ -120,6 +136,7 @@ sync_once() {
return 20
fi
remove_stale_sync_error
write_heartbeat
if ! has_allowed_changes; then
@@ -138,13 +155,11 @@ sync_once() {
fi
git commit -m "Sync agent state from VPS at $(date '+%Y-%m-%d %H:%M:%S')"
git push
push_with_retry
}
while true; do
if sync_once; then
rm -f "$ERROR_FILE"
else
if ! sync_once; then
exit_code="$?"
write_sync_error "sync failed with exit code $exit_code"
echo "sync failed with exit code $exit_code" >&2