From af5ccb77ab599731f5c5838f4d7377815360083a Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:58:43 -0500 Subject: [PATCH] test: control context suggestion budgets through the hook wrapper --- .../hooks/context-prompt-suggestions.test.js | 29 +++++++++++++++---- tests/lib/helpers/context-suggestion-clock.js | 19 ++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 tests/lib/helpers/context-suggestion-clock.js diff --git a/tests/hooks/context-prompt-suggestions.test.js b/tests/hooks/context-prompt-suggestions.test.js index b6c95bef3..1e636a49c 100644 --- a/tests/hooks/context-prompt-suggestions.test.js +++ b/tests/hooks/context-prompt-suggestions.test.js @@ -69,12 +69,31 @@ test('a tampered index or exhausted budget fails open to no suggestion', () => f assert.match(result.stderr, /integrity|index/); })); -test('the hook runs through run-with-flags and returns its text on stdout', () => fixture(({ env }) => { +function controlledWrapper(env) { const wrapper = path.join(__dirname, '../../scripts/hooks/run-with-flags.js'); - const result = spawnSync(process.execPath, [wrapper, 'user-prompt:context-suggestions', 'scripts/hooks/context-prompt-suggestions.js'], - { input: PROMPT, encoding: 'utf8', env: { PATH: process.env.PATH, ...env }, timeout: 30000 }); - assert.equal(result.status, 0); - assert.match(result.stdout, /skill:feature/); + const clock = path.join(__dirname, '../lib/helpers/context-suggestion-clock.js'); + const result = spawnSync(process.execPath, ['--require', clock, wrapper, + 'user-prompt:context-suggestions', 'scripts/hooks/context-prompt-suggestions.js'], + { input: PROMPT, encoding: 'utf8', env: { PATH: process.env.PATH, + HOME: path.dirname(env.ECC_CONTEXT_STATE_ROOT), USERPROFILE: path.dirname(env.ECC_CONTEXT_STATE_ROOT), + ECC_HOOK_PROFILE: 'standard', ECC_HOOKS_ENABLED: 'true', ECC_DISABLED_HOOKS: '', ...env }, timeout: 30000 }); + assert.ifError(result.error); + assert.equal(result.status, 0, result.stderr); + assert.match(result.stderr, /routing load consumed 200ms/); + return result; +} + +test('the wrapper returns suggestions within an explicitly configured budget', () => fixture(({ env }) => { + // Account for a deterministic 200ms cold routing load without relying on + // host scheduling. This positive case explicitly opts into the supported max. + const result = controlledWrapper({ ...env, ECC_CONTEXT_SUGGEST_BUDGET_MS: '1000' }); + assert.match(result.stdout, /skill:feature/, result.stderr); +})); + +test('the wrapper stays silent when cold loading exhausts its budget', () => fixture(({ env }) => { + // The same work exceeds the unchanged 150ms default. Zero remains fail-open. + assert.equal(controlledWrapper(env).stdout, ''); + assert.equal(controlledWrapper({ ...env, ECC_CONTEXT_SUGGEST_BUDGET_MS: '0' }).stdout, ''); })); test('the session-start builder indexes the current generation once and stays silent when disabled', () => { diff --git a/tests/lib/helpers/context-suggestion-clock.js b/tests/lib/helpers/context-suggestion-clock.js new file mode 100644 index 000000000..8a995bb61 --- /dev/null +++ b/tests/lib/helpers/context-suggestion-clock.js @@ -0,0 +1,19 @@ +'use strict'; + +// Deterministic cold-load accounting for a real run-with-flags subprocess. +// The subprocess's 30-second wall-clock deadline remains outside this clock. +const Module = require('node:module'); +const path = require('node:path'); +const load = Module._load; +const hookPath = path.resolve(__dirname, '../../../scripts/hooks/context-prompt-suggestions.js'); +let elapsed = 0; +Date.now = () => elapsed; + +Module._load = function (request, parent, isMain) { + const loaded = load.call(this, request, parent, isMain); + if (request === '../lib/context-routing-index' && parent?.filename === hookPath) { + elapsed += 200; + process.stderr.write('[TestClock] routing load consumed 200ms\n'); + } + return loaded; +};