@remnic/core 9.3.671 → 9.3.673
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/dist/access-audit.js +2 -2
- package/dist/access-cli.js +27 -25
- package/dist/access-cli.js.map +1 -1
- package/dist/access-http.js +12 -12
- package/dist/access-mcp.js +11 -11
- package/dist/access-schema.d.ts +36 -36
- package/dist/access-schema.js +3 -3
- package/dist/access-service.js +9 -9
- package/dist/active-recall.js +3 -1
- package/dist/active-recall.js.map +1 -1
- package/dist/chunk-3BQOQYRB.js +33 -0
- package/dist/chunk-3BQOQYRB.js.map +1 -0
- package/dist/{chunk-UOBLE67F.js → chunk-3IE22DJ2.js} +4 -4
- package/dist/chunk-52LZ42LI.js +25 -0
- package/dist/chunk-52LZ42LI.js.map +1 -0
- package/dist/{chunk-CRO4LCQ6.js → chunk-7OGJQP7T.js} +5 -5
- package/dist/{chunk-23EBQ27U.js → chunk-B55KFEGS.js} +2 -2
- package/dist/{chunk-BTLNC5YM.js → chunk-GNAMDNGT.js} +5 -13
- package/dist/chunk-GNAMDNGT.js.map +1 -0
- package/dist/{chunk-KQAFEZQX.js → chunk-IPLYGWQF.js} +5 -5
- package/dist/{chunk-MLVMBV2C.js → chunk-IUZWBCJX.js} +8 -40
- package/dist/chunk-IUZWBCJX.js.map +1 -0
- package/dist/{chunk-PYTATYUV.js → chunk-ODWI5XU2.js} +2 -2
- package/dist/{chunk-F7OWUP3G.js → chunk-OG7A6AZX.js} +4 -4
- package/dist/{chunk-XS2CWEHZ.js → chunk-Q6MIDQEL.js} +2 -2
- package/dist/{chunk-UDJLF3BO.js → chunk-QLRYXOAD.js} +2 -2
- package/dist/chunk-QO3AILZN.js +89 -0
- package/dist/chunk-QO3AILZN.js.map +1 -0
- package/dist/{chunk-ZI6A7X4V.js → chunk-R37A3BEW.js} +26 -26
- package/dist/{chunk-CPVV2UEL.js → chunk-SDLJ2W7S.js} +6 -6
- package/dist/{chunk-Z4GALEO3.js → chunk-SF45RQDX.js} +3 -3
- package/dist/{chunk-LXVOZ2O6.js → chunk-T2AOOHDA.js} +2 -2
- package/dist/{chunk-7K5Q6COX.js → chunk-TVVEYCNW.js} +4 -4
- package/dist/{chunk-32RD3GIW.js → chunk-XVVEKF5I.js} +19 -19
- package/dist/{chunk-TGN4M5MB.js → chunk-ZLINDOBG.js} +4 -4
- package/dist/cli.js +24 -22
- package/dist/coding/optional-coding-graph.d.ts +63 -0
- package/dist/coding/optional-coding-graph.js +119 -0
- package/dist/coding/optional-coding-graph.js.map +1 -0
- package/dist/coding-graph-types-Dd2tGrnm.d.ts +106 -0
- package/dist/config.d.ts +1 -1
- package/dist/config.js +3 -1
- package/dist/connectors/index.d.ts +6 -2
- package/dist/connectors/index.js +6 -2
- package/dist/conversation-index/backend.js +2 -2
- package/dist/emit-legacy-tools.d.ts +61 -0
- package/dist/emit-legacy-tools.js +12 -0
- package/dist/emit-legacy-tools.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +54 -46
- package/dist/index.js.map +1 -1
- package/dist/lcm/engine.js +2 -2
- package/dist/lcm/index.js +2 -2
- package/dist/namespaces/migrate.js +5 -5
- package/dist/namespaces/search.js +4 -4
- package/dist/operator-toolkit.js +10 -8
- package/dist/orchestrator.js +16 -16
- package/dist/resume-bundles.js +4 -2
- package/dist/schemas.d.ts +42 -42
- package/dist/search/factory.js +3 -3
- package/dist/search/index.js +3 -3
- package/dist/shared-context/manager.d.ts +2 -2
- package/dist/transfer/autodetect.js +1 -1
- package/dist/transfer/backup.js +1 -1
- package/dist/transfer/capsule-export.js +2 -2
- package/package.json +19 -1
- package/src/coding/coding-graph-types.ts +180 -0
- package/src/coding/optional-coding-graph-cache.test.ts +86 -0
- package/src/coding/optional-coding-graph-cacheread.test.ts +78 -0
- package/src/coding/optional-coding-graph-concurrent.test.ts +48 -0
- package/src/coding/optional-coding-graph-incompatible.test.ts +98 -0
- package/src/coding/optional-coding-graph-probe.test.ts +34 -0
- package/src/coding/optional-coding-graph.test.ts +117 -0
- package/src/coding/optional-coding-graph.ts +370 -0
- package/src/config.test.ts +408 -6
- package/src/config.ts +12 -56
- package/src/connectors/index.ts +2 -15
- package/src/connectors/paths.ts +50 -0
- package/src/emit-legacy-tools.test.ts +297 -0
- package/src/emit-legacy-tools.ts +204 -0
- package/src/index.ts +22 -0
- package/dist/chunk-BTLNC5YM.js.map +0 -1
- package/dist/chunk-MLVMBV2C.js.map +0 -1
- /package/dist/{chunk-UOBLE67F.js.map → chunk-3IE22DJ2.js.map} +0 -0
- /package/dist/{chunk-CRO4LCQ6.js.map → chunk-7OGJQP7T.js.map} +0 -0
- /package/dist/{chunk-23EBQ27U.js.map → chunk-B55KFEGS.js.map} +0 -0
- /package/dist/{chunk-KQAFEZQX.js.map → chunk-IPLYGWQF.js.map} +0 -0
- /package/dist/{chunk-PYTATYUV.js.map → chunk-ODWI5XU2.js.map} +0 -0
- /package/dist/{chunk-F7OWUP3G.js.map → chunk-OG7A6AZX.js.map} +0 -0
- /package/dist/{chunk-XS2CWEHZ.js.map → chunk-Q6MIDQEL.js.map} +0 -0
- /package/dist/{chunk-UDJLF3BO.js.map → chunk-QLRYXOAD.js.map} +0 -0
- /package/dist/{chunk-ZI6A7X4V.js.map → chunk-R37A3BEW.js.map} +0 -0
- /package/dist/{chunk-CPVV2UEL.js.map → chunk-SDLJ2W7S.js.map} +0 -0
- /package/dist/{chunk-Z4GALEO3.js.map → chunk-SF45RQDX.js.map} +0 -0
- /package/dist/{chunk-LXVOZ2O6.js.map → chunk-T2AOOHDA.js.map} +0 -0
- /package/dist/{chunk-7K5Q6COX.js.map → chunk-TVVEYCNW.js.map} +0 -0
- /package/dist/{chunk-32RD3GIW.js.map → chunk-XVVEKF5I.js.map} +0 -0
- /package/dist/{chunk-TGN4M5MB.js.map → chunk-ZLINDOBG.js.map} +0 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Tests for the optional @remnic/coding-graph loader.
|
|
2
|
+
//
|
|
3
|
+
// Prove-fail-before (CLAUDE.md rule 6 / issue #1551): the loader throws a
|
|
4
|
+
// user-facing install hint that names the exact `npm install
|
|
5
|
+
// @remnic/coding-graph` command when the optional package is absent. We
|
|
6
|
+
// verify the hint surface via the exported `buildInstallHint` helper so
|
|
7
|
+
// the test exercises the same code path as the live loader without
|
|
8
|
+
// depending on whether the package is currently installed.
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
buildCodingGraphInstallHint,
|
|
15
|
+
isSpecifierNotFoundErrorForCodingGraph,
|
|
16
|
+
loadCodingGraphEngineFactory,
|
|
17
|
+
tryLoadCodingGraphModule,
|
|
18
|
+
isCodingGraphInstalled,
|
|
19
|
+
} from "./optional-coding-graph.js";
|
|
20
|
+
|
|
21
|
+
const CODING_GRAPH_SPECIFIER = "@remnic/coding-graph";
|
|
22
|
+
|
|
23
|
+
test("buildCodingGraphInstallHint names the exact npm install command and package name", () => {
|
|
24
|
+
const hint = buildCodingGraphInstallHint();
|
|
25
|
+
assert.match(
|
|
26
|
+
hint,
|
|
27
|
+
/@remnic\/coding-graph/,
|
|
28
|
+
"hint must reference the package name",
|
|
29
|
+
);
|
|
30
|
+
assert.match(
|
|
31
|
+
hint,
|
|
32
|
+
/npm install @remnic\/coding-graph/,
|
|
33
|
+
"hint must contain the exact install command for npm",
|
|
34
|
+
);
|
|
35
|
+
// pnpm and yarn install variants must also be present so users on any
|
|
36
|
+
// package manager see a working command on first glance.
|
|
37
|
+
assert.match(hint, /pnpm add @remnic\/coding-graph/);
|
|
38
|
+
assert.match(hint, /yarn add @remnic\/coding-graph/);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("isSpecifierNotFoundErrorForCodingGraph rejects ERR_MODULE_NOT_FOUND for unrelated specifiers", () => {
|
|
42
|
+
// A broken *transitive* dependency inside the optional package must NOT
|
|
43
|
+
// be reported as "package missing" — that's a different failure mode.
|
|
44
|
+
const transitive = Object.assign(
|
|
45
|
+
new Error("Cannot find package 'web-tree-sitter'"),
|
|
46
|
+
{ code: "ERR_MODULE_NOT_FOUND" },
|
|
47
|
+
);
|
|
48
|
+
assert.equal(
|
|
49
|
+
isSpecifierNotFoundErrorForCodingGraph(transitive),
|
|
50
|
+
false,
|
|
51
|
+
"transitive misses must not match the install-hint branch",
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("isSpecifierNotFoundErrorForCodingGraph accepts ERR_MODULE_NOT_FOUND that names @remnic/coding-graph", () => {
|
|
56
|
+
const direct = Object.assign(
|
|
57
|
+
new Error(`Cannot find package '${CODING_GRAPH_SPECIFIER}'`),
|
|
58
|
+
{ code: "ERR_MODULE_NOT_FOUND" },
|
|
59
|
+
);
|
|
60
|
+
assert.equal(
|
|
61
|
+
isSpecifierNotFoundErrorForCodingGraph(direct),
|
|
62
|
+
true,
|
|
63
|
+
"the loader's miss branch must trigger for missing top-level package",
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("loadCodingGraphEngineFactory returns a factory when @remnic/coding-graph is installed, throws hint when absent", async () => {
|
|
68
|
+
// The loader throws only when the package is *missing*. In the workspace
|
|
69
|
+
// today the package is installed (sibling workspace link), so we expect
|
|
70
|
+
// a factory back. The throw-path contract is verified by
|
|
71
|
+
// `isSpecifierNotFoundErrorForCodingGraph` + `buildCodingGraphInstallHint`
|
|
72
|
+
// above; together they prove both halves of the loader's behavior.
|
|
73
|
+
try {
|
|
74
|
+
const factory = await loadCodingGraphEngineFactory();
|
|
75
|
+
assert.equal(typeof factory, "function", "factory must be callable");
|
|
76
|
+
// The PR1 placeholder factory is required to throw a tagged
|
|
77
|
+
// CodingGraphError("not_implemented") — never return a fake engine.
|
|
78
|
+
assert.throws(
|
|
79
|
+
() => factory(),
|
|
80
|
+
(err: unknown) => {
|
|
81
|
+
if (!err || typeof err !== "object") return false;
|
|
82
|
+
const e = err as { name?: unknown; code?: unknown; message?: unknown };
|
|
83
|
+
return (
|
|
84
|
+
e.name === "CodingGraphError" &&
|
|
85
|
+
e.code === "not_implemented" &&
|
|
86
|
+
typeof e.message === "string"
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
"placeholder engine must throw CodingGraphError('not_implemented'), not return a fake engine",
|
|
90
|
+
);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
// If the loader throws (e.g. base install without the package), the
|
|
93
|
+
// message must be the install hint.
|
|
94
|
+
if (err instanceof Error) {
|
|
95
|
+
assert.match(err.message, /@remnic\/coding-graph/);
|
|
96
|
+
assert.match(err.message, /npm install @remnic\/coding-graph/);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("tryLoadCodingGraphModule and isCodingGraphInstalled never throw and return module/null", async () => {
|
|
104
|
+
const mod = await tryLoadCodingGraphModule();
|
|
105
|
+
// Either installed (workspace dev) or absent (base install). Both are
|
|
106
|
+
// valid; the invariant is "no throw + correct return type".
|
|
107
|
+
if (mod === null) {
|
|
108
|
+
assert.equal(mod, null);
|
|
109
|
+
} else {
|
|
110
|
+
assert.equal(typeof mod.createCodingGraphEngine, "function");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const installed = await isCodingGraphInstalled();
|
|
114
|
+
assert.equal(typeof installed, "boolean");
|
|
115
|
+
// Tautology with the call above — installed must match presence in mod.
|
|
116
|
+
assert.equal(installed, mod !== null);
|
|
117
|
+
});
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// Lazy loader for the optional @remnic/coding-graph package.
|
|
2
|
+
//
|
|
3
|
+
// Remnic's core is installed à-la-carte: users who only need memory
|
|
4
|
+
// features should not have to install codebase-graph tooling, so
|
|
5
|
+
// @remnic/coding-graph is an optional peer dependency, not a bundled
|
|
6
|
+
// dependency. Any code path that actually needs the graph engine calls
|
|
7
|
+
// loadCodingGraphEngineFactory() or one of the try* helpers; the loader
|
|
8
|
+
// either returns the engine factory or throws a user-facing install hint.
|
|
9
|
+
//
|
|
10
|
+
// Justification for the dynamic import:
|
|
11
|
+
// CLAUDE.md rule 57 / AGENTS.md rule 44 require à-la-carte optional
|
|
12
|
+
// workspace packages to be loaded via a *computed-specifier* dynamic
|
|
13
|
+
// import. Static `import "@remnic/coding-graph"` would either fail the
|
|
14
|
+
// base install when the optional package is absent or — worse — force
|
|
15
|
+
// the bundler to bundle it into @remnic/core's dist. Concat'ing the
|
|
16
|
+
// specifier from string literals keeps the dynamic import a runtime
|
|
17
|
+
// call. The same pattern is canonical in
|
|
18
|
+
// packages/remnic-cli/src/optional-bench.ts and
|
|
19
|
+
// packages/remnic-cli/src/optional-module-loader.ts; we mirror it here
|
|
20
|
+
// because core, not the CLI, owns the engine entry point.
|
|
21
|
+
//
|
|
22
|
+
// Type-source direction:
|
|
23
|
+
// The CodingGraphEngine interface and supporting IR types live in
|
|
24
|
+
// packages/remnic-core/src/coding/coding-graph-types.ts (a local
|
|
25
|
+
// types-only module). They are owned by core; @remnic/coding-graph
|
|
26
|
+
// imports them and implements against them. This breaks the type
|
|
27
|
+
// cycle that would otherwise require core to resolve
|
|
28
|
+
// @remnic/coding-graph at compile time. Core's tsup DTS phase emits
|
|
29
|
+
// declarations against the package's compiled output — this would
|
|
30
|
+
// fail in CI's fresh base install where the optional peer is not
|
|
31
|
+
// symlinked. With the types owned locally, the loader types its
|
|
32
|
+
// dynamic-import result through the local shape (validated at runtime
|
|
33
|
+
// via a structural check, not via TS) so the base install compiles.
|
|
34
|
+
//
|
|
35
|
+
// Failure-mode policy:
|
|
36
|
+
// - Missing optional package → install hint (loadCodingGraphEngineFactory
|
|
37
|
+
// throws the canonical user-facing message).
|
|
38
|
+
// - Present-but-mismatched install (the optional package resolves to a
|
|
39
|
+
// module that does not satisfy the structural contract) →
|
|
40
|
+
// `module_load_failed` Error on the loadFactory path; probe paths
|
|
41
|
+
// return null with no throw (graceful degradation). User sees a
|
|
42
|
+
// real diagnostic telling them the install is broken — NOT the
|
|
43
|
+
// "npm install @remnic/coding-graph" install hint, which would
|
|
44
|
+
// mislead users into re-installing a package that is already there.
|
|
45
|
+
// - Present-but-broken install (transitive dep missing, loader
|
|
46
|
+
// fault, etc.) → underlying import error is rethrown on the
|
|
47
|
+
// loadFactory path; probe paths swallow and return null.
|
|
48
|
+
// - Three separate cache slots, kept strictly disjoint:
|
|
49
|
+
// (a) `cachedLoadResult` — successful-module cache, read by both
|
|
50
|
+
// loadFactory and tryLoad. Never populated on missing or
|
|
51
|
+
// broken installs (preserves the "users see the real
|
|
52
|
+
// diagnostic" invariant).
|
|
53
|
+
// (b) `inFlightProbe` — in-flight promise slot shared by concurrent
|
|
54
|
+
// probe callers; cleared once settled.
|
|
55
|
+
// (c) (No "absent result" cache.) loadFactory ALWAYS attempts a
|
|
56
|
+
// fresh import on a fresh call — never serves a stale
|
|
57
|
+
// decision from a previous attempt.
|
|
58
|
+
|
|
59
|
+
import {
|
|
60
|
+
CODING_GRAPH_ENGINE_VERSION,
|
|
61
|
+
TIER_1_LANGUAGES,
|
|
62
|
+
type CodingGraphEngine,
|
|
63
|
+
type CodingGraphErrorCode,
|
|
64
|
+
type CodingGraphLanguage,
|
|
65
|
+
type CreateCodingGraphEngineOptions,
|
|
66
|
+
type FileIR,
|
|
67
|
+
type ParseFileInput,
|
|
68
|
+
type ParseResult,
|
|
69
|
+
type SymbolIR,
|
|
70
|
+
} from "./coding-graph-types.js";
|
|
71
|
+
|
|
72
|
+
const SPECIFIER = "@remnic/" + "coding-graph";
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Structural minimal shape of `@remnic/coding-graph`'s public surface,
|
|
76
|
+
* as seen by core after a successful dynamic import. We narrow to
|
|
77
|
+
* `unknown` first (since the dynamic import returns `any` at runtime)
|
|
78
|
+
* and then to this interface only after the runtime check below.
|
|
79
|
+
*/
|
|
80
|
+
interface LoadedCodingGraphModule {
|
|
81
|
+
ENGINE_VERSION: string;
|
|
82
|
+
TIER_1_LANGUAGES: readonly CodingGraphLanguage[];
|
|
83
|
+
CodingGraphError: new (
|
|
84
|
+
code: CodingGraphErrorCode,
|
|
85
|
+
message: string,
|
|
86
|
+
engineVersion?: string,
|
|
87
|
+
) => Error;
|
|
88
|
+
createCodingGraphEngine: (
|
|
89
|
+
options?: CreateCodingGraphEngineOptions,
|
|
90
|
+
) => CodingGraphEngine;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Outcome of a single import attempt. The probe path collapses
|
|
95
|
+
* "missing" and "incompatible" into `null` (graceful degradation);
|
|
96
|
+
* the loader path distinguishes the two so users get an accurate
|
|
97
|
+
* diagnostic instead of a misleading install hint on a broken install.
|
|
98
|
+
*/
|
|
99
|
+
type ImportAttemptOutcome =
|
|
100
|
+
| { readonly kind: "ok"; readonly module: LoadedCodingGraphModule }
|
|
101
|
+
| { readonly kind: "missing" }
|
|
102
|
+
| { readonly kind: "incompatible"; readonly actual: unknown }
|
|
103
|
+
| { readonly kind: "broken"; readonly cause: unknown };
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Build the user-facing install hint message. Exported so tests can assert
|
|
107
|
+
* on the exact text (CLAUDE.md rule 51 / issue #1551 prove-fail-before).
|
|
108
|
+
*/
|
|
109
|
+
export function buildCodingGraphInstallHint(): string {
|
|
110
|
+
return (
|
|
111
|
+
"The `@remnic/coding-graph` engine is optional and not installed in this environment.\n" +
|
|
112
|
+
"\n" +
|
|
113
|
+
"Install it alongside @remnic/core to enable codebase-graph features:\n" +
|
|
114
|
+
" npm install @remnic/coding-graph\n" +
|
|
115
|
+
"\n" +
|
|
116
|
+
"Or add it to a project (pnpm / yarn):\n" +
|
|
117
|
+
" pnpm add @remnic/coding-graph\n" +
|
|
118
|
+
" yarn add @remnic/coding-graph\n"
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function notInstalledError(): Error {
|
|
123
|
+
return new Error(buildCodingGraphInstallHint());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Build the present-but-incompatible diagnostic. The user has the
|
|
128
|
+
* package installed but it does not satisfy the structural contract;
|
|
129
|
+
* "npm install @remnic/coding-graph" would NOT help them (the package
|
|
130
|
+
* is already there). Tell them to reinstall or open an issue.
|
|
131
|
+
*/
|
|
132
|
+
function incompatibleModuleError(actual: unknown): Error {
|
|
133
|
+
const keys =
|
|
134
|
+
actual && typeof actual === "object"
|
|
135
|
+
? Object.keys(actual).slice(0, 8).join(", ") || "(none)"
|
|
136
|
+
: typeof actual;
|
|
137
|
+
return new Error(
|
|
138
|
+
"`@remnic/coding-graph` is present but its module shape does not match " +
|
|
139
|
+
"the expected contract. The installed build may be stale, broken, or " +
|
|
140
|
+
"from an incompatible version. Try reinstalling it " +
|
|
141
|
+
"(`npm install --force @remnic/coding-graph`) or pinning to a known " +
|
|
142
|
+
`working version. Detected exports: ${keys}.`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Return true when `err` is a module-not-found failure for exactly the
|
|
148
|
+
* `@remnic/coding-graph` specifier. Same boundary-aware regex as
|
|
149
|
+
* packages/remnic-cli/src/optional-module-loader.ts so transitive
|
|
150
|
+
* misses (a broken @remnic/coding-graph release) bubble up rather than
|
|
151
|
+
* being mis-reported as "run npm install".
|
|
152
|
+
*
|
|
153
|
+
* Exported for tests; the production loaders call the internal alias
|
|
154
|
+
* below.
|
|
155
|
+
*/
|
|
156
|
+
export function isSpecifierNotFoundErrorForCodingGraph(
|
|
157
|
+
err: unknown,
|
|
158
|
+
specifier: string = SPECIFIER,
|
|
159
|
+
): boolean {
|
|
160
|
+
if (!err || typeof err !== "object") {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const code = (err as { code?: unknown }).code;
|
|
164
|
+
if (code !== "ERR_MODULE_NOT_FOUND" && code !== "MODULE_NOT_FOUND") {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const message = (err as { message?: unknown }).message;
|
|
168
|
+
if (typeof message !== "string") {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (message.includes(`'${specifier}'`)) return true;
|
|
172
|
+
if (message.includes(`"${specifier}"`)) return true;
|
|
173
|
+
// Boundary guard so "@remnic/coding-graph" does not match
|
|
174
|
+
// "@remnic/coding-graph-foo".
|
|
175
|
+
const escaped = specifier.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
176
|
+
const boundaryRegex = new RegExp(
|
|
177
|
+
`(?:^|[\\s"'\`\\(])${escaped}(?:[\\s"'\`\\)]|$)`,
|
|
178
|
+
);
|
|
179
|
+
return boundaryRegex.test(message);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Narrow a dynamic import result to the local LoadedCodingGraphModule
|
|
184
|
+
* shape via a runtime structural check (never inline-as casting).
|
|
185
|
+
* The cast-through-unknown double-step satisfies the rule that
|
|
186
|
+
* "unchecked casts" must be justified; here the check is the runtime
|
|
187
|
+
* predicate below.
|
|
188
|
+
*/
|
|
189
|
+
function isLoadedCodingGraphModule(value: unknown): value is LoadedCodingGraphModule {
|
|
190
|
+
if (!value || typeof value !== "object") return false;
|
|
191
|
+
const candidate = value as Record<string, unknown>;
|
|
192
|
+
return (
|
|
193
|
+
typeof candidate.createCodingGraphEngine === "function" &&
|
|
194
|
+
typeof candidate.CodingGraphError === "function" &&
|
|
195
|
+
typeof candidate.ENGINE_VERSION === "string" &&
|
|
196
|
+
Array.isArray(candidate.TIER_1_LANGUAGES)
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Single import attempt. Returns a tagged-outcome object so callers
|
|
202
|
+
* can distinguish "not installed" from "incompatible build" from
|
|
203
|
+
* "broken transitive dep" — each gets its own diagnostic on the
|
|
204
|
+
* user-facing path.
|
|
205
|
+
*
|
|
206
|
+
* Never reads from any cache; the calling function decides whether
|
|
207
|
+
* to consult the success cache first.
|
|
208
|
+
*/
|
|
209
|
+
async function tryImportCodingGraphModule(): Promise<ImportAttemptOutcome> {
|
|
210
|
+
// The dynamic `import()` with a runtime-concatenated specifier is the
|
|
211
|
+
// documented à-la-carte loader pattern. See file header.
|
|
212
|
+
let mod: unknown;
|
|
213
|
+
try {
|
|
214
|
+
// Cast through `unknown` is justified here: the dynamic import below
|
|
215
|
+
// is the LOADING BOUNDARY between core (no compile-time dependency on
|
|
216
|
+
// @remnic/coding-graph by design — see file header) and the optional
|
|
217
|
+
// package whose shape we have documented locally. We validate the
|
|
218
|
+
// structural shape before using any of the cast fields, so the cast
|
|
219
|
+
// asserts the boundary, not specific fields.
|
|
220
|
+
mod = (await import(SPECIFIER)) as unknown;
|
|
221
|
+
} catch (err) {
|
|
222
|
+
if (isSpecifierNotFoundErrorForCodingGraph(err)) {
|
|
223
|
+
return { kind: "missing" };
|
|
224
|
+
}
|
|
225
|
+
// Non-specifier error (broken transitive dep, loader fault, etc.)
|
|
226
|
+
// is wrapped so the loader can surface the diagnostic on a fresh
|
|
227
|
+
// attempt and the probe path can still swallow.
|
|
228
|
+
return { kind: "broken", cause: err };
|
|
229
|
+
}
|
|
230
|
+
if (!isLoadedCodingGraphModule(mod)) {
|
|
231
|
+
return { kind: "incompatible", actual: mod };
|
|
232
|
+
}
|
|
233
|
+
return { kind: "ok", module: mod };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Load `@remnic/coding-graph` if installed and return its
|
|
238
|
+
* `createCodingGraphEngine` factory. Throws a user-facing diagnostic
|
|
239
|
+
* that distinguishes three failure modes:
|
|
240
|
+
* - missing → install hint (canonical "npm install ..." message).
|
|
241
|
+
* - incompatible → "package shape mismatch" diagnostic.
|
|
242
|
+
* - broken → rethrows the original import error.
|
|
243
|
+
*
|
|
244
|
+
* Reuses the cached success path so repeated calls in the same
|
|
245
|
+
* process do not re-import. On success the resolved module is cached
|
|
246
|
+
* and shared with the try* helpers; a failure never poisons the cache
|
|
247
|
+
* so each call attempts a fresh import.
|
|
248
|
+
*/
|
|
249
|
+
export async function loadCodingGraphEngineFactory(): Promise<
|
|
250
|
+
(options?: CreateCodingGraphEngineOptions) => CodingGraphEngine
|
|
251
|
+
> {
|
|
252
|
+
// Fast path: a previous successful load populated cachedLoadResult.
|
|
253
|
+
// Reading it here matches the CLI `loadBenchModule` pattern and the
|
|
254
|
+
// probe path's convention of sharing the success result (Cursor
|
|
255
|
+
// Bugbot P3 round 5: "Loader skips success cache"). The fast path is
|
|
256
|
+
// intentionally read-only — we never write the success cache from
|
|
257
|
+
// the probe or fail-fast paths, so a probe that returned null cannot
|
|
258
|
+
// convert into a stale loader result here.
|
|
259
|
+
if (cachedLoadResult !== undefined && cachedLoadResult !== null) {
|
|
260
|
+
return cachedLoadResult.createCodingGraphEngine;
|
|
261
|
+
}
|
|
262
|
+
// Fresh attempt — never inherited from a probe failure. Users see
|
|
263
|
+
// the real diagnostic on broken installs.
|
|
264
|
+
const outcome = await tryImportCodingGraphModule();
|
|
265
|
+
switch (outcome.kind) {
|
|
266
|
+
case "ok":
|
|
267
|
+
cachedLoadResult = outcome.module;
|
|
268
|
+
return outcome.module.createCodingGraphEngine;
|
|
269
|
+
case "missing":
|
|
270
|
+
throw notInstalledError();
|
|
271
|
+
case "incompatible":
|
|
272
|
+
throw incompatibleModuleError(outcome.actual);
|
|
273
|
+
case "broken":
|
|
274
|
+
throw outcome.cause instanceof Error
|
|
275
|
+
? outcome.cause
|
|
276
|
+
: new Error(`@remnic/coding-graph failed to load: ${String(outcome.cause)}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Run the probe attempt with a Promise slot so concurrent callers
|
|
282
|
+
* await the same in-flight import (Cursor Bugbot P2 round 4).
|
|
283
|
+
* Successful results are also written into `cachedLoadResult` so a
|
|
284
|
+
* SUBSEQUENT loadCodingGraphEngineFactory skips the import (the
|
|
285
|
+
* success-cache fast path above). All non-ok outcomes (missing,
|
|
286
|
+
* incompatible, broken) are collapsed to `null` so the probe stays
|
|
287
|
+
* boolean-safe for gate-off consumers.
|
|
288
|
+
*/
|
|
289
|
+
function startProbeOnce(): Promise<LoadedCodingGraphModule | null> {
|
|
290
|
+
if (inFlightProbe !== null) {
|
|
291
|
+
return inFlightProbe;
|
|
292
|
+
}
|
|
293
|
+
const p = tryImportCodingGraphModule()
|
|
294
|
+
.then((outcome) => {
|
|
295
|
+
if (outcome.kind === "ok") {
|
|
296
|
+
cachedLoadResult = outcome.module;
|
|
297
|
+
return outcome.module;
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
})
|
|
301
|
+
.catch(() => {
|
|
302
|
+
// Defensive: tryImportCodingGraphModule does not reject in
|
|
303
|
+
// practice (we wrap all throws), but a future refactor could
|
|
304
|
+
// regress it — keep the probe boolean-safe.
|
|
305
|
+
return null as LoadedCodingGraphModule | null;
|
|
306
|
+
});
|
|
307
|
+
inFlightProbe = p;
|
|
308
|
+
// Once settled, clear the in-flight slot so a future call after a
|
|
309
|
+
// long delay still re-attempts. The successful module result lives
|
|
310
|
+
// on in cachedLoadResult if it succeeded.
|
|
311
|
+
p.finally(() => {
|
|
312
|
+
if (inFlightProbe === p) {
|
|
313
|
+
inFlightProbe = null;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
return p;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
// Cache slots — see file header for the failure-mode policy that justifies
|
|
321
|
+
// these being three separate slots.
|
|
322
|
+
// ---------------------------------------------------------------------------
|
|
323
|
+
let cachedLoadResult: LoadedCodingGraphModule | null | undefined;
|
|
324
|
+
let inFlightProbe: Promise<LoadedCodingGraphModule | null> | null = null;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Return `true` only when `@remnic/coding-graph` is installed AND
|
|
328
|
+
* importable in a usable shape. Returns `false` for missing, broken,
|
|
329
|
+
* or incompatible installs. Never throws.
|
|
330
|
+
*/
|
|
331
|
+
export async function isCodingGraphInstalled(): Promise<boolean> {
|
|
332
|
+
return (await tryLoadCodingGraphModule()) !== null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Return the engine factory module if `@remnic/coding-graph` is
|
|
337
|
+
* installed AND importable in a usable shape, or `null` otherwise.
|
|
338
|
+
* Use this for graceful-degradation code paths. Never throws.
|
|
339
|
+
*
|
|
340
|
+
* Uses an in-flight-promise slot so concurrent callers share the same
|
|
341
|
+
* import attempt, then caches the successful result so subsequent
|
|
342
|
+
* loadFactory calls skip the import.
|
|
343
|
+
*/
|
|
344
|
+
export async function tryLoadCodingGraphModule(): Promise<LoadedCodingGraphModule | null> {
|
|
345
|
+
// Fast path: a previous loadFactory call stored a successful module.
|
|
346
|
+
if (cachedLoadResult !== undefined && cachedLoadResult !== null) {
|
|
347
|
+
return cachedLoadResult;
|
|
348
|
+
}
|
|
349
|
+
return startProbeOnce();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ---------------------------------------------------------------------------
|
|
353
|
+
// Re-export the engine contract types and stable constants so callers can
|
|
354
|
+
// stay in `core` (no host prefix; the engine contract is owned by core).
|
|
355
|
+
// ---------------------------------------------------------------------------
|
|
356
|
+
export {
|
|
357
|
+
CODING_GRAPH_ENGINE_VERSION,
|
|
358
|
+
TIER_1_LANGUAGES,
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
export type {
|
|
362
|
+
CodingGraphEngine,
|
|
363
|
+
CodingGraphErrorCode,
|
|
364
|
+
CodingGraphLanguage,
|
|
365
|
+
CreateCodingGraphEngineOptions,
|
|
366
|
+
FileIR,
|
|
367
|
+
ParseFileInput,
|
|
368
|
+
ParseResult,
|
|
369
|
+
SymbolIR,
|
|
370
|
+
};
|