Skip to content

Preserve AI session reuse for attached and newly opened files when lookup context changes#244

Merged
tninja merged 5 commits intomainfrom
copilot/fix-c-c-a-z-session-buffer-issue
Mar 21, 2026
Merged

Preserve AI session reuse for attached and newly opened files when lookup context changes#244
tninja merged 5 commits intomainfrom
copilot/fix-c-c-a-z-session-buffer-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 19, 2026

Single-file session attachment improved multi-session UX, but in a rare case C-c a z could no longer find the associated AI session. The failure mode was a live attached session being treated as missing when the current working directory no longer matched the session buffer’s directory lookup. Follow-up feedback also exposed a related same-repo case: when a repo already had an active AI session, opening a different file could still force a fresh selection instead of reusing the current session.

  • Root cause

    • File-to-session attachment was validated through the current working-dir session scan.
    • If project/root resolution changed between session creation and later lookup, the attached buffer could be dropped even though the session was still alive.
    • For newly opened files without an existing file attachment, the resolver also forced an initial file-session selection even when a remembered repo session already existed.
  • Behavior change

    • ai-code-backends-infra--attached-file-session now treats an attached session as valid when:
      • the remembered buffer is still live, and
      • the buffer still parses as a session buffer for the same backend prefix.
    • For newly opened files in the same repo, session resolution now reuses the current remembered repo session by default instead of forcing a new file-session selection.
    • Missing attached sessions still trigger re-selection, and explicit force-prompt rebinding still prompts as before.
    • When multiple active sessions exist for the repo and a newly opened file has no attachment yet, the normal session selector is still used rather than auto-picking arbitrarily.
  • Regression coverage

    • Added a focused infra test for the mismatch case: a file-attached session remains reusable even when the current working-dir differs from the session’s original directory.
    • Updated the infra tests to cover the follow-up scenario where a newly opened file in the same repo should reuse the remembered session, while preserving the expected behavior for missing attachments and explicit rebinding.
    • Added focused coverage confirming that a newly opened file still prompts for selection when multiple active sessions exist in the same repo.
  • Effect on users

    • C-c a z now continues to reopen the remembered session for a file in cases where the attachment is still correct but directory matching is no longer exact.
    • When there is already an active AI session in a repo, opening a different file in that repo now reuses that remembered session by default instead of unnecessarily prompting for a new association.
    • When there are multiple active AI sessions in the same repo, opening a new file still presents the usual session choice instead of silently binding the file to the wrong session.
;; Before:
;; attached session could be discarded if WORKING-DIR no longer matched
;; ai-code-backends-infra--find-session-buffers.
;; Newly opened files also forced an initial file-session selection.

(valid (and (buffer-live-p attached)
            (memq attached
                  (ai-code-backends-infra--find-session-buffers
                   prefix
                   working-dir))))

;; After:
;; explicit file attachment wins as long as the session buffer is still live
;; and still belongs to the same backend prefix.
;; Newly opened files reuse the remembered repo session unless prompting is
;; explicitly required.

(valid (and (buffer-live-p attached)
            (ai-code-backends-infra--parse-session-buffer-name
             (buffer-name attached)
             prefix)))
Original prompt

This section details on the original issue you should resolve

<issue_title>In rare case, C-c a z cannot find the associated ai coding session buffer</issue_title>
<issue_description>Recently we introduced the single file attached session facility, to build the connection between single file and the session it associated. This improve the user experience during using multiple ai coding session in the same repo, so that buffer file has it "remembered" ai coding session.

However, after that change, in rare case I find that sometime C-c a z cannot find the associated ai coding session buffer. C-c a also shows that there is no active ai-coding session.

The issue might be related to multi-session management, and some in memory variable keeping the session information or mapping information.</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI and others added 2 commits March 19, 2026 15:49
Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix C-c a z not finding associated AI coding session buffer Preserve file-attached AI sessions when session directory lookup diverges Mar 19, 2026
Copilot AI requested a review from tninja March 19, 2026 15:51
@tninja
Copy link
Copy Markdown
Owner

tninja commented Mar 20, 2026

@copilot When there is already active ai coding session inside a repo, in some case, I opened a different file in the repo, C-c a z or C-c a cannot find the already opened ai coding session, will this fix resolve the issue?

Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot When there is already active ai coding session inside a repo, in some case, I opened a different file in the repo, C-c a z or C-c a cannot find the already opened ai coding sess...

Addressed in 5075132. Newly opened files in the same repo now reuse the current remembered AI session by default, so C-c a z and send-to-session flows no longer force a fresh selection unless the attached session is missing or you explicitly force a rebind.

Copilot AI changed the title Preserve file-attached AI sessions when session directory lookup diverges Preserve AI session reuse for attached and newly opened files when lookup context changes Mar 20, 2026
@tninja
Copy link
Copy Markdown
Owner

tninja commented Mar 20, 2026

@copilot Will the code be able to handle the case for multiple active ai coding sessions? At that case, opened a new file, C-c a z or C-c a should ask user to choose session it should attach to.

Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 20, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 20, 2026

@copilot Will the code be able to handle the case for multiple active ai coding sessions? At that case, opened a new file, C-c a z or C-c a should ask user to choose session it should attach ...

Yes — that case is covered by af5b77b. For a newly opened file with multiple active sessions in the same repo, C-c a z / C-c a will still ask the user which session to attach to, rather than auto-selecting one. The auto-reuse path only applies when there is a single remembered repo session or an existing file attachment. No UI screenshot here because this follow-up is behavior/test coverage only.

@tninja
Copy link
Copy Markdown
Owner

tninja commented Mar 20, 2026

I'll test this branch for a while before merging it.

@tninja tninja marked this pull request as ready for review March 21, 2026 00:46
Copy link
Copy Markdown
Owner

@tninja tninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LG after one day's use.

@tninja tninja merged commit bec8fb6 into main Mar 21, 2026
1 check passed
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: af5b77b030

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ai-code-backends-infra.el
Comment on lines +535 to +537
(effective-force-prompt
(or force-prompt
attached-missing))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Force selection for first file binding with multiple sessions

Dropping needs-initial-file-selection from effective-force-prompt means a newly opened file with no existing attachment now silently reuses whatever repo session is remembered. In a repo with multiple active sessions, that remembered buffer can belong to an unrelated task, so the first send/switch for the file can bind it to the wrong session and persist that mapping without any chooser prompt. The prior logic explicitly forced a prompt for this first-time file association, so this is a behavior regression introduced here.

Useful? React with 👍 / 👎.

@tninja tninja deleted the copilot/fix-c-c-a-z-session-buffer-issue branch April 9, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In rare case, C-c a z cannot find the associated ai coding session buffer

2 participants