Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"name": "NovusEdge",
"url": "https://github.com/NovusEdge"
},
"description": "Blocks agent turns that add duplicated code.",
"description": "Warns when agent turns add duplicated code.",
"plugins": [
{
"name": "drygate",
"source": "./",
"description": "Blocks the end of a turn that added duplicated code, using jscpd, and gives the agent jscpd's MCP tools to check for existing code first.",
"description": "Warns at the end of a turn that added duplicated code, using jscpd, and gives the agent jscpd's MCP tools to check for existing code first.",
"keywords": [
"dry",
"duplication",
Expand Down
4 changes: 2 additions & 2 deletions .claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "drygate",
"displayName": "drygate",
"version": "0.1.0",
"description": "Blocks the end of a turn that added duplicated code, using jscpd, and gives the agent jscpd's MCP tools to check for existing code first.",
"version": "0.2.0",
"description": "Warns at the end of a turn that added duplicated code, using jscpd, and gives the agent jscpd's MCP tools to check for existing code first.",
"author": {
"name": "NovusEdge"
},
Expand Down
6 changes: 3 additions & 3 deletions .codex-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "drygate",
"version": "0.1.0",
"description": "Blocks the end of a turn that added duplicated code, using jscpd.",
"version": "0.2.0",
"description": "Warns at the end of a turn that added duplicated code, using jscpd.",
"author": {
"name": "NovusEdge",
"url": "https://github.com/NovusEdge"
Expand All @@ -21,7 +21,7 @@
"hooks": "./hooks/hooks.json",
"interface": {
"displayName": "drygate",
"shortDescription": "Blocks agent turns that add duplicated code.",
"shortDescription": "Warns when agent turns add duplicated code.",
"developerName": "NovusEdge",
"category": "Coding",
"websiteURL": "https://github.com/NovusEdge/drygate"
Expand Down
9 changes: 6 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# drygate

- `hooks/drygate.js` is the one hook for Claude Code and Codex. Both send the same Stop and
SessionStart JSON and read `{"decision":"block"}` from stdout. Codex has no
SessionStart JSON and read `systemMessage` from stdout. Codex has no
`CLAUDE_PROJECT_DIR`, so the hook falls back to the stdin `cwd`.
- The hook must exit 0 on every path and report failures as `systemMessage`. Exit 2 from a
- The Stop hook warns and never returns `{"decision":"block"}`. All Stop hooks share one
`stop_hook_active` flag, so a drygate block let flocc's Stop hook end a turn its agent
had not finished.
- The hook must exit 0 on every path and report everything as `systemMessage`. Exit 2 from a
Stop hook blocks the turn, and a crash shows a hook error on every turn in every repo that
enables the plugin.
- `VERSION` in `hooks/jscpd.js` pins the jscpd that drygate installs. The blocking rule reads
- `VERSION` in `hooks/jscpd.js` pins the jscpd that drygate installs. The warning rule reads
jscpd's JSON report (`fragment`, file names), so bump it only with the tests passing
against the new version.
- Options reach jscpd through a temp config file from `config()`, never `--ignore`: jscpd
Expand Down
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

drygate keeps coding agents from adding duplicated code. It has three parts:

- **A stop gate.** When the agent finishes a turn, drygate runs [jscpd](https://github.com/kucherenko/jscpd)
over the repository. If the session added a block that repeats code elsewhere, the turn
does not end: the agent gets the list of copies and has to remove them first.
- **A stop check.** When the agent finishes a turn, drygate runs [jscpd](https://github.com/kucherenko/jscpd)
over the repository. If the session added a block that repeats code elsewhere, drygate
shows the user the list of copies. It warns and never blocks the turn: Claude Code shares
one `stop_hook_active` flag between all Stop hooks, so a block from drygate would use up
the one refusal another Stop hook relies on.
- **A sweep**, `/drygate:sweep`, for code jscpd cannot see: functions that reimplement the
standard library or an installed dependency, and wrappers that add nothing. Haiku scouts
read the code in parallel batches and Sonnet verifies every candidate before it is
reported.
- **jscpd's MCP server**, so the agent can check a draft against the codebase before it
writes it (Claude Code only).

It works in Claude Code and Codex. It needs Node 20 or later and git. The gate does nothing
It works in Claude Code and Codex. It needs Node 20 or later and git. The check does nothing
outside a git repository.

## Install
Expand Down Expand Up @@ -49,22 +51,21 @@ once per machine, in the background; until that finishes, stops are not checked
A failed install, for example offline or behind a proxy npm cannot reach, is retried after
10 minutes. To use a specific binary, set `DRYGATE_JSCPD` to its path.

## How the gate decides
## How the check decides

At session start, drygate scans the working tree in a background process and counts the
clones by the pair of files they join and by the code they repeat. At each stop it scans
again and blocks on a clone when both its file pair and its code are more common than in
the snapshot. After a stop that passes, the snapshot moves forward to the current tree.

- Duplication that was already there, committed or not, never blocks. Editing an old clone,
renaming its file or moving it does not block either.
- A third copy of code that was already duplicated blocks.
- A copy the agent commits before it stops still blocks. drygate compares against the
again and warns on a clone when both its file pair and its code are more common than in
the snapshot. After every checked stop, the snapshot moves forward to the current tree, so
each copy is reported once.

- Duplication that was already there, committed or not, is never reported. Editing an old
clone, renaming its file or moving it is not reported either.
- A third copy of code that was already duplicated is reported.
- A copy the agent commits before it stops is still reported. drygate compares against the
snapshot, not against `HEAD`.
- A copy with its local names changed still counts (renamed-identifier matching).
- After one block, the next stop is let through, the user sees what is left, and those
copies are accepted from then on.
- On resume, drygate takes a new snapshot, so commits pulled in between do not block.
- On resume, drygate takes a new snapshot, so commits pulled in between are not reported.
Uncommitted changes since the last check stay out of it: a turn the user interrupted had
no stop, so they are still checked.
- A stop with no file changed since the last check skips the scan.
Expand All @@ -85,25 +86,28 @@ renamed-copy detection and any other jscpd option. drygate still adds worktrees,
submodules and nested clones to its `ignore` list. See jscpd's
[options](https://github.com/kucherenko/jscpd#options).

To keep a deliberate duplicate, wrap it in comments the gate skips:
To keep a deliberate duplicate out of the report, wrap it in comments the check skips:

```js
// jscpd:ignore-start
...
// jscpd:ignore-end
```

`DRYGATE_DISABLE=1` in the environment turns the gate off.
`DRYGATE_DISABLE=1` in the environment turns the check off.

## Limits

- The gate finds copies of text: exact copies, renamed copies and near-copies. It does not
- The warning goes to the user, not to the agent: a Stop hook cannot add to the model's
context without blocking. In an unattended session nobody sees it; run
`/drygate:sweep` or review the diff instead.
- The check finds copies of text: exact copies, renamed copies and near-copies. It does not
find two functions that do the same thing written differently. The sweep covers part of
that: code the standard library or a dependency already provides.
- Removing one clone and adding a different one between the same two files, in the same
turn, is not caught: the counts do not change.
- A clone that existed before the session, in a file the agent edits before the start
snapshot finishes, counts as new and blocks once. The snapshot takes a few seconds at most,
snapshot finishes, counts as new and is reported once. The snapshot takes a few seconds at most,
and in an interactive session the first edit comes after the user's first prompt.
- The sweep reads every file in scope with a model. It reports the batch and line counts
first and asks before sweeping more than 300,000 lines.
Expand Down
35 changes: 14 additions & 21 deletions hooks/drygate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node
// Blocks the end of a turn that added duplicated code. Claude Code and Codex
// Warns at the end of a turn that added duplicated code. Claude Code and Codex
// both run it with the event JSON on stdin and read a decision from stdout.
//
// It warns instead of blocking because every Stop hook shares one
// stop_hook_active flag: a block from drygate used up the one refusal another
// plugin's Stop hook relies on, such as a harness that refuses to end a turn
// before its agent has reported.
//
// SessionStart snapshots the clones in the working tree. Stop scans again and
// blocks on clones the session added, then moves the snapshot forward. The
// warns on clones the session added, then moves the snapshot forward. The
// snapshot is of the working tree, not of a git ref: jscpd --changed rebuilds
// its baseline when HEAD moves, so a copy the agent committed before stopping
// went unreported.
//
// Always exits 0 and reports failures as systemMessage. Exit 2 from a Stop
// hook blocks the turn, and a crash shows an error on every turn in every repo
// that enables the plugin.
// Always exits 0 and reports through systemMessage. Exit 2 from a Stop hook
// blocks the turn, and a crash shows an error on every turn in every repo that
// enables the plugin.

const crypto = require('crypto');
const fs = require('fs');
Expand All @@ -26,11 +31,6 @@ const PENDING_TTL = 10 * 60 * 1000;
const PENDING_WAIT = 60 * 1000;
const STOP_SCAN_TIMEOUT = 50 * 1000;

const MESSAGE =
'Remove each duplicate before you finish: call or import the existing code, or move the ' +
'shared part into one function that both places use. If the user wants a duplicate kept, ' +
'wrap it in jscpd:ignore-start and jscpd:ignore-end comments and say so in your reply.';

function scan(root, { allowInstall, timeout, ignore = [] }) {
const cmd = jscpd.command({ allowInstall });
if (!cmd) return null;
Expand Down Expand Up @@ -223,19 +223,12 @@ function stop(input, root, state) {
}
const before = JSON.parse(fs.readFileSync(state.snapshot, 'utf8'));
const found = added(before, clones);
// The snapshot moves forward after a pass, and after a second stop lets
// remaining duplicates through: those were reported once, and blocking on
// them again every turn would make the user turn the gate off.
if (!found.length || input.stop_hook_active) {
save(state.snapshot, tally(clones));
fs.writeFileSync(state.passed, fp);
}
// The snapshot always moves forward, so each duplicate is reported once.
save(state.snapshot, tally(clones));
fs.writeFileSync(state.passed, fp);
if (!found.length) return null;
const list = found.map((c) => describe(root, c)).join('\n');
if (input.stop_hook_active) {
return { systemMessage: `drygate: duplicated code kept this turn:\n${list}` };
}
return { decision: 'block', reason: `drygate: this session added duplicated code.\n${list}\n\n${MESSAGE}` };
return { systemMessage: `drygate: this session added duplicated code:\n${list}` };
}

function stateFor(input, root) {
Expand Down
Loading
Loading