89 lines
3.8 KiB
Python
89 lines
3.8 KiB
Python
"""The hub's own prompt envelopes — harness-independent, so every reader
|
|
calls them: the inbox wrapper a follow-up into a live run carries, the Stop
|
|
guard's relay of a message that arrived while a turn was held, the cancel
|
|
note of a queued follow-up, and the author tag of a shared conversation.
|
|
|
|
Each one is the ai-agent's (sidecar ``/message``, `harness-claude's `sidecar/stop_guard.py``,
|
|
``users/runs.py``), never a harness's: a reader turns them into a ``prompt``
|
|
event's ``origin`` / ``author`` or an ``inject`` of kind ``cancel``, and the
|
|
fold never sees the envelope."""
|
|
|
|
import re
|
|
|
|
|
|
# A follow-up the ai-agent delivered into a *live* run through the CLI's
|
|
# cross-session inbox socket (sidecar ``/message``) instead of killing the run.
|
|
# Claude Code records it as a peer message — "Another Claude session sent a
|
|
# message:\n<text>\n\nThis came from another Claude session — not typed by your
|
|
# user …" — and the sidecar prefixes the text with a bracketed note saying it
|
|
# *is* the user's (sidecar.INBOX_PREFIX). Shown as the plain user turn it is.
|
|
# A run that is busy absorbs it mid-turn instead and records only the raw
|
|
# prompt (prefix included, no "Another Claude session" header) on a
|
|
# ``queued_command`` attachment — see ``_absorbed_followup``.
|
|
_INBOX_RE = re.compile(
|
|
r"\A(?:Another Claude session sent a message[^:\n]*:\s*\n)?"
|
|
r"\[(?:Follow-up from your user|Message from the ai-agent hub)[^\]]*\]\s*\n+"
|
|
r"(.*?)"
|
|
r"(?:\n\n+This came from another Claude session\b.*)?\Z", re.S)
|
|
|
|
|
|
def _unwrap_inbox(c: str) -> str:
|
|
m = _INBOX_RE.match(c)
|
|
return m.group(1).strip() if m else c
|
|
|
|
|
|
# A follow-up cancelled while a live run still had it queued (the queued
|
|
# echo's Cancel): the ai-agent sends this note through the same inbox, quoting
|
|
# the message verbatim, so the model reads both together and drops it. Not a
|
|
# turn of its own (it starts with `<`): the message it quotes is marked
|
|
# `cancelled`, and leaves the run's `queued` list with it.
|
|
_CANCEL_RE = re.compile(r"\A<ai-agent-cancel/>.*?\n<<<\n(.*)\n>>>\s*\Z", re.S)
|
|
|
|
|
|
def _cancelled_text(c: str) -> str | None:
|
|
"""The message a cancel note quotes, or None when ``c`` isn't one."""
|
|
m = _CANCEL_RE.match(c)
|
|
return m.group(1).strip() if m else None
|
|
|
|
|
|
# The Stop guard relays a message that reached a held turn inside its
|
|
# feedback (see ``claudetext._HELD_RE`` for the feedback itself).
|
|
_RELAY_RE = re.compile(r"<<<USER MESSAGE>>>\n(.*?)\n<<<END USER MESSAGE>>>", re.S)
|
|
|
|
|
|
def _relayed_message(c: str) -> str | None:
|
|
"""The user's text the Stop guard relayed (it arrived while the turn was
|
|
held), or None for a plain hold feedback."""
|
|
m = _RELAY_RE.search(c)
|
|
return m.group(1).strip() if m else None
|
|
|
|
|
|
# The sidecar's restart note (sidecar/undelivered.py): appended to a resumed
|
|
# prompt when the stopped run had task ends queued it never handed the model.
|
|
# Not the user's words — dropped from the bubble; the tasks it names are read.
|
|
_TASKS_NOTE_RE = re.compile(r"\s*<ai-agent-tasks-done>(.*?)</ai-agent-tasks-done>\s*\Z", re.S)
|
|
_NOTED_RE = re.compile(r"^- task ([\w-]+):", re.M)
|
|
|
|
|
|
def _split_tasks_note(c: str) -> tuple[list[str], str]:
|
|
"""The task ids a trailing restart note reports, and ``c`` without it."""
|
|
m = _TASKS_NOTE_RE.search(c or "")
|
|
if not m:
|
|
return [], c
|
|
return _NOTED_RE.findall(m.group(1)), c[:m.start()]
|
|
|
|
|
|
# Who wrote a turn, in a conversation several people share (a guest's, or one
|
|
# shared with guests): the backend prefixes the prompt with this tag
|
|
# (users/runs.py → author_tag). The thread shows the author and drops the tag.
|
|
_AUTHOR_RE = re.compile(
|
|
r'\A<ai-agent-from user="([^"]*)" name="([^"]*)" role="([^"]*)"\s*/>\s*\n?')
|
|
|
|
|
|
def _split_author(c: str) -> tuple[dict | None, str]:
|
|
m = _AUTHOR_RE.match(c or "")
|
|
if not m:
|
|
return None, c
|
|
return ({"id": m.group(1), "name": m.group(2), "role": m.group(3)},
|
|
c[m.end():])
|