feat(clients): sandbox: Agent Sandbox lifecycle, code/shell/computer-use execution, templates, snapshots
Typed results with hard caps, one call table checked against Discovery (tools/check_discovery.py), live read-only probe, /agent/sandbox demo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
#include "google/sandbox/names.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::string> split(const std::string& s) {
|
||||
std::vector<std::string> out;
|
||||
std::string cur;
|
||||
for (char c : s) {
|
||||
if (c == '/') {
|
||||
out.push_back(cur);
|
||||
cur.clear();
|
||||
} else {
|
||||
cur.push_back(c);
|
||||
}
|
||||
}
|
||||
out.push_back(cur);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool project_ok(const std::string& p) {
|
||||
if (p.empty() || p.size() > 128) return false;
|
||||
for (char c : p)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' ||
|
||||
c == '.' || c == ':'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool location_ok(const std::string& l) {
|
||||
if (l.empty() || l.size() > 64) return false;
|
||||
for (char c : l)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse(const std::string& name, std::size_t segments,
|
||||
SandboxNames::Parts& out, const char* collection = "sandboxEnvironments") {
|
||||
if (name.empty() || name.size() > SandboxCaps::kMaxResourceNameChars) return false;
|
||||
std::vector<std::string> p = split(name);
|
||||
if (p.size() != segments) return false;
|
||||
if (p[0] != "projects" || p[2] != "locations" || p[4] != "reasoningEngines")
|
||||
return false;
|
||||
if (!project_ok(p[1]) || !location_ok(p[3]) || !valid_segment(p[5])) return false;
|
||||
if (segments == 8) {
|
||||
if (p[6] != collection || !valid_segment(p[7])) return false;
|
||||
out.sandbox = p[7];
|
||||
}
|
||||
out.project = p[1];
|
||||
out.location = p[3];
|
||||
out.engine = p[5];
|
||||
return true;
|
||||
}
|
||||
|
||||
// True when no "." or ".." or empty segment exists (separator '/').
|
||||
bool segments_plain(const std::string& s, bool allow_empty_first) {
|
||||
std::size_t start = 0;
|
||||
bool first = true;
|
||||
while (start <= s.size()) {
|
||||
std::size_t end = s.find('/', start);
|
||||
if (end == std::string::npos) end = s.size();
|
||||
std::string seg = s.substr(start, end - start);
|
||||
if (seg == ".." || seg == ".") return false;
|
||||
if (seg.empty() && !(first && allow_empty_first)) return false;
|
||||
first = false;
|
||||
start = end + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool SandboxNames::valid_engine(const std::string& name) {
|
||||
Parts p;
|
||||
return parse(name, 6, p);
|
||||
}
|
||||
|
||||
bool SandboxNames::valid_sandbox(const std::string& name) {
|
||||
Parts p;
|
||||
return parse(name, 8, p);
|
||||
}
|
||||
|
||||
bool SandboxNames::valid_template(const std::string& name) {
|
||||
Parts p;
|
||||
return parse(name, 8, p, "sandboxEnvironmentTemplates");
|
||||
}
|
||||
|
||||
bool SandboxNames::valid_snapshot(const std::string& name) {
|
||||
Parts p;
|
||||
return parse(name, 8, p, "sandboxEnvironmentSnapshots");
|
||||
}
|
||||
|
||||
SandboxNames::Parts SandboxNames::parse_template(const std::string& name) {
|
||||
Parts p;
|
||||
if (!parse(name, 8, p, "sandboxEnvironmentTemplates"))
|
||||
throw GoogleError::invalid(
|
||||
"template must look like projects/{p}/locations/{l}/reasoningEngines/{e}"
|
||||
"/sandboxEnvironmentTemplates/{t}");
|
||||
return p;
|
||||
}
|
||||
|
||||
SandboxNames::Parts SandboxNames::parse_snapshot(const std::string& name) {
|
||||
Parts p;
|
||||
if (!parse(name, 8, p, "sandboxEnvironmentSnapshots"))
|
||||
throw GoogleError::invalid(
|
||||
"snapshot must look like projects/{p}/locations/{l}/reasoningEngines/{e}"
|
||||
"/sandboxEnvironmentSnapshots/{n}");
|
||||
return p;
|
||||
}
|
||||
|
||||
SandboxNames::Parts SandboxNames::parse_engine(const std::string& name) {
|
||||
Parts p;
|
||||
if (!parse(name, 6, p))
|
||||
throw GoogleError::invalid(
|
||||
"engine must look like projects/{p}/locations/{l}/reasoningEngines/{e}");
|
||||
return p;
|
||||
}
|
||||
|
||||
SandboxNames::Parts SandboxNames::parse_sandbox(const std::string& name) {
|
||||
Parts p;
|
||||
if (!parse(name, 8, p))
|
||||
throw GoogleError::invalid(
|
||||
"sandbox must look like projects/{p}/locations/{l}/reasoningEngines/{e}"
|
||||
"/sandboxEnvironments/{s}");
|
||||
return p;
|
||||
}
|
||||
|
||||
std::string SandboxNames::engine_of(const std::string& sandbox_name) {
|
||||
Parts p = parse_sandbox(sandbox_name);
|
||||
return "projects/" + p.project + "/locations/" + p.location +
|
||||
"/reasoningEngines/" + p.engine;
|
||||
}
|
||||
|
||||
bool valid_file_name(const std::string& name) {
|
||||
if (name.empty() || name.size() > SandboxCaps::kMaxFileNameChars) return false;
|
||||
if (name[0] == '/') return false;
|
||||
for (char c : name)
|
||||
if (static_cast<unsigned char>(c) < 0x20 || c == 0x7f || c == '\\') return false;
|
||||
return segments_plain(name, false);
|
||||
}
|
||||
|
||||
bool valid_workspace_path(const std::string& path) {
|
||||
if (path.empty() || path.size() > SandboxCaps::kMaxPathChars) return false;
|
||||
for (char c : path)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-' || c == '/'))
|
||||
return false;
|
||||
// A trailing slash (directory) is fine: "/workspace/".
|
||||
std::string trimmed = path.size() > 1 && path.back() == '/' ? path.substr(0, path.size() - 1) : path;
|
||||
return segments_plain(trimmed, true);
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
Reference in New Issue
Block a user