@getchomp/cli 0.0.0-stage → 0.2.43
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 +39 -2
- package/dist/commands/analyze.d.ts +49 -0
- package/dist/commands/analyze.js +280 -0
- package/dist/commands/connect.d.ts +12 -0
- package/dist/commands/connect.js +255 -0
- package/dist/commands/export.d.ts +29 -0
- package/dist/commands/export.js +356 -0
- package/dist/commands/health.d.ts +19 -0
- package/dist/commands/health.js +259 -0
- package/dist/commands/init.d.ts +40 -0
- package/dist/commands/init.js +659 -0
- package/dist/commands/mcp.d.ts +2 -0
- package/dist/commands/mcp.js +12 -0
- package/dist/commands/plugin.d.ts +8 -0
- package/dist/commands/plugin.js +198 -0
- package/dist/commands/ui.d.ts +20 -0
- package/dist/commands/ui.js +252 -0
- package/dist/commands/uninstall.d.ts +8 -0
- package/dist/commands/uninstall.js +246 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +37 -0
- package/dist/ports/portHydrator.d.ts +63 -0
- package/dist/ports/portHydrator.js +954 -0
- package/dist/ports/portHydratorWorker.d.ts +22 -0
- package/dist/ports/portHydratorWorker.js +75 -0
- package/dist/server/buildStatusTracker.d.ts +45 -0
- package/dist/server/buildStatusTracker.js +122 -0
- package/dist/server/graphLayoutState.d.ts +205 -0
- package/dist/server/graphLayoutState.js +930 -0
- package/dist/server/loopbackSecurity.d.ts +18 -0
- package/dist/server/loopbackSecurity.js +57 -0
- package/dist/server/routes/authSourceRoutes.d.ts +18 -0
- package/dist/server/routes/authSourceRoutes.js +231 -0
- package/dist/server/routes/layoutRoutes.d.ts +3 -0
- package/dist/server/routes/layoutRoutes.js +857 -0
- package/dist/server/routes/pluginRoutes.d.ts +3 -0
- package/dist/server/routes/pluginRoutes.js +368 -0
- package/dist/server/routes/viewRulesRoutes.d.ts +3 -0
- package/dist/server/routes/viewRulesRoutes.js +173 -0
- package/dist/server/uiHotkeys.d.ts +26 -0
- package/dist/server/uiHotkeys.js +160 -0
- package/dist/utils/branding.d.ts +126 -0
- package/dist/utils/branding.js +978 -0
- package/dist/utils/credentials.d.ts +22 -0
- package/dist/utils/credentials.js +126 -0
- package/dist/utils/git.d.ts +95 -0
- package/dist/utils/git.js +780 -0
- package/dist/utils/masterContext.d.ts +112 -0
- package/dist/utils/masterContext.js +642 -0
- package/dist/visualizer/cloudLayoutClient.d.ts +84 -0
- package/dist/visualizer/cloudLayoutClient.js +1200 -0
- package/dist/visualizer/layoutCache.d.ts +25 -0
- package/dist/visualizer/layoutCache.js +406 -0
- package/dist/visualizer/localEngineBridge.d.ts +50 -0
- package/dist/visualizer/localEngineBridge.js +80 -0
- package/dist/visualizer/pluginPresentationApi.d.ts +111 -0
- package/dist/visualizer/pluginPresentationApi.js +432 -0
- package/dist/visualizer/rulesConfig.d.ts +11 -0
- package/dist/visualizer/rulesConfig.js +266 -0
- package/dist/visualizer/sourceAccess.d.ts +45 -0
- package/dist/visualizer/sourceAccess.js +204 -0
- package/package.json +52 -3
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { LayoutData } from "@getchomp/core";
|
|
2
|
+
/**
|
|
3
|
+
* Computes a fast, deterministic fingerprint of the working tree at `repoPath`
|
|
4
|
+
* so `GET /api/layout?diff=true` can be served from memory in <5ms whenever
|
|
5
|
+
* no source files have been modified since the last diff layout build.
|
|
6
|
+
*/
|
|
7
|
+
export declare function computeWorkingTreeFingerprint(repoPath?: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Clears the short-lived TTL cache used by `computeWorkingTreeFingerprintAsync`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function clearWorkingTreeFingerprintCache(repoPath?: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Non-blocking, single-flight, TTL-cached version of `computeWorkingTreeFingerprint`
|
|
14
|
+
* for `chomp ui` polling routes (`/api/layout/fingerprint`, `/api/layout`, `/api/graph`).
|
|
15
|
+
* Produces the exact same SHA-1 digest as `computeWorkingTreeFingerprint` without
|
|
16
|
+
* blocking the Node.js event loop via `execSync`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function computeWorkingTreeFingerprintAsync(repoPath?: string, options?: {
|
|
19
|
+
ttlMs?: number;
|
|
20
|
+
}): Promise<string>;
|
|
21
|
+
export declare function readDiskLayoutCache(dbPath: string, cacheSeed: string): LayoutData | null;
|
|
22
|
+
export declare function writeDiskLayoutCache(dbPath: string, cacheSeed: string, layout: LayoutData): void;
|
|
23
|
+
export declare function clearDiskLayoutCache(dbPath: string): void;
|
|
24
|
+
export declare function readDiskDiffLayoutCache(dbPath: string, baseSha: string, targetSha: string, cacheSeed: string): LayoutData | null;
|
|
25
|
+
export declare function writeDiskDiffLayoutCache(dbPath: string, baseSha: string, targetSha: string, cacheSeed: string, layout: LayoutData): void;
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { execFile, execSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { getGitInfoFromFs } from "../utils/git.js";
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
const DISK_LAYOUT_CACHE_VERSION = "v2";
|
|
9
|
+
const MAX_DISK_CACHE_FILES = 4;
|
|
10
|
+
const IGNORED_DIRS = new Set([
|
|
11
|
+
"node_modules",
|
|
12
|
+
".git",
|
|
13
|
+
".next",
|
|
14
|
+
"dist",
|
|
15
|
+
"out",
|
|
16
|
+
".chomp",
|
|
17
|
+
".turbo",
|
|
18
|
+
"coverage",
|
|
19
|
+
]);
|
|
20
|
+
const SOURCE_EXT_RE = /\.(ts|tsx|js|jsx|mjs|cjs|json)$/i;
|
|
21
|
+
function hashWorkingTreeStatus(repoPath, headSha, branchOut, statusOut) {
|
|
22
|
+
const hash = createHash("sha1");
|
|
23
|
+
hash.update(`head:${headSha}:${branchOut}\n`);
|
|
24
|
+
const lines = statusOut.split("\n").filter(Boolean);
|
|
25
|
+
for (const line of lines) {
|
|
26
|
+
// Porcelain format: XY PATH or XY ORIG -> PATH
|
|
27
|
+
const rawPath = line.slice(3).trim();
|
|
28
|
+
const relPath = rawPath.includes(" -> ")
|
|
29
|
+
? rawPath.split(" -> ").pop().trim()
|
|
30
|
+
: rawPath;
|
|
31
|
+
// Ignore ephemeral UI/debug files inside .chomp/ while still tracking .chomp/plugins/** (and legacy .chomp/shims.json)
|
|
32
|
+
if (((relPath.startsWith(".chomp/") || relPath === ".chomp") &&
|
|
33
|
+
!relPath.startsWith(".chomp/plugins/") &&
|
|
34
|
+
relPath !== ".chomp/shims.json") ||
|
|
35
|
+
relPath.endsWith("layout_debug.csv") ||
|
|
36
|
+
relPath.endsWith("matrix_debug.csv")) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const absPath = resolve(repoPath, relPath);
|
|
40
|
+
let fileMeta = "deleted";
|
|
41
|
+
try {
|
|
42
|
+
if (existsSync(absPath)) {
|
|
43
|
+
const st = statSync(absPath);
|
|
44
|
+
fileMeta = `${Math.trunc(st.mtimeMs)}:${st.size}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Ignore stat race
|
|
49
|
+
}
|
|
50
|
+
hash.update(`${line.slice(0, 2)}:${relPath}:${fileMeta}\n`);
|
|
51
|
+
}
|
|
52
|
+
// Track .chomp/plugins/** (and legacy .chomp/shims.json) explicitly even when .chomp/ is gitignored
|
|
53
|
+
const pluginsDir = resolve(repoPath, ".chomp", "plugins");
|
|
54
|
+
if (existsSync(pluginsDir)) {
|
|
55
|
+
const walkPlugins = (dir, prefix, depth) => {
|
|
56
|
+
if (depth > 4)
|
|
57
|
+
return;
|
|
58
|
+
let entries;
|
|
59
|
+
try {
|
|
60
|
+
entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
for (const e of entries) {
|
|
66
|
+
const full = join(dir, e.name);
|
|
67
|
+
const rel = `${prefix}/${e.name}`;
|
|
68
|
+
if (e.isDirectory()) {
|
|
69
|
+
walkPlugins(full, rel, depth + 1);
|
|
70
|
+
}
|
|
71
|
+
else if (e.isFile()) {
|
|
72
|
+
try {
|
|
73
|
+
const st = statSync(full);
|
|
74
|
+
hash.update(`plugin:${rel}:${Math.trunc(st.mtimeMs)}:${st.size}\n`);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Ignore stat race
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
walkPlugins(pluginsDir, ".chomp/plugins", 0);
|
|
83
|
+
}
|
|
84
|
+
const legacyShimsPath = resolve(repoPath, ".chomp", "shims.json");
|
|
85
|
+
if (existsSync(legacyShimsPath)) {
|
|
86
|
+
try {
|
|
87
|
+
const st = statSync(legacyShimsPath);
|
|
88
|
+
hash.update(`shims:${Math.trunc(st.mtimeMs)}:${st.size}\n`);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Ignore stat race
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return hash.digest("hex").slice(0, 16);
|
|
95
|
+
}
|
|
96
|
+
function computeFsFallbackFingerprint(repoPath) {
|
|
97
|
+
// Fallback when git CLI is unavailable or directory is not a git repo:
|
|
98
|
+
// walk source files and hash their count, total size, and max mtimeMs.
|
|
99
|
+
let fileCount = 0;
|
|
100
|
+
let totalSize = 0;
|
|
101
|
+
let maxMtime = 0;
|
|
102
|
+
const walk = (dir, depth) => {
|
|
103
|
+
if (depth > 6)
|
|
104
|
+
return;
|
|
105
|
+
let entries;
|
|
106
|
+
try {
|
|
107
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (const entry of entries) {
|
|
113
|
+
if (IGNORED_DIRS.has(entry.name))
|
|
114
|
+
continue;
|
|
115
|
+
const full = join(dir, entry.name);
|
|
116
|
+
if (entry.isDirectory()) {
|
|
117
|
+
walk(full, depth + 1);
|
|
118
|
+
}
|
|
119
|
+
else if (entry.isFile() && SOURCE_EXT_RE.test(entry.name)) {
|
|
120
|
+
try {
|
|
121
|
+
const st = statSync(full);
|
|
122
|
+
fileCount++;
|
|
123
|
+
totalSize += st.size;
|
|
124
|
+
if (st.mtimeMs > maxMtime)
|
|
125
|
+
maxMtime = st.mtimeMs;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// Ignore transient file errors
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
walk(repoPath, 0);
|
|
134
|
+
return `fs:${fileCount}:${totalSize}:${Math.trunc(maxMtime)}`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Computes a fast, deterministic fingerprint of the working tree at `repoPath`
|
|
138
|
+
* so `GET /api/layout?diff=true` can be served from memory in <5ms whenever
|
|
139
|
+
* no source files have been modified since the last diff layout build.
|
|
140
|
+
*/
|
|
141
|
+
export function computeWorkingTreeFingerprint(repoPath) {
|
|
142
|
+
if (!repoPath || !existsSync(repoPath)) {
|
|
143
|
+
return "no-repo";
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const headSha = execSync("git rev-parse HEAD", {
|
|
147
|
+
cwd: repoPath,
|
|
148
|
+
encoding: "utf-8",
|
|
149
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
150
|
+
}).trim();
|
|
151
|
+
const branchOut = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
152
|
+
cwd: repoPath,
|
|
153
|
+
encoding: "utf-8",
|
|
154
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
155
|
+
}).trim();
|
|
156
|
+
const statusOut = execSync("git status --porcelain -uall", {
|
|
157
|
+
cwd: repoPath,
|
|
158
|
+
encoding: "utf-8",
|
|
159
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
160
|
+
});
|
|
161
|
+
return hashWorkingTreeStatus(repoPath, headSha, branchOut, statusOut);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return computeFsFallbackFingerprint(repoPath);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const wtFingerprintCache = new Map();
|
|
168
|
+
const wtFingerprintInFlight = new Map();
|
|
169
|
+
/**
|
|
170
|
+
* Clears the short-lived TTL cache used by `computeWorkingTreeFingerprintAsync`.
|
|
171
|
+
*/
|
|
172
|
+
export function clearWorkingTreeFingerprintCache(repoPath) {
|
|
173
|
+
if (!repoPath) {
|
|
174
|
+
wtFingerprintCache.clear();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
wtFingerprintCache.delete(resolve(repoPath));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Non-blocking, single-flight, TTL-cached version of `computeWorkingTreeFingerprint`
|
|
181
|
+
* for `chomp ui` polling routes (`/api/layout/fingerprint`, `/api/layout`, `/api/graph`).
|
|
182
|
+
* Produces the exact same SHA-1 digest as `computeWorkingTreeFingerprint` without
|
|
183
|
+
* blocking the Node.js event loop via `execSync`.
|
|
184
|
+
*/
|
|
185
|
+
export async function computeWorkingTreeFingerprintAsync(repoPath, options = {}) {
|
|
186
|
+
if (!repoPath || !existsSync(repoPath)) {
|
|
187
|
+
return "no-repo";
|
|
188
|
+
}
|
|
189
|
+
const absRepo = resolve(repoPath);
|
|
190
|
+
const ttlMs = options.ttlMs ?? 1500;
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
const cached = wtFingerprintCache.get(absRepo);
|
|
193
|
+
if (ttlMs > 0 && cached && cached.expiresAt > now) {
|
|
194
|
+
return cached.value;
|
|
195
|
+
}
|
|
196
|
+
const pending = wtFingerprintInFlight.get(absRepo);
|
|
197
|
+
if (pending) {
|
|
198
|
+
return pending;
|
|
199
|
+
}
|
|
200
|
+
const promise = (async () => {
|
|
201
|
+
try {
|
|
202
|
+
const fsGit = getGitInfoFromFs(absRepo);
|
|
203
|
+
let headSha = fsGit.commitSha;
|
|
204
|
+
let branchOut = fsGit.commitSha ? (fsGit.branchName ?? "HEAD") : undefined;
|
|
205
|
+
let statusOut;
|
|
206
|
+
if (headSha && branchOut) {
|
|
207
|
+
const statusRes = await execFileAsync("git", ["status", "--porcelain", "-uall"], { cwd: absRepo, timeout: 8000, maxBuffer: 16 * 1024 * 1024 });
|
|
208
|
+
statusOut = statusRes.stdout;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const [shaRes, branchRes, statusRes] = await Promise.all([
|
|
212
|
+
execFileAsync("git", ["rev-parse", "HEAD"], {
|
|
213
|
+
cwd: absRepo,
|
|
214
|
+
timeout: 8000,
|
|
215
|
+
}),
|
|
216
|
+
execFileAsync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
|
|
217
|
+
cwd: absRepo,
|
|
218
|
+
timeout: 8000,
|
|
219
|
+
}),
|
|
220
|
+
execFileAsync("git", ["status", "--porcelain", "-uall"], {
|
|
221
|
+
cwd: absRepo,
|
|
222
|
+
timeout: 8000,
|
|
223
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
224
|
+
}),
|
|
225
|
+
]);
|
|
226
|
+
headSha = shaRes.stdout.trim();
|
|
227
|
+
branchOut = branchRes.stdout.trim();
|
|
228
|
+
statusOut = statusRes.stdout;
|
|
229
|
+
}
|
|
230
|
+
return hashWorkingTreeStatus(absRepo, headSha, branchOut, statusOut);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return computeFsFallbackFingerprint(absRepo);
|
|
234
|
+
}
|
|
235
|
+
})()
|
|
236
|
+
.then((digest) => {
|
|
237
|
+
if (ttlMs > 0) {
|
|
238
|
+
if (wtFingerprintCache.size >= 32) {
|
|
239
|
+
const oldest = wtFingerprintCache.keys().next().value;
|
|
240
|
+
if (oldest)
|
|
241
|
+
wtFingerprintCache.delete(oldest);
|
|
242
|
+
}
|
|
243
|
+
wtFingerprintCache.set(absRepo, {
|
|
244
|
+
expiresAt: Date.now() + ttlMs,
|
|
245
|
+
value: digest,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return digest;
|
|
249
|
+
})
|
|
250
|
+
.finally(() => {
|
|
251
|
+
wtFingerprintInFlight.delete(absRepo);
|
|
252
|
+
});
|
|
253
|
+
wtFingerprintInFlight.set(absRepo, promise);
|
|
254
|
+
return promise;
|
|
255
|
+
}
|
|
256
|
+
function resolveDiskCacheDir(dbPath) {
|
|
257
|
+
return join(dirname(dbPath), "cache");
|
|
258
|
+
}
|
|
259
|
+
function buildDiskCacheFilename(cacheSeed) {
|
|
260
|
+
const digest = createHash("sha1")
|
|
261
|
+
.update(`${DISK_LAYOUT_CACHE_VERSION}:${cacheSeed}`)
|
|
262
|
+
.digest("hex")
|
|
263
|
+
.slice(0, 20);
|
|
264
|
+
return `layout-${digest}.json`;
|
|
265
|
+
}
|
|
266
|
+
export function readDiskLayoutCache(dbPath, cacheSeed) {
|
|
267
|
+
try {
|
|
268
|
+
const filePath = join(resolveDiskCacheDir(dbPath), buildDiskCacheFilename(cacheSeed));
|
|
269
|
+
if (!existsSync(filePath))
|
|
270
|
+
return null;
|
|
271
|
+
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
272
|
+
if (raw?.version !== DISK_LAYOUT_CACHE_VERSION ||
|
|
273
|
+
!raw.layout ||
|
|
274
|
+
!Array.isArray(raw.layout.nodes)) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
return raw.layout;
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
export function writeDiskLayoutCache(dbPath, cacheSeed, layout) {
|
|
284
|
+
try {
|
|
285
|
+
const cacheDir = resolveDiskCacheDir(dbPath);
|
|
286
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
287
|
+
const filename = buildDiskCacheFilename(cacheSeed);
|
|
288
|
+
const targetFile = join(cacheDir, filename);
|
|
289
|
+
writeFileSync(targetFile, JSON.stringify({
|
|
290
|
+
version: DISK_LAYOUT_CACHE_VERSION,
|
|
291
|
+
savedAt: new Date().toISOString(),
|
|
292
|
+
layout,
|
|
293
|
+
}), "utf8");
|
|
294
|
+
// Prune older cached layout files so `.chomp/cache/` stays bounded
|
|
295
|
+
const entries = readdirSync(cacheDir)
|
|
296
|
+
.filter((f) => f.startsWith("layout-") && f.endsWith(".json"))
|
|
297
|
+
.map((f) => {
|
|
298
|
+
const full = join(cacheDir, f);
|
|
299
|
+
let mtimeMs = 0;
|
|
300
|
+
try {
|
|
301
|
+
mtimeMs = statSync(full).mtimeMs;
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
// ignore
|
|
305
|
+
}
|
|
306
|
+
return { f, full, mtimeMs };
|
|
307
|
+
})
|
|
308
|
+
.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
309
|
+
for (const stale of entries.slice(MAX_DISK_CACHE_FILES)) {
|
|
310
|
+
try {
|
|
311
|
+
unlinkSync(stale.full);
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
// ignore
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// Non-fatal if `.chomp/cache` is read-only
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
export function clearDiskLayoutCache(dbPath) {
|
|
323
|
+
try {
|
|
324
|
+
const cacheDir = resolveDiskCacheDir(dbPath);
|
|
325
|
+
if (!existsSync(cacheDir))
|
|
326
|
+
return;
|
|
327
|
+
// Remove layout-*.json and layout-diff-*.json while preserving immutable ref-graph-<sha12>.json entries
|
|
328
|
+
for (const entry of readdirSync(cacheDir)) {
|
|
329
|
+
if (entry.startsWith("layout-") && entry.endsWith(".json")) {
|
|
330
|
+
try {
|
|
331
|
+
unlinkSync(join(cacheDir, entry));
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
// ignore
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
// Non-fatal
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const MAX_DIFF_LAYOUT_CACHE_FILES = 8;
|
|
344
|
+
function buildDiffDiskCacheFilename(baseSha, targetSha, cacheSeed) {
|
|
345
|
+
const shaA = baseSha.trim().slice(0, 12);
|
|
346
|
+
const shaB = targetSha.trim().slice(0, 12);
|
|
347
|
+
const seedHash = createHash("sha1")
|
|
348
|
+
.update(`${DISK_LAYOUT_CACHE_VERSION}:${cacheSeed}`)
|
|
349
|
+
.digest("hex")
|
|
350
|
+
.slice(0, 10);
|
|
351
|
+
return `layout-diff-${shaA}-${shaB}-${seedHash}.json`;
|
|
352
|
+
}
|
|
353
|
+
export function readDiskDiffLayoutCache(dbPath, baseSha, targetSha, cacheSeed) {
|
|
354
|
+
try {
|
|
355
|
+
const filePath = join(resolveDiskCacheDir(dbPath), buildDiffDiskCacheFilename(baseSha, targetSha, cacheSeed));
|
|
356
|
+
if (!existsSync(filePath))
|
|
357
|
+
return null;
|
|
358
|
+
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
359
|
+
if (raw?.version !== DISK_LAYOUT_CACHE_VERSION ||
|
|
360
|
+
!raw.layout ||
|
|
361
|
+
!Array.isArray(raw.layout.nodes)) {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
return raw.layout;
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
export function writeDiskDiffLayoutCache(dbPath, baseSha, targetSha, cacheSeed, layout) {
|
|
371
|
+
try {
|
|
372
|
+
const cacheDir = resolveDiskCacheDir(dbPath);
|
|
373
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
374
|
+
const filename = buildDiffDiskCacheFilename(baseSha, targetSha, cacheSeed);
|
|
375
|
+
writeFileSync(join(cacheDir, filename), JSON.stringify({
|
|
376
|
+
version: DISK_LAYOUT_CACHE_VERSION,
|
|
377
|
+
savedAt: new Date().toISOString(),
|
|
378
|
+
layout,
|
|
379
|
+
}), "utf8");
|
|
380
|
+
const entries = readdirSync(cacheDir)
|
|
381
|
+
.filter((f) => f.startsWith("layout-diff-") && f.endsWith(".json"))
|
|
382
|
+
.map((f) => {
|
|
383
|
+
const full = join(cacheDir, f);
|
|
384
|
+
let mtimeMs = 0;
|
|
385
|
+
try {
|
|
386
|
+
mtimeMs = statSync(full).mtimeMs;
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// ignore
|
|
390
|
+
}
|
|
391
|
+
return { full, mtimeMs };
|
|
392
|
+
})
|
|
393
|
+
.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
394
|
+
for (const stale of entries.slice(MAX_DIFF_LAYOUT_CACHE_FILES)) {
|
|
395
|
+
try {
|
|
396
|
+
unlinkSync(stale.full);
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
// ignore
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
// Non-fatal
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/cli/src/visualizer/localEngineBridge.ts
|
|
3
|
+
*
|
|
4
|
+
* Master Monorepo vs. Published NPM Bridge (`W2.0a` / `STRATEGY_AND_PACKAGING.md` §2).
|
|
5
|
+
*
|
|
6
|
+
* - Inside the private master monorepo (when `apps/cloud-layout-worker/src/pipeline-topo` exists on disk
|
|
7
|
+
* and `CHOMP_DISABLE_LOCAL_LAYOUT_ENGINE` is not set), dynamically loads the proprietary layout engine
|
|
8
|
+
* in-process with zero network calls and `tier: "master"` entitlement.
|
|
9
|
+
* - In published `@getchomp/cli` NPM installs, `apps/cloud-layout-worker` is absent from disk, so the CLI
|
|
10
|
+
* defaults to `https://api.getchomp.dev` and `https://app.getchomp.dev?port=<port>`.
|
|
11
|
+
*/
|
|
12
|
+
import type { ChompRulesConfig, EntityOverlayItem, GraphDelta, LayoutData, LayoutNode, RepresentationGraph, StatelessLayoutRequest, StatelessLayoutResponse, WorkingNode } from "@getchomp/core";
|
|
13
|
+
export declare const DEFAULT_CLOUD_API_URL = "https://api.getchomp.dev";
|
|
14
|
+
export declare const DEFAULT_HOSTED_UI_URL = "https://app.getchomp.dev";
|
|
15
|
+
export interface StoryTraceResultShape {
|
|
16
|
+
stories: Array<{
|
|
17
|
+
storyIndex: number;
|
|
18
|
+
entryId: string;
|
|
19
|
+
entryLabel: string;
|
|
20
|
+
packageLabel: string;
|
|
21
|
+
nodes: Array<WorkingNode & {
|
|
22
|
+
originalId: string;
|
|
23
|
+
columnIndex: number;
|
|
24
|
+
[k: string]: any;
|
|
25
|
+
}>;
|
|
26
|
+
[k: string]: any;
|
|
27
|
+
}>;
|
|
28
|
+
orphanNodes: WorkingNode[];
|
|
29
|
+
foldedMasterNodes?: WorkingNode[];
|
|
30
|
+
totalRawNodeCount: number;
|
|
31
|
+
[k: string]: any;
|
|
32
|
+
}
|
|
33
|
+
export interface MergedStoryGraphShape extends StoryTraceResultShape {
|
|
34
|
+
anchorCount: number;
|
|
35
|
+
badgedCount: number;
|
|
36
|
+
}
|
|
37
|
+
export interface LocalMasterPipelineModule {
|
|
38
|
+
runStoryTracer: (rawGraph: RepresentationGraph, diffDelta?: GraphDelta, rulesConfig?: ChompRulesConfig, overlays?: EntityOverlayItem[]) => StoryTraceResultShape;
|
|
39
|
+
runZipMerge: (storyGraph: StoryTraceResultShape) => MergedStoryGraphShape;
|
|
40
|
+
runGridMapper: (mergedGraph: MergedStoryGraphShape, rulesConfig?: ChompRulesConfig) => LayoutData;
|
|
41
|
+
isSemanticUtilityLabel: (label: string, outDegree?: number, foldedCount?: number) => boolean;
|
|
42
|
+
writeMatrixCsv: (graph: MergedStoryGraphShape, cwd?: string) => void;
|
|
43
|
+
writeLayoutCsv: (layoutData: LayoutData, cwd?: string) => void;
|
|
44
|
+
buildPureLibraryFallbackBoard: (...args: any[]) => any;
|
|
45
|
+
estimateNodeVisualHeight: (node: Partial<LayoutNode> | any) => number;
|
|
46
|
+
executeStatelessLayout: (request: StatelessLayoutRequest) => StatelessLayoutResponse;
|
|
47
|
+
}
|
|
48
|
+
export declare function hasLocalMasterLayoutEngine(): boolean;
|
|
49
|
+
export declare function resolveLocalMasterWebDir(): string | null;
|
|
50
|
+
export declare function loadLocalMasterLayoutEngine(): Promise<LocalMasterPipelineModule>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/cli/src/visualizer/localEngineBridge.ts
|
|
3
|
+
*
|
|
4
|
+
* Master Monorepo vs. Published NPM Bridge (`W2.0a` / `STRATEGY_AND_PACKAGING.md` §2).
|
|
5
|
+
*
|
|
6
|
+
* - Inside the private master monorepo (when `apps/cloud-layout-worker/src/pipeline-topo` exists on disk
|
|
7
|
+
* and `CHOMP_DISABLE_LOCAL_LAYOUT_ENGINE` is not set), dynamically loads the proprietary layout engine
|
|
8
|
+
* in-process with zero network calls and `tier: "master"` entitlement.
|
|
9
|
+
* - In published `@getchomp/cli` NPM installs, `apps/cloud-layout-worker` is absent from disk, so the CLI
|
|
10
|
+
* defaults to `https://api.getchomp.dev` and `https://app.getchomp.dev?port=<port>`.
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync } from "node:fs";
|
|
13
|
+
import { resolve } from "node:path";
|
|
14
|
+
import { pathToFileURL } from "node:url";
|
|
15
|
+
import { getChompMasterRoot } from "../utils/masterContext.js";
|
|
16
|
+
export const DEFAULT_CLOUD_API_URL = "https://api.getchomp.dev";
|
|
17
|
+
export const DEFAULT_HOSTED_UI_URL = "https://app.getchomp.dev";
|
|
18
|
+
function canImportTypeScriptAtRuntime() {
|
|
19
|
+
const metaUrl = typeof import.meta !== "undefined" ? import.meta.url : "";
|
|
20
|
+
if (metaUrl.endsWith(".ts"))
|
|
21
|
+
return true;
|
|
22
|
+
if (process.execArgv.some((arg) => arg.includes("tsx") || arg.includes("strip-types"))) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
const nodeOpts = process.env.NODE_OPTIONS ?? "";
|
|
26
|
+
if (nodeOpts.includes("tsx") || nodeOpts.includes("strip-types")) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const major = Number(process.versions.node.split(".")[0] ?? "0");
|
|
30
|
+
return major >= 24;
|
|
31
|
+
}
|
|
32
|
+
function resolveLocalMasterEnginePath() {
|
|
33
|
+
if (process.env.CHOMP_DISABLE_LOCAL_LAYOUT_ENGINE === "1") {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const masterRoot = getChompMasterRoot();
|
|
37
|
+
const candidates = canImportTypeScriptAtRuntime()
|
|
38
|
+
? [
|
|
39
|
+
resolve(masterRoot, "apps/cloud-layout-worker/src/pipeline-topo/index.ts"),
|
|
40
|
+
resolve(masterRoot, "apps/cloud-layout-worker/src/pipeline-topo/index.js"),
|
|
41
|
+
resolve(masterRoot, "apps/cloud-layout-worker/dist/pipeline-topo/index.js"),
|
|
42
|
+
]
|
|
43
|
+
: [
|
|
44
|
+
resolve(masterRoot, "apps/cloud-layout-worker/dist/pipeline-topo/index.js"),
|
|
45
|
+
resolve(masterRoot, "apps/cloud-layout-worker/src/pipeline-topo/index.js"),
|
|
46
|
+
];
|
|
47
|
+
for (const candidate of candidates) {
|
|
48
|
+
if (existsSync(candidate)) {
|
|
49
|
+
return candidate;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
export function hasLocalMasterLayoutEngine() {
|
|
55
|
+
return resolveLocalMasterEnginePath() !== null;
|
|
56
|
+
}
|
|
57
|
+
export function resolveLocalMasterWebDir() {
|
|
58
|
+
if (process.env.CHOMP_DISABLE_LOCAL_LAYOUT_ENGINE === "1") {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const masterRoot = getChompMasterRoot();
|
|
62
|
+
const webOutDir = resolve(masterRoot, "apps/web/out");
|
|
63
|
+
if (existsSync(resolve(webOutDir, "index.html"))) {
|
|
64
|
+
return webOutDir;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
let cachedEnginePromise = null;
|
|
69
|
+
export async function loadLocalMasterLayoutEngine() {
|
|
70
|
+
const enginePath = resolveLocalMasterEnginePath();
|
|
71
|
+
if (!enginePath) {
|
|
72
|
+
throw new Error("Local master layout engine is not present in this installation. Connect to https://api.getchomp.dev instead.");
|
|
73
|
+
}
|
|
74
|
+
if (!cachedEnginePromise) {
|
|
75
|
+
const fileHref = pathToFileURL(enginePath).href;
|
|
76
|
+
// Variable dynamic import prevents bundlers and npm pack from tracing into apps/cloud-layout-worker
|
|
77
|
+
cachedEnginePromise = import(fileHref);
|
|
78
|
+
}
|
|
79
|
+
return cachedEnginePromise;
|
|
80
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/cli/src/visualizer/pluginPresentationApi.ts
|
|
3
|
+
*
|
|
4
|
+
* Server-side model for the Plugin Presentation Contract ([PAT-VIS-36]). Pure functions (no Express, no DB)
|
|
5
|
+
* so they are unit-testable:
|
|
6
|
+
*
|
|
7
|
+
* - `splitOverlays` – the heavy `payload.presentation` is deferred out of the structural
|
|
8
|
+
* `/api/layout` & `/api/graph` payloads (other payload fields stay, because
|
|
9
|
+
* custom rules and node badges may read them).
|
|
10
|
+
* - `computeStructureFingerprint` / `computePluginFingerprint`
|
|
11
|
+
* – independent fingerprints so the client re-lays-out only when the structure
|
|
12
|
+
* changes and re-fetches plugin data when only plugins changed.
|
|
13
|
+
* - `buildPluginsOverview` – HUD cards (`GET /api/plugins`).
|
|
14
|
+
* - `buildEntityPresentations` – inspector sections for one entity (`GET /api/plugins/presentation`).
|
|
15
|
+
* - `startPluginRun` & co. – in-memory run state for UI-triggered plugin runs.
|
|
16
|
+
*/
|
|
17
|
+
import { type EntityPluginPresentation, type PluginInspectorScope, type PluginRunStatus, type PluginSettingField, type PluginSettingValue, type PluginToolDescriptorSummary, type PluginsOverview, type EntityOverlayItem } from "@getchomp/core";
|
|
18
|
+
export interface PluginMeta {
|
|
19
|
+
name: string;
|
|
20
|
+
displayName?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
version?: string;
|
|
23
|
+
order?: number;
|
|
24
|
+
layer?: string;
|
|
25
|
+
defaultEnabled?: boolean;
|
|
26
|
+
inspectorScopes?: PluginInspectorScope[];
|
|
27
|
+
stateDir?: string;
|
|
28
|
+
settingsSchema?: PluginSettingField[];
|
|
29
|
+
settings?: Record<string, PluginSettingValue>;
|
|
30
|
+
envStatus?: Record<string, boolean>;
|
|
31
|
+
mcpTools?: PluginToolDescriptorSummary[];
|
|
32
|
+
lastRun?: {
|
|
33
|
+
status: "ok" | "error";
|
|
34
|
+
startedAt: string;
|
|
35
|
+
finishedAt: string;
|
|
36
|
+
durationMs: number;
|
|
37
|
+
overlayCount?: number;
|
|
38
|
+
presentationWarnings?: string[];
|
|
39
|
+
error?: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface GraphNodeLike {
|
|
43
|
+
id: string;
|
|
44
|
+
name: string;
|
|
45
|
+
entityType?: string;
|
|
46
|
+
metadata?: Record<string, unknown> | null;
|
|
47
|
+
}
|
|
48
|
+
interface GraphEdgeLike {
|
|
49
|
+
id?: string;
|
|
50
|
+
targetId?: string | null;
|
|
51
|
+
status?: string;
|
|
52
|
+
}
|
|
53
|
+
export declare function isPluginOverlay(ov: EntityOverlayItem): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* `layout`: every overlay in structural form (presentation deferred, `presentationAvailable` marker set).
|
|
56
|
+
* `plugin`: the overlays that carry a presentation, in full, for `/api/plugins/*`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function splitOverlays(overlays: EntityOverlayItem[]): {
|
|
59
|
+
layout: EntityOverlayItem[];
|
|
60
|
+
plugin: EntityOverlayItem[];
|
|
61
|
+
};
|
|
62
|
+
interface RuleLike {
|
|
63
|
+
match?: {
|
|
64
|
+
overlayLayer?: unknown;
|
|
65
|
+
overlaySource?: unknown;
|
|
66
|
+
overlayField?: unknown;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Do the user's custom rules read overlay data? If so, plugin writes can legitimately change the layout. */
|
|
70
|
+
export declare function rulesReferenceOverlays(customRules: RuleLike[] | undefined): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Overlays that participate in the structure fingerprint: all non-plugin overlays, `L1_SHIMS`
|
|
73
|
+
* overlays that project edges/connectors onto the story graph, plus other plugin overlays
|
|
74
|
+
* (in structural form) only when rules reference overlays. Otherwise presentation-only plugin runs
|
|
75
|
+
* never force a re-layout.
|
|
76
|
+
*/
|
|
77
|
+
export declare function overlaysForStructureFingerprint(split: {
|
|
78
|
+
layout: EntityOverlayItem[];
|
|
79
|
+
plugin: EntityOverlayItem[];
|
|
80
|
+
}, rulesUseOverlays: boolean, isStructuralOverlay?: (ov: EntityOverlayItem) => boolean): EntityOverlayItem[];
|
|
81
|
+
/**
|
|
82
|
+
* Content-based: unchanged when a re-analysis produces the same structure, and unaffected by
|
|
83
|
+
* plugin overlay writes.
|
|
84
|
+
*/
|
|
85
|
+
export declare function computeStructureFingerprint(nodes: GraphNodeLike[], edges: GraphEdgeLike[], structuralOverlays: EntityOverlayItem[]): string;
|
|
86
|
+
export declare function computePluginFingerprint(pluginOverlays: EntityOverlayItem[], runs: Record<string, PluginRunStatus>, registry?: PluginMeta[]): string;
|
|
87
|
+
export declare function collectAvailableOverlayEnvironments(pluginOverlays: EntityOverlayItem[]): string[];
|
|
88
|
+
export declare function buildPluginsOverview(params: {
|
|
89
|
+
pluginOverlays: EntityOverlayItem[];
|
|
90
|
+
nodes: GraphNodeLike[];
|
|
91
|
+
registry: PluginMeta[];
|
|
92
|
+
runs: Record<string, PluginRunStatus>;
|
|
93
|
+
structureFp: string;
|
|
94
|
+
environment?: string;
|
|
95
|
+
}): PluginsOverview;
|
|
96
|
+
export declare function buildEntityPresentations(params: {
|
|
97
|
+
pluginOverlays: EntityOverlayItem[];
|
|
98
|
+
entityId: string;
|
|
99
|
+
registry: PluginMeta[];
|
|
100
|
+
nodes?: GraphNodeLike[];
|
|
101
|
+
environment?: string;
|
|
102
|
+
}): EntityPluginPresentation[];
|
|
103
|
+
/** In-memory run state for UI-triggered plugin runs (analyze-triggered runs are visible via the fingerprint). */
|
|
104
|
+
export type PluginRunMap = Map<string, PluginRunStatus>;
|
|
105
|
+
export declare function getPluginRun(runs: PluginRunMap, name: string): PluginRunStatus;
|
|
106
|
+
export declare function snapshotPluginRuns(runs: PluginRunMap): Record<string, PluginRunStatus>;
|
|
107
|
+
export declare function isPluginRunning(runs: PluginRunMap, name: string): boolean;
|
|
108
|
+
export declare function startPluginRun(runs: PluginRunMap, name: string, now?: () => string): PluginRunStatus;
|
|
109
|
+
export declare function finishPluginRun(runs: PluginRunMap, name: string, now?: () => string): PluginRunStatus;
|
|
110
|
+
export declare function failPluginRun(runs: PluginRunMap, name: string, error: string, now?: () => string): PluginRunStatus;
|
|
111
|
+
export {};
|