mirror of
https://github.com/hanzoai/opencyber.git
synced 2026-11-21 23:29:09 +00:00
Reviewed source snapshot with MCP 2.x, bounded execution and durable approvals, private beta onboarding, and immutable candidate qualification gates. Signed-off-by: mitkox <[email protected]>
169 lines
6.8 KiB
Python
169 lines
6.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the error reference and its JSON Schema from ``opencyber.errors``.
|
|
|
|
Two files are produced and tracked:
|
|
|
|
``docs/ERRORS.md``
|
|
Human-readable reference for the refusal contract.
|
|
``docs/error-contract.schema.json``
|
|
Machine-readable JSON Schema for the ``error`` object carried in results.
|
|
|
|
The registry in ``src/opencyber/errors.py`` owns the codes, categories, and
|
|
default remediation. This script only renders them, so the reference cannot
|
|
drift from the server. ``--check`` fails when a tracked file is stale.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "src"))
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
from lib.generated import StaleGeneratedFile, ensure_generated_file # noqa: E402
|
|
from opencyber.errors import ErrorCategory, describe_codes, error_schema # noqa: E402
|
|
|
|
|
|
DOCS = ROOT / "docs"
|
|
MARKDOWN = DOCS / "ERRORS.md"
|
|
SCHEMA = DOCS / "error-contract.schema.json"
|
|
GENERATE_COMMAND = "python scripts/generate-error-docs.py"
|
|
|
|
|
|
def _escape(value: str) -> str:
|
|
return value.replace("|", "\\|")
|
|
|
|
|
|
def render_markdown() -> str:
|
|
rows = describe_codes()
|
|
lines = [
|
|
"<!-- Generated by scripts/generate-error-docs.py. Do not edit by hand. -->",
|
|
"",
|
|
"# Error contract",
|
|
"",
|
|
"A tool result that did not do what the caller asked carries an `error`",
|
|
"object alongside `ok: false`. The object is stable: clients branch on",
|
|
"`code` and `category`, read `retryable` before repeating a call, and show",
|
|
"`remediation.action` to the operator instead of parsing prose.",
|
|
"",
|
|
"Refusals are returned as structured results rather than raised into the",
|
|
"transport, so an agent can read the recovery step. Unexpected exceptions",
|
|
"are translated to `internal_error` with the same shape.",
|
|
"",
|
|
"Regenerate after changing `src/opencyber/errors.py`:",
|
|
"",
|
|
"```sh",
|
|
GENERATE_COMMAND,
|
|
"```",
|
|
"",
|
|
"The machine-readable form is",
|
|
"[error-contract.schema.json](error-contract.schema.json).",
|
|
"",
|
|
"## Shape",
|
|
"",
|
|
"```json",
|
|
json.dumps(
|
|
{
|
|
"code": "scope_denied",
|
|
"category": "denied",
|
|
"message": "scope enforcement refused nmap: 10.0.0.9 is not in the engagement targets",
|
|
"retryable": False,
|
|
"remediation": {"action": "Add 10.0.0.9 to the engagement targets, or call oc_scope_check first."},
|
|
"limit": None,
|
|
"details": {"executable": "nmap", "denied": ["10.0.0.9"]},
|
|
},
|
|
indent=2,
|
|
),
|
|
"```",
|
|
"",
|
|
"`code`, `category`, `message`, `retryable`, and `remediation` are always",
|
|
"present. `limit` is `null` unless a named ceiling caused the failure; it",
|
|
"then names the unit, effective value, source, and what raises it. `details`",
|
|
"carries structured context and may be empty.",
|
|
"",
|
|
"## Categories",
|
|
"",
|
|
"| Category | Meaning |",
|
|
"| --- | --- |",
|
|
f"| `{ErrorCategory.INVALID_INPUT.value}` | The caller's input was wrong or incomplete. |",
|
|
f"| `{ErrorCategory.DENIED.value}` | Scope, authorization, approval, or file permissions refused it. |",
|
|
f"| `{ErrorCategory.UNAVAILABLE.value}` | The deployment cannot perform it: missing binary, device, or network. |",
|
|
f"| `{ErrorCategory.NOT_FOUND.value}` | The named engagement, file, or record does not exist. |",
|
|
f"| `{ErrorCategory.CONFLICT.value}` | It contradicts current state, such as a hash or revision mismatch. |",
|
|
f"| `{ErrorCategory.INTEGRITY_MISMATCH.value}` | Stored hashes or the audit chain do not verify. |",
|
|
f"| `{ErrorCategory.TRUNCATED.value}` | The complete result could not be returned and the partial is unusable. |",
|
|
f"| `{ErrorCategory.TIMEOUT.value}` | A deadline stopped the operation. |",
|
|
f"| `{ErrorCategory.CANCELLED.value}` | The caller cancelled it. |",
|
|
f"| `{ErrorCategory.RESOURCE_CONSTRAINT.value}` | A named size, count, or request ceiling ended it early. |",
|
|
f"| `{ErrorCategory.EXECUTION_FAILURE.value}` | It ran but did not complete successfully. |",
|
|
f"| `{ErrorCategory.INTERNAL.value}` | The harness hit an unexpected condition. |",
|
|
"",
|
|
"## Codes",
|
|
"",
|
|
"| Code | Category | Retryable | Meaning | Default remediation |",
|
|
"| --- | --- | --- | --- | --- |",
|
|
]
|
|
for row in rows:
|
|
lines.append(
|
|
f"| `{row['code']}` | `{row['category']}` | {row['retryable']} | "
|
|
f"{_escape(row['summary'])} | {_escape(row['remediation'])} |"
|
|
)
|
|
lines.extend(
|
|
[
|
|
"",
|
|
"## Limits",
|
|
"",
|
|
"A failure caused by a named ceiling carries `limit` facts instead of a",
|
|
"sentence. See [LIMITS.md](LIMITS.md) for the registry those facts come",
|
|
"from: unit, default, ceiling, source, and recovery for each one.",
|
|
"",
|
|
"## Adding a code",
|
|
"",
|
|
"1. Add an `ErrorSpec` to `_SPECS` in `src/opencyber/errors.py`.",
|
|
"2. Regenerate this page and the schema.",
|
|
"3. Add the code to the per-domain refusal tests that can raise it.",
|
|
"",
|
|
"Codes are a closed set. `OcError` rejects an unregistered code at",
|
|
"construction time, so a typo fails in tests rather than in a client.",
|
|
"",
|
|
]
|
|
)
|
|
return "\n".join(lines)
|
|
|
|
|
|
def render_schema() -> str:
|
|
schema = error_schema()
|
|
schema = {"$comment": "Generated by scripts/generate-error-docs.py. Do not edit by hand.", **schema}
|
|
return json.dumps(schema, indent=2, sort_keys=False) + "\n"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true", help="fail if a tracked file is stale instead of writing it")
|
|
args = parser.parse_args()
|
|
try:
|
|
wrote_markdown = ensure_generated_file(
|
|
MARKDOWN, render_markdown(), check=args.check, generate_command=GENERATE_COMMAND
|
|
)
|
|
wrote_schema = ensure_generated_file(
|
|
SCHEMA, render_schema(), check=args.check, generate_command=GENERATE_COMMAND
|
|
)
|
|
except StaleGeneratedFile as error:
|
|
print(str(error), file=sys.stderr)
|
|
return 1
|
|
if args.check:
|
|
print(f"{MARKDOWN.relative_to(ROOT)} and {SCHEMA.relative_to(ROOT)} are current")
|
|
else:
|
|
for path, wrote in ((MARKDOWN, wrote_markdown), (SCHEMA, wrote_schema)):
|
|
print(f"{'wrote' if wrote else 'unchanged'} {path.relative_to(ROOT)}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|