Update recursive_context.py
Browse files- recursive_context.py +23 -61
recursive_context.py
CHANGED
|
@@ -398,66 +398,33 @@ class RecursiveContextManager:
|
|
| 398 |
return f"STDOUT: {result.stdout}\nSTDERR: {result.stderr}"
|
| 399 |
except Exception as e:
|
| 400 |
return f"Execution Error: {e}"
|
| 401 |
-
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
| 403 |
token = os.getenv("GITHUB_TOKEN")
|
| 404 |
repo = os.getenv("GITHUB_REPO")
|
| 405 |
|
| 406 |
if not token or not repo:
|
| 407 |
-
return
|
| 408 |
-
|
| 409 |
-
# authenticated URL
|
| 410 |
-
remote_url = f"https://{token}@github.com/{repo}.git"
|
| 411 |
-
|
| 412 |
-
try:
|
| 413 |
-
# 1. Initialize if needed (Docker containers often lack .git)
|
| 414 |
-
if not (self.repo_path / ".git").exists():
|
| 415 |
-
subprocess.run(["git", "init"], cwd=self.repo_path, check=True)
|
| 416 |
-
subprocess.run(["git", "config", "user.email", "clawdbot@e-t-systems.ai"], cwd=self.repo_path)
|
| 417 |
-
subprocess.run(["git", "config", "user.name", "Clawdbot"], cwd=self.repo_path)
|
| 418 |
-
subprocess.run(["git", "branch", "-M", "main"], cwd=self.repo_path)
|
| 419 |
-
|
| 420 |
-
# 2. Configure Remote (Idempotent)
|
| 421 |
-
# Remove existing remote to ensure token is fresh/correct
|
| 422 |
-
subprocess.run(["git", "remote", "remove", "origin"], cwd=self.repo_path, stderr=subprocess.DEVNULL)
|
| 423 |
-
subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=self.repo_path, check=True)
|
| 424 |
-
|
| 425 |
-
# 3. Add, Commit, Push
|
| 426 |
-
subprocess.run(["git", "add", "."], cwd=self.repo_path, check=True)
|
| 427 |
-
|
| 428 |
-
# Commit (allow empty if nothing changed)
|
| 429 |
-
commit_res = subprocess.run(
|
| 430 |
-
["git", "commit", "-m", commit_message],
|
| 431 |
-
cwd=self.repo_path, capture_output=True, text=True
|
| 432 |
-
)
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
|
| 440 |
-
|
| 441 |
-
return f"✅ Successfully pushed to GitHub: https://github.com/{repo}"
|
| 442 |
-
else:
|
| 443 |
-
return f"⚠️ Git Push Failed: {push_res.stderr}"
|
| 444 |
-
|
| 445 |
-
except Exception as e:
|
| 446 |
-
return f"❌ Critical Git Error: {e}"
|
| 447 |
|
| 448 |
def push_to_github(self, commit_message="Auto-backup from Clawdbot"):
|
| 449 |
"""Pushes the current workspace to the configured GitHub repository."""
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
if not token or not repo:
|
| 454 |
return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
|
| 455 |
-
|
| 456 |
-
# authenticated URL
|
| 457 |
-
remote_url = f"https://{token}@github.com/{repo}.git"
|
| 458 |
|
| 459 |
try:
|
| 460 |
-
# 1. Initialize if needed
|
| 461 |
if not (self.repo_path / ".git").exists():
|
| 462 |
subprocess.run(["git", "init"], cwd=self.repo_path, check=True)
|
| 463 |
subprocess.run(["git", "config", "user.email", "clawdbot@e-t-systems.ai"], cwd=self.repo_path)
|
|
@@ -465,7 +432,6 @@ class RecursiveContextManager:
|
|
| 465 |
subprocess.run(["git", "branch", "-M", "main"], cwd=self.repo_path)
|
| 466 |
|
| 467 |
# 2. Configure Remote (Idempotent)
|
| 468 |
-
# Remove existing remote to ensure token is fresh/correct
|
| 469 |
subprocess.run(["git", "remote", "remove", "origin"], cwd=self.repo_path, stderr=subprocess.DEVNULL)
|
| 470 |
subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=self.repo_path, check=True)
|
| 471 |
|
|
@@ -478,14 +444,16 @@ class RecursiveContextManager:
|
|
| 478 |
cwd=self.repo_path, capture_output=True, text=True
|
| 479 |
)
|
| 480 |
|
| 481 |
-
# Push (force is safer for a backup mirror
|
| 482 |
push_res = subprocess.run(
|
| 483 |
["git", "push", "-u", "origin", "main", "--force"],
|
| 484 |
cwd=self.repo_path, capture_output=True, text=True
|
| 485 |
)
|
| 486 |
|
| 487 |
if push_res.returncode == 0:
|
| 488 |
-
|
|
|
|
|
|
|
| 489 |
else:
|
| 490 |
return f"⚠️ Git Push Failed: {push_res.stderr}"
|
| 491 |
|
|
@@ -494,13 +462,9 @@ class RecursiveContextManager:
|
|
| 494 |
|
| 495 |
def pull_from_github(self, branch="main"):
|
| 496 |
"""Hard reset: Destroys local changes and pulls clean code from GitHub."""
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
if not token or not repo:
|
| 501 |
return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
|
| 502 |
-
|
| 503 |
-
remote_url = f"https://{token}@github.com/{repo}.git"
|
| 504 |
|
| 505 |
try:
|
| 506 |
# 1. Init if missing
|
|
@@ -555,11 +519,10 @@ class RecursiveContextManager:
|
|
| 555 |
"""Reads the Working Memory notebook."""
|
| 556 |
notes = self._load_notebook()
|
| 557 |
if not notes:
|
| 558 |
-
return ""
|
| 559 |
|
| 560 |
display = []
|
| 561 |
for i, note in enumerate(notes):
|
| 562 |
-
# Format: "1. [2026-02-03 14:00] Note content..."
|
| 563 |
display.append(f"{i+1}. [{note['timestamp']}] {note['content']}")
|
| 564 |
return "\n".join(display)
|
| 565 |
|
|
@@ -567,7 +530,6 @@ class RecursiveContextManager:
|
|
| 567 |
"""Adds a new note to the notebook."""
|
| 568 |
notes = self._load_notebook()
|
| 569 |
|
| 570 |
-
# Pocket Notebook Limit (Force curation)
|
| 571 |
if len(notes) >= 25:
|
| 572 |
return "⚠️ Notebook full (25/25 slots). Please delete obsolete notes first."
|
| 573 |
|
|
@@ -580,7 +542,6 @@ class RecursiveContextManager:
|
|
| 580 |
"""Deletes a note by its number (1-based index)."""
|
| 581 |
notes = self._load_notebook()
|
| 582 |
try:
|
| 583 |
-
# Convert 1-based index (User view) to 0-based (List view)
|
| 584 |
idx = int(index) - 1
|
| 585 |
if 0 <= idx < len(notes):
|
| 586 |
removed = notes.pop(idx)
|
|
@@ -590,6 +551,7 @@ class RecursiveContextManager:
|
|
| 590 |
return f"❌ Invalid index: {index}. Valid range: 1-{len(notes)}"
|
| 591 |
except ValueError:
|
| 592 |
return "❌ Index must be a number."
|
|
|
|
| 593 |
# =====================================================================
|
| 594 |
# RECURSIVE SEARCH TOOLS
|
| 595 |
# =====================================================================
|
|
|
|
| 398 |
return f"STDOUT: {result.stdout}\nSTDERR: {result.stderr}"
|
| 399 |
except Exception as e:
|
| 400 |
return f"Execution Error: {e}"
|
| 401 |
+
# =====================================================================
|
| 402 |
+
# GIT TOOLS (Fixed for Double-URL Issue)
|
| 403 |
+
# =====================================================================
|
| 404 |
+
def _get_authenticated_remote_url(self):
|
| 405 |
+
"""Helper to construct the correct authenticated URL."""
|
| 406 |
token = os.getenv("GITHUB_TOKEN")
|
| 407 |
repo = os.getenv("GITHUB_REPO")
|
| 408 |
|
| 409 |
if not token or not repo:
|
| 410 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
+
# Clean the repo string if it's already a full URL
|
| 413 |
+
if repo.startswith("https://github.com/"):
|
| 414 |
+
repo = repo.replace("https://github.com/", "")
|
| 415 |
+
if repo.endswith(".git"):
|
| 416 |
+
repo = repo[:-4]
|
| 417 |
|
| 418 |
+
return f"https://{token}@github.com/{repo}.git"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
def push_to_github(self, commit_message="Auto-backup from Clawdbot"):
|
| 421 |
"""Pushes the current workspace to the configured GitHub repository."""
|
| 422 |
+
remote_url = self._get_authenticated_remote_url()
|
| 423 |
+
if not remote_url:
|
|
|
|
|
|
|
| 424 |
return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
try:
|
| 427 |
+
# 1. Initialize if needed
|
| 428 |
if not (self.repo_path / ".git").exists():
|
| 429 |
subprocess.run(["git", "init"], cwd=self.repo_path, check=True)
|
| 430 |
subprocess.run(["git", "config", "user.email", "clawdbot@e-t-systems.ai"], cwd=self.repo_path)
|
|
|
|
| 432 |
subprocess.run(["git", "branch", "-M", "main"], cwd=self.repo_path)
|
| 433 |
|
| 434 |
# 2. Configure Remote (Idempotent)
|
|
|
|
| 435 |
subprocess.run(["git", "remote", "remove", "origin"], cwd=self.repo_path, stderr=subprocess.DEVNULL)
|
| 436 |
subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=self.repo_path, check=True)
|
| 437 |
|
|
|
|
| 444 |
cwd=self.repo_path, capture_output=True, text=True
|
| 445 |
)
|
| 446 |
|
| 447 |
+
# Push (force is safer for a backup mirror)
|
| 448 |
push_res = subprocess.run(
|
| 449 |
["git", "push", "-u", "origin", "main", "--force"],
|
| 450 |
cwd=self.repo_path, capture_output=True, text=True
|
| 451 |
)
|
| 452 |
|
| 453 |
if push_res.returncode == 0:
|
| 454 |
+
# Clean URL for display security
|
| 455 |
+
display_url = remote_url.replace(os.getenv("GITHUB_TOKEN"), "TOKEN_HIDDEN")
|
| 456 |
+
return f"✅ Successfully pushed to GitHub"
|
| 457 |
else:
|
| 458 |
return f"⚠️ Git Push Failed: {push_res.stderr}"
|
| 459 |
|
|
|
|
| 462 |
|
| 463 |
def pull_from_github(self, branch="main"):
|
| 464 |
"""Hard reset: Destroys local changes and pulls clean code from GitHub."""
|
| 465 |
+
remote_url = self._get_authenticated_remote_url()
|
| 466 |
+
if not remote_url:
|
|
|
|
|
|
|
| 467 |
return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
|
|
|
|
|
|
|
| 468 |
|
| 469 |
try:
|
| 470 |
# 1. Init if missing
|
|
|
|
| 519 |
"""Reads the Working Memory notebook."""
|
| 520 |
notes = self._load_notebook()
|
| 521 |
if not notes:
|
| 522 |
+
return ""
|
| 523 |
|
| 524 |
display = []
|
| 525 |
for i, note in enumerate(notes):
|
|
|
|
| 526 |
display.append(f"{i+1}. [{note['timestamp']}] {note['content']}")
|
| 527 |
return "\n".join(display)
|
| 528 |
|
|
|
|
| 530 |
"""Adds a new note to the notebook."""
|
| 531 |
notes = self._load_notebook()
|
| 532 |
|
|
|
|
| 533 |
if len(notes) >= 25:
|
| 534 |
return "⚠️ Notebook full (25/25 slots). Please delete obsolete notes first."
|
| 535 |
|
|
|
|
| 542 |
"""Deletes a note by its number (1-based index)."""
|
| 543 |
notes = self._load_notebook()
|
| 544 |
try:
|
|
|
|
| 545 |
idx = int(index) - 1
|
| 546 |
if 0 <= idx < len(notes):
|
| 547 |
removed = notes.pop(idx)
|
|
|
|
| 551 |
return f"❌ Invalid index: {index}. Valid range: 1-{len(notes)}"
|
| 552 |
except ValueError:
|
| 553 |
return "❌ Index must be a number."
|
| 554 |
+
|
| 555 |
# =====================================================================
|
| 556 |
# RECURSIVE SEARCH TOOLS
|
| 557 |
# =====================================================================
|