Skip to content

statelog ​

Enable observability and configure a log file to save eval annotations.

ts
import { evalValue, evalOutput } from "std::statelog"

node main(question: string) {
  evalValue(question)
  const answer: string = llm(question)
  evalOutput(answer)
}

Use evalOutput when the response you want graded differs from the entry node's return value. Read a saved run with evalRecord, or use finalEvalOutput when you only need its last output:

ts
import { finalEvalOutput } from "std::statelog"

node main() {
  const output = finalEvalOutput("./statelog.jsonl")
  return output
}

setAgentName groups runs by agent. turnStart marks messages from a person for the logs viewer. emit sends custom data to the host's onEmit callback.

Reading a log ​

A log file can hold many runs. Four functions read it the way the agency logs commands do: logTraces lists the runs, logOutline prints the rows of one, logSearch finds the rows that mention some text, and logRow opens one row. Each returns the command's JSON, plus text, which is what the command prints.

ts
import { logTraces, logOutline } from "std::statelog"

node main() {
  const recent = logTraces("./statelog.jsonl", { since: "1d" }, errorsOnly: true)
  if (isFailure(recent)) {
    return recent
  }
  for (trace in recent.value.traces) {
    const outline = logOutline("./statelog.jsonl", { trace: trace.traceId })
    if (isSuccess(outline)) {
      print(outline.value.text)
    }
  }
}

Each one reads a file, so each one raises std::read with the log's real directory and name. An empty log reads the log this run is writing. To hand a reader to a model, fix the log first, so the model cannot point it at another file: logTraces.partial(log: "./statelog.jsonl").

Types ​

StatelogEvalValue ​

An eval input or output with its thread and time in milliseconds since trace start.

ts
/** An eval input or output with its thread and time in milliseconds since trace start. */
export type StatelogEvalValue = {
  value: any;
  threadId?: string;
  tMs: number;
  truncated?: boolean
}

(source)

StatelogEvalRecord ​

A normalized view of one saved run for grading and analysis.

ts
/** A normalized view of one saved run for grading and analysis. */
export type StatelogEvalRecord = {
  traceId: string;
  recordVersion: number;
  formatVersion: number;
  durationMs: number;
  startedAtMs: number;
  agentName?: string;
  source: string;
  evalValues: StatelogEvalValue[];
  evalOutputs: StatelogEvalValue[];
  threads: Record<string, any>[];
  events: Record<string, any>[];
  interrupts: Record<string, any>[];
  errors: Record<string, any>[];
  incomplete: Record<string, any>[];
  metrics: Record<string, any>;
  warnings: string[]
}

(source)

StatelogTraceSelection ​

Which traces of a log to read. Give at most one of trace and all; last and since can go together. A field left out, or set to null or "", does not count. With none, logTraces reads the 20 most recent traces and the other readers read the most recent one.

ts
/** Which traces of a log to read. Give at most one of `trace` and `all`;
 *  `last` and `since` can go together. A field left out, or set to `null`
 *  or `""`, does not count. With none, `logTraces` reads the 20 most recent traces and
 *  the other readers read the most recent one. */
export type StatelogTraceSelection = {
  trace?: string;
  last?: number;
  since?: string;
  all?: boolean
}

(source)

StatelogTrace ​

One run in a log, as agency logs traces --json describes it.

ts
/** One run in a log, as `agency logs traces --json` describes it. */
export type StatelogTrace = {
  traceId: string;
  startedAt?: string;
  workingMs?: number;
  costUsd: number;
  tokens: number;
  calls: number;
  agentName?: string;
  hasError: boolean;
  otherErrors: number;
  toolCalls: number;
  toolsFailed: number;
  toolsRejected: number;
  toolsUnfinished: number;
  ask?: string
}

(source)

StatelogTraceList ​

The traces logTraces picked, newest first.

ts
/** The traces `logTraces` picked, newest first. */
export type StatelogTraceList = {
  log: string;
  total: number;
  older: number;
  traces: StatelogTrace[];
  text: string
}

(source)

StatelogRow ​

One row of an outline. address is the value logRow takes.

ts
/** One row of an outline. `address` is the value `logRow` takes. */
export type StatelogRow = {
  address: string;
  rowId: string;
  kind: string;
  depth: number;
  header: string
}

(source)

StatelogRows ​

A trace's rows.

ts
/** A trace's rows. */
export type StatelogRows = {
  rows: StatelogRow[]
}

(source)

StatelogOutlinedTrace ​

A trace's fields with its rows beside them.

ts
/** A trace's fields with its rows beside them. */
export type StatelogOutlinedTrace = StatelogTrace & StatelogRows

(source)

StatelogOutline ​

What logOutline returns.

ts
/** What `logOutline` returns. */
export type StatelogOutline = {
  log: string;
  traces: StatelogOutlinedTrace[];
  text: string
}

(source)

StatelogHit ​

One row that matched a search.

ts
/** One row that matched a search. */
export type StatelogHit = {
  address: string;
  traceId: string;
  rowId: string;
  kind: string;
  header: string;
  matches: number;
  snippet: string
}

(source)

StatelogSearchResult ​

What logSearch returns.

ts
/** What `logSearch` returns. */
export type StatelogSearchResult = {
  log: string;
  pattern: string;
  tracesSearched: number;
  matched: number;
  omitted: number;
  hits: StatelogHit[];
  text: string
}

(source)

StatelogExpandedRow ​

One row, expanded as the logs viewer shows it.

ts
/** One row, expanded as the logs viewer shows it. */
export type StatelogExpandedRow = {
  address: string;
  kind: string;
  header: string;
  text: string;
  truncated: boolean;
  events: Record<string, any>[];
  providerItems?: Record<string, any>[]
}

(source)

Effects ​

std::read ​

ts
@alwaysUnder(dir)
effect std::read {
  dir: string;
  filename: string
}

(source)

Functions ​

emit ​

ts
emit(data: any)

Emit a custom event to the calling TypeScript code.

Use with the host's onEmit callback to deliver progress or other application events.

Parameters:

NameTypeDefaultDescription
dataanyThe payload the host's onEmit callback should receive.

(source)

setAgentName ​

ts
setAgentName(name: string)

Set a stable display name for this agent, used to group its runs.

Call once near startup so the runs explorer and statelog's eval pages group this agent's runs under a stable name. The last call in a trace wins. Use slashes to group variants, such as "agency-agent/coordinator".

Parameters:

NameTypeDefaultDescription
namestringUse 1–200 ASCII letters, digits, ".", "_", "-", or "/", with no empty, ".", or ".." path segments.

(source)

turnStart ​

ts
turnStart(message: string)

Mark the start of a turn when a person sends this program a message.

Records a turnStart event. The logs viewer splits a session into turns at these events: each turn runs from its message to the next, and the time after a turn's last event is shown as waiting for the person. The repl function in std::ui/cli logs one for every line a person submits that is not a slash command, so a program that takes its messages another way (a seeded first prompt, a chat server) calls this itself.

Parameters:

NameTypeDefaultDescription
messagestringThe message the person sent.

(source)

evalValue ​

ts
evalValue(value: any)

Record an input to this agent for evaluation.

Mark the actual input at the point your agent receives it, so evaluators do not have to infer it from the conversation. Without an annotation, extraction uses the last user-role message in the first LLM completion on the top-level thread and adds a warning.

Repeated calls contribute separate entries to evalValues in order. Values follow JSON serialization rules. Circular references, bigint, and top-level functions or symbols throw at the call site.

Parameters:

NameTypeDefaultDescription
valueanyA JSON-serializable value representing the input the user supplied.

(source)

evalOutput ​

ts
evalOutput(value: any)

Record the agent's response for evaluation.

Use when the response to grade differs from the entry node's return value, or the node returns nothing. Explicit outputs replace the return value in the eval record. Without an annotation, extraction uses the entry node's non-null return value.

Repeated calls contribute separate entries to evalOutputs in order. finalEvalOutput selects the last entry. Values follow the same JSON serialization rules as evalValue.

Parameters:

NameTypeDefaultDescription
valueanyA JSON-serializable value representing the response the user receives.

(source)

evalRecord ​

ts
evalRecord(
  statelogPath: string,
  allowedPaths: string[] = [],
): StatelogEvalRecord

Read a saved statelog trace for evaluation or analysis.

Inspect a previous run from Agency code without invoking the CLI. Returns the same record as agency eval extract, including eval values, outputs, conversation activity, metrics, and extraction warnings. The file must contain a single trace.

Parameters:

NameTypeDefaultDescription
statelogPathstringA JSONL file containing one trace, relative to the working directory or absolute.
allowedPathsstring[][]Restrict reads to these directories, or leave empty to allow any path.

Returns: StatelogEvalRecord

(source)

evalValues ​

ts
evalValues(
  statelogPath: string,
  allowedPaths: string[] = [],
): StatelogEvalValue[]

Read the evaluation inputs from a saved statelog trace.

Read the inputs used for evaluation without the full run record. Includes inferred input when the agent did not call evalValue.

Parameters:

NameTypeDefaultDescription
statelogPathstringA JSONL file containing one trace, relative to the working directory or absolute.
allowedPathsstring[][]Restrict reads to these directories, or leave empty to allow any path.

Returns: StatelogEvalValue[]

(source)

evalOutputs ​

ts
evalOutputs(
  statelogPath: string,
  allowedPaths: string[] = [],
): StatelogEvalValue[]

Read the evaluation outputs from a saved statelog trace.

Read all outputs used for evaluation, preserving their order. Uses explicit evalOutput annotations when present, otherwise the entry node's non-null return value. Returns an empty array when neither exists.

Parameters:

NameTypeDefaultDescription
statelogPathstringA JSONL file containing one trace, relative to the working directory or absolute.
allowedPathsstring[][]Restrict reads to these directories, or leave empty to allow any path.

Returns: StatelogEvalValue[]

(source)

finalEvalOutput ​

ts
finalEvalOutput(
  statelogPath: string,
  allowedPaths: string[] = [],
): StatelogEvalValue | null

Read the last evaluation output from a saved statelog trace, or null if none exists.

Select the last entry from evalOutputs for a judge that needs one answer. The result includes the value's thread and time, as well as its payload.

Parameters:

NameTypeDefaultDescription
statelogPathstringA JSONL file containing one trace, relative to the working directory or absolute.
allowedPathsstring[][]Restrict reads to these directories, or leave empty to allow any path.

Returns: StatelogEvalValue | null

(source)

logTraces ​

ts
logTraces(
  log: string = "",
  select: StatelogTraceSelection = {},
  errorsOnly: boolean = false,
): Result<StatelogTraceList> raises <std::read>

List the runs in a statelog file, newest first.

List the traces in a log, newest first: when each started, how long it worked, what it cost, and what went wrong. The same list as agency logs traces.

Parameters:

NameTypeDefaultDescription
logstring""The statelog file, relative to the working directory or absolute. Empty reads this run's own log.
selectStatelogTraceSelection{}Which traces: trace (an id or unique prefix), last (how many), since (30m, 2h, 3d, or an ISO date), or all.
errorsOnlybooleanfalseKeep only the traces that logged an error, which includes every failed tool call.

Returns: Result<StatelogTraceList>

Throws: std::read

Needs: fileRead

(source)

logOutline ​

ts
logOutline(
  log: string = "",
  select: StatelogTraceSelection = {},
  approvals: boolean = false,
  admin: boolean = false,
): Result<StatelogOutline> raises <std::read>

Print every row of a trace in a statelog file, with the address of each.

Every row of a trace with its address, as agency logs outline prints it. Pass an address to logRow to open that row.

Parameters:

NameTypeDefaultDescription
logstring""The statelog file, relative to the working directory or absolute. Empty reads this run's own log.
selectStatelogTraceSelection{}Which traces: trace (an id or unique prefix), last (how many), since (30m, 2h, 3d, or an ISO date), or all. With none, the most recent.
approvalsbooleanfalseAlso show approved interrupts, which are hidden by default.
adminbooleanfalseAlso show the handler decisions under each interrupt.

Returns: Result<StatelogOutline>

Throws: std::read

Needs: fileRead

(source)

logSearch ​

ts
logSearch(
  pattern: string,
  log: string = "",
  select: StatelogTraceSelection = {},
  kinds: string[] = [],
  regex: boolean = false,
  caseSensitive: boolean = false,
  limit: number = 30,
  context: number = 60,
): Result<StatelogSearchResult> raises <std::read>

Find the rows of a statelog file that mention some text.

Find the rows whose own text matches, as agency logs search does. A word repeated in the history of every later LLM call is found once, on the row where it first appeared.

Parameters:

NameTypeDefaultDescription
patternstringThe text to find; a JavaScript regular expression when regex is true.
logstring""The statelog file, relative to the working directory or absolute. Empty reads this run's own log.
selectStatelogTraceSelection{}Which traces: trace (an id or unique prefix), last (how many), since (30m, 2h, 3d, or an ISO date), or all. With none, the most recent.
kindsstring[][]Only these kinds of row: user, call, tool, hosted, error. Empty searches every kind.
regexbooleanfalseRead the pattern as a regular expression.
caseSensitivebooleanfalseMatch case; by default case is ignored.
limitnumber30Return at most this many rows.
contextnumber60Characters of snippet on each side of the match.

Returns: Result<StatelogSearchResult>

Throws: std::read

Needs: fileRead

(source)

logRow ​

ts
logRow(
  address: string,
  log: string = "",
  maxChars: number = 0,
  events: boolean = true,
): Result<StatelogExpandedRow> raises <std::read>

Open one row of a statelog file by its address.

Open one row by the address logOutline or logSearch gave, as the logs viewer shows it, with the statelog events behind it. maxChars bounds text, the marker included. events is not capped, and for an LLM call it holds the call's whole message history, so pass events: false when a model calls this as a tool.

Parameters:

NameTypeDefaultDescription
addressstringA row address, traceId/rowId, as logOutline and logSearch return it.
logstring""The statelog file, relative to the working directory or absolute. Empty reads this run's own log.
maxCharsnumber0Cut the text to at most this many characters, 0 for no cap or at least 100.
eventsbooleantrueInclude the statelog events behind the row, and a hosted row's provider items. They can be very large.

Returns: Result<StatelogExpandedRow>

Throws: std::read

Needs: fileRead

(source)