Skip to content

refactor(integrations): generate the shared Python plugin helpers from one source - #2318

Merged
hidb4ai merged 2 commits into
oceanbase:masterfrom
Fengzdadi:refactor/python-lifecycle-module
Nov 8, 2026
Merged

hidb4ai merged 2 commits into
oceanbase:masterfrom
Fengzdadi:refactor/python-lifecycle-module

Conversation

@Fengzdadi

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

Part of #2250 (plan step 3, first item: the Python module and the generate/check command). Moving the Claude Code and Codex hooks onto the module comes in later PRs.

Rationale for this change

Claude Code, Codex, WorkBuddy, and Hermes install their plugin directories without the PowerContext package, so each plugin ships its own copies of up to four helpers: client configuration, prepared context, prompt privacy, and bootstrap context. Each helper's copies are byte-identical, 12 files in all, and tests/test_integration_shared_files.py fails when one of them drifts, but a change to a helper still has to be repeated in every copy.

This PR keeps one source for the helpers and generates the copies from it, as #2250 plans. No plugin code moves: each plugin keeps importing its copy from the same path.

The source is a top-level package, src/powercontext_integration_lifecycle/, as proposed in the follow-up comment on #2250. Under powercontext.client, importing the module would run powercontext/client/__init__.py, which imports httpx. The website's Python API reference would also publish it, and its normalize_server_url would sit next to an SDK function of the same name that behaves differently. Like src/powercontext_service_bootstrap, the top-level package imports with the standard library alone. It still ships in the wheel, so framework adapters can import it later; this PR adds no such import. Moving it later changes paths only.

What changes are included in this PR?

  • src/powercontext_integration_lifecycle/ holds client_config.py, prepared_context.py, prompt_privacy.py, and bootstrap_context.py, byte-identical to the current copies. The package is added to the wheel's packages. Ruff checks it and every copy with the py39 target, as it already checks the Claude Code plugin, because the Claude Code copies often run on the macOS system Python 3.9. With the default py311 target, Ruff's automatic fixes (fix = true) would rewrite a Codex, WorkBuddy, or Hermes copy alone, for example timezone.utc to datetime.UTC, and the copy would no longer match its source.

  • scripts/generate_integration_lifecycle.py writes each copy to the path its plugin imports it from today. Below the license header, each copy gets two comment lines that name its source and make integration-lifecycle.

    Source Copies
    client_config.py powercontext_client_config.py in Claude Code, Codex, and Hermes; hooks/powercontext_client_config.py in WorkBuddy
    prepared_context.py hooks/prepared_context.py in Claude Code, Codex, and WorkBuddy
    prompt_privacy.py hooks/prompt_privacy.py in Claude Code, Codex, and WorkBuddy
    bootstrap_context.py hooks/bootstrap_context.py in Claude Code and Codex
  • With --check, the script fails when a copy is missing or differs from its source. It also fails when a plugin has a file named like a copy that the generator does not write, such as a new hand-made copy. It finds those files with git ls-files, so ignored build output and installed dependencies do not count.

  • make integration-lifecycle writes the copies. make integration-lifecycle-check runs the check and is part of make check. AGENTS.md gets a short section after "Generated API Contract": edit the source instead of the copies, and keep it standard-library only and runnable on Python 3.9.

  • .github/ci-paths.toml selects the tests and hermes suites when the generator changes. The source is under src/, which already selects every suite. tests/test_ci_changes.py covers the new rule.

  • Tests:

    • tests/test_integration_lifecycle.py runs the generator's check, so the unit test jobs still catch a stale copy, as they do today through the shared-files test. It also fails when a source module imports anything outside the standard library, or imports a sibling module: Claude Code runs its hooks with the python3 on PATH, Hermes installs no dependency for its plugin, and each copy sits alone in its plugin.
    • tests/test_recall_query_bound.py, from fix(integrations): bound the recall query in Pi, OpenCode, DSH, and Bub #2252, imports the source module instead of loading the Claude Code copy.
    • tests/test_integration_shared_files.py keeps only shared/prompt-privacy.mjs and src/transport.ts, which have no generator yet.

Not included:

  • client/transport_policy.py, which feat(integrations): keep host plugins thin and make them work behind the gateway #2250 also plans to fold into the module. It depends on pydantic and validates URLs and settings differently from client_config, so folding it in changes behavior. It needs its own PR.
  • Helpers whose copies differ between hosts, such as hooks/diagnostics.py and the Scope clients. They need the host declarations that come with moving the hook flows.

Are there any user-facing changes?

No. Each copy gains only a blank line and the two comment lines, so plugin behavior, file layout, and installation are unchanged. The wheel gains the top-level package powercontext_integration_lifecycle. Nothing in powercontext imports it, and the Python API reference does not list it.

For contributors, a shared helper is now edited in src/powercontext_integration_lifecycle/, and make integration-lifecycle updates its copies. make check and the unit tests reject an edited copy.

How was this change tested?

  • uv run pytest tests/test_integration_lifecycle.py tests/test_integration_shared_files.py tests/test_recall_query_bound.py tests/test_hook_characterization.py tests/test_ci_changes.py: 181 passed. The Claude Code and Codex characterization fixtures from test(integrations): characterize the Claude Code and Codex hooks #2255 pass unchanged.
  • uv run pytest -n 6 tests/claude_code_plugin tests/codex_plugin --require-claude-code-python-floor: 390 passed and 4 skipped, which test_client_transport.py skips by design. The Python 3.9 tests ran on the macOS system Python 3.9.6.
  • On Python 3.9.6 with -I -S, the package and its four modules import, and no powercontext module is loaded.
  • uv build --wheel: the wheel contains powercontext_integration_lifecycle/ with its five files.
  • make integration-lifecycle-check reported a deleted Hermes copy as missing, an edited Codex copy as drifted, and an untracked bootstrap_context.py in the WorkBuddy plugin as a file the generator does not write, and tests/test_integration_lifecycle.py failed on the same tree. With the files restored, both passed.
  • For the source and each of the 12 copy paths, ruff check --select UP017 --stdin-filename <path> accepted a module that uses timezone.utc. For the Codex plugin's own settings.py, which is not a copy, it still reported UP017.
  • uv run prek run -a: every hook passes except ty check, whose 4 diagnostics are in files this PR does not change and appear on master 0c1dfc28 as well: the SeekDBGraphStore calls in tests/builtin/test_code_publication.py, and socket.SO_PEERCRED, which macOS lacks.
  • make -k check: integration-lifecycle-check and the other targets pass, except upgrade-policy-check, which fails on master 0c1dfc28 as well ("Persistence changed (file set or content; CRLF/LF normalized): update the compatibility review"; this PR leaves the persistence digest unchanged), and workflow-actions-check, which runs prek and fails on the same ty diagnostics. Without -k, make check stops at upgrade-policy-check, so until that is fixed the quality job runs neither integration-lifecycle-check nor prek and ty. The unit test jobs still run the copy check through tests/test_integration_lifecycle.py.
  • make unit-test: 7012 passed, 333 skipped, and 19 failed. The 19 fail on master 0c1dfc28 on the same machine, and none is in a file this PR changes: 4 in tests/test_upgrade_release_gate.py, 3 in tests/builtin/test_code_publication.py, 3 in tests/builtin/persistence/test_mysql_vector_admission.py, and 9 in tests/test_source_management.py.
  • make e2e-test: 1190 passed, 513 skipped, and 5 failed. The 5, in tests/e2e/test_source_collect.py and tests/e2e/test_source_deployment.py, fail on master 0c1dfc28 as well, because Source background processes require Linux.
  • make docs-test: passed.
  • feat(integrations): keep host plugins thin and make them work behind the gateway #2250 asks each step-3 PR to run the real host and to add shell tests built from recorded native payloads. This PR changes no host code beyond comments in the copies, so I did not run any host. The PRs that move the Claude Code and Codex flows will do both.

AI usage statement

This PR was prepared with Claude Code (Claude Opus 5.5). It wrote the change and the tests, ran the checks, and reviewed the change for correctness. I reviewed the change and the results.

…m one source

Claude Code, Codex, WorkBuddy, and Hermes install their plugin directories
without the PowerContext package, so each ships its own copy of the client
configuration, prepared-context, prompt-privacy, and bootstrap-context
helpers. Twelve copies were kept identical by hand, and a test could only
report that one had drifted.

The helpers now live once in src/powercontext_integration_lifecycle/, a
top-level package like src/powercontext_service_bootstrap. Importing it
loads nothing from the SDK, and the Python API reference does not list it.
The wheel includes it. Ruff checks it against Python 3.9, because the
Claude Code copies often run on the macOS system Python, and checks every
copy the same way, so that a fix Ruff applies to a copy cannot make the
copy drift.

`make integration-lifecycle` writes every copy where its plugin already
imports it, marked as generated. `make check` fails when a copy is missing
or differs from its source, or when a plugin keeps a same-named copy that
the generator does not write. A test runs the same check, so the unit test
jobs still catch a stale copy, as they did with the shared-files test. Git
decides which plugin files count, so ignored build output and installed
dependencies are not mistaken for copies. Another test keeps the source
standard-library only and free of sibling imports: Claude Code runs its
hooks with the python3 on PATH, Hermes installs no dependency for its
plugin, and the copies do not keep the package layout.

The copies gain only the generated-file comment, so plugin behavior, layout,
and installation are unchanged, and the Claude Code and Codex
characterization fixtures pass as recorded. The recall-query bound test now
exercises the source module. The shared-files test keeps the JavaScript
prompt-privacy module and the TypeScript transport, which have no generator
yet.
Copilot AI balanced review requested due to automatic review settings October 31, 2026 01:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Copilot AI balanced review requested due to automatic review settings October 31, 2026 12:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed commit 62f23f2. Centralizing the four existing helpers and generating their twelve plugin copies is a reasonable, behavior-preserving way to prevent drift while keeping each plugin independently installable. The new wheel package is internal shared code; there are no new database tables or public HTTP/MCP/SDK contracts. No blocking findings.

@knqiufan knqiufan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the shared helper generation and its integration consumers. No blocking correctness findings; focused tests passed.

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

Labels

1.5.1 v1.5.1 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants