mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 23:26:38 +08:00
5.1 KiB
5.1 KiB
Cordis Architecture Alignment & Refactoring Design
Date: 2026-08-28
Topic: Cordis Meta-framework Alignment (Spatiotemporal Composability, Revertible Effects, Reactive Coeffects & Boundary Isolation)
Status: Approved
1. Background & Objectives
Wavelet adopts the Cordis micro-kernel paradigm (originating from Koishi and DeepSeek Harness) to achieve runtime composability and zero-side-effect lifecycle management. According to the formal metatheory of Cordis (A Programming Paradigm for Spatiotemporal Composability), the runtime must satisfy two orthogonal requirements:
- Temporal Composability (时间可组合性): Every context mutation/registration must track an inverse operation (Revertible Effects) and automatically roll back in LIFO order upon unloading/disposing.
- Spatial Composability (空间可组合性): Components declare required coeffects/dependencies (
inject); when dependencies become available or unavailable, the system reactively activates or deactivates components (Fiber state machine), guaranteeing Confluence (合流) regardless of registration order. - Context as the Sole Surface & Defensive Isolation: Eliminate cross-plugin private imports and global static singletons (
database.DB(), global configs), strictly enforcing single-owner boundaries andcontractsprogramming.
2. Architecture & Detailed Design
2.1 Revertible Effects & Scoped Extpoints (时间可组合性)
ContextScoped Lifetime: Each plugin instance is mounted with a dedicated child contextpluginCtx := rootCtx.Fork().- Automatic Disposer Registration for Extpoints:
When registrations occur through
pluginCtx, inverse operations are automatically pushed topluginCtx's Disposer stack:Router: Registering a route returns a definition with an ID;pluginCtxrecords a disposer that callsrouter.UnregisterByID(id).Events:ctx.Events().On(...)returns aDisposer; when called on a scoped context (or viactx.On(...)), it binds topluginCtx.OnDispose.Tasks: Registering an async task bindstasks.Unregister(taskType)topluginCtx.OnDispose.Schedules: Registering a cron schedule bindsschedules.Unregister(cronName)topluginCtx.OnDispose.Settings: Registering setting schemas binds schema deregistration topluginCtx.OnDispose.Container (Provide): Providing a service typeTbindscontainer.remove(T)topluginCtx.OnDispose.
- LIFO Teardown Guarantee:
Calling
pluginCtx.Dispose()runs all registered disposers in reverse order (LIFO), cleanly revoking routes, event listeners, tasks, schedules, and service bindings without residual side effects.
2.2 Reactive Coeffects & Fiber Lifecycle (空间可组合性)
- Dependency Declaration (
DependentPlugin): Plugins can optionally implement:type DependentPlugin interface { Plugin Inject() []reflect.Type } - Plugin Fiber State Machine:
PENDING ──(All dependencies provided)──> LOADING ──(Apply succeeds)──> ACTIVE ▲ │ └─────────────(Dependency removed / Plugin unloaded)──────────────────────┘- States:
FiberPending,FiberLoading,FiberActive,FiberUnloading,FiberDisposed. - Reconciler: When
core.Provide[T]registers a service orcore.App.Useregisters a plugin, the reconciler checks all pending fibers. Fibers with satisfied dependencies transitionPending -> Loading -> Active. - Confluence: Plugin registration order (
app.Use(A, B)vsapp.Use(B, A)) produces the exact same final active state once all dependencies are satisfied.
- States:
2.3 Boundary Defense & Single Owner Enforcement (架构防线)
- Eliminate Direct Global Invocations:
- Refactor
plugins/domain/user/repository.goand other domain repositories to avoid directimport "Wavelet/plugins/infra/database"and direct calls todatabase.DB(ctx). - Inject
contracts.DBServicevia repository struct or retrieve viactx.DB().
- Refactor
- Strict Package Separation:
backend/core/: Micro-kernel, context, container, fiber, events, scoped extpoints.backend/core/contracts/: Public interfaces and shared DTOs/events.backend/plugins/infra/: Infrastructure implementations providing contracts services.backend/plugins/drivers/: Runtime drivers (HTTP, Asynq Worker, Cron).backend/plugins/domain/: Domain business logic and single-owner tables.backend/pkg/: Stateless utilities and algorithm libraries.
3. Verification Plan
- Unit Tests for Core:
core/fiber_test.go: Test fiber state transitions, out-of-order registration confluence, and dynamic unloading.core/context_test.go&core/extpoints/: Test automatic scoped disposer tracking for routes, tasks, schedules, and event listeners.
- Refactoring Verification for Domain Plugins:
- Run
go test ./backend/...across all domain and infra packages. - Run
make code-checkand verify zero lint regressions.
- Run