chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:12:33 +08:00
commit aacb60a4af
3387 changed files with 981307 additions and 0 deletions
@@ -0,0 +1,165 @@
from __future__ import annotations
import csv
import importlib.util
import json
import sys
from pathlib import Path
def _load_script():
path = Path(__file__).parents[2] / "scripts" / "experiments" / (
"analyze_dashscope_payload_parity.py"
)
spec = importlib.util.spec_from_file_location("analyze_dashscope_payload_parity", path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def test_qwen_payload_parity_reports_hard_failures(tmp_path: Path) -> None:
mod = _load_script()
trace = tmp_path / "artifacts" / "case-1" / "llm_calls.jsonl"
trace.parent.mkdir(parents=True)
payload = {
"model": "qwen3.6-flash",
"extra_body": {"enable_thinking": True, "preserve_thinking": True},
"max_tokens": 2048,
"stream": True,
"tool_choice": {
"type": "function",
"function": {"name": "read_source"},
},
"stream_options": {"include_usage": False},
"messages": [
{
"role": "assistant",
"reasoning_content": "historical reasoning should not replay",
"tool_calls": [
{
"id": "call-1",
"type": "function",
"function": {"name": "read_source", "arguments": "{}"},
}
],
}
],
"tools": [
{
"type": "function",
"function": {
"name": "read_source",
"parameters": {
"type": "object",
"properties": {"path": False},
},
},
}
],
}
trace.write_text(
json.dumps({"event": "llm.request", "call_index": 1, "payload": payload}) + "\n",
encoding="utf-8",
)
summary, rows = mod.analyze_paths([tmp_path / "artifacts"])
json_path = tmp_path / "summary.json"
csv_path = tmp_path / "rows.csv"
mod.write_outputs(summary, rows, json_path=json_path, csv_path=csv_path)
assert summary["checked_payloads"] == 1
assert summary["failed_checks"] >= 6
failed = {row["check"] for row in rows if row["status"] == "fail"}
assert {
"dashscope_max_completion_tokens",
"qwen_flash_no_reasoning_replay",
"qwen_flash_no_preserve_thinking",
"dashscope_thinking_no_forced_tool_choice",
"stream_include_usage",
"tool_schema_no_boolean_values",
"tool_call_pairing",
} <= failed
csv_rows = list(csv.DictReader(csv_path.open()))
assert csv_rows[0]["instance_id"] == "case-1"
assert json.loads(json_path.read_text())["failed_checks_by_name"]["stream_include_usage"] == 1
def test_qwen_payload_parity_accepts_live_dashscope_request_shape(tmp_path: Path) -> None:
mod = _load_script()
trace = tmp_path / "artifacts" / "case-2" / "llm_calls.jsonl"
trace.parent.mkdir(parents=True)
payload = {
"model": "qwen3.6-flash",
"enable_thinking": True,
"max_completion_tokens": 32768,
"stream_options": {"include_usage": True},
"messages": [
{"role": "system", "content": "tools available"},
{
"role": "assistant",
"tool_calls": [
{
"id": "call-1",
"type": "function",
"function": {"name": "read_source", "arguments": "{}"},
}
],
},
{"role": "tool", "tool_call_id": "call-1", "content": "ok"},
],
"tools": [
{
"type": "function",
"function": {
"name": "read_source",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
"additionalProperties": False,
},
},
}
],
}
trace.write_text(
json.dumps({"event": "llm.request", "call_index": 1, "payload": payload}) + "\n",
encoding="utf-8",
)
summary, rows = mod.analyze_paths([tmp_path / "artifacts"])
assert summary["checked_payloads"] == 1
assert summary["failed_checks"] == 0
checks = {row["check"]: row for row in rows}
assert checks["dashscope_enable_thinking"]["status"] == "pass"
assert checks["tool_schema_no_boolean_values"]["status"] == "pass"
def test_qwen_payload_parity_skips_stream_usage_check_for_non_stream_fallback(
tmp_path: Path,
) -> None:
mod = _load_script()
trace = tmp_path / "artifacts" / "case-3" / "llm_calls.jsonl"
trace.parent.mkdir(parents=True)
payload = {
"model": "qwen3.6-flash",
"enable_thinking": True,
"max_completion_tokens": 32768,
"stream": False,
"messages": [{"role": "user", "content": "fix this"}],
"tools": [],
}
trace.write_text(
json.dumps({"event": "llm.request", "call_index": 1, "payload": payload}) + "\n",
encoding="utf-8",
)
summary, rows = mod.analyze_paths([tmp_path / "artifacts"])
assert summary["checked_payloads"] == 1
assert summary["failed_checks"] == 0
checks = {row["check"]: row for row in rows}
assert checks["stream_include_usage"]["status"] == "skip"
@@ -0,0 +1,105 @@
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
def _load_script():
script_path = Path(__file__).resolve().parents[2] / "scripts" / "experiments" / (
"analyze_dashscope_payload_risk.py"
)
spec = importlib.util.spec_from_file_location("analyze_dashscope_payload_risk", script_path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def _write_jsonl(path: Path, rows: list[dict]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("\n".join(json.dumps(row) for row in rows) + "\n", encoding="utf-8")
def test_analyze_dashscope_payload_risk_counts_provider_visible_signals(
tmp_path: Path,
) -> None:
mod = _load_script()
artifact_root = tmp_path / "artifacts"
inst_a = artifact_root / "repo__issue-1"
inst_a.mkdir(parents=True)
_write_jsonl(
inst_a / "transcript.jsonl",
[
{
"message": {
"role": "assistant",
"content": (
"Historical tool call omitted for provider context budget.\n"
"</think>\n"
"[Earlier duplicate tool interaction omitted for DashScope replay "
"compatibility: tool=read_file]"
),
}
}
],
)
_write_jsonl(
inst_a / "llm_calls.jsonl",
[
{"event": "llm.request", "request_id": "req-a", "ts": 10.0},
{"event": "llm.response_chunk", "request_id": "req-a", "ts": 12.5},
{
"event": "llm.error",
"request_id": "req-timeout",
"error_type": "timeout",
"message": "Request timed out",
},
{
"event": "llm.error",
"request_id": "req-429",
"status_code": 429,
"message": "rate limit",
},
],
)
(inst_a / "git.patch").write_text("", encoding="utf-8")
inst_b = artifact_root / "repo__issue-2"
inst_b.mkdir(parents=True)
_write_jsonl(inst_b / "transcript.jsonl", [{"message": {"content": "clean"}}])
_write_jsonl(
inst_b / "llm_calls.jsonl",
[
{"event": "llm.request", "request_id": "req-b", "ts": 20.0},
{"event": "llm.response_chunk", "request_id": "req-b", "ts": 21.0},
],
)
(inst_b / "git.patch").write_text("diff --git a/a.py b/a.py\n", encoding="utf-8")
predictions = tmp_path / "predictions.jsonl"
_write_jsonl(
predictions,
[
{"instance_id": "repo__issue-1", "model_patch": ""},
{"instance_id": "repo__issue-2", "model_patch": "diff --git a/a.py b/a.py\n"},
],
)
summary = mod.analyze_artifact_root(artifact_root, predictions_path=predictions)
assert summary["instances"] == 2
assert summary["predictions"]["submitted"] == 2
assert summary["predictions"]["empty_model_patch"] == 1
assert summary["patches"]["empty_git_patch"] == 1
assert summary["signals"]["dashscope_duplicate_omission"] == 1
assert summary["signals"]["provider_compaction_omission"] == 1
assert summary["signals"]["bare_think_close"] == 1
assert summary["llm"]["requests"] == 2
assert summary["llm"]["errors"] == 2
assert summary["llm"]["status_429"] == 1
assert summary["llm"]["timeout_errors"] == 1
assert summary["llm"]["first_chunk_latency_seconds"]["count"] == 2
assert summary["llm"]["first_chunk_latency_seconds"]["mean"] == 1.75
@@ -0,0 +1,675 @@
from __future__ import annotations
import hashlib
import importlib.util
import json
from pathlib import Path
def _load_script():
script_path = Path(__file__).resolve().parents[2] / "scripts" / "experiments" / (
"analyze_tool_compression.py"
)
spec = importlib.util.spec_from_file_location("analyze_tool_compression", script_path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def _write_json(path: Path, payload: dict) -> None:
path.write_text(json.dumps(payload), encoding="utf-8")
def _write_jsonl(path: Path, rows: list[dict]) -> None:
path.write_text("\n".join(json.dumps(row) for row in rows) + "\n", encoding="utf-8")
def _write_store_record(
record_dir: Path,
*,
handle: str,
tool_use_id: str,
content: str,
) -> None:
record_dir.mkdir(parents=True)
payload = content.encode("utf-8")
# The raw-store contract is byte-oriented (the analyzer reads content.txt
# with read_bytes and hashes the raw payload). Write bytes so text-mode
# newline translation on Windows cannot skew the sha256/char counts below.
(record_dir / "content.txt").write_bytes(payload)
_write_json(
record_dir / "meta.json",
{
"handle": handle,
"tool_use_id": tool_use_id,
"tool_name": "exec_command",
"size_bytes": len(payload),
"stored_size_bytes": len(payload),
"storage_encoding": "utf-8",
"content_file": "content.txt",
"sha256": hashlib.sha256(payload).hexdigest(),
},
)
def test_summarize_instance_classifies_projection_risks(tmp_path: Path) -> None:
mod = _load_script()
inst = tmp_path / "run-a" / "repo__issue-1"
inst.mkdir(parents=True)
_write_json(
inst / "metadata.json",
{
"instance_id": "repo__issue-1",
"run_id": "run-a",
"model": "qwen3.6-flash",
"state": "patch_collected",
"patch_empty": False,
},
)
_write_json(
inst / "usage.json",
{
"input_tokens": 1000,
"cached_tokens": 900,
"request_count": 3,
"cost_usd": 0.01,
},
)
_write_jsonl(
inst / "runtime_events.jsonl",
[
{
"feature": "tool_result_projection",
"tool_name": "grep_search",
"outcome": "applied",
"reason": "tokenjuice",
"original_chars": 1_200_000,
"projected_chars": 1000,
"saved_chars": 1_199_000,
"tool_result_handle_present": True,
},
{
"feature": "tool_result_projection",
"tool_name": "exec_command",
"outcome": "noop",
"reason": "store_budget_exceeded",
"original_chars": 50_000,
"tool_result_handle_present": False,
},
{
"feature": "tool_result_projection",
"tool_name": "read_file",
"outcome": "noop",
"reason": "semantic_read_file_preserved",
"original_chars": 2000,
"tool_result_handle_present": False,
},
],
)
_write_jsonl(
inst / "transcript.jsonl",
[
{
"message": {
"role": "assistant",
"content": [
{
"type": "toolCall",
"name": "retrieve_tool_result",
"arguments": {"handle": "tr-abc", "mode": "query"},
}
],
}
}
],
)
store_dir = inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session"
record_dir = store_dir / "ab" / "tr-abc"
record_dir.mkdir(parents=True)
_write_json(
record_dir / "meta.json",
{
"handle": "tr-abc",
"size_bytes": 1_200_000,
"stored_size_bytes": 5000,
"storage_encoding": "gzip+utf-8",
},
)
(inst / "git.patch").write_text(
"diff --git a/src/main.py b/src/main.py\n"
"diff --git a/fix.py b/fix.py\n",
encoding="utf-8",
)
summary = mod.summarize_instance(inst)
assert summary["usage"]["kv_cache_hit_rate"] == 0.9
assert summary["projection"]["events"] == 3
assert summary["projection"]["applied"] == 1
assert summary["projection"]["semantic_preserves"] == 1
assert summary["projection"]["categories"]["huge_grep_log"] == 1
assert summary["projection"]["categories"]["store_budget_exceeded"] == 1
assert "retrieval_unused" not in summary["projection"]["categories"]
assert summary["retrieval"]["calls"] == 1
assert summary["raw_store"]["records"] == 1
assert summary["raw_store"]["compressed_records"] == 1
assert summary["patch"]["scratch_paths"] == ["fix.py"]
def test_summarize_instance_flags_root_apply_helper_patch_artifacts(
tmp_path: Path,
) -> None:
mod = _load_script()
inst = tmp_path / "run-helper" / "repo__issue-helper"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
(inst / "git.patch").write_text(
"diff --git a/apply_fix.py b/apply_fix.py\n"
"diff --git a/apply_util_fix.py b/apply_util_fix.py\n"
"diff --git a/include/pkg/util.h b/include/pkg/util.h\n",
encoding="utf-8",
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["patch"]["scratch_paths"] == [
"apply_fix.py",
"apply_util_fix.py",
]
assert aggregate["scratch_patch_instances"] == 1
def test_summarize_instance_reports_raw_store_integrity_and_duplicates(
tmp_path: Path,
capsys,
) -> None:
mod = _load_script()
inst = tmp_path / "run-integrity" / "repo__issue-raw"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
_write_jsonl(
inst / "runtime_events.jsonl",
[
{
"feature": "tool_result_projection",
"tool_use_id": "call-1",
"tool_name": "exec_command",
"outcome": "noop",
"reason": "no_reduction",
},
{
"feature": "tool_result_projection",
"tool_use_id": "call-2",
"tool_name": "exec_command",
"outcome": "applied",
"reason": "tokenjuice",
"tool_result_handle": "tr-missing",
"tool_result_handle_present": True,
},
],
)
store_root = inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session"
_write_store_record(
store_root / "aa" / "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
handle="tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
tool_use_id="call-1",
content="first snapshot",
)
_write_store_record(
store_root / "bb" / "tr-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
handle="tr-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
tool_use_id="call-1",
content="duplicate snapshot",
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["raw_store"]["records"] == 2
assert summary["raw_store"]["unique_tool_use_ids"] == 1
assert summary["raw_store"]["duplicate_tool_use_records"] == 1
assert summary["raw_store"]["content_missing"] == 0
assert summary["raw_store"]["hash_mismatches"] == 0
assert summary["raw_store"]["size_mismatches"] == 0
assert summary["raw_store"]["projection_tool_use_ids"] == 2
assert summary["raw_store"]["projection_tool_use_ids_covered"] == 1
assert summary["raw_store"]["projection_tool_use_ids_missing"] == 1
assert summary["raw_store"]["projection_handles_missing"] == 1
assert aggregate["raw_store_duplicate_tool_use_records"] == 1
assert aggregate["raw_store_integrity_bad"] == 0
assert aggregate["raw_store_projection_tool_use_ids_missing"] == 1
assert aggregate["raw_store_projection_handles_missing"] == 1
assert aggregate["raw_store_projection_links_missing"] == 2
mod._print_table([summary])
table = capsys.readouterr().out.splitlines()
header = table[0].split("\t")
row = table[1].split("\t")
assert "raw_missing" not in header
assert header[header.index("raw_bad")] == "raw_bad"
assert header[header.index("raw_link_missing")] == "raw_link_missing"
assert row[header.index("raw_bad")] == "0"
assert row[header.index("raw_link_missing")] == "2"
assert header[header.index("replay_bad")] == "replay_bad"
def test_summarize_instance_verifies_transcript_projection_replay(
tmp_path: Path,
) -> None:
mod = _load_script()
inst = tmp_path / "run-replay" / "repo__issue-replay"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
content = "raw output\n重要 detail\n"
handle = "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_write_store_record(
inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session" / "aa" / handle,
handle=handle,
tool_use_id="call-1",
content=content,
)
envelope = (
"[tool_result_projection]\n"
f"tool_result_handle: {handle}\n"
f"sha256: {hashlib.sha256(content.encode('utf-8')).hexdigest()}\n"
f"original_chars: {len(content)}\n"
"retrieve_hint: this result is incomplete.\n"
"[tokenjuice]\nsummary"
)
_write_jsonl(
inst / "transcript.jsonl",
[
{
"message": {
"role": "toolResult",
"toolName": "exec_command",
"content": [{"type": "text", "text": envelope}],
}
}
],
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["transcript_projection"]["events"] == 1
assert summary["transcript_projection"]["handle_present"] == 1
assert summary["transcript_projection"]["replay_bad"] == 0
assert aggregate["transcript_projection_events"] == 1
assert aggregate["transcript_projection_replay_bad"] == 0
def test_summarize_instance_flags_transcript_projection_replay_failures(
tmp_path: Path,
capsys,
) -> None:
mod = _load_script()
inst = tmp_path / "run-replay-bad" / "repo__issue-replay-bad"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
content = "raw output\n"
handle = "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_write_store_record(
inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session" / "aa" / handle,
handle=handle,
tool_use_id="call-1",
content=content,
)
bad_sha = "0" * 64
envelopes = [
(
"[tool_result_projection]\n"
f"tool_result_handle: {handle}\n"
f"sha256: {bad_sha}\n"
"original_chars: 999\n"
"[tokenjuice]\nsummary"
),
(
"[tool_result_projection]\n"
"tool_result_handle: tr-missing\n"
f"sha256: {bad_sha}\n"
"original_chars: 1\n"
"[tokenjuice]\nsummary"
),
]
_write_jsonl(
inst / "transcript.jsonl",
[
{
"message": {
"role": "toolResult",
"toolName": "exec_command",
"content": [{"type": "text", "text": envelope}],
}
}
for envelope in envelopes
],
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["transcript_projection"]["events"] == 2
assert summary["transcript_projection"]["handles_missing"] == 1
assert summary["transcript_projection"]["sha_mismatches"] == 1
assert summary["transcript_projection"]["size_mismatches"] == 1
assert summary["transcript_projection"]["replay_bad"] == 3
assert aggregate["transcript_projection_replay_bad"] == 3
assert aggregate["categories"]["transcript_projection_handle_missing"] == 1
assert aggregate["categories"]["transcript_projection_sha_mismatch"] == 1
assert aggregate["categories"]["transcript_projection_size_mismatch"] == 1
mod._print_table([summary])
table = capsys.readouterr().out.splitlines()
header = table[0].split("\t")
row = table[1].split("\t")
categories = row[header.index("categories")]
assert "transcript_projection_handle_missing:1" in categories
assert "transcript_projection_sha_mismatch:1" in categories
assert "transcript_projection_size_mismatch:1" in categories
def test_summarize_instance_separates_raw_integrity_from_projection_links(
tmp_path: Path,
) -> None:
mod = _load_script()
inst = tmp_path / "run-integrity-bad" / "repo__issue-raw-bad"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
_write_jsonl(
inst / "runtime_events.jsonl",
[
{
"feature": "tool_result_projection",
"tool_use_id": "call-1",
"tool_name": "exec_command",
"outcome": "applied",
"reason": "tokenjuice",
"tool_result_handle": "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"tool_result_handle_present": True,
}
],
)
store_root = inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session"
record_dir = store_root / "aa" / "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_write_store_record(
record_dir,
handle="tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
tool_use_id="call-1",
content="raw snapshot",
)
meta_path = record_dir / "meta.json"
meta = json.loads(meta_path.read_text(encoding="utf-8"))
meta["sha256"] = "0" * 64
_write_json(meta_path, meta)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["raw_store"]["hash_mismatches"] == 1
assert summary["raw_store"]["projection_tool_use_ids_missing"] == 0
assert summary["raw_store"]["projection_handles_missing"] == 0
assert aggregate["raw_store_integrity_bad"] == 1
assert aggregate["raw_store_projection_links_missing"] == 0
def test_summarize_instance_reports_dispatch_truncated_raw_snapshot(
tmp_path: Path,
capsys,
) -> None:
mod = _load_script()
inst = tmp_path / "run-trunc" / "repo__issue-trunc"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": False})
_write_jsonl(inst / "runtime_events.jsonl", [])
truncated = {
"result_truncated": True,
"result_original_chars": 1_500_000,
"result_omitted_chars": 1_490_000,
"tool": "exec_command",
"preview": "HEAD",
"tail": "TAIL",
"tool_result_handle": "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"retrieve_hint": "Use retrieve_tool_result with tool_result_handle.",
}
_write_jsonl(
inst / "transcript.jsonl",
[
{
"message": {
"role": "toolResult",
"toolName": "exec_command",
"content": [{"type": "text", "text": json.dumps(truncated)}],
}
}
],
)
store_root = inst / "opensquilla_state" / "media" / "tool-results" / "s" / "session"
_write_store_record(
store_root / "aa" / "tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
handle="tr-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
tool_use_id="call-large",
content="HEAD" + ("x" * 1024) + "TAIL",
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["dispatch_truncation"]["events"] == 1
assert summary["dispatch_truncation"]["huge_events"] == 1
assert summary["dispatch_truncation"]["handle_present"] == 1
assert summary["dispatch_truncation"]["handles_missing"] == 0
assert summary["dispatch_truncation"]["tools"] == {"exec_command": 1}
assert aggregate["dispatch_truncation_events"] == 1
assert aggregate["dispatch_truncation_huge_events"] == 1
assert aggregate["dispatch_truncation_handles_missing"] == 0
assert aggregate["categories"]["dispatch_huge_exec_log"] == 1
mod._print_table([summary])
table = capsys.readouterr().out.splitlines()
header = table[0].split("\t")
row = table[1].split("\t")
assert "dispatch_huge_exec_log:1" in row[header.index("categories")]
def test_eval_report_merge_uses_run_id_hint_to_avoid_status_conflicts(
tmp_path: Path,
) -> None:
mod = _load_script()
inst = tmp_path / "run-qwen" / "repo__issue-1"
inst.mkdir(parents=True)
_write_json(
inst / "metadata.json",
{
"instance_id": "repo__issue-1",
"run_id": "run-qwen",
"patch_empty": False,
},
)
qwen_report = tmp_path / "model.run-qwen-eval.json"
glm_report = tmp_path / "model.run-glm-eval.json"
_write_json(qwen_report, {"resolved_ids": ["repo__issue-1"]})
_write_json(glm_report, {"unresolved_ids": ["repo__issue-1"]})
reports = mod._load_eval_reports([qwen_report, glm_report])
instances = mod._annotate_eval_status([mod.summarize_instance(inst)], reports)
aggregate = mod.aggregate(instances)
assert instances[0]["eval"]["status"] == "resolved"
assert aggregate["eval_total"] == 1
assert aggregate["eval_resolved"] == 1
assert aggregate["eval_resolved_rate"] == 1.0
assert aggregate["eval_statuses"] == {"resolved": 1}
def test_aggregate_flags_projection_without_retrieval(tmp_path: Path) -> None:
mod = _load_script()
inst = tmp_path / "run-b" / "repo__issue-2"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": True})
_write_jsonl(
inst / "runtime_events.jsonl",
[
{
"feature": "tool_result_projection",
"tool_name": "exec_command",
"outcome": "applied",
"reason": "tokenjuice",
"original_chars": 9000,
"projected_chars": 500,
"command": "sed -n '1,120p' src/lib.rs",
"tool_arguments": {"command": "sed -n '1,120p' src/lib.rs"},
"tool_result_handle_present": True,
}
],
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["projection"]["categories"]["projection_without_retrieve"] == 1
assert summary["projection"]["categories"]["retrieval_unused"] == 1
assert summary["projection"]["categories"]["source_lost"] == 1
assert aggregate["empty_patches"] == 1
assert aggregate["projection_applied"] == 1
def test_summarize_instance_reports_retrieval_result_continuations(
tmp_path: Path,
capsys,
) -> None:
mod = _load_script()
inst = tmp_path / "run-retrieve" / "repo__issue-retrieve"
inst.mkdir(parents=True)
_write_json(inst / "metadata.json", {"patch_empty": True})
_write_jsonl(inst / "runtime_events.jsonl", [])
_write_jsonl(
inst / "transcript.jsonl",
[
{
"message": {
"role": "assistant",
"content": [
{
"type": "toolCall",
"name": "retrieve_tool_result",
"arguments": {"handle": "tr-abc", "query": "ERROR"},
}
],
}
},
{
"message": {
"role": "toolResult",
"toolName": "retrieve_tool_result",
"content": [
{
"type": "text",
"text": (
"[tool_result_retrieval]\n"
"handle: tr-abc\n"
"mode: query\n"
"---\n"
"[retrieve_tool_result truncated: "
"returned_chars=500, original_chars=5000]\n"
"continuation.next_call_strategy: "
"same_query_larger_max_chars\n"
'continuation.next_call: {"arguments": '
'{"handle": "tr-abc", "query": "ERROR"}}\n'
),
}
],
}
},
],
)
summary = mod.summarize_instance(inst)
aggregate = mod.aggregate([summary])
assert summary["retrieval"]["calls"] == 1
assert summary["retrieval"]["results"] == 1
assert summary["retrieval"]["modes"] == {"query": 1}
assert summary["retrieval"]["result_modes"] == {"query": 1}
assert summary["retrieval"]["truncated_results"] == 1
assert summary["retrieval"]["continuation_suggestions"] == 1
assert summary["retrieval"]["continuation_strategies"] == {
"same_query_larger_max_chars": 1
}
assert aggregate["retrieve_results"] == 1
assert aggregate["retrieve_truncated_results"] == 1
assert aggregate["retrieve_continuation_suggestions"] == 1
assert aggregate["retrieve_continuation_strategies"] == {
"same_query_larger_max_chars": 1
}
assert "retrieval_result_missing" not in aggregate["categories"]
assert "retrieval_truncated_without_continuation" not in aggregate["categories"]
mod._print_table([summary])
table = capsys.readouterr().out.splitlines()
header = table[0].split("\t")
row = table[1].split("\t")
assert row[header.index("retrieve")] == "1"
assert row[header.index("retrieval_results")] == "1"
assert row[header.index("retrieval_continuations")] == "1"
def test_gate_violations_check_expansion_thresholds() -> None:
mod = _load_script()
aggregate = {
"kv_cache_hit_rate": 0.875,
"raw_store_integrity_bad": 1,
"raw_store_projection_links_missing": 2,
"empty_patches": 3,
"dispatch_truncation_handles_missing": 4,
"transcript_projection_replay_bad": 5,
"eval_resolved_rate": 0.6,
"categories": {"source_lost": 1},
}
assert mod._gate_violations(
aggregate,
min_kv_cache_hit_rate=0.87,
max_raw_bad=1,
max_raw_link_missing=2,
max_empty_patches=3,
max_dispatch_truncation_missing=4,
max_transcript_replay_bad=5,
min_eval_resolved_rate=0.6,
max_categories={"source_lost": 1},
) == []
assert mod._gate_violations(
aggregate,
min_kv_cache_hit_rate=0.88,
max_raw_bad=0,
max_raw_link_missing=1,
max_empty_patches=2,
max_dispatch_truncation_missing=3,
max_transcript_replay_bad=4,
min_eval_resolved_rate=0.7,
max_categories={"source_lost": 0},
) == [
"kv_cache_hit_rate 0.8750 < 0.8800",
"raw_store_integrity_bad 1 > 0",
"raw_store_projection_links_missing 2 > 1",
"empty_patches 3 > 2",
"dispatch_truncation_handles_missing 4 > 3",
"transcript_projection_replay_bad 5 > 4",
"eval_resolved_rate 0.6000 < 0.7000",
"category source_lost 1 > 0",
]
def test_instance_dirs_ignore_empty_patch_recovery_artifacts(tmp_path: Path) -> None:
mod = _load_script()
inst = tmp_path / "run-c" / "repo__issue-3"
recovery = inst / "empty_patch_recovery"
recovery.mkdir(parents=True)
_write_json(inst / "metadata.json", {"instance_id": "repo__issue-3"})
_write_jsonl(inst / "runtime_events.jsonl", [])
_write_jsonl(recovery / "runtime_events.jsonl", [])
assert mod._instance_dirs([tmp_path / "run-c"]) == [inst.resolve()]
@@ -0,0 +1,895 @@
from __future__ import annotations
import importlib.util
import os
import stat
import subprocess
import sys
import tomllib
from pathlib import Path
from zipfile import ZipFile
import pytest
SCRIPT_PATH = Path(__file__).resolve().parents[2] / "scripts" / "build_wheelhouse_zip.py"
REPO_ROOT = SCRIPT_PATH.parents[1]
WORKFLOW_PATH = REPO_ROOT / ".github" / "workflows" / "wheelhouse-release.yml"
INTERNAL_RELEASE_MARKERS = (
"INTERNAL_ORG_NAME",
"github.com/internal-org/opensquilla",
".internal/evidence",
"INTERNAL_RELEASE_NOTE.md",
"LOCAL_AGENT_NOTES.md",
)
def assert_executable_on_posix(path: Path) -> None:
if os.name != "nt":
assert path.stat().st_mode & stat.S_IXUSR
def load_script():
spec = importlib.util.spec_from_file_location("build_wheelhouse_zip", SCRIPT_PATH)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def test_build_wheel_retries_once_after_transient_uv_failure(monkeypatch, tmp_path: Path) -> None:
module = load_script()
wheel_path = tmp_path / "wheels" / "opensquilla-0.1.0-py3-none-any.whl"
calls = []
def fake_run(args, *, cwd, env):
calls.append((args, cwd, env))
if len(calls) == 1:
raise subprocess.CalledProcessError(4294967295, args)
monkeypatch.setattr(module, "run", fake_run)
monkeypatch.setattr(module.time, "sleep", lambda _: None)
monkeypatch.setattr(module, "find_built_wheel", lambda wheel_dir: wheel_path)
assert (
module.build_wheel(tmp_path, tmp_path / "wheels", {"UV_CACHE_DIR": "cache"})
== wheel_path
)
assert len(calls) == 2
def test_build_subprocess_env_keeps_uv_cache_outside_repo_root(tmp_path: Path) -> None:
module = load_script()
repo_root = tmp_path / "repo"
work_dir = repo_root / "build" / "wheelhouse-zip"
repo_root.mkdir()
env = module.build_subprocess_env(work_dir, repo_root=repo_root)
uv_cache = Path(env["UV_CACHE_DIR"]).resolve()
pip_cache = Path(env["PIP_CACHE_DIR"]).resolve()
assert not uv_cache.is_relative_to(repo_root.resolve())
assert not pip_cache.is_relative_to(repo_root.resolve())
assert uv_cache.name == "uv-cache"
assert pip_cache.name == "pip-cache"
def test_release_name_records_platform_python_profile() -> None:
module = load_script()
wheelhouse_name = module.release_name(
app_version="0.1.0",
platform_tag="macos-arm64",
python_major=3,
python_minor=12,
profile="recommended",
portable=False,
)
portable_name = module.release_name(
app_version="0.1.0",
platform_tag="macos-arm64",
python_major=3,
python_minor=12,
profile="recommended",
portable=True,
)
assert wheelhouse_name == "OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse"
assert portable_name == "OpenSquilla-0.1.0-macos-arm64-py312-recommended-portable"
def test_python_runtime_asset_name_uses_platform_triple() -> None:
module = load_script()
macos = module.python_runtime_asset_name(
python_version="3.12.13",
runtime_release="20260414",
platform_tag="macos-arm64",
)
windows = module.python_runtime_asset_name(
python_version="3.12.13",
runtime_release="20260414",
platform_tag="windows-x64",
)
assert macos == (
"cpython-3.12.13+20260414-aarch64-apple-darwin-install_only_stripped.tar.gz"
)
assert windows == (
"cpython-3.12.13+20260414-x86_64-pc-windows-msvc-install_only_stripped.tar.gz"
)
def test_cross_platform_wheelhouse_requires_target_host(tmp_path: Path) -> None:
module = load_script()
module.platform_tag = lambda: "linux-x64"
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
package_dir = tmp_path / "packages"
with pytest.raises(SystemExit, match="must run on the target platform"):
module.build_wheelhouse_command(
package_dir,
wheel_path,
"recommended",
target_platform_tag="windows-x64",
python_major=3,
python_minor=12,
)
def test_portable_recommended_wheelhouse_uses_recommended_extra_only(tmp_path: Path) -> None:
module = load_script()
module.platform_tag = lambda: "windows-x64"
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
package_dir = tmp_path / "packages"
command = module.build_wheelhouse_command(
package_dir,
wheel_path,
"recommended",
target_platform_tag="windows-x64",
python_major=3,
python_minor=12,
)
assert str(wheel_path) + "[recommended]" in command
assert str(wheel_path) + "[recommended,feishu]" not in command
def test_release_wheel_allows_router_provenance_markdown() -> None:
module = load_script()
provenance = module.ROUTER_PROVENANCE_WHEEL_PATH
tokenjuice_provenance = module.TOKENJUICE_PROVENANCE_WHEEL_PATH
pptx_reference = "opensquilla/skills/bundled/pptx/references/python_pptx.md"
unrelated_skill_reference = (
"opensquilla/skills/bundled/example/references/private-notes.md"
)
skill_readme = "opensquilla/skills/bundled/filesystem/README.md"
skill_license = "opensquilla/skills/bundled/filesystem/LICENSE.md"
skill_card = "opensquilla/skills/bundled/filesystem/skill-card.md"
unrelated_router_doc = (
"opensquilla/squilla_router/models/v4.2_phase3_inference/README.md"
)
violations = module.forbidden_release_wheel_entries(
(
provenance,
tokenjuice_provenance,
unrelated_router_doc,
"opensquilla/skills/bundled/example/SKILL.md",
pptx_reference,
unrelated_skill_reference,
skill_readme,
skill_license,
skill_card,
)
)
assert provenance not in violations
assert tokenjuice_provenance not in violations
assert "opensquilla/skills/bundled/example/SKILL.md" not in violations
assert pptx_reference not in violations
assert unrelated_router_doc in violations
assert unrelated_skill_reference in violations
assert skill_readme in violations
assert skill_license in violations
assert skill_card in violations
def test_pyproject_release_wheel_config_excludes_forbidden_skill_resources() -> None:
module = load_script()
pyproject = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8"))
wheel_config = pyproject["tool"]["hatch"]["build"]["targets"]["wheel"]
excludes = set(wheel_config.get("exclude", []))
force_includes = wheel_config.get("force-include", {})
assert "src/opensquilla/skills/bundled/**/THIRD_PARTY_NOTICES.md" in excludes
assert "src/opensquilla/skills/bundled/**/README.md" in excludes
assert "src/opensquilla/skills/bundled/**/LICENSE.md" in excludes
assert "src/opensquilla/skills/bundled/**/skill-card.md" in excludes
assert "src/opensquilla/skills/bundled/**/references/*.md" in excludes
assert "src/opensquilla/skills/exp/**" in excludes
assert "src/opensquilla/skills/meta/META_SKILL_AUTHORING.md" in excludes
assert module.forbidden_release_wheel_entries(tuple(force_includes.values())) == []
def test_required_router_assets_include_provenance_and_manifest() -> None:
module = load_script()
assert "v4.2_phase3_inference/PROVENANCE.md" in module.ROUTER_ASSET_RELS
assert "v4.2_phase3_inference/artifact_manifest.json" in module.ROUTER_ASSET_RELS
def test_project_release_metadata_avoids_internal_repository_markers() -> None:
for rel_path in ("pyproject.toml", "README.release.md", "LICENSE"):
text = (REPO_ROOT / rel_path).read_text(encoding="utf-8")
for marker in INTERNAL_RELEASE_MARKERS:
assert marker not in text
def test_public_release_docs_avoid_private_kol_language() -> None:
for rel_path in ("README.md",):
text = (REPO_ROOT / rel_path).read_text(encoding="utf-8").lower()
assert "kol" not in text
assert "private" not in text
def test_release_wheel_content_scanner_flags_internal_markers(tmp_path: Path) -> None:
module = load_script()
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
with ZipFile(wheel_path, "w") as archive:
archive.writestr("opensquilla/__init__.py", "__version__ = '0.1.0'\n")
archive.writestr(
"opensquilla-0.1.0.dist-info/METADATA",
"\n".join(
[
"Author: INTERNAL_ORG_NAME",
"Project-URL: Repository, https://github.com/internal-org/opensquilla",
"",
]
),
)
assert module.forbidden_release_text_hits(wheel_path) == [
"opensquilla-0.1.0.dist-info/METADATA: INTERNAL_ORG_NAME",
"opensquilla-0.1.0.dist-info/METADATA: github.com/internal-org/opensquilla",
]
def test_install_scripts_install_from_local_wheelhouse_and_run_onboarding() -> None:
module = load_script()
sh_script = module.render_install_sh(
wheel_name="opensquilla-0.1.0-py3-none-any.whl",
profile="recommended",
python_major=3,
python_minor=12,
)
ps_script = module.render_install_ps1(
wheel_name="opensquilla-0.1.0-py3-none-any.whl",
profile="recommended",
python_major=3,
python_minor=12,
)
assert 'PACKAGE_DIR="${SCRIPT_DIR}/packages"' in sh_script
assert 'REQUIRED_PYTHON_MINOR=12' in sh_script
assert "uv tool install" in sh_script
assert '--find-links "${PACKAGE_DIR}"' in sh_script
assert '"${PACKAGE_DIR}/opensquilla-0.1.0-py3-none-any.whl[recommended]"' in sh_script
assert '"${OPENSQUILLA_BIN}" onboard' in sh_script
assert '"${OPENSQUILLA_BIN}" onboard --if-needed' in sh_script
assert "opensquilla gateway run" in sh_script
assert "$PackageDir = Join-Path $ScriptDir 'packages'" in ps_script
assert "$RequiredPythonMinor = 12" in ps_script
assert "uv tool install" in ps_script
assert "--find-links" in ps_script
assert "opensquilla-0.1.0-py3-none-any.whl[recommended]" in ps_script
assert "& $OpenSquillaBin onboard --if-needed" in ps_script
assert 'throw "OpenSquilla installation failed with exit code $LASTEXITCODE."' in ps_script
assert 'throw "OpenSquilla onboarding failed with exit code $LASTEXITCODE."' in ps_script
assert "opensquilla gateway run" in ps_script
def test_start_scripts_use_bundled_python_runtime() -> None:
module = load_script()
sh_script = module.render_start_sh()
ps_script = module.render_start_ps1()
cmd_script = module.render_start_cmd()
assert sh_script.startswith('#!/bin/sh\nif [ -z "${BASH_VERSION:-}" ]; then')
assert 'exec /usr/bin/env bash "$0" "$@"' in sh_script
assert 'PYTHON_BIN="${SCRIPT_DIR}/runtime/python/bin/python3"' in sh_script
assert "OPENSQUILLA_WHEEL=" in sh_script
assert "WHEEL_HASH=" in sh_script
assert 'VENV_DIR="${SCRIPT_DIR}/.venv-${WHEEL_HASH}"' in sh_script
assert "--without-pip" in sh_script
assert (
'export PATH="${SCRIPT_DIR}:${VENV_DIR}/bin:${SCRIPT_DIR}/runtime/python/bin:${PATH}"'
in sh_script
)
assert (
'PORTABLE_DATA_DIR="${OPENSQUILLA_PORTABLE_HOME:-${DATA_BASE}/OpenSquilla/'
'portable/${RELEASE_ID}}"' in sh_script
)
assert 'if [[ -z "${OPENSQUILLA_GATEWAY_CONFIG_PATH:-}" ]]; then' in sh_script
assert (
'export OPENSQUILLA_GATEWAY_CONFIG_PATH="${PORTABLE_DATA_DIR}/config.toml"'
in sh_script
)
assert (
'if [[ -z "${OPENSQUILLA_LLM_API_KEY:-}" && -n "${OPENROUTER_API_KEY:-}" ]]; then'
in sh_script
)
assert 'export OPENSQUILLA_STATE_DIR="${PORTABLE_DATA_DIR}"' in sh_script
assert (
'export OPENSQUILLA_GATEWAY_STATE_DIR="${OPENSQUILLA_STATE_DIR}/state"'
in sh_script
)
assert (
'export OPENSQUILLA_GATEWAY_WORKSPACE_DIR="${OPENSQUILLA_STATE_DIR}/workspace"'
in sh_script
)
assert 'mkdir -p "${OPENSQUILLA_STATE_DIR}"' in sh_script
assert '"${PYTHON_BIN}" -m venv --without-pip "${VENV_DIR}"' in sh_script
assert "-m pip install" not in sh_script
assert "Installing OpenSquilla from bundled wheels..." in sh_script
assert 'OPENSQUILLA_MODULE=( "-m" "opensquilla.cli.main" )' in sh_script
assert (
'if [[ ! -f "${OPENSQUILLA_GATEWAY_CONFIG_PATH}" && '
'-n "${OPENROUTER_API_KEY:-}" ]]; then' in sh_script
)
assert (
'"${OPENSQUILLA_BIN}" "${OPENSQUILLA_MODULE[@]}" onboard \\\n'
" --provider openrouter" in sh_script
)
assert "--api-key-env OPENROUTER_API_KEY" in sh_script
assert "--minimal" in sh_script
assert '"${OPENSQUILLA_BIN}" "${OPENSQUILLA_MODULE[@]}" onboard' in sh_script
assert '"${OPENSQUILLA_BIN}" onboard --if-needed' not in sh_script
assert "if [[ -t 1 ]]; then" in sh_script
assert 'exec "${OPENSQUILLA_BIN}" "${OPENSQUILLA_MODULE[@]}" gateway run' in sh_script
assert "else" in sh_script
assert 'CONSOLE_LOG="${OPENSQUILLA_STATE_DIR}/logs/gateway-console.log"' in sh_script
assert 'tee -a "${CONSOLE_LOG}"' in sh_script
assert sh_script.index("if [[ -t 1 ]]; then") < sh_script.index(
'tee -a "${CONSOLE_LOG}"'
)
assert sh_script.index(
'export OPENSQUILLA_GATEWAY_CONFIG_PATH="${PORTABLE_DATA_DIR}/config.toml"'
) < sh_script.index('"${OPENSQUILLA_BIN}" "${OPENSQUILLA_MODULE[@]}" onboard')
assert "$PythonBin = Join-Path $ScriptDir 'runtime\\python\\python.exe'" in ps_script
assert "$OpenSquillaWheel = Get-ChildItem -Path $PackageDir" in ps_script
assert "$WheelHashFull = -join" in ps_script
assert "$WheelHash = $WheelHashFull.Substring(0, 12).ToLowerInvariant()" in ps_script
assert "Get-FileHash" not in ps_script
assert "[System.IO.File]::OpenRead($OpenSquillaWheel.FullName)" in ps_script
assert "$Sha256.ComputeHash($WheelStream)" in ps_script
assert "$VenvDir = Join-Path $VenvRoot $ReleaseId" in ps_script
assert '$env:PATH = "$VenvDir\\Scripts;$env:PATH"' in ps_script
assert 'Join-Path $VenvBase "OpenSquilla\\portable\\$ReleaseId"' in ps_script
assert (
"$env:OPENSQUILLA_GATEWAY_CONFIG_PATH = Join-Path $PortableDataDir 'config.toml'"
in ps_script
)
assert "$env:OPENSQUILLA_LLM_API_KEY = $env:OPENROUTER_API_KEY" in ps_script
assert "$env:OPENSQUILLA_STATE_DIR = $PortableDataDir" in ps_script
assert (
"$env:OPENSQUILLA_GATEWAY_STATE_DIR = Join-Path "
"$env:OPENSQUILLA_STATE_DIR 'state'" in ps_script
)
assert (
"$env:OPENSQUILLA_GATEWAY_WORKSPACE_DIR = Join-Path "
"$env:OPENSQUILLA_STATE_DIR 'workspace'" in ps_script
)
assert "New-Item -ItemType Directory -Path $env:OPENSQUILLA_STATE_DIR -Force" in ps_script
assert "& $PythonBin -m venv --without-pip $VenvDir" in ps_script
assert "-m pip install" not in ps_script
assert "-c $WheelInstallScript" not in ps_script
assert "$WheelInstallScript | & $PythonBin - $PackageDir $SitePackages" in ps_script
assert "Installing OpenSquilla from bundled wheels..." in ps_script
assert '$OpenSquillaArgs = @("-m", "opensquilla.cli.main")' in ps_script
assert (
"if ((-not (Test-Path $env:OPENSQUILLA_GATEWAY_CONFIG_PATH)) "
"-and $env:OPENROUTER_API_KEY) {" in ps_script
)
assert "& $VenvPython @OpenSquillaArgs onboard `" in ps_script
assert "--provider openrouter `" in ps_script
assert "--api-key-env OPENROUTER_API_KEY `" in ps_script
assert "& $VenvPython @OpenSquillaArgs onboard" in ps_script
assert "& $OpenSquillaBin onboard --if-needed" not in ps_script
assert "OpenSquilla environment creation failed" in ps_script
assert "OpenSquilla installation failed" not in ps_script
assert 'throw "OpenSquilla onboarding failed with exit code $LASTEXITCODE."' in ps_script
assert "$OutputRedirected = [Console]::IsOutputRedirected" in ps_script
assert "if (-not $OutputRedirected) {" in ps_script
assert "& $VenvPython @OpenSquillaArgs gateway run" in ps_script
assert "$ConsoleLog = Join-Path $LogDir 'gateway-console.log'" in ps_script
assert "$PreviousErrorActionPreference = $ErrorActionPreference" in ps_script
assert "$ErrorActionPreference = \"Continue\"" in ps_script
assert "$_ -is [System.Management.Automation.ErrorRecord]" in ps_script
assert "Tee-Object -FilePath $ConsoleLog -Append" in ps_script
assert ps_script.index("if (-not $OutputRedirected) {") < ps_script.index(
"Tee-Object -FilePath $ConsoleLog -Append"
)
assert ps_script.index(
"$env:OPENSQUILLA_GATEWAY_CONFIG_PATH = Join-Path $PortableDataDir 'config.toml'"
) < ps_script.index("& $VenvPython @OpenSquillaArgs onboard")
assert ps_script.index(
"$env:OPENSQUILLA_GATEWAY_STATE_DIR = Join-Path "
"$env:OPENSQUILLA_STATE_DIR 'state'"
) < ps_script.index("& $VenvPython @OpenSquillaArgs onboard")
assert cmd_script == (
"@echo off\r\n"
"title OpenSquilla Gateway\r\n"
'cd /d "%~dp0"\r\n'
'set "OSQ_POWERSHELL=powershell.exe"\r\n'
'where pwsh.exe >nul 2>nul && set "OSQ_POWERSHELL=pwsh.exe"\r\n'
'"%OSQ_POWERSHELL%" -NoLogo -NoExit -NoProfile -ExecutionPolicy Bypass '
'-File "%~dp0start.ps1"\r\n'
)
def test_install_script_reexecs_under_bash_before_pipefail() -> None:
module = load_script()
script = module.render_install_sh(
wheel_name="opensquilla-0.1.0-py3-none-any.whl",
profile="recommended",
python_major=3,
python_minor=12,
)
assert script.startswith('#!/bin/sh\nif [ -z "${BASH_VERSION:-}" ]; then')
assert 'exec /usr/bin/env bash "$0" "$@"' in script
assert script.index('exec /usr/bin/env bash "$0" "$@"') < script.index(
"set -euo pipefail"
)
def test_render_readme_is_platform_specific_for_windows_portable() -> None:
module = load_script()
readme = module.render_readme(
app_version="0.1.0",
profile="recommended",
platform_tag="windows-x64",
python_major=3,
python_minor=12,
portable=True,
)
assert "## Windows" in readme
assert "# OpenSquilla 0.1.0 Portable Release" in readme
assert "Wheelhouse Release" not in readme
assert "Right-click `Start OpenSquilla.cmd`" in readme
assert "Run as administrator" in readme
assert "Smart App Control" in readme
assert ".\\start.ps1" in readme
assert "## macOS / Linux" not in readme
assert "bash start.sh" not in readme
assert "Python is bundled in this zip." in readme
assert "Complete onboarding." in readme
assert "Feishu" not in readme
assert "Advanced portable usage" in readme
assert "OPENROUTER_API_KEY" in readme
assert "writes an OpenRouter env-reference config" in readme
assert "supported portable launch\n path is administrator launch" in readme
assert "Microsoft documents that SmartScreen checks downloaded apps" in readme
assert "skip setup when it is complete" not in readme
assert "does not install a global `opensquilla` command" in readme
assert (
"Config, workspace, logs, memory, and runtime state use the normal "
"user-level OpenSquilla directory." in readme
)
def test_render_readme_is_platform_specific_for_macos_portable() -> None:
module = load_script()
readme = module.render_readme(
app_version="0.1.0",
profile="recommended",
platform_tag="macos-arm64",
python_major=3,
python_minor=12,
portable=True,
)
assert "## macOS / Linux" in readme
assert "# OpenSquilla 0.1.0 Portable Release" in readme
assert "Wheelhouse Release" not in readme
assert "bash start.sh" in readme
assert "## Windows PowerShell" not in readme
assert ".\\start.ps1" not in readme
assert "Python is bundled in this zip." in readme
assert "Complete onboarding." in readme
assert "Feishu" not in readme
assert "later starts let you review or change the config" in readme
assert "skip setup when it is complete" not in readme
assert "does not install a global `opensquilla` command" not in readme
assert (
"Config, workspace, logs, memory, and runtime state use the normal "
"user-level OpenSquilla directory." in readme
)
assert ".opensquilla/config.toml" not in readme
def test_prepare_release_tree_writes_user_surface_and_manifest(tmp_path: Path) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse"
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
wheel_path.write_bytes(b"wheel")
bundled_wheel = module.prepare_release_tree(
release_root,
wheel_path,
app_version="0.1.0",
profile="recommended",
platform_tag="macos-arm64",
python_major=3,
python_minor=12,
include_router_assets=True,
portable=False,
runtime_release="",
runtime_asset="",
)
assert bundled_wheel == release_root / "packages" / wheel_path.name
assert bundled_wheel.read_bytes() == b"wheel"
assert (release_root / "README.md").is_file()
assert (release_root / "install.sh").is_file()
assert (release_root / "install.ps1").is_file()
assert (release_root / "LICENSE").is_file()
assert (release_root / "THIRD_PARTY_NOTICES.md").is_file()
assert not (release_root / "runtime").exists()
assert not (release_root / "start.sh").exists()
assert not (release_root / "start.ps1").exists()
assert (release_root / "manifest.json").is_file()
assert_executable_on_posix(release_root / "install.sh")
readme = (release_root / "README.md").read_text(encoding="utf-8")
manifest = (release_root / "manifest.json").read_text(encoding="utf-8")
assert "bash install.sh" in readme
assert ".\\install.ps1" not in readme
assert "Build target:" in readme
assert "Configuration" not in readme
assert "Notes" not in readme
assert "Git repository" not in readme
assert '"platform_tag": "macos-arm64"' in manifest
assert '"profile": "recommended"' in manifest
assert '"include_router_assets": true' in manifest
def test_prepare_portable_release_tree_includes_runtime_and_start_scripts(tmp_path: Path) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-macos-arm64-py312-recommended-portable"
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
runtime_root = tmp_path / "runtime"
(runtime_root / "bin").mkdir(parents=True)
(runtime_root / "bin" / "python3").write_text("python", encoding="utf-8")
(runtime_root / "Lib" / "__pycache__").mkdir(parents=True)
(runtime_root / "Lib" / "module.py").write_text("print('ok')\n", encoding="utf-8")
(runtime_root / "Lib" / "__pycache__" / "module.cpython-312.pyc").write_bytes(b"cache")
wheel_path.write_bytes(b"wheel")
module.prepare_release_tree(
release_root,
wheel_path,
app_version="0.1.0",
profile="recommended",
platform_tag="macos-arm64",
python_major=3,
python_minor=12,
include_router_assets=True,
portable=True,
runtime_release="20260414",
runtime_asset="cpython-3.12.13+20260414-aarch64-apple-darwin-install_only_stripped.tar.gz",
runtime_root=runtime_root,
)
assert (release_root / "runtime" / "python" / "bin" / "python3").is_file()
assert (release_root / "runtime" / "python" / "Lib" / "module.py").is_file()
assert not (release_root / "runtime" / "python" / "Lib" / "__pycache__").exists()
assert (release_root / "start.sh").is_file()
assert (release_root / "start.ps1").is_file()
assert "opensquilla.cli.main" in (release_root / "start.sh").read_text(encoding="utf-8")
assert "opensquilla.cli.main" in (release_root / "start.ps1").read_text(
encoding="utf-8"
)
assert not (release_root / "Start OpenSquilla.cmd").exists()
assert (release_root / "LICENSE").is_file()
assert (release_root / "THIRD_PARTY_NOTICES.md").is_file()
assert not (release_root / "install.sh").exists()
assert not (release_root / "install.ps1").exists()
assert_executable_on_posix(release_root / "start.sh")
manifest = (release_root / "manifest.json").read_text(encoding="utf-8")
assert '"portable": true' in manifest
assert '"runtime_release": "20260414"' in manifest
assert "install_only_stripped.tar.gz" in manifest
def test_prune_portable_runtime_removes_packaging_tools_and_bytecode(
tmp_path: Path,
) -> None:
module = load_script()
runtime_root = tmp_path / "runtime"
site_packages = runtime_root / "Lib" / "site-packages"
long_license_path = (
site_packages
/ "pip-26.0.1.dist-info"
/ "licenses"
/ "src"
/ "pip"
/ "_vendor"
/ "dependency_groups"
)
long_license_path.mkdir(parents=True)
(long_license_path / "LICENSE.txt").write_text("license\n", encoding="utf-8")
for name in ("pip", "setuptools", "wheel", "_distutils_hack", "pkg_resources"):
package = site_packages / name
package.mkdir(parents=True)
(package / "__init__.py").write_text("", encoding="utf-8")
for name in ("setuptools-80.0.0.dist-info", "wheel-0.45.0.dist-info"):
dist_info = site_packages / name
dist_info.mkdir(parents=True)
(dist_info / "METADATA").write_text("Name: test\n", encoding="utf-8")
(site_packages / "opensquilla_runtime_dep").mkdir(parents=True)
(site_packages / "opensquilla_runtime_dep" / "__init__.py").write_text(
"VALUE = 1\n",
encoding="utf-8",
)
pycache = runtime_root / "Lib" / "__pycache__"
pycache.mkdir(parents=True)
(pycache / "module.cpython-312.pyc").write_bytes(b"cache")
module.prune_portable_runtime(runtime_root)
assert not (site_packages / "pip").exists()
assert not (site_packages / "pip-26.0.1.dist-info").exists()
assert not (site_packages / "setuptools").exists()
assert not (site_packages / "setuptools-80.0.0.dist-info").exists()
assert not (site_packages / "wheel").exists()
assert not (site_packages / "wheel-0.45.0.dist-info").exists()
assert not (site_packages / "_distutils_hack").exists()
assert not (site_packages / "pkg_resources").exists()
assert not pycache.exists()
assert (site_packages / "opensquilla_runtime_dep" / "__init__.py").is_file()
def test_prepare_windows_portable_release_tree_includes_double_click_launcher(
tmp_path: Path,
) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-windows-x64-py312-recommended-portable"
wheel_path = tmp_path / "opensquilla-0.1.0-py3-none-any.whl"
runtime_root = tmp_path / "runtime"
runtime_root.mkdir()
(runtime_root / "python.exe").write_text("python", encoding="utf-8")
wheel_path.write_bytes(b"wheel")
module.prepare_release_tree(
release_root,
wheel_path,
app_version="0.1.0",
profile="recommended",
platform_tag="windows-x64",
python_major=3,
python_minor=12,
include_router_assets=True,
portable=True,
runtime_release="20260414",
runtime_asset="cpython-3.12.13+20260414-x86_64-pc-windows-msvc-install_only_stripped.tar.gz",
runtime_root=runtime_root,
)
launcher = release_root / "Start OpenSquilla.cmd"
assert launcher.is_file()
assert launcher.read_bytes() == module.render_start_cmd().encode("utf-8")
cli = release_root / "opensquilla.cmd"
assert cli.is_file()
cli_text = cli.read_text(encoding="utf-8")
assert "start.ps1\" -Cli %*" in cli_text
shell = release_root / "OpenSquilla Shell.cmd"
assert shell.is_file()
shell_text = shell.read_text(encoding="utf-8")
assert "function global:opensquilla" in shell_text
assert "opensquilla.cmd" in shell_text
readme = (release_root / "README.md").read_text(encoding="utf-8")
assert "Right-click `Start OpenSquilla.cmd`" in readme
assert "Run as administrator" in readme
assert "Smart App Control" in readme
assert "run\n`OpenSquilla Shell.cmd`" in readme
assert ".\\opensquilla.cmd onboard" in readme
assert "Closing it stops the gateway." in readme
assert "Advanced portable usage" in readme
start_ps1 = (release_root / "start.ps1").read_text(encoding="utf-8")
assert "Test-WindowsVCRedistInstalled" in start_ps1
assert "RuntimeInformation]::IsOSPlatform" in start_ps1
assert "$RequiresRouterRuntime = $true" in start_ps1
assert '"opensquilla[recommended,feishu]" -notmatch' not in start_ps1
assert "OPENSQUILLA_SKIP_VC_REDIST" in start_ps1
assert "Microsoft.VCRedist.2015+.x64" in start_ps1
assert "https://aka.ms/vs/17/release/vc_redist.x64.exe" in start_ps1
assert "safe router fallback" in start_ps1
assert "If automatic installation fails, install it manually" in start_ps1
assert "After installing, reopen PowerShell and restart OpenSquilla" in start_ps1
assert "$env:PYTHONUTF8 = '1'" in start_ps1
assert "$env:PYTHONIOENCODING = 'utf-8:replace'" in start_ps1
def test_install_portable_wheelhouse_preinstalls_into_bundled_python(
tmp_path: Path,
) -> None:
module = load_script()
release_root = tmp_path / "release"
package_dir = release_root / "packages"
site_packages = release_root / "runtime" / "python" / "Lib" / "site-packages"
package_dir.mkdir(parents=True)
site_packages.mkdir(parents=True)
wheel_path = package_dir / "demo-0.1.0-py3-none-any.whl"
with ZipFile(wheel_path, "w") as wheel:
wheel.writestr("demo_pkg/__init__.py", "VALUE = 1\n")
wheel.writestr("demo-0.1.0.dist-info/METADATA", "Name: demo\n")
wheel.writestr("demo-0.1.0.data/purelib/demo_extra.py", "EXTRA = 2\n")
wheel.writestr("demo-0.1.0.data/scripts/demo-script.py", "print('skip')\n")
module.install_portable_wheelhouse(release_root)
assert (site_packages / "demo_pkg" / "__init__.py").read_text(encoding="utf-8") == (
"VALUE = 1\n"
)
assert (site_packages / "demo_extra.py").read_text(encoding="utf-8") == "EXTRA = 2\n"
assert not (site_packages / "demo-script.py").exists()
def test_create_zip_contains_release_directory_and_preserves_install_mode(tmp_path: Path) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse"
packages = release_root / "packages"
packages.mkdir(parents=True)
(packages / "opensquilla-0.1.0-py3-none-any.whl").write_bytes(b"wheel")
install_script = release_root / "install.sh"
install_script.write_text("#!/usr/bin/env bash\n", encoding="utf-8")
install_script.chmod(0o755)
(release_root / "install.ps1").write_text("Write-Host ok\n", encoding="utf-8")
(release_root / "README.md").write_text("readme\n", encoding="utf-8")
(release_root / "manifest.json").write_text("{}\n", encoding="utf-8")
zip_path = tmp_path / "release.zip"
module.create_zip(release_root, zip_path)
with ZipFile(zip_path) as archive:
names = set(archive.namelist())
install_info = archive.getinfo(
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/install.sh"
)
assert names == {
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/README.md",
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/install.ps1",
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/install.sh",
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/manifest.json",
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-wheelhouse/packages/opensquilla-0.1.0-py3-none-any.whl",
}
assert stat.S_IMODE(install_info.external_attr >> 16) & stat.S_IXUSR
def test_create_zip_preserves_runtime_executable_mode(tmp_path: Path) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-macos-arm64-py312-recommended-portable"
python_bin = release_root / "runtime" / "python" / "bin" / "python3"
python_bin.parent.mkdir(parents=True)
python_bin.write_bytes(b"python")
python_bin.chmod(0o755)
(release_root / "start.sh").write_text("#!/usr/bin/env bash\n", encoding="utf-8")
(release_root / "manifest.json").write_text("{}\n", encoding="utf-8")
zip_path = tmp_path / "release.zip"
module.create_zip(release_root, zip_path)
with ZipFile(zip_path) as archive:
python_info = archive.getinfo(
"OpenSquilla-0.1.0-macos-arm64-py312-recommended-portable/"
"runtime/python/bin/python3"
)
assert stat.S_IMODE(python_info.external_attr >> 16) & stat.S_IXUSR
def test_create_zip_can_use_short_archive_root(tmp_path: Path) -> None:
module = load_script()
release_root = tmp_path / "OpenSquilla-0.1.0-windows-x64-py312-recommended-portable"
(release_root / "runtime" / "python").mkdir(parents=True)
(release_root / "runtime" / "python" / "python.exe").write_bytes(b"python")
zip_path = tmp_path / "release.zip"
module.create_zip(release_root, zip_path, archive_root="OpenSquilla-0.1.0")
with ZipFile(zip_path) as archive:
assert archive.namelist() == ["OpenSquilla-0.1.0/runtime/python/python.exe"]
def test_write_sha256s_records_all_release_zips(tmp_path: Path) -> None:
module = load_script()
first = tmp_path / "OpenSquilla-0.1.0-linux-x64-py312-recommended-portable.zip"
second = tmp_path / "OpenSquilla-0.1.0-linux-x64-py312-recommended-wheelhouse.zip"
first.write_bytes(b"portable")
second.write_bytes(b"wheelhouse")
checksum_path = module.write_sha256s((second, first), tmp_path / "SHA256SUMS")
expected = [
f"{module.sha256_digest(first)} {first.name}",
f"{module.sha256_digest(second)} {second.name}",
]
assert checksum_path == tmp_path / "SHA256SUMS"
assert checksum_path.read_text(encoding="utf-8").splitlines() == expected
def test_release_workflow_publishes_wheel_and_electron_assets_without_portable() -> None:
workflow = WORKFLOW_PATH.read_text(encoding="utf-8")
assert "concurrency:" in workflow
assert "release-assets-${{" in workflow
assert "cancel-in-progress: false" in workflow
assert "timeout-minutes: 90" in workflow
assert workflow.count("timeout-minutes: 150") == 2
assert workflow.count("timeout-minutes: 75") == 2
assert "timeout-minutes: 20" in workflow
assert "build-desktop-macos:" in workflow
assert "build-desktop-windows:" in workflow
assert "Validate workflow inputs" in workflow
assert "python_runtime_release" not in workflow
assert "python_runtime_version" not in workflow
assert "persist-credentials: false" in workflow
assert "bundle_python_runtime:" not in workflow
assert "--platform-tag windows-x64" not in workflow
assert "platform_tag: macos-arm64" not in workflow
assert "platform_tag: linux-x64" not in workflow
assert "for mode in portable wheelhouse" not in workflow
assert "--bundle-python-runtime" not in workflow
assert "uv build --wheel --out-dir dist" in workflow
assert "expected one versioned portable zip" not in workflow
assert "expected one versioned wheel" in workflow
assert "manifest[\"portable\"] is True" not in workflow
assert "SHA256SUMS" in workflow
assert "manifest.version" not in workflow
assert "GH_REPO: ${{ github.repository }}" in workflow
assert "0.5+ release assets must not include Windows portable zips" in workflow
assert "if not is_prerelease:" not in workflow
assert "OpenSquilla-windows-x64-portable.zip" not in workflow
assert "desktop_asset_version" in workflow
assert "OpenSquilla-{desktop_version}-mac-arm64.dmg" in workflow
assert "OpenSquilla-{desktop_version}-win-x64.exe" in workflow
assert "opensquilla-latest-py3-none-any.whl" not in workflow
assert "gh release upload \"${TAG}\" dist/* --clobber" in workflow
assert "dist/*.zip dist/*.zip.sha256 dist/SHA256SUMS" not in workflow
assert "Git LFS pointer leaked into wheel" in workflow
assert "Verify GitHub Release assets" in workflow
assert "release\", \"delete-asset\", tag, name, \"--yes\"" in workflow
assert "name.endswith(\".sha256\")" in workflow
assert '["gh", "release", "view", tag, "--json", "assets"]' in workflow
assert "Unexpected GitHub Release assets" in workflow
assert "\"unexpected\": unexpected" in workflow
assert "zip_path.stem" not in workflow
assert "archive_roots =" not in workflow
assert "root = archive_roots[0] + \"/\"" not in workflow
def test_release_workflow_publishes_from_version_tags() -> None:
workflow = WORKFLOW_PATH.read_text(encoding="utf-8")
assert "tags:" in workflow
assert '- "v*"' in workflow
assert "contents: write" in workflow
assert "RELEASE_TAG:" in workflow
assert "RELEASE_PROFILE:" in workflow
assert "github.ref_name" in workflow
assert "github.event.inputs.tag" in workflow
assert "github.event_name == 'push' || github.event.inputs.tag != ''" in workflow
assert "TAG: ${{ env.RELEASE_TAG }}" in workflow
@@ -0,0 +1,101 @@
from __future__ import annotations
import importlib.util
import json
import subprocess
import sys
from pathlib import Path
def _load_script():
script_path = (
Path(__file__).resolve().parents[2]
/ "scripts"
/ "experiments"
/ "check_docker_image_lock.py"
)
spec = importlib.util.spec_from_file_location("check_docker_image_lock", script_path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _write_lock(path: Path) -> None:
path.write_text(
json.dumps(
{
"schema_version": 1,
"tag": "eval-lock-v1",
"records": [
{
"instance_id": "demo__repo-1",
"image_ref": "sweb.eval.x86_64.demo__repo-1:eval-lock-v1",
"image_id": "sha256:expected1",
},
{
"instance_id": "demo__repo-2",
"image_ref": "sweb.eval.x86_64.demo__repo-2:eval-lock-v1",
"image_id": "sha256:expected2",
},
],
}
),
encoding="utf-8",
)
def test_check_docker_image_lock_accepts_matching_lock(monkeypatch, tmp_path, capsys) -> None:
script = _load_script()
lock = tmp_path / "images.lock.json"
_write_lock(lock)
instances = tmp_path / "instances.txt"
instances.write_text("demo__repo-1\ndemo__repo-2\n", encoding="utf-8")
def fake_run(args, **kwargs):
image_ref = args[2]
if image_ref.endswith("demo__repo-1:eval-lock-v1"):
return subprocess.CompletedProcess(args, 0, stdout="sha256:expected1\n", stderr="")
if image_ref.endswith("demo__repo-2:eval-lock-v1"):
return subprocess.CompletedProcess(args, 0, stdout="sha256:expected2\n", stderr="")
return subprocess.CompletedProcess(args, 1, stdout="", stderr="missing")
monkeypatch.setattr(script.subprocess, "run", fake_run)
rc = script.main(["--lock", str(lock), "--instance-file", str(instances)])
assert rc == 0
assert "checked=2" in capsys.readouterr().out
def test_check_docker_image_lock_fails_on_digest_mismatch(monkeypatch, tmp_path, capsys) -> None:
script = _load_script()
lock = tmp_path / "images.lock.json"
_write_lock(lock)
instances = tmp_path / "instances.txt"
instances.write_text("demo__repo-1\n", encoding="utf-8")
def fake_run(args, **kwargs):
return subprocess.CompletedProcess(args, 0, stdout="sha256:wrong\n", stderr="")
monkeypatch.setattr(script.subprocess, "run", fake_run)
rc = script.main(["--lock", str(lock), "--instance-file", str(instances)])
assert rc == 1
assert "digest_mismatch" in capsys.readouterr().err
def test_check_docker_image_lock_fails_on_missing_lock_record(tmp_path, capsys) -> None:
script = _load_script()
lock = tmp_path / "images.lock.json"
_write_lock(lock)
instances = tmp_path / "instances.txt"
instances.write_text("demo__repo-3\n", encoding="utf-8")
rc = script.main(["--lock", str(lock), "--instance-file", str(instances)])
assert rc == 1
assert "missing_lock_record" in capsys.readouterr().err
@@ -0,0 +1,196 @@
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
def _load_script():
script_path = (
Path(__file__).resolve().parents[2]
/ "scripts"
/ "experiments"
/ "check_treatment_delivery.py"
)
spec = importlib.util.spec_from_file_location("check_treatment_delivery", script_path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _glm_request(effort: str = "high", proof_budget: int = 650_000) -> dict:
return {
"event": "llm.request",
"metadata": {"request_proof": {"proof_budget": proof_budget}},
"payload": {"reasoning": {"effort": effort}},
}
def _dashscope_request(thinking_budget: int = 24_000, proof_budget: int = 650_000) -> dict:
return {
"event": "llm.request",
"metadata": {"request_proof": {"proof_budget": proof_budget}},
"payload": {"enable_thinking": True, "thinking_budget": thinking_budget},
}
def _write_calls(instance_dir: Path, records: list[dict]) -> None:
instance_dir.mkdir(parents=True, exist_ok=True)
(instance_dir / "llm_calls.jsonl").write_text(
"".join(json.dumps(record) + "\n" for record in records),
encoding="utf-8",
)
def test_uniform_treatment_passes(tmp_path, capsys) -> None:
script = _load_script()
_write_calls(tmp_path / "demo__repo-1", [_glm_request(), _glm_request()])
rc = script.main(
[
"--run-dir",
str(tmp_path),
"--expected-proof-budget",
"650000",
"--expected-reasoning-effort",
"high",
]
)
assert rc == 0
out = capsys.readouterr().out
assert "instance=demo__repo-1 requests=2 proof_budget=650000" in out
assert "checked=1 errors=0" in out
def test_treatment_mismatch_fails(tmp_path, capsys) -> None:
script = _load_script()
_write_calls(
tmp_path / "demo__repo-1",
[_glm_request(effort="high"), _glm_request(effort="xhigh")],
)
rc = script.main(
[
"--run-dir",
str(tmp_path),
"--expected-reasoning-effort",
"high",
]
)
assert rc == 1
assert "reasoning_effort_mismatch: demo__repo-1" in capsys.readouterr().err
def test_stream_timeout_fallback_excluded_from_proof_budget(tmp_path, capsys) -> None:
# The non-stream retry of a stream timeout re-sends the same payload but
# records no request_proof; it must not fail the proof-budget assertion.
script = _load_script()
stream_fallback = {
"event": "llm.request",
"metadata": {"fallback_from": "stream_timeout", "stream_error": "ReadTimeout"},
"payload": {"reasoning": {"effort": "high"}},
}
_write_calls(tmp_path / "demo__repo-1", [_glm_request(), stream_fallback, _glm_request()])
rc = script.main(
[
"--run-dir",
str(tmp_path),
"--expected-proof-budget",
"650000",
"--expected-reasoning-effort",
"high",
]
)
assert rc == 0
out = capsys.readouterr().out
assert "requests=3" in out
assert "stream_fallbacks=1" in out
def test_dashscope_thinking_disable_fallback_excluded_and_gated(tmp_path, capsys) -> None:
# The one-shot thinking-disable retry on DashScope carries
# enable_thinking=false and no thinking_budget; it must not fail the
# thinking-budget assertion, but stays gated by --allow-reasoning-fallbacks.
script = _load_script()
thinking_disabled = {
"event": "llm.request",
"metadata": {"request_proof": {"proof_budget": 650_000}},
"payload": {"enable_thinking": False},
}
_write_calls(
tmp_path / "demo__repo-1",
[_dashscope_request(), thinking_disabled, _dashscope_request()],
)
argv = [
"--run-dir",
str(tmp_path),
"--expected-proof-budget",
"650000",
"--expected-thinking-budget",
"24000",
]
strict_rc = script.main(argv)
strict_err = capsys.readouterr().err
tolerant_rc = script.main([*argv, "--allow-reasoning-fallbacks", "1"])
tolerant_out = capsys.readouterr().out
assert strict_rc == 1
assert "reasoning_fallback_exceeded: demo__repo-1 count=1 allowed=0" in strict_err
assert tolerant_rc == 0
assert "thinking_budget=24000" in tolerant_out
assert "reasoning_fallbacks=1" in tolerant_out
def test_openrouter_thinking_disable_fallback_excluded_from_effort(tmp_path, capsys) -> None:
script = _load_script()
thinking_disabled = {
"event": "llm.request",
"metadata": {"request_proof": {"proof_budget": 650_000}},
"payload": {"reasoning": {"enabled": False}},
}
_write_calls(tmp_path / "demo__repo-1", [_glm_request(), thinking_disabled])
rc = script.main(
[
"--run-dir",
str(tmp_path),
"--expected-reasoning-effort",
"high",
"--allow-reasoning-fallbacks",
"1",
]
)
assert rc == 0
out = capsys.readouterr().out
assert "reasoning_effort=high " in out
assert "reasoning_fallbacks=1" in out
def test_unparsed_request_line_fails(tmp_path, capsys) -> None:
script = _load_script()
instance_dir = tmp_path / "demo__repo-1"
_write_calls(instance_dir, [_glm_request()])
with (instance_dir / "llm_calls.jsonl").open("a", encoding="utf-8") as handle:
handle.write('{"event": "llm.request", "payload": {"truncat\n')
rc = script.main(
[
"--run-dir",
str(tmp_path),
"--expected-proof-budget",
"650000",
]
)
assert rc == 1
assert "unparsed_request_lines: demo__repo-1 count=1" in capsys.readouterr().err
+957
View File
@@ -0,0 +1,957 @@
from __future__ import annotations
import importlib.util
import json
import subprocess
import sys
from pathlib import Path
import pytest
pytestmark = pytest.mark.skipif(
sys.platform == "win32",
reason="experiment tooling is POSIX-only (fcntl file locking)",
)
ROOT = Path(__file__).parents[2]
SCRIPTS = ROOT / "scripts" / "experiments"
def _load_script(name: str):
if str(SCRIPTS) not in sys.path:
sys.path.insert(0, str(SCRIPTS))
path = SCRIPTS / f"{name}.py"
spec = importlib.util.spec_from_file_location(name, path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _git_repo(path: Path) -> None:
path.mkdir(parents=True)
subprocess.run(["git", "init"], cwd=path, check=True, capture_output=True)
(path / "README.md").write_text("hello\n", encoding="utf-8")
subprocess.run(["git", "add", "README.md"], cwd=path, check=True)
subprocess.run(
[
"git",
"-c",
"user.email=test@example.com",
"-c",
"user.name=Test",
"commit",
"-m",
"init",
],
cwd=path,
check=True,
capture_output=True,
)
def _handoff_repo(path: Path) -> None:
_git_repo(path)
scripts = path / "scripts"
scripts.mkdir()
runner = scripts / "run_tool_policy_validation_stdin_keys.sh"
runner.write_text("#!/usr/bin/env bash\necho runner\n", encoding="utf-8")
runner.chmod(0o755)
config = path / "config_runs"
(config / "qwen").mkdir(parents=True)
(config / "glm").mkdir(parents=True)
(config / "qwen" / "config.toml").write_text("[llm]\nmodel='qwen'\n", encoding="utf-8")
(config / "glm" / "config.toml").write_text("[llm]\nmodel='glm'\n", encoding="utf-8")
cfg = path / "config"
cfg.mkdir()
(cfg / "ml.txt").write_text("a__b-1\n", encoding="utf-8")
(cfg / "verified.txt").write_text("c__d-2\n", encoding="utf-8")
subprocess.run(["git", "add", "."], cwd=path, check=True)
subprocess.run(
[
"git",
"-c",
"user.email=test@example.com",
"-c",
"user.name=Test",
"commit",
"-m",
"handoff",
],
cwd=path,
check=True,
capture_output=True,
)
def _init_args(source: Path, handoff: Path, exp_id: str = "qwen-ledger-smoke") -> list[str]:
return [
"--exp-id",
exp_id,
"--question",
"does ledger work",
"--condition-label",
"ledger-smoke",
"--run-mode",
"qwen_only",
"--source-root",
str(source),
"--handoff-root",
str(handoff),
"--qwen-config",
str(handoff / "config_runs/qwen/config.toml"),
"--glm-config",
str(handoff / "config_runs/glm/config.toml"),
"--ml-instance-file",
str(handoff / "config/ml.txt"),
"--verified-instance-file",
str(handoff / "config/verified.txt"),
"--ml-count",
"1",
"--verified-count",
"1",
"--qwen-workers",
"10",
"--glm-workers",
"10",
"--eval-workers",
"10",
"--env",
"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE=warn_model",
]
def _write_fake_batch(
batch: Path,
manifest: dict,
report: Path | None = None,
*,
overrides: dict[str, str] | None = None,
infer_rc: int = 0,
eval_rc: int = 0,
) -> None:
batch.mkdir(parents=True)
ml_ids = " ".join(
line.strip()
for line in Path(manifest["slice"]["ml"]["snapshot"]).read_text().splitlines()
if line.strip()
)
verified_ids = " ".join(
line.strip()
for line in Path(manifest["slice"]["verified"]["snapshot"]).read_text().splitlines()
if line.strip()
)
values = {
"batch_id": f"{manifest['exp_id']}-batch",
"opensquilla_source_head": manifest["source"]["head"],
"handoff_head": manifest["handoff"]["head"],
"condition_label": manifest["config"]["condition_label"],
"run_mode": manifest["execution"]["run_mode"],
"qwen_config_sha256": manifest["config"]["qwen_config"]["sha256"],
"glm_config_sha256": manifest["config"]["glm_config"]["sha256"],
"ml_instance_file": manifest["slice"]["ml"]["snapshot"],
"ml_ids": ml_ids,
"verified_instance_file": manifest["slice"]["verified"]["snapshot"],
"verified_ids": verified_ids,
"qwen_workers": str(manifest["execution"]["qwen_workers"]),
"glm_workers": str(manifest["execution"]["glm_workers"]),
"eval_workers": str(manifest["execution"]["eval_workers"]),
}
values.update(overrides or {})
(batch / "manifest.txt").write_text(
"".join(f"{key}={value}\n" for key, value in values.items()),
encoding="utf-8",
)
if report is not None:
(batch / "run-eval.report_paths.txt").write_text(str(report) + "\n", encoding="utf-8")
(batch / "qwen.infer.exit_code").write_text(f"{infer_rc}\n", encoding="utf-8")
(batch / "run-eval.exit_code").write_text(f"{eval_rc}\n", encoding="utf-8")
def _write_fake_instance_metadata(
artifacts_root: Path,
run_id: str,
instance_id: str,
env: dict[str, str],
) -> None:
instance_dir = artifacts_root / run_id / instance_id
instance_dir.mkdir(parents=True)
(instance_dir / "metadata.json").write_text(
json.dumps(
{
"agent": {
"controls": {
"progress_watchdog_env": env,
}
}
}
),
encoding="utf-8",
)
def test_validate_exp_id_and_hash(tmp_path: Path) -> None:
common = _load_script("exp_common")
file = tmp_path / "x.txt"
file.write_text("x\n", encoding="utf-8")
assert common.validate_exp_id("abc-123.x_y") == "abc-123.x_y"
assert common.sha256_file(file) == common.sha256_file(file)
try:
common.validate_exp_id("Bad ID")
except common.LedgerError:
pass
else:
raise AssertionError("invalid exp_id was accepted")
def test_init_writes_manifest_snapshots_and_redacts_secret(
tmp_path: Path,
monkeypatch,
) -> None:
init = _load_script("exp_init")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
rc = init.main(
[
*_init_args(source, handoff),
"--env",
"DASHSCOPE_API_KEY=secret-value",
]
)
assert rc == 0
run_dir = ledger / "runs/qwen-ledger-smoke"
manifest = json.loads((run_dir / "manifest.json").read_text())
command = (run_dir / "command.sh").read_text()
assert manifest["source"]["dirty_count"] == 0
assert manifest["config"]["env"]["DASHSCOPE_API_KEY"]["redacted"] is True
assert "secret-value" not in command
assert (run_dir / "config_snapshot/qwen/config.toml").is_file()
assert (run_dir / "instance_snapshot/ml/ml.txt").is_file()
assert f"QWEN_CONFIG_DIR='{run_dir}/config_snapshot/qwen'" in command
assert f"ML_INSTANCE_FILE='{run_dir}/instance_snapshot/ml/ml.txt'" in command
assert (ledger / "experiments.jsonl").read_text().strip()
def test_init_rejects_dirty_source_and_config_directory(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
(source / "dirty.txt").write_text("dirty\n", encoding="utf-8")
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "dirty-source")) == 2
subprocess.run(["git", "add", "dirty.txt"], cwd=source, check=True)
subprocess.run(
[
"git",
"-c",
"user.email=test@example.com",
"-c",
"user.name=Test",
"commit",
"-m",
"clean",
],
cwd=source,
check=True,
capture_output=True,
)
args = _init_args(source, handoff, "bad-config")
qwen_index = args.index("--qwen-config") + 1
args[qwen_index] = str(handoff / "config_runs/qwen")
assert init.main(args) == 2
def test_init_dry_run_does_not_create_run_dir(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
rc = init.main([*_init_args(source, handoff, "dry-run-exp"), "--dry-run"])
assert rc == 0
assert not (ledger / "runs/dry-run-exp").exists()
def test_status_reports_baseline_and_stale_active(tmp_path: Path, monkeypatch, capsys) -> None:
status = _load_script("exp_status")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
ledger.mkdir(parents=True)
(ledger / "current.json").write_text(
json.dumps({"active_experiment": "missing-exp", "warnings": ["known warning"]}),
encoding="utf-8",
)
(ledger / "baselines.json").write_text(
json.dumps(
{
"qwen": {
"current_best": {
"label": "repo-coding",
"resolved": 4,
"total": 10,
"empty": 0,
}
}
}
),
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
rc = status.main(["--source-root", str(source), "--handoff-root", str(handoff)])
out = capsys.readouterr().out
assert rc == 0
assert "Qwen baseline: repo-coding = 4/10 empty=0" in out
assert "active experiment manifest missing" in out
# An active experiment with no live processes/containers must warn about stale state.
assert "stale active state" in out
def test_run_records_failure_and_clears_active(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
run = _load_script("exp_run")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "run-fails")) == 0
command = ledger / "runs/run-fails/command.sh"
command.write_text("#!/usr/bin/env bash\nexit 7\n", encoding="utf-8")
command.chmod(0o755)
assert run.main(["--exp-id", "run-fails"]) == 7
live_status = json.loads((ledger / "runs/run-fails/live_status.json").read_text())
current = json.loads((ledger / "current.json").read_text())
assert live_status["status"] == "finished"
assert live_status["return_code"] == 7
assert current["active_experiment"] is None
assert current["last_return_code"] == 7
def test_run_rejects_changed_snapshot_hash(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
run = _load_script("exp_run")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "bad-snapshot")) == 0
snapshot = ledger / "runs/bad-snapshot/config_snapshot/qwen/config.toml"
snapshot.write_text("[llm]\nmodel='changed'\n", encoding="utf-8")
assert run.main(["--exp-id", "bad-snapshot"]) == 2
def test_finalize_writes_metrics_and_updates_baseline(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "finalize-smoke")) == 0
run_dir = ledger / "runs/finalize-smoke"
manifest = json.loads((run_dir / "manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps(
{
"total_instances": 2,
"submitted_instances": 2,
"completed_instances": 2,
"resolved_instances": 1,
"unresolved_instances": 1,
"empty_patch_instances": 0,
"error_instances": 0,
"resolved_ids": ["a__b-1"],
}
),
encoding="utf-8",
)
_write_fake_batch(
batch,
manifest,
report,
overrides={"qwen_ml_run_id": "qwen-run", "qwen_verified_run_id": "qwen-verified-run"},
)
_write_fake_instance_metadata(
batch.parent,
"qwen-run",
"a__b-1",
{"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE": "warn_model"},
)
_write_fake_instance_metadata(
batch.parent,
"qwen-verified-run",
"c__d-2",
{"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE": "warn_model"},
)
rc = finalize.main(
[
"--exp-id",
"finalize-smoke",
"--batch-dir",
str(batch),
"--decision",
"adopted",
"--decision-reason",
"test baseline",
"--baseline-model",
"qwen",
"--mechanism",
"repo_coding_qwen",
]
)
assert rc == 0
metrics = json.loads((run_dir / "metrics.json").read_text())
baselines = json.loads((ledger / "baselines.json").read_text())
mechanisms = json.loads((ledger / "mechanisms.json").read_text())
assert metrics["resolved_instances"] == 1
assert baselines["qwen"]["current_best"]["resolved"] == 1
assert mechanisms["repo_coding_qwen"]["status"] == "adopted"
def test_finalize_rejects_mismatched_batch(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "mismatch")) == 0
manifest = json.loads((ledger / "runs/mismatch/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
_write_fake_batch(batch, manifest, report, overrides={"condition_label": "wrong"})
assert (
finalize.main(
[
"--exp-id",
"mismatch",
"--batch-dir",
str(batch),
"--decision",
"adopted",
"--decision-reason",
"must fail",
"--baseline-model",
"qwen",
]
)
== 2
)
def test_finalize_rejects_missing_manifest_env_delivery(
tmp_path: Path,
monkeypatch,
) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "artifacts" / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert (
init.main(
[
*_init_args(source, handoff, "missing-env-delivery"),
"--env",
"OPENSQUILLA_FINALIZE_EVIDENCE_GATE=on",
]
)
== 0
)
manifest = json.loads((ledger / "runs/missing-env-delivery/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
run_id = "qwen-run"
_write_fake_batch(
batch,
manifest,
report,
overrides={"qwen_ml_run_id": run_id, "ml_ids": "a__b-1"},
)
_write_fake_instance_metadata(
batch.parent,
run_id,
"a__b-1",
{"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE": "warn_model"},
)
assert (
finalize.main(
[
"--exp-id",
"missing-env-delivery",
"--batch-dir",
str(batch),
"--decision",
"rejected",
"--decision-reason",
"must fail because treatment env was not delivered",
]
)
== 2
)
assert (
finalize.main(
[
"--exp-id",
"missing-env-delivery",
"--batch-dir",
str(batch),
"--decision",
"invalid",
"--decision-reason",
"missing runtime env delivery",
]
)
== 0
)
def test_finalize_rejects_env_delivery_with_no_resolvable_run_dirs(
tmp_path: Path,
monkeypatch,
) -> None:
"""A pinned env var that can never be checked (no run dirs) must block, not pass."""
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "no-run-dirs")) == 0
manifest = json.loads((ledger / "runs/no-run-dirs/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
# No qwen_ml_run_id/qwen_verified_run_id override: _agent_run_dirs cannot
# resolve any run directory, so env delivery can never be verified.
_write_fake_batch(batch, manifest, report)
assert (
finalize.main(
[
"--exp-id",
"no-run-dirs",
"--batch-dir",
str(batch),
"--decision",
"rejected",
"--decision-reason",
"must fail because run dirs could not be resolved",
]
)
== 2
)
assert (
finalize.main(
[
"--exp-id",
"no-run-dirs",
"--batch-dir",
str(batch),
"--decision",
"invalid",
"--decision-reason",
"no resolvable run dirs",
]
)
== 0
)
def test_finalize_rejects_adopting_quarantined_batch_as_baseline(
tmp_path: Path,
monkeypatch,
) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "quarantined-batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
ledger.mkdir(parents=True)
(ledger / "contaminations.json").write_text(
json.dumps(
{
"version": 1,
"classes": {
"leak_class": {"artifact_names": ["quarantined-batch"]}
},
}
),
encoding="utf-8",
)
assert init.main(_init_args(source, handoff, "quarantined-adopt")) == 0
manifest = json.loads((ledger / "runs/quarantined-adopt/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
_write_fake_batch(
batch,
manifest,
report,
overrides={"qwen_ml_run_id": "qwen-run", "qwen_verified_run_id": "qwen-verified-run"},
)
_write_fake_instance_metadata(
batch.parent,
"qwen-run",
"a__b-1",
{"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE": "warn_model"},
)
_write_fake_instance_metadata(
batch.parent,
"qwen-verified-run",
"c__d-2",
{"OPENSQUILLA_FINAL_DIFF_CONTRACT_MODE": "warn_model"},
)
rc = finalize.main(
[
"--exp-id",
"quarantined-adopt",
"--batch-dir",
str(batch),
"--decision",
"adopted",
"--decision-reason",
"must fail because batch is quarantined",
"--baseline-model",
"qwen",
]
)
assert rc == 2
assert not (ledger / "baselines.json").exists()
def test_finalize_requires_invalid_or_stopped_for_nonzero_infer(
tmp_path: Path,
monkeypatch,
) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "nonzero-infer")) == 0
manifest = json.loads((ledger / "runs/nonzero-infer/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
_write_fake_batch(batch, manifest, report, infer_rc=1)
assert (
finalize.main(
[
"--exp-id",
"nonzero-infer",
"--batch-dir",
str(batch),
"--decision",
"rejected",
"--decision-reason",
"infer failed",
]
)
== 2
)
assert (
finalize.main(
[
"--exp-id",
"nonzero-infer",
"--batch-dir",
str(batch),
"--decision",
"invalid",
"--decision-reason",
"infer failed",
]
)
== 0
)
def test_finalize_flags_nonzero_nonstandard_exit_code(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "nonstd-exit")) == 0
manifest = json.loads((ledger / "runs/nonstd-exit/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
_write_fake_batch(batch, manifest, report)
# A nonzero exit file whose name matches neither the eval nor infer pattern must
# still block a scored decision (regression guard for the silent-ignore hole).
(batch / "containers.exit_code").write_text("3\n", encoding="utf-8")
assert (
finalize.main(
[
"--exp-id",
"nonstd-exit",
"--batch-dir",
str(batch),
"--decision",
"adopted",
"--decision-reason",
"should be blocked",
"--baseline-model",
"qwen",
]
)
== 2
)
assert (
finalize.main(
[
"--exp-id",
"nonstd-exit",
"--batch-dir",
str(batch),
"--decision",
"invalid",
"--decision-reason",
"nonzero container exit",
]
)
== 0
)
metrics = json.loads((ledger / "runs/nonstd-exit/metrics.json").read_text())
assert metrics["nonzero_other_exit_codes"] == {"containers.exit_code": 3}
def test_finalize_rejects_changed_instance_content(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "changed-instances")) == 0
manifest = json.loads((ledger / "runs/changed-instances/manifest.json").read_text())
report = tmp_path / "eval.json"
report.write_text(
json.dumps({"total_instances": 1, "resolved_instances": 1}),
encoding="utf-8",
)
_write_fake_batch(batch, manifest, report)
# Tamper with the instance snapshot the batch references: path still matches the
# manifest, but content (and slice size) no longer do -> must be rejected.
snapshot = Path(manifest["slice"]["ml"]["snapshot"])
snapshot.write_text("a__b-1\nx__y-9\n", encoding="utf-8")
assert (
finalize.main(
[
"--exp-id",
"changed-instances",
"--batch-dir",
str(batch),
"--decision",
"adopted",
"--decision-reason",
"content drifted",
"--baseline-model",
"qwen",
]
)
== 2
)
def test_verify_slice_content_flags_count_mismatch(tmp_path: Path) -> None:
finalize = _load_script("exp_finalize")
instance_file = tmp_path / "ml.txt"
instance_file.write_text("a__b-1\nc__d-2\n", encoding="utf-8")
payload = {
"source": str(instance_file),
"sha256": "", # skip sha check, isolate the count check
"count": 5,
}
errors: list[str] = []
finalize._verify_slice_content(errors, "ml_instance_file", str(instance_file), payload)
assert any("instance count 2 != manifest 5" in item for item in errors)
def test_verify_slice_content_flags_superset_count_mismatch(tmp_path: Path) -> None:
finalize = _load_script("exp_finalize")
instance_file = tmp_path / "ml.txt"
instance_file.write_text("a__b-1\nc__d-2\nc__d-3\n", encoding="utf-8")
payload = {
"source": str(instance_file),
"sha256": "", # skip sha check, isolate the count check
"count": 2,
}
errors: list[str] = []
finalize._verify_slice_content(errors, "ml_instance_file", str(instance_file), payload)
assert any("instance count 3 != manifest 2" in item for item in errors)
def test_verify_slice_content_allows_zero_count_snapshot_file(tmp_path: Path) -> None:
finalize = _load_script("exp_finalize")
instance_file = tmp_path / "verified.txt"
instance_file.write_text("a__b-1\nc__d-2\n", encoding="utf-8")
payload = {
"source": str(instance_file),
"sha256": "", # skip sha check, isolate zero-count behavior
"count": 0,
}
errors: list[str] = []
finalize._verify_slice_content(
errors,
"verified_instance_file",
str(instance_file),
payload,
)
assert errors == []
def test_agent_run_dirs_empty_batch_dir_returns_no_dirs() -> None:
finalize = _load_script("exp_finalize")
artifacts = {
"batch_dir": "",
"batch_manifest": {
"run_mode": "qwen_only",
"ml_ids": "a__b-1",
"qwen_ml_run_id": "qwen-run",
},
}
assert finalize._agent_run_dirs(artifacts) == []
def test_run_rejects_changed_runner(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
run = _load_script("exp_run")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "changed-runner")) == 0
runner = handoff / "scripts/run_tool_policy_validation_stdin_keys.sh"
runner.write_text("#!/usr/bin/env bash\necho tampered\n", encoding="utf-8")
assert run.main(["--exp-id", "changed-runner"]) == 2
def test_finalize_requires_invalid_or_stopped_without_eval(tmp_path: Path, monkeypatch) -> None:
init = _load_script("exp_init")
finalize = _load_script("exp_finalize")
ledger = tmp_path / "ledger"
source = tmp_path / "source"
handoff = tmp_path / "handoff"
batch = tmp_path / "batch"
_git_repo(source)
_handoff_repo(handoff)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
assert init.main(_init_args(source, handoff, "missing-eval")) == 0
manifest = json.loads((ledger / "runs/missing-eval/manifest.json").read_text())
_write_fake_batch(batch, manifest, None)
assert (
finalize.main(
[
"--exp-id",
"missing-eval",
"--batch-dir",
str(batch),
"--decision",
"rejected",
"--decision-reason",
"bad",
]
)
== 2
)
assert (
finalize.main(
[
"--exp-id",
"missing-eval",
"--batch-dir",
str(batch),
"--decision",
"stopped",
"--decision-reason",
"interrupted",
]
)
== 0
)
+416
View File
@@ -0,0 +1,416 @@
from __future__ import annotations
import importlib.util
import json
import subprocess
import sys
from pathlib import Path
import pytest
pytestmark = pytest.mark.skipif(
sys.platform == "win32",
reason="experiment tooling is POSIX-only (fcntl file locking)",
)
ROOT = Path(__file__).parents[2]
SCRIPTS = ROOT / "scripts" / "experiments"
def _load_script(name: str):
if str(SCRIPTS) not in sys.path:
sys.path.insert(0, str(SCRIPTS))
path = SCRIPTS / f"{name}.py"
spec = importlib.util.spec_from_file_location(name, path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _git_repo(path: Path) -> None:
path.mkdir(parents=True)
subprocess.run(["git", "init"], cwd=path, check=True, capture_output=True)
(path / "README.md").write_text("hello\n", encoding="utf-8")
subprocess.run(["git", "add", "README.md"], cwd=path, check=True)
subprocess.run(
[
"git",
"-c",
"user.email=test@example.com",
"-c",
"user.name=Test",
"commit",
"-m",
"init",
],
cwd=path,
check=True,
capture_output=True,
)
def _write_json(path: Path, payload) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
# ---------------------------------------------------------------------------
# exp_common contamination helpers
# ---------------------------------------------------------------------------
def test_artifact_basename_normalizes_paths_and_names():
common = _load_script("exp_common")
assert common.artifact_basename("batch-a") == "batch-a"
assert common.artifact_basename("/data/artifacts/batch-a") == "batch-a"
assert common.artifact_basename("/data/artifacts/batch-a/") == "batch-a"
assert common.artifact_basename(Path("/data/artifacts/batch-a")) == "batch-a"
def test_load_contaminations_defaults_when_missing(tmp_path):
common = _load_script("exp_common")
data = common.load_contaminations(tmp_path)
assert data["classes"] == {}
assert data["version"] == 1
def test_contamination_class_for_matches_by_basename(tmp_path):
common = _load_script("exp_common")
_write_json(
tmp_path / "contaminations.json",
{
"version": 1,
"classes": {
"leak_class": {"artifact_names": ["batch-a", "batch-b"]},
},
},
)
assert common.contamination_class_for(tmp_path, "batch-a") == "leak_class"
assert (
common.contamination_class_for(tmp_path, "/artifacts/batch-b/") == "leak_class"
)
assert common.contamination_class_for(tmp_path, "batch-clean") is None
assert common.contamination_class_for(tmp_path, "") is None
def test_contamination_class_for_tolerates_malformed_registry(tmp_path):
common = _load_script("exp_common")
_write_json(
tmp_path / "contaminations.json",
{"classes": {"bad": "not-a-dict", "worse": {"artifact_names": "not-a-list"}}},
)
assert common.contamination_class_for(tmp_path, "batch-a") is None
# ---------------------------------------------------------------------------
# exp_quarantine CLI
# ---------------------------------------------------------------------------
def _ledger_with_runs(tmp_path: Path) -> Path:
ledger = tmp_path / "ledger"
dirty_run = ledger / "runs" / "exp-dirty"
clean_run = ledger / "runs" / "exp-clean"
_write_json(
dirty_run / "artifacts.json",
{"artifacts": ["/data/artifacts/batch-a", "/data/artifacts/batch-x"]},
)
_write_json(dirty_run / "manifest.json", {"exp_id": "exp-dirty"})
_write_json(
clean_run / "artifacts.json", {"artifacts": ["/data/artifacts/batch-clean"]}
)
_write_json(clean_run / "manifest.json", {"exp_id": "exp-clean"})
return ledger
def test_register_creates_registry_and_stamps_runs(tmp_path, monkeypatch, capsys):
ledger = _ledger_with_runs(tmp_path)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
names_file = tmp_path / "names.json"
_write_json(names_file, ["batch-a", "/data/artifacts/batch-b/", "batch-a"])
rc = quarantine.main(
[
"register",
"--contamination-class",
"leak_class",
"--names-file",
str(names_file),
"--description",
"compaction marker leak",
"--evidence",
"audit/REPORT.md",
"--boundary-commit",
"feedc0de",
"--boundary-commit",
"abadcafe",
]
)
assert rc == 0
registry = json.loads((ledger / "contaminations.json").read_text(encoding="utf-8"))
entry = registry["classes"]["leak_class"]
assert entry["artifact_names"] == ["batch-a", "batch-b"]
assert entry["boundary_commits"] == ["abadcafe", "feedc0de"]
assert entry["description"] == "compaction marker leak"
assert entry["registered_at"]
stamp = json.loads(
(ledger / "runs" / "exp-dirty" / "contamination.json").read_text(
encoding="utf-8"
)
)
assert stamp["classes"]["leak_class"]["matched_artifact_names"] == ["batch-a"]
assert not (ledger / "runs" / "exp-clean" / "contamination.json").exists()
events = [
json.loads(line)
for line in (ledger / "experiments.jsonl")
.read_text(encoding="utf-8")
.splitlines()
]
assert events[-1]["event"] == "contamination_registered"
assert events[-1]["artifact_names_new"] == 2
assert events[-1]["stamped_run_dirs"] == ["exp-dirty"]
out = capsys.readouterr().out
assert "registered leak_class: 2 artifact names (2 new)" in out
def test_register_uses_exact_match_not_substring(tmp_path, monkeypatch):
# A registered name that is a substring of an unrelated, longer batch name
# in a clean run must NOT stamp that clean run - matching is exact-basename,
# consistent with contamination_class_for.
ledger = tmp_path / "ledger"
unrelated_run = ledger / "runs" / "exp-unrelated"
_write_json(
unrelated_run / "artifacts.json",
{"artifacts": ["/data/artifacts/batch-a-extended-run"]},
)
_write_json(unrelated_run / "manifest.json", {"exp_id": "exp-unrelated"})
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
names_file = tmp_path / "names.json"
_write_json(names_file, ["batch-a"])
rc = quarantine.main(
[
"register",
"--contamination-class",
"leak_class",
"--names-file",
str(names_file),
"--description",
"leak",
]
)
assert rc == 0
assert not (unrelated_run / "contamination.json").exists()
def test_register_rejects_names_that_normalize_to_empty(tmp_path, monkeypatch, capsys):
ledger = tmp_path / "ledger"
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
names_file = tmp_path / "names.json"
# "/" is non-empty pre-normalization but collapses to "" via
# artifact_basename - it must not slip into the registry (an empty name
# previously substring-matched, and would exact-match, every run).
_write_json(names_file, ["/", "///"])
rc = quarantine.main(
[
"register",
"--contamination-class",
"leak_class",
"--names-file",
str(names_file),
"--description",
"leak",
]
)
assert rc == 2
assert "names file is empty" in capsys.readouterr().err
def test_register_is_idempotent_and_merges_new_names(tmp_path, monkeypatch, capsys):
ledger = _ledger_with_runs(tmp_path)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
names_file = tmp_path / "names.json"
_write_json(names_file, ["batch-a"])
base_args = [
"register",
"--contamination-class",
"leak_class",
"--names-file",
str(names_file),
"--description",
"leak",
]
assert quarantine.main(base_args) == 0
assert quarantine.main(base_args) == 0
registry = json.loads((ledger / "contaminations.json").read_text(encoding="utf-8"))
assert registry["classes"]["leak_class"]["artifact_names"] == ["batch-a"]
_write_json(names_file, ["batch-b"])
assert quarantine.main(base_args) == 0
registry = json.loads((ledger / "contaminations.json").read_text(encoding="utf-8"))
assert registry["classes"]["leak_class"]["artifact_names"] == ["batch-a", "batch-b"]
out = capsys.readouterr().out
assert "2 artifact names (1 new)" in out
def test_check_reports_quarantined_and_clean(tmp_path, monkeypatch, capsys):
ledger = tmp_path / "ledger"
_write_json(
ledger / "contaminations.json",
{"version": 1, "classes": {"leak_class": {"artifact_names": ["batch-a"]}}},
)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
assert quarantine.main(["check", "/data/artifacts/batch-a"]) == 1
assert "QUARANTINED\tbatch-a\tleak_class" in capsys.readouterr().out
assert quarantine.main(["check", "batch-clean"]) == 0
assert "clean\tbatch-clean" in capsys.readouterr().out
def test_register_rejects_bad_names_file(tmp_path, monkeypatch, capsys):
ledger = tmp_path / "ledger"
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
names_file = tmp_path / "names.json"
_write_json(names_file, {"not": "a list"})
rc = quarantine.main(
[
"register",
"--contamination-class",
"leak_class",
"--names-file",
str(names_file),
"--description",
"leak",
]
)
assert rc == 2
assert "must be a JSON list of strings" in capsys.readouterr().err
def test_list_summarizes_classes(tmp_path, monkeypatch, capsys):
ledger = tmp_path / "ledger"
_write_json(
ledger / "contaminations.json",
{
"version": 1,
"classes": {
"leak_class": {
"artifact_names": ["batch-a", "batch-b"],
"boundary_commits": ["feedc0de"],
"description": "compaction marker leak",
}
},
},
)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
quarantine = _load_script("exp_quarantine")
assert quarantine.main(["list"]) == 0
out = capsys.readouterr().out
assert "leak_class\truns=2\tboundary=feedc0de" in out
assert "compaction marker leak" in out
# ---------------------------------------------------------------------------
# exp_status quarantined-baseline warning
# ---------------------------------------------------------------------------
def test_run_dir_contamination_classes_reads_stamp(tmp_path):
common = _load_script("exp_common")
_write_json(
tmp_path / "runs" / "exp-dirty" / "contamination.json",
{"classes": {"leak_class": {"matched_artifact_names": ["batch-a"]}}},
)
assert common.run_dir_contamination_classes(tmp_path, "exp-dirty") == ["leak_class"]
assert common.run_dir_contamination_classes(tmp_path, "exp-clean") == []
assert common.run_dir_contamination_classes(tmp_path, "") == []
assert common.run_dir_contamination_classes(tmp_path, "../escape") == []
def test_status_warns_when_baseline_artifact_quarantined(tmp_path, monkeypatch):
ledger = tmp_path / "ledger"
_write_json(
ledger / "contaminations.json",
{"version": 1, "classes": {"leak_class": {"artifact_names": ["batch-a"]}}},
)
_write_json(
ledger / "runs" / "exp-glm-dirty" / "contamination.json",
{"classes": {"leak_class": {"matched_artifact_names": ["batch-g"]}}},
)
_write_json(
ledger / "baselines.json",
{
"qwen": {
"current_best": {
"label": "qwen-base",
"artifact": "/data/artifacts/batch-a",
"resolved": 1,
"total": 10,
}
},
"glm": {
"current_best": {
"label": "glm-base",
"exp_id": "exp-glm-dirty",
"resolved": 2,
"total": 10,
}
},
},
)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_git_repo(handoff)
status_mod = _load_script("exp_status")
status = status_mod.collect_status(source, handoff)
quarantine_warnings = sorted(
item for item in status["warnings"] if "quarantined" in item
)
assert quarantine_warnings == [
"glm baseline is quarantined (leak_class); re-baseline on clean runs",
"qwen baseline is quarantined (leak_class); re-baseline on clean runs",
]
def test_status_no_quarantine_warning_for_clean_baselines(tmp_path, monkeypatch):
ledger = tmp_path / "ledger"
_write_json(
ledger / "contaminations.json",
{"version": 1, "classes": {"leak_class": {"artifact_names": ["batch-a"]}}},
)
_write_json(
ledger / "baselines.json",
{
"qwen": {
"current_best": {
"label": "qwen-base",
"artifact": "/data/artifacts/batch-clean",
"exp_id": "exp-clean",
"resolved": 1,
"total": 10,
}
}
},
)
monkeypatch.setenv("OPENSQUILLA_SWE_EXPERIMENT_LEDGER_ROOT", str(ledger))
source = tmp_path / "source"
handoff = tmp_path / "handoff"
_git_repo(source)
_git_repo(handoff)
status_mod = _load_script("exp_status")
status = status_mod.collect_status(source, handoff)
assert not [item for item in status["warnings"] if "quarantined" in item]
@@ -0,0 +1,67 @@
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
def _load_script_module():
path = Path("scripts/live_meta_skill_creator_e2e.py")
spec = importlib.util.spec_from_file_location("live_meta_skill_creator_e2e", path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_live_meta_skill_creator_script_runs_full_flow_with_stub_llm(
tmp_path: Path,
monkeypatch,
) -> None:
module = _load_script_module()
def stub_slots(_prompt: str, **_kwargs) -> str:
return json.dumps({
"name": "script-history-summary",
"description": (
"Script live harness fixture that reads recent history and "
"summarizes the operational pattern."
),
"meta_priority": 50,
"triggers": ["script history summary"],
"steps": [
{
"id": "history",
"skill": "history-explorer",
"task": "read recent history",
"with_keys": {},
},
{
"id": "summary",
"skill": "summarize",
"task": "summarize history",
"with_keys": {},
},
],
})
monkeypatch.setattr(module.proposer, "_call_llm_for_slots", stub_slots)
out = module.run_live_meta_skill_creator_e2e(
home=tmp_path / "home",
model="stub-live-model",
provider="stub-provider",
auto_enable=True,
auto_enable_max_risk="low",
)
assert out["ok"] is True
assert out["llm_slots"]["name"] == "script-history-summary"
assert out["lint"]["G1"]["passed"] is True
assert out["lint"]["G2"]["passed"] is True
assert out["smoke"]["G3"]["passed"] is True
assert out["smoke"]["G4"]["passed"] is True
assert out["persist"]["auto_enable"]["status"] == "enabled"
assert out["managed"] == ["script-history-summary"]
assert out["pending"] == []
@@ -0,0 +1,107 @@
from __future__ import annotations
from collections.abc import AsyncIterator
from pathlib import Path
def test_live_soft_activation_harness_observes_model_meta_invoke(tmp_path: Path) -> None:
from opensquilla.provider.types import DoneEvent as ProviderDoneEvent
from opensquilla.provider.types import ToolUseDeltaEvent as ProviderToolUseDelta
from opensquilla.provider.types import ToolUseEndEvent as ProviderToolUseEnd
from opensquilla.provider.types import ToolUseStartEvent as ProviderToolUseStart
from scripts.live_meta_soft_activation_e2e import (
run_live_meta_soft_activation_e2e,
)
class _StubProvider:
provider_name = "stub"
async def chat(
self,
messages,
tools=None,
config=None,
) -> AsyncIterator:
assert tools is not None
assert any(tool.name == "meta_invoke" for tool in tools)
yield ProviderToolUseStart(tool_use_id="tu_1", tool_name="meta_invoke")
yield ProviderToolUseDelta(
tool_use_id="tu_1",
json_fragment='{"name": "meta-live-soft-activation"}',
)
yield ProviderToolUseEnd(
tool_use_id="tu_1",
tool_name="meta_invoke",
arguments={"name": "meta-live-soft-activation"},
)
yield ProviderDoneEvent(stop_reason="tool_use")
async def list_models(self):
return []
result = run_live_meta_soft_activation_e2e(
home=tmp_path,
provider_instance=_StubProvider(),
model="stub",
classify_override="LIVE_OK",
)
assert result["ok"] is True
assert result["model_decision"]["meta_invoke_called"] is True
assert result["model_decision"]["selected_meta_skill"] == "meta-live-soft-activation"
assert "meta-step:classify" in result["observed_tool_results"]
assert result["expected_output"] in result["final_text"]
def test_live_soft_activation_harness_runs_multiple_model_decision_cases(
tmp_path: Path,
) -> None:
from opensquilla.provider.types import DoneEvent as ProviderDoneEvent
from opensquilla.provider.types import ToolUseEndEvent as ProviderToolUseEnd
from opensquilla.provider.types import ToolUseStartEvent as ProviderToolUseStart
from scripts.live_meta_soft_activation_e2e import run_live_meta_activation_cases
class _CaseProvider:
provider_name = "stub"
def __init__(self) -> None:
self.calls = 0
async def chat(self, messages, tools=None, config=None) -> AsyncIterator:
self.calls += 1
if self.calls == 1:
yield ProviderToolUseStart(tool_use_id="tu_1", tool_name="meta_invoke")
yield ProviderToolUseEnd(
tool_use_id="tu_1",
tool_name="meta_invoke",
arguments={"name": "meta-live-soft-activation"},
)
yield ProviderDoneEvent(stop_reason="tool_use")
else:
yield ProviderDoneEvent(stop_reason="end_turn")
async def list_models(self):
return []
result = run_live_meta_activation_cases(
home=tmp_path,
provider_instance=_CaseProvider(),
cases=[
{
"name": "positive",
"user_message": "Run the live soft activation workflow.",
"expected_meta_skill": "meta-live-soft-activation",
},
{
"name": "negative",
"user_message": "Answer normally without a meta-skill.",
"expected_meta_skill": None,
},
],
model="stub",
classify_override="LIVE_OK",
)
assert result["ok"] is True
assert [case["passed"] for case in result["cases"]] == [True, True]
assert result["summary"] == {"passed": 2, "failed": 0, "total": 2}
@@ -0,0 +1,83 @@
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
def _load_script_module():
path = Path("scripts/meta_skill_validation_matrix.py")
spec = importlib.util.spec_from_file_location("meta_skill_validation_matrix", path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_meta_skill_validation_matrix_materials_are_present() -> None:
module = _load_script_module()
result = module.check_materials(module.load_cases())
assert result["ok"] is True
assert len(result["cases"]) >= 10
assert all(not row["missing"] for row in result["cases"])
def test_meta_skill_validation_matrix_writes_judge_bundle_template(
tmp_path: Path,
) -> None:
module = _load_script_module()
out = tmp_path / "bundle.json"
result = module.write_empty_bundle("B2_pdf_intelligence", out)
assert result == {"ok": True, "bundle": str(out)}
bundle = json.loads(out.read_text(encoding="utf-8"))
assert bundle["case_id"] == "B2_pdf_intelligence"
assert bundle["skill_name"] == "meta-pdf-intelligence"
assert "router-evaluation-summary.pdf" in "\n".join(bundle["materials"])
assert bundle["selected_meta_skill"] == ""
assert bundle["step_trace"] == []
def test_meta_skill_validation_matrix_writes_live_smoke_bundles(
tmp_path: Path,
) -> None:
module = _load_script_module()
result = {
"soft_activation": {
"model_decision": {"selected_meta_skill": "meta-live-soft-activation"},
"observed_tool_results": ["meta-step:classify", "meta_invoke"],
"meta_invoke_result": "meta-skill completed",
"final_text": "LIVE_OK",
"cases": [{"errors": []}],
},
"creator": {
"llm_slots": {
"name": "decision-history-summary",
"triggers": ["show decision history"],
},
"lint": {"G1": {"passed": True}},
"smoke": {"G3": {"passed": True}},
"persist": {
"proposal_id": "abc123",
"auto_enable": {"skill_path": str(tmp_path / "skill")},
},
},
}
written = module.write_live_smoke_bundles(result, tmp_path)
assert [row["case_id"] for row in written] == [
"A1_live_soft_activation",
"C4_live_meta_skill_creator_history_summary",
]
soft = json.loads((tmp_path / "A1_live_soft_activation.bundle.json").read_text())
assert soft["selected_meta_skill"] == "meta-live-soft-activation"
assert soft["step_trace"] == [{"step_id": "classify", "status": "ok"}]
creator = json.loads(
(tmp_path / "C4_live_meta_skill_creator_history_summary.bundle.json").read_text()
)
assert creator["selected_meta_skill"] == "meta-skill-creator"
assert creator["artifacts"][0]["type"] == "proposal"
@@ -0,0 +1,41 @@
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
def _load_script_module():
path = Path("scripts/meta_trigger_accuracy.py")
spec = importlib.util.spec_from_file_location("meta_trigger_accuracy", path)
assert spec is not None
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_meta_trigger_accuracy_script_loads_fixture_cases(tmp_path: Path) -> None:
module = _load_script_module()
fixtures = tmp_path / "cases.json"
fixtures.write_text(
json.dumps([
{
"name": "hit",
"user_message": "run the alpha report",
"expected_meta_skill": "meta-alpha",
},
{
"name": "none",
"user_message": "normal question",
"expected_meta_skill": None,
},
]),
encoding="utf-8",
)
cases = module.load_cases(fixtures)
assert [c.name for c in cases] == ["hit", "none"]
assert cases[0].expected_meta_skill == "meta-alpha"
assert cases[1].expected_meta_skill is None
@@ -0,0 +1,178 @@
"""Offline tests for scripts/refresh_models_dev_snapshot.py.
The transform (``build_snapshot_providers``) and the integrity guards
(``check_snapshot_integrity``) are pure, so everything here runs against
synthetic payloads — no network, no models.dev fetch.
"""
from __future__ import annotations
import json
from scripts.refresh_models_dev_snapshot import (
MAX_SHRINK_RATIO,
SNAPSHOT_PATH,
_trim_model,
build_snapshot_providers,
check_snapshot_integrity,
)
# Synthetic models.dev api.json payload: two providers, three models —
# one with flat costs, one with tiered-only costs (flat keys omitted),
# one self-contradictory (context < output → dropped).
_FAKE_API: dict = {
"acme": {
"models": {
"Acme-Priced": {
"limit": {"context": 200_000, "output": 32_000},
"reasoning": True,
"tool_call": True,
"modalities": {"input": ["text", "image"]},
"cost": {
"input": 2.5,
"output": 10.0,
"cache_read": 0.25,
"cache_write": 3.125,
},
},
"acme-contradictory": {
"limit": {"context": 4_096, "output": 8_192},
"tool_call": True,
"cost": {"input": 1.0, "output": 2.0},
},
}
},
"budget": {
"models": {
"budget-tiered": {
"limit": {"context": 100_000, "output": 8_000},
"cost": {
"input": [{"up_to": 128_000, "cost": 1.0}],
"output": {"tiers": [{"up_to": 128_000, "cost": 4.0}]},
},
}
}
},
}
_FAKE_SOURCES = {"acme_cloud": ("acme",), "budget_cloud": ("budget",)}
def _snapshot(tables: dict) -> dict:
return {"providers": tables}
# ---------------------------------------------------------------------------
# Transform (build_snapshot_providers / _trim_model)
# ---------------------------------------------------------------------------
def test_transform_emits_flat_costs_and_keeps_drop_rules() -> None:
providers = build_snapshot_providers(_FAKE_API, _FAKE_SOURCES)
assert set(providers) == {"acme_cloud", "budget_cloud"}
# Model ids are lowercased; the contradictory entry is dropped.
assert set(providers["acme_cloud"]) == {"acme-priced"}
assert providers["acme_cloud"]["acme-priced"] == {
"ctx": 200_000,
"out": 32_000,
"reasoning": True,
"tools": True,
"vision": True,
"in_mtok": 2.5,
"out_mtok": 10.0,
"cr_mtok": 0.25,
"cw_mtok": 3.125,
}
# Tiered-only pricing: capability shape is emitted, flat cost keys are not.
assert providers["budget_cloud"]["budget-tiered"] == {
"ctx": 100_000,
"out": 8_000,
"reasoning": False,
"tools": False,
"vision": False,
}
def test_trim_model_vendors_only_flat_nonnegative_leaf_costs() -> None:
trimmed = _trim_model(
{
"limit": {"context": 50_000, "output": 5_000},
"cost": {
"input": 0.5, # flat leaf → vendored
"output": {"tiered": True}, # nested → ignored
"cache_read": -1.0, # negative garbage → ignored
"cache_write": True, # boolean → not a price, ignored
},
}
)
assert trimmed == {
"ctx": 50_000,
"out": 5_000,
"reasoning": False,
"tools": False,
"vision": False,
"in_mtok": 0.5,
}
def test_trim_model_without_cost_block_emits_no_cost_keys() -> None:
trimmed = _trim_model({"limit": {"context": 8_192, "output": 4_096}, "tool_call": True})
assert trimmed is not None
assert set(trimmed) == {"ctx", "out", "reasoning", "tools", "vision"}
# ---------------------------------------------------------------------------
# Integrity guards (check_snapshot_integrity)
# ---------------------------------------------------------------------------
def test_shrink_guard_fails_below_the_floor() -> None:
old = _snapshot({"a": {f"m{i}": {} for i in range(10)}})
new = _snapshot({"a": {f"m{i}": {} for i in range(7)}})
errors = check_snapshot_integrity(new, old, required_provider_ids=[])
assert len(errors) == 1
assert "7" in errors[0] and "10" in errors[0]
def test_shrink_guard_allows_exactly_the_floor() -> None:
assert MAX_SHRINK_RATIO == 0.8
old = _snapshot({"a": {f"m{i}": {} for i in range(10)}})
new = _snapshot({"a": {f"m{i}": {} for i in range(8)}})
assert check_snapshot_integrity(new, old, required_provider_ids=[]) == []
def test_regression_guard_flags_a_lost_required_table() -> None:
old = _snapshot({"a": {"m": {}}, "b": {"m": {}}})
new = _snapshot({"a": {"m": {}, "m2": {}}}) # same total, but "b" vanished
errors = check_snapshot_integrity(new, old, required_provider_ids=["a", "b"])
assert len(errors) == 1
assert "'b'" in errors[0]
def test_regression_guard_ignores_providers_the_committed_snapshot_lacks() -> None:
old = _snapshot({"a": {"m": {}}})
new = _snapshot({"a": {"m": {}}})
assert check_snapshot_integrity(new, old, required_provider_ids=["a", "brand-new"]) == []
def test_empty_committed_snapshot_never_blocks_a_first_write() -> None:
new = _snapshot({"a": {"m": {}}})
assert check_snapshot_integrity(new, {}, required_provider_ids=["a"]) == []
def test_committed_snapshot_passes_its_own_integrity_check() -> None:
# Identity comparison over the real committed snapshot, exercising the
# registry-derived default for required provider ids — still offline.
committed = json.loads(SNAPSHOT_PATH.read_text(encoding="utf-8"))
assert check_snapshot_integrity(committed, committed) == []
@@ -0,0 +1,237 @@
"""Tests for scripts/experiments/replay_finalize_gate.py live-parity reporting.
The replay harness must mirror the live gate's firing condition: the gate
only evaluates on a zero-tool-call assistant message (a finalize attempt),
so transcripts that end mid-loop are reported quiet with an explicit
suppression reason while keeping the raw triggers for diagnostics.
"""
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
from typing import Any
def _load_script():
path = Path(__file__).parents[2] / "scripts" / "experiments" / "replay_finalize_gate.py"
spec = importlib.util.spec_from_file_location("replay_finalize_gate", path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _assistant_tool_call(call_id: str, name: str, arguments: dict[str, Any]) -> dict[str, Any]:
return {
"message": {
"role": "assistant",
"content": [
{"type": "toolCall", "id": call_id, "name": name, "arguments": arguments}
],
}
}
def _tool_result(
call_id: str,
name: str,
text: str,
*,
is_error: bool = False,
execution_status: dict[str, Any] | None = None,
) -> dict[str, Any]:
message: dict[str, Any] = {
"role": "toolResult",
"toolName": name,
"toolCallId": call_id,
"isError": is_error,
"content": text,
}
if execution_status is not None:
message["executionStatus"] = execution_status
return {"message": message}
_RED_EXEC_ENTRIES = [
_assistant_tool_call("w1", "edit_file", {"path": "src/main.py"}),
_tool_result("w1", "edit_file", "edited"),
_assistant_tool_call("x1", "exec_command", {"command": "python repro.py"}),
_tool_result(
"x1",
"exec_command",
"exit_code=1\nFAILED: assertion did not hold",
is_error=True,
execution_status={
"version": 1,
"status": "error",
"exit_code": 1,
"timed_out": False,
"reason": "nonzero_exit",
},
),
]
_FINAL_TEXT_ENTRY = {
"message": {
"role": "assistant",
"content": [{"type": "text", "text": "The fix is complete."}],
}
}
def _write_run_dir(
tmp_path: Path,
entries: list[dict[str, Any]],
*,
git_patch: str | None = "diff --git a/src/main.py b/src/main.py\n+new\n",
) -> Path:
run_dir = tmp_path / "run"
run_dir.mkdir()
(run_dir / "transcript.jsonl").write_text(
"".join(json.dumps(entry, ensure_ascii=False) + "\n" for entry in entries),
encoding="utf-8",
)
if git_patch is not None:
(run_dir / "git.patch").write_text(git_patch, encoding="utf-8")
return run_dir
def test_replay_fires_on_finalize_attempt_with_red_evidence(tmp_path) -> None:
module = _load_script()
run_dir = _write_run_dir(tmp_path, [*_RED_EXEC_ENTRIES, _FINAL_TEXT_ENTRY])
report = module.replay_run(run_dir)
assert report["finalize_attempt_at_end"] is True
assert report["should_challenge"] is True
assert report["triggers"] == ["red_execution_after_final_edit"]
assert report["has_workspace_diff_source"] == "git.patch"
def test_replay_suppresses_transcript_ending_mid_loop(tmp_path) -> None:
# Same evidence, but the transcript ends on a tool-call assistant message
# (iteration cap / abort): the live gate would never have evaluated.
module = _load_script()
run_dir = _write_run_dir(tmp_path, list(_RED_EXEC_ENTRIES))
report = module.replay_run(run_dir)
assert report["finalize_attempt_at_end"] is False
assert report["should_challenge"] is False
assert report["suppressed_reason"] == "no_finalize_attempt_at_transcript_end"
# Raw triggers stay visible for diagnostics.
assert report["triggers"] == ["red_execution_after_final_edit"]
def test_replay_quiet_without_workspace_diff(tmp_path) -> None:
module = _load_script()
run_dir = _write_run_dir(
tmp_path, [*_RED_EXEC_ENTRIES, _FINAL_TEXT_ENTRY], git_patch=""
)
report = module.replay_run(run_dir)
assert report["has_workspace_diff"] is False
assert report["should_challenge"] is False
assert report["triggers"] == []
def test_replay_denied_execution_is_not_red_evidence(tmp_path) -> None:
# Live/replay parity for the non-verification skip: a policy-denied
# command after a green verification must not challenge (live
# defect: sandbox denials counted as trailing reds).
module = _load_script()
entries = [
_assistant_tool_call("w1", "edit_file", {"path": "src/main.py"}),
_tool_result("w1", "edit_file", "edited"),
_assistant_tool_call("x1", "exec_command", {"command": "pytest tests/test_main.py"}),
_tool_result(
"x1",
"exec_command",
"exit_code=0\n4 passed",
execution_status={
"version": 1,
"status": "success",
"exit_code": 0,
"timed_out": False,
"reason": None,
},
),
_assistant_tool_call(
"x2",
"exec_command",
{"command": "javac -cp /opt/gradle/caches Probe.java"},
),
_tool_result(
"x2",
"exec_command",
'{"status": "policy_denied", "message": "blocked by sandbox policy"}',
is_error=True,
execution_status={
"version": 1,
"status": "error",
"exit_code": None,
"timed_out": False,
"reason": "denied",
},
),
_FINAL_TEXT_ENTRY,
]
run_dir = _write_run_dir(tmp_path, entries)
report = module.replay_run(run_dir)
assert report["finalize_attempt_at_end"] is True
assert report["should_challenge"] is False
assert report["triggers"] == []
def test_replay_approval_retry_keeps_arguments_for_second_result(tmp_path) -> None:
# The approval flow emits TWO toolResult messages for the same call id:
# first ``approval_pending`` (no outcome), then the real retried result.
# The retried result must still see the original arguments — losing them
# would drop the command and silently erase genuine red evidence.
module = _load_script()
entries = [
_assistant_tool_call("w1", "edit_file", {"path": "src/main.py"}),
_tool_result("w1", "edit_file", "edited"),
_assistant_tool_call("x1", "exec_command", {"command": "python repro.py"}),
_tool_result(
"x1",
"exec_command",
"Approval requested; awaiting decision",
is_error=True,
execution_status={
"version": 1,
"status": "error",
"exit_code": None,
"timed_out": False,
"reason": "approval_pending",
},
),
_tool_result(
"x1",
"exec_command",
"exit_code=1\nFAILED: assertion did not hold",
is_error=True,
execution_status={
"version": 1,
"status": "error",
"exit_code": 1,
"timed_out": False,
"reason": "nonzero_exit",
},
),
_FINAL_TEXT_ENTRY,
]
run_dir = _write_run_dir(tmp_path, entries)
report = module.replay_run(run_dir)
assert report["finalize_attempt_at_end"] is True
assert report["should_challenge"] is True
assert report["triggers"] == ["red_execution_after_final_edit"]
@@ -0,0 +1,65 @@
from __future__ import annotations
import ast
import importlib.util
import sys
import tomllib
from pathlib import Path
from opensquilla.tools.registry import ToolProfile
EXPECTED_ROUTER_MODELS = {
"c0": "deepseek/deepseek-v4-flash",
"c1": "deepseek/deepseek-v4-pro",
"c2": "z-ai/glm-5.2",
"c3": "anthropic/claude-opus-4.8",
}
def _load_smoke_module():
script_path = Path(__file__).resolve().parents[2] / "scripts" / "smoke_v4_phase3_router.py"
spec = importlib.util.spec_from_file_location("smoke_v4_phase3_router", script_path)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def test_smoke_script_tier_defaults_match_router_defaults(tmp_path: Path) -> None:
smoke = _load_smoke_module()
assert {tier: cfg["model"] for tier, cfg in smoke.TIERS.items()} == EXPECTED_ROUTER_MODELS
config_path = tmp_path / "gateway.toml"
smoke._write_live_gateway_config(config_path, "")
data = tomllib.loads(config_path.read_text(encoding="utf-8"))
assert data["llm"]["model"] == EXPECTED_ROUTER_MODELS["c1"]
assert {
tier: cfg["model"]
for tier, cfg in data["squilla_router"]["tiers"].items()
} == EXPECTED_ROUTER_MODELS
def test_live_scripts_use_valid_registry_tool_profile_values() -> None:
valid_profiles = {profile.value for profile in ToolProfile}
script_paths = [
Path("scripts/live_provider_profile_gateway_e2e.py"),
Path("scripts/live_v4_router_evidence.py"),
]
for script_path in script_paths:
tree = ast.parse(script_path.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if not isinstance(node, ast.Assign):
continue
for target in node.targets:
if not (
isinstance(target, ast.Subscript)
and isinstance(target.slice, ast.Constant)
and target.slice.value == "OPENSQUILLA_TOOL_PROFILE"
):
continue
assert isinstance(node.value, ast.Constant), script_path
assert node.value.value in valid_profiles, script_path