-
Notifications
You must be signed in to change notification settings - Fork 0
feat(proposals): supersede non-winning chain links (feat_overnight_final_solution_phase3) #457
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
7 commits
Select commit
Hold shift + click to select a range
2788b83
feat(proposals): supersede non-winning chain links — schema + repo he…
SoundMindsAI d9498e3
feat(proposals): chain-rollup service + _stop wiring + filter widenin…
SoundMindsAI 7f29f1d
feat(proposals): reinstate endpoint + ?include_superseded flag (Story…
SoundMindsAI 2ead188
feat(proposals): superseded badge + reinstate button + filter chip (S…
SoundMindsAI 07907dc
docs(phase3): commit spec + plan + pipeline_status + preflighted idea…
SoundMindsAI 8222f74
test(contract): register POST /proposals/{id}/reinstate in OpenAPI su…
SoundMindsAI 3867408
perf(proposals): eliminate redundant DB queries (Gemini review)
SoundMindsAI 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
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
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,91 @@ | ||
| # SPDX-FileCopyrightText: 2026 soundminds.ai | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Chain-rollup service — supersede non-winning chain links' proposals. | ||
|
|
||
| feat_overnight_final_solution_phase3 Story 2.1 (FR-2). | ||
|
|
||
| Walks the chain anchored at a given study, identifies the winner via | ||
| :func:`select_best_link` (Phase 1 infra), and delegates the loser | ||
| supersession to :func:`repo.bulk_mark_superseded`. Returns the | ||
| ``(superseded_count, superseded_ids)`` tuple so the caller can emit the | ||
| ``chain_proposals_superseded`` structlog event AFTER its commit succeeds | ||
| (spec D-19). Does NOT commit; caller commits per the service-layer | ||
| convention. | ||
|
|
||
| The service is chain-scoped, not proposal-scoped — landing under | ||
| ``services/`` rather than appending to ``agent_proposals_dispatch.py`` | ||
| keeps it usable by both the autopilot path (``_stop`` in | ||
| ``backend/workers/orchestrator.py``) and any future chat-agent surface | ||
| (spec D-4 / Q5 locked). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from backend.app.db import repo | ||
| from backend.app.db.repo import ChainTraversalResult | ||
| from backend.app.domain.study.chain_summary import ( | ||
| derive_chain_stop_reason, | ||
| select_best_link, | ||
| ) | ||
|
|
||
|
|
||
| async def mark_non_winning_chain_proposals_superseded( | ||
| db: AsyncSession, | ||
| *, | ||
| study_id: str, | ||
| traversal: ChainTraversalResult | None = None, | ||
| ) -> tuple[int, list[str]]: | ||
| """Supersede ``pending`` proposals of all chain links other than the winner. | ||
|
|
||
| Walks the chain anchored at the study root of ``study_id`` and, when | ||
| the chain has terminated with at least 2 completed links and a clear | ||
| best link, conditional-UPDATEs all sibling losers' ``pending`` | ||
| proposals to ``superseded`` (via :func:`repo.bulk_mark_superseded`). | ||
|
|
||
| Returns ``(count, ids)`` — the count and IDs actually transitioned — | ||
| so the caller can emit the post-commit ``chain_proposals_superseded`` | ||
| structlog event with the full IDs payload per spec D-19. | ||
|
|
||
| ``traversal`` is an optional pre-fetched | ||
| :class:`~backend.app.db.repo.ChainTraversalResult`. When the caller | ||
| has already walked the chain (e.g. ``_stop`` reads it to capture the | ||
| anchor + winner for its log payload), passing it here avoids a | ||
| duplicate ``get_chain_for_study`` round-trip. When ``None`` the | ||
| function fetches it itself. | ||
|
|
||
| Idempotent. Re-running on the same chain after a successful first | ||
| call returns ``(0, [])`` (the losers are now ``superseded`` and the | ||
| repo helper's ``WHERE status='pending'`` clause excludes them). | ||
|
|
||
| Early-returns ``(0, [])`` when: | ||
| a. the chain is missing (study not found). | ||
| b. The chain has fewer than 2 links (single-link chain has no | ||
| siblings to supersede). | ||
| c. The derived ``stop_reason == "in_flight"`` (chain still | ||
| running; rollup deferred until termination). | ||
| d. :func:`select_best_link` returns ``None`` (no completed link → | ||
| no winner → nothing to supersede against). | ||
|
|
||
| Does NOT commit. Caller commits as part of its own transaction | ||
| boundary (e.g., ``_stop`` commits the link's ``pending`` proposal | ||
| insert and the rollup in the same transaction). | ||
| """ | ||
| if traversal is None: | ||
| traversal = await repo.get_chain_for_study(db, study_id) | ||
| if traversal is None: | ||
| return (0, []) | ||
| if len(traversal.links) < 2: | ||
| return (0, []) | ||
| stop_reason = derive_chain_stop_reason(traversal.links, traversal.anchor_trials) | ||
| if stop_reason == "in_flight": | ||
| return (0, []) | ||
| best_link_id = select_best_link(traversal.links) | ||
| if best_link_id is None: | ||
| return (0, []) | ||
| loser_ids = [link.id for link in traversal.links if link.id != best_link_id] | ||
| transitioned = await repo.bulk_mark_superseded(db, study_ids=loser_ids) | ||
| return (len(transitioned), transitioned) | ||
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.
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.
Import
Anyfromtypingto support the optionaltraversalparameter type annotation.