This commit is contained in:
@@ -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 a 0-byte transcript, which a run that died early leaves: claude refuses `--session-id` for an id whose file exists, 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}; remove it, then run it again")
|
||||
return None
|
||||
|
||||
|
||||
# <session id>.jsonl, <session id>/subagents/, and from older Claude Code versions <session id>.superseded-*/ and <session id>.orphaned-*.jsonl
|
||||
|
||||
@@ -2,12 +2,14 @@ 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 claude_code import entries, reply, session, transcript
|
||||
from failure import Failure
|
||||
from test_env import assert_not_root
|
||||
|
||||
|
||||
def assistant(*parts):
|
||||
@@ -226,3 +228,36 @@ class ClaudeReply(unittest.TestCase):
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
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("{}\n")
|
||||
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("{}\n")
|
||||
self.assertIsNone(session.discard_empty_transcript(self.session))
|
||||
self.assertEqual(self.path.read_text(), "{}\n")
|
||||
|
||||
def test_discarding_says_where_it_could_not_remove_the_file(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.assertIn(str(self.path), failure.reason)
|
||||
|
||||
Reference in New Issue
Block a user