Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ Organization documentation rules are checked by `python scripts/check_style.py`,

Prefer focused import paths over the `quant_platform_kit.common` facade in new
code: `quant_platform_kit.execution`, `quant_platform_kit.reconciliation`, or
`quant_platform_kit.common.<module>`. Guard with
`PYTHONPATH=src python scripts/check_common_facade_imports.py`.
`quant_platform_kit.common.<module>`. Do not add bulk re-exports to
`common/__init__.py`. Platform thin-main layout lives in
`execution_kernel/platform_http.py`. Guard with
`PYTHONPATH=src python scripts/check_common_facade_imports.py` (and
`tests/test_common_facade_imports.py`).

## Naming and documentation

Expand Down
2 changes: 1 addition & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ QuantStrategyLab is a multi-broker, multi-market quantitative trading system. It
| Notifications | `notifications/` | Multi-channel notification pipeline: Email, SMS, Push, Telegram, Webhook. Shared envelope (`RenderedNotification`, `NotificationPublisher`) for consistent delivery |
| Strategy Lifecycle | `strategy_lifecycle/` | Backtest orchestration, drift detection, performance monitoring, AI review, rollback management, health scoring, return collection, audit logging |
| Runtime Contracts | `common/` | Strategy contracts, runtime assembly, runtime reports, plugin artifact parsing (legacy wide re-exports; prefer subpackages below) |
| Execution contracts | `execution/`, `execution_kernel/` | Commands, receipts, outcomes, paper admission, runtime command gate (`execution/`); pure no-I/O submit guards (`execution_kernel/`) |
| Execution contracts | `execution/`, `execution_kernel/` | Commands, receipts, fills, guarded ports, paper admission, runtime command gate (`execution/`); pure no-I/O submit guards (`execution_kernel/`); platform thin-main + explicit re-export template in `execution_kernel/platform_http.py` |
| Reconciliation | `reconciliation/` | Broker reconciliation evidence, enrollment, recovery workflows, live continuity |
| Strategy Plugins | `common/strategy_plugins.py` | Plugin signal parsing, compatibility checks, alert generation |
| Portfolio Ports | `common/ports.py` | Port interfaces for market data, portfolio snapshots, execution, notifications |
Expand Down
63 changes: 59 additions & 4 deletions scripts/check_common_facade_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
1. common/__init__.py stays within a line budget (blocks unbounded facade growth).
2. Topic subpackages ``execution`` and ``reconciliation`` import cleanly and
expose every name listed in their ``__all__``.
3. Optionally (--strict) fail on wide ``from quant_platform_kit.common import``
3. ``execution_kernel.platform_http`` documents the Wave2 thin-main template
(module map + explicit re-export rules).
4. Optionally (--strict) fail on wide ``from quant_platform_kit.common import``
in src/ outside ``common/`` itself (legacy code may still use the facade).

Usage:
Expand All @@ -21,13 +23,37 @@
import sys
from pathlib import Path

# Baseline recorded during refactor/untangle-p0-p2; shrink over time as the facade slims.
COMMON_INIT_LINE_BUDGET = 650
# Baseline after #782; keep closed so new APIs land in subpackages, not here.
COMMON_INIT_LINE_BUDGET = 620

FORBIDDEN_IMPORT_PREFIX = "from quant_platform_kit.common import"
SKIP_DIRS = {".git", "__pycache__", ".venv", "venv", "build", "dist"}

SUBPACKAGES = ("quant_platform_kit.execution", "quant_platform_kit.reconciliation")
SUBPACKAGES = (
"quant_platform_kit.execution",
"quant_platform_kit.reconciliation",
"quant_platform_kit.execution_kernel",
)

PLATFORM_HTTP_MODULE_KEYS = frozenset(
{
"main",
"http_routes",
"runtime_wiring",
"notifications_runtime",
"strategy_plugins_runtime",
"platform_entry",
}
)
PLATFORM_THIN_MAIN_RULE_KEYS = frozenset(
{
"explicit_reexports",
"all_list",
"no_star_api",
"entry_bind",
"behavior",
}
)


def repo_root_from_script() -> Path:
Expand Down Expand Up @@ -64,6 +90,34 @@ def check_subpackage_exports(package_name: str) -> list[str]:
return errors


def check_platform_http_template() -> list[str]:
errors: list[str] = []
try:
from quant_platform_kit.execution_kernel.platform_http import (
describe_platform_http_modules,
describe_platform_thin_main_rules,
)
except Exception as exc: # noqa: BLE001
return [f"failed to import platform_http template: {exc}"]

modules = describe_platform_http_modules()
missing_modules = PLATFORM_HTTP_MODULE_KEYS - frozenset(modules)
if missing_modules:
errors.append(
"platform_http.describe_platform_http_modules missing keys: "
+ ", ".join(sorted(missing_modules))
)

rules = describe_platform_thin_main_rules()
missing_rules = PLATFORM_THIN_MAIN_RULE_KEYS - frozenset(rules)
if missing_rules:
errors.append(
"platform_http.describe_platform_thin_main_rules missing keys: "
+ ", ".join(sorted(missing_rules))
)
return errors


def iter_python_files(root: Path) -> list[Path]:
src = root / "src"
files: list[Path] = []
Expand Down Expand Up @@ -121,6 +175,7 @@ def main() -> int:
errors.extend(check_common_line_budget(root))
for package in SUBPACKAGES:
errors.extend(check_subpackage_exports(package))
errors.extend(check_platform_http_template())

wide_import_hits = check_forbidden_wide_imports(root)
if wide_import_hits:
Expand Down
3 changes: 2 additions & 1 deletion src/quant_platform_kit/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Prefer ``quant_platform_kit.common.<module>`` or the subpackages above over
``from quant_platform_kit.common import ...`` in new in-repo code. Add new
domain APIs via a dedicated subpackage re-export layer, not new bulk imports
here.
here. ``scripts/check_common_facade_imports.py`` enforces the line budget and
subpackage export integrity; use ``--strict`` when hunting wide facade imports.
"""

from .us_equity_sessions import (
Expand Down
68 changes: 68 additions & 0 deletions src/quant_platform_kit/execution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
Import from this package in new code instead of the ``common`` facade.

For pure submission guards (no I/O), see ``quant_platform_kit.execution_kernel``.
For platform HTTP thin-main layout, see
``quant_platform_kit.execution_kernel.platform_http``.
"""

from quant_platform_kit.common.account_identity import (
ACCOUNT_IDENTITY_RECEIPT_SCHEMA_VERSION,
AccountIdentityGuardedExecutionPort,
evaluate_account_identity,
)
from quant_platform_kit.common.capital_base import (
CAPITAL_BASE_CONTRACT_VERSION,
CapitalBaseBinding,
Expand All @@ -17,6 +24,20 @@
build_capital_base_snapshot,
validate_capital_base,
)
from quant_platform_kit.common.execution_capabilities import (
FRACTIONAL_SHARE_EXECUTION_CAPABILITY,
FRACTIONAL_SHARE_EXECUTION_SKIP_REASON,
NOTIONAL_TO_WHOLE_SHARE_COMPAT_SKIP_REASON,
definition_requires_fractional_share_execution,
fractional_share_execution_unsupported_reason,
notional_buy_compat_mode_enabled,
platform_supports_fractional_share_execution,
)
from quant_platform_kit.common.execution_fills import (
EXECUTION_FILL_SCHEMA_VERSION,
ExecutionFillAccumulation,
accumulate_execution_fills,
)
from quant_platform_kit.common.execution_commands import (
DEFAULT_EXECUTION_COMMAND_NAMESPACE,
EXECUTION_COMMAND_EVENT_SCHEMA_VERSION,
Expand Down Expand Up @@ -77,6 +98,12 @@
LongOnlyReduceOnlyValidation,
validate_long_only_reduce_only_order,
)
from quant_platform_kit.common.order_status import (
available_sell_quantity,
compute_confirmed_sell_release_value,
is_confirmed_fill_status,
normalize_order_status_payload,
)
from quant_platform_kit.common.paper_execution_admission import (
PAPER_RISK_ADMISSION_RECEIPT_INTENT_FIELD,
PAPER_RISK_ADMISSION_RECEIPT_SCHEMA_VERSION,
Expand All @@ -98,13 +125,20 @@
build_paper_execution_command_binding,
consume_due_paper_execution_commands,
)
from quant_platform_kit.common.pending_order_evidence import (
validate_pending_order_evidence,
)
from quant_platform_kit.common.reduce_only_command_admission import (
REDUCE_ONLY_ORDER_DIGEST_FIELD,
ReduceOnlyCommandAdmission,
ReduceOnlyCommandFinding,
build_reduce_only_order_digest,
evaluate_reduce_only_command_admission,
)
from quant_platform_kit.common.risk_guarded_execution_port import (
RiskGuardedExecutionPort,
RiskGuardedExecutionRejected,
)
from quant_platform_kit.common.runtime_command_gate import (
RUNTIME_COMMAND_GATE_RECEIPT_SCHEMA_VERSION,
RuntimeCommandAction,
Expand All @@ -117,8 +151,19 @@
evaluate_runtime_command_gate,
normalize_runtime_command_integrity_findings,
)
from quant_platform_kit.common.runtime_target import (
RuntimeExecutionEnvironment,
resolve_runtime_execution_environment,
)
from quant_platform_kit.common.strategy_release import (
RUNTIME_LOADED_RECEIPT_SCHEMA_VERSION,
build_runtime_loaded_receipt,
validate_runtime_loaded_receipt,
)

__all__ = [
"ACCOUNT_IDENTITY_RECEIPT_SCHEMA_VERSION",
"AccountIdentityGuardedExecutionPort",
"CAPITAL_BASE_CONTRACT_VERSION",
"CapitalBaseBinding",
"CapitalBaseFinding",
Expand All @@ -136,6 +181,7 @@
"EXECUTION_COMMAND_EVENT_SCHEMA_VERSION",
"EXECUTION_COMMAND_SCHEMA_VERSION",
"EXECUTION_COMMAND_STRATEGY_RELEASE_FIELD",
"EXECUTION_FILL_SCHEMA_VERSION",
"EXECUTION_LEDGER_DIGEST_SCHEMA_VERSION",
"EXECUTION_RECEIPT_BROKER_CONFIRMATIONS",
"EXECUTION_RECEIPT_OUTCOMES",
Expand All @@ -144,9 +190,13 @@
"ExecutionCommandEvent",
"ExecutionCommandState",
"ExecutionCommandStore",
"ExecutionFillAccumulation",
"ExecutionMarkerStore",
"FRACTIONAL_SHARE_EXECUTION_CAPABILITY",
"FRACTIONAL_SHARE_EXECUTION_SKIP_REASON",
"LongOnlyReduceOnlyFinding",
"LongOnlyReduceOnlyValidation",
"NOTIONAL_TO_WHOLE_SHARE_COMPAT_SKIP_REASON",
"PAPER_EXECUTION_COMMAND_CONSUMER_SCHEMA_VERSION",
"PAPER_RISK_ADMISSION_RECEIPT_INTENT_FIELD",
"PAPER_RISK_ADMISSION_RECEIPT_SCHEMA_VERSION",
Expand All @@ -160,15 +210,19 @@
"PaperRiskAdmissionReceipt",
"REDUCE_ONLY_ORDER_DIGEST_FIELD",
"RUNTIME_COMMAND_GATE_RECEIPT_SCHEMA_VERSION",
"RUNTIME_LOADED_RECEIPT_SCHEMA_VERSION",
"ReduceOnlyCommandAdmission",
"ReduceOnlyCommandFinding",
"RiskGuardedExecutionPort",
"RiskGuardedExecutionRejected",
"RuntimeCommandAction",
"RuntimeCommandExposureEffect",
"RuntimeCommandGateDecision",
"RuntimeCommandGateEnforcement",
"RuntimeCommandGateMode",
"RuntimeCommandGatePolicy",
"RuntimeCommandIntegrityFinding",
"RuntimeExecutionEnvironment",
"STAGE_COMPLETED",
"STAGE_DRY_RUN_COMPLETED",
"STAGE_EXECUTION_BLOCKED",
Expand All @@ -179,35 +233,49 @@
"STAGE_RECONCILED",
"STAGE_SUBMITTED",
"TERMINAL_EXECUTION_COMMAND_STATES",
"accumulate_execution_fills",
"attach_execution_receipt",
"attach_runtime_execution_receipt",
"available_sell_quantity",
"build_capital_base_snapshot",
"build_execution_command_store_from_env",
"build_execution_receipt",
"build_paper_execution_command_binding",
"build_paper_risk_admission_receipt",
"build_reduce_only_order_digest",
"build_runtime_loaded_receipt",
"calculate_execution_receipt_sha256",
"calculate_paper_risk_admission_receipt_sha256",
"canonical_execution_receipt_json",
"canonical_paper_risk_admission_receipt_json",
"compute_confirmed_sell_release_value",
"consume_due_paper_execution_commands",
"definition_requires_fractional_share_execution",
"evaluate_account_identity",
"evaluate_paper_execution_admission",
"evaluate_reduce_only_command_admission",
"evaluate_runtime_command_gate",
"filter_execution_blocking_skips",
"fractional_share_execution_unsupported_reason",
"is_confirmed_fill_status",
"is_funding_block",
"is_retryable_strategy_run_stage",
"is_terminal_funding_block",
"is_terminal_strategy_run_stage",
"normalize_order_status_payload",
"normalize_runtime_command_integrity_findings",
"normalize_skip_reason",
"normalize_stage",
"notional_buy_compat_mode_enabled",
"platform_supports_fractional_share_execution",
"resolve_execution_receipt_fact",
"resolve_runtime_execution_environment",
"resolve_strategy_run_stage",
"validate_capital_base",
"validate_execution_command_release_binding",
"validate_execution_command_transition",
"validate_execution_receipt",
"validate_long_only_reduce_only_order",
"validate_pending_order_evidence",
"validate_runtime_loaded_receipt",
]
7 changes: 6 additions & 1 deletion src/quant_platform_kit/execution_kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
deny_live_submit_without_durable_identity,
deny_new_cycle_when_submission_unknown,
)
from .platform_http import PlatformHttpSurface, describe_platform_http_modules
from .platform_http import (
PlatformHttpSurface,
describe_platform_http_modules,
describe_platform_thin_main_rules,
)
from .types import (
ExecutionGuardDecision,
ExecutionMode,
Expand All @@ -32,4 +36,5 @@
"deny_live_submit_without_durable_identity",
"deny_new_cycle_when_submission_unknown",
"describe_platform_http_modules",
"describe_platform_thin_main_rules",
]
Loading
Loading