docs(backend): define workspace crate boundaries

This commit is contained in:
Tunglies
2026-11-28 21:45:28 +08:00
parent f5d824c80b
commit 63100bbc80
2 changed files with 67 additions and 31 deletions
+51
View File
@@ -0,0 +1,51 @@
# Workspace Crate Architecture
## Choose the boundary
- Extract a capability whose invariants can be owned behind a small API. Before
creating a crate, identify its owner, what callers supply, and what can be
verified through the crate API. Keep a module when the proposed boundary only
relocates implementation or introduces a pass-through API.
- Keep the dependency direction from the app to the crate. Resolve app paths,
read product settings, start app-owned tasks, and publish UI events in the app.
Pass the values, resources, or callbacks the crate needs. For example,
[logging](clash-verge-logging/src/lib.rs) owns its pipeline and writers while the
app supplies directories and limits through `LoggerConfig`.
- Keep framework and platform dependencies scoped to the capability that needs
them. Runtime-independent logic should be runnable without the desktop app.
A platform adapter can own runtime startup when its resource lifetime requires
it, as [signal](clash-verge-signal/src/lib.rs) does for Linux shutdown.
## Design the API
- Start with concrete types for data and outcomes. Use a trait when methods must
cooperate under one contract, a closure for one supplied operation, and
`impl Future` when the caller owns scheduling. Add type parameters and bounds
where the contract needs them. Use dynamic dispatch when the API needs to hold
heterogeneous implementations, as [draft](clash-verge-draft/src/lib.rs) does
for the layers claimed by `DraftTransaction`.
- Keep state and its invariants with one owner. Expose snapshots, typed
operations, or guards. Draft owns claim, commit, and rollback of its in-memory
layers. The caller owns persistence and the recovery of external effects.
- Name product workspace libraries with the `clash-verge-` prefix and inherit
workspace lints. Reuse workspace dependencies where available and declare
only the features the crate needs.
## Own background work
- Give ordered start, replacement, and cleanup to one task owner. Reuse
[`clash-verge-task`](clash-verge-task/src/lib.rs) for coalescing scheduled jobs
and replaceable sessions. Keep domain policy, execution callbacks, and runtime
startup at the call site. Select a driver whose delivery guarantees match the
caller's needs.
- Use typed inputs. Coalesce only when intermediate requests may be skipped.
Reply with the actual outcome when a caller waits for completion. Preserve
transaction boundaries and keep the reply path free of locks held by the
waiting caller. Cancel only at safe boundaries and own issued effects through
completion or explicit reconciliation.
Verify the extracted contract through its public API when practical. Use
`cargo test -p <crate>` for focused tests and keep native behavior checks at the
app boundary. For timing, prefer Tokio's paused clock for task drivers or a
small clock interface where time is an input, as in
[`clash-verge-limiter`](clash-verge-limiter/src/lib.rs).
+16 -31
View File
@@ -1,34 +1,19 @@
# Backend Architecture Guardrails # Backend Architecture Guardrails
These rules supplement the repository's root `AGENTS.md`. Use [Workspace Crate Architecture](../crates/AGENTS.md) when extracting backend
capabilities or changing background task ownership.
- **Configuration ownership.** `config` owns source configuration, drafts and - **Configuration and effects.** `config` owns source configuration, drafts, and
persistence. The effects layer owns patch orchestration. Rollbackable persistence. The effects layer classifies every Verge patch field, including
transactions publish state after persistence and commit; already applied fields without effects and platform-specific fields, and reconciles requested
runtime and independently committed state remain authoritative even when a state in dependency order. Publish rollbackable state after persistence and
later step fails. Preserve that state, its notifications and the error. commit. On failure, preserve already applied runtime effects, independently
- **Effect ownership.** The declarative registry classifies every Verge patch field, committed state, their notifications, and the error.
including fields without effects and platform-specific fields. Effects - **Wire contract.** Use typed notifications and preserve event names, status
reconcile toward the requested state with explicit dependencies and ordering. values, and payload compatibility with frontend consumers.
Configuration rollback does not imply reversal of OS or lifecycle effects. - **Lock order.** Acquire configuration-write, configuration-update, Core
- **Shared wire contract.** Notifications are typed. Event names, status values lifecycle, and DNS ownership in that order when needed. Preserve independent
and payload shapes stay compatible with frontend producers and consumers. transaction boundaries and release synchronous guards before suspension.
- **Synchronization ownership.** Configuration-write serialization precedes - **Retries.** Reuse the backend retry policy and preserve attempts, delays, and
configuration-update ownership, which precedes Core lifecycle ownership. terminal errors. Keep monitor lifetime, condition waits, and candidate fallback
When both are held, the lifecycle lock precedes the DNS lock. Independent under their own contracts.
transactions retain their ownership boundaries; asynchronous work does not
hold synchronous data guards across suspension.
- **Task ownership.** Prefer a single task to own the state and resources of a
background operation whose start, replacement, and cleanup must be ordered.
Use typed inputs and coalesce requests only when intermediate requests may
be skipped. When callers depend on completion, reply with the actual outcome.
Cancel work only at cancellation-safe boundaries. Keep issued side effects
owned through completion or explicit reconciliation. Preserve shared
transaction boundaries and ensure a task can reply without acquiring a lock
held by the waiting caller. Reuse the drivers in
[`clash-verge-task`](../crates/clash-verge-task/src/lib.rs) for coalescing
jobs and replaceable sessions. Keep domain policy and runtime startup local.
- **Retry ownership.** Bounded retries share backend policy while preserving
attempts, delays and terminal errors. Background monitors own their lifetime
and cancellation separately; waiting for a condition or traversing distinct
candidates is not a retry policy.