Module: PostHog::MCP::Ids Private

Defined in:
lib/posthog/mcp/ids.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Id generation: evt_<uuidv7> / ses_<uuidv7> and the deterministic FNV-1a derivation used for session ids that must agree across servers and restarts.

Class Method Summary collapse

Class Method Details

.deterministic_prefixed_id(prefix, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deterministic id derived from an arbitrary string. FNV-1a 64-bit mixed twice to fill 32 hex chars. Not cryptographic; only stable and low-collision.

Iterates code points; session/conversation ids are ASCII in practice.



41
42
43
# File 'lib/posthog/mcp/ids.rb', line 41

def deterministic_prefixed_id(prefix, value)
  "#{prefix}_#{fnv1a_hex(value)}#{fnv1a_hex("#{value}::salt")}"
end

.fnv1a_hex(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
48
49
50
51
52
53
# File 'lib/posthog/mcp/ids.rb', line 45

def fnv1a_hex(value)
  h1 = 0x84222325
  h2 = 0xcbf29ce4
  value.to_s.each_codepoint do |c|
    h1 = ((h1 ^ c) * 0x000001b3) & 0xffffffff
    h2 = ((h2 ^ c) * 0x00000193) & 0xffffffff
  end
  format('%<h1>08x%<h2>08x', h1: h1, h2: h2)
end

.new_prefixed_id(prefix) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • prefix (String) —

    'evt' or 'ses'



33
34
35
# File 'lib/posthog/mcp/ids.rb', line 33

def new_prefixed_id(prefix)
  "#{prefix}_#{uuid_v7}"
end

.uuid_v7 ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

RFC 9562 UUIDv7 (time-ordered), implemented inline so Ruby 3.0/3.1 work too.

Returns:

  • (String) —

    lowercase, hyphenated uuid



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/posthog/mcp/ids.rb', line 17

def uuid_v7
  unix_ts_ms = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) & ((1 << 48) - 1)
  rand_a = SecureRandom.random_number(1 << 12)
  rand_b = SecureRandom.random_number(1 << 62)

  value = unix_ts_ms << 80
  value |= 0x7 << 76
  value |= rand_a << 64
  value |= 0b10 << 62
  value |= rand_b

  hex = format('%032x', value)
  "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
end