fix: preserve Task 2 command loading contracts

This commit is contained in:
2026-08-11 06:31:12 +02:00
parent 9e2c2b37b3
commit f62b4dbc4c
5 changed files with 279 additions and 86 deletions
+57 -25
View File
@@ -11,7 +11,13 @@ from tht.cli._guards import (
require_vector_write_allowed,
)
from tht.cli.config_cmd import CONFIG_OPT
from tht.cli.schema_cmd import _load_config_or_exit, annotations_path, physical_path
from tht.cli.schema_cmd import (
_load_config_or_exit,
_load_schema_config,
annotations_path,
physical_path,
)
from tht.config import Config, ConfigError
from tht.ports.vector import VectorWriteRecord
from tht.vectorstore.store import SyncStats, content_hash
@@ -154,7 +160,7 @@ class IndexSchemaResult(TypedDict):
counts: IndexCounts
def _vector_cfg_or_error(cfg) -> None:
def _vector_cfg_or_error(cfg: Config) -> None:
missing = []
if cfg.embeddings is None:
missing.append("embeddings")
@@ -164,24 +170,42 @@ def _vector_cfg_or_error(cfg) -> None:
raise _MachineVectorError("vector_configuration_missing")
def _vector_write_or_error(cfg) -> None:
def _vector_write_or_error(cfg: Config) -> None:
if cfg.profile == "workstation" and not has_vector_write_rest(cfg):
raise _MachineVectorError("vector_write_not_allowed")
def _load_schema_artifacts(cfg: Config):
import yaml
from pydantic import ValidationError
from tht.mschema.models import Annotations, PhysicalSchema
phys_file = physical_path(cfg)
if not phys_file.exists():
raise _MachineVectorError("physical_schema_missing")
try:
return (
PhysicalSchema.from_yaml(phys_file),
Annotations.from_yaml(annotations_path(cfg)),
)
except (OSError, UnicodeError, TypeError, ValueError, yaml.YAMLError, ValidationError):
raise _MachineVectorError("schema_artifacts_invalid") from None
def index_schema_data(
config: Path, *, suppress_legacy_warning: bool = False
config: Config | Path, *, suppress_legacy_warning: bool = False, physical=None, annotations=None
) -> IndexSchemaResult:
"""Synchronize schema records and return a bounded machine result."""
from tht.cli.schema_cmd import _load_schema_config
from tht.config import ConfigError
from tht.mschema.models import Annotations, PhysicalSchema
from tht.vectorstore.records import schema_records
try:
cfg = _load_schema_config(config, suppress_legacy_warning=suppress_legacy_warning)
except ConfigError:
raise _MachineVectorError("invalid_configuration") from None
if isinstance(config, Path):
try:
cfg = _load_schema_config(config, suppress_legacy_warning=suppress_legacy_warning)
except ConfigError:
raise _MachineVectorError("invalid_configuration") from None
else:
cfg = config
_vector_write_or_error(cfg)
_vector_cfg_or_error(cfg)
phys_file = physical_path(cfg)
@@ -191,8 +215,10 @@ def index_schema_data(
from pydantic import ValidationError
try:
physical = PhysicalSchema.from_yaml(phys_file)
annotations = Annotations.from_yaml(annotations_path(cfg))
if physical is None:
physical = PhysicalSchema.from_yaml(phys_file)
if annotations is None:
annotations = Annotations.from_yaml(annotations_path(cfg))
except (OSError, UnicodeError, TypeError, ValueError, yaml.YAMLError, ValidationError):
raise _MachineVectorError("schema_artifacts_invalid") from None
records = schema_records(physical, annotations)
@@ -226,29 +252,35 @@ def index_schema_cmd(
if json_output:
try:
payload = index_schema_data(config, suppress_legacy_warning=True)
except _MachineVectorError as error:
cfg = _load_schema_config(config, suppress_legacy_warning=True)
except ConfigError:
typer.echo(json.dumps({"status": "failed", "code": "invalid_configuration"}, sort_keys=True, separators=(",", ":")))
raise typer.Exit(code=1) from None
else:
cfg = _load_config_or_exit(config)
try:
physical, annotations = _load_schema_artifacts(cfg)
payload = index_schema_data(cfg, physical=physical, annotations=annotations)
except _MachineVectorError as error:
if json_output:
typer.echo(json.dumps({"status": "failed", "code": error.code}, sort_keys=True, separators=(",", ":")))
raise typer.Exit(code=1) from None
except Exception: # noqa: BLE001 - JSON CLI boundary
typer.echo(json.dumps({"status": "failed", "code": "schema_index_failed"}, sort_keys=True, separators=(",", ":")))
raise typer.Exit(code=1) from None
typer.echo(json.dumps(payload, sort_keys=True, separators=(",", ":")))
return
try:
payload = index_schema_data(config, suppress_legacy_warning=False)
except _MachineVectorError as error:
_render_index_schema_error(error, config)
_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
typer.secho("ERRORE: impossibile indicizzare lo schema.", fg=typer.colors.RED, err=True)
raise typer.Exit(code=1) from None
if json_output:
typer.echo(json.dumps(payload, sort_keys=True, separators=(",", ":")))
return
_print_stats(payload["counts"])
def _render_index_schema_error(error: _MachineVectorError, config: Path) -> None:
def _render_index_schema_error(error: _MachineVectorError, cfg) -> None:
"""Render expected failures without changing the old human CLI messages."""
cfg = _load_config_or_exit(config)
if error.code == "physical_schema_missing":
typer.secho(
f"ERRORE: {physical_path(cfg)} non trovato. Esegui prima `tht schema introspect`.",