Skip to content

refactor(guardrails): split guardrail.ts into 6 modules (Issue #150)#154

Merged
terisuke merged 2 commits intodevfrom
refactor/guardrail-modules
Apr 9, 2026
Merged

refactor(guardrails): split guardrail.ts into 6 modules (Issue #150)#154
terisuke merged 2 commits intodevfrom
refactor/guardrail-modules

Conversation

@terisuke
Copy link
Copy Markdown

@terisuke terisuke commented Apr 9, 2026

Summary

  • Split monolithic guardrail.ts (1944 lines) into 6 modules, all under 800 lines
  • Architecture: Context Object + Handler Factories pattern
  • Dependency DAG: patterns <- context <- {review, git, access} <- guardrail.ts
  • Behavior-preserving refactor — no logic changes

Module breakdown

Module Lines Responsibility
guardrail-patterns.ts 225 Constants, pure utilities
guardrail-context.ts 343 GuardrailContext type, createContext()
guardrail-review.ts 198 Auto-review pipeline, dual review gate
guardrail-git.ts 432 Branch hygiene, merge gates, CI checks
guardrail-access.ts 296 File access control, delegation, tracking
guardrail.ts 568 Entry point, hook registration

Closes #150

Test plan

  • 34/34 scenario tests pass
  • Typecheck passes (FULL TURBO)
  • Manual: opencode binary launch + plugin load

🤖 Generated with Claude Code + Codex CLI

Split monolithic guardrail.ts (1944 lines) into modular architecture:
- guardrail-patterns.ts (225L): constants, pure utilities
- guardrail-context.ts (343L): GuardrailContext type, createContext()
- guardrail-review.ts (198L): auto-review pipeline, dual review gate
- guardrail-git.ts (432L): branch hygiene, merge gates, CI checks
- guardrail-access.ts (296L): file access control, delegation, tracking
- guardrail.ts (568L): entry point, hook registration

Architecture: Context Object + Handler Factories pattern.
Dependency DAG: patterns <- context <- {review, git, access} <- entry.
All files under 800-line limit. 34/34 scenario tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Codex CLI <noreply@openai.com>
Copilot AI review requested due to automatic review settings April 9, 2026 16:10
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

New PR opened -- automated review will run on the next push.

To trigger a manual review, comment /review on this PR.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

The following comment was made by an LLM, it may be inaccurate:

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

Refactors the guardrail plugin by splitting the previously monolithic guardrail.ts into smaller, responsibility-focused modules while preserving existing behavior.

Changes:

  • Extracted shared constants/pure helpers into guardrail-patterns.ts.
  • Introduced GuardrailContext + createContext() to centralize state paths, gating helpers, and utilities.
  • Split hook logic into dedicated handler factories for access control, auto-review, and git/merge gates.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/guardrails/profile/plugins/guardrail.ts New entrypoint wiring: builds context and composes access/review/git handlers into the plugin hooks.
packages/guardrails/profile/plugins/guardrail-context.ts Adds GuardrailContext and createContext() to own core state, gating helpers, and shared utilities.
packages/guardrails/profile/plugins/guardrail-patterns.ts Centralizes regex patterns and pure helper utilities previously embedded in guardrail.ts.
packages/guardrails/profile/plugins/guardrail-access.ts Moves file/tool access policy enforcement + mutation tracking into a dedicated handler module.
packages/guardrails/profile/plugins/guardrail-review.ts Moves auto-review pipeline, dual-review gate logic, and Codex-review detection into a dedicated module.
packages/guardrails/profile/plugins/guardrail-git.ts Moves branch hygiene, merge gates, CI checks, and related advisories into a dedicated module.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

free,
has,
line,
list,
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list is imported from ./guardrail-patterns but not used in this module. Removing the unused import will keep the dependency surface minimal and avoid confusion during future refactors.

Suggested change
list,

Copilot uses AI. Check for mistakes.
Comment on lines +62 to +67
if (/\bgit\s+merge(\s|$)/i.test(cmd) || /\bgh\s+pr\s+merge(\s|$)/i.test(cmd)) {
const checks = review.checklist(data)
if (checks.score < 3) {
out.output = (out.output || "") + `\n\nCompletion checklist (${checks.score}/${checks.total}): ${checks.summary}\nBlocking: ${checks.blocking.join(", ")}`
}
}
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge/PR-merge detection regex is repeated here even though the same condition was already evaluated earlier in bashBeforeGit (see the first if block). Consider computing a single isMerge boolean once and reusing it to reduce duplication and keep the merge-gating logic easier to maintain.

Copilot uses AI. Check for mistakes.
Codex CLI review P2: helper modules had `export default { id, server }`
which matches the plugin shape. While opencode.json explicitly lists
plugins (no auto-discovery), removing these prevents confusion and
keeps helper modules as pure library code.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@terisuke terisuke merged commit a48b011 into dev Apr 9, 2026
6 of 7 checks passed
terisuke added a commit that referenced this pull request Apr 10, 2026
…licate merge regex

- Remove unused `list` import from guardrail-context.ts
- Compute isMerge/isPrMerge booleans once in bashBeforeGit and reuse
- Same pattern applied to afterBashGit handler (afterIsMerge, afterIsPrMerge, etc.)

Addresses review comments on PR #154.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
terisuke added a commit that referenced this pull request Apr 10, 2026
fix(guardrails): address PR #154 review — remove unused import, deduplicate merge regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(guardrails): guardrail.ts 1850行 → モジュール分割 (800行制限超過)

2 participants