fix(agent): paste title hint reaches the instant title and the prompt.submit contract

Build on #114129 (@KoNit-K), which carries a Desktop-generated large-paste
preview from the composer through `prompt.submit` -> `display_metadata` ->
turn context -> the shared title input. Two gaps closed:

- `apply_instant_title` never received the preview, so the instant title of a
  paste-only opener was the generated `@file:` path — and stayed that way,
  because the upgrade thread's `derive_title` fallback writes `derived`
  provenance, which never replaces the `derived` title already stored.
  Thread the hint into the instant stage too.
- `build_title_input` let the `@file:` ref lead when the opener was nothing
  but the generated attachment ref; the preview now leads for a ref-only
  opener (an instruction still leads when the user typed one).
- `prompt.submit` gains `title_preview` in the contract (regenerated shared
  TS/OpenRPC); documented as title-only input in the configuration guide.
- Tests trimmed to two invariants (shared input reaches both stages; budget +
  manual attachments stay unread).
This commit is contained in:
teknium1 authored and Teknium committed 2026-09-18 10:56:00 -07:00
1 parent 8d25e69b6e
commit 34ba61bf67
6 files changed
+47 -17

No files matched your search

+14 -5
View File
@@ -47,6 +47,7 @@ RuntimeValidator = Callable[[], bool]
# Text budget handed to the model (Claude Code / OpenClaw converged on 1000).
MAX_TITLE_INPUT_CHARS = 1000
_PASTE_PREVIEW_LABEL = "\n\nPasted content:\n"
_ATTACHMENT_REF_RE = re.compile(r"@(?:file|folder):\S+")
# Cap on the instant derived title; a raw fragment reads worse the longer it runs.
MAX_DERIVED_TITLE_CHARS = 48
# Answer-shaped guard: a tiny model sometimes answers instead of titling; longer is rejected, not truncated.
@@ -223,7 +224,9 @@ def build_title_input(user_message: str, title_preview: str | None = None) -> st
preview = title_preview.strip() if isinstance(title_preview, str) else ""
if not preview:
return message[:MAX_TITLE_INPUT_CHARS]
if not message:
# A paste-only opener is just the generated `@file:` ref: the preview IS the topic, so it
# leads (derive_title takes the first line, and a file path is not a title).
if not _ATTACHMENT_REF_RE.sub("", message).strip():
return preview[:MAX_TITLE_INPUT_CHARS]
message_budget = min(len(message), MAX_TITLE_INPUT_CHARS // 2)
preview_budget = MAX_TITLE_INPUT_CHARS - message_budget - len(_PASTE_PREVIEW_LABEL)
@@ -495,12 +498,18 @@ def _persist_session_title(session_db, session_id, title, *, source, dedupe=True
return _set(deduped)
def apply_instant_title(session_db, session_id: str, user_message: str, title_callback: Optional[TitleCallback] = None) -> Optional[str]:
"""Write the derived title inline. Returns it, or None (no usable text, or a ``derived``+ title exists). Never raises."""
def apply_instant_title(
session_db, session_id: str, user_message: str, title_callback: Optional[TitleCallback] = None,
title_preview: str | None = None,
) -> Optional[str]:
"""Write the derived title inline. Returns it, or None (no usable text, or a ``derived``+ title exists). Never raises.
``title_preview`` must reach this stage too: the model upgrade's own ``derive_title`` fallback writes
``derived`` provenance, which never replaces the ``derived`` title written here."""
if not session_db or not session_id:
return None
try:
title = derive_title(user_message) if is_titleable_user_message(user_message) else None
title = derive_title(user_message, title_preview) if is_titleable_user_message(user_message) else None
persisted = _persist_session_title(session_db, session_id, title, source="derived", dedupe=False) if title else None
if persisted:
_notify_title(title_callback, persisted, "derived", "Instant-title")
@@ -644,7 +653,7 @@ def maybe_auto_title(
if not _auto_title_enabled(): # config read after the cheap guards so the file isn't touched every turn
logger.debug("Auto-title skipped: auxiliary.title_generation.enabled=false")
return
apply_instant_title(session_db, session_id, user_message, title_callback)
apply_instant_title(session_db, session_id, user_message, title_callback, title_preview=title_preview)
if not _model_title_upgrade_enabled():
logger.debug("Instant title persisted; model upgrade disabled by auxiliary.title_generation.model_upgrade_enabled=false")
return
@@ -2134,6 +2134,7 @@ export interface PromptSubmitParams {
queued?: boolean | null
surface?: string | null
voice_context?: string | null
title_preview?: string | null
truncate_before_user_ordinal?: number | null
truncate_before_row_id?: number | null
truncate_before_message_id?: string | null
@@ -21891,6 +21891,18 @@
"default": null,
"title": "Voice Context"
},
"title_preview": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Title Preview"
},
"truncate_before_user_ordinal": {
"anyOf": [
{
+12 -12
View File
@@ -7,6 +7,7 @@ from unittest.mock import MagicMock, patch
from agent.title_generator import (
MAX_TITLE_INPUT_CHARS,
build_title_input,
derive_title,
generate_title,
auto_title_session,
maybe_auto_title,
@@ -22,32 +23,31 @@ class TestGenerateTitle:
@pytest.mark.parametrize(
("instruction", "paste_preview", "expected_parts"),
[
("", "Quarterly incident analysis for the database migration", ["Quarterly incident analysis"]),
("@file:/tmp/composer-pastes/pasted_content_1.txt", "Quarterly incident analysis for the database migration",
["Quarterly incident analysis"]),
("Analyze this", "Quarterly incident analysis for the database migration", ["Analyze this", "Quarterly incident analysis"]),
("Prepare the deployment follow-up", "Quarterly incident analysis", ["Prepare the deployment follow-up", "Quarterly incident analysis"]),
],
)
def test_title_input_includes_only_generated_paste_preview(
self, instruction, paste_preview, expected_parts
):
"""Regression for #114124: a generated paste remains an attachment for the turn but informs its title."""
def test_generated_paste_preview_reaches_the_shared_title_input(self, instruction, paste_preview, expected_parts):
"""A Desktop large paste stays an @file attachment for the turn, but its preview informs BOTH title
paths (derive_title instant + generate_title model input) through the one shared input."""
title_input = build_title_input(instruction, paste_preview)
assert all(part in title_input for part in expected_parts)
assert "@file:" not in title_input
# Paste-only opener (just the generated ref): the instant title is the paste's topic, not the path.
lead = paste_preview if instruction.startswith("@file:") else instruction
assert derive_title(instruction, paste_preview).startswith(lead[:12])
def test_title_input_keeps_manual_attachment_and_preview_out_of_title_context(self):
title_input = build_title_input("Summarize @file:notes.txt", None)
assert title_input == "Summarize @file:notes.txt"
assert "Pasted content:" not in title_input
def test_title_input_budgets_instruction_and_generated_paste_preview(self):
def test_title_input_budget_and_manual_attachments_stay_unread(self):
title_input = build_title_input("Describe the release plan", "p" * MAX_TITLE_INPUT_CHARS)
assert len(title_input) == MAX_TITLE_INPUT_CHARS
assert title_input.startswith("Describe the release plan")
assert title_input.endswith("p" * 20)
# No preview => an ordinary manual attachment ref is never read for titling.
assert build_title_input("Summarize @file:notes.txt", None) == "Summarize @file:notes.txt"
+2
View File
@@ -35,6 +35,8 @@ class PromptSubmitParams(SessionParams):
queued: bool | None = None # client queue drain — the busy path must hold it, never redirect/steer
surface: str | None = None # a ClientSurface value; unknown values clear the surface
voice_context: str | None = None # recent spoken transcript, model input only (voice-live)
# Desktop-generated large-paste preview (first ~1000 chars); TITLE input only, never the model turn.
title_preview: str | None = None
truncate_before_user_ordinal: int | None = None
truncate_before_row_id: int | None = None
truncate_before_message_id: str | None = None
+6
View File
@@ -1371,6 +1371,12 @@ No background `auto-title` thread starts and no automatic title-model request is
explicit repair command `hermes sessions retitle-skills` still calls the model. `enabled: false`
still disables both stages.
In Hermes Desktop, a plain-text paste over 3,000 characters becomes a generated `.txt`
attachment. The first ~1,000 characters of that paste are handed to the title stages as a
title-only hint (the agent turn still sees only the attachment reference), so a "summarize
this" plus a large paste is named after the pasted topic. Files you attach yourself are never
read for titling.
### Stream-only endpoints
Some OpenAI-compatible endpoints reject non-streaming chat requests outright (e.g. Tencent Copilot returns HTTP 400 `"Non-stream chat request is currently not supported"`). Interactive chat already streams, but auxiliary tasks (title generation, compression, vision) use non-streaming calls and would fail on every attempt. Hermes always treats `copilot.tencent.com` as stream-only; for any other such endpoint, list a URL substring under `auxiliary.stream_only_base_urls`: