Skip to content

Dianel555/DSkills / skills/ace-tool

ace-tool

Verified

Semantic codebase search, code indexing, and prompt enhancement via standalone CLI. Use when: (1) Semantic code search with natural language queries, (2) Code indexing for remote codebase retrieval, (3) Prompt enhancement with codebase context, (4) Before grep/find/glob operations for better accuracy, (5) Complex requirements clarification, (6) Large codebase navigation. Triggers: "search context", "enhance prompt", "find code that", "index project", "clarify requirements". IMPORTANT: Always use ace-tool BEFORE grep/find/glob for semantic-level code location.

tests/test_url_and_400error.py

View on GitHub
"""Regression tests for diagnose fixes: URL consistency, unknown-blobs self-heal, cache hierarchy."""

import gzip
import json
import os
import shutil
import sys
from pathlib import Path
from types import SimpleNamespace

import httpx
import pytest

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))

import ace_cli  # noqa: E402
from client import AceToolClient, IndexReadinessError, build_api_url  # noqa: E402
from indexer import Indexer, IndexRebuildError, ProjectIndex  # noqa: E402
from secure_fs import AuthorizedRoot as _AuthorizedRoot  # noqa: E402
from templates import INDEX_DIR, INDEX_FILE  # noqa: E402
from utils import build_api_url as utils_build_api_url  # noqa: E402


def _ready_status():
    return {"unknown_blob_names": [], "nonindexed_blob_names": [], "checkpoint_not_found": False}


def _write_index(root: Path):
    p = root / INDEX_DIR / INDEX_FILE
    p.parent.mkdir(parents=True, exist_ok=True)
    with gzip.open(p, "wt", encoding="utf-8") as f:
        json.dump({"entries": {}, "last_indexed": 1.0}, f)
    return p


# --- Problem 1: URL construction consistency ---


def test_build_api_url_lives_in_utils_and_reexported():
    assert build_api_url is utils_build_api_url


@pytest.mark.parametrize(
    "base,path,expected",
    [
        ("https://api.example.com", "/v1/messages", "https://api.example.com/v1/messages"),
        ("https://api.example.com/v1", "/v1/messages", "https://api.example.com/v1/messages"),
        ("https://proxy.com/v1beta", "/v1/messages", "https://proxy.com/v1beta/messages"),
        ("https://api.example.com/vertex", "/v1/messages", "https://api.example.com/vertex/v1/messages"),
        ("https://api.example.com", "v1/messages", "https://api.example.com/v1/messages"),
        ("https://api.example.com/v1/", "/v1/messages", "https://api.example.com/v1/messages"),
        ("https://h/v1", "/batch-upload", "https://h/v1/batch-upload"),
    ],
)
def test_build_api_url_version_handling(base, path, expected):
    assert build_api_url(base, path) == expected


def test_upload_url_uses_build_api_url(tmp_path, monkeypatch):
    captured = {}

    class FakeResp:
        status_code = 200

        def raise_for_status(self):
            pass

        def json(self):
            return {"blob_names": []}

    class FakeClient:
        def __init__(self, *a, **k):
            pass

        def __enter__(self):
            return self

        def __exit__(self, *a):
            return False

        def post(self, url, **kw):
            captured["url"] = url
            return FakeResp()

    monkeypatch.setattr("indexer.httpx.Client", FakeClient)
    idx = Indexer(str(tmp_path), "https://h/v1", "tok")
    assert idx._upload_batch_with_retry({}, {"blobs": []})
    assert captured["url"] == build_api_url("https://h/v1", "/batch-upload")


# --- Problem 2: 400 unknown blobs self-heal ---


def test_unknown_blobs_triggers_rebuild_and_single_retry(monkeypatch):
    c = AceToolClient(base_url="https://h", token="tok")
    calls = {"post": 0, "rebuild": 0}

    monkeypatch.setattr("client.Indexer.__init__", lambda self, *a, **k: None)
    monkeypatch.setattr("client.Indexer.close", lambda self: None)
    monkeypatch.setattr("client.Indexer.get_blob_names", lambda self: ["stale"])

    def fake_rebuild(self):
        calls["rebuild"] += 1
        return ["fresh"]

    monkeypatch.setattr("client.Indexer.force_rebuild", fake_rebuild, raising=False)

    def fake_post(url, payload, *, headers, provider="API", timeout=None):
        if url.endswith("/agents/blob-status"):
            return _ready_status()
        calls["post"] += 1
        if calls["post"] == 1:
            resp = httpx.Response(400, text="unknown blobs: ['stale']")
            raise httpx.HTTPStatusError("400 Bad Request", request=None, response=resp)
        assert payload["blobs"]["added_blobs"] == ["fresh"]
        return {"formatted_retrieval": "ok"}

    monkeypatch.setattr(c, "_post_json", fake_post)

    result = c._remote_search("/proj", "query")
    assert result["results"] == "ok"
    assert calls["rebuild"] == 1
    assert calls["post"] == 2


def test_unknown_blobs_no_infinite_retry(monkeypatch):
    c = AceToolClient(base_url="https://h", token="tok")
    calls = {"post": 0}

    monkeypatch.setattr("client.Indexer.__init__", lambda self, *a, **k: None)
    monkeypatch.setattr("client.Indexer.close", lambda self: None)
    monkeypatch.setattr("client.Indexer.get_blob_names", lambda self: ["stale"])
    monkeypatch.setattr("client.Indexer.force_rebuild", lambda self: ["fresh"], raising=False)

    def fake_post(url, payload, *, headers, provider="API", timeout=None):
        if url.endswith("/agents/blob-status"):
            return _ready_status()
        calls["post"] += 1
        resp = httpx.Response(400, text="unknown blobs")
        raise httpx.HTTPStatusError("400 Bad Request", request=None, response=resp)

    monkeypatch.setattr(c, "_post_json", fake_post)

    with pytest.raises(IndexReadinessError, match="unknown"):
        c._remote_search("/proj", "query")
    assert calls["post"] == 2


def test_other_400_errors_not_swallowed(monkeypatch):
    c = AceToolClient(base_url="https://h", token="tok")
    calls = {"post": 0, "rebuild": 0}

    monkeypatch.setattr("client.Indexer.__init__", lambda self, *a, **k: None)
    monkeypatch.setattr("client.Indexer.close", lambda self: None)
    monkeypatch.setattr("client.Indexer.get_blob_names", lambda self: ["a"])
    monkeypatch.setattr(
        "client.Indexer.force_rebuild",
        lambda self: calls.__setitem__("rebuild", calls["rebuild"] + 1) or [],
        raising=False,
    )

    def fake_post(url, payload, *, headers, provider="API", timeout=None):
        if url.endswith("/agents/blob-status"):
            return _ready_status()
        calls["post"] += 1
        resp = httpx.Response(400, text="malformed request")
        raise httpx.HTTPStatusError("400 Bad Request", request=None, response=resp)

    monkeypatch.setattr(c, "_post_json", fake_post)

    with pytest.raises(httpx.HTTPStatusError):
        c._remote_search("/proj", "query")
    assert calls["post"] == 1
    assert calls["rebuild"] == 0


# --- Problem 3: cache hierarchy ---


def test_child_keeps_requested_root_despite_ancestor_cache(tmp_path):
    parent = tmp_path / "parent"
    child = parent / "sub"
    child.mkdir(parents=True)
    _write_index(parent)

    idx = Indexer(str(child), "https://h", "tok")
    assert idx.root == child.resolve()
    assert idx.index_path == child.resolve() / INDEX_DIR / INDEX_FILE


def test_own_cache_preferred_over_ancestor(tmp_path):
    parent = tmp_path / "p2"
    child = parent / "sub"
    child.mkdir(parents=True)
    _write_index(parent)
    _write_index(child)

    idx = Indexer(str(child), "https://h", "tok")
    assert idx.root == child.resolve()


def test_no_cache_anywhere_uses_project_root(tmp_path):
    proj = tmp_path / "fresh"
    proj.mkdir()

    idx = Indexer(str(proj), "https://h", "tok")
    assert idx.root == proj.resolve()


def test_multiple_ancestor_caches_do_not_change_requested_root(tmp_path):
    outer = tmp_path / "repo"
    nearest = outer / "packages"
    child = nearest / "service"
    child.mkdir(parents=True)
    _write_index(outer)
    _write_index(nearest)

    idx = Indexer(str(child), "https://h", "tok")
    assert idx.root == child.resolve()


def test_home_cache_not_inherited(tmp_path, monkeypatch):
    monkeypatch.setattr(Path, "home", lambda: tmp_path)
    _write_index(tmp_path)
    proj = tmp_path / "work" / "proj"
    proj.mkdir(parents=True)

    idx = Indexer(str(proj), "https://h", "tok")
    assert idx.root == proj.resolve()


def test_parent_indexing_preserves_child_cache(tmp_path):
    root = tmp_path / "proj"
    sub = root / "pkg"
    sub.mkdir(parents=True)
    (root / "m.py").write_text("a = 1\n", encoding="utf-8")
    (sub / "n.py").write_text("b = 2\n", encoding="utf-8")
    _write_index(root)
    _write_index(sub)

    idx = Indexer(str(root), "", "")  # empty base_url: upload step is a no-op
    names = idx.get_blob_names()

    assert (sub / INDEX_DIR / INDEX_FILE).is_file()
    assert (root / INDEX_DIR / INDEX_FILE).exists()
    assert len(names) == 2


# --- Review fixes: absorb boundaries, retry amplification, CLI transparency ---


def test_child_cache_kept_when_root_never_saves_index(tmp_path):
    root = tmp_path / "empty-root"
    sub = root / "pkg"
    sub.mkdir(parents=True)
    _write_index(sub)  # only a child cache, no eligible source files anywhere

    idx = Indexer(str(root), "", "")
    names = idx.get_blob_names()

    assert names == []
    assert not (root / INDEX_DIR / INDEX_FILE).exists()
    assert (sub / INDEX_DIR / INDEX_FILE).exists()


def test_ignored_subtree_cache_not_absorbed(tmp_path):
    root = tmp_path / "proj2"
    ignored = root / "legacy"
    ignored.mkdir(parents=True)
    (root / "m.py").write_text("a = 1\n", encoding="utf-8")
    (ignored / "old.py").write_text("z = 9\n", encoding="utf-8")
    (root / ".gitignore").write_text("legacy/\n", encoding="utf-8")
    _write_index(ignored)

    idx = Indexer(str(root), "", "")
    names = idx.get_blob_names()

    assert (ignored / INDEX_DIR / INDEX_FILE).exists()  # not covered by parent scan
    assert len(names) == 1  # only m.py


def test_force_rebuild_raises_on_upload_failure(tmp_path, monkeypatch):
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "https://h", "tok")
    monkeypatch.setattr(idx, "_upload_pending", lambda: False)

    with pytest.raises(IndexRebuildError):
        idx.force_rebuild()


def test_rebuild_failure_not_retried_by_enhance(monkeypatch):
    monkeypatch.delenv("PROMPT_ENHANCER_ENDPOINT", raising=False)
    monkeypatch.delenv("ACE_ENHANCER_ENDPOINT", raising=False)
    c = AceToolClient(base_url="https://h", token="tok", endpoint="claude")
    calls = {"n": 0}

    def fake_third_party(prompt, history, project_root=None):
        calls["n"] += 1
        raise IndexRebuildError("rebuild upload failed")

    monkeypatch.setattr(c, "_call_third_party_api", fake_third_party)

    with pytest.raises(IndexRebuildError):
        c.enhance_prompt("p", "", "/proj")
    assert calls["n"] == 1  # ValueError subclass: excluded from tenacity retry


def test_cmd_index_reports_effective_root(tmp_path, monkeypatch, capsys):
    parent = tmp_path / "top"
    child = parent / "sub"
    child.mkdir(parents=True)
    _write_index(parent)

    monkeypatch.setattr(
        ace_cli,
        "AceToolClient",
        lambda *a: SimpleNamespace(
            base_url="https://h", token="tok", _wait_for_index=lambda indexer, names: (names, False)
        ),
    )

    def fake_names(self):
        self._index = ProjectIndex()
        return []

    monkeypatch.setattr(ace_cli.Indexer, "get_blob_names", fake_names)

    ace_cli.cmd_index(SimpleNamespace(api_url=None, token=None, endpoint=None, project_root=str(child)))
    out = json.loads(capsys.readouterr().out)
    assert out["effective_root"] == str(child.resolve())
    assert out["project_root"] == str(child)


# --- Concurrency race: tolerate-and-converge (absorb vs concurrent child save) ---


def test_save_index_tolerates_dir_deleted_mid_write(tmp_path, monkeypatch):
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "", "")
    idx._load_index()
    idx._scan_and_update()

    real_mkdir = Path.mkdir

    def mkdir_then_vanish(self, *a, **k):
        real_mkdir(self, *a, **k)
        shutil.rmtree(self)  # concurrent ancestor absorbs the dir right after mkdir

    monkeypatch.setattr(Path, "mkdir", mkdir_then_vanish)

    assert idx._save_index() is False  # abandoned, not crashed
    assert not idx.index_path.exists()


def test_save_index_returns_true_on_success(tmp_path):
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "", "")
    idx._load_index()
    idx._scan_and_update()

    assert idx._save_index() is True
    assert idx.index_path.is_file()


def test_no_absorb_when_save_abandoned(tmp_path, monkeypatch):
    root = tmp_path / "proj3"
    sub = root / "pkg"
    sub.mkdir(parents=True)
    (root / "m.py").write_text("a = 1\n", encoding="utf-8")
    (sub / "n.py").write_text("b = 2\n", encoding="utf-8")
    _write_index(root)
    _write_index(sub)

    idx = Indexer(str(root), "", "")
    monkeypatch.setattr(idx, "_save_index", lambda: False)  # own cache absorbed by grandparent
    idx.get_blob_names()

    assert (sub / INDEX_DIR / INDEX_FILE).exists()  # no cascading deletion


def test_force_rebuild_skips_absorb_when_save_abandoned(tmp_path, monkeypatch):
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    sub = tmp_path / "pkg"
    sub.mkdir()
    (sub / "n.py").write_text("b = 2\n", encoding="utf-8")
    _write_index(sub)

    idx = Indexer(str(tmp_path), "", "")
    monkeypatch.setattr(idx, "_save_index", lambda: False)
    names = idx.force_rebuild()  # upload is a no-op (empty base_url) -> success

    assert names  # in-memory result still returned
    assert (sub / INDEX_DIR / INDEX_FILE).exists()


def test_parent_and_rebuild_never_remove_child_cache(tmp_path):
    root = tmp_path / "proj4"
    sub = root / "pkg"
    sub.mkdir(parents=True)
    (root / "m.py").write_text("a = 1\n", encoding="utf-8")
    (sub / "n.py").write_text("b = 2\n", encoding="utf-8")
    _write_index(root)
    _write_index(sub)

    idx = Indexer(str(root), "", "")
    names = idx.get_blob_names()
    rebuilt_names = idx.force_rebuild()

    assert (sub / INDEX_DIR / INDEX_FILE).is_file()
    assert len(names) == len(rebuilt_names) == 2


def test_save_index_tmp_is_per_process(tmp_path, monkeypatch):
    captured = {}
    real_publish = _AuthorizedRoot.publish

    def spy_publish(self, tmp_rel, rel_parts):
        captured["tmp"] = tmp_rel[-1]
        return real_publish(self, tmp_rel, rel_parts)

    monkeypatch.setattr(_AuthorizedRoot, "publish", spy_publish)
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "", "")
    idx._load_index()
    idx._scan_and_update()

    assert idx._save_index() is True
    assert captured["tmp"] == f"{INDEX_FILE}.{os.getpid()}.tmp"  # no shared-name torn writes
    assert list((tmp_path / INDEX_DIR).glob("*.tmp")) == []  # renamed away, no litter


def test_save_index_retries_transient_replace_permission_error(tmp_path, monkeypatch):
    attempts = {"n": 0}
    real_publish = _AuthorizedRoot.publish

    def briefly_locked_publish(self, tmp_rel, rel_parts):
        attempts["n"] += 1
        if attempts["n"] == 1:
            raise PermissionError(5, "target open in a concurrent reader")  # WinError 5
        return real_publish(self, tmp_rel, rel_parts)

    monkeypatch.setattr(_AuthorizedRoot, "publish", briefly_locked_publish)
    monkeypatch.setattr("indexer.time.sleep", lambda s: None)
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "", "")
    idx._load_index()
    idx._scan_and_update()

    assert idx._save_index() is True
    assert attempts["n"] == 2
    assert idx.index_path.is_file()


def test_save_index_propagates_persistent_permission_error(tmp_path, monkeypatch):
    def always_locked_publish(self, tmp_rel, rel_parts):
        raise PermissionError(5, "acl denies write")

    monkeypatch.setattr(_AuthorizedRoot, "publish", always_locked_publish)
    monkeypatch.setattr("indexer.time.sleep", lambda s: None)
    (tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
    idx = Indexer(str(tmp_path), "", "")
    idx._load_index()
    idx._scan_and_update()

    with pytest.raises(PermissionError):  # genuine ACL problems must surface
        idx._save_index()
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall