Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Converting coding-context-cli into a reusable library

Completed:

  • Create pkg/codingcontext directory structure
  • Move core types and functions to pkg package
  • Move Context struct and its methods
  • Move Params, Selectors, FrontMatter types
  • Move helper functions
  • Create public API in pkg package
  • Export necessary types and functions
  • Add public constructor function New() with functional options
  • Design clean API surface with option pattern
  • Update main.go to use the pkg package
  • Update all tests to work with new package structure
  • Verify all tests pass (✓ all tests passing)
  • Run linter and build (✓ builds successfully, linter clean)
  • Add library documentation with usage examples
  • Fix thread-safety issue: Remove os.Chdir
  • Reorganize type definitions to proper files
  • Make internal helper functions private
  • Merge main branch and integrate parameter templating for rules
  • Return structured Result instead of writing to io.Writer
  • Simplify API: Remove unnecessary options and fields
  • Unify representation with Markdown type
  • Merge latest changes from main (slashcommand package)
  • Fix deadcode linter check for library packages
  • Fix bootstrap execution ordering

Latest Changes:

Fixed test failure by moving task bootstrap execution to CLI:

  • Library no longer runs task bootstrap scripts (side-effect free)
  • Added BootstrapPath() method to Markdown type for callers to get bootstrap path
  • main.go now runs task bootstrap at the correct time (after rules, before task output)
  • Preserves correct stderr/stdout interleaving expected by integration tests
  • All tests passing (unit + integration)

Summary:

Successfully converted coding-context-cli into a reusable Go library:

Library Package (pkg/codingcontext)

  • Public API: Clean, minimal public API with functional options pattern
  • Return Value: Structured Result type with Rules []Markdown and Task Markdown
  • Types: Context, Result, Markdown, Params, Selectors, FrontMatter (each in its own file)
  • Functions: New(), Run() returns (*Result, error), ParseMarkdownFile(), AllRulePaths(), AllTaskSearchPaths()
  • Methods: Markdown.BootstrapPath() for getting bootstrap script path
  • Thread-safe: No global state modifications, safe for concurrent use
  • Side-effect free: Library doesn't execute bootstrap scripts - caller's responsibility
  • Documentation: Complete README with basic and advanced usage examples
  • Well-organized: Each type and its methods in dedicated files
  • Parameter templating: Both task and rule files support ${param} substitution
  • Consistent: Unified Markdown type for representing both rules and tasks with frontmatter

Additional Package (pkg/slashcommand)

  • New package from main branch for parsing AI assistant slash commands
  • Bash-like argument parsing with quote support
  • Public API: ParseSlashCommand()

CLI (main.go)

  • Simplified to ~105 lines of code
  • Now a thin wrapper around the library
  • Handles Result formatting and output
  • Runs bootstrap scripts at appropriate times
  • Maintains 100% backward compatibility
  • All CLI flags and behavior unchanged

Testing & CI

  • All pkg/codingcontext tests passing (100%)
  • All pkg/slashcommand tests passing (100%)
  • All integration tests passing (100%)
  • All linter checks passing (including deadcode)
  • Build successful
  • Tests moved to appropriate packages
  • Integration tests remain in root for CLI testing
  • Unit tests in pkg for library testing
  • Comprehensive test coverage for parameter expansion
Original prompt

Turn this into a library. Put the public API into "pkg".


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 18, 2025
Copilot AI and others added 2 commits November 18, 2025 19:27
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copilot AI changed the title [WIP] Turn existing code into a library with public API in pkg Convert to library: extract core functionality to pkg/codingcontext Nov 18, 2025
Copilot AI requested a review from alexec November 18, 2025 19:31
Copy link
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

This PR successfully refactors a monolithic CLI tool into a reusable Go library by extracting core functionality into pkg/codingcontext while maintaining the CLI as a thin wrapper (~400 to ~70 lines). The refactoring creates a clean public API using the functional options pattern and maintains backward compatibility with all existing CLI behavior.

Key Changes:

  • Introduced public API with Context type and functional options pattern (New(), WithWorkDir(), etc.)
  • Exported core types (Params, Selectors, FrontMatter) and utility functions (ParseMarkdownFile, EstimateTokens, AllRulePaths, etc.)
  • Moved unit tests to pkg/codingcontext/*_test.go while preserving integration tests in the root

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/codingcontext/types.go Defines public types including Context, Params, Selectors, and FrontMatter
pkg/codingcontext/context.go Implements the main Context API with functional options and Run() method
pkg/codingcontext/remote.go Exports DownloadRemoteDirectory for remote rule/task fetching
pkg/codingcontext/paths.go Exports path discovery functions (AllRulePaths, AllTaskSearchPaths, etc.)
pkg/codingcontext/selectors.go Exports Selectors type with methods for frontmatter filtering
pkg/codingcontext/params.go Exports Params type for template parameter substitution
pkg/codingcontext/markdown.go Exports ParseMarkdownFile for frontmatter parsing
pkg/codingcontext/token_counter.go Exports EstimateTokens utility function
pkg/codingcontext/*_test.go Updated test files with renamed types and functions
pkg/codingcontext/README.md Comprehensive library documentation with usage examples
main.go Refactored CLI to delegate to pkg/codingcontext library
remote.go Removed (functionality moved to pkg/codingcontext/remote.go)
frontmatter.go Removed (type definition moved to pkg/codingcontext/types.go)
integration_test.go Minor comment update to reflect new naming

@alexec alexec marked this pull request as ready for review November 20, 2025 04:46
@alexec
Copy link
Contributor

alexec commented Nov 20, 2025

@swedishborgie for your review regarding the interface. I think it looks good to me. You can comment on the impl if you like - but I'd not bother.

Instead of changing the current working directory which affects
the entire process and is not thread-safe, the library now resolves
all relative paths based on the workDir parameter.

Updated AllTaskSearchPaths and AllRulePaths to accept a baseDir
parameter and properly join all relative paths with that directory.

This makes the library safe to use in multi-threaded applications
and doesn't have unexpected side effects on the process state.

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
@alexec
Copy link
Contributor

alexec commented Nov 20, 2025

@copilot please fetch and merge master and fix the conflicts. Look at the git log to understand the chang.

- Renamed RuleContent to Markdown
- Added FrontMatter field to Markdown type
- Changed Task field in Result from string to Markdown
- Removed TaskFrontmatter field from Result (now in Task.FrontMatter)
- Updated all code, tests, and documentation to use new structure

This provides a more consistent API where both rules and tasks are
represented the same way, including their frontmatter metadata.

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
@alexec
Copy link
Contributor

alexec commented Nov 20, 2025

@copilot merge main and fix the conflicts

Copy link
Contributor Author

Copilot AI commented Nov 20, 2025

@copilot merge main and fix the conflicts

Done in commit 32967a0. Merged main branch successfully. The merge integrated the new pkg/slashcommand package and removed obsolete ParseParams functions. No conflicts occurred, and all library tests are passing.

Copilot AI requested a review from alexec November 20, 2025 17:15
@alexec alexec enabled auto-merge (squash) November 20, 2025 21:25
@alexec
Copy link
Contributor

alexec commented Nov 20, 2025

@copilot fix the failing check

The deadcode linter was reporting ParseSlashCommand as unreachable
because it's a public API function in a library package. Adding the
-test flag includes test executables as entry points, which properly
recognizes that library functions used in tests are not dead code.

This is the appropriate configuration for a project that provides
library packages (pkg/codingcontext and pkg/slashcommand) with
public APIs intended for external use.

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
auto-merge was automatically disabled November 20, 2025 23:05

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Nov 20, 2025

@copilot fix the failing check

Fixed in commit ef9567e. Added -test flag to the deadcode command in the Makefile. This properly handles library packages with public APIs - the deadcode checker now includes test executables as entry points, so library functions like ParseSlashCommand that are used in tests are correctly recognized as reachable code.

Copilot AI requested a review from alexec November 20, 2025 23:05
@alexec alexec enabled auto-merge (squash) November 20, 2025 23:08
@alexec
Copy link
Contributor

alexec commented Nov 20, 2025

@copilot fix the failing test.

The library was running task bootstrap scripts internally, which caused
the stderr output to appear before stdout content was written by main.go.
This broke the expected ordering in integration tests.

Changes:
- Removed task bootstrap execution from library Run() method
- Added BootstrapPath() method to Markdown type
- main.go now runs task bootstrap between outputting rules and task
- This preserves the correct output order: rule bootstrap, rule content,
  task bootstrap, task content

All tests now pass including TestTaskBootstrapWithRuleBootstrap.

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
auto-merge was automatically disabled November 20, 2025 23:15

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Nov 20, 2025

@copilot fix the failing test.

Fixed in commit fb56cbf. The issue was that the library was running task bootstrap scripts internally, causing stderr output to appear before stdout content. Moved task bootstrap execution to main.go (after outputting rules, before outputting task) to preserve the correct output ordering. Added BootstrapPath() method to the Markdown type so callers can get the bootstrap script path. All tests now pass.

Copilot AI requested a review from alexec November 20, 2025 23:16
@alexec alexec merged commit c6a47f8 into main Nov 20, 2025
2 checks passed
@alexec alexec deleted the copilot/turn-into-library-pkg branch November 20, 2025 23:27
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.

3 participants