feat: working orhcestration engine

This commit is contained in:
2026-10-21 19:50:24 +01:00
commit ce5e2f20cd
161 changed files with 31794 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Checkpointed Execution state and the Ledger that guards it."""

View File

@@ -0,0 +1,116 @@
"""The checkpointed state of one Execution. Plain, JSON-compatible data only."""
from __future__ import annotations
import operator
import re
from pathlib import Path
from typing import Annotated, Any, TypedDict
# A Customer case id becomes a directory name, so it is a short lowercase slug.
CUSTOMER_CASE = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$")
# Executions started without a Customer case are grouped here, so every Execution's
# files sit under a case.
UNASSIGNED_CASE = "unassigned"
def check_customer_case(customer_case: object) -> str:
"""The Customer case id, defaulting to UNASSIGNED_CASE; a ValueError when it is
not a safe slug."""
if customer_case is None:
return UNASSIGNED_CASE
if not isinstance(customer_case, str) or not CUSTOMER_CASE.match(customer_case):
raise ValueError(
"customer_case must be 1 to 64 lowercase letters, digits or hyphens, "
f"starting and ending with a letter or digit; got {customer_case!r}"
)
return customer_case
def case_dir(output_root: Path, customer_case: str) -> Path:
"""Everything generated for one Customer case."""
return output_root / "deliverables" / "cases" / customer_case
def execution_dir(output_root: Path, execution_id: str, customer_case: str) -> Path:
"""Where one Execution's files go, under its Customer case."""
return case_dir(output_root, customer_case) / "executions" / execution_id
def lessons_file(output_root: Path, execution_id: str, customer_case: str) -> Path:
return (
output_root / "lessons" / "candidates" / customer_case / f"{execution_id}.json"
)
def merge_by_key(
current: dict[str, dict[str, Any]] | None, update: dict[str, dict[str, Any]] | None
) -> dict[str, dict[str, Any]]:
"""Parallel Specialist tasks each write their own Deliverable's entry."""
return {**(current or {}), **(update or {})}
class ExecutionState(TypedDict, total=False):
execution_id: str
session_id: str
customer_case: str # the Customer case this Execution belongs to
intent_version: int # which statement of the case's intent started this Execution
workflow_request: str
risks: list[dict[str, Any]] # flags on what the user wrote, for a person to see
overlay_skills: dict[str, str] # overlay Skills this Execution loads -> version
options: dict[str, Any] # the choices made for this Execution, by name
# The customer's scope spec as read at the start: its Skill, status, readiness
# and what a Specialist is shown of it. None when no spec was supplied.
scope: dict[str, Any] | None
# What the spec was digested from: name -> its hash, and whether code read it.
sources: dict[str, dict[str, Any]]
# The sources code did not read, for the Specialist that completes the spec.
source_documents: dict[str, str]
baseline: dict[str, Any] | None # a Change request's Baseline, read from its files
pre_approved: bool
allow_cloud_read: bool | None
ledger: dict[str, Any] # Ledger.to_dict()
deliverables: dict[str, dict[str, Any]] # kind -> {content, attachments}
conversation: list[dict[str, str]] # {"from": "user" | "orchestrator", "text"}
notes: list[str] # facts for the Orchestrator's next turn
pending_questions: list[dict[str, Any]] # {kind, questions} not yet asked
awaiting_questions: list[dict[str, Any]] # {kind, questions} asked, not answered
answers: dict[str, list[dict[str, Any]]] # kind -> [{questions, reply}]
drafts_shown: list[str] # Deliverables a draft of which the user has been shown
drafts: dict[
str, dict[str, Any]
] # kind -> {content, attachments} awaiting the user
# kind -> working memory its tools keep across attempts (e.g. pillar advice)
working: Annotated[dict[str, dict[str, Any]], merge_by_key]
pending_permission: dict[str, Any] | None # {tier, requested_by}
prompt: dict[str, Any] | None # what the user is being asked
# Specialist outcomes, appended concurrently by parallel Specialist tasks and
# applied to the Ledger by the next Orchestrator turn.
outcomes: Annotated[list[dict[str, Any]], operator.add]
outcomes_applied: int
refusals: list[dict[str, Any]] # the Orchestrator's refused tool calls
events_recorded: int
# Routing (ADR-0007).
last_reply: dict[str, Any] | None # the user's message, until a turn routes it
pending_change: dict[str, Any] | None # a change the user was asked to confirm
awaiting_gap_decision: bool # the user was asked whether to close gaps
validation_settled: int # the validation attempt already decided on
awaiting_limit_decision: bool # the user was told a Revision limit was reached
revision_limit_asked: list[str] # Deliverables the user has been told that about
idle_asks: int # questions put to the user running, with nothing changing between
# kind -> runs of its Specialist running that ended without a submission, in an
# Execution with nobody to answer
unfinished_runs: dict[str, int]
ask_fingerprint: str # the Ledger's state when the user was last asked
decisions: list[dict[str, Any]] # every routing decision, in order
# How the finished work was judged when the Execution ended.
assessment: dict[str, Any] | None
turns: int
status: str # running | completed | completed_with_warnings
warnings: list[str]
scripted_answers: list[list[str]] # [regex, answer], pre-approved runs only

View File

@@ -0,0 +1,302 @@
"""The Ledger: every rule code owns about Deliverable progress, Approvals and Revisions.
Pure and synchronous: no I/O, no framework types. Invalid requests raise
LedgerRefusal and leave the Ledger unchanged; tools turn refusals into messages
the agent can act on.
"""
from __future__ import annotations
from collections.abc import Iterable
from dataclasses import asdict, dataclass, field
from enum import StrEnum
from typing import Any
from execution_engine.workflows.workflow import Workflow
PRE_APPROVED_ACTOR = "pre-approved"
class DeliverableStatus(StrEnum):
PENDING = "pending"
IN_PROGRESS = "in_progress"
SUBMITTED = "submitted"
class PhaseStatus(StrEnum):
OPEN = "open"
AWAITING_APPROVAL = "awaiting_approval"
APPROVED = "approved"
class LedgerRefusal(Exception):
"""A request the Ledger's rules don't allow. Nothing was changed."""
@dataclass(frozen=True)
class LedgerEvent:
sequence: int
type: str # started | released | submitted | approval_requested | approved |
# changes_requested | reopened | reset | approval_withdrawn | escalated
subject: str # a Deliverable kind or a Phase id
actor: str
detail: str = ""
@dataclass(frozen=True)
class ReopenResult:
reset: tuple[str, ...]
withdrawn_approvals: tuple[str, ...]
escalation: str | None = None
@dataclass
class _DeliverableRecord:
status: DeliverableStatus = DeliverableStatus.PENDING
attempt: int = 0
revisions: int = 0
feedback: list[str] = field(default_factory=list)
class Ledger:
def __init__(
self, workflow: Workflow, pre_approved: bool = False, max_revisions: int = 3
) -> None:
self.workflow = workflow
self.pre_approved = pre_approved
self.max_revisions = max_revisions
self._graph = workflow.deliverables
self._deliverables = {kind: _DeliverableRecord() for kind in self._graph}
self._phases = {phase: PhaseStatus.OPEN for phase in workflow.phases}
self._events: list[LedgerEvent] = []
# --- queries -----------------------------------------------------------------
@property
def events(self) -> tuple[LedgerEvent, ...]:
return tuple(self._events)
def status(self, kind: str) -> DeliverableStatus:
return self._deliverables[kind].status
def phase_status(self, phase: str) -> PhaseStatus:
return self._phases[phase]
def revisions(self, kind: str) -> int:
return self._deliverables[kind].revisions
def feedback(self, kind: str) -> tuple[str, ...]:
return tuple(self._deliverables[kind].feedback)
def dependencies_met(self, kind: str) -> bool:
spec = self._graph[kind]
for dependency in spec.depends_on:
dependency_phase = self._graph[dependency].phase
if dependency_phase == spec.phase:
if self.status(dependency) is not DeliverableStatus.SUBMITTED:
return False
elif self._phases[dependency_phase] is not PhaseStatus.APPROVED:
return False
return True
def ready_work(self) -> tuple[str, ...]:
return tuple(
kind
for kind in self._graph
if self.status(kind) is DeliverableStatus.PENDING
and self.dependencies_met(kind)
)
def phases_ready_for_approval(self) -> tuple[str, ...]:
return tuple(
phase
for phase in self.workflow.phases
if self._phases[phase] is PhaseStatus.OPEN and not self._unsubmitted(phase)
)
def can_finish(self) -> bool:
return self._phases[self.workflow.phases[-1]] is PhaseStatus.APPROVED
# --- delegation and submission -----------------------------------------------
def start(self, kinds: Iterable[str], actor: str) -> dict[str, int]:
"""Move ready Deliverables in progress; returns the attempt number for each."""
kinds = list(dict.fromkeys(kinds))
ready = set(self.ready_work())
not_ready = [kind for kind in kinds if kind not in ready]
if not_ready:
raise LedgerRefusal(
"Not ready to start: "
+ ", ".join(self._describe_unready(k) for k in not_ready)
)
attempts = {}
for kind in kinds:
record = self._deliverables[kind]
record.status = DeliverableStatus.IN_PROGRESS
record.attempt += 1
attempts[kind] = record.attempt
self._record("started", kind, actor, f"attempt {record.attempt}")
return attempts
def submit(self, kind: str, attempt: int, actor: str) -> None:
"""Accept the current attempt. Any later start, release or reopen supersedes it."""
self._require_current_attempt(kind, attempt)
self._deliverables[kind].status = DeliverableStatus.SUBMITTED
self._record("submitted", kind, actor, f"attempt {attempt}")
def release(self, kind: str, attempt: int, reason: str) -> None:
"""Return in-progress work to pending (e.g. the Specialist needs input)."""
self._require_current_attempt(kind, attempt)
record = self._deliverables[kind]
record.status = DeliverableStatus.PENDING
self._record("released", kind, self._graph[kind].specialist, reason)
# --- approvals ---------------------------------------------------------------
def request_approval(self, phase: str, actor: str) -> None:
if self._phases[phase] is not PhaseStatus.OPEN:
raise LedgerRefusal(f"Phase {phase} is {self._phases[phase]}, not open")
unsubmitted = self._unsubmitted(phase)
if unsubmitted:
raise LedgerRefusal(
f"Phase {phase} has unsubmitted Deliverables: {', '.join(unsubmitted)}"
)
self._phases[phase] = PhaseStatus.AWAITING_APPROVAL
self._record("approval_requested", phase, actor)
if self.pre_approved:
self.approve(phase, actor=PRE_APPROVED_ACTOR)
def approve(self, phase: str, actor: str) -> None:
self._require_awaiting(phase)
self._phases[phase] = PhaseStatus.APPROVED
self._record("approved", phase, actor)
def request_changes(self, phase: str, feedback: str, actor: str) -> None:
self._require_awaiting(phase)
self._phases[phase] = PhaseStatus.OPEN
self._record("changes_requested", phase, actor, feedback)
# --- revisions ---------------------------------------------------------------
def reopen(
self,
kind: str,
reason: str,
actor: str,
override_cap: bool = False,
) -> ReopenResult:
record = self._deliverables[kind]
if record.status is not DeliverableStatus.SUBMITTED:
raise LedgerRefusal(
f"Only submitted Deliverables can be reopened; {kind} is {record.status}"
)
if record.revisions >= self.max_revisions and not override_cap:
escalation = (
f"{kind} has reached its revision limit ({self.max_revisions}). "
f"Requested change: {reason}. Ask the user whether to revise it again "
"or accept it as it stands."
)
self._record("escalated", kind, actor, reason)
return ReopenResult(reset=(), withdrawn_approvals=(), escalation=escalation)
record.status = DeliverableStatus.PENDING
record.revisions += 1
record.feedback.append(reason)
self._record("reopened", kind, actor, reason)
reset = [kind]
for dependent in self.workflow.dependents_of(kind):
dependent_record = self._deliverables[dependent]
if dependent_record.status is DeliverableStatus.PENDING:
continue
dependent_record.status = DeliverableStatus.PENDING
dependent_record.feedback.append(f"Dependency {kind} was revised: {reason}")
self._record("reset", dependent, actor, f"dependency {kind} reopened")
reset.append(dependent)
withdrawn = []
for phase in self.workflow.phases:
affected = any(self._graph[k].phase == phase for k in reset)
if affected and self._phases[phase] is not PhaseStatus.OPEN:
self._phases[phase] = PhaseStatus.OPEN
self._record("approval_withdrawn", phase, actor, f"{kind} reopened")
withdrawn.append(phase)
return ReopenResult(reset=tuple(reset), withdrawn_approvals=tuple(withdrawn))
# --- persistence -------------------------------------------------------------
def to_dict(self) -> dict[str, Any]:
return {
"pre_approved": self.pre_approved,
"max_revisions": self.max_revisions,
"deliverables": {
str(kind): {
"status": record.status.value,
"attempt": record.attempt,
"revisions": record.revisions,
"feedback": list(record.feedback),
}
for kind, record in self._deliverables.items()
},
"phases": {
str(phase): status.value for phase, status in self._phases.items()
},
"events": [asdict(event) for event in self._events],
}
@classmethod
def from_dict(cls, workflow: Workflow, data: dict[str, Any]) -> Ledger:
ledger = cls(
workflow,
pre_approved=data["pre_approved"],
max_revisions=data["max_revisions"],
)
for kind_value, record in data["deliverables"].items():
ledger._deliverables[workflow.kind(kind_value)] = _DeliverableRecord(
status=DeliverableStatus(record["status"]),
attempt=record["attempt"],
revisions=record["revisions"],
feedback=list(record["feedback"]),
)
for phase_value, status in data["phases"].items():
ledger._phases[workflow.phase(phase_value)] = PhaseStatus(status)
ledger._events = [LedgerEvent(**event) for event in data["events"]]
return ledger
# --- internals ---------------------------------------------------------------
def _record(self, type_: str, subject: str, actor: str, detail: str = "") -> None:
self._events.append(
LedgerEvent(len(self._events) + 1, type_, str(subject), actor, detail)
)
def _unsubmitted(self, phase: str) -> list[str]:
return [
kind
for kind in self.workflow.deliverables_in(phase)
if self.status(kind) is not DeliverableStatus.SUBMITTED
]
def _describe_unready(self, kind: str) -> str:
status = self.status(kind)
if status is not DeliverableStatus.PENDING:
return f"{kind} ({status})"
return f"{kind} (dependencies not met)"
def _require_current_attempt(self, kind: str, attempt: int) -> None:
record = self._deliverables[kind]
if (
record.status is not DeliverableStatus.IN_PROGRESS
or attempt != record.attempt
):
raise LedgerRefusal(
f"{kind} is {record.status}, not in_progress with attempt {attempt}; "
"this attempt is not current"
)
def _require_awaiting(self, phase: str) -> None:
if self._phases[phase] is not PhaseStatus.AWAITING_APPROVAL:
raise LedgerRefusal(
f"Phase {phase} is {self._phases[phase]}, not awaiting_approval"
)

View File

@@ -0,0 +1,119 @@
"""Lesson candidates: what went wrong in an Execution and what was asked to fix it.
Collected once, when the Execution ends, from what the Execution already holds: the
Ledger's events, the Specialists' outcome records and the tool refusals. They are raw
material for a reviewed change to a Skill, never memory carried into another Execution.
"""
from __future__ import annotations
from collections.abc import Mapping, Sequence
from typing import Any
from execution_engine.states.ledger import Ledger
MESSAGE_LIMIT = 500
_UNFINISHED = {"budget_exhausted", "stopped"}
def refusal(
role: str, deliverable: str | None, tool: str, tier: str, message: str
) -> dict[str, Any]:
"""One tool refusal, as stored in Execution state.
A cloud-read tool's message may describe the user's environment, so only its
leading words are kept.
"""
if tier == "cloud_read":
message = message.split(":", 1)[0]
return {
"role": role,
"deliverable": deliverable,
"tool": tool,
"message": message[:MESSAGE_LIMIT],
}
def collect_lesson_candidates(
*,
execution_id: str,
customer_case: str,
status: str,
ledger: Ledger,
outcomes: Sequence[Mapping[str, Any]],
orchestrator_refusals: Sequence[Mapping[str, Any]],
role_skills: Mapping[str, Sequence[str]],
skill_versions: Mapping[str, str],
) -> dict[str, Any]:
candidates: list[dict[str, Any]] = []
for event in ledger.events:
if event.type == "reopened":
candidates.append(
{
"type": "revision",
"deliverable": event.subject,
"reason": event.detail,
"requested_by": event.actor,
}
)
elif event.type == "escalated":
candidates.append(
{
"type": "revision_limit",
"deliverable": event.subject,
"reason": event.detail,
}
)
elif event.type == "changes_requested":
candidates.append(
{
"type": "change_request",
"phase": event.subject,
"deliverables": list(
ledger.workflow.deliverables_in(event.subject)
),
"feedback": event.detail,
}
)
elif event.type == "released" and event.detail in _UNFINISHED:
candidates.append(
{
"type": "unfinished",
"deliverable": event.subject,
"role": event.actor,
"reason": event.detail,
}
)
validation = ledger.workflow.validation
for record in outcomes:
for item in record.get("refusals", ()):
candidates.append({"type": "refusal", **item})
if (
validation is not None
and record.get("type") == "submitted"
and record.get("kind") == validation.kind
):
outcome = validation.read(
record.get("attachments", {}).get(validation.data_file, "")
)
attempt = record.get("attempt")
for kind, items in (
("failed_check", outcome["failed"]),
("unverifiable_check", outcome["not_verifiable"]),
("proposed_addition", outcome["proposed_additions"]),
):
for item in items:
candidates.append({"type": kind, "attempt": attempt, **item})
for item in orchestrator_refusals:
candidates.append({"type": "refusal", **item})
return {
"execution_id": execution_id,
"customer_case": customer_case,
"status": status,
"role_skills": {role: list(skills) for role, skills in role_skills.items()},
"skill_versions": dict(skill_versions),
"candidates": candidates,
}