Add job run logs, OpenRouter model picker, and discipline grouping.
- Job logs: each job's stdout/stderr is teed into outputs/<id>/job.log
(survives restarts) and served at GET /jobs/{id}/log as text/plain, so
full run logs can be shared for debugging and refinement.
- Model picker: GET /models proxies OpenRouter's public model list with
per-1M-token pricing (1h cache, 502 on failure); the UI shows a model
dropdown with costs when OpenRouter compute is selected, and the pick
overrides vision+text models for that job (Classic and Agent modes).
- Conflicts in the report view are grouped by discipline pair
(collapsible sections, severity-ordered within groups) instead of one
flat severity-only list.
This commit is contained in:
+97
-38
@@ -12,7 +12,9 @@ not. No external queue/DB.
|
||||
"""
|
||||
|
||||
import json
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
import shutil
|
||||
@@ -20,6 +22,7 @@ import threading
|
||||
from typing import Dict, Optional
|
||||
|
||||
from backend import config
|
||||
from backend import llm
|
||||
from backend.agents.runner import run_agent_pipeline
|
||||
from backend.pipeline.runner import run_pipeline
|
||||
from backend.email_sender import send_conflict_report, send_review_required
|
||||
@@ -29,6 +32,40 @@ _lock = threading.Lock()
|
||||
PIPELINE_MODES = {"classic", "agent"}
|
||||
|
||||
|
||||
class _Tee:
|
||||
"""Write to both the real stream and the job log file."""
|
||||
|
||||
def __init__(self, stream, log_file) -> None:
|
||||
self._stream = stream
|
||||
self._log = log_file
|
||||
|
||||
def write(self, data):
|
||||
self._stream.write(data)
|
||||
self._log.write(data)
|
||||
|
||||
def flush(self):
|
||||
self._stream.flush()
|
||||
self._log.flush()
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def _tee_log(log_path: str, header: str):
|
||||
"""Mirror stdout/stderr into a per-job log file for the duration of a run.
|
||||
|
||||
sys.stdout is process-global, so two concurrent jobs would interleave in
|
||||
each other's logs - acceptable for this single-user tool (same tradeoff as
|
||||
the LLM cost globals in llm.py).
|
||||
"""
|
||||
with open(log_path, "a", encoding="utf-8") as log_file:
|
||||
log_file.write(header + "\n")
|
||||
real_out, real_err = sys.stdout, sys.stderr
|
||||
sys.stdout, sys.stderr = _Tee(real_out, log_file), _Tee(real_err, log_file)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
sys.stdout, sys.stderr = real_out, real_err
|
||||
|
||||
|
||||
def _set(job_id: str, **fields) -> None:
|
||||
with _lock:
|
||||
_jobs[job_id].update(fields)
|
||||
@@ -36,13 +73,14 @@ def _set(job_id: str, **fields) -> None:
|
||||
|
||||
def create_job(pdf_path: str, source_filename: str, email: Optional[str] = None,
|
||||
project_input: Optional[Dict] = None, text_local: bool = False,
|
||||
pipeline_mode: str = "classic") -> str:
|
||||
pipeline_mode: str = "classic", model: Optional[str] = None) -> str:
|
||||
"""Register a job and kick off its background thread. Returns the job_id."""
|
||||
pipeline_mode = pipeline_mode.strip().lower()
|
||||
if pipeline_mode not in PIPELINE_MODES:
|
||||
raise ValueError(f"Unsupported pipeline mode: {pipeline_mode!r}")
|
||||
# Agent mode v1 is OpenRouter-only.
|
||||
text_local = bool(text_local and pipeline_mode == "classic")
|
||||
model = (model or "").strip() or None
|
||||
job_id = uuid.uuid4().hex[:12]
|
||||
with _lock:
|
||||
_jobs[job_id] = {
|
||||
@@ -53,6 +91,7 @@ def create_job(pdf_path: str, source_filename: str, email: Optional[str] = None,
|
||||
"project_input": project_input or {},
|
||||
"text_local": text_local,
|
||||
"pipeline_mode": pipeline_mode,
|
||||
"model": model,
|
||||
"stage": None,
|
||||
"created_at": time.time(),
|
||||
"finished_at": None,
|
||||
@@ -60,54 +99,26 @@ def create_job(pdf_path: str, source_filename: str, email: Optional[str] = None,
|
||||
"error": None,
|
||||
}
|
||||
threading.Thread(target=_run, args=(
|
||||
job_id, pdf_path, project_input, text_local, pipeline_mode,
|
||||
job_id, pdf_path, project_input, text_local, pipeline_mode, model,
|
||||
),
|
||||
daemon=True).start()
|
||||
return job_id
|
||||
|
||||
|
||||
def _run(job_id: str, pdf_path: str, project_input: Optional[Dict] = None,
|
||||
text_local: bool = False, pipeline_mode: str = "classic") -> None:
|
||||
text_local: bool = False, pipeline_mode: str = "classic",
|
||||
model: Optional[str] = None) -> None:
|
||||
out_dir = os.path.join(config.OUTPUT_DIR, job_id)
|
||||
try:
|
||||
_set(job_id, status="running")
|
||||
# Keep a copy of the source PDF so its sheets can be viewed later.
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
# Persist minimal job metadata so the disk fallback in get_job can
|
||||
# recover the recipient email / pipeline mode after a server restart
|
||||
# (plain json.dump, matching the _dump style used elsewhere).
|
||||
with open(os.path.join(out_dir, "job.json"), "w", encoding="utf-8") as f:
|
||||
json.dump({
|
||||
"job_id": job_id,
|
||||
"email": _jobs[job_id].get("email"),
|
||||
"pipeline_mode": pipeline_mode,
|
||||
"source": _jobs[job_id].get("source"),
|
||||
}, f, indent=2)
|
||||
shutil.copy2(pdf_path, os.path.join(out_dir, "source.pdf"))
|
||||
runner = run_agent_pipeline if pipeline_mode == "agent" else run_pipeline
|
||||
runner_kwargs = {
|
||||
"out_dir": out_dir,
|
||||
"on_stage": lambda name: _set(job_id, stage=name),
|
||||
"project_input": project_input,
|
||||
"source_name": _jobs[job_id].get("source"),
|
||||
}
|
||||
if pipeline_mode == "classic":
|
||||
runner_kwargs["text_local"] = text_local
|
||||
else:
|
||||
runner_kwargs["require_review"] = config.AGENT_REQUIRE_REVIEW
|
||||
report = runner(pdf_path, **runner_kwargs)
|
||||
report.setdefault("summary", {})["pipeline_mode"] = pipeline_mode
|
||||
if report["summary"].get("agent_status") == "needs_review":
|
||||
# Human-review gate: hold the job, don't email the unreviewed report.
|
||||
_set(job_id, status="needs_review", report=report,
|
||||
finished_at=time.time(), stage=None)
|
||||
email = _jobs[job_id].get("email")
|
||||
if email:
|
||||
review_url = f"{config.APP_BASE_URL.rstrip('/')}/?job={job_id}"
|
||||
send_review_required(email, report, review_url)
|
||||
else:
|
||||
_set(job_id, status="done", report=report, finished_at=time.time(), stage=None)
|
||||
_notify(job_id, report, out_dir)
|
||||
header = (f"=== Job {job_id} | {pipeline_mode} | {_jobs[job_id].get('source')} | "
|
||||
f"model={model or 'default'} | "
|
||||
f"started {time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime())} UTC ===")
|
||||
with _tee_log(os.path.join(out_dir, "job.log"), header):
|
||||
_run_pipeline(job_id, pdf_path, out_dir, project_input, text_local,
|
||||
pipeline_mode, model)
|
||||
except Exception as e:
|
||||
print(f"[Jobs] Job {job_id} failed: {e}")
|
||||
_set(job_id, status="error", error=str(e), finished_at=time.time())
|
||||
@@ -119,6 +130,54 @@ def _run(job_id: str, pdf_path: str, project_input: Optional[Dict] = None,
|
||||
pass
|
||||
|
||||
|
||||
def _run_pipeline(job_id: str, pdf_path: str, out_dir: str,
|
||||
project_input: Optional[Dict], text_local: bool,
|
||||
pipeline_mode: str, model: Optional[str]) -> None:
|
||||
"""The body of a job run; executes inside the job's tee'd log capture."""
|
||||
# Persist minimal job metadata so the disk fallback in get_job can
|
||||
# recover the recipient email / pipeline mode after a server restart
|
||||
# (plain json.dump, matching the _dump style used elsewhere).
|
||||
with open(os.path.join(out_dir, "job.json"), "w", encoding="utf-8") as f:
|
||||
json.dump({
|
||||
"job_id": job_id,
|
||||
"email": _jobs[job_id].get("email"),
|
||||
"pipeline_mode": pipeline_mode,
|
||||
"source": _jobs[job_id].get("source"),
|
||||
}, f, indent=2)
|
||||
shutil.copy2(pdf_path, os.path.join(out_dir, "source.pdf"))
|
||||
runner = run_agent_pipeline if pipeline_mode == "agent" else run_pipeline
|
||||
runner_kwargs = {
|
||||
"out_dir": out_dir,
|
||||
"on_stage": lambda name: _set(job_id, stage=name),
|
||||
"project_input": project_input,
|
||||
"source_name": _jobs[job_id].get("source"),
|
||||
}
|
||||
if pipeline_mode == "classic":
|
||||
runner_kwargs["text_local"] = text_local
|
||||
else:
|
||||
runner_kwargs["require_review"] = config.AGENT_REQUIRE_REVIEW
|
||||
if model:
|
||||
print(f"[Jobs] Model override for this run: {model}")
|
||||
llm.set_model_override(model)
|
||||
try:
|
||||
report = runner(pdf_path, **runner_kwargs)
|
||||
finally:
|
||||
if model:
|
||||
llm.set_model_override(None)
|
||||
report.setdefault("summary", {})["pipeline_mode"] = pipeline_mode
|
||||
if report["summary"].get("agent_status") == "needs_review":
|
||||
# Human-review gate: hold the job, don't email the unreviewed report.
|
||||
_set(job_id, status="needs_review", report=report,
|
||||
finished_at=time.time(), stage=None)
|
||||
email = _jobs[job_id].get("email")
|
||||
if email:
|
||||
review_url = f"{config.APP_BASE_URL.rstrip('/')}/?job={job_id}"
|
||||
send_review_required(email, report, review_url)
|
||||
else:
|
||||
_set(job_id, status="done", report=report, finished_at=time.time(), stage=None)
|
||||
_notify(job_id, report, out_dir)
|
||||
|
||||
|
||||
def _notify(job_id: str, report: Dict, out_dir: str) -> None:
|
||||
email = _jobs[job_id].get("email")
|
||||
if not email:
|
||||
|
||||
Reference in New Issue
Block a user