The parser cuts a trailing ```notify JSON block out of a main-thread assistant message (closing_notify.split) and keeps it on the thread item (notify) and the summary (closingNotify); the indexer hands it to notifications/closing.maybe_push, which pushes it once — id from the block's time and words, skipped when already logged or older than 15 min — with final: true and the CLI's cost line rebuilt from the summary. The push now leaves once the message it announces is in the transcript, instead of ~11 s (p90 24 s) before it, and the closing tool call (one full-context request) is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
"""The closing push a run writes at the end of its final message.
|
|
|
|
Since 2026-11-23 a run no longer closes on a ``notify-done`` Bash call (which
|
|
reached the phone ~11 s before the message it announced, and drew its card
|
|
above that message). It ends its final message with a fenced block instead:
|
|
|
|
```notify
|
|
{"title": "Deployed", "message": "…", "type": "deploy", "url": "https://…"}
|
|
```
|
|
|
|
The parser cuts the block out of the message — the thread, search and every
|
|
"final answer" read the text without it — and keeps it on the thread item
|
|
(the viewer draws the push card after the message) and on the summary, from
|
|
which the indexer hands it to ``notifications/closing.py`` to push it once.
|
|
|
|
Only a block that **ends** a text block counts: one quoted mid-message (docs,
|
|
an example) stays text, and so does a block whose JSON doesn't parse — the
|
|
reader then sees it raw, which is the hint that it went nowhere.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
|
|
# The opening fence of a block: its own line, info string ``notify``.
|
|
_OPEN_RE = re.compile(r"(?:^|\n)[ \t]*```notify[ \t]*\n")
|
|
# What must follow the last opening fence: the JSON, the closing fence, the end.
|
|
_REST_RE = re.compile(r"(?P<body>.*?)\n?[ \t]*```[ \t]*\s*\Z", re.S)
|
|
|
|
# What a block may set, and how long each may be (a push is a few lines).
|
|
FIELDS = {"title": 200, "message": 2000, "type": 24, "url": 2000,
|
|
"image": 2000, "spoken": 500}
|
|
|
|
|
|
def split(text: str) -> tuple[str, dict | None]:
|
|
"""``(text without the block, the block's fields)`` — or ``(text, None)``
|
|
when the text doesn't end on a valid ``notify`` block."""
|
|
if not text or "```notify" not in text:
|
|
return text, None
|
|
opens = list(_OPEN_RE.finditer(text))
|
|
if not opens:
|
|
return text, None
|
|
start = opens[-1]
|
|
m = _REST_RE.match(text, start.end())
|
|
if not m or "```" in m.group("body"):
|
|
return text, None
|
|
try:
|
|
obj = json.loads(m.group("body"))
|
|
except ValueError:
|
|
return text, None
|
|
if not isinstance(obj, dict):
|
|
return text, None
|
|
if "message" not in obj and isinstance(obj.get("body"), str):
|
|
obj["message"] = obj["body"] # NotifyArgs' own spelling
|
|
out: dict = {}
|
|
for k, cap in FIELDS.items():
|
|
v = obj.get(k)
|
|
if isinstance(v, str) and v.strip():
|
|
out[k] = v.strip()[:cap]
|
|
if not (out.get("title") or out.get("message")):
|
|
return text, None
|
|
return text[:start.start()].rstrip(), out
|