Spaces:
Paused
Paused
Merge pull request #217 from anurag008w/feat/key-max-wait-default-zero-and-git-sync
Browse files- .env.example +8 -0
- README.md +1 -0
- jupyter-devdata-sync.py +32 -0
- multi-provider-key-rotator.cjs +5 -3
- openclaw-sync.py +36 -0
.env.example
CHANGED
|
@@ -235,6 +235,14 @@ LLM_API_KEY_FALLBACK_ENABLED=true
|
|
| 235 |
# Set false if you prefer strict withholding until cooldown expiry.
|
| 236 |
# KEY_USE_SUSPENDED_AS_LAST_RESORT=true
|
| 237 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
#
|
| 239 |
# Optional diagnostics log snapshot for key pools (default: off).
|
| 240 |
# KEY_ROTATOR_DIAGNOSTICS=true
|
|
|
|
| 235 |
# Set false if you prefer strict withholding until cooldown expiry.
|
| 236 |
# KEY_USE_SUSPENDED_AS_LAST_RESORT=true
|
| 237 |
#
|
| 238 |
+
# Max ms to sleep waiting for the soonest suspended key to recover before
|
| 239 |
+
# forwarding the request anyway (default: 0 = disabled / fire-and-miss).
|
| 240 |
+
# When all keys are in cooldown and KEY_USE_SUSPENDED_AS_LAST_RESORT is true,
|
| 241 |
+
# setting this to a positive value (e.g. 20000) lets the rotator sleep up to
|
| 242 |
+
# that long so the request has a real chance of succeeding instead of hitting
|
| 243 |
+
# a guaranteed 429. Leave at 0 if you prefer the old immediate-forward behaviour.
|
| 244 |
+
# KEY_MAX_WAIT_MS=0
|
| 245 |
+
#
|
| 246 |
#
|
| 247 |
# Optional diagnostics log snapshot for key pools (default: off).
|
| 248 |
# KEY_ROTATOR_DIAGNOSTICS=true
|
README.md
CHANGED
|
@@ -308,6 +308,7 @@ Optional tuning:
|
|
| 308 |
- `KEY_STICKY_PROVIDERS` (default `gemini`) — comma-separated provider names that should use sticky key selection instead of per-request round-robin.
|
| 309 |
- `KEY_STICKY_SCOPE` (default `auto`) — `auto` uses per-model sticky buckets for Gemini/per-model providers and provider-level buckets for others; set `provider` or `model` to override.
|
| 310 |
- `KEY_FETCH_MAX_RETRIES` (default `0`) — optional auto-retry count for retryable failures on **GET/HEAD/OPTIONS/POST** with a different key. Default `0` means the rotator does **not** spend extra upstream attempts for a single caller request.
|
|
|
|
| 311 |
- `KEY_FETCH_RETRY_BASE_DELAY_MS` (default `250`) — base delay for retry backoff (respects `Retry-After`, capped to 10s).
|
| 312 |
- `KEY_ROTATOR_ASSERT_NO_EXTRA_CALLS=true` — optional diagnostic warning if a single caller fetch creates more than one upstream provider attempt.
|
| 313 |
- `KEY_ROTATOR_EMIT_SYNTHETIC_EVENTS=true` — optional local-only dashboard probe; with `SYNTHETIC_API_KEYS` configured, emits synthetic rotator events without sending an upstream provider request.
|
|
|
|
| 308 |
- `KEY_STICKY_PROVIDERS` (default `gemini`) — comma-separated provider names that should use sticky key selection instead of per-request round-robin.
|
| 309 |
- `KEY_STICKY_SCOPE` (default `auto`) — `auto` uses per-model sticky buckets for Gemini/per-model providers and provider-level buckets for others; set `provider` or `model` to override.
|
| 310 |
- `KEY_FETCH_MAX_RETRIES` (default `0`) — optional auto-retry count for retryable failures on **GET/HEAD/OPTIONS/POST** with a different key. Default `0` means the rotator does **not** spend extra upstream attempts for a single caller request.
|
| 311 |
+
- `KEY_MAX_WAIT_MS` (default `0` / disabled) — when all keys are suspended and best-effort mode is active, sleep up to this many ms for the soonest-recovering key before forwarding the request. `0` (default) preserves the old fire-and-miss behaviour; set e.g. `20000` to give suspended keys a real recovery window instead of forwarding into a guaranteed 429.
|
| 312 |
- `KEY_FETCH_RETRY_BASE_DELAY_MS` (default `250`) — base delay for retry backoff (respects `Retry-After`, capped to 10s).
|
| 313 |
- `KEY_ROTATOR_ASSERT_NO_EXTRA_CALLS=true` — optional diagnostic warning if a single caller fetch creates more than one upstream provider attempt.
|
| 314 |
- `KEY_ROTATOR_EMIT_SYNTHETIC_EVENTS=true` — optional local-only dashboard probe; with `SYNTHETIC_API_KEYS` configured, emits synthetic rotator events without sending an upstream provider request.
|
jupyter-devdata-sync.py
CHANGED
|
@@ -18,6 +18,34 @@ MAX_FILE_SIZE_BYTES = int(
|
|
| 18 |
# Max stale files to delete per commit. Mirrors openclaw-sync.py behaviour.
|
| 19 |
# Override via DEVDATA_PRUNE_BATCH_SIZE.
|
| 20 |
PRUNE_BATCH_SIZE = int((os.environ.get("DEVDATA_PRUNE_BATCH_SIZE", "").strip() or "50"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Reserved filenames previously generated by DevData. Never copy or restore
|
| 22 |
# them as user content, and let prune delete any old remote marker files.
|
| 23 |
RESERVED_SYNC_FILENAMES = {".huggingclaw-empty-dir"}
|
|
@@ -287,6 +315,8 @@ def restore_once(api, rid: str):
|
|
| 287 |
tmp = Path(tempfile.mkdtemp(prefix="devdata-restore-"))
|
| 288 |
try:
|
| 289 |
snapshot_download(repo_id=rid, repo_type="dataset", local_dir=str(tmp), local_dir_use_symlinks=False, token=HF_TOKEN)
|
|
|
|
|
|
|
| 290 |
for p in tmp.rglob("*"):
|
| 291 |
rel = p.relative_to(tmp)
|
| 292 |
if str(rel) == ".gitattributes":
|
|
@@ -372,6 +402,8 @@ def sync_loop(api, rid: str):
|
|
| 372 |
tmp = Path(tempfile.mkdtemp(prefix="devdata-snap-"))
|
| 373 |
try:
|
| 374 |
had_copy_failures, protected_large_files = snapshot(JUPYTER_ROOT, tmp)
|
|
|
|
|
|
|
| 375 |
upload_folder(
|
| 376 |
folder_path=str(tmp),
|
| 377 |
repo_id=rid,
|
|
|
|
| 18 |
# Max stale files to delete per commit. Mirrors openclaw-sync.py behaviour.
|
| 19 |
# Override via DEVDATA_PRUNE_BATCH_SIZE.
|
| 20 |
PRUNE_BATCH_SIZE = int((os.environ.get("DEVDATA_PRUNE_BATCH_SIZE", "").strip() or "50"))
|
| 21 |
+
|
| 22 |
+
# huggingface_hub.upload_folder hard-blocks .git/ via DEFAULT_IGNORE_PATTERNS
|
| 23 |
+
# regardless of the ignore_patterns= argument. Escape before upload and
|
| 24 |
+
# unescape after restore so git repos inside the Jupyter workspace survive
|
| 25 |
+
# Space restarts.
|
| 26 |
+
_GIT_ESCAPE = "__dot_git__"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _escape_git_dirs(root: Path) -> None:
|
| 30 |
+
"""Rename every .git dir inside *root* to __dot_git__ before upload_folder."""
|
| 31 |
+
for dirpath, dirnames, _ in os.walk(root, topdown=False):
|
| 32 |
+
for dirname in list(dirnames):
|
| 33 |
+
if dirname == ".git":
|
| 34 |
+
src = Path(dirpath) / ".git"
|
| 35 |
+
dst = Path(dirpath) / _GIT_ESCAPE
|
| 36 |
+
if src.exists() and not dst.exists():
|
| 37 |
+
src.rename(dst)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _unescape_git_dirs(root: Path) -> None:
|
| 41 |
+
"""Rename every __dot_git__ dir inside *root* back to .git after restore."""
|
| 42 |
+
for dirpath, dirnames, _ in os.walk(root, topdown=False):
|
| 43 |
+
for dirname in list(dirnames):
|
| 44 |
+
if dirname == _GIT_ESCAPE:
|
| 45 |
+
src = Path(dirpath) / _GIT_ESCAPE
|
| 46 |
+
dst = Path(dirpath) / ".git"
|
| 47 |
+
if src.exists() and not dst.exists():
|
| 48 |
+
src.rename(dst)
|
| 49 |
# Reserved filenames previously generated by DevData. Never copy or restore
|
| 50 |
# them as user content, and let prune delete any old remote marker files.
|
| 51 |
RESERVED_SYNC_FILENAMES = {".huggingclaw-empty-dir"}
|
|
|
|
| 315 |
tmp = Path(tempfile.mkdtemp(prefix="devdata-restore-"))
|
| 316 |
try:
|
| 317 |
snapshot_download(repo_id=rid, repo_type="dataset", local_dir=str(tmp), local_dir_use_symlinks=False, token=HF_TOKEN)
|
| 318 |
+
# Undo the .git → __dot_git__ escaping applied before upload.
|
| 319 |
+
_unescape_git_dirs(tmp)
|
| 320 |
for p in tmp.rglob("*"):
|
| 321 |
rel = p.relative_to(tmp)
|
| 322 |
if str(rel) == ".gitattributes":
|
|
|
|
| 402 |
tmp = Path(tempfile.mkdtemp(prefix="devdata-snap-"))
|
| 403 |
try:
|
| 404 |
had_copy_failures, protected_large_files = snapshot(JUPYTER_ROOT, tmp)
|
| 405 |
+
# Escape .git → __dot_git__ so upload_folder doesn't hard-block them.
|
| 406 |
+
_escape_git_dirs(tmp)
|
| 407 |
upload_folder(
|
| 408 |
folder_path=str(tmp),
|
| 409 |
repo_id=rid,
|
multi-provider-key-rotator.cjs
CHANGED
|
@@ -209,10 +209,12 @@ const MAX_RETRY_AFTER_MS = Math.max(
|
|
| 209 |
// sleep never causes a proxy timeout. With large key pools (50+) this
|
| 210 |
// path rarely triggers anyway; with 2-3 keys it prevents the rotator from
|
| 211 |
// sleeping longer than the upstream proxy allows.
|
| 212 |
-
//
|
|
|
|
|
|
|
| 213 |
const MAX_KEY_WAIT_MS = Math.max(
|
| 214 |
0,
|
| 215 |
-
parseInt(process.env.KEY_MAX_WAIT_MS || '', 10) ||
|
| 216 |
);
|
| 217 |
|
| 218 |
// Long suspend window for exhausted/invalid keys.
|
|
@@ -2338,7 +2340,7 @@ function patchFetch() {
|
|
| 2338 |
// Old behaviour fired immediately into a guaranteed 429 (fake cycle).
|
| 2339 |
// Now we sleep until the key's cooldown expires so the request has a
|
| 2340 |
// real chance of succeeding. Capped by MAX_KEY_WAIT_MS (env-tunable,
|
| 2341 |
-
// default
|
| 2342 |
if (key && waitMs > 0 && MAX_KEY_WAIT_MS > 0) {
|
| 2343 |
const actualWait = Math.min(waitMs, MAX_KEY_WAIT_MS);
|
| 2344 |
await sleep(actualWait);
|
|
|
|
| 209 |
// sleep never causes a proxy timeout. With large key pools (50+) this
|
| 210 |
// path rarely triggers anyway; with 2-3 keys it prevents the rotator from
|
| 211 |
// sleeping longer than the upstream proxy allows.
|
| 212 |
+
// Default is 0 (disabled) — fire-and-miss on all-suspended pools.
|
| 213 |
+
// Set to a positive value (e.g. 20000) to sleep up to that many ms for the
|
| 214 |
+
// soonest-recovering key instead of immediately forwarding into a guaranteed 429.
|
| 215 |
const MAX_KEY_WAIT_MS = Math.max(
|
| 216 |
0,
|
| 217 |
+
parseInt(process.env.KEY_MAX_WAIT_MS || '', 10) || 0,
|
| 218 |
);
|
| 219 |
|
| 220 |
// Long suspend window for exhausted/invalid keys.
|
|
|
|
| 2340 |
// Old behaviour fired immediately into a guaranteed 429 (fake cycle).
|
| 2341 |
// Now we sleep until the key's cooldown expires so the request has a
|
| 2342 |
// real chance of succeeding. Capped by MAX_KEY_WAIT_MS (env-tunable,
|
| 2343 |
+
// default 0 / disabled) so we never stall indefinitely.
|
| 2344 |
if (key && waitMs > 0 && MAX_KEY_WAIT_MS > 0) {
|
| 2345 |
const actualWait = Math.min(waitMs, MAX_KEY_WAIT_MS);
|
| 2346 |
await sleep(actualWait);
|
openclaw-sync.py
CHANGED
|
@@ -31,6 +31,34 @@ os.environ.setdefault("HF_HUB_VERBOSITY", "error")
|
|
| 31 |
from huggingface_hub import CommitOperationDelete, HfApi, snapshot_download, upload_folder
|
| 32 |
from huggingface_hub.errors import HfHubHTTPError, RepositoryNotFoundError
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Belt-and-suspenders: also raise the level after import in case the env var
|
| 35 |
# wasn't honored (older hub versions, or message logged via a sub-logger).
|
| 36 |
logging.getLogger("huggingface_hub").setLevel(logging.ERROR)
|
|
@@ -768,6 +796,10 @@ def create_snapshot_dir(source_root: Path) -> Path:
|
|
| 768 |
raise RuntimeError(
|
| 769 |
f"Snapshot changed while copying {rel_posix}; retrying next sync pass."
|
| 770 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
return staging_root
|
| 772 |
|
| 773 |
|
|
@@ -930,6 +962,10 @@ def restore_workspace() -> bool:
|
|
| 930 |
write_status("fresh", "Backup dataset is empty. Starting fresh.")
|
| 931 |
return True
|
| 932 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 933 |
# Build the restored workspace in a staging dir without touching the
|
| 934 |
# live workspace. Only swap once staging is fully written so a copy
|
| 935 |
# failure mid-way can never leave the workspace in a partial state.
|
|
|
|
| 31 |
from huggingface_hub import CommitOperationDelete, HfApi, snapshot_download, upload_folder
|
| 32 |
from huggingface_hub.errors import HfHubHTTPError, RepositoryNotFoundError
|
| 33 |
|
| 34 |
+
# huggingface_hub.upload_folder always appends DEFAULT_IGNORE_PATTERNS which
|
| 35 |
+
# hard-blocks every .git/ subtree regardless of ignore_patterns= argument.
|
| 36 |
+
# Work around this by escaping .git → __dot_git__ in the local snapshot before
|
| 37 |
+
# upload, then unescaping after snapshot_download on restore.
|
| 38 |
+
_GIT_ESCAPE = "__dot_git__"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _escape_git_dirs(root: Path) -> None:
|
| 42 |
+
"""Rename every .git dir inside *root* to __dot_git__ so upload_folder uploads them."""
|
| 43 |
+
for dirpath, dirnames, _ in os.walk(root, topdown=False):
|
| 44 |
+
for dirname in list(dirnames):
|
| 45 |
+
if dirname == ".git":
|
| 46 |
+
src = Path(dirpath) / ".git"
|
| 47 |
+
dst = Path(dirpath) / _GIT_ESCAPE
|
| 48 |
+
if src.exists() and not dst.exists():
|
| 49 |
+
src.rename(dst)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _unescape_git_dirs(root: Path) -> None:
|
| 53 |
+
"""Rename every __dot_git__ dir inside *root* back to .git after snapshot_download."""
|
| 54 |
+
for dirpath, dirnames, _ in os.walk(root, topdown=False):
|
| 55 |
+
for dirname in list(dirnames):
|
| 56 |
+
if dirname == _GIT_ESCAPE:
|
| 57 |
+
src = Path(dirpath) / _GIT_ESCAPE
|
| 58 |
+
dst = Path(dirpath) / ".git"
|
| 59 |
+
if src.exists() and not dst.exists():
|
| 60 |
+
src.rename(dst)
|
| 61 |
+
|
| 62 |
# Belt-and-suspenders: also raise the level after import in case the env var
|
| 63 |
# wasn't honored (older hub versions, or message logged via a sub-logger).
|
| 64 |
logging.getLogger("huggingface_hub").setLevel(logging.ERROR)
|
|
|
|
| 796 |
raise RuntimeError(
|
| 797 |
f"Snapshot changed while copying {rel_posix}; retrying next sync pass."
|
| 798 |
)
|
| 799 |
+
# Escape .git dirs so upload_folder (which hard-blocks .git via
|
| 800 |
+
# DEFAULT_IGNORE_PATTERNS) can upload them. _unescape_git_dirs is called
|
| 801 |
+
# in restore_workspace after snapshot_download.
|
| 802 |
+
_escape_git_dirs(staging_root)
|
| 803 |
return staging_root
|
| 804 |
|
| 805 |
|
|
|
|
| 962 |
write_status("fresh", "Backup dataset is empty. Starting fresh.")
|
| 963 |
return True
|
| 964 |
|
| 965 |
+
# Undo the .git → __dot_git__ escaping applied before upload
|
| 966 |
+
# (upload_folder hard-blocks .git via DEFAULT_IGNORE_PATTERNS).
|
| 967 |
+
_unescape_git_dirs(tmp_path)
|
| 968 |
+
|
| 969 |
# Build the restored workspace in a staging dir without touching the
|
| 970 |
# live workspace. Only swap once staging is fully written so a copy
|
| 971 |
# failure mid-way can never leave the workspace in a partial state.
|