feat(clients): sessions+registry+optimize+armor: close gaps, loud list truncation, shared Gemini adapter
sessions: operations get/list/cancel/delete/wait for engine, session, memory and serving profile; servingProfiles CRUD. registry: ListResult with truncated flag, v1alpha twins, gateway/policy operations, authz IAM, deny policy update, boundary patch/bindings. optimize: make_gemini_model over models::VertexModel (DirectModel removed), generateSyntheticData, online evaluator operations, trace/log writers. armor: SecurityAdmin for all SCC v2 methods (attack exposure excluded), documented agent threat categories, floor settings fixed to location global after live run. check_discovery.py of all four enforce coverage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,353 @@
|
||||
// Gap closing for Agent Sessions and Memory Bank: operations (get, list,
|
||||
// cancel, delete, wait) under the engine, a session, a memory and a serving
|
||||
// profile; servingProfiles; lists that stop loudly. Requests are inspected as
|
||||
// the mock saw them; nothing answers (no scripted Google replies).
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
#include "framework.h"
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "google/credentials.h"
|
||||
#include "google/sessions/calls.h"
|
||||
#include "google/sessions/memory_bank.h"
|
||||
#include "google/sessions/operation_names.h"
|
||||
#include "google/sessions/serving_profile.h"
|
||||
#include "google/sessions/sessions.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::sessions;
|
||||
using molo::clients::body::Json;
|
||||
using molo::clients::transports::MockTransport;
|
||||
|
||||
namespace {
|
||||
|
||||
const char* kEngine = "projects/p1/locations/us-central1/reasoningEngines/e1";
|
||||
const char* kLocation = "projects/p1/locations/us-central1";
|
||||
|
||||
struct Rig {
|
||||
MockTransport* mock;
|
||||
std::shared_ptr<GoogleClient> client;
|
||||
explicit Rig(bool with_credential = true) {
|
||||
auto t = std::make_unique<MockTransport>();
|
||||
mock = t.get();
|
||||
client = std::make_shared<GoogleClient>(
|
||||
std::move(t), with_credential ? Credentials::bearer("tok") : nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
bool has(const std::string& hay, const std::string& needle) {
|
||||
return hay.find(needle) != std::string::npos;
|
||||
}
|
||||
|
||||
std::set<std::string> words(const std::string& s) {
|
||||
std::set<std::string> out;
|
||||
std::istringstream in(s);
|
||||
for (std::string w; in >> w;) out.insert(w);
|
||||
return out;
|
||||
}
|
||||
|
||||
void keys_declared(const Json& body, Call call) {
|
||||
std::set<std::string> allowed = words(spec_of(call).fields);
|
||||
ASSERT_TRUE(body.is_object());
|
||||
for (const auto& [key, value] : body.as_object()) {
|
||||
(void)value;
|
||||
if (!allowed.count(key)) FAIL("body key '" << key << "' not in the table row");
|
||||
}
|
||||
}
|
||||
|
||||
void invalid(const std::function<void()>& fn) {
|
||||
try {
|
||||
fn();
|
||||
} catch (const GoogleError& e) {
|
||||
ASSERT_EQ(e.http_status(), 400);
|
||||
ASSERT_EQ(e.status(), std::string("INVALID_ARGUMENT"));
|
||||
ASSERT_TRUE(e.service().empty()); // local validation, not upstream
|
||||
return;
|
||||
}
|
||||
FAIL("expected INVALID_ARGUMENT");
|
||||
}
|
||||
|
||||
// Runs fn (which fails: the mock has no expectation) and returns the one
|
||||
// request that reached the transport.
|
||||
MockTransport::SeenRequest sent(Rig& rig, const std::function<void()>& fn) {
|
||||
std::size_t before = rig.mock->seen().size();
|
||||
try {
|
||||
fn();
|
||||
} catch (...) {
|
||||
}
|
||||
auto all = rig.mock->seen();
|
||||
if (all.size() != before + 1)
|
||||
FAIL("expected exactly one request, saw " << all.size() - before);
|
||||
return all.back();
|
||||
}
|
||||
|
||||
std::string engine_op(const std::string& middle = "") {
|
||||
return std::string(kEngine) + middle + "/operations/op1";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(sessions_operations, every_owner_has_all_five_verbs_in_the_table) {
|
||||
const std::string e = kEngine;
|
||||
const std::string sp = std::string(kLocation) + "/servingProfiles/sp1/operations/op1";
|
||||
struct Case {
|
||||
std::string root, name;
|
||||
OperationOwner owner;
|
||||
};
|
||||
std::vector<Case> cases = {{e, engine_op(), OperationOwner::kEngine},
|
||||
{e, engine_op("/sessions/s1"), OperationOwner::kSession},
|
||||
{e, engine_op("/memories/m1"), OperationOwner::kMemory},
|
||||
{kLocation, sp, OperationOwner::kServingProfile}};
|
||||
std::set<int> rows;
|
||||
for (const Case& c : cases) {
|
||||
OperationName n = OperationName::parse(c.root, c.name);
|
||||
ASSERT_EQ(static_cast<int>(n.owner()), static_cast<int>(c.owner));
|
||||
ASSERT_EQ(n.str(), c.name);
|
||||
for (OperationVerb v : {OperationVerb::kGet, OperationVerb::kCancel, OperationVerb::kList,
|
||||
OperationVerb::kDelete, OperationVerb::kWait}) {
|
||||
ASSERT_TRUE(rows.insert(static_cast<int>(n.call(v))).second); // all distinct
|
||||
ASSERT_TRUE(has(spec_of(n.call(v)).method, "operations."));
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(rows.size(), std::size_t(20));
|
||||
}
|
||||
|
||||
TEST(sessions_operations, names_are_confined_to_their_root) {
|
||||
const std::string e = kEngine;
|
||||
for (const char* bad :
|
||||
{"", "operations/1",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/other/operations/1",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/operations/..",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/operations//x",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/jobs/j/operations/1",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/sessions/s/events/e/operations/1",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/servingProfiles/p/operations/1",
|
||||
"projects/p1/locations/us-central1/reasoningEngines/e1/operations/1?x=1"})
|
||||
invalid([&] { OperationName::parse(e, bad); });
|
||||
// A location root only serves serving profiles.
|
||||
invalid([&] { OperationName::parse(kLocation, std::string(kLocation) + "/operations/1"); });
|
||||
invalid([&] {
|
||||
OperationName::parse(kLocation, std::string(kLocation) + "/sessions/s/operations/1");
|
||||
});
|
||||
invalid([&] { OperationName::parse_parent(kLocation, kLocation); });
|
||||
OperationName::parse_parent(e, e);
|
||||
OperationName::parse_parent(e, e + "/sessions/s1");
|
||||
invalid([&] { OperationName::parse_parent(e, e + "/sessions"); });
|
||||
invalid([&] { OperationName::parse_parent(e, e + "/other/x"); });
|
||||
}
|
||||
|
||||
TEST(sessions_operations, requests_use_the_declared_rows) {
|
||||
Rig rig;
|
||||
Sessions s(rig.client, EngineName(kEngine));
|
||||
MemoryBank b(rig.client, EngineName(kEngine));
|
||||
auto r = sent(rig, [&] { s.operation(engine_op()); });
|
||||
ASSERT_EQ(r.method, std::string("GET"));
|
||||
ASSERT_TRUE(has(r.target, "/v1beta1/" + engine_op()));
|
||||
r = sent(rig, [&] { s.delete_operation(engine_op()); });
|
||||
ASSERT_EQ(r.method, std::string("DELETE"));
|
||||
ASSERT_TRUE(has(r.target, "/v1beta1/" + engine_op()));
|
||||
r = sent(rig, [&] { s.cancel_operation(engine_op("/sessions/s1")); });
|
||||
ASSERT_EQ(r.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(r.target, engine_op("/sessions/s1") + ":cancel"));
|
||||
r = sent(rig, [&] { b.wait_server(engine_op("/memories/m1"), 30); });
|
||||
ASSERT_EQ(r.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(r.target, engine_op("/memories/m1") + ":wait?timeout=30s"));
|
||||
r = sent(rig, [&] { s.list_operations("", {"done=true", 10}); });
|
||||
ASSERT_EQ(r.method, std::string("GET"));
|
||||
ASSERT_TRUE(has(r.target, std::string(kEngine) + "/operations?"));
|
||||
ASSERT_TRUE(has(r.target, "pageSize=10"));
|
||||
ASSERT_TRUE(has(r.target, "filter=done%3Dtrue"));
|
||||
r = sent(rig, [&] { s.list_session_operations("s1"); });
|
||||
ASSERT_TRUE(has(r.target, std::string(kEngine) + "/sessions/s1/operations"));
|
||||
r = sent(rig, [&] { b.list_memory_operations("m1"); });
|
||||
ASSERT_TRUE(has(r.target, std::string(kEngine) + "/memories/m1/operations"));
|
||||
}
|
||||
|
||||
TEST(sessions_operations, bad_input_never_reaches_the_network) {
|
||||
Rig rig;
|
||||
Sessions s(rig.client, EngineName(kEngine));
|
||||
invalid([&] { s.wait_server(engine_op(), 0); });
|
||||
invalid([&] { s.wait_server(engine_op(), 61); });
|
||||
invalid([&] { s.wait_server("nope", 5); });
|
||||
invalid([&] { s.delete_operation(engine_op() + "?x"); });
|
||||
invalid([&] { s.list_operations("", {"a\nb", 0}); });
|
||||
invalid([&] { s.list_operations("", {"", 101}); });
|
||||
invalid([&] { s.list_operations(std::string(kEngine) + "/sessions/BAD ID"); });
|
||||
invalid([&] { s.list_session_operations("Bad_Id"); });
|
||||
ASSERT_EQ(rig.mock->send_count(), std::size_t(0));
|
||||
Rig off(false);
|
||||
Sessions n(off.client, EngineName(kEngine));
|
||||
ASSERT_THROW(n.delete_operation(engine_op()), NotConfiguredError);
|
||||
ASSERT_THROW(n.list_operations(), NotConfiguredError);
|
||||
ASSERT_THROW(n.wait_server(engine_op(), 5), NotConfiguredError);
|
||||
ASSERT_EQ(off.mock->send_count(), std::size_t(0));
|
||||
}
|
||||
|
||||
TEST(sessions_operations, lists_stop_loudly_instead_of_truncating) {
|
||||
ASSERT_FALSE(list_over_limit(100, false, 0, 10, 1000));
|
||||
ASSERT_FALSE(list_over_limit(1000, false, 9, 10, 1000));
|
||||
ASSERT_TRUE(list_over_limit(1001, false, 3, 10, 1000));
|
||||
ASSERT_TRUE(list_over_limit(10, true, 9, 10, 1000)); // page 10 used, more left
|
||||
ASSERT_FALSE(list_over_limit(10, true, 8, 10, 1000)); // page 9, one more allowed
|
||||
}
|
||||
|
||||
TEST(sessions_serving_profiles, scope_enum_and_names) {
|
||||
ASSERT_EQ(std::string(serving_scope_name(ServingScope::kGeminiLive)),
|
||||
std::string("GEMINI_LIVE"));
|
||||
ASSERT_EQ(std::string(serving_scope_name(ServingScope::kInteractionsApi)),
|
||||
std::string("INTERACTIONS_API"));
|
||||
ASSERT_EQ(std::string(serving_scope_name(serving_scope_from("RESPONSE_API"))),
|
||||
std::string("RESPONSE_API"));
|
||||
for (const char* bad : {"", "gemini_live", "SERVING_PROFILE_SCOPE_UNSPECIFIED", "ALL"})
|
||||
invalid([&] { serving_scope_from(bad); });
|
||||
LocationName loc(kLocation);
|
||||
ASSERT_EQ(loc.location(), std::string("us-central1"));
|
||||
ASSERT_EQ(loc.serving_profile("sp-1"), std::string(kLocation) + "/servingProfiles/sp-1");
|
||||
for (const char* bad : {"", "projects/p1", "projects/p1/locations", "projects/P/locations/l",
|
||||
"projects/p1/locations/us-central1/x", "projects/p/locations/L",
|
||||
"projects/p/locations/"})
|
||||
invalid([&] { LocationName n{std::string(bad)}; });
|
||||
invalid([&] { loc.serving_profile("Bad_Id"); });
|
||||
invalid([&] { loc.serving_profile(""); });
|
||||
}
|
||||
|
||||
TEST(sessions_serving_profiles, bodies_use_only_declared_fields) {
|
||||
ServingProfileSpec spec;
|
||||
spec.profile_id = "sp-1";
|
||||
spec.display_name = "Live profile";
|
||||
spec.description = "d";
|
||||
spec.scope = ServingScope::kResponseApi;
|
||||
spec.kms_key_name = "projects/p1/locations/us-central1/keyRings/k/cryptoKeys/c";
|
||||
spec.validate();
|
||||
Json body = spec.to_json();
|
||||
keys_declared(body, Call::kServingProfileCreate);
|
||||
ASSERT_EQ(body.find("scope")->as_string(), std::string("RESPONSE_API"));
|
||||
ASSERT_EQ(body.find("cmekConfig")->find("encryptionSpec")->find("kmsKeyName")->as_string(),
|
||||
spec.kms_key_name);
|
||||
ServingProfilePatch patch;
|
||||
patch.profile_id = "sp-1";
|
||||
patch.display_name = "x";
|
||||
patch.description = "y";
|
||||
patch.kms_key_name = spec.kms_key_name;
|
||||
patch.update_mask = "displayName,description,cmekConfig";
|
||||
patch.validate();
|
||||
keys_declared(patch.to_json(), Call::kServingProfilePatch);
|
||||
|
||||
ServingProfileSpec bad = spec;
|
||||
bad.display_name = "";
|
||||
invalid([&] { bad.validate(); });
|
||||
bad = spec;
|
||||
bad.display_name = std::string(129, 'x');
|
||||
invalid([&] { bad.validate(); });
|
||||
bad = spec;
|
||||
bad.profile_id = "A";
|
||||
invalid([&] { bad.validate(); });
|
||||
bad = spec;
|
||||
bad.kms_key_name = "keys/1";
|
||||
invalid([&] { bad.validate(); });
|
||||
bad = spec;
|
||||
bad.description = std::string(4001, 'x');
|
||||
invalid([&] { bad.validate(); });
|
||||
ServingProfilePatch bp = patch;
|
||||
bp.update_mask = "";
|
||||
invalid([&] { bp.validate(); });
|
||||
bp.update_mask = "display name";
|
||||
invalid([&] { bp.validate(); });
|
||||
|
||||
ServingProfile p = ServingProfile::from_json(Json::parse(
|
||||
"{\"name\":\"projects/p1/locations/us-central1/servingProfiles/sp-1\","
|
||||
"\"displayName\":\"n\",\"scope\":\"GEMINI_LIVE\","
|
||||
"\"cmekConfig\":{\"encryptionSpec\":{\"kmsKeyName\":\"k\"}}}"));
|
||||
ASSERT_EQ(p.id(), std::string("sp-1"));
|
||||
ASSERT_EQ(p.kms_key_name, std::string("k"));
|
||||
ASSERT_EQ(p.scope, std::string("GEMINI_LIVE"));
|
||||
}
|
||||
|
||||
TEST(sessions_serving_profiles, requests_use_the_declared_rows) {
|
||||
Rig rig;
|
||||
ServingProfiles sp(rig.client, LocationName(kLocation));
|
||||
ASSERT_EQ(sp.endpoint().base_url,
|
||||
std::string("https://us-central1-aiplatform.googleapis.com/v1beta1"));
|
||||
ServingProfileSpec spec;
|
||||
spec.profile_id = "sp-1";
|
||||
spec.display_name = "Live";
|
||||
spec.scope = ServingScope::kGeminiLive;
|
||||
auto r = sent(rig, [&] { sp.create(spec); });
|
||||
ASSERT_EQ(r.method, std::string("POST"));
|
||||
ASSERT_TRUE(has(r.target, std::string(kLocation) + "/servingProfiles?servingProfileId=sp-1"));
|
||||
ASSERT_TRUE(has(r.body, "\"scope\":\"GEMINI_LIVE\""));
|
||||
r = sent(rig, [&] { sp.get("sp-1"); });
|
||||
ASSERT_EQ(r.method, std::string("GET"));
|
||||
ASSERT_TRUE(has(r.target, std::string(kLocation) + "/servingProfiles/sp-1"));
|
||||
r = sent(rig, [&] { sp.list(25); });
|
||||
ASSERT_TRUE(has(r.target, "/servingProfiles?pageSize=25"));
|
||||
ServingProfilePatch patch;
|
||||
patch.profile_id = "sp-1";
|
||||
patch.display_name = "new";
|
||||
patch.update_mask = "displayName";
|
||||
r = sent(rig, [&] { sp.patch(patch); });
|
||||
ASSERT_EQ(r.method, std::string("PATCH"));
|
||||
ASSERT_TRUE(has(r.target, "/servingProfiles/sp-1?updateMask=displayName"));
|
||||
r = sent(rig, [&] { sp.remove("sp-1"); });
|
||||
ASSERT_EQ(r.method, std::string("DELETE"));
|
||||
r = sent(rig, [&] { sp.list_profile_operations("sp-1"); });
|
||||
ASSERT_TRUE(has(r.target, "/servingProfiles/sp-1/operations"));
|
||||
r = sent(rig, [&] {
|
||||
sp.wait_server(std::string(kLocation) + "/servingProfiles/sp-1/operations/o1", 5);
|
||||
});
|
||||
ASSERT_TRUE(has(r.target, "/servingProfiles/sp-1/operations/o1:wait?timeout=5s"));
|
||||
// The location root has no operations of its own.
|
||||
invalid([&] { sp.list_operations(); });
|
||||
invalid([&] { sp.operation(engine_op()); });
|
||||
}
|
||||
|
||||
TEST(sessions_serving_profiles, validation_and_credentials_run_before_traffic) {
|
||||
Rig rig;
|
||||
ServingProfiles sp(rig.client, LocationName(kLocation));
|
||||
invalid([&] { sp.get("Bad_Id"); });
|
||||
invalid([&] { sp.remove(""); });
|
||||
invalid([&] { sp.create(ServingProfileSpec{}); });
|
||||
invalid([&] { sp.list(101); });
|
||||
ASSERT_EQ(rig.mock->send_count(), std::size_t(0));
|
||||
Rig off(false);
|
||||
ServingProfiles n(off.client, LocationName(kLocation));
|
||||
ASSERT_THROW(n.get("sp-1"), NotConfiguredError);
|
||||
ASSERT_THROW(n.list(), NotConfiguredError);
|
||||
ASSERT_EQ(off.mock->send_count(), std::size_t(0));
|
||||
}
|
||||
|
||||
// Read-only live probe of the gap-closing calls (MOLO_LIVE_NET=1, a
|
||||
// credential, GOOGLE_CLOUD_PROJECT; loud SKIP otherwise). Observed
|
||||
// 2026-10-24 with a service-account token: operations lists under any engine,
|
||||
// session or memory answer 200 with an empty list even for a missing engine;
|
||||
// servingProfiles list answers 501 UNIMPLEMENTED (not available to the
|
||||
// project). Either answer is accepted for servingProfiles and reported.
|
||||
TEST(sessions_live, operations_and_serving_profiles_reads) {
|
||||
std::string project = Credentials::env("GOOGLE_CLOUD_PROJECT");
|
||||
bool has_credential =
|
||||
!Credentials::env().empty() || !Credentials::env("GOOGLE_ACCESS_TOKEN").empty();
|
||||
if (!std::getenv("MOLO_LIVE_NET") || !has_credential || project.empty()) {
|
||||
std::cout << "SKIP sessions_live gaps (set MOLO_LIVE_NET=1, a credential and "
|
||||
"GOOGLE_CLOUD_PROJECT)\n";
|
||||
return;
|
||||
}
|
||||
auto client = GoogleClient::from_env();
|
||||
EngineName engine("projects/" + project + "/locations/us-central1/reasoningEngines/1");
|
||||
Sessions s(client, engine);
|
||||
MemoryBank b(client, engine);
|
||||
ASSERT_EQ(s.list_operations().size(), std::size_t(0));
|
||||
ASSERT_EQ(s.list_session_operations("probe").size(), std::size_t(0));
|
||||
ASSERT_EQ(b.list_memory_operations("probe").size(), std::size_t(0));
|
||||
ServingProfiles sp(client, LocationName("projects/" + project + "/locations/us-central1"));
|
||||
try {
|
||||
std::size_t n = sp.list(5).size();
|
||||
std::cout << "servingProfiles list: 200, " << n << " profiles\n";
|
||||
} catch (const GoogleError& e) {
|
||||
std::cout << "servingProfiles list: " << e.http_status() << " " << e.status() << "\n";
|
||||
ASSERT_TRUE(e.http_status() == 501 || e.http_status() == 403 || e.http_status() == 404);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user