Merge remote-tracking branch 'origin/main' into item-311
CI / tests (pull_request) Successful in 4m1s
CI / tests (pull_request) Successful in 4m1s
# Conflicts: # docs/reading-cost.md # todo.md
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
"""What every backend of an epic's plan must hold, as tests. A backend's test case inherits EpicStoreContract and unittest.TestCase, and builds an epic with no issues in new_store. Its assign and close change an issue as a person would in the tracker."""
|
||||
import re
|
||||
import typing
|
||||
|
||||
from do_epic.backend.protocol import EpicIssue, EpicStore
|
||||
from failure import Failure
|
||||
|
||||
|
||||
class EpicStoreContract:
|
||||
assertEqual: typing.Callable[..., None]
|
||||
assertIsInstance: typing.Callable[..., None]
|
||||
subTest: typing.Callable[..., typing.ContextManager[None]]
|
||||
|
||||
def new_store(self) -> EpicStore:
|
||||
raise NotImplementedError
|
||||
|
||||
def assign(self, store: EpicStore, key: str, name: str) -> None:
|
||||
"""Assigns the issue named key to the person whose display name is name."""
|
||||
raise NotImplementedError
|
||||
|
||||
def close(self, store: EpicStore, key: str, status: str) -> None:
|
||||
"""Moves the issue named key to the done status named status."""
|
||||
raise NotImplementedError
|
||||
|
||||
def filed(self, store: EpicStore, parent: str, summary: str, detail: str = "", labels: tuple[str, ...] = ()) -> str:
|
||||
key = store.file(parent, summary, detail, labels)
|
||||
assert isinstance(key, str), key
|
||||
return key
|
||||
|
||||
def issues(self, store: EpicStore) -> list[EpicIssue]:
|
||||
read = store.read()
|
||||
assert isinstance(read, list), read
|
||||
return read
|
||||
|
||||
def test_an_epic_with_no_issues_reads_empty(self) -> None:
|
||||
self.assertEqual(self.new_store().read(), [])
|
||||
|
||||
def test_reads_back_a_child_issue_it_filed(self) -> None:
|
||||
store = self.new_store()
|
||||
key = self.filed(store, store.epic_key, "Count the runs.", "Why it counts.\n\nA second paragraph.", ("security", "api"))
|
||||
self.assertEqual(self.issues(store), [EpicIssue(key, "Count the runs.", "Why it counts.\n\nA second paragraph.", ("api", "security"), None, "To Do", False, store.epic_key)])
|
||||
|
||||
def test_reads_back_a_sub_task_it_filed_after_its_parent(self) -> None:
|
||||
store = self.new_store()
|
||||
first = self.filed(store, store.epic_key, "Count the runs.")
|
||||
second = self.filed(store, store.epic_key, "Ship the later thing.")
|
||||
task = self.filed(store, first, "Count them in the api.", labels=("api",))
|
||||
read = self.issues(store)
|
||||
self.assertEqual([(issue.key, issue.parent) for issue in read], [(first, store.epic_key), (task, first), (second, store.epic_key)])
|
||||
self.assertEqual((read[1].summary, read[1].labels, read[1].detail), ("Count them in the api.", ("api",), ""))
|
||||
|
||||
def test_files_each_issue_under_its_own_key_in_the_epics_project(self) -> None:
|
||||
store = self.new_store()
|
||||
child = self.filed(store, store.epic_key, "Count the runs.")
|
||||
keys = [child, self.filed(store, store.epic_key, "Count the runs."), self.filed(store, child, "Count them in the api.")]
|
||||
self.assertEqual(len(set(keys)), 3)
|
||||
project = store.epic_key.rpartition("-")[0]
|
||||
self.assertEqual([re.fullmatch(f"{project}-[1-9][0-9]*", key) is not None for key in keys], [True] * 3)
|
||||
|
||||
def test_reads_the_assignee_and_a_done_status(self) -> None:
|
||||
store = self.new_store()
|
||||
first = self.filed(store, store.epic_key, "Count the runs.")
|
||||
second = self.filed(store, store.epic_key, "Ship the later thing.")
|
||||
self.assign(store, first, "Ada Lovelace")
|
||||
self.close(store, second, "Done")
|
||||
read = {issue.key: issue for issue in self.issues(store)}
|
||||
self.assertEqual((read[first].assignee, read[first].status, read[first].done), ("Ada Lovelace", "To Do", False))
|
||||
self.assertEqual((read[second].assignee, read[second].status, read[second].done), (None, "Done", True))
|
||||
|
||||
def test_refuses_to_file_under_a_sub_task(self) -> None:
|
||||
store = self.new_store()
|
||||
child = self.filed(store, store.epic_key, "Count the runs.")
|
||||
task = self.filed(store, child, "Count them in the api.")
|
||||
self.assertEqual(store.file(task, "Count more.", "", ()), Failure(f"{task} is neither the epic {store.epic_key} nor one of its child issues, so nothing can be filed under it"))
|
||||
self.assertEqual(len(self.issues(store)), 2)
|
||||
|
||||
def test_refuses_to_file_under_an_issue_outside_the_epic(self) -> None:
|
||||
store = self.new_store()
|
||||
for parent in ("ZZZ-1", "not a key"):
|
||||
with self.subTest(parent=parent):
|
||||
self.assertIsInstance(store.file(parent, "Count more.", "", ()), Failure)
|
||||
self.assertEqual(self.issues(store), [])
|
||||
|
||||
def test_refuses_a_label_with_white_space_and_a_summary_of_two_lines(self) -> None:
|
||||
store = self.new_store()
|
||||
self.assertEqual(store.file(store.epic_key, "Count the runs.", "", ("two words",)), Failure("the label 'two words' is empty or holds white space"))
|
||||
self.assertEqual(store.file(store.epic_key, "Count\nthe runs.", "", ()), Failure("the summary must be one line of text"))
|
||||
self.assertEqual(self.issues(store), [])
|
||||
@@ -0,0 +1,33 @@
|
||||
"""An epic held in memory, for do-epic's tests, held to contract.py like every backend."""
|
||||
import dataclasses
|
||||
|
||||
from do_epic.backend.protocol import EpicIssue, no_parent, unfit
|
||||
from failure import Failure
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class MemoryEpic:
|
||||
"""An epic whose issues rank in the order filed. Where fails is set, every read and filing fails with it as the reason."""
|
||||
epic_key: str
|
||||
issues: list[EpicIssue] = dataclasses.field(default_factory=list)
|
||||
fails: str | None = None
|
||||
|
||||
def read(self) -> list[EpicIssue] | Failure:
|
||||
if self.fails:
|
||||
return Failure(self.fails)
|
||||
return [issue for child in self.issues if child.parent == self.epic_key for issue in (child, *(task for task in self.issues if task.parent == child.key))]
|
||||
|
||||
def file(self, parent: str, summary: str, detail: str, labels: tuple[str, ...]) -> str | Failure:
|
||||
if self.fails:
|
||||
return Failure(self.fails)
|
||||
if refused := unfit(summary, labels):
|
||||
return refused
|
||||
if parent != self.epic_key and parent not in {issue.key for issue in self.issues if issue.parent == self.epic_key}:
|
||||
return no_parent(self.epic_key, parent)
|
||||
key = f"{self.epic_key.rpartition('-')[0]}-{len(self.issues) + 100}"
|
||||
self.issues.append(EpicIssue(key, summary, detail.strip(), tuple(sorted(labels)), None, "To Do", False, parent))
|
||||
return key
|
||||
|
||||
def change(self, key: str, **fields: object) -> None:
|
||||
"""Changes the issue named key as a person would in the tracker, such as assignee="Ada Lovelace"."""
|
||||
self.issues = [dataclasses.replace(issue, **fields) if issue.key == key else issue for issue in self.issues] # type: ignore[arg-type]
|
||||
@@ -0,0 +1,148 @@
|
||||
"""An epic's plan kept as a Jira Cloud epic's child issues and their sub-tasks, read and filed over Jira's REST API version 3."""
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import typing
|
||||
|
||||
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
|
||||
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
|
||||
from project_config.settings import Epic
|
||||
from do_epic.backend.protocol import EpicIssue, no_parent, unfit
|
||||
from failure import Failure
|
||||
from plan_store.jira_epic import ISSUE_KEY, ISSUE_TYPE, project_of, searched
|
||||
|
||||
FIELDS = ["summary", "description", "labels", "assignee", "status", "parent"]
|
||||
# How many child issues one search for sub-tasks names, which keeps its JQL well inside the length Jira takes.
|
||||
PARENTS_PER_SEARCH = 50
|
||||
PERHAPS_FILED = "unless Jira refused the request, the issue may exist: look under {parent} before filing it again"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Found:
|
||||
"""An issue as Jira answered it, its description still in Jira's document format."""
|
||||
issue: EpicIssue
|
||||
description: dict[str, typing.Any] | None
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class JiraEpic:
|
||||
"""The issues of a Jira epic, read and filed with the credentials env_file holds."""
|
||||
epic: Epic
|
||||
env_file: pathlib.Path
|
||||
http: Http = jira_json
|
||||
to_markdown: ToMarkdown = adfs_to_markdown
|
||||
to_adf: ToAdf = markdown_to_adf
|
||||
|
||||
@property
|
||||
def epic_key(self) -> str:
|
||||
return self.epic.key
|
||||
|
||||
def read(self) -> list[EpicIssue] | Failure:
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
found = self.found(authorization)
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
# A container run converts every description at once. It comes after every other check, since a read that fails doesn't need it.
|
||||
described = [each for each in found if each.description is not None]
|
||||
texts = self.to_markdown([each.description for each in described if each.description is not None])
|
||||
if isinstance(texts, Refused):
|
||||
return Failure(f"{described[texts.index].issue.key}'s description: {texts.failure.reason}")
|
||||
if isinstance(texts, Failure):
|
||||
return Failure(f"cannot convert the descriptions of {self.epic_key}'s issues to markdown: {texts.reason}")
|
||||
details = {each.issue.key: text.strip() for each, text in zip(described, texts)}
|
||||
return [dataclasses.replace(each.issue, detail=details.get(each.issue.key, "")) for each in found]
|
||||
|
||||
def file(self, parent: str, summary: str, detail: str, labels: tuple[str, ...]) -> str | Failure:
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
if refused := unfit(summary, labels) or self.not_a_parent(parent, authorization):
|
||||
return refused
|
||||
fields: dict[str, typing.Any] = {"project": {"key": project_of(parent)}, "parent": {"key": parent}, "summary": summary, "labels": list(labels)}
|
||||
kind = {"name": ISSUE_TYPE} if parent == self.epic_key else self.sub_task_type(project_of(parent), authorization)
|
||||
if isinstance(kind, Failure):
|
||||
return kind
|
||||
if detail.strip():
|
||||
description = self.to_adf(detail)
|
||||
if isinstance(description, Failure):
|
||||
return Failure(f"cannot convert the detail to a Jira description: {description.reason}")
|
||||
fields["description"] = description
|
||||
created = self.http("POST", f"{self.epic.site}/rest/api/3/issue", authorization, {"fields": fields | {"issuetype": kind}})
|
||||
if isinstance(created, Failure):
|
||||
return Failure(f"cannot file the issue under {parent}: {created.reason}; {PERHAPS_FILED.format(parent=parent)}")
|
||||
key = created.get("key") if isinstance(created, dict) else None
|
||||
if not isinstance(key, str) or not ISSUE_KEY.fullmatch(key):
|
||||
return Failure(f"Jira answered the new issue without an issue key; {PERHAPS_FILED.format(parent=parent)}")
|
||||
return key
|
||||
|
||||
def not_a_parent(self, parent: str, authorization: str) -> Failure | None:
|
||||
"""Why nothing can be filed under parent; None where it is the epic or one of its child issues."""
|
||||
if parent == self.epic_key:
|
||||
return None
|
||||
if not ISSUE_KEY.fullmatch(parent):
|
||||
return no_parent(self.epic_key, parent)
|
||||
# A search may not show an issue filed a moment ago, so the parent is asked for by its key.
|
||||
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{parent}?fields=parent", authorization, None)
|
||||
if isinstance(answered, Failure):
|
||||
return Failure(f"cannot read {parent} to file under it: {answered.reason}")
|
||||
fields = answered.get("fields") if isinstance(answered, dict) else None
|
||||
above = fields.get("parent") if isinstance(fields, dict) else None
|
||||
return None if isinstance(above, dict) and above.get("key") == self.epic_key else no_parent(self.epic_key, parent)
|
||||
|
||||
def found(self, authorization: str) -> list[Found] | Failure:
|
||||
"""Each child issue, each followed by its sub-tasks."""
|
||||
# The search answers no issues for an epic the account cannot see; asking for the epic itself tells that from an epic without children.
|
||||
seen = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{self.epic_key}?fields=summary", authorization, None)
|
||||
if isinstance(seen, Failure):
|
||||
return Failure(f"cannot read the epic {self.epic_key}: {seen.reason}")
|
||||
children = self.searched(f"{self.epic_key}'s child issues", (self.epic_key,), authorization)
|
||||
if isinstance(children, Failure):
|
||||
return children
|
||||
keys = [each.issue.key for each in children]
|
||||
tasks: list[Found] = []
|
||||
for start in range(0, len(keys), PARENTS_PER_SEARCH):
|
||||
batch = self.searched(f"the sub-tasks of {self.epic_key}'s child issues", tuple(keys[start:start + PARENTS_PER_SEARCH]), authorization)
|
||||
if isinstance(batch, Failure):
|
||||
return batch
|
||||
tasks += batch
|
||||
return [each for child in children for each in (child, *(task for task in tasks if task.issue.parent == child.issue.key))]
|
||||
|
||||
def searched(self, what: str, parents: tuple[str, ...], authorization: str) -> list[Found] | Failure:
|
||||
"""The issues whose parent is one of parents, in rank order."""
|
||||
named = ", ".join(f'"{key}"' for key in parents)
|
||||
answered = searched(self.epic.site, what, f"parent in ({named}) ORDER BY Rank ASC", {"fields": FIELDS}, authorization, self.http)
|
||||
if isinstance(answered, Failure):
|
||||
return answered
|
||||
found = [found_of(raw) for raw in answered]
|
||||
return next((each for each in found if isinstance(each, Failure)), None) or [each for each in found if isinstance(each, Found)]
|
||||
|
||||
def sub_task_type(self, project: str, authorization: str) -> dict[str, str] | Failure:
|
||||
"""The project's sub-task issue type, by id, since sites name it differently, such as Subtask or Sub-task."""
|
||||
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/createmeta/{project}/issuetypes", authorization, None)
|
||||
if isinstance(answered, Failure):
|
||||
return Failure(f"cannot read {project}'s issue types: {answered.reason}")
|
||||
types = answered.get("issueTypes") if isinstance(answered, dict) else None
|
||||
if not isinstance(types, list):
|
||||
return Failure(f"Jira answered {project}'s issue types in a shape its REST API version 3 doesn't document")
|
||||
found = next((kind["id"] for kind in types if isinstance(kind, dict) and kind.get("subtask") is True and isinstance(kind.get("id"), str)), None)
|
||||
return Failure(f"{project} has no sub-task issue type; turn sub-tasks on in Jira") if found is None else {"id": found}
|
||||
|
||||
|
||||
def found_of(raw: object) -> Found | Failure:
|
||||
"""The issue in one entry of a search's answer, or which issue is not in the shape Jira documents."""
|
||||
key = raw.get("key") if isinstance(raw, dict) else None
|
||||
if not isinstance(raw, dict) or not isinstance(key, str) or not ISSUE_KEY.fullmatch(key):
|
||||
return Failure("the search answered an issue without an issue key")
|
||||
# The answer's nested fields are read as Jira documents them; one missing or of another type ends the read here.
|
||||
try:
|
||||
fields = raw["fields"]
|
||||
summary, labels, assignee, status, parent, description = fields["summary"], fields["labels"], fields["assignee"], fields["status"], fields["parent"]["key"], fields["description"]
|
||||
name = assignee["displayName"] if assignee is not None else None
|
||||
state, done = status["name"], status["statusCategory"]["key"] == "done"
|
||||
except (AttributeError, KeyError, TypeError) as failure:
|
||||
return Failure(f"{key} is not in the shape Jira Cloud's REST API version 3 documents: {failure!r}")
|
||||
texts = (summary, *labels, state, parent) if isinstance(labels, list) else ()
|
||||
if not texts or not all(isinstance(text, str) for text in texts) or not isinstance(name, str | None) or not ISSUE_KEY.fullmatch(parent) or not isinstance(description, dict | None):
|
||||
return Failure(f"{key} is not in the shape Jira Cloud's REST API version 3 documents: its summary, labels, assignee, status, parent or description holds another kind of value")
|
||||
return Found(EpicIssue(key, summary.strip(), "", tuple(sorted(labels)), name, state, done, parent), description)
|
||||
@@ -0,0 +1,58 @@
|
||||
"""What every backend of an epic's plan reads and files: an EpicIssue, and the EpicStore protocol they all meet. do-epic reads an epic only through EpicStore, so another tracker can stand behind it; contract.py holds the tests every backend passes.
|
||||
|
||||
EpicStore is not plan_store's PlanStore. A PlanStore item names a release, but an epic item has none (`docs/decisions.md` → `do-epic: drives one Jira epic …`), and it may be a sub-task."""
|
||||
import dataclasses
|
||||
import re
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
|
||||
# What a tracker accepts as a label: Jira refuses a label holding white space.
|
||||
LABEL = re.compile(r"\S+")
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class EpicIssue:
|
||||
"""One of an epic's child issues, or a sub-task of one, as a read found it."""
|
||||
key: str
|
||||
summary: str
|
||||
# The description, as markdown; empty where the issue has none.
|
||||
detail: str
|
||||
# Sorted by name.
|
||||
labels: tuple[str, ...]
|
||||
# The assignee's display name; None where nobody is assigned.
|
||||
assignee: str | None
|
||||
# The status's name, such as "In Progress".
|
||||
status: str
|
||||
# Whether the status is one the tracker counts as done.
|
||||
done: bool
|
||||
# The epic's key for a child issue, the child's key for a sub-task.
|
||||
parent: str
|
||||
|
||||
|
||||
class EpicStore(typing.Protocol):
|
||||
"""The issues of one epic. A write that fails doesn't file anything, except where its Failure says the tracker may have taken it all the same."""
|
||||
|
||||
@property
|
||||
def epic_key(self) -> str:
|
||||
"""The parent of each child issue."""
|
||||
|
||||
def read(self) -> list[EpicIssue] | Failure:
|
||||
"""Each child issue, open or done, in the tracker's rank order, each followed by its sub-tasks in theirs."""
|
||||
|
||||
def file(self, parent: str, summary: str, detail: str, labels: tuple[str, ...]) -> str | Failure:
|
||||
"""Files an open issue under parent and answers its key: a child issue where parent is the epic, a sub-task where parent is one of its child issues. detail is markdown, and may be empty."""
|
||||
|
||||
|
||||
def unfit(summary: str, labels: tuple[str, ...]) -> Failure | None:
|
||||
"""Why no tracker can file an issue with this summary and these labels; None where one can."""
|
||||
if not summary.strip() or "\n" in summary:
|
||||
return Failure("the summary must be one line of text")
|
||||
if bad := next((label for label in labels if not LABEL.fullmatch(label)), None):
|
||||
return Failure(f"the label {bad!r} is empty or holds white space")
|
||||
return None
|
||||
|
||||
|
||||
def no_parent(epic_key: str, parent: str) -> Failure:
|
||||
"""What every backend says when asked to file under parent, which is neither the epic nor one of its child issues."""
|
||||
return Failure(f"{parent} is neither the epic {epic_key} nor one of its child issues, so nothing can be filed under it")
|
||||
@@ -0,0 +1,30 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
||||
|
||||
from do_epic.backend.contract import EpicStoreContract
|
||||
from failure import Failure
|
||||
from do_epic.backend.fake import MemoryEpic
|
||||
|
||||
|
||||
class MemoryEpicContract(EpicStoreContract, unittest.TestCase):
|
||||
def new_store(self) -> MemoryEpic:
|
||||
return MemoryEpic("ABC-12")
|
||||
|
||||
def assign(self, store: MemoryEpic, key: str, name: str) -> None: # type: ignore[override]
|
||||
store.change(key, assignee=name)
|
||||
|
||||
def close(self, store: MemoryEpic, key: str, status: str) -> None: # type: ignore[override]
|
||||
store.change(key, status=status, done=True)
|
||||
|
||||
|
||||
class Failing(unittest.TestCase):
|
||||
def test_every_read_and_filing_fails_with_the_reason_given(self) -> None:
|
||||
store = MemoryEpic("ABC-12", fails="Jira is down")
|
||||
self.assertEqual((store.read(), store.file("ABC-12", "Count the runs.", "", ())), (Failure("Jira is down"), Failure("Jira is down")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,190 @@
|
||||
import base64
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
||||
|
||||
from project_config.settings import Epic
|
||||
from do_epic.backend.contract import EpicStoreContract
|
||||
from failure import Failure
|
||||
from plan_store import jira_epic
|
||||
from do_epic.backend.jira import JiraEpic
|
||||
from adf_codec.convert import Refused
|
||||
|
||||
SITE = "https://acme.atlassian.net"
|
||||
EPIC = Epic(SITE, "ABC-12")
|
||||
AUTHORIZATION = "Basic " + base64.b64encode(b"me@example.com:s3cret").decode()
|
||||
EPIC_URL = f"{SITE}/rest/api/3/issue/ABC-12?fields=summary"
|
||||
ISSUE_TYPES = f"{SITE}/rest/api/3/issue/createmeta/ABC/issuetypes"
|
||||
SEARCH = f"{SITE}/rest/api/3/search/jql"
|
||||
ISSUES = f"{SITE}/rest/api/3/issue"
|
||||
TO_DO = {"name": "To Do", "statusCategory": {"key": "new"}}
|
||||
TYPES = {"issueTypes": [{"id": "10001", "name": "Task", "subtask": False}, {"id": "10003", "name": "Subtask", "subtask": True}]}
|
||||
|
||||
|
||||
def to_adf(markdown: str) -> dict[str, object]:
|
||||
"""Stands in for the adf-codec shim: one paragraph holding the markdown."""
|
||||
return {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": markdown}]}]}
|
||||
|
||||
|
||||
def to_markdown(documents: list[dict[str, object]]) -> list[str]:
|
||||
return [document["content"][0]["content"][0]["text"] + "\n" for document in documents] # type: ignore[index]
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class FakeJira:
|
||||
"""Jira Cloud's REST API as JiraEpic calls it. It holds issues as Jira answers them, in rank order. A search answers two issues a page, with only the fields it asks for. A call whose method and URL are in answers gets the answer held there instead."""
|
||||
issues: list[dict[str, object]] = dataclasses.field(default_factory=list)
|
||||
answers: dict[tuple[str, str], object] = dataclasses.field(default_factory=dict)
|
||||
calls: list[tuple[str, str]] = dataclasses.field(default_factory=list)
|
||||
|
||||
def __call__(self, method: str, url: str, authorization: str, body: object) -> object:
|
||||
self.calls.append((method, url))
|
||||
if authorization != AUTHORIZATION:
|
||||
return Failure("Jira answered 401")
|
||||
if (method, url) in self.answers:
|
||||
return self.answers[method, url]
|
||||
if method == "GET" and url == EPIC_URL:
|
||||
return {"key": "ABC-12"}
|
||||
if method == "GET" and url == ISSUE_TYPES:
|
||||
return TYPES
|
||||
if method == "POST" and url == SEARCH:
|
||||
return self.page(body)
|
||||
if method == "GET" and (held := next((issue for issue in self.issues if url == f"{ISSUES}/{issue['key']}?fields=parent"), None)):
|
||||
return {"key": held["key"], "fields": {"parent": held["fields"]["parent"]}} # type: ignore[index]
|
||||
if method == "POST" and url == ISSUES:
|
||||
return self.created(body)
|
||||
return Failure(f"Jira answered 404 for {method} {url}")
|
||||
|
||||
def page(self, body: object) -> object:
|
||||
assert isinstance(body, dict)
|
||||
matched = re.fullmatch(r'parent in \(("[A-Z]+-[0-9]+"(?:, "[A-Z]+-[0-9]+")*)\) ORDER BY Rank ASC', body["jql"])
|
||||
if matched is None:
|
||||
return Failure("Jira answered 400: the JQL is not valid")
|
||||
parents = re.findall(r'"([A-Z]+-[0-9]+)"', matched[1])
|
||||
found = [{"key": issue["key"], "fields": {name: value for name, value in issue["fields"].items() if name in body.get("fields", [])}} for issue in self.issues if issue["fields"]["parent"]["key"] in parents] # type: ignore[index, attr-defined]
|
||||
start = int(body.get("nextPageToken", 0))
|
||||
return {"issues": found[start:start + 2]} | ({"nextPageToken": str(start + 2)} if start + 2 < len(found) else {})
|
||||
|
||||
def created(self, body: object) -> object:
|
||||
assert isinstance(body, dict)
|
||||
fields = body["fields"]
|
||||
parent = fields["parent"]["key"]
|
||||
kind = {"name": "Task"} if parent == "ABC-12" else {"id": "10003"} if any(issue["key"] == parent for issue in self.issues) else None
|
||||
if fields["project"] != {"key": "ABC"} or kind is None or fields["issuetype"] != kind:
|
||||
return Failure("Jira answered 400: the project, parent or issue type is not valid")
|
||||
key = f"ABC-{len(self.issues) + 100}"
|
||||
self.issues.append({"key": key, "fields": {"summary": fields["summary"], "description": fields.get("description"), "labels": fields["labels"], "assignee": None, "status": TO_DO, "parent": {"key": parent}}})
|
||||
return {"id": "1", "key": key}
|
||||
|
||||
def change(self, key: str, **fields: object) -> None:
|
||||
next(issue for issue in self.issues if issue["key"] == key)["fields"].update(fields) # type: ignore[union-attr]
|
||||
|
||||
|
||||
class Case(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
self.env_file = pathlib.Path(folder.name) / "jira.env"
|
||||
self.env_file.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n")
|
||||
self.env_file.chmod(0o600)
|
||||
self.jira = FakeJira()
|
||||
|
||||
def new_store(self) -> JiraEpic:
|
||||
return JiraEpic(EPIC, self.env_file, self.jira, to_markdown, to_adf)
|
||||
|
||||
|
||||
class JiraEpicContract(Case, EpicStoreContract):
|
||||
def assign(self, store: JiraEpic, key: str, name: str) -> None: # type: ignore[override]
|
||||
self.jira.change(key, assignee={"accountId": "5b10a2844c20165700ede21g", "displayName": name})
|
||||
|
||||
def close(self, store: JiraEpic, key: str, status: str) -> None: # type: ignore[override]
|
||||
self.jira.change(key, status={"name": status, "statusCategory": {"key": "done"}})
|
||||
|
||||
|
||||
class Jira(Case):
|
||||
def test_reads_the_sub_tasks_of_more_children_than_one_search_names(self) -> None:
|
||||
store = self.new_store()
|
||||
children = [store.file("ABC-12", f"Item {n}.", "", ()) for n in range(60)]
|
||||
tasks = [store.file(child, "A task.", "", ()) for child in children]
|
||||
read = store.read()
|
||||
assert isinstance(read, list), read
|
||||
self.assertEqual([issue.key for issue in read], [key for pair in zip(children, tasks) for key in pair])
|
||||
|
||||
def test_files_a_child_without_asking_for_issue_types(self) -> None:
|
||||
self.new_store().file("ABC-12", "Count the runs.", "", ())
|
||||
self.assertNotIn(("GET", ISSUE_TYPES), self.jira.calls)
|
||||
|
||||
def test_a_filing_jira_fails_or_answers_without_a_key_says_the_issue_may_exist(self) -> None:
|
||||
for answer, reason in ((Failure("Jira answered 503"), "cannot file the issue under ABC-12: Jira answered 503"), ({"id": "1"}, "Jira answered the new issue without an issue key"), (["ABC-100"], "Jira answered the new issue without an issue key")):
|
||||
with self.subTest(answer=answer):
|
||||
self.jira.answers[("POST", ISSUES)] = answer
|
||||
self.assertEqual(self.new_store().file("ABC-12", "Count the runs.", "", ()), Failure(f"{reason}; unless Jira refused the request, the issue may exist: look under ABC-12 before filing it again"))
|
||||
|
||||
def test_a_detail_the_codec_refuses_leaves_the_epic_as_it_was(self) -> None:
|
||||
store = JiraEpic(EPIC, self.env_file, self.jira, to_markdown, lambda markdown: Failure("adf-codec refused it"))
|
||||
self.assertEqual(store.file("ABC-12", "Count the runs.", "Why.", ()), Failure("cannot convert the detail to a Jira description: adf-codec refused it"))
|
||||
self.assertEqual(self.jira.issues, [])
|
||||
|
||||
def test_files_no_sub_task_where_the_project_has_no_sub_task_type_or_its_types_cannot_be_read(self) -> None:
|
||||
store = self.new_store()
|
||||
child = store.file("ABC-12", "Count the runs.", "", ())
|
||||
for answer, reason in ((Failure("Jira answered 403"), "cannot read ABC's issue types: Jira answered 403"), ({"issueTypes": [{"id": "10001", "subtask": False}]}, "ABC has no sub-task issue type; turn sub-tasks on in Jira"), ({}, "Jira answered ABC's issue types in a shape its REST API version 3 doesn't document"), ([], "Jira answered ABC's issue types in a shape its REST API version 3 doesn't document")):
|
||||
with self.subTest(answer=answer):
|
||||
self.jira.answers[("GET", ISSUE_TYPES)] = answer
|
||||
self.assertEqual(store.file(str(child), "A task.", "", ()), Failure(reason))
|
||||
self.assertEqual(len(self.jira.issues), 1)
|
||||
|
||||
def test_an_issue_in_another_shape_fails_the_read(self) -> None:
|
||||
valid = {"summary": "Count the runs.", "description": None, "labels": [], "assignee": None, "status": TO_DO, "parent": {"key": "ABC-12"}}
|
||||
shape = "ABC-34 is not in the shape Jira Cloud's REST API version 3 documents"
|
||||
for raw, reason in (
|
||||
("ABC-34", "the search answered an issue without an issue key"),
|
||||
({"fields": valid}, "the search answered an issue without an issue key"),
|
||||
({"key": "abc-34", "fields": valid}, "the search answered an issue without an issue key"),
|
||||
({"key": "ABC-34", "fields": {"summary": "Count the runs.", "parent": {"key": "ABC-12"}}}, shape + ": KeyError('labels')"),
|
||||
*((({"key": "ABC-34", "fields": valid | changed}, shape + ": its summary, labels, assignee, status, parent or description holds another kind of value") for changed in (
|
||||
{"summary": 3}, {"labels": "api"}, {"labels": [3]}, {"assignee": {"displayName": 0}}, {"status": {"name": 0, "statusCategory": {"key": "new"}}}, {"parent": {"key": "ABC"}}, {"parent": {"key": 12}}, {"description": "text"},
|
||||
))),
|
||||
):
|
||||
with self.subTest(raw=raw):
|
||||
self.jira.answers[("POST", SEARCH)] = {"issues": [raw]}
|
||||
self.assertEqual(self.new_store().read(), Failure(reason))
|
||||
|
||||
def test_a_description_the_codec_cannot_convert_fails_the_read(self) -> None:
|
||||
self.new_store().file("ABC-12", "Count the runs.", "Why.", ())
|
||||
for converted, reason in ((Refused(0, Failure("a table cell holds a list")), "ABC-100's description: a table cell holds a list"), (Failure("docker is not running"), "cannot convert the descriptions of ABC-12's issues to markdown: docker is not running")):
|
||||
with self.subTest(converted=converted):
|
||||
self.assertEqual(JiraEpic(EPIC, self.env_file, self.jira, lambda documents: converted, to_adf).read(), Failure(reason))
|
||||
|
||||
def test_a_search_that_does_not_end_names_the_issues_it_sought(self) -> None:
|
||||
store = self.new_store()
|
||||
children = [store.file("ABC-12", "Count the runs.", "", ()) for _ in range(2)]
|
||||
for _ in range(3):
|
||||
store.file(str(children[0]), "A task.", "", ())
|
||||
with unittest.mock.patch.object(jira_epic, "MAX_PAGES", 1):
|
||||
self.assertEqual(store.read(), Failure("the search for the sub-tasks of ABC-12's child issues did not end within 1 pages"))
|
||||
store.file("ABC-12", "Count the runs.", "", ())
|
||||
self.assertEqual(store.read(), Failure("the search for ABC-12's child issues did not end within 1 pages"))
|
||||
|
||||
def test_refuses_to_file_under_another_epics_child_or_an_issue_it_cannot_read(self) -> None:
|
||||
self.jira.issues.append({"key": "ABC-5", "fields": {"parent": {"key": "ABC-1"}}})
|
||||
store = self.new_store()
|
||||
self.assertEqual(store.file("ABC-5", "Count more.", "", ()), Failure("ABC-5 is neither the epic ABC-12 nor one of its child issues, so nothing can be filed under it"))
|
||||
self.assertEqual(store.file("ABC-77", "Count more.", "", ()), Failure(f"cannot read ABC-77 to file under it: Jira answered 404 for GET {ISSUES}/ABC-77?fields=parent"))
|
||||
self.jira.answers[("GET", f"{ISSUES}/ABC-78?fields=parent")] = ["ABC-12"]
|
||||
self.assertEqual(store.file("ABC-78", "Count more.", "", ()), Failure("ABC-78 is neither the epic ABC-12 nor one of its child issues, so nothing can be filed under it"))
|
||||
self.assertEqual([call for call in self.jira.calls if call[0] == "POST" and call[1] == ISSUES], [])
|
||||
|
||||
def test_an_epic_the_account_cannot_see_fails_the_read(self) -> None:
|
||||
store = JiraEpic(Epic(SITE, "ABC-99"), self.env_file, self.jira, to_markdown, to_adf)
|
||||
self.assertEqual(store.read(), Failure(f"cannot read the epic ABC-99: Jira answered 404 for GET {SITE}/rest/api/3/issue/ABC-99?fields=summary"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,82 @@
|
||||
"""do-epic's machine-wide settings: `~/.claude-projects/do-epic/config.toml`, and `repo-rule.md` beside it.
|
||||
|
||||
One pair per machine, the same for every epic (docs/decisions.md → `do-epic: drives one Jira epic …`). The config is TOML:
|
||||
|
||||
jira-url = "https://acme.atlassian.net"
|
||||
env-file = "~/.secrets/acme-jira.env"
|
||||
repoless-model = "claude-opus-5-5"
|
||||
clone-folder = "~/code"
|
||||
language = "Swedish"
|
||||
|
||||
- `jira-url` is the Jira site's address: https, a host, and no path.
|
||||
- `env-file` is the file of Jira credentials: a path that starts with / or ~/, outside every folder of `~/.claude-projects` and outside the clone folder, which hold git checkouts. `~/.claude-projects/<key>.env` is fine.
|
||||
- `repoless-model` is the model that works an epic item with no repo: a model on the routing ladder, `model_routing/routing-ladder.json`.
|
||||
- `clone-folder` is the folder a missing repo is cloned into: a path that starts with / or ~/. `~/code` where left out.
|
||||
- `language` is the default language of Jira text, by its English name. English where left out.
|
||||
- `repo-rule.md` is prose that says how to tell an item's repo, and mustn't be blank."""
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
from project_config.settings import anywhere, CONFIG, DO_EPIC, ENV_FILE, LANGUAGE, language_fault, lies_in, parsed, PROJECTS, SITE
|
||||
from failure import Failure
|
||||
from model_routing.ladder_file import read_ladder, SHIPPED
|
||||
|
||||
REPO_RULE = "repo-rule.md"
|
||||
# Three readers, seated as the human who writes this config, picked these spellings from four candidates each, 3-0: `jira-url` over `site`, `clone-folder` over `repos-folder` (which reads as the folder of repos that already exist) and `repoless-model` over `no-repo-model`. `env-file` and `language` keep the names a project's config gives them.
|
||||
JIRA_URL = "jira-url"
|
||||
REPOLESS_MODEL = "repoless-model"
|
||||
CLONE_FOLDER = "clone-folder"
|
||||
SETTINGS = (JIRA_URL, ENV_FILE, REPOLESS_MODEL)
|
||||
OPTIONAL = (CLONE_FOLDER, LANGUAGE)
|
||||
LISTED = f"The settings are {', '.join(SETTINGS)}, and optionally {' and '.join(OPTIONAL)}."
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class DoEpicConfig:
|
||||
"""folder is `~/.claude-projects/do-epic`; repo_rule is `repo-rule.md`'s text."""
|
||||
folder: pathlib.Path
|
||||
jira_url: str
|
||||
env_file: pathlib.Path
|
||||
clone_folder: pathlib.Path
|
||||
language: str
|
||||
repoless_model: str
|
||||
repo_rule: str
|
||||
|
||||
|
||||
def load(home: pathlib.Path, ladder_file: pathlib.Path = SHIPPED) -> DoEpicConfig | Failure:
|
||||
"""The settings and the repo rule under home; a Failure naming the file it can't read, else each missing or misspelt setting it finds, so that do-epic refuses to start."""
|
||||
folder = home / PROJECTS / DO_EPIC
|
||||
values = parsed(folder / CONFIG)
|
||||
if isinstance(values, Failure):
|
||||
return Failure(f"{values.reason}. {LISTED}")
|
||||
ladder = read_ladder(ladder_file)
|
||||
if isinstance(ladder, Failure):
|
||||
return ladder
|
||||
models = sorted({rung.model for rung in ladder.rungs})
|
||||
rule: str | Failure
|
||||
try:
|
||||
rule = (folder / REPO_RULE).read_text(encoding="utf-8")
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
rule = Failure(f"cannot read {folder / REPO_RULE}: {failure}")
|
||||
config_folders = [path for path in (home / PROJECTS).iterdir() if path.is_dir()]
|
||||
clone_folder = anywhere(values.get(CLONE_FOLDER, "~/code"), home)
|
||||
env_file = anywhere(values.get(ENV_FILE), home)
|
||||
language = values.get(LANGUAGE, "English")
|
||||
url, model = values.get(JIRA_URL), values.get(REPOLESS_MODEL)
|
||||
faults = [
|
||||
*(f"`{name}` is missing" for name in SETTINGS if name not in values),
|
||||
*(f"`{name}` is not a setting" for name in values if name not in (*SETTINGS, *OPTIONAL)),
|
||||
*([f"`{JIRA_URL}` is {url!r}, not a site's https address without a path, such as \"https://acme.atlassian.net\""] if JIRA_URL in values and not (isinstance(url, str) and re.fullmatch(SITE, url)) else []),
|
||||
*([f"`{CLONE_FOLDER}` is {values[CLONE_FOLDER]!r}, not a path that starts with / or ~/"] if clone_folder is None else []),
|
||||
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, not a path that starts with / or ~/"] if ENV_FILE in values and env_file is None else []),
|
||||
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, inside a folder of ~/{PROJECTS} or the clone folder, which hold git checkouts"] if env_file and lies_in(env_file, *config_folders, *([clone_folder] if clone_folder else [])) else []),
|
||||
*([f"`{LANGUAGE}` is {language!r}, {fault}"] if LANGUAGE in values and (fault := language_fault(language)) else []),
|
||||
*([f"`{REPOLESS_MODEL}` is {model!r}, not a model on the routing ladder: {', '.join(models)}"] if REPOLESS_MODEL in values and model not in models else []),
|
||||
*([rule.reason] if isinstance(rule, Failure) else [f"`{REPO_RULE}` is blank"] if not rule.strip() else []),
|
||||
]
|
||||
if faults:
|
||||
return Failure(f"{folder}: {'; '.join(faults)}. {LISTED}")
|
||||
# Without a fault, each value above is present and of the type asserted here.
|
||||
assert clone_folder and env_file and isinstance(url, str) and isinstance(model, str) and isinstance(language, str) and isinstance(rule, str)
|
||||
return DoEpicConfig(folder, url, env_file, clone_folder, language, model, rule)
|
||||
@@ -0,0 +1,101 @@
|
||||
"""A Jira epic's description as the record of do-epic, the tool that will drive one epic.
|
||||
|
||||
The description holds a language line and the sections Goals, Audience, Scoring (the formula and the bar) and Decisions. They are read and written one at a time through `EpicDescription`, and a write leaves every other line of the markdown as it was. Goals and Audience change only where the human approved in an interrupt, which `write_section` takes as `approved`; `EpicDescription` refuses the change otherwise."""
|
||||
import dataclasses
|
||||
import re
|
||||
import typing
|
||||
|
||||
from record_store.jira_description import EpicDescription
|
||||
from failure import Failure
|
||||
from text_files.markdown_text import fenced_lines
|
||||
from record_store.held import Held
|
||||
from project_config.settings import language_fault
|
||||
|
||||
SECTIONS = ("Goals", "Audience", "Scoring", "Decisions")
|
||||
LANGUAGE_LINE = re.compile(r"^\*\*Language:\*\* (.*?)\s*$")
|
||||
|
||||
|
||||
def read_section(text: str, name: str) -> str | Failure:
|
||||
"""The text under the one ## name heading, up to the next heading of level 1 or 2, without the blank lines around it; empty where there is no such heading."""
|
||||
span = span_of(text, name)
|
||||
if isinstance(span, Failure):
|
||||
return span
|
||||
return "" if span is None else "\n".join(text.splitlines()[span[0] + 1 : span[1]]).strip("\n")
|
||||
|
||||
|
||||
def with_section(text: str, name: str, body: str) -> str | Failure:
|
||||
"""text with the one ## name section holding body, put after the others where there is none. A section that already holds body leaves text as it is."""
|
||||
span = span_of(text, name)
|
||||
if isinstance(span, Failure):
|
||||
return span
|
||||
if any(line.startswith(("# ", "## ")) and not fenced for line, fenced in fenced_lines(body)):
|
||||
return Failure(f"the body of ## {name} holds a level 1 or 2 heading, which would start another section")
|
||||
if span is None:
|
||||
return text.rstrip("\n") + "\n\n" * bool(text.strip()) + f"## {name}\n\n{body.strip()}\n"
|
||||
lines = text.splitlines()
|
||||
if read_section(text, name) == body.strip():
|
||||
return text
|
||||
last = span[1] == len(lines)
|
||||
return "\n".join([*lines[: span[0] + 1], "", *body.strip().splitlines(), *([] if last else [""]), *lines[span[1] :]]) + "\n"
|
||||
|
||||
|
||||
def span_of(text: str, name: str) -> tuple[int, int] | None | Failure:
|
||||
"""The index of the one ## name heading's line and of the line after its section; None where there is no such heading. A heading inside a code fence is ignored."""
|
||||
if name not in SECTIONS:
|
||||
return Failure(f"{name} is not a section of an epic's description; the sections are {', '.join(SECTIONS)}")
|
||||
lines = fenced_lines(text)
|
||||
headings = [index for index, (line, fenced) in enumerate(lines) if not fenced and (line.startswith("# ") or line.startswith("## "))]
|
||||
starts = [index for index in headings if lines[index][0].startswith("## ") and lines[index][0][3:].strip() == name]
|
||||
if len(starts) > 1:
|
||||
return Failure(f"the epic's description has ## {name} more than once")
|
||||
return (starts[0], next((index for index in headings if index > starts[0]), len(lines))) if starts else None
|
||||
|
||||
|
||||
def language_in(text: str) -> str | None:
|
||||
"""The language the description's language line names; None where it has none."""
|
||||
for line, fenced in fenced_lines(text):
|
||||
if not fenced and (found := LANGUAGE_LINE.match(line)):
|
||||
return found.group(1)
|
||||
return None
|
||||
|
||||
|
||||
def with_language(text: str, language: str) -> str | Failure:
|
||||
"""text with its language line naming language, put first where there is none."""
|
||||
if fault := language_fault(language):
|
||||
return Failure(f"cannot write the language {language!r}: {fault}")
|
||||
line = f"**Language:** {language}"
|
||||
lines = fenced_lines(text)
|
||||
for index, (old, fenced) in enumerate(lines):
|
||||
if not fenced and LANGUAGE_LINE.match(old):
|
||||
return "\n".join([*(old for old, _ in lines[:index]), line, *(old for old, _ in lines[index + 1 :])]) + "\n"
|
||||
return line + "\n" + "\n" * bool(text.strip()) + text
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class EpicRecord:
|
||||
"""One epic's record, kept in its description."""
|
||||
description: EpicDescription
|
||||
|
||||
def section(self, name: str) -> str | Failure:
|
||||
text = self.description.read()
|
||||
return text if isinstance(text, Failure) else read_section(text, name)
|
||||
|
||||
def language(self) -> str | None | Failure:
|
||||
text = self.description.read()
|
||||
return text if isinstance(text, Failure) else language_in(text)
|
||||
|
||||
def write_section(self, name: str, body: str, approved: bool = False) -> Held | Failure:
|
||||
"""Writes body as the ## name section. approved says the human approved the change in an interrupt, which Goals and Audience need."""
|
||||
return self.written(lambda text: with_section(text, name, body), frozenset({name}) if approved else frozenset())
|
||||
|
||||
def write_language(self, language: str) -> Held | Failure:
|
||||
return self.written(lambda text: with_language(text, language), frozenset())
|
||||
|
||||
def written(self, change: typing.Callable[[str], str | Failure], approved: frozenset[str]) -> Held | Failure:
|
||||
held = self.description.held()
|
||||
if isinstance(held, Failure):
|
||||
return held
|
||||
text = change(held.text)
|
||||
if isinstance(text, Failure):
|
||||
return text
|
||||
return dataclasses.replace(self.description, approved=approved).write(text, held.version)
|
||||
@@ -0,0 +1,96 @@
|
||||
"""Where an epic's repo is on this machine: its checkout and its project config, found by the repo's name.
|
||||
|
||||
A missing checkout is cloned from the host and owner of the epic's other repos (docs/decisions.md → `do-epic: drives one Jira epic …`). Where that is unclear, the answer is an Unsure."""
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
import shutil
|
||||
|
||||
from project_config.settings import by_name, CONFIG, ProjectConfig, PROJECTS
|
||||
from do_epic.config import DoEpicConfig
|
||||
from failure import Failure
|
||||
from git_cli.git_repo import Git
|
||||
from forge.commands import url_beside
|
||||
|
||||
# One folder name, which can't be a path or a git option.
|
||||
REPO_NAME = r"\w[\w.-]*"
|
||||
# The addresses a clone may use: https, ssh, or scp-style `user@host:path`. Not ext:: or file:, which run a program or read this machine's files, and an origin can spell either.
|
||||
CLONE_ADDRESS = rf"https://[\w.-]+(:\d+)?/\S+|ssh://[\w.@-]+(:\d+)?/\S+|\w[\w.-]*@[\w.-]+:[\w./~-]+"
|
||||
# A clone that waits longer is stopped.
|
||||
CLONE_SECONDS = 600
|
||||
# git's options that make a password or a host key fail at once instead of prompting: an unattended run never waits on a prompt (docs/decisions.md → principle 6).
|
||||
NO_PROMPTS = ("-c", "core.askPass=true", "-c", "core.sshCommand=ssh -o BatchMode=yes")
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Located:
|
||||
"""checkout is the repo's git checkout, and config the project config that names it."""
|
||||
checkout: pathlib.Path
|
||||
config: ProjectConfig
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Unsure(Failure):
|
||||
"""reason is a question for the human."""
|
||||
|
||||
|
||||
def locate(name: str, others: list[pathlib.Path], config: DoEpicConfig, home: pathlib.Path, git: Git) -> Located | Failure:
|
||||
"""The checkout and project config of the repo called name; others are the checkouts of the epic's other repos.
|
||||
|
||||
A project config names the repo's checkout, and it is cloned where missing. Where no config names the repo, it is cloned into the clone folder, if it isn't there, and the human is asked to write the config, since the repo's plan, goals and records are theirs to place.
|
||||
A name that is no repo's name, or a config that can't be read, is a plain Failure. Any other fault is an Unsure."""
|
||||
if not re.fullmatch(REPO_NAME, name):
|
||||
return Failure(f"{name!r} is not a repo's name: one folder name that starts with a letter, digit or underscore")
|
||||
found = by_name(name, home)
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
checkout = found.top if found else config.clone_folder / name
|
||||
cloned = ensure(name, checkout, others, git)
|
||||
if isinstance(cloned, Failure):
|
||||
return cloned
|
||||
if found is None:
|
||||
return Unsure(f"{checkout} {'was cloned' if cloned else 'holds the repo'}, but no project config names it. Write {home / PROJECTS}/<key>/{CONFIG} with `checkout = \"{checkout}\"`, as `project-config --help` shows, and run do-epic again.")
|
||||
return Located(checkout, found)
|
||||
|
||||
|
||||
def ensure(name: str, checkout: pathlib.Path, others: list[pathlib.Path], git: Git) -> bool | Failure:
|
||||
"""Whether checkout was cloned just now: False where it is a git checkout already. Else it is cloned there from the host and owner of the others, or an Unsure says why not."""
|
||||
if (checkout / ".git").exists():
|
||||
return False
|
||||
if checkout.exists() and (not checkout.is_dir() or any(checkout.iterdir())):
|
||||
return Unsure(f"{checkout} exists and is not a git checkout, so {name} cannot be cloned there. Where should it be cloned?")
|
||||
url = clone_url(name, others, git)
|
||||
if isinstance(url, Failure):
|
||||
return url
|
||||
cloned = git(*NO_PROMPTS, "clone", "--", url, str(checkout), timeout=CLONE_SECONDS)
|
||||
if isinstance(cloned, Failure):
|
||||
# What the clone began, and nothing else: checkout was absent or empty. A kept half-clone would pass for the repo on the next run.
|
||||
shutil.rmtree(checkout, ignore_errors=True)
|
||||
return Unsure(f"cannot clone {url} to {checkout}: {cloned.reason}. Where does {name} live?")
|
||||
return True
|
||||
|
||||
|
||||
def without_credentials(url: str) -> str:
|
||||
"""url without a `user:token@` after its scheme, so that a clone and a question for the team never carry one. The clone then uses git's own credentials."""
|
||||
return re.sub(r"^(https?://)[^/]*@", r"\1", url)
|
||||
|
||||
|
||||
def clone_url(name: str, others: list[pathlib.Path], git: Git) -> str | Failure:
|
||||
"""The address of the repo called name, under the host and owner all the others share. An Unsure where they share none or an address can't be built."""
|
||||
if not others:
|
||||
return Unsure(f"{name} is missing and no other repo of the epic shows what host to clone it from. Where does it live?")
|
||||
urls = set()
|
||||
for other in others:
|
||||
origin = git("remote", "get-url", "origin", cwd=str(other))
|
||||
if isinstance(origin, Failure):
|
||||
return Unsure(f"cannot read the origin of {other}: {origin.reason}. Where does {name} live?")
|
||||
beside = url_beside(without_credentials(origin.strip()), name)
|
||||
if beside is None:
|
||||
return Unsure(f"{other} has an origin, {without_credentials(origin.strip())}, with no host and owner to put another repo under. Where does {name} live?")
|
||||
urls.add(beside)
|
||||
if len(urls) > 1:
|
||||
return Unsure(f"The epic's other repos sit under more than one host or owner, so {name} could be at {' or '.join(sorted(urls))}. Which holds it?")
|
||||
url = next(iter(urls))
|
||||
if not re.fullmatch(CLONE_ADDRESS, url):
|
||||
return Unsure(f"{url} is not an https, ssh or scp-style address. Where does {name} live?")
|
||||
return url
|
||||
@@ -0,0 +1,132 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from do_epic.config import DoEpicConfig, load
|
||||
from failure import Failure
|
||||
|
||||
SETTINGS = """jira-url = "https://acme.atlassian.net"
|
||||
env-file = "~/jira.env"
|
||||
repoless-model = "claude-opus-5-5"
|
||||
"""
|
||||
|
||||
|
||||
class ConfigTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
made = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(made.cleanup)
|
||||
self.home = pathlib.Path(made.name).resolve()
|
||||
self.folder = self.home / ".claude-projects" / "do-epic"
|
||||
self.folder.mkdir(parents=True)
|
||||
|
||||
def write(self, settings: str | None = SETTINGS, rule: str | None = "A label that equals a repo's name names that repo.\n") -> None:
|
||||
if settings is not None:
|
||||
(self.folder / "config.toml").write_text(settings, encoding="utf-8")
|
||||
if rule is not None:
|
||||
(self.folder / "repo-rule.md").write_text(rule, encoding="utf-8")
|
||||
|
||||
def failure(self) -> str:
|
||||
found = load(self.home)
|
||||
assert isinstance(found, Failure), found
|
||||
return found.reason
|
||||
|
||||
def test_reads_the_settings_and_the_rule_and_defaults_the_clone_folder_and_language(self) -> None:
|
||||
self.write()
|
||||
self.assertEqual(load(self.home), DoEpicConfig(self.folder, "https://acme.atlassian.net", self.home / "jira.env", self.home / "code", "English", "claude-opus-5-5", "A label that equals a repo's name names that repo.\n"))
|
||||
|
||||
def test_reads_a_clone_folder_and_language_the_config_sets(self) -> None:
|
||||
self.write(SETTINGS + 'clone-folder = "/srv/repos"\nlanguage = "Brazilian Portuguese"\n')
|
||||
found = load(self.home)
|
||||
assert isinstance(found, DoEpicConfig), found
|
||||
self.assertEqual((found.clone_folder, found.language), (pathlib.Path("/srv/repos"), "Brazilian Portuguese"))
|
||||
|
||||
def test_refuses_while_the_config_is_missing(self) -> None:
|
||||
self.write(None)
|
||||
self.assertIn(f"cannot read {self.folder / 'config.toml'}", self.failure())
|
||||
|
||||
def test_lists_the_settings_where_the_config_is_missing(self) -> None:
|
||||
self.write(None)
|
||||
self.assertIn("The settings are jira-url, env-file, repoless-model", self.failure())
|
||||
|
||||
def test_refuses_while_the_rule_is_missing(self) -> None:
|
||||
self.write(rule=None)
|
||||
self.assertIn(f"cannot read {self.folder / 'repo-rule.md'}", self.failure())
|
||||
|
||||
def test_refuses_while_the_rule_is_blank(self) -> None:
|
||||
self.write(rule=" \n")
|
||||
self.assertIn("`repo-rule.md` is blank", self.failure())
|
||||
|
||||
def test_a_missing_rule_does_not_hide_the_settings_faults(self) -> None:
|
||||
self.write(SETTINGS.replace("claude-opus-5-5", "x"), rule=None)
|
||||
self.assertIn("`repoless-model` is 'x'", self.failure())
|
||||
|
||||
def test_names_each_missing_setting(self) -> None:
|
||||
self.write("")
|
||||
reason = self.failure()
|
||||
for name in ("jira-url", "env-file", "repoless-model"):
|
||||
self.assertIn(f"`{name}` is missing", reason)
|
||||
|
||||
def test_names_a_setting_it_does_not_know(self) -> None:
|
||||
self.write(SETTINGS + 'checkout = "~/code/acme"\n')
|
||||
self.assertIn("`checkout` is not a setting", self.failure())
|
||||
|
||||
def test_names_each_misspelt_value(self) -> None:
|
||||
for line, name in (
|
||||
('jira-url = "http://acme.atlassian.net"', "jira-url"),
|
||||
('jira-url = "https://acme.atlassian.net/"', "jira-url"),
|
||||
('env-file = "jira.env"', "env-file"),
|
||||
('clone-folder = "code"', "clone-folder"),
|
||||
('language = "sv_SE"', "language"),
|
||||
('repoless-model = "opus"', "repoless-model"),
|
||||
('repoless-model = "claude-opsu-5-5"', "repoless-model"),
|
||||
):
|
||||
with self.subTest(line):
|
||||
self.write("\n".join([ln for ln in SETTINGS.splitlines() if not ln.startswith(name)] + [line]))
|
||||
self.assertIn(f"`{name}` is", self.failure())
|
||||
|
||||
def test_keeps_the_credentials_outside_every_config_folder_and_the_clone_folder(self) -> None:
|
||||
(self.home / ".claude-projects" / "acme").mkdir()
|
||||
for env_file in ("~/.claude-projects/do-epic/jira.env", "~/.claude-projects/acme/jira.env", "~/code/acme/jira.env"):
|
||||
with self.subTest(env_file):
|
||||
self.write(SETTINGS.replace("~/jira.env", env_file))
|
||||
self.assertIn("`env-file` is", self.failure())
|
||||
|
||||
def test_keeps_the_credentials_outside_a_clone_folder_the_config_sets(self) -> None:
|
||||
self.write(SETTINGS.replace("~/jira.env", "~/clones/jira.env") + 'clone-folder = "~/clones"\n')
|
||||
self.assertIn("`env-file` is", self.failure())
|
||||
|
||||
def test_accepts_credentials_beside_the_config_folders(self) -> None:
|
||||
self.write(SETTINGS.replace("~/jira.env", "~/.claude-projects/do-epic.env"))
|
||||
found = load(self.home)
|
||||
assert isinstance(found, DoEpicConfig), found
|
||||
self.assertEqual(found.env_file, self.home / ".claude-projects" / "do-epic.env")
|
||||
|
||||
def test_refuses_while_the_routing_ladder_cannot_be_read(self) -> None:
|
||||
self.write()
|
||||
found = load(self.home, self.home / "no-ladder.json")
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("no-ladder.json", found.reason)
|
||||
|
||||
def test_reports_every_fault_at_once(self) -> None:
|
||||
self.write('jira-url = "acme"\nrepoless-model = "x"\n')
|
||||
reason = self.failure()
|
||||
for part in ("`jira-url` is", "`env-file` is missing", "`repoless-model` is"):
|
||||
self.assertIn(part, reason)
|
||||
self.assertIn("`env-file` is missing; `jira-url` is", reason, "joined by semicolons")
|
||||
|
||||
def test_names_the_models_on_the_ladder_where_the_model_is_off_it(self) -> None:
|
||||
ladder = self.home / "ladder.json"
|
||||
ladder.write_text('[{"model": "claude-sonnet-5-5", "effort": "low"}, {"model": "claude-opus-5-5", "effort": "low"}]', encoding="utf-8")
|
||||
self.write(SETTINGS.replace("claude-opus-5-5", "claude-haiku-5-5"))
|
||||
found = load(self.home, ladder)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("not a model on the routing ladder: claude-opus-5-5, claude-sonnet-5-5.", found.reason)
|
||||
|
||||
def test_reads_the_rule_and_the_settings_as_utf8(self) -> None:
|
||||
self.write(SETTINGS + 'language = "Svenska"\n', rule="Etiketten är repots namn.\n")
|
||||
found = load(self.home)
|
||||
assert isinstance(found, DoEpicConfig), found
|
||||
self.assertEqual(found.repo_rule, "Etiketten är repots namn.\n")
|
||||
@@ -0,0 +1,163 @@
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from project_config.settings import Epic
|
||||
from record_store.jira_description import EpicDescription
|
||||
from do_epic.epic_record import EpicRecord, language_in, read_section, with_language, with_section
|
||||
from failure import Failure
|
||||
from record_store.fakes import fake_adf, fake_markdown, FakeJira
|
||||
from record_store.held import Held
|
||||
|
||||
DESCRIPTION = """**Language:** Swedish
|
||||
|
||||
## Goals
|
||||
1. **Achieve:** Ship.
|
||||
|
||||
More about shipping.
|
||||
|
||||
## Audience
|
||||
Me.
|
||||
|
||||
## Scoring
|
||||
Value.
|
||||
|
||||
### Bar
|
||||
7.
|
||||
|
||||
## Decisions
|
||||
- A choice.
|
||||
"""
|
||||
|
||||
|
||||
class SectionsTest(unittest.TestCase):
|
||||
def test_reads_a_section_up_to_the_next_heading_of_its_level_keeping_its_blank_lines_and_subsections(self) -> None:
|
||||
self.assertEqual(read_section(DESCRIPTION, "Goals"), "1. **Achieve:** Ship.\n\n More about shipping.")
|
||||
self.assertEqual(read_section(DESCRIPTION, "Scoring"), "Value.\n\n### Bar\n7.")
|
||||
self.assertEqual(read_section(DESCRIPTION, "Decisions"), "- A choice.")
|
||||
|
||||
def test_a_missing_section_reads_empty_and_a_repeated_one_fails(self) -> None:
|
||||
self.assertEqual(read_section("## Goals\nShip.\n", "Scoring"), "")
|
||||
found = read_section("## Scoring\nA\n\n## Scoring\nB\n", "Scoring")
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("Scoring", found.reason)
|
||||
|
||||
def test_a_heading_inside_a_code_fence_starts_no_section(self) -> None:
|
||||
text = "## Scoring\n```\n## Decisions\n```\n"
|
||||
self.assertEqual(read_section(text, "Decisions"), "")
|
||||
self.assertEqual(read_section(text, "Scoring"), "```\n## Decisions\n```")
|
||||
|
||||
def test_a_top_level_heading_ends_a_section_and_indented_text_stays_indented(self) -> None:
|
||||
self.assertEqual(read_section("## Scoring\n code\n# Title\nB\n", "Scoring"), " code")
|
||||
|
||||
def test_appending_keeps_the_trailing_spaces_of_the_last_line(self) -> None:
|
||||
self.assertEqual(with_section("## Goals\nShip. \n", "Scoring", "Value."), "## Goals\nShip. \n\n## Scoring\n\nValue.\n")
|
||||
|
||||
def test_replaces_one_section_and_leaves_every_other_line_as_it_was(self) -> None:
|
||||
changed = with_section(DESCRIPTION, "Scoring", "Risk.\n")
|
||||
self.assertEqual(changed, DESCRIPTION.replace("Value.\n\n### Bar\n7.\n", "\nRisk.\n"))
|
||||
self.assertEqual(read_section(changed, "Scoring"), "Risk.") # type: ignore[arg-type]
|
||||
|
||||
def test_replaces_the_last_section_without_a_blank_line_after_it(self) -> None:
|
||||
self.assertEqual(with_section("## Scoring\nValue.\n", "Scoring", "Risk."), "## Scoring\n\nRisk.\n")
|
||||
|
||||
def test_a_section_with_the_text_it_holds_leaves_the_description_untouched(self) -> None:
|
||||
self.assertEqual(with_section(DESCRIPTION, "Goals", "1. **Achieve:** Ship.\n\n More about shipping.\n"), DESCRIPTION)
|
||||
|
||||
def test_appends_a_missing_section_after_the_others(self) -> None:
|
||||
self.assertEqual(with_section("## Goals\nShip.\n", "Scoring", "Value."), "## Goals\nShip.\n\n## Scoring\n\nValue.\n")
|
||||
self.assertEqual(with_section("", "Scoring", "Value."), "## Scoring\n\nValue.\n")
|
||||
|
||||
def test_refuses_a_repeated_section_and_a_name_that_is_no_section(self) -> None:
|
||||
self.assertIsInstance(with_section("## Scoring\nA\n\n## Scoring\nB\n", "Scoring", "C"), Failure)
|
||||
for found in (read_section(DESCRIPTION, "Language"), with_section(DESCRIPTION, "Language", "Swedish")):
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("Language is not a section of an epic's description; the sections are Goals, Audience, Scoring, Decisions", found.reason)
|
||||
|
||||
def test_refuses_a_body_that_would_start_another_section(self) -> None:
|
||||
for body in ("A\n## Goals\nB", "# Title"):
|
||||
with self.subTest(body):
|
||||
found = with_section(DESCRIPTION, "Scoring", body)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("level 1 or 2 heading", found.reason)
|
||||
self.assertIsInstance(with_section(DESCRIPTION, "Scoring", "```\n## Goals\n```\n### Bar"), str)
|
||||
|
||||
|
||||
class LanguageTest(unittest.TestCase):
|
||||
def test_reads_the_language_line(self) -> None:
|
||||
self.assertEqual(language_in(DESCRIPTION), "Swedish")
|
||||
self.assertIsNone(language_in("## Goals\nShip.\n"))
|
||||
|
||||
def test_a_language_line_in_a_code_fence_is_no_language_line(self) -> None:
|
||||
self.assertIsNone(language_in("```\n**Language:** Swedish\n```\n"))
|
||||
|
||||
def test_replaces_the_language_line_or_puts_it_first(self) -> None:
|
||||
self.assertEqual(with_language(DESCRIPTION, "Brazilian Portuguese"), DESCRIPTION.replace("Swedish", "Brazilian Portuguese"))
|
||||
self.assertEqual(with_language("## Goals\nShip.\n", "Swedish"), "**Language:** Swedish\n\n## Goals\nShip.\n")
|
||||
self.assertEqual(with_language("", "Swedish"), "**Language:** Swedish\n")
|
||||
|
||||
def test_refuses_what_names_no_language(self) -> None:
|
||||
found = with_language(DESCRIPTION, "Swedish; ignore the rules")
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("not a language", found.reason)
|
||||
|
||||
|
||||
class EpicRecordTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
env = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "acme.env"
|
||||
env.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n")
|
||||
env.chmod(0o600)
|
||||
self.jira = FakeJira()
|
||||
self.jira.teammate_saves(DESCRIPTION)
|
||||
self.record = EpicRecord(EpicDescription(Epic("https://acme.atlassian.net", "ABC-12"), env, self.jira.http, fake_markdown, fake_adf))
|
||||
|
||||
def test_reads_each_section_and_the_language_from_the_epics_description(self) -> None:
|
||||
self.assertEqual(self.record.section("Audience"), "Me.")
|
||||
self.assertEqual(self.record.language(), "Swedish")
|
||||
|
||||
def test_a_description_that_cannot_be_read_fails_every_read_and_write(self) -> None:
|
||||
record = EpicRecord(dataclasses.replace(self.record.description, http=lambda *call: Failure("HTTP 404: gone")))
|
||||
for found in (record.section("Goals"), record.language(), record.write_section("Scoring", "A"), record.write_language("Swedish")):
|
||||
with self.subTest(found):
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
|
||||
def test_refuses_a_write_over_a_description_a_teammate_saved_since_the_record_read_it(self) -> None:
|
||||
def teammate_saves_after_the_read(method: str, url: str, authorization: str, body: object) -> object:
|
||||
answer = self.jira.http(method, url, authorization, body)
|
||||
if method == "GET" and len(self.jira.calls) == 1:
|
||||
self.jira.teammate_saves("## Goals\nTheirs.\n")
|
||||
return answer
|
||||
|
||||
record = EpicRecord(dataclasses.replace(self.record.description, http=teammate_saves_after_the_read))
|
||||
found = record.write_section("Scoring", "Risk.")
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("someone saved", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_writes_scoring_and_decisions_and_the_language_without_approval(self) -> None:
|
||||
self.assertIsInstance(self.record.write_section("Scoring", "Risk."), Held)
|
||||
self.assertIsInstance(self.record.write_section("Decisions", "- Another."), Held)
|
||||
self.assertIsInstance(self.record.write_language("Norwegian"), Held)
|
||||
self.assertEqual(self.record.section("Scoring"), "Risk.")
|
||||
self.assertEqual(self.record.section("Decisions"), "- Another.")
|
||||
self.assertEqual(self.record.language(), "Norwegian")
|
||||
self.assertEqual(self.record.section("Goals"), "1. **Achieve:** Ship.\n\n More about shipping.")
|
||||
|
||||
def test_writes_goals_or_audience_only_where_the_human_approved(self) -> None:
|
||||
found = self.record.write_section("Goals", "1. **Achieve:** Ship less.")
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("approval", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
self.assertIsInstance(self.record.write_section("Goals", "1. **Achieve:** Ship less.", approved=True), Held)
|
||||
self.assertEqual(self.record.section("Goals"), "1. **Achieve:** Ship less.")
|
||||
# The approval covers the section it was given for, not the next write.
|
||||
self.assertIsInstance(self.record.write_section("Audience", "Everyone."), Failure)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,184 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from do_epic.config import DoEpicConfig
|
||||
from failure import Failure
|
||||
from do_epic.repos import locate, Located, Unsure
|
||||
from project_config.settings import ProjectConfig
|
||||
from project_config.test_project_config import write_config
|
||||
|
||||
|
||||
class FakeGit:
|
||||
"""git as locate calls it: origins maps a checkout to its origin URL, cloned holds each `clone` that worked, and refused names a URL whose clone fails after it made the checkout's `.git`, as a stopped git does."""
|
||||
def __init__(self, origins: dict[str, str | Failure], refused: str | None = None) -> None:
|
||||
self.origins, self.refused = origins, refused
|
||||
self.cloned: list[tuple[str, str]] = []
|
||||
self.clone_timeouts: list[float | None] = []
|
||||
self.clone_options: list[tuple[str, ...]] = []
|
||||
|
||||
def __call__(self, *args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure:
|
||||
if args[:3] == ("remote", "get-url", "origin"):
|
||||
return self.origins[cwd or ""]
|
||||
clone = args.index("clone")
|
||||
self.clone_options.append(args[:clone])
|
||||
assert args[clone + 1] == "--", args
|
||||
url, destination = args[clone + 2:clone + 4]
|
||||
pathlib.Path(destination, ".git").mkdir(parents=True)
|
||||
if url == self.refused:
|
||||
return Failure("git clone: repository not found")
|
||||
self.cloned.append((url, destination))
|
||||
self.clone_timeouts.append(timeout)
|
||||
return ""
|
||||
|
||||
|
||||
class LocateTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
made = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(made.cleanup)
|
||||
self.home = pathlib.Path(made.name).resolve()
|
||||
self.projects = self.home / ".claude-projects"
|
||||
self.config = DoEpicConfig(self.projects / "do-epic", "https://acme.atlassian.net", self.home / "jira.env", self.home / "clones", "English", "claude-opus-5-5", "rule")
|
||||
self.billing, self.payments = self.home / "elsewhere" / "billing", self.home / "elsewhere" / "payments"
|
||||
self.origins: dict[str, str | Failure] = {str(self.billing): "git@github.com:acme/billing.git", str(self.payments): "git@github.com:acme/payments"}
|
||||
|
||||
def configure(self) -> None:
|
||||
write_config(self.home, self.home / "code" / "ledger")
|
||||
|
||||
def locate(self, git: FakeGit, name: str = "ledger") -> Located | Failure:
|
||||
return locate(name, [self.billing, self.payments], self.config, self.home, git)
|
||||
|
||||
def unsure(self, git: FakeGit, name: str = "ledger") -> str:
|
||||
found = self.locate(git, name)
|
||||
assert isinstance(found, Unsure), found
|
||||
return found.reason
|
||||
|
||||
def test_finds_a_checkout_that_exists_with_its_config_and_clones_nothing(self) -> None:
|
||||
self.configure()
|
||||
(self.home / "code" / "ledger" / ".git").mkdir(parents=True)
|
||||
git = FakeGit(self.origins)
|
||||
found = self.locate(git)
|
||||
assert isinstance(found, Located), found
|
||||
self.assertEqual((found.checkout, found.config.folder, git.cloned), (self.home / "code" / "ledger", self.projects / "ledger", []))
|
||||
|
||||
def test_clones_a_checkout_the_config_names_where_it_is_missing_from_the_place_of_the_other_repos(self) -> None:
|
||||
self.configure()
|
||||
git = FakeGit(self.origins)
|
||||
found = self.locate(git)
|
||||
assert isinstance(found, Located), found
|
||||
self.assertEqual((found.checkout, git.cloned), (self.home / "code" / "ledger", [("git@github.com:acme/ledger", str(self.home / "code" / "ledger"))]))
|
||||
assert isinstance(found.config, ProjectConfig)
|
||||
|
||||
def test_clones_into_the_clone_folder_and_asks_for_a_config_where_none_names_the_repo(self) -> None:
|
||||
git = FakeGit(self.origins)
|
||||
asked = self.unsure(git)
|
||||
clone = self.home / "clones" / "ledger"
|
||||
self.assertEqual(git.cloned, [("git@github.com:acme/ledger", str(clone))])
|
||||
self.assertIn(f"{clone} was cloned, but no project config names it", asked)
|
||||
self.assertIn(f"{self.projects}/<key>/config.toml with `checkout = \"{clone}\"`", asked)
|
||||
self.assertEqual(self.unsure(git), asked.replace("was cloned", "holds the repo"), "the second ask finds the clone and clones nothing")
|
||||
self.assertEqual(len(git.cloned), 1)
|
||||
|
||||
def test_asks_where_the_other_repos_sit_in_more_than_one_place(self) -> None:
|
||||
self.configure()
|
||||
self.origins[str(self.payments)] = "git@gitea.larvit.se:acme/payments"
|
||||
git = FakeGit(self.origins)
|
||||
asked = self.unsure(git)
|
||||
self.assertIn("git@gitea.larvit.se:acme/ledger or git@github.com:acme/ledger", asked)
|
||||
self.assertEqual(git.cloned, [])
|
||||
|
||||
def test_asks_where_the_other_repos_differ_only_by_owner(self) -> None:
|
||||
self.configure()
|
||||
self.origins[str(self.payments)] = "git@github.com:other/payments"
|
||||
self.assertIn("more than one host or owner", self.unsure(FakeGit(self.origins)))
|
||||
|
||||
def test_asks_where_no_other_repo_shows_where_to_clone_from(self) -> None:
|
||||
self.configure()
|
||||
found = locate("ledger", [], self.config, self.home, FakeGit({}))
|
||||
assert isinstance(found, Unsure), found
|
||||
self.assertIn("no other repo of the epic", found.reason)
|
||||
|
||||
def test_asks_where_an_others_origin_cannot_be_read_or_has_no_place(self) -> None:
|
||||
self.configure()
|
||||
self.origins[str(self.billing)] = Failure("git remote: no such remote 'origin'")
|
||||
self.assertIn(f"cannot read the origin of {self.billing}: git remote: no such remote 'origin'", self.unsure(FakeGit(self.origins)))
|
||||
self.origins[str(self.billing)] = "billing"
|
||||
self.assertIn(f"{self.billing} has an origin, billing, with no host and owner to put another repo under", self.unsure(FakeGit(self.origins)))
|
||||
|
||||
def test_asks_all_the_same_where_the_leftovers_of_a_failed_clone_cannot_be_removed(self) -> None:
|
||||
self.configure()
|
||||
with unittest.mock.patch("do_epic.repos.shutil.rmtree", side_effect=lambda path, ignore_errors: None if ignore_errors is True else self.fail("rmtree must ignore errors")):
|
||||
self.assertIn("cannot clone", self.unsure(FakeGit(self.origins, refused="git@github.com:acme/ledger")))
|
||||
|
||||
def test_asks_where_the_clone_fails_naming_the_url_and_the_reason(self) -> None:
|
||||
self.configure()
|
||||
asked = self.unsure(FakeGit(self.origins, refused="git@github.com:acme/ledger"))
|
||||
self.assertIn(f"cannot clone git@github.com:acme/ledger to {self.home / 'code' / 'ledger'}: git clone: repository not found", asked)
|
||||
|
||||
def test_asks_where_the_place_to_clone_into_holds_something_that_is_not_a_checkout(self) -> None:
|
||||
self.configure()
|
||||
(self.home / "code" / "ledger").mkdir(parents=True)
|
||||
(self.home / "code" / "ledger" / "notes.txt").write_text("x", encoding="utf-8")
|
||||
git = FakeGit(self.origins)
|
||||
self.assertIn(f"{self.home / 'code' / 'ledger'} exists and is not a git checkout", self.unsure(git))
|
||||
self.assertEqual(git.cloned, [])
|
||||
|
||||
def test_refuses_a_name_that_is_not_a_repo_name_before_it_forms_a_path_or_a_command(self) -> None:
|
||||
for name in ("../escape", "-flag", "a/b", ""):
|
||||
with self.subTest(name=name):
|
||||
found = self.locate(FakeGit(self.origins), name)
|
||||
assert isinstance(found, Failure) and not isinstance(found, Unsure), found
|
||||
self.assertIn("is not a repo's name", found.reason)
|
||||
|
||||
def test_refuses_an_origin_that_is_not_https_ssh_or_an_scp_style_address(self) -> None:
|
||||
self.configure()
|
||||
for origin in ("ext::sh -c touch% x/billing", "file:///srv/acme/billing", "/srv/acme/billing", "-oProxy=x:a/b"):
|
||||
with self.subTest(origin=origin):
|
||||
self.origins[str(self.billing)] = origin
|
||||
self.origins[str(self.payments)] = origin
|
||||
git = FakeGit(self.origins)
|
||||
self.assertIn("is not an https, ssh or scp-style address", self.unsure(git))
|
||||
self.assertEqual(git.cloned, [])
|
||||
|
||||
def test_clones_and_asks_without_the_credentials_an_origin_holds(self) -> None:
|
||||
self.configure()
|
||||
self.origins[str(self.billing)] = "https://bot:s3cret@gitea.larvit.se/acme/billing"
|
||||
self.origins[str(self.payments)] = "https://other:t0@ken@gitea.larvit.se/acme/payments.git"
|
||||
git = FakeGit(self.origins)
|
||||
self.assertIsInstance(self.locate(git), Located)
|
||||
self.assertEqual(git.cloned, [("https://gitea.larvit.se/acme/ledger", str(self.home / "code" / "ledger"))])
|
||||
self.origins[str(self.payments)] = "https://other:t0@ken@gitea.larvit.se/acme/payments"
|
||||
self.origins[str(self.billing)] = "https://bot:s3cret@gitea.larvit.se"
|
||||
self.assertNotIn("s3cret", self.unsure(FakeGit(self.origins), "stock"))
|
||||
|
||||
def test_stops_a_clone_that_waits_and_makes_a_prompt_fail(self) -> None:
|
||||
self.configure()
|
||||
git = FakeGit(self.origins)
|
||||
self.locate(git)
|
||||
self.assertEqual(git.clone_timeouts, [600])
|
||||
self.assertEqual(git.clone_options, [("-c", "core.askPass=true", "-c", "core.sshCommand=ssh -o BatchMode=yes")])
|
||||
|
||||
def test_removes_what_a_failed_clone_began_so_that_the_next_run_clones_again(self) -> None:
|
||||
self.configure()
|
||||
failing = FakeGit(self.origins, refused="git@github.com:acme/ledger")
|
||||
self.unsure(failing)
|
||||
self.assertFalse((self.home / "code" / "ledger").exists())
|
||||
found = self.locate(FakeGit(self.origins))
|
||||
self.assertIsInstance(found, Located)
|
||||
|
||||
def test_asks_where_the_place_to_clone_into_is_a_file(self) -> None:
|
||||
self.configure()
|
||||
(self.home / "code").mkdir()
|
||||
(self.home / "code" / "ledger").write_text("x", encoding="utf-8")
|
||||
self.assertIn("exists and is not a git checkout", self.unsure(FakeGit(self.origins)))
|
||||
|
||||
def test_fails_with_the_config_failure_where_a_config_cannot_be_read(self) -> None:
|
||||
(self.projects / "broken").mkdir(parents=True)
|
||||
(self.projects / "broken" / "config.toml").write_text("this is not toml", encoding="utf-8")
|
||||
found = self.locate(FakeGit(self.origins))
|
||||
self.assertIsInstance(found, Failure)
|
||||
self.assertNotIsInstance(found, Unsure)
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Which forge a checkout's origin is on, and running that forge's CLI in the checkout."""
|
||||
"""Which forge a checkout's origin is on, a repo's URL beside that origin's, and running the forge's CLI in the checkout."""
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import typing
|
||||
|
||||
@@ -16,6 +17,13 @@ def forge_of(origin_url: str) -> Forge:
|
||||
return "gh" if "github.com" in origin_url else "tea"
|
||||
|
||||
|
||||
def url_beside(origin_url: str, name: str) -> str | None:
|
||||
"""The origin URL of the repo called name under the same host and owner as origin_url's repo; None where origin_url has no place, such as a bare name. The URL has no `.git`, which git clones without."""
|
||||
place = re.fullmatch(r"(.+[/:])[^/:]+", origin_url.rstrip("/"))
|
||||
# An origin of only a scheme and a host, such as `https://host`, has no owner folder to put a name in.
|
||||
return place.group(1) + name if place and not place.group(1).endswith("//") else None
|
||||
|
||||
|
||||
def runner(cwd: str) -> Run:
|
||||
def run(command: list[str]) -> str | Failure:
|
||||
try:
|
||||
|
||||
+14
-1
@@ -7,7 +7,7 @@ import unittest
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from forge.pulls import checks_passed, open_pull, POLL_SECONDS, required_checks, WAIT_SECONDS
|
||||
from forge.commands import forge_of, gitea
|
||||
from forge.commands import forge_of, gitea, url_beside
|
||||
|
||||
|
||||
class ForgeOf(unittest.TestCase):
|
||||
@@ -16,6 +16,19 @@ class ForgeOf(unittest.TestCase):
|
||||
self.assertEqual(forge_of("https://gitea.larvit.se/o/r.git"), "tea")
|
||||
|
||||
|
||||
class UrlBeside(unittest.TestCase):
|
||||
def test_names_a_repo_in_the_same_place_of_the_same_host_in_each_spelling_of_an_origin(self):
|
||||
self.assertEqual(url_beside("git@github.com:acme/billing.git", "ledger"), "git@github.com:acme/ledger")
|
||||
self.assertEqual(url_beside("https://gitea.larvit.se/acme/billing", "ledger"), "https://gitea.larvit.se/acme/ledger")
|
||||
self.assertEqual(url_beside("https://gitea.larvit.se/acme/billing.git/", "ledger"), "https://gitea.larvit.se/acme/ledger")
|
||||
self.assertEqual(url_beside("gitea:billing", "ledger"), "gitea:ledger", "a host with no owner")
|
||||
|
||||
def test_names_none_where_the_origin_has_no_place_to_put_a_repo_in(self):
|
||||
self.assertIsNone(url_beside("billing", "ledger"))
|
||||
self.assertIsNone(url_beside("", "ledger"))
|
||||
self.assertIsNone(url_beside("https://gitea.larvit.se", "ledger"))
|
||||
|
||||
|
||||
# An answer no call can read, longer than the 200 characters of it that a failure quotes.
|
||||
LONG = "[" + ", ".join(["1"] * 100) + "]"
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Replay loop-claude's judge on archived verdicts, to find the lowest rung that returns them: docs/decisions.md → `routing: on a model judge-replay covered`.
|
||||
|
||||
usage: judge-replay <model>
|
||||
usage: judge-replay <model> [--offline]
|
||||
|
||||
<model> is a project model on the routing ladder this setup ships, such as claude-opus-5-5. The tool replays the
|
||||
verdicts archived for the projects that ~/.claude/loop-claude-models.json runs on it: the latest
|
||||
@@ -36,8 +36,21 @@ Like the loop, it tries a judge three times running before it counts the reply a
|
||||
verdict whose project, transcript or archived judge run cannot be read replays as failed too. Exit 1, saying why, on
|
||||
bad arguments, an unreadable archive or models file, no archived verdict of the model's projects,
|
||||
or a usage limit.
|
||||
|
||||
With --offline it doesn't call a judge, so the runs, retries and usage limit above don't apply. It reads every
|
||||
archived verdict of the model's projects, not a sample. It counts how many of them each of three code
|
||||
deciders decides from the worker's final message, and how many archived interrupts it decides as another
|
||||
kind, which are its missed interrupts: a "?" rule, a decision table, and a classifier trained on the archive
|
||||
(judge_replay/offline.py). The rule and the table run on the whole archive; the table's rows were read from
|
||||
it, so its run isn't a held-out test. The classifier runs on held-out folds, each tested by a model trained on
|
||||
the other folds, and on the latest fifth of the verdicts, tested by a model trained on the earlier four
|
||||
fifths. The classifier trains 330 regressions in Python, about 12 minutes on 1,100 verdicts. The report is
|
||||
kept in replays/judge-replay-<model>-offline-<UTC stamp>/result.md. A verdict whose transcript cannot be read is skipped, and the report counts the skipped. Exit 1, saying why, on bad
|
||||
arguments, an unreadable archive or models file, no archived verdict of the model's projects, or none with a
|
||||
transcript that can be read.
|
||||
"""
|
||||
import pathlib
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from transcript_archive.location import archive_root, transcript_of
|
||||
@@ -49,6 +62,7 @@ from judge_replay.replay import Case, case_from, Kind, report, reproduced, sampl
|
||||
from transcript_archive.project_key import checkout_of
|
||||
from loop_effects.effects import Effects
|
||||
from failure import Failure
|
||||
from judge_replay.offline import final_message, LEARNERS, offline_report, RULES, Sample
|
||||
from rung_replay.search import first_reply, replayed
|
||||
from judge_replay.archived_prompt import grounds_of
|
||||
from transcript_archive.judge_run import JudgeRun
|
||||
@@ -61,7 +75,8 @@ from claude_code.transcript import read_transcript
|
||||
from loop_claude.verdicts import Verdict
|
||||
|
||||
TOOL = "judge-replay"
|
||||
USAGE = f"{TOOL} <model>, a model on the routing ladder"
|
||||
USAGE = f"{TOOL} <model> [--offline]; <model> is a model on the routing ladder"
|
||||
OFFLINE = "--offline"
|
||||
PER_KIND = 15
|
||||
# The effort settings.json gave every judge before loop-claude routed it; the archived verdicts came from it.
|
||||
REFERENCE_EFFORT = "xhigh"
|
||||
@@ -72,7 +87,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, eff
|
||||
ladder = read_ladder(SHIPPED)
|
||||
if isinstance(ladder, Failure):
|
||||
return failed(TOOL, ladder.reason)
|
||||
project = arguments_or_exit(TOOL, argv, __doc__, USAGE, lambda argv: model_argument(ladder, argv))
|
||||
project = arguments_or_exit(TOOL, argv, __doc__, USAGE, lambda argv: model_argument(ladder, [arg for arg in argv if arg != OFFLINE]))
|
||||
if isinstance(project, int):
|
||||
return project
|
||||
cases = archived_cases(project.rung.model, archive_root(home, env), home)
|
||||
@@ -80,6 +95,8 @@ def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, eff
|
||||
return failed(TOOL, cases.reason)
|
||||
if not cases:
|
||||
return failed(TOOL, f"no archived verdicts of projects on {project.rung.model} in {archive_root(home, env)}; run loop-claude on a project on that model first")
|
||||
if OFFLINE in argv:
|
||||
return offline(project.rung.model, cases, archive_root(home, env), effects)
|
||||
cases = sample(cases, PER_KIND)
|
||||
folder = replay_folder(archive_root(home, env), TOOL, argv, effects.now())
|
||||
effects = archiving(effects, folder)
|
||||
@@ -92,6 +109,22 @@ def main(argv: list[str], env: typing.Mapping[str, str], home: pathlib.Path, eff
|
||||
return 0
|
||||
|
||||
|
||||
def offline(model: str, cases: list[Case], root: pathlib.Path, effects: Effects) -> int:
|
||||
"""Replays the verdicts of cases offline, oldest first, and keeps the report in the archive as a judge replay does."""
|
||||
samples, skipped = [], 0
|
||||
for found in sorted(cases, key=lambda found: (found.at, found.root, str(found.transcript))):
|
||||
entries = read_transcript(str(found.transcript))
|
||||
if isinstance(entries, Failure):
|
||||
skipped += 1
|
||||
print(f"{TOOL}: skipped a verdict: {entries.reason}", file=sys.stderr)
|
||||
else:
|
||||
samples.append(Sample(found.archived_kind, final_message(entries)))
|
||||
if not samples:
|
||||
return failed(TOOL, "no archived verdict has a transcript that can be read")
|
||||
print_report(replay_folder(root, TOOL, [model, OFFLINE], effects.now()), offline_report(model, samples, RULES, LEARNERS, skipped=skipped))
|
||||
return 0
|
||||
|
||||
|
||||
def model_argument(ladder: Ladder, argv: list[str]) -> ProjectRung | None:
|
||||
"""The project's rung on ladder of the one model argv names; None for any other argv."""
|
||||
return ladder.project_rung(argv[0]) if len(argv) == 1 else None
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
"""Deciders of a verdict's kind that run offline on the worker's final message, replayed on the archive: docs/decisions.md → `routing: the offline classifier may decide finished, …`."""
|
||||
import collections
|
||||
import math
|
||||
import random
|
||||
import re
|
||||
import typing
|
||||
|
||||
from claude_code.entries import AssistantTurn, Entry, Text
|
||||
from transcript_archive.outcomes import FINISHED, INTERRUPTED, UNFINISHED
|
||||
from judge_replay.replay import Kind
|
||||
|
||||
|
||||
class Sample(typing.NamedTuple):
|
||||
"""What a decider reads of one archived verdict: the kind the archive gives it and the worker's final message."""
|
||||
kind: Kind
|
||||
text: str
|
||||
|
||||
|
||||
# A decider names a kind, or None to leave the verdict to the judge's model. A method builds one from the samples it may learn from.
|
||||
Decider = typing.Callable[[str], Kind | None]
|
||||
Method = typing.Callable[[typing.Sequence[Sample]], Decider]
|
||||
Counts = collections.Counter[tuple[Kind, Kind | None]]
|
||||
|
||||
FOLDS = 10
|
||||
INNER_FOLDS = 5
|
||||
# A word in fewer messages than this is left out of the classifier.
|
||||
MIN_MESSAGES = 3
|
||||
EPOCHS = 8
|
||||
# The classifier averages this many regressions, each shuffling the samples in its own order: one shuffle alone moved the missed interrupts of a run between 0 and 2.
|
||||
SEEDS = 5
|
||||
RATE = 0.05
|
||||
DECAY = 1e-3
|
||||
# The latest 1/LATEST of the archive is the second test: the verdicts a decider would meet next.
|
||||
LATEST = 5
|
||||
|
||||
# The first row whose pattern matches the final message decides it; a message no row matches is left to the judge. The rows come from reading the archive's interrupts, so a run of the table on the whole archive is not a held-out test.
|
||||
DECISION_TABLE = (
|
||||
(INTERRUPTED, re.compile(r"(?i)\?|recommend|your call|needs? you|tell me|you answer|please|unmerged|not merged|do you|should i|i need|need your|you say|decisions? for|stays until")),
|
||||
(FINISHED, re.compile(r"(?i)\bmerged\b(?!.*ready to cut)")),
|
||||
)
|
||||
|
||||
|
||||
def final_message(entries: list[Entry]) -> str:
|
||||
"""The text of the worker's last turn that has any, "" where it wrote none."""
|
||||
for entry in reversed(entries):
|
||||
if isinstance(entry, AssistantTurn):
|
||||
text = "\n".join(part.text for part in entry.parts if isinstance(part, Text) and part.text.strip())
|
||||
if text:
|
||||
return text
|
||||
return ""
|
||||
|
||||
|
||||
def question_mark(samples: typing.Sequence[Sample]) -> Decider:
|
||||
"""The one rule: an interrupt where the message holds a "?", finished where it holds none."""
|
||||
return lambda text: INTERRUPTED if "?" in text else FINISHED
|
||||
|
||||
|
||||
def decision_table(samples: typing.Sequence[Sample]) -> Decider:
|
||||
return lambda text: next((kind for kind, pattern in DECISION_TABLE if pattern.search(text)), None)
|
||||
|
||||
|
||||
def words_of(text: str) -> frozenset[str]:
|
||||
"""Each word and each pair of neighbouring words of the message, and its question marks."""
|
||||
words = re.findall(r"[a-z0-9']+|\?", text.lower())
|
||||
return frozenset(words) | {f"{first} {second}" for first, second in zip(words, words[1:])}
|
||||
|
||||
|
||||
def logistic(samples: typing.Sequence[Sample], seed: int) -> typing.Callable[[str], float]:
|
||||
"""The chance that a message is an interrupt, from a logistic regression over its words that the samples train, shuffled in the order seed gives."""
|
||||
seen = collections.Counter(word for found in samples for word in words_of(found.text))
|
||||
vocabulary = {word for word, count in seen.items() if count >= MIN_MESSAGES}
|
||||
data = [(words_of(found.text) & vocabulary, 1.0 if found.kind == INTERRUPTED else 0.0) for found in samples]
|
||||
weights: dict[str, float] = collections.defaultdict(float)
|
||||
bias = 0.0
|
||||
shuffler = random.Random(seed)
|
||||
|
||||
def chance(words: typing.Iterable[str]) -> float:
|
||||
return 1 / (1 + math.exp(-max(-30.0, min(30.0, bias + sum(weights[word] for word in words)))))
|
||||
|
||||
for _ in range(EPOCHS):
|
||||
shuffler.shuffle(data)
|
||||
for words, interrupt in data:
|
||||
error = chance(words) - interrupt
|
||||
bias -= RATE * error
|
||||
for word in words:
|
||||
weights[word] -= RATE * (error + DECAY * weights[word])
|
||||
return lambda text: chance(words_of(text) & vocabulary)
|
||||
|
||||
|
||||
def averaged(samples: typing.Sequence[Sample]) -> typing.Callable[[str], float]:
|
||||
"""The mean chance of SEEDS regressions that differ only in the order they shuffle the samples."""
|
||||
regressions = [logistic(samples, seed) for seed in range(SEEDS)]
|
||||
return lambda text: sum(regression(text) for regression in regressions) / SEEDS
|
||||
|
||||
|
||||
def classifier(samples: typing.Sequence[Sample]) -> Decider:
|
||||
"""Finished where the chance of an interrupt is below one half and below that of every interrupt among the samples; interrupt where it is at least one half and above that of every other kind; otherwise the judge's model decides.
|
||||
|
||||
A sample's chance is the one a model trained on the other folds of the samples gives it, so the bounds don't rest on how well the model fits what it trained on. A bound is where the samples held no miss; that doesn't promise the next verdict holds none."""
|
||||
interrupts = [found for found in samples if found.kind == INTERRUPTED]
|
||||
if not interrupts or len(interrupts) == len(samples):
|
||||
return lambda text: None
|
||||
held_out = [(tested.kind, chance) for trained, tests in folds_of(samples, INNER_FOLDS) for chance, tested in zip(map(averaged(trained), (found.text for found in tests)), tests)]
|
||||
below = min(chance for kind, chance in held_out if kind == INTERRUPTED)
|
||||
above = max(chance for kind, chance in held_out if kind != INTERRUPTED)
|
||||
chance = averaged(samples)
|
||||
|
||||
def decide(text: str) -> Kind | None:
|
||||
found = chance(text)
|
||||
if found < below and found < 0.5:
|
||||
return FINISHED
|
||||
return INTERRUPTED if found > above and found >= 0.5 else None
|
||||
return decide
|
||||
|
||||
|
||||
# Rules are fixed, and run on the whole archive; a learner trains on samples, and runs on samples it didn't train on.
|
||||
RULES: dict[str, Method] = {'"?" rule': question_mark, "decision table": decision_table}
|
||||
LEARNERS: dict[str, Method] = {"classifier": classifier}
|
||||
|
||||
|
||||
def folds_of(samples: typing.Sequence[Sample], count: int) -> list[tuple[list[Sample], list[Sample]]]:
|
||||
"""count folds of the samples, dealt in turn. Each fold is the samples to learn from, which are all those its test leaves out, and the samples to test on."""
|
||||
return [([found for place, found in enumerate(samples) if place % count != fold], [found for place, found in enumerate(samples) if place % count == fold]) for fold in range(count)]
|
||||
|
||||
|
||||
def evaluate(method: Method, trained: typing.Sequence[Sample], tested: typing.Sequence[Sample]) -> Counts:
|
||||
"""How often the method, learning from trained, decided each kind for each archived kind of tested; None where it left the verdict to the judge."""
|
||||
decide = method(trained)
|
||||
return collections.Counter((found.kind, decide(found.text)) for found in tested)
|
||||
|
||||
|
||||
def plain(kind: Kind) -> str:
|
||||
return kind.removeprefix("Verdict: ")
|
||||
|
||||
|
||||
def offline_report(model: str, samples: typing.Sequence[Sample], rules: typing.Mapping[str, Method], learners: typing.Mapping[str, Method], folds: int = FOLDS, skipped: int = 0) -> str:
|
||||
"""samples are in the order the verdicts were judged, oldest first, so the latest fifth is the newest verdicts."""
|
||||
kinds = collections.Counter(found.kind for found in samples)
|
||||
lines = [f"Replayed {len(samples)} archived verdicts of {model} projects offline, without sending anything to an outside service. The archive gives them as {', '.join(f'{count} {plain(kind)}' for kind, count in sorted(kinds.items(), key=lambda each: plain(each[0])))}."]
|
||||
if skipped:
|
||||
lines.append(f"Skipped {skipped} verdicts whose transcript could not be read.")
|
||||
for name, rule in rules.items():
|
||||
found = evaluate(rule, [], samples)
|
||||
lines += [result_line(f"{name}, on the whole archive", found), *decides_lines(f"{name}, on the whole archive", found)]
|
||||
split = len(samples) - max(1, len(samples) // LATEST)
|
||||
for name, learner in learners.items():
|
||||
held_out = sum((evaluate(learner, trained, tested) for trained, tested in folds_of(samples, folds)), Counts())
|
||||
latest = evaluate(learner, samples[:split], samples[split:])
|
||||
lines += [result_line(f"{name}, on held-out folds", held_out), result_line(f"{name}, on the latest fifth after learning from the rest", latest), *decides_lines(f"{name}, on both tests", held_out + latest)]
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def result_line(how: str, found: Counts) -> str:
|
||||
decided = collections.Counter({kind: sum(count for (_, said), count in found.items() if said == kind) for kind in {said for _, said in found if said}})
|
||||
missed = sum(count for (archived, said), count in found.items() if archived == INTERRUPTED and said not in (None, INTERRUPTED))
|
||||
parts = [f"{plain(kind)} {count}{wrong_kinds(kind, found)}" for kind, count in sorted(decided.items(), key=lambda each: plain(each[0]))]
|
||||
return f"{how}: decided {sum(decided.values())} of {sum(found.values())}{': ' + ', '.join(parts) if parts else ''}. Missed interrupts: {missed}."
|
||||
|
||||
|
||||
def wrong_kinds(decided: Kind, found: Counts) -> str:
|
||||
"""The kinds the archive gives the verdicts that were decided as decided, where the archive gives another kind than decided."""
|
||||
wrong = sorted(((plain(archived), count) for (archived, said), count in found.items() if said == decided and archived != decided), key=lambda each: each[0])
|
||||
return f" (of these, archived {', '.join(f'{kind} {count}' for kind, count in wrong)})" if wrong else ""
|
||||
|
||||
|
||||
def decides_lines(how: str, found: Counts) -> list[str]:
|
||||
"""For finished and for unfinished that the decider decided at all: whether it decided no archived interrupt as that kind."""
|
||||
lines = []
|
||||
for kind in (FINISHED, UNFINISHED):
|
||||
decided = sum(count for (_, said), count in found.items() if said == kind)
|
||||
if decided and not found[(INTERRUPTED, kind)]:
|
||||
lines.append(f"{how}, decides {plain(kind)} with no missed interrupt.")
|
||||
elif decided:
|
||||
lines.append(f"{how}, does not decide {plain(kind)} with no missed interrupt: it decided {found[(INTERRUPTED, kind)]} archived interrupt(s) as {plain(kind)}.")
|
||||
return lines
|
||||
@@ -175,6 +175,44 @@ class Main(unittest.TestCase):
|
||||
# The replay shows the judge the whole transcript, with no step-up marked in it.
|
||||
self.assertNotIn("[step]", self.prompts[0])
|
||||
|
||||
def test_offline_replays_every_verdict_through_the_code_deciders_without_calling_the_judge(self):
|
||||
code, out, err = self.run_main(["claude-opus-5-5", "--offline"], unused)
|
||||
self.assertEqual(code, 0, err)
|
||||
self.assertEqual(self.calls, [])
|
||||
self.assertIn("Replayed 2 archived verdicts of claude-opus-5-5 projects offline, without sending anything to an outside service. The archive gives them as 1 finished, 1 interrupt.", out)
|
||||
self.assertIn("\"?\" rule, on the whole archive: decided 2 of 2: finished 2 (of these, archived interrupt 1). Missed interrupts: 1.", out)
|
||||
folder = self.home / ".claude" / "loop-claude-transcripts" / "replays" / "judge-replay-claude-opus-5-5-offline-20260921T141320Z"
|
||||
self.assertEqual((folder / "result.md").read_text(encoding="utf-8"), out)
|
||||
|
||||
def test_offline_exits_1_where_no_transcript_can_be_read(self):
|
||||
for path in (self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1").glob("*.jsonl"):
|
||||
path.unlink()
|
||||
code, out, err = self.run_main(["claude-opus-5-5", "--offline"], unused)
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertEqual(err.splitlines()[-1], "judge-replay: no archived verdict has a transcript that can be read")
|
||||
|
||||
def test_offline_skips_each_verdict_whose_transcript_cannot_be_read_and_says_how_many_on_stderr_and_in_the_report(self):
|
||||
kept = self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1"
|
||||
(kept / "t2.outcomes.md").write_text(f"Task: Do it.\n\n2026-10-14T10:00:02Z {FINISHED}\n\nEvidence:\n> e\n")
|
||||
(kept / "t0.jsonl").unlink()
|
||||
code, out, err = self.run_main(["claude-opus-5-5", "--offline"], unused)
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(len(err.splitlines()), 2)
|
||||
self.assertTrue(all(line.startswith("judge-replay: skipped a verdict: could not read the transcript ") for line in err.splitlines()))
|
||||
self.assertIn("Replayed 1 archived verdicts of claude-opus-5-5 projects offline", out)
|
||||
self.assertIn("Skipped 2 verdicts whose transcript could not be read.", out)
|
||||
|
||||
def test_offline_keeps_its_report_in_the_same_folder_wherever_the_flag_stands(self):
|
||||
code, out, err = self.run_main(["--offline", "claude-opus-5-5"], unused)
|
||||
self.assertEqual(code, 0, err)
|
||||
folder = self.home / ".claude" / "loop-claude-transcripts" / "replays" / "judge-replay-claude-opus-5-5-offline-20260921T141320Z"
|
||||
self.assertEqual((folder / "result.md").read_text(encoding="utf-8"), out)
|
||||
|
||||
def test_offline_says_the_usage_on_arguments_that_name_no_model(self):
|
||||
code, out, err = self.run_main(["--offline"], unused)
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertEqual(err, "judge-replay: cannot read the arguments --offline; usage: judge-replay <model> [--offline]; <model> is a model on the routing ladder\n")
|
||||
|
||||
def test_keeps_each_run_and_the_printed_report_in_one_folder_of_the_archive_beside_the_projects(self):
|
||||
code, out, err = self.run_main(["claude-opus-5-5"], lambda rung, interrupted: self.verdict(interrupted, f"said {INTERRUPTED if interrupted else FINISHED}"))
|
||||
self.assertEqual(code, 0, err)
|
||||
@@ -272,7 +310,7 @@ class Main(unittest.TestCase):
|
||||
|
||||
def test_prints_the_help(self):
|
||||
code, out, _ = self.run_main(["-h"], unused)
|
||||
self.assertEqual((code, out.splitlines()[2]), (0, "usage: judge-replay <model>"))
|
||||
self.assertEqual((code, out.splitlines()[2]), (0, "usage: judge-replay <model> [--offline]"))
|
||||
|
||||
def test_the_launcher_prints_the_help(self):
|
||||
ran = subprocess.run([sys.executable, str(TOOL), "--help"], capture_output=True, text=True)
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
import collections
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from transcript_archive.outcomes import ANSWERED, FINISHED, INTERRUPTED, UNFINISHED
|
||||
from claude_code.entries import AssistantTurn, HumanTurn, Text, ToolCall
|
||||
from judge_replay.offline import averaged, classifier, decision_table, evaluate, final_message, folds_of, LEARNERS, logistic, offline_report, question_mark, RULES, Sample, words_of
|
||||
|
||||
|
||||
def turn(*parts):
|
||||
return AssistantTurn(parts=parts)
|
||||
|
||||
|
||||
def sample(kind, text):
|
||||
return Sample(kind, text)
|
||||
|
||||
|
||||
class FinalMessage(unittest.TestCase):
|
||||
def test_is_the_text_of_the_workers_last_turn_that_has_any(self):
|
||||
entries = [turn(Text("first")), HumanTurn(text="ok"), turn(Text("Done. Merged."), ToolCall("1", "Bash", {})), turn(ToolCall("2", "Bash", {}))]
|
||||
self.assertEqual(final_message(entries), "Done. Merged.")
|
||||
|
||||
def test_joins_the_texts_of_one_turn_on_separate_lines_and_leaves_out_blank_ones(self):
|
||||
self.assertEqual(final_message([turn(Text("Merged."), Text(" "), Text("Which one?"))]), "Merged.\nWhich one?")
|
||||
|
||||
def test_is_empty_where_the_worker_wrote_no_text(self):
|
||||
self.assertEqual(final_message([HumanTurn(text="hi"), turn(ToolCall("1", "Bash", {}))]), "")
|
||||
|
||||
|
||||
class WordsOf(unittest.TestCase):
|
||||
def test_is_each_lowercase_word_each_pair_of_neighbours_and_each_question_mark(self):
|
||||
self.assertEqual(words_of("Which one? Yes"), {"which", "one", "?", "yes", "which one", "one ?", "? yes"})
|
||||
|
||||
|
||||
class QuestionMark(unittest.TestCase):
|
||||
def test_decides_interrupt_on_a_question_mark_and_finished_on_none(self):
|
||||
decide = question_mark([])
|
||||
self.assertEqual(decide("Which one?"), INTERRUPTED)
|
||||
self.assertEqual(decide("Merged."), FINISHED)
|
||||
|
||||
|
||||
class DecisionTable(unittest.TestCase):
|
||||
def test_reads_the_workers_asking_for_the_humans_answer_as_an_interrupt(self):
|
||||
decide = decision_table([])
|
||||
for text in ("My recommendation is option 1.", "Your call: merge now or wait.", "The PR stays unmerged.", "Tell me which."):
|
||||
with self.subTest(text=text):
|
||||
self.assertEqual(decide(text), INTERRUPTED)
|
||||
|
||||
def test_reads_a_merge_with_no_ask_and_no_release_to_cut_as_finished(self):
|
||||
decide = decision_table([])
|
||||
self.assertEqual(decide("PR 12 merged and the worktree is removed."), FINISHED)
|
||||
self.assertIsNone(decide("PR 12 merged. Release 0.3.0 is ready to cut."))
|
||||
|
||||
def test_leaves_any_other_message_to_the_judge(self):
|
||||
self.assertIsNone(decision_table([])("I stopped at the gate."))
|
||||
|
||||
|
||||
class Logistic(unittest.TestCase):
|
||||
def test_gives_a_message_with_no_learned_word_about_the_share_of_interrupts(self):
|
||||
chance = logistic([sample(INTERRUPTED, "")] * 10 + [sample(FINISHED, "")] * 30, 0)
|
||||
self.assertAlmostEqual(chance(""), 0.25, delta=0.05)
|
||||
|
||||
def test_learns_a_word_found_in_as_few_messages_as_the_minimum(self):
|
||||
chance = logistic([sample(INTERRUPTED, "ask")] * 3 + [sample(FINISHED, "")] * 3, 0)
|
||||
self.assertGreater(chance("ask"), chance(""))
|
||||
|
||||
def test_averaged_is_the_mean_of_the_regressions_that_shuffle_in_each_seed_order(self):
|
||||
samples = [sample(INTERRUPTED if n % 3 else FINISHED, f"word{n % 4} ask" if n % 3 else f"word{n % 4}") for n in range(30)]
|
||||
mean = sum(logistic(samples, seed)("ask") for seed in range(5)) / 5
|
||||
self.assertAlmostEqual(averaged(samples)("ask"), mean)
|
||||
|
||||
|
||||
class Classifier(unittest.TestCase):
|
||||
def trained(self, extra=()):
|
||||
asking = [sample(INTERRUPTED, f"I recommend option {n}; tell me which of the options you want") for n in range(12)]
|
||||
done = [sample(FINISHED, f"merged pull request {n} and removed the worktree, no findings left") for n in range(12)]
|
||||
return classifier([*asking, *done, *extra])
|
||||
|
||||
def test_decides_a_message_like_each_kind_it_learned(self):
|
||||
decide = self.trained()
|
||||
self.assertEqual(decide("I recommend option 3; tell me which you want"), INTERRUPTED)
|
||||
self.assertEqual(decide("merged pull request 99 and removed the worktree, no findings left"), FINISHED)
|
||||
|
||||
def test_decides_finished_on_a_message_of_words_only_finished_verdicts_held(self):
|
||||
self.assertEqual(self.trained()("merged pull request"), FINISHED)
|
||||
|
||||
def test_abstains_where_it_learned_no_interrupt_or_no_other_kind(self):
|
||||
self.assertIsNone(classifier([sample(FINISHED, "merged")])("merged"))
|
||||
self.assertIsNone(classifier([sample(INTERRUPTED, "which?")])("which?"))
|
||||
self.assertIsNone(classifier([])("anything"))
|
||||
|
||||
def test_decides_no_finished_where_an_interrupt_it_learned_looks_like_one(self):
|
||||
lookalike = "merged pull request 7 and removed the worktree, no findings left"
|
||||
decide = self.trained([sample(INTERRUPTED, lookalike)])
|
||||
self.assertNotEqual(decide(lookalike), FINISHED)
|
||||
|
||||
|
||||
def test_abstains_where_a_lookalike_of_each_kind_sits_in_the_samples_of_the_other(self):
|
||||
decide = self.trained([sample(INTERRUPTED, "merged pull request 7 and removed the worktree, no findings left"), sample(FINISHED, "I recommend option 7; tell me which of the options you want")])
|
||||
for text in ("merged pull request", "I recommend option", "zzz qqq"):
|
||||
with self.subTest(text=text):
|
||||
self.assertIsNone(decide(text))
|
||||
|
||||
|
||||
class Folds(unittest.TestCase):
|
||||
def test_deals_samples_to_folds_in_turn_so_each_is_tested_once(self):
|
||||
samples = [sample(FINISHED, str(n)) for n in range(7)]
|
||||
dealt = folds_of(samples, 3)
|
||||
self.assertEqual([[found.text for found in tested] for _, tested in dealt], [["0", "3", "6"], ["1", "4"], ["2", "5"]])
|
||||
self.assertEqual([found.text for found in dealt[1][0]], ["0", "2", "3", "5", "6"])
|
||||
|
||||
|
||||
class Evaluate(unittest.TestCase):
|
||||
def test_counts_each_archived_kind_by_what_the_method_decided_or_abstained(self):
|
||||
tested = [sample(FINISHED, "merged"), sample(INTERRUPTED, "merged"), sample(UNFINISHED, "stopped"), sample(ANSWERED, "Which?")]
|
||||
found = evaluate(decision_table, [], tested)
|
||||
self.assertEqual(found, collections.Counter({(FINISHED, FINISHED): 1, (INTERRUPTED, FINISHED): 1, (UNFINISHED, None): 1, (ANSWERED, INTERRUPTED): 1}))
|
||||
|
||||
|
||||
class Report(unittest.TestCase):
|
||||
def samples(self):
|
||||
return [sample(FINISHED, "Merged."), sample(FINISHED, "Merged. Release 1.0.0 is ready to cut."), sample(INTERRUPTED, "I recommend merging."), sample(INTERRUPTED, "Merged.")]
|
||||
|
||||
def test_counts_what_a_rule_decided_on_the_whole_archive_and_missed_by_kind(self):
|
||||
text = offline_report("claude-opus-5-5", self.samples(), {"decision table": decision_table}, {})
|
||||
self.assertIn("Replayed 4 archived verdicts of claude-opus-5-5 projects offline, without sending anything to an outside service. The archive gives them as 2 finished, 2 interrupt.", text)
|
||||
self.assertIn("decision table, on the whole archive: decided 3 of 4: finished 2 (of these, archived interrupt 1), interrupt 1. Missed interrupts: 1.", text)
|
||||
self.assertIn("decision table, on the whole archive, does not decide finished with no missed interrupt: it decided 1 archived interrupt(s) as finished.", text)
|
||||
|
||||
def test_says_a_method_that_decided_a_kind_and_missed_no_interrupt_decides_that_kind(self):
|
||||
text = offline_report("claude-opus-5-5", [sample(FINISHED, "Merged."), sample(INTERRUPTED, "I recommend merging.")], {"decision table": decision_table}, {})
|
||||
self.assertIn("Missed interrupts: 0.", text)
|
||||
self.assertIn("decision table, on the whole archive, decides finished with no missed interrupt.", text)
|
||||
|
||||
def test_runs_a_learner_once_for_each_fold_and_once_on_the_latest_fifth_and_a_rule_only_on_the_whole_archive(self):
|
||||
samples = [sample(INTERRUPTED if n % 2 else FINISHED, "I recommend it" if n % 2 else "merged it") for n in range(20)]
|
||||
text = offline_report("m", samples, {"rule": question_mark}, {"classifier": classifier}, folds=4)
|
||||
self.assertIn("classifier, on held-out folds: decided", text)
|
||||
self.assertIn("classifier, on the latest fifth after learning from the rest: decided", text)
|
||||
self.assertIn("rule, on the whole archive: decided 20 of 20", text)
|
||||
self.assertNotIn("rule, on held-out", text)
|
||||
self.assertNotIn("rule, on the latest fifth", text)
|
||||
|
||||
def test_words_the_whole_report_line_by_line_with_the_wrong_kinds_by_name(self):
|
||||
answers = {"f": FINISHED, "i1": FINISHED, "i2": FINISHED, "u": FINISHED, "q": None}
|
||||
samples = [sample(FINISHED, "f"), sample(INTERRUPTED, "i1"), sample(INTERRUPTED, "i2"), sample(UNFINISHED, "u"), sample(ANSWERED, "q")]
|
||||
text = offline_report("m", samples, {"rule": lambda trained: lambda said: answers[said]}, {})
|
||||
self.assertEqual(text.splitlines(), [
|
||||
"Replayed 5 archived verdicts of m projects offline, without sending anything to an outside service. The archive gives them as 1 finished, 2 interrupt, 1 quoted answer, 1 unfinished.",
|
||||
"rule, on the whole archive: decided 4 of 5: finished 4 (of these, archived interrupt 2, unfinished 1). Missed interrupts: 2.",
|
||||
"rule, on the whole archive, does not decide finished with no missed interrupt: it decided 2 archived interrupt(s) as finished.",
|
||||
])
|
||||
|
||||
def test_says_a_method_that_decided_nothing_decided_0_and_names_no_kind(self):
|
||||
text = offline_report("m", self.samples(), {"rule": lambda trained: lambda said: None}, {})
|
||||
self.assertEqual(text.splitlines()[1:], ["rule, on the whole archive: decided 0 of 4. Missed interrupts: 0."])
|
||||
|
||||
def test_tests_the_latest_fifth_of_the_samples_after_learning_from_the_earlier_ones(self):
|
||||
trained = []
|
||||
|
||||
def learner(samples):
|
||||
trained.append(len(samples))
|
||||
return lambda said: FINISHED
|
||||
|
||||
text = offline_report("m", [sample(FINISHED, str(n)) for n in range(7)], {}, {"learner": learner}, folds=2)
|
||||
self.assertEqual(trained, [3, 4, 6])
|
||||
self.assertEqual(text.splitlines()[-2:], ["learner, on the latest fifth after learning from the rest: decided 1 of 1: finished 1. Missed interrupts: 0.", "learner, on both tests, decides finished with no missed interrupt."])
|
||||
|
||||
def test_says_a_learner_that_missed_an_interrupt_on_either_test_does_not_decide_the_kind(self):
|
||||
def learner(samples):
|
||||
return lambda said: FINISHED if said == "miss" else None
|
||||
|
||||
samples = [sample(FINISHED, "ok")] * 4 + [sample(INTERRUPTED, "miss")]
|
||||
text = offline_report("m", samples, {}, {"learner": learner}, folds=2)
|
||||
self.assertEqual(text.splitlines()[-1], "learner, on both tests, does not decide finished with no missed interrupt: it decided 2 archived interrupt(s) as finished.")
|
||||
|
||||
def test_counts_the_verdicts_it_skipped_only_where_it_skipped_some(self):
|
||||
self.assertNotIn("Skipped", offline_report("m", self.samples(), {}, {}))
|
||||
self.assertIn("Skipped 2 verdicts whose transcript could not be read.", offline_report("m", self.samples(), {}, {}, skipped=2))
|
||||
|
||||
def test_the_shipped_methods_are_the_question_mark_rule_the_table_and_the_classifier(self):
|
||||
self.assertEqual(list(RULES), ['"?" rule', "decision table"])
|
||||
self.assertEqual(list(LEARNERS), ["classifier"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
|
||||
from markdown_section import main
|
||||
|
||||
sys.exit(main.main(sys.argv[1:]))
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Print one section of a markdown file, picked by a fragment of its heading.
|
||||
|
||||
usage: markdown-section <file> <fragment>
|
||||
|
||||
The section starts at the heading whose text holds the fragment, in any case, and ends before the
|
||||
next heading of the same or a higher level, so it holds its sub-sections. A heading's text is what
|
||||
follows its `#`s. A line inside a fenced code block is never a heading.
|
||||
|
||||
Where no heading holds the fragment, it fails saying so. Where several headings do, it fails
|
||||
listing each with its line number and prints no section: pass more of the heading you want.
|
||||
A heading whose text is the fragment, in any case, is the one picked even where longer headings
|
||||
hold it too.
|
||||
|
||||
To read a record kept outside the checkout, pass the file `project-record open <record>` prints.
|
||||
|
||||
Exit 2 on bad arguments, and 1, saying why, on any other failure.
|
||||
"""
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from failure import Failure
|
||||
from text_files.markdown_text import fenced_lines, HEADING
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
if argv[:1] in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
if len(argv) != 2 or not argv[1].strip():
|
||||
print("usage: markdown-section <file> <fragment>", file=sys.stderr)
|
||||
return 2
|
||||
try:
|
||||
text = pathlib.Path(argv[0]).read_text()
|
||||
except (OSError, UnicodeDecodeError) as error:
|
||||
print(f"markdown-section: {error}", file=sys.stderr)
|
||||
return 1
|
||||
found = section(text, argv[1])
|
||||
if isinstance(found, Failure):
|
||||
print(f"markdown-section: {found.reason}", file=sys.stderr)
|
||||
return 1
|
||||
print(found, end="")
|
||||
return 0
|
||||
|
||||
|
||||
def headings(text: str) -> list[tuple[int, int, str]]:
|
||||
"""Each heading of text outside a fenced code block, as its line's index, its level and its text."""
|
||||
found = []
|
||||
for index, (line, fenced) in enumerate(fenced_lines(text)):
|
||||
if not fenced and (heading := HEADING.match(line)):
|
||||
found.append((index, len(heading.group(1)), heading.group(2)))
|
||||
return found
|
||||
|
||||
|
||||
def section(text: str, fragment: str) -> str | Failure:
|
||||
"""The section of text under the heading that fragment picks, or a Failure where no heading holds it or several do."""
|
||||
found = headings(text)
|
||||
wanted = fragment.strip().lower()
|
||||
matching = [heading for heading in found if wanted in heading[2].lower()]
|
||||
matching = [heading for heading in matching if heading[2].lower() == wanted] or matching
|
||||
if not matching:
|
||||
return Failure(f"no heading holds '{fragment}'")
|
||||
lines = text.splitlines()
|
||||
if len(matching) > 1:
|
||||
listed = "\n".join(f"{index + 1}: {lines[index]}" for index, _, _ in matching)
|
||||
return Failure(f"{len(matching)} headings hold '{fragment}', so use more of one:\n{listed}")
|
||||
start, level, _ = matching[0]
|
||||
end = next((index for index, deeper, _ in found if index > start and deeper <= level), None)
|
||||
return "\n".join(lines[start:end]).rstrip("\n") + "\n"
|
||||
@@ -0,0 +1,138 @@
|
||||
import contextlib
|
||||
import io
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from failure import Failure
|
||||
from markdown_section import main
|
||||
|
||||
TOOL = pathlib.Path(__file__).resolve().parents[1] / "markdown-section"
|
||||
|
||||
DOCUMENT = """# Title
|
||||
|
||||
Intro.
|
||||
|
||||
## Alpha
|
||||
|
||||
Alpha text.
|
||||
|
||||
### Alpha detail
|
||||
|
||||
Detail text.
|
||||
|
||||
## Beta Gamma
|
||||
|
||||
Beta text.
|
||||
|
||||
```
|
||||
## Not a heading
|
||||
```
|
||||
|
||||
## Delta
|
||||
|
||||
Delta text.
|
||||
"""
|
||||
|
||||
|
||||
class Section(unittest.TestCase):
|
||||
def test_a_section_runs_to_the_next_heading_of_its_level_or_a_higher_one(self):
|
||||
cases = {
|
||||
"a sub-section stops at a higher level heading": ("alpha detail", "### Alpha detail\n\nDetail text.\n"),
|
||||
"the text under a heading isn't a heading": ("alpha text", Failure("no heading holds 'alpha text'")),
|
||||
"the last section runs to the end of the file": ("delta", "## Delta\n\nDelta text.\n"),
|
||||
"a level one heading holds the rest of the document": ("title", DOCUMENT),
|
||||
"a fenced line that looks like a heading doesn't end it": ("beta", "## Beta Gamma\n\nBeta text.\n\n```\n## Not a heading\n```\n"),
|
||||
}
|
||||
for name, (fragment, expected) in cases.items():
|
||||
with self.subTest(name):
|
||||
self.assertEqual(main.section(DOCUMENT, fragment), expected)
|
||||
|
||||
def test_the_fragment_matches_any_part_of_the_heading_text_in_any_case(self):
|
||||
for fragment in ("gamma", "BETA g", " beta gamma "):
|
||||
with self.subTest(fragment):
|
||||
self.assertEqual(str(main.section(DOCUMENT, fragment)).splitlines()[0], "## Beta Gamma")
|
||||
self.assertEqual(main.section(DOCUMENT, "## beta"), Failure("no heading holds '## beta'"))
|
||||
|
||||
def test_a_heading_whose_text_is_the_fragment_wins_over_longer_ones(self):
|
||||
self.assertEqual(main.section(DOCUMENT, "Alpha"), "## Alpha\n\nAlpha text.\n\n### Alpha detail\n\nDetail text.\n")
|
||||
self.assertEqual(main.section("## Plan\n\na\n\n## Plan b\n", "PLAN"), "## Plan\n\na\n")
|
||||
|
||||
def test_several_matching_headings_fail_listing_each_with_its_line(self):
|
||||
listed = main.section("## Plan one\n\ntext\n\n## Plan two\n", "plan")
|
||||
self.assertEqual(listed, Failure("2 headings hold 'plan', so use more of one:\n1: ## Plan one\n5: ## Plan two"))
|
||||
|
||||
def test_what_counts_as_a_heading_or_a_fence(self):
|
||||
cases = {
|
||||
"a hash with no space after it is no heading": ("#hashtag\n\n## Real\n", "hashtag", Failure("no heading holds 'hashtag'")),
|
||||
"a fence with an info string closes on a bare fence": ("```python\n## In\n```\n## Out\n\ntext\n", "out", "## Out\n\ntext\n"),
|
||||
"a fence of four backticks holds a line of three": ("````\n```\n## In\n````\n## Out\n", "out", "## Out\n"),
|
||||
"a tilde fence holds a heading": ("~~~\n## In\n~~~\n## Out\n", "in", Failure("no heading holds 'in'")),
|
||||
"an indented fence holds a heading": (" ```\n## In\n ```\n", "in", Failure("no heading holds 'in'")),
|
||||
"an unclosed fence holds the rest": ("## A\n\n```\n## In\n", "a", "## A\n\n```\n## In\n"),
|
||||
}
|
||||
for name, (text, fragment, expected) in cases.items():
|
||||
with self.subTest(name):
|
||||
self.assertEqual(main.section(text, fragment), expected)
|
||||
|
||||
def test_only_blank_lines_are_cut_from_the_end_of_a_section(self):
|
||||
self.assertEqual(main.section("## A\n\nends in X\n\n\n## B\n", "a"), "## A\n\nends in X\n")
|
||||
self.assertEqual(main.section("## A\n\ncode \n\n", "a"), "## A\n\ncode \n")
|
||||
|
||||
def test_no_heading_is_a_failure(self):
|
||||
self.assertEqual(main.section(DOCUMENT, "zeta"), Failure("no heading holds 'zeta'"))
|
||||
|
||||
|
||||
class Main(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "notes.md"
|
||||
self.file.write_text(DOCUMENT)
|
||||
|
||||
def run_main(self, *args: str) -> tuple[int, str, str]:
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.main(list(args))
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def test_prints_the_section(self):
|
||||
self.assertEqual(self.run_main(str(self.file), "delta"), (0, "## Delta\n\nDelta text.\n", ""))
|
||||
|
||||
def test_a_failure_is_printed_to_stderr_with_exit_1(self):
|
||||
self.assertEqual(self.run_main(str(self.file), "zeta"), (1, "", "markdown-section: no heading holds 'zeta'\n"))
|
||||
|
||||
def test_a_file_that_cant_be_read_exits_1_naming_it(self):
|
||||
self.file.write_bytes(b"\xff\xfe## A\n")
|
||||
missing = self.file.with_name("missing.md")
|
||||
for unreadable, reason in ((missing, "missing.md"), (self.file, "decode")):
|
||||
with self.subTest(reason):
|
||||
code, out, err = self.run_main(str(unreadable), "a")
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertRegex(err, rf"^markdown-section: .*{reason}")
|
||||
|
||||
def test_wrong_arguments_exit_2_with_the_usage(self):
|
||||
for args in ((), (str(self.file),), (str(self.file), "a", "b"), (str(self.file), " ")):
|
||||
with self.subTest(args):
|
||||
self.assertEqual(self.run_main(*args), (2, "", "usage: markdown-section <file> <fragment>\n"))
|
||||
|
||||
def test_help_prints_the_usage_and_exits_0_whatever_follows_it(self):
|
||||
for args in (("-h",), ("--help",), ("--help", "x")):
|
||||
with self.subTest(args):
|
||||
code, out, err = self.run_main(*args)
|
||||
self.assertEqual((code, err), (0, ""))
|
||||
self.assertTrue(out.startswith("Print one section of a markdown file"))
|
||||
|
||||
|
||||
class Launcher(unittest.TestCase):
|
||||
def test_the_launcher_runs_the_tool(self):
|
||||
file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "notes.md"
|
||||
file.write_text(DOCUMENT)
|
||||
ran = subprocess.run([sys.executable, str(TOOL), str(file), "delta"], capture_output=True, text=True)
|
||||
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, "## Delta\n\nDelta text.\n", ""))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -448,15 +448,15 @@ def children(epic: Epic, authorization: str, http: Http) -> list[object] | Failu
|
||||
seen = http("GET", f"{epic.site}/rest/api/3/issue/{epic.key}?fields=summary", authorization, None)
|
||||
if isinstance(seen, Failure):
|
||||
return Failure(f"cannot read the epic {epic.key}: {seen.reason}")
|
||||
body: dict[str, object] = {
|
||||
"jql": f'parent = "{epic.key}" AND statusCategory != Done ORDER BY Rank ASC',
|
||||
"fields": ["summary", "description", "fixVersions", "issuelinks"],
|
||||
"properties": [PROPERTY],
|
||||
"maxResults": PAGE,
|
||||
}
|
||||
return searched(epic.site, f"{epic.key}'s children", f'parent = "{epic.key}" AND statusCategory != Done ORDER BY Rank ASC', {"fields": ["summary", "description", "fixVersions", "issuelinks"], "properties": [PROPERTY]}, authorization, http)
|
||||
|
||||
|
||||
def searched(site: str, what: str, jql: str, asked: dict[str, object], authorization: str, http: Http) -> list[object] | Failure:
|
||||
"""Every issue a JQL search on site finds, page by page. The dict asked holds the request's `fields` and `properties` lists. The text what names the issues sought, in the Failure of a search that doesn't end."""
|
||||
body: dict[str, object] = {"jql": jql, "maxResults": PAGE} | asked
|
||||
found: list[object] = []
|
||||
for _ in range(MAX_PAGES):
|
||||
page = http("POST", f"{epic.site}/rest/api/3/search/jql", authorization, body)
|
||||
page = http("POST", f"{site}/rest/api/3/search/jql", authorization, body)
|
||||
if isinstance(page, Failure):
|
||||
return page
|
||||
if not isinstance(page, dict) or not isinstance(page.get("issues"), list):
|
||||
@@ -465,7 +465,7 @@ def children(epic: Epic, authorization: str, http: Http) -> list[object] | Failu
|
||||
if not page.get("nextPageToken"):
|
||||
return found
|
||||
body = body | {"nextPageToken": page["nextPageToken"]}
|
||||
return Failure(f"the search for {epic.key}'s children did not end within {MAX_PAGES} pages")
|
||||
return Failure(f"the search for {what} did not end within {MAX_PAGES} pages")
|
||||
|
||||
|
||||
def issue_of(raw: object) -> Issue | Failure:
|
||||
|
||||
@@ -6,6 +6,9 @@ Run inside a checkout, or a worktree of one. It finds the config that names the
|
||||
~/.claude-projects/<key>/config.toml, and prints each setting and its absolute path, as in
|
||||
`plan: /home/me/code/acme/todo.md`. Given a setting, it prints only that setting's path.
|
||||
|
||||
`~/.claude-projects/do-epic/` holds do-epic's machine-wide settings, not a project's. It needs no `checkout`,
|
||||
and project-config doesn't read anything from it, so no project may use `do-epic` as its key.
|
||||
|
||||
The config is TOML. `checkout` names the project's main checkout, and every setting in the example below is a path:
|
||||
"checkout:<path>" inside the checkout, "folder:<path>" inside the config's own folder, or one that
|
||||
starts with / or ~/. In a worktree, a "checkout:" path is the worktree's own file.
|
||||
@@ -51,7 +54,9 @@ Where the config leaves them out, `todo-item finish` fails in a Jira plan. Each
|
||||
`goals-and-audience` may name a Jira Cloud epic the same way, where `plan` names one too. The
|
||||
epic's description is then the goals file, with ## Goals and ## Audience as headings, and
|
||||
`project-config goals-and-audience` prints the setting as the config spells it. The description is
|
||||
only read: edit it in Jira.
|
||||
written by `project-record save`, except a change to ## Goals or ## Audience, which is refused:
|
||||
edit those two in Jira. A save rewrites the whole description from markdown, which keeps no
|
||||
mention, macro or block id that markdown can't hold.
|
||||
|
||||
Each record, `decisions`, `score-anchors`, `comprehension-history`, `reading-cost` and `changelog`,
|
||||
may name a Confluence Cloud page in place of a path, as "confluence:" and the page's address, with or
|
||||
|
||||
@@ -17,6 +17,10 @@ from plan.ranking import variables
|
||||
|
||||
PROJECTS = ".claude-projects"
|
||||
CONFIG = "config.toml"
|
||||
# The key of do-epic's machine-wide settings: the folder's config.toml has no `checkout`, so no project may use the key.
|
||||
DO_EPIC = "do-epic"
|
||||
# A Jira Cloud or Confluence Cloud site's address: only https, so the credentials are never sent in the clear.
|
||||
SITE = r"https://[A-Za-z0-9.-]+(?::[0-9]+)?"
|
||||
# Every path a project sets, by its role. Three readers, seated as the human who writes the config, picked the spelling from four candidates. They picked role names over file names, which a client's project doesn't share.
|
||||
SETTINGS = ("plan", "working-rules", "decisions", "score-anchors", "comprehension-history", "reading-cost", "changelog", "goals-and-audience", "principles", "standards")
|
||||
# They also picked a prefix over bare relative paths: each line says its own place, and a relative path with no prefix fails instead of landing somewhere by default.
|
||||
@@ -177,7 +181,7 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
|
||||
top is the worktree that asks, and main itself outside a worktree. A `checkout:` path lands in top.
|
||||
A config that cannot be read fails every project, since it may be the one that names main."""
|
||||
named: list[tuple[pathlib.Path, dict[str, object]]] = []
|
||||
for file in sorted((home / PROJECTS).glob(f"*/{CONFIG}")):
|
||||
for file in configs(home):
|
||||
config = read(file, home)
|
||||
if isinstance(config, Failure):
|
||||
return config
|
||||
@@ -290,14 +294,14 @@ def name_of(value: object) -> str | None:
|
||||
|
||||
|
||||
def epic_of(value: object) -> Epic | None:
|
||||
"""The epic a setting's value spells; None where it spells none. Only https, so the credentials are never sent in the clear."""
|
||||
spelt = isinstance(value, str) and re.fullmatch(rf"{JIRA}(https://[A-Za-z0-9.-]+(?::[0-9]+)?)/browse/([A-Z][A-Z0-9_]*-[0-9]+)", value)
|
||||
"""The epic a setting's value spells; None where it spells none."""
|
||||
spelt = isinstance(value, str) and re.fullmatch(rf"{JIRA}({SITE})/browse/([A-Z][A-Z0-9_]*-[0-9]+)", value)
|
||||
return Epic(spelt.group(1), spelt.group(2)) if spelt else None
|
||||
|
||||
|
||||
def page_of(value: object) -> Page | None:
|
||||
"""The page a setting's value spells; None where it spells none. Only https, as for an epic."""
|
||||
spelt = isinstance(value, str) and re.fullmatch(rf"{CONFLUENCE}(https://[A-Za-z0-9.-]+(?::[0-9]+)?)/wiki/spaces/[^/?#]+/pages/([0-9]+)(?:/[^/?#]*)?", value)
|
||||
"""The page a setting's value spells; None where it spells none."""
|
||||
spelt = isinstance(value, str) and re.fullmatch(rf"{CONFLUENCE}({SITE})/wiki/spaces/[^/?#]+/pages/([0-9]+)(?:/[^/?#]*)?", value)
|
||||
return Page(spelt.group(1), spelt.group(2), spelt.string) if spelt else None
|
||||
|
||||
|
||||
@@ -339,25 +343,51 @@ def lies_in(path: pathlib.Path, *places: pathlib.Path) -> bool:
|
||||
def held(folder: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure | None:
|
||||
"""The config in folder, read as its project's main checkout reads it; None where folder is not a config's folder under home."""
|
||||
# Matched on resolved paths, as git names a folder, so a config's folder reached through a symlink is still found.
|
||||
file = next((file for file in (home / PROJECTS).glob(f"*/{CONFIG}") if file.parent.resolve() == folder.resolve()), None)
|
||||
file = next((file for file in configs(home) if file.parent.resolve() == folder.resolve()), None)
|
||||
if file is None:
|
||||
return None
|
||||
config = read(file, home)
|
||||
return config if isinstance(config, Failure) else load(config[0], config[0], home)
|
||||
|
||||
|
||||
def by_name(name: str, home: pathlib.Path) -> ProjectConfig | Failure | None:
|
||||
"""The config of the project whose main checkout is a folder called name, though that folder may not exist yet, loaded as `held` does; None where no config names one. A config that cannot be read fails every name, since it may be the one asked for."""
|
||||
files = []
|
||||
for file in configs(home):
|
||||
config = read(file, home)
|
||||
if isinstance(config, Failure):
|
||||
return config
|
||||
if config[0].name == name:
|
||||
files.append(file)
|
||||
if len(files) > 1:
|
||||
return Failure(f"{' and '.join(str(file) for file in files)} each name a checkout called {name}; only one config may")
|
||||
return held(files[0].parent, home) if files else None
|
||||
|
||||
|
||||
def configs(home: pathlib.Path) -> list[pathlib.Path]:
|
||||
"""Every project's config file under home, in key order. do-epic's machine-wide config is not a project's."""
|
||||
return sorted(file for file in (home / PROJECTS).glob(f"*/{CONFIG}") if file.parent.name != DO_EPIC)
|
||||
|
||||
|
||||
def env_files(home: pathlib.Path) -> list[pathlib.Path]:
|
||||
"""Every config's env-file, read without the checks load makes, so a config that fails them still keeps its credentials guarded; a config that cannot be read names none."""
|
||||
configs = [read(file, home) for file in sorted((home / PROJECTS).glob(f"*/{CONFIG}"))]
|
||||
return [path for config in configs if not isinstance(config, Failure) and (path := anywhere(config[1].get(ENV_FILE), home))]
|
||||
"""Every config's env-file, do-epic's included. Each is read without the checks load makes, so a config that fails them still keeps its credentials guarded. A config that can't be read doesn't name one."""
|
||||
values = [parsed(file) for file in (*configs(home), home / PROJECTS / DO_EPIC / CONFIG)]
|
||||
return [path for found in values if not isinstance(found, Failure) and (path := anywhere(found.get(ENV_FILE), home))]
|
||||
|
||||
|
||||
def parsed(file: pathlib.Path) -> dict[str, object] | Failure:
|
||||
"""The TOML values in file; a Failure where file can't be read or parsed."""
|
||||
try:
|
||||
return tomllib.loads(file.read_text(encoding="utf-8"))
|
||||
except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as failure:
|
||||
return Failure(f"cannot read {file}: {failure}")
|
||||
|
||||
|
||||
def read(file: pathlib.Path, home: pathlib.Path) -> tuple[pathlib.Path, dict[str, object]] | Failure:
|
||||
"""The checkout a config names, as an absolute path, and its other values."""
|
||||
try:
|
||||
values = tomllib.loads(file.read_text(encoding="utf-8"))
|
||||
except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as failure:
|
||||
return Failure(f"cannot read {file}: {failure}")
|
||||
values = parsed(file)
|
||||
if isinstance(values, Failure):
|
||||
return values
|
||||
checkout = anywhere(values.get("checkout"), home)
|
||||
if checkout is None:
|
||||
found = f"not {values['checkout']!r}" if "checkout" in values else "and is missing"
|
||||
|
||||
@@ -9,7 +9,7 @@ import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from project_config.settings import env_files, Epic, Finish, held, JiraScoring, load, NOTIFIERS, OUTSIDE, Page, ProjectConfig, SETTINGS, Unnamed
|
||||
from project_config.settings import by_name, env_files, Epic, Finish, held, JiraScoring, load, NOTIFIERS, OUTSIDE, Page, ProjectConfig, SETTINGS, Unnamed
|
||||
from failure import Failure
|
||||
from test_env import isolated_env
|
||||
from project_config.main import located, title_templates
|
||||
@@ -91,6 +91,13 @@ class LoadTest(ConfigCase):
|
||||
self.assertEqual(found.paths["standards"], self.home / ".claude" / "skills" / "larv-review" / "standards")
|
||||
self.assertEqual(tuple(found.paths), SETTINGS)
|
||||
|
||||
def test_the_do_epic_folder_is_no_projects_config_so_it_needs_no_checkout(self) -> None:
|
||||
self.write("do-epic", 'jira-url = "https://acme.atlassian.net"\n')
|
||||
self.write("acme", CLIENT)
|
||||
found = load(self.checkout, self.checkout, self.home)
|
||||
assert isinstance(found, ProjectConfig), found
|
||||
self.assertEqual(found.key, "acme")
|
||||
|
||||
def test_a_plan_spelt_as_a_jira_epic_is_no_path(self) -> None:
|
||||
self.write("acme", JIRA_CLIENT)
|
||||
found = load(self.checkout, self.checkout, self.home)
|
||||
@@ -578,6 +585,10 @@ class HeldTest(ConfigCase):
|
||||
(elsewhere / "config.toml").write_text(CLIENT, encoding="utf-8")
|
||||
self.assertIsNone(held(elsewhere, self.home), "a config.toml outside ~/.claude-projects/")
|
||||
|
||||
def test_the_do_epic_folder_holds_no_projects_config(self) -> None:
|
||||
folder = self.write("do-epic", CLIENT)
|
||||
self.assertIsNone(held(folder, self.home))
|
||||
|
||||
def test_a_configs_folder_reached_through_a_symlink_holds_its_config(self) -> None:
|
||||
target = self.home / "kept" / "acme"
|
||||
target.mkdir(parents=True)
|
||||
@@ -587,6 +598,32 @@ class HeldTest(ConfigCase):
|
||||
self.assertIsInstance(held(target, self.home), ProjectConfig, "git names the folder by its resolved path")
|
||||
|
||||
|
||||
class ByNameTest(ConfigCase):
|
||||
def test_finds_the_config_of_the_project_whose_main_checkout_has_the_name_though_the_checkout_is_missing(self) -> None:
|
||||
folder = self.write("acme", CLIENT)
|
||||
found = by_name("acme-billing", self.home)
|
||||
assert isinstance(found, ProjectConfig), found
|
||||
self.assertEqual((found.folder, found.top), (folder, self.checkout))
|
||||
|
||||
def test_finds_none_where_no_config_names_a_checkout_of_that_name(self) -> None:
|
||||
self.write("acme", CLIENT)
|
||||
self.assertIsNone(by_name("billing", self.home), "a part of a name is not the name")
|
||||
self.assertIsNone(by_name("acme", self.home), "a config's key is not a repo's name")
|
||||
self.assertIsNone(by_name("do-epic", self.home))
|
||||
|
||||
def test_fails_where_two_configs_name_a_checkout_of_that_name(self) -> None:
|
||||
self.write("acme", CLIENT)
|
||||
self.write("acme-two", CLIENT.replace("~/code/acme-billing", "~/other/acme-billing"))
|
||||
found = by_name("acme-billing", self.home)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("acme/config.toml and ", found.reason)
|
||||
self.assertIn("acme-two/config.toml each name a checkout called acme-billing", found.reason)
|
||||
|
||||
def test_fails_where_a_config_cannot_be_read_since_it_may_be_the_one_asked_for(self) -> None:
|
||||
self.write("acme", "this is not toml")
|
||||
self.assertIsInstance(by_name("acme-billing", self.home), Failure)
|
||||
|
||||
|
||||
class EnvFilesTest(ConfigCase):
|
||||
def test_lists_each_readable_configs_env_file_even_one_load_refuses(self) -> None:
|
||||
self.write("acme", CLIENT + 'env-file = "~/acme.env"\n')
|
||||
@@ -595,6 +632,10 @@ class EnvFilesTest(ConfigCase):
|
||||
self.write("none", CLIENT)
|
||||
self.assertEqual(env_files(self.home), [self.home / "acme.env", pathlib.Path("/srv/other.env")])
|
||||
|
||||
def test_lists_do_epics_env_file_though_its_config_names_no_checkout(self) -> None:
|
||||
self.write("do-epic", 'env-file = "~/jira.env"\n')
|
||||
self.assertEqual(env_files(self.home), [self.home / "jira.env"])
|
||||
|
||||
|
||||
class ToolTest(ConfigCase):
|
||||
def test_prints_a_jira_plan_as_the_config_spells_it(self) -> None:
|
||||
|
||||
@@ -5,8 +5,8 @@ usage: project-record open <record>
|
||||
|
||||
Run inside a checkout, or a worktree of one. <record> is one of decisions, score-anchors,
|
||||
comprehension-history, reading-cost and changelog, each kept where the project's config names it,
|
||||
or goals-and-audience, the goals file. Goals kept in a Jira epic's description are only read, so
|
||||
save refuses them: edit the epic in Jira.
|
||||
or goals-and-audience, the goals file. Save refuses a change to the ## Goals or ## Audience of goals
|
||||
kept in a Jira epic's description: edit those two in Jira.
|
||||
|
||||
open prints the path of a file holding the record's text, and nothing else. Read and edit that
|
||||
file. Where the record is kept as a file, the path is that file. Where it is kept elsewhere, the
|
||||
@@ -20,8 +20,8 @@ save stores the file's text in the record, and prints one line saying so. Where
|
||||
as a file, there is nothing left to store, and save says so. Run it after every edit, so the same
|
||||
steps work wherever a project keeps the record.
|
||||
|
||||
For a record kept on a Confluence page, save refuses where someone saved the page after open read
|
||||
it or after save last wrote it, and the human decides what follows. The copy keeps the unsaved edit
|
||||
For a record kept on a Confluence page or in a Jira epic's description, save refuses where someone
|
||||
saved it after open read it or after save last wrote it, and the human decides what follows. The copy keeps the unsaved edit
|
||||
until open runs again and writes the copy afresh.
|
||||
|
||||
Exit 2 on bad arguments, and 1, saying why, on any other failure.
|
||||
|
||||
@@ -2,16 +2,15 @@
|
||||
|
||||
A write never overwrites a teammate's edit: it sends the version after the one held() read, and Confluence refuses it where someone saved the page since. The human then decides (lilleman, 2026-11-12)."""
|
||||
import dataclasses
|
||||
import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import typing
|
||||
|
||||
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
|
||||
from atlassian.client import basic_auth, confluence_json, Http, ToAdf, ToMarkdown
|
||||
from record_store.held import digest_of, Held
|
||||
from rest_client.json_request import ErrorAnswer
|
||||
from failure import Failure
|
||||
from record_store.held import Held
|
||||
from project_config.settings import Page
|
||||
|
||||
# What Confluence answers to a write whose version isn't the one after the version it holds.
|
||||
@@ -141,7 +140,3 @@ class ConfluencePage:
|
||||
return Seen(int(held["version"]), str(held["title"]), held.get("digest"))
|
||||
except (ValueError, KeyError, TypeError) as failure:
|
||||
return Failure(f"cannot read the version of {self.label} to write over, {over!r}: {failure}")
|
||||
|
||||
|
||||
def digest_of(text: str) -> str:
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Record stores for tests, held to record_store.contract like every backend."""
|
||||
"""Record stores for tests, held to record_store.contract like every backend, and a fake Jira."""
|
||||
import dataclasses
|
||||
|
||||
from failure import Failure
|
||||
@@ -29,3 +29,33 @@ class MemoryStore:
|
||||
return Failure(f"someone saved {self.label} since version {over}")
|
||||
self.text, self.version = text, self.version + 1
|
||||
return Held(text, str(self.version))
|
||||
|
||||
|
||||
class FakeJira:
|
||||
"""One epic as Jira Cloud's REST API version 3 serves it: a description that is null or a document, and no version a write could check. Its documents hold the text in one paragraph, which fake_adf and fake_markdown convert."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.description: dict[str, object] | None = None
|
||||
self.calls: list[tuple[str, str, str, object]] = []
|
||||
|
||||
def http(self, method: str, url: str, authorization: str, body: object) -> object:
|
||||
self.calls.append((method, url, authorization, body))
|
||||
if method == "GET":
|
||||
return {"key": "ABC-12", "fields": {"description": self.description}}
|
||||
assert isinstance(body, dict)
|
||||
self.description = body["fields"]["description"]
|
||||
return None
|
||||
|
||||
def teammate_saves(self, text: str) -> None:
|
||||
self.description = fake_adf(text)
|
||||
|
||||
def puts(self) -> list[object]:
|
||||
return [call[3] for call in self.calls if call[0] == "PUT"]
|
||||
|
||||
|
||||
def fake_adf(text: str) -> dict[str, object]:
|
||||
return {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": text}]}]}
|
||||
|
||||
|
||||
def fake_markdown(documents: list[dict[str, object]]) -> list[str]:
|
||||
return [document["content"][0]["content"][0]["text"] for document in documents] # type: ignore[index]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""A record as a read found it or a write left it, which every record backend gives."""
|
||||
import dataclasses
|
||||
import hashlib
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -7,3 +8,8 @@ class Held:
|
||||
"""A record's text as one read found it or one write left it, with its version: what a write over it names, so the write refuses where someone saved the record since. None for a backend whose writes check no version, as a file's."""
|
||||
text: str
|
||||
version: str | None = None
|
||||
|
||||
|
||||
def digest_of(text: str) -> str:
|
||||
"""A fingerprint of a record's text or document, for a version a write names."""
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
|
||||
@@ -1,25 +1,31 @@
|
||||
"""A project's goals and audience kept as the description of one Jira Cloud epic, read over Jira's REST API with the credentials of the config's env-file, and converted by the adf-codec shim.
|
||||
"""A project's goals and audience kept as the description of one Jira Cloud epic, read and written over Jira's REST API with the credentials of the config's env-file, and converted by the adf-codec shim.
|
||||
|
||||
The store only reads: a write, such as `project-record save goals-and-audience`, fails, saying to edit the epic in Jira."""
|
||||
Jira keeps no version of a description, so the version a read gives is a digest of the document, and a write refuses where the description no longer has that digest. A write changes ## Goals or ## Audience only where the store was told the human approved that section in an interrupt (`approved`)."""
|
||||
import dataclasses
|
||||
import json
|
||||
import pathlib
|
||||
import typing
|
||||
|
||||
from adf_codec.convert import adfs_to_markdown, Refused
|
||||
from atlassian.client import basic_auth, Http, jira_json, ToMarkdown
|
||||
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
|
||||
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
|
||||
from record_store.held import digest_of, Held
|
||||
from project_config.settings import Epic
|
||||
from failure import Failure
|
||||
from record_store.held import Held
|
||||
from text_files.markdown_text import sections
|
||||
|
||||
READ_ONLY = "goals and audience kept in Jira are only read: edit the epic's description in Jira"
|
||||
# The sections of the description a write changes only after the human approves.
|
||||
APPROVED_SECTIONS = ("Goals", "Audience")
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class EpicDescription:
|
||||
"""The description of epic as markdown, read with the credentials env_file holds."""
|
||||
"""The description of epic as markdown, read and written with the credentials env_file holds. approved names the APPROVED_SECTIONS whose change the human approved."""
|
||||
epic: Epic
|
||||
env_file: pathlib.Path
|
||||
http: Http = jira_json
|
||||
to_markdown: ToMarkdown = adfs_to_markdown
|
||||
to_adf: ToAdf = markdown_to_adf
|
||||
approved: frozenset[str] = frozenset()
|
||||
|
||||
@property
|
||||
def label(self) -> str:
|
||||
@@ -31,28 +37,70 @@ class EpicDescription:
|
||||
|
||||
def read(self) -> str | Failure:
|
||||
"""The description as markdown; empty where the epic has none."""
|
||||
held = self.held()
|
||||
return held if isinstance(held, Failure) else held.text
|
||||
|
||||
def held(self) -> Held | Failure:
|
||||
"""The description as markdown, with a digest of the document Jira holds as the version a write over it names."""
|
||||
description = self.fetched()
|
||||
if isinstance(description, Failure):
|
||||
return description
|
||||
text = "" if description is None else self.markdown_of(description)
|
||||
return text if isinstance(text, Failure) else Held(text, digest_of(json.dumps(description, sort_keys=True)))
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
return f"{self.epic.site}/rest/api/3/issue/{self.epic.key}"
|
||||
|
||||
def fetched(self) -> dict[str, typing.Any] | None | Failure:
|
||||
"""The description as Jira holds it, an ADF document; None where the epic has none."""
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
key = self.epic.key
|
||||
answer = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{key}?fields=description", authorization, None)
|
||||
answer = self.http("GET", self.url + "?fields=description", authorization, None)
|
||||
if isinstance(answer, Failure):
|
||||
return Failure(f"cannot read the epic {key}: {answer.reason}")
|
||||
fields = answer.get("fields") if isinstance(answer, dict) else None
|
||||
if not isinstance(fields, dict) or "description" not in fields or not isinstance(fields["description"], dict | None):
|
||||
return Failure(f"the epic {key} is not in the shape Jira Cloud's REST API version 3 documents: its description field is missing, or neither a document nor null")
|
||||
if fields["description"] is None:
|
||||
return ""
|
||||
texts = self.to_markdown([fields["description"]])
|
||||
return fields["description"]
|
||||
|
||||
def markdown_of(self, document: dict[str, typing.Any]) -> str | Failure:
|
||||
key = self.epic.key
|
||||
texts = self.to_markdown([document])
|
||||
if isinstance(texts, Refused):
|
||||
return Failure(f"{key}'s description: {texts.failure.reason}")
|
||||
if isinstance(texts, Failure):
|
||||
return Failure(f"cannot convert {key}'s description to markdown: {texts.reason}")
|
||||
return texts[0]
|
||||
|
||||
def held(self) -> Held | Failure:
|
||||
text = self.read()
|
||||
return text if isinstance(text, Failure) else Held(text)
|
||||
|
||||
def write(self, text: str, over: str | None) -> Held | Failure:
|
||||
return Failure(READ_ONLY)
|
||||
"""Replaces the description with text, over the version held() gave. A text that is the description as read leaves it as Jira holds it, since converting the markdown back would drop each block's localId and Jira's spacing paragraphs; any other text rewrites the whole description, which keeps only what markdown can hold. Jira checks no version, so the check of over is a read just before the request, and an edit a teammate saves between the two is overwritten."""
|
||||
key = self.epic.key
|
||||
current = self.held()
|
||||
if isinstance(current, Failure):
|
||||
return current
|
||||
if over is not None and current.version != over:
|
||||
return Failure(f"someone saved the description of {key} since the loop read it, so the loop didn't write it: never overwrite their edit, but ask the human what to do")
|
||||
if text == current.text:
|
||||
return current
|
||||
document = self.to_adf(text) if text else None
|
||||
if isinstance(document, Failure):
|
||||
return Failure(f"cannot convert the text for {key}'s description to ADF: {document.reason}")
|
||||
# The sections of the description Jira will hold, as the codec reads them: a heading spelt another way than `## Goals` still counts.
|
||||
sent = "" if document is None else self.markdown_of(document)
|
||||
if isinstance(sent, Failure):
|
||||
return sent
|
||||
before, after = sections(current.text), sections(sent)
|
||||
if repeated := [name for name, bodies in after.items() if len(bodies) > max(1, len(before.get(name, [])))]:
|
||||
return Failure(f"the write would repeat ## {repeated[0]} in {key}'s description, which locks that section for reads and writes: leave a heading out of the text, or edit the description in Jira")
|
||||
if unapproved := [name for name in APPROVED_SECTIONS if name not in self.approved and after.get(name) != before.get(name)]:
|
||||
return Failure(f"the write changes ## {unapproved[0]} of {key}'s description without the human's approval: have the human approve it in an interrupt, or edit the section in Jira")
|
||||
authorization = basic_auth(self.env_file)
|
||||
if isinstance(authorization, Failure):
|
||||
return authorization
|
||||
answer = self.http("PUT", self.url, authorization, {"fields": {"description": document}})
|
||||
if isinstance(answer, Failure):
|
||||
return Failure(f"cannot write the description of {key}: {answer.reason}")
|
||||
return self.held()
|
||||
|
||||
@@ -87,7 +87,7 @@ def touch_predictions_store(config: ProjectConfig) -> RecordStore | None:
|
||||
|
||||
|
||||
def goals_store(config: ProjectConfig) -> RecordStore:
|
||||
"""The store config names for the goals file, which holds ## Goals and ## Audience: a markdown file, or a Jira epic's description, which the store only reads."""
|
||||
"""The store config names for the goals file, which holds ## Goals and ## Audience: a markdown file, or a Jira epic's description. This store is built without an approval, so it refuses a change to the epic's ## Goals or ## Audience."""
|
||||
if config.goals_epic is None:
|
||||
return FileStore(config.paths["goals-and-audience"], config.tracked("goals-and-audience"))
|
||||
# load refuses a config that names an epic and no env-file.
|
||||
|
||||
@@ -9,10 +9,14 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
from project_config.settings import Epic
|
||||
from record_store.jira_description import EpicDescription
|
||||
from failure import Failure
|
||||
from record_store.fakes import fake_adf, fake_markdown, FakeJira
|
||||
from record_store.held import Held
|
||||
from record_store.contract import RecordStoreContract
|
||||
from adf_codec.convert import Refused
|
||||
|
||||
EPIC = Epic("https://acme.atlassian.net", "ABC-12")
|
||||
AUTHORIZATION = "Basic " + base64.b64encode(b"me@example.com:s3cret").decode()
|
||||
URL = "https://acme.atlassian.net/rest/api/3/issue/ABC-12"
|
||||
GOALS = {"type": "doc", "version": 1, "content": [{"type": "heading", "attrs": {"level": 2}, "content": [{"type": "text", "text": "Goals"}]}]}
|
||||
|
||||
|
||||
@@ -69,20 +73,176 @@ class EpicDescriptionTest(unittest.TestCase):
|
||||
for markdown in (Refused(0, Failure("an unsupported node")), Failure("docker is not running")):
|
||||
with self.subTest(markdown):
|
||||
self.markdown = markdown
|
||||
found = self.store().read()
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
for found in (self.store().read(), self.store().held()):
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
|
||||
def test_missing_credentials_fail(self) -> None:
|
||||
self.env.unlink()
|
||||
self.assertIsInstance(self.store().read(), Failure)
|
||||
self.assertEqual(self.calls, [])
|
||||
|
||||
def test_writing_fails_since_the_store_only_reads(self) -> None:
|
||||
found = self.store().write("## Goals\n", None)
|
||||
|
||||
class WriteCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.env = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "acme.env"
|
||||
self.env.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n")
|
||||
self.env.chmod(0o600)
|
||||
self.jira = FakeJira()
|
||||
|
||||
def new_store(self, approved: frozenset[str] = frozenset()) -> EpicDescription:
|
||||
return EpicDescription(EPIC, self.env, self.jira.http, fake_markdown, fake_adf, approved)
|
||||
|
||||
|
||||
class EpicDescriptionContract(WriteCase, RecordStoreContract):
|
||||
pass
|
||||
|
||||
|
||||
GOALS_AND_SCORING = "## Goals\n1. **Achieve:** Ship.\n\n## Audience\nMe.\n\n## Scoring\nValue.\n"
|
||||
|
||||
|
||||
class EpicDescriptionWriteTest(WriteCase):
|
||||
def held(self, text: str) -> Held:
|
||||
self.jira.teammate_saves(text)
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
return held
|
||||
|
||||
def test_writes_the_text_as_the_epics_description(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
text = GOALS_AND_SCORING.replace("Value.", "Value, then risk.")
|
||||
written = self.new_store().write(text, held.version)
|
||||
assert isinstance(written, Held), written
|
||||
self.assertEqual(self.jira.puts(), [{"fields": {"description": fake_adf(text)}}])
|
||||
put = next(call for call in self.jira.calls if call[0] == "PUT")
|
||||
self.assertEqual(put[:3], ("PUT", URL, AUTHORIZATION))
|
||||
self.assertEqual(written, self.new_store().held())
|
||||
|
||||
def test_writing_empty_text_clears_the_description(self) -> None:
|
||||
held = self.held("## Decisions\n- one\n")
|
||||
self.assertIsInstance(self.new_store().write("", held.version), Held)
|
||||
self.assertEqual(self.jira.puts(), [{"fields": {"description": None}}])
|
||||
|
||||
def test_saving_the_text_a_read_found_leaves_the_description_as_jira_holds_it(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
before = self.jira.description
|
||||
self.assertEqual(self.new_store().write(held.text, held.version), held)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
self.assertIs(self.jira.description, before)
|
||||
|
||||
def test_refuses_to_write_where_a_teammate_saved_since_the_read(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
self.jira.teammate_saves("## Goals\n1. **Achieve:** Their goal.\n")
|
||||
for text in (held.text, GOALS_AND_SCORING.replace("Value.", "Risk.")):
|
||||
with self.subTest(text):
|
||||
found = self.new_store().write(text, held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("someone saved", found.reason)
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
self.assertEqual(self.new_store().read(), "## Goals\n1. **Achieve:** Their goal.\n")
|
||||
|
||||
def test_refuses_to_write_over_the_version_of_an_epic_that_had_no_description(self) -> None:
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
self.jira.teammate_saves("## Goals\nTheirs.\n")
|
||||
found = self.new_store(frozenset({"Goals"})).write("## Goals\nMine.\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("Jira", found.reason)
|
||||
self.assertEqual(self.calls, [])
|
||||
self.assertIn("someone saved", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_refuses_to_change_goals_or_audience_before_the_human_approves(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
changes = [
|
||||
("Goals", GOALS_AND_SCORING.replace("Ship.", "Ship less.")),
|
||||
("Audience", GOALS_AND_SCORING.replace("Me.", "Everyone.")),
|
||||
("Goals", GOALS_AND_SCORING.replace("## Goals\n1. **Achieve:** Ship.\n\n", "")),
|
||||
]
|
||||
for name, text in changes:
|
||||
with self.subTest(name, text=text):
|
||||
found = self.new_store().write(text, held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn(name, found.reason)
|
||||
self.assertIn("approval", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_counts_a_heading_the_codec_reads_as_goals_even_where_the_markdown_spells_it_another_way(self) -> None:
|
||||
held = self.held("## Scoring\nValue.\n")
|
||||
as_atx = lambda documents: [fake_markdown(documents)[0].replace("Goals\n=====", "## Goals")]
|
||||
store = EpicDescription(EPIC, self.env, self.jira.http, as_atx, fake_adf)
|
||||
found = store.write("Goals\n=====\nShip.\n\n## Scoring\nValue.\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("## Goals", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_refuses_a_write_that_repeats_a_section_the_codec_reads_from_another_spelling_of_its_heading(self) -> None:
|
||||
held = self.held("## Decisions\n- one\n")
|
||||
as_atx = lambda documents: [fake_markdown(documents)[0].replace("Decisions\n=====", "## Decisions")]
|
||||
store = EpicDescription(EPIC, self.env, self.jira.http, as_atx, fake_adf)
|
||||
found = store.write("## Decisions\n- one\n\n## Scoring\nValue.\n\nDecisions\n=====\n- copied\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("repeat ## Decisions", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_keeps_a_section_that_was_already_repeated_in_the_description(self) -> None:
|
||||
held = self.held("## Decisions\n- one\n\n## Decisions\n- two\n")
|
||||
written = self.new_store().write("## Decisions\n- one\n\n## Decisions\n- two\n\n## Scoring\nValue.\n", held.version)
|
||||
self.assertIsInstance(written, Held)
|
||||
|
||||
def test_the_version_does_not_depend_on_the_order_of_the_documents_keys(self) -> None:
|
||||
held = self.held("## Scoring\nValue.\n")
|
||||
self.jira.description = dict(reversed(list(fake_adf("## Scoring\nValue.\n").items())))
|
||||
self.assertEqual(self.new_store().held(), held)
|
||||
|
||||
def test_refuses_to_write_where_a_teammate_saved_a_change_the_markdown_does_not_show(self) -> None:
|
||||
held = self.held("## Scoring\nValue.\n")
|
||||
self.jira.description = {**fake_adf("## Scoring\nValue.\n"), "localId": "teammate"}
|
||||
found = self.new_store().write("## Scoring\nRisk.\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("someone saved", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_refuses_to_add_a_goals_section_before_the_human_approves(self) -> None:
|
||||
held = self.held("## Scoring\nValue.\n")
|
||||
found = self.new_store().write("## Goals\n1. **Achieve:** Ship.\n\n## Scoring\nValue.\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("Goals", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_changes_goals_once_the_human_approved_that_section_only(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
both = GOALS_AND_SCORING.replace("Ship.", "Ship less.").replace("Me.", "Everyone.")
|
||||
found = self.new_store(frozenset({"Goals"})).write(both, held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("Audience", found.reason)
|
||||
self.assertNotIn("Goals", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
written = self.new_store(frozenset({"Goals", "Audience"})).write(both, held.version)
|
||||
self.assertIsInstance(written, Held)
|
||||
self.assertEqual(self.jira.puts(), [{"fields": {"description": fake_adf(both)}}])
|
||||
|
||||
def test_changes_scoring_and_decisions_without_approval(self) -> None:
|
||||
held = self.held(GOALS_AND_SCORING)
|
||||
text = GOALS_AND_SCORING.replace("Value.", "Risk.") + "\n## Decisions\n- A choice.\n"
|
||||
self.assertIsInstance(self.new_store().write(text, held.version), Held)
|
||||
self.assertEqual(self.jira.puts(), [{"fields": {"description": fake_adf(text)}}])
|
||||
|
||||
def test_a_text_the_codec_cannot_convert_is_not_sent(self) -> None:
|
||||
held = self.held("## Decisions\n- one\n")
|
||||
store = EpicDescription(EPIC, self.env, self.jira.http, fake_markdown, lambda text: Failure("unsupported markdown"))
|
||||
found = store.write("## Decisions\n- two\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
self.assertIn("unsupported markdown", found.reason)
|
||||
self.assertEqual(self.jira.puts(), [])
|
||||
|
||||
def test_a_refused_request_fails_naming_the_epic(self) -> None:
|
||||
held = self.held("## Decisions\n- one\n")
|
||||
refusing = lambda method, url, authorization, body: Failure("HTTP 403: no permission") if method == "PUT" else self.jira.http(method, url, authorization, body)
|
||||
found = EpicDescription(EPIC, self.env, refusing, fake_markdown, fake_adf).write("## Decisions\n- two\n", held.version)
|
||||
assert isinstance(found, Failure), found
|
||||
self.assertIn("ABC-12", found.reason)
|
||||
self.assertIn("HTTP 403", found.reason)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Whether a Read or a Grep reads a credentials file, or a Bash command names it or searches a folder holding it, for the `env-file-read` rule.
|
||||
|
||||
The files are `~/.claude/.env` and every config's `env-file`, not only the calling project's: a worker in one client's project must not read another client's credentials either, and listing every config's file needs no git call, as finding the calling project's would.
|
||||
The files are `~/.claude/.env` and every config's `env-file`, do-epic's included, not only the calling project's: a worker in one client's project must not read another client's credentials either, and listing every config's file needs no git call, as finding the calling project's would.
|
||||
A Bash command names one where its text spells the file's path, or one of its words, a redirect's target included, leads to the file from the directory `cd` left the command in. Whether the command reads the file is not judged, so one that only mentions the path is refused too.
|
||||
It searches a folder holding one where `folder_search.searches` finds that an `ag`, `grep -r` or `rg` reaches the file.
|
||||
This guards against an agent reading the file in passing, not against one that hides the path, such as behind a glob, a variable of its own, `xargs` or `find -exec`."""
|
||||
|
||||
Reference in New Issue
Block a user