# `Imp.Trajectory`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/trajectory.ex#L1)

ATIF-v1.8 projection of ordered native `Imp.Run.Event` observations.

`to_atif/2` projects one run's ordered events into a JSON-encodable ATIF
document that reads as the model saw the run:

  * The first request's messages are steps marked as copied context, with
    their roles, an assistant message's tool calls and reasoning, and each
    tool message as the observation of the call it answers. A user or system
    message a later request appends is a step of its own.
  * Each model response is one agent step (`llm_call_count: 1`, or null for
    a response served from Imp's cache) with the response text as
    `message`, the provider's own reasoning as `reasoning_content`, the
    calls the loop dispatched for it as `tool_calls`, `model_name`, and
    `metrics` from its usage and cost.
  * A tool result's `content` is the tool message the model read in the
    next request, which may be shortened or rendered differently from what
    the tool returned. `extra.seen_by_model` says whether a later request
    carried it; when the two differ, the tool's own output is kept in
    `extra.output`.
  * A tool call no model response asked for, such as one a host dispatches
    itself, is a step of its own with `llm_call_count: 0`.
  * A call whose arguments are not a JSON object keeps empty `arguments`
    and the rest in `extra.raw_arguments`. For a dispatched call that is
    its `:tool_call` event's input: the text the model sent when it does
    not parse, or the decoded value when it parses to something else (a
    JSON array is a list). A call replayed in a request keeps the string
    the request carried.
  * `agent.tool_definitions` holds every tool roster the run sent (from
    `:tools_sent`), `agent.model_name` the model when every request used the
    same one, and `final_metrics` the run's token and cost totals where
    every response reported them.

Every model request is also kept whole, without repeating the history it
shares with the requests before it. `extra.model_messages` lists each
distinct message the run sent once, in the order the requests first sent
it, and each entry of `extra.model_requests` (in request order, with its
`metadata`) names the messages it sent, in order, as `message_ids`:
positions in `extra.model_messages`, counted from 0. A request sent exactly
the messages at its `message_ids`, in order (read them through a tuple,
`elem(List.to_tuple(model_messages), id)`, as `Enum.at/2` on each id is
quadratic on a long run). Messages are told apart as the document shows
them, after redaction, so two that differed only in a redacted secret share
one position. A request whose
input was not a list of messages keeps it as `messages` instead. Listing
each request's messages in full made the document, and the redaction over
it, grow as the number of requests times the length of the history.
Lifecycle events and capture gaps become entries in `extra.diagnostics`
rather than dialogue steps. A loop's `:reasoning` event is the visible
thought a step wrote, which the response already carries, so it is kept on
that response step as `extra.next_thought`. `extra.terminal_event` is the
kind of the event that ended the run (`"run_finished"`, `"run_failed"`,
`"run_cancelled"`), or `"unknown"` when the events hold none.

A tool result's `extra.outcome`, on the result and on its call, is the
`t:Imp.Tool.outcome/0` the loop recorded on the `:tool_result` event
(`metadata.outcome`); an event that carries none, and a call whose result was
not captured, is `"unknown"`.

Stored event maps are read by their `"kind"`. A kind that is not one of
`Imp.Run.Event.kinds/0`, such as one a host emitted itself, keeps its stored
string and becomes a diagnostic entry.

Inference counts, metrics, reasoning and tool results are never synthesized
from missing evidence. Step tool call IDs are scoped by run and event
sequence, with the provider's ID in `extra.native_tool_call_id`. A provider
tool call ID may be reused once its result has arrived, but an overlapping
reuse raises `ArgumentError`.

The document is a projection, not a replay and not a durable effect ledger.
Redaction runs again on export, including caller metadata; prompts
and results are still private application data.

# `to_atif`

Builds a JSON-encodable ATIF document from one run's ordered events or stored event maps.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
