File size: 9,704 Bytes
f8ee2b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
POptimizer — Speed / RAM / Quality / Proprietary Objective Engine

Implements the optimization formula:

  J(x) = λ₁·L(x) + λ₂·M(x) + λ₃·C(x) + λ₄·B(x) + λ₅·D(x) + λ₆·R(x) + λ₇·I(x) - λ₈·Q(x) - λ₉·V(x)

Subject to hard constraints:
  secrets_exposed = 0
  private_code_uploaded = 0
  license_conflict = 0
  tests_required = pass
  destructive_action => explicit_approval

Priority order:
  security > correctness > verification > RAM > speed > quality > convenience
"""

import hashlib
import json
import time
from dataclasses import dataclass, field, asdict
from datetime import datetime
from typing import Optional

DEFAULT_LAMBDAS = {
    'latency': 1.0,
    'memory': 1.2,
    'cpu': 0.8,
    'bundle_size': 0.5,
    'dependency_risk': 1.5,
    'runtime_risk': 1.3,
    'ip_leakage': 10.0,
    'code_quality': 0.9,
    'verification_confidence': 0.7,
}


@dataclass
class OptimizationInput:
    startup_time_ms: float = 0.0
    p95_latency_ms: float = 0.0
    p99_latency_ms: float = 0.0
    blocking_io_time_ms: float = 0.0
    cold_path_penalty: float = 0.0

    peak_rss_mb: float = 0.0
    heap_used_mb: float = 0.0
    allocation_rate_mbps: float = 0.0
    cache_unboundedness: float = 0.0
    leak_probability: float = 0.0

    cpu_usage_pct: float = 0.0

    bundle_size_kb: float = 0.0

    dependency_license_risk: float = 0.0
    dependency_maintenance_risk: float = 0.0
    dependency_supply_chain_risk: float = 0.0

    runtime_failure_probability: float = 0.0

    secret_exposure: int = 0
    private_code_upload: int = 0
    license_conflict: int = 0
    public_disclosure_unapproved: int = 0
    ownership_notice_removed: int = 0
    unapproved_third_party_dependency: int = 0

    readability: float = 50.0
    maintainability: float = 50.0
    testability: float = 50.0
    type_safety: float = 50.0
    locality_of_change: float = 50.0
    style_consistency: float = 50.0
    complexity: float = 50.0
    cleverness: float = 50.0
    surface_area: float = 50.0

    tests_passed: bool = False
    build_passed: bool = False
    lint_passed: bool = False
    benchmark_available: bool = False
    manual_inspection: bool = False
    receipt_created: bool = False

    lambdas: dict = field(default_factory=lambda: dict(DEFAULT_LAMBDAS))


@dataclass
class OptimizationResult:
    valid: bool
    j_score: float
    latency_cost: float
    memory_cost: float
    cpu_cost: float
    bundle_cost: float
    dependency_cost: float
    runtime_risk: float
    ip_risk: float
    quality_score: float
    verification_confidence: float
    constraint_violations: list = field(default_factory=list)
    receipt: dict = field(default_factory=dict)
    timestamp: str = ''

    def to_dict(self) -> dict:
        return asdict(self)


def compute_latency(x: OptimizationInput) -> float:
    return (
        0.3 * x.startup_time_ms
        + 0.3 * x.p95_latency_ms
        + 0.2 * x.p99_latency_ms
        + 0.15 * x.blocking_io_time_ms
        + 0.05 * x.cold_path_penalty
    )


def compute_memory(x: OptimizationInput) -> float:
    if x.cache_unboundedness > 0.5:
        return 1e9
    if x.leak_probability > 0.5:
        return 1e9
    return (
        0.3 * x.peak_rss_mb
        + 0.3 * x.heap_used_mb
        + 0.2 * x.allocation_rate_mbps
        + 0.15 * x.cache_unboundedness * 1000
        + 0.05 * x.leak_probability * 1000
    )


def compute_cpu(x: OptimizationInput) -> float:
    return x.cpu_usage_pct


def compute_bundle(x: OptimizationInput) -> float:
    return x.bundle_size_kb


def compute_dependency(x: OptimizationInput) -> float:
    return (
        0.3 * x.dependency_license_risk * 100
        + 0.3 * x.dependency_maintenance_risk * 100
        + 0.2 * x.bundle_size_kb * 0.01
        + 0.1 * x.dependency_supply_chain_risk * 100
    )


def compute_runtime_risk(x: OptimizationInput) -> float:
    return x.runtime_failure_probability * 100


def compute_ip_risk(x: OptimizationInput) -> float:
    total = (
        x.secret_exposure
        + x.private_code_upload
        + x.license_conflict
        + x.public_disclosure_unapproved
        + x.ownership_notice_removed
        + x.unapproved_third_party_dependency
    )
    if total > 0:
        return 1e9
    return 0.0


def compute_quality(x: OptimizationInput) -> float:
    positive = (
        0.15 * x.readability
        + 0.15 * x.maintainability
        + 0.15 * x.testability
        + 0.10 * x.type_safety
        + 0.10 * x.locality_of_change
        + 0.10 * x.style_consistency
    )
    negative = (
        0.10 * x.complexity
        + 0.10 * x.cleverness
        + 0.05 * x.surface_area
    )
    return positive - negative


def compute_verification(x: OptimizationInput) -> float:
    score = 0.0
    if x.tests_passed:
        score += 25
    if x.build_passed:
        score += 20
    if x.lint_passed:
        score += 10
    if x.benchmark_available:
        score += 15
    if x.manual_inspection:
        score += 15
    if x.receipt_created:
        score += 15
    return score


def check_constraints(x: OptimizationInput) -> list:
    violations = []
    if x.secret_exposure > 0:
        violations.append('secret_exposure > 0')
    if x.private_code_upload > 0:
        violations.append('private_code_uploaded > 0')
    if x.license_conflict > 0:
        violations.append('license_conflict > 0')
    if x.public_disclosure_unapproved > 0:
        violations.append('public_disclosure_unapproved > 0')
    if x.ownership_notice_removed > 0:
        violations.append('ownership_notice_removed > 0')
    if x.unapproved_third_party_dependency > 0:
        violations.append('unapproved_third_party_dependency > 0')
    return violations


def optimize(x: OptimizationInput) -> OptimizationResult:
    lam = x.lambdas

    violations = check_constraints(x)
    valid = len(violations) == 0

    L = compute_latency(x)
    M = compute_memory(x)
    C = compute_cpu(x)
    B = compute_bundle(x)
    D = compute_dependency(x)
    R = compute_runtime_risk(x)
    I = compute_ip_risk(x)
    Q = compute_quality(x)
    V = compute_verification(x)

    j = (
        lam['latency'] * L
        + lam['memory'] * M
        + lam['cpu'] * C
        + lam['bundle_size'] * B
        + lam['dependency_risk'] * D
        + lam['runtime_risk'] * R
        + lam['ip_leakage'] * I
        - lam['code_quality'] * Q
        - lam['verification_confidence'] * V
    )

    receipt = {
        'timestamp': datetime.now().isoformat(),
        'j_score': round(j, 4),
        'valid': valid,
        'constraint_violations': violations,
        'L_latency': round(L, 4),
        'M_memory': round(M, 4),
        'C_cpu': round(C, 4),
        'B_bundle': round(B, 4),
        'D_dependency': round(D, 4),
        'R_runtime_risk': round(R, 4),
        'I_ip_risk': I,
        'Q_quality': round(Q, 4),
        'V_verification': round(V, 4),
        'lambdas': lam,
        'hard_rule': 'No receipt → no production claim.',
    }

    return OptimizationResult(
        valid=valid,
        j_score=round(j, 4),
        latency_cost=round(L, 4),
        memory_cost=round(M, 4),
        cpu_cost=round(C, 4),
        bundle_cost=round(B, 4),
        dependency_cost=round(D, 4),
        runtime_risk=round(R, 4),
        ip_risk=I,
        quality_score=round(Q, 4),
        verification_confidence=round(V, 4),
        constraint_violations=violations,
        receipt=receipt,
        timestamp=datetime.now().isoformat(),
    )


def compare(x_old: OptimizationInput, x_new: OptimizationInput) -> dict:
    r_old = optimize(x_old)
    r_new = optimize(x_new)

    if not r_new.valid:
        return {
            'decision': 'REJECT',
            'reason': 'constraint_violations',
            'violations': r_new.constraint_violations,
            'j_old': r_old.j_score,
            'j_new': r_new.j_score,
        }

    improved = r_new.j_score < r_old.j_score
    return {
        'decision': 'ACCEPT' if improved else 'REJECT',
        'reason': 'J(x_new) < J(x_old)' if improved else 'J(x_new) >= J(x_old)',
        'j_old': r_old.j_score,
        'j_new': r_new.j_score,
        'delta_j': round(r_new.j_score - r_old.j_score, 4),
        'delta_speed': round(r_old.latency_cost - r_new.latency_cost, 4),
        'delta_ram': round(r_old.memory_cost - r_new.memory_cost, 4),
        'delta_quality': round(r_new.quality_score - r_old.quality_score, 4),
        'ip_risk': r_new.ip_risk,
        'verification': r_new.verification_confidence,
        'receipt': r_new.receipt,
    }


def endpoint_receipt(
    endpoint: str,
    classification: str,
    availability: bool,
    latency_ms: float,
    schema_valid: bool,
    auth_flow: str,
    security_observations: list,
    build_evidence: bool,
    runtime_evidence: bool,
    verification_confidence: float,
) -> dict:
    return {
        'endpoint': endpoint,
        'classification': classification,
        'availability': availability,
        'latency_ms': latency_ms,
        'schema_valid': schema_valid,
        'authentication': auth_flow,
        'security_observations': security_observations,
        'build_evidence': build_evidence,
        'runtime_evidence': runtime_evidence,
        'verification_confidence': verification_confidence,
        'receipt_hash': hashlib.sha256(
            json.dumps({
                'endpoint': endpoint,
                'classification': classification,
                'timestamp': datetime.now().isoformat(),
            }, sort_keys=True).encode()
        ).hexdigest()[:16],
        'next_hardening_action': 'Add rate limiting and input validation' if not security_observations else security_observations[0],
    }