Skip to content

Feat: Add Emacs runtime AI debugging tool#326

Open
tninja wants to merge 2 commits intomainfrom
kang_feat
Open

Feat: Add Emacs runtime AI debugging tool#326
tninja wants to merge 2 commits intomainfrom
kang_feat

Conversation

@tninja
Copy link
Copy Markdown
Owner

@tninja tninja commented Apr 30, 2026

Summary

Add an Emacs runtime debugging flow that builds a focused prompt for the active AI session, while keeping MCP access tied to the existing global debug flags.

This is meant for cases like a broken key binding, an interactive command that behaves unexpectedly, or package/config runtime issues where the AI should inspect Emacs state with the MCP debug tools.

What changed

  • keep the ai-code-debug-emacs-runtime entry in the Other Tools menu and let it assemble a runtime-debugging prompt around the user's description
  • remove the session-scoped override path and instead require the existing global flags to already be enabled when the user wants debug tools, especially eval_elisp
  • update the tests to cover the global-flag prompt text and the error path when ai-code-mcp-debug-tools-enable-eval-elisp is off

Verification

  • emacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -l ert -l test/test_ai-code.el -f ert-run-tests-batch-and-exit
  • emacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -l ert -l test/test_ai-code-mcp-debug-tools.el -f ert-run-tests-batch-and-exit
  • emacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -f batch-byte-compile ai-code.el ai-code-mcp-debug-tools.el test/test_ai-code.el test/test_ai-code-mcp-debug-tools.el
  • emacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el --eval '(progn (require '\''checkdoc) (checkdoc-file "ai-code.el") (checkdoc-file "ai-code-mcp-debug-tools.el") (checkdoc-file "test/test_ai-code.el") (checkdoc-file "test/test_ai-code-mcp-debug-tools.el"))'

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: 481690a3b6

ℹ️ 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-mcp-debug-tools.el Outdated
Comment on lines +174 to +176
(ai-code-mcp-debug-tools--register-base-tools)
(when enable-eval-elisp
(ai-code-mcp-debug-tools--register-eval-tool))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep session-only debug tools out of global MCP tool list

ai-code-mcp-debug-tools-enable-for-session is documented as session-scoped, but this code path unconditionally registers debug tools into the global ai-code-mcp-server-tools registry. Since ai-code-mcp--tools-list returns that global list for every session, enabling debug tools for one session makes other sessions advertise those tools even when they are disabled there, which leads agents to call tools that will fail at runtime and breaks the session-scoping contract.

Useful? React with 👍 / 👎.

Comment thread ai-code.el Outdated
Comment on lines +536 to +537
(when (and session-id
(fboundp 'ai-code-mcp-debug-tools-enable-for-session))
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 Fail fast when no MCP session is attached for runtime debug

This command still builds and sends a prompt asserting that Emacs MCP tools are available even when no session ID exists (for example, non-MCP backends or no active session), because tool enablement is conditional later and silently skipped. In that state the AI receives inaccurate capabilities and will attempt tool calls that cannot work, so the flow should error early or generate a non-MCP prompt variant.

Useful? React with 👍 / 👎.

@tninja tninja changed the title Feat: Add Emacs runtime debugging flow with session-scoped MCP tools Feat: Add Emacs runtime AI debugging tool Apr 30, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a dedicated “Emacs runtime debugging” flow to the package, allowing users to compose a focused debugging prompt for the active AI session and enable MCP debug tools on a per-session basis (optionally including eval_elisp), with ERT coverage for the new behavior.

Changes:

  • Added ai-code-debug-emacs-runtime and wired it into the “Other Tools” transient menu.
  • Introduced session-scoped enabling for MCP debug tools (and optional eval_elisp) via session overrides.
  • Added/updated ERT tests covering the new menu entry, prompt flow, and session override behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
ai-code.el Adds the runtime debug command, active MCP session-id lookup, and a new transient menu entry.
ai-code-mcp-debug-tools.el Adds session override state + enable-for-session entrypoint; gates debug tool functions on per-session enablement.
test/test_ai-code.el Adds tests for the new runtime debug prompt flow and transient menu entry.
test/test_ai-code-mcp-debug-tools.el Adds test ensuring session overrides can enable tools even when globally disabled.

Comment thread ai-code.el Outdated
Comment on lines +150 to +151
(defvar ai-code-mcp-agent--session-id nil
"Buffer-local MCP session id attached by `ai-code-mcp-agent`.")
Comment thread ai-code.el
Comment on lines +519 to +541
(defun ai-code-debug-emacs-runtime ()
"Assemble and send an Emacs runtime debugging prompt for the current AI session."
(interactive)
(let* ((description
(ai-code-read-string
"Describe the Emacs runtime issue (eg: key binding / interactive function): "))
(enable-eval-elisp
(y-or-n-p
"Allow AI to eval Emacs Lisp while debugging this Emacs runtime issue? "))
(session-id (ai-code--active-mcp-session-id)))
(when description
(when-let* ((prompt
(ai-code-read-string
"Confirm and edit Emacs runtime debug prompt: "
(ai-code--emacs-runtime-debug-prompt
description
enable-eval-elisp))))
(when (and session-id
(fboundp 'ai-code-mcp-debug-tools-enable-for-session))
(ai-code-mcp-debug-tools-enable-for-session
session-id
enable-eval-elisp))
(ai-code--insert-prompt prompt)))))
Comment thread ai-code.el
("p" "Open prompt history file" ai-code-open-prompt-file)
("m" "Debug python MCP server" ai-code-debug-mcp)
("N" "Toggle notifications" ai-code-notifications-toggle)
;; DONE: add a menu item: Debug your emacs runtime. It will temporarily enable ai-code-mcp-debug-tools-enabled, and ask user if they want to enable ai-code-mcp-debug-tools-enable-eval-elisp (eval elisp with AI?) to further help debugging. User can describe what happens (We prompt them that it can debug an interactive function or a key-binding). The final prompt will assemble with user description and then tell AI to user emacs mcp tools to debug. After user confirm the prompt, send to AI.
Comment thread ai-code-mcp-debug-tools.el Outdated
Comment on lines +36 to +37
(defvar ai-code-mcp--current-session-id nil
"Dynamically bound MCP session id for the current tool invocation.")
Comment thread ai-code-mcp-debug-tools.el Outdated
Comment on lines +169 to +176
(puthash session-id
`((enabled . t)
(enable_eval_elisp . ,(and enable-eval-elisp t)))
ai-code-mcp-debug-tools--session-overrides)
(ai-code-mcp--ensure-error-capture)
(ai-code-mcp-debug-tools--register-base-tools)
(when enable-eval-elisp
(ai-code-mcp-debug-tools--register-eval-tool))
Comment thread ai-code-mcp-debug-tools.el Outdated
(when enable-eval-elisp
(ai-code-mcp-debug-tools--register-eval-tool))
session-id)

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.

2 participants