Files
Agentswarm/scripts/test-run-isolation.py
gongzhiyongandClaude Opus 4.8 c257e7e562 feat(orchestrator): P0 run 边界隔离 — 蜂后防跨 run 抢夺(agent_swarm#8)
run-boundary isolation:agent 只能竞争/认领属于自己 run 的 task,杜绝跨 run 抢夺
(A run 的 agent 抢 B run 的 task → 计费错账/结果污染)。
- 新增 extract_swarm_from_agent / _agent_belongs_to_run(复用 -agent- 前缀)
- swarm_dispatch:候选过滤为本 run 的 task
- handle_task_bid/yield/takeover:拒绝跨 run 请求(cross_run_denied)
- 测试 scripts/test-run-isolation.py(7 检查全过)

影响:仅 orchestrator 派发/竞争路径;不涉及 Manager 契约/计费字段/发布链路。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 18:42:39 +08:00

49 lines
1.8 KiB
Python

"""P0 run-boundary isolation (agent_swarm#8 / Queen anti-grab).
Verifies the Queen's run-ownership guard: a launcher-minted agent is bound to its own run by the
`{swarm_id}-agent-{N}` id prefix, so cross-run task grab (bid/yield/takeover/self-select) is denied.
Externally-supplied agents (no resolvable prefix) keep the global behavior (fail-soft).
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault("REDIS_FAKE", "1")
os.environ.setdefault("ALLOW_MEMORY_STORE", "1")
os.environ.setdefault("OPENAI_API_KEY", "test-key")
from orchestrator.main import extract_swarm_from_agent, _agent_belongs_to_run
_failures = []
def check(name, cond):
print(("PASS" if cond else "FAIL") + " - " + name)
if not cond:
_failures.append(name)
class _Run:
def __init__(self, swarm_id):
self.swarm_id = swarm_id
# extract_swarm_from_agent: strip the -agent-N suffix
check("extract swarm prefix", extract_swarm_from_agent("swarm-abc-agent-1") == "swarm-abc")
check("extract multi-digit N", extract_swarm_from_agent("swarm-xyz123-agent-16") == "swarm-xyz123")
check("extract None for external id", extract_swarm_from_agent("external-worker") is None)
check("extract None for empty", extract_swarm_from_agent("") is None)
run_a = _Run("swarm-abc")
# same run → allowed
check("own-run agent allowed", _agent_belongs_to_run("swarm-abc-agent-3", run_a) is True)
# cross run → DENIED (the core anti-grab guarantee)
check("cross-run agent denied", _agent_belongs_to_run("swarm-xyz-agent-1", run_a) is False)
# external (unresolvable prefix) → fail-soft allow
check("external agent fail-soft allowed", _agent_belongs_to_run("external-worker", run_a) is True)
if _failures:
print(f"\nFAILED: {len(_failures)} check(s): {_failures}")
raise SystemExit(1)
print("\nALL PASSED")