Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe36cc9241 | ||
|
|
eb4a3adb3f | ||
|
|
f1ebd2cf99 | ||
|
|
cb3b5f1e01 | ||
|
|
6fc538bf50 | ||
|
|
665c33f69a | ||
|
|
880591fc00 | ||
|
|
68060fbcba | ||
|
|
db1ebc29e0 | ||
|
|
93b8709c36 | ||
|
|
7900c4df2e | ||
|
|
8b6fb1c06e | ||
|
|
de8b996dea | ||
|
|
eb8d229f28 |
@@ -10,3 +10,4 @@ node_modules/
|
||||
runtime/tmp/
|
||||
runtime/cache/
|
||||
logs/
|
||||
config/test_write_perms
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""API adapter for agent context routing and visibility."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
try:
|
||||
from fastapi import APIRouter
|
||||
except Exception:
|
||||
APIRouter = None
|
||||
|
||||
from .agent_context_dashboard import AgentContextDashboard
|
||||
from .agent_context_rbac import AgentContextRBAC
|
||||
from .agent_context_resolver import AgentContextResolver
|
||||
|
||||
resolver = AgentContextResolver()
|
||||
rbac = AgentContextRBAC()
|
||||
dashboard = AgentContextDashboard()
|
||||
|
||||
|
||||
def resolve_agent_context(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
route = rbac.route(payload["user_id"], payload.get("group_id"), payload.get("role", "user"))
|
||||
context = resolver.resolve(
|
||||
user_id=payload["user_id"],
|
||||
group_id=payload.get("group_id", ""),
|
||||
session=payload.get("session"),
|
||||
user_profile=payload.get("user_profile"),
|
||||
group_policy=payload.get("group_policy"),
|
||||
agent_default=payload.get("agent_default"),
|
||||
owner_policy=payload.get("owner_policy"),
|
||||
owner=payload.get("role") == "owner",
|
||||
)
|
||||
return {"routing": route.to_dict(), "context": context.to_dict()}
|
||||
|
||||
|
||||
def get_context_dashboard() -> dict[str, Any]:
|
||||
return dashboard.overview()
|
||||
|
||||
|
||||
def build_router():
|
||||
if APIRouter is None:
|
||||
return None
|
||||
router = APIRouter(prefix="/admin/ai-evolution/context", tags=["context-isolation"])
|
||||
|
||||
@router.post("/resolve", operation_id="resolveAgentContext")
|
||||
def resolve(payload: dict[str, Any]):
|
||||
return resolve_agent_context(payload)
|
||||
|
||||
@router.get("/overview", operation_id="getAgentContextDashboard")
|
||||
def overview():
|
||||
return get_context_dashboard()
|
||||
|
||||
return router
|
||||
|
||||
|
||||
router = build_router()
|
||||
|
||||
__all__ = ["router", "resolve_agent_context", "get_context_dashboard"]
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Audit context resolution decisions."""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
|
||||
def utc_now():
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
class AgentContextAudit:
|
||||
def record(self, context: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"event": "context_loaded",
|
||||
"agent_id": context.get("agent_id"),
|
||||
"user_id": context.get("user_id"),
|
||||
"group_id": context.get("group_id"),
|
||||
"layers": [x.get("name") for x in context.get("layers", [])],
|
||||
"timestamp": utc_now(),
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["AgentContextAudit"]
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Web UI model for context isolation visibility."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
class AgentContextDashboard:
|
||||
def overview(self, contexts: list[dict[str, Any]] | None = None) -> dict[str, Any]:
|
||||
contexts = contexts or []
|
||||
return {
|
||||
"module": "agent-context-isolation",
|
||||
"route": "/admin/ai-evolution/context",
|
||||
"title": "Agent Context Isolation",
|
||||
"panels": [
|
||||
{
|
||||
"name": "agent-routing",
|
||||
"title": "Agent 路由",
|
||||
"data": contexts,
|
||||
},
|
||||
{
|
||||
"name": "context-layers",
|
||||
"title": "Context 来源层级",
|
||||
"layers": [
|
||||
"safety_policy",
|
||||
"owner_policy",
|
||||
"group_policy",
|
||||
"role_policy",
|
||||
"user_profile",
|
||||
"session_context",
|
||||
"agent_default",
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "audit",
|
||||
"title": "Context 审计",
|
||||
"events": [
|
||||
"context_loaded",
|
||||
"profile_updated",
|
||||
"agent_selected",
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["AgentContextDashboard"]
|
||||
@@ -0,0 +1,55 @@
|
||||
"""RBAC binding for group main agents and owner agents."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict, dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AgentRoutingDecision:
|
||||
user_id: str
|
||||
group_id: str
|
||||
role: str
|
||||
agent_id: str
|
||||
scope: str
|
||||
allowed: bool
|
||||
reason: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
class AgentContextRBAC:
|
||||
"""Routes users to group agents and owners to dedicated owner agents."""
|
||||
|
||||
VALID_ROLES = {"user", "group_admin", "owner"}
|
||||
|
||||
def route(self, user_id: str, group_id: str | None, role: str) -> AgentRoutingDecision:
|
||||
if role not in self.VALID_ROLES:
|
||||
return AgentRoutingDecision(user_id, group_id or "", role, "", "none", False, "unknown_role")
|
||||
if role == "owner":
|
||||
return AgentRoutingDecision(
|
||||
user_id=user_id,
|
||||
group_id=group_id or "global",
|
||||
role=role,
|
||||
agent_id=f"owner-agent:{user_id}",
|
||||
scope="global",
|
||||
allowed=True,
|
||||
reason="owner_dedicated_agent",
|
||||
)
|
||||
if not group_id:
|
||||
return AgentRoutingDecision(user_id, "", role, "", "none", False, "group_required")
|
||||
scope = "group" if role == "group_admin" else "self"
|
||||
return AgentRoutingDecision(
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
role=role,
|
||||
agent_id=f"group-main-agent:{group_id}",
|
||||
scope=scope,
|
||||
allowed=True,
|
||||
reason="group_agent_routing",
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["AgentRoutingDecision", "AgentContextRBAC"]
|
||||
@@ -0,0 +1,82 @@
|
||||
"""Multi-user agent context resolver.
|
||||
|
||||
Architecture:
|
||||
- shared agent kernel
|
||||
- group main agent
|
||||
- owner dedicated agent
|
||||
- user profile overlay
|
||||
- session overlay
|
||||
|
||||
Personal preferences never enter shared USER.md.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
|
||||
def utc_now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ContextLayer:
|
||||
name: str
|
||||
priority: int
|
||||
data: dict[str, Any]
|
||||
|
||||
|
||||
@dataclass
|
||||
class EffectiveContext:
|
||||
agent_id: str
|
||||
user_id: str
|
||||
group_id: str
|
||||
layers: list[ContextLayer]
|
||||
resolved: dict[str, Any]
|
||||
resolved_at: str = field(default_factory=utc_now)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"agent_id": self.agent_id,
|
||||
"user_id": self.user_id,
|
||||
"group_id": self.group_id,
|
||||
"layers": [asdict(x) for x in self.layers],
|
||||
"resolved": self.resolved,
|
||||
"resolved_at": self.resolved_at,
|
||||
}
|
||||
|
||||
|
||||
class AgentContextResolver:
|
||||
"""Resolves context without shared user-state pollution."""
|
||||
|
||||
def resolve(
|
||||
self,
|
||||
user_id: str,
|
||||
group_id: str,
|
||||
session: dict[str, Any] | None = None,
|
||||
user_profile: dict[str, Any] | None = None,
|
||||
group_policy: dict[str, Any] | None = None,
|
||||
agent_default: dict[str, Any] | None = None,
|
||||
owner_policy: dict[str, Any] | None = None,
|
||||
owner: bool = False,
|
||||
) -> EffectiveContext:
|
||||
layers = [
|
||||
ContextLayer("agent_default", 10, agent_default or {}),
|
||||
ContextLayer("group_policy", 30, group_policy or {}),
|
||||
ContextLayer("user_profile", 50, user_profile or {}),
|
||||
ContextLayer("session", 70, session or {}),
|
||||
]
|
||||
agent_id = f"group-main-agent:{group_id}"
|
||||
if owner:
|
||||
agent_id = f"owner-agent:{user_id}"
|
||||
layers.append(ContextLayer("owner_policy", 90, owner_policy or {}))
|
||||
resolved: dict[str, Any] = {}
|
||||
# Higher priority overlays lower priority except safety policies handled upstream.
|
||||
for layer in sorted(layers, key=lambda x: x.priority):
|
||||
resolved.update(layer.data)
|
||||
return EffectiveContext(agent_id, user_id, group_id, layers, resolved)
|
||||
|
||||
|
||||
__all__ = ["ContextLayer", "EffectiveContext", "AgentContextResolver"]
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Bridge Agent Event Bus events into Agent Observability metrics."""
|
||||
|
||||
from .agent_observability_hooks import AgentObservabilityHooks
|
||||
|
||||
|
||||
class AgentEventObservabilityBridge:
|
||||
def __init__(self, hooks: AgentObservabilityHooks | None = None):
|
||||
self.hooks = hooks or AgentObservabilityHooks()
|
||||
|
||||
def consume(self, event: dict):
|
||||
kind = event.get("kind")
|
||||
payload = event.get("payload", {})
|
||||
if kind == "agent_started":
|
||||
return self.hooks.start(
|
||||
user_id=payload.get("user_id", "unknown"),
|
||||
visible_scope=payload.get("scope", "self"),
|
||||
entry_agent=payload.get("agent", "unknown"),
|
||||
execution_id=event.get("id"),
|
||||
)
|
||||
if kind == "agent_delegated":
|
||||
return self.hooks.delegate(event["execution_id"], payload.get("agent", "unknown"))
|
||||
if kind == "token_consumed":
|
||||
return self.hooks.tokens(
|
||||
event["execution_id"],
|
||||
payload.get("input_tokens", 0),
|
||||
payload.get("output_tokens", 0),
|
||||
)
|
||||
if kind in {"agent_completed", "agent_failed"}:
|
||||
return self.hooks.finish(
|
||||
event["execution_id"],
|
||||
payload.get("closed_by"),
|
||||
"failed" if kind == "agent_failed" else "completed",
|
||||
)
|
||||
return {"status": "ignored", "kind": kind}
|
||||
|
||||
|
||||
__all__ = ["AgentEventObservabilityBridge"]
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Agent orchestration observability model.
|
||||
|
||||
Tracks OpenClaw, Hermes and sub-agent execution chains. Token usage is optional
|
||||
and depends on model adapter instrumentation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
|
||||
def utc_now():
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentExecutionRecord:
|
||||
execution_id: str
|
||||
user_id: str
|
||||
visible_scope: str
|
||||
entry_agent: str
|
||||
path: list[str]
|
||||
used_hermes: bool
|
||||
closed_by: str
|
||||
duration_ms: int = 0
|
||||
input_tokens: int | None = None
|
||||
output_tokens: int | None = None
|
||||
total_tokens: int | None = None
|
||||
status: str = "completed"
|
||||
started_at: str = field(default_factory=utc_now)
|
||||
ended_at: str = field(default_factory=utc_now)
|
||||
|
||||
def to_dict(self):
|
||||
return asdict(self)
|
||||
|
||||
|
||||
class AgentObservability:
|
||||
def __init__(self):
|
||||
self.records: list[AgentExecutionRecord] = []
|
||||
|
||||
def record(self, record: AgentExecutionRecord):
|
||||
self.records.append(record)
|
||||
return record.to_dict()
|
||||
|
||||
def summary(self, user_id: str | None = None) -> dict[str, Any]:
|
||||
records = [r for r in self.records if not user_id or r.user_id == user_id]
|
||||
hermes = [r for r in records if r.used_hermes]
|
||||
direct = [r for r in records if not r.used_hermes]
|
||||
return {
|
||||
"total_executions": len(records),
|
||||
"openclaw_independent_loops": len(direct),
|
||||
"hermes_orchestrated_loops": len(hermes),
|
||||
"average_duration_ms": (
|
||||
sum(r.duration_ms for r in records) / len(records) if records else 0
|
||||
),
|
||||
"token_usage": {
|
||||
"input": sum(r.input_tokens or 0 for r in records),
|
||||
"output": sum(r.output_tokens or 0 for r in records),
|
||||
"total": sum(r.total_tokens or 0 for r in records),
|
||||
},
|
||||
}
|
||||
|
||||
def hermes_tree(self, user_id: str | None = None):
|
||||
return [
|
||||
{
|
||||
"execution_id": r.execution_id,
|
||||
"entry": r.entry_agent,
|
||||
"used_hermes": r.used_hermes,
|
||||
"children": r.path,
|
||||
"duration_ms": r.duration_ms,
|
||||
"tokens": r.total_tokens,
|
||||
}
|
||||
for r in self.records
|
||||
if (not user_id or r.user_id == user_id)
|
||||
]
|
||||
|
||||
|
||||
__all__ = ["AgentExecutionRecord", "AgentObservability"]
|
||||
@@ -0,0 +1,52 @@
|
||||
"""API for agent orchestration observability."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
try:
|
||||
from fastapi import APIRouter
|
||||
except Exception:
|
||||
APIRouter = None
|
||||
|
||||
from .agent_observability import AgentExecutionRecord
|
||||
from .agent_observability_dashboard import AgentObservabilityDashboard
|
||||
from .agent_observability_store import AgentObservabilityStore
|
||||
|
||||
store = AgentObservabilityStore()
|
||||
dashboard = AgentObservabilityDashboard(store.load())
|
||||
|
||||
|
||||
def record_agent_execution(payload: dict[str, Any]):
|
||||
record = AgentExecutionRecord(**payload)
|
||||
return {"status": "ok", "record": store.append(record)}
|
||||
|
||||
|
||||
def get_agent_observability(user_id: str | None = None, group_id: str | None = None, role: str = "user"):
|
||||
records = store.query(user_id, group_id, role)
|
||||
return {
|
||||
"status": "ok",
|
||||
"records": records,
|
||||
"summary": store.load().summary(user_id if role != "owner" else None),
|
||||
}
|
||||
|
||||
|
||||
def build_router():
|
||||
if APIRouter is None:
|
||||
return None
|
||||
router = APIRouter(prefix="/admin/ai-evolution/agents", tags=["agent-observability"])
|
||||
|
||||
@router.post("/execution", operation_id="recordAgentExecution")
|
||||
def record(payload: dict[str, Any]):
|
||||
return record_agent_execution(payload)
|
||||
|
||||
@router.get("/overview", operation_id="getAgentObservabilityOverview")
|
||||
def overview(user_id: str | None = None, group_id: str | None = None, role: str = "user"):
|
||||
return get_agent_observability(user_id, group_id, role)
|
||||
|
||||
return router
|
||||
|
||||
|
||||
router = build_router()
|
||||
|
||||
__all__ = ["router", "record_agent_execution", "get_agent_observability"]
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Web UI dashboard model for agent execution visibility."""
|
||||
|
||||
from .agent_observability import AgentObservability
|
||||
|
||||
|
||||
class AgentObservabilityDashboard:
|
||||
def __init__(self, observability: AgentObservability | None = None):
|
||||
self.observability = observability or AgentObservability()
|
||||
|
||||
def overview(self, user_id: str | None = None):
|
||||
return {
|
||||
"route": "/admin/ai-evolution/agents",
|
||||
"title": "Agent Orchestration Observability",
|
||||
"summary": self.observability.summary(user_id),
|
||||
"views": [
|
||||
{
|
||||
"name": "scheduler_flow",
|
||||
"title": "OpenClaw/Hermes 调度流程",
|
||||
"data": self.observability.hermes_tree(user_id),
|
||||
},
|
||||
{
|
||||
"name": "agent_metrics",
|
||||
"title": "智能体指标",
|
||||
"fields": [
|
||||
"duration_ms",
|
||||
"input_tokens",
|
||||
"output_tokens",
|
||||
"total_tokens",
|
||||
"status",
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "loop_distribution",
|
||||
"title": "闭环分布",
|
||||
"fields": [
|
||||
"openclaw_independent_loops",
|
||||
"hermes_orchestrated_loops",
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["AgentObservabilityDashboard"]
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Runtime hooks for Agent Observability.
|
||||
|
||||
OpenClaw/Hermes/sub-agents can call these hooks at start/end/delegation points.
|
||||
The hooks store sanitized metadata only: no prompts, no secrets, no private message
|
||||
content.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from .agent_observability import AgentExecutionRecord
|
||||
from .agent_observability_store import AgentObservabilityStore
|
||||
|
||||
|
||||
def utc_now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
def parse_time(value: str) -> datetime:
|
||||
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentRunContext:
|
||||
execution_id: str
|
||||
user_id: str
|
||||
visible_scope: str
|
||||
entry_agent: str
|
||||
path: list[str] = field(default_factory=list)
|
||||
used_hermes: bool = False
|
||||
started_at: str = field(default_factory=utc_now)
|
||||
input_tokens: int = 0
|
||||
output_tokens: int = 0
|
||||
status: str = "running"
|
||||
|
||||
def delegate(self, agent_name: str) -> None:
|
||||
if agent_name not in self.path:
|
||||
self.path.append(agent_name)
|
||||
if agent_name.lower() == "hermes":
|
||||
self.used_hermes = True
|
||||
|
||||
def add_tokens(self, input_tokens: int = 0, output_tokens: int = 0) -> None:
|
||||
self.input_tokens += max(input_tokens, 0)
|
||||
self.output_tokens += max(output_tokens, 0)
|
||||
|
||||
|
||||
class AgentObservabilityHooks:
|
||||
def __init__(self, store: AgentObservabilityStore | None = None):
|
||||
self.store = store or AgentObservabilityStore()
|
||||
self.active: dict[str, AgentRunContext] = {}
|
||||
|
||||
def start(
|
||||
self,
|
||||
user_id: str,
|
||||
visible_scope: str,
|
||||
entry_agent: str,
|
||||
execution_id: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
run = AgentRunContext(
|
||||
execution_id=execution_id or f"exec_{uuid4().hex[:12]}",
|
||||
user_id=user_id,
|
||||
visible_scope=visible_scope,
|
||||
entry_agent=entry_agent,
|
||||
path=[entry_agent],
|
||||
used_hermes=entry_agent.lower() == "hermes",
|
||||
)
|
||||
self.active[run.execution_id] = run
|
||||
return {"status": "started", "execution_id": run.execution_id, "run": run.__dict__}
|
||||
|
||||
def delegate(self, execution_id: str, agent_name: str) -> dict[str, Any]:
|
||||
run = self.active[execution_id]
|
||||
run.delegate(agent_name)
|
||||
return {"status": "delegated", "execution_id": execution_id, "path": run.path}
|
||||
|
||||
def tokens(self, execution_id: str, input_tokens: int = 0, output_tokens: int = 0) -> dict[str, Any]:
|
||||
run = self.active[execution_id]
|
||||
run.add_tokens(input_tokens, output_tokens)
|
||||
return {
|
||||
"status": "tokens_recorded",
|
||||
"execution_id": execution_id,
|
||||
"input_tokens": run.input_tokens,
|
||||
"output_tokens": run.output_tokens,
|
||||
"total_tokens": run.input_tokens + run.output_tokens,
|
||||
}
|
||||
|
||||
def finish(self, execution_id: str, closed_by: str | None = None, status: str = "completed") -> dict[str, Any]:
|
||||
run = self.active.pop(execution_id)
|
||||
ended_at = utc_now()
|
||||
duration_ms = int((parse_time(ended_at) - parse_time(run.started_at)).total_seconds() * 1000)
|
||||
record = AgentExecutionRecord(
|
||||
execution_id=run.execution_id,
|
||||
user_id=run.user_id,
|
||||
visible_scope=run.visible_scope,
|
||||
entry_agent=run.entry_agent,
|
||||
path=run.path,
|
||||
used_hermes=run.used_hermes,
|
||||
closed_by=closed_by or run.path[-1],
|
||||
duration_ms=duration_ms,
|
||||
input_tokens=run.input_tokens,
|
||||
output_tokens=run.output_tokens,
|
||||
total_tokens=run.input_tokens + run.output_tokens,
|
||||
status=status,
|
||||
started_at=run.started_at,
|
||||
ended_at=ended_at,
|
||||
)
|
||||
return {"status": "stored", "record": self.store.append(record)}
|
||||
|
||||
|
||||
__all__ = ["AgentRunContext", "AgentObservabilityHooks"]
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Persistent store for agent observability records.
|
||||
|
||||
Stores sanitized execution metrics for OpenClaw/Hermes/sub-agents. No prompts,
|
||||
secrets, or private message contents are persisted here; only metrics and routing
|
||||
metadata are stored for Web UI statistics and audit.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from .agent_observability import AgentExecutionRecord, AgentObservability
|
||||
|
||||
|
||||
class AgentObservabilityStore:
|
||||
def __init__(self, root: str = "runtime/agent-observability"):
|
||||
self.root = Path(root)
|
||||
self.records_dir = self.root / "records"
|
||||
self.records_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def append(self, record: AgentExecutionRecord) -> dict[str, Any]:
|
||||
path = self.records_dir / f"{record.execution_id}.json"
|
||||
data = record.to_dict()
|
||||
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
return data
|
||||
|
||||
def load(self) -> AgentObservability:
|
||||
observability = AgentObservability()
|
||||
for path in sorted(self.records_dir.glob("*.json")):
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
observability.records.append(AgentExecutionRecord(**raw))
|
||||
return observability
|
||||
|
||||
def query(self, user_id: str | None = None, group_id: str | None = None, role: str = "user") -> list[dict[str, Any]]:
|
||||
records = [record.to_dict() for record in self.load().records]
|
||||
if role == "owner":
|
||||
return records
|
||||
if role == "group_admin" and group_id:
|
||||
return [r for r in records if r.get("visible_scope") == group_id or r.get("user_id") == user_id]
|
||||
return [r for r in records if r.get("user_id") == user_id]
|
||||
|
||||
|
||||
__all__ = ["AgentObservabilityStore"]
|
||||
Regular → Executable
+385
-82
@@ -1,95 +1,398 @@
|
||||
#!/usr/bin/env python3
|
||||
""" AI GitOps Actions API """
|
||||
import asyncio, base64, hashlib, hmac, json, os, re, shutil, subprocess, sys, time, uuid
|
||||
from __future__ import annotations
|
||||
import hashlib, hmac, json, os, re, subprocess, sys, time, urllib.parse, uuid
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import web
|
||||
sys.path.insert(0, str(Path(os.getenv('AIOS_GITOPS_WORKSPACE', '/data/aios-gitops/workspace')) / 'runtime' / 'agent-os'))
|
||||
from orchestrator.action_monitor import ActionMonitor
|
||||
|
||||
# Environment
|
||||
WS = Path(os.environ.get("AIOS_WORKSPACE", "/data/aios-gitops/workspace"))
|
||||
BASE_DIR = WS / "active-runtime"
|
||||
APPDIR = BASE_DIR / "app"
|
||||
AUDITDIR = WS / "audit"
|
||||
WORKDIR = WS / "work"
|
||||
DOTDIR = WS.parent
|
||||
GITEA_DIR = DOTDIR / "runtime" / "gitea-bin" / "data" / "repositories" / "aios"
|
||||
ROOT = Path(os.getenv('AIOS_GITOPS_ROOT', '/data/aios-gitops'))
|
||||
WS = Path(os.getenv('AIOS_GITOPS_WORKSPACE', str(ROOT / 'workspace')))
|
||||
SECRETS = Path(os.getenv('AIOS_GITOPS_SECRETS_DIR', str(ROOT / 'runtime' / 'secrets')))
|
||||
TOKEN_FILE = Path(os.getenv('AIOS_GITOPS_TOKEN_FILE', str(SECRETS / 'gitops.tokens')))
|
||||
HOST = os.getenv('AIOS_GITOPS_HOST', '127.0.0.1')
|
||||
PORT = int(os.getenv('AIOS_GITOPS_PORT', '18890'))
|
||||
OPENAPI = WS / 'docs' / 'gitops' / 'openapi-chatgpt-actions.yaml'
|
||||
BARE_REPO = Path(os.getenv('AIOS_GITOPS_BARE_REPO', str(ROOT / 'runtime' / 'gitea-bin' / 'data' / 'repositories' / 'aios' / 'aios-v23.git')))
|
||||
monitor = ActionMonitor()
|
||||
|
||||
# Load config
|
||||
try:
|
||||
import yaml
|
||||
config_path = WS / "active-runtime" / "app" / "config.yaml"
|
||||
with open(config_path) as f:
|
||||
CONFIG = yaml.safe_load(f) or {}
|
||||
except: CONFIG = {}
|
||||
BASE_MODELS = [
|
||||
{'id':'base.deepseekv4-flash','provider':'one-api','model':'deepseekv4-flash','display_name':'DeepSeek V4 Flash','scope':'global'},
|
||||
{'id':'base.qwen3.6-35b','provider':'one-api','model':'qwen3.6-35b','display_name':'Qwen 3.6 35B','scope':'global'},
|
||||
{'id':'base.gpt-5.5','provider':'openai','model':'gpt-5.5','display_name':'GPT-5.5','scope':'global'},
|
||||
]
|
||||
SECRET_RE = re.compile(r'(?i)(api[_-]?key|secret[_-]?access[_-]?key|secret|password|token)\s*[:=]\s*["\']?([A-Za-z0-9_./+=:-]{16,})')
|
||||
SECRET_VAL_RE = re.compile(r'(sk-[A-Za-z0-9_-]{20,}|HPUA[A-Z0-9]{10,}|SDC[A-Za-z0-9]{20,})')
|
||||
|
||||
start_time = time.time()
|
||||
log_ident = "aios-api"
|
||||
def now() -> str: return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
|
||||
def uid(p='id') -> str: return f'{p}-{int(time.time()*1000)}-{uuid.uuid4().hex[:8]}'
|
||||
def safe(s: str, n=160) -> str: return re.sub(r'[^A-Za-z0-9_./@-]+', '-', str(s or 'unknown')).strip('/-')[:n] or 'unknown'
|
||||
def sh(args, check=False):
|
||||
p = subprocess.run(['git'] + args, cwd=str(WS), text=True, capture_output=True)
|
||||
if check and p.returncode != 0: raise RuntimeError(p.stderr or p.stdout or 'git failed')
|
||||
return {'code':p.returncode,'stdout':p.stdout.strip(),'stderr':p.stderr.strip(),'cmd':['git']+args}
|
||||
def jload(p: Path, default):
|
||||
try: return json.loads(p.read_text(encoding='utf-8'))
|
||||
except Exception: return default
|
||||
def relpath(path: str) -> str:
|
||||
x = str(path or '').replace('\\','/').strip()
|
||||
if not x or x.startswith('/') or '..' in Path(x).parts or Path(x).parts[0] == '.git': raise ValueError('invalid path')
|
||||
return str(Path(x))
|
||||
def branch_name(name: str | None, prefix='patch/chatgpt') -> str:
|
||||
b = safe(name or f'{prefix}-{uid("br")}', 180)
|
||||
if b in ('main','master') or b.startswith('main/') or b.startswith('master/'): raise ValueError('direct main/master writes are forbidden')
|
||||
return b
|
||||
def _json_has_plain_secret(obj: Any) -> bool:
|
||||
if isinstance(obj, dict):
|
||||
for k, v in obj.items():
|
||||
lk = str(k).lower()
|
||||
if lk == 'secret_ref' or lk.endswith('_secret_ref'):
|
||||
continue
|
||||
if lk in {'api_key', 'access_key', 'secret_access_key', 'secret_key', 'password', 'token', 'bearer_token'}:
|
||||
if isinstance(v, str) and v and not v.startswith('secret://') and v != 'REDACTED':
|
||||
return True
|
||||
if _json_has_plain_secret(v):
|
||||
return True
|
||||
return False
|
||||
if isinstance(obj, list):
|
||||
return any(_json_has_plain_secret(x) for x in obj)
|
||||
if isinstance(obj, str):
|
||||
return bool(obj and not obj.startswith('secret://') and obj != 'REDACTED' and SECRET_VAL_RE.search(obj))
|
||||
return False
|
||||
|
||||
def sh(cmd, **kw):
|
||||
try:
|
||||
r = subprocess.run(cmd, capture_output=True, text=True, cwd=WS, **kw)
|
||||
return r.stdout.strip() if r else None
|
||||
except: return None
|
||||
def content_has_secret(text: str) -> bool:
|
||||
text = text or ''
|
||||
try:
|
||||
return _json_has_plain_secret(json.loads(text))
|
||||
except Exception:
|
||||
pass
|
||||
if SECRET_VAL_RE.search(text):
|
||||
return True
|
||||
for m in SECRET_RE.finditer(text):
|
||||
key = str(m.group(1)).lower()
|
||||
value = str(m.group(2))
|
||||
if 'secret_ref' in key or value.startswith('secret://') or value == 'REDACTED':
|
||||
continue
|
||||
return True
|
||||
return False
|
||||
def token_records():
|
||||
if not TOKEN_FILE.exists(): return []
|
||||
out=[]
|
||||
for raw in TOKEN_FILE.read_text(encoding='utf-8').splitlines():
|
||||
line=raw.strip()
|
||||
if not line or line.startswith('#'): continue
|
||||
if line.startswith('{'):
|
||||
try: out.append(json.loads(line))
|
||||
except Exception: pass
|
||||
else:
|
||||
out.append({'account':'legacy-token','role':'owner','scopes':['*'],'token_plain':line})
|
||||
return out
|
||||
def authenticate(token: str):
|
||||
if os.getenv('AIOS_GITOPS_DEV_ALLOW','0') == '1': return {'account':'dev-owner','role':'owner','scopes':['*']}
|
||||
if not token: return None
|
||||
digest=hashlib.sha256(token.encode()).hexdigest()
|
||||
for r in token_records():
|
||||
if r.get('disabled'): continue
|
||||
if r.get('token_sha256') and hmac.compare_digest(str(r['token_sha256']), digest): return r
|
||||
if r.get('token_plain') and hmac.compare_digest(str(r['token_plain']), token): return r
|
||||
return None
|
||||
def sanitize(obj: Any) -> Any:
|
||||
if isinstance(obj, dict):
|
||||
return {k: ('REDACTED' if re.search(r'(?i)(api[_-]?key|secret|password|token)', str(k)) else sanitize(v)) for k,v in obj.items()}
|
||||
if isinstance(obj, list): return [sanitize(x) for x in obj]
|
||||
if isinstance(obj, str): return SECRET_VAL_RE.sub('REDACTED', obj)
|
||||
return obj
|
||||
def ensure_git():
|
||||
if not sh(['config','user.email'])['stdout']: sh(['config','user.email','chatgpt-gitops-bot@cnnetmkt.top'], True)
|
||||
if not sh(['config','user.name'])['stdout']: sh(['config','user.name','chatgpt-gitops-bot'], True)
|
||||
def checkout(branch, base='main', create=False):
|
||||
ensure_git(); b=branch_name(branch)
|
||||
if create or sh(['rev-parse','--verify',b])['code'] != 0:
|
||||
sh(['checkout',base], True); sh(['checkout','-B',b], True)
|
||||
else: sh(['checkout',b], True)
|
||||
return b
|
||||
def write_file(path, content):
|
||||
if content_has_secret(content): raise ValueError('secret_rejected: use secret_ref; plaintext API keys must not enter Git')
|
||||
r=relpath(path); t=WS/r; t.parent.mkdir(parents=True, exist_ok=True); t.write_text(content, encoding='utf-8'); return r
|
||||
def write_json(path, data): return write_file(path, json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
||||
def sync_branch(branch: str) -> dict:
|
||||
if not branch or branch in ('main','master') or branch.startswith('main/') or branch.startswith('master/'):
|
||||
return {'status':'skipped','reason':'protected_branch','branch':branch}
|
||||
if BARE_REPO.exists():
|
||||
ref=f'refs/heads/{branch}:refs/heads/{branch}'
|
||||
p=subprocess.run(['git','--git-dir',str(BARE_REPO),'fetch',str(WS),ref], text=True, capture_output=True)
|
||||
if p.returncode != 0: raise RuntimeError(p.stderr or p.stdout or f'git sync failed for {branch}')
|
||||
return {'status':'synced','target':'bare','branch':branch,'stdout':p.stdout.strip(),'stderr':p.stderr.strip()}
|
||||
p=subprocess.run(['git','push','origin',f'HEAD:refs/heads/{branch}'], cwd=str(WS), text=True, capture_output=True)
|
||||
if p.returncode != 0: raise RuntimeError(p.stderr or p.stdout or f'git push failed for {branch}')
|
||||
return {'status':'synced','target':'origin','branch':branch,'stdout':p.stdout.strip(),'stderr':p.stderr.strip()}
|
||||
|
||||
def baselines():
|
||||
p = WS / "baselines" / "current.json"
|
||||
if p.exists():
|
||||
try: return json.loads(p.read_text())
|
||||
except: pass
|
||||
return {}
|
||||
def sync_tag(tag: str) -> dict:
|
||||
if not tag: return {'status':'skipped','reason':'empty_tag'}
|
||||
if BARE_REPO.exists():
|
||||
ref=f'refs/tags/{tag}:refs/tags/{tag}'
|
||||
p=subprocess.run(['git','--git-dir',str(BARE_REPO),'fetch',str(WS),ref], text=True, capture_output=True)
|
||||
if p.returncode != 0: raise RuntimeError(p.stderr or p.stdout or f'git tag sync failed for {tag}')
|
||||
return {'status':'synced','target':'bare','tag':tag,'stdout':p.stdout.strip(),'stderr':p.stderr.strip()}
|
||||
p=subprocess.run(['git','push','origin',tag], cwd=str(WS), text=True, capture_output=True)
|
||||
if p.returncode != 0: raise RuntimeError(p.stderr or p.stdout or f'git tag push failed for {tag}')
|
||||
return {'status':'synced','target':'origin','tag':tag,'stdout':p.stdout.strip(),'stderr':p.stderr.strip()}
|
||||
|
||||
def commands(status):
|
||||
d = WS / "agents" / "commands" / status
|
||||
if not d.exists(): return []
|
||||
return [{"name": f.name, "state": status} for f in sorted(d.iterdir()) if f.name.endswith(".md")]
|
||||
def commit(paths, msg):
|
||||
ensure_git(); paths = [relpath(p) for p in paths] if paths else ['.']; sh(['add'] + paths, True)
|
||||
branch=sh(['rev-parse','--abbrev-ref','HEAD'])['stdout']
|
||||
if not sh(['status','--short'])['stdout']:
|
||||
return {'status':'no_changes','branch':branch,'sync':sync_branch(branch)}
|
||||
out=sh(['commit','-m',msg], True); branch=sh(['rev-parse','--abbrev-ref','HEAD'])['stdout']; sync=sync_branch(branch); return {'status':'committed','branch':branch,'rev':sh(['rev-parse','--short','HEAD'], True)['stdout'],'git':out,'sync':sync}
|
||||
def audit(actor, action, target, payload):
|
||||
p=ROOT/'artifacts'/'chatgpt-gitops-audit.jsonl'; p.parent.mkdir(parents=True, exist_ok=True)
|
||||
with p.open('a', encoding='utf-8') as f: f.write(json.dumps({'at':now(),'actor':actor,'action':action,'target':target,'payload':sanitize(payload)}, ensure_ascii=False)+'\n')
|
||||
def model_docs():
|
||||
out=[]
|
||||
for p in sorted((WS/'config'/'model_registry').glob('**/*.json')): out.append(jload(p, {'path':str(p.relative_to(WS))}))
|
||||
return out
|
||||
def patches():
|
||||
out=[]
|
||||
for p in sorted((WS/'patches').glob('**/*.json')): out.append(jload(p, {'path':str(p.relative_to(WS))}))
|
||||
return out
|
||||
|
||||
def agent_runs():
|
||||
d = AUDITDIR / "agent-runs"
|
||||
if not d.exists(): return []
|
||||
rs = []
|
||||
for f in sorted(d.iterdir(), reverse=True):
|
||||
if f.name.endswith(".json"):
|
||||
try:
|
||||
j = json.loads(f.read_text())
|
||||
j["file"] = f.stem
|
||||
rs.append(j)
|
||||
except: pass
|
||||
return rs
|
||||
class H(BaseHTTPRequestHandler):
|
||||
server_version='AIOSChatGPTGitOps/V1R1C0'
|
||||
def sendj(self, data, code=200):
|
||||
b=json.dumps(data, ensure_ascii=False, indent=2).encode(); self.send_response(code); self.send_header('Content-Type','application/json; charset=utf-8'); self.send_header('Content-Length',str(len(b))); self.end_headers(); self.wfile.write(b)
|
||||
def sendt(self, text, ctype='text/plain; charset=utf-8', code=200):
|
||||
b=text.encode(); self.send_response(code); self.send_header('Content-Type',ctype); self.send_header('Content-Length',str(len(b))); self.end_headers(); self.wfile.write(b)
|
||||
def body(self):
|
||||
n=int(self.headers.get('Content-Length','0') or '0'); raw=self.rfile.read(n).decode() if n else '{}'
|
||||
try: return json.loads(raw or '{}')
|
||||
except Exception: return {'_raw':raw}
|
||||
def actor(self):
|
||||
auth=self.headers.get('Authorization',''); tok=auth[7:].strip() if auth.startswith('Bearer ') else self.headers.get('X-AIOS-Token','')
|
||||
rec=authenticate(tok)
|
||||
if rec: return {'ok':True,'account':safe(rec.get('account') or rec.get('name') or 'token-user'),'role':rec.get('role','user'),'scopes':set(rec.get('scopes') or [])}
|
||||
return {'ok':False,'account':'anonymous','role':'anonymous','scopes':set()}
|
||||
def need(self, scope):
|
||||
a=self.actor()
|
||||
if not a['ok']: self.sendj({'error':'unauthorized','hint':'Bearer token required'},401); return None
|
||||
if '*' not in a['scopes'] and scope not in a['scopes']: self.sendj({'error':'forbidden','required_scope':scope,'account':a['account']},403); return None
|
||||
return a
|
||||
def do_GET(self):
|
||||
u=urllib.parse.urlparse(self.path); path=u.path.rstrip('/') if u.path != '/' else u.path; q=urllib.parse.parse_qs(u.query)
|
||||
try:
|
||||
if path in ('/health','/api/health'): return self.sendj({'status':'ok','service':'chatgpt-gitops-adapter','workspace':str(WS)})
|
||||
if path in ('/api/openapi.yaml','/api/openapi-chatgpt-actions.yaml','/api/schema'):
|
||||
return self.sendt(OPENAPI.read_text(encoding='utf-8'), 'application/yaml; charset=utf-8') if OPENAPI.exists() else self.sendj({'error':'schema_missing'},404)
|
||||
if path in ('/api','/api/status'): return self.sendj(api_spec())
|
||||
if path in ('/api/base','/api/baseline','/base'):
|
||||
data=jload(WS/'baseline'/'current.json', {}); data.setdefault('git', sh(['rev-parse','--short','HEAD'])['stdout']); return self.sendj(data)
|
||||
if path in ('/api/ver','/api/versions','/ver'):
|
||||
return self.sendj({'tags':sh(['tag','--list','V*'])['stdout'].splitlines(),'versions_dir':[p.name for p in (WS/'versions').glob('*')],'head':sh(['rev-parse','--short','HEAD'])['stdout']})
|
||||
if path in ('/api/diff','/diff'):
|
||||
frm=(q.get('from') or ['HEAD~1'])[0]; to=(q.get('to') or ['HEAD'])[0]; return self.sendj({'from':frm,'to':to,'diff':sh(['diff',frm,to])})
|
||||
if path in ('/api/patch','/api/patches','/api/branches','/patch'): return self.sendj({'patches':patches(),'branches':sh(['branch','--format','%(refname:short)'])['stdout'].splitlines()})
|
||||
if path in ('/api/model','/model'): return self.sendj({'base':BASE_MODELS,'custom':model_docs(),'priority':[jload(p,{}) for p in sorted((WS/'config'/'model_priority').glob('*.json'))]})
|
||||
if path in ('/api/run','/api/runtime-status','/run'):
|
||||
svc=subprocess.run(['systemctl','is-active','aios-gitops-model-api'], text=True, capture_output=True)
|
||||
return self.sendj({'service':'aios-gitops-model-api','status':svc.stdout.strip() or svc.stderr.strip(),'workspace_branch':sh(['rev-parse','--abbrev-ref','HEAD'])['stdout']})
|
||||
if path in ('/api/action-monitor','/action-monitor'):
|
||||
latest=monitor.latest()
|
||||
ldict={'event_id':latest.event_id,'action':latest.action,'error':latest.error,'context':latest.context,'created_at':latest.created_at} if latest else None
|
||||
return self.sendj({'events':[{"event_id":e.event_id,"action":e.action,"error":e.error,"context":e.context,"created_at":e.created_at} for e in monitor.list_events()],'latest':ldict})
|
||||
# Admin AI Evolution Dashboard
|
||||
if path in ('/admin/ai-evolution','/admin/ai-evolution/'):
|
||||
return self.ai_evolution_page()
|
||||
if path in ('/admin/ai-evolution/api/data'):
|
||||
return self.ai_evolution_data()
|
||||
return self.sendj({'error':'not_found','path':path},404)
|
||||
except Exception as e:
|
||||
monitor.record_failure('do_GET', str(e), {'path':path})
|
||||
return self.sendj({'error':str(e)},500)
|
||||
def do_PUT(self):
|
||||
path=urllib.parse.urlparse(self.path).path.rstrip('/'); data=self.body()
|
||||
if path == '/api/model-priority': return self.model_priority(data)
|
||||
return self.sendj({'error':'not_found','path':path},404)
|
||||
def do_POST(self):
|
||||
path=urllib.parse.urlparse(self.path).path.rstrip('/'); data=self.body()
|
||||
try:
|
||||
if path in ('/api/branch','/api/create-branch'): return self.branch(data)
|
||||
if path in ('/api/file','/api/commit-file'): return self.file(data)
|
||||
if path == '/api/commit': return self.commit(data)
|
||||
if path in ('/api/patch','/api/create-patch'): return self.patch(data)
|
||||
if path in ('/api/tag','/api/create-tag'): return self.tag(data)
|
||||
if path == '/api/rollback': return self.rollback(data)
|
||||
if path == '/api/model': return self.model(data)
|
||||
if path == '/api/model-priority': return self.model_priority(data)
|
||||
if path == '/api/nl': return self.nl(data)
|
||||
return self.sendj({'error':'not_found','path':path},404)
|
||||
except Exception as e:
|
||||
monitor.record_failure('do_POST', str(e), {'path':path})
|
||||
return self.sendj({'error':str(e)},500)
|
||||
def branch(self,data):
|
||||
a=self.need('branch:create');
|
||||
if not a: return
|
||||
b=checkout(data.get('branch'), data.get('base','main'), True); meta={'type':'branch','branch':b,'base':data.get('base','main'),'created_by':a['account'],'created_at':now(),'direct_main_push':False,'owner_approval_required':True}
|
||||
r=write_json(f'patches/{safe(b).replace("/","_")}.branch.json', meta); c=commit([r], data.get('message') or f'chore(chatgpt): create {b}'); audit(a['account'],'branch',b,meta); self.sendj({'status':'ok','branch':b,'commit':c})
|
||||
def file(self,data):
|
||||
a=self.need('file:write');
|
||||
if not a: return
|
||||
b=checkout(data.get('branch'), data.get('base','main'), bool(data.get('create_branch'))); r=write_file(data.get('path'), data.get('content','')); c=commit([r], data.get('message') or f'chore(chatgpt): update {r}'); audit(a['account'],'file',r,{'branch':b,'commit':c}); self.sendj({'status':'committed','branch':b,'path':r,'commit':c})
|
||||
def commit(self,data):
|
||||
a=self.need('commit:create');
|
||||
if not a: return
|
||||
b=checkout(data.get('branch') or sh(['rev-parse','--abbrev-ref','HEAD'])['stdout'], data.get('base','main'), False); paths=data.get('paths') or ['.']; paths=[paths] if isinstance(paths,str) else paths; c=commit(paths, data.get('message') or 'chore(chatgpt): commit changes'); audit(a['account'],'commit',b,{'paths':paths,'commit':c}); self.sendj({'status':c['status'],'branch':b,'commit':c})
|
||||
def patch(self,data):
|
||||
a=self.need('patch:create');
|
||||
if not a: return
|
||||
name=data.get('patch_name') or data.get('name') or uid('patch'); b=checkout(data.get('branch') or f'patch/{safe(name)}', data.get('base','main'), True); meta={'id':uid('patch'),'patch_name':name,'branch':b,'risk':data.get('risk','medium'),'description':data.get('description',''),'created_by':a['account'],'created_at':now()}
|
||||
r=write_json(f'patches/{safe(name)}.json', meta); c=commit([r], data.get('message') or f'chore(chatgpt): create patch {name}'); audit(a['account'],'patch',name,meta); self.sendj({'status':'patch_branch_created','patch':meta,'commit':c})
|
||||
def tag(self,data):
|
||||
a=self.need('tag:create');
|
||||
if not a: return
|
||||
ver=data.get('version') or data.get('tag')
|
||||
if not re.match(r'^V\d+R\d+C\d+(-[A-Za-z0-9_.-]+)?$', ver or ''): return self.sendj({'error':'version must match VxRxCx'},400)
|
||||
if data.get('confirm') != True:
|
||||
self.sendj({'status':'confirm_required','operation':'tag','hint':'This operation creates a Git tag. Confirm to proceed.','x-openai-isConsequential':True}, 428)
|
||||
return
|
||||
if a['role'] != 'owner' or not data.get('execute'):
|
||||
b=checkout(data.get('branch') or f'request/tag-{safe(ver)}', 'main', True); req={'version':ver,'requested_by':a['account'],'approval_required':True,'created_at':now()}; r=write_json(f'baseline/tag_requests/{safe(ver)}.json', req); c=commit([r], f'chore(chatgpt): request tag {ver}'); return self.sendj({'status':'approval_required','branch':b,'commit':c})
|
||||
out=sh(['tag','-a',ver,'-m',data.get('message') or f'release {ver}'], True); self.sendj({'status':'tagged','version':ver,'git':out,'sync':sync_tag(ver)})
|
||||
def rollback(self,data):
|
||||
a=self.need('rollback:request');
|
||||
if not a: return
|
||||
target=data.get('target_version') or data.get('version') or data.get('target')
|
||||
if not target: return self.sendj({'error':'target_version required'},400)
|
||||
b=checkout(data.get('branch') or f'request/rollback-{safe(target)}-{uid("rb")}', 'main', True); req={'id':uid('rollback'),'target_version':target,'reason':data.get('reason',''),'requested_by':a['account'],'approval_required':True,'execute':False,'created_at':now()}; r=write_json(f'baseline/rollback_requests/{safe(req["id"])}.json', req); c=commit([r], f'chore(chatgpt): request rollback {target}'); self.sendj({'status':'rollback_request_committed','branch':b,'request':req,'commit':c})
|
||||
def model(self,data):
|
||||
a=self.need('model:write');
|
||||
if not a: return
|
||||
name=data.get('model_name') or data.get('model'); base=(data.get('base_url') or '').rstrip('/'); prov=data.get('provider_name') or 'custom-provider'
|
||||
if not name or not base: return self.sendj({'error':'provider_name/base_url/model_name required'},400)
|
||||
owner=safe(data.get('username') or a['account']); secret_ref=data.get('api_key_secret_ref') or data.get('secret_ref')
|
||||
if data.get('api_key'):
|
||||
d=SECRETS/'llm'/owner; d.mkdir(parents=True, exist_ok=True); sid=uid('llmkey'); p=d/f'{sid}.secret'; p.write_text(str(data['api_key']), encoding='utf-8'); os.chmod(p,0o600); secret_ref=f'secret://llm/{owner}/{sid}'
|
||||
if not secret_ref: return self.sendj({'error':'api_key_secret_ref or api_key required'},400)
|
||||
mid=data.get('model_id') or f'custom.{owner}.{hashlib.sha1((prov+base+name).encode()).hexdigest()[:10]}'; b=checkout(data.get('branch') or f'patch/model-{safe(mid)}','main',True)
|
||||
doc=sanitize({k:v for k,v in data.items() if k!='api_key'}); doc.update({'model_id':mid,'provider_name':prov,'base_url':base,'model_name':name,'api_key_secret_ref':secret_ref,'created_by':a['account'],'created_at':now()})
|
||||
if data.get('confirm') != True:
|
||||
self.sendj({'status':'confirm_required','operation':'model','hint':'This operation registers a new model. Confirm to proceed.','x-openai-isConsequential':True}, 428)
|
||||
return
|
||||
r=write_json(f'config/model_registry/chatgpt/{safe(mid)}.json', doc); c=commit([r], data.get('message') or f'chore(chatgpt): register model {name}'); audit(a['account'],'model',mid,doc); self.sendj({'status':'model_registered','branch':b,'model_id':mid,'secret_ref':secret_ref,'commit':c})
|
||||
def model_priority(self,data):
|
||||
a=self.need('model:priority');
|
||||
if not a: return
|
||||
if data.get('confirm') != True:
|
||||
self.sendj({'status':'confirm_required','operation':'model-priority','hint':'This operation updates model priority. Confirm to proceed.','x-openai-isConsequential':True}, 428)
|
||||
return
|
||||
user=safe(data.get('username') or a['account']); b=checkout(data.get('branch') or f'patch/model-priority-{user}', 'main', True); doc={'username':user,'items':data.get('items') or data.get('priority') or [],'updated_by':a['account'],'updated_at':now()}; r=write_json(f'config/model_priority/{user}.json', doc); c=commit([r], data.get('message') or f'chore(chatgpt): update model priority {user}'); audit(a['account'],'model-priority',user,doc); self.sendj({'status':'updated','branch':b,'priority':doc,'commit':c})
|
||||
def nl(self,data):
|
||||
a=self.need('nl:plan');
|
||||
if not a: return
|
||||
text=data.get('text',''); low=text.lower(); intent='unknown'; plan=[]
|
||||
if 'rollback' in low or '回滚' in text: intent='rollback_request'; plan=['commit rollback request','wait owner/admin approval']
|
||||
elif 'diff' in low or '对比' in text: intent='diff'; plan=['call GET /api/diff']
|
||||
elif 'model' in low or '模型' in text: intent='model_change'; plan=['use POST /api/model or PUT /api/model-priority','commit metadata']
|
||||
elif 'patch' in low or '补丁' in text: intent='patch_create'; plan=['call POST /api/patch']
|
||||
b=checkout(data.get('branch') or f'patch/nl-{uid("nl")}', 'main', True); doc={'id':uid('nl'),'text':text,'intent':intent,'action_plan':plan,'execute':False,'owner_approval_required':True,'created_by':a['account'],'created_at':now()}; r=write_json(f'patches/nl/{safe(doc["id"])}.json', doc); c=commit([r], f'chore(chatgpt): record nl plan {intent}'); self.sendj({'status':'planned','branch':b,'request':doc,'commit':c})
|
||||
_EVOLUTION_HTML = '''<!DOCTYPE html>
|
||||
<html lang=zh-CN>
|
||||
<head>
|
||||
<meta charset=UTF-8>
|
||||
<meta name=viewport content="width=device-width,initial-scale=1.0">
|
||||
<title>AIOS Evolution Dashboard</title>
|
||||
<script src=https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js></script>
|
||||
<style>
|
||||
*{margin:0;padding:0;box-sizing:border-box}
|
||||
body{background:#0d1117;color:#c9d1d9;font-size:14px;line-height:1.7;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif}
|
||||
.c{max-width:1000px;margin:0 auto;padding:32px 24px}
|
||||
h1{font-size:24px;color:#58a6ff;margin-bottom:2px}
|
||||
.sub{color:#8b949e;font-size:13px;border-bottom:1px solid #21262d;padding-bottom:16px;margin-bottom:20px;display:flex;justify-content:space-between}
|
||||
.g{display:grid;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));gap:16px;margin-bottom:20px}
|
||||
.s{background:#161b22;border:1px solid #21262d;border-radius:8px;padding:18px;text-align:center}
|
||||
.s .v{font-size:28px;font-weight:700;color:#f0f6fc}
|
||||
.s .l{font-size:11px;color:#8b949e;margin-top:4px}
|
||||
.bx{background:#161b22;border:1px solid #21262d;border-radius:8px;margin-bottom:14px}
|
||||
.bh{border-bottom:1px solid #21262d;padding:10px 16px;font-weight:600;font-size:14px;display:flex;gap:6px;align-items:center}
|
||||
.bd{padding:12px 16px}
|
||||
.rw{display:grid;grid-template-columns:1fr 1fr 100px 60px;gap:8px;padding:8px 16px;border-bottom:1px solid #21262d;font-size:13px;align-items:center;cursor:pointer;transition:background .15s}
|
||||
.rw:hover{background:#1c2128}.rw:last-child{border-bottom:none}
|
||||
.tg{display:inline-block;padding:2px 10px;border-radius:99px;font-size:11px;font-weight:600}
|
||||
.ok{background:#3fb95033;color:#3fb950}.fl{background:#da363333;color:#f85149}.if{background:#1f6feb33;color:#58a6ff}
|
||||
pre{background:#0d1117;padding:12px;border-radius:6px;overflow:auto;font-size:12px;color:#8b949e;max-height:300px;margin-top:8px}select{padding:8px 10px;border:1px solid #30363d;border-radius:6px;background:#21262d;color:#c9d1d9;font-size:13px;max-width:400px;width:100%;margin-bottom:4px;cursor:pointer}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id=app class=c>
|
||||
<h1>AIOS Evolution Dashboard</h1>
|
||||
<div class=sub><span>Baseline: {{baseline}}</span><span style=color:#8b949e>{{refreshMsg}}</span></div>
|
||||
<div class=g><div class=s><div class=v>{{tot}}</div><div class=l>Commands</div></div><div class=s><div class=v style=color:#3fb950>{{sok}}</div><div class=l>Succeeded</div></div><div class=s><div class=v style=color:#f85149>{{sfail}}</div><div class=l>Failed</div></div><div class=s><div class=v>{{runs}}</div><div class=l>Executions</div></div></div>
|
||||
<div class=bx><div class=bh>Version Timeline</div><div class=bd><div class=rw style=font-weight:600;border-bottom:2px solid #30363d;cursor:default><span>Version</span><span>Time</span><span>Result</span></div><div v-for=v in vs class=rw @click=selVer(v)><span style=color:#58a6ff>{{v.tag}}</span><span>{{v.time||'-'}}</span><span :class="'tg '+v.result">{{v.result||'-'}}</span></div></div></div>
|
||||
<div class=bx><div class=bh>Commands</div><div class=bd><select v-model=cmd @change=loadCmd><option value>-- Select --</option><option v-for=c in cmds :value=c>{{c}}</option></select><pre v-if=cdet>{{cdet}}</pre></div></div>
|
||||
<div class=bx><div class=bh>Recent Agent Runs</div><div class=bd><div class=rw style=font-weight:600;border-bottom:2px solid #30363d;cursor:default><span>Agent</span><span>Command</span><span>Count</span><span>Result</span></div><div v-for=r in aruns class=rw><span>{{r.agent||'-'}}</span><span style=overflow:hidden;white-space:nowrap;text-overflow:ellipsis>{{r.command}}</span><span>{{r.interaction_count||0}}</span><span :class="'tg '+r.final_result">{{r.final_result||'-'}}</span></div></div></div></div>
|
||||
<script>
|
||||
const{createApp,ref,onMounted}=Vue;createApp({setup(){const vs=ref([]),cmds=ref([]),ar=ref([]),cmd=ref(''),cdet=ref(null),tot=ref(0),sok=ref(0),sfail=ref(0),runs=ref(0),baseline=ref('V1R3C1'),refreshMsg=ref('')
|
||||
async function ld(){try{const r=await fetch('/admin/ai-evolution/api/data'),d=await r.json();baseline.value=d.baseline||'V1R3C1';vs.value=d.versions||[];cmds.value=d.commands||[];ar.value=d.agent_runs||[];tot.value=d.stats?.total||0;sok.value=d.stats?.success||0;sfail.value=d.stats?.failed||0;runs.value=d.stats?.agent_runs||0;refreshMsg.value='Last updated: '+new Date().toLocaleString()}catch(e){}}
|
||||
async function loadCmd(){if(!cmd.value){cdet.value=null;return}try{const r=await fetch('/admin/ai-evolution/api/data?command='+encodeURIComponent(cmd.value)),d=await r.json();cdet.value=JSON.stringify(d,null,2)}catch(e){cdet.value='Error'}}
|
||||
function selVer(v){cmd.value=v.tag||v.id||v.version;loadCmd()}
|
||||
onMounted(()=>{ld();setInterval(ld,30000)})
|
||||
return{vs,cmds,ar,cmd,cdet,tot,sok,sfail,runs,baseline,refreshMsg,loadCmd,selVer}})mount('#app')
|
||||
</script></body></html>'''
|
||||
|
||||
async def handle_api(request):
|
||||
path = request.path
|
||||
if path in ('/admin/ai-evolution','/admin/ai-evolution/'):
|
||||
# Serve the HTML page
|
||||
html_path = APPDIR / 'static' / 'admin-ai-evolution.html'
|
||||
if html_path.exists():
|
||||
return web.Response(text=html_path.read_text(), content_type='text/html')
|
||||
return web.Response(text='Dashboard not found', status=404)
|
||||
if path == '/admin/ai-evolution/api/data':
|
||||
result = subprocess.run(['git','tag','--list','V*'], capture_output=True, text=True, cwd=WS)
|
||||
tags = result.stdout.strip().splitlines() if result.returncode == 0 else []
|
||||
h = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, text=True, cwd=WS)
|
||||
head = h.stdout.strip() if h.returncode == 0 else 'unknown'
|
||||
b = subprocess.run(['git','rev-parse','--abbrev-ref','HEAD'], capture_output=True, text=True, cwd=WS)
|
||||
branch = b.stdout.strip() if b.returncode == 0 else 'unknown'
|
||||
return web.json_response({
|
||||
'baseline': baselines(),
|
||||
'versions': [{'tag':t,'name':p.name} for p in sorted((WS/'versions').glob('*')) for t in tags if p.name in t],
|
||||
'commands': commands('active') + commands('running') + commands('completed'),
|
||||
'agent_runs': agent_runs(),
|
||||
'stats': {
|
||||
'current': head,
|
||||
'branch': branch,
|
||||
'uptime': round(time.time()-start_time, 2),
|
||||
'baseline': baselines().get('current_baseline','unknown'),
|
||||
}
|
||||
})
|
||||
return web.json_response({'error':'not_found','path':path}, status=404)
|
||||
def ai_evolution_page(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||
self.send_header('Content-Length', str(len(self._EVOLUTION_HTML)))
|
||||
self.end_headers()
|
||||
self.wfile.write(self._EVOLUTION_HTML.encode())
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_route('GET', '/admin/ai-evolution', handle_api)
|
||||
app.router.add_route('GET', '/admin/ai-evolution/', handle_api)
|
||||
app.router.add_route('GET', '/admin/ai-evolution/api/data', handle_api)
|
||||
def ai_evolution_data(self):
|
||||
q=urllib.parse.urlparse(self.path).query
|
||||
params=urllib.parse.parse_qs(q)
|
||||
cmd_filter=(params.get('command') or [None])[0]
|
||||
bl=jload(WS/'baseline'/'current.json',{})
|
||||
current_ver=bl.get('current_baseline','V1R3C1')
|
||||
tags=sh(['tag','--list','V*'])['stdout'].splitlines() if WS.exists() else []
|
||||
versions=[]
|
||||
if (WS/'baseline'/'promotions').exists():
|
||||
for p in sorted((WS/'baseline'/'promotions').glob('*.json')):
|
||||
d=jload(p,{});v=d.get('version',p.stem)
|
||||
versions.append({'tag':v,'time':d.get('promoted_at','-'),'result':'ok'})
|
||||
for t in tags:
|
||||
if not any(v['tag']==t for v in versions):
|
||||
versions.append({'tag':t,'time':'-','result':'if'})
|
||||
agent_runs=[];ard=WS/'audit'/'agent-runs'
|
||||
if ard.exists():
|
||||
for f in sorted(ard.glob('*.json'),reverse=True)[:50]:
|
||||
d=jload(f,{});agent_runs.append(d) if isinstance(d,dict) else None
|
||||
cmd_dirs=[WS/'agents/commands/active',WS/'agents/commands/running',WS/'agents/commands/completed',WS/'agents/commands/failed']
|
||||
commands=[]
|
||||
for d in cmd_dirs:
|
||||
if d.exists():
|
||||
for f in sorted(d.glob('*.md')):
|
||||
commands.append(f.name)
|
||||
command_detail=None
|
||||
if cmd_filter:
|
||||
for d in cmd_dirs:
|
||||
if d.exists():
|
||||
fp=d/cmd_filter
|
||||
if fp.exists():
|
||||
command_detail={'command':cmd_filter,'content':fp.read_text(encoding='utf-8')}
|
||||
break
|
||||
if not command_detail:
|
||||
command_detail={'command':cmd_filter,'error':'not_found'}
|
||||
return self.sendj(command_detail)
|
||||
completed=list((WS/'agents/commands/completed').glob('*.md')) if (WS/'agents/commands/completed').exists() else []
|
||||
failed=list((WS/'agents/commands/failed').glob('*.md')) if (WS/'agents/commands/failed').exists() else []
|
||||
stats={'total':len(commands),'success':len(completed),'failed':len(failed),'agent_runs':len(agent_runs)}
|
||||
data={'baseline':current_ver,'versions':versions[-30:],'agent_runs':agent_runs,'commands':sorted(set(commands)),'stats':stats}
|
||||
self.sendj(data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
web.run_app(app, host='127.0.0.1', port=18890)
|
||||
|
||||
def api_spec():
|
||||
return {'version':'V1R1C0-chatgpt-actions','openapi':'/api/openapi.yaml','read':['GET /api/base','GET /api/ver','GET /api/diff','GET /api/patch','GET /api/model','GET /api/run','GET /admin/ai-evolution'],'write':['POST /api/branch','POST /api/file','POST /api/commit','POST /api/patch','POST /api/tag','POST /api/rollback','POST /api/model','PUT /api/model-priority','POST /api/nl'],'policy':{'bearer_required_for_write':True,'direct_main_push':False,'delete_tag':False,'owner_approval_required_for_v_level':True,'api_keys_in_git':False,'git_secret_field':'secret_ref','online_git_sync':True}}
|
||||
|
||||
def main():
|
||||
print(f'AIOS ChatGPT GitOps adapter listening on {HOST}:{PORT}')
|
||||
ThreadingHTTPServer((HOST,PORT), H).serve_forever()
|
||||
if __name__ == '__main__': main()
|
||||
@@ -1,36 +0,0 @@
|
||||
# CMD-20260708-fix-git-actions-write-and-enable-audit-plane
|
||||
|
||||
## Command ID
|
||||
CMD-20260708-fix-git-actions-write-and-enable-audit-plane
|
||||
|
||||
## Priority
|
||||
P0
|
||||
|
||||
## Mode
|
||||
autonomous
|
||||
|
||||
## Target
|
||||
Primary: OpenClaw
|
||||
Supporting: Hermes
|
||||
Review: Codex
|
||||
|
||||
## Objective
|
||||
解决当前 AIOS Git Actions 写权限问题,恢复 ChatGPT Actions → AIOS GitOps API → Git Commit 链路,
|
||||
同时建立 Agent 全生命周期审计体系。
|
||||
|
||||
## Status
|
||||
- Part 1 (Fix Git Actions Write Permission): ✅ COMPLETE
|
||||
- Part 2 (Agent Command Plane Structure): ✅ COMPLETE
|
||||
- Part 3 (Agent Execution Audit): ✅ COMPLETE
|
||||
- Part 4 (Conversation Trace): 🚧 IN PROGRESS
|
||||
- Part 5 (Version Association): ⏳ PENDING
|
||||
- Part 6 (Web UI Integration): ⏳ PENDING
|
||||
- Part 7 (Evidence): ⏳ PENDING
|
||||
- Part 8 (Regression Test): ⏳ PENDING
|
||||
|
||||
## Completion Criteria
|
||||
1. Code committed: agents/, audit/, evidence/, web-ui/
|
||||
2. Doc: docs/architecture/AIOS_AGENT_AUDIT_PLANE.md
|
||||
3. Report: evidence/CMD-20260708.../final-report.md
|
||||
4. Output: AIOS_AGENT_AUDIT_PLANE_READY, AIOS_GIT_ACTION_WRITE_READY
|
||||
5. Next command created: CMD-20260709-autonomous-controller-enable
|
||||
@@ -1,36 +0,0 @@
|
||||
# CMD-20260708-fix-git-actions-write-and-enable-audit-plane
|
||||
|
||||
## Command ID
|
||||
CMD-20260708-fix-git-actions-write-and-enable-audit-plane
|
||||
|
||||
## Priority
|
||||
P0
|
||||
|
||||
## Mode
|
||||
autonomous
|
||||
|
||||
## Target
|
||||
Primary: OpenClaw
|
||||
Supporting: Hermes
|
||||
Review: Codex
|
||||
|
||||
## Objective
|
||||
解决当前 AIOS Git Actions 写权限问题,恢复 ChatGPT Actions → AIOS GitOps API → Git Commit 链路,
|
||||
同时建立 Agent 全生命周期审计体系。
|
||||
|
||||
## Status
|
||||
- Part 1 (Fix Git Actions Write Permission): ✅ COMPLETE
|
||||
- Part 2 (Agent Command Plane Structure): ✅ COMPLETE
|
||||
- Part 3 (Agent Execution Audit): ✅ COMPLETE
|
||||
- Part 4 (Conversation Trace): 🚧 IN PROGRESS
|
||||
- Part 5 (Version Association): ⏳ PENDING
|
||||
- Part 6 (Web UI Integration): ⏳ PENDING
|
||||
- Part 7 (Evidence): ⏳ PENDING
|
||||
- Part 8 (Regression Test): ⏳ PENDING
|
||||
|
||||
## Completion Criteria
|
||||
1. Code committed: agents/, audit/, evidence/, web-ui/
|
||||
2. Doc: docs/architecture/AIOS_AGENT_AUDIT_PLANE.md
|
||||
3. Report: evidence/CMD-20260708.../final-report.md
|
||||
4. Output: AIOS_AGENT_AUDIT_PLANE_READY, AIOS_GIT_ACTION_WRITE_READY
|
||||
5. Next command created: CMD-20260709-autonomous-controller-enable
|
||||
@@ -1,35 +0,0 @@
|
||||
# CMD-20260709-enable-autonomous-controller
|
||||
|
||||
## Command ID
|
||||
CMD-20260709-enable-autonomous-controller
|
||||
|
||||
## Priority
|
||||
P0
|
||||
|
||||
## Mode
|
||||
autonomous
|
||||
|
||||
## Target
|
||||
Primary: OpenClaw
|
||||
Supporting: Hermes
|
||||
Review: Codex
|
||||
|
||||
## Objective
|
||||
Enable autonomous controller for AIOS GitOps:
|
||||
- Auto-detect issues
|
||||
- Auto-diagnose
|
||||
- Auto-generate fixes
|
||||
- Auto-test
|
||||
- Auto-create patches
|
||||
- Auto-request approval
|
||||
|
||||
## Preconditions
|
||||
- Agent Command Plane initialized (CMD-BOOTSTRAP-001)
|
||||
- Git Actions write permission fixed (CMD-20260708)
|
||||
- Audit plane enabled (CMD-20260708)
|
||||
- Notification service enabled (CMD-20260708)
|
||||
|
||||
## Completion Criteria
|
||||
- Autonomous controller service running
|
||||
- Can detect, diagnose, fix, and report
|
||||
- Pipeline: detect → diagnose → fix → test → patch → approve → commit → notify
|
||||
@@ -2,8 +2,8 @@
|
||||
"processed": {
|
||||
"CMD-20260708-fix-git-actions-write-and-enable-audit-plane.md": {
|
||||
"hash": "9442e78e3deb35e03227016b2265d34feb4640f8efdcb07baffe06d52ef947c8",
|
||||
"status": "discovered",
|
||||
"discovered_at": "2026-07-08T19:01:32+0800"
|
||||
"status": "running",
|
||||
"discovered_at": "2026-07-09T00:12:52+0800"
|
||||
},
|
||||
"CMD-20260709-enable-autonomous-controller.md": {
|
||||
"hash": "2825811706389130b2f531a2d3814b800b1181598f871b496e5a05fa13adbbec",
|
||||
@@ -11,5 +11,5 @@
|
||||
"discovered_at": "2026-07-08T19:01:45+0800"
|
||||
}
|
||||
},
|
||||
"last_scan": "2026-07-08T21:06:45+0800"
|
||||
"last_scan": "2026-07-09T10:42:52+0800"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"agent_run_id": "openclaw-1783527172",
|
||||
"command": "CMD-20260708-fix-git-actions-write-and-enable-audit-plane.md",
|
||||
"agent": "openclaw",
|
||||
"version": "V1R3C1",
|
||||
"objective": "Command: CMD-20260708-fix-git-actions-write-and-enable-audit-plane.md",
|
||||
"start_time": "2026-07-09T00:12:52+0800",
|
||||
"end_time": null,
|
||||
"interaction_count": 0,
|
||||
"tool_call_count": 0,
|
||||
"changed_files": [],
|
||||
"commit_sha": null,
|
||||
"test_result": null,
|
||||
"final_result": "running",
|
||||
"evidence_path": "evidence/CMD-20260708-fix-git-actions-write-and-enable-audit-plane/"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
agent_context_isolation:
|
||||
enabled: true
|
||||
architecture:
|
||||
shared_kernel: true
|
||||
group_main_agent: true
|
||||
owner_dedicated_agent: true
|
||||
user_dedicated_agent: false
|
||||
layers:
|
||||
- system_policy
|
||||
- agent_default
|
||||
- group_policy
|
||||
- role_policy
|
||||
- user_profile
|
||||
- session_context
|
||||
priority:
|
||||
session_context: 70
|
||||
user_profile: 50
|
||||
group_policy: 30
|
||||
agent_default: 10
|
||||
safety_policy: immutable
|
||||
forbidden:
|
||||
- user_preferences_in_USER_md
|
||||
- cross_user_profile_access
|
||||
- shared_personalization_state
|
||||
audit:
|
||||
enabled: true
|
||||
events:
|
||||
- context_loaded
|
||||
- profile_updated
|
||||
- agent_selected
|
||||
@@ -0,0 +1,26 @@
|
||||
agent_observability:
|
||||
enabled: true
|
||||
tracked_agents:
|
||||
- openclaw
|
||||
- hermes
|
||||
- codex
|
||||
- sub_agents
|
||||
metrics:
|
||||
- execution_duration
|
||||
- input_tokens
|
||||
- output_tokens
|
||||
- total_tokens
|
||||
- loop_type
|
||||
- delegation_tree
|
||||
loop_types:
|
||||
- openclaw_independent
|
||||
- hermes_orchestrated
|
||||
visibility:
|
||||
user: own_sessions_only
|
||||
group_admin: group_sessions
|
||||
owner: global_audit
|
||||
privacy:
|
||||
token_details_visible: permission_based
|
||||
cross_user_execution_visibility: false
|
||||
audit:
|
||||
enabled: true
|
||||
@@ -0,0 +1,45 @@
|
||||
# CMD-20260709-complete-agent-observability-loop
|
||||
|
||||
## Goal
|
||||
|
||||
Complete the OpenClaw/Hermes/sub-agent observability closed loop.
|
||||
|
||||
## Deploy
|
||||
|
||||
1. Deploy agent observability store.
|
||||
2. Connect execution hooks:
|
||||
- OpenClaw start/end
|
||||
- Hermes delegation
|
||||
- child agent execution
|
||||
- completion/failure
|
||||
3. Enable API:
|
||||
- recordAgentExecution
|
||||
- getAgentObservabilityOverview
|
||||
4. Enable Web UI:
|
||||
- /admin/ai-evolution/agents
|
||||
5. Enforce permission views:
|
||||
- user: own execution
|
||||
- group_admin: group execution
|
||||
- owner: global audit
|
||||
|
||||
## Required metrics
|
||||
|
||||
- independent OpenClaw loops
|
||||
- Hermes orchestrated loops
|
||||
- child agent tree
|
||||
- duration
|
||||
- token usage when available
|
||||
- failure rate
|
||||
|
||||
## Validation signals
|
||||
|
||||
- AIOS_AGENT_OBSERVABILITY_READY
|
||||
- AIOS_AGENT_ROUTING_VISUAL_READY
|
||||
- AIOS_AGENT_COST_METRICS_READY
|
||||
- AIOS_AGENT_OBSERVABILITY_CLOSED_LOOP_READY
|
||||
|
||||
## Governance
|
||||
|
||||
- Do not expose cross-user data.
|
||||
- Token details require permission.
|
||||
- All records are audited.
|
||||
@@ -0,0 +1,34 @@
|
||||
# CMD-20260709-enable-agent-observability-dashboard
|
||||
|
||||
## Goal
|
||||
|
||||
Expose OpenClaw/Hermes/sub-agent scheduling and resource metrics in Web UI.
|
||||
|
||||
## Deploy
|
||||
|
||||
1. Deploy agent observability collector.
|
||||
2. Capture execution records:
|
||||
- entry agent
|
||||
- delegation path
|
||||
- Hermes usage
|
||||
- child agents
|
||||
- duration
|
||||
- token usage when adapter provides it
|
||||
3. Enable dashboard:
|
||||
- /admin/ai-evolution/agents
|
||||
4. Apply permission visibility:
|
||||
- user: own sessions
|
||||
- group_admin: group sessions
|
||||
- owner: global audit
|
||||
|
||||
## Validation signals
|
||||
|
||||
- AIOS_AGENT_OBSERVABILITY_READY
|
||||
- AIOS_AGENT_ROUTING_VISUAL_READY
|
||||
- AIOS_AGENT_COST_METRICS_READY
|
||||
|
||||
## Governance
|
||||
|
||||
- No cross-user visibility.
|
||||
- Token details are permission controlled.
|
||||
- All execution records are audited.
|
||||
@@ -0,0 +1,32 @@
|
||||
# CMD-20260709-enable-group-owner-agent-isolation
|
||||
|
||||
## Goal
|
||||
|
||||
Deploy multi-user agent context isolation:
|
||||
|
||||
- group main agent per group/tenant
|
||||
- dedicated owner agent per owner
|
||||
- user profile and session overlays
|
||||
- no user personalization in shared USER.md
|
||||
|
||||
## Deploy
|
||||
|
||||
1. Deploy context resolver.
|
||||
2. Enable group agent routing.
|
||||
3. Enable owner agent routing.
|
||||
4. Create runtime user profile storage.
|
||||
5. Migrate user-specific preferences out of shared USER.md.
|
||||
6. Enable context audit events.
|
||||
|
||||
## Validation
|
||||
|
||||
- AIOS_USER_PROFILE_ISOLATION_READY
|
||||
- AIOS_GROUP_AGENT_ROUTING_READY
|
||||
- AIOS_OWNER_AGENT_READY
|
||||
- AIOS_CONTEXT_AUDIT_READY
|
||||
|
||||
## Governance
|
||||
|
||||
- Safety policy overrides all user customization.
|
||||
- No cross-user profile access.
|
||||
- No personal profile data in shared Git configuration.
|
||||
@@ -0,0 +1,77 @@
|
||||
# CMD-20260709-fix-openapi-workspace-permission
|
||||
|
||||
## Goal
|
||||
|
||||
Fix Git workspace permission issue that blocks writing OpenAPI files.
|
||||
|
||||
## Symptom
|
||||
|
||||
ChatGPT Actions failed to write:
|
||||
|
||||
```text
|
||||
openapi/agent-context-isolation.yaml
|
||||
```
|
||||
|
||||
Error:
|
||||
|
||||
```text
|
||||
Permission denied: /data/aios-gitops/workspace/openapi/agent-context-isolation.yaml
|
||||
```
|
||||
|
||||
## Execute
|
||||
|
||||
1. Inspect permissions:
|
||||
|
||||
```bash
|
||||
ls -ld /data/aios-gitops/workspace/openapi
|
||||
ls -la /data/aios-gitops/workspace/openapi | head
|
||||
stat /data/aios-gitops/workspace/openapi
|
||||
ps aux | grep -E 'aios-gitops|model-api|actions' | grep -v grep
|
||||
```
|
||||
|
||||
2. Identify runtime write user for Actions API.
|
||||
|
||||
3. Fix ownership/permissions with least privilege. Expected owner should match the GitOps runtime write user, likely `aiman` or the service user.
|
||||
|
||||
Example only after confirming user/group:
|
||||
|
||||
```bash
|
||||
sudo chown -R <runtime_user>:<runtime_group> /data/aios-gitops/workspace/openapi
|
||||
sudo chmod -R u+rwX,g+rwX /data/aios-gitops/workspace/openapi
|
||||
```
|
||||
|
||||
4. Do not make the directory world-writable unless explicitly approved.
|
||||
|
||||
5. Retry creating:
|
||||
|
||||
```text
|
||||
openapi/agent-context-isolation.yaml
|
||||
```
|
||||
|
||||
6. Commit through GitOps patch branch only:
|
||||
|
||||
```text
|
||||
patch/group-owner-agent-context-isolation
|
||||
```
|
||||
|
||||
## Validation
|
||||
|
||||
- OpenAPI file write succeeds.
|
||||
- Git commit succeeds.
|
||||
- No direct main push.
|
||||
- No secret written.
|
||||
- Permission repair is recorded in audit.
|
||||
|
||||
Expected signal:
|
||||
|
||||
```text
|
||||
AIOS_OPENAPI_WORKSPACE_PERMISSION_READY
|
||||
```
|
||||
|
||||
## Forbidden
|
||||
|
||||
- Do not push main.
|
||||
- Do not create tag.
|
||||
- Do not chmod 777 unless owner explicitly approves.
|
||||
- Do not write secrets.
|
||||
- Do not change unrelated directories.
|
||||
@@ -0,0 +1,35 @@
|
||||
# CMD-20260709-wire-agent-observability-hooks
|
||||
|
||||
## Goal
|
||||
|
||||
Connect real OpenClaw/Hermes/Codex execution lifecycle into Agent Observability.
|
||||
|
||||
## Deploy
|
||||
|
||||
1. Hook agent start/end lifecycle.
|
||||
2. Emit events:
|
||||
- agent_started
|
||||
- agent_delegated
|
||||
- token_consumed
|
||||
- agent_completed
|
||||
- agent_failed
|
||||
3. Persist execution metrics.
|
||||
4. Enable Web UI agent dashboard.
|
||||
|
||||
## Validation
|
||||
|
||||
- AIOS_AGENT_OBSERVABILITY_READY
|
||||
- AIOS_AGENT_ROUTING_VISUAL_READY
|
||||
- AIOS_AGENT_COST_METRICS_READY
|
||||
- AIOS_AGENT_OBSERVABILITY_CLOSED_LOOP_READY
|
||||
|
||||
## Notes
|
||||
|
||||
Token usage depends on model adapter instrumentation. If unavailable, record null
|
||||
rather than guessing.
|
||||
|
||||
## Governance
|
||||
|
||||
- Respect user/group/owner visibility.
|
||||
- Do not expose cross-user execution details.
|
||||
- Keep audit records.
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"type": "branch",
|
||||
"branch": "patch/agent-observability-webui",
|
||||
"base": "main",
|
||||
"created_by": "chatgpt-gitops-bot",
|
||||
"created_at": "2026-07-09T02:02:20Z",
|
||||
"direct_main_push": false,
|
||||
"owner_approval_required": true
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
from active_runtime.app.agent_observability_hooks import AgentObservabilityHooks
|
||||
|
||||
|
||||
def test_agent_observability_closed_loop():
|
||||
hooks = AgentObservabilityHooks()
|
||||
started = hooks.start("user1", "group1", "openclaw")
|
||||
execution_id = started["execution_id"]
|
||||
hooks.delegate(execution_id, "hermes")
|
||||
hooks.delegate(execution_id, "codex")
|
||||
hooks.tokens(execution_id, 100, 200)
|
||||
result = hooks.finish(execution_id)
|
||||
|
||||
record = result["record"]
|
||||
assert record["used_hermes"] is True
|
||||
assert "codex" in record["path"]
|
||||
assert record["total_tokens"] == 300
|
||||
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"version": "V1R4C1",
|
||||
"tag": "V1R4C1",
|
||||
"previous_version": "V1R3C1",
|
||||
"rollback_version": "V1R3C1",
|
||||
"baseline": "V1R3C1",
|
||||
"release_reason": "Promote group-owner-agent-context-isolation with Agent Observability and Agent Event Bus unified platform",
|
||||
"purpose": "Deploy complete agent observability, context isolation, and unified event bus pipeline",
|
||||
"changes": [
|
||||
"Group-owner-agent-context-isolation: resolver, RBAC, API, audit, dashboard",
|
||||
"Agent observability: Hermes model, Web UI dashboard, policy, store, API, runtime hooks, event bus bridge, closed loop tests",
|
||||
"Agent Event Bus unified: admin dashboard, configuration, API adapters, openapi operation ids",
|
||||
"AI evolution dashboard API endpoint",
|
||||
"Agent Command Plane and Audit Plane (V1R3C1 baseline)",
|
||||
"OpenAPI workspace permission fix",
|
||||
"ChatGPT Actions API internals refactored with ActionMonitor"
|
||||
],
|
||||
"artifacts": [
|
||||
"active-runtime/app/agent_context_resolver.py",
|
||||
"active-runtime/app/agent_context_rbac.py",
|
||||
"active-runtime/app/agent_context_api.py",
|
||||
"active-runtime/app/agent_context_audit.py",
|
||||
"active-runtime/app/agent_context_dashboard.py",
|
||||
"active-runtime/app/aios_chatgpt_actions_api.py",
|
||||
"config/agent-context-isolation-policy.yaml",
|
||||
"active-runtime/app/agent_observability_model.py",
|
||||
"active-runtime/app/agent_observability_dashboard.py",
|
||||
"active-runtime/app/agent_observability_policy.py",
|
||||
"active-runtime/app/agent_observability_store.py",
|
||||
"active-runtime/app/agent_observability_api.py",
|
||||
"active-runtime/app/agent_observability_hooks.py",
|
||||
"active-runtime/app/agent_event_bus_bridge.py",
|
||||
"active-runtime/app/agent_event_bus_dashboard.py",
|
||||
"active-runtime/app/agent_event_bus_config.py",
|
||||
"active-runtime/app/agent_event_bus_api.py",
|
||||
"tests/test_agent_observability_loop.py"
|
||||
],
|
||||
"validated_actions": [
|
||||
"getRuntimeStatus",
|
||||
"getBaseline",
|
||||
"listVersions",
|
||||
"listPatches",
|
||||
"getDiff",
|
||||
"createBranch",
|
||||
"createPatch",
|
||||
"commitFile"
|
||||
],
|
||||
"secret_plaintext_written": false,
|
||||
"owner_approval_required": true,
|
||||
"owner_notification_channels": ["wechat", "web_ui_audit"],
|
||||
"created_at": "2026-07-09T10:57:00+08:00",
|
||||
"author": "openclaw"
|
||||
}
|
||||
Reference in New Issue
Block a user