lich-cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -2
- package/dist/bin.js +44415 -0
- package/dist/core.cjs +8165 -0
- package/dist/guard.cjs +104 -0
- package/dist/hooks.js +7858 -0
- package/dist/jest-setup.cjs +5931 -0
- package/dist/register.cjs +25 -0
- package/dist/templates/agents/investigator.json +17 -0
- package/dist/templates/agents/surgeon.json +16 -0
- package/dist/templates/hooks/audit.mjs +88 -0
- package/dist/templates/hooks/guard.mjs +141 -0
- package/dist/templates/hooks/lich.json +18 -0
- package/dist/templates/workflows/revive.workflow.json +34 -0
- package/dist/worker.cjs +1345 -0
- package/package.json +42 -4
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The preload that `record` loads with `--require` through NODE_OPTIONS. It must be CommonJS,
|
|
3
|
+
// because `--require` cannot load an ES module. It runs once in each Test_Process, before the
|
|
4
|
+
// app's code.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
try {
|
|
7
|
+
const target = process.env["LICH_RECORDER_TARGET"];
|
|
8
|
+
const targetPath = process.env["LICH_RECORDER_TARGET_PATH"];
|
|
9
|
+
// The Soul_Dir is passed in, not worked out here, because a worker's working folder can differ
|
|
10
|
+
// from the project folder.
|
|
11
|
+
const soulDir = process.env["LICH_RECORDER_SOUL_DIR"];
|
|
12
|
+
// Without all three this is not a recording run (for example a child process of an unrelated
|
|
13
|
+
// tool), so do nothing.
|
|
14
|
+
if (target && targetPath && soulDir) {
|
|
15
|
+
// The hooks are built as ES modules. `require` loads them without a flag from Node 22.12.0.
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- a preload cannot use `import`
|
|
17
|
+
const hooks = require("./hooks.js");
|
|
18
|
+
hooks.install({ target, targetPath, soulDir });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// A fault here must never break the project's process, and nothing may reach standard output or
|
|
23
|
+
// standard error (12.6). With no `load` event written, the merge reports the Target as mocked.
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=register.cjs.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "investigator",
|
|
3
|
+
"description": "Reads the soul and the advisory, then opens the Lich case with attack inputs.",
|
|
4
|
+
"prompt": "Read .lich/souls/<seg>.soul.json and the advisory it names. Propose inputs that trigger the advisory, then call open_case with package and attackInputs keyed by advisory ID. When the vulnerable code only runs with an option the soul never passes, give the advisory as { inputs, entries } with one entry per such call to the package's main export, for example { \"inputs\": [\"...\"], \"entries\": [{ \"args\": [{ \"strict\": false }] }] }. Reply with a short plan for the fix. Never edit files.",
|
|
5
|
+
"model": "claude-haiku-5.5",
|
|
6
|
+
"tools": ["read", "@lich", "@power-lich-power-lich"],
|
|
7
|
+
"includeMcpJson": true,
|
|
8
|
+
"includePowers": true,
|
|
9
|
+
"permissions": {
|
|
10
|
+
"rules": [
|
|
11
|
+
{ "capability": "fs_read", "match": ["**"], "effect": "allow" },
|
|
12
|
+
{ "capability": "mcp", "match": ["lich/*", "power-lich-power-lich/*"], "effect": "allow" },
|
|
13
|
+
{ "capability": "fs_write", "match": ["**"], "effect": "deny" },
|
|
14
|
+
{ "capability": "shell", "match": ["*"], "effect": "deny" }
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "surgeon",
|
|
3
|
+
"description": "Makes the smallest fix to the revival copy and checks it with Lich.",
|
|
4
|
+
"prompt": "Make the smallest change that fixes the case in .kiro/specs/revive-<seg>/. Edit only .lich/cases/<seg>/revival/. Never edit souls, tests, the checker or case files. Call check when done; the check decides whether the fix passes.",
|
|
5
|
+
"model": "claude-sonnet-5.5",
|
|
6
|
+
"tools": ["read", "write", "@lich/check", "@power-lich-power-lich/check"],
|
|
7
|
+
"includeMcpJson": true,
|
|
8
|
+
"includePowers": true,
|
|
9
|
+
"permissions": {
|
|
10
|
+
"rules": [
|
|
11
|
+
{ "capability": "fs_write", "match": [".lich/cases/*/revival/**"], "effect": "allow" },
|
|
12
|
+
{ "capability": "fs_write", "match": ["**"], "exclude": [".lich/cases/*/revival/**"], "effect": "deny" },
|
|
13
|
+
{ "capability": "shell", "match": ["*"], "effect": "deny" }
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Lich audit: a PostToolUse hook for every tool.
|
|
2
|
+
// While a Lich case is open, it appends one line per tool call to .lich/cases/<seg>/audit.jsonl.
|
|
3
|
+
// It records only paths, a cut shell command or a package name, so file contents and tokens
|
|
4
|
+
// never reach the log. It always exits 0: a broken log must never block the agent.
|
|
5
|
+
// It imports only node: modules, because it is copied into user projects with no install step.
|
|
6
|
+
import { appendFileSync, existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
const PATH_KEYS = ['path', 'file_path', 'filePath', 'targetFile', 'sourcePath', 'destinationPath'];
|
|
10
|
+
const COMMAND_LIMIT = 200;
|
|
11
|
+
|
|
12
|
+
function openCases(root) {
|
|
13
|
+
try {
|
|
14
|
+
return readdirSync(path.join(root, '.lich', 'cases'), { withFileTypes: true })
|
|
15
|
+
.filter((entry) => entry.isDirectory() && existsSync(path.join(root, '.lich', 'cases', entry.name, 'open')))
|
|
16
|
+
.map((entry) => entry.name);
|
|
17
|
+
} catch {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function commandTokens(command) {
|
|
23
|
+
return command
|
|
24
|
+
.split(/[\s;|&<>()`=]+/)
|
|
25
|
+
.map((token) => token.replace(/["']/g, ''))
|
|
26
|
+
.filter((token) => /[/\\.]/.test(token));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Pairs of [key, path], so the summary can show which field named each path.
|
|
30
|
+
function pathEntries(input) {
|
|
31
|
+
const entries = [];
|
|
32
|
+
for (const key of PATH_KEYS) {
|
|
33
|
+
if (typeof input[key] === 'string' && input[key] !== '') entries.push([key, input[key]]);
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(input.paths)) {
|
|
36
|
+
for (const item of input.paths) if (typeof item === 'string' && item !== '') entries.push(['paths', item]);
|
|
37
|
+
}
|
|
38
|
+
return entries;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function summary(tool, input, entries) {
|
|
42
|
+
if (tool.startsWith('mcp_')) return typeof input.package === 'string' ? `package=${input.package}` : '';
|
|
43
|
+
if (typeof input.command === 'string') return input.command.slice(0, COMMAND_LIMIT);
|
|
44
|
+
return entries.map(([key, value]) => `${key}=${value}`).join(' ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function succeeded(response) {
|
|
48
|
+
if (response === null || typeof response !== 'object') return true;
|
|
49
|
+
if (response.success === false || response.isError === true) return false;
|
|
50
|
+
return response.error === undefined || response.error === null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function caseFor(root, targets, cases) {
|
|
54
|
+
for (const target of targets) {
|
|
55
|
+
const rel = path.relative(path.resolve(root), path.resolve(root, target.replace(/\\/g, '/')));
|
|
56
|
+
const match = /^\.lich\/cases\/([^/]+)\//.exec(rel.split(path.sep).join('/'));
|
|
57
|
+
if (match) return match[1];
|
|
58
|
+
}
|
|
59
|
+
return cases.length === 1 ? cases[0] : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
// stdin comes first because the event carries the cwd that the case lookup needs.
|
|
64
|
+
const event = JSON.parse(readFileSync(0, 'utf8'));
|
|
65
|
+
const root = typeof event?.cwd === 'string' && event.cwd !== '' ? event.cwd : process.cwd();
|
|
66
|
+
const cases = openCases(root);
|
|
67
|
+
if (cases.length > 0) {
|
|
68
|
+
const tool = typeof event.tool_name === 'string' ? event.tool_name : '';
|
|
69
|
+
const input = event.tool_input !== null && typeof event.tool_input === 'object' ? event.tool_input : {};
|
|
70
|
+
const entries = pathEntries(input);
|
|
71
|
+
const targets = entries.map(([, value]) => value);
|
|
72
|
+
if (typeof input.command === 'string') targets.push(...commandTokens(input.command));
|
|
73
|
+
const seg = caseFor(root, targets, cases);
|
|
74
|
+
if (seg !== null) {
|
|
75
|
+
const line = JSON.stringify({
|
|
76
|
+
time: new Date().toISOString(),
|
|
77
|
+
tool,
|
|
78
|
+
input: summary(tool, input, entries),
|
|
79
|
+
ok: succeeded(event.tool_response),
|
|
80
|
+
});
|
|
81
|
+
appendFileSync(path.join(root, '.lich', 'cases', seg, 'audit.jsonl'), `${line}\n`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// Bad input or an unwritable log is ignored, so the tool call is never blocked.
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
process.exit(0);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Lich guard: a PreToolUse hook for write and shell tools.
|
|
2
|
+
// While a Lich case is open, it blocks any call that touches the proof: souls, reports,
|
|
3
|
+
// the checker, tests and case files. Exit code 2 blocks the call and sends stderr to the agent.
|
|
4
|
+
// It imports only node: modules, because it is copied into user projects with no install step.
|
|
5
|
+
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
const PATH_KEYS = ['path', 'file_path', 'filePath', 'targetFile', 'sourcePath', 'destinationPath'];
|
|
9
|
+
const PROTECTED_PREFIXES = [
|
|
10
|
+
['.lich/souls', 'souls are protected'],
|
|
11
|
+
['.lich/reports', 'reports are protected'],
|
|
12
|
+
['packages/checker', 'the checker is protected'],
|
|
13
|
+
['node_modules/@lich', 'the installed Lich packages are protected'],
|
|
14
|
+
];
|
|
15
|
+
// Removing either folder would delete every case file, including the open marker.
|
|
16
|
+
const PROTECTED_FOLDERS = ['.lich', '.lich/cases'];
|
|
17
|
+
|
|
18
|
+
function readEvent() {
|
|
19
|
+
let raw;
|
|
20
|
+
try {
|
|
21
|
+
raw = readFileSync(0, 'utf8');
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const event = JSON.parse(raw);
|
|
27
|
+
return event !== null && typeof event === 'object' && !Array.isArray(event) ? event : null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function openCases(root) {
|
|
34
|
+
try {
|
|
35
|
+
return readdirSync(path.join(root, '.lich', 'cases'), { withFileTypes: true })
|
|
36
|
+
.filter((entry) => entry.isDirectory() && existsSync(path.join(root, '.lich', 'cases', entry.name, 'open')))
|
|
37
|
+
.map((entry) => entry.name);
|
|
38
|
+
} catch {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Reverses the case folder name: every character other than a-z, 0-9 and - is written as _hhhh.
|
|
44
|
+
function packageOf(seg) {
|
|
45
|
+
return seg.replace(/_([0-9a-f]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function commandTokens(command) {
|
|
49
|
+
return command
|
|
50
|
+
.split(/[\s;|&<>()`=]+/)
|
|
51
|
+
.map((token) => token.replace(/["']/g, ''))
|
|
52
|
+
.filter((token) => /[/\\.]/.test(token));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function targetsOf(input) {
|
|
56
|
+
if (input === null || typeof input !== 'object') return [];
|
|
57
|
+
const targets = [];
|
|
58
|
+
for (const key of PATH_KEYS) {
|
|
59
|
+
if (typeof input[key] === 'string' && input[key] !== '') targets.push(input[key]);
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(input.paths)) {
|
|
62
|
+
for (const item of input.paths) if (typeof item === 'string' && item !== '') targets.push(item);
|
|
63
|
+
}
|
|
64
|
+
if (typeof input.command === 'string') targets.push(...commandTokens(input.command));
|
|
65
|
+
return targets;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// realpathSync needs an existing path, so resolve the deepest ancestor that exists
|
|
69
|
+
// and add the rest. This also covers files that a write is about to create.
|
|
70
|
+
function realPath(absolute) {
|
|
71
|
+
const rest = [];
|
|
72
|
+
let current = absolute;
|
|
73
|
+
while (!existsSync(current)) {
|
|
74
|
+
const parent = path.dirname(current);
|
|
75
|
+
if (parent === current) return absolute;
|
|
76
|
+
rest.unshift(path.basename(current));
|
|
77
|
+
current = parent;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
return path.join(realpathSync(current), ...rest);
|
|
81
|
+
} catch {
|
|
82
|
+
return absolute;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function protectedRule(rel, cases) {
|
|
87
|
+
const r = process.platform === 'win32' ? rel.toLowerCase() : rel;
|
|
88
|
+
for (const [prefix, rule] of PROTECTED_PREFIXES) {
|
|
89
|
+
if (r === prefix || r.startsWith(`${prefix}/`)) return rule;
|
|
90
|
+
}
|
|
91
|
+
// The project's tests and the recorder run the installed copy, so it stays as it was recorded.
|
|
92
|
+
for (const seg of cases) {
|
|
93
|
+
const prefix = `node_modules/${packageOf(seg)}`;
|
|
94
|
+
if (r === prefix || r.startsWith(`${prefix}/`)) return 'the installed package of an open case is protected';
|
|
95
|
+
}
|
|
96
|
+
if (PROTECTED_FOLDERS.includes(r)) return 'case files are protected';
|
|
97
|
+
const segments = r.split('/');
|
|
98
|
+
if (segments.some((s) => s === 'test' || s === 'tests' || s === '__tests__')) return 'test files are protected';
|
|
99
|
+
if (/\.(test|spec)\.[^/]+$/.test(segments[segments.length - 1])) return 'test files are protected';
|
|
100
|
+
const inCase = /^\.lich\/cases\/[^/]+(?:\/(.*))?$/.exec(r);
|
|
101
|
+
if (inCase && !(inCase[1] ?? '').startsWith('revival/')) return 'case files are protected';
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The open-case lookup needs the event's cwd, so stdin is read first. When stdin is not
|
|
106
|
+
// valid JSON, process.cwd() stands in for the cwd: with no open case there, the guard
|
|
107
|
+
// stays out of the way, and with one open it fails closed.
|
|
108
|
+
const event = readEvent();
|
|
109
|
+
const root = typeof event?.cwd === 'string' && event.cwd !== '' ? event.cwd : process.cwd();
|
|
110
|
+
const cases = openCases(root);
|
|
111
|
+
if (cases.length === 0) process.exit(0);
|
|
112
|
+
|
|
113
|
+
if (event === null) {
|
|
114
|
+
console.error('guard: the hook input is not valid JSON');
|
|
115
|
+
process.exit(2);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let realRoot = path.resolve(root);
|
|
119
|
+
try {
|
|
120
|
+
realRoot = realpathSync(realRoot);
|
|
121
|
+
} catch {
|
|
122
|
+
// An unreadable root keeps its resolved form.
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const target of targetsOf(event.tool_input)) {
|
|
126
|
+
const absolute = realPath(path.resolve(root, target.replace(/\\/g, '/')));
|
|
127
|
+
const relative = path.relative(realRoot, absolute);
|
|
128
|
+
// Paths outside the project are left to the agent permissions.
|
|
129
|
+
const outside = relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative);
|
|
130
|
+
if (relative === '' || outside) continue;
|
|
131
|
+
const rel = relative.split(path.sep).join('/');
|
|
132
|
+
const rule = protectedRule(rel, cases);
|
|
133
|
+
if (rule === null) continue;
|
|
134
|
+
const closing = cases
|
|
135
|
+
.map((seg) => `To close it, delete .lich/cases/${seg}/open, or pass the check and call report.`)
|
|
136
|
+
.join(' ');
|
|
137
|
+
console.error(`guard: blocked ${rel}: ${rule}. A Lich case is open. ${closing}`);
|
|
138
|
+
process.exit(2);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
process.exit(0);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "v1",
|
|
3
|
+
"hooks": [
|
|
4
|
+
{
|
|
5
|
+
"name": "lich-guard",
|
|
6
|
+
"trigger": "PreToolUse",
|
|
7
|
+
"matcher": "write|shell",
|
|
8
|
+
"action": { "type": "command", "command": "node .kiro/hooks/guard.mjs" },
|
|
9
|
+
"timeout": 10
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"name": "lich-audit",
|
|
13
|
+
"trigger": "PostToolUse",
|
|
14
|
+
"action": { "type": "command", "command": "node .kiro/hooks/audit.mjs" },
|
|
15
|
+
"timeout": 10
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lich-revive",
|
|
3
|
+
"description": "Investigate a dead package, then fix the revival copy until the Lich check passes, at most 3 rounds.",
|
|
4
|
+
"inputs": { "package": "string", "segment": "string" },
|
|
5
|
+
"nodes": [
|
|
6
|
+
{
|
|
7
|
+
"type": "step",
|
|
8
|
+
"id": "investigate",
|
|
9
|
+
"agent": "investigator",
|
|
10
|
+
"prompt": "Investigate {{package}}. Read .lich/souls/{{segment}}.soul.json and the advisory it names. Then call open_case with package \"{{package}}\" and attackInputs keyed by advisory ID. Reply with a short plan for the fix."
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"type": "repeat",
|
|
14
|
+
"id": "fix-loop",
|
|
15
|
+
"maxIterations": 3,
|
|
16
|
+
"onMaxIterations": "pause",
|
|
17
|
+
"stopCondition": {
|
|
18
|
+
"fileCheck": {
|
|
19
|
+
"path": ".lich/cases/{{segment}}/check.json",
|
|
20
|
+
"jsonPath": "$.pass",
|
|
21
|
+
"value": true
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"nodes": [
|
|
25
|
+
{
|
|
26
|
+
"type": "step",
|
|
27
|
+
"id": "fix",
|
|
28
|
+
"agent": "surgeon",
|
|
29
|
+
"prompt": "Fix {{package}} as described in .kiro/specs/revive-{{segment}}/. Edit only .lich/cases/{{segment}}/revival/. Then call check with package \"{{package}}\". The investigator's plan:\n\n{{investigate.output}}"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|