ctxfw 3.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ctxfw/__init__.py ADDED
@@ -0,0 +1,42 @@
1
+ """
2
+ ctxfw — Sovereign Context Firewall & Token Optimization Engine (v3.4.0)
3
+ Hermetic Toolchain implementation adhering to Google style and POSIX/XDG standards.
4
+ """
5
+ from __future__ import annotations
6
+
7
+ from ctxfw.core.contracts import (
8
+ OptimizationRequestDTO,
9
+ OptimizationResultDTO,
10
+ PruningDepth,
11
+ SupportedLanguage,
12
+ )
13
+ from ctxfw.core.polyglot import TreeSitterContextPruner
14
+ from ctxfw.core.pruner import DeterministicContextPruner, _MethodBodyStripper
15
+ from ctxfw.core.topological import (
16
+ ContextFirewallEngine,
17
+ ProjectDependencyGraph,
18
+ StaticImportExtractor,
19
+ TopologicalContextBundleDTO,
20
+ )
21
+ from ctxfw.cli import handle_init_command
22
+ from ctxfw.storage.cache import LocalSemanticCache, get_canonical_cache_path
23
+
24
+ __version__ = "3.5.0"
25
+
26
+ __all__ = [
27
+ "__version__",
28
+ "PruningDepth",
29
+ "SupportedLanguage",
30
+ "OptimizationRequestDTO",
31
+ "OptimizationResultDTO",
32
+ "DeterministicContextPruner",
33
+ "_MethodBodyStripper",
34
+ "TreeSitterContextPruner",
35
+ "StaticImportExtractor",
36
+ "ProjectDependencyGraph",
37
+ "TopologicalContextBundleDTO",
38
+ "ContextFirewallEngine",
39
+ "LocalSemanticCache",
40
+ "get_canonical_cache_path",
41
+ "handle_init_command",
42
+ ]
ctxfw/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ """
2
+ src/ctxfw/__main__.py — Entrypoint for `python -m ctxfw`
3
+ """
4
+ from ctxfw.cli import main
5
+
6
+ if __name__ == "__main__":
7
+ main()
ctxfw/auditor.py ADDED
@@ -0,0 +1,165 @@
1
+ """
2
+ src/ctxfw/auditor.py — FinOps Ledger & Token Economics Auditor (v3.4.0)
3
+ Calculates token reduction metrics, USD cost avoidance, and cache bypass efficiency
4
+ in a 100% air-gapped, zero-cloud verification loop.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import argparse
9
+ import json
10
+ import sqlite3
11
+ import sys
12
+ from datetime import datetime, timezone
13
+ from pathlib import Path
14
+ from typing import Optional
15
+ from pydantic import BaseModel, ConfigDict, Field
16
+
17
+ from ctxfw.core.contracts import OptimizationResultDTO
18
+ from ctxfw.core.pruner import DeterministicContextPruner
19
+ from ctxfw.core.topological import TopologicalContextBundleDTO
20
+ from ctxfw.storage.cache import get_canonical_cache_path
21
+
22
+
23
+ class FinOpsReportDTO(BaseModel):
24
+ """Contrato inmutable de auditoría financiera y métricas de tokens."""
25
+ model_config = ConfigDict(frozen=True, extra="forbid")
26
+
27
+ total_tokens_evaluated: int = Field(..., ge=0, description="Volumen total de tokens evaluados en código original")
28
+ tokens_saved_cold: int = Field(..., ge=0, description="Tokens ahorrados mediante slicing sintáctico AST")
29
+ tokens_saved_cache_bypass: int = Field(..., ge=0, description="Tokens 100% eludidos mediante hits de caché cálida")
30
+ net_tokens_saved: int = Field(..., ge=0, description="Total neto de tokens ahorrados")
31
+ estimated_usd_savings: float = Field(..., ge=0.0, description="Ahorro financiero proyectado en USD")
32
+ cache_hit_ratio: float = Field(..., ge=0.0, le=100.0, description="Tasa porcentual de aciertos de caché")
33
+ air_gapped_certified: bool = Field(default=True, description="Certificación de ejecución 100% local sin red")
34
+ timestamp: str = Field(..., description="Marca temporal UTC de la auditoría en formato ISO 8601")
35
+
36
+
37
+ class FinOpsAuditor:
38
+ """Motor de cálculo financiero y auditoría de tokens."""
39
+
40
+ DEFAULT_PRICE_PER_MILLION: float = 3.0 # Base USD por 1M de tokens (tier estándar frontier LLM)
41
+
42
+ @classmethod
43
+ def audit_bundle(
44
+ cls,
45
+ bundle: TopologicalContextBundleDTO,
46
+ price_per_million_tokens: float = DEFAULT_PRICE_PER_MILLION,
47
+ ) -> FinOpsReportDTO:
48
+ """Calculates token savings and monetary return on an active context bundle."""
49
+ total_tokens = 0
50
+ saved_cold = 0
51
+ saved_bypass = 0
52
+ hits = 0
53
+ total_entries = len(bundle.entries)
54
+
55
+ for res in bundle.entries.values():
56
+ orig_t = DeterministicContextPruner.estimate_tokens(res.original_chars)
57
+ total_tokens += orig_t
58
+
59
+ if res.cache_hit:
60
+ hits += 1
61
+ # In warm cache hits, 100% of the token context is bypassed
62
+ saved_bypass += orig_t
63
+ else:
64
+ saved_cold += res.estimated_tokens_saved
65
+
66
+ net_saved = saved_cold + saved_bypass
67
+ hit_ratio = round((hits / max(1, total_entries)) * 100.0, 2)
68
+ usd_savings = round((net_saved / 1_000_000.0) * price_per_million_tokens, 6)
69
+
70
+ return FinOpsReportDTO(
71
+ total_tokens_evaluated=total_tokens,
72
+ tokens_saved_cold=saved_cold,
73
+ tokens_saved_cache_bypass=saved_bypass,
74
+ net_tokens_saved=net_saved,
75
+ estimated_usd_savings=usd_savings,
76
+ cache_hit_ratio=hit_ratio,
77
+ air_gapped_certified=True,
78
+ timestamp=datetime.now(timezone.utc).isoformat(),
79
+ )
80
+
81
+ @classmethod
82
+ def audit_cache(
83
+ cls,
84
+ db_path: Optional[str | Path] = None,
85
+ price_per_million_tokens: float = DEFAULT_PRICE_PER_MILLION,
86
+ ) -> FinOpsReportDTO:
87
+ """Queries persistent SQLite WAL tokens_cache and computes aggregate financial return."""
88
+ actual_path = Path(db_path) if db_path else get_canonical_cache_path()
89
+ if not actual_path.is_file():
90
+ return FinOpsReportDTO(
91
+ total_tokens_evaluated=0,
92
+ tokens_saved_cold=0,
93
+ tokens_saved_cache_bypass=0,
94
+ net_tokens_saved=0,
95
+ estimated_usd_savings=0.0,
96
+ cache_hit_ratio=0.0,
97
+ air_gapped_certified=True,
98
+ timestamp=datetime.now(timezone.utc).isoformat(),
99
+ )
100
+
101
+ with sqlite3.connect(str(actual_path), timeout=5.0) as conn:
102
+ cur = conn.cursor()
103
+ cur.execute("""
104
+ SELECT
105
+ COUNT(*),
106
+ COALESCE(SUM(original_chars), 0),
107
+ COALESCE(SUM(pruned_chars), 0),
108
+ COALESCE(SUM(estimated_tokens_saved), 0)
109
+ FROM tokens_cache;
110
+ """)
111
+ count, orig_chars, pruned_chars, saved_tokens = cur.fetchone()
112
+
113
+ total_tokens = DeterministicContextPruner.estimate_tokens(orig_chars)
114
+ net_saved = int(saved_tokens)
115
+ usd_savings = round((net_saved / 1_000_000.0) * price_per_million_tokens, 6)
116
+ hit_ratio = 100.0 if count > 0 else 0.0
117
+
118
+ return FinOpsReportDTO(
119
+ total_tokens_evaluated=total_tokens,
120
+ tokens_saved_cold=net_saved,
121
+ tokens_saved_cache_bypass=0,
122
+ net_tokens_saved=net_saved,
123
+ estimated_usd_savings=usd_savings,
124
+ cache_hit_ratio=hit_ratio,
125
+ air_gapped_certified=True,
126
+ timestamp=datetime.now(timezone.utc).isoformat(),
127
+ )
128
+
129
+ @classmethod
130
+ def export_audit(
131
+ cls,
132
+ report: FinOpsReportDTO,
133
+ output_path: str = "tests/finops_audit.json",
134
+ ) -> None:
135
+ """Exports the FinOps report to a machine-readable JSON artifact."""
136
+ out = Path(output_path)
137
+ out.parent.mkdir(parents=True, exist_ok=True)
138
+ with open(out, "w", encoding="utf-8") as f:
139
+ f.write(report.model_dump_json(indent=2))
140
+
141
+
142
+ def main():
143
+ parser = argparse.ArgumentParser(description="ctxfw-audit — Token Economics & FinOps Auditor (v3.4.0)")
144
+ parser.add_argument("--db", type=str, default=None, help="Path to SQLite cache database (defaults to canonical XDG path)")
145
+ parser.add_argument("--price", type=float, default=3.0, help="Base USD price per 1M tokens")
146
+ parser.add_argument("--json", action="store_true", help="Print report as JSON to stdout")
147
+ args = parser.parse_args()
148
+
149
+ report = FinOpsAuditor.audit_cache(db_path=args.db, price_per_million_tokens=args.price)
150
+ if args.json:
151
+ print(report.model_dump_json(indent=2))
152
+ else:
153
+ print("======================================================")
154
+ print(" CTXFW FINOPS LEDGER & AUDIT TELEMETRY ")
155
+ print("======================================================")
156
+ print(f"Total Tokens Evaluated: {report.total_tokens_evaluated:,}")
157
+ print(f"Net Tokens Saved: {report.net_tokens_saved:,}")
158
+ print(f"Estimated USD Savings: ${report.estimated_usd_savings:.6f}")
159
+ print(f"Air-gapped Certified: {report.air_gapped_certified}")
160
+ print(f"Timestamp: {report.timestamp}")
161
+ print("======================================================")
162
+
163
+
164
+ if __name__ == "__main__":
165
+ main()