[claude] feat: enforce 3-issue cap on Kimi delegation (#1304) (#1310)

This commit is contained in:
2026-03-24 02:00:34 +00:00
parent 8304cf50da
commit 0b4ed1b756
2 changed files with 247 additions and 0 deletions

View File

@@ -28,6 +28,9 @@ KIMI_READY_LABEL = "kimi-ready"
# Label colour for the kimi-ready label (dark teal)
KIMI_LABEL_COLOR = "#006b75"
# Maximum number of concurrent active (open) Kimi-delegated issues
KIMI_MAX_ACTIVE_ISSUES = 3
# Keywords that suggest a task exceeds local capacity
_HEAVY_RESEARCH_KEYWORDS = frozenset(
{
@@ -176,6 +179,38 @@ async def _get_or_create_label(
return None
async def _count_active_kimi_issues(
client: Any,
base_url: str,
headers: dict[str, str],
repo: str,
) -> int:
"""Count open issues that carry the `kimi-ready` label.
Args:
client: httpx.AsyncClient instance.
base_url: Gitea API base URL.
headers: Auth headers.
repo: owner/repo string.
Returns:
Number of open kimi-ready issues, or 0 on error (fail-open to avoid
blocking delegation when Gitea is unreachable).
"""
try:
resp = await client.get(
f"{base_url}/repos/{repo}/issues",
headers=headers,
params={"state": "open", "type": "issues", "labels": KIMI_READY_LABEL, "limit": 50},
)
if resp.status_code == 200:
return len(resp.json())
logger.warning("count_active_kimi_issues: unexpected status %s", resp.status_code)
except Exception as exc:
logger.warning("count_active_kimi_issues failed: %s", exc)
return 0
async def create_kimi_research_issue(
task: str,
context: str,
@@ -217,6 +252,22 @@ async def create_kimi_research_issue(
async with httpx.AsyncClient(timeout=15) as client:
label_id = await _get_or_create_label(client, base_url, headers, repo)
active_count = await _count_active_kimi_issues(client, base_url, headers, repo)
if active_count >= KIMI_MAX_ACTIVE_ISSUES:
logger.warning(
"Kimi delegation cap reached (%d/%d active) — skipping: %s",
active_count,
KIMI_MAX_ACTIVE_ISSUES,
task[:60],
)
return {
"success": False,
"error": (
f"Kimi delegation cap reached: {active_count} active issues "
f"(max {KIMI_MAX_ACTIVE_ISSUES}). Resolve existing issues first."
),
}
body = _build_research_template(task, context, question, priority)
issue_payload: dict[str, Any] = {"title": task, "body": body}
if label_id is not None: