Skip to content

Commit e77a0f9

Browse files
committed
Fix macos performance issue
Update core Optimize some details
1 parent 6697ac5 commit e77a0f9

186 files changed

Lines changed: 4200 additions & 2365 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/agent-config.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Agent Configuration Model
2+
3+
Use the smallest durable surface that matches the job.
4+
5+
## Surfaces
6+
7+
- `AGENTS.md`: auto-loaded repository entry point. Keep it small and reserve it for always-on rules, routing, and high-priority expectations.
8+
- `.agents/*.md`: human- and agent-readable reference docs linked from `AGENTS.md`. Use these for detailed project context, commands, architecture, and conventions.
9+
- `.agents/skills/*/SKILL.md`: repo-scoped Codex skills. Use these for repeatable workflows that should be discoverable by name and description.
10+
- `.codex/config.toml`: trusted project Codex settings such as MCP, hooks, sandbox, approval, or model defaults.
11+
- `.codex/rules/*.rules`: command permission rules for running commands outside the sandbox. Do not mix these with coding conventions in `.agents/rules.md`.
12+
- `.codex/hooks.json` or `.codex/config.toml` hooks: lifecycle automation such as prompt checks, command validation, or final verification gates.
13+
14+
## Placement Rules
15+
16+
- Put stable team conventions in `AGENTS.md` only when every task must see them without opening another file.
17+
- Put detailed explanations in `.agents/*.md` and link them from `AGENTS.md`.
18+
- Put reusable task workflows in `.agents/skills/<skill-name>/SKILL.md`.
19+
- Put mechanical enforcement in linters, tests, hooks, or `.codex/rules`; do not rely on prose when tooling can enforce the rule.
20+
- Keep user-specific preferences out of the repository. They belong in user-level Codex configuration or user-level skills.
21+
22+
## Skill Authoring Rules
23+
24+
- Use lowercase hyphenated names.
25+
- Start descriptions with `Use when...`.
26+
- Describe trigger conditions in the description, not the workflow.
27+
- Keep `SKILL.md` lean; link to `.agents/*.md` for large reference material.
28+
- Add scripts only when deterministic behavior is needed repeatedly.

‎.agents/architecture.md‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Architecture
2+
3+
## Core Integration
4+
5+
The Go proxy core in `core/` operates in two modes.
6+
7+
Android lib mode:
8+
9+
- Go core is compiled as a C shared library, `libclash.so`, through `go build -buildmode=c-shared` with CGO.
10+
- Flutter calls it via FFI through the `service` plugin.
11+
- Dart-side implementation: `lib/core/lib.dart` (`CoreLib`).
12+
13+
Desktop core mode:
14+
15+
- Go core runs as a separate process with `CGO_ENABLED=0`.
16+
- Flutter communicates via JSON over socket, using a Unix socket on macOS/Linux and TCP on Windows.
17+
- Dart-side implementation: `lib/core/service.dart` (`CoreService`).
18+
19+
`lib/core/controller.dart` (`CoreController`) selects the implementation based on platform. `lib/core/interface.dart` defines the shared `CoreHandlerInterface`.
20+
21+
Key Go core files:
22+
23+
- `core/hub.go`: handler functions.
24+
- `core/action.go`: dispatch.
25+
- `core/lib.go`: CGO exports.
26+
- `core/server.go`: socket server.
27+
28+
## State Management
29+
30+
Provider files in `lib/providers/`:
31+
32+
- `app.dart`: runtime/UI state, logs, traffic, delays, loading, navigation.
33+
- `config.dart`: persistent config providers, app settings, theme, VPN, proxy style.
34+
- `state.dart`: derived/computed providers, navigation, proxy, tray, color scheme.
35+
- `action.dart`: business logic notifiers, setup, backup, core lifecycle, proxy selection.
36+
- `database.dart`: Drift database provider wrappers.
37+
38+
`globalState` in `lib/state.dart` is a singleton holding app lifecycle, timers, theme, and start/stop state. Providers are generated into `lib/providers/generated/`.
39+
40+
## Database
41+
42+
The app uses Drift/SQLite in `lib/database/`. Current schema version is 2.
43+
44+
Tables:
45+
46+
- `Profiles`
47+
- `Scripts`
48+
- `Rules`
49+
- `ProfileRuleLinks` (`profile_rule_mapping`)
50+
- `ProxyGroups`
51+
- `IconRecords` (`icon_records`)
52+
53+
Rule scenes distinguish global added rules, profile added rules, profile custom rules, and disabled links. Rule and proxy-group ordering use fractional indexing.
54+
55+
Generated Drift output lives in `lib/database/generated/database.g.dart`. After schema changes, run code generation and add or update focused database tests under `test/database/` when converter or migration behavior changes.
56+
57+
## Manager Stack
58+
59+
Managers are nested `InheritedWidget`/`StatefulWidget` components in `lib/application.dart`:
60+
61+
```text
62+
AppEnvManager > StatusManager > ThemeManager
63+
> [Desktop: WindowManager > TrayManager > HotKeyManager > ProxyManager]
64+
> ConnectivityManager > CoreManager > AppStateManager
65+
> [Mobile: AndroidManager > VpnManager | Desktop: WindowHeaderContainer]
66+
```
67+
68+
Each manager in `lib/manager/` handles a specific platform concern. Desktop-only managers are conditionally inserted.
69+
70+
## Core Controller and Actions
71+
72+
`lib/core/controller.dart` (`CoreController`) is a singleton facade over `CoreHandlerInterface`. Public methods delegate to the platform-specific interface, either Android FFI or desktop socket. It has an `@visibleForTesting` constructor and `resetInstance()` for test injection.
73+
74+
Business logic lives in Riverpod notifier classes in `lib/providers/action.dart`:
75+
76+
- `CommonAction`: update check and common UI operations.
77+
- `SetupAction`: config setup and TUN management.
78+
- `BackupAction`: backup/restore with WebDAV sync.
79+
- `CoreAction`: core lifecycle, init, connect, restart, shutdown.
80+
- `SystemAction`: system integration, tray, exit, brightness.
81+
- `StoreAction`: profile storage operations.
82+
- `ThemeAction`: theme state updates.
83+
- `ProxiesAction`: group management and proxy selection.
84+
- `ProfilesAction`: profile CRUD, auto-update, import.
85+
86+
## Platform Managers
87+
88+
Desktop:
89+
90+
- `WindowManager`
91+
- `TrayManager`
92+
- `HotKeyManager`
93+
- `ProxyManager`
94+
95+
Mobile:
96+
97+
- `AndroidManager`
98+
- `TileManager`
99+
- `VpnManager`
100+
101+
Shared:
102+
103+
- `ConnectivityManager`
104+
- `CoreManager`
105+
- `AppStateManager`
106+
- `StatusManager`
107+
- `ThemeManager`
108+
109+
## Build System
110+
111+
`setup.dart` is the release build orchestrator:
112+
113+
1. On Windows, pre-builds Go core via `dart run build_tool windows` and reads `core_sha256.json`.
114+
2. Writes `env.json` (`APP_ENV`).
115+
3. Passes SHA256 as `--dart-define=CORE_SHA256=$val`, embedded at compile time for Windows.
116+
4. Activates `flutter_distributor` for packaging.
117+
118+
Go core building is handled by `build_tool`, a standalone Dart CLI in `plugins/setup/buildkit/build_tool/`.
119+
120+
Platform build hooks inside `flutter build` trigger `build_tool` automatically:
121+
122+
- macOS: podspec script phase, `build_pod.sh`, `build_tool macos`.
123+
- Linux: CMake include, `buildkit/cmake/buildkit.cmake`, `build_tool linux`.
124+
- Windows: CMake include, `buildkit/cmake/buildkit.cmake`, `build_tool windows`. Debug passes `--dev` via `CMAKE_BUILD_TYPE`.
125+
- Android: Gradle include, `buildkit/gradle/plugin.gradle`, `build_tool android`.
126+
127+
Windows helper auth:
128+
129+
- Release: Core SHA256 is embedded in both the Flutter app and the Rust helper. The app pings the helper and verifies the token matches.
130+
- Debug: The Rust helper skips token verification when built in debug mode, so `flutter run` works without the SHA256 flow.
131+
132+
`plugins/setup/` is an FFI plugin that exists only as a build harness. It carries no Dart API, only platform build hooks that trigger Go compilation. Windows builds also compile a Rust helper in `services/helper/` through `RustBuilder`.
133+
134+
Build configuration defaults live in `build_tool/lib/src/options.dart` and can be overridden via `build_config.yaml`.
135+
136+
Architecture detection is automatic. The `--description` flag passed to `flutter_distributor` adds arch suffixes to artifact names, such as `FlClash-0.8.93-macos-arm64.dmg`.
137+
138+
## Local Plugins
139+
140+
- `setup`: build harness FFI plugin.
141+
- `proxy`: system proxy configuration.
142+
- `rust_api`: Flutter Rust Bridge FFI plugin.
143+
- `tray_manager`: system tray fork/customization.
144+
- `wifi_ssid`: Wi-Fi SSID detection.
145+
- `window_ext`: window extensions.
146+
- `flutter_distributor`: app packaging/distribution.
147+
148+
## Rust Helper Service
149+
150+
`services/helper/` is a Windows-only privileged helper for starting the core as admin and managing TUN. It is built with:
151+
152+
```bash
153+
cargo build --release --features windows-service
154+
```
155+
156+
It uses token-based auth with the Flutter app.

‎.agents/commands.md‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Commands
2+
3+
## Building
4+
5+
Update submodules first. The ClashMeta Go core lives in `core/Clash.Meta/`.
6+
7+
```bash
8+
git submodule update --init --recursive
9+
```
10+
11+
Full package build, including Go core, Flutter, and packaging, runs through `setup.dart`:
12+
13+
```bash
14+
dart setup.dart macos
15+
dart setup.dart linux
16+
dart setup.dart windows
17+
dart setup.dart android
18+
```
19+
20+
Build only the Go core and skip Flutter packaging:
21+
22+
```bash
23+
bash plugins/setup/buildkit/run_build_tool.sh macos
24+
bash plugins/setup/buildkit/run_build_tool.sh linux
25+
bash plugins/setup/buildkit/run_build_tool.sh windows
26+
bash plugins/setup/buildkit/run_build_tool.sh android
27+
```
28+
29+
## Flutter Development
30+
31+
The project is pinned with FVM.
32+
33+
```bash
34+
fvm flutter pub get
35+
fvm flutter run
36+
fvm flutter test
37+
```
38+
39+
Plain Flutter also works when the global SDK matches project constraints:
40+
41+
```bash
42+
flutter pub get
43+
flutter run
44+
flutter test
45+
```
46+
47+
Use `flutter test`, not `dart test`, because models pull in Flutter types.
48+
49+
## Code Generation
50+
51+
Run code generation after modifying models, providers, or database schema:
52+
53+
```bash
54+
dart run build_runner build --delete-conflicting-outputs
55+
dart run build_runner watch
56+
```
57+
58+
Code generation covers:
59+
60+
- Riverpod providers through `riverpod_generator`.
61+
- Models through `freezed` and `json_serializable`.
62+
- Database tables through `drift_dev`.
63+
64+
Generated output paths, configured in `build.yaml`:
65+
66+
- `lib/models/generated/*.g.dart`, `*.freezed.dart`.
67+
- `lib/providers/generated/*.g.dart`.
68+
- `lib/database/generated/*.g.dart`.
69+
70+
## Testing
71+
72+
Tests use `package:test/test.dart` for pure Dart logic and `flutter_test` for provider and widget tests. `mocktail` is the mocking framework.
73+
74+
```bash
75+
flutter test test/models/
76+
flutter test test/core/
77+
flutter test test/providers/
78+
flutter test test/common/
79+
flutter test test/database/
80+
flutter test test/widgets/
81+
flutter test test/setup_test.dart
82+
flutter test plugins/proxy/test/proxy_test.dart
83+
```
84+
85+
Root `flutter test` only discovers the root package's `test/` directory by default. Include bundled plugin Dart tests by passing paths explicitly, or run `flutter test` from that plugin package directory. Native plugin tests under platform folders are not run by `flutter test`.
86+
87+
## Verify
88+
89+
CI runs these in order:
90+
91+
```bash
92+
flutter pub get
93+
flutter analyze --no-fatal-infos
94+
flutter test --reporter expanded
95+
```
96+
97+
Run `flutter analyze` locally before committing when practical.

‎.agents/project.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Project Context
2+
3+
FlClash is a multi-platform proxy client based on ClashMeta (mihomo), built with Flutter. It supports Android, Windows, macOS, and Linux, using a Material You design with Surfboard-like UI.
4+
5+
## Version Notes
6+
7+
- `.fvmrc` pins Flutter 3.35.7 for local development.
8+
- CI uses Flutter 3.41.9. These may diverge; trust CI as the source of truth for release builds.
9+
- Dart SDK constraint: `>=3.8.0 <4.0.0`.
10+
11+
## Build Dependencies
12+
13+
Linux:
14+
15+
```bash
16+
sudo apt-get install libayatana-appindicator3-dev libkeybinder-3.0-dev
17+
```
18+
19+
Windows:
20+
21+
- GCC and Inno Setup.
22+
- `ANDROID_NDK` env var for Android builds.
23+
24+
macOS:
25+
26+
```bash
27+
npm install -g appdmg
28+
```

‎.agents/rules.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Rules
2+
3+
These are repository coding and testing conventions. Codex command permission rules belong in `.codex/rules/*.rules`; see `.agents/agent-config.md` before adding those.
4+
5+
## Dart and Flutter Style
6+
7+
`analysis_options.yaml` enforces these non-default rules:
8+
9+
- `prefer_single_quotes: true`: always use single quotes.
10+
- `require_trailing_commas: true`: use trailing commas in multi-line argument lists.
11+
- `sort_child_properties_last: true`: `child:` must be the last named parameter.
12+
- `avoid_print: true`: do not use `print()` calls.
13+
- `prefer_const_constructors: true` and `prefer_const_declarations: true`.
14+
- `prefer_final_locals: true` and `prefer_final_in_for_each: true`.
15+
- `always_declare_return_types: true`.
16+
17+
Generated directories are excluded from analysis:
18+
19+
- `build/**`
20+
- `lib/l10n/intl/**`
21+
- `lib/**/generated/**`
22+
- `plugins/**`
23+
24+
## Testing Rules
25+
26+
Use `CoreController.test(mock)` to inject a mocked `CoreHandlerInterface`. Call `CoreController.resetInstance()` in `tearDown` to clean up the singleton between tests.
27+
28+
Register fallback values for freezed params used with `any()` matchers.
29+
30+
Use `ProviderContainer` directly for simple Riverpod provider tests. The generated Riverpod `update()` method takes a callback:
31+
32+
```dart
33+
notifier.update((state) => newValue);
34+
```
35+
36+
When testing freezed models with nested objects, always round-trip through `jsonEncode` and `jsonDecode`. Direct `fromJson(toJson())` fails for nested freezed types because `toJson()` stores child objects directly instead of maps.
37+
38+
## Generated Code
39+
40+
Do not manually edit generated files under:
41+
42+
- `lib/models/generated/`
43+
- `lib/providers/generated/`
44+
- `lib/database/generated/`
45+
- `lib/l10n/intl/`
46+
47+
After schema, model, or provider changes, run build generation and include focused tests when behavior changes.

‎.agents/skills.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Agent Skills
2+
3+
Repo-scoped Codex skills live under `.agents/skills/*/SKILL.md`. Codex can discover these by skill `name` and `description`, then load the full instructions only when a task matches.
4+
5+
## Available Repo Skills
6+
7+
- `localization`: hardcoded UI text scans, ARB updates, locale generation, and localization verification.
8+
- `provider-tests`: Riverpod provider, notifier, and state-management tests.
9+
- `ui-work`: Flutter UI, widgets, Material You styling, navigation surfaces, and user-facing interactions.
10+
- `core-platform`: core integration, platform managers, Go core communication, desktop/mobile behavior, and Windows helper flow.
11+
12+
## Authoring Notes
13+
14+
- Add new repeatable workflows as `.agents/skills/<skill-name>/SKILL.md`.
15+
- Keep skill descriptions trigger-focused and start them with `Use when...`.
16+
- Keep long reference material in `.agents/*.md`; skills should link to it instead of duplicating it.
17+
- Put command permission rules in `.codex/rules/*.rules`, not in this file.

0 commit comments

Comments
 (0)