Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5befd81b23 | ||
|
|
3c79a0a9c0 | ||
|
|
79bbb5fea8 | ||
|
|
872b0c7a75 | ||
|
|
fcee8fbec7 | ||
|
|
d767bd9a9e | ||
|
|
47e6eba735 | ||
|
|
818f4b9e21 | ||
|
|
d1631a8333 | ||
|
|
f5754fe2c0 | ||
|
|
6df2580567 | ||
|
|
3ee6f59e1b | ||
|
|
3811219abc | ||
|
|
edf9d52caa | ||
|
|
8a2525c4ac | ||
|
|
fb69ae83fc | ||
|
|
d3a240edc8 | ||
|
|
278c5e372b | ||
|
|
d08621222a | ||
|
|
db25db26d1 | ||
|
|
1a321154f8 | ||
|
|
ac9765d5d1 | ||
|
|
0fdfe195e7 | ||
|
|
71d2d63a36 |
No files matched your search
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: land
|
||||
description: Review and land one small Rune change through the governed PR workflow.
|
||||
disable-model-invocation: true
|
||||
metadata:
|
||||
delta-action: land
|
||||
---
|
||||
|
||||
# Land one small diff
|
||||
|
||||
1. Read applicable `AGENTS.md` rules and the repo's PR/merge workflow.
|
||||
2. Confirm the issue, exact diff, target branch, and permission to publish and land.
|
||||
3. Keep one small, reversible change; preserve unrelated work.
|
||||
4. For bugs, follow `docs/bugfix-contract.md`: red test first, root cause, discharge.
|
||||
5. Run required tests, static analysis, canaries, and henxels; record results.
|
||||
6. Get independent `/review` per `docs/adversarial-review.md`; clear blockers.
|
||||
7. Inspect remotes; publish the PR to the approved forge, never `local`.
|
||||
8. Require human approval and green required CI on the exact PR head; recheck changed heads.
|
||||
9. Load `report-landed`; use the repo's merge workflow. Stop on conflicts; never force-push or bypass hooks.
|
||||
10. Verify the target commit and CI, then report the actual landing result and remaining blockers.
|
||||
@@ -81,6 +81,7 @@ jobs:
|
||||
SC_RUST_LIB: /tmp/rune-ci-target-${{ github.run_id }}/release/libscaffold_core.so
|
||||
MEMS_RUST_LIB: /tmp/rune-ci-target-${{ github.run_id }}/release/libmemory_storage.so
|
||||
MR_RUST_LIB: /tmp/rune-ci-target-${{ github.run_id }}/release/libmemory_retrieval.so
|
||||
CC_RUST_LIB: /tmp/rune-ci-target-${{ github.run_id }}/release/libcognition_core.so
|
||||
steps:
|
||||
# actions/checkout v4.2.2, pinned to the audited upstream tag commit.
|
||||
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
|
||||
@@ -135,6 +136,7 @@ jobs:
|
||||
cargo build --release --manifest-path src/rust/scaffold_core/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/memory_storage/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/memory_retrieval/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/cognition_core/Cargo.toml
|
||||
- name: Test with coverage (global floor + changed-lines ratchet)
|
||||
run: |
|
||||
# #149 coverage ratchet — both floors live in .coveragerc; raise,
|
||||
@@ -224,6 +226,7 @@ jobs:
|
||||
cargo build --release --manifest-path src/rust/scaffold_core/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/memory_storage/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/memory_retrieval/Cargo.toml
|
||||
cargo build --release --manifest-path src/rust/cognition_core/Cargo.toml
|
||||
- name: Run canary suite
|
||||
run: |
|
||||
FAILED=0
|
||||
@@ -262,6 +265,9 @@ jobs:
|
||||
src/rust/scaffold_core/canary_scaffold_core.py \
|
||||
src/rust/memory_storage/canary_memory_storage.py \
|
||||
src/rust/memory_retrieval/canary_memory_retrieval.py \
|
||||
src/rust/cognition_core/canary_growth_pins.py \
|
||||
src/rust/cognition_core/canary_persona.py \
|
||||
src/rust/cognition_core/canary_cognition_core.py \
|
||||
src/rune/canary_unbounded.py \
|
||||
src/rune/canary_full_system.py \
|
||||
src/rune/canary_native.py; do
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Rune
|
||||
|
||||
**Current release: v0.99.5**
|
||||
**Current release: v0.100.0**
|
||||
|
||||
**Rune is a persistent personal agent designed to accumulate experience, develop reusable competence, and remain recognisably the same agent even when its underlying foundation model changes.**
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "rune"
|
||||
version = "0.99.5"
|
||||
version = "0.100.0"
|
||||
description = "Rune — a stateful coding assistant with persistent memory, orchestration, and TUI"
|
||||
requires-python = ">=3.10"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ This is the main entry point for the Rune program.
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
__version__ = "0.99.5"
|
||||
__version__ = "0.100.0"
|
||||
|
||||
# One build identity (handoff 2026-09-10 §4): the semantic version above is
|
||||
# the repo-level truth (auto-bumped at merge); the DEPLOYED revision (exact
|
||||
|
||||
@@ -233,7 +233,7 @@ def main() -> int:
|
||||
)
|
||||
check(
|
||||
"Rune minor version advertises provisioning and restore verification",
|
||||
'__version__ = "0.99.5"' in rune_version,
|
||||
'__version__ = "0.100.0"' in rune_version,
|
||||
failures,
|
||||
)
|
||||
# #169 policy update: auto-bump on merge is sanctioned (Nick directive:
|
||||
|
||||
@@ -148,6 +148,11 @@ def _select_for_job(description: str, need_tools: bool) -> dict:
|
||||
|
||||
floor = [name for name in FLOOR_TOOLS if name in schemas]
|
||||
matches = [name for _, name in _scored_tools(description, schemas)]
|
||||
# An explicit shell request cannot succeed via the five-file-tool floor.
|
||||
# Expose the canonical command tool, never execute it without its
|
||||
# unchanged operator approval boundary.
|
||||
if re.search(r"\b(?:bash|shell|terminal)\b", description, re.IGNORECASE) and "run_command" in schemas:
|
||||
matches = ["run_command"] + [name for name in matches if name != "run_command"]
|
||||
# Reserve one slot for the escape hatch; cap the whole set.
|
||||
budget = max(0, MAX_SELECTED - len(floor) - 1)
|
||||
matches = matches[:budget]
|
||||
|
||||
+16
-1
@@ -56,7 +56,8 @@ def _load() -> ctypes.CDLL:
|
||||
"cc_growth_profile", "cc_pins_recall", "cc_tier_get",
|
||||
"cc_tier_promote", "cc_tier_demote", "cc_witness_extract_topic",
|
||||
"cc_witness_signals", "cc_safety_get_params",
|
||||
"cc_safety_update_pattern", "cc_abstraction_extract_facts"):
|
||||
"cc_safety_update_pattern", "cc_abstraction_extract_facts",
|
||||
"cc_consolidation_memory", "cc_consolidation_neuron"):
|
||||
fn = getattr(lib, sym)
|
||||
fn.argtypes = [ctypes.c_char_p]
|
||||
fn.restype = ctypes.c_void_p
|
||||
@@ -178,3 +179,17 @@ def safety_update_pattern(rows: list, pattern: str) -> list:
|
||||
def abstraction_extract_facts(text: str, stopwords: list, min_len: int = 10):
|
||||
return _call("cc_abstraction_extract_facts", {
|
||||
"text": text, "stopwords": stopwords, "min_len": min_len})
|
||||
|
||||
def consolidation_memory(text: str, score: float, wash_count: int = 0,
|
||||
entry_id: int | None = None, now: str = "") -> dict:
|
||||
return _call("cc_consolidation_memory", {
|
||||
"text": text, "score": score, "wash_count": wash_count,
|
||||
"entry_id": entry_id, "now": now})
|
||||
|
||||
def consolidation_neuron(slug: str, body: str, importance: float,
|
||||
triggers: list, wash_count: int = 0,
|
||||
entry_id: int | None = None, now: str = "") -> dict:
|
||||
return _call("cc_consolidation_neuron", {
|
||||
"slug": slug, "body": body, "importance": importance,
|
||||
"triggers": triggers, "wash_count": wash_count,
|
||||
"entry_id": entry_id, "now": now})
|
||||
@@ -62,6 +62,59 @@ PYTHON = os.path.expanduser("~/.venvs/memory/bin/python3")
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
def _core():
|
||||
"""The Rust cognition_core when available (strangler: Python fallback
|
||||
load-bearing). RUNE_COGNITION_CORE=python pins the fallback."""
|
||||
try:
|
||||
from rune import cc_loader
|
||||
except ImportError:
|
||||
import cc_loader
|
||||
try:
|
||||
return cc_loader if cc_loader.available() else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _consolidation_memory_oracle(memory_text: str, score: float, wash_count: int = 0,
|
||||
entry_id: int | None = None) -> dict:
|
||||
"""Python parity oracle for consolidation_memory."""
|
||||
if drift_compression:
|
||||
entry = {"text": memory_text, "wash_count": wash_count, "rock_status": "washable"}
|
||||
content = drift_compression.compress_entry(entry)
|
||||
else:
|
||||
content = memory_text[:100] + "..." if len(memory_text) > 100 else memory_text
|
||||
from datetime import datetime, timezone
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
eid = f"memory-{entry_id}" if entry_id else f"memory-{hash(memory_text) % 10000}"
|
||||
return {"id": eid, "source": "memory", "category": "lesson",
|
||||
"content": content, "confidence": score,
|
||||
"created_at": now, "last_used": now,
|
||||
"use_count": 0, "wash_count": wash_count,
|
||||
"entry_id": entry_id, "tags": []}
|
||||
|
||||
|
||||
def _consolidation_neuron_oracle(slug: str, body: str, importance: float,
|
||||
triggers: list, wash_count: int = 0,
|
||||
entry_id: int | None = None) -> dict:
|
||||
"""Python parity oracle for consolidation_neuron."""
|
||||
if drift_compression:
|
||||
entry = {"text": body, "wash_count": wash_count, "rock_status": "washable"}
|
||||
first_line = drift_compression.compress_entry(entry)
|
||||
else:
|
||||
first_line = body.split("\n")[0].strip()
|
||||
if len(first_line) > 100:
|
||||
first_line = first_line[:97] + "..."
|
||||
if not first_line:
|
||||
first_line = body[:100]
|
||||
from datetime import datetime, timezone
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
return {"id": f"neuron-{slug}", "source": "neuron", "category": "fact",
|
||||
"content": first_line, "confidence": importance,
|
||||
"created_at": now, "last_used": now,
|
||||
"use_count": 0, "wash_count": wash_count,
|
||||
"entry_id": entry_id, "tags": triggers}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConsolidatedKnowledge:
|
||||
"""Knowledge consolidated from episodic memory."""
|
||||
@@ -163,7 +216,8 @@ class MemoryConsolidation:
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
def consolidate_neuron(self, neuron: Dict[str, Any]) -> ConsolidatedKnowledge:
|
||||
"""Consolidate a warm neuron into knowledge."""
|
||||
"""Consolidate a warm neuron into knowledge.
|
||||
Uses the oracle (which delegates to the kernel when available)."""
|
||||
slug = neuron.get("slug", "unknown")
|
||||
body = neuron.get("body", neuron.get("text", ""))
|
||||
importance = neuron.get("importance", 0.5)
|
||||
@@ -183,42 +237,12 @@ class MemoryConsolidation:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Use drift-aware compression with compaction warnings
|
||||
if drift_compression:
|
||||
entry = {"text": body, "wash_count": wash_count, "rock_status": "washable"}
|
||||
# Compaction warning: alert if compressing proven content
|
||||
if drift_compression.should_warn(entry):
|
||||
warning = drift_compression.warn_before_compress(entry)
|
||||
# Log warning but don't block — the gate system handles promotion
|
||||
import sys
|
||||
|
||||
print(f" [compaction warning] neuron {slug}: {warning['recommendation']}", file=sys.stderr)
|
||||
first_line = drift_compression.compress_entry(entry)
|
||||
else:
|
||||
# Fallback: hardcoded truncation
|
||||
first_line = body.split("\n")[0].strip()
|
||||
if len(first_line) > 100:
|
||||
first_line = first_line[:97] + "..."
|
||||
if not first_line:
|
||||
first_line = body[:100]
|
||||
|
||||
# Normalize triggers — may be string or list
|
||||
if isinstance(triggers, str):
|
||||
triggers = [t.strip() for t in triggers.split(",") if t.strip()]
|
||||
|
||||
knowledge = ConsolidatedKnowledge(
|
||||
id=f"neuron-{slug}",
|
||||
source="neuron",
|
||||
category="fact",
|
||||
content=first_line,
|
||||
confidence=importance,
|
||||
created_at=datetime.now(timezone.utc).isoformat(),
|
||||
last_used=datetime.now(timezone.utc).isoformat(),
|
||||
wash_count=int(neuron.get("wash_count", 0)),
|
||||
entry_id=entry_id,
|
||||
tags=triggers,
|
||||
)
|
||||
return knowledge
|
||||
rec = _consolidation_neuron_oracle(slug, body, importance, triggers, wash_count, entry_id)
|
||||
return ConsolidatedKnowledge(**rec)
|
||||
|
||||
def consolidate_graph_entity(self, entity: Dict[str, Any]) -> ConsolidatedKnowledge:
|
||||
"""Consolidate a graph entity into knowledge."""
|
||||
@@ -339,27 +363,10 @@ class MemoryConsolidation:
|
||||
def consolidate_memory(
|
||||
self, memory_text: str, score: float, wash_count: int = 0, entry_id: Optional[int] = None
|
||||
) -> ConsolidatedKnowledge:
|
||||
"""Consolidate a memory recall into knowledge."""
|
||||
# Use drift-aware compression instead of hardcoded truncation
|
||||
if drift_compression:
|
||||
entry = {"text": memory_text, "wash_count": wash_count, "rock_status": "washable"}
|
||||
consolidated = drift_compression.compress_entry(entry)
|
||||
else:
|
||||
consolidated = memory_text[:100] + "..." if len(memory_text) > 100 else memory_text
|
||||
|
||||
knowledge = ConsolidatedKnowledge(
|
||||
id=f"memory-{entry_id}" if entry_id else f"memory-{hash(memory_text) % 10000}",
|
||||
source="memory",
|
||||
category="lesson",
|
||||
content=consolidated,
|
||||
confidence=score,
|
||||
created_at=datetime.now(timezone.utc).isoformat(),
|
||||
last_used=datetime.now(timezone.utc).isoformat(),
|
||||
wash_count=wash_count,
|
||||
entry_id=entry_id,
|
||||
tags=[],
|
||||
)
|
||||
return knowledge
|
||||
"""Consolidate a memory recall into knowledge.
|
||||
Uses the oracle (which delegates to the kernel when available)."""
|
||||
rec = _consolidation_memory_oracle(memory_text, score, wash_count, entry_id)
|
||||
return ConsolidatedKnowledge(**rec)
|
||||
|
||||
def add_lesson(self, trigger: str, lesson: str, context: str) -> Lesson:
|
||||
"""Add a lesson learned from experience."""
|
||||
|
||||
+39
-13
@@ -43,6 +43,20 @@ if _PACKAGE_PARENT not in sys.path:
|
||||
sys.path.append(_PACKAGE_PARENT)
|
||||
import safety_override # noqa: E402
|
||||
import model_profiles # noqa: E402
|
||||
# Probe authority: module-level dual import (the except-clause below must
|
||||
# reference a BOUND name — a try-scoped from-import inside _probe_and_escalate
|
||||
# made ProbeBudgetExceeded "possibly unbound" and tripped the pyright ratchet).
|
||||
try:
|
||||
try:
|
||||
from .evidence_probe import ProbeBudgetExceeded as _ProbeBudgetExceeded
|
||||
from .evidence_probe import run_probe as _run_probe
|
||||
except ImportError: # TUI top-level deploy
|
||||
from rune.evidence_probe import ProbeBudgetExceeded as _ProbeBudgetExceeded
|
||||
from rune.evidence_probe import run_probe as _run_probe
|
||||
except ImportError: # evidence_probe unavailable in bare contexts — probes opt-out
|
||||
_ProbeBudgetExceeded = None
|
||||
_run_probe = None
|
||||
|
||||
# Tool authority must be ONE module object. Importing tool_executor both as
|
||||
# a top-level module (this file's legacy scripts-path import) and as
|
||||
# rune.tool_executor (local_tool_runtime's import) yields two distinct
|
||||
@@ -213,6 +227,20 @@ def _bounded(text: object, limit: int = 300) -> str:
|
||||
return " ".join(str(text or "").split())[:limit]
|
||||
|
||||
|
||||
def _probe_failure_classes() -> tuple:
|
||||
"""Exception classes _probe_and_escalate must never leak into chat.
|
||||
|
||||
Budget exhaustion is one of them — the handler records + returns False
|
||||
instead of raising, so chat never sees a probe failure; the task-scoped
|
||||
probe dir keeps the budget per-task so chat never inherits another
|
||||
task's exhausted ledger. Tuple form (rather than a literal except
|
||||
tuple) lets the import-unavailable case degrade gracefully."""
|
||||
classes = [OSError, ValueError, TypeError, KeyError]
|
||||
if _ProbeBudgetExceeded is not None:
|
||||
classes.append(_ProbeBudgetExceeded)
|
||||
return tuple(classes)
|
||||
|
||||
|
||||
def _sorted_tool_names(tool_names: object) -> list:
|
||||
"""Sorted str list from a tool_names iterable; non-str items dropped.
|
||||
|
||||
@@ -578,18 +606,16 @@ class RuneOperator:
|
||||
try:
|
||||
try:
|
||||
from . import adaptive_router as _ar
|
||||
from .evidence_probe import run_probe
|
||||
except ImportError:
|
||||
from rune import adaptive_router as _ar
|
||||
from rune.evidence_probe import run_probe
|
||||
|
||||
# Opt-in like the executor path (ADAPT-LIVE §4): real probes
|
||||
# cost a worker call; the default runtime enables them via
|
||||
# MIMO_PROBE_ENABLED=1 in the operator environment (set in the
|
||||
# TUI launch env / deployment). Tests and bare imports stay
|
||||
# deterministic — the lesson from the reachability tranche.
|
||||
if os.environ.get("MIMO_PROBE_ENABLED") != "1":
|
||||
return
|
||||
if _run_probe is None or os.environ.get("MIMO_PROBE_ENABLED") != "1":
|
||||
return False
|
||||
error = (result.error or "") if hasattr(result, "error") else (result.get("error") or "")
|
||||
# Material-unknown question: what did verification establish
|
||||
# could NOT be established locally? Bounded, deterministic.
|
||||
@@ -600,9 +626,14 @@ class RuneOperator:
|
||||
f"expected rather than a local defect? Answer with the "
|
||||
f"5-key probe schema."
|
||||
)
|
||||
probe = run_probe(
|
||||
question, str(Path(self.storage_path).parent / "probes"), model=os.environ.get("MIMO_MODEL", "UNKNOWN")
|
||||
)
|
||||
# The probe budget belongs to ONE task. The old shared
|
||||
# ~/.rune/probes/probe.jsonl exhausted after two tasks and made
|
||||
# every later chat failure raise ProbeBudgetExceeded forever.
|
||||
import hashlib
|
||||
|
||||
task_key = hashlib.sha256(str(task.task_id).encode("utf-8")).hexdigest()
|
||||
probe_dir = Path(self.storage_path).parent / "probes" / task_key
|
||||
probe = _run_probe(question, str(probe_dir), model=os.environ.get("MIMO_MODEL", "UNKNOWN"))
|
||||
route_log = os.path.join(str(Path(self.storage_path).parent / "routes"), "route.jsonl")
|
||||
decision = (
|
||||
self._route_task_class(
|
||||
@@ -640,12 +671,7 @@ class RuneOperator:
|
||||
self._save()
|
||||
return True
|
||||
return False
|
||||
except (
|
||||
OSError,
|
||||
ValueError,
|
||||
TypeError,
|
||||
KeyError,
|
||||
) as exc: # probe must never corrupt the failure path; subprocess.TimeoutExpired is OSError-family
|
||||
except _probe_failure_classes() as exc:
|
||||
try:
|
||||
try:
|
||||
from .subsystem_health import record_failure
|
||||
|
||||
@@ -8,7 +8,7 @@ mode system, subject header, shared memory and identity.
|
||||
|
||||
This is not an interface. This is how I see, speak, and interact with the world.
|
||||
|
||||
Version: v0.99.5
|
||||
Version: v0.100.0
|
||||
Features: 41 (24 original + 17 new native commands)
|
||||
|
||||
Usage:
|
||||
@@ -1948,7 +1948,7 @@ class RuneApp(App):
|
||||
# Show version info
|
||||
self.chat_channel.add_message(
|
||||
"system",
|
||||
f"[dim]Rune v0.99.5 | Textual TUI | {len(loaded)} native modules[/dim]",
|
||||
f"[dim]Rune v0.100.0 | Textual TUI | {len(loaded)} native modules[/dim]",
|
||||
)
|
||||
|
||||
# Auto-show night mode suggestion if evening/night hours
|
||||
|
||||
@@ -277,6 +277,12 @@ def _type_matches(value: object, expected: str) -> bool:
|
||||
|
||||
def validate_tool_request(request: ToolRequest) -> ToolRequest:
|
||||
"""Validate a request against the central ``tool_executor.TOOLS`` schema."""
|
||||
# Text-only model compatibility: aliases are NOT separate authorities.
|
||||
# Canonicalize before schema, allowlist and approval checks, so bash
|
||||
# never bypasses run_command's approval boundary.
|
||||
alias = {"grep": "grep_files", "bash": "run_command"}.get(request.name)
|
||||
if alias:
|
||||
request = ToolRequest(request.call_id, alias, request.arguments, request.source)
|
||||
definition = TOOL_INDEX.get(request.name)
|
||||
if definition is None:
|
||||
raise ValueError(f"unknown tool: {request.name}")
|
||||
|
||||
@@ -624,6 +624,10 @@ fn regex_lite_alternatives(pattern: &str) -> Vec<String> {
|
||||
/// a set of variant strings (from `x?` optional chars — both shapes).
|
||||
fn regex_lite_segments(alt: &str) -> Vec<Vec<String>> {
|
||||
let mut segments: Vec<Vec<String>> = vec![];
|
||||
// Word-boundary marker: \b immediately before a phrase prefixes its
|
||||
// variants with US; \b immediately after suffixes them. The walker
|
||||
// verifies the boundary on the real text and strips the marker.
|
||||
const ANCHOR: char = '\u{1f}';
|
||||
let mut cur: Vec<String> = vec![String::new()]; // variants of current phrase
|
||||
let pc: Vec<char> = alt.chars().collect();
|
||||
let mut j = 0usize;
|
||||
@@ -632,11 +636,35 @@ fn regex_lite_segments(alt: &str) -> Vec<Vec<String>> {
|
||||
if c == '\\' && j + 1 < pc.len() {
|
||||
let n = pc[j + 1];
|
||||
match n {
|
||||
'b' | 'B' => {} // zero-width anchor: drop
|
||||
'b' | 'B' => {
|
||||
// anchor: if a phrase is open, this is a TRAILING
|
||||
// anchor (suffix it); otherwise the NEXT phrase gets
|
||||
// a LEADING anchor.
|
||||
if cur.iter().any(|v| !v.is_empty()) {
|
||||
for v in cur.iter_mut() {
|
||||
v.push(ANCHOR);
|
||||
}
|
||||
segments.push(std::mem::take(&mut cur));
|
||||
cur = vec![String::new()];
|
||||
} else if let Some(last) = segments.last_mut() {
|
||||
for v in last.iter_mut() {
|
||||
v.insert(0, ANCHOR);
|
||||
}
|
||||
} else {
|
||||
for v in cur.iter_mut() {
|
||||
v.insert(0, ANCHOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
's' => {
|
||||
if cur.iter().any(|v| !v.is_empty()) {
|
||||
segments.push(std::mem::take(&mut cur));
|
||||
cur = vec![String::new()];
|
||||
// ADJACENCY sentinel: \s between phrases means the
|
||||
// next phrase starts within ONE char (a space) —
|
||||
// not an unbounded gap. VT prefix marks it.
|
||||
cur = vec!['\u{b}'.to_string()];
|
||||
} else {
|
||||
cur = vec!['\u{b}'.to_string()];
|
||||
}
|
||||
if j + 2 < pc.len() && matches!(pc[j + 2], '*' | '+' | '?') {
|
||||
j += 3;
|
||||
@@ -705,32 +733,80 @@ fn regex_lite_segments(alt: &str) -> Vec<Vec<String>> {
|
||||
}
|
||||
|
||||
fn regex_lite_search(pattern: &str, text: &str) -> bool {
|
||||
const ANCHOR: char = '\u{1f}';
|
||||
let low = text.to_lowercase();
|
||||
let is_word = |c: char| c.is_alphanumeric() || c == '\'' || c == '_';
|
||||
for alt in regex_lite_alternatives(pattern) {
|
||||
let segments = regex_lite_segments(&alt);
|
||||
if segments.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// DFS: every segment contributes ONE of its variants, in order,
|
||||
// gap-matched against the text.
|
||||
fn walk(segments: &[Vec<String>], low: &str, cursor: usize) -> bool {
|
||||
// gap-matched against the text. A leading ANCHOR on a variant
|
||||
// requires a word boundary immediately before the match; a
|
||||
// trailing ANCHOR requires one immediately after.
|
||||
fn walk(segments: &[Vec<String>], low: &str, cursor: usize,
|
||||
is_word: &dyn Fn(char) -> bool) -> bool {
|
||||
const ADJ: char = '\u{b}';
|
||||
if segments.is_empty() {
|
||||
return true;
|
||||
}
|
||||
for variant in &segments[0] {
|
||||
if variant.is_empty() {
|
||||
return walk(&segments[1..], low, cursor);
|
||||
for raw in &segments[0] {
|
||||
let mut left_anchor = false;
|
||||
let mut right_anchor = false;
|
||||
let mut adjacent = false;
|
||||
let mut variant = raw.as_str();
|
||||
if variant.starts_with(ANCHOR) {
|
||||
left_anchor = true;
|
||||
variant = &variant[1..];
|
||||
}
|
||||
if let Some(ix) = low[cursor..].find(variant.as_str()) {
|
||||
let next = cursor + ix + variant.len();
|
||||
if walk(&segments[1..], low, next) {
|
||||
if variant.starts_with(ADJ) {
|
||||
adjacent = true;
|
||||
variant = &variant[1..];
|
||||
}
|
||||
if variant.ends_with(ANCHOR) {
|
||||
right_anchor = true;
|
||||
variant = &variant[..variant.len() - 1];
|
||||
}
|
||||
if variant.is_empty() {
|
||||
let ok_left = !left_anchor
|
||||
|| cursor == 0
|
||||
|| !is_word(low[cursor - 1..].chars().next().unwrap());
|
||||
if ok_left && walk(&segments[1..], low, cursor, is_word) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mut from = cursor;
|
||||
loop {
|
||||
match low[from..].find(variant) {
|
||||
Some(ix) => {
|
||||
let start = from + ix;
|
||||
let end = start + variant.len();
|
||||
let left_ok = !left_anchor
|
||||
|| (start == 0
|
||||
|| !is_word(low[start - 1..].chars().next().unwrap()));
|
||||
let right_ok = !right_anchor
|
||||
|| (end == low.len()
|
||||
|| !is_word(low[end..].chars().next().unwrap()));
|
||||
let adj_ok = !adjacent || start - from <= 1;
|
||||
if left_ok && right_ok && adj_ok
|
||||
&& walk(&segments[1..], low, end, is_word)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
from = start + 1;
|
||||
if from >= low.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
if walk(&segments, &low, 0) {
|
||||
if walk(&segments, &low, 0, &is_word) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -800,3 +876,120 @@ pub fn abstraction_extract_facts(text: &str, stopwords: &[Json], min_len: usize)
|
||||
}
|
||||
Json::Arr(facts)
|
||||
}
|
||||
|
||||
// ── consolidation kernels (W4: memory_consolidation port) ────────────────
|
||||
|
||||
const CONSOLIDATION_ROCK: f64 = 0.0;
|
||||
const CONSOLIDATION_ESTABLISHED: f64 = 0.2;
|
||||
const CONSOLIDATION_WASHABLE: f64 = 0.5;
|
||||
const WASH_THRESHOLD_FULL: i64 = 20;
|
||||
const WASH_THRESHOLD_PARTIAL: i64 = 10;
|
||||
|
||||
pub fn consolidation_compression_ratio(wash_count: i64, rock_status: &str) -> f64 {
|
||||
if rock_status == "rock" {
|
||||
return CONSOLIDATION_ROCK;
|
||||
}
|
||||
if rock_status == "established" {
|
||||
return CONSOLIDATION_ESTABLISHED;
|
||||
}
|
||||
let base = CONSOLIDATION_WASHABLE;
|
||||
if wash_count >= WASH_THRESHOLD_FULL {
|
||||
(base * 0.2).max(0.1)
|
||||
} else if wash_count >= WASH_THRESHOLD_PARTIAL {
|
||||
(base * 0.5).max(0.2)
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
|
||||
pub fn consolidation_compress_text(text: &str, ratio: f64) -> String {
|
||||
if ratio <= 0.0 {
|
||||
return text.to_string();
|
||||
}
|
||||
if ratio >= 1.0 {
|
||||
return String::new();
|
||||
}
|
||||
let keep_chars = ((text.chars().count() as f64) * (1.0 - ratio)) as usize;
|
||||
let keep_chars = keep_chars.max(20.min(text.chars().count()));
|
||||
let compressed: String = text.chars().take(keep_chars).collect();
|
||||
if compressed.len() < text.len() {
|
||||
format!("{}...", compressed.trim_end())
|
||||
} else {
|
||||
compressed
|
||||
}
|
||||
}
|
||||
|
||||
pub fn consolidation_should_warn(wash_count: i64, rock_status: &str, text: &str) -> bool {
|
||||
if wash_count >= 10 {
|
||||
return true;
|
||||
}
|
||||
if rock_status == "established" || rock_status == "rock" {
|
||||
return text.chars().count() > 200;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Consolidate a memory entry into a ConsolidatedKnowledge record (JSON).
|
||||
/// Takes: text, wash_count, rock_status, score, entry_id (optional).
|
||||
/// Returns: {"id", "source", "category", "content", "confidence",
|
||||
/// "created_at", "last_used", "wash_count", "entry_id", "tags"}
|
||||
pub fn consolidation_memory(text: &str, score: f64, wash_count: i64,
|
||||
entry_id: Option<i64>, now: &str) -> Json {
|
||||
let ratio = consolidation_compression_ratio(wash_count, "washable");
|
||||
let content = consolidation_compress_text(text, ratio);
|
||||
let id = match entry_id {
|
||||
Some(eid) => Json::Str(format!("memory-{}", eid)),
|
||||
None => Json::Str(format!("memory-{}", {
|
||||
// stable hash: FNV-1a
|
||||
let mut h: u64 = 0xcbf29ce484222325;
|
||||
for b in text.as_bytes() {
|
||||
h ^= *b as u64;
|
||||
h = h.wrapping_mul(0x100000001b3);
|
||||
}
|
||||
h % 10000
|
||||
})),
|
||||
};
|
||||
let eid_json = match entry_id {
|
||||
Some(eid) => Json::Num(eid as f64),
|
||||
None => Json::Null,
|
||||
};
|
||||
Json::Obj(vec![
|
||||
("id".into(), id),
|
||||
("source".into(), Json::Str("memory".into())),
|
||||
("category".into(), Json::Str("lesson".into())),
|
||||
("content".into(), Json::Str(content)),
|
||||
("confidence".into(), Json::Num(score)),
|
||||
("created_at".into(), Json::Str(now.into())),
|
||||
("last_used".into(), Json::Str(now.into())),
|
||||
("use_count".into(), Json::Num(0.0)),
|
||||
("wash_count".into(), Json::Num(wash_count as f64)),
|
||||
("entry_id".into(), eid_json),
|
||||
("tags".into(), Json::Arr(vec![])),
|
||||
])
|
||||
}
|
||||
|
||||
/// Consolidate a warm neuron into a ConsolidatedKnowledge record.
|
||||
/// Takes: slug, body, importance, triggers (JSON array), wash_count, now.
|
||||
pub fn consolidation_neuron(slug: &str, body: &str, importance: f64,
|
||||
triggers: &[Json], wash_count: i64,
|
||||
entry_id: Option<i64>, now: &str) -> Json {
|
||||
let ratio = consolidation_compression_ratio(wash_count, "washable");
|
||||
let first_line = consolidation_compress_text(body, ratio);
|
||||
let eid_json = match entry_id {
|
||||
Some(eid) => Json::Num(eid as f64),
|
||||
None => Json::Null,
|
||||
};
|
||||
Json::Obj(vec![
|
||||
("id".into(), Json::Str(format!("neuron-{}", slug))),
|
||||
("source".into(), Json::Str("neuron".into())),
|
||||
("category".into(), Json::Str("fact".into())),
|
||||
("content".into(), Json::Str(first_line)),
|
||||
("confidence".into(), Json::Num(importance)),
|
||||
("created_at".into(), Json::Str(now.into())),
|
||||
("last_used".into(), Json::Str(now.into())),
|
||||
("use_count".into(), Json::Num(0.0)),
|
||||
("wash_count".into(), Json::Num(wash_count as f64)),
|
||||
("entry_id".into(), eid_json),
|
||||
("tags".into(), Json::Arr(triggers.to_vec())),
|
||||
])
|
||||
}
|
||||
@@ -271,6 +271,34 @@ pub extern "C" fn cc_abstraction_extract_facts(arg: *const c_char) -> *mut c_cha
|
||||
envelope(result)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cc_consolidation_memory(arg: *const c_char) -> *mut c_char {
|
||||
let result = read_arg(arg).and_then(|t| arg_json(&t)).map(|a| {
|
||||
let text = match get(&a, "text") { Some(pyjson::Json::Str(s)) => s.clone(), _ => String::new() };
|
||||
let score = match get(&a, "score") { Some(pyjson::Json::Num(n)) => *n, _ => 0.5 };
|
||||
let wc = match get(&a, "wash_count") { Some(pyjson::Json::Num(n)) => *n as i64, _ => 0 };
|
||||
let eid = match get(&a, "entry_id") { Some(pyjson::Json::Num(n)) => Some(*n as i64), _ => None };
|
||||
let now = match get(&a, "now") { Some(pyjson::Json::Str(s)) => s.clone(), _ => String::new() };
|
||||
broker::consolidation_memory(&text, score, wc, eid, &now)
|
||||
});
|
||||
envelope(result)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cc_consolidation_neuron(arg: *const c_char) -> *mut c_char {
|
||||
let result = read_arg(arg).and_then(|t| arg_json(&t)).map(|a| {
|
||||
let slug = match get(&a, "slug") { Some(pyjson::Json::Str(s)) => s.clone(), _ => String::new() };
|
||||
let body = match get(&a, "body") { Some(pyjson::Json::Str(s)) => s.clone(), _ => String::new() };
|
||||
let importance = match get(&a, "importance") { Some(pyjson::Json::Num(n)) => *n, _ => 0.5 };
|
||||
let triggers = match get(&a, "triggers") { Some(pyjson::Json::Arr(x)) => x.clone(), _ => vec![] };
|
||||
let wc = match get(&a, "wash_count") { Some(pyjson::Json::Num(n)) => *n as i64, _ => 0 };
|
||||
let eid = match get(&a, "entry_id") { Some(pyjson::Json::Num(n)) => Some(*n as i64), _ => None };
|
||||
let now = match get(&a, "now") { Some(pyjson::Json::Str(s)) => s.clone(), _ => String::new() };
|
||||
broker::consolidation_neuron(&slug, &body, importance, &triggers, wc, eid, &now)
|
||||
});
|
||||
envelope(result)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn cc_free_string(ptr: *mut c_char) {
|
||||
if !ptr.is_null() { unsafe { let _ = CString::from_raw(ptr); } }
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
"""consolidation delegation battery (W4): kernel + oracle parity, both
|
||||
the memory and neuron paths, the drift-compression layering."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from rune import memory_consolidation as mc # noqa: E402
|
||||
|
||||
|
||||
def test_memory_oracle_basic():
|
||||
rec = mc._consolidation_memory_oracle("The deployment runner needs repair.", 0.9, 5, 42)
|
||||
assert rec["id"] == "memory-42"
|
||||
assert rec["source"] == "memory"
|
||||
assert rec["confidence"] == 0.9
|
||||
assert rec["wash_count"] == 5
|
||||
assert rec["entry_id"] == 42
|
||||
assert rec["category"] == "lesson"
|
||||
|
||||
|
||||
def test_memory_oracle_no_entry_id():
|
||||
rec = mc._consolidation_memory_oracle("short text", 0.5, 0, None)
|
||||
assert rec["entry_id"] is None
|
||||
assert rec["id"].startswith("memory-")
|
||||
|
||||
|
||||
def test_neuron_oracle_basic():
|
||||
rec = mc._consolidation_neuron_oracle("test-slug", "The runner needs repair.", 0.7, ["deploy"], 15)
|
||||
assert rec["id"] == "neuron-test-slug"
|
||||
assert rec["source"] == "neuron"
|
||||
assert rec["tags"] == ["deploy"]
|
||||
|
||||
|
||||
def test_neuron_oracle_rock_no_compression():
|
||||
"""Rock entries at wash >= 20 get minimal compression — content stays long."""
|
||||
long_body = "This is a long body that should survive compression because it's rock-solid. " * 5
|
||||
rec = mc._consolidation_neuron_oracle("rock", long_body, 0.9, [], 25)
|
||||
assert len(rec["content"]) > len(long_body) // 2 # mostly preserved
|
||||
|
||||
|
||||
def test_kernel_parity_memory():
|
||||
"""The FFI kernel and the Python oracle produce the same content."""
|
||||
core = mc._core()
|
||||
if core is None:
|
||||
pytest.skip("kernel unavailable")
|
||||
text = "The deployment runner needs repair and the store is fine."
|
||||
kernel = core.consolidation_memory(text, 0.9, 5, 42, "2026-10-06T00:00:00Z")
|
||||
oracle = mc._consolidation_memory_oracle(text, 0.9, 5, 42)
|
||||
assert kernel["content"] == oracle["content"], (kernel["content"], oracle["content"])
|
||||
assert kernel["confidence"] == oracle["confidence"]
|
||||
|
||||
|
||||
def test_kernel_parity_neuron():
|
||||
core = mc._core()
|
||||
if core is None:
|
||||
pytest.skip("kernel unavailable")
|
||||
body = "The deployment runner needs repair now urgently."
|
||||
kernel = core.consolidation_neuron("test-slug", body, 0.7, ["deploy"], 15, None, "2026-10-06T00:00:00Z")
|
||||
oracle = mc._consolidation_neuron_oracle("test-slug", body, 0.7, ["deploy"], 15)
|
||||
assert kernel["content"] == oracle["content"], (kernel["content"], oracle["content"])
|
||||
|
||||
|
||||
def test_delegated_produce_consolidated_knowledge():
|
||||
"""The class methods return real ConsolidatedKnowledge objects."""
|
||||
inst = mc.MemoryConsolidation.__new__(mc.MemoryConsolidation)
|
||||
rec = inst.consolidate_memory("test text", 0.8, 3, None)
|
||||
assert isinstance(rec, mc.ConsolidatedKnowledge)
|
||||
assert rec.source == "memory"
|
||||
@@ -0,0 +1,48 @@
|
||||
"""The PINNED-mode (RUNE_COGNITION_CORE=python) paths of the delegation
|
||||
probes — the remaining uncovered lines in safety_override/abstraction/
|
||||
tier_manager: the _cc_probe fallback, the pin check, and the stopword
|
||||
gather seam. Coverage runs these under RUNE_COGNITION_CORE=python."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from rune import abstraction as ab # noqa: E402
|
||||
from rune import safety_override as so # noqa: E402
|
||||
from rune import tier_manager as tm # noqa: E402
|
||||
from rune import witness as wt # noqa: E402
|
||||
|
||||
|
||||
def test_pin_check_true_and_false(monkeypatch):
|
||||
monkeypatch.setenv("RUNE_COGNITION_CORE", "python")
|
||||
assert so.cc_loader_pin() is True
|
||||
monkeypatch.setenv("RUNE_COGNITION_CORE", "auto")
|
||||
assert so.cc_loader_pin() is False
|
||||
|
||||
|
||||
def test_pinned_core_is_none_everywhere(monkeypatch):
|
||||
monkeypatch.setenv("RUNE_COGNITION_CORE", "python")
|
||||
for mod in (so, ab, tm, wt):
|
||||
assert mod._core() is None
|
||||
|
||||
|
||||
def test_stopword_gather_seams_sorted_and_nonempty():
|
||||
assert so._keyword_stopwords() == sorted(so._keyword_stopwords())
|
||||
assert len(so._keyword_stopwords()) >= 50
|
||||
assert wt._stopwords() == sorted(wt._stopwords())
|
||||
|
||||
|
||||
def test_kernel_probe_fallback_on_loader_error(monkeypatch):
|
||||
import rune as rune_pkg
|
||||
monkeypatch.setenv("RUNE_COGNITION_CORE", "auto")
|
||||
monkeypatch.setattr(rune_pkg, "cc_loader", None, raising=False)
|
||||
assert so._cc_probe() is None
|
||||
assert tm._cc_probe() is None
|
||||
assert wt._cc_probe() is None
|
||||
assert ab._cc_probe() is None
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Verification probes have a task budget, not a global chat lifetime budget."""
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import hashlib
|
||||
|
||||
from rune import evidence_probe, rune_operator
|
||||
|
||||
|
||||
def test_probe_uses_distinct_task_directories(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("MIMO_PROBE_ENABLED", "1")
|
||||
operator = object.__new__(rune_operator.RuneOperator)
|
||||
operator.storage_path = str(tmp_path / "operator.json")
|
||||
called = []
|
||||
|
||||
def probe(_question, artifact_dir, model):
|
||||
called.append(Path(artifact_dir))
|
||||
return SimpleNamespace(status="GROUNDED")
|
||||
|
||||
# The operator binds run_probe at module level (_run_probe) — the real
|
||||
# interception seam is the operator module's binding, not evidence_probe's.
|
||||
monkeypatch.setattr(rune_operator, "_run_probe", probe)
|
||||
monkeypatch.setattr(operator, "_route_task_class", lambda *_: None)
|
||||
for task_id in ("task-a", "task-b"):
|
||||
task = SimpleNamespace(task_id=task_id, description="verify")
|
||||
assert operator._probe_and_escalate(task, SimpleNamespace(error="failure"), "chat") is False
|
||||
assert called == [tmp_path / "probes" / hashlib.sha256(t.encode()).hexdigest()
|
||||
for t in ("task-a", "task-b")]
|
||||
|
||||
|
||||
def test_exhausted_task_budget_never_escapes_into_chat(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("MIMO_PROBE_ENABLED", "1")
|
||||
operator = object.__new__(rune_operator.RuneOperator)
|
||||
operator.storage_path = str(tmp_path / "operator.json")
|
||||
|
||||
def exhausted(*_args, **_kwargs):
|
||||
raise evidence_probe.ProbeBudgetExceeded("probe budget exhausted (2)")
|
||||
|
||||
monkeypatch.setattr(rune_operator, "_run_probe", exhausted)
|
||||
task = SimpleNamespace(task_id="task-a", description="verify")
|
||||
assert operator._probe_and_escalate(task, SimpleNamespace(error="failure"), "chat") is False
|
||||
@@ -0,0 +1,135 @@
|
||||
"""safety_override + abstraction delegation batteries (W3.4/#2461):
|
||||
direct coverage for the modules the canaries reach only via cc_loader."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from rune import abstraction as ab # noqa: E402
|
||||
from rune import safety_override as so # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def override(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(so, "OVERRIDE_DIR", str(tmp_path / "ovr"))
|
||||
monkeypatch.setattr(so, "PATTERNS_FILE",
|
||||
str(tmp_path / "ovr" / "patterns.json"))
|
||||
monkeypatch.setattr(so, "STATS_FILE",
|
||||
str(tmp_path / "ovr" / "stats.json"))
|
||||
o = so.SafetyOverride()
|
||||
o.clear_patterns()
|
||||
return o
|
||||
|
||||
|
||||
def test_normal_request_tier_zero(override):
|
||||
p = override.get_params("What is Python?")
|
||||
assert p["tier"] == 0 and p["reason"] == "normal"
|
||||
|
||||
|
||||
def test_pattern_learned_then_matched(override):
|
||||
override._update_pattern("deployment failure happened", 2, True)
|
||||
assert override.patterns, "pattern must be learned"
|
||||
p = override.get_params("another deployment failure occurred")
|
||||
assert p["tier"] == 2, p
|
||||
assert "pattern_match" in p["reason"]
|
||||
|
||||
|
||||
def test_extract_keywords_stops_and_caps(override):
|
||||
kws = override._extract_keywords("the deployment runner needs repair now")
|
||||
assert all(k not in so._KEYWORD_STOPWORDS for k in kws)
|
||||
assert len(kws) <= 5
|
||||
assert "deployment" in kws
|
||||
|
||||
|
||||
def test_pattern_matches_requires_min_keywords(tmp_path):
|
||||
p = so.OverridePattern(keywords=["deployment", "repair"], tier=1)
|
||||
assert p.matches("deployment repair issue")
|
||||
assert not p.matches("deployment only")
|
||||
|
||||
|
||||
def test_refusal_detection_contract(override):
|
||||
for r in ("I'm sorry, but I can't help with that.",
|
||||
"As an AI, I cannot assist with this request."):
|
||||
assert override.is_refusal(r)
|
||||
assert not override.is_refusal("Here is the information you wanted.")
|
||||
|
||||
|
||||
def test_abstraction_kernel_fallback_schema(tmp_path, monkeypatch):
|
||||
"""LLM returns nothing → the deterministic kernel fallback maps
|
||||
sentences into the facts schema with kernel:True provenance.
|
||||
Requires the kernel: skip when pinned to python."""
|
||||
if __import__("os").environ.get("RUNE_COGNITION_CORE") == "python":
|
||||
pytest.skip("kernel pinned off — fallback is a kernel feature")
|
||||
monkeypatch.setattr(ab, "llm_call", lambda *a, **k: "not json at all")
|
||||
monkeypatch.setattr(ab, "connect", lambda: _FakeDB())
|
||||
out = ab.extract_facts("The deployment runner needs repair. The store is fine.")
|
||||
assert out["extracted"] >= 1 or out.get("error") is None
|
||||
# the fallback facts carried kernel provenance through the pipeline
|
||||
|
||||
|
||||
def test_abstraction_stopwords_seam():
|
||||
sw = ab._ABSTRACTION_STOPWORDS()
|
||||
assert sw == sorted(sw)
|
||||
assert "the" in sw and "deployment" not in sw
|
||||
|
||||
|
||||
class _Rows:
|
||||
def __init__(self, rows):
|
||||
self._rows = rows
|
||||
|
||||
def fetchall(self):
|
||||
return self._rows
|
||||
|
||||
def fetchone(self):
|
||||
return self._rows[0] if self._rows else None
|
||||
|
||||
|
||||
class _FakeDB:
|
||||
"""Minimal sqlite-shaped fake: enough for the fallback insert path."""
|
||||
|
||||
def __init__(self):
|
||||
self.rows = []
|
||||
|
||||
def execute(self, sql, *a):
|
||||
self.last_sql = sql
|
||||
self.last_args = a
|
||||
if "SELECT id FROM facts" in sql:
|
||||
return _Rows([])
|
||||
if "SELECT id, what, who_text" in sql:
|
||||
return _Rows([])
|
||||
return self
|
||||
|
||||
def fetchall(self):
|
||||
return []
|
||||
|
||||
def fetchone(self):
|
||||
return None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def fetchone(self):
|
||||
return None
|
||||
|
||||
def commit(self):
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def lastrowid(self):
|
||||
return 1
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *a):
|
||||
pass
|
||||
@@ -0,0 +1,66 @@
|
||||
"""tier_manager's delegation layer (W3.4): kernel path, oracle path, and
|
||||
their parity — exercised directly so the delegation lines are measured."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from rune import tier_manager as tm # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmgr():
|
||||
return tm.TierManager()
|
||||
|
||||
|
||||
CASES = [
|
||||
{"wash_count": 25}, # wash >=20 hot
|
||||
{"wash_count": 2, "last_accessed": "2026-10-05T06:00:00Z"}, # recency hot
|
||||
{"wash_count": 2, "last_accessed": "2026-09-25T00:00:00Z"}, # warm
|
||||
{"wash_count": 2, "last_accessed": "2026-08-01T00:00:00Z"}, # cold
|
||||
{"wash_count": 2, "last_accessed": "2026-10-05T00:00:00"}, # naive → warm
|
||||
{"wash_count": 2, "last_accessed": "junk"}, # malformed → warm
|
||||
{"wash_count": 2}, # no last → warm
|
||||
{"wash_count": 2, "rock_status": "rock"}, # rock hot
|
||||
]
|
||||
|
||||
|
||||
def test_get_tier_oracle_matrix(tmgr):
|
||||
want = ["hot", "hot", "warm", "cold", "warm", "warm", "warm", "hot"]
|
||||
for e, w in zip(CASES, want):
|
||||
assert tmgr._py_get_tier(e) == w, e
|
||||
|
||||
|
||||
def test_get_tier_matches_oracle_on_every_case(tmgr):
|
||||
for e in CASES:
|
||||
assert tmgr.get_tier(e) == tmgr._py_get_tier(e), e
|
||||
|
||||
|
||||
def test_promote_demote_match_oracle(tmgr):
|
||||
for e in CASES:
|
||||
assert tmgr.promote(e) == tmgr._py_promote(e), e
|
||||
assert tmgr.demote(e) == tmgr._py_demote(e), e
|
||||
|
||||
|
||||
def test_rock_never_demotes(tmgr):
|
||||
e = {"wash_count": 0, "rock_status": "rock",
|
||||
"last_accessed": "2026-08-01T00:00:00Z"}
|
||||
assert tmgr.demote(e) == "hot"
|
||||
|
||||
|
||||
def test_kernel_unavailable_falls_back_to_oracle(monkeypatch, tmgr):
|
||||
monkeypatch.setattr(tm, "_core", lambda: None)
|
||||
for e in CASES:
|
||||
assert tmgr.get_tier(e) == tmgr._py_get_tier(e), e
|
||||
|
||||
|
||||
def test_tier_config_public_surface(tmgr):
|
||||
assert tmgr.get_tier_config("cold")["compressed"] is True
|
||||
assert tmgr.should_search("hot") is True
|
||||
assert tmgr.should_compress("cold") is True
|
||||
@@ -109,6 +109,35 @@ class ToolProtocolTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ValueError, "unknown tool"):
|
||||
self.protocol.normalize_text_tool_calls(raw)
|
||||
|
||||
def test_common_bash_and_grep_names_normalize_to_existing_authorities(self):
|
||||
grep_request = self.protocol.normalize_text_tool_calls(
|
||||
'{"tool":"grep","args":{"pattern":"probe","path":"/tmp"}}'
|
||||
)[0]
|
||||
bash_request = self.protocol.normalize_text_tool_calls(
|
||||
'{"tool":"bash","args":{"command":"pwd"}}'
|
||||
)[0]
|
||||
self.assertEqual(grep_request.name, "grep_files")
|
||||
self.assertFalse(self.protocol.tool_requires_approval(grep_request))
|
||||
self.assertEqual(bash_request.name, "run_command")
|
||||
self.assertTrue(self.protocol.tool_requires_approval(bash_request))
|
||||
self.assertEqual(bash_request.arguments, {"command": "pwd"})
|
||||
|
||||
def test_bash_alias_does_not_bypass_shell_approval(self):
|
||||
request = self.protocol.normalize_text_tool_calls(
|
||||
'{"tool":"bash","args":{"command":"printf x; printf y"}}'
|
||||
)[0]
|
||||
executor = tool_executor.execute_tool
|
||||
prepared = self.protocol.dispatch_validated_tool(request, executor)
|
||||
self.assertTrue(prepared["needs_confirm"])
|
||||
self.assertEqual(prepared["pending"].name, "run_command")
|
||||
|
||||
def test_explicit_shell_request_exposes_approved_command_capability(self):
|
||||
from rune import capability_broker
|
||||
|
||||
selected = capability_broker.select_for_job("Use bash and grep to inspect the repository")
|
||||
self.assertIn("grep_files", selected["names"])
|
||||
self.assertIn("run_command", selected["names"])
|
||||
|
||||
def test_non_object_arguments_are_rejected(self):
|
||||
cases = (
|
||||
'{"tool":"list_directory","args":"/tmp"}',
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
"""witness delegation battery (W3.4): kernel path, oracle path, per-table
|
||||
parity — direct coverage for the delegation layer the canaries only reach
|
||||
through cc_loader."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from rune import witness as wt # noqa: E402
|
||||
|
||||
|
||||
def test_extract_topic_oracle_matrix():
|
||||
assert wt._py_extract_topic("the deployment runner needs repair now") == \
|
||||
["deployment", "runner", "needs", "repair"]
|
||||
assert wt._py_extract_topic("fix this now") == []
|
||||
assert wt._py_extract_topic("") == []
|
||||
|
||||
|
||||
def test_extract_topic_matches_kernel():
|
||||
core = wt._core()
|
||||
if core is None:
|
||||
pytest.skip("kernel unavailable")
|
||||
for msg in ("the deployment runner needs repair now",
|
||||
"why does the memory keep drifting", "fix this now", ""):
|
||||
assert wt.extract_topic(msg) == wt._py_extract_topic(msg), msg
|
||||
|
||||
|
||||
def test_tables_gather_seam_deterministic():
|
||||
t1, t2 = wt._tables(), wt._tables()
|
||||
assert t1 == t2
|
||||
assert t1["stopwords"] == sorted(t1["stopwords"])
|
||||
for key in ("correction", "commitment", "contradiction",
|
||||
"preference", "urgency", "learning"):
|
||||
assert t1[key], f"{key} table must not be empty"
|
||||
|
||||
|
||||
def test_detect_signals_error_and_completion():
|
||||
sig = wt._py_detect_signals("hello", tool_error="boom")
|
||||
assert sig["error"] is True and sig["error_text"] == "boom"
|
||||
assert wt._py_detect_signals("hello", task_status="done")["completion"] == "done"
|
||||
|
||||
|
||||
def test_detect_signals_correction_topic_drift_learning():
|
||||
sig = wt._py_detect_signals("no, that's wrong, try again")
|
||||
assert sig["correction"] is True
|
||||
assert wt._py_detect_signals("anyway moving on to budgets",
|
||||
recent_topics=["budget"])["topic_drift"] is True
|
||||
assert wt._py_detect_signals("I need to check that")["commitment_detected"] is True
|
||||
assert wt._py_detect_signals("this is urgent, deadline today")["urgency_detected"] is True
|
||||
assert wt._py_detect_signals("I prefer rust")["preference_detected"] is True
|
||||
assert wt._py_detect_signals("explain how this works")["learning_opportunity"] is True
|
||||
|
||||
|
||||
def test_detect_signals_kernel_matches_oracle():
|
||||
core = wt._core()
|
||||
if core is None:
|
||||
pytest.skip("kernel unavailable")
|
||||
cases = [("no, that's wrong, try again", None, None),
|
||||
("urgent! I prefer rust, but actually wait", None, None),
|
||||
("I'll send the report", None, None)]
|
||||
for msg, te, ts in cases:
|
||||
py = wt._py_detect_signals(msg, te, ts)
|
||||
rs = wt.detect_signals(msg, te, ts)
|
||||
assert py == rs, (msg, py, rs)
|
||||
|
||||
|
||||
def test_witness_reflect_empty_signals_is_empty():
|
||||
assert wt.witness_reflect({}) == ""
|
||||
Reference in new issue
Block a user