Tell a user entry the human wrote apart with one predicate

This commit was merged in pull request #134.
This commit is contained in:
2026-09-29 11:43:59 +02:00
parent d9ba36732f
commit 969bb26668
2 changed files with 8 additions and 29 deletions
+7 -27
View File
@@ -1,5 +1,4 @@
"""Every Claude Code format loop-claude reads, each under a sample of it, as Claude Code 2.1.284 writes it."""
import enum
import glob
import json
import os
@@ -7,18 +6,6 @@ import os
from loop_claude.outcome import Failure
class UserEntryKind(enum.Enum):
"""Who wrote a "user" entry: the human wrote HUMAN, Claude Code every other kind."""
COMMAND = enum.auto()
CTRL_C = enum.auto()
EMPTY = enum.auto()
HUMAN = enum.auto()
META = enum.auto()
NON_TEXT = enum.auto()
SDK_PROMPT = enum.auto()
def locations(home):
return home / ".claude.json", home / ".claude" / "cache" / "model-catalog", home / ".claude" / "projects"
@@ -57,36 +44,29 @@ def assistant_parts(entry):
return parts(entry) if entry.get("type") == "assistant" else []
def user_entry_kind(entry):
def written_by_human(entry):
"""Whether the human wrote a "user" entry; Claude Code writes the rest."""
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
if entry.get("isMeta"):
return UserEntryKind.META
# {"type": "user", "promptSource": "sdk", "message": {"content": "Continue the task; ..."}}
if entry.get("promptSource") == "sdk":
return UserEntryKind.SDK_PROMPT
if entry.get("isMeta") or entry.get("promptSource") == "sdk":
return False
# {"type": "user", "message": {"content": "<command-name>/model</command-name>\n..."}}
# {"type": "user", "promptSource": "system", "message": {"content": "<task-notification>..."}}
# {"type": "user", "promptSource": "typed", "message": {"content": "merge it"}}
if isinstance(text := content(entry), str):
source = entry.get("promptSource")
return UserEntryKind.COMMAND if source == "system" or (source != "typed" and text.startswith("<")) else UserEntryKind.HUMAN
return source != "system" and (source == "typed" or not text.startswith("<"))
texts = [part_text(part) for part in parts(entry)]
if not texts:
return UserEntryKind.EMPTY
# {"type": "user", "message": {"content": [{"tool_use_id": "toolu_01…", "type": "tool_result", "content": "hi"}]}}
if None in texts:
return UserEntryKind.NON_TEXT
# {"type": "user", "message": {"content": [{"type": "text", "text": "[Request interrupted by user]"}]}}
if any(text.startswith("[Request interrupted") for text in texts):
return UserEntryKind.CTRL_C
return UserEntryKind.HUMAN
return bool(texts) and None not in texts and not any(text.startswith("[Request interrupted") for text in texts)
def human_text(entry):
"""What the human wrote in the entry, or None where Claude Code wrote it."""
if (queued := queued_human_text(entry)) is not None:
return queued
if entry.get("type") != "user" or user_entry_kind(entry) is not UserEntryKind.HUMAN:
if entry.get("type") != "user" or not written_by_human(entry):
return None
text = content(entry)
return text if isinstance(text, str) else "".join(part_text(part) or "" for part in parts(entry))