refactor: extract lib/heuristics.mjs + lib/operations.mjs (Phase 0)#41
Merged
Conversation
Extract tool classification sets (FILE_CREATE_TOOLS, FILE_EDIT_TOOLS, PR_TOOLS, SHELL_TOOLS), extraction helpers (extractFilePath, extractPrInfo, detectShellGitAction), brag keyword detection (isBragRequest), and composite classifier (classifyToolUse) into a reusable, dependency-free lib module. extension.mjs now imports from lib/heuristics.mjs and uses classifyToolUse() in onPostToolUse instead of inline classification. test/heuristics.test.mjs has 33 tests covering all extracted functions. test/extension.test.mjs updated to import from lib/heuristics.mjs instead of re-implementing inline copies. 158 tests passing (was 127). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extract saveBragEntry, reviewBragEntries, and generateWorkLog into
lib/operations.mjs with discriminated { ok: true/false } returns.
Both extension.mjs and mcp-server.mjs now delegate to these shared
operations instead of duplicating validate-create-persist-backup logic.
Move atomicWriteText from inline definitions in both entry points
into lib/storage.mjs where it belongs alongside atomicWriteJSON.
test/operations.test.mjs adds 16 tests covering all three operations
including whitespace-only summary validation edge cases.
test/storage.test.mjs adds 2 tests for atomicWriteText.
176 tests passing (was 158).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add lib/heuristics.mjs and lib/operations.mjs to release.yml tarball validation required[] array. Update AGENTS.md §2 (architecture: new modules in lib table), §3 (test count 107→176), §4 (atomicWriteText unified in storage.mjs), §5 (test table with new test files), and §9 (repo navigation: heuristics.mjs, operations.mjs entries). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…allback
isBragRequest: The BRAG_EXCLUDE_REGEX (/brag(?:ging|gart)/i) caused
false negatives on prompts containing both 'bragging' and standalone
'brag' (e.g. 'bragging rights brag'). The \b word boundary already
prevents matching 'bragging' or 'braggart', making the exclude
redundant. Removed per adversarial council skeptic review.
extension.mjs: Align git config fallback with mcp-server.mjs — add
null-safe ?? { enabled: false, push: false } guard to prevent NPE
when config.git is undefined. Per architect review.
177 tests passing (was 176).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove 5 dead node:fs imports from extension.mjs (openSync, closeSync, writeFileSync, fsyncSync, renameSync) left over after atomicWriteText moved to lib/storage.mjs - Remove 10 dead lib imports from extension.mjs (detectBragSheetPath, isValidCategory, atomicWriteText, createEntryRecord, backupToGit, renderMarkdown, renderReviewSummary, FILE_CREATE_TOOLS, FILE_EDIT_TOOLS, PR_TOOLS, SHELL_TOOLS, extractFilePath, extractPrInfo, detectShellGitAction) - Add CHANGELOG.md [Unreleased] section for Phase 0 changes - Update CONTRIBUTING.md module table with heuristics.mjs, operations.mjs, mcp-server.mjs, bin/mcp-server.mjs - Update docs/testing-strategy.md coverage map with actual per-file counts (177 total) and new module rows - Fix AGENTS.md test counts (176 -> 177, per-file corrections) 177 tests passing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Refactor that extracts shared logic from extension.mjs and mcp-server.mjs into two new dependency-free modules (lib/heuristics.mjs, lib/operations.mjs), and moves atomicWriteText into lib/storage.mjs. Also fixes a isBragRequest false-negative regression and a potential null-deref on config.git.
Changes:
- New
lib/heuristics.mjswith tool classification sets, extraction helpers, andclassifyToolUsecomposite. - New
lib/operations.mjsprovidingsaveBragEntry/reviewBragEntries/generateWorkLogwith{ ok }discriminated returns; both entry points now delegate to it. atomicWriteTextconsolidated intolib/storage.mjs; tests added for new modules; docs updated.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/heuristics.mjs | New module: tool classification + brag detection (drops faulty exclude regex). |
| lib/operations.mjs | New module: shared validate→create→persist→backup orchestration. |
| lib/storage.mjs | Adds atomicWriteText previously duplicated in entry points. |
| extension.mjs | Removes inline classification/operations; delegates to new lib modules; adds null guard on config.git. |
| mcp-server.mjs | Replaces inline handlers with calls into lib/operations.mjs; drops local atomicWriteText. |
| test/heuristics.test.mjs | New 34-test suite for the heuristics module. |
| test/operations.test.mjs | New 16-test suite for operations with real disk I/O. |
| test/storage.test.mjs | Adds two atomicWriteText tests. |
| test/extension.test.mjs | Trims duplicated tests, replaces inline reimplementations with real imports. |
| docs/testing-strategy.md, CONTRIBUTING.md, AGENTS.md, CHANGELOG.md | Documentation updates reflecting the new modules and test counts. |
| .github/workflows/release.yml | Includes new lib files in release artifacts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+80
to
+99
| * Read and render recent brag entries. | ||
| * | ||
| * Returns the raw records plus a pre-rendered markdown summary. Callers | ||
| * handle pagination, truncation, and structured output as needed for | ||
| * their surface. | ||
| * | ||
| * @param {{ weeks?: number }} args | ||
| * @param {{ dataDir: string, config: object }} ctx | ||
| * @returns {{ ok: true, records: object[], markdown: string, weeks: number }} | ||
| */ | ||
| export function reviewBragEntries(args, { dataDir, config }) { | ||
| const weeks = args.weeks ?? 4; | ||
|
|
||
| const records = readRecords(dataDir, { | ||
| since: new Date(Date.now() - weeks * 7 * 86400000).toISOString(), | ||
| }); | ||
|
|
||
| const markdown = renderReviewSummary(records, { weeks, config }); | ||
|
|
||
| return { ok: true, records, markdown: markdown || "", weeks }; |
| */ | ||
| export function saveBragEntry(args, { dataDir, config, gitConfig }) { | ||
| // Validate summary | ||
| if (!args.summary?.trim()) { |
| * Detect whether the user is asking to save work to their brag sheet. | ||
| * | ||
| * The `\b` word boundary already prevents matching "bragging" or | ||
| * "braggart" — no exclusion regex needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 0: Extract shared lib modules
Problem
extension.mjsandmcp-server.mjsduplicate tool classification logic, brag-entry orchestration, andatomicWriteText. Adding Agency hooks (Phase 1) would triplicate this.Solution
Extract shared logic into two new dependency-free
lib/modules:lib/heuristics.mjs- Tool classification sets, extraction helpers,isBragRequest, compositeclassifyToolUse()lib/operations.mjs- SharedsaveBragEntry/reviewBragEntries/generateWorkLogwith{ ok: true/false }returnsatomicWriteTextmoved from both entry points intolib/storage.mjsTest results
177 tests, 0 failures (was 127). Three-model adversarial council review passed.
Bug fixes included
isBragRequestfalse negatives on mixed prompts (caught by skeptic review)Partial progress on #22.