Skip to content

Domain Types & Contracts

Core data structures and exceptions defining the Sovereign Dark Factory domain model.

dark_factory.domain.types

Domain data contracts and types for Sovereign Dark Factory.

Classes

RunStatus

Bases: StrEnum

Lifecycle status of a dark factory run.

Source code in dark_factory/domain/types.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class RunStatus(StrEnum):
    """Lifecycle status of a dark factory run."""

    PENDING = "PENDING"
    CREATING_SANDBOX = "CREATING_SANDBOX"
    AGENT_RUNNING = "AGENT_RUNNING"
    VERIFYING = "VERIFYING"
    SELF_HEALING = "SELF_HEALING"
    AWAITING_REVIEW = "AWAITING_REVIEW"
    APPROVED = "APPROVED"
    REJECTED = "REJECTED"
    FAILED = "FAILED"
    TIMED_OUT = "TIMED_OUT"
    CANCELLED = "CANCELLED"

    @property
    def is_terminal(self) -> bool:
        return self in (
            RunStatus.APPROVED,
            RunStatus.REJECTED,
            RunStatus.FAILED,
            RunStatus.TIMED_OUT,
            RunStatus.CANCELLED,
        )

TaskSpec dataclass

Specification for an autonomous coding task.

Source code in dark_factory/domain/types.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@dataclass
class TaskSpec:
    """Specification for an autonomous coding task."""

    repo_path: str
    task_prompt: str
    base_rev: str = "HEAD"
    agent: str = "local-coder"
    model: str = "qwen2.5-coder:14b"
    verification_steps: list[VerificationStep] = field(default_factory=list)
    allow_no_verify: bool = False
    protected_paths: list[str] = field(default_factory=list)
    allow_gate_edits: bool = False
    max_healing_attempts: int = 3
    timeout_minutes: float = 30
    metadata: dict[str, Any] = field(default_factory=dict)
    skip_adversarial: bool = False
    mutate_adversarial: bool = False
    skip_analysis: bool = False
    planner_model: str | None = None
    skip_plan: bool = False
    target_files: list[str] = field(default_factory=list)  # files the agent edits; inlined in full as context
    allow_out_of_scope: bool = False  # bypass the patch-scope gate (placeholder paths, files outside target_files)

StepExecution dataclass

Outcome of a single verification step or command execution.

Source code in dark_factory/domain/types.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@dataclass
class StepExecution:
    """Outcome of a single verification step or command execution."""

    step_id: str
    exit_code: int
    stdout: str
    stderr: str
    duration_sec: float
    timed_out: bool = False

    @property
    def passed(self) -> bool:
        return self.exit_code == 0 and not self.timed_out

VerificationStep dataclass

A single deterministic verification gate.

Source code in dark_factory/domain/types.py
37
38
39
40
41
42
43
44
45
@dataclass(frozen=True)
class VerificationStep:
    """A single deterministic verification gate."""

    id: str
    argv: list[str]
    timeout_sec: int = 300
    mandatory: bool = True
    description: str = ""

EvidenceManifest dataclass

Immutable audit record and evidence of a completed or paused run.

Source code in dark_factory/domain/types.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@dataclass
class EvidenceManifest:
    """Immutable audit record and evidence of a completed or paused run."""

    run_id: str
    status: RunStatus
    repo_path: str
    base_rev: str
    created_at: str
    completed_at: str | None = None
    resulting_rev: str | None = None
    patch_path: str | None = None
    patch_size_bytes: int = 0
    patch_sha256: str | None = None
    healing_attempts: int = 0
    verification_results: list[StepExecution] = field(default_factory=list)
    model_telemetry: ModelTelemetry | None = None
    operator_notes: str | None = None
    phase_timings: list[PhaseTiming] = field(default_factory=list)
    repeated_failure_streak: int = 0
    adversarial_report: AdversarialReport | None = None
    execution_plan: ExecutionPlan | None = None
    adversarial_test_code: str | None = None
    analysis_report: AnalysisReport | None = None

    @classmethod
    def create(
        cls,
        run_id: str,
        repo_path: str,
        base_rev: str,
        status: RunStatus = RunStatus.PENDING,
    ) -> EvidenceManifest:
        return cls(
            run_id=run_id,
            status=status,
            repo_path=repo_path,
            base_rev=base_rev,
            created_at=datetime.now(UTC).isoformat(),
        )

ModelTelemetry dataclass

Telemetry captured from local inference engines (Ollama / Prism).

Source code in dark_factory/domain/types.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@dataclass
class ModelTelemetry:
    """Telemetry captured from local inference engines (Ollama / Prism)."""

    engine: str
    model_name: str
    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0
    duration_sec: float = 0.0
    tokens_per_sec: float = 0.0
    cost_usd: float = 0.0  # Always 0.0 for local models
    num_ctx: int | None = None  # context window requested from the engine (Ollama only)
    finish_reason: str | None = None  # why the last call stopped: `stop`, `length`, ... (None: engine did not say)
    max_tokens: int | None = None  # completion budget requested from the engine (Prism only)
    reasoning_chars: int = 0  # length of the engine's separate reasoning text; diagnostic only, never executed

dark_factory.domain.errors

Domain error definitions for Sovereign Dark Factory.

Classes

DarkFactoryError

Bases: Exception

Base exception for all Dark Factory errors.

Source code in dark_factory/domain/errors.py
4
5
class DarkFactoryError(Exception):
    """Base exception for all Dark Factory errors."""

HarnessError

Bases: DarkFactoryError

Raised when an agent harness fails to invoke or communicate with local models.

Source code in dark_factory/domain/errors.py
20
21
class HarnessError(DarkFactoryError):
    """Raised when an agent harness fails to invoke or communicate with local models."""

LocalEngineOfflineError

Bases: HarnessError

Raised when Ollama or Prism are unreachable.

Source code in dark_factory/domain/errors.py
28
29
class LocalEngineOfflineError(HarnessError):
    """Raised when Ollama or Prism are unreachable."""

PromptTooLargeError

Bases: HarnessError

Raised before any model call when the prompt plus output reserve exceeds max_num_ctx.

Source code in dark_factory/domain/errors.py
24
25
class PromptTooLargeError(HarnessError):
    """Raised before any model call when the prompt plus output reserve exceeds `max_num_ctx`."""

RunNotFoundError

Bases: DarkFactoryError

Raised when attempting to inspect or review a non-existent run ID.

Source code in dark_factory/domain/errors.py
50
51
class RunNotFoundError(DarkFactoryError):
    """Raised when attempting to inspect or review a non-existent run ID."""

RunTimeoutError

Bases: DarkFactoryError

Raised when a run exceeds its deadline; carries the partial results gathered so far.

Source code in dark_factory/domain/errors.py
36
37
38
39
40
41
42
43
class RunTimeoutError(DarkFactoryError):
    """Raised when a run exceeds its deadline; carries the partial results gathered so far."""

    def __init__(self, message: str, executions=None, healing_attempts: int = 0, telemetry=None) -> None:
        super().__init__(message)
        self.executions = list(executions or [])
        self.healing_attempts = healing_attempts
        self.telemetry = telemetry

SandboxError

Bases: DarkFactoryError

Raised when sandbox creation, command execution, or teardown fails.

Source code in dark_factory/domain/errors.py
8
9
class SandboxError(DarkFactoryError):
    """Raised when sandbox creation, command execution, or teardown fails."""

VerificationFailedError

Bases: DarkFactoryError

Raised when deterministic verification gates fail and maximum healing retries are exceeded.

Source code in dark_factory/domain/errors.py
32
33
class VerificationFailedError(DarkFactoryError):
    """Raised when deterministic verification gates fail and maximum healing retries are exceeded."""

WorkflowStateError

Bases: DarkFactoryError

Raised on invalid workflow state transitions or journal corruption.

Source code in dark_factory/domain/errors.py
46
47
class WorkflowStateError(DarkFactoryError):
    """Raised on invalid workflow state transitions or journal corruption."""

WorktreeCleanupError

Bases: SandboxError

Raised when git worktree destruction fails.

Source code in dark_factory/domain/errors.py
16
17
class WorktreeCleanupError(SandboxError):
    """Raised when git worktree destruction fails."""

WorktreeCreationError

Bases: SandboxError

Raised when git worktree creation fails.

Source code in dark_factory/domain/errors.py
12
13
class WorktreeCreationError(SandboxError):
    """Raised when git worktree creation fails."""