Add async pipeline with progress monitoring, resumability, and result transparency
Pipeline engine rewritten with combo-first loop: each combination is processed through all requested passes before moving to the next, with incremental DB saves after every step (crash-safe). Blocked combos now get result rows so they appear in the results page with constraint violation reasons. New pipeline_runs table tracks run lifecycle (pending/running/completed/failed/ cancelled). Web route launches pipeline in a background thread with its own DB connection. HTMX polling partial shows live progress with per-pass breakdown. Also: status guard prevents reviewed->scored downgrade, save_combination loads existing status on dedup for correct resume, per-metric scores show domain bounds + units + position bars, ensure_metric backfills units on existing rows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -40,18 +40,8 @@ def entity_new():
|
||||
dimensions=repo.list_dimensions())
|
||||
|
||||
|
||||
@bp.route("/<int:entity_id>")
|
||||
@bp.route("/<int:entity_id>", methods=["GET", "POST"])
|
||||
def entity_detail(entity_id: int):
|
||||
repo = get_repo()
|
||||
entity = repo.get_entity(entity_id)
|
||||
if not entity:
|
||||
flash("Entity not found.", "error")
|
||||
return redirect(url_for("entities.entity_list"))
|
||||
return render_template("entities/detail.html", entity=entity)
|
||||
|
||||
|
||||
@bp.route("/<int:entity_id>/edit", methods=["GET", "POST"])
|
||||
def entity_edit(entity_id: int):
|
||||
repo = get_repo()
|
||||
entity = repo.get_entity(entity_id)
|
||||
if not entity:
|
||||
@@ -62,13 +52,17 @@ def entity_edit(entity_id: int):
|
||||
description = request.form.get("description", "").strip()
|
||||
if not name:
|
||||
flash("Name is required.", "error")
|
||||
return render_template("entities/form.html", entity=entity,
|
||||
dimensions=repo.list_dimensions())
|
||||
repo.update_entity(entity_id, name, description)
|
||||
flash(f"Entity '{name}' updated.", "success")
|
||||
return redirect(url_for("entities.entity_detail", entity_id=entity_id))
|
||||
return render_template("entities/form.html", entity=entity,
|
||||
dimensions=repo.list_dimensions())
|
||||
else:
|
||||
repo.update_entity(entity_id, name, description)
|
||||
flash(f"Entity '{name}' updated.", "success")
|
||||
entity = repo.get_entity(entity_id)
|
||||
return render_template("entities/detail.html", entity=entity)
|
||||
|
||||
|
||||
@bp.route("/<int:entity_id>/edit")
|
||||
def entity_edit(entity_id: int):
|
||||
"""Legacy route — redirect to detail page."""
|
||||
return redirect(url_for("entities.entity_detail", entity_id=entity_id))
|
||||
|
||||
|
||||
@bp.route("/<int:entity_id>/delete", methods=["POST"])
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
"""Pipeline run routes."""
|
||||
"""Pipeline run routes with background execution and progress monitoring."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Blueprint, flash, redirect, render_template, request, url_for
|
||||
|
||||
from physcom_web.app import get_repo
|
||||
@@ -9,12 +14,77 @@ from physcom_web.app import get_repo
|
||||
bp = Blueprint("pipeline", __name__, url_prefix="/pipeline")
|
||||
|
||||
|
||||
def _run_pipeline_in_background(
|
||||
db_path: str,
|
||||
domain_name: str,
|
||||
dim_list: list[str],
|
||||
passes: list[int],
|
||||
threshold: float,
|
||||
run_id: int,
|
||||
) -> None:
|
||||
"""Run the pipeline in a background thread with its own DB connection."""
|
||||
from physcom.db.schema import init_db
|
||||
from physcom.db.repository import Repository
|
||||
from physcom.engine.constraint_resolver import ConstraintResolver
|
||||
from physcom.engine.scorer import Scorer
|
||||
from physcom.engine.pipeline import Pipeline
|
||||
|
||||
try:
|
||||
conn = init_db(db_path)
|
||||
repo = Repository(conn)
|
||||
|
||||
domain = repo.get_domain(domain_name)
|
||||
if not domain:
|
||||
repo.update_pipeline_run(
|
||||
run_id, status="failed",
|
||||
error_message=f"Domain '{domain_name}' not found",
|
||||
)
|
||||
conn.close()
|
||||
return
|
||||
|
||||
resolver = ConstraintResolver()
|
||||
scorer = Scorer(domain)
|
||||
pipeline = Pipeline(repo, resolver, scorer, llm=None)
|
||||
|
||||
pipeline.run(
|
||||
domain, dim_list,
|
||||
score_threshold=threshold,
|
||||
passes=passes,
|
||||
run_id=run_id,
|
||||
)
|
||||
except Exception as exc:
|
||||
try:
|
||||
repo.update_pipeline_run(
|
||||
run_id, status="failed",
|
||||
error_message=str(exc)[:500],
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def pipeline_form():
|
||||
repo = get_repo()
|
||||
domains = repo.list_domains()
|
||||
dimensions = repo.list_dimensions()
|
||||
return render_template("pipeline/run.html", domains=domains, dimensions=dimensions)
|
||||
# Build per-domain summaries
|
||||
summaries = {}
|
||||
for d in domains:
|
||||
summaries[d.name] = repo.get_pipeline_summary(d.name)
|
||||
# Get recent pipeline runs
|
||||
runs = repo.list_pipeline_runs()
|
||||
return render_template(
|
||||
"pipeline/run.html",
|
||||
domains=domains,
|
||||
dimensions=dimensions,
|
||||
summaries=summaries,
|
||||
runs=runs,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/run", methods=["POST"])
|
||||
@@ -37,20 +107,50 @@ def pipeline_run():
|
||||
flash("Select at least one dimension.", "error")
|
||||
return redirect(url_for("pipeline.pipeline_form"))
|
||||
|
||||
from physcom.engine.constraint_resolver import ConstraintResolver
|
||||
from physcom.engine.scorer import Scorer
|
||||
from physcom.engine.pipeline import Pipeline
|
||||
# Create pipeline_run record
|
||||
config = {
|
||||
"passes": passes,
|
||||
"threshold": threshold,
|
||||
"dimensions": dim_list,
|
||||
}
|
||||
run_id = repo.create_pipeline_run(domain.id, config)
|
||||
|
||||
resolver = ConstraintResolver()
|
||||
scorer = Scorer(domain)
|
||||
pipeline = Pipeline(repo, resolver, scorer, llm=None)
|
||||
# Resolve DB path for the background thread
|
||||
from physcom_web.app import DEFAULT_DB
|
||||
db_path = str(Path(os.environ.get("PHYSCOM_DB", str(DEFAULT_DB))))
|
||||
|
||||
result = pipeline.run(domain, dim_list, score_threshold=threshold, passes=passes)
|
||||
# Start background thread
|
||||
t = threading.Thread(
|
||||
target=_run_pipeline_in_background,
|
||||
args=(db_path, domain_name, dim_list, passes, threshold, run_id),
|
||||
daemon=True,
|
||||
)
|
||||
t.start()
|
||||
|
||||
flash(
|
||||
f"Pipeline complete: {result.total_generated} combos generated, "
|
||||
f"{result.pass1_valid} valid, {result.pass1_blocked} blocked, "
|
||||
f"{result.pass3_above_threshold} above threshold.",
|
||||
"success",
|
||||
f"Pipeline run #{run_id} started for {domain_name} "
|
||||
f"(passes {passes}, threshold {threshold}).",
|
||||
"info",
|
||||
)
|
||||
return redirect(url_for("results.results_domain", domain_name=domain_name))
|
||||
return redirect(url_for("pipeline.pipeline_form"))
|
||||
|
||||
|
||||
@bp.route("/runs/<int:run_id>/status")
|
||||
def run_status(run_id: int):
|
||||
"""HTMX partial: returns live progress for a single pipeline run."""
|
||||
repo = get_repo()
|
||||
run = repo.get_pipeline_run(run_id)
|
||||
if not run:
|
||||
return "<p>Run not found.</p>", 404
|
||||
return render_template("pipeline/_run_status.html", run=run)
|
||||
|
||||
|
||||
@bp.route("/runs/<int:run_id>/cancel", methods=["POST"])
|
||||
def run_cancel(run_id: int):
|
||||
"""Set a running pipeline to cancelled. The pipeline checks this flag."""
|
||||
repo = get_repo()
|
||||
run = repo.get_pipeline_run(run_id)
|
||||
if run and run["status"] == "running":
|
||||
repo.update_pipeline_run(run_id, status="cancelled")
|
||||
flash(f"Run #{run_id} cancellation requested.", "info")
|
||||
return redirect(url_for("pipeline.pipeline_form"))
|
||||
|
||||
@@ -26,7 +26,8 @@ def results_domain(domain_name: str):
|
||||
|
||||
status_filter = request.args.get("status")
|
||||
results = repo.get_all_results(domain_name, status=status_filter)
|
||||
statuses = repo.count_combinations_by_status()
|
||||
# Domain-scoped status counts (only combos that have results in this domain)
|
||||
statuses = repo.count_combinations_by_status(domain_name=domain_name)
|
||||
|
||||
return render_template(
|
||||
"results/list.html",
|
||||
@@ -35,6 +36,7 @@ def results_domain(domain_name: str):
|
||||
results=results,
|
||||
status_filter=status_filter,
|
||||
statuses=statuses,
|
||||
total_results=sum(statuses.values()),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user