Skip to content

feat: use selected Magit hunks for agent explain, change, and test actions - #508

Merged
tninja merged 7 commits into
mainfrom
feat/magit-hunk-context
Sep 6, 2026
Merged

feat: use selected Magit hunks for agent explain, change, and test actions#508
tninja merged 7 commits into
mainfrom
feat/magit-hunk-context

Conversation

@tninja

@tninja tninja commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Magit hunks currently fall through to file-oriented actions or scope menus, so users cannot hand a selected patch directly to their coding agent. This PR makes the existing commands use the displayed hunk as their scope.

Command Behavior in Magit
C-c a x Explain behavioral changes, assumptions, and risks, read-only
C-c a q Ask a question about the selected patch, read-only
C-c a c Apply feedback to current working-tree source
C-c a t Add useful missing regression tests for the existing change
C-c a @ Copy or store the selected patch snapshot

A shared extractor captures the current hunk, sibling hunk selections, or a complete enclosing hunk with a focused excerpt. It preserves paths, diff direction/range, revision identity, HEAD and capture time. Context is captured before minibuffer interaction, and patch text stays literal during prompt filepath preprocessing, including when reused from stored context.

Edit and test requests ask the agent to re-read current files, preserve unrelated changes, and avoid staging or committing. The test action leaves production code alone and reports verification commands/results without claiming a test-first cycle for existing code. Tests run in the agent. Historical diffs allow read-only actions and context capture; write actions reject them. Invalid selections and non-text/combined hunks produce actionable errors. Existing file and Dired behavior is retained.

Validation uses GitHub Actions because this workspace has no Emacs executable:

  • TDD Red: all 16 initial integration tests failed before implementation while the existing suite passed.
  • Subsequent Red/Green cycles cover selection loss in the context menu, revision OIDs, and patch corruption during filepath preprocessing.
  • Final validation: 22 real-Magit integration tests; existing 1492-test suite (1458 passed, 34 existing skips); native Windows platform tests; byte compilation and checkdoc for touched Lisp files. The new module is compiled with warnings treated as errors.
  • Real Magit tests run in a separate Emacs process to avoid replacing the unit suite's Magit stubs.

The repository-wide melpazoid job already fails at the unchanged base commit: baseline run. Its package-lint errors concern any in ai-code-session-link.el and the declared Emacs/compat dependency. This PR does not modify that file.

README documentation includes the key mapping, selection rules, snapshot semantics, and test boundaries.

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

Copy link
Copy Markdown

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: 27b9b5a6f8

ℹ️ 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".

emacs -Q --batch -L . \
--eval "(progn (require 'package) (package-initialize))" \
--eval "(progn (require 'bytecomp) (let ((byte-compile-error-on-warn t)) (unless (byte-compile-file \"ai-code-magit.el\") (kill-emacs 1))))" \
--eval "(dolist (file '(\"ai-code-change.el\" \"ai-code-discussion.el\" \"ai-code-agile.el\" \"ai-code-file.el\" \"ai-code-prompt-mode.el\")) (unless (byte-compile-file file) (kill-emacs 1)))" \

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 Enforce warning failures for every modified Lisp file

When any of these five modified modules emits a byte-compilation warning, byte-compile-file can still return an output filename and this CI command succeeds because byte-compile-error-on-warn is bound only in the preceding --eval for ai-code-magit.el. Bind it while compiling this entire touched-file list so newly introduced warnings cannot pass CI.

AGENTS.md reference: AGENTS.md:L12-L16

Useful? React with 👍 / 👎.

(lambda (&rest _) "0123456789abcdef")))
,@body))))

(ert-deftest ai-code-magit-context-current-hunk ()

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 Rename the Magit tests to the required test pattern

This and the other new ERT tests use names beginning with ai-code-magit-, whereas the repository requires test names in the test-module--function-name-scenario form. Rename the new test symbols consistently so test discovery and filtering follow the established convention.

AGENTS.md reference: AGENTS.md:L172-L175

Useful? React with 👍 / 👎.

@tninja
tninja merged commit 910d8b3 into main Sep 6, 2026
4 of 6 checks passed
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.

1 participant