From 87b4eed5c1678e8a52376498f2ca98bd7ed04bce Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 11 Nov 2026 21:39:50 +0800 Subject: [PATCH] fix(metrics): fingerprint and track native notebook edits --- scripts/hooks/ecc-metrics-bridge.js | 14 ++-- tests/hooks/ecc-notebook-metrics.test.js | 84 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests/hooks/ecc-notebook-metrics.test.js diff --git a/scripts/hooks/ecc-metrics-bridge.js b/scripts/hooks/ecc-metrics-bridge.js index 31ecad948..6d221b424 100644 --- a/scripts/hooks/ecc-metrics-bridge.js +++ b/scripts/hooks/ecc-metrics-bridge.js @@ -63,7 +63,13 @@ function hashToolCall(toolName, toolInput) { key = crypto .createHash('sha256') .update( - stableStringify({ + stableStringify(name === 'NotebookEdit' ? { + notebook_path: toolInput?.notebook_path, + cell_id: toolInput?.cell_id, + new_source: toolInput?.new_source, + edit_mode: toolInput?.edit_mode, + cell_type: toolInput?.cell_type + } : { file_path: toolInput?.file_path, old_string: toolInput?.old_string, new_string: toolInput?.new_string, @@ -87,7 +93,7 @@ function extractFilePaths(toolName, toolInput) { const paths = []; if (!toolInput || typeof toolInput !== 'object') return paths; - const fp = toolInput.file_path; + const fp = /^NotebookEdit$/i.test(toolName) ? toolInput.notebook_path : toolInput.file_path; if (fp && typeof fp === 'string') paths.push(fp); const edits = toolInput.edits; @@ -229,8 +235,8 @@ function run(rawInput) { bridge.last_timestamp = now; if (!bridge.first_timestamp) bridge.first_timestamp = now; - // Track modified files (Write/Edit/MultiEdit only) - const isWriteOp = /^(Write|Edit|MultiEdit)$/i.test(toolName); + // Track modified files, including notebooks edited through NotebookEdit. + const isWriteOp = /^(Write|Edit|MultiEdit|NotebookEdit)$/i.test(toolName); if (isWriteOp) { const newPaths = extractFilePaths(toolName, toolInput); const existing = new Set(bridge.files_modified || []); diff --git a/tests/hooks/ecc-notebook-metrics.test.js b/tests/hooks/ecc-notebook-metrics.test.js new file mode 100644 index 000000000..1ae04a9c8 --- /dev/null +++ b/tests/hooks/ecc-notebook-metrics.test.js @@ -0,0 +1,84 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const { spawnSync } = require('node:child_process'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const test = require('node:test'); +const { run, hashToolCall, extractFilePaths } = require('../../scripts/hooks/ecc-metrics-bridge'); +const { detectLoop } = require('../../scripts/hooks/ecc-context-monitor'); + +test('NotebookEdit fingerprints distinguish each native edit field', () => { + const original = { + notebook_path: '/repo/analysis.ipynb', cell_id: 'cell-1', + new_source: 'print(1)', edit_mode: 'replace', cell_type: 'code' + }; + const originalHash = hashToolCall('NotebookEdit', original); + for (const change of [ + { notebook_path: '/repo/other.ipynb' }, { cell_id: 'cell-2' }, + { new_source: 'print(2)' }, { edit_mode: 'insert' }, { cell_type: 'markdown' } + ]) { + assert.notEqual(hashToolCall('NotebookEdit', { ...original, ...change }), originalHash, + `NotebookEdit must distinguish ${Object.keys(change)[0]}`); + } + assert.equal(hashToolCall('NotebookEdit', { ...original }), originalHash); +}); + +test('file fingerprints retain existing tool and missing-input behavior', () => { + assert.match(hashToolCall(null, null), /^[0-9a-f]{8}$/); + assert.equal(hashToolCall('NotebookEdit'), hashToolCall('NotebookEdit', {})); + assert.equal(hashToolCall('Bash'), hashToolCall('Bash', { command: '' })); + assert.notEqual(hashToolCall('Read', { file_path: 'a.js' }), hashToolCall('Read', { file_path: 'b.js' })); + assert.notEqual( + hashToolCall('MultiEdit', { edits: [{ old_string: 'a', new_string: 'b' }] }), + hashToolCall('MultiEdit', { edits: [{ old_string: 'a', new_string: 'c' }] }) + ); +}); + +test('NotebookEdit extracts its notebook_path and ignores invalid paths', () => { + assert.deepEqual(extractFilePaths('NotebookEdit', { notebook_path: '/repo/a.ipynb' }), ['/repo/a.ipynb']); + assert.deepEqual(extractFilePaths('NotebookEdit', { notebook_path: 42 }), []); +}); + +test('NotebookEdit tracks notebook scope without false loop warnings', t => { + const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-notebook-metrics-')); + t.after(() => fs.rmSync(tmpHome, { recursive: true, force: true })); + const temporaryDirectory = path.join(tmpHome, 'tmp'); + fs.mkdirSync(temporaryDirectory); + const sessionId = `notebook-edits-${process.pid}`; + const bridgeScript = path.resolve(__dirname, '../../scripts/hooks/ecc-metrics-bridge.js'); + const bridgePath = path.join(temporaryDirectory, `ecc-metrics-${sessionId}.json`); + const readBridge = () => JSON.parse(fs.readFileSync(bridgePath, 'utf8')); + const invoke = toolInput => { + const input = JSON.stringify({ session_id: sessionId, tool_name: 'NotebookEdit', tool_input: toolInput }); + const result = spawnSync(process.execPath, [bridgeScript], { + input, encoding: 'utf8', cwd: tmpHome, timeout: 5000, + env: { + ...process.env, HOME: tmpHome, USERPROFILE: tmpHome, + ECC_AGENT_DATA_HOME: path.join(tmpHome, '.claude'), + CLAUDE_CONFIG_DIR: path.join(tmpHome, '.claude'), + TMPDIR: temporaryDirectory, TEMP: temporaryDirectory, TMP: temporaryDirectory + } + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, input); + }; + const notebook = path.join(tmpHome, 'analysis.ipynb'); + for (let cell = 0; cell < 5; cell++) { + invoke({ notebook_path: notebook, cell_id: `cell-${cell}`, new_source: 'print(1)' }); + } + const distinct = readBridge(); + assert.equal(detectLoop(distinct.recent_tools).detected, false, + 'Editing different notebook cells must not be treated as a stuck loop'); + assert.deepEqual(distinct.files_modified, [notebook]); + assert.equal(distinct.files_modified_count, 1); + const repeated = { notebook_path: notebook, cell_id: 'same-cell', new_source: 'print(2)' }; + for (let repeat = 0; repeat < 5; repeat++) invoke(repeated); + assert.equal(detectLoop(readBridge().recent_tools).detected, true, + 'Identical repeated notebook edits must still be detected'); +}); + +test('malformed hook input remains unchanged', () => { + assert.equal(run('{invalid'), '{invalid'); +});