Skip to content

feat(agent): lk agent restart, rollback, promote, delete and secrets through the Public API when signed in - #1140

Open
Topherhindman wants to merge 10 commits into
topherhindman/agent-user-auth-readsfrom
topherhindman/agent-user-auth-writes
Open

Topherhindman wants to merge 10 commits into
topherhindman/agent-user-auth-readsfrom
topherhindman/agent-user-auth-writes

Conversation

@Topherhindman

@Topherhindman Topherhindman commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Under --user-auth, the agent writes that the Public API serves now go through AgentService in place of the token bridge: lk agent restart, rollback, promote, delete, update-secrets and update. The bridge needs Agent.Admin, so before this only a project admin could make any of them. Now a WRITE member can restart, roll back and promote. Delete and secrets still need a project admin, and the refusal says so.

Stacked on #1139.

Commands

Command Under --user-auth Who
agent restart RestartAgent WRITE
agent rollback --version RollbackAgent WRITE
agent promote --deployment PromoteAgent (to production) WRITE
agent delete, delete --deployment DeleteAgent ADMIN
agent update-secrets UpdateAgentSecrets (merge) ADMIN
agent update UpdateAgentSecrets with its secrets, or RestartAgent with none ADMIN / WRITE
agent update-secrets --overwrite bridge ADMIN, with the bridge's refusal
create, deploy (source and image), runtime logs, private-link * bridge ADMIN
list, status, config, versions, secrets, dockerfile, build logs AgentService (#1138, #1139) READ (secrets: ADMIN)

What's in it

  • feat(public): RestartAgent, RollbackAgent, PromoteAgent, DeleteAgent, UpdateAgentSecrets, plus FieldViolations (the google.rpc.BadRequest detail) and IsAgentBuilderAgent. A secret's value goes out as base64, as protojson sends bytes, and its kind goes out by name.
  • feat(agent): userAuthAgentAPI calls them and maps the errors.

Decisions

  • Secrets merge. AgentService never overwrites. update-secrets without --overwrite maps to set. lk has no flag to remove a secret today. The request's remove field maps to AgentService's remove, but nothing in lk sets it.
  • --overwrite stays on the bridge. It replaces every secret, which AgentService doesn't do. A non-admin gets the bridge's "this command needs a project admin" refusal.
  • lk agent update. Under --user-auth lk sends the secrets to UpdateAgentSecrets (merge), and with no secrets calls RestartAgent. Both roll the agent, as cloud agents' UpdateAgent does with an API key, but the upstream calls differ: cloud agents' UpdateAgent also saves the agent and marks its production deployment Updating, with or without secrets; RestartAgent only touches the agent so that it reconciles, and UpdateAgentSecrets saves the merged secrets without marking it Updating.
  • Errors. A refusal for a role that is too low names the role once ("this command needs write access to the project", or "… a project admin"); the Public API's own text follows in parentheses only when it says more, such as the permission a server that checks permissions names. An invalid secret gets a line by name: secret 1KEY: name must have only letters, …. A write to an Agent Builder agent names the lk command to use, with the AB_ ID from the refusal's ResourceInfo detail: lk agent builder deploy AB_… --version … for rollback and promote, lk agent builder delete AB_… for delete, and lk agent builder update-secrets AB_… for secrets. A restart says the API can't restart it. Each invalid-secret line prints once. When the role is too low, the hint says a project API key also works: "pass --legacy-auth", as chore(auth): prepare for migration to user-auth as default #1156's hints do. lk words other refusals ("version not found") as the Public API does.

Output differences vs the API-key path

  • With an API key too, a failed lk agent restart now starts with unable to restart agent:, as every other agent command's error does. This is intentional.
  • The success lines are the same. That includes the existing "Deleted agent agent [CA_1]" doubled word, which this PR leaves alone.
  • A secret named LIVEKIT_* is the same in both modes. lk drops LIVEKIT_URL, LIVEKIT_API_KEY and LIVEKIT_API_SECRET before it sends (ignoredSecrets, cmd/lk/agent.go). The Public API passes any other name to cloud agents (livekit/public-api-server#242), which drops the names that it sets on each agent itself (its SecretExclusionList) without an error and keeps the rest.
  • A leftover Agent Builder cloud agent (an Agent Builder create whose undo failed, so it has no Agent Builder agent): with an API key, cloud agents refuses lk agent delete. Under --user-auth, the Public API deletes it, as the dashboard does (livekit/public-api-server#242).
  • An Agent Builder agent: with an API key, rollback and promote go through. Under --user-auth they are refused, by design in livekit/public-api-server#242.
  • A rollback to a version that doesn't exist, or whose build failed: under --user-auth this is "version not found" or "version is not available: …". With an API key, cloud agents answers without a code.

Open questions

  • Should update-secrets --overwrite become native, as a list of the names plus a merge with remove? That is not atomic, which is why this PR keeps the bridge.
  • RestartAgent's REST route has body: "*", but its request has only path fields, so the generated client sends no body. Vanguard reads an empty body as an empty message. This has not run against a real PAS yet.

Blockers

  • pkg/public generated from livekit/public-api-server@372fb27e (production v2026-12-02-2258).

Testing

  • GOPRIVATE= go build ./..., go vet ./cmd/lk/... ./pkg/..., go test ./cmd/lk/... ./pkg/... and golangci-lint run ./cmd/lk/... ./pkg/public/... pass.
  • New testscript agent-writes-with-user-auth: each command, once with an API key against fake cloud agents and once under --user-auth against the fake Public API, prints the same success line. That covers delete whole and per deployment, and update with and without secrets. --overwrite still gets the bridge's refusal. The request log shows no token request for the native writes.
  • New testscript agent-writes-refused-with-user-auth: the role refusals for restart (WRITE), delete and secrets (ADMIN), invalid secrets by name, an Agent Builder agent, and a plain NotFound.
  • Unit tests: the request of each write (method, path, query, body), base64 values that protojson decodes back, field violations, IsAgentBuilderAgent, what update-secrets and update send, --overwrite reaching only the bridge, secretsError and agentWriteError.

Review fixes

  • LIVEKIT_ secrets (582fe2f): the Public API no longer refuses a LIVEKIT_ name, so agent-writes-with-user-auth sends LIVEKIT_AGENT_NAME in both modes and checks that LIVEKIT_URL is dropped in both, and the refusal script, TestSecretsError and TestFieldViolations use violations that the Public API still sends. Same checks as above: pass.
  • The writes script now proves each flag reaches the Public API. The script fake's requests -v prints each URL's query and each JSON body, with every secret value replaced by REDACTED, and agent-writes-with-user-auth uses it: delete --deployment, rollback --version, promote's deployment, and the secret names of update-secrets and update. Dropping any of them in userAuthAgentAPI now fails the script (checked by mutation).
  • userAuthAgentAPI.UpdateAgent's comment no longer says it does what cloud agents' UpdateAgent does; it says how the calls differ (see Decisions).

Demo

Recorded on staging, not uploaded yet: ~/hosted-agents-work/demos/M3/m3-agent-writes.mp4 (write-up ~/hosted-agents-work/demos/M3/README.md). Local Public API from this branch against staging cloud-agents (staging database read-only); lk from #1140. READ is refused a restart; WRITE restarts and rolls back; WRITE is refused secrets and delete; ADMIN sets a secret, is refused a reserved LIVEKIT_ name, and deletes the agent. The reserved-name refusal predates livekit/public-api-server#242's change: that update now succeeds, as it does with an API key. Promote isn't shown: the staging plan allows no extra deployment.

Depends on #1136 (the hints name lk agent builder).

…of agents

The Public API client makes AgentService's writes on a cloud agent:
restart, rollback (to a version, or the previous one when none is
named), promote a deployment to production, delete the agent or one
deployment, and set and remove secrets. A secret's value goes out as
base64, as protojson sends bytes, with its kind by name.

IsAgentBuilderAgent reads the refusal to roll back, promote or delete
an Agent Builder agent. An invalid secret's google.rpc.BadRequest
detail is read with the FieldViolations that main already has.
…ough the Public API

Under --user-auth, these commands now call the Public API's
AgentService in place of the token bridge, so a member can make the
writes that their role allows:

- lk agent restart, rollback and promote: a WRITE member.
- lk agent delete, whole or --deployment: a project admin.
- lk agent update-secrets: a project admin. It merges, as without
  --overwrite it does with an API key. lk has no flag to remove a
  secret; the request's remove names map to AgentService's remove.
- lk agent update: its secrets merge as update-secrets does, then the
  agent restarts, as cloud agents' UpdateAgent does. With no secrets it
  only restarts the agent.

update-secrets --overwrite, which replaces every secret, stays on the
bridge: AgentService only merges. The bridge needs a project admin and
says so.

AgentService's refusals read as lk's own. A member whose role is too
low hears which role the command needs, not that they lack access to
the project. An invalid secret gets a line by name. A rollback, promote
or delete of an Agent Builder agent points at Agent Builder in the
dashboard.
Every other agent command starts its error with "unable to <verb> agent:";
restart returned the error as it came, with an API key and under
--user-auth alike.
"this command needs a project admin (this needs ADMIN on the project)"
said the role twice. lk now quotes the Public API's refusal only when it
says more than the role lk names, such as the permissions a server that
checks them lists ("this needs agent:delete on the project").
The script fake logged only each request's method and path, so the
writes script passed with delete's --deployment, rollback's --version,
promote's deployment and update-secrets' secrets dropped. `requests -v`
also prints each URL's query and each JSON body, with secret values
replaced, and the writes script uses it.
…loud agents' UpdateAgent

The comment said that update under --user-auth does what cloud agents'
UpdateAgent does. It calls AgentService's UpdateAgentSecrets, or
RestartAgent with no secrets: both roll the agent, but cloud agents'
UpdateAgent also saves the agent and marks its production deployment
Updating.
… key

The Public API no longer refuses a secret name because it starts with
LIVEKIT_ (livekit/public-api-server#242): it passes the name to cloud
agents, which drops the names that it sets on each agent itself, as it
does for lk with an API key. lk still drops LIVEKIT_URL, LIVEKIT_API_KEY
and LIVEKIT_API_SECRET before it sends, in both modes.

The writes script now sends LIVEKIT_AGENT_NAME in both modes and checks
that LIVEKIT_URL is dropped in both. The refusal script, TestSecretsError
and TestFieldViolations use violations that the Public API still sends.
The Public API now refuses to restart an Agent Builder agent or change
its secrets, as it refuses a rollback, promote or delete, with
FailedPrecondition "agent is an Agent Builder agent; manage it in Agent
Builder". lk agent restart, update and update-secrets under --user-auth
now word that refusal as rollback, promote and delete do, and the
wording covers every write: "manage it in Agent Builder", then the
Public API's message.
…mmand to use

AgentService refuses a write to an Agent Builder agent with
FailedPrecondition, a message that names the AgentBuilderService RPC to
call instead, and a ResourceInfo detail with the Agent Builder agent's
ID. lk now names the lk agent builder command that makes the write,
with that ID, in place of the dashboard:

- rollback and promote: lk agent builder deploy AB_ID --version VERSION_ID
- delete: lk agent builder delete AB_ID
- update-secrets, and update with secrets: lk agent builder
  update-secrets AB_ID
- restart, and update without secrets: the API can't restart it

The Public API's words stay in the error that lk wraps. The script's
refusals are the Public API's own texts for each RPC.
The Public API sends a violation for each field that is wrong, so a
secret named twice in one request, or a violation it sends twice, made
lk print the same line more than once. secretsError now prints each
line once, in the order of the first violation that makes it.
@Topherhindman
Topherhindman force-pushed the topherhindman/agent-user-auth-writes branch from 1fc4d1f to b6948db Compare December 3, 2026 00:32
@Topherhindman
Topherhindman marked this pull request as ready for review December 3, 2026 00:57

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 3 potential issues.

3 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment thread cmd/lk/agent_user_auth.go
Comment on lines +346 to +347
if err := a.client.DeleteAgent(ctx, a.projectID, req.GetAgentId(), req.GetDeployment()); err != nil {
return nil, agentWriteError(err, req.GetAgentId(), projectAdmin, builderDelete)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Whole-agent deletion suggested for deployment

When Agent Builder refuses a deployment-only delete, DeleteAgent recommends deleting the whole agent. Following that advice with session auth and confirming deletes every saved version as well.

Learn more

The deleteAgent command uses --deployment to distinguish deleting one deployment from deleting the whole agent. The Public API delete operation documents a refusal when Agent Builder manages the agent. On that refusal, the CLI uses the same recovery hint for both scopes. The Builder command provided by the companion PR deletes the agent, its versions, and its deployment, as documented by AgentBuilderServiceDeleteAgent.

Example: An admin requests deletion of staging from CA_1. The API refuses because the agent is managed as AB_1. If the admin follows the Builder delete hint with --user-auth and confirms, the operation removes AB_1 and all its versions, not just staging.

Recommended fix: Use a deployment-specific refusal when req.GetDeployment() is nonempty. Do not suggest a whole-agent delete in that case. Add a refusal script that sends --deployment staging and checks that the error does not suggest Builder deletion.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread cmd/lk/agent_user_auth.go
Comment on lines +375 to +377
func builderSecrets(id string) string {
return "; update its secrets with `lk agent builder update-secrets " + id + "`"
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Missing Agent Builder secrets command

When Agent Builder refuses a secret update, builderSecrets points to lk agent builder update-secrets. The companion Builder CLI does not register that command, so the suggested recovery fails.

Learn more

Both UpdateAgentSecrets and UpdateAgent use the shared secrets write path for a merge. On an Agent Builder refusal, the path prints a Builder subcommand. The dependency branch origin/topherhindman/devx-973-builder-commands has no update-secrets subcommand, even though AgentBuilderServiceUpdateAgentSecrets is available in the REST client. The refusal script asserts only the error text, so it cannot catch the unavailable command.

Example: An admin runs lk --user-auth agent update-secrets --id CA_1 --secrets API_TOKEN=example and receives a hint to run lk agent builder update-secrets AB_1. Even with --user-auth, the companion CLI has no such subcommand.

Recommended fix: Until the Builder secrets command is implemented and included in the merge dependency, give a supported recovery path, such as the Agent Builder dashboard.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread pkg/public/agents.go
Comment on lines +229 to +234
func (c *Client) RollbackAgent(ctx context.Context, projectID, agentID, version string) error {
body := oapi.AgentServiceRollbackAgentJSONRequestBody{}
if version != "" {
body.Version = &version
}
resp, err := c.gen.AgentServiceRollbackAgentWithResponse(ctx, projectID, agentID, body)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Empty-version rollback sends invalid request

Calling RollbackAgent with an empty version sends {} instead of rolling back to the previous version. The rollback request requires an explicit version.

Learn more

The wrapper advertises that an empty version rolls back to the previous one. The generated request contract instead requires a version from the agent's version list. The empty-version test returns a canned success and verifies the empty request, not the server's required field.

Example: A caller requests a rollback with version == "" for an agent running v2 with v1 available. The client sends {} rather than naming v1, so the documented rollback mode does not work against the REST contract.

Recommended fix: Require a nonempty version in the wrapper, document that requirement, and replace the canned success test with a test that verifies an empty version is rejected before sending a request.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread cmd/lk/agent_user_auth.go
Comment on lines +334 to +339
if deploymentName(dstDeployment) != productionDeployment {
return bridgeError(a.bridge.PromoteAgent(ctx, agentID, srcDeployment, dstDeployment))
}
if _, err := a.client.PromoteAgent(ctx, a.projectID, agentID, srcDeployment); err != nil {
return agentWriteError(err, agentID, projectWriter, builderDeployVersion)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. Can't we fix the API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants