feat(clients): models+runtime: close gaps (all Discovery methods wrapped, live re-verification)

models: EndpointAdmin and PublisherAdmin wrap every remaining method of the
owned resources (84 call table rows, 132 Discovery methods covered, v1beta1
twins, IAM, operations, responses, export, config, enableModel); planner
rejects undeclared body keys; VertexModel::TurnBuilder replays model turns
with every thoughtSignature; Lyria WAV mime from bytes; veo-3.1 default.
runtime: v1beta1 feedbackEntries, A2A gateway (40 rpcs), a2aTasks, task store
records, scoped operations, beta twin flag (103 rows, 214 methods covered);
checker verifies twins, a2aEndpoint patterns and coverage.
example: admin routes for both parts. SPECs: live results moved from
Unverified to Verified live (Veo, Lyria, thought signatures, v1beta1 routes).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 05:47:34 +02:00
co-authored by Claude Sonnet 5.5
parent 29d775a923
commit 8e807945a2
38 changed files with 4437 additions and 72 deletions
+106
View File
@@ -0,0 +1,106 @@
// Routes: POST /agent/models/{endpoints,endpoint/call,endpoint/operations,publisher}
// Without a credential every one is 503 google_not_configured and sends
// nothing; with one, bad input is 422 before any network traffic; gated
// actions are dry runs unless confirm:true; GET is 405.
#include <cstdlib>
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
namespace {
struct Route {
const char* path;
const char* valid;
const char* bad;
};
const Route kRoutes[] = {
{"/agent/models/endpoints", "{\"action\":\"list_beta\"}",
"{\"action\":\"patch\",\"endpoint\":\"7/7\",\"body\":{},\"updateMask\":\"displayName\"}"},
{"/agent/models/endpoint/call",
"{\"kind\":\"compute_tokens\",\"endpoint\":\"7\",\"contents\":[{\"role\":\"user\",\"parts\":[{\"text\":\"x\"}]}]}",
"{\"kind\":\"compute_tokens\",\"endpoint\":\"7\",\"contents\":[]}"},
{"/agent/models/endpoint/operations", "{\"action\":\"list\",\"endpoint\":\"7\"}",
"{\"action\":\"get\",\"endpoint\":\"7\"}"},
{"/agent/models/publisher", "{\"action\":\"beta_get\",\"model\":\"gemini-2.5-flash\"}",
"{\"action\":\"beta_get\",\"model\":\"a/b\"}"},
};
// More rejected bodies, one per kind of rule.
const Route kMore[] = {
{"/agent/models/endpoints", "", "{\"action\":\"explode\"}"},
{"/agent/models/endpoints", "", "{\"action\":\"create\"}"},
{"/agent/models/endpoints", "", "{\"action\":\"create\",\"body\":{\"notAField\":1}}"},
{"/agent/models/endpoints", "", "{\"action\":\"patch\",\"endpoint\":\"7\",\"body\":{},\"updateMask\":\"bad mask!\"}"},
{"/agent/models/endpoints", "", "{\"action\":\"get_iam\",\"endpoint\":\"7\",\"policyVersion\":9}"},
{"/agent/models/endpoints", "", "{\"action\":\"test_iam\",\"endpoint\":\"7\",\"permissions\":[]}"},
{"/agent/models/endpoints", "", "{\"action\":\"delete\",\"endpoint\":\"7\",\"confirm\":\"yes\"}"},
{"/agent/models/endpoint/call", "", "{\"kind\":\"openapi\",\"openapiKind\":\"images\",\"body\":{}}"},
{"/agent/models/endpoint/call", "", "{\"kind\":\"direct_raw_predict\",\"endpoint\":\"7\",\"methodName\":\"bad method\",\"input\":\"AAAA\"}"},
{"/agent/models/endpoint/call", "", "{\"kind\":\"invoke\",\"endpoint\":\"7\",\"invokeId\":\"../x\",\"body\":{}}"},
{"/agent/models/endpoint/operations", "", "{\"action\":\"wait\",\"endpoint\":\"7\",\"operation\":\"9\",\"timeoutSeconds\":9999}"},
{"/agent/models/publisher", "", "{\"action\":\"responses_get\",\"publisher\":\"a/b\",\"responseId\":\"r1\"}"},
{"/agent/models/publisher", "", "{\"action\":\"beta_post\",\"model\":\"m\",\"callId\":\"beta.generate\",\"body\":{}}"},
{"/agent/models/publisher", "", "{\"action\":\"stream_predict\",\"model\":\"m\",\"inputs\":[]}"},
{"/agent/models/publisher", "", "{\"action\":\"beta_generate\",\"model\":\"m\",\"prompt\":\"\"}"},
};
// Gated actions: a dry run without confirm.
const Route kGated[] = {
{"/agent/models/endpoints", "{\"action\":\"delete\",\"endpoint\":\"7\"}", ""},
{"/agent/models/endpoints", "{\"action\":\"deploy\",\"endpoint\":\"7\",\"deployedModel\":{\"model\":\"m\"}}", ""},
{"/agent/models/endpoints", "{\"action\":\"undeploy\",\"endpoint\":\"7\",\"deployedModelId\":\"3\"}", ""},
{"/agent/models/endpoints", "{\"action\":\"mutate\",\"endpoint\":\"7\",\"deployedModel\":{},\"updateMask\":\"x\"}", ""},
{"/agent/models/endpoints", "{\"action\":\"set_iam\",\"endpoint\":\"7\",\"policy\":{}}", ""},
{"/agent/models/endpoint/call", "{\"kind\":\"response_delete\",\"endpoint\":\"7\",\"responseId\":\"r1\"}", ""},
{"/agent/models/endpoint/operations", "{\"action\":\"delete\",\"endpoint\":\"7\",\"operation\":\"9\"}", ""},
{"/agent/models/publisher", "{\"action\":\"export\",\"model\":\"gemma3\",\"destination\":\"gs://b/x\"}", ""},
{"/agent/models/publisher", "{\"action\":\"set_config\",\"model\":\"gemma3\",\"publisherModelConfig\":{},\"updateMask\":\"x\"}", ""},
{"/agent/models/publisher", "{\"action\":\"enable\",\"model\":\"gemma3\"}", ""},
{"/agent/models/publisher", "{\"action\":\"responses_delete\",\"responseId\":\"r1\"}", ""},
};
} // namespace
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "demo-proj", 1);
{
GoogleRig none(false);
Fixture f(none.client);
for (const Route& r : kRoutes) {
auto res = f.client->post(r.path, r.valid, "application/json");
check(res.status == 503 && contains(res.body, "google_not_configured"),
std::string(r.path) + ": 503 google_not_configured without a credential");
check(f.client->get(r.path).status == 405, std::string(r.path) + ": GET is 405");
}
for (const Route& r : kGated) {
auto res = f.client->post(r.path, r.valid, "application/json");
check(res.status == 503, std::string(r.path) + ": gated action is 503 without a credential");
}
check(none.mock->send_count() == 0, "side effect: nothing sent without a credential");
}
{
GoogleRig rig;
Fixture f(rig.client);
for (const Route& r : kRoutes) {
auto res = f.client->post(r.path, r.bad, "application/json");
check(res.status == 422 && contains(res.body, "validation_error"),
std::string(r.path) + ": bad input is 422 (got " + std::to_string(res.status) + ")");
check(!contains(res.body, kTestKey), std::string(r.path) + ": credential never in a response");
}
for (const Route& r : kMore) {
auto res = f.client->post(r.path, r.bad, "application/json");
check(res.status == 422, std::string(r.path) + ": rejected " + r.bad + " (got " +
std::to_string(res.status) + ")");
}
for (const Route& r : kGated) {
auto res = f.client->post(r.path, r.valid, "application/json");
check(res.status == 200 && contains(res.body, "\"dry_run\":true"),
std::string(r.path) + ": " + r.valid + " is a dry run (got " + std::to_string(res.status) + ")");
}
std::string huge = "{\"action\":\"create\",\"body\":{\"displayName\":\"" + std::string(300 * 1024, 'x') + "\"}}";
check(f.client->post("/agent/models/endpoints", huge, "application/json").status == 413,
"oversize body is 413");
check(f.client->post("/agent/models/endpoints", "not json", "application/json").status == 422,
"non JSON body is 422");
check(rig.mock->send_count() == 0, "side effect: rejected input and dry runs never reached Google");
}
return finish("agent/models/admin");
}
@@ -0,0 +1,147 @@
// Routes: POST /agent/runtime/{operations,revisions,update,iam,code,async/cancel,
// feedback,a2a,a2a/tasks,a2a/records}
// Without a credential a well formed request is 503 google_not_configured;
// bad input is 422 (with or without a credential) and never reaches Google;
// destructive actions are dry runs unless confirm:true; GET is 405.
#include <cstdlib>
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
namespace {
struct Route {
const char* path;
const char* valid;
const char* bad;
};
const char* kOp = "projects/p/locations/us-central1/reasoningEngines/1/operations/2";
const Route kRoutes[] = {
{"/agent/runtime/operations",
"{\"action\":\"get\",\"name\":\"projects/p/locations/us-central1/reasoningEngines/1/operations/2\"}",
"{\"action\":\"get\",\"name\":\"projects/p/operations/2\"}"},
{"/agent/runtime/revisions", "{\"action\":\"get\",\"id\":\"1\",\"revision\":\"r1\"}",
"{\"action\":\"get\",\"id\":\"1\"}"},
{"/agent/runtime/update", "{\"id\":\"1\",\"displayName\":\"new name\"}", "{\"id\":\"1\"}"},
{"/agent/runtime/iam", "{\"action\":\"get\",\"id\":\"1\"}",
"{\"action\":\"get\",\"id\":\"1\",\"policyVersion\":9}"},
{"/agent/runtime/code", "{\"id\":\"1\",\"inputs\":[{\"code\":\"x\"}]}",
"{\"id\":\"1\",\"inputs\":[]}"},
{"/agent/runtime/async/cancel",
"{\"id\":\"1\",\"operation\":\"projects/p/locations/us-central1/operations/9\"}",
"{\"id\":\"1\",\"operation\":\"nope\"}"},
{"/agent/runtime/feedback", "{\"action\":\"list\",\"id\":\"1\"}",
"{\"action\":\"get\",\"id\":\"1\"}"},
{"/agent/runtime/a2a", "{\"target\":\"engine\",\"protocol\":\"v1\",\"op\":\"card\",\"id\":\"1\"}",
"{\"target\":\"engine\",\"protocol\":\"v1\",\"op\":\"task\",\"id\":\"1\"}"},
{"/agent/runtime/a2a/tasks", "{\"action\":\"list\",\"id\":\"1\"}",
"{\"action\":\"create\",\"id\":\"1\",\"task\":\"Bad_ID\"}"},
{"/agent/runtime/a2a/records", "{\"action\":\"list\",\"store\":\"s1\"}",
"{\"action\":\"get\",\"store\":\"s1\"}"},
};
// Further rejected bodies per route family.
const Route kMore[] = {
{"/agent/runtime/operations", "", "{\"action\":\"explode\",\"name\":\"x\"}"},
{"/agent/runtime/operations", "", "{\"action\":\"wait\",\"name\":\"projects/p/locations/l/reasoningEngines/1/operations/2\",\"timeoutSeconds\":9999}"},
{"/agent/runtime/operations", "", "{\"action\":\"list\",\"parent\":\"projects/p/locations/l/reasoningEngines/1/examples/x\"}"},
{"/agent/runtime/revisions", "", "{\"action\":\"query\",\"id\":\"1\",\"revision\":\"r\",\"input\":\"text\"}"},
{"/agent/runtime/iam", "", "{\"action\":\"test\",\"id\":\"1\",\"permissions\":\"x\"}"},
{"/agent/runtime/iam", "", "{\"action\":\"set\",\"id\":\"1\",\"confirm\":true}"},
{"/agent/runtime/feedback", "", "{\"action\":\"create\",\"id\":\"1\",\"feedbackLabels\":[1]}"},
{"/agent/runtime/feedback", "", "{\"action\":\"create\",\"id\":\"1\",\"feedbackText\":\"x\",\"customMetadata\":5}"},
{"/agent/runtime/a2a", "", "{\"target\":\"engine\",\"protocol\":\"v2\",\"op\":\"card\",\"id\":\"1\"}"},
{"/agent/runtime/a2a", "", "{\"target\":\"revision\",\"protocol\":\"plain\",\"op\":\"card\",\"id\":\"1\"}"},
{"/agent/runtime/a2a", "", "{\"target\":\"engine\",\"protocol\":\"plain\",\"op\":\"send\",\"id\":\"1\"}"},
{"/agent/runtime/a2a", "", "{\"target\":\"engine\",\"protocol\":\"plain\",\"op\":\"card\",\"id\":\"1\",\"historyLength\":99999}"},
{"/agent/runtime/a2a/tasks", "", "{\"action\":\"append\",\"id\":\"1\",\"task\":\"t1\",\"events\":[]}"},
{"/agent/runtime/a2a/records", "", "{\"action\":\"create\",\"store\":\"s1\",\"record\":\"a/b\"}"},
};
// Destructive actions: dry run without confirm, then 503 with it.
struct Destructive {
const char* path;
const char* body;
const char* confirmed;
};
const Destructive kDestructive[] = {
{"/agent/runtime/operations",
"{\"action\":\"delete\",\"name\":\"projects/p/locations/us-central1/reasoningEngines/1/operations/2\"}",
"{\"action\":\"delete\",\"confirm\":true,\"name\":\"projects/p/locations/us-central1/reasoningEngines/1/operations/2\"}"},
{"/agent/runtime/revisions", "{\"action\":\"delete\",\"id\":\"1\",\"revision\":\"r1\"}",
"{\"action\":\"delete\",\"id\":\"1\",\"revision\":\"r1\",\"confirm\":true}"},
{"/agent/runtime/iam", "{\"action\":\"set\",\"id\":\"1\",\"policy\":{}}",
"{\"action\":\"set\",\"id\":\"1\",\"policy\":{},\"confirm\":true}"},
{"/agent/runtime/feedback", "{\"action\":\"delete\",\"id\":\"1\",\"entry\":\"f1\"}",
"{\"action\":\"delete\",\"id\":\"1\",\"entry\":\"f1\",\"confirm\":true}"},
{"/agent/runtime/a2a/tasks", "{\"action\":\"delete\",\"id\":\"1\",\"task\":\"t1\"}",
"{\"action\":\"delete\",\"id\":\"1\",\"task\":\"t1\",\"confirm\":true}"},
{"/agent/runtime/a2a/records", "{\"action\":\"delete\",\"store\":\"s1\",\"record\":\"r1\"}",
"{\"action\":\"delete\",\"store\":\"s1\",\"record\":\"r1\",\"confirm\":true}"},
};
} // namespace
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
(void)kOp;
{
GoogleRig none(false);
Fixture f(none.client);
for (const Route& r : kRoutes) {
auto res = f.client->post(r.path, r.valid, "application/json");
check(res.status == 503 && contains(res.body, "google_not_configured"),
std::string(r.path) + ": 503 google_not_configured without a credential (got " +
std::to_string(res.status) + ")");
auto bad = f.client->post(r.path, r.bad, "application/json");
check(bad.status == 422 && contains(bad.body, "validation_error"),
std::string(r.path) + ": bad input is 422 even without a credential (got " +
std::to_string(bad.status) + ")");
check(f.client->get(r.path).status == 405, std::string(r.path) + ": GET is 405");
check(f.client->post(r.path, "not json", "application/json").status == 422,
std::string(r.path) + ": non JSON body is 422");
}
for (const Route& r : kMore) {
auto bad = f.client->post(r.path, r.bad, "application/json");
check(bad.status == 422, std::string(r.path) + ": rejected: " + r.bad + " (got " +
std::to_string(bad.status) + ")");
}
for (const Destructive& d : kDestructive) {
auto dry = f.client->post(d.path, d.body, "application/json");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"),
std::string(d.path) + ": no confirm is a dry run (got " + std::to_string(dry.status) + ")");
auto real = f.client->post(d.path, d.confirmed, "application/json");
check(real.status == 503 && contains(real.body, "google_not_configured"),
std::string(d.path) + ": confirmed without a credential is 503 (got " +
std::to_string(real.status) + ")");
auto wrong = f.client->post(d.path, "{\"action\":\"delete\",\"confirm\":\"yes\"}", "application/json");
check(wrong.status == 422, std::string(d.path) + ": non boolean confirm is 422");
}
std::string huge = "{\"id\":\"1\",\"inputs\":[{\"code\":\"" + std::string(300 * 1024, 'x') + "\"}]}";
check(f.client->post("/agent/runtime/code", huge, "application/json").status == 413,
"oversize body is 413");
check(none.mock->send_count() == 0, "side effect: nothing sent without a credential");
}
{
GoogleRig rig; // dummy credential: validation must still stop first
Fixture f(rig.client);
for (const Route& r : kRoutes) {
auto bad = f.client->post(r.path, r.bad, "application/json");
check(bad.status == 422, std::string(r.path) + ": bad input is 422 with a credential (got " +
std::to_string(bad.status) + ")");
check(!contains(bad.body, kTestKey), std::string(r.path) + ": credential never in a response");
}
for (const Route& r : kMore) {
auto bad = f.client->post(r.path, r.bad, "application/json");
check(bad.status == 422, std::string(r.path) + ": rejected with a credential: " + r.bad);
}
for (const Destructive& d : kDestructive) {
auto dry = f.client->post(d.path, d.body, "application/json");
check(dry.status == 200 && contains(dry.body, "dry_run"),
std::string(d.path) + ": dry run with a credential");
}
check(rig.mock->send_count() == 0, "side effect: rejected input and dry runs never reached Google");
}
return finish("agent/runtime/extended");
}
+133
View File
@@ -0,0 +1,133 @@
// Opt-in live checks (MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN, GOOGLE_CLOUD_PROJECT)
// for the admin surface: read-only calls only, plus one tiny function calling
// round trip that proves the thoughtSignature replay. Nothing is created,
// changed or deleted. Without a credential the test prints a loud SKIP.
#include <cstdlib>
#include <iostream>
#include "framework.h"
#include "google/credentials.h"
#include "google/errors.h"
#include "google/models/endpoint_admin.h"
#include "google/models/gemini.h"
#include "google/models/publisher_admin.h"
#include "google/models/vertex_model.h"
using namespace molo::google;
using namespace molo::google::models;
using molo::clients::body::Json;
namespace {
bool ready() {
return std::getenv("MOLO_LIVE_NET") && !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() &&
!Credentials::env("GOOGLE_CLOUD_PROJECT").empty();
}
VertexTarget target(const std::string& location) {
CloudContext c = CloudContext::from_env();
c.location = location;
return VertexTarget::project(c);
}
// A missing resource is 404 (or 400 for a bad id shape): the route exists and
// auth works. Anything else is a failure.
template <typename F>
void expect_missing(const char* what, F&& f) {
try {
f();
std::cout << " " << what << ": 200\n";
} catch (const GoogleError& e) {
std::cout << " " << what << ": " << e.http_status() << " " << e.status() << "\n";
// 501: the stored responses routes answer UNIMPLEMENTED in this project.
ASSERT_TRUE(e.http_status() == 404 || e.http_status() == 400 || e.http_status() == 501);
}
}
} // namespace
TEST(models_live_admin, read_only_admin_calls) {
if (!ready()) {
std::cout << "SKIP models_live_admin.read_only_admin_calls (set MOLO_LIVE_NET=1, "
"GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n";
return;
}
auto client = GoogleClient::from_env();
EndpointAdmin v1(client, target("us-central1"));
EndpointAdmin beta(client, target("us-central1"), true);
Json listed = beta.list_beta();
std::cout << " endpoints (v1beta1 list): " << listed.dump().substr(0, 60) << "\n";
ASSERT_TRUE(listed.is_object());
expect_missing("v1 operations list of a missing endpoint",
[&] { ASSERT_TRUE(v1.list_operations("1234567890123").size() == 0); });
expect_missing("v1beta1 operations list of a missing endpoint",
[&] { ASSERT_TRUE(beta.list_operations("1234567890123").size() == 0); });
expect_missing("v1beta1 getIamPolicy of a missing endpoint",
[&] { beta.get_iam_policy("1234567890123"); });
expect_missing("operation get on a missing endpoint", [&] { v1.operation("1234567890123", "1"); });
expect_missing("response get on a missing endpoint",
[&] { v1.get_response("1234567890123", "resp1"); });
PublisherAdmin pub(client, target("us-central1"));
Json model = pub.beta_get("google", "gemini-2.5-flash");
ASSERT_TRUE(model.find("name") != nullptr);
expect_missing("publisher getIamPolicy (v1beta1)", [&] { pub.get_iam_policy("google", "gemini-2.5-flash"); });
}
// Gemini 3 attaches a thoughtSignature to a functionCall part and wants it
// back when the turn is replayed. Two calls of a few tokens each.
TEST(models_live_admin, thought_signature_replay_round_trip) {
if (!ready()) {
std::cout << "SKIP models_live_admin.thought_signature_replay_round_trip (set MOLO_LIVE_NET=1, "
"GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n";
return;
}
CloudContext g = CloudContext::from_env();
g.location = "global";
auto gemini = std::make_shared<Gemini>(GoogleClient::from_env(), VertexTarget::project(g));
VertexModel model(gemini, "gemini-3.5-flash");
GenerateRequest r;
r.contents.push_back(Content::user("What is the weather in Paris? Use the tool."));
Tool t;
FunctionDeclaration fn;
fn.name = "get_weather";
fn.description = "Weather for a city";
Json params = Json::object();
params.set("type", Json("OBJECT"));
Json props = Json::object();
Json city = Json::object();
city.set("type", Json("STRING"));
props.set("city", city);
params.set("properties", props);
fn.parameters = params;
t.functions.push_back(fn);
r.tools.push_back(t);
r.config.thinking_budget = 0;
r.config.max_output_tokens = 200;
try {
VertexModel::TurnBuilder turn;
model.stream(r, [&](const GenerateResponse& c) {
turn.add(c);
return true;
});
bool called = false;
for (const Part& p : turn.content().parts)
if (p.function_call) called = true;
std::cout << " model called the tool: " << called << ", parts with a signature: "
<< turn.signatures() << "\n";
if (!called) {
std::cout << " note: the model answered without a tool call; replay not exercised\n";
return;
}
Content answer;
answer.role = "user";
for (const Part& p : turn.content().parts)
if (p.function_call) {
Json out = Json::object();
out.set("temp_c", Json(18));
answer.parts.push_back(Part::of_response(FunctionResponse{p.function_call->name, out, p.function_call->id}));
}
VertexModel::replay(r, turn.content(), answer);
GenerateResponse second = model.generate(r); // rejected with 400 when a required signature is missing
ASSERT_TRUE(!second.candidates.empty());
std::cout << " replay accepted, answer: " << second.text().substr(0, 60) << "\n";
} catch (const GoogleError& e) {
if (e.http_status() != 429) throw;
std::cout << " note: reached Google, quota exhausted (429)\n";
}
}
+1 -1
View File
@@ -93,7 +93,7 @@ TEST(models_table, rows_are_well_formed_and_unique) {
for (const CallSpec& s : kCalls) {
ASSERT_TRUE(ids.insert(s.id).second);
std::string http = s.http;
ASSERT_TRUE(http == "GET" || http == "POST");
ASSERT_TRUE(http == "GET" || http == "POST" || http == "PATCH" || http == "DELETE");
std::string v = s.version;
ASSERT_TRUE(v == "v1" || v == "v1beta1");
std::string p = s.prefix;
@@ -0,0 +1,318 @@
// Deterministic local tests for the admin surface of the models module
// (EndpointAdmin, PublisherAdmin) and for the thought signature replay of
// VertexModel. Requests are compared to the call table, which
// tools/check_discovery.py checks against Google's Discovery documents. No
// Google replies are scripted anywhere.
#include <memory>
#include <set>
#include <sstream>
#include "clients/transports/mock_transport.h"
#include "framework.h"
#include "google/credentials.h"
#include "google/errors.h"
#include "google/models/calls.h"
#include "google/models/caps.h"
#include "google/models/endpoint_admin.h"
#include "google/models/gemini.h"
#include "google/models/media.h"
#include "google/models/publisher_admin.h"
#include "google/models/vertex_model.h"
using namespace molo::google;
using namespace molo::google::models;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
namespace {
std::shared_ptr<GoogleClient> client(bool configured, MockTransport** raw = nullptr) {
auto t = std::make_unique<MockTransport>();
if (raw) *raw = t.get();
return std::make_shared<GoogleClient>(std::move(t),
configured ? Credentials::bearer("tok") : nullptr);
}
VertexTarget project() {
CloudContext c;
c.project = "demo-proj";
c.location = "us-central1";
return VertexTarget::project(c);
}
const std::string kEp =
"https://us-central1-aiplatform.googleapis.com/v1/projects/demo-proj/locations/us-central1";
const std::string kEpBeta =
"https://us-central1-aiplatform.googleapis.com/v1beta1/projects/demo-proj/locations/us-central1";
// True when the call is rejected locally (invalid) and nothing was sent.
template <typename F>
bool rejected(MockTransport* mock, F&& f) {
std::size_t before = mock->send_count();
try {
f();
} catch (const GoogleError& e) {
return e.http_status() == 400 && e.status() == "INVALID_ARGUMENT" &&
mock->send_count() == before;
} catch (...) {
}
return false;
}
Json obj(const char* k, Json v) {
Json o = Json::object();
o.set(k, std::move(v));
return o;
}
} // namespace
TEST(models_admin, every_table_row_plans_to_a_concrete_url) {
CallPlanner planner(project());
CallPlanner express(VertexTarget::express());
CallPlanner::Args args = {{"publisher", "google"}, {"model", "m1"}, {"endpoint", "11"},
{"operation", "22"}, {"response", "33"}, {"deployed", "44"},
{"invoke", "55"}};
std::size_t rows = 0;
for (const CallSpec& s : kCalls) {
Json body = Json::object();
std::istringstream in(s.fields);
for (std::string f; in >> f;)
if (f != "-" && f[0] != '?' && f.find('.') == std::string::npos) body.set(f, Json(1));
PlannedCall p = planner.plan(s.id, args, body.as_object().empty() ? nullptr : &body);
ASSERT_TRUE(p.url().find('{') == std::string::npos);
ASSERT_TRUE(p.url().find("demo-proj") != std::string::npos || std::string(s.prefix) == "");
if (s.express) {
PlannedCall e = express.plan(s.id, args, body.as_object().empty() ? nullptr : &body);
ASSERT_TRUE(e.url().find("projects") == std::string::npos);
}
++rows;
}
ASSERT_EQ(rows, kCallCount);
ASSERT_TRUE(kCallCount >= 84);
}
TEST(models_admin, endpoint_urls_and_beta_twin) {
EndpointAdmin v1(client(false), project());
EndpointAdmin b(client(false), project(), true);
Json body = obj("displayName", Json("e"));
ASSERT_EQ(v1.plan_call("endpoint.create", "", &body, "endpointId=e1").url(),
kEp + "/endpoints?endpointId=e1");
ASSERT_EQ(b.plan_call("endpoint.create", "", &body, "endpointId=e1").url(),
kEpBeta + "/endpoints?endpointId=e1");
ASSERT_EQ(v1.plan_call("endpoint.deploy_model", "11", nullptr).url(),
kEp + "/endpoints/11:deployModel");
ASSERT_EQ(v1.plan_call("endpoint.operation_wait", "11", nullptr, "timeout=5s", {{"operation", "9"}})
.url(),
kEp + "/endpoints/11/operations/9:wait?timeout=5s");
ASSERT_EQ(v1.plan_call("endpoint.deployed_model_invoke", "11", nullptr, "",
{{"deployed", "4"}, {"invoke", "predict"}})
.url(),
kEp + "/endpoints/11/deployedModels/4/invoke/predict");
ASSERT_EQ(v1.plan_call("endpoint.openapi_embeddings", "").url(),
kEp + "/endpoints/openapi/embeddings");
// beta only rows are beta in either mode
ASSERT_EQ(v1.plan_call("endpoint.beta_get_iam_policy", "11").url(),
kEpBeta + "/endpoints/11:getIamPolicy");
ASSERT_TRUE(v1.plan_call("endpoint.delete", "11").method == molo::clients::Method::kDelete);
// the planner refuses a beta twin for a non endpoint row
CallPlanner p(project());
ASSERT_THROW(p.plan("gemini.generate", {{"publisher", "google"}, {"model", "m"}}, nullptr, "", true),
GoogleError);
}
TEST(models_admin, stray_body_keys_are_rejected_by_the_planner) {
EndpointAdmin a(client(false), project());
Json bad = obj("notAField", Json(1));
ASSERT_THROW(a.plan_call("endpoint.update", "11", &bad), GoogleError);
Json ok = obj("endpoint", Json::object());
a.plan_call("endpoint.update", "11", &ok);
// raw rows ("-") take any body
a.plan_call("endpoint.chat_completions", "11", &bad);
}
TEST(models_admin, endpoint_validation_and_confirm_gates) {
MockTransport* mock = nullptr;
EndpointAdmin a(client(true, &mock), project());
Json o = Json::object();
ASSERT_TRUE(rejected(mock, [&] { a.destroy("11", false); }));
ASSERT_TRUE(rejected(mock, [&] { a.destroy("a/b", true); }));
ASSERT_TRUE(rejected(mock, [&] { a.deploy_model("11", obj("model", Json("m")), o, false); }));
ASSERT_TRUE(rejected(mock, [&] { a.deploy_model("11", Json("not an object"), o, true); }));
ASSERT_TRUE(rejected(mock, [&] { a.undeploy_model("11", "d1", o, false); }));
ASSERT_TRUE(rejected(mock, [&] { a.undeploy_model("11", "d/1", o, true); }));
ASSERT_TRUE(rejected(mock, [&] { a.mutate_deployed_model("11", o, "bad mask!", true); }));
ASSERT_TRUE(rejected(mock, [&] { a.mutate_deployed_model("11", o, "minReplicaCount", false); }));
ASSERT_TRUE(rejected(mock, [&] { a.patch("11", o, ""); }));
ASSERT_TRUE(rejected(mock, [&] { a.patch("11", Json::array(), "displayName"); }));
ASSERT_TRUE(rejected(mock, [&] { a.create(o, "bad id"); }));
ASSERT_TRUE(rejected(mock, [&] { a.delete_operation("11", "5", false); }));
ASSERT_TRUE(rejected(mock, [&] { a.delete_response("11", "5", false); }));
ASSERT_TRUE(rejected(mock, [&] { a.set_iam_policy("11", o, false); }));
ASSERT_TRUE(rejected(mock, [&] { a.wait_operation("11", "5", 0); }));
ASSERT_TRUE(rejected(mock, [&] {
a.wait_operation("11", "5", ModelsCaps::kMaxOperationTimeoutSeconds + 1);
}));
ASSERT_TRUE(rejected(mock, [&] { a.list_operations("11", ModelsCaps::kMaxOperationPages + 1); }));
ASSERT_TRUE(rejected(mock, [&] { a.get_iam_policy("11", 4); }));
ASSERT_TRUE(rejected(mock, [&] { a.test_iam_permissions("11", {}); }));
ASSERT_TRUE(rejected(mock, [&] { a.test_iam_permissions("11", {"a b"}); }));
ASSERT_TRUE(rejected(mock, [&] { a.openapi("images", o); }));
ASSERT_TRUE(rejected(mock, [&] { a.direct_predict("11", Json::array()); }));
ASSERT_TRUE(rejected(mock, [&] { a.direct_raw_predict("11", "bad method", "AAAA"); }));
ASSERT_TRUE(rejected(mock, [&] { a.direct_raw_predict("11", "pkg.Svc/M", "not base64!"); }));
ASSERT_TRUE(rejected(mock, [&] { a.explain("11", Json::array()); }));
ASSERT_TRUE(rejected(mock, [&] { a.compute_tokens("11", Json::array()); }));
ASSERT_TRUE(rejected(mock, [&] { a.invoke("11", "../x", o); }));
ASSERT_TRUE(rejected(mock, [&] { a.get_response("11", "a/b"); }));
Json big = obj("endpoint", Json(std::string(ModelsCaps::kMaxAdminBodyChars + 1, 'x')));
ASSERT_TRUE(rejected(mock, [&] { a.update("11", big); }));
ASSERT_EQ(mock->send_count(), 0u);
}
TEST(models_admin, publisher_urls_validation_and_gates) {
MockTransport* mock = nullptr;
PublisherAdmin p(client(true, &mock), project());
ASSERT_EQ(p.plan_call("beta.export", {{"publisher", "google"}, {"model", "gemma3"}}).url(),
kEpBeta + "/publishers/google/models/gemma3:export");
ASSERT_EQ(p.plan_call("beta.enable_model", {{"publisher", "google"}, {"model", "gemma3"}}).url(),
"https://aiplatform.googleapis.com/v1beta1/projects/demo-proj/publishers/google/models/gemma3:enableModel");
ASSERT_EQ(p.plan_call("publisher.responses_compact", {{"publisher", "google"}}).url(),
kEp + "/publishers/google/v1/responses/compact");
ASSERT_EQ(p.plan_call("beta.responses_get", {{"publisher", "google"}, {"response", "r1"}}).url(),
kEpBeta + "/publishers/google/v1/responses/r1");
ASSERT_EQ(p.plan_call("publisher.server_streaming_predict",
{{"publisher", "google"}, {"model", "m"}})
.url(),
kEp + "/publishers/google/models/m:serverStreamingPredict");
Json o = Json::object();
ASSERT_TRUE(rejected(mock, [&] { p.export_model("google", "m", "gs://b/x", false); }));
ASSERT_TRUE(rejected(mock, [&] { p.export_model("google", "m", "http://b/x", true); }));
ASSERT_TRUE(rejected(mock, [&] { p.set_config("google", "m", o, "x", false); }));
ASSERT_TRUE(rejected(mock, [&] { p.set_config("google", "m", o, "", true); }));
ASSERT_TRUE(rejected(mock, [&] { p.enable_model("google", "m", "svc", false); }));
ASSERT_TRUE(rejected(mock, [&] { p.enable_model("google", "m", "bad service!", true); }));
ASSERT_TRUE(rejected(mock, [&] { p.responses_delete("google", "r1", false); }));
ASSERT_TRUE(rejected(mock, [&] { p.responses_get("a/b", "r1"); }));
ASSERT_TRUE(rejected(mock, [&] { p.get_iam_policy("google", "m", 9); }));
ASSERT_TRUE(rejected(mock, [&] { p.beta_post("beta.generate", "google", "m", o); }));
ASSERT_TRUE(rejected(mock, [&] { p.beta_compute_tokens("google", "m", Json::array()); }));
ASSERT_TRUE(rejected(mock, [&] {
p.server_streaming_predict("google", "m", Json::array(), o, nullptr);
}));
ASSERT_EQ(mock->send_count(), 0u);
}
TEST(models_admin, unconfigured_admin_calls_refuse_and_never_send) {
MockTransport* mock = nullptr;
auto c = client(false, &mock);
EndpointAdmin a(c, project());
PublisherAdmin p(c, project());
Json o = Json::object();
ASSERT_THROW(a.destroy("11", true), NotConfiguredError);
ASSERT_THROW(a.list_operations("11"), NotConfiguredError);
ASSERT_THROW(a.operation("11", "5"), NotConfiguredError);
ASSERT_THROW(a.openapi("completions", o), NotConfiguredError);
ASSERT_THROW(a.get_iam_policy("11"), NotConfiguredError);
ASSERT_THROW(p.get_iam_policy("google", "m"), NotConfiguredError);
ASSERT_THROW(p.responses_get("google", "r1"), NotConfiguredError);
ASSERT_THROW(p.beta_get("google", "m"), NotConfiguredError);
ASSERT_EQ(mock->send_count(), 0u);
}
// --- thought signatures ------------------------------------------------------
namespace {
GenerateResponse chunk(std::vector<Part> parts) {
GenerateResponse r;
Candidate c;
c.content.role = "model";
c.content.parts = std::move(parts);
r.candidates.push_back(std::move(c));
return r;
}
Part text(const std::string& t, const std::string& sig = "", bool thought = false) {
Part p = Part::of_text(t);
p.thought_signature = sig;
p.thought = thought;
return p;
}
} // namespace
TEST(models_signature, turn_builder_keeps_every_signature) {
VertexModel::TurnBuilder b;
b.add(chunk({text("Hel")}));
b.add(chunk({text("lo ")}));
b.add(chunk({text("world", "SIG1")})); // carries a signature
ASSERT_EQ(b.content().parts.size(), 1u);
ASSERT_EQ(b.content().parts[0].text, "Hello world");
ASSERT_EQ(b.signatures(), 1u);
// a part that has a signature is never merged into
b.add(chunk({text(" more")}));
ASSERT_EQ(b.content().parts.size(), 2u);
ASSERT_EQ(b.content().parts[0].thought_signature, "SIG1");
// a function call stays a separate part with its signature
Part call = Part::of_call(FunctionCall{"get_weather", obj("city", Json("Paris")), ""});
call.thought_signature = "SIG3";
b.add(chunk({call}));
ASSERT_EQ(b.content().parts.size(), 3u);
ASSERT_EQ(b.content().parts[2].thought_signature, "SIG3");
ASSERT_EQ(b.signatures(), 2u);
// a signature that arrives alone attaches to the text before it
VertexModel::TurnBuilder late;
late.add(chunk({text("answer")}));
late.add(chunk({text("", "LATE")}));
ASSERT_EQ(late.content().parts.size(), 1u);
ASSERT_EQ(late.content().parts[0].thought_signature, "LATE");
// the stream ends with an empty text chunk: it must not become an empty part
VertexModel::TurnBuilder tail;
tail.add(chunk({call}));
tail.add(chunk({text("")}));
ASSERT_EQ(tail.content().parts.size(), 1u);
// thought text and answer text are not merged
VertexModel::TurnBuilder t;
t.add(chunk({text("thinking", "", true)}));
t.add(chunk({text("answer")}));
ASSERT_EQ(t.content().parts.size(), 2u);
}
TEST(models_signature, replayed_turn_sends_the_signature_back) {
Part call = Part::of_call(FunctionCall{"get_weather", obj("city", Json("Paris")), "c1"});
call.thought_signature = "c2ln-base64";
GenerateResponse reply = chunk({call});
Content turn = VertexModel::model_turn(reply);
GenerateRequest r;
r.contents.push_back(Content::user("weather in Paris?"));
Part fr = Part::of_response(FunctionResponse{"get_weather", obj("temp", Json(20)), "c1"});
Content user;
user.role = "user";
user.parts.push_back(fr);
VertexModel::replay(r, turn, user);
ASSERT_EQ(r.contents.size(), 3u);
Gemini g(client(false), VertexTarget::express());
PlannedCall p = g.plan_generate("gemini-3.5-flash", r);
const Json& parts = *p.body.find("contents")->as_array()[1].find("parts");
ASSERT_EQ(parts.as_array()[0].find("thoughtSignature")->as_string(), "c2ln-base64");
// a signature-only part is a valid part (validate does not call it empty)
GenerateRequest only;
Content m;
m.role = "model";
Part sig;
sig.thought_signature = "abc";
m.parts.push_back(sig);
only.contents.push_back(Content::user("x"));
only.contents.push_back(m);
g.plan_generate("gemini-3.5-flash", only);
ASSERT_THROW(VertexModel::model_turn(GenerateResponse{}), GoogleError);
}
TEST(models_media, lyria_wav_bytes_name_their_mime) {
// "RIFF....WAVE" is what lyria-002 returned live (bytesBase64Encoded only).
Json item = Json::object();
item.set("bytesBase64Encoded", Json("UklGRnwAwABXQVZFZm10IBAAAAABAAIAgLsAAADuAgAEABAA"));
Json list = Json::array();
list.push(item);
auto items = MediaGenerator::items_from(list);
ASSERT_EQ(items.size(), 1u);
ASSERT_EQ(items[0].mime, "audio/wav");
}
@@ -0,0 +1,320 @@
// Agent Runtime, closed gaps: v1beta1 feedbackEntries, A2A gateway, a2aTasks,
// task store records, scoped operations, the beta twin. Deterministic and
// local: request construction against the call table (checked against
// Discovery by src/google/runtime/tools/check_discovery.py), validation,
// caps, confirm gates, no-credential behavior. No Google replies are scripted.
#include <memory>
#include <set>
#include "clients/transports/mock_transport.h"
#include "framework.h"
#include "google/credentials.h"
#include "google/runtime/a2a.h"
#include "google/runtime/agent_runtime.h"
#include "google/runtime/calls.h"
#include "google/runtime/feedback.h"
#include "google/runtime/operation_calls.h"
using namespace molo::google;
using namespace molo::google::runtime;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
namespace {
struct Rig {
MockTransport* mock = nullptr;
std::shared_ptr<GoogleClient> client;
CloudContext ctx;
explicit Rig(bool configured = false) {
auto t = std::make_unique<MockTransport>();
mock = t.get();
client = std::make_shared<GoogleClient>(std::move(t),
configured ? Credentials::bearer("tok") : nullptr);
ctx.project = "p1";
ctx.location = "us-central1";
}
};
// True when validation rejects the call; false when it reached the
// credential check (valid input) or did anything else.
template <typename F>
bool invalid(F&& f) {
try {
f();
} catch (const NotConfiguredError&) {
return false;
} catch (const GoogleError& e) {
return e.http_status() == 400 && e.status() == "INVALID_ARGUMENT";
}
return false;
}
template <typename F>
bool reaches_credential_check(F&& f) {
try {
f();
} catch (const NotConfiguredError&) {
return true;
} catch (...) {
}
return false;
}
const std::string kEngine = "projects/p1/locations/us-central1/reasoningEngines/e1";
} // namespace
TEST(runtime_gaps, every_enum_value_has_exactly_one_row) {
std::set<int> seen;
for (std::size_t i = 0; i < call_count(); ++i) {
const CallSpec& s = spec_at(i);
ASSERT_TRUE(seen.insert(static_cast<int>(s.call)).second);
ASSERT_TRUE(std::string(s.rpc_id).rfind("aiplatform.projects.locations.", 0) == 0);
std::string v = s.version;
ASSERT_TRUE(v == "v1" || v == "v1beta1");
ASSERT_TRUE(std::string(s.path).rfind(v + "/", 0) == 0);
}
ASSERT_TRUE(call_count() >= 103);
for (int c = static_cast<int>(Call::kCreate); c <= static_cast<int>(Call::kTaskRecordEventsList); ++c)
ASSERT_EQ(static_cast<int>(spec_of(static_cast<Call>(c)).call), c);
ASSERT_EQ(seen.size(), call_count());
}
TEST(runtime_gaps, every_row_renders_a_concrete_path) {
for (std::size_t i = 0; i < call_count(); ++i) {
const CallSpec& s = spec_at(i);
std::string path = render(s.call, kEngine, "card");
ASSERT_TRUE(path.find('{') == std::string::npos);
ASSERT_TRUE(path.rfind("/projects/", 0) == 0);
}
ASSERT_THROW(render(Call::kA2aEngineCard, kEngine, "../x"), GoogleError);
ASSERT_THROW(render(Call::kA2aEngineCard, kEngine, ""), GoogleError);
ASSERT_THROW(render(Call::kA2aEngineCard, kEngine, "a b"), GoogleError);
ASSERT_EQ(render(Call::kA2aEngineV1MessageSend, kEngine, "v1/message"),
"/" + kEngine + "/a2a/v1/message:send");
}
TEST(runtime_gaps, beta_twin_selects_the_v1beta1_endpoint) {
ASSERT_EQ(endpoint_of(Call::kGet, "us-central1").base_url,
"https://us-central1-aiplatform.googleapis.com/v1");
ASSERT_EQ(endpoint_of(Call::kGet, "us-central1", true).base_url,
"https://us-central1-aiplatform.googleapis.com/v1beta1");
ASSERT_EQ(endpoint_of(Call::kFeedbackList, "us-central1").base_url,
"https://us-central1-aiplatform.googleapis.com/v1beta1");
Rig r;
AgentRuntime beta(r.client, r.ctx, nullptr, true);
ASSERT_TRUE(beta.beta());
ASSERT_EQ(beta.endpoint().base_url, "https://us-central1-aiplatform.googleapis.com/v1beta1");
AgentRuntime v1(r.client, r.ctx);
ASSERT_EQ(v1.endpoint().base_url, "https://us-central1-aiplatform.googleapis.com/v1");
}
TEST(runtime_a2a, every_op_builds_the_path_discovery_declares) {
Rig r;
RuntimeA2a a2a(r.client, r.ctx);
struct Case {
A2aOp op;
const char* tail; // after /a2a/ (plain)
const char* verb; // suffix appended by the rpc path
};
const Case cases[] = {
{A2aOp::kCard, "card", ""},
{A2aOp::kExtendedAgentCard, "extendedAgentCard", ""},
{A2aOp::kTasks, "tasks", ""},
{A2aOp::kTaskGet, "tasks/t1", ""},
{A2aOp::kPushConfigs, "tasks/t1/pushNotificationConfigs", ""},
{A2aOp::kPushConfigGet, "tasks/t1/pushNotificationConfigs/c1", ""},
{A2aOp::kTaskCancel, "tasks/t1", ":cancel"},
{A2aOp::kTaskSubscribe, "tasks/t1", ":subscribe"},
{A2aOp::kMessageSend, "message", ":send"},
{A2aOp::kMessageStream, "message", ":stream"},
};
for (A2aTarget target : {A2aTarget::kEngine, A2aTarget::kRevision})
for (A2aProtocol proto : {A2aProtocol::kPlain, A2aProtocol::kV1})
for (const Case& c : cases) {
A2aRequest q;
q.target = target;
q.protocol = proto;
q.op = c.op;
q.engine_id = "e1";
if (target == A2aTarget::kRevision) q.revision_id = "rev9";
if (std::string(c.tail).find("t1") != std::string::npos) q.task_id = "t1";
if (std::string(c.tail).find("c1") != std::string::npos) q.config_id = "c1";
std::string want = "/" + kEngine +
(target == A2aTarget::kRevision ? "/runtimeRevisions/rev9" : "") +
"/a2a/" + (proto == A2aProtocol::kV1 ? "v1/" : "") + c.tail + c.verb;
ASSERT_EQ(a2a.path_of(q), want);
// the row behind it is the one Discovery names for this (target, protocol, op)
std::string rpc = spec_of(RuntimeA2a::call_of(target, proto, c.op)).rpc_id;
ASSERT_TRUE(rpc.find(target == A2aTarget::kRevision ? "runtimeRevisions.a2a." : ".a2a.") !=
std::string::npos);
ASSERT_EQ(rpc.find(".v1.") != std::string::npos, proto == A2aProtocol::kV1);
}
}
TEST(runtime_a2a, validation_and_caps) {
Rig r;
RuntimeA2a a2a(r.client, r.ctx);
A2aRequest q;
q.engine_id = "e1";
q.op = A2aOp::kTaskGet;
ASSERT_TRUE(invalid([&] { a2a.path_of(q); })); // task id missing
q.task_id = "a/b";
ASSERT_TRUE(invalid([&] { a2a.path_of(q); }));
q.task_id = "t1";
q.history_length = A2aCaps::kMaxHistoryLength + 1;
ASSERT_TRUE(invalid([&] { a2a.path_of(q); }));
q.history_length = 5;
ASSERT_EQ(a2a.path_of(q), "/" + kEngine + "/a2a/tasks/t1?historyLength=5");
A2aRequest card;
card.engine_id = "e1";
card.config_id = "c";
ASSERT_TRUE(invalid([&] { a2a.path_of(card); })); // config id on an op without one
card.config_id.clear();
card.revision_id = "rev";
ASSERT_TRUE(invalid([&] { a2a.path_of(card); })); // revision id on an engine target
A2aRequest send;
send.engine_id = "e1";
send.op = A2aOp::kMessageSend;
send.history_length = 3;
ASSERT_TRUE(invalid([&] { a2a.path_of(send); })); // history is for GET ops
send.history_length = 0;
send.body = Json("text");
ASSERT_TRUE(invalid([&] { a2a.call(send); }));
send.body = Json::object();
ASSERT_TRUE(reaches_credential_check([&] { a2a.call(send); }));
A2aRequest streaming;
streaming.engine_id = "e1";
streaming.op = A2aOp::kMessageStream;
ASSERT_TRUE(invalid([&] { a2a.call(streaming); }));
A2aRequest sub;
sub.engine_id = "e1";
sub.op = A2aOp::kTaskSubscribe;
sub.task_id = "t1";
ASSERT_TRUE(invalid([&] { a2a.call(sub); }));
ASSERT_TRUE(invalid([&] { a2a.stream(sub, nullptr); }));
ASSERT_TRUE(invalid([&] { a2a.subscribe(card); }));
ASSERT_TRUE(reaches_credential_check([&] { a2a.subscribe(sub); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(runtime_a2a, tasks_and_records_validation) {
Rig r;
RuntimeA2a a2a(r.client, r.ctx);
A2aTaskInfo t;
t.context_id = "ctx";
t.metadata = Json::object();
ASSERT_TRUE(invalid([&] { a2a.create_task("e1", "", t); }));
ASSERT_TRUE(invalid([&] { a2a.create_task("e1", "Upper", t); }));
ASSERT_TRUE(invalid([&] { a2a.create_task("e1", "-lead", t); }));
ASSERT_TRUE(invalid([&] { a2a.create_task("e1", std::string(64, 'a'), t); }));
ASSERT_TRUE(reaches_credential_check([&] { a2a.create_task("e1", "task-1", t); }));
A2aTaskInfo bad;
bad.metadata = Json("not an object");
ASSERT_TRUE(invalid([&] { a2a.create_task("e1", "task-1", bad); }));
ASSERT_TRUE(invalid([&] { a2a.delete_task("e1", "t1", false); }));
ASSERT_TRUE(invalid([&] { a2a.delete_record("s1", "r1", false); }));
ASSERT_TRUE(invalid([&] { a2a.get_task("e1", "t1", "lower"); }));
ASSERT_TRUE(invalid([&] { a2a.list_tasks("e1", std::string(513, 'f')); }));
ASSERT_TRUE(invalid([&] { a2a.append_task_events("e1", "t1", Json::array()); }));
ASSERT_TRUE(invalid([&] { a2a.append_task_events("e1", "t1", Json("x")); }));
Json events = Json::array();
events.push(Json::object());
ASSERT_TRUE(reaches_credential_check([&] { a2a.append_task_events("e1", "t1", events, "7"); }));
ASSERT_TRUE(invalid([&] { a2a.create_record("s/1", "r1", t); }));
ASSERT_TRUE(invalid([&] { a2a.get_record("s1", "a/b"); }));
ASSERT_TRUE(reaches_credential_check([&] { a2a.create_record("s1", "", t); }));
ASSERT_TRUE(reaches_credential_check([&] { a2a.list_record_events("s1", "r1"); }));
ASSERT_EQ(a2a.store_name("s1"), "projects/p1/locations/us-central1/taskStores/s1");
// writable fields only
Json body = t.to_json();
ASSERT_TRUE(body.find("contextId") != nullptr);
ASSERT_TRUE(body.find("state") == nullptr);
A2aTaskInfo parsed = A2aTaskInfo::from_json(Json::parse(
"{\"name\":\"n\",\"status\":{\"state\":\"TASK_STATE_WORKING\"},\"generation\":\"3\"}"));
ASSERT_EQ(parsed.state, "TASK_STATE_WORKING");
ASSERT_EQ(parsed.generation, "3");
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(runtime_feedback, entry_body_mask_and_gates) {
Rig r;
RuntimeFeedback fb(r.client, r.ctx);
FeedbackEntry e;
e.feedback_type = "THUMBS_DOWN";
e.feedback_text = "wrong answer";
e.feedback_labels = {"accuracy", "tone"};
e.session_id = "s1";
Json body = e.to_json();
ASSERT_TRUE(body.find("feedbackLabels") != nullptr);
ASSERT_TRUE(body.find("createTime") == nullptr); // output only: never sent
ASSERT_EQ(e.update_mask(), "sessionId,feedbackType,feedbackText,feedbackLabels");
ASSERT_EQ(fb.entry_name("e1", "f1"), kEngine + "/feedbackEntries/f1");
ASSERT_TRUE(reaches_credential_check([&] { fb.create("e1", e); }));
ASSERT_TRUE(reaches_credential_check([&] { fb.patch("e1", "f1", e); }));
ASSERT_TRUE(invalid([&] { fb.create("e1", FeedbackEntry{}); })); // nothing to send
ASSERT_TRUE(invalid([&] { fb.patch("e1", "f1", FeedbackEntry{}); }));
ASSERT_TRUE(invalid([&] { fb.patch("e1", "a/b", e); }));
ASSERT_TRUE(invalid([&] { fb.destroy("e1", "f1", false); }));
FeedbackEntry big;
big.feedback_text = std::string(FeedbackCaps::kMaxTextChars + 1, 'x');
ASSERT_TRUE(invalid([&] { fb.create("e1", big); }));
FeedbackEntry many;
many.feedback_labels.assign(FeedbackCaps::kMaxLabels + 1, "l");
ASSERT_TRUE(invalid([&] { fb.create("e1", many); }));
ASSERT_TRUE(invalid([&] { fb.update_context("e1", "f1", Json::array()); }));
ASSERT_TRUE(invalid([&] { fb.list("e1", std::string(513, 'f')); }));
ASSERT_TRUE(invalid([&] { fb.get("e1", "a/b"); }));
ASSERT_THROW(fb.list("e1"), NotConfiguredError);
ASSERT_THROW(fb.get_context("e1", "f1"), NotConfiguredError);
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(runtime_operations, scopes_names_and_gates) {
ASSERT_TRUE(OperationCalls::scope_from(kEngine + "/operations/1") == OperationScope::kEngine);
ASSERT_TRUE(OperationCalls::scope_from(kEngine + "/runtimeRevisions/r/operations/1") ==
OperationScope::kRevision);
ASSERT_TRUE(OperationCalls::scope_from(kEngine + "/feedbackEntries/f/operations/1") ==
OperationScope::kFeedback);
ASSERT_TRUE(OperationCalls::scope_from(kEngine + "/examples/x/operations/1") ==
OperationScope::kExample);
ASSERT_TRUE(OperationCalls::scope_from("projects/p1/locations/l/taskStores/s/operations/1") ==
OperationScope::kTaskStore);
ASSERT_THROW(OperationCalls::scope_from("projects/p1/operations/1"), GoogleError);
// a name only passes for its own scope
ASSERT_THROW(OperationCalls::check(OperationScope::kEngine, kEngine + "/feedbackEntries/f/operations/1"),
GoogleError);
ASSERT_THROW(OperationCalls::check(OperationScope::kRevision, kEngine + "/operations/1"),
GoogleError);
ASSERT_THROW(OperationCalls::check(OperationScope::kEngine, "x/" + kEngine + "/operations/1"),
GoogleError);
ASSERT_THROW(OperationCalls::check(OperationScope::kEngine, kEngine + "/operations/../x"),
GoogleError);
ASSERT_THROW(OperationCalls::check(OperationScope::kEngine, kEngine + "/operations/1?x=1"),
GoogleError);
ASSERT_FALSE(OperationCalls::has_list(OperationScope::kExample));
// every scope maps its verbs onto distinct rows
std::set<int> calls;
for (OperationScope s : {OperationScope::kEngine, OperationScope::kRevision,
OperationScope::kFeedback, OperationScope::kTaskStore})
for (const char* verb : {"get", "list", "cancel", "wait", "delete"})
ASSERT_TRUE(calls.insert(static_cast<int>(OperationCalls::call_for(s, verb))).second);
Rig r;
OperationCalls ops(r.client, "us-central1", false);
const std::string rev = kEngine + "/runtimeRevisions/r/operations/1";
ASSERT_TRUE(invalid([&] { ops.wait(OperationScope::kRevision, rev, 0); }));
ASSERT_TRUE(invalid([&] { ops.wait(OperationScope::kRevision, rev, ScopedOperationCaps::kMaxWaitSeconds + 1); }));
ASSERT_TRUE(invalid([&] { ops.remove(OperationScope::kRevision, rev, false); }));
ASSERT_TRUE(invalid([&] { ops.list(OperationScope::kExample, kEngine + "/examples/x"); }));
ASSERT_TRUE(invalid([&] { ops.list(OperationScope::kFeedback, kEngine); })); // wrong parent
ASSERT_TRUE(invalid([&] { ops.get(OperationScope::kEngine, rev); }));
ASSERT_THROW(ops.wait(OperationScope::kRevision, rev, 30), NotConfiguredError);
ASSERT_THROW(ops.list(OperationScope::kFeedback, kEngine + "/feedbackEntries/f"),
NotConfiguredError);
ASSERT_EQ(r.mock->send_count(), 0u);
// AgentRuntime hands out the same helper
AgentRuntime rt(r.client, r.ctx, nullptr, true);
ASSERT_THROW(rt.operations().get(OperationScope::kEngine, kEngine + "/operations/1"),
NotConfiguredError);
}
@@ -0,0 +1,79 @@
// Opt-in live, read-only checks for the closed gaps (MOLO_LIVE_NET=1,
// GOOGLE_ACCESS_TOKEN, GOOGLE_CLOUD_PROJECT): the v1beta1 resources of an
// engine that does not exist answer 404 NOT_FOUND, which proves the route,
// the auth and the error mapping without touching anything. Nothing is
// created, changed or deleted.
#include <cstdlib>
#include <iostream>
#include "framework.h"
#include "google/credentials.h"
#include "google/errors.h"
#include "google/runtime/a2a.h"
#include "google/runtime/agent_runtime.h"
#include "google/runtime/feedback.h"
using namespace molo::google;
using namespace molo::google::runtime;
namespace {
bool ready() {
return std::getenv("MOLO_LIVE_NET") && !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() &&
!Credentials::env("GOOGLE_CLOUD_PROJECT").empty();
}
template <typename F>
void expect_status(const char* what, int want, F&& f) {
try {
f();
FAIL(std::string(what) + ": expected " + std::to_string(want) + ", got success");
} catch (const GoogleError& e) {
std::cout << " " << what << ": " << e.http_status() << " " << e.status() << "\n";
ASSERT_EQ(e.http_status(), want);
}
}
} // namespace
TEST(runtime_live_extra, v1beta1_resources_of_a_missing_engine) {
if (!ready()) {
std::cout << "SKIP runtime_live_extra.v1beta1_resources_of_a_missing_engine (set MOLO_LIVE_NET=1, "
"GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n";
return;
}
CloudContext ctx = CloudContext::from_env();
ctx.location = "us-central1";
auto client = GoogleClient::from_env();
AgentRuntime v1(client, ctx);
AgentRuntime beta(client, ctx, nullptr, true);
const std::string missing = "1234567890123";
std::cout << " engines (v1): " << v1.list().size() << ", (v1beta1): " << beta.list().size() << "\n";
ASSERT_EQ(v1.list().size(), beta.list().size());
expect_status("v1beta1 get of a missing engine", 404, [&] { beta.get(missing); });
expect_status("v1 IAM policy of a missing engine", 404, [&] { v1.get_iam_policy(missing); });
expect_status("revisions list of a missing engine", 404, [&] { v1.list_revisions(missing); });
RuntimeFeedback fb(client, ctx);
expect_status("feedback list of a missing engine", 404, [&] { fb.list(missing); });
RuntimeA2a a2a(client, ctx);
expect_status("a2aTasks list of a missing engine", 404, [&] { a2a.list_tasks(missing); });
A2aRequest card;
card.engine_id = missing;
card.protocol = A2aProtocol::kV1;
expect_status("a2a v1 card of a missing engine", 404, [&] { a2a.call(card); });
card.protocol = A2aProtocol::kPlain;
expect_status("a2a card of a missing engine", 404, [&] { a2a.call(card); });
// operations of an engine: a missing engine has none, and the list answers 200.
OperationCalls ops = v1.operations();
auto listed = ops.list(OperationScope::kEngine, ctx.child("reasoningEngines", missing));
std::cout << " operations of a missing engine: " << listed.size() << "\n";
ASSERT_EQ(listed.size(), 0u);
expect_status("operation get of a missing engine", 404, [&] {
ops.get(OperationScope::kEngine, ctx.child("reasoningEngines", missing) + "/operations/1");
});
// The task store route answered 501 (not implemented) in this project.
try {
a2a.list_records("s1");
std::cout << " task store list: 200\n";
} catch (const GoogleError& e) {
std::cout << " task store list: " << e.http_status() << " " << e.status() << "\n";
ASSERT_TRUE(e.http_status() == 404 || e.http_status() == 501 || e.http_status() == 403);
}
}