diff options
Diffstat (limited to 'makima/src')
| -rw-r--r-- | makima/src/db/repository.rs | 74 | ||||
| -rw-r--r-- | makima/src/orchestration/directive.rs | 52 | ||||
| -rw-r--r-- | makima/src/server/handlers/directives.rs | 60 | ||||
| -rw-r--r-- | makima/src/server/mod.rs | 1 | ||||
| -rw-r--r-- | makima/src/server/openapi.rs | 1 |
5 files changed, 181 insertions, 7 deletions
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 1021c35..27bd47e 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -5625,12 +5625,15 @@ pub async fn check_directive_idle( } /// Update a directive's goal and bump goal_updated_at. -/// Reactivates draft/idle/paused directives and clears any stale orchestrator -/// task so that planning/replanning triggers on the next reconciler tick. +/// Reactivates draft/idle/paused/inactive directives and clears any stale +/// orchestrator task so that planning/replanning triggers on the next +/// reconciler tick. /// -/// `draft` is included in the flip set because the document-mode UI treats -/// the first goal save as the implicit "start" — without this, a brand-new -/// directive's goal save would persist but never spawn a planner. +/// `draft` flips because the document-mode UI treats the first goal save as +/// the implicit "start". `inactive` flips because editing a contract whose +/// last revision was already shipped is the way the user kicks off an +/// amendment — the planner picks it up via phase_planning/replanning and +/// uses get_latest_merged_revision to learn the BEFORE→AFTER diff. pub async fn update_directive_goal( pool: &PgPool, owner_id: Uuid, @@ -5642,7 +5645,10 @@ pub async fn update_directive_goal( UPDATE directives SET goal = $3, goal_updated_at = NOW(), - status = CASE WHEN status IN ('draft', 'idle', 'paused') THEN 'active' ELSE status END, + status = CASE + WHEN status IN ('draft', 'idle', 'paused', 'inactive') THEN 'active' + ELSE status + END, orchestrator_task_id = NULL, updated_at = NOW(), version = version + 1 @@ -5657,6 +5663,62 @@ pub async fn update_directive_goal( .await } +/// Mark a directive 'inactive'. Used at the moment a PR is raised — at that +/// point the contract's current iteration is "shipped" and editing the goal +/// (Stage 4) starts an amendment cycle. Idempotent: no-op if status is +/// already inactive or already past it (e.g. archived). +pub async fn set_directive_inactive( + pool: &PgPool, + directive_id: Uuid, +) -> Result<(), sqlx::Error> { + sqlx::query( + r#" + UPDATE directives + SET status = 'inactive', + updated_at = NOW(), + version = version + 1 + WHERE id = $1 + AND status IN ('active', 'idle', 'paused') + "#, + ) + .bind(directive_id) + .execute(pool) + .await?; + Ok(()) +} + +/// Reset a directive for a "new draft" cycle: clear the goal back to empty, +/// flip status to 'draft', and detach the current pr_url / pr_branch / +/// orchestrator linkage so the next goal save starts fresh. Prior revisions +/// remain in `directive_revisions` as the historical record. Used by the +/// sidebar's "New draft" right-click on inactive contracts. +pub async fn reset_directive_for_new_draft( + pool: &PgPool, + owner_id: Uuid, + directive_id: Uuid, +) -> Result<Option<Directive>, sqlx::Error> { + sqlx::query_as::<_, Directive>( + r#" + UPDATE directives + SET goal = '', + goal_updated_at = NOW(), + status = 'draft', + pr_url = NULL, + pr_branch = NULL, + orchestrator_task_id = NULL, + completion_task_id = NULL, + updated_at = NOW(), + version = version + 1 + WHERE id = $1 AND owner_id = $2 + RETURNING * + "#, + ) + .bind(directive_id) + .bind(owner_id) + .fetch_optional(pool) + .await +} + /// Update a directive's goal WITHOUT clearing the orchestrator task id. /// /// This is the path used by the goal-edit interrupt cycle: when a small goal diff --git a/makima/src/orchestration/directive.rs b/makima/src/orchestration/directive.rs index 7dbfe65..1e004bf 100644 --- a/makima/src/orchestration/directive.rs +++ b/makima/src/orchestration/directive.rs @@ -56,7 +56,21 @@ impl DirectiveOrchestrator { "Directive needs planning — spawning planning task" ); - let plan = build_planning_prompt(&directive, &[], 1, &[], None); + // If the contract has previously-merged revisions, this is an + // amendment — pass the latest merged revision so the planner can + // reason about the delta instead of replanning from scratch. + let prev_merged = repository::get_latest_merged_revision(&self.pool, directive.id) + .await + .unwrap_or(None); + + let plan = build_planning_prompt( + &directive, + &[], + 1, + &[], + None, + prev_merged.as_ref(), + ); if let Err(e) = self .spawn_orchestrator_task( @@ -484,12 +498,20 @@ impl DirectiveOrchestrator { let progress_summary = summarize_in_progress_steps(&existing_steps); + // If the contract has previously-merged revisions, this is an + // amendment — pass the latest merged revision so the planner + // sees the BEFORE→AFTER diff for the new PR. + let prev_merged = repository::get_latest_merged_revision(&self.pool, directive.id) + .await + .unwrap_or(None); + let plan = build_planning_prompt( &directive, &existing_steps, generation, &goal_history, progress_summary.as_deref(), + prev_merged.as_ref(), ); if let Err(e) = self @@ -1475,9 +1497,37 @@ fn build_planning_prompt( generation: i32, goal_history: &[crate::db::models::DirectiveGoalHistory], progress_summary: Option<&str>, + previous_merged_revision: Option<&crate::db::models::DirectiveRevision>, ) -> String { let mut prompt = String::new(); + // Amendments to a previously-shipped contract. When the user edits a + // contract whose prior revision was already merged, the planner needs to + // reason about the BEFORE→AFTER diff so the new PR reflects only the + // intended delta, not a from-scratch reinterpretation. + if let Some(prev) = previous_merged_revision { + prompt.push_str("── AMENDMENT TO A PREVIOUSLY-MERGED CONTRACT ──\n"); + prompt.push_str(&format!( + "This contract was previously shipped via PR {} (revision v{}, frozen {}). \ + The user has now edited the contract to amend or extend that work. \ + Plan the new PR as a DELTA on top of the merged prior PR, not a fresh build.\n\n", + prev.pr_url, + prev.version, + prev.frozen_at.format("%Y-%m-%d %H:%M:%S UTC"), + )); + prompt.push_str("PREVIOUSLY-MERGED CONTRACT (frozen content):\n"); + prompt.push_str(&prev.content); + prompt.push_str("\n\nAMENDED CONTRACT (what the user wants now):\n"); + prompt.push_str(&directive.goal); + prompt.push_str( + "\n\nIMPORTANT:\n\ + - Identify what CHANGED between the previously-merged contract and the amended one.\n\ + - Keep work that already shipped — only plan the delta.\n\ + - The amended PR should land on top of master containing JUST the additions/edits \ + implied by the diff, not a re-implementation of the original contract.\n\n", + ); + } + if let Some(progress) = progress_summary { let trimmed = progress.trim(); if !trimmed.is_empty() { diff --git a/makima/src/server/handlers/directives.rs b/makima/src/server/handlers/directives.rs index 91f5892..7a7aff4 100644 --- a/makima/src/server/handlers/directives.rs +++ b/makima/src/server/handlers/directives.rs @@ -227,6 +227,17 @@ pub async fn update_directive( "Snapshotted directive revision on PR creation" ); } + + // Transition the contract to 'inactive' now that its + // iteration is "shipped" — editing the goal again starts + // an amendment cycle, surfaced via the New draft action. + if let Err(e) = repository::set_directive_inactive(pool, directive.id).await { + tracing::warn!( + directive_id = %directive.id, + error = %e, + "Failed to mark directive inactive after PR creation" + ); + } } } Json(directive).into_response() @@ -2094,6 +2105,55 @@ pub struct DirectiveRevisionListResponse { pub total: i64, } +/// Reset a directive for a new draft cycle — clears its goal and detaches +/// the current PR linkage. Past revisions remain attached as history. +/// +/// Intended for the sidebar's "New draft" right-click on an inactive +/// directive: the contract has shipped, the user wants to start the next +/// iteration on a clean slate without losing the prior PR's record. +#[utoipa::path( + post, + path = "/api/v1/directives/{id}/new-draft", + params(("id" = Uuid, Path, description = "Directive ID")), + responses( + (status = 200, description = "Directive reset to draft", body = Directive), + (status = 404, description = "Not found", body = ApiError), + (status = 503, description = "Database not configured", body = ApiError), + ), + security(("bearer_auth" = []), ("api_key" = [])), + tag = "Directives" +)] +pub async fn new_directive_draft( + State(state): State<SharedState>, + Authenticated(auth): Authenticated, + Path(id): Path<Uuid>, +) -> impl IntoResponse { + let Some(ref pool) = state.db_pool else { + return ( + StatusCode::SERVICE_UNAVAILABLE, + Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")), + ) + .into_response(); + }; + + match repository::reset_directive_for_new_draft(pool, auth.owner_id, id).await { + Ok(Some(directive)) => Json(directive).into_response(), + Ok(None) => ( + StatusCode::NOT_FOUND, + Json(ApiError::new("NOT_FOUND", "Directive not found")), + ) + .into_response(), + Err(e) => { + tracing::error!("Failed to reset directive for new draft: {}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(ApiError::new("RESET_FAILED", &e.to_string())), + ) + .into_response() + } + } +} + /// List all per-PR revisions for a directive, newest first. #[utoipa::path( get, diff --git a/makima/src/server/mod.rs b/makima/src/server/mod.rs index 31052bf..c577904 100644 --- a/makima/src/server/mod.rs +++ b/makima/src/server/mod.rs @@ -254,6 +254,7 @@ pub fn make_router(state: SharedState) -> Router { .route("/directives/{id}/steps/{step_id}/skip", post(directives::skip_step)) .route("/directives/{id}/goal", put(directives::update_goal)) .route("/directives/{id}/revisions", get(directives::list_directive_revisions)) + .route("/directives/{id}/new-draft", post(directives::new_directive_draft)) .route("/directives/{id}/cleanup", post(directives::cleanup_directive)) .route("/directives/{id}/create-pr", post(directives::create_pr)) .route("/directives/{id}/pick-up-orders", post(directives::pick_up_orders)) diff --git a/makima/src/server/openapi.rs b/makima/src/server/openapi.rs index e3ff757..e6d4547 100644 --- a/makima/src/server/openapi.rs +++ b/makima/src/server/openapi.rs @@ -131,6 +131,7 @@ use crate::server::messages::{ApiError, AudioEncoding, StartMessage, StopMessage directives::skip_step, directives::update_goal, directives::list_directive_revisions, + directives::new_directive_draft, directives::cleanup_directive, directives::create_pr, // Order endpoints |
