@krisada-m/ksd-intelligence 0.0.0-stage → 1.1.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/.antigravity-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/agy-skills/erosiano-review/SKILL.md +17 -0
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1013 -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 +884 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +389 -0
- package/dist/adapters/ui/tree.js +471 -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 +452 -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 +811 -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/engineering/baseline.js +337 -0
- package/dist/engineering/change-scope.js +500 -0
- package/dist/engineering/check-changed.js +40 -0
- package/dist/engineering/config.js +129 -0
- package/dist/engineering/dist/policy.d.ts +17 -0
- package/dist/engineering/dist/policy.js +150 -0
- package/dist/engineering/dist/runtime.d.ts +40 -0
- package/dist/engineering/dist/runtime.js +77 -0
- package/dist/engineering/dist/sdlc.d.ts +7 -0
- package/dist/engineering/dist/sdlc.js +27 -0
- package/dist/engineering/eslint-rules/index.js +107 -0
- package/dist/engineering/index.d.ts +7 -0
- package/dist/engineering/index.js +319 -0
- package/dist/engineering/intelligence.js +44 -0
- package/dist/engineering/package.json +36 -0
- package/dist/engineering/policy.js +2 -0
- package/dist/engineering/practice-tool.js +72 -0
- package/dist/engineering/practices.js +283 -0
- package/dist/engineering/practices.json +2453 -0
- package/dist/engineering/review-verdict.js +262 -0
- package/dist/engineering/sdlc.js +2 -0
- package/dist/index.js +20 -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/engineering.lock.json +94 -0
- package/package.json +81 -4
- package/rules/engineering.md +20 -0
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/engineering.mjs +91 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +491 -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,194 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { dirname, join } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { promisify } from 'node:util';
|
|
10
|
+
import { isRecord } from '../kernel/guards.js';
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
// Files the checker cannot work without; their absence means a broken or partial install.
|
|
13
|
+
const REQUIRED_LIB_FILES = ['lib.d.ts', 'lib.es5.d.ts'];
|
|
14
|
+
function readJson(path) {
|
|
15
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
16
|
+
}
|
|
17
|
+
/** This package's own package.json, found by walking up from the built module. */
|
|
18
|
+
function ownPackage() {
|
|
19
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
for (;;) {
|
|
21
|
+
const file = join(dir, 'package.json');
|
|
22
|
+
const pkg = existsSync(file) ? readJson(file) : null;
|
|
23
|
+
if (isRecord(pkg) &&
|
|
24
|
+
(pkg.name === 'ksd-intelligence' || pkg.name === '@krisada-m/ksd-intelligence'))
|
|
25
|
+
return { file, pkg };
|
|
26
|
+
const parent = dirname(dir);
|
|
27
|
+
if (parent === dir)
|
|
28
|
+
throw new Error(`ksd-intelligence package.json not found above ${dir}`);
|
|
29
|
+
dir = parent;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** `eiRuntime.typescript` from this package's own package.json: the one pinned version. */
|
|
33
|
+
export function pinnedTypescriptVersion() {
|
|
34
|
+
const { file, pkg } = ownPackage();
|
|
35
|
+
const pinned = isRecord(pkg.eiRuntime) ? pkg.eiRuntime.typescript : undefined;
|
|
36
|
+
if (typeof pinned !== 'string' || !/^\d+\.\d+\.\d+$/.test(pinned)) {
|
|
37
|
+
throw new Error(`eiRuntime.typescript must be an exact version in ${file}`);
|
|
38
|
+
}
|
|
39
|
+
return pinned;
|
|
40
|
+
}
|
|
41
|
+
/** The engine version every result carries (DESIGN §7.1): this package's own version. */
|
|
42
|
+
export function engineVersion() {
|
|
43
|
+
const { file, pkg } = ownPackage();
|
|
44
|
+
if (typeof pkg.version !== 'string')
|
|
45
|
+
throw new Error(`version missing in ${file}`);
|
|
46
|
+
return pkg.version;
|
|
47
|
+
}
|
|
48
|
+
export function typescriptRuntimeDir(options) {
|
|
49
|
+
const home = options.cacheHome ?? homedir();
|
|
50
|
+
return join(home, '.cache', 'ksd-intelligence', 'runtime', `typescript-${options.version}`);
|
|
51
|
+
}
|
|
52
|
+
const SETUP_FIX = 'run: ei setup';
|
|
53
|
+
async function sha256Of(path) {
|
|
54
|
+
return createHash('sha256')
|
|
55
|
+
.update(await readFile(path))
|
|
56
|
+
.digest('hex');
|
|
57
|
+
}
|
|
58
|
+
/** Problems with an installed package, or null when it is complete and at `version`. */
|
|
59
|
+
function inspectPackage(packageDir, version) {
|
|
60
|
+
const pkgFile = join(packageDir, 'package.json');
|
|
61
|
+
if (!existsSync(pkgFile))
|
|
62
|
+
return 'typescript runtime missing';
|
|
63
|
+
const pkg = readJson(pkgFile);
|
|
64
|
+
const installed = isRecord(pkg) ? pkg.version : undefined;
|
|
65
|
+
if (installed !== version) {
|
|
66
|
+
return `typescript runtime mismatch (installed ${String(installed)}, pinned ${version})`;
|
|
67
|
+
}
|
|
68
|
+
const missing = ['typescript.js', ...REQUIRED_LIB_FILES].filter((name) => !existsSync(join(packageDir, 'lib', name)));
|
|
69
|
+
return missing.length === 0 ? null : `typescript runtime incomplete (missing lib/${missing[0]})`;
|
|
70
|
+
}
|
|
71
|
+
/** Verifies the provisioned runtime against its manifest; reads files, never the network. */
|
|
72
|
+
export async function checkTypescriptRuntime(options) {
|
|
73
|
+
const dir = typescriptRuntimeDir(options);
|
|
74
|
+
const packageDir = join(dir, 'node_modules', 'typescript');
|
|
75
|
+
const path = join(packageDir, 'lib', 'typescript.js');
|
|
76
|
+
try {
|
|
77
|
+
const manifestFile = join(dir, 'manifest.json');
|
|
78
|
+
if (!existsSync(manifestFile))
|
|
79
|
+
return { ok: false, reason: 'typescript runtime missing', fix: SETUP_FIX };
|
|
80
|
+
const manifest = readJson(manifestFile);
|
|
81
|
+
if (!isRecord(manifest) || manifest.version !== options.version) {
|
|
82
|
+
return { ok: false, reason: 'typescript runtime mismatch (manifest)', fix: SETUP_FIX };
|
|
83
|
+
}
|
|
84
|
+
const problem = inspectPackage(packageDir, options.version);
|
|
85
|
+
if (problem !== null)
|
|
86
|
+
return { ok: false, reason: problem, fix: SETUP_FIX };
|
|
87
|
+
const sha256 = await sha256Of(path);
|
|
88
|
+
if (sha256 !== manifest.sha256) {
|
|
89
|
+
return { ok: false, reason: 'typescript runtime modified (sha256 mismatch)', fix: SETUP_FIX };
|
|
90
|
+
}
|
|
91
|
+
return { ok: true, runtime: { version: options.version, path, source: 'runtime' }, sha256 };
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
95
|
+
return { ok: false, reason: `typescript runtime unreadable (${message})`, fix: SETUP_FIX };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function onPath(command, env) {
|
|
99
|
+
try {
|
|
100
|
+
await run(command, ['--version'], { env });
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* `ei setup` (D44): installs exactly the pinned TypeScript into the private runtime directory,
|
|
109
|
+
* verifies it and writes the manifest last, so an interrupted install never passes the check.
|
|
110
|
+
* The only network use in the library; owner-run.
|
|
111
|
+
*/
|
|
112
|
+
export async function setupTypescriptRuntime(options) {
|
|
113
|
+
const hud = (phase) => {
|
|
114
|
+
try {
|
|
115
|
+
options.onHudProgress?.({ phase });
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// Presentation cannot change setup outcome.
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
hud('setup:prepare');
|
|
122
|
+
const dir = typescriptRuntimeDir(options);
|
|
123
|
+
const spec = options.spec ?? `typescript@${options.version}`;
|
|
124
|
+
const installer = options.installer ?? ((await onPath('npm', options.env)) ? 'npm' : 'bun');
|
|
125
|
+
// A clean reinstall: npm and bun skip a package already at the version, even if its files changed.
|
|
126
|
+
await rm(dir, { recursive: true, force: true });
|
|
127
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
128
|
+
await chmod(dir, 0o700);
|
|
129
|
+
await writeFile(join(dir, 'package.json'), '{ "private": true }\n');
|
|
130
|
+
const args = installer === 'npm'
|
|
131
|
+
? [
|
|
132
|
+
'install',
|
|
133
|
+
'--prefix',
|
|
134
|
+
dir,
|
|
135
|
+
'--no-save',
|
|
136
|
+
'--no-package-lock',
|
|
137
|
+
'--no-audit',
|
|
138
|
+
'--no-fund',
|
|
139
|
+
'--ignore-scripts',
|
|
140
|
+
spec,
|
|
141
|
+
]
|
|
142
|
+
: ['add', '--cwd', dir, '--exact', '--ignore-scripts', '--no-save', spec];
|
|
143
|
+
hud('setup:install');
|
|
144
|
+
try {
|
|
145
|
+
await run(installer, args, { env: options.env, maxBuffer: 16 * 2 ** 20 });
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
149
|
+
return {
|
|
150
|
+
ok: false,
|
|
151
|
+
reason: `typescript install failed (${installer}): ${message.trim()}`,
|
|
152
|
+
fix: SETUP_FIX,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
hud('setup:verify');
|
|
156
|
+
const packageDir = join(dir, 'node_modules', 'typescript');
|
|
157
|
+
const problem = inspectPackage(packageDir, options.version);
|
|
158
|
+
if (problem !== null)
|
|
159
|
+
return { ok: false, reason: problem, fix: SETUP_FIX };
|
|
160
|
+
const manifest = {
|
|
161
|
+
version: options.version,
|
|
162
|
+
sha256: await sha256Of(join(packageDir, 'lib', 'typescript.js')),
|
|
163
|
+
installer,
|
|
164
|
+
installedAt: new Date().toISOString(),
|
|
165
|
+
};
|
|
166
|
+
await writeFile(join(dir, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`, {
|
|
167
|
+
mode: 0o600,
|
|
168
|
+
});
|
|
169
|
+
const result = await checkTypescriptRuntime(options);
|
|
170
|
+
if (result.ok)
|
|
171
|
+
hud('setup:complete');
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The TypeScript the engine loads: the provisioned runtime, or only with
|
|
176
|
+
* `impact.useWorkspaceTypescript: true` the analysed repository's own copy (D30, D44).
|
|
177
|
+
*/
|
|
178
|
+
export async function resolveTypescript(options) {
|
|
179
|
+
if (!options.useWorkspaceTypescript)
|
|
180
|
+
return checkTypescriptRuntime(options);
|
|
181
|
+
try {
|
|
182
|
+
const path = createRequire(join(options.repoRoot, 'package.json')).resolve('typescript');
|
|
183
|
+
const pkg = readJson(join(dirname(dirname(path)), 'package.json'));
|
|
184
|
+
const version = isRecord(pkg) && typeof pkg.version === 'string' ? pkg.version : 'unknown';
|
|
185
|
+
return { ok: true, runtime: { version, path, source: 'workspace' }, sha256: null };
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
reason: 'workspace typescript missing',
|
|
191
|
+
fix: 'install typescript in the repository, or set impact.useWorkspaceTypescript: false and run: ei setup',
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { readFile, realpath } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { defaultConfig, parseProjectConfig } from '../kernel/config.js';
|
|
5
|
+
import { computeRepoId, detectProjectLayout, findGitRoot, resolveCacheDir, } from '../workspace/init.js';
|
|
6
|
+
// Searched upward from `start`, but never above the git work-tree root that contains it.
|
|
7
|
+
function findConfigRoot(start, gitRoot) {
|
|
8
|
+
let dir = start;
|
|
9
|
+
for (;;) {
|
|
10
|
+
if (existsSync(join(dir, 'ei.config.json')))
|
|
11
|
+
return dir;
|
|
12
|
+
const parent = dirname(dir);
|
|
13
|
+
if (dir === gitRoot || parent === dir)
|
|
14
|
+
return null;
|
|
15
|
+
dir = parent;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Lightweight repository root + cache directory lookup (no config read or layout detection). */
|
|
19
|
+
export async function resolveWorkspaceCache(start, cacheHome) {
|
|
20
|
+
const real = await realpath(start);
|
|
21
|
+
const gitRoot = findGitRoot(real);
|
|
22
|
+
const root = findConfigRoot(real, gitRoot) ?? gitRoot ?? real;
|
|
23
|
+
const repoId = computeRepoId(root);
|
|
24
|
+
return {
|
|
25
|
+
root,
|
|
26
|
+
repoId,
|
|
27
|
+
cacheDir: resolveCacheDir(repoId, cacheHome),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The repository a session or CLI call is about: the nearest directory holding `ei.config.json`,
|
|
32
|
+
* else the git work-tree root, else `start` itself. Same repo id and cache dir as `ei init`.
|
|
33
|
+
*/
|
|
34
|
+
export async function loadWorkspace(start, cacheHome) {
|
|
35
|
+
const { root, repoId, cacheDir } = await resolveWorkspaceCache(start, cacheHome);
|
|
36
|
+
const configPath = join(root, 'ei.config.json');
|
|
37
|
+
if (!existsSync(configPath)) {
|
|
38
|
+
return {
|
|
39
|
+
root,
|
|
40
|
+
repoId,
|
|
41
|
+
cacheDir,
|
|
42
|
+
configPath: null,
|
|
43
|
+
config: defaultConfig(detectProjectLayout(root)),
|
|
44
|
+
configErrors: [],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const { config, errors } = parseProjectConfig(await readFile(configPath, 'utf8'));
|
|
48
|
+
return {
|
|
49
|
+
root,
|
|
50
|
+
repoId,
|
|
51
|
+
cacheDir,
|
|
52
|
+
configPath,
|
|
53
|
+
config,
|
|
54
|
+
configErrors: errors,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The project config that applies at `start`, read synchronously for load-time decisions
|
|
59
|
+
* (which V2 tools to register). Null when there is no `ei.config.json` or it cannot be read.
|
|
60
|
+
*/
|
|
61
|
+
export function readProjectConfigSync(start) {
|
|
62
|
+
try {
|
|
63
|
+
const real = realpathSync(start);
|
|
64
|
+
const root = findConfigRoot(real, findGitRoot(real));
|
|
65
|
+
if (root === null)
|
|
66
|
+
return null;
|
|
67
|
+
return parseProjectConfig(readFileSync(join(root, 'ei.config.json'), 'utf8')).config;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { openPublished } from '../../storage/sqlite.js';
|
|
2
|
+
// Declarations with their keys; symbol lookups filter this.
|
|
3
|
+
const DECLARED = 'declarations d JOIN symbols s ON s.id = d.id';
|
|
4
|
+
// File-level edges say which modules a file loads or tests, not who uses a symbol.
|
|
5
|
+
const FILE_LEVEL = "('IMPORTS', 'TESTS')";
|
|
6
|
+
/**
|
|
7
|
+
* Read-only queries over a published index (DESIGN §6.2.1: an impact query is a graph traversal).
|
|
8
|
+
* Internal: tests and M2b use it; no agent tool is registered on it in M2a.
|
|
9
|
+
*/
|
|
10
|
+
export class Graph {
|
|
11
|
+
db;
|
|
12
|
+
constructor(db) {
|
|
13
|
+
this.db = db;
|
|
14
|
+
}
|
|
15
|
+
static open(cacheDir) {
|
|
16
|
+
const db = openPublished(cacheDir);
|
|
17
|
+
return db === null ? null : new Graph(db);
|
|
18
|
+
}
|
|
19
|
+
close() {
|
|
20
|
+
this.db.close();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Who uses `key`, breadth first over incoming symbol edges: `result[0]` holds direct users.
|
|
24
|
+
* Each symbol appears once, at its shallowest depth; the target is never its own user.
|
|
25
|
+
*/
|
|
26
|
+
incoming(key, depth = 10) {
|
|
27
|
+
const statement = this.db.prepare(`SELECT s.key AS source, e.kind AS kind, e.strength AS strength, d.file AS source_file,
|
|
28
|
+
e.lines AS lines
|
|
29
|
+
FROM edges e JOIN symbols t ON t.id = e.target JOIN symbols s ON s.id = e.source
|
|
30
|
+
JOIN declarations d ON d.id = e.source
|
|
31
|
+
WHERE t.key = ? AND e.kind NOT IN ${FILE_LEVEL} ORDER BY s.key, e.kind`);
|
|
32
|
+
const seen = new Set([key]);
|
|
33
|
+
const levels = [];
|
|
34
|
+
let frontier = [key];
|
|
35
|
+
while (frontier.length > 0 && levels.length < depth) {
|
|
36
|
+
const level = [];
|
|
37
|
+
const next = [];
|
|
38
|
+
for (const target of frontier) {
|
|
39
|
+
for (const row of statement.all(target)) {
|
|
40
|
+
const source = String(row.source);
|
|
41
|
+
if (seen.has(source))
|
|
42
|
+
continue;
|
|
43
|
+
seen.add(source);
|
|
44
|
+
next.push(source);
|
|
45
|
+
const lines = JSON.parse(String(row.lines));
|
|
46
|
+
level.push({
|
|
47
|
+
symbol: source,
|
|
48
|
+
via: String(row.kind),
|
|
49
|
+
strength: String(row.strength),
|
|
50
|
+
file: String(row.source_file),
|
|
51
|
+
lines: Array.isArray(lines) ? lines.map(Number) : [],
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (level.length === 0)
|
|
56
|
+
break;
|
|
57
|
+
level.sort((a, b) => (a.symbol < b.symbol ? -1 : a.symbol > b.symbol ? 1 : 0));
|
|
58
|
+
levels.push(level);
|
|
59
|
+
frontier = next;
|
|
60
|
+
}
|
|
61
|
+
return levels;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resolves what an agent would type to a key: `path#name`, `path:line`, `Owner.member` or a bare
|
|
65
|
+
* name. A bare name matches module-level names and member names alike (F7).
|
|
66
|
+
*/
|
|
67
|
+
symbol(query) {
|
|
68
|
+
const keys = (sql, ...params) => this.db
|
|
69
|
+
.prepare(sql)
|
|
70
|
+
.all(...params)
|
|
71
|
+
.map((row) => String(row.key));
|
|
72
|
+
let candidates;
|
|
73
|
+
const atLine = /^(.+):(\d+)$/.exec(query);
|
|
74
|
+
if (query.includes('#')) {
|
|
75
|
+
candidates = keys(`SELECT s.key AS key FROM ${DECLARED} WHERE s.key = ?`, query);
|
|
76
|
+
}
|
|
77
|
+
else if (atLine !== null) {
|
|
78
|
+
const [, file = '', line = '0'] = atLine;
|
|
79
|
+
// The innermost declaration whose span (or an overload's) holds the line.
|
|
80
|
+
const rows = this.db
|
|
81
|
+
.prepare(`SELECT s.key AS key, d.start_line AS start_line, d.end_line AS end_line,
|
|
82
|
+
d.other_spans AS other_spans FROM ${DECLARED} WHERE d.file = ? AND d.name != '<module>'`)
|
|
83
|
+
.all(file);
|
|
84
|
+
const n = Number(line);
|
|
85
|
+
const holding = rows
|
|
86
|
+
.map((row) => {
|
|
87
|
+
const spans = JSON.parse(String(row.other_spans));
|
|
88
|
+
const all = [
|
|
89
|
+
[Number(row.start_line), Number(row.end_line)],
|
|
90
|
+
...(Array.isArray(spans) ? spans : []).map((span) => String(span).split('-').map(Number)),
|
|
91
|
+
];
|
|
92
|
+
const hit = all.filter(([start = 0, end = 0]) => start <= n && n <= end);
|
|
93
|
+
const width = Math.min(...hit.map(([start = 0, end = 0]) => end - start));
|
|
94
|
+
return { key: String(row.key), width, hit: hit.length > 0 };
|
|
95
|
+
})
|
|
96
|
+
.filter((row) => row.hit)
|
|
97
|
+
.sort((a, b) => a.width - b.width);
|
|
98
|
+
if (holding.length === 0) {
|
|
99
|
+
candidates = [];
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const minWidth = holding[0].width;
|
|
103
|
+
const narrowest = holding.filter((row) => row.width === minWidth);
|
|
104
|
+
// Drop parent containers that share an equal span with an inner member; keep sibling ties.
|
|
105
|
+
candidates = narrowest
|
|
106
|
+
.filter((row) => !narrowest.some((other) => other.key.startsWith(`${row.key}.`)))
|
|
107
|
+
.map((row) => row.key);
|
|
108
|
+
}
|
|
109
|
+
if (candidates.length === 0)
|
|
110
|
+
candidates = keys(`SELECT s.key AS key FROM ${DECLARED} WHERE s.key = ?`, `${file}#<module>`);
|
|
111
|
+
}
|
|
112
|
+
else if (query.includes('.')) {
|
|
113
|
+
candidates = keys(`SELECT s.key AS key FROM ${DECLARED} WHERE d.name = ? ORDER BY 1`, query);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
candidates = keys(`SELECT s.key AS key FROM ${DECLARED} WHERE d.simple = ? OR d.name = ? ORDER BY 1`, query, query);
|
|
117
|
+
}
|
|
118
|
+
if (candidates.length === 1)
|
|
119
|
+
return { kind: 'found', key: candidates[0] ?? '' };
|
|
120
|
+
if (candidates.length > 1)
|
|
121
|
+
return { kind: 'ambiguous', candidates };
|
|
122
|
+
const stem = query.replace(/^.*[#.]/, '').slice(0, 4);
|
|
123
|
+
const nearest = keys(`SELECT s.key AS key FROM ${DECLARED} WHERE d.simple LIKE ? AND d.name != '<module>'
|
|
124
|
+
ORDER BY length(d.simple), s.key LIMIT 5`, `${stem}%`);
|
|
125
|
+
return { kind: 'not-found', nearest };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** D55: repository context, never target completeness or verdict inputs. */
|
|
2
|
+
export function repositoryLimits(db) {
|
|
3
|
+
const row = db
|
|
4
|
+
.prepare(`SELECT
|
|
5
|
+
(SELECT COUNT(*) FROM gap_sites WHERE code IN ('dynamic-import', 'cjs-require')
|
|
6
|
+
AND detail = '*') AS unbounded,
|
|
7
|
+
(SELECT COUNT(*) FROM files WHERE reason IN ('excluded', 'secret', 'symlink')
|
|
8
|
+
OR (reason = 'outside-index-scope' AND kind NOT IN ('json', 'other'))) AS excluded,
|
|
9
|
+
(SELECT COUNT(*) FROM file_index WHERE extraction = 'not-in-program') AS failed`)
|
|
10
|
+
.get();
|
|
11
|
+
return {
|
|
12
|
+
unboundedDynamicImports: Number(row?.unbounded ?? 0),
|
|
13
|
+
excludedFiles: Number(row?.excluded ?? 0),
|
|
14
|
+
failedFiles: Number(row?.failed ?? 0),
|
|
15
|
+
};
|
|
16
|
+
}
|