fix: harden task2 preprocessing and vector guards
This commit is contained in:
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
@@ -12,8 +13,17 @@ import typer
|
||||
from tht.cli.config_cmd import CONFIG_OPT
|
||||
from tht.cli.schema_cmd import _load_config_or_exit
|
||||
from tht.config import workspace_id_for_config
|
||||
from tht.ports.evidence import EvidenceSourceError
|
||||
from tht.ports.vector import VectorStoreError
|
||||
from tht.vectorstore.embeddings import EmbeddingsError
|
||||
|
||||
preprocess_app = typer.Typer(help="Materialize versioned preprocessing artifacts")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_PREPROCESS_EXPECTED_ERRORS = (
|
||||
OSError, RuntimeError, ValueError, TypeError, KeyError,
|
||||
EvidenceSourceError, VectorStoreError, EmbeddingsError,
|
||||
)
|
||||
|
||||
|
||||
def run_dwh_from_config(
|
||||
@@ -132,7 +142,16 @@ def evidence_cmd(
|
||||
if action == "gc":
|
||||
try:
|
||||
payload = gc_from_config(config, dry_run=dry_run)
|
||||
except (OSError, RuntimeError, ValueError, TypeError, KeyError):
|
||||
except _PREPROCESS_EXPECTED_ERRORS:
|
||||
payload = {"status": "failed", "error": "evidence cleanup failed"}
|
||||
if json_output:
|
||||
typer.echo(json.dumps(payload, sort_keys=True))
|
||||
else:
|
||||
typer.secho("ERRORE: evidence cleanup failed", fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1) from None
|
||||
except Exception:
|
||||
if not json_output:
|
||||
logger.exception("Evidence cleanup failed")
|
||||
payload = {"status": "failed", "error": "evidence cleanup failed"}
|
||||
if json_output:
|
||||
typer.echo(json.dumps(payload, sort_keys=True))
|
||||
@@ -153,7 +172,16 @@ def evidence_cmd(
|
||||
raise typer.Exit(code=2)
|
||||
try:
|
||||
result = run_from_config(config, dry_run=dry_run, resume=resume)
|
||||
except (OSError, RuntimeError, ValueError, TypeError, KeyError):
|
||||
except _PREPROCESS_EXPECTED_ERRORS:
|
||||
payload = {"status": "failed", "error": "preprocessing failed"}
|
||||
if json_output:
|
||||
typer.echo(json.dumps(payload, sort_keys=True))
|
||||
else:
|
||||
typer.secho("ERRORE: preprocessing failed", fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1) from None
|
||||
except Exception:
|
||||
if not json_output:
|
||||
logger.exception("Evidence preprocessing failed")
|
||||
payload = {"status": "failed", "error": "preprocessing failed"}
|
||||
if json_output:
|
||||
typer.echo(json.dumps(payload, sort_keys=True))
|
||||
|
||||
@@ -448,10 +448,10 @@ def check_cmd(
|
||||
raise typer.Exit(code=1) from None
|
||||
_render_schema_machine_error(error, cfg)
|
||||
except Exception:
|
||||
logger.exception("Schema check failed")
|
||||
if json_output:
|
||||
_schema_json({"status": "failed", "code": "schema_check_failed"})
|
||||
raise typer.Exit(code=1) from None
|
||||
logger.exception("Schema check failed")
|
||||
typer.secho("ERRORE: impossibile verificare le annotazioni.", fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1) from None
|
||||
if json_output:
|
||||
@@ -531,10 +531,10 @@ def suggest_fks_cmd(
|
||||
raise typer.Exit(code=1) from None
|
||||
_render_schema_machine_error(error, cfg)
|
||||
except Exception:
|
||||
logger.exception("Schema suggestion failed")
|
||||
if json_output:
|
||||
_schema_json({"status": "failed", "code": "schema_suggestion_failed"})
|
||||
raise typer.Exit(code=1) from None
|
||||
logger.exception("Schema suggestion failed")
|
||||
typer.secho("ERRORE: impossibile elaborare il catalogo.", fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1) from None
|
||||
if json_output:
|
||||
|
||||
@@ -208,9 +208,10 @@ def index_schema_data(
|
||||
cfg = config
|
||||
_vector_write_or_error(cfg)
|
||||
_vector_cfg_or_error(cfg)
|
||||
phys_file = physical_path(cfg)
|
||||
if not phys_file.exists():
|
||||
raise _MachineVectorError("physical_schema_missing")
|
||||
if physical is None:
|
||||
phys_file = physical_path(cfg)
|
||||
if not phys_file.exists():
|
||||
raise _MachineVectorError("physical_schema_missing")
|
||||
import yaml
|
||||
from pydantic import ValidationError
|
||||
|
||||
@@ -259,6 +260,9 @@ def index_schema_cmd(
|
||||
else:
|
||||
cfg = _load_config_or_exit(config)
|
||||
try:
|
||||
# Authorization and configuration are checked before touching any artifacts.
|
||||
_vector_write_or_error(cfg)
|
||||
_vector_cfg_or_error(cfg)
|
||||
physical, annotations = _load_schema_artifacts(cfg)
|
||||
payload = index_schema_data(cfg, physical=physical, annotations=annotations)
|
||||
except _MachineVectorError as error:
|
||||
@@ -267,10 +271,10 @@ def index_schema_cmd(
|
||||
raise typer.Exit(code=1) from None
|
||||
_render_index_schema_error(error, cfg)
|
||||
except Exception:
|
||||
logger.exception("Schema indexing failed")
|
||||
if json_output:
|
||||
typer.echo(json.dumps({"status": "failed", "code": "schema_index_failed"}, sort_keys=True, separators=(",", ":")))
|
||||
raise typer.Exit(code=1) from None
|
||||
logger.exception("Schema indexing failed")
|
||||
typer.secho("ERRORE: impossibile indicizzare lo schema.", fg=typer.colors.RED, err=True)
|
||||
raise typer.Exit(code=1) from None
|
||||
if json_output:
|
||||
|
||||
Reference in New Issue
Block a user