feat: metrics record --emit-event for kaizen.metrics.recorded

Publish activity-core EventEnvelope payloads to NATS subject
activity.kaizen.metrics.recorded after a successful append.
Optional nats-py via kaizen-agentic[events]; project slug from
KAIZEN_PROJECT_SLUG or directory basename. Skips emit on
idempotency duplicates. Closes KAIZEN-WP-0008 T03.
This commit is contained in:
2026-06-18 08:53:36 +02:00
parent c5798f58e4
commit 1641a3165d
11 changed files with 405 additions and 7 deletions

View File

@@ -15,6 +15,11 @@ from .integrations.artifact_store import (
default_api_url,
publish_optimizer_evidence,
)
from .integrations.event_bus import (
build_metrics_recorded_envelope,
publish_metrics_recorded_event,
resolve_project_slug,
)
from .integrations.helix import HelixCorrelationAdapter, enrich_helix_correlation
from .metrics import MetricsStore, OptimizerStore, performance_summary_markdown
from .optimization import OptimizationLoop, MIN_SAMPLES_FOR_RECOMMENDATIONS
@@ -1069,6 +1074,11 @@ def metrics():
@click.option(
"--json", "json_input", is_flag=True, help="Read full record JSON from stdin"
)
@click.option(
"--emit-event",
is_flag=True,
help="Publish kaizen.metrics.recorded to NATS (requires nats-py)",
)
def metrics_record(
agent_name: str,
target: str,
@@ -1079,6 +1089,7 @@ def metrics_record(
session_id: Optional[str],
idempotency_key: Optional[str],
json_input: bool,
emit_event: bool,
):
"""Append one execution record for an agent."""
store = MetricsStore(_project_root(target), agent_name)
@@ -1109,6 +1120,21 @@ def metrics_record(
if store.append(payload, idempotency_key=idempotency_key):
click.echo(f"Recorded metrics for '{agent_name}'")
if emit_event:
summary = store.read_summary() or store.write_summary()
envelope = build_metrics_recorded_envelope(
agent=agent_name,
project=resolve_project_slug(store.project_root),
summary=summary,
)
try:
subject = publish_metrics_recorded_event(envelope)
except RuntimeError as exc:
click.echo(f"Error: {exc}", err=True)
sys.exit(1)
click.echo(
f"Emitted kaizen.metrics.recorded for '{agent_name}'{subject}"
)
else:
click.echo(
f"Skipped duplicate record for '{agent_name}' (idempotency key exists)"