feat: document and expose swarm runtime contract

This commit is contained in:
gongzhiyong
2026-05-28 11:57:58 +08:00
parent ee5342c404
commit 8075891770
10 changed files with 1025 additions and 37 deletions
+132
View File
@@ -11,6 +11,9 @@ authenticated deployment flow, provide:
Optional:
HEICODE_RUN_SIMULATE=true
HEICODE_RUN_CALLBACK_SMOKE=true
HEICODE_CALLBACK_TOKEN=<Manager callback service token>
HEICODE_CALLBACK_SWARM_ID=<runtime swarm id, optional>
HEICODE_MANAGER_BASE_URL=https://code.xinghanlab.com
AGENT_MANAGER_HEALTH_URL=http://20.212.121.126/api/agnet/health
"""
@@ -31,11 +34,18 @@ AGENT_HEALTH = os.getenv(
ACCESS_TOKEN = os.getenv("HEICODE_ACCESS_TOKEN", "").strip()
USER_ID = os.getenv("HEICODE_USER_ID", "").strip()
DEPLOYMENT_ID = os.getenv("HEICODE_DEPLOYMENT_ID", "").strip()
CALLBACK_TOKEN = os.getenv("HEICODE_CALLBACK_TOKEN", "").strip()
CALLBACK_SWARM_ID = os.getenv("HEICODE_CALLBACK_SWARM_ID", "").strip()
RUN_SIMULATE = os.getenv("HEICODE_RUN_SIMULATE", "").strip().lower() in {
"1",
"true",
"yes",
}
RUN_CALLBACK_SMOKE = os.getenv("HEICODE_RUN_CALLBACK_SMOKE", "").strip().lower() in {
"1",
"true",
"yes",
}
UA = "heicode-agnet-sub-mode-smoke/1.0"
FAILS: list[str] = []
@@ -93,6 +103,23 @@ check("Manager /api/status success", code == 200 and status.get("success") is Tr
version = status.get("data", {}).get("version") if isinstance(status.get("data"), dict) else ""
check("Manager status carries version", bool(version), f"version={version!r}")
code, _headers, body = manager_request("GET", "/api/agnet/callbacks/swarm-events/schema")
schema = parse_json("callback schema JSON parseable", body)
schema_data = schema.get("data", {}) if isinstance(schema.get("data"), dict) else {}
schema_events = schema_data.get("events") or []
schema_event_types = {
item.get("event_type")
for item in schema_events
if isinstance(item, dict)
}
check(
"callback schema lists swarm and ordinary sub events",
code == 200
and schema.get("success") is True
and {"task.claimed", "handoff.requested", "phase.changed", "artifact.created"}.issubset(schema_event_types),
f"HTTP {code}",
)
code, _headers, body = request("GET", AGENT_HEALTH)
agent = parse_json("Agent Manager health JSON parseable", body)
agent_data = agent.get("data", {}) if isinstance(agent.get("data"), dict) else {}
@@ -106,6 +133,91 @@ check(
if not (ACCESS_TOKEN and USER_ID and DEPLOYMENT_ID):
print("\nAuthenticated deployment checks skipped: set HEICODE_ACCESS_TOKEN, HEICODE_USER_ID and HEICODE_DEPLOYMENT_ID.")
else:
if RUN_CALLBACK_SMOKE:
if not CALLBACK_TOKEN:
check("callback smoke has HEICODE_CALLBACK_TOKEN", False)
else:
callback_headers = {
"Content-Type": "application/json",
"X-Agnet-Service-Token": CALLBACK_TOKEN,
"X-Correlation-ID": f"smoke-corr-{DEPLOYMENT_ID}",
}
swarm_id = CALLBACK_SWARM_ID or f"smoke-swarm-{DEPLOYMENT_ID}"
callback_events = [
{
"event_id": f"smoke-task-created-{DEPLOYMENT_ID}",
"event_type": "task.created",
"deployment_id": DEPLOYMENT_ID,
"swarm_id": swarm_id,
"task_id": f"smoke-task-{DEPLOYMENT_ID}",
"source": "smoke-runtime",
"payload": {
"title": "Smoke task created",
"agent_role": "backend",
},
},
{
"event_id": f"smoke-task-claimed-{DEPLOYMENT_ID}",
"event_type": "task.claimed",
"deployment_id": DEPLOYMENT_ID,
"swarm_id": swarm_id,
"task_id": f"smoke-task-{DEPLOYMENT_ID}",
"source": "smoke-runtime",
"payload": {
"agent_role": "backend",
},
},
{
"event_id": f"smoke-handoff-{DEPLOYMENT_ID}",
"event_type": "handoff.requested",
"deployment_id": DEPLOYMENT_ID,
"swarm_id": swarm_id,
"task_id": f"smoke-task-{DEPLOYMENT_ID}",
"source": "smoke-runtime",
"payload": {
"from_role": "backend",
"to_role": "reviewer",
"reason": "Smoke handoff validation",
},
},
{
"event_id": f"smoke-artifact-{DEPLOYMENT_ID}",
"event_type": "artifact.created",
"deployment_id": DEPLOYMENT_ID,
"swarm_id": swarm_id,
"task_id": f"smoke-task-{DEPLOYMENT_ID}",
"source": "smoke-runtime",
"artifact": {
"artifact_id": f"smoke-artifact-{DEPLOYMENT_ID}",
"artifact_type": "test_report",
"title": "Smoke artifact",
"summary": "Callback smoke wrote this artifact through the Manager callback endpoint.",
"uri": f"artifact://smoke/{DEPLOYMENT_ID}/report",
},
"payload": {
"stage": "testing",
"checkpoint": "artifact_ready",
},
},
]
for event in callback_events:
body_bytes = json.dumps(event).encode("utf-8")
callback_headers["X-Agnet-Event-Id"] = event["event_id"]
code, _headers, body = request(
"POST",
BASE + "/api/agnet/callbacks/swarm-events",
headers=callback_headers,
body=body_bytes,
)
callback_response = parse_json(
f"callback {event['event_type']} JSON parseable", body
)
check(
f"callback {event['event_type']} accepted",
code == 200 and callback_response.get("success") is True,
f"HTTP {code}",
)
if RUN_SIMULATE:
code, _headers, body = manager_request(
"POST",
@@ -155,6 +267,11 @@ else:
for item in merged
if isinstance(item, dict)
}
sources = {
item.get("source")
for item in merged
if isinstance(item, dict) and item.get("source")
}
check(
"timeline endpoint success",
code == 200 and timeline.get("success") is True and isinstance(merged, list),
@@ -170,6 +287,21 @@ else:
"artifact.created" in event_types or "approval.requested" in event_types,
"events=" + ",".join(sorted(str(event) for event in event_types if event)[:10]),
)
if RUN_CALLBACK_SMOKE:
check(
"timeline includes smoke callback source",
"smoke-runtime" in sources,
"sources=" + ",".join(sorted(str(source) for source in sources)[:10]),
)
check(
"callback smoke artifact is queryable",
any(
isinstance(item, dict)
and item.get("artifact_id") == f"smoke-artifact-{DEPLOYMENT_ID}"
for item in artifact_items
),
f"artifact_count={len(artifact_items) if isinstance(artifact_items, list) else 'n/a'}",
)
if FAILS:
print(f"\n{len(FAILS)} check(s) failed:")