@krisada-m/ksd-intelligence 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +167 -2
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1002 -0
- package/dist/adapters/cli/policy-bin.js +10 -0
- package/dist/adapters/cli/review-verdict.js +3 -0
- package/dist/adapters/omp/extension.js +893 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +318 -0
- package/dist/adapters/ui/tree.js +460 -0
- package/dist/core/audit/index.js +2 -0
- package/dist/core/audit/log.js +505 -0
- package/dist/core/audit/summary.js +188 -0
- package/dist/core/changes/changes.js +591 -0
- package/dist/core/changes/git.js +266 -0
- package/dist/core/changes/plan.js +109 -0
- package/dist/core/changes/render.js +356 -0
- package/dist/core/engine/build.js +178 -0
- package/dist/core/engine/child.js +389 -0
- package/dist/core/engine/host.js +206 -0
- package/dist/core/engine/lock.js +133 -0
- package/dist/core/engine/protocol.js +43 -0
- package/dist/core/engine/runtime.js +44 -0
- package/dist/core/engine/status.js +381 -0
- package/dist/core/engine/typescript.js +194 -0
- package/dist/core/engine/workspace.js +72 -0
- package/dist/core/graph/graph.js +127 -0
- package/dist/core/graph/limits.js +16 -0
- package/dist/core/impact/query.js +1292 -0
- package/dist/core/impact/render.js +590 -0
- package/dist/core/impact/session.js +694 -0
- package/dist/core/indexing/declarations.js +455 -0
- package/dist/core/indexing/differential.js +345 -0
- package/dist/core/indexing/doctor.js +120 -0
- package/dist/core/indexing/indexer.js +792 -0
- package/dist/core/indexing/keys.js +451 -0
- package/dist/core/indexing/lexical.js +212 -0
- package/dist/core/indexing/packages.js +57 -0
- package/dist/core/indexing/program.js +171 -0
- package/dist/core/indexing/resolve.js +1004 -0
- package/dist/core/indexing/stamp.js +52 -0
- package/dist/core/indexing/surface.js +335 -0
- package/dist/core/indexing/ui-progress.js +37 -0
- package/dist/core/indexing/verify.js +76 -0
- package/dist/core/kernel/config.js +383 -0
- package/dist/core/kernel/glob.js +105 -0
- package/dist/core/kernel/guards.js +4 -0
- package/dist/core/kernel/types.js +3 -0
- package/dist/core/kernel/verdict.js +142 -0
- package/dist/core/knowledge/anchors.js +139 -0
- package/dist/core/knowledge/knowledge.js +95 -0
- package/dist/core/knowledge/learning.js +102 -0
- package/dist/core/knowledge/render.js +76 -0
- package/dist/core/knowledge/store.js +109 -0
- package/dist/core/knowledge/yaml.js +87 -0
- package/dist/core/search/lexical.js +382 -0
- package/dist/core/search/render.js +98 -0
- package/dist/core/search/segments.js +118 -0
- package/dist/core/verify/parse.js +127 -0
- package/dist/core/verify/render.js +67 -0
- package/dist/core/verify/report.js +40 -0
- package/dist/core/verify/revision.js +79 -0
- package/dist/core/verify/run.js +117 -0
- package/dist/core/verify/store.js +66 -0
- package/dist/core/verify/verify.js +173 -0
- package/dist/core/workspace/init.js +267 -0
- package/dist/core/workspace/probe.js +56 -0
- package/dist/core/workspace/scan.js +420 -0
- package/dist/index.js +20 -0
- package/dist/policy/change-scope.js +500 -0
- package/dist/policy/check-changed.js +40 -0
- package/dist/policy/config.js +106 -0
- package/dist/policy/dist/policy.d.ts +17 -0
- package/dist/policy/dist/policy.js +147 -0
- package/dist/policy/dist/runtime.d.ts +40 -0
- package/dist/policy/dist/runtime.js +75 -0
- package/dist/policy/dist/sdlc.d.ts +7 -0
- package/dist/policy/dist/sdlc.js +27 -0
- package/dist/policy/eslint-rules/index.js +107 -0
- package/dist/policy/index.d.ts +2 -0
- package/dist/policy/index.js +241 -0
- package/dist/policy/package.json +33 -0
- package/dist/policy/policy.d.ts +2 -0
- package/dist/policy/policy.js +2 -0
- package/dist/policy/review-verdict.js +262 -0
- package/dist/policy/sdlc.js +2 -0
- package/dist/storage/records.js +245 -0
- package/dist/storage/sqlite.js +314 -0
- package/dist/workflow/catalog.js +69 -0
- package/dist/workflow/classify.js +55 -0
- package/dist/workflow/loader.js +140 -0
- package/dist/workflow/packs.js +469 -0
- package/dist/workflow/registry.js +359 -0
- package/dist/workflow/render.js +61 -0
- package/dist/workflow/router.js +141 -0
- package/dist/workflow/sources.js +229 -0
- package/dist/workflow/types.js +44 -0
- package/dist/workflow/updates.js +532 -0
- package/package.json +74 -4
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +451 -0
- package/skills/ksd-intelligence/SKILL.md +54 -0
- package/skills/matt-sdlc/SKILL.md +42 -0
- package/skills/osmani-quality/SKILL.md +38 -0
- package/skills/poteto-mode/SKILL.md +40 -0
- package/skills/pstack-analysis/SKILL.md +36 -0
- package/skills.lock.json +71 -0
- package/skills.registry.json +181 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { chmodSync, closeSync, constants, copyFileSync, existsSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, } from 'node:fs';
|
|
2
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
export const INDEX_FILE = 'index.sqlite';
|
|
5
|
+
export const STAGING_FILE = 'index.staging.sqlite';
|
|
6
|
+
/** Written when a build fails VERIFY: the previous index was kept; these are `incompleteReasons`. */
|
|
7
|
+
export const FAILURE_FILE = 'last-failure.json';
|
|
8
|
+
// Numbered migrations; a database at version n gets every migration with version > n, in order.
|
|
9
|
+
const MIGRATIONS = [
|
|
10
|
+
{
|
|
11
|
+
version: 1,
|
|
12
|
+
sql: `
|
|
13
|
+
CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL) STRICT;
|
|
14
|
+
CREATE TABLE files (
|
|
15
|
+
path TEXT PRIMARY KEY,
|
|
16
|
+
size INTEGER NOT NULL,
|
|
17
|
+
mtime_ms REAL NOT NULL,
|
|
18
|
+
hash TEXT,
|
|
19
|
+
kind TEXT NOT NULL,
|
|
20
|
+
package TEXT,
|
|
21
|
+
tsconfig TEXT,
|
|
22
|
+
status TEXT NOT NULL,
|
|
23
|
+
reason TEXT
|
|
24
|
+
) STRICT;`,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
// M2a.2: extract-local and resolve records (ADR 0002, ADR 0003). Keys are stored once, in
|
|
28
|
+
// `symbols`; declarations and edges refer to them by id (an index of text keys was 80% of
|
|
29
|
+
// the oh-my-pi database).
|
|
30
|
+
version: 2,
|
|
31
|
+
sql: `
|
|
32
|
+
CREATE TABLE file_index (
|
|
33
|
+
file TEXT PRIMARY KEY,
|
|
34
|
+
extraction TEXT NOT NULL,
|
|
35
|
+
lines INTEGER NOT NULL,
|
|
36
|
+
surface TEXT NOT NULL,
|
|
37
|
+
global INTEGER NOT NULL,
|
|
38
|
+
indexed_uses INTEGER NOT NULL,
|
|
39
|
+
local_uses INTEGER NOT NULL,
|
|
40
|
+
external INTEGER NOT NULL,
|
|
41
|
+
external_unresolved INTEGER NOT NULL,
|
|
42
|
+
unresolved_other INTEGER NOT NULL,
|
|
43
|
+
outside_scope INTEGER NOT NULL
|
|
44
|
+
) STRICT;
|
|
45
|
+
CREATE TABLE symbols (
|
|
46
|
+
id INTEGER PRIMARY KEY,
|
|
47
|
+
key TEXT NOT NULL UNIQUE
|
|
48
|
+
) STRICT;
|
|
49
|
+
CREATE TABLE declarations (
|
|
50
|
+
id INTEGER PRIMARY KEY,
|
|
51
|
+
file TEXT NOT NULL,
|
|
52
|
+
name TEXT NOT NULL,
|
|
53
|
+
simple TEXT NOT NULL,
|
|
54
|
+
kind TEXT NOT NULL,
|
|
55
|
+
exported INTEGER NOT NULL,
|
|
56
|
+
start_line INTEGER NOT NULL,
|
|
57
|
+
end_line INTEGER NOT NULL,
|
|
58
|
+
other_spans TEXT NOT NULL,
|
|
59
|
+
sig_hash TEXT NOT NULL
|
|
60
|
+
) STRICT;
|
|
61
|
+
CREATE INDEX declarations_file ON declarations (file);
|
|
62
|
+
CREATE INDEX declarations_simple ON declarations (simple);
|
|
63
|
+
CREATE TABLE edges (
|
|
64
|
+
source INTEGER NOT NULL,
|
|
65
|
+
target INTEGER NOT NULL,
|
|
66
|
+
kind TEXT NOT NULL,
|
|
67
|
+
strength TEXT NOT NULL,
|
|
68
|
+
lines TEXT NOT NULL,
|
|
69
|
+
PRIMARY KEY (source, target, kind)
|
|
70
|
+
) STRICT, WITHOUT ROWID;
|
|
71
|
+
CREATE INDEX edges_target ON edges (target);
|
|
72
|
+
CREATE TABLE unresolved (
|
|
73
|
+
file TEXT NOT NULL,
|
|
74
|
+
line INTEGER NOT NULL,
|
|
75
|
+
name TEXT NOT NULL,
|
|
76
|
+
reason TEXT NOT NULL,
|
|
77
|
+
source TEXT NOT NULL
|
|
78
|
+
) STRICT;
|
|
79
|
+
CREATE INDEX unresolved_file ON unresolved (file);
|
|
80
|
+
CREATE TABLE gap_sites (
|
|
81
|
+
file TEXT NOT NULL,
|
|
82
|
+
line INTEGER NOT NULL,
|
|
83
|
+
code TEXT NOT NULL,
|
|
84
|
+
detail TEXT NOT NULL,
|
|
85
|
+
source TEXT NOT NULL
|
|
86
|
+
) STRICT;
|
|
87
|
+
CREATE INDEX gap_sites_file ON gap_sites (file);
|
|
88
|
+
CREATE TABLE name_tokens (
|
|
89
|
+
file TEXT NOT NULL,
|
|
90
|
+
name TEXT NOT NULL,
|
|
91
|
+
kind TEXT NOT NULL,
|
|
92
|
+
lines TEXT NOT NULL,
|
|
93
|
+
PRIMARY KEY (file, name, kind)
|
|
94
|
+
) STRICT, WITHOUT ROWID;
|
|
95
|
+
CREATE INDEX name_tokens_name ON name_tokens (name);
|
|
96
|
+
CREATE TABLE external_imports (
|
|
97
|
+
file TEXT NOT NULL,
|
|
98
|
+
specifier TEXT NOT NULL,
|
|
99
|
+
line INTEGER NOT NULL
|
|
100
|
+
) STRICT;
|
|
101
|
+
CREATE INDEX external_imports_file ON external_imports (file);
|
|
102
|
+
CREATE TABLE exports (
|
|
103
|
+
file TEXT NOT NULL,
|
|
104
|
+
name TEXT NOT NULL,
|
|
105
|
+
target TEXT NOT NULL,
|
|
106
|
+
PRIMARY KEY (file, name, target)
|
|
107
|
+
) STRICT, WITHOUT ROWID;
|
|
108
|
+
CREATE INDEX exports_target ON exports (target);`,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
// M2b: boundary markers a file's syntax shows beyond its bare imports (docs/RISK.md §3.2).
|
|
112
|
+
version: 3,
|
|
113
|
+
sql: `
|
|
114
|
+
CREATE TABLE markers (
|
|
115
|
+
file TEXT NOT NULL,
|
|
116
|
+
line INTEGER NOT NULL,
|
|
117
|
+
kind TEXT NOT NULL
|
|
118
|
+
) STRICT;
|
|
119
|
+
CREATE INDEX markers_file ON markers (file);`,
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
// M2b R8: covering and on-demand indexes so queries avoid full-table constructor loads.
|
|
123
|
+
version: 4,
|
|
124
|
+
sql: `
|
|
125
|
+
DROP INDEX edges_target;
|
|
126
|
+
CREATE INDEX edges_target_covering ON edges (target, source, kind, strength);
|
|
127
|
+
CREATE INDEX edges_implements ON edges (source, target) WHERE kind = 'IMPLEMENTS';
|
|
128
|
+
CREATE INDEX unresolved_name ON unresolved (name);
|
|
129
|
+
CREATE INDEX gap_sites_code ON gap_sites (code, detail);
|
|
130
|
+
CREATE INDEX markers_kind ON markers (kind);
|
|
131
|
+
CREATE INDEX external_imports_specifier ON external_imports (specifier);`,
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
// M6a: lexical search index over indexed declarations (`extraction = 'ok'`, non-module only).
|
|
135
|
+
// `stable_key` is column 0 (UNINDEXED), so bm25() calls MUST pass 7 weights starting with
|
|
136
|
+
// `0.0` for `stable_key` (SQLite silently shifts weights left if fewer weights are passed).
|
|
137
|
+
// `rowid` matches `declarations.id` (`symbols.id`).
|
|
138
|
+
version: 5,
|
|
139
|
+
sql: `
|
|
140
|
+
CREATE VIRTUAL TABLE declarations_fts USING fts5(
|
|
141
|
+
stable_key UNINDEXED,
|
|
142
|
+
name_tokens,
|
|
143
|
+
owner_tokens,
|
|
144
|
+
kind,
|
|
145
|
+
signature,
|
|
146
|
+
doc,
|
|
147
|
+
path_tokens,
|
|
148
|
+
tokenize = 'unicode61'
|
|
149
|
+
);`,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
// M6a Iteration 3: partial case-insensitive indexes so exact-match supplement avoids SCAN d.
|
|
153
|
+
version: 6,
|
|
154
|
+
sql: `
|
|
155
|
+
CREATE INDEX declarations_simple_nocase ON declarations (simple COLLATE NOCASE) WHERE kind != 'module';
|
|
156
|
+
CREATE INDEX declarations_name_nocase ON declarations (name COLLATE NOCASE) WHERE kind != 'module';`,
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
export const SCHEMA_VERSION = MIGRATIONS.length;
|
|
160
|
+
export function indexPath(cacheDir) {
|
|
161
|
+
return join(cacheDir, INDEX_FILE);
|
|
162
|
+
}
|
|
163
|
+
export function stagingPath(cacheDir) {
|
|
164
|
+
return join(cacheDir, STAGING_FILE);
|
|
165
|
+
}
|
|
166
|
+
function removeDatabase(path) {
|
|
167
|
+
for (const suffix of ['', '-wal', '-shm', '-journal'])
|
|
168
|
+
rmSync(path + suffix, { force: true });
|
|
169
|
+
}
|
|
170
|
+
export function readSchemaVersion(db) {
|
|
171
|
+
const table = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'");
|
|
172
|
+
if (table.get() === undefined)
|
|
173
|
+
return 0;
|
|
174
|
+
const row = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
175
|
+
return row === undefined ? 0 : Number(row.value);
|
|
176
|
+
}
|
|
177
|
+
/** All of `work` or none of it; nests (a savepoint inside a transaction is its own unit). */
|
|
178
|
+
export function transaction(db, work) {
|
|
179
|
+
db.exec('SAVEPOINT work');
|
|
180
|
+
try {
|
|
181
|
+
work();
|
|
182
|
+
db.exec('RELEASE work');
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
db.exec('ROLLBACK TO work');
|
|
186
|
+
db.exec('RELEASE work');
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function migrate(db) {
|
|
191
|
+
const from = readSchemaVersion(db);
|
|
192
|
+
for (const migration of MIGRATIONS) {
|
|
193
|
+
if (migration.version <= from)
|
|
194
|
+
continue;
|
|
195
|
+
transaction(db, () => {
|
|
196
|
+
db.exec(migration.sql);
|
|
197
|
+
writeMeta(db, { schema_version: String(migration.version) });
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Starts a build in a fresh staging database. Leftovers of an interrupted build are discarded;
|
|
203
|
+
* the published index is never touched until publishBuild renames over it.
|
|
204
|
+
*/
|
|
205
|
+
export function beginBuild(cacheDir) {
|
|
206
|
+
mkdirSync(cacheDir, { recursive: true, mode: 0o700 });
|
|
207
|
+
chmodSync(cacheDir, 0o700);
|
|
208
|
+
const path = stagingPath(cacheDir);
|
|
209
|
+
removeDatabase(path);
|
|
210
|
+
// SQLite gives its -wal/-shm/-journal files the main file's mode, so creating it 0600 covers them.
|
|
211
|
+
closeSync(openSync(path, 'wx', 0o600));
|
|
212
|
+
const db = new DatabaseSync(path);
|
|
213
|
+
db.exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;');
|
|
214
|
+
migrate(db);
|
|
215
|
+
return db;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Starts an update as a copy of the published index (D49: every publish is copy-then-rename).
|
|
219
|
+
* Null when nothing is published or the published schema is not this engine's.
|
|
220
|
+
*/
|
|
221
|
+
export function beginUpdate(cacheDir) {
|
|
222
|
+
const published = indexPath(cacheDir);
|
|
223
|
+
if (!existsSync(published))
|
|
224
|
+
return null;
|
|
225
|
+
const path = stagingPath(cacheDir);
|
|
226
|
+
removeDatabase(path);
|
|
227
|
+
// A reflink where the filesystem has one (APFS, Btrfs, XFS), a plain copy otherwise.
|
|
228
|
+
copyFileSync(published, path, constants.COPYFILE_FICLONE);
|
|
229
|
+
chmodSync(path, 0o600);
|
|
230
|
+
const db = new DatabaseSync(path);
|
|
231
|
+
if (readSchemaVersion(db) !== SCHEMA_VERSION) {
|
|
232
|
+
db.close();
|
|
233
|
+
removeDatabase(path);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
db.exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;');
|
|
237
|
+
return db;
|
|
238
|
+
}
|
|
239
|
+
/** Drops a staging database that will not be published (a failed verify, an abandoned build). */
|
|
240
|
+
export function discardBuild(db, cacheDir) {
|
|
241
|
+
if (db.isOpen)
|
|
242
|
+
db.close();
|
|
243
|
+
removeDatabase(stagingPath(cacheDir));
|
|
244
|
+
}
|
|
245
|
+
/** Atomically replaces the published index with the staging database and closes it. */
|
|
246
|
+
export function publishBuild(db, cacheDir) {
|
|
247
|
+
// Back to a rollback journal: the WAL is folded into the file, so the rename carries every page,
|
|
248
|
+
// and readers of the published file never create -wal/-shm files that a later rename would orphan.
|
|
249
|
+
db.exec('PRAGMA journal_mode = DELETE');
|
|
250
|
+
db.close();
|
|
251
|
+
const staging = stagingPath(cacheDir);
|
|
252
|
+
const fd = openSync(staging, 'r');
|
|
253
|
+
try {
|
|
254
|
+
fsyncSync(fd);
|
|
255
|
+
}
|
|
256
|
+
finally {
|
|
257
|
+
closeSync(fd);
|
|
258
|
+
}
|
|
259
|
+
renameSync(staging, indexPath(cacheDir));
|
|
260
|
+
const dir = openSync(cacheDir, 'r');
|
|
261
|
+
try {
|
|
262
|
+
fsyncSync(dir);
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
closeSync(dir);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** Opens the published index read-only; null when none has been published yet. */
|
|
269
|
+
export function openPublished(cacheDir) {
|
|
270
|
+
const path = indexPath(cacheDir);
|
|
271
|
+
if (!existsSync(path))
|
|
272
|
+
return null;
|
|
273
|
+
return new DatabaseSync(path, { readOnly: true });
|
|
274
|
+
}
|
|
275
|
+
export function writeMeta(db, entries) {
|
|
276
|
+
const upsert = db.prepare('INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value');
|
|
277
|
+
for (const [key, value] of Object.entries(entries))
|
|
278
|
+
upsert.run(key, value);
|
|
279
|
+
}
|
|
280
|
+
export function readMeta(db) {
|
|
281
|
+
const meta = {};
|
|
282
|
+
for (const row of db.prepare('SELECT key, value FROM meta').all()) {
|
|
283
|
+
meta[String(row.key)] = String(row.value);
|
|
284
|
+
}
|
|
285
|
+
return meta;
|
|
286
|
+
}
|
|
287
|
+
export function writeFiles(db, records) {
|
|
288
|
+
const insert = db.prepare(`INSERT INTO files (path, size, mtime_ms, hash, kind, package, tsconfig, status, reason)
|
|
289
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
290
|
+
transaction(db, () => {
|
|
291
|
+
for (const r of records) {
|
|
292
|
+
insert.run(r.path, r.size, r.mtimeMs, r.hash, r.kind, r.package, r.tsconfig, r.status, r.reason);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
function textOrNull(value) {
|
|
297
|
+
return value === null ? null : String(value);
|
|
298
|
+
}
|
|
299
|
+
export function readFiles(db) {
|
|
300
|
+
const rows = db
|
|
301
|
+
.prepare('SELECT path, size, mtime_ms, hash, kind, package, tsconfig, status, reason FROM files ORDER BY path')
|
|
302
|
+
.all();
|
|
303
|
+
return rows.map((row) => ({
|
|
304
|
+
path: String(row.path),
|
|
305
|
+
size: Number(row.size),
|
|
306
|
+
mtimeMs: Number(row.mtime_ms),
|
|
307
|
+
hash: textOrNull(row.hash),
|
|
308
|
+
kind: String(row.kind),
|
|
309
|
+
package: textOrNull(row.package),
|
|
310
|
+
tsconfig: textOrNull(row.tsconfig),
|
|
311
|
+
status: String(row.status),
|
|
312
|
+
reason: textOrNull(row.reason),
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PROVIDER_IDS } from './types.js';
|
|
2
|
+
export const PROVIDERS = {
|
|
3
|
+
matt: {
|
|
4
|
+
envVar: 'EI_MATT_ROOT',
|
|
5
|
+
repo: 'https://github.com/mattpocock/skills.git',
|
|
6
|
+
rootSubpath: 'skills/engineering',
|
|
7
|
+
},
|
|
8
|
+
poteto: {
|
|
9
|
+
envVar: 'EI_POTETO_ROOT',
|
|
10
|
+
repo: 'https://github.com/cursor/plugins.git',
|
|
11
|
+
rootSubpath: 'pstack/skills',
|
|
12
|
+
},
|
|
13
|
+
osmani: {
|
|
14
|
+
envVar: 'EI_OSMANI_ROOT',
|
|
15
|
+
repo: 'https://github.com/addyosmani/agent-skills.git',
|
|
16
|
+
rootSubpath: 'skills',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
// Allowlist: only these IDs can ever be loaded.
|
|
20
|
+
export const catalog = {
|
|
21
|
+
'matt:diagnosing-bugs': { provider: 'matt', path: 'diagnosing-bugs/SKILL.md' },
|
|
22
|
+
'matt:codebase-design': { provider: 'matt', path: 'codebase-design/SKILL.md' },
|
|
23
|
+
'matt:tdd': { provider: 'matt', path: 'tdd/SKILL.md' },
|
|
24
|
+
'matt:code-review': { provider: 'matt', path: 'code-review/SKILL.md' },
|
|
25
|
+
'poteto:how': { provider: 'poteto', path: 'how/SKILL.md' },
|
|
26
|
+
'poteto:why': { provider: 'poteto', path: 'why/SKILL.md' },
|
|
27
|
+
'poteto:blast-radius': { provider: 'poteto', path: 'blast-radius/SKILL.md' },
|
|
28
|
+
'osmani:spec-driven-development': {
|
|
29
|
+
provider: 'osmani',
|
|
30
|
+
path: 'spec-driven-development/SKILL.md',
|
|
31
|
+
},
|
|
32
|
+
'osmani:incremental-implementation': {
|
|
33
|
+
provider: 'osmani',
|
|
34
|
+
path: 'incremental-implementation/SKILL.md',
|
|
35
|
+
},
|
|
36
|
+
'osmani:code-review-and-quality': {
|
|
37
|
+
provider: 'osmani',
|
|
38
|
+
path: 'code-review-and-quality/SKILL.md',
|
|
39
|
+
},
|
|
40
|
+
'osmani:security-and-hardening': { provider: 'osmani', path: 'security-and-hardening/SKILL.md' },
|
|
41
|
+
'osmani:performance-optimization': {
|
|
42
|
+
provider: 'osmani',
|
|
43
|
+
path: 'performance-optimization/SKILL.md',
|
|
44
|
+
},
|
|
45
|
+
'osmani:frontend-ui-engineering': {
|
|
46
|
+
provider: 'osmani',
|
|
47
|
+
path: 'frontend-ui-engineering/SKILL.md',
|
|
48
|
+
},
|
|
49
|
+
'osmani:browser-testing-with-devtools': {
|
|
50
|
+
provider: 'osmani',
|
|
51
|
+
path: 'browser-testing-with-devtools/SKILL.md',
|
|
52
|
+
},
|
|
53
|
+
'osmani:shipping-and-launch': { provider: 'osmani', path: 'shipping-and-launch/SKILL.md' },
|
|
54
|
+
};
|
|
55
|
+
export function lookupSkill(id) {
|
|
56
|
+
return Object.entries(catalog).find(([key]) => key === id)?.[1];
|
|
57
|
+
}
|
|
58
|
+
export function skillIdsOf(provider) {
|
|
59
|
+
return Object.entries(catalog)
|
|
60
|
+
.filter(([, entry]) => entry.provider === provider)
|
|
61
|
+
.map(([id]) => id);
|
|
62
|
+
}
|
|
63
|
+
export function rootsFromEnv(env) {
|
|
64
|
+
const roots = {};
|
|
65
|
+
for (const provider of PROVIDER_IDS) {
|
|
66
|
+
roots[provider] = env[PROVIDERS[provider].envVar];
|
|
67
|
+
}
|
|
68
|
+
return roots;
|
|
69
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// `words` match whole words only (short or ambiguous tokens: "auth" must not hit "author",
|
|
2
|
+
// "oauth" or "authority"). `stems` get a leading boundary only, so "vulnerab" matches
|
|
3
|
+
// "vulnerability" and "crash" matches "crashing". English only; callers pass `type` for other languages.
|
|
4
|
+
function rule(type, words, stems) {
|
|
5
|
+
const alternatives = [`\\b(?:${stems.join('|')})`];
|
|
6
|
+
// An empty `\b(?:)\b` would match at every word boundary.
|
|
7
|
+
if (words.length > 0)
|
|
8
|
+
alternatives.push(`\\b(?:${words.join('|')})\\b`);
|
|
9
|
+
return [type, new RegExp(alternatives.join('|'), 'i')];
|
|
10
|
+
}
|
|
11
|
+
// Order is priority: the first rule that matches wins.
|
|
12
|
+
const RULES = [
|
|
13
|
+
rule('security', ['auth', 'cve', 'xss', 'csrf', 'ssrf', 'sqli'], [
|
|
14
|
+
'secur',
|
|
15
|
+
'authenticat',
|
|
16
|
+
'authoriz',
|
|
17
|
+
'permission',
|
|
18
|
+
'vulnerab',
|
|
19
|
+
'secret',
|
|
20
|
+
'injection',
|
|
21
|
+
'exploit',
|
|
22
|
+
'credential',
|
|
23
|
+
'privilege',
|
|
24
|
+
]),
|
|
25
|
+
rule('performance', [], [
|
|
26
|
+
'performance',
|
|
27
|
+
'performant',
|
|
28
|
+
'latenc',
|
|
29
|
+
'slow',
|
|
30
|
+
'benchmark',
|
|
31
|
+
'bottleneck',
|
|
32
|
+
'leak',
|
|
33
|
+
'throughput',
|
|
34
|
+
'speed',
|
|
35
|
+
]),
|
|
36
|
+
rule('ui', ['ui', 'ux', 'css'], [
|
|
37
|
+
'figma',
|
|
38
|
+
'visual',
|
|
39
|
+
'layout',
|
|
40
|
+
'frontend',
|
|
41
|
+
'front-end',
|
|
42
|
+
'responsive',
|
|
43
|
+
'tailwind',
|
|
44
|
+
'pixel',
|
|
45
|
+
'stylesheet',
|
|
46
|
+
]),
|
|
47
|
+
rule('refactor', [], ['refactor', 'cleanup', 'clean up', 'simplif', 'architectur', 'migrat', 'restructur']),
|
|
48
|
+
rule('bug', ['fix', 'fixes', 'fixed', 'fixing'], ['bug', 'crash', 'fail', 'regression', 'broken', 'debug', 'flak', 'race condition']),
|
|
49
|
+
];
|
|
50
|
+
export function classifyTask(task) {
|
|
51
|
+
const hit = RULES.find(([, pattern]) => pattern.test(task));
|
|
52
|
+
if (!hit)
|
|
53
|
+
return { type: 'feature', classifiedBy: 'default' };
|
|
54
|
+
return { type: hit[0], classifiedBy: 'keyword' };
|
|
55
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFile, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { PROVIDERS, lookupSkill } from './catalog.js';
|
|
5
|
+
import { defaultSkillsDir, parseSkillFrontmatter, SKILL_PACKS } from './packs.js';
|
|
6
|
+
import { routeTask } from './router.js';
|
|
7
|
+
import { WorkflowError, } from './types.js';
|
|
8
|
+
export const MAX_SKILL_BYTES = 64 * 1024;
|
|
9
|
+
const FRONTMATTER = /^---\r?\n[\s\S]*?\r?\n---\r?\n/;
|
|
10
|
+
export function sha256Hex(bytes) {
|
|
11
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
12
|
+
}
|
|
13
|
+
function isMissingPath(error) {
|
|
14
|
+
return (error instanceof Error &&
|
|
15
|
+
'code' in error &&
|
|
16
|
+
(error.code === 'ENOENT' || error.code === 'ENOTDIR'));
|
|
17
|
+
}
|
|
18
|
+
async function realpathOrThrow(path, onMissing) {
|
|
19
|
+
try {
|
|
20
|
+
return await realpath(path);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (isMissingPath(error))
|
|
24
|
+
throw onMissing();
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// Reads one allowlisted SKILL.md and enforces every check except the lock-file hash.
|
|
29
|
+
// `sources check` reuses this so it measures exactly what the loader would load.
|
|
30
|
+
export async function readSkill(id, roots) {
|
|
31
|
+
const entry = lookupSkill(id);
|
|
32
|
+
if (!entry)
|
|
33
|
+
throw new WorkflowError('NOT_ALLOWLISTED', `${id} is not in the skill catalog`);
|
|
34
|
+
const provider = PROVIDERS[entry.provider];
|
|
35
|
+
const root = roots[entry.provider];
|
|
36
|
+
if (!root || !isAbsolute(root)) {
|
|
37
|
+
throw new WorkflowError('PROVIDER_ROOT_MISSING', `${provider.envVar} is unset or not an absolute path (needed for ${id})`);
|
|
38
|
+
}
|
|
39
|
+
const base = await realpathOrThrow(root, () => new WorkflowError('PROVIDER_ROOT_NOT_FOUND', `${provider.envVar}=${root} does not exist (needed for ${id})`));
|
|
40
|
+
const candidate = resolve(base, provider.rootSubpath, entry.path);
|
|
41
|
+
const file = await realpathOrThrow(candidate, () => new WorkflowError('SKILL_NOT_FOUND', `${id}: root exists but ${candidate} does not`));
|
|
42
|
+
const rel = relative(base, file);
|
|
43
|
+
if (rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
|
|
44
|
+
throw new WorkflowError('PATH_ESCAPE', `${id} resolves to ${file}, outside provider root ${base}`);
|
|
45
|
+
}
|
|
46
|
+
const info = await stat(file);
|
|
47
|
+
if (!info.isFile())
|
|
48
|
+
throw new WorkflowError('INVALID_SKILL', `${id}: ${file} is not a regular file`);
|
|
49
|
+
if (info.size > MAX_SKILL_BYTES)
|
|
50
|
+
throw new WorkflowError('INVALID_SKILL', `${id}: ${file} is ${info.size} bytes, over the ${MAX_SKILL_BYTES} byte cap`);
|
|
51
|
+
const bytes = await readFile(file);
|
|
52
|
+
if (bytes.length > MAX_SKILL_BYTES)
|
|
53
|
+
throw new WorkflowError('INVALID_SKILL', `${id}: ${file} grew past the ${MAX_SKILL_BYTES} byte cap while reading`);
|
|
54
|
+
const text = bytes.toString('utf8');
|
|
55
|
+
if (!FRONTMATTER.test(text))
|
|
56
|
+
throw new WorkflowError('INVALID_SKILL', `${id}: ${file} has no frontmatter`);
|
|
57
|
+
return { id, file, baseDir: dirname(file), text, sha256: sha256Hex(bytes) };
|
|
58
|
+
}
|
|
59
|
+
export async function loadSkill(id, roots, lock) {
|
|
60
|
+
const skill = await readSkill(id, roots);
|
|
61
|
+
const entry = lookupSkill(id);
|
|
62
|
+
const pinned = entry ? (lock?.providers[entry.provider].skills[id]?.sha256 ?? null) : null;
|
|
63
|
+
if (pinned === null)
|
|
64
|
+
return { ...skill, hash: 'unpinned' };
|
|
65
|
+
if (pinned !== skill.sha256) {
|
|
66
|
+
throw new WorkflowError('HASH_MISMATCH', `${id}: ${skill.file} has sha256 ${skill.sha256}, lock pins ${pinned}`);
|
|
67
|
+
}
|
|
68
|
+
return { ...skill, hash: 'verified' };
|
|
69
|
+
}
|
|
70
|
+
export async function loadPackSkill(packId, skillsDir = defaultSkillsDir()) {
|
|
71
|
+
const pack = SKILL_PACKS[packId];
|
|
72
|
+
if (!isAbsolute(skillsDir)) {
|
|
73
|
+
throw new WorkflowError('PROVIDER_ROOT_MISSING', `skillsDir must be an absolute path (needed for ${packId})`);
|
|
74
|
+
}
|
|
75
|
+
const base = await realpathOrThrow(skillsDir, () => new WorkflowError('PROVIDER_ROOT_NOT_FOUND', `skills catalog root ${skillsDir} does not exist (needed for ${packId})`));
|
|
76
|
+
const candidate = resolve(base, packId, 'SKILL.md');
|
|
77
|
+
const file = await realpathOrThrow(candidate, () => new WorkflowError('SKILL_NOT_FOUND', `${packId}: catalog skill ${candidate} not found`));
|
|
78
|
+
const rel = relative(base, file);
|
|
79
|
+
if (rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
|
|
80
|
+
throw new WorkflowError('PATH_ESCAPE', `${packId} resolves to ${file}, outside catalog root ${base}`);
|
|
81
|
+
}
|
|
82
|
+
const info = await stat(file);
|
|
83
|
+
if (!info.isFile()) {
|
|
84
|
+
throw new WorkflowError('INVALID_SKILL', `${packId}: ${file} is not a regular file`);
|
|
85
|
+
}
|
|
86
|
+
if (info.size > MAX_SKILL_BYTES) {
|
|
87
|
+
throw new WorkflowError('INVALID_SKILL', `${packId}: ${file} is ${info.size} bytes, over the ${MAX_SKILL_BYTES} byte cap`);
|
|
88
|
+
}
|
|
89
|
+
const bytes = await readFile(file);
|
|
90
|
+
if (bytes.length > MAX_SKILL_BYTES) {
|
|
91
|
+
throw new WorkflowError('INVALID_SKILL', `${packId}: ${file} grew past the ${MAX_SKILL_BYTES} byte cap while reading`);
|
|
92
|
+
}
|
|
93
|
+
const text = bytes.toString('utf8');
|
|
94
|
+
const frontmatter = parseSkillFrontmatter(text);
|
|
95
|
+
if (!frontmatter || !frontmatter.name || !frontmatter.description) {
|
|
96
|
+
throw new WorkflowError('INVALID_SKILL', `${packId}: ${file} has invalid frontmatter`);
|
|
97
|
+
}
|
|
98
|
+
if (pack.disableModelInvocation && !frontmatter.disableModelInvocation) {
|
|
99
|
+
throw new WorkflowError('INVALID_SKILL', `${packId}: ${file} must declare disable-model-invocation: true`);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
id: packId,
|
|
103
|
+
file,
|
|
104
|
+
baseDir: dirname(file),
|
|
105
|
+
text,
|
|
106
|
+
sha256: sha256Hex(bytes),
|
|
107
|
+
hash: 'unpinned',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export async function prepareWorkflow(options) {
|
|
111
|
+
const route = routeTask(options);
|
|
112
|
+
const loaded = [];
|
|
113
|
+
const packInstructions = [];
|
|
114
|
+
const missing = [];
|
|
115
|
+
for (const id of route.skillIds) {
|
|
116
|
+
try {
|
|
117
|
+
loaded.push(await loadSkill(id, options.roots, options.lock));
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (!(error instanceof WorkflowError))
|
|
121
|
+
throw error;
|
|
122
|
+
missing.push({ id, code: error.code, reason: error.message });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const packId of route.optInPacks) {
|
|
126
|
+
try {
|
|
127
|
+
packInstructions.push(await loadPackSkill(packId, options.skillsDir));
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (!(error instanceof WorkflowError))
|
|
131
|
+
throw error;
|
|
132
|
+
missing.push({ id: packId, code: error.code, reason: error.message });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (options.strict && missing.length > 0) {
|
|
136
|
+
const detail = missing.map((entry) => `${entry.id} (${entry.code})`).join(', ');
|
|
137
|
+
throw new WorkflowError('SKILLS_UNAVAILABLE', `required skills unavailable: ${detail}`);
|
|
138
|
+
}
|
|
139
|
+
return { ...route, loaded, packInstructions, missing, ready: missing.length === 0 };
|
|
140
|
+
}
|