CodeCompanion session_id #24
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@hermes-bot On line 88 of users/modules/neovim/default.nix, we define a session id to persist my chat between hermes and codecompanion. On the line before, this is defined using the current working directory in some way.
Instead, I would like it to be defined from the name of the git repository I'm working in, and the branch I'm on. It's fine if this only done at startup, but if there's a way to make it aware of the current repository changing, please let me know.
Before you implement, let me know how you're going to do it in a plan posted as a response here
Plan (no code pushed yet, per your request):
How it works today
Line 87-88 computes
session_idonce fromfnamemodify(getcwd(), ":t")inside the adapter factory function. That function runs when the adapter is first resolved, so the id is effectively fixed at startup and only reflects the cwd name.The mechanism I'll use
I read the current CodeCompanion source (
olimorris/codecompanion.nvim). Two facts matter:adapter_utils.set_env_vars(adapter, adapter.headers)(http.lua->write_headers_file).set_env_varsevaluates function values in the headers table on each call:replaced[k] = replace_var(adapter, v(adapter))(adapters/utils/init.lua).So if I make
X-Hermes-Session-Ida function instead of a static string, it is re-evaluated on every message you send - which answers your second question: it automatically tracks repo and branch changes, no autocmd needed (the id switches the next time you send a message after agit switchor:cdinto another repo).The change (users/modules/neovim/default.nix, adapter
__rawblock)Details:
vim.system(Neovim 0.10+, no shell quoting issues), ~1-5 ms per message send - negligible next to an LLM request.HEADoutput) still yields a valid, stable-per-checkout id; non-repo directories keep today's behavior.nvim-nixos-main,nvim-SeesDeployment-RP_S1-5940_van-deployment, etc. Hermes persists a separate conversation per repo+branch, which is exactly the persistence boundary you described.Semantics worth knowing
ChatCreated) - say the word.session_idin the request body; the Hermes gateway keeps using ourX-Hermes-Session-Idheader, so nothing else changes.getcwd()/git commands resolve from the editor's cwd. If you use tab-local working dirs and want the id per-tab, I can resolve git fromvim.fn.getcwd(-1, <tab>)instead - tell me if you use that.If this looks right, I'll open a PR changing only that
__rawblock.Please implement this and push a PR. I'll give it a test when you're done.
While you're in there, could you also refactor codecompanion into a separate plugin like the other neovim plugins are defined? Just to make it cleaner.
Implemented in PR #26 — #26 (mergeable).
Both parts are in there:
X-Hermes-Session-Idis now the function from the plan above:nvim-<repo>-<branch>, recomputed on every message (CodeCompanion evaluates function header values per request), with the cwd-name fallback outside git repos,HEADfor detached checkouts, and header-safe sanitisation of branch names (feature/foo bar→feature-foo-bar).users/modules/neovim/plugins/codecompanion.nix, imported fromplugins/default.nixlike telescope/lsp/cmp. Its three keymaps (<leader>a,<leader>ac,<leader>ae) moved with it; nothing else changed.To test: rebuild as usual, open nvim in a git repo, and check the id in the gateway request logs (or evaluate the header function directly in
:lua). It should readnvim-<repo>-<branch>and flip on the next message after agit switch.Verified locally: the embedded Lua parses, and all three changed
.nixfiles passnix-instantiate --parse. Real-world behaviour is yours to confirm per the issue — happy to adjust if you'd rather freeze the id per chat buffer (variant noted in the plan comment).