-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add in-session workflow nudges #150
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
+470
−1
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,161 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from primer.common.schemas import ( | ||
| CoachingBrief, | ||
| InSessionNudge, | ||
| InSessionNudgesResponse, | ||
| LiveSessionSignal, | ||
| LiveSessionSignalsResponse, | ||
| ) | ||
|
|
||
|
|
||
| def build_in_session_nudges( | ||
| live_signals: LiveSessionSignalsResponse, | ||
| coaching_brief: CoachingBrief | None = None, | ||
| *, | ||
| limit: int = 3, | ||
| ) -> InSessionNudgesResponse: | ||
| section_items = _section_items_by_title(coaching_brief) | ||
| nudges: list[InSessionNudge] = [] | ||
|
|
||
| for signal in live_signals.signals: | ||
| nudge = _nudge_for_signal(signal, live_signals, section_items) | ||
| if nudge is None: | ||
| continue | ||
| nudges.append(nudge) | ||
|
|
||
| if not nudges and live_signals.risk_level == "low": | ||
| nudges.append( | ||
| InSessionNudge( | ||
| nudge_type="stay_the_course", | ||
| severity="info", | ||
| title="Stay on the current path", | ||
| message=( | ||
| "No strong intervention signal is standing out right now. Keep the current " | ||
| "workflow tight and only widen scope if the next verification step fails." | ||
| ), | ||
| rationale="Live signals look healthy and there is no urgent corrective action.", | ||
| suggested_actions=section_items.get("How to start this session", [])[:1], | ||
| ) | ||
| ) | ||
|
|
||
| nudges.sort(key=_nudge_sort_key) | ||
| return InSessionNudgesResponse( | ||
| session_id=live_signals.session_id, | ||
| project_name=live_signals.project_name, | ||
| risk_level=live_signals.risk_level, | ||
| nudges=nudges[:limit], | ||
| ) | ||
|
|
||
|
|
||
| def _section_items_by_title(coaching_brief: CoachingBrief | None) -> dict[str, list[str]]: | ||
| if coaching_brief is None: | ||
| return {} | ||
| return {section.title: list(section.items) for section in coaching_brief.sections} | ||
|
|
||
|
|
||
| def _nudge_for_signal( | ||
| signal: LiveSessionSignal, | ||
| live_signals: LiveSessionSignalsResponse, | ||
| section_items: dict[str, list[str]], | ||
| ) -> InSessionNudge | None: | ||
| if signal.signal_type == "verification_failure": | ||
| return InSessionNudge( | ||
| nudge_type="tighten_verification_loop", | ||
| severity=signal.severity, | ||
| title="Tighten the verification loop", | ||
| message=( | ||
| "Recent verification steps are failing. Narrow the loop to the smallest proving " | ||
| "command before making broader edits." | ||
| ), | ||
| rationale=signal.detail, | ||
| suggested_actions=_merge_actions( | ||
| section_items.get("How to start this session", []), | ||
| section_items.get("What to reach for", []), | ||
| )[:2], | ||
| ) | ||
|
|
||
| if signal.signal_type == "tool_error_cluster": | ||
| return InSessionNudge( | ||
| nudge_type="fallback_from_tooling", | ||
| severity=signal.severity, | ||
| title="Switch away from the failing tool path", | ||
| message=( | ||
| "Multiple tool errors are stacking up. Move to the project fallback path or a " | ||
| "simpler local inspection step instead of retrying the same integration." | ||
| ), | ||
| rationale=signal.detail, | ||
| suggested_actions=_merge_actions( | ||
| section_items.get("What to watch for", []), | ||
| section_items.get("How to start this session", []), | ||
| )[:2], | ||
| ) | ||
|
|
||
| if signal.signal_type == "recovery_loop": | ||
| return InSessionNudge( | ||
| nudge_type="break_recovery_loop", | ||
| severity=signal.severity, | ||
| title="Break the recovery loop", | ||
| message=( | ||
| "The session looks stuck in repeated recovery attempts. Pause, return to the best " | ||
| "known playbook, and pick a single next proving action." | ||
| ), | ||
| rationale=signal.detail, | ||
| suggested_actions=_merge_actions( | ||
| section_items.get("How to start this session", []), | ||
| section_items.get("What to reach for", []), | ||
| )[:3], | ||
| ) | ||
|
|
||
| if signal.signal_type == "negative_sentiment": | ||
| return InSessionNudge( | ||
| nudge_type="reframe_task", | ||
| severity=signal.severity, | ||
| title="Reframe before the next step", | ||
| message=( | ||
| "Recent user messages look frustrated. Reset to the smallest next step that can " | ||
| "confirm or disprove the current hypothesis." | ||
| ), | ||
| rationale=signal.detail, | ||
| suggested_actions=_merge_actions( | ||
| section_items.get("How to start this session", []), | ||
| section_items.get("What to watch for", []), | ||
| )[:2], | ||
| ) | ||
|
|
||
| if signal.signal_type == "positive_momentum" and live_signals.risk_level == "low": | ||
| return InSessionNudge( | ||
| nudge_type="maintain_momentum", | ||
| severity="info", | ||
| title="Keep the loop tight", | ||
| message=( | ||
| "The session looks healthy. Stay with the current workflow and avoid adding extra " | ||
| "scope until the next verification step." | ||
| ), | ||
| rationale=signal.detail, | ||
| suggested_actions=section_items.get("How to start this session", [])[:1], | ||
| ) | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def _merge_actions(*groups: list[str]) -> list[str]: | ||
| merged: list[str] = [] | ||
| seen: set[str] = set() | ||
| for group in groups: | ||
| for item in group: | ||
| normalized = item.strip() | ||
| if not normalized or normalized in seen: | ||
| continue | ||
| seen.add(normalized) | ||
| merged.append(normalized) | ||
| return merged | ||
|
|
||
|
|
||
| def _nudge_sort_key(nudge: InSessionNudge) -> tuple[int, int, str]: | ||
| severity_score = {"critical": 2, "warning": 1, "info": 0} | ||
| return ( | ||
| -severity_score.get(nudge.severity, 0), | ||
| -len(nudge.suggested_actions), | ||
| nudge.title, | ||
| ) |
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
Oops, something went wrong.
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.