phase: 145_git_source_links
**Phase 145 final verification pass — all green, no code changes needed** - **Verified (no defects found):** resolver `origin_for_source` + `_url_basename` (scp/`.git`/trailing-slash, sanitize, ambiguity WARNING); `origin:`/`branch:` lines in all four read shapes via shared `_origin_lines`; HIGH `<tools>` origin rule (verbatim + only-byte-change pins); `DocContent.origin_url/origin_branch` (absent-never-null serializer); `renderDocument` "Original" anchor (copyable verbatim URL, `_blank`/`noopener`, branch in title); mock scripted origin-link flow (disjoint trigger — grep-verified vs all other E2E files) - **Tests:** `uv run pytest --cov=app --cov-report=term-missing` → 3402 passed, `app/` coverage **99%** (>90% gate); `uv run pytest tests/e2e/test_git_source_links.py -v --no-cov` → **4 passed** in isolation; `uv run ruff check .` → clean; `uv run pyright` → 0 errors, 0 warnings - **Completion criteria:** (1) git read results carry origin/branch, local byte-identical — ✅ (unit pins + E2E control); (2) HIGH `<tools>` rule pinned, LOW byte-identical — ✅; (3) modal AND /document.html show Original link, branch in label when pinned, nothing for local — ✅; (4) full suite green + coverage + E2E + lint/types — ✅; (5) no completed-phase behavior change (read header line 1, mock prefix, done frame/SourceRef untouched, LOW sha) — ✅ - **Extra regression check:** re-ran shared-mock E2E suites `test_read_line_ranges.py` (2 passed) and `test_document_viewer.py` (8 passed) — no cross-suite regressions from the `mock_llm.py` addition - **Documentation:** README.md reviewed in full (env table ⊇ `.env.example`, pages/routes, config defaults, scripts, 17-color palette, 300 s guard, 2 s sync poll, phase-145 provenance section matches implementation) and AGENTS.md (72rem container, no-CDN test, smoke test, gitignore, gates) — **verified consistent, no changes needed** - **Notable:** phase-116 screenshot PNG was regenerated during this phase's E2E runs (byte re-encode only, visually identical, correct no-anchor local-source meta row) — left in tree for the harness commit - **Next pending phase:** `146_search_tool_scope` (then 147, 148, 149)
This commit is contained in:
@@ -179,3 +179,124 @@ def test_alias_env_fallback_unchanged(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
|
||||
assert urls == ["https://env.example/one.git"]
|
||||
assert origin == "env"
|
||||
|
||||
|
||||
# --- phase 145 (task 01): origin_for_source (the read result's origin) ---
|
||||
|
||||
|
||||
class _GitOnlySession:
|
||||
"""The origin resolver's session (phase 145, task 01): emulates the
|
||||
resolver's ``kind = 'git'`` WHERE clause — only the git rows ever
|
||||
reach the caller, exactly like the real query; the seeded local
|
||||
rows prove they are never candidates."""
|
||||
|
||||
def __init__(self, rows: list[GitSource]) -> None:
|
||||
self._rows = [row for row in rows if row.kind == "git"]
|
||||
self.statements: list[Any] = []
|
||||
|
||||
def scalars(self, stmt: Any) -> _FakeScalars:
|
||||
self.statements.append(stmt)
|
||||
return _FakeScalars(self._rows)
|
||||
|
||||
|
||||
# --- the basename mapping --------------------------------------------------
|
||||
|
||||
|
||||
def test_origin_basename_mapping() -> None:
|
||||
"""The URL's path component — after the ``://``, or after the ``:``
|
||||
for scp-style ``git@host:owner/repo`` — trailing ``/`` stripped,
|
||||
a single trailing ``.git`` suffix stripped, last segment taken. An
|
||||
``@`` inside the PATH is not userinfo and stays (no split, no
|
||||
strip)."""
|
||||
cases = {
|
||||
"https://github.com/owner/myrepo.git": "myrepo", # the .git suffix
|
||||
"https://github.com/owner/myrepo": "myrepo", # no suffix, same dir
|
||||
"https://github.com/owner/myrepo/": "myrepo", # trailing slash
|
||||
"https://github.com/owner/myrepo.git/": "myrepo", # both
|
||||
"git@github.com:owner/myrepo.git": "myrepo", # scp-style + suffix
|
||||
"git@github.com:owner/myrepo": "myrepo", # scp-style, no suffix
|
||||
"ssh://git@host/owner/myrepo.git": "myrepo", # ssh scheme, @ in the host part
|
||||
"https://host/a@b/myrepo.git": "myrepo", # @ inside the PATH — not userinfo
|
||||
}
|
||||
for url, expected in cases.items():
|
||||
assert resolver._url_basename(url) == expected, url
|
||||
|
||||
|
||||
# --- the match: sanitize + branch ------------------------------------------
|
||||
|
||||
|
||||
def test_origin_exact_match_is_sanitized() -> None:
|
||||
"""EXACTLY ONE match → the phase-121 masked URL + the branch — a
|
||||
legacy embedded-credential row never leaks its secret (the stored
|
||||
value is untouched; the OUTPUT mask does the work)."""
|
||||
session = _GitOnlySession(
|
||||
[GitSource(url="https://user:tok@host/x.git", kind="git", branch=None)]
|
||||
)
|
||||
|
||||
assert resolver.origin_for_source(session, "x") == ("https://host/x.git", None) # pyright: ignore[reportArgumentType]
|
||||
|
||||
|
||||
def test_origin_branch_passthrough() -> None:
|
||||
"""The branch rides through as-is: pinned → the name (the read
|
||||
result's ``branch:`` line), NULL → ``None`` (the default branch —
|
||||
no ``branch:`` line)."""
|
||||
session = _GitOnlySession(
|
||||
[GitSource(url="https://host/x.git", kind="git", branch="release/1.2")]
|
||||
)
|
||||
|
||||
assert resolver.origin_for_source(session, "x") == ("https://host/x.git", "release/1.2") # pyright: ignore[reportArgumentType]
|
||||
|
||||
|
||||
def test_origin_local_rows_never_origins() -> None:
|
||||
"""Local rows are never origins (nothing to link to from the user's
|
||||
browser): a source that exists ONLY as a local row gets no origin
|
||||
(zero git candidates); a local + git pair resolves to the git row."""
|
||||
local = GitSource(url="/srv/repo", kind="local", path="/srv/repo")
|
||||
session = _GitOnlySession([local]) # the WHERE clause drops it
|
||||
assert resolver.origin_for_source(session, "repo") is None # pyright: ignore[reportArgumentType]
|
||||
|
||||
session = _GitOnlySession(
|
||||
[local, GitSource(url="https://host/repo.git", kind="git", branch=None)]
|
||||
)
|
||||
assert resolver.origin_for_source(session, "repo") == ("https://host/repo.git", None) # pyright: ignore[reportArgumentType]
|
||||
|
||||
|
||||
# --- the no-guess rows: None + WARNING --------------------------------------
|
||||
|
||||
|
||||
def test_origin_zero_match_is_none_with_warning(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""No git row's basename matches (a plain local source) → ``None``
|
||||
+ a WARNING naming the source — the read result stays
|
||||
byte-identical to pre-phase."""
|
||||
session = _GitOnlySession([GitSource(url="https://host/other.git", kind="git")])
|
||||
|
||||
assert resolver.origin_for_source(session, "repo") is None # pyright: ignore[reportArgumentType]
|
||||
|
||||
warnings = [record for record in caplog.records if record.levelname == "WARNING"]
|
||||
assert len(warnings) == 1
|
||||
assert "repo" in warnings[0].message
|
||||
|
||||
|
||||
def test_origin_ambiguous_is_none_with_warning(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""TWO-OR-MORE rows share the basename → ``None`` + a WARNING naming
|
||||
the sanitized candidates (never guess — a wrong origin would ride
|
||||
into every answer about the document). The candidates are logged
|
||||
SANITIZED: a legacy embedded credential stays out of the log too."""
|
||||
session = _GitOnlySession(
|
||||
[
|
||||
GitSource(url="https://user:tok@a.example/org/repo.git", kind="git"),
|
||||
GitSource(url="git@b.example:other/repo.git", kind="git"),
|
||||
]
|
||||
)
|
||||
|
||||
assert resolver.origin_for_source(session, "repo") is None # pyright: ignore[reportArgumentType]
|
||||
|
||||
warnings = [record for record in caplog.records if record.levelname == "WARNING"]
|
||||
assert len(warnings) == 1
|
||||
assert "https://a.example/org/repo.git" in warnings[0].message
|
||||
assert "git@b.example:other/repo.git" in warnings[0].message
|
||||
assert "tok" not in warnings[0].message
|
||||
|
||||
Reference in New Issue
Block a user