Skip to content

Lua API Reference

Rust bindings exposed to Lua workflows. See crates/moss/src/workflow/lua_runtime/.

Stability

APIs are marked with stability levels:

  • Stable - Safe to depend on. Breaking changes require deprecation.
  • Experimental - Works but interface may change between releases.
  • Internal - Implementation details. Do not use in user scripts.

What's Stable

CategoryStableExperimentalInternal
Moss commandsview, analyze, grepedit, index, lint-
Helpersshell, read_file, write_file, file_exists, printis_dirty, tests_pass-
Memorystore, recall, forget--
Shadowshadow.*shadow.worktree.*-
Tree-sitterts.parse, node methods--
LLMllm.complete, llm.chat--
Agent-agent.run, agent.classify_tasksubmodules
Globals_moss_rootargs, task-

Agent Submodules (Internal)

The agent.* submodules are implementation details:

  • agent.parser - Command parsing, JSON encode/decode
  • agent.session - Checkpoints, logs, session management
  • agent.context - Context builders for LLM prompts
  • agent.risk - Risk assessment and validators
  • agent.commands - Batch edit execution
  • agent.roles - Role prompts and state machine config

These are internal to agent.lua and may change without notice.

Moss Commands (Stable)

Wrappers around moss CLI commands, run as subprocesses.

FunctionStabilityDescription
view(opts)StableView code structure. opts: {target, deps, context, depth}
analyze(opts)StableAnalyze code. opts: {health, complexity, target}
grep(opts)StableSearch code. opts: {pattern, path, file_type}
edit(arg?)ExperimentalEdit files
index(arg?)ExperimentalManage index
lint(arg?)ExperimentalRun linter
plans(arg?)ExperimentalManage plans
sessions(arg?)ExperimentalView sessions

Helpers (Stable)

FunctionStabilityDescription
shell(cmd)StableExecute shell command. Returns {output, success}
file_exists(path)StableCheck if path exists relative to project root
read_file(path)StableRead file contents as string
write_file(path, content)StableWrite string to file
print(...)StablePrint values to stdout
is_dirty()ExperimentalCheck if git working tree has uncommitted changes
tests_pass()ExperimentalRun cargo test --quiet, return success boolean

Memory Store (Stable)

Semantic memory with vector search (SQLite + embeddings).

FunctionDescription
store(content, opts?)Store content. opts: {context, weight, metadata}
recall(query, limit?)Search by similarity. Returns {id, content, context, similarity}[]
forget(id)Delete entry by ID

Shadow Git (Stable)

Lightweight snapshots for rollback without polluting git history.

FunctionStabilityDescription
shadow.open()StableInitialize, returns current HEAD commit
shadow.snapshot(files)StableCreate snapshot of file list, returns snapshot ID
shadow.hunks()StableGet current uncommitted hunks
shadow.hunks_since(id)StableGet hunks since snapshot ID
shadow.restore(id, files?)StableRestore snapshot (optionally only specific files)
shadow.head()StableGet current HEAD commit
shadow.list()StableList all snapshots as {id, message}[]

Hunk structure: {id, file, old_start, old_lines, new_start, new_lines, header, content, is_deletion, deletion_ratio}

Shadow Worktree (Experimental)

Isolated editing environment for validation before applying changes.

FunctionDescription
shadow.worktree.open()Create/open worktree, returns path
shadow.worktree.sync()Reset worktree to HEAD
shadow.worktree.edit(path, content)Edit file in worktree
shadow.worktree.read(path)Read file from worktree
shadow.worktree.validate(cmd)Run validation command. Returns {success, stdout, stderr, exit_code}
shadow.worktree.diff()Get diff of changes
shadow.worktree.apply()Apply changes to real repo, returns file list
shadow.worktree.reset()Discard changes
shadow.worktree.modified()List modified file paths
shadow.worktree.enable()Route edit() through shadow worktree
shadow.worktree.disable()Stop routing through shadow
shadow.worktree.enabled()Check if shadow edit mode is active

Tree-sitter (Stable)

FunctionDescription
ts.parse(source, grammar)Parse source string with grammar name, returns tree userdata

Tree methods:

  • tree:root() - Get root node

Node methods:

  • :kind() - Node type as string
  • :text() - Source text for this node
  • :start_row(), :end_row() - Line numbers (1-indexed)
  • :child_count() - Number of children
  • :child(i) - Get child by index (1-indexed)
  • :children() - All children as table
  • :named_children() - Named children only
  • :child_by_field(name) - Get child by field name
  • :is_named() - Whether node is named vs anonymous
  • :parent() - Parent node or nil
  • :next_sibling(), :prev_sibling() - Sibling navigation

LLM (Stable)

Requires --features llm build.

FunctionDescription
llm.complete(provider?, model?, system?, prompt)Single-turn completion
llm.chat(provider, model, system?, prompt, history)Multi-turn chat with history

Arguments:

  • provider: "anthropic", "openai", "gemini", "openrouter", etc.
  • model: Model name, or nil for provider default
  • system: System prompt, or nil
  • prompt: User prompt (required)
  • history: Table of {role, content} messages (for chat)

Example:

lua
-- Single completion
local response = llm.complete(nil, nil, "You are helpful.", "What is 2+2?")

-- Multi-turn chat
local history = {
    {role = "user", content = "Hello"},
    {role = "assistant", content = "Hi there!"}
}
local response = llm.chat("anthropic", nil, nil, "How are you?", history)

Agent (Experimental)

FunctionDescription
auto(config)Run autonomous agent loop. config: {model, prompt, max_turns}

Preloaded Modules (Stable)

Available via require():

ModuleStabilityDescription
cliStableCLI argument parsing
typeStableType definitions and schemas
type.describeStableGenerate descriptions from types
type.validateStableValidate values against types
type.generateStableGenerate test values from types
testStableTesting utilities
test.propertyStableProperty-based testing
agentInternalAgent implementation (use CLI instead)
agent.*InternalAgent submodules (parser, session, context, risk, commands, roles)

Globals

NameStabilityDescription
_moss_rootStableProject root path as string
argsExperimentalCommand-line arguments table (1-indexed)
taskExperimentalTask description from --task flag