nemo_gym.token_id_capture.lineage
nemo_gym.token_id_capture.lineage
Resolve the recorded call that a request continues.
A rollout can contain several model calls. Training consumes their exact tokens as one contiguous sequence. Request-time lineage identifies the earlier call that each request continues.
assistant_fingerprint is the lookup key.
It hashes model-authored turns and ignores user and tool content added between calls.
conversation_digest verifies the unchanged request context.
A digest mismatch rejects the claimed lineage before any parent tokens are reused.
The shared LineageStore resolves entries already committed by TokenSink.
FileLineageStore tails the token JSONL through the token store’s lock.
Each child receives its parent’s cumulative tokens.
Downstream inference consumes those tokens to supply the exact prompt prefix.
Every supported record distinguishes a root, a resolved parent, and an unresolved boundary. The builder uses token-prefix matching only when a verified parent is absent from the frozen snapshot. It never uses prefix matching to cross an unresolved boundary.
A delivered chain contains exactly the tokens the policy emitted over the recorded context. The hashes ignore reasoning and selected items that a harness may omit when it echoes model output. These differences do not change the captured token sequence. Ambiguous matches remain unresolved rather than risking tokens from the wrong call.
Module Contents
Classes
Functions
Data
API
Bases: IncrementalLineageStore
Resolve lineage from the token JSONL committed by TokenCaptureStore.
The reference IncrementalLineageStore backend: cursor = (inode, offset),
ref = byte offset, reads under the store’s shared flock so a committed
put is immediately visible.
Reference resolver for in-process framework backends and tests.
Production wiring uses FileLineageStore when a token store exists.
This class supports in-process framework adapters and tests.
Its index is memory-only.
Eviction or restart leaves affected continuations unresolved.
That failure mode is safe but can mask otherwise usable rollouts.
Production adapters should back the incremental resolver with durable records.
Publish one committed entry to the worker-local index.
Base class for lineage resolvers over any committed-entry backend.
An external backend implements two hooks. It inherits Gym’s matcher, bounded index, locking, and token materialization. Hash-for-hash agreement is the wire contract. The backend remains the source of truth when cache rows are evicted. A resolved match loads only the winning call’s token chain.
Insert or touch a cache row with LRU semantics.
Reinsert a touched row so dictionary order tracks recency. Eviction only requires a later backend refetch.
Load several committed entries.
Backends can override this hook to fetch a parent chain in one operation.
Load one RESOLVED parent’s cumulative tokens from the backend.
Read the chain in one batch and append each token segment once. Digest verification makes stale references fail closed.
Bound worker-local lineage by rollout and cumulative token counts.
This index backs the single-worker fallback. Shared stores provide cross-worker visibility. Eviction removes the oldest rollout. An evicted parent leaves later continuations unresolved and the builder masks them. The only live rollout is never evicted.
Release a rollout’s lineage early.
Gym’s model server has no rollout-completion signal. An in-process framework can call this when it retires the records.
Keep an append-only per-rollout call index.
Return whether this request extends the node’s recorded context.
The leading context_len items must match the recorded request.
A rewritten or summarized context fails verification.
Verification excludes the model response because dialects can echo it as different item counts.
Index lookup metadata carried by one committed token entry.
store_tokens=False keeps token arrays in the durable log.
Build an in-memory entry for direct index tests.
Return the immutable parent decision for this request.
A request without model-authored history is a root. A request with unverified history is unresolved. Never guess among calls with identical output.
Return the parent decision without touching token arrays.
Matching needs only fingerprints, digests, and lengths. The caller materializes tokens for the single winner.
Serialize JSON-compatible prompt content without losing structure.
Return typed content parts without discarding prompt-shaping blocks.
Tool calls are normalized separately by _tools_of.
Tool results are normalized separately by _tool_results_of.
Return whether the model produced this item.
Chat and Anthropic use the assistant role.
Responses tool calls are roleless function_call items.
Return tool result identities and payloads across dialects.
Responses stores results in standalone function_call_output items.
Anthropic stores results in tool_result content blocks.
Chat stores results as plain message content.
Return tool calls as (id, name, canonical arguments) tuples.
Chat stores calls in the message’s tool_calls field.
Anthropic stores calls in tool_use content blocks.
Responses stores each call as a standalone function_call item.
Hash one tagged, length-delimited UTF-8 field.
Fingerprint the model-authored turns of a request, in order.
The fingerprint identifies the call that produced the last model-authored turn. User and tool content is excluded from the lookup key. Dialect-specific tool-call shapes normalize to the same hash input.
Normalize a tool call’s arguments for comparison only.
Harnesses can reserialize tool-call arguments between turns. Comparison uses sorted-key JSON with normalized separators. The record retains the model’s original string.
Hash every turn of a conversation, model-authored or not.
assistant_fingerprint ignores user and tool content.
This digest covers that omitted context.
A mismatch rejects the parent before its tokens are reused.
Add compact lookup metadata before the token entry is committed.