Paneflow runs Lua 5.4 in two places. `paneflow do` runs a script in its
own process, from your terminal or from an agent's shell tool. The
worker, the background process of every Paneflow home, loads `init.lua`
and runs the actions it declares when you pick them in the command
palette or when a script or an agent asks for one.

Both reach the same registry the CLI uses. Every method is a Lua
function named after it: `surface.list` is `paneflow.surface.list`, and
`action.invoke` is `paneflow.action.invoke`. A script can call many
methods and return one small result, so an agent spends one shell call
where it would otherwise spend one tool call per method.

**TL;DR for agents.** Run `paneflow api list` to see every method and
`paneflow api describe <method>` for its parameters. Run a chunk with
`paneflow do -e 'return paneflow.surface.list()'`. List the actions you
may run with `paneflow actions`, and run one with
`paneflow do <name> key=value --wait 20s`. Exit code `4` with
`running <run_id>` means the action still runs: continue with
`paneflow do --resume <run_id> --wait 20s`.

## How do I run a script?

`paneflow do` takes a Lua file, a chunk after `-e`, or `-` to read the
script from standard input. A file is anything that ends in `.lua` or
contains a path separator; any other word is the name of an action (see
below). The value the script returns is printed as one line of JSON.

```lua
-- panes.lua
local listed = paneflow.surface.list()
return { panes = #listed.surfaces, who = paneflow.args.who }
```

```text
$ paneflow do panes.lua who=arthur
{"panes":2,"who":"arthur"}

$ paneflow do -e 'return #paneflow.surface.list().surfaces'
2

$ echo 'return paneflow.args.n * 2' | paneflow do - n=21
42
```

The `key=value` pairs after the script fill `paneflow.args`, typed by
their spelling: `n=2` is a number, `on=true` a boolean, anything else a
string. `--args` takes raw JSON instead, or `@path` to read it from a
file, and `--json` indents the result.

```text
$ paneflow do -e 'return paneflow.args' n=2 on=true env=prod
{"env":"prod","n":2,"on":true}

$ paneflow do --json -e 'return {n = paneflow.args.n}' --args '{"n": 3}'
{
  "n": 3
}
```

A script started from a pane hears the events of that pane's workspace,
or of every workspace with `--all`. `--for 30s` stops it after that
long, `--memory-limit <mib>` caps the memory of the Lua state, and
`--deadline <seconds>` stops Lua code that runs that long without
waiting. `paneflow.stop(n)` ends the script with exit code `n`, and
Ctrl-C exits `130`.

The standard library is Lua's, with two exceptions: `io.popen` and
`os.execute` are refused, because `paneflow.exec` runs a program from an
argument list, never through a shell. `require` finds modules beside the
script and in the `lua` folder of the Paneflow home.

```text
$ paneflow do -e 'return io.popen("ls")'
Error: (command line):1: io.popen is not available in Paneflow scripts: run programs with paneflow.exec, which takes an argument list and never starts a shell
```

## What happens when a script fails?

A Lua error prints `Error: <message>`, with the file and the line, and
exits `1`.

```lua
-- hello.lua
return "hello " .. paneflow.args.name:upper()
```

```text
$ paneflow do hello.lua name=arthur
"hello ARTHUR"

$ paneflow do hello.lua
Error: hello.lua:1: attempt to index a nil value (field 'name')

$ paneflow do -e 'error("deploy refused: tests are red")'
Error: (command line):1: deploy refused: tests are red

$ paneflow do -e 'return 1 +'
Error: (command line):1: unexpected symbol near <eof>
```

A method that fails does not raise. It returns `nil`, the message and
the JSON-RPC error code, so the script decides what to do:

```text
$ paneflow do -e 'local result, message, code = paneflow.surface.read{ surface_id = 999 } return { message = message, code = code }'
{"code":-32000,"message":"Surface not found: no pane 999 on this host"}
```

A script is a client like the CLI: writing into a pane passes the same
gates as `paneflow send`, described in
[How do I write safely?](/docs/scripting#how-do-i-write-safely).

## How do I declare actions in init.lua?

`init.lua` lives in the Paneflow home: `~/.paneflow/init.lua`, or
`%USERPROFILE%\.paneflow\init.lua` on Windows. The worker loads it at
start and again each time you save it. Each `paneflow.action{...}`
declares one action: a lowercase `name`, a `title` the command palette
shows, the types of its `args`, and the `run` function.

```lua
-- ~/.paneflow/init.lua
paneflow.action{
  name = "greet",
  title = "Greet",
  args = { name = "string" },
  run = function(ctx, args)
    return { greeting = "hello " .. args.name, origin = ctx.origin }
  end,
}

paneflow.action{
  name = "build",
  title = "Build the project",
  description = "Wait for a build to finish",
  agents = true,
  args = { seconds = "number?" },
  run = function(ctx, args)
    paneflow.sleep(args.seconds or 12)
    return "build finished"
  end,
}
```

`build` stands in for a long job here: it only sleeps. An argument type
is `string`, `number`, `integer`, `boolean`, `object`, `array` or
`any`, optional when it ends in `?`, or a JSON Schema table. Arguments
are checked against the types before `run` starts. `ctx.origin` says
what started the run: `palette`, `key`, `cli`, `agent`, and so on.

```bash
paneflow config check ~/.paneflow/init.lua
paneflow config reload
paneflow actions
```

```text
$ paneflow config reload
/home/you/.paneflow/init.lua: loaded (2 actions, 0 bindings, 0 handlers, 0 timers)

$ paneflow actions
greet  Greet  [Actions]  init.lua:1
build  Build the project  [Actions]  init.lua:10
```

`paneflow config check <file>` loads a file in its own process and
prints its problems with file and line, so a mistake never reaches the
worker. A failed reload keeps the previous configuration.
`paneflow actions --json` prints each action with its argument schema,
its `agents` flag and where it is declared.

## How do I run an action?

`paneflow do <name>` runs the action on the worker and prints what
`run` returned. A wrong argument fails before `run` starts.

```text
$ paneflow do greet name=arthur
{"greeting":"hello arthur","origin":"cli"}

$ paneflow do greet
Error: paneflow error -32050: missing argument "name"; the action takes name

$ paneflow do greet name=arthur extra=1
Error: paneflow error -32050: no argument named "extra"; the action takes name

$ paneflow do nope
Error: paneflow error -32602: no action named 'nope'; paneflow actions lists the actions init.lua declares
```

A run is stopped after 30 seconds. Time spent waiting for the user in
`paneflow.ui.input` or `paneflow.ui.pick` does not count.

```text
$ paneflow do build seconds=40
Error: paneflow error -32051: timeout: the action build was stopped after 30 s
```

To try an action without touching the worker's configuration, run it
from the file in your own process:
`paneflow do --action greet ./init.lua name=arthur`.

## How do I keep a long action from blocking my shell?

Without `--wait`, `paneflow do <name>` waits until the action ends. An
agent's shell tool has its own timeout, so pass `--wait <duration>`:
the CLI waits at most that long. When the action ends first, its result
is printed and the exit code is `0`. When it still runs, the CLI prints
`running <run_id>` and exits `4`, and the action keeps running on the
worker.

```text
$ paneflow do build seconds=1 --wait 10s
"build finished"

$ paneflow do build --wait 5s
running b322557838e3d024cc35e884b915b67a

$ paneflow do --resume b322557838e3d024cc35e884b915b67a --wait 20s
"build finished"
```

`--resume <run_id>` keeps waiting, for at most its own `--wait` when
you pass one. A run's result is returned once; resuming it again fails:

```text
$ paneflow do --resume b322557838e3d024cc35e884b915b67a
Error: paneflow error -32602: no action run with id b322557838e3d024cc35e884b915b67a; its result was already returned or it never existed
```

`--wait` applies to actions only. A script runs in the `paneflow do`
process until it ends, so `paneflow do panes.lua --wait 5s` fails with
`--wait applies to an action of init.lua, run by its name; a script runs
in this process until it ends`.

## Which actions can an agent run?

An action runs for an agent only when it declares `agents = true`. The
worker recognizes an agent from the process that calls it: a
`paneflow do` started in a pane by a coding agent Paneflow knows, such
as Claude Code or Codex, runs as that agent, whatever origin the call
declares. For an
agent, an action without `agents = true` does not exist: it fails with
the same message as an unknown name.

```text
Error: paneflow error -32602: no action named 'greet'; paneflow actions lists the actions init.lua declares
```

An agent runs at most 3 actions in a burst, then 1 per second. Past
that, `paneflow do` fails with `rate limited` and the agent retries a
second later. You run every action, from your terminal, a key or the
command palette.

## How do I ask the user from a script or an action?

`paneflow.ui.input` asks for a line of text and `paneflow.ui.pick` for
a choice. Paneflow shows the question in every window that can show
it, names the action or the script that asks, and the first answer
wins. Each returns the answer, or `nil` and the reason nobody answered:
`cancelled`, `timeout`, `no_client` (no window is open), `unsupported`,
`busy` or `failed` with a detail.

```lua
paneflow.action{
  name = "new_branch",
  title = "New branch",
  agents = true,
  run = function()
    local name, reason = paneflow.ui.input{ title = "Branch name", value = "feature/" }
    if not name then return { cancelled = reason } end
    return { branch = name }
  end,
}

paneflow.action{
  name = "deploy",
  title = "Deploy",
  run = function()
    local target, reason = paneflow.ui.pick{
      title = "Deploy to",
      items = { "staging", { id = "prod", title = "Production", icon = "server" } },
    }
    if not target then return { cancelled = reason } end
    return { deployed = target }
  end,
}
```

```text
$ paneflow do new_branch
{"branch":"feature/login"}

$ paneflow do deploy
{"deployed":"prod"}
```

With no window open, the same runs end at once:

```text
$ paneflow do new_branch
{"cancelled":"no_client"}
```

A pick item is a string, which is its own id and title, or a table with
`id`, `title`, `subtitle`, `icon` and `detail`. With `multiple = true`
the answer is the list of the ids the user ticked, in the order of
`items`; with `allow_other = true` the user may type text that matches
no item, and the answer is `{ text = ... }`. A question waits 300
seconds by default; set `timeout`, at most 3600. Both functions work in
an action, an event handler, a timer or a `paneflow do` script, but not
while `init.lua` loads.

```text
$ paneflow do -e 'return paneflow.ui.pick{ title = "Deploy targets", items = { "staging", "prod", "dev" }, multiple = true }'
["staging","prod"]
```

From an agent's shell, `paneflow ask` asks the same kind of question
without a script; see
[How do agents message each other and ask you?](/docs/scripting#how-do-agents-message-each-other-and-ask-you).

## How do I find the methods a script can call?

`paneflow api list` lists every method the running processes serve,
with the process that owns it. `paneflow api describe <method>` shows
its parameters, its result fields and an example call. The same names,
with a dot, are the Lua functions.

```text
$ paneflow api list | grep '^action\.'
action.list               worker  List the actions init.lua declared, with their titles, argument schemas and sources.
action.invoke             worker  Run an action of init.lua with arguments and return what it returned. Arguments are checked against its schema first; a run is stopped after 30 seconds. With wait_ms, a run still going past it answers {status: running, run_id} for action.wait. With ctx.origin agent, only an action declared with agents = true runs.
action.wait               worker  Keep waiting on an action run that action.invoke returned as {status: running, run_id}: the run's result with status done, the same running status, or the action's error.

$ paneflow api describe action.wait
action.wait
  Keep waiting on an action run that action.invoke returned as {status: running, run_id}: the run's result with status done, the same running status, or the action's error.

  owner: worker   gate: none   reply: ready

Parameters:
  run_id   string                required  default -
  wait_ms  integer >= 0          optional  default -         Milliseconds to wait, 0 by default, at most 25000

Result fields:
  result  any
  status  "done"|"running"|"rate_limited"
  run_id  string

Example:
  paneflow api call action.wait run_id=6f1c0e9a2b7d4c3e8f5a1b2c3d4e5f60 wait_ms=20000
```

`paneflow api list --json` and `paneflow api describe --json` print the
full descriptors. `paneflow api call <method> key=value` calls one
method without Lua, and `paneflow api stubs` prints a definition file
for a Lua language server, so your editor completes every
`paneflow.*` function.

## Related

- [Scripting and automation](/docs/scripting): the CLI, the gates and
  the agent verbs.
- [Scripting reference](/docs/scripting/reference): every verb, method
  and exit code.