Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix: deactivate Git watches on authentication failure (#5145)
zihenglia55afb001bca
diff --git a/openviking/parse/accessors/git_accessor.py b/openviking/parse/accessors/git_accessor.pyindex 999f9b5c0..8b049fe61 100644--- a/openviking/parse/accessors/git_accessor.py+++ b/openviking/parse/accessors/git_accessor.py@@ -32,6 +32,7 @@ from openviking.utils.code_hosting_utils import (from openviking.utils.git_auth import (build_git_http_auth_env,parse_git_http_auth_config,+ raise_git_auth_error,)from openviking_cli.utils.logger import get_logger@@ -311,16 +312,13 @@ class GitAccessor(DataAccessor):await asyncio.shield(proc.wait())raiseif proc.returncode != 0:+ raise_git_auth_error(stderr)error_msg = stderr.decode().strip()user_msg = "Git command failed."if "Could not resolve hostname" in error_msg:user_msg = ("Git command failed: could not resolve hostname. Check the URL or your network.")- elif "Permission denied" in error_msg or "publickey" in error_msg:- user_msg = (- "Git command failed: authentication error. Check your SSH keys or credentials."- )if env is None:logger.warning(f"[GitAccessor] {user_msg} Details: {error_msg}")else:diff --git a/openviking/resource/watch_scheduler.py b/openviking/resource/watch_scheduler.pyindex f9fcd4112..7c1d941c7 100644--- a/openviking/resource/watch_scheduler.py+++ b/openviking/resource/watch_scheduler.py@@ -28,6 +28,7 @@ from openviking.resource.watch_manager import WatchManager, WatchTaskfrom openviking.server.error_mapping import is_not_found_errorfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.resource_service import ResourceService+from openviking.utils.git_auth import GIT_AUTH_FAILEDfrom openviking_cli.exceptions import NotFoundErrorfrom openviking_cli.utils import get_logger@@ -318,19 +319,19 @@ class WatchScheduler:cancelled = Falseshould_deactivate = False- deactivation_reason = ""execution_task_id = Noneexecution_status = Noneexecution_error = None+ execution_code = Nonetry:auth_state = getattr(task, "auth_state", None)connector_watch = ConnectorDelegate.is_watch_auth_state(auth_state)if not connector_watch and not self._check_resource_exists(task.path):should_deactivate = True- deactivation_reason = f"Resource path does not exist: {task.path}"+ execution_error = f"Resource path does not exist: {task.path}"logger.warning(- f"[WatchScheduler] Task {task.task_id}: {deactivation_reason}. "+ f"[WatchScheduler] Task {task.task_id}: {execution_error}. ""Deactivating task.")else:@@ -358,9 +359,9 @@ class WatchScheduler:target_exists = await self._check_target_uri_exists(task.to_uri, ctx)if target_exists is False:should_deactivate = True- deactivation_reason = f"Watched target URI does not exist: {task.to_uri}"+ execution_error = f"Watched target URI does not exist: {task.to_uri}"logger.warning(- f"[WatchScheduler] Task {task.task_id}: {deactivation_reason}. "+ f"[WatchScheduler] Task {task.task_id}: {execution_error}. ""Deactivating task.")@@ -375,7 +376,7 @@ class WatchScheduler:except FeishuTokenRefreshError as e:if e.permanent:should_deactivate = True- deactivation_reason = str(e)+ execution_error = str(e)logger.error(f"[WatchScheduler] Task {task.task_id} permanent Feishu "f"token refresh failure: {e}. Deactivating task."@@ -422,6 +423,7 @@ class WatchScheduler:**processor_kwargs,)+ execution_code = result.get("code")execution_task_id = result.get("task_id")result_status = str(result.get("status") or "").lower()if execution_task_id and result_status not in {@@ -460,6 +462,9 @@ class WatchScheduler:else:result_status = ingestion_task.status.valueexecution_error = ingestion_task.error+ execution_code = (getattr(ingestion_task, "result", None) or {}).get(+ "code"+ )if result_status in {"failed", "error"}:execution_status = "failed"@@ -493,15 +498,15 @@ class WatchScheduler:raiseexcept FileNotFoundError as e:should_deactivate = True- deactivation_reason = f"Resource not found: {e}"+ execution_error = f"Resource not found: {e}"execution_status = "failed"- execution_error = deactivation_reasonlogger.error(f"[WatchScheduler] Task {task.task_id} resource not found: {e}. Deactivating task.")except Exception as e:execution_status = "failed"execution_error = str(e) or type(e).__name__+ execution_code = getattr(e, "code", None)logger.error(f"[WatchScheduler] Task {task.task_id} execution failed, "f"error_type={type(e).__name__}"@@ -510,9 +515,10 @@ class WatchScheduler:finally:try:if not cancelled:+ if execution_status == "failed" and execution_code == GIT_AUTH_FAILED:+ should_deactivate = Trueif should_deactivate:execution_status = "failed"- execution_error = deactivation_reasonawait asyncio.shield(self._watch_manager.record_execution(task.task_id,@@ -532,7 +538,7 @@ class WatchScheduler:))logger.info(- f"[WatchScheduler] Deactivated task {task.task_id}: {deactivation_reason}"+ f"[WatchScheduler] Deactivated task {task.task_id}: {execution_error}")else:logger.info(diff --git a/openviking/server/models.py b/openviking/server/models.pyindex 67c08cf53..ab3160150 100644--- a/openviking/server/models.py+++ b/openviking/server/models.py@@ -29,6 +29,7 @@ class Response(BaseModel):ERROR_CODE_TO_HTTP_STATUS = {"OK": 200,"INVALID_ARGUMENT": 400,+ "GIT_AUTH_FAILED": 400,"INVALID_URI": 400,"NOT_FOUND": 404,"ALREADY_EXISTS": 409,diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.pyindex c479d4f6c..14aeed694 100644--- a/openviking/service/resource_service.py+++ b/openviking/service/resource_service.py@@ -70,6 +70,7 @@ from openviking.utils.git_auth import (GitHttpAuthConfig,build_git_http_auth_env,parse_git_http_auth_config,+ raise_git_auth_error,reject_git_http_userinfo,)from openviking.utils.ingest_options import IngestOptions@@ -1385,6 +1386,7 @@ class ResourceService:raiseif proc.returncode != 0:+ raise_git_auth_error(stderr)raise InvalidArgumentError("Cannot access Git repository; git ls-remote failed.")repo_name = parse_code_hosting_url(source)return _ResourceSourceInfo(diff --git a/openviking/utils/git_auth.py b/openviking/utils/git_auth.pyindex 0d8403fc4..1932f210c 100644--- a/openviking/utils/git_auth.py+++ b/openviking/utils/git_auth.py@@ -10,7 +10,33 @@ from dataclasses import dataclass, fieldfrom typing import Mappingfrom urllib.parse import urlparse-from openviking_cli.exceptions import InvalidArgumentError+from openviking_cli.exceptions import InvalidArgumentError, OpenVikingError++GIT_AUTH_FAILED = "GIT_AUTH_FAILED"+++def raise_git_auth_error(stderr: bytes) -> None:+ """Preserve explicit Git authentication failures without exposing credentials."""+ error = stderr.decode(errors="replace").lower()+ if any(+ marker in error+ for marker in (+ "authentication failed",+ "authentication failure",+ "http basic: access denied",+ "invalid username or password",+ "invalid username or token",+ "permission denied (publickey",+ "permission denied (password",+ "the requested url returned error: 401",+ "could not read username",+ "could not read password",+ )+ ):+ raise OpenVikingError(+ "Git authentication failed. Check your SSH keys or credentials.",+ code=GIT_AUTH_FAILED,+ )@dataclass(frozen=True)diff --git a/tests/resource/test_git_watch_auth.py b/tests/resource/test_git_watch_auth.pyindex f584155be..12726d209 100644--- a/tests/resource/test_git_watch_auth.py+++ b/tests/resource/test_git_watch_auth.py@@ -57,3 +57,83 @@ def test_git_watch_auth_state_is_persisted_but_hidden_from_public_task_dict():assert restored.auth_state == stateassert "auth_state" not in task.to_dict()assert "git-secret" not in str(task.to_dict())++[email protected]("operation", ["preflight", "clone"])+ "stderr,auth_failure",+ [+ (b"fatal: Authentication failed for 'https://secret-token@example/repo'", True),+ (b"remote: HTTP Basic: Access denied", True),+ (b"git@example: Permission denied (publickey).", True),+ (b"fatal: could not read Username: terminal prompts disabled", True),+ (b"fatal: The requested URL returned error: 401", True),+ (b"fatal: Could not resolve host: example", False),+ (b"fatal: repository not found", False),+ (b"fatal: unable to access: Connection timed out", False),+ (b"fatal: cannot create directory: Permission denied", False),+ ],+)+async def test_git_commands_preserve_only_explicit_auth_failure(+ monkeypatch, operation, stderr, auth_failure+):+ from types import SimpleNamespace+ from unittest.mock import AsyncMock++ from openviking.parse.accessors.git_accessor import GitAccessor+ from openviking.service.resource_service import ResourceService+ from openviking.utils.git_auth import GIT_AUTH_FAILED++ process = SimpleNamespace(returncode=1, communicate=AsyncMock(return_value=(b"", stderr)))+ monkeypatch.setattr("asyncio.create_subprocess_exec", AsyncMock(return_value=process))+ with pytest.raises(Exception) as caught:+ if operation == "preflight":+ await ResourceService()._preflight_git_source("https://example/repo")+ else:+ await GitAccessor()._run_git(["git", "clone", "https://example/repo"])+ assert (getattr(caught.value, "code", None) == GIT_AUTH_FAILED) is auth_failure+ assert "secret-token" not in str(caught.value)++[email protected]("mode", ["exception", "result", "async_task"])[email protected]("auth_failure", [True, False])+async def test_git_watch_pauses_only_on_auth_failure(monkeypatch, mode, auth_failure):+ from types import SimpleNamespace+ from unittest.mock import AsyncMock++ from openviking.resource.watch_manager import WatchManager+ from openviking.resource.watch_scheduler import WatchScheduler+ from openviking.service.resource_service import ResourceService+ from openviking.utils.git_auth import GIT_AUTH_FAILED+ from openviking_cli.exceptions import OpenVikingError++ error = "Git authentication failed." if auth_failure else "Connection timed out"+ code = GIT_AUTH_FAILED if auth_failure else "UNKNOWN"+ service = ResourceService()+ service.refresh_resource = AsyncMock(+ return_value={"status": "failed", "error": error, "code": code}+ )+ if mode == "exception":+ service.refresh_resource.side_effect = OpenVikingError(error, code=code)+ elif mode == "async_task":+ service.refresh_resource.return_value = {"status": "queued", "task_id": "git-import"}+ tracker = AsyncMock()+ tracker.wait.return_value = SimpleNamespace(+ status=SimpleNamespace(value="failed"), error=error, result={"code": code}+ )+ monkeypatch.setattr("openviking.service.task_tracker.get_task_tracker", lambda: tracker)+ scheduler = WatchScheduler(resource_service=service)+ manager = WatchManager(viking_fs=None)+ await manager.initialize()+ scheduler._watch_manager = manager+ task = await manager.create_task(+ path="https://github.com/org/repo.git",+ to_uri="viking://resources/repo",+ watch_interval=30,+ )+ await scheduler._execute_task(task)+ updated = await manager.get_task(task.task_id)+ assert updated.last_status == "failed"+ assert updated.last_error == error+ assert updated.is_active is (not auth_failure)+ assert (updated.next_execution_time is None) is auth_failurediff --git a/tests/server/test_error_mapping.py b/tests/server/test_error_mapping.pyindex bb4f5ccd2..0bfa23407 100644--- a/tests/server/test_error_mapping.py+++ b/tests/server/test_error_mapping.py@@ -263,3 +263,19 @@ def test_git_concurrent_commit_maps_to_conflict():assert mapped is not Noneassert mapped.code == "CONFLICT"assert ERROR_CODE_TO_HTTP_STATUS.get(mapped.code) == 409+++def test_git_auth_failure_remains_a_client_error():+ from openviking.server.responses import error_response, response_from_result+ from openviking.utils.git_auth import GIT_AUTH_FAILED, raise_git_auth_error+ from openviking_cli.exceptions import OpenVikingError++ with pytest.raises(OpenVikingError) as caught:+ raise_git_auth_error(b"fatal: Authentication failed")+ error = map_exception(caught.value)+ assert error.code == GIT_AUTH_FAILED+ assert ERROR_CODE_TO_HTTP_STATUS.get(error.code, 500) == 400+ assert error_response(error.code, error.message).status_code == 400+ assert response_from_result(+ {"status": "error", "code": error.code, "errors": [error.message]}+ ).status_code == 400