Libra CodeHub

CedarCopilot/cedar-mail

Branch: staging

feat(playbook): add Jev action gate in front of the email orchestrator

merged#2827CedarCopilot

CedarCopilot wants to merge 2 commits into staging from worktree-jev-action-gate

Live on prod, watching, 1 day leftTimeline and evidence
  1. Opened
    Sep 19, 2026, 1:24 PM
  2. Sep 19, 2026, 1:45 PM
  3. Merged
    Sep 19, 2026, 9:49 PM
  4. Live on prod
    Sep 19, 2026, 9:49 PM
  5. Observed 1 hour
    Sep 19, 2026, 9:49 PM
  6. Watching

    Live on prod, watching, 1 day left

    Sep 19, 2026, 9:49 PM
  7. Pipelines steady after this deploy
    Sep 19, 2026, 9:49 PM

Behaviors Libra is checking

The Jev gate is bypassed for meeting, Slack, external-CRM, missing-event, and missing-primary-event executions so those flows retain their existing orchestration behavior.Not checked
prod
Eligible email executions now call TypeSafe AI Jev before starting the on-event email orchestrator to predict whether any action is needed.Not checked
prod
A missing Jev key, non-success response, malformed response, API error, or gate integration exception leaves `skip` false so the normal orchestrator continues instead of dropping the execution.Not checked
prod
The Jev request is aborted after an eight-second deadline so a stalled gate cannot hold pre-execution setup indefinitely.Not checked
prod
The gate evaluates the executing user's AOP and current-stage playbook context, including whether an email trigger exists, and evaluates participant executions with participant context rather than blanket-suppressing them or looking up the owner's AOP.Not checked
prod
When Jev returns a `noul` score below 0.3, pre-execution setup returns a no-op result and avoids running the email orchestrator.Not checked
prod

Libra has verdicts on 0 of 8 tracked behaviors on prod; 8 are still being checked. Libra checks hourly for 3 days after each deploy.

Summary

Adds a pre-check gate in front of on-event-email-orchestrator, calling TypeSafe AI's Jev model (a typed Choice/Score/Boolean judgment model, not a general LLM) to predict whether an email event needs any orchestrator action at all. When it confidently predicts no action, the orchestrator LLM call is skipped entirely , the same short-circuit mechanism the existing no-op-AOP check already uses.

  • State shape (role, upstream significance classifier verdict, event title/summary, playbook trigger-coverage) and threshold (0.3) are the result of iterative testing against a labeled sample of real on-event-email-orchestrator outcomes. Giving Jev the full conversation timeline + full rendered playbook section (what the orchestrator itself receives) was tested and measurably performed worse than this narrow, curated state , Jev's own docs recommend hand-picked state over a raw prompt dump, and that held up empirically here.
  • Fails open at two layers: evaluateJevActionGate catches every fetch/parse failure internally; the calling block in pre-execution-setup.ts additionally wraps its own DB work (resolvePlaybookContext, the event lookup) in an outer try/catch. Any failure anywhere falls through to normal orchestrator execution , it never fails the pre-execution-setup call outright.
  • Telemetry: every evaluation (skip, no-skip, or fail-open) is logged via createStructuredLog under jev-action-gate.evaluated with run/conversation/event ids, so real skip and false-negative rates can be measured against production traffic rather than trusting the pilot sample.
  • Scoped strictly to event.type === 'email' , the only population this has been validated against.
  • Also extracts the early-exit result construction (previously duplicated between the no-op-AOP check and this new gate) into one shared buildEarlyExitResult helper, and derives the playbook's org id from the conversation owner rather than the executing user (the two can differ on a participant/collaborator execution).
  • JEV_API_KEY wired through apps/server/src/env.ts, .env.example, and aws/lib/runtime-contract.ts's UNIFIED_SERVICE_RUNTIME_SECRET_FIELDS. Already added to the live /cedar/aws-staging/runtime and /cedar/aws-prod/runtime Secrets Manager entries (surgical merge, verified every other key unchanged) , but ECS tasks resolve secrets at container startup, so a deploy/restart is needed before either environment picks it up.

Cost savings math

Grounded in live Understudy trace data (7-day window) and a labeled n=50 sample of real on-event-email-orchestrator outcomes , not a projection.

23,856   weekly email executions (7d)
→ 14,598 (61.2%) never reach the orchestrator , existing no-op-AOP gate, already free
→  9,206 (38.6%) reach it and the orchestrator actually runs
     →  4,483 (48.7% of reached, by RUN COUNT) conclude "no action needed"
     →  but only 33.9% of reached-orchestrator COST, weighted by tool-call count ,
        "no action" runs average 4.97 tool calls vs. 9.22 for real-work runs, so they're
        cheaper per run and a flat 48.7% overstates their share of the $ spent

on-event-email-orchestrator costs $1,854.81/7d on Understudy. The gate's measured no-action recall, at the threshold (0.3) that showed zero missed real-work cases in the n=50 sample, is 92% (23 of 25 true no-action cases caught).

estimated savings = 92% recall × 33.9% cost-weighted no-action share × $1,854.81/7d
                   ≈ $579/7d
                   ≈ ~$2,480/month

Gate cost itself is negligible and already netted against that figure: ~$0.0008/call (2k-token state × Jev's $0.04/1M input, output free) × 9,206 calls/week ≈ $7.30/week ($31/month).

Caveats, stated plainly:

  • n=50 is a pilot sample, not a production guarantee , the real false-negative rate at scale is unmeasured. That's the whole reason for the telemetry in this PR: jev-action-gate.evaluated carries
Show production surfaces and changed-file mapping

Production surfaces

Libra has not measured any production surfaces for this change yet.

Changed files → surfaces

  • .env.exampleno production surface mapped
  • apps/server/src/env.tsno production surface mapped
  • apps/server/src/mastra/workflows/event-execution/__tests__/jev-action-gate-fallback.test.tsno production surface mapped
  • apps/server/src/mastra/workflows/event-execution/pre-execution-setup.tsno production surface mapped
  • apps/server/src/services/playbook/__tests__/jev-action-gate.test.tsno production surface mapped
  • apps/server/src/services/playbook/jev-action-gate.tsno production surface mapped
  • aws/lib/runtime-contract.tsno production surface mapped