Install
Install with PiG
pig install npm:flattrace-piInstall with Pi
pi install npm:flattrace-piPackages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.
Pi manifest JSON
The pi field of package.json.
{
"extensions": [
"./extensions/flattrace-pi.js"
]
}README
Written by the package's publisher, shown as published in version 0.1.2 (README.md). PiG does not review it.
flattrace-pi
A generic flattrace recorder for Pi sessions. When enabled, it writes local JSONL traces of timing, causal links, journal pointers and tool activity.
Install
Requirements: Pi >= 1.4.0 and < 2, Node >= 22.18 (or Pi's higher requirement).
Pi 1.4.0 is the tested floor: the recorder depends on agent_settled.aborted,
the <skill> block format, parentToolCallId, and appendMessage object identity.
pi install npm:flattrace-pi
Or add it to the packages array in your Pi settings:
{ "packages": ["npm:flattrace-pi"] }
For local development, pi install ./flattrace-pi loads the package directory.
Turn recording on or off
Recording is opt-in. Set a writable directory before starting Pi:
export FLATTRACE_DIR="$HOME/.local/share/flattrace-pi"
export FLATTRACE_LEVEL=1
Unset FLATTRACE_DIR or set FLATTRACE_LEVEL=0 to turn recording off.
When off, recording opens no writer, writes no trace files and starts no timer.
A sink failure (unwritable directory, malformed inherited context) disables
tracing for the rest of the process with one stderr warning; product work
is never interrupted.
Level 1: what it records
One run file per session (.jsonl under $FLATTRACE_DIR). Envelope tool
is the writing program: flattrace-pi by default, or the host name given to
configureHost (see below). A second session in the same process (/new,
/resume, /fork) opens a new run file whose start carries
data.links: [{relation: "continues_process", trace, run}] pointing at the
run it replaces. The old run takes no new spans, drains its open spans, then
ends. At quit, spans still open end cancelled. Compaction and model changes
stay in the same run. Process-level samples go to the active run.
Events (see flattrace-events.json for the exact field lists):
run— the run span and its end summary: observed counts, usage subtotals with known/missing accounting, process residence, sampling costs.pi.agent,pi.turn,pi.model(plusheaders,response,first_token,unjoinedpoints),pi.message,pi.tool(plusunstarted),pi.compaction(plusunstarted),pi.input,pi.inventory,pi.resources,pi.ext.pi.session.bind— the session id changed (or the first bind). The process run outlives any one session; this is the record of which session a span belongs to.pi.journal— exact persisted entry ids. Model, tool and compaction spans link the entries Pi persisted for them. The admitted user prompt is joined by object identity afterappendMessage: the message object extensions saw atmessage_endis the same object Pi persists, so the join never matches text, time, or the latest user row. A steered input that never lands in the journal stays unjoined; missing persistence stays missing.pi.skill.read— a skill file the session actually read, from the two observed paths: the persisted<skill name location>block of a/skill:expansion (source: "command"), or atool_resultwhoseinput.pathis aSKILL.mdfile or a file beside one (source: "tool", including calls nested under a parent tool call). Data is{name, path, source, range, bytes, content_sha256, complete}plusparentToolCallIdfor nested calls. The hash covers the bytes actually observed — the persisted block body for the command path (never the disk, which may have changed since), the exact returned text for the tool path — and no content is stored.rangeis{offset, limit}for a partial read and null for a whole one;completeis false when the returned text was truncated or partial. It is measured ontool_result, never ontool_call: a failed or blocked read emits no point at all, so no failed read can claim a full one.annotate/annotation/verdict— detector and grader results (see below). A detector notices a shape; a grader compares against a reference. Neither is authority: suggestive labels only. These names are declared by the flattrace dependency's ownschema/conventions-events.json, which this package registers at load (resolved from the installedflattracepackage) and lists in every run-startdata.vocabularies; they are deliberately not repeated in this package's vocabulary, so composed runs never see conflicting definitions.
Deliberately not recorded: the event-loop sampler some setups run stays out of this package. No message bodies, prompts, tool arguments, results, error prose, credentials, or file content are stored at the default level.
What a run records
The vocabulary (flattrace-events.json) declares event shapes: each event
maps to a kind (span or point) and its field list. It never validates
payload values. The shared run span is exactly the reserved declaration
from the flattrace dependency ({"kind":"span","fields":{}}): every
vocabulary in a composed run copies that same block, so a differing run
would collide under the union check. The run's data meanings live here, not
in the vocabulary:
- Run start
data:command(sessionfor live Pi runs),sessionId(the Pi session id the run was opened for; turns join to spans on it),level(the effectiveFLATTRACE_LEVEL),vocabularies(the registered vocabularies actually in force:[{tool, version, sha256}]with the SHA-256 over the exact file bytes parsed),links(only on a rotated run: thecontinues_processpointer at the run it replaces; batch detector runs instead linkannotatesat each run they labelled),env(harness/host facts:harness.name,harness.version,host.name, initialgen_ai.provider.name,gen_ai.request.model,model.requested_effort, andsession.start_reasonwhen known; unknown stays null), andinventory(initial committed-package discovery for tools/commands, with the active/instrumented scope caveats). The writer itself addsargv(empty here),host,version, andpid. - Run end
data:summary(observed counts, usage subtotals with known/missing accounting, wall timings),process(this Node process residence over the run),processLifetimeMs(uptime at close),resourceSampling/inventorySampling(collection costs),cancelledAtQuit(open spans endedcancelledat quit; zero when the run drained cleanly), andoutcomeScope(the run outcome describes local session shutdown, not task/model/tool success). A rotated run that drained after replacement closes with a drain note instead. - A check needs the union of the run's vocabularies: pass every file its
manifest lists with repeatable
--events(see the flattrace checker). An extra--eventsfile the manifest does not list fails the check and is named; a listed file whose bytes differ fails too. With no manifest (a bare-writer run), a single vocabulary file must match the envelope tool.
Level 2: the opt-in verbosity module
At the default level the recorder stores no argument vectors. Set
FLATTRACE_LEVEL=2 (or higher) to also load vocabularies/verbosity.json
and emit action points: one known fact per tool call for Pi's built-in
tools.
kindcomes only from a named, versioned allowlist of Pi built-in tools (read,edit,write,bash,powershell,grep,find,ls; allowlist version 1.4.0). Anything else recordskind: null, never a guess.argvpasses the writer'ssanitizeArgvplus tool-specific redaction: only scalar path-like parameters are kept; free-text parameters (command,content,text,prompt,messageand the like) are never stored, and their names are listed inwithheldParamsso the omission is explicit.
The vocabulary file declares action with level: 2, and the run start
lists only the vocabularies actually registered, so a level-1 run never
claims level-2 events.
Privacy
At level 1 the recorder stores no prompts, message bodies or free-text tool
parameters. Credentials must never be recorded at any level. Counts, timings,
hashes and presence flags are the default; action.argv exists only at level 2
and is sanitized before storage. Seeded credential negatives are tested.
Skill reads hash the observed bytes without storing content.
Custom event data and detector labels are supplied by your code: do not put
credentials or private prose in them. Use flattrace export to prepare traces
for sharing, and review the exported data before sharing it.
Extend it with your own vocabulary
At extension load time — before the writer opens on session_start — call:
const registry = globalThis.__flattrace_pi;
registry.registerVocabulary("/path/to/your/package/your-events.json");
registry.registerDetector({ name: "my-shape", version: "1.0.0", kind: "code", run });
registry.registerGrader({ name: "my-grade", version: "1.0.0", kind: "code", run });
registerVocabulary(path): registers a package-owned vocabulary file. The exact file bytes are hashed (SHA-256) and the pair is listed in the run start'sdata.vocabularies: [{tool, version, sha256}], where tool and version come from the nearestpackage.jsonabove the file. A fragment that redefinesrunor anypi.*event with a different definition is refused. Registration after the writer has opened throws an API error; it is never silently omitted and never rewrites the flushed start line. Points for your events go throughregistry.emitPoint(event, data, span?), which refuses names outside the registered union as described below.
Write spans from your extension
The process handle also carries the public span/context API (frozen names):
const registry = globalThis.__flattrace_pi;
// At load, before the writer opens: name the run envelope for your program.
// Default is flattrace-pi with the package version. One host per process:
// repeating the same host is a no-op (extension factories re-run on every
// session rotation, with the previous run still open), and any change —
// a different host, or a first host after the writer opened — is an error.
registry.configureHost({ tool: 'my-program', toolVersion: '1.2.0' });
// Async: ends with the outcome the callback reports (a string return, or
// {outcome, data}), `unknown` when it reports none, `error` on throw.
await registry.withSpan('my.event', { where: 'work' }, async span => {
registry.emitPoint('my.note', { where: 'inside' });
const traceparent = registry.currentTraceparent(); // W3C string for children
return 'ok';
});
// Manual: beginSpan returns an opaque handle (null when tracing is off);
// endSpan routes the end to the run that opened the span, even across
// rotation, and never throws for lifecycle reasons.
const handle = registry.beginSpan('my.event', { where: 'work' });
registry.endSpan(handle, 'ok');
const current = registry.currentSpan(); // innermost live span handle, or null
const run = registry.currentRun(); // {trace, run}, or null when tracing is off
Spans and points use AsyncLocalStorage context. Declare event names in
your vocabulary first. Runtime vocabulary mistakes are fail-safe by default:
- Recording off (
FLATTRACE_DIRunset or empty, level 0, or writer disabled):beginSpan,withSpan,emitPointandannotate*perform no vocabulary checks. They return null, or run thewithSpancallback with a null handle and pass its value or throw through unchanged, as for other off-state calls. - Recording on, undeclared name:
beginSpanandemitPointreturn null;withSpanruns its callback with a null handle, passing its value or throw through unchanged. No span or point for that name is written. Instead,pi.trace.undeclaredrecordsdata: {event, api}once per (event, api) per run, whereapiisbeginSpan,withSpanoremitPoint. - Set
FLATTRACE_PI_STRICT=1in consumer test suites to restore the existing undeclared-name error, whether recording is on or off. Only the exact value1enables strict mode; every other value leaves it off.
Non-vocabulary programming errors, including foreign span handles, non-string
outcomes and configureHost conflicts, still throw.
Pass
currentTraceparent() to a child process through FLATTRACE_TRACEPARENT;
a child writer opened with fromEnv parents its run to that span. See the
live proof (proof/) for a worked example: nested public-API spans, an
inner point, and a spawned child run.
Per-call context requires opt-in
A tool must opt in through traceOptIn or the public bus handshake to get its
own pi.tool span as its execution context. Without opt-in, the recorder can
still observe the tool lifecycle, but the tool's currentTraceparent() returns
the run root, not its per-call span. This matters when parenting custom spans
or child processes, especially across parallel tool calls.
import { traceOptIn } from 'flattrace-pi';
pi = traceOptIn(pi, { name: 'my-package', version: '1.0.0', source: import.meta.url });
pi.registerTool(toolDefinition); // register through the returned facade
See the context proof for the recorder-first, recorder-last, tracing-off and no-recorder cases and the independent bus fixture.
Detectors and graders
registerDetector({name, version, kind, questions?, run})/registerGrader({...}):kindismodel,code, orhuman.runreceives{target: {trace, run, span?, entryId?}, input_sha256, chunk}and returns{labels: [{label, probability|null}]}(detector) or verdict fields{correct, score, reward, reason}plus optionalusage/cost(grader). Return{refused: true, reason}to record a refusal. The recorder wraps each call in anannotatespan through the sharedannotate()helper (lib/annotate.mjs): it writes the span and the resultingannotation/verdictpoints in the convention shape —target{trace, run, span?, entryId?, t_from?, t_to?, seq?}, a stableidacross re-labels, optionalsupersedesnaming the replaced annotation, anddetector{name, version, kind}— and ends the spanok,refused,error, orcancelled. A call still in flight at shutdown endscancelled; close never waits for a detector. An unavailable model route never blocks recording: there is no dispatch without yourrun, and no retry of an unknown send.
Annotate the active run directly
annotateCurrent({detector, target, input_sha256, labels | run, usage?, cost?})
is exported from flattrace-pi and available on globalThis.__flattrace_pi.
It uses the private active writer and the same annotate() implementation,
without dispatching any registered detector or grader.
import { annotateCurrent } from 'flattrace-pi';
const call = annotateCurrent({
detector: { name: 'my-shape', version: '1.0.0', kind: 'code' },
target: { entryId }, input_sha256,
labels: [{ label: 'shaped', probability: null }],
});
if (call) await call.done;
Missing target.trace and target.run are filled from the active run; an
explicit foreign trace or run throws an API error before writing or running
code. Omit target to label the run itself. Supply either a labels array or
one run callback with the same contract as annotate, never both. Optional
usage and cost are defaults; callback results can override them. The return
is {span, id, done}, or null with no trace I/O and no callback execution when
tracing is off or no run is active. This call never opens a writer.
Batch and late detector runs
A batch or late detector run (for example a learning pass over earlier,
closed runs) is its own run, written through the public flattrace Writer
by the program that runs it — flattrace-pi never opens batch runs. That
run links each target run with run-start
data.links: [{relation: "annotates", trace, run}], lists the
conventions vocabulary in data.vocabularies, and labels targets through
the same helper the live dispatch uses:
import { Writer } from 'flattrace';
import { annotate } from 'flattrace-pi';
const writer = Writer.fromEnv('my-detector-run', '1.0.0', { argv: [],
data: { command: 'batch',
links: [{ relation: 'annotates', trace, run }],
vocabularies: [{ tool: 'flattrace-conventions', version: '0.3.0', sha256 }] } });
const { id, done } = annotate(writer,
{ detector: { name: 'my-shape', version: '1.0.0', kind: 'code' },
target: { trace, run, entryId }, input_sha256, chunk: { entryId },
run: ({ target, input_sha256, chunk }) =>
({ labels: [{ label: 'shaped', probability: null }] }) });
await done; // {status: 'ok'|'refused'|'error'|'cancelled', span, id}
writer.close('ok', {});
annotate(writer, {detector, target, input_sha256, chunk, run}) accepts
any flattrace Writer, opens the span synchronously (so a live dispatcher
can track it for at-quit cancellation), and settles asynchronously.
input_sha256 fingerprints the detector input and is null only with
explicit coverage; chunk is passed to run and never stored. The live
proof (proof/) pairs a live annotation with a batch run targeting a
prior closed proof run; both pass the union checker.
Check a trace
Use the flattrace 0.3.0 checker with every vocabulary listed in the run's manifest. For a default level-1 run:
flattrace check run-*.jsonl --events node_modules/flattrace-pi/flattrace-events.json \
--events node_modules/flattrace/schema/conventions-events.json
Use the paths from your actual installation. Add
--events node_modules/flattrace-pi/vocabularies/verbosity.json for level 2
and each custom vocabulary declared by the run. Manifest hashes must match
these exact files.
Layout
extensions/flattrace-pi.js— Pi extension entry (factory).lib/— recorder implementation (ESM).flattrace-events.json— the package vocabulary (owner: this package).vocabularies/verbosity.json— opt-in level-2 fragment.internal/— private development notes, excluded from any public export.tests/— unit tests (node --test 'tests/*.test.mjs').
Changelog
See CHANGELOG.md. License: MIT.