algo-cli-runtime 0.14.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.
- algo_cli/__init__.py +3 -0
- algo_cli/__main__.py +7 -0
- algo_cli/_internal/__init__.py +12 -0
- algo_cli/_internal/policy_chain.py +259 -0
- algo_cli/action_registry.py +1047 -0
- algo_cli/agent_blocks.py +550 -0
- algo_cli/agent_pipeline.py +1457 -0
- algo_cli/agent_threads.py +308 -0
- algo_cli/animations.py +316 -0
- algo_cli/cache_admission.py +209 -0
- algo_cli/capability_mask.py +66 -0
- algo_cli/chat_protocol.py +116 -0
- algo_cli/chatgpt_auth.py +510 -0
- algo_cli/chatgpt_client.py +657 -0
- algo_cli/code_rag.py +479 -0
- algo_cli/config.py +651 -0
- algo_cli/context_budget.py +679 -0
- algo_cli/credential_helpers.py +315 -0
- algo_cli/deliberation.py +29 -0
- algo_cli/display.py +1470 -0
- algo_cli/evals/__init__.py +21 -0
- algo_cli/evals/algorithm_effectiveness.py +560 -0
- algo_cli/evals/competitive_harness_rating.py +702 -0
- algo_cli/evals/cot_quality.py +220 -0
- algo_cli/evals/harness_retrieval_benchmark.py +401 -0
- algo_cli/evals/performance_regression.py +136 -0
- algo_cli/evals/scorecard_grading.py +308 -0
- algo_cli/evals/session_distribution.py +84 -0
- algo_cli/execution_guardrails.py +806 -0
- algo_cli/extensions_manifest.py +84 -0
- algo_cli/git_evidence.py +227 -0
- algo_cli/google_workspace.py +407 -0
- algo_cli/google_workspace_auth.py +523 -0
- algo_cli/harness.py +2587 -0
- algo_cli/identity.py +557 -0
- algo_cli/index_compute_lab.py +228 -0
- algo_cli/inference_harness.py +70 -0
- algo_cli/intelligence/__init__.py +1103 -0
- algo_cli/intelligence/acrobat_config.py +307 -0
- algo_cli/intelligence/acrobat_manifests.py +338 -0
- algo_cli/intelligence/acrobat_models.py +195 -0
- algo_cli/intelligence/acrobat_pipeline.py +295 -0
- algo_cli/intelligence/acrobat_runtime.py +302 -0
- algo_cli/intelligence/acrobat_security.py +261 -0
- algo_cli/intelligence/acrobat_workflows.py +226 -0
- algo_cli/intelligence/actionability.py +165 -0
- algo_cli/intelligence/adversarial_audit.py +136 -0
- algo_cli/intelligence/agent_arena.py +92 -0
- algo_cli/intelligence/agent_benchmark.py +236 -0
- algo_cli/intelligence/agent_runtime.py +171 -0
- algo_cli/intelligence/agents_as_tools.py +70 -0
- algo_cli/intelligence/artifact_binding.py +80 -0
- algo_cli/intelligence/autonomous_engineer.py +1976 -0
- algo_cli/intelligence/backpressure.py +99 -0
- algo_cli/intelligence/bloom_filter.py +186 -0
- algo_cli/intelligence/bonferroni.py +66 -0
- algo_cli/intelligence/boundary_compaction.py +98 -0
- algo_cli/intelligence/catalog_verifier.py +172 -0
- algo_cli/intelligence/cavecrew.py +118 -0
- algo_cli/intelligence/changelog.py +176 -0
- algo_cli/intelligence/checkpoint_resume.py +92 -0
- algo_cli/intelligence/circuit_breaker.py +88 -0
- algo_cli/intelligence/clarification_gate.py +101 -0
- algo_cli/intelligence/code_graph.py +180 -0
- algo_cli/intelligence/coderank.py +97 -0
- algo_cli/intelligence/consistent_hash.py +150 -0
- algo_cli/intelligence/consortium_synthesis.py +139 -0
- algo_cli/intelligence/construction/__init__.py +241 -0
- algo_cli/intelligence/construction/common.py +273 -0
- algo_cli/intelligence/construction/documents.py +496 -0
- algo_cli/intelligence/construction/labor_units.py +1395 -0
- algo_cli/intelligence/construction/payments.py +470 -0
- algo_cli/intelligence/construction/risk.py +784 -0
- algo_cli/intelligence/content_extractor.py +132 -0
- algo_cli/intelligence/context_adaptive.py +102 -0
- algo_cli/intelligence/context_ops.py +95 -0
- algo_cli/intelligence/count_min.py +145 -0
- algo_cli/intelligence/cow_state.py +103 -0
- algo_cli/intelligence/critic_loop.py +119 -0
- algo_cli/intelligence/cross_source.py +113 -0
- algo_cli/intelligence/daemon_mode.py +99 -0
- algo_cli/intelligence/dag_orchestration.py +151 -0
- algo_cli/intelligence/deep_research.py +155 -0
- algo_cli/intelligence/degenerate_detector.py +78 -0
- algo_cli/intelligence/delta_report.py +92 -0
- algo_cli/intelligence/discovery_event_log.py +92 -0
- algo_cli/intelligence/document_ingest.py +298 -0
- algo_cli/intelligence/dual_layer_validate.py +151 -0
- algo_cli/intelligence/echo_fidelity.py +73 -0
- algo_cli/intelligence/ema_tuning.py +104 -0
- algo_cli/intelligence/event_log.py +92 -0
- algo_cli/intelligence/evidence_graph.py +114 -0
- algo_cli/intelligence/extension_host.py +162 -0
- algo_cli/intelligence/extension_manifest.py +115 -0
- algo_cli/intelligence/falsification_suite.py +178 -0
- algo_cli/intelligence/finance/__init__.py +169 -0
- algo_cli/intelligence/finance/anomalies.py +135 -0
- algo_cli/intelligence/finance/ap_ar.py +351 -0
- algo_cli/intelligence/finance/cash.py +162 -0
- algo_cli/intelligence/finance/close.py +332 -0
- algo_cli/intelligence/finance/common.py +244 -0
- algo_cli/intelligence/finance/construction.py +135 -0
- algo_cli/intelligence/finance/controls.py +172 -0
- algo_cli/intelligence/finance/evidence.py +119 -0
- algo_cli/intelligence/finance/exceptions.py +157 -0
- algo_cli/intelligence/finance/reconciliations.py +254 -0
- algo_cli/intelligence/finance/revenue.py +109 -0
- algo_cli/intelligence/finance/tax.py +74 -0
- algo_cli/intelligence/finance/workpapers.py +111 -0
- algo_cli/intelligence/finding_record.py +120 -0
- algo_cli/intelligence/flow_dag.py +267 -0
- algo_cli/intelligence/gatherer.py +223 -0
- algo_cli/intelligence/golden_master.py +98 -0
- algo_cli/intelligence/graph_rag.py +195 -0
- algo_cli/intelligence/group_chat.py +143 -0
- algo_cli/intelligence/hash_dedup.py +145 -0
- algo_cli/intelligence/hyperloglog.py +128 -0
- algo_cli/intelligence/incremental_index.py +316 -0
- algo_cli/intelligence/index_store.py +16 -0
- algo_cli/intelligence/iteration_plan.py +133 -0
- algo_cli/intelligence/kernel_plugins.py +167 -0
- algo_cli/intelligence/lesson_catalog.py +135 -0
- algo_cli/intelligence/llm_fallback.py +169 -0
- algo_cli/intelligence/log2_histogram.py +267 -0
- algo_cli/intelligence/lsp_integration.py +147 -0
- algo_cli/intelligence/memory_evolution.py +117 -0
- algo_cli/intelligence/minhash_lsh.py +182 -0
- algo_cli/intelligence/multi_model_score.py +174 -0
- algo_cli/intelligence/multi_tier_grade.py +211 -0
- algo_cli/intelligence/negative_controls.py +113 -0
- algo_cli/intelligence/numeric_clamp.py +63 -0
- algo_cli/intelligence/occ_editor.py +66 -0
- algo_cli/intelligence/output_normalize.py +112 -0
- algo_cli/intelligence/parallel_delegation.py +98 -0
- algo_cli/intelligence/parallel_fanout.py +104 -0
- algo_cli/intelligence/permission_modes.py +105 -0
- algo_cli/intelligence/pre_push_gate.py +68 -0
- algo_cli/intelligence/prefetch.py +171 -0
- algo_cli/intelligence/process_framework.py +217 -0
- algo_cli/intelligence/project_graph.py +387 -0
- algo_cli/intelligence/query_expansion.py +146 -0
- algo_cli/intelligence/ralph_loop.py +117 -0
- algo_cli/intelligence/rate_limiter.py +153 -0
- algo_cli/intelligence/refactor_transaction.py +94 -0
- algo_cli/intelligence/research_workspace.py +108 -0
- algo_cli/intelligence/retraction_ledger.py +72 -0
- algo_cli/intelligence/saga_pattern.py +88 -0
- algo_cli/intelligence/session_fork.py +100 -0
- algo_cli/intelligence/shadow_editor.py +67 -0
- algo_cli/intelligence/shell_session.py +213 -0
- algo_cli/intelligence/source_registry.py +143 -0
- algo_cli/intelligence/spawn_scales.py +99 -0
- algo_cli/intelligence/stat_stability.py +104 -0
- algo_cli/intelligence/structural_validator.py +148 -0
- algo_cli/intelligence/subagent_spawner.py +111 -0
- algo_cli/intelligence/symmetric_verify.py +70 -0
- algo_cli/intelligence/task_classifier.py +129 -0
- algo_cli/intelligence/team_execution.py +122 -0
- algo_cli/intelligence/tiered_access.py +121 -0
- algo_cli/intelligence/utility_registry.py +159 -0
- algo_cli/intuition_engine.py +560 -0
- algo_cli/intuition_injector.py +82 -0
- algo_cli/kernels/__init__.py +5 -0
- algo_cli/kernels/manifest.py +763 -0
- algo_cli/main.py +3903 -0
- algo_cli/memory_candidates.py +541 -0
- algo_cli/memory_echo_veil.py +394 -0
- algo_cli/memory_runtime.py +112 -0
- algo_cli/model_info.py +548 -0
- algo_cli/model_profile.py +160 -0
- algo_cli/model_routing.py +74 -0
- algo_cli/oneshot.py +331 -0
- algo_cli/perf_telemetry.py +389 -0
- algo_cli/plugins.py +245 -0
- algo_cli/private_event_store.py +654 -0
- algo_cli/quantization/__init__.py +24 -0
- algo_cli/quantization/lloyd_max.py +98 -0
- algo_cli/quantization/turbo_quant.py +308 -0
- algo_cli/reasoning/__init__.py +46 -0
- algo_cli/reasoning/combinatorial.py +356 -0
- algo_cli/reasoning/graph_of_thought.py +297 -0
- algo_cli/reasoning/mcts.py +220 -0
- algo_cli/reasoning/neuro_symbolic.py +250 -0
- algo_cli/reasoning/react.py +246 -0
- algo_cli/reasoning/reflexion.py +225 -0
- algo_cli/reasoning/tree_of_thought.py +241 -0
- algo_cli/reasoning_bridge.py +150 -0
- algo_cli/reconciliation.py +284 -0
- algo_cli/reflex.py +385 -0
- algo_cli/resources/docs/ALGO.md +13958 -0
- algo_cli/resources/docs/algo-cli-algorithm-evidence-contract.md +60 -0
- algo_cli/resources/docs/algo-cli-execution-verification-contract.md +59 -0
- algo_cli/resources/docs/algo-cli-memory-lifecycle-contract.md +72 -0
- algo_cli/resources/docs/harness-extension-cleanup-recommendation.md +41 -0
- algo_cli/resources/docs/index-compute-lab-integration.md +32 -0
- algo_cli/resources/docs/inference-harness-loop-blueprint-2026-06.md +55 -0
- algo_cli/resources/docs/main-split-map.md +35 -0
- algo_cli/resources/docs/privacy-and-context.md +48 -0
- algo_cli/resources/docs/reflex-loop-v0.2.md +354 -0
- algo_cli/resources/skills/README.md +26 -0
- algo_cli/resources/skills/algo-cli.md +59 -0
- algo_cli/resources/skills/edit-file-precision.md +49 -0
- algo_cli/resources/skills/harness-search-first.md +47 -0
- algo_cli/resources/skills/memory-recall-ritual.md +51 -0
- algo_cli/resources/skills/qol-algorithms.md +224 -0
- algo_cli/resources/skills/smart-error-recovery.md +56 -0
- algo_cli/resources/skills/tool-selection-cheatsheet.md +65 -0
- algo_cli/retrieval_algorithms.py +127 -0
- algo_cli/runtime_qos.py +236 -0
- algo_cli/runtime_services.py +320 -0
- algo_cli/session_commands.py +95 -0
- algo_cli/session_mode.py +113 -0
- algo_cli/skills.py +430 -0
- algo_cli/slash_dispatch.py +1265 -0
- algo_cli/small_context.py +206 -0
- algo_cli/spawn_budget.py +89 -0
- algo_cli/task_ledger.py +84 -0
- algo_cli/task_router.py +197 -0
- algo_cli/tool_context.py +94 -0
- algo_cli/tool_contract.py +99 -0
- algo_cli/tool_policy.py +357 -0
- algo_cli/tool_runtime.py +647 -0
- algo_cli/tools.py +3056 -0
- algo_cli/url_scheme.py +174 -0
- algo_cli/verify.py +154 -0
- algo_cli/version_manifest.py +178 -0
- algo_cli/vision_screenshot_verify.py +76 -0
- algo_cli/workspace_resolver.py +68 -0
- algo_cli/x_account.py +209 -0
- algo_cli/xai_auth.py +374 -0
- algo_cli/xai_client.py +600 -0
- algo_cli_runtime-0.14.0.dist-info/METADATA +369 -0
- algo_cli_runtime-0.14.0.dist-info/RECORD +237 -0
- algo_cli_runtime-0.14.0.dist-info/WHEEL +4 -0
- algo_cli_runtime-0.14.0.dist-info/entry_points.txt +3 -0
- algo_cli_runtime-0.14.0.dist-info/licenses/LICENSE +21 -0
- ollama_cli/__init__.py +67 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""B86. Backpressure Signals for Token-Aware Agents.
|
|
2
|
+
|
|
3
|
+
PRESSURE=LOW/MED/HIGH tells agent when to expand or contract output.
|
|
4
|
+
BUDGET directives. Source: Keel pattern.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from enum import Enum, auto
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PressureLevel(Enum):
|
|
13
|
+
LOW = auto() # plenty of budget — be thorough
|
|
14
|
+
MEDIUM = auto() # budget tightening — be concise
|
|
15
|
+
HIGH = auto() # budget critical — minimal output only
|
|
16
|
+
CRITICAL = auto() # budget exhausted — stop
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class BackpressureSignal:
|
|
21
|
+
level: PressureLevel
|
|
22
|
+
remaining_tokens: int = 0
|
|
23
|
+
total_budget: int = 0
|
|
24
|
+
used_pct: float = 0.0
|
|
25
|
+
directive: str = ""
|
|
26
|
+
recommended_max_output: int = 0
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class BackpressureMonitor:
|
|
30
|
+
"""Monitor token budget and emit pressure signals."""
|
|
31
|
+
|
|
32
|
+
THRESHOLDS: dict[PressureLevel, float] = {
|
|
33
|
+
PressureLevel.LOW: 0.5, # >50% remaining
|
|
34
|
+
PressureLevel.MEDIUM: 0.25, # >25% remaining
|
|
35
|
+
PressureLevel.HIGH: 0.10, # >10% remaining
|
|
36
|
+
PressureLevel.CRITICAL: 0.0, # exhausted
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
DIRECTIVES: dict[PressureLevel, str] = {
|
|
40
|
+
PressureLevel.LOW: "BUDGET:EXPAND — be thorough, include examples and context",
|
|
41
|
+
PressureLevel.MEDIUM: "BUDGET:NORMAL — be concise but complete",
|
|
42
|
+
PressureLevel.HIGH: "BUDGET:CONTRACT — minimal output, skip explanations",
|
|
43
|
+
PressureLevel.CRITICAL: "BUDGET:STOP — output budget exhausted, stop immediately",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
MAX_OUTPUT: dict[PressureLevel, int] = {
|
|
47
|
+
PressureLevel.LOW: 4000,
|
|
48
|
+
PressureLevel.MEDIUM: 2000,
|
|
49
|
+
PressureLevel.HIGH: 500,
|
|
50
|
+
PressureLevel.CRITICAL: 0,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
def __init__(self, total_budget: int = 8000) -> None:
|
|
54
|
+
self._total = total_budget
|
|
55
|
+
self._used = 0
|
|
56
|
+
|
|
57
|
+
def consume(self, tokens: int) -> BackpressureSignal:
|
|
58
|
+
"""Consume tokens and return current pressure signal."""
|
|
59
|
+
self._used += tokens
|
|
60
|
+
return self.signal()
|
|
61
|
+
|
|
62
|
+
def signal(self) -> BackpressureSignal:
|
|
63
|
+
"""Get current backpressure signal without consuming."""
|
|
64
|
+
remaining = max(0, self._total - self._used)
|
|
65
|
+
used_pct = self._used / self._total if self._total > 0 else 1.0
|
|
66
|
+
remaining_pct = 1.0 - used_pct
|
|
67
|
+
|
|
68
|
+
# Determine level
|
|
69
|
+
level = PressureLevel.CRITICAL
|
|
70
|
+
for lvl, threshold in self.THRESHOLDS.items():
|
|
71
|
+
if remaining_pct >= threshold:
|
|
72
|
+
level = lvl
|
|
73
|
+
break
|
|
74
|
+
|
|
75
|
+
return BackpressureSignal(
|
|
76
|
+
level=level,
|
|
77
|
+
remaining_tokens=remaining,
|
|
78
|
+
total_budget=self._total,
|
|
79
|
+
used_pct=used_pct,
|
|
80
|
+
directive=self.DIRECTIVES[level],
|
|
81
|
+
recommended_max_output=self.MAX_OUTPUT[level],
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def reset(self) -> None:
|
|
85
|
+
self._used = 0
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def used(self) -> int:
|
|
89
|
+
return self._used
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def remaining(self) -> int:
|
|
93
|
+
return max(0, self._total - self._used)
|
|
94
|
+
|
|
95
|
+
def should_stop(self) -> bool:
|
|
96
|
+
return self.signal().level == PressureLevel.CRITICAL
|
|
97
|
+
|
|
98
|
+
def should_contract(self) -> bool:
|
|
99
|
+
return self.signal().level in (PressureLevel.HIGH, PressureLevel.CRITICAL)
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""Bloom Filter — probabilistic set membership with false positives.
|
|
2
|
+
|
|
3
|
+
A Bloom filter answers "is this item in the set?" in O(k) time using
|
|
4
|
+
k hash functions and a bit array. False positives are possible but
|
|
5
|
+
false negatives are impossible. Memory is ~10x smaller than a hash set.
|
|
6
|
+
|
|
7
|
+
Harness use: dedup for embedding pipeline — "has this file already been
|
|
8
|
+
embedded?" without storing every path. Also useful for URL dedup
|
|
9
|
+
("have we already fetched this URL?") and content dedup.
|
|
10
|
+
|
|
11
|
+
Operations:
|
|
12
|
+
- add(item): O(k) — set k bits
|
|
13
|
+
- contains(item): O(k) — check k bits
|
|
14
|
+
- false_positive_rate(): current estimated FPR
|
|
15
|
+
- merge(other): union two filters (same size/hashes)
|
|
16
|
+
|
|
17
|
+
Properties:
|
|
18
|
+
- No false negatives (if contains() returns False, item was never added)
|
|
19
|
+
- False positive rate tunable via bit_array_size and num_hashes
|
|
20
|
+
- Cannot remove items (use CountingBloomFilter for that)
|
|
21
|
+
"""
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import hashlib
|
|
25
|
+
import math
|
|
26
|
+
from typing import Any
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class BloomFilter:
|
|
30
|
+
"""Standard Bloom filter with double-hashing (Kirsch-Mitzenmacher).
|
|
31
|
+
|
|
32
|
+
Uses two base hashes h1=MD5, h2=SHA1 and derives k hashes via
|
|
33
|
+
h_i(x) = h1(x) + i * h2(x), avoiding k separate hash computations.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
capacity: int = 10_000,
|
|
39
|
+
false_positive_rate: float = 0.01,
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Create a Bloom filter sized for *capacity* items at target FPR.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
capacity: Expected number of items to add.
|
|
45
|
+
false_positive_rate: Target false positive rate (e.g. 0.01 = 1%).
|
|
46
|
+
"""
|
|
47
|
+
self.capacity = capacity
|
|
48
|
+
self.target_fpr = false_positive_rate
|
|
49
|
+
self.bit_array_size = self._optimal_m(capacity, false_positive_rate)
|
|
50
|
+
self.num_hashes = self._optimal_k(self.bit_array_size, capacity)
|
|
51
|
+
self._bits = bytearray((self.bit_array_size + 7) // 8)
|
|
52
|
+
self.count = 0
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def _optimal_m(n: int, p: float) -> int:
|
|
56
|
+
"""Optimal bit array size: m = -n*ln(p) / (ln(2)^2)."""
|
|
57
|
+
return max(1, int(-n * math.log(p) / (math.log(2) ** 2)))
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def _optimal_k(m: int, n: int) -> int:
|
|
61
|
+
"""Optimal number of hash functions: k = (m/n) * ln(2)."""
|
|
62
|
+
return max(1, int((m / max(1, n)) * math.log(2)))
|
|
63
|
+
|
|
64
|
+
def _hashes(self, item: Any) -> list[int]:
|
|
65
|
+
"""Generate k hash positions using double-hashing."""
|
|
66
|
+
data = str(item).encode("utf-8")
|
|
67
|
+
h1 = int.from_bytes(hashlib.md5(data).digest()[:8], "little")
|
|
68
|
+
h2 = int.from_bytes(hashlib.sha1(data).digest()[:8], "little")
|
|
69
|
+
m = self.bit_array_size
|
|
70
|
+
return [(h1 + i * h2) % m for i in range(self.num_hashes)]
|
|
71
|
+
|
|
72
|
+
def _set_bit(self, pos: int) -> None:
|
|
73
|
+
self._bits[pos >> 3] |= (1 << (pos & 7))
|
|
74
|
+
|
|
75
|
+
def _get_bit(self, pos: int) -> bool:
|
|
76
|
+
return bool(self._bits[pos >> 3] & (1 << (pos & 7)))
|
|
77
|
+
|
|
78
|
+
def add(self, item: Any) -> None:
|
|
79
|
+
"""Add an item to the filter."""
|
|
80
|
+
for pos in self._hashes(item):
|
|
81
|
+
self._set_bit(pos)
|
|
82
|
+
self.count += 1
|
|
83
|
+
|
|
84
|
+
def add_many(self, items: list[Any]) -> None:
|
|
85
|
+
"""Add multiple items."""
|
|
86
|
+
for item in items:
|
|
87
|
+
self.add(item)
|
|
88
|
+
|
|
89
|
+
def contains(self, item: Any) -> bool:
|
|
90
|
+
"""Check if item might be in the set.
|
|
91
|
+
|
|
92
|
+
Returns True if the item is possibly in the set (may be false positive).
|
|
93
|
+
Returns False if the item is definitely not in the set.
|
|
94
|
+
"""
|
|
95
|
+
return all(self._get_bit(pos) for pos in self._hashes(item))
|
|
96
|
+
|
|
97
|
+
def __contains__(self, item: Any) -> bool:
|
|
98
|
+
return self.contains(item)
|
|
99
|
+
|
|
100
|
+
def false_positive_rate(self) -> float:
|
|
101
|
+
"""Current estimated false positive rate based on items added."""
|
|
102
|
+
if self.count == 0:
|
|
103
|
+
return 0.0
|
|
104
|
+
k = self.num_hashes
|
|
105
|
+
m = self.bit_array_size
|
|
106
|
+
n = self.count
|
|
107
|
+
return (1 - math.exp(-k * n / m)) ** k
|
|
108
|
+
|
|
109
|
+
def merge(self, other: BloomFilter) -> None:
|
|
110
|
+
"""Merge another filter into this one (in-place union).
|
|
111
|
+
|
|
112
|
+
Both filters must have the same bit_array_size and num_hashes.
|
|
113
|
+
"""
|
|
114
|
+
if self.bit_array_size != other.bit_array_size:
|
|
115
|
+
raise ValueError("Cannot merge filters with different sizes")
|
|
116
|
+
if self.num_hashes != other.num_hashes:
|
|
117
|
+
raise ValueError("Cannot merge filters with different hash counts")
|
|
118
|
+
for i in range(len(self._bits)):
|
|
119
|
+
self._bits[i] |= other._bits[i]
|
|
120
|
+
self.count += other.count
|
|
121
|
+
|
|
122
|
+
def bit_density(self) -> float:
|
|
123
|
+
"""Fraction of bits set — useful for monitoring saturation."""
|
|
124
|
+
set_bits = sum(bin(b).count("1") for b in self._bits)
|
|
125
|
+
return set_bits / self.bit_array_size
|
|
126
|
+
|
|
127
|
+
def stats(self) -> dict[str, Any]:
|
|
128
|
+
return {
|
|
129
|
+
"capacity": self.capacity,
|
|
130
|
+
"bit_array_size": self.bit_array_size,
|
|
131
|
+
"num_hashes": self.num_hashes,
|
|
132
|
+
"items_added": self.count,
|
|
133
|
+
"false_positive_rate": round(self.false_positive_rate(), 6),
|
|
134
|
+
"bit_density": round(self.bit_density(), 4),
|
|
135
|
+
"memory_bytes": len(self._bits),
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class CountingBloomFilter:
|
|
140
|
+
"""Counting Bloom filter — supports removal via counter per slot.
|
|
141
|
+
|
|
142
|
+
Uses 4-bit counters (max count 15) instead of single bits.
|
|
143
|
+
Slightly more memory than standard BloomFilter but supports delete().
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
def __init__(
|
|
147
|
+
self,
|
|
148
|
+
capacity: int = 10_000,
|
|
149
|
+
false_positive_rate: float = 0.01,
|
|
150
|
+
) -> None:
|
|
151
|
+
self.capacity = capacity
|
|
152
|
+
m = BloomFilter._optimal_m(capacity, false_positive_rate)
|
|
153
|
+
self.bit_array_size = m
|
|
154
|
+
self.num_hashes = BloomFilter._optimal_k(m, capacity)
|
|
155
|
+
self._counters = bytearray(m) # each counter 0-255 (we cap at 255)
|
|
156
|
+
self.count = 0
|
|
157
|
+
|
|
158
|
+
def _hashes(self, item: Any) -> list[int]:
|
|
159
|
+
data = str(item).encode("utf-8")
|
|
160
|
+
h1 = int.from_bytes(hashlib.md5(data).digest()[:8], "little")
|
|
161
|
+
h2 = int.from_bytes(hashlib.sha1(data).digest()[:8], "little")
|
|
162
|
+
m = self.bit_array_size
|
|
163
|
+
return [(h1 + i * h2) % m for i in range(self.num_hashes)]
|
|
164
|
+
|
|
165
|
+
def add(self, item: Any) -> None:
|
|
166
|
+
for pos in self._hashes(item):
|
|
167
|
+
if self._counters[pos] < 255:
|
|
168
|
+
self._counters[pos] += 1
|
|
169
|
+
self.count += 1
|
|
170
|
+
|
|
171
|
+
def remove(self, item: Any) -> None:
|
|
172
|
+
"""Remove an item. Only call if you know the item was previously added."""
|
|
173
|
+
for pos in self._hashes(item):
|
|
174
|
+
if self._counters[pos] > 0:
|
|
175
|
+
self._counters[pos] -= 1
|
|
176
|
+
self.count = max(0, self.count - 1)
|
|
177
|
+
|
|
178
|
+
def contains(self, item: Any) -> bool:
|
|
179
|
+
return all(self._counters[pos] > 0 for pos in self._hashes(item))
|
|
180
|
+
|
|
181
|
+
def __contains__(self, item: Any) -> bool:
|
|
182
|
+
return self.contains(item)
|
|
183
|
+
|
|
184
|
+
def estimate_count(self, item: Any) -> int:
|
|
185
|
+
"""Estimate the number of times item was added (min of counters)."""
|
|
186
|
+
return min(self._counters[pos] for pos in self._hashes(item))
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""H24 — Bonferroni Correction for Multiple Comparisons.
|
|
2
|
+
|
|
3
|
+
Prevents false discoveries when running many tests by adjusting the
|
|
4
|
+
significance threshold: divide alpha by the number of comparisons.
|
|
5
|
+
|
|
6
|
+
Source: GLOSSOPETRAE ``e3s_multiconstruction_stego.mjs``.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class BonferroniResult:
|
|
15
|
+
"""Result of a Bonferroni correction check."""
|
|
16
|
+
|
|
17
|
+
p_value: float
|
|
18
|
+
adjusted_alpha: float
|
|
19
|
+
is_significant: bool
|
|
20
|
+
n_tests: int
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class BonferroniGuard:
|
|
24
|
+
"""Bonferroni correction guard for multiple comparisons."""
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def correct(alpha: float, n_tests: int) -> float:
|
|
28
|
+
"""Return the Bonferroni-adjusted alpha threshold.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
alpha: Original significance level (e.g. 0.05).
|
|
32
|
+
n_tests: Number of comparisons performed.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
Adjusted alpha = alpha / n_tests.
|
|
36
|
+
"""
|
|
37
|
+
if n_tests < 1:
|
|
38
|
+
raise ValueError(f"n_tests must be >= 1, got {n_tests}")
|
|
39
|
+
return alpha / n_tests
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def is_significant(p_value: float, alpha: float, n_tests: int) -> bool:
|
|
43
|
+
"""Check if a p-value is significant after Bonferroni correction."""
|
|
44
|
+
adjusted = BonferroniGuard.correct(alpha, n_tests)
|
|
45
|
+
return p_value < adjusted
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def evaluate(p_value: float, alpha: float, n_tests: int) -> BonferroniResult:
|
|
49
|
+
"""Full evaluation with details."""
|
|
50
|
+
adjusted = BonferroniGuard.correct(alpha, n_tests)
|
|
51
|
+
return BonferroniResult(
|
|
52
|
+
p_value=p_value,
|
|
53
|
+
adjusted_alpha=adjusted,
|
|
54
|
+
is_significant=p_value < adjusted,
|
|
55
|
+
n_tests=n_tests,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def evaluate_many(
|
|
60
|
+
p_values: list[float], alpha: float
|
|
61
|
+
) -> list[BonferroniResult]:
|
|
62
|
+
"""Evaluate multiple p-values with Bonferroni correction."""
|
|
63
|
+
n = len(p_values)
|
|
64
|
+
return [
|
|
65
|
+
BonferroniGuard.evaluate(p, alpha, n) for p in p_values
|
|
66
|
+
]
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""B54. Boundary-Aware Context Compaction.
|
|
2
|
+
|
|
3
|
+
Preserve function-call pairs atomically during compaction.
|
|
4
|
+
Source: openai-agents-context-compaction pattern.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class Message:
|
|
13
|
+
role: str
|
|
14
|
+
content: str
|
|
15
|
+
tool_call_id: str | None = None
|
|
16
|
+
tool_calls: list[dict] | None = None
|
|
17
|
+
token_estimate: int = 0
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class CompactionResult:
|
|
22
|
+
messages: list[Message]
|
|
23
|
+
removed_count: int
|
|
24
|
+
preserved_pairs: int
|
|
25
|
+
summary: str = ""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class BoundaryAwareCompactor:
|
|
29
|
+
"""Compact context while preserving function-call pairs atomically."""
|
|
30
|
+
|
|
31
|
+
def __init__(self, target_tokens: int = 8000, min_keep: int = 4):
|
|
32
|
+
self.target_tokens = target_tokens
|
|
33
|
+
self.min_keep = min_keep
|
|
34
|
+
|
|
35
|
+
def _estimate_tokens(self, msg: Message) -> int:
|
|
36
|
+
if msg.token_estimate:
|
|
37
|
+
return msg.token_estimate
|
|
38
|
+
return max(1, len(msg.content) // 4)
|
|
39
|
+
|
|
40
|
+
def _total_tokens(self, messages: list[Message]) -> int:
|
|
41
|
+
return sum(self._estimate_tokens(m) for m in messages)
|
|
42
|
+
|
|
43
|
+
def _find_call_pairs(self, messages: list[Message]) -> list[tuple[int, int]]:
|
|
44
|
+
"""Find (assistant_with_tool_call, tool_response) index pairs."""
|
|
45
|
+
pairs: list[tuple[int, int]] = []
|
|
46
|
+
for i, msg in enumerate(messages):
|
|
47
|
+
if msg.role == "assistant" and msg.tool_calls:
|
|
48
|
+
for call in msg.tool_calls:
|
|
49
|
+
call_id = call.get("id")
|
|
50
|
+
for j in range(i + 1, len(messages)):
|
|
51
|
+
if messages[j].tool_call_id == call_id:
|
|
52
|
+
pairs.append((i, j))
|
|
53
|
+
break
|
|
54
|
+
return pairs
|
|
55
|
+
|
|
56
|
+
def compact(self, messages: list[Message]) -> CompactionResult:
|
|
57
|
+
"""Compact messages to fit within target_tokens."""
|
|
58
|
+
total = self._total_tokens(messages)
|
|
59
|
+
if total <= self.target_tokens:
|
|
60
|
+
return CompactionResult(messages=messages, removed_count=0, preserved_pairs=0)
|
|
61
|
+
|
|
62
|
+
pairs = self._find_call_pairs(messages)
|
|
63
|
+
pair_indices: set[int] = set()
|
|
64
|
+
for a, b in pairs:
|
|
65
|
+
pair_indices.add(a)
|
|
66
|
+
pair_indices.add(b)
|
|
67
|
+
|
|
68
|
+
# Always keep the last min_keep messages
|
|
69
|
+
protected = set(range(max(0, len(messages) - self.min_keep), len(messages)))
|
|
70
|
+
protected |= pair_indices
|
|
71
|
+
|
|
72
|
+
# Remove oldest non-protected messages until under target
|
|
73
|
+
removed = 0
|
|
74
|
+
result = list(messages)
|
|
75
|
+
i = 0
|
|
76
|
+
while self._total_tokens(result) > self.target_tokens and i < len(result):
|
|
77
|
+
if i in protected:
|
|
78
|
+
i += 1
|
|
79
|
+
continue
|
|
80
|
+
result.pop(i)
|
|
81
|
+
removed += 1
|
|
82
|
+
# Rebuild protected set indices after removal
|
|
83
|
+
protected = set(range(max(0, len(result) - self.min_keep), len(result)))
|
|
84
|
+
# Rebuild pair indices
|
|
85
|
+
pair_indices.clear()
|
|
86
|
+
new_pairs = self._find_call_pairs(result)
|
|
87
|
+
for a, b in new_pairs:
|
|
88
|
+
pair_indices.add(a)
|
|
89
|
+
pair_indices.add(b)
|
|
90
|
+
protected |= pair_indices
|
|
91
|
+
else:
|
|
92
|
+
i += 1
|
|
93
|
+
|
|
94
|
+
return CompactionResult(
|
|
95
|
+
messages=result,
|
|
96
|
+
removed_count=removed,
|
|
97
|
+
preserved_pairs=len(pairs),
|
|
98
|
+
)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""H2 — Algorithm Catalog Verifier.
|
|
2
|
+
|
|
3
|
+
Re-derive every `implemented` status from live tests.
|
|
4
|
+
Mined from T3MP3ST verify-claims.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class CatalogEntry:
|
|
15
|
+
"""A single catalog entry parsed from ALGO.md."""
|
|
16
|
+
|
|
17
|
+
id: str
|
|
18
|
+
title: str
|
|
19
|
+
status: str = "unknown" # "implemented", "proposed", "partial", "retired"
|
|
20
|
+
section: str = ""
|
|
21
|
+
line_number: int = 0
|
|
22
|
+
metadata: dict[str, Any] = field(default_factory=dict)
|
|
23
|
+
|
|
24
|
+
def to_dict(self) -> dict[str, Any]:
|
|
25
|
+
return {
|
|
26
|
+
"id": self.id,
|
|
27
|
+
"title": self.title,
|
|
28
|
+
"status": self.status,
|
|
29
|
+
"section": self.section,
|
|
30
|
+
"line_number": self.line_number,
|
|
31
|
+
"metadata": dict(self.metadata),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class VerificationResult:
|
|
37
|
+
"""Result of verifying a single catalog entry."""
|
|
38
|
+
|
|
39
|
+
entry_id: str
|
|
40
|
+
claimed_status: str
|
|
41
|
+
verified: bool
|
|
42
|
+
reason: str = ""
|
|
43
|
+
test_names: list[str] = field(default_factory=list)
|
|
44
|
+
|
|
45
|
+
def to_dict(self) -> dict[str, Any]:
|
|
46
|
+
return {
|
|
47
|
+
"entry_id": self.entry_id,
|
|
48
|
+
"claimed_status": self.claimed_status,
|
|
49
|
+
"verified": self.verified,
|
|
50
|
+
"reason": self.reason,
|
|
51
|
+
"test_names": list(self.test_names),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class VerificationReport:
|
|
57
|
+
"""Full report of catalog verification."""
|
|
58
|
+
|
|
59
|
+
results: list[VerificationResult] = field(default_factory=list)
|
|
60
|
+
total_entries: int = 0
|
|
61
|
+
verified_count: int = 0
|
|
62
|
+
failed_count: int = 0
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def all_verified(self) -> bool:
|
|
66
|
+
return self.failed_count == 0
|
|
67
|
+
|
|
68
|
+
def to_dict(self) -> dict[str, Any]:
|
|
69
|
+
return {
|
|
70
|
+
"results": [r.to_dict() for r in self.results],
|
|
71
|
+
"total_entries": self.total_entries,
|
|
72
|
+
"verified_count": self.verified_count,
|
|
73
|
+
"failed_count": self.failed_count,
|
|
74
|
+
"all_verified": self.all_verified,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class CatalogVerifier:
|
|
79
|
+
"""Parse ALGO.md and verify claimed statuses against live tests."""
|
|
80
|
+
|
|
81
|
+
# Match entries like "### H1. Title", "### H1 — Title", or "### H1 Title"
|
|
82
|
+
_ENTRY_RE = re.compile(r"^###\s+([A-Z]\d+)\.?\s*[—–\-\s]\s*(.+)$", re.MULTILINE)
|
|
83
|
+
# Match status markers
|
|
84
|
+
_STATUS_RE = re.compile(r"\b(implemented|proposed|partial|retired)\b", re.IGNORECASE)
|
|
85
|
+
|
|
86
|
+
def parse_catalog(self, markdown_text: str) -> list[CatalogEntry]:
|
|
87
|
+
"""Parse ALGO.md markdown and return catalog entries."""
|
|
88
|
+
entries: list[CatalogEntry] = []
|
|
89
|
+
for match in self._ENTRY_RE.finditer(markdown_text):
|
|
90
|
+
entry_id = match.group(1)
|
|
91
|
+
title = match.group(2).strip()
|
|
92
|
+
line_num = markdown_text[: match.start()].count("\n") + 1
|
|
93
|
+
# Look for status in the next ~500 chars
|
|
94
|
+
after_text = markdown_text[match.end() : match.end() + 500]
|
|
95
|
+
status_match = self._STATUS_RE.search(after_text)
|
|
96
|
+
status = status_match.group(1).lower() if status_match else "unknown"
|
|
97
|
+
entries.append(
|
|
98
|
+
CatalogEntry(
|
|
99
|
+
id=entry_id,
|
|
100
|
+
title=title,
|
|
101
|
+
status=status,
|
|
102
|
+
line_number=line_num,
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
return entries
|
|
106
|
+
|
|
107
|
+
def verify(
|
|
108
|
+
self,
|
|
109
|
+
entries: list[CatalogEntry],
|
|
110
|
+
test_results: dict[str, bool] | None = None,
|
|
111
|
+
) -> VerificationReport:
|
|
112
|
+
"""Verify entries against test results.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
entries: Parsed catalog entries.
|
|
116
|
+
test_results: Map of entry_id → test_passed. If None, all claimed
|
|
117
|
+
"implemented" entries without tests are flagged.
|
|
118
|
+
"""
|
|
119
|
+
test_results = test_results or {}
|
|
120
|
+
results: list[VerificationResult] = []
|
|
121
|
+
verified = 0
|
|
122
|
+
failed = 0
|
|
123
|
+
for entry in entries:
|
|
124
|
+
if entry.status != "implemented":
|
|
125
|
+
results.append(
|
|
126
|
+
VerificationResult(
|
|
127
|
+
entry_id=entry.id,
|
|
128
|
+
claimed_status=entry.status,
|
|
129
|
+
verified=True,
|
|
130
|
+
reason=f"Status is '{entry.status}' — no verification needed",
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
verified += 1
|
|
134
|
+
continue
|
|
135
|
+
# Check if we have test results
|
|
136
|
+
test_passed = test_results.get(entry.id)
|
|
137
|
+
if test_passed is None:
|
|
138
|
+
results.append(
|
|
139
|
+
VerificationResult(
|
|
140
|
+
entry_id=entry.id,
|
|
141
|
+
claimed_status=entry.status,
|
|
142
|
+
verified=False,
|
|
143
|
+
reason="Claimed 'implemented' but no test result provided",
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
failed += 1
|
|
147
|
+
elif test_passed:
|
|
148
|
+
results.append(
|
|
149
|
+
VerificationResult(
|
|
150
|
+
entry_id=entry.id,
|
|
151
|
+
claimed_status=entry.status,
|
|
152
|
+
verified=True,
|
|
153
|
+
reason="Test passed",
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
verified += 1
|
|
157
|
+
else:
|
|
158
|
+
results.append(
|
|
159
|
+
VerificationResult(
|
|
160
|
+
entry_id=entry.id,
|
|
161
|
+
claimed_status=entry.status,
|
|
162
|
+
verified=False,
|
|
163
|
+
reason="Test failed",
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
failed += 1
|
|
167
|
+
return VerificationReport(
|
|
168
|
+
results=results,
|
|
169
|
+
total_entries=len(entries),
|
|
170
|
+
verified_count=verified,
|
|
171
|
+
failed_count=failed,
|
|
172
|
+
)
|