From 63100bbc808fbd09e78a05c929769adc60977653 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:45:28 +0800 Subject: [PATCH] docs(backend): define workspace crate boundaries --- crates/AGENTS.md | 51 +++++++++++++++++++++++++++++++++++++++++++++ src-tauri/AGENTS.md | 47 ++++++++++++++--------------------------- 2 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 crates/AGENTS.md diff --git a/crates/AGENTS.md b/crates/AGENTS.md new file mode 100644 index 000000000..5436a3d04 --- /dev/null +++ b/crates/AGENTS.md @@ -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 ` 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). diff --git a/src-tauri/AGENTS.md b/src-tauri/AGENTS.md index 54a3fd5dc..af2554807 100644 --- a/src-tauri/AGENTS.md +++ b/src-tauri/AGENTS.md @@ -1,34 +1,19 @@ # 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 - persistence. The effects layer owns patch orchestration. Rollbackable - transactions publish state after persistence and commit; already applied - runtime and independently committed state remain authoritative even when a - later step fails. Preserve that state, its notifications and the error. -- **Effect ownership.** The declarative registry classifies every Verge patch field, - including fields without effects and platform-specific fields. Effects - reconcile toward the requested state with explicit dependencies and ordering. - Configuration rollback does not imply reversal of OS or lifecycle effects. -- **Shared wire contract.** Notifications are typed. Event names, status values - and payload shapes stay compatible with frontend producers and consumers. -- **Synchronization ownership.** Configuration-write serialization precedes - configuration-update ownership, which precedes Core lifecycle ownership. - When both are held, the lifecycle lock precedes the DNS lock. Independent - 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. +- **Configuration and effects.** `config` owns source configuration, drafts, and + persistence. The effects layer classifies every Verge patch field, including + fields without effects and platform-specific fields, and reconciles requested + state in dependency order. Publish rollbackable state after persistence and + commit. On failure, preserve already applied runtime effects, independently + committed state, their notifications, and the error. +- **Wire contract.** Use typed notifications and preserve event names, status + values, and payload compatibility with frontend consumers. +- **Lock order.** Acquire configuration-write, configuration-update, Core + lifecycle, and DNS ownership in that order when needed. Preserve independent + transaction boundaries and release synchronous guards before suspension. +- **Retries.** Reuse the backend retry policy and preserve attempts, delays, and + terminal errors. Keep monitor lifetime, condition waits, and candidate fallback + under their own contracts.