Session attachment ignores working directory
Location
ai-code-backends-infra.el:503-518
Severity
Medium
Problem
The ai-code-backends-infra--attached-file-session function now validates sessions only by checking if the buffer name parses correctly for the prefix, ignoring the working-dir parameter.
Before:
(valid (and (buffer-live-p attached)
(memq attached
(ai-code-backends-infra--find-session-buffers
prefix
working-dir))))
After:
(valid (and (buffer-live-p attached)
(ai-code-backends-infra--parse-session-buffer-name
(buffer-name attached)
prefix)))
Impact
A session buffer that was started for a different working directory could now be incorrectly considered valid if its name matches the expected prefix pattern. This could lead to attaching to the wrong session when working across multiple projects.
Scenario
- User has project A open with a session
- User switches to project B in the same frame
- The old session from project A could be incorrectly attached because the buffer name parses correctly
Suggested Fix
Re-add working directory validation to the session attachment check.
@davidwuchn Thanks. how did you find this? I thought you are using eca, that shouldn't use this file.
this issue found by https://github.com/davidwuchn/minimal-emacs.d/blob/main/docs/auto-workflow.md , maybe we can add this function step by step to ai-code, now i am just try to test this concept, maybe useful for more people if ai-code can adopt this concept
@davidwuchn Got it. Cool way to identify potential issues. Are you able to reproduce it on your laptop? Would be great to let me know the correct step to reproduce it, and I'll try it and see if it happens on mine.
I fix it in my fork , and keeping forward. may be you should double check by yourself.
I see. It might be an issue but not directly observable from user side. I’ll let AI to check it out
On Fri, Apr 3, 2026 at 11:54 PM David Wu @.***> wrote:
davidwuchn left a comment (tninja/ai-code-interface.el#252) https://github.com/tninja/ai-code-interface.el/issues/252#issuecomment-4186614466
I fix it in my fork , and keeping forward. may be you should double check by yourself.
— Reply to this email directly, view it on GitHub https://github.com/tninja/ai-code-interface.el/issues/252#issuecomment-4186614466, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFOPAME6LLKIBPIDR5YUZD4UCWSXAVCNFSM6AAAAACW2MAP6WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCOBWGYYTINBWGY . You are receiving this because you were assigned.Message ID: @.***>
@davidwuchn this is what codex (gpt-5.4 xhigh) say:
• 结论
#252 我判断是“指出了一个真实的语义变化”,但不是一个已被证实的回归 bug,也不值 得按 issue 里建议那样直接修回去。
更准确地说,davidwuchn 是“部分对”:
- 他对代码变化本身的观察是对的:working-dir 在 ai-code-backends-infra.el:575 里确实被故意忽略了。
- 但他据此推出的“切到另一个项目会误绑旧 session”这个结论,和当前数据流不完全吻 合。文件 attachment 的 key 是 (prefix . absolute-file-path),见 ai-code- backends-infra.el:559;它是按当前文件绝对路径取 attachment,不是按 frame、窗 口、也不是单靠 session buffer 名称取。
为什么我这样判断
- 这不是无意回归,而是 #244 在 2026-03-21 合并时引入的刻意行为变更,目的就是修 #243 里“明明 session 还活着,但 C-c a z 找不到”的问题。来源:
- #252: https://github.com/tninja/ai-code-interface.el/issues/252
- #243: https://github.com/tninja/ai-code-interface.el/issues/243
- #244: https://github.com/tninja/ai-code-interface.el/pull/244
- 当前仓库里还有一条明确支持这个新语义的回归测试:test/test_ai-code-backends- infra.el:915。它的测试名和断言都写得很直接,就是“WORKING-DIR 不匹配时仍应复 用 live attachment”。
- 我还直接跑了两条相关 ERT:
- test-ai-code-backends-infra-send-line-reselects-when-attached-session- missing
- test-ai-code-backends-infra-switch-reuses-live-attached-session-despite- working-dir-mismatch 结果都通过了。
- 反过来,普通 repo/session 选择路径仍然是按目录精确匹配的,没有被废掉,见 ai- code-backends-infra.el:696。也就是说,被放宽的只有“已显式绑定到文件的 attachment”这一路。
- #252 里给的“项目 A 切项目 B”场景,我认为不够成立。因为跨项目误绑想要发生,前 提不是“目录变了”,而是“当前文件已经持有一个错误的 file->session 映射”。仅仅 切 frame 或切项目,不会自动把 A 的 attachment 套到 B 的另一个文件上。仓库里 还有按文件隔离的测试:test/test_ai-code-backends-infra.el:520。
- 另外,#252 评论里,作者自己也没有给出稳定复现步骤,更多像是从概念/代码审阅角 度提出担忧,而不是已经复现的用户侧故障。
是否值得修
不值得按 issue 里的建议,简单把 working-dir 校验加回 ai-code-backends- infra.el:575。那样大概率会重新引入 #243/#244 想解决的问题,而且会直接和现有测 试冲突。
如果要继续改,我建议是“增强 attachment 有效性判断”,而不是“恢复严格目录匹配”。 更合理的落点在这几个位置:
- ai-code-backends-infra.el:575
- ai-code-backends-infra.el:598
- test/test_ai-code-backends-infra.el:915
可行方向是:
- 绑定时把更多元数据一起存下来,比如 session directory / project root / instance name,而不是只存 buffer。
- 校验时优先检查“live buffer + backend prefix + session metadata”,必要时比 对“同 repo”而不是“完全相同 working-dir”。
- 如果担心误绑,补一个真正的复现测试:同文件 attachment 被错误复用到不该去的 live session,而不是只验证目录不一致。
所以最终回答是:
- 这不是一个足够成立、值得立刻修的 bug。
- davidwuchn 对代码变化的描述是对的,但对风险和修法的判断我不认同。
- 当前行为更像是有意的产品语义:显式 file attachment 优先于目录启发式。
Closing the issue given above assessment.