Stop the `gh-aw` engine putting the checkout on `sys.path` ahead of installed packages
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Closes #12815
Why we make these changes
The pydantic-ai gh-aw engine put the checkout on sys.path while the agent's lazily imported dependencies were still to load, so a top-level module in the checkout could replace one. Its comments, README and docs said the checkout was added after the framework packages load.
- The wrapper's
PAI_AGENTmodule branch prepended the checkout. It now appends it, after installed packages, only when nothing installed has the target's top-level name. pydantic_clai2.cli.agent_import.import_agentappended the launch directory even for an already imported target, whichgh_aw_agentis in every engine mode. It now follows the same rule.- The rule, the same in both: append only when the target's top-level name is neither in
sys.modulesnor found byimportlib.util.find_spec. A top-level lookup imports nothing. - Accepted cost: a checkout target whose top-level name is also installed, as a module, a regular package or a namespace directory, resolves against the installed package instead, so it usually fails with
ModuleNotFoundError. Onmain, the wrapper loaded such a target from the checkout, which came first;import_agentresolved it against an installed module or regular package, and loaded it beside an installed namespace directory. - Once the checkout is appended, an installed module or regular package wins over it, but a namespace package nothing has imported yet can lose to a checkout package. That happens only for a target in the checkout, which is the agent's own code.
- The
import_agentdocstring, the CLAI--agentreference and customization guide, the gh-aw README anddocs/harness/gh-aw.mddescribe the rule and its cost.
New public surface
none
User-visible behavior
Where the checkout sits on the runtime sys.path of an engine run:
| mode | before | after |
|---|---|---|
default Coder | appended | absent (once engine.version pins a pydantic-clai2 with this change) |
| spec file | appended | absent (same) |
| installed target | first | absent |
target in the checkout (my_agent:agent) | first | appended after installed packages |
| checkout target whose top-level name is also installed | first | absent; the target resolves against the installed package, usually ModuleNotFoundError |
Verification
TestLauncherProgram::test_checkout_modules_do_not_shadow_installed_onesruns the real launcher in all four modes over a checkout holding a module named after a stdlib module and one nothing installs. It fails onmainin every mode.- A target only the launch directory has loads: clai2, wrapper.
- An importable target leaves
sys.pathalone:test_import_agent_leaves_sys_path_alone_for_an_importable_module. - A checkout target whose top-level name an installed regular package or namespace directory also has fails with the existing error, and nothing of that package is loaded before the real import: clai2, wrapper.
What changes for existing users
Module PAI_AGENT workflows importing @main get the wrapper change on their next recompile; a checkout target whose top-level name is also installed then resolves against the installed package. Default and spec modes change once the next release's pin bump moves engine.version, because the engine installs pydantic-clai2 from PyPI. clai2 --agent with an importable module no longer appends the launch directory, so an installed agent that imports a module found only in the launch directory at run time no longer finds it. A launch-directory module under a top-level name an installed namespace directory also has is no longer found.
PR decisions
2026-11-07 · launch-directory append kept for unimportable targets · iter -
- Decision: import_agent and the wrapper append cwd only when find_spec(top-level) is None; an importable target leaves sys.path unchanged
- Why: Keeps the documented my_agent:agent flow; an installed agent that imports a cwd-only module at run time would no longer resolve it (judged acceptable: clai2 unlaunched)
- Source: https://github.com/pydantic/pydantic-ai/issues/12815
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 1 of 3 at 1ecf52936cb500b7d013f70149ee202a6ac4c5dd · iter -
- Decision: dispatched (reserved)
- Why: tree 832cfd4c277d3c51c13346e9dab6bdfda1c33208; scope 58b007e6c33b48b739035a56dc0f1f3849debb50..1ecf52936cb500b7d013f70149ee202a6ac4c5dd full branch; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/gh-aw-engine-checkout-imports
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 1 of 3 at 1ecf52936cb500b7d013f70149ee202a6ac4c5dd · iter -
- Decision: remediated: namespace-package targets append the launch directory again (regression #1); wrapper uses the same spec lookup; docs and launcher comment describe only the wrapper, since pinned clai2 0.66.0 still appends in default/spec modes until the next engine.version bump
- Why: reviewed tree 832cfd4c277d3c51c13346e9dab6bdfda1c33208; scope 58b007e6c3..1ecf52936c; policy base 6cd17b80f9
- Source: https://github.com/pydantic/pydantic-ai/tree/gh-aw-engine-checkout-imports
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 2 of 3 at 6a79e042eca104ed9da60a2aef868d1a4de8bcc4 · iter -
- Decision: dispatched (reserved)
- Why: tree c7e00fa5d9c32bca6d4ab0eef9bfe6bdf4e604fa; follow-up scope 1ecf52936c..6a79e042ec plus review-1 findings
- Source: https://github.com/pydantic/pydantic-ai/tree/gh-aw-engine-checkout-imports
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 2 of 3 at 6a79e042eca104ed9da60a2aef868d1a4de8bcc4 · iter -
- Decision: review-1 findings verified fixed; new finding (spec-less imported module such as main still appends cwd) dismissed as net-neutral
- Why: reviewed tree c7e00fa5d9c32bca6d4ab0eef9bfe6bdf4e604fa; same as main, which always appends; only main is spec-less in practice, import_agent is internal, and the wrapper errors on main:agent anyway
- Source: https://github.com/pydantic/pydantic-ai/tree/gh-aw-engine-checkout-imports
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · importability is decided on the whole module name · iter -
- Decision: import_agent and the wrapper call find_spec on the full target (parents imported, as the import would) and append the launch directory only when the target or a parent package is missing; supersedes the top-level spec and namespace-origin check
- Why: a regular package anywhere on sys.path takes precedence over namespace portions, so appending for an installed namespace target let a launch-directory package replace it; reproduced on 6a79e04 by two new tests
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4503564110
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 3 of 3 at a04f1fbab9ad541c3661f8aee09121774f2ea202 · iter -
- Decision: dispatched (reserved)
- Why: tree 76ccbca2166cdaebb183df18fd02eca54e81bf6e; follow-up scope 6a79e042ec..a04f1fbab9 plus the veria finding
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · pre-push review 3 of 3 at a04f1fbab9ad541c3661f8aee09121774f2ea202 · iter -
- Decision: veria finding verified fixed; regression #2 found (installed namespace directory named like the target, regular module in the checkout); David chose: fix (namespace spec counts as absent), push, no 4th review
- Why: reviewed tree 76ccbca2166cdaebb183df18fd02eca54e81bf6e; regression confirmed by stdlib probe (main resolves, a04f1fb misses agent, proposed rule resolves); review budget exhausted
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4503564110
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · forget probe-imported namespace parents on fallback · iter -
- Decision: when the launch directory/checkout is appended, namespace parent packages that find_spec imported (absent from sys.modules before) are removed from sys.modules, in import_agent and the wrapper
- Why: a cached installed namespace parent kept a same-named regular package in the launch directory from taking precedence, as it does on main; reproduced on 4868581 by the regular-package cases of both namespace tests
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · fourth local review at 31aee2050d (David) · iter -
- Decision: David authorized a fourth local pre-push review beyond the 3-review budget, run by an OpenCode reviewer on 4868581c5b..31aee2050d; 31aee20 is held unpushed until its findings are validated
- Why: the review budget was spent and the macroscope fix changes runtime behavior; the push is David's call
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · fourth local review (OpenCode) at 31aee2050d: findings validated · iter -
- Decision: claim 2 (three-level target, installed namespace pkg + installed regular pkg.sub, checkout regular pkg) confirmed real against the PR's design: fails on 31aee20/4868581/a04f1fb and 1ecf529, loads on main and 6a79e04; claim 1 (installed regular parent) is intended behavior, same as main's import_agent; held for David's call, 31aee20 still unpushed
- Why: repros run with python -P through import_agent and the generated wrapper at each revision; namespace-portion test cases already pass on 4868581 (pre-existing guards), only regular-package cases exercise 31aee20
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-07 · non-importing importability lookup (David) · iter -
- Decision: David chose: replace the find_spec probe in import_agent and the wrapper with a lookup through sys.meta_path that imports nothing, walking each parent spec's submodule_search_locations; delete the forgetting step entirely; accepted cost: legacy pkgutil-style namespace packages are treated as missing, so the launch directory is appended after installed packages for them
- Why: fourth regression (three-level target, installed regular pkg.sub cached by the probe) came from the probe importing parents; a lookup that runs no module code removes the class
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
- Supersedes: forget probe-imported namespace parents on fallback
2026-11-07 · reword --agent launch-directory sentences (David) · iter -
- Decision: David chose: reword the import_agent docstring, clai2 reference.md (--agent) and customization.md to state that an installed package with the same top-level name wins over the launch directory; keep the gh-aw README and docs unless the new lookup changes what they describe
- Why: they said a module in the launch directory resolves without installing it, which an installed package of the same top-level name prevents (main behaves the same)
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · non-importing lookup: nested namespace packages · iter -
- Decision: when PathFinder raises KeyError(parent name) for a nested namespace package under an unimported parent, the lookup builds the namespace spec itself, with the directories named like the package in the parent's locations as portions; any other KeyError re-raises
- Why: PathFinder wraps a namespace result in _NamespacePath, which reads the parent from sys.modules; without this the namespace-parent-and-target shapes raised KeyError in both copies (found by repro before commit); zip-file namespace portions are not seen
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · one last fix round at 2da4d92e54 (David) · iter -
- Decision: David chose "one last fix round": fix the Python 3.15 ModuleNotFoundError in the nested-namespace fallback, skip sys.meta_path finders without find_spec, and qualify the docs (import-hook installs; parents whose init changes path, zip namespace portions); stop condition: "if the next validator finds anything blocking, this PR stops for re-scoping"
- Why: a Claude validator and the OpenCode review found the same three issues independently on 2da4d92e54; the 3.15 one breaks the namespace-parent cases on CI's 3.15 leg
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · last fix round committed at 87d3af667e · iter -
- Decision: both lookup copies: second except arm for ModuleNotFoundError(name=parent) on Python 3.15; skip sys.meta_path finders without find_spec (hasattr); next(filter(None, specs)) keeps complexity at 14; docs limit precedence to packages installed on sys.path (an import-hook install such as setuptools editable can lose) and say the lookup may miss a module, such as one under a package whose init.py changes path or in zip namespace portions
- Why: a Python 3.11 find_module-only finder is skipped while 3.11's import still asks it, so such a target counts as missing; noted in the code comment only, covered by the docs' 'such as' wording; held unpushed for the 3.11-3.15 validator
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · validator cleared 87d3af667e (David's last fix round) · iter -
- Decision: Claude validator CLEARED 87d3af667e: no blocking defect on 3.11, 3.12, 3.13, 3.14, uv 3.15.0 and Homebrew 3.15.1; non-blocking notes (a) 3.11 find_module-only finder skipped, (b) test finder's return None runs only on 3.11, (c) editable-install example, need no change; manager relayed: push
- Why: matrix of 43 shapes through import_agent and the wrapper matched main except the intended differences; lookup imports nothing; new tests fail on 2da4d92e54 and pass on 87d3af667e; the stop condition did not trigger
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · pre-push gate at 87d3af667e (David) · iter -
- Decision: push 87d3af667e without a further pre-push-review dispatch: the 3-review budget is spent, and David authorized this round's local reviews (OpenCode at 31aee2050d and 2da4d92e54, Claude validators at 2da4d92e54 and 87d3af667e) in place of further pre-push reviews
- Why: HEAD 87d3af667e equals the validator-cleared commit, tree 07c74216921b0de085a951b138261d2ffa260205; worktree clean; fast-forward of origin 4868581c5b; merges cleanly with upstream/main 0f7648a1b0
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · Macroscope thread closed at 87d3af667e (David) · iter -
- Decision: Macroscope's finding (probe-imported installed namespace parent kept a launch-directory regular package from resolving) addressed by David's non-importing lookup, shipped in 2da4d92e54 and 87d3af667e; replied with the commits and reproducing tests, reacted +1, resolved
- Why: the regular-package cases of both namespace tests reproduce it on 4868581c5b and pass at 87d3af667e
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4504802231
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · coverage repair at 056dd21343da0d740076e0e41caa5b146d715c97 (exempt delta) · iter -
- Decision: test-only: package_modules_at_import in tests/clai2/test_agent_flag.py records every importlib.import_module call, dropping a name check whose false branch never ran in the clai2 suite; pushed without another review
- Why: exempt delta 'missing coverage / test-harness repair' chained from reviewed 87d3af667e: git diff 87d3af667e..HEAD touches only tests/clai2/test_agent_flag.py; CI run 42441758558 failed coverage on test_agent_flag.py 132->134; the file passes on 3.11 and 3.14
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/42441758558
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · douwebot finding at 056dd21343: top-level-only rule (pending David) · iter -
- Decision: PENDING David's decision, no reply, push, resolve or code change meanwhile: douwebot REQUEST_CHANGES asks to replace the non-importing lookup in both copies with "append the launch directory only when the target's top-level name can't be found at all" (find_spec(top_level) is None), and to simplify the docs and the namespace/finder tests to match; it reverses David's non-importing-lookup decision, so it falls under his stop condition
- Why: douwebot's stated cost: a checkout target whose top-level name is installed as a module, regular package or namespace directory fails with module not found; history: this rule was the PR's first design (entry 'launch-directory append kept for unimportable targets'), and pre-push review 1 at 1ecf52936c reported its namespace-package-target case as regression #1 against main
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4504921209
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · top-level rule adopted (David) · iter -
- Decision: David chose: adopt douwebot's top-level rule in import_agent and the wrapper: "append the launch directory to the end of sys.path only when the target's top-level name is neither in sys.modules nor found by importlib.util.find_spec(top_level)"; delete _find_spec_without_importing and _find_spec_on_meta_path from both copies; one plain docs sentence replaces the pkgutil/path/zip/import-hook caveats; accepted cost: a launch-directory target whose top-level name is also installed (a module, a regular package or a namespace directory) fails with the existing module-not-found error, the case review 1 flagged as regression #1
- Why: douwebot review 5816490323 on 056dd21343; a top-level lookup imports nothing, so the probe-import regressions and the nested-namespace fallback go away
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4504921209
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
- Supersedes: non-importing importability lookup (David); non-importing lookup: nested namespace packages; last fix round committed at 87d3af667e; douwebot finding at 056dd21343: top-level-only rule (pending David)
2026-11-08 · top-level rule committed at 47821c74b6 (held for validator) · iter -
- Decision: import_agent and the wrapper: append the launch directory only when the target's top-level name is neither in sys.modules nor found by importlib.util.find_spec; _find_spec_without_importing/_find_spec_on_meta_path deleted; import_agent keeps main's ModuleNotFoundError name matching and cwd-not-in-sys.path guard, the wrapper keeps main's bare import (no guard; it resolves once); namespace, finder and nested tests removed; collision tests parametrized over regular package and namespace directory; unpushed until a Claude validator clears 3.11-3.15
- Why: CI commands (no -n/--dist/shards) pass on 3.11.17, 3.14.8 (all-extras, harness, clai2 cells) and 3.15.1; coverage 100% for both test files and agent_import.py; new tests fail on main's sources (9 of 9); two timing tests (MCP 5 s init, 30 s command timeout) failed only in full-file runs at load 8-20 and pass alone on 47821c74b6 and 056dd21343 sources
- Source: https://github.com/pydantic/pydantic-ai/pull/12818#discussion_r4504921209
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · validator cleared 47821c74b6 (top-level rule) · iter -
- Decision: Claude validator CLEARED 47821c74b6: no findings in 1224 runs on 3.11-3.15; #12815 security property holds; coverage 100%; manager relayed: apply note 1 (wrapper and TestLauncherProgram comments: an installed module or regular package wins, a namespace package nothing has imported yet can lose to a checkout package) and note 2 (docs: such a target resolves against the installed package instead, so it usually fails with ModuleNotFoundError) as a separate wording commit without re-validation; skip note 3 (empty target name fails on both); then push
- Why: differences from main are only the accepted cost and intended shapes; note 2: an installed namespace dir with a target-named directory gives AttributeError, and an installed package with the same dotted module loads silently (main same)
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
2026-11-08 · pre-push gate at b57f6a19b5 (David) · iter -
- Decision: push 47821c74b6 and b57f6a19b5 without a further pre-push-review dispatch: the Claude validator cleared 47821c74b6 and the manager relayed that b57f6a19b5 (comment and docs wording, notes 1 and 2) needs no re-validation
- Why: HEAD b57f6a19b5, tree 0aec3094a41ece5e65c9aefbfa2a77cd06d8e0fc; fast-forward of 056dd21343; merges cleanly with upstream/main 0bb132ff7c; pre-commit passed; docs tests for the touched pages pass on 3.14; test_headless_agent_loads_no_plugins fails when run after tests/clai2/test_clai_docs.py etc. on both HEAD and main's sources (pre-existing order dependence, reported as a sibling)
- Source: https://github.com/pydantic/pydantic-ai/pull/12818
- Session: 0388fc60-70d9-4077-ac03-93c3974ac70a
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.