93d1b21的任务收尾

This commit is contained in:
2025-12-19 02:30:45 +08:00
parent 93d1b21bb8
commit 5a29146c32
40 changed files with 3596 additions and 93 deletions

View File

@@ -4,13 +4,14 @@ from __future__ import annotations
import json
import os
import secrets
import time
from contextlib import contextmanager
from pathlib import Path
from aide.flow.errors import FlowError
from aide.flow.types import FlowStatus
from aide.flow.utils import now_task_id
from aide.flow.utils import now_iso, now_task_id
class FlowStorage:
@@ -21,6 +22,7 @@ class FlowStorage:
self.lock_path = self.aide_dir / "flow-status.lock"
self.tmp_path = self.aide_dir / "flow-status.json.tmp"
self.logs_dir = self.aide_dir / "logs"
self.back_confirm_path = self.aide_dir / "back-confirm-state.json"
def ensure_ready(self) -> None:
if not self.aide_dir.exists():
@@ -144,3 +146,45 @@ class FlowStorage:
return None
# === Back-confirm 状态管理 ===
def has_pending_back_confirm(self) -> bool:
"""检查是否存在待确认的 back 请求。"""
return self.back_confirm_path.exists()
def load_back_confirm_state(self) -> dict | None:
"""加载 back-confirm 状态。"""
if not self.back_confirm_path.exists():
return None
try:
raw = self.back_confirm_path.read_text(encoding="utf-8")
data = json.loads(raw)
if not isinstance(data, dict):
raise ValueError("back-confirm 状态文件格式错误")
return data
except Exception as exc:
raise FlowError(f"读取 back-confirm 状态失败: {exc}")
def save_back_confirm_state(self, target_part: str, reason: str) -> str:
"""保存 back-confirm 状态,返回生成的 key。"""
key = secrets.token_hex(6) # 12 字符的随机 key
data = {
"pending_key": key,
"target_part": target_part,
"reason": reason,
"created_at": now_iso(),
}
try:
payload = json.dumps(data, ensure_ascii=False, indent=2) + "\n"
self.back_confirm_path.write_text(payload, encoding="utf-8")
except Exception as exc:
raise FlowError(f"保存 back-confirm 状态失败: {exc}")
return key
def clear_back_confirm_state(self) -> None:
"""清除 back-confirm 状态文件。"""
try:
self.back_confirm_path.unlink(missing_ok=True)
except Exception as exc:
raise FlowError(f"清除 back-confirm 状态失败: {exc}")

View File

@@ -39,7 +39,64 @@ class FlowTracker:
return self._run(action="next-part", to_phase=phase, text=summary)
def back_part(self, phase: str, reason: str) -> bool:
return self._run(action="back-part", to_phase=phase, text=reason)
"""返工请求:检测是否已确认,未确认则生成 key。"""
try:
self.storage.ensure_ready()
# 检查是否存在待确认的 back 请求
if self.storage.has_pending_back_confirm():
state = self.storage.load_back_confirm_state()
if state:
output.warn("已存在待确认的返工请求")
output.info(f"目标环节: {state['target_part']}")
output.info(f"原因: {state['reason']}")
output.info(f"请执行: aide flow back-confirm --key {state['pending_key']}")
return False
# 生成新的确认 key
key = self.storage.save_back_confirm_state(phase, reason)
output.warn("返工需要确认。请先完成准备工作,然后执行:")
output.info(f"aide flow back-confirm --key {key}")
return True
except FlowError as exc:
output.err(str(exc))
return False
def back_confirm(self, key: str) -> bool:
"""确认返工请求并执行。"""
try:
self.storage.ensure_ready()
# 检查是否存在待确认的请求
state = self.storage.load_back_confirm_state()
if state is None:
output.err("无待确认的返工请求")
return False
# 验证 key
if state.get("pending_key") != key:
output.err("确认 key 不匹配")
return False
# 获取目标阶段和原因
target_part = state["target_part"]
reason = state["reason"]
# 清除确认状态文件
self.storage.clear_back_confirm_state()
# 执行实际的 back-part 操作
result = self._run(action="back-part", to_phase=target_part, text=reason)
if result:
output.warn("建议执行 /exit 重新开始对话")
return result
except FlowError as exc:
output.err(str(exc))
return False
def issue(self, description: str) -> bool:
return self._run(action="issue", to_phase=None, text=description)