Files
gcp_solution_architecture_a…/app/customer_case.py
Jonathan Boniface 5ef434d00e feat: Change requests against a Baseline (ADR-0006)
A caller restates a request to say how requirements have changed, naming a
completed Execution of the same Customer case as the Baseline.

- app/baseline.py reads a Baseline from its files and refuses one that
  does not exist, did not complete or belongs to another case, before
  anything runs or is recorded. A Baseline is never modified.
- Requirements carry a change (unchanged, added, changed, removed) and a
  reason. Submission is refused when a Baseline requirement disappears, an
  id is reused, or an unchanged requirement's text differs. A removed
  requirement stays on record and asks nothing downstream.
- Each Specialist starts from its delivered version; the graph and the code
  are its working copy, and the description is carried over when the
  design's shape is the same.
- app/workflows/changes.py compares versions by id and traces what each
  changed requirement reaches; anything else that changed is listed as
  unrequested. Approvals show the change for their Phase, and changes.json
  is written with the full Deliverables.
- Intake no longer starts a message read as a change: it names the
  Execution to give as the Baseline.
- A fifth evaluation case runs a request and then a change to it, with
  four checks that the change is recorded, reaches Checks, stays contained
  and costs less than its Baseline.

Tested with scripted agents only; no Change request has run with real
models.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 11:38:15 +01:00

114 lines
3.8 KiB
Python

"""A Customer case on disk: its intent and the history of that intent.
deliverables/cases/<customer_case>/
intent/
001.md, 002.md, ... each statement of what the customer wants, as given
history.json every statement in order, the Execution it started
and how that Execution ended
executions/<execution_id>/ what was generated for one statement
The intent is recorded when an Execution starts, so a request is on record even if
nothing was delivered for it. history.json is the index of the case.
"""
from __future__ import annotations
import json
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from app.states.execution_state import case_dir
INTENT_DIR = "intent"
HISTORY_FILENAME = "history.json"
IN_PROGRESS = "in_progress"
def intent_dir(output_root: Path, customer_case: str) -> Path:
return case_dir(output_root, customer_case) / INTENT_DIR
def read_history(output_root: Path, customer_case: str) -> dict[str, Any]:
"""The case's intent history; empty when the case has none yet."""
path = intent_dir(output_root, customer_case) / HISTORY_FILENAME
if path.is_file():
try:
history = json.loads(path.read_text(encoding="utf-8"))
if isinstance(history, dict) and isinstance(history.get("intents"), list):
return history
except ValueError:
pass
return {"customer_case": customer_case, "intents": []}
def record_intent(
output_root: Path,
customer_case: str,
*,
execution_id: str,
session_id: str,
workflow_request: str,
baseline: str | None = None,
) -> int:
"""Record a new statement of the customer's intent and return its version.
`baseline` is the Execution a Change request starts from; with it the history
shows the case's line of versions.
"""
folder = intent_dir(output_root, customer_case)
folder.mkdir(parents=True, exist_ok=True)
history = read_history(output_root, customer_case)
version = max((item["version"] for item in history["intents"]), default=0) + 1
recorded_at = datetime.now(UTC).isoformat(timespec="seconds")
statement = f"{version:03d}.md"
(folder / statement).write_text(
f"# Intent {version}: {customer_case}\n\n"
f"- Recorded: {recorded_at}\n"
f"- Execution: `{execution_id}`\n"
+ (f"- Changes: `{baseline}`\n" if baseline else "")
+ "\n"
f"## What the customer wants\n\n{workflow_request.strip()}\n",
encoding="utf-8",
)
history["intents"].append(
{
"version": version,
"recorded_at": recorded_at,
"statement": f"{INTENT_DIR}/{statement}",
"workflow_request": workflow_request,
"execution_id": execution_id,
"session_id": session_id,
"status": IN_PROGRESS,
"execution": None,
"baseline": baseline,
}
)
_write_history(folder, history)
return version
def close_intent(
output_root: Path,
customer_case: str,
*,
execution_id: str,
status: str,
has_files: bool,
) -> None:
"""Record how the Execution started by an intent ended, and where its files are."""
history = read_history(output_root, customer_case)
for item in history["intents"]:
if item.get("execution_id") == execution_id:
item["status"] = status
item["execution"] = f"executions/{execution_id}" if has_files else None
_write_history(intent_dir(output_root, customer_case), history)
return
def _write_history(folder: Path, history: dict[str, Any]) -> None:
folder.mkdir(parents=True, exist_ok=True)
(folder / HISTORY_FILENAME).write_text(
json.dumps(history, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
)