Skip to content

fix(agent): avoid duplicate imports and expensive watcher scans - #1094

Merged
theomonnom merged 11 commits into
mainfrom
fix/python-agent-dev-startup
Nov 13, 2026
Merged

theomonnom merged 11 commits into
mainfrom
fix/python-agent-dev-startup

Conversation

@theomonnom

@theomonnom theomonnom commented Nov 2, 2026 •

Copy link
Copy Markdown
Member
  • Skip common dependency, cache, build, and coverage directories.
  • Honor Git-ignored directories, including custom Cargo output paths.
  • Use WalkDir to avoid statting every file.
  • Import the Python SDK once; show import hints from the worker process.
  • Avoid a separate Python process for dev-channel version detection.
  • Overlap watcher setup with Python startup.
  • Watch project source, including modules outside the entrypoint directory.
  • Preserve the 500 ms reload debounce.
  • Keep two focused regressions for single imports and watcher exclusions.

@theomonnom theomonnom changed the title fix(agent): avoid Python package gates and Cargo build scans fix(agent): validate Python imports and skip dev watcher artifacts Nov 2, 2026
@theomonnom theomonnom changed the title fix(agent): validate Python imports and skip dev watcher artifacts fix(agent): avoid duplicate imports and expensive watcher scans Nov 2, 2026
@theomonnom
theomonnom force-pushed the fix/python-agent-dev-startup branch from c1b5dc8 to a54548d Compare November 12, 2026 23:38

@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.

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)

Devin Review

Comment on lines +268 to 274
// 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
}

@devin-ai-integration devin-ai-integration Bot Nov 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


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

@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.

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)

Devin Review

Comment on lines +52 to +56
if env := os.Getenv("UV_PROJECT_ENVIRONMENT"); env != "" {
if !filepath.IsAbs(env) {
env = filepath.Join(dir, env)
}
return venvPython(env)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


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

Comment on lines +58 to +62
for current := dir; ; current = filepath.Dir(current) {
for _, name := range []string{".venv", "venv"} {
if python := venvPython(filepath.Join(current, name)); python != "" {
return python
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


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

Comment on lines +75 to 78
if venv := os.Getenv("VIRTUAL_ENV"); venv != "" {
if python := venvPython(venv); python != "" {
return python, nil
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


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

@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 1 new potential issue.

Devin Review

Comment thread cmd/lk/app_credentials.go
Comment on lines +256 to +260
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 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.

Devin Review


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

@theomonnom
theomonnom merged commit a4c7138 into main Nov 13, 2026
27 checks passed
@theomonnom
theomonnom deleted the fix/python-agent-dev-startup branch November 13, 2026 00:13
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