Answer stability round 1: 1 🔴, 4 🟡 (0 misread); check Goals and Audience on the document that is sent, report an unknown section as a Failure, refuse a section body that holds a heading, correct the stale read-only text

This commit is contained in:
2026-11-18 10:39:22 +01:00
parent 0755291112
commit fe351d3ed4
12 changed files with 77 additions and 30 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
"""Record stores and a Jira 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
+1 -1
View File
@@ -11,5 +11,5 @@ class Held:
def digest_of(text: str) -> str:
"""A fingerprint of a record's text, for a backend whose service keeps no version a write could name."""
"""A fingerprint of a record's text or document, for a version a write names."""
return hashlib.sha256(text.encode("utf-8")).hexdigest()
+29 -13
View File
@@ -1,8 +1,10 @@
"""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.
Jira keeps no version of a description, so the version a read gives is a digest of its markdown, 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`)."""
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, markdown_to_adf, Refused
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
@@ -39,7 +41,19 @@ class EpicDescription:
return held if isinstance(held, Failure) else held.text
def held(self) -> Held | Failure:
"""The description as markdown, with its digest as the version a write over it names."""
"""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
@@ -50,21 +64,19 @@ class EpicDescription:
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 Held("", digest_of(""))
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 Held(texts[0], digest_of(texts[0]))
@property
def url(self) -> str:
return f"{self.epic.site}/rest/api/3/issue/{self.epic.key}"
return texts[0]
def write(self, text: str, over: str | None) -> Held | Failure:
"""Replaces the description with text, over the version held() gave. A text that is the description as read sends nothing, since converting it back would drop each block's localId and Jira's spacing paragraphs. The check of over is a read just before the request: Jira would still take an edit made between the two."""
"""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):
@@ -73,11 +85,15 @@ class EpicDescription:
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
if unapproved := [name for name in APPROVED_SECTIONS if name not in self.approved and sections(text).get(name) != sections(current.text).get(name)]:
return Failure(f"the write changes ## {unapproved[0]} of {key}'s description, which needs the human's approval in an interrupt first")
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
if unapproved := [name for name in APPROVED_SECTIONS if name not in self.approved and sections(sent).get(name) != sections(current.text).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
+1 -1
View File
@@ -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, whose ## Goals and ## Audience the store changes only once the human approved."""
"""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.
+17
View File
@@ -166,6 +166,23 @@ class EpicDescriptionWriteTest(WriteCase):
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_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)