statelog
Enable observability and configure a log file to save eval annotations.
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:
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.
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.
/** 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.
/** 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.
/** 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.
/** 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.
/** 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.
/** 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.
/** A trace's rows. */
export type StatelogRows = {
rows: StatelogRow[]
}(source)
StatelogOutlinedTrace
A trace's fields with its rows beside them.
/** A trace's fields with its rows beside them. */
export type StatelogOutlinedTrace = StatelogTrace & StatelogRows(source)
StatelogOutline
What logOutline returns.
/** What `logOutline` returns. */
export type StatelogOutline = {
log: string;
traces: StatelogOutlinedTrace[];
text: string
}(source)
StatelogHit
One row that matched a search.
/** 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.
/** 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.
/** 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
@alwaysUnder(dir)
effect std::read {
dir: string;
filename: string
}(source)
Functions
emit
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:
| Name | Type | Default | Description |
|---|---|---|---|
| data | any | The payload the host's onEmit callback should receive. |
(source)
setAgentName
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:
| Name | Type | Default | Description |
|---|---|---|---|
| name | string | Use 1–200 ASCII letters, digits, ".", "_", "-", or "/", with no empty, ".", or ".." path segments. |
(source)
turnStart
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:
| Name | Type | Default | Description |
|---|---|---|---|
| message | string | The message the person sent. |
(source)
evalValue
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:
| Name | Type | Default | Description |
|---|---|---|---|
| value | any | A JSON-serializable value representing the input the user supplied. |
(source)
evalOutput
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:
| Name | Type | Default | Description |
|---|---|---|---|
| value | any | A JSON-serializable value representing the response the user receives. |
(source)
evalRecord
evalRecord(
statelogPath: string,
allowedPaths: string[] = [],
): StatelogEvalRecordRead 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:
| Name | Type | Default | Description |
|---|---|---|---|
| statelogPath | string | A JSONL file containing one trace, relative to the working directory or absolute. | |
| allowedPaths | string[] | [] | Restrict reads to these directories, or leave empty to allow any path. |
Returns: StatelogEvalRecord
(source)
evalValues
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:
| Name | Type | Default | Description |
|---|---|---|---|
| statelogPath | string | A JSONL file containing one trace, relative to the working directory or absolute. | |
| allowedPaths | string[] | [] | Restrict reads to these directories, or leave empty to allow any path. |
Returns: StatelogEvalValue[]
(source)
evalOutputs
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:
| Name | Type | Default | Description |
|---|---|---|---|
| statelogPath | string | A JSONL file containing one trace, relative to the working directory or absolute. | |
| allowedPaths | string[] | [] | Restrict reads to these directories, or leave empty to allow any path. |
Returns: StatelogEvalValue[]
(source)
finalEvalOutput
finalEvalOutput(
statelogPath: string,
allowedPaths: string[] = [],
): StatelogEvalValue | nullRead 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:
| Name | Type | Default | Description |
|---|---|---|---|
| statelogPath | string | A JSONL file containing one trace, relative to the working directory or absolute. | |
| allowedPaths | string[] | [] | Restrict reads to these directories, or leave empty to allow any path. |
Returns: StatelogEvalValue | null
(source)
logTraces
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:
| Name | Type | Default | Description |
|---|---|---|---|
| log | string | "" | The statelog file, relative to the working directory or absolute. Empty reads this run's own log. |
| select | StatelogTraceSelection | {} | Which traces: trace (an id or unique prefix), last (how many), since (30m, 2h, 3d, or an ISO date), or all. |
| errorsOnly | boolean | false | Keep only the traces that logged an error, which includes every failed tool call. |
Returns: Result<StatelogTraceList>
Throws: std::read
Needs: fileRead
(source)
logOutline
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:
| Name | Type | Default | Description |
|---|---|---|---|
| log | string | "" | The statelog file, relative to the working directory or absolute. Empty reads this run's own log. |
| select | StatelogTraceSelection | {} | 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. |
| approvals | boolean | false | Also show approved interrupts, which are hidden by default. |
| admin | boolean | false | Also show the handler decisions under each interrupt. |
Returns: Result<StatelogOutline>
Throws: std::read
Needs: fileRead
(source)
logSearch
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:
| Name | Type | Default | Description |
|---|---|---|---|
| pattern | string | The text to find; a JavaScript regular expression when regex is true. | |
| log | string | "" | The statelog file, relative to the working directory or absolute. Empty reads this run's own log. |
| select | StatelogTraceSelection | {} | 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. |
| kinds | string[] | [] | Only these kinds of row: user, call, tool, hosted, error. Empty searches every kind. |
| regex | boolean | false | Read the pattern as a regular expression. |
| caseSensitive | boolean | false | Match case; by default case is ignored. |
| limit | number | 30 | Return at most this many rows. |
| context | number | 60 | Characters of snippet on each side of the match. |
Returns: Result<StatelogSearchResult>
Throws: std::read
Needs: fileRead
(source)
logRow
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:
| Name | Type | Default | Description |
|---|---|---|---|
| address | string | A row address, traceId/rowId, as logOutline and logSearch return it. | |
| log | string | "" | The statelog file, relative to the working directory or absolute. Empty reads this run's own log. |
| maxChars | number | 0 | Cut the text to at most this many characters, 0 for no cap or at least 100. |
| events | boolean | true | Include 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)