Skip to content

feat(insights): accurate paste detection via Claude Code history.jsonl + hook ground truth #801

Closed

Description

@Sinity

Problem

Polylogue's detect_paste() uses pure heuristics: text >4000 chars, chatlog forwarding patterns, >70% code fence ratio. Two bugs:

  1. Runs on all messages, not just user messages (materialization_runtime.py:258). Assistant code blocks incorrectly flagged as paste. Explains inflated 92% Codex paste rate.
  2. No provider awareness: Codex has no paste tracking at all. The code-fence heuristic is especially wrong for Codex.

The heuristics themselves need calibration: the 4000-char threshold, the code-fence ratio, and the forwarding patterns were chosen without ground truth validation. Claude Code's history.jsonl provides explicit [Pasted text #N +M lines] markers with pastedContents — this is ground truth data that can be used to measure false positive/negative rates and tune thresholds per provider.

Ground truth available

Claude Code history.jsonl: LogEntry records with [Pasted text #N +M lines] markers and pastedContents map (content hashes, types, filenames). 4.6% of records contain pastes. 79% session coverage overall, ~92% for interactive sessions.

Hook integration (UserPromptSubmit): fires on every prompt before paste expansion, 100% session coverage vs 79% from history.jsonl.

Fix

  1. One-line fix: has_paste = detect_paste(msg.text) if msg.role == "user" else 0
  2. Use history.jsonl ground truth to measure current heuristic accuracy (false positive/negative rates)
  3. Tune thresholds (length, code-fence ratio) per provider based on ground truth
  4. Ingest history.jsonl as new acquisition source
  5. Replace heuristic with explicit markers from matched records; fall back to tuned heuristics when no history
  6. Add message_pastes table for content hash dedup across sessions
  7. Phase 2: hook integration for 100% coverage

Activity

  1. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Current heuristic analysis

    archive/message/paste_detection.py uses three heuristics, any one sets has_paste=1:

    1. Text length > 4000 characters
    2. Matches chatlog-forwarding regex ("previous chatlog attached below", etc.)
    3. 70% of characters inside fenced code blocks

    Bug: detect_paste(msg.text) at materialization_runtime.py:258 runs on ALL messages regardless of role. Assistant messages with code blocks are incorrectly flagged. Production DB shows Codex 92% paste rate — almost entirely assistant code blocks misclassified. One-line fix: has_paste = detect_paste(msg.text) if msg.role == "user" else 0.

    Codex note: Codex has no paste tracking at all — no [Pasted text #N] markers, no pastedContents map in session files. The upstream source only has "paste burst" detection at the TUI level for coalescing rapid keystrokes during paste operations into fewer redraws. Not stored anywhere.

    Implementation sketch (~330 LOC)

    Phase 1 — history.jsonl ingestion:

    1. New acquisition source reading ~/.claude/history.jsonl
    2. Classify as ArtifactKind.PROMPT_HISTORY (new variant in artifact taxonomy)
    3. Match history records to sessions by sessionId + project fields (both present in history.jsonl and session JSONL)
    4. Parse display field for [Pasted text #N +M lines] markers
    5. Extract pastedContents map: content hashes (SHA-256), types (text/image), filenames
    6. Store in new message_pastes table: conversation_id, message_id, content_hash, paste_type, filename, start_offset, end_offset
    7. Replace heuristic detect_paste() call with explicit marker lookup
    8. Fall back to heuristics when no history record exists (70% of sessions)

    Phase 2 — hook integration:

    • UserPromptSubmit hook captures prompt BEFORE paste expansion for 100% of sessions
    • Removes dependency on history.jsonl entirely

    Cross-session dedup query:

    SELECT content_hash, COUNT(DISTINCT conversation_id) 
    FROM message_pastes 
    GROUP BY content_hash 
    HAVING COUNT(DISTINCT conversation_id) > 1
  2. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Correction to the 29.9% figure in the issue body: the actual coverage is 79% (5,723 of 7,233 sessions with sessionId), not 29.9%. The earlier analysis contained a bug — comparing history.jsonl sessionIds against JSONL filenames (slugs) instead of the sessionId field inside the files.

    The 20% gap (1,510 sessions) breaks down as:

    • 1,023 (68%): queue-operation automated batch sessions — no user prompts to record
    • 284 (19%): pre-history era, versions 1.0.x from May-Aug 2025, before history.jsonl existed
    • 199 (13%): user/assistant sessions from the history era — tiny sessions (example: 2 lines), likely interrupted or abandoned

    For real interactive sessions, effective coverage is ~92%. This makes history.jsonl MORE viable as a paste detection ground truth source, not less.

  3. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor
  4. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Acceptance Criteria

    1. detect_paste(msg.text) guarded by msg.role == "user" — Codex paste rate drops from 92% to actual
    2. Current heuristic accuracy measured against history.jsonl ground truth (false positive/negative rates)
    3. Thresholds (4000 chars, 0.7 code-fence ratio) tuned per provider based on ground truth data
    4. history.jsonl ingested as acquisition source with ArtifactKind.PROMPT_HISTORY
    5. message_pastes table populated with content hashes, types, filenames from matched history records
    6. Cross-session dedup query functional

    When closing: comment on this issue or the PR explicitly stating how each criterion was satisfied, deferred, or found misframed.

  5. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Correction: current Codex paste rate is ~23% all messages, ~10% user messages (production DB). The 92% figure cited was inflated by the no-role-guard bug — assistant code blocks were being counted. The one-line fix (msg.role == 'user' guard) corrects this.

  6. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Related: #827 (session intelligence umbrella), #804 (temporal analysis), #803 (cost tracking), #801 (paste detection). These four insight features share infrastructure (session_profiles, action_events, day_session_summaries) and should be sequenced together.

  7. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Fixed in 4d60bde. paste_detection.py now only runs on user messages (role guard added at materialization_runtime.py:258).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:parserarea:storagetype:feat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions