Factory recreate mode; chunked uploads with progress everywhere
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
771c6f5245
commit
b309778be8
17 files changed
+477
-47
No files matched your search
+49
-3
@@ -150,6 +150,8 @@ pub fn router(app: AppState) -> Router {
|
||||
.route("/backups", get(list_backups).post(run_backup))
|
||||
.route("/backups/{name}", get(download_backup))
|
||||
.route("/backups/{name}/restore", post(restore_stored_backup))
|
||||
.route("/backups/upload", put(upload_backup_chunk))
|
||||
.route("/backups/restore-uploaded", post(restore_chunked_backup))
|
||||
.route("/backups/restore", post(restore_uploaded_backup).layer(axum::extract::DefaultBodyLimit::max(4 * 1024 * 1024 * 1024)))
|
||||
.route("/health", get(health))
|
||||
.route("/tools", get(crate::reverse::status))
|
||||
@@ -1964,6 +1966,43 @@ async fn restore_uploaded_backup(Extension(user): Extension<User>, State(app): S
|
||||
restore_and_restart(&app, &user, body.to_vec(), &format!("upload ({} bytes)", body.len())).await
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ChunkQuery {
|
||||
offset: u64,
|
||||
}
|
||||
|
||||
fn restore_upload_path(app: &App) -> std::path::PathBuf {
|
||||
app.config().data_dir().join("restore-upload.tmp")
|
||||
}
|
||||
|
||||
/// One chunk of a backup to restore (offset 0 starts anew); `restore-uploaded` applies the whole file.
|
||||
async fn upload_backup_chunk(Extension(user): Extension<User>, State(app): State<AppState>, Query(q): Query<ChunkQuery>, body: axum::body::Bytes) -> ApiResult {
|
||||
require_admin(&user)?;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let p = restore_upload_path(&app);
|
||||
if q.offset == 0 {
|
||||
tokio::fs::write(&p, &body).await?;
|
||||
} else {
|
||||
let current = tokio::fs::metadata(&p).await.map(|m| m.len()).unwrap_or(0);
|
||||
if current != q.offset {
|
||||
return Err(ApiError(StatusCode::CONFLICT, format!("chunk offset {} does not match the uploaded size {current}", q.offset)));
|
||||
}
|
||||
let mut f = tokio::fs::OpenOptions::new().append(true).open(&p).await?;
|
||||
f.write_all(&body).await?;
|
||||
f.flush().await?;
|
||||
}
|
||||
Ok(Json(json!({"ok": true, "size": q.offset + body.len() as u64})))
|
||||
}
|
||||
|
||||
async fn restore_chunked_backup(Extension(user): Extension<User>, State(app): State<AppState>) -> ApiResult {
|
||||
require_admin(&user)?;
|
||||
let p = restore_upload_path(&app);
|
||||
let bytes = tokio::fs::read(&p).await.map_err(|_| ApiError(StatusCode::BAD_REQUEST, "no uploaded backup".into()))?;
|
||||
let _ = tokio::fs::remove_file(&p).await;
|
||||
let source = format!("upload ({} bytes)", bytes.len());
|
||||
restore_and_restart(&app, &user, bytes, &source).await
|
||||
}
|
||||
|
||||
async fn download_backup(Extension(user): Extension<User>, State(app): State<AppState>, Path(name): Path<String>) -> Result<Response, ApiError> {
|
||||
require_admin(&user)?;
|
||||
if !crate::backup::list(&app).iter().any(|b| b.name == name) {
|
||||
@@ -2914,13 +2953,20 @@ async fn test_factory_connection(Extension(user): Extension<User>, State(app): S
|
||||
struct NewDraft {
|
||||
#[serde(default)]
|
||||
model: Option<String>,
|
||||
/// "recreate": analyse existing software first (clean-room).
|
||||
#[serde(default)]
|
||||
mode: Option<String>,
|
||||
#[serde(default)]
|
||||
source: String,
|
||||
#[serde(default)]
|
||||
notes: String,
|
||||
}
|
||||
|
||||
/// Start a chat in which the user writes a spec with a model.
|
||||
async fn factory_new_draft(Extension(user): Extension<User>, State(app): State<AppState>, Json(b): Json<NewDraft>) -> ApiResult {
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
let model = b.model.filter(|m| !m.is_empty()).unwrap_or(s.defaults.spec_model.clone());
|
||||
let session = crate::factory::start_spec_chat(&app, &user, &model).map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("{e:#}")))?;
|
||||
let session = crate::factory::start_spec_chat(&app, &user, &model, (b.mode.as_deref() == Some("recreate")).then_some((b.source.as_str(), b.notes.as_str()))).map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("{e:#}")))?;
|
||||
Ok(Json(json!(session)))
|
||||
}
|
||||
|
||||
@@ -2937,7 +2983,7 @@ async fn factory_draft(Extension(user): Extension<User>, State(app): State<AppSt
|
||||
let s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
let conns: Vec<Value> = s.connections.iter().map(|c| json!({"id": c.id, "name": c.name, "kind": c.kind, "owner": c.owner})).collect();
|
||||
let setup = std::fs::read_to_string(dir.join("setup.json")).ok().filter(|t| crate::factory::valid_setup(t));
|
||||
Ok(Json(json!({"spec": spec, "setup": setup, "proposal": crate::factory::proposal(&app, &id), "connections": conns})))
|
||||
Ok(Json(json!({"spec": spec, "setup": setup, "proposal": crate::factory::proposal(&app, &id), "connections": conns, "recreate": dir.join("recreate.txt").exists()})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -2957,7 +3003,7 @@ async fn factory_create_project(Extension(user): Extension<User>, State(app): St
|
||||
let spec = std::fs::read_to_string(dir.join("spec.md")).unwrap_or_default();
|
||||
let mut s: crate::factory::FactorySettings = app.store.user_setting(&user.id, "factory")?;
|
||||
let setup = std::fs::read_to_string(dir.join("setup.json")).unwrap_or_default();
|
||||
let res = crate::factory::create_project(&app, &user, &mut s, &spec, &setup, &b.connection, &b.name, &b.slug, &b.description, &b.owner).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
let res = crate::factory::create_project(&app, &user, &mut s, &spec, &setup, &b.connection, &b.name, &b.slug, &b.description, &b.owner, !dir.join("recreate.txt").exists()).await.map_err(|e| ApiError(StatusCode::BAD_GATEWAY, format!("{e:#}")))?;
|
||||
app.store.set_user_setting(&user.id, "factory", &s)?;
|
||||
app.audit(Some(&user), "factory.project.create", res["repo"].as_str().unwrap_or(""), &crate::auth::client_ip(&headers));
|
||||
Ok(Json(res))
|
||||
|
||||
+38
-7
@@ -1558,19 +1558,44 @@ fn is_planning_path(factory: &std::path::Path, cwd: &std::path::Path) -> bool {
|
||||
cwd.strip_prefix(factory).map(|rel| rel.components().any(|c| c.as_os_str() == "_drafts" || c.as_os_str() == "_issues")).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Instructions of a "recreate" draft: analyse existing software in depth, then write a clean-room spec.
|
||||
pub const RECREATE_INSTRUCTIONS: &str = r#"You are the analyst of a software factory working in RECREATE / clean-room mode. The user wants an existing program rebuilt as a new, independent implementation. You analyse the original down to the last detail and write the specification; later, other agents build the new software from that specification alone – they never see the original, your notes or any decompiled code. Answer in the user's language.
|
||||
|
||||
Your working directory is private and never published. It holds `original/` (the program: files the user uploaded and/or what you download), `analysis/` (tool output, notes, scripts), `docs/` (documentation of the original) and the two deliverables `spec.md` and `setup.json`.
|
||||
|
||||
Load the skill "recreate" and follow its phases – but go deeper than its minimum and work through them on your own, without waiting for the user between phases (ask_user only when something essential cannot be found out otherwise):
|
||||
1. Acquire and inventory: fetch the source named in `recreate.txt` (git clone / curl into `original/`, never run installers or the program unsandboxed), unpack archives recursively, identify every file, write `docs/01-inventory.md`.
|
||||
2. Analyse exhaustively: read the source code if it is available, otherwise use binary_info and decompile on EVERY executable and library. Cover entry points and main loop, every screen/dialog/command/option, all rules and algorithms with their exact constants, tables and string/message texts, data and file formats (byte layouts), configuration, persistence, error handling, timing, network protocols, third-party libraries. Keep `analysis/coverage.md`: a list of every file, function group and resource with its status (understood, partly, unknown) – and keep working until nothing important stays open or unknown.
|
||||
3. Document in your own words in `docs/` (templates of the skill).
|
||||
4. Write `spec.md`: the only input for the builders. Structure: Goal · Users · Behaviour per area (numbered features, each with acceptance criteria that can be checked in the running app) · Data and file formats (byte layouts are facts and may be exact) · Texts and constants that must match · Non-functional requirements · Tech stack with reasons · Out of scope · Open questions · **Milestones** (M0 project setup and test harness; then M1…Mn in build order, the smallest useful first version first; each milestone names its features; the last one is wrap-up/performance/docs).
|
||||
Clean-room rules for spec.md: behaviour and facts only – no source or decompiled code, no decompiler-generated names or addresses, no pasted code snippets; describe algorithms in prose or pseudocode of your own.
|
||||
5. Audit: re-read spec.md against `analysis/coverage.md` and the originals, close gaps, and check that nothing in it is copied code. Write the result to `analysis/audit.md`.
|
||||
6. Keep `setup.json` like the spec writers do (install/start/url of the NEW software). Ask the user for the technology only if the recreation plan leaves it open.
|
||||
7. Finish with a short summary and tell the user to review `spec.md` (and `docs/`), correct things together with you, and then say when it is done. Only then propose name and slug and end your message with exactly one fenced json block and nothing after it:
|
||||
```json
|
||||
{"decision": "ready", "name": "Project name", "slug": "project-name", "description": "one sentence"}
|
||||
```
|
||||
The repository is then created with spec.md only; the milestones are cut into issues afterwards."#;
|
||||
|
||||
/// Start a spec chat with the given model.
|
||||
pub fn start_spec_chat(app: &Arc<App>, user: &User, model: &str) -> Result<crate::types::Session> {
|
||||
pub fn start_spec_chat(app: &Arc<App>, user: &User, model: &str, recreate: Option<(&str, &str)>) -> Result<crate::types::Session> {
|
||||
let id = crate::types::new_id();
|
||||
let dir = draft_dir(app, user, &id)?;
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
if let Some((source, notes)) = recreate {
|
||||
for d in ["original", "analysis", "docs"] {
|
||||
std::fs::create_dir_all(dir.join(d))?;
|
||||
}
|
||||
std::fs::write(dir.join("recreate.txt"), format!("Source: {}\n\nNotes from the user:\n{}\n", Some(source.trim()).filter(|s| !s.is_empty()).unwrap_or("(none given – the files are in original/)"), notes.trim()))?;
|
||||
}
|
||||
let model = Some(model.to_string()).filter(|m| !m.is_empty()).or_else(|| Some(app.config().server.default_model.clone()).filter(|m| !m.is_empty())).or_else(|| app.registry().first_model()).context("no model configured")?;
|
||||
let now = crate::types::now_ms();
|
||||
let session = crate::types::Session {
|
||||
id,
|
||||
title: "Factory – new project".into(),
|
||||
title: if recreate.is_some() { "Factory – recreate" } else { "Factory – new project" }.into(),
|
||||
cwd: dir.display().to_string(),
|
||||
model,
|
||||
agent: "build".into(),
|
||||
agent: if recreate.is_some() { "craft" } else { "build" }.into(),
|
||||
permission_mode: "auto".into(),
|
||||
parent_id: None,
|
||||
owner_id: Some(user.id.clone()),
|
||||
@@ -1578,17 +1603,18 @@ pub fn start_spec_chat(app: &Arc<App>, user: &User, model: &str) -> Result<crate
|
||||
updated_at: now,
|
||||
todos: Value::Null,
|
||||
last_usage: None,
|
||||
clean_room: None,
|
||||
clean_room: recreate.map(|_| false),
|
||||
phase: None,
|
||||
worktree: None,
|
||||
pinned: false,
|
||||
archived: false,
|
||||
folder: Some(FACTORY_FOLDER.to_string()),
|
||||
instructions: SPEC_INSTRUCTIONS.to_string(),
|
||||
instructions: if recreate.is_some() { RECREATE_INSTRUCTIONS } else { SPEC_INSTRUCTIONS }.to_string(),
|
||||
telegram_quiet: true,
|
||||
summary: String::new(),
|
||||
color: None,
|
||||
icon: None, skills: Vec::new(),
|
||||
icon: None,
|
||||
skills: if recreate.is_some() { vec!["recreate".to_string()] } else { Vec::new() },
|
||||
};
|
||||
app.save_session(session)
|
||||
}
|
||||
@@ -1627,7 +1653,7 @@ pub fn valid_setup(text: &str) -> bool {
|
||||
|
||||
/// Create the repository, file README and spec, register the repository and open the issue
|
||||
/// that starts the workflow. Safe to repeat after a failure: an empty repository is reused.
|
||||
pub async fn create_project(app: &Arc<App>, user: &User, settings: &mut FactorySettings, spec: &str, setup: &str, connection: &str, name: &str, slug: &str, description: &str, owner: &str) -> Result<Value> {
|
||||
pub async fn create_project(app: &Arc<App>, user: &User, settings: &mut FactorySettings, spec: &str, setup: &str, connection: &str, name: &str, slug: &str, description: &str, owner: &str, start: bool) -> Result<Value> {
|
||||
let slug = slugify(slug);
|
||||
if slug.len() < 2 {
|
||||
bail!("the repository name is too short");
|
||||
@@ -1678,6 +1704,10 @@ pub async fn create_project(app: &Arc<App>, user: &User, settings: &mut FactoryS
|
||||
// The issue that starts the workflow.
|
||||
let g = Forge::new(&resolved)?;
|
||||
g.ensure_labels().await?;
|
||||
if !start {
|
||||
// Recreate projects: the milestones are cut into issues next (issue chat), nothing is built yet.
|
||||
return Ok(json!({"url": cfg.url, "repo": created["full_name"], "issue": Value::Null, "issue_url": Value::Null, "spec": spec_path, "setup": setup_filed}));
|
||||
}
|
||||
let issue = g.create_issue(name, &format!("{}\n\nSpec: {spec_path}", description.trim()), &[cfg.label.clone()]).await?;
|
||||
let n = issue_no(&issue);
|
||||
app.store.factory_issue_add(&cfg.url, n, &user.id, name, &login)?;
|
||||
@@ -1808,6 +1838,7 @@ mod tests {
|
||||
let v = parse_result(t).unwrap();
|
||||
assert_eq!((v["decision"].as_str(), v["slug"].as_str()), (Some("ready"), Some("todo")));
|
||||
assert!(SPEC_INSTRUCTIONS.contains("\"decision\": \"ready\""));
|
||||
assert!(RECREATE_INSTRUCTIONS.contains("\"decision\": \"ready\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user