Core

Events and the ambient sink

How events from different packages land in one trajectory with zero config, and how to take control when you need to.

Core holds one context-local sink scope: a ContextVar in Python, an AsyncLocalStorage in Node (shared through a Symbol.for global, so two copies of core in one process still agree). trace() opens a scope that records every event emitted inside it. HITL emits approval events and Context emits skill_load events into the same scope, so with Evals installed they appear in the trajectory with no wiring.

  • emit(event) sends an event to every sink in scope, innermost first. An event without an id gets one evt_ id that every sink sees, so nested traces agree. If the event has no parent_id and a span is open, the span's id is filled in.
  • span(event) gives an event an id and started_at, runs your code with that id as the parent of everything inside, then emits it with latency_ms and, if your code raised, error.
  • with_sink(sink) / withSink(sink, fn) adds your own sink without replacing the enclosing ones.
  • Every API that reads the scope also takes an explicit sink, which replaces the ambient chain.
  • Concurrent tasks each get their own scope: events from one asyncio task or promise chain never reach another.
main.py
import asyncio

from viewportai.core import AgentEvent, ApprovalEvent, emit, span, trace
from viewportai.core.testing import FakeClock, SequentialIds


async def agent(name: str) -> None:
    async with span(AgentEvent(name=name)) as s:
        emit(ApprovalEvent(decision="allow", tool_name="lookup", decided_by="policy"))
        s.set(output="done")


async def main() -> None:
    with trace(clock=FakeClock(), ids=SequentialIds()) as t:
        await asyncio.gather(agent("a"), agent("b"))
    events = t.trajectory.events
    parents = {e.parent_id for e in events if e.type == "approval"}
    print(
        str(parents == {e.id for e in events if e.type == "agent"}).lower()
    )  # true: each approval belongs to its own agent


asyncio.run(main())
main.ts
import { emit, span, trace } from "@viewportai/core";
import { FakeClock, SequentialIds } from "@viewportai/core/testing";

const agent = (name: string) =>
  span({ type: "agent", name }, async (s) => {
    emit({ type: "approval", decision: "allow", tool_name: "lookup", decided_by: "policy" });
    s.set({ output: "done" });
  });

const { events } = await trace(() => Promise.all([agent("a"), agent("b")]), { clock: new FakeClock(), ids: new SequentialIds() });
const agents = new Set(events.filter((e) => e.type === "agent").map((e) => e.id));
console.log(events.filter((e) => e.type === "approval").every((e) => agents.has(e.parent_id))); // true

A trajectory from a failed run

In Python, t.trajectory exists after the block even when it raised: the error is recorded as trajectory.error and re-raised unchanged. In TypeScript, trace() rethrows the original error; traceSettled() never throws and returns { ok: true, result, events, trajectory } or { ok: false, error, events, trajectory }, so the partial trajectory of a failed run is always there.

Sinks

A sink is any object with emit(event) (optionally start(event) for live views and flush()), or a plain function. emit is synchronous; sinks that do IO buffer and flush. A sink that throws does not stop the other sinks from seeing the event; the first error is then raised to the emitter. The one exception is the closing event of a span or tool call: a sink failure there is reported (Python logs it on viewportai.core, TypeScript calls reportError or console.error) instead of raised. A broken sink never makes a completed side effect look failed, and never replaces the error a failed call raised, so a HITL denial stays a denial.

On this page