kibi-plugin-builtin 0.3.0 → 0.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # kibi-plugin-builtin
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - d25a626: Kibi now answers questions on a CI checkout or any detached commit, compiles a cold knowledge base minutes faster, and can stop a runaway read before it blocks other agents. On a bare SHA, search, query, status, check, coverage and graph read a snapshot of that checkout and say so in every answer, while writes are refused with the command that fixes it. A cold `kibi sync` of the Kibi repository went from about 4 min 23 s to 19 s, and `--refresh-symbol-coordinates` from about 5 min 55 s to 26 s, with byte-identical results.
8
+
9
+ - Detached HEAD with zero or several local branches at HEAD: read-only operations (CLI routes, human commands, MCP tools) attach a read-only snapshot store (`kibi-internal/detached-head-snapshot`) compiled incrementally from the checkout's tracked sources, and add a `detached_head_read_only` warning diagnostic with the commit, branches at HEAD, store path and `writes: "refused"`. Write operations and `kibi sync` refuse with an actionable message (`git switch <branch>`, `git switch -c <branch>`, or `KIBI_BRANCH`). One branch at HEAD still attaches that branch exactly; no branch KB is ever guessed or written. `kibi engine stop/status` address the snapshot daemon, and `kibi gc` keeps the snapshot while it is in use.
10
+ - Sync: source-owned entity lookups before retracts run in batches of 200 instead of one engine round trip per path candidate; the Prolog client wakes on the answer frame instead of a 50 ms poll; TypeScript coordinate enrichment adds every source file before the first export check, so the type checker program is built once instead of once per file.
11
+ - Engine read limits: `KIBI_ENGINE_READ_TIME_LIMIT_MS` and `KIBI_ENGINE_READ_INFERENCE_LIMIT` (opt-in, unset by default) bound each read-only engine request with `call_with_time_limit/2` and `call_with_inference_limit/3`. A read that hits its limit fails with `QUERY_LIMIT_EXCEEDED` and `error.details.limitExceeded` (`kind`, `limit`) on the CLI and MCP envelopes, never with a partial answer; write requests, module loads and sync compilation are never bounded, while the read-only queries a write operation such as `kb_upsert` runs before writing count as reads and can stop it before anything is written.
12
+ - A CLI JSON route whose runtime cannot open (a detached HEAD refusing a write, no branch to attach) now prints an `OPERATION_FAILED` error envelope on stdout, not just a message on stderr.
13
+
14
+ ## 0.4.0
15
+
16
+ ### Minor Changes
17
+
18
+ - db5376c: Kibi can now analyze source in Python, Go, Rust and a broader set of common programming and configuration languages, offline, while JavaScript and TypeScript keep working as before. Staged checks read source and authored knowledge from one immutable Git tree, inspect both sides of every change, and keep incomplete analysis explicit instead of treating it as proof. Large files now return an explicit analysis failure instead of a second validation error.
19
+ - Add the asynchronous `kibi.symbol-extractor.v2` contract with validated UTF-16 ranges, bounded inputs and host-assigned provenance, plus a ts-morph v2 adapter.
20
+ - Add the optional `kibi-plugin-treesitter` package with pinned WASM grammars, queries, licenses and an approved-analyzer closure verified before import.
21
+ - Capture staged and explicit-diff snapshots from Git objects; merge duplicate symbol-manifest records deterministically and fail conflicting authored fields with `SOURCE_DUPLICATE_CONFLICT`.
22
+ - Add opt-in, bounded parser-phase and Prolog round-trip timing observations that never enter result schemas.
23
+
24
+ ### Patch Changes
25
+
26
+ - 8622782: An impact review prepared on one machine now still validates in CI, and the MCP server and the CLI agree on whether it is current. Checks only block on incomplete source analysis the author can act on: the committed side of a change never blocks, and a known analyzer limitation such as a Rust macro or a Python decorator matters only where it overlaps the changed lines. Syntax, parse, timeout and integrity problems in the staged file still block. Long-running hosts no longer grow memory with every analyzed file, and Tree-sitter analysis reuses warm workers instead of starting one per file.
27
+
28
+ - Identify the impact evaluator by its contract version and the canonical JSON of the schemas it consumes, instead of hashing the installed file tree and `process.version`. Identify the builtin analyzer runtime by its package identity rather than its installed tree.
29
+ - `kibi-runtime` bundles the CLI operations again (`kibi-cli` is a build-time dependency only); it no longer installs `kibi-cli`.
30
+ - Add one shared analysis gate (`analysisObligation`) used by staged checks, impact-review preparation and validation, and impact reports.
31
+ - Classify source paths from a single extension table in `source-classification`, with parity tests against the Tree-sitter catalog and the builtin extractor.
32
+ - Remove analyzed files from the ts-morph project after each v1 and v2 analysis.
33
+ - Add an optional `timeoutMs` budget to `SymbolExtractorV2AnalyzeInput`; the host sets it slightly below its own deadline, so providers report their own timeout. Tree-sitter uses a bounded, persistent worker pool with grammars loaded once per worker, and the host analyzes up to four changed files at a time.
34
+
35
+ - Updated dependencies [db5376c]
36
+ - Updated dependencies [8622782]
37
+ - kibi-plugin-sdk@0.4.0
38
+
3
39
  ## 0.3.0
4
40
 
5
41
  ### Minor Changes
@@ -46,7 +82,6 @@
46
82
  - Drop `ts-morph` from `kibi-cli` runtime dependencies (kept as a test-only devDependency for AST spies)
47
83
 
48
84
  - 783cc75: Kibi ships a default capability plugin package so TypeScript symbol extraction, ontology matching, and semantic lane classification live behind the public plugin protocol instead of private CLI paths.
49
-
50
85
  - Add `kibi-plugin-builtin@0.1.0` with `kibiPlugin` exporting all three capabilities
51
86
  - Built-in ts-morph extractor, ontology pack (launcher→core→policy→product→product-tail), and sync semantic classifier
52
87
  - CLI depends on this package and thin-wraps the extractor / ontology pack for default parity
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { PRODUCT_PREDICATE_RULES } from "./ontology/predicate-rules-product.js";
8
8
  export { PRODUCT_TAIL_PREDICATE_RULES } from "./ontology/predicate-rules-product-tail.js";
9
9
  export { normalizeKey, normalizePredicateToken, normalizeSubjectKey, singularize, commaList, } from "./ontology/normalize.js";
10
10
  export { createBuiltinSemanticClassifier, BuiltinSemanticClassifier, detectSignals, chooseLane, } from "./semantic/builtin-classifier.js";
11
- export { createBuiltinTsMorphSymbolExtractor, createBuiltinTsMorphSourceAnalysisProvider, } from "./symbols/ts-morph-extractor.js";
11
+ export { createBuiltinTsMorphSymbolExtractor, createBuiltinTsMorphSymbolExtractorV2, createBuiltinTsMorphSourceAnalysisProvider, } from "./symbols/ts-morph-extractor.js";
12
12
  export { enrichSymbolCoordinatesWithTsMorph, type ManifestSymbolEntry, type SymbolCoordinates, } from "./symbols/ts-morph-enrichment.js";
13
13
  export { collectGranularityCandidates, type GranularitySymbolCandidate, } from "./symbols/ts-morph-granularity.js";
14
14
  export { isPrivateClassMember, onlyCandidate, } from "./symbols/ts-morph-shared.js";
@@ -29,6 +29,7 @@ export declare const kibiPlugin: {
29
29
  semanticClassifier: import("kibi-plugin-sdk").SemanticClassifierV1;
30
30
  ontologyPack: import("kibi-plugin-sdk").OntologyPackV1;
31
31
  symbolExtractor: import("kibi-plugin-sdk").SymbolExtractorV1;
32
+ symbolExtractorV2: import("kibi-plugin-sdk").SymbolExtractorV2;
32
33
  vocabularyAlignment: import("./index.js").BuiltinVocabularyAlignment;
33
34
  };
34
35
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA8BA,OAAO,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AAEhF,OAAO,EACL,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,KAAK,aAAa,GACnB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAC3B,MAAM,wCAAwC,CAAC;AAEhD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sCAAsC,CAAC;AAE9E,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAEhF,OAAO,EAAE,4BAA4B,EAAE,MAAM,4CAA4C,CAAC;AAE1F,OAAO,EACL,YAAY,EACZ,uBAAuB,EACvB,mBAAmB,EACnB,WAAW,EACX,SAAS,GACV,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,+BAA+B,EAC/B,yBAAyB,EACzB,aAAa,EACb,UAAU,GACX,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,mCAAmC,EACnC,0CAA0C,GAC3C,MAAM,iCAAiC,CAAC;AAEzC,OAAO,EACL,kCAAkC,EAClC,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,GACvB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,4BAA4B,EAC5B,KAAK,0BAA0B,GAChC,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,oBAAoB,EACpB,aAAa,GACd,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EACL,kCAAkC,EAClC,+BAA+B,EAC/B,0BAA0B,EAC1B,sBAAsB,EACtB,eAAe,EACf,gCAAgC,EAChC,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,8CAA8C,CAAC;AAEtD;;GAEG;AAEH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;CAerB,CAAC;AAGH,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiCA,OAAO,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AAEhF,OAAO,EACL,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,KAAK,aAAa,GACnB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAC3B,MAAM,wCAAwC,CAAC;AAEhD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sCAAsC,CAAC;AAE9E,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAEhF,OAAO,EAAE,4BAA4B,EAAE,MAAM,4CAA4C,CAAC;AAE1F,OAAO,EACL,YAAY,EACZ,uBAAuB,EACvB,mBAAmB,EACnB,WAAW,EACX,SAAS,GACV,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,+BAA+B,EAC/B,yBAAyB,EACzB,aAAa,EACb,UAAU,GACX,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,mCAAmC,EACnC,qCAAqC,EACrC,0CAA0C,GAC3C,MAAM,iCAAiC,CAAC;AAEzC,OAAO,EACL,kCAAkC,EAClC,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,GACvB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,4BAA4B,EAC5B,KAAK,0BAA0B,GAChC,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,oBAAoB,EACpB,aAAa,GACd,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EACL,kCAAkC,EAClC,+BAA+B,EAC/B,0BAA0B,EAC1B,sBAAsB,EACtB,eAAe,EACf,gCAAgC,EAChC,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,8CAA8C,CAAC;AAEtD;;GAEG;AAEH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;CAgBrB,CAAC;AAGH,eAAe,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ import { createRequire } from "node:module";
19
19
  import { KIBI_PLUGIN_API_VERSION, defineKibiPlugin } from "kibi-plugin-sdk";
20
20
  import { createBuiltinOntologyPack } from "./ontology/builtin-ontology-pack.js";
21
21
  import { createBuiltinSemanticClassifier } from "./semantic/builtin-classifier.js";
22
- import { createBuiltinTsMorphSymbolExtractor } from "./symbols/ts-morph-extractor.js";
22
+ import { createBuiltinTsMorphSymbolExtractor, createBuiltinTsMorphSymbolExtractorV2, } from "./symbols/ts-morph-extractor.js";
23
23
  import { createBuiltinVocabularyAlignment } from "./vocabulary/builtin-vocabulary-alignment.js";
24
24
  const packageJson = createRequire(import.meta.url)("../package.json");
25
25
  // implements REQ-capability-plugin-builtin-parity-v1
@@ -43,7 +43,7 @@ export { normalizeKey, normalizePredicateToken, normalizeSubjectKey, singularize
43
43
  // implements REQ-capability-plugin-builtin-parity-v1
44
44
  export { createBuiltinSemanticClassifier, BuiltinSemanticClassifier, detectSignals, chooseLane, } from "./semantic/builtin-classifier.js";
45
45
  // implements REQ-capability-plugin-builtin-parity-v1
46
- export { createBuiltinTsMorphSymbolExtractor, createBuiltinTsMorphSourceAnalysisProvider, } from "./symbols/ts-morph-extractor.js";
46
+ export { createBuiltinTsMorphSymbolExtractor, createBuiltinTsMorphSymbolExtractorV2, createBuiltinTsMorphSourceAnalysisProvider, } from "./symbols/ts-morph-extractor.js";
47
47
  // implements REQ-capability-plugin-builtin-parity-v1
48
48
  export { enrichSymbolCoordinatesWithTsMorph, } from "./symbols/ts-morph-enrichment.js";
49
49
  // implements REQ-capability-plugin-builtin-parity-v1
@@ -69,6 +69,7 @@ export const kibiPlugin = defineKibiPlugin({
69
69
  semanticClassifier: createBuiltinSemanticClassifier(),
70
70
  ontologyPack: createBuiltinOntologyPack(),
71
71
  symbolExtractor: createBuiltinTsMorphSymbolExtractor(),
72
+ symbolExtractorV2: createBuiltinTsMorphSymbolExtractorV2(),
72
73
  vocabularyAlignment: createBuiltinVocabularyAlignment(),
73
74
  },
74
75
  });
@@ -1 +1 @@
1
- {"version":3,"file":"ts-morph-enrichment.d.ts","sourceRoot":"","sources":["../../src/symbols/ts-morph-enrichment.ts"],"names":[],"mappings":"AAmCA,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAGD,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAKD,wBAAsB,kCAAkC,CACtD,OAAO,EAAE,mBAAmB,EAAE,EAC9B,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAsEhC"}
1
+ {"version":3,"file":"ts-morph-enrichment.d.ts","sourceRoot":"","sources":["../../src/symbols/ts-morph-enrichment.ts"],"names":[],"mappings":"AAmCA,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAGD,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAKD,wBAAsB,kCAAkC,CACtD,OAAO,EAAE,mBAAmB,EAAE,EAC9B,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,mBAAmB,EAAE,CAAC,CA8FhC"}
@@ -25,10 +25,32 @@ export async function enrichSymbolCoordinatesWithTsMorph(entries, workspaceRoot)
25
25
  skipAddingFilesFromTsConfig: true,
26
26
  });
27
27
  const sourceFileCache = new Map();
28
- const enriched = [];
28
+ // Add every source file before the first declaration lookup. Export checks
29
+ // consult the type checker, and adding a file discards the program, so
30
+ // interleaving adds with lookups rebuilt the whole program (and its module
31
+ // resolution) once per newly seen file. A declaration's own symbol does
32
+ // not depend on which other root files are present, so the coordinates are
33
+ // unchanged; only the repeated program construction disappears.
34
+ const resolvedPaths = [];
29
35
  for (const entry of entries) {
30
36
  try {
31
37
  const absolutePath = await resolveSourcePath(entry.sourceFile, workspaceRoot);
38
+ if (absolutePath) {
39
+ getOrAddSourceFile(project, sourceFileCache, absolutePath);
40
+ }
41
+ resolvedPaths.push({ ok: true, absolutePath });
42
+ }
43
+ catch (error) {
44
+ resolvedPaths.push({ ok: false, error });
45
+ }
46
+ }
47
+ const enriched = [];
48
+ for (const [index, entry] of entries.entries()) {
49
+ try {
50
+ const resolved = resolvedPaths[index];
51
+ if (resolved !== undefined && !resolved.ok)
52
+ throw resolved.error;
53
+ const absolutePath = resolved?.absolutePath ?? null;
32
54
  if (!absolutePath) {
33
55
  enriched.push(entry);
34
56
  continue;
@@ -1,8 +1,10 @@
1
- import type { SourceAnalysisProvider, SymbolExtractorV1 } from "kibi-plugin-sdk";
1
+ import type { SourceAnalysisProvider, SymbolExtractorV1, SymbolExtractorV2 } from "kibi-plugin-sdk";
2
2
  /**
3
3
  * Built-in ts-morph symbol extractor. Behavior matches the historical CLI
4
4
  * `createTsMorphSourceAnalysisProvider` analysis path.
5
5
  */
6
6
  export declare function createBuiltinTsMorphSymbolExtractor(): SymbolExtractorV1;
7
+ /** Built-in asynchronous ts-morph extractor for the source-bound v2 contract. */
8
+ export declare function createBuiltinTsMorphSymbolExtractorV2(): SymbolExtractorV2;
7
9
  export declare function createBuiltinTsMorphSourceAnalysisProvider(): SourceAnalysisProvider;
8
10
  //# sourceMappingURL=ts-morph-extractor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ts-morph-extractor.d.ts","sourceRoot":"","sources":["../../src/symbols/ts-morph-extractor.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EACV,sBAAsB,EAItB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AAmBzB;;;GAGG;AAEH,wBAAgB,mCAAmC,IAAI,iBAAiB,CAsCvE;AAGD,wBAAgB,0CAA0C,IAAI,sBAAsB,CAEnF"}
1
+ {"version":3,"file":"ts-morph-extractor.d.ts","sourceRoot":"","sources":["../../src/symbols/ts-morph-extractor.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EACV,sBAAsB,EAMtB,iBAAiB,EACjB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AAsDzB;;;GAGG;AAEH,wBAAgB,mCAAmC,IAAI,iBAAiB,CA4CvE;AAED,iFAAiF;AAEjF,wBAAgB,qCAAqC,IAAI,iBAAiB,CAqHzE;AA+BD,wBAAgB,0CAA0C,IAAI,sBAAsB,CAEnF"}
@@ -20,6 +20,34 @@ import { toSourceAnalysisProvider } from "kibi-plugin-sdk";
20
20
  import { tsMorph } from "./ts-morph-runtime.js";
21
21
  import { SUPPORTED_SOURCE_EXTENSIONS, chooseScriptKind, formatMethodSymbolName, isPrivateClassMember, } from "./ts-morph-shared.js";
22
22
  const EXTRACTOR_ID = "kibi-plugin-builtin.ts-morph";
23
+ function normalizeV2ScriptLanguage(language) {
24
+ const normalized = language?.trim().toLowerCase();
25
+ if (["javascript", "js", "node", "nodejs"].includes(normalized ?? "")) {
26
+ return "javascript";
27
+ }
28
+ if (["typescript", "ts"].includes(normalized ?? "")) {
29
+ return "typescript";
30
+ }
31
+ return undefined;
32
+ }
33
+ function v2ScriptKind(filePath, languageHint) {
34
+ const extension = path.extname(filePath).toLowerCase();
35
+ const language = normalizeV2ScriptLanguage(languageHint);
36
+ if (SUPPORTED_SOURCE_EXTENSIONS.has(extension)) {
37
+ if (languageHint !== undefined && language === undefined)
38
+ return undefined;
39
+ if (language !== undefined && language !== inferSourceLanguage(filePath)) {
40
+ return undefined;
41
+ }
42
+ return chooseScriptKind(filePath);
43
+ }
44
+ if (extension === "" && language !== undefined) {
45
+ return language === "typescript"
46
+ ? tsMorph().ScriptKind.TS
47
+ : tsMorph().ScriptKind.JS;
48
+ }
49
+ return undefined;
50
+ }
23
51
  /**
24
52
  * Built-in ts-morph symbol extractor. Behavior matches the historical CLI
25
53
  * `createTsMorphSourceAnalysisProvider` analysis path.
@@ -43,41 +71,175 @@ export function createBuiltinTsMorphSymbolExtractor() {
43
71
  overwrite: true,
44
72
  scriptKind: chooseScriptKind(input.path),
45
73
  });
46
- return {
47
- sourceFile: input.path,
48
- language: inferSourceLanguage(input.path),
49
- module: {
50
- title: inferModuleTitle(input.path),
74
+ try {
75
+ return {
76
+ sourceFile: input.path,
51
77
  language: inferSourceLanguage(input.path),
52
- analysisMode: "parser",
53
- },
54
- symbols: collectSourceSymbols(sourceFile),
78
+ module: {
79
+ title: inferModuleTitle(input.path),
80
+ language: inferSourceLanguage(input.path),
81
+ analysisMode: "parser",
82
+ },
83
+ symbols: collectSourceSymbols(sourceFile),
84
+ };
85
+ }
86
+ finally {
87
+ // Analysis is per file and purely syntactic; keeping files would grow
88
+ // the project without bound in a long-running daemon.
89
+ getProject().removeSourceFile(sourceFile);
90
+ }
91
+ },
92
+ };
93
+ }
94
+ /** Built-in asynchronous ts-morph extractor for the source-bound v2 contract. */
95
+ // implements REQ-capability-plugin-builtin-parity-v1
96
+ export function createBuiltinTsMorphSymbolExtractorV2() {
97
+ // Like the v1 extractor, build the project (and load the TypeScript
98
+ // compiler) on the first analysis rather than at plugin registration.
99
+ let project;
100
+ const getProject = () => {
101
+ project ??= new (tsMorph().Project)({ skipAddingFilesFromTsConfig: true });
102
+ return project;
103
+ };
104
+ return {
105
+ id: `${EXTRACTOR_ID}.v2`,
106
+ supports(input) {
107
+ return v2ScriptKind(input.path, input.language) !== undefined;
108
+ },
109
+ async analyze(input) {
110
+ const extension = path.extname(input.path).toLowerCase();
111
+ const language = normalizeV2ScriptLanguage(input.language) ??
112
+ (input.language?.trim() ||
113
+ (SUPPORTED_SOURCE_EXTENSIONS.has(extension)
114
+ ? inferSourceLanguage(input.path)
115
+ : "unknown"));
116
+ const module = {
117
+ title: inferModuleTitle(input.path),
118
+ language,
119
+ analysisMode: "parser",
55
120
  };
121
+ const scriptKind = v2ScriptKind(input.path, input.language);
122
+ if (scriptKind === undefined) {
123
+ return {
124
+ contractVersion: "kibi.symbol-extractor.v2",
125
+ status: "unsupported",
126
+ sourceFile: input.path,
127
+ language,
128
+ module,
129
+ symbols: [],
130
+ diagnostics: [
131
+ {
132
+ code: "UNSUPPORTED_SOURCE_TYPE",
133
+ message: `The built-in ts-morph extractor does not support '${path.extname(input.path) || "extensionless"}' files`,
134
+ },
135
+ ],
136
+ uncoveredRanges: [],
137
+ };
138
+ }
139
+ let sourceFile;
140
+ try {
141
+ sourceFile = getProject().createSourceFile(input.path, input.content, {
142
+ overwrite: true,
143
+ scriptKind,
144
+ });
145
+ const analyzed = sourceFile;
146
+ const diagnostics = analyzed
147
+ .getProject()
148
+ .getProgram()
149
+ .getSyntacticDiagnostics(analyzed)
150
+ .map((diagnostic) => {
151
+ const start = diagnostic.getStart();
152
+ const length = diagnostic.getLength();
153
+ const range = start === undefined
154
+ ? wholeSourceRange(analyzed, input.content)
155
+ : sourceRangeAtOffsets(analyzed, input.content, start, start + (length ?? 0));
156
+ const message = diagnostic.getMessageText();
157
+ return {
158
+ code: `TS${diagnostic.getCode()}`,
159
+ message: typeof message === "string"
160
+ ? message
161
+ : message.getMessageText(),
162
+ range,
163
+ };
164
+ });
165
+ const result = {
166
+ contractVersion: "kibi.symbol-extractor.v2",
167
+ status: diagnostics.length > 0 ? "partial" : "ok",
168
+ sourceFile: input.path,
169
+ language,
170
+ module,
171
+ symbols: collectSourceSymbols(analyzed, true),
172
+ diagnostics,
173
+ uncoveredRanges: diagnostics.map(({ range, code }) => ({
174
+ ...range,
175
+ reason: `The TypeScript parser reported ${code} at this source span.`,
176
+ })),
177
+ };
178
+ return result;
179
+ }
180
+ catch (error) {
181
+ return {
182
+ contractVersion: "kibi.symbol-extractor.v2",
183
+ status: "failed",
184
+ sourceFile: input.path,
185
+ language,
186
+ module,
187
+ symbols: [],
188
+ diagnostics: [
189
+ {
190
+ code: "SOURCE_ANALYSIS_FAILED",
191
+ message: error instanceof Error
192
+ ? error.message
193
+ : "The built-in ts-morph extractor failed",
194
+ },
195
+ ],
196
+ uncoveredRanges: [],
197
+ };
198
+ }
199
+ finally {
200
+ if (sourceFile)
201
+ getProject().removeSourceFile(sourceFile);
202
+ }
56
203
  },
57
204
  };
58
205
  }
206
+ function sourceRangeAtOffsets(sourceFile, content, startOffset, endOffset) {
207
+ const boundedStart = Math.min(Math.max(startOffset, 0), content.length);
208
+ const boundedEnd = Math.min(Math.max(endOffset, boundedStart), content.length);
209
+ const start = sourceFile.getLineAndColumnAtPos(boundedStart);
210
+ const end = sourceFile.getLineAndColumnAtPos(boundedEnd);
211
+ return {
212
+ startLine: start.line,
213
+ startColumn: Math.max(0, start.column - 1),
214
+ endLine: end.line,
215
+ endColumn: Math.max(0, end.column - 1),
216
+ };
217
+ }
218
+ function wholeSourceRange(sourceFile, content) {
219
+ return sourceRangeAtOffsets(sourceFile, content, 0, content.length);
220
+ }
59
221
  // implements REQ-capability-plugin-builtin-parity-v1
60
222
  export function createBuiltinTsMorphSourceAnalysisProvider() {
61
223
  return toSourceAnalysisProvider(createBuiltinTsMorphSymbolExtractor());
62
224
  }
63
- function collectSourceSymbols(sourceFile) {
225
+ function collectSourceSymbols(sourceFile, includeV2Fields = false) {
64
226
  const symbols = [];
65
227
  for (const decl of sourceFile.getFunctions()) {
66
228
  if (!decl.isExported())
67
229
  continue;
68
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "function", decl.getNameNode() ?? decl, decl,
230
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "function", decl.getNameNode() ?? decl, decl,
69
231
  // Functions: leading trivia via getFullText + JSDoc (historical CLI).
70
- fullTextWithJsDocs(decl)));
232
+ fullTextWithJsDocs(decl), includeV2Fields));
71
233
  }
72
234
  for (const decl of sourceFile.getClasses()) {
73
235
  if (!decl.isExported())
74
236
  continue;
75
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "class", decl.getNameNode() ?? decl, decl,
237
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "class", decl.getNameNode() ?? decl, decl,
76
238
  // Leading comments + JSDoc only — never the class body (method
77
239
  // `// implements` must stay on the method symbol).
78
- leadingCommentsAndJsDocs(sourceFile, decl)));
240
+ leadingCommentsAndJsDocs(sourceFile, decl), includeV2Fields));
79
241
  try {
80
- appendClassMembers(sourceFile, decl, decl.getName(), symbols);
242
+ appendClassMembers(sourceFile, decl, decl.getName(), symbols, includeV2Fields);
81
243
  }
82
244
  catch {
83
245
  // Skip malformed class member walks; keep the class symbol.
@@ -86,42 +248,44 @@ function collectSourceSymbols(sourceFile) {
86
248
  for (const decl of sourceFile.getInterfaces()) {
87
249
  if (!decl.isExported())
88
250
  continue;
89
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "interface", decl.getNameNode() ?? decl, decl,
251
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "interface", decl.getNameNode() ?? decl, decl,
90
252
  // Historical CLI used getText(); keep calling it so characterization
91
253
  // mocks that throw from getText still isolate the failure.
92
- `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`));
254
+ `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`, includeV2Fields));
93
255
  }
94
256
  for (const decl of sourceFile.getTypeAliases()) {
95
257
  if (!decl.isExported())
96
258
  continue;
97
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "type", decl.getNameNode() ?? decl, decl, `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`));
259
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "type", decl.getNameNode() ?? decl, decl, `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`, includeV2Fields));
98
260
  }
99
261
  for (const decl of sourceFile.getEnums()) {
100
262
  if (!decl.isExported())
101
263
  continue;
102
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "enum", decl.getNameNode() ?? decl, decl,
264
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, decl.getName() ?? "<anonymous>", "enum", decl.getNameNode() ?? decl, decl,
103
265
  // Enums historically used getText(); prefer leading comments when the
104
266
  // real AST is available, else getText for test doubles.
105
- `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`));
267
+ `${leadingCommentsAndJsDocs(sourceFile, decl)}\n${safeGetText(decl)}`, includeV2Fields));
106
268
  }
107
269
  for (const statement of sourceFile.getVariableStatements()) {
108
270
  if (!statement.isExported())
109
271
  continue;
110
272
  for (const declaration of statement.getDeclarations()) {
111
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, declaration.getName(), "variable", declaration.getNameNode() ?? declaration, declaration,
273
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, declaration.getName(), "variable", declaration.getNameNode() ?? declaration, declaration,
112
274
  // Statement carries leading `// implements`; declaration text is the
113
275
  // historical fallback when only getText is stubbed in tests.
114
- `${safeFullText(statement)}\n${safeGetText(declaration)}`));
276
+ `${safeFullText(statement)}\n${safeGetText(declaration)}`, includeV2Fields));
115
277
  try {
116
278
  const classExpression = typeof declaration.getInitializerIfKind === "function"
117
279
  ? declaration.getInitializerIfKind(tsMorph().SyntaxKind.ClassExpression)
118
280
  : undefined;
119
281
  if (classExpression) {
120
- appendClassMembers(sourceFile, classExpression, declaration.getName(), symbols);
282
+ appendClassMembers(sourceFile, classExpression, declaration.getName(), symbols, includeV2Fields);
121
283
  }
122
284
  }
123
- catch {
124
- // Skip malformed class-expression members; keep the variable symbol.
285
+ catch (error) {
286
+ if (includeV2Fields)
287
+ throw error;
288
+ // Preserve legacy v1 class-expression recovery.
125
289
  }
126
290
  }
127
291
  }
@@ -164,7 +328,7 @@ function safeFullText(node) {
164
328
  function safeGetText(node) {
165
329
  return typeof node.getText === "function" ? node.getText() : "";
166
330
  }
167
- function appendClassMembers(sourceFile, declaration, className, symbols) {
331
+ function appendClassMembers(sourceFile, declaration, className, symbols, includeV2Fields) {
168
332
  // Inherit class *leading* ownership comments onto members (not the class
169
333
  // body). Method-local `// implements` stay on the method via getFullText.
170
334
  const classLeading = leadingCommentsAndJsDocs(sourceFile, declaration);
@@ -174,7 +338,7 @@ function appendClassMembers(sourceFile, declaration, className, symbols) {
174
338
  for (const method of methods) {
175
339
  if (isPrivateClassMember(method))
176
340
  continue;
177
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, method.getName()), "method", method.getNameNode() ?? method, method, `${classLeading}\n${fullTextWithJsDocs(method)}`));
341
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, method.getName()), "method", method.getNameNode() ?? method, method, `${classLeading}\n${fullTextWithJsDocs(method)}`, includeV2Fields, className));
178
342
  }
179
343
  const properties = typeof declaration.getProperties === "function"
180
344
  ? declaration.getProperties()
@@ -182,7 +346,7 @@ function appendClassMembers(sourceFile, declaration, className, symbols) {
182
346
  for (const property of properties) {
183
347
  if (isPrivateClassMember(property))
184
348
  continue;
185
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, property.getName()), "property", property.getNameNode() ?? property, property, `${classLeading}\n${fullTextWithJsDocs(property)}`));
349
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, property.getName()), "property", property.getNameNode() ?? property, property, `${classLeading}\n${fullTextWithJsDocs(property)}`, includeV2Fields, className));
186
350
  }
187
351
  const accessors = [
188
352
  ...(typeof declaration.getGetAccessors === "function"
@@ -192,24 +356,47 @@ function appendClassMembers(sourceFile, declaration, className, symbols) {
192
356
  ? declaration.getSetAccessors()
193
357
  : []),
194
358
  ];
359
+ const pairedAccessors = new Set();
195
360
  for (const accessor of accessors) {
196
361
  if (isPrivateClassMember(accessor))
197
362
  continue;
198
- pushSymbol(symbols, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, accessor.getName()), "accessor", accessor.getNameNode() ?? accessor, accessor, `${classLeading}\n${fullTextWithJsDocs(accessor)}`));
363
+ if (pairedAccessors.has(accessor))
364
+ continue;
365
+ if (includeV2Fields) {
366
+ const candidates = accessors.filter((candidate) => candidate.getName() === accessor.getName() &&
367
+ candidate.isStatic() === accessor.isStatic());
368
+ const other = candidates.find((candidate) => candidate !== accessor);
369
+ if (candidates.length === 2 &&
370
+ other &&
371
+ other.getKind() !== accessor.getKind()) {
372
+ const first = accessor.getStart() < other.getStart() ? accessor : other;
373
+ const last = first === accessor ? other : accessor;
374
+ // A complementary getter/setter pair is one authored property. Keep
375
+ // both bodies in its span without assigning the same ID twice.
376
+ const symbol = toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, accessor.getName()), "accessor", first.getNameNode() ?? first, last, `${classLeading}\n${fullTextWithJsDocs(first)}\n${fullTextWithJsDocs(last)}`, true, className);
377
+ symbols.push({ ...symbol, nativeKind: "AccessorPair" });
378
+ pairedAccessors.add(accessor);
379
+ pairedAccessors.add(other);
380
+ continue;
381
+ }
382
+ }
383
+ pushSymbol(symbols, includeV2Fields, () => toSourceSymbolAnalysis(sourceFile, formatMethodSymbolName(className, accessor.getName()), "accessor", accessor.getNameNode() ?? accessor, accessor, `${classLeading}\n${fullTextWithJsDocs(accessor)}`, includeV2Fields, className));
199
384
  }
200
385
  }
201
- function pushSymbol(symbols, build) {
386
+ function pushSymbol(symbols, strict, build) {
202
387
  try {
203
388
  symbols.push(build());
204
389
  }
205
- catch {
206
- // Skip malformed declarations; keep sibling symbols.
390
+ catch (error) {
391
+ if (strict)
392
+ throw error;
393
+ // Preserve legacy v1 recovery; v2 must expose extraction failure.
207
394
  }
208
395
  }
209
- function toSourceSymbolAnalysis(sourceFile, name, kind, startNode, endNode, directiveText) {
396
+ function toSourceSymbolAnalysis(sourceFile, name, kind, startNode, endNode, directiveText, includeV2Fields = false, containerName) {
210
397
  const start = sourceFile.getLineAndColumnAtPos(startNode.getStart());
211
398
  const end = sourceFile.getLineAndColumnAtPos(endNode.getEnd());
212
- return {
399
+ const result = {
213
400
  name,
214
401
  kind,
215
402
  startLine: start.line,
@@ -218,6 +405,35 @@ function toSourceSymbolAnalysis(sourceFile, name, kind, startNode, endNode, dire
218
405
  endColumn: Math.max(0, end.column - 1),
219
406
  directiveText,
220
407
  };
408
+ if (!includeV2Fields)
409
+ return result;
410
+ const nameKind = startNode.getKindName();
411
+ const hasNameNode = [
412
+ "Identifier",
413
+ "PrivateIdentifier",
414
+ "StringLiteral",
415
+ "NumericLiteral",
416
+ "NoSubstitutionTemplateLiteral",
417
+ "ComputedPropertyName",
418
+ ].includes(nameKind);
419
+ const nameRangeStart = sourceFile.getLineAndColumnAtPos(startNode.getStart());
420
+ const nameRangeEnd = sourceFile.getLineAndColumnAtPos(startNode.getEnd());
421
+ return {
422
+ ...result,
423
+ qualifiedName: name,
424
+ ...(containerName ? { containerName } : {}),
425
+ nativeKind: endNode.getKindName(),
426
+ ...(hasNameNode
427
+ ? {
428
+ nameRange: {
429
+ startLine: nameRangeStart.line,
430
+ startColumn: Math.max(0, nameRangeStart.column - 1),
431
+ endLine: nameRangeEnd.line,
432
+ endColumn: Math.max(0, nameRangeEnd.column - 1),
433
+ },
434
+ }
435
+ : {}),
436
+ };
221
437
  }
222
438
  function inferSourceLanguage(filePath) {
223
439
  const extension = path.extname(filePath).toLowerCase();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kibi-plugin-builtin",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Default Kibi capability plugins: TS symbol extraction, ontology pack, and semantic classifier",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -12,7 +12,10 @@
12
12
  },
13
13
  "./package.json": "./package.json"
14
14
  },
15
- "files": ["dist", "CHANGELOG.md"],
15
+ "files": [
16
+ "dist",
17
+ "CHANGELOG.md"
18
+ ],
16
19
  "scripts": {
17
20
  "build": "tsc -p tsconfig.json",
18
21
  "typecheck": "tsc -p tsconfig.json --noEmit",
@@ -32,7 +35,7 @@
32
35
  "access": "public"
33
36
  },
34
37
  "dependencies": {
35
- "kibi-plugin-sdk": "^0.3.0",
38
+ "kibi-plugin-sdk": "^0.4.0",
36
39
  "ts-morph": "^23.0.0"
37
40
  },
38
41
  "devDependencies": {