revka 2026.6.11

Revka — memory-native AI agent runtime powered by Kumiho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! REST API handlers for local gcloud configuration profiles.
//!
//! These endpoints expose metadata-only gcloud config management for the
//! workflow editor's private Cloud Run A2A fields. A gcloud configuration is
//! not a secret itself; it references credentials in the local Cloud SDK
//! credential store. Responses therefore include display metadata only and
//! never include access tokens, refresh tokens, or identity tokens.

use super::AppState;
use super::api::require_auth;
use axum::{
    extract::State,
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Json},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::process::Output;
use std::time::Duration;
use tokio::process::Command;

const GCLOUD_TIMEOUT_SECS: u64 = 20;
const GCLOUD_LIST_FORMAT: &str = "json(name,is_active,properties.core.account,properties.core.project,properties.run.region,properties.compute.region)";

#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
pub struct GcloudConfigSummary {
    pub name: String,
    pub is_active: bool,
    pub account: Option<String>,
    pub project: Option<String>,
    pub run_region: Option<String>,
    pub compute_region: Option<String>,
}

#[derive(Serialize)]
pub struct ListGcloudConfigsResponse {
    pub available: bool,
    pub configs: Vec<GcloudConfigSummary>,
    pub error: Option<String>,
}

#[derive(Deserialize)]
pub struct CreateGcloudConfigBody {
    pub name: String,
    pub project: String,
    #[serde(default)]
    pub account: Option<String>,
    #[serde(default)]
    pub run_region: Option<String>,
    #[serde(default)]
    pub compute_region: Option<String>,
}

pub async fn handle_list_gcloud_configs(
    State(state): State<AppState>,
    headers: HeaderMap,
) -> impl IntoResponse {
    if let Err(e) = require_auth(&state, &headers) {
        return e.into_response();
    }

    match list_gcloud_configs().await {
        Ok(configs) => Json(ListGcloudConfigsResponse {
            available: true,
            configs,
            error: None,
        })
        .into_response(),
        Err(GcloudCommandError::NotFound) => Json(ListGcloudConfigsResponse {
            available: false,
            configs: vec![],
            error: Some("gcloud was not found in PATH".to_string()),
        })
        .into_response(),
        Err(err) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({
                "error": "Failed to list gcloud configurations",
                "code": "gcloud_configs_list_failed",
                "detail": err.public_message(),
            })),
        )
            .into_response(),
    }
}

pub async fn handle_create_gcloud_config(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(body): Json<CreateGcloudConfigBody>,
) -> impl IntoResponse {
    if let Err(e) = require_auth(&state, &headers) {
        return e.into_response();
    }

    let name = match validate_config_name(&body.name) {
        Ok(name) => name,
        Err(message) => return bad_request(message, "gcloud_config_invalid_name"),
    };
    let project = match validate_property_value("project", &body.project, true) {
        Ok(project) => project,
        Err(message) => return bad_request(message, "gcloud_config_invalid_project"),
    };
    let account = match validate_optional_property("account", body.account.as_deref()) {
        Ok(account) => account,
        Err(message) => return bad_request(message, "gcloud_config_invalid_account"),
    };
    let run_region = match validate_optional_property("run_region", body.run_region.as_deref()) {
        Ok(region) => region,
        Err(message) => return bad_request(message, "gcloud_config_invalid_run_region"),
    };
    let compute_region =
        match validate_optional_property("compute_region", body.compute_region.as_deref()) {
            Ok(region) => region,
            Err(message) => return bad_request(message, "gcloud_config_invalid_compute_region"),
        };

    let existing = match list_gcloud_configs().await {
        Ok(configs) => configs,
        Err(GcloudCommandError::NotFound) => {
            return (
                StatusCode::SERVICE_UNAVAILABLE,
                Json(serde_json::json!({
                    "error": "gcloud was not found in PATH",
                    "code": "gcloud_not_found"
                })),
            )
                .into_response();
        }
        Err(err) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(serde_json::json!({
                    "error": "Failed to inspect existing gcloud configurations",
                    "code": "gcloud_configs_list_failed",
                    "detail": err.public_message(),
                })),
            )
                .into_response();
        }
    };

    if existing.iter().any(|config| config.name == name) {
        return (
            StatusCode::CONFLICT,
            Json(serde_json::json!({
                "error": format!("gcloud configuration already exists: {name}"),
                "code": "gcloud_config_already_exists"
            })),
        )
            .into_response();
    }

    let create_args = vec![
        "config".to_string(),
        "configurations".to_string(),
        "create".to_string(),
        name.clone(),
        "--no-activate".to_string(),
        "--quiet".to_string(),
    ];
    if let Err(err) = run_gcloud_checked(create_args).await {
        return gcloud_create_error("Failed to create gcloud configuration", err);
    }

    let mut set_ops: Vec<(&str, String)> = vec![("core/project", project.clone())];
    if let Some(value) = account.clone() {
        set_ops.push(("core/account", value));
    }
    if let Some(value) = run_region.clone() {
        set_ops.push(("run/region", value));
    }
    if let Some(value) = compute_region.clone() {
        set_ops.push(("compute/region", value));
    }

    for (key, value) in set_ops {
        let args = vec![
            format!("--configuration={name}"),
            "config".to_string(),
            "set".to_string(),
            key.to_string(),
            value,
            "--quiet".to_string(),
        ];
        if let Err(err) = run_gcloud_checked(args).await {
            return gcloud_create_error("Failed to set gcloud configuration property", err);
        }
    }

    let created = list_gcloud_configs()
        .await
        .ok()
        .and_then(|configs| configs.into_iter().find(|config| config.name == name))
        .unwrap_or(GcloudConfigSummary {
            name,
            is_active: false,
            account,
            project: Some(project),
            run_region,
            compute_region,
        });

    (StatusCode::CREATED, Json(created)).into_response()
}

fn bad_request(message: String, code: &'static str) -> axum::response::Response {
    (
        StatusCode::BAD_REQUEST,
        Json(serde_json::json!({
            "error": message,
            "code": code
        })),
    )
        .into_response()
}

fn gcloud_create_error(message: &'static str, err: GcloudCommandError) -> axum::response::Response {
    let status = match &err {
        GcloudCommandError::NotFound => StatusCode::SERVICE_UNAVAILABLE,
        GcloudCommandError::TimedOut | GcloudCommandError::Failed(_) => {
            StatusCode::INTERNAL_SERVER_ERROR
        }
    };
    (
        status,
        Json(serde_json::json!({
            "error": message,
            "code": "gcloud_config_create_failed",
            "detail": err.public_message(),
        })),
    )
        .into_response()
}

async fn list_gcloud_configs() -> Result<Vec<GcloudConfigSummary>, GcloudCommandError> {
    let args = vec![
        "config".to_string(),
        "configurations".to_string(),
        "list".to_string(),
        format!("--format={GCLOUD_LIST_FORMAT}"),
    ];
    let output = run_gcloud_checked(args).await?;
    let parsed: Value = serde_json::from_slice(&output.stdout).map_err(|err| {
        GcloudCommandError::Failed(format!("failed to parse gcloud config list output: {err}"))
    })?;
    Ok(parse_gcloud_config_list(&parsed))
}

async fn run_gcloud_checked(args: Vec<String>) -> Result<Output, GcloudCommandError> {
    let mut command = Command::new("gcloud");
    command
        .args(args)
        .env("CLOUDSDK_CORE_DISABLE_PROMPTS", "1")
        .env("CLOUDSDK_CORE_LOG_HTTP", "false")
        .stdin(std::process::Stdio::null());

    let output = match tokio::time::timeout(
        Duration::from_secs(GCLOUD_TIMEOUT_SECS),
        command.output(),
    )
    .await
    {
        Ok(Ok(output)) => output,
        Ok(Err(err)) if err.kind() == std::io::ErrorKind::NotFound => {
            return Err(GcloudCommandError::NotFound);
        }
        Ok(Err(err)) => {
            return Err(GcloudCommandError::Failed(format!(
                "failed to execute gcloud: {err}"
            )));
        }
        Err(_) => return Err(GcloudCommandError::TimedOut),
    };

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
        return Err(GcloudCommandError::Failed(if stderr.is_empty() {
            stdout
        } else {
            stderr
        }));
    }

    Ok(output)
}

fn parse_gcloud_config_list(value: &Value) -> Vec<GcloudConfigSummary> {
    let Some(items) = value.as_array() else {
        return vec![];
    };
    let mut configs: Vec<GcloudConfigSummary> = items
        .iter()
        .filter_map(|item| {
            let name = string_at(item, &["name"])?;
            Some(GcloudConfigSummary {
                name,
                is_active: item
                    .get("is_active")
                    .and_then(Value::as_bool)
                    .unwrap_or(false),
                account: string_at(item, &["properties", "core", "account"]),
                project: string_at(item, &["properties", "core", "project"]),
                run_region: string_at(item, &["properties", "run", "region"]),
                compute_region: string_at(item, &["properties", "compute", "region"]),
            })
        })
        .collect();
    configs.sort_by(|a, b| {
        b.is_active
            .cmp(&a.is_active)
            .then_with(|| a.name.cmp(&b.name))
    });
    configs
}

fn string_at(value: &Value, path: &[&str]) -> Option<String> {
    let mut cursor = value;
    for key in path {
        cursor = cursor.get(*key)?;
    }
    cursor
        .as_str()
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(str::to_string)
}

fn validate_config_name(value: &str) -> Result<String, String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err("configuration name is required".to_string());
    }
    if trimmed.len() > 64 {
        return Err("configuration name must be 64 characters or fewer".to_string());
    }
    let mut chars = trimmed.chars();
    let Some(first) = chars.next() else {
        return Err("configuration name is required".to_string());
    };
    if !first.is_ascii_alphanumeric() {
        return Err("configuration name must start with an ASCII letter or number".to_string());
    }
    if !chars.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.')) {
        return Err(
            "configuration name may contain only ASCII letters, numbers, '-', '_', and '.'"
                .to_string(),
        );
    }
    Ok(trimmed.to_string())
}

fn validate_optional_property(label: &str, value: Option<&str>) -> Result<Option<String>, String> {
    match value {
        Some(value) if value.trim().is_empty() => Ok(None),
        Some(value) => validate_property_value(label, value, false).map(Some),
        None => Ok(None),
    }
}

fn validate_property_value(label: &str, value: &str, required: bool) -> Result<String, String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        if required {
            return Err(format!("{label} is required"));
        }
        return Ok(String::new());
    }
    if trimmed.len() > 256 {
        return Err(format!("{label} must be 256 characters or fewer"));
    }
    if trimmed.chars().any(|ch| ch.is_control()) {
        return Err(format!("{label} may not contain control characters"));
    }
    Ok(trimmed.to_string())
}

#[derive(Debug)]
enum GcloudCommandError {
    NotFound,
    TimedOut,
    Failed(String),
}

impl GcloudCommandError {
    fn public_message(&self) -> String {
        match self {
            GcloudCommandError::NotFound => "gcloud was not found in PATH".to_string(),
            GcloudCommandError::TimedOut => {
                format!("gcloud command timed out after {GCLOUD_TIMEOUT_SECS}s")
            }
            GcloudCommandError::Failed(message) => truncate(message, 500),
        }
    }
}

fn truncate(value: &str, max: usize) -> String {
    if value.chars().count() <= max {
        return value.to_string();
    }
    format!("{}...", value.chars().take(max).collect::<String>())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_config_name_accepts_safe_names() {
        assert_eq!(
            validate_config_name("revka-track3").unwrap(),
            "revka-track3"
        );
        assert_eq!(validate_config_name("team.prod_1").unwrap(), "team.prod_1");
    }

    #[test]
    fn validate_config_name_rejects_shell_like_or_path_names() {
        assert!(validate_config_name("-prod").is_err());
        assert!(validate_config_name("team/prod").is_err());
        assert!(validate_config_name("team prod").is_err());
        assert!(validate_config_name("prod;rm").is_err());
    }

    #[test]
    fn parse_gcloud_config_list_extracts_metadata() {
        let raw = serde_json::json!([
            {
                "name": "default",
                "is_active": true,
                "properties": {
                    "core": {
                        "account": "support@example.com",
                        "project": "construct-498201"
                    },
                    "run": { "region": "us-central1" },
                    "compute": { "region": "us-central1" }
                }
            }
        ]);

        assert_eq!(
            parse_gcloud_config_list(&raw),
            vec![GcloudConfigSummary {
                name: "default".to_string(),
                is_active: true,
                account: Some("support@example.com".to_string()),
                project: Some("construct-498201".to_string()),
                run_region: Some("us-central1".to_string()),
                compute_region: Some("us-central1".to_string()),
            }]
        );
    }
}