-
Notifications
You must be signed in to change notification settings - Fork 1
feat(noema): route LLM through contextual-orchestrator #1384
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
Draft
seonghobae
wants to merge
18
commits into
develop
Choose a base branch
from
cursor/noema-orchestrator-decision-agent-816f
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
17e36c3
feat(noema): route in-process decisions through contextual-orchestrator
cursoragent f59ab2d
test(noema): lock orchestrator-only LLM routing
cursoragent 15ec8c8
fix(noema): keep first-slice orchestrator routing only
cursoragent 20a95af
test(noema): prove gateway client failures fail closed
seonghobae 38f4e97
fix(noema): fail closed when orchestrator URL is rejected
cursoragent a8eec44
merge(develop): refresh Noema orchestrator slice
cursoragent 67e7231
Merge branch 'develop' into cursor/noema-orchestrator-decision-agent-…
seonghobae 5f374ff
test(noema): cover rejected gateway client paths
seonghobae 1fc2733
feat(noema): add signed gateway settings (#1425)
seonghobae 7f18343
fix(noema): classify encryption configuration errors
seonghobae 2660132
test(noema): cover wrapped encryption failures
seonghobae c8890a7
test(secrets): assert typed encryption configuration error
seonghobae 7c29c9e
chore: sync Noema gateway branch with develop
seonghobae 93dcfb9
fix(noema): handle encryption errors during config reads
seonghobae 414cb60
style(noema): remove trailing blank line
seonghobae c0fbc32
fix(noema): keep gateway DNS validation off event loop
seonghobae 4b4e7ac
fix: remove unreachable noema response
seonghobae 0fd3301
Merge remote-tracking branch 'origin/develop' into codex/pr1384-recon…
seonghobae 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """add noema contextual-orchestrator gateway columns | ||
|
|
||
| Revision ID: 0018_noema_orch_gateway | ||
| Revises: 0017_merge_newsdom_carddav_heads | ||
| Create Date: 2026-08-16 00:00:00.000000 | ||
|
|
||
| Noema judgments call only the contextual-orchestrator OpenAI-compatible | ||
| gateway. The dedicated inference token and HTTPS ``/v1`` base URL live on | ||
| ``tenant_configs`` in the Fernet KV (token is EncryptedString). naruon does | ||
| not store upstream provider keys for this path. | ||
| """ | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| revision = "0018_noema_orch_gateway" | ||
| down_revision = "0017_merge_newsdom_carddav_heads" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| _TENANT_TABLE = "tenant_configs" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| connection = op.get_bind() | ||
| inspector = sa.inspect(connection) | ||
| if not inspector.has_table(_TENANT_TABLE): | ||
| return | ||
| for column in _noema_gateway_columns(): | ||
| if not _has_column(inspector, _TENANT_TABLE, column.name): | ||
| op.add_column(_TENANT_TABLE, column) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| connection = op.get_bind() | ||
| inspector = sa.inspect(connection) | ||
| if not inspector.has_table(_TENANT_TABLE): | ||
| return | ||
| for column in reversed(_noema_gateway_columns()): | ||
| if _has_column(inspector, _TENANT_TABLE, column.name): | ||
| op.drop_column(_TENANT_TABLE, column.name) | ||
|
|
||
|
|
||
| def _noema_gateway_columns() -> list["sa.Column"]: | ||
| return [ | ||
| sa.Column("noema_orchestrator_base_url", sa.String(), nullable=True), | ||
| sa.Column("noema_orchestrator_token", sa.String(), nullable=True), | ||
| ] | ||
|
|
||
|
|
||
| def _has_column(inspector, table_name: str, column_name: str) -> bool: | ||
| return any( | ||
| column["name"] == column_name for column in inspector.get_columns(table_name) | ||
| ) |
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,192 @@ | ||
| """Signed-session settings for the per-user Noema gateway credential.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from pydantic import BaseModel, ConfigDict | ||
| from sqlalchemy.exc import StatementError | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from api.auth import AuthContext, get_auth_context | ||
| from core.runtime_secrets import EncryptionConfigurationError | ||
| from db.models import AuditLog, SecurityAuditEvent, TenantConfig | ||
| from db.session import get_db | ||
| from services.llm_provider_urls import validate_llm_provider_base_url_async | ||
| from services.orchestrator_gateway import validate_orchestrator_gateway_url | ||
| from services.tenant_config_scope import ( | ||
| get_scoped_tenant_config, | ||
| new_scoped_tenant_config, | ||
| ) | ||
|
|
||
| router = APIRouter(prefix="/api/noema-gateway", tags=["noema-gateway"]) | ||
|
|
||
|
|
||
| class NoemaGatewayUpdate(BaseModel): | ||
| """Optional values for the signed-session user's Noema gateway.""" | ||
|
|
||
| model_config = ConfigDict(extra="forbid") | ||
|
|
||
| base_url: str | None = None | ||
| token: str | None = None | ||
|
|
||
|
|
||
| class NoemaGatewayResponse(BaseModel): | ||
| """Safe gateway state that never returns the Fernet-protected token.""" | ||
|
|
||
| base_url: str | None = None | ||
| configured: bool = False | ||
| has_token: bool = False | ||
|
|
||
|
|
||
| def _resource_uid(auth_context: AuthContext) -> str: | ||
| """Return a stable, non-secret audit identifier for the scoped setting.""" | ||
| scope = f"{auth_context.organization_id or ''}:{auth_context.user_id}" | ||
| digest = hashlib.sha256(scope.encode("utf-8")).hexdigest()[:16] | ||
| return f"noema_gateway:{digest}" | ||
|
|
||
|
|
||
| async def _validated_base_url(value: str) -> str: | ||
| """Validate the HTTPS /v1 shape and the configured global-host policy.""" | ||
| try: | ||
| shaped_url = validate_orchestrator_gateway_url(value) | ||
| normalized_url = await validate_llm_provider_base_url_async(shaped_url) | ||
| if not normalized_url: | ||
| raise ValueError("gateway host is not allowlisted") | ||
| return validate_orchestrator_gateway_url(normalized_url) | ||
| except ValueError as exc: | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail="Noema gateway base URL is not allowed", | ||
| ) from exc | ||
|
|
||
|
|
||
| def _clean_token(value: str | None) -> str | None: | ||
| """Normalize a submitted token without recording or returning its value.""" | ||
| if value is None: | ||
| return None | ||
| token = value.strip() | ||
| if not token or token == "*" * 8: | ||
| return None | ||
| if any(ord(character) < 32 or ord(character) == 127 for character in token): | ||
| raise HTTPException(status_code=422, detail="Noema gateway token is invalid") | ||
| return token | ||
|
|
||
|
|
||
| def _response(config: TenantConfig | None) -> NoemaGatewayResponse: | ||
| """Build a response containing only non-secret gateway state.""" | ||
| if config is None: | ||
| return NoemaGatewayResponse() | ||
| has_token = bool(config.noema_orchestrator_token) | ||
| return NoemaGatewayResponse( | ||
| base_url=config.noema_orchestrator_base_url, | ||
| configured=bool(config.noema_orchestrator_base_url and has_token), | ||
| has_token=has_token, | ||
| ) | ||
|
|
||
|
|
||
| def _is_encryption_configuration_error(error: BaseException) -> bool: | ||
| """Recognize direct or SQLAlchemy-wrapped encryption configuration errors.""" | ||
| if isinstance(error, EncryptionConfigurationError): | ||
| return True | ||
| return isinstance(error, StatementError) and isinstance( | ||
| error.orig, EncryptionConfigurationError | ||
| ) | ||
|
|
||
|
|
||
| @router.get("", response_model=NoemaGatewayResponse) | ||
| async def get_noema_gateway( | ||
| db: AsyncSession = Depends(get_db), | ||
| auth_context: AuthContext = Depends(get_auth_context), | ||
| ) -> NoemaGatewayResponse: | ||
| """Return the signed-session user's scoped gateway readiness state.""" | ||
| try: | ||
| config = await get_scoped_tenant_config( | ||
| db, auth_context.user_id, auth_context.organization_id | ||
| ) | ||
| return _response(config) | ||
| except Exception as exc: | ||
| if not _is_encryption_configuration_error(exc): | ||
| raise | ||
| raise HTTPException( | ||
| status_code=503, | ||
| detail="Server encryption key is not configured. Contact your workspace administrator.", | ||
| ) from exc | ||
|
|
||
|
|
||
| @router.put("", response_model=NoemaGatewayResponse) | ||
| async def update_noema_gateway( | ||
| update: NoemaGatewayUpdate, | ||
| db: AsyncSession = Depends(get_db), | ||
| auth_context: AuthContext = Depends(get_auth_context), | ||
| ) -> NoemaGatewayResponse: | ||
| """Persist the current user's gateway settings with an auditable change.""" | ||
| updates = update.model_dump(exclude_unset=True) | ||
| if not updates: | ||
| raise HTTPException(status_code=422, detail="No gateway settings supplied") | ||
|
|
||
| try: | ||
| config = await get_scoped_tenant_config( | ||
| db, auth_context.user_id, auth_context.organization_id | ||
| ) | ||
| if config is None: | ||
| config = new_scoped_tenant_config( | ||
| user_id=auth_context.user_id, | ||
| organization_id=auth_context.organization_id, | ||
| ) | ||
| db.add(config) | ||
|
|
||
| if "token" in updates: | ||
| token = _clean_token(updates["token"]) | ||
| if token is not None: | ||
| config.noema_orchestrator_token = token | ||
| elif not config.noema_orchestrator_token: | ||
| raise HTTPException( | ||
| status_code=422, detail="Noema gateway token is required" | ||
| ) | ||
|
|
||
| if "base_url" in updates: | ||
| config.noema_orchestrator_base_url = await _validated_base_url( | ||
| updates["base_url"] or "" | ||
| ) | ||
|
|
||
| if not config.noema_orchestrator_base_url or not config.noema_orchestrator_token: | ||
| raise HTTPException( | ||
| status_code=422, | ||
| detail="Noema gateway base URL and token are required", | ||
| ) | ||
|
|
||
| resource_uid = _resource_uid(auth_context) | ||
| db.add( | ||
| AuditLog( | ||
| user_id=auth_context.user_id, | ||
| action="update", | ||
| resource_type="noema_gateway", | ||
| resource_id=resource_uid, | ||
| details="Updated Noema gateway settings", | ||
| ) | ||
| ) | ||
| db.add( | ||
| SecurityAuditEvent( | ||
| actor_user_id=auth_context.user_id, | ||
| actor_role=auth_context.role, | ||
| organization_id=auth_context.organization_id, | ||
| workspace_id=auth_context.workspace_id, | ||
| event_action="update", | ||
| resource_type="noema_gateway", | ||
| resource_uid=resource_uid, | ||
| evidence_source="api.noema_config", | ||
| detail_text="Updated Noema gateway settings", | ||
| ) | ||
| ) | ||
| await db.commit() | ||
| return _response(config) | ||
| except Exception as exc: | ||
| if not _is_encryption_configuration_error(exc): | ||
| raise | ||
| raise HTTPException( | ||
| status_code=503, | ||
| detail="Server encryption key is not configured. Contact your workspace administrator.", | ||
| ) from exc | ||
| return _response(config) | ||
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.