lingflow-plus 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 灵字辈 (Ling) Ecosystem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: lingflow-plus
3
+ Version: 0.1.0
4
+ Summary: LingFlow+ — Multi-project parallel CLI Agent for 灵字辈 ecosystem
5
+ License: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: lingflow
10
+ Requires-Dist: pyyaml>=6.0
11
+ Provides-Extra: dev
12
+ Requires-Dist: pytest>=7.4; extra == "dev"
13
+ Requires-Dist: pytest-cov>=4.1; extra == "dev"
14
+ Requires-Dist: pytest-asyncio; extra == "dev"
15
+
16
+ # LingFlow+
17
+
18
+ 灵字辈生态系统的多项目并行 CLI Agent。
19
+
20
+ ## 设计宪章
21
+
22
+ **轻框架,多工具。重流程,重协调,重约束,重验证。和谐欢畅,自然端庄。**
23
+
24
+ ## 架构
25
+
26
+ ```
27
+ LingFlow+ (集成层, ~800 行)
28
+ ├── project_manager.py 项目注册、Git 状态、会话绑定
29
+ ├── scheduler.py 跨项目并行调度(复用 LingFlow WorkflowOrchestrator)
30
+ ├── constraints.py TokenQuota + RateLimiter + FileLock + ContextBudget
31
+ ├── tool_router.py 任务类型 → Agent 路由(MCP 协议)
32
+ ├── quality_gate.py 提交前质量门
33
+ ├── coordinator.py 主协调器(组合所有子系统)
34
+ └── cli.py CLI 入口(+run, +status, +projects, +dashboard, +review)
35
+ ```
36
+
37
+ ## 依赖
38
+
39
+ - `lingflow` — 工作流引擎
40
+ - `pyyaml` — YAML 工作流加载
41
+
42
+ ## 安装
43
+
44
+ ```bash
45
+ pip install -e .
46
+ ```
47
+
48
+ ## 使用
49
+
50
+ ```bash
51
+ # 注册项目
52
+ lingflow-plus register LingFlow /home/ai/LingFlow
53
+ lingflow-plus register LingClaude /home/ai/LingClaude
54
+
55
+ # 查看所有项目
56
+ lingflow-plus projects
57
+
58
+ # 看板
59
+ lingflow-plus dashboard
60
+
61
+ # 执行跨项目工作流
62
+ lingflow-plus run workflow.yaml
63
+
64
+ # 质量门检查
65
+ lingflow-plus review src/main.py tests/test_main.py
66
+
67
+ # 全局状态
68
+ lingflow-plus status
69
+ ```
70
+
71
+ ## 测试
72
+
73
+ ```bash
74
+ pytest tests/ -v
75
+ ```
@@ -0,0 +1,60 @@
1
+ # LingFlow+
2
+
3
+ 灵字辈生态系统的多项目并行 CLI Agent。
4
+
5
+ ## 设计宪章
6
+
7
+ **轻框架,多工具。重流程,重协调,重约束,重验证。和谐欢畅,自然端庄。**
8
+
9
+ ## 架构
10
+
11
+ ```
12
+ LingFlow+ (集成层, ~800 行)
13
+ ├── project_manager.py 项目注册、Git 状态、会话绑定
14
+ ├── scheduler.py 跨项目并行调度(复用 LingFlow WorkflowOrchestrator)
15
+ ├── constraints.py TokenQuota + RateLimiter + FileLock + ContextBudget
16
+ ├── tool_router.py 任务类型 → Agent 路由(MCP 协议)
17
+ ├── quality_gate.py 提交前质量门
18
+ ├── coordinator.py 主协调器(组合所有子系统)
19
+ └── cli.py CLI 入口(+run, +status, +projects, +dashboard, +review)
20
+ ```
21
+
22
+ ## 依赖
23
+
24
+ - `lingflow` — 工作流引擎
25
+ - `pyyaml` — YAML 工作流加载
26
+
27
+ ## 安装
28
+
29
+ ```bash
30
+ pip install -e .
31
+ ```
32
+
33
+ ## 使用
34
+
35
+ ```bash
36
+ # 注册项目
37
+ lingflow-plus register LingFlow /home/ai/LingFlow
38
+ lingflow-plus register LingClaude /home/ai/LingClaude
39
+
40
+ # 查看所有项目
41
+ lingflow-plus projects
42
+
43
+ # 看板
44
+ lingflow-plus dashboard
45
+
46
+ # 执行跨项目工作流
47
+ lingflow-plus run workflow.yaml
48
+
49
+ # 质量门检查
50
+ lingflow-plus review src/main.py tests/test_main.py
51
+
52
+ # 全局状态
53
+ lingflow-plus status
54
+ ```
55
+
56
+ ## 测试
57
+
58
+ ```bash
59
+ pytest tests/ -v
60
+ ```
@@ -0,0 +1,7 @@
1
+ from __future__ import annotations
2
+ """LingFlow+ — Multi-project parallel CLI Agent
3
+
4
+ 灵字辈生态系统的多项目并行协调器。
5
+ """
6
+
7
+ __version__ = "0.1.0"
@@ -0,0 +1,160 @@
1
+ from __future__ import annotations
2
+ """LingFlow+ CLI 入口
3
+
4
+ 命令:
5
+ +run <workflow.yaml> 执行跨项目工作流
6
+ +status 查看全局状态
7
+ +projects 列出注册的项目
8
+ +register <name> <path> 注册项目
9
+ +unregister <name> 取消注册
10
+ +dashboard 看板(所有项目状态)
11
+ +review <files...> 质量门检查
12
+ +version 版本信息
13
+ """
14
+
15
+ import argparse
16
+ import json
17
+ import sys
18
+ from pathlib import Path
19
+ from typing import List, Optional
20
+
21
+ from lingflow_plus import __version__
22
+ from lingflow_plus.coordinator import LingFlowPlus
23
+
24
+
25
+ def cmd_status(args: argparse.Namespace) -> None:
26
+ """查看全局状态"""
27
+ lf = LingFlowPlus()
28
+ status = lf.status()
29
+ print(json.dumps(status, indent=2, ensure_ascii=False, default=str))
30
+
31
+
32
+ def cmd_projects(args: argparse.Namespace) -> None:
33
+ """列出所有项目"""
34
+ lf = LingFlowPlus()
35
+ projects = lf.project_manager.list()
36
+ if not projects:
37
+ print("No projects registered.")
38
+ return
39
+ for p in projects:
40
+ git = p.git_status()
41
+ session = p.terminal_session or "-"
42
+ print(f" {p.name:20s} {git.get('branch', '?'):15s} dirty={git.get('dirty_files', 0):3d} session={session}")
43
+ if p.description:
44
+ print(f" {'':20s} {p.description}")
45
+
46
+
47
+ def cmd_register(args: argparse.Namespace) -> None:
48
+ """注册项目"""
49
+ lf = LingFlowPlus()
50
+ path = str(Path(args.path).resolve())
51
+ ctx = lf.project_manager.register(args.name, path, description=args.description or "")
52
+ print(f"Registered: {ctx.name} -> {ctx.path}")
53
+
54
+
55
+ def cmd_unregister(args: argparse.Namespace) -> None:
56
+ """取消注册"""
57
+ lf = LingFlowPlus()
58
+ if lf.project_manager.unregister(args.name):
59
+ print(f"Unregistered: {args.name}")
60
+ else:
61
+ print(f"Project not found: {args.name}")
62
+
63
+
64
+ def cmd_dashboard(args: argparse.Namespace) -> None:
65
+ """看板"""
66
+ lf = LingFlowPlus()
67
+ dash = lf.project_manager.dashboard()
68
+ for item in dash:
69
+ name = item.get("name", "?")
70
+ git = item.get("git", {})
71
+ valid = item.get("valid", False)
72
+ branch = git.get("branch", "?")
73
+ dirty = git.get("dirty_files", 0)
74
+ status_mark = "✓" if valid else "✗"
75
+ print(f" [{status_mark}] {name:20s} branch={branch:15s} dirty={dirty}")
76
+ token_status = lf.token_quota.get_status()
77
+ print(f"\n Token: {token_status['window_used']:,} / {token_status['window_total']:,} used")
78
+
79
+
80
+ def cmd_run(args: argparse.Namespace) -> None:
81
+ """执行工作流"""
82
+ lf = LingFlowPlus()
83
+ filepath = args.workflow
84
+ if not Path(filepath).exists():
85
+ print(f"File not found: {filepath}")
86
+ sys.exit(1)
87
+ print(f"Loading workflow: {filepath}")
88
+ results = lf.run_workflow_file(filepath)
89
+ success = sum(1 for r in results.values() if r.success)
90
+ failed = sum(1 for r in results.values() if not r.success)
91
+ print(f"Done: {success} success, {failed} failed, {len(results)} total")
92
+
93
+
94
+ def cmd_review(args: argparse.Namespace) -> None:
95
+ """质量门检查"""
96
+ lf = LingFlowPlus()
97
+ report = lf.quality_check(args.files)
98
+ print(f"Score: {report.score}/100")
99
+ print(f"Gate: {'PASSED' if report.passed else 'BLOCKED'}")
100
+ if report.critical_issues:
101
+ print("\nCritical issues:")
102
+ for issue in report.critical_issues:
103
+ print(f" ✗ {issue}")
104
+ if report.warnings:
105
+ print("\nWarnings:")
106
+ for w in report.warnings:
107
+ print(f" ⚠ {w}")
108
+
109
+
110
+ def cmd_version(args: argparse.Namespace) -> None:
111
+ """版本信息"""
112
+ print(f"LingFlow+ v{__version__}")
113
+
114
+
115
+ def main(argv: Optional[List[str]] = None) -> None:
116
+ parser = argparse.ArgumentParser(
117
+ prog="lingflow-plus",
118
+ description="LingFlow+ — 灵字辈多项目并行 CLI Agent",
119
+ )
120
+ sub = parser.add_subparsers(dest="command")
121
+
122
+ p_run = sub.add_parser("run", help="Execute cross-project workflow")
123
+ p_run.add_argument("workflow", help="YAML workflow file")
124
+ p_run.set_defaults(func=cmd_run)
125
+
126
+ p_status = sub.add_parser("status", help="Global status")
127
+ p_status.set_defaults(func=cmd_status)
128
+
129
+ p_projects = sub.add_parser("projects", help="List projects")
130
+ p_projects.set_defaults(func=cmd_projects)
131
+
132
+ p_register = sub.add_parser("register", help="Register project")
133
+ p_register.add_argument("name", help="Project alias")
134
+ p_register.add_argument("path", help="Project directory")
135
+ p_register.add_argument("--description", "-d", default="", help="Description")
136
+ p_register.set_defaults(func=cmd_register)
137
+
138
+ p_unregister = sub.add_parser("unregister", help="Unregister project")
139
+ p_unregister.add_argument("name", help="Project alias")
140
+ p_unregister.set_defaults(func=cmd_unregister)
141
+
142
+ p_dashboard = sub.add_parser("dashboard", help="Dashboard view")
143
+ p_dashboard.set_defaults(func=cmd_dashboard)
144
+
145
+ p_review = sub.add_parser("review", help="Quality gate check")
146
+ p_review.add_argument("files", nargs="+", help="Files to check")
147
+ p_review.set_defaults(func=cmd_review)
148
+
149
+ p_version = sub.add_parser("version", help="Version info")
150
+ p_version.set_defaults(func=cmd_version)
151
+
152
+ args = parser.parse_args(argv)
153
+ if not hasattr(args, "func"):
154
+ parser.print_help()
155
+ sys.exit(0)
156
+ args.func(args)
157
+
158
+
159
+ if __name__ == "__main__":
160
+ main()
@@ -0,0 +1,267 @@
1
+ from __future__ import annotations
2
+ """约束层
3
+
4
+ TokenQuota、RateLimiter、FileLock、ContextBudget 四大约束。
5
+ 确保多项目并行执行时的资源安全。
6
+ """
7
+
8
+ import fcntl
9
+ import json
10
+ import logging
11
+ import time
12
+ from dataclasses import dataclass, field
13
+ from pathlib import Path
14
+ from threading import Lock
15
+ from typing import Any, Dict, List, Optional
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+ LOCKS_DIR = Path.home() / ".lingflow-plus" / "locks"
20
+ STATE_PATH = Path.home() / ".lingflow-plus" / "state.json"
21
+
22
+
23
+ @dataclass
24
+ class TokenBudget:
25
+ """单项目 token 预算"""
26
+ project: str
27
+ allocated: int = 0
28
+ used: int = 0
29
+
30
+ @property
31
+ def remaining(self) -> int:
32
+ return max(0, self.allocated - self.used)
33
+
34
+ @property
35
+ def usage_ratio(self) -> float:
36
+ return self.used / self.allocated if self.allocated > 0 else 0.0
37
+
38
+
39
+ class TokenQuotaManager:
40
+ """GLM API Token 配额管理器
41
+
42
+ 跟踪 5 小时窗口内的 token 消耗,按项目分配预算。
43
+ """
44
+
45
+ def __init__(self, window_tokens: int = 5_000_000, window_seconds: int = 18000):
46
+ self.window_tokens = window_tokens
47
+ self.window_seconds = window_seconds
48
+ self._budgets: Dict[str, TokenBudget] = {}
49
+ self._lock = Lock()
50
+ self._window_start = time.time()
51
+ self._total_used = 0
52
+
53
+ def allocate(self, project: str, tokens: int) -> TokenBudget:
54
+ """为项目分配 token 预算"""
55
+ with self._lock:
56
+ self._check_window_reset()
57
+ budget = TokenBudget(project=project, allocated=tokens)
58
+ self._budgets[project] = budget
59
+ logger.info(f"Allocated {tokens} tokens for {project}")
60
+ return budget
61
+
62
+ def consume(self, project: str, tokens: int) -> bool:
63
+ """消耗 token,返回是否成功(超预算返回 False)"""
64
+ with self._lock:
65
+ self._check_window_reset()
66
+ budget = self._budgets.get(project)
67
+ if not budget:
68
+ logger.warning(f"No budget for project {project}")
69
+ return False
70
+ if budget.remaining < tokens:
71
+ logger.warning(f"Token budget exhausted for {project}: {budget.remaining} < {tokens}")
72
+ return False
73
+ budget.used += tokens
74
+ self._total_used += tokens
75
+ return True
76
+
77
+ def get_status(self) -> Dict[str, Any]:
78
+ """获取配额状态"""
79
+ with self._lock:
80
+ self._check_window_reset()
81
+ return {
82
+ "window_total": self.window_tokens,
83
+ "window_used": self._total_used,
84
+ "window_remaining": self.window_tokens - self._total_used,
85
+ "window_elapsed": round(time.time() - self._window_start, 1),
86
+ "projects": {name: {"allocated": b.allocated, "used": b.used, "remaining": b.remaining}
87
+ for name, b in self._budgets.items()},
88
+ }
89
+
90
+ def _check_window_reset(self) -> None:
91
+ """检查是否需要重置窗口"""
92
+ if time.time() - self._window_start >= self.window_seconds:
93
+ self._window_start = time.time()
94
+ self._total_used = 0
95
+ for budget in self._budgets.values():
96
+ budget.used = 0
97
+
98
+
99
+ class RateLimiter:
100
+ """请求速率限制器
101
+
102
+ 指数退避 + 滑动窗口。
103
+ """
104
+
105
+ def __init__(self, max_rpm: int = 60, max_concurrent: int = 5):
106
+ self.max_rpm = max_rpm
107
+ self.max_concurrent = max_concurrent
108
+ self._timestamps: List[float] = []
109
+ self._active = 0
110
+ self._lock = Lock()
111
+ self._backoff_until: float = 0
112
+
113
+ def acquire(self) -> float:
114
+ """获取执行许可,返回需要等待的秒数"""
115
+ with self._lock:
116
+ now = time.time()
117
+ if now < self._backoff_until:
118
+ return self._backoff_until - now
119
+
120
+ self._timestamps = [t for t in self._timestamps if now - t < 60]
121
+ if len(self._timestamps) >= self.max_rpm:
122
+ wait = 60 - (now - self._timestamps[0]) + 0.1
123
+ return wait
124
+
125
+ if self._active >= self.max_concurrent:
126
+ return 0.5
127
+
128
+ self._timestamps.append(now)
129
+ self._active += 1
130
+ return 0.0
131
+
132
+ def release(self) -> None:
133
+ """释放执行许可"""
134
+ with self._lock:
135
+ self._active = max(0, self._active - 1)
136
+
137
+ def trigger_backoff(self, seconds: float = 30.0) -> None:
138
+ """触发退避(如收到 429 响应)"""
139
+ with self._lock:
140
+ self._backoff_until = time.time() + seconds
141
+ logger.warning(f"Rate limiter backoff triggered: {seconds}s")
142
+
143
+ def get_status(self) -> Dict[str, Any]:
144
+ """获取速率状态"""
145
+ with self._lock:
146
+ now = time.time()
147
+ recent = [t for t in self._timestamps if now - t < 60]
148
+ return {
149
+ "rpm_used": len(recent),
150
+ "rpm_limit": self.max_rpm,
151
+ "active_requests": self._active,
152
+ "max_concurrent": self.max_concurrent,
153
+ "backoff_remaining": max(0, self._backoff_until - now),
154
+ }
155
+
156
+
157
+ class FileLock:
158
+ """基于 fcntl.flock 的文件级互斥锁
159
+
160
+ 用于防止多个进程同时修改同一文件。
161
+ """
162
+
163
+ def __init__(self, locks_dir: Optional[str] = None):
164
+ self._locks_dir = Path(locks_dir) if locks_dir else LOCKS_DIR
165
+ self._locks_dir.mkdir(parents=True, exist_ok=True)
166
+ self._fd_map: Dict[str, Any] = {}
167
+
168
+ def acquire(self, filepath: str, timeout: float = 30.0) -> bool:
169
+ """获取文件锁"""
170
+ lock_path = str(self._locks_dir / _safe_lock_name(filepath))
171
+ start = time.time()
172
+ while time.time() - start < timeout:
173
+ try:
174
+ fd = open(lock_path, "w")
175
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
176
+ self._fd_map[filepath] = fd
177
+ return True
178
+ except (IOError, OSError):
179
+ try:
180
+ fd.close()
181
+ except Exception:
182
+ pass
183
+ time.sleep(0.1)
184
+ logger.warning(f"FileLock timeout for {filepath}")
185
+ return False
186
+
187
+ def release(self, filepath: str) -> None:
188
+ """释放文件锁"""
189
+ fd = self._fd_map.pop(filepath, None)
190
+ if fd is not None:
191
+ try:
192
+ fcntl.flock(fd, fcntl.LOCK_UN)
193
+ fd.close()
194
+ except (IOError, OSError):
195
+ pass
196
+
197
+ def is_locked(self, filepath: str) -> bool:
198
+ """检查文件是否被锁"""
199
+ lock_path = str(self._locks_dir / _safe_lock_name(filepath))
200
+ try:
201
+ fd = open(lock_path, "w")
202
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
203
+ fcntl.flock(fd, fcntl.LOCK_UN)
204
+ fd.close()
205
+ return False
206
+ except (IOError, OSError):
207
+ try:
208
+ fd.close()
209
+ except Exception:
210
+ pass
211
+ return True
212
+
213
+
214
+ class ContextBudget:
215
+ """项目上下文预算管理
216
+
217
+ 每个项目有独立上下文预算,超限时自动触发压缩。
218
+ """
219
+
220
+ def __init__(self, default_limit: int = 8000, compress_threshold: float = 0.85):
221
+ self.default_limit = default_limit
222
+ self.compress_threshold = compress_threshold
223
+ self._limits: Dict[str, int] = {}
224
+ self._usage: Dict[str, int] = {}
225
+
226
+ def set_limit(self, project: str, limit: int) -> None:
227
+ """设置项目上下文限制"""
228
+ self._limits[project] = limit
229
+
230
+ def get_limit(self, project: str) -> int:
231
+ """获取项目上下文限制"""
232
+ return self._limits.get(project, self.default_limit)
233
+
234
+ def track(self, project: str, tokens: int) -> None:
235
+ """记录 token 消耗"""
236
+ self._usage[project] = self._usage.get(project, 0) + tokens
237
+
238
+ def should_compress(self, project: str) -> bool:
239
+ """是否需要压缩上下文"""
240
+ usage = self._usage.get(project, 0)
241
+ limit = self.get_limit(project)
242
+ return usage >= limit * self.compress_threshold
243
+
244
+ def reset(self, project: str) -> None:
245
+ """重置项目上下文使用量"""
246
+ self._usage[project] = 0
247
+
248
+ def get_status(self) -> Dict[str, Dict[str, Any]]:
249
+ """获取所有项目上下文状态"""
250
+ result = {}
251
+ for project in set(list(self._limits.keys()) + list(self._usage.keys())):
252
+ limit = self.get_limit(project)
253
+ used = self._usage.get(project, 0)
254
+ result[project] = {
255
+ "limit": limit,
256
+ "used": used,
257
+ "remaining": max(0, limit - used),
258
+ "usage_ratio": round(used / limit, 3) if limit > 0 else 0,
259
+ "should_compress": used >= limit * self.compress_threshold,
260
+ }
261
+ return result
262
+
263
+
264
+ def _safe_lock_name(filepath: str) -> str:
265
+ """将文件路径转换为安全的锁文件名"""
266
+ import hashlib
267
+ return hashlib.md5(filepath.encode()).hexdigest() + ".lock"