mustflow 2.39.1 → 2.58.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.
Files changed (94) hide show
  1. package/dist/cli/commands/run/executor.js +16 -0
  2. package/dist/cli/commands/run/process-tree.js +6 -3
  3. package/dist/cli/commands/tech.js +60 -4
  4. package/dist/cli/commands/verify.js +3 -1
  5. package/dist/cli/lib/git-changes.js +11 -1
  6. package/dist/cli/lib/i18n.js +1 -1
  7. package/dist/cli/lib/local-index/index.js +8 -4
  8. package/dist/cli/lib/local-index/populate.js +17 -3
  9. package/dist/cli/lib/local-index/search-read-model.js +9 -7
  10. package/dist/cli/lib/local-index/search-text.js +2 -2
  11. package/dist/cli/lib/local-index/workflow-documents.js +17 -2
  12. package/dist/cli/lib/mustflow-read.js +14 -2
  13. package/dist/cli/lib/npm-version-check.js +36 -0
  14. package/dist/cli/lib/repo-map.js +16 -3
  15. package/dist/cli/lib/templates.js +8 -7
  16. package/dist/cli/lib/validation/constants.js +1 -1
  17. package/dist/core/active-run-locks.js +78 -20
  18. package/dist/core/change-classification.js +4 -0
  19. package/dist/core/command-contract-rules.js +1 -1
  20. package/dist/core/command-contract-validation.js +1 -1
  21. package/dist/core/command-cwd.js +13 -2
  22. package/dist/core/command-effects.js +22 -4
  23. package/dist/core/command-env.js +8 -6
  24. package/dist/core/command-preconditions.js +28 -2
  25. package/dist/core/completion-verdict.js +1 -1
  26. package/dist/core/line-endings.js +8 -4
  27. package/dist/core/safe-filesystem.js +9 -1
  28. package/dist/core/source-anchor-validation.js +7 -1
  29. package/dist/core/source-anchors.js +8 -2
  30. package/dist/core/verification-scheduler.js +8 -2
  31. package/package.json +1 -1
  32. package/templates/default/i18n.toml +330 -1
  33. package/templates/default/locales/en/.mustflow/skills/INDEX.md +302 -5
  34. package/templates/default/locales/en/.mustflow/skills/agent-eval-integrity-review/SKILL.md +160 -0
  35. package/templates/default/locales/en/.mustflow/skills/agent-execution-control-review/SKILL.md +163 -0
  36. package/templates/default/locales/en/.mustflow/skills/ai-generated-code-hardening/SKILL.md +49 -13
  37. package/templates/default/locales/en/.mustflow/skills/api-access-control-review/SKILL.md +298 -0
  38. package/templates/default/locales/en/.mustflow/skills/api-misuse-resistance-review/SKILL.md +297 -0
  39. package/templates/default/locales/en/.mustflow/skills/api-request-performance-review/SKILL.md +189 -0
  40. package/templates/default/locales/en/.mustflow/skills/app-startup-performance-review/SKILL.md +309 -0
  41. package/templates/default/locales/en/.mustflow/skills/backend-log-evidence-review/SKILL.md +213 -0
  42. package/templates/default/locales/en/.mustflow/skills/business-rule-leakage-review/SKILL.md +295 -0
  43. package/templates/default/locales/en/.mustflow/skills/cache-integrity-review/SKILL.md +291 -0
  44. package/templates/default/locales/en/.mustflow/skills/change-blast-radius-review/SKILL.md +297 -0
  45. package/templates/default/locales/en/.mustflow/skills/client-bundle-pruning-review/SKILL.md +160 -0
  46. package/templates/default/locales/en/.mustflow/skills/cloud-cost-guardrail-review/SKILL.md +321 -0
  47. package/templates/default/locales/en/.mustflow/skills/concurrency-invariant-review/SKILL.md +193 -0
  48. package/templates/default/locales/en/.mustflow/skills/core-web-vitals-field-review/SKILL.md +161 -0
  49. package/templates/default/locales/en/.mustflow/skills/credit-ledger-integrity-review/SKILL.md +156 -0
  50. package/templates/default/locales/en/.mustflow/skills/database-json-modeling-review/SKILL.md +171 -0
  51. package/templates/default/locales/en/.mustflow/skills/database-lock-contention-review/SKILL.md +192 -0
  52. package/templates/default/locales/en/.mustflow/skills/database-migration-change/SKILL.md +76 -34
  53. package/templates/default/locales/en/.mustflow/skills/database-query-bottleneck-review/SKILL.md +194 -0
  54. package/templates/default/locales/en/.mustflow/skills/deletion-lifecycle-review/SKILL.md +171 -0
  55. package/templates/default/locales/en/.mustflow/skills/deployment-rollout-safety-review/SKILL.md +321 -0
  56. package/templates/default/locales/en/.mustflow/skills/desktop-auto-update-safety-review/SKILL.md +265 -0
  57. package/templates/default/locales/en/.mustflow/skills/desktop-background-process-stability-review/SKILL.md +318 -0
  58. package/templates/default/locales/en/.mustflow/skills/desktop-memory-footprint-review/SKILL.md +318 -0
  59. package/templates/default/locales/en/.mustflow/skills/error-message-integrity-review/SKILL.md +283 -0
  60. package/templates/default/locales/en/.mustflow/skills/failure-integrity-review/SKILL.md +193 -0
  61. package/templates/default/locales/en/.mustflow/skills/file-upload-security-review/SKILL.md +305 -0
  62. package/templates/default/locales/en/.mustflow/skills/frame-render-performance-review/SKILL.md +159 -0
  63. package/templates/default/locales/en/.mustflow/skills/frontend-accessibility-tree-review/SKILL.md +202 -0
  64. package/templates/default/locales/en/.mustflow/skills/frontend-localization-review/SKILL.md +202 -0
  65. package/templates/default/locales/en/.mustflow/skills/frontend-state-ownership-review/SKILL.md +183 -0
  66. package/templates/default/locales/en/.mustflow/skills/frontend-stress-layout-review/SKILL.md +193 -0
  67. package/templates/default/locales/en/.mustflow/skills/hot-path-performance-review/SKILL.md +159 -0
  68. package/templates/default/locales/en/.mustflow/skills/idempotency-integrity-review/SKILL.md +195 -0
  69. package/templates/default/locales/en/.mustflow/skills/image-delivery-performance-review/SKILL.md +161 -0
  70. package/templates/default/locales/en/.mustflow/skills/incident-triage-review/SKILL.md +185 -0
  71. package/templates/default/locales/en/.mustflow/skills/llm-hallucination-control-review/SKILL.md +155 -0
  72. package/templates/default/locales/en/.mustflow/skills/llm-response-latency-review/SKILL.md +155 -0
  73. package/templates/default/locales/en/.mustflow/skills/llm-service-ux-review/SKILL.md +2 -0
  74. package/templates/default/locales/en/.mustflow/skills/llm-token-cost-control-review/SKILL.md +155 -0
  75. package/templates/default/locales/en/.mustflow/skills/low-end-device-support-review/SKILL.md +340 -0
  76. package/templates/default/locales/en/.mustflow/skills/memory-lifetime-review/SKILL.md +169 -0
  77. package/templates/default/locales/en/.mustflow/skills/mobile-energy-efficiency-review/SKILL.md +329 -0
  78. package/templates/default/locales/en/.mustflow/skills/module-boundary-review/SKILL.md +278 -0
  79. package/templates/default/locales/en/.mustflow/skills/multi-agent-work-coordination/SKILL.md +1 -0
  80. package/templates/default/locales/en/.mustflow/skills/observability-debuggability-review/SKILL.md +208 -0
  81. package/templates/default/locales/en/.mustflow/skills/payment-integrity-review/SKILL.md +155 -0
  82. package/templates/default/locales/en/.mustflow/skills/prompt-contract-quality-review/SKILL.md +158 -0
  83. package/templates/default/locales/en/.mustflow/skills/quadratic-scan-review/SKILL.md +155 -0
  84. package/templates/default/locales/en/.mustflow/skills/queue-processing-integrity-review/SKILL.md +193 -0
  85. package/templates/default/locales/en/.mustflow/skills/race-condition-review/SKILL.md +188 -0
  86. package/templates/default/locales/en/.mustflow/skills/rate-limit-integrity-review/SKILL.md +344 -0
  87. package/templates/default/locales/en/.mustflow/skills/retry-policy-integrity-review/SKILL.md +195 -0
  88. package/templates/default/locales/en/.mustflow/skills/routes.toml +330 -0
  89. package/templates/default/locales/en/.mustflow/skills/security-flow-review/SKILL.md +279 -0
  90. package/templates/default/locales/en/.mustflow/skills/testability-boundary-review/SKILL.md +295 -0
  91. package/templates/default/locales/en/.mustflow/skills/transaction-boundary-integrity-review/SKILL.md +196 -0
  92. package/templates/default/locales/en/.mustflow/skills/type-state-modeling-review/SKILL.md +179 -0
  93. package/templates/default/locales/en/.mustflow/skills/web-render-performance-review/SKILL.md +164 -0
  94. package/templates/default/manifest.toml +386 -1
@@ -3,7 +3,23 @@ import { BoundedOutputBuffer } from '../../../core/bounded-output.js';
3
3
  import { createPendingTimeoutTermination, forceTerminateProcessTree, getKillMethod, terminateProcessTree, } from './process-tree.js';
4
4
  import { createOutputLimitError, isOutputLimitExceededError, writeOutputLimitTerminationMarker, writeStreamChunk, writeStreamChunkPrefix, } from './output.js';
5
5
  const TERMINATION_CONFIRMATION_FALLBACK_MS = 1000;
6
+ function createEmptyOutputSnapshot(maxBytes) {
7
+ return new BoundedOutputBuffer(maxBytes).toSnapshot();
8
+ }
9
+ function createInvalidExecutableError() {
10
+ return Object.assign(new Error('Command executable must not be empty'), { code: 'EINVAL' });
11
+ }
6
12
  function runSpawnedCommandStreaming(command, cwd, env, timeoutSeconds, killAfterSeconds, maxOutputBytes, stdoutTailBytes, stderrTailBytes, reporter, streamOutput, enforceOutputLimit) {
13
+ if (command.executable.trim().length === 0) {
14
+ return Promise.resolve({
15
+ status: null,
16
+ signal: null,
17
+ error: createInvalidExecutableError(),
18
+ stdout: createEmptyOutputSnapshot(stdoutTailBytes),
19
+ stderr: createEmptyOutputSnapshot(stderrTailBytes),
20
+ termination: null,
21
+ });
22
+ }
7
23
  return new Promise((resolve) => {
8
24
  const stdout = new BoundedOutputBuffer(stdoutTailBytes);
9
25
  const stderr = new BoundedOutputBuffer(stderrTailBytes);
@@ -1,10 +1,13 @@
1
1
  import { spawn, spawnSync } from 'node:child_process';
2
+ function windowsTaskkillArgs(pid, signal) {
3
+ return signal === 'SIGKILL' ? ['/PID', String(pid), '/T', '/F'] : ['/PID', String(pid), '/T'];
4
+ }
2
5
  function signalProcessTree(pid, signal) {
3
6
  if (!pid || pid <= 0) {
4
7
  return;
5
8
  }
6
9
  if (process.platform === 'win32') {
7
- spawnSync('taskkill', ['/PID', String(pid), '/T', '/F'], {
10
+ spawnSync('taskkill', windowsTaskkillArgs(pid, signal), {
8
11
  stdio: 'ignore',
9
12
  windowsHide: true,
10
13
  });
@@ -35,7 +38,7 @@ function signalProcessTreeNonBlocking(pid, signal) {
35
38
  return;
36
39
  }
37
40
  if (process.platform === 'win32') {
38
- const killer = spawn('taskkill', ['/PID', String(pid), '/T', '/F'], {
41
+ const killer = spawn('taskkill', windowsTaskkillArgs(pid, signal), {
39
42
  stdio: 'ignore',
40
43
  windowsHide: true,
41
44
  detached: true,
@@ -76,7 +79,7 @@ export function forceTerminateProcessTreeNonBlocking(pid) {
76
79
  signalProcessTreeNonBlocking(pid, 'SIGKILL');
77
80
  }
78
81
  export function getKillMethod() {
79
- return process.platform === 'win32' ? 'taskkill_process_tree_forced' : 'process_group_sigterm';
82
+ return process.platform === 'win32' ? 'taskkill_process_tree' : 'process_group_sigterm';
80
83
  }
81
84
  export function createPendingTimeoutTermination(method, forcedKillAttempted = false) {
82
85
  return {
@@ -3,6 +3,7 @@ import { printUsageError, renderCliError, renderHelp } from '../lib/cli-output.j
3
3
  import { writeUtf8FileInsideWithoutSymlinks } from '../lib/filesystem.js';
4
4
  import { isRecord } from '../lib/command-contract.js';
5
5
  import { t } from '../lib/i18n.js';
6
+ import { checkNpmPackageExists } from '../lib/npm-version-check.js';
6
7
  import { formatCliOptionParseError, getParsedCliStringOption, hasCliOptionToken, hasParsedCliOption, parseCliOptions, } from '../lib/option-parser.js';
7
8
  import { resolveMustflowRoot } from '../lib/project-root.js';
8
9
  import { readMustflowTomlFile } from '../lib/toml.js';
@@ -14,6 +15,7 @@ const TECH_OPTIONS = [
14
15
  { name: '--status', kind: 'string' },
15
16
  { name: '--ecosystem', kind: 'string' },
16
17
  { name: '--package', kind: 'string' },
18
+ { name: '--verify', kind: 'boolean' },
17
19
  { name: '--why', kind: 'string' },
18
20
  { name: '--constraint', kind: 'string' },
19
21
  ];
@@ -40,13 +42,14 @@ export function getTechHelp(lang = 'en') {
40
42
  { label: '--status <status>', description: `Preference status: ${TECHNOLOGY_STATUSES.join(', ')}` },
41
43
  { label: '--ecosystem <ecosystem>', description: 'Package ecosystem or platform, such as npm, cargo, pip, go, or deno' },
42
44
  { label: '--package <package>', description: 'Package name to associate with the preference. Repeatable.' },
45
+ { label: '--verify', description: 'Verify listed npm packages exist before writing the preference' },
43
46
  { label: '--why <text>', description: 'Short rationale for the preference' },
44
47
  { label: '--constraint <text>', description: 'Guardrail agents must keep in mind. Repeatable.' },
45
48
  { label: '-h, --help', description: t(lang, 'cli.option.help') },
46
49
  ],
47
50
  examples: [
48
51
  'mf tech list',
49
- 'mf tech add framework nextjs --scope frontend --ecosystem npm --package next --package react --why "Preferred React app framework"',
52
+ 'mf tech add framework nextjs --scope frontend --ecosystem npm --package next --package react --verify --why "Preferred React app framework"',
50
53
  'mf tech add language rust --scope backend --status preferred --why "Use for correctness-critical engines"',
51
54
  'mf tech add library jquery --scope frontend --status avoid --why "Avoid new usage"',
52
55
  'mf tech suggest --scope frontend',
@@ -71,6 +74,7 @@ function parseTechOptions(args, lang) {
71
74
  status: null,
72
75
  ecosystem: null,
73
76
  packages: [],
77
+ verify: false,
74
78
  why: null,
75
79
  constraints: [],
76
80
  error: actionToken ? `Unknown tech action: ${actionToken}` : 'Specify a tech action: list, add, remove, or suggest',
@@ -87,6 +91,7 @@ function parseTechOptions(args, lang) {
87
91
  status: null,
88
92
  ecosystem: null,
89
93
  packages: [],
94
+ verify: hasParsedCliOption(parsed, '--verify'),
90
95
  why: null,
91
96
  constraints: [],
92
97
  error: formatCliOptionParseError(parsed.error, lang),
@@ -109,6 +114,7 @@ function parseTechOptions(args, lang) {
109
114
  status,
110
115
  ecosystem: getParsedCliStringOption(parsed, '--ecosystem'),
111
116
  packages: getRepeatedStringOptions(parsed.occurrences, '--package'),
117
+ verify: hasParsedCliOption(parsed, '--verify'),
112
118
  why: getParsedCliStringOption(parsed, '--why'),
113
119
  constraints: getRepeatedStringOptions(parsed.occurrences, '--constraint'),
114
120
  };
@@ -123,6 +129,7 @@ function invalidParsed(action, positionals, json, error) {
123
129
  status: null,
124
130
  ecosystem: null,
125
131
  packages: [],
132
+ verify: false,
126
133
  why: null,
127
134
  constraints: [],
128
135
  error,
@@ -239,7 +246,44 @@ function findExistingIndex(preferences, candidate) {
239
246
  return preference.kind === candidate.kind && normalizeTechnologyKey(preference.name) === normalizeTechnologyKey(candidate.name);
240
247
  });
241
248
  }
242
- function runAdd(projectRoot, options, reporter) {
249
+ async function verifyNpmPackages(options, reporter) {
250
+ if (!options.verify) {
251
+ return [];
252
+ }
253
+ if (options.action !== 'add') {
254
+ reporter.stderr(renderCliError('--verify is only supported with mf tech add', 'mf tech --help'));
255
+ return null;
256
+ }
257
+ if (options.packages.length === 0) {
258
+ reporter.stderr(renderCliError('--verify requires at least one --package value', 'mf tech --help'));
259
+ return null;
260
+ }
261
+ if (options.ecosystem !== 'npm') {
262
+ reporter.stderr(renderCliError('--verify currently supports --ecosystem npm only', 'mf tech --help'));
263
+ return null;
264
+ }
265
+ const checks = [];
266
+ for (const packageName of options.packages) {
267
+ try {
268
+ const check = await checkNpmPackageExists(packageName);
269
+ if (!check.exists) {
270
+ reporter.stderr(renderCliError(`npm package not found: ${packageName}`, 'mf tech --help'));
271
+ return null;
272
+ }
273
+ checks.push(check);
274
+ }
275
+ catch (error) {
276
+ const message = error instanceof Error ? error.message : String(error);
277
+ reporter.stderr(renderCliError(`Could not verify npm package ${packageName}: ${message}`, 'mf tech --help'));
278
+ return null;
279
+ }
280
+ }
281
+ return checks;
282
+ }
283
+ function renderVerifiedPackageNames(checks) {
284
+ return checks.map((check) => check.resolvedName ?? check.packageName).join(', ');
285
+ }
286
+ async function runAdd(projectRoot, options, reporter) {
243
287
  const [kindToken, nameToken] = options.positionals;
244
288
  if (!isTechnologyKind(kindToken ?? null)) {
245
289
  reporter.stderr(renderCliError('Missing or unsupported technology kind', 'mf tech --help'));
@@ -277,6 +321,10 @@ function runAdd(projectRoot, options, reporter) {
277
321
  const preferences = [...file.preferences];
278
322
  const existingIndex = findExistingIndex(preferences, candidate);
279
323
  const action = existingIndex === -1 ? 'created' : 'updated';
324
+ const verifiedPackages = await verifyNpmPackages(options, reporter);
325
+ if (verifiedPackages === null) {
326
+ return 1;
327
+ }
280
328
  if (existingIndex === -1) {
281
329
  preferences.push(candidate);
282
330
  }
@@ -285,9 +333,17 @@ function runAdd(projectRoot, options, reporter) {
285
333
  }
286
334
  writeTechnologyPreferences(projectRoot, preferences);
287
335
  if (options.json) {
288
- reporter.stdout(JSON.stringify({ action, preference: candidate, path: TECHNOLOGY_CONFIG_RELATIVE_PATH }, null, 2));
336
+ reporter.stdout(JSON.stringify({
337
+ action,
338
+ preference: candidate,
339
+ path: TECHNOLOGY_CONFIG_RELATIVE_PATH,
340
+ verified_packages: verifiedPackages,
341
+ }, null, 2));
289
342
  return 0;
290
343
  }
344
+ if (verifiedPackages.length > 0) {
345
+ reporter.stdout(`Verified npm packages: ${renderVerifiedPackageNames(verifiedPackages)}`);
346
+ }
291
347
  reporter.stdout(`${action === 'created' ? 'Created' : 'Updated'} ${candidate.id} in ${TECHNOLOGY_CONFIG_RELATIVE_PATH}`);
292
348
  return 0;
293
349
  }
@@ -322,7 +378,7 @@ function runRemove(projectRoot, options, reporter) {
322
378
  reporter.stdout(`Removed ${removed.id} from ${TECHNOLOGY_CONFIG_RELATIVE_PATH}`);
323
379
  return 0;
324
380
  }
325
- export function runTech(args, reporter, lang = 'en') {
381
+ export async function runTech(args, reporter, lang = 'en') {
326
382
  if (hasCliOptionToken(args, '--help', ['-h'])) {
327
383
  reporter.stdout(getTechHelp(lang));
328
384
  return 0;
@@ -206,10 +206,11 @@ async function runVerificationEntriesInParallelChunks(projectRoot, entries, para
206
206
  const chunk = entries.slice(index, index + parallelism);
207
207
  const batchTracker = startRunWriteBatchTracking(projectRoot, createCommandEnv(projectRoot, { policy: 'minimal', allowlist: [] }));
208
208
  const chunkResults = await Promise.all(chunk.map((entry) => runVerificationIntent(entry.intent, lang, verificationPlanId, correlationId, scheduledTestTargets.get(entry.intent) ?? [])));
209
+ const chunkResultsByIntent = new Map(chunkResults.map((result) => [result.intent, result]));
209
210
  const writeDriftByIntent = finishRunWriteBatchTracking(batchTracker, chunk.map((entry) => ({
210
211
  intentName: entry.intent,
211
212
  declaredPaths: declaredWritePathsForScheduleEntry(entry),
212
- observedPaths: observedWriteDriftPaths(chunkResults.find((result) => result.intent === entry.intent)),
213
+ observedPaths: observedWriteDriftPaths(chunkResultsByIntent.get(entry.intent)),
213
214
  })));
214
215
  results.push(...chunkResults.map((result) => applyParallelChunkWriteDrift(result, writeDriftByIntent)));
215
216
  }
@@ -493,6 +494,7 @@ function createCompletionVerdictForResults(input) {
493
494
  failedIntents: input.summary.failed,
494
495
  skippedIntents: input.summary.skipped,
495
496
  receiptCount: input.results.filter((result) => result.receipt !== null).length,
497
+ gapCount: input.report.gaps.length,
496
498
  sourceAnchorRiskCount: input.sourceAnchorRiskCount,
497
499
  scopeDiffRiskCount: input.scopeDiffRiskCount,
498
500
  repeatedFailureCount: input.repeatedFailureRisks.length,
@@ -33,7 +33,17 @@ export function readGitChangedFiles(projectRoot) {
33
33
  stderr,
34
34
  };
35
35
  }
36
- return { ok: true, files: parseGitStatusOutput(result.stdout) };
36
+ try {
37
+ return { ok: true, files: parseGitStatusOutput(result.stdout) };
38
+ }
39
+ catch (error) {
40
+ return {
41
+ ok: false,
42
+ message: error instanceof Error ? error.message : String(error),
43
+ status: result.status,
44
+ stderr: '',
45
+ };
46
+ }
37
47
  }
38
48
  export function requireGitChangedFiles(projectRoot) {
39
49
  const result = readGitChangedFiles(projectRoot);
@@ -24,7 +24,7 @@ function interpolate(template, params) {
24
24
  });
25
25
  }
26
26
  export function t(lang, key, params = {}) {
27
- return interpolate(MESSAGE_CATALOGS[lang][key] ?? enMessages[key], params);
27
+ return interpolate(MESSAGE_CATALOGS[lang][key] ?? enMessages[key] ?? key, params);
28
28
  }
29
29
  export function localeMessage(locale, key, params = {}) {
30
30
  return t(isCliLang(locale) ? locale : DEFAULT_CLI_LANG, key, params);
@@ -310,10 +310,14 @@ export async function createLocalIndex(projectRoot, options = {}) {
310
310
  }
311
311
  if (!dryRun && !reusedExisting) {
312
312
  const database = new SQL.Database();
313
- createSchema(database, capabilities);
314
- populateDatabase(database, capabilities, documents, skills, skillRoutes, commandIntents, sourceAnchors, indexedFiles, verificationEvidence, indexMode, sourceScopeHash, includeSource, new Date().toISOString());
315
- writeFileInsideWithoutSymlinks(projectRoot, databasePath, database.export());
316
- database.close();
313
+ try {
314
+ createSchema(database, capabilities);
315
+ populateDatabase(database, capabilities, documents, skills, skillRoutes, commandIntents, sourceAnchors, indexedFiles, verificationEvidence, indexMode, sourceScopeHash, includeSource, new Date().toISOString());
316
+ writeFileInsideWithoutSymlinks(projectRoot, databasePath, database.export());
317
+ }
318
+ finally {
319
+ database.close();
320
+ }
317
321
  }
318
322
  return {
319
323
  schema_version: LOCAL_INDEX_SCHEMA_VERSION,
@@ -46,11 +46,25 @@ function populatePathSurfaceReadModel(database) {
46
46
  insertPathSurfaceReasons(database, rule.id, 'drift_check', rule.surface.driftChecks);
47
47
  }
48
48
  }
49
+ function readDocumentTermsByPath(database) {
50
+ const termsByPath = new Map();
51
+ for (const row of queryRows(database, 'SELECT document_path, term FROM document_terms ORDER BY document_path, term')) {
52
+ const documentPath = toSearchString(row.document_path);
53
+ const term = toSearchString(row.term);
54
+ const terms = termsByPath.get(documentPath);
55
+ if (terms) {
56
+ terms.push(term);
57
+ }
58
+ else {
59
+ termsByPath.set(documentPath, [term]);
60
+ }
61
+ }
62
+ return termsByPath;
63
+ }
49
64
  function populateSearchTables(database, capabilities, documents, skills, skillRoutes, commandIntents, sourceAnchors) {
65
+ const documentTermsByPath = readDocumentTermsByPath(database);
50
66
  for (const document of documents) {
51
- const documentTerms = queryRows(database, 'SELECT term FROM document_terms WHERE document_path = ? ORDER BY term', [
52
- document.path,
53
- ]).map((row) => toSearchString(row.term));
67
+ const documentTerms = documentTermsByPath.get(document.path) ?? [];
54
68
  insertSearchNgrams(database, 'document', document.path, [
55
69
  document.path,
56
70
  document.type,
@@ -299,6 +299,7 @@ function collectBoundedDirectSearchDocuments(projectRoot) {
299
299
  }
300
300
  function searchLocalWorkflowFilesDirectly(projectRoot, databasePath, normalizedQuery, limit, scope) {
301
301
  const cacheLayers = readCacheLayerSets(projectRoot);
302
+ const querySnippetNgrams = buildSearchNgrams([normalizedQuery]);
302
303
  const results = [];
303
304
  if (scope === 'workflow' || scope === 'all') {
304
305
  const documents = collectBoundedDirectSearchDocuments(projectRoot);
@@ -322,7 +323,7 @@ function searchLocalWorkflowFilesDirectly(projectRoot, databasePath, normalizedQ
322
323
  title: document.title,
323
324
  document_type: document.type,
324
325
  ...workflowAuthorityForDocument(document.type),
325
- match: getMatchSnippet(fields, normalizedQuery),
326
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
326
327
  score: scoreMatch(primaryFields, secondaryFields, normalizedQuery),
327
328
  }, cacheLayers));
328
329
  }
@@ -337,7 +338,7 @@ function searchLocalWorkflowFilesDirectly(projectRoot, databasePath, normalizedQ
337
338
  path: skill.path,
338
339
  title: skill.title,
339
340
  ...skillAuthority(),
340
- match: getMatchSnippet(fields, normalizedQuery),
341
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
341
342
  score: scoreMatch(fields, [], normalizedQuery),
342
343
  }, cacheLayers));
343
344
  }
@@ -393,6 +394,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
393
394
  const stalePaths = getStalePaths(projectRoot, database);
394
395
  capabilities = readStoredSearchCapabilities(database);
395
396
  const indexedMatches = getIndexedSearchMatches(database, normalizedQuery);
397
+ const querySnippetNgrams = buildSearchNgrams([normalizedQuery]);
396
398
  if (stalePaths.length > 0) {
397
399
  throw new Error(`Local mustflow index is stale: ${stalePaths.join(', ')}. Run \`mf index\` before searching. Refresh command: mf index`);
398
400
  }
@@ -419,7 +421,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
419
421
  title,
420
422
  document_type: typeValue,
421
423
  ...workflowAuthorityForDocument(typeValue),
422
- match: getMatchSnippet(fields, normalizedQuery),
424
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
423
425
  score: scoreIndexedOrTableScan(primaryFields, secondaryFields, normalizedQuery, indexedMatches, indexedMatches.documents, pathValue),
424
426
  }, cacheLayers));
425
427
  }
@@ -437,7 +439,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
437
439
  path: pathValue,
438
440
  title,
439
441
  ...skillAuthority(),
440
- match: getMatchSnippet(fields, normalizedQuery),
442
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
441
443
  score: scoreIndexedOrTableScan([name, pathValue, title], [], normalizedQuery, indexedMatches, indexedMatches.skills, name),
442
444
  }, cacheLayers));
443
445
  }
@@ -468,7 +470,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
468
470
  route_risk: risk,
469
471
  verification_intents: verificationIntents,
470
472
  ...skillAuthority(),
471
- match: getMatchSnippet(fields, normalizedQuery),
473
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
472
474
  score: indexedRouteMatch
473
475
  ? Math.max(scoreMatch(primaryFields, secondaryFields, normalizedQuery), 20)
474
476
  : scoreMatch(primaryFields, secondaryFields, normalizedQuery),
@@ -502,7 +504,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
502
504
  effect_paths: effectPaths,
503
505
  effect_modes: effectModes,
504
506
  ...commandIntentAuthority(),
505
- match: getMatchSnippet(fields, normalizedQuery),
507
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
506
508
  score: scoreIndexedOrTableScan(primaryFields, secondaryFields, normalizedQuery, indexedMatches, indexedMatches.commandIntents, name),
507
509
  }, cacheLayers));
508
510
  }
@@ -532,7 +534,7 @@ export async function searchLocalIndex(projectRoot, query, options = {}) {
532
534
  ...sourceAnchorAuthority(),
533
535
  stale_status: toSearchString(row.status),
534
536
  stale_confidence: Number(row.confidence),
535
- match: getMatchSnippet(fields, normalizedQuery),
537
+ match: getMatchSnippet(fields, normalizedQuery, querySnippetNgrams),
536
538
  score: scoreIndexedOrTableScan(primaryFields, secondaryFields, normalizedQuery, indexedMatches, indexedMatches.sourceAnchors, id),
537
539
  }, cacheLayers));
538
540
  }
@@ -28,13 +28,13 @@ export function buildSearchNgrams(values) {
28
28
  }
29
29
  return [...grams].sort((left, right) => left.localeCompare(right));
30
30
  }
31
- export function getMatchSnippet(fields, query) {
31
+ export function getMatchSnippet(fields, query, queryNgrams = buildSearchNgrams([query])) {
32
32
  const normalized = normalizeSearchText(fields.join(' '));
33
33
  const lower = normalized.toLowerCase();
34
34
  let start = lower.indexOf(query.toLowerCase());
35
35
  let matchLength = query.length;
36
36
  if (start === -1) {
37
- const [firstGram] = buildSearchNgrams([query]).filter((gram) => lower.includes(gram));
37
+ const firstGram = queryNgrams.find((gram) => lower.includes(gram));
38
38
  if (!firstGram) {
39
39
  return truncateSearchMatchSnippet(normalized);
40
40
  }
@@ -65,12 +65,27 @@ function parseFrontmatter(content) {
65
65
  if (!content.startsWith('---')) {
66
66
  return {};
67
67
  }
68
- const end = content.indexOf('\n---', 3);
68
+ const firstLineEnd = content.indexOf('\n');
69
+ if (firstLineEnd === -1) {
70
+ return {};
71
+ }
72
+ let end = -1;
73
+ let lineStart = firstLineEnd + 1;
74
+ while (lineStart < content.length) {
75
+ const lineEnd = content.indexOf('\n', lineStart);
76
+ const nextLineStart = lineEnd === -1 ? content.length : lineEnd + 1;
77
+ const line = content.slice(lineStart, lineEnd === -1 ? content.length : lineEnd).replace(/\r$/u, '');
78
+ if (line.trim() === '---') {
79
+ end = lineStart;
80
+ break;
81
+ }
82
+ lineStart = nextLineStart;
83
+ }
69
84
  if (end === -1) {
70
85
  return {};
71
86
  }
72
87
  const result = {};
73
- const rawFrontmatter = content.slice(3, end);
88
+ const rawFrontmatter = content.slice(firstLineEnd + 1, end);
74
89
  for (const line of rawFrontmatter.split(/\r?\n/)) {
75
90
  const separatorIndex = line.indexOf(':');
76
91
  if (separatorIndex === -1) {
@@ -5,7 +5,13 @@ export const MUSTFLOW_TEXT_MAX_BYTES = 1024 * 1024;
5
5
  export const MUSTFLOW_TOML_MAX_BYTES = 256 * 1024;
6
6
  export const MUSTFLOW_JSON_MAX_BYTES = 1024 * 1024;
7
7
  export function mustflowProjectPath(projectRoot, relativePath) {
8
- return path.join(projectRoot, ...relativePath.split('/'));
8
+ const normalizedRoot = path.resolve(projectRoot);
9
+ const resolvedPath = path.resolve(normalizedRoot, ...relativePath.split('/'));
10
+ const relative = path.relative(normalizedRoot, resolvedPath);
11
+ if (relative.startsWith('..') || path.isAbsolute(relative)) {
12
+ throw new Error(`Path escapes mustflow project root: ${relativePath}`);
13
+ }
14
+ return resolvedPath;
9
15
  }
10
16
  function missingPath(error) {
11
17
  return error instanceof Error && 'code' in error && error.code === 'ENOENT';
@@ -14,7 +20,13 @@ export function readMustflowTextFile(projectRoot, relativePath, options = {}) {
14
20
  return readUtf8FileInsideWithoutSymlinks(projectRoot, mustflowProjectPath(projectRoot, relativePath), { maxBytes: options.maxBytes ?? MUSTFLOW_TEXT_MAX_BYTES });
15
21
  }
16
22
  export function readMustflowTextFileResult(projectRoot, relativePath, options = {}) {
17
- const filePath = mustflowProjectPath(projectRoot, relativePath);
23
+ let filePath;
24
+ try {
25
+ filePath = mustflowProjectPath(projectRoot, relativePath);
26
+ }
27
+ catch (error) {
28
+ return { ok: false, exists: false, error: error instanceof Error ? error.message : String(error) };
29
+ }
18
30
  if (!existsSync(filePath)) {
19
31
  return { ok: false, exists: false, error: null };
20
32
  }
@@ -150,6 +150,42 @@ function buildLatestPackageUrl(registryUrl, packageName) {
150
150
  : encodeURIComponent(packageName);
151
151
  return `${trimmedRegistryUrl}/${encodedPackageName}/latest`;
152
152
  }
153
+ function buildPackageMetadataUrl(registryUrl, packageName) {
154
+ const trimmedRegistryUrl = registryUrl.replace(/\/+$/u, '');
155
+ const encodedPackageName = packageName.startsWith('@')
156
+ ? `@${encodeURIComponent(packageName.slice(1))}`
157
+ : encodeURIComponent(packageName);
158
+ return `${trimmedRegistryUrl}/${encodedPackageName}`;
159
+ }
160
+ export async function checkNpmPackageExists(packageName) {
161
+ const registryUrl = getRegistryUrl();
162
+ const response = await fetch(buildPackageMetadataUrl(registryUrl, packageName), {
163
+ headers: { accept: 'application/json' },
164
+ signal: AbortSignal.timeout(getTimeoutMs()),
165
+ });
166
+ if (response.status === 404) {
167
+ return {
168
+ packageName,
169
+ registryUrl,
170
+ exists: false,
171
+ resolvedName: null,
172
+ };
173
+ }
174
+ if (!response.ok) {
175
+ throw new Error(`npm registry returned HTTP ${response.status}`);
176
+ }
177
+ const body = await response.json();
178
+ const resolvedName = isRecord(body) && typeof body.name === 'string' ? body.name : null;
179
+ if (!resolvedName) {
180
+ throw new Error('npm registry response did not include a package name');
181
+ }
182
+ return {
183
+ packageName,
184
+ registryUrl,
185
+ exists: true,
186
+ resolvedName,
187
+ };
188
+ }
153
189
  export async function checkNpmLatestVersion(metadata) {
154
190
  const registryUrl = getRegistryUrl();
155
191
  const response = await fetch(buildLatestPackageUrl(registryUrl, metadata.name), {
@@ -193,6 +193,14 @@ const EXACT_ANCHOR_DESCRIPTIONS = new Map([
193
193
  ['.mustflow/docs/agent-workflow.md', 'Shared workflow policy for agent work.'],
194
194
  ['.mustflow/skills/INDEX.md', 'Index of available procedural skills.'],
195
195
  ]);
196
+ function spawnGitLsFiles(command, args, options) {
197
+ const result = spawnSync(command, [...args], options);
198
+ return {
199
+ status: result.status,
200
+ error: result.error,
201
+ stdout: result.stdout,
202
+ };
203
+ }
196
204
  function getStringArray(value) {
197
205
  return Array.isArray(value) ? value.filter((entry) => typeof entry === 'string') : [];
198
206
  }
@@ -250,8 +258,7 @@ function classifyGitLsFilesFailure(result) {
250
258
  return 'error';
251
259
  }
252
260
  export function discoverGitFilesForRepoMap(projectRoot, options = {}) {
253
- const spawnGit = options.spawnGit ??
254
- ((command, args, spawnOptions) => spawnSync(command, [...args], spawnOptions));
261
+ const spawnGit = options.spawnGit ?? spawnGitLsFiles;
255
262
  const result = spawnGit('git', ['ls-files', '-z'], {
256
263
  cwd: projectRoot,
257
264
  encoding: 'utf8',
@@ -413,7 +420,13 @@ function renderDirectoryAnchors(anchors) {
413
420
  return lines;
414
421
  }
415
422
  function hasGitMarker(directoryPath) {
416
- return existsSync(path.join(directoryPath, '.git'));
423
+ try {
424
+ const marker = lstatSync(path.join(directoryPath, '.git'));
425
+ return marker.isDirectory() || marker.isFile();
426
+ }
427
+ catch {
428
+ return false;
429
+ }
417
430
  }
418
431
  function isRealPathInside(parentRealPath, childRealPath) {
419
432
  const relative = path.relative(parentRealPath, childRealPath);
@@ -304,11 +304,12 @@ export function getTemplateFiles(template, locale = template.manifest.defaultLoc
304
304
  const commonPath = path.join(commonRoot, ...relativePath.split('/'));
305
305
  const localizedPath = localePath && existsSync(localePath) ? localePath : undefined;
306
306
  const fallbackLocalePath = sourceLocalePath && existsSync(sourceLocalePath) ? sourceLocalePath : undefined;
307
- const indexSourcePath = localizedPath ?? fallbackLocalePath ?? commonPath;
308
- const content = relativePath === '.mustflow/skills/INDEX.md'
309
- ? filterSkillIndexContent(readFileSync(indexSourcePath, 'utf8'), selectedSkills)
310
- : relativePath === '.mustflow/skills/routes.toml'
311
- ? filterSkillRouteMetadataContent(readFileSync(indexSourcePath, 'utf8'), selectedSkills)
307
+ const commonSourcePath = existsSync(commonPath) ? commonPath : undefined;
308
+ const selectedSourcePath = localizedPath ?? fallbackLocalePath ?? commonSourcePath;
309
+ const content = selectedSourcePath && relativePath === '.mustflow/skills/INDEX.md'
310
+ ? filterSkillIndexContent(readFileSync(selectedSourcePath, 'utf8'), selectedSkills)
311
+ : selectedSourcePath && relativePath === '.mustflow/skills/routes.toml'
312
+ ? filterSkillRouteMetadataContent(readFileSync(selectedSourcePath, 'utf8'), selectedSkills)
312
313
  : undefined;
313
314
  if (localizedPath) {
314
315
  return {
@@ -326,10 +327,10 @@ export function getTemplateFiles(template, locale = template.manifest.defaultLoc
326
327
  content,
327
328
  };
328
329
  }
329
- if (existsSync(commonPath)) {
330
+ if (commonSourcePath) {
330
331
  return {
331
332
  relativePath,
332
- sourcePath: commonPath,
333
+ sourcePath: commonSourcePath,
333
334
  sourceKind: template.manifest.localesRoot ? 'common' : 'legacy',
334
335
  content,
335
336
  };
@@ -172,7 +172,7 @@ export const ALLOWED_TRANSLATION_POLICIES = new Set([
172
172
  'update_source_mark_targets_stale',
173
173
  'machine_translate_requires_review',
174
174
  ]);
175
- export const RAW_COMMAND_FENCE_PATTERN = /```(?:sh|bash|shell|zsh|powershell|ps1|cmd)\s+[\s\S]*?```/giu;
175
+ export const RAW_COMMAND_FENCE_PATTERN = /```(?:sh|bash|shell|zsh|powershell|ps1|cmd)\s+[\s\S]*?```/iu;
176
176
  export const SKILL_COMMAND_PERMISSION_CLAIM_PATTERNS = [
177
177
  /\b(?:this\s+skill|skill\s+documents?|skills?)\s+(?:authorizes?|grants?|allows?|permits?)\s+(?:agents?\s+)?(?:to\s+)?(?:run|execute)\b/iu,
178
178
  /\b(?:agents?\s+)?(?:may|can|are\s+allowed\s+to|is\s+allowed\s+to|is\s+permitted\s+to|have\s+permission\s+to)\s+(?:run|execute)\s+(?:raw\s+)?(?:shell\s+)?commands?\b/iu,