본문으로 건너뛰기
이 페이지는 아직 사용자의 언어로 번역되지 않았거나 번역이 기술 검토를 기다리고 있습니다. 아래에는 영어 원문이 표시됩니다. 영어 페이지 열기

Automated Documentation Pipeline with Control Zero

This guide shows how to build an automated documentation pipeline using CrewAI multi-agent orchestration with Control Zero policy enforcement. Multiple agents collaborate to analyze code, generate documentation, and review the output, all under governance policies.

What You Will Build​

A multi-agent documentation system with:

  • A Code Analyzer agent that reads source code
  • A Writer agent that generates documentation
  • A Reviewer agent that checks quality
  • Policy enforcement at every step, controlling which agents can access which resources

Architecture​

Source Code Repository
|
v
[Code Analyzer Agent]
- Reads source files (enforced: data.read)
- Extracts functions, classes, patterns
|
v
Analysis Report
|
v
[Writer Agent]
- Generates documentation (enforced: llm.generate)
- Creates markdown files (enforced: data.write)
|
v
Draft Documentation
|
v
[Reviewer Agent]
- Reviews for accuracy (enforced: llm.generate)
- Suggests corrections
|
v
Final Documentation

Implementation​

Setup​

pip install controlzero crewai

Policy-Enforced Agents​

from controlzero import Client
from crewai import Agent, Task, Crew, Process

# Initialize Control Zero
cz = Client(api_key="cz_live_your_api_key_here")


def enforce_step(step_output):
"""Enforce policies on every agent step."""
if hasattr(step_output, "tool") and step_output.tool:
cz.guard(step_output.tool, args={"agent_id": str(step_output.agent)})


# [Agents]
code_analyzer = Agent(
role="Code Analyzer",
goal="Read and analyze source code to extract documentation-relevant information",
backstory=(
"Expert software engineer who understands code structure, "
"design patterns, and can identify key components for documentation."
),
step_callback=enforce_step,
verbose=True,
)

doc_writer = Agent(
role="Documentation Writer",
goal="Write clear, comprehensive technical documentation from analysis reports",
backstory=(
"Professional technical writer with deep experience in API documentation, "
"tutorials, and developer guides."
),
step_callback=enforce_step,
verbose=True,
)

doc_reviewer = Agent(
role="Documentation Reviewer",
goal="Review documentation for accuracy, completeness, and clarity",
backstory=(
"Senior developer advocate who ensures documentation is accurate, "
"follows best practices, and helps developers succeed."
),
step_callback=enforce_step,
verbose=True,
)

Define Tasks​

analysis_task = Task(
description=(
"Analyze the source code in the provided files. "
"Extract all public functions, classes, and their signatures. "
"Identify the main architectural patterns and data flows. "
"Output a structured analysis report."
),
expected_output="A structured report of all public APIs, classes, and patterns",
agent=code_analyzer,
)

writing_task = Task(
description=(
"Using the analysis report, write comprehensive API documentation. "
"Include: overview, installation, quick start, API reference with "
"all public functions and classes, and usage examples for each."
),
expected_output="Complete API documentation in Markdown format",
agent=doc_writer,
)

review_task = Task(
description=(
"Review the generated documentation for: "
"1. Technical accuracy (do examples match the API?) "
"2. Completeness (are all public APIs documented?) "
"3. Clarity (can a new developer follow this?) "
"4. Best practices (proper formatting, consistent style?) "
"Output the final reviewed documentation with any corrections applied."
),
expected_output="Final, reviewed documentation ready for publishing",
agent=doc_reviewer,
)

Run the Pipeline​

crew = Crew(
agents=[code_analyzer, doc_writer, doc_reviewer],
tasks=[analysis_task, writing_task, review_task],
process=Process.sequential,
verbose=True,
)

try:
result = crew.kickoff()
print("Documentation generated successfully:")
print(result)
except controlzero.PolicyDeniedError as e:
print(f"Pipeline blocked by policy: {e.decision.reason}")

Example Policy​

Restrict what each agent can do:

{
"name": "doc-pipeline-policy",
"rules": [
{
"effect": "allow",
"action": "llm:generate",
"resource": "model/*"
},
{
"effect": "allow",
"action": "tool:call",
"resource": "tool/read_file"
},
{
"effect": "allow",
"action": "tool:call",
"resource": "tool/list_directory"
},
{
"effect": "deny",
"action": "tool:call",
"resource": "tool/write_file"
},
{
"effect": "deny",
"action": "tool:call",
"resource": "tool/execute_code"
},
{
"effect": "deny",
"action": "tool:call",
"resource": "tool/send_email"
}
]
}

This policy allows the agents to read code and generate documentation, but blocks file writes (documentation output goes to stdout/return values), code execution, and email sending. This ensures the documentation pipeline is read-only and cannot modify the codebase.

Next Steps​