Skip to content

fix(grok): O(1) session path via encode_cwd_dirname parity - #9267

Closed
bbingz wants to merge 1 commit into
stablyai:mainfrom
bbingz:bbingz/fix-grok-encode-cwd-dirname
Closed

bbingz wants to merge 1 commit into
stablyai:mainfrom
bbingz:bbingz/fix-grok-encode-cwd-dirname

Conversation

@bbingz

@bbingz bbingz commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Grok chat-history lookup can miss the session file entirely for working directories containing !, ', (, ) or *. origin/main builds the sessions group directory name with encodeURIComponent (src/shared/grok-session-paths.ts:63), which leaves those five characters unescaped. Grok Build's Rust urlencoding::encode percent-encodes every byte outside the unreserved set (ALPHA / DIGIT / - . _ ~), so the two disagree on the real on-disk name:

cwd    /Users/dev/repos/app (v2)
main   %2FUsers%2Fdev%2Frepos%2Fapp%20(v2)      <- looked up, never exists
grok   %2FUsers%2Fdev%2Frepos%2Fapp%20%28v2%29  <- the directory Grok wrote

The new encodeGrokUrlComponent closes that gap. The same module also ports Grok's long-path branch, which main declined to encode at all — it returned null once the encoded name exceeded 255 bytes and fell back to a bounded scan of up to GROK_SESSION_GROUP_SCAN_MAX_ENTRIES (2048) group dirs. Both branches now resolve in O(1) when cwd is known.

Aligned with open-source xai-org/grok-build xai_grok_config::paths::encode_cwd_dirname:

CWD size Group dirname
URL-encoded ≤ 255 bytes percent-encoded path (urlencoding unreserved set)
Longer {slug40}-{blake3_hex16} (+ on-disk .cwd written by Grok)
  • New pure module: src/shared/grok-cwd-dirname.ts
  • buildGrokChatHistoryPathCandidates / sync resolve use the full encoder (short and long)
  • Bounded async scan remains the fallback when cwd is unknown

Dependency note

@noble/hashes is pure JavaScript: no native addon, no gypfile, no install scripts. It therefore cannot affect the Ubuntu 20.04 / glibc 2.31 floor described in docs/reference/linux-glibc-compatibility.md, which constrains bundled .node binaries only. It has zero runtime dependencies — pnpm-lock.yaml records the snapshot as '@noble/hashes@1.8.0': {}. The library is independently audited (Cure53, 2022), with the caveat that upstream states plainly that blake3 fell outside that audit's scope. blake3 is used on exactly one branch: the {slug40}-{blake3_hex16} form for cwds whose URL-encoded name exceeds 255 bytes.

Claude Opus review

Verdict: APPROVED (pre-push). The reviewer independently re-verified the golden blake3 vectors against the open-source algorithm.

Addressed before push:

  • removed unused isGrokSlugHashCwdDirName (dead export)
  • documented the deliberate / + \ leaf split for SSH/guest path hosts

Testing

  • vitest grok-cwd-dirname + grok-session-paths — 22 passed (includes open-source golden long-path vectors)
  • full repository Vitest suite

Notes

Contributor PR; enables native-chat, Stop enrichment, and AI Vault path hits for deep worktree paths without scanning.

ELI5

Orca encoded the Grok chat-history directory name slightly differently from Grok itself, so some working directories were never found, and very long ones were not looked up at all. Orca now uses Grok's exact encoding, so the session file is located directly whenever the working directory is known.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds shared Grok CWD directory-name encoding with Rust-aligned URL encoding, slugification, validation, 255-byte handling, and Blake3-based fallback names. Re-exports the helpers through session path utilities and updates history path construction and synchronous resolution to support long CWD slug groups. Adds dependency and golden-vector tests covering encoding, validation, truncation, and path discovery.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the change and testing, but omits the required Screenshots, AI Review Report, and Security Audit sections. Add the required template sections, including visual-change status, cross-platform AI review confirmation, and a security audit summary.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the Grok session-path fix and encoder parity change.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@bbingz
bbingz force-pushed the bbingz/fix-grok-encode-cwd-dirname branch 5 times, most recently from a6dfd99 to 526ad24 Compare July 18, 2026 14:17
@AmethystLiang AmethystLiang self-assigned this Jul 18, 2026
@AmethystLiang
AmethystLiang self-requested a review July 18, 2026 16:47
Port Grok Build's sessions group encoding so Orca can resolve
chat_history.jsonl without scanning the sessions root when cwd is known:

- Short cwds: URL-encode with the urlencoding unreserved set
- Long cwds (>255 encoded bytes): `{slug40}-{blake3_hex16}` matching
  xai-org/grok-build `encode_cwd_dirname`
- Pure-JS blake3 via @noble/hashes; golden vectors from OSS algorithm
- Bounded async scan remains the fallback when cwd is unknown
@bbingz

bbingz commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current origin/main (0326594d52).

Conflict was only pnpm-lock.yaml for @noble/hashes; regenerated with pnpm install --lockfile-only.

Focused tests: grok-cwd-dirname + grok-session-paths 22/22 green. ahead=1 behind=0.

@bbingz
bbingz force-pushed the bbingz/fix-grok-encode-cwd-dirname branch from 526ad24 to ef047c9 Compare July 23, 2026 02:03
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
@Jinwoo-H

Copy link
Copy Markdown
Contributor

Closing this submitted branch and routing the bug to canonical owner issue #17583. We will carry the work ourselves on current main and will not request contributor follow-up. Full contributor credit is preserved for @bbingz. No action is requested from you. Thank you.

@Jinwoo-H Jinwoo-H closed this Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants