Repository navigation
Add approval API action routes #73
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from fastapi import HTTPException | ||
| import pytest | ||
|
|
||
| from sovereign_cortex.api import RejectApprovalRequest, create_app | ||
| from sovereign_cortex.approvals import ApprovalStore | ||
| from sovereign_cortex.events import PatchOperation, ProposedPatch | ||
| from sovereign_cortex.policy import PolicyDecision | ||
|
|
||
|
|
||
| def _endpoint(app, path: str): | ||
| for route in app.routes: | ||
| if route.path == path: | ||
| return route.endpoint | ||
| raise AssertionError(f"Route not found: {path}") | ||
|
|
||
|
|
||
| def _write_workspace(tmp_path): | ||
| vault = tmp_path / "vault" / "demo-project" / "tasks" | ||
| vault.mkdir(parents=True) | ||
| (tmp_path / "workspaces.yaml").write_text( | ||
| "workspaces:\n demo-project:\n vault_root: vault/demo-project\n", | ||
| encoding="utf-8", | ||
| ) | ||
| task = vault / "launch.md" | ||
| task.write_text( | ||
| "---\nid: launch\ntitle: Launch\nstatus: planned\n---\n# Launch\n", | ||
| encoding="utf-8", | ||
| ) | ||
| return task | ||
|
|
||
|
|
||
| def _create_request(tmp_path, *, before: str, after: str): | ||
| return ApprovalStore(tmp_path).create( | ||
| workspace="demo-project", | ||
| command_text="Update launch task", | ||
| correlation_id="api-action-test", | ||
| patch=ProposedPatch( | ||
| operation=PatchOperation.UPDATE_FILE, | ||
| relative_path="tasks/launch.md", | ||
| before=before, | ||
| after=after, | ||
| summary="Update launch task", | ||
| risk="medium", | ||
| requires_human_approval=True, | ||
| ), | ||
| policy_decision=PolicyDecision( | ||
| allowed=True, | ||
| approval_required=True, | ||
| reason="Medium-risk patch requires approval.", | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def test_approval_action_routes_are_registered(): | ||
| app = create_app() | ||
| paths = {route.path for route in app.routes} | ||
|
|
||
| assert "/approvals/{approval_id}/approve" in paths | ||
| assert "/approvals/{approval_id}/reject" in paths | ||
|
|
||
|
|
||
| def test_approval_approve_route_applies_patch_and_moves_request(tmp_path): | ||
| task = _write_workspace(tmp_path) | ||
| before = task.read_text(encoding="utf-8") | ||
| after = before.replace("status: planned", "status: active") | ||
| request = _create_request(tmp_path, before=before, after=after) | ||
| approve = _endpoint(create_app(tmp_path), "/approvals/{approval_id}/approve") | ||
|
|
||
| response = approve(request.approval_id) | ||
|
|
||
| assert response.status == "approved" | ||
| assert response.commit_hash | ||
| assert task.read_text(encoding="utf-8") == after | ||
| assert (tmp_path / "approval_requests" / "approved" / f"{request.approval_id}.json").exists() | ||
| assert not (tmp_path / "approval_requests" / "pending" / f"{request.approval_id}.json").exists() | ||
|
|
||
|
|
||
| def test_approval_approve_route_returns_409_for_conflict(tmp_path): | ||
| task = _write_workspace(tmp_path) | ||
| request = _create_request(tmp_path, before="older content", after="new content") | ||
| approve = _endpoint(create_app(tmp_path), "/approvals/{approval_id}/approve") | ||
|
|
||
| with pytest.raises(HTTPException) as error: | ||
| approve(request.approval_id) | ||
|
|
||
| assert error.value.status_code == 409 | ||
| assert task.exists() | ||
| assert (tmp_path / "approval_requests" / "pending" / f"{request.approval_id}.json").exists() | ||
|
|
||
|
|
||
| def test_approval_reject_route_moves_request_without_writing(tmp_path): | ||
| task = _write_workspace(tmp_path) | ||
| before = task.read_text(encoding="utf-8") | ||
| request = _create_request(tmp_path, before=before, after=before + "\nextra\n") | ||
| reject = _endpoint(create_app(tmp_path), "/approvals/{approval_id}/reject") | ||
|
|
||
| response = reject(request.approval_id, RejectApprovalRequest(reason="Not now.")) | ||
|
|
||
| assert response.status == "rejected" | ||
| assert response.rejection_reason == "Not now." | ||
| assert task.read_text(encoding="utf-8") == before | ||
| assert (tmp_path / "approval_requests" / "rejected" / f"{request.approval_id}.json").exists() | ||
| assert not (tmp_path / "approval_requests" / "pending" / f"{request.approval_id}.json").exists() |
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.
When an approval is resolved through either new HTTP route, the store is mutated and the function returns without appending a
PATCH_APPROVEDorPATCH_REJECTEDevent toActivityRecordStore. The equivalent CLI paths explicitly append these events insrc/sovereign_cortex/cli.py, so API-driven decisions leave the activity JSONL lifecycle incomplete and cannot be correlated or replayed with the original approval request.Useful? React with 👍 / 👎.