Start a fresh session where its transcript is empty #722

Merged
lilleman merged 8 commits from item-539 into main 2026-11-26 01:36:08 +01:00
7 changed files with 105 additions and 20 deletions
+1
View File
@@ -2,6 +2,7 @@
## Unreleased
- loop-claude starts a chunk on a fresh session where its session's transcript is empty, in place of resuming a session claude cannot find, which failed three times and waited for the human.
- larv-review's prose pass takes a second round after a fix that rewrites a claim a command or a source file can check, such as generated output, a number or a licence term.
- `deploy-release --cut-patch <commits>` cuts the newest tag's next patch release, such as 0.15.2 after v0.15.1, holding only the commits named, for a fix that must ship before main's other unreleased work. It runs `ci.sh` on the patch, tags and pushes it, merges the tag back into main through a pull request, and moves `~/.claude` to it. Plain `--cut` now refuses a patch release, and a release review counts only for a release later than the last cut.
- `task-bench` plants bugs in Rust files too, from the mutants cargo-mutants catches, and tells Haiku the panic message `cargo test` prints.
+14 -4
View File
@@ -7,7 +7,7 @@ from transcript_archive.write import archive
from agent_cycle.worker import AUTO_COMPACT_WINDOW, COMPACT_WINDOW, run_worker, worker_args
from claude_code.output_limits import BASH_CEILING, BASH_CEILING_CHARACTERS
from agent_cycle.judge import Brief, judge, Rejected
from claude_code.session import delete_session, Session, session_open, transcript_file
from claude_code.session import delete_session, discard_empty_transcript, Session, session_open, transcript_file
from loop_effects.effects import Effects, say
from claude_code.entries import Entry
from failure import Failure
@@ -15,6 +15,7 @@ from agent_cycle.verdicts import Finished, Interrupt, Judged, QuotedAnswer, Take
from agent_cycle.interrupt import interrupt, Talked, WorkerSession
from transcript_archive.judge_run import JudgeRun
from transcript_archive.read import last_spent
from claude_code.reply import Limited
from agent_cycle.routing import next_route, step_up, StepUp
from transcript_archive import outcomes
from model_routing.ladder import ProjectRung, Rung
@@ -121,8 +122,7 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> WorkerRan | Stop:
if isinstance(answered, QuotedAnswer):
say(f"\n=== {cycle.prompts.quote_sources} answered: {answered.question} ===\n{answered.answer}", cycle.effects, stdout=True)
# worker_start runs inside the lambda: a failed attempt may have opened the session.
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, stepped), worker_environment(cycle), cycle.project_archive, cycle.project.rung if cycle.project else cycle.route.rung, cycle.effects), cycle.effects):
if stop := retried("worker", lambda: started_worker(cycle, answered, stepped), cycle.effects):
return stop
path = transcript_file(cycle.session)
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
@@ -137,13 +137,23 @@ def worker_session(cycle: Cycle, transcript_path: str, stepped_from: Rung | None
return WorkerSession(cycle.root, cycle.session.id, cycle.route.rung, stepped_from or cycle.route.rung, transcript_path, worker_environment(cycle))
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> list[str]:
def started_worker(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> Failure | Limited | None:
"""One run of the worker. worker_start runs on each attempt: a failed one may have opened the session, or left it empty."""
args = worker_start(cycle, answered, stepped)
if isinstance(args, Failure):
return args
return run_worker(cycle.root, args, worker_environment(cycle), cycle.project_archive, cycle.project.rung if cycle.project else cycle.route.rung, cycle.effects)
def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: bool) -> list[str] | Failure:
if isinstance(answered, QuotedAnswer):
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.answered(answered.question, answered.answer, cycle.route, cycle.project, stepped), resume=True)
if isinstance(answered, TypedAnswer):
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.typed(answered.question, answered.answer, cycle.route, cycle.project, stepped), resume=True)
if session_open(cycle.session):
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.resumed(cycle.unfinished_evidence, cycle.route, cycle.project), resume=True)
if failure := discard_empty_transcript(cycle.session):
return failure
return worker_args(cycle.session, cycle.route.rung, cycle.prompts.fresh(cycle.task, cycle.premises, cycle.route, cycle.project, cycle.facts), resume=False)
+37
View File
@@ -172,6 +172,43 @@ Worker: {OFF_LADDER}
self.assertEqual(workers[1][workers[1].index("-p") + 1], "Resumed: working")
self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: unfinished\n\nEvidence:\n> working\n\nConfig: {CONFIG}\nJudge: judge-model\nWorker: {OFF_LADDER}\n"] * 2)
def test_a_session_whose_transcript_is_empty_starts_fresh_with_its_session_id(self):
path = self.paths.claude_code_transcripts / "-fake" / f"{self.session.id}.jsonl"
path.parent.mkdir(parents=True)
path.touch()
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working"))
worker, = fake.made("stream_claude")
self.assertIn("--session-id", worker)
self.assertNotIn("--resume", worker)
def test_a_worker_that_fails_leaving_an_empty_transcript_is_retried_with_a_fresh_session(self):
path = self.paths.claude_code_transcripts / "-fake" / f"{self.session.id}.jsonl"
def fails_leaving_it_empty(args):
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
return Streamed(1)
fake = FakeEffects(streams=[fails_leaving_it_empty, Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("finished")])
with contextlib.redirect_stderr(io.StringIO()) as said:
self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working"))
self.assertIn("retrying the worker in a minute", said.getvalue())
self.assertEqual(["--session-id" in worker and "--resume" not in worker for worker in fake.made("stream_claude")], [True, True])
def test_a_worker_stops_where_it_cannot_remove_the_empty_transcript_of_its_session(self):
assert_not_root(self)
path = self.paths.claude_code_transcripts / "-fake" / f"{self.session.id}.jsonl"
path.parent.mkdir(parents=True)
path.touch()
path.parent.chmod(0o500)
self.addCleanup(path.parent.chmod, 0o700)
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)])
with contextlib.redirect_stderr(io.StringIO()):
stop = run.run_cycle(self.cycle(fake))
self.assertEqual(stop, Stop(1, f"the worker failed three times running: could not remove the empty transcript {path}: Permission denied; claude refuses to start the session while that file stands"))
self.assertEqual(fake.made("stream_claude"), [])
def test_a_cycle_stopped_by_a_failure_or_ctrl_c_archives_what_its_session_holds(self):
fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[ran(code=1)] * 3)
self.assertEqual(run.run_cycle(self.cycle(fake)).code, 1)
+15 -2
View File
@@ -12,7 +12,7 @@ NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
@dataclasses.dataclass(frozen=True)
class Session:
"""Exists while its transcript does."""
"""Open while its transcript holds something: claude cannot resume an empty one."""
id: uuid.UUID
claude_code_transcripts: pathlib.Path
name: str
@@ -29,7 +29,20 @@ def transcript_file(session: Session) -> str | None:
def session_open(session: Session) -> bool:
return transcript_file(session) is not None
path = transcript_file(session)
return path is not None and os.path.getsize(path) > 0
def discard_empty_transcript(session: Session) -> Failure | None:
"""Removes the 0-byte transcript a failed run can leave: claude refuses `--session-id` for an id whose file stands, and cannot `--resume` it."""
path = transcript_file(session)
if path is None or os.path.getsize(path) > 0:
return None
try:
os.remove(path)
except OSError as failure:
return Failure(f"could not remove the empty transcript {path}: {failure.strerror or failure}; claude refuses to start the session while that file stands")
return None
# <session id>.jsonl, <session id>/subagents/, and from older Claude Code versions <session id>.superseded-*/ and <session id>.orphaned-*.jsonl
+36 -1
View File
@@ -2,11 +2,13 @@ import json
import pathlib
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from claude_code import entries, reply, transcript
from test_env import assert_not_root
from claude_code import entries, reply, session, transcript
from failure import Failure
@@ -224,5 +226,38 @@ class ClaudeReply(unittest.TestCase):
self.assertEqual(reply.claude_reply(self.claude(FileNotFoundError(2, "No such file or directory")), [], "/r", "p", "claude"), Failure("could not start claude: No such file or directory"))
class EmptyTranscript(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.session = session.session_named("A proj a-driver", pathlib.Path(temp.name))
self.path = pathlib.Path(temp.name) / "-home-me-code-log" / f"{self.session.id}.jsonl"
self.path.parent.mkdir()
def test_a_session_is_open_only_where_its_transcript_holds_something(self):
self.assertFalse(session.session_open(self.session))
self.path.touch()
self.assertFalse(session.session_open(self.session))
self.path.write_text("{")
self.assertTrue(session.session_open(self.session))
def test_discarding_removes_an_empty_transcript_and_keeps_one_that_holds_something(self):
self.path.touch()
self.assertIsNone(session.discard_empty_transcript(self.session))
self.assertFalse(self.path.exists())
self.path.write_text("{")
self.assertIsNone(session.discard_empty_transcript(self.session))
self.assertEqual(self.path.read_text(), "{")
def test_discarding_fails_naming_the_file_it_could_not_remove(self):
assert_not_root(self)
self.path.touch()
self.path.parent.chmod(0o500)
self.addCleanup(self.path.parent.chmod, 0o700)
failure = session.discard_empty_transcript(self.session)
self.assertIsInstance(failure, Failure)
self.assertEqual(failure.reason, f"could not remove the empty transcript {self.path}: Permission denied; claude refuses to start the session while that file stands")
if __name__ == "__main__":
unittest.main()
+2 -2
View File
@@ -46,7 +46,7 @@ class RouteSources(unittest.TestCase):
opened = session.session_named("A proj loop-claude", pathlib.Path(root, "projects"))
transcript = pathlib.Path(root, "projects", "-fake", f"{opened.id}.jsonl")
transcript.parent.mkdir(parents=True)
transcript.write_text("")
transcript.write_text("{}\n")
unread = Failure("no git")
fake = FakeEffects(git=[*ON_MAIN, unread, unread, unread, unread])
with contextlib.redirect_stderr(io.StringIO()) as stderr:
@@ -176,7 +176,7 @@ class ChunkFacts(unittest.TestCase):
self.assertEqual((fresh.made("python"), "- A fact." in facts), ([(["/b/chunk-preflight", "--worktree", "--item", "7"], "/r")], True))
transcript = pathlib.Path(home, "projects", "-fake", f"{opened.id}.jsonl")
transcript.parent.mkdir(parents=True)
transcript.write_text("")
transcript.write_text("{}\n")
resumed = FakeEffects()
facts, _ = chunk_start.chunk_start(a_loop(resumed.effects(), session=opened, launcher=self.LAUNCHER))
self.assertEqual((resumed.made("python"), facts), ([], ""))
-11
View File
@@ -12,7 +12,6 @@
| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 539 | 0.16.0 | | defect | **Start a chunk's worker on a fresh session where its session's transcript is empty, in place of resuming a session claude cannot find.** | 1 | 2 | 3 | 4 | 2 | 1.5 by 3 | 1.1 |
| 524 | 0.16.0 | | owner | **Write fejkdata's first six bench features, each a spec, acceptance tests in Go and a reference patch that builds it, in the `bench-tasks` folder its config names.** | 3 | 2 | 1 | 4 | 6 | 1.1 by 3 | 0.5 |
| 532 | 0.16.0 | | | **Leave out of a `task-bench` fix a change Haiku makes to a Rust test that stands in the file of the code it tests.** | 2 | 2 | 1 | 3 | 3 | 1.1 by 1 | 0.4 |
| 527 | 0.16.0 | | owner | **Plant bugs for `task-bench` in TypeScript, from the mutants Stryker kills.** | 3 | 2 | 1 | 4 | 4 | 1.1 by 1 | 0.3 |
@@ -113,16 +112,6 @@
## Details
### 539. Start a chunk's worker on a fresh session where its session's transcript is empty, in place of resuming a session claude cannot find.
Today `session_open` in `bin/claude_code/session.py` counts a session as open wherever its `<session id>.jsonl` exists. On 2026-11-24 a run left item-532's transcript as a 0-byte file, with an archive folder whose transcript and outcomes were empty too. On 2026-11-25 every rerun of item-532 called `claude --resume <id>`. Each got "No conversation found with session ID …" and exited 1. The window stopped after three failures and waited for the human, who had to move the file away by hand.
Count an empty transcript as no session. Before the worker starts that session with `--session-id`, remove the empty file, so claude doesn't refuse the id. Leave a non-empty transcript to resume as today. The worker's start, in `bin/agent_cycle/run.py` → `worker_start`, and a chunk's start, in `bin/loop_claude/loop/chunk_start.py`, both read `session_open`, so the fix holds for both.
Touches:
- `bin/claude_code/session.py`: `session_open` (small)
- `bin/claude_code/test_claude_code.py`
### 524. Write fejkdata's first six bench features, each a spec, acceptance tests in Go and a reference patch that builds it, in the `bench-tasks` folder its config names.
The second part of item 504, which says what a bench feature is and when a run counts. A stronger model than the project's writes the six, once. Each breaks one of fejkdata's goals while it would suit another audience: