fix(agent): persist signal incidents with tenant context
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / build-and-deploy (push) Has been cancelled
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / tests (push) Has been cancelled

This commit is contained in:
ogt
2026-07-10 21:09:31 +08:00
parent c004615220
commit 8aa8967c2b
5 changed files with 83 additions and 6 deletions

View File

@@ -1,9 +1,16 @@
from __future__ import annotations
from contextlib import asynccontextmanager
from types import SimpleNamespace
import pytest
from src.models.incident import Severity
from src.services.incident_memory import _derive_incident_alert_metadata
from src.services import incident_memory as incident_memory_module
from src.services.incident_memory import (
IncidentDbAdapter,
_derive_incident_alert_metadata,
)
def test_signal_worker_incident_metadata_uses_signal_alert_name() -> None:
@@ -30,3 +37,31 @@ def test_signal_worker_incident_metadata_uses_signal_alert_name() -> None:
assert metadata["description"] == "29 ERROR log entries in last 5min"
assert metadata["actor"] == "journal"
@pytest.mark.asyncio
async def test_incident_db_adapter_uses_explicit_project_context(monkeypatch) -> None:
project_ids: list[str | None] = []
class FakeResult:
def scalar_one_or_none(self):
return None
class FakeDb:
async def execute(self, _statement):
return FakeResult()
@asynccontextmanager
async def fake_get_db_context(project_id=None):
project_ids.append(project_id)
yield FakeDb()
monkeypatch.setattr(
incident_memory_module,
"get_db_context",
fake_get_db_context,
)
result = await IncidentDbAdapter(project_id="awoooi").load("INC-NOT-FOUND")
assert result is None
assert project_ids == ["awoooi"]

View File

@@ -2,7 +2,10 @@ from __future__ import annotations
from types import SimpleNamespace
import pytest
from src.models.incident import Severity
from src.services import signal_observation_service as signal_observation_module
from src.services.signal_observation_service import (
build_signal_worker_provider_event_id,
enrich_incident_for_signal_observation,
@@ -72,3 +75,26 @@ def test_signal_worker_event_content_is_operator_readable() -> None:
assert "Incident: INC-20260518-TEST01" in content
assert "Alert: HostErrorLogFlood" in content
assert "Telegram: not sent" in content
@pytest.mark.asyncio
async def test_signal_observation_timeline_uses_explicit_project_context(monkeypatch) -> None:
calls: list[dict] = []
class FakeTimelineService:
async def add_event(self, **kwargs):
calls.append(kwargs)
monkeypatch.setattr(
"src.services.approval_db.get_timeline_service",
lambda: FakeTimelineService(),
)
await signal_observation_module._add_observation_timeline(
incident=_host_incident(),
signal_data={"alert_name": "HostErrorLogFlood"},
labels={"target": "ollama"},
raw_evidence_created=True,
)
assert calls[0]["project_id"] == "awoooi"