From d12188b75f6baa2f7a7c031429d2a4343f9bb4c5 Mon Sep 17 00:00:00 2001 From: Jon Scalet Date: Fri, 24 Apr 2026 19:23:11 -0700 Subject: [PATCH] fix: guard slice access in agent log streaming createAgent panics with 'index out of range [0] with length 0' when the CreateAgent response returns an empty ServerRegions and the user accepts the post-deploy 'view logs?' prompt. Fall back to the region already resolved earlier in the function. agentLogs has the same class of bug on AgentDeployments: Agents is length-checked but AgentDeployments is not. Return a clean error instead of panicking when an agent has no deployments. --- cmd/lk/agent.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/lk/agent.go b/cmd/lk/agent.go index fe373a56..25ff3c12 100644 --- a/cmd/lk/agent.go +++ b/cmd/lk/agent.go @@ -665,7 +665,11 @@ func createAgent(ctx context.Context, cmd *cli.Command) error { return err } else if viewLogs { fmt.Println("Tailing runtime logs...safe to exit at any time") - return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout, resp.ServerRegions[0]) + logRegion := region + if len(resp.ServerRegions) > 0 { + logRegion = resp.ServerRegions[0] + } + return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout, logRegion) } } return nil @@ -1014,6 +1018,9 @@ func getLogs(ctx context.Context, cmd *cli.Command) error { if len(response.Agents) == 0 { return fmt.Errorf("no agent deployments found") } + if len(response.Agents[0].AgentDeployments) == 0 { + return fmt.Errorf("agent has no deployments to stream logs from") + } return agentsClient.StreamLogs(ctx, cmd.String("log-type"), agentID, os.Stdout, response.Agents[0].AgentDeployments[0].ServerRegion) }