[PR #1787] [MERGED] Discord Rich Presence: the IPC client and its thread #3762

Closed
opened 2026-11-03 04:12:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1787
Author: @ludovic111
Created: 10/25/2026
Status: ✅ Merged
Merged: 10/25/2026
Merged by: @ludovic111

Base: main ← Head: feat/discord-presence-ipc


📝 Commits (3)

  • 604ec7e Discord Rich Presence: the IPC client and its thread (inert until wired)
  • e49de34 Discord Rich Presence: the Horizon application's id
  • 628e230 Merge main (the save_root lint fix, #1789)

📊 Changes

3 files changed (+573 additions, -1 deletions)

View changed files

➕ crates/horizon-client/src/discord_presence.rs (+568 -0)
📝 crates/horizon-client/src/lib.rs (+1 -0)
📝 tools/lab.py (+4 -1)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What

The first half of Discord Rich Presence (Ludovic asked for it on 2026-10-25): crates/horizon-client/src/discord_presence.rs, a client for Discord's local IPC and the thread that runs it. Nothing calls it yet. The follow-up PR wires it into the game and adds the setting.

  • IPC written here, no crate. The transport follows Discord's public RPC docs. Linux and macOS use the discord-ipc-0..9 Unix sockets in XDG_RUNTIME_DIR / TMPDIR / TMP / TEMP / /tmp, plus the Flatpak (app/com.discordapp.Discord) and Snap (snap.discord) subfolders. Windows uses the \\?\pipe\discord-ipc-N named pipes. Each frame is opcode u32 LE + length u32 LE + JSON. The client sends HANDSHAKE {v:1, client_id} and waits for READY, then sends SET_ACTIVITY {pid, activity} and reads the reply that carries its nonce. It answers PING with PONG. A CLOSE or an I/O error drops the connection. The client is about 120 lines on std and the serde_json the crate already uses, so no new dependency.
  • Its own thread, never in a frame or a tick. The game calls set(status), which locks one mutex and wakes the thread only when the status changes. The thread keeps only the latest value (the same park/unpark pattern as heartbeat.rs), so a burst of changes never queues up. When Discord isn't running, it retries every 15 s. It sends at most 5 updates in any 20 s, the most Discord accepts. It resends the activity every 60 s, so a Discord restarted in the meantime gets it back. stop() on quit clears the activity, waiting at most 300 ms. With no Discord the cost is nothing beyond those 15 s retries, which log at debug level only.
  • Activity. One of "In the menus", "Singleplayer", "Multiplayer" or "Hosting a world". In a world the state line is "In the Overworld", "In the Nether" or "In the End". The start timestamp is when the player entered the world: changing dimension or opening to LAN keeps it. Hosting adds a party of N of 8 (lan::MAX_PLAYERS). The large image is horizon. The game-mode small images (survival, creative, adventure, spectator) are wired but stay off (MODE_IMAGES = false) until such assets are uploaded. The activity never contains a world's name or a server's address.
  • When it stays quiet. It is off with HORIZON_NO_DISCORD=1. Automated runs (offscreen, benchmarks, frame-limited) only talk to a fake Discord that HORIZON_DISCORD_IPC_DIR points to. lab.game_env sets HORIZON_NO_DISCORD=1, so harnesses never reach a real Discord.
  • Application id. DISCORD_APP_ID = "1563857609738690560", Discord's "Horizon" application. It is public, and its horizon asset is the logo.

How tested

  • Unit tests: frames encode and decode (including a truncated frame and an oversized one); the status → activity mapping, including that nothing private gets in; the clock running from entering the world; the 5-in-20-s limiter; and a client talking to a fake Discord over a real Unix socket (handshake, a ping in the middle, a set, a clear, distinct nonces).
  • Windows: the module passes cargo clippy --target x86_64-pc-windows-msvc --all-targets -D warnings and its tests in a stub crate (the named-pipe path compiles).
  • End to end with real games: see the follow-up PR.

Self-check

Run once on the stack's head (3063d87, both PRs together).

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 574d54e6e, 13 min 7 s)

Check Result
Scope ⚠️ 10 files, +964 −5; 947 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ❌ 267 run, FAILED (errors=2) (1 min 40 s)
rustfmt ✅ (13 s)
clippy ✅ (1 min 32 s)
tests: horizon-client ✅ 1218 passed (9 min 6 s)
doc tests ✅ 0 passed (4 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 331 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 319 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)
  • Python tests: the 2 errors are test_parity_runner.OracleReuseTests.test_reuses_until_an_input_or_output_changes and test_rust_parity.ParityRunnerTests.test_report_rejects_sources_changed_during_a_run. They are not this branch's: both fail the same way on main on macOS (/var vs /private/var temp paths, and a java shim exiting 127).
  • Parity: left to CI. The Mac mini was running three full parity runs, so the orchestrator stopped the local one.

Needs Ludovic

A look at a real Discord profile. No Discord client is installed on the Mac mini, so the agent could not check against a real Discord (it did not install one or log in to one).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/1787 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/25/2026 **Status:** ✅ Merged **Merged:** 10/25/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/discord-presence-ipc` --- ### 📝 Commits (3) - [`604ec7e`](https://github.com/horizon-rust/horizon/commit/604ec7e2e4fd4de7b5f5e481a7c7a0af881e3419) Discord Rich Presence: the IPC client and its thread (inert until wired) - [`e49de34`](https://github.com/horizon-rust/horizon/commit/e49de342ed93b6a1a79238946164aada5d1974e5) Discord Rich Presence: the Horizon application's id - [`628e230`](https://github.com/horizon-rust/horizon/commit/628e2302179910e9ee1c3d368203869a30f67103) Merge main (the save_root lint fix, #1789) ### 📊 Changes **3 files changed** (+573 additions, -1 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/src/discord_presence.rs` (+568 -0) 📝 `crates/horizon-client/src/lib.rs` (+1 -0) 📝 `tools/lab.py` (+4 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What The first half of Discord Rich Presence (Ludovic asked for it on 2026-10-25): `crates/horizon-client/src/discord_presence.rs`, a client for Discord's local IPC and the thread that runs it. Nothing calls it yet. The follow-up PR wires it into the game and adds the setting. - **IPC written here, no crate.** The transport follows Discord's public RPC docs. Linux and macOS use the `discord-ipc-0..9` Unix sockets in `XDG_RUNTIME_DIR` / `TMPDIR` / `TMP` / `TEMP` / `/tmp`, plus the Flatpak (`app/com.discordapp.Discord`) and Snap (`snap.discord`) subfolders. Windows uses the `\\?\pipe\discord-ipc-N` named pipes. Each frame is opcode u32 LE + length u32 LE + JSON. The client sends `HANDSHAKE {v:1, client_id}` and waits for `READY`, then sends `SET_ACTIVITY {pid, activity}` and reads the reply that carries its nonce. It answers `PING` with `PONG`. A `CLOSE` or an I/O error drops the connection. The client is about 120 lines on std and the `serde_json` the crate already uses, so no new dependency. - **Its own thread, never in a frame or a tick.** The game calls `set(status)`, which locks one mutex and wakes the thread only when the status changes. The thread keeps only the latest value (the same park/unpark pattern as `heartbeat.rs`), so a burst of changes never queues up. When Discord isn't running, it retries every 15 s. It sends at most 5 updates in any 20 s, the most Discord accepts. It resends the activity every 60 s, so a Discord restarted in the meantime gets it back. `stop()` on quit clears the activity, waiting at most 300 ms. With no Discord the cost is nothing beyond those 15 s retries, which log at debug level only. - **Activity.** One of "In the menus", "Singleplayer", "Multiplayer" or "Hosting a world". In a world the state line is "In the Overworld", "In the Nether" or "In the End". The start timestamp is when the player entered the world: changing dimension or opening to LAN keeps it. Hosting adds a party of N of 8 (`lan::MAX_PLAYERS`). The large image is `horizon`. The game-mode small images (`survival`, `creative`, `adventure`, `spectator`) are wired but stay off (`MODE_IMAGES = false`) until such assets are uploaded. The activity never contains a world's name or a server's address. - **When it stays quiet.** It is off with `HORIZON_NO_DISCORD=1`. Automated runs (offscreen, benchmarks, frame-limited) only talk to a fake Discord that `HORIZON_DISCORD_IPC_DIR` points to. `lab.game_env` sets `HORIZON_NO_DISCORD=1`, so harnesses never reach a real Discord. - **Application id.** `DISCORD_APP_ID = "1563857609738690560"`, Discord's "Horizon" application. It is public, and its `horizon` asset is the logo. ## How tested - Unit tests: frames encode and decode (including a truncated frame and an oversized one); the status → activity mapping, including that nothing private gets in; the clock running from entering the world; the 5-in-20-s limiter; and a client talking to a fake Discord over a real Unix socket (handshake, a ping in the middle, a set, a clear, distinct nonces). - Windows: the module passes `cargo clippy --target x86_64-pc-windows-msvc --all-targets -D warnings` and its tests in a stub crate (the named-pipe path compiles). - End to end with real games: see the follow-up PR. ## Self-check Run once on the stack's head (3063d87, both PRs together). **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 574d54e6e, 13 min 7 s) | Check | Result | | --- | --- | | Scope | ⚠️ 10 files, +964 −5; 947 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ❌ 267 run, FAILED (errors=2) (1 min 40 s) | | rustfmt | ✅ (13 s) | | clippy | ✅ (1 min 32 s) | | tests: horizon-client | ✅ 1218 passed (9 min 6 s) | | doc tests | ✅ 0 passed (4 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 331 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 319 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | - **Python tests:** the 2 errors are `test_parity_runner.OracleReuseTests.test_reuses_until_an_input_or_output_changes` and `test_rust_parity.ParityRunnerTests.test_report_rejects_sources_changed_during_a_run`. They are not this branch's: both fail the same way on main on macOS (`/var` vs `/private/var` temp paths, and a `java` shim exiting 127). - **Parity:** left to CI. The Mac mini was running three full parity runs, so the orchestrator stopped the local one. ## Needs Ludovic A look at a real Discord profile. No Discord client is installed on the Mac mini, so the agent could not check against a real Discord (it did not install one or log in to one). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 04:12:08 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#3762
No description provided.