From 90d5f0039da06c7ec504393bd03d181ff94bca6c Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:27:10 +0100 Subject: [PATCH 1/8] Convert markdown to ADF and back through a pinned adf-codec shim in a container --- README.md | 1 + docs/reading-cost.md | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 378e1cdf..7484b458 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a - `branch_chunks/` cuts a default branch into chunks and finds, for each, the items it shipped and the later chunks that fixed its code, for `score_outcomes` and `routing_figures`. It imports `reading_cost`'s record path. - `command_lines/` reads a shell command: its simple commands, and a docker call's subcommand and memory limits, for `command_patterns`, `reading_cost`, `routing_figures`, `rule_hook` and `run_heavy`. +- `adf_codec/` converts markdown to ADF (the rich text of Jira Cloud and Confluence) and back, with `@larvit/adf-codec`'s portable calls in a throwaway container: `convert.py` is the Python side, `node_shim/` the pinned Node shim and its Dockerfile. No tool calls it yet. - `chunk_pr` imports `deploy_release`'s forge calls, and `forge_of` as `deploy_release` does. - `deploy_release` imports `chunk_preflight`'s `forge_of`, which tells a GitHub origin from a Gitea one, and `release_plan`'s `release_standing`. - `git_cli/` runs git for every tool (`git_repo.py`), and reads the git calls in a shell command (`git_command.py`). diff --git a/docs/reading-cost.md b/docs/reading-cost.md index bd0a1fb3..49d354d2 100644 --- a/docs/reading-cost.md +++ b/docs/reading-cost.md @@ -2016,3 +2016,20 @@ Read most (opens and searches): - `bin/project_config/settings.py` 1 - `bin/review_rules/round_titles.py` 1 - `todo.md` 1 + +## Reading cost, chunk 4e71c0f752d0, finished 2026-11-14T15:25:12Z + +279869 new input tokens before the first commit. + +Read most (opens and searches): + +- `docs/decisions.md` 12 +- `bin/release_plan/todo_table.py` 10 +- `bin/release_plan/release_standing.py` 7 +- `bin/release_plan/test_release_standing.py` 7 +- `bin/todo_item/test_todo_item.py` 6 +- `README.md` 5 +- `bin/chunk_preflight/test_chunk_preflight.py` 5 +- `bin/release_plan/plan_file.py` 5 +- `bin/release_plan/ranking.py` 5 +- `bin/larv_review/owed.py` 4 -- 2.52.0 From 18dbf7b3d8519df280244dcadf1a48058594435f Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:27:40 +0100 Subject: [PATCH 2/8] Track the adf_codec package, which the allowlist ignored --- .gitignore | 7 ++ bin/adf_codec/__init__.py | 0 bin/adf_codec/convert.py | 114 ++++++++++++++++++++++ bin/adf_codec/node_shim/Dockerfile | 7 ++ bin/adf_codec/node_shim/package-lock.json | 22 +++++ bin/adf_codec/node_shim/package.json | 8 ++ bin/adf_codec/node_shim/shim.mjs | 38 ++++++++ bin/adf_codec/test_adf_codec.py | 98 +++++++++++++++++++ 8 files changed, 294 insertions(+) create mode 100644 bin/adf_codec/__init__.py create mode 100644 bin/adf_codec/convert.py create mode 100644 bin/adf_codec/node_shim/Dockerfile create mode 100644 bin/adf_codec/node_shim/package-lock.json create mode 100644 bin/adf_codec/node_shim/package.json create mode 100644 bin/adf_codec/node_shim/shim.mjs create mode 100644 bin/adf_codec/test_adf_codec.py diff --git a/.gitignore b/.gitignore index 5bdf2c9d..39346409 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,13 @@ !agents/ !agents/*.md !bin/ +!bin/adf_codec/ +!bin/adf_codec/*.py +!bin/adf_codec/node_shim/ +!bin/adf_codec/node_shim/Dockerfile +!bin/adf_codec/node_shim/package-lock.json +!bin/adf_codec/node_shim/package.json +!bin/adf_codec/node_shim/shim.mjs !bin/branch_chunks/ !bin/branch_chunks/*.py !bin/chunk-pr diff --git a/bin/adf_codec/__init__.py b/bin/adf_codec/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bin/adf_codec/convert.py b/bin/adf_codec/convert.py new file mode 100644 index 00000000..b282c3ed --- /dev/null +++ b/bin/adf_codec/convert.py @@ -0,0 +1,114 @@ +"""Converts markdown to ADF (Atlassian Document Format, the rich text of Jira Cloud and Confluence) and back. + +It runs @larvit/adf-codec's portable calls in a throwaway container, since adf-codec is a Node package: +`portableMarkdownToAdf` to write ADF and `adfToPortableMarkdown` to read it. The container holds node_shim/shim.mjs, +whose exit status says what happened: 0 answered, 2 adf-codec refused the input (its error code is in the Failure), 1 the shim itself failed. +""" +import dataclasses +import hashlib +import json +import pathlib +import subprocess +import typing + +from failure import Failure + +SHIM = pathlib.Path(__file__).resolve().parent / "node_shim" +SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs") +# Both limits are equal, so the container can't swap: a document too big for it ends in a Failure, not in a slow machine. +MEMORY = "256m" +RUN_SECONDS = 60 +BUILD_SECONDS = 300 +REFUSED = 2 + + +@dataclasses.dataclass(frozen=True) +class Ran: + status: int + out: str + err: str + + +# Runs the shim in the given mode (`to-adf` or `to-markdown`) on a text, or fails to start it. +Run = typing.Callable[[str, str], Ran | Failure] + + +def image_tag(shim: pathlib.Path) -> str: + """A tag that changes with every file the image is built from, so an edited shim or a bumped adf-codec rebuilds it.""" + digest = hashlib.sha256() + for name in SHIM_FILES: + digest.update((shim / name).read_bytes()) + return f"claude-config-adf-codec:{digest.hexdigest()[:12]}" + + +def docker_args(image: str, mode: str) -> list[str]: + return ["docker", "run", "--rm", "-i", "--memory", MEMORY, "--memory-swap", MEMORY, "--network", "none", "--read-only", image, mode] + + +def docker(args: list[str], text: str, seconds: int) -> Ran | Failure: + try: + done = subprocess.run(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds) + except FileNotFoundError: + return Failure("docker is not installed, and the adf-codec shim runs in a container") + except subprocess.TimeoutExpired: + return Failure(f"`{' '.join(args[:2])}` took more than {seconds} seconds") + return Ran(done.returncode, done.stdout, done.stderr) + + +def ensure_image(shim: pathlib.Path) -> str | Failure: + """The tag of the shim's image, built from node_shim/ first where this machine has no image under that tag.""" + tag = image_tag(shim) + found = docker(["docker", "image", "inspect", tag], "", RUN_SECONDS) + if isinstance(found, Failure) or found.status == 0: + return found if isinstance(found, Failure) else tag + built = docker(["docker", "build", "--quiet", "--memory", "1g", "--memory-swap", "1g", "--tag", tag, str(shim)], "", BUILD_SECONDS) + if isinstance(built, Failure): + return built + if built.status != 0: + return Failure(f"building the adf-codec shim's image failed with exit {built.status}: {built.err.strip()}") + return tag + + +def container_run(mode: str, text: str) -> Ran | Failure: + image = ensure_image(SHIM) + if isinstance(image, Failure): + return image + return docker(docker_args(image, mode), text, RUN_SECONDS) + + +def answer(mode: str, text: str, run: Run) -> str | Failure: + """What the shim printed for text, or a Failure saying why it printed none.""" + ran = run(mode, text) + if isinstance(ran, Failure): + return ran + if ran.status == 0: + return ran.out + if ran.status == REFUSED: + return refusal(ran.out) + return Failure(f"the adf-codec shim failed with exit {ran.status}: {ran.err.strip()}") + + +def refusal(out: str) -> Failure: + try: + error = json.loads(out)["error"] + where = f"line {error['position']['line']}" if "position" in error else ".".join(str(step) for step in error["path"]) or "the document" + return Failure(f"adf-codec refused the input with {error['code']}: {error['message']} (at {where})") + except (ValueError, KeyError, TypeError): + return Failure(f"the adf-codec shim refused with output that is not an error: {out.strip()}") + + +def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing.Any] | Failure: + """The ADF document markdown reads as.""" + out = answer("to-adf", markdown, run) + if isinstance(out, Failure): + return out + try: + document = json.loads(out) + except ValueError: + return Failure(f"the adf-codec shim printed output that is not JSON: {out.strip()}") + return document + + +def adf_to_markdown(document: dict[str, typing.Any], run: Run = container_run) -> str | Failure: + """The markdown an ADF document reads as.""" + return answer("to-markdown", json.dumps(document), run) diff --git a/bin/adf_codec/node_shim/Dockerfile b/bin/adf_codec/node_shim/Dockerfile new file mode 100644 index 00000000..f138df4d --- /dev/null +++ b/bin/adf_codec/node_shim/Dockerfile @@ -0,0 +1,7 @@ +FROM node:24.19.0-alpine3.24 +WORKDIR /shim +COPY package.json package-lock.json ./ +RUN npm ci --ignore-scripts --omit=dev +COPY shim.mjs ./ +USER node +ENTRYPOINT ["node", "shim.mjs"] diff --git a/bin/adf_codec/node_shim/package-lock.json b/bin/adf_codec/node_shim/package-lock.json new file mode 100644 index 00000000..d19e6c69 --- /dev/null +++ b/bin/adf_codec/node_shim/package-lock.json @@ -0,0 +1,22 @@ +{ + "name": "adf-codec-shim", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "adf-codec-shim", + "dependencies": { + "@larvit/adf-codec": "0.3.1" + } + }, + "node_modules/@larvit/adf-codec": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/@larvit/adf-codec/-/adf-codec-0.3.1.tgz", + "integrity": "sha512-hI15WrztaGU90nEc2+IeFj16LtqpTxb2hahdhfj/YvdFTwd2lzhdQN1MRG4/65i6FiEZR2lIlFxyIYqx6EXjvg==", + "license": "MIT", + "engines": { + "node": ">=18" + } + } + } +} diff --git a/bin/adf_codec/node_shim/package.json b/bin/adf_codec/node_shim/package.json new file mode 100644 index 00000000..a69fbe40 --- /dev/null +++ b/bin/adf_codec/node_shim/package.json @@ -0,0 +1,8 @@ +{ + "name": "adf-codec-shim", + "private": true, + "type": "module", + "dependencies": { + "@larvit/adf-codec": "0.3.1" + } +} diff --git a/bin/adf_codec/node_shim/shim.mjs b/bin/adf_codec/node_shim/shim.mjs new file mode 100644 index 00000000..6c889399 --- /dev/null +++ b/bin/adf_codec/node_shim/shim.mjs @@ -0,0 +1,38 @@ +// Converts between markdown and ADF with @larvit/adf-codec's portable calls. The mode is the one argument: +// to-adf stdin is markdown, stdout is the ADF document as JSON +// to-markdown stdin is an ADF document as JSON, stdout is markdown +// Exit 0: stdout holds the answer. Exit 2: adf-codec refused the input, and stdout holds {"error": {code, message, path, position?}}. +// Exit 1: the shim could not run, and stderr says why. +import { adfToPortableMarkdown, portableMarkdownToAdf } from '@larvit/adf-codec' + +const REFUSED = 2 + +async function readStdin() { + const chunks = [] + for await (const chunk of process.stdin) chunks.push(chunk) + return Buffer.concat(chunks).toString('utf8') +} + +function finish(result, render) { + if (result.ok) { + process.stdout.write(render(result.value)) + return 0 + } + process.stdout.write(JSON.stringify({ error: result.error })) + return REFUSED +} + +async function main(mode) { + const input = await readStdin() + if (mode === 'to-adf') return finish(portableMarkdownToAdf(input), (doc) => JSON.stringify(doc)) + if (mode === 'to-markdown') return finish(adfToPortableMarkdown(JSON.parse(input)), (markdown) => markdown) + throw new Error(`mode must be to-adf or to-markdown, not ${mode}`) +} + +main(process.argv[2]).then( + (code) => { process.exitCode = code }, + (failure) => { + process.stderr.write(`${failure instanceof Error ? failure.message : failure}\n`) + process.exitCode = 1 + }, +) diff --git a/bin/adf_codec/test_adf_codec.py b/bin/adf_codec/test_adf_codec.py new file mode 100644 index 00000000..3293afd2 --- /dev/null +++ b/bin/adf_codec/test_adf_codec.py @@ -0,0 +1,98 @@ +import json +import shutil +import subprocess + +import pytest + +from adf_codec import convert +from failure import Failure + +DOC = {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": "Hi"}]}]} + + +def fake(status: int, out: str = "", err: str = ""): + calls: list[tuple[str, str]] = [] + + def run(mode: str, text: str) -> convert.Ran: + calls.append((mode, text)) + return convert.Ran(status, out, err) + + run.calls = calls # type: ignore[attr-defined] + return run + + +def test_markdown_to_adf_returns_the_document_the_shim_printed(): + run = fake(0, json.dumps(DOC)) + assert convert.markdown_to_adf("Hi\n", run) == DOC + assert run.calls == [("to-adf", "Hi\n")] + + +def test_adf_to_markdown_sends_the_document_as_json_and_returns_the_text(): + run = fake(0, "Hi\n") + assert convert.adf_to_markdown(DOC, run) == "Hi\n" + assert run.calls == [("to-markdown", json.dumps(DOC))] + + +def test_a_refusal_becomes_a_failure_with_adf_codecs_code_and_message(): + refusal = {"error": {"code": "malformed-pipe-table", "message": "row has too many cells", "path": [], "position": {"line": 3, "offset": 9}}} + failure = convert.markdown_to_adf("| a |\n", fake(2, json.dumps(refusal))) + assert failure == Failure("adf-codec refused the input with malformed-pipe-table: row has too many cells (at line 3)") + + +def test_a_refusal_without_a_position_names_the_path(): + refusal = {"error": {"code": "unsupported-node-shape", "message": "bad node", "path": ["content", 0]}} + failure = convert.adf_to_markdown(DOC, fake(2, json.dumps(refusal))) + assert failure == Failure("adf-codec refused the input with unsupported-node-shape: bad node (at content.0)") + + +def test_a_shim_that_could_not_run_becomes_a_failure_with_its_stderr(): + failure = convert.adf_to_markdown(DOC, fake(1, "", "Unexpected token")) + assert failure == Failure("the adf-codec shim failed with exit 1: Unexpected token") + + +def test_a_container_that_was_killed_for_memory_says_so(): + failure = convert.markdown_to_adf("x", fake(137)) + assert isinstance(failure, Failure) and "137" in failure.reason + + +def test_output_that_is_not_json_becomes_a_failure(): + failure = convert.markdown_to_adf("x", fake(0, "not json")) + assert failure == Failure("the adf-codec shim printed output that is not JSON: not json") + + +def test_a_refusal_that_is_not_the_expected_json_becomes_a_failure(): + failure = convert.markdown_to_adf("x", fake(2, "oops")) + assert failure == Failure("the adf-codec shim refused with output that is not an error: oops") + + +def test_a_failure_from_the_runner_passes_through(): + def run(mode: str, text: str) -> Failure: + return Failure("docker is not installed") + + assert convert.markdown_to_adf("x", run) == Failure("docker is not installed") + + +def test_the_container_has_a_memory_and_swap_limit_and_no_network(): + args = convert.docker_args("image:1", "to-adf") + assert args[:3] == ["docker", "run", "--rm"] + assert args[args.index("--memory") + 1] == args[args.index("--memory-swap") + 1] + assert args[args.index("--network") + 1] == "none" + assert args[-2:] == ["image:1", "to-adf"] + + +def test_the_image_tag_changes_with_the_shim_files(tmp_path): + for name in convert.SHIM_FILES: + (tmp_path / name).write_text("a") + before = convert.image_tag(tmp_path) + (tmp_path / "shim.mjs").write_text("b") + assert convert.image_tag(tmp_path) != before + + +@pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True).returncode != 0, reason="needs a docker daemon") +def test_round_trip_through_the_real_container(): + doc = convert.markdown_to_adf("# Title\n\nSome **bold** text.\n") + assert isinstance(doc, dict) and doc["type"] == "doc" and doc["content"][0]["type"] == "heading" + markdown = convert.adf_to_markdown(doc) + assert isinstance(markdown, str) and markdown.startswith("# Title") and "**bold**" in markdown + refused = convert.adf_to_markdown({"type": "nope"}) + assert isinstance(refused, Failure) and "not-an-adf-document" in refused.reason -- 2.52.0 From 8a50a676a4e4e0b2491014f481e38f36ca8296d1 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:40:46 +0100 Subject: [PATCH 3/8] Inject the docker runner into the adf_codec tool and test its image and container calls --- bin/adf_codec/convert.py | 18 +++--- bin/adf_codec/test_adf_codec.py | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 8 deletions(-) diff --git a/bin/adf_codec/convert.py b/bin/adf_codec/convert.py index b282c3ed..b1eba9f2 100644 --- a/bin/adf_codec/convert.py +++ b/bin/adf_codec/convert.py @@ -31,6 +31,8 @@ class Ran: # Runs the shim in the given mode (`to-adf` or `to-markdown`) on a text, or fails to start it. Run = typing.Callable[[str, str], Ran | Failure] +# Runs a docker command line on a text, in at most the given seconds, or fails to start it. +Docker = typing.Callable[[list[str], str, int], Ran | Failure] def image_tag(shim: pathlib.Path) -> str: @@ -45,9 +47,9 @@ def docker_args(image: str, mode: str) -> list[str]: return ["docker", "run", "--rm", "-i", "--memory", MEMORY, "--memory-swap", MEMORY, "--network", "none", "--read-only", image, mode] -def docker(args: list[str], text: str, seconds: int) -> Ran | Failure: +def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any] = subprocess.run) -> Ran | Failure: try: - done = subprocess.run(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds) + done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds) except FileNotFoundError: return Failure("docker is not installed, and the adf-codec shim runs in a container") except subprocess.TimeoutExpired: @@ -55,13 +57,13 @@ def docker(args: list[str], text: str, seconds: int) -> Ran | Failure: return Ran(done.returncode, done.stdout, done.stderr) -def ensure_image(shim: pathlib.Path) -> str | Failure: +def ensure_image(shim: pathlib.Path, run_docker: Docker = docker) -> str | Failure: """The tag of the shim's image, built from node_shim/ first where this machine has no image under that tag.""" tag = image_tag(shim) - found = docker(["docker", "image", "inspect", tag], "", RUN_SECONDS) + found = run_docker(["docker", "image", "inspect", tag], "", RUN_SECONDS) if isinstance(found, Failure) or found.status == 0: return found if isinstance(found, Failure) else tag - built = docker(["docker", "build", "--quiet", "--memory", "1g", "--memory-swap", "1g", "--tag", tag, str(shim)], "", BUILD_SECONDS) + built = run_docker(["docker", "build", "--quiet", "--memory", "1g", "--memory-swap", "1g", "--tag", tag, str(shim)], "", BUILD_SECONDS) if isinstance(built, Failure): return built if built.status != 0: @@ -69,11 +71,11 @@ def ensure_image(shim: pathlib.Path) -> str | Failure: return tag -def container_run(mode: str, text: str) -> Ran | Failure: - image = ensure_image(SHIM) +def container_run(mode: str, text: str, run_docker: Docker = docker) -> Ran | Failure: + image = ensure_image(SHIM, run_docker) if isinstance(image, Failure): return image - return docker(docker_args(image, mode), text, RUN_SECONDS) + return run_docker(docker_args(image, mode), text, RUN_SECONDS) def answer(mode: str, text: str, run: Run) -> str | Failure: diff --git a/bin/adf_codec/test_adf_codec.py b/bin/adf_codec/test_adf_codec.py index 3293afd2..5f7a89e2 100644 --- a/bin/adf_codec/test_adf_codec.py +++ b/bin/adf_codec/test_adf_codec.py @@ -1,4 +1,5 @@ import json +import re import shutil import subprocess @@ -88,6 +89,106 @@ def test_the_image_tag_changes_with_the_shim_files(tmp_path): assert convert.image_tag(tmp_path) != before +def test_the_container_command_line_is_exactly_this(): + assert convert.docker_args("image:1", "to-adf") == [ + "docker", "run", "--rm", "-i", "--memory", "256m", "--memory-swap", "256m", "--network", "none", "--read-only", "image:1", "to-adf", + ] + + +def test_a_refusal_at_the_root_says_the_document(): + refusal = {"error": {"code": "not-an-adf-document", "message": "no type", "path": []}} + failure = convert.adf_to_markdown(DOC, fake(2, json.dumps(refusal))) + assert failure == Failure("adf-codec refused the input with not-an-adf-document: no type (at the document)") + + +def test_the_image_tag_is_a_12_digit_hash_of_the_shim_files(tmp_path): + for name in convert.SHIM_FILES: + (tmp_path / name).write_text("a") + assert re.fullmatch(r"claude-config-adf-codec:[0-9a-f]{12}", convert.image_tag(tmp_path)) + + +def test_docker_passes_the_text_on_stdin_and_returns_the_output(): + seen = {} + + def call(args, **kwargs): + seen.update(kwargs, args=args) + return subprocess.CompletedProcess(args, 3, "out", "err") + + assert convert.docker(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err") + assert seen == {"args": ["docker", "x"], "input": "text", "capture_output": True, "text": True, "encoding": "utf-8", "timeout": 7} + + +def test_docker_missing_becomes_a_failure(): + def call(args, **kwargs): + raise FileNotFoundError + + failure = convert.docker(["docker", "x"], "", 7, call) + assert failure == Failure("docker is not installed, and the adf-codec shim runs in a container") + + +def test_docker_that_takes_too_long_becomes_a_failure(): + def call(args, **kwargs): + raise subprocess.TimeoutExpired(args, 7) + + assert convert.docker(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds") + + +def shim_dir(tmp_path): + for name in convert.SHIM_FILES: + (tmp_path / name).write_text("a") + return tmp_path + + +def scripted(*answers): + lines: list[tuple[list[str], str, int]] = [] + queue = list(answers) + + def run_docker(args, text, seconds): + lines.append((args, text, seconds)) + return queue.pop(0) + + run_docker.lines = lines # type: ignore[attr-defined] + return run_docker + + +def test_an_image_that_exists_is_not_built(tmp_path): + run_docker = scripted(convert.Ran(0, "", "")) + tag = convert.image_tag(shim_dir(tmp_path)) + assert convert.ensure_image(tmp_path, run_docker) == tag + assert run_docker.lines == [(["docker", "image", "inspect", tag], "", convert.RUN_SECONDS)] + + +def test_a_missing_image_is_built_from_the_shim_folder_with_a_memory_limit(tmp_path): + run_docker = scripted(convert.Ran(1, "", ""), convert.Ran(0, "", "")) + tag = convert.image_tag(shim_dir(tmp_path)) + assert convert.ensure_image(tmp_path, run_docker) == tag + assert run_docker.lines[1] == (["docker", "build", "--quiet", "--memory", "1g", "--memory-swap", "1g", "--tag", tag, str(tmp_path)], "", convert.BUILD_SECONDS) + + +def test_a_failed_build_becomes_a_failure_with_its_stderr(tmp_path): + run_docker = scripted(convert.Ran(1, "", ""), convert.Ran(5, "", "no network\n")) + shim_dir(tmp_path) + assert convert.ensure_image(tmp_path, run_docker) == Failure("building the adf-codec shim's image failed with exit 5: no network") + + +def test_a_failure_to_start_docker_passes_through_inspect_and_build(tmp_path): + shim_dir(tmp_path) + down = Failure("docker is not installed") + assert convert.ensure_image(tmp_path, scripted(down)) == down + assert convert.ensure_image(tmp_path, scripted(convert.Ran(1, "", ""), down)) == down + + +def test_container_run_runs_the_image_on_the_text(): + run_docker = scripted(convert.Ran(0, "", ""), convert.Ran(0, "answer", "")) + assert convert.container_run("to-adf", "text", run_docker) == convert.Ran(0, "answer", "") + assert run_docker.lines[1] == (convert.docker_args(convert.image_tag(convert.SHIM), "to-adf"), "text", convert.RUN_SECONDS) + + +def test_container_run_passes_an_image_failure_through(): + down = Failure("docker is not installed") + assert convert.container_run("to-adf", "text", scripted(down)) == down + + @pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True).returncode != 0, reason="needs a docker daemon") def test_round_trip_through_the_real_container(): doc = convert.markdown_to_adf("# Title\n\nSome **bold** text.\n") -- 2.52.0 From 4eed9114e5589d667cbc9144aea1878955ab7032 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:46:50 +0100 Subject: [PATCH 4/8] Pass the shim folder into container_run so its test needs no built shim --- bin/adf_codec/convert.py | 4 ++-- bin/adf_codec/test_adf_codec.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/adf_codec/convert.py b/bin/adf_codec/convert.py index b1eba9f2..a2c8bbdc 100644 --- a/bin/adf_codec/convert.py +++ b/bin/adf_codec/convert.py @@ -71,8 +71,8 @@ def ensure_image(shim: pathlib.Path, run_docker: Docker = docker) -> str | Failu return tag -def container_run(mode: str, text: str, run_docker: Docker = docker) -> Ran | Failure: - image = ensure_image(SHIM, run_docker) +def container_run(mode: str, text: str, run_docker: Docker = docker, shim: pathlib.Path = SHIM) -> Ran | Failure: + image = ensure_image(shim, run_docker) if isinstance(image, Failure): return image return run_docker(docker_args(image, mode), text, RUN_SECONDS) diff --git a/bin/adf_codec/test_adf_codec.py b/bin/adf_codec/test_adf_codec.py index 5f7a89e2..8a910835 100644 --- a/bin/adf_codec/test_adf_codec.py +++ b/bin/adf_codec/test_adf_codec.py @@ -178,15 +178,15 @@ def test_a_failure_to_start_docker_passes_through_inspect_and_build(tmp_path): assert convert.ensure_image(tmp_path, scripted(convert.Ran(1, "", ""), down)) == down -def test_container_run_runs_the_image_on_the_text(): +def test_container_run_runs_the_image_on_the_text(tmp_path): run_docker = scripted(convert.Ran(0, "", ""), convert.Ran(0, "answer", "")) - assert convert.container_run("to-adf", "text", run_docker) == convert.Ran(0, "answer", "") - assert run_docker.lines[1] == (convert.docker_args(convert.image_tag(convert.SHIM), "to-adf"), "text", convert.RUN_SECONDS) + assert convert.container_run("to-adf", "text", run_docker, shim_dir(tmp_path)) == convert.Ran(0, "answer", "") + assert run_docker.lines[1] == (convert.docker_args(convert.image_tag(tmp_path), "to-adf"), "text", convert.RUN_SECONDS) -def test_container_run_passes_an_image_failure_through(): +def test_container_run_passes_an_image_failure_through(tmp_path): down = Failure("docker is not installed") - assert convert.container_run("to-adf", "text", scripted(down)) == down + assert convert.container_run("to-adf", "text", scripted(down), shim_dir(tmp_path)) == down @pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True).returncode != 0, reason="needs a docker daemon") -- 2.52.0 From 0f2cb3557f087a7b21a626bddf2e14aa1204e77b Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:56:58 +0100 Subject: [PATCH 5/8] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=204=20=F0=9F=9F=A1=20(0=20misread);=20stop=20a=20?= =?UTF-8?q?slow=20shim=20inside=20the=20container,=20test=20a=20crashing?= =?UTF-8?q?=20shim,=20and=20drop=20two=20tests=20that=20repeated=20others?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/adf_codec/node_shim/Dockerfile | 4 +++- bin/adf_codec/test_adf_codec.py | 21 ++++----------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/bin/adf_codec/node_shim/Dockerfile b/bin/adf_codec/node_shim/Dockerfile index f138df4d..eeca65e9 100644 --- a/bin/adf_codec/node_shim/Dockerfile +++ b/bin/adf_codec/node_shim/Dockerfile @@ -4,4 +4,6 @@ COPY package.json package-lock.json ./ RUN npm ci --ignore-scripts --omit=dev COPY shim.mjs ./ USER node -ENTRYPOINT ["node", "shim.mjs"] +# The deadline is inside the container, since a timeout in the caller kills only the docker client and leaves the container running. +# It is under convert.py's RUN_SECONDS, so it fires first. +ENTRYPOINT ["timeout", "50", "node", "shim.mjs"] diff --git a/bin/adf_codec/test_adf_codec.py b/bin/adf_codec/test_adf_codec.py index 8a910835..470b288f 100644 --- a/bin/adf_codec/test_adf_codec.py +++ b/bin/adf_codec/test_adf_codec.py @@ -1,5 +1,4 @@ import json -import re import shutil import subprocess @@ -51,7 +50,7 @@ def test_a_shim_that_could_not_run_becomes_a_failure_with_its_stderr(): assert failure == Failure("the adf-codec shim failed with exit 1: Unexpected token") -def test_a_container_that_was_killed_for_memory_says_so(): +def test_a_shim_killed_for_memory_becomes_a_failure_with_its_exit_status(): failure = convert.markdown_to_adf("x", fake(137)) assert isinstance(failure, Failure) and "137" in failure.reason @@ -73,14 +72,6 @@ def test_a_failure_from_the_runner_passes_through(): assert convert.markdown_to_adf("x", run) == Failure("docker is not installed") -def test_the_container_has_a_memory_and_swap_limit_and_no_network(): - args = convert.docker_args("image:1", "to-adf") - assert args[:3] == ["docker", "run", "--rm"] - assert args[args.index("--memory") + 1] == args[args.index("--memory-swap") + 1] - assert args[args.index("--network") + 1] == "none" - assert args[-2:] == ["image:1", "to-adf"] - - def test_the_image_tag_changes_with_the_shim_files(tmp_path): for name in convert.SHIM_FILES: (tmp_path / name).write_text("a") @@ -101,12 +92,6 @@ def test_a_refusal_at_the_root_says_the_document(): assert failure == Failure("adf-codec refused the input with not-an-adf-document: no type (at the document)") -def test_the_image_tag_is_a_12_digit_hash_of_the_shim_files(tmp_path): - for name in convert.SHIM_FILES: - (tmp_path / name).write_text("a") - assert re.fullmatch(r"claude-config-adf-codec:[0-9a-f]{12}", convert.image_tag(tmp_path)) - - def test_docker_passes_the_text_on_stdin_and_returns_the_output(): seen = {} @@ -189,7 +174,7 @@ def test_container_run_passes_an_image_failure_through(tmp_path): assert convert.container_run("to-adf", "text", scripted(down), shim_dir(tmp_path)) == down -@pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True).returncode != 0, reason="needs a docker daemon") +@pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True, timeout=20).returncode != 0, reason="needs a docker daemon") def test_round_trip_through_the_real_container(): doc = convert.markdown_to_adf("# Title\n\nSome **bold** text.\n") assert isinstance(doc, dict) and doc["type"] == "doc" and doc["content"][0]["type"] == "heading" @@ -197,3 +182,5 @@ def test_round_trip_through_the_real_container(): assert isinstance(markdown, str) and markdown.startswith("# Title") and "**bold**" in markdown refused = convert.adf_to_markdown({"type": "nope"}) assert isinstance(refused, Failure) and "not-an-adf-document" in refused.reason + crashed = convert.answer("to-markdown", "not json", convert.container_run) + assert isinstance(crashed, Failure) and crashed.reason.startswith("the adf-codec shim failed with exit 1: ") -- 2.52.0 From 2fb0e7be5af70ae37702b97e00f0582ec496ba3b Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:56:58 +0100 Subject: [PATCH 6/8] =?UTF-8?q?Answer=20prose=20round=201:=201=20?= =?UTF-8?q?=F0=9F=94=B4,=203=20=F0=9F=9F=A1=20(0=20misread);=20sort=20the?= =?UTF-8?q?=20README=20entry,=20and=20reword=20the=20docstrings=20and=20co?= =?UTF-8?q?mments=20the=20reviewer=20misread?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- bin/adf_codec/convert.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7484b458..776fbafa 100644 --- a/README.md +++ b/README.md @@ -165,9 +165,9 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a `bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `branch_chunks/`, `claude_code/`, `command_lines/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `mattermost_dm/`, `model_routing/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool. +- `adf_codec/` converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back, in a throwaway container running a pinned `@larvit/adf-codec`. - `branch_chunks/` cuts a default branch into chunks and finds, for each, the items it shipped and the later chunks that fixed its code, for `score_outcomes` and `routing_figures`. It imports `reading_cost`'s record path. - `command_lines/` reads a shell command: its simple commands, and a docker call's subcommand and memory limits, for `command_patterns`, `reading_cost`, `routing_figures`, `rule_hook` and `run_heavy`. -- `adf_codec/` converts markdown to ADF (the rich text of Jira Cloud and Confluence) and back, with `@larvit/adf-codec`'s portable calls in a throwaway container: `convert.py` is the Python side, `node_shim/` the pinned Node shim and its Dockerfile. No tool calls it yet. - `chunk_pr` imports `deploy_release`'s forge calls, and `forge_of` as `deploy_release` does. - `deploy_release` imports `chunk_preflight`'s `forge_of`, which tells a GitHub origin from a Gitea one, and `release_plan`'s `release_standing`. - `git_cli/` runs git for every tool (`git_repo.py`), and reads the git calls in a shell command (`git_command.py`). diff --git a/bin/adf_codec/convert.py b/bin/adf_codec/convert.py index a2c8bbdc..4bc4b482 100644 --- a/bin/adf_codec/convert.py +++ b/bin/adf_codec/convert.py @@ -1,8 +1,6 @@ """Converts markdown to ADF (Atlassian Document Format, the rich text of Jira Cloud and Confluence) and back. -It runs @larvit/adf-codec's portable calls in a throwaway container, since adf-codec is a Node package: -`portableMarkdownToAdf` to write ADF and `adfToPortableMarkdown` to read it. The container holds node_shim/shim.mjs, -whose exit status says what happened: 0 answered, 2 adf-codec refused the input (its error code is in the Failure), 1 the shim itself failed. +It runs @larvit/adf-codec, a Node package, in a throwaway container that holds node_shim/shim.mjs; the shim's header says what its exit status means. """ import dataclasses import hashlib @@ -15,7 +13,7 @@ from failure import Failure SHIM = pathlib.Path(__file__).resolve().parent / "node_shim" SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs") -# Both limits are equal, so the container can't swap: a document too big for it ends in a Failure, not in a slow machine. +# `--memory` and `--memory-swap` both take this, so the container can't swap: a document too big for it ends in a Failure. MEMORY = "256m" RUN_SECONDS = 60 BUILD_SECONDS = 300 @@ -29,14 +27,14 @@ class Ran: err: str -# Runs the shim in the given mode (`to-adf` or `to-markdown`) on a text, or fails to start it. +# Runs the shim in the given mode (`to-adf` or `to-markdown`) on a text, or fails to run it. Run = typing.Callable[[str, str], Ran | Failure] -# Runs a docker command line on a text, in at most the given seconds, or fails to start it. +# Runs a docker command line on a text, in at most the given seconds, or fails to run it. Docker = typing.Callable[[list[str], str, int], Ran | Failure] def image_tag(shim: pathlib.Path) -> str: - """A tag that changes with every file the image is built from, so an edited shim or a bumped adf-codec rebuilds it.""" + """A tag that changes with every file the image is built from, so an edited shim or a bumped adf-codec builds a new image.""" digest = hashlib.sha256() for name in SHIM_FILES: digest.update((shim / name).read_bytes()) @@ -58,7 +56,7 @@ def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., def ensure_image(shim: pathlib.Path, run_docker: Docker = docker) -> str | Failure: - """The tag of the shim's image, built from node_shim/ first where this machine has no image under that tag.""" + """The tag of the image for the shim folder, built from that folder first where this machine has no image under that tag.""" tag = image_tag(shim) found = run_docker(["docker", "image", "inspect", tag], "", RUN_SECONDS) if isinstance(found, Failure) or found.status == 0: @@ -79,7 +77,7 @@ def container_run(mode: str, text: str, run_docker: Docker = docker, shim: pathl def answer(mode: str, text: str, run: Run) -> str | Failure: - """What the shim printed for text, or a Failure saying why it printed none.""" + """What the shim printed for text, or a Failure saying why it gave no answer.""" ran = run(mode, text) if isinstance(ran, Failure): return ran @@ -100,7 +98,7 @@ def refusal(out: str) -> Failure: def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing.Any] | Failure: - """The ADF document markdown reads as.""" + """The ADF document for the markdown.""" out = answer("to-adf", markdown, run) if isinstance(out, Failure): return out @@ -112,5 +110,5 @@ def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing def adf_to_markdown(document: dict[str, typing.Any], run: Run = container_run) -> str | Failure: - """The markdown an ADF document reads as.""" + """The markdown for the ADF document.""" return answer("to-markdown", json.dumps(document), run) -- 2.52.0 From 969002d118e87adb6c95def66388217f58ea1087 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:57:05 +0100 Subject: [PATCH 7/8] =?UTF-8?q?Answer=20security=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=200=20=F0=9F=9F=A1=20(0=20misread);=20the=20revie?= =?UTF-8?q?w=20found=20nothing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- 2.52.0 From 728030b39a2da49d4760fad6b5d98c32578be778 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 14 Nov 2026 16:57:11 +0100 Subject: [PATCH 8/8] Finish item 349: the adf_codec package --- CHANGELOG.md | 1 + todo.md | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8082844..bd805987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- A new `adf_codec` package converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back with `@larvit/adf-codec` 0.3.1. It runs in a pinned Node container with a memory limit and no network, and returns adf-codec's error codes as a `Failure`. No tool calls it yet. - The new `todo-item list [--release ]` prints the plan's items in the plan's order, `todo-item edit ` sets an item's cells or detail, and `todo-item move --release ` moves it to another release; each change ranks the plan. Agents now read and change the plan's items only with `todo-item`, and the tools read the plan as it stands through one store, `bin/plan_store`, so a later backend can keep a plan outside `todo.md`. `todo-item list --release` with a bad version exits 2, and `finish --no-changelog` also drops an item no chunk built. - A project's config may name the language its prose is written in (`language`) and templates for its commit titles, pull request titles and branch names (`commit-title`, `pull-request-title`, `branch`), in which `{id}` stands for the plan item's ID and `{summary}` for the one-line summary. The tools and the prose reviewer follow them. On a branch that fits the `branch` template, rule-hook's new `commit-title` rule refuses a commit title that doesn't fit, and `chunk-pr` such a pull request title. While a project's config holds a fault, rule-hook refuses every commit there. - `chunk-pr merge` waits for a human's approval of the pull request where the project's config sets `wait-for-approval = true`: it merges once GitHub reports the pull request approved, and gives up after 90 minutes. It reads approvals on GitHub only. Set it for a client whose pull requests need another person's approval. diff --git a/todo.md b/todo.md index 3d9d5df8..087b5b90 100644 --- a/todo.md +++ b/todo.md @@ -12,15 +12,14 @@ | ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | |---|---|---|---|---|---|---|---|---|---|---|---| -| 349 | 0.11.0 | | | **Convert markdown to ADF and back with `@larvit/adf-codec`'s portable calls, through a pinned Node shim run in a container with a memory limit.** | 2 | 1 | 2 | 3 | 2 | 1.6 by 3 | 1.1 | -| 351 | 0.11.0 | 349 | | **Read a project's plan from the child issues of a Jira Cloud epic its config names, ranked in Jira's backlog order.** | 3 | 2 | 3 | 6 | 5 | 1.6 by 6 | 1.2 | +| 351 | 0.11.0 | | | **Read a project's plan from the child issues of a Jira Cloud epic its config names, ranked in Jira's backlog order.** | 3 | 2 | 3 | 6 | 5 | 1.6 by 6 | 1.2 | | 353 | 0.11.0 | 351 | | **Let a project's config take its goals and audience from a Jira epic's description, under the same two headings.** | 2 | 2 | 3 | 5 | 3 | 1.6 by 4 | 1.3 | | 358 | 0.11.0 | | | **Let a project's config allow its runtime and tests on the host, and set the comprehension score under which code health is very important.** | 2 | 2 | 3 | 5 | 3 | 1.6 by 4 | 1.3 | | 362 | 0.11.0 | | | **Send loop-claude's notices through the notifier a project's config names, with Mattermost as the first backend.** | 2 | 1 | 2 | 3 | 3 | 1.6 by 3 | 1.0 | | 363 | 0.11.0 | 362 | | **Add a Slack backend to the notifier, sending each notice as a DM from a bot.** | 2 | 1 | 2 | 3 | 3 | 1.6 by 4 | 1.3 | | 373 | 0.11.0 | | | **Pick the next release's chunks that may run at once by the files and the parts of files each is predicted to touch, and record the prediction beside what each merged chunk touched.** | 3 | 2 | 2 | 5 | 5 | 1.5 by 4, 1.4 by 2 | 1.3 | | 352 | 0.11.0 | 351 | | **Write a project's plan to its Jira epic: file items as child issues, edit those the loop filed, score unscored issues with a comment, and move a finished item to the status its config names.** | 3 | 2 | 3 | 6 | 5 | 1.6 by 6 | 1.2 | -| 355 | 0.11.0 | 349 | | **Store a record on the Confluence Cloud page its config names, as markdown converted through the adf-codec shim, and interrupt the human when a teammate saved the page since the loop read it.** | 3 | 2 | 3 | 6 | 5 | 1.6 by 6 | 1.2 | +| 355 | 0.11.0 | | | **Store a record on the Confluence Cloud page its config names, as markdown converted through the adf-codec shim, and interrupt the human when a teammate saved the page since the loop read it.** | 3 | 2 | 3 | 6 | 5 | 1.6 by 6 | 1.2 | | 356 | 0.11.0 | | | **Hand reviewers the review standards from the folder a project's config names, in place of `~/.claude/skills/larv-review/standards/`.** | 2 | 2 | 3 | 5 | 2 | 1.6 by 3 | 1.1 | | 372 | 0.11.0 | | | **Key loop-claude's session, archive, routing state and config-folder commits per chunk, so two chunks of one project run side by side without touching each other's state.** | 3 | 2 | 3 | 6 | 6 | 1.5 by 3 | 0.4 | | 374 | 0.11.0 | 372 | | **Merge open chunks one at a time: rebase, finish the item in todo.md and CHANGELOG.md, rerun the gate and the paired ruling, and keep a chunk waiting for approval in the queue without blocking the next.** | 4 | 2 | 3 | 7 | 6 | 1.5 by 4, 1.6 by 3 | 1.0 | @@ -84,10 +83,6 @@ ## Details -### 349. Convert markdown to ADF and back with `@larvit/adf-codec`'s portable calls, through a pinned Node shim run in a container with a memory limit. - -lilleman's call, 2026-11-12: Jira Cloud descriptions and Confluence pages are ADF, and the loop keeps working in markdown. Write with `portableMarkdownToAdf` and read with `adfToPortableMarkdown`. The shim reads stdin and writes stdout, runs in a `node:` image with a pinned adf-codec version, and returns adf-codec's error codes as a Failure. - ### 351. Read a project's plan from the child issues of a Jira Cloud epic its config names, ranked in Jira's backlog order. lilleman's calls, 2026-11-12. One epic is one project's plan. An item is a child issue: its key is the item's ID, so branches, worktrees and commit and PR titles follow `CLAUDE.md`'s ticket rules (`ABC-123`, `ABC-123 - ...`). Summary is the tagline, description the detail (ADF, read through the adf-codec shim). Release is the Fix Version, or what item 359 configures. Blocked by is the "is blocked by" link. L, E, I, S, Moves and Exempt are issue entity properties, so no Jira admin is needed; the computed score also shows as a label such as `score-2.4`. Jira's backlog rank decides the order, since the team sets it; the loop's score doesn't break ties, and the loop doesn't reorder anything others filed. Auth: email plus API token from the project's env file. -- 2.52.0