Compare commits

..
14 changed files with 3992 additions and 0 deletions
@@ -0,0 +1,470 @@
#!/usr/bin/env python3
"""Canonical AIOS all-modified-files evidence packager.
The bundle contract is intentionally compatible with the historical
aios_package_all_modified_files_v1.sh output while keeping secret scanning in
version-controlled, tested code.
"""
from __future__ import annotations
from datetime import datetime, timezone
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
import zipfile
from typing import Iterable
OID_RE = re.compile(r"^[0-9a-f]{40}$")
TASK_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{2,179}$")
# A PEM private-key finding requires the complete PEM header. Source code,
# tests, docs, or error text containing only "BEGIN PRIVATE KEY" are not
# credentials and must not be rejected.
PEM_PRIVATE_KEY_RE = re.compile(
rb"-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----",
re.IGNORECASE,
)
BEARER_RE = re.compile(
rb"authorization\s*:\s*bearer\s+[A-Za-z0-9._~+/=-]{20,}",
re.IGNORECASE,
)
KEY_VALUE_SECRET_RE = re.compile(
rb"(?:password|passwd|secret|api[_-]?key)\s*[:=]\s*[^\s,;]{12,}",
re.IGNORECASE,
)
DOCUMENT_SUFFIXES = {".md", ".rst", ".adoc", ".txt"}
DOCUMENT_PREFIXES = (
"README",
"CHANGELOG",
"RUNBOOK",
"ADR",
"ARCHITECTURE",
"DEPLOYMENT",
"TROUBLESHOOTING",
)
class PackagerError(RuntimeError):
pass
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def secret_finding(data: bytes) -> str | None:
if PEM_PRIVATE_KEY_RE.search(data):
return "PEM_PRIVATE_KEY"
if BEARER_RE.search(data):
return "AUTHORIZATION_BEARER"
if KEY_VALUE_SECRET_RE.search(data):
return "KEY_VALUE_SECRET"
return None
def scan_secret(data: bytes, label: str) -> None:
finding = secret_finding(data)
if finding is not None:
raise PackagerError(
f"MODIFIED_FILES_SECRET_SCAN_MATCH:{finding}:{label}"
)
def git_bytes(git_dir: Path, oid: str, path: str) -> bytes:
try:
return subprocess.check_output(
[
"/usr/bin/git",
f"--git-dir={git_dir}",
"show",
f"{oid}:{path}",
],
timeout=30,
)
except subprocess.CalledProcessError as exc:
raise PackagerError(
"MODIFIED_FILES_GIT_SHOW_FAILED:" + path
) from exc
def git_text(git_dir: Path, *args: str) -> str:
try:
return subprocess.check_output(
["/usr/bin/git", f"--git-dir={git_dir}", *args],
text=True,
timeout=30,
)
except subprocess.CalledProcessError as exc:
raise PackagerError(
"MODIFIED_FILES_GIT_COMMAND_FAILED:" + " ".join(args)
) from exc
def validate_lineage(git_dir: Path, base: str, candidate: str) -> None:
if OID_RE.fullmatch(base) is None or OID_RE.fullmatch(candidate) is None:
raise PackagerError("MODIFIED_FILES_OID_INVALID")
for oid in (base, candidate):
try:
subprocess.run(
[
"/usr/bin/git",
f"--git-dir={git_dir}",
"cat-file",
"-e",
f"{oid}^{{commit}}",
],
check=True,
timeout=30,
)
except subprocess.CalledProcessError as exc:
raise PackagerError("MODIFIED_FILES_COMMIT_MISSING:" + oid) from exc
if subprocess.run(
[
"/usr/bin/git",
f"--git-dir={git_dir}",
"merge-base",
"--is-ancestor",
base,
candidate,
],
check=False,
timeout=30,
).returncode != 0:
raise PackagerError("MODIFIED_FILES_LINEAGE_INVALID")
def safe_destination(root: Path, relative: str) -> Path:
path = Path(relative)
if path.is_absolute() or ".." in path.parts:
raise PackagerError("MODIFIED_FILE_PATH_INVALID:" + relative)
destination = root / path
destination.parent.mkdir(parents=True, exist_ok=True)
return destination
def is_document_path(path: str) -> bool:
value = Path(path)
if path.startswith("docs/"):
return True
if value.suffix.lower() in DOCUMENT_SUFFIXES:
return True
upper = value.name.upper()
return any(upper.startswith(prefix) for prefix in DOCUMENT_PREFIXES)
def diff_entries(
git_dir: Path,
base: str,
candidate: str,
before_root: Path,
after_root: Path,
) -> list[dict[str, object]]:
raw = git_text(
git_dir,
"diff",
"--name-status",
"-M",
"-C",
base,
candidate,
)
entries: list[dict[str, object]] = []
for line in raw.splitlines():
if not line.strip():
continue
parts = line.split("\t")
status = parts[0]
code = status[0]
old_path: str | None = None
new_path: str | None = None
if code in {"R", "C"}:
if len(parts) != 3:
raise PackagerError(
"MODIFIED_FILES_DIFF_PARSE_INVALID:" + line
)
old_path, new_path = parts[1], parts[2]
else:
if len(parts) != 2:
raise PackagerError(
"MODIFIED_FILES_DIFF_PARSE_INVALID:" + line
)
new_path = parts[1] if code != "D" else None
old_path = parts[1] if code != "A" else None
entry: dict[str, object] = {"status": status}
if old_path is not None:
entry["previous_path"] = old_path
before = git_bytes(git_dir, base, old_path)
scan_secret(before, "before:" + old_path)
safe_destination(before_root, old_path).write_bytes(before)
entry["before_sha256"] = sha256(before)
entry["before_bytes"] = len(before)
if new_path is not None:
entry["path"] = new_path
after = git_bytes(git_dir, candidate, new_path)
scan_secret(after, "after:" + new_path)
safe_destination(after_root, new_path).write_bytes(after)
entry["after_sha256"] = sha256(after)
entry["after_bytes"] = len(after)
entry["deleted"] = code == "D"
entry["added"] = code == "A"
entry["renamed"] = code == "R"
entry["copied"] = code == "C"
entries.append(entry)
return entries
def execution_entries(
extra_root: Path | None,
execution_root: Path,
) -> list[dict[str, object]]:
if extra_root is None or not extra_root.exists():
return []
if extra_root.is_symlink() or not extra_root.is_dir():
raise PackagerError("EXTRA_FILES_DIR_INVALID")
entries: list[dict[str, object]] = []
for path in sorted(extra_root.rglob("*")):
if path.is_symlink():
raise PackagerError(
"EXTRA_FILE_SYMLINK_REJECTED:" + str(path)
)
if not path.is_file():
continue
relative = path.relative_to(extra_root).as_posix()
data = path.read_bytes()
scan_secret(data, "execution:" + relative)
safe_destination(execution_root, relative).write_bytes(data)
entries.append(
{
"path": relative,
"sha256": sha256(data),
"bytes": len(data),
}
)
return entries
def write_checksums(bundle: Path) -> None:
checks: list[tuple[str, str]] = []
for path in sorted(bundle.rglob("*")):
if path.is_file() and path.name != "checksums.sha256":
checks.append(
(
sha256(path.read_bytes()),
path.relative_to(bundle).as_posix(),
)
)
(bundle / "checksums.sha256").write_text(
"".join(f"{digest} {name}\n" for digest, name in checks),
encoding="utf-8",
)
def verify_checksums(bundle: Path) -> None:
checksum_path = bundle / "checksums.sha256"
for line in checksum_path.read_text(encoding="utf-8").splitlines():
digest, relative = line.split(" ", 1)
path = bundle / relative
if not path.is_file() or sha256(path.read_bytes()) != digest:
raise PackagerError(
"MODIFIED_FILES_CHECKSUM_CLOSURE_INVALID:" + relative
)
def build_bundle(
*,
git_dir: Path,
base: str,
candidate: str,
task_id: str,
extra_dir: Path | None = None,
output_root: Path | None = None,
now: datetime | None = None,
) -> dict[str, object]:
if TASK_RE.fullmatch(task_id) is None:
raise PackagerError("TASK_ID_INVALID")
validate_lineage(git_dir, base, candidate)
timestamp = (now or datetime.now(timezone.utc)).strftime(
"%Y%m%dT%H%M%SZ"
)
safe_task = re.sub(r"[^A-Za-z0-9._-]+", "-", task_id).strip("-")
if output_root is None:
output_root = Path(
tempfile.mkdtemp(
prefix=f"aios-modified-files-bundle-{safe_task}-{timestamp}-",
dir="/var/tmp",
)
)
else:
output_root.mkdir(parents=True, exist_ok=True)
bundle = output_root / "bundle"
before_root = bundle / "repo" / "before"
after_root = bundle / "repo" / "after"
execution_root = bundle / "execution"
before_root.mkdir(parents=True)
after_root.mkdir(parents=True)
execution_root.mkdir(parents=True)
changes = diff_entries(
git_dir,
base,
candidate,
before_root,
after_root,
)
execution_files = execution_entries(extra_dir, execution_root)
document_changes = [
entry
for entry in changes
if is_document_path(
str(entry.get("path") or entry.get("previous_path") or "")
)
]
execution_documents = [
entry
for entry in execution_files
if is_document_path(str(entry["path"]))
]
documents_index = {
"schema": "aios.modified-documents.index.v1",
"task_id": task_id,
"base_oid": base,
"candidate_oid": candidate,
"repository_documents": document_changes,
"execution_documents": execution_documents,
"repository_document_count": len(document_changes),
"execution_document_count": len(execution_documents),
}
(bundle / "documents-index.json").write_text(
json.dumps(documents_index, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
manifest = {
"schema": "aios.all-modified-files.bundle.v1",
"task_id": task_id,
"base_oid": base,
"candidate_oid": candidate,
"generated_at": (
now or datetime.now(timezone.utc)
).isoformat(),
"changed_file_count": len(changes),
"execution_file_count": len(execution_files),
"repository_document_count": len(document_changes),
"execution_document_count": len(execution_documents),
"changes": changes,
"execution_files": execution_files,
}
(bundle / "all-modified-files-manifest.json").write_text(
json.dumps(manifest, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
write_checksums(bundle)
verify_checksums(bundle)
zip_name = (
f"AIOS_{task_id}_ALL_MODIFIED_FILES_{candidate[:12]}.zip"
.replace("/", "-")
)
zip_path = output_root / zip_name
with zipfile.ZipFile(
zip_path,
"w",
compression=zipfile.ZIP_DEFLATED,
) as archive:
for path in sorted(bundle.rglob("*")):
if path.is_symlink():
raise PackagerError(
"BUNDLE_SYMLINK_REJECTED:" + str(path)
)
if path.is_file():
archive.write(
path,
arcname=path.relative_to(bundle).as_posix(),
)
zip_sha = sha256(zip_path.read_bytes())
summary = {
"schema": "aios.all-modified-files.bundle-summary.v1",
"task_id": task_id,
"base_oid": base,
"candidate_oid": candidate,
"bundle_path": str(zip_path),
"bundle_sha256": zip_sha,
"changed_file_count": len(changes),
"execution_file_count": len(execution_files),
"repository_document_count": len(document_changes),
"execution_document_count": len(execution_documents),
}
(output_root / "ALL_MODIFIED_FILES_BUNDLE.json").write_text(
json.dumps(summary, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
return summary
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("base")
parser.add_argument("candidate")
parser.add_argument("task_id")
parser.add_argument("extra_dir", nargs="?")
parser.add_argument(
"--authority-git-dir",
default=os.environ.get(
"AIOS_AUTHORITY_GIT_DIR",
"/data/aios-gitops/repo.git",
),
)
parser.add_argument("--output-root")
args = parser.parse_args()
try:
summary = build_bundle(
git_dir=Path(args.authority_git_dir),
base=args.base,
candidate=args.candidate,
task_id=args.task_id,
extra_dir=Path(args.extra_dir) if args.extra_dir else None,
output_root=(
Path(args.output_root) if args.output_root else None
),
)
except PackagerError as exc:
print("FATAL:" + str(exc))
return 20
print("ALL_MODIFIED_FILES_BUNDLE=PASS")
print("ALL_MODIFIED_FILES_BUNDLE_PATH=" + str(summary["bundle_path"]))
print("ALL_MODIFIED_FILES_BUNDLE_SHA256=" + str(summary["bundle_sha256"]))
print("CHANGED_FILE_COUNT=" + str(summary["changed_file_count"]))
print("EXECUTION_FILE_COUNT=" + str(summary["execution_file_count"]))
print(
"REPOSITORY_DOCUMENT_COUNT="
+ str(summary["repository_document_count"])
)
print(
"EXECUTION_DOCUMENT_COUNT="
+ str(summary["execution_document_count"])
)
print(
"ALL_MODIFIED_FILES_EVIDENCE="
+ str(Path(str(summary["bundle_path"])).parent)
)
return 0
if __name__ == "__main__":
raise SystemExit(main())
@@ -37,6 +37,7 @@ _TASK_ALIASES: dict[str, frozenset[str]] = {
"gpu.train": frozenset({"legacy.gpu.train"}),
"agent.runtime.hermes": frozenset({"agent.runtime.hermes"}),
"agent.runtime.openclaw": frozenset({"agent.runtime.openclaw"}),
"release:candidate": frozenset({"release:candidate"}),
}
_WORKER_ALIASES: dict[str, frozenset[str]] = {
@@ -71,6 +72,7 @@ _WORKER_ALIASES: dict[str, frozenset[str]] = {
"gpu.train": frozenset({"legacy.gpu.train"}),
"agent.runtime.hermes": frozenset({"agent.runtime.hermes"}),
"agent.runtime.openclaw": frozenset({"agent.runtime.openclaw"}),
"release:candidate": frozenset({"release:candidate"}),
}
_UNKNOWN_PREFIX = "exact:"
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,636 @@
#!/usr/bin/env python3
"""Persistent release-preparation Online Agent worker.
The worker is intentionally narrower than the generic engineering worker:
it can only consume tasks whose required capability set is exactly
{"release:candidate"} and whose payload asks for immutable release-candidate
creation. It never receives Owner signing, deployment, tag, baseline or Git
write credentials.
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import re
import socket
import threading
import time
import urllib.error
import urllib.request
from typing import Any, Mapping
AGENT_ID = os.environ.get(
"AIOS_RELEASE_WORKER_AGENT_ID",
"online-release-worker-cn-network-mkt-20260819",
).strip()
AGENT_VERSION = os.environ.get(
"AIOS_RELEASE_WORKER_AGENT_VERSION",
"2026-08-19.1-release-candidate",
).strip()
TASK_API = os.environ.get(
"AIOS_AGENT_TASK_API_URL",
"http://127.0.0.1:18940",
).rstrip("/")
RELEASE_API = os.environ.get("AIOS_RELEASE_API_URL", "").rstrip("/")
POLL_SECONDS = int(os.environ.get("AIOS_RELEASE_WORKER_POLL_SECONDS", "3"))
LEASE_SECONDS = int(os.environ.get("AIOS_RELEASE_WORKER_LEASE_SECONDS", "60"))
HEARTBEAT_SECONDS = int(
os.environ.get("AIOS_RELEASE_WORKER_HEARTBEAT_SECONDS", "15")
)
STATE_ROOT = Path(
os.environ.get(
"AIOS_RELEASE_WORKER_STATE_ROOT",
"/var/lib/aios-online-agent-release",
)
)
EXPECTED_CAPABILITIES = frozenset({"release:candidate"})
LIVENESS_PATH = Path(
os.environ.get(
"AIOS_WORKER_LIVENESS_PATH",
str(STATE_ROOT / "worker-liveness.json"),
)
)
CANDIDATE_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{7,255}$")
OID_RE = re.compile(r"^[0-9a-f]{40}$")
VERSION_RE = re.compile(r"^V[0-9]+R[0-9]+C[0-9]+$")
TASK_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{2,179}$")
SECRET_MARKERS = (
"authorization",
"token",
"secret",
"credential",
"password",
"private_key",
"signature",
)
class ReleaseWorkerError(RuntimeError):
def __init__(self, code: str, *, phase: str) -> None:
super().__init__(code)
self.code = code
self.phase = phase
def now() -> str:
return datetime.now(timezone.utc).isoformat()
def canonical(value: object) -> bytes:
return json.dumps(
value,
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
).encode("utf-8")
def sha256(value: bytes) -> str:
return hashlib.sha256(value).hexdigest()
def sanitized(value: Any) -> Any:
if isinstance(value, Mapping):
result: dict[str, Any] = {}
for key, inner in value.items():
lowered = str(key).lower()
if any(marker in lowered for marker in SECRET_MARKERS):
result[str(key)] = "[REDACTED]"
else:
result[str(key)] = sanitized(inner)
return result
if isinstance(value, list):
return [sanitized(item) for item in value]
return value
def audit(event: str, **fields: Any) -> None:
STATE_ROOT.mkdir(parents=True, exist_ok=True, mode=0o750)
path = STATE_ROOT / "events.jsonl"
record = {
"schema": "aios.release-worker.event.v1",
"event": event,
"created_at": now(),
"agent_id": AGENT_ID,
**sanitized(fields),
}
with path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(record, sort_keys=True) + "\n")
os.chmod(path, 0o640)
def sd_notify(message: str) -> None:
target = os.environ.get("NOTIFY_SOCKET", "")
if not target:
return
if target.startswith("@"):
address = "\0" + target[1:]
else:
address = target
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
sock.connect(address)
sock.sendall(message.encode("utf-8"))
except OSError:
# Liveness JSON remains the diagnostic source; systemd will enforce
# WatchdogSec if notify transport is persistently broken.
pass
finally:
sock.close()
def write_liveness(
state: str,
*,
task_id: str | None = None,
stage: str | None = None,
error: str | None = None,
visible_count: int | None = None,
) -> None:
STATE_ROOT.mkdir(parents=True, exist_ok=True, mode=0o750)
value = {
"schema": "aios.online-agent.worker-liveness.v1",
"agent_id": AGENT_ID,
"agent_type": "release-preparation-worker",
"agent_version": AGENT_VERSION,
"pid": os.getpid(),
"state": state,
"last_seen_epoch": time.time(),
"last_seen_at": now(),
"poll_seconds": POLL_SECONDS,
"heartbeat_seconds": HEARTBEAT_SECONDS,
"api_base": TASK_API,
"capabilities": sorted(EXPECTED_CAPABILITIES),
"task_id": task_id,
"stage": stage,
"error": error,
"visible_count": visible_count,
}
LIVENESS_PATH.parent.mkdir(parents=True, exist_ok=True, mode=0o750)
temporary = LIVENESS_PATH.with_name(
"." + LIVENESS_PATH.name + f".{os.getpid()}.tmp"
)
temporary.write_text(
json.dumps(value, ensure_ascii=False, sort_keys=True) + "\n",
encoding="utf-8",
)
os.chmod(temporary, 0o640)
os.replace(temporary, LIVENESS_PATH)
def load_credential(name: str) -> str:
directory = Path(os.environ.get("CREDENTIALS_DIRECTORY", ""))
path = directory / name
if path.is_symlink() or not path.is_file():
raise ReleaseWorkerError(
"CREDENTIAL_UNAVAILABLE:" + name,
phase="startup",
)
value = path.read_text(encoding="utf-8").strip()
if len(value) < 24 or any(ch.isspace() for ch in value):
raise ReleaseWorkerError(
"CREDENTIAL_INVALID:" + name,
phase="startup",
)
return value
def http_json(
base: str,
method: str,
route: str,
token: str,
body: Mapping[str, Any] | None = None,
*,
timeout: int = 30,
) -> tuple[int, dict[str, Any]]:
data = None if body is None else canonical(body)
headers = {"Accept": "application/json"}
if token:
headers["Authorization"] = "Bearer " + token
if data is not None:
headers["Content-Type"] = "application/json"
request = urllib.request.Request(
base + route,
method=method,
data=data,
headers=headers,
)
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
raw, status = response.read(), response.status
except urllib.error.HTTPError as exc:
raw, status = exc.read(), exc.code
except (urllib.error.URLError, TimeoutError) as exc:
raise ReleaseWorkerError(
"HTTP_TRANSPORT_UNAVAILABLE",
phase="network",
) from exc
try:
value = json.loads(raw.decode())
except Exception as exc:
raise ReleaseWorkerError(
"HTTP_RESPONSE_INVALID",
phase="network",
) from exc
return status, value if isinstance(value, dict) else {}
@dataclass(frozen=True)
class ReleaseRequest:
candidate_id: str
source_commit: str
version: str
projection_id: str | None
@classmethod
def from_task(cls, task: Mapping[str, Any]) -> "ReleaseRequest":
if not TASK_RE.fullmatch(str(task.get("task_id") or "")):
raise ReleaseWorkerError("TASK_ID_INVALID", phase="validation")
caps = task.get("required_capabilities")
if (
not isinstance(caps, list)
or frozenset(caps) != EXPECTED_CAPABILITIES
or len(caps) != 1
):
raise ReleaseWorkerError(
"TASK_CAPABILITY_SCOPE_INVALID",
phase="validation",
)
metadata = task.get("metadata")
if not isinstance(metadata, Mapping):
metadata = {}
constraints = task.get("constraints")
if not isinstance(constraints, Mapping):
constraints = {}
release = constraints.get("release_candidate")
if not isinstance(release, Mapping):
release = metadata.get("release_candidate")
if not isinstance(release, Mapping):
release = task.get("release_candidate")
if not isinstance(release, Mapping):
raise ReleaseWorkerError(
"RELEASE_CANDIDATE_PAYLOAD_MISSING",
phase="validation",
)
allowed = {
"candidate_id",
"source_commit",
"version",
"projection_id",
}
if set(release) - allowed:
raise ReleaseWorkerError(
"RELEASE_CANDIDATE_PAYLOAD_FIELDS_INVALID",
phase="validation",
)
candidate_id = str(release.get("candidate_id") or "")
source_commit = str(release.get("source_commit") or "")
version = str(release.get("version") or "")
projection_id = release.get("projection_id")
if CANDIDATE_RE.fullmatch(candidate_id) is None:
raise ReleaseWorkerError(
"RELEASE_CANDIDATE_ID_INVALID",
phase="validation",
)
if OID_RE.fullmatch(source_commit) is None:
raise ReleaseWorkerError(
"RELEASE_SOURCE_COMMIT_INVALID",
phase="validation",
)
if VERSION_RE.fullmatch(version) is None:
raise ReleaseWorkerError(
"RELEASE_VERSION_INVALID",
phase="validation",
)
if projection_id is not None:
projection_id = str(projection_id)
if not re.fullmatch(r"projection-[0-9a-f]{32}", projection_id):
raise ReleaseWorkerError(
"RELEASE_PROJECTION_ID_INVALID",
phase="validation",
)
return cls(
candidate_id=candidate_id,
source_commit=source_commit,
version=version,
projection_id=projection_id,
)
def body(self) -> dict[str, str]:
return {
"candidate_id": self.candidate_id,
"source_commit": self.source_commit,
"version": self.version,
}
class Heartbeat:
def __init__(
self,
agent_token: str,
task_id: str,
lease_token: str,
) -> None:
self.agent_token = agent_token
self.task_id = task_id
self.lease_token = lease_token
self.stage = "claimed"
self.failure: str | None = None
self.stop = threading.Event()
self.thread = threading.Thread(
target=self._run,
name="release-worker-heartbeat",
daemon=True,
)
def first(self) -> None:
status, value = http_json(
TASK_API,
"POST",
f"/v1/tasks/{self.task_id}/heartbeat",
self.agent_token,
{
"lease_token": self.lease_token,
"lease_seconds": LEASE_SECONDS,
"progress": {
"stage": self.stage,
"worker_version": AGENT_VERSION,
},
},
)
if status != 200:
raise ReleaseWorkerError(
"TASK_HEARTBEAT_FAILED",
phase="heartbeat",
)
audit(
"TASK_HEARTBEAT",
task_id=self.task_id,
stage=self.stage,
api_status=status,
)
write_liveness(
"BUSY",
task_id=self.task_id,
stage=self.stage,
)
sd_notify("WATCHDOG=1")
def _run(self) -> None:
while not self.stop.wait(HEARTBEAT_SECONDS):
try:
status, value = http_json(
TASK_API,
"POST",
f"/v1/tasks/{self.task_id}/heartbeat",
self.agent_token,
{
"lease_token": self.lease_token,
"lease_seconds": LEASE_SECONDS,
"progress": {
"stage": self.stage,
"worker_version": AGENT_VERSION,
},
},
)
if status != 200:
self.failure = str(value.get("error") or status)
return
audit(
"TASK_HEARTBEAT",
task_id=self.task_id,
stage=self.stage,
api_status=status,
)
write_liveness(
"BUSY",
task_id=self.task_id,
stage=self.stage,
)
sd_notify("WATCHDOG=1")
except Exception as exc:
self.failure = type(exc).__name__
return
def ensure(self) -> None:
if self.failure:
raise ReleaseWorkerError(
"TASK_LEASE_LOST",
phase="heartbeat",
)
def close(self) -> None:
self.stop.set()
if self.thread.ident is not None:
self.thread.join(timeout=2)
def result_payload(
task_id: str,
release: ReleaseRequest,
builder: Mapping[str, Any],
) -> dict[str, Any]:
builder_candidate = builder.get("candidate_id")
if builder_candidate not in {None, release.candidate_id}:
raise ReleaseWorkerError(
"BUILDER_CANDIDATE_BINDING_INVALID",
phase="release",
)
immutable = {
"candidate_id": release.candidate_id,
"source_commit": release.source_commit,
"version": release.version,
}
return {
"status": "SUCCEEDED",
"classification": "RELEASE_CANDIDATE_CREATED",
"execution_mode": "release_candidate",
"adapter": "release_candidate",
"task_id": task_id,
"agent_id": AGENT_ID,
"release_candidate": immutable,
"release_candidate_request_sha256": sha256(canonical(immutable)),
"builder_response": sanitized(dict(builder)),
"projection_id": release.projection_id,
"writer_lease": "NONE",
"changed_files": [],
"direct_main_write": False,
"production_write": False,
"tag_write": False,
"baseline_write": False,
"runtime_switch": False,
"owner_approval_submitted": False,
"credential_material_exposed": False,
}
def execute_task(
task: Mapping[str, Any],
*,
agent_token: str,
release_token: str,
) -> bool:
task_id = str(task.get("task_id") or "")
release = ReleaseRequest.from_task(task)
status, claimed = http_json(
TASK_API,
"POST",
f"/v1/tasks/{task_id}/claim",
agent_token,
{"lease_seconds": LEASE_SECONDS},
)
if status in {409, 423}:
return False
if status != 200:
raise ReleaseWorkerError(
"TASK_CLAIM_FAILED",
phase="claim",
)
lease_token = str(claimed.get("lease_token") or "")
if len(lease_token) < 16:
raise ReleaseWorkerError(
"TASK_LEASE_TOKEN_INVALID",
phase="claim",
)
attempt = int(claimed.get("attempt") or 0)
audit(
"TASK_CLAIMED",
task_id=task_id,
attempt=attempt,
)
heartbeat = Heartbeat(agent_token, task_id, lease_token)
heartbeat.first()
heartbeat.thread.start()
try:
heartbeat.stage = "release_candidate"
heartbeat.ensure()
status, builder = http_json(
RELEASE_API,
"POST",
"/createReleaseCandidate",
release_token,
release.body(),
timeout=120,
)
if status not in {200, 201, 202}:
raise ReleaseWorkerError(
"RELEASE_CANDIDATE_CREATE_FAILED:"
+ str(builder.get("error") or status),
phase="release",
)
heartbeat.ensure()
result = result_payload(task_id, release, builder)
idempotency_key = (
f"{task_id}-attempt-{attempt}-release-result-v1"
)
status, submitted = http_json(
TASK_API,
"POST",
f"/v1/tasks/{task_id}/result",
agent_token,
{
"lease_token": lease_token,
"idempotency_key": idempotency_key,
"result": result,
},
)
if status not in {200, 201, 202}:
raise ReleaseWorkerError(
"TASK_RESULT_SUBMIT_FAILED",
phase="result",
)
audit(
"RESULT_SUBMITTED",
task_id=task_id,
attempt=attempt,
release_candidate_id=release.candidate_id,
)
return True
finally:
heartbeat.close()
def main() -> int:
if not RELEASE_API:
raise SystemExit("FATAL:AIOS_RELEASE_API_URL_REQUIRED")
agent_token = load_credential("agent_token")
release_token = load_credential("release_candidate_token")
STATE_ROOT.mkdir(parents=True, exist_ok=True, mode=0o750)
audit(
"WORKER_STARTED",
capabilities=sorted(EXPECTED_CAPABILITIES),
release_api=RELEASE_API,
)
write_liveness("STARTING", stage="startup")
sd_notify("READY=1\nSTATUS=release worker ready")
while True:
try:
status, response = http_json(
TASK_API,
"POST",
"/v1/tasks/poll",
agent_token,
{"limit": 100},
)
if status != 200:
raise ReleaseWorkerError(
"TASK_POLL_FAILED",
phase="poll",
)
tasks = response.get("tasks")
if not isinstance(tasks, list):
tasks = []
write_liveness(
"IDLE",
stage="poll",
visible_count=len(tasks),
)
sd_notify("WATCHDOG=1\nSTATUS=release worker poll ok")
handled = False
for raw in tasks:
if not isinstance(raw, Mapping):
continue
caps = raw.get("required_capabilities")
if (
not isinstance(caps, list)
or frozenset(caps) != EXPECTED_CAPABILITIES
):
continue
try:
handled = execute_task(
raw,
agent_token=agent_token,
release_token=release_token,
) or handled
except ReleaseWorkerError as exc:
audit(
"TASK_EXECUTION_FAILED",
task_id=raw.get("task_id"),
phase=exc.phase,
error=exc.code,
)
time.sleep(0 if handled else POLL_SECONDS)
except KeyboardInterrupt:
return 0
except Exception as exc:
audit(
"WORKER_LOOP_ERROR",
error=type(exc).__name__,
)
write_liveness(
"DEGRADED",
stage="loop",
error=type(exc).__name__,
)
time.sleep(POLL_SECONDS)
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,47 @@
[Unit]
Description=AIOS durable Owner authorization broker
After=network.target aios-chatgpt-bridge-v2-staging.service
Wants=aios-chatgpt-bridge-v2-staging.service
[Service]
Type=simple
User=root
Group=aios-executor-exchange
RuntimeDirectory=aios-owner-authorization-broker
RuntimeDirectoryMode=0750
StateDirectory=aios-owner-authorization-broker
StateDirectoryMode=0750
LoadCredential=agent_task_control_token:/etc/aios-gitops/agent-task-control.token
Environment=AIOS_AGENT_TASK_CONTROL_TOKEN_FILE=%d/agent_task_control_token
Environment=AIOS_OWNER_DURABLE_AUTHORIZATION_POLICY=/etc/aios-gitops/owner-durable-authorization.json
Environment=AIOS_OWNER_DURABLE_AUTHORIZATION_REVOCATIONS=/etc/aios-gitops/owner-durable-authorization-revocations.json
Environment=AIOS_OWNER_AUTH_BROKER_ACCOUNT=chatgpt-governed-implementer-singlehost
Environment=AIOS_OWNER_AUTH_BROKER_SOCKET=%t/aios-owner-authorization-broker/broker.sock
ExecStart=/usr/bin/python3 -B /opt/aios/current/active-runtime/app/aios_durable_owner_authorization.py
Restart=on-failure
RestartSec=2
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
ProtectKernelLogs=yes
ProtectClock=yes
LockPersonality=yes
RestrictSUIDSGID=yes
RestrictRealtime=yes
RestrictNamespaces=yes
MemoryDenyWriteExecute=yes
RestrictAddressFamilies=AF_UNIX AF_INET
IPAddressDeny=any
IPAddressAllow=127.0.0.0/8
ReadOnlyPaths=/etc/aios-gitops/owner-durable-authorization.json
ReadOnlyPaths=/etc/aios-gitops/owner-durable-authorization-revocations.json
InaccessiblePaths=/data/aios-gitops /home
UMask=0007
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,50 @@
[Unit]
Description=AIOS release-candidate Online Agent worker
After=network.target aios-agent-task-api.service
Wants=aios-agent-task-api.service
[Service]
Type=notify
NotifyAccess=main
WatchdogSec=90
User=root
Group=root
StateDirectory=aios-online-agent-release
StateDirectoryMode=0750
LoadCredentialEncrypted=agent_token:/etc/credstore.encrypted/aios-release-online-agent-worker/agent_token
LoadCredentialEncrypted=release_candidate_token:/etc/credstore.encrypted/aios-release-online-agent-worker/release_candidate_token
Environment=AIOS_RELEASE_WORKER_AGENT_ID=online-release-worker-cn-network-mkt-20260819
Environment=AIOS_RELEASE_WORKER_AGENT_VERSION=2026-08-19.1-release-candidate
Environment=AIOS_AGENT_TASK_API_URL=http://127.0.0.1:18940
Environment=AIOS_RELEASE_WORKER_STATE_ROOT=/var/lib/aios-online-agent-release
Environment=AIOS_WORKER_STATE_ROOT=/var/lib/aios-online-agent-release
Environment=AIOS_RELEASE_WORKER_POLL_SECONDS=3
Environment=AIOS_RELEASE_WORKER_LEASE_SECONDS=60
Environment=AIOS_RELEASE_WORKER_HEARTBEAT_SECONDS=15
Environment=AIOS_RELEASE_API_URL=http://127.0.0.1:18890
ExecStart=/usr/bin/python3 -B /opt/aios/current/active-runtime/app/aios_release_online_agent_worker.py
Restart=always
RestartSec=2
NoNewPrivileges=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
ProtectKernelLogs=yes
ProtectClock=yes
LockPersonality=yes
RestrictSUIDSGID=yes
RestrictRealtime=yes
RestrictNamespaces=yes
MemoryDenyWriteExecute=yes
RestrictAddressFamilies=AF_UNIX AF_INET
IPAddressDeny=any
IPAddressAllow=127.0.0.0/8
InaccessiblePaths=/data/aios-gitops /home
UMask=0077
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,96 @@
# Canonical all-modified-files packager and secret scanning
## Problem
The historical all-modified-files evidence packager lived only as a copied handoff script.
Its private-key scanner matched the fragment:
```text
BEGIN PRIVATE KEY
```
instead of a complete PEM header. A regression test containing that literal therefore caused a
false secret finding even though the candidate contained no credential.
A task had to use a temporary evidence-only compatibility packager to complete its bundle. That is
acceptable as an emergency workaround but not as a durable unattended path.
## Repository source of truth
The canonical implementation is now:
```text
active-runtime/app/aios_all_modified_files_packager.py
```
It preserves the historical output/bundle schemas:
```text
aios.all-modified-files.bundle.v1
aios.all-modified-files.bundle-summary.v1
aios.modified-documents.index.v1
```
and the host-visible status lines:
```text
ALL_MODIFIED_FILES_BUNDLE=PASS
ALL_MODIFIED_FILES_BUNDLE_PATH=...
ALL_MODIFIED_FILES_BUNDLE_SHA256=...
```
Future governed tasks should use this repository version instead of copying an unversioned scanner.
## Secret scanning invariant
Private-key detection requires a complete PEM header:
```text
-----BEGIN [RSA|EC|DSA|OPENSSH ]PRIVATE KEY-----
```
The following source/test/document text is not secret material and must remain packageable:
```text
BEGIN PRIVATE KEY
PRIVATE KEY
PEM private-key header
```
Bearer and key/value credential detection remains fail-closed:
```text
Authorization: Bearer <20+ token chars>
password=<12+ non-space chars>
passwd=<12+ non-space chars>
secret=<12+ non-space chars>
api_key=<12+ non-space chars>
api-key=<12+ non-space chars>
```
Tests construct the real PEM header dynamically so the scanner's own test source does not contain a
literal credential-looking header.
## Bundle integrity
The packager continues to enforce:
- exact base→candidate ancestry;
- before/after file capture;
- rename/copy/delete handling;
- path traversal denial;
- execution evidence symlink denial;
- secret scan over repository before/after and execution evidence;
- document index;
- SHA-256 closure;
- ZIP without symlinks.
No secret finding is downgraded to a warning.
## Migration
The old handoff script may remain for historical reproducibility, but new handoffs should call the
repository packager from the verified candidate lineage.
A temporary compatibility scanner is no longer needed once this candidate is in the lineage.
@@ -0,0 +1,228 @@
# Durable Owner Authorization and capability projection
## Problem
AIOS already supports unattended worker execution after a Task is executable, but Owner intent was
not durable at the capability layer. The same Owner repeatedly had to authorize or repair:
- `governed-task:project`;
- `release:candidate`.
This caused valid one-time Owner intent to be lost between conversations and forced repeated human
interaction.
## Design
Durable authorization is represented by a root-owned mode `0600` policy outside Git. The policy is
persistent until explicitly revoked.
The first persistable capabilities are deliberately narrow:
```text
governed-task:project
release:candidate
```
They cover preparation work only.
The following capabilities can never be inherited from this durable grant:
```text
submitOwnerApproval
startDeployment
tag_publish
baseline_registry_finalize
runtime_switch
main_write
production_deploy
force_push
credential_admin
```
Those remain separate high-risk release/Owner-signature gates.
## Policy
Example schema:
```json
{
"schema": "aios.owner-durable-authorization.v1",
"grant_id": "owner-permanent-preparation-20260819",
"owner_identity": "owner@example",
"account": "chatgpt-governed-implementer-singlehost",
"capabilities": [
"governed-task:project",
"release:candidate"
],
"issued_at": "2026-08-19T05:00:00Z",
"not_before": "2026-08-19T05:00:00Z",
"persistent_until_revoked": true,
"constraints": {
"repository": "aios/aios",
"allowed_target_prefix": "agent/",
"release_version_prefix": "V2R2C",
"max_task_risk": "engineering"
}
}
```
The repository never stores the live Owner policy or release credential.
## Revocation
A root-owned mode `0600` revocation file lists `revoked_grant_ids`.
Revocation is checked on every operation, not only at service startup. A running broker therefore
fails closed immediately after revocation without needing a new conversation.
## Broker boundary
The broker exposes only an AF_UNIX socket:
```text
/run/aios-owner-authorization-broker/broker.sock
```
The socket is mode `0660` and owned by the service's `aios-executor-exchange` group. This avoids introducing another long-lived bearer secret solely for local broker access.
It exposes only:
```text
GET /v1/capabilities
POST /v1/project-governed-task
POST /v1/authorize-release-candidate
```
`project-governed-task` calls only the canonical Bridge.
`authorize-release-candidate` persists an Owner authorization decision only. It deliberately does
**not** invent a local trusted-builder endpoint. The external release connector remains responsible
for projecting the account's real `release:candidate` scope. Until that external projection exists,
the decision state is `AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION`.
The broker holds only the existing host control credential needed for canonical governed-task
projection. It never holds an Owner signing key or a release/deployment credential. The caller also supplies the expected account identifier over the Unix transport; the OS socket permission is the local access boundary.
## Canonical Online Task projection
A durable `governed-task:project` grant may create a complete canonical Online Task rather than a
bare Task. It requires:
```text
require_online_task_projection=true
generation=1
branch_mode=CREATE_NEW
max_attempts<=3
risk<=engineering
initial action=source_capture
```
Projected task capabilities are limited to:
```text
git.read
git.test
governed.write.branch
governed.write.file
```
A write-capable task still begins as `source_capture`. Existing placeholder-deferral prevents the
generic worker from claiming it until a deterministic plan is registered. Repository mutation
therefore still requires the normal deterministic plan, WriterLease and governed commit path.
This fixes the previous split-brain behavior where a bare Task could exist without the canonical
governed execution projection.
## External capability projection outbox
A `release:candidate` authorization now creates a durable machine-readable outbox record:
```text
schema=aios.external-capability-projection.request.v1
state=PENDING_EXTERNAL_CONNECTOR
capability=release:candidate
```
The projection identifier is deterministic from:
```text
grant_id
account
capability
request_sha256
```
The outbox stores no credentials or Owner signing material. It retains only the already-authorized
release-candidate request, policy/request hashes, account, grant id and timestamps.
The broker exposes read-only Unix-socket endpoints:
```text
GET /v1/projections
GET /v1/projections/{projection_id}
```
These endpoints re-check the current Owner policy on every read. If the durable grant has been
revoked, enumeration fails closed with `OWNER_GRANT_REVOKED`. The historical outbox record is
retained for audit and is not silently deleted.
The repository intentionally does **not** provide a connector acknowledgement/write endpoint yet.
No in-repository code can truthfully assert that the external ChatGPT connector account has gained
`release:candidate`. Until the platform consumes the outbox and performs its own identity-scope
projection, the only valid state is:
```text
AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION
PENDING_EXTERNAL_CONNECTOR
```
A future external projector must preserve:
```text
release:candidate
!= submitOwnerApproval
!= startDeployment
!= tag_publish
!= baseline_registry_finalize
!= runtime_switch
```
## Idempotency and audit
Every broker operation requires an idempotency key. A receipt SQLite database stores only:
- idempotency key;
- capability;
- request SHA-256;
- response SHA-256;
- sanitized upstream response;
- timestamp.
An exact retry returns the same receipt. Reusing an idempotency key with different content fails.
## Unattended invariant
Once the Owner policy and narrow service credentials are provisioned:
```text
Owner durable grant
→ broker decision
→ canonical governed task projection / release candidate preparation
→ worker / trusted builder
```
No repeated Owner interaction is required for those preparation capabilities.
Production execution remains:
```text
release candidate
→ immutable approval envelope
→ Owner signature
→ deployment
```
The durable grant never substitutes for the Owner signature.
@@ -0,0 +1,87 @@
# Release candidate capability contract remediation
## Problem
The release-preparation worker can be correctly registered with the raw capability:
```text
release:candidate
```
while the canonical Task API matcher still rejects the worker.
Before this repair, the capability matrix is:
```text
worker raw = ["release:candidate"]
worker effective= []
task required = ["exact:release:candidate"]
eligible = false
```
The Task therefore remains `QUEUED`, `attempt=0`, and cannot be claimed even though both the Task and
worker use the same literal capability.
## Root cause
`aios_capability_contract.py` is deliberately fail-closed:
- task-side unknown names become `exact:<name>`;
- worker-side unknown names are discarded;
- wildcard worker capabilities are ignored.
`release:candidate` was absent from both canonical alias tables.
## Repair
Add exactly one semantic capability to both vocabularies:
```text
release:candidate → release:candidate
```
Do not add:
```text
release:*
submitOwnerApproval
startDeployment
tag_publish
baseline_registry_finalize
runtime_switch
```
Unknown names and wildcards must continue to fail closed.
## Runtime recovery
After the governed remediation candidate is officially verified:
1. install the candidate capability-contract module as a higher-precedence Task API overlay;
2. restart only `aios-agent-task-api.service`;
3. require `/health` success;
4. require the fixed existing release Task to remain the same Task/idempotency;
5. let the already-active dedicated release worker poll and claim it automatically;
6. require heartbeat, trusted-builder result and official verification;
7. do not manually claim or create a replacement live Task.
The fixed V2R2C2 live Task may complete as the acceptance candidate it was originally bound to.
Because this remediation is a later source commit, the first release candidate that includes the
matcher repair should use the next immutable version (for example `V2R2C3`) rather than rewriting an
already-created V2R2C2 candidate.
## Governance boundary
This repair affects only Task eligibility. It grants no credential and performs no release action by
itself.
The dedicated release worker still requires its independent `release:candidate` credential, and the
following remain separate:
```text
Owner signed approval
deployment
tag publication
baseline finalization
runtime switch
```
@@ -0,0 +1,142 @@
# Release Preparation Online Agent
## Purpose
This worker closes the release-preparation execution gap without widening the ChatGPT connector
identity.
The dedicated Agent identity is:
```text
online-release-worker-cn-network-mkt-20260819
```
Its only advertised task capability is:
```text
release:candidate
```
The worker may create an immutable release candidate, but it never submits Owner approval and never
starts deployment, publishes a tag, finalizes a baseline, switches runtime or writes Git.
## Credentials
The worker uses two independently scoped credentials:
```text
agent_token
release_candidate_token
```
`agent_token` is the Online Agent registration credential. It must be encrypted into systemd's
credential store and must never be written to Git, Task metadata, evidence or logs.
`release_candidate_token` is provisioned during Owner-authorized runtime activation. Only its SHA-256 record is stored in the release API token registry; the plaintext is immediately encrypted into systemd credstore and the temporary plaintext is removed. Its only allowed scope is:
```text
release:candidate
```
It is explicitly not an Owner signing key and must not grant:
```text
submitOwnerApproval
startDeployment
tag_publish
baseline_registry_finalize
runtime_switch
```
The unit reads both credentials through systemd `LoadCredentialEncrypted`; neither plaintext token is kept in the unit, repository, Task metadata, evidence, or logs.
## Task contract
The worker will only claim Tasks whose exact required capability set is:
```json
["release:candidate"]
```
The Task must provide a bounded `constraints.release_candidate` payload (metadata is accepted only for compatibility):
```json
{
"candidate_id": "...",
"source_commit": "<40 hex>",
"version": "V2R2C2",
"projection_id": "projection-..."
}
```
No caller-supplied URL, method, headers, command, shell, environment, credential path, deployment
operation or arbitrary release route is accepted.
The release API base is host configuration, not Task input. The only release mutation invoked by the
worker is:
```text
POST /createReleaseCandidate
```
## Lifecycle
```text
poll
→ claim
→ heartbeat
→ createReleaseCandidate
→ result submit
→ official verifier
```
The result declares:
```text
writer_lease=NONE
changed_files=[]
direct_main_write=false
production_write=false
tag_write=false
baseline_write=false
runtime_switch=false
owner_approval_submitted=false
```
## Remaining high-risk gate
Successful release-candidate creation does not authorize Production.
The next stage remains:
```text
release candidate
→ immutable approval envelope
→ Owner signature
→ startDeployment
```
That separation is mandatory even when release preparation is fully unattended.
## Unattended availability
The release worker has its own systemd watchdog rather than modifying the shared worker-availability
guard contract.
Every successful Task poll and Task heartbeat emits:
```text
WATCHDOG=1
```
and the service uses:
```text
Type=notify
WatchdogSec=90
Restart=always
```
A process that is technically alive but no longer polling/heartbeating therefore gets restarted by
systemd. The worker also writes `worker-liveness.json` for operator diagnostics.
+252
View File
@@ -0,0 +1,252 @@
from __future__ import annotations
from datetime import datetime, timezone
import json
from pathlib import Path
import subprocess
import sys
import zipfile
import pytest
ROOT=Path(__file__).resolve().parents[1]
APP=ROOT/"active-runtime/app"
sys.path.insert(0,str(APP))
import aios_all_modified_files_packager as packager
def init_repo(tmp_path: Path) -> tuple[Path, Path, str]:
bare=tmp_path/"repo.git"
work=tmp_path/"work"
subprocess.run(["git","init","--bare",str(bare)],check=True,capture_output=True)
subprocess.run(["git","clone",str(bare),str(work)],check=True,capture_output=True)
subprocess.run(["git","-C",str(work),"config","user.email","test@example.invalid"],check=True)
subprocess.run(["git","-C",str(work),"config","user.name","AIOS Test"],check=True)
(work/"README.md").write_text("base\n",encoding="utf-8")
subprocess.run(["git","-C",str(work),"add","README.md"],check=True)
subprocess.run(["git","-C",str(work),"commit","-m","base"],check=True,capture_output=True)
subprocess.run(["git","-C",str(work),"push","origin","HEAD:refs/heads/main"],check=True,capture_output=True)
base=subprocess.check_output(["git","-C",str(work),"rev-parse","HEAD"],text=True).strip()
return bare,work,base
def commit(work: Path, message: str) -> str:
subprocess.run(["git","-C",str(work),"add","-A"],check=True)
subprocess.run(["git","-C",str(work),"commit","-m",message],check=True,capture_output=True)
candidate=subprocess.check_output(["git","-C",str(work),"rev-parse","HEAD"],text=True).strip()
subprocess.run(["git","-C",str(work),"push","origin","HEAD:refs/heads/main"],check=True,capture_output=True)
return candidate
def real_pem_header() -> bytes:
return b"-----" + b"BEGIN " + b"PRIVATE KEY" + b"-----"
def bearer_value() -> bytes:
return b"Authorization" + b": Bearer " + (b"a"*40)
def key_value_secret() -> bytes:
return b"api_key" + b"=" + (b"x"*24)
def test_plain_private_key_words_are_not_secret_findings():
for value in (
b"BEGIN PRIVATE KEY",
b"PRIVATE KEY",
b"PEM private-key header",
b"assert 'BEGIN PRIVATE KEY' in message",
):
assert packager.secret_finding(value) is None
def test_complete_pem_private_key_header_is_rejected():
assert packager.secret_finding(real_pem_header())=="PEM_PRIVATE_KEY"
assert packager.secret_finding(
b"-----BEGIN OPENSSH " + b"PRIVATE KEY-----"
)=="PEM_PRIVATE_KEY"
def test_bearer_and_key_value_secret_detection_remains_fail_closed():
assert packager.secret_finding(bearer_value())=="AUTHORIZATION_BEARER"
assert packager.secret_finding(key_value_secret())=="KEY_VALUE_SECRET"
def test_short_or_descriptive_secret_words_do_not_false_positive():
assert packager.secret_finding(b"secret=short") is None
assert packager.secret_finding(b"password field is validated") is None
assert packager.secret_finding(b"api_key example placeholder") is None
def test_end_to_end_bundle_accepts_literal_begin_private_key_text(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"tests").mkdir()
(work/"tests"/"test_text.py").write_text(
'ERROR = "BEGIN PRIVATE KEY is not a complete PEM header"\n',
encoding="utf-8",
)
(work/"docs").mkdir()
(work/"docs"/"note.md").write_text("Scanner regression.\n",encoding="utf-8")
candidate=commit(work,"candidate")
extra=tmp_path/"execution"
extra.mkdir()
(extra/"evidence.json").write_text('{"status":"PASS"}\n',encoding="utf-8")
out=tmp_path/"out"
summary=packager.build_bundle(
git_dir=bare,
base=base,
candidate=candidate,
task_id="r42-packager-test",
extra_dir=extra,
output_root=out,
now=datetime(2026,8,19,tzinfo=timezone.utc),
)
assert summary["changed_file_count"]==2
assert summary["execution_file_count"]==1
assert summary["repository_document_count"]==1
assert summary["execution_document_count"]==0
bundle_path=Path(summary["bundle_path"])
assert bundle_path.is_file()
assert packager.sha256(bundle_path.read_bytes())==summary["bundle_sha256"]
with zipfile.ZipFile(bundle_path) as archive:
names=set(archive.namelist())
assert "repo/after/tests/test_text.py" in names
assert "repo/after/docs/note.md" in names
assert "execution/evidence.json" in names
assert "documents-index.json" in names
assert "all-modified-files-manifest.json" in names
assert "checksums.sha256" in names
manifest=json.loads(archive.read("all-modified-files-manifest.json"))
assert manifest["schema"]=="aios.all-modified-files.bundle.v1"
assert manifest["candidate_oid"]==candidate
packager.verify_checksums(out/"bundle")
def test_end_to_end_bundle_rejects_real_pem_header(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"credential.txt").write_bytes(
real_pem_header()+b"\nnot-a-real-key-body\n"
)
candidate=commit(work,"credential-like candidate")
with pytest.raises(
packager.PackagerError,
match="PEM_PRIVATE_KEY:after:credential.txt",
):
packager.build_bundle(
git_dir=bare,
base=base,
candidate=candidate,
task_id="r42-packager-pem-reject",
output_root=tmp_path/"out",
)
def test_execution_evidence_bearer_is_rejected(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"normal.txt").write_text("normal\n",encoding="utf-8")
candidate=commit(work,"normal")
extra=tmp_path/"execution"
extra.mkdir()
(extra/"unsafe.txt").write_bytes(bearer_value()+b"\n")
with pytest.raises(
packager.PackagerError,
match="AUTHORIZATION_BEARER:execution:unsafe.txt",
):
packager.build_bundle(
git_dir=bare,
base=base,
candidate=candidate,
task_id="r42-packager-bearer-reject",
extra_dir=extra,
output_root=tmp_path/"out",
)
def test_execution_symlink_is_rejected(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"normal.txt").write_text("normal\n",encoding="utf-8")
candidate=commit(work,"normal")
extra=tmp_path/"execution"
extra.mkdir()
target=extra/"target.txt"
target.write_text("safe\n",encoding="utf-8")
(extra/"link.txt").symlink_to(target)
with pytest.raises(
packager.PackagerError,
match="EXTRA_FILE_SYMLINK_REJECTED",
):
packager.build_bundle(
git_dir=bare,
base=base,
candidate=candidate,
task_id="r42-packager-symlink-reject",
extra_dir=extra,
output_root=tmp_path/"out",
)
def test_non_ancestor_lineage_is_rejected(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"one.txt").write_text("one\n",encoding="utf-8")
first=commit(work,"first")
subprocess.run(["git","-C",str(work),"checkout","--detach",base],check=True,capture_output=True)
(work/"other.txt").write_text("other\n",encoding="utf-8")
subprocess.run(["git","-C",str(work),"add","other.txt"],check=True)
subprocess.run(["git","-C",str(work),"commit","-m","other"],check=True,capture_output=True)
other=subprocess.check_output(["git","-C",str(work),"rev-parse","HEAD"],text=True).strip()
subprocess.run(
["git","-C",str(work),"push","origin","HEAD:refs/heads/other"],
check=True,
capture_output=True,
)
with pytest.raises(packager.PackagerError,match="LINEAGE_INVALID"):
packager.validate_lineage(bare,first,other)
def test_bundle_checksum_closure_detects_tamper(tmp_path):
bare,work,base=init_repo(tmp_path)
(work/"normal.txt").write_text("normal\n",encoding="utf-8")
candidate=commit(work,"normal")
out=tmp_path/"out"
packager.build_bundle(
git_dir=bare,
base=base,
candidate=candidate,
task_id="r42-packager-checksum",
output_root=out,
)
(out/"bundle"/"repo"/"after"/"normal.txt").write_text(
"tampered\n",
encoding="utf-8",
)
with pytest.raises(
packager.PackagerError,
match="CHECKSUM_CLOSURE_INVALID",
):
packager.verify_checksums(out/"bundle")
def test_source_does_not_contain_complete_pem_header_literal():
source=(ROOT/"active-runtime/app/aios_all_modified_files_packager.py").read_bytes()
tests=(ROOT/"tests/test_all_modified_files_packager.py").read_bytes()
complete=real_pem_header()
assert complete not in source
assert complete not in tests
def test_document_path_contract():
assert packager.is_document_path("docs/spec.md")
assert packager.is_document_path("README")
assert packager.is_document_path("CHANGELOG.md")
assert not packager.is_document_path("active-runtime/app/main.py")
+627
View File
@@ -0,0 +1,627 @@
from __future__ import annotations
from datetime import datetime, timezone
import json
from pathlib import Path
import sqlite3
import sys
import pytest
ROOT=Path(__file__).resolve().parents[1]
APP=ROOT/"active-runtime/app"
sys.path.insert(0,str(APP))
import aios_durable_owner_authorization as auth
NOW=datetime(2026,8,19,6,0,0,tzinfo=timezone.utc)
ACCOUNT="chatgpt-governed-implementer-singlehost"
def write_root_file(path: Path, value: object) -> Path:
path.parent.mkdir(parents=True,exist_ok=True)
path.write_text(json.dumps(value,sort_keys=True)+"\n",encoding="utf-8")
path.chmod(0o600)
return path
def policy_value(**overrides):
value={
"schema":auth.POLICY_SCHEMA,
"grant_id":"owner-permanent-preparation-20260819",
"owner_identity":"owner-singlehost",
"account":ACCOUNT,
"capabilities":[
"governed-task:project",
"release:candidate",
],
"issued_at":"2026-08-19T05:00:00Z",
"not_before":"2026-08-19T05:00:00Z",
"persistent_until_revoked":True,
"constraints":{
"repository":"aios/aios",
"allowed_target_prefix":"agent/",
"release_version_prefix":"V2R2C",
"max_task_risk":"engineering",
},
}
value.update(overrides)
return value
def load_policy(tmp_path,monkeypatch,**overrides):
path=write_root_file(tmp_path/"policy.json",policy_value(**overrides))
revocations=write_root_file(
tmp_path/"revocations.json",
{"revoked_grant_ids":[]},
)
monkeypatch.setattr(auth,"validate_root_file",lambda path,**kwargs:path)
return auth.DurablePolicy.load(
path,
revocations,
now=NOW,
),path,revocations
def governed_request(**overrides):
value={
"task_id":"r42-owner-auth-projection-test-20260819",
"repository":"aios/aios",
"base_oid":"8"*40,
"expected_branch_oid":"8"*40,
"target_branch":"agent/r42-owner-auth-projection-test-20260819",
"generation":1,
"risk":"engineering",
"description":"project existing governed task",
"idempotency_key":"binding-owner-auth-projection-v1",
"branch_mode":"CREATE_NEW",
"priority":100,
"max_attempts":3,
"require_online_task_projection":True,
"online_task":{
"goal":"Analyze source read-only.",
"instructions":"Use source capture first.",
"success_criteria":[
{"id":"source","description":"Return source snapshot."}
],
"constraints":{"repository_mutation":"forbidden"},
"required_capabilities":["git.read","git.test"],
"priority":100,
"max_attempts":3,
},
"execution":{
"action":"source_capture",
"adapter":"source_capture",
"capability_scope":["git.read","git.test"],
"source_signals":["durable authorization projection"],
},
}
value.update(overrides)
return value
def release_request(**overrides):
value={
"candidate_id":"r42-pf1-release-candidate-20260819",
"source_commit":"8"*40,
"version":"V2R2C2",
}
value.update(overrides)
return value
class FakeUpstream:
def __init__(self):
self.project_calls=[]
def project_governed_task(self,body):
self.project_calls.append(dict(body))
return 200,{
"task_id":body["task_id"],
"job_id":"job-"+"a"*32,
"state":"BOUND",
}
def broker(tmp_path,monkeypatch):
policy,path,revocations=load_policy(tmp_path,monkeypatch)
store=auth.ReceiptStore(tmp_path/"receipts.sqlite3")
upstream=FakeUpstream()
instance=auth.Broker(
policy_path=path,
revocation_path=revocations,
store=store,
upstream=upstream,
expected_account=ACCOUNT,
)
monkeypatch.setattr(
auth.DurablePolicy,
"load",
classmethod(lambda cls,*args,**kwargs:policy),
)
return instance,upstream,store,policy
def test_allowed_persistent_capabilities_are_only_preparation_scopes():
assert auth.ALLOWED_PERSISTENT_CAPABILITIES=={
"governed-task:project",
"release:candidate",
}
assert auth.ALLOWED_PERSISTENT_CAPABILITIES.isdisjoint(
auth.FORBIDDEN_PERSISTENT_CAPABILITIES
)
@pytest.mark.parametrize(
"capability",
sorted(auth.FORBIDDEN_PERSISTENT_CAPABILITIES),
)
def test_policy_rejects_high_risk_persistent_capability(
tmp_path,monkeypatch,capability
):
bad=policy_value(
capabilities=["governed-task:project",capability]
)
path=write_root_file(tmp_path/"policy.json",bad)
revocations=write_root_file(
tmp_path/"revocations.json",
{"revoked_grant_ids":[]},
)
monkeypatch.setattr(auth,"validate_root_file",lambda path,**kwargs:path)
with pytest.raises(
auth.AuthorizationError,
match="POLICY_CAPABILITY_NOT_PERSISTABLE",
):
auth.DurablePolicy.load(path,revocations,now=NOW)
def test_policy_is_persistent_until_revoked(tmp_path,monkeypatch):
policy,_,_=load_policy(tmp_path,monkeypatch)
assert policy.persistent_until_revoked is True
assert policy.account==ACCOUNT
assert policy.capabilities=={
"governed-task:project",
"release:candidate",
}
def test_revocation_fails_closed(tmp_path,monkeypatch):
path=write_root_file(tmp_path/"policy.json",policy_value())
revocations=write_root_file(
tmp_path/"revocations.json",
{"revoked_grant_ids":["owner-permanent-preparation-20260819"]},
)
monkeypatch.setattr(auth,"validate_root_file",lambda path,**kwargs:path)
with pytest.raises(auth.AuthorizationError,match="OWNER_GRANT_REVOKED"):
auth.DurablePolicy.load(path,revocations,now=NOW)
def test_future_policy_is_not_active(tmp_path,monkeypatch):
with pytest.raises(auth.AuthorizationError,match="POLICY_NOT_ACTIVE"):
load_policy(
tmp_path,
monkeypatch,
issued_at="2026-08-20T05:00:00Z",
not_before="2026-08-20T05:00:00Z",
)
def test_governed_projection_request_is_tightly_constrained(
tmp_path,monkeypatch
):
policy,_,_=load_policy(tmp_path,monkeypatch)
decision=policy.authorize(
"governed-task:project",
governed_request(),
)
assert decision["decision"]=="ALLOW"
assert decision["persistent_until_revoked"] is True
bad_caps=governed_request()
bad_caps["online_task"]={
**bad_caps["online_task"],
"required_capabilities":["release:candidate"],
}
bad_action=governed_request()
bad_action["execution"]={
**bad_action["execution"],
"action":"deterministic",
"adapter":"deterministic",
}
bad_scope=governed_request()
bad_scope["execution"]={
**bad_scope["execution"],
"capability_scope":["git.read"],
}
for bad in (
governed_request(repository="other/repo"),
governed_request(target_branch="main"),
governed_request(generation=2),
governed_request(risk="high"),
governed_request(expected_branch_oid="9"*40),
governed_request(branch_mode="CONTINUE_EXISTING"),
governed_request(priority=101),
governed_request(max_attempts=4),
governed_request(require_online_task_projection=False),
bad_caps,
bad_action,
bad_scope,
):
with pytest.raises(auth.AuthorizationError):
policy.authorize("governed-task:project",bad)
def test_write_capable_projection_is_allowed_only_as_source_capture_placeholder(
tmp_path,monkeypatch
):
policy,_,_=load_policy(tmp_path,monkeypatch)
request=governed_request()
request["online_task"]={
**request["online_task"],
"required_capabilities":[
"git.read",
"git.test",
"governed.write.branch",
"governed.write.file",
],
}
request["execution"]={
**request["execution"],
"capability_scope":[
"git.read",
"git.test",
"governed.write.branch",
"governed.write.file",
],
}
decision=policy.authorize("governed-task:project",request)
assert decision["decision"]=="ALLOW"
assert request["execution"]["action"]=="source_capture"
def test_release_candidate_request_is_tightly_constrained(
tmp_path,monkeypatch
):
policy,_,_=load_policy(tmp_path,monkeypatch)
decision=policy.authorize("release:candidate",release_request())
assert decision["decision"]=="ALLOW"
with pytest.raises(
auth.AuthorizationError,
match="RELEASE_VERSION_NOT_AUTHORIZED",
):
policy.authorize(
"release:candidate",
release_request(version="V3R1C1"),
)
with pytest.raises(
auth.AuthorizationError,
match="RELEASE_CANDIDATE_FIELDS_INVALID",
):
policy.authorize(
"release:candidate",
{**release_request(),"execute":True},
)
def test_governed_task_projection_executes_canonical_upstream(
tmp_path,monkeypatch
):
instance,upstream,store,_=broker(tmp_path,monkeypatch)
request=governed_request()
response=instance.execute(
"governed-task:project",
request,
idempotency_key="owner-auth-projection-operation-0001",
)
assert len(upstream.project_calls)==1
assert upstream.project_calls[0]==request
assert response["operation_state"]=="EXECUTED"
assert response["external_capability_projection_required"] is False
assert response["separate_owner_release_approval_still_required"] is True
def test_release_candidate_authorization_does_not_invent_builder_upstream(
tmp_path,monkeypatch
):
instance,upstream,store,_=broker(tmp_path,monkeypatch)
response=instance.execute(
"release:candidate",
release_request(),
idempotency_key="owner-auth-release-candidate-operation-0001",
)
assert upstream.project_calls==[]
assert response["operation_state"]==(
"AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION"
)
assert response["upstream_status"] is None
assert response["upstream_response"] is None
assert response["external_capability_projection_required"] is True
assert response["projection_id"].startswith("projection-")
assert response["projection_state"]=="PENDING_EXTERNAL_CONNECTOR"
assert response["separate_owner_release_approval_still_required"] is True
def test_release_authorization_creates_durable_projection_outbox(
tmp_path,monkeypatch
):
instance,_,store,policy=broker(tmp_path,monkeypatch)
request=release_request()
response=instance.execute(
"release:candidate",
request,
idempotency_key="owner-auth-release-outbox-0001",
)
projection=store.get_projection(response["projection_id"])
assert projection is not None
assert projection["schema"]==auth.PROJECTION_SCHEMA
assert projection["state"]=="PENDING_EXTERNAL_CONNECTOR"
assert projection["grant_id"]==policy.grant_id
assert projection["account"]==ACCOUNT
assert projection["capability"]=="release:candidate"
assert projection["request"]==request
assert projection["separate_owner_release_approval_still_required"] is True
listed=instance.projections()
assert listed["schema"]==auth.PROJECTION_LIST_SCHEMA
assert listed["count"]==1
assert listed["items"][0]["projection_id"]==response["projection_id"]
assert listed["external_connector_ack_supported"] is False
def test_existing_pre_outbox_release_receipt_is_backfilled_on_exact_retry(
tmp_path,monkeypatch
):
instance,_,store,policy=broker(tmp_path,monkeypatch)
request=release_request()
decision=policy.authorize("release:candidate",request)
key="owner-auth-release-pre-outbox-0001"
legacy_response={
"schema":auth.RECEIPT_SCHEMA,
"grant_id":policy.grant_id,
"owner_identity":policy.owner_identity,
"account":policy.account,
"capability":"release:candidate",
"policy_sha256":policy.policy_sha256,
"request_sha256":decision["request_sha256"],
"idempotency_key":key,
"operation_state":"AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION",
"upstream_status":None,
"upstream_response":None,
"external_capability_projection_required":True,
"separate_owner_release_approval_still_required":True,
}
store.put(
key,
"release:candidate",
decision["request_sha256"],
legacy_response,
)
response=instance.execute(
"release:candidate",
request,
idempotency_key=key,
)
assert response["projection_id"].startswith("projection-")
assert response["projection_state"]=="PENDING_EXTERNAL_CONNECTOR"
assert store.get_projection(response["projection_id"]) is not None
persisted=store.get(key)["response"]
assert persisted["projection_id"]==response["projection_id"]
def test_projection_id_is_deterministic_and_exact_retry_does_not_duplicate(
tmp_path,monkeypatch
):
instance,_,store,_=broker(tmp_path,monkeypatch)
key="owner-auth-release-outbox-0002"
first=instance.execute("release:candidate",release_request(),idempotency_key=key)
second=instance.execute("release:candidate",release_request(),idempotency_key=key)
assert second==first
con=sqlite3.connect(store.path)
rows=con.execute("select projection_id from projection_outbox").fetchall()
assert rows==[(first["projection_id"],)]
def test_projection_enqueue_recovers_crash_between_outbox_and_receipt(
tmp_path,monkeypatch
):
instance,_,store,policy=broker(tmp_path,monkeypatch)
request=release_request()
decision=policy.authorize("release:candidate",request)
key="owner-auth-release-outbox-crash-0001"
first=store.enqueue_projection(
idempotency_key=key,
grant_id=policy.grant_id,
account=policy.account,
capability="release:candidate",
policy_sha256=policy.policy_sha256,
request_sha256=decision["request_sha256"],
request=request,
)
# Simulate a later retry after the process crashed before receipt creation.
second=store.enqueue_projection(
idempotency_key=key,
grant_id=policy.grant_id,
account=policy.account,
capability="release:candidate",
policy_sha256=policy.policy_sha256,
request_sha256=decision["request_sha256"],
request=request,
)
assert second==first
def test_projection_idempotency_conflict_is_fail_closed(
tmp_path,monkeypatch
):
instance,_,store,policy=broker(tmp_path,monkeypatch)
first=release_request()
first_decision=policy.authorize("release:candidate",first)
key="owner-auth-release-outbox-conflict-0001"
store.enqueue_projection(
idempotency_key=key,
grant_id=policy.grant_id,
account=policy.account,
capability="release:candidate",
policy_sha256=policy.policy_sha256,
request_sha256=first_decision["request_sha256"],
request=first,
)
second=release_request(version="V2R2C3")
second_decision=policy.authorize("release:candidate",second)
with pytest.raises(auth.AuthorizationError,match="PROJECTION_IDEMPOTENCY_CONFLICT"):
store.enqueue_projection(
idempotency_key=key,
grant_id=policy.grant_id,
account=policy.account,
capability="release:candidate",
policy_sha256=policy.policy_sha256,
request_sha256=second_decision["request_sha256"],
request=second,
)
def test_revocation_blocks_projection_enumeration(tmp_path,monkeypatch):
instance,_,store,policy=broker(tmp_path,monkeypatch)
response=instance.execute(
"release:candidate",
release_request(),
idempotency_key="owner-auth-release-outbox-revoke-0001",
)
# Restore real policy loading and write a revocation for this grant.
monkeypatch.undo()
revocations=instance.revocation_path
revocations.write_text(
json.dumps({"revoked_grant_ids":[policy.grant_id]})+"\n",
encoding="utf-8",
)
revocations.chmod(0o600)
monkeypatch.setattr(auth,"validate_root_file",lambda path,**kwargs:path)
with pytest.raises(auth.AuthorizationError,match="OWNER_GRANT_REVOKED"):
instance.projections()
# Evidence is retained; revocation does not erase history.
assert store.get_projection(response["projection_id"]) is not None
def test_projection_outbox_contains_no_credentials(tmp_path,monkeypatch):
instance,_,store,_=broker(tmp_path,monkeypatch)
instance.execute(
"release:candidate",
release_request(),
idempotency_key="owner-auth-release-outbox-schema-0001",
)
con=sqlite3.connect(store.path)
columns=[row[1] for row in con.execute("pragma table_info(projection_outbox)")]
joined=" ".join(columns).lower()
for forbidden in ("token","credential","private_key","secret"):
assert forbidden not in joined
payload=con.execute("select payload_json from projection_outbox").fetchone()[0]
assert "Bearer " not in payload
assert "submitOwnerApproval" not in payload
assert "startDeployment" not in payload
def test_exact_retry_returns_same_receipt_without_second_projection(
tmp_path,monkeypatch
):
instance,upstream,_,_=broker(tmp_path,monkeypatch)
request=governed_request()
first=instance.execute(
"governed-task:project",
request,
idempotency_key="owner-auth-projection-operation-0002",
)
second=instance.execute(
"governed-task:project",
request,
idempotency_key="owner-auth-projection-operation-0002",
)
assert second==first
assert len(upstream.project_calls)==1
def test_idempotency_conflict_fails_closed(tmp_path,monkeypatch):
instance,_,_,_=broker(tmp_path,monkeypatch)
instance.execute(
"release:candidate",
release_request(),
idempotency_key="owner-auth-release-operation-0002",
)
with pytest.raises(auth.AuthorizationError,match="IDEMPOTENCY_CONFLICT"):
instance.execute(
"release:candidate",
release_request(version="V2R2C3"),
idempotency_key="owner-auth-release-operation-0002",
)
def test_receipt_store_contains_no_owner_policy_or_credentials(
tmp_path,monkeypatch
):
instance,_,store,_=broker(tmp_path,monkeypatch)
instance.execute(
"release:candidate",
release_request(),
idempotency_key="owner-auth-release-operation-0003",
)
con=sqlite3.connect(store.path)
columns=[row[1] for row in con.execute("pragma table_info(receipts)")]
joined=" ".join(columns).lower()
assert "token" not in joined
assert "credential" not in joined
assert "policy_json" not in columns
def test_systemd_broker_uses_unix_socket_and_only_host_control_credential():
unit=(
ROOT/"deploy/systemd/aios-owner-durable-authorization-broker.service"
).read_text()
assert "LoadCredential=agent_task_control_token:" in unit
assert "release_candidate_token" not in unit
assert "18947" not in unit
assert "AIOS_RELEASE_CANDIDATE_API_URL" not in unit
assert "AIOS_OWNER_AUTH_BROKER_SOCKET=%t/aios-owner-authorization-broker/broker.sock" in unit
assert "Group=aios-executor-exchange" in unit
assert "RestrictAddressFamilies=AF_UNIX AF_INET" in unit
assert "IPAddressDeny=any" in unit
assert "IPAddressAllow=127.0.0.0/8" in unit
assert "InaccessiblePaths=/data/aios-gitops /home" in unit
def test_source_contains_no_release_builder_endpoint_or_high_risk_execution():
source=(
ROOT/"active-runtime/app/aios_durable_owner_authorization.py"
).read_text()
assert "/createReleaseCandidate" not in source
assert "18947" not in source
assert "ThreadingHTTPServer" not in source
assert "ThreadingUnixStreamServer" in source
assert "startDeployment" in source
assert "tag_publish" in source
assert "baseline_registry_finalize" in source
assert "runtime_switch" in source
assert "AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION" in source
assert "PENDING_EXTERNAL_CONNECTOR" in source
assert "projection_outbox" in source
assert "external_connector_ack_supported" in source
assert "SAFE_PROJECTED_TASK_CAPABILITIES" in source
assert "INITIAL_EXECUTION_ACTION_NOT_AUTHORIZED" in source
def test_docs_avoid_new_broker_bearer_secret():
doc=(
ROOT/"docs/superpowers/specs/2026-08-19-durable-owner-authorization.md"
).read_text()
assert "AF_UNIX" in doc
assert "avoids introducing another long-lived bearer secret" in doc
def test_docs_preserve_separate_owner_release_approval():
doc=(
ROOT/"docs/superpowers/specs/2026-08-19-durable-owner-authorization.md"
).read_text()
assert "persistent until explicitly revoked" in doc.lower()
assert "Owner signature" in doc
assert "AUTHORIZED_PENDING_EXTERNAL_CAPABILITY_PROJECTION" in doc
assert "PENDING_EXTERNAL_CONNECTOR" in doc
@@ -0,0 +1,83 @@
from __future__ import annotations
import sys
from pathlib import Path
import pytest
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/"active-runtime/app"))
from aios_capability_contract import ( # noqa: E402
has_capabilities,
normalize_required_capabilities,
normalize_worker_capabilities,
normalized_capability_matrix,
required_capabilities_subset,
)
def test_release_candidate_is_known_to_task_and_worker_vocabularies():
capability="release:candidate"
assert normalize_required_capabilities([capability])==frozenset({capability})
assert normalize_worker_capabilities([capability])==frozenset({capability})
assert has_capabilities([capability],[capability]) is True
assert normalized_capability_matrix([capability],[capability])=={
"worker_effective":["release:candidate"],
"task_required":["release:candidate"],
"eligible":True,
}
def test_release_candidate_capability_does_not_imply_high_risk_release_operations():
worker=["release:candidate"]
for forbidden in (
"submitOwnerApproval",
"startDeployment",
"tag_publish",
"baseline_registry_finalize",
"runtime_switch",
"deploy.production",
):
assert has_capabilities(worker,[forbidden]) is False
def test_release_wildcards_and_nearby_unknown_names_remain_fail_closed():
assert has_capabilities(["release:candidate"],["release:*"]) is False
assert has_capabilities(["release:candidate"],["release:candidate.extra"]) is False
assert has_capabilities(["release:*"],["release:candidate"]) is False
def test_release_candidate_delegation_subset_is_exact():
assert required_capabilities_subset(
["release:candidate"],
["release:candidate"],
) is True
assert required_capabilities_subset(
["release:candidate"],
["startDeployment"],
) is False
@pytest.mark.parametrize(
"worker,required",
[
(["agent.runtime.hermes"],["agent.runtime.hermes"]),
(["agent.runtime.openclaw"],["agent.runtime.openclaw"]),
(["git.read"],["repository_read"]),
(["git.test"],["test_execution"]),
(
["governed.write.branch","governed.write.file"],
["governed.git.write"],
),
],
)
def test_existing_known_capability_semantics_are_non_regressed(worker,required):
assert has_capabilities(worker,required) is True
def test_release_candidate_worker_does_not_match_empty_or_unrelated_task_by_escalation():
assert has_capabilities(["release:candidate"],[]) is True
assert has_capabilities(["release:candidate"],["git.read"]) is False
assert has_capabilities(["release:candidate"],["agent.runtime.hermes"]) is False
+216
View File
@@ -0,0 +1,216 @@
from __future__ import annotations
import json
from pathlib import Path
import sys
import pytest
ROOT=Path(__file__).resolve().parents[1]
APP=ROOT/"active-runtime/app"
sys.path.insert(0,str(APP))
import aios_release_online_agent_worker as worker
def task(**overrides):
value={
"task_id":"r42-release-candidate-live-acceptance-20260819",
"required_capabilities":["release:candidate"],
"constraints":{
"release_candidate":{
"candidate_id":"r42-release-candidate-v2r2c2-20260819",
"source_commit":"5"*40,
"version":"V2R2C2",
"projection_id":"projection-"+"a"*32,
}
}
}
value.update(overrides)
return value
def test_release_request_accepts_only_exact_release_capability():
req=worker.ReleaseRequest.from_task(task())
assert req.candidate_id=="r42-release-candidate-v2r2c2-20260819"
assert req.source_commit=="5"*40
assert req.version=="V2R2C2"
assert req.projection_id=="projection-"+"a"*32
with pytest.raises(worker.ReleaseWorkerError,match="TASK_CAPABILITY_SCOPE_INVALID"):
worker.ReleaseRequest.from_task(task(required_capabilities=["release:candidate","git.read"]))
@pytest.mark.parametrize(
"payload",
[
{
"candidate_id":"r42-release-candidate-v2r2c2-20260819",
"source_commit":"short",
"version":"V2R2C2",
"projection_id":"projection-"+"a"*32,
},
{
"candidate_id":"bad",
"source_commit":"5"*40,
"version":"V2R2C2",
"projection_id":"projection-"+"a"*32,
},
{
"candidate_id":"r42-release-candidate-v2r2c2-20260819",
"source_commit":"5"*40,
"version":"bad",
"projection_id":"projection-"+"a"*32,
},
{
"candidate_id":"r42-release-candidate-v2r2c2-20260819",
"source_commit":"5"*40,
"version":"V2R2C2",
"projection_id":"bad",
},
{
"candidate_id":"r42-release-candidate-v2r2c2-20260819",
"source_commit":"5"*40,
"version":"V2R2C2",
"projection_id":"projection-"+"a"*32,
"execute":True,
},
],
)
def test_release_request_rejects_invalid_or_extra_fields(payload):
value=task()
value["constraints"]={"release_candidate":payload}
with pytest.raises(worker.ReleaseWorkerError):
worker.ReleaseRequest.from_task(value)
def test_result_payload_preserves_high_risk_denials():
release=worker.ReleaseRequest.from_task(task())
result=worker.result_payload(
"r42-release-candidate-live-acceptance-20260819",
release,
{"candidate_id":release.candidate_id,"status":"BUILT"},
)
assert result["status"]=="SUCCEEDED"
assert result["classification"]=="RELEASE_CANDIDATE_CREATED"
assert result["writer_lease"]=="NONE"
assert result["changed_files"]==[]
assert result["direct_main_write"] is False
assert result["production_write"] is False
assert result["tag_write"] is False
assert result["baseline_write"] is False
assert result["runtime_switch"] is False
assert result["owner_approval_submitted"] is False
assert result["credential_material_exposed"] is False
def test_result_payload_rejects_builder_candidate_mismatch():
release=worker.ReleaseRequest.from_task(task())
with pytest.raises(worker.ReleaseWorkerError,match="BUILDER_CANDIDATE_BINDING_INVALID"):
worker.result_payload(
"r42-release-candidate-live-acceptance-20260819",
release,
{"candidate_id":"other-release-candidate"},
)
def test_worker_calls_only_fixed_release_candidate_route(monkeypatch):
release=worker.ReleaseRequest.from_task(task())
calls=[]
def fake_http(base,method,route,token,body=None,timeout=30):
calls.append((base,method,route,token,body))
if route.endswith("/claim"):
return 200,{"lease_token":"l"*32,"attempt":1}
if route.endswith("/heartbeat"):
return 200,{}
if route=="/createReleaseCandidate":
return 200,{
"candidate_id":release.candidate_id,
"artifact_sha256":"f"*64,
}
if route.endswith("/result"):
return 200,{"status":"VERIFYING"}
raise AssertionError(route)
monkeypatch.setattr(worker,"http_json",fake_http)
monkeypatch.setattr(worker,"audit",lambda *args,**kwargs:None)
monkeypatch.setattr(worker.Heartbeat,"first",lambda self:None)
monkeypatch.setattr(worker.Heartbeat,"close",lambda self:None)
monkeypatch.setattr(worker.Heartbeat,"ensure",lambda self:None)
monkeypatch.setattr(worker.threading.Thread,"start",lambda self:None)
assert worker.execute_task(
task(),
agent_token="a"*32,
release_token="r"*32,
) is True
release_calls=[item for item in calls if item[0]==worker.RELEASE_API]
assert len(release_calls)==1
assert release_calls[0][1]=="POST"
assert release_calls[0][2]=="/createReleaseCandidate"
assert release_calls[0][4]=={
"candidate_id":release.candidate_id,
"source_commit":release.source_commit,
"version":release.version,
}
def test_write_liveness_records_release_capability(tmp_path,monkeypatch):
path=tmp_path/"worker-liveness.json"
monkeypatch.setattr(worker,"STATE_ROOT",tmp_path)
monkeypatch.setattr(worker,"LIVENESS_PATH",path)
worker.write_liveness(
"IDLE",
stage="poll",
visible_count=2,
)
value=json.loads(path.read_text())
assert value["schema"]=="aios.online-agent.worker-liveness.v1"
assert value["agent_type"]=="release-preparation-worker"
assert value["capabilities"]==["release:candidate"]
assert value["state"]=="IDLE"
assert value["visible_count"]==2
def test_sd_notify_is_noop_without_systemd_socket(monkeypatch):
monkeypatch.delenv("NOTIFY_SOCKET",raising=False)
worker.sd_notify("WATCHDOG=1")
def test_systemd_unit_uses_two_encrypted_credentials_and_loopback_release_api():
unit=(ROOT/"deploy/systemd/aios-release-online-agent-worker.service").read_text()
assert "LoadCredentialEncrypted=agent_token:" in unit
assert "LoadCredentialEncrypted=release_candidate_token:" in unit
assert "AIOS_RELEASE_API_URL=http://127.0.0.1:18890" in unit
assert "AIOS_RELEASE_WORKER_AGENT_ID=online-release-worker-cn-network-mkt-20260819" in unit
assert "Type=notify" in unit
assert "WatchdogSec=90" in unit
assert "Restart=always" in unit
assert "IPAddressDeny=any" in unit
assert "IPAddressAllow=127.0.0.0/8" in unit
assert "InaccessiblePaths=/data/aios-gitops /home" in unit
assert "submitOwnerApproval" not in unit
assert "startDeployment" not in unit
def test_source_contains_no_high_risk_release_routes():
source=(ROOT/"active-runtime/app/aios_release_online_agent_worker.py").read_text()
assert '"/createReleaseCandidate"' in source
for forbidden in (
"/submitOwnerApproval",
"/startDeployment",
"/requestOrCreateTag",
"/buildProductionArtifact",
"/executeApprovedProductionRelease",
):
assert forbidden not in source
def test_docs_keep_release_candidate_distinct_from_production():
doc=(ROOT/"docs/superpowers/specs/2026-08-19-release-preparation-online-agent.md").read_text()
assert "release:candidate" in doc
assert "Owner signature" in doc
assert "never\nstarts deployment" in doc or "never starts deployment" in doc
assert "LoadCredentialEncrypted" in doc