-
Notifications
You must be signed in to change notification settings - Fork 0
foundation: create the modular-monolith API and worker skeleton (#6) #33
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| checks: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: astral-sh/setup-uv@v6 | ||
| with: | ||
| version: "0.8.22" | ||
| enable-cache: true | ||
| - run: uv python install 3.13 | ||
| - run: make install | ||
| - run: make check |
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,21 @@ | ||
| .PHONY: install build lint typecheck test smoke check | ||
|
|
||
| install: | ||
| uv sync --frozen | ||
|
|
||
| build: | ||
| uv build | ||
|
|
||
| lint: | ||
| uv run ruff check . | ||
|
|
||
| typecheck: | ||
| uv run mypy | ||
|
|
||
| test: | ||
| uv run pytest -q tests/unit | ||
|
|
||
| smoke: | ||
| uv run pytest -q tests/process | ||
|
|
||
| check: build lint typecheck test smoke |
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 @@ | ||
| """Infrastructure adapters around the shared ContextEngine domain.""" |
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 @@ | ||
| """HTTP ingress adapter.""" |
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,32 @@ | ||
| """Minimal FastAPI composition root.""" | ||
|
|
||
| from typing import Final | ||
|
|
||
| from fastapi import FastAPI | ||
|
|
||
| from engine import BUILD_IDENTIFIER | ||
| from engine.runtime import Runtime | ||
| from engine.runtime.construction import required_kernel_dependencies | ||
|
|
||
| HEALTH_RESPONSE: Final = { | ||
| "status": "ready", | ||
| "service": "context-engine-api", | ||
| "version": BUILD_IDENTIFIER, | ||
| "runtime_delivery": "NOT_ACTIVE", | ||
| } | ||
|
|
||
|
|
||
| def create_app() -> FastAPI: | ||
| """Construct the API and fail before serving if kernel wiring is incomplete.""" | ||
|
|
||
| Runtime(required_kernel_dependencies()) | ||
| app = FastAPI(title="ContextEngine", version=BUILD_IDENTIFIER) | ||
|
|
||
| @app.get("/health", include_in_schema=False) | ||
| def health() -> dict[str, str]: | ||
| return HEALTH_RESPONSE.copy() | ||
|
|
||
| return app | ||
|
|
||
|
|
||
| app = create_app() |
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 @@ | ||
| """Process composition roots around the shared domain.""" |
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,24 @@ | ||
| """Engine API process entry point.""" | ||
|
|
||
| import argparse | ||
| from collections.abc import Sequence | ||
|
|
||
| import uvicorn | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> None: | ||
| parser = argparse.ArgumentParser(description="ContextEngine API") | ||
| parser.add_argument("--host", default="127.0.0.1") | ||
| parser.add_argument("--port", default=8000, type=int) | ||
| parser.add_argument("--log-level", default="info") | ||
| args = parser.parse_args(argv) | ||
| uvicorn.run( | ||
| "adapters.http.app:app", | ||
| host=args.host, | ||
| port=args.port, | ||
| log_level=args.log_level, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,45 @@ | ||
| """Independent Supply worker process entry point.""" | ||
|
|
||
| import argparse | ||
| import json | ||
| import threading | ||
| from collections.abc import Sequence | ||
|
|
||
| from engine import BUILD_IDENTIFIER | ||
| from engine.runtime import Runtime | ||
| from engine.runtime.construction import required_kernel_dependencies | ||
|
|
||
|
|
||
| def run(*, test_mode: bool) -> int: | ||
| Runtime(required_kernel_dependencies()) | ||
| lifecycle = "test-complete" if test_mode else "ready" | ||
| print( | ||
| json.dumps( | ||
| { | ||
| "status": lifecycle, | ||
| "service": "context-engine-worker", | ||
| "version": BUILD_IDENTIFIER, | ||
| "job_behavior": "NOT_ACTIVE", | ||
| }, | ||
| sort_keys=True, | ||
| ), | ||
| flush=True, | ||
| ) | ||
| if not test_mode: | ||
| threading.Event().wait() | ||
| return 0 | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description="ContextEngine Supply worker") | ||
| parser.add_argument( | ||
| "--test-mode", | ||
| action="store_true", | ||
| help="complete the deterministic no-op lifecycle and exit", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
| return run(test_mode=args.test_mode) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
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,5 @@ | ||
| """Shared ContextEngine domain package.""" | ||
|
|
||
| from engine.build import BUILD_IDENTIFIER | ||
|
|
||
| __all__ = ["BUILD_IDENTIFIER"] |
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,8 @@ | ||
| """Build identity shared by every ContextEngine process.""" | ||
|
|
||
| from importlib.metadata import PackageNotFoundError, version | ||
|
|
||
| try: | ||
| BUILD_IDENTIFIER = version("context-engine") | ||
| except PackageNotFoundError: | ||
| BUILD_IDENTIFIER = "0.1.0+uninstalled" |
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,15 @@ | ||
| """Runtime construction boundary; context delivery is not active yet.""" | ||
|
|
||
| from engine.runtime.construction import ( | ||
| KernelDependencies, | ||
| KernelDependency, | ||
| Runtime, | ||
| RuntimeConfigurationError, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "KernelDependencies", | ||
| "KernelDependency", | ||
| "Runtime", | ||
| "RuntimeConfigurationError", | ||
| ] |
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,63 @@ | ||
| """Fail-closed construction for the future sealed Runtime. | ||
|
|
||
| This module proves mandatory dependency wiring only. It deliberately exposes no | ||
| ``resolve`` method or authorization behavior before the owning M0 issues land. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import StrEnum | ||
|
|
||
|
|
||
| class KernelDependency(StrEnum): | ||
| """Closed set of security-kernel dependency identities.""" | ||
|
|
||
| POLICY = "policy" | ||
| AUDIT = "audit" | ||
| BUDGET = "budget" | ||
| PROVENANCE = "provenance" | ||
|
|
||
|
|
||
| class RuntimeConfigurationError(RuntimeError): | ||
| """Raised when the sealed Runtime composition is incomplete or invalid.""" | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class KernelDependencies: | ||
| """Explicit mandatory inputs to Runtime construction.""" | ||
|
|
||
| policy: KernelDependency | ||
| audit: KernelDependency | ||
| budget: KernelDependency | ||
| provenance: KernelDependency | ||
|
|
||
| class Runtime: | ||
| """Construction seam for a sealed Runtime whose delivery API is not active.""" | ||
|
|
||
| def __init__(self, dependencies: KernelDependencies) -> None: | ||
| if type(dependencies) is not KernelDependencies: | ||
| raise RuntimeConfigurationError( | ||
| "runtime dependencies must be KernelDependencies" | ||
| ) | ||
| expected = ( | ||
| ("policy", KernelDependency.POLICY), | ||
| ("audit", KernelDependency.AUDIT), | ||
| ("budget", KernelDependency.BUDGET), | ||
| ("provenance", KernelDependency.PROVENANCE), | ||
| ) | ||
| for field_name, expected_dependency in expected: | ||
| if getattr(dependencies, field_name) is not expected_dependency: | ||
| raise RuntimeConfigurationError( | ||
| f"mandatory kernel dependency is missing or invalid: {field_name}" | ||
| ) | ||
| self._dependencies = dependencies | ||
|
|
||
|
|
||
| def required_kernel_dependencies() -> KernelDependencies: | ||
| """Return the only allowed skeleton composition; no disable flag exists.""" | ||
|
|
||
| return KernelDependencies( | ||
| policy=KernelDependency.POLICY, | ||
| audit=KernelDependency.AUDIT, | ||
| budget=KernelDependency.BUDGET, | ||
| provenance=KernelDependency.PROVENANCE, | ||
| ) |
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,44 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "context-engine" | ||
| version = "0.1.0" | ||
| description = "Permission-aware context delivery engine" | ||
| readme = "README.md" | ||
| requires-python = ">=3.13,<3.14" | ||
| dependencies = [ | ||
| "fastapi>=0.116,<0.117", | ||
| "uvicorn>=0.35,<0.36", | ||
| ] | ||
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "httpx>=0.28,<0.29", | ||
| "mypy>=1.17,<1.18", | ||
| "pytest>=8.4,<8.5", | ||
| "ruff>=0.12,<0.13", | ||
| ] | ||
|
|
||
| [project.scripts] | ||
| context-engine-api = "applications.api:main" | ||
| context-engine-worker = "applications.worker:main" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["engine", "adapters", "applications"] | ||
|
|
||
| [tool.mypy] | ||
| python_version = "3.13" | ||
| strict = true | ||
| files = ["engine", "adapters", "applications", "tests"] | ||
|
|
||
| [tool.pytest.ini_options] | ||
| testpaths = ["tests"] | ||
|
|
||
| [tool.ruff] | ||
| target-version = "py313" | ||
| line-length = 88 | ||
|
|
||
| [tool.ruff.lint] | ||
| select = ["E", "F", "I", "UP", "B", "SIM"] |
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.
When the worker is launched normally as
context-engine-workerwithout--test-mode, this unconditional return lets the process printreadyand exit immediately. That contradicts the separate--test-modelifecycle and leaves deployments or smoke scripts with no running Supply worker process; normal mode should block/run its service loop even while job behavior isNOT_ACTIVE.Useful? React with 👍 / 👎.
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.
will-fix — fixed in e740392. Normal mode now prints readiness with flush and blocks at applications/worker.py:28-29; --test-mode alone exits deterministically. Regression coverage at tests/process/test_processes.py:81-101 proves the installed worker remains alive until terminated.