1414 */
1515
1616import type { PluginInput } from "@opencode-ai/plugin"
17+ import * as fs from "fs"
18+ import * as path from "path"
19+ import {
20+ initStore ,
21+ recordChange ,
22+ clearChanges ,
23+ } from "./lib/changed-files-store.js"
24+ import changedFilesTool from "../tools/changed-files.js"
1725
1826export const ECCHooksPlugin = async ( {
1927 client,
@@ -23,9 +31,25 @@ export const ECCHooksPlugin = async ({
2331} : PluginInput ) => {
2432 type HookProfile = "minimal" | "standard" | "strict"
2533
26- // Track files edited in current session for console.log audit
34+ const worktreePath = worktree || directory
35+ initStore ( worktreePath )
36+
2737 const editedFiles = new Set < string > ( )
2838
39+ function resolvePath ( p : string ) : string {
40+ if ( path . isAbsolute ( p ) ) return p
41+ return path . join ( worktreePath , p )
42+ }
43+
44+ const pendingToolChanges = new Map < string , { path : string ; type : "added" | "modified" } > ( )
45+ let writeCounter = 0
46+
47+ function getFilePath ( args : Record < string , unknown > | undefined ) : string | null {
48+ if ( ! args ) return null
49+ const p = ( args . filePath ?? args . file_path ?? args . path ) as string | undefined
50+ return typeof p === "string" && p . trim ( ) ? p : null
51+ }
52+
2953 // Helper to call the SDK's log API with correct signature
3054 const log = ( level : "debug" | "info" | "warn" | "error" , message : string ) =>
3155 client . app . log ( { body : { service : "ecc" , level, message } } )
@@ -73,8 +97,8 @@ export const ECCHooksPlugin = async ({
7397 * Action: Runs prettier --write on the file
7498 */
7599 "file.edited" : async ( event : { path : string } ) => {
76- // Track edited files for console.log audit
77100 editedFiles . add ( event . path )
101+ recordChange ( event . path , "modified" )
78102
79103 // Auto-format JS/TS files
80104 if ( hookEnabled ( "post:edit:format" , [ "strict" ] ) && event . path . match ( / \. ( t s | t s x | j s | j s x ) $ / ) ) {
@@ -111,9 +135,24 @@ export const ECCHooksPlugin = async ({
111135 * Action: Runs tsc --noEmit to check for type errors
112136 */
113137 "tool.execute.after" : async (
114- input : { tool : string ; args ?: { filePath ?: string } } ,
138+ input : { tool : string ; callID ?: string ; args ?: { filePath ?: string ; file_path ?: string ; path ?: string } } ,
115139 output : unknown
116140 ) => {
141+ const filePath = getFilePath ( input . args as Record < string , unknown > )
142+ if ( input . tool === "edit" && filePath ) {
143+ recordChange ( filePath , "modified" )
144+ }
145+ if ( input . tool === "write" && filePath ) {
146+ const key = input . callID ?? `write-${ ++ writeCounter } -${ filePath } `
147+ const pending = pendingToolChanges . get ( key )
148+ if ( pending ) {
149+ recordChange ( pending . path , pending . type )
150+ pendingToolChanges . delete ( key )
151+ } else {
152+ recordChange ( filePath , "modified" )
153+ }
154+ }
155+
117156 // Check if a TypeScript file was edited
118157 if (
119158 hookEnabled ( "post:edit:typecheck" , [ "strict" ] ) &&
@@ -152,8 +191,25 @@ export const ECCHooksPlugin = async ({
152191 * Action: Warns about potential security issues
153192 */
154193 "tool.execute.before" : async (
155- input : { tool : string ; args ?: Record < string , unknown > }
194+ input : { tool : string ; callID ?: string ; args ?: Record < string , unknown > }
156195 ) => {
196+ if ( input . tool === "write" ) {
197+ const filePath = getFilePath ( input . args )
198+ if ( filePath ) {
199+ const absPath = resolvePath ( filePath )
200+ let type : "added" | "modified" = "modified"
201+ try {
202+ if ( typeof fs . existsSync === "function" ) {
203+ type = fs . existsSync ( absPath ) ? "modified" : "added"
204+ }
205+ } catch {
206+ type = "modified"
207+ }
208+ const key = input . callID ?? `write-${ ++ writeCounter } -${ filePath } `
209+ pendingToolChanges . set ( key , { path : filePath , type } )
210+ }
211+ }
212+
157213 // Git push review reminder
158214 if (
159215 hookEnabled ( "pre:bash:git-push-reminder" , "strict" ) &&
@@ -293,6 +349,8 @@ export const ECCHooksPlugin = async ({
293349 if ( ! hookEnabled ( "session:end-marker" , [ "minimal" , "standard" , "strict" ] ) ) return
294350 log ( "info" , "[ECC] Session ended - cleaning up" )
295351 editedFiles . clear ( )
352+ clearChanges ( )
353+ pendingToolChanges . clear ( )
296354 } ,
297355
298356 /**
@@ -303,6 +361,10 @@ export const ECCHooksPlugin = async ({
303361 * Action: Updates tracking
304362 */
305363 "file.watcher.updated" : async ( event : { path : string ; type : string } ) => {
364+ let changeType : "added" | "modified" | "deleted" = "modified"
365+ if ( event . type === "create" || event . type === "add" ) changeType = "added"
366+ else if ( event . type === "delete" || event . type === "remove" ) changeType = "deleted"
367+ recordChange ( event . path , changeType )
306368 if ( event . type === "change" && event . path . match ( / \. ( t s | t s x | j s | j s x ) $ / ) ) {
307369 editedFiles . add ( event . path )
308370 }
@@ -394,7 +456,7 @@ export const ECCHooksPlugin = async ({
394456 "" ,
395457 "## Active Plugin: Everything Claude Code v1.8.0" ,
396458 "- Hooks: file.edited, tool.execute.before/after, session.created/idle/deleted, shell.env, compacting, permission.ask" ,
397- "- Tools: run-tests, check-coverage, security-audit, format-code, lint-check, git-summary" ,
459+ "- Tools: run-tests, check-coverage, security-audit, format-code, lint-check, git-summary, changed-files " ,
398460 "- Agents: 13 specialized (planner, architect, tdd-guide, code-reviewer, security-reviewer, build-error-resolver, e2e-runner, refactor-cleaner, doc-updater, go-reviewer, go-build-resolver, database-reviewer, python-reviewer)" ,
399461 "" ,
400462 "## Key Principles" ,
@@ -449,6 +511,10 @@ export const ECCHooksPlugin = async ({
449511 // Everything else: let user decide
450512 return { approved : undefined }
451513 } ,
514+
515+ tool : {
516+ "changed-files" : changedFilesTool ,
517+ } ,
452518 }
453519}
454520
0 commit comments