ion7-grammar / runtime.backtrack

class

Backtrack

local bt = Grammar.backtrack(ctx, vocab, sampler, {
    seq_id = session.seq_id,
    pos    = session.n_past,
})

bt:checkpoint("tbl")                -- save seq KV at this pos
bt:forward(function(p) return p:find("%s") end)  -- gen until space
bt:constrain("tbl", function(text)  -- validate; rollback if false
    return db:has_table(text:match("(%w+)$"))
end)
local result = bt:run()

Functions

Backtrack.new

Create a Backtrack session.

Backtrack.new(ctx, vocab, sampler, opts)
ctxanyion7-core Context.
vocabanyion7-core Vocab.
sampleranyion7-core Sampler (grammar-constrained).
optstable?

Backtrack:_gen_one

Backtrack:_gen_one(batch_idx)

Backtrack:_save_snapshot

Backtrack:_save_snapshot()

Backtrack:_restore_to

Backtrack:_restore_to(snap_idx)

Backtrack:checkpoint

Register a grammar symbol checkpoint. Saves a KV snapshot at the current position and associates it with a name.

Backtrack:checkpoint(symbol)
symbolstringSymbol name (e.g. "field_name", "table_ref").
→ numberToken index of this checkpoint.

Backtrack:forward

Generate tokens until a predicate returns true or max_tokens reached.

Backtrack:forward(predicate, max)
predicatefunction?Called after each token with (piece, all_text).
maxnumber?Override max_tokens for this call.
→ stringAll text generated so far.

Backtrack:backward

Backtrack to the last checkpoint for a symbol and resample.

Backtrack:backward(symbol, validator)
symbolstringSymbol name to rewind to.
validatorfunction?Called with (new_text) after resample.
→ booleantrue if accepted, false if max_retries exhausted.

Backtrack:text

Return the full generated text so far.

Backtrack:text()
→ string

Backtrack:is_done

Return whether generation is complete.

Backtrack:is_done()
→ booleandone
→ string?stop_reason "stop" | "length" | nil

Backtrack:last_checkpoint

Return the last checkpoint index for a symbol.

Backtrack:last_checkpoint(symbol)
symbolstring
→ number?

Backtrack:n_tokens

Number of tokens generated so far.

Backtrack:n_tokens()
→ number

Backtrack:constrain

Apply a semantic constraint at the current position (CRANE pattern). If the validator fails on the current text, rolls back to the last checkpoint for `symbol` and re-runs `forward` (using `opts.forward_pred` and `opts.max_forward`) before re-validating. This mirrors how `run()` uses constrain: checkpoint → forward → constrain, so retries must repeat the forward pass to produce a complete new candidate.

Backtrack:constrain(symbol, validator, opts)
symbolstringCheckpoint symbol to backtrack to on failure.
validatorfunctionfn(text) → boolean.
optstable?
→ boolean

Backtrack:run

Run a complete constrained generation loop with automatic backtracking.

Backtrack:run(steps)
stepstableArray of step tables:
→ stringtext Full generated text.
→ booleanok true if all constraints satisfied.