Skip to content

feat(server): octop.extensions entry points for packages built on Octop - #3129

Merged
liukewia merged 2 commits into
TencentCloud:developfrom
alison-xx:feat/octop-extensions
Nov 24, 2026
Merged

liukewia merged 2 commits into
TencentCloud:developfrom
alison-xx:feat/octop-extensions

Conversation

@alison-xx

Copy link
Copy Markdown
Contributor

Summary

Today a product built on Octop can only be a fork. Adding sign-in routes, background tasks, extra tables, or public endpoints means patching Octop's own files, and those forks drift from develop quickly. I maintain one (a multi-bot desktop/mobile client on top of Octop). I would like to turn it into a package that depends on the octop wheel instead and sends generic fixes back here. This PR is the first, smallest piece: a loader plus lifecycle and HTTP hooks. I am opening it as a proposal, so feedback on the shape is very welcome before anything builds on it.

Mechanism. A package declares an entry point in the octop.extensions group. octop/infra/extensions.py loads every entry point once per process, in name order. Octop then calls each of these optional hooks only if the object defines it:

Hook Called from
async on_startup(server) End of OctopServer._boot_runtime
async on_shutdown(server) First step of OctopServer.stop, while the database is still open; runs in reverse order, and a failure in one does not stop the others
configure_app(app, server) In build_app, after the core routers and before the dashboard catch-all, so extension routes and static mounts are not shadowed
public_paths() -> PublicPaths Merged into is_jwt_exempt_path (exact / prefixes) and into setup_lockdown (setup_exact / setup_prefixes); every path must start with /api/

Failure handling. An extension that fails to import is logged and skipped. An exception in on_startup fails the start, so the product cannot run half-configured.

Scope. Harness tools, skills, middleware and channels keep using octop_harness.plugins. This group is only for things in the Octop server itself. docs/extensions.md documents the contract.

With no extension installed, nothing changes.

Possible follow-ups (separate PRs, only if this direction is acceptable):

  • extension-owned SQL migrations with their own watermark, so a product's tables never take Octop's next migration number;
  • repos(db) on RepoBundle;
  • a hook in _build_harness_config.

Target branch

  • Base is develop (feature / fix — default)
  • Base is main (release/* or hotfix/* only)

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactor / chore
  • Release / hotfix

Test plan

  • uv run pytest -m "not live" -n 8: 6240 passed, 46 skipped. make lint and mypy are clean.
  • Added/updated tests
  • New tests/unit/infra/test_extensions.py checks that:
    • a broken extension is skipped;
    • non-/api/ public paths are rejected;
    • a declared path is reachable before setup and without a JWT, while an undeclared extension route still gets 503 before setup and 401 after;
    • on_startup and on_shutdown run once each, with the database open.

Checklist

  • Updated CHANGELOG.md (if user-facing). Happy to add one once the shape is agreed.
  • README / docs updated (if needed): docs/extensions.md

Copy link
Copy Markdown
Collaborator

The extension-point design looks reasonable to me. Before merging, please fix partial startup failure cleanup.

If extension A starts successfully and extension B raises, _extensions_started is never set, so A's on_shutdown is not called. This is particularly problematic during first-time bind_control_plane(), where the HTTP process remains alive and can retain background tasks, connections, and partially initialized state.

Please track successfully started extensions and shut them down in reverse order on failure, restoring the failed bind to a retryable state. Simply setting the boolean earlier would also shut down extensions that never started.

Please add regression tests for “A succeeds, B fails” during both normal startup and first-time binding.

This finding is based on static review; I have not run a reproduction.

Products built on Octop (for example a branded client with its own sign-in
and extra tables) can only fork today: there is no way to add routes,
background tasks, or public paths without patching Octop's files, and
forks drift from upstream fast.

A package now declares an entry point in the "octop.extensions" group.
Octop loads them once per process and calls whichever optional hooks the
object defines: on_startup / on_shutdown around the runtime (shutdown runs
first in stop(), while the database is open), configure_app at the end of
build_app but before the dashboard catch-all, and public_paths for /api/
paths that skip the JWT or stay open before setup. A broken extension is
logged and skipped. Harness tools, skills and channels keep using harness
plugins. docs/extensions.md describes the contract.
If extension A started and B raised in on_startup, A's on_shutdown never
ran, and a failed first-run bind_control_plane() left the runtime half up
with database_bound already true, so a retry did nothing.

run_startup_hooks now returns the extensions whose startup finished and,
when one raises, shuts the started ones down in reverse order before
re-raising. stop() shuts down only those. Opening the control plane (in
start() and bind_control_plane()) tears the runtime down and closes the
database on failure, so the bind can be retried.
@alison-xx
alison-xx force-pushed the feat/octop-extensions branch from f1a7012 to d1ec066 Compare November 24, 2026 08:20
@alison-xx

Copy link
Copy Markdown
Contributor Author

Thanks, you're right. Fixed in d1ec066 (rebased on current develop).

Startup tracks what started. run_startup_hooks walks the extensions in load order and returns the names whose startup finished. An extension with no on_startup counts as started, so its on_shutdown still runs at stop. If one raises, the ones that already started get on_shutdown in reverse order, then the error propagates. stop() shuts down only that list, so an extension that never started is never shut down.

A failed boot is undone. start() and bind_control_plane() now share _open_control_plane(). If anything after opening the database fails (including an extension), it runs the same teardown as stop(): extensions, scheduler, cron, gateway, agents, users, then closes the database and clears services / app_runtime. database_bound is false again, so the setup wizard can retry the bind.

Tests (tests/unit/infra/test_extensions.py), with extensions A, B, C where B raises:

  • test_failed_first_bind_shuts_down_started_extensions_and_can_retry: greenfield start, first bind_control_plane() fails. A gets startup then shutdown, C gets nothing, the server is unbound. After B is fixed, the same server binds again, and stop shuts down C, B, A in that order.
  • test_failed_startup_shuts_down_started_extensions: a server whose database is already configured fails in start(). A is shut down and the runtime is cleared.

Both tests fail on the previous commit and pass now. Full backend suite: 6257 passed, 48 skipped. docs/extensions.md describes the rollback.

#3130 and #3131 are rebased on top of this.

@liukewia
liukewia merged commit 1ef67fd into TencentCloud:develop Nov 24, 2026
5 checks passed
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