kumiho-construct 2026.5.11

Construct — 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
//! Mutating Asset Browser API routes.
//!
//! The generic `/api/kumiho/*` proxy is GET-only by design. These routes keep
//! Asset Browser writes typed and idempotency-aware while still delegating the
//! persistent model to Kumiho.

use super::AppState;
use super::api::require_auth;
use super::api_agents::build_kumiho_client;
use super::kumiho_client::{
    ArtifactResponse, KumihoError, RevisionResponse, kumiho_error_to_response,
};
use axum::{
    Json,
    extract::State,
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

const MAX_EDIT_BYTES: usize = 1024 * 1024;

#[derive(Deserialize)]
pub struct DeprecateItemBody {
    pub kref: String,
    pub deprecated: bool,
}

#[derive(Deserialize)]
pub struct DeprecateRevisionBody {
    pub kref: String,
    pub deprecated: bool,
}

#[derive(Deserialize)]
pub struct DeprecateArtifactBody {
    pub kref: String,
    pub deprecated: bool,
}

#[derive(Deserialize)]
pub struct PublishRevisionBody {
    pub kref: String,
}

#[derive(Deserialize)]
pub struct UpdateArtifactContentBody {
    pub artifact_kref: String,
    pub revision_kref: String,
    pub content: String,
}

#[derive(Serialize)]
pub struct UpdateArtifactContentResponse {
    pub revision: RevisionResponse,
    pub artifact: ArtifactResponse,
    pub created_revision: bool,
    pub copied_artifacts: usize,
}

fn kumiho_err(e: KumihoError) -> Response {
    kumiho_error_to_response(e)
}

fn json_error(status: StatusCode, msg: impl Into<String>) -> Response {
    (
        status,
        Json(serde_json::json!({
            "error": msg.into(),
        })),
    )
        .into_response()
}

fn is_published(revision: &RevisionResponse) -> bool {
    revision.tags.iter().any(|tag| tag == "published")
}

fn resolve_location(raw: &str) -> Result<PathBuf, String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return Err("artifact location is empty".to_string());
    }

    let stripped = trimmed.strip_prefix("file://").unwrap_or(trimmed);
    let expanded = if let Some(rest) = stripped.strip_prefix("~/") {
        match directories::UserDirs::new() {
            Some(dirs) => dirs.home_dir().join(rest),
            None => return Err("cannot resolve '~': no home directory".to_string()),
        }
    } else if stripped == "~" {
        match directories::UserDirs::new() {
            Some(dirs) => dirs.home_dir().to_path_buf(),
            None => return Err("cannot resolve '~': no home directory".to_string()),
        }
    } else {
        PathBuf::from(stripped)
    };

    if !Path::new(&expanded).is_absolute() {
        return Err("artifact location must be an absolute path".to_string());
    }

    Ok(expanded)
}

fn location_uses_file_uri(raw: &str) -> bool {
    raw.trim().starts_with("file://")
}

fn location_from_path(path: &Path, file_uri: bool) -> String {
    let path = path.display().to_string();
    if file_uri {
        format!("file://{path}")
    } else {
        path
    }
}

fn revision_location(path: &Path, revision_number: i32) -> PathBuf {
    let parent = path.parent().unwrap_or_else(|| Path::new("/"));
    let stem = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("artifact");
    let ext = path.extension().and_then(|s| s.to_str());
    let file_name = match ext {
        Some(ext) if !ext.is_empty() => format!("{stem}.r{revision_number}.{ext}"),
        _ => format!("{stem}.r{revision_number}"),
    };
    parent.join(file_name)
}

async fn unique_revision_location(path: &Path, revision_number: i32) -> PathBuf {
    let base = revision_location(path, revision_number);
    if tokio::fs::metadata(&base).await.is_err() {
        return base;
    }

    let parent = base.parent().unwrap_or_else(|| Path::new("/"));
    let stem = base
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("artifact");
    let ext = base.extension().and_then(|s| s.to_str());
    for i in 2..100 {
        let name = match ext {
            Some(ext) if !ext.is_empty() => format!("{stem}.{i}.{ext}"),
            _ => format!("{stem}.{i}"),
        };
        let candidate = parent.join(name);
        if tokio::fs::metadata(&candidate).await.is_err() {
            return candidate;
        }
    }
    base
}

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

    match build_kumiho_client(&state)
        .deprecate_item(&body.kref, body.deprecated)
        .await
    {
        Ok(item) => Json(serde_json::json!({ "item": item })).into_response(),
        Err(e) => kumiho_err(e),
    }
}

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

    match build_kumiho_client(&state)
        .deprecate_revision(&body.kref, body.deprecated)
        .await
    {
        Ok(revision) => Json(serde_json::json!({ "revision": revision })).into_response(),
        Err(e) => kumiho_err(e),
    }
}

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

    match build_kumiho_client(&state)
        .deprecate_artifact(&body.kref, body.deprecated)
        .await
    {
        Ok(artifact) => Json(serde_json::json!({ "artifact": artifact })).into_response(),
        Err(e) => kumiho_err(e),
    }
}

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

    let client = build_kumiho_client(&state);
    if let Err(e) = client.tag_revision(&body.kref, "published").await {
        return kumiho_err(e);
    }

    match client.get_revision(&body.kref).await {
        Ok(revision) => Json(serde_json::json!({ "revision": revision })).into_response(),
        Err(e) => kumiho_err(e),
    }
}

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

    if body.content.len() > MAX_EDIT_BYTES {
        return json_error(
            StatusCode::PAYLOAD_TOO_LARGE,
            format!("artifact edits are limited to {} bytes", MAX_EDIT_BYTES),
        );
    }

    let client = build_kumiho_client(&state);
    let source_revision = match client.get_revision(&body.revision_kref).await {
        Ok(revision) => revision,
        Err(e) => return kumiho_err(e),
    };
    let source_artifact = match client.get_artifact(&body.artifact_kref).await {
        Ok(artifact) => artifact,
        Err(e) => return kumiho_err(e),
    };

    if source_artifact.revision_kref != source_revision.kref {
        return json_error(
            StatusCode::BAD_REQUEST,
            "artifact does not belong to the selected revision",
        );
    }

    let source_path = match resolve_location(&source_artifact.location) {
        Ok(path) => path,
        Err(msg) => return json_error(StatusCode::BAD_REQUEST, msg),
    };
    let file_uri = location_uses_file_uri(&source_artifact.location);

    if !is_published(&source_revision) {
        if let Err(e) = tokio::fs::write(&source_path, body.content.as_bytes()).await {
            return json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("failed to write artifact content: {e}"),
            );
        }
        return Json(UpdateArtifactContentResponse {
            revision: source_revision,
            artifact: source_artifact,
            created_revision: false,
            copied_artifacts: 0,
        })
        .into_response();
    }

    let mut metadata = source_revision.metadata.clone();
    metadata.insert(
        "edited_from_revision".to_string(),
        source_revision.kref.clone(),
    );
    metadata.insert("edited_artifact".to_string(), source_artifact.name.clone());
    metadata.insert(
        "edited_by".to_string(),
        "construct-asset-browser".to_string(),
    );

    let new_revision = match client
        .create_revision(&source_revision.item_kref, metadata)
        .await
    {
        Ok(revision) => revision,
        Err(e) => return kumiho_err(e),
    };

    let new_path = unique_revision_location(&source_path, new_revision.number).await;
    if let Some(parent) = new_path.parent() {
        if let Err(e) = tokio::fs::create_dir_all(parent).await {
            return json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("failed to create artifact directory: {e}"),
            );
        }
    }
    if let Err(e) = tokio::fs::write(&new_path, body.content.as_bytes()).await {
        return json_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("failed to write new artifact content: {e}"),
        );
    }

    let source_artifacts = match client.get_artifacts(&source_revision.kref).await {
        Ok(artifacts) => artifacts,
        Err(e) => return kumiho_err(e),
    };

    let mut edited_artifact: Option<ArtifactResponse> = None;
    let mut copied = 0usize;
    for artifact in source_artifacts {
        let (location, metadata) = if artifact.kref == source_artifact.kref {
            (location_from_path(&new_path, file_uri), {
                let mut meta = artifact.metadata.clone();
                meta.insert("edited_from_artifact".to_string(), artifact.kref.clone());
                meta
            })
        } else {
            (artifact.location.clone(), artifact.metadata.clone())
        };

        match client
            .create_artifact(&new_revision.kref, &artifact.name, &location, metadata)
            .await
        {
            Ok(new_artifact) => {
                copied += 1;
                if artifact.kref == source_artifact.kref {
                    edited_artifact = Some(new_artifact);
                }
            }
            Err(e) => return kumiho_err(e),
        }
    }

    let artifact = match edited_artifact {
        Some(artifact) => artifact,
        None => {
            let mut metadata = source_artifact.metadata.clone();
            metadata.insert(
                "edited_from_artifact".to_string(),
                source_artifact.kref.clone(),
            );
            match client
                .create_artifact(
                    &new_revision.kref,
                    &source_artifact.name,
                    &location_from_path(&new_path, file_uri),
                    metadata,
                )
                .await
            {
                Ok(artifact) => artifact,
                Err(e) => return kumiho_err(e),
            }
        }
    };

    Json(UpdateArtifactContentResponse {
        revision: new_revision,
        artifact,
        created_revision: true,
        copied_artifacts: copied,
    })
    .into_response()
}

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

    #[test]
    fn revision_location_inserts_revision_before_extension() {
        let path = Path::new("/tmp/report.md");
        assert_eq!(
            revision_location(path, 7),
            PathBuf::from("/tmp/report.r7.md")
        );
    }

    #[test]
    fn revision_location_handles_extensionless_files() {
        let path = Path::new("/tmp/notes");
        assert_eq!(revision_location(path, 3), PathBuf::from("/tmp/notes.r3"));
    }
}