# Approvals and questions

> Answer the agent's approval requests and questions, at once through a callback or later after suspending.

When the permission rules say a call needs a person, or the model asks the
user a question, the session needs an answer. A host chooses how it gets
one.

## Answer at once

`WithInputCallback` hands each request to your function while the
execution waits. A terminal or a chat UI asks its user there and returns
the answer:

```go
// core/lifecycle_example_test.go
deploy, err := tool.Define("Deploy", "Deploys the named service.", func(_ context.Context, in struct {
	Service string `json:"service"`
}) (tool.Result, error) {
	return tool.Text("deployed " + in.Service), nil
})
if err != nil {
	panic(err)
}
p, err := replay.New(provider.Caps{},
	replay.ToolCalls(history.ToolCall{ID: "d", Name: "Deploy", Input: json.RawMessage(`{"service":"web"}`)}),
	replay.Reported("met", "Deployed web."))
if err != nil {
	panic(err)
}
ctx := context.Background()
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithTools(deploy),
	// The default mode asks before any action no rule allows.
	zelk.WithPermissionMode(permission.ModeDefault),
	zelk.WithInputCallback(func(_ context.Context, r zelk.InputRequest) (event.AnswerInput, error) {
		if r.Kind != event.InputApproval {
			return r.Cancel(), nil
		}
		fmt.Println("approve", r.Approval.Tool+"?")
		return r.AllowOnce(), nil
	}))
if err != nil {
	panic(err)
}
defer a.Close(ctx)
s, err := a.Session(ctx, "ops-1")
if err != nil {
	panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Deploy web.")))
if err != nil {
	panic(err)
}
res, err := x.Wait(ctx)
if err != nil {
	panic(err)
}
fmt.Println(res.Text)
// Output:
// approve Deploy?
// Deployed web.
```

An approval is bound to the action's digest: the tool, its exact input, the
working directory and the policy's revision. It approves exactly what was
asked, never a call that differs. The request builds every answer:
`AllowOnce`, `AllowForSession` with rules it suggested, `AllowAlways`,
`AllowEdited` with changed input, `Deny` with a note, `Answer` for a
question, and `ApprovePlan` or `RejectPlan` for a plan.

## Suspend and answer later

A server can't hold a request open for hours. With `WithInputSuspend`, the
session records its requests and the execution ends with
[`ErrSuspended`](/reference/go/zelk/#variables). Answer whenever you like,
from any process that shares the store, and the next `Start` carries the
answer:

```go
// core/lifecycle_example_test.go
deploy, err := tool.Define("Deploy", "Deploys the named service.", func(_ context.Context, in struct {
	Service string `json:"service"`
}) (tool.Result, error) {
	return tool.Text("deployed " + in.Service), nil
})
if err != nil {
	panic(err)
}
p, err := replay.New(provider.Caps{},
	replay.ToolCalls(history.ToolCall{ID: "d", Name: "Deploy", Input: json.RawMessage(`{"service":"web"}`)}),
	replay.Reported("met", "Deployed web."))
if err != nil {
	panic(err)
}
ctx := context.Background()
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithTools(deploy),
	zelk.WithPermissionMode(permission.ModeDefault), zelk.WithInputSuspend())
if err != nil {
	panic(err)
}
defer a.Close(ctx)
s, err := a.Session(ctx, "ops-2")
if err != nil {
	panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Deploy web.")))
if err != nil {
	panic(err)
}
res, err := x.Wait(ctx)
if !errors.Is(err, zelk.ErrSuspended) {
	panic(err)
}
req := res.Requests[0]
fmt.Println("suspended for", req.Kind, "of", req.Approval.Tool)

// Later: answer and resume.
x, err = s.Start(ctx, zelk.WithCommands(zelk.Respond(req.AllowOnce())))
if err != nil {
	panic(err)
}
res, err = x.Wait(ctx)
if err != nil {
	panic(err)
}
fmt.Println(res.Text)
// Output:
// suspended for approval of Deploy
// Deployed web.
```

The answer is recorded once. It binds the call it was given for, and a
second answer, or one that arrives after the request expired, changes
nothing. Whatever happened in between, the approved call is not asked about
again.

Whether it runs once is a different question, and the tool answers it. If the
process dies while the approved call is running, the next one to open the
session finds the call started and not ended. It runs the call again only
when that is safe: for a read-only, idempotent tool, or for one that
deduplicates by its idempotency key, which zelk gives the same on every
attempt. Any other call is reported to the model as unknown, with an
instruction to check what it would have changed before retrying. A tool
that changes data and must not run twice declares
`tool.WithEnforcedIdempotency` and acts at most once per key itself, as
[Custom tools](/go/tools/) describes; [Stores, leases and
recovery](/go/stores/#recovery) has the rule for each effect.

`WithInputTimeout` expires a request left unanswered too long.

## Nobody to ask

`WithInputDeny` refuses every request: approvals are denied and questions
go unanswered, so a headless job does only what the rules allow. It is the
default, but a session that didn't choose it records a warning, so the
choice is never silent. In `auto` mode, a reviewer model decides what would
otherwise ask, and sends only what it is unsure of to a person
([Permissions](/concepts/permissions/#auto-mode)).

## Next

[Permissions from Go](/go/permissions/) is how rules and modes decide which
calls need an answer in the first place.
