LOGIKASign in
On this page

Instrumentation

Applications are instrumented with standard OpenTelemetry. An application already instrumented works as it is: plain OpenTelemetry is always valid instrumentation, nothing Logika-specific is required in application code, and until the ledger declares an event, every span, log record and metric is stored and queryable, and any span that ends in an error, or log record with an exception or an ERROR or FATAL severity, raises an issue. Declaring events, as below, is what lets Logika judge more.

from opentelemetry import trace
tracer = trace.get_tracer("backend")

with tracer.start_as_current_span(
    "checkout", attributes={"order_id": order_id, "user_id": user_id, "amount": str(amount)}
):
    with tracer.start_as_current_span("checkout.reserved", attributes={"sku": sku}):
        pass
    with tracer.start_as_current_span(
        "checkout.captured",
        attributes={"order_id": order_id, "user_id": user_id, "provider": "stripe", "amount": str(amount)},
    ):
        pass

From ledger to spans

  • A root event is a span whose name is the full event name; its params are attributes.
  • Every declared child is a child span; a leaf is a short child span around the moment it describes.
  • The enclosing operation is the parent_span_id, the journey is the trace id, and the process is service.instance.id.
  • Work queued for later (a job, a message) runs in a trace of its own: start its root span with an OpenTelemetry span link to the span that queued it, and the journey of either shows the other as a weak edge.
  • The application version is the service.version resource attribute. The environment comes from the SDK key, never from an attribute.
  • Declared spans set their params at start.

One judged signal. Traces are the signal Logika judges. Log records are accepted too and stored unmanaged: the enhancer exports your existing logging (Python's logging, the OpenTelemetry logs API in TypeScript) on the same connection, logika query journey shows each record under the span that was current when it was logged, and logika query sql reads them as the logs view; only a record with an exception or an ERROR or FATAL severity is judged, as an error issue of the span it ran in (see the ledger's Errors), and they count toward your quota. Metrics are stored the same way: the enhancer installs the SDK's meter provider, collects every instrument every 60 seconds and at shutdown, and sends each data point raw (its metric's name, kind, unit, attributes and value or histogram buckets), which logika query sql reads as the metrics view; nothing judges or aggregates them, analytics come from spans, and they count toward your quota. Profiles requests are rejected. A counter can also be the count of a declared event, and process liveness rides the heartbeat. Undeclared spans inside a trace batch are accepted and stored unmanaged: a journey shows them in place, only their failures are judged, and an undeclared name from your own instrumentation (not a library's) is reported through the queue.

The enhancer

Your agent installs the logika-otel package and writes its initialisation for each component (the logika-instrument skill, at the places each component's init names), and it is mandatory: ingest requires its handshake and headers. The package is logika-otel on PyPI (imported as logika_otel) and on npm (Node, browser and React Native). It rides standard OpenTelemetry extension points:

  1. Open markers. When a span starts, a short marker (_open) carries its start attributes, so an unfinished operation is visible within a flush interval.
  2. One pipeline. Children are child spans, not span events, so a short child exports while its parent is still open: the evidence a hang needs is never held back.
  3. Loss and clock headers. Each export carries the instance, the send time and the drop count with its causes, so loss is accounted and appears in evidence, and says whether its runtime can go offline (browser and React Native: yes; Node and Python: no). In a browser or React Native app, batches the enhancer could not send are kept on the device (IndexedDB in the browser; in React Native, the storage you pass to init, such as AsyncStorage) and sent by the next launch as the instance that produced them, so the server judges them by their own timestamps. The device keeps at most 3 MB for at most a week; what it has to drop is counted as lost. Stored batches are the bytes that would have been sent, so with a PII key nothing on the device is plaintext PII.
  4. PII encryption. With your key, pii params are encrypted in the client before serialisation, deterministically so equality still works. Everything else (undeclared attributes, library spans, log records, metrics) is sent as plain OpenTelemetry sends it, so declare what must stay private as pii. The CLI decrypts for you. Without a key nothing is encrypted.
  5. Heartbeat and lifecycle. A heartbeat (_heartbeat) every 30 seconds carries liveness: a server process silent for three heartbeats without a clean shutdown is reported as dead, many at once as one issue grouped by app version. A clean shutdown flushes and says so (_instance_end), so it never looks like a crash. A browser tab or mobile app that goes offline is never reported dead; its data is judged when it comes back.

At startup the enhancer asks https://api.logika.host/handshake for the project's ingest endpoint; clients never hardcode an ingest host.

Generated bindings

Optional typed helpers, generated from the ledger by templates your agent writes to match the project's conventions and lists under config.generate in logika.json. The helpers are nested: a root event is a function, and a span hands its body an object whose methods are its declared children, so the type checker rejects a child emitted outside its parent. logika check regenerates them whenever the ledger or a template changed since they were generated.

Good logging practices: state transitions

As your agent writes code, it logs as many state transitions as matter: wherever the code creates, updates, moves, charges, sends, retries or gives up on something, an event naming the entity, its identity, its values before and after, and the cause, so whoever diagnoses a problem later, by querying the logs, has what happened in hand. For the nuances (context across threads, pii, linking work across processes) it reads the articles of logika docs. It doesn't write rules about what must hold (validate, paths, timeouts, invariants) while it codes: those are written later with you, against real captures, in reflection with nock (nock reflect logika), which logika init installs.

Each component's conventions in logika.json's map say how it changes state and logs it: where its transitions happen (transitions: files or an ast-grep pattern), its span helpers, and globs for its event names, its identity param and its before and after params. The agent that instruments a component writes them, and amends them as it learns more.

At the end of a session, the stop hook logika init installs runs logika check --review. When the change touches at least 3 lines of code (not tests, comments or imports) in components marked "instrumented": true, that runs Logika's reviewer: your own agent command (LOGIKA_AGENT, claude -p by default), headless and read-only, which reads the diff against its components' conventions and tells your agent which transitions aren't logged yet and how, or nothing. It changes nothing itself and reviews the same diff once. LOGIKA_REVIEW=off turns it off; logika check --review runs it by hand.

Instrumenting existing code

The logika-instrument skill maps the components and lets you pick which to instrument (all of them when nobody can answer); for each of those, it chooses a logging strategy (spans in the body of the operations that matter, a middleware or wrapper that opens every request, a decorator, linked spans for jobs and consumers, a span per run for scripts) and writes it down as the component's conventions in logika.json: where its state transitions happen, its span helpers, how its events and params are named. It marks the component "instrumented": true, instruments, and runs logika check and the component's tests under logika test, correcting the code or the conventions until it's content with both. The conventions are the team's: check holds every change to them, and, for a component marked instrumented, the reviewer reads them when later work touches it.

Checks

logika check's mechanical layer is deterministic, never edits code, and blocks nothing but its exit code:

  • logika.json is validated: the ledger against the published JSON Schema, its conditions, paths and invariants parsed and type-checked as the server would, and its components and config;
  • the mechanical rules: an undeclared span name or attribute, a dynamic span name, a literal attribute of the wrong type, add_event on a declared span, a swallowed exception, a pii param without a key, stale bindings (names and attributes only once the ledger declares an event);
  • this change against each component's conventions, as warnings: a changed function at a transition site that opens no span, an event whose name misses the convention, that carries no identity, or a before without its after;
  • this change's files outside every component, to map or ignore;
  • the items this session claimed and no logika resolve closed;
  • at most once a day, a component marked instrumented whose transition sites changed by 200 lines, or 5 functions without an event, since logika.json did: instrument it again (LOGIKA_NUDGE=off turns it off).

A finding the team accepts is marked at its site: # logika: legacy[<rule>] for debt from before adoption, # logika: ignore[<rule>] -- <reason> for a deliberate exception. The conventions' warnings take no marker: fix the code or the conventions.

Local capture

logika test "<command>" runs a command (your test suite, or the app itself) with a local OTLP receiver capturing every span and log record, then has the server judge the capture against logika.json as it is on disk, synced or not: violations with their evidence, declared events never seen, undeclared spans. It exits 1 on a violation, so the agent catches a name that does not match the ledger before anything ships.

Feedback in production

What static analysis cannot see is caught nightly as instrumentation feedback: optional params that are always null, error events with empty operands, cardinality spikes, spans that systematically time out, undeclared names your own code sends in production, growing "other" shares, and events with no validate, paths or rules. Each finding lands in the queue; ask the agent to work it from the console.

Doctrine

The logika-instrument skill, which a coding agent reads before it instruments, applies one question to code that runs in production: once this runs, what changed, and what would we need to see to understand it, and fix it, without reproducing it?

  • the span's name is a string literal equal to the declared path; what varies goes in a param;
  • params are start attributes, so a hang shows what it was doing;
  • an outcome is a child span ended at once, never add_event, which stays hidden until the parent ends;
  • a failure is recorded once, where it happens: let the exception escape the span, or set error status; never log and rethrow, never swallow;
  • expected outcomes (a declined card) are ordinary children, never errors;
  • log each state transition with the entity, its identity, before and after, and the cause;
  • report the app version from every process, so a regression can be told from code a fix has not reached;
  • secrets never, and personal data only under pii.