Spaces:
Running
Running
Anurag commited on
Implement sessions_marker to monitor session changes
Browse filesAdded sessions_marker function to track changes in the sessions directory and trigger immediate sync on session updates.
- openclaw-sync.py +23 -0
openclaw-sync.py
CHANGED
|
@@ -75,6 +75,7 @@ EXCLUDED_STATE_NAMES = {
|
|
| 75 |
"browser",
|
| 76 |
"npm",
|
| 77 |
}
|
|
|
|
| 78 |
WHATSAPP_CREDS_DIR = OPENCLAW_HOME / "credentials" / "whatsapp" / "default"
|
| 79 |
WHATSAPP_BACKUP_DIR = STATE_DIR / "credentials" / "whatsapp" / "default"
|
| 80 |
RESET_MARKER = WORKSPACE / ".reset_credentials"
|
|
@@ -525,6 +526,15 @@ def is_valid_json_file(path: Path) -> bool:
|
|
| 525 |
return False
|
| 526 |
|
| 527 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
def wait_for_config_settle(config_marker: tuple[int, int, int]) -> tuple[str, tuple[int, int, int]]:
|
| 529 |
stable_since = time.monotonic()
|
| 530 |
current_marker = config_marker
|
|
@@ -549,12 +559,23 @@ def wait_for_config_settle(config_marker: tuple[int, int, int]) -> tuple[str, tu
|
|
| 549 |
|
| 550 |
def wait_for_sync_trigger(config_marker: tuple[int, int, int]) -> tuple[str, tuple[int, int, int]]:
|
| 551 |
deadline = time.monotonic() + max(0, INTERVAL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
while not STOP_EVENT.is_set():
|
| 554 |
current_config_marker = file_marker(OPENCLAW_CONFIG_FILE)
|
| 555 |
if current_config_marker != config_marker:
|
| 556 |
return wait_for_config_settle(current_config_marker)
|
| 557 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
remaining = deadline - time.monotonic()
|
| 559 |
if remaining <= 0:
|
| 560 |
return ("interval", current_config_marker)
|
|
@@ -633,6 +654,8 @@ def loop() -> int:
|
|
| 633 |
break
|
| 634 |
if trigger == "settled":
|
| 635 |
print("OpenClaw config changed and settled; syncing immediately.")
|
|
|
|
|
|
|
| 636 |
|
| 637 |
return 0
|
| 638 |
|
|
|
|
| 75 |
"browser",
|
| 76 |
"npm",
|
| 77 |
}
|
| 78 |
+
SESSIONS_DIR = OPENCLAW_HOME / "agents" / "main" / "sessions"
|
| 79 |
WHATSAPP_CREDS_DIR = OPENCLAW_HOME / "credentials" / "whatsapp" / "default"
|
| 80 |
WHATSAPP_BACKUP_DIR = STATE_DIR / "credentials" / "whatsapp" / "default"
|
| 81 |
RESET_MARKER = WORKSPACE / ".reset_credentials"
|
|
|
|
| 526 |
return False
|
| 527 |
|
| 528 |
|
| 529 |
+
def sessions_marker() -> tuple[int, int, int, str]:
|
| 530 |
+
"""Return a lightweight marker for the sessions directory.
|
| 531 |
+
|
| 532 |
+
Uses the same metadata_marker() logic so any new, deleted, or modified
|
| 533 |
+
session file is detected without hashing file contents.
|
| 534 |
+
"""
|
| 535 |
+
return metadata_marker(SESSIONS_DIR)
|
| 536 |
+
|
| 537 |
+
|
| 538 |
def wait_for_config_settle(config_marker: tuple[int, int, int]) -> tuple[str, tuple[int, int, int]]:
|
| 539 |
stable_since = time.monotonic()
|
| 540 |
current_marker = config_marker
|
|
|
|
| 559 |
|
| 560 |
def wait_for_sync_trigger(config_marker: tuple[int, int, int]) -> tuple[str, tuple[int, int, int]]:
|
| 561 |
deadline = time.monotonic() + max(0, INTERVAL)
|
| 562 |
+
# BUG FIX: also watch sessions directory so new/updated sessions
|
| 563 |
+
# trigger an immediate sync instead of waiting the full interval.
|
| 564 |
+
# Without this, sessions created between 180-second intervals were
|
| 565 |
+
# lost when the container restarted (e.g. HF Space going to sleep).
|
| 566 |
+
last_sessions_marker = sessions_marker()
|
| 567 |
|
| 568 |
while not STOP_EVENT.is_set():
|
| 569 |
current_config_marker = file_marker(OPENCLAW_CONFIG_FILE)
|
| 570 |
if current_config_marker != config_marker:
|
| 571 |
return wait_for_config_settle(current_config_marker)
|
| 572 |
|
| 573 |
+
# Sessions changed -> trigger sync immediately (no settle needed;
|
| 574 |
+
# session files are written atomically by OpenClaw).
|
| 575 |
+
current_sessions_marker = sessions_marker()
|
| 576 |
+
if current_sessions_marker != last_sessions_marker:
|
| 577 |
+
return ("sessions", current_config_marker)
|
| 578 |
+
|
| 579 |
remaining = deadline - time.monotonic()
|
| 580 |
if remaining <= 0:
|
| 581 |
return ("interval", current_config_marker)
|
|
|
|
| 654 |
break
|
| 655 |
if trigger == "settled":
|
| 656 |
print("OpenClaw config changed and settled; syncing immediately.")
|
| 657 |
+
if trigger == "sessions":
|
| 658 |
+
print("Session files changed; syncing immediately.")
|
| 659 |
|
| 660 |
return 0
|
| 661 |
|