mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 05:22:17 +00:00
- R2-1: the V2 execute.after adapter passed { result: ... } as the tool
output; skill-run-tracker derives outcomes from error fields on the
response itself, so failed skills recorded as successes. The adapter
now forwards the raw event.result ?? event.output. Covered by a T05
assertion that an is_error skill event records outcome failure and a
clean event still records success.
- R2-2: cmd.exe expands %VAR% and honors embedded quotes even inside
double quotes, so escaping " was unsafe. spawnCommand now refuses
arguments carrying ", %, CR or LF (nothing spawns; the rejection
surfaces through the formatter's catch) and emits the canonical /s
form with one outer quote pair around the whole command. T01 asserts
the outer pair, strips it before inspecting the inner quoting, and
covers two refusal payloads.
- R2-3: repo review rules ban console.log; the client-less log fallback
and the startup line now use console.info (identical stdout behavior).
- R2-4: declined installs recorded before the barrel split retained the
old module/factory stub in plugins/index.ts, and a later selective
install (e.g. commands-core) has no operation rewriting it, so the
strict deactivation gate failed the install after all writes landed.
The strict branch now migrates such retained files in place only when
ownership is proven twice (managed-recorded contentSha256 matches disk
AND disk equals an exact historical disabled-stub payload), written
through writeFileNoFollow with that digest as expectedContent;
anything else keeps failing closed. The preflight branch accepts an
already-inactive recorded config under the same digest-verified
condition, since selective installs legitimately plan no .opencode
writes. New lifecycle test covers both the migration (install ok,
doctor clean, ownership retained) and the refusal path (mis-recorded
digest never grants a rewrite).
499 lines
17 KiB
JavaScript
499 lines
17 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Explicit consent gate for materializing the automatic hook runtime.
|
|
*
|
|
* The capability disclosure and held-materialization semantics were
|
|
* contributed in PR #2634 by Samarjeet Singh Tomar (@samartomar); this
|
|
* module integrates them with the single-decision consent model used by
|
|
* the guided installer.
|
|
*/
|
|
|
|
const HOOK_CAPABILITY_GROUPS = Object.freeze([
|
|
Object.freeze({
|
|
id: 'automatic-source-writes',
|
|
description: 'Automatically format or otherwise modify project source files.',
|
|
}),
|
|
Object.freeze({
|
|
id: 'command-rewrite-and-process-control',
|
|
description: 'Rewrite requested commands and start, replace, or terminate processes.',
|
|
}),
|
|
Object.freeze({
|
|
id: 'transcript-derived-llm-egress',
|
|
description: 'Send transcript-derived conversation text to an external LLM.',
|
|
}),
|
|
Object.freeze({
|
|
id: 'mcp-network-and-process-activity',
|
|
description: 'Probe MCP endpoints and launch, reconnect, or terminate MCP processes.',
|
|
}),
|
|
Object.freeze({
|
|
id: 'automatic-permission-gates',
|
|
description: 'Automatically deny or alter Edit, Write, Bash, and configuration operations.',
|
|
}),
|
|
Object.freeze({
|
|
id: 'session-observation-and-cost-records',
|
|
description: 'Persist session, observation, governance, notification, and cost records.',
|
|
}),
|
|
]);
|
|
|
|
const HOOK_CONSENT_DECISIONS = Object.freeze(['enabled', 'declined']);
|
|
const HOOK_RUNTIME_MODULE_ID = 'hooks-runtime';
|
|
const OPENCODE_DISABLE_ECC_HOOKS_TRANSFORM = 'opencode-disable-ecc-hooks';
|
|
const OPENCODE_DISABLE_PLUGIN_TRANSFORM = 'opencode-disable-plugin-entrypoint';
|
|
|
|
function normalizeOperationPath(value) {
|
|
return String(value || '').replace(/\\/g, '/').toLowerCase();
|
|
}
|
|
|
|
/**
|
|
* Parse an OpenCode config the way OpenCode itself does: comments and the
|
|
* trailing commas they hide are legal (JSONC). The installer only ever
|
|
* reads these files (the ownership guard never writes user-owned configs),
|
|
* so a tolerant read prevents a membership check from crashing the install
|
|
* on a config OpenCode accepts.
|
|
*/
|
|
function parseJsoncConfig(content, sourceRelativePath) {
|
|
let config;
|
|
try {
|
|
config = JSON.parse(stripJsonc(content));
|
|
} catch (error) {
|
|
throw new Error(`Failed to parse ${sourceRelativePath}: ${error.message}`);
|
|
}
|
|
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
|
throw new Error(`Invalid ${sourceRelativePath}: expected a JSON object`);
|
|
}
|
|
return config;
|
|
}
|
|
|
|
function isLineBreak(ch) {
|
|
return ch === '\n' || ch === '\r';
|
|
}
|
|
|
|
/**
|
|
* Advance past whitespace and // or /* *\/ comments; returns the index of
|
|
* the next significant character.
|
|
*/
|
|
function skipJsoncTrivia(input, start) {
|
|
let j = start;
|
|
for (;;) {
|
|
while (j < input.length && /\s/.test(input[j])) j += 1;
|
|
if (input[j] === '/' && input[j + 1] === '/') {
|
|
while (j < input.length && !isLineBreak(input[j])) j += 1;
|
|
continue;
|
|
}
|
|
if (input[j] === '/' && input[j + 1] === '*') {
|
|
j += 2;
|
|
while (j < input.length && !(input[j] === '*' && input[j + 1] === '/')) j += 1;
|
|
if (j >= input.length) throw new Error('Unterminated block comment');
|
|
j += 2;
|
|
continue;
|
|
}
|
|
return j;
|
|
}
|
|
}
|
|
|
|
function isJsoncTrailingComma(input, index, previousSignificant) {
|
|
const next = skipJsoncTrivia(input, index + 1);
|
|
// Preserve commas in empty containers or after another separator so
|
|
// JSON.parse rejects them; it validates the remaining value syntax.
|
|
return (input[next] === '}' || input[next] === ']')
|
|
&& previousSignificant !== null && !'[{,:'.includes(previousSignificant);
|
|
}
|
|
|
|
/**
|
|
* Strip // and /* *\/ comments and the trailing commas they hide, without
|
|
* touching string contents (URLs like "https://..." must survive intact).
|
|
*/
|
|
function stripJsonc(input) {
|
|
let out = '';
|
|
let inString = false;
|
|
let escaped = false;
|
|
let previousSignificant = null;
|
|
for (let i = 0; i < input.length; i += 1) {
|
|
const ch = input[i];
|
|
if (inString) {
|
|
out += ch;
|
|
if (escaped) escaped = false;
|
|
else if (ch === '\\') escaped = true;
|
|
else if (ch === '"') inString = false;
|
|
continue;
|
|
}
|
|
if (ch === '"') {
|
|
inString = true;
|
|
out += ch;
|
|
previousSignificant = '"';
|
|
continue;
|
|
}
|
|
if (ch === '/' && input[i + 1] === '/') {
|
|
while (i < input.length && !isLineBreak(input[i])) i += 1;
|
|
out += '\n';
|
|
continue;
|
|
}
|
|
if (ch === '/' && input[i + 1] === '*') {
|
|
i += 2;
|
|
while (i < input.length && !(input[i] === '*' && input[i + 1] === '/')) i += 1;
|
|
if (i >= input.length) throw new Error('Unterminated block comment');
|
|
i += 1; // consume the closing '/'
|
|
out += ' ';
|
|
continue;
|
|
}
|
|
if (ch === ',') {
|
|
if (isJsoncTrailingComma(input, i, previousSignificant)) continue;
|
|
}
|
|
out += ch;
|
|
if (!/\s/.test(ch)) previousSignificant = ch;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Registrations that point the (legacy) OpenCode plugin config at the ECC
|
|
// hooks: the old directory form and the per-file forms some setups wrote.
|
|
// OpenCode 2.x ignores file-path entries at runtime, but they must still be
|
|
// stripped for a clean deactivation and to keep install verification honest.
|
|
function isOpenCodeHookConfigRegistration(value) {
|
|
const entry = String(value);
|
|
if (entry === './plugins') return true;
|
|
return /(^|[\\/])plugins[\\/]+ecc-hooks\.(?:ts|[cm]?js)$/.test(entry);
|
|
}
|
|
|
|
function disableOpenCodeHookPluginRegistration(content, sourceRelativePath) {
|
|
const config = parseJsoncConfig(content, sourceRelativePath);
|
|
if (config.plugin !== undefined && !Array.isArray(config.plugin)) {
|
|
throw new Error(`Invalid ${sourceRelativePath}: plugin must be an array`);
|
|
}
|
|
|
|
if (!Array.isArray(config.plugin)) {
|
|
return `${JSON.stringify(config, null, 2)}\n`;
|
|
}
|
|
|
|
return `${JSON.stringify({
|
|
...config,
|
|
plugin: config.plugin.filter(plugin => !isOpenCodeHookConfigRegistration(plugin)),
|
|
}, null, 2)}\n`;
|
|
}
|
|
|
|
function getOpenCodeActivationPathKind(value) {
|
|
const relative = normalizeOperationPath(value);
|
|
if (relative === 'opencode.json') return 'config';
|
|
if (/^plugins\/[^/]+\/package\.json$/.test(relative)) return 'package';
|
|
if (/^plugins\/(?:[^/]+\.(?:[cm]?js|ts)|[^/]+\/index\.(?:[cm]?js|ts))$/.test(relative)) return 'plugin';
|
|
return null;
|
|
}
|
|
|
|
function getOpenCodeSourceActivationKind(operation = {}) {
|
|
const source = normalizeOperationPath(operation.sourceRelativePath);
|
|
if (!source.startsWith('.opencode/')) return null;
|
|
return getOpenCodeActivationPathKind(source.replace(/^\.opencode\/(?:dist\/)?/, ''));
|
|
}
|
|
|
|
function isOpenCodePluginEntrypoint(operation = {}) {
|
|
return getOpenCodeSourceActivationKind(operation) === 'plugin';
|
|
}
|
|
|
|
const DISABLED_OPENCODE_PLUGIN_STUB = 'export default { id: "ecc-hooks", setup() {} };\n';
|
|
// OpenCode 2.x scans every top-level plugins/*.ts independently, so an inert
|
|
// barrel that kept the `ecc-hooks` id would collide with the real plugin file
|
|
// (live or stubbed) sitting beside it and register a red
|
|
// "Duplicate plugin ID" entry. The top-level barrel gets its own distinct id.
|
|
const DISABLED_OPENCODE_BARREL_STUB = 'export default { id: "ecc-hooks-entry", setup() {} };\n';
|
|
|
|
function isTopLevelPluginsBarrel(sourceRelativePath) {
|
|
if (typeof sourceRelativePath !== 'string') return false;
|
|
// Same prefix stripping as getOpenCodeSourceActivationKind: plans carry
|
|
// '.opencode/…', built payloads 'dist/…'; compare against the bare
|
|
// plugins-relative path.
|
|
const relative = normalizeOperationPath(sourceRelativePath)
|
|
.replace(/^\.opencode\//, '')
|
|
.replace(/^dist\//, '');
|
|
return relative === 'plugins/index.ts' || relative === 'plugins/index.js'
|
|
|| relative === 'plugins/index.mjs' || relative === 'plugins/index.cjs';
|
|
}
|
|
|
|
function getDisabledOpenCodePluginContent(sourceRelativePath) {
|
|
// OpenCode discovers plugins independently of opencode.json registration.
|
|
// Do not import the original module: even module initialization has effects.
|
|
// The stub must still satisfy the V2 loader schema: the default export has to
|
|
// be a definition object with an id and an effect or setup function. A factory
|
|
// (`export default async () => ({})`) is rejected at startup with
|
|
// PluginModule.LoadError / SchemaError(Expected object at ["default"]), which
|
|
// is the same failure reported for the V1 hook plugin in issue #3537.
|
|
return isTopLevelPluginsBarrel(sourceRelativePath)
|
|
? DISABLED_OPENCODE_BARREL_STUB
|
|
: DISABLED_OPENCODE_PLUGIN_STUB;
|
|
}
|
|
|
|
// Installs made before the V2 stub fix wrote this stub. It is inert either way;
|
|
// the V2 loader merely rejects it.
|
|
const LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT = 'export default async () => ({});\n';
|
|
|
|
function isInertOpenCodePluginStub(content) {
|
|
const text = content.toString('utf8');
|
|
return text === DISABLED_OPENCODE_PLUGIN_STUB
|
|
|| text === DISABLED_OPENCODE_BARREL_STUB
|
|
|| text === LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT;
|
|
}
|
|
|
|
// Exact historical disabled-entrypoint payloads that earlier ECC releases
|
|
// wrote to EVERY plugin file, barrel included. Retained copies of these are
|
|
// migrated in place to the current expected stub before the strict
|
|
// deactivation gate verifies the install; a rewrite is only allowed for
|
|
// files whose managed-recorded digest still matches these bytes.
|
|
function isLegacyDisabledOpenCodePluginStub(content) {
|
|
const text = content.toString('utf8');
|
|
return text === DISABLED_OPENCODE_PLUGIN_STUB
|
|
|| text === LEGACY_DISABLED_OPENCODE_PLUGIN_CONTENT;
|
|
}
|
|
|
|
function isOpenCodeHookActivationOperation(operation = {}) {
|
|
return getOpenCodeSourceActivationKind(operation) !== null;
|
|
}
|
|
|
|
function isHookRuntimeOperation(operation = {}) {
|
|
if (
|
|
operation.kind === 'update-claude-settings'
|
|
|| operation.moduleId === HOOK_RUNTIME_MODULE_ID
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
if (isOpenCodeHookActivationOperation(operation)) {
|
|
return !(
|
|
operation.kind === 'copy-file'
|
|
&& operation.contentTransform === (isOpenCodePluginEntrypoint(operation)
|
|
? OPENCODE_DISABLE_PLUGIN_TRANSFORM
|
|
: OPENCODE_DISABLE_ECC_HOOKS_TRANSFORM)
|
|
);
|
|
}
|
|
|
|
const source = normalizeOperationPath(operation.sourceRelativePath);
|
|
const destination = normalizeOperationPath(operation.destinationPath);
|
|
return (
|
|
source === 'hooks'
|
|
|| source.startsWith('hooks/')
|
|
|| source === '.cursor/hooks'
|
|
|| source.startsWith('.cursor/hooks/')
|
|
|| source === '.cursor/hooks.json'
|
|
|| destination.endsWith('/hooks/hooks.json')
|
|
|| destination.endsWith('/.cursor/hooks.json')
|
|
|| destination.includes('/.cursor/hooks/')
|
|
);
|
|
}
|
|
|
|
function planMaterializesHookRuntime(plan = {}) {
|
|
const operations = Array.isArray(plan.operations) ? plan.operations : [];
|
|
return operations.some(isHookRuntimeOperation);
|
|
}
|
|
|
|
function formatHookCapabilityDisclosure(indent = ' ') {
|
|
return HOOK_CAPABILITY_GROUPS
|
|
.map((group, index) => `${indent}${index + 1}. ${group.description}`)
|
|
.join('\n');
|
|
}
|
|
|
|
function resolveHookConsentFlags({ enableHooks = false, noHooks = false } = {}) {
|
|
if (enableHooks && noHooks) {
|
|
throw new Error('--enable-hooks and --no-hooks are mutually exclusive');
|
|
}
|
|
if (enableHooks) {
|
|
return 'enabled';
|
|
}
|
|
if (noHooks) {
|
|
return 'declined';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function withoutHookRuntimeId(values) {
|
|
return (Array.isArray(values) ? values : []).filter(value => value !== HOOK_RUNTIME_MODULE_ID);
|
|
}
|
|
|
|
function withoutOpenCodeHookActivation(operation) {
|
|
if (getOpenCodeSourceActivationKind(operation) === 'package') {
|
|
throw new Error(`Unsupported OpenCode package metadata deactivation: ${operation.sourceRelativePath}`);
|
|
}
|
|
if (
|
|
!isOpenCodeHookActivationOperation(operation)
|
|
|| operation.kind !== 'copy-file'
|
|
) {
|
|
return operation;
|
|
}
|
|
return {
|
|
...operation,
|
|
contentTransform: isOpenCodePluginEntrypoint(operation)
|
|
? OPENCODE_DISABLE_PLUGIN_TRANSFORM
|
|
: OPENCODE_DISABLE_ECC_HOOKS_TRANSFORM,
|
|
};
|
|
}
|
|
|
|
function planSelectsHookRuntime(plan = {}) {
|
|
return (
|
|
Array.isArray(plan.selectedModuleIds)
|
|
&& plan.selectedModuleIds.includes(HOOK_RUNTIME_MODULE_ID)
|
|
) || (
|
|
Array.isArray(plan.operations)
|
|
&& plan.operations.some(operation => operation.moduleId === HOOK_RUNTIME_MODULE_ID)
|
|
);
|
|
}
|
|
|
|
function disableUnselectedOpenCodeHooks(plan) {
|
|
if (plan.target !== 'opencode') {
|
|
return plan;
|
|
}
|
|
const runtimeSelected = planSelectsHookRuntime(plan);
|
|
const transformOperations = operations => (Array.isArray(operations) ? operations : []).map(operation => {
|
|
// The source barrel is needed by the published package, but OpenCode scans
|
|
// plugins/index.ts and plugins/ecc-hooks.ts independently in home installs.
|
|
if (!runtimeSelected || normalizeOperationPath(operation.sourceRelativePath) === '.opencode/plugins/index.ts') {
|
|
return withoutOpenCodeHookActivation(operation);
|
|
}
|
|
return operation;
|
|
});
|
|
|
|
return {
|
|
...plan,
|
|
operations: transformOperations(plan.operations),
|
|
statePreview: plan.statePreview
|
|
? {
|
|
...plan.statePreview,
|
|
operations: transformOperations(plan.statePreview.operations),
|
|
}
|
|
: plan.statePreview,
|
|
};
|
|
}
|
|
|
|
function shouldDisableOpenCodeHooks(plan = {}) {
|
|
return plan.target === 'opencode'
|
|
&& (plan.hookConsent === 'declined' || !planSelectsHookRuntime(plan));
|
|
}
|
|
|
|
function setStatePreviewHookConsent(statePreview, hookConsent) {
|
|
if (!statePreview || !statePreview.request) {
|
|
return statePreview;
|
|
}
|
|
|
|
return {
|
|
...statePreview,
|
|
request: {
|
|
...statePreview.request,
|
|
hookConsent,
|
|
},
|
|
};
|
|
}
|
|
|
|
function getRecordedHookConsent(state = {}) {
|
|
const explicitDecision = state.request && HOOK_CONSENT_DECISIONS.includes(state.request.hookConsent)
|
|
? state.request.hookConsent
|
|
: null;
|
|
if (explicitDecision) {
|
|
return explicitDecision;
|
|
}
|
|
|
|
if (Array.isArray(state.request && state.request.modules) && state.request.modules.includes(HOOK_RUNTIME_MODULE_ID)) {
|
|
return 'enabled';
|
|
}
|
|
|
|
if (Array.isArray(state.resolution && state.resolution.selectedModules) && state.resolution.selectedModules.includes(HOOK_RUNTIME_MODULE_ID)) {
|
|
return 'enabled';
|
|
}
|
|
|
|
// Older OpenCode installs copied activation files without asking for consent.
|
|
// Their presence cannot establish that the user opted in to automatic hooks.
|
|
if ((Array.isArray(state.operations) ? state.operations : []).some(operation => (
|
|
!isOpenCodeHookActivationOperation(operation) && isHookRuntimeOperation(operation)
|
|
))) {
|
|
return 'enabled';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function stripHookRuntimeFromPlan(plan) {
|
|
const hadHookRuntimeModule = Array.isArray(plan.selectedModuleIds)
|
|
&& plan.selectedModuleIds.includes('hooks-runtime');
|
|
const operations = (Array.isArray(plan.operations) ? plan.operations : [])
|
|
.map(operation => (
|
|
plan.target === 'opencode' ? withoutOpenCodeHookActivation(operation) : operation
|
|
))
|
|
.filter(operation => !isHookRuntimeOperation(operation));
|
|
const statePreview = plan.statePreview
|
|
? {
|
|
...plan.statePreview,
|
|
operations: (Array.isArray(plan.statePreview.operations) ? plan.statePreview.operations : [])
|
|
.map(operation => (
|
|
plan.target === 'opencode' ? withoutOpenCodeHookActivation(operation) : operation
|
|
))
|
|
.filter(operation => !isHookRuntimeOperation(operation)),
|
|
resolution: plan.statePreview.resolution
|
|
? {
|
|
...plan.statePreview.resolution,
|
|
selectedModules: withoutHookRuntimeId(plan.statePreview.resolution.selectedModules),
|
|
}
|
|
: plan.statePreview.resolution,
|
|
}
|
|
: plan.statePreview;
|
|
|
|
return {
|
|
...plan,
|
|
operations,
|
|
statePreview: setStatePreviewHookConsent(statePreview, 'declined'),
|
|
selectedModuleIds: withoutHookRuntimeId(plan.selectedModuleIds),
|
|
excludedModuleIds: hadHookRuntimeModule && Array.isArray(plan.excludedModuleIds)
|
|
? [...new Set([...plan.excludedModuleIds, HOOK_RUNTIME_MODULE_ID])]
|
|
: plan.excludedModuleIds,
|
|
};
|
|
}
|
|
|
|
function withHookConsent(plan, hookConsent = null) {
|
|
if (hookConsent !== null && !HOOK_CONSENT_DECISIONS.includes(hookConsent)) {
|
|
throw new Error(`Unknown hook consent decision: ${hookConsent}`);
|
|
}
|
|
if (hookConsent === 'declined') {
|
|
return { ...stripHookRuntimeFromPlan(plan), hookConsent };
|
|
}
|
|
const effectivePlan = disableUnselectedOpenCodeHooks(plan);
|
|
return {
|
|
...effectivePlan,
|
|
hookConsent,
|
|
statePreview: setStatePreviewHookConsent(effectivePlan.statePreview, hookConsent),
|
|
};
|
|
}
|
|
|
|
function assertHookConsentReady(plan = {}) {
|
|
if (!planMaterializesHookRuntime(plan)) {
|
|
return;
|
|
}
|
|
if (plan.hookConsent === 'enabled') {
|
|
return;
|
|
}
|
|
throw new Error(
|
|
'This install would enable ECC\'s automatic hook runtime, which can:\n'
|
|
+ `${formatHookCapabilityDisclosure()}\n`
|
|
+ 'Confirm with --enable-hooks to install it, or --no-hooks to install '
|
|
+ 'everything else without the hook runtime. The guided installer '
|
|
+ '(ecc install --guided) collects this choice interactively.'
|
|
);
|
|
}
|
|
|
|
module.exports = {
|
|
HOOK_CAPABILITY_GROUPS,
|
|
assertHookConsentReady,
|
|
disableUnselectedOpenCodeHooks,
|
|
disableOpenCodeHookPluginRegistration,
|
|
isOpenCodeHookConfigRegistration,
|
|
parseJsoncConfig,
|
|
getDisabledOpenCodePluginContent,
|
|
isInertOpenCodePluginStub,
|
|
isLegacyDisabledOpenCodePluginStub,
|
|
getOpenCodeActivationPathKind,
|
|
getOpenCodeSourceActivationKind,
|
|
formatHookCapabilityDisclosure,
|
|
getRecordedHookConsent,
|
|
isHookRuntimeOperation,
|
|
isOpenCodeHookActivationOperation,
|
|
isOpenCodePluginEntrypoint,
|
|
planMaterializesHookRuntime,
|
|
resolveHookConsentFlags,
|
|
shouldDisableOpenCodeHooks,
|
|
withHookConsent,
|
|
};
|