# MATRIX 5.1.56 FINAL

Base: `matrix-5.1.55-mentions-last-date.pl`
Final file: `matrix-5.1.56-final.pl`

## Confirmed fixes

1. **Event-format snapshot priority**
   - Runtime theme/`/FORMAT` state is kept when available.
   - Persisted config is now only a fallback for missing format keys.
   - Completeness is calculated from the actual saved-key set.

2. **PM nick-change state migration**
   - `matrix_pm_recent` and `matrix_pm_last_incoming` no longer blindly overwrite a newer destination entry.
   - The newer recency/timestamp wins.
   - Moving recency/timestamp state now also triggers the required redraw.
   - Existing unread-count merge remains intact.

3. **PRV-GUARD trust after private `/ME` / `/ACTION`**
   - Added `message irc own_action` to the existing own-private trust handler.
   - Matching unload cleanup added.
   - Channel actions are still excluded by the handler's channel-target check.

4. **Mouse/sidebar stale-state protection after reconnect**
   - CHANNEL and QUERY hitboxes now carry the live connection identity.
   - Item resolution rejects hitboxes from an older connection instance even when server tag/name are unchanged.
   - Press/release validation, right-click confirmation, drag/drop and undo now retain this identity.
   - Channel drag remains limited to the same live server connection.

5. **Disconnect cleanup for channel activity**
   - Disconnect now clears `message_activity`, `nick_attention` and `channel_unread_count` for the disconnected network.
   - Persistent mute settings are intentionally not cleared.

6. **MAP request serialization and timeout desynchronization handling**
   - A manual `/MAP` is queued instead of overlapping an already-running background or manual MAP owned by MATRIX.
   - A queued manual MAP starts only after a clean normal MAP end.
   - MAP timeout now enters a fail-closed `map_desynced` state.
   - While desynchronized, MATRIX does not start background MAP and does not claim ownership of manual `/MAP`; native Irssi handles it.
   - The first later end-of-MAP (`017`) is used only as a synchronization boundary, after which MATRIX tracking can resume.
   - Pending manual MAP requests are cancelled after a timeout instead of being started against an ambiguous late numeric stream.

7. **Nicklist prefix sanitization**
   - `nicklist_screen_prefix` is UTF-8-normalized and accepts only printable text plus SGR colour sequences.
   - Non-SGR ESC/control sequences and line-breaking/control characters are stripped.
   - Mode-prefix sanitization was aligned with its existing safety comment so TAB/LF cannot enter terminal output.

8. **GNU Screen nicklist failure handling**
   - `nicklist_screen_size()` now returns success/failure.
   - SCREEN mode is not enabled when initial `screen -X fit/width` configuration fails.
   - A later resize failure drops cleanly to OFF instead of leaving stale SCREEN-mode state.
   - Resize timers are cancelled consistently when SCREEN mode stops.
   - `/nicklist screensize` uses the same guarded resize path.

## Static regression audit

Checked after all changes:

- Perl parse/syntax with a minimal Irssi parse stub: **OK**.
- No new command/signal registrations were left without the corresponding existing cleanup path.
- New MAP scheduling reuses the existing per-server pending-manual timer and its disconnect/unload cleanup.
- New SCREEN resize path reuses and explicitly clears the existing resize timers.
- No unresolved merge/conflict markers.
- No trailing-whitespace changes.
- Cumulative diff against 5.1.55 was reviewed for scope.

## Runtime-only checks still required in real Irssi 1.4.5 + GNU Screen

These cannot be proven by static parsing alone:

1. Receive a PM from a nick with no existing QUERY, wait more than 5 seconds, and verify the QUERY/unread entry survives as expected.
2. Run `/NAMES` on a busy multi-column channel and confirm double-click opens the correct PM in every column; also verify ordinary non-NAMES lines mentioning two channel nicks are not falsely clickable.
3. With nicklist SCREEN enabled, resize the terminal and detach/reattach GNU Screen; verify the nicklist resizes cleanly and falls back to OFF if the Screen command genuinely fails.
4. Optional MAP check: run `/MAP` twice quickly and verify the second request waits rather than overlapping; if a MAP is forced to timeout, confirm native `/MAP` remains usable while MATRIX waits for a clean `017` boundary/reconnect.

## Final file integrity

SHA-256 of `matrix-5.1.56-final.pl`:

`e838e33ee257b6a5416365f033894b997ebcc1f3e53a3d2c1a86be4f8ace0307`
