|
| 1 | +;;; ai-code-harness.el --- Harness support for ai-code -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Author: Kang Tu <tninja@gmail.com> |
| 4 | + |
| 5 | +;; SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +;;; Commentary: |
| 8 | +;; Harness generation and prompt suffix helpers for ai-code. |
| 9 | + |
| 10 | +;;; Code: |
| 11 | + |
| 12 | +(require 'subr-x) |
| 13 | + |
| 14 | +(require 'ai-code-agile) |
| 15 | +(require 'ai-code-backends) |
| 16 | + |
| 17 | +(declare-function ai-code--ensure-files-directory "ai-code-prompt-mode" ()) |
| 18 | +(declare-function ai-code--git-root "ai-code-file" (&optional dir)) |
| 19 | + |
| 20 | +(defvar ai-code-mcp-agent-enabled-backends) |
| 21 | +(defvar ai-code-selected-backend) |
| 22 | + |
| 23 | +(defconst ai-code--diagnostics-first-harness-instruction |
| 24 | + "Record a diagnostics baseline with the get_diagnostics MCP tool before editing. After each edit, re-run get_diagnostics for the touched files and do not finish until they have no new diagnostics compared with the baseline." |
| 25 | + "Shared diagnostics-first harness guidance for code-change prompts.") |
| 26 | + |
| 27 | +(defun ai-code--diagnostics-first-harness-instruction-inline () |
| 28 | + "Return diagnostics-first guidance formatted for inline prompt text." |
| 29 | + (concat (downcase (substring ai-code--diagnostics-first-harness-instruction 0 1)) |
| 30 | + (substring ai-code--diagnostics-first-harness-instruction 1))) |
| 31 | + |
| 32 | +;;;###autoload |
| 33 | +(defcustom ai-code-test-after-code-change-suffix |
| 34 | + "If any program code changes, run unit-tests and follow up on the test-result (fix code if there is an error)." |
| 35 | + "User-provided prompt suffix for test-after-code-change." |
| 36 | + :type '(choice (const nil) string) |
| 37 | + :group 'ai-code) |
| 38 | + |
| 39 | +(defconst ai-code--auto-test-harness-file-version "v1" |
| 40 | + "Version tag appended to generated auto-test harness file names.") |
| 41 | + |
| 42 | +;;;###autoload |
| 43 | +(defcustom ai-code-auto-test-harness-cache-directory |
| 44 | + nil |
| 45 | + "Directory used to cache generated auto-test harness files. |
| 46 | +
|
| 47 | +When nil, store harness files under `harness/` inside the directory returned |
| 48 | +by `ai-code--ensure-files-directory`. In a Git repository, that is typically |
| 49 | +`.ai.code.files/harness/` under the current repository so prompts can cite |
| 50 | +them with `@`-prefixed repo-relative paths. Outside a Git repository, this |
| 51 | +falls back to `harness/` under `default-directory`. |
| 52 | +
|
| 53 | +Set this to a directory path to override the default location." |
| 54 | + :type '(choice |
| 55 | + (const :tag "Use default harness directory (.ai.code.files/harness in a repo, or harness under default-directory otherwise)" |
| 56 | + nil) |
| 57 | + directory) |
| 58 | + :group 'ai-code) |
| 59 | + |
| 60 | +(defun ai-code--auto-test-harness-directory () |
| 61 | + "Return the directory used for generated auto-test harness files." |
| 62 | + (let ((cache-directory (and (boundp 'ai-code-auto-test-harness-cache-directory) |
| 63 | + ai-code-auto-test-harness-cache-directory))) |
| 64 | + (if cache-directory |
| 65 | + (expand-file-name cache-directory) |
| 66 | + (expand-file-name "harness/" (ai-code--ensure-files-directory))))) |
| 67 | + |
| 68 | +(defun ai-code--auto-test-harness-prompt-path (file-path) |
| 69 | + "Return FILE-PATH formatted for prompt usage. |
| 70 | +When FILE-PATH is inside the current git repository, return an `@`-prefixed |
| 71 | +repo-relative path. Otherwise return the absolute FILE-PATH." |
| 72 | + (if-let ((git-root (ai-code--git-root))) |
| 73 | + (let ((git-root-truename (file-name-as-directory (file-truename git-root))) |
| 74 | + (file-truename (file-truename file-path))) |
| 75 | + (if (file-in-directory-p file-truename git-root-truename) |
| 76 | + (concat "@" (file-relative-name file-truename git-root-truename)) |
| 77 | + file-path)) |
| 78 | + file-path)) |
| 79 | + |
| 80 | +(defun ai-code--auto-test-backend () |
| 81 | + "Return the backend symbol used for auto-test prompt decisions." |
| 82 | + (if (fboundp 'ai-code--effective-backend) |
| 83 | + (or (ai-code--effective-backend) ai-code-selected-backend) |
| 84 | + ai-code-selected-backend)) |
| 85 | + |
| 86 | +(defun ai-code--diagnostics-harness-enabled-p () |
| 87 | + "Return non-nil when the current backend should get diagnostics guidance." |
| 88 | + (memq (ai-code--auto-test-backend) |
| 89 | + ai-code-mcp-agent-enabled-backends)) |
| 90 | + |
| 91 | +(defun ai-code--maybe-append-diagnostics-harness-instruction (suffix &optional inline) |
| 92 | + "Append diagnostics harness guidance to SUFFIX when the backend supports it. |
| 93 | +When INLINE is non-nil, use the inline-formatted diagnostics instruction." |
| 94 | + (if (and (stringp suffix) |
| 95 | + (> (length suffix) 0) |
| 96 | + (ai-code--diagnostics-harness-enabled-p)) |
| 97 | + (let ((instruction (if inline |
| 98 | + (ai-code--diagnostics-first-harness-instruction-inline) |
| 99 | + ai-code--diagnostics-first-harness-instruction))) |
| 100 | + (concat suffix |
| 101 | + (if inline " " "") |
| 102 | + instruction)) |
| 103 | + suffix)) |
| 104 | + |
| 105 | +(defun ai-code--test-after-code-change--resolve-tdd-suffix () |
| 106 | + "Return the TDD-style suffix for test-after-code-change prompt text." |
| 107 | + (ai-code--maybe-append-diagnostics-harness-instruction |
| 108 | + (concat ai-code--tdd-red-green-base-instruction |
| 109 | + ai-code--tdd-red-green-tail-instruction |
| 110 | + ai-code--tdd-run-test-after-each-stage-instruction |
| 111 | + ai-code--tdd-test-pattern-instruction))) |
| 112 | + |
| 113 | +(defun ai-code--test-after-code-change--resolve-tdd-with-refactoring-suffix () |
| 114 | + "Return the TDD+refactoring suffix for test-after-code-change prompt text." |
| 115 | + (ai-code--maybe-append-diagnostics-harness-instruction |
| 116 | + (concat ai-code--tdd-red-green-base-instruction |
| 117 | + ai-code--tdd-with-refactoring-extension-instruction |
| 118 | + ai-code--tdd-red-green-tail-instruction |
| 119 | + ai-code--tdd-run-test-after-each-stage-instruction |
| 120 | + ai-code--tdd-test-pattern-instruction))) |
| 121 | + |
| 122 | +(defun ai-code--auto-test-inline-suffix-for-type (type) |
| 123 | + "Return the inline prompt suffix for auto test TYPE." |
| 124 | + (pcase type |
| 125 | + ('test-after-change |
| 126 | + (ai-code--maybe-append-diagnostics-harness-instruction |
| 127 | + ai-code-test-after-code-change-suffix t)) |
| 128 | + ('tdd (ai-code--test-after-code-change--resolve-tdd-suffix)) |
| 129 | + ('tdd-with-refactoring (ai-code--test-after-code-change--resolve-tdd-with-refactoring-suffix)) |
| 130 | + ('no-test "Do not write or run any test.") |
| 131 | + (_ nil))) |
| 132 | + |
| 133 | +(defun ai-code--auto-test-harness-file-name (type) |
| 134 | + "Return the stable harness file name for auto test TYPE." |
| 135 | + (let ((base-name (symbol-name type))) |
| 136 | + (format "%s%s.%s.md" |
| 137 | + base-name |
| 138 | + (if (ai-code--diagnostics-harness-enabled-p) |
| 139 | + "-diagnostics" |
| 140 | + "") |
| 141 | + ai-code--auto-test-harness-file-version))) |
| 142 | + |
| 143 | +(defun ai-code--ensure-auto-test-harness-cache-directory () |
| 144 | + "Ensure the auto-test harness cache directory exists and return it." |
| 145 | + (let ((directory (ai-code--auto-test-harness-directory))) |
| 146 | + (unless (file-directory-p directory) |
| 147 | + (make-directory directory t)) |
| 148 | + directory)) |
| 149 | + |
| 150 | +(defun ai-code--auto-test-harness-text-for-type (type) |
| 151 | + "Return the externalized harness text for auto test TYPE." |
| 152 | + (pcase type |
| 153 | + ('no-test nil) |
| 154 | + (_ (ai-code--auto-test-inline-suffix-for-type type)))) |
| 155 | + |
| 156 | +(defun ai-code--ensure-auto-test-harness-file (type) |
| 157 | + "Write and return the cached harness file path for auto test TYPE." |
| 158 | + (when-let ((content (ai-code--auto-test-harness-text-for-type type))) |
| 159 | + (let* ((directory (ai-code--ensure-auto-test-harness-cache-directory)) |
| 160 | + (file-path (expand-file-name |
| 161 | + (ai-code--auto-test-harness-file-name type) |
| 162 | + directory))) |
| 163 | + (with-temp-file file-path |
| 164 | + (insert content) |
| 165 | + (unless (bolp) |
| 166 | + (insert "\n"))) |
| 167 | + file-path))) |
| 168 | + |
| 169 | +(defun ai-code--auto-test-harness-reference-suffix (type) |
| 170 | + "Return a short suffix that references the cached harness file for TYPE. |
| 171 | +
|
| 172 | +If the harness file cannot be prepared, fall back to the inline suffix." |
| 173 | + (condition-case err |
| 174 | + (when-let ((file-path (ai-code--ensure-auto-test-harness-file type))) |
| 175 | + (format |
| 176 | + "Read the local harness file: %s. Use its instructions for this work. Apply it without repeating its full contents." |
| 177 | + (ai-code--auto-test-harness-prompt-path file-path))) |
| 178 | + (file-error |
| 179 | + (message "Failed to prepare auto-test harness file for %s: %s" |
| 180 | + type |
| 181 | + (error-message-string err)) |
| 182 | + (ai-code--auto-test-inline-suffix-for-type type)))) |
| 183 | + |
| 184 | +(defun ai-code--auto-test-suffix-for-type (type) |
| 185 | + "Return prompt suffix for auto test TYPE." |
| 186 | + (pcase type |
| 187 | + ((or 'test-after-change 'tdd 'tdd-with-refactoring) |
| 188 | + (ai-code--auto-test-harness-reference-suffix type)) |
| 189 | + ('no-test "Do not write or run any test.") |
| 190 | + (_ nil))) |
| 191 | + |
| 192 | +(provide 'ai-code-harness) |
| 193 | + |
| 194 | +;;; ai-code-harness.el ends here |
0 commit comments