phase: 145_git_source_links
Build and Push Containers / build-and-push-app (push) Successful in 2m23s
Build and Push Containers / build-and-push-db (push) Successful in 13s

**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:
2026-10-01 11:13:43 -04:00
parent 486ceb4019
commit 32a3ec4f5e
37 changed files with 2856 additions and 22 deletions
+242
View File
@@ -714,6 +714,55 @@ Implements just enough of the aipi surface:
deflected turns never hit it); verified 2026-10-01: no existing
E2E question or fixture file contains the phrase, so every
other suite is unaffected.
- user message containing ``original source link``
(``ORIGIN_LINK_TRIGGER``, phase 145 task 04 — the git-source-
links' dedicated story suite
``tests/e2e/test_git_source_links.py``) **and** the system
prompt carries the ``<tools>`` section -> the deterministic
SCRIPTED ORIGIN-LINK flow (the owner's scenario, TODO.md L7:
the answer grounded in a git-sourced document must carry the
way to the complete original in ONE step): the question
carries its own tool call after the colon — ``original source
link: read source/path`` — the question-carried-call
convention (``_READ_CAP_CALL_RE``), then discriminated
statelessly from the tool results (streaming only):
* request 1 (``tools`` offered, no tool results yet): a
``tool_calls`` delta — ``read`` on the parsed target
(synthetic id ``call_0``);
* a ``tool``-role READ result is in the messages (content
starting with the agent's ``"Document "`` prefix —
``_READ_RESULT_PREFIX``; the phase-145 ``origin:`` /
``branch:`` lines sit BELOW the header, so the first-line
parsing is untouched): the deterministic FINAL answer —
the repository URL VERBATIM, parsed from the read
result's ``origin:`` line BY THE MOCK ITSELF (the mock
cannot fabricate it — the real server's resolver put the
line in the result, so the URL in the answer is the
server-side half's assertion): ``The full source lives at
<url> — see the repo for the complete original.``
(``ORIGIN_LINK_ANSWER``) followed by the house
scripted-turn echo — the read result VERBATIM under
``Here's what the read returned:`` (the rendered output
is the E2E's lens on the ``origin:``/``branch:`` lines'
presence);
* a tool result is in the messages that carries NO
``origin:`` line (a local-sourced document — the byte-
identity control): the deterministic NO-ORIGIN answer —
``No origin line in the read result — the document is not
from a git source.`` (``ORIGIN_LINK_NO_ORIGIN``) + the
same verbatim echo (the suite asserts the line's ABSENCE
through it);
* a tool result is in the messages that is NOT a read
result (a refusal — the premise broke): the
deterministic broken-premise echo — the offending result
VERBATIM under ``Premise broke:`` (the two-ranges
convention — the suite fails loudly on it).
Checked BEFORE the plain ``TOOLS_TRIGGER`` flow (disjoint
trigger phrases — the phase-71/72/94/118/131/139/141 ordering
convention; the trigger needs the ``<tools>`` section, so
deflected turns never hit it); verified 2026-10-01: no
existing E2E question or fixture file contains the phrase, so
every other suite is unaffected.
Failure injection (phase 67, LLM retry, TODO.md L3) — deterministic
dead-endpoint behavior for the retry E2E suite (``tests/e2e/
@@ -2667,6 +2716,148 @@ def _two_ranges_flow(body: dict[str, Any]) -> tuple[str, ...] | None:
return ("read1", target)
# ---------------------------------------------------------------------------
# Phase 145 (task 04): the git-source-links' dedicated story suite — the
# owner's "the answer points at the original git source" scenario. See
# the module docstring.
# ---------------------------------------------------------------------------
#: A user message containing this substring (case-insensitive) —
#: combined with the ``<tools>`` section in the system prompt —
#: drives the deterministic SCRIPTED ORIGIN-LINK flow (the
#: git-source-links' story suite,
#: ``tests/e2e/test_git_source_links.py`` — the owner's scenario,
#: TODO.md L7: the answer grounded in a git-sourced document must
#: carry the way to the complete original in ONE step). Checked
#: BEFORE the plain ``TOOLS_TRIGGER`` flow (disjoint trigger
#: phrases — the phase-71/72/94/118/131/139/141 ordering
#: convention; the trigger needs the ``<tools>`` section, so
#: deflected turns never hit it); verified 2026-10-01: no
#: existing E2E question or fixture file contains the phrase, so
#: every other suite is unaffected.
ORIGIN_LINK_TRIGGER = "original source link"
#: The scripted call in the origin-link question (case-insensitive
#: — the suite's questions capitalize the trigger's first
#: letter): the verb (``read``) plus the target — a combined
#: ``source/path``, parsed from the RAW user message so the target
#: keeps its case. The target is a ``[a-z0-9_./-]`` run (case-
#: insensitively), so the suite's `` — `` flavor separator (em
#: dash) can never bleed into it (the read-cap / drill-down
#: convention, ``_READ_CAP_CALL_RE`` / ``_DRILL_CALL_RE``).
_ORIGIN_LINK_CALL_RE = re.compile(
r"original source link:\s*read\s+(?P<arg>[a-z0-9_./-]+)",
re.I,
)
#: The flow's FINAL answer for a read result that carries the
#: phase-145 ``origin:`` line — the DETERMINISTIC string carrying
#: the repository URL VERBATIM (the model-side contract, owner-
#: confirmed 2026-09-30: the answer includes the original source).
#: ``{url}`` is parsed from the read result's ``origin:`` line BY
#: THE MOCK ITSELF — the mock cannot fabricate it, so the URL in
#: the answer is the server-side half's assertion (the real
#: server's resolver put the line in the result the model saw).
ORIGIN_LINK_ANSWER = (
"The full source lives at {url} — see the repo for the complete original."
)
#: The flow's FINAL answer for a read result that carries NO
#: ``origin:`` line (a local-sourced document — the byte-identity
#: control): the deterministic no-origin sentence (its wording
#: carries NO ``origin:`` substring — the suite's absence
#: assertion keys off the line's exact prefix).
ORIGIN_LINK_NO_ORIGIN = (
"No origin line in the read result — the document is not from a git source."
)
#: The house scripted-turn echo header (the ``READ_CAP_TRIGGER``
#: flow's convention): the read result rides VERBATIM into the
#: answer, so the rendered output is the E2E's lens on the
#: ``origin:``/``branch:`` lines' presence (and their absence in
#: the local-source control).
_ORIGIN_LINK_ECHO_HEADER = "Here's what the read returned:"
def _origin_link_url(result: str) -> str | None:
"""The read result's ``origin:`` line's URL (the phase-145
task-01 line — the sanitized repository URL the real server's
resolver put in the result) — or ``None`` when the result
carries no origin line (a local-sourced document, no git row,
or an ambiguous basename).
The search is position-independent (any line with the exact
``origin: `` prefix): the phase-145 lines sit between the
``date:`` line and the content, but the parse never drifts if
the result's header shape changes. A document BODY line that
merely starts with ``origin: `` would be a fixture defect (the
suite's fixtures are pinned content — no such line), never a
mock defect.
"""
for line in result.splitlines():
if line.startswith("origin: "):
return line[len("origin: "):]
return None
def _origin_link_flow(body: dict[str, Any]) -> tuple[str, ...] | None:
"""Classify a phase-145 scripted origin-link request (see the
module docstring). The question carries the scripted call
(``original source link: read source/path``); the step is then
discriminated statelessly from the tool results, like the
other marker flows:
* ``("call", target, "call_0")`` — ``tools`` are offered and
no ``tool``-role result is in the messages yet: the scripted
``read`` on the parsed target (synthetic id ``call_0``).
* ``("answer", url, result)`` — a ``tool``-role READ result
(content starting with the agent's ``"Document "`` prefix —
``_READ_RESULT_PREFIX``; the phase-145 ``origin:`` /
``branch:`` lines sit BELOW the header, so the first-line
parsing is untouched) is in the messages: the deterministic
FINAL answer — ``url`` the result's ``origin:`` line's URL
(or ``""`` — the no-origin sentence, the local-source
control; the empty-string sentinel keeps the return
homogeneous ``tuple[str, …]``, the ``_read_lines_flow``
convention) and ``result`` the read result VERBATIM (the
house scripted-turn echo — the rendered output is the lens
on the origin lines).
* ``("bad", result)`` — a tool result is in the messages that
is NOT a read result (a refusal — the premise broke): the
deterministic broken-premise echo — the offending result
VERBATIM under ``Premise broke:`` (the two-ranges convention
— the suite fails loudly on it).
* ``None`` — not the flow: the trigger is absent, the
``<tools>`` section is missing (deflected turns never carry
it), the scripted call is unparseable, or ``tools`` are not
offered and no tool results are in the messages yet (e.g.
``agent_max_rounds=0``).
"""
user = _user(body)
if ORIGIN_LINK_TRIGGER not in user.lower():
return None
if "<tools>" not in _system(body):
return None
match = _ORIGIN_LINK_CALL_RE.search(user)
if match is None:
return None
target = match.group("arg")
results = _tool_results(body)
if results:
last = results[-1]
if not last.startswith(_READ_RESULT_PREFIX):
return ("bad", last)
# The empty-string sentinel (never a real URL — a URL is
# always non-empty: the resolver returns the stored URL's
# sanitized form, which is a non-empty string): keeps the
# return homogeneous ``tuple[str, …]`` (pyright), the
# ``_read_lines_flow`` convention.
return ("answer", _origin_link_url(last) or "", last)
if not body.get("tools"):
return None
return ("call", target, "call_0")
def long_answer() -> str:
"""~900-word deterministic walkthrough (phase 11): numbered steps plus
a unique final line that must survive the stream untruncated."""
@@ -4044,6 +4235,57 @@ def chat_completions(body: dict[str, Any]) -> Any:
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
# Phase 145 (task 04): the owner's "the answer points at the
# original git source" scenario (the git-source-links' story
# suite, tests/e2e/test_git_source_links.py — TODO.md L7):
# the scripted read, then the final answer that carries the
# repository URL parsed from the read result's ``origin:``
# line (the server-side half — the mock cannot fabricate it)
# + the result VERBATIM (the lens on the origin lines'
# presence/absence). Checked BEFORE the plain TOOLS_TRIGGER
# flow (disjoint trigger phrases — the phase-71/72/94/118/
# 131/139/141 ordering convention; the trigger needs the
# ``<tools>`` section, so deflected turns never hit it).
origin_link = _origin_link_flow(body)
if origin_link is not None:
if origin_link[0] == "call":
stream = _tool_call_stream(
"read", {"path": origin_link[1]}, origin_link[2]
)
elif origin_link[0] == "bad":
# The premise broke (a refusal — the scripted read
# was refused): echo the offending result verbatim
# so a suite fails loudly on it (the two-ranges
# convention).
stream = _sse_stream(
_apply_max_tokens(
f"Premise broke:\n{origin_link[1]}",
body.get("max_tokens"),
),
0.0,
)
else: # "answer" — the final answer (tests 1–3's model-
# side contract) + the verbatim echo (the lens on
# the origin lines' presence/absence). ``url`` is
# the ``origin:`` line's URL or the "" sentinel
# (the no-origin sentence — the local-source
# control).
url = origin_link[1]
sentence = (
ORIGIN_LINK_ANSWER.format(url=url) if url else ORIGIN_LINK_NO_ORIGIN
)
stream = _sse_stream(
_apply_max_tokens(
f"{sentence}\n{_ORIGIN_LINK_ECHO_HEADER}\n{origin_link[2]}",
body.get("max_tokens"),
),
0.0,
)
return StreamingResponse(
stream,
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
# Phase 124 (task 07, image chips and answers, TODO L3): the
# scripted SHOW_IMAGE flow — the mock shows the seeded image
# document (the ``<documents>`` block with the image-