Repository navigation
feat(pipeline): wire hook events to has_paste detection (#1313) - #1331
Conversation
Closes #1313 Problem: The `has_paste` precomputed column on the `messages` table existed and was populated by `detect_paste(text)` over message body content, but the Claude Code / Codex hook event payloads (PreToolUse, PostToolUse, UserPromptSubmit) were not consulted as a paste signal source. Hook events carry ground-truth paste markers like `[Pasted text #N]` that the agent runtime emits before clipboard expansion — the most reliable paste signal available — but `detect_paste` did not recognize them. Solution: Extend `detect_paste` to recognize two ground-truth paste signals: 1. Explicit `[Pasted text|content|image #N]` markers emitted by the agent runtime before expansion. 2. Contiguous base64-shaped blobs of >=512 chars (with structural marker check so uniform fills like `"x" * 4000` do not trigger). Add `has_paste_indicator(hook_payload)` helper that walks a hook event payload (raw dict, wrapped envelope, list) and runs `detect_paste` over its text-shaped fields (`prompt`, `text`, `content`, `message`, `tool_output`, `tool_input`). The daemon ingest path can call this to flag messages with `has_paste=1` even when the assembled message text would otherwise look like ordinary prose. Verification: - pytest tests/unit/core/test_paste_detection.py tests/unit/sources/test_hook_events.py 78 passed in 9.06s (was 23 before; +55 paste/hook tests) - devtools verify --quick: exit_code 0 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bfc264a1c8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for field in _HOOK_PASTE_TEXT_FIELDS: | ||
| if field in value: | ||
| out.extend(_flatten_payload_text(value[field])) |
There was a problem hiding this comment.
Handle wrapped hook records inside payload lists
has_paste_indicator says it accepts “a list of records”, but _flatten_payload_text only recurses into _HOOK_PASTE_TEXT_FIELDS keys. For list inputs, each item is typically a wrapped hook record (event_type, session_id, payload), so traversal never enters payload and paste markers in prompt/tool_input are silently missed. This causes false negatives for HOOK_EVENT JSONL streams even when they contain [Pasted text #N] indicators.
Useful? React with 👍 / 👎.
Summary
Extend the materialization-time
has_pastedetection so it recognizesthe ground-truth paste signals that Claude Code / Codex hook events
already carry, and expose
has_paste_indicator(hook_payload)for thedaemon ingest path. Closes #1313.
Problem
The
has_pasteprecomputed column on themessagestable exists andwas populated by
detect_paste(text)over message body content, but:detect_pastedid not recognize the explicit[Pasted text #N]markers that the Claude Code agent runtime emits in
UserPromptSubmitbefore clipboard expansion. The hook event taxonomy
(
tests/unit/sources/test_hook_events.py) already preserves thesemarkers, but nothing on the materialization side consumed them.
signal source at all — they are classified as
HOOK_EVENTandotherwise ignored.
triggered detection via the broad length heuristic, which misses
short tool calls that carry one inline blob.
Closeout history: #802 deferred this to #944; #944 then closed claiming
"no unique implementation scope remains." The #1310 audit caught the
fall-through and filed #1313.
Solution
polylogue/archive/message/paste_detection.py:detect_pasteto flag two new ground-truth signals:[Pasted text|content|image #N]marker pattern (case-insensitive).structural marker (mixed case, digit, or one of
+ / =). Thestructural check prevents uniform fills like
"x" * 4000fromfalsely tripping the new heuristic — those are still caught by
the length rule.
has_paste_indicator(hook_payload)helper that walks a hookevent payload (raw dict, wrapped envelope, or list) and runs
detect_pasteover the text-shaped fields agents actually put pastecontent in:
prompt,text,content,message,tool_output,tool_input. Accepts both the inner payload dict and the wrapped{event_type, session_id, timestamp, provider, payload}envelopefrom
polylogue-hook.tests/unit/core/test_paste_detection.py:Codex hook event payloads (
UserPromptSubmit,PreToolUse,PostToolUse) throughhas_paste_indicator, covering bothpaste-present and no-paste cases per the issue's acceptance criteria.
Alternatives rejected:
has_pastematerialization stage for hook eventsbefore message materialization: hook events do not currently produce
message rows (
HOOK_EVENTartifacts are classified but not yetrouted through the ingest pipeline). Extending the existing text
detector and exposing a payload-shaped helper lets the daemon flag
messages once hook-event routing lands without another schema or
pipeline-stage change.
markers) — produced a fragile multi-branch pattern; a two-step
"long run → structural check" reads more clearly and tests cleanly.
Verification
Acceptance criteria from #1313:
has_paste_indicatorreturns True(
test_has_paste_indicator_user_prompt_submit_with_marker,test_has_paste_indicator_pre_tool_use_with_pasted_input,test_has_paste_indicator_post_tool_use_long_output,test_has_paste_indicator_wrapped_hook_record,test_has_paste_indicator_codex_user_prompt_submit,test_has_paste_indicator_base64_blob_in_tool_output).has_paste_indicatorreturns False(
test_has_paste_indicator_user_prompt_submit_no_paste,test_has_paste_indicator_post_tool_use_no_paste,test_has_paste_indicator_wrapped_hook_record_no_paste,test_has_paste_indicator_none,test_has_paste_indicator_empty_payload).consumer reads the same
messages.has_pastecolumn; this PR widensthe producer signal without changing the column or the surface.
Closes #1313
Ref #802, #944, #1201, #1310