fix(kirocli): harden v2 and v3 hooks
This commit is contained in:
1 parent
5889b09a09
commit
3f43f4aad5
14 files changed
+1466
-597
No files matched your search
+379
-245
@@ -1,314 +1,448 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import {
|
||||
lstat,
|
||||
mkdir,
|
||||
readFile,
|
||||
readdir,
|
||||
rm,
|
||||
stat,
|
||||
symlink,
|
||||
writeFile,
|
||||
} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import {
|
||||
VALID_PRIVATE_KEY,
|
||||
cliPath,
|
||||
createFixture,
|
||||
environment,
|
||||
findV3Hook,
|
||||
managedV2Document,
|
||||
readJson,
|
||||
registryModuleUrl,
|
||||
runNode,
|
||||
runPlugin,
|
||||
runShell,
|
||||
startApiServer,
|
||||
} from '../test-support/kirocli-test-helpers.mjs';
|
||||
|
||||
const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/kirocli.js')).href;
|
||||
const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href;
|
||||
|
||||
function runNode(args, env, input = '') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, args, {
|
||||
env: { ...process.env, ...env },
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
});
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
||||
child.once('error', reject);
|
||||
child.once('close', (code) => resolve({ code, stdout, stderr }));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
async function assertMissing(filePath) {
|
||||
await assert.rejects(lstat(filePath), { code: 'ENOENT' });
|
||||
}
|
||||
|
||||
function runCommand(command, input = '') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(command, { shell: true, stdio: ['pipe', 'pipe', 'pipe'] });
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
||||
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
||||
child.once('error', reject);
|
||||
child.once('close', (code) => resolve({ code, stdout, stderr }));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
}
|
||||
|
||||
async function runPlugin(homeDir, method, argument) {
|
||||
const script = `
|
||||
import { kirocliPlugin } from ${JSON.stringify(pluginModuleUrl)};
|
||||
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
|
||||
const result = await kirocliPlugin[process.env.ELYDORA_TEST_METHOD](argument);
|
||||
if (result !== undefined) console.log(JSON.stringify(result));
|
||||
`;
|
||||
return runNode(
|
||||
['--input-type=module', '--eval', script],
|
||||
{
|
||||
HOME: homeDir,
|
||||
USERPROFILE: homeDir,
|
||||
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
|
||||
ELYDORA_TEST_METHOD: method,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function writeJsonOrText(filePath, value) {
|
||||
async function writeJson(filePath, value) {
|
||||
await mkdir(path.dirname(filePath), { recursive: true });
|
||||
await writeFile(filePath, typeof value === 'string' ? value : JSON.stringify(value, null, 2));
|
||||
await writeFile(filePath, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
||||
}
|
||||
|
||||
async function createFixture({ existingV2, existingV3, guardSource, hookSource } = {}) {
|
||||
const homeDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-kirocli-'));
|
||||
const agentDir = path.join(homeDir, '.elydora', 'agent-1');
|
||||
const v2Path = path.join(homeDir, '.kiro', 'agents', 'elydora-audit.json');
|
||||
const v3Path = path.join(homeDir, '.kiro', 'hooks', 'elydora-audit.json');
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const guardScriptPath = path.join(agentDir, 'guard.js');
|
||||
const hookScriptPath = path.join(agentDir, 'hook.js');
|
||||
const runtimeConfigPath = path.join(agentDir, 'config.json');
|
||||
await writeFile(
|
||||
guardScriptPath,
|
||||
guardSource ?? "process.stderr.write('Agent is frozen by Elydora.'); process.exit(2);\n",
|
||||
);
|
||||
await writeFile(hookScriptPath, hookSource ?? 'process.exit(0);\n');
|
||||
await writeFile(runtimeConfigPath, JSON.stringify({
|
||||
agent_id: 'agent-1',
|
||||
agent_name: 'kirocli',
|
||||
}));
|
||||
|
||||
if (existingV2 !== undefined) await writeJsonOrText(v2Path, existingV2);
|
||||
if (existingV3 !== undefined) await writeJsonOrText(v3Path, existingV3);
|
||||
|
||||
const config = {
|
||||
agentName: 'kirocli',
|
||||
agentId: 'agent-1',
|
||||
baseUrl: 'https://api.elydora.com',
|
||||
guardScriptPath,
|
||||
hookScriptPath,
|
||||
};
|
||||
const installResult = await runPlugin(homeDir, 'install', config);
|
||||
|
||||
function userV3Hook() {
|
||||
return {
|
||||
config,
|
||||
guardScriptPath,
|
||||
homeDir,
|
||||
hookScriptPath,
|
||||
installResult,
|
||||
runtimeConfigPath,
|
||||
v2Path,
|
||||
v3Path,
|
||||
async close() {
|
||||
await rm(homeDir, { recursive: true, force: true });
|
||||
name: 'global-context',
|
||||
trigger: 'SessionStart',
|
||||
action: { type: 'agent', prompt: 'Read AGENTS.md' },
|
||||
enabled: true,
|
||||
};
|
||||
}
|
||||
|
||||
function legacyCommand(scriptPath) {
|
||||
if (process.platform === 'win32') return `"${process.execPath}" "${scriptPath}"`;
|
||||
const quote = (value) => `'${value.replaceAll("'", `'"'"'`)}'`;
|
||||
return `${quote(process.execPath)} ${quote(scriptPath)}`;
|
||||
}
|
||||
|
||||
function legacyDocuments(fixture) {
|
||||
const guard = legacyCommand(fixture.guardScriptPath);
|
||||
const audit = legacyCommand(fixture.hookScriptPath);
|
||||
return {
|
||||
v2: managedV2Document({
|
||||
hooks: {
|
||||
preToolUse: [{ matcher: '*', command: guard, timeout_ms: 5000 }],
|
||||
postToolUse: [{ matcher: '*', command: audit, timeout_ms: 5000 }],
|
||||
},
|
||||
}),
|
||||
v3: {
|
||||
version: 'v1',
|
||||
hooks: [
|
||||
{
|
||||
name: 'elydora-guard',
|
||||
description: 'Block tool use when the Elydora agent is frozen',
|
||||
trigger: 'PreToolUse',
|
||||
matcher: '.*',
|
||||
action: { type: 'command', command: guard },
|
||||
timeout: 5,
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'elydora-audit',
|
||||
description: 'Record tool use in the Elydora audit trail',
|
||||
trigger: 'PostToolUse',
|
||||
matcher: '.*',
|
||||
action: { type: 'command', command: audit },
|
||||
timeout: 5,
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function findV3Hook(config, name) {
|
||||
return config.hooks.find((hook) => hook.name === name);
|
||||
}
|
||||
|
||||
test('Kiro CLI registry points at the v3 global hook contract', async () => {
|
||||
test('Kiro CLI installs exact v2 and 2.13 v3 global contracts atomically', async () => {
|
||||
const { SUPPORTED_AGENTS } = await import(registryModuleUrl);
|
||||
assert.deepEqual(SUPPORTED_AGENTS.get('kirocli'), {
|
||||
name: 'Kiro CLI',
|
||||
configDir: '~/.kiro/hooks',
|
||||
configFile: 'elydora-audit.json',
|
||||
});
|
||||
});
|
||||
|
||||
test('Kiro CLI install preserves user hooks and writes idempotent v2 and v3 contracts', async () => {
|
||||
const fixture = await createFixture({
|
||||
existingV2: {
|
||||
description: 'User Kiro agent',
|
||||
tools: ['read'],
|
||||
existingV2: managedV2Document({
|
||||
owner: 'user',
|
||||
hooks: {
|
||||
agentSpawn: [{ command: 'existing-spawn' }],
|
||||
preToolUse: [{ matcher: 'read', command: 'existing-v2' }],
|
||||
},
|
||||
},
|
||||
existingV3: {
|
||||
version: 'v1',
|
||||
hooks: [{
|
||||
name: 'existing-v3',
|
||||
trigger: 'SessionStart',
|
||||
action: { type: 'command', command: 'existing-command' },
|
||||
}],
|
||||
},
|
||||
}),
|
||||
existingV3: { version: 'v1', owner: 'user', hooks: [userV3Hook()] },
|
||||
});
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
assert.match(fixture.installResult.stdout, /--agent elydora-audit/);
|
||||
assert.match(fixture.installResult.stdout, /--v3/);
|
||||
const secondInstall = await runPlugin(fixture.homeDir, 'install', fixture.config);
|
||||
assert.equal(secondInstall.code, 0, secondInstall.stderr);
|
||||
|
||||
const v2 = JSON.parse(await readFile(fixture.v2Path, 'utf-8'));
|
||||
assert.equal(v2.description, 'User Kiro agent');
|
||||
assert.deepEqual(v2.tools, ['read']);
|
||||
assert.equal(v2.hooks.agentSpawn[0].command, 'existing-spawn');
|
||||
const first = await fixture.install();
|
||||
assert.equal(first.code, 0, first.stderr);
|
||||
assert.match(first.stdout, /2\.13\.0\+/);
|
||||
assert.match(first.stdout, /agent validate/);
|
||||
assert.match(first.stdout, /--agent elydora-audit/);
|
||||
const firstV2Source = await readFile(fixture.v2Path, 'utf-8');
|
||||
const firstV3Source = await readFile(fixture.v3Path, 'utf-8');
|
||||
const v2 = JSON.parse(firstV2Source);
|
||||
assert.equal(v2.owner, 'user');
|
||||
assert.deepEqual(v2.hooks.agentSpawn, [{ command: 'existing-spawn' }]);
|
||||
assert.equal(v2.hooks.preToolUse.length, 2);
|
||||
assert.equal(v2.hooks.postToolUse.length, 1);
|
||||
assert.equal(v2.hooks.preToolUse[1].matcher, '*');
|
||||
assert.equal(typeof v2.hooks.preToolUse[1].command, 'string');
|
||||
|
||||
const v3 = JSON.parse(await readFile(fixture.v3Path, 'utf-8'));
|
||||
assert.equal(v3.version, 'v1');
|
||||
assert.equal(v3.hooks.length, 3);
|
||||
assert.equal(v3.hooks[0].name, 'existing-v3');
|
||||
assert.deepEqual(v2.hooks.preToolUse[1], {
|
||||
matcher: '*',
|
||||
command: v2.hooks.preToolUse[1].command,
|
||||
timeout_ms: 10_000,
|
||||
});
|
||||
assert.deepEqual(v2.hooks.postToolUse[0], {
|
||||
matcher: '*',
|
||||
command: v2.hooks.postToolUse[0].command,
|
||||
timeout_ms: 10_000,
|
||||
});
|
||||
const v3 = JSON.parse(firstV3Source);
|
||||
assert.equal(v3.owner, 'user');
|
||||
assert.deepEqual(v3.hooks[0], userV3Hook());
|
||||
assert.deepEqual(findV3Hook(v3, 'elydora-guard'), {
|
||||
name: 'elydora-guard',
|
||||
description: 'Block tool use when the Elydora agent is frozen',
|
||||
trigger: 'PreToolUse',
|
||||
matcher: '.*',
|
||||
action: { type: 'command', command: findV3Hook(v3, 'elydora-guard').action.command },
|
||||
timeout: 5,
|
||||
timeout: 10,
|
||||
enabled: true,
|
||||
});
|
||||
assert.equal(findV3Hook(v3, 'elydora-audit').trigger, 'PostToolUse');
|
||||
assert.deepEqual(await readJson(path.join(fixture.agentDir, 'config.json')), {
|
||||
org_id: 'org-1',
|
||||
agent_id: fixture.agentId,
|
||||
kid: 'kid-1',
|
||||
base_url: fixture.baseUrl,
|
||||
token: 'token-1',
|
||||
agent_name: 'kirocli',
|
||||
});
|
||||
assert.equal(await readFile(path.join(fixture.agentDir, 'private.key'), 'utf-8'), VALID_PRIVATE_KEY);
|
||||
const second = await fixture.install();
|
||||
assert.equal(second.code, 0, second.stderr);
|
||||
assert.equal(await readFile(fixture.v2Path, 'utf-8'), firstV2Source);
|
||||
assert.equal(await readFile(fixture.v3Path, 'utf-8'), firstV3Source);
|
||||
const names = [
|
||||
...await readdir(path.dirname(fixture.v2Path)),
|
||||
...await readdir(path.dirname(fixture.v3Path)),
|
||||
...await readdir(fixture.agentDir),
|
||||
];
|
||||
assert.equal(names.some((name) => /\.(tmp|rollback)$/.test(name)), false);
|
||||
if (process.platform !== 'win32') {
|
||||
assert.equal((await stat(fixture.v2Path)).mode & 0o777, 0o600);
|
||||
assert.equal((await stat(fixture.guardScriptPath)).mode & 0o777, 0o700);
|
||||
}
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI hook commands enforce freezes and forward the official event payload', async () => {
|
||||
const capturePath = path.join(os.tmpdir(), `elydora-kiro-event-${process.pid}-${Date.now()}.json`);
|
||||
const fixture = await createFixture({
|
||||
hookSource: `
|
||||
const fs = require('node:fs');
|
||||
const chunks = [];
|
||||
process.stdin.on('data', (chunk) => chunks.push(chunk));
|
||||
process.stdin.on('end', () => fs.writeFileSync(${JSON.stringify(capturePath)}, Buffer.concat(chunks)));
|
||||
`,
|
||||
});
|
||||
test('Kiro CLI commands block frozen agents and preserve native events', async () => {
|
||||
const api = await startApiServer();
|
||||
const fixture = await createFixture({ baseUrl: api.baseUrl });
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
const v3 = JSON.parse(await readFile(fixture.v3Path, 'utf-8'));
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const v2 = await readJson(fixture.v2Path);
|
||||
const v3 = await readJson(fixture.v3Path);
|
||||
assert.equal(v2.hooks.preToolUse[0].command, findV3Hook(v3, 'elydora-guard').action.command);
|
||||
const payload = {
|
||||
hook_event_name: 'PreToolUse',
|
||||
cwd: fixture.homeDir,
|
||||
hook_event_name: 'preToolUse',
|
||||
cwd: fixture.projectDir,
|
||||
session_id: 'session-1',
|
||||
tool_name: 'execute_bash',
|
||||
tool_input: { command: 'echo test' },
|
||||
tool_input: { command: 'npm test' },
|
||||
future_field: { retained: true },
|
||||
};
|
||||
const guardResult = await runCommand(findV3Hook(v3, 'elydora-guard').action.command, JSON.stringify(payload));
|
||||
assert.equal(guardResult.code, 2);
|
||||
assert.match(guardResult.stderr, /Agent is frozen by Elydora/);
|
||||
|
||||
payload.hook_event_name = 'PostToolUse';
|
||||
payload.tool_response = { success: true, result: 'test' };
|
||||
const auditResult = await runCommand(findV3Hook(v3, 'elydora-audit').action.command, JSON.stringify(payload));
|
||||
assert.equal(auditResult.code, 0, auditResult.stderr);
|
||||
assert.deepEqual(JSON.parse(await readFile(capturePath, 'utf-8')), payload);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
await rm(capturePath, { force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI status accepts either contract and requires both runtime scripts', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
const status = await runPlugin(fixture.homeDir, 'status', null);
|
||||
assert.equal(status.code, 0, status.stderr);
|
||||
assert.equal(JSON.parse(status.stdout).installed, true);
|
||||
|
||||
await rm(fixture.v3Path);
|
||||
const v2Only = await runPlugin(fixture.homeDir, 'status', null);
|
||||
const v2Status = JSON.parse(v2Only.stdout);
|
||||
assert.equal(v2Status.installed, true);
|
||||
assert.equal(v2Status.configPath, fixture.v2Path);
|
||||
|
||||
const reinstall = await runPlugin(fixture.homeDir, 'install', fixture.config);
|
||||
assert.equal(reinstall.code, 0, reinstall.stderr);
|
||||
await rm(fixture.v2Path);
|
||||
const v3Only = await runPlugin(fixture.homeDir, 'status', null);
|
||||
const v3Status = JSON.parse(v3Only.stdout);
|
||||
assert.equal(v3Status.installed, true);
|
||||
assert.equal(v3Status.configPath, fixture.v3Path);
|
||||
|
||||
await rm(fixture.guardScriptPath);
|
||||
const degraded = await runPlugin(fixture.homeDir, 'status', null);
|
||||
assert.equal(JSON.parse(degraded.stdout).installed, false);
|
||||
await writeJson(path.join(fixture.agentDir, 'status-cache.json'), {
|
||||
status: 'frozen',
|
||||
cached_at: Date.now(),
|
||||
});
|
||||
const guard = await runShell(
|
||||
findV3Hook(v3, 'elydora-guard').action.command,
|
||||
environment(fixture),
|
||||
fixture.projectDir,
|
||||
JSON.stringify(payload),
|
||||
);
|
||||
assert.equal(guard.code, 2, guard.stderr);
|
||||
await rm(path.join(fixture.agentDir, 'status-cache.json'));
|
||||
payload.hook_event_name = 'postToolUse';
|
||||
payload.tool_response = { success: true, result: '12 passed' };
|
||||
const audit = await runShell(
|
||||
findV3Hook(v3, 'elydora-audit').action.command,
|
||||
environment(fixture),
|
||||
fixture.projectDir,
|
||||
JSON.stringify(payload),
|
||||
);
|
||||
assert.equal(audit.code, 0, audit.stderr);
|
||||
const operation = JSON.parse(api.requests.find(({ method }) => method === 'POST').raw);
|
||||
assert.deepEqual(operation.payload, payload);
|
||||
assert.deepEqual(operation.action, { tool: 'execute_bash' });
|
||||
assert.deepEqual(operation.subject, { session_id: 'session-1' });
|
||||
} finally {
|
||||
await api.close();
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI status surfaces malformed referenced runtime metadata', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
await writeFile(fixture.runtimeConfigPath, '{ malformed');
|
||||
const status = await runPlugin(fixture.homeDir, 'status', null);
|
||||
assert.equal(status.code, 1);
|
||||
assert.match(status.stderr, /parse Elydora runtime config/i);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI uninstall preserves unrelated v2 and v3 hooks', async () => {
|
||||
const fixture = await createFixture({
|
||||
existingV2: { hooks: { preToolUse: [{ matcher: 'read', command: 'existing-v2' }] } },
|
||||
existingV3: {
|
||||
version: 'v1',
|
||||
hooks: [{
|
||||
name: 'existing-v3',
|
||||
trigger: 'SessionStart',
|
||||
action: { type: 'command', command: 'existing-command' },
|
||||
}],
|
||||
},
|
||||
test('Kiro CLI status requires both exact contracts and physical runtime identity', async (t) => {
|
||||
await t.test('missing home is an empty installation', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
await rm(fixture.homeDir, { recursive: true });
|
||||
const status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(status.code, 0, status.stderr);
|
||||
assert.equal(JSON.parse(status.stdout).installed, false);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
const result = await runPlugin(fixture.homeDir, 'uninstall', 'agent-1');
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
const v2 = JSON.parse(await readFile(fixture.v2Path, 'utf-8'));
|
||||
assert.deepEqual(v2.hooks.preToolUse, [{ matcher: 'read', command: 'existing-v2' }]);
|
||||
assert.deepEqual(v2.hooks.postToolUse, []);
|
||||
const v3 = JSON.parse(await readFile(fixture.v3Path, 'utf-8'));
|
||||
assert.equal(v3.hooks.length, 1);
|
||||
assert.equal(v3.hooks[0].name, 'existing-v3');
|
||||
} finally {
|
||||
await fixture.close();
|
||||
await t.test('missing or disabled contract is degraded and reinstall repairs it', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
let status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(JSON.parse(status.stdout).installed, true);
|
||||
const v3 = await readJson(fixture.v3Path);
|
||||
findV3Hook(v3, 'elydora-guard').enabled = false;
|
||||
await writeJson(fixture.v3Path, v3);
|
||||
status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(JSON.parse(status.stdout).installed, false);
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
await rm(fixture.v2Path);
|
||||
status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(JSON.parse(status.stdout).hookConfigured, false);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
await t.test('recognized altered hooks degrade status', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const v2 = await readJson(fixture.v2Path);
|
||||
v2.hooks.preToolUse.push({ ...v2.hooks.preToolUse[0], timeout_ms: 5000 });
|
||||
await writeJson(fixture.v2Path, v2);
|
||||
let status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(JSON.parse(status.stdout).hookConfigured, false);
|
||||
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const v3 = await readJson(fixture.v3Path);
|
||||
v3.hooks.push({ ...findV3Hook(v3, 'elydora-guard'), enabled: false });
|
||||
await writeJson(fixture.v3Path, v3);
|
||||
status = await runPlugin(fixture, 'status', null);
|
||||
assert.equal(JSON.parse(status.stdout).hookConfigured, false);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
for (const [label, target, contents, expected] of [
|
||||
['guard runtime', 'guard', 'tampered\n', /"installed":false/],
|
||||
['runtime config', 'config', '{ malformed', /parse Elydora runtime config/i],
|
||||
['runtime identity', 'config', JSON.stringify({
|
||||
org_id: 'org-1',
|
||||
agent_id: 'agent-1',
|
||||
kid: 'kid-1',
|
||||
base_url: 'http://127.0.0.1:9',
|
||||
token: 'token-1',
|
||||
agent_name: 'kiroide',
|
||||
}), /runtime identity/i],
|
||||
['private key', 'key', 'invalid', /private key is invalid/i],
|
||||
]) {
|
||||
await t.test(label, async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const targets = {
|
||||
guard: fixture.guardScriptPath,
|
||||
config: path.join(fixture.agentDir, 'config.json'),
|
||||
key: path.join(fixture.agentDir, 'private.key'),
|
||||
};
|
||||
await writeFile(targets[target], contents);
|
||||
const status = await runPlugin(fixture, 'status', null);
|
||||
assert.match(`${status.stdout}${status.stderr}`, expected);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI uninstall removes configs owned entirely by Elydora', async () => {
|
||||
test('Kiro CLI rejects malformed, colliding, invalid, and linked inputs before writes', async (t) => {
|
||||
for (const [label, options, pattern] of [
|
||||
['duplicate v2', { existingV2: '{"name":"elydora-audit","name":"other"}' }, /duplicate/i],
|
||||
['v2 path collision', { existingV2: { name: 'user-agent', hooks: {} } }, /v2 agent path conflicts/i],
|
||||
['v3 name collision', { existingV3: { version: 'v1', hooks: [{ name: 'elydora-guard', trigger: 'PreToolUse', action: { type: 'command', command: 'user-command' } }] } }, /hook name.*conflicts/i],
|
||||
]) {
|
||||
await t.test(label, async () => {
|
||||
const fixture = await createFixture(options);
|
||||
try {
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, pattern);
|
||||
await assertMissing(fixture.guardScriptPath);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
await t.test('non-canonical private key', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
const result = await fixture.install({ privateKey: 'invalid' });
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /canonical 32-byte/);
|
||||
await assertMissing(fixture.v2Path);
|
||||
await assertMissing(fixture.v3Path);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
await t.test('linked Kiro directory', async () => {
|
||||
const fixture = await createFixture();
|
||||
const external = path.join(fixture.rootDir, 'external');
|
||||
try {
|
||||
await mkdir(external);
|
||||
await symlink(external, path.join(fixture.homeDir, '.kiro'), 'junction');
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 1);
|
||||
assert.match(result.stderr, /not a physical directory|symbolic link/i);
|
||||
await assertMissing(path.join(external, 'agents', 'elydora-audit.json'));
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('Kiro CLI migrates exact legacy commands and repairs both schemas', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 0, fixture.installResult.stderr);
|
||||
const result = await runPlugin(fixture.homeDir, 'uninstall', 'agent-1');
|
||||
const legacy = legacyDocuments(fixture);
|
||||
await writeJson(fixture.v2Path, legacy.v2);
|
||||
await writeJson(fixture.v3Path, legacy.v3);
|
||||
const result = await fixture.install();
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
await assert.rejects(readFile(fixture.v2Path), { code: 'ENOENT' });
|
||||
await assert.rejects(readFile(fixture.v3Path), { code: 'ENOENT' });
|
||||
const v2 = await readJson(fixture.v2Path);
|
||||
const v3 = await readJson(fixture.v3Path);
|
||||
assert.equal(v2.hooks.preToolUse.length, 1);
|
||||
assert.equal(v2.hooks.preToolUse[0].timeout_ms, 10_000);
|
||||
assert.equal(findV3Hook(v3, 'elydora-guard').timeout, 10);
|
||||
assert.equal(findV3Hook(v3, 'elydora-guard').action.command, v2.hooks.preToolUse[0].command);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('Kiro CLI install preserves malformed configs for recovery', async () => {
|
||||
const fixture = await createFixture({ existingV3: '{ malformed' });
|
||||
test('Kiro CLI uninstall preserves user hooks and removes fully owned files', async (t) => {
|
||||
await t.test('mixed files', async () => {
|
||||
const fixture = await createFixture({
|
||||
existingV2: managedV2Document({ hooks: { agentSpawn: [{ command: 'existing-spawn' }] } }),
|
||||
existingV3: { version: 'v1', hooks: [userV3Hook()] },
|
||||
});
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const result = await runPlugin(fixture, 'uninstall', fixture.agentId);
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
const v2 = await readJson(fixture.v2Path);
|
||||
assert.deepEqual(v2.hooks, { agentSpawn: [{ command: 'existing-spawn' }] });
|
||||
const v3 = await readJson(fixture.v3Path);
|
||||
assert.deepEqual(v3.hooks, [userV3Hook()]);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
await t.test('fully owned files', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
assert.equal((await fixture.install()).code, 0);
|
||||
const result = await runPlugin(fixture, 'uninstall', fixture.agentId);
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
await assertMissing(fixture.v2Path);
|
||||
await assertMissing(fixture.v3Path);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
await t.test('legacy files', async () => {
|
||||
const fixture = await createFixture();
|
||||
try {
|
||||
const legacy = legacyDocuments(fixture);
|
||||
await writeJson(fixture.v2Path, legacy.v2);
|
||||
await writeJson(fixture.v3Path, legacy.v3);
|
||||
const result = await runPlugin(fixture, 'uninstall', fixture.agentId);
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
await assertMissing(fixture.v2Path);
|
||||
await assertMissing(fixture.v3Path);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
await t.test('unrelated v3 source bytes', async () => {
|
||||
const source = '{ "version": "v1", "hooks": [{ "name": "user", "trigger": "SessionStart", "action": { "type": "command", "command": "echo user" } }] }\n';
|
||||
const fixture = await createFixture({ existingV3: source });
|
||||
try {
|
||||
const result = await runPlugin(fixture, 'uninstall', fixture.agentId);
|
||||
assert.equal(result.code, 0, result.stderr);
|
||||
assert.equal(await readFile(fixture.v3Path, 'utf-8'), source);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('Kiro CLI command completes install, status, and uninstall end to end', async () => {
|
||||
const fixture = await createFixture();
|
||||
const privateKeyFile = path.join(fixture.projectDir, 'private-key.txt');
|
||||
const tokenFile = path.join(fixture.projectDir, 'token.txt');
|
||||
try {
|
||||
assert.equal(fixture.installResult.code, 1);
|
||||
assert.match(fixture.installResult.stderr, /parse Kiro CLI v3 hooks config/i);
|
||||
assert.equal(await readFile(fixture.v3Path, 'utf-8'), '{ malformed');
|
||||
await writeFile(privateKeyFile, `${VALID_PRIVATE_KEY}\n`, { mode: 0o600 });
|
||||
await writeFile(tokenFile, 'token-1\n', { mode: 0o600 });
|
||||
const install = await runNode([
|
||||
cliPath, 'install', '--agent', 'kirocli', '--org_id', 'org-1',
|
||||
'--agent_id', fixture.agentId, '--kid', 'kid-1',
|
||||
'--private_key_file', privateKeyFile, '--token_file', tokenFile,
|
||||
'--base_url', fixture.baseUrl,
|
||||
], environment(fixture), fixture.projectDir);
|
||||
assert.equal(install.code, 0, install.stderr);
|
||||
const status = await runNode([cliPath, 'status'], environment(fixture), fixture.projectDir);
|
||||
assert.equal(status.code, 0, status.stderr);
|
||||
assert.match(status.stdout, /Kiro CLI \(kirocli\) \[installed\]/);
|
||||
const uninstall = await runNode([
|
||||
cliPath, 'uninstall', '--agent', 'kirocli', '--agent_id', fixture.agentId,
|
||||
], environment(fixture), fixture.projectDir);
|
||||
assert.equal(uninstall.code, 0, uninstall.stderr);
|
||||
await assertMissing(fixture.v2Path);
|
||||
await assertMissing(fixture.v3Path);
|
||||
await assertMissing(fixture.agentDir);
|
||||
} finally {
|
||||
await fixture.close();
|
||||
}
|
||||
|
||||
const v2Fixture = await createFixture({ existingV2: '{ malformed' });
|
||||
try {
|
||||
assert.equal(v2Fixture.installResult.code, 1);
|
||||
assert.match(v2Fixture.installResult.stderr, /parse Kiro CLI v2 agent config/i);
|
||||
assert.equal(await readFile(v2Fixture.v2Path, 'utf-8'), '{ malformed');
|
||||
} finally {
|
||||
await v2Fixture.close();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user