Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/llm-coding-tools-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ blocking = ["maybe-async/is_sync", "dep:reqwest", "reqwest?/blocking", "process-
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

# High-performance hashing for permission keys
ahash = "0.8"

# Inline string storage for patterns
tinyvec_string = { version = "0.3", features = ["alloc"] }

# ToolError type uses thiserror for ergonomic error definitions
thiserror = "2.0"

Expand Down
38 changes: 38 additions & 0 deletions src/llm-coding-tools-core/src/internal/hash63.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! 63-bit hash type.
//!
//! A 63-bit hash value representing the upper 63 bits of an original hash.
//! More specifically, a 64-bit hash value which has been `>> 1`.
//! i.e. 64th bit is always 0.

/// A 63-bit hash value representing the upper 63 bits of an original hash.
/// Result of right shifting a hash `>> 1`.
///
/// # Bit Layout
/// - Bits 0-62: Original hash bits 1-63 (upper 63 bits)
/// - Bit 63: Always 0
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct Hash63(u64);

impl Hash63 {
/// Creates a new Hash63 from a raw u64 value.
///
/// The caller is responsible for ensuring bit 63 is 0.
#[inline]
#[allow(dead_code)] // internal public API
pub(crate) const fn from_u64(value: u64) -> Self {
Self(value)
}

/// Returns the underlying u64 value.
#[inline]
#[allow(dead_code)] // internal public API
pub(crate) const fn as_u64(&self) -> u64 {
self.0
}

/// Creates a Hash63 from a Hash64 by extracting upper 63 bits.
#[inline]
pub(crate) fn from_hash64(hash: crate::internal::hash64::Hash64) -> Self {
Self(hash.as_u64() >> 1)
}
}
61 changes: 61 additions & 0 deletions src/llm-coding-tools-core/src/internal/hash64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Wrapper for an internal 64-bit hash.
//!
//! Currently uses ahash64 under the hood, based on performance requirements
//! (handling short strings, while also scaling well); but given this is an
//! internal type, that's an implementation detail.

/// A 64-bit hash value using the ahash algorithm.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct Hash64(u64);

impl Hash64 {
/// Creates a new Hash64 from a raw u64 value.
#[inline]
#[allow(dead_code)] // internal public API
pub(crate) fn from_u64(value: u64) -> Self {
Self(value)
}

/// Returns the underlying u64 value.
#[inline]
#[allow(dead_code)] // internal public API
pub(crate) fn as_u64(&self) -> u64 {
self.0
}
}

/// Hashes a string to Hash64 using ahash64.
#[inline(always)]
#[allow(dead_code)] // internal public API
pub(crate) fn hash_u64(s: &str) -> Hash64 {
hash_u64_bytes(s.as_bytes())
}

/// Hashes raw bytes to Hash64 using ahash64.
#[inline(always)]
#[allow(dead_code)] // internal public API
pub(crate) fn hash_u64_bytes(bytes: &[u8]) -> Hash64 {
Hash64(ahash::RandomState::with_seed(0xDEAD_CAFE).hash_one(bytes))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hash_is_deterministic() {
let hash1 = hash_u64("bash");
let hash2 = hash_u64("bash");
assert_eq!(hash1, hash2);
}

#[test]
fn different_inputs_produce_different_hashes() {
let h1 = hash_u64("bash");
let h2 = hash_u64("read");
let h3 = hash_u64("write");
assert_ne!(h1, h2);
assert_ne!(h1, h3);
assert_ne!(h2, h3);
}
}
8 changes: 8 additions & 0 deletions src/llm-coding-tools-core/src/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Internal implementation details.
//!
//! This module contains private implementation details that are not part of
//! the public API. Items here may change without notice.

pub mod hash63;
pub mod hash64;
pub mod packed_permission;
76 changes: 76 additions & 0 deletions src/llm-coding-tools-core/src/internal/packed_permission.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Bit-packed permission storage.
//!
//! Packs permission hash and action into a single u64.
//! - Lower 63 bits: hash63 (upper 63 bits of original hash)
//! - Upper 1 bit (bit 63): PermissionAction

use crate::internal::hash63::Hash63;
use crate::internal::hash64::Hash64;
use crate::permissions::PermissionAction;

/// Action bit mask - highest bit (bit 63).
const ACTION_MASK: u64 = 1u64 << 63;

/// Hash mask - lower 63 bits.
const HASH_MASK: u64 = !ACTION_MASK;

// Compile-time assertion: PermissionAction must be 1 byte for bit-packing
const _: () = assert!(
std::mem::size_of::<PermissionAction>() == 1,
"PermissionAction must be 1 byte for bit-packing"
);

/// A u64 containing both permission hash and action.
///
/// Layout:
/// - Bits 0-62: hash63 (upper 63 bits of original hash)
/// - Bit 63: PermissionAction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct PackedPermission(u64);

impl PackedPermission {
/// Creates a packed permission from hash and action.
#[inline]
pub(crate) fn new(hash: Hash64, action: PermissionAction) -> Self {
let hash63 = Hash63::from_hash64(hash);
let action_bit = (action as u64) << 63;
Self(hash63.as_u64() | action_bit)
}

/// Returns the hash portion (lower 63 bits) as a [`Hash63`].
/// Use `Hash63::from_hash64()` to compare with an original Hash64.
#[inline]
pub(crate) fn hash(&self) -> Hash63 {
Hash63::from_u64(self.0 & HASH_MASK)
}

/// Returns the PermissionAction stored in bit 63.
#[inline]
pub(crate) fn action(&self) -> PermissionAction {
unsafe { std::mem::transmute(((self.0 >> 63) & 1) as u8) }
}
Comment thread
Sewer56 marked this conversation as resolved.
}

#[cfg(test)]
mod tests {
use super::*;
use crate::internal::hash63::Hash63;
use crate::internal::hash64::Hash64;

#[test]
fn pack_unpack_roundtrip() {
// Use distinctive pattern: 0x1122334455667788 (easily detect if bits are lost)
let hash = Hash64::from_u64(0x1122334455667788u64);
let hash_shifted = Hash63::from_hash64(hash);

// Test roundtrip for Allow
let packed_allow = PackedPermission::new(hash, PermissionAction::Allow);
assert_eq!(packed_allow.hash(), hash_shifted);
assert_eq!(packed_allow.action(), PermissionAction::Allow);

// Test roundtrip for Deny
let packed_deny = PackedPermission::new(hash, PermissionAction::Deny);
assert_eq!(packed_deny.hash(), hash_shifted);
assert_eq!(packed_deny.action(), PermissionAction::Deny);
}
}
2 changes: 2 additions & 0 deletions src/llm-coding-tools-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub mod tool_names;
pub mod tools;
pub mod util;

mod internal;

pub use context::ToolContext;
pub use error::{ToolError, ToolResult};
pub use output::ToolOutput;
Expand Down
Loading
Loading