Skip to content

refactor: extract lib/heuristics.mjs + lib/operations.mjs (Phase 0)#41

Merged
vidhartbhatia merged 6 commits into
mainfrom
phase0-extract-heuristics-operations
May 13, 2026
Merged

refactor: extract lib/heuristics.mjs + lib/operations.mjs (Phase 0)#41
vidhartbhatia merged 6 commits into
mainfrom
phase0-extract-heuristics-operations

Conversation

@vidhartbhatia
Copy link
Copy Markdown
Collaborator

Phase 0: Extract shared lib modules

Problem

extension.mjs and mcp-server.mjs duplicate tool classification logic, brag-entry orchestration, and atomicWriteText. 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, composite classifyToolUse()
  • lib/operations.mjs - Shared saveBragEntry/reviewBragEntries/generateWorkLog with { ok: true/false } returns
  • atomicWriteText moved from both entry points into lib/storage.mjs

Test results

177 tests, 0 failures (was 127). Three-model adversarial council review passed.

Bug fixes included

  • isBragRequest false negatives on mixed prompts (caught by skeptic review)
  • git config fallback NPE risk in extension.mjs (caught by architect review)

Partial progress on #22.

vidhartbhatia and others added 4 commits May 12, 2026 11:04
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>
@vidhartbhatia vidhartbhatia requested a review from a team as a code owner May 12, 2026 21:58
- 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>
@vidhartbhatia vidhartbhatia requested a review from Copilot May 13, 2026 21:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vidhartbhatia vidhartbhatia merged commit abc9b04 into main May 13, 2026
14 checks passed
Copy link
Copy Markdown

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

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.mjs with tool classification sets, extraction helpers, and classifyToolUse composite.
  • New lib/operations.mjs providing saveBragEntry / reviewBragEntries / generateWorkLog with { ok } discriminated returns; both entry points now delegate to it.
  • atomicWriteText consolidated into lib/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 thread lib/operations.mjs
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 };
Comment thread lib/operations.mjs
*/
export function saveBragEntry(args, { dataDir, config, gitConfig }) {
// Validate summary
if (!args.summary?.trim()) {
Comment thread lib/heuristics.mjs
* 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.
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