ko-qmd 2.8.3-ko.2

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +1376 -0
  2. package/LICENSE +21 -0
  3. package/README.ko.md +86 -0
  4. package/README.md +1307 -0
  5. package/bin/qmd +192 -0
  6. package/dist/ast.d.ts +65 -0
  7. package/dist/ast.js +334 -0
  8. package/dist/bench/bench.d.ts +35 -0
  9. package/dist/bench/bench.js +327 -0
  10. package/dist/bench/score.d.ts +33 -0
  11. package/dist/bench/score.js +88 -0
  12. package/dist/bench/types.d.ts +80 -0
  13. package/dist/bench/types.js +8 -0
  14. package/dist/cli/build-info.json +4 -0
  15. package/dist/cli/embed-lock.d.ts +24 -0
  16. package/dist/cli/embed-lock.js +94 -0
  17. package/dist/cli/formatter.d.ts +120 -0
  18. package/dist/cli/formatter.js +355 -0
  19. package/dist/cli/mcp-pid.d.ts +25 -0
  20. package/dist/cli/mcp-pid.js +86 -0
  21. package/dist/cli/qmd.d.ts +43 -0
  22. package/dist/cli/qmd.js +4424 -0
  23. package/dist/cli/version.d.ts +42 -0
  24. package/dist/cli/version.js +80 -0
  25. package/dist/collections.d.ts +167 -0
  26. package/dist/collections.js +410 -0
  27. package/dist/db.d.ts +63 -0
  28. package/dist/db.js +130 -0
  29. package/dist/hangul.d.ts +54 -0
  30. package/dist/hangul.js +140 -0
  31. package/dist/index.d.ts +237 -0
  32. package/dist/index.js +244 -0
  33. package/dist/llm.d.ts +543 -0
  34. package/dist/llm.js +1747 -0
  35. package/dist/maintenance.d.ts +33 -0
  36. package/dist/maintenance.js +52 -0
  37. package/dist/mcp/origin-guard.d.ts +67 -0
  38. package/dist/mcp/origin-guard.js +137 -0
  39. package/dist/mcp/server.d.ts +116 -0
  40. package/dist/mcp/server.js +883 -0
  41. package/dist/paths.d.ts +1 -0
  42. package/dist/paths.js +4 -0
  43. package/dist/store.d.ts +1100 -0
  44. package/dist/store.js +4965 -0
  45. package/dist/trust.d.ts +152 -0
  46. package/dist/trust.js +249 -0
  47. package/package.json +114 -0
  48. package/scripts/build.mjs +72 -0
  49. package/scripts/check-package-grammars.mjs +29 -0
  50. package/scripts/package-smoke.mjs +65 -0
  51. package/scripts/test-all.mjs +38 -0
  52. package/skills/qmd/SKILL.md +316 -0
  53. package/skills/qmd/references/mcp-setup.md +119 -0
  54. package/skills/release/SKILL.md +141 -0
  55. package/skills/release/scripts/install-hooks.sh +38 -0
  56. package/skills/release/scripts/release-context.sh +129 -0
@@ -0,0 +1,120 @@
1
+ /**
2
+ * formatter.ts - Output formatting utilities for QMD
3
+ *
4
+ * Provides methods to format search results and documents into various output formats:
5
+ * JSON, CSV, XML, Markdown, files list, and CLI (colored terminal output).
6
+ */
7
+ import type { SearchResult, MultiGetResult, DocumentResult } from "../store.js";
8
+ export type { SearchResult, MultiGetResult, DocumentResult };
9
+ export type MultiGetFile = {
10
+ filepath: string;
11
+ displayPath: string;
12
+ title: string;
13
+ body: string;
14
+ context?: string | null;
15
+ skipped: false;
16
+ } | {
17
+ filepath: string;
18
+ displayPath: string;
19
+ title: string;
20
+ body: string;
21
+ context?: string | null;
22
+ skipped: true;
23
+ skipReason: string;
24
+ };
25
+ export type OutputFormat = "cli" | "csv" | "md" | "xml" | "files" | "json";
26
+ export type FormatOptions = {
27
+ full?: boolean;
28
+ query?: string;
29
+ useColor?: boolean;
30
+ lineNumbers?: boolean;
31
+ intent?: string;
32
+ };
33
+ /**
34
+ * Add line numbers to text content.
35
+ * Each line becomes: "{lineNum}: {content}"
36
+ * @param text The text to add line numbers to
37
+ * @param startLine Optional starting line number (default: 1)
38
+ */
39
+ export declare function addLineNumbers(text: string, startLine?: number): string;
40
+ /**
41
+ * Extract short docid from a full hash (first 6 characters).
42
+ */
43
+ export declare function getDocid(hash: string): string;
44
+ export declare function escapeCSV(value: string | null | number): string;
45
+ export declare function escapeXml(str: string): string;
46
+ /**
47
+ * Format search results as JSON
48
+ */
49
+ export declare function searchResultsToJson(results: SearchResult[], opts?: FormatOptions): string;
50
+ /**
51
+ * Format search results as CSV
52
+ */
53
+ export declare function searchResultsToCsv(results: SearchResult[], opts?: FormatOptions): string;
54
+ /**
55
+ * Format search results as simple files list (docid,score,filepath,context)
56
+ */
57
+ export declare function searchResultsToFiles(results: SearchResult[]): string;
58
+ /**
59
+ * Format search results as Markdown
60
+ */
61
+ export declare function searchResultsToMarkdown(results: SearchResult[], opts?: FormatOptions): string;
62
+ /**
63
+ * Format search results as XML
64
+ */
65
+ export declare function searchResultsToXml(results: SearchResult[], opts?: FormatOptions): string;
66
+ /**
67
+ * Format search results for MCP (simpler CSV format with pre-extracted snippets)
68
+ */
69
+ export declare function searchResultsToMcpCsv(results: {
70
+ docid: string;
71
+ file: string;
72
+ title: string;
73
+ score: number;
74
+ context: string | null;
75
+ snippet: string;
76
+ }[]): string;
77
+ /**
78
+ * Format documents as JSON
79
+ */
80
+ export declare function documentsToJson(results: MultiGetFile[]): string;
81
+ /**
82
+ * Format documents as CSV
83
+ */
84
+ export declare function documentsToCsv(results: MultiGetFile[]): string;
85
+ /**
86
+ * Format documents as files list
87
+ */
88
+ export declare function documentsToFiles(results: MultiGetFile[]): string;
89
+ /**
90
+ * Format documents as Markdown
91
+ */
92
+ export declare function documentsToMarkdown(results: MultiGetFile[]): string;
93
+ /**
94
+ * Format documents as XML
95
+ */
96
+ export declare function documentsToXml(results: MultiGetFile[]): string;
97
+ /**
98
+ * Format a single DocumentResult as JSON
99
+ */
100
+ export declare function documentToJson(doc: DocumentResult): string;
101
+ /**
102
+ * Format a single DocumentResult as Markdown
103
+ */
104
+ export declare function documentToMarkdown(doc: DocumentResult): string;
105
+ /**
106
+ * Format a single DocumentResult as XML
107
+ */
108
+ export declare function documentToXml(doc: DocumentResult): string;
109
+ /**
110
+ * Format a single document to the specified format
111
+ */
112
+ export declare function formatDocument(doc: DocumentResult, format: OutputFormat): string;
113
+ /**
114
+ * Format search results to the specified output format
115
+ */
116
+ export declare function formatSearchResults(results: SearchResult[], format: OutputFormat, opts?: FormatOptions): string;
117
+ /**
118
+ * Format documents to the specified output format
119
+ */
120
+ export declare function formatDocuments(results: MultiGetFile[], format: OutputFormat): string;
@@ -0,0 +1,355 @@
1
+ /**
2
+ * formatter.ts - Output formatting utilities for QMD
3
+ *
4
+ * Provides methods to format search results and documents into various output formats:
5
+ * JSON, CSV, XML, Markdown, files list, and CLI (colored terminal output).
6
+ */
7
+ import { extractSnippet } from "../store.js";
8
+ // =============================================================================
9
+ // Helper Functions
10
+ // =============================================================================
11
+ /**
12
+ * Add line numbers to text content.
13
+ * Each line becomes: "{lineNum}: {content}"
14
+ * @param text The text to add line numbers to
15
+ * @param startLine Optional starting line number (default: 1)
16
+ */
17
+ export function addLineNumbers(text, startLine = 1) {
18
+ const lines = text.split('\n');
19
+ return lines.map((line, i) => `${startLine + i}: ${line}`).join('\n');
20
+ }
21
+ /**
22
+ * Extract short docid from a full hash (first 6 characters).
23
+ */
24
+ export function getDocid(hash) {
25
+ return hash.slice(0, 6);
26
+ }
27
+ // =============================================================================
28
+ // Escape Helpers
29
+ // =============================================================================
30
+ export function escapeCSV(value) {
31
+ if (value === null || value === undefined)
32
+ return "";
33
+ const str = String(value);
34
+ if (str.includes(",") || str.includes('"') || str.includes("\n")) {
35
+ return `"${str.replace(/"/g, '""')}"`;
36
+ }
37
+ return str;
38
+ }
39
+ export function escapeXml(str) {
40
+ return str
41
+ .replace(/&/g, "&")
42
+ .replace(/</g, "&lt;")
43
+ .replace(/>/g, "&gt;")
44
+ .replace(/"/g, "&quot;")
45
+ .replace(/'/g, "&apos;");
46
+ }
47
+ // =============================================================================
48
+ // Search Results Formatters
49
+ // =============================================================================
50
+ /**
51
+ * Format search results as JSON
52
+ */
53
+ export function searchResultsToJson(results, opts = {}) {
54
+ const query = opts.query || "";
55
+ const output = results.map(row => {
56
+ const bodyStr = row.body || "";
57
+ const snippetInfo = bodyStr
58
+ ? extractSnippet(bodyStr, query, 300, row.chunkPos, undefined, opts.intent)
59
+ : undefined;
60
+ let body = opts.full ? bodyStr : undefined;
61
+ let snippet = !opts.full ? snippetInfo?.snippet : undefined;
62
+ if (opts.lineNumbers) {
63
+ if (body)
64
+ body = addLineNumbers(body);
65
+ if (snippet)
66
+ snippet = addLineNumbers(snippet);
67
+ }
68
+ return {
69
+ docid: `#${row.docid}`,
70
+ score: Math.round(row.score * 100) / 100,
71
+ file: row.displayPath,
72
+ ...(snippetInfo && { line: snippetInfo.line }),
73
+ title: row.title,
74
+ ...(row.context && { context: row.context }),
75
+ ...(body && { body }),
76
+ ...(snippet && { snippet }),
77
+ };
78
+ });
79
+ return JSON.stringify(output, null, 2);
80
+ }
81
+ /**
82
+ * Format search results as CSV
83
+ */
84
+ export function searchResultsToCsv(results, opts = {}) {
85
+ const query = opts.query || "";
86
+ const header = "docid,score,file,title,context,line,snippet";
87
+ const rows = results.map(row => {
88
+ const bodyStr = row.body || "";
89
+ const { line, snippet } = extractSnippet(bodyStr, query, 500, row.chunkPos, undefined, opts.intent);
90
+ let content = opts.full ? bodyStr : snippet;
91
+ if (opts.lineNumbers && content) {
92
+ content = addLineNumbers(content);
93
+ }
94
+ return [
95
+ `#${row.docid}`,
96
+ row.score.toFixed(4),
97
+ escapeCSV(row.displayPath),
98
+ escapeCSV(row.title),
99
+ escapeCSV(row.context || ""),
100
+ line,
101
+ escapeCSV(content),
102
+ ].join(",");
103
+ });
104
+ return [header, ...rows].join("\n");
105
+ }
106
+ /**
107
+ * Format search results as simple files list (docid,score,filepath,context)
108
+ */
109
+ export function searchResultsToFiles(results) {
110
+ return results.map(row => {
111
+ const ctx = row.context ? `,"${row.context.replace(/"/g, '""')}"` : "";
112
+ return `#${row.docid},${row.score.toFixed(2)},${row.displayPath}${ctx}`;
113
+ }).join("\n");
114
+ }
115
+ /**
116
+ * Format search results as Markdown
117
+ */
118
+ export function searchResultsToMarkdown(results, opts = {}) {
119
+ const query = opts.query || "";
120
+ return results.map(row => {
121
+ const heading = row.title || row.displayPath;
122
+ const bodyStr = row.body || "";
123
+ let content;
124
+ if (opts.full) {
125
+ content = bodyStr;
126
+ }
127
+ else {
128
+ content = extractSnippet(bodyStr, query, 500, row.chunkPos, undefined, opts.intent).snippet;
129
+ }
130
+ if (opts.lineNumbers) {
131
+ content = addLineNumbers(content);
132
+ }
133
+ const fileLine = `**file:** \`${row.displayPath}\`\n`;
134
+ const contextLine = row.context ? `**context:** ${row.context}\n` : "";
135
+ return `---\n# ${heading}\n\n${fileLine}**docid:** \`#${row.docid}\`\n${contextLine}\n${content}\n`;
136
+ }).join("\n");
137
+ }
138
+ /**
139
+ * Format search results as XML
140
+ */
141
+ export function searchResultsToXml(results, opts = {}) {
142
+ const query = opts.query || "";
143
+ const items = results.map(row => {
144
+ const titleAttr = row.title ? ` title="${escapeXml(row.title)}"` : "";
145
+ const bodyStr = row.body || "";
146
+ let content = opts.full ? bodyStr : extractSnippet(bodyStr, query, 500, row.chunkPos, undefined, opts.intent).snippet;
147
+ if (opts.lineNumbers) {
148
+ content = addLineNumbers(content);
149
+ }
150
+ const contextAttr = row.context ? ` context="${escapeXml(row.context)}"` : "";
151
+ return `<file docid="#${row.docid}" name="${escapeXml(row.displayPath)}"${titleAttr}${contextAttr}>\n${escapeXml(content)}\n</file>`;
152
+ });
153
+ return items.join("\n\n");
154
+ }
155
+ /**
156
+ * Format search results for MCP (simpler CSV format with pre-extracted snippets)
157
+ */
158
+ export function searchResultsToMcpCsv(results) {
159
+ const header = "docid,file,title,score,context,snippet";
160
+ const rows = results.map(r => [`#${r.docid}`, r.file, r.title, r.score, r.context || "", r.snippet].map(escapeCSV).join(","));
161
+ return [header, ...rows].join("\n");
162
+ }
163
+ // =============================================================================
164
+ // Document Formatters (for multi-get using MultiGetFile from store)
165
+ // =============================================================================
166
+ /**
167
+ * Format documents as JSON
168
+ */
169
+ export function documentsToJson(results) {
170
+ const output = results.map(r => ({
171
+ file: r.displayPath,
172
+ title: r.title,
173
+ ...(r.context && { context: r.context }),
174
+ ...(r.skipped ? { skipped: true, reason: r.skipReason } : { body: r.body }),
175
+ }));
176
+ return JSON.stringify(output, null, 2);
177
+ }
178
+ /**
179
+ * Format documents as CSV
180
+ */
181
+ export function documentsToCsv(results) {
182
+ const header = "file,title,context,skipped,body";
183
+ const rows = results.map(r => [
184
+ r.displayPath,
185
+ r.title,
186
+ r.context || "",
187
+ r.skipped ? "true" : "false",
188
+ r.skipped ? (r.skipReason || "") : r.body
189
+ ].map(escapeCSV).join(","));
190
+ return [header, ...rows].join("\n");
191
+ }
192
+ /**
193
+ * Format documents as files list
194
+ */
195
+ export function documentsToFiles(results) {
196
+ return results.map(r => {
197
+ const ctx = r.context ? `,"${r.context.replace(/"/g, '""')}"` : "";
198
+ const status = r.skipped ? ",[SKIPPED]" : "";
199
+ return `${r.displayPath}${ctx}${status}`;
200
+ }).join("\n");
201
+ }
202
+ /**
203
+ * Format documents as Markdown
204
+ */
205
+ export function documentsToMarkdown(results) {
206
+ return results.map(r => {
207
+ let md = `## ${r.displayPath}\n\n`;
208
+ if (r.title && r.title !== r.displayPath)
209
+ md += `**Title:** ${r.title}\n\n`;
210
+ if (r.context)
211
+ md += `**Context:** ${r.context}\n\n`;
212
+ if (r.skipped) {
213
+ md += `> ${r.skipReason}\n`;
214
+ }
215
+ else {
216
+ md += "```\n" + r.body + "\n```\n";
217
+ }
218
+ return md;
219
+ }).join("\n");
220
+ }
221
+ /**
222
+ * Format documents as XML
223
+ */
224
+ export function documentsToXml(results) {
225
+ const items = results.map(r => {
226
+ let xml = " <document>\n";
227
+ xml += ` <file>${escapeXml(r.displayPath)}</file>\n`;
228
+ xml += ` <title>${escapeXml(r.title)}</title>\n`;
229
+ if (r.context)
230
+ xml += ` <context>${escapeXml(r.context)}</context>\n`;
231
+ if (r.skipped) {
232
+ xml += ` <skipped>true</skipped>\n`;
233
+ xml += ` <reason>${escapeXml(r.skipReason || "")}</reason>\n`;
234
+ }
235
+ else {
236
+ xml += ` <body>${escapeXml(r.body)}</body>\n`;
237
+ }
238
+ xml += " </document>";
239
+ return xml;
240
+ });
241
+ return `<?xml version="1.0" encoding="UTF-8"?>\n<documents>\n${items.join("\n")}\n</documents>`;
242
+ }
243
+ // =============================================================================
244
+ // Single Document Formatters
245
+ // =============================================================================
246
+ /**
247
+ * Format a single DocumentResult as JSON
248
+ */
249
+ export function documentToJson(doc) {
250
+ return JSON.stringify({
251
+ file: doc.displayPath,
252
+ title: doc.title,
253
+ ...(doc.context && { context: doc.context }),
254
+ hash: doc.hash,
255
+ modifiedAt: doc.modifiedAt,
256
+ bodyLength: doc.bodyLength,
257
+ ...(doc.body !== undefined && { body: doc.body }),
258
+ }, null, 2);
259
+ }
260
+ /**
261
+ * Format a single DocumentResult as Markdown
262
+ */
263
+ export function documentToMarkdown(doc) {
264
+ let md = `# ${doc.title || doc.displayPath}\n\n`;
265
+ if (doc.context)
266
+ md += `**Context:** ${doc.context}\n\n`;
267
+ md += `**File:** ${doc.displayPath}\n`;
268
+ md += `**Modified:** ${doc.modifiedAt}\n\n`;
269
+ if (doc.body !== undefined) {
270
+ md += "---\n\n" + doc.body + "\n";
271
+ }
272
+ return md;
273
+ }
274
+ /**
275
+ * Format a single DocumentResult as XML
276
+ */
277
+ export function documentToXml(doc) {
278
+ let xml = `<?xml version="1.0" encoding="UTF-8"?>\n<document>\n`;
279
+ xml += ` <file>${escapeXml(doc.displayPath)}</file>\n`;
280
+ xml += ` <title>${escapeXml(doc.title)}</title>\n`;
281
+ if (doc.context)
282
+ xml += ` <context>${escapeXml(doc.context)}</context>\n`;
283
+ xml += ` <hash>${escapeXml(doc.hash)}</hash>\n`;
284
+ xml += ` <modifiedAt>${escapeXml(doc.modifiedAt)}</modifiedAt>\n`;
285
+ xml += ` <bodyLength>${doc.bodyLength}</bodyLength>\n`;
286
+ if (doc.body !== undefined) {
287
+ xml += ` <body>${escapeXml(doc.body)}</body>\n`;
288
+ }
289
+ xml += `</document>`;
290
+ return xml;
291
+ }
292
+ /**
293
+ * Format a single document to the specified format
294
+ */
295
+ export function formatDocument(doc, format) {
296
+ switch (format) {
297
+ case "json":
298
+ return documentToJson(doc);
299
+ case "md":
300
+ return documentToMarkdown(doc);
301
+ case "xml":
302
+ return documentToXml(doc);
303
+ default:
304
+ // Default to markdown for CLI and other formats
305
+ return documentToMarkdown(doc);
306
+ }
307
+ }
308
+ // =============================================================================
309
+ // Universal Format Function
310
+ // =============================================================================
311
+ /**
312
+ * Format search results to the specified output format
313
+ */
314
+ export function formatSearchResults(results, format, opts = {}) {
315
+ switch (format) {
316
+ case "json":
317
+ return searchResultsToJson(results, opts);
318
+ case "csv":
319
+ return searchResultsToCsv(results, opts);
320
+ case "files":
321
+ return searchResultsToFiles(results);
322
+ case "md":
323
+ return searchResultsToMarkdown(results, opts);
324
+ case "xml":
325
+ return searchResultsToXml(results, opts);
326
+ case "cli":
327
+ // CLI format should be handled separately with colors
328
+ // Return a simple text version as fallback
329
+ return searchResultsToMarkdown(results, opts);
330
+ default:
331
+ return searchResultsToJson(results, opts);
332
+ }
333
+ }
334
+ /**
335
+ * Format documents to the specified output format
336
+ */
337
+ export function formatDocuments(results, format) {
338
+ switch (format) {
339
+ case "json":
340
+ return documentsToJson(results);
341
+ case "csv":
342
+ return documentsToCsv(results);
343
+ case "files":
344
+ return documentsToFiles(results);
345
+ case "md":
346
+ return documentsToMarkdown(results);
347
+ case "xml":
348
+ return documentsToXml(results);
349
+ case "cli":
350
+ // CLI format should be handled separately with colors
351
+ return documentsToMarkdown(results);
352
+ default:
353
+ return documentsToJson(results);
354
+ }
355
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * MCP daemon pidfile identity helpers.
3
+ *
4
+ * Pidfiles alone are unsafe after PID reuse (e.g. post-reboot). Callers must
5
+ * confirm a recorded PID still belongs to a qmd process before signalling it
6
+ * or treating it as "already running".
7
+ */
8
+ /**
9
+ * Pid/log filenames for the MCP HTTP daemon.
10
+ * The default index keeps `mcp.pid` / `mcp.log` for compatibility; named
11
+ * indexes are scoped so a named daemon can run alongside the default (#772).
12
+ */
13
+ export declare function mcpDaemonStateFiles(indexName?: string): {
14
+ pidFile: string;
15
+ logFile: string;
16
+ };
17
+ /** True if a process command line looks like a qmd CLI invocation. */
18
+ export declare function looksLikeQmdMcpCommand(cmdline: string): boolean;
19
+ /** Read process cmdline (Linux /proc preferred; ps fallback for macOS). */
20
+ export declare function readProcessCmdline(pid: number): string | null;
21
+ /**
22
+ * Returns true only if `pid` is alive AND its command line looks like qmd.
23
+ * If cmdline cannot be read or does not match, returns false (treat as stale).
24
+ */
25
+ export declare function isQmdMcpPid(pid: number): boolean;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * MCP daemon pidfile identity helpers.
3
+ *
4
+ * Pidfiles alone are unsafe after PID reuse (e.g. post-reboot). Callers must
5
+ * confirm a recorded PID still belongs to a qmd process before signalling it
6
+ * or treating it as "already running".
7
+ */
8
+ import { existsSync, readFileSync } from "node:fs";
9
+ import { execFileSync } from "node:child_process";
10
+ /**
11
+ * Pid/log filenames for the MCP HTTP daemon.
12
+ * The default index keeps `mcp.pid` / `mcp.log` for compatibility; named
13
+ * indexes are scoped so a named daemon can run alongside the default (#772).
14
+ */
15
+ export function mcpDaemonStateFiles(indexName = "index") {
16
+ const suffix = !indexName || indexName === "index" ? "" : `-${indexName}`;
17
+ return {
18
+ pidFile: `mcp${suffix}.pid`,
19
+ logFile: `mcp${suffix}.log`,
20
+ };
21
+ }
22
+ /** True if a process command line looks like a qmd CLI invocation. */
23
+ export function looksLikeQmdMcpCommand(cmdline) {
24
+ const s = cmdline.trim();
25
+ if (!s)
26
+ return false;
27
+ // Match bare `qmd`, `qmd.ts`/`qmd.js`, or a path ending in /qmd(.ts|.js)
28
+ return /(?:^|[\s/\\])qmd(?:\.(?:ts|js))?(?:[\s]|$)/i.test(s);
29
+ }
30
+ /** Read process cmdline (Linux /proc preferred; ps fallback for macOS). */
31
+ export function readProcessCmdline(pid) {
32
+ if (!Number.isInteger(pid) || pid <= 0)
33
+ return null;
34
+ const procPath = `/proc/${pid}/cmdline`;
35
+ if (existsSync(procPath)) {
36
+ try {
37
+ const raw = readFileSync(procPath, "utf-8");
38
+ const cmdline = raw.replace(/\0/g, " ").trim();
39
+ if (cmdline)
40
+ return cmdline;
41
+ }
42
+ catch {
43
+ // fall through to ps
44
+ }
45
+ }
46
+ try {
47
+ let cmdline = "";
48
+ try {
49
+ cmdline = execFileSync("ps", ["-p", String(pid), "-o", "args="], {
50
+ encoding: "utf-8",
51
+ timeout: 2000,
52
+ stdio: ["ignore", "pipe", "ignore"],
53
+ });
54
+ }
55
+ catch {
56
+ cmdline = execFileSync("ps", ["-p", String(pid), "-o", "command="], {
57
+ encoding: "utf-8",
58
+ timeout: 2000,
59
+ stdio: ["ignore", "pipe", "ignore"],
60
+ });
61
+ }
62
+ const trimmed = cmdline.trim();
63
+ return trimmed || null;
64
+ }
65
+ catch {
66
+ return null;
67
+ }
68
+ }
69
+ /**
70
+ * Returns true only if `pid` is alive AND its command line looks like qmd.
71
+ * If cmdline cannot be read or does not match, returns false (treat as stale).
72
+ */
73
+ export function isQmdMcpPid(pid) {
74
+ if (!Number.isInteger(pid) || pid <= 0)
75
+ return false;
76
+ try {
77
+ process.kill(pid, 0);
78
+ }
79
+ catch {
80
+ return false;
81
+ }
82
+ const cmdline = readProcessCmdline(pid);
83
+ if (!cmdline)
84
+ return false;
85
+ return looksLikeQmdMcpCommand(cmdline);
86
+ }
@@ -0,0 +1,43 @@
1
+ import { type OutputFormat } from "./formatter.js";
2
+ type CliLifecycleWritable = {
3
+ write(chunk: string | Uint8Array, callback?: (error?: Error | null) => void): boolean;
4
+ };
5
+ type FinishSuccessfulCliCommandOptions = {
6
+ command: string;
7
+ format?: OutputFormat;
8
+ cleanup?: () => Promise<void>;
9
+ exit?: (code: number) => void;
10
+ stdout?: CliLifecycleWritable;
11
+ stderr?: CliLifecycleWritable;
12
+ };
13
+ /**
14
+ * Finish a successful CLI command after output has been flushed.
15
+ *
16
+ * We deliberately do NOT call `process.exit(0)`. `process.exit()` skips
17
+ * Node's `beforeExit` event, and node-llama-cpp registers a `beforeExit` hook
18
+ * that auto-disposes its native handles. On darwin, without that hook firing,
19
+ * libggml-metal's static `ggml_metal_device` destructor asserts on a
20
+ * non-empty residency-set collection during `__cxa_finalize_ranges` and
21
+ * dumps a multi-kB backtrace (upstream ggml-org/llama.cpp#22593, fix open as
22
+ * PR #22595). Empirically, even with explicit `disposeDefaultLlamaCpp()` the
23
+ * direct `process.exit(0)` path still trips the assertion — letting the
24
+ * event loop drain naturally is what actually clears the rsets.
25
+ *
26
+ * So: set `process.exitCode = 0` and return. The main module finishes, the
27
+ * event loop drains, `beforeExit` fires, native resources tear down in
28
+ * order, and the process exits cleanly. The `GGML_METAL_NO_RESIDENCY=1` env
29
+ * var that `bin/qmd` exports is a defense-in-depth safety net for paths
30
+ * that still call `process.exit()` after loading the native binding
31
+ * (signal handlers, error paths, `bun test`).
32
+ *
33
+ * If the caller passes an explicit `exit` for testability, we honor it —
34
+ * the lifecycle tests verify the legacy flush → cleanup → exit ordering.
35
+ * Production callers must not pass `exit`.
36
+ */
37
+ export declare function finishSuccessfulCliCommand(options: FinishSuccessfulCliCommandOptions): Promise<void>;
38
+ export declare function resolveEmbedModelForCli(): string;
39
+ export declare function resolveGenerateModelForCli(): string;
40
+ export declare function resolveRerankModelForCli(): string;
41
+ export declare function buildEditorUri(template: string, absolutePath: string, line: number, col: number): string;
42
+ export declare function termLink(text: string, url: string, isTTY?: boolean): string;
43
+ export {};