mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-16 15:54:10 +00:00
fix: keep session automations bound-only
This commit is contained in:
parent
369237f6a8
commit
29f1473940
@ -4,12 +4,16 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any, Protocol
|
from typing import Any, Protocol
|
||||||
|
|
||||||
from nanobot.cron.automation import is_bound_agent_job
|
|
||||||
from nanobot.cron.types import CronJob
|
from nanobot.cron.types import CronJob
|
||||||
|
|
||||||
|
|
||||||
class _CronServiceLike(Protocol):
|
class _CronServiceLike(Protocol):
|
||||||
def list_jobs(self, *, include_disabled: bool = False) -> list[CronJob]: ...
|
def list_bound_agent_jobs_for_session(
|
||||||
|
self,
|
||||||
|
session_key: str,
|
||||||
|
*,
|
||||||
|
include_disabled: bool = True,
|
||||||
|
) -> list[CronJob]: ...
|
||||||
|
|
||||||
|
|
||||||
def session_automation_jobs(
|
def session_automation_jobs(
|
||||||
@ -19,11 +23,10 @@ def session_automation_jobs(
|
|||||||
"""Return user automations attached to the WebUI session."""
|
"""Return user automations attached to the WebUI session."""
|
||||||
if cron_service is None:
|
if cron_service is None:
|
||||||
return []
|
return []
|
||||||
return [
|
return cron_service.list_bound_agent_jobs_for_session(
|
||||||
job
|
session_key,
|
||||||
for job in cron_service.list_jobs(include_disabled=True)
|
include_disabled=True,
|
||||||
if _matches_webui_session(job, session_key)
|
)
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def session_automations_payload(
|
def session_automations_payload(
|
||||||
@ -36,19 +39,6 @@ def session_automations_payload(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _matches_webui_session(job: CronJob, session_key: str) -> bool:
|
|
||||||
payload = job.payload
|
|
||||||
if payload.kind != "agent_turn":
|
|
||||||
return False
|
|
||||||
if is_bound_agent_job(job):
|
|
||||||
return payload.session_key == session_key
|
|
||||||
return bool(
|
|
||||||
payload.channel == "websocket"
|
|
||||||
and payload.to
|
|
||||||
and session_key == f"websocket:{payload.to}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_automation_jobs(jobs: list[CronJob]) -> list[dict[str, Any]]:
|
def serialize_automation_jobs(jobs: list[CronJob]) -> list[dict[str, Any]]:
|
||||||
return [_serialize_job(job) for job in jobs]
|
return [_serialize_job(job) for job in jobs]
|
||||||
|
|
||||||
|
|||||||
@ -189,7 +189,7 @@ async def test_session_automations_route_filters_by_webui_session(
|
|||||||
cron.add_job(
|
cron.add_job(
|
||||||
name="Legacy same target",
|
name="Legacy same target",
|
||||||
schedule=hourly,
|
schedule=hourly,
|
||||||
message="Legacy job should still show in WebUI",
|
message="Legacy job should not be treated as bound",
|
||||||
deliver=True,
|
deliver=True,
|
||||||
channel="websocket",
|
channel="websocket",
|
||||||
to="abc",
|
to="abc",
|
||||||
@ -227,15 +227,11 @@ async def test_session_automations_route_filters_by_webui_session(
|
|||||||
|
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
body = resp.json()
|
body = resp.json()
|
||||||
assert [job["name"] for job in body["jobs"]] == [
|
assert [job["name"] for job in body["jobs"]] == ["Morning check"]
|
||||||
"Morning check",
|
|
||||||
"Legacy same target",
|
|
||||||
]
|
|
||||||
job = body["jobs"][0]
|
job = body["jobs"][0]
|
||||||
assert job["schedule"]["kind"] == "every"
|
assert job["schedule"]["kind"] == "every"
|
||||||
assert job["schedule"]["every_ms"] == 3_600_000
|
assert job["schedule"]["every_ms"] == 3_600_000
|
||||||
assert job["payload"]["message"] == "Check the project status"
|
assert job["payload"]["message"] == "Check the project status"
|
||||||
assert body["jobs"][1]["payload"]["message"] == "Legacy job should still show in WebUI"
|
|
||||||
finally:
|
finally:
|
||||||
await channel.stop()
|
await channel.stop()
|
||||||
await server_task
|
await server_task
|
||||||
@ -747,7 +743,9 @@ async def test_session_delete_can_cascade_bound_automations(
|
|||||||
assert resp.json()["deleted"] is True
|
assert resp.json()["deleted"] is True
|
||||||
assert not path.exists()
|
assert not path.exists()
|
||||||
assert cron.list_bound_agent_jobs_for_session("websocket:doomed") == []
|
assert cron.list_bound_agent_jobs_for_session("websocket:doomed") == []
|
||||||
assert cron.list_jobs(include_disabled=True) == []
|
assert [job.name for job in cron.list_jobs(include_disabled=True)] == [
|
||||||
|
"Legacy same target"
|
||||||
|
]
|
||||||
finally:
|
finally:
|
||||||
await channel.stop()
|
await channel.stop()
|
||||||
await server_task
|
await server_task
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user