Repository navigation
fix(agent): avoid duplicate imports and expensive watcher scans - #1094
Conversation
c1b5dc8 to
a54548d
Compare
There was a problem hiding this comment.
Note
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| // Python validates imports in the worker itself; do not import the SDK | ||
| // in a throwaway preflight process. Node still checks its SDK baseline. | ||
| if cfg.ProjectType.IsNode() { | ||
| if err := checkNodeSDKVersion(cfg); err != nil { | ||
| return nil, err | ||
| } | ||
| } else if err := checkPythonSDKVersion(cfg); err != nil { | ||
| return nil, err | ||
| } |
There was a problem hiding this comment.
🔴 Unsupported Python agent exits successfully
With a Python SDK older than 1.6.0, startAgent launches an agent lacking the required thin CLI. The agent exits immediately, but runAgentStart returns success.
Learn more
Python agent launches use python -m livekit.agents with a subcommand and entrypoint through buildAgentCommand. The removed version check rejected installed SDKs below 1.6.0 because they do not provide that thin CLI. Without it, process creation succeeds, then the Python process fails before registering. runAgentStart waits for exit but does not return the process error, so the command finishes successfully.
Example: A project has livekit-agents 1.5.x installed. lk agent start now launches its interpreter, the module rejects the unsupported CLI invocation, and lk exits with status zero instead of reporting the incompatible SDK.
Recommended fix: Retain the 1.6.0 compatibility gate without importing the SDK in a second interpreter. Check installed distribution metadata by an import-free path, or have the worker validate and report its SDK version before the command treats startup as successful.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Note
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 3 new potential issues.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| if env := os.Getenv("UV_PROJECT_ENVIRONMENT"); env != "" { | ||
| if !filepath.IsAbs(env) { | ||
| env = filepath.Join(dir, env) | ||
| } | ||
| return venvPython(env) |
There was a problem hiding this comment.
🔴 Workspace agents miss relative custom environments
For a workspace member, projectVenvPython resolves relative UV_PROJECT_ENVIRONMENT against the member instead of the workspace root. The agent uses another interpreter and cannot load its installed dependencies.
Learn more
A uv workspace shares its environment at the workspace root. A relative UV_PROJECT_ENVIRONMENT path is relative to that root, but dir can be a member project root from detectProject. Joining the path to dir checks the wrong location. When that interpreter is absent, FindPythonBinary falls through to another environment or system Python, and the agent loses its installed dependencies.
Example: A workspace at /work contains member /work/agents/bot. With UV_PROJECT_ENVIRONMENT=envs/shared and an installed interpreter at /work/envs/shared/bin/python, the lookup checks /work/agents/bot/envs/shared/bin/python instead.
Recommended fix: Locate the uv workspace root before resolving a relative UV_PROJECT_ENVIRONMENT, and use that same root for interpreter discovery. Add a test that launches from a member directory with a relative custom environment.
Was this helpful? React with 👍 or 👎 to provide feedback.
| for current := dir; ; current = filepath.Dir(current) { | ||
| for _, name := range []string{".venv", "venv"} { | ||
| if python := venvPython(filepath.Join(current, name)); python != "" { | ||
| return python | ||
| } |
There was a problem hiding this comment.
🔴 Member environment shadows workspace dependencies
When a uv workspace member contains .venv, projectVenvPython picks it before the workspace environment. The agent then runs without dependencies installed in the shared environment.
Learn more
A uv workspace uses the environment of its workspace root. The agent directory can be a member root because detectProject stops at the nearest project manifest. This lookup walks from that member upward and returns the first interpreter it finds, even if the member contains an unrelated .venv. It therefore differs from the uv project environment used by the previous launch command.
Example: /repo/.venv holds livekit-agents for a uv workspace; /repo/member/.venv holds unrelated development tools. Launching an agent from /repo/member chooses the latter interpreter and cannot import livekit.agents.
Recommended fix: For uv workspace members, resolve the workspace root and select its configured environment before searching other nearby environments. Cover a member with its own .venv in a test.
Was this helpful? React with 👍 or 👎 to provide feedback.
| if venv := os.Getenv("VIRTUAL_ENV"); venv != "" { | ||
| if python := venvPython(venv); python != "" { | ||
| return python, nil | ||
| } |
There was a problem hiding this comment.
🔴 Active shell environment displaces uv agent
When an unrelated VIRTUAL_ENV is active, FindPythonBinary selects it instead of the uv project's environment. The agent fails to import dependencies installed only in the project.
Learn more
A uv project launched through uv run --no-sync runs in its project environment; uv ignores an active virtual environment that does not match that project unless explicitly told to use it. This lookup now accepts any existing VIRTUAL_ENV before checking the project environment. The CLI can therefore start an agent with an unrelated Python interpreter even though the project's dependencies are installed.
Example: /agents/.venv contains livekit-agents, but the user's shell has activated /other/.venv. Running lk agent start from /agents selects /other/.venv/bin/python and fails to import the SDK.
Recommended fix: Preserve uv project environment selection when VIRTUAL_ENV does not match the resolved project environment; retain active-environment precedence only where that is the intended project execution mode.
Was this helpful? React with 👍 or 👎 to provide feedback.
| if fileCreds.apiKey != "" && !namedProject(cmd) && target != "" && | ||
| (fileCreds.url == "" || sameProjectURL(fileCreds.url, target)) { | ||
| creds := agentCredentials{url: target, apiKey: fileCreds.apiKey, apiSecret: fileCreds.apiSecret} | ||
| path := filepath.Join(dir, file) | ||
| if keyAccepted(ctx, creds, path) { | ||
| out.Statusf("Using API key [%s] from %s", util.Accented(creds.apiKey), util.Accented(path)) | ||
| return creds, nil | ||
| } | ||
| fileCreds = agentCredentials{} | ||
| out.Statusf("Using API key [%s] from %s", util.Accented(creds.apiKey), util.Accented(filepath.Join(dir, file))) | ||
| return creds, nil |
There was a problem hiding this comment.
🔴 Wrong-project key prevents agent registration
When --url selects another project, agentCredentialsChain reuses a URL-less env-file key from the original project. The worker cannot authenticate, even when the selected project has a valid saved key.
Learn more
An agent's env file can contain a complete key pair without LIVEKIT_URL. When --url names another known project, target comes from the flag, but fileCreds.url == "" makes this branch return the file's key without resolving the target project. The worker receives a key for one project and a URL for another, so it cannot register. The same problem occurs when a higher-priority env file supplies the URL and a lower-priority file supplies the key. agentKeyForProject is bypassed, so the target project's saved key cannot be used.
Example: .env.local has project one's LIVEKIT_API_KEY and LIVEKIT_API_SECRET but no URL. lk --user-auth agent start --url ws://two.test sends that key to project two, instead of using its saved key.
Recommended fix: Do not treat a URL-less file key as belonging to an independently selected target. Resolve the target project when --url or another file supplies its URL, and use the file key only when its ownership is established; otherwise fall through to that project's saved or minted key.
Was this helpful? React with 👍 or 👎 to provide feedback.
WalkDirto avoid statting every file.