forked from NousResearch/hermes-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
✨ feat(kanban): wake origin thread session on every transition #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Tests for the wake-origin-on-transition fix. | ||
|
|
||
| Two defects proven from a live E2E test (card t_278408f5, 2026-07-01): | ||
|
|
||
| 1. ``should_emit_transition`` / ``DEFAULT_EMIT_KINDS`` did not cover the | ||
| actionable lane-move kinds (``status_changed``, ``assigned``, ``unblocked``), | ||
| so a card moving to ``review`` fired no wake at all. | ||
|
|
||
| 2. The webhook route ignored the ``origin_*`` fields the emitter sends, so every | ||
| wake ran in a contextless ``webhook:<route>:<delivery>`` session instead of | ||
| the origin thread session it was born in. | ||
|
|
||
| These tests assert the intended behavior and are RED against the pre-fix code. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import gateway.kanban_transition_emit as kte | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Defect 1 — the wake gate must cover actionable lane-move kinds | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def _enabled_cfg(**over): | ||
| cfg = {"enabled": True} | ||
| cfg.update(over) | ||
| return cfg | ||
|
|
||
|
|
||
| def test_should_emit_covers_status_changed_by_default(): | ||
| # A card moving to review fires ``status_changed`` — it MUST wake the | ||
| # orchestrator so it can act on the handoff. Was silent pre-fix. | ||
| assert kte.should_emit_transition(_enabled_cfg(), "status_changed") is True | ||
|
|
||
|
|
||
| def test_should_emit_covers_assigned_by_default(): | ||
| assert kte.should_emit_transition(_enabled_cfg(), "assigned") is True | ||
|
|
||
|
|
||
| def test_should_emit_covers_unblocked_by_default(): | ||
| assert kte.should_emit_transition(_enabled_cfg(), "unblocked") is True | ||
|
|
||
|
|
||
| def test_should_emit_still_covers_blocked_and_loop_detected(): | ||
| # No regression to the two kinds that already worked. | ||
|
cwest marked this conversation as resolved.
|
||
| assert kte.should_emit_transition(_enabled_cfg(), "blocked") is True | ||
| assert kte.should_emit_transition( | ||
| _enabled_cfg(), "block_loop_detected" | ||
| ) is True | ||
|
|
||
|
|
||
| def test_should_emit_covers_terminal_failure_kinds_by_default(): | ||
| # A worker that gives up / crashes / times out is a terminal-failure | ||
| # escalation: the notifier's NOTIFY_KINDS pings chat for these (via | ||
| # TERMINAL_KINDS), but the wake must ALSO fire so the orchestrator can act | ||
| # on the failure path. These were silent pre-fix — a crashed worker pinged | ||
| # chat but woke no orchestrator, the same silent-escalation gap this fix | ||
| # closes, on the failure side. | ||
| for kind in ("gave_up", "crashed", "timed_out"): | ||
| assert kte.should_emit_transition(_enabled_cfg(), kind) is True, kind | ||
|
|
||
|
|
||
| def test_should_emit_respects_explicit_config_override(): | ||
| # An explicit emit_kinds list still wins over the default set. | ||
| cfg = _enabled_cfg(emit_kinds=["blocked"]) | ||
| assert kte.should_emit_transition(cfg, "blocked") is True | ||
| assert kte.should_emit_transition(cfg, "status_changed") is False | ||
|
|
||
|
|
||
| def test_should_emit_disabled_when_not_enabled(): | ||
| assert kte.should_emit_transition({"enabled": False}, "status_changed") is False | ||
| assert kte.should_emit_transition(None, "status_changed") is False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| """Tests: the kanban-transition wake routes to the ORIGIN thread session. | ||
|
|
||
| Defect 2 (proven live, card t_278408f5): the webhook route ignored the | ||
| ``origin_*`` fields the emitter sends, so every transition wake ran in a | ||
| contextless ``webhook:<route>:<delivery>`` session and never reached the origin | ||
| Discord thread. These assert the route now honors origin routing, with a clean | ||
| fallback when origin fields are absent (no regression to non-origin webhooks). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| from aiohttp.test_utils import TestClient, TestServer | ||
| from aiohttp import web | ||
|
|
||
| from gateway.platforms.webhook import WebhookAdapter, _INSECURE_NO_AUTH | ||
| from gateway.config import PlatformConfig | ||
|
|
||
|
|
||
| def _make_adapter(routes): | ||
| extra = {"host": "0.0.0.0", "port": 0, "routes": routes, | ||
| "rate_limit": 100, "max_body_bytes": 1_048_576} | ||
| return WebhookAdapter(PlatformConfig(enabled=True, extra=extra)) | ||
|
|
||
|
|
||
| def _app(adapter): | ||
| app = web.Application() | ||
| app.router.add_post("/webhooks/{route_name}", adapter._handle_webhook) | ||
| return app | ||
|
|
||
|
|
||
| def _kt_route(): | ||
| return {"kanban-transition": {"secret": _INSECURE_NO_AUTH, "prompt": "{title}"}} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_wake_targets_origin_thread_when_origin_fields_present(): | ||
| """A POST carrying origin_* fields builds a source targeting the origin | ||
| thread/session — NOT the contextless webhook: session.""" | ||
| adapter = _make_adapter(_kt_route()) | ||
| captured = {} | ||
|
|
||
| async def _capture(event): | ||
| captured["event"] = event | ||
|
|
||
| adapter.handle_message = AsyncMock(side_effect=_capture) | ||
|
|
||
| async with TestClient(TestServer(_app(adapter))) as cli: | ||
| resp = await cli.post( | ||
| "/webhooks/kanban-transition", | ||
| json={ | ||
| "event_type": "status_changed", | ||
| "task_id": "t_abc", | ||
| "title": "probe card", | ||
| "origin_session_id": "20260629_125345_f5fe11ba", | ||
| "origin_platform": "discord", | ||
| "origin_chat_id": "1520255822704152666", | ||
| "origin_thread_id": "1520255822704152666", | ||
| }, | ||
| ) | ||
| assert resp.status == 202 | ||
|
|
||
| adapter.handle_message.assert_awaited_once() | ||
| src = captured["event"].source | ||
| # The wake must be routed to the ORIGIN, not a webhook: synthetic session. | ||
| assert src.platform.value == "discord" | ||
| assert str(src.chat_id) == "1520255822704152666" | ||
| assert str(getattr(src, "thread_id", "")) == "1520255822704152666" | ||
| assert not str(src.chat_id).startswith("webhook:") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_wake_falls_back_to_webhook_session_without_origin_fields(): | ||
| """No origin fields → current behavior: a webhook:<route>:<delivery> | ||
| session. Backward-compatible; non-origin webhooks are unaffected.""" | ||
| adapter = _make_adapter(_kt_route()) | ||
| captured = {} | ||
|
|
||
| async def _capture(event): | ||
| captured["event"] = event | ||
|
|
||
| adapter.handle_message = AsyncMock(side_effect=_capture) | ||
|
|
||
| async with TestClient(TestServer(_app(adapter))) as cli: | ||
| resp = await cli.post( | ||
| "/webhooks/kanban-transition", | ||
| json={"event_type": "status_changed", "task_id": "t_abc", | ||
| "title": "no-origin card"}, | ||
| ) | ||
| assert resp.status == 202 | ||
|
|
||
| adapter.handle_message.assert_awaited_once() | ||
| src = captured["event"].source | ||
| assert str(src.chat_id).startswith("webhook:kanban-transition:") | ||
|
|
||
|
|
||
| def test_origin_source_yields_the_live_thread_session_key(): | ||
| """The origin source MUST produce the exact session key the live Discord | ||
| thread inbound produces — otherwise the wake lands in a phantom session | ||
| (the F2 key-mismatch trap). Locks the key-mirror invariant.""" | ||
| from gateway.session import build_session_key | ||
| adapter = _make_adapter(_kt_route()) | ||
| src = adapter._build_origin_source( | ||
| "discord", "1520255822704152666", "1520255822704152666" | ||
| ) | ||
| assert src is not None | ||
| key = build_session_key( | ||
| src, group_sessions_per_user=True, thread_sessions_per_user=False | ||
| ) | ||
| assert key == "agent:main:discord:thread:1520255822704152666:1520255822704152666" | ||
|
|
||
|
|
||
| def test_origin_source_none_when_fields_absent(): | ||
| adapter = _make_adapter(_kt_route()) | ||
| assert adapter._build_origin_source(None, None, None) is None | ||
| assert adapter._build_origin_source("discord", None, None) is None | ||
|
|
||
|
|
||
| def test_origin_source_unknown_platform_falls_back(): | ||
| adapter = _make_adapter(_kt_route()) | ||
| assert adapter._build_origin_source("nope", "123", "123") is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.