Skip to content

feat(pipeline): wire hook events to has_paste detection (#1313) - #1331

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/hook-has-paste-wiring-1313-v2
May 18, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/hook-has-paste-wiring-1313-v2

Conversation

@Sinity

@Sinity Sinity commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Extend the materialization-time has_paste detection so it recognizes
the ground-truth paste signals that Claude Code / Codex hook events
already carry, and expose has_paste_indicator(hook_payload) for the
daemon ingest path. Closes #1313.

Problem

The has_paste precomputed column on the messages table exists and
was populated by detect_paste(text) over message body content, but:

  • detect_paste did not recognize the explicit [Pasted text #N]
    markers that the Claude Code agent runtime emits in UserPromptSubmit
    before clipboard expansion. The hook event taxonomy
    (tests/unit/sources/test_hook_events.py) already preserves these
    markers, but nothing on the materialization side consumed them.
  • The PreToolUse/PostToolUse hook payloads were not wired as a paste
    signal source at all — they are classified as HOOK_EVENT and
    otherwise ignored.
  • Long base64-shaped blobs (typical of tool output pastes) only
    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:

  1. Extend detect_paste to flag two new ground-truth signals:
    • [Pasted text|content|image #N] marker pattern (case-insensitive).
    • Contiguous base64-alphabet runs of >=512 chars that contain a
      structural marker (mixed case, digit, or one of + / =). The
      structural check prevents uniform fills like "x" * 4000 from
      falsely tripping the new heuristic — those are still caught by
      the length rule.
  2. Add has_paste_indicator(hook_payload) helper that walks a hook
    event payload (raw dict, wrapped envelope, or list) and runs
    detect_paste over the text-shaped fields agents actually put paste
    content 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} envelope
    from polylogue-hook.

tests/unit/core/test_paste_detection.py:

  • +13 unit cases covering the new heuristics in isolation.
  • +13 integration-shaped cases that feed synthetic Claude Code and
    Codex hook event payloads (UserPromptSubmit, PreToolUse,
    PostToolUse) through has_paste_indicator, covering both
    paste-present and no-paste cases per the issue's acceptance criteria.

Alternatives rejected:

  • Adding a separate has_paste materialization stage for hook events
    before message materialization: hook events do not currently produce
    message rows (HOOK_EVENT artifacts are classified but not yet
    routed 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.
  • Tightening the base64 regex itself (alternation over structural
    markers) — produced a fragile multi-branch pattern; a two-step
    "long run → structural check" reads more clearly and tests cleanly.

Verification

$ nix develop -c pytest -q tests/unit/core/test_paste_detection.py \
                        tests/unit/sources/test_hook_events.py
78 passed in 9.06s

$ nix develop -c devtools verify --quick
... "exit_code": 0

Acceptance criteria from #1313:

  • Test: simulated hook event with paste content → has_paste_indicator returns 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).
  • Test: hook event without paste → has_paste_indicator returns 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).
  • Coordinate with feat(reader): paste spans rendering and paste browser MVP #1201 (paste spans rendering): the rendering
    consumer reads the same messages.has_paste column; this PR widens
    the producer signal without changing the column or the surface.

Closes #1313
Ref #802, #944, #1201, #1310

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]>
@Sinity
Sinity merged commit c06d8fa into master May 18, 2026
@Sinity
Sinity deleted the feature/feat/hook-has-paste-wiring-1313-v2 branch May 18, 2026 19:59

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +120 to +122
for field in _HOOK_PASTE_TEXT_FIELDS:
if field in value:
out.extend(_flatten_payload_text(value[field]))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(pipeline): wire Claude Code hook events to has_paste detection (from #802 fall-through)

1 participant