-
Notifications
You must be signed in to change notification settings - Fork 0
fix(p0): Stop hook transitions to COMPLETED on explicit signal (CL-3) #24
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """Tests for the Stop → COMPLETED transition (P0 #3 from CTO audit). | ||
|
|
||
| PRD §CL-3: 'Detect Stop events to transition a session to Completed.' | ||
| The handler accepts several explicit-completion signals from the | ||
| Stop hook payload; absent any signal, Stop only arms the idle-WAITING | ||
| timer (preserving the existing semantics). | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from bridge.hook_listener import HookListener | ||
| from bridge.session_state import SessionState, SessionStatus | ||
|
|
||
|
|
||
| def _state_with_active(sid: str = "s1") -> SessionState: | ||
| state = SessionState(kingdom_name="Camelot") | ||
| s = state.add_session("Royal Court") | ||
| s.session_id = sid | ||
| s.status = SessionStatus.ACTIVE | ||
| return state | ||
|
|
||
|
|
||
| def test_stop_with_final_flag_transitions_to_completed(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| changes = listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "final": True}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.COMPLETED | ||
| assert any(c.note == "stop_final" for c in changes) | ||
|
|
||
|
|
||
| def test_stop_with_session_ended_flag_transitions_to_completed(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "session_ended": True}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.COMPLETED | ||
|
|
||
|
|
||
| def test_stop_with_reason_session_complete_transitions_to_completed(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "reason": "session_complete"}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.COMPLETED | ||
|
|
||
|
|
||
| def test_stop_with_reason_completed_transitions_to_completed(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "reason": "completed"}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.COMPLETED | ||
|
|
||
|
|
||
| def test_stop_without_signal_only_arms_idle_timer(): | ||
| """Backward-compat: a vanilla Stop with no completion signal still | ||
| just arms the idle timer so tick_idle_check eventually flips | ||
| ACTIVE → WAITING. Status stays ACTIVE in the immediate response. | ||
| """ | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state, idle_threshold_sec=30) | ||
| changes = listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1"}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.ACTIVE | ||
| assert changes == [] | ||
| # Stop time recorded so the next tick_idle_check (after threshold) | ||
| # fires the WAITING transition. | ||
| fake_now = time.time() + 31 | ||
| later_changes = listener.tick_idle_check(now=fake_now) | ||
| assert state.sessions[0].status == SessionStatus.WAITING | ||
| assert any(c.note == "idle_threshold_crossed" for c in later_changes) | ||
|
|
||
|
|
||
| def test_stop_with_unrelated_reason_does_not_complete(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| changes = listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "reason": "interrupted"}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.ACTIVE | ||
| assert changes == [] | ||
|
|
||
|
|
||
| def test_stop_idempotent_when_already_completed(): | ||
| state = _state_with_active() | ||
| state.sessions[0].status = SessionStatus.COMPLETED | ||
| listener = HookListener(state=state) | ||
| changes = listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "s1", "final": True}, | ||
| }) | ||
| # Already COMPLETED — no spurious status change emitted. | ||
| assert state.sessions[0].status == SessionStatus.COMPLETED | ||
| assert changes == [] | ||
|
|
||
|
|
||
| def test_stop_for_unknown_session_is_noop(): | ||
| state = _state_with_active() | ||
| listener = HookListener(state=state) | ||
| changes = listener.feed({ | ||
| "event_type": "Stop", | ||
| "payload": {"session_id": "sess-other", "final": True}, | ||
| }) | ||
| assert state.sessions[0].status == SessionStatus.ACTIVE | ||
| assert changes == [] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation always records the stop time in
_last_stop_time, even when the session is transitioning toCOMPLETED. SinceCOMPLETEDis a terminal state andtick_idle_checkonly processesACTIVEsessions, these entries will persist indefinitely in the dictionary, leading to a memory leak in theHookListenerinstance over time.Additionally, the
Sessionmodel has acompleted_atfield (seebridge/session_state.py) that should ideally be populated when a session is completed to maintain data consistency.I suggest moving the idle timer arming logic into the
elseblock and ensuring the timer is cleared if the session is final.