Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat(config)!: cap `sanitize_db` output at 48 chars (#2467)

Worktrunk Bot5dae8d8e92a5
diff --git a/docs/content/hook.md b/docs/content/hook.md
index df55f6051..d21178341 100644
--- a/docs/content/hook.md
+++ b/docs/content/hook.md
@@ -182,7 +182,7 @@ Templates support Jinja2 filters for transforming values:
| Filter | Example | Description |
|--------|---------|-------------|
| `sanitize` | `{{ branch \| sanitize }}` | Replace `/` and `\` with `-` |
-| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 63 chars) |
+| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 48 chars) |
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
diff --git a/skills/worktrunk/reference/hook.md b/skills/worktrunk/reference/hook.md
index 812a3c541..1aaccb3c2 100644
--- a/skills/worktrunk/reference/hook.md
+++ b/skills/worktrunk/reference/hook.md
@@ -173,7 +173,7 @@ Templates support Jinja2 filters for transforming values:
| Filter | Example | Description |
|--------|---------|-------------|
| `sanitize` | `{{ branch \| sanitize }}` | Replace `/` and `\` with `-` |
-| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 63 chars) |
+| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 48 chars) |
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 9f519e094..b2cfffd02 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -1359,7 +1359,7 @@ Templates support Jinja2 filters for transforming values:
| Filter | Example | Description |
|--------|---------|-------------|
| `sanitize` | `{{ branch \| sanitize }}` | Replace `/` and `\` with `-` |
-| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 63 chars) |
+| `sanitize_db` | `{{ branch \| sanitize_db }}` | Database-safe identifier with hash suffix (`[a-z0-9_]`, max 48 chars) |
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
diff --git a/src/config/expansion.rs b/src/config/expansion.rs
index 996a702d4..05f5e7aa0 100644
--- a/src/config/expansion.rs
+++ b/src/config/expansion.rs
@@ -344,7 +344,8 @@ pub fn sanitize_branch_name(branch: &str) -> String {
/// 3. Collapse consecutive underscores into single underscore
/// 4. Add `_` prefix if identifier starts with a digit (SQL prohibits leading digits)
/// 5. Append 3-character hash suffix for uniqueness (avoids reserved words and collisions)
-/// 6. Truncate to 63 characters (PostgreSQL limit; MySQL=64, SQL Server=128)
+/// 6. Truncate to 48 characters total (well within PostgreSQL's 63-char identifier
+/// limit, leaving room for prefixes/suffixes when composing paths or identifiers)
///
/// The hash suffix ensures that:
/// - SQL reserved words are avoided (e.g., `user` → `user_abc`, not a reserved word)
@@ -388,10 +389,11 @@ pub fn sanitize_db(s: &str) -> String {
result.insert(0, '_');
}
- // Truncate base to leave room for hash suffix (4 chars: _ + 3 hash chars)
- // PostgreSQL limit is 63, so max base is 59
- if result.len() > 59 {
- result.truncate(59);
+ // Truncate base to leave room for hash suffix (4 chars: _ + 3 hash chars).
+ // Total cap is 48 chars (well within PostgreSQL's 63-char identifier limit),
+ // so max base is 44.
+ if result.len() > 44 {
+ result.truncate(44);
}
// Append 3-character hash suffix for collision avoidance and reserved word safety
@@ -704,7 +706,7 @@ pub fn validate_template(
///
/// # Filters
/// - `sanitize` — Replace `/` and `\` with `-` for filesystem-safe paths
-/// - `sanitize_db` — Transform to database-safe identifier (`[a-z0-9_]`, max 63 chars)
+/// - `sanitize_db` — Transform to database-safe identifier (`[a-z0-9_]`, max 48 chars)
/// - `sanitize_hash` — Filesystem-safe name with hash suffix so distinct inputs never collide
/// - `hash` — 3-character base36 hash digest of the input
/// - `hash_port` — Hash to deterministic port number (10000-19999)
@@ -964,14 +966,14 @@ mod tests {
#[test]
fn test_sanitize_db_truncation() {
- // Total output is always max 63 characters
- // Base is truncated to 59 chars, then _xxx suffix (4 chars) is added
+ // Total output is always max 48 characters
+ // Base is truncated to 44 chars, then _xxx suffix (4 chars) is added
- // Very long input: base truncated to 59, + 4 = 63
+ // Very long input: base truncated to 44, + 4 = 48
let long_input = "a".repeat(100);
let result = sanitize_db(&long_input);
- assert_eq!(result.len(), 63, "result: {result}");
- assert!(result.starts_with(&"a".repeat(58)), "result: {result}");
+ assert_eq!(result.len(), 48, "result: {result}");
+ assert!(result.starts_with(&"a".repeat(43)), "result: {result}");
assert!(!result.ends_with('_'), "should end with hash chars");
// Short input: base + _ + hash
@@ -983,7 +985,7 @@ mod tests {
// Truncation happens after prefix is added for digit-starting inputs
let digit_start = format!("1{}", "x".repeat(100));
let result = sanitize_db(&digit_start);
- assert_eq!(result.len(), 63, "result: {result}");
+ assert_eq!(result.len(), 48, "result: {result}");
assert!(result.starts_with("_1"), "result: {result}");
}
diff --git a/tests/integration_tests/doc_templates.rs b/tests/integration_tests/doc_templates.rs
index 85e5bdb52..af747365c 100644
--- a/tests/integration_tests/doc_templates.rs
+++ b/tests/integration_tests/doc_templates.rs
@@ -92,7 +92,7 @@ fn test_doc_sanitize_filter(repo: TestRepo) {
// =============================================================================
// Sanitize DB Filter (docs/content/hook.md: Filters table)
-// "Transform to database-safe identifier ([a-z0-9_], max 63 chars)"
+// "Transform to database-safe identifier ([a-z0-9_], max 48 chars)"
// =============================================================================
#[rstest]
@@ -191,7 +191,7 @@ fn test_doc_sanitize_db_truncation(repo: TestRepo) {
let repository = Repository::at(repo.root_path()).unwrap();
let mut vars = HashMap::new();
- // Truncates to 63 characters (PostgreSQL limit)
+ // Truncates to 48 characters (well within PostgreSQL's 63-char identifier limit)
let long_branch = "a".repeat(100);
vars.insert("branch", long_branch.as_str());
let result = expand_template(
@@ -204,8 +204,8 @@ fn test_doc_sanitize_db_truncation(repo: TestRepo) {
.unwrap();
assert_eq!(
result.len(),
- 63,
- "sanitize_db should truncate to 63 characters"
+ 48,
+ "sanitize_db should truncate to 48 characters"
);
}