The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: Vertex AI wire-conformance E2E suite (OAuth JWT mint, TLS proxy fake, tools, signatures, errors, compaction, stream drop)
teknium15c69c57f9a36
diff --git a/tests/e2e/core/providers/test_native_vertex_errors.py b/tests/e2e/core/providers/test_native_vertex_errors.pynew file mode 100644index 00000000000..43fac257ff6--- /dev/null+++ b/tests/e2e/core/providers/test_native_vertex_errors.py@@ -0,0 +1,152 @@+"""Vertex AI documented errors through the real CLI: retried per semantics or surfaced exactly once.++Each scenario is one ``hermes chat -q`` turn against its own fake (``tests/fakes/providers/vertex.py``);+all run concurrently. Vertex's OpenAI-compatible endpoint returns google.rpc errors in a list-wrapped+envelope (``[{"error": {"code", "message", "status"}}]``); the fake uses exactly that.++* 429 RESOURCE_EXHAUSTED (quota) and 503 UNAVAILABLE are transient: retried, then the answer.+* 400 INVALID_ARGUMENT and 403 PERMISSION_DENIED are terminal: one request, message shown once.+* 401 UNAUTHENTICATED on every bearer: one token refresh + retry at most, then shown once.+* OAuth ``invalid_grant`` at the token endpoint: nothing is sent to Vertex; actionable error.+"""++from __future__ import annotations++from concurrent.futures import ThreadPoolExecutor+from dataclasses import dataclass, field+from pathlib import Path+from typing import Any++import pytest++pytest.importorskip("google.auth", reason="Vertex minting needs google-auth (CI installs it)")++from tests.e2e.core.providers._native_helpers import ChatResult, make_home, run_chat # noqa: E402+from tests.fakes.providers.vertex import ( # noqa: E402+ PROJECT,+ REGION,+ SA_EMBEDDED_PROJECT,+ Fail,+ FakeVertex,+ Say,+ TokenPolicy,+ hermes_setup,+)++KNOWN: dict[str, str] = {+ "guidance:permission_denied": "#121295 Vertex 401/403 are reported as 'rejected your API key' (Vertex has no API "+ "key; the fix is the service account / IAM role)",+ "guidance:unauthenticated": "#121295 Vertex 401/403 are reported as 'rejected your API key' (Vertex has no API "+ "key; the fix is the service account / IAM role)",+}++QUOTA_MSG = ("Resource exhausted. Please try again later. Please refer to "+ "https://cloud.google.com/vertex-ai/generative-ai/docs/error-code-429 for more details.")+UNAVAILABLE_MSG = "The service is currently unavailable."+INVALID_MSG = "Request contains an invalid argument."+DENIED_MSG = (f"Permission 'aiplatform.endpoints.predict' denied on resource '//aiplatform.googleapis.com/projects/"+ f"{PROJECT}/locations/{REGION}/publishers/google/models/gemini-3-flash-preview' (or it may not exist).")+UNAUTH_FRAGMENT = "Request had invalid authentication credentials"+++@dataclass+class Case:+ script: list[Any]+ policy: TokenPolicy = field(default_factory=TokenPolicy)+ answer: str | None = None # the final answer when the error is transient+ max_attempts: int = 2 # agent.api_max_retries (total attempts per API call)+++CASES: dict[str, Case] = {+ "rate_limited": Case([Fail(429, QUOTA_MSG), Fail(429, QUOTA_MSG), Say("Answer after the quota recovered.")],+ answer="Answer after the quota recovered.", max_attempts=3),+ "unavailable": Case([Fail(503, UNAVAILABLE_MSG), Say("Answer after UNAVAILABLE.")], answer="Answer after UNAVAILABLE."),+ "invalid_argument": Case([Fail(400, INVALID_MSG)]),+ "permission_denied": Case([Fail(403, DENIED_MSG)]),+ "unauthenticated": Case([Say("never served")], TokenPolicy(reject_bearers=True)),+ "invalid_grant": Case([Say("never served")], TokenPolicy(error=(400, "invalid_grant", "Invalid JWT Signature."))),+}+++def _run(tmp: Path, name: str, case: Case) -> dict[str, Any]:+ fake = FakeVertex(tmp / name / "fake", project=PROJECT, region=REGION, sa_project=SA_EMBEDDED_PROJECT,+ script=list(case.script))+ fake.start()+ fake.token_policy = case.policy+ nh = make_home(tmp / name / "h", **hermes_setup(fake, extra_config={"agent": {"api_max_retries": case.max_attempts}}))+ return {"fake": fake, "nh": nh, "turn": run_chat(nh, "Say hello.", env=fake.child_env(), args=("-t", "file"))}++[email protected](scope="module")+def results(tmp_path_factory: pytest.TempPathFactory) -> Any:+ tmp = tmp_path_factory.mktemp("vertex_errors")+ with ThreadPoolExecutor(max_workers=len(CASES)) as pool:+ futures = {name: pool.submit(_run, tmp, name, case) for name, case in CASES.items()}+ out = {name: f.result() for name, f in futures.items()}+ yield out+ for res in out.values():+ res["fake"].stop()+++def _output(turn: ChatResult) -> str:+ return turn.stdout + turn.stderr++[email protected]("name", ["rate_limited", "unavailable"])+def test_transient_error_is_retried_then_answers(results: dict[str, Any], name: str) -> None:+ res, case = results[name], CASES[name]+ fake, turn = res["fake"], res["turn"]+ assert turn.returncode == 0 and case.answer in turn.stdout, turn.describe()+ statuses = [r.get("status") for r in fake.requests]+ assert statuses == [f.status for f in case.script if isinstance(f, Fail)] + [200], statuses+ bodies = [r["body"]["messages"] for r in fake.requests]+ assert all(b == bodies[0] for b in bodies), "a retry changed the conversation it resent"+ assert len({r["auth"] for r in fake.requests}) == 1 and len(fake.token_requests) == 1, "retry re-minted needlessly"++[email protected](("name", "vendor_text"), [("invalid_argument", INVALID_MSG), ("permission_denied", DENIED_MSG)])+def test_terminal_error_surfaced_once_without_retry(results: dict[str, Any], name: str, vendor_text: str) -> None:+ res = results[name]+ fake, turn = res["fake"], res["turn"]+ assert len(fake.requests) == 1, f"terminal {name} was retried: {[r.get('status') for r in fake.requests]}"+ assert turn.returncode != 0, turn.describe()+ assert _output(turn).count(vendor_text) == 1, f"vendor message not surfaced exactly once:\n{turn.describe()}"+++def test_rejected_bearer_refreshes_once_then_surfaces(results: dict[str, Any]) -> None:+ """Every bearer 401s: Hermes may re-mint and retry once, never loop, and shows the error once."""+ res = results["unauthenticated"]+ fake, turn = res["fake"], res["turn"]+ statuses = [r.get("status") for r in fake.requests]+ assert statuses and set(statuses) == {401} and len(statuses) <= 2, statuses+ assert turn.returncode != 0+ assert _output(turn).count(UNAUTH_FRAGMENT) == 1, turn.describe()+++def test_oauth_invalid_grant_sends_nothing_and_names_the_credential(results: dict[str, Any]) -> None:+ """The SA key is refused at Google's token endpoint: no Vertex request goes out with a missing+ bearer, and the user is told which credential setting to fix."""+ res = results["invalid_grant"]+ fake, turn = res["fake"], res["turn"]+ assert fake.token_requests and all(t.get("error") == "invalid_grant" for t in fake.token_requests)+ assert fake.token_requests[0]["claims"], "the JWT assertion itself failed verification"+ assert not fake.requests, f"requests reached Vertex without a token: {[r['auth'][:16] for r in fake.requests]}"+ assert turn.returncode != 0+ out = _output(turn)+ assert "VERTEX_CREDENTIALS_PATH" in out or "GOOGLE_APPLICATION_CREDENTIALS" in out, turn.describe()+++def _guidance_param(name: str) -> Any:+ return pytest.param(name, marks=pytest.mark.xfail(strict=True, raises=AssertionError, reason=KNOWN[f"guidance:{name}"]))++[email protected]("name", [_guidance_param("permission_denied"), _guidance_param("unauthenticated")])+def test_auth_failure_guidance_is_vertex_specific(results: dict[str, Any], name: str) -> None:+ """Vertex authenticates with an OAuth service account, so the guidance must not send the user to+ rotate an 'API key' that does not exist."""+ res = results[name]+ turn = res["turn"]+ if not (res["fake"].requests and turn.returncode != 0):+ raise RuntimeError(f"{name}: the auth failure never happened:\n{turn.describe()}")+ guidance = _output(turn).split("Provider said:")[0].lower()+ assert "api key" not in guidance, f"Vertex auth failure blamed on an API key:\n{turn.stdout}"diff --git a/tests/e2e/core/providers/test_native_vertex_recovery.py b/tests/e2e/core/providers/test_native_vertex_recovery.pynew file mode 100644index 00000000000..7eff31dfc95--- /dev/null+++ b/tests/e2e/core/providers/test_native_vertex_recovery.py@@ -0,0 +1,169 @@+"""Vertex AI recovery paths: compaction in a signed reasoning session, and a mid-stream drop.++Real ``hermes chat -q`` turns against the fake Vertex (``tests/fakes/providers/vertex.py``), which+rejects exactly what Gemini 3 rejects after history surgery: orphaned tool results, unanswered+calls, and a current-turn function call without its thought signature (or with a signature it+never issued). Scenarios run concurrently in a module fixture:++* ``compaction`` — ``--reasoning high``, every step a signed ``read_file`` call, a tiny+ ``compression.threshold_tokens`` and large reported prompt tokens, across a ``--resume``.+* ``drop`` — the stream after a signed tool step is cut mid-body (incomplete chunked+ TLS response); the retry must resend a valid request and persist the answer once.+"""++from __future__ import annotations++import json+from concurrent.futures import ThreadPoolExecutor+from pathlib import Path+from typing import Any, Callable++import pytest++pytest.importorskip("google.auth", reason="Vertex minting needs google-auth (CI installs it)")++from tests.e2e.core.providers._native_helpers import ( # noqa: E402+ ChatResult,+ assert_no_duplicate_assistant_text,+ latest_session,+ make_home,+ messages,+ run_chat,+ tool_calls_of,+)+from tests.fakes.providers.vertex import ( # noqa: E402+ PROJECT,+ REGION,+ SA_EMBEDDED_PROJECT,+ Call,+ Drop,+ FakeVertex,+ Say,+ hermes_setup,+)++KNOWN: dict[str, str] = {}++SUMMARY_MARK = "VERTEX-SUMMARY-OK"+TURN1_FINAL = "Compaction turn one done."+TURN2_FINAL = "Compaction turn two done."+DROP_PARTIAL = "DROPPED-PARTIAL-ANSWER that never finishes"+DROP_FINAL = "Recovered answer after the stream dropped."+# Reported prompt tokens stay far above ``threshold_tokens`` so the compressor must run.+CONTEXT_LENGTH = 64_000+THRESHOLD_TOKENS = 12_000+ARGS = ("-t", "file", "--reasoning", "high")+++def _prompt_tokens(body: dict[str, Any]) -> int:+ return 20_000 + len(json.dumps(body.get("messages", []))) // 4+++def _summary(_rec: dict[str, Any]) -> Say:+ return Say(f"## Goal\nKeep reading the seeded files ({SUMMARY_MARK}).\n## Progress\n- Read several files.\n", chunk_chars=64)+++def _fake(tmp: Path, name: str, script: list[Any], **kw: Any) -> FakeVertex:+ fake = FakeVertex(tmp / name / "fake", project=PROJECT, region=REGION, sa_project=SA_EMBEDDED_PROJECT,+ script=script, **kw)+ fake.start()+ return fake+++def run_compaction(tmp: Path) -> dict[str, Any]:+ script: list[Any] = [Call([("read_file", {"path": f"f{i}.txt"})]) for i in range(4)] + [Say(TURN1_FINAL)]+ script += [Call([("read_file", {"path": f"f{i}.txt"})]) for i in range(4, 7)] + [Say(TURN2_FINAL)]+ fake = _fake(tmp, "compaction", script, aux=_summary, prompt_tokens_fn=_prompt_tokens)+ setup = hermes_setup(fake, context_length=CONTEXT_LENGTH, extra_config={+ "compression": {"threshold_tokens": THRESHOLD_TOKENS, "protect_last_n": 4}})+ nh = make_home(tmp / "compaction" / "h", **setup)+ for i in range(7):+ (nh.project / f"f{i}.txt").write_text(f"file {i} " + "lorem ipsum dolor " * 300 + "\n", encoding="utf-8")+ turn1 = run_chat(nh, "Read f0.txt through f3.txt.", env=fake.child_env(), args=ARGS)+ sid = latest_session(nh) if turn1.returncode == 0 else None+ turn2 = run_chat(nh, "Now read f4.txt through f6.txt.", env=fake.child_env(), args=ARGS, resume=sid) if sid else None+ return {"fake": fake, "nh": nh, "turn1": turn1, "turn2": turn2}+++def run_drop(tmp: Path) -> dict[str, Any]:+ fake = _fake(tmp, "drop", [Call([("read_file", {"path": "note.txt"})]), Drop(DROP_PARTIAL, after_chars=22), Say(DROP_FINAL)])+ nh = make_home(tmp / "drop" / "h", **hermes_setup(fake))+ (nh.project / "note.txt").write_text("note: KIWI-13\n", encoding="utf-8")+ return {"fake": fake, "nh": nh, "turn": run_chat(nh, "Read note.txt.", env=fake.child_env(), args=ARGS)}+++SCENARIOS: dict[str, Callable[[Path], dict[str, Any]]] = {"compaction": run_compaction, "drop": run_drop}++[email protected](scope="module")+def results(tmp_path_factory: pytest.TempPathFactory) -> Any:+ tmp = tmp_path_factory.mktemp("vertex_recovery")+ with ThreadPoolExecutor(max_workers=len(SCENARIOS)) as pool:+ futures = {name: pool.submit(fn, tmp) for name, fn in SCENARIOS.items()}+ out = {name: f.result() for name, f in futures.items()}+ yield out+ for res in out.values():+ res["fake"].stop()+++def _ok(turn: ChatResult | None, what: str, answer: str) -> None:+ assert turn is not None and turn.returncode == 0, f"{what} failed:\n{turn.describe() if turn else 'not run'}"+ assert answer in turn.stdout, turn.describe()+++def _assert_pairs(rows: list[dict[str, Any]]) -> None:+ calls = [tc["id"] for r in rows if r["role"] == "assistant" for tc in tool_calls_of(r)]+ results = [r["tool_call_id"] for r in rows if r["role"] == "tool"]+ assert sorted(calls) == sorted(results), f"persisted tool pairs broken: calls={calls} results={results}"+++def test_compacted_signed_session_stays_valid_for_vertex(results: dict[str, Any]) -> None:+ """The summary call goes to Vertex with the minted bearer, and every request after compaction is+ accepted: tool pairs intact, current-turn calls still signed, each signature on its own call."""+ res = results["compaction"]+ fake = res["fake"]+ _ok(res["turn1"], "turn 1", TURN1_FINAL)+ _ok(res["turn2"], "turn 2 (--resume)", TURN2_FINAL)+ assert not fake.rejected(), json.dumps([(r["status"], r["rejected"]) for r in fake.rejected()], indent=1)+ aux = fake.aux_requests()+ assert aux, "compaction never called the summarizer (threshold not crossed?)"+ minted = {f"Bearer {t}" for t in fake.minted_tokens()}+ assert {r["auth"] for r in aux} <= minted+ first_aux = fake.requests.index(aux[0])+ after = [r for r in fake.requests[first_aux:] if r["kind"] == "main"]+ assert after and any(SUMMARY_MARK in json.dumps(r["body"]["messages"]) for r in after), (+ "no post-compaction request carries the summary")+ for rec in after:+ for msg in rec["body"]["messages"]:+ for tc in msg.get("tool_calls") or []:+ sig = (tc.get("extra_content") or {}).get("google", {}).get("thought_signature")+ if sig is not None:+ assert fake.signature_by_call.get(tc["id"]) == sig, f"signature moved to another call: {tc['id']}"+++def test_compacted_session_rows_keep_tool_pairs(results: dict[str, Any]) -> None:+ res = results["compaction"]+ nh = res["nh"]+ _ok(res["turn2"], "turn 2 (--resume)", TURN2_FINAL)+ rows = messages(nh, latest_session(nh))+ _assert_pairs(rows)+ assert_no_duplicate_assistant_text(rows, TURN2_FINAL)+ assert any(r["role"] == "assistant" and r.get("content") == TURN2_FINAL for r in rows)+++def test_stream_drop_retried_without_duplicate_content(results: dict[str, Any]) -> None:+ """The response after a signed tool step dies mid-body: Hermes resends the same valid request+ (signature intact), prints the recovered answer, and persists it exactly once."""+ res = results["drop"]+ fake, nh = res["fake"], res["nh"]+ _ok(res["turn"], "drop turn", DROP_FINAL)+ assert not fake.rejected(), [r["rejected"] for r in fake.rejected()]+ mains = fake.main_requests()+ assert [r["response"] for r in mains] == ["Call", "Drop", "Say"], [r.get("response") for r in mains]+ assert mains[2]["body"]["messages"] == mains[1]["body"]["messages"], "the retry changed the conversation"+ rows = messages(nh, latest_session(nh))+ assert [r["role"] for r in rows] == ["user", "assistant", "tool", "assistant"], rows+ partial = DROP_PARTIAL[:22]+ assert not [r["id"] for r in rows if partial in (r.get("content") or "")], "the dropped partial was persisted"+ assert_no_duplicate_assistant_text(rows, DROP_FINAL)+ assert partial not in res["turn"].stdoutdiff --git a/tests/e2e/core/providers/test_native_vertex_tools.py b/tests/e2e/core/providers/test_native_vertex_tools.pynew file mode 100644index 00000000000..f00f340e960--- /dev/null+++ b/tests/e2e/core/providers/test_native_vertex_tools.py@@ -0,0 +1,245 @@+"""Vertex AI wire conformance: OAuth minting, multi-turn tools, thought-signature replay after resume.++The real ``hermes chat -q`` CLI talks to Vertex through the standard ``HTTPS_PROXY`` ++``SSL_CERT_FILE`` channel; the fake (``tests/fakes/providers/vertex.py``) terminates TLS for+``us-central1-aiplatform.googleapis.com``, validates every request against the Vertex+OpenAI-compatibility contract, and mints tokens for the REAL ``google-auth`` JWT exchange.++Scenarios run concurrently (one fake + one hermetic home each) in a module fixture:++* ``session`` — turn 1 calls ``read_file`` (signed call), turn 2 runs in a NEW process via+ ``--resume`` and calls it again; ``--reasoning high`` throughout.+* ``refresh`` — tokens are minted with a short ``expires_in``; the vendor expires them right+ after the first response, so the next request 401s and must be retried with a re-minted token.+* ``default_toolset`` — a turn with Hermes' default toolsets (every tool schema goes to Vertex).+"""++from __future__ import annotations++import json+from concurrent.futures import ThreadPoolExecutor+from pathlib import Path+from typing import Any, Callable++import pytest++pytest.importorskip("google.auth", reason="Vertex minting needs google-auth (CI installs it)")++from tests.e2e.core.providers._native_helpers import ( # noqa: E402+ ChatResult,+ NativeHome,+ latest_session,+ make_home,+ messages,+ run_chat,+ tool_calls_of,+)+from tests.fakes.providers.vertex import ( # noqa: E402+ PROJECT,+ REGION,+ SA_EMBEDDED_PROJECT,+ Call,+ FakeVertex,+ Say,+ hermes_setup,+ signatures_on_wire,+)++# key -> "#issue reason"; a strict xfail turns red the moment the bug is fixed.+KNOWN: dict[str, str] = {+ "default_toolset": "#109115 terminal.notify anyOf[boolean, array] is rejected by Vertex's FunctionDeclaration "+ "translator, so every default-toolset turn 400s",+}++SECRET_1 = "PINEAPPLE-42"+SECRET_2 = "MANGO-77"+FINAL_1 = "Turn one answer: the note says PINEAPPLE-42."+FINAL_2 = "Turn two answer: the second note says MANGO-77."+FINAL_REFRESH = "Answer after the token was re-minted."+FILE_ONLY = ("-t", "file")+++class Precondition(RuntimeError):+ """A scenario broke before reaching the property under test (never masquerades as a KNOWN xfail)."""+++def require(ok: Any, what: str) -> None:+ if not ok:+ raise Precondition(what)+++def _home(tmp: Path, name: str, fake: FakeVertex) -> NativeHome:+ nh = make_home(tmp / name / "h", **hermes_setup(fake))+ (nh.project / "note1.txt").write_text(f"note: {SECRET_1}\n", encoding="utf-8")+ (nh.project / "note2.txt").write_text(f"note: {SECRET_2}\n", encoding="utf-8")+ return nh+++def _fake(tmp: Path, name: str, script: list[Any]) -> FakeVertex:+ fake = FakeVertex(tmp / name / "fake", project=PROJECT, region=REGION, sa_project=SA_EMBEDDED_PROJECT, script=script)+ fake.start()+ return fake+++def run_session(tmp: Path) -> dict[str, Any]:+ fake = _fake(tmp, "session", [+ Call([("read_file", {"path": "note1.txt"})]), Say(FINAL_1),+ Call([("read_file", {"path": "note2.txt"})]), Say(FINAL_2),+ ])+ nh = _home(tmp, "session", fake)+ args = (*FILE_ONLY, "--reasoning", "high")+ turn1 = run_chat(nh, "Read note1.txt and tell me what it says.", env=fake.child_env(), args=args)+ boundary = len(fake.requests)+ tokens_after_turn1 = len(fake.token_requests)+ sid = latest_session(nh) if turn1.returncode == 0 else None+ turn2 = run_chat(nh, "Now read note2.txt.", env=fake.child_env(), args=args, resume=sid) if sid else None+ return {"fake": fake, "nh": nh, "turn1": turn1, "turn2": turn2, "boundary": boundary,+ "tokens_after_turn1": tokens_after_turn1, "sid": sid}+++def run_refresh(tmp: Path) -> dict[str, Any]:+ fake = _fake(tmp, "refresh", [Call([("read_file", {"path": "note1.txt"})], expire_tokens_after=True), Say(FINAL_REFRESH)])+ # Below google-auth's refresh window, so a re-mint yields a NEW token rather than the cached one.+ fake.token_policy.expires_in = 120+ nh = _home(tmp, "refresh", fake)+ turn = run_chat(nh, "Read note1.txt.", env=fake.child_env(), args=FILE_ONLY)+ return {"fake": fake, "nh": nh, "turn": turn}+++def run_default_toolset(tmp: Path) -> dict[str, Any]:+ fake = _fake(tmp, "default_toolset", [Say("Default toolset answer.")])+ nh = _home(tmp, "default_toolset", fake)+ return {"fake": fake, "nh": nh, "turn": run_chat(nh, "Say hello.", env=fake.child_env())}+++SCENARIOS: dict[str, Callable[[Path], dict[str, Any]]] = {+ "session": run_session, "refresh": run_refresh, "default_toolset": run_default_toolset,+}++[email protected](scope="module")+def results(tmp_path_factory: pytest.TempPathFactory) -> Any:+ tmp = tmp_path_factory.mktemp("vertex_tools")+ with ThreadPoolExecutor(max_workers=len(SCENARIOS)) as pool:+ futures = {name: pool.submit(fn, tmp) for name, fn in SCENARIOS.items()}+ out = {name: f.result() for name, f in futures.items()}+ yield out+ for res in out.values():+ res["fake"].stop()+++def _ok(turn: ChatResult | None, what: str) -> ChatResult:+ require(turn is not None and turn.returncode == 0, f"{what} failed:\n{turn.describe() if turn else 'not run'}")+ assert turn is not None+ return turn+++def _rejections(fake: FakeVertex) -> str:+ return json.dumps([(r["status"], r["rejected"]) for r in fake.rejected()], indent=1)+++def test_tool_result_goes_back_paired_to_the_signed_call(results: dict[str, Any]) -> None:+ """Turn 1: the model's ``read_file`` call runs for real and its result goes back as a ``tool``+ message paired to the call id, with the call's thought signature replayed byte-for-byte."""+ res = results["session"]+ fake, nh = res["fake"], res["nh"]+ turn1 = _ok(res["turn1"], "turn 1")+ assert FINAL_1 in turn1.stdout, turn1.describe()+ assert not fake.rejected(), f"Vertex rejected requests:\n{_rejections(fake)}"+ first, second = fake.main_requests()[:2]+ (issued,) = first["tool_calls"]+ msgs = second["body"]["messages"]+ asst = next(m for m in msgs if m.get("role") == "assistant" and m.get("tool_calls"))+ (sent,) = asst["tool_calls"]+ assert sent["id"] == issued["id"] and sent["function"]["name"] == "read_file"+ assert sent["extra_content"] == issued["extra_content"], "thought signature not replayed verbatim"+ tool_msgs = [m for m in msgs if m.get("role") == "tool"]+ assert [m["tool_call_id"] for m in tool_msgs] == [issued["id"]]+ assert SECRET_1 in tool_msgs[0]["content"], "the real read_file result did not reach the model"+ rows = messages(nh, res["sid"])[:4] # turn 2 (--resume) appends to the same session afterwards+ assert [r["role"] for r in rows] == ["user", "assistant", "tool", "assistant"], rows+ persisted = tool_calls_of(rows[1])+ assert [tc["id"] for tc in persisted] == [issued["id"]]+ assert rows[2]["tool_call_id"] == issued["id"] and rows[3]["content"] == FINAL_1+++def test_wire_scheme_bearer_and_single_token_mint(results: dict[str, Any]) -> None:+ """Every request hits the configured project/location path on the regional host with a bearer the+ OAuth endpoint minted from a verified SA JWT; one mint per process, reused across API calls."""+ res = results["session"]+ fake = res["fake"]+ _ok(res["turn1"], "turn 1")+ _ok(res["turn2"], "turn 2")+ assert all(r["claims"] for r in fake.token_requests), fake.token_requests+ assert [c["target"] for c in fake.connects if c["allowed"]], "no request reached Vertex through the proxy"+ per_process = [fake.requests[: res["boundary"]], fake.requests[res["boundary"]:]]+ minted = fake.minted_tokens()+ assert res["tokens_after_turn1"] == 1 and len(minted) == 2, (+ f"expected one token exchange per process, saw {len(fake.token_requests)}: {fake.token_requests}")+ for token, reqs in zip(minted, per_process):+ assert len(reqs) >= 2+ assert {r["auth"] for r in reqs} == {f"Bearer {token}"}, "bearer not the minted token / not reused"+ expected_path = f"/v1beta1/projects/{PROJECT}/locations/{REGION}/endpoints/openapi/chat/completions"+ assert {(r["host"], r["path"]) for r in fake.requests} == {(f"{REGION}-aiplatform.googleapis.com", expected_path)}+ assert {r["body"]["model"] for r in fake.requests} == {"google/gemini-3-flash-preview"}+++def test_reasoning_effort_reaches_vertex_as_thinking_config(results: dict[str, Any]) -> None:+ """``--reasoning high`` rides in the documented ``extra_body.google.thinking_config`` wrapper+ (a bare top-level ``google`` key is silently ignored by Vertex) and never alongside+ ``reasoning_effort`` (Vertex allows only one of the two)."""+ res = results["session"]+ _ok(res["turn1"], "turn 1")+ for rec in res["fake"].main_requests():+ body = rec["body"]+ thinking = body.get("extra_body", {}).get("google", {}).get("thinking_config")+ assert thinking and thinking.get("thinking_level") == "high", {k: v for k, v in body.items() if k != "messages"}+ assert "reasoning_effort" not in body+++def test_thought_signature_replayed_after_resume_in_new_process(results: dict[str, Any]) -> None:+ """Turn 2 runs in a fresh process: its first request replays turn 1's signed call (same id, same+ signature bytes) from state.db, and Vertex accepts every request of the resumed turn."""+ res = results["session"]+ fake = res["fake"]+ turn2 = _ok(res["turn2"], "turn 2 (--resume)")+ assert FINAL_2 in turn2.stdout, turn2.describe()+ assert not fake.rejected(), f"Vertex rejected requests:\n{_rejections(fake)}"+ turn1_call = fake.main_requests()[0]["tool_calls"][0]+ resumed = fake.requests[res["boundary"]]+ replayed = {tc["id"]: tc for m in resumed["body"]["messages"] if m.get("role") == "assistant"+ for tc in m.get("tool_calls") or []}+ assert turn1_call["id"] in replayed, "turn 1's tool call missing from the resumed request"+ assert replayed[turn1_call["id"]].get("extra_content") == turn1_call["extra_content"]+ final = fake.main_requests()[-1]["body"]+ assert signatures_on_wire(final) == fake.issued_signatures, "resumed turn lost or reordered signatures"+ assert SECRET_2 in json.dumps(final["messages"])+++def test_expired_token_is_reminted_and_request_retried(results: dict[str, Any]) -> None:+ """The vendor expires the bearer mid-turn: the 401 UNAUTHENTICATED triggers a fresh JWT exchange+ and the SAME request is retried once with the new token; the user only sees the answer."""+ res = results["refresh"]+ fake, nh = res["fake"], res["nh"]+ turn = _ok(res["turn"], "refresh turn")+ assert FINAL_REFRESH in turn.stdout, turn.describe()+ statuses = [r.get("status") for r in fake.requests]+ assert statuses == [200, 401, 200], statuses+ stale, retried = fake.requests[1], fake.requests[2]+ assert stale["auth"] == fake.requests[0]["auth"] and retried["auth"] != stale["auth"]+ assert retried["auth"] == f"Bearer {fake.minted_tokens()[-1]}", "retry did not use the newest minted token"+ assert retried["body"]["messages"] == stale["body"]["messages"]+ assert "401" not in turn.stdout and "UNAUTHENTICATED" not in turn.stdout+ rows = messages(nh, latest_session(nh))+ assert [r["role"] for r in rows] == ["user", "assistant", "tool", "assistant"], rows++[email protected](strict=True, raises=AssertionError, reason=KNOWN["default_toolset"])+def test_default_toolset_schemas_accepted_by_vertex(results: dict[str, Any]) -> None:+ """With Hermes' default toolsets every tool declaration must survive Vertex's translation."""+ res = results["default_toolset"]+ fake = res["fake"]+ require(fake.requests and fake.requests[0]["auth"].startswith("Bearer ya29."), "turn never reached Vertex")+ schema_rejects = [r["rejected"] for r in fake.rejected() if "schema type should be ARRAY" in (r["rejected"] or "")]+ assert not schema_rejects, f"Vertex rejected the tool declarations: {schema_rejects[0]}"+ assert "Default toolset answer." in res["turn"].stdout, res["turn"].describe()diff --git a/tests/fakes/providers/vertex.py b/tests/fakes/providers/vertex.pynew file mode 100644index 00000000000..6936388347b--- /dev/null+++ b/tests/fakes/providers/vertex.py@@ -0,0 +1,709 @@+"""Loopback fake of Google Vertex AI (Gemini behind the OpenAI-compatible endpoint) + Google OAuth.++Two external boundaries Hermes does not own, both faked for real:++* **Google OAuth2 token endpoint** (``POST /token`` on plain loopback HTTP). A generated+ service-account JSON names it as ``token_uri``, so the REAL ``google-auth`` library signs an+ RS256 JWT assertion with the SA private key and exchanges it. The fake verifies the signature+ against the SA public key and the claims Google checks (``iss``/``aud``/``scope``/``iat``/``exp``)+ and mints ``ya29.``-style access tokens with a scripted ``expires_in``.+* **Vertex AI** at ``https://{region}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/+ {region}/endpoints/openapi/chat/completions``. Hermes has no base-URL override for Vertex, so the+ fake is an HTTPS ``CONNECT`` proxy that terminates TLS with a leaf cert signed by a generated CA:+ the child trusts it through the standard ``SSL_CERT_FILE`` and reaches it through the standard+ ``HTTPS_PROXY`` (the corporate-proxy channel Hermes documents). CONNECTs to any other host are+ refused (recorded), so nothing can leak to the real network.++Every Vertex request is recorded (host, path, headers, body) and validated against the published+OpenAI-compatibility contract before a scripted response is chosen; a malformed request gets the+error Vertex returns (``[{"error": {"code", "message", "status"}}]``) and is marked ``rejected``.+Checks: URL scheme (project/location path segments), a live minted bearer, the ``google/<model>``+publisher form, tool-call/tool-result pairing, JSON-object function arguments, function-name rules,+and Gemini 3 thought signatures (``extra_content.google.thought_signature``) on the first function+call of every step in the current turn, byte-identical to one the fake issued.+"""++from __future__ import annotations++import base64+import datetime as _dt+import json+import re+import secrets+import socketserver+import ssl+import threading+import time+import urllib.parse+from dataclasses import dataclass+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from pathlib import Path+from typing import Any, Callable, Union++from cryptography import x509+from cryptography.hazmat.primitives import hashes, serialization+from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa+from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID++CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform"+JWT_BEARER_GRANT = "urn:ietf:params:oauth:grant-type:jwt-bearer"+# Google's token endpoint requires this audience whatever host the SA file's token_uri names.+GOOGLE_TOKEN_AUDIENCE = "https://oauth2.googleapis.com/token"+UNAUTHENTICATED_MESSAGE = (+ "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or "+ "other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.")+MISSING_SIGNATURE_MESSAGE = (+ "Function call is missing a thought_signature in functionCall parts. This is required for tools to work "+ "correctly, and missing thought_signature may lead to degraded model performance. Additional data, function "+ "call `default_api:{name}` , position {pos}. Please refer to https://ai.google.dev/gemini-api/docs/"+ "thought-signatures for more details.")+# HTTP status -> google.rpc canonical status name (https://cloud.google.com/apis/design/errors).+GRPC_STATUS = {400: "INVALID_ARGUMENT", 401: "UNAUTHENTICATED", 403: "PERMISSION_DENIED", 404: "NOT_FOUND",+ 429: "RESOURCE_EXHAUSTED", 500: "INTERNAL", 503: "UNAVAILABLE", 504: "DEADLINE_EXCEEDED"}+_MODEL_RE = re.compile(r"^google/gemini-[0-9a-z.\-]+$")+# FunctionDeclaration.name: letter/underscore first, then [a-zA-Z0-9_.:-], at most 64 chars.+_FUNC_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_.:\-]{0,63}$")+_ROLES = frozenset({"system", "developer", "user", "assistant", "tool"})+++# Scripted responses --------------------------------------------------------------------------------+++@dataclass+class Say:+ """A final assistant answer streamed in chunks."""++ text: str+ prompt_tokens: int | None = None+ completion_tokens: int = 20+ chunk_chars: int = 16+ expire_tokens_after: bool = False # the ~1h boundary passes right after this response+++@dataclass+class Call:+ """One assistant step issuing function calls; Gemini 3 signs the first call of each step."""++ calls: list[tuple[str, dict[str, Any]]]+ text: str | None = None+ signed: bool = True+ prompt_tokens: int | None = None+ expire_tokens_after: bool = False+++@dataclass+class Fail:+ """A Vertex error response; ``list_body`` is the openapi endpoint's list-wrapped envelope."""++ status: int+ message: str+ list_body: bool = True+ retry_after: float | None = None+++@dataclass+class Drop:+ """Open the SSE stream, send ``text[:after_chars]``, then reset the TLS connection mid-body."""++ text: str+ after_chars: int = 10+++Response = Union[Say, Call, Fail, Drop]+Responder = Callable[[dict[str, Any]], Response]+++# Certificates and service account --------------------------------------------------------------------+++def _name(cn: str) -> x509.Name:+ return x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])+++def _write_pem(path: Path, data: bytes) -> Path:+ path.write_bytes(data)+ return path+++def make_tls_material(root: Path, hosts: list[str]) -> tuple[Path, Path, Path]:+ """A throwaway CA (PEM for ``SSL_CERT_FILE``) and a leaf for ``hosts`` signed by it."""+ root.mkdir(parents=True, exist_ok=True)+ now = _dt.datetime.now(_dt.timezone.utc)+ ca_key = ec.generate_private_key(ec.SECP256R1())+ ca_ski = x509.SubjectKeyIdentifier.from_public_key(ca_key.public_key())+ ca_cert = (+ x509.CertificateBuilder().subject_name(_name("hermes-e2e fake Google CA")).issuer_name(_name("hermes-e2e fake Google CA"))+ .public_key(ca_key.public_key()).serial_number(x509.random_serial_number())+ .not_valid_before(now - _dt.timedelta(minutes=5)).not_valid_after(now + _dt.timedelta(days=1))+ .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True)+ .add_extension(x509.KeyUsage(digital_signature=True, key_cert_sign=True, crl_sign=True, content_commitment=False,+ key_encipherment=False, data_encipherment=False, key_agreement=False,+ encipher_only=False, decipher_only=False), critical=True)+ .add_extension(ca_ski, critical=False)+ .sign(ca_key, hashes.SHA256()))+ leaf_key = ec.generate_private_key(ec.SECP256R1())+ leaf_cert = (+ x509.CertificateBuilder().subject_name(_name(hosts[0])).issuer_name(ca_cert.subject)+ .public_key(leaf_key.public_key()).serial_number(x509.random_serial_number())+ .not_valid_before(now - _dt.timedelta(minutes=5)).not_valid_after(now + _dt.timedelta(days=1))+ .add_extension(x509.SubjectAlternativeName([x509.DNSName(h) for h in hosts]), critical=False)+ .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True)+ .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), critical=False)+ .add_extension(x509.AuthorityKeyIdentifier.from_issuer_subject_key_identifier(ca_ski), critical=False)+ .sign(ca_key, hashes.SHA256()))+ ca_pem = _write_pem(root / "fake-google-ca.pem", ca_cert.public_bytes(serialization.Encoding.PEM))+ cert_pem = _write_pem(root / "leaf.pem", leaf_cert.public_bytes(serialization.Encoding.PEM))+ key_pem = _write_pem(root / "leaf.key", leaf_key.private_bytes(+ serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption()))+ return ca_pem, cert_pem, key_pem+++@dataclass+class ServiceAccount:+ path: Path+ client_email: str+ project_id: str+ private_key_id: str+ public_key: rsa.RSAPublicKey+++def make_service_account(path: Path, token_uri: str, project_id: str) -> ServiceAccount:+ """A service-account key file shaped like the one the Cloud console downloads."""+ key = rsa.generate_private_key(public_exponent=65537, key_size=2048)+ email = f"hermes-e2e@{project_id}.iam.gserviceaccount.com"+ kid = secrets.token_hex(20)+ info = {+ "type": "service_account", "project_id": project_id, "private_key_id": kid,+ "private_key": key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8,+ serialization.NoEncryption()).decode(),+ "client_email": email, "client_id": str(secrets.randbelow(10**20)),+ "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": token_uri,+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",+ "universe_domain": "googleapis.com",+ }+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(json.dumps(info, indent=2), encoding="utf-8")+ return ServiceAccount(path, email, project_id, kid, key.public_key())+++def _b64url_decode(part: str) -> bytes:+ return base64.urlsafe_b64decode(part + "=" * (-len(part) % 4))+++def verify_jwt_assertion(assertion: str, sa: ServiceAccount) -> tuple[dict[str, Any] | None, str]:+ """RS256 signature + the claims Google's token endpoint enforces; (claims, "") or (None, reason)."""+ try:+ head_b64, body_b64, sig_b64 = assertion.split(".")+ header, claims = json.loads(_b64url_decode(head_b64)), json.loads(_b64url_decode(body_b64))+ sa.public_key.verify(_b64url_decode(sig_b64), f"{head_b64}.{body_b64}".encode(), padding.PKCS1v15(), hashes.SHA256())+ except Exception as exc: # noqa: BLE001 - any parse/verify failure is Google's "Invalid JWT Signature."+ return None, f"Invalid JWT Signature. ({type(exc).__name__})"+ now = time.time()+ problems = {+ "alg": header.get("alg") != "RS256",+ "kid": header.get("kid") not in (None, sa.private_key_id),+ "iss": claims.get("iss") != sa.client_email,+ "aud": claims.get("aud") != GOOGLE_TOKEN_AUDIENCE,+ "scope": CLOUD_PLATFORM_SCOPE not in str(claims.get("scope", "")).split(),+ "iat": not isinstance(claims.get("iat"), int) or abs(claims["iat"] - now) > 300,+ "exp": not isinstance(claims.get("exp"), int) or not 0 < claims["exp"] - claims.get("iat", 0) <= 3600,+ }+ bad = sorted(k for k, v in problems.items() if v)+ return (None, f"Invalid JWT: bad claims {bad}") if bad else (claims, "")+++# Request validation ----------------------------------------------------------------------------------+++def _first_signature(tool_call: dict[str, Any]) -> Any:+ extra = tool_call.get("extra_content")+ google = extra.get("google") if isinstance(extra, dict) else None+ return google.get("thought_signature") if isinstance(google, dict) else None+++def _check_tool_call_shape(tc: Any) -> str | None:+ if not isinstance(tc, dict) or tc.get("type", "function") != "function" or not isinstance(tc.get("function"), dict):+ return "Invalid value at 'messages[].tool_calls[]': expected a function tool call"+ fn = tc["function"]+ if not isinstance(tc.get("id"), str) or not tc["id"]:+ return "tool_calls[].id must be a non-empty string"+ try:+ args = json.loads(fn.get("arguments") or "{}")+ except (TypeError, json.JSONDecodeError):+ return f"Invalid JSON payload in function call arguments for `{fn.get('name')}`"+ return None if isinstance(args, dict) else "function call arguments must be a JSON object (google.protobuf.Struct)"+++def _check_pairing(messages: list[dict[str, Any]]) -> str | None:+ """Every function-call turn is answered by exactly its function responses, immediately after."""+ i = 0+ while i < len(messages):+ msg = messages[i]+ if msg.get("role") == "tool":+ return ("Please ensure that function response turn comes immediately after a function call turn. "+ f"(orphan tool message at index {i}, tool_call_id={msg.get('tool_call_id')!r})")+ calls = msg.get("tool_calls") if msg.get("role") == "assistant" else None+ if not calls:+ i += 1+ continue+ expected = [tc.get("id") for tc in calls]+ j = i + 1+ answered: list[Any] = []+ while j < len(messages) and messages[j].get("role") == "tool":+ answered.append(messages[j].get("tool_call_id"))+ j += 1+ if sorted(map(str, answered)) != sorted(map(str, expected)):+ return ("Please ensure that the number of function response parts is equal to the number of function "+ f"call parts of the function call turn. (calls {expected} at index {i}, responses {answered})")+ i = j+ return None+++def _check_signatures(messages: list[dict[str, Any]], issued: set[str]) -> str | None:+ """Gemini 3: the first call of every step in the current turn carries a signature we issued;+ any replayed signature (current or historical) must be byte-identical to one we issued."""+ last_user = max((i for i, m in enumerate(messages) if m.get("role") == "user"), default=-1)+ step = 0+ for i, msg in enumerate(messages):+ calls = msg.get("tool_calls") if msg.get("role") == "assistant" else None+ if not calls:+ continue+ for pos, tc in enumerate(calls):+ sig = _first_signature(tc)+ if sig is not None and sig not in issued:+ return "Corrupted thought signature."+ if i > last_user and pos == 0 and sig is None:+ return MISSING_SIGNATURE_MESSAGE.format(name=tc.get("function", {}).get("name"), pos=step + 1)+ step += 1+ return None+++def _merged_anyof_error(node: Any, where: str) -> str | None:+ """Google's OpenAI->FunctionDeclaration translator merges ``anyOf`` branches into one node; a+ merged node carrying ``items`` under a non-array type is rejected (#109115 evidence)."""+ if isinstance(node, dict):+ branches = node.get("anyOf")+ if isinstance(branches, list) and len(branches) > 1:+ types = {b.get("type") for b in branches if isinstance(b, dict)}+ if len(types) > 1 and any(isinstance(b, dict) and "items" in b for b in branches):+ return (f"functionDeclaration `{where}` schema specified incorrect schema type field. "+ "For schema with items, schema type should be ARRAY.")+ for key, child in node.items():+ found = _merged_anyof_error(child, f"{where}.{key}")+ if found:+ return found+ elif isinstance(node, list):+ for child in node:+ found = _merged_anyof_error(child, where)+ if found:+ return found+ return None+++def _check_tools(tools: Any) -> str | None:+ if tools is None:+ return None+ if not isinstance(tools, list):+ return "Invalid value at 'tools': expected a list"+ seen: set[str] = set()+ for tool in tools:+ fn = tool.get("function") if isinstance(tool, dict) and tool.get("type") == "function" else None+ if not isinstance(fn, dict):+ return "Invalid value at 'tools[]': only function tools are supported"+ name = str(fn.get("name", ""))+ if not _FUNC_NAME_RE.match(name):+ return f"Invalid function name `{name}`: must start with a letter or underscore, [a-zA-Z0-9_.:-], max 64"+ if name in seen:+ return f"Duplicate function declaration found: {name}"+ seen.add(name)+ params = fn.get("parameters")+ if params is not None and (not isinstance(params, dict) or params.get("type", "object") != "object"):+ return f"functionDeclaration `{name}` parameters must be an OBJECT schema"+ found = _merged_anyof_error(params, f"{name}.parameters")+ if found:+ return f"Unable to submit request because `{name}` {found}"+ return None+++def validate_chat_body(body: dict[str, Any], issued_signatures: set[str]) -> str | None:+ """The first contract violation Vertex would 400 on, or None."""+ if not _MODEL_RE.match(str(body.get("model", ""))):+ return f"Invalid model name {body.get('model')!r}: the OpenAI-compatible endpoint expects 'google/<model>'"+ messages = body.get("messages")+ if not isinstance(messages, list) or not messages:+ return "* GenerateContentRequest.contents: contents is not specified"+ for idx, msg in enumerate(messages):+ if not isinstance(msg, dict) or msg.get("role") not in _ROLES:+ return f"Invalid value at 'messages[{idx}].role'"+ if msg.get("role") == "tool" and not msg.get("tool_call_id"):+ return f"messages[{idx}]: a tool message requires tool_call_id"+ for tc in msg.get("tool_calls") or []:+ bad = _check_tool_call_shape(tc)+ if bad:+ return f"messages[{idx}]: {bad}"+ return _check_tools(body.get("tools")) or _check_pairing(messages) or _check_signatures(messages, issued_signatures)+++# Server ----------------------------------------------------------------------------------------------+++@dataclass+class _Token:+ value: str+ expires_at: float+++@dataclass+class TokenPolicy:+ expires_in: int = 3600+ error: tuple[int, str, str] | None = None # (status, error, error_description) for every exchange+ reject_bearers: bool = False # Vertex refuses every bearer (disabled SA / revoked grant)+++class FakeVertex:+ """OAuth token endpoint + TLS-terminating CONNECT proxy serving Vertex. Context manager."""++ def __init__(self, root: Path, *, project: str, region: str, sa_project: str | None = None,+ script: list[Response] | Responder | None = None, aux: Responder | None = None,+ default_text: str = "ok", prompt_tokens_fn: Callable[[dict[str, Any]], int] | None = None) -> None:+ self.root, self.project, self.region = root, project, region+ self.host = "aiplatform.googleapis.com" if region == "global" else f"{region}-aiplatform.googleapis.com"+ self._script: list[Response] = list(script) if isinstance(script, list) else []+ self._responder = script if callable(script) else None+ self._aux = aux or (lambda _rec: Say("Summary of the earlier conversation (fake)."))+ self.default_text = default_text+ self.prompt_tokens_fn = prompt_tokens_fn+ self.token_policy = TokenPolicy()+ self.requests: list[dict[str, Any]] = []+ self.token_requests: list[dict[str, Any]] = []+ self.connects: list[dict[str, Any]] = []+ self.issued_signatures: list[str] = []+ self.signature_by_call: dict[str, str | None] = {}+ self._tokens: dict[str, _Token] = {}+ self._lock = threading.Lock()+ self._seq = 0+ self._server: ThreadingHTTPServer | None = None+ self._sa_project = sa_project or project+ self.sa: ServiceAccount | None = None+ self.ca_pem: Path | None = None+ self._tls: ssl.SSLContext | None = None++ # lifecycle+ def __enter__(self) -> "FakeVertex":+ self.start()+ return self++ def __exit__(self, *_exc: object) -> None:+ self.stop()++ def start(self) -> None:+ server = ThreadingHTTPServer(("127.0.0.1", 0), _handler_for(self))+ server.daemon_threads = True+ self._server = server+ self.ca_pem, cert, key = make_tls_material(self.root / "tls", [self.host])+ self._tls = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)+ self._tls.load_cert_chain(cert, key)+ self.sa = make_service_account(self.root / "sa.json", self.token_uri, self._sa_project)+ threading.Thread(target=server.serve_forever, name="fake-vertex", daemon=True).start()++ def stop(self) -> None:+ if self._server is not None:+ self._server.shutdown()+ self._server.server_close()++ @property+ def port(self) -> int:+ assert self._server is not None+ return self._server.server_address[1]++ @property+ def token_uri(self) -> str:+ return f"http://127.0.0.1:{self.port}/token"++ @property+ def chat_path(self) -> str:+ return f"/v1beta1/projects/{self.project}/locations/{self.region}/endpoints/openapi/chat/completions"++ def child_env(self) -> dict[str, str]:+ """Standard proxy + CA-trust env for the Hermes child (no Hermes-specific knobs)."""+ return {"HTTPS_PROXY": f"http://127.0.0.1:{self.port}", "NO_PROXY": "127.0.0.1,localhost",+ "SSL_CERT_FILE": str(self.ca_pem)}++ # scripting+ def push(self, *responses: Response) -> None:+ with self._lock:+ self._script.extend(responses)++ def _next_main(self, record: dict[str, Any]) -> Response:+ if self._responder is not None:+ return self._responder(record)+ with self._lock:+ return self._script.pop(0) if self._script else Say(self.default_text)++ def expire_all_tokens(self) -> None:+ with self._lock:+ for tok in self._tokens.values():+ tok.expires_at = 0.0++ # inspection+ def main_requests(self) -> list[dict[str, Any]]:+ return [r for r in self.requests if r["kind"] == "main"]++ def aux_requests(self) -> list[dict[str, Any]]:+ return [r for r in self.requests if r["kind"] == "aux"]++ def rejected(self) -> list[dict[str, Any]]:+ return [r for r in self.requests if r.get("rejected")]++ def minted_tokens(self) -> list[str]:+ return [t["access_token"] for t in self.token_requests if t.get("access_token")]++ # token endpoint+ def _mint(self, form: dict[str, str]) -> tuple[int, dict[str, Any]]:+ rec: dict[str, Any] = {"grant_type": form.get("grant_type"), "t": time.time()}+ with self._lock:+ self.token_requests.append(rec)+ policy = self.token_policy+ if form.get("grant_type") != JWT_BEARER_GRANT:+ rec["error"] = "unsupported_grant_type"+ return 400, {"error": "unsupported_grant_type", "error_description": "Invalid grant_type"}+ assert self.sa is not None+ claims, why = verify_jwt_assertion(form.get("assertion", ""), self.sa)+ rec["claims"] = claims+ if claims is None:+ rec["error"] = why+ return 400, {"error": "invalid_grant", "error_description": why}+ if policy.error:+ rec["error"] = policy.error[1]+ return policy.error[0], {"error": policy.error[1], "error_description": policy.error[2]}+ value = f"ya29.fake-{secrets.token_urlsafe(24)}"+ with self._lock:+ self._tokens[value] = _Token(value, time.time() + policy.expires_in)+ rec["access_token"] = value+ return 200, {"access_token": value, "expires_in": policy.expires_in, "token_type": "Bearer"}++ def _auth_ok(self, header: str) -> bool:+ if self.token_policy.reject_bearers:+ return False+ scheme, _, value = header.partition(" ")+ tok = self._tokens.get(value) if scheme == "Bearer" else None+ return bool(tok and tok.expires_at > time.time())++ def next_ids(self) -> tuple[str, str]:+ with self._lock:+ self._seq += 1+ sig = base64.b64encode(f"sig-{self._seq}-".encode() + secrets.token_bytes(24)).decode()+ return f"function-call-{self._seq}{secrets.randbelow(10**6):06d}", sig+++def _vertex_error(status: int, message: str, list_body: bool = True) -> bytes:+ err = {"error": {"code": status, "message": message, "status": GRPC_STATUS.get(status, "UNKNOWN")}}+ return json.dumps([err] if list_body else err).encode()+++def _chunk(model: str, delta: dict[str, Any], finish: str | None = None, usage: dict | None = None) -> dict[str, Any]:+ out: dict[str, Any] = {"id": "vertex-fake", "object": "chat.completion.chunk", "created": int(time.time()),+ "model": model, "choices": [{"index": 0, "delta": delta, "finish_reason": finish}]}+ if usage is not None:+ out["usage"] = usage+ return out+++def _handler_for(fake: FakeVertex) -> type[BaseHTTPRequestHandler]:+ class Handler(BaseHTTPRequestHandler):+ protocol_version = "HTTP/1.1"+ tunnel_host: str | None = None++ def log_message(self, format: str, *args: Any) -> None: # noqa: A002+ pass++ def _send(self, status: int, body: bytes, headers: dict[str, str] | None = None) -> None:+ self.send_response(status)+ self.send_header("Content-Type", "application/json; charset=UTF-8")+ self.send_header("Content-Length", str(len(body)))+ for k, v in (headers or {}).items():+ self.send_header(k, v)+ self.end_headers()+ self.wfile.write(body)+ self.wfile.flush()++ # proxy+ def do_CONNECT(self) -> None: # noqa: N802+ host, _, port = self.path.partition(":")+ allowed = host == fake.host and port == "443"+ with fake._lock:+ fake.connects.append({"target": self.path, "allowed": allowed, "t": time.time()})+ if not allowed:+ self._send(403, b'{"error": "fake proxy: host not allowed"}')+ self.close_connection = True+ return+ self.send_response(200, "Connection Established")+ self.end_headers()+ self.wfile.flush()+ assert fake._tls is not None+ try:+ tls = fake._tls.wrap_socket(self.connection, server_side=True)+ except (ssl.SSLError, OSError):+ self.close_connection = True+ return+ self.connection = tls+ self.rfile = tls.makefile("rb")+ self.wfile = socketserver._SocketWriter(tls) # type: ignore[attr-defined]+ self.tunnel_host = host+ self.close_connection = False++ # token endpoint (plain loopback) and Vertex (inside the TLS tunnel)+ def do_POST(self) -> None: # noqa: N802+ raw = self.rfile.read(int(self.headers.get("Content-Length", 0) or 0))+ if self.tunnel_host is None:+ if self.path != "/token":+ self._send(404, b'{"error": "not found"}')+ return+ form = dict(urllib.parse.parse_qsl(raw.decode()))+ status, payload = fake._mint(form)+ self._send(status, json.dumps(payload).encode())+ return+ self._vertex(raw)++ def do_GET(self) -> None: # noqa: N802+ self._send(404, _vertex_error(404, f"The requested URL {self.path} was not found on this server."))++ def _vertex(self, raw: bytes) -> None:+ record: dict[str, Any] = {+ "host": self.tunnel_host, "path": self.path, "auth": self.headers.get("Authorization", ""),+ "headers": {k.lower(): v for k, v in self.headers.items()}, "t": time.time(), "rejected": None,+ }+ try:+ body = json.loads(raw or b"{}")+ except json.JSONDecodeError:+ body = None+ record["body"] = body+ record["kind"] = "main" if isinstance(body, dict) and body.get("tools") else "aux"+ with fake._lock:+ fake.requests.append(record)+ problem = self._precheck(record)+ if problem:+ record["rejected"], record["status"] = problem[1], problem[0]+ self._send(problem[0], _vertex_error(problem[0], problem[1]))+ return+ resp = fake._next_main(record) if record["kind"] == "main" else fake._aux(record)+ record["response"] = type(resp).__name__+ self._respond(resp, record)+ if getattr(resp, "expire_tokens_after", False):+ fake.expire_all_tokens()++ def _precheck(self, record: dict[str, Any]) -> tuple[int, str] | None:+ if record["host"] != fake.host or record["path"] != fake.chat_path:+ return 404, f"Resource not found: {record['host']}{record['path']} (expected {fake.host}{fake.chat_path})"+ if not fake._auth_ok(record["auth"]):+ return 401, UNAUTHENTICATED_MESSAGE+ if not isinstance(record["body"], dict):+ return 400, "Invalid JSON payload received."+ bad = validate_chat_body(record["body"], set(fake.issued_signatures))+ return (400, bad) if bad else None++ # rendering+ def _respond(self, resp: Response, record: dict[str, Any]) -> None:+ body = record["body"]+ model = body.get("model", "google/gemini")+ if isinstance(resp, Fail):+ record["status"] = resp.status+ headers = {"Retry-After": str(resp.retry_after)} if resp.retry_after is not None else {}+ self._send(resp.status, _vertex_error(resp.status, resp.message, resp.list_body), headers)+ return+ record["status"] = 200+ if isinstance(resp, Drop):+ self._start_sse()+ self._sse(_chunk(model, {"role": "assistant", "content": resp.text[: resp.after_chars]}))+ self.connection.close() # no terminal chunk: the chunked body is left incomplete+ self.close_connection = True+ return+ pt = resp.prompt_tokens if resp.prompt_tokens is not None else (+ fake.prompt_tokens_fn(body) if fake.prompt_tokens_fn else 120)+ deltas, finish = self._deltas(resp, record)+ usage = {"prompt_tokens": pt, "completion_tokens": 20, "total_tokens": pt + 20}+ if not body.get("stream"):+ message: dict[str, Any] = {"role": "assistant", "content": "".join(d.get("content", "") for d in deltas) or None}+ calls = [tc for d in deltas for tc in d.get("tool_calls", [])]+ if calls:+ message["tool_calls"] = [{k: v for k, v in tc.items() if k != "index"} for tc in calls]+ self._send(200, json.dumps({"id": "vertex-fake", "object": "chat.completion", "created": int(time.time()),+ "model": model, "usage": usage,+ "choices": [{"index": 0, "message": message, "finish_reason": finish}]}).encode())+ return+ self._start_sse()+ for delta in deltas:+ self._sse(_chunk(model, {"role": "assistant", **delta}))+ self._sse(_chunk(model, {}, finish, usage))+ self._write_chunk(b"data: [DONE]\n\n")+ self._write_chunk(b"")+ self.close_connection = True++ def _deltas(self, resp: Say | Call, record: dict[str, Any]) -> tuple[list[dict[str, Any]], str]:+ if isinstance(resp, Say):+ size = max(1, resp.chunk_chars)+ return [{"content": resp.text[i:i + size]} for i in range(0, len(resp.text), size)] or [{"content": ""}], "stop"+ tool_calls = []+ for pos, (name, args) in enumerate(resp.calls):+ call_id, sig = fake.next_ids()+ tc: dict[str, Any] = {"index": pos, "id": call_id, "type": "function",+ "function": {"name": name, "arguments": json.dumps(args)}}+ if resp.signed and pos == 0:+ tc["extra_content"] = {"google": {"thought_signature": sig}}+ with fake._lock:+ fake.issued_signatures.append(sig)+ tool_calls.append(tc)+ record["tool_calls"] = tool_calls+ with fake._lock:+ fake.signature_by_call.update({tc["id"]: _first_signature(tc) for tc in tool_calls})+ deltas: list[dict[str, Any]] = [{"content": resp.text}] if resp.text else []+ deltas.append({"tool_calls": tool_calls})+ return deltas, "tool_calls"++ def _start_sse(self) -> None:+ self.send_response(200)+ self.send_header("Content-Type", "text/event-stream")+ self.send_header("Transfer-Encoding", "chunked")+ self.end_headers()+ self.wfile.flush()++ def _write_chunk(self, data: bytes) -> None:+ self.wfile.write(f"{len(data):x}\r\n".encode() + data + b"\r\n")+ self.wfile.flush()++ def _sse(self, payload: dict[str, Any]) -> None:+ self._write_chunk(f"data: {json.dumps(payload)}\n\n".encode())++ return Handler+++MODEL = "google/gemini-3-flash-preview"+PROJECT = "hermes-e2e-proj"+SA_EMBEDDED_PROJECT = "hermes-sa-embedded-proj" # differs from PROJECT: proves the config override wins+REGION = "us-central1"+++def hermes_setup(fake: FakeVertex, *, model: str = MODEL, extra_config: dict[str, Any] | None = None,+ context_length: int | None = None) -> dict[str, Any]:+ """``make_home`` kwargs for a Hermes home that selects ``provider: vertex`` against ``fake``: the SA+ key path in ``.env`` (VERTEX_CREDENTIALS_PATH) and project/region under ``vertex:`` in config.yaml."""+ assert fake.sa is not None+ block: dict[str, Any] = {"provider": "vertex", "default": model}+ if context_length:+ block["context_length"] = context_length+ cfg: dict[str, Any] = {"vertex": {"project_id": fake.project, "region": fake.region}}+ cfg.update(extra_config or {})+ return {"model": block, "env_file": {"VERTEX_CREDENTIALS_PATH": str(fake.sa.path)}, "extra_config": cfg}+++def signatures_on_wire(body: dict[str, Any]) -> list[str]:+ """Every thought signature replayed in a request's assistant tool calls, in order."""+ return [sig for m in body.get("messages", []) if m.get("role") == "assistant"+ for tc in m.get("tool_calls") or [] if (sig := _first_signature(tc))]+++__all__ = [+ "MODEL", "PROJECT", "REGION", "SA_EMBEDDED_PROJECT", "hermes_setup",+ "Call", "Drop", "Fail", "FakeVertex", "GRPC_STATUS", "Say", "ServiceAccount", "TokenPolicy",+ "make_service_account", "make_tls_material", "signatures_on_wire", "validate_chat_body", "verify_jwt_assertion",+]