feat(engine): 添加MCP连接管理和工具集成功能

- 集成MCP连接管理器,支持MCP服务器连接
- 添加多种内置工具:ClarifyTool、CronTool、DelegateTool、ExecuteCodeTool、
  PatchFileTool、ProcessTool、SendMessageTool、SpawnTool、TerminalTool、
  TodoTool、WebFetchTool、WebSearchTool、WriteFileTool等
- 实现工具注册和装配功能
- 添加技能选择上下文参数
- 支持思考模式控制参数thinking_enabled

feat(coordinator): 重构任务执行计划器参数命名

- 将learning_candidate_enabled重命名为allow_candidate_generation
- 更新TeamGraphScheduler中的参数传递
- 修改LocalAgentRunner中的相关参数处理
- 更新README文档中的相应描述

refactor(context): 标准化工具调用参数格式

- 添加_json导入用于参数序列化
- 实现_provider_tool_calls方法标准化OpenAI兼容的工具调用载荷
- 修复工具调用中参数非字符串类型的序列化问题

refactor(session): 优化消息历史记录过滤逻辑

- 修改get_messages_as_conversation为基于运行状态过滤消息
- 排除未完成、失败或错误结束的运行记录
- 改进对话历史的可见性控制机制

fix(store): 修复FTS索引重建逻辑

- 添加异常处理防止FTS索引创建失败
- 实现_rebuild_fts_index方法重新构建全文搜索索引
- 优化索引触发器和表的维护流程
This commit is contained in:
2026-05-14 09:43:48 +08:00
parent 8a12c30141
commit 30ab74ffb2
149 changed files with 12293 additions and 2812 deletions

View File

@ -1,19 +1,22 @@
"""LLM-driven skill assembler.
这层现在不再自己做规则打分,而是直接把:
这层现在不再自己做规则打分,而是分两步把:
1. task description
2. embedding 召回后的候选 skill 摘要
3. 粗选候选的完整 skill 正文
交给一个模型来决定本轮要激活哪些 skill。
当前目标非常克制:
- 输入尽量简单
- 主 agent 不拿 skill_view也不动态探索技能库
- SkillAssembler 可以在系统侧内部读取候选 skill 正文
- 输出只要 skill 名称
- 没有命中就返回空 skills
"""
from __future__ import annotations
import asyncio
from dataclasses import dataclass, field
import json
from typing import Any
@ -31,6 +34,7 @@ class SkillAssemblyResult:
"""一次装配后真正要注入当前 run 的 skills。"""
activated_skills: list[SkillContext] = field(default_factory=list)
llm_interactions: list[dict[str, Any]] = field(default_factory=list)
class SkillAssembler:
@ -40,9 +44,14 @@ class SkillAssembler:
self,
loader: SkillsLoader,
retriever: SkillEmbeddingRetriever | None = None,
*,
max_detailed_candidates: int = 5,
max_candidate_content_chars: int = 6000,
) -> None:
self.loader = loader
self.retriever = retriever or SkillEmbeddingRetriever()
self.max_detailed_candidates = max(1, max_detailed_candidates)
self.max_candidate_content_chars = max(1000, max_candidate_content_chars)
async def assemble(
self,
@ -51,6 +60,7 @@ class SkillAssembler:
provider: LLMProvider,
model: str,
embedding_runtime: ProviderRuntime | None = None,
thinking_enabled: bool | None = None,
top_k: int = 12,
) -> SkillAssemblyResult:
candidates = self.loader.build_selection_candidates()
@ -71,15 +81,39 @@ class SkillAssembler:
)
if not candidates:
return SkillAssemblyResult()
llm_interactions: list[dict[str, Any]] = []
if len(candidates) <= self.max_detailed_candidates:
shortlisted_names = [item["name"] for item in candidates]
else:
shortlisted_names = await self._select_skill_names(
task_description=task_description,
candidates=candidates,
provider=provider,
model=model,
thinking_enabled=thinking_enabled,
max_selected=self.max_detailed_candidates,
selection_stage="shortlist",
llm_interactions=llm_interactions,
)
if not shortlisted_names:
return SkillAssemblyResult(llm_interactions=llm_interactions)
detailed_candidates = self._build_detailed_candidates(
candidates=candidates,
selected_names=shortlisted_names,
)
selected_names = await self._select_skill_names(
task_description=task_description,
candidates=candidates,
candidates=detailed_candidates,
provider=provider,
model=model,
thinking_enabled=thinking_enabled,
selection_stage="final",
llm_interactions=llm_interactions,
)
if not selected_names:
return SkillAssemblyResult()
return SkillAssemblyResult(llm_interactions=llm_interactions)
activated_skills: list[SkillContext] = []
for name in selected_names:
@ -99,7 +133,7 @@ class SkillAssembler:
)
)
return SkillAssemblyResult(activated_skills=activated_skills)
return SkillAssemblyResult(activated_skills=activated_skills, llm_interactions=llm_interactions)
async def _select_skill_names(
self,
@ -108,17 +142,28 @@ class SkillAssembler:
candidates: list[dict[str, str]],
provider: LLMProvider,
model: str,
thinking_enabled: bool | None = None,
max_selected: int | None = None,
selection_stage: str = "final",
llm_interactions: list[dict[str, Any]] | None = None,
timeout_seconds: float = 8.0,
) -> list[str]:
candidate_summary = self._render_candidates(candidates)
candidate_names = {item["name"] for item in candidates}
selection_instruction = (
f"Return at most {max_selected} names for detailed inspection. "
if max_selected is not None
else "Return the final skill names to activate. "
)
messages = [
{
"role": "system",
"content": (
"You select Beaver skills for a single run. "
"Given a task description and candidate skill summaries, "
"Given a task description and candidate skill information, "
"return only a JSON array of skill names to activate. "
"Do not invent names. If nothing matches, return []."
"Do not invent names. If nothing matches, return []. "
f"Selection stage: {selection_stage}. {selection_instruction}"
),
},
{
@ -130,13 +175,34 @@ class SkillAssembler:
),
},
]
response = await provider.chat(
messages=messages,
tools=None,
model=model,
max_tokens=512,
temperature=0,
)
chat_kwargs: dict[str, Any] = {
"messages": messages,
"tools": None,
"model": model,
"max_tokens": 256,
"temperature": 0,
}
if thinking_enabled is not None:
chat_kwargs["thinking_enabled"] = thinking_enabled
try:
response = await asyncio.wait_for(provider.chat(**chat_kwargs), timeout=timeout_seconds)
except Exception:
return []
if llm_interactions is not None:
llm_interactions.append(
{
"stage": selection_stage,
"model": model,
"messages": messages,
"response": {
"content": response.content,
"finish_reason": response.finish_reason,
"provider_name": response.provider_name,
"model": response.model,
"usage": response.usage,
},
}
)
if response.finish_reason == "error" or not response.content:
return []
@ -149,15 +215,42 @@ class SkillAssembler:
for name in parsed:
if name in candidate_names and name not in filtered:
filtered.append(name)
return filtered
return filtered[:max_selected] if max_selected is not None else filtered
@staticmethod
def _render_candidates(candidates: list[dict[str, str]]) -> str:
lines: list[str] = []
for item in candidates:
lines.append(f"- {item['name']}: {item['description']}")
content = item.get("content")
if content:
lines.append(
f"## {item['name']}\n"
f"Description: {item['description']}\n"
f"Skill content:\n{content}"
)
else:
lines.append(f"- {item['name']}: {item['description']}")
return "\n".join(lines)
def _build_detailed_candidates(
self,
*,
candidates: list[dict[str, str]],
selected_names: list[str],
) -> list[dict[str, str]]:
by_name = {item["name"]: item for item in candidates}
detailed: list[dict[str, str]] = []
for name in selected_names:
candidate = by_name.get(name)
if candidate is None:
continue
raw_content = self.loader.load_published_skill(name)
content = strip_frontmatter(raw_content).strip() if raw_content else ""
if len(content) > self.max_candidate_content_chars:
content = content[: self.max_candidate_content_chars].rstrip() + "\n...[truncated]"
detailed.append({**candidate, "content": content})
return detailed
@staticmethod
def _parse_selected_names(content: str) -> list[str]:
cleaned = content.strip()

View File

@ -244,12 +244,10 @@ class SkillsLoader:
meta_blob = parse_skill_metadata_blob(frontmatter.get("metadata", ""))
available = check_requirements(meta_blob)
description = frontmatter.get("description") or record.description or record.name
load_hint = f'Use skill_view(name="{record.name}") to load the full skill.'
lines.append(f' <skill available="{str(available).lower()}">')
lines.append(f" <name>{escape_xml(record.name)}</name>")
lines.append(f" <description>{escape_xml(description)}</description>")
lines.append(f" <version>{escape_xml(record.version)}</version>")
lines.append(f" <load_hint>{escape_xml(load_hint)}</load_hint>")
support_files = self.list_skill_supporting_files(record.name)
if support_files:
lines.append(" <supporting_files>")

View File

@ -124,6 +124,9 @@ class DraftService:
def get_draft(self, skill_name: str, draft_id: str) -> SkillDraft | None:
return self.store.read_draft(skill_name, draft_id)
def delete_draft(self, skill_name: str, draft_id: str) -> bool:
return self.store.delete_draft(skill_name, draft_id)
def _utc_now() -> str:
from datetime import datetime, timezone

View File

@ -2,7 +2,12 @@
from .evidence import EvidencePacket, EvidenceSelector
from .eval import SkillDraftEvaluator
from .missing_skill import MissingSkillDraftResult, MissingSkillSynthesizer
from .missing_skill import (
EphemeralGuidanceResult,
EphemeralGuidanceSynthesizer,
MissingSkillDraftResult,
MissingSkillSynthesizer,
)
from .pipeline import SkillLearningPipelineService
from .service import RunReceiptContext, SkillLearningService
from .synthesizer import SkillDraftSynthesizer
@ -12,6 +17,8 @@ __all__ = [
"EvidencePacket",
"EvidenceSelector",
"SkillDraftEvaluator",
"EphemeralGuidanceResult",
"EphemeralGuidanceSynthesizer",
"MissingSkillDraftResult",
"MissingSkillSynthesizer",
"RunReceiptContext",

View File

@ -1,4 +1,4 @@
"""Synthesize draft-only skills for missing sub-agent guidance."""
"""Synthesize ephemeral guidance for missing sub-agent skills."""
from __future__ import annotations
@ -6,11 +6,10 @@ import json
import re
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from uuid import uuid4
from beaver.engine.context import SkillContext
from beaver.engine.providers import ProviderBundle
from beaver.skills.drafts import DraftService
from beaver.skills.specs import SkillDraft
from beaver.skills.specs.serialization import canonical_hash
if TYPE_CHECKING:
@ -18,13 +17,14 @@ if TYPE_CHECKING:
@dataclass(slots=True)
class MissingSkillDraftResult:
draft: SkillDraft
class EphemeralGuidanceResult:
guidance_id: str
guidance_name: str
skill_context: SkillContext
class MissingSkillSynthesizer:
"""Create a draft skill and an ephemeral SkillContext for the current run."""
class EphemeralGuidanceSynthesizer:
"""Create one-run guidance for the current delegated sub-agent."""
async def synthesize(
self,
@ -37,8 +37,7 @@ class MissingSkillSynthesizer:
skill_query: str,
required_capabilities: list[str],
provider_bundle: ProviderBundle,
draft_service: DraftService,
) -> MissingSkillDraftResult:
) -> EphemeralGuidanceResult:
provider = provider_bundle.auxiliary_provider or provider_bundle.main_provider
runtime = provider_bundle.auxiliary_runtime or provider_bundle.main_runtime
model = getattr(runtime, "model", None)
@ -49,14 +48,14 @@ class MissingSkillSynthesizer:
{
"role": "system",
"content": (
"You create concise Beaver skill drafts. Return only JSON with keys: "
"skill_name, description, content, tags."
"You create concise Beaver ephemeral guidance. Return only JSON with keys: "
"guidance_name, description, content, tags."
),
},
{
"role": "user",
"content": (
"Create a procedural skill draft for this missing Task sub-agent guidance.\n\n"
"Create procedural guidance for this missing Task sub-agent capability.\n\n"
f"Task goal:\n{task.goal}\n\n"
f"Current user request:\n{user_message}\n\n"
f"Node id: {node_id}\n"
@ -64,62 +63,37 @@ class MissingSkillSynthesizer:
f"Skill query:\n{skill_query}\n"
f"Required capabilities: {required_capabilities}\n\n"
"The content must be actionable guidance for a temporary sub-agent. "
"Do not include implementation claims or publish metadata."
"Do not include implementation claims, review metadata, or publish metadata."
),
},
],
tools=None,
model=model,
max_tokens=1200,
max_tokens=4096,
temperature=0,
)
payload = self._parse_payload(response.content or "") or payload
except Exception:
payload = payload
skill_name = _slug(str(payload.get("skill_name") or skill_query or node_id))
guidance_name = _slug(str(payload.get("guidance_name") or payload.get("skill_name") or skill_query or node_id))
guidance_id = f"eg_{uuid4().hex}"
content = str(payload.get("content") or "").strip()
if not content:
content = str(self._fallback_payload(skill_query=skill_query, node_task=node_task, capabilities=required_capabilities)["content"])
frontmatter = {
"description": str(payload.get("description") or f"Draft guidance for {skill_query or node_id}").strip(),
"tags": [str(item) for item in payload.get("tags") or ["generated", "task-sub-agent"]],
"metadata": {
"origin": "missing_task_subagent_skill",
"task_id": task.task_id,
"node_id": node_id,
"attempt_index": attempt_index,
"skill_query": skill_query,
"required_capabilities": list(required_capabilities),
},
}
draft = draft_service.create_new_skill_draft(
skill_name=skill_name,
proposed_content=content,
proposed_frontmatter=frontmatter,
created_by="task-skill-resolver",
reason="generated_for_missing_task_subagent_skill",
trigger_session_id=task.session_id,
evidence_refs=[
{
"task_id": task.task_id,
"session_id": task.session_id,
"attempt_index": attempt_index,
"node_id": node_id,
"skill_query": skill_query,
"required_capabilities": list(required_capabilities),
}
],
)
context = SkillContext(
name=f"draft:{draft.skill_name}",
content=draft.proposed_content,
version=f"draft:{draft.draft_id}",
content_hash=canonical_hash(draft.proposed_content),
activation_reason="generated_missing_skill",
name=f"ephemeral:{guidance_name}",
content=content,
version=f"ephemeral:{guidance_id}",
content_hash=canonical_hash(content),
activation_reason="ephemeral_guidance",
tool_hints=[],
)
return MissingSkillDraftResult(draft=draft, skill_context=context)
return EphemeralGuidanceResult(
guidance_id=guidance_id,
guidance_name=guidance_name,
skill_context=context,
)
@staticmethod
def _parse_payload(text: str) -> dict[str, Any] | None:
@ -145,7 +119,7 @@ class MissingSkillSynthesizer:
title = skill_query or node_task or "task subagent guidance"
capability_lines = "\n".join(f"- {item}" for item in capabilities) or "- Follow the node task precisely."
return {
"skill_name": _slug(title),
"guidance_name": _slug(title),
"description": f"Draft guidance for {title}.",
"tags": ["generated", "task-sub-agent"],
"content": (
@ -163,4 +137,8 @@ class MissingSkillSynthesizer:
def _slug(value: str) -> str:
cleaned = re.sub(r"[^a-zA-Z0-9]+", "-", value.strip().lower()).strip("-")
return cleaned[:64].strip("-") or "generated-task-subagent-skill"
return cleaned[:64].strip("-") or "generated-task-subagent-guidance"
MissingSkillDraftResult = EphemeralGuidanceResult
MissingSkillSynthesizer = EphemeralGuidanceSynthesizer

View File

@ -14,6 +14,12 @@ from beaver.skills.publisher import SkillPublisher
from beaver.skills.reviews import ReviewService
from beaver.skills.specs import SkillDraft, SkillReviewRecord, SkillReviewState, SkillSpec, SkillVersion
_REJECTABLE_DRAFT_STATUSES = {
SkillReviewState.DRAFT.value,
SkillReviewState.IN_REVIEW.value,
SkillReviewState.APPROVED.value,
}
class SkillLearningPipelineService:
"""Coordinates candidate -> draft -> review -> publish lifecycle."""
@ -161,6 +167,9 @@ class SkillLearningPipelineService:
requested_by: str = "system",
notes: str = "",
) -> SkillReviewRecord:
draft = self.get_draft(skill_name, draft_id)
if draft.status != SkillReviewState.DRAFT.value:
raise ValueError("Draft must be in draft status before review submission")
safety = self.get_safety_report(skill_name, draft_id)
if safety is not None and (not safety.passed or safety.risk_level == "critical"):
raise ValueError("Draft cannot enter review because safety check failed")
@ -179,6 +188,12 @@ class SkillLearningPipelineService:
reviewer: str = "system",
notes: str = "",
) -> SkillReviewRecord:
draft = self.get_draft(skill_name, draft_id)
if draft.status != SkillReviewState.IN_REVIEW.value:
raise ValueError("Draft must be in review before approval")
safety = self.get_safety_report(skill_name, draft_id)
if safety is not None and (not safety.passed or safety.risk_level == "critical"):
raise ValueError("Draft cannot be approved because safety check failed")
review = self.review_service.approve(skill_name, draft_id, reviewer=reviewer, notes=notes)
self._mark_candidate_by_draft(skill_name, draft_id, "approved", "approved")
return review
@ -191,6 +206,9 @@ class SkillLearningPipelineService:
reviewer: str = "system",
notes: str = "",
) -> SkillReviewRecord:
draft = self.get_draft(skill_name, draft_id)
if draft.status not in _REJECTABLE_DRAFT_STATUSES:
raise ValueError("Draft is not rejectable from its current status")
review = self.review_service.reject(skill_name, draft_id, reviewer=reviewer, notes=notes)
self._mark_candidate_by_draft(skill_name, draft_id, "rejected", "rejected")
return review

View File

@ -69,6 +69,94 @@ class SkillLearningService:
existing_ids.add(candidate.candidate_id)
return candidates
def build_learning_candidates_for_task(self, task_id: str, *, trigger_run_id: str) -> list[SkillLearningCandidate]:
"""Build candidates scoped to a single validated and satisfied Task run."""
runs = [record for record in self.run_store.list_runs() if record.task_id == task_id]
trigger_run = next((record for record in runs if record.run_id == trigger_run_id), None)
if trigger_run is None or not self._is_confirmed_positive_run(trigger_run):
return []
source_runs = [record for record in runs if self._is_confirmed_positive_run(record)]
if not source_runs:
return []
candidates: list[SkillLearningCandidate] = []
published_receipts = [
receipt
for record in source_runs
for receipt in record.activated_skills
if self._is_published_skill_receipt(receipt)
]
source_run_ids = [record.run_id for record in source_runs]
source_session_ids = list(dict.fromkeys(record.session_id for record in source_runs))
if not published_receipts:
candidates.append(
SkillLearningCandidate(
candidate_id=f"new:task:{task_id}",
kind="new_skill",
source_run_ids=source_run_ids,
source_session_ids=source_session_ids,
related_skill_names=[],
reason=f"Task {task_id} completed successfully without a published skill; consider extracting reusable guidance.",
evidence={"task_id": task_id, "trigger_run_id": trigger_run_id, "theme": self._task_theme(trigger_run.task_text)},
status="open",
priority=1,
confidence=0.8,
trigger_reason="validation_accepted_and_user_satisfied",
)
)
else:
seen: set[tuple[str, str]] = set()
for receipt in published_receipts:
key = (receipt.skill_name, receipt.skill_version)
if key in seen:
continue
seen.add(key)
skill_runs = [
record
for record in source_runs
if any(
item.skill_name == receipt.skill_name
and item.skill_version == receipt.skill_version
and self._is_published_skill_receipt(item)
for item in record.activated_skills
)
]
candidates.append(
SkillLearningCandidate(
candidate_id=f"revise:{receipt.skill_name}:{receipt.skill_version}:task:{task_id}",
kind="revise_skill",
source_run_ids=[record.run_id for record in skill_runs],
source_session_ids=list(dict.fromkeys(record.session_id for record in skill_runs)),
related_skill_names=[receipt.skill_name],
reason=(
f"Task {task_id} succeeded with published skill "
f"{receipt.skill_name}/{receipt.skill_version}; consider whether the skill should capture this evidence."
),
evidence={
"task_id": task_id,
"trigger_run_id": trigger_run_id,
"skill_version": receipt.skill_version,
},
status="open",
priority=1,
confidence=0.7,
trigger_reason="validation_accepted_and_user_satisfied",
)
)
existing_ids = {item.candidate_id for item in self.learning_store.list_learning_candidates()}
created: list[SkillLearningCandidate] = []
for candidate in candidates:
if candidate.candidate_id in existing_ids:
continue
self.learning_store.record_learning_candidate(candidate)
existing_ids.add(candidate.candidate_id)
created.append(candidate)
return created
async def synthesize_draft(self, candidate_id: str, provider_bundle: ProviderBundle) -> Any:
candidates = {item.candidate_id: item for item in self.learning_store.list_learning_candidates()}
candidate = candidates.get(candidate_id)
@ -181,7 +269,7 @@ class SkillLearningService:
groups.setdefault(key, []).append(record)
candidates: list[SkillLearningCandidate] = []
for theme, runs in groups.items():
successful = [record for record in runs if record.success]
successful = [record for record in runs if self._is_confirmed_positive_run(record)]
if len(successful) < 2:
continue
if any(record.activated_skills for record in successful):
@ -202,6 +290,8 @@ class SkillLearningService:
def _build_merge_candidates(self) -> list[SkillLearningCandidate]:
pair_counts: dict[tuple[str, str], list[RunRecord]] = {}
for record in self.run_store.list_runs():
if not self._is_confirmed_positive_run(record):
continue
unique = sorted({receipt.skill_name for receipt in record.activated_skills})
for pair in combinations(unique, 2):
pair_counts.setdefault(pair, []).append(record)
@ -260,6 +350,25 @@ class SkillLearningService:
effects.extend(self.run_store.list_skill_effects(receipt.skill_name, version=receipt.skill_version))
return effects
@staticmethod
def _is_confirmed_positive_run(record: RunRecord) -> bool:
validation = record.validation_result or {}
feedback = record.feedback or {}
return (
bool(record.success)
and bool(record.task_id)
and validation.get("accepted") is True
and feedback.get("feedback_type") == "satisfied"
)
@staticmethod
def _is_published_skill_receipt(receipt: SkillActivationReceipt) -> bool:
return (
not receipt.skill_name.startswith(("draft:", "ephemeral:"))
and not receipt.skill_version.startswith(("draft:", "ephemeral:"))
and receipt.activation_reason not in {"generated_missing_skill", "ephemeral_guidance"}
)
@staticmethod
def _candidate_id(kind: str, *parts: str) -> str:
return f"{kind}:{'|'.join(parts)}"

View File

@ -60,7 +60,7 @@ class SkillDraftSynthesizer:
],
tools=None,
model=model,
max_tokens=1500,
max_tokens=4096,
temperature=0,
)
payload = self._parse_payload(response.content or "")

View File

@ -2,6 +2,9 @@
from __future__ import annotations
import shutil
from pathlib import Path
from beaver.skills.catalog.utils import strip_frontmatter
from beaver.skills.specs import SkillDraft, SkillReviewState, SkillSpec, SkillSpecStore, SkillStatus, SkillVersion
from beaver.skills.specs.serialization import canonical_hash, normalize_frontmatter, summarize_skill_content
@ -44,6 +47,7 @@ class SkillPublisher:
},
)
self.store.write_skill_version(version, content)
self._copy_uploaded_supporting_files(draft, next_version)
self.store.set_current_version(skill_name, next_version)
spec = self.store.get_skill_spec(skill_name)
@ -169,6 +173,27 @@ class SkillPublisher:
self.store.update_index("published", published)
self.store.update_index("disabled", disabled)
def _copy_uploaded_supporting_files(self, draft: SkillDraft, version: str) -> None:
for evidence in draft.evidence_refs:
if not isinstance(evidence, dict) or evidence.get("kind") != "upload":
continue
raw_dir = evidence.get("supporting_upload_dir")
if not raw_dir:
continue
source_root = Path(str(raw_dir))
if not source_root.exists() or not source_root.is_dir():
continue
target_root = self.store.root / draft.skill_name / "versions" / version
for source in sorted(source_root.rglob("*")):
if not source.is_file() or source.is_symlink():
continue
relative = source.relative_to(source_root)
if any(part in {"", ".", ".."} for part in relative.parts):
continue
target = target_root / relative
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(source, target)
def _require_draft(self, skill_name: str, draft_id: str) -> SkillDraft:
draft = self.store.read_draft(skill_name, draft_id)
if draft is None:

View File

@ -47,8 +47,6 @@ class ReviewService:
def reject(self, skill_name: str, draft_id: str, reviewer: str, notes: str = "") -> SkillReviewRecord:
draft = self._require_draft(skill_name, draft_id)
draft.status = SkillReviewState.REJECTED.value
self.store.write_draft(draft)
review = SkillReviewRecord(
review_id=uuid4().hex,
draft_id=draft_id,
@ -61,6 +59,7 @@ class ReviewService:
notes=notes,
)
self.store.write_review(review)
self.store.delete_draft(skill_name, draft_id)
return review
def _require_draft(self, skill_name: str, draft_id: str) -> SkillDraft:

View File

@ -87,6 +87,11 @@ class SkillSpecStore:
return str(self._read_json(current_path).get("current_version") or "") or None
if (directory / "SKILL.md").exists():
return "legacy"
versions_dir = directory / "versions"
if versions_dir.exists():
versions = [child.name for child in sorted(versions_dir.iterdir()) if child.is_dir()]
if versions:
return versions[-1]
spec = self.get_skill_spec(name)
if spec is not None and spec.current_version:
return spec.current_version
@ -182,6 +187,13 @@ class SkillSpecStore:
drafts_dir.mkdir(parents=True, exist_ok=True)
self._write_json(drafts_dir / f"draft-{draft.draft_id}.json", draft.to_dict())
def delete_draft(self, skill_name: str, draft_id: str) -> bool:
path = self._skill_dir(skill_name) / "drafts" / f"draft-{draft_id}.json"
if not path.exists():
return False
path.unlink()
return True
def list_reviews(self, skill_name: str, draft_id: str | None = None) -> list[SkillReviewRecord]:
reviews_dir = self._skill_dir(skill_name) / "reviews"
if not reviews_dir.exists():
@ -199,6 +211,19 @@ class SkillSpecStore:
reviews_dir.mkdir(parents=True, exist_ok=True)
self._write_json(reviews_dir / f"review-{review.review_id}.json", review.to_dict())
def delete_reviews_for_draft(self, skill_name: str, draft_id: str) -> int:
reviews_dir = self._skill_dir(skill_name) / "reviews"
if not reviews_dir.exists():
return 0
deleted = 0
for path in sorted(reviews_dir.glob("review-*.json")):
record = SkillReviewRecord.from_dict(self._read_json(path))
if record.draft_id != draft_id:
continue
path.unlink()
deleted += 1
return deleted
def update_index(self, index_name: str, values: list[str]) -> None:
self._write_json(self.index_dir / f"{index_name}.json", {"items": list(dict.fromkeys(values))})